2012-09-26 Jan Kratochvil <jan.kratochvil@redhat.com>
[binutils-gdb.git] / gdb / dwarf2read.c
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 "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67 #include "gdb_bfd.h"
68 #include "f-lang.h"
69
70 #include <fcntl.h>
71 #include "gdb_string.h"
72 #include "gdb_assert.h"
73 #include <sys/types.h>
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 /* When non-zero, print basic high level tracing messages.
79 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
80 static int dwarf2_read_debug = 0;
81
82 /* When non-zero, dump DIEs after they are read in. */
83 static unsigned int dwarf2_die_debug = 0;
84
85 /* When non-zero, cross-check physname against demangler. */
86 static int check_physname = 0;
87
88 /* When non-zero, do not reject deprecated .gdb_index sections. */
89 static int use_deprecated_index_sections = 0;
90
91 /* When set, the file that we're processing is known to have debugging
92 info for C++ namespaces. GCC 3.3.x did not produce this information,
93 but later versions do. */
94
95 static int processing_has_namespace_info;
96
97 static const struct objfile_data *dwarf2_objfile_data_key;
98
99 struct dwarf2_section_info
100 {
101 asection *asection;
102 gdb_byte *buffer;
103 bfd_size_type size;
104 /* True if we have tried to read this section. */
105 int readin;
106 };
107
108 typedef struct dwarf2_section_info dwarf2_section_info_def;
109 DEF_VEC_O (dwarf2_section_info_def);
110
111 /* All offsets in the index are of this type. It must be
112 architecture-independent. */
113 typedef uint32_t offset_type;
114
115 DEF_VEC_I (offset_type);
116
117 /* Ensure only legit values are used. */
118 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
119 do { \
120 gdb_assert ((unsigned int) (value) <= 1); \
121 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
122 } while (0)
123
124 /* Ensure only legit values are used. */
125 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
126 do { \
127 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
128 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
129 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
130 } while (0)
131
132 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
133 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
134 do { \
135 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
136 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
137 } while (0)
138
139 /* A description of the mapped index. The file format is described in
140 a comment by the code that writes the index. */
141 struct mapped_index
142 {
143 /* Index data format version. */
144 int version;
145
146 /* The total length of the buffer. */
147 off_t total_size;
148
149 /* A pointer to the address table data. */
150 const gdb_byte *address_table;
151
152 /* Size of the address table data in bytes. */
153 offset_type address_table_size;
154
155 /* The symbol table, implemented as a hash table. */
156 const offset_type *symbol_table;
157
158 /* Size in slots, each slot is 2 offset_types. */
159 offset_type symbol_table_slots;
160
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
166 DEF_VEC_P (dwarf2_per_cu_ptr);
167
168 /* Collection of data recorded per objfile.
169 This hangs off of dwarf2_objfile_data_key. */
170
171 struct dwarf2_per_objfile
172 {
173 struct dwarf2_section_info info;
174 struct dwarf2_section_info abbrev;
175 struct dwarf2_section_info line;
176 struct dwarf2_section_info loc;
177 struct dwarf2_section_info macinfo;
178 struct dwarf2_section_info macro;
179 struct dwarf2_section_info str;
180 struct dwarf2_section_info ranges;
181 struct dwarf2_section_info addr;
182 struct dwarf2_section_info frame;
183 struct dwarf2_section_info eh_frame;
184 struct dwarf2_section_info gdb_index;
185
186 VEC (dwarf2_section_info_def) *types;
187
188 /* Back link. */
189 struct objfile *objfile;
190
191 /* Table of all the compilation units. This is used to locate
192 the target compilation unit of a particular reference. */
193 struct dwarf2_per_cu_data **all_comp_units;
194
195 /* The number of compilation units in ALL_COMP_UNITS. */
196 int n_comp_units;
197
198 /* The number of .debug_types-related CUs. */
199 int n_type_units;
200
201 /* The .debug_types-related CUs (TUs). */
202 struct signatured_type **all_type_units;
203
204 /* The number of entries in all_type_unit_groups. */
205 int n_type_unit_groups;
206
207 /* Table of type unit groups.
208 This exists to make it easy to iterate over all CUs and TU groups. */
209 struct type_unit_group **all_type_unit_groups;
210
211 /* Table of struct type_unit_group objects.
212 The hash key is the DW_AT_stmt_list value. */
213 htab_t type_unit_groups;
214
215 /* A table mapping .debug_types signatures to its signatured_type entry.
216 This is NULL if the .debug_types section hasn't been read in yet. */
217 htab_t signatured_types;
218
219 /* Type unit statistics, to see how well the scaling improvements
220 are doing. */
221 struct tu_stats
222 {
223 int nr_uniq_abbrev_tables;
224 int nr_symtabs;
225 int nr_symtab_sharers;
226 int nr_stmt_less_type_units;
227 } tu_stats;
228
229 /* A chain of compilation units that are currently read in, so that
230 they can be freed later. */
231 struct dwarf2_per_cu_data *read_in_chain;
232
233 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
234 This is NULL if the table hasn't been allocated yet. */
235 htab_t dwo_files;
236
237 /* The shared '.dwz' file, if one exists. This is used when the
238 original data was compressed using 'dwz -m'. */
239 struct dwz_file *dwz_file;
240
241 /* A flag indicating wether this objfile has a section loaded at a
242 VMA of 0. */
243 int has_section_at_zero;
244
245 /* True if we are using the mapped index,
246 or we are faking it for OBJF_READNOW's sake. */
247 unsigned char using_index;
248
249 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
250 struct mapped_index *index_table;
251
252 /* When using index_table, this keeps track of all quick_file_names entries.
253 TUs can share line table entries with CUs or other TUs, and there can be
254 a lot more TUs than unique line tables, so we maintain a separate table
255 of all line table entries to support the sharing. */
256 htab_t quick_file_names_table;
257
258 /* Set during partial symbol reading, to prevent queueing of full
259 symbols. */
260 int reading_partial_symbols;
261
262 /* Table mapping type DIEs to their struct type *.
263 This is NULL if not allocated yet.
264 The mapping is done via (CU/TU signature + DIE offset) -> type. */
265 htab_t die_type_hash;
266
267 /* The CUs we recently read. */
268 VEC (dwarf2_per_cu_ptr) *just_read_cus;
269 };
270
271 static struct dwarf2_per_objfile *dwarf2_per_objfile;
272
273 /* Default names of the debugging sections. */
274
275 /* Note that if the debugging section has been compressed, it might
276 have a name like .zdebug_info. */
277
278 static const struct dwarf2_debug_sections dwarf2_elf_names =
279 {
280 { ".debug_info", ".zdebug_info" },
281 { ".debug_abbrev", ".zdebug_abbrev" },
282 { ".debug_line", ".zdebug_line" },
283 { ".debug_loc", ".zdebug_loc" },
284 { ".debug_macinfo", ".zdebug_macinfo" },
285 { ".debug_macro", ".zdebug_macro" },
286 { ".debug_str", ".zdebug_str" },
287 { ".debug_ranges", ".zdebug_ranges" },
288 { ".debug_types", ".zdebug_types" },
289 { ".debug_addr", ".zdebug_addr" },
290 { ".debug_frame", ".zdebug_frame" },
291 { ".eh_frame", NULL },
292 { ".gdb_index", ".zgdb_index" },
293 23
294 };
295
296 /* List of DWO sections. */
297
298 static const struct dwo_section_names
299 {
300 struct dwarf2_section_names abbrev_dwo;
301 struct dwarf2_section_names info_dwo;
302 struct dwarf2_section_names line_dwo;
303 struct dwarf2_section_names loc_dwo;
304 struct dwarf2_section_names macinfo_dwo;
305 struct dwarf2_section_names macro_dwo;
306 struct dwarf2_section_names str_dwo;
307 struct dwarf2_section_names str_offsets_dwo;
308 struct dwarf2_section_names types_dwo;
309 }
310 dwo_section_names =
311 {
312 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
313 { ".debug_info.dwo", ".zdebug_info.dwo" },
314 { ".debug_line.dwo", ".zdebug_line.dwo" },
315 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
316 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
317 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
318 { ".debug_str.dwo", ".zdebug_str.dwo" },
319 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
320 { ".debug_types.dwo", ".zdebug_types.dwo" },
321 };
322
323 /* local data types */
324
325 /* The data in a compilation unit header, after target2host
326 translation, looks like this. */
327 struct comp_unit_head
328 {
329 unsigned int length;
330 short version;
331 unsigned char addr_size;
332 unsigned char signed_addr_p;
333 sect_offset abbrev_offset;
334
335 /* Size of file offsets; either 4 or 8. */
336 unsigned int offset_size;
337
338 /* Size of the length field; either 4 or 12. */
339 unsigned int initial_length_size;
340
341 /* Offset to the first byte of this compilation unit header in the
342 .debug_info section, for resolving relative reference dies. */
343 sect_offset offset;
344
345 /* Offset to first die in this cu from the start of the cu.
346 This will be the first byte following the compilation unit header. */
347 cu_offset first_die_offset;
348 };
349
350 /* Type used for delaying computation of method physnames.
351 See comments for compute_delayed_physnames. */
352 struct delayed_method_info
353 {
354 /* The type to which the method is attached, i.e., its parent class. */
355 struct type *type;
356
357 /* The index of the method in the type's function fieldlists. */
358 int fnfield_index;
359
360 /* The index of the method in the fieldlist. */
361 int index;
362
363 /* The name of the DIE. */
364 const char *name;
365
366 /* The DIE associated with this method. */
367 struct die_info *die;
368 };
369
370 typedef struct delayed_method_info delayed_method_info;
371 DEF_VEC_O (delayed_method_info);
372
373 /* Internal state when decoding a particular compilation unit. */
374 struct dwarf2_cu
375 {
376 /* The objfile containing this compilation unit. */
377 struct objfile *objfile;
378
379 /* The header of the compilation unit. */
380 struct comp_unit_head header;
381
382 /* Base address of this compilation unit. */
383 CORE_ADDR base_address;
384
385 /* Non-zero if base_address has been set. */
386 int base_known;
387
388 /* The language we are debugging. */
389 enum language language;
390 const struct language_defn *language_defn;
391
392 const char *producer;
393
394 /* The generic symbol table building routines have separate lists for
395 file scope symbols and all all other scopes (local scopes). So
396 we need to select the right one to pass to add_symbol_to_list().
397 We do it by keeping a pointer to the correct list in list_in_scope.
398
399 FIXME: The original dwarf code just treated the file scope as the
400 first local scope, and all other local scopes as nested local
401 scopes, and worked fine. Check to see if we really need to
402 distinguish these in buildsym.c. */
403 struct pending **list_in_scope;
404
405 /* The abbrev table for this CU.
406 Normally this points to the abbrev table in the objfile.
407 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
408 struct abbrev_table *abbrev_table;
409
410 /* Hash table holding all the loaded partial DIEs
411 with partial_die->offset.SECT_OFF as hash. */
412 htab_t partial_dies;
413
414 /* Storage for things with the same lifetime as this read-in compilation
415 unit, including partial DIEs. */
416 struct obstack comp_unit_obstack;
417
418 /* When multiple dwarf2_cu structures are living in memory, this field
419 chains them all together, so that they can be released efficiently.
420 We will probably also want a generation counter so that most-recently-used
421 compilation units are cached... */
422 struct dwarf2_per_cu_data *read_in_chain;
423
424 /* Backchain to our per_cu entry if the tree has been built. */
425 struct dwarf2_per_cu_data *per_cu;
426
427 /* How many compilation units ago was this CU last referenced? */
428 int last_used;
429
430 /* A hash table of DIE cu_offset for following references with
431 die_info->offset.sect_off as hash. */
432 htab_t die_hash;
433
434 /* Full DIEs if read in. */
435 struct die_info *dies;
436
437 /* A set of pointers to dwarf2_per_cu_data objects for compilation
438 units referenced by this one. Only set during full symbol processing;
439 partial symbol tables do not have dependencies. */
440 htab_t dependencies;
441
442 /* Header data from the line table, during full symbol processing. */
443 struct line_header *line_header;
444
445 /* A list of methods which need to have physnames computed
446 after all type information has been read. */
447 VEC (delayed_method_info) *method_list;
448
449 /* To be copied to symtab->call_site_htab. */
450 htab_t call_site_htab;
451
452 /* Non-NULL if this CU came from a DWO file.
453 There is an invariant here that is important to remember:
454 Except for attributes copied from the top level DIE in the "main"
455 (or "stub") file in preparation for reading the DWO file
456 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
457 Either there isn't a DWO file (in which case this is NULL and the point
458 is moot), or there is and either we're not going to read it (in which
459 case this is NULL) or there is and we are reading it (in which case this
460 is non-NULL). */
461 struct dwo_unit *dwo_unit;
462
463 /* The DW_AT_addr_base attribute if present, zero otherwise
464 (zero is a valid value though).
465 Note this value comes from the stub CU/TU's DIE. */
466 ULONGEST addr_base;
467
468 /* The DW_AT_ranges_base attribute if present, zero otherwise
469 (zero is a valid value though).
470 Note this value comes from the stub CU/TU's DIE.
471 Also note that the value is zero in the non-DWO case so this value can
472 be used without needing to know whether DWO files are in use or not. */
473 ULONGEST ranges_base;
474
475 /* Mark used when releasing cached dies. */
476 unsigned int mark : 1;
477
478 /* This CU references .debug_loc. See the symtab->locations_valid field.
479 This test is imperfect as there may exist optimized debug code not using
480 any location list and still facing inlining issues if handled as
481 unoptimized code. For a future better test see GCC PR other/32998. */
482 unsigned int has_loclist : 1;
483
484 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
485 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
486 are valid. This information is cached because profiling CU expansion
487 showed excessive time spent in producer_is_gxx_lt_4_6. */
488 unsigned int checked_producer : 1;
489 unsigned int producer_is_gxx_lt_4_6 : 1;
490 unsigned int producer_is_icc : 1;
491 };
492
493 /* Persistent data held for a compilation unit, even when not
494 processing it. We put a pointer to this structure in the
495 read_symtab_private field of the psymtab. */
496
497 struct dwarf2_per_cu_data
498 {
499 /* The start offset and length of this compilation unit.
500 NOTE: Unlike comp_unit_head.length, this length includes
501 initial_length_size.
502 If the DIE refers to a DWO file, this is always of the original die,
503 not the DWO file. */
504 sect_offset offset;
505 unsigned int length;
506
507 /* Flag indicating this compilation unit will be read in before
508 any of the current compilation units are processed. */
509 unsigned int queued : 1;
510
511 /* This flag will be set when reading partial DIEs if we need to load
512 absolutely all DIEs for this compilation unit, instead of just the ones
513 we think are interesting. It gets set if we look for a DIE in the
514 hash table and don't find it. */
515 unsigned int load_all_dies : 1;
516
517 /* Non-zero if this CU is from .debug_types. */
518 unsigned int is_debug_types : 1;
519
520 /* Non-zero if this CU is from the .dwz file. */
521 unsigned int is_dwz : 1;
522
523 /* The section this CU/TU lives in.
524 If the DIE refers to a DWO file, this is always the original die,
525 not the DWO file. */
526 struct dwarf2_section_info *info_or_types_section;
527
528 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
529 of the CU cache it gets reset to NULL again. */
530 struct dwarf2_cu *cu;
531
532 /* The corresponding objfile.
533 Normally we can get the objfile from dwarf2_per_objfile.
534 However we can enter this file with just a "per_cu" handle. */
535 struct objfile *objfile;
536
537 /* When using partial symbol tables, the 'psymtab' field is active.
538 Otherwise the 'quick' field is active. */
539 union
540 {
541 /* The partial symbol table associated with this compilation unit,
542 or NULL for unread partial units. */
543 struct partial_symtab *psymtab;
544
545 /* Data needed by the "quick" functions. */
546 struct dwarf2_per_cu_quick_data *quick;
547 } v;
548
549 union
550 {
551 /* The CUs we import using DW_TAG_imported_unit. This is filled in
552 while reading psymtabs, used to compute the psymtab dependencies,
553 and then cleared. Then it is filled in again while reading full
554 symbols, and only deleted when the objfile is destroyed. */
555 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
556
557 /* Type units are grouped by their DW_AT_stmt_list entry so that they
558 can share them. If this is a TU, this points to the containing
559 symtab. */
560 struct type_unit_group *type_unit_group;
561 } s;
562 };
563
564 /* Entry in the signatured_types hash table. */
565
566 struct signatured_type
567 {
568 /* The "per_cu" object of this type.
569 N.B.: This is the first member so that it's easy to convert pointers
570 between them. */
571 struct dwarf2_per_cu_data per_cu;
572
573 /* The type's signature. */
574 ULONGEST signature;
575
576 /* Offset in the TU of the type's DIE, as read from the TU header.
577 If the definition lives in a DWO file, this value is unusable. */
578 cu_offset type_offset_in_tu;
579
580 /* Offset in the section of the type's DIE.
581 If the definition lives in a DWO file, this is the offset in the
582 .debug_types.dwo section.
583 The value is zero until the actual value is known.
584 Zero is otherwise not a valid section offset. */
585 sect_offset type_offset_in_section;
586 };
587
588 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
589 This includes type_unit_group and quick_file_names. */
590
591 struct stmt_list_hash
592 {
593 /* The DWO unit this table is from or NULL if there is none. */
594 struct dwo_unit *dwo_unit;
595
596 /* Offset in .debug_line or .debug_line.dwo. */
597 sect_offset line_offset;
598 };
599
600 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
601 an object of this type. */
602
603 struct type_unit_group
604 {
605 /* dwarf2read.c's main "handle" on the symtab.
606 To simplify things we create an artificial CU that "includes" all the
607 type units using this stmt_list so that the rest of the code still has
608 a "per_cu" handle on the symtab.
609 This PER_CU is recognized by having no section. */
610 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
611 struct dwarf2_per_cu_data per_cu;
612
613 union
614 {
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
618 VEC (dwarf2_per_cu_ptr) *tus;
619
620 /* When reading the line table in "quick" functions, we need a real TU.
621 Any will do, we know they all share the same DW_AT_stmt_list entry.
622 For simplicity's sake, we pick the first one. */
623 struct dwarf2_per_cu_data *first_tu;
624 } t;
625
626 /* The primary symtab.
627 Type units in a group needn't all be defined in the same source file,
628 so we create an essentially anonymous symtab as the primary symtab. */
629 struct symtab *primary_symtab;
630
631 /* The data used to construct the hash key. */
632 struct stmt_list_hash hash;
633
634 /* The number of symtabs from the line header.
635 The value here must match line_header.num_file_names. */
636 unsigned int num_symtabs;
637
638 /* The symbol tables for this TU (obtained from the files listed in
639 DW_AT_stmt_list).
640 WARNING: The order of entries here must match the order of entries
641 in the line header. After the first TU using this type_unit_group, the
642 line header for the subsequent TUs is recreated from this. This is done
643 because we need to use the same symtabs for each TU using the same
644 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
645 there's no guarantee the line header doesn't have duplicate entries. */
646 struct symtab **symtabs;
647 };
648
649 /* These sections are what may appear in a "dwo" file. */
650
651 struct dwo_sections
652 {
653 struct dwarf2_section_info abbrev;
654 struct dwarf2_section_info info;
655 struct dwarf2_section_info line;
656 struct dwarf2_section_info loc;
657 struct dwarf2_section_info macinfo;
658 struct dwarf2_section_info macro;
659 struct dwarf2_section_info str;
660 struct dwarf2_section_info str_offsets;
661 VEC (dwarf2_section_info_def) *types;
662 };
663
664 /* Common bits of DWO CUs/TUs. */
665
666 struct dwo_unit
667 {
668 /* Backlink to the containing struct dwo_file. */
669 struct dwo_file *dwo_file;
670
671 /* The "id" that distinguishes this CU/TU.
672 .debug_info calls this "dwo_id", .debug_types calls this "signature".
673 Since signatures came first, we stick with it for consistency. */
674 ULONGEST signature;
675
676 /* The section this CU/TU lives in, in the DWO file. */
677 struct dwarf2_section_info *info_or_types_section;
678
679 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
680 sect_offset offset;
681 unsigned int length;
682
683 /* For types, offset in the type's DIE of the type defined by this TU. */
684 cu_offset type_offset_in_tu;
685 };
686
687 /* Data for one DWO file. */
688
689 struct dwo_file
690 {
691 /* The DW_AT_GNU_dwo_name attribute.
692 We don't manage space for this, it's an attribute. */
693 const char *dwo_name;
694
695 /* The bfd, when the file is open. Otherwise this is NULL. */
696 bfd *dwo_bfd;
697
698 /* Section info for this file. */
699 struct dwo_sections sections;
700
701 /* Table of CUs in the file.
702 Each element is a struct dwo_unit. */
703 htab_t cus;
704
705 /* Table of TUs in the file.
706 Each element is a struct dwo_unit. */
707 htab_t tus;
708 };
709
710 /* This represents a '.dwz' file. */
711
712 struct dwz_file
713 {
714 /* A dwz file can only contain a few sections. */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info str;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info macro;
720 struct dwarf2_section_info gdb_index;
721
722 /* The dwz's BFD. */
723 bfd *dwz_bfd;
724 };
725
726 /* Struct used to pass misc. parameters to read_die_and_children, et
727 al. which are used for both .debug_info and .debug_types dies.
728 All parameters here are unchanging for the life of the call. This
729 struct exists to abstract away the constant parameters of die reading. */
730
731 struct die_reader_specs
732 {
733 /* die_section->asection->owner. */
734 bfd* abfd;
735
736 /* The CU of the DIE we are parsing. */
737 struct dwarf2_cu *cu;
738
739 /* Non-NULL if reading a DWO file. */
740 struct dwo_file *dwo_file;
741
742 /* The section the die comes from.
743 This is either .debug_info or .debug_types, or the .dwo variants. */
744 struct dwarf2_section_info *die_section;
745
746 /* die_section->buffer. */
747 gdb_byte *buffer;
748
749 /* The end of the buffer. */
750 const gdb_byte *buffer_end;
751 };
752
753 /* Type of function passed to init_cutu_and_read_dies, et.al. */
754 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
755 gdb_byte *info_ptr,
756 struct die_info *comp_unit_die,
757 int has_children,
758 void *data);
759
760 /* The line number information for a compilation unit (found in the
761 .debug_line section) begins with a "statement program header",
762 which contains the following information. */
763 struct line_header
764 {
765 unsigned int total_length;
766 unsigned short version;
767 unsigned int header_length;
768 unsigned char minimum_instruction_length;
769 unsigned char maximum_ops_per_instruction;
770 unsigned char default_is_stmt;
771 int line_base;
772 unsigned char line_range;
773 unsigned char opcode_base;
774
775 /* standard_opcode_lengths[i] is the number of operands for the
776 standard opcode whose value is i. This means that
777 standard_opcode_lengths[0] is unused, and the last meaningful
778 element is standard_opcode_lengths[opcode_base - 1]. */
779 unsigned char *standard_opcode_lengths;
780
781 /* The include_directories table. NOTE! These strings are not
782 allocated with xmalloc; instead, they are pointers into
783 debug_line_buffer. If you try to free them, `free' will get
784 indigestion. */
785 unsigned int num_include_dirs, include_dirs_size;
786 char **include_dirs;
787
788 /* The file_names table. NOTE! These strings are not allocated
789 with xmalloc; instead, they are pointers into debug_line_buffer.
790 Don't try to free them directly. */
791 unsigned int num_file_names, file_names_size;
792 struct file_entry
793 {
794 char *name;
795 unsigned int dir_index;
796 unsigned int mod_time;
797 unsigned int length;
798 int included_p; /* Non-zero if referenced by the Line Number Program. */
799 struct symtab *symtab; /* The associated symbol table, if any. */
800 } *file_names;
801
802 /* The start and end of the statement program following this
803 header. These point into dwarf2_per_objfile->line_buffer. */
804 gdb_byte *statement_program_start, *statement_program_end;
805 };
806
807 /* When we construct a partial symbol table entry we only
808 need this much information. */
809 struct partial_die_info
810 {
811 /* Offset of this DIE. */
812 sect_offset offset;
813
814 /* DWARF-2 tag for this DIE. */
815 ENUM_BITFIELD(dwarf_tag) tag : 16;
816
817 /* Assorted flags describing the data found in this DIE. */
818 unsigned int has_children : 1;
819 unsigned int is_external : 1;
820 unsigned int is_declaration : 1;
821 unsigned int has_type : 1;
822 unsigned int has_specification : 1;
823 unsigned int has_pc_info : 1;
824 unsigned int may_be_inlined : 1;
825
826 /* Flag set if the SCOPE field of this structure has been
827 computed. */
828 unsigned int scope_set : 1;
829
830 /* Flag set if the DIE has a byte_size attribute. */
831 unsigned int has_byte_size : 1;
832
833 /* Flag set if any of the DIE's children are template arguments. */
834 unsigned int has_template_arguments : 1;
835
836 /* Flag set if fixup_partial_die has been called on this die. */
837 unsigned int fixup_called : 1;
838
839 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
840 unsigned int is_dwz : 1;
841
842 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
843 unsigned int spec_is_dwz : 1;
844
845 /* The name of this DIE. Normally the value of DW_AT_name, but
846 sometimes a default name for unnamed DIEs. */
847 char *name;
848
849 /* The linkage name, if present. */
850 const char *linkage_name;
851
852 /* The scope to prepend to our children. This is generally
853 allocated on the comp_unit_obstack, so will disappear
854 when this compilation unit leaves the cache. */
855 char *scope;
856
857 /* Some data associated with the partial DIE. The tag determines
858 which field is live. */
859 union
860 {
861 /* The location description associated with this DIE, if any. */
862 struct dwarf_block *locdesc;
863 /* The offset of an import, for DW_TAG_imported_unit. */
864 sect_offset offset;
865 } d;
866
867 /* If HAS_PC_INFO, the PC range associated with this DIE. */
868 CORE_ADDR lowpc;
869 CORE_ADDR highpc;
870
871 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
872 DW_AT_sibling, if any. */
873 /* NOTE: This member isn't strictly necessary, read_partial_die could
874 return DW_AT_sibling values to its caller load_partial_dies. */
875 gdb_byte *sibling;
876
877 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
878 DW_AT_specification (or DW_AT_abstract_origin or
879 DW_AT_extension). */
880 sect_offset spec_offset;
881
882 /* Pointers to this DIE's parent, first child, and next sibling,
883 if any. */
884 struct partial_die_info *die_parent, *die_child, *die_sibling;
885 };
886
887 /* This data structure holds the information of an abbrev. */
888 struct abbrev_info
889 {
890 unsigned int number; /* number identifying abbrev */
891 enum dwarf_tag tag; /* dwarf tag */
892 unsigned short has_children; /* boolean */
893 unsigned short num_attrs; /* number of attributes */
894 struct attr_abbrev *attrs; /* an array of attribute descriptions */
895 struct abbrev_info *next; /* next in chain */
896 };
897
898 struct attr_abbrev
899 {
900 ENUM_BITFIELD(dwarf_attribute) name : 16;
901 ENUM_BITFIELD(dwarf_form) form : 16;
902 };
903
904 /* Size of abbrev_table.abbrev_hash_table. */
905 #define ABBREV_HASH_SIZE 121
906
907 /* Top level data structure to contain an abbreviation table. */
908
909 struct abbrev_table
910 {
911 /* Where the abbrev table came from.
912 This is used as a sanity check when the table is used. */
913 sect_offset offset;
914
915 /* Storage for the abbrev table. */
916 struct obstack abbrev_obstack;
917
918 /* Hash table of abbrevs.
919 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
920 It could be statically allocated, but the previous code didn't so we
921 don't either. */
922 struct abbrev_info **abbrevs;
923 };
924
925 /* Attributes have a name and a value. */
926 struct attribute
927 {
928 ENUM_BITFIELD(dwarf_attribute) name : 16;
929 ENUM_BITFIELD(dwarf_form) form : 15;
930
931 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
932 field should be in u.str (existing only for DW_STRING) but it is kept
933 here for better struct attribute alignment. */
934 unsigned int string_is_canonical : 1;
935
936 union
937 {
938 char *str;
939 struct dwarf_block *blk;
940 ULONGEST unsnd;
941 LONGEST snd;
942 CORE_ADDR addr;
943 struct signatured_type *signatured_type;
944 }
945 u;
946 };
947
948 /* This data structure holds a complete die structure. */
949 struct die_info
950 {
951 /* DWARF-2 tag for this DIE. */
952 ENUM_BITFIELD(dwarf_tag) tag : 16;
953
954 /* Number of attributes */
955 unsigned char num_attrs;
956
957 /* True if we're presently building the full type name for the
958 type derived from this DIE. */
959 unsigned char building_fullname : 1;
960
961 /* Abbrev number */
962 unsigned int abbrev;
963
964 /* Offset in .debug_info or .debug_types section. */
965 sect_offset offset;
966
967 /* The dies in a compilation unit form an n-ary tree. PARENT
968 points to this die's parent; CHILD points to the first child of
969 this node; and all the children of a given node are chained
970 together via their SIBLING fields. */
971 struct die_info *child; /* Its first child, if any. */
972 struct die_info *sibling; /* Its next sibling, if any. */
973 struct die_info *parent; /* Its parent, if any. */
974
975 /* An array of attributes, with NUM_ATTRS elements. There may be
976 zero, but it's not common and zero-sized arrays are not
977 sufficiently portable C. */
978 struct attribute attrs[1];
979 };
980
981 /* Get at parts of an attribute structure. */
982
983 #define DW_STRING(attr) ((attr)->u.str)
984 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
985 #define DW_UNSND(attr) ((attr)->u.unsnd)
986 #define DW_BLOCK(attr) ((attr)->u.blk)
987 #define DW_SND(attr) ((attr)->u.snd)
988 #define DW_ADDR(attr) ((attr)->u.addr)
989 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
990
991 /* Blocks are a bunch of untyped bytes. */
992 struct dwarf_block
993 {
994 size_t size;
995
996 /* Valid only if SIZE is not zero. */
997 gdb_byte *data;
998 };
999
1000 #ifndef ATTR_ALLOC_CHUNK
1001 #define ATTR_ALLOC_CHUNK 4
1002 #endif
1003
1004 /* Allocate fields for structs, unions and enums in this size. */
1005 #ifndef DW_FIELD_ALLOC_CHUNK
1006 #define DW_FIELD_ALLOC_CHUNK 4
1007 #endif
1008
1009 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1010 but this would require a corresponding change in unpack_field_as_long
1011 and friends. */
1012 static int bits_per_byte = 8;
1013
1014 /* The routines that read and process dies for a C struct or C++ class
1015 pass lists of data member fields and lists of member function fields
1016 in an instance of a field_info structure, as defined below. */
1017 struct field_info
1018 {
1019 /* List of data member and baseclasses fields. */
1020 struct nextfield
1021 {
1022 struct nextfield *next;
1023 int accessibility;
1024 int virtuality;
1025 struct field field;
1026 }
1027 *fields, *baseclasses;
1028
1029 /* Number of fields (including baseclasses). */
1030 int nfields;
1031
1032 /* Number of baseclasses. */
1033 int nbaseclasses;
1034
1035 /* Set if the accesibility of one of the fields is not public. */
1036 int non_public_fields;
1037
1038 /* Member function fields array, entries are allocated in the order they
1039 are encountered in the object file. */
1040 struct nextfnfield
1041 {
1042 struct nextfnfield *next;
1043 struct fn_field fnfield;
1044 }
1045 *fnfields;
1046
1047 /* Member function fieldlist array, contains name of possibly overloaded
1048 member function, number of overloaded member functions and a pointer
1049 to the head of the member function field chain. */
1050 struct fnfieldlist
1051 {
1052 char *name;
1053 int length;
1054 struct nextfnfield *head;
1055 }
1056 *fnfieldlists;
1057
1058 /* Number of entries in the fnfieldlists array. */
1059 int nfnfields;
1060
1061 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1062 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1063 struct typedef_field_list
1064 {
1065 struct typedef_field field;
1066 struct typedef_field_list *next;
1067 }
1068 *typedef_field_list;
1069 unsigned typedef_field_list_count;
1070 };
1071
1072 /* One item on the queue of compilation units to read in full symbols
1073 for. */
1074 struct dwarf2_queue_item
1075 {
1076 struct dwarf2_per_cu_data *per_cu;
1077 enum language pretend_language;
1078 struct dwarf2_queue_item *next;
1079 };
1080
1081 /* The current queue. */
1082 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1083
1084 /* Loaded secondary compilation units are kept in memory until they
1085 have not been referenced for the processing of this many
1086 compilation units. Set this to zero to disable caching. Cache
1087 sizes of up to at least twenty will improve startup time for
1088 typical inter-CU-reference binaries, at an obvious memory cost. */
1089 static int dwarf2_max_cache_age = 5;
1090 static void
1091 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1092 struct cmd_list_element *c, const char *value)
1093 {
1094 fprintf_filtered (file, _("The upper bound on the age of cached "
1095 "dwarf2 compilation units is %s.\n"),
1096 value);
1097 }
1098
1099
1100 /* Various complaints about symbol reading that don't abort the process. */
1101
1102 static void
1103 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1104 {
1105 complaint (&symfile_complaints,
1106 _("statement list doesn't fit in .debug_line section"));
1107 }
1108
1109 static void
1110 dwarf2_debug_line_missing_file_complaint (void)
1111 {
1112 complaint (&symfile_complaints,
1113 _(".debug_line section has line data without a file"));
1114 }
1115
1116 static void
1117 dwarf2_debug_line_missing_end_sequence_complaint (void)
1118 {
1119 complaint (&symfile_complaints,
1120 _(".debug_line section has line "
1121 "program sequence without an end"));
1122 }
1123
1124 static void
1125 dwarf2_complex_location_expr_complaint (void)
1126 {
1127 complaint (&symfile_complaints, _("location expression too complex"));
1128 }
1129
1130 static void
1131 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1132 int arg3)
1133 {
1134 complaint (&symfile_complaints,
1135 _("const value length mismatch for '%s', got %d, expected %d"),
1136 arg1, arg2, arg3);
1137 }
1138
1139 static void
1140 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1141 {
1142 complaint (&symfile_complaints,
1143 _("debug info runs off end of %s section"
1144 " [in module %s]"),
1145 section->asection->name,
1146 bfd_get_filename (section->asection->owner));
1147 }
1148
1149 static void
1150 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1151 {
1152 complaint (&symfile_complaints,
1153 _("macro debug info contains a "
1154 "malformed macro definition:\n`%s'"),
1155 arg1);
1156 }
1157
1158 static void
1159 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1160 {
1161 complaint (&symfile_complaints,
1162 _("invalid attribute class or form for '%s' in '%s'"),
1163 arg1, arg2);
1164 }
1165
1166 /* local function prototypes */
1167
1168 static void dwarf2_locate_sections (bfd *, asection *, void *);
1169
1170 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1171 struct objfile *);
1172
1173 static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
1176 static void dwarf2_build_psymtabs_hard (struct objfile *);
1177
1178 static void scan_partial_symbols (struct partial_die_info *,
1179 CORE_ADDR *, CORE_ADDR *,
1180 int, struct dwarf2_cu *);
1181
1182 static void add_partial_symbol (struct partial_die_info *,
1183 struct dwarf2_cu *);
1184
1185 static void add_partial_namespace (struct partial_die_info *pdi,
1186 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1187 int need_pc, struct dwarf2_cu *cu);
1188
1189 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1190 CORE_ADDR *highpc, int need_pc,
1191 struct dwarf2_cu *cu);
1192
1193 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1194 struct dwarf2_cu *cu);
1195
1196 static void add_partial_subprogram (struct partial_die_info *pdi,
1197 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1198 int need_pc, struct dwarf2_cu *cu);
1199
1200 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1201
1202 static void psymtab_to_symtab_1 (struct partial_symtab *);
1203
1204 static struct abbrev_info *abbrev_table_lookup_abbrev
1205 (const struct abbrev_table *, unsigned int);
1206
1207 static struct abbrev_table *abbrev_table_read_table
1208 (struct dwarf2_section_info *, sect_offset);
1209
1210 static void abbrev_table_free (struct abbrev_table *);
1211
1212 static void abbrev_table_free_cleanup (void *);
1213
1214 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1215 struct dwarf2_section_info *);
1216
1217 static void dwarf2_free_abbrev_table (void *);
1218
1219 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1220
1221 static struct partial_die_info *load_partial_dies
1222 (const struct die_reader_specs *, gdb_byte *, int);
1223
1224 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1225 struct partial_die_info *,
1226 struct abbrev_info *,
1227 unsigned int,
1228 gdb_byte *);
1229
1230 static struct partial_die_info *find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
1232
1233 static void fixup_partial_die (struct partial_die_info *,
1234 struct dwarf2_cu *);
1235
1236 static gdb_byte *read_attribute (const struct die_reader_specs *,
1237 struct attribute *, struct attr_abbrev *,
1238 gdb_byte *);
1239
1240 static unsigned int read_1_byte (bfd *, gdb_byte *);
1241
1242 static int read_1_signed_byte (bfd *, gdb_byte *);
1243
1244 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1245
1246 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1247
1248 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1249
1250 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1251 unsigned int *);
1252
1253 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1254
1255 static LONGEST read_checked_initial_length_and_offset
1256 (bfd *, gdb_byte *, const struct comp_unit_head *,
1257 unsigned int *, unsigned int *);
1258
1259 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1260 unsigned int *);
1261
1262 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1263
1264 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1265 sect_offset);
1266
1267 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1268
1269 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1270
1271 static char *read_indirect_string (bfd *, gdb_byte *,
1272 const struct comp_unit_head *,
1273 unsigned int *);
1274
1275 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1276
1277 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1278
1279 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1280
1281 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1282 unsigned int *);
1283
1284 static char *read_str_index (const struct die_reader_specs *reader,
1285 struct dwarf2_cu *cu, ULONGEST str_index);
1286
1287 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1288
1289 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1290 struct dwarf2_cu *);
1291
1292 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1293 unsigned int);
1294
1295 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1296 struct dwarf2_cu *cu);
1297
1298 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1299
1300 static struct die_info *die_specification (struct die_info *die,
1301 struct dwarf2_cu **);
1302
1303 static void free_line_header (struct line_header *lh);
1304
1305 static void add_file_name (struct line_header *, char *, unsigned int,
1306 unsigned int, unsigned int);
1307
1308 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1309 struct dwarf2_cu *cu);
1310
1311 static void dwarf_decode_lines (struct line_header *, const char *,
1312 struct dwarf2_cu *, struct partial_symtab *,
1313 int);
1314
1315 static void dwarf2_start_subfile (char *, const char *, const char *);
1316
1317 static void dwarf2_start_symtab (struct dwarf2_cu *,
1318 char *, char *, CORE_ADDR);
1319
1320 static struct symbol *new_symbol (struct die_info *, struct type *,
1321 struct dwarf2_cu *);
1322
1323 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1324 struct dwarf2_cu *, struct symbol *);
1325
1326 static void dwarf2_const_value (struct attribute *, struct symbol *,
1327 struct dwarf2_cu *);
1328
1329 static void dwarf2_const_value_attr (struct attribute *attr,
1330 struct type *type,
1331 const char *name,
1332 struct obstack *obstack,
1333 struct dwarf2_cu *cu, LONGEST *value,
1334 gdb_byte **bytes,
1335 struct dwarf2_locexpr_baton **baton);
1336
1337 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1338
1339 static int need_gnat_info (struct dwarf2_cu *);
1340
1341 static struct type *die_descriptive_type (struct die_info *,
1342 struct dwarf2_cu *);
1343
1344 static void set_descriptive_type (struct type *, struct die_info *,
1345 struct dwarf2_cu *);
1346
1347 static struct type *die_containing_type (struct die_info *,
1348 struct dwarf2_cu *);
1349
1350 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1351 struct dwarf2_cu *);
1352
1353 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1354
1355 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1356
1357 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1358
1359 static char *typename_concat (struct obstack *obs, const char *prefix,
1360 const char *suffix, int physname,
1361 struct dwarf2_cu *cu);
1362
1363 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1364
1365 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1366
1367 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1368
1369 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1370
1371 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1372
1373 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1374 struct dwarf2_cu *, struct partial_symtab *);
1375
1376 static int dwarf2_get_pc_bounds (struct die_info *,
1377 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1378 struct partial_symtab *);
1379
1380 static void get_scope_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *);
1383
1384 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1385 CORE_ADDR, struct dwarf2_cu *);
1386
1387 static void dwarf2_add_field (struct field_info *, struct die_info *,
1388 struct dwarf2_cu *);
1389
1390 static void dwarf2_attach_fields_to_type (struct field_info *,
1391 struct type *, struct dwarf2_cu *);
1392
1393 static void dwarf2_add_member_fn (struct field_info *,
1394 struct die_info *, struct type *,
1395 struct dwarf2_cu *);
1396
1397 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1398 struct type *,
1399 struct dwarf2_cu *);
1400
1401 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1402
1403 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1404
1405 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1406
1407 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1408
1409 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1410
1411 static struct type *read_module_type (struct die_info *die,
1412 struct dwarf2_cu *cu);
1413
1414 static const char *namespace_name (struct die_info *die,
1415 int *is_anonymous, struct dwarf2_cu *);
1416
1417 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1418
1419 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1420
1421 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1422 struct dwarf2_cu *);
1423
1424 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1425 gdb_byte *info_ptr,
1426 gdb_byte **new_info_ptr,
1427 struct die_info *parent);
1428
1429 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1430 gdb_byte *info_ptr,
1431 gdb_byte **new_info_ptr,
1432 struct die_info *parent);
1433
1434 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1435 struct die_info **, gdb_byte *, int *, int);
1436
1437 static gdb_byte *read_full_die (const struct die_reader_specs *,
1438 struct die_info **, gdb_byte *, int *);
1439
1440 static void process_die (struct die_info *, struct dwarf2_cu *);
1441
1442 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1443 struct obstack *);
1444
1445 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1446
1447 static const char *dwarf2_full_name (char *name,
1448 struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
1451 static struct die_info *dwarf2_extension (struct die_info *die,
1452 struct dwarf2_cu **);
1453
1454 static const char *dwarf_tag_name (unsigned int);
1455
1456 static const char *dwarf_attr_name (unsigned int);
1457
1458 static const char *dwarf_form_name (unsigned int);
1459
1460 static char *dwarf_bool_name (unsigned int);
1461
1462 static const char *dwarf_type_encoding_name (unsigned int);
1463
1464 static struct die_info *sibling_die (struct die_info *);
1465
1466 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1467
1468 static void dump_die_for_error (struct die_info *);
1469
1470 static void dump_die_1 (struct ui_file *, int level, int max_level,
1471 struct die_info *);
1472
1473 /*static*/ void dump_die (struct die_info *, int max_level);
1474
1475 static void store_in_ref_table (struct die_info *,
1476 struct dwarf2_cu *);
1477
1478 static int is_ref_attr (struct attribute *);
1479
1480 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1481
1482 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1483
1484 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1485 struct attribute *,
1486 struct dwarf2_cu **);
1487
1488 static struct die_info *follow_die_ref (struct die_info *,
1489 struct attribute *,
1490 struct dwarf2_cu **);
1491
1492 static struct die_info *follow_die_sig (struct die_info *,
1493 struct attribute *,
1494 struct dwarf2_cu **);
1495
1496 static struct signatured_type *lookup_signatured_type_at_offset
1497 (struct objfile *objfile,
1498 struct dwarf2_section_info *section, sect_offset offset);
1499
1500 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1501
1502 static void read_signatured_type (struct signatured_type *);
1503
1504 static struct type_unit_group *get_type_unit_group
1505 (struct dwarf2_cu *, struct attribute *);
1506
1507 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1508
1509 /* memory allocation interface */
1510
1511 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1512
1513 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1514
1515 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1516 char *, int);
1517
1518 static int attr_form_is_block (struct attribute *);
1519
1520 static int attr_form_is_section_offset (struct attribute *);
1521
1522 static int attr_form_is_constant (struct attribute *);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu);
1531
1532 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1533 gdb_byte *info_ptr,
1534 struct abbrev_info *abbrev);
1535
1536 static void free_stack_comp_unit (void *);
1537
1538 static hashval_t partial_die_hash (const void *item);
1539
1540 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1541
1542 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1543 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1544
1545 static void init_one_comp_unit (struct dwarf2_cu *cu,
1546 struct dwarf2_per_cu_data *per_cu);
1547
1548 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1549 struct die_info *comp_unit_die,
1550 enum language pretend_language);
1551
1552 static void free_heap_comp_unit (void *);
1553
1554 static void free_cached_comp_units (void *);
1555
1556 static void age_cached_comp_units (void);
1557
1558 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1559
1560 static struct type *set_die_type (struct die_info *, struct type *,
1561 struct dwarf2_cu *);
1562
1563 static void create_all_comp_units (struct objfile *);
1564
1565 static int create_all_type_units (struct objfile *);
1566
1567 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1568 enum language);
1569
1570 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1571 enum language);
1572
1573 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1574 enum language);
1575
1576 static void dwarf2_add_dependence (struct dwarf2_cu *,
1577 struct dwarf2_per_cu_data *);
1578
1579 static void dwarf2_mark (struct dwarf2_cu *);
1580
1581 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1582
1583 static struct type *get_die_type_at_offset (sect_offset,
1584 struct dwarf2_per_cu_data *per_cu);
1585
1586 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1587
1588 static void dwarf2_release_queue (void *dummy);
1589
1590 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1591 enum language pretend_language);
1592
1593 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1594 struct dwarf2_per_cu_data *per_cu,
1595 enum language pretend_language);
1596
1597 static void process_queue (void);
1598
1599 static void find_file_and_directory (struct die_info *die,
1600 struct dwarf2_cu *cu,
1601 char **name, char **comp_dir);
1602
1603 static char *file_full_name (int file, struct line_header *lh,
1604 const char *comp_dir);
1605
1606 static gdb_byte *read_and_check_comp_unit_head
1607 (struct comp_unit_head *header,
1608 struct dwarf2_section_info *section,
1609 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1610 int is_debug_types_section);
1611
1612 static void init_cutu_and_read_dies
1613 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1614 int use_existing_cu, int keep,
1615 die_reader_func_ftype *die_reader_func, void *data);
1616
1617 static void init_cutu_and_read_dies_simple
1618 (struct dwarf2_per_cu_data *this_cu,
1619 die_reader_func_ftype *die_reader_func, void *data);
1620
1621 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1622
1623 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1624
1625 static struct dwo_unit *lookup_dwo_comp_unit
1626 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1627
1628 static struct dwo_unit *lookup_dwo_type_unit
1629 (struct signatured_type *, char *, const char *);
1630
1631 static void free_dwo_file_cleanup (void *);
1632
1633 static void process_cu_includes (void);
1634
1635 #if WORDS_BIGENDIAN
1636
1637 /* Convert VALUE between big- and little-endian. */
1638 static offset_type
1639 byte_swap (offset_type value)
1640 {
1641 offset_type result;
1642
1643 result = (value & 0xff) << 24;
1644 result |= (value & 0xff00) << 8;
1645 result |= (value & 0xff0000) >> 8;
1646 result |= (value & 0xff000000) >> 24;
1647 return result;
1648 }
1649
1650 #define MAYBE_SWAP(V) byte_swap (V)
1651
1652 #else
1653 #define MAYBE_SWAP(V) (V)
1654 #endif /* WORDS_BIGENDIAN */
1655
1656 /* The suffix for an index file. */
1657 #define INDEX_SUFFIX ".gdb-index"
1658
1659 static const char *dwarf2_physname (char *name, struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 /* Try to locate the sections we need for DWARF 2 debugging
1663 information and return true if we have enough to do something.
1664 NAMES points to the dwarf2 section names, or is NULL if the standard
1665 ELF names are used. */
1666
1667 int
1668 dwarf2_has_info (struct objfile *objfile,
1669 const struct dwarf2_debug_sections *names)
1670 {
1671 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1672 if (!dwarf2_per_objfile)
1673 {
1674 /* Initialize per-objfile state. */
1675 struct dwarf2_per_objfile *data
1676 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1677
1678 memset (data, 0, sizeof (*data));
1679 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1680 dwarf2_per_objfile = data;
1681
1682 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1683 (void *) names);
1684 dwarf2_per_objfile->objfile = objfile;
1685 }
1686 return (dwarf2_per_objfile->info.asection != NULL
1687 && dwarf2_per_objfile->abbrev.asection != NULL);
1688 }
1689
1690 /* When loading sections, we look either for uncompressed section or for
1691 compressed section names. */
1692
1693 static int
1694 section_is_p (const char *section_name,
1695 const struct dwarf2_section_names *names)
1696 {
1697 if (names->normal != NULL
1698 && strcmp (section_name, names->normal) == 0)
1699 return 1;
1700 if (names->compressed != NULL
1701 && strcmp (section_name, names->compressed) == 0)
1702 return 1;
1703 return 0;
1704 }
1705
1706 /* This function is mapped across the sections and remembers the
1707 offset and size of each of the debugging sections we are interested
1708 in. */
1709
1710 static void
1711 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1712 {
1713 const struct dwarf2_debug_sections *names;
1714 flagword aflag = bfd_get_section_flags (abfd, sectp);
1715
1716 if (vnames == NULL)
1717 names = &dwarf2_elf_names;
1718 else
1719 names = (const struct dwarf2_debug_sections *) vnames;
1720
1721 if ((aflag & SEC_HAS_CONTENTS) == 0)
1722 {
1723 }
1724 else if (section_is_p (sectp->name, &names->info))
1725 {
1726 dwarf2_per_objfile->info.asection = sectp;
1727 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1728 }
1729 else if (section_is_p (sectp->name, &names->abbrev))
1730 {
1731 dwarf2_per_objfile->abbrev.asection = sectp;
1732 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1733 }
1734 else if (section_is_p (sectp->name, &names->line))
1735 {
1736 dwarf2_per_objfile->line.asection = sectp;
1737 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1738 }
1739 else if (section_is_p (sectp->name, &names->loc))
1740 {
1741 dwarf2_per_objfile->loc.asection = sectp;
1742 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1743 }
1744 else if (section_is_p (sectp->name, &names->macinfo))
1745 {
1746 dwarf2_per_objfile->macinfo.asection = sectp;
1747 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1748 }
1749 else if (section_is_p (sectp->name, &names->macro))
1750 {
1751 dwarf2_per_objfile->macro.asection = sectp;
1752 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1753 }
1754 else if (section_is_p (sectp->name, &names->str))
1755 {
1756 dwarf2_per_objfile->str.asection = sectp;
1757 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1758 }
1759 else if (section_is_p (sectp->name, &names->addr))
1760 {
1761 dwarf2_per_objfile->addr.asection = sectp;
1762 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1763 }
1764 else if (section_is_p (sectp->name, &names->frame))
1765 {
1766 dwarf2_per_objfile->frame.asection = sectp;
1767 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1768 }
1769 else if (section_is_p (sectp->name, &names->eh_frame))
1770 {
1771 dwarf2_per_objfile->eh_frame.asection = sectp;
1772 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1773 }
1774 else if (section_is_p (sectp->name, &names->ranges))
1775 {
1776 dwarf2_per_objfile->ranges.asection = sectp;
1777 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1778 }
1779 else if (section_is_p (sectp->name, &names->types))
1780 {
1781 struct dwarf2_section_info type_section;
1782
1783 memset (&type_section, 0, sizeof (type_section));
1784 type_section.asection = sectp;
1785 type_section.size = bfd_get_section_size (sectp);
1786
1787 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1788 &type_section);
1789 }
1790 else if (section_is_p (sectp->name, &names->gdb_index))
1791 {
1792 dwarf2_per_objfile->gdb_index.asection = sectp;
1793 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1794 }
1795
1796 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1797 && bfd_section_vma (abfd, sectp) == 0)
1798 dwarf2_per_objfile->has_section_at_zero = 1;
1799 }
1800
1801 /* A helper function that decides whether a section is empty,
1802 or not present. */
1803
1804 static int
1805 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1806 {
1807 return info->asection == NULL || info->size == 0;
1808 }
1809
1810 /* Read the contents of the section INFO.
1811 OBJFILE is the main object file, but not necessarily the file where
1812 the section comes from. E.g., for DWO files INFO->asection->owner
1813 is the bfd of the DWO file.
1814 If the section is compressed, uncompress it before returning. */
1815
1816 static void
1817 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1818 {
1819 asection *sectp = info->asection;
1820 bfd *abfd;
1821 gdb_byte *buf, *retbuf;
1822 unsigned char header[4];
1823
1824 if (info->readin)
1825 return;
1826 info->buffer = NULL;
1827 info->readin = 1;
1828
1829 if (dwarf2_section_empty_p (info))
1830 return;
1831
1832 abfd = sectp->owner;
1833
1834 /* If the section has relocations, we must read it ourselves.
1835 Otherwise we attach it to the BFD. */
1836 if ((sectp->flags & SEC_RELOC) == 0)
1837 {
1838 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1839
1840 /* We have to cast away const here for historical reasons.
1841 Fixing dwarf2read to be const-correct would be quite nice. */
1842 info->buffer = (gdb_byte *) bytes;
1843 return;
1844 }
1845
1846 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1847 info->buffer = buf;
1848
1849 /* When debugging .o files, we may need to apply relocations; see
1850 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1851 We never compress sections in .o files, so we only need to
1852 try this when the section is not compressed. */
1853 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1854 if (retbuf != NULL)
1855 {
1856 info->buffer = retbuf;
1857 return;
1858 }
1859
1860 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1861 || bfd_bread (buf, info->size, abfd) != info->size)
1862 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1863 bfd_get_filename (abfd));
1864 }
1865
1866 /* A helper function that returns the size of a section in a safe way.
1867 If you are positive that the section has been read before using the
1868 size, then it is safe to refer to the dwarf2_section_info object's
1869 "size" field directly. In other cases, you must call this
1870 function, because for compressed sections the size field is not set
1871 correctly until the section has been read. */
1872
1873 static bfd_size_type
1874 dwarf2_section_size (struct objfile *objfile,
1875 struct dwarf2_section_info *info)
1876 {
1877 if (!info->readin)
1878 dwarf2_read_section (objfile, info);
1879 return info->size;
1880 }
1881
1882 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1883 SECTION_NAME. */
1884
1885 void
1886 dwarf2_get_section_info (struct objfile *objfile,
1887 enum dwarf2_section_enum sect,
1888 asection **sectp, gdb_byte **bufp,
1889 bfd_size_type *sizep)
1890 {
1891 struct dwarf2_per_objfile *data
1892 = objfile_data (objfile, dwarf2_objfile_data_key);
1893 struct dwarf2_section_info *info;
1894
1895 /* We may see an objfile without any DWARF, in which case we just
1896 return nothing. */
1897 if (data == NULL)
1898 {
1899 *sectp = NULL;
1900 *bufp = NULL;
1901 *sizep = 0;
1902 return;
1903 }
1904 switch (sect)
1905 {
1906 case DWARF2_DEBUG_FRAME:
1907 info = &data->frame;
1908 break;
1909 case DWARF2_EH_FRAME:
1910 info = &data->eh_frame;
1911 break;
1912 default:
1913 gdb_assert_not_reached ("unexpected section");
1914 }
1915
1916 dwarf2_read_section (objfile, info);
1917
1918 *sectp = info->asection;
1919 *bufp = info->buffer;
1920 *sizep = info->size;
1921 }
1922
1923 /* A helper function to find the sections for a .dwz file. */
1924
1925 static void
1926 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1927 {
1928 struct dwz_file *dwz_file = arg;
1929
1930 /* Note that we only support the standard ELF names, because .dwz
1931 is ELF-only (at the time of writing). */
1932 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1933 {
1934 dwz_file->abbrev.asection = sectp;
1935 dwz_file->abbrev.size = bfd_get_section_size (sectp);
1936 }
1937 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1938 {
1939 dwz_file->info.asection = sectp;
1940 dwz_file->info.size = bfd_get_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1943 {
1944 dwz_file->str.asection = sectp;
1945 dwz_file->str.size = bfd_get_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1948 {
1949 dwz_file->line.asection = sectp;
1950 dwz_file->line.size = bfd_get_section_size (sectp);
1951 }
1952 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1953 {
1954 dwz_file->macro.asection = sectp;
1955 dwz_file->macro.size = bfd_get_section_size (sectp);
1956 }
1957 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1958 {
1959 dwz_file->gdb_index.asection = sectp;
1960 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1961 }
1962 }
1963
1964 /* Open the separate '.dwz' debug file, if needed. Error if the file
1965 cannot be found. */
1966
1967 static struct dwz_file *
1968 dwarf2_get_dwz_file (void)
1969 {
1970 bfd *abfd, *dwz_bfd;
1971 asection *section;
1972 gdb_byte *data;
1973 struct cleanup *cleanup;
1974 const char *filename;
1975 struct dwz_file *result;
1976
1977 if (dwarf2_per_objfile->dwz_file != NULL)
1978 return dwarf2_per_objfile->dwz_file;
1979
1980 abfd = dwarf2_per_objfile->objfile->obfd;
1981 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1982 if (section == NULL)
1983 error (_("could not find '.gnu_debugaltlink' section"));
1984 if (!bfd_malloc_and_get_section (abfd, section, &data))
1985 error (_("could not read '.gnu_debugaltlink' section: %s"),
1986 bfd_errmsg (bfd_get_error ()));
1987 cleanup = make_cleanup (xfree, data);
1988
1989 filename = data;
1990 if (!IS_ABSOLUTE_PATH (filename))
1991 {
1992 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
1993 char *rel;
1994
1995 make_cleanup (xfree, abs);
1996 abs = ldirname (abs);
1997 make_cleanup (xfree, abs);
1998
1999 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2000 make_cleanup (xfree, rel);
2001 filename = rel;
2002 }
2003
2004 /* The format is just a NUL-terminated file name, followed by the
2005 build-id. For now, though, we ignore the build-id. */
2006 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2007 if (dwz_bfd == NULL)
2008 error (_("could not read '%s': %s"), filename,
2009 bfd_errmsg (bfd_get_error ()));
2010
2011 if (!bfd_check_format (dwz_bfd, bfd_object))
2012 {
2013 gdb_bfd_unref (dwz_bfd);
2014 error (_("file '%s' was not usable: %s"), filename,
2015 bfd_errmsg (bfd_get_error ()));
2016 }
2017
2018 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2019 struct dwz_file);
2020 result->dwz_bfd = dwz_bfd;
2021
2022 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2023
2024 do_cleanups (cleanup);
2025
2026 return result;
2027 }
2028 \f
2029 /* DWARF quick_symbols_functions support. */
2030
2031 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2032 unique line tables, so we maintain a separate table of all .debug_line
2033 derived entries to support the sharing.
2034 All the quick functions need is the list of file names. We discard the
2035 line_header when we're done and don't need to record it here. */
2036 struct quick_file_names
2037 {
2038 /* The data used to construct the hash key. */
2039 struct stmt_list_hash hash;
2040
2041 /* The number of entries in file_names, real_names. */
2042 unsigned int num_file_names;
2043
2044 /* The file names from the line table, after being run through
2045 file_full_name. */
2046 const char **file_names;
2047
2048 /* The file names from the line table after being run through
2049 gdb_realpath. These are computed lazily. */
2050 const char **real_names;
2051 };
2052
2053 /* When using the index (and thus not using psymtabs), each CU has an
2054 object of this type. This is used to hold information needed by
2055 the various "quick" methods. */
2056 struct dwarf2_per_cu_quick_data
2057 {
2058 /* The file table. This can be NULL if there was no file table
2059 or it's currently not read in.
2060 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2061 struct quick_file_names *file_names;
2062
2063 /* The corresponding symbol table. This is NULL if symbols for this
2064 CU have not yet been read. */
2065 struct symtab *symtab;
2066
2067 /* A temporary mark bit used when iterating over all CUs in
2068 expand_symtabs_matching. */
2069 unsigned int mark : 1;
2070
2071 /* True if we've tried to read the file table and found there isn't one.
2072 There will be no point in trying to read it again next time. */
2073 unsigned int no_file_data : 1;
2074 };
2075
2076 /* Utility hash function for a stmt_list_hash. */
2077
2078 static hashval_t
2079 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2080 {
2081 hashval_t v = 0;
2082
2083 if (stmt_list_hash->dwo_unit != NULL)
2084 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2085 v += stmt_list_hash->line_offset.sect_off;
2086 return v;
2087 }
2088
2089 /* Utility equality function for a stmt_list_hash. */
2090
2091 static int
2092 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2093 const struct stmt_list_hash *rhs)
2094 {
2095 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2096 return 0;
2097 if (lhs->dwo_unit != NULL
2098 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2099 return 0;
2100
2101 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2102 }
2103
2104 /* Hash function for a quick_file_names. */
2105
2106 static hashval_t
2107 hash_file_name_entry (const void *e)
2108 {
2109 const struct quick_file_names *file_data = e;
2110
2111 return hash_stmt_list_entry (&file_data->hash);
2112 }
2113
2114 /* Equality function for a quick_file_names. */
2115
2116 static int
2117 eq_file_name_entry (const void *a, const void *b)
2118 {
2119 const struct quick_file_names *ea = a;
2120 const struct quick_file_names *eb = b;
2121
2122 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2123 }
2124
2125 /* Delete function for a quick_file_names. */
2126
2127 static void
2128 delete_file_name_entry (void *e)
2129 {
2130 struct quick_file_names *file_data = e;
2131 int i;
2132
2133 for (i = 0; i < file_data->num_file_names; ++i)
2134 {
2135 xfree ((void*) file_data->file_names[i]);
2136 if (file_data->real_names)
2137 xfree ((void*) file_data->real_names[i]);
2138 }
2139
2140 /* The space for the struct itself lives on objfile_obstack,
2141 so we don't free it here. */
2142 }
2143
2144 /* Create a quick_file_names hash table. */
2145
2146 static htab_t
2147 create_quick_file_names_table (unsigned int nr_initial_entries)
2148 {
2149 return htab_create_alloc (nr_initial_entries,
2150 hash_file_name_entry, eq_file_name_entry,
2151 delete_file_name_entry, xcalloc, xfree);
2152 }
2153
2154 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2155 have to be created afterwards. You should call age_cached_comp_units after
2156 processing PER_CU->CU. dw2_setup must have been already called. */
2157
2158 static void
2159 load_cu (struct dwarf2_per_cu_data *per_cu)
2160 {
2161 if (per_cu->is_debug_types)
2162 load_full_type_unit (per_cu);
2163 else
2164 load_full_comp_unit (per_cu, language_minimal);
2165
2166 gdb_assert (per_cu->cu != NULL);
2167
2168 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2169 }
2170
2171 /* Read in the symbols for PER_CU. */
2172
2173 static void
2174 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2175 {
2176 struct cleanup *back_to;
2177
2178 /* Skip type_unit_groups, reading the type units they contain
2179 is handled elsewhere. */
2180 if (IS_TYPE_UNIT_GROUP (per_cu))
2181 return;
2182
2183 back_to = make_cleanup (dwarf2_release_queue, NULL);
2184
2185 if (dwarf2_per_objfile->using_index
2186 ? per_cu->v.quick->symtab == NULL
2187 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2188 {
2189 queue_comp_unit (per_cu, language_minimal);
2190 load_cu (per_cu);
2191 }
2192
2193 process_queue ();
2194
2195 /* Age the cache, releasing compilation units that have not
2196 been used recently. */
2197 age_cached_comp_units ();
2198
2199 do_cleanups (back_to);
2200 }
2201
2202 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2203 the objfile from which this CU came. Returns the resulting symbol
2204 table. */
2205
2206 static struct symtab *
2207 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2208 {
2209 gdb_assert (dwarf2_per_objfile->using_index);
2210 if (!per_cu->v.quick->symtab)
2211 {
2212 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2213 increment_reading_symtab ();
2214 dw2_do_instantiate_symtab (per_cu);
2215 process_cu_includes ();
2216 do_cleanups (back_to);
2217 }
2218 return per_cu->v.quick->symtab;
2219 }
2220
2221 /* Return the CU given its index.
2222
2223 This is intended for loops like:
2224
2225 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2226 + dwarf2_per_objfile->n_type_units); ++i)
2227 {
2228 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2229
2230 ...;
2231 }
2232 */
2233
2234 static struct dwarf2_per_cu_data *
2235 dw2_get_cu (int index)
2236 {
2237 if (index >= dwarf2_per_objfile->n_comp_units)
2238 {
2239 index -= dwarf2_per_objfile->n_comp_units;
2240 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2241 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2242 }
2243
2244 return dwarf2_per_objfile->all_comp_units[index];
2245 }
2246
2247 /* Return the primary CU given its index.
2248 The difference between this function and dw2_get_cu is in the handling
2249 of type units (TUs). Here we return the type_unit_group object.
2250
2251 This is intended for loops like:
2252
2253 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2254 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2255 {
2256 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2257
2258 ...;
2259 }
2260 */
2261
2262 static struct dwarf2_per_cu_data *
2263 dw2_get_primary_cu (int index)
2264 {
2265 if (index >= dwarf2_per_objfile->n_comp_units)
2266 {
2267 index -= dwarf2_per_objfile->n_comp_units;
2268 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2269 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2270 }
2271
2272 return dwarf2_per_objfile->all_comp_units[index];
2273 }
2274
2275 /* A helper function that knows how to read a 64-bit value in a way
2276 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2277 otherwise. */
2278
2279 static int
2280 extract_cu_value (const char *bytes, ULONGEST *result)
2281 {
2282 if (sizeof (ULONGEST) < 8)
2283 {
2284 int i;
2285
2286 /* Ignore the upper 4 bytes if they are all zero. */
2287 for (i = 0; i < 4; ++i)
2288 if (bytes[i + 4] != 0)
2289 return 0;
2290
2291 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2292 }
2293 else
2294 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2295 return 1;
2296 }
2297
2298 /* A helper for create_cus_from_index that handles a given list of
2299 CUs. */
2300
2301 static int
2302 create_cus_from_index_list (struct objfile *objfile,
2303 const gdb_byte *cu_list, offset_type n_elements,
2304 struct dwarf2_section_info *section,
2305 int is_dwz,
2306 int base_offset)
2307 {
2308 offset_type i;
2309
2310 for (i = 0; i < n_elements; i += 2)
2311 {
2312 struct dwarf2_per_cu_data *the_cu;
2313 ULONGEST offset, length;
2314
2315 if (!extract_cu_value (cu_list, &offset)
2316 || !extract_cu_value (cu_list + 8, &length))
2317 return 0;
2318 cu_list += 2 * 8;
2319
2320 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2321 struct dwarf2_per_cu_data);
2322 the_cu->offset.sect_off = offset;
2323 the_cu->length = length;
2324 the_cu->objfile = objfile;
2325 the_cu->info_or_types_section = section;
2326 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2327 struct dwarf2_per_cu_quick_data);
2328 the_cu->is_dwz = is_dwz;
2329 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2330 }
2331
2332 return 1;
2333 }
2334
2335 /* Read the CU list from the mapped index, and use it to create all
2336 the CU objects for this objfile. Return 0 if something went wrong,
2337 1 if everything went ok. */
2338
2339 static int
2340 create_cus_from_index (struct objfile *objfile,
2341 const gdb_byte *cu_list, offset_type cu_list_elements,
2342 const gdb_byte *dwz_list, offset_type dwz_elements)
2343 {
2344 struct dwz_file *dwz;
2345
2346 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2347 dwarf2_per_objfile->all_comp_units
2348 = obstack_alloc (&objfile->objfile_obstack,
2349 dwarf2_per_objfile->n_comp_units
2350 * sizeof (struct dwarf2_per_cu_data *));
2351
2352 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2353 &dwarf2_per_objfile->info, 0, 0))
2354 return 0;
2355
2356 if (dwz_elements == 0)
2357 return 1;
2358
2359 dwz = dwarf2_get_dwz_file ();
2360 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2361 &dwz->info, 1, cu_list_elements / 2);
2362 }
2363
2364 /* Create the signatured type hash table from the index. */
2365
2366 static int
2367 create_signatured_type_table_from_index (struct objfile *objfile,
2368 struct dwarf2_section_info *section,
2369 const gdb_byte *bytes,
2370 offset_type elements)
2371 {
2372 offset_type i;
2373 htab_t sig_types_hash;
2374
2375 dwarf2_per_objfile->n_type_units = elements / 3;
2376 dwarf2_per_objfile->all_type_units
2377 = obstack_alloc (&objfile->objfile_obstack,
2378 dwarf2_per_objfile->n_type_units
2379 * sizeof (struct signatured_type *));
2380
2381 sig_types_hash = allocate_signatured_type_table (objfile);
2382
2383 for (i = 0; i < elements; i += 3)
2384 {
2385 struct signatured_type *sig_type;
2386 ULONGEST offset, type_offset_in_tu, signature;
2387 void **slot;
2388
2389 if (!extract_cu_value (bytes, &offset)
2390 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2391 return 0;
2392 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2393 bytes += 3 * 8;
2394
2395 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2396 struct signatured_type);
2397 sig_type->signature = signature;
2398 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2399 sig_type->per_cu.is_debug_types = 1;
2400 sig_type->per_cu.info_or_types_section = section;
2401 sig_type->per_cu.offset.sect_off = offset;
2402 sig_type->per_cu.objfile = objfile;
2403 sig_type->per_cu.v.quick
2404 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2406
2407 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2408 *slot = sig_type;
2409
2410 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2411 }
2412
2413 dwarf2_per_objfile->signatured_types = sig_types_hash;
2414
2415 return 1;
2416 }
2417
2418 /* Read the address map data from the mapped index, and use it to
2419 populate the objfile's psymtabs_addrmap. */
2420
2421 static void
2422 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2423 {
2424 const gdb_byte *iter, *end;
2425 struct obstack temp_obstack;
2426 struct addrmap *mutable_map;
2427 struct cleanup *cleanup;
2428 CORE_ADDR baseaddr;
2429
2430 obstack_init (&temp_obstack);
2431 cleanup = make_cleanup_obstack_free (&temp_obstack);
2432 mutable_map = addrmap_create_mutable (&temp_obstack);
2433
2434 iter = index->address_table;
2435 end = iter + index->address_table_size;
2436
2437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2438
2439 while (iter < end)
2440 {
2441 ULONGEST hi, lo, cu_index;
2442 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2443 iter += 8;
2444 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2445 iter += 8;
2446 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2447 iter += 4;
2448
2449 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2450 dw2_get_cu (cu_index));
2451 }
2452
2453 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2454 &objfile->objfile_obstack);
2455 do_cleanups (cleanup);
2456 }
2457
2458 /* The hash function for strings in the mapped index. This is the same as
2459 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2460 implementation. This is necessary because the hash function is tied to the
2461 format of the mapped index file. The hash values do not have to match with
2462 SYMBOL_HASH_NEXT.
2463
2464 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2465
2466 static hashval_t
2467 mapped_index_string_hash (int index_version, const void *p)
2468 {
2469 const unsigned char *str = (const unsigned char *) p;
2470 hashval_t r = 0;
2471 unsigned char c;
2472
2473 while ((c = *str++) != 0)
2474 {
2475 if (index_version >= 5)
2476 c = tolower (c);
2477 r = r * 67 + c - 113;
2478 }
2479
2480 return r;
2481 }
2482
2483 /* Find a slot in the mapped index INDEX for the object named NAME.
2484 If NAME is found, set *VEC_OUT to point to the CU vector in the
2485 constant pool and return 1. If NAME cannot be found, return 0. */
2486
2487 static int
2488 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2489 offset_type **vec_out)
2490 {
2491 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2492 offset_type hash;
2493 offset_type slot, step;
2494 int (*cmp) (const char *, const char *);
2495
2496 if (current_language->la_language == language_cplus
2497 || current_language->la_language == language_java
2498 || current_language->la_language == language_fortran)
2499 {
2500 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2501 not contain any. */
2502 const char *paren = strchr (name, '(');
2503
2504 if (paren)
2505 {
2506 char *dup;
2507
2508 dup = xmalloc (paren - name + 1);
2509 memcpy (dup, name, paren - name);
2510 dup[paren - name] = 0;
2511
2512 make_cleanup (xfree, dup);
2513 name = dup;
2514 }
2515 }
2516
2517 /* Index version 4 did not support case insensitive searches. But the
2518 indices for case insensitive languages are built in lowercase, therefore
2519 simulate our NAME being searched is also lowercased. */
2520 hash = mapped_index_string_hash ((index->version == 4
2521 && case_sensitivity == case_sensitive_off
2522 ? 5 : index->version),
2523 name);
2524
2525 slot = hash & (index->symbol_table_slots - 1);
2526 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2527 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2528
2529 for (;;)
2530 {
2531 /* Convert a slot number to an offset into the table. */
2532 offset_type i = 2 * slot;
2533 const char *str;
2534 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2535 {
2536 do_cleanups (back_to);
2537 return 0;
2538 }
2539
2540 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2541 if (!cmp (name, str))
2542 {
2543 *vec_out = (offset_type *) (index->constant_pool
2544 + MAYBE_SWAP (index->symbol_table[i + 1]));
2545 do_cleanups (back_to);
2546 return 1;
2547 }
2548
2549 slot = (slot + step) & (index->symbol_table_slots - 1);
2550 }
2551 }
2552
2553 /* A helper function that reads the .gdb_index from SECTION and fills
2554 in MAP. FILENAME is the name of the file containing the section;
2555 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2556 ok to use deprecated sections.
2557
2558 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2559 out parameters that are filled in with information about the CU and
2560 TU lists in the section.
2561
2562 Returns 1 if all went well, 0 otherwise. */
2563
2564 static int
2565 read_index_from_section (struct objfile *objfile,
2566 const char *filename,
2567 int deprecated_ok,
2568 struct dwarf2_section_info *section,
2569 struct mapped_index *map,
2570 const gdb_byte **cu_list,
2571 offset_type *cu_list_elements,
2572 const gdb_byte **types_list,
2573 offset_type *types_list_elements)
2574 {
2575 char *addr;
2576 offset_type version;
2577 offset_type *metadata;
2578 int i;
2579
2580 if (dwarf2_section_empty_p (section))
2581 return 0;
2582
2583 /* Older elfutils strip versions could keep the section in the main
2584 executable while splitting it for the separate debug info file. */
2585 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2586 return 0;
2587
2588 dwarf2_read_section (objfile, section);
2589
2590 addr = section->buffer;
2591 /* Version check. */
2592 version = MAYBE_SWAP (*(offset_type *) addr);
2593 /* Versions earlier than 3 emitted every copy of a psymbol. This
2594 causes the index to behave very poorly for certain requests. Version 3
2595 contained incomplete addrmap. So, it seems better to just ignore such
2596 indices. */
2597 if (version < 4)
2598 {
2599 static int warning_printed = 0;
2600 if (!warning_printed)
2601 {
2602 warning (_("Skipping obsolete .gdb_index section in %s."),
2603 filename);
2604 warning_printed = 1;
2605 }
2606 return 0;
2607 }
2608 /* Index version 4 uses a different hash function than index version
2609 5 and later.
2610
2611 Versions earlier than 6 did not emit psymbols for inlined
2612 functions. Using these files will cause GDB not to be able to
2613 set breakpoints on inlined functions by name, so we ignore these
2614 indices unless the user has done
2615 "set use-deprecated-index-sections on". */
2616 if (version < 6 && !deprecated_ok)
2617 {
2618 static int warning_printed = 0;
2619 if (!warning_printed)
2620 {
2621 warning (_("\
2622 Skipping deprecated .gdb_index section in %s.\n\
2623 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2624 to use the section anyway."),
2625 filename);
2626 warning_printed = 1;
2627 }
2628 return 0;
2629 }
2630 /* Indexes with higher version than the one supported by GDB may be no
2631 longer backward compatible. */
2632 if (version > 7)
2633 return 0;
2634
2635 map->version = version;
2636 map->total_size = section->size;
2637
2638 metadata = (offset_type *) (addr + sizeof (offset_type));
2639
2640 i = 0;
2641 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2642 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2643 / 8);
2644 ++i;
2645
2646 *types_list = addr + MAYBE_SWAP (metadata[i]);
2647 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2648 - MAYBE_SWAP (metadata[i]))
2649 / 8);
2650 ++i;
2651
2652 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2653 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2654 - MAYBE_SWAP (metadata[i]));
2655 ++i;
2656
2657 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2658 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2659 - MAYBE_SWAP (metadata[i]))
2660 / (2 * sizeof (offset_type)));
2661 ++i;
2662
2663 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2664
2665 return 1;
2666 }
2667
2668
2669 /* Read the index file. If everything went ok, initialize the "quick"
2670 elements of all the CUs and return 1. Otherwise, return 0. */
2671
2672 static int
2673 dwarf2_read_index (struct objfile *objfile)
2674 {
2675 struct mapped_index local_map, *map;
2676 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2677 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2678
2679 if (!read_index_from_section (objfile, objfile->name,
2680 use_deprecated_index_sections,
2681 &dwarf2_per_objfile->gdb_index, &local_map,
2682 &cu_list, &cu_list_elements,
2683 &types_list, &types_list_elements))
2684 return 0;
2685
2686 /* Don't use the index if it's empty. */
2687 if (local_map.symbol_table_slots == 0)
2688 return 0;
2689
2690 /* If there is a .dwz file, read it so we can get its CU list as
2691 well. */
2692 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2693 {
2694 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2695 struct mapped_index dwz_map;
2696 const gdb_byte *dwz_types_ignore;
2697 offset_type dwz_types_elements_ignore;
2698
2699 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2700 1,
2701 &dwz->gdb_index, &dwz_map,
2702 &dwz_list, &dwz_list_elements,
2703 &dwz_types_ignore,
2704 &dwz_types_elements_ignore))
2705 {
2706 warning (_("could not read '.gdb_index' section from %s; skipping"),
2707 bfd_get_filename (dwz->dwz_bfd));
2708 return 0;
2709 }
2710 }
2711
2712 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2713 dwz_list, dwz_list_elements))
2714 return 0;
2715
2716 if (types_list_elements)
2717 {
2718 struct dwarf2_section_info *section;
2719
2720 /* We can only handle a single .debug_types when we have an
2721 index. */
2722 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2723 return 0;
2724
2725 section = VEC_index (dwarf2_section_info_def,
2726 dwarf2_per_objfile->types, 0);
2727
2728 if (!create_signatured_type_table_from_index (objfile, section,
2729 types_list,
2730 types_list_elements))
2731 return 0;
2732 }
2733
2734 create_addrmap_from_index (objfile, &local_map);
2735
2736 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2737 *map = local_map;
2738
2739 dwarf2_per_objfile->index_table = map;
2740 dwarf2_per_objfile->using_index = 1;
2741 dwarf2_per_objfile->quick_file_names_table =
2742 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2743
2744 return 1;
2745 }
2746
2747 /* A helper for the "quick" functions which sets the global
2748 dwarf2_per_objfile according to OBJFILE. */
2749
2750 static void
2751 dw2_setup (struct objfile *objfile)
2752 {
2753 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2754 gdb_assert (dwarf2_per_objfile);
2755 }
2756
2757 /* Reader function for dw2_build_type_unit_groups. */
2758
2759 static void
2760 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2761 gdb_byte *info_ptr,
2762 struct die_info *type_unit_die,
2763 int has_children,
2764 void *data)
2765 {
2766 struct dwarf2_cu *cu = reader->cu;
2767 struct attribute *attr;
2768 struct type_unit_group *tu_group;
2769
2770 gdb_assert (data == NULL);
2771
2772 if (! has_children)
2773 return;
2774
2775 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2776 /* Call this for its side-effect of creating the associated
2777 struct type_unit_group if it doesn't already exist. */
2778 tu_group = get_type_unit_group (cu, attr);
2779 }
2780
2781 /* Build dwarf2_per_objfile->type_unit_groups.
2782 This function may be called multiple times. */
2783
2784 static void
2785 dw2_build_type_unit_groups (void)
2786 {
2787 if (dwarf2_per_objfile->type_unit_groups == NULL)
2788 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2789 }
2790
2791 /* die_reader_func for dw2_get_file_names. */
2792
2793 static void
2794 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2795 gdb_byte *info_ptr,
2796 struct die_info *comp_unit_die,
2797 int has_children,
2798 void *data)
2799 {
2800 struct dwarf2_cu *cu = reader->cu;
2801 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2802 struct objfile *objfile = dwarf2_per_objfile->objfile;
2803 struct dwarf2_per_cu_data *lh_cu;
2804 struct line_header *lh;
2805 struct attribute *attr;
2806 int i;
2807 char *name, *comp_dir;
2808 void **slot;
2809 struct quick_file_names *qfn;
2810 unsigned int line_offset;
2811
2812 /* Our callers never want to match partial units -- instead they
2813 will match the enclosing full CU. */
2814 if (comp_unit_die->tag == DW_TAG_partial_unit)
2815 {
2816 this_cu->v.quick->no_file_data = 1;
2817 return;
2818 }
2819
2820 /* If we're reading the line header for TUs, store it in the "per_cu"
2821 for tu_group. */
2822 if (this_cu->is_debug_types)
2823 {
2824 struct type_unit_group *tu_group = data;
2825
2826 gdb_assert (tu_group != NULL);
2827 lh_cu = &tu_group->per_cu;
2828 }
2829 else
2830 lh_cu = this_cu;
2831
2832 lh = NULL;
2833 slot = NULL;
2834 line_offset = 0;
2835
2836 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2837 if (attr)
2838 {
2839 struct quick_file_names find_entry;
2840
2841 line_offset = DW_UNSND (attr);
2842
2843 /* We may have already read in this line header (TU line header sharing).
2844 If we have we're done. */
2845 find_entry.hash.dwo_unit = cu->dwo_unit;
2846 find_entry.hash.line_offset.sect_off = line_offset;
2847 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2848 &find_entry, INSERT);
2849 if (*slot != NULL)
2850 {
2851 lh_cu->v.quick->file_names = *slot;
2852 return;
2853 }
2854
2855 lh = dwarf_decode_line_header (line_offset, cu);
2856 }
2857 if (lh == NULL)
2858 {
2859 lh_cu->v.quick->no_file_data = 1;
2860 return;
2861 }
2862
2863 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2864 qfn->hash.dwo_unit = cu->dwo_unit;
2865 qfn->hash.line_offset.sect_off = line_offset;
2866 gdb_assert (slot != NULL);
2867 *slot = qfn;
2868
2869 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2870
2871 qfn->num_file_names = lh->num_file_names;
2872 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2873 lh->num_file_names * sizeof (char *));
2874 for (i = 0; i < lh->num_file_names; ++i)
2875 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2876 qfn->real_names = NULL;
2877
2878 free_line_header (lh);
2879
2880 lh_cu->v.quick->file_names = qfn;
2881 }
2882
2883 /* A helper for the "quick" functions which attempts to read the line
2884 table for THIS_CU. */
2885
2886 static struct quick_file_names *
2887 dw2_get_file_names (struct objfile *objfile,
2888 struct dwarf2_per_cu_data *this_cu)
2889 {
2890 /* For TUs this should only be called on the parent group. */
2891 if (this_cu->is_debug_types)
2892 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2893
2894 if (this_cu->v.quick->file_names != NULL)
2895 return this_cu->v.quick->file_names;
2896 /* If we know there is no line data, no point in looking again. */
2897 if (this_cu->v.quick->no_file_data)
2898 return NULL;
2899
2900 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2901 in the stub for CUs, there's is no need to lookup the DWO file.
2902 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2903 DWO file. */
2904 if (this_cu->is_debug_types)
2905 {
2906 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2907
2908 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2909 dw2_get_file_names_reader, tu_group);
2910 }
2911 else
2912 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2913
2914 if (this_cu->v.quick->no_file_data)
2915 return NULL;
2916 return this_cu->v.quick->file_names;
2917 }
2918
2919 /* A helper for the "quick" functions which computes and caches the
2920 real path for a given file name from the line table. */
2921
2922 static const char *
2923 dw2_get_real_path (struct objfile *objfile,
2924 struct quick_file_names *qfn, int index)
2925 {
2926 if (qfn->real_names == NULL)
2927 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2928 qfn->num_file_names, sizeof (char *));
2929
2930 if (qfn->real_names[index] == NULL)
2931 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2932
2933 return qfn->real_names[index];
2934 }
2935
2936 static struct symtab *
2937 dw2_find_last_source_symtab (struct objfile *objfile)
2938 {
2939 int index;
2940
2941 dw2_setup (objfile);
2942 index = dwarf2_per_objfile->n_comp_units - 1;
2943 return dw2_instantiate_symtab (dw2_get_cu (index));
2944 }
2945
2946 /* Traversal function for dw2_forget_cached_source_info. */
2947
2948 static int
2949 dw2_free_cached_file_names (void **slot, void *info)
2950 {
2951 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2952
2953 if (file_data->real_names)
2954 {
2955 int i;
2956
2957 for (i = 0; i < file_data->num_file_names; ++i)
2958 {
2959 xfree ((void*) file_data->real_names[i]);
2960 file_data->real_names[i] = NULL;
2961 }
2962 }
2963
2964 return 1;
2965 }
2966
2967 static void
2968 dw2_forget_cached_source_info (struct objfile *objfile)
2969 {
2970 dw2_setup (objfile);
2971
2972 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2973 dw2_free_cached_file_names, NULL);
2974 }
2975
2976 /* Helper function for dw2_map_symtabs_matching_filename that expands
2977 the symtabs and calls the iterator. */
2978
2979 static int
2980 dw2_map_expand_apply (struct objfile *objfile,
2981 struct dwarf2_per_cu_data *per_cu,
2982 const char *name,
2983 const char *full_path, const char *real_path,
2984 int (*callback) (struct symtab *, void *),
2985 void *data)
2986 {
2987 struct symtab *last_made = objfile->symtabs;
2988
2989 /* Don't visit already-expanded CUs. */
2990 if (per_cu->v.quick->symtab)
2991 return 0;
2992
2993 /* This may expand more than one symtab, and we want to iterate over
2994 all of them. */
2995 dw2_instantiate_symtab (per_cu);
2996
2997 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2998 objfile->symtabs, last_made);
2999 }
3000
3001 /* Implementation of the map_symtabs_matching_filename method. */
3002
3003 static int
3004 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3005 const char *full_path, const char *real_path,
3006 int (*callback) (struct symtab *, void *),
3007 void *data)
3008 {
3009 int i;
3010 const char *name_basename = lbasename (name);
3011 int name_len = strlen (name);
3012 int is_abs = IS_ABSOLUTE_PATH (name);
3013
3014 dw2_setup (objfile);
3015
3016 dw2_build_type_unit_groups ();
3017
3018 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3019 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3020 {
3021 int j;
3022 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3023 struct quick_file_names *file_data;
3024
3025 /* We only need to look at symtabs not already expanded. */
3026 if (per_cu->v.quick->symtab)
3027 continue;
3028
3029 file_data = dw2_get_file_names (objfile, per_cu);
3030 if (file_data == NULL)
3031 continue;
3032
3033 for (j = 0; j < file_data->num_file_names; ++j)
3034 {
3035 const char *this_name = file_data->file_names[j];
3036
3037 if (FILENAME_CMP (name, this_name) == 0
3038 || (!is_abs && compare_filenames_for_search (this_name,
3039 name, name_len)))
3040 {
3041 if (dw2_map_expand_apply (objfile, per_cu,
3042 name, full_path, real_path,
3043 callback, data))
3044 return 1;
3045 }
3046
3047 /* Before we invoke realpath, which can get expensive when many
3048 files are involved, do a quick comparison of the basenames. */
3049 if (! basenames_may_differ
3050 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3051 continue;
3052
3053 if (full_path != NULL)
3054 {
3055 const char *this_real_name = dw2_get_real_path (objfile,
3056 file_data, j);
3057
3058 if (this_real_name != NULL
3059 && (FILENAME_CMP (full_path, this_real_name) == 0
3060 || (!is_abs
3061 && compare_filenames_for_search (this_real_name,
3062 name, name_len))))
3063 {
3064 if (dw2_map_expand_apply (objfile, per_cu,
3065 name, full_path, real_path,
3066 callback, data))
3067 return 1;
3068 }
3069 }
3070
3071 if (real_path != NULL)
3072 {
3073 const char *this_real_name = dw2_get_real_path (objfile,
3074 file_data, j);
3075
3076 if (this_real_name != NULL
3077 && (FILENAME_CMP (real_path, this_real_name) == 0
3078 || (!is_abs
3079 && compare_filenames_for_search (this_real_name,
3080 name, name_len))))
3081 {
3082 if (dw2_map_expand_apply (objfile, per_cu,
3083 name, full_path, real_path,
3084 callback, data))
3085 return 1;
3086 }
3087 }
3088 }
3089 }
3090
3091 return 0;
3092 }
3093
3094 static struct symtab *
3095 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3096 const char *name, domain_enum domain)
3097 {
3098 /* We do all the work in the pre_expand_symtabs_matching hook
3099 instead. */
3100 return NULL;
3101 }
3102
3103 /* A helper function that expands all symtabs that hold an object
3104 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3105 symbols in block BLOCK_KIND. */
3106
3107 static void
3108 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3109 int want_specific_block,
3110 enum block_enum block_kind,
3111 const char *name, domain_enum domain)
3112 {
3113 struct mapped_index *index;
3114
3115 dw2_setup (objfile);
3116
3117 index = dwarf2_per_objfile->index_table;
3118
3119 /* index_table is NULL if OBJF_READNOW. */
3120 if (index)
3121 {
3122 offset_type *vec;
3123
3124 if (find_slot_in_mapped_hash (index, name, &vec))
3125 {
3126 offset_type i, len = MAYBE_SWAP (*vec);
3127 for (i = 0; i < len; ++i)
3128 {
3129 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3130 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3131 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3132 int want_static = block_kind != GLOBAL_BLOCK;
3133 /* This value is only valid for index versions >= 7. */
3134 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3135 gdb_index_symbol_kind symbol_kind =
3136 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3137 /* Only check the symbol attributes if they're present.
3138 Indices prior to version 7 don't record them,
3139 and indices >= 7 may elide them for certain symbols
3140 (gold does this). */
3141 int attrs_valid =
3142 (index->version >= 7
3143 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3144
3145 if (attrs_valid
3146 && want_specific_block
3147 && want_static != is_static)
3148 continue;
3149
3150 /* Only check the symbol's kind if it has one. */
3151 if (attrs_valid)
3152 {
3153 switch (domain)
3154 {
3155 case VAR_DOMAIN:
3156 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3157 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3158 /* Some types are also in VAR_DOMAIN. */
3159 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3160 continue;
3161 break;
3162 case STRUCT_DOMAIN:
3163 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3164 continue;
3165 break;
3166 case LABEL_DOMAIN:
3167 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3168 continue;
3169 break;
3170 default:
3171 break;
3172 }
3173 }
3174
3175 dw2_instantiate_symtab (per_cu);
3176 }
3177 }
3178 }
3179 }
3180
3181 static void
3182 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3183 enum block_enum block_kind, const char *name,
3184 domain_enum domain)
3185 {
3186 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3187 }
3188
3189 static void
3190 dw2_print_stats (struct objfile *objfile)
3191 {
3192 int i, count;
3193
3194 dw2_setup (objfile);
3195 count = 0;
3196 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3197 + dwarf2_per_objfile->n_type_units); ++i)
3198 {
3199 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3200
3201 if (!per_cu->v.quick->symtab)
3202 ++count;
3203 }
3204 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3205 }
3206
3207 static void
3208 dw2_dump (struct objfile *objfile)
3209 {
3210 /* Nothing worth printing. */
3211 }
3212
3213 static void
3214 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3215 struct section_offsets *delta)
3216 {
3217 /* There's nothing to relocate here. */
3218 }
3219
3220 static void
3221 dw2_expand_symtabs_for_function (struct objfile *objfile,
3222 const char *func_name)
3223 {
3224 /* Note: It doesn't matter what we pass for block_kind here. */
3225 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3226 VAR_DOMAIN);
3227 }
3228
3229 static void
3230 dw2_expand_all_symtabs (struct objfile *objfile)
3231 {
3232 int i;
3233
3234 dw2_setup (objfile);
3235
3236 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3237 + dwarf2_per_objfile->n_type_units); ++i)
3238 {
3239 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3240
3241 dw2_instantiate_symtab (per_cu);
3242 }
3243 }
3244
3245 static void
3246 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3247 const char *filename)
3248 {
3249 int i;
3250
3251 dw2_setup (objfile);
3252
3253 /* We don't need to consider type units here.
3254 This is only called for examining code, e.g. expand_line_sal.
3255 There can be an order of magnitude (or more) more type units
3256 than comp units, and we avoid them if we can. */
3257
3258 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3259 {
3260 int j;
3261 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3262 struct quick_file_names *file_data;
3263
3264 /* We only need to look at symtabs not already expanded. */
3265 if (per_cu->v.quick->symtab)
3266 continue;
3267
3268 file_data = dw2_get_file_names (objfile, per_cu);
3269 if (file_data == NULL)
3270 continue;
3271
3272 for (j = 0; j < file_data->num_file_names; ++j)
3273 {
3274 const char *this_name = file_data->file_names[j];
3275 if (FILENAME_CMP (this_name, filename) == 0)
3276 {
3277 dw2_instantiate_symtab (per_cu);
3278 break;
3279 }
3280 }
3281 }
3282 }
3283
3284 /* A helper function for dw2_find_symbol_file that finds the primary
3285 file name for a given CU. This is a die_reader_func. */
3286
3287 static void
3288 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3289 gdb_byte *info_ptr,
3290 struct die_info *comp_unit_die,
3291 int has_children,
3292 void *data)
3293 {
3294 const char **result_ptr = data;
3295 struct dwarf2_cu *cu = reader->cu;
3296 struct attribute *attr;
3297
3298 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3299 if (attr == NULL)
3300 *result_ptr = NULL;
3301 else
3302 *result_ptr = DW_STRING (attr);
3303 }
3304
3305 static const char *
3306 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3307 {
3308 struct dwarf2_per_cu_data *per_cu;
3309 offset_type *vec;
3310 struct quick_file_names *file_data;
3311 const char *filename;
3312
3313 dw2_setup (objfile);
3314
3315 /* index_table is NULL if OBJF_READNOW. */
3316 if (!dwarf2_per_objfile->index_table)
3317 {
3318 struct symtab *s;
3319
3320 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3321 {
3322 struct blockvector *bv = BLOCKVECTOR (s);
3323 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3324 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3325
3326 if (sym)
3327 return sym->symtab->filename;
3328 }
3329 return NULL;
3330 }
3331
3332 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3333 name, &vec))
3334 return NULL;
3335
3336 /* Note that this just looks at the very first one named NAME -- but
3337 actually we are looking for a function. find_main_filename
3338 should be rewritten so that it doesn't require a custom hook. It
3339 could just use the ordinary symbol tables. */
3340 /* vec[0] is the length, which must always be >0. */
3341 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3342
3343 if (per_cu->v.quick->symtab != NULL)
3344 return per_cu->v.quick->symtab->filename;
3345
3346 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3347 dw2_get_primary_filename_reader, &filename);
3348
3349 return filename;
3350 }
3351
3352 static void
3353 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3354 struct objfile *objfile, int global,
3355 int (*callback) (struct block *,
3356 struct symbol *, void *),
3357 void *data, symbol_compare_ftype *match,
3358 symbol_compare_ftype *ordered_compare)
3359 {
3360 /* Currently unimplemented; used for Ada. The function can be called if the
3361 current language is Ada for a non-Ada objfile using GNU index. As Ada
3362 does not look for non-Ada symbols this function should just return. */
3363 }
3364
3365 static void
3366 dw2_expand_symtabs_matching
3367 (struct objfile *objfile,
3368 int (*file_matcher) (const char *, void *),
3369 int (*name_matcher) (const char *, void *),
3370 enum search_domain kind,
3371 void *data)
3372 {
3373 int i;
3374 offset_type iter;
3375 struct mapped_index *index;
3376
3377 dw2_setup (objfile);
3378
3379 /* index_table is NULL if OBJF_READNOW. */
3380 if (!dwarf2_per_objfile->index_table)
3381 return;
3382 index = dwarf2_per_objfile->index_table;
3383
3384 if (file_matcher != NULL)
3385 {
3386 struct cleanup *cleanup;
3387 htab_t visited_found, visited_not_found;
3388
3389 dw2_build_type_unit_groups ();
3390
3391 visited_found = htab_create_alloc (10,
3392 htab_hash_pointer, htab_eq_pointer,
3393 NULL, xcalloc, xfree);
3394 cleanup = make_cleanup_htab_delete (visited_found);
3395 visited_not_found = htab_create_alloc (10,
3396 htab_hash_pointer, htab_eq_pointer,
3397 NULL, xcalloc, xfree);
3398 make_cleanup_htab_delete (visited_not_found);
3399
3400 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3401 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3402 {
3403 int j;
3404 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3405 struct quick_file_names *file_data;
3406 void **slot;
3407
3408 per_cu->v.quick->mark = 0;
3409
3410 /* We only need to look at symtabs not already expanded. */
3411 if (per_cu->v.quick->symtab)
3412 continue;
3413
3414 file_data = dw2_get_file_names (objfile, per_cu);
3415 if (file_data == NULL)
3416 continue;
3417
3418 if (htab_find (visited_not_found, file_data) != NULL)
3419 continue;
3420 else if (htab_find (visited_found, file_data) != NULL)
3421 {
3422 per_cu->v.quick->mark = 1;
3423 continue;
3424 }
3425
3426 for (j = 0; j < file_data->num_file_names; ++j)
3427 {
3428 if (file_matcher (file_data->file_names[j], data))
3429 {
3430 per_cu->v.quick->mark = 1;
3431 break;
3432 }
3433 }
3434
3435 slot = htab_find_slot (per_cu->v.quick->mark
3436 ? visited_found
3437 : visited_not_found,
3438 file_data, INSERT);
3439 *slot = file_data;
3440 }
3441
3442 do_cleanups (cleanup);
3443 }
3444
3445 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3446 {
3447 offset_type idx = 2 * iter;
3448 const char *name;
3449 offset_type *vec, vec_len, vec_idx;
3450
3451 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3452 continue;
3453
3454 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3455
3456 if (! (*name_matcher) (name, data))
3457 continue;
3458
3459 /* The name was matched, now expand corresponding CUs that were
3460 marked. */
3461 vec = (offset_type *) (index->constant_pool
3462 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3463 vec_len = MAYBE_SWAP (vec[0]);
3464 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3465 {
3466 struct dwarf2_per_cu_data *per_cu;
3467 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3468 gdb_index_symbol_kind symbol_kind =
3469 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3470 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3471
3472 /* Don't crash on bad data. */
3473 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3474 + dwarf2_per_objfile->n_type_units))
3475 continue;
3476
3477 /* Only check the symbol's kind if it has one.
3478 Indices prior to version 7 don't record it. */
3479 if (index->version >= 7)
3480 {
3481 switch (kind)
3482 {
3483 case VARIABLES_DOMAIN:
3484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3485 continue;
3486 break;
3487 case FUNCTIONS_DOMAIN:
3488 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3489 continue;
3490 break;
3491 case TYPES_DOMAIN:
3492 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3493 continue;
3494 break;
3495 default:
3496 break;
3497 }
3498 }
3499
3500 per_cu = dw2_get_cu (cu_index);
3501 if (file_matcher == NULL || per_cu->v.quick->mark)
3502 dw2_instantiate_symtab (per_cu);
3503 }
3504 }
3505 }
3506
3507 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3508 symtab. */
3509
3510 static struct symtab *
3511 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3512 {
3513 int i;
3514
3515 if (BLOCKVECTOR (symtab) != NULL
3516 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3517 return symtab;
3518
3519 if (symtab->includes == NULL)
3520 return NULL;
3521
3522 for (i = 0; symtab->includes[i]; ++i)
3523 {
3524 struct symtab *s = symtab->includes[i];
3525
3526 s = recursively_find_pc_sect_symtab (s, pc);
3527 if (s != NULL)
3528 return s;
3529 }
3530
3531 return NULL;
3532 }
3533
3534 static struct symtab *
3535 dw2_find_pc_sect_symtab (struct objfile *objfile,
3536 struct minimal_symbol *msymbol,
3537 CORE_ADDR pc,
3538 struct obj_section *section,
3539 int warn_if_readin)
3540 {
3541 struct dwarf2_per_cu_data *data;
3542 struct symtab *result;
3543
3544 dw2_setup (objfile);
3545
3546 if (!objfile->psymtabs_addrmap)
3547 return NULL;
3548
3549 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3550 if (!data)
3551 return NULL;
3552
3553 if (warn_if_readin && data->v.quick->symtab)
3554 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3555 paddress (get_objfile_arch (objfile), pc));
3556
3557 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3558 gdb_assert (result != NULL);
3559 return result;
3560 }
3561
3562 static void
3563 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3564 void *data, int need_fullname)
3565 {
3566 int i;
3567 struct cleanup *cleanup;
3568 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3569 NULL, xcalloc, xfree);
3570
3571 cleanup = make_cleanup_htab_delete (visited);
3572 dw2_setup (objfile);
3573
3574 dw2_build_type_unit_groups ();
3575
3576 /* We can ignore file names coming from already-expanded CUs. */
3577 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3578 + dwarf2_per_objfile->n_type_units); ++i)
3579 {
3580 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3581
3582 if (per_cu->v.quick->symtab)
3583 {
3584 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3585 INSERT);
3586
3587 *slot = per_cu->v.quick->file_names;
3588 }
3589 }
3590
3591 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3592 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3593 {
3594 int j;
3595 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3596 struct quick_file_names *file_data;
3597 void **slot;
3598
3599 /* We only need to look at symtabs not already expanded. */
3600 if (per_cu->v.quick->symtab)
3601 continue;
3602
3603 file_data = dw2_get_file_names (objfile, per_cu);
3604 if (file_data == NULL)
3605 continue;
3606
3607 slot = htab_find_slot (visited, file_data, INSERT);
3608 if (*slot)
3609 {
3610 /* Already visited. */
3611 continue;
3612 }
3613 *slot = file_data;
3614
3615 for (j = 0; j < file_data->num_file_names; ++j)
3616 {
3617 const char *this_real_name;
3618
3619 if (need_fullname)
3620 this_real_name = dw2_get_real_path (objfile, file_data, j);
3621 else
3622 this_real_name = NULL;
3623 (*fun) (file_data->file_names[j], this_real_name, data);
3624 }
3625 }
3626
3627 do_cleanups (cleanup);
3628 }
3629
3630 static int
3631 dw2_has_symbols (struct objfile *objfile)
3632 {
3633 return 1;
3634 }
3635
3636 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3637 {
3638 dw2_has_symbols,
3639 dw2_find_last_source_symtab,
3640 dw2_forget_cached_source_info,
3641 dw2_map_symtabs_matching_filename,
3642 dw2_lookup_symbol,
3643 dw2_pre_expand_symtabs_matching,
3644 dw2_print_stats,
3645 dw2_dump,
3646 dw2_relocate,
3647 dw2_expand_symtabs_for_function,
3648 dw2_expand_all_symtabs,
3649 dw2_expand_symtabs_with_filename,
3650 dw2_find_symbol_file,
3651 dw2_map_matching_symbols,
3652 dw2_expand_symtabs_matching,
3653 dw2_find_pc_sect_symtab,
3654 dw2_map_symbol_filenames
3655 };
3656
3657 /* Initialize for reading DWARF for this objfile. Return 0 if this
3658 file will use psymtabs, or 1 if using the GNU index. */
3659
3660 int
3661 dwarf2_initialize_objfile (struct objfile *objfile)
3662 {
3663 /* If we're about to read full symbols, don't bother with the
3664 indices. In this case we also don't care if some other debug
3665 format is making psymtabs, because they are all about to be
3666 expanded anyway. */
3667 if ((objfile->flags & OBJF_READNOW))
3668 {
3669 int i;
3670
3671 dwarf2_per_objfile->using_index = 1;
3672 create_all_comp_units (objfile);
3673 create_all_type_units (objfile);
3674 dwarf2_per_objfile->quick_file_names_table =
3675 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3676
3677 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3678 + dwarf2_per_objfile->n_type_units); ++i)
3679 {
3680 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3681
3682 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3683 struct dwarf2_per_cu_quick_data);
3684 }
3685
3686 /* Return 1 so that gdb sees the "quick" functions. However,
3687 these functions will be no-ops because we will have expanded
3688 all symtabs. */
3689 return 1;
3690 }
3691
3692 if (dwarf2_read_index (objfile))
3693 return 1;
3694
3695 return 0;
3696 }
3697
3698 \f
3699
3700 /* Build a partial symbol table. */
3701
3702 void
3703 dwarf2_build_psymtabs (struct objfile *objfile)
3704 {
3705 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3706 {
3707 init_psymbol_list (objfile, 1024);
3708 }
3709
3710 dwarf2_build_psymtabs_hard (objfile);
3711 }
3712
3713 /* Return the total length of the CU described by HEADER. */
3714
3715 static unsigned int
3716 get_cu_length (const struct comp_unit_head *header)
3717 {
3718 return header->initial_length_size + header->length;
3719 }
3720
3721 /* Return TRUE if OFFSET is within CU_HEADER. */
3722
3723 static inline int
3724 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3725 {
3726 sect_offset bottom = { cu_header->offset.sect_off };
3727 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3728
3729 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3730 }
3731
3732 /* Find the base address of the compilation unit for range lists and
3733 location lists. It will normally be specified by DW_AT_low_pc.
3734 In DWARF-3 draft 4, the base address could be overridden by
3735 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3736 compilation units with discontinuous ranges. */
3737
3738 static void
3739 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3740 {
3741 struct attribute *attr;
3742
3743 cu->base_known = 0;
3744 cu->base_address = 0;
3745
3746 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3747 if (attr)
3748 {
3749 cu->base_address = DW_ADDR (attr);
3750 cu->base_known = 1;
3751 }
3752 else
3753 {
3754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3755 if (attr)
3756 {
3757 cu->base_address = DW_ADDR (attr);
3758 cu->base_known = 1;
3759 }
3760 }
3761 }
3762
3763 /* Read in the comp unit header information from the debug_info at info_ptr.
3764 NOTE: This leaves members offset, first_die_offset to be filled in
3765 by the caller. */
3766
3767 static gdb_byte *
3768 read_comp_unit_head (struct comp_unit_head *cu_header,
3769 gdb_byte *info_ptr, bfd *abfd)
3770 {
3771 int signed_addr;
3772 unsigned int bytes_read;
3773
3774 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3775 cu_header->initial_length_size = bytes_read;
3776 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3777 info_ptr += bytes_read;
3778 cu_header->version = read_2_bytes (abfd, info_ptr);
3779 info_ptr += 2;
3780 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3781 &bytes_read);
3782 info_ptr += bytes_read;
3783 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3784 info_ptr += 1;
3785 signed_addr = bfd_get_sign_extend_vma (abfd);
3786 if (signed_addr < 0)
3787 internal_error (__FILE__, __LINE__,
3788 _("read_comp_unit_head: dwarf from non elf file"));
3789 cu_header->signed_addr_p = signed_addr;
3790
3791 return info_ptr;
3792 }
3793
3794 /* Helper function that returns the proper abbrev section for
3795 THIS_CU. */
3796
3797 static struct dwarf2_section_info *
3798 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3799 {
3800 struct dwarf2_section_info *abbrev;
3801
3802 if (this_cu->is_dwz)
3803 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3804 else
3805 abbrev = &dwarf2_per_objfile->abbrev;
3806
3807 return abbrev;
3808 }
3809
3810 /* Subroutine of read_and_check_comp_unit_head and
3811 read_and_check_type_unit_head to simplify them.
3812 Perform various error checking on the header. */
3813
3814 static void
3815 error_check_comp_unit_head (struct comp_unit_head *header,
3816 struct dwarf2_section_info *section,
3817 struct dwarf2_section_info *abbrev_section)
3818 {
3819 bfd *abfd = section->asection->owner;
3820 const char *filename = bfd_get_filename (abfd);
3821
3822 if (header->version != 2 && header->version != 3 && header->version != 4)
3823 error (_("Dwarf Error: wrong version in compilation unit header "
3824 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3825 filename);
3826
3827 if (header->abbrev_offset.sect_off
3828 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3829 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3830 "(offset 0x%lx + 6) [in module %s]"),
3831 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3832 filename);
3833
3834 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3835 avoid potential 32-bit overflow. */
3836 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3837 > section->size)
3838 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3839 "(offset 0x%lx + 0) [in module %s]"),
3840 (long) header->length, (long) header->offset.sect_off,
3841 filename);
3842 }
3843
3844 /* Read in a CU/TU header and perform some basic error checking.
3845 The contents of the header are stored in HEADER.
3846 The result is a pointer to the start of the first DIE. */
3847
3848 static gdb_byte *
3849 read_and_check_comp_unit_head (struct comp_unit_head *header,
3850 struct dwarf2_section_info *section,
3851 struct dwarf2_section_info *abbrev_section,
3852 gdb_byte *info_ptr,
3853 int is_debug_types_section)
3854 {
3855 gdb_byte *beg_of_comp_unit = info_ptr;
3856 bfd *abfd = section->asection->owner;
3857
3858 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3859
3860 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3861
3862 /* If we're reading a type unit, skip over the signature and
3863 type_offset fields. */
3864 if (is_debug_types_section)
3865 info_ptr += 8 /*signature*/ + header->offset_size;
3866
3867 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3868
3869 error_check_comp_unit_head (header, section, abbrev_section);
3870
3871 return info_ptr;
3872 }
3873
3874 /* Read in the types comp unit header information from .debug_types entry at
3875 types_ptr. The result is a pointer to one past the end of the header. */
3876
3877 static gdb_byte *
3878 read_and_check_type_unit_head (struct comp_unit_head *header,
3879 struct dwarf2_section_info *section,
3880 struct dwarf2_section_info *abbrev_section,
3881 gdb_byte *info_ptr,
3882 ULONGEST *signature,
3883 cu_offset *type_offset_in_tu)
3884 {
3885 gdb_byte *beg_of_comp_unit = info_ptr;
3886 bfd *abfd = section->asection->owner;
3887
3888 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3889
3890 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3891
3892 /* If we're reading a type unit, skip over the signature and
3893 type_offset fields. */
3894 if (signature != NULL)
3895 *signature = read_8_bytes (abfd, info_ptr);
3896 info_ptr += 8;
3897 if (type_offset_in_tu != NULL)
3898 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3899 header->offset_size);
3900 info_ptr += header->offset_size;
3901
3902 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3903
3904 error_check_comp_unit_head (header, section, abbrev_section);
3905
3906 return info_ptr;
3907 }
3908
3909 /* Fetch the abbreviation table offset from a comp or type unit header. */
3910
3911 static sect_offset
3912 read_abbrev_offset (struct dwarf2_section_info *section,
3913 sect_offset offset)
3914 {
3915 bfd *abfd = section->asection->owner;
3916 gdb_byte *info_ptr;
3917 unsigned int length, initial_length_size, offset_size;
3918 sect_offset abbrev_offset;
3919
3920 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3921 info_ptr = section->buffer + offset.sect_off;
3922 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3923 offset_size = initial_length_size == 4 ? 4 : 8;
3924 info_ptr += initial_length_size + 2 /*version*/;
3925 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3926 return abbrev_offset;
3927 }
3928
3929 /* Allocate a new partial symtab for file named NAME and mark this new
3930 partial symtab as being an include of PST. */
3931
3932 static void
3933 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3934 struct objfile *objfile)
3935 {
3936 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3937
3938 subpst->section_offsets = pst->section_offsets;
3939 subpst->textlow = 0;
3940 subpst->texthigh = 0;
3941
3942 subpst->dependencies = (struct partial_symtab **)
3943 obstack_alloc (&objfile->objfile_obstack,
3944 sizeof (struct partial_symtab *));
3945 subpst->dependencies[0] = pst;
3946 subpst->number_of_dependencies = 1;
3947
3948 subpst->globals_offset = 0;
3949 subpst->n_global_syms = 0;
3950 subpst->statics_offset = 0;
3951 subpst->n_static_syms = 0;
3952 subpst->symtab = NULL;
3953 subpst->read_symtab = pst->read_symtab;
3954 subpst->readin = 0;
3955
3956 /* No private part is necessary for include psymtabs. This property
3957 can be used to differentiate between such include psymtabs and
3958 the regular ones. */
3959 subpst->read_symtab_private = NULL;
3960 }
3961
3962 /* Read the Line Number Program data and extract the list of files
3963 included by the source file represented by PST. Build an include
3964 partial symtab for each of these included files. */
3965
3966 static void
3967 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3968 struct die_info *die,
3969 struct partial_symtab *pst)
3970 {
3971 struct line_header *lh = NULL;
3972 struct attribute *attr;
3973
3974 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3975 if (attr)
3976 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3977 if (lh == NULL)
3978 return; /* No linetable, so no includes. */
3979
3980 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3981 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3982
3983 free_line_header (lh);
3984 }
3985
3986 static hashval_t
3987 hash_signatured_type (const void *item)
3988 {
3989 const struct signatured_type *sig_type = item;
3990
3991 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3992 return sig_type->signature;
3993 }
3994
3995 static int
3996 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3997 {
3998 const struct signatured_type *lhs = item_lhs;
3999 const struct signatured_type *rhs = item_rhs;
4000
4001 return lhs->signature == rhs->signature;
4002 }
4003
4004 /* Allocate a hash table for signatured types. */
4005
4006 static htab_t
4007 allocate_signatured_type_table (struct objfile *objfile)
4008 {
4009 return htab_create_alloc_ex (41,
4010 hash_signatured_type,
4011 eq_signatured_type,
4012 NULL,
4013 &objfile->objfile_obstack,
4014 hashtab_obstack_allocate,
4015 dummy_obstack_deallocate);
4016 }
4017
4018 /* A helper function to add a signatured type CU to a table. */
4019
4020 static int
4021 add_signatured_type_cu_to_table (void **slot, void *datum)
4022 {
4023 struct signatured_type *sigt = *slot;
4024 struct signatured_type ***datap = datum;
4025
4026 **datap = sigt;
4027 ++*datap;
4028
4029 return 1;
4030 }
4031
4032 /* Create the hash table of all entries in the .debug_types section.
4033 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4034 The result is a pointer to the hash table or NULL if there are
4035 no types. */
4036
4037 static htab_t
4038 create_debug_types_hash_table (struct dwo_file *dwo_file,
4039 VEC (dwarf2_section_info_def) *types)
4040 {
4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
4042 htab_t types_htab = NULL;
4043 int ix;
4044 struct dwarf2_section_info *section;
4045 struct dwarf2_section_info *abbrev_section;
4046
4047 if (VEC_empty (dwarf2_section_info_def, types))
4048 return NULL;
4049
4050 abbrev_section = (dwo_file != NULL
4051 ? &dwo_file->sections.abbrev
4052 : &dwarf2_per_objfile->abbrev);
4053
4054 if (dwarf2_read_debug)
4055 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4056 dwo_file ? ".dwo" : "",
4057 bfd_get_filename (abbrev_section->asection->owner));
4058
4059 for (ix = 0;
4060 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4061 ++ix)
4062 {
4063 bfd *abfd;
4064 gdb_byte *info_ptr, *end_ptr;
4065 struct dwarf2_section_info *abbrev_section;
4066
4067 dwarf2_read_section (objfile, section);
4068 info_ptr = section->buffer;
4069
4070 if (info_ptr == NULL)
4071 continue;
4072
4073 /* We can't set abfd until now because the section may be empty or
4074 not present, in which case section->asection will be NULL. */
4075 abfd = section->asection->owner;
4076
4077 if (dwo_file)
4078 abbrev_section = &dwo_file->sections.abbrev;
4079 else
4080 abbrev_section = &dwarf2_per_objfile->abbrev;
4081
4082 if (types_htab == NULL)
4083 {
4084 if (dwo_file)
4085 types_htab = allocate_dwo_unit_table (objfile);
4086 else
4087 types_htab = allocate_signatured_type_table (objfile);
4088 }
4089
4090 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4091 because we don't need to read any dies: the signature is in the
4092 header. */
4093
4094 end_ptr = info_ptr + section->size;
4095 while (info_ptr < end_ptr)
4096 {
4097 sect_offset offset;
4098 cu_offset type_offset_in_tu;
4099 ULONGEST signature;
4100 struct signatured_type *sig_type;
4101 struct dwo_unit *dwo_tu;
4102 void **slot;
4103 gdb_byte *ptr = info_ptr;
4104 struct comp_unit_head header;
4105 unsigned int length;
4106
4107 offset.sect_off = ptr - section->buffer;
4108
4109 /* We need to read the type's signature in order to build the hash
4110 table, but we don't need anything else just yet. */
4111
4112 ptr = read_and_check_type_unit_head (&header, section,
4113 abbrev_section, ptr,
4114 &signature, &type_offset_in_tu);
4115
4116 length = get_cu_length (&header);
4117
4118 /* Skip dummy type units. */
4119 if (ptr >= info_ptr + length
4120 || peek_abbrev_code (abfd, ptr) == 0)
4121 {
4122 info_ptr += length;
4123 continue;
4124 }
4125
4126 if (dwo_file)
4127 {
4128 sig_type = NULL;
4129 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4130 struct dwo_unit);
4131 dwo_tu->dwo_file = dwo_file;
4132 dwo_tu->signature = signature;
4133 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4134 dwo_tu->info_or_types_section = section;
4135 dwo_tu->offset = offset;
4136 dwo_tu->length = length;
4137 }
4138 else
4139 {
4140 /* N.B.: type_offset is not usable if this type uses a DWO file.
4141 The real type_offset is in the DWO file. */
4142 dwo_tu = NULL;
4143 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4144 struct signatured_type);
4145 sig_type->signature = signature;
4146 sig_type->type_offset_in_tu = type_offset_in_tu;
4147 sig_type->per_cu.objfile = objfile;
4148 sig_type->per_cu.is_debug_types = 1;
4149 sig_type->per_cu.info_or_types_section = section;
4150 sig_type->per_cu.offset = offset;
4151 sig_type->per_cu.length = length;
4152 }
4153
4154 slot = htab_find_slot (types_htab,
4155 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4156 INSERT);
4157 gdb_assert (slot != NULL);
4158 if (*slot != NULL)
4159 {
4160 sect_offset dup_offset;
4161
4162 if (dwo_file)
4163 {
4164 const struct dwo_unit *dup_tu = *slot;
4165
4166 dup_offset = dup_tu->offset;
4167 }
4168 else
4169 {
4170 const struct signatured_type *dup_tu = *slot;
4171
4172 dup_offset = dup_tu->per_cu.offset;
4173 }
4174
4175 complaint (&symfile_complaints,
4176 _("debug type entry at offset 0x%x is duplicate to the "
4177 "entry at offset 0x%x, signature 0x%s"),
4178 offset.sect_off, dup_offset.sect_off,
4179 phex (signature, sizeof (signature)));
4180 }
4181 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4182
4183 if (dwarf2_read_debug)
4184 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4185 offset.sect_off,
4186 phex (signature, sizeof (signature)));
4187
4188 info_ptr += length;
4189 }
4190 }
4191
4192 return types_htab;
4193 }
4194
4195 /* Create the hash table of all entries in the .debug_types section,
4196 and initialize all_type_units.
4197 The result is zero if there is an error (e.g. missing .debug_types section),
4198 otherwise non-zero. */
4199
4200 static int
4201 create_all_type_units (struct objfile *objfile)
4202 {
4203 htab_t types_htab;
4204 struct signatured_type **iter;
4205
4206 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4207 if (types_htab == NULL)
4208 {
4209 dwarf2_per_objfile->signatured_types = NULL;
4210 return 0;
4211 }
4212
4213 dwarf2_per_objfile->signatured_types = types_htab;
4214
4215 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4216 dwarf2_per_objfile->all_type_units
4217 = obstack_alloc (&objfile->objfile_obstack,
4218 dwarf2_per_objfile->n_type_units
4219 * sizeof (struct signatured_type *));
4220 iter = &dwarf2_per_objfile->all_type_units[0];
4221 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4222 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4223 == dwarf2_per_objfile->n_type_units);
4224
4225 return 1;
4226 }
4227
4228 /* Lookup a signature based type for DW_FORM_ref_sig8.
4229 Returns NULL if signature SIG is not present in the table. */
4230
4231 static struct signatured_type *
4232 lookup_signatured_type (ULONGEST sig)
4233 {
4234 struct signatured_type find_entry, *entry;
4235
4236 if (dwarf2_per_objfile->signatured_types == NULL)
4237 {
4238 complaint (&symfile_complaints,
4239 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4240 return NULL;
4241 }
4242
4243 find_entry.signature = sig;
4244 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4245 return entry;
4246 }
4247 \f
4248 /* Low level DIE reading support. */
4249
4250 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4251
4252 static void
4253 init_cu_die_reader (struct die_reader_specs *reader,
4254 struct dwarf2_cu *cu,
4255 struct dwarf2_section_info *section,
4256 struct dwo_file *dwo_file)
4257 {
4258 gdb_assert (section->readin && section->buffer != NULL);
4259 reader->abfd = section->asection->owner;
4260 reader->cu = cu;
4261 reader->dwo_file = dwo_file;
4262 reader->die_section = section;
4263 reader->buffer = section->buffer;
4264 reader->buffer_end = section->buffer + section->size;
4265 }
4266
4267 /* Initialize a CU (or TU) and read its DIEs.
4268 If the CU defers to a DWO file, read the DWO file as well.
4269
4270 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4271 Otherwise the table specified in the comp unit header is read in and used.
4272 This is an optimization for when we already have the abbrev table.
4273
4274 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4275 Otherwise, a new CU is allocated with xmalloc.
4276
4277 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4278 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4279
4280 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4281 linker) then DIE_READER_FUNC will not get called. */
4282
4283 static void
4284 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4285 struct abbrev_table *abbrev_table,
4286 int use_existing_cu, int keep,
4287 die_reader_func_ftype *die_reader_func,
4288 void *data)
4289 {
4290 struct objfile *objfile = dwarf2_per_objfile->objfile;
4291 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4292 bfd *abfd = section->asection->owner;
4293 struct dwarf2_cu *cu;
4294 gdb_byte *begin_info_ptr, *info_ptr;
4295 struct die_reader_specs reader;
4296 struct die_info *comp_unit_die;
4297 int has_children;
4298 struct attribute *attr;
4299 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4300 struct signatured_type *sig_type = NULL;
4301 struct dwarf2_section_info *abbrev_section;
4302 /* Non-zero if CU currently points to a DWO file and we need to
4303 reread it. When this happens we need to reread the skeleton die
4304 before we can reread the DWO file. */
4305 int rereading_dwo_cu = 0;
4306
4307 if (dwarf2_die_debug)
4308 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4309 this_cu->is_debug_types ? "type" : "comp",
4310 this_cu->offset.sect_off);
4311
4312 if (use_existing_cu)
4313 gdb_assert (keep);
4314
4315 cleanups = make_cleanup (null_cleanup, NULL);
4316
4317 /* This is cheap if the section is already read in. */
4318 dwarf2_read_section (objfile, section);
4319
4320 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4321
4322 abbrev_section = get_abbrev_section_for_cu (this_cu);
4323
4324 if (use_existing_cu && this_cu->cu != NULL)
4325 {
4326 cu = this_cu->cu;
4327
4328 /* If this CU is from a DWO file we need to start over, we need to
4329 refetch the attributes from the skeleton CU.
4330 This could be optimized by retrieving those attributes from when we
4331 were here the first time: the previous comp_unit_die was stored in
4332 comp_unit_obstack. But there's no data yet that we need this
4333 optimization. */
4334 if (cu->dwo_unit != NULL)
4335 rereading_dwo_cu = 1;
4336 }
4337 else
4338 {
4339 /* If !use_existing_cu, this_cu->cu must be NULL. */
4340 gdb_assert (this_cu->cu == NULL);
4341
4342 cu = xmalloc (sizeof (*cu));
4343 init_one_comp_unit (cu, this_cu);
4344
4345 /* If an error occurs while loading, release our storage. */
4346 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4347 }
4348
4349 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4350 {
4351 /* We already have the header, there's no need to read it in again. */
4352 info_ptr += cu->header.first_die_offset.cu_off;
4353 }
4354 else
4355 {
4356 if (this_cu->is_debug_types)
4357 {
4358 ULONGEST signature;
4359 cu_offset type_offset_in_tu;
4360
4361 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4362 abbrev_section, info_ptr,
4363 &signature,
4364 &type_offset_in_tu);
4365
4366 /* Since per_cu is the first member of struct signatured_type,
4367 we can go from a pointer to one to a pointer to the other. */
4368 sig_type = (struct signatured_type *) this_cu;
4369 gdb_assert (sig_type->signature == signature);
4370 gdb_assert (sig_type->type_offset_in_tu.cu_off
4371 == type_offset_in_tu.cu_off);
4372 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4373
4374 /* LENGTH has not been set yet for type units if we're
4375 using .gdb_index. */
4376 this_cu->length = get_cu_length (&cu->header);
4377
4378 /* Establish the type offset that can be used to lookup the type. */
4379 sig_type->type_offset_in_section.sect_off =
4380 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4381 }
4382 else
4383 {
4384 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4385 abbrev_section,
4386 info_ptr, 0);
4387
4388 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4389 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4390 }
4391 }
4392
4393 /* Skip dummy compilation units. */
4394 if (info_ptr >= begin_info_ptr + this_cu->length
4395 || peek_abbrev_code (abfd, info_ptr) == 0)
4396 {
4397 do_cleanups (cleanups);
4398 return;
4399 }
4400
4401 /* If we don't have them yet, read the abbrevs for this compilation unit.
4402 And if we need to read them now, make sure they're freed when we're
4403 done. Note that it's important that if the CU had an abbrev table
4404 on entry we don't free it when we're done: Somewhere up the call stack
4405 it may be in use. */
4406 if (abbrev_table != NULL)
4407 {
4408 gdb_assert (cu->abbrev_table == NULL);
4409 gdb_assert (cu->header.abbrev_offset.sect_off
4410 == abbrev_table->offset.sect_off);
4411 cu->abbrev_table = abbrev_table;
4412 }
4413 else if (cu->abbrev_table == NULL)
4414 {
4415 dwarf2_read_abbrevs (cu, abbrev_section);
4416 make_cleanup (dwarf2_free_abbrev_table, cu);
4417 }
4418 else if (rereading_dwo_cu)
4419 {
4420 dwarf2_free_abbrev_table (cu);
4421 dwarf2_read_abbrevs (cu, abbrev_section);
4422 }
4423
4424 /* Read the top level CU/TU die. */
4425 init_cu_die_reader (&reader, cu, section, NULL);
4426 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4427
4428 /* If we have a DWO stub, process it and then read in the DWO file.
4429 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4430 a DWO CU, that this test will fail. */
4431 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4432 if (attr)
4433 {
4434 char *dwo_name = DW_STRING (attr);
4435 const char *comp_dir_string;
4436 struct dwo_unit *dwo_unit;
4437 ULONGEST signature; /* Or dwo_id. */
4438 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4439 int i,num_extra_attrs;
4440 struct dwarf2_section_info *dwo_abbrev_section;
4441
4442 if (has_children)
4443 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4444 " has children (offset 0x%x) [in module %s]"),
4445 this_cu->offset.sect_off, bfd_get_filename (abfd));
4446
4447 /* These attributes aren't processed until later:
4448 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4449 However, the attribute is found in the stub which we won't have later.
4450 In order to not impose this complication on the rest of the code,
4451 we read them here and copy them to the DWO CU/TU die. */
4452
4453 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4454 DWO file. */
4455 stmt_list = NULL;
4456 if (! this_cu->is_debug_types)
4457 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4458 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4459 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4460 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4461 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4462
4463 /* There should be a DW_AT_addr_base attribute here (if needed).
4464 We need the value before we can process DW_FORM_GNU_addr_index. */
4465 cu->addr_base = 0;
4466 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4467 if (attr)
4468 cu->addr_base = DW_UNSND (attr);
4469
4470 /* There should be a DW_AT_ranges_base attribute here (if needed).
4471 We need the value before we can process DW_AT_ranges. */
4472 cu->ranges_base = 0;
4473 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4474 if (attr)
4475 cu->ranges_base = DW_UNSND (attr);
4476
4477 if (this_cu->is_debug_types)
4478 {
4479 gdb_assert (sig_type != NULL);
4480 signature = sig_type->signature;
4481 }
4482 else
4483 {
4484 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4485 if (! attr)
4486 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4487 dwo_name);
4488 signature = DW_UNSND (attr);
4489 }
4490
4491 /* We may need the comp_dir in order to find the DWO file. */
4492 comp_dir_string = NULL;
4493 if (comp_dir)
4494 comp_dir_string = DW_STRING (comp_dir);
4495
4496 if (this_cu->is_debug_types)
4497 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4498 else
4499 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4500 signature);
4501
4502 if (dwo_unit == NULL)
4503 {
4504 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4505 " with ID %s [in module %s]"),
4506 this_cu->offset.sect_off,
4507 phex (signature, sizeof (signature)),
4508 objfile->name);
4509 }
4510
4511 /* Set up for reading the DWO CU/TU. */
4512 cu->dwo_unit = dwo_unit;
4513 section = dwo_unit->info_or_types_section;
4514 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4515 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4516 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4517
4518 if (this_cu->is_debug_types)
4519 {
4520 ULONGEST signature;
4521
4522 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4523 dwo_abbrev_section,
4524 info_ptr,
4525 &signature, NULL);
4526 gdb_assert (sig_type->signature == signature);
4527 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4528 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4529
4530 /* Establish the type offset that can be used to lookup the type.
4531 For DWO files, we don't know it until now. */
4532 sig_type->type_offset_in_section.sect_off =
4533 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4534 }
4535 else
4536 {
4537 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4538 dwo_abbrev_section,
4539 info_ptr, 0);
4540 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4541 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4542 }
4543
4544 /* Discard the original CU's abbrev table, and read the DWO's. */
4545 if (abbrev_table == NULL)
4546 {
4547 dwarf2_free_abbrev_table (cu);
4548 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4549 }
4550 else
4551 {
4552 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4553 make_cleanup (dwarf2_free_abbrev_table, cu);
4554 }
4555
4556 /* Read in the die, but leave space to copy over the attributes
4557 from the stub. This has the benefit of simplifying the rest of
4558 the code - all the real work is done here. */
4559 num_extra_attrs = ((stmt_list != NULL)
4560 + (low_pc != NULL)
4561 + (high_pc != NULL)
4562 + (ranges != NULL)
4563 + (comp_dir != NULL));
4564 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4565 &has_children, num_extra_attrs);
4566
4567 /* Copy over the attributes from the stub to the DWO die. */
4568 i = comp_unit_die->num_attrs;
4569 if (stmt_list != NULL)
4570 comp_unit_die->attrs[i++] = *stmt_list;
4571 if (low_pc != NULL)
4572 comp_unit_die->attrs[i++] = *low_pc;
4573 if (high_pc != NULL)
4574 comp_unit_die->attrs[i++] = *high_pc;
4575 if (ranges != NULL)
4576 comp_unit_die->attrs[i++] = *ranges;
4577 if (comp_dir != NULL)
4578 comp_unit_die->attrs[i++] = *comp_dir;
4579 comp_unit_die->num_attrs += num_extra_attrs;
4580
4581 /* Skip dummy compilation units. */
4582 if (info_ptr >= begin_info_ptr + dwo_unit->length
4583 || peek_abbrev_code (abfd, info_ptr) == 0)
4584 {
4585 do_cleanups (cleanups);
4586 return;
4587 }
4588 }
4589
4590 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4591
4592 if (free_cu_cleanup != NULL)
4593 {
4594 if (keep)
4595 {
4596 /* We've successfully allocated this compilation unit. Let our
4597 caller clean it up when finished with it. */
4598 discard_cleanups (free_cu_cleanup);
4599
4600 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4601 So we have to manually free the abbrev table. */
4602 dwarf2_free_abbrev_table (cu);
4603
4604 /* Link this CU into read_in_chain. */
4605 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4606 dwarf2_per_objfile->read_in_chain = this_cu;
4607 }
4608 else
4609 do_cleanups (free_cu_cleanup);
4610 }
4611
4612 do_cleanups (cleanups);
4613 }
4614
4615 /* Read CU/TU THIS_CU in section SECTION,
4616 but do not follow DW_AT_GNU_dwo_name if present.
4617 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4618 have already done the lookup to find the DWO file).
4619
4620 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4621 THIS_CU->is_debug_types, but nothing else.
4622
4623 We fill in THIS_CU->length.
4624
4625 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4626 linker) then DIE_READER_FUNC will not get called.
4627
4628 THIS_CU->cu is always freed when done.
4629 This is done in order to not leave THIS_CU->cu in a state where we have
4630 to care whether it refers to the "main" CU or the DWO CU. */
4631
4632 static void
4633 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4634 struct dwarf2_section_info *abbrev_section,
4635 struct dwo_file *dwo_file,
4636 die_reader_func_ftype *die_reader_func,
4637 void *data)
4638 {
4639 struct objfile *objfile = dwarf2_per_objfile->objfile;
4640 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4641 bfd *abfd = section->asection->owner;
4642 struct dwarf2_cu cu;
4643 gdb_byte *begin_info_ptr, *info_ptr;
4644 struct die_reader_specs reader;
4645 struct cleanup *cleanups;
4646 struct die_info *comp_unit_die;
4647 int has_children;
4648
4649 if (dwarf2_die_debug)
4650 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4651 this_cu->is_debug_types ? "type" : "comp",
4652 this_cu->offset.sect_off);
4653
4654 gdb_assert (this_cu->cu == NULL);
4655
4656 /* This is cheap if the section is already read in. */
4657 dwarf2_read_section (objfile, section);
4658
4659 init_one_comp_unit (&cu, this_cu);
4660
4661 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4662
4663 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4664 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4665 abbrev_section, info_ptr,
4666 this_cu->is_debug_types);
4667
4668 this_cu->length = get_cu_length (&cu.header);
4669
4670 /* Skip dummy compilation units. */
4671 if (info_ptr >= begin_info_ptr + this_cu->length
4672 || peek_abbrev_code (abfd, info_ptr) == 0)
4673 {
4674 do_cleanups (cleanups);
4675 return;
4676 }
4677
4678 dwarf2_read_abbrevs (&cu, abbrev_section);
4679 make_cleanup (dwarf2_free_abbrev_table, &cu);
4680
4681 init_cu_die_reader (&reader, &cu, section, dwo_file);
4682 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4683
4684 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4685
4686 do_cleanups (cleanups);
4687 }
4688
4689 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4690 does not lookup the specified DWO file.
4691 This cannot be used to read DWO files.
4692
4693 THIS_CU->cu is always freed when done.
4694 This is done in order to not leave THIS_CU->cu in a state where we have
4695 to care whether it refers to the "main" CU or the DWO CU.
4696 We can revisit this if the data shows there's a performance issue. */
4697
4698 static void
4699 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4700 die_reader_func_ftype *die_reader_func,
4701 void *data)
4702 {
4703 init_cutu_and_read_dies_no_follow (this_cu,
4704 get_abbrev_section_for_cu (this_cu),
4705 NULL,
4706 die_reader_func, data);
4707 }
4708
4709 /* Create a psymtab named NAME and assign it to PER_CU.
4710
4711 The caller must fill in the following details:
4712 dirname, textlow, texthigh. */
4713
4714 static struct partial_symtab *
4715 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4716 {
4717 struct objfile *objfile = per_cu->objfile;
4718 struct partial_symtab *pst;
4719
4720 pst = start_psymtab_common (objfile, objfile->section_offsets,
4721 name, 0,
4722 objfile->global_psymbols.next,
4723 objfile->static_psymbols.next);
4724
4725 pst->psymtabs_addrmap_supported = 1;
4726
4727 /* This is the glue that links PST into GDB's symbol API. */
4728 pst->read_symtab_private = per_cu;
4729 pst->read_symtab = dwarf2_psymtab_to_symtab;
4730 per_cu->v.psymtab = pst;
4731
4732 return pst;
4733 }
4734
4735 /* die_reader_func for process_psymtab_comp_unit. */
4736
4737 static void
4738 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4739 gdb_byte *info_ptr,
4740 struct die_info *comp_unit_die,
4741 int has_children,
4742 void *data)
4743 {
4744 struct dwarf2_cu *cu = reader->cu;
4745 struct objfile *objfile = cu->objfile;
4746 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4747 struct attribute *attr;
4748 CORE_ADDR baseaddr;
4749 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4750 struct partial_symtab *pst;
4751 int has_pc_info;
4752 const char *filename;
4753 int *want_partial_unit_ptr = data;
4754
4755 if (comp_unit_die->tag == DW_TAG_partial_unit
4756 && (want_partial_unit_ptr == NULL
4757 || !*want_partial_unit_ptr))
4758 return;
4759
4760 gdb_assert (! per_cu->is_debug_types);
4761
4762 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4763
4764 cu->list_in_scope = &file_symbols;
4765
4766 /* Allocate a new partial symbol table structure. */
4767 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4768 if (attr == NULL || !DW_STRING (attr))
4769 filename = "";
4770 else
4771 filename = DW_STRING (attr);
4772
4773 pst = create_partial_symtab (per_cu, filename);
4774
4775 /* This must be done before calling dwarf2_build_include_psymtabs. */
4776 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4777 if (attr != NULL)
4778 pst->dirname = DW_STRING (attr);
4779
4780 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4781
4782 dwarf2_find_base_address (comp_unit_die, cu);
4783
4784 /* Possibly set the default values of LOWPC and HIGHPC from
4785 `DW_AT_ranges'. */
4786 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4787 &best_highpc, cu, pst);
4788 if (has_pc_info == 1 && best_lowpc < best_highpc)
4789 /* Store the contiguous range if it is not empty; it can be empty for
4790 CUs with no code. */
4791 addrmap_set_empty (objfile->psymtabs_addrmap,
4792 best_lowpc + baseaddr,
4793 best_highpc + baseaddr - 1, pst);
4794
4795 /* Check if comp unit has_children.
4796 If so, read the rest of the partial symbols from this comp unit.
4797 If not, there's no more debug_info for this comp unit. */
4798 if (has_children)
4799 {
4800 struct partial_die_info *first_die;
4801 CORE_ADDR lowpc, highpc;
4802
4803 lowpc = ((CORE_ADDR) -1);
4804 highpc = ((CORE_ADDR) 0);
4805
4806 first_die = load_partial_dies (reader, info_ptr, 1);
4807
4808 scan_partial_symbols (first_die, &lowpc, &highpc,
4809 ! has_pc_info, cu);
4810
4811 /* If we didn't find a lowpc, set it to highpc to avoid
4812 complaints from `maint check'. */
4813 if (lowpc == ((CORE_ADDR) -1))
4814 lowpc = highpc;
4815
4816 /* If the compilation unit didn't have an explicit address range,
4817 then use the information extracted from its child dies. */
4818 if (! has_pc_info)
4819 {
4820 best_lowpc = lowpc;
4821 best_highpc = highpc;
4822 }
4823 }
4824 pst->textlow = best_lowpc + baseaddr;
4825 pst->texthigh = best_highpc + baseaddr;
4826
4827 pst->n_global_syms = objfile->global_psymbols.next -
4828 (objfile->global_psymbols.list + pst->globals_offset);
4829 pst->n_static_syms = objfile->static_psymbols.next -
4830 (objfile->static_psymbols.list + pst->statics_offset);
4831 sort_pst_symbols (pst);
4832
4833 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4834 {
4835 int i;
4836 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4837 struct dwarf2_per_cu_data *iter;
4838
4839 /* Fill in 'dependencies' here; we fill in 'users' in a
4840 post-pass. */
4841 pst->number_of_dependencies = len;
4842 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4843 len * sizeof (struct symtab *));
4844 for (i = 0;
4845 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4846 i, iter);
4847 ++i)
4848 pst->dependencies[i] = iter->v.psymtab;
4849
4850 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4851 }
4852
4853 /* Get the list of files included in the current compilation unit,
4854 and build a psymtab for each of them. */
4855 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4856
4857 if (dwarf2_read_debug)
4858 {
4859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4860
4861 fprintf_unfiltered (gdb_stdlog,
4862 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4863 ", %d global, %d static syms\n",
4864 per_cu->is_debug_types ? "type" : "comp",
4865 per_cu->offset.sect_off,
4866 paddress (gdbarch, pst->textlow),
4867 paddress (gdbarch, pst->texthigh),
4868 pst->n_global_syms, pst->n_static_syms);
4869 }
4870 }
4871
4872 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4873 Process compilation unit THIS_CU for a psymtab. */
4874
4875 static void
4876 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4877 int want_partial_unit)
4878 {
4879 /* If this compilation unit was already read in, free the
4880 cached copy in order to read it in again. This is
4881 necessary because we skipped some symbols when we first
4882 read in the compilation unit (see load_partial_dies).
4883 This problem could be avoided, but the benefit is unclear. */
4884 if (this_cu->cu != NULL)
4885 free_one_cached_comp_unit (this_cu);
4886
4887 gdb_assert (! this_cu->is_debug_types);
4888 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4889 process_psymtab_comp_unit_reader,
4890 &want_partial_unit);
4891
4892 /* Age out any secondary CUs. */
4893 age_cached_comp_units ();
4894 }
4895
4896 static hashval_t
4897 hash_type_unit_group (const void *item)
4898 {
4899 const struct type_unit_group *tu_group = item;
4900
4901 return hash_stmt_list_entry (&tu_group->hash);
4902 }
4903
4904 static int
4905 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4906 {
4907 const struct type_unit_group *lhs = item_lhs;
4908 const struct type_unit_group *rhs = item_rhs;
4909
4910 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4911 }
4912
4913 /* Allocate a hash table for type unit groups. */
4914
4915 static htab_t
4916 allocate_type_unit_groups_table (void)
4917 {
4918 return htab_create_alloc_ex (3,
4919 hash_type_unit_group,
4920 eq_type_unit_group,
4921 NULL,
4922 &dwarf2_per_objfile->objfile->objfile_obstack,
4923 hashtab_obstack_allocate,
4924 dummy_obstack_deallocate);
4925 }
4926
4927 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4928 partial symtabs. We combine several TUs per psymtab to not let the size
4929 of any one psymtab grow too big. */
4930 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4931 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4932
4933 /* Helper routine for get_type_unit_group.
4934 Create the type_unit_group object used to hold one or more TUs. */
4935
4936 static struct type_unit_group *
4937 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4938 {
4939 struct objfile *objfile = dwarf2_per_objfile->objfile;
4940 struct dwarf2_per_cu_data *per_cu;
4941 struct type_unit_group *tu_group;
4942
4943 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4944 struct type_unit_group);
4945 per_cu = &tu_group->per_cu;
4946 per_cu->objfile = objfile;
4947 per_cu->is_debug_types = 1;
4948 per_cu->s.type_unit_group = tu_group;
4949
4950 if (dwarf2_per_objfile->using_index)
4951 {
4952 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4953 struct dwarf2_per_cu_quick_data);
4954 tu_group->t.first_tu = cu->per_cu;
4955 }
4956 else
4957 {
4958 unsigned int line_offset = line_offset_struct.sect_off;
4959 struct partial_symtab *pst;
4960 char *name;
4961
4962 /* Give the symtab a useful name for debug purposes. */
4963 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4964 name = xstrprintf ("<type_units_%d>",
4965 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4966 else
4967 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4968
4969 pst = create_partial_symtab (per_cu, name);
4970 pst->anonymous = 1;
4971
4972 xfree (name);
4973 }
4974
4975 tu_group->hash.dwo_unit = cu->dwo_unit;
4976 tu_group->hash.line_offset = line_offset_struct;
4977
4978 return tu_group;
4979 }
4980
4981 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4982 STMT_LIST is a DW_AT_stmt_list attribute. */
4983
4984 static struct type_unit_group *
4985 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4986 {
4987 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4988 struct type_unit_group *tu_group;
4989 void **slot;
4990 unsigned int line_offset;
4991 struct type_unit_group type_unit_group_for_lookup;
4992
4993 if (dwarf2_per_objfile->type_unit_groups == NULL)
4994 {
4995 dwarf2_per_objfile->type_unit_groups =
4996 allocate_type_unit_groups_table ();
4997 }
4998
4999 /* Do we need to create a new group, or can we use an existing one? */
5000
5001 if (stmt_list)
5002 {
5003 line_offset = DW_UNSND (stmt_list);
5004 ++tu_stats->nr_symtab_sharers;
5005 }
5006 else
5007 {
5008 /* Ugh, no stmt_list. Rare, but we have to handle it.
5009 We can do various things here like create one group per TU or
5010 spread them over multiple groups to split up the expansion work.
5011 To avoid worst case scenarios (too many groups or too large groups)
5012 we, umm, group them in bunches. */
5013 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5014 | (tu_stats->nr_stmt_less_type_units
5015 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5016 ++tu_stats->nr_stmt_less_type_units;
5017 }
5018
5019 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5020 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5021 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5022 &type_unit_group_for_lookup, INSERT);
5023 if (*slot != NULL)
5024 {
5025 tu_group = *slot;
5026 gdb_assert (tu_group != NULL);
5027 }
5028 else
5029 {
5030 sect_offset line_offset_struct;
5031
5032 line_offset_struct.sect_off = line_offset;
5033 tu_group = create_type_unit_group (cu, line_offset_struct);
5034 *slot = tu_group;
5035 ++tu_stats->nr_symtabs;
5036 }
5037
5038 return tu_group;
5039 }
5040
5041 /* Struct used to sort TUs by their abbreviation table offset. */
5042
5043 struct tu_abbrev_offset
5044 {
5045 struct signatured_type *sig_type;
5046 sect_offset abbrev_offset;
5047 };
5048
5049 /* Helper routine for build_type_unit_groups, passed to qsort. */
5050
5051 static int
5052 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5053 {
5054 const struct tu_abbrev_offset * const *a = ap;
5055 const struct tu_abbrev_offset * const *b = bp;
5056 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5057 unsigned int boff = (*b)->abbrev_offset.sect_off;
5058
5059 return (aoff > boff) - (aoff < boff);
5060 }
5061
5062 /* A helper function to add a type_unit_group to a table. */
5063
5064 static int
5065 add_type_unit_group_to_table (void **slot, void *datum)
5066 {
5067 struct type_unit_group *tu_group = *slot;
5068 struct type_unit_group ***datap = datum;
5069
5070 **datap = tu_group;
5071 ++*datap;
5072
5073 return 1;
5074 }
5075
5076 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5077 each one passing FUNC,DATA.
5078
5079 The efficiency is because we sort TUs by the abbrev table they use and
5080 only read each abbrev table once. In one program there are 200K TUs
5081 sharing 8K abbrev tables.
5082
5083 The main purpose of this function is to support building the
5084 dwarf2_per_objfile->type_unit_groups table.
5085 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5086 can collapse the search space by grouping them by stmt_list.
5087 The savings can be significant, in the same program from above the 200K TUs
5088 share 8K stmt_list tables.
5089
5090 FUNC is expected to call get_type_unit_group, which will create the
5091 struct type_unit_group if necessary and add it to
5092 dwarf2_per_objfile->type_unit_groups. */
5093
5094 static void
5095 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5096 {
5097 struct objfile *objfile = dwarf2_per_objfile->objfile;
5098 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5099 struct cleanup *cleanups;
5100 struct abbrev_table *abbrev_table;
5101 sect_offset abbrev_offset;
5102 struct tu_abbrev_offset *sorted_by_abbrev;
5103 struct type_unit_group **iter;
5104 int i;
5105
5106 /* It's up to the caller to not call us multiple times. */
5107 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5108
5109 if (dwarf2_per_objfile->n_type_units == 0)
5110 return;
5111
5112 /* TUs typically share abbrev tables, and there can be way more TUs than
5113 abbrev tables. Sort by abbrev table to reduce the number of times we
5114 read each abbrev table in.
5115 Alternatives are to punt or to maintain a cache of abbrev tables.
5116 This is simpler and efficient enough for now.
5117
5118 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5119 symtab to use). Typically TUs with the same abbrev offset have the same
5120 stmt_list value too so in practice this should work well.
5121
5122 The basic algorithm here is:
5123
5124 sort TUs by abbrev table
5125 for each TU with same abbrev table:
5126 read abbrev table if first user
5127 read TU top level DIE
5128 [IWBN if DWO skeletons had DW_AT_stmt_list]
5129 call FUNC */
5130
5131 if (dwarf2_read_debug)
5132 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5133
5134 /* Sort in a separate table to maintain the order of all_type_units
5135 for .gdb_index: TU indices directly index all_type_units. */
5136 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5137 dwarf2_per_objfile->n_type_units);
5138 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5139 {
5140 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5141
5142 sorted_by_abbrev[i].sig_type = sig_type;
5143 sorted_by_abbrev[i].abbrev_offset =
5144 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5145 sig_type->per_cu.offset);
5146 }
5147 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5148 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5149 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5150
5151 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5152 called any number of times, so we don't reset tu_stats here. */
5153
5154 abbrev_offset.sect_off = ~(unsigned) 0;
5155 abbrev_table = NULL;
5156 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5157
5158 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5159 {
5160 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5161
5162 /* Switch to the next abbrev table if necessary. */
5163 if (abbrev_table == NULL
5164 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5165 {
5166 if (abbrev_table != NULL)
5167 {
5168 abbrev_table_free (abbrev_table);
5169 /* Reset to NULL in case abbrev_table_read_table throws
5170 an error: abbrev_table_free_cleanup will get called. */
5171 abbrev_table = NULL;
5172 }
5173 abbrev_offset = tu->abbrev_offset;
5174 abbrev_table =
5175 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5176 abbrev_offset);
5177 ++tu_stats->nr_uniq_abbrev_tables;
5178 }
5179
5180 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5181 func, data);
5182 }
5183
5184 /* Create a vector of pointers to primary type units to make it easy to
5185 iterate over them and CUs. See dw2_get_primary_cu. */
5186 dwarf2_per_objfile->n_type_unit_groups =
5187 htab_elements (dwarf2_per_objfile->type_unit_groups);
5188 dwarf2_per_objfile->all_type_unit_groups =
5189 obstack_alloc (&objfile->objfile_obstack,
5190 dwarf2_per_objfile->n_type_unit_groups
5191 * sizeof (struct type_unit_group *));
5192 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5193 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5194 add_type_unit_group_to_table, &iter);
5195 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5196 == dwarf2_per_objfile->n_type_unit_groups);
5197
5198 do_cleanups (cleanups);
5199
5200 if (dwarf2_read_debug)
5201 {
5202 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5203 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5204 dwarf2_per_objfile->n_type_units);
5205 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5206 tu_stats->nr_uniq_abbrev_tables);
5207 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5208 tu_stats->nr_symtabs);
5209 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5210 tu_stats->nr_symtab_sharers);
5211 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5212 tu_stats->nr_stmt_less_type_units);
5213 }
5214 }
5215
5216 /* Reader function for build_type_psymtabs. */
5217
5218 static void
5219 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5220 gdb_byte *info_ptr,
5221 struct die_info *type_unit_die,
5222 int has_children,
5223 void *data)
5224 {
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
5226 struct dwarf2_cu *cu = reader->cu;
5227 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5228 struct type_unit_group *tu_group;
5229 struct attribute *attr;
5230 struct partial_die_info *first_die;
5231 CORE_ADDR lowpc, highpc;
5232 struct partial_symtab *pst;
5233
5234 gdb_assert (data == NULL);
5235
5236 if (! has_children)
5237 return;
5238
5239 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5240 tu_group = get_type_unit_group (cu, attr);
5241
5242 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5243
5244 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5245 cu->list_in_scope = &file_symbols;
5246 pst = create_partial_symtab (per_cu, "");
5247 pst->anonymous = 1;
5248
5249 first_die = load_partial_dies (reader, info_ptr, 1);
5250
5251 lowpc = (CORE_ADDR) -1;
5252 highpc = (CORE_ADDR) 0;
5253 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5254
5255 pst->n_global_syms = objfile->global_psymbols.next -
5256 (objfile->global_psymbols.list + pst->globals_offset);
5257 pst->n_static_syms = objfile->static_psymbols.next -
5258 (objfile->static_psymbols.list + pst->statics_offset);
5259 sort_pst_symbols (pst);
5260 }
5261
5262 /* Traversal function for build_type_psymtabs. */
5263
5264 static int
5265 build_type_psymtab_dependencies (void **slot, void *info)
5266 {
5267 struct objfile *objfile = dwarf2_per_objfile->objfile;
5268 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5269 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5270 struct partial_symtab *pst = per_cu->v.psymtab;
5271 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5272 struct dwarf2_per_cu_data *iter;
5273 int i;
5274
5275 gdb_assert (len > 0);
5276
5277 pst->number_of_dependencies = len;
5278 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5279 len * sizeof (struct psymtab *));
5280 for (i = 0;
5281 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5282 ++i)
5283 {
5284 pst->dependencies[i] = iter->v.psymtab;
5285 iter->s.type_unit_group = tu_group;
5286 }
5287
5288 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5289
5290 return 1;
5291 }
5292
5293 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5294 Build partial symbol tables for the .debug_types comp-units. */
5295
5296 static void
5297 build_type_psymtabs (struct objfile *objfile)
5298 {
5299 if (! create_all_type_units (objfile))
5300 return;
5301
5302 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5303
5304 /* Now that all TUs have been processed we can fill in the dependencies. */
5305 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5306 build_type_psymtab_dependencies, NULL);
5307 }
5308
5309 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5310
5311 static void
5312 psymtabs_addrmap_cleanup (void *o)
5313 {
5314 struct objfile *objfile = o;
5315
5316 objfile->psymtabs_addrmap = NULL;
5317 }
5318
5319 /* Compute the 'user' field for each psymtab in OBJFILE. */
5320
5321 static void
5322 set_partial_user (struct objfile *objfile)
5323 {
5324 int i;
5325
5326 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5327 {
5328 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5329 struct partial_symtab *pst = per_cu->v.psymtab;
5330 int j;
5331
5332 if (pst == NULL)
5333 continue;
5334
5335 for (j = 0; j < pst->number_of_dependencies; ++j)
5336 {
5337 /* Set the 'user' field only if it is not already set. */
5338 if (pst->dependencies[j]->user == NULL)
5339 pst->dependencies[j]->user = pst;
5340 }
5341 }
5342 }
5343
5344 /* Build the partial symbol table by doing a quick pass through the
5345 .debug_info and .debug_abbrev sections. */
5346
5347 static void
5348 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5349 {
5350 struct cleanup *back_to, *addrmap_cleanup;
5351 struct obstack temp_obstack;
5352 int i;
5353
5354 if (dwarf2_read_debug)
5355 {
5356 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5357 objfile->name);
5358 }
5359
5360 dwarf2_per_objfile->reading_partial_symbols = 1;
5361
5362 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5363
5364 /* Any cached compilation units will be linked by the per-objfile
5365 read_in_chain. Make sure to free them when we're done. */
5366 back_to = make_cleanup (free_cached_comp_units, NULL);
5367
5368 build_type_psymtabs (objfile);
5369
5370 create_all_comp_units (objfile);
5371
5372 /* Create a temporary address map on a temporary obstack. We later
5373 copy this to the final obstack. */
5374 obstack_init (&temp_obstack);
5375 make_cleanup_obstack_free (&temp_obstack);
5376 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5377 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5378
5379 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5380 {
5381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5382
5383 process_psymtab_comp_unit (per_cu, 0);
5384 }
5385
5386 set_partial_user (objfile);
5387
5388 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5389 &objfile->objfile_obstack);
5390 discard_cleanups (addrmap_cleanup);
5391
5392 do_cleanups (back_to);
5393
5394 if (dwarf2_read_debug)
5395 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5396 objfile->name);
5397 }
5398
5399 /* die_reader_func for load_partial_comp_unit. */
5400
5401 static void
5402 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5403 gdb_byte *info_ptr,
5404 struct die_info *comp_unit_die,
5405 int has_children,
5406 void *data)
5407 {
5408 struct dwarf2_cu *cu = reader->cu;
5409
5410 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5411
5412 /* Check if comp unit has_children.
5413 If so, read the rest of the partial symbols from this comp unit.
5414 If not, there's no more debug_info for this comp unit. */
5415 if (has_children)
5416 load_partial_dies (reader, info_ptr, 0);
5417 }
5418
5419 /* Load the partial DIEs for a secondary CU into memory.
5420 This is also used when rereading a primary CU with load_all_dies. */
5421
5422 static void
5423 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5424 {
5425 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5426 load_partial_comp_unit_reader, NULL);
5427 }
5428
5429 static void
5430 read_comp_units_from_section (struct objfile *objfile,
5431 struct dwarf2_section_info *section,
5432 unsigned int is_dwz,
5433 int *n_allocated,
5434 int *n_comp_units,
5435 struct dwarf2_per_cu_data ***all_comp_units)
5436 {
5437 gdb_byte *info_ptr;
5438 bfd *abfd = section->asection->owner;
5439
5440 dwarf2_read_section (objfile, section);
5441
5442 info_ptr = section->buffer;
5443
5444 while (info_ptr < section->buffer + section->size)
5445 {
5446 unsigned int length, initial_length_size;
5447 struct dwarf2_per_cu_data *this_cu;
5448 sect_offset offset;
5449
5450 offset.sect_off = info_ptr - section->buffer;
5451
5452 /* Read just enough information to find out where the next
5453 compilation unit is. */
5454 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5455
5456 /* Save the compilation unit for later lookup. */
5457 this_cu = obstack_alloc (&objfile->objfile_obstack,
5458 sizeof (struct dwarf2_per_cu_data));
5459 memset (this_cu, 0, sizeof (*this_cu));
5460 this_cu->offset = offset;
5461 this_cu->length = length + initial_length_size;
5462 this_cu->is_dwz = is_dwz;
5463 this_cu->objfile = objfile;
5464 this_cu->info_or_types_section = section;
5465
5466 if (*n_comp_units == *n_allocated)
5467 {
5468 *n_allocated *= 2;
5469 *all_comp_units = xrealloc (*all_comp_units,
5470 *n_allocated
5471 * sizeof (struct dwarf2_per_cu_data *));
5472 }
5473 (*all_comp_units)[*n_comp_units] = this_cu;
5474 ++*n_comp_units;
5475
5476 info_ptr = info_ptr + this_cu->length;
5477 }
5478 }
5479
5480 /* Create a list of all compilation units in OBJFILE.
5481 This is only done for -readnow and building partial symtabs. */
5482
5483 static void
5484 create_all_comp_units (struct objfile *objfile)
5485 {
5486 int n_allocated;
5487 int n_comp_units;
5488 struct dwarf2_per_cu_data **all_comp_units;
5489
5490 n_comp_units = 0;
5491 n_allocated = 10;
5492 all_comp_units = xmalloc (n_allocated
5493 * sizeof (struct dwarf2_per_cu_data *));
5494
5495 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5496 &n_allocated, &n_comp_units, &all_comp_units);
5497
5498 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5499 {
5500 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5501
5502 read_comp_units_from_section (objfile, &dwz->info, 1,
5503 &n_allocated, &n_comp_units,
5504 &all_comp_units);
5505 }
5506
5507 dwarf2_per_objfile->all_comp_units
5508 = obstack_alloc (&objfile->objfile_obstack,
5509 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5510 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5511 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5512 xfree (all_comp_units);
5513 dwarf2_per_objfile->n_comp_units = n_comp_units;
5514 }
5515
5516 /* Process all loaded DIEs for compilation unit CU, starting at
5517 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5518 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5519 DW_AT_ranges). If NEED_PC is set, then this function will set
5520 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5521 and record the covered ranges in the addrmap. */
5522
5523 static void
5524 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5525 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5526 {
5527 struct partial_die_info *pdi;
5528
5529 /* Now, march along the PDI's, descending into ones which have
5530 interesting children but skipping the children of the other ones,
5531 until we reach the end of the compilation unit. */
5532
5533 pdi = first_die;
5534
5535 while (pdi != NULL)
5536 {
5537 fixup_partial_die (pdi, cu);
5538
5539 /* Anonymous namespaces or modules have no name but have interesting
5540 children, so we need to look at them. Ditto for anonymous
5541 enums. */
5542
5543 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5544 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5545 || pdi->tag == DW_TAG_imported_unit)
5546 {
5547 switch (pdi->tag)
5548 {
5549 case DW_TAG_subprogram:
5550 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5551 break;
5552 case DW_TAG_constant:
5553 case DW_TAG_variable:
5554 case DW_TAG_typedef:
5555 case DW_TAG_union_type:
5556 if (!pdi->is_declaration)
5557 {
5558 add_partial_symbol (pdi, cu);
5559 }
5560 break;
5561 case DW_TAG_class_type:
5562 case DW_TAG_interface_type:
5563 case DW_TAG_structure_type:
5564 if (!pdi->is_declaration)
5565 {
5566 add_partial_symbol (pdi, cu);
5567 }
5568 break;
5569 case DW_TAG_enumeration_type:
5570 if (!pdi->is_declaration)
5571 add_partial_enumeration (pdi, cu);
5572 break;
5573 case DW_TAG_base_type:
5574 case DW_TAG_subrange_type:
5575 /* File scope base type definitions are added to the partial
5576 symbol table. */
5577 add_partial_symbol (pdi, cu);
5578 break;
5579 case DW_TAG_namespace:
5580 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5581 break;
5582 case DW_TAG_module:
5583 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5584 break;
5585 case DW_TAG_imported_unit:
5586 {
5587 struct dwarf2_per_cu_data *per_cu;
5588
5589 /* For now we don't handle imported units in type units. */
5590 if (cu->per_cu->is_debug_types)
5591 {
5592 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5593 " supported in type units [in module %s]"),
5594 cu->objfile->name);
5595 }
5596
5597 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5598 pdi->is_dwz,
5599 cu->objfile);
5600
5601 /* Go read the partial unit, if needed. */
5602 if (per_cu->v.psymtab == NULL)
5603 process_psymtab_comp_unit (per_cu, 1);
5604
5605 VEC_safe_push (dwarf2_per_cu_ptr,
5606 cu->per_cu->s.imported_symtabs, per_cu);
5607 }
5608 break;
5609 default:
5610 break;
5611 }
5612 }
5613
5614 /* If the die has a sibling, skip to the sibling. */
5615
5616 pdi = pdi->die_sibling;
5617 }
5618 }
5619
5620 /* Functions used to compute the fully scoped name of a partial DIE.
5621
5622 Normally, this is simple. For C++, the parent DIE's fully scoped
5623 name is concatenated with "::" and the partial DIE's name. For
5624 Java, the same thing occurs except that "." is used instead of "::".
5625 Enumerators are an exception; they use the scope of their parent
5626 enumeration type, i.e. the name of the enumeration type is not
5627 prepended to the enumerator.
5628
5629 There are two complexities. One is DW_AT_specification; in this
5630 case "parent" means the parent of the target of the specification,
5631 instead of the direct parent of the DIE. The other is compilers
5632 which do not emit DW_TAG_namespace; in this case we try to guess
5633 the fully qualified name of structure types from their members'
5634 linkage names. This must be done using the DIE's children rather
5635 than the children of any DW_AT_specification target. We only need
5636 to do this for structures at the top level, i.e. if the target of
5637 any DW_AT_specification (if any; otherwise the DIE itself) does not
5638 have a parent. */
5639
5640 /* Compute the scope prefix associated with PDI's parent, in
5641 compilation unit CU. The result will be allocated on CU's
5642 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5643 field. NULL is returned if no prefix is necessary. */
5644 static char *
5645 partial_die_parent_scope (struct partial_die_info *pdi,
5646 struct dwarf2_cu *cu)
5647 {
5648 char *grandparent_scope;
5649 struct partial_die_info *parent, *real_pdi;
5650
5651 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5652 then this means the parent of the specification DIE. */
5653
5654 real_pdi = pdi;
5655 while (real_pdi->has_specification)
5656 real_pdi = find_partial_die (real_pdi->spec_offset,
5657 real_pdi->spec_is_dwz, cu);
5658
5659 parent = real_pdi->die_parent;
5660 if (parent == NULL)
5661 return NULL;
5662
5663 if (parent->scope_set)
5664 return parent->scope;
5665
5666 fixup_partial_die (parent, cu);
5667
5668 grandparent_scope = partial_die_parent_scope (parent, cu);
5669
5670 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5671 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5672 Work around this problem here. */
5673 if (cu->language == language_cplus
5674 && parent->tag == DW_TAG_namespace
5675 && strcmp (parent->name, "::") == 0
5676 && grandparent_scope == NULL)
5677 {
5678 parent->scope = NULL;
5679 parent->scope_set = 1;
5680 return NULL;
5681 }
5682
5683 if (pdi->tag == DW_TAG_enumerator)
5684 /* Enumerators should not get the name of the enumeration as a prefix. */
5685 parent->scope = grandparent_scope;
5686 else if (parent->tag == DW_TAG_namespace
5687 || parent->tag == DW_TAG_module
5688 || parent->tag == DW_TAG_structure_type
5689 || parent->tag == DW_TAG_class_type
5690 || parent->tag == DW_TAG_interface_type
5691 || parent->tag == DW_TAG_union_type
5692 || parent->tag == DW_TAG_enumeration_type)
5693 {
5694 if (grandparent_scope == NULL)
5695 parent->scope = parent->name;
5696 else
5697 parent->scope = typename_concat (&cu->comp_unit_obstack,
5698 grandparent_scope,
5699 parent->name, 0, cu);
5700 }
5701 else
5702 {
5703 /* FIXME drow/2004-04-01: What should we be doing with
5704 function-local names? For partial symbols, we should probably be
5705 ignoring them. */
5706 complaint (&symfile_complaints,
5707 _("unhandled containing DIE tag %d for DIE at %d"),
5708 parent->tag, pdi->offset.sect_off);
5709 parent->scope = grandparent_scope;
5710 }
5711
5712 parent->scope_set = 1;
5713 return parent->scope;
5714 }
5715
5716 /* Return the fully scoped name associated with PDI, from compilation unit
5717 CU. The result will be allocated with malloc. */
5718
5719 static char *
5720 partial_die_full_name (struct partial_die_info *pdi,
5721 struct dwarf2_cu *cu)
5722 {
5723 char *parent_scope;
5724
5725 /* If this is a template instantiation, we can not work out the
5726 template arguments from partial DIEs. So, unfortunately, we have
5727 to go through the full DIEs. At least any work we do building
5728 types here will be reused if full symbols are loaded later. */
5729 if (pdi->has_template_arguments)
5730 {
5731 fixup_partial_die (pdi, cu);
5732
5733 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5734 {
5735 struct die_info *die;
5736 struct attribute attr;
5737 struct dwarf2_cu *ref_cu = cu;
5738
5739 /* DW_FORM_ref_addr is using section offset. */
5740 attr.name = 0;
5741 attr.form = DW_FORM_ref_addr;
5742 attr.u.unsnd = pdi->offset.sect_off;
5743 die = follow_die_ref (NULL, &attr, &ref_cu);
5744
5745 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5746 }
5747 }
5748
5749 parent_scope = partial_die_parent_scope (pdi, cu);
5750 if (parent_scope == NULL)
5751 return NULL;
5752 else
5753 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5754 }
5755
5756 static void
5757 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5758 {
5759 struct objfile *objfile = cu->objfile;
5760 CORE_ADDR addr = 0;
5761 char *actual_name = NULL;
5762 CORE_ADDR baseaddr;
5763 int built_actual_name = 0;
5764
5765 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5766
5767 actual_name = partial_die_full_name (pdi, cu);
5768 if (actual_name)
5769 built_actual_name = 1;
5770
5771 if (actual_name == NULL)
5772 actual_name = pdi->name;
5773
5774 switch (pdi->tag)
5775 {
5776 case DW_TAG_subprogram:
5777 if (pdi->is_external || cu->language == language_ada)
5778 {
5779 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5780 of the global scope. But in Ada, we want to be able to access
5781 nested procedures globally. So all Ada subprograms are stored
5782 in the global scope. */
5783 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5784 mst_text, objfile); */
5785 add_psymbol_to_list (actual_name, strlen (actual_name),
5786 built_actual_name,
5787 VAR_DOMAIN, LOC_BLOCK,
5788 &objfile->global_psymbols,
5789 0, pdi->lowpc + baseaddr,
5790 cu->language, objfile);
5791 }
5792 else
5793 {
5794 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5795 mst_file_text, objfile); */
5796 add_psymbol_to_list (actual_name, strlen (actual_name),
5797 built_actual_name,
5798 VAR_DOMAIN, LOC_BLOCK,
5799 &objfile->static_psymbols,
5800 0, pdi->lowpc + baseaddr,
5801 cu->language, objfile);
5802 }
5803 break;
5804 case DW_TAG_constant:
5805 {
5806 struct psymbol_allocation_list *list;
5807
5808 if (pdi->is_external)
5809 list = &objfile->global_psymbols;
5810 else
5811 list = &objfile->static_psymbols;
5812 add_psymbol_to_list (actual_name, strlen (actual_name),
5813 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5814 list, 0, 0, cu->language, objfile);
5815 }
5816 break;
5817 case DW_TAG_variable:
5818 if (pdi->d.locdesc)
5819 addr = decode_locdesc (pdi->d.locdesc, cu);
5820
5821 if (pdi->d.locdesc
5822 && addr == 0
5823 && !dwarf2_per_objfile->has_section_at_zero)
5824 {
5825 /* A global or static variable may also have been stripped
5826 out by the linker if unused, in which case its address
5827 will be nullified; do not add such variables into partial
5828 symbol table then. */
5829 }
5830 else if (pdi->is_external)
5831 {
5832 /* Global Variable.
5833 Don't enter into the minimal symbol tables as there is
5834 a minimal symbol table entry from the ELF symbols already.
5835 Enter into partial symbol table if it has a location
5836 descriptor or a type.
5837 If the location descriptor is missing, new_symbol will create
5838 a LOC_UNRESOLVED symbol, the address of the variable will then
5839 be determined from the minimal symbol table whenever the variable
5840 is referenced.
5841 The address for the partial symbol table entry is not
5842 used by GDB, but it comes in handy for debugging partial symbol
5843 table building. */
5844
5845 if (pdi->d.locdesc || pdi->has_type)
5846 add_psymbol_to_list (actual_name, strlen (actual_name),
5847 built_actual_name,
5848 VAR_DOMAIN, LOC_STATIC,
5849 &objfile->global_psymbols,
5850 0, addr + baseaddr,
5851 cu->language, objfile);
5852 }
5853 else
5854 {
5855 /* Static Variable. Skip symbols without location descriptors. */
5856 if (pdi->d.locdesc == NULL)
5857 {
5858 if (built_actual_name)
5859 xfree (actual_name);
5860 return;
5861 }
5862 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5863 mst_file_data, objfile); */
5864 add_psymbol_to_list (actual_name, strlen (actual_name),
5865 built_actual_name,
5866 VAR_DOMAIN, LOC_STATIC,
5867 &objfile->static_psymbols,
5868 0, addr + baseaddr,
5869 cu->language, objfile);
5870 }
5871 break;
5872 case DW_TAG_typedef:
5873 case DW_TAG_base_type:
5874 case DW_TAG_subrange_type:
5875 add_psymbol_to_list (actual_name, strlen (actual_name),
5876 built_actual_name,
5877 VAR_DOMAIN, LOC_TYPEDEF,
5878 &objfile->static_psymbols,
5879 0, (CORE_ADDR) 0, cu->language, objfile);
5880 break;
5881 case DW_TAG_namespace:
5882 add_psymbol_to_list (actual_name, strlen (actual_name),
5883 built_actual_name,
5884 VAR_DOMAIN, LOC_TYPEDEF,
5885 &objfile->global_psymbols,
5886 0, (CORE_ADDR) 0, cu->language, objfile);
5887 break;
5888 case DW_TAG_class_type:
5889 case DW_TAG_interface_type:
5890 case DW_TAG_structure_type:
5891 case DW_TAG_union_type:
5892 case DW_TAG_enumeration_type:
5893 /* Skip external references. The DWARF standard says in the section
5894 about "Structure, Union, and Class Type Entries": "An incomplete
5895 structure, union or class type is represented by a structure,
5896 union or class entry that does not have a byte size attribute
5897 and that has a DW_AT_declaration attribute." */
5898 if (!pdi->has_byte_size && pdi->is_declaration)
5899 {
5900 if (built_actual_name)
5901 xfree (actual_name);
5902 return;
5903 }
5904
5905 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5906 static vs. global. */
5907 add_psymbol_to_list (actual_name, strlen (actual_name),
5908 built_actual_name,
5909 STRUCT_DOMAIN, LOC_TYPEDEF,
5910 (cu->language == language_cplus
5911 || cu->language == language_java)
5912 ? &objfile->global_psymbols
5913 : &objfile->static_psymbols,
5914 0, (CORE_ADDR) 0, cu->language, objfile);
5915
5916 break;
5917 case DW_TAG_enumerator:
5918 add_psymbol_to_list (actual_name, strlen (actual_name),
5919 built_actual_name,
5920 VAR_DOMAIN, LOC_CONST,
5921 (cu->language == language_cplus
5922 || cu->language == language_java)
5923 ? &objfile->global_psymbols
5924 : &objfile->static_psymbols,
5925 0, (CORE_ADDR) 0, cu->language, objfile);
5926 break;
5927 default:
5928 break;
5929 }
5930
5931 if (built_actual_name)
5932 xfree (actual_name);
5933 }
5934
5935 /* Read a partial die corresponding to a namespace; also, add a symbol
5936 corresponding to that namespace to the symbol table. NAMESPACE is
5937 the name of the enclosing namespace. */
5938
5939 static void
5940 add_partial_namespace (struct partial_die_info *pdi,
5941 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5942 int need_pc, struct dwarf2_cu *cu)
5943 {
5944 /* Add a symbol for the namespace. */
5945
5946 add_partial_symbol (pdi, cu);
5947
5948 /* Now scan partial symbols in that namespace. */
5949
5950 if (pdi->has_children)
5951 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5952 }
5953
5954 /* Read a partial die corresponding to a Fortran module. */
5955
5956 static void
5957 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5958 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5959 {
5960 /* Now scan partial symbols in that module. */
5961
5962 if (pdi->has_children)
5963 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5964 }
5965
5966 /* Read a partial die corresponding to a subprogram and create a partial
5967 symbol for that subprogram. When the CU language allows it, this
5968 routine also defines a partial symbol for each nested subprogram
5969 that this subprogram contains.
5970
5971 DIE my also be a lexical block, in which case we simply search
5972 recursively for suprograms defined inside that lexical block.
5973 Again, this is only performed when the CU language allows this
5974 type of definitions. */
5975
5976 static void
5977 add_partial_subprogram (struct partial_die_info *pdi,
5978 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5979 int need_pc, struct dwarf2_cu *cu)
5980 {
5981 if (pdi->tag == DW_TAG_subprogram)
5982 {
5983 if (pdi->has_pc_info)
5984 {
5985 if (pdi->lowpc < *lowpc)
5986 *lowpc = pdi->lowpc;
5987 if (pdi->highpc > *highpc)
5988 *highpc = pdi->highpc;
5989 if (need_pc)
5990 {
5991 CORE_ADDR baseaddr;
5992 struct objfile *objfile = cu->objfile;
5993
5994 baseaddr = ANOFFSET (objfile->section_offsets,
5995 SECT_OFF_TEXT (objfile));
5996 addrmap_set_empty (objfile->psymtabs_addrmap,
5997 pdi->lowpc + baseaddr,
5998 pdi->highpc - 1 + baseaddr,
5999 cu->per_cu->v.psymtab);
6000 }
6001 }
6002
6003 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6004 {
6005 if (!pdi->is_declaration)
6006 /* Ignore subprogram DIEs that do not have a name, they are
6007 illegal. Do not emit a complaint at this point, we will
6008 do so when we convert this psymtab into a symtab. */
6009 if (pdi->name)
6010 add_partial_symbol (pdi, cu);
6011 }
6012 }
6013
6014 if (! pdi->has_children)
6015 return;
6016
6017 if (cu->language == language_ada)
6018 {
6019 pdi = pdi->die_child;
6020 while (pdi != NULL)
6021 {
6022 fixup_partial_die (pdi, cu);
6023 if (pdi->tag == DW_TAG_subprogram
6024 || pdi->tag == DW_TAG_lexical_block)
6025 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6026 pdi = pdi->die_sibling;
6027 }
6028 }
6029 }
6030
6031 /* Read a partial die corresponding to an enumeration type. */
6032
6033 static void
6034 add_partial_enumeration (struct partial_die_info *enum_pdi,
6035 struct dwarf2_cu *cu)
6036 {
6037 struct partial_die_info *pdi;
6038
6039 if (enum_pdi->name != NULL)
6040 add_partial_symbol (enum_pdi, cu);
6041
6042 pdi = enum_pdi->die_child;
6043 while (pdi)
6044 {
6045 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6046 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6047 else
6048 add_partial_symbol (pdi, cu);
6049 pdi = pdi->die_sibling;
6050 }
6051 }
6052
6053 /* Return the initial uleb128 in the die at INFO_PTR. */
6054
6055 static unsigned int
6056 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6057 {
6058 unsigned int bytes_read;
6059
6060 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6061 }
6062
6063 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6064 Return the corresponding abbrev, or NULL if the number is zero (indicating
6065 an empty DIE). In either case *BYTES_READ will be set to the length of
6066 the initial number. */
6067
6068 static struct abbrev_info *
6069 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6070 struct dwarf2_cu *cu)
6071 {
6072 bfd *abfd = cu->objfile->obfd;
6073 unsigned int abbrev_number;
6074 struct abbrev_info *abbrev;
6075
6076 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6077
6078 if (abbrev_number == 0)
6079 return NULL;
6080
6081 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6082 if (!abbrev)
6083 {
6084 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6085 abbrev_number, bfd_get_filename (abfd));
6086 }
6087
6088 return abbrev;
6089 }
6090
6091 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6092 Returns a pointer to the end of a series of DIEs, terminated by an empty
6093 DIE. Any children of the skipped DIEs will also be skipped. */
6094
6095 static gdb_byte *
6096 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6097 {
6098 struct dwarf2_cu *cu = reader->cu;
6099 struct abbrev_info *abbrev;
6100 unsigned int bytes_read;
6101
6102 while (1)
6103 {
6104 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6105 if (abbrev == NULL)
6106 return info_ptr + bytes_read;
6107 else
6108 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6109 }
6110 }
6111
6112 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6113 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6114 abbrev corresponding to that skipped uleb128 should be passed in
6115 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6116 children. */
6117
6118 static gdb_byte *
6119 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6120 struct abbrev_info *abbrev)
6121 {
6122 unsigned int bytes_read;
6123 struct attribute attr;
6124 bfd *abfd = reader->abfd;
6125 struct dwarf2_cu *cu = reader->cu;
6126 gdb_byte *buffer = reader->buffer;
6127 const gdb_byte *buffer_end = reader->buffer_end;
6128 gdb_byte *start_info_ptr = info_ptr;
6129 unsigned int form, i;
6130
6131 for (i = 0; i < abbrev->num_attrs; i++)
6132 {
6133 /* The only abbrev we care about is DW_AT_sibling. */
6134 if (abbrev->attrs[i].name == DW_AT_sibling)
6135 {
6136 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6137 if (attr.form == DW_FORM_ref_addr)
6138 complaint (&symfile_complaints,
6139 _("ignoring absolute DW_AT_sibling"));
6140 else
6141 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6142 }
6143
6144 /* If it isn't DW_AT_sibling, skip this attribute. */
6145 form = abbrev->attrs[i].form;
6146 skip_attribute:
6147 switch (form)
6148 {
6149 case DW_FORM_ref_addr:
6150 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6151 and later it is offset sized. */
6152 if (cu->header.version == 2)
6153 info_ptr += cu->header.addr_size;
6154 else
6155 info_ptr += cu->header.offset_size;
6156 break;
6157 case DW_FORM_GNU_ref_alt:
6158 info_ptr += cu->header.offset_size;
6159 break;
6160 case DW_FORM_addr:
6161 info_ptr += cu->header.addr_size;
6162 break;
6163 case DW_FORM_data1:
6164 case DW_FORM_ref1:
6165 case DW_FORM_flag:
6166 info_ptr += 1;
6167 break;
6168 case DW_FORM_flag_present:
6169 break;
6170 case DW_FORM_data2:
6171 case DW_FORM_ref2:
6172 info_ptr += 2;
6173 break;
6174 case DW_FORM_data4:
6175 case DW_FORM_ref4:
6176 info_ptr += 4;
6177 break;
6178 case DW_FORM_data8:
6179 case DW_FORM_ref8:
6180 case DW_FORM_ref_sig8:
6181 info_ptr += 8;
6182 break;
6183 case DW_FORM_string:
6184 read_direct_string (abfd, info_ptr, &bytes_read);
6185 info_ptr += bytes_read;
6186 break;
6187 case DW_FORM_sec_offset:
6188 case DW_FORM_strp:
6189 case DW_FORM_GNU_strp_alt:
6190 info_ptr += cu->header.offset_size;
6191 break;
6192 case DW_FORM_exprloc:
6193 case DW_FORM_block:
6194 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6195 info_ptr += bytes_read;
6196 break;
6197 case DW_FORM_block1:
6198 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6199 break;
6200 case DW_FORM_block2:
6201 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6202 break;
6203 case DW_FORM_block4:
6204 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6205 break;
6206 case DW_FORM_sdata:
6207 case DW_FORM_udata:
6208 case DW_FORM_ref_udata:
6209 case DW_FORM_GNU_addr_index:
6210 case DW_FORM_GNU_str_index:
6211 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6212 break;
6213 case DW_FORM_indirect:
6214 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6215 info_ptr += bytes_read;
6216 /* We need to continue parsing from here, so just go back to
6217 the top. */
6218 goto skip_attribute;
6219
6220 default:
6221 error (_("Dwarf Error: Cannot handle %s "
6222 "in DWARF reader [in module %s]"),
6223 dwarf_form_name (form),
6224 bfd_get_filename (abfd));
6225 }
6226 }
6227
6228 if (abbrev->has_children)
6229 return skip_children (reader, info_ptr);
6230 else
6231 return info_ptr;
6232 }
6233
6234 /* Locate ORIG_PDI's sibling.
6235 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6236
6237 static gdb_byte *
6238 locate_pdi_sibling (const struct die_reader_specs *reader,
6239 struct partial_die_info *orig_pdi,
6240 gdb_byte *info_ptr)
6241 {
6242 /* Do we know the sibling already? */
6243
6244 if (orig_pdi->sibling)
6245 return orig_pdi->sibling;
6246
6247 /* Are there any children to deal with? */
6248
6249 if (!orig_pdi->has_children)
6250 return info_ptr;
6251
6252 /* Skip the children the long way. */
6253
6254 return skip_children (reader, info_ptr);
6255 }
6256
6257 /* Expand this partial symbol table into a full symbol table. */
6258
6259 static void
6260 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6261 {
6262 if (pst != NULL)
6263 {
6264 if (pst->readin)
6265 {
6266 warning (_("bug: psymtab for %s is already read in."),
6267 pst->filename);
6268 }
6269 else
6270 {
6271 if (info_verbose)
6272 {
6273 printf_filtered (_("Reading in symbols for %s..."),
6274 pst->filename);
6275 gdb_flush (gdb_stdout);
6276 }
6277
6278 /* Restore our global data. */
6279 dwarf2_per_objfile = objfile_data (pst->objfile,
6280 dwarf2_objfile_data_key);
6281
6282 /* If this psymtab is constructed from a debug-only objfile, the
6283 has_section_at_zero flag will not necessarily be correct. We
6284 can get the correct value for this flag by looking at the data
6285 associated with the (presumably stripped) associated objfile. */
6286 if (pst->objfile->separate_debug_objfile_backlink)
6287 {
6288 struct dwarf2_per_objfile *dpo_backlink
6289 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6290 dwarf2_objfile_data_key);
6291
6292 dwarf2_per_objfile->has_section_at_zero
6293 = dpo_backlink->has_section_at_zero;
6294 }
6295
6296 dwarf2_per_objfile->reading_partial_symbols = 0;
6297
6298 psymtab_to_symtab_1 (pst);
6299
6300 /* Finish up the debug error message. */
6301 if (info_verbose)
6302 printf_filtered (_("done.\n"));
6303 }
6304 }
6305
6306 process_cu_includes ();
6307 }
6308 \f
6309 /* Reading in full CUs. */
6310
6311 /* Add PER_CU to the queue. */
6312
6313 static void
6314 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6315 enum language pretend_language)
6316 {
6317 struct dwarf2_queue_item *item;
6318
6319 per_cu->queued = 1;
6320 item = xmalloc (sizeof (*item));
6321 item->per_cu = per_cu;
6322 item->pretend_language = pretend_language;
6323 item->next = NULL;
6324
6325 if (dwarf2_queue == NULL)
6326 dwarf2_queue = item;
6327 else
6328 dwarf2_queue_tail->next = item;
6329
6330 dwarf2_queue_tail = item;
6331 }
6332
6333 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6334 unit and add it to our queue.
6335 The result is non-zero if PER_CU was queued, otherwise the result is zero
6336 meaning either PER_CU is already queued or it is already loaded. */
6337
6338 static int
6339 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6340 struct dwarf2_per_cu_data *per_cu,
6341 enum language pretend_language)
6342 {
6343 /* We may arrive here during partial symbol reading, if we need full
6344 DIEs to process an unusual case (e.g. template arguments). Do
6345 not queue PER_CU, just tell our caller to load its DIEs. */
6346 if (dwarf2_per_objfile->reading_partial_symbols)
6347 {
6348 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6349 return 1;
6350 return 0;
6351 }
6352
6353 /* Mark the dependence relation so that we don't flush PER_CU
6354 too early. */
6355 dwarf2_add_dependence (this_cu, per_cu);
6356
6357 /* If it's already on the queue, we have nothing to do. */
6358 if (per_cu->queued)
6359 return 0;
6360
6361 /* If the compilation unit is already loaded, just mark it as
6362 used. */
6363 if (per_cu->cu != NULL)
6364 {
6365 per_cu->cu->last_used = 0;
6366 return 0;
6367 }
6368
6369 /* Add it to the queue. */
6370 queue_comp_unit (per_cu, pretend_language);
6371
6372 return 1;
6373 }
6374
6375 /* Process the queue. */
6376
6377 static void
6378 process_queue (void)
6379 {
6380 struct dwarf2_queue_item *item, *next_item;
6381
6382 if (dwarf2_read_debug)
6383 {
6384 fprintf_unfiltered (gdb_stdlog,
6385 "Expanding one or more symtabs of objfile %s ...\n",
6386 dwarf2_per_objfile->objfile->name);
6387 }
6388
6389 /* The queue starts out with one item, but following a DIE reference
6390 may load a new CU, adding it to the end of the queue. */
6391 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6392 {
6393 if (dwarf2_per_objfile->using_index
6394 ? !item->per_cu->v.quick->symtab
6395 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6396 {
6397 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6398
6399 if (dwarf2_read_debug)
6400 {
6401 fprintf_unfiltered (gdb_stdlog,
6402 "Expanding symtab of %s at offset 0x%x\n",
6403 per_cu->is_debug_types ? "TU" : "CU",
6404 per_cu->offset.sect_off);
6405 }
6406
6407 if (per_cu->is_debug_types)
6408 process_full_type_unit (per_cu, item->pretend_language);
6409 else
6410 process_full_comp_unit (per_cu, item->pretend_language);
6411
6412 if (dwarf2_read_debug)
6413 {
6414 fprintf_unfiltered (gdb_stdlog,
6415 "Done expanding %s at offset 0x%x\n",
6416 per_cu->is_debug_types ? "TU" : "CU",
6417 per_cu->offset.sect_off);
6418 }
6419 }
6420
6421 item->per_cu->queued = 0;
6422 next_item = item->next;
6423 xfree (item);
6424 }
6425
6426 dwarf2_queue_tail = NULL;
6427
6428 if (dwarf2_read_debug)
6429 {
6430 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6431 dwarf2_per_objfile->objfile->name);
6432 }
6433 }
6434
6435 /* Free all allocated queue entries. This function only releases anything if
6436 an error was thrown; if the queue was processed then it would have been
6437 freed as we went along. */
6438
6439 static void
6440 dwarf2_release_queue (void *dummy)
6441 {
6442 struct dwarf2_queue_item *item, *last;
6443
6444 item = dwarf2_queue;
6445 while (item)
6446 {
6447 /* Anything still marked queued is likely to be in an
6448 inconsistent state, so discard it. */
6449 if (item->per_cu->queued)
6450 {
6451 if (item->per_cu->cu != NULL)
6452 free_one_cached_comp_unit (item->per_cu);
6453 item->per_cu->queued = 0;
6454 }
6455
6456 last = item;
6457 item = item->next;
6458 xfree (last);
6459 }
6460
6461 dwarf2_queue = dwarf2_queue_tail = NULL;
6462 }
6463
6464 /* Read in full symbols for PST, and anything it depends on. */
6465
6466 static void
6467 psymtab_to_symtab_1 (struct partial_symtab *pst)
6468 {
6469 struct dwarf2_per_cu_data *per_cu;
6470 int i;
6471
6472 if (pst->readin)
6473 return;
6474
6475 for (i = 0; i < pst->number_of_dependencies; i++)
6476 if (!pst->dependencies[i]->readin
6477 && pst->dependencies[i]->user == NULL)
6478 {
6479 /* Inform about additional files that need to be read in. */
6480 if (info_verbose)
6481 {
6482 /* FIXME: i18n: Need to make this a single string. */
6483 fputs_filtered (" ", gdb_stdout);
6484 wrap_here ("");
6485 fputs_filtered ("and ", gdb_stdout);
6486 wrap_here ("");
6487 printf_filtered ("%s...", pst->dependencies[i]->filename);
6488 wrap_here (""); /* Flush output. */
6489 gdb_flush (gdb_stdout);
6490 }
6491 psymtab_to_symtab_1 (pst->dependencies[i]);
6492 }
6493
6494 per_cu = pst->read_symtab_private;
6495
6496 if (per_cu == NULL)
6497 {
6498 /* It's an include file, no symbols to read for it.
6499 Everything is in the parent symtab. */
6500 pst->readin = 1;
6501 return;
6502 }
6503
6504 dw2_do_instantiate_symtab (per_cu);
6505 }
6506
6507 /* Trivial hash function for die_info: the hash value of a DIE
6508 is its offset in .debug_info for this objfile. */
6509
6510 static hashval_t
6511 die_hash (const void *item)
6512 {
6513 const struct die_info *die = item;
6514
6515 return die->offset.sect_off;
6516 }
6517
6518 /* Trivial comparison function for die_info structures: two DIEs
6519 are equal if they have the same offset. */
6520
6521 static int
6522 die_eq (const void *item_lhs, const void *item_rhs)
6523 {
6524 const struct die_info *die_lhs = item_lhs;
6525 const struct die_info *die_rhs = item_rhs;
6526
6527 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6528 }
6529
6530 /* die_reader_func for load_full_comp_unit.
6531 This is identical to read_signatured_type_reader,
6532 but is kept separate for now. */
6533
6534 static void
6535 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6536 gdb_byte *info_ptr,
6537 struct die_info *comp_unit_die,
6538 int has_children,
6539 void *data)
6540 {
6541 struct dwarf2_cu *cu = reader->cu;
6542 enum language *language_ptr = data;
6543
6544 gdb_assert (cu->die_hash == NULL);
6545 cu->die_hash =
6546 htab_create_alloc_ex (cu->header.length / 12,
6547 die_hash,
6548 die_eq,
6549 NULL,
6550 &cu->comp_unit_obstack,
6551 hashtab_obstack_allocate,
6552 dummy_obstack_deallocate);
6553
6554 if (has_children)
6555 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6556 &info_ptr, comp_unit_die);
6557 cu->dies = comp_unit_die;
6558 /* comp_unit_die is not stored in die_hash, no need. */
6559
6560 /* We try not to read any attributes in this function, because not
6561 all CUs needed for references have been loaded yet, and symbol
6562 table processing isn't initialized. But we have to set the CU language,
6563 or we won't be able to build types correctly.
6564 Similarly, if we do not read the producer, we can not apply
6565 producer-specific interpretation. */
6566 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6567 }
6568
6569 /* Load the DIEs associated with PER_CU into memory. */
6570
6571 static void
6572 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6573 enum language pretend_language)
6574 {
6575 gdb_assert (! this_cu->is_debug_types);
6576
6577 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6578 load_full_comp_unit_reader, &pretend_language);
6579 }
6580
6581 /* Add a DIE to the delayed physname list. */
6582
6583 static void
6584 add_to_method_list (struct type *type, int fnfield_index, int index,
6585 const char *name, struct die_info *die,
6586 struct dwarf2_cu *cu)
6587 {
6588 struct delayed_method_info mi;
6589 mi.type = type;
6590 mi.fnfield_index = fnfield_index;
6591 mi.index = index;
6592 mi.name = name;
6593 mi.die = die;
6594 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6595 }
6596
6597 /* A cleanup for freeing the delayed method list. */
6598
6599 static void
6600 free_delayed_list (void *ptr)
6601 {
6602 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6603 if (cu->method_list != NULL)
6604 {
6605 VEC_free (delayed_method_info, cu->method_list);
6606 cu->method_list = NULL;
6607 }
6608 }
6609
6610 /* Compute the physnames of any methods on the CU's method list.
6611
6612 The computation of method physnames is delayed in order to avoid the
6613 (bad) condition that one of the method's formal parameters is of an as yet
6614 incomplete type. */
6615
6616 static void
6617 compute_delayed_physnames (struct dwarf2_cu *cu)
6618 {
6619 int i;
6620 struct delayed_method_info *mi;
6621 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6622 {
6623 const char *physname;
6624 struct fn_fieldlist *fn_flp
6625 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6626 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6627 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6628 }
6629 }
6630
6631 /* Go objects should be embedded in a DW_TAG_module DIE,
6632 and it's not clear if/how imported objects will appear.
6633 To keep Go support simple until that's worked out,
6634 go back through what we've read and create something usable.
6635 We could do this while processing each DIE, and feels kinda cleaner,
6636 but that way is more invasive.
6637 This is to, for example, allow the user to type "p var" or "b main"
6638 without having to specify the package name, and allow lookups
6639 of module.object to work in contexts that use the expression
6640 parser. */
6641
6642 static void
6643 fixup_go_packaging (struct dwarf2_cu *cu)
6644 {
6645 char *package_name = NULL;
6646 struct pending *list;
6647 int i;
6648
6649 for (list = global_symbols; list != NULL; list = list->next)
6650 {
6651 for (i = 0; i < list->nsyms; ++i)
6652 {
6653 struct symbol *sym = list->symbol[i];
6654
6655 if (SYMBOL_LANGUAGE (sym) == language_go
6656 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6657 {
6658 char *this_package_name = go_symbol_package_name (sym);
6659
6660 if (this_package_name == NULL)
6661 continue;
6662 if (package_name == NULL)
6663 package_name = this_package_name;
6664 else
6665 {
6666 if (strcmp (package_name, this_package_name) != 0)
6667 complaint (&symfile_complaints,
6668 _("Symtab %s has objects from two different Go packages: %s and %s"),
6669 (sym->symtab && sym->symtab->filename
6670 ? sym->symtab->filename
6671 : cu->objfile->name),
6672 this_package_name, package_name);
6673 xfree (this_package_name);
6674 }
6675 }
6676 }
6677 }
6678
6679 if (package_name != NULL)
6680 {
6681 struct objfile *objfile = cu->objfile;
6682 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6683 package_name, objfile);
6684 struct symbol *sym;
6685
6686 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6687
6688 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6689 SYMBOL_SET_LANGUAGE (sym, language_go);
6690 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6691 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6692 e.g., "main" finds the "main" module and not C's main(). */
6693 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6694 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6695 SYMBOL_TYPE (sym) = type;
6696
6697 add_symbol_to_list (sym, &global_symbols);
6698
6699 xfree (package_name);
6700 }
6701 }
6702
6703 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6704
6705 /* Return the symtab for PER_CU. This works properly regardless of
6706 whether we're using the index or psymtabs. */
6707
6708 static struct symtab *
6709 get_symtab (struct dwarf2_per_cu_data *per_cu)
6710 {
6711 return (dwarf2_per_objfile->using_index
6712 ? per_cu->v.quick->symtab
6713 : per_cu->v.psymtab->symtab);
6714 }
6715
6716 /* A helper function for computing the list of all symbol tables
6717 included by PER_CU. */
6718
6719 static void
6720 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6721 htab_t all_children,
6722 struct dwarf2_per_cu_data *per_cu)
6723 {
6724 void **slot;
6725 int ix;
6726 struct dwarf2_per_cu_data *iter;
6727
6728 slot = htab_find_slot (all_children, per_cu, INSERT);
6729 if (*slot != NULL)
6730 {
6731 /* This inclusion and its children have been processed. */
6732 return;
6733 }
6734
6735 *slot = per_cu;
6736 /* Only add a CU if it has a symbol table. */
6737 if (get_symtab (per_cu) != NULL)
6738 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6739
6740 for (ix = 0;
6741 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6742 ++ix)
6743 recursively_compute_inclusions (result, all_children, iter);
6744 }
6745
6746 /* Compute the symtab 'includes' fields for the symtab related to
6747 PER_CU. */
6748
6749 static void
6750 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6751 {
6752 gdb_assert (! per_cu->is_debug_types);
6753
6754 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6755 {
6756 int ix, len;
6757 struct dwarf2_per_cu_data *iter;
6758 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6759 htab_t all_children;
6760 struct symtab *symtab = get_symtab (per_cu);
6761
6762 /* If we don't have a symtab, we can just skip this case. */
6763 if (symtab == NULL)
6764 return;
6765
6766 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6767 NULL, xcalloc, xfree);
6768
6769 for (ix = 0;
6770 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6771 ix, iter);
6772 ++ix)
6773 recursively_compute_inclusions (&result_children, all_children, iter);
6774
6775 /* Now we have a transitive closure of all the included CUs, so
6776 we can convert it to a list of symtabs. */
6777 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6778 symtab->includes
6779 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6780 (len + 1) * sizeof (struct symtab *));
6781 for (ix = 0;
6782 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6783 ++ix)
6784 symtab->includes[ix] = get_symtab (iter);
6785 symtab->includes[len] = NULL;
6786
6787 VEC_free (dwarf2_per_cu_ptr, result_children);
6788 htab_delete (all_children);
6789 }
6790 }
6791
6792 /* Compute the 'includes' field for the symtabs of all the CUs we just
6793 read. */
6794
6795 static void
6796 process_cu_includes (void)
6797 {
6798 int ix;
6799 struct dwarf2_per_cu_data *iter;
6800
6801 for (ix = 0;
6802 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6803 ix, iter);
6804 ++ix)
6805 {
6806 if (! iter->is_debug_types)
6807 compute_symtab_includes (iter);
6808 }
6809
6810 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6811 }
6812
6813 /* Generate full symbol information for PER_CU, whose DIEs have
6814 already been loaded into memory. */
6815
6816 static void
6817 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6818 enum language pretend_language)
6819 {
6820 struct dwarf2_cu *cu = per_cu->cu;
6821 struct objfile *objfile = per_cu->objfile;
6822 CORE_ADDR lowpc, highpc;
6823 struct symtab *symtab;
6824 struct cleanup *back_to, *delayed_list_cleanup;
6825 CORE_ADDR baseaddr;
6826 struct block *static_block;
6827
6828 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6829
6830 buildsym_init ();
6831 back_to = make_cleanup (really_free_pendings, NULL);
6832 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6833
6834 cu->list_in_scope = &file_symbols;
6835
6836 cu->language = pretend_language;
6837 cu->language_defn = language_def (cu->language);
6838
6839 /* Do line number decoding in read_file_scope () */
6840 process_die (cu->dies, cu);
6841
6842 /* For now fudge the Go package. */
6843 if (cu->language == language_go)
6844 fixup_go_packaging (cu);
6845
6846 /* Now that we have processed all the DIEs in the CU, all the types
6847 should be complete, and it should now be safe to compute all of the
6848 physnames. */
6849 compute_delayed_physnames (cu);
6850 do_cleanups (delayed_list_cleanup);
6851
6852 /* Some compilers don't define a DW_AT_high_pc attribute for the
6853 compilation unit. If the DW_AT_high_pc is missing, synthesize
6854 it, by scanning the DIE's below the compilation unit. */
6855 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6856
6857 static_block
6858 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6859 per_cu->s.imported_symtabs != NULL);
6860
6861 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6862 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6863 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6864 addrmap to help ensure it has an accurate map of pc values belonging to
6865 this comp unit. */
6866 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6867
6868 symtab = end_symtab_from_static_block (static_block, objfile,
6869 SECT_OFF_TEXT (objfile), 0);
6870
6871 if (symtab != NULL)
6872 {
6873 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6874
6875 /* Set symtab language to language from DW_AT_language. If the
6876 compilation is from a C file generated by language preprocessors, do
6877 not set the language if it was already deduced by start_subfile. */
6878 if (!(cu->language == language_c && symtab->language != language_c))
6879 symtab->language = cu->language;
6880
6881 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6882 produce DW_AT_location with location lists but it can be possibly
6883 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6884 there were bugs in prologue debug info, fixed later in GCC-4.5
6885 by "unwind info for epilogues" patch (which is not directly related).
6886
6887 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6888 needed, it would be wrong due to missing DW_AT_producer there.
6889
6890 Still one can confuse GDB by using non-standard GCC compilation
6891 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6892 */
6893 if (cu->has_loclist && gcc_4_minor >= 5)
6894 symtab->locations_valid = 1;
6895
6896 if (gcc_4_minor >= 5)
6897 symtab->epilogue_unwind_valid = 1;
6898
6899 symtab->call_site_htab = cu->call_site_htab;
6900 }
6901
6902 if (dwarf2_per_objfile->using_index)
6903 per_cu->v.quick->symtab = symtab;
6904 else
6905 {
6906 struct partial_symtab *pst = per_cu->v.psymtab;
6907 pst->symtab = symtab;
6908 pst->readin = 1;
6909 }
6910
6911 /* Push it for inclusion processing later. */
6912 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6913
6914 do_cleanups (back_to);
6915 }
6916
6917 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6918 already been loaded into memory. */
6919
6920 static void
6921 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6922 enum language pretend_language)
6923 {
6924 struct dwarf2_cu *cu = per_cu->cu;
6925 struct objfile *objfile = per_cu->objfile;
6926 struct symtab *symtab;
6927 struct cleanup *back_to, *delayed_list_cleanup;
6928
6929 buildsym_init ();
6930 back_to = make_cleanup (really_free_pendings, NULL);
6931 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6932
6933 cu->list_in_scope = &file_symbols;
6934
6935 cu->language = pretend_language;
6936 cu->language_defn = language_def (cu->language);
6937
6938 /* The symbol tables are set up in read_type_unit_scope. */
6939 process_die (cu->dies, cu);
6940
6941 /* For now fudge the Go package. */
6942 if (cu->language == language_go)
6943 fixup_go_packaging (cu);
6944
6945 /* Now that we have processed all the DIEs in the CU, all the types
6946 should be complete, and it should now be safe to compute all of the
6947 physnames. */
6948 compute_delayed_physnames (cu);
6949 do_cleanups (delayed_list_cleanup);
6950
6951 /* TUs share symbol tables.
6952 If this is the first TU to use this symtab, complete the construction
6953 of it with end_expandable_symtab. Otherwise, complete the addition of
6954 this TU's symbols to the existing symtab. */
6955 if (per_cu->s.type_unit_group->primary_symtab == NULL)
6956 {
6957 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6958 per_cu->s.type_unit_group->primary_symtab = symtab;
6959
6960 if (symtab != NULL)
6961 {
6962 /* Set symtab language to language from DW_AT_language. If the
6963 compilation is from a C file generated by language preprocessors,
6964 do not set the language if it was already deduced by
6965 start_subfile. */
6966 if (!(cu->language == language_c && symtab->language != language_c))
6967 symtab->language = cu->language;
6968 }
6969 }
6970 else
6971 {
6972 augment_type_symtab (objfile,
6973 per_cu->s.type_unit_group->primary_symtab);
6974 symtab = per_cu->s.type_unit_group->primary_symtab;
6975 }
6976
6977 if (dwarf2_per_objfile->using_index)
6978 per_cu->v.quick->symtab = symtab;
6979 else
6980 {
6981 struct partial_symtab *pst = per_cu->v.psymtab;
6982 pst->symtab = symtab;
6983 pst->readin = 1;
6984 }
6985
6986 do_cleanups (back_to);
6987 }
6988
6989 /* Process an imported unit DIE. */
6990
6991 static void
6992 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6993 {
6994 struct attribute *attr;
6995
6996 /* For now we don't handle imported units in type units. */
6997 if (cu->per_cu->is_debug_types)
6998 {
6999 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7000 " supported in type units [in module %s]"),
7001 cu->objfile->name);
7002 }
7003
7004 attr = dwarf2_attr (die, DW_AT_import, cu);
7005 if (attr != NULL)
7006 {
7007 struct dwarf2_per_cu_data *per_cu;
7008 struct symtab *imported_symtab;
7009 sect_offset offset;
7010 int is_dwz;
7011
7012 offset = dwarf2_get_ref_die_offset (attr);
7013 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7014 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7015
7016 /* Queue the unit, if needed. */
7017 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7018 load_full_comp_unit (per_cu, cu->language);
7019
7020 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7021 per_cu);
7022 }
7023 }
7024
7025 /* Process a die and its children. */
7026
7027 static void
7028 process_die (struct die_info *die, struct dwarf2_cu *cu)
7029 {
7030 switch (die->tag)
7031 {
7032 case DW_TAG_padding:
7033 break;
7034 case DW_TAG_compile_unit:
7035 case DW_TAG_partial_unit:
7036 read_file_scope (die, cu);
7037 break;
7038 case DW_TAG_type_unit:
7039 read_type_unit_scope (die, cu);
7040 break;
7041 case DW_TAG_subprogram:
7042 case DW_TAG_inlined_subroutine:
7043 read_func_scope (die, cu);
7044 break;
7045 case DW_TAG_lexical_block:
7046 case DW_TAG_try_block:
7047 case DW_TAG_catch_block:
7048 read_lexical_block_scope (die, cu);
7049 break;
7050 case DW_TAG_GNU_call_site:
7051 read_call_site_scope (die, cu);
7052 break;
7053 case DW_TAG_class_type:
7054 case DW_TAG_interface_type:
7055 case DW_TAG_structure_type:
7056 case DW_TAG_union_type:
7057 process_structure_scope (die, cu);
7058 break;
7059 case DW_TAG_enumeration_type:
7060 process_enumeration_scope (die, cu);
7061 break;
7062
7063 /* These dies have a type, but processing them does not create
7064 a symbol or recurse to process the children. Therefore we can
7065 read them on-demand through read_type_die. */
7066 case DW_TAG_subroutine_type:
7067 case DW_TAG_set_type:
7068 case DW_TAG_array_type:
7069 case DW_TAG_pointer_type:
7070 case DW_TAG_ptr_to_member_type:
7071 case DW_TAG_reference_type:
7072 case DW_TAG_string_type:
7073 break;
7074
7075 case DW_TAG_base_type:
7076 case DW_TAG_subrange_type:
7077 case DW_TAG_typedef:
7078 /* Add a typedef symbol for the type definition, if it has a
7079 DW_AT_name. */
7080 new_symbol (die, read_type_die (die, cu), cu);
7081 break;
7082 case DW_TAG_common_block:
7083 read_common_block (die, cu);
7084 break;
7085 case DW_TAG_common_inclusion:
7086 break;
7087 case DW_TAG_namespace:
7088 processing_has_namespace_info = 1;
7089 read_namespace (die, cu);
7090 break;
7091 case DW_TAG_module:
7092 processing_has_namespace_info = 1;
7093 read_module (die, cu);
7094 break;
7095 case DW_TAG_imported_declaration:
7096 case DW_TAG_imported_module:
7097 processing_has_namespace_info = 1;
7098 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7099 || cu->language != language_fortran))
7100 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7101 dwarf_tag_name (die->tag));
7102 read_import_statement (die, cu);
7103 break;
7104
7105 case DW_TAG_imported_unit:
7106 process_imported_unit_die (die, cu);
7107 break;
7108
7109 default:
7110 new_symbol (die, NULL, cu);
7111 break;
7112 }
7113 }
7114
7115 /* A helper function for dwarf2_compute_name which determines whether DIE
7116 needs to have the name of the scope prepended to the name listed in the
7117 die. */
7118
7119 static int
7120 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7121 {
7122 struct attribute *attr;
7123
7124 switch (die->tag)
7125 {
7126 case DW_TAG_namespace:
7127 case DW_TAG_typedef:
7128 case DW_TAG_class_type:
7129 case DW_TAG_interface_type:
7130 case DW_TAG_structure_type:
7131 case DW_TAG_union_type:
7132 case DW_TAG_enumeration_type:
7133 case DW_TAG_enumerator:
7134 case DW_TAG_subprogram:
7135 case DW_TAG_member:
7136 return 1;
7137
7138 case DW_TAG_variable:
7139 case DW_TAG_constant:
7140 /* We only need to prefix "globally" visible variables. These include
7141 any variable marked with DW_AT_external or any variable that
7142 lives in a namespace. [Variables in anonymous namespaces
7143 require prefixing, but they are not DW_AT_external.] */
7144
7145 if (dwarf2_attr (die, DW_AT_specification, cu))
7146 {
7147 struct dwarf2_cu *spec_cu = cu;
7148
7149 return die_needs_namespace (die_specification (die, &spec_cu),
7150 spec_cu);
7151 }
7152
7153 attr = dwarf2_attr (die, DW_AT_external, cu);
7154 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7155 && die->parent->tag != DW_TAG_module)
7156 return 0;
7157 /* A variable in a lexical block of some kind does not need a
7158 namespace, even though in C++ such variables may be external
7159 and have a mangled name. */
7160 if (die->parent->tag == DW_TAG_lexical_block
7161 || die->parent->tag == DW_TAG_try_block
7162 || die->parent->tag == DW_TAG_catch_block
7163 || die->parent->tag == DW_TAG_subprogram)
7164 return 0;
7165 return 1;
7166
7167 default:
7168 return 0;
7169 }
7170 }
7171
7172 /* Retrieve the last character from a mem_file. */
7173
7174 static void
7175 do_ui_file_peek_last (void *object, const char *buffer, long length)
7176 {
7177 char *last_char_p = (char *) object;
7178
7179 if (length > 0)
7180 *last_char_p = buffer[length - 1];
7181 }
7182
7183 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7184 compute the physname for the object, which include a method's:
7185 - formal parameters (C++/Java),
7186 - receiver type (Go),
7187 - return type (Java).
7188
7189 The term "physname" is a bit confusing.
7190 For C++, for example, it is the demangled name.
7191 For Go, for example, it's the mangled name.
7192
7193 For Ada, return the DIE's linkage name rather than the fully qualified
7194 name. PHYSNAME is ignored..
7195
7196 The result is allocated on the objfile_obstack and canonicalized. */
7197
7198 static const char *
7199 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7200 int physname)
7201 {
7202 struct objfile *objfile = cu->objfile;
7203
7204 if (name == NULL)
7205 name = dwarf2_name (die, cu);
7206
7207 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7208 compute it by typename_concat inside GDB. */
7209 if (cu->language == language_ada
7210 || (cu->language == language_fortran && physname))
7211 {
7212 /* For Ada unit, we prefer the linkage name over the name, as
7213 the former contains the exported name, which the user expects
7214 to be able to reference. Ideally, we want the user to be able
7215 to reference this entity using either natural or linkage name,
7216 but we haven't started looking at this enhancement yet. */
7217 struct attribute *attr;
7218
7219 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7220 if (attr == NULL)
7221 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7222 if (attr && DW_STRING (attr))
7223 return DW_STRING (attr);
7224 }
7225
7226 /* These are the only languages we know how to qualify names in. */
7227 if (name != NULL
7228 && (cu->language == language_cplus || cu->language == language_java
7229 || cu->language == language_fortran))
7230 {
7231 if (die_needs_namespace (die, cu))
7232 {
7233 long length;
7234 const char *prefix;
7235 struct ui_file *buf;
7236
7237 prefix = determine_prefix (die, cu);
7238 buf = mem_fileopen ();
7239 if (*prefix != '\0')
7240 {
7241 char *prefixed_name = typename_concat (NULL, prefix, name,
7242 physname, cu);
7243
7244 fputs_unfiltered (prefixed_name, buf);
7245 xfree (prefixed_name);
7246 }
7247 else
7248 fputs_unfiltered (name, buf);
7249
7250 /* Template parameters may be specified in the DIE's DW_AT_name, or
7251 as children with DW_TAG_template_type_param or
7252 DW_TAG_value_type_param. If the latter, add them to the name
7253 here. If the name already has template parameters, then
7254 skip this step; some versions of GCC emit both, and
7255 it is more efficient to use the pre-computed name.
7256
7257 Something to keep in mind about this process: it is very
7258 unlikely, or in some cases downright impossible, to produce
7259 something that will match the mangled name of a function.
7260 If the definition of the function has the same debug info,
7261 we should be able to match up with it anyway. But fallbacks
7262 using the minimal symbol, for instance to find a method
7263 implemented in a stripped copy of libstdc++, will not work.
7264 If we do not have debug info for the definition, we will have to
7265 match them up some other way.
7266
7267 When we do name matching there is a related problem with function
7268 templates; two instantiated function templates are allowed to
7269 differ only by their return types, which we do not add here. */
7270
7271 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7272 {
7273 struct attribute *attr;
7274 struct die_info *child;
7275 int first = 1;
7276
7277 die->building_fullname = 1;
7278
7279 for (child = die->child; child != NULL; child = child->sibling)
7280 {
7281 struct type *type;
7282 LONGEST value;
7283 gdb_byte *bytes;
7284 struct dwarf2_locexpr_baton *baton;
7285 struct value *v;
7286
7287 if (child->tag != DW_TAG_template_type_param
7288 && child->tag != DW_TAG_template_value_param)
7289 continue;
7290
7291 if (first)
7292 {
7293 fputs_unfiltered ("<", buf);
7294 first = 0;
7295 }
7296 else
7297 fputs_unfiltered (", ", buf);
7298
7299 attr = dwarf2_attr (child, DW_AT_type, cu);
7300 if (attr == NULL)
7301 {
7302 complaint (&symfile_complaints,
7303 _("template parameter missing DW_AT_type"));
7304 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7305 continue;
7306 }
7307 type = die_type (child, cu);
7308
7309 if (child->tag == DW_TAG_template_type_param)
7310 {
7311 c_print_type (type, "", buf, -1, 0);
7312 continue;
7313 }
7314
7315 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7316 if (attr == NULL)
7317 {
7318 complaint (&symfile_complaints,
7319 _("template parameter missing "
7320 "DW_AT_const_value"));
7321 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7322 continue;
7323 }
7324
7325 dwarf2_const_value_attr (attr, type, name,
7326 &cu->comp_unit_obstack, cu,
7327 &value, &bytes, &baton);
7328
7329 if (TYPE_NOSIGN (type))
7330 /* GDB prints characters as NUMBER 'CHAR'. If that's
7331 changed, this can use value_print instead. */
7332 c_printchar (value, type, buf);
7333 else
7334 {
7335 struct value_print_options opts;
7336
7337 if (baton != NULL)
7338 v = dwarf2_evaluate_loc_desc (type, NULL,
7339 baton->data,
7340 baton->size,
7341 baton->per_cu);
7342 else if (bytes != NULL)
7343 {
7344 v = allocate_value (type);
7345 memcpy (value_contents_writeable (v), bytes,
7346 TYPE_LENGTH (type));
7347 }
7348 else
7349 v = value_from_longest (type, value);
7350
7351 /* Specify decimal so that we do not depend on
7352 the radix. */
7353 get_formatted_print_options (&opts, 'd');
7354 opts.raw = 1;
7355 value_print (v, buf, &opts);
7356 release_value (v);
7357 value_free (v);
7358 }
7359 }
7360
7361 die->building_fullname = 0;
7362
7363 if (!first)
7364 {
7365 /* Close the argument list, with a space if necessary
7366 (nested templates). */
7367 char last_char = '\0';
7368 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7369 if (last_char == '>')
7370 fputs_unfiltered (" >", buf);
7371 else
7372 fputs_unfiltered (">", buf);
7373 }
7374 }
7375
7376 /* For Java and C++ methods, append formal parameter type
7377 information, if PHYSNAME. */
7378
7379 if (physname && die->tag == DW_TAG_subprogram
7380 && (cu->language == language_cplus
7381 || cu->language == language_java))
7382 {
7383 struct type *type = read_type_die (die, cu);
7384
7385 c_type_print_args (type, buf, 1, cu->language);
7386
7387 if (cu->language == language_java)
7388 {
7389 /* For java, we must append the return type to method
7390 names. */
7391 if (die->tag == DW_TAG_subprogram)
7392 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7393 0, 0);
7394 }
7395 else if (cu->language == language_cplus)
7396 {
7397 /* Assume that an artificial first parameter is
7398 "this", but do not crash if it is not. RealView
7399 marks unnamed (and thus unused) parameters as
7400 artificial; there is no way to differentiate
7401 the two cases. */
7402 if (TYPE_NFIELDS (type) > 0
7403 && TYPE_FIELD_ARTIFICIAL (type, 0)
7404 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7405 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7406 0))))
7407 fputs_unfiltered (" const", buf);
7408 }
7409 }
7410
7411 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7412 &length);
7413 ui_file_delete (buf);
7414
7415 if (cu->language == language_cplus)
7416 {
7417 char *cname
7418 = dwarf2_canonicalize_name (name, cu,
7419 &objfile->objfile_obstack);
7420
7421 if (cname != NULL)
7422 name = cname;
7423 }
7424 }
7425 }
7426
7427 return name;
7428 }
7429
7430 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7431 If scope qualifiers are appropriate they will be added. The result
7432 will be allocated on the objfile_obstack, or NULL if the DIE does
7433 not have a name. NAME may either be from a previous call to
7434 dwarf2_name or NULL.
7435
7436 The output string will be canonicalized (if C++/Java). */
7437
7438 static const char *
7439 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7440 {
7441 return dwarf2_compute_name (name, die, cu, 0);
7442 }
7443
7444 /* Construct a physname for the given DIE in CU. NAME may either be
7445 from a previous call to dwarf2_name or NULL. The result will be
7446 allocated on the objfile_objstack or NULL if the DIE does not have a
7447 name.
7448
7449 The output string will be canonicalized (if C++/Java). */
7450
7451 static const char *
7452 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7453 {
7454 struct objfile *objfile = cu->objfile;
7455 struct attribute *attr;
7456 const char *retval, *mangled = NULL, *canon = NULL;
7457 struct cleanup *back_to;
7458 int need_copy = 1;
7459
7460 /* In this case dwarf2_compute_name is just a shortcut not building anything
7461 on its own. */
7462 if (!die_needs_namespace (die, cu))
7463 return dwarf2_compute_name (name, die, cu, 1);
7464
7465 back_to = make_cleanup (null_cleanup, NULL);
7466
7467 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7468 if (!attr)
7469 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7470
7471 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7472 has computed. */
7473 if (attr && DW_STRING (attr))
7474 {
7475 char *demangled;
7476
7477 mangled = DW_STRING (attr);
7478
7479 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7480 type. It is easier for GDB users to search for such functions as
7481 `name(params)' than `long name(params)'. In such case the minimal
7482 symbol names do not match the full symbol names but for template
7483 functions there is never a need to look up their definition from their
7484 declaration so the only disadvantage remains the minimal symbol
7485 variant `long name(params)' does not have the proper inferior type.
7486 */
7487
7488 if (cu->language == language_go)
7489 {
7490 /* This is a lie, but we already lie to the caller new_symbol_full.
7491 new_symbol_full assumes we return the mangled name.
7492 This just undoes that lie until things are cleaned up. */
7493 demangled = NULL;
7494 }
7495 else
7496 {
7497 demangled = cplus_demangle (mangled,
7498 (DMGL_PARAMS | DMGL_ANSI
7499 | (cu->language == language_java
7500 ? DMGL_JAVA | DMGL_RET_POSTFIX
7501 : DMGL_RET_DROP)));
7502 }
7503 if (demangled)
7504 {
7505 make_cleanup (xfree, demangled);
7506 canon = demangled;
7507 }
7508 else
7509 {
7510 canon = mangled;
7511 need_copy = 0;
7512 }
7513 }
7514
7515 if (canon == NULL || check_physname)
7516 {
7517 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7518
7519 if (canon != NULL && strcmp (physname, canon) != 0)
7520 {
7521 /* It may not mean a bug in GDB. The compiler could also
7522 compute DW_AT_linkage_name incorrectly. But in such case
7523 GDB would need to be bug-to-bug compatible. */
7524
7525 complaint (&symfile_complaints,
7526 _("Computed physname <%s> does not match demangled <%s> "
7527 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7528 physname, canon, mangled, die->offset.sect_off, objfile->name);
7529
7530 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7531 is available here - over computed PHYSNAME. It is safer
7532 against both buggy GDB and buggy compilers. */
7533
7534 retval = canon;
7535 }
7536 else
7537 {
7538 retval = physname;
7539 need_copy = 0;
7540 }
7541 }
7542 else
7543 retval = canon;
7544
7545 if (need_copy)
7546 retval = obsavestring (retval, strlen (retval),
7547 &objfile->objfile_obstack);
7548
7549 do_cleanups (back_to);
7550 return retval;
7551 }
7552
7553 /* Read the import statement specified by the given die and record it. */
7554
7555 static void
7556 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7557 {
7558 struct objfile *objfile = cu->objfile;
7559 struct attribute *import_attr;
7560 struct die_info *imported_die, *child_die;
7561 struct dwarf2_cu *imported_cu;
7562 const char *imported_name;
7563 const char *imported_name_prefix;
7564 const char *canonical_name;
7565 const char *import_alias;
7566 const char *imported_declaration = NULL;
7567 const char *import_prefix;
7568 VEC (const_char_ptr) *excludes = NULL;
7569 struct cleanup *cleanups;
7570
7571 char *temp;
7572
7573 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7574 if (import_attr == NULL)
7575 {
7576 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7577 dwarf_tag_name (die->tag));
7578 return;
7579 }
7580
7581 imported_cu = cu;
7582 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7583 imported_name = dwarf2_name (imported_die, imported_cu);
7584 if (imported_name == NULL)
7585 {
7586 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7587
7588 The import in the following code:
7589 namespace A
7590 {
7591 typedef int B;
7592 }
7593
7594 int main ()
7595 {
7596 using A::B;
7597 B b;
7598 return b;
7599 }
7600
7601 ...
7602 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7603 <52> DW_AT_decl_file : 1
7604 <53> DW_AT_decl_line : 6
7605 <54> DW_AT_import : <0x75>
7606 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7607 <59> DW_AT_name : B
7608 <5b> DW_AT_decl_file : 1
7609 <5c> DW_AT_decl_line : 2
7610 <5d> DW_AT_type : <0x6e>
7611 ...
7612 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7613 <76> DW_AT_byte_size : 4
7614 <77> DW_AT_encoding : 5 (signed)
7615
7616 imports the wrong die ( 0x75 instead of 0x58 ).
7617 This case will be ignored until the gcc bug is fixed. */
7618 return;
7619 }
7620
7621 /* Figure out the local name after import. */
7622 import_alias = dwarf2_name (die, cu);
7623
7624 /* Figure out where the statement is being imported to. */
7625 import_prefix = determine_prefix (die, cu);
7626
7627 /* Figure out what the scope of the imported die is and prepend it
7628 to the name of the imported die. */
7629 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7630
7631 if (imported_die->tag != DW_TAG_namespace
7632 && imported_die->tag != DW_TAG_module)
7633 {
7634 imported_declaration = imported_name;
7635 canonical_name = imported_name_prefix;
7636 }
7637 else if (strlen (imported_name_prefix) > 0)
7638 {
7639 temp = alloca (strlen (imported_name_prefix)
7640 + 2 + strlen (imported_name) + 1);
7641 strcpy (temp, imported_name_prefix);
7642 strcat (temp, "::");
7643 strcat (temp, imported_name);
7644 canonical_name = temp;
7645 }
7646 else
7647 canonical_name = imported_name;
7648
7649 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7650
7651 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7652 for (child_die = die->child; child_die && child_die->tag;
7653 child_die = sibling_die (child_die))
7654 {
7655 /* DWARF-4: A Fortran use statement with a “rename list” may be
7656 represented by an imported module entry with an import attribute
7657 referring to the module and owned entries corresponding to those
7658 entities that are renamed as part of being imported. */
7659
7660 if (child_die->tag != DW_TAG_imported_declaration)
7661 {
7662 complaint (&symfile_complaints,
7663 _("child DW_TAG_imported_declaration expected "
7664 "- DIE at 0x%x [in module %s]"),
7665 child_die->offset.sect_off, objfile->name);
7666 continue;
7667 }
7668
7669 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7670 if (import_attr == NULL)
7671 {
7672 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7673 dwarf_tag_name (child_die->tag));
7674 continue;
7675 }
7676
7677 imported_cu = cu;
7678 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7679 &imported_cu);
7680 imported_name = dwarf2_name (imported_die, imported_cu);
7681 if (imported_name == NULL)
7682 {
7683 complaint (&symfile_complaints,
7684 _("child DW_TAG_imported_declaration has unknown "
7685 "imported name - DIE at 0x%x [in module %s]"),
7686 child_die->offset.sect_off, objfile->name);
7687 continue;
7688 }
7689
7690 VEC_safe_push (const_char_ptr, excludes, imported_name);
7691
7692 process_die (child_die, cu);
7693 }
7694
7695 cp_add_using_directive (import_prefix,
7696 canonical_name,
7697 import_alias,
7698 imported_declaration,
7699 excludes,
7700 &objfile->objfile_obstack);
7701
7702 do_cleanups (cleanups);
7703 }
7704
7705 /* Cleanup function for handle_DW_AT_stmt_list. */
7706
7707 static void
7708 free_cu_line_header (void *arg)
7709 {
7710 struct dwarf2_cu *cu = arg;
7711
7712 free_line_header (cu->line_header);
7713 cu->line_header = NULL;
7714 }
7715
7716 static void
7717 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7718 char **name, char **comp_dir)
7719 {
7720 struct attribute *attr;
7721
7722 *name = NULL;
7723 *comp_dir = NULL;
7724
7725 /* Find the filename. Do not use dwarf2_name here, since the filename
7726 is not a source language identifier. */
7727 attr = dwarf2_attr (die, DW_AT_name, cu);
7728 if (attr)
7729 {
7730 *name = DW_STRING (attr);
7731 }
7732
7733 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7734 if (attr)
7735 *comp_dir = DW_STRING (attr);
7736 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7737 {
7738 *comp_dir = ldirname (*name);
7739 if (*comp_dir != NULL)
7740 make_cleanup (xfree, *comp_dir);
7741 }
7742 if (*comp_dir != NULL)
7743 {
7744 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7745 directory, get rid of it. */
7746 char *cp = strchr (*comp_dir, ':');
7747
7748 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7749 *comp_dir = cp + 1;
7750 }
7751
7752 if (*name == NULL)
7753 *name = "<unknown>";
7754 }
7755
7756 /* Handle DW_AT_stmt_list for a compilation unit.
7757 DIE is the DW_TAG_compile_unit die for CU.
7758 COMP_DIR is the compilation directory.
7759 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7760
7761 static void
7762 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7763 const char *comp_dir)
7764 {
7765 struct attribute *attr;
7766
7767 gdb_assert (! cu->per_cu->is_debug_types);
7768
7769 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7770 if (attr)
7771 {
7772 unsigned int line_offset = DW_UNSND (attr);
7773 struct line_header *line_header
7774 = dwarf_decode_line_header (line_offset, cu);
7775
7776 if (line_header)
7777 {
7778 cu->line_header = line_header;
7779 make_cleanup (free_cu_line_header, cu);
7780 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7781 }
7782 }
7783 }
7784
7785 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7786
7787 static void
7788 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7789 {
7790 struct objfile *objfile = dwarf2_per_objfile->objfile;
7791 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7792 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7793 CORE_ADDR highpc = ((CORE_ADDR) 0);
7794 struct attribute *attr;
7795 char *name = NULL;
7796 char *comp_dir = NULL;
7797 struct die_info *child_die;
7798 bfd *abfd = objfile->obfd;
7799 CORE_ADDR baseaddr;
7800
7801 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7802
7803 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7804
7805 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7806 from finish_block. */
7807 if (lowpc == ((CORE_ADDR) -1))
7808 lowpc = highpc;
7809 lowpc += baseaddr;
7810 highpc += baseaddr;
7811
7812 find_file_and_directory (die, cu, &name, &comp_dir);
7813
7814 prepare_one_comp_unit (cu, die, cu->language);
7815
7816 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7817 standardised yet. As a workaround for the language detection we fall
7818 back to the DW_AT_producer string. */
7819 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7820 cu->language = language_opencl;
7821
7822 /* Similar hack for Go. */
7823 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7824 set_cu_language (DW_LANG_Go, cu);
7825
7826 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7827
7828 /* Decode line number information if present. We do this before
7829 processing child DIEs, so that the line header table is available
7830 for DW_AT_decl_file. */
7831 handle_DW_AT_stmt_list (die, cu, comp_dir);
7832
7833 /* Process all dies in compilation unit. */
7834 if (die->child != NULL)
7835 {
7836 child_die = die->child;
7837 while (child_die && child_die->tag)
7838 {
7839 process_die (child_die, cu);
7840 child_die = sibling_die (child_die);
7841 }
7842 }
7843
7844 /* Decode macro information, if present. Dwarf 2 macro information
7845 refers to information in the line number info statement program
7846 header, so we can only read it if we've read the header
7847 successfully. */
7848 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7849 if (attr && cu->line_header)
7850 {
7851 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7852 complaint (&symfile_complaints,
7853 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7854
7855 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7856 }
7857 else
7858 {
7859 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7860 if (attr && cu->line_header)
7861 {
7862 unsigned int macro_offset = DW_UNSND (attr);
7863
7864 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7865 }
7866 }
7867
7868 do_cleanups (back_to);
7869 }
7870
7871 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7872 Create the set of symtabs used by this TU, or if this TU is sharing
7873 symtabs with another TU and the symtabs have already been created
7874 then restore those symtabs in the line header.
7875 We don't need the pc/line-number mapping for type units. */
7876
7877 static void
7878 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7879 {
7880 struct objfile *objfile = dwarf2_per_objfile->objfile;
7881 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7882 struct type_unit_group *tu_group;
7883 int first_time;
7884 struct line_header *lh;
7885 struct attribute *attr;
7886 unsigned int i, line_offset;
7887
7888 gdb_assert (per_cu->is_debug_types);
7889
7890 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7891
7892 /* If we're using .gdb_index (includes -readnow) then
7893 per_cu->s.type_unit_group may not have been set up yet. */
7894 if (per_cu->s.type_unit_group == NULL)
7895 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7896 tu_group = per_cu->s.type_unit_group;
7897
7898 /* If we've already processed this stmt_list there's no real need to
7899 do it again, we could fake it and just recreate the part we need
7900 (file name,index -> symtab mapping). If data shows this optimization
7901 is useful we can do it then. */
7902 first_time = tu_group->primary_symtab == NULL;
7903
7904 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7905 debug info. */
7906 lh = NULL;
7907 if (attr != NULL)
7908 {
7909 line_offset = DW_UNSND (attr);
7910 lh = dwarf_decode_line_header (line_offset, cu);
7911 }
7912 if (lh == NULL)
7913 {
7914 if (first_time)
7915 dwarf2_start_symtab (cu, "", NULL, 0);
7916 else
7917 {
7918 gdb_assert (tu_group->symtabs == NULL);
7919 restart_symtab (0);
7920 }
7921 /* Note: The primary symtab will get allocated at the end. */
7922 return;
7923 }
7924
7925 cu->line_header = lh;
7926 make_cleanup (free_cu_line_header, cu);
7927
7928 if (first_time)
7929 {
7930 dwarf2_start_symtab (cu, "", NULL, 0);
7931
7932 tu_group->num_symtabs = lh->num_file_names;
7933 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7934
7935 for (i = 0; i < lh->num_file_names; ++i)
7936 {
7937 char *dir = NULL;
7938 struct file_entry *fe = &lh->file_names[i];
7939
7940 if (fe->dir_index)
7941 dir = lh->include_dirs[fe->dir_index - 1];
7942 dwarf2_start_subfile (fe->name, dir, NULL);
7943
7944 /* Note: We don't have to watch for the main subfile here, type units
7945 don't have DW_AT_name. */
7946
7947 if (current_subfile->symtab == NULL)
7948 {
7949 /* NOTE: start_subfile will recognize when it's been passed
7950 a file it has already seen. So we can't assume there's a
7951 simple mapping from lh->file_names to subfiles,
7952 lh->file_names may contain dups. */
7953 current_subfile->symtab = allocate_symtab (current_subfile->name,
7954 objfile);
7955 }
7956
7957 fe->symtab = current_subfile->symtab;
7958 tu_group->symtabs[i] = fe->symtab;
7959 }
7960 }
7961 else
7962 {
7963 restart_symtab (0);
7964
7965 for (i = 0; i < lh->num_file_names; ++i)
7966 {
7967 struct file_entry *fe = &lh->file_names[i];
7968
7969 fe->symtab = tu_group->symtabs[i];
7970 }
7971 }
7972
7973 /* The main symtab is allocated last. Type units don't have DW_AT_name
7974 so they don't have a "real" (so to speak) symtab anyway.
7975 There is later code that will assign the main symtab to all symbols
7976 that don't have one. We need to handle the case of a symbol with a
7977 missing symtab (DW_AT_decl_file) anyway. */
7978 }
7979
7980 /* Process DW_TAG_type_unit.
7981 For TUs we want to skip the first top level sibling if it's not the
7982 actual type being defined by this TU. In this case the first top
7983 level sibling is there to provide context only. */
7984
7985 static void
7986 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7987 {
7988 struct die_info *child_die;
7989
7990 prepare_one_comp_unit (cu, die, language_minimal);
7991
7992 /* Initialize (or reinitialize) the machinery for building symtabs.
7993 We do this before processing child DIEs, so that the line header table
7994 is available for DW_AT_decl_file. */
7995 setup_type_unit_groups (die, cu);
7996
7997 if (die->child != NULL)
7998 {
7999 child_die = die->child;
8000 while (child_die && child_die->tag)
8001 {
8002 process_die (child_die, cu);
8003 child_die = sibling_die (child_die);
8004 }
8005 }
8006 }
8007 \f
8008 /* DWO files. */
8009
8010 static hashval_t
8011 hash_dwo_file (const void *item)
8012 {
8013 const struct dwo_file *dwo_file = item;
8014
8015 return htab_hash_string (dwo_file->dwo_name);
8016 }
8017
8018 static int
8019 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8020 {
8021 const struct dwo_file *lhs = item_lhs;
8022 const struct dwo_file *rhs = item_rhs;
8023
8024 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8025 }
8026
8027 /* Allocate a hash table for DWO files. */
8028
8029 static htab_t
8030 allocate_dwo_file_hash_table (void)
8031 {
8032 struct objfile *objfile = dwarf2_per_objfile->objfile;
8033
8034 return htab_create_alloc_ex (41,
8035 hash_dwo_file,
8036 eq_dwo_file,
8037 NULL,
8038 &objfile->objfile_obstack,
8039 hashtab_obstack_allocate,
8040 dummy_obstack_deallocate);
8041 }
8042
8043 static hashval_t
8044 hash_dwo_unit (const void *item)
8045 {
8046 const struct dwo_unit *dwo_unit = item;
8047
8048 /* This drops the top 32 bits of the id, but is ok for a hash. */
8049 return dwo_unit->signature;
8050 }
8051
8052 static int
8053 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8054 {
8055 const struct dwo_unit *lhs = item_lhs;
8056 const struct dwo_unit *rhs = item_rhs;
8057
8058 /* The signature is assumed to be unique within the DWO file.
8059 So while object file CU dwo_id's always have the value zero,
8060 that's OK, assuming each object file DWO file has only one CU,
8061 and that's the rule for now. */
8062 return lhs->signature == rhs->signature;
8063 }
8064
8065 /* Allocate a hash table for DWO CUs,TUs.
8066 There is one of these tables for each of CUs,TUs for each DWO file. */
8067
8068 static htab_t
8069 allocate_dwo_unit_table (struct objfile *objfile)
8070 {
8071 /* Start out with a pretty small number.
8072 Generally DWO files contain only one CU and maybe some TUs. */
8073 return htab_create_alloc_ex (3,
8074 hash_dwo_unit,
8075 eq_dwo_unit,
8076 NULL,
8077 &objfile->objfile_obstack,
8078 hashtab_obstack_allocate,
8079 dummy_obstack_deallocate);
8080 }
8081
8082 /* This function is mapped across the sections and remembers the offset and
8083 size of each of the DWO debugging sections we are interested in. */
8084
8085 static void
8086 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8087 {
8088 struct dwo_file *dwo_file = dwo_file_ptr;
8089 const struct dwo_section_names *names = &dwo_section_names;
8090
8091 if (section_is_p (sectp->name, &names->abbrev_dwo))
8092 {
8093 dwo_file->sections.abbrev.asection = sectp;
8094 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8095 }
8096 else if (section_is_p (sectp->name, &names->info_dwo))
8097 {
8098 dwo_file->sections.info.asection = sectp;
8099 dwo_file->sections.info.size = bfd_get_section_size (sectp);
8100 }
8101 else if (section_is_p (sectp->name, &names->line_dwo))
8102 {
8103 dwo_file->sections.line.asection = sectp;
8104 dwo_file->sections.line.size = bfd_get_section_size (sectp);
8105 }
8106 else if (section_is_p (sectp->name, &names->loc_dwo))
8107 {
8108 dwo_file->sections.loc.asection = sectp;
8109 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8110 }
8111 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8112 {
8113 dwo_file->sections.macinfo.asection = sectp;
8114 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8115 }
8116 else if (section_is_p (sectp->name, &names->macro_dwo))
8117 {
8118 dwo_file->sections.macro.asection = sectp;
8119 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8120 }
8121 else if (section_is_p (sectp->name, &names->str_dwo))
8122 {
8123 dwo_file->sections.str.asection = sectp;
8124 dwo_file->sections.str.size = bfd_get_section_size (sectp);
8125 }
8126 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8127 {
8128 dwo_file->sections.str_offsets.asection = sectp;
8129 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8130 }
8131 else if (section_is_p (sectp->name, &names->types_dwo))
8132 {
8133 struct dwarf2_section_info type_section;
8134
8135 memset (&type_section, 0, sizeof (type_section));
8136 type_section.asection = sectp;
8137 type_section.size = bfd_get_section_size (sectp);
8138 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8139 &type_section);
8140 }
8141 }
8142
8143 /* Structure used to pass data to create_debug_info_hash_table_reader. */
8144
8145 struct create_dwo_info_table_data
8146 {
8147 struct dwo_file *dwo_file;
8148 htab_t cu_htab;
8149 };
8150
8151 /* die_reader_func for create_debug_info_hash_table. */
8152
8153 static void
8154 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8155 gdb_byte *info_ptr,
8156 struct die_info *comp_unit_die,
8157 int has_children,
8158 void *datap)
8159 {
8160 struct dwarf2_cu *cu = reader->cu;
8161 struct objfile *objfile = dwarf2_per_objfile->objfile;
8162 sect_offset offset = cu->per_cu->offset;
8163 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8164 struct create_dwo_info_table_data *data = datap;
8165 struct dwo_file *dwo_file = data->dwo_file;
8166 htab_t cu_htab = data->cu_htab;
8167 void **slot;
8168 struct attribute *attr;
8169 struct dwo_unit *dwo_unit;
8170
8171 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8172 if (attr == NULL)
8173 {
8174 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8175 " its dwo_id [in module %s]"),
8176 offset.sect_off, dwo_file->dwo_name);
8177 return;
8178 }
8179
8180 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8181 dwo_unit->dwo_file = dwo_file;
8182 dwo_unit->signature = DW_UNSND (attr);
8183 dwo_unit->info_or_types_section = section;
8184 dwo_unit->offset = offset;
8185 dwo_unit->length = cu->per_cu->length;
8186
8187 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8188 gdb_assert (slot != NULL);
8189 if (*slot != NULL)
8190 {
8191 const struct dwo_unit *dup_dwo_unit = *slot;
8192
8193 complaint (&symfile_complaints,
8194 _("debug entry at offset 0x%x is duplicate to the entry at"
8195 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8196 offset.sect_off, dup_dwo_unit->offset.sect_off,
8197 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8198 dwo_file->dwo_name);
8199 }
8200 else
8201 *slot = dwo_unit;
8202
8203 if (dwarf2_read_debug)
8204 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8205 offset.sect_off,
8206 phex (dwo_unit->signature,
8207 sizeof (dwo_unit->signature)));
8208 }
8209
8210 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
8211
8212 static htab_t
8213 create_debug_info_hash_table (struct dwo_file *dwo_file)
8214 {
8215 struct objfile *objfile = dwarf2_per_objfile->objfile;
8216 struct dwarf2_section_info *section = &dwo_file->sections.info;
8217 bfd *abfd;
8218 htab_t cu_htab;
8219 gdb_byte *info_ptr, *end_ptr;
8220 struct create_dwo_info_table_data create_dwo_info_table_data;
8221
8222 dwarf2_read_section (objfile, section);
8223 info_ptr = section->buffer;
8224
8225 if (info_ptr == NULL)
8226 return NULL;
8227
8228 /* We can't set abfd until now because the section may be empty or
8229 not present, in which case section->asection will be NULL. */
8230 abfd = section->asection->owner;
8231
8232 if (dwarf2_read_debug)
8233 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8234 bfd_get_filename (abfd));
8235
8236 cu_htab = allocate_dwo_unit_table (objfile);
8237
8238 create_dwo_info_table_data.dwo_file = dwo_file;
8239 create_dwo_info_table_data.cu_htab = cu_htab;
8240
8241 end_ptr = info_ptr + section->size;
8242 while (info_ptr < end_ptr)
8243 {
8244 struct dwarf2_per_cu_data per_cu;
8245
8246 memset (&per_cu, 0, sizeof (per_cu));
8247 per_cu.objfile = objfile;
8248 per_cu.is_debug_types = 0;
8249 per_cu.offset.sect_off = info_ptr - section->buffer;
8250 per_cu.info_or_types_section = section;
8251
8252 init_cutu_and_read_dies_no_follow (&per_cu,
8253 &dwo_file->sections.abbrev,
8254 dwo_file,
8255 create_debug_info_hash_table_reader,
8256 &create_dwo_info_table_data);
8257
8258 info_ptr += per_cu.length;
8259 }
8260
8261 return cu_htab;
8262 }
8263
8264 /* Subroutine of open_dwo_file to simplify it.
8265 Open the file specified by FILE_NAME and hand it off to BFD for
8266 preliminary analysis. Return a newly initialized bfd *, which
8267 includes a canonicalized copy of FILE_NAME.
8268 In case of trouble, return NULL.
8269 NOTE: This function is derived from symfile_bfd_open. */
8270
8271 static bfd *
8272 try_open_dwo_file (const char *file_name)
8273 {
8274 bfd *sym_bfd;
8275 int desc;
8276 char *absolute_name;
8277
8278 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8279 O_RDONLY | O_BINARY, &absolute_name);
8280 if (desc < 0)
8281 return NULL;
8282
8283 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8284 if (!sym_bfd)
8285 {
8286 xfree (absolute_name);
8287 return NULL;
8288 }
8289 xfree (absolute_name);
8290 bfd_set_cacheable (sym_bfd, 1);
8291
8292 if (!bfd_check_format (sym_bfd, bfd_object))
8293 {
8294 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8295 return NULL;
8296 }
8297
8298 return sym_bfd;
8299 }
8300
8301 /* Try to open DWO file DWO_NAME.
8302 COMP_DIR is the DW_AT_comp_dir attribute.
8303 The result is the bfd handle of the file.
8304 If there is a problem finding or opening the file, return NULL.
8305 Upon success, the canonicalized path of the file is stored in the bfd,
8306 same as symfile_bfd_open. */
8307
8308 static bfd *
8309 open_dwo_file (const char *dwo_name, const char *comp_dir)
8310 {
8311 bfd *abfd;
8312
8313 if (IS_ABSOLUTE_PATH (dwo_name))
8314 return try_open_dwo_file (dwo_name);
8315
8316 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8317
8318 if (comp_dir != NULL)
8319 {
8320 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8321
8322 /* NOTE: If comp_dir is a relative path, this will also try the
8323 search path, which seems useful. */
8324 abfd = try_open_dwo_file (path_to_try);
8325 xfree (path_to_try);
8326 if (abfd != NULL)
8327 return abfd;
8328 }
8329
8330 /* That didn't work, try debug-file-directory, which, despite its name,
8331 is a list of paths. */
8332
8333 if (*debug_file_directory == '\0')
8334 return NULL;
8335
8336 return try_open_dwo_file (dwo_name);
8337 }
8338
8339 /* Initialize the use of the DWO file specified by DWO_NAME. */
8340
8341 static struct dwo_file *
8342 init_dwo_file (const char *dwo_name, const char *comp_dir)
8343 {
8344 struct objfile *objfile = dwarf2_per_objfile->objfile;
8345 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8346 struct dwo_file);
8347 bfd *abfd;
8348 struct cleanup *cleanups;
8349
8350 if (dwarf2_read_debug)
8351 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8352
8353 abfd = open_dwo_file (dwo_name, comp_dir);
8354 if (abfd == NULL)
8355 return NULL;
8356 dwo_file->dwo_name = dwo_name;
8357 dwo_file->dwo_bfd = abfd;
8358
8359 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8360
8361 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8362
8363 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8364
8365 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8366 dwo_file->sections.types);
8367
8368 discard_cleanups (cleanups);
8369
8370 return dwo_file;
8371 }
8372
8373 /* Lookup DWO file DWO_NAME. */
8374
8375 static struct dwo_file *
8376 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8377 {
8378 struct dwo_file *dwo_file;
8379 struct dwo_file find_entry;
8380 void **slot;
8381
8382 if (dwarf2_per_objfile->dwo_files == NULL)
8383 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8384
8385 /* Have we already seen this DWO file? */
8386 find_entry.dwo_name = dwo_name;
8387 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8388
8389 /* If not, read it in and build a table of the DWOs it contains. */
8390 if (*slot == NULL)
8391 *slot = init_dwo_file (dwo_name, comp_dir);
8392
8393 /* NOTE: This will be NULL if unable to open the file. */
8394 dwo_file = *slot;
8395
8396 return dwo_file;
8397 }
8398
8399 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8400 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8401 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8402 nomenclature as TUs).
8403 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8404 (dwo_id mismatch or couldn't find the DWO file). */
8405
8406 static struct dwo_unit *
8407 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8408 char *dwo_name, const char *comp_dir,
8409 ULONGEST signature)
8410 {
8411 struct objfile *objfile = dwarf2_per_objfile->objfile;
8412 struct dwo_file *dwo_file;
8413
8414 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8415 if (dwo_file == NULL)
8416 return NULL;
8417
8418 /* Look up the DWO using its signature(dwo_id). */
8419
8420 if (dwo_file->cus != NULL)
8421 {
8422 struct dwo_unit find_dwo_cu, *dwo_cu;
8423
8424 find_dwo_cu.signature = signature;
8425 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8426
8427 if (dwo_cu != NULL)
8428 return dwo_cu;
8429 }
8430
8431 /* We didn't find it. This must mean a dwo_id mismatch. */
8432
8433 complaint (&symfile_complaints,
8434 _("Could not find DWO CU referenced by CU at offset 0x%x"
8435 " [in module %s]"),
8436 this_cu->offset.sect_off, objfile->name);
8437 return NULL;
8438 }
8439
8440 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8441 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8442 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8443 (dwo_id mismatch or couldn't find the DWO file). */
8444
8445 static struct dwo_unit *
8446 lookup_dwo_type_unit (struct signatured_type *this_tu,
8447 char *dwo_name, const char *comp_dir)
8448 {
8449 struct objfile *objfile = dwarf2_per_objfile->objfile;
8450 struct dwo_file *dwo_file;
8451
8452 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8453 if (dwo_file == NULL)
8454 return NULL;
8455
8456 /* Look up the DWO using its signature(dwo_id). */
8457
8458 if (dwo_file->tus != NULL)
8459 {
8460 struct dwo_unit find_dwo_tu, *dwo_tu;
8461
8462 find_dwo_tu.signature = this_tu->signature;
8463 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8464
8465 if (dwo_tu != NULL)
8466 return dwo_tu;
8467 }
8468
8469 /* We didn't find it. This must mean a dwo_id mismatch. */
8470
8471 complaint (&symfile_complaints,
8472 _("Could not find DWO TU referenced by TU at offset 0x%x"
8473 " [in module %s]"),
8474 this_tu->per_cu.offset.sect_off, objfile->name);
8475 return NULL;
8476 }
8477
8478 /* Free all resources associated with DWO_FILE.
8479 Close the DWO file and munmap the sections.
8480 All memory should be on the objfile obstack. */
8481
8482 static void
8483 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8484 {
8485 int ix;
8486 struct dwarf2_section_info *section;
8487
8488 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8489 gdb_bfd_unref (dwo_file->dwo_bfd);
8490
8491 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8492 }
8493
8494 /* Wrapper for free_dwo_file for use in cleanups. */
8495
8496 static void
8497 free_dwo_file_cleanup (void *arg)
8498 {
8499 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8500 struct objfile *objfile = dwarf2_per_objfile->objfile;
8501
8502 free_dwo_file (dwo_file, objfile);
8503 }
8504
8505 /* Traversal function for free_dwo_files. */
8506
8507 static int
8508 free_dwo_file_from_slot (void **slot, void *info)
8509 {
8510 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8511 struct objfile *objfile = (struct objfile *) info;
8512
8513 free_dwo_file (dwo_file, objfile);
8514
8515 return 1;
8516 }
8517
8518 /* Free all resources associated with DWO_FILES. */
8519
8520 static void
8521 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8522 {
8523 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8524 }
8525 \f
8526 /* Read in various DIEs. */
8527
8528 /* qsort helper for inherit_abstract_dies. */
8529
8530 static int
8531 unsigned_int_compar (const void *ap, const void *bp)
8532 {
8533 unsigned int a = *(unsigned int *) ap;
8534 unsigned int b = *(unsigned int *) bp;
8535
8536 return (a > b) - (b > a);
8537 }
8538
8539 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8540 Inherit only the children of the DW_AT_abstract_origin DIE not being
8541 already referenced by DW_AT_abstract_origin from the children of the
8542 current DIE. */
8543
8544 static void
8545 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8546 {
8547 struct die_info *child_die;
8548 unsigned die_children_count;
8549 /* CU offsets which were referenced by children of the current DIE. */
8550 sect_offset *offsets;
8551 sect_offset *offsets_end, *offsetp;
8552 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8553 struct die_info *origin_die;
8554 /* Iterator of the ORIGIN_DIE children. */
8555 struct die_info *origin_child_die;
8556 struct cleanup *cleanups;
8557 struct attribute *attr;
8558 struct dwarf2_cu *origin_cu;
8559 struct pending **origin_previous_list_in_scope;
8560
8561 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8562 if (!attr)
8563 return;
8564
8565 /* Note that following die references may follow to a die in a
8566 different cu. */
8567
8568 origin_cu = cu;
8569 origin_die = follow_die_ref (die, attr, &origin_cu);
8570
8571 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8572 symbols in. */
8573 origin_previous_list_in_scope = origin_cu->list_in_scope;
8574 origin_cu->list_in_scope = cu->list_in_scope;
8575
8576 if (die->tag != origin_die->tag
8577 && !(die->tag == DW_TAG_inlined_subroutine
8578 && origin_die->tag == DW_TAG_subprogram))
8579 complaint (&symfile_complaints,
8580 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8581 die->offset.sect_off, origin_die->offset.sect_off);
8582
8583 child_die = die->child;
8584 die_children_count = 0;
8585 while (child_die && child_die->tag)
8586 {
8587 child_die = sibling_die (child_die);
8588 die_children_count++;
8589 }
8590 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8591 cleanups = make_cleanup (xfree, offsets);
8592
8593 offsets_end = offsets;
8594 child_die = die->child;
8595 while (child_die && child_die->tag)
8596 {
8597 /* For each CHILD_DIE, find the corresponding child of
8598 ORIGIN_DIE. If there is more than one layer of
8599 DW_AT_abstract_origin, follow them all; there shouldn't be,
8600 but GCC versions at least through 4.4 generate this (GCC PR
8601 40573). */
8602 struct die_info *child_origin_die = child_die;
8603 struct dwarf2_cu *child_origin_cu = cu;
8604
8605 while (1)
8606 {
8607 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8608 child_origin_cu);
8609 if (attr == NULL)
8610 break;
8611 child_origin_die = follow_die_ref (child_origin_die, attr,
8612 &child_origin_cu);
8613 }
8614
8615 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8616 counterpart may exist. */
8617 if (child_origin_die != child_die)
8618 {
8619 if (child_die->tag != child_origin_die->tag
8620 && !(child_die->tag == DW_TAG_inlined_subroutine
8621 && child_origin_die->tag == DW_TAG_subprogram))
8622 complaint (&symfile_complaints,
8623 _("Child DIE 0x%x and its abstract origin 0x%x have "
8624 "different tags"), child_die->offset.sect_off,
8625 child_origin_die->offset.sect_off);
8626 if (child_origin_die->parent != origin_die)
8627 complaint (&symfile_complaints,
8628 _("Child DIE 0x%x and its abstract origin 0x%x have "
8629 "different parents"), child_die->offset.sect_off,
8630 child_origin_die->offset.sect_off);
8631 else
8632 *offsets_end++ = child_origin_die->offset;
8633 }
8634 child_die = sibling_die (child_die);
8635 }
8636 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8637 unsigned_int_compar);
8638 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8639 if (offsetp[-1].sect_off == offsetp->sect_off)
8640 complaint (&symfile_complaints,
8641 _("Multiple children of DIE 0x%x refer "
8642 "to DIE 0x%x as their abstract origin"),
8643 die->offset.sect_off, offsetp->sect_off);
8644
8645 offsetp = offsets;
8646 origin_child_die = origin_die->child;
8647 while (origin_child_die && origin_child_die->tag)
8648 {
8649 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
8650 while (offsetp < offsets_end
8651 && offsetp->sect_off < origin_child_die->offset.sect_off)
8652 offsetp++;
8653 if (offsetp >= offsets_end
8654 || offsetp->sect_off > origin_child_die->offset.sect_off)
8655 {
8656 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
8657 process_die (origin_child_die, origin_cu);
8658 }
8659 origin_child_die = sibling_die (origin_child_die);
8660 }
8661 origin_cu->list_in_scope = origin_previous_list_in_scope;
8662
8663 do_cleanups (cleanups);
8664 }
8665
8666 static void
8667 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8668 {
8669 struct objfile *objfile = cu->objfile;
8670 struct context_stack *new;
8671 CORE_ADDR lowpc;
8672 CORE_ADDR highpc;
8673 struct die_info *child_die;
8674 struct attribute *attr, *call_line, *call_file;
8675 char *name;
8676 CORE_ADDR baseaddr;
8677 struct block *block;
8678 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8679 VEC (symbolp) *template_args = NULL;
8680 struct template_symbol *templ_func = NULL;
8681
8682 if (inlined_func)
8683 {
8684 /* If we do not have call site information, we can't show the
8685 caller of this inlined function. That's too confusing, so
8686 only use the scope for local variables. */
8687 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8688 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8689 if (call_line == NULL || call_file == NULL)
8690 {
8691 read_lexical_block_scope (die, cu);
8692 return;
8693 }
8694 }
8695
8696 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8697
8698 name = dwarf2_name (die, cu);
8699
8700 /* Ignore functions with missing or empty names. These are actually
8701 illegal according to the DWARF standard. */
8702 if (name == NULL)
8703 {
8704 complaint (&symfile_complaints,
8705 _("missing name for subprogram DIE at %d"),
8706 die->offset.sect_off);
8707 return;
8708 }
8709
8710 /* Ignore functions with missing or invalid low and high pc attributes. */
8711 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8712 {
8713 attr = dwarf2_attr (die, DW_AT_external, cu);
8714 if (!attr || !DW_UNSND (attr))
8715 complaint (&symfile_complaints,
8716 _("cannot get low and high bounds "
8717 "for subprogram DIE at %d"),
8718 die->offset.sect_off);
8719 return;
8720 }
8721
8722 lowpc += baseaddr;
8723 highpc += baseaddr;
8724
8725 /* If we have any template arguments, then we must allocate a
8726 different sort of symbol. */
8727 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8728 {
8729 if (child_die->tag == DW_TAG_template_type_param
8730 || child_die->tag == DW_TAG_template_value_param)
8731 {
8732 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8733 struct template_symbol);
8734 templ_func->base.is_cplus_template_function = 1;
8735 break;
8736 }
8737 }
8738
8739 new = push_context (0, lowpc);
8740 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8741 (struct symbol *) templ_func);
8742
8743 /* If there is a location expression for DW_AT_frame_base, record
8744 it. */
8745 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8746 if (attr)
8747 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8748 expression is being recorded directly in the function's symbol
8749 and not in a separate frame-base object. I guess this hack is
8750 to avoid adding some sort of frame-base adjunct/annex to the
8751 function's symbol :-(. The problem with doing this is that it
8752 results in a function symbol with a location expression that
8753 has nothing to do with the location of the function, ouch! The
8754 relationship should be: a function's symbol has-a frame base; a
8755 frame-base has-a location expression. */
8756 dwarf2_symbol_mark_computed (attr, new->name, cu);
8757
8758 cu->list_in_scope = &local_symbols;
8759
8760 if (die->child != NULL)
8761 {
8762 child_die = die->child;
8763 while (child_die && child_die->tag)
8764 {
8765 if (child_die->tag == DW_TAG_template_type_param
8766 || child_die->tag == DW_TAG_template_value_param)
8767 {
8768 struct symbol *arg = new_symbol (child_die, NULL, cu);
8769
8770 if (arg != NULL)
8771 VEC_safe_push (symbolp, template_args, arg);
8772 }
8773 else
8774 process_die (child_die, cu);
8775 child_die = sibling_die (child_die);
8776 }
8777 }
8778
8779 inherit_abstract_dies (die, cu);
8780
8781 /* If we have a DW_AT_specification, we might need to import using
8782 directives from the context of the specification DIE. See the
8783 comment in determine_prefix. */
8784 if (cu->language == language_cplus
8785 && dwarf2_attr (die, DW_AT_specification, cu))
8786 {
8787 struct dwarf2_cu *spec_cu = cu;
8788 struct die_info *spec_die = die_specification (die, &spec_cu);
8789
8790 while (spec_die)
8791 {
8792 child_die = spec_die->child;
8793 while (child_die && child_die->tag)
8794 {
8795 if (child_die->tag == DW_TAG_imported_module)
8796 process_die (child_die, spec_cu);
8797 child_die = sibling_die (child_die);
8798 }
8799
8800 /* In some cases, GCC generates specification DIEs that
8801 themselves contain DW_AT_specification attributes. */
8802 spec_die = die_specification (spec_die, &spec_cu);
8803 }
8804 }
8805
8806 new = pop_context ();
8807 /* Make a block for the local symbols within. */
8808 block = finish_block (new->name, &local_symbols, new->old_blocks,
8809 lowpc, highpc, objfile);
8810
8811 /* For C++, set the block's scope. */
8812 if (cu->language == language_cplus || cu->language == language_fortran)
8813 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8814 determine_prefix (die, cu),
8815 processing_has_namespace_info);
8816
8817 /* If we have address ranges, record them. */
8818 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8819
8820 /* Attach template arguments to function. */
8821 if (! VEC_empty (symbolp, template_args))
8822 {
8823 gdb_assert (templ_func != NULL);
8824
8825 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8826 templ_func->template_arguments
8827 = obstack_alloc (&objfile->objfile_obstack,
8828 (templ_func->n_template_arguments
8829 * sizeof (struct symbol *)));
8830 memcpy (templ_func->template_arguments,
8831 VEC_address (symbolp, template_args),
8832 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8833 VEC_free (symbolp, template_args);
8834 }
8835
8836 /* In C++, we can have functions nested inside functions (e.g., when
8837 a function declares a class that has methods). This means that
8838 when we finish processing a function scope, we may need to go
8839 back to building a containing block's symbol lists. */
8840 local_symbols = new->locals;
8841 using_directives = new->using_directives;
8842
8843 /* If we've finished processing a top-level function, subsequent
8844 symbols go in the file symbol list. */
8845 if (outermost_context_p ())
8846 cu->list_in_scope = &file_symbols;
8847 }
8848
8849 /* Process all the DIES contained within a lexical block scope. Start
8850 a new scope, process the dies, and then close the scope. */
8851
8852 static void
8853 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8854 {
8855 struct objfile *objfile = cu->objfile;
8856 struct context_stack *new;
8857 CORE_ADDR lowpc, highpc;
8858 struct die_info *child_die;
8859 CORE_ADDR baseaddr;
8860
8861 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8862
8863 /* Ignore blocks with missing or invalid low and high pc attributes. */
8864 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8865 as multiple lexical blocks? Handling children in a sane way would
8866 be nasty. Might be easier to properly extend generic blocks to
8867 describe ranges. */
8868 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8869 return;
8870 lowpc += baseaddr;
8871 highpc += baseaddr;
8872
8873 push_context (0, lowpc);
8874 if (die->child != NULL)
8875 {
8876 child_die = die->child;
8877 while (child_die && child_die->tag)
8878 {
8879 process_die (child_die, cu);
8880 child_die = sibling_die (child_die);
8881 }
8882 }
8883 new = pop_context ();
8884
8885 if (local_symbols != NULL || using_directives != NULL)
8886 {
8887 struct block *block
8888 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8889 highpc, objfile);
8890
8891 /* Note that recording ranges after traversing children, as we
8892 do here, means that recording a parent's ranges entails
8893 walking across all its children's ranges as they appear in
8894 the address map, which is quadratic behavior.
8895
8896 It would be nicer to record the parent's ranges before
8897 traversing its children, simply overriding whatever you find
8898 there. But since we don't even decide whether to create a
8899 block until after we've traversed its children, that's hard
8900 to do. */
8901 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8902 }
8903 local_symbols = new->locals;
8904 using_directives = new->using_directives;
8905 }
8906
8907 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8908
8909 static void
8910 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8911 {
8912 struct objfile *objfile = cu->objfile;
8913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8914 CORE_ADDR pc, baseaddr;
8915 struct attribute *attr;
8916 struct call_site *call_site, call_site_local;
8917 void **slot;
8918 int nparams;
8919 struct die_info *child_die;
8920
8921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8922
8923 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8924 if (!attr)
8925 {
8926 complaint (&symfile_complaints,
8927 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8928 "DIE 0x%x [in module %s]"),
8929 die->offset.sect_off, objfile->name);
8930 return;
8931 }
8932 pc = DW_ADDR (attr) + baseaddr;
8933
8934 if (cu->call_site_htab == NULL)
8935 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8936 NULL, &objfile->objfile_obstack,
8937 hashtab_obstack_allocate, NULL);
8938 call_site_local.pc = pc;
8939 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8940 if (*slot != NULL)
8941 {
8942 complaint (&symfile_complaints,
8943 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8944 "DIE 0x%x [in module %s]"),
8945 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8946 return;
8947 }
8948
8949 /* Count parameters at the caller. */
8950
8951 nparams = 0;
8952 for (child_die = die->child; child_die && child_die->tag;
8953 child_die = sibling_die (child_die))
8954 {
8955 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8956 {
8957 complaint (&symfile_complaints,
8958 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8959 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8960 child_die->tag, child_die->offset.sect_off, objfile->name);
8961 continue;
8962 }
8963
8964 nparams++;
8965 }
8966
8967 call_site = obstack_alloc (&objfile->objfile_obstack,
8968 (sizeof (*call_site)
8969 + (sizeof (*call_site->parameter)
8970 * (nparams - 1))));
8971 *slot = call_site;
8972 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8973 call_site->pc = pc;
8974
8975 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8976 {
8977 struct die_info *func_die;
8978
8979 /* Skip also over DW_TAG_inlined_subroutine. */
8980 for (func_die = die->parent;
8981 func_die && func_die->tag != DW_TAG_subprogram
8982 && func_die->tag != DW_TAG_subroutine_type;
8983 func_die = func_die->parent);
8984
8985 /* DW_AT_GNU_all_call_sites is a superset
8986 of DW_AT_GNU_all_tail_call_sites. */
8987 if (func_die
8988 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8989 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8990 {
8991 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8992 not complete. But keep CALL_SITE for look ups via call_site_htab,
8993 both the initial caller containing the real return address PC and
8994 the final callee containing the current PC of a chain of tail
8995 calls do not need to have the tail call list complete. But any
8996 function candidate for a virtual tail call frame searched via
8997 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8998 determined unambiguously. */
8999 }
9000 else
9001 {
9002 struct type *func_type = NULL;
9003
9004 if (func_die)
9005 func_type = get_die_type (func_die, cu);
9006 if (func_type != NULL)
9007 {
9008 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9009
9010 /* Enlist this call site to the function. */
9011 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9012 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9013 }
9014 else
9015 complaint (&symfile_complaints,
9016 _("Cannot find function owning DW_TAG_GNU_call_site "
9017 "DIE 0x%x [in module %s]"),
9018 die->offset.sect_off, objfile->name);
9019 }
9020 }
9021
9022 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9023 if (attr == NULL)
9024 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9025 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9026 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9027 /* Keep NULL DWARF_BLOCK. */;
9028 else if (attr_form_is_block (attr))
9029 {
9030 struct dwarf2_locexpr_baton *dlbaton;
9031
9032 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9033 dlbaton->data = DW_BLOCK (attr)->data;
9034 dlbaton->size = DW_BLOCK (attr)->size;
9035 dlbaton->per_cu = cu->per_cu;
9036
9037 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9038 }
9039 else if (is_ref_attr (attr))
9040 {
9041 struct dwarf2_cu *target_cu = cu;
9042 struct die_info *target_die;
9043
9044 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9045 gdb_assert (target_cu->objfile == objfile);
9046 if (die_is_declaration (target_die, target_cu))
9047 {
9048 const char *target_physname;
9049
9050 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9051 if (target_physname == NULL)
9052 complaint (&symfile_complaints,
9053 _("DW_AT_GNU_call_site_target target DIE has invalid "
9054 "physname, for referencing DIE 0x%x [in module %s]"),
9055 die->offset.sect_off, objfile->name);
9056 else
9057 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9058 }
9059 else
9060 {
9061 CORE_ADDR lowpc;
9062
9063 /* DW_AT_entry_pc should be preferred. */
9064 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9065 complaint (&symfile_complaints,
9066 _("DW_AT_GNU_call_site_target target DIE has invalid "
9067 "low pc, for referencing DIE 0x%x [in module %s]"),
9068 die->offset.sect_off, objfile->name);
9069 else
9070 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9071 }
9072 }
9073 else
9074 complaint (&symfile_complaints,
9075 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9076 "block nor reference, for DIE 0x%x [in module %s]"),
9077 die->offset.sect_off, objfile->name);
9078
9079 call_site->per_cu = cu->per_cu;
9080
9081 for (child_die = die->child;
9082 child_die && child_die->tag;
9083 child_die = sibling_die (child_die))
9084 {
9085 struct call_site_parameter *parameter;
9086 struct attribute *loc, *origin;
9087
9088 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9089 {
9090 /* Already printed the complaint above. */
9091 continue;
9092 }
9093
9094 gdb_assert (call_site->parameter_count < nparams);
9095 parameter = &call_site->parameter[call_site->parameter_count];
9096
9097 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9098 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9099 register is contained in DW_AT_GNU_call_site_value. */
9100
9101 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9102 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9103 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9104 {
9105 sect_offset offset;
9106
9107 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9108 offset = dwarf2_get_ref_die_offset (origin);
9109 if (!offset_in_cu_p (&cu->header, offset))
9110 {
9111 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9112 binding can be done only inside one CU. Such referenced DIE
9113 therefore cannot be even moved to DW_TAG_partial_unit. */
9114 complaint (&symfile_complaints,
9115 _("DW_AT_abstract_origin offset is not in CU for "
9116 "DW_TAG_GNU_call_site child DIE 0x%x "
9117 "[in module %s]"),
9118 child_die->offset.sect_off, objfile->name);
9119 continue;
9120 }
9121 parameter->u.param_offset.cu_off = (offset.sect_off
9122 - cu->header.offset.sect_off);
9123 }
9124 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9125 {
9126 complaint (&symfile_complaints,
9127 _("No DW_FORM_block* DW_AT_location for "
9128 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9129 child_die->offset.sect_off, objfile->name);
9130 continue;
9131 }
9132 else
9133 {
9134 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9135 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9136 if (parameter->u.dwarf_reg != -1)
9137 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9138 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9139 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9140 &parameter->u.fb_offset))
9141 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9142 else
9143 {
9144 complaint (&symfile_complaints,
9145 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9146 "for DW_FORM_block* DW_AT_location is supported for "
9147 "DW_TAG_GNU_call_site child DIE 0x%x "
9148 "[in module %s]"),
9149 child_die->offset.sect_off, objfile->name);
9150 continue;
9151 }
9152 }
9153
9154 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9155 if (!attr_form_is_block (attr))
9156 {
9157 complaint (&symfile_complaints,
9158 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9159 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9160 child_die->offset.sect_off, objfile->name);
9161 continue;
9162 }
9163 parameter->value = DW_BLOCK (attr)->data;
9164 parameter->value_size = DW_BLOCK (attr)->size;
9165
9166 /* Parameters are not pre-cleared by memset above. */
9167 parameter->data_value = NULL;
9168 parameter->data_value_size = 0;
9169 call_site->parameter_count++;
9170
9171 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9172 if (attr)
9173 {
9174 if (!attr_form_is_block (attr))
9175 complaint (&symfile_complaints,
9176 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9177 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9178 child_die->offset.sect_off, objfile->name);
9179 else
9180 {
9181 parameter->data_value = DW_BLOCK (attr)->data;
9182 parameter->data_value_size = DW_BLOCK (attr)->size;
9183 }
9184 }
9185 }
9186 }
9187
9188 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9189 Return 1 if the attributes are present and valid, otherwise, return 0.
9190 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9191
9192 static int
9193 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9194 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9195 struct partial_symtab *ranges_pst)
9196 {
9197 struct objfile *objfile = cu->objfile;
9198 struct comp_unit_head *cu_header = &cu->header;
9199 bfd *obfd = objfile->obfd;
9200 unsigned int addr_size = cu_header->addr_size;
9201 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9202 /* Base address selection entry. */
9203 CORE_ADDR base;
9204 int found_base;
9205 unsigned int dummy;
9206 gdb_byte *buffer;
9207 CORE_ADDR marker;
9208 int low_set;
9209 CORE_ADDR low = 0;
9210 CORE_ADDR high = 0;
9211 CORE_ADDR baseaddr;
9212
9213 found_base = cu->base_known;
9214 base = cu->base_address;
9215
9216 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9217 if (offset >= dwarf2_per_objfile->ranges.size)
9218 {
9219 complaint (&symfile_complaints,
9220 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9221 offset);
9222 return 0;
9223 }
9224 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9225
9226 /* Read in the largest possible address. */
9227 marker = read_address (obfd, buffer, cu, &dummy);
9228 if ((marker & mask) == mask)
9229 {
9230 /* If we found the largest possible address, then
9231 read the base address. */
9232 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9233 buffer += 2 * addr_size;
9234 offset += 2 * addr_size;
9235 found_base = 1;
9236 }
9237
9238 low_set = 0;
9239
9240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9241
9242 while (1)
9243 {
9244 CORE_ADDR range_beginning, range_end;
9245
9246 range_beginning = read_address (obfd, buffer, cu, &dummy);
9247 buffer += addr_size;
9248 range_end = read_address (obfd, buffer, cu, &dummy);
9249 buffer += addr_size;
9250 offset += 2 * addr_size;
9251
9252 /* An end of list marker is a pair of zero addresses. */
9253 if (range_beginning == 0 && range_end == 0)
9254 /* Found the end of list entry. */
9255 break;
9256
9257 /* Each base address selection entry is a pair of 2 values.
9258 The first is the largest possible address, the second is
9259 the base address. Check for a base address here. */
9260 if ((range_beginning & mask) == mask)
9261 {
9262 /* If we found the largest possible address, then
9263 read the base address. */
9264 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9265 found_base = 1;
9266 continue;
9267 }
9268
9269 if (!found_base)
9270 {
9271 /* We have no valid base address for the ranges
9272 data. */
9273 complaint (&symfile_complaints,
9274 _("Invalid .debug_ranges data (no base address)"));
9275 return 0;
9276 }
9277
9278 if (range_beginning > range_end)
9279 {
9280 /* Inverted range entries are invalid. */
9281 complaint (&symfile_complaints,
9282 _("Invalid .debug_ranges data (inverted range)"));
9283 return 0;
9284 }
9285
9286 /* Empty range entries have no effect. */
9287 if (range_beginning == range_end)
9288 continue;
9289
9290 range_beginning += base;
9291 range_end += base;
9292
9293 /* A not-uncommon case of bad debug info.
9294 Don't pollute the addrmap with bad data. */
9295 if (range_beginning + baseaddr == 0
9296 && !dwarf2_per_objfile->has_section_at_zero)
9297 {
9298 complaint (&symfile_complaints,
9299 _(".debug_ranges entry has start address of zero"
9300 " [in module %s]"), objfile->name);
9301 continue;
9302 }
9303
9304 if (ranges_pst != NULL)
9305 addrmap_set_empty (objfile->psymtabs_addrmap,
9306 range_beginning + baseaddr,
9307 range_end - 1 + baseaddr,
9308 ranges_pst);
9309
9310 /* FIXME: This is recording everything as a low-high
9311 segment of consecutive addresses. We should have a
9312 data structure for discontiguous block ranges
9313 instead. */
9314 if (! low_set)
9315 {
9316 low = range_beginning;
9317 high = range_end;
9318 low_set = 1;
9319 }
9320 else
9321 {
9322 if (range_beginning < low)
9323 low = range_beginning;
9324 if (range_end > high)
9325 high = range_end;
9326 }
9327 }
9328
9329 if (! low_set)
9330 /* If the first entry is an end-of-list marker, the range
9331 describes an empty scope, i.e. no instructions. */
9332 return 0;
9333
9334 if (low_return)
9335 *low_return = low;
9336 if (high_return)
9337 *high_return = high;
9338 return 1;
9339 }
9340
9341 /* Get low and high pc attributes from a die. Return 1 if the attributes
9342 are present and valid, otherwise, return 0. Return -1 if the range is
9343 discontinuous, i.e. derived from DW_AT_ranges information. */
9344
9345 static int
9346 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9347 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9348 struct partial_symtab *pst)
9349 {
9350 struct attribute *attr;
9351 struct attribute *attr_high;
9352 CORE_ADDR low = 0;
9353 CORE_ADDR high = 0;
9354 int ret = 0;
9355
9356 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9357 if (attr_high)
9358 {
9359 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9360 if (attr)
9361 {
9362 low = DW_ADDR (attr);
9363 if (attr_high->form == DW_FORM_addr
9364 || attr_high->form == DW_FORM_GNU_addr_index)
9365 high = DW_ADDR (attr_high);
9366 else
9367 high = low + DW_UNSND (attr_high);
9368 }
9369 else
9370 /* Found high w/o low attribute. */
9371 return 0;
9372
9373 /* Found consecutive range of addresses. */
9374 ret = 1;
9375 }
9376 else
9377 {
9378 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9379 if (attr != NULL)
9380 {
9381 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9382
9383 /* Value of the DW_AT_ranges attribute is the offset in the
9384 .debug_ranges section. */
9385 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9386 return 0;
9387 /* Found discontinuous range of addresses. */
9388 ret = -1;
9389 }
9390 }
9391
9392 /* read_partial_die has also the strict LOW < HIGH requirement. */
9393 if (high <= low)
9394 return 0;
9395
9396 /* When using the GNU linker, .gnu.linkonce. sections are used to
9397 eliminate duplicate copies of functions and vtables and such.
9398 The linker will arbitrarily choose one and discard the others.
9399 The AT_*_pc values for such functions refer to local labels in
9400 these sections. If the section from that file was discarded, the
9401 labels are not in the output, so the relocs get a value of 0.
9402 If this is a discarded function, mark the pc bounds as invalid,
9403 so that GDB will ignore it. */
9404 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9405 return 0;
9406
9407 *lowpc = low;
9408 if (highpc)
9409 *highpc = high;
9410 return ret;
9411 }
9412
9413 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9414 its low and high PC addresses. Do nothing if these addresses could not
9415 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9416 and HIGHPC to the high address if greater than HIGHPC. */
9417
9418 static void
9419 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9420 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9421 struct dwarf2_cu *cu)
9422 {
9423 CORE_ADDR low, high;
9424 struct die_info *child = die->child;
9425
9426 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9427 {
9428 *lowpc = min (*lowpc, low);
9429 *highpc = max (*highpc, high);
9430 }
9431
9432 /* If the language does not allow nested subprograms (either inside
9433 subprograms or lexical blocks), we're done. */
9434 if (cu->language != language_ada)
9435 return;
9436
9437 /* Check all the children of the given DIE. If it contains nested
9438 subprograms, then check their pc bounds. Likewise, we need to
9439 check lexical blocks as well, as they may also contain subprogram
9440 definitions. */
9441 while (child && child->tag)
9442 {
9443 if (child->tag == DW_TAG_subprogram
9444 || child->tag == DW_TAG_lexical_block)
9445 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9446 child = sibling_die (child);
9447 }
9448 }
9449
9450 /* Get the low and high pc's represented by the scope DIE, and store
9451 them in *LOWPC and *HIGHPC. If the correct values can't be
9452 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9453
9454 static void
9455 get_scope_pc_bounds (struct die_info *die,
9456 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9457 struct dwarf2_cu *cu)
9458 {
9459 CORE_ADDR best_low = (CORE_ADDR) -1;
9460 CORE_ADDR best_high = (CORE_ADDR) 0;
9461 CORE_ADDR current_low, current_high;
9462
9463 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9464 {
9465 best_low = current_low;
9466 best_high = current_high;
9467 }
9468 else
9469 {
9470 struct die_info *child = die->child;
9471
9472 while (child && child->tag)
9473 {
9474 switch (child->tag) {
9475 case DW_TAG_subprogram:
9476 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9477 break;
9478 case DW_TAG_namespace:
9479 case DW_TAG_module:
9480 /* FIXME: carlton/2004-01-16: Should we do this for
9481 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9482 that current GCC's always emit the DIEs corresponding
9483 to definitions of methods of classes as children of a
9484 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9485 the DIEs giving the declarations, which could be
9486 anywhere). But I don't see any reason why the
9487 standards says that they have to be there. */
9488 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9489
9490 if (current_low != ((CORE_ADDR) -1))
9491 {
9492 best_low = min (best_low, current_low);
9493 best_high = max (best_high, current_high);
9494 }
9495 break;
9496 default:
9497 /* Ignore. */
9498 break;
9499 }
9500
9501 child = sibling_die (child);
9502 }
9503 }
9504
9505 *lowpc = best_low;
9506 *highpc = best_high;
9507 }
9508
9509 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9510 in DIE. */
9511
9512 static void
9513 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9514 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9515 {
9516 struct objfile *objfile = cu->objfile;
9517 struct attribute *attr;
9518 struct attribute *attr_high;
9519
9520 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9521 if (attr_high)
9522 {
9523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9524 if (attr)
9525 {
9526 CORE_ADDR low = DW_ADDR (attr);
9527 CORE_ADDR high;
9528 if (attr_high->form == DW_FORM_addr
9529 || attr_high->form == DW_FORM_GNU_addr_index)
9530 high = DW_ADDR (attr_high);
9531 else
9532 high = low + DW_UNSND (attr_high);
9533
9534 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9535 }
9536 }
9537
9538 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9539 if (attr)
9540 {
9541 bfd *obfd = objfile->obfd;
9542
9543 /* The value of the DW_AT_ranges attribute is the offset of the
9544 address range list in the .debug_ranges section. */
9545 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9546 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9547
9548 /* For some target architectures, but not others, the
9549 read_address function sign-extends the addresses it returns.
9550 To recognize base address selection entries, we need a
9551 mask. */
9552 unsigned int addr_size = cu->header.addr_size;
9553 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9554
9555 /* The base address, to which the next pair is relative. Note
9556 that this 'base' is a DWARF concept: most entries in a range
9557 list are relative, to reduce the number of relocs against the
9558 debugging information. This is separate from this function's
9559 'baseaddr' argument, which GDB uses to relocate debugging
9560 information from a shared library based on the address at
9561 which the library was loaded. */
9562 CORE_ADDR base = cu->base_address;
9563 int base_known = cu->base_known;
9564
9565 gdb_assert (dwarf2_per_objfile->ranges.readin);
9566 if (offset >= dwarf2_per_objfile->ranges.size)
9567 {
9568 complaint (&symfile_complaints,
9569 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9570 offset);
9571 return;
9572 }
9573
9574 for (;;)
9575 {
9576 unsigned int bytes_read;
9577 CORE_ADDR start, end;
9578
9579 start = read_address (obfd, buffer, cu, &bytes_read);
9580 buffer += bytes_read;
9581 end = read_address (obfd, buffer, cu, &bytes_read);
9582 buffer += bytes_read;
9583
9584 /* Did we find the end of the range list? */
9585 if (start == 0 && end == 0)
9586 break;
9587
9588 /* Did we find a base address selection entry? */
9589 else if ((start & base_select_mask) == base_select_mask)
9590 {
9591 base = end;
9592 base_known = 1;
9593 }
9594
9595 /* We found an ordinary address range. */
9596 else
9597 {
9598 if (!base_known)
9599 {
9600 complaint (&symfile_complaints,
9601 _("Invalid .debug_ranges data "
9602 "(no base address)"));
9603 return;
9604 }
9605
9606 if (start > end)
9607 {
9608 /* Inverted range entries are invalid. */
9609 complaint (&symfile_complaints,
9610 _("Invalid .debug_ranges data "
9611 "(inverted range)"));
9612 return;
9613 }
9614
9615 /* Empty range entries have no effect. */
9616 if (start == end)
9617 continue;
9618
9619 start += base + baseaddr;
9620 end += base + baseaddr;
9621
9622 /* A not-uncommon case of bad debug info.
9623 Don't pollute the addrmap with bad data. */
9624 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9625 {
9626 complaint (&symfile_complaints,
9627 _(".debug_ranges entry has start address of zero"
9628 " [in module %s]"), objfile->name);
9629 continue;
9630 }
9631
9632 record_block_range (block, start, end - 1);
9633 }
9634 }
9635 }
9636 }
9637
9638 /* Check whether the producer field indicates either of GCC < 4.6, or the
9639 Intel C/C++ compiler, and cache the result in CU. */
9640
9641 static void
9642 check_producer (struct dwarf2_cu *cu)
9643 {
9644 const char *cs;
9645 int major, minor, release;
9646
9647 if (cu->producer == NULL)
9648 {
9649 /* For unknown compilers expect their behavior is DWARF version
9650 compliant.
9651
9652 GCC started to support .debug_types sections by -gdwarf-4 since
9653 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9654 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9655 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9656 interpreted incorrectly by GDB now - GCC PR debug/48229. */
9657 }
9658 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9659 {
9660 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9661
9662 cs = &cu->producer[strlen ("GNU ")];
9663 while (*cs && !isdigit (*cs))
9664 cs++;
9665 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9666 {
9667 /* Not recognized as GCC. */
9668 }
9669 else
9670 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9671 }
9672 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9673 cu->producer_is_icc = 1;
9674 else
9675 {
9676 /* For other non-GCC compilers, expect their behavior is DWARF version
9677 compliant. */
9678 }
9679
9680 cu->checked_producer = 1;
9681 }
9682
9683 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9684 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9685 during 4.6.0 experimental. */
9686
9687 static int
9688 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9689 {
9690 if (!cu->checked_producer)
9691 check_producer (cu);
9692
9693 return cu->producer_is_gxx_lt_4_6;
9694 }
9695
9696 /* Return the default accessibility type if it is not overriden by
9697 DW_AT_accessibility. */
9698
9699 static enum dwarf_access_attribute
9700 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9701 {
9702 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9703 {
9704 /* The default DWARF 2 accessibility for members is public, the default
9705 accessibility for inheritance is private. */
9706
9707 if (die->tag != DW_TAG_inheritance)
9708 return DW_ACCESS_public;
9709 else
9710 return DW_ACCESS_private;
9711 }
9712 else
9713 {
9714 /* DWARF 3+ defines the default accessibility a different way. The same
9715 rules apply now for DW_TAG_inheritance as for the members and it only
9716 depends on the container kind. */
9717
9718 if (die->parent->tag == DW_TAG_class_type)
9719 return DW_ACCESS_private;
9720 else
9721 return DW_ACCESS_public;
9722 }
9723 }
9724
9725 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9726 offset. If the attribute was not found return 0, otherwise return
9727 1. If it was found but could not properly be handled, set *OFFSET
9728 to 0. */
9729
9730 static int
9731 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9732 LONGEST *offset)
9733 {
9734 struct attribute *attr;
9735
9736 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9737 if (attr != NULL)
9738 {
9739 *offset = 0;
9740
9741 /* Note that we do not check for a section offset first here.
9742 This is because DW_AT_data_member_location is new in DWARF 4,
9743 so if we see it, we can assume that a constant form is really
9744 a constant and not a section offset. */
9745 if (attr_form_is_constant (attr))
9746 *offset = dwarf2_get_attr_constant_value (attr, 0);
9747 else if (attr_form_is_section_offset (attr))
9748 dwarf2_complex_location_expr_complaint ();
9749 else if (attr_form_is_block (attr))
9750 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9751 else
9752 dwarf2_complex_location_expr_complaint ();
9753
9754 return 1;
9755 }
9756
9757 return 0;
9758 }
9759
9760 /* Add an aggregate field to the field list. */
9761
9762 static void
9763 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9764 struct dwarf2_cu *cu)
9765 {
9766 struct objfile *objfile = cu->objfile;
9767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9768 struct nextfield *new_field;
9769 struct attribute *attr;
9770 struct field *fp;
9771 char *fieldname = "";
9772
9773 /* Allocate a new field list entry and link it in. */
9774 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9775 make_cleanup (xfree, new_field);
9776 memset (new_field, 0, sizeof (struct nextfield));
9777
9778 if (die->tag == DW_TAG_inheritance)
9779 {
9780 new_field->next = fip->baseclasses;
9781 fip->baseclasses = new_field;
9782 }
9783 else
9784 {
9785 new_field->next = fip->fields;
9786 fip->fields = new_field;
9787 }
9788 fip->nfields++;
9789
9790 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9791 if (attr)
9792 new_field->accessibility = DW_UNSND (attr);
9793 else
9794 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9795 if (new_field->accessibility != DW_ACCESS_public)
9796 fip->non_public_fields = 1;
9797
9798 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9799 if (attr)
9800 new_field->virtuality = DW_UNSND (attr);
9801 else
9802 new_field->virtuality = DW_VIRTUALITY_none;
9803
9804 fp = &new_field->field;
9805
9806 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9807 {
9808 LONGEST offset;
9809
9810 /* Data member other than a C++ static data member. */
9811
9812 /* Get type of field. */
9813 fp->type = die_type (die, cu);
9814
9815 SET_FIELD_BITPOS (*fp, 0);
9816
9817 /* Get bit size of field (zero if none). */
9818 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9819 if (attr)
9820 {
9821 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9822 }
9823 else
9824 {
9825 FIELD_BITSIZE (*fp) = 0;
9826 }
9827
9828 /* Get bit offset of field. */
9829 if (handle_data_member_location (die, cu, &offset))
9830 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9831 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9832 if (attr)
9833 {
9834 if (gdbarch_bits_big_endian (gdbarch))
9835 {
9836 /* For big endian bits, the DW_AT_bit_offset gives the
9837 additional bit offset from the MSB of the containing
9838 anonymous object to the MSB of the field. We don't
9839 have to do anything special since we don't need to
9840 know the size of the anonymous object. */
9841 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9842 }
9843 else
9844 {
9845 /* For little endian bits, compute the bit offset to the
9846 MSB of the anonymous object, subtract off the number of
9847 bits from the MSB of the field to the MSB of the
9848 object, and then subtract off the number of bits of
9849 the field itself. The result is the bit offset of
9850 the LSB of the field. */
9851 int anonymous_size;
9852 int bit_offset = DW_UNSND (attr);
9853
9854 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9855 if (attr)
9856 {
9857 /* The size of the anonymous object containing
9858 the bit field is explicit, so use the
9859 indicated size (in bytes). */
9860 anonymous_size = DW_UNSND (attr);
9861 }
9862 else
9863 {
9864 /* The size of the anonymous object containing
9865 the bit field must be inferred from the type
9866 attribute of the data member containing the
9867 bit field. */
9868 anonymous_size = TYPE_LENGTH (fp->type);
9869 }
9870 SET_FIELD_BITPOS (*fp,
9871 (FIELD_BITPOS (*fp)
9872 + anonymous_size * bits_per_byte
9873 - bit_offset - FIELD_BITSIZE (*fp)));
9874 }
9875 }
9876
9877 /* Get name of field. */
9878 fieldname = dwarf2_name (die, cu);
9879 if (fieldname == NULL)
9880 fieldname = "";
9881
9882 /* The name is already allocated along with this objfile, so we don't
9883 need to duplicate it for the type. */
9884 fp->name = fieldname;
9885
9886 /* Change accessibility for artificial fields (e.g. virtual table
9887 pointer or virtual base class pointer) to private. */
9888 if (dwarf2_attr (die, DW_AT_artificial, cu))
9889 {
9890 FIELD_ARTIFICIAL (*fp) = 1;
9891 new_field->accessibility = DW_ACCESS_private;
9892 fip->non_public_fields = 1;
9893 }
9894 }
9895 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9896 {
9897 /* C++ static member. */
9898
9899 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9900 is a declaration, but all versions of G++ as of this writing
9901 (so through at least 3.2.1) incorrectly generate
9902 DW_TAG_variable tags. */
9903
9904 const char *physname;
9905
9906 /* Get name of field. */
9907 fieldname = dwarf2_name (die, cu);
9908 if (fieldname == NULL)
9909 return;
9910
9911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9912 if (attr
9913 /* Only create a symbol if this is an external value.
9914 new_symbol checks this and puts the value in the global symbol
9915 table, which we want. If it is not external, new_symbol
9916 will try to put the value in cu->list_in_scope which is wrong. */
9917 && dwarf2_flag_true_p (die, DW_AT_external, cu))
9918 {
9919 /* A static const member, not much different than an enum as far as
9920 we're concerned, except that we can support more types. */
9921 new_symbol (die, NULL, cu);
9922 }
9923
9924 /* Get physical name. */
9925 physname = dwarf2_physname (fieldname, die, cu);
9926
9927 /* The name is already allocated along with this objfile, so we don't
9928 need to duplicate it for the type. */
9929 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9930 FIELD_TYPE (*fp) = die_type (die, cu);
9931 FIELD_NAME (*fp) = fieldname;
9932 }
9933 else if (die->tag == DW_TAG_inheritance)
9934 {
9935 LONGEST offset;
9936
9937 /* C++ base class field. */
9938 if (handle_data_member_location (die, cu, &offset))
9939 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9940 FIELD_BITSIZE (*fp) = 0;
9941 FIELD_TYPE (*fp) = die_type (die, cu);
9942 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9943 fip->nbaseclasses++;
9944 }
9945 }
9946
9947 /* Add a typedef defined in the scope of the FIP's class. */
9948
9949 static void
9950 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9951 struct dwarf2_cu *cu)
9952 {
9953 struct objfile *objfile = cu->objfile;
9954 struct typedef_field_list *new_field;
9955 struct attribute *attr;
9956 struct typedef_field *fp;
9957 char *fieldname = "";
9958
9959 /* Allocate a new field list entry and link it in. */
9960 new_field = xzalloc (sizeof (*new_field));
9961 make_cleanup (xfree, new_field);
9962
9963 gdb_assert (die->tag == DW_TAG_typedef);
9964
9965 fp = &new_field->field;
9966
9967 /* Get name of field. */
9968 fp->name = dwarf2_name (die, cu);
9969 if (fp->name == NULL)
9970 return;
9971
9972 fp->type = read_type_die (die, cu);
9973
9974 new_field->next = fip->typedef_field_list;
9975 fip->typedef_field_list = new_field;
9976 fip->typedef_field_list_count++;
9977 }
9978
9979 /* Create the vector of fields, and attach it to the type. */
9980
9981 static void
9982 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9983 struct dwarf2_cu *cu)
9984 {
9985 int nfields = fip->nfields;
9986
9987 /* Record the field count, allocate space for the array of fields,
9988 and create blank accessibility bitfields if necessary. */
9989 TYPE_NFIELDS (type) = nfields;
9990 TYPE_FIELDS (type) = (struct field *)
9991 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9992 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9993
9994 if (fip->non_public_fields && cu->language != language_ada)
9995 {
9996 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9997
9998 TYPE_FIELD_PRIVATE_BITS (type) =
9999 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10000 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10001
10002 TYPE_FIELD_PROTECTED_BITS (type) =
10003 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10004 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10005
10006 TYPE_FIELD_IGNORE_BITS (type) =
10007 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10008 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10009 }
10010
10011 /* If the type has baseclasses, allocate and clear a bit vector for
10012 TYPE_FIELD_VIRTUAL_BITS. */
10013 if (fip->nbaseclasses && cu->language != language_ada)
10014 {
10015 int num_bytes = B_BYTES (fip->nbaseclasses);
10016 unsigned char *pointer;
10017
10018 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10019 pointer = TYPE_ALLOC (type, num_bytes);
10020 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10021 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10022 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10023 }
10024
10025 /* Copy the saved-up fields into the field vector. Start from the head of
10026 the list, adding to the tail of the field array, so that they end up in
10027 the same order in the array in which they were added to the list. */
10028 while (nfields-- > 0)
10029 {
10030 struct nextfield *fieldp;
10031
10032 if (fip->fields)
10033 {
10034 fieldp = fip->fields;
10035 fip->fields = fieldp->next;
10036 }
10037 else
10038 {
10039 fieldp = fip->baseclasses;
10040 fip->baseclasses = fieldp->next;
10041 }
10042
10043 TYPE_FIELD (type, nfields) = fieldp->field;
10044 switch (fieldp->accessibility)
10045 {
10046 case DW_ACCESS_private:
10047 if (cu->language != language_ada)
10048 SET_TYPE_FIELD_PRIVATE (type, nfields);
10049 break;
10050
10051 case DW_ACCESS_protected:
10052 if (cu->language != language_ada)
10053 SET_TYPE_FIELD_PROTECTED (type, nfields);
10054 break;
10055
10056 case DW_ACCESS_public:
10057 break;
10058
10059 default:
10060 /* Unknown accessibility. Complain and treat it as public. */
10061 {
10062 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10063 fieldp->accessibility);
10064 }
10065 break;
10066 }
10067 if (nfields < fip->nbaseclasses)
10068 {
10069 switch (fieldp->virtuality)
10070 {
10071 case DW_VIRTUALITY_virtual:
10072 case DW_VIRTUALITY_pure_virtual:
10073 if (cu->language == language_ada)
10074 error (_("unexpected virtuality in component of Ada type"));
10075 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10076 break;
10077 }
10078 }
10079 }
10080 }
10081
10082 /* Add a member function to the proper fieldlist. */
10083
10084 static void
10085 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10086 struct type *type, struct dwarf2_cu *cu)
10087 {
10088 struct objfile *objfile = cu->objfile;
10089 struct attribute *attr;
10090 struct fnfieldlist *flp;
10091 int i;
10092 struct fn_field *fnp;
10093 char *fieldname;
10094 struct nextfnfield *new_fnfield;
10095 struct type *this_type;
10096 enum dwarf_access_attribute accessibility;
10097
10098 if (cu->language == language_ada)
10099 error (_("unexpected member function in Ada type"));
10100
10101 /* Get name of member function. */
10102 fieldname = dwarf2_name (die, cu);
10103 if (fieldname == NULL)
10104 return;
10105
10106 /* Look up member function name in fieldlist. */
10107 for (i = 0; i < fip->nfnfields; i++)
10108 {
10109 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10110 break;
10111 }
10112
10113 /* Create new list element if necessary. */
10114 if (i < fip->nfnfields)
10115 flp = &fip->fnfieldlists[i];
10116 else
10117 {
10118 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10119 {
10120 fip->fnfieldlists = (struct fnfieldlist *)
10121 xrealloc (fip->fnfieldlists,
10122 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10123 * sizeof (struct fnfieldlist));
10124 if (fip->nfnfields == 0)
10125 make_cleanup (free_current_contents, &fip->fnfieldlists);
10126 }
10127 flp = &fip->fnfieldlists[fip->nfnfields];
10128 flp->name = fieldname;
10129 flp->length = 0;
10130 flp->head = NULL;
10131 i = fip->nfnfields++;
10132 }
10133
10134 /* Create a new member function field and chain it to the field list
10135 entry. */
10136 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10137 make_cleanup (xfree, new_fnfield);
10138 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10139 new_fnfield->next = flp->head;
10140 flp->head = new_fnfield;
10141 flp->length++;
10142
10143 /* Fill in the member function field info. */
10144 fnp = &new_fnfield->fnfield;
10145
10146 /* Delay processing of the physname until later. */
10147 if (cu->language == language_cplus || cu->language == language_java)
10148 {
10149 add_to_method_list (type, i, flp->length - 1, fieldname,
10150 die, cu);
10151 }
10152 else
10153 {
10154 const char *physname = dwarf2_physname (fieldname, die, cu);
10155 fnp->physname = physname ? physname : "";
10156 }
10157
10158 fnp->type = alloc_type (objfile);
10159 this_type = read_type_die (die, cu);
10160 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10161 {
10162 int nparams = TYPE_NFIELDS (this_type);
10163
10164 /* TYPE is the domain of this method, and THIS_TYPE is the type
10165 of the method itself (TYPE_CODE_METHOD). */
10166 smash_to_method_type (fnp->type, type,
10167 TYPE_TARGET_TYPE (this_type),
10168 TYPE_FIELDS (this_type),
10169 TYPE_NFIELDS (this_type),
10170 TYPE_VARARGS (this_type));
10171
10172 /* Handle static member functions.
10173 Dwarf2 has no clean way to discern C++ static and non-static
10174 member functions. G++ helps GDB by marking the first
10175 parameter for non-static member functions (which is the this
10176 pointer) as artificial. We obtain this information from
10177 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10178 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10179 fnp->voffset = VOFFSET_STATIC;
10180 }
10181 else
10182 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10183 dwarf2_full_name (fieldname, die, cu));
10184
10185 /* Get fcontext from DW_AT_containing_type if present. */
10186 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10187 fnp->fcontext = die_containing_type (die, cu);
10188
10189 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10190 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10191
10192 /* Get accessibility. */
10193 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10194 if (attr)
10195 accessibility = DW_UNSND (attr);
10196 else
10197 accessibility = dwarf2_default_access_attribute (die, cu);
10198 switch (accessibility)
10199 {
10200 case DW_ACCESS_private:
10201 fnp->is_private = 1;
10202 break;
10203 case DW_ACCESS_protected:
10204 fnp->is_protected = 1;
10205 break;
10206 }
10207
10208 /* Check for artificial methods. */
10209 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10210 if (attr && DW_UNSND (attr) != 0)
10211 fnp->is_artificial = 1;
10212
10213 /* Get index in virtual function table if it is a virtual member
10214 function. For older versions of GCC, this is an offset in the
10215 appropriate virtual table, as specified by DW_AT_containing_type.
10216 For everyone else, it is an expression to be evaluated relative
10217 to the object address. */
10218
10219 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10220 if (attr)
10221 {
10222 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10223 {
10224 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10225 {
10226 /* Old-style GCC. */
10227 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10228 }
10229 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10230 || (DW_BLOCK (attr)->size > 1
10231 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10232 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10233 {
10234 struct dwarf_block blk;
10235 int offset;
10236
10237 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10238 ? 1 : 2);
10239 blk.size = DW_BLOCK (attr)->size - offset;
10240 blk.data = DW_BLOCK (attr)->data + offset;
10241 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10242 if ((fnp->voffset % cu->header.addr_size) != 0)
10243 dwarf2_complex_location_expr_complaint ();
10244 else
10245 fnp->voffset /= cu->header.addr_size;
10246 fnp->voffset += 2;
10247 }
10248 else
10249 dwarf2_complex_location_expr_complaint ();
10250
10251 if (!fnp->fcontext)
10252 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10253 }
10254 else if (attr_form_is_section_offset (attr))
10255 {
10256 dwarf2_complex_location_expr_complaint ();
10257 }
10258 else
10259 {
10260 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10261 fieldname);
10262 }
10263 }
10264 else
10265 {
10266 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10267 if (attr && DW_UNSND (attr))
10268 {
10269 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10270 complaint (&symfile_complaints,
10271 _("Member function \"%s\" (offset %d) is virtual "
10272 "but the vtable offset is not specified"),
10273 fieldname, die->offset.sect_off);
10274 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10275 TYPE_CPLUS_DYNAMIC (type) = 1;
10276 }
10277 }
10278 }
10279
10280 /* Create the vector of member function fields, and attach it to the type. */
10281
10282 static void
10283 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10284 struct dwarf2_cu *cu)
10285 {
10286 struct fnfieldlist *flp;
10287 int i;
10288
10289 if (cu->language == language_ada)
10290 error (_("unexpected member functions in Ada type"));
10291
10292 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10293 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10294 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10295
10296 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10297 {
10298 struct nextfnfield *nfp = flp->head;
10299 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10300 int k;
10301
10302 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10303 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10304 fn_flp->fn_fields = (struct fn_field *)
10305 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10306 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10307 fn_flp->fn_fields[k] = nfp->fnfield;
10308 }
10309
10310 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10311 }
10312
10313 /* Returns non-zero if NAME is the name of a vtable member in CU's
10314 language, zero otherwise. */
10315 static int
10316 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10317 {
10318 static const char vptr[] = "_vptr";
10319 static const char vtable[] = "vtable";
10320
10321 /* Look for the C++ and Java forms of the vtable. */
10322 if ((cu->language == language_java
10323 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10324 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10325 && is_cplus_marker (name[sizeof (vptr) - 1])))
10326 return 1;
10327
10328 return 0;
10329 }
10330
10331 /* GCC outputs unnamed structures that are really pointers to member
10332 functions, with the ABI-specified layout. If TYPE describes
10333 such a structure, smash it into a member function type.
10334
10335 GCC shouldn't do this; it should just output pointer to member DIEs.
10336 This is GCC PR debug/28767. */
10337
10338 static void
10339 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10340 {
10341 struct type *pfn_type, *domain_type, *new_type;
10342
10343 /* Check for a structure with no name and two children. */
10344 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10345 return;
10346
10347 /* Check for __pfn and __delta members. */
10348 if (TYPE_FIELD_NAME (type, 0) == NULL
10349 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10350 || TYPE_FIELD_NAME (type, 1) == NULL
10351 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10352 return;
10353
10354 /* Find the type of the method. */
10355 pfn_type = TYPE_FIELD_TYPE (type, 0);
10356 if (pfn_type == NULL
10357 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10358 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10359 return;
10360
10361 /* Look for the "this" argument. */
10362 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10363 if (TYPE_NFIELDS (pfn_type) == 0
10364 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10365 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10366 return;
10367
10368 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10369 new_type = alloc_type (objfile);
10370 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10371 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10372 TYPE_VARARGS (pfn_type));
10373 smash_to_methodptr_type (type, new_type);
10374 }
10375
10376 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10377 (icc). */
10378
10379 static int
10380 producer_is_icc (struct dwarf2_cu *cu)
10381 {
10382 if (!cu->checked_producer)
10383 check_producer (cu);
10384
10385 return cu->producer_is_icc;
10386 }
10387
10388 /* Called when we find the DIE that starts a structure or union scope
10389 (definition) to create a type for the structure or union. Fill in
10390 the type's name and general properties; the members will not be
10391 processed until process_structure_type.
10392
10393 NOTE: we need to call these functions regardless of whether or not the
10394 DIE has a DW_AT_name attribute, since it might be an anonymous
10395 structure or union. This gets the type entered into our set of
10396 user defined types.
10397
10398 However, if the structure is incomplete (an opaque struct/union)
10399 then suppress creating a symbol table entry for it since gdb only
10400 wants to find the one with the complete definition. Note that if
10401 it is complete, we just call new_symbol, which does it's own
10402 checking about whether the struct/union is anonymous or not (and
10403 suppresses creating a symbol table entry itself). */
10404
10405 static struct type *
10406 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10407 {
10408 struct objfile *objfile = cu->objfile;
10409 struct type *type;
10410 struct attribute *attr;
10411 char *name;
10412
10413 /* If the definition of this type lives in .debug_types, read that type.
10414 Don't follow DW_AT_specification though, that will take us back up
10415 the chain and we want to go down. */
10416 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10417 if (attr)
10418 {
10419 struct dwarf2_cu *type_cu = cu;
10420 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10421
10422 /* We could just recurse on read_structure_type, but we need to call
10423 get_die_type to ensure only one type for this DIE is created.
10424 This is important, for example, because for c++ classes we need
10425 TYPE_NAME set which is only done by new_symbol. Blech. */
10426 type = read_type_die (type_die, type_cu);
10427
10428 /* TYPE_CU may not be the same as CU.
10429 Ensure TYPE is recorded in CU's type_hash table. */
10430 return set_die_type (die, type, cu);
10431 }
10432
10433 type = alloc_type (objfile);
10434 INIT_CPLUS_SPECIFIC (type);
10435
10436 name = dwarf2_name (die, cu);
10437 if (name != NULL)
10438 {
10439 if (cu->language == language_cplus
10440 || cu->language == language_java)
10441 {
10442 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10443
10444 /* dwarf2_full_name might have already finished building the DIE's
10445 type. If so, there is no need to continue. */
10446 if (get_die_type (die, cu) != NULL)
10447 return get_die_type (die, cu);
10448
10449 TYPE_TAG_NAME (type) = full_name;
10450 if (die->tag == DW_TAG_structure_type
10451 || die->tag == DW_TAG_class_type)
10452 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10453 }
10454 else
10455 {
10456 /* The name is already allocated along with this objfile, so
10457 we don't need to duplicate it for the type. */
10458 TYPE_TAG_NAME (type) = (char *) name;
10459 if (die->tag == DW_TAG_class_type)
10460 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10461 }
10462 }
10463
10464 if (die->tag == DW_TAG_structure_type)
10465 {
10466 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10467 }
10468 else if (die->tag == DW_TAG_union_type)
10469 {
10470 TYPE_CODE (type) = TYPE_CODE_UNION;
10471 }
10472 else
10473 {
10474 TYPE_CODE (type) = TYPE_CODE_CLASS;
10475 }
10476
10477 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10478 TYPE_DECLARED_CLASS (type) = 1;
10479
10480 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10481 if (attr)
10482 {
10483 TYPE_LENGTH (type) = DW_UNSND (attr);
10484 }
10485 else
10486 {
10487 TYPE_LENGTH (type) = 0;
10488 }
10489
10490 if (producer_is_icc (cu))
10491 {
10492 /* ICC does not output the required DW_AT_declaration
10493 on incomplete types, but gives them a size of zero. */
10494 }
10495 else
10496 TYPE_STUB_SUPPORTED (type) = 1;
10497
10498 if (die_is_declaration (die, cu))
10499 TYPE_STUB (type) = 1;
10500 else if (attr == NULL && die->child == NULL
10501 && producer_is_realview (cu->producer))
10502 /* RealView does not output the required DW_AT_declaration
10503 on incomplete types. */
10504 TYPE_STUB (type) = 1;
10505
10506 /* We need to add the type field to the die immediately so we don't
10507 infinitely recurse when dealing with pointers to the structure
10508 type within the structure itself. */
10509 set_die_type (die, type, cu);
10510
10511 /* set_die_type should be already done. */
10512 set_descriptive_type (type, die, cu);
10513
10514 return type;
10515 }
10516
10517 /* Finish creating a structure or union type, including filling in
10518 its members and creating a symbol for it. */
10519
10520 static void
10521 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10522 {
10523 struct objfile *objfile = cu->objfile;
10524 struct die_info *child_die = die->child;
10525 struct type *type;
10526
10527 type = get_die_type (die, cu);
10528 if (type == NULL)
10529 type = read_structure_type (die, cu);
10530
10531 if (die->child != NULL && ! die_is_declaration (die, cu))
10532 {
10533 struct field_info fi;
10534 struct die_info *child_die;
10535 VEC (symbolp) *template_args = NULL;
10536 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10537
10538 memset (&fi, 0, sizeof (struct field_info));
10539
10540 child_die = die->child;
10541
10542 while (child_die && child_die->tag)
10543 {
10544 if (child_die->tag == DW_TAG_member
10545 || child_die->tag == DW_TAG_variable)
10546 {
10547 /* NOTE: carlton/2002-11-05: A C++ static data member
10548 should be a DW_TAG_member that is a declaration, but
10549 all versions of G++ as of this writing (so through at
10550 least 3.2.1) incorrectly generate DW_TAG_variable
10551 tags for them instead. */
10552 dwarf2_add_field (&fi, child_die, cu);
10553 }
10554 else if (child_die->tag == DW_TAG_subprogram)
10555 {
10556 /* C++ member function. */
10557 dwarf2_add_member_fn (&fi, child_die, type, cu);
10558 }
10559 else if (child_die->tag == DW_TAG_inheritance)
10560 {
10561 /* C++ base class field. */
10562 dwarf2_add_field (&fi, child_die, cu);
10563 }
10564 else if (child_die->tag == DW_TAG_typedef)
10565 dwarf2_add_typedef (&fi, child_die, cu);
10566 else if (child_die->tag == DW_TAG_template_type_param
10567 || child_die->tag == DW_TAG_template_value_param)
10568 {
10569 struct symbol *arg = new_symbol (child_die, NULL, cu);
10570
10571 if (arg != NULL)
10572 VEC_safe_push (symbolp, template_args, arg);
10573 }
10574
10575 child_die = sibling_die (child_die);
10576 }
10577
10578 /* Attach template arguments to type. */
10579 if (! VEC_empty (symbolp, template_args))
10580 {
10581 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10582 TYPE_N_TEMPLATE_ARGUMENTS (type)
10583 = VEC_length (symbolp, template_args);
10584 TYPE_TEMPLATE_ARGUMENTS (type)
10585 = obstack_alloc (&objfile->objfile_obstack,
10586 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10587 * sizeof (struct symbol *)));
10588 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10589 VEC_address (symbolp, template_args),
10590 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10591 * sizeof (struct symbol *)));
10592 VEC_free (symbolp, template_args);
10593 }
10594
10595 /* Attach fields and member functions to the type. */
10596 if (fi.nfields)
10597 dwarf2_attach_fields_to_type (&fi, type, cu);
10598 if (fi.nfnfields)
10599 {
10600 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10601
10602 /* Get the type which refers to the base class (possibly this
10603 class itself) which contains the vtable pointer for the current
10604 class from the DW_AT_containing_type attribute. This use of
10605 DW_AT_containing_type is a GNU extension. */
10606
10607 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10608 {
10609 struct type *t = die_containing_type (die, cu);
10610
10611 TYPE_VPTR_BASETYPE (type) = t;
10612 if (type == t)
10613 {
10614 int i;
10615
10616 /* Our own class provides vtbl ptr. */
10617 for (i = TYPE_NFIELDS (t) - 1;
10618 i >= TYPE_N_BASECLASSES (t);
10619 --i)
10620 {
10621 const char *fieldname = TYPE_FIELD_NAME (t, i);
10622
10623 if (is_vtable_name (fieldname, cu))
10624 {
10625 TYPE_VPTR_FIELDNO (type) = i;
10626 break;
10627 }
10628 }
10629
10630 /* Complain if virtual function table field not found. */
10631 if (i < TYPE_N_BASECLASSES (t))
10632 complaint (&symfile_complaints,
10633 _("virtual function table pointer "
10634 "not found when defining class '%s'"),
10635 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10636 "");
10637 }
10638 else
10639 {
10640 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10641 }
10642 }
10643 else if (cu->producer
10644 && strncmp (cu->producer,
10645 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10646 {
10647 /* The IBM XLC compiler does not provide direct indication
10648 of the containing type, but the vtable pointer is
10649 always named __vfp. */
10650
10651 int i;
10652
10653 for (i = TYPE_NFIELDS (type) - 1;
10654 i >= TYPE_N_BASECLASSES (type);
10655 --i)
10656 {
10657 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10658 {
10659 TYPE_VPTR_FIELDNO (type) = i;
10660 TYPE_VPTR_BASETYPE (type) = type;
10661 break;
10662 }
10663 }
10664 }
10665 }
10666
10667 /* Copy fi.typedef_field_list linked list elements content into the
10668 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10669 if (fi.typedef_field_list)
10670 {
10671 int i = fi.typedef_field_list_count;
10672
10673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10674 TYPE_TYPEDEF_FIELD_ARRAY (type)
10675 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10676 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10677
10678 /* Reverse the list order to keep the debug info elements order. */
10679 while (--i >= 0)
10680 {
10681 struct typedef_field *dest, *src;
10682
10683 dest = &TYPE_TYPEDEF_FIELD (type, i);
10684 src = &fi.typedef_field_list->field;
10685 fi.typedef_field_list = fi.typedef_field_list->next;
10686 *dest = *src;
10687 }
10688 }
10689
10690 do_cleanups (back_to);
10691
10692 if (HAVE_CPLUS_STRUCT (type))
10693 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10694 }
10695
10696 quirk_gcc_member_function_pointer (type, objfile);
10697
10698 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10699 snapshots) has been known to create a die giving a declaration
10700 for a class that has, as a child, a die giving a definition for a
10701 nested class. So we have to process our children even if the
10702 current die is a declaration. Normally, of course, a declaration
10703 won't have any children at all. */
10704
10705 while (child_die != NULL && child_die->tag)
10706 {
10707 if (child_die->tag == DW_TAG_member
10708 || child_die->tag == DW_TAG_variable
10709 || child_die->tag == DW_TAG_inheritance
10710 || child_die->tag == DW_TAG_template_value_param
10711 || child_die->tag == DW_TAG_template_type_param)
10712 {
10713 /* Do nothing. */
10714 }
10715 else
10716 process_die (child_die, cu);
10717
10718 child_die = sibling_die (child_die);
10719 }
10720
10721 /* Do not consider external references. According to the DWARF standard,
10722 these DIEs are identified by the fact that they have no byte_size
10723 attribute, and a declaration attribute. */
10724 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10725 || !die_is_declaration (die, cu))
10726 new_symbol (die, type, cu);
10727 }
10728
10729 /* Given a DW_AT_enumeration_type die, set its type. We do not
10730 complete the type's fields yet, or create any symbols. */
10731
10732 static struct type *
10733 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735 struct objfile *objfile = cu->objfile;
10736 struct type *type;
10737 struct attribute *attr;
10738 const char *name;
10739
10740 /* If the definition of this type lives in .debug_types, read that type.
10741 Don't follow DW_AT_specification though, that will take us back up
10742 the chain and we want to go down. */
10743 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10744 if (attr)
10745 {
10746 struct dwarf2_cu *type_cu = cu;
10747 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10748
10749 type = read_type_die (type_die, type_cu);
10750
10751 /* TYPE_CU may not be the same as CU.
10752 Ensure TYPE is recorded in CU's type_hash table. */
10753 return set_die_type (die, type, cu);
10754 }
10755
10756 type = alloc_type (objfile);
10757
10758 TYPE_CODE (type) = TYPE_CODE_ENUM;
10759 name = dwarf2_full_name (NULL, die, cu);
10760 if (name != NULL)
10761 TYPE_TAG_NAME (type) = (char *) name;
10762
10763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10764 if (attr)
10765 {
10766 TYPE_LENGTH (type) = DW_UNSND (attr);
10767 }
10768 else
10769 {
10770 TYPE_LENGTH (type) = 0;
10771 }
10772
10773 /* The enumeration DIE can be incomplete. In Ada, any type can be
10774 declared as private in the package spec, and then defined only
10775 inside the package body. Such types are known as Taft Amendment
10776 Types. When another package uses such a type, an incomplete DIE
10777 may be generated by the compiler. */
10778 if (die_is_declaration (die, cu))
10779 TYPE_STUB (type) = 1;
10780
10781 return set_die_type (die, type, cu);
10782 }
10783
10784 /* Given a pointer to a die which begins an enumeration, process all
10785 the dies that define the members of the enumeration, and create the
10786 symbol for the enumeration type.
10787
10788 NOTE: We reverse the order of the element list. */
10789
10790 static void
10791 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10792 {
10793 struct type *this_type;
10794
10795 this_type = get_die_type (die, cu);
10796 if (this_type == NULL)
10797 this_type = read_enumeration_type (die, cu);
10798
10799 if (die->child != NULL)
10800 {
10801 struct die_info *child_die;
10802 struct symbol *sym;
10803 struct field *fields = NULL;
10804 int num_fields = 0;
10805 int unsigned_enum = 1;
10806 char *name;
10807 int flag_enum = 1;
10808 ULONGEST mask = 0;
10809
10810 child_die = die->child;
10811 while (child_die && child_die->tag)
10812 {
10813 if (child_die->tag != DW_TAG_enumerator)
10814 {
10815 process_die (child_die, cu);
10816 }
10817 else
10818 {
10819 name = dwarf2_name (child_die, cu);
10820 if (name)
10821 {
10822 sym = new_symbol (child_die, this_type, cu);
10823 if (SYMBOL_VALUE (sym) < 0)
10824 {
10825 unsigned_enum = 0;
10826 flag_enum = 0;
10827 }
10828 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10829 flag_enum = 0;
10830 else
10831 mask |= SYMBOL_VALUE (sym);
10832
10833 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10834 {
10835 fields = (struct field *)
10836 xrealloc (fields,
10837 (num_fields + DW_FIELD_ALLOC_CHUNK)
10838 * sizeof (struct field));
10839 }
10840
10841 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10842 FIELD_TYPE (fields[num_fields]) = NULL;
10843 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10844 FIELD_BITSIZE (fields[num_fields]) = 0;
10845
10846 num_fields++;
10847 }
10848 }
10849
10850 child_die = sibling_die (child_die);
10851 }
10852
10853 if (num_fields)
10854 {
10855 TYPE_NFIELDS (this_type) = num_fields;
10856 TYPE_FIELDS (this_type) = (struct field *)
10857 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10858 memcpy (TYPE_FIELDS (this_type), fields,
10859 sizeof (struct field) * num_fields);
10860 xfree (fields);
10861 }
10862 if (unsigned_enum)
10863 TYPE_UNSIGNED (this_type) = 1;
10864 if (flag_enum)
10865 TYPE_FLAG_ENUM (this_type) = 1;
10866 }
10867
10868 /* If we are reading an enum from a .debug_types unit, and the enum
10869 is a declaration, and the enum is not the signatured type in the
10870 unit, then we do not want to add a symbol for it. Adding a
10871 symbol would in some cases obscure the true definition of the
10872 enum, giving users an incomplete type when the definition is
10873 actually available. Note that we do not want to do this for all
10874 enums which are just declarations, because C++0x allows forward
10875 enum declarations. */
10876 if (cu->per_cu->is_debug_types
10877 && die_is_declaration (die, cu))
10878 {
10879 struct signatured_type *sig_type;
10880
10881 sig_type
10882 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10883 cu->per_cu->info_or_types_section,
10884 cu->per_cu->offset);
10885 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10886 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10887 return;
10888 }
10889
10890 new_symbol (die, this_type, cu);
10891 }
10892
10893 /* Extract all information from a DW_TAG_array_type DIE and put it in
10894 the DIE's type field. For now, this only handles one dimensional
10895 arrays. */
10896
10897 static struct type *
10898 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10899 {
10900 struct objfile *objfile = cu->objfile;
10901 struct die_info *child_die;
10902 struct type *type;
10903 struct type *element_type, *range_type, *index_type;
10904 struct type **range_types = NULL;
10905 struct attribute *attr;
10906 int ndim = 0;
10907 struct cleanup *back_to;
10908 char *name;
10909
10910 element_type = die_type (die, cu);
10911
10912 /* The die_type call above may have already set the type for this DIE. */
10913 type = get_die_type (die, cu);
10914 if (type)
10915 return type;
10916
10917 /* Irix 6.2 native cc creates array types without children for
10918 arrays with unspecified length. */
10919 if (die->child == NULL)
10920 {
10921 index_type = objfile_type (objfile)->builtin_int;
10922 range_type = create_range_type (NULL, index_type, 0, -1);
10923 type = create_array_type (NULL, element_type, range_type);
10924 return set_die_type (die, type, cu);
10925 }
10926
10927 back_to = make_cleanup (null_cleanup, NULL);
10928 child_die = die->child;
10929 while (child_die && child_die->tag)
10930 {
10931 if (child_die->tag == DW_TAG_subrange_type)
10932 {
10933 struct type *child_type = read_type_die (child_die, cu);
10934
10935 if (child_type != NULL)
10936 {
10937 /* The range type was succesfully read. Save it for the
10938 array type creation. */
10939 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10940 {
10941 range_types = (struct type **)
10942 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10943 * sizeof (struct type *));
10944 if (ndim == 0)
10945 make_cleanup (free_current_contents, &range_types);
10946 }
10947 range_types[ndim++] = child_type;
10948 }
10949 }
10950 child_die = sibling_die (child_die);
10951 }
10952
10953 /* Dwarf2 dimensions are output from left to right, create the
10954 necessary array types in backwards order. */
10955
10956 type = element_type;
10957
10958 if (read_array_order (die, cu) == DW_ORD_col_major)
10959 {
10960 int i = 0;
10961
10962 while (i < ndim)
10963 type = create_array_type (NULL, type, range_types[i++]);
10964 }
10965 else
10966 {
10967 while (ndim-- > 0)
10968 type = create_array_type (NULL, type, range_types[ndim]);
10969 }
10970
10971 /* Understand Dwarf2 support for vector types (like they occur on
10972 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10973 array type. This is not part of the Dwarf2/3 standard yet, but a
10974 custom vendor extension. The main difference between a regular
10975 array and the vector variant is that vectors are passed by value
10976 to functions. */
10977 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10978 if (attr)
10979 make_vector_type (type);
10980
10981 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10982 implementation may choose to implement triple vectors using this
10983 attribute. */
10984 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10985 if (attr)
10986 {
10987 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10988 TYPE_LENGTH (type) = DW_UNSND (attr);
10989 else
10990 complaint (&symfile_complaints,
10991 _("DW_AT_byte_size for array type smaller "
10992 "than the total size of elements"));
10993 }
10994
10995 name = dwarf2_name (die, cu);
10996 if (name)
10997 TYPE_NAME (type) = name;
10998
10999 /* Install the type in the die. */
11000 set_die_type (die, type, cu);
11001
11002 /* set_die_type should be already done. */
11003 set_descriptive_type (type, die, cu);
11004
11005 do_cleanups (back_to);
11006
11007 return type;
11008 }
11009
11010 static enum dwarf_array_dim_ordering
11011 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11012 {
11013 struct attribute *attr;
11014
11015 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11016
11017 if (attr) return DW_SND (attr);
11018
11019 /* GNU F77 is a special case, as at 08/2004 array type info is the
11020 opposite order to the dwarf2 specification, but data is still
11021 laid out as per normal fortran.
11022
11023 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11024 version checking. */
11025
11026 if (cu->language == language_fortran
11027 && cu->producer && strstr (cu->producer, "GNU F77"))
11028 {
11029 return DW_ORD_row_major;
11030 }
11031
11032 switch (cu->language_defn->la_array_ordering)
11033 {
11034 case array_column_major:
11035 return DW_ORD_col_major;
11036 case array_row_major:
11037 default:
11038 return DW_ORD_row_major;
11039 };
11040 }
11041
11042 /* Extract all information from a DW_TAG_set_type DIE and put it in
11043 the DIE's type field. */
11044
11045 static struct type *
11046 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11047 {
11048 struct type *domain_type, *set_type;
11049 struct attribute *attr;
11050
11051 domain_type = die_type (die, cu);
11052
11053 /* The die_type call above may have already set the type for this DIE. */
11054 set_type = get_die_type (die, cu);
11055 if (set_type)
11056 return set_type;
11057
11058 set_type = create_set_type (NULL, domain_type);
11059
11060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11061 if (attr)
11062 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11063
11064 return set_die_type (die, set_type, cu);
11065 }
11066
11067 /* Create appropriate locally-scoped variables for all the
11068 DW_TAG_common_block entries. Also create a struct common_block
11069 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11070 is used to sepate the common blocks name namespace from regular
11071 variable names. */
11072
11073 static void
11074 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11075 {
11076 if (die->child != NULL)
11077 {
11078 struct objfile *objfile = cu->objfile;
11079 struct die_info *child_die;
11080 size_t n_entries = 0, size;
11081 struct common_block *common_block;
11082 struct symbol *sym;
11083
11084 for (child_die = die->child;
11085 child_die && child_die->tag;
11086 child_die = sibling_die (child_die))
11087 ++n_entries;
11088
11089 size = (sizeof (struct common_block)
11090 + (n_entries - 1) * sizeof (struct symbol *));
11091 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11092 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11093 common_block->n_entries = 0;
11094
11095 for (child_die = die->child;
11096 child_die && child_die->tag;
11097 child_die = sibling_die (child_die))
11098 {
11099 /* Create the symbol in the DW_TAG_common_block block in the current
11100 symbol scope. */
11101 sym = new_symbol (child_die, NULL, cu);
11102 if (sym)
11103 common_block->contents[common_block->n_entries++] = sym;
11104 }
11105
11106 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11107 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
11108 }
11109 }
11110
11111 /* Create a type for a C++ namespace. */
11112
11113 static struct type *
11114 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11115 {
11116 struct objfile *objfile = cu->objfile;
11117 const char *previous_prefix, *name;
11118 int is_anonymous;
11119 struct type *type;
11120
11121 /* For extensions, reuse the type of the original namespace. */
11122 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11123 {
11124 struct die_info *ext_die;
11125 struct dwarf2_cu *ext_cu = cu;
11126
11127 ext_die = dwarf2_extension (die, &ext_cu);
11128 type = read_type_die (ext_die, ext_cu);
11129
11130 /* EXT_CU may not be the same as CU.
11131 Ensure TYPE is recorded in CU's type_hash table. */
11132 return set_die_type (die, type, cu);
11133 }
11134
11135 name = namespace_name (die, &is_anonymous, cu);
11136
11137 /* Now build the name of the current namespace. */
11138
11139 previous_prefix = determine_prefix (die, cu);
11140 if (previous_prefix[0] != '\0')
11141 name = typename_concat (&objfile->objfile_obstack,
11142 previous_prefix, name, 0, cu);
11143
11144 /* Create the type. */
11145 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11146 objfile);
11147 TYPE_NAME (type) = (char *) name;
11148 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11149
11150 return set_die_type (die, type, cu);
11151 }
11152
11153 /* Read a C++ namespace. */
11154
11155 static void
11156 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11157 {
11158 struct objfile *objfile = cu->objfile;
11159 int is_anonymous;
11160
11161 /* Add a symbol associated to this if we haven't seen the namespace
11162 before. Also, add a using directive if it's an anonymous
11163 namespace. */
11164
11165 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11166 {
11167 struct type *type;
11168
11169 type = read_type_die (die, cu);
11170 new_symbol (die, type, cu);
11171
11172 namespace_name (die, &is_anonymous, cu);
11173 if (is_anonymous)
11174 {
11175 const char *previous_prefix = determine_prefix (die, cu);
11176
11177 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11178 NULL, NULL, &objfile->objfile_obstack);
11179 }
11180 }
11181
11182 if (die->child != NULL)
11183 {
11184 struct die_info *child_die = die->child;
11185
11186 while (child_die && child_die->tag)
11187 {
11188 process_die (child_die, cu);
11189 child_die = sibling_die (child_die);
11190 }
11191 }
11192 }
11193
11194 /* Read a Fortran module as type. This DIE can be only a declaration used for
11195 imported module. Still we need that type as local Fortran "use ... only"
11196 declaration imports depend on the created type in determine_prefix. */
11197
11198 static struct type *
11199 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11200 {
11201 struct objfile *objfile = cu->objfile;
11202 char *module_name;
11203 struct type *type;
11204
11205 module_name = dwarf2_name (die, cu);
11206 if (!module_name)
11207 complaint (&symfile_complaints,
11208 _("DW_TAG_module has no name, offset 0x%x"),
11209 die->offset.sect_off);
11210 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11211
11212 /* determine_prefix uses TYPE_TAG_NAME. */
11213 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11214
11215 return set_die_type (die, type, cu);
11216 }
11217
11218 /* Read a Fortran module. */
11219
11220 static void
11221 read_module (struct die_info *die, struct dwarf2_cu *cu)
11222 {
11223 struct die_info *child_die = die->child;
11224
11225 while (child_die && child_die->tag)
11226 {
11227 process_die (child_die, cu);
11228 child_die = sibling_die (child_die);
11229 }
11230 }
11231
11232 /* Return the name of the namespace represented by DIE. Set
11233 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11234 namespace. */
11235
11236 static const char *
11237 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11238 {
11239 struct die_info *current_die;
11240 const char *name = NULL;
11241
11242 /* Loop through the extensions until we find a name. */
11243
11244 for (current_die = die;
11245 current_die != NULL;
11246 current_die = dwarf2_extension (die, &cu))
11247 {
11248 name = dwarf2_name (current_die, cu);
11249 if (name != NULL)
11250 break;
11251 }
11252
11253 /* Is it an anonymous namespace? */
11254
11255 *is_anonymous = (name == NULL);
11256 if (*is_anonymous)
11257 name = CP_ANONYMOUS_NAMESPACE_STR;
11258
11259 return name;
11260 }
11261
11262 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11263 the user defined type vector. */
11264
11265 static struct type *
11266 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11267 {
11268 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11269 struct comp_unit_head *cu_header = &cu->header;
11270 struct type *type;
11271 struct attribute *attr_byte_size;
11272 struct attribute *attr_address_class;
11273 int byte_size, addr_class;
11274 struct type *target_type;
11275
11276 target_type = die_type (die, cu);
11277
11278 /* The die_type call above may have already set the type for this DIE. */
11279 type = get_die_type (die, cu);
11280 if (type)
11281 return type;
11282
11283 type = lookup_pointer_type (target_type);
11284
11285 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11286 if (attr_byte_size)
11287 byte_size = DW_UNSND (attr_byte_size);
11288 else
11289 byte_size = cu_header->addr_size;
11290
11291 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11292 if (attr_address_class)
11293 addr_class = DW_UNSND (attr_address_class);
11294 else
11295 addr_class = DW_ADDR_none;
11296
11297 /* If the pointer size or address class is different than the
11298 default, create a type variant marked as such and set the
11299 length accordingly. */
11300 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11301 {
11302 if (gdbarch_address_class_type_flags_p (gdbarch))
11303 {
11304 int type_flags;
11305
11306 type_flags = gdbarch_address_class_type_flags
11307 (gdbarch, byte_size, addr_class);
11308 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11309 == 0);
11310 type = make_type_with_address_space (type, type_flags);
11311 }
11312 else if (TYPE_LENGTH (type) != byte_size)
11313 {
11314 complaint (&symfile_complaints,
11315 _("invalid pointer size %d"), byte_size);
11316 }
11317 else
11318 {
11319 /* Should we also complain about unhandled address classes? */
11320 }
11321 }
11322
11323 TYPE_LENGTH (type) = byte_size;
11324 return set_die_type (die, type, cu);
11325 }
11326
11327 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11328 the user defined type vector. */
11329
11330 static struct type *
11331 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11332 {
11333 struct type *type;
11334 struct type *to_type;
11335 struct type *domain;
11336
11337 to_type = die_type (die, cu);
11338 domain = die_containing_type (die, cu);
11339
11340 /* The calls above may have already set the type for this DIE. */
11341 type = get_die_type (die, cu);
11342 if (type)
11343 return type;
11344
11345 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11346 type = lookup_methodptr_type (to_type);
11347 else
11348 type = lookup_memberptr_type (to_type, domain);
11349
11350 return set_die_type (die, type, cu);
11351 }
11352
11353 /* Extract all information from a DW_TAG_reference_type DIE and add to
11354 the user defined type vector. */
11355
11356 static struct type *
11357 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11358 {
11359 struct comp_unit_head *cu_header = &cu->header;
11360 struct type *type, *target_type;
11361 struct attribute *attr;
11362
11363 target_type = die_type (die, cu);
11364
11365 /* The die_type call above may have already set the type for this DIE. */
11366 type = get_die_type (die, cu);
11367 if (type)
11368 return type;
11369
11370 type = lookup_reference_type (target_type);
11371 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11372 if (attr)
11373 {
11374 TYPE_LENGTH (type) = DW_UNSND (attr);
11375 }
11376 else
11377 {
11378 TYPE_LENGTH (type) = cu_header->addr_size;
11379 }
11380 return set_die_type (die, type, cu);
11381 }
11382
11383 static struct type *
11384 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11385 {
11386 struct type *base_type, *cv_type;
11387
11388 base_type = die_type (die, cu);
11389
11390 /* The die_type call above may have already set the type for this DIE. */
11391 cv_type = get_die_type (die, cu);
11392 if (cv_type)
11393 return cv_type;
11394
11395 /* In case the const qualifier is applied to an array type, the element type
11396 is so qualified, not the array type (section 6.7.3 of C99). */
11397 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11398 {
11399 struct type *el_type, *inner_array;
11400
11401 base_type = copy_type (base_type);
11402 inner_array = base_type;
11403
11404 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11405 {
11406 TYPE_TARGET_TYPE (inner_array) =
11407 copy_type (TYPE_TARGET_TYPE (inner_array));
11408 inner_array = TYPE_TARGET_TYPE (inner_array);
11409 }
11410
11411 el_type = TYPE_TARGET_TYPE (inner_array);
11412 TYPE_TARGET_TYPE (inner_array) =
11413 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11414
11415 return set_die_type (die, base_type, cu);
11416 }
11417
11418 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11419 return set_die_type (die, cv_type, cu);
11420 }
11421
11422 static struct type *
11423 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11424 {
11425 struct type *base_type, *cv_type;
11426
11427 base_type = die_type (die, cu);
11428
11429 /* The die_type call above may have already set the type for this DIE. */
11430 cv_type = get_die_type (die, cu);
11431 if (cv_type)
11432 return cv_type;
11433
11434 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11435 return set_die_type (die, cv_type, cu);
11436 }
11437
11438 /* Extract all information from a DW_TAG_string_type DIE and add to
11439 the user defined type vector. It isn't really a user defined type,
11440 but it behaves like one, with other DIE's using an AT_user_def_type
11441 attribute to reference it. */
11442
11443 static struct type *
11444 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11445 {
11446 struct objfile *objfile = cu->objfile;
11447 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11448 struct type *type, *range_type, *index_type, *char_type;
11449 struct attribute *attr;
11450 unsigned int length;
11451
11452 attr = dwarf2_attr (die, DW_AT_string_length, cu);
11453 if (attr)
11454 {
11455 length = DW_UNSND (attr);
11456 }
11457 else
11458 {
11459 /* Check for the DW_AT_byte_size attribute. */
11460 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11461 if (attr)
11462 {
11463 length = DW_UNSND (attr);
11464 }
11465 else
11466 {
11467 length = 1;
11468 }
11469 }
11470
11471 index_type = objfile_type (objfile)->builtin_int;
11472 range_type = create_range_type (NULL, index_type, 1, length);
11473 char_type = language_string_char_type (cu->language_defn, gdbarch);
11474 type = create_string_type (NULL, char_type, range_type);
11475
11476 return set_die_type (die, type, cu);
11477 }
11478
11479 /* Handle DIES due to C code like:
11480
11481 struct foo
11482 {
11483 int (*funcp)(int a, long l);
11484 int b;
11485 };
11486
11487 ('funcp' generates a DW_TAG_subroutine_type DIE). */
11488
11489 static struct type *
11490 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11491 {
11492 struct objfile *objfile = cu->objfile;
11493 struct type *type; /* Type that this function returns. */
11494 struct type *ftype; /* Function that returns above type. */
11495 struct attribute *attr;
11496
11497 type = die_type (die, cu);
11498
11499 /* The die_type call above may have already set the type for this DIE. */
11500 ftype = get_die_type (die, cu);
11501 if (ftype)
11502 return ftype;
11503
11504 ftype = lookup_function_type (type);
11505
11506 /* All functions in C++, Pascal and Java have prototypes. */
11507 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11508 if ((attr && (DW_UNSND (attr) != 0))
11509 || cu->language == language_cplus
11510 || cu->language == language_java
11511 || cu->language == language_pascal)
11512 TYPE_PROTOTYPED (ftype) = 1;
11513 else if (producer_is_realview (cu->producer))
11514 /* RealView does not emit DW_AT_prototyped. We can not
11515 distinguish prototyped and unprototyped functions; default to
11516 prototyped, since that is more common in modern code (and
11517 RealView warns about unprototyped functions). */
11518 TYPE_PROTOTYPED (ftype) = 1;
11519
11520 /* Store the calling convention in the type if it's available in
11521 the subroutine die. Otherwise set the calling convention to
11522 the default value DW_CC_normal. */
11523 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11524 if (attr)
11525 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11526 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11527 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11528 else
11529 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11530
11531 /* We need to add the subroutine type to the die immediately so
11532 we don't infinitely recurse when dealing with parameters
11533 declared as the same subroutine type. */
11534 set_die_type (die, ftype, cu);
11535
11536 if (die->child != NULL)
11537 {
11538 struct type *void_type = objfile_type (objfile)->builtin_void;
11539 struct die_info *child_die;
11540 int nparams, iparams;
11541
11542 /* Count the number of parameters.
11543 FIXME: GDB currently ignores vararg functions, but knows about
11544 vararg member functions. */
11545 nparams = 0;
11546 child_die = die->child;
11547 while (child_die && child_die->tag)
11548 {
11549 if (child_die->tag == DW_TAG_formal_parameter)
11550 nparams++;
11551 else if (child_die->tag == DW_TAG_unspecified_parameters)
11552 TYPE_VARARGS (ftype) = 1;
11553 child_die = sibling_die (child_die);
11554 }
11555
11556 /* Allocate storage for parameters and fill them in. */
11557 TYPE_NFIELDS (ftype) = nparams;
11558 TYPE_FIELDS (ftype) = (struct field *)
11559 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11560
11561 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11562 even if we error out during the parameters reading below. */
11563 for (iparams = 0; iparams < nparams; iparams++)
11564 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11565
11566 iparams = 0;
11567 child_die = die->child;
11568 while (child_die && child_die->tag)
11569 {
11570 if (child_die->tag == DW_TAG_formal_parameter)
11571 {
11572 struct type *arg_type;
11573
11574 /* DWARF version 2 has no clean way to discern C++
11575 static and non-static member functions. G++ helps
11576 GDB by marking the first parameter for non-static
11577 member functions (which is the this pointer) as
11578 artificial. We pass this information to
11579 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11580
11581 DWARF version 3 added DW_AT_object_pointer, which GCC
11582 4.5 does not yet generate. */
11583 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11584 if (attr)
11585 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11586 else
11587 {
11588 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11589
11590 /* GCC/43521: In java, the formal parameter
11591 "this" is sometimes not marked with DW_AT_artificial. */
11592 if (cu->language == language_java)
11593 {
11594 const char *name = dwarf2_name (child_die, cu);
11595
11596 if (name && !strcmp (name, "this"))
11597 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11598 }
11599 }
11600 arg_type = die_type (child_die, cu);
11601
11602 /* RealView does not mark THIS as const, which the testsuite
11603 expects. GCC marks THIS as const in method definitions,
11604 but not in the class specifications (GCC PR 43053). */
11605 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11606 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11607 {
11608 int is_this = 0;
11609 struct dwarf2_cu *arg_cu = cu;
11610 const char *name = dwarf2_name (child_die, cu);
11611
11612 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11613 if (attr)
11614 {
11615 /* If the compiler emits this, use it. */
11616 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11617 is_this = 1;
11618 }
11619 else if (name && strcmp (name, "this") == 0)
11620 /* Function definitions will have the argument names. */
11621 is_this = 1;
11622 else if (name == NULL && iparams == 0)
11623 /* Declarations may not have the names, so like
11624 elsewhere in GDB, assume an artificial first
11625 argument is "this". */
11626 is_this = 1;
11627
11628 if (is_this)
11629 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11630 arg_type, 0);
11631 }
11632
11633 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11634 iparams++;
11635 }
11636 child_die = sibling_die (child_die);
11637 }
11638 }
11639
11640 return ftype;
11641 }
11642
11643 static struct type *
11644 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11645 {
11646 struct objfile *objfile = cu->objfile;
11647 const char *name = NULL;
11648 struct type *this_type, *target_type;
11649
11650 name = dwarf2_full_name (NULL, die, cu);
11651 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11652 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11653 TYPE_NAME (this_type) = (char *) name;
11654 set_die_type (die, this_type, cu);
11655 target_type = die_type (die, cu);
11656 if (target_type != this_type)
11657 TYPE_TARGET_TYPE (this_type) = target_type;
11658 else
11659 {
11660 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11661 spec and cause infinite loops in GDB. */
11662 complaint (&symfile_complaints,
11663 _("Self-referential DW_TAG_typedef "
11664 "- DIE at 0x%x [in module %s]"),
11665 die->offset.sect_off, objfile->name);
11666 TYPE_TARGET_TYPE (this_type) = NULL;
11667 }
11668 return this_type;
11669 }
11670
11671 /* Find a representation of a given base type and install
11672 it in the TYPE field of the die. */
11673
11674 static struct type *
11675 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11676 {
11677 struct objfile *objfile = cu->objfile;
11678 struct type *type;
11679 struct attribute *attr;
11680 int encoding = 0, size = 0;
11681 char *name;
11682 enum type_code code = TYPE_CODE_INT;
11683 int type_flags = 0;
11684 struct type *target_type = NULL;
11685
11686 attr = dwarf2_attr (die, DW_AT_encoding, cu);
11687 if (attr)
11688 {
11689 encoding = DW_UNSND (attr);
11690 }
11691 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11692 if (attr)
11693 {
11694 size = DW_UNSND (attr);
11695 }
11696 name = dwarf2_name (die, cu);
11697 if (!name)
11698 {
11699 complaint (&symfile_complaints,
11700 _("DW_AT_name missing from DW_TAG_base_type"));
11701 }
11702
11703 switch (encoding)
11704 {
11705 case DW_ATE_address:
11706 /* Turn DW_ATE_address into a void * pointer. */
11707 code = TYPE_CODE_PTR;
11708 type_flags |= TYPE_FLAG_UNSIGNED;
11709 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11710 break;
11711 case DW_ATE_boolean:
11712 code = TYPE_CODE_BOOL;
11713 type_flags |= TYPE_FLAG_UNSIGNED;
11714 break;
11715 case DW_ATE_complex_float:
11716 code = TYPE_CODE_COMPLEX;
11717 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11718 break;
11719 case DW_ATE_decimal_float:
11720 code = TYPE_CODE_DECFLOAT;
11721 break;
11722 case DW_ATE_float:
11723 code = TYPE_CODE_FLT;
11724 break;
11725 case DW_ATE_signed:
11726 break;
11727 case DW_ATE_unsigned:
11728 type_flags |= TYPE_FLAG_UNSIGNED;
11729 if (cu->language == language_fortran
11730 && name
11731 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11732 code = TYPE_CODE_CHAR;
11733 break;
11734 case DW_ATE_signed_char:
11735 if (cu->language == language_ada || cu->language == language_m2
11736 || cu->language == language_pascal
11737 || cu->language == language_fortran)
11738 code = TYPE_CODE_CHAR;
11739 break;
11740 case DW_ATE_unsigned_char:
11741 if (cu->language == language_ada || cu->language == language_m2
11742 || cu->language == language_pascal
11743 || cu->language == language_fortran)
11744 code = TYPE_CODE_CHAR;
11745 type_flags |= TYPE_FLAG_UNSIGNED;
11746 break;
11747 case DW_ATE_UTF:
11748 /* We just treat this as an integer and then recognize the
11749 type by name elsewhere. */
11750 break;
11751
11752 default:
11753 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11754 dwarf_type_encoding_name (encoding));
11755 break;
11756 }
11757
11758 type = init_type (code, size, type_flags, NULL, objfile);
11759 TYPE_NAME (type) = name;
11760 TYPE_TARGET_TYPE (type) = target_type;
11761
11762 if (name && strcmp (name, "char") == 0)
11763 TYPE_NOSIGN (type) = 1;
11764
11765 return set_die_type (die, type, cu);
11766 }
11767
11768 /* Read the given DW_AT_subrange DIE. */
11769
11770 static struct type *
11771 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11772 {
11773 struct type *base_type;
11774 struct type *range_type;
11775 struct attribute *attr;
11776 LONGEST low, high;
11777 int low_default_is_valid;
11778 char *name;
11779 LONGEST negative_mask;
11780
11781 base_type = die_type (die, cu);
11782 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11783 check_typedef (base_type);
11784
11785 /* The die_type call above may have already set the type for this DIE. */
11786 range_type = get_die_type (die, cu);
11787 if (range_type)
11788 return range_type;
11789
11790 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11791 omitting DW_AT_lower_bound. */
11792 switch (cu->language)
11793 {
11794 case language_c:
11795 case language_cplus:
11796 low = 0;
11797 low_default_is_valid = 1;
11798 break;
11799 case language_fortran:
11800 low = 1;
11801 low_default_is_valid = 1;
11802 break;
11803 case language_d:
11804 case language_java:
11805 case language_objc:
11806 low = 0;
11807 low_default_is_valid = (cu->header.version >= 4);
11808 break;
11809 case language_ada:
11810 case language_m2:
11811 case language_pascal:
11812 low = 1;
11813 low_default_is_valid = (cu->header.version >= 4);
11814 break;
11815 default:
11816 low = 0;
11817 low_default_is_valid = 0;
11818 break;
11819 }
11820
11821 /* FIXME: For variable sized arrays either of these could be
11822 a variable rather than a constant value. We'll allow it,
11823 but we don't know how to handle it. */
11824 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11825 if (attr)
11826 low = dwarf2_get_attr_constant_value (attr, low);
11827 else if (!low_default_is_valid)
11828 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11829 "- DIE at 0x%x [in module %s]"),
11830 die->offset.sect_off, cu->objfile->name);
11831
11832 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11833 if (attr)
11834 {
11835 if (attr_form_is_block (attr) || is_ref_attr (attr))
11836 {
11837 /* GCC encodes arrays with unspecified or dynamic length
11838 with a DW_FORM_block1 attribute or a reference attribute.
11839 FIXME: GDB does not yet know how to handle dynamic
11840 arrays properly, treat them as arrays with unspecified
11841 length for now.
11842
11843 FIXME: jimb/2003-09-22: GDB does not really know
11844 how to handle arrays of unspecified length
11845 either; we just represent them as zero-length
11846 arrays. Choose an appropriate upper bound given
11847 the lower bound we've computed above. */
11848 high = low - 1;
11849 }
11850 else
11851 high = dwarf2_get_attr_constant_value (attr, 1);
11852 }
11853 else
11854 {
11855 attr = dwarf2_attr (die, DW_AT_count, cu);
11856 if (attr)
11857 {
11858 int count = dwarf2_get_attr_constant_value (attr, 1);
11859 high = low + count - 1;
11860 }
11861 else
11862 {
11863 /* Unspecified array length. */
11864 high = low - 1;
11865 }
11866 }
11867
11868 /* Dwarf-2 specifications explicitly allows to create subrange types
11869 without specifying a base type.
11870 In that case, the base type must be set to the type of
11871 the lower bound, upper bound or count, in that order, if any of these
11872 three attributes references an object that has a type.
11873 If no base type is found, the Dwarf-2 specifications say that
11874 a signed integer type of size equal to the size of an address should
11875 be used.
11876 For the following C code: `extern char gdb_int [];'
11877 GCC produces an empty range DIE.
11878 FIXME: muller/2010-05-28: Possible references to object for low bound,
11879 high bound or count are not yet handled by this code. */
11880 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11881 {
11882 struct objfile *objfile = cu->objfile;
11883 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11884 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11885 struct type *int_type = objfile_type (objfile)->builtin_int;
11886
11887 /* Test "int", "long int", and "long long int" objfile types,
11888 and select the first one having a size above or equal to the
11889 architecture address size. */
11890 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11891 base_type = int_type;
11892 else
11893 {
11894 int_type = objfile_type (objfile)->builtin_long;
11895 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11896 base_type = int_type;
11897 else
11898 {
11899 int_type = objfile_type (objfile)->builtin_long_long;
11900 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11901 base_type = int_type;
11902 }
11903 }
11904 }
11905
11906 negative_mask =
11907 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11908 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11909 low |= negative_mask;
11910 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11911 high |= negative_mask;
11912
11913 range_type = create_range_type (NULL, base_type, low, high);
11914
11915 /* Mark arrays with dynamic length at least as an array of unspecified
11916 length. GDB could check the boundary but before it gets implemented at
11917 least allow accessing the array elements. */
11918 if (attr && attr_form_is_block (attr))
11919 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11920
11921 /* Ada expects an empty array on no boundary attributes. */
11922 if (attr == NULL && cu->language != language_ada)
11923 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11924
11925 name = dwarf2_name (die, cu);
11926 if (name)
11927 TYPE_NAME (range_type) = name;
11928
11929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11930 if (attr)
11931 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11932
11933 set_die_type (die, range_type, cu);
11934
11935 /* set_die_type should be already done. */
11936 set_descriptive_type (range_type, die, cu);
11937
11938 return range_type;
11939 }
11940
11941 static struct type *
11942 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11943 {
11944 struct type *type;
11945
11946 /* For now, we only support the C meaning of an unspecified type: void. */
11947
11948 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11949 TYPE_NAME (type) = dwarf2_name (die, cu);
11950
11951 return set_die_type (die, type, cu);
11952 }
11953
11954 /* Read a single die and all its descendents. Set the die's sibling
11955 field to NULL; set other fields in the die correctly, and set all
11956 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11957 location of the info_ptr after reading all of those dies. PARENT
11958 is the parent of the die in question. */
11959
11960 static struct die_info *
11961 read_die_and_children (const struct die_reader_specs *reader,
11962 gdb_byte *info_ptr,
11963 gdb_byte **new_info_ptr,
11964 struct die_info *parent)
11965 {
11966 struct die_info *die;
11967 gdb_byte *cur_ptr;
11968 int has_children;
11969
11970 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11971 if (die == NULL)
11972 {
11973 *new_info_ptr = cur_ptr;
11974 return NULL;
11975 }
11976 store_in_ref_table (die, reader->cu);
11977
11978 if (has_children)
11979 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11980 else
11981 {
11982 die->child = NULL;
11983 *new_info_ptr = cur_ptr;
11984 }
11985
11986 die->sibling = NULL;
11987 die->parent = parent;
11988 return die;
11989 }
11990
11991 /* Read a die, all of its descendents, and all of its siblings; set
11992 all of the fields of all of the dies correctly. Arguments are as
11993 in read_die_and_children. */
11994
11995 static struct die_info *
11996 read_die_and_siblings (const struct die_reader_specs *reader,
11997 gdb_byte *info_ptr,
11998 gdb_byte **new_info_ptr,
11999 struct die_info *parent)
12000 {
12001 struct die_info *first_die, *last_sibling;
12002 gdb_byte *cur_ptr;
12003
12004 cur_ptr = info_ptr;
12005 first_die = last_sibling = NULL;
12006
12007 while (1)
12008 {
12009 struct die_info *die
12010 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12011
12012 if (die == NULL)
12013 {
12014 *new_info_ptr = cur_ptr;
12015 return first_die;
12016 }
12017
12018 if (!first_die)
12019 first_die = die;
12020 else
12021 last_sibling->sibling = die;
12022
12023 last_sibling = die;
12024 }
12025 }
12026
12027 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12028 attributes.
12029 The caller is responsible for filling in the extra attributes
12030 and updating (*DIEP)->num_attrs.
12031 Set DIEP to point to a newly allocated die with its information,
12032 except for its child, sibling, and parent fields.
12033 Set HAS_CHILDREN to tell whether the die has children or not. */
12034
12035 static gdb_byte *
12036 read_full_die_1 (const struct die_reader_specs *reader,
12037 struct die_info **diep, gdb_byte *info_ptr,
12038 int *has_children, int num_extra_attrs)
12039 {
12040 unsigned int abbrev_number, bytes_read, i;
12041 sect_offset offset;
12042 struct abbrev_info *abbrev;
12043 struct die_info *die;
12044 struct dwarf2_cu *cu = reader->cu;
12045 bfd *abfd = reader->abfd;
12046
12047 offset.sect_off = info_ptr - reader->buffer;
12048 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12049 info_ptr += bytes_read;
12050 if (!abbrev_number)
12051 {
12052 *diep = NULL;
12053 *has_children = 0;
12054 return info_ptr;
12055 }
12056
12057 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12058 if (!abbrev)
12059 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12060 abbrev_number,
12061 bfd_get_filename (abfd));
12062
12063 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12064 die->offset = offset;
12065 die->tag = abbrev->tag;
12066 die->abbrev = abbrev_number;
12067
12068 /* Make the result usable.
12069 The caller needs to update num_attrs after adding the extra
12070 attributes. */
12071 die->num_attrs = abbrev->num_attrs;
12072
12073 for (i = 0; i < abbrev->num_attrs; ++i)
12074 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12075 info_ptr);
12076
12077 *diep = die;
12078 *has_children = abbrev->has_children;
12079 return info_ptr;
12080 }
12081
12082 /* Read a die and all its attributes.
12083 Set DIEP to point to a newly allocated die with its information,
12084 except for its child, sibling, and parent fields.
12085 Set HAS_CHILDREN to tell whether the die has children or not. */
12086
12087 static gdb_byte *
12088 read_full_die (const struct die_reader_specs *reader,
12089 struct die_info **diep, gdb_byte *info_ptr,
12090 int *has_children)
12091 {
12092 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12093 }
12094 \f
12095 /* Abbreviation tables.
12096
12097 In DWARF version 2, the description of the debugging information is
12098 stored in a separate .debug_abbrev section. Before we read any
12099 dies from a section we read in all abbreviations and install them
12100 in a hash table. */
12101
12102 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12103
12104 static struct abbrev_info *
12105 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12106 {
12107 struct abbrev_info *abbrev;
12108
12109 abbrev = (struct abbrev_info *)
12110 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12111 memset (abbrev, 0, sizeof (struct abbrev_info));
12112 return abbrev;
12113 }
12114
12115 /* Add an abbreviation to the table. */
12116
12117 static void
12118 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12119 unsigned int abbrev_number,
12120 struct abbrev_info *abbrev)
12121 {
12122 unsigned int hash_number;
12123
12124 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12125 abbrev->next = abbrev_table->abbrevs[hash_number];
12126 abbrev_table->abbrevs[hash_number] = abbrev;
12127 }
12128
12129 /* Look up an abbrev in the table.
12130 Returns NULL if the abbrev is not found. */
12131
12132 static struct abbrev_info *
12133 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12134 unsigned int abbrev_number)
12135 {
12136 unsigned int hash_number;
12137 struct abbrev_info *abbrev;
12138
12139 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12140 abbrev = abbrev_table->abbrevs[hash_number];
12141
12142 while (abbrev)
12143 {
12144 if (abbrev->number == abbrev_number)
12145 return abbrev;
12146 abbrev = abbrev->next;
12147 }
12148 return NULL;
12149 }
12150
12151 /* Read in an abbrev table. */
12152
12153 static struct abbrev_table *
12154 abbrev_table_read_table (struct dwarf2_section_info *section,
12155 sect_offset offset)
12156 {
12157 struct objfile *objfile = dwarf2_per_objfile->objfile;
12158 bfd *abfd = section->asection->owner;
12159 struct abbrev_table *abbrev_table;
12160 gdb_byte *abbrev_ptr;
12161 struct abbrev_info *cur_abbrev;
12162 unsigned int abbrev_number, bytes_read, abbrev_name;
12163 unsigned int abbrev_form;
12164 struct attr_abbrev *cur_attrs;
12165 unsigned int allocated_attrs;
12166
12167 abbrev_table = XMALLOC (struct abbrev_table);
12168 abbrev_table->offset = offset;
12169 obstack_init (&abbrev_table->abbrev_obstack);
12170 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12171 (ABBREV_HASH_SIZE
12172 * sizeof (struct abbrev_info *)));
12173 memset (abbrev_table->abbrevs, 0,
12174 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12175
12176 dwarf2_read_section (objfile, section);
12177 abbrev_ptr = section->buffer + offset.sect_off;
12178 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12179 abbrev_ptr += bytes_read;
12180
12181 allocated_attrs = ATTR_ALLOC_CHUNK;
12182 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12183
12184 /* Loop until we reach an abbrev number of 0. */
12185 while (abbrev_number)
12186 {
12187 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12188
12189 /* read in abbrev header */
12190 cur_abbrev->number = abbrev_number;
12191 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12192 abbrev_ptr += bytes_read;
12193 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12194 abbrev_ptr += 1;
12195
12196 /* now read in declarations */
12197 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12198 abbrev_ptr += bytes_read;
12199 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12200 abbrev_ptr += bytes_read;
12201 while (abbrev_name)
12202 {
12203 if (cur_abbrev->num_attrs == allocated_attrs)
12204 {
12205 allocated_attrs += ATTR_ALLOC_CHUNK;
12206 cur_attrs
12207 = xrealloc (cur_attrs, (allocated_attrs
12208 * sizeof (struct attr_abbrev)));
12209 }
12210
12211 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12212 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12213 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12214 abbrev_ptr += bytes_read;
12215 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12216 abbrev_ptr += bytes_read;
12217 }
12218
12219 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12220 (cur_abbrev->num_attrs
12221 * sizeof (struct attr_abbrev)));
12222 memcpy (cur_abbrev->attrs, cur_attrs,
12223 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12224
12225 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12226
12227 /* Get next abbreviation.
12228 Under Irix6 the abbreviations for a compilation unit are not
12229 always properly terminated with an abbrev number of 0.
12230 Exit loop if we encounter an abbreviation which we have
12231 already read (which means we are about to read the abbreviations
12232 for the next compile unit) or if the end of the abbreviation
12233 table is reached. */
12234 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12235 break;
12236 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12237 abbrev_ptr += bytes_read;
12238 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12239 break;
12240 }
12241
12242 xfree (cur_attrs);
12243 return abbrev_table;
12244 }
12245
12246 /* Free the resources held by ABBREV_TABLE. */
12247
12248 static void
12249 abbrev_table_free (struct abbrev_table *abbrev_table)
12250 {
12251 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12252 xfree (abbrev_table);
12253 }
12254
12255 /* Same as abbrev_table_free but as a cleanup.
12256 We pass in a pointer to the pointer to the table so that we can
12257 set the pointer to NULL when we're done. It also simplifies
12258 build_type_unit_groups. */
12259
12260 static void
12261 abbrev_table_free_cleanup (void *table_ptr)
12262 {
12263 struct abbrev_table **abbrev_table_ptr = table_ptr;
12264
12265 if (*abbrev_table_ptr != NULL)
12266 abbrev_table_free (*abbrev_table_ptr);
12267 *abbrev_table_ptr = NULL;
12268 }
12269
12270 /* Read the abbrev table for CU from ABBREV_SECTION. */
12271
12272 static void
12273 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12274 struct dwarf2_section_info *abbrev_section)
12275 {
12276 cu->abbrev_table =
12277 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12278 }
12279
12280 /* Release the memory used by the abbrev table for a compilation unit. */
12281
12282 static void
12283 dwarf2_free_abbrev_table (void *ptr_to_cu)
12284 {
12285 struct dwarf2_cu *cu = ptr_to_cu;
12286
12287 abbrev_table_free (cu->abbrev_table);
12288 /* Set this to NULL so that we SEGV if we try to read it later,
12289 and also because free_comp_unit verifies this is NULL. */
12290 cu->abbrev_table = NULL;
12291 }
12292 \f
12293 /* Returns nonzero if TAG represents a type that we might generate a partial
12294 symbol for. */
12295
12296 static int
12297 is_type_tag_for_partial (int tag)
12298 {
12299 switch (tag)
12300 {
12301 #if 0
12302 /* Some types that would be reasonable to generate partial symbols for,
12303 that we don't at present. */
12304 case DW_TAG_array_type:
12305 case DW_TAG_file_type:
12306 case DW_TAG_ptr_to_member_type:
12307 case DW_TAG_set_type:
12308 case DW_TAG_string_type:
12309 case DW_TAG_subroutine_type:
12310 #endif
12311 case DW_TAG_base_type:
12312 case DW_TAG_class_type:
12313 case DW_TAG_interface_type:
12314 case DW_TAG_enumeration_type:
12315 case DW_TAG_structure_type:
12316 case DW_TAG_subrange_type:
12317 case DW_TAG_typedef:
12318 case DW_TAG_union_type:
12319 return 1;
12320 default:
12321 return 0;
12322 }
12323 }
12324
12325 /* Load all DIEs that are interesting for partial symbols into memory. */
12326
12327 static struct partial_die_info *
12328 load_partial_dies (const struct die_reader_specs *reader,
12329 gdb_byte *info_ptr, int building_psymtab)
12330 {
12331 struct dwarf2_cu *cu = reader->cu;
12332 struct objfile *objfile = cu->objfile;
12333 struct partial_die_info *part_die;
12334 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12335 struct abbrev_info *abbrev;
12336 unsigned int bytes_read;
12337 unsigned int load_all = 0;
12338 int nesting_level = 1;
12339
12340 parent_die = NULL;
12341 last_die = NULL;
12342
12343 gdb_assert (cu->per_cu != NULL);
12344 if (cu->per_cu->load_all_dies)
12345 load_all = 1;
12346
12347 cu->partial_dies
12348 = htab_create_alloc_ex (cu->header.length / 12,
12349 partial_die_hash,
12350 partial_die_eq,
12351 NULL,
12352 &cu->comp_unit_obstack,
12353 hashtab_obstack_allocate,
12354 dummy_obstack_deallocate);
12355
12356 part_die = obstack_alloc (&cu->comp_unit_obstack,
12357 sizeof (struct partial_die_info));
12358
12359 while (1)
12360 {
12361 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12362
12363 /* A NULL abbrev means the end of a series of children. */
12364 if (abbrev == NULL)
12365 {
12366 if (--nesting_level == 0)
12367 {
12368 /* PART_DIE was probably the last thing allocated on the
12369 comp_unit_obstack, so we could call obstack_free
12370 here. We don't do that because the waste is small,
12371 and will be cleaned up when we're done with this
12372 compilation unit. This way, we're also more robust
12373 against other users of the comp_unit_obstack. */
12374 return first_die;
12375 }
12376 info_ptr += bytes_read;
12377 last_die = parent_die;
12378 parent_die = parent_die->die_parent;
12379 continue;
12380 }
12381
12382 /* Check for template arguments. We never save these; if
12383 they're seen, we just mark the parent, and go on our way. */
12384 if (parent_die != NULL
12385 && cu->language == language_cplus
12386 && (abbrev->tag == DW_TAG_template_type_param
12387 || abbrev->tag == DW_TAG_template_value_param))
12388 {
12389 parent_die->has_template_arguments = 1;
12390
12391 if (!load_all)
12392 {
12393 /* We don't need a partial DIE for the template argument. */
12394 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12395 continue;
12396 }
12397 }
12398
12399 /* We only recurse into c++ subprograms looking for template arguments.
12400 Skip their other children. */
12401 if (!load_all
12402 && cu->language == language_cplus
12403 && parent_die != NULL
12404 && parent_die->tag == DW_TAG_subprogram)
12405 {
12406 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12407 continue;
12408 }
12409
12410 /* Check whether this DIE is interesting enough to save. Normally
12411 we would not be interested in members here, but there may be
12412 later variables referencing them via DW_AT_specification (for
12413 static members). */
12414 if (!load_all
12415 && !is_type_tag_for_partial (abbrev->tag)
12416 && abbrev->tag != DW_TAG_constant
12417 && abbrev->tag != DW_TAG_enumerator
12418 && abbrev->tag != DW_TAG_subprogram
12419 && abbrev->tag != DW_TAG_lexical_block
12420 && abbrev->tag != DW_TAG_variable
12421 && abbrev->tag != DW_TAG_namespace
12422 && abbrev->tag != DW_TAG_module
12423 && abbrev->tag != DW_TAG_member
12424 && abbrev->tag != DW_TAG_imported_unit)
12425 {
12426 /* Otherwise we skip to the next sibling, if any. */
12427 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12428 continue;
12429 }
12430
12431 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12432 info_ptr);
12433
12434 /* This two-pass algorithm for processing partial symbols has a
12435 high cost in cache pressure. Thus, handle some simple cases
12436 here which cover the majority of C partial symbols. DIEs
12437 which neither have specification tags in them, nor could have
12438 specification tags elsewhere pointing at them, can simply be
12439 processed and discarded.
12440
12441 This segment is also optional; scan_partial_symbols and
12442 add_partial_symbol will handle these DIEs if we chain
12443 them in normally. When compilers which do not emit large
12444 quantities of duplicate debug information are more common,
12445 this code can probably be removed. */
12446
12447 /* Any complete simple types at the top level (pretty much all
12448 of them, for a language without namespaces), can be processed
12449 directly. */
12450 if (parent_die == NULL
12451 && part_die->has_specification == 0
12452 && part_die->is_declaration == 0
12453 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12454 || part_die->tag == DW_TAG_base_type
12455 || part_die->tag == DW_TAG_subrange_type))
12456 {
12457 if (building_psymtab && part_die->name != NULL)
12458 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12459 VAR_DOMAIN, LOC_TYPEDEF,
12460 &objfile->static_psymbols,
12461 0, (CORE_ADDR) 0, cu->language, objfile);
12462 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12463 continue;
12464 }
12465
12466 /* The exception for DW_TAG_typedef with has_children above is
12467 a workaround of GCC PR debug/47510. In the case of this complaint
12468 type_name_no_tag_or_error will error on such types later.
12469
12470 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12471 it could not find the child DIEs referenced later, this is checked
12472 above. In correct DWARF DW_TAG_typedef should have no children. */
12473
12474 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12475 complaint (&symfile_complaints,
12476 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12477 "- DIE at 0x%x [in module %s]"),
12478 part_die->offset.sect_off, objfile->name);
12479
12480 /* If we're at the second level, and we're an enumerator, and
12481 our parent has no specification (meaning possibly lives in a
12482 namespace elsewhere), then we can add the partial symbol now
12483 instead of queueing it. */
12484 if (part_die->tag == DW_TAG_enumerator
12485 && parent_die != NULL
12486 && parent_die->die_parent == NULL
12487 && parent_die->tag == DW_TAG_enumeration_type
12488 && parent_die->has_specification == 0)
12489 {
12490 if (part_die->name == NULL)
12491 complaint (&symfile_complaints,
12492 _("malformed enumerator DIE ignored"));
12493 else if (building_psymtab)
12494 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12495 VAR_DOMAIN, LOC_CONST,
12496 (cu->language == language_cplus
12497 || cu->language == language_java)
12498 ? &objfile->global_psymbols
12499 : &objfile->static_psymbols,
12500 0, (CORE_ADDR) 0, cu->language, objfile);
12501
12502 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12503 continue;
12504 }
12505
12506 /* We'll save this DIE so link it in. */
12507 part_die->die_parent = parent_die;
12508 part_die->die_sibling = NULL;
12509 part_die->die_child = NULL;
12510
12511 if (last_die && last_die == parent_die)
12512 last_die->die_child = part_die;
12513 else if (last_die)
12514 last_die->die_sibling = part_die;
12515
12516 last_die = part_die;
12517
12518 if (first_die == NULL)
12519 first_die = part_die;
12520
12521 /* Maybe add the DIE to the hash table. Not all DIEs that we
12522 find interesting need to be in the hash table, because we
12523 also have the parent/sibling/child chains; only those that we
12524 might refer to by offset later during partial symbol reading.
12525
12526 For now this means things that might have be the target of a
12527 DW_AT_specification, DW_AT_abstract_origin, or
12528 DW_AT_extension. DW_AT_extension will refer only to
12529 namespaces; DW_AT_abstract_origin refers to functions (and
12530 many things under the function DIE, but we do not recurse
12531 into function DIEs during partial symbol reading) and
12532 possibly variables as well; DW_AT_specification refers to
12533 declarations. Declarations ought to have the DW_AT_declaration
12534 flag. It happens that GCC forgets to put it in sometimes, but
12535 only for functions, not for types.
12536
12537 Adding more things than necessary to the hash table is harmless
12538 except for the performance cost. Adding too few will result in
12539 wasted time in find_partial_die, when we reread the compilation
12540 unit with load_all_dies set. */
12541
12542 if (load_all
12543 || abbrev->tag == DW_TAG_constant
12544 || abbrev->tag == DW_TAG_subprogram
12545 || abbrev->tag == DW_TAG_variable
12546 || abbrev->tag == DW_TAG_namespace
12547 || part_die->is_declaration)
12548 {
12549 void **slot;
12550
12551 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12552 part_die->offset.sect_off, INSERT);
12553 *slot = part_die;
12554 }
12555
12556 part_die = obstack_alloc (&cu->comp_unit_obstack,
12557 sizeof (struct partial_die_info));
12558
12559 /* For some DIEs we want to follow their children (if any). For C
12560 we have no reason to follow the children of structures; for other
12561 languages we have to, so that we can get at method physnames
12562 to infer fully qualified class names, for DW_AT_specification,
12563 and for C++ template arguments. For C++, we also look one level
12564 inside functions to find template arguments (if the name of the
12565 function does not already contain the template arguments).
12566
12567 For Ada, we need to scan the children of subprograms and lexical
12568 blocks as well because Ada allows the definition of nested
12569 entities that could be interesting for the debugger, such as
12570 nested subprograms for instance. */
12571 if (last_die->has_children
12572 && (load_all
12573 || last_die->tag == DW_TAG_namespace
12574 || last_die->tag == DW_TAG_module
12575 || last_die->tag == DW_TAG_enumeration_type
12576 || (cu->language == language_cplus
12577 && last_die->tag == DW_TAG_subprogram
12578 && (last_die->name == NULL
12579 || strchr (last_die->name, '<') == NULL))
12580 || (cu->language != language_c
12581 && (last_die->tag == DW_TAG_class_type
12582 || last_die->tag == DW_TAG_interface_type
12583 || last_die->tag == DW_TAG_structure_type
12584 || last_die->tag == DW_TAG_union_type))
12585 || (cu->language == language_ada
12586 && (last_die->tag == DW_TAG_subprogram
12587 || last_die->tag == DW_TAG_lexical_block))))
12588 {
12589 nesting_level++;
12590 parent_die = last_die;
12591 continue;
12592 }
12593
12594 /* Otherwise we skip to the next sibling, if any. */
12595 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12596
12597 /* Back to the top, do it again. */
12598 }
12599 }
12600
12601 /* Read a minimal amount of information into the minimal die structure. */
12602
12603 static gdb_byte *
12604 read_partial_die (const struct die_reader_specs *reader,
12605 struct partial_die_info *part_die,
12606 struct abbrev_info *abbrev, unsigned int abbrev_len,
12607 gdb_byte *info_ptr)
12608 {
12609 struct dwarf2_cu *cu = reader->cu;
12610 struct objfile *objfile = cu->objfile;
12611 gdb_byte *buffer = reader->buffer;
12612 unsigned int i;
12613 struct attribute attr;
12614 int has_low_pc_attr = 0;
12615 int has_high_pc_attr = 0;
12616 int high_pc_relative = 0;
12617
12618 memset (part_die, 0, sizeof (struct partial_die_info));
12619
12620 part_die->offset.sect_off = info_ptr - buffer;
12621
12622 info_ptr += abbrev_len;
12623
12624 if (abbrev == NULL)
12625 return info_ptr;
12626
12627 part_die->tag = abbrev->tag;
12628 part_die->has_children = abbrev->has_children;
12629
12630 for (i = 0; i < abbrev->num_attrs; ++i)
12631 {
12632 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12633
12634 /* Store the data if it is of an attribute we want to keep in a
12635 partial symbol table. */
12636 switch (attr.name)
12637 {
12638 case DW_AT_name:
12639 switch (part_die->tag)
12640 {
12641 case DW_TAG_compile_unit:
12642 case DW_TAG_partial_unit:
12643 case DW_TAG_type_unit:
12644 /* Compilation units have a DW_AT_name that is a filename, not
12645 a source language identifier. */
12646 case DW_TAG_enumeration_type:
12647 case DW_TAG_enumerator:
12648 /* These tags always have simple identifiers already; no need
12649 to canonicalize them. */
12650 part_die->name = DW_STRING (&attr);
12651 break;
12652 default:
12653 part_die->name
12654 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12655 &objfile->objfile_obstack);
12656 break;
12657 }
12658 break;
12659 case DW_AT_linkage_name:
12660 case DW_AT_MIPS_linkage_name:
12661 /* Note that both forms of linkage name might appear. We
12662 assume they will be the same, and we only store the last
12663 one we see. */
12664 if (cu->language == language_ada)
12665 part_die->name = DW_STRING (&attr);
12666 part_die->linkage_name = DW_STRING (&attr);
12667 break;
12668 case DW_AT_low_pc:
12669 has_low_pc_attr = 1;
12670 part_die->lowpc = DW_ADDR (&attr);
12671 break;
12672 case DW_AT_high_pc:
12673 has_high_pc_attr = 1;
12674 if (attr.form == DW_FORM_addr
12675 || attr.form == DW_FORM_GNU_addr_index)
12676 part_die->highpc = DW_ADDR (&attr);
12677 else
12678 {
12679 high_pc_relative = 1;
12680 part_die->highpc = DW_UNSND (&attr);
12681 }
12682 break;
12683 case DW_AT_location:
12684 /* Support the .debug_loc offsets. */
12685 if (attr_form_is_block (&attr))
12686 {
12687 part_die->d.locdesc = DW_BLOCK (&attr);
12688 }
12689 else if (attr_form_is_section_offset (&attr))
12690 {
12691 dwarf2_complex_location_expr_complaint ();
12692 }
12693 else
12694 {
12695 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12696 "partial symbol information");
12697 }
12698 break;
12699 case DW_AT_external:
12700 part_die->is_external = DW_UNSND (&attr);
12701 break;
12702 case DW_AT_declaration:
12703 part_die->is_declaration = DW_UNSND (&attr);
12704 break;
12705 case DW_AT_type:
12706 part_die->has_type = 1;
12707 break;
12708 case DW_AT_abstract_origin:
12709 case DW_AT_specification:
12710 case DW_AT_extension:
12711 part_die->has_specification = 1;
12712 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12713 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12714 || cu->per_cu->is_dwz);
12715 break;
12716 case DW_AT_sibling:
12717 /* Ignore absolute siblings, they might point outside of
12718 the current compile unit. */
12719 if (attr.form == DW_FORM_ref_addr)
12720 complaint (&symfile_complaints,
12721 _("ignoring absolute DW_AT_sibling"));
12722 else
12723 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12724 break;
12725 case DW_AT_byte_size:
12726 part_die->has_byte_size = 1;
12727 break;
12728 case DW_AT_calling_convention:
12729 /* DWARF doesn't provide a way to identify a program's source-level
12730 entry point. DW_AT_calling_convention attributes are only meant
12731 to describe functions' calling conventions.
12732
12733 However, because it's a necessary piece of information in
12734 Fortran, and because DW_CC_program is the only piece of debugging
12735 information whose definition refers to a 'main program' at all,
12736 several compilers have begun marking Fortran main programs with
12737 DW_CC_program --- even when those functions use the standard
12738 calling conventions.
12739
12740 So until DWARF specifies a way to provide this information and
12741 compilers pick up the new representation, we'll support this
12742 practice. */
12743 if (DW_UNSND (&attr) == DW_CC_program
12744 && cu->language == language_fortran)
12745 {
12746 set_main_name (part_die->name);
12747
12748 /* As this DIE has a static linkage the name would be difficult
12749 to look up later. */
12750 language_of_main = language_fortran;
12751 }
12752 break;
12753 case DW_AT_inline:
12754 if (DW_UNSND (&attr) == DW_INL_inlined
12755 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12756 part_die->may_be_inlined = 1;
12757 break;
12758
12759 case DW_AT_import:
12760 if (part_die->tag == DW_TAG_imported_unit)
12761 {
12762 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12763 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12764 || cu->per_cu->is_dwz);
12765 }
12766 break;
12767
12768 default:
12769 break;
12770 }
12771 }
12772
12773 if (high_pc_relative)
12774 part_die->highpc += part_die->lowpc;
12775
12776 if (has_low_pc_attr && has_high_pc_attr)
12777 {
12778 /* When using the GNU linker, .gnu.linkonce. sections are used to
12779 eliminate duplicate copies of functions and vtables and such.
12780 The linker will arbitrarily choose one and discard the others.
12781 The AT_*_pc values for such functions refer to local labels in
12782 these sections. If the section from that file was discarded, the
12783 labels are not in the output, so the relocs get a value of 0.
12784 If this is a discarded function, mark the pc bounds as invalid,
12785 so that GDB will ignore it. */
12786 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12787 {
12788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12789
12790 complaint (&symfile_complaints,
12791 _("DW_AT_low_pc %s is zero "
12792 "for DIE at 0x%x [in module %s]"),
12793 paddress (gdbarch, part_die->lowpc),
12794 part_die->offset.sect_off, objfile->name);
12795 }
12796 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12797 else if (part_die->lowpc >= part_die->highpc)
12798 {
12799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12800
12801 complaint (&symfile_complaints,
12802 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12803 "for DIE at 0x%x [in module %s]"),
12804 paddress (gdbarch, part_die->lowpc),
12805 paddress (gdbarch, part_die->highpc),
12806 part_die->offset.sect_off, objfile->name);
12807 }
12808 else
12809 part_die->has_pc_info = 1;
12810 }
12811
12812 return info_ptr;
12813 }
12814
12815 /* Find a cached partial DIE at OFFSET in CU. */
12816
12817 static struct partial_die_info *
12818 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12819 {
12820 struct partial_die_info *lookup_die = NULL;
12821 struct partial_die_info part_die;
12822
12823 part_die.offset = offset;
12824 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12825 offset.sect_off);
12826
12827 return lookup_die;
12828 }
12829
12830 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12831 except in the case of .debug_types DIEs which do not reference
12832 outside their CU (they do however referencing other types via
12833 DW_FORM_ref_sig8). */
12834
12835 static struct partial_die_info *
12836 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
12837 {
12838 struct objfile *objfile = cu->objfile;
12839 struct dwarf2_per_cu_data *per_cu = NULL;
12840 struct partial_die_info *pd = NULL;
12841
12842 if (offset_in_dwz == cu->per_cu->is_dwz
12843 && offset_in_cu_p (&cu->header, offset))
12844 {
12845 pd = find_partial_die_in_comp_unit (offset, cu);
12846 if (pd != NULL)
12847 return pd;
12848 /* We missed recording what we needed.
12849 Load all dies and try again. */
12850 per_cu = cu->per_cu;
12851 }
12852 else
12853 {
12854 /* TUs don't reference other CUs/TUs (except via type signatures). */
12855 if (cu->per_cu->is_debug_types)
12856 {
12857 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12858 " external reference to offset 0x%lx [in module %s].\n"),
12859 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12860 bfd_get_filename (objfile->obfd));
12861 }
12862 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12863 objfile);
12864
12865 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12866 load_partial_comp_unit (per_cu);
12867
12868 per_cu->cu->last_used = 0;
12869 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12870 }
12871
12872 /* If we didn't find it, and not all dies have been loaded,
12873 load them all and try again. */
12874
12875 if (pd == NULL && per_cu->load_all_dies == 0)
12876 {
12877 per_cu->load_all_dies = 1;
12878
12879 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12880 THIS_CU->cu may already be in use. So we can't just free it and
12881 replace its DIEs with the ones we read in. Instead, we leave those
12882 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12883 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12884 set. */
12885 load_partial_comp_unit (per_cu);
12886
12887 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12888 }
12889
12890 if (pd == NULL)
12891 internal_error (__FILE__, __LINE__,
12892 _("could not find partial DIE 0x%x "
12893 "in cache [from module %s]\n"),
12894 offset.sect_off, bfd_get_filename (objfile->obfd));
12895 return pd;
12896 }
12897
12898 /* See if we can figure out if the class lives in a namespace. We do
12899 this by looking for a member function; its demangled name will
12900 contain namespace info, if there is any. */
12901
12902 static void
12903 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12904 struct dwarf2_cu *cu)
12905 {
12906 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12907 what template types look like, because the demangler
12908 frequently doesn't give the same name as the debug info. We
12909 could fix this by only using the demangled name to get the
12910 prefix (but see comment in read_structure_type). */
12911
12912 struct partial_die_info *real_pdi;
12913 struct partial_die_info *child_pdi;
12914
12915 /* If this DIE (this DIE's specification, if any) has a parent, then
12916 we should not do this. We'll prepend the parent's fully qualified
12917 name when we create the partial symbol. */
12918
12919 real_pdi = struct_pdi;
12920 while (real_pdi->has_specification)
12921 real_pdi = find_partial_die (real_pdi->spec_offset,
12922 real_pdi->spec_is_dwz, cu);
12923
12924 if (real_pdi->die_parent != NULL)
12925 return;
12926
12927 for (child_pdi = struct_pdi->die_child;
12928 child_pdi != NULL;
12929 child_pdi = child_pdi->die_sibling)
12930 {
12931 if (child_pdi->tag == DW_TAG_subprogram
12932 && child_pdi->linkage_name != NULL)
12933 {
12934 char *actual_class_name
12935 = language_class_name_from_physname (cu->language_defn,
12936 child_pdi->linkage_name);
12937 if (actual_class_name != NULL)
12938 {
12939 struct_pdi->name
12940 = obsavestring (actual_class_name,
12941 strlen (actual_class_name),
12942 &cu->objfile->objfile_obstack);
12943 xfree (actual_class_name);
12944 }
12945 break;
12946 }
12947 }
12948 }
12949
12950 /* Adjust PART_DIE before generating a symbol for it. This function
12951 may set the is_external flag or change the DIE's name. */
12952
12953 static void
12954 fixup_partial_die (struct partial_die_info *part_die,
12955 struct dwarf2_cu *cu)
12956 {
12957 /* Once we've fixed up a die, there's no point in doing so again.
12958 This also avoids a memory leak if we were to call
12959 guess_partial_die_structure_name multiple times. */
12960 if (part_die->fixup_called)
12961 return;
12962
12963 /* If we found a reference attribute and the DIE has no name, try
12964 to find a name in the referred to DIE. */
12965
12966 if (part_die->name == NULL && part_die->has_specification)
12967 {
12968 struct partial_die_info *spec_die;
12969
12970 spec_die = find_partial_die (part_die->spec_offset,
12971 part_die->spec_is_dwz, cu);
12972
12973 fixup_partial_die (spec_die, cu);
12974
12975 if (spec_die->name)
12976 {
12977 part_die->name = spec_die->name;
12978
12979 /* Copy DW_AT_external attribute if it is set. */
12980 if (spec_die->is_external)
12981 part_die->is_external = spec_die->is_external;
12982 }
12983 }
12984
12985 /* Set default names for some unnamed DIEs. */
12986
12987 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12988 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12989
12990 /* If there is no parent die to provide a namespace, and there are
12991 children, see if we can determine the namespace from their linkage
12992 name. */
12993 if (cu->language == language_cplus
12994 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12995 && part_die->die_parent == NULL
12996 && part_die->has_children
12997 && (part_die->tag == DW_TAG_class_type
12998 || part_die->tag == DW_TAG_structure_type
12999 || part_die->tag == DW_TAG_union_type))
13000 guess_partial_die_structure_name (part_die, cu);
13001
13002 /* GCC might emit a nameless struct or union that has a linkage
13003 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13004 if (part_die->name == NULL
13005 && (part_die->tag == DW_TAG_class_type
13006 || part_die->tag == DW_TAG_interface_type
13007 || part_die->tag == DW_TAG_structure_type
13008 || part_die->tag == DW_TAG_union_type)
13009 && part_die->linkage_name != NULL)
13010 {
13011 char *demangled;
13012
13013 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13014 if (demangled)
13015 {
13016 const char *base;
13017
13018 /* Strip any leading namespaces/classes, keep only the base name.
13019 DW_AT_name for named DIEs does not contain the prefixes. */
13020 base = strrchr (demangled, ':');
13021 if (base && base > demangled && base[-1] == ':')
13022 base++;
13023 else
13024 base = demangled;
13025
13026 part_die->name = obsavestring (base, strlen (base),
13027 &cu->objfile->objfile_obstack);
13028 xfree (demangled);
13029 }
13030 }
13031
13032 part_die->fixup_called = 1;
13033 }
13034
13035 /* Read an attribute value described by an attribute form. */
13036
13037 static gdb_byte *
13038 read_attribute_value (const struct die_reader_specs *reader,
13039 struct attribute *attr, unsigned form,
13040 gdb_byte *info_ptr)
13041 {
13042 struct dwarf2_cu *cu = reader->cu;
13043 bfd *abfd = reader->abfd;
13044 struct comp_unit_head *cu_header = &cu->header;
13045 unsigned int bytes_read;
13046 struct dwarf_block *blk;
13047
13048 attr->form = form;
13049 switch (form)
13050 {
13051 case DW_FORM_ref_addr:
13052 if (cu->header.version == 2)
13053 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13054 else
13055 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13056 &cu->header, &bytes_read);
13057 info_ptr += bytes_read;
13058 break;
13059 case DW_FORM_GNU_ref_alt:
13060 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13061 info_ptr += bytes_read;
13062 break;
13063 case DW_FORM_addr:
13064 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13065 info_ptr += bytes_read;
13066 break;
13067 case DW_FORM_block2:
13068 blk = dwarf_alloc_block (cu);
13069 blk->size = read_2_bytes (abfd, info_ptr);
13070 info_ptr += 2;
13071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13072 info_ptr += blk->size;
13073 DW_BLOCK (attr) = blk;
13074 break;
13075 case DW_FORM_block4:
13076 blk = dwarf_alloc_block (cu);
13077 blk->size = read_4_bytes (abfd, info_ptr);
13078 info_ptr += 4;
13079 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13080 info_ptr += blk->size;
13081 DW_BLOCK (attr) = blk;
13082 break;
13083 case DW_FORM_data2:
13084 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13085 info_ptr += 2;
13086 break;
13087 case DW_FORM_data4:
13088 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13089 info_ptr += 4;
13090 break;
13091 case DW_FORM_data8:
13092 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13093 info_ptr += 8;
13094 break;
13095 case DW_FORM_sec_offset:
13096 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13097 info_ptr += bytes_read;
13098 break;
13099 case DW_FORM_string:
13100 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13101 DW_STRING_IS_CANONICAL (attr) = 0;
13102 info_ptr += bytes_read;
13103 break;
13104 case DW_FORM_strp:
13105 if (!cu->per_cu->is_dwz)
13106 {
13107 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13108 &bytes_read);
13109 DW_STRING_IS_CANONICAL (attr) = 0;
13110 info_ptr += bytes_read;
13111 break;
13112 }
13113 /* FALLTHROUGH */
13114 case DW_FORM_GNU_strp_alt:
13115 {
13116 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13117 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13118 &bytes_read);
13119
13120 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13121 DW_STRING_IS_CANONICAL (attr) = 0;
13122 info_ptr += bytes_read;
13123 }
13124 break;
13125 case DW_FORM_exprloc:
13126 case DW_FORM_block:
13127 blk = dwarf_alloc_block (cu);
13128 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13129 info_ptr += bytes_read;
13130 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13131 info_ptr += blk->size;
13132 DW_BLOCK (attr) = blk;
13133 break;
13134 case DW_FORM_block1:
13135 blk = dwarf_alloc_block (cu);
13136 blk->size = read_1_byte (abfd, info_ptr);
13137 info_ptr += 1;
13138 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13139 info_ptr += blk->size;
13140 DW_BLOCK (attr) = blk;
13141 break;
13142 case DW_FORM_data1:
13143 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13144 info_ptr += 1;
13145 break;
13146 case DW_FORM_flag:
13147 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13148 info_ptr += 1;
13149 break;
13150 case DW_FORM_flag_present:
13151 DW_UNSND (attr) = 1;
13152 break;
13153 case DW_FORM_sdata:
13154 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13155 info_ptr += bytes_read;
13156 break;
13157 case DW_FORM_udata:
13158 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13159 info_ptr += bytes_read;
13160 break;
13161 case DW_FORM_ref1:
13162 DW_UNSND (attr) = (cu->header.offset.sect_off
13163 + read_1_byte (abfd, info_ptr));
13164 info_ptr += 1;
13165 break;
13166 case DW_FORM_ref2:
13167 DW_UNSND (attr) = (cu->header.offset.sect_off
13168 + read_2_bytes (abfd, info_ptr));
13169 info_ptr += 2;
13170 break;
13171 case DW_FORM_ref4:
13172 DW_UNSND (attr) = (cu->header.offset.sect_off
13173 + read_4_bytes (abfd, info_ptr));
13174 info_ptr += 4;
13175 break;
13176 case DW_FORM_ref8:
13177 DW_UNSND (attr) = (cu->header.offset.sect_off
13178 + read_8_bytes (abfd, info_ptr));
13179 info_ptr += 8;
13180 break;
13181 case DW_FORM_ref_sig8:
13182 /* Convert the signature to something we can record in DW_UNSND
13183 for later lookup.
13184 NOTE: This is NULL if the type wasn't found. */
13185 DW_SIGNATURED_TYPE (attr) =
13186 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
13187 info_ptr += 8;
13188 break;
13189 case DW_FORM_ref_udata:
13190 DW_UNSND (attr) = (cu->header.offset.sect_off
13191 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
13192 info_ptr += bytes_read;
13193 break;
13194 case DW_FORM_indirect:
13195 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13196 info_ptr += bytes_read;
13197 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
13198 break;
13199 case DW_FORM_GNU_addr_index:
13200 if (reader->dwo_file == NULL)
13201 {
13202 /* For now flag a hard error.
13203 Later we can turn this into a complaint. */
13204 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13205 dwarf_form_name (form),
13206 bfd_get_filename (abfd));
13207 }
13208 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13209 info_ptr += bytes_read;
13210 break;
13211 case DW_FORM_GNU_str_index:
13212 if (reader->dwo_file == NULL)
13213 {
13214 /* For now flag a hard error.
13215 Later we can turn this into a complaint if warranted. */
13216 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13217 dwarf_form_name (form),
13218 bfd_get_filename (abfd));
13219 }
13220 {
13221 ULONGEST str_index =
13222 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13223
13224 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13225 DW_STRING_IS_CANONICAL (attr) = 0;
13226 info_ptr += bytes_read;
13227 }
13228 break;
13229 default:
13230 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13231 dwarf_form_name (form),
13232 bfd_get_filename (abfd));
13233 }
13234
13235 /* Super hack. */
13236 if (cu->per_cu->is_dwz && is_ref_attr (attr))
13237 attr->form = DW_FORM_GNU_ref_alt;
13238
13239 /* We have seen instances where the compiler tried to emit a byte
13240 size attribute of -1 which ended up being encoded as an unsigned
13241 0xffffffff. Although 0xffffffff is technically a valid size value,
13242 an object of this size seems pretty unlikely so we can relatively
13243 safely treat these cases as if the size attribute was invalid and
13244 treat them as zero by default. */
13245 if (attr->name == DW_AT_byte_size
13246 && form == DW_FORM_data4
13247 && DW_UNSND (attr) >= 0xffffffff)
13248 {
13249 complaint
13250 (&symfile_complaints,
13251 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13252 hex_string (DW_UNSND (attr)));
13253 DW_UNSND (attr) = 0;
13254 }
13255
13256 return info_ptr;
13257 }
13258
13259 /* Read an attribute described by an abbreviated attribute. */
13260
13261 static gdb_byte *
13262 read_attribute (const struct die_reader_specs *reader,
13263 struct attribute *attr, struct attr_abbrev *abbrev,
13264 gdb_byte *info_ptr)
13265 {
13266 attr->name = abbrev->name;
13267 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13268 }
13269
13270 /* Read dwarf information from a buffer. */
13271
13272 static unsigned int
13273 read_1_byte (bfd *abfd, gdb_byte *buf)
13274 {
13275 return bfd_get_8 (abfd, buf);
13276 }
13277
13278 static int
13279 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13280 {
13281 return bfd_get_signed_8 (abfd, buf);
13282 }
13283
13284 static unsigned int
13285 read_2_bytes (bfd *abfd, gdb_byte *buf)
13286 {
13287 return bfd_get_16 (abfd, buf);
13288 }
13289
13290 static int
13291 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13292 {
13293 return bfd_get_signed_16 (abfd, buf);
13294 }
13295
13296 static unsigned int
13297 read_4_bytes (bfd *abfd, gdb_byte *buf)
13298 {
13299 return bfd_get_32 (abfd, buf);
13300 }
13301
13302 static int
13303 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13304 {
13305 return bfd_get_signed_32 (abfd, buf);
13306 }
13307
13308 static ULONGEST
13309 read_8_bytes (bfd *abfd, gdb_byte *buf)
13310 {
13311 return bfd_get_64 (abfd, buf);
13312 }
13313
13314 static CORE_ADDR
13315 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13316 unsigned int *bytes_read)
13317 {
13318 struct comp_unit_head *cu_header = &cu->header;
13319 CORE_ADDR retval = 0;
13320
13321 if (cu_header->signed_addr_p)
13322 {
13323 switch (cu_header->addr_size)
13324 {
13325 case 2:
13326 retval = bfd_get_signed_16 (abfd, buf);
13327 break;
13328 case 4:
13329 retval = bfd_get_signed_32 (abfd, buf);
13330 break;
13331 case 8:
13332 retval = bfd_get_signed_64 (abfd, buf);
13333 break;
13334 default:
13335 internal_error (__FILE__, __LINE__,
13336 _("read_address: bad switch, signed [in module %s]"),
13337 bfd_get_filename (abfd));
13338 }
13339 }
13340 else
13341 {
13342 switch (cu_header->addr_size)
13343 {
13344 case 2:
13345 retval = bfd_get_16 (abfd, buf);
13346 break;
13347 case 4:
13348 retval = bfd_get_32 (abfd, buf);
13349 break;
13350 case 8:
13351 retval = bfd_get_64 (abfd, buf);
13352 break;
13353 default:
13354 internal_error (__FILE__, __LINE__,
13355 _("read_address: bad switch, "
13356 "unsigned [in module %s]"),
13357 bfd_get_filename (abfd));
13358 }
13359 }
13360
13361 *bytes_read = cu_header->addr_size;
13362 return retval;
13363 }
13364
13365 /* Read the initial length from a section. The (draft) DWARF 3
13366 specification allows the initial length to take up either 4 bytes
13367 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13368 bytes describe the length and all offsets will be 8 bytes in length
13369 instead of 4.
13370
13371 An older, non-standard 64-bit format is also handled by this
13372 function. The older format in question stores the initial length
13373 as an 8-byte quantity without an escape value. Lengths greater
13374 than 2^32 aren't very common which means that the initial 4 bytes
13375 is almost always zero. Since a length value of zero doesn't make
13376 sense for the 32-bit format, this initial zero can be considered to
13377 be an escape value which indicates the presence of the older 64-bit
13378 format. As written, the code can't detect (old format) lengths
13379 greater than 4GB. If it becomes necessary to handle lengths
13380 somewhat larger than 4GB, we could allow other small values (such
13381 as the non-sensical values of 1, 2, and 3) to also be used as
13382 escape values indicating the presence of the old format.
13383
13384 The value returned via bytes_read should be used to increment the
13385 relevant pointer after calling read_initial_length().
13386
13387 [ Note: read_initial_length() and read_offset() are based on the
13388 document entitled "DWARF Debugging Information Format", revision
13389 3, draft 8, dated November 19, 2001. This document was obtained
13390 from:
13391
13392 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13393
13394 This document is only a draft and is subject to change. (So beware.)
13395
13396 Details regarding the older, non-standard 64-bit format were
13397 determined empirically by examining 64-bit ELF files produced by
13398 the SGI toolchain on an IRIX 6.5 machine.
13399
13400 - Kevin, July 16, 2002
13401 ] */
13402
13403 static LONGEST
13404 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13405 {
13406 LONGEST length = bfd_get_32 (abfd, buf);
13407
13408 if (length == 0xffffffff)
13409 {
13410 length = bfd_get_64 (abfd, buf + 4);
13411 *bytes_read = 12;
13412 }
13413 else if (length == 0)
13414 {
13415 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
13416 length = bfd_get_64 (abfd, buf);
13417 *bytes_read = 8;
13418 }
13419 else
13420 {
13421 *bytes_read = 4;
13422 }
13423
13424 return length;
13425 }
13426
13427 /* Cover function for read_initial_length.
13428 Returns the length of the object at BUF, and stores the size of the
13429 initial length in *BYTES_READ and stores the size that offsets will be in
13430 *OFFSET_SIZE.
13431 If the initial length size is not equivalent to that specified in
13432 CU_HEADER then issue a complaint.
13433 This is useful when reading non-comp-unit headers. */
13434
13435 static LONGEST
13436 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13437 const struct comp_unit_head *cu_header,
13438 unsigned int *bytes_read,
13439 unsigned int *offset_size)
13440 {
13441 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13442
13443 gdb_assert (cu_header->initial_length_size == 4
13444 || cu_header->initial_length_size == 8
13445 || cu_header->initial_length_size == 12);
13446
13447 if (cu_header->initial_length_size != *bytes_read)
13448 complaint (&symfile_complaints,
13449 _("intermixed 32-bit and 64-bit DWARF sections"));
13450
13451 *offset_size = (*bytes_read == 4) ? 4 : 8;
13452 return length;
13453 }
13454
13455 /* Read an offset from the data stream. The size of the offset is
13456 given by cu_header->offset_size. */
13457
13458 static LONGEST
13459 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13460 unsigned int *bytes_read)
13461 {
13462 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13463
13464 *bytes_read = cu_header->offset_size;
13465 return offset;
13466 }
13467
13468 /* Read an offset from the data stream. */
13469
13470 static LONGEST
13471 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13472 {
13473 LONGEST retval = 0;
13474
13475 switch (offset_size)
13476 {
13477 case 4:
13478 retval = bfd_get_32 (abfd, buf);
13479 break;
13480 case 8:
13481 retval = bfd_get_64 (abfd, buf);
13482 break;
13483 default:
13484 internal_error (__FILE__, __LINE__,
13485 _("read_offset_1: bad switch [in module %s]"),
13486 bfd_get_filename (abfd));
13487 }
13488
13489 return retval;
13490 }
13491
13492 static gdb_byte *
13493 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13494 {
13495 /* If the size of a host char is 8 bits, we can return a pointer
13496 to the buffer, otherwise we have to copy the data to a buffer
13497 allocated on the temporary obstack. */
13498 gdb_assert (HOST_CHAR_BIT == 8);
13499 return buf;
13500 }
13501
13502 static char *
13503 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13504 {
13505 /* If the size of a host char is 8 bits, we can return a pointer
13506 to the string, otherwise we have to copy the string to a buffer
13507 allocated on the temporary obstack. */
13508 gdb_assert (HOST_CHAR_BIT == 8);
13509 if (*buf == '\0')
13510 {
13511 *bytes_read_ptr = 1;
13512 return NULL;
13513 }
13514 *bytes_read_ptr = strlen ((char *) buf) + 1;
13515 return (char *) buf;
13516 }
13517
13518 static char *
13519 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13520 {
13521 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13522 if (dwarf2_per_objfile->str.buffer == NULL)
13523 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13524 bfd_get_filename (abfd));
13525 if (str_offset >= dwarf2_per_objfile->str.size)
13526 error (_("DW_FORM_strp pointing outside of "
13527 ".debug_str section [in module %s]"),
13528 bfd_get_filename (abfd));
13529 gdb_assert (HOST_CHAR_BIT == 8);
13530 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13531 return NULL;
13532 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13533 }
13534
13535 /* Read a string at offset STR_OFFSET in the .debug_str section from
13536 the .dwz file DWZ. Throw an error if the offset is too large. If
13537 the string consists of a single NUL byte, return NULL; otherwise
13538 return a pointer to the string. */
13539
13540 static char *
13541 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13542 {
13543 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13544
13545 if (dwz->str.buffer == NULL)
13546 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13547 "section [in module %s]"),
13548 bfd_get_filename (dwz->dwz_bfd));
13549 if (str_offset >= dwz->str.size)
13550 error (_("DW_FORM_GNU_strp_alt pointing outside of "
13551 ".debug_str section [in module %s]"),
13552 bfd_get_filename (dwz->dwz_bfd));
13553 gdb_assert (HOST_CHAR_BIT == 8);
13554 if (dwz->str.buffer[str_offset] == '\0')
13555 return NULL;
13556 return (char *) (dwz->str.buffer + str_offset);
13557 }
13558
13559 static char *
13560 read_indirect_string (bfd *abfd, gdb_byte *buf,
13561 const struct comp_unit_head *cu_header,
13562 unsigned int *bytes_read_ptr)
13563 {
13564 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13565
13566 return read_indirect_string_at_offset (abfd, str_offset);
13567 }
13568
13569 static ULONGEST
13570 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13571 {
13572 ULONGEST result;
13573 unsigned int num_read;
13574 int i, shift;
13575 unsigned char byte;
13576
13577 result = 0;
13578 shift = 0;
13579 num_read = 0;
13580 i = 0;
13581 while (1)
13582 {
13583 byte = bfd_get_8 (abfd, buf);
13584 buf++;
13585 num_read++;
13586 result |= ((ULONGEST) (byte & 127) << shift);
13587 if ((byte & 128) == 0)
13588 {
13589 break;
13590 }
13591 shift += 7;
13592 }
13593 *bytes_read_ptr = num_read;
13594 return result;
13595 }
13596
13597 static LONGEST
13598 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13599 {
13600 LONGEST result;
13601 int i, shift, num_read;
13602 unsigned char byte;
13603
13604 result = 0;
13605 shift = 0;
13606 num_read = 0;
13607 i = 0;
13608 while (1)
13609 {
13610 byte = bfd_get_8 (abfd, buf);
13611 buf++;
13612 num_read++;
13613 result |= ((LONGEST) (byte & 127) << shift);
13614 shift += 7;
13615 if ((byte & 128) == 0)
13616 {
13617 break;
13618 }
13619 }
13620 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13621 result |= -(((LONGEST) 1) << shift);
13622 *bytes_read_ptr = num_read;
13623 return result;
13624 }
13625
13626 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13627 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13628 ADDR_SIZE is the size of addresses from the CU header. */
13629
13630 static CORE_ADDR
13631 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13632 {
13633 struct objfile *objfile = dwarf2_per_objfile->objfile;
13634 bfd *abfd = objfile->obfd;
13635 const gdb_byte *info_ptr;
13636
13637 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13638 if (dwarf2_per_objfile->addr.buffer == NULL)
13639 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13640 objfile->name);
13641 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13642 error (_("DW_FORM_addr_index pointing outside of "
13643 ".debug_addr section [in module %s]"),
13644 objfile->name);
13645 info_ptr = (dwarf2_per_objfile->addr.buffer
13646 + addr_base + addr_index * addr_size);
13647 if (addr_size == 4)
13648 return bfd_get_32 (abfd, info_ptr);
13649 else
13650 return bfd_get_64 (abfd, info_ptr);
13651 }
13652
13653 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13654
13655 static CORE_ADDR
13656 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13657 {
13658 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13659 }
13660
13661 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13662
13663 static CORE_ADDR
13664 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13665 unsigned int *bytes_read)
13666 {
13667 bfd *abfd = cu->objfile->obfd;
13668 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13669
13670 return read_addr_index (cu, addr_index);
13671 }
13672
13673 /* Data structure to pass results from dwarf2_read_addr_index_reader
13674 back to dwarf2_read_addr_index. */
13675
13676 struct dwarf2_read_addr_index_data
13677 {
13678 ULONGEST addr_base;
13679 int addr_size;
13680 };
13681
13682 /* die_reader_func for dwarf2_read_addr_index. */
13683
13684 static void
13685 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13686 gdb_byte *info_ptr,
13687 struct die_info *comp_unit_die,
13688 int has_children,
13689 void *data)
13690 {
13691 struct dwarf2_cu *cu = reader->cu;
13692 struct dwarf2_read_addr_index_data *aidata =
13693 (struct dwarf2_read_addr_index_data *) data;
13694
13695 aidata->addr_base = cu->addr_base;
13696 aidata->addr_size = cu->header.addr_size;
13697 }
13698
13699 /* Given an index in .debug_addr, fetch the value.
13700 NOTE: This can be called during dwarf expression evaluation,
13701 long after the debug information has been read, and thus per_cu->cu
13702 may no longer exist. */
13703
13704 CORE_ADDR
13705 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13706 unsigned int addr_index)
13707 {
13708 struct objfile *objfile = per_cu->objfile;
13709 struct dwarf2_cu *cu = per_cu->cu;
13710 ULONGEST addr_base;
13711 int addr_size;
13712
13713 /* This is intended to be called from outside this file. */
13714 dw2_setup (objfile);
13715
13716 /* We need addr_base and addr_size.
13717 If we don't have PER_CU->cu, we have to get it.
13718 Nasty, but the alternative is storing the needed info in PER_CU,
13719 which at this point doesn't seem justified: it's not clear how frequently
13720 it would get used and it would increase the size of every PER_CU.
13721 Entry points like dwarf2_per_cu_addr_size do a similar thing
13722 so we're not in uncharted territory here.
13723 Alas we need to be a bit more complicated as addr_base is contained
13724 in the DIE.
13725
13726 We don't need to read the entire CU(/TU).
13727 We just need the header and top level die.
13728
13729 IWBN to use the aging mechanism to let us lazily later discard the CU.
13730 For now we skip this optimization. */
13731
13732 if (cu != NULL)
13733 {
13734 addr_base = cu->addr_base;
13735 addr_size = cu->header.addr_size;
13736 }
13737 else
13738 {
13739 struct dwarf2_read_addr_index_data aidata;
13740
13741 /* Note: We can't use init_cutu_and_read_dies_simple here,
13742 we need addr_base. */
13743 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
13744 dwarf2_read_addr_index_reader, &aidata);
13745 addr_base = aidata.addr_base;
13746 addr_size = aidata.addr_size;
13747 }
13748
13749 return read_addr_index_1 (addr_index, addr_base, addr_size);
13750 }
13751
13752 /* Given a DW_AT_str_index, fetch the string. */
13753
13754 static char *
13755 read_str_index (const struct die_reader_specs *reader,
13756 struct dwarf2_cu *cu, ULONGEST str_index)
13757 {
13758 struct objfile *objfile = dwarf2_per_objfile->objfile;
13759 const char *dwo_name = objfile->name;
13760 bfd *abfd = objfile->obfd;
13761 struct dwo_sections *sections = &reader->dwo_file->sections;
13762 gdb_byte *info_ptr;
13763 ULONGEST str_offset;
13764
13765 dwarf2_read_section (objfile, &sections->str);
13766 dwarf2_read_section (objfile, &sections->str_offsets);
13767 if (sections->str.buffer == NULL)
13768 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13769 " in CU at offset 0x%lx [in module %s]"),
13770 (long) cu->header.offset.sect_off, dwo_name);
13771 if (sections->str_offsets.buffer == NULL)
13772 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13773 " in CU at offset 0x%lx [in module %s]"),
13774 (long) cu->header.offset.sect_off, dwo_name);
13775 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13776 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13777 " section in CU at offset 0x%lx [in module %s]"),
13778 (long) cu->header.offset.sect_off, dwo_name);
13779 info_ptr = (sections->str_offsets.buffer
13780 + str_index * cu->header.offset_size);
13781 if (cu->header.offset_size == 4)
13782 str_offset = bfd_get_32 (abfd, info_ptr);
13783 else
13784 str_offset = bfd_get_64 (abfd, info_ptr);
13785 if (str_offset >= sections->str.size)
13786 error (_("Offset from DW_FORM_str_index pointing outside of"
13787 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13788 (long) cu->header.offset.sect_off, dwo_name);
13789 return (char *) (sections->str.buffer + str_offset);
13790 }
13791
13792 /* Return the length of an LEB128 number in BUF. */
13793
13794 static int
13795 leb128_size (const gdb_byte *buf)
13796 {
13797 const gdb_byte *begin = buf;
13798 gdb_byte byte;
13799
13800 while (1)
13801 {
13802 byte = *buf++;
13803 if ((byte & 128) == 0)
13804 return buf - begin;
13805 }
13806 }
13807
13808 static void
13809 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13810 {
13811 switch (lang)
13812 {
13813 case DW_LANG_C89:
13814 case DW_LANG_C99:
13815 case DW_LANG_C:
13816 cu->language = language_c;
13817 break;
13818 case DW_LANG_C_plus_plus:
13819 cu->language = language_cplus;
13820 break;
13821 case DW_LANG_D:
13822 cu->language = language_d;
13823 break;
13824 case DW_LANG_Fortran77:
13825 case DW_LANG_Fortran90:
13826 case DW_LANG_Fortran95:
13827 cu->language = language_fortran;
13828 break;
13829 case DW_LANG_Go:
13830 cu->language = language_go;
13831 break;
13832 case DW_LANG_Mips_Assembler:
13833 cu->language = language_asm;
13834 break;
13835 case DW_LANG_Java:
13836 cu->language = language_java;
13837 break;
13838 case DW_LANG_Ada83:
13839 case DW_LANG_Ada95:
13840 cu->language = language_ada;
13841 break;
13842 case DW_LANG_Modula2:
13843 cu->language = language_m2;
13844 break;
13845 case DW_LANG_Pascal83:
13846 cu->language = language_pascal;
13847 break;
13848 case DW_LANG_ObjC:
13849 cu->language = language_objc;
13850 break;
13851 case DW_LANG_Cobol74:
13852 case DW_LANG_Cobol85:
13853 default:
13854 cu->language = language_minimal;
13855 break;
13856 }
13857 cu->language_defn = language_def (cu->language);
13858 }
13859
13860 /* Return the named attribute or NULL if not there. */
13861
13862 static struct attribute *
13863 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13864 {
13865 for (;;)
13866 {
13867 unsigned int i;
13868 struct attribute *spec = NULL;
13869
13870 for (i = 0; i < die->num_attrs; ++i)
13871 {
13872 if (die->attrs[i].name == name)
13873 return &die->attrs[i];
13874 if (die->attrs[i].name == DW_AT_specification
13875 || die->attrs[i].name == DW_AT_abstract_origin)
13876 spec = &die->attrs[i];
13877 }
13878
13879 if (!spec)
13880 break;
13881
13882 die = follow_die_ref (die, spec, &cu);
13883 }
13884
13885 return NULL;
13886 }
13887
13888 /* Return the named attribute or NULL if not there,
13889 but do not follow DW_AT_specification, etc.
13890 This is for use in contexts where we're reading .debug_types dies.
13891 Following DW_AT_specification, DW_AT_abstract_origin will take us
13892 back up the chain, and we want to go down. */
13893
13894 static struct attribute *
13895 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13896 {
13897 unsigned int i;
13898
13899 for (i = 0; i < die->num_attrs; ++i)
13900 if (die->attrs[i].name == name)
13901 return &die->attrs[i];
13902
13903 return NULL;
13904 }
13905
13906 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13907 and holds a non-zero value. This function should only be used for
13908 DW_FORM_flag or DW_FORM_flag_present attributes. */
13909
13910 static int
13911 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13912 {
13913 struct attribute *attr = dwarf2_attr (die, name, cu);
13914
13915 return (attr && DW_UNSND (attr));
13916 }
13917
13918 static int
13919 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13920 {
13921 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13922 which value is non-zero. However, we have to be careful with
13923 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13924 (via dwarf2_flag_true_p) follows this attribute. So we may
13925 end up accidently finding a declaration attribute that belongs
13926 to a different DIE referenced by the specification attribute,
13927 even though the given DIE does not have a declaration attribute. */
13928 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13929 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13930 }
13931
13932 /* Return the die giving the specification for DIE, if there is
13933 one. *SPEC_CU is the CU containing DIE on input, and the CU
13934 containing the return value on output. If there is no
13935 specification, but there is an abstract origin, that is
13936 returned. */
13937
13938 static struct die_info *
13939 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13940 {
13941 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13942 *spec_cu);
13943
13944 if (spec_attr == NULL)
13945 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13946
13947 if (spec_attr == NULL)
13948 return NULL;
13949 else
13950 return follow_die_ref (die, spec_attr, spec_cu);
13951 }
13952
13953 /* Free the line_header structure *LH, and any arrays and strings it
13954 refers to.
13955 NOTE: This is also used as a "cleanup" function. */
13956
13957 static void
13958 free_line_header (struct line_header *lh)
13959 {
13960 if (lh->standard_opcode_lengths)
13961 xfree (lh->standard_opcode_lengths);
13962
13963 /* Remember that all the lh->file_names[i].name pointers are
13964 pointers into debug_line_buffer, and don't need to be freed. */
13965 if (lh->file_names)
13966 xfree (lh->file_names);
13967
13968 /* Similarly for the include directory names. */
13969 if (lh->include_dirs)
13970 xfree (lh->include_dirs);
13971
13972 xfree (lh);
13973 }
13974
13975 /* Add an entry to LH's include directory table. */
13976
13977 static void
13978 add_include_dir (struct line_header *lh, char *include_dir)
13979 {
13980 /* Grow the array if necessary. */
13981 if (lh->include_dirs_size == 0)
13982 {
13983 lh->include_dirs_size = 1; /* for testing */
13984 lh->include_dirs = xmalloc (lh->include_dirs_size
13985 * sizeof (*lh->include_dirs));
13986 }
13987 else if (lh->num_include_dirs >= lh->include_dirs_size)
13988 {
13989 lh->include_dirs_size *= 2;
13990 lh->include_dirs = xrealloc (lh->include_dirs,
13991 (lh->include_dirs_size
13992 * sizeof (*lh->include_dirs)));
13993 }
13994
13995 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13996 }
13997
13998 /* Add an entry to LH's file name table. */
13999
14000 static void
14001 add_file_name (struct line_header *lh,
14002 char *name,
14003 unsigned int dir_index,
14004 unsigned int mod_time,
14005 unsigned int length)
14006 {
14007 struct file_entry *fe;
14008
14009 /* Grow the array if necessary. */
14010 if (lh->file_names_size == 0)
14011 {
14012 lh->file_names_size = 1; /* for testing */
14013 lh->file_names = xmalloc (lh->file_names_size
14014 * sizeof (*lh->file_names));
14015 }
14016 else if (lh->num_file_names >= lh->file_names_size)
14017 {
14018 lh->file_names_size *= 2;
14019 lh->file_names = xrealloc (lh->file_names,
14020 (lh->file_names_size
14021 * sizeof (*lh->file_names)));
14022 }
14023
14024 fe = &lh->file_names[lh->num_file_names++];
14025 fe->name = name;
14026 fe->dir_index = dir_index;
14027 fe->mod_time = mod_time;
14028 fe->length = length;
14029 fe->included_p = 0;
14030 fe->symtab = NULL;
14031 }
14032
14033 /* A convenience function to find the proper .debug_line section for a
14034 CU. */
14035
14036 static struct dwarf2_section_info *
14037 get_debug_line_section (struct dwarf2_cu *cu)
14038 {
14039 struct dwarf2_section_info *section;
14040
14041 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14042 DWO file. */
14043 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14044 section = &cu->dwo_unit->dwo_file->sections.line;
14045 else if (cu->per_cu->is_dwz)
14046 {
14047 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14048
14049 section = &dwz->line;
14050 }
14051 else
14052 section = &dwarf2_per_objfile->line;
14053
14054 return section;
14055 }
14056
14057 /* Read the statement program header starting at OFFSET in
14058 .debug_line, or .debug_line.dwo. Return a pointer
14059 to a struct line_header, allocated using xmalloc.
14060
14061 NOTE: the strings in the include directory and file name tables of
14062 the returned object point into the dwarf line section buffer,
14063 and must not be freed. */
14064
14065 static struct line_header *
14066 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14067 {
14068 struct cleanup *back_to;
14069 struct line_header *lh;
14070 gdb_byte *line_ptr;
14071 unsigned int bytes_read, offset_size;
14072 int i;
14073 char *cur_dir, *cur_file;
14074 struct dwarf2_section_info *section;
14075 bfd *abfd;
14076
14077 section = get_debug_line_section (cu);
14078 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14079 if (section->buffer == NULL)
14080 {
14081 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14082 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14083 else
14084 complaint (&symfile_complaints, _("missing .debug_line section"));
14085 return 0;
14086 }
14087
14088 /* We can't do this until we know the section is non-empty.
14089 Only then do we know we have such a section. */
14090 abfd = section->asection->owner;
14091
14092 /* Make sure that at least there's room for the total_length field.
14093 That could be 12 bytes long, but we're just going to fudge that. */
14094 if (offset + 4 >= section->size)
14095 {
14096 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14097 return 0;
14098 }
14099
14100 lh = xmalloc (sizeof (*lh));
14101 memset (lh, 0, sizeof (*lh));
14102 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14103 (void *) lh);
14104
14105 line_ptr = section->buffer + offset;
14106
14107 /* Read in the header. */
14108 lh->total_length =
14109 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14110 &bytes_read, &offset_size);
14111 line_ptr += bytes_read;
14112 if (line_ptr + lh->total_length > (section->buffer + section->size))
14113 {
14114 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14115 return 0;
14116 }
14117 lh->statement_program_end = line_ptr + lh->total_length;
14118 lh->version = read_2_bytes (abfd, line_ptr);
14119 line_ptr += 2;
14120 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14121 line_ptr += offset_size;
14122 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14123 line_ptr += 1;
14124 if (lh->version >= 4)
14125 {
14126 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14127 line_ptr += 1;
14128 }
14129 else
14130 lh->maximum_ops_per_instruction = 1;
14131
14132 if (lh->maximum_ops_per_instruction == 0)
14133 {
14134 lh->maximum_ops_per_instruction = 1;
14135 complaint (&symfile_complaints,
14136 _("invalid maximum_ops_per_instruction "
14137 "in `.debug_line' section"));
14138 }
14139
14140 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14141 line_ptr += 1;
14142 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14143 line_ptr += 1;
14144 lh->line_range = read_1_byte (abfd, line_ptr);
14145 line_ptr += 1;
14146 lh->opcode_base = read_1_byte (abfd, line_ptr);
14147 line_ptr += 1;
14148 lh->standard_opcode_lengths
14149 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14150
14151 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14152 for (i = 1; i < lh->opcode_base; ++i)
14153 {
14154 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14155 line_ptr += 1;
14156 }
14157
14158 /* Read directory table. */
14159 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14160 {
14161 line_ptr += bytes_read;
14162 add_include_dir (lh, cur_dir);
14163 }
14164 line_ptr += bytes_read;
14165
14166 /* Read file name table. */
14167 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14168 {
14169 unsigned int dir_index, mod_time, length;
14170
14171 line_ptr += bytes_read;
14172 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14173 line_ptr += bytes_read;
14174 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14175 line_ptr += bytes_read;
14176 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14177 line_ptr += bytes_read;
14178
14179 add_file_name (lh, cur_file, dir_index, mod_time, length);
14180 }
14181 line_ptr += bytes_read;
14182 lh->statement_program_start = line_ptr;
14183
14184 if (line_ptr > (section->buffer + section->size))
14185 complaint (&symfile_complaints,
14186 _("line number info header doesn't "
14187 "fit in `.debug_line' section"));
14188
14189 discard_cleanups (back_to);
14190 return lh;
14191 }
14192
14193 /* Subroutine of dwarf_decode_lines to simplify it.
14194 Return the file name of the psymtab for included file FILE_INDEX
14195 in line header LH of PST.
14196 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14197 If space for the result is malloc'd, it will be freed by a cleanup.
14198 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
14199
14200 static char *
14201 psymtab_include_file_name (const struct line_header *lh, int file_index,
14202 const struct partial_symtab *pst,
14203 const char *comp_dir)
14204 {
14205 const struct file_entry fe = lh->file_names [file_index];
14206 char *include_name = fe.name;
14207 char *include_name_to_compare = include_name;
14208 char *dir_name = NULL;
14209 const char *pst_filename;
14210 char *copied_name = NULL;
14211 int file_is_pst;
14212
14213 if (fe.dir_index)
14214 dir_name = lh->include_dirs[fe.dir_index - 1];
14215
14216 if (!IS_ABSOLUTE_PATH (include_name)
14217 && (dir_name != NULL || comp_dir != NULL))
14218 {
14219 /* Avoid creating a duplicate psymtab for PST.
14220 We do this by comparing INCLUDE_NAME and PST_FILENAME.
14221 Before we do the comparison, however, we need to account
14222 for DIR_NAME and COMP_DIR.
14223 First prepend dir_name (if non-NULL). If we still don't
14224 have an absolute path prepend comp_dir (if non-NULL).
14225 However, the directory we record in the include-file's
14226 psymtab does not contain COMP_DIR (to match the
14227 corresponding symtab(s)).
14228
14229 Example:
14230
14231 bash$ cd /tmp
14232 bash$ gcc -g ./hello.c
14233 include_name = "hello.c"
14234 dir_name = "."
14235 DW_AT_comp_dir = comp_dir = "/tmp"
14236 DW_AT_name = "./hello.c" */
14237
14238 if (dir_name != NULL)
14239 {
14240 include_name = concat (dir_name, SLASH_STRING,
14241 include_name, (char *)NULL);
14242 include_name_to_compare = include_name;
14243 make_cleanup (xfree, include_name);
14244 }
14245 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14246 {
14247 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14248 include_name, (char *)NULL);
14249 }
14250 }
14251
14252 pst_filename = pst->filename;
14253 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14254 {
14255 copied_name = concat (pst->dirname, SLASH_STRING,
14256 pst_filename, (char *)NULL);
14257 pst_filename = copied_name;
14258 }
14259
14260 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14261
14262 if (include_name_to_compare != include_name)
14263 xfree (include_name_to_compare);
14264 if (copied_name != NULL)
14265 xfree (copied_name);
14266
14267 if (file_is_pst)
14268 return NULL;
14269 return include_name;
14270 }
14271
14272 /* Ignore this record_line request. */
14273
14274 static void
14275 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14276 {
14277 return;
14278 }
14279
14280 /* Subroutine of dwarf_decode_lines to simplify it.
14281 Process the line number information in LH. */
14282
14283 static void
14284 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14285 struct dwarf2_cu *cu, struct partial_symtab *pst)
14286 {
14287 gdb_byte *line_ptr, *extended_end;
14288 gdb_byte *line_end;
14289 unsigned int bytes_read, extended_len;
14290 unsigned char op_code, extended_op, adj_opcode;
14291 CORE_ADDR baseaddr;
14292 struct objfile *objfile = cu->objfile;
14293 bfd *abfd = objfile->obfd;
14294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14295 const int decode_for_pst_p = (pst != NULL);
14296 struct subfile *last_subfile = NULL;
14297 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14298 = record_line;
14299
14300 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14301
14302 line_ptr = lh->statement_program_start;
14303 line_end = lh->statement_program_end;
14304
14305 /* Read the statement sequences until there's nothing left. */
14306 while (line_ptr < line_end)
14307 {
14308 /* state machine registers */
14309 CORE_ADDR address = 0;
14310 unsigned int file = 1;
14311 unsigned int line = 1;
14312 unsigned int column = 0;
14313 int is_stmt = lh->default_is_stmt;
14314 int basic_block = 0;
14315 int end_sequence = 0;
14316 CORE_ADDR addr;
14317 unsigned char op_index = 0;
14318
14319 if (!decode_for_pst_p && lh->num_file_names >= file)
14320 {
14321 /* Start a subfile for the current file of the state machine. */
14322 /* lh->include_dirs and lh->file_names are 0-based, but the
14323 directory and file name numbers in the statement program
14324 are 1-based. */
14325 struct file_entry *fe = &lh->file_names[file - 1];
14326 char *dir = NULL;
14327
14328 if (fe->dir_index)
14329 dir = lh->include_dirs[fe->dir_index - 1];
14330
14331 dwarf2_start_subfile (fe->name, dir, comp_dir);
14332 }
14333
14334 /* Decode the table. */
14335 while (!end_sequence)
14336 {
14337 op_code = read_1_byte (abfd, line_ptr);
14338 line_ptr += 1;
14339 if (line_ptr > line_end)
14340 {
14341 dwarf2_debug_line_missing_end_sequence_complaint ();
14342 break;
14343 }
14344
14345 if (op_code >= lh->opcode_base)
14346 {
14347 /* Special operand. */
14348 adj_opcode = op_code - lh->opcode_base;
14349 address += (((op_index + (adj_opcode / lh->line_range))
14350 / lh->maximum_ops_per_instruction)
14351 * lh->minimum_instruction_length);
14352 op_index = ((op_index + (adj_opcode / lh->line_range))
14353 % lh->maximum_ops_per_instruction);
14354 line += lh->line_base + (adj_opcode % lh->line_range);
14355 if (lh->num_file_names < file || file == 0)
14356 dwarf2_debug_line_missing_file_complaint ();
14357 /* For now we ignore lines not starting on an
14358 instruction boundary. */
14359 else if (op_index == 0)
14360 {
14361 lh->file_names[file - 1].included_p = 1;
14362 if (!decode_for_pst_p && is_stmt)
14363 {
14364 if (last_subfile != current_subfile)
14365 {
14366 addr = gdbarch_addr_bits_remove (gdbarch, address);
14367 if (last_subfile)
14368 (*p_record_line) (last_subfile, 0, addr);
14369 last_subfile = current_subfile;
14370 }
14371 /* Append row to matrix using current values. */
14372 addr = gdbarch_addr_bits_remove (gdbarch, address);
14373 (*p_record_line) (current_subfile, line, addr);
14374 }
14375 }
14376 basic_block = 0;
14377 }
14378 else switch (op_code)
14379 {
14380 case DW_LNS_extended_op:
14381 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14382 &bytes_read);
14383 line_ptr += bytes_read;
14384 extended_end = line_ptr + extended_len;
14385 extended_op = read_1_byte (abfd, line_ptr);
14386 line_ptr += 1;
14387 switch (extended_op)
14388 {
14389 case DW_LNE_end_sequence:
14390 p_record_line = record_line;
14391 end_sequence = 1;
14392 break;
14393 case DW_LNE_set_address:
14394 address = read_address (abfd, line_ptr, cu, &bytes_read);
14395
14396 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14397 {
14398 /* This line table is for a function which has been
14399 GCd by the linker. Ignore it. PR gdb/12528 */
14400
14401 long line_offset
14402 = line_ptr - get_debug_line_section (cu)->buffer;
14403
14404 complaint (&symfile_complaints,
14405 _(".debug_line address at offset 0x%lx is 0 "
14406 "[in module %s]"),
14407 line_offset, objfile->name);
14408 p_record_line = noop_record_line;
14409 }
14410
14411 op_index = 0;
14412 line_ptr += bytes_read;
14413 address += baseaddr;
14414 break;
14415 case DW_LNE_define_file:
14416 {
14417 char *cur_file;
14418 unsigned int dir_index, mod_time, length;
14419
14420 cur_file = read_direct_string (abfd, line_ptr,
14421 &bytes_read);
14422 line_ptr += bytes_read;
14423 dir_index =
14424 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14425 line_ptr += bytes_read;
14426 mod_time =
14427 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14428 line_ptr += bytes_read;
14429 length =
14430 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14431 line_ptr += bytes_read;
14432 add_file_name (lh, cur_file, dir_index, mod_time, length);
14433 }
14434 break;
14435 case DW_LNE_set_discriminator:
14436 /* The discriminator is not interesting to the debugger;
14437 just ignore it. */
14438 line_ptr = extended_end;
14439 break;
14440 default:
14441 complaint (&symfile_complaints,
14442 _("mangled .debug_line section"));
14443 return;
14444 }
14445 /* Make sure that we parsed the extended op correctly. If e.g.
14446 we expected a different address size than the producer used,
14447 we may have read the wrong number of bytes. */
14448 if (line_ptr != extended_end)
14449 {
14450 complaint (&symfile_complaints,
14451 _("mangled .debug_line section"));
14452 return;
14453 }
14454 break;
14455 case DW_LNS_copy:
14456 if (lh->num_file_names < file || file == 0)
14457 dwarf2_debug_line_missing_file_complaint ();
14458 else
14459 {
14460 lh->file_names[file - 1].included_p = 1;
14461 if (!decode_for_pst_p && is_stmt)
14462 {
14463 if (last_subfile != current_subfile)
14464 {
14465 addr = gdbarch_addr_bits_remove (gdbarch, address);
14466 if (last_subfile)
14467 (*p_record_line) (last_subfile, 0, addr);
14468 last_subfile = current_subfile;
14469 }
14470 addr = gdbarch_addr_bits_remove (gdbarch, address);
14471 (*p_record_line) (current_subfile, line, addr);
14472 }
14473 }
14474 basic_block = 0;
14475 break;
14476 case DW_LNS_advance_pc:
14477 {
14478 CORE_ADDR adjust
14479 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14480
14481 address += (((op_index + adjust)
14482 / lh->maximum_ops_per_instruction)
14483 * lh->minimum_instruction_length);
14484 op_index = ((op_index + adjust)
14485 % lh->maximum_ops_per_instruction);
14486 line_ptr += bytes_read;
14487 }
14488 break;
14489 case DW_LNS_advance_line:
14490 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14491 line_ptr += bytes_read;
14492 break;
14493 case DW_LNS_set_file:
14494 {
14495 /* The arrays lh->include_dirs and lh->file_names are
14496 0-based, but the directory and file name numbers in
14497 the statement program are 1-based. */
14498 struct file_entry *fe;
14499 char *dir = NULL;
14500
14501 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14502 line_ptr += bytes_read;
14503 if (lh->num_file_names < file || file == 0)
14504 dwarf2_debug_line_missing_file_complaint ();
14505 else
14506 {
14507 fe = &lh->file_names[file - 1];
14508 if (fe->dir_index)
14509 dir = lh->include_dirs[fe->dir_index - 1];
14510 if (!decode_for_pst_p)
14511 {
14512 last_subfile = current_subfile;
14513 dwarf2_start_subfile (fe->name, dir, comp_dir);
14514 }
14515 }
14516 }
14517 break;
14518 case DW_LNS_set_column:
14519 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14520 line_ptr += bytes_read;
14521 break;
14522 case DW_LNS_negate_stmt:
14523 is_stmt = (!is_stmt);
14524 break;
14525 case DW_LNS_set_basic_block:
14526 basic_block = 1;
14527 break;
14528 /* Add to the address register of the state machine the
14529 address increment value corresponding to special opcode
14530 255. I.e., this value is scaled by the minimum
14531 instruction length since special opcode 255 would have
14532 scaled the increment. */
14533 case DW_LNS_const_add_pc:
14534 {
14535 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14536
14537 address += (((op_index + adjust)
14538 / lh->maximum_ops_per_instruction)
14539 * lh->minimum_instruction_length);
14540 op_index = ((op_index + adjust)
14541 % lh->maximum_ops_per_instruction);
14542 }
14543 break;
14544 case DW_LNS_fixed_advance_pc:
14545 address += read_2_bytes (abfd, line_ptr);
14546 op_index = 0;
14547 line_ptr += 2;
14548 break;
14549 default:
14550 {
14551 /* Unknown standard opcode, ignore it. */
14552 int i;
14553
14554 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14555 {
14556 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14557 line_ptr += bytes_read;
14558 }
14559 }
14560 }
14561 }
14562 if (lh->num_file_names < file || file == 0)
14563 dwarf2_debug_line_missing_file_complaint ();
14564 else
14565 {
14566 lh->file_names[file - 1].included_p = 1;
14567 if (!decode_for_pst_p)
14568 {
14569 addr = gdbarch_addr_bits_remove (gdbarch, address);
14570 (*p_record_line) (current_subfile, 0, addr);
14571 }
14572 }
14573 }
14574 }
14575
14576 /* Decode the Line Number Program (LNP) for the given line_header
14577 structure and CU. The actual information extracted and the type
14578 of structures created from the LNP depends on the value of PST.
14579
14580 1. If PST is NULL, then this procedure uses the data from the program
14581 to create all necessary symbol tables, and their linetables.
14582
14583 2. If PST is not NULL, this procedure reads the program to determine
14584 the list of files included by the unit represented by PST, and
14585 builds all the associated partial symbol tables.
14586
14587 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14588 It is used for relative paths in the line table.
14589 NOTE: When processing partial symtabs (pst != NULL),
14590 comp_dir == pst->dirname.
14591
14592 NOTE: It is important that psymtabs have the same file name (via strcmp)
14593 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14594 symtab we don't use it in the name of the psymtabs we create.
14595 E.g. expand_line_sal requires this when finding psymtabs to expand.
14596 A good testcase for this is mb-inline.exp. */
14597
14598 static void
14599 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14600 struct dwarf2_cu *cu, struct partial_symtab *pst,
14601 int want_line_info)
14602 {
14603 struct objfile *objfile = cu->objfile;
14604 const int decode_for_pst_p = (pst != NULL);
14605 struct subfile *first_subfile = current_subfile;
14606
14607 if (want_line_info)
14608 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14609
14610 if (decode_for_pst_p)
14611 {
14612 int file_index;
14613
14614 /* Now that we're done scanning the Line Header Program, we can
14615 create the psymtab of each included file. */
14616 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14617 if (lh->file_names[file_index].included_p == 1)
14618 {
14619 char *include_name =
14620 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14621 if (include_name != NULL)
14622 dwarf2_create_include_psymtab (include_name, pst, objfile);
14623 }
14624 }
14625 else
14626 {
14627 /* Make sure a symtab is created for every file, even files
14628 which contain only variables (i.e. no code with associated
14629 line numbers). */
14630 int i;
14631
14632 for (i = 0; i < lh->num_file_names; i++)
14633 {
14634 char *dir = NULL;
14635 struct file_entry *fe;
14636
14637 fe = &lh->file_names[i];
14638 if (fe->dir_index)
14639 dir = lh->include_dirs[fe->dir_index - 1];
14640 dwarf2_start_subfile (fe->name, dir, comp_dir);
14641
14642 /* Skip the main file; we don't need it, and it must be
14643 allocated last, so that it will show up before the
14644 non-primary symtabs in the objfile's symtab list. */
14645 if (current_subfile == first_subfile)
14646 continue;
14647
14648 if (current_subfile->symtab == NULL)
14649 current_subfile->symtab = allocate_symtab (current_subfile->name,
14650 objfile);
14651 fe->symtab = current_subfile->symtab;
14652 }
14653 }
14654 }
14655
14656 /* Start a subfile for DWARF. FILENAME is the name of the file and
14657 DIRNAME the name of the source directory which contains FILENAME
14658 or NULL if not known. COMP_DIR is the compilation directory for the
14659 linetable's compilation unit or NULL if not known.
14660 This routine tries to keep line numbers from identical absolute and
14661 relative file names in a common subfile.
14662
14663 Using the `list' example from the GDB testsuite, which resides in
14664 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14665 of /srcdir/list0.c yields the following debugging information for list0.c:
14666
14667 DW_AT_name: /srcdir/list0.c
14668 DW_AT_comp_dir: /compdir
14669 files.files[0].name: list0.h
14670 files.files[0].dir: /srcdir
14671 files.files[1].name: list0.c
14672 files.files[1].dir: /srcdir
14673
14674 The line number information for list0.c has to end up in a single
14675 subfile, so that `break /srcdir/list0.c:1' works as expected.
14676 start_subfile will ensure that this happens provided that we pass the
14677 concatenation of files.files[1].dir and files.files[1].name as the
14678 subfile's name. */
14679
14680 static void
14681 dwarf2_start_subfile (char *filename, const char *dirname,
14682 const char *comp_dir)
14683 {
14684 char *fullname;
14685
14686 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14687 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14688 second argument to start_subfile. To be consistent, we do the
14689 same here. In order not to lose the line information directory,
14690 we concatenate it to the filename when it makes sense.
14691 Note that the Dwarf3 standard says (speaking of filenames in line
14692 information): ``The directory index is ignored for file names
14693 that represent full path names''. Thus ignoring dirname in the
14694 `else' branch below isn't an issue. */
14695
14696 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14697 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14698 else
14699 fullname = filename;
14700
14701 start_subfile (fullname, comp_dir);
14702
14703 if (fullname != filename)
14704 xfree (fullname);
14705 }
14706
14707 /* Start a symtab for DWARF.
14708 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14709
14710 static void
14711 dwarf2_start_symtab (struct dwarf2_cu *cu,
14712 char *name, char *comp_dir, CORE_ADDR low_pc)
14713 {
14714 start_symtab (name, comp_dir, low_pc);
14715 record_debugformat ("DWARF 2");
14716 record_producer (cu->producer);
14717
14718 /* We assume that we're processing GCC output. */
14719 processing_gcc_compilation = 2;
14720
14721 processing_has_namespace_info = 0;
14722 }
14723
14724 static void
14725 var_decode_location (struct attribute *attr, struct symbol *sym,
14726 struct dwarf2_cu *cu)
14727 {
14728 struct objfile *objfile = cu->objfile;
14729 struct comp_unit_head *cu_header = &cu->header;
14730
14731 /* NOTE drow/2003-01-30: There used to be a comment and some special
14732 code here to turn a symbol with DW_AT_external and a
14733 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14734 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14735 with some versions of binutils) where shared libraries could have
14736 relocations against symbols in their debug information - the
14737 minimal symbol would have the right address, but the debug info
14738 would not. It's no longer necessary, because we will explicitly
14739 apply relocations when we read in the debug information now. */
14740
14741 /* A DW_AT_location attribute with no contents indicates that a
14742 variable has been optimized away. */
14743 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14744 {
14745 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14746 return;
14747 }
14748
14749 /* Handle one degenerate form of location expression specially, to
14750 preserve GDB's previous behavior when section offsets are
14751 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14752 then mark this symbol as LOC_STATIC. */
14753
14754 if (attr_form_is_block (attr)
14755 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14756 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14757 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14758 && (DW_BLOCK (attr)->size
14759 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14760 {
14761 unsigned int dummy;
14762
14763 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14764 SYMBOL_VALUE_ADDRESS (sym) =
14765 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14766 else
14767 SYMBOL_VALUE_ADDRESS (sym) =
14768 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14769 SYMBOL_CLASS (sym) = LOC_STATIC;
14770 fixup_symbol_section (sym, objfile);
14771 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14772 SYMBOL_SECTION (sym));
14773 return;
14774 }
14775
14776 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14777 expression evaluator, and use LOC_COMPUTED only when necessary
14778 (i.e. when the value of a register or memory location is
14779 referenced, or a thread-local block, etc.). Then again, it might
14780 not be worthwhile. I'm assuming that it isn't unless performance
14781 or memory numbers show me otherwise. */
14782
14783 dwarf2_symbol_mark_computed (attr, sym, cu);
14784 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14785
14786 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14787 cu->has_loclist = 1;
14788 }
14789
14790 /* Given a pointer to a DWARF information entry, figure out if we need
14791 to make a symbol table entry for it, and if so, create a new entry
14792 and return a pointer to it.
14793 If TYPE is NULL, determine symbol type from the die, otherwise
14794 used the passed type.
14795 If SPACE is not NULL, use it to hold the new symbol. If it is
14796 NULL, allocate a new symbol on the objfile's obstack. */
14797
14798 static struct symbol *
14799 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14800 struct symbol *space)
14801 {
14802 struct objfile *objfile = cu->objfile;
14803 struct symbol *sym = NULL;
14804 char *name;
14805 struct attribute *attr = NULL;
14806 struct attribute *attr2 = NULL;
14807 CORE_ADDR baseaddr;
14808 struct pending **list_to_add = NULL;
14809
14810 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14811
14812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14813
14814 name = dwarf2_name (die, cu);
14815 if (name)
14816 {
14817 const char *linkagename;
14818 int suppress_add = 0;
14819
14820 if (space)
14821 sym = space;
14822 else
14823 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14824 OBJSTAT (objfile, n_syms++);
14825
14826 /* Cache this symbol's name and the name's demangled form (if any). */
14827 SYMBOL_SET_LANGUAGE (sym, cu->language);
14828 linkagename = dwarf2_physname (name, die, cu);
14829 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14830
14831 /* Fortran does not have mangling standard and the mangling does differ
14832 between gfortran, iFort etc. */
14833 if (cu->language == language_fortran
14834 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14835 symbol_set_demangled_name (&(sym->ginfo),
14836 (char *) dwarf2_full_name (name, die, cu),
14837 NULL);
14838
14839 /* Default assumptions.
14840 Use the passed type or decode it from the die. */
14841 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14842 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14843 if (type != NULL)
14844 SYMBOL_TYPE (sym) = type;
14845 else
14846 SYMBOL_TYPE (sym) = die_type (die, cu);
14847 attr = dwarf2_attr (die,
14848 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14849 cu);
14850 if (attr)
14851 {
14852 SYMBOL_LINE (sym) = DW_UNSND (attr);
14853 }
14854
14855 attr = dwarf2_attr (die,
14856 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14857 cu);
14858 if (attr)
14859 {
14860 int file_index = DW_UNSND (attr);
14861
14862 if (cu->line_header == NULL
14863 || file_index > cu->line_header->num_file_names)
14864 complaint (&symfile_complaints,
14865 _("file index out of range"));
14866 else if (file_index > 0)
14867 {
14868 struct file_entry *fe;
14869
14870 fe = &cu->line_header->file_names[file_index - 1];
14871 SYMBOL_SYMTAB (sym) = fe->symtab;
14872 }
14873 }
14874
14875 switch (die->tag)
14876 {
14877 case DW_TAG_label:
14878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14879 if (attr)
14880 {
14881 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14882 }
14883 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14884 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14885 SYMBOL_CLASS (sym) = LOC_LABEL;
14886 add_symbol_to_list (sym, cu->list_in_scope);
14887 break;
14888 case DW_TAG_subprogram:
14889 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14890 finish_block. */
14891 SYMBOL_CLASS (sym) = LOC_BLOCK;
14892 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14893 if ((attr2 && (DW_UNSND (attr2) != 0))
14894 || cu->language == language_ada)
14895 {
14896 /* Subprograms marked external are stored as a global symbol.
14897 Ada subprograms, whether marked external or not, are always
14898 stored as a global symbol, because we want to be able to
14899 access them globally. For instance, we want to be able
14900 to break on a nested subprogram without having to
14901 specify the context. */
14902 list_to_add = &global_symbols;
14903 }
14904 else
14905 {
14906 list_to_add = cu->list_in_scope;
14907 }
14908 break;
14909 case DW_TAG_inlined_subroutine:
14910 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14911 finish_block. */
14912 SYMBOL_CLASS (sym) = LOC_BLOCK;
14913 SYMBOL_INLINED (sym) = 1;
14914 list_to_add = cu->list_in_scope;
14915 break;
14916 case DW_TAG_template_value_param:
14917 suppress_add = 1;
14918 /* Fall through. */
14919 case DW_TAG_constant:
14920 case DW_TAG_variable:
14921 case DW_TAG_member:
14922 /* Compilation with minimal debug info may result in
14923 variables with missing type entries. Change the
14924 misleading `void' type to something sensible. */
14925 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14926 SYMBOL_TYPE (sym)
14927 = objfile_type (objfile)->nodebug_data_symbol;
14928
14929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14930 /* In the case of DW_TAG_member, we should only be called for
14931 static const members. */
14932 if (die->tag == DW_TAG_member)
14933 {
14934 /* dwarf2_add_field uses die_is_declaration,
14935 so we do the same. */
14936 gdb_assert (die_is_declaration (die, cu));
14937 gdb_assert (attr);
14938 }
14939 if (attr)
14940 {
14941 dwarf2_const_value (attr, sym, cu);
14942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14943 if (!suppress_add)
14944 {
14945 if (attr2 && (DW_UNSND (attr2) != 0))
14946 list_to_add = &global_symbols;
14947 else
14948 list_to_add = cu->list_in_scope;
14949 }
14950 break;
14951 }
14952 attr = dwarf2_attr (die, DW_AT_location, cu);
14953 if (attr)
14954 {
14955 var_decode_location (attr, sym, cu);
14956 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14957
14958 /* Fortran explicitly imports any global symbols to the local
14959 scope by DW_TAG_common_block. */
14960 if (cu->language == language_fortran && die->parent
14961 && die->parent->tag == DW_TAG_common_block)
14962 attr2 = NULL;
14963
14964 if (SYMBOL_CLASS (sym) == LOC_STATIC
14965 && SYMBOL_VALUE_ADDRESS (sym) == 0
14966 && !dwarf2_per_objfile->has_section_at_zero)
14967 {
14968 /* When a static variable is eliminated by the linker,
14969 the corresponding debug information is not stripped
14970 out, but the variable address is set to null;
14971 do not add such variables into symbol table. */
14972 }
14973 else if (attr2 && (DW_UNSND (attr2) != 0))
14974 {
14975 /* Workaround gfortran PR debug/40040 - it uses
14976 DW_AT_location for variables in -fPIC libraries which may
14977 get overriden by other libraries/executable and get
14978 a different address. Resolve it by the minimal symbol
14979 which may come from inferior's executable using copy
14980 relocation. Make this workaround only for gfortran as for
14981 other compilers GDB cannot guess the minimal symbol
14982 Fortran mangling kind. */
14983 if (cu->language == language_fortran && die->parent
14984 && die->parent->tag == DW_TAG_module
14985 && cu->producer
14986 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14987 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14988
14989 /* A variable with DW_AT_external is never static,
14990 but it may be block-scoped. */
14991 list_to_add = (cu->list_in_scope == &file_symbols
14992 ? &global_symbols : cu->list_in_scope);
14993 }
14994 else
14995 list_to_add = cu->list_in_scope;
14996 }
14997 else
14998 {
14999 /* We do not know the address of this symbol.
15000 If it is an external symbol and we have type information
15001 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15002 The address of the variable will then be determined from
15003 the minimal symbol table whenever the variable is
15004 referenced. */
15005 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15006 if (attr2 && (DW_UNSND (attr2) != 0)
15007 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15008 {
15009 /* A variable with DW_AT_external is never static, but it
15010 may be block-scoped. */
15011 list_to_add = (cu->list_in_scope == &file_symbols
15012 ? &global_symbols : cu->list_in_scope);
15013
15014 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15015 }
15016 else if (!die_is_declaration (die, cu))
15017 {
15018 /* Use the default LOC_OPTIMIZED_OUT class. */
15019 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15020 if (!suppress_add)
15021 list_to_add = cu->list_in_scope;
15022 }
15023 }
15024 break;
15025 case DW_TAG_formal_parameter:
15026 /* If we are inside a function, mark this as an argument. If
15027 not, we might be looking at an argument to an inlined function
15028 when we do not have enough information to show inlined frames;
15029 pretend it's a local variable in that case so that the user can
15030 still see it. */
15031 if (context_stack_depth > 0
15032 && context_stack[context_stack_depth - 1].name != NULL)
15033 SYMBOL_IS_ARGUMENT (sym) = 1;
15034 attr = dwarf2_attr (die, DW_AT_location, cu);
15035 if (attr)
15036 {
15037 var_decode_location (attr, sym, cu);
15038 }
15039 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15040 if (attr)
15041 {
15042 dwarf2_const_value (attr, sym, cu);
15043 }
15044
15045 list_to_add = cu->list_in_scope;
15046 break;
15047 case DW_TAG_unspecified_parameters:
15048 /* From varargs functions; gdb doesn't seem to have any
15049 interest in this information, so just ignore it for now.
15050 (FIXME?) */
15051 break;
15052 case DW_TAG_template_type_param:
15053 suppress_add = 1;
15054 /* Fall through. */
15055 case DW_TAG_class_type:
15056 case DW_TAG_interface_type:
15057 case DW_TAG_structure_type:
15058 case DW_TAG_union_type:
15059 case DW_TAG_set_type:
15060 case DW_TAG_enumeration_type:
15061 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15062 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15063
15064 {
15065 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15066 really ever be static objects: otherwise, if you try
15067 to, say, break of a class's method and you're in a file
15068 which doesn't mention that class, it won't work unless
15069 the check for all static symbols in lookup_symbol_aux
15070 saves you. See the OtherFileClass tests in
15071 gdb.c++/namespace.exp. */
15072
15073 if (!suppress_add)
15074 {
15075 list_to_add = (cu->list_in_scope == &file_symbols
15076 && (cu->language == language_cplus
15077 || cu->language == language_java)
15078 ? &global_symbols : cu->list_in_scope);
15079
15080 /* The semantics of C++ state that "struct foo {
15081 ... }" also defines a typedef for "foo". A Java
15082 class declaration also defines a typedef for the
15083 class. */
15084 if (cu->language == language_cplus
15085 || cu->language == language_java
15086 || cu->language == language_ada)
15087 {
15088 /* The symbol's name is already allocated along
15089 with this objfile, so we don't need to
15090 duplicate it for the type. */
15091 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15092 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15093 }
15094 }
15095 }
15096 break;
15097 case DW_TAG_typedef:
15098 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15099 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15100 list_to_add = cu->list_in_scope;
15101 break;
15102 case DW_TAG_base_type:
15103 case DW_TAG_subrange_type:
15104 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15105 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15106 list_to_add = cu->list_in_scope;
15107 break;
15108 case DW_TAG_enumerator:
15109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15110 if (attr)
15111 {
15112 dwarf2_const_value (attr, sym, cu);
15113 }
15114 {
15115 /* NOTE: carlton/2003-11-10: See comment above in the
15116 DW_TAG_class_type, etc. block. */
15117
15118 list_to_add = (cu->list_in_scope == &file_symbols
15119 && (cu->language == language_cplus
15120 || cu->language == language_java)
15121 ? &global_symbols : cu->list_in_scope);
15122 }
15123 break;
15124 case DW_TAG_namespace:
15125 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15126 list_to_add = &global_symbols;
15127 break;
15128 case DW_TAG_common_block:
15129 SYMBOL_CLASS (sym) = LOC_STATIC;
15130 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15131 add_symbol_to_list (sym, cu->list_in_scope);
15132 break;
15133 default:
15134 /* Not a tag we recognize. Hopefully we aren't processing
15135 trash data, but since we must specifically ignore things
15136 we don't recognize, there is nothing else we should do at
15137 this point. */
15138 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15139 dwarf_tag_name (die->tag));
15140 break;
15141 }
15142
15143 if (suppress_add)
15144 {
15145 sym->hash_next = objfile->template_symbols;
15146 objfile->template_symbols = sym;
15147 list_to_add = NULL;
15148 }
15149
15150 if (list_to_add != NULL)
15151 add_symbol_to_list (sym, list_to_add);
15152
15153 /* For the benefit of old versions of GCC, check for anonymous
15154 namespaces based on the demangled name. */
15155 if (!processing_has_namespace_info
15156 && cu->language == language_cplus)
15157 cp_scan_for_anonymous_namespaces (sym, objfile);
15158 }
15159 return (sym);
15160 }
15161
15162 /* A wrapper for new_symbol_full that always allocates a new symbol. */
15163
15164 static struct symbol *
15165 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15166 {
15167 return new_symbol_full (die, type, cu, NULL);
15168 }
15169
15170 /* Given an attr with a DW_FORM_dataN value in host byte order,
15171 zero-extend it as appropriate for the symbol's type. The DWARF
15172 standard (v4) is not entirely clear about the meaning of using
15173 DW_FORM_dataN for a constant with a signed type, where the type is
15174 wider than the data. The conclusion of a discussion on the DWARF
15175 list was that this is unspecified. We choose to always zero-extend
15176 because that is the interpretation long in use by GCC. */
15177
15178 static gdb_byte *
15179 dwarf2_const_value_data (struct attribute *attr, struct type *type,
15180 const char *name, struct obstack *obstack,
15181 struct dwarf2_cu *cu, LONGEST *value, int bits)
15182 {
15183 struct objfile *objfile = cu->objfile;
15184 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15185 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
15186 LONGEST l = DW_UNSND (attr);
15187
15188 if (bits < sizeof (*value) * 8)
15189 {
15190 l &= ((LONGEST) 1 << bits) - 1;
15191 *value = l;
15192 }
15193 else if (bits == sizeof (*value) * 8)
15194 *value = l;
15195 else
15196 {
15197 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15198 store_unsigned_integer (bytes, bits / 8, byte_order, l);
15199 return bytes;
15200 }
15201
15202 return NULL;
15203 }
15204
15205 /* Read a constant value from an attribute. Either set *VALUE, or if
15206 the value does not fit in *VALUE, set *BYTES - either already
15207 allocated on the objfile obstack, or newly allocated on OBSTACK,
15208 or, set *BATON, if we translated the constant to a location
15209 expression. */
15210
15211 static void
15212 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15213 const char *name, struct obstack *obstack,
15214 struct dwarf2_cu *cu,
15215 LONGEST *value, gdb_byte **bytes,
15216 struct dwarf2_locexpr_baton **baton)
15217 {
15218 struct objfile *objfile = cu->objfile;
15219 struct comp_unit_head *cu_header = &cu->header;
15220 struct dwarf_block *blk;
15221 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15222 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15223
15224 *value = 0;
15225 *bytes = NULL;
15226 *baton = NULL;
15227
15228 switch (attr->form)
15229 {
15230 case DW_FORM_addr:
15231 case DW_FORM_GNU_addr_index:
15232 {
15233 gdb_byte *data;
15234
15235 if (TYPE_LENGTH (type) != cu_header->addr_size)
15236 dwarf2_const_value_length_mismatch_complaint (name,
15237 cu_header->addr_size,
15238 TYPE_LENGTH (type));
15239 /* Symbols of this form are reasonably rare, so we just
15240 piggyback on the existing location code rather than writing
15241 a new implementation of symbol_computed_ops. */
15242 *baton = obstack_alloc (&objfile->objfile_obstack,
15243 sizeof (struct dwarf2_locexpr_baton));
15244 (*baton)->per_cu = cu->per_cu;
15245 gdb_assert ((*baton)->per_cu);
15246
15247 (*baton)->size = 2 + cu_header->addr_size;
15248 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15249 (*baton)->data = data;
15250
15251 data[0] = DW_OP_addr;
15252 store_unsigned_integer (&data[1], cu_header->addr_size,
15253 byte_order, DW_ADDR (attr));
15254 data[cu_header->addr_size + 1] = DW_OP_stack_value;
15255 }
15256 break;
15257 case DW_FORM_string:
15258 case DW_FORM_strp:
15259 case DW_FORM_GNU_str_index:
15260 case DW_FORM_GNU_strp_alt:
15261 /* DW_STRING is already allocated on the objfile obstack, point
15262 directly to it. */
15263 *bytes = (gdb_byte *) DW_STRING (attr);
15264 break;
15265 case DW_FORM_block1:
15266 case DW_FORM_block2:
15267 case DW_FORM_block4:
15268 case DW_FORM_block:
15269 case DW_FORM_exprloc:
15270 blk = DW_BLOCK (attr);
15271 if (TYPE_LENGTH (type) != blk->size)
15272 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15273 TYPE_LENGTH (type));
15274 *bytes = blk->data;
15275 break;
15276
15277 /* The DW_AT_const_value attributes are supposed to carry the
15278 symbol's value "represented as it would be on the target
15279 architecture." By the time we get here, it's already been
15280 converted to host endianness, so we just need to sign- or
15281 zero-extend it as appropriate. */
15282 case DW_FORM_data1:
15283 *bytes = dwarf2_const_value_data (attr, type, name,
15284 obstack, cu, value, 8);
15285 break;
15286 case DW_FORM_data2:
15287 *bytes = dwarf2_const_value_data (attr, type, name,
15288 obstack, cu, value, 16);
15289 break;
15290 case DW_FORM_data4:
15291 *bytes = dwarf2_const_value_data (attr, type, name,
15292 obstack, cu, value, 32);
15293 break;
15294 case DW_FORM_data8:
15295 *bytes = dwarf2_const_value_data (attr, type, name,
15296 obstack, cu, value, 64);
15297 break;
15298
15299 case DW_FORM_sdata:
15300 *value = DW_SND (attr);
15301 break;
15302
15303 case DW_FORM_udata:
15304 *value = DW_UNSND (attr);
15305 break;
15306
15307 default:
15308 complaint (&symfile_complaints,
15309 _("unsupported const value attribute form: '%s'"),
15310 dwarf_form_name (attr->form));
15311 *value = 0;
15312 break;
15313 }
15314 }
15315
15316
15317 /* Copy constant value from an attribute to a symbol. */
15318
15319 static void
15320 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15321 struct dwarf2_cu *cu)
15322 {
15323 struct objfile *objfile = cu->objfile;
15324 struct comp_unit_head *cu_header = &cu->header;
15325 LONGEST value;
15326 gdb_byte *bytes;
15327 struct dwarf2_locexpr_baton *baton;
15328
15329 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15330 SYMBOL_PRINT_NAME (sym),
15331 &objfile->objfile_obstack, cu,
15332 &value, &bytes, &baton);
15333
15334 if (baton != NULL)
15335 {
15336 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15337 SYMBOL_LOCATION_BATON (sym) = baton;
15338 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15339 }
15340 else if (bytes != NULL)
15341 {
15342 SYMBOL_VALUE_BYTES (sym) = bytes;
15343 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15344 }
15345 else
15346 {
15347 SYMBOL_VALUE (sym) = value;
15348 SYMBOL_CLASS (sym) = LOC_CONST;
15349 }
15350 }
15351
15352 /* Return the type of the die in question using its DW_AT_type attribute. */
15353
15354 static struct type *
15355 die_type (struct die_info *die, struct dwarf2_cu *cu)
15356 {
15357 struct attribute *type_attr;
15358
15359 type_attr = dwarf2_attr (die, DW_AT_type, cu);
15360 if (!type_attr)
15361 {
15362 /* A missing DW_AT_type represents a void type. */
15363 return objfile_type (cu->objfile)->builtin_void;
15364 }
15365
15366 return lookup_die_type (die, type_attr, cu);
15367 }
15368
15369 /* True iff CU's producer generates GNAT Ada auxiliary information
15370 that allows to find parallel types through that information instead
15371 of having to do expensive parallel lookups by type name. */
15372
15373 static int
15374 need_gnat_info (struct dwarf2_cu *cu)
15375 {
15376 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15377 of GNAT produces this auxiliary information, without any indication
15378 that it is produced. Part of enhancing the FSF version of GNAT
15379 to produce that information will be to put in place an indicator
15380 that we can use in order to determine whether the descriptive type
15381 info is available or not. One suggestion that has been made is
15382 to use a new attribute, attached to the CU die. For now, assume
15383 that the descriptive type info is not available. */
15384 return 0;
15385 }
15386
15387 /* Return the auxiliary type of the die in question using its
15388 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15389 attribute is not present. */
15390
15391 static struct type *
15392 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15393 {
15394 struct attribute *type_attr;
15395
15396 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15397 if (!type_attr)
15398 return NULL;
15399
15400 return lookup_die_type (die, type_attr, cu);
15401 }
15402
15403 /* If DIE has a descriptive_type attribute, then set the TYPE's
15404 descriptive type accordingly. */
15405
15406 static void
15407 set_descriptive_type (struct type *type, struct die_info *die,
15408 struct dwarf2_cu *cu)
15409 {
15410 struct type *descriptive_type = die_descriptive_type (die, cu);
15411
15412 if (descriptive_type)
15413 {
15414 ALLOCATE_GNAT_AUX_TYPE (type);
15415 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15416 }
15417 }
15418
15419 /* Return the containing type of the die in question using its
15420 DW_AT_containing_type attribute. */
15421
15422 static struct type *
15423 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15424 {
15425 struct attribute *type_attr;
15426
15427 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15428 if (!type_attr)
15429 error (_("Dwarf Error: Problem turning containing type into gdb type "
15430 "[in module %s]"), cu->objfile->name);
15431
15432 return lookup_die_type (die, type_attr, cu);
15433 }
15434
15435 /* Look up the type of DIE in CU using its type attribute ATTR.
15436 If there is no type substitute an error marker. */
15437
15438 static struct type *
15439 lookup_die_type (struct die_info *die, struct attribute *attr,
15440 struct dwarf2_cu *cu)
15441 {
15442 struct objfile *objfile = cu->objfile;
15443 struct type *this_type;
15444
15445 /* First see if we have it cached. */
15446
15447 if (attr->form == DW_FORM_GNU_ref_alt)
15448 {
15449 struct dwarf2_per_cu_data *per_cu;
15450 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15451
15452 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15453 this_type = get_die_type_at_offset (offset, per_cu);
15454 }
15455 else if (is_ref_attr (attr))
15456 {
15457 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15458
15459 this_type = get_die_type_at_offset (offset, cu->per_cu);
15460 }
15461 else if (attr->form == DW_FORM_ref_sig8)
15462 {
15463 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15464
15465 /* sig_type will be NULL if the signatured type is missing from
15466 the debug info. */
15467 if (sig_type == NULL)
15468 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15469 "at 0x%x [in module %s]"),
15470 die->offset.sect_off, objfile->name);
15471
15472 gdb_assert (sig_type->per_cu.is_debug_types);
15473 /* If we haven't filled in type_offset_in_section yet, then we
15474 haven't read the type in yet. */
15475 this_type = NULL;
15476 if (sig_type->type_offset_in_section.sect_off != 0)
15477 {
15478 this_type =
15479 get_die_type_at_offset (sig_type->type_offset_in_section,
15480 &sig_type->per_cu);
15481 }
15482 }
15483 else
15484 {
15485 dump_die_for_error (die);
15486 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15487 dwarf_attr_name (attr->name), objfile->name);
15488 }
15489
15490 /* If not cached we need to read it in. */
15491
15492 if (this_type == NULL)
15493 {
15494 struct die_info *type_die;
15495 struct dwarf2_cu *type_cu = cu;
15496
15497 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15498 /* If we found the type now, it's probably because the type came
15499 from an inter-CU reference and the type's CU got expanded before
15500 ours. */
15501 this_type = get_die_type (type_die, type_cu);
15502 if (this_type == NULL)
15503 this_type = read_type_die_1 (type_die, type_cu);
15504 }
15505
15506 /* If we still don't have a type use an error marker. */
15507
15508 if (this_type == NULL)
15509 {
15510 char *message, *saved;
15511
15512 /* read_type_die already issued a complaint. */
15513 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15514 objfile->name,
15515 cu->header.offset.sect_off,
15516 die->offset.sect_off);
15517 saved = obstack_copy0 (&objfile->objfile_obstack,
15518 message, strlen (message));
15519 xfree (message);
15520
15521 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15522 }
15523
15524 return this_type;
15525 }
15526
15527 /* Return the type in DIE, CU.
15528 Returns NULL for invalid types.
15529
15530 This first does a lookup in the appropriate type_hash table,
15531 and only reads the die in if necessary.
15532
15533 NOTE: This can be called when reading in partial or full symbols. */
15534
15535 static struct type *
15536 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15537 {
15538 struct type *this_type;
15539
15540 this_type = get_die_type (die, cu);
15541 if (this_type)
15542 return this_type;
15543
15544 return read_type_die_1 (die, cu);
15545 }
15546
15547 /* Read the type in DIE, CU.
15548 Returns NULL for invalid types. */
15549
15550 static struct type *
15551 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15552 {
15553 struct type *this_type = NULL;
15554
15555 switch (die->tag)
15556 {
15557 case DW_TAG_class_type:
15558 case DW_TAG_interface_type:
15559 case DW_TAG_structure_type:
15560 case DW_TAG_union_type:
15561 this_type = read_structure_type (die, cu);
15562 break;
15563 case DW_TAG_enumeration_type:
15564 this_type = read_enumeration_type (die, cu);
15565 break;
15566 case DW_TAG_subprogram:
15567 case DW_TAG_subroutine_type:
15568 case DW_TAG_inlined_subroutine:
15569 this_type = read_subroutine_type (die, cu);
15570 break;
15571 case DW_TAG_array_type:
15572 this_type = read_array_type (die, cu);
15573 break;
15574 case DW_TAG_set_type:
15575 this_type = read_set_type (die, cu);
15576 break;
15577 case DW_TAG_pointer_type:
15578 this_type = read_tag_pointer_type (die, cu);
15579 break;
15580 case DW_TAG_ptr_to_member_type:
15581 this_type = read_tag_ptr_to_member_type (die, cu);
15582 break;
15583 case DW_TAG_reference_type:
15584 this_type = read_tag_reference_type (die, cu);
15585 break;
15586 case DW_TAG_const_type:
15587 this_type = read_tag_const_type (die, cu);
15588 break;
15589 case DW_TAG_volatile_type:
15590 this_type = read_tag_volatile_type (die, cu);
15591 break;
15592 case DW_TAG_string_type:
15593 this_type = read_tag_string_type (die, cu);
15594 break;
15595 case DW_TAG_typedef:
15596 this_type = read_typedef (die, cu);
15597 break;
15598 case DW_TAG_subrange_type:
15599 this_type = read_subrange_type (die, cu);
15600 break;
15601 case DW_TAG_base_type:
15602 this_type = read_base_type (die, cu);
15603 break;
15604 case DW_TAG_unspecified_type:
15605 this_type = read_unspecified_type (die, cu);
15606 break;
15607 case DW_TAG_namespace:
15608 this_type = read_namespace_type (die, cu);
15609 break;
15610 case DW_TAG_module:
15611 this_type = read_module_type (die, cu);
15612 break;
15613 default:
15614 complaint (&symfile_complaints,
15615 _("unexpected tag in read_type_die: '%s'"),
15616 dwarf_tag_name (die->tag));
15617 break;
15618 }
15619
15620 return this_type;
15621 }
15622
15623 /* See if we can figure out if the class lives in a namespace. We do
15624 this by looking for a member function; its demangled name will
15625 contain namespace info, if there is any.
15626 Return the computed name or NULL.
15627 Space for the result is allocated on the objfile's obstack.
15628 This is the full-die version of guess_partial_die_structure_name.
15629 In this case we know DIE has no useful parent. */
15630
15631 static char *
15632 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15633 {
15634 struct die_info *spec_die;
15635 struct dwarf2_cu *spec_cu;
15636 struct die_info *child;
15637
15638 spec_cu = cu;
15639 spec_die = die_specification (die, &spec_cu);
15640 if (spec_die != NULL)
15641 {
15642 die = spec_die;
15643 cu = spec_cu;
15644 }
15645
15646 for (child = die->child;
15647 child != NULL;
15648 child = child->sibling)
15649 {
15650 if (child->tag == DW_TAG_subprogram)
15651 {
15652 struct attribute *attr;
15653
15654 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15655 if (attr == NULL)
15656 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15657 if (attr != NULL)
15658 {
15659 char *actual_name
15660 = language_class_name_from_physname (cu->language_defn,
15661 DW_STRING (attr));
15662 char *name = NULL;
15663
15664 if (actual_name != NULL)
15665 {
15666 char *die_name = dwarf2_name (die, cu);
15667
15668 if (die_name != NULL
15669 && strcmp (die_name, actual_name) != 0)
15670 {
15671 /* Strip off the class name from the full name.
15672 We want the prefix. */
15673 int die_name_len = strlen (die_name);
15674 int actual_name_len = strlen (actual_name);
15675
15676 /* Test for '::' as a sanity check. */
15677 if (actual_name_len > die_name_len + 2
15678 && actual_name[actual_name_len
15679 - die_name_len - 1] == ':')
15680 name =
15681 obsavestring (actual_name,
15682 actual_name_len - die_name_len - 2,
15683 &cu->objfile->objfile_obstack);
15684 }
15685 }
15686 xfree (actual_name);
15687 return name;
15688 }
15689 }
15690 }
15691
15692 return NULL;
15693 }
15694
15695 /* GCC might emit a nameless typedef that has a linkage name. Determine the
15696 prefix part in such case. See
15697 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15698
15699 static char *
15700 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15701 {
15702 struct attribute *attr;
15703 char *base;
15704
15705 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15706 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15707 return NULL;
15708
15709 attr = dwarf2_attr (die, DW_AT_name, cu);
15710 if (attr != NULL && DW_STRING (attr) != NULL)
15711 return NULL;
15712
15713 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15714 if (attr == NULL)
15715 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15716 if (attr == NULL || DW_STRING (attr) == NULL)
15717 return NULL;
15718
15719 /* dwarf2_name had to be already called. */
15720 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15721
15722 /* Strip the base name, keep any leading namespaces/classes. */
15723 base = strrchr (DW_STRING (attr), ':');
15724 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15725 return "";
15726
15727 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15728 &cu->objfile->objfile_obstack);
15729 }
15730
15731 /* Return the name of the namespace/class that DIE is defined within,
15732 or "" if we can't tell. The caller should not xfree the result.
15733
15734 For example, if we're within the method foo() in the following
15735 code:
15736
15737 namespace N {
15738 class C {
15739 void foo () {
15740 }
15741 };
15742 }
15743
15744 then determine_prefix on foo's die will return "N::C". */
15745
15746 static const char *
15747 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15748 {
15749 struct die_info *parent, *spec_die;
15750 struct dwarf2_cu *spec_cu;
15751 struct type *parent_type;
15752 char *retval;
15753
15754 if (cu->language != language_cplus && cu->language != language_java
15755 && cu->language != language_fortran)
15756 return "";
15757
15758 retval = anonymous_struct_prefix (die, cu);
15759 if (retval)
15760 return retval;
15761
15762 /* We have to be careful in the presence of DW_AT_specification.
15763 For example, with GCC 3.4, given the code
15764
15765 namespace N {
15766 void foo() {
15767 // Definition of N::foo.
15768 }
15769 }
15770
15771 then we'll have a tree of DIEs like this:
15772
15773 1: DW_TAG_compile_unit
15774 2: DW_TAG_namespace // N
15775 3: DW_TAG_subprogram // declaration of N::foo
15776 4: DW_TAG_subprogram // definition of N::foo
15777 DW_AT_specification // refers to die #3
15778
15779 Thus, when processing die #4, we have to pretend that we're in
15780 the context of its DW_AT_specification, namely the contex of die
15781 #3. */
15782 spec_cu = cu;
15783 spec_die = die_specification (die, &spec_cu);
15784 if (spec_die == NULL)
15785 parent = die->parent;
15786 else
15787 {
15788 parent = spec_die->parent;
15789 cu = spec_cu;
15790 }
15791
15792 if (parent == NULL)
15793 return "";
15794 else if (parent->building_fullname)
15795 {
15796 const char *name;
15797 const char *parent_name;
15798
15799 /* It has been seen on RealView 2.2 built binaries,
15800 DW_TAG_template_type_param types actually _defined_ as
15801 children of the parent class:
15802
15803 enum E {};
15804 template class <class Enum> Class{};
15805 Class<enum E> class_e;
15806
15807 1: DW_TAG_class_type (Class)
15808 2: DW_TAG_enumeration_type (E)
15809 3: DW_TAG_enumerator (enum1:0)
15810 3: DW_TAG_enumerator (enum2:1)
15811 ...
15812 2: DW_TAG_template_type_param
15813 DW_AT_type DW_FORM_ref_udata (E)
15814
15815 Besides being broken debug info, it can put GDB into an
15816 infinite loop. Consider:
15817
15818 When we're building the full name for Class<E>, we'll start
15819 at Class, and go look over its template type parameters,
15820 finding E. We'll then try to build the full name of E, and
15821 reach here. We're now trying to build the full name of E,
15822 and look over the parent DIE for containing scope. In the
15823 broken case, if we followed the parent DIE of E, we'd again
15824 find Class, and once again go look at its template type
15825 arguments, etc., etc. Simply don't consider such parent die
15826 as source-level parent of this die (it can't be, the language
15827 doesn't allow it), and break the loop here. */
15828 name = dwarf2_name (die, cu);
15829 parent_name = dwarf2_name (parent, cu);
15830 complaint (&symfile_complaints,
15831 _("template param type '%s' defined within parent '%s'"),
15832 name ? name : "<unknown>",
15833 parent_name ? parent_name : "<unknown>");
15834 return "";
15835 }
15836 else
15837 switch (parent->tag)
15838 {
15839 case DW_TAG_namespace:
15840 parent_type = read_type_die (parent, cu);
15841 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15842 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15843 Work around this problem here. */
15844 if (cu->language == language_cplus
15845 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15846 return "";
15847 /* We give a name to even anonymous namespaces. */
15848 return TYPE_TAG_NAME (parent_type);
15849 case DW_TAG_class_type:
15850 case DW_TAG_interface_type:
15851 case DW_TAG_structure_type:
15852 case DW_TAG_union_type:
15853 case DW_TAG_module:
15854 parent_type = read_type_die (parent, cu);
15855 if (TYPE_TAG_NAME (parent_type) != NULL)
15856 return TYPE_TAG_NAME (parent_type);
15857 else
15858 /* An anonymous structure is only allowed non-static data
15859 members; no typedefs, no member functions, et cetera.
15860 So it does not need a prefix. */
15861 return "";
15862 case DW_TAG_compile_unit:
15863 case DW_TAG_partial_unit:
15864 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15865 if (cu->language == language_cplus
15866 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15867 && die->child != NULL
15868 && (die->tag == DW_TAG_class_type
15869 || die->tag == DW_TAG_structure_type
15870 || die->tag == DW_TAG_union_type))
15871 {
15872 char *name = guess_full_die_structure_name (die, cu);
15873 if (name != NULL)
15874 return name;
15875 }
15876 return "";
15877 default:
15878 return determine_prefix (parent, cu);
15879 }
15880 }
15881
15882 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15883 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15884 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15885 an obconcat, otherwise allocate storage for the result. The CU argument is
15886 used to determine the language and hence, the appropriate separator. */
15887
15888 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
15889
15890 static char *
15891 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15892 int physname, struct dwarf2_cu *cu)
15893 {
15894 const char *lead = "";
15895 const char *sep;
15896
15897 if (suffix == NULL || suffix[0] == '\0'
15898 || prefix == NULL || prefix[0] == '\0')
15899 sep = "";
15900 else if (cu->language == language_java)
15901 sep = ".";
15902 else if (cu->language == language_fortran && physname)
15903 {
15904 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15905 DW_AT_MIPS_linkage_name is preferred and used instead. */
15906
15907 lead = "__";
15908 sep = "_MOD_";
15909 }
15910 else
15911 sep = "::";
15912
15913 if (prefix == NULL)
15914 prefix = "";
15915 if (suffix == NULL)
15916 suffix = "";
15917
15918 if (obs == NULL)
15919 {
15920 char *retval
15921 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15922
15923 strcpy (retval, lead);
15924 strcat (retval, prefix);
15925 strcat (retval, sep);
15926 strcat (retval, suffix);
15927 return retval;
15928 }
15929 else
15930 {
15931 /* We have an obstack. */
15932 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15933 }
15934 }
15935
15936 /* Return sibling of die, NULL if no sibling. */
15937
15938 static struct die_info *
15939 sibling_die (struct die_info *die)
15940 {
15941 return die->sibling;
15942 }
15943
15944 /* Get name of a die, return NULL if not found. */
15945
15946 static char *
15947 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15948 struct obstack *obstack)
15949 {
15950 if (name && cu->language == language_cplus)
15951 {
15952 char *canon_name = cp_canonicalize_string (name);
15953
15954 if (canon_name != NULL)
15955 {
15956 if (strcmp (canon_name, name) != 0)
15957 name = obsavestring (canon_name, strlen (canon_name),
15958 obstack);
15959 xfree (canon_name);
15960 }
15961 }
15962
15963 return name;
15964 }
15965
15966 /* Get name of a die, return NULL if not found. */
15967
15968 static char *
15969 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15970 {
15971 struct attribute *attr;
15972
15973 attr = dwarf2_attr (die, DW_AT_name, cu);
15974 if ((!attr || !DW_STRING (attr))
15975 && die->tag != DW_TAG_class_type
15976 && die->tag != DW_TAG_interface_type
15977 && die->tag != DW_TAG_structure_type
15978 && die->tag != DW_TAG_union_type)
15979 return NULL;
15980
15981 switch (die->tag)
15982 {
15983 case DW_TAG_compile_unit:
15984 case DW_TAG_partial_unit:
15985 /* Compilation units have a DW_AT_name that is a filename, not
15986 a source language identifier. */
15987 case DW_TAG_enumeration_type:
15988 case DW_TAG_enumerator:
15989 /* These tags always have simple identifiers already; no need
15990 to canonicalize them. */
15991 return DW_STRING (attr);
15992
15993 case DW_TAG_subprogram:
15994 /* Java constructors will all be named "<init>", so return
15995 the class name when we see this special case. */
15996 if (cu->language == language_java
15997 && DW_STRING (attr) != NULL
15998 && strcmp (DW_STRING (attr), "<init>") == 0)
15999 {
16000 struct dwarf2_cu *spec_cu = cu;
16001 struct die_info *spec_die;
16002
16003 /* GCJ will output '<init>' for Java constructor names.
16004 For this special case, return the name of the parent class. */
16005
16006 /* GCJ may output suprogram DIEs with AT_specification set.
16007 If so, use the name of the specified DIE. */
16008 spec_die = die_specification (die, &spec_cu);
16009 if (spec_die != NULL)
16010 return dwarf2_name (spec_die, spec_cu);
16011
16012 do
16013 {
16014 die = die->parent;
16015 if (die->tag == DW_TAG_class_type)
16016 return dwarf2_name (die, cu);
16017 }
16018 while (die->tag != DW_TAG_compile_unit
16019 && die->tag != DW_TAG_partial_unit);
16020 }
16021 break;
16022
16023 case DW_TAG_class_type:
16024 case DW_TAG_interface_type:
16025 case DW_TAG_structure_type:
16026 case DW_TAG_union_type:
16027 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16028 structures or unions. These were of the form "._%d" in GCC 4.1,
16029 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16030 and GCC 4.4. We work around this problem by ignoring these. */
16031 if (attr && DW_STRING (attr)
16032 && (strncmp (DW_STRING (attr), "._", 2) == 0
16033 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16034 return NULL;
16035
16036 /* GCC might emit a nameless typedef that has a linkage name. See
16037 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16038 if (!attr || DW_STRING (attr) == NULL)
16039 {
16040 char *demangled = NULL;
16041
16042 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16043 if (attr == NULL)
16044 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16045
16046 if (attr == NULL || DW_STRING (attr) == NULL)
16047 return NULL;
16048
16049 /* Avoid demangling DW_STRING (attr) the second time on a second
16050 call for the same DIE. */
16051 if (!DW_STRING_IS_CANONICAL (attr))
16052 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16053
16054 if (demangled)
16055 {
16056 char *base;
16057
16058 /* FIXME: we already did this for the partial symbol... */
16059 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16060 &cu->objfile->objfile_obstack);
16061 DW_STRING_IS_CANONICAL (attr) = 1;
16062 xfree (demangled);
16063
16064 /* Strip any leading namespaces/classes, keep only the base name.
16065 DW_AT_name for named DIEs does not contain the prefixes. */
16066 base = strrchr (DW_STRING (attr), ':');
16067 if (base && base > DW_STRING (attr) && base[-1] == ':')
16068 return &base[1];
16069 else
16070 return DW_STRING (attr);
16071 }
16072 }
16073 break;
16074
16075 default:
16076 break;
16077 }
16078
16079 if (!DW_STRING_IS_CANONICAL (attr))
16080 {
16081 DW_STRING (attr)
16082 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16083 &cu->objfile->objfile_obstack);
16084 DW_STRING_IS_CANONICAL (attr) = 1;
16085 }
16086 return DW_STRING (attr);
16087 }
16088
16089 /* Return the die that this die in an extension of, or NULL if there
16090 is none. *EXT_CU is the CU containing DIE on input, and the CU
16091 containing the return value on output. */
16092
16093 static struct die_info *
16094 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16095 {
16096 struct attribute *attr;
16097
16098 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16099 if (attr == NULL)
16100 return NULL;
16101
16102 return follow_die_ref (die, attr, ext_cu);
16103 }
16104
16105 /* Convert a DIE tag into its string name. */
16106
16107 static const char *
16108 dwarf_tag_name (unsigned tag)
16109 {
16110 const char *name = get_DW_TAG_name (tag);
16111
16112 if (name == NULL)
16113 return "DW_TAG_<unknown>";
16114
16115 return name;
16116 }
16117
16118 /* Convert a DWARF attribute code into its string name. */
16119
16120 static const char *
16121 dwarf_attr_name (unsigned attr)
16122 {
16123 const char *name;
16124
16125 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16126 if (attr == DW_AT_MIPS_fde)
16127 return "DW_AT_MIPS_fde";
16128 #else
16129 if (attr == DW_AT_HP_block_index)
16130 return "DW_AT_HP_block_index";
16131 #endif
16132
16133 name = get_DW_AT_name (attr);
16134
16135 if (name == NULL)
16136 return "DW_AT_<unknown>";
16137
16138 return name;
16139 }
16140
16141 /* Convert a DWARF value form code into its string name. */
16142
16143 static const char *
16144 dwarf_form_name (unsigned form)
16145 {
16146 const char *name = get_DW_FORM_name (form);
16147
16148 if (name == NULL)
16149 return "DW_FORM_<unknown>";
16150
16151 return name;
16152 }
16153
16154 static char *
16155 dwarf_bool_name (unsigned mybool)
16156 {
16157 if (mybool)
16158 return "TRUE";
16159 else
16160 return "FALSE";
16161 }
16162
16163 /* Convert a DWARF type code into its string name. */
16164
16165 static const char *
16166 dwarf_type_encoding_name (unsigned enc)
16167 {
16168 const char *name = get_DW_ATE_name (enc);
16169
16170 if (name == NULL)
16171 return "DW_ATE_<unknown>";
16172
16173 return name;
16174 }
16175
16176 static void
16177 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
16178 {
16179 unsigned int i;
16180
16181 print_spaces (indent, f);
16182 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
16183 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
16184
16185 if (die->parent != NULL)
16186 {
16187 print_spaces (indent, f);
16188 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
16189 die->parent->offset.sect_off);
16190 }
16191
16192 print_spaces (indent, f);
16193 fprintf_unfiltered (f, " has children: %s\n",
16194 dwarf_bool_name (die->child != NULL));
16195
16196 print_spaces (indent, f);
16197 fprintf_unfiltered (f, " attributes:\n");
16198
16199 for (i = 0; i < die->num_attrs; ++i)
16200 {
16201 print_spaces (indent, f);
16202 fprintf_unfiltered (f, " %s (%s) ",
16203 dwarf_attr_name (die->attrs[i].name),
16204 dwarf_form_name (die->attrs[i].form));
16205
16206 switch (die->attrs[i].form)
16207 {
16208 case DW_FORM_addr:
16209 case DW_FORM_GNU_addr_index:
16210 fprintf_unfiltered (f, "address: ");
16211 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
16212 break;
16213 case DW_FORM_block2:
16214 case DW_FORM_block4:
16215 case DW_FORM_block:
16216 case DW_FORM_block1:
16217 fprintf_unfiltered (f, "block: size %s",
16218 pulongest (DW_BLOCK (&die->attrs[i])->size));
16219 break;
16220 case DW_FORM_exprloc:
16221 fprintf_unfiltered (f, "expression: size %s",
16222 pulongest (DW_BLOCK (&die->attrs[i])->size));
16223 break;
16224 case DW_FORM_ref_addr:
16225 fprintf_unfiltered (f, "ref address: ");
16226 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16227 break;
16228 case DW_FORM_GNU_ref_alt:
16229 fprintf_unfiltered (f, "alt ref address: ");
16230 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16231 break;
16232 case DW_FORM_ref1:
16233 case DW_FORM_ref2:
16234 case DW_FORM_ref4:
16235 case DW_FORM_ref8:
16236 case DW_FORM_ref_udata:
16237 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
16238 (long) (DW_UNSND (&die->attrs[i])));
16239 break;
16240 case DW_FORM_data1:
16241 case DW_FORM_data2:
16242 case DW_FORM_data4:
16243 case DW_FORM_data8:
16244 case DW_FORM_udata:
16245 case DW_FORM_sdata:
16246 fprintf_unfiltered (f, "constant: %s",
16247 pulongest (DW_UNSND (&die->attrs[i])));
16248 break;
16249 case DW_FORM_sec_offset:
16250 fprintf_unfiltered (f, "section offset: %s",
16251 pulongest (DW_UNSND (&die->attrs[i])));
16252 break;
16253 case DW_FORM_ref_sig8:
16254 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16255 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
16256 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
16257 else
16258 fprintf_unfiltered (f, "signatured type, offset: unknown");
16259 break;
16260 case DW_FORM_string:
16261 case DW_FORM_strp:
16262 case DW_FORM_GNU_str_index:
16263 case DW_FORM_GNU_strp_alt:
16264 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
16265 DW_STRING (&die->attrs[i])
16266 ? DW_STRING (&die->attrs[i]) : "",
16267 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
16268 break;
16269 case DW_FORM_flag:
16270 if (DW_UNSND (&die->attrs[i]))
16271 fprintf_unfiltered (f, "flag: TRUE");
16272 else
16273 fprintf_unfiltered (f, "flag: FALSE");
16274 break;
16275 case DW_FORM_flag_present:
16276 fprintf_unfiltered (f, "flag: TRUE");
16277 break;
16278 case DW_FORM_indirect:
16279 /* The reader will have reduced the indirect form to
16280 the "base form" so this form should not occur. */
16281 fprintf_unfiltered (f,
16282 "unexpected attribute form: DW_FORM_indirect");
16283 break;
16284 default:
16285 fprintf_unfiltered (f, "unsupported attribute form: %d.",
16286 die->attrs[i].form);
16287 break;
16288 }
16289 fprintf_unfiltered (f, "\n");
16290 }
16291 }
16292
16293 static void
16294 dump_die_for_error (struct die_info *die)
16295 {
16296 dump_die_shallow (gdb_stderr, 0, die);
16297 }
16298
16299 static void
16300 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16301 {
16302 int indent = level * 4;
16303
16304 gdb_assert (die != NULL);
16305
16306 if (level >= max_level)
16307 return;
16308
16309 dump_die_shallow (f, indent, die);
16310
16311 if (die->child != NULL)
16312 {
16313 print_spaces (indent, f);
16314 fprintf_unfiltered (f, " Children:");
16315 if (level + 1 < max_level)
16316 {
16317 fprintf_unfiltered (f, "\n");
16318 dump_die_1 (f, level + 1, max_level, die->child);
16319 }
16320 else
16321 {
16322 fprintf_unfiltered (f,
16323 " [not printed, max nesting level reached]\n");
16324 }
16325 }
16326
16327 if (die->sibling != NULL && level > 0)
16328 {
16329 dump_die_1 (f, level, max_level, die->sibling);
16330 }
16331 }
16332
16333 /* This is called from the pdie macro in gdbinit.in.
16334 It's not static so gcc will keep a copy callable from gdb. */
16335
16336 void
16337 dump_die (struct die_info *die, int max_level)
16338 {
16339 dump_die_1 (gdb_stdlog, 0, max_level, die);
16340 }
16341
16342 static void
16343 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16344 {
16345 void **slot;
16346
16347 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16348 INSERT);
16349
16350 *slot = die;
16351 }
16352
16353 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16354 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16355
16356 static int
16357 is_ref_attr (struct attribute *attr)
16358 {
16359 switch (attr->form)
16360 {
16361 case DW_FORM_ref_addr:
16362 case DW_FORM_ref1:
16363 case DW_FORM_ref2:
16364 case DW_FORM_ref4:
16365 case DW_FORM_ref8:
16366 case DW_FORM_ref_udata:
16367 case DW_FORM_GNU_ref_alt:
16368 return 1;
16369 default:
16370 return 0;
16371 }
16372 }
16373
16374 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16375 required kind. */
16376
16377 static sect_offset
16378 dwarf2_get_ref_die_offset (struct attribute *attr)
16379 {
16380 sect_offset retval = { DW_UNSND (attr) };
16381
16382 if (is_ref_attr (attr))
16383 return retval;
16384
16385 retval.sect_off = 0;
16386 complaint (&symfile_complaints,
16387 _("unsupported die ref attribute form: '%s'"),
16388 dwarf_form_name (attr->form));
16389 return retval;
16390 }
16391
16392 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16393 * the value held by the attribute is not constant. */
16394
16395 static LONGEST
16396 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16397 {
16398 if (attr->form == DW_FORM_sdata)
16399 return DW_SND (attr);
16400 else if (attr->form == DW_FORM_udata
16401 || attr->form == DW_FORM_data1
16402 || attr->form == DW_FORM_data2
16403 || attr->form == DW_FORM_data4
16404 || attr->form == DW_FORM_data8)
16405 return DW_UNSND (attr);
16406 else
16407 {
16408 complaint (&symfile_complaints,
16409 _("Attribute value is not a constant (%s)"),
16410 dwarf_form_name (attr->form));
16411 return default_value;
16412 }
16413 }
16414
16415 /* Follow reference or signature attribute ATTR of SRC_DIE.
16416 On entry *REF_CU is the CU of SRC_DIE.
16417 On exit *REF_CU is the CU of the result. */
16418
16419 static struct die_info *
16420 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16421 struct dwarf2_cu **ref_cu)
16422 {
16423 struct die_info *die;
16424
16425 if (is_ref_attr (attr))
16426 die = follow_die_ref (src_die, attr, ref_cu);
16427 else if (attr->form == DW_FORM_ref_sig8)
16428 die = follow_die_sig (src_die, attr, ref_cu);
16429 else
16430 {
16431 dump_die_for_error (src_die);
16432 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16433 (*ref_cu)->objfile->name);
16434 }
16435
16436 return die;
16437 }
16438
16439 /* Follow reference OFFSET.
16440 On entry *REF_CU is the CU of the source die referencing OFFSET.
16441 On exit *REF_CU is the CU of the result.
16442 Returns NULL if OFFSET is invalid. */
16443
16444 static struct die_info *
16445 follow_die_offset (sect_offset offset, int offset_in_dwz,
16446 struct dwarf2_cu **ref_cu)
16447 {
16448 struct die_info temp_die;
16449 struct dwarf2_cu *target_cu, *cu = *ref_cu;
16450
16451 gdb_assert (cu->per_cu != NULL);
16452
16453 target_cu = cu;
16454
16455 if (cu->per_cu->is_debug_types)
16456 {
16457 /* .debug_types CUs cannot reference anything outside their CU.
16458 If they need to, they have to reference a signatured type via
16459 DW_FORM_ref_sig8. */
16460 if (! offset_in_cu_p (&cu->header, offset))
16461 return NULL;
16462 }
16463 else if (offset_in_dwz != cu->per_cu->is_dwz
16464 || ! offset_in_cu_p (&cu->header, offset))
16465 {
16466 struct dwarf2_per_cu_data *per_cu;
16467
16468 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16469 cu->objfile);
16470
16471 /* If necessary, add it to the queue and load its DIEs. */
16472 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16473 load_full_comp_unit (per_cu, cu->language);
16474
16475 target_cu = per_cu->cu;
16476 }
16477 else if (cu->dies == NULL)
16478 {
16479 /* We're loading full DIEs during partial symbol reading. */
16480 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16481 load_full_comp_unit (cu->per_cu, language_minimal);
16482 }
16483
16484 *ref_cu = target_cu;
16485 temp_die.offset = offset;
16486 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16487 }
16488
16489 /* Follow reference attribute ATTR of SRC_DIE.
16490 On entry *REF_CU is the CU of SRC_DIE.
16491 On exit *REF_CU is the CU of the result. */
16492
16493 static struct die_info *
16494 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16495 struct dwarf2_cu **ref_cu)
16496 {
16497 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16498 struct dwarf2_cu *cu = *ref_cu;
16499 struct die_info *die;
16500
16501 die = follow_die_offset (offset,
16502 (attr->form == DW_FORM_GNU_ref_alt
16503 || cu->per_cu->is_dwz),
16504 ref_cu);
16505 if (!die)
16506 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16507 "at 0x%x [in module %s]"),
16508 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16509
16510 return die;
16511 }
16512
16513 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16514 Returned value is intended for DW_OP_call*. Returned
16515 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
16516
16517 struct dwarf2_locexpr_baton
16518 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16519 struct dwarf2_per_cu_data *per_cu,
16520 CORE_ADDR (*get_frame_pc) (void *baton),
16521 void *baton)
16522 {
16523 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16524 struct dwarf2_cu *cu;
16525 struct die_info *die;
16526 struct attribute *attr;
16527 struct dwarf2_locexpr_baton retval;
16528
16529 dw2_setup (per_cu->objfile);
16530
16531 if (per_cu->cu == NULL)
16532 load_cu (per_cu);
16533 cu = per_cu->cu;
16534
16535 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
16536 if (!die)
16537 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16538 offset.sect_off, per_cu->objfile->name);
16539
16540 attr = dwarf2_attr (die, DW_AT_location, cu);
16541 if (!attr)
16542 {
16543 /* DWARF: "If there is no such attribute, then there is no effect.".
16544 DATA is ignored if SIZE is 0. */
16545
16546 retval.data = NULL;
16547 retval.size = 0;
16548 }
16549 else if (attr_form_is_section_offset (attr))
16550 {
16551 struct dwarf2_loclist_baton loclist_baton;
16552 CORE_ADDR pc = (*get_frame_pc) (baton);
16553 size_t size;
16554
16555 fill_in_loclist_baton (cu, &loclist_baton, attr);
16556
16557 retval.data = dwarf2_find_location_expression (&loclist_baton,
16558 &size, pc);
16559 retval.size = size;
16560 }
16561 else
16562 {
16563 if (!attr_form_is_block (attr))
16564 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16565 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16566 offset.sect_off, per_cu->objfile->name);
16567
16568 retval.data = DW_BLOCK (attr)->data;
16569 retval.size = DW_BLOCK (attr)->size;
16570 }
16571 retval.per_cu = cu->per_cu;
16572
16573 age_cached_comp_units ();
16574
16575 return retval;
16576 }
16577
16578 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16579 PER_CU. */
16580
16581 struct type *
16582 dwarf2_get_die_type (cu_offset die_offset,
16583 struct dwarf2_per_cu_data *per_cu)
16584 {
16585 sect_offset die_offset_sect;
16586
16587 dw2_setup (per_cu->objfile);
16588
16589 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16590 return get_die_type_at_offset (die_offset_sect, per_cu);
16591 }
16592
16593 /* Follow the signature attribute ATTR in SRC_DIE.
16594 On entry *REF_CU is the CU of SRC_DIE.
16595 On exit *REF_CU is the CU of the result. */
16596
16597 static struct die_info *
16598 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16599 struct dwarf2_cu **ref_cu)
16600 {
16601 struct objfile *objfile = (*ref_cu)->objfile;
16602 struct die_info temp_die;
16603 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16604 struct dwarf2_cu *sig_cu;
16605 struct die_info *die;
16606
16607 /* sig_type will be NULL if the signatured type is missing from
16608 the debug info. */
16609 if (sig_type == NULL)
16610 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16611 "at 0x%x [in module %s]"),
16612 src_die->offset.sect_off, objfile->name);
16613
16614 /* If necessary, add it to the queue and load its DIEs. */
16615
16616 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16617 read_signatured_type (sig_type);
16618
16619 gdb_assert (sig_type->per_cu.cu != NULL);
16620
16621 sig_cu = sig_type->per_cu.cu;
16622 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16623 temp_die.offset = sig_type->type_offset_in_section;
16624 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16625 temp_die.offset.sect_off);
16626 if (die)
16627 {
16628 *ref_cu = sig_cu;
16629 return die;
16630 }
16631
16632 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16633 "from DIE at 0x%x [in module %s]"),
16634 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16635 }
16636
16637 /* Given an offset of a signatured type, return its signatured_type. */
16638
16639 static struct signatured_type *
16640 lookup_signatured_type_at_offset (struct objfile *objfile,
16641 struct dwarf2_section_info *section,
16642 sect_offset offset)
16643 {
16644 gdb_byte *info_ptr = section->buffer + offset.sect_off;
16645 unsigned int length, initial_length_size;
16646 unsigned int sig_offset;
16647 struct signatured_type find_entry, *sig_type;
16648
16649 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16650 sig_offset = (initial_length_size
16651 + 2 /*version*/
16652 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16653 + 1 /*address_size*/);
16654 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16655 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16656
16657 /* This is only used to lookup previously recorded types.
16658 If we didn't find it, it's our bug. */
16659 gdb_assert (sig_type != NULL);
16660 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16661
16662 return sig_type;
16663 }
16664
16665 /* Load the DIEs associated with type unit PER_CU into memory. */
16666
16667 static void
16668 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16669 {
16670 struct signatured_type *sig_type;
16671
16672 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16673 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16674
16675 /* We have the per_cu, but we need the signatured_type.
16676 Fortunately this is an easy translation. */
16677 gdb_assert (per_cu->is_debug_types);
16678 sig_type = (struct signatured_type *) per_cu;
16679
16680 gdb_assert (per_cu->cu == NULL);
16681
16682 read_signatured_type (sig_type);
16683
16684 gdb_assert (per_cu->cu != NULL);
16685 }
16686
16687 /* die_reader_func for read_signatured_type.
16688 This is identical to load_full_comp_unit_reader,
16689 but is kept separate for now. */
16690
16691 static void
16692 read_signatured_type_reader (const struct die_reader_specs *reader,
16693 gdb_byte *info_ptr,
16694 struct die_info *comp_unit_die,
16695 int has_children,
16696 void *data)
16697 {
16698 struct dwarf2_cu *cu = reader->cu;
16699
16700 gdb_assert (cu->die_hash == NULL);
16701 cu->die_hash =
16702 htab_create_alloc_ex (cu->header.length / 12,
16703 die_hash,
16704 die_eq,
16705 NULL,
16706 &cu->comp_unit_obstack,
16707 hashtab_obstack_allocate,
16708 dummy_obstack_deallocate);
16709
16710 if (has_children)
16711 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16712 &info_ptr, comp_unit_die);
16713 cu->dies = comp_unit_die;
16714 /* comp_unit_die is not stored in die_hash, no need. */
16715
16716 /* We try not to read any attributes in this function, because not
16717 all CUs needed for references have been loaded yet, and symbol
16718 table processing isn't initialized. But we have to set the CU language,
16719 or we won't be able to build types correctly.
16720 Similarly, if we do not read the producer, we can not apply
16721 producer-specific interpretation. */
16722 prepare_one_comp_unit (cu, cu->dies, language_minimal);
16723 }
16724
16725 /* Read in a signatured type and build its CU and DIEs.
16726 If the type is a stub for the real type in a DWO file,
16727 read in the real type from the DWO file as well. */
16728
16729 static void
16730 read_signatured_type (struct signatured_type *sig_type)
16731 {
16732 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16733
16734 gdb_assert (per_cu->is_debug_types);
16735 gdb_assert (per_cu->cu == NULL);
16736
16737 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16738 read_signatured_type_reader, NULL);
16739 }
16740
16741 /* Decode simple location descriptions.
16742 Given a pointer to a dwarf block that defines a location, compute
16743 the location and return the value.
16744
16745 NOTE drow/2003-11-18: This function is called in two situations
16746 now: for the address of static or global variables (partial symbols
16747 only) and for offsets into structures which are expected to be
16748 (more or less) constant. The partial symbol case should go away,
16749 and only the constant case should remain. That will let this
16750 function complain more accurately. A few special modes are allowed
16751 without complaint for global variables (for instance, global
16752 register values and thread-local values).
16753
16754 A location description containing no operations indicates that the
16755 object is optimized out. The return value is 0 for that case.
16756 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16757 callers will only want a very basic result and this can become a
16758 complaint.
16759
16760 Note that stack[0] is unused except as a default error return. */
16761
16762 static CORE_ADDR
16763 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16764 {
16765 struct objfile *objfile = cu->objfile;
16766 size_t i;
16767 size_t size = blk->size;
16768 gdb_byte *data = blk->data;
16769 CORE_ADDR stack[64];
16770 int stacki;
16771 unsigned int bytes_read, unsnd;
16772 gdb_byte op;
16773
16774 i = 0;
16775 stacki = 0;
16776 stack[stacki] = 0;
16777 stack[++stacki] = 0;
16778
16779 while (i < size)
16780 {
16781 op = data[i++];
16782 switch (op)
16783 {
16784 case DW_OP_lit0:
16785 case DW_OP_lit1:
16786 case DW_OP_lit2:
16787 case DW_OP_lit3:
16788 case DW_OP_lit4:
16789 case DW_OP_lit5:
16790 case DW_OP_lit6:
16791 case DW_OP_lit7:
16792 case DW_OP_lit8:
16793 case DW_OP_lit9:
16794 case DW_OP_lit10:
16795 case DW_OP_lit11:
16796 case DW_OP_lit12:
16797 case DW_OP_lit13:
16798 case DW_OP_lit14:
16799 case DW_OP_lit15:
16800 case DW_OP_lit16:
16801 case DW_OP_lit17:
16802 case DW_OP_lit18:
16803 case DW_OP_lit19:
16804 case DW_OP_lit20:
16805 case DW_OP_lit21:
16806 case DW_OP_lit22:
16807 case DW_OP_lit23:
16808 case DW_OP_lit24:
16809 case DW_OP_lit25:
16810 case DW_OP_lit26:
16811 case DW_OP_lit27:
16812 case DW_OP_lit28:
16813 case DW_OP_lit29:
16814 case DW_OP_lit30:
16815 case DW_OP_lit31:
16816 stack[++stacki] = op - DW_OP_lit0;
16817 break;
16818
16819 case DW_OP_reg0:
16820 case DW_OP_reg1:
16821 case DW_OP_reg2:
16822 case DW_OP_reg3:
16823 case DW_OP_reg4:
16824 case DW_OP_reg5:
16825 case DW_OP_reg6:
16826 case DW_OP_reg7:
16827 case DW_OP_reg8:
16828 case DW_OP_reg9:
16829 case DW_OP_reg10:
16830 case DW_OP_reg11:
16831 case DW_OP_reg12:
16832 case DW_OP_reg13:
16833 case DW_OP_reg14:
16834 case DW_OP_reg15:
16835 case DW_OP_reg16:
16836 case DW_OP_reg17:
16837 case DW_OP_reg18:
16838 case DW_OP_reg19:
16839 case DW_OP_reg20:
16840 case DW_OP_reg21:
16841 case DW_OP_reg22:
16842 case DW_OP_reg23:
16843 case DW_OP_reg24:
16844 case DW_OP_reg25:
16845 case DW_OP_reg26:
16846 case DW_OP_reg27:
16847 case DW_OP_reg28:
16848 case DW_OP_reg29:
16849 case DW_OP_reg30:
16850 case DW_OP_reg31:
16851 stack[++stacki] = op - DW_OP_reg0;
16852 if (i < size)
16853 dwarf2_complex_location_expr_complaint ();
16854 break;
16855
16856 case DW_OP_regx:
16857 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16858 i += bytes_read;
16859 stack[++stacki] = unsnd;
16860 if (i < size)
16861 dwarf2_complex_location_expr_complaint ();
16862 break;
16863
16864 case DW_OP_addr:
16865 stack[++stacki] = read_address (objfile->obfd, &data[i],
16866 cu, &bytes_read);
16867 i += bytes_read;
16868 break;
16869
16870 case DW_OP_const1u:
16871 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16872 i += 1;
16873 break;
16874
16875 case DW_OP_const1s:
16876 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16877 i += 1;
16878 break;
16879
16880 case DW_OP_const2u:
16881 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16882 i += 2;
16883 break;
16884
16885 case DW_OP_const2s:
16886 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16887 i += 2;
16888 break;
16889
16890 case DW_OP_const4u:
16891 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16892 i += 4;
16893 break;
16894
16895 case DW_OP_const4s:
16896 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16897 i += 4;
16898 break;
16899
16900 case DW_OP_const8u:
16901 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16902 i += 8;
16903 break;
16904
16905 case DW_OP_constu:
16906 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16907 &bytes_read);
16908 i += bytes_read;
16909 break;
16910
16911 case DW_OP_consts:
16912 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16913 i += bytes_read;
16914 break;
16915
16916 case DW_OP_dup:
16917 stack[stacki + 1] = stack[stacki];
16918 stacki++;
16919 break;
16920
16921 case DW_OP_plus:
16922 stack[stacki - 1] += stack[stacki];
16923 stacki--;
16924 break;
16925
16926 case DW_OP_plus_uconst:
16927 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16928 &bytes_read);
16929 i += bytes_read;
16930 break;
16931
16932 case DW_OP_minus:
16933 stack[stacki - 1] -= stack[stacki];
16934 stacki--;
16935 break;
16936
16937 case DW_OP_deref:
16938 /* If we're not the last op, then we definitely can't encode
16939 this using GDB's address_class enum. This is valid for partial
16940 global symbols, although the variable's address will be bogus
16941 in the psymtab. */
16942 if (i < size)
16943 dwarf2_complex_location_expr_complaint ();
16944 break;
16945
16946 case DW_OP_GNU_push_tls_address:
16947 /* The top of the stack has the offset from the beginning
16948 of the thread control block at which the variable is located. */
16949 /* Nothing should follow this operator, so the top of stack would
16950 be returned. */
16951 /* This is valid for partial global symbols, but the variable's
16952 address will be bogus in the psymtab. Make it always at least
16953 non-zero to not look as a variable garbage collected by linker
16954 which have DW_OP_addr 0. */
16955 if (i < size)
16956 dwarf2_complex_location_expr_complaint ();
16957 stack[stacki]++;
16958 break;
16959
16960 case DW_OP_GNU_uninit:
16961 break;
16962
16963 case DW_OP_GNU_addr_index:
16964 case DW_OP_GNU_const_index:
16965 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16966 &bytes_read);
16967 i += bytes_read;
16968 break;
16969
16970 default:
16971 {
16972 const char *name = get_DW_OP_name (op);
16973
16974 if (name)
16975 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16976 name);
16977 else
16978 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16979 op);
16980 }
16981
16982 return (stack[stacki]);
16983 }
16984
16985 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16986 outside of the allocated space. Also enforce minimum>0. */
16987 if (stacki >= ARRAY_SIZE (stack) - 1)
16988 {
16989 complaint (&symfile_complaints,
16990 _("location description stack overflow"));
16991 return 0;
16992 }
16993
16994 if (stacki <= 0)
16995 {
16996 complaint (&symfile_complaints,
16997 _("location description stack underflow"));
16998 return 0;
16999 }
17000 }
17001 return (stack[stacki]);
17002 }
17003
17004 /* memory allocation interface */
17005
17006 static struct dwarf_block *
17007 dwarf_alloc_block (struct dwarf2_cu *cu)
17008 {
17009 struct dwarf_block *blk;
17010
17011 blk = (struct dwarf_block *)
17012 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17013 return (blk);
17014 }
17015
17016 static struct die_info *
17017 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17018 {
17019 struct die_info *die;
17020 size_t size = sizeof (struct die_info);
17021
17022 if (num_attrs > 1)
17023 size += (num_attrs - 1) * sizeof (struct attribute);
17024
17025 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17026 memset (die, 0, sizeof (struct die_info));
17027 return (die);
17028 }
17029
17030 \f
17031 /* Macro support. */
17032
17033 /* Return the full name of file number I in *LH's file name table.
17034 Use COMP_DIR as the name of the current directory of the
17035 compilation. The result is allocated using xmalloc; the caller is
17036 responsible for freeing it. */
17037 static char *
17038 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17039 {
17040 /* Is the file number a valid index into the line header's file name
17041 table? Remember that file numbers start with one, not zero. */
17042 if (1 <= file && file <= lh->num_file_names)
17043 {
17044 struct file_entry *fe = &lh->file_names[file - 1];
17045
17046 if (IS_ABSOLUTE_PATH (fe->name))
17047 return xstrdup (fe->name);
17048 else
17049 {
17050 const char *dir;
17051 int dir_len;
17052 char *full_name;
17053
17054 if (fe->dir_index)
17055 dir = lh->include_dirs[fe->dir_index - 1];
17056 else
17057 dir = comp_dir;
17058
17059 if (dir)
17060 {
17061 dir_len = strlen (dir);
17062 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17063 strcpy (full_name, dir);
17064 full_name[dir_len] = '/';
17065 strcpy (full_name + dir_len + 1, fe->name);
17066 return full_name;
17067 }
17068 else
17069 return xstrdup (fe->name);
17070 }
17071 }
17072 else
17073 {
17074 /* The compiler produced a bogus file number. We can at least
17075 record the macro definitions made in the file, even if we
17076 won't be able to find the file by name. */
17077 char fake_name[80];
17078
17079 sprintf (fake_name, "<bad macro file number %d>", file);
17080
17081 complaint (&symfile_complaints,
17082 _("bad file number in macro information (%d)"),
17083 file);
17084
17085 return xstrdup (fake_name);
17086 }
17087 }
17088
17089
17090 static struct macro_source_file *
17091 macro_start_file (int file, int line,
17092 struct macro_source_file *current_file,
17093 const char *comp_dir,
17094 struct line_header *lh, struct objfile *objfile)
17095 {
17096 /* The full name of this source file. */
17097 char *full_name = file_full_name (file, lh, comp_dir);
17098
17099 /* We don't create a macro table for this compilation unit
17100 at all until we actually get a filename. */
17101 if (! pending_macros)
17102 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17103 objfile->per_bfd->macro_cache);
17104
17105 if (! current_file)
17106 {
17107 /* If we have no current file, then this must be the start_file
17108 directive for the compilation unit's main source file. */
17109 current_file = macro_set_main (pending_macros, full_name);
17110 macro_define_special (pending_macros);
17111 }
17112 else
17113 current_file = macro_include (current_file, line, full_name);
17114
17115 xfree (full_name);
17116
17117 return current_file;
17118 }
17119
17120
17121 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17122 followed by a null byte. */
17123 static char *
17124 copy_string (const char *buf, int len)
17125 {
17126 char *s = xmalloc (len + 1);
17127
17128 memcpy (s, buf, len);
17129 s[len] = '\0';
17130 return s;
17131 }
17132
17133
17134 static const char *
17135 consume_improper_spaces (const char *p, const char *body)
17136 {
17137 if (*p == ' ')
17138 {
17139 complaint (&symfile_complaints,
17140 _("macro definition contains spaces "
17141 "in formal argument list:\n`%s'"),
17142 body);
17143
17144 while (*p == ' ')
17145 p++;
17146 }
17147
17148 return p;
17149 }
17150
17151
17152 static void
17153 parse_macro_definition (struct macro_source_file *file, int line,
17154 const char *body)
17155 {
17156 const char *p;
17157
17158 /* The body string takes one of two forms. For object-like macro
17159 definitions, it should be:
17160
17161 <macro name> " " <definition>
17162
17163 For function-like macro definitions, it should be:
17164
17165 <macro name> "() " <definition>
17166 or
17167 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17168
17169 Spaces may appear only where explicitly indicated, and in the
17170 <definition>.
17171
17172 The Dwarf 2 spec says that an object-like macro's name is always
17173 followed by a space, but versions of GCC around March 2002 omit
17174 the space when the macro's definition is the empty string.
17175
17176 The Dwarf 2 spec says that there should be no spaces between the
17177 formal arguments in a function-like macro's formal argument list,
17178 but versions of GCC around March 2002 include spaces after the
17179 commas. */
17180
17181
17182 /* Find the extent of the macro name. The macro name is terminated
17183 by either a space or null character (for an object-like macro) or
17184 an opening paren (for a function-like macro). */
17185 for (p = body; *p; p++)
17186 if (*p == ' ' || *p == '(')
17187 break;
17188
17189 if (*p == ' ' || *p == '\0')
17190 {
17191 /* It's an object-like macro. */
17192 int name_len = p - body;
17193 char *name = copy_string (body, name_len);
17194 const char *replacement;
17195
17196 if (*p == ' ')
17197 replacement = body + name_len + 1;
17198 else
17199 {
17200 dwarf2_macro_malformed_definition_complaint (body);
17201 replacement = body + name_len;
17202 }
17203
17204 macro_define_object (file, line, name, replacement);
17205
17206 xfree (name);
17207 }
17208 else if (*p == '(')
17209 {
17210 /* It's a function-like macro. */
17211 char *name = copy_string (body, p - body);
17212 int argc = 0;
17213 int argv_size = 1;
17214 char **argv = xmalloc (argv_size * sizeof (*argv));
17215
17216 p++;
17217
17218 p = consume_improper_spaces (p, body);
17219
17220 /* Parse the formal argument list. */
17221 while (*p && *p != ')')
17222 {
17223 /* Find the extent of the current argument name. */
17224 const char *arg_start = p;
17225
17226 while (*p && *p != ',' && *p != ')' && *p != ' ')
17227 p++;
17228
17229 if (! *p || p == arg_start)
17230 dwarf2_macro_malformed_definition_complaint (body);
17231 else
17232 {
17233 /* Make sure argv has room for the new argument. */
17234 if (argc >= argv_size)
17235 {
17236 argv_size *= 2;
17237 argv = xrealloc (argv, argv_size * sizeof (*argv));
17238 }
17239
17240 argv[argc++] = copy_string (arg_start, p - arg_start);
17241 }
17242
17243 p = consume_improper_spaces (p, body);
17244
17245 /* Consume the comma, if present. */
17246 if (*p == ',')
17247 {
17248 p++;
17249
17250 p = consume_improper_spaces (p, body);
17251 }
17252 }
17253
17254 if (*p == ')')
17255 {
17256 p++;
17257
17258 if (*p == ' ')
17259 /* Perfectly formed definition, no complaints. */
17260 macro_define_function (file, line, name,
17261 argc, (const char **) argv,
17262 p + 1);
17263 else if (*p == '\0')
17264 {
17265 /* Complain, but do define it. */
17266 dwarf2_macro_malformed_definition_complaint (body);
17267 macro_define_function (file, line, name,
17268 argc, (const char **) argv,
17269 p);
17270 }
17271 else
17272 /* Just complain. */
17273 dwarf2_macro_malformed_definition_complaint (body);
17274 }
17275 else
17276 /* Just complain. */
17277 dwarf2_macro_malformed_definition_complaint (body);
17278
17279 xfree (name);
17280 {
17281 int i;
17282
17283 for (i = 0; i < argc; i++)
17284 xfree (argv[i]);
17285 }
17286 xfree (argv);
17287 }
17288 else
17289 dwarf2_macro_malformed_definition_complaint (body);
17290 }
17291
17292 /* Skip some bytes from BYTES according to the form given in FORM.
17293 Returns the new pointer. */
17294
17295 static gdb_byte *
17296 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17297 enum dwarf_form form,
17298 unsigned int offset_size,
17299 struct dwarf2_section_info *section)
17300 {
17301 unsigned int bytes_read;
17302
17303 switch (form)
17304 {
17305 case DW_FORM_data1:
17306 case DW_FORM_flag:
17307 ++bytes;
17308 break;
17309
17310 case DW_FORM_data2:
17311 bytes += 2;
17312 break;
17313
17314 case DW_FORM_data4:
17315 bytes += 4;
17316 break;
17317
17318 case DW_FORM_data8:
17319 bytes += 8;
17320 break;
17321
17322 case DW_FORM_string:
17323 read_direct_string (abfd, bytes, &bytes_read);
17324 bytes += bytes_read;
17325 break;
17326
17327 case DW_FORM_sec_offset:
17328 case DW_FORM_strp:
17329 case DW_FORM_GNU_strp_alt:
17330 bytes += offset_size;
17331 break;
17332
17333 case DW_FORM_block:
17334 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17335 bytes += bytes_read;
17336 break;
17337
17338 case DW_FORM_block1:
17339 bytes += 1 + read_1_byte (abfd, bytes);
17340 break;
17341 case DW_FORM_block2:
17342 bytes += 2 + read_2_bytes (abfd, bytes);
17343 break;
17344 case DW_FORM_block4:
17345 bytes += 4 + read_4_bytes (abfd, bytes);
17346 break;
17347
17348 case DW_FORM_sdata:
17349 case DW_FORM_udata:
17350 case DW_FORM_GNU_addr_index:
17351 case DW_FORM_GNU_str_index:
17352 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17353 if (bytes == NULL)
17354 {
17355 dwarf2_section_buffer_overflow_complaint (section);
17356 return NULL;
17357 }
17358 break;
17359
17360 default:
17361 {
17362 complain:
17363 complaint (&symfile_complaints,
17364 _("invalid form 0x%x in `%s'"),
17365 form,
17366 section->asection->name);
17367 return NULL;
17368 }
17369 }
17370
17371 return bytes;
17372 }
17373
17374 /* A helper for dwarf_decode_macros that handles skipping an unknown
17375 opcode. Returns an updated pointer to the macro data buffer; or,
17376 on error, issues a complaint and returns NULL. */
17377
17378 static gdb_byte *
17379 skip_unknown_opcode (unsigned int opcode,
17380 gdb_byte **opcode_definitions,
17381 gdb_byte *mac_ptr, gdb_byte *mac_end,
17382 bfd *abfd,
17383 unsigned int offset_size,
17384 struct dwarf2_section_info *section)
17385 {
17386 unsigned int bytes_read, i;
17387 unsigned long arg;
17388 gdb_byte *defn;
17389
17390 if (opcode_definitions[opcode] == NULL)
17391 {
17392 complaint (&symfile_complaints,
17393 _("unrecognized DW_MACFINO opcode 0x%x"),
17394 opcode);
17395 return NULL;
17396 }
17397
17398 defn = opcode_definitions[opcode];
17399 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17400 defn += bytes_read;
17401
17402 for (i = 0; i < arg; ++i)
17403 {
17404 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17405 section);
17406 if (mac_ptr == NULL)
17407 {
17408 /* skip_form_bytes already issued the complaint. */
17409 return NULL;
17410 }
17411 }
17412
17413 return mac_ptr;
17414 }
17415
17416 /* A helper function which parses the header of a macro section.
17417 If the macro section is the extended (for now called "GNU") type,
17418 then this updates *OFFSET_SIZE. Returns a pointer to just after
17419 the header, or issues a complaint and returns NULL on error. */
17420
17421 static gdb_byte *
17422 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17423 bfd *abfd,
17424 gdb_byte *mac_ptr,
17425 unsigned int *offset_size,
17426 int section_is_gnu)
17427 {
17428 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17429
17430 if (section_is_gnu)
17431 {
17432 unsigned int version, flags;
17433
17434 version = read_2_bytes (abfd, mac_ptr);
17435 if (version != 4)
17436 {
17437 complaint (&symfile_complaints,
17438 _("unrecognized version `%d' in .debug_macro section"),
17439 version);
17440 return NULL;
17441 }
17442 mac_ptr += 2;
17443
17444 flags = read_1_byte (abfd, mac_ptr);
17445 ++mac_ptr;
17446 *offset_size = (flags & 1) ? 8 : 4;
17447
17448 if ((flags & 2) != 0)
17449 /* We don't need the line table offset. */
17450 mac_ptr += *offset_size;
17451
17452 /* Vendor opcode descriptions. */
17453 if ((flags & 4) != 0)
17454 {
17455 unsigned int i, count;
17456
17457 count = read_1_byte (abfd, mac_ptr);
17458 ++mac_ptr;
17459 for (i = 0; i < count; ++i)
17460 {
17461 unsigned int opcode, bytes_read;
17462 unsigned long arg;
17463
17464 opcode = read_1_byte (abfd, mac_ptr);
17465 ++mac_ptr;
17466 opcode_definitions[opcode] = mac_ptr;
17467 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17468 mac_ptr += bytes_read;
17469 mac_ptr += arg;
17470 }
17471 }
17472 }
17473
17474 return mac_ptr;
17475 }
17476
17477 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17478 including DW_MACRO_GNU_transparent_include. */
17479
17480 static void
17481 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17482 struct macro_source_file *current_file,
17483 struct line_header *lh, char *comp_dir,
17484 struct dwarf2_section_info *section,
17485 int section_is_gnu, int section_is_dwz,
17486 unsigned int offset_size,
17487 struct objfile *objfile,
17488 htab_t include_hash)
17489 {
17490 enum dwarf_macro_record_type macinfo_type;
17491 int at_commandline;
17492 gdb_byte *opcode_definitions[256];
17493
17494 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17495 &offset_size, section_is_gnu);
17496 if (mac_ptr == NULL)
17497 {
17498 /* We already issued a complaint. */
17499 return;
17500 }
17501
17502 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17503 GDB is still reading the definitions from command line. First
17504 DW_MACINFO_start_file will need to be ignored as it was already executed
17505 to create CURRENT_FILE for the main source holding also the command line
17506 definitions. On first met DW_MACINFO_start_file this flag is reset to
17507 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17508
17509 at_commandline = 1;
17510
17511 do
17512 {
17513 /* Do we at least have room for a macinfo type byte? */
17514 if (mac_ptr >= mac_end)
17515 {
17516 dwarf2_section_buffer_overflow_complaint (section);
17517 break;
17518 }
17519
17520 macinfo_type = read_1_byte (abfd, mac_ptr);
17521 mac_ptr++;
17522
17523 /* Note that we rely on the fact that the corresponding GNU and
17524 DWARF constants are the same. */
17525 switch (macinfo_type)
17526 {
17527 /* A zero macinfo type indicates the end of the macro
17528 information. */
17529 case 0:
17530 break;
17531
17532 case DW_MACRO_GNU_define:
17533 case DW_MACRO_GNU_undef:
17534 case DW_MACRO_GNU_define_indirect:
17535 case DW_MACRO_GNU_undef_indirect:
17536 case DW_MACRO_GNU_define_indirect_alt:
17537 case DW_MACRO_GNU_undef_indirect_alt:
17538 {
17539 unsigned int bytes_read;
17540 int line;
17541 char *body;
17542 int is_define;
17543
17544 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17545 mac_ptr += bytes_read;
17546
17547 if (macinfo_type == DW_MACRO_GNU_define
17548 || macinfo_type == DW_MACRO_GNU_undef)
17549 {
17550 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17551 mac_ptr += bytes_read;
17552 }
17553 else
17554 {
17555 LONGEST str_offset;
17556
17557 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17558 mac_ptr += offset_size;
17559
17560 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
17561 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17562 || section_is_dwz)
17563 {
17564 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17565
17566 body = read_indirect_string_from_dwz (dwz, str_offset);
17567 }
17568 else
17569 body = read_indirect_string_at_offset (abfd, str_offset);
17570 }
17571
17572 is_define = (macinfo_type == DW_MACRO_GNU_define
17573 || macinfo_type == DW_MACRO_GNU_define_indirect
17574 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
17575 if (! current_file)
17576 {
17577 /* DWARF violation as no main source is present. */
17578 complaint (&symfile_complaints,
17579 _("debug info with no main source gives macro %s "
17580 "on line %d: %s"),
17581 is_define ? _("definition") : _("undefinition"),
17582 line, body);
17583 break;
17584 }
17585 if ((line == 0 && !at_commandline)
17586 || (line != 0 && at_commandline))
17587 complaint (&symfile_complaints,
17588 _("debug info gives %s macro %s with %s line %d: %s"),
17589 at_commandline ? _("command-line") : _("in-file"),
17590 is_define ? _("definition") : _("undefinition"),
17591 line == 0 ? _("zero") : _("non-zero"), line, body);
17592
17593 if (is_define)
17594 parse_macro_definition (current_file, line, body);
17595 else
17596 {
17597 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17598 || macinfo_type == DW_MACRO_GNU_undef_indirect
17599 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
17600 macro_undef (current_file, line, body);
17601 }
17602 }
17603 break;
17604
17605 case DW_MACRO_GNU_start_file:
17606 {
17607 unsigned int bytes_read;
17608 int line, file;
17609
17610 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17611 mac_ptr += bytes_read;
17612 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17613 mac_ptr += bytes_read;
17614
17615 if ((line == 0 && !at_commandline)
17616 || (line != 0 && at_commandline))
17617 complaint (&symfile_complaints,
17618 _("debug info gives source %d included "
17619 "from %s at %s line %d"),
17620 file, at_commandline ? _("command-line") : _("file"),
17621 line == 0 ? _("zero") : _("non-zero"), line);
17622
17623 if (at_commandline)
17624 {
17625 /* This DW_MACRO_GNU_start_file was executed in the
17626 pass one. */
17627 at_commandline = 0;
17628 }
17629 else
17630 current_file = macro_start_file (file, line,
17631 current_file, comp_dir,
17632 lh, objfile);
17633 }
17634 break;
17635
17636 case DW_MACRO_GNU_end_file:
17637 if (! current_file)
17638 complaint (&symfile_complaints,
17639 _("macro debug info has an unmatched "
17640 "`close_file' directive"));
17641 else
17642 {
17643 current_file = current_file->included_by;
17644 if (! current_file)
17645 {
17646 enum dwarf_macro_record_type next_type;
17647
17648 /* GCC circa March 2002 doesn't produce the zero
17649 type byte marking the end of the compilation
17650 unit. Complain if it's not there, but exit no
17651 matter what. */
17652
17653 /* Do we at least have room for a macinfo type byte? */
17654 if (mac_ptr >= mac_end)
17655 {
17656 dwarf2_section_buffer_overflow_complaint (section);
17657 return;
17658 }
17659
17660 /* We don't increment mac_ptr here, so this is just
17661 a look-ahead. */
17662 next_type = read_1_byte (abfd, mac_ptr);
17663 if (next_type != 0)
17664 complaint (&symfile_complaints,
17665 _("no terminating 0-type entry for "
17666 "macros in `.debug_macinfo' section"));
17667
17668 return;
17669 }
17670 }
17671 break;
17672
17673 case DW_MACRO_GNU_transparent_include:
17674 case DW_MACRO_GNU_transparent_include_alt:
17675 {
17676 LONGEST offset;
17677 void **slot;
17678 bfd *include_bfd = abfd;
17679 struct dwarf2_section_info *include_section = section;
17680 struct dwarf2_section_info alt_section;
17681 gdb_byte *include_mac_end = mac_end;
17682 int is_dwz = section_is_dwz;
17683 gdb_byte *new_mac_ptr;
17684
17685 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17686 mac_ptr += offset_size;
17687
17688 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17689 {
17690 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17691
17692 dwarf2_read_section (dwarf2_per_objfile->objfile,
17693 &dwz->macro);
17694
17695 include_bfd = dwz->macro.asection->owner;
17696 include_section = &dwz->macro;
17697 include_mac_end = dwz->macro.buffer + dwz->macro.size;
17698 is_dwz = 1;
17699 }
17700
17701 new_mac_ptr = include_section->buffer + offset;
17702 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
17703
17704 if (*slot != NULL)
17705 {
17706 /* This has actually happened; see
17707 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17708 complaint (&symfile_complaints,
17709 _("recursive DW_MACRO_GNU_transparent_include in "
17710 ".debug_macro section"));
17711 }
17712 else
17713 {
17714 *slot = new_mac_ptr;
17715
17716 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
17717 include_mac_end, current_file,
17718 lh, comp_dir,
17719 section, section_is_gnu, is_dwz,
17720 offset_size, objfile, include_hash);
17721
17722 htab_remove_elt (include_hash, new_mac_ptr);
17723 }
17724 }
17725 break;
17726
17727 case DW_MACINFO_vendor_ext:
17728 if (!section_is_gnu)
17729 {
17730 unsigned int bytes_read;
17731 int constant;
17732
17733 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17734 mac_ptr += bytes_read;
17735 read_direct_string (abfd, mac_ptr, &bytes_read);
17736 mac_ptr += bytes_read;
17737
17738 /* We don't recognize any vendor extensions. */
17739 break;
17740 }
17741 /* FALLTHROUGH */
17742
17743 default:
17744 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17745 mac_ptr, mac_end, abfd, offset_size,
17746 section);
17747 if (mac_ptr == NULL)
17748 return;
17749 break;
17750 }
17751 } while (macinfo_type != 0);
17752 }
17753
17754 static void
17755 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17756 char *comp_dir, int section_is_gnu)
17757 {
17758 struct objfile *objfile = dwarf2_per_objfile->objfile;
17759 struct line_header *lh = cu->line_header;
17760 bfd *abfd;
17761 gdb_byte *mac_ptr, *mac_end;
17762 struct macro_source_file *current_file = 0;
17763 enum dwarf_macro_record_type macinfo_type;
17764 unsigned int offset_size = cu->header.offset_size;
17765 gdb_byte *opcode_definitions[256];
17766 struct cleanup *cleanup;
17767 htab_t include_hash;
17768 void **slot;
17769 struct dwarf2_section_info *section;
17770 const char *section_name;
17771
17772 if (cu->dwo_unit != NULL)
17773 {
17774 if (section_is_gnu)
17775 {
17776 section = &cu->dwo_unit->dwo_file->sections.macro;
17777 section_name = ".debug_macro.dwo";
17778 }
17779 else
17780 {
17781 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17782 section_name = ".debug_macinfo.dwo";
17783 }
17784 }
17785 else
17786 {
17787 if (section_is_gnu)
17788 {
17789 section = &dwarf2_per_objfile->macro;
17790 section_name = ".debug_macro";
17791 }
17792 else
17793 {
17794 section = &dwarf2_per_objfile->macinfo;
17795 section_name = ".debug_macinfo";
17796 }
17797 }
17798
17799 dwarf2_read_section (objfile, section);
17800 if (section->buffer == NULL)
17801 {
17802 complaint (&symfile_complaints, _("missing %s section"), section_name);
17803 return;
17804 }
17805 abfd = section->asection->owner;
17806
17807 /* First pass: Find the name of the base filename.
17808 This filename is needed in order to process all macros whose definition
17809 (or undefinition) comes from the command line. These macros are defined
17810 before the first DW_MACINFO_start_file entry, and yet still need to be
17811 associated to the base file.
17812
17813 To determine the base file name, we scan the macro definitions until we
17814 reach the first DW_MACINFO_start_file entry. We then initialize
17815 CURRENT_FILE accordingly so that any macro definition found before the
17816 first DW_MACINFO_start_file can still be associated to the base file. */
17817
17818 mac_ptr = section->buffer + offset;
17819 mac_end = section->buffer + section->size;
17820
17821 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17822 &offset_size, section_is_gnu);
17823 if (mac_ptr == NULL)
17824 {
17825 /* We already issued a complaint. */
17826 return;
17827 }
17828
17829 do
17830 {
17831 /* Do we at least have room for a macinfo type byte? */
17832 if (mac_ptr >= mac_end)
17833 {
17834 /* Complaint is printed during the second pass as GDB will probably
17835 stop the first pass earlier upon finding
17836 DW_MACINFO_start_file. */
17837 break;
17838 }
17839
17840 macinfo_type = read_1_byte (abfd, mac_ptr);
17841 mac_ptr++;
17842
17843 /* Note that we rely on the fact that the corresponding GNU and
17844 DWARF constants are the same. */
17845 switch (macinfo_type)
17846 {
17847 /* A zero macinfo type indicates the end of the macro
17848 information. */
17849 case 0:
17850 break;
17851
17852 case DW_MACRO_GNU_define:
17853 case DW_MACRO_GNU_undef:
17854 /* Only skip the data by MAC_PTR. */
17855 {
17856 unsigned int bytes_read;
17857
17858 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17859 mac_ptr += bytes_read;
17860 read_direct_string (abfd, mac_ptr, &bytes_read);
17861 mac_ptr += bytes_read;
17862 }
17863 break;
17864
17865 case DW_MACRO_GNU_start_file:
17866 {
17867 unsigned int bytes_read;
17868 int line, file;
17869
17870 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17871 mac_ptr += bytes_read;
17872 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17873 mac_ptr += bytes_read;
17874
17875 current_file = macro_start_file (file, line, current_file,
17876 comp_dir, lh, objfile);
17877 }
17878 break;
17879
17880 case DW_MACRO_GNU_end_file:
17881 /* No data to skip by MAC_PTR. */
17882 break;
17883
17884 case DW_MACRO_GNU_define_indirect:
17885 case DW_MACRO_GNU_undef_indirect:
17886 case DW_MACRO_GNU_define_indirect_alt:
17887 case DW_MACRO_GNU_undef_indirect_alt:
17888 {
17889 unsigned int bytes_read;
17890
17891 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17892 mac_ptr += bytes_read;
17893 mac_ptr += offset_size;
17894 }
17895 break;
17896
17897 case DW_MACRO_GNU_transparent_include:
17898 case DW_MACRO_GNU_transparent_include_alt:
17899 /* Note that, according to the spec, a transparent include
17900 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17901 skip this opcode. */
17902 mac_ptr += offset_size;
17903 break;
17904
17905 case DW_MACINFO_vendor_ext:
17906 /* Only skip the data by MAC_PTR. */
17907 if (!section_is_gnu)
17908 {
17909 unsigned int bytes_read;
17910
17911 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17912 mac_ptr += bytes_read;
17913 read_direct_string (abfd, mac_ptr, &bytes_read);
17914 mac_ptr += bytes_read;
17915 }
17916 /* FALLTHROUGH */
17917
17918 default:
17919 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17920 mac_ptr, mac_end, abfd, offset_size,
17921 section);
17922 if (mac_ptr == NULL)
17923 return;
17924 break;
17925 }
17926 } while (macinfo_type != 0 && current_file == NULL);
17927
17928 /* Second pass: Process all entries.
17929
17930 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17931 command-line macro definitions/undefinitions. This flag is unset when we
17932 reach the first DW_MACINFO_start_file entry. */
17933
17934 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17935 NULL, xcalloc, xfree);
17936 cleanup = make_cleanup_htab_delete (include_hash);
17937 mac_ptr = section->buffer + offset;
17938 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17939 *slot = mac_ptr;
17940 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17941 current_file, lh, comp_dir, section,
17942 section_is_gnu, 0,
17943 offset_size, objfile, include_hash);
17944 do_cleanups (cleanup);
17945 }
17946
17947 /* Check if the attribute's form is a DW_FORM_block*
17948 if so return true else false. */
17949
17950 static int
17951 attr_form_is_block (struct attribute *attr)
17952 {
17953 return (attr == NULL ? 0 :
17954 attr->form == DW_FORM_block1
17955 || attr->form == DW_FORM_block2
17956 || attr->form == DW_FORM_block4
17957 || attr->form == DW_FORM_block
17958 || attr->form == DW_FORM_exprloc);
17959 }
17960
17961 /* Return non-zero if ATTR's value is a section offset --- classes
17962 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17963 You may use DW_UNSND (attr) to retrieve such offsets.
17964
17965 Section 7.5.4, "Attribute Encodings", explains that no attribute
17966 may have a value that belongs to more than one of these classes; it
17967 would be ambiguous if we did, because we use the same forms for all
17968 of them. */
17969
17970 static int
17971 attr_form_is_section_offset (struct attribute *attr)
17972 {
17973 return (attr->form == DW_FORM_data4
17974 || attr->form == DW_FORM_data8
17975 || attr->form == DW_FORM_sec_offset);
17976 }
17977
17978 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17979 zero otherwise. When this function returns true, you can apply
17980 dwarf2_get_attr_constant_value to it.
17981
17982 However, note that for some attributes you must check
17983 attr_form_is_section_offset before using this test. DW_FORM_data4
17984 and DW_FORM_data8 are members of both the constant class, and of
17985 the classes that contain offsets into other debug sections
17986 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17987 that, if an attribute's can be either a constant or one of the
17988 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17989 taken as section offsets, not constants. */
17990
17991 static int
17992 attr_form_is_constant (struct attribute *attr)
17993 {
17994 switch (attr->form)
17995 {
17996 case DW_FORM_sdata:
17997 case DW_FORM_udata:
17998 case DW_FORM_data1:
17999 case DW_FORM_data2:
18000 case DW_FORM_data4:
18001 case DW_FORM_data8:
18002 return 1;
18003 default:
18004 return 0;
18005 }
18006 }
18007
18008 /* Return the .debug_loc section to use for CU.
18009 For DWO files use .debug_loc.dwo. */
18010
18011 static struct dwarf2_section_info *
18012 cu_debug_loc_section (struct dwarf2_cu *cu)
18013 {
18014 if (cu->dwo_unit)
18015 return &cu->dwo_unit->dwo_file->sections.loc;
18016 return &dwarf2_per_objfile->loc;
18017 }
18018
18019 /* A helper function that fills in a dwarf2_loclist_baton. */
18020
18021 static void
18022 fill_in_loclist_baton (struct dwarf2_cu *cu,
18023 struct dwarf2_loclist_baton *baton,
18024 struct attribute *attr)
18025 {
18026 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18027
18028 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18029
18030 baton->per_cu = cu->per_cu;
18031 gdb_assert (baton->per_cu);
18032 /* We don't know how long the location list is, but make sure we
18033 don't run off the edge of the section. */
18034 baton->size = section->size - DW_UNSND (attr);
18035 baton->data = section->buffer + DW_UNSND (attr);
18036 baton->base_address = cu->base_address;
18037 baton->from_dwo = cu->dwo_unit != NULL;
18038 }
18039
18040 static void
18041 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18042 struct dwarf2_cu *cu)
18043 {
18044 struct objfile *objfile = dwarf2_per_objfile->objfile;
18045 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18046
18047 if (attr_form_is_section_offset (attr)
18048 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18049 the section. If so, fall through to the complaint in the
18050 other branch. */
18051 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18052 {
18053 struct dwarf2_loclist_baton *baton;
18054
18055 baton = obstack_alloc (&objfile->objfile_obstack,
18056 sizeof (struct dwarf2_loclist_baton));
18057
18058 fill_in_loclist_baton (cu, baton, attr);
18059
18060 if (cu->base_known == 0)
18061 complaint (&symfile_complaints,
18062 _("Location list used without "
18063 "specifying the CU base address."));
18064
18065 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18066 SYMBOL_LOCATION_BATON (sym) = baton;
18067 }
18068 else
18069 {
18070 struct dwarf2_locexpr_baton *baton;
18071
18072 baton = obstack_alloc (&objfile->objfile_obstack,
18073 sizeof (struct dwarf2_locexpr_baton));
18074 baton->per_cu = cu->per_cu;
18075 gdb_assert (baton->per_cu);
18076
18077 if (attr_form_is_block (attr))
18078 {
18079 /* Note that we're just copying the block's data pointer
18080 here, not the actual data. We're still pointing into the
18081 info_buffer for SYM's objfile; right now we never release
18082 that buffer, but when we do clean up properly this may
18083 need to change. */
18084 baton->size = DW_BLOCK (attr)->size;
18085 baton->data = DW_BLOCK (attr)->data;
18086 }
18087 else
18088 {
18089 dwarf2_invalid_attrib_class_complaint ("location description",
18090 SYMBOL_NATURAL_NAME (sym));
18091 baton->size = 0;
18092 }
18093
18094 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18095 SYMBOL_LOCATION_BATON (sym) = baton;
18096 }
18097 }
18098
18099 /* Return the OBJFILE associated with the compilation unit CU. If CU
18100 came from a separate debuginfo file, then the master objfile is
18101 returned. */
18102
18103 struct objfile *
18104 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18105 {
18106 struct objfile *objfile = per_cu->objfile;
18107
18108 /* Return the master objfile, so that we can report and look up the
18109 correct file containing this variable. */
18110 if (objfile->separate_debug_objfile_backlink)
18111 objfile = objfile->separate_debug_objfile_backlink;
18112
18113 return objfile;
18114 }
18115
18116 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18117 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18118 CU_HEADERP first. */
18119
18120 static const struct comp_unit_head *
18121 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18122 struct dwarf2_per_cu_data *per_cu)
18123 {
18124 gdb_byte *info_ptr;
18125
18126 if (per_cu->cu)
18127 return &per_cu->cu->header;
18128
18129 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18130
18131 memset (cu_headerp, 0, sizeof (*cu_headerp));
18132 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18133
18134 return cu_headerp;
18135 }
18136
18137 /* Return the address size given in the compilation unit header for CU. */
18138
18139 int
18140 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18141 {
18142 struct comp_unit_head cu_header_local;
18143 const struct comp_unit_head *cu_headerp;
18144
18145 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18146
18147 return cu_headerp->addr_size;
18148 }
18149
18150 /* Return the offset size given in the compilation unit header for CU. */
18151
18152 int
18153 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18154 {
18155 struct comp_unit_head cu_header_local;
18156 const struct comp_unit_head *cu_headerp;
18157
18158 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18159
18160 return cu_headerp->offset_size;
18161 }
18162
18163 /* See its dwarf2loc.h declaration. */
18164
18165 int
18166 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18167 {
18168 struct comp_unit_head cu_header_local;
18169 const struct comp_unit_head *cu_headerp;
18170
18171 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18172
18173 if (cu_headerp->version == 2)
18174 return cu_headerp->addr_size;
18175 else
18176 return cu_headerp->offset_size;
18177 }
18178
18179 /* Return the text offset of the CU. The returned offset comes from
18180 this CU's objfile. If this objfile came from a separate debuginfo
18181 file, then the offset may be different from the corresponding
18182 offset in the parent objfile. */
18183
18184 CORE_ADDR
18185 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
18186 {
18187 struct objfile *objfile = per_cu->objfile;
18188
18189 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18190 }
18191
18192 /* Locate the .debug_info compilation unit from CU's objfile which contains
18193 the DIE at OFFSET. Raises an error on failure. */
18194
18195 static struct dwarf2_per_cu_data *
18196 dwarf2_find_containing_comp_unit (sect_offset offset,
18197 unsigned int offset_in_dwz,
18198 struct objfile *objfile)
18199 {
18200 struct dwarf2_per_cu_data *this_cu;
18201 int low, high;
18202 const sect_offset *cu_off;
18203
18204 low = 0;
18205 high = dwarf2_per_objfile->n_comp_units - 1;
18206 while (high > low)
18207 {
18208 struct dwarf2_per_cu_data *mid_cu;
18209 int mid = low + (high - low) / 2;
18210
18211 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
18212 cu_off = &mid_cu->offset;
18213 if (mid_cu->is_dwz > offset_in_dwz
18214 || (mid_cu->is_dwz == offset_in_dwz
18215 && cu_off->sect_off >= offset.sect_off))
18216 high = mid;
18217 else
18218 low = mid + 1;
18219 }
18220 gdb_assert (low == high);
18221 this_cu = dwarf2_per_objfile->all_comp_units[low];
18222 cu_off = &this_cu->offset;
18223 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
18224 {
18225 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
18226 error (_("Dwarf Error: could not find partial DIE containing "
18227 "offset 0x%lx [in module %s]"),
18228 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
18229
18230 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
18231 <= offset.sect_off);
18232 return dwarf2_per_objfile->all_comp_units[low-1];
18233 }
18234 else
18235 {
18236 this_cu = dwarf2_per_objfile->all_comp_units[low];
18237 if (low == dwarf2_per_objfile->n_comp_units - 1
18238 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
18239 error (_("invalid dwarf2 offset %u"), offset.sect_off);
18240 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
18241 return this_cu;
18242 }
18243 }
18244
18245 /* Initialize dwarf2_cu CU, owned by PER_CU. */
18246
18247 static void
18248 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
18249 {
18250 memset (cu, 0, sizeof (*cu));
18251 per_cu->cu = cu;
18252 cu->per_cu = per_cu;
18253 cu->objfile = per_cu->objfile;
18254 obstack_init (&cu->comp_unit_obstack);
18255 }
18256
18257 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
18258
18259 static void
18260 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
18261 enum language pretend_language)
18262 {
18263 struct attribute *attr;
18264
18265 /* Set the language we're debugging. */
18266 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
18267 if (attr)
18268 set_cu_language (DW_UNSND (attr), cu);
18269 else
18270 {
18271 cu->language = pretend_language;
18272 cu->language_defn = language_def (cu->language);
18273 }
18274
18275 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
18276 if (attr)
18277 cu->producer = DW_STRING (attr);
18278 }
18279
18280 /* Release one cached compilation unit, CU. We unlink it from the tree
18281 of compilation units, but we don't remove it from the read_in_chain;
18282 the caller is responsible for that.
18283 NOTE: DATA is a void * because this function is also used as a
18284 cleanup routine. */
18285
18286 static void
18287 free_heap_comp_unit (void *data)
18288 {
18289 struct dwarf2_cu *cu = data;
18290
18291 gdb_assert (cu->per_cu != NULL);
18292 cu->per_cu->cu = NULL;
18293 cu->per_cu = NULL;
18294
18295 obstack_free (&cu->comp_unit_obstack, NULL);
18296
18297 xfree (cu);
18298 }
18299
18300 /* This cleanup function is passed the address of a dwarf2_cu on the stack
18301 when we're finished with it. We can't free the pointer itself, but be
18302 sure to unlink it from the cache. Also release any associated storage. */
18303
18304 static void
18305 free_stack_comp_unit (void *data)
18306 {
18307 struct dwarf2_cu *cu = data;
18308
18309 gdb_assert (cu->per_cu != NULL);
18310 cu->per_cu->cu = NULL;
18311 cu->per_cu = NULL;
18312
18313 obstack_free (&cu->comp_unit_obstack, NULL);
18314 cu->partial_dies = NULL;
18315 }
18316
18317 /* Free all cached compilation units. */
18318
18319 static void
18320 free_cached_comp_units (void *data)
18321 {
18322 struct dwarf2_per_cu_data *per_cu, **last_chain;
18323
18324 per_cu = dwarf2_per_objfile->read_in_chain;
18325 last_chain = &dwarf2_per_objfile->read_in_chain;
18326 while (per_cu != NULL)
18327 {
18328 struct dwarf2_per_cu_data *next_cu;
18329
18330 next_cu = per_cu->cu->read_in_chain;
18331
18332 free_heap_comp_unit (per_cu->cu);
18333 *last_chain = next_cu;
18334
18335 per_cu = next_cu;
18336 }
18337 }
18338
18339 /* Increase the age counter on each cached compilation unit, and free
18340 any that are too old. */
18341
18342 static void
18343 age_cached_comp_units (void)
18344 {
18345 struct dwarf2_per_cu_data *per_cu, **last_chain;
18346
18347 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18348 per_cu = dwarf2_per_objfile->read_in_chain;
18349 while (per_cu != NULL)
18350 {
18351 per_cu->cu->last_used ++;
18352 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18353 dwarf2_mark (per_cu->cu);
18354 per_cu = per_cu->cu->read_in_chain;
18355 }
18356
18357 per_cu = dwarf2_per_objfile->read_in_chain;
18358 last_chain = &dwarf2_per_objfile->read_in_chain;
18359 while (per_cu != NULL)
18360 {
18361 struct dwarf2_per_cu_data *next_cu;
18362
18363 next_cu = per_cu->cu->read_in_chain;
18364
18365 if (!per_cu->cu->mark)
18366 {
18367 free_heap_comp_unit (per_cu->cu);
18368 *last_chain = next_cu;
18369 }
18370 else
18371 last_chain = &per_cu->cu->read_in_chain;
18372
18373 per_cu = next_cu;
18374 }
18375 }
18376
18377 /* Remove a single compilation unit from the cache. */
18378
18379 static void
18380 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
18381 {
18382 struct dwarf2_per_cu_data *per_cu, **last_chain;
18383
18384 per_cu = dwarf2_per_objfile->read_in_chain;
18385 last_chain = &dwarf2_per_objfile->read_in_chain;
18386 while (per_cu != NULL)
18387 {
18388 struct dwarf2_per_cu_data *next_cu;
18389
18390 next_cu = per_cu->cu->read_in_chain;
18391
18392 if (per_cu == target_per_cu)
18393 {
18394 free_heap_comp_unit (per_cu->cu);
18395 per_cu->cu = NULL;
18396 *last_chain = next_cu;
18397 break;
18398 }
18399 else
18400 last_chain = &per_cu->cu->read_in_chain;
18401
18402 per_cu = next_cu;
18403 }
18404 }
18405
18406 /* Release all extra memory associated with OBJFILE. */
18407
18408 void
18409 dwarf2_free_objfile (struct objfile *objfile)
18410 {
18411 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18412
18413 if (dwarf2_per_objfile == NULL)
18414 return;
18415
18416 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18417 free_cached_comp_units (NULL);
18418
18419 if (dwarf2_per_objfile->quick_file_names_table)
18420 htab_delete (dwarf2_per_objfile->quick_file_names_table);
18421
18422 /* Everything else should be on the objfile obstack. */
18423 }
18424
18425 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18426 We store these in a hash table separate from the DIEs, and preserve them
18427 when the DIEs are flushed out of cache.
18428
18429 The CU "per_cu" pointer is needed because offset alone is not enough to
18430 uniquely identify the type. A file may have multiple .debug_types sections,
18431 or the type may come from a DWO file. We have to use something in
18432 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18433 routine, get_die_type_at_offset, from outside this file, and thus won't
18434 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18435 of the objfile. */
18436
18437 struct dwarf2_per_cu_offset_and_type
18438 {
18439 const struct dwarf2_per_cu_data *per_cu;
18440 sect_offset offset;
18441 struct type *type;
18442 };
18443
18444 /* Hash function for a dwarf2_per_cu_offset_and_type. */
18445
18446 static hashval_t
18447 per_cu_offset_and_type_hash (const void *item)
18448 {
18449 const struct dwarf2_per_cu_offset_and_type *ofs = item;
18450
18451 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
18452 }
18453
18454 /* Equality function for a dwarf2_per_cu_offset_and_type. */
18455
18456 static int
18457 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
18458 {
18459 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18460 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
18461
18462 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18463 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18464 }
18465
18466 /* Set the type associated with DIE to TYPE. Save it in CU's hash
18467 table if necessary. For convenience, return TYPE.
18468
18469 The DIEs reading must have careful ordering to:
18470 * Not cause infite loops trying to read in DIEs as a prerequisite for
18471 reading current DIE.
18472 * Not trying to dereference contents of still incompletely read in types
18473 while reading in other DIEs.
18474 * Enable referencing still incompletely read in types just by a pointer to
18475 the type without accessing its fields.
18476
18477 Therefore caller should follow these rules:
18478 * Try to fetch any prerequisite types we may need to build this DIE type
18479 before building the type and calling set_die_type.
18480 * After building type call set_die_type for current DIE as soon as
18481 possible before fetching more types to complete the current type.
18482 * Make the type as complete as possible before fetching more types. */
18483
18484 static struct type *
18485 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18486 {
18487 struct dwarf2_per_cu_offset_and_type **slot, ofs;
18488 struct objfile *objfile = cu->objfile;
18489
18490 /* For Ada types, make sure that the gnat-specific data is always
18491 initialized (if not already set). There are a few types where
18492 we should not be doing so, because the type-specific area is
18493 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18494 where the type-specific area is used to store the floatformat).
18495 But this is not a problem, because the gnat-specific information
18496 is actually not needed for these types. */
18497 if (need_gnat_info (cu)
18498 && TYPE_CODE (type) != TYPE_CODE_FUNC
18499 && TYPE_CODE (type) != TYPE_CODE_FLT
18500 && !HAVE_GNAT_AUX_INFO (type))
18501 INIT_GNAT_SPECIFIC (type);
18502
18503 if (dwarf2_per_objfile->die_type_hash == NULL)
18504 {
18505 dwarf2_per_objfile->die_type_hash =
18506 htab_create_alloc_ex (127,
18507 per_cu_offset_and_type_hash,
18508 per_cu_offset_and_type_eq,
18509 NULL,
18510 &objfile->objfile_obstack,
18511 hashtab_obstack_allocate,
18512 dummy_obstack_deallocate);
18513 }
18514
18515 ofs.per_cu = cu->per_cu;
18516 ofs.offset = die->offset;
18517 ofs.type = type;
18518 slot = (struct dwarf2_per_cu_offset_and_type **)
18519 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18520 if (*slot)
18521 complaint (&symfile_complaints,
18522 _("A problem internal to GDB: DIE 0x%x has type already set"),
18523 die->offset.sect_off);
18524 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18525 **slot = ofs;
18526 return type;
18527 }
18528
18529 /* Look up the type for the die at OFFSET in the appropriate type_hash
18530 table, or return NULL if the die does not have a saved type. */
18531
18532 static struct type *
18533 get_die_type_at_offset (sect_offset offset,
18534 struct dwarf2_per_cu_data *per_cu)
18535 {
18536 struct dwarf2_per_cu_offset_and_type *slot, ofs;
18537
18538 if (dwarf2_per_objfile->die_type_hash == NULL)
18539 return NULL;
18540
18541 ofs.per_cu = per_cu;
18542 ofs.offset = offset;
18543 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18544 if (slot)
18545 return slot->type;
18546 else
18547 return NULL;
18548 }
18549
18550 /* Look up the type for DIE in the appropriate type_hash table,
18551 or return NULL if DIE does not have a saved type. */
18552
18553 static struct type *
18554 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18555 {
18556 return get_die_type_at_offset (die->offset, cu->per_cu);
18557 }
18558
18559 /* Add a dependence relationship from CU to REF_PER_CU. */
18560
18561 static void
18562 dwarf2_add_dependence (struct dwarf2_cu *cu,
18563 struct dwarf2_per_cu_data *ref_per_cu)
18564 {
18565 void **slot;
18566
18567 if (cu->dependencies == NULL)
18568 cu->dependencies
18569 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18570 NULL, &cu->comp_unit_obstack,
18571 hashtab_obstack_allocate,
18572 dummy_obstack_deallocate);
18573
18574 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18575 if (*slot == NULL)
18576 *slot = ref_per_cu;
18577 }
18578
18579 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18580 Set the mark field in every compilation unit in the
18581 cache that we must keep because we are keeping CU. */
18582
18583 static int
18584 dwarf2_mark_helper (void **slot, void *data)
18585 {
18586 struct dwarf2_per_cu_data *per_cu;
18587
18588 per_cu = (struct dwarf2_per_cu_data *) *slot;
18589
18590 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18591 reading of the chain. As such dependencies remain valid it is not much
18592 useful to track and undo them during QUIT cleanups. */
18593 if (per_cu->cu == NULL)
18594 return 1;
18595
18596 if (per_cu->cu->mark)
18597 return 1;
18598 per_cu->cu->mark = 1;
18599
18600 if (per_cu->cu->dependencies != NULL)
18601 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18602
18603 return 1;
18604 }
18605
18606 /* Set the mark field in CU and in every other compilation unit in the
18607 cache that we must keep because we are keeping CU. */
18608
18609 static void
18610 dwarf2_mark (struct dwarf2_cu *cu)
18611 {
18612 if (cu->mark)
18613 return;
18614 cu->mark = 1;
18615 if (cu->dependencies != NULL)
18616 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18617 }
18618
18619 static void
18620 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18621 {
18622 while (per_cu)
18623 {
18624 per_cu->cu->mark = 0;
18625 per_cu = per_cu->cu->read_in_chain;
18626 }
18627 }
18628
18629 /* Trivial hash function for partial_die_info: the hash value of a DIE
18630 is its offset in .debug_info for this objfile. */
18631
18632 static hashval_t
18633 partial_die_hash (const void *item)
18634 {
18635 const struct partial_die_info *part_die = item;
18636
18637 return part_die->offset.sect_off;
18638 }
18639
18640 /* Trivial comparison function for partial_die_info structures: two DIEs
18641 are equal if they have the same offset. */
18642
18643 static int
18644 partial_die_eq (const void *item_lhs, const void *item_rhs)
18645 {
18646 const struct partial_die_info *part_die_lhs = item_lhs;
18647 const struct partial_die_info *part_die_rhs = item_rhs;
18648
18649 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18650 }
18651
18652 static struct cmd_list_element *set_dwarf2_cmdlist;
18653 static struct cmd_list_element *show_dwarf2_cmdlist;
18654
18655 static void
18656 set_dwarf2_cmd (char *args, int from_tty)
18657 {
18658 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18659 }
18660
18661 static void
18662 show_dwarf2_cmd (char *args, int from_tty)
18663 {
18664 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18665 }
18666
18667 /* Free data associated with OBJFILE, if necessary. */
18668
18669 static void
18670 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18671 {
18672 struct dwarf2_per_objfile *data = d;
18673 int ix;
18674
18675 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18676 VEC_free (dwarf2_per_cu_ptr,
18677 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18678
18679 VEC_free (dwarf2_section_info_def, data->types);
18680
18681 if (data->dwo_files)
18682 free_dwo_files (data->dwo_files, objfile);
18683
18684 if (data->dwz_file && data->dwz_file->dwz_bfd)
18685 gdb_bfd_unref (data->dwz_file->dwz_bfd);
18686 }
18687
18688 \f
18689 /* The "save gdb-index" command. */
18690
18691 /* The contents of the hash table we create when building the string
18692 table. */
18693 struct strtab_entry
18694 {
18695 offset_type offset;
18696 const char *str;
18697 };
18698
18699 /* Hash function for a strtab_entry.
18700
18701 Function is used only during write_hash_table so no index format backward
18702 compatibility is needed. */
18703
18704 static hashval_t
18705 hash_strtab_entry (const void *e)
18706 {
18707 const struct strtab_entry *entry = e;
18708 return mapped_index_string_hash (INT_MAX, entry->str);
18709 }
18710
18711 /* Equality function for a strtab_entry. */
18712
18713 static int
18714 eq_strtab_entry (const void *a, const void *b)
18715 {
18716 const struct strtab_entry *ea = a;
18717 const struct strtab_entry *eb = b;
18718 return !strcmp (ea->str, eb->str);
18719 }
18720
18721 /* Create a strtab_entry hash table. */
18722
18723 static htab_t
18724 create_strtab (void)
18725 {
18726 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18727 xfree, xcalloc, xfree);
18728 }
18729
18730 /* Add a string to the constant pool. Return the string's offset in
18731 host order. */
18732
18733 static offset_type
18734 add_string (htab_t table, struct obstack *cpool, const char *str)
18735 {
18736 void **slot;
18737 struct strtab_entry entry;
18738 struct strtab_entry *result;
18739
18740 entry.str = str;
18741 slot = htab_find_slot (table, &entry, INSERT);
18742 if (*slot)
18743 result = *slot;
18744 else
18745 {
18746 result = XNEW (struct strtab_entry);
18747 result->offset = obstack_object_size (cpool);
18748 result->str = str;
18749 obstack_grow_str0 (cpool, str);
18750 *slot = result;
18751 }
18752 return result->offset;
18753 }
18754
18755 /* An entry in the symbol table. */
18756 struct symtab_index_entry
18757 {
18758 /* The name of the symbol. */
18759 const char *name;
18760 /* The offset of the name in the constant pool. */
18761 offset_type index_offset;
18762 /* A sorted vector of the indices of all the CUs that hold an object
18763 of this name. */
18764 VEC (offset_type) *cu_indices;
18765 };
18766
18767 /* The symbol table. This is a power-of-2-sized hash table. */
18768 struct mapped_symtab
18769 {
18770 offset_type n_elements;
18771 offset_type size;
18772 struct symtab_index_entry **data;
18773 };
18774
18775 /* Hash function for a symtab_index_entry. */
18776
18777 static hashval_t
18778 hash_symtab_entry (const void *e)
18779 {
18780 const struct symtab_index_entry *entry = e;
18781 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18782 sizeof (offset_type) * VEC_length (offset_type,
18783 entry->cu_indices),
18784 0);
18785 }
18786
18787 /* Equality function for a symtab_index_entry. */
18788
18789 static int
18790 eq_symtab_entry (const void *a, const void *b)
18791 {
18792 const struct symtab_index_entry *ea = a;
18793 const struct symtab_index_entry *eb = b;
18794 int len = VEC_length (offset_type, ea->cu_indices);
18795 if (len != VEC_length (offset_type, eb->cu_indices))
18796 return 0;
18797 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18798 VEC_address (offset_type, eb->cu_indices),
18799 sizeof (offset_type) * len);
18800 }
18801
18802 /* Destroy a symtab_index_entry. */
18803
18804 static void
18805 delete_symtab_entry (void *p)
18806 {
18807 struct symtab_index_entry *entry = p;
18808 VEC_free (offset_type, entry->cu_indices);
18809 xfree (entry);
18810 }
18811
18812 /* Create a hash table holding symtab_index_entry objects. */
18813
18814 static htab_t
18815 create_symbol_hash_table (void)
18816 {
18817 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18818 delete_symtab_entry, xcalloc, xfree);
18819 }
18820
18821 /* Create a new mapped symtab object. */
18822
18823 static struct mapped_symtab *
18824 create_mapped_symtab (void)
18825 {
18826 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18827 symtab->n_elements = 0;
18828 symtab->size = 1024;
18829 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18830 return symtab;
18831 }
18832
18833 /* Destroy a mapped_symtab. */
18834
18835 static void
18836 cleanup_mapped_symtab (void *p)
18837 {
18838 struct mapped_symtab *symtab = p;
18839 /* The contents of the array are freed when the other hash table is
18840 destroyed. */
18841 xfree (symtab->data);
18842 xfree (symtab);
18843 }
18844
18845 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
18846 the slot.
18847
18848 Function is used only during write_hash_table so no index format backward
18849 compatibility is needed. */
18850
18851 static struct symtab_index_entry **
18852 find_slot (struct mapped_symtab *symtab, const char *name)
18853 {
18854 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18855
18856 index = hash & (symtab->size - 1);
18857 step = ((hash * 17) & (symtab->size - 1)) | 1;
18858
18859 for (;;)
18860 {
18861 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18862 return &symtab->data[index];
18863 index = (index + step) & (symtab->size - 1);
18864 }
18865 }
18866
18867 /* Expand SYMTAB's hash table. */
18868
18869 static void
18870 hash_expand (struct mapped_symtab *symtab)
18871 {
18872 offset_type old_size = symtab->size;
18873 offset_type i;
18874 struct symtab_index_entry **old_entries = symtab->data;
18875
18876 symtab->size *= 2;
18877 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18878
18879 for (i = 0; i < old_size; ++i)
18880 {
18881 if (old_entries[i])
18882 {
18883 struct symtab_index_entry **slot = find_slot (symtab,
18884 old_entries[i]->name);
18885 *slot = old_entries[i];
18886 }
18887 }
18888
18889 xfree (old_entries);
18890 }
18891
18892 /* Add an entry to SYMTAB. NAME is the name of the symbol.
18893 CU_INDEX is the index of the CU in which the symbol appears.
18894 IS_STATIC is one if the symbol is static, otherwise zero (global). */
18895
18896 static void
18897 add_index_entry (struct mapped_symtab *symtab, const char *name,
18898 int is_static, gdb_index_symbol_kind kind,
18899 offset_type cu_index)
18900 {
18901 struct symtab_index_entry **slot;
18902 offset_type cu_index_and_attrs;
18903
18904 ++symtab->n_elements;
18905 if (4 * symtab->n_elements / 3 >= symtab->size)
18906 hash_expand (symtab);
18907
18908 slot = find_slot (symtab, name);
18909 if (!*slot)
18910 {
18911 *slot = XNEW (struct symtab_index_entry);
18912 (*slot)->name = name;
18913 /* index_offset is set later. */
18914 (*slot)->cu_indices = NULL;
18915 }
18916
18917 cu_index_and_attrs = 0;
18918 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18919 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18920 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18921
18922 /* We don't want to record an index value twice as we want to avoid the
18923 duplication.
18924 We process all global symbols and then all static symbols
18925 (which would allow us to avoid the duplication by only having to check
18926 the last entry pushed), but a symbol could have multiple kinds in one CU.
18927 To keep things simple we don't worry about the duplication here and
18928 sort and uniqufy the list after we've processed all symbols. */
18929 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18930 }
18931
18932 /* qsort helper routine for uniquify_cu_indices. */
18933
18934 static int
18935 offset_type_compare (const void *ap, const void *bp)
18936 {
18937 offset_type a = *(offset_type *) ap;
18938 offset_type b = *(offset_type *) bp;
18939
18940 return (a > b) - (b > a);
18941 }
18942
18943 /* Sort and remove duplicates of all symbols' cu_indices lists. */
18944
18945 static void
18946 uniquify_cu_indices (struct mapped_symtab *symtab)
18947 {
18948 int i;
18949
18950 for (i = 0; i < symtab->size; ++i)
18951 {
18952 struct symtab_index_entry *entry = symtab->data[i];
18953
18954 if (entry
18955 && entry->cu_indices != NULL)
18956 {
18957 unsigned int next_to_insert, next_to_check;
18958 offset_type last_value;
18959
18960 qsort (VEC_address (offset_type, entry->cu_indices),
18961 VEC_length (offset_type, entry->cu_indices),
18962 sizeof (offset_type), offset_type_compare);
18963
18964 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18965 next_to_insert = 1;
18966 for (next_to_check = 1;
18967 next_to_check < VEC_length (offset_type, entry->cu_indices);
18968 ++next_to_check)
18969 {
18970 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18971 != last_value)
18972 {
18973 last_value = VEC_index (offset_type, entry->cu_indices,
18974 next_to_check);
18975 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18976 last_value);
18977 ++next_to_insert;
18978 }
18979 }
18980 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18981 }
18982 }
18983 }
18984
18985 /* Add a vector of indices to the constant pool. */
18986
18987 static offset_type
18988 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18989 struct symtab_index_entry *entry)
18990 {
18991 void **slot;
18992
18993 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18994 if (!*slot)
18995 {
18996 offset_type len = VEC_length (offset_type, entry->cu_indices);
18997 offset_type val = MAYBE_SWAP (len);
18998 offset_type iter;
18999 int i;
19000
19001 *slot = entry;
19002 entry->index_offset = obstack_object_size (cpool);
19003
19004 obstack_grow (cpool, &val, sizeof (val));
19005 for (i = 0;
19006 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19007 ++i)
19008 {
19009 val = MAYBE_SWAP (iter);
19010 obstack_grow (cpool, &val, sizeof (val));
19011 }
19012 }
19013 else
19014 {
19015 struct symtab_index_entry *old_entry = *slot;
19016 entry->index_offset = old_entry->index_offset;
19017 entry = old_entry;
19018 }
19019 return entry->index_offset;
19020 }
19021
19022 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19023 constant pool entries going into the obstack CPOOL. */
19024
19025 static void
19026 write_hash_table (struct mapped_symtab *symtab,
19027 struct obstack *output, struct obstack *cpool)
19028 {
19029 offset_type i;
19030 htab_t symbol_hash_table;
19031 htab_t str_table;
19032
19033 symbol_hash_table = create_symbol_hash_table ();
19034 str_table = create_strtab ();
19035
19036 /* We add all the index vectors to the constant pool first, to
19037 ensure alignment is ok. */
19038 for (i = 0; i < symtab->size; ++i)
19039 {
19040 if (symtab->data[i])
19041 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19042 }
19043
19044 /* Now write out the hash table. */
19045 for (i = 0; i < symtab->size; ++i)
19046 {
19047 offset_type str_off, vec_off;
19048
19049 if (symtab->data[i])
19050 {
19051 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19052 vec_off = symtab->data[i]->index_offset;
19053 }
19054 else
19055 {
19056 /* While 0 is a valid constant pool index, it is not valid
19057 to have 0 for both offsets. */
19058 str_off = 0;
19059 vec_off = 0;
19060 }
19061
19062 str_off = MAYBE_SWAP (str_off);
19063 vec_off = MAYBE_SWAP (vec_off);
19064
19065 obstack_grow (output, &str_off, sizeof (str_off));
19066 obstack_grow (output, &vec_off, sizeof (vec_off));
19067 }
19068
19069 htab_delete (str_table);
19070 htab_delete (symbol_hash_table);
19071 }
19072
19073 /* Struct to map psymtab to CU index in the index file. */
19074 struct psymtab_cu_index_map
19075 {
19076 struct partial_symtab *psymtab;
19077 unsigned int cu_index;
19078 };
19079
19080 static hashval_t
19081 hash_psymtab_cu_index (const void *item)
19082 {
19083 const struct psymtab_cu_index_map *map = item;
19084
19085 return htab_hash_pointer (map->psymtab);
19086 }
19087
19088 static int
19089 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19090 {
19091 const struct psymtab_cu_index_map *lhs = item_lhs;
19092 const struct psymtab_cu_index_map *rhs = item_rhs;
19093
19094 return lhs->psymtab == rhs->psymtab;
19095 }
19096
19097 /* Helper struct for building the address table. */
19098 struct addrmap_index_data
19099 {
19100 struct objfile *objfile;
19101 struct obstack *addr_obstack;
19102 htab_t cu_index_htab;
19103
19104 /* Non-zero if the previous_* fields are valid.
19105 We can't write an entry until we see the next entry (since it is only then
19106 that we know the end of the entry). */
19107 int previous_valid;
19108 /* Index of the CU in the table of all CUs in the index file. */
19109 unsigned int previous_cu_index;
19110 /* Start address of the CU. */
19111 CORE_ADDR previous_cu_start;
19112 };
19113
19114 /* Write an address entry to OBSTACK. */
19115
19116 static void
19117 add_address_entry (struct objfile *objfile, struct obstack *obstack,
19118 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19119 {
19120 offset_type cu_index_to_write;
19121 char addr[8];
19122 CORE_ADDR baseaddr;
19123
19124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19125
19126 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19127 obstack_grow (obstack, addr, 8);
19128 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19129 obstack_grow (obstack, addr, 8);
19130 cu_index_to_write = MAYBE_SWAP (cu_index);
19131 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19132 }
19133
19134 /* Worker function for traversing an addrmap to build the address table. */
19135
19136 static int
19137 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19138 {
19139 struct addrmap_index_data *data = datap;
19140 struct partial_symtab *pst = obj;
19141
19142 if (data->previous_valid)
19143 add_address_entry (data->objfile, data->addr_obstack,
19144 data->previous_cu_start, start_addr,
19145 data->previous_cu_index);
19146
19147 data->previous_cu_start = start_addr;
19148 if (pst != NULL)
19149 {
19150 struct psymtab_cu_index_map find_map, *map;
19151 find_map.psymtab = pst;
19152 map = htab_find (data->cu_index_htab, &find_map);
19153 gdb_assert (map != NULL);
19154 data->previous_cu_index = map->cu_index;
19155 data->previous_valid = 1;
19156 }
19157 else
19158 data->previous_valid = 0;
19159
19160 return 0;
19161 }
19162
19163 /* Write OBJFILE's address map to OBSTACK.
19164 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19165 in the index file. */
19166
19167 static void
19168 write_address_map (struct objfile *objfile, struct obstack *obstack,
19169 htab_t cu_index_htab)
19170 {
19171 struct addrmap_index_data addrmap_index_data;
19172
19173 /* When writing the address table, we have to cope with the fact that
19174 the addrmap iterator only provides the start of a region; we have to
19175 wait until the next invocation to get the start of the next region. */
19176
19177 addrmap_index_data.objfile = objfile;
19178 addrmap_index_data.addr_obstack = obstack;
19179 addrmap_index_data.cu_index_htab = cu_index_htab;
19180 addrmap_index_data.previous_valid = 0;
19181
19182 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
19183 &addrmap_index_data);
19184
19185 /* It's highly unlikely the last entry (end address = 0xff...ff)
19186 is valid, but we should still handle it.
19187 The end address is recorded as the start of the next region, but that
19188 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
19189 anyway. */
19190 if (addrmap_index_data.previous_valid)
19191 add_address_entry (objfile, obstack,
19192 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
19193 addrmap_index_data.previous_cu_index);
19194 }
19195
19196 /* Return the symbol kind of PSYM. */
19197
19198 static gdb_index_symbol_kind
19199 symbol_kind (struct partial_symbol *psym)
19200 {
19201 domain_enum domain = PSYMBOL_DOMAIN (psym);
19202 enum address_class aclass = PSYMBOL_CLASS (psym);
19203
19204 switch (domain)
19205 {
19206 case VAR_DOMAIN:
19207 switch (aclass)
19208 {
19209 case LOC_BLOCK:
19210 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
19211 case LOC_TYPEDEF:
19212 return GDB_INDEX_SYMBOL_KIND_TYPE;
19213 case LOC_COMPUTED:
19214 case LOC_CONST_BYTES:
19215 case LOC_OPTIMIZED_OUT:
19216 case LOC_STATIC:
19217 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19218 case LOC_CONST:
19219 /* Note: It's currently impossible to recognize psyms as enum values
19220 short of reading the type info. For now punt. */
19221 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19222 default:
19223 /* There are other LOC_FOO values that one might want to classify
19224 as variables, but dwarf2read.c doesn't currently use them. */
19225 return GDB_INDEX_SYMBOL_KIND_OTHER;
19226 }
19227 case STRUCT_DOMAIN:
19228 return GDB_INDEX_SYMBOL_KIND_TYPE;
19229 default:
19230 return GDB_INDEX_SYMBOL_KIND_OTHER;
19231 }
19232 }
19233
19234 /* Add a list of partial symbols to SYMTAB. */
19235
19236 static void
19237 write_psymbols (struct mapped_symtab *symtab,
19238 htab_t psyms_seen,
19239 struct partial_symbol **psymp,
19240 int count,
19241 offset_type cu_index,
19242 int is_static)
19243 {
19244 for (; count-- > 0; ++psymp)
19245 {
19246 struct partial_symbol *psym = *psymp;
19247 void **slot;
19248
19249 if (SYMBOL_LANGUAGE (psym) == language_ada)
19250 error (_("Ada is not currently supported by the index"));
19251
19252 /* Only add a given psymbol once. */
19253 slot = htab_find_slot (psyms_seen, psym, INSERT);
19254 if (!*slot)
19255 {
19256 gdb_index_symbol_kind kind = symbol_kind (psym);
19257
19258 *slot = psym;
19259 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
19260 is_static, kind, cu_index);
19261 }
19262 }
19263 }
19264
19265 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
19266 exception if there is an error. */
19267
19268 static void
19269 write_obstack (FILE *file, struct obstack *obstack)
19270 {
19271 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
19272 file)
19273 != obstack_object_size (obstack))
19274 error (_("couldn't data write to file"));
19275 }
19276
19277 /* Unlink a file if the argument is not NULL. */
19278
19279 static void
19280 unlink_if_set (void *p)
19281 {
19282 char **filename = p;
19283 if (*filename)
19284 unlink (*filename);
19285 }
19286
19287 /* A helper struct used when iterating over debug_types. */
19288 struct signatured_type_index_data
19289 {
19290 struct objfile *objfile;
19291 struct mapped_symtab *symtab;
19292 struct obstack *types_list;
19293 htab_t psyms_seen;
19294 int cu_index;
19295 };
19296
19297 /* A helper function that writes a single signatured_type to an
19298 obstack. */
19299
19300 static int
19301 write_one_signatured_type (void **slot, void *d)
19302 {
19303 struct signatured_type_index_data *info = d;
19304 struct signatured_type *entry = (struct signatured_type *) *slot;
19305 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19306 struct partial_symtab *psymtab = per_cu->v.psymtab;
19307 gdb_byte val[8];
19308
19309 write_psymbols (info->symtab,
19310 info->psyms_seen,
19311 info->objfile->global_psymbols.list
19312 + psymtab->globals_offset,
19313 psymtab->n_global_syms, info->cu_index,
19314 0);
19315 write_psymbols (info->symtab,
19316 info->psyms_seen,
19317 info->objfile->static_psymbols.list
19318 + psymtab->statics_offset,
19319 psymtab->n_static_syms, info->cu_index,
19320 1);
19321
19322 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19323 entry->per_cu.offset.sect_off);
19324 obstack_grow (info->types_list, val, 8);
19325 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19326 entry->type_offset_in_tu.cu_off);
19327 obstack_grow (info->types_list, val, 8);
19328 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19329 obstack_grow (info->types_list, val, 8);
19330
19331 ++info->cu_index;
19332
19333 return 1;
19334 }
19335
19336 /* Recurse into all "included" dependencies and write their symbols as
19337 if they appeared in this psymtab. */
19338
19339 static void
19340 recursively_write_psymbols (struct objfile *objfile,
19341 struct partial_symtab *psymtab,
19342 struct mapped_symtab *symtab,
19343 htab_t psyms_seen,
19344 offset_type cu_index)
19345 {
19346 int i;
19347
19348 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19349 if (psymtab->dependencies[i]->user != NULL)
19350 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19351 symtab, psyms_seen, cu_index);
19352
19353 write_psymbols (symtab,
19354 psyms_seen,
19355 objfile->global_psymbols.list + psymtab->globals_offset,
19356 psymtab->n_global_syms, cu_index,
19357 0);
19358 write_psymbols (symtab,
19359 psyms_seen,
19360 objfile->static_psymbols.list + psymtab->statics_offset,
19361 psymtab->n_static_syms, cu_index,
19362 1);
19363 }
19364
19365 /* Create an index file for OBJFILE in the directory DIR. */
19366
19367 static void
19368 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19369 {
19370 struct cleanup *cleanup;
19371 char *filename, *cleanup_filename;
19372 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19373 struct obstack cu_list, types_cu_list;
19374 int i;
19375 FILE *out_file;
19376 struct mapped_symtab *symtab;
19377 offset_type val, size_of_contents, total_len;
19378 struct stat st;
19379 htab_t psyms_seen;
19380 htab_t cu_index_htab;
19381 struct psymtab_cu_index_map *psymtab_cu_index_map;
19382
19383 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
19384 return;
19385
19386 if (dwarf2_per_objfile->using_index)
19387 error (_("Cannot use an index to create the index"));
19388
19389 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19390 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19391
19392 if (stat (objfile->name, &st) < 0)
19393 perror_with_name (objfile->name);
19394
19395 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19396 INDEX_SUFFIX, (char *) NULL);
19397 cleanup = make_cleanup (xfree, filename);
19398
19399 out_file = fopen (filename, "wb");
19400 if (!out_file)
19401 error (_("Can't open `%s' for writing"), filename);
19402
19403 cleanup_filename = filename;
19404 make_cleanup (unlink_if_set, &cleanup_filename);
19405
19406 symtab = create_mapped_symtab ();
19407 make_cleanup (cleanup_mapped_symtab, symtab);
19408
19409 obstack_init (&addr_obstack);
19410 make_cleanup_obstack_free (&addr_obstack);
19411
19412 obstack_init (&cu_list);
19413 make_cleanup_obstack_free (&cu_list);
19414
19415 obstack_init (&types_cu_list);
19416 make_cleanup_obstack_free (&types_cu_list);
19417
19418 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19419 NULL, xcalloc, xfree);
19420 make_cleanup_htab_delete (psyms_seen);
19421
19422 /* While we're scanning CU's create a table that maps a psymtab pointer
19423 (which is what addrmap records) to its index (which is what is recorded
19424 in the index file). This will later be needed to write the address
19425 table. */
19426 cu_index_htab = htab_create_alloc (100,
19427 hash_psymtab_cu_index,
19428 eq_psymtab_cu_index,
19429 NULL, xcalloc, xfree);
19430 make_cleanup_htab_delete (cu_index_htab);
19431 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19432 xmalloc (sizeof (struct psymtab_cu_index_map)
19433 * dwarf2_per_objfile->n_comp_units);
19434 make_cleanup (xfree, psymtab_cu_index_map);
19435
19436 /* The CU list is already sorted, so we don't need to do additional
19437 work here. Also, the debug_types entries do not appear in
19438 all_comp_units, but only in their own hash table. */
19439 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19440 {
19441 struct dwarf2_per_cu_data *per_cu
19442 = dwarf2_per_objfile->all_comp_units[i];
19443 struct partial_symtab *psymtab = per_cu->v.psymtab;
19444 gdb_byte val[8];
19445 struct psymtab_cu_index_map *map;
19446 void **slot;
19447
19448 if (psymtab->user == NULL)
19449 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
19450
19451 map = &psymtab_cu_index_map[i];
19452 map->psymtab = psymtab;
19453 map->cu_index = i;
19454 slot = htab_find_slot (cu_index_htab, map, INSERT);
19455 gdb_assert (slot != NULL);
19456 gdb_assert (*slot == NULL);
19457 *slot = map;
19458
19459 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19460 per_cu->offset.sect_off);
19461 obstack_grow (&cu_list, val, 8);
19462 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
19463 obstack_grow (&cu_list, val, 8);
19464 }
19465
19466 /* Dump the address map. */
19467 write_address_map (objfile, &addr_obstack, cu_index_htab);
19468
19469 /* Write out the .debug_type entries, if any. */
19470 if (dwarf2_per_objfile->signatured_types)
19471 {
19472 struct signatured_type_index_data sig_data;
19473
19474 sig_data.objfile = objfile;
19475 sig_data.symtab = symtab;
19476 sig_data.types_list = &types_cu_list;
19477 sig_data.psyms_seen = psyms_seen;
19478 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19479 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19480 write_one_signatured_type, &sig_data);
19481 }
19482
19483 /* Now that we've processed all symbols we can shrink their cu_indices
19484 lists. */
19485 uniquify_cu_indices (symtab);
19486
19487 obstack_init (&constant_pool);
19488 make_cleanup_obstack_free (&constant_pool);
19489 obstack_init (&symtab_obstack);
19490 make_cleanup_obstack_free (&symtab_obstack);
19491 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19492
19493 obstack_init (&contents);
19494 make_cleanup_obstack_free (&contents);
19495 size_of_contents = 6 * sizeof (offset_type);
19496 total_len = size_of_contents;
19497
19498 /* The version number. */
19499 val = MAYBE_SWAP (7);
19500 obstack_grow (&contents, &val, sizeof (val));
19501
19502 /* The offset of the CU list from the start of the file. */
19503 val = MAYBE_SWAP (total_len);
19504 obstack_grow (&contents, &val, sizeof (val));
19505 total_len += obstack_object_size (&cu_list);
19506
19507 /* The offset of the types CU list from the start of the file. */
19508 val = MAYBE_SWAP (total_len);
19509 obstack_grow (&contents, &val, sizeof (val));
19510 total_len += obstack_object_size (&types_cu_list);
19511
19512 /* The offset of the address table from the start of the file. */
19513 val = MAYBE_SWAP (total_len);
19514 obstack_grow (&contents, &val, sizeof (val));
19515 total_len += obstack_object_size (&addr_obstack);
19516
19517 /* The offset of the symbol table from the start of the file. */
19518 val = MAYBE_SWAP (total_len);
19519 obstack_grow (&contents, &val, sizeof (val));
19520 total_len += obstack_object_size (&symtab_obstack);
19521
19522 /* The offset of the constant pool from the start of the file. */
19523 val = MAYBE_SWAP (total_len);
19524 obstack_grow (&contents, &val, sizeof (val));
19525 total_len += obstack_object_size (&constant_pool);
19526
19527 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19528
19529 write_obstack (out_file, &contents);
19530 write_obstack (out_file, &cu_list);
19531 write_obstack (out_file, &types_cu_list);
19532 write_obstack (out_file, &addr_obstack);
19533 write_obstack (out_file, &symtab_obstack);
19534 write_obstack (out_file, &constant_pool);
19535
19536 fclose (out_file);
19537
19538 /* We want to keep the file, so we set cleanup_filename to NULL
19539 here. See unlink_if_set. */
19540 cleanup_filename = NULL;
19541
19542 do_cleanups (cleanup);
19543 }
19544
19545 /* Implementation of the `save gdb-index' command.
19546
19547 Note that the file format used by this command is documented in the
19548 GDB manual. Any changes here must be documented there. */
19549
19550 static void
19551 save_gdb_index_command (char *arg, int from_tty)
19552 {
19553 struct objfile *objfile;
19554
19555 if (!arg || !*arg)
19556 error (_("usage: save gdb-index DIRECTORY"));
19557
19558 ALL_OBJFILES (objfile)
19559 {
19560 struct stat st;
19561
19562 /* If the objfile does not correspond to an actual file, skip it. */
19563 if (stat (objfile->name, &st) < 0)
19564 continue;
19565
19566 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19567 if (dwarf2_per_objfile)
19568 {
19569 volatile struct gdb_exception except;
19570
19571 TRY_CATCH (except, RETURN_MASK_ERROR)
19572 {
19573 write_psymtabs_to_index (objfile, arg);
19574 }
19575 if (except.reason < 0)
19576 exception_fprintf (gdb_stderr, except,
19577 _("Error while writing index for `%s': "),
19578 objfile->name);
19579 }
19580 }
19581 }
19582
19583 \f
19584
19585 int dwarf2_always_disassemble;
19586
19587 static void
19588 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19589 struct cmd_list_element *c, const char *value)
19590 {
19591 fprintf_filtered (file,
19592 _("Whether to always disassemble "
19593 "DWARF expressions is %s.\n"),
19594 value);
19595 }
19596
19597 static void
19598 show_check_physname (struct ui_file *file, int from_tty,
19599 struct cmd_list_element *c, const char *value)
19600 {
19601 fprintf_filtered (file,
19602 _("Whether to check \"physname\" is %s.\n"),
19603 value);
19604 }
19605
19606 void _initialize_dwarf2_read (void);
19607
19608 void
19609 _initialize_dwarf2_read (void)
19610 {
19611 struct cmd_list_element *c;
19612
19613 dwarf2_objfile_data_key
19614 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19615
19616 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19617 Set DWARF 2 specific variables.\n\
19618 Configure DWARF 2 variables such as the cache size"),
19619 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19620 0/*allow-unknown*/, &maintenance_set_cmdlist);
19621
19622 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19623 Show DWARF 2 specific variables\n\
19624 Show DWARF 2 variables such as the cache size"),
19625 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19626 0/*allow-unknown*/, &maintenance_show_cmdlist);
19627
19628 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19629 &dwarf2_max_cache_age, _("\
19630 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19631 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19632 A higher limit means that cached compilation units will be stored\n\
19633 in memory longer, and more total memory will be used. Zero disables\n\
19634 caching, which can slow down startup."),
19635 NULL,
19636 show_dwarf2_max_cache_age,
19637 &set_dwarf2_cmdlist,
19638 &show_dwarf2_cmdlist);
19639
19640 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19641 &dwarf2_always_disassemble, _("\
19642 Set whether `info address' always disassembles DWARF expressions."), _("\
19643 Show whether `info address' always disassembles DWARF expressions."), _("\
19644 When enabled, DWARF expressions are always printed in an assembly-like\n\
19645 syntax. When disabled, expressions will be printed in a more\n\
19646 conversational style, when possible."),
19647 NULL,
19648 show_dwarf2_always_disassemble,
19649 &set_dwarf2_cmdlist,
19650 &show_dwarf2_cmdlist);
19651
19652 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19653 Set debugging of the dwarf2 reader."), _("\
19654 Show debugging of the dwarf2 reader."), _("\
19655 When enabled, debugging messages are printed during dwarf2 reading\n\
19656 and symtab expansion."),
19657 NULL,
19658 NULL,
19659 &setdebuglist, &showdebuglist);
19660
19661 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19662 Set debugging of the dwarf2 DIE reader."), _("\
19663 Show debugging of the dwarf2 DIE reader."), _("\
19664 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19665 The value is the maximum depth to print."),
19666 NULL,
19667 NULL,
19668 &setdebuglist, &showdebuglist);
19669
19670 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19671 Set cross-checking of \"physname\" code against demangler."), _("\
19672 Show cross-checking of \"physname\" code against demangler."), _("\
19673 When enabled, GDB's internal \"physname\" code is checked against\n\
19674 the demangler."),
19675 NULL, show_check_physname,
19676 &setdebuglist, &showdebuglist);
19677
19678 add_setshow_boolean_cmd ("use-deprecated-index-sections",
19679 no_class, &use_deprecated_index_sections, _("\
19680 Set whether to use deprecated gdb_index sections."), _("\
19681 Show whether to use deprecated gdb_index sections."), _("\
19682 When enabled, deprecated .gdb_index sections are used anyway.\n\
19683 Normally they are ignored either because of a missing feature or\n\
19684 performance issue.\n\
19685 Warning: This option must be enabled before gdb reads the file."),
19686 NULL,
19687 NULL,
19688 &setlist, &showlist);
19689
19690 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19691 _("\
19692 Save a gdb-index file.\n\
19693 Usage: save gdb-index DIRECTORY"),
19694 &save_cmdlist);
19695 set_cmd_completer (c, filename_completer);
19696 }