2012-07-20 Jan Kratochvil <jan.kratochvil@redhat.com>
[deliverable/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
69 #include <fcntl.h>
70 #include "gdb_string.h"
71 #include "gdb_assert.h"
72 #include <sys/types.h>
73
74 typedef struct symbol *symbolp;
75 DEF_VEC_P (symbolp);
76
77 /* When non-zero, print basic high level tracing messages.
78 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
79 static int dwarf2_read_debug = 0;
80
81 /* When non-zero, dump DIEs after they are read in. */
82 static int dwarf2_die_debug = 0;
83
84 /* When non-zero, cross-check physname against demangler. */
85 static int check_physname = 0;
86
87 /* When non-zero, do not reject deprecated .gdb_index sections. */
88 int use_deprecated_index_sections = 0;
89
90 /* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94 static int processing_has_namespace_info;
95
96 static const struct objfile_data *dwarf2_objfile_data_key;
97
98 struct dwarf2_section_info
99 {
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
103 /* True if we have tried to read this section. */
104 int readin;
105 };
106
107 typedef struct dwarf2_section_info dwarf2_section_info_def;
108 DEF_VEC_O (dwarf2_section_info_def);
109
110 /* All offsets in the index are of this type. It must be
111 architecture-independent. */
112 typedef uint32_t offset_type;
113
114 DEF_VEC_I (offset_type);
115
116 /* Ensure only legit values are used. */
117 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
118 do { \
119 gdb_assert ((unsigned int) (value) <= 1); \
120 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
121 } while (0)
122
123 /* Ensure only legit values are used. */
124 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
125 do { \
126 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
127 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
128 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
129 } while (0)
130
131 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
132 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
133 do { \
134 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
135 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
136 } while (0)
137
138 /* A description of the mapped index. The file format is described in
139 a comment by the code that writes the index. */
140 struct mapped_index
141 {
142 /* Index data format version. */
143 int version;
144
145 /* The total length of the buffer. */
146 off_t total_size;
147
148 /* A pointer to the address table data. */
149 const gdb_byte *address_table;
150
151 /* Size of the address table data in bytes. */
152 offset_type address_table_size;
153
154 /* The symbol table, implemented as a hash table. */
155 const offset_type *symbol_table;
156
157 /* Size in slots, each slot is 2 offset_types. */
158 offset_type symbol_table_slots;
159
160 /* A pointer to the constant pool. */
161 const char *constant_pool;
162 };
163
164 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
165 DEF_VEC_P (dwarf2_per_cu_ptr);
166
167 /* Collection of data recorded per objfile.
168 This hangs off of dwarf2_objfile_data_key. */
169
170 struct dwarf2_per_objfile
171 {
172 struct dwarf2_section_info info;
173 struct dwarf2_section_info abbrev;
174 struct dwarf2_section_info line;
175 struct dwarf2_section_info loc;
176 struct dwarf2_section_info macinfo;
177 struct dwarf2_section_info macro;
178 struct dwarf2_section_info str;
179 struct dwarf2_section_info ranges;
180 struct dwarf2_section_info addr;
181 struct dwarf2_section_info frame;
182 struct dwarf2_section_info eh_frame;
183 struct dwarf2_section_info gdb_index;
184
185 VEC (dwarf2_section_info_def) *types;
186
187 /* Back link. */
188 struct objfile *objfile;
189
190 /* Table of all the compilation units. This is used to locate
191 the target compilation unit of a particular reference. */
192 struct dwarf2_per_cu_data **all_comp_units;
193
194 /* The number of compilation units in ALL_COMP_UNITS. */
195 int n_comp_units;
196
197 /* The number of .debug_types-related CUs. */
198 int n_type_units;
199
200 /* The .debug_types-related CUs (TUs). */
201 struct signatured_type **all_type_units;
202
203 /* The number of entries in all_type_unit_groups. */
204 int n_type_unit_groups;
205
206 /* Table of type unit groups.
207 This exists to make it easy to iterate over all CUs and TU groups. */
208 struct type_unit_group **all_type_unit_groups;
209
210 /* Table of struct type_unit_group objects.
211 The hash key is the DW_AT_stmt_list value. */
212 htab_t type_unit_groups;
213
214 /* A table mapping .debug_types signatures to its signatured_type entry.
215 This is NULL if the .debug_types section hasn't been read in yet. */
216 htab_t signatured_types;
217
218 /* Type unit statistics, to see how well the scaling improvements
219 are doing. */
220 struct tu_stats
221 {
222 int nr_uniq_abbrev_tables;
223 int nr_symtabs;
224 int nr_symtab_sharers;
225 int nr_stmt_less_type_units;
226 } tu_stats;
227
228 /* A chain of compilation units that are currently read in, so that
229 they can be freed later. */
230 struct dwarf2_per_cu_data *read_in_chain;
231
232 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
233 This is NULL if the table hasn't been allocated yet. */
234 htab_t dwo_files;
235
236 /* The shared '.dwz' file, if one exists. This is used when the
237 original data was compressed using 'dwz -m'. */
238 struct dwz_file *dwz_file;
239
240 /* A flag indicating wether this objfile has a section loaded at a
241 VMA of 0. */
242 int has_section_at_zero;
243
244 /* True if we are using the mapped index,
245 or we are faking it for OBJF_READNOW's sake. */
246 unsigned char using_index;
247
248 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
249 struct mapped_index *index_table;
250
251 /* When using index_table, this keeps track of all quick_file_names entries.
252 TUs can share line table entries with CUs or other TUs, and there can be
253 a lot more TUs than unique line tables, so we maintain a separate table
254 of all line table entries to support the sharing. */
255 htab_t quick_file_names_table;
256
257 /* Set during partial symbol reading, to prevent queueing of full
258 symbols. */
259 int reading_partial_symbols;
260
261 /* Table mapping type DIEs to their struct type *.
262 This is NULL if not allocated yet.
263 The mapping is done via (CU/TU signature + DIE offset) -> type. */
264 htab_t die_type_hash;
265
266 /* The CUs we recently read. */
267 VEC (dwarf2_per_cu_ptr) *just_read_cus;
268 };
269
270 static struct dwarf2_per_objfile *dwarf2_per_objfile;
271
272 /* Default names of the debugging sections. */
273
274 /* Note that if the debugging section has been compressed, it might
275 have a name like .zdebug_info. */
276
277 static const struct dwarf2_debug_sections dwarf2_elf_names =
278 {
279 { ".debug_info", ".zdebug_info" },
280 { ".debug_abbrev", ".zdebug_abbrev" },
281 { ".debug_line", ".zdebug_line" },
282 { ".debug_loc", ".zdebug_loc" },
283 { ".debug_macinfo", ".zdebug_macinfo" },
284 { ".debug_macro", ".zdebug_macro" },
285 { ".debug_str", ".zdebug_str" },
286 { ".debug_ranges", ".zdebug_ranges" },
287 { ".debug_types", ".zdebug_types" },
288 { ".debug_addr", ".zdebug_addr" },
289 { ".debug_frame", ".zdebug_frame" },
290 { ".eh_frame", NULL },
291 { ".gdb_index", ".zgdb_index" },
292 23
293 };
294
295 /* List of DWO sections. */
296
297 static const struct dwo_section_names
298 {
299 struct dwarf2_section_names abbrev_dwo;
300 struct dwarf2_section_names info_dwo;
301 struct dwarf2_section_names line_dwo;
302 struct dwarf2_section_names loc_dwo;
303 struct dwarf2_section_names macinfo_dwo;
304 struct dwarf2_section_names macro_dwo;
305 struct dwarf2_section_names str_dwo;
306 struct dwarf2_section_names str_offsets_dwo;
307 struct dwarf2_section_names types_dwo;
308 }
309 dwo_section_names =
310 {
311 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
312 { ".debug_info.dwo", ".zdebug_info.dwo" },
313 { ".debug_line.dwo", ".zdebug_line.dwo" },
314 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
315 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
316 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
317 { ".debug_str.dwo", ".zdebug_str.dwo" },
318 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
319 { ".debug_types.dwo", ".zdebug_types.dwo" },
320 };
321
322 /* local data types */
323
324 /* The data in a compilation unit header, after target2host
325 translation, looks like this. */
326 struct comp_unit_head
327 {
328 unsigned int length;
329 short version;
330 unsigned char addr_size;
331 unsigned char signed_addr_p;
332 sect_offset abbrev_offset;
333
334 /* Size of file offsets; either 4 or 8. */
335 unsigned int offset_size;
336
337 /* Size of the length field; either 4 or 12. */
338 unsigned int initial_length_size;
339
340 /* Offset to the first byte of this compilation unit header in the
341 .debug_info section, for resolving relative reference dies. */
342 sect_offset offset;
343
344 /* Offset to first die in this cu from the start of the cu.
345 This will be the first byte following the compilation unit header. */
346 cu_offset first_die_offset;
347 };
348
349 /* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351 struct delayed_method_info
352 {
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367 };
368
369 typedef struct delayed_method_info delayed_method_info;
370 DEF_VEC_O (delayed_method_info);
371
372 /* Internal state when decoding a particular compilation unit. */
373 struct dwarf2_cu
374 {
375 /* The objfile containing this compilation unit. */
376 struct objfile *objfile;
377
378 /* The header of the compilation unit. */
379 struct comp_unit_head header;
380
381 /* Base address of this compilation unit. */
382 CORE_ADDR base_address;
383
384 /* Non-zero if base_address has been set. */
385 int base_known;
386
387 /* The language we are debugging. */
388 enum language language;
389 const struct language_defn *language_defn;
390
391 const char *producer;
392
393 /* The generic symbol table building routines have separate lists for
394 file scope symbols and all all other scopes (local scopes). So
395 we need to select the right one to pass to add_symbol_to_list().
396 We do it by keeping a pointer to the correct list in list_in_scope.
397
398 FIXME: The original dwarf code just treated the file scope as the
399 first local scope, and all other local scopes as nested local
400 scopes, and worked fine. Check to see if we really need to
401 distinguish these in buildsym.c. */
402 struct pending **list_in_scope;
403
404 /* The abbrev table for this CU.
405 Normally this points to the abbrev table in the objfile.
406 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
407 struct abbrev_table *abbrev_table;
408
409 /* Hash table holding all the loaded partial DIEs
410 with partial_die->offset.SECT_OFF as hash. */
411 htab_t partial_dies;
412
413 /* Storage for things with the same lifetime as this read-in compilation
414 unit, including partial DIEs. */
415 struct obstack comp_unit_obstack;
416
417 /* When multiple dwarf2_cu structures are living in memory, this field
418 chains them all together, so that they can be released efficiently.
419 We will probably also want a generation counter so that most-recently-used
420 compilation units are cached... */
421 struct dwarf2_per_cu_data *read_in_chain;
422
423 /* Backchain to our per_cu entry if the tree has been built. */
424 struct dwarf2_per_cu_data *per_cu;
425
426 /* How many compilation units ago was this CU last referenced? */
427 int last_used;
428
429 /* A hash table of DIE cu_offset for following references with
430 die_info->offset.sect_off as hash. */
431 htab_t die_hash;
432
433 /* Full DIEs if read in. */
434 struct die_info *dies;
435
436 /* A set of pointers to dwarf2_per_cu_data objects for compilation
437 units referenced by this one. Only set during full symbol processing;
438 partial symbol tables do not have dependencies. */
439 htab_t dependencies;
440
441 /* Header data from the line table, during full symbol processing. */
442 struct line_header *line_header;
443
444 /* A list of methods which need to have physnames computed
445 after all type information has been read. */
446 VEC (delayed_method_info) *method_list;
447
448 /* To be copied to symtab->call_site_htab. */
449 htab_t call_site_htab;
450
451 /* Non-NULL if this CU came from a DWO file.
452 There is an invariant here that is important to remember:
453 Except for attributes copied from the top level DIE in the "main"
454 (or "stub") file in preparation for reading the DWO file
455 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
456 Either there isn't a DWO file (in which case this is NULL and the point
457 is moot), or there is and either we're not going to read it (in which
458 case this is NULL) or there is and we are reading it (in which case this
459 is non-NULL). */
460 struct dwo_unit *dwo_unit;
461
462 /* The DW_AT_addr_base attribute if present, zero otherwise
463 (zero is a valid value though).
464 Note this value comes from the stub CU/TU's DIE. */
465 ULONGEST addr_base;
466
467 /* The DW_AT_ranges_base attribute if present, zero otherwise
468 (zero is a valid value though).
469 Note this value comes from the stub CU/TU's DIE.
470 Also note that the value is zero in the non-DWO case so this value can
471 be used without needing to know whether DWO files are in use or not. */
472 ULONGEST ranges_base;
473
474 /* Mark used when releasing cached dies. */
475 unsigned int mark : 1;
476
477 /* This CU references .debug_loc. See the symtab->locations_valid field.
478 This test is imperfect as there may exist optimized debug code not using
479 any location list and still facing inlining issues if handled as
480 unoptimized code. For a future better test see GCC PR other/32998. */
481 unsigned int has_loclist : 1;
482
483 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
484 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
485 are valid. This information is cached because profiling CU expansion
486 showed excessive time spent in producer_is_gxx_lt_4_6. */
487 unsigned int checked_producer : 1;
488 unsigned int producer_is_gxx_lt_4_6 : 1;
489 unsigned int producer_is_icc : 1;
490 };
491
492 /* Persistent data held for a compilation unit, even when not
493 processing it. We put a pointer to this structure in the
494 read_symtab_private field of the psymtab. */
495
496 struct dwarf2_per_cu_data
497 {
498 /* The start offset and length of this compilation unit.
499 NOTE: Unlike comp_unit_head.length, this length includes
500 initial_length_size.
501 If the DIE refers to a DWO file, this is always of the original die,
502 not the DWO file. */
503 sect_offset offset;
504 unsigned int length;
505
506 /* Flag indicating this compilation unit will be read in before
507 any of the current compilation units are processed. */
508 unsigned int queued : 1;
509
510 /* This flag will be set when reading partial DIEs if we need to load
511 absolutely all DIEs for this compilation unit, instead of just the ones
512 we think are interesting. It gets set if we look for a DIE in the
513 hash table and don't find it. */
514 unsigned int load_all_dies : 1;
515
516 /* Non-zero if this CU is from .debug_types. */
517 unsigned int is_debug_types : 1;
518
519 /* Non-zero if this CU is from the .dwz file. */
520 unsigned int is_dwz : 1;
521
522 /* The section this CU/TU lives in.
523 If the DIE refers to a DWO file, this is always the original die,
524 not the DWO file. */
525 struct dwarf2_section_info *info_or_types_section;
526
527 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
528 of the CU cache it gets reset to NULL again. */
529 struct dwarf2_cu *cu;
530
531 /* The corresponding objfile.
532 Normally we can get the objfile from dwarf2_per_objfile.
533 However we can enter this file with just a "per_cu" handle. */
534 struct objfile *objfile;
535
536 /* When using partial symbol tables, the 'psymtab' field is active.
537 Otherwise the 'quick' field is active. */
538 union
539 {
540 /* The partial symbol table associated with this compilation unit,
541 or NULL for unread partial units. */
542 struct partial_symtab *psymtab;
543
544 /* Data needed by the "quick" functions. */
545 struct dwarf2_per_cu_quick_data *quick;
546 } v;
547
548 union
549 {
550 /* The CUs we import using DW_TAG_imported_unit. This is filled in
551 while reading psymtabs, used to compute the psymtab dependencies,
552 and then cleared. Then it is filled in again while reading full
553 symbols, and only deleted when the objfile is destroyed. */
554 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
555
556 /* Type units are grouped by their DW_AT_stmt_list entry so that they
557 can share them. If this is a TU, this points to the containing
558 symtab. */
559 struct type_unit_group *type_unit_group;
560 } s;
561 };
562
563 /* Entry in the signatured_types hash table. */
564
565 struct signatured_type
566 {
567 /* The "per_cu" object of this type.
568 N.B.: This is the first member so that it's easy to convert pointers
569 between them. */
570 struct dwarf2_per_cu_data per_cu;
571
572 /* The type's signature. */
573 ULONGEST signature;
574
575 /* Offset in the TU of the type's DIE, as read from the TU header.
576 If the definition lives in a DWO file, this value is unusable. */
577 cu_offset type_offset_in_tu;
578
579 /* Offset in the section of the type's DIE.
580 If the definition lives in a DWO file, this is the offset in the
581 .debug_types.dwo section.
582 The value is zero until the actual value is known.
583 Zero is otherwise not a valid section offset. */
584 sect_offset type_offset_in_section;
585 };
586
587 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588 This includes type_unit_group and quick_file_names. */
589
590 struct stmt_list_hash
591 {
592 /* The DWO unit this table is from or NULL if there is none. */
593 struct dwo_unit *dwo_unit;
594
595 /* Offset in .debug_line or .debug_line.dwo. */
596 sect_offset line_offset;
597 };
598
599 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600 an object of this type. */
601
602 struct type_unit_group
603 {
604 /* dwarf2read.c's main "handle" on the symtab.
605 To simplify things we create an artificial CU that "includes" all the
606 type units using this stmt_list so that the rest of the code still has
607 a "per_cu" handle on the symtab.
608 This PER_CU is recognized by having no section. */
609 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
610 struct dwarf2_per_cu_data per_cu;
611
612 union
613 {
614 /* The TUs that share this DW_AT_stmt_list entry.
615 This is added to while parsing type units to build partial symtabs,
616 and is deleted afterwards and not used again. */
617 VEC (dwarf2_per_cu_ptr) *tus;
618
619 /* When reading the line table in "quick" functions, we need a real TU.
620 Any will do, we know they all share the same DW_AT_stmt_list entry.
621 For simplicity's sake, we pick the first one. */
622 struct dwarf2_per_cu_data *first_tu;
623 } t;
624
625 /* The primary symtab.
626 Type units in a group needn't all be defined in the same source file,
627 so we create an essentially anonymous symtab as the primary symtab. */
628 struct symtab *primary_symtab;
629
630 /* The data used to construct the hash key. */
631 struct stmt_list_hash hash;
632
633 /* The number of symtabs from the line header.
634 The value here must match line_header.num_file_names. */
635 unsigned int num_symtabs;
636
637 /* The symbol tables for this TU (obtained from the files listed in
638 DW_AT_stmt_list).
639 WARNING: The order of entries here must match the order of entries
640 in the line header. After the first TU using this type_unit_group, the
641 line header for the subsequent TUs is recreated from this. This is done
642 because we need to use the same symtabs for each TU using the same
643 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
644 there's no guarantee the line header doesn't have duplicate entries. */
645 struct symtab **symtabs;
646 };
647
648 /* These sections are what may appear in a "dwo" file. */
649
650 struct dwo_sections
651 {
652 struct dwarf2_section_info abbrev;
653 struct dwarf2_section_info info;
654 struct dwarf2_section_info line;
655 struct dwarf2_section_info loc;
656 struct dwarf2_section_info macinfo;
657 struct dwarf2_section_info macro;
658 struct dwarf2_section_info str;
659 struct dwarf2_section_info str_offsets;
660 VEC (dwarf2_section_info_def) *types;
661 };
662
663 /* Common bits of DWO CUs/TUs. */
664
665 struct dwo_unit
666 {
667 /* Backlink to the containing struct dwo_file. */
668 struct dwo_file *dwo_file;
669
670 /* The "id" that distinguishes this CU/TU.
671 .debug_info calls this "dwo_id", .debug_types calls this "signature".
672 Since signatures came first, we stick with it for consistency. */
673 ULONGEST signature;
674
675 /* The section this CU/TU lives in, in the DWO file. */
676 struct dwarf2_section_info *info_or_types_section;
677
678 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
679 sect_offset offset;
680 unsigned int length;
681
682 /* For types, offset in the type's DIE of the type defined by this TU. */
683 cu_offset type_offset_in_tu;
684 };
685
686 /* Data for one DWO file. */
687
688 struct dwo_file
689 {
690 /* The DW_AT_GNU_dwo_name attribute.
691 We don't manage space for this, it's an attribute. */
692 const char *dwo_name;
693
694 /* The bfd, when the file is open. Otherwise this is NULL. */
695 bfd *dwo_bfd;
696
697 /* Section info for this file. */
698 struct dwo_sections sections;
699
700 /* Table of CUs in the file.
701 Each element is a struct dwo_unit. */
702 htab_t cus;
703
704 /* Table of TUs in the file.
705 Each element is a struct dwo_unit. */
706 htab_t tus;
707 };
708
709 /* This represents a '.dwz' file. */
710
711 struct dwz_file
712 {
713 /* A dwz file can only contain a few sections. */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info str;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info macro;
719 struct dwarf2_section_info gdb_index;
720
721 /* The dwz's BFD. */
722 bfd *dwz_bfd;
723 };
724
725 /* Struct used to pass misc. parameters to read_die_and_children, et
726 al. which are used for both .debug_info and .debug_types dies.
727 All parameters here are unchanging for the life of the call. This
728 struct exists to abstract away the constant parameters of die reading. */
729
730 struct die_reader_specs
731 {
732 /* die_section->asection->owner. */
733 bfd* abfd;
734
735 /* The CU of the DIE we are parsing. */
736 struct dwarf2_cu *cu;
737
738 /* Non-NULL if reading a DWO file. */
739 struct dwo_file *dwo_file;
740
741 /* The section the die comes from.
742 This is either .debug_info or .debug_types, or the .dwo variants. */
743 struct dwarf2_section_info *die_section;
744
745 /* die_section->buffer. */
746 gdb_byte *buffer;
747
748 /* The end of the buffer. */
749 const gdb_byte *buffer_end;
750 };
751
752 /* Type of function passed to init_cutu_and_read_dies, et.al. */
753 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
754 gdb_byte *info_ptr,
755 struct die_info *comp_unit_die,
756 int has_children,
757 void *data);
758
759 /* The line number information for a compilation unit (found in the
760 .debug_line section) begins with a "statement program header",
761 which contains the following information. */
762 struct line_header
763 {
764 unsigned int total_length;
765 unsigned short version;
766 unsigned int header_length;
767 unsigned char minimum_instruction_length;
768 unsigned char maximum_ops_per_instruction;
769 unsigned char default_is_stmt;
770 int line_base;
771 unsigned char line_range;
772 unsigned char opcode_base;
773
774 /* standard_opcode_lengths[i] is the number of operands for the
775 standard opcode whose value is i. This means that
776 standard_opcode_lengths[0] is unused, and the last meaningful
777 element is standard_opcode_lengths[opcode_base - 1]. */
778 unsigned char *standard_opcode_lengths;
779
780 /* The include_directories table. NOTE! These strings are not
781 allocated with xmalloc; instead, they are pointers into
782 debug_line_buffer. If you try to free them, `free' will get
783 indigestion. */
784 unsigned int num_include_dirs, include_dirs_size;
785 char **include_dirs;
786
787 /* The file_names table. NOTE! These strings are not allocated
788 with xmalloc; instead, they are pointers into debug_line_buffer.
789 Don't try to free them directly. */
790 unsigned int num_file_names, file_names_size;
791 struct file_entry
792 {
793 char *name;
794 unsigned int dir_index;
795 unsigned int mod_time;
796 unsigned int length;
797 int included_p; /* Non-zero if referenced by the Line Number Program. */
798 struct symtab *symtab; /* The associated symbol table, if any. */
799 } *file_names;
800
801 /* The start and end of the statement program following this
802 header. These point into dwarf2_per_objfile->line_buffer. */
803 gdb_byte *statement_program_start, *statement_program_end;
804 };
805
806 /* When we construct a partial symbol table entry we only
807 need this much information. */
808 struct partial_die_info
809 {
810 /* Offset of this DIE. */
811 sect_offset offset;
812
813 /* DWARF-2 tag for this DIE. */
814 ENUM_BITFIELD(dwarf_tag) tag : 16;
815
816 /* Assorted flags describing the data found in this DIE. */
817 unsigned int has_children : 1;
818 unsigned int is_external : 1;
819 unsigned int is_declaration : 1;
820 unsigned int has_type : 1;
821 unsigned int has_specification : 1;
822 unsigned int has_pc_info : 1;
823 unsigned int may_be_inlined : 1;
824
825 /* Flag set if the SCOPE field of this structure has been
826 computed. */
827 unsigned int scope_set : 1;
828
829 /* Flag set if the DIE has a byte_size attribute. */
830 unsigned int has_byte_size : 1;
831
832 /* Flag set if any of the DIE's children are template arguments. */
833 unsigned int has_template_arguments : 1;
834
835 /* Flag set if fixup_partial_die has been called on this die. */
836 unsigned int fixup_called : 1;
837
838 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
839 unsigned int is_dwz : 1;
840
841 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
842 unsigned int spec_is_dwz : 1;
843
844 /* The name of this DIE. Normally the value of DW_AT_name, but
845 sometimes a default name for unnamed DIEs. */
846 char *name;
847
848 /* The linkage name, if present. */
849 const char *linkage_name;
850
851 /* The scope to prepend to our children. This is generally
852 allocated on the comp_unit_obstack, so will disappear
853 when this compilation unit leaves the cache. */
854 char *scope;
855
856 /* Some data associated with the partial DIE. The tag determines
857 which field is live. */
858 union
859 {
860 /* The location description associated with this DIE, if any. */
861 struct dwarf_block *locdesc;
862 /* The offset of an import, for DW_TAG_imported_unit. */
863 sect_offset offset;
864 } d;
865
866 /* If HAS_PC_INFO, the PC range associated with this DIE. */
867 CORE_ADDR lowpc;
868 CORE_ADDR highpc;
869
870 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
871 DW_AT_sibling, if any. */
872 /* NOTE: This member isn't strictly necessary, read_partial_die could
873 return DW_AT_sibling values to its caller load_partial_dies. */
874 gdb_byte *sibling;
875
876 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
877 DW_AT_specification (or DW_AT_abstract_origin or
878 DW_AT_extension). */
879 sect_offset spec_offset;
880
881 /* Pointers to this DIE's parent, first child, and next sibling,
882 if any. */
883 struct partial_die_info *die_parent, *die_child, *die_sibling;
884 };
885
886 /* This data structure holds the information of an abbrev. */
887 struct abbrev_info
888 {
889 unsigned int number; /* number identifying abbrev */
890 enum dwarf_tag tag; /* dwarf tag */
891 unsigned short has_children; /* boolean */
892 unsigned short num_attrs; /* number of attributes */
893 struct attr_abbrev *attrs; /* an array of attribute descriptions */
894 struct abbrev_info *next; /* next in chain */
895 };
896
897 struct attr_abbrev
898 {
899 ENUM_BITFIELD(dwarf_attribute) name : 16;
900 ENUM_BITFIELD(dwarf_form) form : 16;
901 };
902
903 /* Size of abbrev_table.abbrev_hash_table. */
904 #define ABBREV_HASH_SIZE 121
905
906 /* Top level data structure to contain an abbreviation table. */
907
908 struct abbrev_table
909 {
910 /* Where the abbrev table came from.
911 This is used as a sanity check when the table is used. */
912 sect_offset offset;
913
914 /* Storage for the abbrev table. */
915 struct obstack abbrev_obstack;
916
917 /* Hash table of abbrevs.
918 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
919 It could be statically allocated, but the previous code didn't so we
920 don't either. */
921 struct abbrev_info **abbrevs;
922 };
923
924 /* Attributes have a name and a value. */
925 struct attribute
926 {
927 ENUM_BITFIELD(dwarf_attribute) name : 16;
928 ENUM_BITFIELD(dwarf_form) form : 15;
929
930 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
931 field should be in u.str (existing only for DW_STRING) but it is kept
932 here for better struct attribute alignment. */
933 unsigned int string_is_canonical : 1;
934
935 union
936 {
937 char *str;
938 struct dwarf_block *blk;
939 ULONGEST unsnd;
940 LONGEST snd;
941 CORE_ADDR addr;
942 struct signatured_type *signatured_type;
943 }
944 u;
945 };
946
947 /* This data structure holds a complete die structure. */
948 struct die_info
949 {
950 /* DWARF-2 tag for this DIE. */
951 ENUM_BITFIELD(dwarf_tag) tag : 16;
952
953 /* Number of attributes */
954 unsigned char num_attrs;
955
956 /* True if we're presently building the full type name for the
957 type derived from this DIE. */
958 unsigned char building_fullname : 1;
959
960 /* Abbrev number */
961 unsigned int abbrev;
962
963 /* Offset in .debug_info or .debug_types section. */
964 sect_offset offset;
965
966 /* The dies in a compilation unit form an n-ary tree. PARENT
967 points to this die's parent; CHILD points to the first child of
968 this node; and all the children of a given node are chained
969 together via their SIBLING fields. */
970 struct die_info *child; /* Its first child, if any. */
971 struct die_info *sibling; /* Its next sibling, if any. */
972 struct die_info *parent; /* Its parent, if any. */
973
974 /* An array of attributes, with NUM_ATTRS elements. There may be
975 zero, but it's not common and zero-sized arrays are not
976 sufficiently portable C. */
977 struct attribute attrs[1];
978 };
979
980 /* Get at parts of an attribute structure. */
981
982 #define DW_STRING(attr) ((attr)->u.str)
983 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
984 #define DW_UNSND(attr) ((attr)->u.unsnd)
985 #define DW_BLOCK(attr) ((attr)->u.blk)
986 #define DW_SND(attr) ((attr)->u.snd)
987 #define DW_ADDR(attr) ((attr)->u.addr)
988 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
989
990 /* Blocks are a bunch of untyped bytes. */
991 struct dwarf_block
992 {
993 unsigned int size;
994
995 /* Valid only if SIZE is not zero. */
996 gdb_byte *data;
997 };
998
999 #ifndef ATTR_ALLOC_CHUNK
1000 #define ATTR_ALLOC_CHUNK 4
1001 #endif
1002
1003 /* Allocate fields for structs, unions and enums in this size. */
1004 #ifndef DW_FIELD_ALLOC_CHUNK
1005 #define DW_FIELD_ALLOC_CHUNK 4
1006 #endif
1007
1008 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1009 but this would require a corresponding change in unpack_field_as_long
1010 and friends. */
1011 static int bits_per_byte = 8;
1012
1013 /* The routines that read and process dies for a C struct or C++ class
1014 pass lists of data member fields and lists of member function fields
1015 in an instance of a field_info structure, as defined below. */
1016 struct field_info
1017 {
1018 /* List of data member and baseclasses fields. */
1019 struct nextfield
1020 {
1021 struct nextfield *next;
1022 int accessibility;
1023 int virtuality;
1024 struct field field;
1025 }
1026 *fields, *baseclasses;
1027
1028 /* Number of fields (including baseclasses). */
1029 int nfields;
1030
1031 /* Number of baseclasses. */
1032 int nbaseclasses;
1033
1034 /* Set if the accesibility of one of the fields is not public. */
1035 int non_public_fields;
1036
1037 /* Member function fields array, entries are allocated in the order they
1038 are encountered in the object file. */
1039 struct nextfnfield
1040 {
1041 struct nextfnfield *next;
1042 struct fn_field fnfield;
1043 }
1044 *fnfields;
1045
1046 /* Member function fieldlist array, contains name of possibly overloaded
1047 member function, number of overloaded member functions and a pointer
1048 to the head of the member function field chain. */
1049 struct fnfieldlist
1050 {
1051 char *name;
1052 int length;
1053 struct nextfnfield *head;
1054 }
1055 *fnfieldlists;
1056
1057 /* Number of entries in the fnfieldlists array. */
1058 int nfnfields;
1059
1060 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1061 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1062 struct typedef_field_list
1063 {
1064 struct typedef_field field;
1065 struct typedef_field_list *next;
1066 }
1067 *typedef_field_list;
1068 unsigned typedef_field_list_count;
1069 };
1070
1071 /* One item on the queue of compilation units to read in full symbols
1072 for. */
1073 struct dwarf2_queue_item
1074 {
1075 struct dwarf2_per_cu_data *per_cu;
1076 enum language pretend_language;
1077 struct dwarf2_queue_item *next;
1078 };
1079
1080 /* The current queue. */
1081 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1082
1083 /* Loaded secondary compilation units are kept in memory until they
1084 have not been referenced for the processing of this many
1085 compilation units. Set this to zero to disable caching. Cache
1086 sizes of up to at least twenty will improve startup time for
1087 typical inter-CU-reference binaries, at an obvious memory cost. */
1088 static int dwarf2_max_cache_age = 5;
1089 static void
1090 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1091 struct cmd_list_element *c, const char *value)
1092 {
1093 fprintf_filtered (file, _("The upper bound on the age of cached "
1094 "dwarf2 compilation units is %s.\n"),
1095 value);
1096 }
1097
1098
1099 /* Various complaints about symbol reading that don't abort the process. */
1100
1101 static void
1102 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1103 {
1104 complaint (&symfile_complaints,
1105 _("statement list doesn't fit in .debug_line section"));
1106 }
1107
1108 static void
1109 dwarf2_debug_line_missing_file_complaint (void)
1110 {
1111 complaint (&symfile_complaints,
1112 _(".debug_line section has line data without a file"));
1113 }
1114
1115 static void
1116 dwarf2_debug_line_missing_end_sequence_complaint (void)
1117 {
1118 complaint (&symfile_complaints,
1119 _(".debug_line section has line "
1120 "program sequence without an end"));
1121 }
1122
1123 static void
1124 dwarf2_complex_location_expr_complaint (void)
1125 {
1126 complaint (&symfile_complaints, _("location expression too complex"));
1127 }
1128
1129 static void
1130 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1131 int arg3)
1132 {
1133 complaint (&symfile_complaints,
1134 _("const value length mismatch for '%s', got %d, expected %d"),
1135 arg1, arg2, arg3);
1136 }
1137
1138 static void
1139 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1140 {
1141 complaint (&symfile_complaints,
1142 _("debug info runs off end of %s section"
1143 " [in module %s]"),
1144 section->asection->name,
1145 bfd_get_filename (section->asection->owner));
1146 }
1147
1148 static void
1149 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1150 {
1151 complaint (&symfile_complaints,
1152 _("macro debug info contains a "
1153 "malformed macro definition:\n`%s'"),
1154 arg1);
1155 }
1156
1157 static void
1158 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1159 {
1160 complaint (&symfile_complaints,
1161 _("invalid attribute class or form for '%s' in '%s'"),
1162 arg1, arg2);
1163 }
1164
1165 /* local function prototypes */
1166
1167 static void dwarf2_locate_sections (bfd *, asection *, void *);
1168
1169 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1170 struct objfile *);
1171
1172 static void dwarf2_find_base_address (struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
1175 static void dwarf2_build_psymtabs_hard (struct objfile *);
1176
1177 static void scan_partial_symbols (struct partial_die_info *,
1178 CORE_ADDR *, CORE_ADDR *,
1179 int, struct dwarf2_cu *);
1180
1181 static void add_partial_symbol (struct partial_die_info *,
1182 struct dwarf2_cu *);
1183
1184 static void add_partial_namespace (struct partial_die_info *pdi,
1185 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1186 int need_pc, struct dwarf2_cu *cu);
1187
1188 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1189 CORE_ADDR *highpc, int need_pc,
1190 struct dwarf2_cu *cu);
1191
1192 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1193 struct dwarf2_cu *cu);
1194
1195 static void add_partial_subprogram (struct partial_die_info *pdi,
1196 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1197 int need_pc, struct dwarf2_cu *cu);
1198
1199 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1200
1201 static void psymtab_to_symtab_1 (struct partial_symtab *);
1202
1203 static struct abbrev_info *abbrev_table_lookup_abbrev
1204 (const struct abbrev_table *, unsigned int);
1205
1206 static struct abbrev_table *abbrev_table_read_table
1207 (struct dwarf2_section_info *, sect_offset);
1208
1209 static void abbrev_table_free (struct abbrev_table *);
1210
1211 static void abbrev_table_free_cleanup (void *);
1212
1213 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1214 struct dwarf2_section_info *);
1215
1216 static void dwarf2_free_abbrev_table (void *);
1217
1218 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1219
1220 static struct partial_die_info *load_partial_dies
1221 (const struct die_reader_specs *, gdb_byte *, int);
1222
1223 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1224 struct partial_die_info *,
1225 struct abbrev_info *,
1226 unsigned int,
1227 gdb_byte *);
1228
1229 static struct partial_die_info *find_partial_die (sect_offset, int,
1230 struct dwarf2_cu *);
1231
1232 static void fixup_partial_die (struct partial_die_info *,
1233 struct dwarf2_cu *);
1234
1235 static gdb_byte *read_attribute (const struct die_reader_specs *,
1236 struct attribute *, struct attr_abbrev *,
1237 gdb_byte *);
1238
1239 static unsigned int read_1_byte (bfd *, gdb_byte *);
1240
1241 static int read_1_signed_byte (bfd *, gdb_byte *);
1242
1243 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1244
1245 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1246
1247 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1248
1249 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1250 unsigned int *);
1251
1252 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1253
1254 static LONGEST read_checked_initial_length_and_offset
1255 (bfd *, gdb_byte *, const struct comp_unit_head *,
1256 unsigned int *, unsigned int *);
1257
1258 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1259 unsigned int *);
1260
1261 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1262
1263 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1264 sect_offset);
1265
1266 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1267
1268 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1269
1270 static char *read_indirect_string (bfd *, gdb_byte *,
1271 const struct comp_unit_head *,
1272 unsigned int *);
1273
1274 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1275
1276 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1277
1278 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1279
1280 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1281 unsigned int *);
1282
1283 static char *read_str_index (const struct die_reader_specs *reader,
1284 struct dwarf2_cu *cu, ULONGEST str_index);
1285
1286 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1287
1288 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
1290
1291 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1292 unsigned int);
1293
1294 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295 struct dwarf2_cu *cu);
1296
1297 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1298
1299 static struct die_info *die_specification (struct die_info *die,
1300 struct dwarf2_cu **);
1301
1302 static void free_line_header (struct line_header *lh);
1303
1304 static void add_file_name (struct line_header *, char *, unsigned int,
1305 unsigned int, unsigned int);
1306
1307 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1308 struct dwarf2_cu *cu);
1309
1310 static void dwarf_decode_lines (struct line_header *, const char *,
1311 struct dwarf2_cu *, struct partial_symtab *,
1312 int);
1313
1314 static void dwarf2_start_subfile (char *, const char *, const char *);
1315
1316 static void dwarf2_start_symtab (struct dwarf2_cu *,
1317 char *, char *, CORE_ADDR);
1318
1319 static struct symbol *new_symbol (struct die_info *, struct type *,
1320 struct dwarf2_cu *);
1321
1322 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1323 struct dwarf2_cu *, struct symbol *);
1324
1325 static void dwarf2_const_value (struct attribute *, struct symbol *,
1326 struct dwarf2_cu *);
1327
1328 static void dwarf2_const_value_attr (struct attribute *attr,
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
1332 struct dwarf2_cu *cu, LONGEST *value,
1333 gdb_byte **bytes,
1334 struct dwarf2_locexpr_baton **baton);
1335
1336 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1337
1338 static int need_gnat_info (struct dwarf2_cu *);
1339
1340 static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1350 struct dwarf2_cu *);
1351
1352 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
1356 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1357
1358 static char *typename_concat (struct obstack *obs, const char *prefix,
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
1361
1362 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1363
1364 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
1366 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1367
1368 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1369
1370 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
1372 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1373 struct dwarf2_cu *, struct partial_symtab *);
1374
1375 static int dwarf2_get_pc_bounds (struct die_info *,
1376 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1377 struct partial_symtab *);
1378
1379 static void get_scope_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *);
1382
1383 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1384 CORE_ADDR, struct dwarf2_cu *);
1385
1386 static void dwarf2_add_field (struct field_info *, struct die_info *,
1387 struct dwarf2_cu *);
1388
1389 static void dwarf2_attach_fields_to_type (struct field_info *,
1390 struct type *, struct dwarf2_cu *);
1391
1392 static void dwarf2_add_member_fn (struct field_info *,
1393 struct die_info *, struct type *,
1394 struct dwarf2_cu *);
1395
1396 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1397 struct type *,
1398 struct dwarf2_cu *);
1399
1400 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1401
1402 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1403
1404 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1405
1406 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1407
1408 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1409
1410 static struct type *read_module_type (struct die_info *die,
1411 struct dwarf2_cu *cu);
1412
1413 static const char *namespace_name (struct die_info *die,
1414 int *is_anonymous, struct dwarf2_cu *);
1415
1416 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1417
1418 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1419
1420 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1421 struct dwarf2_cu *);
1422
1423 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1424 gdb_byte *info_ptr,
1425 gdb_byte **new_info_ptr,
1426 struct die_info *parent);
1427
1428 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1429 gdb_byte *info_ptr,
1430 gdb_byte **new_info_ptr,
1431 struct die_info *parent);
1432
1433 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1434 struct die_info **, gdb_byte *, int *, int);
1435
1436 static gdb_byte *read_full_die (const struct die_reader_specs *,
1437 struct die_info **, gdb_byte *, int *);
1438
1439 static void process_die (struct die_info *, struct dwarf2_cu *);
1440
1441 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1442 struct obstack *);
1443
1444 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1445
1446 static const char *dwarf2_full_name (char *name,
1447 struct die_info *die,
1448 struct dwarf2_cu *cu);
1449
1450 static struct die_info *dwarf2_extension (struct die_info *die,
1451 struct dwarf2_cu **);
1452
1453 static const char *dwarf_tag_name (unsigned int);
1454
1455 static const char *dwarf_attr_name (unsigned int);
1456
1457 static const char *dwarf_form_name (unsigned int);
1458
1459 static char *dwarf_bool_name (unsigned int);
1460
1461 static const char *dwarf_type_encoding_name (unsigned int);
1462
1463 static struct die_info *sibling_die (struct die_info *);
1464
1465 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1466
1467 static void dump_die_for_error (struct die_info *);
1468
1469 static void dump_die_1 (struct ui_file *, int level, int max_level,
1470 struct die_info *);
1471
1472 /*static*/ void dump_die (struct die_info *, int max_level);
1473
1474 static void store_in_ref_table (struct die_info *,
1475 struct dwarf2_cu *);
1476
1477 static int is_ref_attr (struct attribute *);
1478
1479 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1480
1481 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1482
1483 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1484 struct attribute *,
1485 struct dwarf2_cu **);
1486
1487 static struct die_info *follow_die_ref (struct die_info *,
1488 struct attribute *,
1489 struct dwarf2_cu **);
1490
1491 static struct die_info *follow_die_sig (struct die_info *,
1492 struct attribute *,
1493 struct dwarf2_cu **);
1494
1495 static struct signatured_type *lookup_signatured_type_at_offset
1496 (struct objfile *objfile,
1497 struct dwarf2_section_info *section, sect_offset offset);
1498
1499 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1500
1501 static void read_signatured_type (struct signatured_type *);
1502
1503 static struct type_unit_group *get_type_unit_group
1504 (struct dwarf2_cu *, struct attribute *);
1505
1506 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1507
1508 /* memory allocation interface */
1509
1510 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1511
1512 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1513
1514 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1515 char *, int);
1516
1517 static int attr_form_is_block (struct attribute *);
1518
1519 static int attr_form_is_section_offset (struct attribute *);
1520
1521 static int attr_form_is_constant (struct attribute *);
1522
1523 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1524 struct dwarf2_loclist_baton *baton,
1525 struct attribute *attr);
1526
1527 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1528 struct symbol *sym,
1529 struct dwarf2_cu *cu);
1530
1531 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1532 gdb_byte *info_ptr,
1533 struct abbrev_info *abbrev);
1534
1535 static void free_stack_comp_unit (void *);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1543
1544 static void init_one_comp_unit (struct dwarf2_cu *cu,
1545 struct dwarf2_per_cu_data *per_cu);
1546
1547 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1548 struct die_info *comp_unit_die,
1549 enum language pretend_language);
1550
1551 static void free_heap_comp_unit (void *);
1552
1553 static void free_cached_comp_units (void *);
1554
1555 static void age_cached_comp_units (void);
1556
1557 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1558
1559 static struct type *set_die_type (struct die_info *, struct type *,
1560 struct dwarf2_cu *);
1561
1562 static void create_all_comp_units (struct objfile *);
1563
1564 static int create_all_type_units (struct objfile *);
1565
1566 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1570 enum language);
1571
1572 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1573 enum language);
1574
1575 static void dwarf2_add_dependence (struct dwarf2_cu *,
1576 struct dwarf2_per_cu_data *);
1577
1578 static void dwarf2_mark (struct dwarf2_cu *);
1579
1580 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1581
1582 static struct type *get_die_type_at_offset (sect_offset,
1583 struct dwarf2_per_cu_data *per_cu);
1584
1585 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1586
1587 static void dwarf2_release_queue (void *dummy);
1588
1589 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1590 enum language pretend_language);
1591
1592 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1593 struct dwarf2_per_cu_data *per_cu,
1594 enum language pretend_language);
1595
1596 static void process_queue (void);
1597
1598 static void find_file_and_directory (struct die_info *die,
1599 struct dwarf2_cu *cu,
1600 char **name, char **comp_dir);
1601
1602 static char *file_full_name (int file, struct line_header *lh,
1603 const char *comp_dir);
1604
1605 static gdb_byte *read_and_check_comp_unit_head
1606 (struct comp_unit_head *header,
1607 struct dwarf2_section_info *section,
1608 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1609 int is_debug_types_section);
1610
1611 static void init_cutu_and_read_dies
1612 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1613 int use_existing_cu, int keep,
1614 die_reader_func_ftype *die_reader_func, void *data);
1615
1616 static void init_cutu_and_read_dies_simple
1617 (struct dwarf2_per_cu_data *this_cu,
1618 die_reader_func_ftype *die_reader_func, void *data);
1619
1620 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1621
1622 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1623
1624 static struct dwo_unit *lookup_dwo_comp_unit
1625 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1626
1627 static struct dwo_unit *lookup_dwo_type_unit
1628 (struct signatured_type *, char *, const char *);
1629
1630 static void free_dwo_file_cleanup (void *);
1631
1632 static void process_cu_includes (void);
1633
1634 #if WORDS_BIGENDIAN
1635
1636 /* Convert VALUE between big- and little-endian. */
1637 static offset_type
1638 byte_swap (offset_type value)
1639 {
1640 offset_type result;
1641
1642 result = (value & 0xff) << 24;
1643 result |= (value & 0xff00) << 8;
1644 result |= (value & 0xff0000) >> 8;
1645 result |= (value & 0xff000000) >> 24;
1646 return result;
1647 }
1648
1649 #define MAYBE_SWAP(V) byte_swap (V)
1650
1651 #else
1652 #define MAYBE_SWAP(V) (V)
1653 #endif /* WORDS_BIGENDIAN */
1654
1655 /* The suffix for an index file. */
1656 #define INDEX_SUFFIX ".gdb-index"
1657
1658 static const char *dwarf2_physname (char *name, struct die_info *die,
1659 struct dwarf2_cu *cu);
1660
1661 /* Try to locate the sections we need for DWARF 2 debugging
1662 information and return true if we have enough to do something.
1663 NAMES points to the dwarf2 section names, or is NULL if the standard
1664 ELF names are used. */
1665
1666 int
1667 dwarf2_has_info (struct objfile *objfile,
1668 const struct dwarf2_debug_sections *names)
1669 {
1670 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1671 if (!dwarf2_per_objfile)
1672 {
1673 /* Initialize per-objfile state. */
1674 struct dwarf2_per_objfile *data
1675 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1676
1677 memset (data, 0, sizeof (*data));
1678 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1679 dwarf2_per_objfile = data;
1680
1681 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1682 (void *) names);
1683 dwarf2_per_objfile->objfile = objfile;
1684 }
1685 return (dwarf2_per_objfile->info.asection != NULL
1686 && dwarf2_per_objfile->abbrev.asection != NULL);
1687 }
1688
1689 /* When loading sections, we look either for uncompressed section or for
1690 compressed section names. */
1691
1692 static int
1693 section_is_p (const char *section_name,
1694 const struct dwarf2_section_names *names)
1695 {
1696 if (names->normal != NULL
1697 && strcmp (section_name, names->normal) == 0)
1698 return 1;
1699 if (names->compressed != NULL
1700 && strcmp (section_name, names->compressed) == 0)
1701 return 1;
1702 return 0;
1703 }
1704
1705 /* This function is mapped across the sections and remembers the
1706 offset and size of each of the debugging sections we are interested
1707 in. */
1708
1709 static void
1710 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1711 {
1712 const struct dwarf2_debug_sections *names;
1713
1714 if (vnames == NULL)
1715 names = &dwarf2_elf_names;
1716 else
1717 names = (const struct dwarf2_debug_sections *) vnames;
1718
1719 if (section_is_p (sectp->name, &names->info))
1720 {
1721 dwarf2_per_objfile->info.asection = sectp;
1722 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1723 }
1724 else if (section_is_p (sectp->name, &names->abbrev))
1725 {
1726 dwarf2_per_objfile->abbrev.asection = sectp;
1727 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1728 }
1729 else if (section_is_p (sectp->name, &names->line))
1730 {
1731 dwarf2_per_objfile->line.asection = sectp;
1732 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1733 }
1734 else if (section_is_p (sectp->name, &names->loc))
1735 {
1736 dwarf2_per_objfile->loc.asection = sectp;
1737 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1738 }
1739 else if (section_is_p (sectp->name, &names->macinfo))
1740 {
1741 dwarf2_per_objfile->macinfo.asection = sectp;
1742 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1743 }
1744 else if (section_is_p (sectp->name, &names->macro))
1745 {
1746 dwarf2_per_objfile->macro.asection = sectp;
1747 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1748 }
1749 else if (section_is_p (sectp->name, &names->str))
1750 {
1751 dwarf2_per_objfile->str.asection = sectp;
1752 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1753 }
1754 else if (section_is_p (sectp->name, &names->addr))
1755 {
1756 dwarf2_per_objfile->addr.asection = sectp;
1757 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1758 }
1759 else if (section_is_p (sectp->name, &names->frame))
1760 {
1761 dwarf2_per_objfile->frame.asection = sectp;
1762 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1763 }
1764 else if (section_is_p (sectp->name, &names->eh_frame))
1765 {
1766 flagword aflag = bfd_get_section_flags (abfd, sectp);
1767
1768 if (aflag & SEC_HAS_CONTENTS)
1769 {
1770 dwarf2_per_objfile->eh_frame.asection = sectp;
1771 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1772 }
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 --use-deprecated-index-sections command line
2615 option was supplied. */
2616 if (version < 6 && !deprecated_ok)
2617 {
2618 static int warning_printed = 0;
2619 if (!warning_printed)
2620 {
2621 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2622 "--use-deprecated-index-sections to use them anyway"),
2623 filename);
2624 warning_printed = 1;
2625 }
2626 return 0;
2627 }
2628 /* Indexes with higher version than the one supported by GDB may be no
2629 longer backward compatible. */
2630 if (version > 7)
2631 return 0;
2632
2633 map->version = version;
2634 map->total_size = section->size;
2635
2636 metadata = (offset_type *) (addr + sizeof (offset_type));
2637
2638 i = 0;
2639 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2640 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2641 / 8);
2642 ++i;
2643
2644 *types_list = addr + MAYBE_SWAP (metadata[i]);
2645 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2646 - MAYBE_SWAP (metadata[i]))
2647 / 8);
2648 ++i;
2649
2650 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2651 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2652 - MAYBE_SWAP (metadata[i]));
2653 ++i;
2654
2655 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2656 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2657 - MAYBE_SWAP (metadata[i]))
2658 / (2 * sizeof (offset_type)));
2659 ++i;
2660
2661 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2662
2663 return 1;
2664 }
2665
2666
2667 /* Read the index file. If everything went ok, initialize the "quick"
2668 elements of all the CUs and return 1. Otherwise, return 0. */
2669
2670 static int
2671 dwarf2_read_index (struct objfile *objfile)
2672 {
2673 struct mapped_index local_map, *map;
2674 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2675 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2676
2677 if (!read_index_from_section (objfile, objfile->name,
2678 use_deprecated_index_sections,
2679 &dwarf2_per_objfile->gdb_index, &local_map,
2680 &cu_list, &cu_list_elements,
2681 &types_list, &types_list_elements))
2682 return 0;
2683
2684 /* Don't use the index if it's empty. */
2685 if (local_map.symbol_table_slots == 0)
2686 return 0;
2687
2688 /* If there is a .dwz file, read it so we can get its CU list as
2689 well. */
2690 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2691 {
2692 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2693 struct mapped_index dwz_map;
2694 const gdb_byte *dwz_types_ignore;
2695 offset_type dwz_types_elements_ignore;
2696
2697 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2698 1,
2699 &dwz->gdb_index, &dwz_map,
2700 &dwz_list, &dwz_list_elements,
2701 &dwz_types_ignore,
2702 &dwz_types_elements_ignore))
2703 {
2704 warning (_("could not read '.gdb_index' section from %s; skipping"),
2705 bfd_get_filename (dwz->dwz_bfd));
2706 return 0;
2707 }
2708 }
2709
2710 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2711 dwz_list, dwz_list_elements))
2712 return 0;
2713
2714 if (types_list_elements)
2715 {
2716 struct dwarf2_section_info *section;
2717
2718 /* We can only handle a single .debug_types when we have an
2719 index. */
2720 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2721 return 0;
2722
2723 section = VEC_index (dwarf2_section_info_def,
2724 dwarf2_per_objfile->types, 0);
2725
2726 if (!create_signatured_type_table_from_index (objfile, section,
2727 types_list,
2728 types_list_elements))
2729 return 0;
2730 }
2731
2732 create_addrmap_from_index (objfile, &local_map);
2733
2734 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2735 *map = local_map;
2736
2737 dwarf2_per_objfile->index_table = map;
2738 dwarf2_per_objfile->using_index = 1;
2739 dwarf2_per_objfile->quick_file_names_table =
2740 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2741
2742 return 1;
2743 }
2744
2745 /* A helper for the "quick" functions which sets the global
2746 dwarf2_per_objfile according to OBJFILE. */
2747
2748 static void
2749 dw2_setup (struct objfile *objfile)
2750 {
2751 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2752 gdb_assert (dwarf2_per_objfile);
2753 }
2754
2755 /* Reader function for dw2_build_type_unit_groups. */
2756
2757 static void
2758 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2759 gdb_byte *info_ptr,
2760 struct die_info *type_unit_die,
2761 int has_children,
2762 void *data)
2763 {
2764 struct dwarf2_cu *cu = reader->cu;
2765 struct attribute *attr;
2766 struct type_unit_group *tu_group;
2767
2768 gdb_assert (data == NULL);
2769
2770 if (! has_children)
2771 return;
2772
2773 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2774 /* Call this for its side-effect of creating the associated
2775 struct type_unit_group if it doesn't already exist. */
2776 tu_group = get_type_unit_group (cu, attr);
2777 }
2778
2779 /* Build dwarf2_per_objfile->type_unit_groups.
2780 This function may be called multiple times. */
2781
2782 static void
2783 dw2_build_type_unit_groups (void)
2784 {
2785 if (dwarf2_per_objfile->type_unit_groups == NULL)
2786 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2787 }
2788
2789 /* die_reader_func for dw2_get_file_names. */
2790
2791 static void
2792 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2793 gdb_byte *info_ptr,
2794 struct die_info *comp_unit_die,
2795 int has_children,
2796 void *data)
2797 {
2798 struct dwarf2_cu *cu = reader->cu;
2799 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2800 struct objfile *objfile = dwarf2_per_objfile->objfile;
2801 struct dwarf2_per_cu_data *lh_cu;
2802 struct line_header *lh;
2803 struct attribute *attr;
2804 int i;
2805 char *name, *comp_dir;
2806 void **slot;
2807 struct quick_file_names *qfn;
2808 unsigned int line_offset;
2809
2810 /* Our callers never want to match partial units -- instead they
2811 will match the enclosing full CU. */
2812 if (comp_unit_die->tag == DW_TAG_partial_unit)
2813 {
2814 this_cu->v.quick->no_file_data = 1;
2815 return;
2816 }
2817
2818 /* If we're reading the line header for TUs, store it in the "per_cu"
2819 for tu_group. */
2820 if (this_cu->is_debug_types)
2821 {
2822 struct type_unit_group *tu_group = data;
2823
2824 gdb_assert (tu_group != NULL);
2825 lh_cu = &tu_group->per_cu;
2826 }
2827 else
2828 lh_cu = this_cu;
2829
2830 lh = NULL;
2831 slot = NULL;
2832 line_offset = 0;
2833
2834 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2835 if (attr)
2836 {
2837 struct quick_file_names find_entry;
2838
2839 line_offset = DW_UNSND (attr);
2840
2841 /* We may have already read in this line header (TU line header sharing).
2842 If we have we're done. */
2843 find_entry.hash.dwo_unit = cu->dwo_unit;
2844 find_entry.hash.line_offset.sect_off = line_offset;
2845 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2846 &find_entry, INSERT);
2847 if (*slot != NULL)
2848 {
2849 lh_cu->v.quick->file_names = *slot;
2850 return;
2851 }
2852
2853 lh = dwarf_decode_line_header (line_offset, cu);
2854 }
2855 if (lh == NULL)
2856 {
2857 lh_cu->v.quick->no_file_data = 1;
2858 return;
2859 }
2860
2861 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2862 qfn->hash.dwo_unit = cu->dwo_unit;
2863 qfn->hash.line_offset.sect_off = line_offset;
2864 gdb_assert (slot != NULL);
2865 *slot = qfn;
2866
2867 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2868
2869 qfn->num_file_names = lh->num_file_names;
2870 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2871 lh->num_file_names * sizeof (char *));
2872 for (i = 0; i < lh->num_file_names; ++i)
2873 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2874 qfn->real_names = NULL;
2875
2876 free_line_header (lh);
2877
2878 lh_cu->v.quick->file_names = qfn;
2879 }
2880
2881 /* A helper for the "quick" functions which attempts to read the line
2882 table for THIS_CU. */
2883
2884 static struct quick_file_names *
2885 dw2_get_file_names (struct objfile *objfile,
2886 struct dwarf2_per_cu_data *this_cu)
2887 {
2888 /* For TUs this should only be called on the parent group. */
2889 if (this_cu->is_debug_types)
2890 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2891
2892 if (this_cu->v.quick->file_names != NULL)
2893 return this_cu->v.quick->file_names;
2894 /* If we know there is no line data, no point in looking again. */
2895 if (this_cu->v.quick->no_file_data)
2896 return NULL;
2897
2898 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2899 in the stub for CUs, there's is no need to lookup the DWO file.
2900 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2901 DWO file. */
2902 if (this_cu->is_debug_types)
2903 {
2904 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2905
2906 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2907 dw2_get_file_names_reader, tu_group);
2908 }
2909 else
2910 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2911
2912 if (this_cu->v.quick->no_file_data)
2913 return NULL;
2914 return this_cu->v.quick->file_names;
2915 }
2916
2917 /* A helper for the "quick" functions which computes and caches the
2918 real path for a given file name from the line table. */
2919
2920 static const char *
2921 dw2_get_real_path (struct objfile *objfile,
2922 struct quick_file_names *qfn, int index)
2923 {
2924 if (qfn->real_names == NULL)
2925 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2926 qfn->num_file_names, sizeof (char *));
2927
2928 if (qfn->real_names[index] == NULL)
2929 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2930
2931 return qfn->real_names[index];
2932 }
2933
2934 static struct symtab *
2935 dw2_find_last_source_symtab (struct objfile *objfile)
2936 {
2937 int index;
2938
2939 dw2_setup (objfile);
2940 index = dwarf2_per_objfile->n_comp_units - 1;
2941 return dw2_instantiate_symtab (dw2_get_cu (index));
2942 }
2943
2944 /* Traversal function for dw2_forget_cached_source_info. */
2945
2946 static int
2947 dw2_free_cached_file_names (void **slot, void *info)
2948 {
2949 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2950
2951 if (file_data->real_names)
2952 {
2953 int i;
2954
2955 for (i = 0; i < file_data->num_file_names; ++i)
2956 {
2957 xfree ((void*) file_data->real_names[i]);
2958 file_data->real_names[i] = NULL;
2959 }
2960 }
2961
2962 return 1;
2963 }
2964
2965 static void
2966 dw2_forget_cached_source_info (struct objfile *objfile)
2967 {
2968 dw2_setup (objfile);
2969
2970 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2971 dw2_free_cached_file_names, NULL);
2972 }
2973
2974 /* Helper function for dw2_map_symtabs_matching_filename that expands
2975 the symtabs and calls the iterator. */
2976
2977 static int
2978 dw2_map_expand_apply (struct objfile *objfile,
2979 struct dwarf2_per_cu_data *per_cu,
2980 const char *name,
2981 const char *full_path, const char *real_path,
2982 int (*callback) (struct symtab *, void *),
2983 void *data)
2984 {
2985 struct symtab *last_made = objfile->symtabs;
2986
2987 /* Don't visit already-expanded CUs. */
2988 if (per_cu->v.quick->symtab)
2989 return 0;
2990
2991 /* This may expand more than one symtab, and we want to iterate over
2992 all of them. */
2993 dw2_instantiate_symtab (per_cu);
2994
2995 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2996 objfile->symtabs, last_made);
2997 }
2998
2999 /* Implementation of the map_symtabs_matching_filename method. */
3000
3001 static int
3002 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3003 const char *full_path, const char *real_path,
3004 int (*callback) (struct symtab *, void *),
3005 void *data)
3006 {
3007 int i;
3008 const char *name_basename = lbasename (name);
3009 int name_len = strlen (name);
3010 int is_abs = IS_ABSOLUTE_PATH (name);
3011
3012 dw2_setup (objfile);
3013
3014 dw2_build_type_unit_groups ();
3015
3016 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3017 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3018 {
3019 int j;
3020 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3021 struct quick_file_names *file_data;
3022
3023 /* We only need to look at symtabs not already expanded. */
3024 if (per_cu->v.quick->symtab)
3025 continue;
3026
3027 file_data = dw2_get_file_names (objfile, per_cu);
3028 if (file_data == NULL)
3029 continue;
3030
3031 for (j = 0; j < file_data->num_file_names; ++j)
3032 {
3033 const char *this_name = file_data->file_names[j];
3034
3035 if (FILENAME_CMP (name, this_name) == 0
3036 || (!is_abs && compare_filenames_for_search (this_name,
3037 name, name_len)))
3038 {
3039 if (dw2_map_expand_apply (objfile, per_cu,
3040 name, full_path, real_path,
3041 callback, data))
3042 return 1;
3043 }
3044
3045 /* Before we invoke realpath, which can get expensive when many
3046 files are involved, do a quick comparison of the basenames. */
3047 if (! basenames_may_differ
3048 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3049 continue;
3050
3051 if (full_path != NULL)
3052 {
3053 const char *this_real_name = dw2_get_real_path (objfile,
3054 file_data, j);
3055
3056 if (this_real_name != NULL
3057 && (FILENAME_CMP (full_path, this_real_name) == 0
3058 || (!is_abs
3059 && compare_filenames_for_search (this_real_name,
3060 name, name_len))))
3061 {
3062 if (dw2_map_expand_apply (objfile, per_cu,
3063 name, full_path, real_path,
3064 callback, data))
3065 return 1;
3066 }
3067 }
3068
3069 if (real_path != NULL)
3070 {
3071 const char *this_real_name = dw2_get_real_path (objfile,
3072 file_data, j);
3073
3074 if (this_real_name != NULL
3075 && (FILENAME_CMP (real_path, this_real_name) == 0
3076 || (!is_abs
3077 && compare_filenames_for_search (this_real_name,
3078 name, name_len))))
3079 {
3080 if (dw2_map_expand_apply (objfile, per_cu,
3081 name, full_path, real_path,
3082 callback, data))
3083 return 1;
3084 }
3085 }
3086 }
3087 }
3088
3089 return 0;
3090 }
3091
3092 static struct symtab *
3093 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3094 const char *name, domain_enum domain)
3095 {
3096 /* We do all the work in the pre_expand_symtabs_matching hook
3097 instead. */
3098 return NULL;
3099 }
3100
3101 /* A helper function that expands all symtabs that hold an object
3102 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3103 symbols in block BLOCK_KIND. */
3104
3105 static void
3106 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3107 int want_specific_block,
3108 enum block_enum block_kind,
3109 const char *name, domain_enum domain)
3110 {
3111 struct mapped_index *index;
3112
3113 dw2_setup (objfile);
3114
3115 index = dwarf2_per_objfile->index_table;
3116
3117 /* index_table is NULL if OBJF_READNOW. */
3118 if (index)
3119 {
3120 offset_type *vec;
3121
3122 if (find_slot_in_mapped_hash (index, name, &vec))
3123 {
3124 offset_type i, len = MAYBE_SWAP (*vec);
3125 for (i = 0; i < len; ++i)
3126 {
3127 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3128 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3129 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3130 int want_static = block_kind != GLOBAL_BLOCK;
3131 /* This value is only valid for index versions >= 7. */
3132 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3133 gdb_index_symbol_kind symbol_kind =
3134 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3135
3136 if (want_specific_block
3137 && index->version >= 7
3138 && want_static != is_static)
3139 continue;
3140
3141 /* Only check the symbol's kind if it has one.
3142 Indices prior to version 7 don't record it. */
3143 if (index->version >= 7)
3144 {
3145 switch (domain)
3146 {
3147 case VAR_DOMAIN:
3148 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3149 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3150 /* Some types are also in VAR_DOMAIN. */
3151 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3152 continue;
3153 break;
3154 case STRUCT_DOMAIN:
3155 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3156 continue;
3157 break;
3158 case LABEL_DOMAIN:
3159 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3160 continue;
3161 break;
3162 default:
3163 break;
3164 }
3165 }
3166
3167 dw2_instantiate_symtab (per_cu);
3168 }
3169 }
3170 }
3171 }
3172
3173 static void
3174 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3175 enum block_enum block_kind, const char *name,
3176 domain_enum domain)
3177 {
3178 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3179 }
3180
3181 static void
3182 dw2_print_stats (struct objfile *objfile)
3183 {
3184 int i, count;
3185
3186 dw2_setup (objfile);
3187 count = 0;
3188 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3189 + dwarf2_per_objfile->n_type_units); ++i)
3190 {
3191 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3192
3193 if (!per_cu->v.quick->symtab)
3194 ++count;
3195 }
3196 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3197 }
3198
3199 static void
3200 dw2_dump (struct objfile *objfile)
3201 {
3202 /* Nothing worth printing. */
3203 }
3204
3205 static void
3206 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3207 struct section_offsets *delta)
3208 {
3209 /* There's nothing to relocate here. */
3210 }
3211
3212 static void
3213 dw2_expand_symtabs_for_function (struct objfile *objfile,
3214 const char *func_name)
3215 {
3216 /* Note: It doesn't matter what we pass for block_kind here. */
3217 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3218 VAR_DOMAIN);
3219 }
3220
3221 static void
3222 dw2_expand_all_symtabs (struct objfile *objfile)
3223 {
3224 int i;
3225
3226 dw2_setup (objfile);
3227
3228 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3229 + dwarf2_per_objfile->n_type_units); ++i)
3230 {
3231 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3232
3233 dw2_instantiate_symtab (per_cu);
3234 }
3235 }
3236
3237 static void
3238 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3239 const char *filename)
3240 {
3241 int i;
3242
3243 dw2_setup (objfile);
3244
3245 /* We don't need to consider type units here.
3246 This is only called for examining code, e.g. expand_line_sal.
3247 There can be an order of magnitude (or more) more type units
3248 than comp units, and we avoid them if we can. */
3249
3250 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3251 {
3252 int j;
3253 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3254 struct quick_file_names *file_data;
3255
3256 /* We only need to look at symtabs not already expanded. */
3257 if (per_cu->v.quick->symtab)
3258 continue;
3259
3260 file_data = dw2_get_file_names (objfile, per_cu);
3261 if (file_data == NULL)
3262 continue;
3263
3264 for (j = 0; j < file_data->num_file_names; ++j)
3265 {
3266 const char *this_name = file_data->file_names[j];
3267 if (FILENAME_CMP (this_name, filename) == 0)
3268 {
3269 dw2_instantiate_symtab (per_cu);
3270 break;
3271 }
3272 }
3273 }
3274 }
3275
3276 /* A helper function for dw2_find_symbol_file that finds the primary
3277 file name for a given CU. This is a die_reader_func. */
3278
3279 static void
3280 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3281 gdb_byte *info_ptr,
3282 struct die_info *comp_unit_die,
3283 int has_children,
3284 void *data)
3285 {
3286 const char **result_ptr = data;
3287 struct dwarf2_cu *cu = reader->cu;
3288 struct attribute *attr;
3289
3290 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3291 if (attr == NULL)
3292 *result_ptr = NULL;
3293 else
3294 *result_ptr = DW_STRING (attr);
3295 }
3296
3297 static const char *
3298 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3299 {
3300 struct dwarf2_per_cu_data *per_cu;
3301 offset_type *vec;
3302 struct quick_file_names *file_data;
3303 const char *filename;
3304
3305 dw2_setup (objfile);
3306
3307 /* index_table is NULL if OBJF_READNOW. */
3308 if (!dwarf2_per_objfile->index_table)
3309 {
3310 struct symtab *s;
3311
3312 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3313 {
3314 struct blockvector *bv = BLOCKVECTOR (s);
3315 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3316 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3317
3318 if (sym)
3319 return sym->symtab->filename;
3320 }
3321 return NULL;
3322 }
3323
3324 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3325 name, &vec))
3326 return NULL;
3327
3328 /* Note that this just looks at the very first one named NAME -- but
3329 actually we are looking for a function. find_main_filename
3330 should be rewritten so that it doesn't require a custom hook. It
3331 could just use the ordinary symbol tables. */
3332 /* vec[0] is the length, which must always be >0. */
3333 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3334
3335 if (per_cu->v.quick->symtab != NULL)
3336 return per_cu->v.quick->symtab->filename;
3337
3338 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3339 dw2_get_primary_filename_reader, &filename);
3340
3341 return filename;
3342 }
3343
3344 static void
3345 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3346 struct objfile *objfile, int global,
3347 int (*callback) (struct block *,
3348 struct symbol *, void *),
3349 void *data, symbol_compare_ftype *match,
3350 symbol_compare_ftype *ordered_compare)
3351 {
3352 /* Currently unimplemented; used for Ada. The function can be called if the
3353 current language is Ada for a non-Ada objfile using GNU index. As Ada
3354 does not look for non-Ada symbols this function should just return. */
3355 }
3356
3357 static void
3358 dw2_expand_symtabs_matching
3359 (struct objfile *objfile,
3360 int (*file_matcher) (const char *, void *),
3361 int (*name_matcher) (const char *, void *),
3362 enum search_domain kind,
3363 void *data)
3364 {
3365 int i;
3366 offset_type iter;
3367 struct mapped_index *index;
3368
3369 dw2_setup (objfile);
3370
3371 /* index_table is NULL if OBJF_READNOW. */
3372 if (!dwarf2_per_objfile->index_table)
3373 return;
3374 index = dwarf2_per_objfile->index_table;
3375
3376 if (file_matcher != NULL)
3377 {
3378 struct cleanup *cleanup;
3379 htab_t visited_found, visited_not_found;
3380
3381 dw2_build_type_unit_groups ();
3382
3383 visited_found = htab_create_alloc (10,
3384 htab_hash_pointer, htab_eq_pointer,
3385 NULL, xcalloc, xfree);
3386 cleanup = make_cleanup_htab_delete (visited_found);
3387 visited_not_found = htab_create_alloc (10,
3388 htab_hash_pointer, htab_eq_pointer,
3389 NULL, xcalloc, xfree);
3390 make_cleanup_htab_delete (visited_not_found);
3391
3392 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3393 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3394 {
3395 int j;
3396 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3397 struct quick_file_names *file_data;
3398 void **slot;
3399
3400 per_cu->v.quick->mark = 0;
3401
3402 /* We only need to look at symtabs not already expanded. */
3403 if (per_cu->v.quick->symtab)
3404 continue;
3405
3406 file_data = dw2_get_file_names (objfile, per_cu);
3407 if (file_data == NULL)
3408 continue;
3409
3410 if (htab_find (visited_not_found, file_data) != NULL)
3411 continue;
3412 else if (htab_find (visited_found, file_data) != NULL)
3413 {
3414 per_cu->v.quick->mark = 1;
3415 continue;
3416 }
3417
3418 for (j = 0; j < file_data->num_file_names; ++j)
3419 {
3420 if (file_matcher (file_data->file_names[j], data))
3421 {
3422 per_cu->v.quick->mark = 1;
3423 break;
3424 }
3425 }
3426
3427 slot = htab_find_slot (per_cu->v.quick->mark
3428 ? visited_found
3429 : visited_not_found,
3430 file_data, INSERT);
3431 *slot = file_data;
3432 }
3433
3434 do_cleanups (cleanup);
3435 }
3436
3437 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3438 {
3439 offset_type idx = 2 * iter;
3440 const char *name;
3441 offset_type *vec, vec_len, vec_idx;
3442
3443 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3444 continue;
3445
3446 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3447
3448 if (! (*name_matcher) (name, data))
3449 continue;
3450
3451 /* The name was matched, now expand corresponding CUs that were
3452 marked. */
3453 vec = (offset_type *) (index->constant_pool
3454 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3455 vec_len = MAYBE_SWAP (vec[0]);
3456 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3457 {
3458 struct dwarf2_per_cu_data *per_cu;
3459 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3460 gdb_index_symbol_kind symbol_kind =
3461 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3462 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3463
3464 /* Don't crash on bad data. */
3465 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3466 + dwarf2_per_objfile->n_type_units))
3467 continue;
3468
3469 /* Only check the symbol's kind if it has one.
3470 Indices prior to version 7 don't record it. */
3471 if (index->version >= 7)
3472 {
3473 switch (kind)
3474 {
3475 case VARIABLES_DOMAIN:
3476 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3477 continue;
3478 break;
3479 case FUNCTIONS_DOMAIN:
3480 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3481 continue;
3482 break;
3483 case TYPES_DOMAIN:
3484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3485 continue;
3486 break;
3487 default:
3488 break;
3489 }
3490 }
3491
3492 per_cu = dw2_get_cu (cu_index);
3493 if (file_matcher == NULL || per_cu->v.quick->mark)
3494 dw2_instantiate_symtab (per_cu);
3495 }
3496 }
3497 }
3498
3499 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3500 symtab. */
3501
3502 static struct symtab *
3503 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3504 {
3505 int i;
3506
3507 if (BLOCKVECTOR (symtab) != NULL
3508 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3509 return symtab;
3510
3511 if (symtab->includes == NULL)
3512 return NULL;
3513
3514 for (i = 0; symtab->includes[i]; ++i)
3515 {
3516 struct symtab *s = symtab->includes[i];
3517
3518 s = recursively_find_pc_sect_symtab (s, pc);
3519 if (s != NULL)
3520 return s;
3521 }
3522
3523 return NULL;
3524 }
3525
3526 static struct symtab *
3527 dw2_find_pc_sect_symtab (struct objfile *objfile,
3528 struct minimal_symbol *msymbol,
3529 CORE_ADDR pc,
3530 struct obj_section *section,
3531 int warn_if_readin)
3532 {
3533 struct dwarf2_per_cu_data *data;
3534 struct symtab *result;
3535
3536 dw2_setup (objfile);
3537
3538 if (!objfile->psymtabs_addrmap)
3539 return NULL;
3540
3541 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3542 if (!data)
3543 return NULL;
3544
3545 if (warn_if_readin && data->v.quick->symtab)
3546 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3547 paddress (get_objfile_arch (objfile), pc));
3548
3549 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3550 gdb_assert (result != NULL);
3551 return result;
3552 }
3553
3554 static void
3555 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3556 void *data, int need_fullname)
3557 {
3558 int i;
3559 struct cleanup *cleanup;
3560 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3561 NULL, xcalloc, xfree);
3562
3563 cleanup = make_cleanup_htab_delete (visited);
3564 dw2_setup (objfile);
3565
3566 dw2_build_type_unit_groups ();
3567
3568 /* We can ignore file names coming from already-expanded CUs. */
3569 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3570 + dwarf2_per_objfile->n_type_units); ++i)
3571 {
3572 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3573
3574 if (per_cu->v.quick->symtab)
3575 {
3576 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3577 INSERT);
3578
3579 *slot = per_cu->v.quick->file_names;
3580 }
3581 }
3582
3583 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3584 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3585 {
3586 int j;
3587 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3588 struct quick_file_names *file_data;
3589 void **slot;
3590
3591 /* We only need to look at symtabs not already expanded. */
3592 if (per_cu->v.quick->symtab)
3593 continue;
3594
3595 file_data = dw2_get_file_names (objfile, per_cu);
3596 if (file_data == NULL)
3597 continue;
3598
3599 slot = htab_find_slot (visited, file_data, INSERT);
3600 if (*slot)
3601 {
3602 /* Already visited. */
3603 continue;
3604 }
3605 *slot = file_data;
3606
3607 for (j = 0; j < file_data->num_file_names; ++j)
3608 {
3609 const char *this_real_name;
3610
3611 if (need_fullname)
3612 this_real_name = dw2_get_real_path (objfile, file_data, j);
3613 else
3614 this_real_name = NULL;
3615 (*fun) (file_data->file_names[j], this_real_name, data);
3616 }
3617 }
3618
3619 do_cleanups (cleanup);
3620 }
3621
3622 static int
3623 dw2_has_symbols (struct objfile *objfile)
3624 {
3625 return 1;
3626 }
3627
3628 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3629 {
3630 dw2_has_symbols,
3631 dw2_find_last_source_symtab,
3632 dw2_forget_cached_source_info,
3633 dw2_map_symtabs_matching_filename,
3634 dw2_lookup_symbol,
3635 dw2_pre_expand_symtabs_matching,
3636 dw2_print_stats,
3637 dw2_dump,
3638 dw2_relocate,
3639 dw2_expand_symtabs_for_function,
3640 dw2_expand_all_symtabs,
3641 dw2_expand_symtabs_with_filename,
3642 dw2_find_symbol_file,
3643 dw2_map_matching_symbols,
3644 dw2_expand_symtabs_matching,
3645 dw2_find_pc_sect_symtab,
3646 dw2_map_symbol_filenames
3647 };
3648
3649 /* Initialize for reading DWARF for this objfile. Return 0 if this
3650 file will use psymtabs, or 1 if using the GNU index. */
3651
3652 int
3653 dwarf2_initialize_objfile (struct objfile *objfile)
3654 {
3655 /* If we're about to read full symbols, don't bother with the
3656 indices. In this case we also don't care if some other debug
3657 format is making psymtabs, because they are all about to be
3658 expanded anyway. */
3659 if ((objfile->flags & OBJF_READNOW))
3660 {
3661 int i;
3662
3663 dwarf2_per_objfile->using_index = 1;
3664 create_all_comp_units (objfile);
3665 create_all_type_units (objfile);
3666 dwarf2_per_objfile->quick_file_names_table =
3667 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3668
3669 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3670 + dwarf2_per_objfile->n_type_units); ++i)
3671 {
3672 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3673
3674 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3675 struct dwarf2_per_cu_quick_data);
3676 }
3677
3678 /* Return 1 so that gdb sees the "quick" functions. However,
3679 these functions will be no-ops because we will have expanded
3680 all symtabs. */
3681 return 1;
3682 }
3683
3684 if (dwarf2_read_index (objfile))
3685 return 1;
3686
3687 return 0;
3688 }
3689
3690 \f
3691
3692 /* Build a partial symbol table. */
3693
3694 void
3695 dwarf2_build_psymtabs (struct objfile *objfile)
3696 {
3697 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3698 {
3699 init_psymbol_list (objfile, 1024);
3700 }
3701
3702 dwarf2_build_psymtabs_hard (objfile);
3703 }
3704
3705 /* Return the total length of the CU described by HEADER. */
3706
3707 static unsigned int
3708 get_cu_length (const struct comp_unit_head *header)
3709 {
3710 return header->initial_length_size + header->length;
3711 }
3712
3713 /* Return TRUE if OFFSET is within CU_HEADER. */
3714
3715 static inline int
3716 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3717 {
3718 sect_offset bottom = { cu_header->offset.sect_off };
3719 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3720
3721 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3722 }
3723
3724 /* Find the base address of the compilation unit for range lists and
3725 location lists. It will normally be specified by DW_AT_low_pc.
3726 In DWARF-3 draft 4, the base address could be overridden by
3727 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3728 compilation units with discontinuous ranges. */
3729
3730 static void
3731 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3732 {
3733 struct attribute *attr;
3734
3735 cu->base_known = 0;
3736 cu->base_address = 0;
3737
3738 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3739 if (attr)
3740 {
3741 cu->base_address = DW_ADDR (attr);
3742 cu->base_known = 1;
3743 }
3744 else
3745 {
3746 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3747 if (attr)
3748 {
3749 cu->base_address = DW_ADDR (attr);
3750 cu->base_known = 1;
3751 }
3752 }
3753 }
3754
3755 /* Read in the comp unit header information from the debug_info at info_ptr.
3756 NOTE: This leaves members offset, first_die_offset to be filled in
3757 by the caller. */
3758
3759 static gdb_byte *
3760 read_comp_unit_head (struct comp_unit_head *cu_header,
3761 gdb_byte *info_ptr, bfd *abfd)
3762 {
3763 int signed_addr;
3764 unsigned int bytes_read;
3765
3766 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3767 cu_header->initial_length_size = bytes_read;
3768 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3769 info_ptr += bytes_read;
3770 cu_header->version = read_2_bytes (abfd, info_ptr);
3771 info_ptr += 2;
3772 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3773 &bytes_read);
3774 info_ptr += bytes_read;
3775 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3776 info_ptr += 1;
3777 signed_addr = bfd_get_sign_extend_vma (abfd);
3778 if (signed_addr < 0)
3779 internal_error (__FILE__, __LINE__,
3780 _("read_comp_unit_head: dwarf from non elf file"));
3781 cu_header->signed_addr_p = signed_addr;
3782
3783 return info_ptr;
3784 }
3785
3786 /* Helper function that returns the proper abbrev section for
3787 THIS_CU. */
3788
3789 static struct dwarf2_section_info *
3790 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3791 {
3792 struct dwarf2_section_info *abbrev;
3793
3794 if (this_cu->is_dwz)
3795 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3796 else
3797 abbrev = &dwarf2_per_objfile->abbrev;
3798
3799 return abbrev;
3800 }
3801
3802 /* Subroutine of read_and_check_comp_unit_head and
3803 read_and_check_type_unit_head to simplify them.
3804 Perform various error checking on the header. */
3805
3806 static void
3807 error_check_comp_unit_head (struct comp_unit_head *header,
3808 struct dwarf2_section_info *section,
3809 struct dwarf2_section_info *abbrev_section)
3810 {
3811 bfd *abfd = section->asection->owner;
3812 const char *filename = bfd_get_filename (abfd);
3813
3814 if (header->version != 2 && header->version != 3 && header->version != 4)
3815 error (_("Dwarf Error: wrong version in compilation unit header "
3816 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3817 filename);
3818
3819 if (header->abbrev_offset.sect_off
3820 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3821 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3822 "(offset 0x%lx + 6) [in module %s]"),
3823 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3824 filename);
3825
3826 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3827 avoid potential 32-bit overflow. */
3828 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3829 > section->size)
3830 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3831 "(offset 0x%lx + 0) [in module %s]"),
3832 (long) header->length, (long) header->offset.sect_off,
3833 filename);
3834 }
3835
3836 /* Read in a CU/TU header and perform some basic error checking.
3837 The contents of the header are stored in HEADER.
3838 The result is a pointer to the start of the first DIE. */
3839
3840 static gdb_byte *
3841 read_and_check_comp_unit_head (struct comp_unit_head *header,
3842 struct dwarf2_section_info *section,
3843 struct dwarf2_section_info *abbrev_section,
3844 gdb_byte *info_ptr,
3845 int is_debug_types_section)
3846 {
3847 gdb_byte *beg_of_comp_unit = info_ptr;
3848 bfd *abfd = section->asection->owner;
3849
3850 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3851
3852 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3853
3854 /* If we're reading a type unit, skip over the signature and
3855 type_offset fields. */
3856 if (is_debug_types_section)
3857 info_ptr += 8 /*signature*/ + header->offset_size;
3858
3859 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3860
3861 error_check_comp_unit_head (header, section, abbrev_section);
3862
3863 return info_ptr;
3864 }
3865
3866 /* Read in the types comp unit header information from .debug_types entry at
3867 types_ptr. The result is a pointer to one past the end of the header. */
3868
3869 static gdb_byte *
3870 read_and_check_type_unit_head (struct comp_unit_head *header,
3871 struct dwarf2_section_info *section,
3872 struct dwarf2_section_info *abbrev_section,
3873 gdb_byte *info_ptr,
3874 ULONGEST *signature,
3875 cu_offset *type_offset_in_tu)
3876 {
3877 gdb_byte *beg_of_comp_unit = info_ptr;
3878 bfd *abfd = section->asection->owner;
3879
3880 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3881
3882 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3883
3884 /* If we're reading a type unit, skip over the signature and
3885 type_offset fields. */
3886 if (signature != NULL)
3887 *signature = read_8_bytes (abfd, info_ptr);
3888 info_ptr += 8;
3889 if (type_offset_in_tu != NULL)
3890 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3891 header->offset_size);
3892 info_ptr += header->offset_size;
3893
3894 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3895
3896 error_check_comp_unit_head (header, section, abbrev_section);
3897
3898 return info_ptr;
3899 }
3900
3901 /* Fetch the abbreviation table offset from a comp or type unit header. */
3902
3903 static sect_offset
3904 read_abbrev_offset (struct dwarf2_section_info *section,
3905 sect_offset offset)
3906 {
3907 bfd *abfd = section->asection->owner;
3908 gdb_byte *info_ptr;
3909 unsigned int length, initial_length_size, offset_size;
3910 sect_offset abbrev_offset;
3911
3912 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3913 info_ptr = section->buffer + offset.sect_off;
3914 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3915 offset_size = initial_length_size == 4 ? 4 : 8;
3916 info_ptr += initial_length_size + 2 /*version*/;
3917 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3918 return abbrev_offset;
3919 }
3920
3921 /* Allocate a new partial symtab for file named NAME and mark this new
3922 partial symtab as being an include of PST. */
3923
3924 static void
3925 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3926 struct objfile *objfile)
3927 {
3928 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3929
3930 subpst->section_offsets = pst->section_offsets;
3931 subpst->textlow = 0;
3932 subpst->texthigh = 0;
3933
3934 subpst->dependencies = (struct partial_symtab **)
3935 obstack_alloc (&objfile->objfile_obstack,
3936 sizeof (struct partial_symtab *));
3937 subpst->dependencies[0] = pst;
3938 subpst->number_of_dependencies = 1;
3939
3940 subpst->globals_offset = 0;
3941 subpst->n_global_syms = 0;
3942 subpst->statics_offset = 0;
3943 subpst->n_static_syms = 0;
3944 subpst->symtab = NULL;
3945 subpst->read_symtab = pst->read_symtab;
3946 subpst->readin = 0;
3947
3948 /* No private part is necessary for include psymtabs. This property
3949 can be used to differentiate between such include psymtabs and
3950 the regular ones. */
3951 subpst->read_symtab_private = NULL;
3952 }
3953
3954 /* Read the Line Number Program data and extract the list of files
3955 included by the source file represented by PST. Build an include
3956 partial symtab for each of these included files. */
3957
3958 static void
3959 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3960 struct die_info *die,
3961 struct partial_symtab *pst)
3962 {
3963 struct line_header *lh = NULL;
3964 struct attribute *attr;
3965
3966 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3967 if (attr)
3968 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3969 if (lh == NULL)
3970 return; /* No linetable, so no includes. */
3971
3972 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3973 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3974
3975 free_line_header (lh);
3976 }
3977
3978 static hashval_t
3979 hash_signatured_type (const void *item)
3980 {
3981 const struct signatured_type *sig_type = item;
3982
3983 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3984 return sig_type->signature;
3985 }
3986
3987 static int
3988 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3989 {
3990 const struct signatured_type *lhs = item_lhs;
3991 const struct signatured_type *rhs = item_rhs;
3992
3993 return lhs->signature == rhs->signature;
3994 }
3995
3996 /* Allocate a hash table for signatured types. */
3997
3998 static htab_t
3999 allocate_signatured_type_table (struct objfile *objfile)
4000 {
4001 return htab_create_alloc_ex (41,
4002 hash_signatured_type,
4003 eq_signatured_type,
4004 NULL,
4005 &objfile->objfile_obstack,
4006 hashtab_obstack_allocate,
4007 dummy_obstack_deallocate);
4008 }
4009
4010 /* A helper function to add a signatured type CU to a table. */
4011
4012 static int
4013 add_signatured_type_cu_to_table (void **slot, void *datum)
4014 {
4015 struct signatured_type *sigt = *slot;
4016 struct signatured_type ***datap = datum;
4017
4018 **datap = sigt;
4019 ++*datap;
4020
4021 return 1;
4022 }
4023
4024 /* Create the hash table of all entries in the .debug_types section.
4025 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4026 The result is a pointer to the hash table or NULL if there are
4027 no types. */
4028
4029 static htab_t
4030 create_debug_types_hash_table (struct dwo_file *dwo_file,
4031 VEC (dwarf2_section_info_def) *types)
4032 {
4033 struct objfile *objfile = dwarf2_per_objfile->objfile;
4034 htab_t types_htab = NULL;
4035 int ix;
4036 struct dwarf2_section_info *section;
4037 struct dwarf2_section_info *abbrev_section;
4038
4039 if (VEC_empty (dwarf2_section_info_def, types))
4040 return NULL;
4041
4042 abbrev_section = (dwo_file != NULL
4043 ? &dwo_file->sections.abbrev
4044 : &dwarf2_per_objfile->abbrev);
4045
4046 if (dwarf2_read_debug)
4047 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4048 dwo_file ? ".dwo" : "",
4049 bfd_get_filename (abbrev_section->asection->owner));
4050
4051 for (ix = 0;
4052 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4053 ++ix)
4054 {
4055 bfd *abfd;
4056 gdb_byte *info_ptr, *end_ptr;
4057 struct dwarf2_section_info *abbrev_section;
4058
4059 dwarf2_read_section (objfile, section);
4060 info_ptr = section->buffer;
4061
4062 if (info_ptr == NULL)
4063 continue;
4064
4065 /* We can't set abfd until now because the section may be empty or
4066 not present, in which case section->asection will be NULL. */
4067 abfd = section->asection->owner;
4068
4069 if (dwo_file)
4070 abbrev_section = &dwo_file->sections.abbrev;
4071 else
4072 abbrev_section = &dwarf2_per_objfile->abbrev;
4073
4074 if (types_htab == NULL)
4075 {
4076 if (dwo_file)
4077 types_htab = allocate_dwo_unit_table (objfile);
4078 else
4079 types_htab = allocate_signatured_type_table (objfile);
4080 }
4081
4082 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4083 because we don't need to read any dies: the signature is in the
4084 header. */
4085
4086 end_ptr = info_ptr + section->size;
4087 while (info_ptr < end_ptr)
4088 {
4089 sect_offset offset;
4090 cu_offset type_offset_in_tu;
4091 ULONGEST signature;
4092 struct signatured_type *sig_type;
4093 struct dwo_unit *dwo_tu;
4094 void **slot;
4095 gdb_byte *ptr = info_ptr;
4096 struct comp_unit_head header;
4097 unsigned int length;
4098
4099 offset.sect_off = ptr - section->buffer;
4100
4101 /* We need to read the type's signature in order to build the hash
4102 table, but we don't need anything else just yet. */
4103
4104 ptr = read_and_check_type_unit_head (&header, section,
4105 abbrev_section, ptr,
4106 &signature, &type_offset_in_tu);
4107
4108 length = get_cu_length (&header);
4109
4110 /* Skip dummy type units. */
4111 if (ptr >= info_ptr + length
4112 || peek_abbrev_code (abfd, ptr) == 0)
4113 {
4114 info_ptr += length;
4115 continue;
4116 }
4117
4118 if (dwo_file)
4119 {
4120 sig_type = NULL;
4121 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4122 struct dwo_unit);
4123 dwo_tu->dwo_file = dwo_file;
4124 dwo_tu->signature = signature;
4125 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4126 dwo_tu->info_or_types_section = section;
4127 dwo_tu->offset = offset;
4128 dwo_tu->length = length;
4129 }
4130 else
4131 {
4132 /* N.B.: type_offset is not usable if this type uses a DWO file.
4133 The real type_offset is in the DWO file. */
4134 dwo_tu = NULL;
4135 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4136 struct signatured_type);
4137 sig_type->signature = signature;
4138 sig_type->type_offset_in_tu = type_offset_in_tu;
4139 sig_type->per_cu.objfile = objfile;
4140 sig_type->per_cu.is_debug_types = 1;
4141 sig_type->per_cu.info_or_types_section = section;
4142 sig_type->per_cu.offset = offset;
4143 sig_type->per_cu.length = length;
4144 }
4145
4146 slot = htab_find_slot (types_htab,
4147 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4148 INSERT);
4149 gdb_assert (slot != NULL);
4150 if (*slot != NULL)
4151 {
4152 sect_offset dup_offset;
4153
4154 if (dwo_file)
4155 {
4156 const struct dwo_unit *dup_tu = *slot;
4157
4158 dup_offset = dup_tu->offset;
4159 }
4160 else
4161 {
4162 const struct signatured_type *dup_tu = *slot;
4163
4164 dup_offset = dup_tu->per_cu.offset;
4165 }
4166
4167 complaint (&symfile_complaints,
4168 _("debug type entry at offset 0x%x is duplicate to the "
4169 "entry at offset 0x%x, signature 0x%s"),
4170 offset.sect_off, dup_offset.sect_off,
4171 phex (signature, sizeof (signature)));
4172 }
4173 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4174
4175 if (dwarf2_read_debug)
4176 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4177 offset.sect_off,
4178 phex (signature, sizeof (signature)));
4179
4180 info_ptr += length;
4181 }
4182 }
4183
4184 return types_htab;
4185 }
4186
4187 /* Create the hash table of all entries in the .debug_types section,
4188 and initialize all_type_units.
4189 The result is zero if there is an error (e.g. missing .debug_types section),
4190 otherwise non-zero. */
4191
4192 static int
4193 create_all_type_units (struct objfile *objfile)
4194 {
4195 htab_t types_htab;
4196 struct signatured_type **iter;
4197
4198 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4199 if (types_htab == NULL)
4200 {
4201 dwarf2_per_objfile->signatured_types = NULL;
4202 return 0;
4203 }
4204
4205 dwarf2_per_objfile->signatured_types = types_htab;
4206
4207 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4208 dwarf2_per_objfile->all_type_units
4209 = obstack_alloc (&objfile->objfile_obstack,
4210 dwarf2_per_objfile->n_type_units
4211 * sizeof (struct signatured_type *));
4212 iter = &dwarf2_per_objfile->all_type_units[0];
4213 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4214 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4215 == dwarf2_per_objfile->n_type_units);
4216
4217 return 1;
4218 }
4219
4220 /* Lookup a signature based type for DW_FORM_ref_sig8.
4221 Returns NULL if signature SIG is not present in the table. */
4222
4223 static struct signatured_type *
4224 lookup_signatured_type (ULONGEST sig)
4225 {
4226 struct signatured_type find_entry, *entry;
4227
4228 if (dwarf2_per_objfile->signatured_types == NULL)
4229 {
4230 complaint (&symfile_complaints,
4231 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4232 return NULL;
4233 }
4234
4235 find_entry.signature = sig;
4236 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4237 return entry;
4238 }
4239 \f
4240 /* Low level DIE reading support. */
4241
4242 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4243
4244 static void
4245 init_cu_die_reader (struct die_reader_specs *reader,
4246 struct dwarf2_cu *cu,
4247 struct dwarf2_section_info *section,
4248 struct dwo_file *dwo_file)
4249 {
4250 gdb_assert (section->readin && section->buffer != NULL);
4251 reader->abfd = section->asection->owner;
4252 reader->cu = cu;
4253 reader->dwo_file = dwo_file;
4254 reader->die_section = section;
4255 reader->buffer = section->buffer;
4256 reader->buffer_end = section->buffer + section->size;
4257 }
4258
4259 /* Initialize a CU (or TU) and read its DIEs.
4260 If the CU defers to a DWO file, read the DWO file as well.
4261
4262 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4263 Otherwise the table specified in the comp unit header is read in and used.
4264 This is an optimization for when we already have the abbrev table.
4265
4266 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4267 Otherwise, a new CU is allocated with xmalloc.
4268
4269 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4270 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4271
4272 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4273 linker) then DIE_READER_FUNC will not get called. */
4274
4275 static void
4276 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4277 struct abbrev_table *abbrev_table,
4278 int use_existing_cu, int keep,
4279 die_reader_func_ftype *die_reader_func,
4280 void *data)
4281 {
4282 struct objfile *objfile = dwarf2_per_objfile->objfile;
4283 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4284 bfd *abfd = section->asection->owner;
4285 struct dwarf2_cu *cu;
4286 gdb_byte *begin_info_ptr, *info_ptr;
4287 struct die_reader_specs reader;
4288 struct die_info *comp_unit_die;
4289 int has_children;
4290 struct attribute *attr;
4291 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4292 struct signatured_type *sig_type = NULL;
4293 struct dwarf2_section_info *abbrev_section;
4294 /* Non-zero if CU currently points to a DWO file and we need to
4295 reread it. When this happens we need to reread the skeleton die
4296 before we can reread the DWO file. */
4297 int rereading_dwo_cu = 0;
4298
4299 if (dwarf2_die_debug)
4300 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4301 this_cu->is_debug_types ? "type" : "comp",
4302 this_cu->offset.sect_off);
4303
4304 if (use_existing_cu)
4305 gdb_assert (keep);
4306
4307 cleanups = make_cleanup (null_cleanup, NULL);
4308
4309 /* This is cheap if the section is already read in. */
4310 dwarf2_read_section (objfile, section);
4311
4312 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4313
4314 abbrev_section = get_abbrev_section_for_cu (this_cu);
4315
4316 if (use_existing_cu && this_cu->cu != NULL)
4317 {
4318 cu = this_cu->cu;
4319
4320 /* If this CU is from a DWO file we need to start over, we need to
4321 refetch the attributes from the skeleton CU.
4322 This could be optimized by retrieving those attributes from when we
4323 were here the first time: the previous comp_unit_die was stored in
4324 comp_unit_obstack. But there's no data yet that we need this
4325 optimization. */
4326 if (cu->dwo_unit != NULL)
4327 rereading_dwo_cu = 1;
4328 }
4329 else
4330 {
4331 /* If !use_existing_cu, this_cu->cu must be NULL. */
4332 gdb_assert (this_cu->cu == NULL);
4333
4334 cu = xmalloc (sizeof (*cu));
4335 init_one_comp_unit (cu, this_cu);
4336
4337 /* If an error occurs while loading, release our storage. */
4338 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4339 }
4340
4341 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4342 {
4343 /* We already have the header, there's no need to read it in again. */
4344 info_ptr += cu->header.first_die_offset.cu_off;
4345 }
4346 else
4347 {
4348 if (this_cu->is_debug_types)
4349 {
4350 ULONGEST signature;
4351 cu_offset type_offset_in_tu;
4352
4353 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4354 abbrev_section, info_ptr,
4355 &signature,
4356 &type_offset_in_tu);
4357
4358 /* Since per_cu is the first member of struct signatured_type,
4359 we can go from a pointer to one to a pointer to the other. */
4360 sig_type = (struct signatured_type *) this_cu;
4361 gdb_assert (sig_type->signature == signature);
4362 gdb_assert (sig_type->type_offset_in_tu.cu_off
4363 == type_offset_in_tu.cu_off);
4364 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4365
4366 /* LENGTH has not been set yet for type units if we're
4367 using .gdb_index. */
4368 this_cu->length = get_cu_length (&cu->header);
4369
4370 /* Establish the type offset that can be used to lookup the type. */
4371 sig_type->type_offset_in_section.sect_off =
4372 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4373 }
4374 else
4375 {
4376 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4377 abbrev_section,
4378 info_ptr, 0);
4379
4380 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4381 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4382 }
4383 }
4384
4385 /* Skip dummy compilation units. */
4386 if (info_ptr >= begin_info_ptr + this_cu->length
4387 || peek_abbrev_code (abfd, info_ptr) == 0)
4388 {
4389 do_cleanups (cleanups);
4390 return;
4391 }
4392
4393 /* If we don't have them yet, read the abbrevs for this compilation unit.
4394 And if we need to read them now, make sure they're freed when we're
4395 done. Note that it's important that if the CU had an abbrev table
4396 on entry we don't free it when we're done: Somewhere up the call stack
4397 it may be in use. */
4398 if (abbrev_table != NULL)
4399 {
4400 gdb_assert (cu->abbrev_table == NULL);
4401 gdb_assert (cu->header.abbrev_offset.sect_off
4402 == abbrev_table->offset.sect_off);
4403 cu->abbrev_table = abbrev_table;
4404 }
4405 else if (cu->abbrev_table == NULL)
4406 {
4407 dwarf2_read_abbrevs (cu, abbrev_section);
4408 make_cleanup (dwarf2_free_abbrev_table, cu);
4409 }
4410 else if (rereading_dwo_cu)
4411 {
4412 dwarf2_free_abbrev_table (cu);
4413 dwarf2_read_abbrevs (cu, abbrev_section);
4414 }
4415
4416 /* Read the top level CU/TU die. */
4417 init_cu_die_reader (&reader, cu, section, NULL);
4418 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4419
4420 /* If we have a DWO stub, process it and then read in the DWO file.
4421 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4422 a DWO CU, that this test will fail. */
4423 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4424 if (attr)
4425 {
4426 char *dwo_name = DW_STRING (attr);
4427 const char *comp_dir_string;
4428 struct dwo_unit *dwo_unit;
4429 ULONGEST signature; /* Or dwo_id. */
4430 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4431 int i,num_extra_attrs;
4432 struct dwarf2_section_info *dwo_abbrev_section;
4433
4434 if (has_children)
4435 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4436 " has children (offset 0x%x) [in module %s]"),
4437 this_cu->offset.sect_off, bfd_get_filename (abfd));
4438
4439 /* These attributes aren't processed until later:
4440 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4441 However, the attribute is found in the stub which we won't have later.
4442 In order to not impose this complication on the rest of the code,
4443 we read them here and copy them to the DWO CU/TU die. */
4444
4445 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4446 DWO file. */
4447 stmt_list = NULL;
4448 if (! this_cu->is_debug_types)
4449 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4450 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4451 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4452 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4453 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4454
4455 /* There should be a DW_AT_addr_base attribute here (if needed).
4456 We need the value before we can process DW_FORM_GNU_addr_index. */
4457 cu->addr_base = 0;
4458 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4459 if (attr)
4460 cu->addr_base = DW_UNSND (attr);
4461
4462 /* There should be a DW_AT_ranges_base attribute here (if needed).
4463 We need the value before we can process DW_AT_ranges. */
4464 cu->ranges_base = 0;
4465 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4466 if (attr)
4467 cu->ranges_base = DW_UNSND (attr);
4468
4469 if (this_cu->is_debug_types)
4470 {
4471 gdb_assert (sig_type != NULL);
4472 signature = sig_type->signature;
4473 }
4474 else
4475 {
4476 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4477 if (! attr)
4478 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4479 dwo_name);
4480 signature = DW_UNSND (attr);
4481 }
4482
4483 /* We may need the comp_dir in order to find the DWO file. */
4484 comp_dir_string = NULL;
4485 if (comp_dir)
4486 comp_dir_string = DW_STRING (comp_dir);
4487
4488 if (this_cu->is_debug_types)
4489 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4490 else
4491 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4492 signature);
4493
4494 if (dwo_unit == NULL)
4495 {
4496 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4497 " with ID %s [in module %s]"),
4498 this_cu->offset.sect_off,
4499 phex (signature, sizeof (signature)),
4500 objfile->name);
4501 }
4502
4503 /* Set up for reading the DWO CU/TU. */
4504 cu->dwo_unit = dwo_unit;
4505 section = dwo_unit->info_or_types_section;
4506 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4507 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4508 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4509
4510 if (this_cu->is_debug_types)
4511 {
4512 ULONGEST signature;
4513
4514 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4515 dwo_abbrev_section,
4516 info_ptr,
4517 &signature, NULL);
4518 gdb_assert (sig_type->signature == signature);
4519 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4520 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4521
4522 /* Establish the type offset that can be used to lookup the type.
4523 For DWO files, we don't know it until now. */
4524 sig_type->type_offset_in_section.sect_off =
4525 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4526 }
4527 else
4528 {
4529 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4530 dwo_abbrev_section,
4531 info_ptr, 0);
4532 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4533 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4534 }
4535
4536 /* Discard the original CU's abbrev table, and read the DWO's. */
4537 if (abbrev_table == NULL)
4538 {
4539 dwarf2_free_abbrev_table (cu);
4540 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4541 }
4542 else
4543 {
4544 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4545 make_cleanup (dwarf2_free_abbrev_table, cu);
4546 }
4547
4548 /* Read in the die, but leave space to copy over the attributes
4549 from the stub. This has the benefit of simplifying the rest of
4550 the code - all the real work is done here. */
4551 num_extra_attrs = ((stmt_list != NULL)
4552 + (low_pc != NULL)
4553 + (high_pc != NULL)
4554 + (ranges != NULL)
4555 + (comp_dir != NULL));
4556 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4557 &has_children, num_extra_attrs);
4558
4559 /* Copy over the attributes from the stub to the DWO die. */
4560 i = comp_unit_die->num_attrs;
4561 if (stmt_list != NULL)
4562 comp_unit_die->attrs[i++] = *stmt_list;
4563 if (low_pc != NULL)
4564 comp_unit_die->attrs[i++] = *low_pc;
4565 if (high_pc != NULL)
4566 comp_unit_die->attrs[i++] = *high_pc;
4567 if (ranges != NULL)
4568 comp_unit_die->attrs[i++] = *ranges;
4569 if (comp_dir != NULL)
4570 comp_unit_die->attrs[i++] = *comp_dir;
4571 comp_unit_die->num_attrs += num_extra_attrs;
4572
4573 /* Skip dummy compilation units. */
4574 if (info_ptr >= begin_info_ptr + dwo_unit->length
4575 || peek_abbrev_code (abfd, info_ptr) == 0)
4576 {
4577 do_cleanups (cleanups);
4578 return;
4579 }
4580 }
4581
4582 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4583
4584 if (free_cu_cleanup != NULL)
4585 {
4586 if (keep)
4587 {
4588 /* We've successfully allocated this compilation unit. Let our
4589 caller clean it up when finished with it. */
4590 discard_cleanups (free_cu_cleanup);
4591
4592 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4593 So we have to manually free the abbrev table. */
4594 dwarf2_free_abbrev_table (cu);
4595
4596 /* Link this CU into read_in_chain. */
4597 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4598 dwarf2_per_objfile->read_in_chain = this_cu;
4599 }
4600 else
4601 do_cleanups (free_cu_cleanup);
4602 }
4603
4604 do_cleanups (cleanups);
4605 }
4606
4607 /* Read CU/TU THIS_CU in section SECTION,
4608 but do not follow DW_AT_GNU_dwo_name if present.
4609 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4610 have already done the lookup to find the DWO file).
4611
4612 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4613 THIS_CU->is_debug_types, but nothing else.
4614
4615 We fill in THIS_CU->length.
4616
4617 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4618 linker) then DIE_READER_FUNC will not get called.
4619
4620 THIS_CU->cu is always freed when done.
4621 This is done in order to not leave THIS_CU->cu in a state where we have
4622 to care whether it refers to the "main" CU or the DWO CU. */
4623
4624 static void
4625 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4626 struct dwarf2_section_info *abbrev_section,
4627 struct dwo_file *dwo_file,
4628 die_reader_func_ftype *die_reader_func,
4629 void *data)
4630 {
4631 struct objfile *objfile = dwarf2_per_objfile->objfile;
4632 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4633 bfd *abfd = section->asection->owner;
4634 struct dwarf2_cu cu;
4635 gdb_byte *begin_info_ptr, *info_ptr;
4636 struct die_reader_specs reader;
4637 struct cleanup *cleanups;
4638 struct die_info *comp_unit_die;
4639 int has_children;
4640
4641 if (dwarf2_die_debug)
4642 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4643 this_cu->is_debug_types ? "type" : "comp",
4644 this_cu->offset.sect_off);
4645
4646 gdb_assert (this_cu->cu == NULL);
4647
4648 /* This is cheap if the section is already read in. */
4649 dwarf2_read_section (objfile, section);
4650
4651 init_one_comp_unit (&cu, this_cu);
4652
4653 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4654
4655 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4656 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4657 abbrev_section, info_ptr,
4658 this_cu->is_debug_types);
4659
4660 this_cu->length = get_cu_length (&cu.header);
4661
4662 /* Skip dummy compilation units. */
4663 if (info_ptr >= begin_info_ptr + this_cu->length
4664 || peek_abbrev_code (abfd, info_ptr) == 0)
4665 {
4666 do_cleanups (cleanups);
4667 return;
4668 }
4669
4670 dwarf2_read_abbrevs (&cu, abbrev_section);
4671 make_cleanup (dwarf2_free_abbrev_table, &cu);
4672
4673 init_cu_die_reader (&reader, &cu, section, dwo_file);
4674 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4675
4676 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4677
4678 do_cleanups (cleanups);
4679 }
4680
4681 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4682 does not lookup the specified DWO file.
4683 This cannot be used to read DWO files.
4684
4685 THIS_CU->cu is always freed when done.
4686 This is done in order to not leave THIS_CU->cu in a state where we have
4687 to care whether it refers to the "main" CU or the DWO CU.
4688 We can revisit this if the data shows there's a performance issue. */
4689
4690 static void
4691 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4692 die_reader_func_ftype *die_reader_func,
4693 void *data)
4694 {
4695 init_cutu_and_read_dies_no_follow (this_cu,
4696 get_abbrev_section_for_cu (this_cu),
4697 NULL,
4698 die_reader_func, data);
4699 }
4700
4701 /* Create a psymtab named NAME and assign it to PER_CU.
4702
4703 The caller must fill in the following details:
4704 dirname, textlow, texthigh. */
4705
4706 static struct partial_symtab *
4707 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4708 {
4709 struct objfile *objfile = per_cu->objfile;
4710 struct partial_symtab *pst;
4711
4712 pst = start_psymtab_common (objfile, objfile->section_offsets,
4713 name, 0,
4714 objfile->global_psymbols.next,
4715 objfile->static_psymbols.next);
4716
4717 pst->psymtabs_addrmap_supported = 1;
4718
4719 /* This is the glue that links PST into GDB's symbol API. */
4720 pst->read_symtab_private = per_cu;
4721 pst->read_symtab = dwarf2_psymtab_to_symtab;
4722 per_cu->v.psymtab = pst;
4723
4724 return pst;
4725 }
4726
4727 /* die_reader_func for process_psymtab_comp_unit. */
4728
4729 static void
4730 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4731 gdb_byte *info_ptr,
4732 struct die_info *comp_unit_die,
4733 int has_children,
4734 void *data)
4735 {
4736 struct dwarf2_cu *cu = reader->cu;
4737 struct objfile *objfile = cu->objfile;
4738 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4739 struct attribute *attr;
4740 CORE_ADDR baseaddr;
4741 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4742 struct partial_symtab *pst;
4743 int has_pc_info;
4744 const char *filename;
4745 int *want_partial_unit_ptr = data;
4746
4747 if (comp_unit_die->tag == DW_TAG_partial_unit
4748 && (want_partial_unit_ptr == NULL
4749 || !*want_partial_unit_ptr))
4750 return;
4751
4752 gdb_assert (! per_cu->is_debug_types);
4753
4754 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4755
4756 cu->list_in_scope = &file_symbols;
4757
4758 /* Allocate a new partial symbol table structure. */
4759 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4760 if (attr == NULL || !DW_STRING (attr))
4761 filename = "";
4762 else
4763 filename = DW_STRING (attr);
4764
4765 pst = create_partial_symtab (per_cu, filename);
4766
4767 /* This must be done before calling dwarf2_build_include_psymtabs. */
4768 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4769 if (attr != NULL)
4770 pst->dirname = DW_STRING (attr);
4771
4772 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4773
4774 dwarf2_find_base_address (comp_unit_die, cu);
4775
4776 /* Possibly set the default values of LOWPC and HIGHPC from
4777 `DW_AT_ranges'. */
4778 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4779 &best_highpc, cu, pst);
4780 if (has_pc_info == 1 && best_lowpc < best_highpc)
4781 /* Store the contiguous range if it is not empty; it can be empty for
4782 CUs with no code. */
4783 addrmap_set_empty (objfile->psymtabs_addrmap,
4784 best_lowpc + baseaddr,
4785 best_highpc + baseaddr - 1, pst);
4786
4787 /* Check if comp unit has_children.
4788 If so, read the rest of the partial symbols from this comp unit.
4789 If not, there's no more debug_info for this comp unit. */
4790 if (has_children)
4791 {
4792 struct partial_die_info *first_die;
4793 CORE_ADDR lowpc, highpc;
4794
4795 lowpc = ((CORE_ADDR) -1);
4796 highpc = ((CORE_ADDR) 0);
4797
4798 first_die = load_partial_dies (reader, info_ptr, 1);
4799
4800 scan_partial_symbols (first_die, &lowpc, &highpc,
4801 ! has_pc_info, cu);
4802
4803 /* If we didn't find a lowpc, set it to highpc to avoid
4804 complaints from `maint check'. */
4805 if (lowpc == ((CORE_ADDR) -1))
4806 lowpc = highpc;
4807
4808 /* If the compilation unit didn't have an explicit address range,
4809 then use the information extracted from its child dies. */
4810 if (! has_pc_info)
4811 {
4812 best_lowpc = lowpc;
4813 best_highpc = highpc;
4814 }
4815 }
4816 pst->textlow = best_lowpc + baseaddr;
4817 pst->texthigh = best_highpc + baseaddr;
4818
4819 pst->n_global_syms = objfile->global_psymbols.next -
4820 (objfile->global_psymbols.list + pst->globals_offset);
4821 pst->n_static_syms = objfile->static_psymbols.next -
4822 (objfile->static_psymbols.list + pst->statics_offset);
4823 sort_pst_symbols (pst);
4824
4825 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4826 {
4827 int i;
4828 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4829 struct dwarf2_per_cu_data *iter;
4830
4831 /* Fill in 'dependencies' here; we fill in 'users' in a
4832 post-pass. */
4833 pst->number_of_dependencies = len;
4834 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4835 len * sizeof (struct symtab *));
4836 for (i = 0;
4837 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4838 i, iter);
4839 ++i)
4840 pst->dependencies[i] = iter->v.psymtab;
4841
4842 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4843 }
4844
4845 /* Get the list of files included in the current compilation unit,
4846 and build a psymtab for each of them. */
4847 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4848
4849 if (dwarf2_read_debug)
4850 {
4851 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4852
4853 fprintf_unfiltered (gdb_stdlog,
4854 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4855 ", %d global, %d static syms\n",
4856 per_cu->is_debug_types ? "type" : "comp",
4857 per_cu->offset.sect_off,
4858 paddress (gdbarch, pst->textlow),
4859 paddress (gdbarch, pst->texthigh),
4860 pst->n_global_syms, pst->n_static_syms);
4861 }
4862 }
4863
4864 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4865 Process compilation unit THIS_CU for a psymtab. */
4866
4867 static void
4868 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4869 int want_partial_unit)
4870 {
4871 /* If this compilation unit was already read in, free the
4872 cached copy in order to read it in again. This is
4873 necessary because we skipped some symbols when we first
4874 read in the compilation unit (see load_partial_dies).
4875 This problem could be avoided, but the benefit is unclear. */
4876 if (this_cu->cu != NULL)
4877 free_one_cached_comp_unit (this_cu);
4878
4879 gdb_assert (! this_cu->is_debug_types);
4880 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4881 process_psymtab_comp_unit_reader,
4882 &want_partial_unit);
4883
4884 /* Age out any secondary CUs. */
4885 age_cached_comp_units ();
4886 }
4887
4888 static hashval_t
4889 hash_type_unit_group (const void *item)
4890 {
4891 const struct type_unit_group *tu_group = item;
4892
4893 return hash_stmt_list_entry (&tu_group->hash);
4894 }
4895
4896 static int
4897 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4898 {
4899 const struct type_unit_group *lhs = item_lhs;
4900 const struct type_unit_group *rhs = item_rhs;
4901
4902 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4903 }
4904
4905 /* Allocate a hash table for type unit groups. */
4906
4907 static htab_t
4908 allocate_type_unit_groups_table (void)
4909 {
4910 return htab_create_alloc_ex (3,
4911 hash_type_unit_group,
4912 eq_type_unit_group,
4913 NULL,
4914 &dwarf2_per_objfile->objfile->objfile_obstack,
4915 hashtab_obstack_allocate,
4916 dummy_obstack_deallocate);
4917 }
4918
4919 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4920 partial symtabs. We combine several TUs per psymtab to not let the size
4921 of any one psymtab grow too big. */
4922 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4923 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4924
4925 /* Helper routine for get_type_unit_group.
4926 Create the type_unit_group object used to hold one or more TUs. */
4927
4928 static struct type_unit_group *
4929 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4930 {
4931 struct objfile *objfile = dwarf2_per_objfile->objfile;
4932 struct dwarf2_per_cu_data *per_cu;
4933 struct type_unit_group *tu_group;
4934
4935 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4936 struct type_unit_group);
4937 per_cu = &tu_group->per_cu;
4938 per_cu->objfile = objfile;
4939 per_cu->is_debug_types = 1;
4940 per_cu->s.type_unit_group = tu_group;
4941
4942 if (dwarf2_per_objfile->using_index)
4943 {
4944 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4945 struct dwarf2_per_cu_quick_data);
4946 tu_group->t.first_tu = cu->per_cu;
4947 }
4948 else
4949 {
4950 unsigned int line_offset = line_offset_struct.sect_off;
4951 struct partial_symtab *pst;
4952 char *name;
4953
4954 /* Give the symtab a useful name for debug purposes. */
4955 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4956 name = xstrprintf ("<type_units_%d>",
4957 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4958 else
4959 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4960
4961 pst = create_partial_symtab (per_cu, name);
4962 pst->anonymous = 1;
4963
4964 xfree (name);
4965 }
4966
4967 tu_group->hash.dwo_unit = cu->dwo_unit;
4968 tu_group->hash.line_offset = line_offset_struct;
4969
4970 return tu_group;
4971 }
4972
4973 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4974 STMT_LIST is a DW_AT_stmt_list attribute. */
4975
4976 static struct type_unit_group *
4977 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4978 {
4979 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4980 struct type_unit_group *tu_group;
4981 void **slot;
4982 unsigned int line_offset;
4983 struct type_unit_group type_unit_group_for_lookup;
4984
4985 if (dwarf2_per_objfile->type_unit_groups == NULL)
4986 {
4987 dwarf2_per_objfile->type_unit_groups =
4988 allocate_type_unit_groups_table ();
4989 }
4990
4991 /* Do we need to create a new group, or can we use an existing one? */
4992
4993 if (stmt_list)
4994 {
4995 line_offset = DW_UNSND (stmt_list);
4996 ++tu_stats->nr_symtab_sharers;
4997 }
4998 else
4999 {
5000 /* Ugh, no stmt_list. Rare, but we have to handle it.
5001 We can do various things here like create one group per TU or
5002 spread them over multiple groups to split up the expansion work.
5003 To avoid worst case scenarios (too many groups or too large groups)
5004 we, umm, group them in bunches. */
5005 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5006 | (tu_stats->nr_stmt_less_type_units
5007 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5008 ++tu_stats->nr_stmt_less_type_units;
5009 }
5010
5011 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5012 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5013 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5014 &type_unit_group_for_lookup, INSERT);
5015 if (*slot != NULL)
5016 {
5017 tu_group = *slot;
5018 gdb_assert (tu_group != NULL);
5019 }
5020 else
5021 {
5022 sect_offset line_offset_struct;
5023
5024 line_offset_struct.sect_off = line_offset;
5025 tu_group = create_type_unit_group (cu, line_offset_struct);
5026 *slot = tu_group;
5027 ++tu_stats->nr_symtabs;
5028 }
5029
5030 return tu_group;
5031 }
5032
5033 /* Struct used to sort TUs by their abbreviation table offset. */
5034
5035 struct tu_abbrev_offset
5036 {
5037 struct signatured_type *sig_type;
5038 sect_offset abbrev_offset;
5039 };
5040
5041 /* Helper routine for build_type_unit_groups, passed to qsort. */
5042
5043 static int
5044 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5045 {
5046 const struct tu_abbrev_offset * const *a = ap;
5047 const struct tu_abbrev_offset * const *b = bp;
5048 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5049 unsigned int boff = (*b)->abbrev_offset.sect_off;
5050
5051 return (aoff > boff) - (aoff < boff);
5052 }
5053
5054 /* A helper function to add a type_unit_group to a table. */
5055
5056 static int
5057 add_type_unit_group_to_table (void **slot, void *datum)
5058 {
5059 struct type_unit_group *tu_group = *slot;
5060 struct type_unit_group ***datap = datum;
5061
5062 **datap = tu_group;
5063 ++*datap;
5064
5065 return 1;
5066 }
5067
5068 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5069 each one passing FUNC,DATA.
5070
5071 The efficiency is because we sort TUs by the abbrev table they use and
5072 only read each abbrev table once. In one program there are 200K TUs
5073 sharing 8K abbrev tables.
5074
5075 The main purpose of this function is to support building the
5076 dwarf2_per_objfile->type_unit_groups table.
5077 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5078 can collapse the search space by grouping them by stmt_list.
5079 The savings can be significant, in the same program from above the 200K TUs
5080 share 8K stmt_list tables.
5081
5082 FUNC is expected to call get_type_unit_group, which will create the
5083 struct type_unit_group if necessary and add it to
5084 dwarf2_per_objfile->type_unit_groups. */
5085
5086 static void
5087 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5088 {
5089 struct objfile *objfile = dwarf2_per_objfile->objfile;
5090 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5091 struct cleanup *cleanups;
5092 struct abbrev_table *abbrev_table;
5093 sect_offset abbrev_offset;
5094 struct tu_abbrev_offset *sorted_by_abbrev;
5095 struct type_unit_group **iter;
5096 int i;
5097
5098 /* It's up to the caller to not call us multiple times. */
5099 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5100
5101 if (dwarf2_per_objfile->n_type_units == 0)
5102 return;
5103
5104 /* TUs typically share abbrev tables, and there can be way more TUs than
5105 abbrev tables. Sort by abbrev table to reduce the number of times we
5106 read each abbrev table in.
5107 Alternatives are to punt or to maintain a cache of abbrev tables.
5108 This is simpler and efficient enough for now.
5109
5110 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5111 symtab to use). Typically TUs with the same abbrev offset have the same
5112 stmt_list value too so in practice this should work well.
5113
5114 The basic algorithm here is:
5115
5116 sort TUs by abbrev table
5117 for each TU with same abbrev table:
5118 read abbrev table if first user
5119 read TU top level DIE
5120 [IWBN if DWO skeletons had DW_AT_stmt_list]
5121 call FUNC */
5122
5123 if (dwarf2_read_debug)
5124 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5125
5126 /* Sort in a separate table to maintain the order of all_type_units
5127 for .gdb_index: TU indices directly index all_type_units. */
5128 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5129 dwarf2_per_objfile->n_type_units);
5130 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5131 {
5132 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5133
5134 sorted_by_abbrev[i].sig_type = sig_type;
5135 sorted_by_abbrev[i].abbrev_offset =
5136 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5137 sig_type->per_cu.offset);
5138 }
5139 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5140 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5141 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5142
5143 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5144 called any number of times, so we don't reset tu_stats here. */
5145
5146 abbrev_offset.sect_off = ~(unsigned) 0;
5147 abbrev_table = NULL;
5148 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5149
5150 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5151 {
5152 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5153
5154 /* Switch to the next abbrev table if necessary. */
5155 if (abbrev_table == NULL
5156 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5157 {
5158 if (abbrev_table != NULL)
5159 {
5160 abbrev_table_free (abbrev_table);
5161 /* Reset to NULL in case abbrev_table_read_table throws
5162 an error: abbrev_table_free_cleanup will get called. */
5163 abbrev_table = NULL;
5164 }
5165 abbrev_offset = tu->abbrev_offset;
5166 abbrev_table =
5167 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5168 abbrev_offset);
5169 ++tu_stats->nr_uniq_abbrev_tables;
5170 }
5171
5172 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5173 func, data);
5174 }
5175
5176 /* Create a vector of pointers to primary type units to make it easy to
5177 iterate over them and CUs. See dw2_get_primary_cu. */
5178 dwarf2_per_objfile->n_type_unit_groups =
5179 htab_elements (dwarf2_per_objfile->type_unit_groups);
5180 dwarf2_per_objfile->all_type_unit_groups =
5181 obstack_alloc (&objfile->objfile_obstack,
5182 dwarf2_per_objfile->n_type_unit_groups
5183 * sizeof (struct type_unit_group *));
5184 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5185 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5186 add_type_unit_group_to_table, &iter);
5187 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5188 == dwarf2_per_objfile->n_type_unit_groups);
5189
5190 do_cleanups (cleanups);
5191
5192 if (dwarf2_read_debug)
5193 {
5194 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5195 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5196 dwarf2_per_objfile->n_type_units);
5197 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5198 tu_stats->nr_uniq_abbrev_tables);
5199 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5200 tu_stats->nr_symtabs);
5201 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5202 tu_stats->nr_symtab_sharers);
5203 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5204 tu_stats->nr_stmt_less_type_units);
5205 }
5206 }
5207
5208 /* Reader function for build_type_psymtabs. */
5209
5210 static void
5211 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5212 gdb_byte *info_ptr,
5213 struct die_info *type_unit_die,
5214 int has_children,
5215 void *data)
5216 {
5217 struct objfile *objfile = dwarf2_per_objfile->objfile;
5218 struct dwarf2_cu *cu = reader->cu;
5219 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5220 struct type_unit_group *tu_group;
5221 struct attribute *attr;
5222 struct partial_die_info *first_die;
5223 CORE_ADDR lowpc, highpc;
5224 struct partial_symtab *pst;
5225
5226 gdb_assert (data == NULL);
5227
5228 if (! has_children)
5229 return;
5230
5231 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5232 tu_group = get_type_unit_group (cu, attr);
5233
5234 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5235
5236 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5237 cu->list_in_scope = &file_symbols;
5238 pst = create_partial_symtab (per_cu, "");
5239 pst->anonymous = 1;
5240
5241 first_die = load_partial_dies (reader, info_ptr, 1);
5242
5243 lowpc = (CORE_ADDR) -1;
5244 highpc = (CORE_ADDR) 0;
5245 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5246
5247 pst->n_global_syms = objfile->global_psymbols.next -
5248 (objfile->global_psymbols.list + pst->globals_offset);
5249 pst->n_static_syms = objfile->static_psymbols.next -
5250 (objfile->static_psymbols.list + pst->statics_offset);
5251 sort_pst_symbols (pst);
5252 }
5253
5254 /* Traversal function for build_type_psymtabs. */
5255
5256 static int
5257 build_type_psymtab_dependencies (void **slot, void *info)
5258 {
5259 struct objfile *objfile = dwarf2_per_objfile->objfile;
5260 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5261 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5262 struct partial_symtab *pst = per_cu->v.psymtab;
5263 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5264 struct dwarf2_per_cu_data *iter;
5265 int i;
5266
5267 gdb_assert (len > 0);
5268
5269 pst->number_of_dependencies = len;
5270 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5271 len * sizeof (struct psymtab *));
5272 for (i = 0;
5273 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5274 ++i)
5275 {
5276 pst->dependencies[i] = iter->v.psymtab;
5277 iter->s.type_unit_group = tu_group;
5278 }
5279
5280 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5281
5282 return 1;
5283 }
5284
5285 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5286 Build partial symbol tables for the .debug_types comp-units. */
5287
5288 static void
5289 build_type_psymtabs (struct objfile *objfile)
5290 {
5291 if (! create_all_type_units (objfile))
5292 return;
5293
5294 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5295
5296 /* Now that all TUs have been processed we can fill in the dependencies. */
5297 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5298 build_type_psymtab_dependencies, NULL);
5299 }
5300
5301 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5302
5303 static void
5304 psymtabs_addrmap_cleanup (void *o)
5305 {
5306 struct objfile *objfile = o;
5307
5308 objfile->psymtabs_addrmap = NULL;
5309 }
5310
5311 /* Compute the 'user' field for each psymtab in OBJFILE. */
5312
5313 static void
5314 set_partial_user (struct objfile *objfile)
5315 {
5316 int i;
5317
5318 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5319 {
5320 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5321 struct partial_symtab *pst = per_cu->v.psymtab;
5322 int j;
5323
5324 if (pst == NULL)
5325 continue;
5326
5327 for (j = 0; j < pst->number_of_dependencies; ++j)
5328 {
5329 /* Set the 'user' field only if it is not already set. */
5330 if (pst->dependencies[j]->user == NULL)
5331 pst->dependencies[j]->user = pst;
5332 }
5333 }
5334 }
5335
5336 /* Build the partial symbol table by doing a quick pass through the
5337 .debug_info and .debug_abbrev sections. */
5338
5339 static void
5340 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5341 {
5342 struct cleanup *back_to, *addrmap_cleanup;
5343 struct obstack temp_obstack;
5344 int i;
5345
5346 if (dwarf2_read_debug)
5347 {
5348 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5349 objfile->name);
5350 }
5351
5352 dwarf2_per_objfile->reading_partial_symbols = 1;
5353
5354 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5355
5356 /* Any cached compilation units will be linked by the per-objfile
5357 read_in_chain. Make sure to free them when we're done. */
5358 back_to = make_cleanup (free_cached_comp_units, NULL);
5359
5360 build_type_psymtabs (objfile);
5361
5362 create_all_comp_units (objfile);
5363
5364 /* Create a temporary address map on a temporary obstack. We later
5365 copy this to the final obstack. */
5366 obstack_init (&temp_obstack);
5367 make_cleanup_obstack_free (&temp_obstack);
5368 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5369 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5370
5371 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5372 {
5373 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5374
5375 process_psymtab_comp_unit (per_cu, 0);
5376 }
5377
5378 set_partial_user (objfile);
5379
5380 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5381 &objfile->objfile_obstack);
5382 discard_cleanups (addrmap_cleanup);
5383
5384 do_cleanups (back_to);
5385
5386 if (dwarf2_read_debug)
5387 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5388 objfile->name);
5389 }
5390
5391 /* die_reader_func for load_partial_comp_unit. */
5392
5393 static void
5394 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5395 gdb_byte *info_ptr,
5396 struct die_info *comp_unit_die,
5397 int has_children,
5398 void *data)
5399 {
5400 struct dwarf2_cu *cu = reader->cu;
5401
5402 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5403
5404 /* Check if comp unit has_children.
5405 If so, read the rest of the partial symbols from this comp unit.
5406 If not, there's no more debug_info for this comp unit. */
5407 if (has_children)
5408 load_partial_dies (reader, info_ptr, 0);
5409 }
5410
5411 /* Load the partial DIEs for a secondary CU into memory.
5412 This is also used when rereading a primary CU with load_all_dies. */
5413
5414 static void
5415 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5416 {
5417 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5418 load_partial_comp_unit_reader, NULL);
5419 }
5420
5421 static void
5422 read_comp_units_from_section (struct objfile *objfile,
5423 struct dwarf2_section_info *section,
5424 unsigned int is_dwz,
5425 int *n_allocated,
5426 int *n_comp_units,
5427 struct dwarf2_per_cu_data ***all_comp_units)
5428 {
5429 gdb_byte *info_ptr;
5430 bfd *abfd = section->asection->owner;
5431
5432 dwarf2_read_section (objfile, section);
5433
5434 info_ptr = section->buffer;
5435
5436 while (info_ptr < section->buffer + section->size)
5437 {
5438 unsigned int length, initial_length_size;
5439 struct dwarf2_per_cu_data *this_cu;
5440 sect_offset offset;
5441
5442 offset.sect_off = info_ptr - section->buffer;
5443
5444 /* Read just enough information to find out where the next
5445 compilation unit is. */
5446 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5447
5448 /* Save the compilation unit for later lookup. */
5449 this_cu = obstack_alloc (&objfile->objfile_obstack,
5450 sizeof (struct dwarf2_per_cu_data));
5451 memset (this_cu, 0, sizeof (*this_cu));
5452 this_cu->offset = offset;
5453 this_cu->length = length + initial_length_size;
5454 this_cu->is_dwz = is_dwz;
5455 this_cu->objfile = objfile;
5456 this_cu->info_or_types_section = section;
5457
5458 if (*n_comp_units == *n_allocated)
5459 {
5460 *n_allocated *= 2;
5461 *all_comp_units = xrealloc (*all_comp_units,
5462 *n_allocated
5463 * sizeof (struct dwarf2_per_cu_data *));
5464 }
5465 (*all_comp_units)[*n_comp_units] = this_cu;
5466 ++*n_comp_units;
5467
5468 info_ptr = info_ptr + this_cu->length;
5469 }
5470 }
5471
5472 /* Create a list of all compilation units in OBJFILE.
5473 This is only done for -readnow and building partial symtabs. */
5474
5475 static void
5476 create_all_comp_units (struct objfile *objfile)
5477 {
5478 int n_allocated;
5479 int n_comp_units;
5480 struct dwarf2_per_cu_data **all_comp_units;
5481
5482 n_comp_units = 0;
5483 n_allocated = 10;
5484 all_comp_units = xmalloc (n_allocated
5485 * sizeof (struct dwarf2_per_cu_data *));
5486
5487 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5488 &n_allocated, &n_comp_units, &all_comp_units);
5489
5490 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5491 {
5492 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5493
5494 read_comp_units_from_section (objfile, &dwz->info, 1,
5495 &n_allocated, &n_comp_units,
5496 &all_comp_units);
5497 }
5498
5499 dwarf2_per_objfile->all_comp_units
5500 = obstack_alloc (&objfile->objfile_obstack,
5501 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5502 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5503 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5504 xfree (all_comp_units);
5505 dwarf2_per_objfile->n_comp_units = n_comp_units;
5506 }
5507
5508 /* Process all loaded DIEs for compilation unit CU, starting at
5509 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5510 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5511 DW_AT_ranges). If NEED_PC is set, then this function will set
5512 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5513 and record the covered ranges in the addrmap. */
5514
5515 static void
5516 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5517 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5518 {
5519 struct partial_die_info *pdi;
5520
5521 /* Now, march along the PDI's, descending into ones which have
5522 interesting children but skipping the children of the other ones,
5523 until we reach the end of the compilation unit. */
5524
5525 pdi = first_die;
5526
5527 while (pdi != NULL)
5528 {
5529 fixup_partial_die (pdi, cu);
5530
5531 /* Anonymous namespaces or modules have no name but have interesting
5532 children, so we need to look at them. Ditto for anonymous
5533 enums. */
5534
5535 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5536 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5537 || pdi->tag == DW_TAG_imported_unit)
5538 {
5539 switch (pdi->tag)
5540 {
5541 case DW_TAG_subprogram:
5542 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5543 break;
5544 case DW_TAG_constant:
5545 case DW_TAG_variable:
5546 case DW_TAG_typedef:
5547 case DW_TAG_union_type:
5548 if (!pdi->is_declaration)
5549 {
5550 add_partial_symbol (pdi, cu);
5551 }
5552 break;
5553 case DW_TAG_class_type:
5554 case DW_TAG_interface_type:
5555 case DW_TAG_structure_type:
5556 if (!pdi->is_declaration)
5557 {
5558 add_partial_symbol (pdi, cu);
5559 }
5560 break;
5561 case DW_TAG_enumeration_type:
5562 if (!pdi->is_declaration)
5563 add_partial_enumeration (pdi, cu);
5564 break;
5565 case DW_TAG_base_type:
5566 case DW_TAG_subrange_type:
5567 /* File scope base type definitions are added to the partial
5568 symbol table. */
5569 add_partial_symbol (pdi, cu);
5570 break;
5571 case DW_TAG_namespace:
5572 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5573 break;
5574 case DW_TAG_module:
5575 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5576 break;
5577 case DW_TAG_imported_unit:
5578 {
5579 struct dwarf2_per_cu_data *per_cu;
5580
5581 /* For now we don't handle imported units in type units. */
5582 if (cu->per_cu->is_debug_types)
5583 {
5584 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5585 " supported in type units [in module %s]"),
5586 cu->objfile->name);
5587 }
5588
5589 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5590 pdi->is_dwz,
5591 cu->objfile);
5592
5593 /* Go read the partial unit, if needed. */
5594 if (per_cu->v.psymtab == NULL)
5595 process_psymtab_comp_unit (per_cu, 1);
5596
5597 VEC_safe_push (dwarf2_per_cu_ptr,
5598 cu->per_cu->s.imported_symtabs, per_cu);
5599 }
5600 break;
5601 default:
5602 break;
5603 }
5604 }
5605
5606 /* If the die has a sibling, skip to the sibling. */
5607
5608 pdi = pdi->die_sibling;
5609 }
5610 }
5611
5612 /* Functions used to compute the fully scoped name of a partial DIE.
5613
5614 Normally, this is simple. For C++, the parent DIE's fully scoped
5615 name is concatenated with "::" and the partial DIE's name. For
5616 Java, the same thing occurs except that "." is used instead of "::".
5617 Enumerators are an exception; they use the scope of their parent
5618 enumeration type, i.e. the name of the enumeration type is not
5619 prepended to the enumerator.
5620
5621 There are two complexities. One is DW_AT_specification; in this
5622 case "parent" means the parent of the target of the specification,
5623 instead of the direct parent of the DIE. The other is compilers
5624 which do not emit DW_TAG_namespace; in this case we try to guess
5625 the fully qualified name of structure types from their members'
5626 linkage names. This must be done using the DIE's children rather
5627 than the children of any DW_AT_specification target. We only need
5628 to do this for structures at the top level, i.e. if the target of
5629 any DW_AT_specification (if any; otherwise the DIE itself) does not
5630 have a parent. */
5631
5632 /* Compute the scope prefix associated with PDI's parent, in
5633 compilation unit CU. The result will be allocated on CU's
5634 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5635 field. NULL is returned if no prefix is necessary. */
5636 static char *
5637 partial_die_parent_scope (struct partial_die_info *pdi,
5638 struct dwarf2_cu *cu)
5639 {
5640 char *grandparent_scope;
5641 struct partial_die_info *parent, *real_pdi;
5642
5643 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5644 then this means the parent of the specification DIE. */
5645
5646 real_pdi = pdi;
5647 while (real_pdi->has_specification)
5648 real_pdi = find_partial_die (real_pdi->spec_offset,
5649 real_pdi->spec_is_dwz, cu);
5650
5651 parent = real_pdi->die_parent;
5652 if (parent == NULL)
5653 return NULL;
5654
5655 if (parent->scope_set)
5656 return parent->scope;
5657
5658 fixup_partial_die (parent, cu);
5659
5660 grandparent_scope = partial_die_parent_scope (parent, cu);
5661
5662 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5663 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5664 Work around this problem here. */
5665 if (cu->language == language_cplus
5666 && parent->tag == DW_TAG_namespace
5667 && strcmp (parent->name, "::") == 0
5668 && grandparent_scope == NULL)
5669 {
5670 parent->scope = NULL;
5671 parent->scope_set = 1;
5672 return NULL;
5673 }
5674
5675 if (pdi->tag == DW_TAG_enumerator)
5676 /* Enumerators should not get the name of the enumeration as a prefix. */
5677 parent->scope = grandparent_scope;
5678 else if (parent->tag == DW_TAG_namespace
5679 || parent->tag == DW_TAG_module
5680 || parent->tag == DW_TAG_structure_type
5681 || parent->tag == DW_TAG_class_type
5682 || parent->tag == DW_TAG_interface_type
5683 || parent->tag == DW_TAG_union_type
5684 || parent->tag == DW_TAG_enumeration_type)
5685 {
5686 if (grandparent_scope == NULL)
5687 parent->scope = parent->name;
5688 else
5689 parent->scope = typename_concat (&cu->comp_unit_obstack,
5690 grandparent_scope,
5691 parent->name, 0, cu);
5692 }
5693 else
5694 {
5695 /* FIXME drow/2004-04-01: What should we be doing with
5696 function-local names? For partial symbols, we should probably be
5697 ignoring them. */
5698 complaint (&symfile_complaints,
5699 _("unhandled containing DIE tag %d for DIE at %d"),
5700 parent->tag, pdi->offset.sect_off);
5701 parent->scope = grandparent_scope;
5702 }
5703
5704 parent->scope_set = 1;
5705 return parent->scope;
5706 }
5707
5708 /* Return the fully scoped name associated with PDI, from compilation unit
5709 CU. The result will be allocated with malloc. */
5710
5711 static char *
5712 partial_die_full_name (struct partial_die_info *pdi,
5713 struct dwarf2_cu *cu)
5714 {
5715 char *parent_scope;
5716
5717 /* If this is a template instantiation, we can not work out the
5718 template arguments from partial DIEs. So, unfortunately, we have
5719 to go through the full DIEs. At least any work we do building
5720 types here will be reused if full symbols are loaded later. */
5721 if (pdi->has_template_arguments)
5722 {
5723 fixup_partial_die (pdi, cu);
5724
5725 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5726 {
5727 struct die_info *die;
5728 struct attribute attr;
5729 struct dwarf2_cu *ref_cu = cu;
5730
5731 /* DW_FORM_ref_addr is using section offset. */
5732 attr.name = 0;
5733 attr.form = DW_FORM_ref_addr;
5734 attr.u.unsnd = pdi->offset.sect_off;
5735 die = follow_die_ref (NULL, &attr, &ref_cu);
5736
5737 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5738 }
5739 }
5740
5741 parent_scope = partial_die_parent_scope (pdi, cu);
5742 if (parent_scope == NULL)
5743 return NULL;
5744 else
5745 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5746 }
5747
5748 static void
5749 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5750 {
5751 struct objfile *objfile = cu->objfile;
5752 CORE_ADDR addr = 0;
5753 char *actual_name = NULL;
5754 CORE_ADDR baseaddr;
5755 int built_actual_name = 0;
5756
5757 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5758
5759 actual_name = partial_die_full_name (pdi, cu);
5760 if (actual_name)
5761 built_actual_name = 1;
5762
5763 if (actual_name == NULL)
5764 actual_name = pdi->name;
5765
5766 switch (pdi->tag)
5767 {
5768 case DW_TAG_subprogram:
5769 if (pdi->is_external || cu->language == language_ada)
5770 {
5771 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5772 of the global scope. But in Ada, we want to be able to access
5773 nested procedures globally. So all Ada subprograms are stored
5774 in the global scope. */
5775 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5776 mst_text, objfile); */
5777 add_psymbol_to_list (actual_name, strlen (actual_name),
5778 built_actual_name,
5779 VAR_DOMAIN, LOC_BLOCK,
5780 &objfile->global_psymbols,
5781 0, pdi->lowpc + baseaddr,
5782 cu->language, objfile);
5783 }
5784 else
5785 {
5786 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5787 mst_file_text, objfile); */
5788 add_psymbol_to_list (actual_name, strlen (actual_name),
5789 built_actual_name,
5790 VAR_DOMAIN, LOC_BLOCK,
5791 &objfile->static_psymbols,
5792 0, pdi->lowpc + baseaddr,
5793 cu->language, objfile);
5794 }
5795 break;
5796 case DW_TAG_constant:
5797 {
5798 struct psymbol_allocation_list *list;
5799
5800 if (pdi->is_external)
5801 list = &objfile->global_psymbols;
5802 else
5803 list = &objfile->static_psymbols;
5804 add_psymbol_to_list (actual_name, strlen (actual_name),
5805 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5806 list, 0, 0, cu->language, objfile);
5807 }
5808 break;
5809 case DW_TAG_variable:
5810 if (pdi->d.locdesc)
5811 addr = decode_locdesc (pdi->d.locdesc, cu);
5812
5813 if (pdi->d.locdesc
5814 && addr == 0
5815 && !dwarf2_per_objfile->has_section_at_zero)
5816 {
5817 /* A global or static variable may also have been stripped
5818 out by the linker if unused, in which case its address
5819 will be nullified; do not add such variables into partial
5820 symbol table then. */
5821 }
5822 else if (pdi->is_external)
5823 {
5824 /* Global Variable.
5825 Don't enter into the minimal symbol tables as there is
5826 a minimal symbol table entry from the ELF symbols already.
5827 Enter into partial symbol table if it has a location
5828 descriptor or a type.
5829 If the location descriptor is missing, new_symbol will create
5830 a LOC_UNRESOLVED symbol, the address of the variable will then
5831 be determined from the minimal symbol table whenever the variable
5832 is referenced.
5833 The address for the partial symbol table entry is not
5834 used by GDB, but it comes in handy for debugging partial symbol
5835 table building. */
5836
5837 if (pdi->d.locdesc || pdi->has_type)
5838 add_psymbol_to_list (actual_name, strlen (actual_name),
5839 built_actual_name,
5840 VAR_DOMAIN, LOC_STATIC,
5841 &objfile->global_psymbols,
5842 0, addr + baseaddr,
5843 cu->language, objfile);
5844 }
5845 else
5846 {
5847 /* Static Variable. Skip symbols without location descriptors. */
5848 if (pdi->d.locdesc == NULL)
5849 {
5850 if (built_actual_name)
5851 xfree (actual_name);
5852 return;
5853 }
5854 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5855 mst_file_data, objfile); */
5856 add_psymbol_to_list (actual_name, strlen (actual_name),
5857 built_actual_name,
5858 VAR_DOMAIN, LOC_STATIC,
5859 &objfile->static_psymbols,
5860 0, addr + baseaddr,
5861 cu->language, objfile);
5862 }
5863 break;
5864 case DW_TAG_typedef:
5865 case DW_TAG_base_type:
5866 case DW_TAG_subrange_type:
5867 add_psymbol_to_list (actual_name, strlen (actual_name),
5868 built_actual_name,
5869 VAR_DOMAIN, LOC_TYPEDEF,
5870 &objfile->static_psymbols,
5871 0, (CORE_ADDR) 0, cu->language, objfile);
5872 break;
5873 case DW_TAG_namespace:
5874 add_psymbol_to_list (actual_name, strlen (actual_name),
5875 built_actual_name,
5876 VAR_DOMAIN, LOC_TYPEDEF,
5877 &objfile->global_psymbols,
5878 0, (CORE_ADDR) 0, cu->language, objfile);
5879 break;
5880 case DW_TAG_class_type:
5881 case DW_TAG_interface_type:
5882 case DW_TAG_structure_type:
5883 case DW_TAG_union_type:
5884 case DW_TAG_enumeration_type:
5885 /* Skip external references. The DWARF standard says in the section
5886 about "Structure, Union, and Class Type Entries": "An incomplete
5887 structure, union or class type is represented by a structure,
5888 union or class entry that does not have a byte size attribute
5889 and that has a DW_AT_declaration attribute." */
5890 if (!pdi->has_byte_size && pdi->is_declaration)
5891 {
5892 if (built_actual_name)
5893 xfree (actual_name);
5894 return;
5895 }
5896
5897 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5898 static vs. global. */
5899 add_psymbol_to_list (actual_name, strlen (actual_name),
5900 built_actual_name,
5901 STRUCT_DOMAIN, LOC_TYPEDEF,
5902 (cu->language == language_cplus
5903 || cu->language == language_java)
5904 ? &objfile->global_psymbols
5905 : &objfile->static_psymbols,
5906 0, (CORE_ADDR) 0, cu->language, objfile);
5907
5908 break;
5909 case DW_TAG_enumerator:
5910 add_psymbol_to_list (actual_name, strlen (actual_name),
5911 built_actual_name,
5912 VAR_DOMAIN, LOC_CONST,
5913 (cu->language == language_cplus
5914 || cu->language == language_java)
5915 ? &objfile->global_psymbols
5916 : &objfile->static_psymbols,
5917 0, (CORE_ADDR) 0, cu->language, objfile);
5918 break;
5919 default:
5920 break;
5921 }
5922
5923 if (built_actual_name)
5924 xfree (actual_name);
5925 }
5926
5927 /* Read a partial die corresponding to a namespace; also, add a symbol
5928 corresponding to that namespace to the symbol table. NAMESPACE is
5929 the name of the enclosing namespace. */
5930
5931 static void
5932 add_partial_namespace (struct partial_die_info *pdi,
5933 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5934 int need_pc, struct dwarf2_cu *cu)
5935 {
5936 /* Add a symbol for the namespace. */
5937
5938 add_partial_symbol (pdi, cu);
5939
5940 /* Now scan partial symbols in that namespace. */
5941
5942 if (pdi->has_children)
5943 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5944 }
5945
5946 /* Read a partial die corresponding to a Fortran module. */
5947
5948 static void
5949 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5950 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5951 {
5952 /* Now scan partial symbols in that module. */
5953
5954 if (pdi->has_children)
5955 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5956 }
5957
5958 /* Read a partial die corresponding to a subprogram and create a partial
5959 symbol for that subprogram. When the CU language allows it, this
5960 routine also defines a partial symbol for each nested subprogram
5961 that this subprogram contains.
5962
5963 DIE my also be a lexical block, in which case we simply search
5964 recursively for suprograms defined inside that lexical block.
5965 Again, this is only performed when the CU language allows this
5966 type of definitions. */
5967
5968 static void
5969 add_partial_subprogram (struct partial_die_info *pdi,
5970 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5971 int need_pc, struct dwarf2_cu *cu)
5972 {
5973 if (pdi->tag == DW_TAG_subprogram)
5974 {
5975 if (pdi->has_pc_info)
5976 {
5977 if (pdi->lowpc < *lowpc)
5978 *lowpc = pdi->lowpc;
5979 if (pdi->highpc > *highpc)
5980 *highpc = pdi->highpc;
5981 if (need_pc)
5982 {
5983 CORE_ADDR baseaddr;
5984 struct objfile *objfile = cu->objfile;
5985
5986 baseaddr = ANOFFSET (objfile->section_offsets,
5987 SECT_OFF_TEXT (objfile));
5988 addrmap_set_empty (objfile->psymtabs_addrmap,
5989 pdi->lowpc + baseaddr,
5990 pdi->highpc - 1 + baseaddr,
5991 cu->per_cu->v.psymtab);
5992 }
5993 }
5994
5995 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5996 {
5997 if (!pdi->is_declaration)
5998 /* Ignore subprogram DIEs that do not have a name, they are
5999 illegal. Do not emit a complaint at this point, we will
6000 do so when we convert this psymtab into a symtab. */
6001 if (pdi->name)
6002 add_partial_symbol (pdi, cu);
6003 }
6004 }
6005
6006 if (! pdi->has_children)
6007 return;
6008
6009 if (cu->language == language_ada)
6010 {
6011 pdi = pdi->die_child;
6012 while (pdi != NULL)
6013 {
6014 fixup_partial_die (pdi, cu);
6015 if (pdi->tag == DW_TAG_subprogram
6016 || pdi->tag == DW_TAG_lexical_block)
6017 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6018 pdi = pdi->die_sibling;
6019 }
6020 }
6021 }
6022
6023 /* Read a partial die corresponding to an enumeration type. */
6024
6025 static void
6026 add_partial_enumeration (struct partial_die_info *enum_pdi,
6027 struct dwarf2_cu *cu)
6028 {
6029 struct partial_die_info *pdi;
6030
6031 if (enum_pdi->name != NULL)
6032 add_partial_symbol (enum_pdi, cu);
6033
6034 pdi = enum_pdi->die_child;
6035 while (pdi)
6036 {
6037 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6038 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6039 else
6040 add_partial_symbol (pdi, cu);
6041 pdi = pdi->die_sibling;
6042 }
6043 }
6044
6045 /* Return the initial uleb128 in the die at INFO_PTR. */
6046
6047 static unsigned int
6048 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6049 {
6050 unsigned int bytes_read;
6051
6052 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6053 }
6054
6055 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6056 Return the corresponding abbrev, or NULL if the number is zero (indicating
6057 an empty DIE). In either case *BYTES_READ will be set to the length of
6058 the initial number. */
6059
6060 static struct abbrev_info *
6061 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6062 struct dwarf2_cu *cu)
6063 {
6064 bfd *abfd = cu->objfile->obfd;
6065 unsigned int abbrev_number;
6066 struct abbrev_info *abbrev;
6067
6068 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6069
6070 if (abbrev_number == 0)
6071 return NULL;
6072
6073 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6074 if (!abbrev)
6075 {
6076 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6077 abbrev_number, bfd_get_filename (abfd));
6078 }
6079
6080 return abbrev;
6081 }
6082
6083 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6084 Returns a pointer to the end of a series of DIEs, terminated by an empty
6085 DIE. Any children of the skipped DIEs will also be skipped. */
6086
6087 static gdb_byte *
6088 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6089 {
6090 struct dwarf2_cu *cu = reader->cu;
6091 struct abbrev_info *abbrev;
6092 unsigned int bytes_read;
6093
6094 while (1)
6095 {
6096 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6097 if (abbrev == NULL)
6098 return info_ptr + bytes_read;
6099 else
6100 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6101 }
6102 }
6103
6104 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6105 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6106 abbrev corresponding to that skipped uleb128 should be passed in
6107 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6108 children. */
6109
6110 static gdb_byte *
6111 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6112 struct abbrev_info *abbrev)
6113 {
6114 unsigned int bytes_read;
6115 struct attribute attr;
6116 bfd *abfd = reader->abfd;
6117 struct dwarf2_cu *cu = reader->cu;
6118 gdb_byte *buffer = reader->buffer;
6119 const gdb_byte *buffer_end = reader->buffer_end;
6120 gdb_byte *start_info_ptr = info_ptr;
6121 unsigned int form, i;
6122
6123 for (i = 0; i < abbrev->num_attrs; i++)
6124 {
6125 /* The only abbrev we care about is DW_AT_sibling. */
6126 if (abbrev->attrs[i].name == DW_AT_sibling)
6127 {
6128 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6129 if (attr.form == DW_FORM_ref_addr)
6130 complaint (&symfile_complaints,
6131 _("ignoring absolute DW_AT_sibling"));
6132 else
6133 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6134 }
6135
6136 /* If it isn't DW_AT_sibling, skip this attribute. */
6137 form = abbrev->attrs[i].form;
6138 skip_attribute:
6139 switch (form)
6140 {
6141 case DW_FORM_ref_addr:
6142 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6143 and later it is offset sized. */
6144 if (cu->header.version == 2)
6145 info_ptr += cu->header.addr_size;
6146 else
6147 info_ptr += cu->header.offset_size;
6148 break;
6149 case DW_FORM_GNU_ref_alt:
6150 info_ptr += cu->header.offset_size;
6151 break;
6152 case DW_FORM_addr:
6153 info_ptr += cu->header.addr_size;
6154 break;
6155 case DW_FORM_data1:
6156 case DW_FORM_ref1:
6157 case DW_FORM_flag:
6158 info_ptr += 1;
6159 break;
6160 case DW_FORM_flag_present:
6161 break;
6162 case DW_FORM_data2:
6163 case DW_FORM_ref2:
6164 info_ptr += 2;
6165 break;
6166 case DW_FORM_data4:
6167 case DW_FORM_ref4:
6168 info_ptr += 4;
6169 break;
6170 case DW_FORM_data8:
6171 case DW_FORM_ref8:
6172 case DW_FORM_ref_sig8:
6173 info_ptr += 8;
6174 break;
6175 case DW_FORM_string:
6176 read_direct_string (abfd, info_ptr, &bytes_read);
6177 info_ptr += bytes_read;
6178 break;
6179 case DW_FORM_sec_offset:
6180 case DW_FORM_strp:
6181 case DW_FORM_GNU_strp_alt:
6182 info_ptr += cu->header.offset_size;
6183 break;
6184 case DW_FORM_exprloc:
6185 case DW_FORM_block:
6186 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6187 info_ptr += bytes_read;
6188 break;
6189 case DW_FORM_block1:
6190 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6191 break;
6192 case DW_FORM_block2:
6193 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6194 break;
6195 case DW_FORM_block4:
6196 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6197 break;
6198 case DW_FORM_sdata:
6199 case DW_FORM_udata:
6200 case DW_FORM_ref_udata:
6201 case DW_FORM_GNU_addr_index:
6202 case DW_FORM_GNU_str_index:
6203 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6204 break;
6205 case DW_FORM_indirect:
6206 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6207 info_ptr += bytes_read;
6208 /* We need to continue parsing from here, so just go back to
6209 the top. */
6210 goto skip_attribute;
6211
6212 default:
6213 error (_("Dwarf Error: Cannot handle %s "
6214 "in DWARF reader [in module %s]"),
6215 dwarf_form_name (form),
6216 bfd_get_filename (abfd));
6217 }
6218 }
6219
6220 if (abbrev->has_children)
6221 return skip_children (reader, info_ptr);
6222 else
6223 return info_ptr;
6224 }
6225
6226 /* Locate ORIG_PDI's sibling.
6227 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6228
6229 static gdb_byte *
6230 locate_pdi_sibling (const struct die_reader_specs *reader,
6231 struct partial_die_info *orig_pdi,
6232 gdb_byte *info_ptr)
6233 {
6234 /* Do we know the sibling already? */
6235
6236 if (orig_pdi->sibling)
6237 return orig_pdi->sibling;
6238
6239 /* Are there any children to deal with? */
6240
6241 if (!orig_pdi->has_children)
6242 return info_ptr;
6243
6244 /* Skip the children the long way. */
6245
6246 return skip_children (reader, info_ptr);
6247 }
6248
6249 /* Expand this partial symbol table into a full symbol table. */
6250
6251 static void
6252 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6253 {
6254 if (pst != NULL)
6255 {
6256 if (pst->readin)
6257 {
6258 warning (_("bug: psymtab for %s is already read in."),
6259 pst->filename);
6260 }
6261 else
6262 {
6263 if (info_verbose)
6264 {
6265 printf_filtered (_("Reading in symbols for %s..."),
6266 pst->filename);
6267 gdb_flush (gdb_stdout);
6268 }
6269
6270 /* Restore our global data. */
6271 dwarf2_per_objfile = objfile_data (pst->objfile,
6272 dwarf2_objfile_data_key);
6273
6274 /* If this psymtab is constructed from a debug-only objfile, the
6275 has_section_at_zero flag will not necessarily be correct. We
6276 can get the correct value for this flag by looking at the data
6277 associated with the (presumably stripped) associated objfile. */
6278 if (pst->objfile->separate_debug_objfile_backlink)
6279 {
6280 struct dwarf2_per_objfile *dpo_backlink
6281 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6282 dwarf2_objfile_data_key);
6283
6284 dwarf2_per_objfile->has_section_at_zero
6285 = dpo_backlink->has_section_at_zero;
6286 }
6287
6288 dwarf2_per_objfile->reading_partial_symbols = 0;
6289
6290 psymtab_to_symtab_1 (pst);
6291
6292 /* Finish up the debug error message. */
6293 if (info_verbose)
6294 printf_filtered (_("done.\n"));
6295 }
6296 }
6297
6298 process_cu_includes ();
6299 }
6300 \f
6301 /* Reading in full CUs. */
6302
6303 /* Add PER_CU to the queue. */
6304
6305 static void
6306 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6307 enum language pretend_language)
6308 {
6309 struct dwarf2_queue_item *item;
6310
6311 per_cu->queued = 1;
6312 item = xmalloc (sizeof (*item));
6313 item->per_cu = per_cu;
6314 item->pretend_language = pretend_language;
6315 item->next = NULL;
6316
6317 if (dwarf2_queue == NULL)
6318 dwarf2_queue = item;
6319 else
6320 dwarf2_queue_tail->next = item;
6321
6322 dwarf2_queue_tail = item;
6323 }
6324
6325 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6326 unit and add it to our queue.
6327 The result is non-zero if PER_CU was queued, otherwise the result is zero
6328 meaning either PER_CU is already queued or it is already loaded. */
6329
6330 static int
6331 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6332 struct dwarf2_per_cu_data *per_cu,
6333 enum language pretend_language)
6334 {
6335 /* We may arrive here during partial symbol reading, if we need full
6336 DIEs to process an unusual case (e.g. template arguments). Do
6337 not queue PER_CU, just tell our caller to load its DIEs. */
6338 if (dwarf2_per_objfile->reading_partial_symbols)
6339 {
6340 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6341 return 1;
6342 return 0;
6343 }
6344
6345 /* Mark the dependence relation so that we don't flush PER_CU
6346 too early. */
6347 dwarf2_add_dependence (this_cu, per_cu);
6348
6349 /* If it's already on the queue, we have nothing to do. */
6350 if (per_cu->queued)
6351 return 0;
6352
6353 /* If the compilation unit is already loaded, just mark it as
6354 used. */
6355 if (per_cu->cu != NULL)
6356 {
6357 per_cu->cu->last_used = 0;
6358 return 0;
6359 }
6360
6361 /* Add it to the queue. */
6362 queue_comp_unit (per_cu, pretend_language);
6363
6364 return 1;
6365 }
6366
6367 /* Process the queue. */
6368
6369 static void
6370 process_queue (void)
6371 {
6372 struct dwarf2_queue_item *item, *next_item;
6373
6374 if (dwarf2_read_debug)
6375 {
6376 fprintf_unfiltered (gdb_stdlog,
6377 "Expanding one or more symtabs of objfile %s ...\n",
6378 dwarf2_per_objfile->objfile->name);
6379 }
6380
6381 /* The queue starts out with one item, but following a DIE reference
6382 may load a new CU, adding it to the end of the queue. */
6383 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6384 {
6385 if (dwarf2_per_objfile->using_index
6386 ? !item->per_cu->v.quick->symtab
6387 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6388 {
6389 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6390
6391 if (dwarf2_read_debug)
6392 {
6393 fprintf_unfiltered (gdb_stdlog,
6394 "Expanding symtab of %s at offset 0x%x\n",
6395 per_cu->is_debug_types ? "TU" : "CU",
6396 per_cu->offset.sect_off);
6397 }
6398
6399 if (per_cu->is_debug_types)
6400 process_full_type_unit (per_cu, item->pretend_language);
6401 else
6402 process_full_comp_unit (per_cu, item->pretend_language);
6403
6404 if (dwarf2_read_debug)
6405 {
6406 fprintf_unfiltered (gdb_stdlog,
6407 "Done expanding %s at offset 0x%x\n",
6408 per_cu->is_debug_types ? "TU" : "CU",
6409 per_cu->offset.sect_off);
6410 }
6411 }
6412
6413 item->per_cu->queued = 0;
6414 next_item = item->next;
6415 xfree (item);
6416 }
6417
6418 dwarf2_queue_tail = NULL;
6419
6420 if (dwarf2_read_debug)
6421 {
6422 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6423 dwarf2_per_objfile->objfile->name);
6424 }
6425 }
6426
6427 /* Free all allocated queue entries. This function only releases anything if
6428 an error was thrown; if the queue was processed then it would have been
6429 freed as we went along. */
6430
6431 static void
6432 dwarf2_release_queue (void *dummy)
6433 {
6434 struct dwarf2_queue_item *item, *last;
6435
6436 item = dwarf2_queue;
6437 while (item)
6438 {
6439 /* Anything still marked queued is likely to be in an
6440 inconsistent state, so discard it. */
6441 if (item->per_cu->queued)
6442 {
6443 if (item->per_cu->cu != NULL)
6444 free_one_cached_comp_unit (item->per_cu);
6445 item->per_cu->queued = 0;
6446 }
6447
6448 last = item;
6449 item = item->next;
6450 xfree (last);
6451 }
6452
6453 dwarf2_queue = dwarf2_queue_tail = NULL;
6454 }
6455
6456 /* Read in full symbols for PST, and anything it depends on. */
6457
6458 static void
6459 psymtab_to_symtab_1 (struct partial_symtab *pst)
6460 {
6461 struct dwarf2_per_cu_data *per_cu;
6462 int i;
6463
6464 if (pst->readin)
6465 return;
6466
6467 for (i = 0; i < pst->number_of_dependencies; i++)
6468 if (!pst->dependencies[i]->readin
6469 && pst->dependencies[i]->user == NULL)
6470 {
6471 /* Inform about additional files that need to be read in. */
6472 if (info_verbose)
6473 {
6474 /* FIXME: i18n: Need to make this a single string. */
6475 fputs_filtered (" ", gdb_stdout);
6476 wrap_here ("");
6477 fputs_filtered ("and ", gdb_stdout);
6478 wrap_here ("");
6479 printf_filtered ("%s...", pst->dependencies[i]->filename);
6480 wrap_here (""); /* Flush output. */
6481 gdb_flush (gdb_stdout);
6482 }
6483 psymtab_to_symtab_1 (pst->dependencies[i]);
6484 }
6485
6486 per_cu = pst->read_symtab_private;
6487
6488 if (per_cu == NULL)
6489 {
6490 /* It's an include file, no symbols to read for it.
6491 Everything is in the parent symtab. */
6492 pst->readin = 1;
6493 return;
6494 }
6495
6496 dw2_do_instantiate_symtab (per_cu);
6497 }
6498
6499 /* Trivial hash function for die_info: the hash value of a DIE
6500 is its offset in .debug_info for this objfile. */
6501
6502 static hashval_t
6503 die_hash (const void *item)
6504 {
6505 const struct die_info *die = item;
6506
6507 return die->offset.sect_off;
6508 }
6509
6510 /* Trivial comparison function for die_info structures: two DIEs
6511 are equal if they have the same offset. */
6512
6513 static int
6514 die_eq (const void *item_lhs, const void *item_rhs)
6515 {
6516 const struct die_info *die_lhs = item_lhs;
6517 const struct die_info *die_rhs = item_rhs;
6518
6519 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6520 }
6521
6522 /* die_reader_func for load_full_comp_unit.
6523 This is identical to read_signatured_type_reader,
6524 but is kept separate for now. */
6525
6526 static void
6527 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6528 gdb_byte *info_ptr,
6529 struct die_info *comp_unit_die,
6530 int has_children,
6531 void *data)
6532 {
6533 struct dwarf2_cu *cu = reader->cu;
6534 enum language *language_ptr = data;
6535
6536 gdb_assert (cu->die_hash == NULL);
6537 cu->die_hash =
6538 htab_create_alloc_ex (cu->header.length / 12,
6539 die_hash,
6540 die_eq,
6541 NULL,
6542 &cu->comp_unit_obstack,
6543 hashtab_obstack_allocate,
6544 dummy_obstack_deallocate);
6545
6546 if (has_children)
6547 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6548 &info_ptr, comp_unit_die);
6549 cu->dies = comp_unit_die;
6550 /* comp_unit_die is not stored in die_hash, no need. */
6551
6552 /* We try not to read any attributes in this function, because not
6553 all CUs needed for references have been loaded yet, and symbol
6554 table processing isn't initialized. But we have to set the CU language,
6555 or we won't be able to build types correctly.
6556 Similarly, if we do not read the producer, we can not apply
6557 producer-specific interpretation. */
6558 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6559 }
6560
6561 /* Load the DIEs associated with PER_CU into memory. */
6562
6563 static void
6564 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6565 enum language pretend_language)
6566 {
6567 gdb_assert (! this_cu->is_debug_types);
6568
6569 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6570 load_full_comp_unit_reader, &pretend_language);
6571 }
6572
6573 /* Add a DIE to the delayed physname list. */
6574
6575 static void
6576 add_to_method_list (struct type *type, int fnfield_index, int index,
6577 const char *name, struct die_info *die,
6578 struct dwarf2_cu *cu)
6579 {
6580 struct delayed_method_info mi;
6581 mi.type = type;
6582 mi.fnfield_index = fnfield_index;
6583 mi.index = index;
6584 mi.name = name;
6585 mi.die = die;
6586 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6587 }
6588
6589 /* A cleanup for freeing the delayed method list. */
6590
6591 static void
6592 free_delayed_list (void *ptr)
6593 {
6594 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6595 if (cu->method_list != NULL)
6596 {
6597 VEC_free (delayed_method_info, cu->method_list);
6598 cu->method_list = NULL;
6599 }
6600 }
6601
6602 /* Compute the physnames of any methods on the CU's method list.
6603
6604 The computation of method physnames is delayed in order to avoid the
6605 (bad) condition that one of the method's formal parameters is of an as yet
6606 incomplete type. */
6607
6608 static void
6609 compute_delayed_physnames (struct dwarf2_cu *cu)
6610 {
6611 int i;
6612 struct delayed_method_info *mi;
6613 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6614 {
6615 const char *physname;
6616 struct fn_fieldlist *fn_flp
6617 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6618 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6619 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6620 }
6621 }
6622
6623 /* Go objects should be embedded in a DW_TAG_module DIE,
6624 and it's not clear if/how imported objects will appear.
6625 To keep Go support simple until that's worked out,
6626 go back through what we've read and create something usable.
6627 We could do this while processing each DIE, and feels kinda cleaner,
6628 but that way is more invasive.
6629 This is to, for example, allow the user to type "p var" or "b main"
6630 without having to specify the package name, and allow lookups
6631 of module.object to work in contexts that use the expression
6632 parser. */
6633
6634 static void
6635 fixup_go_packaging (struct dwarf2_cu *cu)
6636 {
6637 char *package_name = NULL;
6638 struct pending *list;
6639 int i;
6640
6641 for (list = global_symbols; list != NULL; list = list->next)
6642 {
6643 for (i = 0; i < list->nsyms; ++i)
6644 {
6645 struct symbol *sym = list->symbol[i];
6646
6647 if (SYMBOL_LANGUAGE (sym) == language_go
6648 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6649 {
6650 char *this_package_name = go_symbol_package_name (sym);
6651
6652 if (this_package_name == NULL)
6653 continue;
6654 if (package_name == NULL)
6655 package_name = this_package_name;
6656 else
6657 {
6658 if (strcmp (package_name, this_package_name) != 0)
6659 complaint (&symfile_complaints,
6660 _("Symtab %s has objects from two different Go packages: %s and %s"),
6661 (sym->symtab && sym->symtab->filename
6662 ? sym->symtab->filename
6663 : cu->objfile->name),
6664 this_package_name, package_name);
6665 xfree (this_package_name);
6666 }
6667 }
6668 }
6669 }
6670
6671 if (package_name != NULL)
6672 {
6673 struct objfile *objfile = cu->objfile;
6674 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6675 package_name, objfile);
6676 struct symbol *sym;
6677
6678 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6679
6680 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6681 SYMBOL_SET_LANGUAGE (sym, language_go);
6682 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6683 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6684 e.g., "main" finds the "main" module and not C's main(). */
6685 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6686 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6687 SYMBOL_TYPE (sym) = type;
6688
6689 add_symbol_to_list (sym, &global_symbols);
6690
6691 xfree (package_name);
6692 }
6693 }
6694
6695 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6696
6697 /* Return the symtab for PER_CU. This works properly regardless of
6698 whether we're using the index or psymtabs. */
6699
6700 static struct symtab *
6701 get_symtab (struct dwarf2_per_cu_data *per_cu)
6702 {
6703 return (dwarf2_per_objfile->using_index
6704 ? per_cu->v.quick->symtab
6705 : per_cu->v.psymtab->symtab);
6706 }
6707
6708 /* A helper function for computing the list of all symbol tables
6709 included by PER_CU. */
6710
6711 static void
6712 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6713 htab_t all_children,
6714 struct dwarf2_per_cu_data *per_cu)
6715 {
6716 void **slot;
6717 int ix;
6718 struct dwarf2_per_cu_data *iter;
6719
6720 slot = htab_find_slot (all_children, per_cu, INSERT);
6721 if (*slot != NULL)
6722 {
6723 /* This inclusion and its children have been processed. */
6724 return;
6725 }
6726
6727 *slot = per_cu;
6728 /* Only add a CU if it has a symbol table. */
6729 if (get_symtab (per_cu) != NULL)
6730 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6731
6732 for (ix = 0;
6733 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6734 ++ix)
6735 recursively_compute_inclusions (result, all_children, iter);
6736 }
6737
6738 /* Compute the symtab 'includes' fields for the symtab related to
6739 PER_CU. */
6740
6741 static void
6742 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6743 {
6744 gdb_assert (! per_cu->is_debug_types);
6745
6746 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6747 {
6748 int ix, len;
6749 struct dwarf2_per_cu_data *iter;
6750 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6751 htab_t all_children;
6752 struct symtab *symtab = get_symtab (per_cu);
6753
6754 /* If we don't have a symtab, we can just skip this case. */
6755 if (symtab == NULL)
6756 return;
6757
6758 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6759 NULL, xcalloc, xfree);
6760
6761 for (ix = 0;
6762 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6763 ix, iter);
6764 ++ix)
6765 recursively_compute_inclusions (&result_children, all_children, iter);
6766
6767 /* Now we have a transitive closure of all the included CUs, so
6768 we can convert it to a list of symtabs. */
6769 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6770 symtab->includes
6771 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6772 (len + 1) * sizeof (struct symtab *));
6773 for (ix = 0;
6774 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6775 ++ix)
6776 symtab->includes[ix] = get_symtab (iter);
6777 symtab->includes[len] = NULL;
6778
6779 VEC_free (dwarf2_per_cu_ptr, result_children);
6780 htab_delete (all_children);
6781 }
6782 }
6783
6784 /* Compute the 'includes' field for the symtabs of all the CUs we just
6785 read. */
6786
6787 static void
6788 process_cu_includes (void)
6789 {
6790 int ix;
6791 struct dwarf2_per_cu_data *iter;
6792
6793 for (ix = 0;
6794 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6795 ix, iter);
6796 ++ix)
6797 {
6798 if (! iter->is_debug_types)
6799 compute_symtab_includes (iter);
6800 }
6801
6802 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6803 }
6804
6805 /* Generate full symbol information for PER_CU, whose DIEs have
6806 already been loaded into memory. */
6807
6808 static void
6809 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6810 enum language pretend_language)
6811 {
6812 struct dwarf2_cu *cu = per_cu->cu;
6813 struct objfile *objfile = per_cu->objfile;
6814 CORE_ADDR lowpc, highpc;
6815 struct symtab *symtab;
6816 struct cleanup *back_to, *delayed_list_cleanup;
6817 CORE_ADDR baseaddr;
6818 struct block *static_block;
6819
6820 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6821
6822 buildsym_init ();
6823 back_to = make_cleanup (really_free_pendings, NULL);
6824 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6825
6826 cu->list_in_scope = &file_symbols;
6827
6828 cu->language = pretend_language;
6829 cu->language_defn = language_def (cu->language);
6830
6831 /* Do line number decoding in read_file_scope () */
6832 process_die (cu->dies, cu);
6833
6834 /* For now fudge the Go package. */
6835 if (cu->language == language_go)
6836 fixup_go_packaging (cu);
6837
6838 /* Now that we have processed all the DIEs in the CU, all the types
6839 should be complete, and it should now be safe to compute all of the
6840 physnames. */
6841 compute_delayed_physnames (cu);
6842 do_cleanups (delayed_list_cleanup);
6843
6844 /* Some compilers don't define a DW_AT_high_pc attribute for the
6845 compilation unit. If the DW_AT_high_pc is missing, synthesize
6846 it, by scanning the DIE's below the compilation unit. */
6847 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6848
6849 static_block
6850 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6851 per_cu->s.imported_symtabs != NULL);
6852
6853 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6854 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6855 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6856 addrmap to help ensure it has an accurate map of pc values belonging to
6857 this comp unit. */
6858 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6859
6860 symtab = end_symtab_from_static_block (static_block, objfile,
6861 SECT_OFF_TEXT (objfile), 0);
6862
6863 if (symtab != NULL)
6864 {
6865 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6866
6867 /* Set symtab language to language from DW_AT_language. If the
6868 compilation is from a C file generated by language preprocessors, do
6869 not set the language if it was already deduced by start_subfile. */
6870 if (!(cu->language == language_c && symtab->language != language_c))
6871 symtab->language = cu->language;
6872
6873 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6874 produce DW_AT_location with location lists but it can be possibly
6875 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6876 there were bugs in prologue debug info, fixed later in GCC-4.5
6877 by "unwind info for epilogues" patch (which is not directly related).
6878
6879 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6880 needed, it would be wrong due to missing DW_AT_producer there.
6881
6882 Still one can confuse GDB by using non-standard GCC compilation
6883 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6884 */
6885 if (cu->has_loclist && gcc_4_minor >= 5)
6886 symtab->locations_valid = 1;
6887
6888 if (gcc_4_minor >= 5)
6889 symtab->epilogue_unwind_valid = 1;
6890
6891 symtab->call_site_htab = cu->call_site_htab;
6892 }
6893
6894 if (dwarf2_per_objfile->using_index)
6895 per_cu->v.quick->symtab = symtab;
6896 else
6897 {
6898 struct partial_symtab *pst = per_cu->v.psymtab;
6899 pst->symtab = symtab;
6900 pst->readin = 1;
6901 }
6902
6903 /* Push it for inclusion processing later. */
6904 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6905
6906 do_cleanups (back_to);
6907 }
6908
6909 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6910 already been loaded into memory. */
6911
6912 static void
6913 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6914 enum language pretend_language)
6915 {
6916 struct dwarf2_cu *cu = per_cu->cu;
6917 struct objfile *objfile = per_cu->objfile;
6918 struct symtab *symtab;
6919 struct cleanup *back_to, *delayed_list_cleanup;
6920
6921 buildsym_init ();
6922 back_to = make_cleanup (really_free_pendings, NULL);
6923 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6924
6925 cu->list_in_scope = &file_symbols;
6926
6927 cu->language = pretend_language;
6928 cu->language_defn = language_def (cu->language);
6929
6930 /* The symbol tables are set up in read_type_unit_scope. */
6931 process_die (cu->dies, cu);
6932
6933 /* For now fudge the Go package. */
6934 if (cu->language == language_go)
6935 fixup_go_packaging (cu);
6936
6937 /* Now that we have processed all the DIEs in the CU, all the types
6938 should be complete, and it should now be safe to compute all of the
6939 physnames. */
6940 compute_delayed_physnames (cu);
6941 do_cleanups (delayed_list_cleanup);
6942
6943 /* TUs share symbol tables.
6944 If this is the first TU to use this symtab, complete the construction
6945 of it with end_expandable_symtab. Otherwise, complete the addition of
6946 this TU's symbols to the existing symtab. */
6947 if (per_cu->s.type_unit_group->primary_symtab == NULL)
6948 {
6949 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6950 per_cu->s.type_unit_group->primary_symtab = symtab;
6951
6952 if (symtab != NULL)
6953 {
6954 /* Set symtab language to language from DW_AT_language. If the
6955 compilation is from a C file generated by language preprocessors,
6956 do not set the language if it was already deduced by
6957 start_subfile. */
6958 if (!(cu->language == language_c && symtab->language != language_c))
6959 symtab->language = cu->language;
6960 }
6961 }
6962 else
6963 {
6964 augment_type_symtab (objfile,
6965 per_cu->s.type_unit_group->primary_symtab);
6966 symtab = per_cu->s.type_unit_group->primary_symtab;
6967 }
6968
6969 if (dwarf2_per_objfile->using_index)
6970 per_cu->v.quick->symtab = symtab;
6971 else
6972 {
6973 struct partial_symtab *pst = per_cu->v.psymtab;
6974 pst->symtab = symtab;
6975 pst->readin = 1;
6976 }
6977
6978 do_cleanups (back_to);
6979 }
6980
6981 /* Process an imported unit DIE. */
6982
6983 static void
6984 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6985 {
6986 struct attribute *attr;
6987
6988 /* For now we don't handle imported units in type units. */
6989 if (cu->per_cu->is_debug_types)
6990 {
6991 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6992 " supported in type units [in module %s]"),
6993 cu->objfile->name);
6994 }
6995
6996 attr = dwarf2_attr (die, DW_AT_import, cu);
6997 if (attr != NULL)
6998 {
6999 struct dwarf2_per_cu_data *per_cu;
7000 struct symtab *imported_symtab;
7001 sect_offset offset;
7002 int is_dwz;
7003
7004 offset = dwarf2_get_ref_die_offset (attr);
7005 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7006 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7007
7008 /* Queue the unit, if needed. */
7009 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7010 load_full_comp_unit (per_cu, cu->language);
7011
7012 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7013 per_cu);
7014 }
7015 }
7016
7017 /* Process a die and its children. */
7018
7019 static void
7020 process_die (struct die_info *die, struct dwarf2_cu *cu)
7021 {
7022 switch (die->tag)
7023 {
7024 case DW_TAG_padding:
7025 break;
7026 case DW_TAG_compile_unit:
7027 case DW_TAG_partial_unit:
7028 read_file_scope (die, cu);
7029 break;
7030 case DW_TAG_type_unit:
7031 read_type_unit_scope (die, cu);
7032 break;
7033 case DW_TAG_subprogram:
7034 case DW_TAG_inlined_subroutine:
7035 read_func_scope (die, cu);
7036 break;
7037 case DW_TAG_lexical_block:
7038 case DW_TAG_try_block:
7039 case DW_TAG_catch_block:
7040 read_lexical_block_scope (die, cu);
7041 break;
7042 case DW_TAG_GNU_call_site:
7043 read_call_site_scope (die, cu);
7044 break;
7045 case DW_TAG_class_type:
7046 case DW_TAG_interface_type:
7047 case DW_TAG_structure_type:
7048 case DW_TAG_union_type:
7049 process_structure_scope (die, cu);
7050 break;
7051 case DW_TAG_enumeration_type:
7052 process_enumeration_scope (die, cu);
7053 break;
7054
7055 /* These dies have a type, but processing them does not create
7056 a symbol or recurse to process the children. Therefore we can
7057 read them on-demand through read_type_die. */
7058 case DW_TAG_subroutine_type:
7059 case DW_TAG_set_type:
7060 case DW_TAG_array_type:
7061 case DW_TAG_pointer_type:
7062 case DW_TAG_ptr_to_member_type:
7063 case DW_TAG_reference_type:
7064 case DW_TAG_string_type:
7065 break;
7066
7067 case DW_TAG_base_type:
7068 case DW_TAG_subrange_type:
7069 case DW_TAG_typedef:
7070 /* Add a typedef symbol for the type definition, if it has a
7071 DW_AT_name. */
7072 new_symbol (die, read_type_die (die, cu), cu);
7073 break;
7074 case DW_TAG_common_block:
7075 read_common_block (die, cu);
7076 break;
7077 case DW_TAG_common_inclusion:
7078 break;
7079 case DW_TAG_namespace:
7080 processing_has_namespace_info = 1;
7081 read_namespace (die, cu);
7082 break;
7083 case DW_TAG_module:
7084 processing_has_namespace_info = 1;
7085 read_module (die, cu);
7086 break;
7087 case DW_TAG_imported_declaration:
7088 case DW_TAG_imported_module:
7089 processing_has_namespace_info = 1;
7090 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7091 || cu->language != language_fortran))
7092 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7093 dwarf_tag_name (die->tag));
7094 read_import_statement (die, cu);
7095 break;
7096
7097 case DW_TAG_imported_unit:
7098 process_imported_unit_die (die, cu);
7099 break;
7100
7101 default:
7102 new_symbol (die, NULL, cu);
7103 break;
7104 }
7105 }
7106
7107 /* A helper function for dwarf2_compute_name which determines whether DIE
7108 needs to have the name of the scope prepended to the name listed in the
7109 die. */
7110
7111 static int
7112 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7113 {
7114 struct attribute *attr;
7115
7116 switch (die->tag)
7117 {
7118 case DW_TAG_namespace:
7119 case DW_TAG_typedef:
7120 case DW_TAG_class_type:
7121 case DW_TAG_interface_type:
7122 case DW_TAG_structure_type:
7123 case DW_TAG_union_type:
7124 case DW_TAG_enumeration_type:
7125 case DW_TAG_enumerator:
7126 case DW_TAG_subprogram:
7127 case DW_TAG_member:
7128 return 1;
7129
7130 case DW_TAG_variable:
7131 case DW_TAG_constant:
7132 /* We only need to prefix "globally" visible variables. These include
7133 any variable marked with DW_AT_external or any variable that
7134 lives in a namespace. [Variables in anonymous namespaces
7135 require prefixing, but they are not DW_AT_external.] */
7136
7137 if (dwarf2_attr (die, DW_AT_specification, cu))
7138 {
7139 struct dwarf2_cu *spec_cu = cu;
7140
7141 return die_needs_namespace (die_specification (die, &spec_cu),
7142 spec_cu);
7143 }
7144
7145 attr = dwarf2_attr (die, DW_AT_external, cu);
7146 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7147 && die->parent->tag != DW_TAG_module)
7148 return 0;
7149 /* A variable in a lexical block of some kind does not need a
7150 namespace, even though in C++ such variables may be external
7151 and have a mangled name. */
7152 if (die->parent->tag == DW_TAG_lexical_block
7153 || die->parent->tag == DW_TAG_try_block
7154 || die->parent->tag == DW_TAG_catch_block
7155 || die->parent->tag == DW_TAG_subprogram)
7156 return 0;
7157 return 1;
7158
7159 default:
7160 return 0;
7161 }
7162 }
7163
7164 /* Retrieve the last character from a mem_file. */
7165
7166 static void
7167 do_ui_file_peek_last (void *object, const char *buffer, long length)
7168 {
7169 char *last_char_p = (char *) object;
7170
7171 if (length > 0)
7172 *last_char_p = buffer[length - 1];
7173 }
7174
7175 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7176 compute the physname for the object, which include a method's:
7177 - formal parameters (C++/Java),
7178 - receiver type (Go),
7179 - return type (Java).
7180
7181 The term "physname" is a bit confusing.
7182 For C++, for example, it is the demangled name.
7183 For Go, for example, it's the mangled name.
7184
7185 For Ada, return the DIE's linkage name rather than the fully qualified
7186 name. PHYSNAME is ignored..
7187
7188 The result is allocated on the objfile_obstack and canonicalized. */
7189
7190 static const char *
7191 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7192 int physname)
7193 {
7194 struct objfile *objfile = cu->objfile;
7195
7196 if (name == NULL)
7197 name = dwarf2_name (die, cu);
7198
7199 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7200 compute it by typename_concat inside GDB. */
7201 if (cu->language == language_ada
7202 || (cu->language == language_fortran && physname))
7203 {
7204 /* For Ada unit, we prefer the linkage name over the name, as
7205 the former contains the exported name, which the user expects
7206 to be able to reference. Ideally, we want the user to be able
7207 to reference this entity using either natural or linkage name,
7208 but we haven't started looking at this enhancement yet. */
7209 struct attribute *attr;
7210
7211 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7212 if (attr == NULL)
7213 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7214 if (attr && DW_STRING (attr))
7215 return DW_STRING (attr);
7216 }
7217
7218 /* These are the only languages we know how to qualify names in. */
7219 if (name != NULL
7220 && (cu->language == language_cplus || cu->language == language_java
7221 || cu->language == language_fortran))
7222 {
7223 if (die_needs_namespace (die, cu))
7224 {
7225 long length;
7226 const char *prefix;
7227 struct ui_file *buf;
7228
7229 prefix = determine_prefix (die, cu);
7230 buf = mem_fileopen ();
7231 if (*prefix != '\0')
7232 {
7233 char *prefixed_name = typename_concat (NULL, prefix, name,
7234 physname, cu);
7235
7236 fputs_unfiltered (prefixed_name, buf);
7237 xfree (prefixed_name);
7238 }
7239 else
7240 fputs_unfiltered (name, buf);
7241
7242 /* Template parameters may be specified in the DIE's DW_AT_name, or
7243 as children with DW_TAG_template_type_param or
7244 DW_TAG_value_type_param. If the latter, add them to the name
7245 here. If the name already has template parameters, then
7246 skip this step; some versions of GCC emit both, and
7247 it is more efficient to use the pre-computed name.
7248
7249 Something to keep in mind about this process: it is very
7250 unlikely, or in some cases downright impossible, to produce
7251 something that will match the mangled name of a function.
7252 If the definition of the function has the same debug info,
7253 we should be able to match up with it anyway. But fallbacks
7254 using the minimal symbol, for instance to find a method
7255 implemented in a stripped copy of libstdc++, will not work.
7256 If we do not have debug info for the definition, we will have to
7257 match them up some other way.
7258
7259 When we do name matching there is a related problem with function
7260 templates; two instantiated function templates are allowed to
7261 differ only by their return types, which we do not add here. */
7262
7263 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7264 {
7265 struct attribute *attr;
7266 struct die_info *child;
7267 int first = 1;
7268
7269 die->building_fullname = 1;
7270
7271 for (child = die->child; child != NULL; child = child->sibling)
7272 {
7273 struct type *type;
7274 LONGEST value;
7275 gdb_byte *bytes;
7276 struct dwarf2_locexpr_baton *baton;
7277 struct value *v;
7278
7279 if (child->tag != DW_TAG_template_type_param
7280 && child->tag != DW_TAG_template_value_param)
7281 continue;
7282
7283 if (first)
7284 {
7285 fputs_unfiltered ("<", buf);
7286 first = 0;
7287 }
7288 else
7289 fputs_unfiltered (", ", buf);
7290
7291 attr = dwarf2_attr (child, DW_AT_type, cu);
7292 if (attr == NULL)
7293 {
7294 complaint (&symfile_complaints,
7295 _("template parameter missing DW_AT_type"));
7296 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7297 continue;
7298 }
7299 type = die_type (child, cu);
7300
7301 if (child->tag == DW_TAG_template_type_param)
7302 {
7303 c_print_type (type, "", buf, -1, 0);
7304 continue;
7305 }
7306
7307 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7308 if (attr == NULL)
7309 {
7310 complaint (&symfile_complaints,
7311 _("template parameter missing "
7312 "DW_AT_const_value"));
7313 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7314 continue;
7315 }
7316
7317 dwarf2_const_value_attr (attr, type, name,
7318 &cu->comp_unit_obstack, cu,
7319 &value, &bytes, &baton);
7320
7321 if (TYPE_NOSIGN (type))
7322 /* GDB prints characters as NUMBER 'CHAR'. If that's
7323 changed, this can use value_print instead. */
7324 c_printchar (value, type, buf);
7325 else
7326 {
7327 struct value_print_options opts;
7328
7329 if (baton != NULL)
7330 v = dwarf2_evaluate_loc_desc (type, NULL,
7331 baton->data,
7332 baton->size,
7333 baton->per_cu);
7334 else if (bytes != NULL)
7335 {
7336 v = allocate_value (type);
7337 memcpy (value_contents_writeable (v), bytes,
7338 TYPE_LENGTH (type));
7339 }
7340 else
7341 v = value_from_longest (type, value);
7342
7343 /* Specify decimal so that we do not depend on
7344 the radix. */
7345 get_formatted_print_options (&opts, 'd');
7346 opts.raw = 1;
7347 value_print (v, buf, &opts);
7348 release_value (v);
7349 value_free (v);
7350 }
7351 }
7352
7353 die->building_fullname = 0;
7354
7355 if (!first)
7356 {
7357 /* Close the argument list, with a space if necessary
7358 (nested templates). */
7359 char last_char = '\0';
7360 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7361 if (last_char == '>')
7362 fputs_unfiltered (" >", buf);
7363 else
7364 fputs_unfiltered (">", buf);
7365 }
7366 }
7367
7368 /* For Java and C++ methods, append formal parameter type
7369 information, if PHYSNAME. */
7370
7371 if (physname && die->tag == DW_TAG_subprogram
7372 && (cu->language == language_cplus
7373 || cu->language == language_java))
7374 {
7375 struct type *type = read_type_die (die, cu);
7376
7377 c_type_print_args (type, buf, 1, cu->language);
7378
7379 if (cu->language == language_java)
7380 {
7381 /* For java, we must append the return type to method
7382 names. */
7383 if (die->tag == DW_TAG_subprogram)
7384 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7385 0, 0);
7386 }
7387 else if (cu->language == language_cplus)
7388 {
7389 /* Assume that an artificial first parameter is
7390 "this", but do not crash if it is not. RealView
7391 marks unnamed (and thus unused) parameters as
7392 artificial; there is no way to differentiate
7393 the two cases. */
7394 if (TYPE_NFIELDS (type) > 0
7395 && TYPE_FIELD_ARTIFICIAL (type, 0)
7396 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7397 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7398 0))))
7399 fputs_unfiltered (" const", buf);
7400 }
7401 }
7402
7403 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7404 &length);
7405 ui_file_delete (buf);
7406
7407 if (cu->language == language_cplus)
7408 {
7409 char *cname
7410 = dwarf2_canonicalize_name (name, cu,
7411 &objfile->objfile_obstack);
7412
7413 if (cname != NULL)
7414 name = cname;
7415 }
7416 }
7417 }
7418
7419 return name;
7420 }
7421
7422 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7423 If scope qualifiers are appropriate they will be added. The result
7424 will be allocated on the objfile_obstack, or NULL if the DIE does
7425 not have a name. NAME may either be from a previous call to
7426 dwarf2_name or NULL.
7427
7428 The output string will be canonicalized (if C++/Java). */
7429
7430 static const char *
7431 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7432 {
7433 return dwarf2_compute_name (name, die, cu, 0);
7434 }
7435
7436 /* Construct a physname for the given DIE in CU. NAME may either be
7437 from a previous call to dwarf2_name or NULL. The result will be
7438 allocated on the objfile_objstack or NULL if the DIE does not have a
7439 name.
7440
7441 The output string will be canonicalized (if C++/Java). */
7442
7443 static const char *
7444 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7445 {
7446 struct objfile *objfile = cu->objfile;
7447 struct attribute *attr;
7448 const char *retval, *mangled = NULL, *canon = NULL;
7449 struct cleanup *back_to;
7450 int need_copy = 1;
7451
7452 /* In this case dwarf2_compute_name is just a shortcut not building anything
7453 on its own. */
7454 if (!die_needs_namespace (die, cu))
7455 return dwarf2_compute_name (name, die, cu, 1);
7456
7457 back_to = make_cleanup (null_cleanup, NULL);
7458
7459 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7460 if (!attr)
7461 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7462
7463 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7464 has computed. */
7465 if (attr && DW_STRING (attr))
7466 {
7467 char *demangled;
7468
7469 mangled = DW_STRING (attr);
7470
7471 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7472 type. It is easier for GDB users to search for such functions as
7473 `name(params)' than `long name(params)'. In such case the minimal
7474 symbol names do not match the full symbol names but for template
7475 functions there is never a need to look up their definition from their
7476 declaration so the only disadvantage remains the minimal symbol
7477 variant `long name(params)' does not have the proper inferior type.
7478 */
7479
7480 if (cu->language == language_go)
7481 {
7482 /* This is a lie, but we already lie to the caller new_symbol_full.
7483 new_symbol_full assumes we return the mangled name.
7484 This just undoes that lie until things are cleaned up. */
7485 demangled = NULL;
7486 }
7487 else
7488 {
7489 demangled = cplus_demangle (mangled,
7490 (DMGL_PARAMS | DMGL_ANSI
7491 | (cu->language == language_java
7492 ? DMGL_JAVA | DMGL_RET_POSTFIX
7493 : DMGL_RET_DROP)));
7494 }
7495 if (demangled)
7496 {
7497 make_cleanup (xfree, demangled);
7498 canon = demangled;
7499 }
7500 else
7501 {
7502 canon = mangled;
7503 need_copy = 0;
7504 }
7505 }
7506
7507 if (canon == NULL || check_physname)
7508 {
7509 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7510
7511 if (canon != NULL && strcmp (physname, canon) != 0)
7512 {
7513 /* It may not mean a bug in GDB. The compiler could also
7514 compute DW_AT_linkage_name incorrectly. But in such case
7515 GDB would need to be bug-to-bug compatible. */
7516
7517 complaint (&symfile_complaints,
7518 _("Computed physname <%s> does not match demangled <%s> "
7519 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7520 physname, canon, mangled, die->offset.sect_off, objfile->name);
7521
7522 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7523 is available here - over computed PHYSNAME. It is safer
7524 against both buggy GDB and buggy compilers. */
7525
7526 retval = canon;
7527 }
7528 else
7529 {
7530 retval = physname;
7531 need_copy = 0;
7532 }
7533 }
7534 else
7535 retval = canon;
7536
7537 if (need_copy)
7538 retval = obsavestring (retval, strlen (retval),
7539 &objfile->objfile_obstack);
7540
7541 do_cleanups (back_to);
7542 return retval;
7543 }
7544
7545 /* Read the import statement specified by the given die and record it. */
7546
7547 static void
7548 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7549 {
7550 struct objfile *objfile = cu->objfile;
7551 struct attribute *import_attr;
7552 struct die_info *imported_die, *child_die;
7553 struct dwarf2_cu *imported_cu;
7554 const char *imported_name;
7555 const char *imported_name_prefix;
7556 const char *canonical_name;
7557 const char *import_alias;
7558 const char *imported_declaration = NULL;
7559 const char *import_prefix;
7560 VEC (const_char_ptr) *excludes = NULL;
7561 struct cleanup *cleanups;
7562
7563 char *temp;
7564
7565 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7566 if (import_attr == NULL)
7567 {
7568 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7569 dwarf_tag_name (die->tag));
7570 return;
7571 }
7572
7573 imported_cu = cu;
7574 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7575 imported_name = dwarf2_name (imported_die, imported_cu);
7576 if (imported_name == NULL)
7577 {
7578 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7579
7580 The import in the following code:
7581 namespace A
7582 {
7583 typedef int B;
7584 }
7585
7586 int main ()
7587 {
7588 using A::B;
7589 B b;
7590 return b;
7591 }
7592
7593 ...
7594 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7595 <52> DW_AT_decl_file : 1
7596 <53> DW_AT_decl_line : 6
7597 <54> DW_AT_import : <0x75>
7598 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7599 <59> DW_AT_name : B
7600 <5b> DW_AT_decl_file : 1
7601 <5c> DW_AT_decl_line : 2
7602 <5d> DW_AT_type : <0x6e>
7603 ...
7604 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7605 <76> DW_AT_byte_size : 4
7606 <77> DW_AT_encoding : 5 (signed)
7607
7608 imports the wrong die ( 0x75 instead of 0x58 ).
7609 This case will be ignored until the gcc bug is fixed. */
7610 return;
7611 }
7612
7613 /* Figure out the local name after import. */
7614 import_alias = dwarf2_name (die, cu);
7615
7616 /* Figure out where the statement is being imported to. */
7617 import_prefix = determine_prefix (die, cu);
7618
7619 /* Figure out what the scope of the imported die is and prepend it
7620 to the name of the imported die. */
7621 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7622
7623 if (imported_die->tag != DW_TAG_namespace
7624 && imported_die->tag != DW_TAG_module)
7625 {
7626 imported_declaration = imported_name;
7627 canonical_name = imported_name_prefix;
7628 }
7629 else if (strlen (imported_name_prefix) > 0)
7630 {
7631 temp = alloca (strlen (imported_name_prefix)
7632 + 2 + strlen (imported_name) + 1);
7633 strcpy (temp, imported_name_prefix);
7634 strcat (temp, "::");
7635 strcat (temp, imported_name);
7636 canonical_name = temp;
7637 }
7638 else
7639 canonical_name = imported_name;
7640
7641 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7642
7643 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7644 for (child_die = die->child; child_die && child_die->tag;
7645 child_die = sibling_die (child_die))
7646 {
7647 /* DWARF-4: A Fortran use statement with a “rename list” may be
7648 represented by an imported module entry with an import attribute
7649 referring to the module and owned entries corresponding to those
7650 entities that are renamed as part of being imported. */
7651
7652 if (child_die->tag != DW_TAG_imported_declaration)
7653 {
7654 complaint (&symfile_complaints,
7655 _("child DW_TAG_imported_declaration expected "
7656 "- DIE at 0x%x [in module %s]"),
7657 child_die->offset.sect_off, objfile->name);
7658 continue;
7659 }
7660
7661 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7662 if (import_attr == NULL)
7663 {
7664 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7665 dwarf_tag_name (child_die->tag));
7666 continue;
7667 }
7668
7669 imported_cu = cu;
7670 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7671 &imported_cu);
7672 imported_name = dwarf2_name (imported_die, imported_cu);
7673 if (imported_name == NULL)
7674 {
7675 complaint (&symfile_complaints,
7676 _("child DW_TAG_imported_declaration has unknown "
7677 "imported name - DIE at 0x%x [in module %s]"),
7678 child_die->offset.sect_off, objfile->name);
7679 continue;
7680 }
7681
7682 VEC_safe_push (const_char_ptr, excludes, imported_name);
7683
7684 process_die (child_die, cu);
7685 }
7686
7687 cp_add_using_directive (import_prefix,
7688 canonical_name,
7689 import_alias,
7690 imported_declaration,
7691 excludes,
7692 &objfile->objfile_obstack);
7693
7694 do_cleanups (cleanups);
7695 }
7696
7697 /* Cleanup function for handle_DW_AT_stmt_list. */
7698
7699 static void
7700 free_cu_line_header (void *arg)
7701 {
7702 struct dwarf2_cu *cu = arg;
7703
7704 free_line_header (cu->line_header);
7705 cu->line_header = NULL;
7706 }
7707
7708 static void
7709 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7710 char **name, char **comp_dir)
7711 {
7712 struct attribute *attr;
7713
7714 *name = NULL;
7715 *comp_dir = NULL;
7716
7717 /* Find the filename. Do not use dwarf2_name here, since the filename
7718 is not a source language identifier. */
7719 attr = dwarf2_attr (die, DW_AT_name, cu);
7720 if (attr)
7721 {
7722 *name = DW_STRING (attr);
7723 }
7724
7725 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7726 if (attr)
7727 *comp_dir = DW_STRING (attr);
7728 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7729 {
7730 *comp_dir = ldirname (*name);
7731 if (*comp_dir != NULL)
7732 make_cleanup (xfree, *comp_dir);
7733 }
7734 if (*comp_dir != NULL)
7735 {
7736 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7737 directory, get rid of it. */
7738 char *cp = strchr (*comp_dir, ':');
7739
7740 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7741 *comp_dir = cp + 1;
7742 }
7743
7744 if (*name == NULL)
7745 *name = "<unknown>";
7746 }
7747
7748 /* Handle DW_AT_stmt_list for a compilation unit.
7749 DIE is the DW_TAG_compile_unit die for CU.
7750 COMP_DIR is the compilation directory.
7751 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7752
7753 static void
7754 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7755 const char *comp_dir)
7756 {
7757 struct attribute *attr;
7758
7759 gdb_assert (! cu->per_cu->is_debug_types);
7760
7761 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7762 if (attr)
7763 {
7764 unsigned int line_offset = DW_UNSND (attr);
7765 struct line_header *line_header
7766 = dwarf_decode_line_header (line_offset, cu);
7767
7768 if (line_header)
7769 {
7770 cu->line_header = line_header;
7771 make_cleanup (free_cu_line_header, cu);
7772 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7773 }
7774 }
7775 }
7776
7777 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7778
7779 static void
7780 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7781 {
7782 struct objfile *objfile = dwarf2_per_objfile->objfile;
7783 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7784 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7785 CORE_ADDR highpc = ((CORE_ADDR) 0);
7786 struct attribute *attr;
7787 char *name = NULL;
7788 char *comp_dir = NULL;
7789 struct die_info *child_die;
7790 bfd *abfd = objfile->obfd;
7791 CORE_ADDR baseaddr;
7792
7793 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7794
7795 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7796
7797 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7798 from finish_block. */
7799 if (lowpc == ((CORE_ADDR) -1))
7800 lowpc = highpc;
7801 lowpc += baseaddr;
7802 highpc += baseaddr;
7803
7804 find_file_and_directory (die, cu, &name, &comp_dir);
7805
7806 prepare_one_comp_unit (cu, die, cu->language);
7807
7808 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7809 standardised yet. As a workaround for the language detection we fall
7810 back to the DW_AT_producer string. */
7811 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7812 cu->language = language_opencl;
7813
7814 /* Similar hack for Go. */
7815 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7816 set_cu_language (DW_LANG_Go, cu);
7817
7818 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7819
7820 /* Decode line number information if present. We do this before
7821 processing child DIEs, so that the line header table is available
7822 for DW_AT_decl_file. */
7823 handle_DW_AT_stmt_list (die, cu, comp_dir);
7824
7825 /* Process all dies in compilation unit. */
7826 if (die->child != NULL)
7827 {
7828 child_die = die->child;
7829 while (child_die && child_die->tag)
7830 {
7831 process_die (child_die, cu);
7832 child_die = sibling_die (child_die);
7833 }
7834 }
7835
7836 /* Decode macro information, if present. Dwarf 2 macro information
7837 refers to information in the line number info statement program
7838 header, so we can only read it if we've read the header
7839 successfully. */
7840 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7841 if (attr && cu->line_header)
7842 {
7843 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7844 complaint (&symfile_complaints,
7845 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7846
7847 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7848 }
7849 else
7850 {
7851 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7852 if (attr && cu->line_header)
7853 {
7854 unsigned int macro_offset = DW_UNSND (attr);
7855
7856 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7857 }
7858 }
7859
7860 do_cleanups (back_to);
7861 }
7862
7863 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7864 Create the set of symtabs used by this TU, or if this TU is sharing
7865 symtabs with another TU and the symtabs have already been created
7866 then restore those symtabs in the line header.
7867 We don't need the pc/line-number mapping for type units. */
7868
7869 static void
7870 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7871 {
7872 struct objfile *objfile = dwarf2_per_objfile->objfile;
7873 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7874 struct type_unit_group *tu_group;
7875 int first_time;
7876 struct line_header *lh;
7877 struct attribute *attr;
7878 unsigned int i, line_offset;
7879
7880 gdb_assert (per_cu->is_debug_types);
7881
7882 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7883
7884 /* If we're using .gdb_index (includes -readnow) then
7885 per_cu->s.type_unit_group may not have been set up yet. */
7886 if (per_cu->s.type_unit_group == NULL)
7887 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7888 tu_group = per_cu->s.type_unit_group;
7889
7890 /* If we've already processed this stmt_list there's no real need to
7891 do it again, we could fake it and just recreate the part we need
7892 (file name,index -> symtab mapping). If data shows this optimization
7893 is useful we can do it then. */
7894 first_time = tu_group->primary_symtab == NULL;
7895
7896 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7897 debug info. */
7898 lh = NULL;
7899 if (attr != NULL)
7900 {
7901 line_offset = DW_UNSND (attr);
7902 lh = dwarf_decode_line_header (line_offset, cu);
7903 }
7904 if (lh == NULL)
7905 {
7906 if (first_time)
7907 dwarf2_start_symtab (cu, "", NULL, 0);
7908 else
7909 {
7910 gdb_assert (tu_group->symtabs == NULL);
7911 restart_symtab (0);
7912 }
7913 /* Note: The primary symtab will get allocated at the end. */
7914 return;
7915 }
7916
7917 cu->line_header = lh;
7918 make_cleanup (free_cu_line_header, cu);
7919
7920 if (first_time)
7921 {
7922 dwarf2_start_symtab (cu, "", NULL, 0);
7923
7924 tu_group->num_symtabs = lh->num_file_names;
7925 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7926
7927 for (i = 0; i < lh->num_file_names; ++i)
7928 {
7929 char *dir = NULL;
7930 struct file_entry *fe = &lh->file_names[i];
7931
7932 if (fe->dir_index)
7933 dir = lh->include_dirs[fe->dir_index - 1];
7934 dwarf2_start_subfile (fe->name, dir, NULL);
7935
7936 /* Note: We don't have to watch for the main subfile here, type units
7937 don't have DW_AT_name. */
7938
7939 if (current_subfile->symtab == NULL)
7940 {
7941 /* NOTE: start_subfile will recognize when it's been passed
7942 a file it has already seen. So we can't assume there's a
7943 simple mapping from lh->file_names to subfiles,
7944 lh->file_names may contain dups. */
7945 current_subfile->symtab = allocate_symtab (current_subfile->name,
7946 objfile);
7947 }
7948
7949 fe->symtab = current_subfile->symtab;
7950 tu_group->symtabs[i] = fe->symtab;
7951 }
7952 }
7953 else
7954 {
7955 restart_symtab (0);
7956
7957 for (i = 0; i < lh->num_file_names; ++i)
7958 {
7959 struct file_entry *fe = &lh->file_names[i];
7960
7961 fe->symtab = tu_group->symtabs[i];
7962 }
7963 }
7964
7965 /* The main symtab is allocated last. Type units don't have DW_AT_name
7966 so they don't have a "real" (so to speak) symtab anyway.
7967 There is later code that will assign the main symtab to all symbols
7968 that don't have one. We need to handle the case of a symbol with a
7969 missing symtab (DW_AT_decl_file) anyway. */
7970 }
7971
7972 /* Process DW_TAG_type_unit.
7973 For TUs we want to skip the first top level sibling if it's not the
7974 actual type being defined by this TU. In this case the first top
7975 level sibling is there to provide context only. */
7976
7977 static void
7978 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7979 {
7980 struct die_info *child_die;
7981
7982 prepare_one_comp_unit (cu, die, language_minimal);
7983
7984 /* Initialize (or reinitialize) the machinery for building symtabs.
7985 We do this before processing child DIEs, so that the line header table
7986 is available for DW_AT_decl_file. */
7987 setup_type_unit_groups (die, cu);
7988
7989 if (die->child != NULL)
7990 {
7991 child_die = die->child;
7992 while (child_die && child_die->tag)
7993 {
7994 process_die (child_die, cu);
7995 child_die = sibling_die (child_die);
7996 }
7997 }
7998 }
7999 \f
8000 /* DWO files. */
8001
8002 static hashval_t
8003 hash_dwo_file (const void *item)
8004 {
8005 const struct dwo_file *dwo_file = item;
8006
8007 return htab_hash_string (dwo_file->dwo_name);
8008 }
8009
8010 static int
8011 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8012 {
8013 const struct dwo_file *lhs = item_lhs;
8014 const struct dwo_file *rhs = item_rhs;
8015
8016 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8017 }
8018
8019 /* Allocate a hash table for DWO files. */
8020
8021 static htab_t
8022 allocate_dwo_file_hash_table (void)
8023 {
8024 struct objfile *objfile = dwarf2_per_objfile->objfile;
8025
8026 return htab_create_alloc_ex (41,
8027 hash_dwo_file,
8028 eq_dwo_file,
8029 NULL,
8030 &objfile->objfile_obstack,
8031 hashtab_obstack_allocate,
8032 dummy_obstack_deallocate);
8033 }
8034
8035 static hashval_t
8036 hash_dwo_unit (const void *item)
8037 {
8038 const struct dwo_unit *dwo_unit = item;
8039
8040 /* This drops the top 32 bits of the id, but is ok for a hash. */
8041 return dwo_unit->signature;
8042 }
8043
8044 static int
8045 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8046 {
8047 const struct dwo_unit *lhs = item_lhs;
8048 const struct dwo_unit *rhs = item_rhs;
8049
8050 /* The signature is assumed to be unique within the DWO file.
8051 So while object file CU dwo_id's always have the value zero,
8052 that's OK, assuming each object file DWO file has only one CU,
8053 and that's the rule for now. */
8054 return lhs->signature == rhs->signature;
8055 }
8056
8057 /* Allocate a hash table for DWO CUs,TUs.
8058 There is one of these tables for each of CUs,TUs for each DWO file. */
8059
8060 static htab_t
8061 allocate_dwo_unit_table (struct objfile *objfile)
8062 {
8063 /* Start out with a pretty small number.
8064 Generally DWO files contain only one CU and maybe some TUs. */
8065 return htab_create_alloc_ex (3,
8066 hash_dwo_unit,
8067 eq_dwo_unit,
8068 NULL,
8069 &objfile->objfile_obstack,
8070 hashtab_obstack_allocate,
8071 dummy_obstack_deallocate);
8072 }
8073
8074 /* This function is mapped across the sections and remembers the offset and
8075 size of each of the DWO debugging sections we are interested in. */
8076
8077 static void
8078 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8079 {
8080 struct dwo_file *dwo_file = dwo_file_ptr;
8081 const struct dwo_section_names *names = &dwo_section_names;
8082
8083 if (section_is_p (sectp->name, &names->abbrev_dwo))
8084 {
8085 dwo_file->sections.abbrev.asection = sectp;
8086 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8087 }
8088 else if (section_is_p (sectp->name, &names->info_dwo))
8089 {
8090 dwo_file->sections.info.asection = sectp;
8091 dwo_file->sections.info.size = bfd_get_section_size (sectp);
8092 }
8093 else if (section_is_p (sectp->name, &names->line_dwo))
8094 {
8095 dwo_file->sections.line.asection = sectp;
8096 dwo_file->sections.line.size = bfd_get_section_size (sectp);
8097 }
8098 else if (section_is_p (sectp->name, &names->loc_dwo))
8099 {
8100 dwo_file->sections.loc.asection = sectp;
8101 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8102 }
8103 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8104 {
8105 dwo_file->sections.macinfo.asection = sectp;
8106 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8107 }
8108 else if (section_is_p (sectp->name, &names->macro_dwo))
8109 {
8110 dwo_file->sections.macro.asection = sectp;
8111 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8112 }
8113 else if (section_is_p (sectp->name, &names->str_dwo))
8114 {
8115 dwo_file->sections.str.asection = sectp;
8116 dwo_file->sections.str.size = bfd_get_section_size (sectp);
8117 }
8118 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8119 {
8120 dwo_file->sections.str_offsets.asection = sectp;
8121 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8122 }
8123 else if (section_is_p (sectp->name, &names->types_dwo))
8124 {
8125 struct dwarf2_section_info type_section;
8126
8127 memset (&type_section, 0, sizeof (type_section));
8128 type_section.asection = sectp;
8129 type_section.size = bfd_get_section_size (sectp);
8130 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8131 &type_section);
8132 }
8133 }
8134
8135 /* Structure used to pass data to create_debug_info_hash_table_reader. */
8136
8137 struct create_dwo_info_table_data
8138 {
8139 struct dwo_file *dwo_file;
8140 htab_t cu_htab;
8141 };
8142
8143 /* die_reader_func for create_debug_info_hash_table. */
8144
8145 static void
8146 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8147 gdb_byte *info_ptr,
8148 struct die_info *comp_unit_die,
8149 int has_children,
8150 void *datap)
8151 {
8152 struct dwarf2_cu *cu = reader->cu;
8153 struct objfile *objfile = dwarf2_per_objfile->objfile;
8154 sect_offset offset = cu->per_cu->offset;
8155 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8156 struct create_dwo_info_table_data *data = datap;
8157 struct dwo_file *dwo_file = data->dwo_file;
8158 htab_t cu_htab = data->cu_htab;
8159 void **slot;
8160 struct attribute *attr;
8161 struct dwo_unit *dwo_unit;
8162
8163 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8164 if (attr == NULL)
8165 {
8166 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8167 " its dwo_id [in module %s]"),
8168 offset.sect_off, dwo_file->dwo_name);
8169 return;
8170 }
8171
8172 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8173 dwo_unit->dwo_file = dwo_file;
8174 dwo_unit->signature = DW_UNSND (attr);
8175 dwo_unit->info_or_types_section = section;
8176 dwo_unit->offset = offset;
8177 dwo_unit->length = cu->per_cu->length;
8178
8179 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8180 gdb_assert (slot != NULL);
8181 if (*slot != NULL)
8182 {
8183 const struct dwo_unit *dup_dwo_unit = *slot;
8184
8185 complaint (&symfile_complaints,
8186 _("debug entry at offset 0x%x is duplicate to the entry at"
8187 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8188 offset.sect_off, dup_dwo_unit->offset.sect_off,
8189 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8190 dwo_file->dwo_name);
8191 }
8192 else
8193 *slot = dwo_unit;
8194
8195 if (dwarf2_read_debug)
8196 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8197 offset.sect_off,
8198 phex (dwo_unit->signature,
8199 sizeof (dwo_unit->signature)));
8200 }
8201
8202 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
8203
8204 static htab_t
8205 create_debug_info_hash_table (struct dwo_file *dwo_file)
8206 {
8207 struct objfile *objfile = dwarf2_per_objfile->objfile;
8208 struct dwarf2_section_info *section = &dwo_file->sections.info;
8209 bfd *abfd;
8210 htab_t cu_htab;
8211 gdb_byte *info_ptr, *end_ptr;
8212 struct create_dwo_info_table_data create_dwo_info_table_data;
8213
8214 dwarf2_read_section (objfile, section);
8215 info_ptr = section->buffer;
8216
8217 if (info_ptr == NULL)
8218 return NULL;
8219
8220 /* We can't set abfd until now because the section may be empty or
8221 not present, in which case section->asection will be NULL. */
8222 abfd = section->asection->owner;
8223
8224 if (dwarf2_read_debug)
8225 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8226 bfd_get_filename (abfd));
8227
8228 cu_htab = allocate_dwo_unit_table (objfile);
8229
8230 create_dwo_info_table_data.dwo_file = dwo_file;
8231 create_dwo_info_table_data.cu_htab = cu_htab;
8232
8233 end_ptr = info_ptr + section->size;
8234 while (info_ptr < end_ptr)
8235 {
8236 struct dwarf2_per_cu_data per_cu;
8237
8238 memset (&per_cu, 0, sizeof (per_cu));
8239 per_cu.objfile = objfile;
8240 per_cu.is_debug_types = 0;
8241 per_cu.offset.sect_off = info_ptr - section->buffer;
8242 per_cu.info_or_types_section = section;
8243
8244 init_cutu_and_read_dies_no_follow (&per_cu,
8245 &dwo_file->sections.abbrev,
8246 dwo_file,
8247 create_debug_info_hash_table_reader,
8248 &create_dwo_info_table_data);
8249
8250 info_ptr += per_cu.length;
8251 }
8252
8253 return cu_htab;
8254 }
8255
8256 /* Subroutine of open_dwo_file to simplify it.
8257 Open the file specified by FILE_NAME and hand it off to BFD for
8258 preliminary analysis. Return a newly initialized bfd *, which
8259 includes a canonicalized copy of FILE_NAME.
8260 In case of trouble, return NULL.
8261 NOTE: This function is derived from symfile_bfd_open. */
8262
8263 static bfd *
8264 try_open_dwo_file (const char *file_name)
8265 {
8266 bfd *sym_bfd;
8267 int desc;
8268 char *absolute_name;
8269
8270 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8271 O_RDONLY | O_BINARY, &absolute_name);
8272 if (desc < 0)
8273 return NULL;
8274
8275 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8276 if (!sym_bfd)
8277 {
8278 xfree (absolute_name);
8279 return NULL;
8280 }
8281 xfree (absolute_name);
8282 bfd_set_cacheable (sym_bfd, 1);
8283
8284 if (!bfd_check_format (sym_bfd, bfd_object))
8285 {
8286 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8287 return NULL;
8288 }
8289
8290 return sym_bfd;
8291 }
8292
8293 /* Try to open DWO file DWO_NAME.
8294 COMP_DIR is the DW_AT_comp_dir attribute.
8295 The result is the bfd handle of the file.
8296 If there is a problem finding or opening the file, return NULL.
8297 Upon success, the canonicalized path of the file is stored in the bfd,
8298 same as symfile_bfd_open. */
8299
8300 static bfd *
8301 open_dwo_file (const char *dwo_name, const char *comp_dir)
8302 {
8303 bfd *abfd;
8304
8305 if (IS_ABSOLUTE_PATH (dwo_name))
8306 return try_open_dwo_file (dwo_name);
8307
8308 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8309
8310 if (comp_dir != NULL)
8311 {
8312 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8313
8314 /* NOTE: If comp_dir is a relative path, this will also try the
8315 search path, which seems useful. */
8316 abfd = try_open_dwo_file (path_to_try);
8317 xfree (path_to_try);
8318 if (abfd != NULL)
8319 return abfd;
8320 }
8321
8322 /* That didn't work, try debug-file-directory, which, despite its name,
8323 is a list of paths. */
8324
8325 if (*debug_file_directory == '\0')
8326 return NULL;
8327
8328 return try_open_dwo_file (dwo_name);
8329 }
8330
8331 /* Initialize the use of the DWO file specified by DWO_NAME. */
8332
8333 static struct dwo_file *
8334 init_dwo_file (const char *dwo_name, const char *comp_dir)
8335 {
8336 struct objfile *objfile = dwarf2_per_objfile->objfile;
8337 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8338 struct dwo_file);
8339 bfd *abfd;
8340 struct cleanup *cleanups;
8341
8342 if (dwarf2_read_debug)
8343 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8344
8345 abfd = open_dwo_file (dwo_name, comp_dir);
8346 if (abfd == NULL)
8347 return NULL;
8348 dwo_file->dwo_name = dwo_name;
8349 dwo_file->dwo_bfd = abfd;
8350
8351 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8352
8353 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8354
8355 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8356
8357 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8358 dwo_file->sections.types);
8359
8360 discard_cleanups (cleanups);
8361
8362 return dwo_file;
8363 }
8364
8365 /* Lookup DWO file DWO_NAME. */
8366
8367 static struct dwo_file *
8368 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8369 {
8370 struct dwo_file *dwo_file;
8371 struct dwo_file find_entry;
8372 void **slot;
8373
8374 if (dwarf2_per_objfile->dwo_files == NULL)
8375 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8376
8377 /* Have we already seen this DWO file? */
8378 find_entry.dwo_name = dwo_name;
8379 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8380
8381 /* If not, read it in and build a table of the DWOs it contains. */
8382 if (*slot == NULL)
8383 *slot = init_dwo_file (dwo_name, comp_dir);
8384
8385 /* NOTE: This will be NULL if unable to open the file. */
8386 dwo_file = *slot;
8387
8388 return dwo_file;
8389 }
8390
8391 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8392 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8393 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8394 nomenclature as TUs).
8395 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8396 (dwo_id mismatch or couldn't find the DWO file). */
8397
8398 static struct dwo_unit *
8399 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8400 char *dwo_name, const char *comp_dir,
8401 ULONGEST signature)
8402 {
8403 struct objfile *objfile = dwarf2_per_objfile->objfile;
8404 struct dwo_file *dwo_file;
8405
8406 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8407 if (dwo_file == NULL)
8408 return NULL;
8409
8410 /* Look up the DWO using its signature(dwo_id). */
8411
8412 if (dwo_file->cus != NULL)
8413 {
8414 struct dwo_unit find_dwo_cu, *dwo_cu;
8415
8416 find_dwo_cu.signature = signature;
8417 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8418
8419 if (dwo_cu != NULL)
8420 return dwo_cu;
8421 }
8422
8423 /* We didn't find it. This must mean a dwo_id mismatch. */
8424
8425 complaint (&symfile_complaints,
8426 _("Could not find DWO CU referenced by CU at offset 0x%x"
8427 " [in module %s]"),
8428 this_cu->offset.sect_off, objfile->name);
8429 return NULL;
8430 }
8431
8432 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8433 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8434 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8435 (dwo_id mismatch or couldn't find the DWO file). */
8436
8437 static struct dwo_unit *
8438 lookup_dwo_type_unit (struct signatured_type *this_tu,
8439 char *dwo_name, const char *comp_dir)
8440 {
8441 struct objfile *objfile = dwarf2_per_objfile->objfile;
8442 struct dwo_file *dwo_file;
8443
8444 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8445 if (dwo_file == NULL)
8446 return NULL;
8447
8448 /* Look up the DWO using its signature(dwo_id). */
8449
8450 if (dwo_file->tus != NULL)
8451 {
8452 struct dwo_unit find_dwo_tu, *dwo_tu;
8453
8454 find_dwo_tu.signature = this_tu->signature;
8455 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8456
8457 if (dwo_tu != NULL)
8458 return dwo_tu;
8459 }
8460
8461 /* We didn't find it. This must mean a dwo_id mismatch. */
8462
8463 complaint (&symfile_complaints,
8464 _("Could not find DWO TU referenced by TU at offset 0x%x"
8465 " [in module %s]"),
8466 this_tu->per_cu.offset.sect_off, objfile->name);
8467 return NULL;
8468 }
8469
8470 /* Free all resources associated with DWO_FILE.
8471 Close the DWO file and munmap the sections.
8472 All memory should be on the objfile obstack. */
8473
8474 static void
8475 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8476 {
8477 int ix;
8478 struct dwarf2_section_info *section;
8479
8480 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8481 gdb_bfd_unref (dwo_file->dwo_bfd);
8482
8483 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8484 }
8485
8486 /* Wrapper for free_dwo_file for use in cleanups. */
8487
8488 static void
8489 free_dwo_file_cleanup (void *arg)
8490 {
8491 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8492 struct objfile *objfile = dwarf2_per_objfile->objfile;
8493
8494 free_dwo_file (dwo_file, objfile);
8495 }
8496
8497 /* Traversal function for free_dwo_files. */
8498
8499 static int
8500 free_dwo_file_from_slot (void **slot, void *info)
8501 {
8502 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8503 struct objfile *objfile = (struct objfile *) info;
8504
8505 free_dwo_file (dwo_file, objfile);
8506
8507 return 1;
8508 }
8509
8510 /* Free all resources associated with DWO_FILES. */
8511
8512 static void
8513 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8514 {
8515 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8516 }
8517 \f
8518 /* Read in various DIEs. */
8519
8520 /* qsort helper for inherit_abstract_dies. */
8521
8522 static int
8523 unsigned_int_compar (const void *ap, const void *bp)
8524 {
8525 unsigned int a = *(unsigned int *) ap;
8526 unsigned int b = *(unsigned int *) bp;
8527
8528 return (a > b) - (b > a);
8529 }
8530
8531 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8532 Inherit only the children of the DW_AT_abstract_origin DIE not being
8533 already referenced by DW_AT_abstract_origin from the children of the
8534 current DIE. */
8535
8536 static void
8537 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8538 {
8539 struct die_info *child_die;
8540 unsigned die_children_count;
8541 /* CU offsets which were referenced by children of the current DIE. */
8542 sect_offset *offsets;
8543 sect_offset *offsets_end, *offsetp;
8544 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8545 struct die_info *origin_die;
8546 /* Iterator of the ORIGIN_DIE children. */
8547 struct die_info *origin_child_die;
8548 struct cleanup *cleanups;
8549 struct attribute *attr;
8550 struct dwarf2_cu *origin_cu;
8551 struct pending **origin_previous_list_in_scope;
8552
8553 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8554 if (!attr)
8555 return;
8556
8557 /* Note that following die references may follow to a die in a
8558 different cu. */
8559
8560 origin_cu = cu;
8561 origin_die = follow_die_ref (die, attr, &origin_cu);
8562
8563 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8564 symbols in. */
8565 origin_previous_list_in_scope = origin_cu->list_in_scope;
8566 origin_cu->list_in_scope = cu->list_in_scope;
8567
8568 if (die->tag != origin_die->tag
8569 && !(die->tag == DW_TAG_inlined_subroutine
8570 && origin_die->tag == DW_TAG_subprogram))
8571 complaint (&symfile_complaints,
8572 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8573 die->offset.sect_off, origin_die->offset.sect_off);
8574
8575 child_die = die->child;
8576 die_children_count = 0;
8577 while (child_die && child_die->tag)
8578 {
8579 child_die = sibling_die (child_die);
8580 die_children_count++;
8581 }
8582 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8583 cleanups = make_cleanup (xfree, offsets);
8584
8585 offsets_end = offsets;
8586 child_die = die->child;
8587 while (child_die && child_die->tag)
8588 {
8589 /* For each CHILD_DIE, find the corresponding child of
8590 ORIGIN_DIE. If there is more than one layer of
8591 DW_AT_abstract_origin, follow them all; there shouldn't be,
8592 but GCC versions at least through 4.4 generate this (GCC PR
8593 40573). */
8594 struct die_info *child_origin_die = child_die;
8595 struct dwarf2_cu *child_origin_cu = cu;
8596
8597 while (1)
8598 {
8599 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8600 child_origin_cu);
8601 if (attr == NULL)
8602 break;
8603 child_origin_die = follow_die_ref (child_origin_die, attr,
8604 &child_origin_cu);
8605 }
8606
8607 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8608 counterpart may exist. */
8609 if (child_origin_die != child_die)
8610 {
8611 if (child_die->tag != child_origin_die->tag
8612 && !(child_die->tag == DW_TAG_inlined_subroutine
8613 && child_origin_die->tag == DW_TAG_subprogram))
8614 complaint (&symfile_complaints,
8615 _("Child DIE 0x%x and its abstract origin 0x%x have "
8616 "different tags"), child_die->offset.sect_off,
8617 child_origin_die->offset.sect_off);
8618 if (child_origin_die->parent != origin_die)
8619 complaint (&symfile_complaints,
8620 _("Child DIE 0x%x and its abstract origin 0x%x have "
8621 "different parents"), child_die->offset.sect_off,
8622 child_origin_die->offset.sect_off);
8623 else
8624 *offsets_end++ = child_origin_die->offset;
8625 }
8626 child_die = sibling_die (child_die);
8627 }
8628 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8629 unsigned_int_compar);
8630 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8631 if (offsetp[-1].sect_off == offsetp->sect_off)
8632 complaint (&symfile_complaints,
8633 _("Multiple children of DIE 0x%x refer "
8634 "to DIE 0x%x as their abstract origin"),
8635 die->offset.sect_off, offsetp->sect_off);
8636
8637 offsetp = offsets;
8638 origin_child_die = origin_die->child;
8639 while (origin_child_die && origin_child_die->tag)
8640 {
8641 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
8642 while (offsetp < offsets_end
8643 && offsetp->sect_off < origin_child_die->offset.sect_off)
8644 offsetp++;
8645 if (offsetp >= offsets_end
8646 || offsetp->sect_off > origin_child_die->offset.sect_off)
8647 {
8648 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
8649 process_die (origin_child_die, origin_cu);
8650 }
8651 origin_child_die = sibling_die (origin_child_die);
8652 }
8653 origin_cu->list_in_scope = origin_previous_list_in_scope;
8654
8655 do_cleanups (cleanups);
8656 }
8657
8658 static void
8659 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8660 {
8661 struct objfile *objfile = cu->objfile;
8662 struct context_stack *new;
8663 CORE_ADDR lowpc;
8664 CORE_ADDR highpc;
8665 struct die_info *child_die;
8666 struct attribute *attr, *call_line, *call_file;
8667 char *name;
8668 CORE_ADDR baseaddr;
8669 struct block *block;
8670 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8671 VEC (symbolp) *template_args = NULL;
8672 struct template_symbol *templ_func = NULL;
8673
8674 if (inlined_func)
8675 {
8676 /* If we do not have call site information, we can't show the
8677 caller of this inlined function. That's too confusing, so
8678 only use the scope for local variables. */
8679 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8680 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8681 if (call_line == NULL || call_file == NULL)
8682 {
8683 read_lexical_block_scope (die, cu);
8684 return;
8685 }
8686 }
8687
8688 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8689
8690 name = dwarf2_name (die, cu);
8691
8692 /* Ignore functions with missing or empty names. These are actually
8693 illegal according to the DWARF standard. */
8694 if (name == NULL)
8695 {
8696 complaint (&symfile_complaints,
8697 _("missing name for subprogram DIE at %d"),
8698 die->offset.sect_off);
8699 return;
8700 }
8701
8702 /* Ignore functions with missing or invalid low and high pc attributes. */
8703 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8704 {
8705 attr = dwarf2_attr (die, DW_AT_external, cu);
8706 if (!attr || !DW_UNSND (attr))
8707 complaint (&symfile_complaints,
8708 _("cannot get low and high bounds "
8709 "for subprogram DIE at %d"),
8710 die->offset.sect_off);
8711 return;
8712 }
8713
8714 lowpc += baseaddr;
8715 highpc += baseaddr;
8716
8717 /* If we have any template arguments, then we must allocate a
8718 different sort of symbol. */
8719 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8720 {
8721 if (child_die->tag == DW_TAG_template_type_param
8722 || child_die->tag == DW_TAG_template_value_param)
8723 {
8724 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8725 struct template_symbol);
8726 templ_func->base.is_cplus_template_function = 1;
8727 break;
8728 }
8729 }
8730
8731 new = push_context (0, lowpc);
8732 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8733 (struct symbol *) templ_func);
8734
8735 /* If there is a location expression for DW_AT_frame_base, record
8736 it. */
8737 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8738 if (attr)
8739 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8740 expression is being recorded directly in the function's symbol
8741 and not in a separate frame-base object. I guess this hack is
8742 to avoid adding some sort of frame-base adjunct/annex to the
8743 function's symbol :-(. The problem with doing this is that it
8744 results in a function symbol with a location expression that
8745 has nothing to do with the location of the function, ouch! The
8746 relationship should be: a function's symbol has-a frame base; a
8747 frame-base has-a location expression. */
8748 dwarf2_symbol_mark_computed (attr, new->name, cu);
8749
8750 cu->list_in_scope = &local_symbols;
8751
8752 if (die->child != NULL)
8753 {
8754 child_die = die->child;
8755 while (child_die && child_die->tag)
8756 {
8757 if (child_die->tag == DW_TAG_template_type_param
8758 || child_die->tag == DW_TAG_template_value_param)
8759 {
8760 struct symbol *arg = new_symbol (child_die, NULL, cu);
8761
8762 if (arg != NULL)
8763 VEC_safe_push (symbolp, template_args, arg);
8764 }
8765 else
8766 process_die (child_die, cu);
8767 child_die = sibling_die (child_die);
8768 }
8769 }
8770
8771 inherit_abstract_dies (die, cu);
8772
8773 /* If we have a DW_AT_specification, we might need to import using
8774 directives from the context of the specification DIE. See the
8775 comment in determine_prefix. */
8776 if (cu->language == language_cplus
8777 && dwarf2_attr (die, DW_AT_specification, cu))
8778 {
8779 struct dwarf2_cu *spec_cu = cu;
8780 struct die_info *spec_die = die_specification (die, &spec_cu);
8781
8782 while (spec_die)
8783 {
8784 child_die = spec_die->child;
8785 while (child_die && child_die->tag)
8786 {
8787 if (child_die->tag == DW_TAG_imported_module)
8788 process_die (child_die, spec_cu);
8789 child_die = sibling_die (child_die);
8790 }
8791
8792 /* In some cases, GCC generates specification DIEs that
8793 themselves contain DW_AT_specification attributes. */
8794 spec_die = die_specification (spec_die, &spec_cu);
8795 }
8796 }
8797
8798 new = pop_context ();
8799 /* Make a block for the local symbols within. */
8800 block = finish_block (new->name, &local_symbols, new->old_blocks,
8801 lowpc, highpc, objfile);
8802
8803 /* For C++, set the block's scope. */
8804 if (cu->language == language_cplus || cu->language == language_fortran)
8805 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8806 determine_prefix (die, cu),
8807 processing_has_namespace_info);
8808
8809 /* If we have address ranges, record them. */
8810 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8811
8812 /* Attach template arguments to function. */
8813 if (! VEC_empty (symbolp, template_args))
8814 {
8815 gdb_assert (templ_func != NULL);
8816
8817 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8818 templ_func->template_arguments
8819 = obstack_alloc (&objfile->objfile_obstack,
8820 (templ_func->n_template_arguments
8821 * sizeof (struct symbol *)));
8822 memcpy (templ_func->template_arguments,
8823 VEC_address (symbolp, template_args),
8824 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8825 VEC_free (symbolp, template_args);
8826 }
8827
8828 /* In C++, we can have functions nested inside functions (e.g., when
8829 a function declares a class that has methods). This means that
8830 when we finish processing a function scope, we may need to go
8831 back to building a containing block's symbol lists. */
8832 local_symbols = new->locals;
8833 param_symbols = new->params;
8834 using_directives = new->using_directives;
8835
8836 /* If we've finished processing a top-level function, subsequent
8837 symbols go in the file symbol list. */
8838 if (outermost_context_p ())
8839 cu->list_in_scope = &file_symbols;
8840 }
8841
8842 /* Process all the DIES contained within a lexical block scope. Start
8843 a new scope, process the dies, and then close the scope. */
8844
8845 static void
8846 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8847 {
8848 struct objfile *objfile = cu->objfile;
8849 struct context_stack *new;
8850 CORE_ADDR lowpc, highpc;
8851 struct die_info *child_die;
8852 CORE_ADDR baseaddr;
8853
8854 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8855
8856 /* Ignore blocks with missing or invalid low and high pc attributes. */
8857 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8858 as multiple lexical blocks? Handling children in a sane way would
8859 be nasty. Might be easier to properly extend generic blocks to
8860 describe ranges. */
8861 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8862 return;
8863 lowpc += baseaddr;
8864 highpc += baseaddr;
8865
8866 push_context (0, lowpc);
8867 if (die->child != NULL)
8868 {
8869 child_die = die->child;
8870 while (child_die && child_die->tag)
8871 {
8872 process_die (child_die, cu);
8873 child_die = sibling_die (child_die);
8874 }
8875 }
8876 new = pop_context ();
8877
8878 if (local_symbols != NULL || using_directives != NULL)
8879 {
8880 struct block *block
8881 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8882 highpc, objfile);
8883
8884 /* Note that recording ranges after traversing children, as we
8885 do here, means that recording a parent's ranges entails
8886 walking across all its children's ranges as they appear in
8887 the address map, which is quadratic behavior.
8888
8889 It would be nicer to record the parent's ranges before
8890 traversing its children, simply overriding whatever you find
8891 there. But since we don't even decide whether to create a
8892 block until after we've traversed its children, that's hard
8893 to do. */
8894 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8895 }
8896 local_symbols = new->locals;
8897 using_directives = new->using_directives;
8898 }
8899
8900 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8901
8902 static void
8903 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8904 {
8905 struct objfile *objfile = cu->objfile;
8906 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8907 CORE_ADDR pc, baseaddr;
8908 struct attribute *attr;
8909 struct call_site *call_site, call_site_local;
8910 void **slot;
8911 int nparams;
8912 struct die_info *child_die;
8913
8914 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8915
8916 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8917 if (!attr)
8918 {
8919 complaint (&symfile_complaints,
8920 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8921 "DIE 0x%x [in module %s]"),
8922 die->offset.sect_off, objfile->name);
8923 return;
8924 }
8925 pc = DW_ADDR (attr) + baseaddr;
8926
8927 if (cu->call_site_htab == NULL)
8928 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8929 NULL, &objfile->objfile_obstack,
8930 hashtab_obstack_allocate, NULL);
8931 call_site_local.pc = pc;
8932 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8933 if (*slot != NULL)
8934 {
8935 complaint (&symfile_complaints,
8936 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8937 "DIE 0x%x [in module %s]"),
8938 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8939 return;
8940 }
8941
8942 /* Count parameters at the caller. */
8943
8944 nparams = 0;
8945 for (child_die = die->child; child_die && child_die->tag;
8946 child_die = sibling_die (child_die))
8947 {
8948 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8949 {
8950 complaint (&symfile_complaints,
8951 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8952 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8953 child_die->tag, child_die->offset.sect_off, objfile->name);
8954 continue;
8955 }
8956
8957 nparams++;
8958 }
8959
8960 call_site = obstack_alloc (&objfile->objfile_obstack,
8961 (sizeof (*call_site)
8962 + (sizeof (*call_site->parameter)
8963 * (nparams - 1))));
8964 *slot = call_site;
8965 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8966 call_site->pc = pc;
8967
8968 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8969 {
8970 struct die_info *func_die;
8971
8972 /* Skip also over DW_TAG_inlined_subroutine. */
8973 for (func_die = die->parent;
8974 func_die && func_die->tag != DW_TAG_subprogram
8975 && func_die->tag != DW_TAG_subroutine_type;
8976 func_die = func_die->parent);
8977
8978 /* DW_AT_GNU_all_call_sites is a superset
8979 of DW_AT_GNU_all_tail_call_sites. */
8980 if (func_die
8981 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8982 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8983 {
8984 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8985 not complete. But keep CALL_SITE for look ups via call_site_htab,
8986 both the initial caller containing the real return address PC and
8987 the final callee containing the current PC of a chain of tail
8988 calls do not need to have the tail call list complete. But any
8989 function candidate for a virtual tail call frame searched via
8990 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8991 determined unambiguously. */
8992 }
8993 else
8994 {
8995 struct type *func_type = NULL;
8996
8997 if (func_die)
8998 func_type = get_die_type (func_die, cu);
8999 if (func_type != NULL)
9000 {
9001 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9002
9003 /* Enlist this call site to the function. */
9004 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9005 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9006 }
9007 else
9008 complaint (&symfile_complaints,
9009 _("Cannot find function owning DW_TAG_GNU_call_site "
9010 "DIE 0x%x [in module %s]"),
9011 die->offset.sect_off, objfile->name);
9012 }
9013 }
9014
9015 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9016 if (attr == NULL)
9017 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9018 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9019 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9020 /* Keep NULL DWARF_BLOCK. */;
9021 else if (attr_form_is_block (attr))
9022 {
9023 struct dwarf2_locexpr_baton *dlbaton;
9024
9025 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9026 dlbaton->data = DW_BLOCK (attr)->data;
9027 dlbaton->size = DW_BLOCK (attr)->size;
9028 dlbaton->per_cu = cu->per_cu;
9029
9030 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9031 }
9032 else if (is_ref_attr (attr))
9033 {
9034 struct dwarf2_cu *target_cu = cu;
9035 struct die_info *target_die;
9036
9037 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9038 gdb_assert (target_cu->objfile == objfile);
9039 if (die_is_declaration (target_die, target_cu))
9040 {
9041 const char *target_physname;
9042
9043 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9044 if (target_physname == NULL)
9045 complaint (&symfile_complaints,
9046 _("DW_AT_GNU_call_site_target target DIE has invalid "
9047 "physname, for referencing DIE 0x%x [in module %s]"),
9048 die->offset.sect_off, objfile->name);
9049 else
9050 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9051 }
9052 else
9053 {
9054 CORE_ADDR lowpc;
9055
9056 /* DW_AT_entry_pc should be preferred. */
9057 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9058 complaint (&symfile_complaints,
9059 _("DW_AT_GNU_call_site_target target DIE has invalid "
9060 "low pc, for referencing DIE 0x%x [in module %s]"),
9061 die->offset.sect_off, objfile->name);
9062 else
9063 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9064 }
9065 }
9066 else
9067 complaint (&symfile_complaints,
9068 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9069 "block nor reference, for DIE 0x%x [in module %s]"),
9070 die->offset.sect_off, objfile->name);
9071
9072 call_site->per_cu = cu->per_cu;
9073
9074 for (child_die = die->child;
9075 child_die && child_die->tag;
9076 child_die = sibling_die (child_die))
9077 {
9078 struct call_site_parameter *parameter;
9079 struct attribute *loc, *origin;
9080
9081 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9082 {
9083 /* Already printed the complaint above. */
9084 continue;
9085 }
9086
9087 gdb_assert (call_site->parameter_count < nparams);
9088 parameter = &call_site->parameter[call_site->parameter_count];
9089
9090 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9091 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9092 register is contained in DW_AT_GNU_call_site_value. */
9093
9094 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9095 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9096 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9097 {
9098 sect_offset offset;
9099
9100 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9101 offset = dwarf2_get_ref_die_offset (origin);
9102 if (!offset_in_cu_p (&cu->header, offset))
9103 {
9104 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9105 binding can be done only inside one CU. Such referenced DIE
9106 therefore cannot be even moved to DW_TAG_partial_unit. */
9107 complaint (&symfile_complaints,
9108 _("DW_AT_abstract_origin offset is not in CU for "
9109 "DW_TAG_GNU_call_site child DIE 0x%x "
9110 "[in module %s]"),
9111 child_die->offset.sect_off, objfile->name);
9112 continue;
9113 }
9114 parameter->u.param_offset.cu_off = (offset.sect_off
9115 - cu->header.offset.sect_off);
9116 }
9117 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9118 {
9119 complaint (&symfile_complaints,
9120 _("No DW_FORM_block* DW_AT_location for "
9121 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9122 child_die->offset.sect_off, objfile->name);
9123 continue;
9124 }
9125 else
9126 {
9127 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9128 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9129 if (parameter->u.dwarf_reg != -1)
9130 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9131 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9132 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9133 &parameter->u.fb_offset))
9134 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9135 else
9136 {
9137 complaint (&symfile_complaints,
9138 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9139 "for DW_FORM_block* DW_AT_location is supported for "
9140 "DW_TAG_GNU_call_site child DIE 0x%x "
9141 "[in module %s]"),
9142 child_die->offset.sect_off, objfile->name);
9143 continue;
9144 }
9145 }
9146
9147 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9148 if (!attr_form_is_block (attr))
9149 {
9150 complaint (&symfile_complaints,
9151 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9152 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9153 child_die->offset.sect_off, objfile->name);
9154 continue;
9155 }
9156 parameter->value = DW_BLOCK (attr)->data;
9157 parameter->value_size = DW_BLOCK (attr)->size;
9158
9159 /* Parameters are not pre-cleared by memset above. */
9160 parameter->data_value = NULL;
9161 parameter->data_value_size = 0;
9162 call_site->parameter_count++;
9163
9164 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9165 if (attr)
9166 {
9167 if (!attr_form_is_block (attr))
9168 complaint (&symfile_complaints,
9169 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9170 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9171 child_die->offset.sect_off, objfile->name);
9172 else
9173 {
9174 parameter->data_value = DW_BLOCK (attr)->data;
9175 parameter->data_value_size = DW_BLOCK (attr)->size;
9176 }
9177 }
9178 }
9179 }
9180
9181 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9182 Return 1 if the attributes are present and valid, otherwise, return 0.
9183 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9184
9185 static int
9186 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9187 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9188 struct partial_symtab *ranges_pst)
9189 {
9190 struct objfile *objfile = cu->objfile;
9191 struct comp_unit_head *cu_header = &cu->header;
9192 bfd *obfd = objfile->obfd;
9193 unsigned int addr_size = cu_header->addr_size;
9194 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9195 /* Base address selection entry. */
9196 CORE_ADDR base;
9197 int found_base;
9198 unsigned int dummy;
9199 gdb_byte *buffer;
9200 CORE_ADDR marker;
9201 int low_set;
9202 CORE_ADDR low = 0;
9203 CORE_ADDR high = 0;
9204 CORE_ADDR baseaddr;
9205
9206 found_base = cu->base_known;
9207 base = cu->base_address;
9208
9209 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9210 if (offset >= dwarf2_per_objfile->ranges.size)
9211 {
9212 complaint (&symfile_complaints,
9213 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9214 offset);
9215 return 0;
9216 }
9217 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9218
9219 /* Read in the largest possible address. */
9220 marker = read_address (obfd, buffer, cu, &dummy);
9221 if ((marker & mask) == mask)
9222 {
9223 /* If we found the largest possible address, then
9224 read the base address. */
9225 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9226 buffer += 2 * addr_size;
9227 offset += 2 * addr_size;
9228 found_base = 1;
9229 }
9230
9231 low_set = 0;
9232
9233 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9234
9235 while (1)
9236 {
9237 CORE_ADDR range_beginning, range_end;
9238
9239 range_beginning = read_address (obfd, buffer, cu, &dummy);
9240 buffer += addr_size;
9241 range_end = read_address (obfd, buffer, cu, &dummy);
9242 buffer += addr_size;
9243 offset += 2 * addr_size;
9244
9245 /* An end of list marker is a pair of zero addresses. */
9246 if (range_beginning == 0 && range_end == 0)
9247 /* Found the end of list entry. */
9248 break;
9249
9250 /* Each base address selection entry is a pair of 2 values.
9251 The first is the largest possible address, the second is
9252 the base address. Check for a base address here. */
9253 if ((range_beginning & mask) == mask)
9254 {
9255 /* If we found the largest possible address, then
9256 read the base address. */
9257 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9258 found_base = 1;
9259 continue;
9260 }
9261
9262 if (!found_base)
9263 {
9264 /* We have no valid base address for the ranges
9265 data. */
9266 complaint (&symfile_complaints,
9267 _("Invalid .debug_ranges data (no base address)"));
9268 return 0;
9269 }
9270
9271 if (range_beginning > range_end)
9272 {
9273 /* Inverted range entries are invalid. */
9274 complaint (&symfile_complaints,
9275 _("Invalid .debug_ranges data (inverted range)"));
9276 return 0;
9277 }
9278
9279 /* Empty range entries have no effect. */
9280 if (range_beginning == range_end)
9281 continue;
9282
9283 range_beginning += base;
9284 range_end += base;
9285
9286 /* A not-uncommon case of bad debug info.
9287 Don't pollute the addrmap with bad data. */
9288 if (range_beginning + baseaddr == 0
9289 && !dwarf2_per_objfile->has_section_at_zero)
9290 {
9291 complaint (&symfile_complaints,
9292 _(".debug_ranges entry has start address of zero"
9293 " [in module %s]"), objfile->name);
9294 continue;
9295 }
9296
9297 if (ranges_pst != NULL)
9298 addrmap_set_empty (objfile->psymtabs_addrmap,
9299 range_beginning + baseaddr,
9300 range_end - 1 + baseaddr,
9301 ranges_pst);
9302
9303 /* FIXME: This is recording everything as a low-high
9304 segment of consecutive addresses. We should have a
9305 data structure for discontiguous block ranges
9306 instead. */
9307 if (! low_set)
9308 {
9309 low = range_beginning;
9310 high = range_end;
9311 low_set = 1;
9312 }
9313 else
9314 {
9315 if (range_beginning < low)
9316 low = range_beginning;
9317 if (range_end > high)
9318 high = range_end;
9319 }
9320 }
9321
9322 if (! low_set)
9323 /* If the first entry is an end-of-list marker, the range
9324 describes an empty scope, i.e. no instructions. */
9325 return 0;
9326
9327 if (low_return)
9328 *low_return = low;
9329 if (high_return)
9330 *high_return = high;
9331 return 1;
9332 }
9333
9334 /* Get low and high pc attributes from a die. Return 1 if the attributes
9335 are present and valid, otherwise, return 0. Return -1 if the range is
9336 discontinuous, i.e. derived from DW_AT_ranges information. */
9337
9338 static int
9339 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9340 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9341 struct partial_symtab *pst)
9342 {
9343 struct attribute *attr;
9344 struct attribute *attr_high;
9345 CORE_ADDR low = 0;
9346 CORE_ADDR high = 0;
9347 int ret = 0;
9348
9349 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9350 if (attr_high)
9351 {
9352 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9353 if (attr)
9354 {
9355 low = DW_ADDR (attr);
9356 if (attr_high->form == DW_FORM_addr
9357 || attr_high->form == DW_FORM_GNU_addr_index)
9358 high = DW_ADDR (attr_high);
9359 else
9360 high = low + DW_UNSND (attr_high);
9361 }
9362 else
9363 /* Found high w/o low attribute. */
9364 return 0;
9365
9366 /* Found consecutive range of addresses. */
9367 ret = 1;
9368 }
9369 else
9370 {
9371 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9372 if (attr != NULL)
9373 {
9374 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9375
9376 /* Value of the DW_AT_ranges attribute is the offset in the
9377 .debug_ranges section. */
9378 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9379 return 0;
9380 /* Found discontinuous range of addresses. */
9381 ret = -1;
9382 }
9383 }
9384
9385 /* read_partial_die has also the strict LOW < HIGH requirement. */
9386 if (high <= low)
9387 return 0;
9388
9389 /* When using the GNU linker, .gnu.linkonce. sections are used to
9390 eliminate duplicate copies of functions and vtables and such.
9391 The linker will arbitrarily choose one and discard the others.
9392 The AT_*_pc values for such functions refer to local labels in
9393 these sections. If the section from that file was discarded, the
9394 labels are not in the output, so the relocs get a value of 0.
9395 If this is a discarded function, mark the pc bounds as invalid,
9396 so that GDB will ignore it. */
9397 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9398 return 0;
9399
9400 *lowpc = low;
9401 if (highpc)
9402 *highpc = high;
9403 return ret;
9404 }
9405
9406 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9407 its low and high PC addresses. Do nothing if these addresses could not
9408 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9409 and HIGHPC to the high address if greater than HIGHPC. */
9410
9411 static void
9412 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9413 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9414 struct dwarf2_cu *cu)
9415 {
9416 CORE_ADDR low, high;
9417 struct die_info *child = die->child;
9418
9419 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9420 {
9421 *lowpc = min (*lowpc, low);
9422 *highpc = max (*highpc, high);
9423 }
9424
9425 /* If the language does not allow nested subprograms (either inside
9426 subprograms or lexical blocks), we're done. */
9427 if (cu->language != language_ada)
9428 return;
9429
9430 /* Check all the children of the given DIE. If it contains nested
9431 subprograms, then check their pc bounds. Likewise, we need to
9432 check lexical blocks as well, as they may also contain subprogram
9433 definitions. */
9434 while (child && child->tag)
9435 {
9436 if (child->tag == DW_TAG_subprogram
9437 || child->tag == DW_TAG_lexical_block)
9438 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9439 child = sibling_die (child);
9440 }
9441 }
9442
9443 /* Get the low and high pc's represented by the scope DIE, and store
9444 them in *LOWPC and *HIGHPC. If the correct values can't be
9445 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9446
9447 static void
9448 get_scope_pc_bounds (struct die_info *die,
9449 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9450 struct dwarf2_cu *cu)
9451 {
9452 CORE_ADDR best_low = (CORE_ADDR) -1;
9453 CORE_ADDR best_high = (CORE_ADDR) 0;
9454 CORE_ADDR current_low, current_high;
9455
9456 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9457 {
9458 best_low = current_low;
9459 best_high = current_high;
9460 }
9461 else
9462 {
9463 struct die_info *child = die->child;
9464
9465 while (child && child->tag)
9466 {
9467 switch (child->tag) {
9468 case DW_TAG_subprogram:
9469 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9470 break;
9471 case DW_TAG_namespace:
9472 case DW_TAG_module:
9473 /* FIXME: carlton/2004-01-16: Should we do this for
9474 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9475 that current GCC's always emit the DIEs corresponding
9476 to definitions of methods of classes as children of a
9477 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9478 the DIEs giving the declarations, which could be
9479 anywhere). But I don't see any reason why the
9480 standards says that they have to be there. */
9481 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9482
9483 if (current_low != ((CORE_ADDR) -1))
9484 {
9485 best_low = min (best_low, current_low);
9486 best_high = max (best_high, current_high);
9487 }
9488 break;
9489 default:
9490 /* Ignore. */
9491 break;
9492 }
9493
9494 child = sibling_die (child);
9495 }
9496 }
9497
9498 *lowpc = best_low;
9499 *highpc = best_high;
9500 }
9501
9502 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9503 in DIE. */
9504
9505 static void
9506 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9507 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9508 {
9509 struct objfile *objfile = cu->objfile;
9510 struct attribute *attr;
9511 struct attribute *attr_high;
9512
9513 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9514 if (attr_high)
9515 {
9516 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9517 if (attr)
9518 {
9519 CORE_ADDR low = DW_ADDR (attr);
9520 CORE_ADDR high;
9521 if (attr_high->form == DW_FORM_addr
9522 || attr_high->form == DW_FORM_GNU_addr_index)
9523 high = DW_ADDR (attr_high);
9524 else
9525 high = low + DW_UNSND (attr_high);
9526
9527 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9528 }
9529 }
9530
9531 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9532 if (attr)
9533 {
9534 bfd *obfd = objfile->obfd;
9535
9536 /* The value of the DW_AT_ranges attribute is the offset of the
9537 address range list in the .debug_ranges section. */
9538 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9539 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9540
9541 /* For some target architectures, but not others, the
9542 read_address function sign-extends the addresses it returns.
9543 To recognize base address selection entries, we need a
9544 mask. */
9545 unsigned int addr_size = cu->header.addr_size;
9546 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9547
9548 /* The base address, to which the next pair is relative. Note
9549 that this 'base' is a DWARF concept: most entries in a range
9550 list are relative, to reduce the number of relocs against the
9551 debugging information. This is separate from this function's
9552 'baseaddr' argument, which GDB uses to relocate debugging
9553 information from a shared library based on the address at
9554 which the library was loaded. */
9555 CORE_ADDR base = cu->base_address;
9556 int base_known = cu->base_known;
9557
9558 gdb_assert (dwarf2_per_objfile->ranges.readin);
9559 if (offset >= dwarf2_per_objfile->ranges.size)
9560 {
9561 complaint (&symfile_complaints,
9562 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9563 offset);
9564 return;
9565 }
9566
9567 for (;;)
9568 {
9569 unsigned int bytes_read;
9570 CORE_ADDR start, end;
9571
9572 start = read_address (obfd, buffer, cu, &bytes_read);
9573 buffer += bytes_read;
9574 end = read_address (obfd, buffer, cu, &bytes_read);
9575 buffer += bytes_read;
9576
9577 /* Did we find the end of the range list? */
9578 if (start == 0 && end == 0)
9579 break;
9580
9581 /* Did we find a base address selection entry? */
9582 else if ((start & base_select_mask) == base_select_mask)
9583 {
9584 base = end;
9585 base_known = 1;
9586 }
9587
9588 /* We found an ordinary address range. */
9589 else
9590 {
9591 if (!base_known)
9592 {
9593 complaint (&symfile_complaints,
9594 _("Invalid .debug_ranges data "
9595 "(no base address)"));
9596 return;
9597 }
9598
9599 if (start > end)
9600 {
9601 /* Inverted range entries are invalid. */
9602 complaint (&symfile_complaints,
9603 _("Invalid .debug_ranges data "
9604 "(inverted range)"));
9605 return;
9606 }
9607
9608 /* Empty range entries have no effect. */
9609 if (start == end)
9610 continue;
9611
9612 start += base + baseaddr;
9613 end += base + baseaddr;
9614
9615 /* A not-uncommon case of bad debug info.
9616 Don't pollute the addrmap with bad data. */
9617 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9618 {
9619 complaint (&symfile_complaints,
9620 _(".debug_ranges entry has start address of zero"
9621 " [in module %s]"), objfile->name);
9622 continue;
9623 }
9624
9625 record_block_range (block, start, end - 1);
9626 }
9627 }
9628 }
9629 }
9630
9631 /* Check whether the producer field indicates either of GCC < 4.6, or the
9632 Intel C/C++ compiler, and cache the result in CU. */
9633
9634 static void
9635 check_producer (struct dwarf2_cu *cu)
9636 {
9637 const char *cs;
9638 int major, minor, release;
9639
9640 if (cu->producer == NULL)
9641 {
9642 /* For unknown compilers expect their behavior is DWARF version
9643 compliant.
9644
9645 GCC started to support .debug_types sections by -gdwarf-4 since
9646 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9647 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9648 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9649 interpreted incorrectly by GDB now - GCC PR debug/48229. */
9650 }
9651 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9652 {
9653 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9654
9655 cs = &cu->producer[strlen ("GNU ")];
9656 while (*cs && !isdigit (*cs))
9657 cs++;
9658 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9659 {
9660 /* Not recognized as GCC. */
9661 }
9662 else
9663 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9664 }
9665 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9666 cu->producer_is_icc = 1;
9667 else
9668 {
9669 /* For other non-GCC compilers, expect their behavior is DWARF version
9670 compliant. */
9671 }
9672
9673 cu->checked_producer = 1;
9674 }
9675
9676 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9677 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9678 during 4.6.0 experimental. */
9679
9680 static int
9681 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9682 {
9683 if (!cu->checked_producer)
9684 check_producer (cu);
9685
9686 return cu->producer_is_gxx_lt_4_6;
9687 }
9688
9689 /* Return the default accessibility type if it is not overriden by
9690 DW_AT_accessibility. */
9691
9692 static enum dwarf_access_attribute
9693 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9694 {
9695 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9696 {
9697 /* The default DWARF 2 accessibility for members is public, the default
9698 accessibility for inheritance is private. */
9699
9700 if (die->tag != DW_TAG_inheritance)
9701 return DW_ACCESS_public;
9702 else
9703 return DW_ACCESS_private;
9704 }
9705 else
9706 {
9707 /* DWARF 3+ defines the default accessibility a different way. The same
9708 rules apply now for DW_TAG_inheritance as for the members and it only
9709 depends on the container kind. */
9710
9711 if (die->parent->tag == DW_TAG_class_type)
9712 return DW_ACCESS_private;
9713 else
9714 return DW_ACCESS_public;
9715 }
9716 }
9717
9718 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9719 offset. If the attribute was not found return 0, otherwise return
9720 1. If it was found but could not properly be handled, set *OFFSET
9721 to 0. */
9722
9723 static int
9724 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9725 LONGEST *offset)
9726 {
9727 struct attribute *attr;
9728
9729 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9730 if (attr != NULL)
9731 {
9732 *offset = 0;
9733
9734 /* Note that we do not check for a section offset first here.
9735 This is because DW_AT_data_member_location is new in DWARF 4,
9736 so if we see it, we can assume that a constant form is really
9737 a constant and not a section offset. */
9738 if (attr_form_is_constant (attr))
9739 *offset = dwarf2_get_attr_constant_value (attr, 0);
9740 else if (attr_form_is_section_offset (attr))
9741 dwarf2_complex_location_expr_complaint ();
9742 else if (attr_form_is_block (attr))
9743 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9744 else
9745 dwarf2_complex_location_expr_complaint ();
9746
9747 return 1;
9748 }
9749
9750 return 0;
9751 }
9752
9753 /* Add an aggregate field to the field list. */
9754
9755 static void
9756 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9757 struct dwarf2_cu *cu)
9758 {
9759 struct objfile *objfile = cu->objfile;
9760 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9761 struct nextfield *new_field;
9762 struct attribute *attr;
9763 struct field *fp;
9764 char *fieldname = "";
9765
9766 /* Allocate a new field list entry and link it in. */
9767 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9768 make_cleanup (xfree, new_field);
9769 memset (new_field, 0, sizeof (struct nextfield));
9770
9771 if (die->tag == DW_TAG_inheritance)
9772 {
9773 new_field->next = fip->baseclasses;
9774 fip->baseclasses = new_field;
9775 }
9776 else
9777 {
9778 new_field->next = fip->fields;
9779 fip->fields = new_field;
9780 }
9781 fip->nfields++;
9782
9783 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9784 if (attr)
9785 new_field->accessibility = DW_UNSND (attr);
9786 else
9787 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9788 if (new_field->accessibility != DW_ACCESS_public)
9789 fip->non_public_fields = 1;
9790
9791 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9792 if (attr)
9793 new_field->virtuality = DW_UNSND (attr);
9794 else
9795 new_field->virtuality = DW_VIRTUALITY_none;
9796
9797 fp = &new_field->field;
9798
9799 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9800 {
9801 LONGEST offset;
9802
9803 /* Data member other than a C++ static data member. */
9804
9805 /* Get type of field. */
9806 fp->type = die_type (die, cu);
9807
9808 SET_FIELD_BITPOS (*fp, 0);
9809
9810 /* Get bit size of field (zero if none). */
9811 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9812 if (attr)
9813 {
9814 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9815 }
9816 else
9817 {
9818 FIELD_BITSIZE (*fp) = 0;
9819 }
9820
9821 /* Get bit offset of field. */
9822 if (handle_data_member_location (die, cu, &offset))
9823 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9824 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9825 if (attr)
9826 {
9827 if (gdbarch_bits_big_endian (gdbarch))
9828 {
9829 /* For big endian bits, the DW_AT_bit_offset gives the
9830 additional bit offset from the MSB of the containing
9831 anonymous object to the MSB of the field. We don't
9832 have to do anything special since we don't need to
9833 know the size of the anonymous object. */
9834 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9835 }
9836 else
9837 {
9838 /* For little endian bits, compute the bit offset to the
9839 MSB of the anonymous object, subtract off the number of
9840 bits from the MSB of the field to the MSB of the
9841 object, and then subtract off the number of bits of
9842 the field itself. The result is the bit offset of
9843 the LSB of the field. */
9844 int anonymous_size;
9845 int bit_offset = DW_UNSND (attr);
9846
9847 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9848 if (attr)
9849 {
9850 /* The size of the anonymous object containing
9851 the bit field is explicit, so use the
9852 indicated size (in bytes). */
9853 anonymous_size = DW_UNSND (attr);
9854 }
9855 else
9856 {
9857 /* The size of the anonymous object containing
9858 the bit field must be inferred from the type
9859 attribute of the data member containing the
9860 bit field. */
9861 anonymous_size = TYPE_LENGTH (fp->type);
9862 }
9863 SET_FIELD_BITPOS (*fp,
9864 (FIELD_BITPOS (*fp)
9865 + anonymous_size * bits_per_byte
9866 - bit_offset - FIELD_BITSIZE (*fp)));
9867 }
9868 }
9869
9870 /* Get name of field. */
9871 fieldname = dwarf2_name (die, cu);
9872 if (fieldname == NULL)
9873 fieldname = "";
9874
9875 /* The name is already allocated along with this objfile, so we don't
9876 need to duplicate it for the type. */
9877 fp->name = fieldname;
9878
9879 /* Change accessibility for artificial fields (e.g. virtual table
9880 pointer or virtual base class pointer) to private. */
9881 if (dwarf2_attr (die, DW_AT_artificial, cu))
9882 {
9883 FIELD_ARTIFICIAL (*fp) = 1;
9884 new_field->accessibility = DW_ACCESS_private;
9885 fip->non_public_fields = 1;
9886 }
9887 }
9888 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9889 {
9890 /* C++ static member. */
9891
9892 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9893 is a declaration, but all versions of G++ as of this writing
9894 (so through at least 3.2.1) incorrectly generate
9895 DW_TAG_variable tags. */
9896
9897 const char *physname;
9898
9899 /* Get name of field. */
9900 fieldname = dwarf2_name (die, cu);
9901 if (fieldname == NULL)
9902 return;
9903
9904 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9905 if (attr
9906 /* Only create a symbol if this is an external value.
9907 new_symbol checks this and puts the value in the global symbol
9908 table, which we want. If it is not external, new_symbol
9909 will try to put the value in cu->list_in_scope which is wrong. */
9910 && dwarf2_flag_true_p (die, DW_AT_external, cu))
9911 {
9912 /* A static const member, not much different than an enum as far as
9913 we're concerned, except that we can support more types. */
9914 new_symbol (die, NULL, cu);
9915 }
9916
9917 /* Get physical name. */
9918 physname = dwarf2_physname (fieldname, die, cu);
9919
9920 /* The name is already allocated along with this objfile, so we don't
9921 need to duplicate it for the type. */
9922 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9923 FIELD_TYPE (*fp) = die_type (die, cu);
9924 FIELD_NAME (*fp) = fieldname;
9925 }
9926 else if (die->tag == DW_TAG_inheritance)
9927 {
9928 LONGEST offset;
9929
9930 /* C++ base class field. */
9931 if (handle_data_member_location (die, cu, &offset))
9932 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9933 FIELD_BITSIZE (*fp) = 0;
9934 FIELD_TYPE (*fp) = die_type (die, cu);
9935 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9936 fip->nbaseclasses++;
9937 }
9938 }
9939
9940 /* Add a typedef defined in the scope of the FIP's class. */
9941
9942 static void
9943 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9944 struct dwarf2_cu *cu)
9945 {
9946 struct objfile *objfile = cu->objfile;
9947 struct typedef_field_list *new_field;
9948 struct attribute *attr;
9949 struct typedef_field *fp;
9950 char *fieldname = "";
9951
9952 /* Allocate a new field list entry and link it in. */
9953 new_field = xzalloc (sizeof (*new_field));
9954 make_cleanup (xfree, new_field);
9955
9956 gdb_assert (die->tag == DW_TAG_typedef);
9957
9958 fp = &new_field->field;
9959
9960 /* Get name of field. */
9961 fp->name = dwarf2_name (die, cu);
9962 if (fp->name == NULL)
9963 return;
9964
9965 fp->type = read_type_die (die, cu);
9966
9967 new_field->next = fip->typedef_field_list;
9968 fip->typedef_field_list = new_field;
9969 fip->typedef_field_list_count++;
9970 }
9971
9972 /* Create the vector of fields, and attach it to the type. */
9973
9974 static void
9975 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9976 struct dwarf2_cu *cu)
9977 {
9978 int nfields = fip->nfields;
9979
9980 /* Record the field count, allocate space for the array of fields,
9981 and create blank accessibility bitfields if necessary. */
9982 TYPE_NFIELDS (type) = nfields;
9983 TYPE_FIELDS (type) = (struct field *)
9984 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9985 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9986
9987 if (fip->non_public_fields && cu->language != language_ada)
9988 {
9989 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9990
9991 TYPE_FIELD_PRIVATE_BITS (type) =
9992 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9993 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9994
9995 TYPE_FIELD_PROTECTED_BITS (type) =
9996 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9997 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
9998
9999 TYPE_FIELD_IGNORE_BITS (type) =
10000 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10001 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10002 }
10003
10004 /* If the type has baseclasses, allocate and clear a bit vector for
10005 TYPE_FIELD_VIRTUAL_BITS. */
10006 if (fip->nbaseclasses && cu->language != language_ada)
10007 {
10008 int num_bytes = B_BYTES (fip->nbaseclasses);
10009 unsigned char *pointer;
10010
10011 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10012 pointer = TYPE_ALLOC (type, num_bytes);
10013 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10014 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10015 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10016 }
10017
10018 /* Copy the saved-up fields into the field vector. Start from the head of
10019 the list, adding to the tail of the field array, so that they end up in
10020 the same order in the array in which they were added to the list. */
10021 while (nfields-- > 0)
10022 {
10023 struct nextfield *fieldp;
10024
10025 if (fip->fields)
10026 {
10027 fieldp = fip->fields;
10028 fip->fields = fieldp->next;
10029 }
10030 else
10031 {
10032 fieldp = fip->baseclasses;
10033 fip->baseclasses = fieldp->next;
10034 }
10035
10036 TYPE_FIELD (type, nfields) = fieldp->field;
10037 switch (fieldp->accessibility)
10038 {
10039 case DW_ACCESS_private:
10040 if (cu->language != language_ada)
10041 SET_TYPE_FIELD_PRIVATE (type, nfields);
10042 break;
10043
10044 case DW_ACCESS_protected:
10045 if (cu->language != language_ada)
10046 SET_TYPE_FIELD_PROTECTED (type, nfields);
10047 break;
10048
10049 case DW_ACCESS_public:
10050 break;
10051
10052 default:
10053 /* Unknown accessibility. Complain and treat it as public. */
10054 {
10055 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10056 fieldp->accessibility);
10057 }
10058 break;
10059 }
10060 if (nfields < fip->nbaseclasses)
10061 {
10062 switch (fieldp->virtuality)
10063 {
10064 case DW_VIRTUALITY_virtual:
10065 case DW_VIRTUALITY_pure_virtual:
10066 if (cu->language == language_ada)
10067 error (_("unexpected virtuality in component of Ada type"));
10068 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10069 break;
10070 }
10071 }
10072 }
10073 }
10074
10075 /* Add a member function to the proper fieldlist. */
10076
10077 static void
10078 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10079 struct type *type, struct dwarf2_cu *cu)
10080 {
10081 struct objfile *objfile = cu->objfile;
10082 struct attribute *attr;
10083 struct fnfieldlist *flp;
10084 int i;
10085 struct fn_field *fnp;
10086 char *fieldname;
10087 struct nextfnfield *new_fnfield;
10088 struct type *this_type;
10089 enum dwarf_access_attribute accessibility;
10090
10091 if (cu->language == language_ada)
10092 error (_("unexpected member function in Ada type"));
10093
10094 /* Get name of member function. */
10095 fieldname = dwarf2_name (die, cu);
10096 if (fieldname == NULL)
10097 return;
10098
10099 /* Look up member function name in fieldlist. */
10100 for (i = 0; i < fip->nfnfields; i++)
10101 {
10102 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10103 break;
10104 }
10105
10106 /* Create new list element if necessary. */
10107 if (i < fip->nfnfields)
10108 flp = &fip->fnfieldlists[i];
10109 else
10110 {
10111 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10112 {
10113 fip->fnfieldlists = (struct fnfieldlist *)
10114 xrealloc (fip->fnfieldlists,
10115 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10116 * sizeof (struct fnfieldlist));
10117 if (fip->nfnfields == 0)
10118 make_cleanup (free_current_contents, &fip->fnfieldlists);
10119 }
10120 flp = &fip->fnfieldlists[fip->nfnfields];
10121 flp->name = fieldname;
10122 flp->length = 0;
10123 flp->head = NULL;
10124 i = fip->nfnfields++;
10125 }
10126
10127 /* Create a new member function field and chain it to the field list
10128 entry. */
10129 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10130 make_cleanup (xfree, new_fnfield);
10131 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10132 new_fnfield->next = flp->head;
10133 flp->head = new_fnfield;
10134 flp->length++;
10135
10136 /* Fill in the member function field info. */
10137 fnp = &new_fnfield->fnfield;
10138
10139 /* Delay processing of the physname until later. */
10140 if (cu->language == language_cplus || cu->language == language_java)
10141 {
10142 add_to_method_list (type, i, flp->length - 1, fieldname,
10143 die, cu);
10144 }
10145 else
10146 {
10147 const char *physname = dwarf2_physname (fieldname, die, cu);
10148 fnp->physname = physname ? physname : "";
10149 }
10150
10151 fnp->type = alloc_type (objfile);
10152 this_type = read_type_die (die, cu);
10153 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10154 {
10155 int nparams = TYPE_NFIELDS (this_type);
10156
10157 /* TYPE is the domain of this method, and THIS_TYPE is the type
10158 of the method itself (TYPE_CODE_METHOD). */
10159 smash_to_method_type (fnp->type, type,
10160 TYPE_TARGET_TYPE (this_type),
10161 TYPE_FIELDS (this_type),
10162 TYPE_NFIELDS (this_type),
10163 TYPE_VARARGS (this_type));
10164
10165 /* Handle static member functions.
10166 Dwarf2 has no clean way to discern C++ static and non-static
10167 member functions. G++ helps GDB by marking the first
10168 parameter for non-static member functions (which is the this
10169 pointer) as artificial. We obtain this information from
10170 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10171 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10172 fnp->voffset = VOFFSET_STATIC;
10173 }
10174 else
10175 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10176 dwarf2_full_name (fieldname, die, cu));
10177
10178 /* Get fcontext from DW_AT_containing_type if present. */
10179 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10180 fnp->fcontext = die_containing_type (die, cu);
10181
10182 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10183 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10184
10185 /* Get accessibility. */
10186 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10187 if (attr)
10188 accessibility = DW_UNSND (attr);
10189 else
10190 accessibility = dwarf2_default_access_attribute (die, cu);
10191 switch (accessibility)
10192 {
10193 case DW_ACCESS_private:
10194 fnp->is_private = 1;
10195 break;
10196 case DW_ACCESS_protected:
10197 fnp->is_protected = 1;
10198 break;
10199 }
10200
10201 /* Check for artificial methods. */
10202 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10203 if (attr && DW_UNSND (attr) != 0)
10204 fnp->is_artificial = 1;
10205
10206 /* Get index in virtual function table if it is a virtual member
10207 function. For older versions of GCC, this is an offset in the
10208 appropriate virtual table, as specified by DW_AT_containing_type.
10209 For everyone else, it is an expression to be evaluated relative
10210 to the object address. */
10211
10212 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10213 if (attr)
10214 {
10215 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10216 {
10217 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10218 {
10219 /* Old-style GCC. */
10220 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10221 }
10222 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10223 || (DW_BLOCK (attr)->size > 1
10224 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10225 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10226 {
10227 struct dwarf_block blk;
10228 int offset;
10229
10230 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10231 ? 1 : 2);
10232 blk.size = DW_BLOCK (attr)->size - offset;
10233 blk.data = DW_BLOCK (attr)->data + offset;
10234 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10235 if ((fnp->voffset % cu->header.addr_size) != 0)
10236 dwarf2_complex_location_expr_complaint ();
10237 else
10238 fnp->voffset /= cu->header.addr_size;
10239 fnp->voffset += 2;
10240 }
10241 else
10242 dwarf2_complex_location_expr_complaint ();
10243
10244 if (!fnp->fcontext)
10245 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10246 }
10247 else if (attr_form_is_section_offset (attr))
10248 {
10249 dwarf2_complex_location_expr_complaint ();
10250 }
10251 else
10252 {
10253 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10254 fieldname);
10255 }
10256 }
10257 else
10258 {
10259 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10260 if (attr && DW_UNSND (attr))
10261 {
10262 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10263 complaint (&symfile_complaints,
10264 _("Member function \"%s\" (offset %d) is virtual "
10265 "but the vtable offset is not specified"),
10266 fieldname, die->offset.sect_off);
10267 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10268 TYPE_CPLUS_DYNAMIC (type) = 1;
10269 }
10270 }
10271 }
10272
10273 /* Create the vector of member function fields, and attach it to the type. */
10274
10275 static void
10276 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10277 struct dwarf2_cu *cu)
10278 {
10279 struct fnfieldlist *flp;
10280 int i;
10281
10282 if (cu->language == language_ada)
10283 error (_("unexpected member functions in Ada type"));
10284
10285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10286 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10287 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10288
10289 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10290 {
10291 struct nextfnfield *nfp = flp->head;
10292 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10293 int k;
10294
10295 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10296 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10297 fn_flp->fn_fields = (struct fn_field *)
10298 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10299 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10300 fn_flp->fn_fields[k] = nfp->fnfield;
10301 }
10302
10303 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10304 }
10305
10306 /* Returns non-zero if NAME is the name of a vtable member in CU's
10307 language, zero otherwise. */
10308 static int
10309 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10310 {
10311 static const char vptr[] = "_vptr";
10312 static const char vtable[] = "vtable";
10313
10314 /* Look for the C++ and Java forms of the vtable. */
10315 if ((cu->language == language_java
10316 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10317 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10318 && is_cplus_marker (name[sizeof (vptr) - 1])))
10319 return 1;
10320
10321 return 0;
10322 }
10323
10324 /* GCC outputs unnamed structures that are really pointers to member
10325 functions, with the ABI-specified layout. If TYPE describes
10326 such a structure, smash it into a member function type.
10327
10328 GCC shouldn't do this; it should just output pointer to member DIEs.
10329 This is GCC PR debug/28767. */
10330
10331 static void
10332 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10333 {
10334 struct type *pfn_type, *domain_type, *new_type;
10335
10336 /* Check for a structure with no name and two children. */
10337 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10338 return;
10339
10340 /* Check for __pfn and __delta members. */
10341 if (TYPE_FIELD_NAME (type, 0) == NULL
10342 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10343 || TYPE_FIELD_NAME (type, 1) == NULL
10344 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10345 return;
10346
10347 /* Find the type of the method. */
10348 pfn_type = TYPE_FIELD_TYPE (type, 0);
10349 if (pfn_type == NULL
10350 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10351 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10352 return;
10353
10354 /* Look for the "this" argument. */
10355 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10356 if (TYPE_NFIELDS (pfn_type) == 0
10357 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10358 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10359 return;
10360
10361 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10362 new_type = alloc_type (objfile);
10363 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10364 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10365 TYPE_VARARGS (pfn_type));
10366 smash_to_methodptr_type (type, new_type);
10367 }
10368
10369 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10370 (icc). */
10371
10372 static int
10373 producer_is_icc (struct dwarf2_cu *cu)
10374 {
10375 if (!cu->checked_producer)
10376 check_producer (cu);
10377
10378 return cu->producer_is_icc;
10379 }
10380
10381 /* Called when we find the DIE that starts a structure or union scope
10382 (definition) to create a type for the structure or union. Fill in
10383 the type's name and general properties; the members will not be
10384 processed until process_structure_type.
10385
10386 NOTE: we need to call these functions regardless of whether or not the
10387 DIE has a DW_AT_name attribute, since it might be an anonymous
10388 structure or union. This gets the type entered into our set of
10389 user defined types.
10390
10391 However, if the structure is incomplete (an opaque struct/union)
10392 then suppress creating a symbol table entry for it since gdb only
10393 wants to find the one with the complete definition. Note that if
10394 it is complete, we just call new_symbol, which does it's own
10395 checking about whether the struct/union is anonymous or not (and
10396 suppresses creating a symbol table entry itself). */
10397
10398 static struct type *
10399 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10400 {
10401 struct objfile *objfile = cu->objfile;
10402 struct type *type;
10403 struct attribute *attr;
10404 char *name;
10405
10406 /* If the definition of this type lives in .debug_types, read that type.
10407 Don't follow DW_AT_specification though, that will take us back up
10408 the chain and we want to go down. */
10409 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10410 if (attr)
10411 {
10412 struct dwarf2_cu *type_cu = cu;
10413 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10414
10415 /* We could just recurse on read_structure_type, but we need to call
10416 get_die_type to ensure only one type for this DIE is created.
10417 This is important, for example, because for c++ classes we need
10418 TYPE_NAME set which is only done by new_symbol. Blech. */
10419 type = read_type_die (type_die, type_cu);
10420
10421 /* TYPE_CU may not be the same as CU.
10422 Ensure TYPE is recorded in CU's type_hash table. */
10423 return set_die_type (die, type, cu);
10424 }
10425
10426 type = alloc_type (objfile);
10427 INIT_CPLUS_SPECIFIC (type);
10428
10429 name = dwarf2_name (die, cu);
10430 if (name != NULL)
10431 {
10432 if (cu->language == language_cplus
10433 || cu->language == language_java)
10434 {
10435 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10436
10437 /* dwarf2_full_name might have already finished building the DIE's
10438 type. If so, there is no need to continue. */
10439 if (get_die_type (die, cu) != NULL)
10440 return get_die_type (die, cu);
10441
10442 TYPE_TAG_NAME (type) = full_name;
10443 if (die->tag == DW_TAG_structure_type
10444 || die->tag == DW_TAG_class_type)
10445 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10446 }
10447 else
10448 {
10449 /* The name is already allocated along with this objfile, so
10450 we don't need to duplicate it for the type. */
10451 TYPE_TAG_NAME (type) = (char *) name;
10452 if (die->tag == DW_TAG_class_type)
10453 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10454 }
10455 }
10456
10457 if (die->tag == DW_TAG_structure_type)
10458 {
10459 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10460 }
10461 else if (die->tag == DW_TAG_union_type)
10462 {
10463 TYPE_CODE (type) = TYPE_CODE_UNION;
10464 }
10465 else
10466 {
10467 TYPE_CODE (type) = TYPE_CODE_CLASS;
10468 }
10469
10470 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10471 TYPE_DECLARED_CLASS (type) = 1;
10472
10473 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10474 if (attr)
10475 {
10476 TYPE_LENGTH (type) = DW_UNSND (attr);
10477 }
10478 else
10479 {
10480 TYPE_LENGTH (type) = 0;
10481 }
10482
10483 if (producer_is_icc (cu))
10484 {
10485 /* ICC does not output the required DW_AT_declaration
10486 on incomplete types, but gives them a size of zero. */
10487 }
10488 else
10489 TYPE_STUB_SUPPORTED (type) = 1;
10490
10491 if (die_is_declaration (die, cu))
10492 TYPE_STUB (type) = 1;
10493 else if (attr == NULL && die->child == NULL
10494 && producer_is_realview (cu->producer))
10495 /* RealView does not output the required DW_AT_declaration
10496 on incomplete types. */
10497 TYPE_STUB (type) = 1;
10498
10499 /* We need to add the type field to the die immediately so we don't
10500 infinitely recurse when dealing with pointers to the structure
10501 type within the structure itself. */
10502 set_die_type (die, type, cu);
10503
10504 /* set_die_type should be already done. */
10505 set_descriptive_type (type, die, cu);
10506
10507 return type;
10508 }
10509
10510 /* Finish creating a structure or union type, including filling in
10511 its members and creating a symbol for it. */
10512
10513 static void
10514 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10515 {
10516 struct objfile *objfile = cu->objfile;
10517 struct die_info *child_die = die->child;
10518 struct type *type;
10519
10520 type = get_die_type (die, cu);
10521 if (type == NULL)
10522 type = read_structure_type (die, cu);
10523
10524 if (die->child != NULL && ! die_is_declaration (die, cu))
10525 {
10526 struct field_info fi;
10527 struct die_info *child_die;
10528 VEC (symbolp) *template_args = NULL;
10529 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10530
10531 memset (&fi, 0, sizeof (struct field_info));
10532
10533 child_die = die->child;
10534
10535 while (child_die && child_die->tag)
10536 {
10537 if (child_die->tag == DW_TAG_member
10538 || child_die->tag == DW_TAG_variable)
10539 {
10540 /* NOTE: carlton/2002-11-05: A C++ static data member
10541 should be a DW_TAG_member that is a declaration, but
10542 all versions of G++ as of this writing (so through at
10543 least 3.2.1) incorrectly generate DW_TAG_variable
10544 tags for them instead. */
10545 dwarf2_add_field (&fi, child_die, cu);
10546 }
10547 else if (child_die->tag == DW_TAG_subprogram)
10548 {
10549 /* C++ member function. */
10550 dwarf2_add_member_fn (&fi, child_die, type, cu);
10551 }
10552 else if (child_die->tag == DW_TAG_inheritance)
10553 {
10554 /* C++ base class field. */
10555 dwarf2_add_field (&fi, child_die, cu);
10556 }
10557 else if (child_die->tag == DW_TAG_typedef)
10558 dwarf2_add_typedef (&fi, child_die, cu);
10559 else if (child_die->tag == DW_TAG_template_type_param
10560 || child_die->tag == DW_TAG_template_value_param)
10561 {
10562 struct symbol *arg = new_symbol (child_die, NULL, cu);
10563
10564 if (arg != NULL)
10565 VEC_safe_push (symbolp, template_args, arg);
10566 }
10567
10568 child_die = sibling_die (child_die);
10569 }
10570
10571 /* Attach template arguments to type. */
10572 if (! VEC_empty (symbolp, template_args))
10573 {
10574 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10575 TYPE_N_TEMPLATE_ARGUMENTS (type)
10576 = VEC_length (symbolp, template_args);
10577 TYPE_TEMPLATE_ARGUMENTS (type)
10578 = obstack_alloc (&objfile->objfile_obstack,
10579 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10580 * sizeof (struct symbol *)));
10581 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10582 VEC_address (symbolp, template_args),
10583 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10584 * sizeof (struct symbol *)));
10585 VEC_free (symbolp, template_args);
10586 }
10587
10588 /* Attach fields and member functions to the type. */
10589 if (fi.nfields)
10590 dwarf2_attach_fields_to_type (&fi, type, cu);
10591 if (fi.nfnfields)
10592 {
10593 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10594
10595 /* Get the type which refers to the base class (possibly this
10596 class itself) which contains the vtable pointer for the current
10597 class from the DW_AT_containing_type attribute. This use of
10598 DW_AT_containing_type is a GNU extension. */
10599
10600 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10601 {
10602 struct type *t = die_containing_type (die, cu);
10603
10604 TYPE_VPTR_BASETYPE (type) = t;
10605 if (type == t)
10606 {
10607 int i;
10608
10609 /* Our own class provides vtbl ptr. */
10610 for (i = TYPE_NFIELDS (t) - 1;
10611 i >= TYPE_N_BASECLASSES (t);
10612 --i)
10613 {
10614 const char *fieldname = TYPE_FIELD_NAME (t, i);
10615
10616 if (is_vtable_name (fieldname, cu))
10617 {
10618 TYPE_VPTR_FIELDNO (type) = i;
10619 break;
10620 }
10621 }
10622
10623 /* Complain if virtual function table field not found. */
10624 if (i < TYPE_N_BASECLASSES (t))
10625 complaint (&symfile_complaints,
10626 _("virtual function table pointer "
10627 "not found when defining class '%s'"),
10628 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10629 "");
10630 }
10631 else
10632 {
10633 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10634 }
10635 }
10636 else if (cu->producer
10637 && strncmp (cu->producer,
10638 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10639 {
10640 /* The IBM XLC compiler does not provide direct indication
10641 of the containing type, but the vtable pointer is
10642 always named __vfp. */
10643
10644 int i;
10645
10646 for (i = TYPE_NFIELDS (type) - 1;
10647 i >= TYPE_N_BASECLASSES (type);
10648 --i)
10649 {
10650 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10651 {
10652 TYPE_VPTR_FIELDNO (type) = i;
10653 TYPE_VPTR_BASETYPE (type) = type;
10654 break;
10655 }
10656 }
10657 }
10658 }
10659
10660 /* Copy fi.typedef_field_list linked list elements content into the
10661 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10662 if (fi.typedef_field_list)
10663 {
10664 int i = fi.typedef_field_list_count;
10665
10666 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10667 TYPE_TYPEDEF_FIELD_ARRAY (type)
10668 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10669 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10670
10671 /* Reverse the list order to keep the debug info elements order. */
10672 while (--i >= 0)
10673 {
10674 struct typedef_field *dest, *src;
10675
10676 dest = &TYPE_TYPEDEF_FIELD (type, i);
10677 src = &fi.typedef_field_list->field;
10678 fi.typedef_field_list = fi.typedef_field_list->next;
10679 *dest = *src;
10680 }
10681 }
10682
10683 do_cleanups (back_to);
10684
10685 if (HAVE_CPLUS_STRUCT (type))
10686 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10687 }
10688
10689 quirk_gcc_member_function_pointer (type, objfile);
10690
10691 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10692 snapshots) has been known to create a die giving a declaration
10693 for a class that has, as a child, a die giving a definition for a
10694 nested class. So we have to process our children even if the
10695 current die is a declaration. Normally, of course, a declaration
10696 won't have any children at all. */
10697
10698 while (child_die != NULL && child_die->tag)
10699 {
10700 if (child_die->tag == DW_TAG_member
10701 || child_die->tag == DW_TAG_variable
10702 || child_die->tag == DW_TAG_inheritance
10703 || child_die->tag == DW_TAG_template_value_param
10704 || child_die->tag == DW_TAG_template_type_param)
10705 {
10706 /* Do nothing. */
10707 }
10708 else
10709 process_die (child_die, cu);
10710
10711 child_die = sibling_die (child_die);
10712 }
10713
10714 /* Do not consider external references. According to the DWARF standard,
10715 these DIEs are identified by the fact that they have no byte_size
10716 attribute, and a declaration attribute. */
10717 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10718 || !die_is_declaration (die, cu))
10719 new_symbol (die, type, cu);
10720 }
10721
10722 /* Given a DW_AT_enumeration_type die, set its type. We do not
10723 complete the type's fields yet, or create any symbols. */
10724
10725 static struct type *
10726 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10727 {
10728 struct objfile *objfile = cu->objfile;
10729 struct type *type;
10730 struct attribute *attr;
10731 const char *name;
10732
10733 /* If the definition of this type lives in .debug_types, read that type.
10734 Don't follow DW_AT_specification though, that will take us back up
10735 the chain and we want to go down. */
10736 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10737 if (attr)
10738 {
10739 struct dwarf2_cu *type_cu = cu;
10740 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10741
10742 type = read_type_die (type_die, type_cu);
10743
10744 /* TYPE_CU may not be the same as CU.
10745 Ensure TYPE is recorded in CU's type_hash table. */
10746 return set_die_type (die, type, cu);
10747 }
10748
10749 type = alloc_type (objfile);
10750
10751 TYPE_CODE (type) = TYPE_CODE_ENUM;
10752 name = dwarf2_full_name (NULL, die, cu);
10753 if (name != NULL)
10754 TYPE_TAG_NAME (type) = (char *) name;
10755
10756 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10757 if (attr)
10758 {
10759 TYPE_LENGTH (type) = DW_UNSND (attr);
10760 }
10761 else
10762 {
10763 TYPE_LENGTH (type) = 0;
10764 }
10765
10766 /* The enumeration DIE can be incomplete. In Ada, any type can be
10767 declared as private in the package spec, and then defined only
10768 inside the package body. Such types are known as Taft Amendment
10769 Types. When another package uses such a type, an incomplete DIE
10770 may be generated by the compiler. */
10771 if (die_is_declaration (die, cu))
10772 TYPE_STUB (type) = 1;
10773
10774 return set_die_type (die, type, cu);
10775 }
10776
10777 /* Given a pointer to a die which begins an enumeration, process all
10778 the dies that define the members of the enumeration, and create the
10779 symbol for the enumeration type.
10780
10781 NOTE: We reverse the order of the element list. */
10782
10783 static void
10784 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10785 {
10786 struct type *this_type;
10787
10788 this_type = get_die_type (die, cu);
10789 if (this_type == NULL)
10790 this_type = read_enumeration_type (die, cu);
10791
10792 if (die->child != NULL)
10793 {
10794 struct die_info *child_die;
10795 struct symbol *sym;
10796 struct field *fields = NULL;
10797 int num_fields = 0;
10798 int unsigned_enum = 1;
10799 char *name;
10800 int flag_enum = 1;
10801 ULONGEST mask = 0;
10802
10803 child_die = die->child;
10804 while (child_die && child_die->tag)
10805 {
10806 if (child_die->tag != DW_TAG_enumerator)
10807 {
10808 process_die (child_die, cu);
10809 }
10810 else
10811 {
10812 name = dwarf2_name (child_die, cu);
10813 if (name)
10814 {
10815 sym = new_symbol (child_die, this_type, cu);
10816 if (SYMBOL_VALUE (sym) < 0)
10817 {
10818 unsigned_enum = 0;
10819 flag_enum = 0;
10820 }
10821 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10822 flag_enum = 0;
10823 else
10824 mask |= SYMBOL_VALUE (sym);
10825
10826 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10827 {
10828 fields = (struct field *)
10829 xrealloc (fields,
10830 (num_fields + DW_FIELD_ALLOC_CHUNK)
10831 * sizeof (struct field));
10832 }
10833
10834 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10835 FIELD_TYPE (fields[num_fields]) = NULL;
10836 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10837 FIELD_BITSIZE (fields[num_fields]) = 0;
10838
10839 num_fields++;
10840 }
10841 }
10842
10843 child_die = sibling_die (child_die);
10844 }
10845
10846 if (num_fields)
10847 {
10848 TYPE_NFIELDS (this_type) = num_fields;
10849 TYPE_FIELDS (this_type) = (struct field *)
10850 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10851 memcpy (TYPE_FIELDS (this_type), fields,
10852 sizeof (struct field) * num_fields);
10853 xfree (fields);
10854 }
10855 if (unsigned_enum)
10856 TYPE_UNSIGNED (this_type) = 1;
10857 if (flag_enum)
10858 TYPE_FLAG_ENUM (this_type) = 1;
10859 }
10860
10861 /* If we are reading an enum from a .debug_types unit, and the enum
10862 is a declaration, and the enum is not the signatured type in the
10863 unit, then we do not want to add a symbol for it. Adding a
10864 symbol would in some cases obscure the true definition of the
10865 enum, giving users an incomplete type when the definition is
10866 actually available. Note that we do not want to do this for all
10867 enums which are just declarations, because C++0x allows forward
10868 enum declarations. */
10869 if (cu->per_cu->is_debug_types
10870 && die_is_declaration (die, cu))
10871 {
10872 struct signatured_type *sig_type;
10873
10874 sig_type
10875 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10876 cu->per_cu->info_or_types_section,
10877 cu->per_cu->offset);
10878 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10879 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10880 return;
10881 }
10882
10883 new_symbol (die, this_type, cu);
10884 }
10885
10886 /* Extract all information from a DW_TAG_array_type DIE and put it in
10887 the DIE's type field. For now, this only handles one dimensional
10888 arrays. */
10889
10890 static struct type *
10891 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10892 {
10893 struct objfile *objfile = cu->objfile;
10894 struct die_info *child_die;
10895 struct type *type;
10896 struct type *element_type, *range_type, *index_type;
10897 struct type **range_types = NULL;
10898 struct attribute *attr;
10899 int ndim = 0;
10900 struct cleanup *back_to;
10901 char *name;
10902
10903 element_type = die_type (die, cu);
10904
10905 /* The die_type call above may have already set the type for this DIE. */
10906 type = get_die_type (die, cu);
10907 if (type)
10908 return type;
10909
10910 /* Irix 6.2 native cc creates array types without children for
10911 arrays with unspecified length. */
10912 if (die->child == NULL)
10913 {
10914 index_type = objfile_type (objfile)->builtin_int;
10915 range_type = create_range_type (NULL, index_type, 0, -1);
10916 type = create_array_type (NULL, element_type, range_type);
10917 return set_die_type (die, type, cu);
10918 }
10919
10920 back_to = make_cleanup (null_cleanup, NULL);
10921 child_die = die->child;
10922 while (child_die && child_die->tag)
10923 {
10924 if (child_die->tag == DW_TAG_subrange_type)
10925 {
10926 struct type *child_type = read_type_die (child_die, cu);
10927
10928 if (child_type != NULL)
10929 {
10930 /* The range type was succesfully read. Save it for the
10931 array type creation. */
10932 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10933 {
10934 range_types = (struct type **)
10935 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10936 * sizeof (struct type *));
10937 if (ndim == 0)
10938 make_cleanup (free_current_contents, &range_types);
10939 }
10940 range_types[ndim++] = child_type;
10941 }
10942 }
10943 child_die = sibling_die (child_die);
10944 }
10945
10946 /* Dwarf2 dimensions are output from left to right, create the
10947 necessary array types in backwards order. */
10948
10949 type = element_type;
10950
10951 if (read_array_order (die, cu) == DW_ORD_col_major)
10952 {
10953 int i = 0;
10954
10955 while (i < ndim)
10956 type = create_array_type (NULL, type, range_types[i++]);
10957 }
10958 else
10959 {
10960 while (ndim-- > 0)
10961 type = create_array_type (NULL, type, range_types[ndim]);
10962 }
10963
10964 /* Understand Dwarf2 support for vector types (like they occur on
10965 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10966 array type. This is not part of the Dwarf2/3 standard yet, but a
10967 custom vendor extension. The main difference between a regular
10968 array and the vector variant is that vectors are passed by value
10969 to functions. */
10970 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10971 if (attr)
10972 make_vector_type (type);
10973
10974 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10975 implementation may choose to implement triple vectors using this
10976 attribute. */
10977 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10978 if (attr)
10979 {
10980 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10981 TYPE_LENGTH (type) = DW_UNSND (attr);
10982 else
10983 complaint (&symfile_complaints,
10984 _("DW_AT_byte_size for array type smaller "
10985 "than the total size of elements"));
10986 }
10987
10988 name = dwarf2_name (die, cu);
10989 if (name)
10990 TYPE_NAME (type) = name;
10991
10992 /* Install the type in the die. */
10993 set_die_type (die, type, cu);
10994
10995 /* set_die_type should be already done. */
10996 set_descriptive_type (type, die, cu);
10997
10998 do_cleanups (back_to);
10999
11000 return type;
11001 }
11002
11003 static enum dwarf_array_dim_ordering
11004 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11005 {
11006 struct attribute *attr;
11007
11008 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11009
11010 if (attr) return DW_SND (attr);
11011
11012 /* GNU F77 is a special case, as at 08/2004 array type info is the
11013 opposite order to the dwarf2 specification, but data is still
11014 laid out as per normal fortran.
11015
11016 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11017 version checking. */
11018
11019 if (cu->language == language_fortran
11020 && cu->producer && strstr (cu->producer, "GNU F77"))
11021 {
11022 return DW_ORD_row_major;
11023 }
11024
11025 switch (cu->language_defn->la_array_ordering)
11026 {
11027 case array_column_major:
11028 return DW_ORD_col_major;
11029 case array_row_major:
11030 default:
11031 return DW_ORD_row_major;
11032 };
11033 }
11034
11035 /* Extract all information from a DW_TAG_set_type DIE and put it in
11036 the DIE's type field. */
11037
11038 static struct type *
11039 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11040 {
11041 struct type *domain_type, *set_type;
11042 struct attribute *attr;
11043
11044 domain_type = die_type (die, cu);
11045
11046 /* The die_type call above may have already set the type for this DIE. */
11047 set_type = get_die_type (die, cu);
11048 if (set_type)
11049 return set_type;
11050
11051 set_type = create_set_type (NULL, domain_type);
11052
11053 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11054 if (attr)
11055 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11056
11057 return set_die_type (die, set_type, cu);
11058 }
11059
11060 /* First cut: install each common block member as a global variable. */
11061
11062 static void
11063 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11064 {
11065 struct die_info *child_die;
11066 struct attribute *attr;
11067 struct symbol *sym;
11068 CORE_ADDR base = (CORE_ADDR) 0;
11069
11070 attr = dwarf2_attr (die, DW_AT_location, cu);
11071 if (attr)
11072 {
11073 /* Support the .debug_loc offsets. */
11074 if (attr_form_is_block (attr))
11075 {
11076 base = decode_locdesc (DW_BLOCK (attr), cu);
11077 }
11078 else if (attr_form_is_section_offset (attr))
11079 {
11080 dwarf2_complex_location_expr_complaint ();
11081 }
11082 else
11083 {
11084 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11085 "common block member");
11086 }
11087 }
11088 if (die->child != NULL)
11089 {
11090 child_die = die->child;
11091 while (child_die && child_die->tag)
11092 {
11093 LONGEST offset;
11094
11095 sym = new_symbol (child_die, NULL, cu);
11096 if (sym != NULL
11097 && handle_data_member_location (child_die, cu, &offset))
11098 {
11099 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
11100 add_symbol_to_list (sym, &global_symbols);
11101 }
11102 child_die = sibling_die (child_die);
11103 }
11104 }
11105 }
11106
11107 /* Create a type for a C++ namespace. */
11108
11109 static struct type *
11110 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11111 {
11112 struct objfile *objfile = cu->objfile;
11113 const char *previous_prefix, *name;
11114 int is_anonymous;
11115 struct type *type;
11116
11117 /* For extensions, reuse the type of the original namespace. */
11118 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11119 {
11120 struct die_info *ext_die;
11121 struct dwarf2_cu *ext_cu = cu;
11122
11123 ext_die = dwarf2_extension (die, &ext_cu);
11124 type = read_type_die (ext_die, ext_cu);
11125
11126 /* EXT_CU may not be the same as CU.
11127 Ensure TYPE is recorded in CU's type_hash table. */
11128 return set_die_type (die, type, cu);
11129 }
11130
11131 name = namespace_name (die, &is_anonymous, cu);
11132
11133 /* Now build the name of the current namespace. */
11134
11135 previous_prefix = determine_prefix (die, cu);
11136 if (previous_prefix[0] != '\0')
11137 name = typename_concat (&objfile->objfile_obstack,
11138 previous_prefix, name, 0, cu);
11139
11140 /* Create the type. */
11141 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11142 objfile);
11143 TYPE_NAME (type) = (char *) name;
11144 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11145
11146 return set_die_type (die, type, cu);
11147 }
11148
11149 /* Read a C++ namespace. */
11150
11151 static void
11152 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11153 {
11154 struct objfile *objfile = cu->objfile;
11155 int is_anonymous;
11156
11157 /* Add a symbol associated to this if we haven't seen the namespace
11158 before. Also, add a using directive if it's an anonymous
11159 namespace. */
11160
11161 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11162 {
11163 struct type *type;
11164
11165 type = read_type_die (die, cu);
11166 new_symbol (die, type, cu);
11167
11168 namespace_name (die, &is_anonymous, cu);
11169 if (is_anonymous)
11170 {
11171 const char *previous_prefix = determine_prefix (die, cu);
11172
11173 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11174 NULL, NULL, &objfile->objfile_obstack);
11175 }
11176 }
11177
11178 if (die->child != NULL)
11179 {
11180 struct die_info *child_die = die->child;
11181
11182 while (child_die && child_die->tag)
11183 {
11184 process_die (child_die, cu);
11185 child_die = sibling_die (child_die);
11186 }
11187 }
11188 }
11189
11190 /* Read a Fortran module as type. This DIE can be only a declaration used for
11191 imported module. Still we need that type as local Fortran "use ... only"
11192 declaration imports depend on the created type in determine_prefix. */
11193
11194 static struct type *
11195 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11196 {
11197 struct objfile *objfile = cu->objfile;
11198 char *module_name;
11199 struct type *type;
11200
11201 module_name = dwarf2_name (die, cu);
11202 if (!module_name)
11203 complaint (&symfile_complaints,
11204 _("DW_TAG_module has no name, offset 0x%x"),
11205 die->offset.sect_off);
11206 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11207
11208 /* determine_prefix uses TYPE_TAG_NAME. */
11209 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11210
11211 return set_die_type (die, type, cu);
11212 }
11213
11214 /* Read a Fortran module. */
11215
11216 static void
11217 read_module (struct die_info *die, struct dwarf2_cu *cu)
11218 {
11219 struct die_info *child_die = die->child;
11220
11221 while (child_die && child_die->tag)
11222 {
11223 process_die (child_die, cu);
11224 child_die = sibling_die (child_die);
11225 }
11226 }
11227
11228 /* Return the name of the namespace represented by DIE. Set
11229 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11230 namespace. */
11231
11232 static const char *
11233 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11234 {
11235 struct die_info *current_die;
11236 const char *name = NULL;
11237
11238 /* Loop through the extensions until we find a name. */
11239
11240 for (current_die = die;
11241 current_die != NULL;
11242 current_die = dwarf2_extension (die, &cu))
11243 {
11244 name = dwarf2_name (current_die, cu);
11245 if (name != NULL)
11246 break;
11247 }
11248
11249 /* Is it an anonymous namespace? */
11250
11251 *is_anonymous = (name == NULL);
11252 if (*is_anonymous)
11253 name = CP_ANONYMOUS_NAMESPACE_STR;
11254
11255 return name;
11256 }
11257
11258 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11259 the user defined type vector. */
11260
11261 static struct type *
11262 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11263 {
11264 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11265 struct comp_unit_head *cu_header = &cu->header;
11266 struct type *type;
11267 struct attribute *attr_byte_size;
11268 struct attribute *attr_address_class;
11269 int byte_size, addr_class;
11270 struct type *target_type;
11271
11272 target_type = die_type (die, cu);
11273
11274 /* The die_type call above may have already set the type for this DIE. */
11275 type = get_die_type (die, cu);
11276 if (type)
11277 return type;
11278
11279 type = lookup_pointer_type (target_type);
11280
11281 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11282 if (attr_byte_size)
11283 byte_size = DW_UNSND (attr_byte_size);
11284 else
11285 byte_size = cu_header->addr_size;
11286
11287 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11288 if (attr_address_class)
11289 addr_class = DW_UNSND (attr_address_class);
11290 else
11291 addr_class = DW_ADDR_none;
11292
11293 /* If the pointer size or address class is different than the
11294 default, create a type variant marked as such and set the
11295 length accordingly. */
11296 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11297 {
11298 if (gdbarch_address_class_type_flags_p (gdbarch))
11299 {
11300 int type_flags;
11301
11302 type_flags = gdbarch_address_class_type_flags
11303 (gdbarch, byte_size, addr_class);
11304 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11305 == 0);
11306 type = make_type_with_address_space (type, type_flags);
11307 }
11308 else if (TYPE_LENGTH (type) != byte_size)
11309 {
11310 complaint (&symfile_complaints,
11311 _("invalid pointer size %d"), byte_size);
11312 }
11313 else
11314 {
11315 /* Should we also complain about unhandled address classes? */
11316 }
11317 }
11318
11319 TYPE_LENGTH (type) = byte_size;
11320 return set_die_type (die, type, cu);
11321 }
11322
11323 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11324 the user defined type vector. */
11325
11326 static struct type *
11327 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11328 {
11329 struct type *type;
11330 struct type *to_type;
11331 struct type *domain;
11332
11333 to_type = die_type (die, cu);
11334 domain = die_containing_type (die, cu);
11335
11336 /* The calls above may have already set the type for this DIE. */
11337 type = get_die_type (die, cu);
11338 if (type)
11339 return type;
11340
11341 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11342 type = lookup_methodptr_type (to_type);
11343 else
11344 type = lookup_memberptr_type (to_type, domain);
11345
11346 return set_die_type (die, type, cu);
11347 }
11348
11349 /* Extract all information from a DW_TAG_reference_type DIE and add to
11350 the user defined type vector. */
11351
11352 static struct type *
11353 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11354 {
11355 struct comp_unit_head *cu_header = &cu->header;
11356 struct type *type, *target_type;
11357 struct attribute *attr;
11358
11359 target_type = die_type (die, cu);
11360
11361 /* The die_type call above may have already set the type for this DIE. */
11362 type = get_die_type (die, cu);
11363 if (type)
11364 return type;
11365
11366 type = lookup_reference_type (target_type);
11367 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11368 if (attr)
11369 {
11370 TYPE_LENGTH (type) = DW_UNSND (attr);
11371 }
11372 else
11373 {
11374 TYPE_LENGTH (type) = cu_header->addr_size;
11375 }
11376 return set_die_type (die, type, cu);
11377 }
11378
11379 static struct type *
11380 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11381 {
11382 struct type *base_type, *cv_type;
11383
11384 base_type = die_type (die, cu);
11385
11386 /* The die_type call above may have already set the type for this DIE. */
11387 cv_type = get_die_type (die, cu);
11388 if (cv_type)
11389 return cv_type;
11390
11391 /* In case the const qualifier is applied to an array type, the element type
11392 is so qualified, not the array type (section 6.7.3 of C99). */
11393 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11394 {
11395 struct type *el_type, *inner_array;
11396
11397 base_type = copy_type (base_type);
11398 inner_array = base_type;
11399
11400 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11401 {
11402 TYPE_TARGET_TYPE (inner_array) =
11403 copy_type (TYPE_TARGET_TYPE (inner_array));
11404 inner_array = TYPE_TARGET_TYPE (inner_array);
11405 }
11406
11407 el_type = TYPE_TARGET_TYPE (inner_array);
11408 TYPE_TARGET_TYPE (inner_array) =
11409 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11410
11411 return set_die_type (die, base_type, cu);
11412 }
11413
11414 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11415 return set_die_type (die, cv_type, cu);
11416 }
11417
11418 static struct type *
11419 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11420 {
11421 struct type *base_type, *cv_type;
11422
11423 base_type = die_type (die, cu);
11424
11425 /* The die_type call above may have already set the type for this DIE. */
11426 cv_type = get_die_type (die, cu);
11427 if (cv_type)
11428 return cv_type;
11429
11430 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11431 return set_die_type (die, cv_type, cu);
11432 }
11433
11434 /* Extract all information from a DW_TAG_string_type DIE and add to
11435 the user defined type vector. It isn't really a user defined type,
11436 but it behaves like one, with other DIE's using an AT_user_def_type
11437 attribute to reference it. */
11438
11439 static struct type *
11440 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11441 {
11442 struct objfile *objfile = cu->objfile;
11443 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11444 struct type *type, *range_type, *index_type, *char_type;
11445 struct attribute *attr;
11446 unsigned int length;
11447
11448 attr = dwarf2_attr (die, DW_AT_string_length, cu);
11449 if (attr)
11450 {
11451 length = DW_UNSND (attr);
11452 }
11453 else
11454 {
11455 /* Check for the DW_AT_byte_size attribute. */
11456 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11457 if (attr)
11458 {
11459 length = DW_UNSND (attr);
11460 }
11461 else
11462 {
11463 length = 1;
11464 }
11465 }
11466
11467 index_type = objfile_type (objfile)->builtin_int;
11468 range_type = create_range_type (NULL, index_type, 1, length);
11469 char_type = language_string_char_type (cu->language_defn, gdbarch);
11470 type = create_string_type (NULL, char_type, range_type);
11471
11472 return set_die_type (die, type, cu);
11473 }
11474
11475 /* Handle DIES due to C code like:
11476
11477 struct foo
11478 {
11479 int (*funcp)(int a, long l);
11480 int b;
11481 };
11482
11483 ('funcp' generates a DW_TAG_subroutine_type DIE). */
11484
11485 static struct type *
11486 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11487 {
11488 struct objfile *objfile = cu->objfile;
11489 struct type *type; /* Type that this function returns. */
11490 struct type *ftype; /* Function that returns above type. */
11491 struct attribute *attr;
11492
11493 type = die_type (die, cu);
11494
11495 /* The die_type call above may have already set the type for this DIE. */
11496 ftype = get_die_type (die, cu);
11497 if (ftype)
11498 return ftype;
11499
11500 ftype = lookup_function_type (type);
11501
11502 /* All functions in C++, Pascal and Java have prototypes. */
11503 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11504 if ((attr && (DW_UNSND (attr) != 0))
11505 || cu->language == language_cplus
11506 || cu->language == language_java
11507 || cu->language == language_pascal)
11508 TYPE_PROTOTYPED (ftype) = 1;
11509 else if (producer_is_realview (cu->producer))
11510 /* RealView does not emit DW_AT_prototyped. We can not
11511 distinguish prototyped and unprototyped functions; default to
11512 prototyped, since that is more common in modern code (and
11513 RealView warns about unprototyped functions). */
11514 TYPE_PROTOTYPED (ftype) = 1;
11515
11516 /* Store the calling convention in the type if it's available in
11517 the subroutine die. Otherwise set the calling convention to
11518 the default value DW_CC_normal. */
11519 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11520 if (attr)
11521 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11522 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11523 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11524 else
11525 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11526
11527 /* We need to add the subroutine type to the die immediately so
11528 we don't infinitely recurse when dealing with parameters
11529 declared as the same subroutine type. */
11530 set_die_type (die, ftype, cu);
11531
11532 if (die->child != NULL)
11533 {
11534 struct type *void_type = objfile_type (objfile)->builtin_void;
11535 struct die_info *child_die;
11536 int nparams, iparams;
11537
11538 /* Count the number of parameters.
11539 FIXME: GDB currently ignores vararg functions, but knows about
11540 vararg member functions. */
11541 nparams = 0;
11542 child_die = die->child;
11543 while (child_die && child_die->tag)
11544 {
11545 if (child_die->tag == DW_TAG_formal_parameter)
11546 nparams++;
11547 else if (child_die->tag == DW_TAG_unspecified_parameters)
11548 TYPE_VARARGS (ftype) = 1;
11549 child_die = sibling_die (child_die);
11550 }
11551
11552 /* Allocate storage for parameters and fill them in. */
11553 TYPE_NFIELDS (ftype) = nparams;
11554 TYPE_FIELDS (ftype) = (struct field *)
11555 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11556
11557 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11558 even if we error out during the parameters reading below. */
11559 for (iparams = 0; iparams < nparams; iparams++)
11560 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11561
11562 iparams = 0;
11563 child_die = die->child;
11564 while (child_die && child_die->tag)
11565 {
11566 if (child_die->tag == DW_TAG_formal_parameter)
11567 {
11568 struct type *arg_type;
11569
11570 /* DWARF version 2 has no clean way to discern C++
11571 static and non-static member functions. G++ helps
11572 GDB by marking the first parameter for non-static
11573 member functions (which is the this pointer) as
11574 artificial. We pass this information to
11575 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11576
11577 DWARF version 3 added DW_AT_object_pointer, which GCC
11578 4.5 does not yet generate. */
11579 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11580 if (attr)
11581 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11582 else
11583 {
11584 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11585
11586 /* GCC/43521: In java, the formal parameter
11587 "this" is sometimes not marked with DW_AT_artificial. */
11588 if (cu->language == language_java)
11589 {
11590 const char *name = dwarf2_name (child_die, cu);
11591
11592 if (name && !strcmp (name, "this"))
11593 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11594 }
11595 }
11596 arg_type = die_type (child_die, cu);
11597
11598 /* RealView does not mark THIS as const, which the testsuite
11599 expects. GCC marks THIS as const in method definitions,
11600 but not in the class specifications (GCC PR 43053). */
11601 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11602 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11603 {
11604 int is_this = 0;
11605 struct dwarf2_cu *arg_cu = cu;
11606 const char *name = dwarf2_name (child_die, cu);
11607
11608 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11609 if (attr)
11610 {
11611 /* If the compiler emits this, use it. */
11612 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11613 is_this = 1;
11614 }
11615 else if (name && strcmp (name, "this") == 0)
11616 /* Function definitions will have the argument names. */
11617 is_this = 1;
11618 else if (name == NULL && iparams == 0)
11619 /* Declarations may not have the names, so like
11620 elsewhere in GDB, assume an artificial first
11621 argument is "this". */
11622 is_this = 1;
11623
11624 if (is_this)
11625 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11626 arg_type, 0);
11627 }
11628
11629 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11630 iparams++;
11631 }
11632 child_die = sibling_die (child_die);
11633 }
11634 }
11635
11636 return ftype;
11637 }
11638
11639 static struct type *
11640 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11641 {
11642 struct objfile *objfile = cu->objfile;
11643 const char *name = NULL;
11644 struct type *this_type, *target_type;
11645
11646 name = dwarf2_full_name (NULL, die, cu);
11647 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11648 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11649 TYPE_NAME (this_type) = (char *) name;
11650 set_die_type (die, this_type, cu);
11651 target_type = die_type (die, cu);
11652 if (target_type != this_type)
11653 TYPE_TARGET_TYPE (this_type) = target_type;
11654 else
11655 {
11656 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11657 spec and cause infinite loops in GDB. */
11658 complaint (&symfile_complaints,
11659 _("Self-referential DW_TAG_typedef "
11660 "- DIE at 0x%x [in module %s]"),
11661 die->offset.sect_off, objfile->name);
11662 TYPE_TARGET_TYPE (this_type) = NULL;
11663 }
11664 return this_type;
11665 }
11666
11667 /* Find a representation of a given base type and install
11668 it in the TYPE field of the die. */
11669
11670 static struct type *
11671 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11672 {
11673 struct objfile *objfile = cu->objfile;
11674 struct type *type;
11675 struct attribute *attr;
11676 int encoding = 0, size = 0;
11677 char *name;
11678 enum type_code code = TYPE_CODE_INT;
11679 int type_flags = 0;
11680 struct type *target_type = NULL;
11681
11682 attr = dwarf2_attr (die, DW_AT_encoding, cu);
11683 if (attr)
11684 {
11685 encoding = DW_UNSND (attr);
11686 }
11687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11688 if (attr)
11689 {
11690 size = DW_UNSND (attr);
11691 }
11692 name = dwarf2_name (die, cu);
11693 if (!name)
11694 {
11695 complaint (&symfile_complaints,
11696 _("DW_AT_name missing from DW_TAG_base_type"));
11697 }
11698
11699 switch (encoding)
11700 {
11701 case DW_ATE_address:
11702 /* Turn DW_ATE_address into a void * pointer. */
11703 code = TYPE_CODE_PTR;
11704 type_flags |= TYPE_FLAG_UNSIGNED;
11705 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11706 break;
11707 case DW_ATE_boolean:
11708 code = TYPE_CODE_BOOL;
11709 type_flags |= TYPE_FLAG_UNSIGNED;
11710 break;
11711 case DW_ATE_complex_float:
11712 code = TYPE_CODE_COMPLEX;
11713 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11714 break;
11715 case DW_ATE_decimal_float:
11716 code = TYPE_CODE_DECFLOAT;
11717 break;
11718 case DW_ATE_float:
11719 code = TYPE_CODE_FLT;
11720 break;
11721 case DW_ATE_signed:
11722 break;
11723 case DW_ATE_unsigned:
11724 type_flags |= TYPE_FLAG_UNSIGNED;
11725 if (cu->language == language_fortran
11726 && name
11727 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11728 code = TYPE_CODE_CHAR;
11729 break;
11730 case DW_ATE_signed_char:
11731 if (cu->language == language_ada || cu->language == language_m2
11732 || cu->language == language_pascal
11733 || cu->language == language_fortran)
11734 code = TYPE_CODE_CHAR;
11735 break;
11736 case DW_ATE_unsigned_char:
11737 if (cu->language == language_ada || cu->language == language_m2
11738 || cu->language == language_pascal
11739 || cu->language == language_fortran)
11740 code = TYPE_CODE_CHAR;
11741 type_flags |= TYPE_FLAG_UNSIGNED;
11742 break;
11743 case DW_ATE_UTF:
11744 /* We just treat this as an integer and then recognize the
11745 type by name elsewhere. */
11746 break;
11747
11748 default:
11749 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11750 dwarf_type_encoding_name (encoding));
11751 break;
11752 }
11753
11754 type = init_type (code, size, type_flags, NULL, objfile);
11755 TYPE_NAME (type) = name;
11756 TYPE_TARGET_TYPE (type) = target_type;
11757
11758 if (name && strcmp (name, "char") == 0)
11759 TYPE_NOSIGN (type) = 1;
11760
11761 return set_die_type (die, type, cu);
11762 }
11763
11764 /* Read the given DW_AT_subrange DIE. */
11765
11766 static struct type *
11767 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11768 {
11769 struct type *base_type;
11770 struct type *range_type;
11771 struct attribute *attr;
11772 LONGEST low, high;
11773 int low_default_is_valid;
11774 char *name;
11775 LONGEST negative_mask;
11776
11777 base_type = die_type (die, cu);
11778 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11779 check_typedef (base_type);
11780
11781 /* The die_type call above may have already set the type for this DIE. */
11782 range_type = get_die_type (die, cu);
11783 if (range_type)
11784 return range_type;
11785
11786 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11787 omitting DW_AT_lower_bound. */
11788 switch (cu->language)
11789 {
11790 case language_c:
11791 case language_cplus:
11792 low = 0;
11793 low_default_is_valid = 1;
11794 break;
11795 case language_fortran:
11796 low = 1;
11797 low_default_is_valid = 1;
11798 break;
11799 case language_d:
11800 case language_java:
11801 case language_objc:
11802 low = 0;
11803 low_default_is_valid = (cu->header.version >= 4);
11804 break;
11805 case language_ada:
11806 case language_m2:
11807 case language_pascal:
11808 low = 1;
11809 low_default_is_valid = (cu->header.version >= 4);
11810 break;
11811 default:
11812 low = 0;
11813 low_default_is_valid = 0;
11814 break;
11815 }
11816
11817 /* FIXME: For variable sized arrays either of these could be
11818 a variable rather than a constant value. We'll allow it,
11819 but we don't know how to handle it. */
11820 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11821 if (attr)
11822 low = dwarf2_get_attr_constant_value (attr, low);
11823 else if (!low_default_is_valid)
11824 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11825 "- DIE at 0x%x [in module %s]"),
11826 die->offset.sect_off, cu->objfile->name);
11827
11828 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11829 if (attr)
11830 {
11831 if (attr_form_is_block (attr) || is_ref_attr (attr))
11832 {
11833 /* GCC encodes arrays with unspecified or dynamic length
11834 with a DW_FORM_block1 attribute or a reference attribute.
11835 FIXME: GDB does not yet know how to handle dynamic
11836 arrays properly, treat them as arrays with unspecified
11837 length for now.
11838
11839 FIXME: jimb/2003-09-22: GDB does not really know
11840 how to handle arrays of unspecified length
11841 either; we just represent them as zero-length
11842 arrays. Choose an appropriate upper bound given
11843 the lower bound we've computed above. */
11844 high = low - 1;
11845 }
11846 else
11847 high = dwarf2_get_attr_constant_value (attr, 1);
11848 }
11849 else
11850 {
11851 attr = dwarf2_attr (die, DW_AT_count, cu);
11852 if (attr)
11853 {
11854 int count = dwarf2_get_attr_constant_value (attr, 1);
11855 high = low + count - 1;
11856 }
11857 else
11858 {
11859 /* Unspecified array length. */
11860 high = low - 1;
11861 }
11862 }
11863
11864 /* Dwarf-2 specifications explicitly allows to create subrange types
11865 without specifying a base type.
11866 In that case, the base type must be set to the type of
11867 the lower bound, upper bound or count, in that order, if any of these
11868 three attributes references an object that has a type.
11869 If no base type is found, the Dwarf-2 specifications say that
11870 a signed integer type of size equal to the size of an address should
11871 be used.
11872 For the following C code: `extern char gdb_int [];'
11873 GCC produces an empty range DIE.
11874 FIXME: muller/2010-05-28: Possible references to object for low bound,
11875 high bound or count are not yet handled by this code. */
11876 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11877 {
11878 struct objfile *objfile = cu->objfile;
11879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11880 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11881 struct type *int_type = objfile_type (objfile)->builtin_int;
11882
11883 /* Test "int", "long int", and "long long int" objfile types,
11884 and select the first one having a size above or equal to the
11885 architecture address size. */
11886 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11887 base_type = int_type;
11888 else
11889 {
11890 int_type = objfile_type (objfile)->builtin_long;
11891 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11892 base_type = int_type;
11893 else
11894 {
11895 int_type = objfile_type (objfile)->builtin_long_long;
11896 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11897 base_type = int_type;
11898 }
11899 }
11900 }
11901
11902 negative_mask =
11903 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11904 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11905 low |= negative_mask;
11906 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11907 high |= negative_mask;
11908
11909 range_type = create_range_type (NULL, base_type, low, high);
11910
11911 /* Mark arrays with dynamic length at least as an array of unspecified
11912 length. GDB could check the boundary but before it gets implemented at
11913 least allow accessing the array elements. */
11914 if (attr && attr_form_is_block (attr))
11915 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11916
11917 /* Ada expects an empty array on no boundary attributes. */
11918 if (attr == NULL && cu->language != language_ada)
11919 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11920
11921 name = dwarf2_name (die, cu);
11922 if (name)
11923 TYPE_NAME (range_type) = name;
11924
11925 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11926 if (attr)
11927 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11928
11929 set_die_type (die, range_type, cu);
11930
11931 /* set_die_type should be already done. */
11932 set_descriptive_type (range_type, die, cu);
11933
11934 return range_type;
11935 }
11936
11937 static struct type *
11938 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11939 {
11940 struct type *type;
11941
11942 /* For now, we only support the C meaning of an unspecified type: void. */
11943
11944 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11945 TYPE_NAME (type) = dwarf2_name (die, cu);
11946
11947 return set_die_type (die, type, cu);
11948 }
11949
11950 /* Read a single die and all its descendents. Set the die's sibling
11951 field to NULL; set other fields in the die correctly, and set all
11952 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11953 location of the info_ptr after reading all of those dies. PARENT
11954 is the parent of the die in question. */
11955
11956 static struct die_info *
11957 read_die_and_children (const struct die_reader_specs *reader,
11958 gdb_byte *info_ptr,
11959 gdb_byte **new_info_ptr,
11960 struct die_info *parent)
11961 {
11962 struct die_info *die;
11963 gdb_byte *cur_ptr;
11964 int has_children;
11965
11966 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11967 if (die == NULL)
11968 {
11969 *new_info_ptr = cur_ptr;
11970 return NULL;
11971 }
11972 store_in_ref_table (die, reader->cu);
11973
11974 if (has_children)
11975 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11976 else
11977 {
11978 die->child = NULL;
11979 *new_info_ptr = cur_ptr;
11980 }
11981
11982 die->sibling = NULL;
11983 die->parent = parent;
11984 return die;
11985 }
11986
11987 /* Read a die, all of its descendents, and all of its siblings; set
11988 all of the fields of all of the dies correctly. Arguments are as
11989 in read_die_and_children. */
11990
11991 static struct die_info *
11992 read_die_and_siblings (const struct die_reader_specs *reader,
11993 gdb_byte *info_ptr,
11994 gdb_byte **new_info_ptr,
11995 struct die_info *parent)
11996 {
11997 struct die_info *first_die, *last_sibling;
11998 gdb_byte *cur_ptr;
11999
12000 cur_ptr = info_ptr;
12001 first_die = last_sibling = NULL;
12002
12003 while (1)
12004 {
12005 struct die_info *die
12006 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12007
12008 if (die == NULL)
12009 {
12010 *new_info_ptr = cur_ptr;
12011 return first_die;
12012 }
12013
12014 if (!first_die)
12015 first_die = die;
12016 else
12017 last_sibling->sibling = die;
12018
12019 last_sibling = die;
12020 }
12021 }
12022
12023 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12024 attributes.
12025 The caller is responsible for filling in the extra attributes
12026 and updating (*DIEP)->num_attrs.
12027 Set DIEP to point to a newly allocated die with its information,
12028 except for its child, sibling, and parent fields.
12029 Set HAS_CHILDREN to tell whether the die has children or not. */
12030
12031 static gdb_byte *
12032 read_full_die_1 (const struct die_reader_specs *reader,
12033 struct die_info **diep, gdb_byte *info_ptr,
12034 int *has_children, int num_extra_attrs)
12035 {
12036 unsigned int abbrev_number, bytes_read, i;
12037 sect_offset offset;
12038 struct abbrev_info *abbrev;
12039 struct die_info *die;
12040 struct dwarf2_cu *cu = reader->cu;
12041 bfd *abfd = reader->abfd;
12042
12043 offset.sect_off = info_ptr - reader->buffer;
12044 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12045 info_ptr += bytes_read;
12046 if (!abbrev_number)
12047 {
12048 *diep = NULL;
12049 *has_children = 0;
12050 return info_ptr;
12051 }
12052
12053 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12054 if (!abbrev)
12055 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12056 abbrev_number,
12057 bfd_get_filename (abfd));
12058
12059 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12060 die->offset = offset;
12061 die->tag = abbrev->tag;
12062 die->abbrev = abbrev_number;
12063
12064 /* Make the result usable.
12065 The caller needs to update num_attrs after adding the extra
12066 attributes. */
12067 die->num_attrs = abbrev->num_attrs;
12068
12069 for (i = 0; i < abbrev->num_attrs; ++i)
12070 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12071 info_ptr);
12072
12073 *diep = die;
12074 *has_children = abbrev->has_children;
12075 return info_ptr;
12076 }
12077
12078 /* Read a die and all its attributes.
12079 Set DIEP to point to a newly allocated die with its information,
12080 except for its child, sibling, and parent fields.
12081 Set HAS_CHILDREN to tell whether the die has children or not. */
12082
12083 static gdb_byte *
12084 read_full_die (const struct die_reader_specs *reader,
12085 struct die_info **diep, gdb_byte *info_ptr,
12086 int *has_children)
12087 {
12088 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12089 }
12090 \f
12091 /* Abbreviation tables.
12092
12093 In DWARF version 2, the description of the debugging information is
12094 stored in a separate .debug_abbrev section. Before we read any
12095 dies from a section we read in all abbreviations and install them
12096 in a hash table. */
12097
12098 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12099
12100 static struct abbrev_info *
12101 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12102 {
12103 struct abbrev_info *abbrev;
12104
12105 abbrev = (struct abbrev_info *)
12106 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12107 memset (abbrev, 0, sizeof (struct abbrev_info));
12108 return abbrev;
12109 }
12110
12111 /* Add an abbreviation to the table. */
12112
12113 static void
12114 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12115 unsigned int abbrev_number,
12116 struct abbrev_info *abbrev)
12117 {
12118 unsigned int hash_number;
12119
12120 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12121 abbrev->next = abbrev_table->abbrevs[hash_number];
12122 abbrev_table->abbrevs[hash_number] = abbrev;
12123 }
12124
12125 /* Look up an abbrev in the table.
12126 Returns NULL if the abbrev is not found. */
12127
12128 static struct abbrev_info *
12129 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12130 unsigned int abbrev_number)
12131 {
12132 unsigned int hash_number;
12133 struct abbrev_info *abbrev;
12134
12135 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12136 abbrev = abbrev_table->abbrevs[hash_number];
12137
12138 while (abbrev)
12139 {
12140 if (abbrev->number == abbrev_number)
12141 return abbrev;
12142 abbrev = abbrev->next;
12143 }
12144 return NULL;
12145 }
12146
12147 /* Read in an abbrev table. */
12148
12149 static struct abbrev_table *
12150 abbrev_table_read_table (struct dwarf2_section_info *section,
12151 sect_offset offset)
12152 {
12153 struct objfile *objfile = dwarf2_per_objfile->objfile;
12154 bfd *abfd = section->asection->owner;
12155 struct abbrev_table *abbrev_table;
12156 gdb_byte *abbrev_ptr;
12157 struct abbrev_info *cur_abbrev;
12158 unsigned int abbrev_number, bytes_read, abbrev_name;
12159 unsigned int abbrev_form;
12160 struct attr_abbrev *cur_attrs;
12161 unsigned int allocated_attrs;
12162
12163 abbrev_table = XMALLOC (struct abbrev_table);
12164 abbrev_table->offset = offset;
12165 obstack_init (&abbrev_table->abbrev_obstack);
12166 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12167 (ABBREV_HASH_SIZE
12168 * sizeof (struct abbrev_info *)));
12169 memset (abbrev_table->abbrevs, 0,
12170 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12171
12172 dwarf2_read_section (objfile, section);
12173 abbrev_ptr = section->buffer + offset.sect_off;
12174 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12175 abbrev_ptr += bytes_read;
12176
12177 allocated_attrs = ATTR_ALLOC_CHUNK;
12178 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12179
12180 /* Loop until we reach an abbrev number of 0. */
12181 while (abbrev_number)
12182 {
12183 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12184
12185 /* read in abbrev header */
12186 cur_abbrev->number = abbrev_number;
12187 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12188 abbrev_ptr += bytes_read;
12189 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12190 abbrev_ptr += 1;
12191
12192 /* now read in declarations */
12193 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12194 abbrev_ptr += bytes_read;
12195 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12196 abbrev_ptr += bytes_read;
12197 while (abbrev_name)
12198 {
12199 if (cur_abbrev->num_attrs == allocated_attrs)
12200 {
12201 allocated_attrs += ATTR_ALLOC_CHUNK;
12202 cur_attrs
12203 = xrealloc (cur_attrs, (allocated_attrs
12204 * sizeof (struct attr_abbrev)));
12205 }
12206
12207 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12208 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12209 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12210 abbrev_ptr += bytes_read;
12211 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12212 abbrev_ptr += bytes_read;
12213 }
12214
12215 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12216 (cur_abbrev->num_attrs
12217 * sizeof (struct attr_abbrev)));
12218 memcpy (cur_abbrev->attrs, cur_attrs,
12219 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12220
12221 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12222
12223 /* Get next abbreviation.
12224 Under Irix6 the abbreviations for a compilation unit are not
12225 always properly terminated with an abbrev number of 0.
12226 Exit loop if we encounter an abbreviation which we have
12227 already read (which means we are about to read the abbreviations
12228 for the next compile unit) or if the end of the abbreviation
12229 table is reached. */
12230 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12231 break;
12232 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12233 abbrev_ptr += bytes_read;
12234 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12235 break;
12236 }
12237
12238 xfree (cur_attrs);
12239 return abbrev_table;
12240 }
12241
12242 /* Free the resources held by ABBREV_TABLE. */
12243
12244 static void
12245 abbrev_table_free (struct abbrev_table *abbrev_table)
12246 {
12247 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12248 xfree (abbrev_table);
12249 }
12250
12251 /* Same as abbrev_table_free but as a cleanup.
12252 We pass in a pointer to the pointer to the table so that we can
12253 set the pointer to NULL when we're done. It also simplifies
12254 build_type_unit_groups. */
12255
12256 static void
12257 abbrev_table_free_cleanup (void *table_ptr)
12258 {
12259 struct abbrev_table **abbrev_table_ptr = table_ptr;
12260
12261 if (*abbrev_table_ptr != NULL)
12262 abbrev_table_free (*abbrev_table_ptr);
12263 *abbrev_table_ptr = NULL;
12264 }
12265
12266 /* Read the abbrev table for CU from ABBREV_SECTION. */
12267
12268 static void
12269 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12270 struct dwarf2_section_info *abbrev_section)
12271 {
12272 cu->abbrev_table =
12273 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12274 }
12275
12276 /* Release the memory used by the abbrev table for a compilation unit. */
12277
12278 static void
12279 dwarf2_free_abbrev_table (void *ptr_to_cu)
12280 {
12281 struct dwarf2_cu *cu = ptr_to_cu;
12282
12283 abbrev_table_free (cu->abbrev_table);
12284 /* Set this to NULL so that we SEGV if we try to read it later,
12285 and also because free_comp_unit verifies this is NULL. */
12286 cu->abbrev_table = NULL;
12287 }
12288 \f
12289 /* Returns nonzero if TAG represents a type that we might generate a partial
12290 symbol for. */
12291
12292 static int
12293 is_type_tag_for_partial (int tag)
12294 {
12295 switch (tag)
12296 {
12297 #if 0
12298 /* Some types that would be reasonable to generate partial symbols for,
12299 that we don't at present. */
12300 case DW_TAG_array_type:
12301 case DW_TAG_file_type:
12302 case DW_TAG_ptr_to_member_type:
12303 case DW_TAG_set_type:
12304 case DW_TAG_string_type:
12305 case DW_TAG_subroutine_type:
12306 #endif
12307 case DW_TAG_base_type:
12308 case DW_TAG_class_type:
12309 case DW_TAG_interface_type:
12310 case DW_TAG_enumeration_type:
12311 case DW_TAG_structure_type:
12312 case DW_TAG_subrange_type:
12313 case DW_TAG_typedef:
12314 case DW_TAG_union_type:
12315 return 1;
12316 default:
12317 return 0;
12318 }
12319 }
12320
12321 /* Load all DIEs that are interesting for partial symbols into memory. */
12322
12323 static struct partial_die_info *
12324 load_partial_dies (const struct die_reader_specs *reader,
12325 gdb_byte *info_ptr, int building_psymtab)
12326 {
12327 struct dwarf2_cu *cu = reader->cu;
12328 struct objfile *objfile = cu->objfile;
12329 struct partial_die_info *part_die;
12330 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12331 struct abbrev_info *abbrev;
12332 unsigned int bytes_read;
12333 unsigned int load_all = 0;
12334 int nesting_level = 1;
12335
12336 parent_die = NULL;
12337 last_die = NULL;
12338
12339 gdb_assert (cu->per_cu != NULL);
12340 if (cu->per_cu->load_all_dies)
12341 load_all = 1;
12342
12343 cu->partial_dies
12344 = htab_create_alloc_ex (cu->header.length / 12,
12345 partial_die_hash,
12346 partial_die_eq,
12347 NULL,
12348 &cu->comp_unit_obstack,
12349 hashtab_obstack_allocate,
12350 dummy_obstack_deallocate);
12351
12352 part_die = obstack_alloc (&cu->comp_unit_obstack,
12353 sizeof (struct partial_die_info));
12354
12355 while (1)
12356 {
12357 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12358
12359 /* A NULL abbrev means the end of a series of children. */
12360 if (abbrev == NULL)
12361 {
12362 if (--nesting_level == 0)
12363 {
12364 /* PART_DIE was probably the last thing allocated on the
12365 comp_unit_obstack, so we could call obstack_free
12366 here. We don't do that because the waste is small,
12367 and will be cleaned up when we're done with this
12368 compilation unit. This way, we're also more robust
12369 against other users of the comp_unit_obstack. */
12370 return first_die;
12371 }
12372 info_ptr += bytes_read;
12373 last_die = parent_die;
12374 parent_die = parent_die->die_parent;
12375 continue;
12376 }
12377
12378 /* Check for template arguments. We never save these; if
12379 they're seen, we just mark the parent, and go on our way. */
12380 if (parent_die != NULL
12381 && cu->language == language_cplus
12382 && (abbrev->tag == DW_TAG_template_type_param
12383 || abbrev->tag == DW_TAG_template_value_param))
12384 {
12385 parent_die->has_template_arguments = 1;
12386
12387 if (!load_all)
12388 {
12389 /* We don't need a partial DIE for the template argument. */
12390 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12391 continue;
12392 }
12393 }
12394
12395 /* We only recurse into c++ subprograms looking for template arguments.
12396 Skip their other children. */
12397 if (!load_all
12398 && cu->language == language_cplus
12399 && parent_die != NULL
12400 && parent_die->tag == DW_TAG_subprogram)
12401 {
12402 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12403 continue;
12404 }
12405
12406 /* Check whether this DIE is interesting enough to save. Normally
12407 we would not be interested in members here, but there may be
12408 later variables referencing them via DW_AT_specification (for
12409 static members). */
12410 if (!load_all
12411 && !is_type_tag_for_partial (abbrev->tag)
12412 && abbrev->tag != DW_TAG_constant
12413 && abbrev->tag != DW_TAG_enumerator
12414 && abbrev->tag != DW_TAG_subprogram
12415 && abbrev->tag != DW_TAG_lexical_block
12416 && abbrev->tag != DW_TAG_variable
12417 && abbrev->tag != DW_TAG_namespace
12418 && abbrev->tag != DW_TAG_module
12419 && abbrev->tag != DW_TAG_member
12420 && abbrev->tag != DW_TAG_imported_unit)
12421 {
12422 /* Otherwise we skip to the next sibling, if any. */
12423 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12424 continue;
12425 }
12426
12427 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12428 info_ptr);
12429
12430 /* This two-pass algorithm for processing partial symbols has a
12431 high cost in cache pressure. Thus, handle some simple cases
12432 here which cover the majority of C partial symbols. DIEs
12433 which neither have specification tags in them, nor could have
12434 specification tags elsewhere pointing at them, can simply be
12435 processed and discarded.
12436
12437 This segment is also optional; scan_partial_symbols and
12438 add_partial_symbol will handle these DIEs if we chain
12439 them in normally. When compilers which do not emit large
12440 quantities of duplicate debug information are more common,
12441 this code can probably be removed. */
12442
12443 /* Any complete simple types at the top level (pretty much all
12444 of them, for a language without namespaces), can be processed
12445 directly. */
12446 if (parent_die == NULL
12447 && part_die->has_specification == 0
12448 && part_die->is_declaration == 0
12449 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12450 || part_die->tag == DW_TAG_base_type
12451 || part_die->tag == DW_TAG_subrange_type))
12452 {
12453 if (building_psymtab && part_die->name != NULL)
12454 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12455 VAR_DOMAIN, LOC_TYPEDEF,
12456 &objfile->static_psymbols,
12457 0, (CORE_ADDR) 0, cu->language, objfile);
12458 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12459 continue;
12460 }
12461
12462 /* The exception for DW_TAG_typedef with has_children above is
12463 a workaround of GCC PR debug/47510. In the case of this complaint
12464 type_name_no_tag_or_error will error on such types later.
12465
12466 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12467 it could not find the child DIEs referenced later, this is checked
12468 above. In correct DWARF DW_TAG_typedef should have no children. */
12469
12470 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12471 complaint (&symfile_complaints,
12472 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12473 "- DIE at 0x%x [in module %s]"),
12474 part_die->offset.sect_off, objfile->name);
12475
12476 /* If we're at the second level, and we're an enumerator, and
12477 our parent has no specification (meaning possibly lives in a
12478 namespace elsewhere), then we can add the partial symbol now
12479 instead of queueing it. */
12480 if (part_die->tag == DW_TAG_enumerator
12481 && parent_die != NULL
12482 && parent_die->die_parent == NULL
12483 && parent_die->tag == DW_TAG_enumeration_type
12484 && parent_die->has_specification == 0)
12485 {
12486 if (part_die->name == NULL)
12487 complaint (&symfile_complaints,
12488 _("malformed enumerator DIE ignored"));
12489 else if (building_psymtab)
12490 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12491 VAR_DOMAIN, LOC_CONST,
12492 (cu->language == language_cplus
12493 || cu->language == language_java)
12494 ? &objfile->global_psymbols
12495 : &objfile->static_psymbols,
12496 0, (CORE_ADDR) 0, cu->language, objfile);
12497
12498 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12499 continue;
12500 }
12501
12502 /* We'll save this DIE so link it in. */
12503 part_die->die_parent = parent_die;
12504 part_die->die_sibling = NULL;
12505 part_die->die_child = NULL;
12506
12507 if (last_die && last_die == parent_die)
12508 last_die->die_child = part_die;
12509 else if (last_die)
12510 last_die->die_sibling = part_die;
12511
12512 last_die = part_die;
12513
12514 if (first_die == NULL)
12515 first_die = part_die;
12516
12517 /* Maybe add the DIE to the hash table. Not all DIEs that we
12518 find interesting need to be in the hash table, because we
12519 also have the parent/sibling/child chains; only those that we
12520 might refer to by offset later during partial symbol reading.
12521
12522 For now this means things that might have be the target of a
12523 DW_AT_specification, DW_AT_abstract_origin, or
12524 DW_AT_extension. DW_AT_extension will refer only to
12525 namespaces; DW_AT_abstract_origin refers to functions (and
12526 many things under the function DIE, but we do not recurse
12527 into function DIEs during partial symbol reading) and
12528 possibly variables as well; DW_AT_specification refers to
12529 declarations. Declarations ought to have the DW_AT_declaration
12530 flag. It happens that GCC forgets to put it in sometimes, but
12531 only for functions, not for types.
12532
12533 Adding more things than necessary to the hash table is harmless
12534 except for the performance cost. Adding too few will result in
12535 wasted time in find_partial_die, when we reread the compilation
12536 unit with load_all_dies set. */
12537
12538 if (load_all
12539 || abbrev->tag == DW_TAG_constant
12540 || abbrev->tag == DW_TAG_subprogram
12541 || abbrev->tag == DW_TAG_variable
12542 || abbrev->tag == DW_TAG_namespace
12543 || part_die->is_declaration)
12544 {
12545 void **slot;
12546
12547 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12548 part_die->offset.sect_off, INSERT);
12549 *slot = part_die;
12550 }
12551
12552 part_die = obstack_alloc (&cu->comp_unit_obstack,
12553 sizeof (struct partial_die_info));
12554
12555 /* For some DIEs we want to follow their children (if any). For C
12556 we have no reason to follow the children of structures; for other
12557 languages we have to, so that we can get at method physnames
12558 to infer fully qualified class names, for DW_AT_specification,
12559 and for C++ template arguments. For C++, we also look one level
12560 inside functions to find template arguments (if the name of the
12561 function does not already contain the template arguments).
12562
12563 For Ada, we need to scan the children of subprograms and lexical
12564 blocks as well because Ada allows the definition of nested
12565 entities that could be interesting for the debugger, such as
12566 nested subprograms for instance. */
12567 if (last_die->has_children
12568 && (load_all
12569 || last_die->tag == DW_TAG_namespace
12570 || last_die->tag == DW_TAG_module
12571 || last_die->tag == DW_TAG_enumeration_type
12572 || (cu->language == language_cplus
12573 && last_die->tag == DW_TAG_subprogram
12574 && (last_die->name == NULL
12575 || strchr (last_die->name, '<') == NULL))
12576 || (cu->language != language_c
12577 && (last_die->tag == DW_TAG_class_type
12578 || last_die->tag == DW_TAG_interface_type
12579 || last_die->tag == DW_TAG_structure_type
12580 || last_die->tag == DW_TAG_union_type))
12581 || (cu->language == language_ada
12582 && (last_die->tag == DW_TAG_subprogram
12583 || last_die->tag == DW_TAG_lexical_block))))
12584 {
12585 nesting_level++;
12586 parent_die = last_die;
12587 continue;
12588 }
12589
12590 /* Otherwise we skip to the next sibling, if any. */
12591 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12592
12593 /* Back to the top, do it again. */
12594 }
12595 }
12596
12597 /* Read a minimal amount of information into the minimal die structure. */
12598
12599 static gdb_byte *
12600 read_partial_die (const struct die_reader_specs *reader,
12601 struct partial_die_info *part_die,
12602 struct abbrev_info *abbrev, unsigned int abbrev_len,
12603 gdb_byte *info_ptr)
12604 {
12605 struct dwarf2_cu *cu = reader->cu;
12606 struct objfile *objfile = cu->objfile;
12607 gdb_byte *buffer = reader->buffer;
12608 unsigned int i;
12609 struct attribute attr;
12610 int has_low_pc_attr = 0;
12611 int has_high_pc_attr = 0;
12612 int high_pc_relative = 0;
12613
12614 memset (part_die, 0, sizeof (struct partial_die_info));
12615
12616 part_die->offset.sect_off = info_ptr - buffer;
12617
12618 info_ptr += abbrev_len;
12619
12620 if (abbrev == NULL)
12621 return info_ptr;
12622
12623 part_die->tag = abbrev->tag;
12624 part_die->has_children = abbrev->has_children;
12625
12626 for (i = 0; i < abbrev->num_attrs; ++i)
12627 {
12628 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12629
12630 /* Store the data if it is of an attribute we want to keep in a
12631 partial symbol table. */
12632 switch (attr.name)
12633 {
12634 case DW_AT_name:
12635 switch (part_die->tag)
12636 {
12637 case DW_TAG_compile_unit:
12638 case DW_TAG_partial_unit:
12639 case DW_TAG_type_unit:
12640 /* Compilation units have a DW_AT_name that is a filename, not
12641 a source language identifier. */
12642 case DW_TAG_enumeration_type:
12643 case DW_TAG_enumerator:
12644 /* These tags always have simple identifiers already; no need
12645 to canonicalize them. */
12646 part_die->name = DW_STRING (&attr);
12647 break;
12648 default:
12649 part_die->name
12650 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12651 &objfile->objfile_obstack);
12652 break;
12653 }
12654 break;
12655 case DW_AT_linkage_name:
12656 case DW_AT_MIPS_linkage_name:
12657 /* Note that both forms of linkage name might appear. We
12658 assume they will be the same, and we only store the last
12659 one we see. */
12660 if (cu->language == language_ada)
12661 part_die->name = DW_STRING (&attr);
12662 part_die->linkage_name = DW_STRING (&attr);
12663 break;
12664 case DW_AT_low_pc:
12665 has_low_pc_attr = 1;
12666 part_die->lowpc = DW_ADDR (&attr);
12667 break;
12668 case DW_AT_high_pc:
12669 has_high_pc_attr = 1;
12670 if (attr.form == DW_FORM_addr
12671 || attr.form == DW_FORM_GNU_addr_index)
12672 part_die->highpc = DW_ADDR (&attr);
12673 else
12674 {
12675 high_pc_relative = 1;
12676 part_die->highpc = DW_UNSND (&attr);
12677 }
12678 break;
12679 case DW_AT_location:
12680 /* Support the .debug_loc offsets. */
12681 if (attr_form_is_block (&attr))
12682 {
12683 part_die->d.locdesc = DW_BLOCK (&attr);
12684 }
12685 else if (attr_form_is_section_offset (&attr))
12686 {
12687 dwarf2_complex_location_expr_complaint ();
12688 }
12689 else
12690 {
12691 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12692 "partial symbol information");
12693 }
12694 break;
12695 case DW_AT_external:
12696 part_die->is_external = DW_UNSND (&attr);
12697 break;
12698 case DW_AT_declaration:
12699 part_die->is_declaration = DW_UNSND (&attr);
12700 break;
12701 case DW_AT_type:
12702 part_die->has_type = 1;
12703 break;
12704 case DW_AT_abstract_origin:
12705 case DW_AT_specification:
12706 case DW_AT_extension:
12707 part_die->has_specification = 1;
12708 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12709 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12710 || cu->per_cu->is_dwz);
12711 break;
12712 case DW_AT_sibling:
12713 /* Ignore absolute siblings, they might point outside of
12714 the current compile unit. */
12715 if (attr.form == DW_FORM_ref_addr)
12716 complaint (&symfile_complaints,
12717 _("ignoring absolute DW_AT_sibling"));
12718 else
12719 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12720 break;
12721 case DW_AT_byte_size:
12722 part_die->has_byte_size = 1;
12723 break;
12724 case DW_AT_calling_convention:
12725 /* DWARF doesn't provide a way to identify a program's source-level
12726 entry point. DW_AT_calling_convention attributes are only meant
12727 to describe functions' calling conventions.
12728
12729 However, because it's a necessary piece of information in
12730 Fortran, and because DW_CC_program is the only piece of debugging
12731 information whose definition refers to a 'main program' at all,
12732 several compilers have begun marking Fortran main programs with
12733 DW_CC_program --- even when those functions use the standard
12734 calling conventions.
12735
12736 So until DWARF specifies a way to provide this information and
12737 compilers pick up the new representation, we'll support this
12738 practice. */
12739 if (DW_UNSND (&attr) == DW_CC_program
12740 && cu->language == language_fortran)
12741 {
12742 set_main_name (part_die->name);
12743
12744 /* As this DIE has a static linkage the name would be difficult
12745 to look up later. */
12746 language_of_main = language_fortran;
12747 }
12748 break;
12749 case DW_AT_inline:
12750 if (DW_UNSND (&attr) == DW_INL_inlined
12751 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12752 part_die->may_be_inlined = 1;
12753 break;
12754
12755 case DW_AT_import:
12756 if (part_die->tag == DW_TAG_imported_unit)
12757 {
12758 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12759 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12760 || cu->per_cu->is_dwz);
12761 }
12762 break;
12763
12764 default:
12765 break;
12766 }
12767 }
12768
12769 if (high_pc_relative)
12770 part_die->highpc += part_die->lowpc;
12771
12772 if (has_low_pc_attr && has_high_pc_attr)
12773 {
12774 /* When using the GNU linker, .gnu.linkonce. sections are used to
12775 eliminate duplicate copies of functions and vtables and such.
12776 The linker will arbitrarily choose one and discard the others.
12777 The AT_*_pc values for such functions refer to local labels in
12778 these sections. If the section from that file was discarded, the
12779 labels are not in the output, so the relocs get a value of 0.
12780 If this is a discarded function, mark the pc bounds as invalid,
12781 so that GDB will ignore it. */
12782 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12783 {
12784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12785
12786 complaint (&symfile_complaints,
12787 _("DW_AT_low_pc %s is zero "
12788 "for DIE at 0x%x [in module %s]"),
12789 paddress (gdbarch, part_die->lowpc),
12790 part_die->offset.sect_off, objfile->name);
12791 }
12792 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12793 else if (part_die->lowpc >= part_die->highpc)
12794 {
12795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12796
12797 complaint (&symfile_complaints,
12798 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12799 "for DIE at 0x%x [in module %s]"),
12800 paddress (gdbarch, part_die->lowpc),
12801 paddress (gdbarch, part_die->highpc),
12802 part_die->offset.sect_off, objfile->name);
12803 }
12804 else
12805 part_die->has_pc_info = 1;
12806 }
12807
12808 return info_ptr;
12809 }
12810
12811 /* Find a cached partial DIE at OFFSET in CU. */
12812
12813 static struct partial_die_info *
12814 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12815 {
12816 struct partial_die_info *lookup_die = NULL;
12817 struct partial_die_info part_die;
12818
12819 part_die.offset = offset;
12820 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12821 offset.sect_off);
12822
12823 return lookup_die;
12824 }
12825
12826 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12827 except in the case of .debug_types DIEs which do not reference
12828 outside their CU (they do however referencing other types via
12829 DW_FORM_ref_sig8). */
12830
12831 static struct partial_die_info *
12832 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
12833 {
12834 struct objfile *objfile = cu->objfile;
12835 struct dwarf2_per_cu_data *per_cu = NULL;
12836 struct partial_die_info *pd = NULL;
12837
12838 if (offset_in_dwz == cu->per_cu->is_dwz
12839 && offset_in_cu_p (&cu->header, offset))
12840 {
12841 pd = find_partial_die_in_comp_unit (offset, cu);
12842 if (pd != NULL)
12843 return pd;
12844 /* We missed recording what we needed.
12845 Load all dies and try again. */
12846 per_cu = cu->per_cu;
12847 }
12848 else
12849 {
12850 /* TUs don't reference other CUs/TUs (except via type signatures). */
12851 if (cu->per_cu->is_debug_types)
12852 {
12853 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12854 " external reference to offset 0x%lx [in module %s].\n"),
12855 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12856 bfd_get_filename (objfile->obfd));
12857 }
12858 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12859 objfile);
12860
12861 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12862 load_partial_comp_unit (per_cu);
12863
12864 per_cu->cu->last_used = 0;
12865 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12866 }
12867
12868 /* If we didn't find it, and not all dies have been loaded,
12869 load them all and try again. */
12870
12871 if (pd == NULL && per_cu->load_all_dies == 0)
12872 {
12873 per_cu->load_all_dies = 1;
12874
12875 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12876 THIS_CU->cu may already be in use. So we can't just free it and
12877 replace its DIEs with the ones we read in. Instead, we leave those
12878 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12879 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12880 set. */
12881 load_partial_comp_unit (per_cu);
12882
12883 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12884 }
12885
12886 if (pd == NULL)
12887 internal_error (__FILE__, __LINE__,
12888 _("could not find partial DIE 0x%x "
12889 "in cache [from module %s]\n"),
12890 offset.sect_off, bfd_get_filename (objfile->obfd));
12891 return pd;
12892 }
12893
12894 /* See if we can figure out if the class lives in a namespace. We do
12895 this by looking for a member function; its demangled name will
12896 contain namespace info, if there is any. */
12897
12898 static void
12899 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12900 struct dwarf2_cu *cu)
12901 {
12902 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12903 what template types look like, because the demangler
12904 frequently doesn't give the same name as the debug info. We
12905 could fix this by only using the demangled name to get the
12906 prefix (but see comment in read_structure_type). */
12907
12908 struct partial_die_info *real_pdi;
12909 struct partial_die_info *child_pdi;
12910
12911 /* If this DIE (this DIE's specification, if any) has a parent, then
12912 we should not do this. We'll prepend the parent's fully qualified
12913 name when we create the partial symbol. */
12914
12915 real_pdi = struct_pdi;
12916 while (real_pdi->has_specification)
12917 real_pdi = find_partial_die (real_pdi->spec_offset,
12918 real_pdi->spec_is_dwz, cu);
12919
12920 if (real_pdi->die_parent != NULL)
12921 return;
12922
12923 for (child_pdi = struct_pdi->die_child;
12924 child_pdi != NULL;
12925 child_pdi = child_pdi->die_sibling)
12926 {
12927 if (child_pdi->tag == DW_TAG_subprogram
12928 && child_pdi->linkage_name != NULL)
12929 {
12930 char *actual_class_name
12931 = language_class_name_from_physname (cu->language_defn,
12932 child_pdi->linkage_name);
12933 if (actual_class_name != NULL)
12934 {
12935 struct_pdi->name
12936 = obsavestring (actual_class_name,
12937 strlen (actual_class_name),
12938 &cu->objfile->objfile_obstack);
12939 xfree (actual_class_name);
12940 }
12941 break;
12942 }
12943 }
12944 }
12945
12946 /* Adjust PART_DIE before generating a symbol for it. This function
12947 may set the is_external flag or change the DIE's name. */
12948
12949 static void
12950 fixup_partial_die (struct partial_die_info *part_die,
12951 struct dwarf2_cu *cu)
12952 {
12953 /* Once we've fixed up a die, there's no point in doing so again.
12954 This also avoids a memory leak if we were to call
12955 guess_partial_die_structure_name multiple times. */
12956 if (part_die->fixup_called)
12957 return;
12958
12959 /* If we found a reference attribute and the DIE has no name, try
12960 to find a name in the referred to DIE. */
12961
12962 if (part_die->name == NULL && part_die->has_specification)
12963 {
12964 struct partial_die_info *spec_die;
12965
12966 spec_die = find_partial_die (part_die->spec_offset,
12967 part_die->spec_is_dwz, cu);
12968
12969 fixup_partial_die (spec_die, cu);
12970
12971 if (spec_die->name)
12972 {
12973 part_die->name = spec_die->name;
12974
12975 /* Copy DW_AT_external attribute if it is set. */
12976 if (spec_die->is_external)
12977 part_die->is_external = spec_die->is_external;
12978 }
12979 }
12980
12981 /* Set default names for some unnamed DIEs. */
12982
12983 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12984 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12985
12986 /* If there is no parent die to provide a namespace, and there are
12987 children, see if we can determine the namespace from their linkage
12988 name. */
12989 if (cu->language == language_cplus
12990 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12991 && part_die->die_parent == NULL
12992 && part_die->has_children
12993 && (part_die->tag == DW_TAG_class_type
12994 || part_die->tag == DW_TAG_structure_type
12995 || part_die->tag == DW_TAG_union_type))
12996 guess_partial_die_structure_name (part_die, cu);
12997
12998 /* GCC might emit a nameless struct or union that has a linkage
12999 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13000 if (part_die->name == NULL
13001 && (part_die->tag == DW_TAG_class_type
13002 || part_die->tag == DW_TAG_interface_type
13003 || part_die->tag == DW_TAG_structure_type
13004 || part_die->tag == DW_TAG_union_type)
13005 && part_die->linkage_name != NULL)
13006 {
13007 char *demangled;
13008
13009 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13010 if (demangled)
13011 {
13012 const char *base;
13013
13014 /* Strip any leading namespaces/classes, keep only the base name.
13015 DW_AT_name for named DIEs does not contain the prefixes. */
13016 base = strrchr (demangled, ':');
13017 if (base && base > demangled && base[-1] == ':')
13018 base++;
13019 else
13020 base = demangled;
13021
13022 part_die->name = obsavestring (base, strlen (base),
13023 &cu->objfile->objfile_obstack);
13024 xfree (demangled);
13025 }
13026 }
13027
13028 part_die->fixup_called = 1;
13029 }
13030
13031 /* Read an attribute value described by an attribute form. */
13032
13033 static gdb_byte *
13034 read_attribute_value (const struct die_reader_specs *reader,
13035 struct attribute *attr, unsigned form,
13036 gdb_byte *info_ptr)
13037 {
13038 struct dwarf2_cu *cu = reader->cu;
13039 bfd *abfd = reader->abfd;
13040 struct comp_unit_head *cu_header = &cu->header;
13041 unsigned int bytes_read;
13042 struct dwarf_block *blk;
13043
13044 attr->form = form;
13045 switch (form)
13046 {
13047 case DW_FORM_ref_addr:
13048 if (cu->header.version == 2)
13049 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13050 else
13051 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13052 &cu->header, &bytes_read);
13053 info_ptr += bytes_read;
13054 break;
13055 case DW_FORM_GNU_ref_alt:
13056 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13057 info_ptr += bytes_read;
13058 break;
13059 case DW_FORM_addr:
13060 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13061 info_ptr += bytes_read;
13062 break;
13063 case DW_FORM_block2:
13064 blk = dwarf_alloc_block (cu);
13065 blk->size = read_2_bytes (abfd, info_ptr);
13066 info_ptr += 2;
13067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13068 info_ptr += blk->size;
13069 DW_BLOCK (attr) = blk;
13070 break;
13071 case DW_FORM_block4:
13072 blk = dwarf_alloc_block (cu);
13073 blk->size = read_4_bytes (abfd, info_ptr);
13074 info_ptr += 4;
13075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13076 info_ptr += blk->size;
13077 DW_BLOCK (attr) = blk;
13078 break;
13079 case DW_FORM_data2:
13080 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13081 info_ptr += 2;
13082 break;
13083 case DW_FORM_data4:
13084 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13085 info_ptr += 4;
13086 break;
13087 case DW_FORM_data8:
13088 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13089 info_ptr += 8;
13090 break;
13091 case DW_FORM_sec_offset:
13092 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13093 info_ptr += bytes_read;
13094 break;
13095 case DW_FORM_string:
13096 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13097 DW_STRING_IS_CANONICAL (attr) = 0;
13098 info_ptr += bytes_read;
13099 break;
13100 case DW_FORM_strp:
13101 if (!cu->per_cu->is_dwz)
13102 {
13103 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13104 &bytes_read);
13105 DW_STRING_IS_CANONICAL (attr) = 0;
13106 info_ptr += bytes_read;
13107 break;
13108 }
13109 /* FALLTHROUGH */
13110 case DW_FORM_GNU_strp_alt:
13111 {
13112 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13113 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13114 &bytes_read);
13115
13116 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13117 DW_STRING_IS_CANONICAL (attr) = 0;
13118 info_ptr += bytes_read;
13119 }
13120 break;
13121 case DW_FORM_exprloc:
13122 case DW_FORM_block:
13123 blk = dwarf_alloc_block (cu);
13124 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13125 info_ptr += bytes_read;
13126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13127 info_ptr += blk->size;
13128 DW_BLOCK (attr) = blk;
13129 break;
13130 case DW_FORM_block1:
13131 blk = dwarf_alloc_block (cu);
13132 blk->size = read_1_byte (abfd, info_ptr);
13133 info_ptr += 1;
13134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13135 info_ptr += blk->size;
13136 DW_BLOCK (attr) = blk;
13137 break;
13138 case DW_FORM_data1:
13139 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13140 info_ptr += 1;
13141 break;
13142 case DW_FORM_flag:
13143 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13144 info_ptr += 1;
13145 break;
13146 case DW_FORM_flag_present:
13147 DW_UNSND (attr) = 1;
13148 break;
13149 case DW_FORM_sdata:
13150 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13151 info_ptr += bytes_read;
13152 break;
13153 case DW_FORM_udata:
13154 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13155 info_ptr += bytes_read;
13156 break;
13157 case DW_FORM_ref1:
13158 DW_UNSND (attr) = (cu->header.offset.sect_off
13159 + read_1_byte (abfd, info_ptr));
13160 info_ptr += 1;
13161 break;
13162 case DW_FORM_ref2:
13163 DW_UNSND (attr) = (cu->header.offset.sect_off
13164 + read_2_bytes (abfd, info_ptr));
13165 info_ptr += 2;
13166 break;
13167 case DW_FORM_ref4:
13168 DW_UNSND (attr) = (cu->header.offset.sect_off
13169 + read_4_bytes (abfd, info_ptr));
13170 info_ptr += 4;
13171 break;
13172 case DW_FORM_ref8:
13173 DW_UNSND (attr) = (cu->header.offset.sect_off
13174 + read_8_bytes (abfd, info_ptr));
13175 info_ptr += 8;
13176 break;
13177 case DW_FORM_ref_sig8:
13178 /* Convert the signature to something we can record in DW_UNSND
13179 for later lookup.
13180 NOTE: This is NULL if the type wasn't found. */
13181 DW_SIGNATURED_TYPE (attr) =
13182 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
13183 info_ptr += 8;
13184 break;
13185 case DW_FORM_ref_udata:
13186 DW_UNSND (attr) = (cu->header.offset.sect_off
13187 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
13188 info_ptr += bytes_read;
13189 break;
13190 case DW_FORM_indirect:
13191 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13192 info_ptr += bytes_read;
13193 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
13194 break;
13195 case DW_FORM_GNU_addr_index:
13196 if (reader->dwo_file == NULL)
13197 {
13198 /* For now flag a hard error.
13199 Later we can turn this into a complaint. */
13200 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13201 dwarf_form_name (form),
13202 bfd_get_filename (abfd));
13203 }
13204 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13205 info_ptr += bytes_read;
13206 break;
13207 case DW_FORM_GNU_str_index:
13208 if (reader->dwo_file == NULL)
13209 {
13210 /* For now flag a hard error.
13211 Later we can turn this into a complaint if warranted. */
13212 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13213 dwarf_form_name (form),
13214 bfd_get_filename (abfd));
13215 }
13216 {
13217 ULONGEST str_index =
13218 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13219
13220 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13221 DW_STRING_IS_CANONICAL (attr) = 0;
13222 info_ptr += bytes_read;
13223 }
13224 break;
13225 default:
13226 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13227 dwarf_form_name (form),
13228 bfd_get_filename (abfd));
13229 }
13230
13231 /* Super hack. */
13232 if (cu->per_cu->is_dwz && is_ref_attr (attr))
13233 attr->form = DW_FORM_GNU_ref_alt;
13234
13235 /* We have seen instances where the compiler tried to emit a byte
13236 size attribute of -1 which ended up being encoded as an unsigned
13237 0xffffffff. Although 0xffffffff is technically a valid size value,
13238 an object of this size seems pretty unlikely so we can relatively
13239 safely treat these cases as if the size attribute was invalid and
13240 treat them as zero by default. */
13241 if (attr->name == DW_AT_byte_size
13242 && form == DW_FORM_data4
13243 && DW_UNSND (attr) >= 0xffffffff)
13244 {
13245 complaint
13246 (&symfile_complaints,
13247 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13248 hex_string (DW_UNSND (attr)));
13249 DW_UNSND (attr) = 0;
13250 }
13251
13252 return info_ptr;
13253 }
13254
13255 /* Read an attribute described by an abbreviated attribute. */
13256
13257 static gdb_byte *
13258 read_attribute (const struct die_reader_specs *reader,
13259 struct attribute *attr, struct attr_abbrev *abbrev,
13260 gdb_byte *info_ptr)
13261 {
13262 attr->name = abbrev->name;
13263 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13264 }
13265
13266 /* Read dwarf information from a buffer. */
13267
13268 static unsigned int
13269 read_1_byte (bfd *abfd, gdb_byte *buf)
13270 {
13271 return bfd_get_8 (abfd, buf);
13272 }
13273
13274 static int
13275 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13276 {
13277 return bfd_get_signed_8 (abfd, buf);
13278 }
13279
13280 static unsigned int
13281 read_2_bytes (bfd *abfd, gdb_byte *buf)
13282 {
13283 return bfd_get_16 (abfd, buf);
13284 }
13285
13286 static int
13287 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13288 {
13289 return bfd_get_signed_16 (abfd, buf);
13290 }
13291
13292 static unsigned int
13293 read_4_bytes (bfd *abfd, gdb_byte *buf)
13294 {
13295 return bfd_get_32 (abfd, buf);
13296 }
13297
13298 static int
13299 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13300 {
13301 return bfd_get_signed_32 (abfd, buf);
13302 }
13303
13304 static ULONGEST
13305 read_8_bytes (bfd *abfd, gdb_byte *buf)
13306 {
13307 return bfd_get_64 (abfd, buf);
13308 }
13309
13310 static CORE_ADDR
13311 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13312 unsigned int *bytes_read)
13313 {
13314 struct comp_unit_head *cu_header = &cu->header;
13315 CORE_ADDR retval = 0;
13316
13317 if (cu_header->signed_addr_p)
13318 {
13319 switch (cu_header->addr_size)
13320 {
13321 case 2:
13322 retval = bfd_get_signed_16 (abfd, buf);
13323 break;
13324 case 4:
13325 retval = bfd_get_signed_32 (abfd, buf);
13326 break;
13327 case 8:
13328 retval = bfd_get_signed_64 (abfd, buf);
13329 break;
13330 default:
13331 internal_error (__FILE__, __LINE__,
13332 _("read_address: bad switch, signed [in module %s]"),
13333 bfd_get_filename (abfd));
13334 }
13335 }
13336 else
13337 {
13338 switch (cu_header->addr_size)
13339 {
13340 case 2:
13341 retval = bfd_get_16 (abfd, buf);
13342 break;
13343 case 4:
13344 retval = bfd_get_32 (abfd, buf);
13345 break;
13346 case 8:
13347 retval = bfd_get_64 (abfd, buf);
13348 break;
13349 default:
13350 internal_error (__FILE__, __LINE__,
13351 _("read_address: bad switch, "
13352 "unsigned [in module %s]"),
13353 bfd_get_filename (abfd));
13354 }
13355 }
13356
13357 *bytes_read = cu_header->addr_size;
13358 return retval;
13359 }
13360
13361 /* Read the initial length from a section. The (draft) DWARF 3
13362 specification allows the initial length to take up either 4 bytes
13363 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13364 bytes describe the length and all offsets will be 8 bytes in length
13365 instead of 4.
13366
13367 An older, non-standard 64-bit format is also handled by this
13368 function. The older format in question stores the initial length
13369 as an 8-byte quantity without an escape value. Lengths greater
13370 than 2^32 aren't very common which means that the initial 4 bytes
13371 is almost always zero. Since a length value of zero doesn't make
13372 sense for the 32-bit format, this initial zero can be considered to
13373 be an escape value which indicates the presence of the older 64-bit
13374 format. As written, the code can't detect (old format) lengths
13375 greater than 4GB. If it becomes necessary to handle lengths
13376 somewhat larger than 4GB, we could allow other small values (such
13377 as the non-sensical values of 1, 2, and 3) to also be used as
13378 escape values indicating the presence of the old format.
13379
13380 The value returned via bytes_read should be used to increment the
13381 relevant pointer after calling read_initial_length().
13382
13383 [ Note: read_initial_length() and read_offset() are based on the
13384 document entitled "DWARF Debugging Information Format", revision
13385 3, draft 8, dated November 19, 2001. This document was obtained
13386 from:
13387
13388 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13389
13390 This document is only a draft and is subject to change. (So beware.)
13391
13392 Details regarding the older, non-standard 64-bit format were
13393 determined empirically by examining 64-bit ELF files produced by
13394 the SGI toolchain on an IRIX 6.5 machine.
13395
13396 - Kevin, July 16, 2002
13397 ] */
13398
13399 static LONGEST
13400 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13401 {
13402 LONGEST length = bfd_get_32 (abfd, buf);
13403
13404 if (length == 0xffffffff)
13405 {
13406 length = bfd_get_64 (abfd, buf + 4);
13407 *bytes_read = 12;
13408 }
13409 else if (length == 0)
13410 {
13411 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
13412 length = bfd_get_64 (abfd, buf);
13413 *bytes_read = 8;
13414 }
13415 else
13416 {
13417 *bytes_read = 4;
13418 }
13419
13420 return length;
13421 }
13422
13423 /* Cover function for read_initial_length.
13424 Returns the length of the object at BUF, and stores the size of the
13425 initial length in *BYTES_READ and stores the size that offsets will be in
13426 *OFFSET_SIZE.
13427 If the initial length size is not equivalent to that specified in
13428 CU_HEADER then issue a complaint.
13429 This is useful when reading non-comp-unit headers. */
13430
13431 static LONGEST
13432 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13433 const struct comp_unit_head *cu_header,
13434 unsigned int *bytes_read,
13435 unsigned int *offset_size)
13436 {
13437 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13438
13439 gdb_assert (cu_header->initial_length_size == 4
13440 || cu_header->initial_length_size == 8
13441 || cu_header->initial_length_size == 12);
13442
13443 if (cu_header->initial_length_size != *bytes_read)
13444 complaint (&symfile_complaints,
13445 _("intermixed 32-bit and 64-bit DWARF sections"));
13446
13447 *offset_size = (*bytes_read == 4) ? 4 : 8;
13448 return length;
13449 }
13450
13451 /* Read an offset from the data stream. The size of the offset is
13452 given by cu_header->offset_size. */
13453
13454 static LONGEST
13455 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13456 unsigned int *bytes_read)
13457 {
13458 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13459
13460 *bytes_read = cu_header->offset_size;
13461 return offset;
13462 }
13463
13464 /* Read an offset from the data stream. */
13465
13466 static LONGEST
13467 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13468 {
13469 LONGEST retval = 0;
13470
13471 switch (offset_size)
13472 {
13473 case 4:
13474 retval = bfd_get_32 (abfd, buf);
13475 break;
13476 case 8:
13477 retval = bfd_get_64 (abfd, buf);
13478 break;
13479 default:
13480 internal_error (__FILE__, __LINE__,
13481 _("read_offset_1: bad switch [in module %s]"),
13482 bfd_get_filename (abfd));
13483 }
13484
13485 return retval;
13486 }
13487
13488 static gdb_byte *
13489 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13490 {
13491 /* If the size of a host char is 8 bits, we can return a pointer
13492 to the buffer, otherwise we have to copy the data to a buffer
13493 allocated on the temporary obstack. */
13494 gdb_assert (HOST_CHAR_BIT == 8);
13495 return buf;
13496 }
13497
13498 static char *
13499 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13500 {
13501 /* If the size of a host char is 8 bits, we can return a pointer
13502 to the string, otherwise we have to copy the string to a buffer
13503 allocated on the temporary obstack. */
13504 gdb_assert (HOST_CHAR_BIT == 8);
13505 if (*buf == '\0')
13506 {
13507 *bytes_read_ptr = 1;
13508 return NULL;
13509 }
13510 *bytes_read_ptr = strlen ((char *) buf) + 1;
13511 return (char *) buf;
13512 }
13513
13514 static char *
13515 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13516 {
13517 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13518 if (dwarf2_per_objfile->str.buffer == NULL)
13519 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13520 bfd_get_filename (abfd));
13521 if (str_offset >= dwarf2_per_objfile->str.size)
13522 error (_("DW_FORM_strp pointing outside of "
13523 ".debug_str section [in module %s]"),
13524 bfd_get_filename (abfd));
13525 gdb_assert (HOST_CHAR_BIT == 8);
13526 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13527 return NULL;
13528 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13529 }
13530
13531 /* Read a string at offset STR_OFFSET in the .debug_str section from
13532 the .dwz file DWZ. Throw an error if the offset is too large. If
13533 the string consists of a single NUL byte, return NULL; otherwise
13534 return a pointer to the string. */
13535
13536 static char *
13537 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13538 {
13539 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13540
13541 if (dwz->str.buffer == NULL)
13542 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13543 "section [in module %s]"),
13544 bfd_get_filename (dwz->dwz_bfd));
13545 if (str_offset >= dwz->str.size)
13546 error (_("DW_FORM_GNU_strp_alt pointing outside of "
13547 ".debug_str section [in module %s]"),
13548 bfd_get_filename (dwz->dwz_bfd));
13549 gdb_assert (HOST_CHAR_BIT == 8);
13550 if (dwz->str.buffer[str_offset] == '\0')
13551 return NULL;
13552 return (char *) (dwz->str.buffer + str_offset);
13553 }
13554
13555 static char *
13556 read_indirect_string (bfd *abfd, gdb_byte *buf,
13557 const struct comp_unit_head *cu_header,
13558 unsigned int *bytes_read_ptr)
13559 {
13560 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13561
13562 return read_indirect_string_at_offset (abfd, str_offset);
13563 }
13564
13565 static ULONGEST
13566 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13567 {
13568 ULONGEST result;
13569 unsigned int num_read;
13570 int i, shift;
13571 unsigned char byte;
13572
13573 result = 0;
13574 shift = 0;
13575 num_read = 0;
13576 i = 0;
13577 while (1)
13578 {
13579 byte = bfd_get_8 (abfd, buf);
13580 buf++;
13581 num_read++;
13582 result |= ((ULONGEST) (byte & 127) << shift);
13583 if ((byte & 128) == 0)
13584 {
13585 break;
13586 }
13587 shift += 7;
13588 }
13589 *bytes_read_ptr = num_read;
13590 return result;
13591 }
13592
13593 static LONGEST
13594 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13595 {
13596 LONGEST result;
13597 int i, shift, num_read;
13598 unsigned char byte;
13599
13600 result = 0;
13601 shift = 0;
13602 num_read = 0;
13603 i = 0;
13604 while (1)
13605 {
13606 byte = bfd_get_8 (abfd, buf);
13607 buf++;
13608 num_read++;
13609 result |= ((LONGEST) (byte & 127) << shift);
13610 shift += 7;
13611 if ((byte & 128) == 0)
13612 {
13613 break;
13614 }
13615 }
13616 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13617 result |= -(((LONGEST) 1) << shift);
13618 *bytes_read_ptr = num_read;
13619 return result;
13620 }
13621
13622 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13623 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13624 ADDR_SIZE is the size of addresses from the CU header. */
13625
13626 static CORE_ADDR
13627 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13628 {
13629 struct objfile *objfile = dwarf2_per_objfile->objfile;
13630 bfd *abfd = objfile->obfd;
13631 const gdb_byte *info_ptr;
13632
13633 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13634 if (dwarf2_per_objfile->addr.buffer == NULL)
13635 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13636 objfile->name);
13637 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13638 error (_("DW_FORM_addr_index pointing outside of "
13639 ".debug_addr section [in module %s]"),
13640 objfile->name);
13641 info_ptr = (dwarf2_per_objfile->addr.buffer
13642 + addr_base + addr_index * addr_size);
13643 if (addr_size == 4)
13644 return bfd_get_32 (abfd, info_ptr);
13645 else
13646 return bfd_get_64 (abfd, info_ptr);
13647 }
13648
13649 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13650
13651 static CORE_ADDR
13652 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13653 {
13654 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13655 }
13656
13657 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13658
13659 static CORE_ADDR
13660 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13661 unsigned int *bytes_read)
13662 {
13663 bfd *abfd = cu->objfile->obfd;
13664 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13665
13666 return read_addr_index (cu, addr_index);
13667 }
13668
13669 /* Data structure to pass results from dwarf2_read_addr_index_reader
13670 back to dwarf2_read_addr_index. */
13671
13672 struct dwarf2_read_addr_index_data
13673 {
13674 ULONGEST addr_base;
13675 int addr_size;
13676 };
13677
13678 /* die_reader_func for dwarf2_read_addr_index. */
13679
13680 static void
13681 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13682 gdb_byte *info_ptr,
13683 struct die_info *comp_unit_die,
13684 int has_children,
13685 void *data)
13686 {
13687 struct dwarf2_cu *cu = reader->cu;
13688 struct dwarf2_read_addr_index_data *aidata =
13689 (struct dwarf2_read_addr_index_data *) data;
13690
13691 aidata->addr_base = cu->addr_base;
13692 aidata->addr_size = cu->header.addr_size;
13693 }
13694
13695 /* Given an index in .debug_addr, fetch the value.
13696 NOTE: This can be called during dwarf expression evaluation,
13697 long after the debug information has been read, and thus per_cu->cu
13698 may no longer exist. */
13699
13700 CORE_ADDR
13701 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13702 unsigned int addr_index)
13703 {
13704 struct objfile *objfile = per_cu->objfile;
13705 struct dwarf2_cu *cu = per_cu->cu;
13706 ULONGEST addr_base;
13707 int addr_size;
13708
13709 /* This is intended to be called from outside this file. */
13710 dw2_setup (objfile);
13711
13712 /* We need addr_base and addr_size.
13713 If we don't have PER_CU->cu, we have to get it.
13714 Nasty, but the alternative is storing the needed info in PER_CU,
13715 which at this point doesn't seem justified: it's not clear how frequently
13716 it would get used and it would increase the size of every PER_CU.
13717 Entry points like dwarf2_per_cu_addr_size do a similar thing
13718 so we're not in uncharted territory here.
13719 Alas we need to be a bit more complicated as addr_base is contained
13720 in the DIE.
13721
13722 We don't need to read the entire CU(/TU).
13723 We just need the header and top level die.
13724 IWBN to use the aging mechanism to let us lazily later discard the CU.
13725 See however init_cutu_and_read_dies_simple. */
13726
13727 if (cu != NULL)
13728 {
13729 addr_base = cu->addr_base;
13730 addr_size = cu->header.addr_size;
13731 }
13732 else
13733 {
13734 struct dwarf2_read_addr_index_data aidata;
13735
13736 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13737 &aidata);
13738 addr_base = aidata.addr_base;
13739 addr_size = aidata.addr_size;
13740 }
13741
13742 return read_addr_index_1 (addr_index, addr_base, addr_size);
13743 }
13744
13745 /* Given a DW_AT_str_index, fetch the string. */
13746
13747 static char *
13748 read_str_index (const struct die_reader_specs *reader,
13749 struct dwarf2_cu *cu, ULONGEST str_index)
13750 {
13751 struct objfile *objfile = dwarf2_per_objfile->objfile;
13752 const char *dwo_name = objfile->name;
13753 bfd *abfd = objfile->obfd;
13754 struct dwo_sections *sections = &reader->dwo_file->sections;
13755 gdb_byte *info_ptr;
13756 ULONGEST str_offset;
13757
13758 dwarf2_read_section (objfile, &sections->str);
13759 dwarf2_read_section (objfile, &sections->str_offsets);
13760 if (sections->str.buffer == NULL)
13761 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13762 " in CU at offset 0x%lx [in module %s]"),
13763 (long) cu->header.offset.sect_off, dwo_name);
13764 if (sections->str_offsets.buffer == NULL)
13765 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13766 " in CU at offset 0x%lx [in module %s]"),
13767 (long) cu->header.offset.sect_off, dwo_name);
13768 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13769 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13770 " section in CU at offset 0x%lx [in module %s]"),
13771 (long) cu->header.offset.sect_off, dwo_name);
13772 info_ptr = (sections->str_offsets.buffer
13773 + str_index * cu->header.offset_size);
13774 if (cu->header.offset_size == 4)
13775 str_offset = bfd_get_32 (abfd, info_ptr);
13776 else
13777 str_offset = bfd_get_64 (abfd, info_ptr);
13778 if (str_offset >= sections->str.size)
13779 error (_("Offset from DW_FORM_str_index pointing outside of"
13780 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13781 (long) cu->header.offset.sect_off, dwo_name);
13782 return (char *) (sections->str.buffer + str_offset);
13783 }
13784
13785 /* Return the length of an LEB128 number in BUF. */
13786
13787 static int
13788 leb128_size (const gdb_byte *buf)
13789 {
13790 const gdb_byte *begin = buf;
13791 gdb_byte byte;
13792
13793 while (1)
13794 {
13795 byte = *buf++;
13796 if ((byte & 128) == 0)
13797 return buf - begin;
13798 }
13799 }
13800
13801 static void
13802 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13803 {
13804 switch (lang)
13805 {
13806 case DW_LANG_C89:
13807 case DW_LANG_C99:
13808 case DW_LANG_C:
13809 cu->language = language_c;
13810 break;
13811 case DW_LANG_C_plus_plus:
13812 cu->language = language_cplus;
13813 break;
13814 case DW_LANG_D:
13815 cu->language = language_d;
13816 break;
13817 case DW_LANG_Fortran77:
13818 case DW_LANG_Fortran90:
13819 case DW_LANG_Fortran95:
13820 cu->language = language_fortran;
13821 break;
13822 case DW_LANG_Go:
13823 cu->language = language_go;
13824 break;
13825 case DW_LANG_Mips_Assembler:
13826 cu->language = language_asm;
13827 break;
13828 case DW_LANG_Java:
13829 cu->language = language_java;
13830 break;
13831 case DW_LANG_Ada83:
13832 case DW_LANG_Ada95:
13833 cu->language = language_ada;
13834 break;
13835 case DW_LANG_Modula2:
13836 cu->language = language_m2;
13837 break;
13838 case DW_LANG_Pascal83:
13839 cu->language = language_pascal;
13840 break;
13841 case DW_LANG_ObjC:
13842 cu->language = language_objc;
13843 break;
13844 case DW_LANG_Cobol74:
13845 case DW_LANG_Cobol85:
13846 default:
13847 cu->language = language_minimal;
13848 break;
13849 }
13850 cu->language_defn = language_def (cu->language);
13851 }
13852
13853 /* Return the named attribute or NULL if not there. */
13854
13855 static struct attribute *
13856 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13857 {
13858 for (;;)
13859 {
13860 unsigned int i;
13861 struct attribute *spec = NULL;
13862
13863 for (i = 0; i < die->num_attrs; ++i)
13864 {
13865 if (die->attrs[i].name == name)
13866 return &die->attrs[i];
13867 if (die->attrs[i].name == DW_AT_specification
13868 || die->attrs[i].name == DW_AT_abstract_origin)
13869 spec = &die->attrs[i];
13870 }
13871
13872 if (!spec)
13873 break;
13874
13875 die = follow_die_ref (die, spec, &cu);
13876 }
13877
13878 return NULL;
13879 }
13880
13881 /* Return the named attribute or NULL if not there,
13882 but do not follow DW_AT_specification, etc.
13883 This is for use in contexts where we're reading .debug_types dies.
13884 Following DW_AT_specification, DW_AT_abstract_origin will take us
13885 back up the chain, and we want to go down. */
13886
13887 static struct attribute *
13888 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13889 {
13890 unsigned int i;
13891
13892 for (i = 0; i < die->num_attrs; ++i)
13893 if (die->attrs[i].name == name)
13894 return &die->attrs[i];
13895
13896 return NULL;
13897 }
13898
13899 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13900 and holds a non-zero value. This function should only be used for
13901 DW_FORM_flag or DW_FORM_flag_present attributes. */
13902
13903 static int
13904 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13905 {
13906 struct attribute *attr = dwarf2_attr (die, name, cu);
13907
13908 return (attr && DW_UNSND (attr));
13909 }
13910
13911 static int
13912 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13913 {
13914 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13915 which value is non-zero. However, we have to be careful with
13916 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13917 (via dwarf2_flag_true_p) follows this attribute. So we may
13918 end up accidently finding a declaration attribute that belongs
13919 to a different DIE referenced by the specification attribute,
13920 even though the given DIE does not have a declaration attribute. */
13921 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13922 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13923 }
13924
13925 /* Return the die giving the specification for DIE, if there is
13926 one. *SPEC_CU is the CU containing DIE on input, and the CU
13927 containing the return value on output. If there is no
13928 specification, but there is an abstract origin, that is
13929 returned. */
13930
13931 static struct die_info *
13932 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13933 {
13934 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13935 *spec_cu);
13936
13937 if (spec_attr == NULL)
13938 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13939
13940 if (spec_attr == NULL)
13941 return NULL;
13942 else
13943 return follow_die_ref (die, spec_attr, spec_cu);
13944 }
13945
13946 /* Free the line_header structure *LH, and any arrays and strings it
13947 refers to.
13948 NOTE: This is also used as a "cleanup" function. */
13949
13950 static void
13951 free_line_header (struct line_header *lh)
13952 {
13953 if (lh->standard_opcode_lengths)
13954 xfree (lh->standard_opcode_lengths);
13955
13956 /* Remember that all the lh->file_names[i].name pointers are
13957 pointers into debug_line_buffer, and don't need to be freed. */
13958 if (lh->file_names)
13959 xfree (lh->file_names);
13960
13961 /* Similarly for the include directory names. */
13962 if (lh->include_dirs)
13963 xfree (lh->include_dirs);
13964
13965 xfree (lh);
13966 }
13967
13968 /* Add an entry to LH's include directory table. */
13969
13970 static void
13971 add_include_dir (struct line_header *lh, char *include_dir)
13972 {
13973 /* Grow the array if necessary. */
13974 if (lh->include_dirs_size == 0)
13975 {
13976 lh->include_dirs_size = 1; /* for testing */
13977 lh->include_dirs = xmalloc (lh->include_dirs_size
13978 * sizeof (*lh->include_dirs));
13979 }
13980 else if (lh->num_include_dirs >= lh->include_dirs_size)
13981 {
13982 lh->include_dirs_size *= 2;
13983 lh->include_dirs = xrealloc (lh->include_dirs,
13984 (lh->include_dirs_size
13985 * sizeof (*lh->include_dirs)));
13986 }
13987
13988 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13989 }
13990
13991 /* Add an entry to LH's file name table. */
13992
13993 static void
13994 add_file_name (struct line_header *lh,
13995 char *name,
13996 unsigned int dir_index,
13997 unsigned int mod_time,
13998 unsigned int length)
13999 {
14000 struct file_entry *fe;
14001
14002 /* Grow the array if necessary. */
14003 if (lh->file_names_size == 0)
14004 {
14005 lh->file_names_size = 1; /* for testing */
14006 lh->file_names = xmalloc (lh->file_names_size
14007 * sizeof (*lh->file_names));
14008 }
14009 else if (lh->num_file_names >= lh->file_names_size)
14010 {
14011 lh->file_names_size *= 2;
14012 lh->file_names = xrealloc (lh->file_names,
14013 (lh->file_names_size
14014 * sizeof (*lh->file_names)));
14015 }
14016
14017 fe = &lh->file_names[lh->num_file_names++];
14018 fe->name = name;
14019 fe->dir_index = dir_index;
14020 fe->mod_time = mod_time;
14021 fe->length = length;
14022 fe->included_p = 0;
14023 fe->symtab = NULL;
14024 }
14025
14026 /* A convenience function to find the proper .debug_line section for a
14027 CU. */
14028
14029 static struct dwarf2_section_info *
14030 get_debug_line_section (struct dwarf2_cu *cu)
14031 {
14032 struct dwarf2_section_info *section;
14033
14034 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14035 DWO file. */
14036 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14037 section = &cu->dwo_unit->dwo_file->sections.line;
14038 else if (cu->per_cu->is_dwz)
14039 {
14040 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14041
14042 section = &dwz->line;
14043 }
14044 else
14045 section = &dwarf2_per_objfile->line;
14046
14047 return section;
14048 }
14049
14050 /* Read the statement program header starting at OFFSET in
14051 .debug_line, or .debug_line.dwo. Return a pointer
14052 to a struct line_header, allocated using xmalloc.
14053
14054 NOTE: the strings in the include directory and file name tables of
14055 the returned object point into the dwarf line section buffer,
14056 and must not be freed. */
14057
14058 static struct line_header *
14059 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14060 {
14061 struct cleanup *back_to;
14062 struct line_header *lh;
14063 gdb_byte *line_ptr;
14064 unsigned int bytes_read, offset_size;
14065 int i;
14066 char *cur_dir, *cur_file;
14067 struct dwarf2_section_info *section;
14068 bfd *abfd;
14069
14070 section = get_debug_line_section (cu);
14071 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14072 if (section->buffer == NULL)
14073 {
14074 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14075 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14076 else
14077 complaint (&symfile_complaints, _("missing .debug_line section"));
14078 return 0;
14079 }
14080
14081 /* We can't do this until we know the section is non-empty.
14082 Only then do we know we have such a section. */
14083 abfd = section->asection->owner;
14084
14085 /* Make sure that at least there's room for the total_length field.
14086 That could be 12 bytes long, but we're just going to fudge that. */
14087 if (offset + 4 >= section->size)
14088 {
14089 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14090 return 0;
14091 }
14092
14093 lh = xmalloc (sizeof (*lh));
14094 memset (lh, 0, sizeof (*lh));
14095 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14096 (void *) lh);
14097
14098 line_ptr = section->buffer + offset;
14099
14100 /* Read in the header. */
14101 lh->total_length =
14102 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14103 &bytes_read, &offset_size);
14104 line_ptr += bytes_read;
14105 if (line_ptr + lh->total_length > (section->buffer + section->size))
14106 {
14107 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14108 return 0;
14109 }
14110 lh->statement_program_end = line_ptr + lh->total_length;
14111 lh->version = read_2_bytes (abfd, line_ptr);
14112 line_ptr += 2;
14113 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14114 line_ptr += offset_size;
14115 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14116 line_ptr += 1;
14117 if (lh->version >= 4)
14118 {
14119 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14120 line_ptr += 1;
14121 }
14122 else
14123 lh->maximum_ops_per_instruction = 1;
14124
14125 if (lh->maximum_ops_per_instruction == 0)
14126 {
14127 lh->maximum_ops_per_instruction = 1;
14128 complaint (&symfile_complaints,
14129 _("invalid maximum_ops_per_instruction "
14130 "in `.debug_line' section"));
14131 }
14132
14133 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14134 line_ptr += 1;
14135 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14136 line_ptr += 1;
14137 lh->line_range = read_1_byte (abfd, line_ptr);
14138 line_ptr += 1;
14139 lh->opcode_base = read_1_byte (abfd, line_ptr);
14140 line_ptr += 1;
14141 lh->standard_opcode_lengths
14142 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14143
14144 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14145 for (i = 1; i < lh->opcode_base; ++i)
14146 {
14147 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14148 line_ptr += 1;
14149 }
14150
14151 /* Read directory table. */
14152 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14153 {
14154 line_ptr += bytes_read;
14155 add_include_dir (lh, cur_dir);
14156 }
14157 line_ptr += bytes_read;
14158
14159 /* Read file name table. */
14160 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14161 {
14162 unsigned int dir_index, mod_time, length;
14163
14164 line_ptr += bytes_read;
14165 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14166 line_ptr += bytes_read;
14167 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14168 line_ptr += bytes_read;
14169 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14170 line_ptr += bytes_read;
14171
14172 add_file_name (lh, cur_file, dir_index, mod_time, length);
14173 }
14174 line_ptr += bytes_read;
14175 lh->statement_program_start = line_ptr;
14176
14177 if (line_ptr > (section->buffer + section->size))
14178 complaint (&symfile_complaints,
14179 _("line number info header doesn't "
14180 "fit in `.debug_line' section"));
14181
14182 discard_cleanups (back_to);
14183 return lh;
14184 }
14185
14186 /* Subroutine of dwarf_decode_lines to simplify it.
14187 Return the file name of the psymtab for included file FILE_INDEX
14188 in line header LH of PST.
14189 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14190 If space for the result is malloc'd, it will be freed by a cleanup.
14191 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
14192
14193 static char *
14194 psymtab_include_file_name (const struct line_header *lh, int file_index,
14195 const struct partial_symtab *pst,
14196 const char *comp_dir)
14197 {
14198 const struct file_entry fe = lh->file_names [file_index];
14199 char *include_name = fe.name;
14200 char *include_name_to_compare = include_name;
14201 char *dir_name = NULL;
14202 const char *pst_filename;
14203 char *copied_name = NULL;
14204 int file_is_pst;
14205
14206 if (fe.dir_index)
14207 dir_name = lh->include_dirs[fe.dir_index - 1];
14208
14209 if (!IS_ABSOLUTE_PATH (include_name)
14210 && (dir_name != NULL || comp_dir != NULL))
14211 {
14212 /* Avoid creating a duplicate psymtab for PST.
14213 We do this by comparing INCLUDE_NAME and PST_FILENAME.
14214 Before we do the comparison, however, we need to account
14215 for DIR_NAME and COMP_DIR.
14216 First prepend dir_name (if non-NULL). If we still don't
14217 have an absolute path prepend comp_dir (if non-NULL).
14218 However, the directory we record in the include-file's
14219 psymtab does not contain COMP_DIR (to match the
14220 corresponding symtab(s)).
14221
14222 Example:
14223
14224 bash$ cd /tmp
14225 bash$ gcc -g ./hello.c
14226 include_name = "hello.c"
14227 dir_name = "."
14228 DW_AT_comp_dir = comp_dir = "/tmp"
14229 DW_AT_name = "./hello.c" */
14230
14231 if (dir_name != NULL)
14232 {
14233 include_name = concat (dir_name, SLASH_STRING,
14234 include_name, (char *)NULL);
14235 include_name_to_compare = include_name;
14236 make_cleanup (xfree, include_name);
14237 }
14238 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14239 {
14240 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14241 include_name, (char *)NULL);
14242 }
14243 }
14244
14245 pst_filename = pst->filename;
14246 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14247 {
14248 copied_name = concat (pst->dirname, SLASH_STRING,
14249 pst_filename, (char *)NULL);
14250 pst_filename = copied_name;
14251 }
14252
14253 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14254
14255 if (include_name_to_compare != include_name)
14256 xfree (include_name_to_compare);
14257 if (copied_name != NULL)
14258 xfree (copied_name);
14259
14260 if (file_is_pst)
14261 return NULL;
14262 return include_name;
14263 }
14264
14265 /* Ignore this record_line request. */
14266
14267 static void
14268 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14269 {
14270 return;
14271 }
14272
14273 /* Subroutine of dwarf_decode_lines to simplify it.
14274 Process the line number information in LH. */
14275
14276 static void
14277 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14278 struct dwarf2_cu *cu, struct partial_symtab *pst)
14279 {
14280 gdb_byte *line_ptr, *extended_end;
14281 gdb_byte *line_end;
14282 unsigned int bytes_read, extended_len;
14283 unsigned char op_code, extended_op, adj_opcode;
14284 CORE_ADDR baseaddr;
14285 struct objfile *objfile = cu->objfile;
14286 bfd *abfd = objfile->obfd;
14287 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14288 const int decode_for_pst_p = (pst != NULL);
14289 struct subfile *last_subfile = NULL;
14290 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14291 = record_line;
14292
14293 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14294
14295 line_ptr = lh->statement_program_start;
14296 line_end = lh->statement_program_end;
14297
14298 /* Read the statement sequences until there's nothing left. */
14299 while (line_ptr < line_end)
14300 {
14301 /* state machine registers */
14302 CORE_ADDR address = 0;
14303 unsigned int file = 1;
14304 unsigned int line = 1;
14305 unsigned int column = 0;
14306 int is_stmt = lh->default_is_stmt;
14307 int basic_block = 0;
14308 int end_sequence = 0;
14309 CORE_ADDR addr;
14310 unsigned char op_index = 0;
14311
14312 if (!decode_for_pst_p && lh->num_file_names >= file)
14313 {
14314 /* Start a subfile for the current file of the state machine. */
14315 /* lh->include_dirs and lh->file_names are 0-based, but the
14316 directory and file name numbers in the statement program
14317 are 1-based. */
14318 struct file_entry *fe = &lh->file_names[file - 1];
14319 char *dir = NULL;
14320
14321 if (fe->dir_index)
14322 dir = lh->include_dirs[fe->dir_index - 1];
14323
14324 dwarf2_start_subfile (fe->name, dir, comp_dir);
14325 }
14326
14327 /* Decode the table. */
14328 while (!end_sequence)
14329 {
14330 op_code = read_1_byte (abfd, line_ptr);
14331 line_ptr += 1;
14332 if (line_ptr > line_end)
14333 {
14334 dwarf2_debug_line_missing_end_sequence_complaint ();
14335 break;
14336 }
14337
14338 if (op_code >= lh->opcode_base)
14339 {
14340 /* Special operand. */
14341 adj_opcode = op_code - lh->opcode_base;
14342 address += (((op_index + (adj_opcode / lh->line_range))
14343 / lh->maximum_ops_per_instruction)
14344 * lh->minimum_instruction_length);
14345 op_index = ((op_index + (adj_opcode / lh->line_range))
14346 % lh->maximum_ops_per_instruction);
14347 line += lh->line_base + (adj_opcode % lh->line_range);
14348 if (lh->num_file_names < file || file == 0)
14349 dwarf2_debug_line_missing_file_complaint ();
14350 /* For now we ignore lines not starting on an
14351 instruction boundary. */
14352 else if (op_index == 0)
14353 {
14354 lh->file_names[file - 1].included_p = 1;
14355 if (!decode_for_pst_p && is_stmt)
14356 {
14357 if (last_subfile != current_subfile)
14358 {
14359 addr = gdbarch_addr_bits_remove (gdbarch, address);
14360 if (last_subfile)
14361 (*p_record_line) (last_subfile, 0, addr);
14362 last_subfile = current_subfile;
14363 }
14364 /* Append row to matrix using current values. */
14365 addr = gdbarch_addr_bits_remove (gdbarch, address);
14366 (*p_record_line) (current_subfile, line, addr);
14367 }
14368 }
14369 basic_block = 0;
14370 }
14371 else switch (op_code)
14372 {
14373 case DW_LNS_extended_op:
14374 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14375 &bytes_read);
14376 line_ptr += bytes_read;
14377 extended_end = line_ptr + extended_len;
14378 extended_op = read_1_byte (abfd, line_ptr);
14379 line_ptr += 1;
14380 switch (extended_op)
14381 {
14382 case DW_LNE_end_sequence:
14383 p_record_line = record_line;
14384 end_sequence = 1;
14385 break;
14386 case DW_LNE_set_address:
14387 address = read_address (abfd, line_ptr, cu, &bytes_read);
14388
14389 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14390 {
14391 /* This line table is for a function which has been
14392 GCd by the linker. Ignore it. PR gdb/12528 */
14393
14394 long line_offset
14395 = line_ptr - get_debug_line_section (cu)->buffer;
14396
14397 complaint (&symfile_complaints,
14398 _(".debug_line address at offset 0x%lx is 0 "
14399 "[in module %s]"),
14400 line_offset, objfile->name);
14401 p_record_line = noop_record_line;
14402 }
14403
14404 op_index = 0;
14405 line_ptr += bytes_read;
14406 address += baseaddr;
14407 break;
14408 case DW_LNE_define_file:
14409 {
14410 char *cur_file;
14411 unsigned int dir_index, mod_time, length;
14412
14413 cur_file = read_direct_string (abfd, line_ptr,
14414 &bytes_read);
14415 line_ptr += bytes_read;
14416 dir_index =
14417 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14418 line_ptr += bytes_read;
14419 mod_time =
14420 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14421 line_ptr += bytes_read;
14422 length =
14423 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14424 line_ptr += bytes_read;
14425 add_file_name (lh, cur_file, dir_index, mod_time, length);
14426 }
14427 break;
14428 case DW_LNE_set_discriminator:
14429 /* The discriminator is not interesting to the debugger;
14430 just ignore it. */
14431 line_ptr = extended_end;
14432 break;
14433 default:
14434 complaint (&symfile_complaints,
14435 _("mangled .debug_line section"));
14436 return;
14437 }
14438 /* Make sure that we parsed the extended op correctly. If e.g.
14439 we expected a different address size than the producer used,
14440 we may have read the wrong number of bytes. */
14441 if (line_ptr != extended_end)
14442 {
14443 complaint (&symfile_complaints,
14444 _("mangled .debug_line section"));
14445 return;
14446 }
14447 break;
14448 case DW_LNS_copy:
14449 if (lh->num_file_names < file || file == 0)
14450 dwarf2_debug_line_missing_file_complaint ();
14451 else
14452 {
14453 lh->file_names[file - 1].included_p = 1;
14454 if (!decode_for_pst_p && is_stmt)
14455 {
14456 if (last_subfile != current_subfile)
14457 {
14458 addr = gdbarch_addr_bits_remove (gdbarch, address);
14459 if (last_subfile)
14460 (*p_record_line) (last_subfile, 0, addr);
14461 last_subfile = current_subfile;
14462 }
14463 addr = gdbarch_addr_bits_remove (gdbarch, address);
14464 (*p_record_line) (current_subfile, line, addr);
14465 }
14466 }
14467 basic_block = 0;
14468 break;
14469 case DW_LNS_advance_pc:
14470 {
14471 CORE_ADDR adjust
14472 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14473
14474 address += (((op_index + adjust)
14475 / lh->maximum_ops_per_instruction)
14476 * lh->minimum_instruction_length);
14477 op_index = ((op_index + adjust)
14478 % lh->maximum_ops_per_instruction);
14479 line_ptr += bytes_read;
14480 }
14481 break;
14482 case DW_LNS_advance_line:
14483 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14484 line_ptr += bytes_read;
14485 break;
14486 case DW_LNS_set_file:
14487 {
14488 /* The arrays lh->include_dirs and lh->file_names are
14489 0-based, but the directory and file name numbers in
14490 the statement program are 1-based. */
14491 struct file_entry *fe;
14492 char *dir = NULL;
14493
14494 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14495 line_ptr += bytes_read;
14496 if (lh->num_file_names < file || file == 0)
14497 dwarf2_debug_line_missing_file_complaint ();
14498 else
14499 {
14500 fe = &lh->file_names[file - 1];
14501 if (fe->dir_index)
14502 dir = lh->include_dirs[fe->dir_index - 1];
14503 if (!decode_for_pst_p)
14504 {
14505 last_subfile = current_subfile;
14506 dwarf2_start_subfile (fe->name, dir, comp_dir);
14507 }
14508 }
14509 }
14510 break;
14511 case DW_LNS_set_column:
14512 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14513 line_ptr += bytes_read;
14514 break;
14515 case DW_LNS_negate_stmt:
14516 is_stmt = (!is_stmt);
14517 break;
14518 case DW_LNS_set_basic_block:
14519 basic_block = 1;
14520 break;
14521 /* Add to the address register of the state machine the
14522 address increment value corresponding to special opcode
14523 255. I.e., this value is scaled by the minimum
14524 instruction length since special opcode 255 would have
14525 scaled the increment. */
14526 case DW_LNS_const_add_pc:
14527 {
14528 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14529
14530 address += (((op_index + adjust)
14531 / lh->maximum_ops_per_instruction)
14532 * lh->minimum_instruction_length);
14533 op_index = ((op_index + adjust)
14534 % lh->maximum_ops_per_instruction);
14535 }
14536 break;
14537 case DW_LNS_fixed_advance_pc:
14538 address += read_2_bytes (abfd, line_ptr);
14539 op_index = 0;
14540 line_ptr += 2;
14541 break;
14542 default:
14543 {
14544 /* Unknown standard opcode, ignore it. */
14545 int i;
14546
14547 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14548 {
14549 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14550 line_ptr += bytes_read;
14551 }
14552 }
14553 }
14554 }
14555 if (lh->num_file_names < file || file == 0)
14556 dwarf2_debug_line_missing_file_complaint ();
14557 else
14558 {
14559 lh->file_names[file - 1].included_p = 1;
14560 if (!decode_for_pst_p)
14561 {
14562 addr = gdbarch_addr_bits_remove (gdbarch, address);
14563 (*p_record_line) (current_subfile, 0, addr);
14564 }
14565 }
14566 }
14567 }
14568
14569 /* Decode the Line Number Program (LNP) for the given line_header
14570 structure and CU. The actual information extracted and the type
14571 of structures created from the LNP depends on the value of PST.
14572
14573 1. If PST is NULL, then this procedure uses the data from the program
14574 to create all necessary symbol tables, and their linetables.
14575
14576 2. If PST is not NULL, this procedure reads the program to determine
14577 the list of files included by the unit represented by PST, and
14578 builds all the associated partial symbol tables.
14579
14580 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14581 It is used for relative paths in the line table.
14582 NOTE: When processing partial symtabs (pst != NULL),
14583 comp_dir == pst->dirname.
14584
14585 NOTE: It is important that psymtabs have the same file name (via strcmp)
14586 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14587 symtab we don't use it in the name of the psymtabs we create.
14588 E.g. expand_line_sal requires this when finding psymtabs to expand.
14589 A good testcase for this is mb-inline.exp. */
14590
14591 static void
14592 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14593 struct dwarf2_cu *cu, struct partial_symtab *pst,
14594 int want_line_info)
14595 {
14596 struct objfile *objfile = cu->objfile;
14597 const int decode_for_pst_p = (pst != NULL);
14598 struct subfile *first_subfile = current_subfile;
14599
14600 if (want_line_info)
14601 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14602
14603 if (decode_for_pst_p)
14604 {
14605 int file_index;
14606
14607 /* Now that we're done scanning the Line Header Program, we can
14608 create the psymtab of each included file. */
14609 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14610 if (lh->file_names[file_index].included_p == 1)
14611 {
14612 char *include_name =
14613 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14614 if (include_name != NULL)
14615 dwarf2_create_include_psymtab (include_name, pst, objfile);
14616 }
14617 }
14618 else
14619 {
14620 /* Make sure a symtab is created for every file, even files
14621 which contain only variables (i.e. no code with associated
14622 line numbers). */
14623 int i;
14624
14625 for (i = 0; i < lh->num_file_names; i++)
14626 {
14627 char *dir = NULL;
14628 struct file_entry *fe;
14629
14630 fe = &lh->file_names[i];
14631 if (fe->dir_index)
14632 dir = lh->include_dirs[fe->dir_index - 1];
14633 dwarf2_start_subfile (fe->name, dir, comp_dir);
14634
14635 /* Skip the main file; we don't need it, and it must be
14636 allocated last, so that it will show up before the
14637 non-primary symtabs in the objfile's symtab list. */
14638 if (current_subfile == first_subfile)
14639 continue;
14640
14641 if (current_subfile->symtab == NULL)
14642 current_subfile->symtab = allocate_symtab (current_subfile->name,
14643 objfile);
14644 fe->symtab = current_subfile->symtab;
14645 }
14646 }
14647 }
14648
14649 /* Start a subfile for DWARF. FILENAME is the name of the file and
14650 DIRNAME the name of the source directory which contains FILENAME
14651 or NULL if not known. COMP_DIR is the compilation directory for the
14652 linetable's compilation unit or NULL if not known.
14653 This routine tries to keep line numbers from identical absolute and
14654 relative file names in a common subfile.
14655
14656 Using the `list' example from the GDB testsuite, which resides in
14657 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14658 of /srcdir/list0.c yields the following debugging information for list0.c:
14659
14660 DW_AT_name: /srcdir/list0.c
14661 DW_AT_comp_dir: /compdir
14662 files.files[0].name: list0.h
14663 files.files[0].dir: /srcdir
14664 files.files[1].name: list0.c
14665 files.files[1].dir: /srcdir
14666
14667 The line number information for list0.c has to end up in a single
14668 subfile, so that `break /srcdir/list0.c:1' works as expected.
14669 start_subfile will ensure that this happens provided that we pass the
14670 concatenation of files.files[1].dir and files.files[1].name as the
14671 subfile's name. */
14672
14673 static void
14674 dwarf2_start_subfile (char *filename, const char *dirname,
14675 const char *comp_dir)
14676 {
14677 char *fullname;
14678
14679 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14680 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14681 second argument to start_subfile. To be consistent, we do the
14682 same here. In order not to lose the line information directory,
14683 we concatenate it to the filename when it makes sense.
14684 Note that the Dwarf3 standard says (speaking of filenames in line
14685 information): ``The directory index is ignored for file names
14686 that represent full path names''. Thus ignoring dirname in the
14687 `else' branch below isn't an issue. */
14688
14689 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14690 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14691 else
14692 fullname = filename;
14693
14694 start_subfile (fullname, comp_dir);
14695
14696 if (fullname != filename)
14697 xfree (fullname);
14698 }
14699
14700 /* Start a symtab for DWARF.
14701 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14702
14703 static void
14704 dwarf2_start_symtab (struct dwarf2_cu *cu,
14705 char *name, char *comp_dir, CORE_ADDR low_pc)
14706 {
14707 start_symtab (name, comp_dir, low_pc);
14708 record_debugformat ("DWARF 2");
14709 record_producer (cu->producer);
14710
14711 /* We assume that we're processing GCC output. */
14712 processing_gcc_compilation = 2;
14713
14714 processing_has_namespace_info = 0;
14715 }
14716
14717 static void
14718 var_decode_location (struct attribute *attr, struct symbol *sym,
14719 struct dwarf2_cu *cu)
14720 {
14721 struct objfile *objfile = cu->objfile;
14722 struct comp_unit_head *cu_header = &cu->header;
14723
14724 /* NOTE drow/2003-01-30: There used to be a comment and some special
14725 code here to turn a symbol with DW_AT_external and a
14726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14728 with some versions of binutils) where shared libraries could have
14729 relocations against symbols in their debug information - the
14730 minimal symbol would have the right address, but the debug info
14731 would not. It's no longer necessary, because we will explicitly
14732 apply relocations when we read in the debug information now. */
14733
14734 /* A DW_AT_location attribute with no contents indicates that a
14735 variable has been optimized away. */
14736 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14737 {
14738 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14739 return;
14740 }
14741
14742 /* Handle one degenerate form of location expression specially, to
14743 preserve GDB's previous behavior when section offsets are
14744 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14745 then mark this symbol as LOC_STATIC. */
14746
14747 if (attr_form_is_block (attr)
14748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14750 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14751 && (DW_BLOCK (attr)->size
14752 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14753 {
14754 unsigned int dummy;
14755
14756 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14757 SYMBOL_VALUE_ADDRESS (sym) =
14758 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14759 else
14760 SYMBOL_VALUE_ADDRESS (sym) =
14761 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14762 SYMBOL_CLASS (sym) = LOC_STATIC;
14763 fixup_symbol_section (sym, objfile);
14764 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14765 SYMBOL_SECTION (sym));
14766 return;
14767 }
14768
14769 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14770 expression evaluator, and use LOC_COMPUTED only when necessary
14771 (i.e. when the value of a register or memory location is
14772 referenced, or a thread-local block, etc.). Then again, it might
14773 not be worthwhile. I'm assuming that it isn't unless performance
14774 or memory numbers show me otherwise. */
14775
14776 dwarf2_symbol_mark_computed (attr, sym, cu);
14777 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14778
14779 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14780 cu->has_loclist = 1;
14781 }
14782
14783 /* Given a pointer to a DWARF information entry, figure out if we need
14784 to make a symbol table entry for it, and if so, create a new entry
14785 and return a pointer to it.
14786 If TYPE is NULL, determine symbol type from the die, otherwise
14787 used the passed type.
14788 If SPACE is not NULL, use it to hold the new symbol. If it is
14789 NULL, allocate a new symbol on the objfile's obstack. */
14790
14791 static struct symbol *
14792 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14793 struct symbol *space)
14794 {
14795 struct objfile *objfile = cu->objfile;
14796 struct symbol *sym = NULL;
14797 char *name;
14798 struct attribute *attr = NULL;
14799 struct attribute *attr2 = NULL;
14800 CORE_ADDR baseaddr;
14801 struct pending **list_to_add = NULL;
14802
14803 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14804
14805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14806
14807 name = dwarf2_name (die, cu);
14808 if (name)
14809 {
14810 const char *linkagename;
14811 int suppress_add = 0;
14812
14813 if (space)
14814 sym = space;
14815 else
14816 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14817 OBJSTAT (objfile, n_syms++);
14818
14819 /* Cache this symbol's name and the name's demangled form (if any). */
14820 SYMBOL_SET_LANGUAGE (sym, cu->language);
14821 linkagename = dwarf2_physname (name, die, cu);
14822 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14823
14824 /* Fortran does not have mangling standard and the mangling does differ
14825 between gfortran, iFort etc. */
14826 if (cu->language == language_fortran
14827 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14828 symbol_set_demangled_name (&(sym->ginfo),
14829 (char *) dwarf2_full_name (name, die, cu),
14830 NULL);
14831
14832 /* Default assumptions.
14833 Use the passed type or decode it from the die. */
14834 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14835 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14836 if (type != NULL)
14837 SYMBOL_TYPE (sym) = type;
14838 else
14839 SYMBOL_TYPE (sym) = die_type (die, cu);
14840 attr = dwarf2_attr (die,
14841 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14842 cu);
14843 if (attr)
14844 {
14845 SYMBOL_LINE (sym) = DW_UNSND (attr);
14846 }
14847
14848 attr = dwarf2_attr (die,
14849 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14850 cu);
14851 if (attr)
14852 {
14853 int file_index = DW_UNSND (attr);
14854
14855 if (cu->line_header == NULL
14856 || file_index > cu->line_header->num_file_names)
14857 complaint (&symfile_complaints,
14858 _("file index out of range"));
14859 else if (file_index > 0)
14860 {
14861 struct file_entry *fe;
14862
14863 fe = &cu->line_header->file_names[file_index - 1];
14864 SYMBOL_SYMTAB (sym) = fe->symtab;
14865 }
14866 }
14867
14868 switch (die->tag)
14869 {
14870 case DW_TAG_label:
14871 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14872 if (attr)
14873 {
14874 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14875 }
14876 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14877 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14878 SYMBOL_CLASS (sym) = LOC_LABEL;
14879 add_symbol_to_list (sym, cu->list_in_scope);
14880 break;
14881 case DW_TAG_subprogram:
14882 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14883 finish_block. */
14884 SYMBOL_CLASS (sym) = LOC_BLOCK;
14885 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14886 if ((attr2 && (DW_UNSND (attr2) != 0))
14887 || cu->language == language_ada)
14888 {
14889 /* Subprograms marked external are stored as a global symbol.
14890 Ada subprograms, whether marked external or not, are always
14891 stored as a global symbol, because we want to be able to
14892 access them globally. For instance, we want to be able
14893 to break on a nested subprogram without having to
14894 specify the context. */
14895 list_to_add = &global_symbols;
14896 }
14897 else
14898 {
14899 list_to_add = cu->list_in_scope;
14900 }
14901 break;
14902 case DW_TAG_inlined_subroutine:
14903 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14904 finish_block. */
14905 SYMBOL_CLASS (sym) = LOC_BLOCK;
14906 SYMBOL_INLINED (sym) = 1;
14907 list_to_add = cu->list_in_scope;
14908 break;
14909 case DW_TAG_template_value_param:
14910 suppress_add = 1;
14911 /* Fall through. */
14912 case DW_TAG_constant:
14913 case DW_TAG_variable:
14914 case DW_TAG_member:
14915 /* Compilation with minimal debug info may result in
14916 variables with missing type entries. Change the
14917 misleading `void' type to something sensible. */
14918 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14919 SYMBOL_TYPE (sym)
14920 = objfile_type (objfile)->nodebug_data_symbol;
14921
14922 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14923 /* In the case of DW_TAG_member, we should only be called for
14924 static const members. */
14925 if (die->tag == DW_TAG_member)
14926 {
14927 /* dwarf2_add_field uses die_is_declaration,
14928 so we do the same. */
14929 gdb_assert (die_is_declaration (die, cu));
14930 gdb_assert (attr);
14931 }
14932 if (attr)
14933 {
14934 dwarf2_const_value (attr, sym, cu);
14935 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14936 if (!suppress_add)
14937 {
14938 if (attr2 && (DW_UNSND (attr2) != 0))
14939 list_to_add = &global_symbols;
14940 else
14941 list_to_add = cu->list_in_scope;
14942 }
14943 break;
14944 }
14945 attr = dwarf2_attr (die, DW_AT_location, cu);
14946 if (attr)
14947 {
14948 var_decode_location (attr, sym, cu);
14949 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14950 if (SYMBOL_CLASS (sym) == LOC_STATIC
14951 && SYMBOL_VALUE_ADDRESS (sym) == 0
14952 && !dwarf2_per_objfile->has_section_at_zero)
14953 {
14954 /* When a static variable is eliminated by the linker,
14955 the corresponding debug information is not stripped
14956 out, but the variable address is set to null;
14957 do not add such variables into symbol table. */
14958 }
14959 else if (attr2 && (DW_UNSND (attr2) != 0))
14960 {
14961 /* Workaround gfortran PR debug/40040 - it uses
14962 DW_AT_location for variables in -fPIC libraries which may
14963 get overriden by other libraries/executable and get
14964 a different address. Resolve it by the minimal symbol
14965 which may come from inferior's executable using copy
14966 relocation. Make this workaround only for gfortran as for
14967 other compilers GDB cannot guess the minimal symbol
14968 Fortran mangling kind. */
14969 if (cu->language == language_fortran && die->parent
14970 && die->parent->tag == DW_TAG_module
14971 && cu->producer
14972 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14973 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14974
14975 /* A variable with DW_AT_external is never static,
14976 but it may be block-scoped. */
14977 list_to_add = (cu->list_in_scope == &file_symbols
14978 ? &global_symbols : cu->list_in_scope);
14979 }
14980 else
14981 list_to_add = cu->list_in_scope;
14982 }
14983 else
14984 {
14985 /* We do not know the address of this symbol.
14986 If it is an external symbol and we have type information
14987 for it, enter the symbol as a LOC_UNRESOLVED symbol.
14988 The address of the variable will then be determined from
14989 the minimal symbol table whenever the variable is
14990 referenced. */
14991 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14992 if (attr2 && (DW_UNSND (attr2) != 0)
14993 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
14994 {
14995 /* A variable with DW_AT_external is never static, but it
14996 may be block-scoped. */
14997 list_to_add = (cu->list_in_scope == &file_symbols
14998 ? &global_symbols : cu->list_in_scope);
14999
15000 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15001 }
15002 else if (!die_is_declaration (die, cu))
15003 {
15004 /* Use the default LOC_OPTIMIZED_OUT class. */
15005 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15006 if (!suppress_add)
15007 list_to_add = cu->list_in_scope;
15008 }
15009 }
15010 break;
15011 case DW_TAG_formal_parameter:
15012 /* If we are inside a function, mark this as an argument. If
15013 not, we might be looking at an argument to an inlined function
15014 when we do not have enough information to show inlined frames;
15015 pretend it's a local variable in that case so that the user can
15016 still see it. */
15017 if (context_stack_depth > 0
15018 && context_stack[context_stack_depth - 1].name != NULL)
15019 SYMBOL_IS_ARGUMENT (sym) = 1;
15020 attr = dwarf2_attr (die, DW_AT_location, cu);
15021 if (attr)
15022 {
15023 var_decode_location (attr, sym, cu);
15024 }
15025 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15026 if (attr)
15027 {
15028 dwarf2_const_value (attr, sym, cu);
15029 }
15030
15031 list_to_add = cu->list_in_scope;
15032 break;
15033 case DW_TAG_unspecified_parameters:
15034 /* From varargs functions; gdb doesn't seem to have any
15035 interest in this information, so just ignore it for now.
15036 (FIXME?) */
15037 break;
15038 case DW_TAG_template_type_param:
15039 suppress_add = 1;
15040 /* Fall through. */
15041 case DW_TAG_class_type:
15042 case DW_TAG_interface_type:
15043 case DW_TAG_structure_type:
15044 case DW_TAG_union_type:
15045 case DW_TAG_set_type:
15046 case DW_TAG_enumeration_type:
15047 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15048 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15049
15050 {
15051 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15052 really ever be static objects: otherwise, if you try
15053 to, say, break of a class's method and you're in a file
15054 which doesn't mention that class, it won't work unless
15055 the check for all static symbols in lookup_symbol_aux
15056 saves you. See the OtherFileClass tests in
15057 gdb.c++/namespace.exp. */
15058
15059 if (!suppress_add)
15060 {
15061 list_to_add = (cu->list_in_scope == &file_symbols
15062 && (cu->language == language_cplus
15063 || cu->language == language_java)
15064 ? &global_symbols : cu->list_in_scope);
15065
15066 /* The semantics of C++ state that "struct foo {
15067 ... }" also defines a typedef for "foo". A Java
15068 class declaration also defines a typedef for the
15069 class. */
15070 if (cu->language == language_cplus
15071 || cu->language == language_java
15072 || cu->language == language_ada)
15073 {
15074 /* The symbol's name is already allocated along
15075 with this objfile, so we don't need to
15076 duplicate it for the type. */
15077 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15078 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15079 }
15080 }
15081 }
15082 break;
15083 case DW_TAG_typedef:
15084 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15085 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15086 list_to_add = cu->list_in_scope;
15087 break;
15088 case DW_TAG_base_type:
15089 case DW_TAG_subrange_type:
15090 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15091 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15092 list_to_add = cu->list_in_scope;
15093 break;
15094 case DW_TAG_enumerator:
15095 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15096 if (attr)
15097 {
15098 dwarf2_const_value (attr, sym, cu);
15099 }
15100 {
15101 /* NOTE: carlton/2003-11-10: See comment above in the
15102 DW_TAG_class_type, etc. block. */
15103
15104 list_to_add = (cu->list_in_scope == &file_symbols
15105 && (cu->language == language_cplus
15106 || cu->language == language_java)
15107 ? &global_symbols : cu->list_in_scope);
15108 }
15109 break;
15110 case DW_TAG_namespace:
15111 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15112 list_to_add = &global_symbols;
15113 break;
15114 default:
15115 /* Not a tag we recognize. Hopefully we aren't processing
15116 trash data, but since we must specifically ignore things
15117 we don't recognize, there is nothing else we should do at
15118 this point. */
15119 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15120 dwarf_tag_name (die->tag));
15121 break;
15122 }
15123
15124 if (suppress_add)
15125 {
15126 sym->hash_next = objfile->template_symbols;
15127 objfile->template_symbols = sym;
15128 list_to_add = NULL;
15129 }
15130
15131 if (list_to_add != NULL)
15132 add_symbol_to_list (sym, list_to_add);
15133
15134 /* For the benefit of old versions of GCC, check for anonymous
15135 namespaces based on the demangled name. */
15136 if (!processing_has_namespace_info
15137 && cu->language == language_cplus)
15138 cp_scan_for_anonymous_namespaces (sym, objfile);
15139 }
15140 return (sym);
15141 }
15142
15143 /* A wrapper for new_symbol_full that always allocates a new symbol. */
15144
15145 static struct symbol *
15146 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15147 {
15148 return new_symbol_full (die, type, cu, NULL);
15149 }
15150
15151 /* Given an attr with a DW_FORM_dataN value in host byte order,
15152 zero-extend it as appropriate for the symbol's type. The DWARF
15153 standard (v4) is not entirely clear about the meaning of using
15154 DW_FORM_dataN for a constant with a signed type, where the type is
15155 wider than the data. The conclusion of a discussion on the DWARF
15156 list was that this is unspecified. We choose to always zero-extend
15157 because that is the interpretation long in use by GCC. */
15158
15159 static gdb_byte *
15160 dwarf2_const_value_data (struct attribute *attr, struct type *type,
15161 const char *name, struct obstack *obstack,
15162 struct dwarf2_cu *cu, LONGEST *value, int bits)
15163 {
15164 struct objfile *objfile = cu->objfile;
15165 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15166 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
15167 LONGEST l = DW_UNSND (attr);
15168
15169 if (bits < sizeof (*value) * 8)
15170 {
15171 l &= ((LONGEST) 1 << bits) - 1;
15172 *value = l;
15173 }
15174 else if (bits == sizeof (*value) * 8)
15175 *value = l;
15176 else
15177 {
15178 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15179 store_unsigned_integer (bytes, bits / 8, byte_order, l);
15180 return bytes;
15181 }
15182
15183 return NULL;
15184 }
15185
15186 /* Read a constant value from an attribute. Either set *VALUE, or if
15187 the value does not fit in *VALUE, set *BYTES - either already
15188 allocated on the objfile obstack, or newly allocated on OBSTACK,
15189 or, set *BATON, if we translated the constant to a location
15190 expression. */
15191
15192 static void
15193 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15194 const char *name, struct obstack *obstack,
15195 struct dwarf2_cu *cu,
15196 LONGEST *value, gdb_byte **bytes,
15197 struct dwarf2_locexpr_baton **baton)
15198 {
15199 struct objfile *objfile = cu->objfile;
15200 struct comp_unit_head *cu_header = &cu->header;
15201 struct dwarf_block *blk;
15202 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15203 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15204
15205 *value = 0;
15206 *bytes = NULL;
15207 *baton = NULL;
15208
15209 switch (attr->form)
15210 {
15211 case DW_FORM_addr:
15212 case DW_FORM_GNU_addr_index:
15213 {
15214 gdb_byte *data;
15215
15216 if (TYPE_LENGTH (type) != cu_header->addr_size)
15217 dwarf2_const_value_length_mismatch_complaint (name,
15218 cu_header->addr_size,
15219 TYPE_LENGTH (type));
15220 /* Symbols of this form are reasonably rare, so we just
15221 piggyback on the existing location code rather than writing
15222 a new implementation of symbol_computed_ops. */
15223 *baton = obstack_alloc (&objfile->objfile_obstack,
15224 sizeof (struct dwarf2_locexpr_baton));
15225 (*baton)->per_cu = cu->per_cu;
15226 gdb_assert ((*baton)->per_cu);
15227
15228 (*baton)->size = 2 + cu_header->addr_size;
15229 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15230 (*baton)->data = data;
15231
15232 data[0] = DW_OP_addr;
15233 store_unsigned_integer (&data[1], cu_header->addr_size,
15234 byte_order, DW_ADDR (attr));
15235 data[cu_header->addr_size + 1] = DW_OP_stack_value;
15236 }
15237 break;
15238 case DW_FORM_string:
15239 case DW_FORM_strp:
15240 case DW_FORM_GNU_str_index:
15241 case DW_FORM_GNU_strp_alt:
15242 /* DW_STRING is already allocated on the objfile obstack, point
15243 directly to it. */
15244 *bytes = (gdb_byte *) DW_STRING (attr);
15245 break;
15246 case DW_FORM_block1:
15247 case DW_FORM_block2:
15248 case DW_FORM_block4:
15249 case DW_FORM_block:
15250 case DW_FORM_exprloc:
15251 blk = DW_BLOCK (attr);
15252 if (TYPE_LENGTH (type) != blk->size)
15253 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15254 TYPE_LENGTH (type));
15255 *bytes = blk->data;
15256 break;
15257
15258 /* The DW_AT_const_value attributes are supposed to carry the
15259 symbol's value "represented as it would be on the target
15260 architecture." By the time we get here, it's already been
15261 converted to host endianness, so we just need to sign- or
15262 zero-extend it as appropriate. */
15263 case DW_FORM_data1:
15264 *bytes = dwarf2_const_value_data (attr, type, name,
15265 obstack, cu, value, 8);
15266 break;
15267 case DW_FORM_data2:
15268 *bytes = dwarf2_const_value_data (attr, type, name,
15269 obstack, cu, value, 16);
15270 break;
15271 case DW_FORM_data4:
15272 *bytes = dwarf2_const_value_data (attr, type, name,
15273 obstack, cu, value, 32);
15274 break;
15275 case DW_FORM_data8:
15276 *bytes = dwarf2_const_value_data (attr, type, name,
15277 obstack, cu, value, 64);
15278 break;
15279
15280 case DW_FORM_sdata:
15281 *value = DW_SND (attr);
15282 break;
15283
15284 case DW_FORM_udata:
15285 *value = DW_UNSND (attr);
15286 break;
15287
15288 default:
15289 complaint (&symfile_complaints,
15290 _("unsupported const value attribute form: '%s'"),
15291 dwarf_form_name (attr->form));
15292 *value = 0;
15293 break;
15294 }
15295 }
15296
15297
15298 /* Copy constant value from an attribute to a symbol. */
15299
15300 static void
15301 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15302 struct dwarf2_cu *cu)
15303 {
15304 struct objfile *objfile = cu->objfile;
15305 struct comp_unit_head *cu_header = &cu->header;
15306 LONGEST value;
15307 gdb_byte *bytes;
15308 struct dwarf2_locexpr_baton *baton;
15309
15310 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15311 SYMBOL_PRINT_NAME (sym),
15312 &objfile->objfile_obstack, cu,
15313 &value, &bytes, &baton);
15314
15315 if (baton != NULL)
15316 {
15317 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15318 SYMBOL_LOCATION_BATON (sym) = baton;
15319 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15320 }
15321 else if (bytes != NULL)
15322 {
15323 SYMBOL_VALUE_BYTES (sym) = bytes;
15324 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15325 }
15326 else
15327 {
15328 SYMBOL_VALUE (sym) = value;
15329 SYMBOL_CLASS (sym) = LOC_CONST;
15330 }
15331 }
15332
15333 /* Return the type of the die in question using its DW_AT_type attribute. */
15334
15335 static struct type *
15336 die_type (struct die_info *die, struct dwarf2_cu *cu)
15337 {
15338 struct attribute *type_attr;
15339
15340 type_attr = dwarf2_attr (die, DW_AT_type, cu);
15341 if (!type_attr)
15342 {
15343 /* A missing DW_AT_type represents a void type. */
15344 return objfile_type (cu->objfile)->builtin_void;
15345 }
15346
15347 return lookup_die_type (die, type_attr, cu);
15348 }
15349
15350 /* True iff CU's producer generates GNAT Ada auxiliary information
15351 that allows to find parallel types through that information instead
15352 of having to do expensive parallel lookups by type name. */
15353
15354 static int
15355 need_gnat_info (struct dwarf2_cu *cu)
15356 {
15357 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15358 of GNAT produces this auxiliary information, without any indication
15359 that it is produced. Part of enhancing the FSF version of GNAT
15360 to produce that information will be to put in place an indicator
15361 that we can use in order to determine whether the descriptive type
15362 info is available or not. One suggestion that has been made is
15363 to use a new attribute, attached to the CU die. For now, assume
15364 that the descriptive type info is not available. */
15365 return 0;
15366 }
15367
15368 /* Return the auxiliary type of the die in question using its
15369 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15370 attribute is not present. */
15371
15372 static struct type *
15373 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15374 {
15375 struct attribute *type_attr;
15376
15377 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15378 if (!type_attr)
15379 return NULL;
15380
15381 return lookup_die_type (die, type_attr, cu);
15382 }
15383
15384 /* If DIE has a descriptive_type attribute, then set the TYPE's
15385 descriptive type accordingly. */
15386
15387 static void
15388 set_descriptive_type (struct type *type, struct die_info *die,
15389 struct dwarf2_cu *cu)
15390 {
15391 struct type *descriptive_type = die_descriptive_type (die, cu);
15392
15393 if (descriptive_type)
15394 {
15395 ALLOCATE_GNAT_AUX_TYPE (type);
15396 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15397 }
15398 }
15399
15400 /* Return the containing type of the die in question using its
15401 DW_AT_containing_type attribute. */
15402
15403 static struct type *
15404 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15405 {
15406 struct attribute *type_attr;
15407
15408 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15409 if (!type_attr)
15410 error (_("Dwarf Error: Problem turning containing type into gdb type "
15411 "[in module %s]"), cu->objfile->name);
15412
15413 return lookup_die_type (die, type_attr, cu);
15414 }
15415
15416 /* Look up the type of DIE in CU using its type attribute ATTR.
15417 If there is no type substitute an error marker. */
15418
15419 static struct type *
15420 lookup_die_type (struct die_info *die, struct attribute *attr,
15421 struct dwarf2_cu *cu)
15422 {
15423 struct objfile *objfile = cu->objfile;
15424 struct type *this_type;
15425
15426 /* First see if we have it cached. */
15427
15428 if (attr->form == DW_FORM_GNU_ref_alt)
15429 {
15430 struct dwarf2_per_cu_data *per_cu;
15431 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15432
15433 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15434 this_type = get_die_type_at_offset (offset, per_cu);
15435 }
15436 else if (is_ref_attr (attr))
15437 {
15438 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15439
15440 this_type = get_die_type_at_offset (offset, cu->per_cu);
15441 }
15442 else if (attr->form == DW_FORM_ref_sig8)
15443 {
15444 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15445
15446 /* sig_type will be NULL if the signatured type is missing from
15447 the debug info. */
15448 if (sig_type == NULL)
15449 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15450 "at 0x%x [in module %s]"),
15451 die->offset.sect_off, objfile->name);
15452
15453 gdb_assert (sig_type->per_cu.is_debug_types);
15454 /* If we haven't filled in type_offset_in_section yet, then we
15455 haven't read the type in yet. */
15456 this_type = NULL;
15457 if (sig_type->type_offset_in_section.sect_off != 0)
15458 {
15459 this_type =
15460 get_die_type_at_offset (sig_type->type_offset_in_section,
15461 &sig_type->per_cu);
15462 }
15463 }
15464 else
15465 {
15466 dump_die_for_error (die);
15467 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15468 dwarf_attr_name (attr->name), objfile->name);
15469 }
15470
15471 /* If not cached we need to read it in. */
15472
15473 if (this_type == NULL)
15474 {
15475 struct die_info *type_die;
15476 struct dwarf2_cu *type_cu = cu;
15477
15478 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15479 /* If we found the type now, it's probably because the type came
15480 from an inter-CU reference and the type's CU got expanded before
15481 ours. */
15482 this_type = get_die_type (type_die, type_cu);
15483 if (this_type == NULL)
15484 this_type = read_type_die_1 (type_die, type_cu);
15485 }
15486
15487 /* If we still don't have a type use an error marker. */
15488
15489 if (this_type == NULL)
15490 {
15491 char *message, *saved;
15492
15493 /* read_type_die already issued a complaint. */
15494 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15495 objfile->name,
15496 cu->header.offset.sect_off,
15497 die->offset.sect_off);
15498 saved = obstack_copy0 (&objfile->objfile_obstack,
15499 message, strlen (message));
15500 xfree (message);
15501
15502 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15503 }
15504
15505 return this_type;
15506 }
15507
15508 /* Return the type in DIE, CU.
15509 Returns NULL for invalid types.
15510
15511 This first does a lookup in the appropriate type_hash table,
15512 and only reads the die in if necessary.
15513
15514 NOTE: This can be called when reading in partial or full symbols. */
15515
15516 static struct type *
15517 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15518 {
15519 struct type *this_type;
15520
15521 this_type = get_die_type (die, cu);
15522 if (this_type)
15523 return this_type;
15524
15525 return read_type_die_1 (die, cu);
15526 }
15527
15528 /* Read the type in DIE, CU.
15529 Returns NULL for invalid types. */
15530
15531 static struct type *
15532 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15533 {
15534 struct type *this_type = NULL;
15535
15536 switch (die->tag)
15537 {
15538 case DW_TAG_class_type:
15539 case DW_TAG_interface_type:
15540 case DW_TAG_structure_type:
15541 case DW_TAG_union_type:
15542 this_type = read_structure_type (die, cu);
15543 break;
15544 case DW_TAG_enumeration_type:
15545 this_type = read_enumeration_type (die, cu);
15546 break;
15547 case DW_TAG_subprogram:
15548 case DW_TAG_subroutine_type:
15549 case DW_TAG_inlined_subroutine:
15550 this_type = read_subroutine_type (die, cu);
15551 break;
15552 case DW_TAG_array_type:
15553 this_type = read_array_type (die, cu);
15554 break;
15555 case DW_TAG_set_type:
15556 this_type = read_set_type (die, cu);
15557 break;
15558 case DW_TAG_pointer_type:
15559 this_type = read_tag_pointer_type (die, cu);
15560 break;
15561 case DW_TAG_ptr_to_member_type:
15562 this_type = read_tag_ptr_to_member_type (die, cu);
15563 break;
15564 case DW_TAG_reference_type:
15565 this_type = read_tag_reference_type (die, cu);
15566 break;
15567 case DW_TAG_const_type:
15568 this_type = read_tag_const_type (die, cu);
15569 break;
15570 case DW_TAG_volatile_type:
15571 this_type = read_tag_volatile_type (die, cu);
15572 break;
15573 case DW_TAG_string_type:
15574 this_type = read_tag_string_type (die, cu);
15575 break;
15576 case DW_TAG_typedef:
15577 this_type = read_typedef (die, cu);
15578 break;
15579 case DW_TAG_subrange_type:
15580 this_type = read_subrange_type (die, cu);
15581 break;
15582 case DW_TAG_base_type:
15583 this_type = read_base_type (die, cu);
15584 break;
15585 case DW_TAG_unspecified_type:
15586 this_type = read_unspecified_type (die, cu);
15587 break;
15588 case DW_TAG_namespace:
15589 this_type = read_namespace_type (die, cu);
15590 break;
15591 case DW_TAG_module:
15592 this_type = read_module_type (die, cu);
15593 break;
15594 default:
15595 complaint (&symfile_complaints,
15596 _("unexpected tag in read_type_die: '%s'"),
15597 dwarf_tag_name (die->tag));
15598 break;
15599 }
15600
15601 return this_type;
15602 }
15603
15604 /* See if we can figure out if the class lives in a namespace. We do
15605 this by looking for a member function; its demangled name will
15606 contain namespace info, if there is any.
15607 Return the computed name or NULL.
15608 Space for the result is allocated on the objfile's obstack.
15609 This is the full-die version of guess_partial_die_structure_name.
15610 In this case we know DIE has no useful parent. */
15611
15612 static char *
15613 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15614 {
15615 struct die_info *spec_die;
15616 struct dwarf2_cu *spec_cu;
15617 struct die_info *child;
15618
15619 spec_cu = cu;
15620 spec_die = die_specification (die, &spec_cu);
15621 if (spec_die != NULL)
15622 {
15623 die = spec_die;
15624 cu = spec_cu;
15625 }
15626
15627 for (child = die->child;
15628 child != NULL;
15629 child = child->sibling)
15630 {
15631 if (child->tag == DW_TAG_subprogram)
15632 {
15633 struct attribute *attr;
15634
15635 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15636 if (attr == NULL)
15637 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15638 if (attr != NULL)
15639 {
15640 char *actual_name
15641 = language_class_name_from_physname (cu->language_defn,
15642 DW_STRING (attr));
15643 char *name = NULL;
15644
15645 if (actual_name != NULL)
15646 {
15647 char *die_name = dwarf2_name (die, cu);
15648
15649 if (die_name != NULL
15650 && strcmp (die_name, actual_name) != 0)
15651 {
15652 /* Strip off the class name from the full name.
15653 We want the prefix. */
15654 int die_name_len = strlen (die_name);
15655 int actual_name_len = strlen (actual_name);
15656
15657 /* Test for '::' as a sanity check. */
15658 if (actual_name_len > die_name_len + 2
15659 && actual_name[actual_name_len
15660 - die_name_len - 1] == ':')
15661 name =
15662 obsavestring (actual_name,
15663 actual_name_len - die_name_len - 2,
15664 &cu->objfile->objfile_obstack);
15665 }
15666 }
15667 xfree (actual_name);
15668 return name;
15669 }
15670 }
15671 }
15672
15673 return NULL;
15674 }
15675
15676 /* GCC might emit a nameless typedef that has a linkage name. Determine the
15677 prefix part in such case. See
15678 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15679
15680 static char *
15681 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15682 {
15683 struct attribute *attr;
15684 char *base;
15685
15686 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15687 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15688 return NULL;
15689
15690 attr = dwarf2_attr (die, DW_AT_name, cu);
15691 if (attr != NULL && DW_STRING (attr) != NULL)
15692 return NULL;
15693
15694 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15695 if (attr == NULL)
15696 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15697 if (attr == NULL || DW_STRING (attr) == NULL)
15698 return NULL;
15699
15700 /* dwarf2_name had to be already called. */
15701 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15702
15703 /* Strip the base name, keep any leading namespaces/classes. */
15704 base = strrchr (DW_STRING (attr), ':');
15705 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15706 return "";
15707
15708 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15709 &cu->objfile->objfile_obstack);
15710 }
15711
15712 /* Return the name of the namespace/class that DIE is defined within,
15713 or "" if we can't tell. The caller should not xfree the result.
15714
15715 For example, if we're within the method foo() in the following
15716 code:
15717
15718 namespace N {
15719 class C {
15720 void foo () {
15721 }
15722 };
15723 }
15724
15725 then determine_prefix on foo's die will return "N::C". */
15726
15727 static const char *
15728 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15729 {
15730 struct die_info *parent, *spec_die;
15731 struct dwarf2_cu *spec_cu;
15732 struct type *parent_type;
15733 char *retval;
15734
15735 if (cu->language != language_cplus && cu->language != language_java
15736 && cu->language != language_fortran)
15737 return "";
15738
15739 retval = anonymous_struct_prefix (die, cu);
15740 if (retval)
15741 return retval;
15742
15743 /* We have to be careful in the presence of DW_AT_specification.
15744 For example, with GCC 3.4, given the code
15745
15746 namespace N {
15747 void foo() {
15748 // Definition of N::foo.
15749 }
15750 }
15751
15752 then we'll have a tree of DIEs like this:
15753
15754 1: DW_TAG_compile_unit
15755 2: DW_TAG_namespace // N
15756 3: DW_TAG_subprogram // declaration of N::foo
15757 4: DW_TAG_subprogram // definition of N::foo
15758 DW_AT_specification // refers to die #3
15759
15760 Thus, when processing die #4, we have to pretend that we're in
15761 the context of its DW_AT_specification, namely the contex of die
15762 #3. */
15763 spec_cu = cu;
15764 spec_die = die_specification (die, &spec_cu);
15765 if (spec_die == NULL)
15766 parent = die->parent;
15767 else
15768 {
15769 parent = spec_die->parent;
15770 cu = spec_cu;
15771 }
15772
15773 if (parent == NULL)
15774 return "";
15775 else if (parent->building_fullname)
15776 {
15777 const char *name;
15778 const char *parent_name;
15779
15780 /* It has been seen on RealView 2.2 built binaries,
15781 DW_TAG_template_type_param types actually _defined_ as
15782 children of the parent class:
15783
15784 enum E {};
15785 template class <class Enum> Class{};
15786 Class<enum E> class_e;
15787
15788 1: DW_TAG_class_type (Class)
15789 2: DW_TAG_enumeration_type (E)
15790 3: DW_TAG_enumerator (enum1:0)
15791 3: DW_TAG_enumerator (enum2:1)
15792 ...
15793 2: DW_TAG_template_type_param
15794 DW_AT_type DW_FORM_ref_udata (E)
15795
15796 Besides being broken debug info, it can put GDB into an
15797 infinite loop. Consider:
15798
15799 When we're building the full name for Class<E>, we'll start
15800 at Class, and go look over its template type parameters,
15801 finding E. We'll then try to build the full name of E, and
15802 reach here. We're now trying to build the full name of E,
15803 and look over the parent DIE for containing scope. In the
15804 broken case, if we followed the parent DIE of E, we'd again
15805 find Class, and once again go look at its template type
15806 arguments, etc., etc. Simply don't consider such parent die
15807 as source-level parent of this die (it can't be, the language
15808 doesn't allow it), and break the loop here. */
15809 name = dwarf2_name (die, cu);
15810 parent_name = dwarf2_name (parent, cu);
15811 complaint (&symfile_complaints,
15812 _("template param type '%s' defined within parent '%s'"),
15813 name ? name : "<unknown>",
15814 parent_name ? parent_name : "<unknown>");
15815 return "";
15816 }
15817 else
15818 switch (parent->tag)
15819 {
15820 case DW_TAG_namespace:
15821 parent_type = read_type_die (parent, cu);
15822 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15823 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15824 Work around this problem here. */
15825 if (cu->language == language_cplus
15826 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15827 return "";
15828 /* We give a name to even anonymous namespaces. */
15829 return TYPE_TAG_NAME (parent_type);
15830 case DW_TAG_class_type:
15831 case DW_TAG_interface_type:
15832 case DW_TAG_structure_type:
15833 case DW_TAG_union_type:
15834 case DW_TAG_module:
15835 parent_type = read_type_die (parent, cu);
15836 if (TYPE_TAG_NAME (parent_type) != NULL)
15837 return TYPE_TAG_NAME (parent_type);
15838 else
15839 /* An anonymous structure is only allowed non-static data
15840 members; no typedefs, no member functions, et cetera.
15841 So it does not need a prefix. */
15842 return "";
15843 case DW_TAG_compile_unit:
15844 case DW_TAG_partial_unit:
15845 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15846 if (cu->language == language_cplus
15847 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15848 && die->child != NULL
15849 && (die->tag == DW_TAG_class_type
15850 || die->tag == DW_TAG_structure_type
15851 || die->tag == DW_TAG_union_type))
15852 {
15853 char *name = guess_full_die_structure_name (die, cu);
15854 if (name != NULL)
15855 return name;
15856 }
15857 return "";
15858 default:
15859 return determine_prefix (parent, cu);
15860 }
15861 }
15862
15863 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15864 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15865 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15866 an obconcat, otherwise allocate storage for the result. The CU argument is
15867 used to determine the language and hence, the appropriate separator. */
15868
15869 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
15870
15871 static char *
15872 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15873 int physname, struct dwarf2_cu *cu)
15874 {
15875 const char *lead = "";
15876 const char *sep;
15877
15878 if (suffix == NULL || suffix[0] == '\0'
15879 || prefix == NULL || prefix[0] == '\0')
15880 sep = "";
15881 else if (cu->language == language_java)
15882 sep = ".";
15883 else if (cu->language == language_fortran && physname)
15884 {
15885 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15886 DW_AT_MIPS_linkage_name is preferred and used instead. */
15887
15888 lead = "__";
15889 sep = "_MOD_";
15890 }
15891 else
15892 sep = "::";
15893
15894 if (prefix == NULL)
15895 prefix = "";
15896 if (suffix == NULL)
15897 suffix = "";
15898
15899 if (obs == NULL)
15900 {
15901 char *retval
15902 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15903
15904 strcpy (retval, lead);
15905 strcat (retval, prefix);
15906 strcat (retval, sep);
15907 strcat (retval, suffix);
15908 return retval;
15909 }
15910 else
15911 {
15912 /* We have an obstack. */
15913 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15914 }
15915 }
15916
15917 /* Return sibling of die, NULL if no sibling. */
15918
15919 static struct die_info *
15920 sibling_die (struct die_info *die)
15921 {
15922 return die->sibling;
15923 }
15924
15925 /* Get name of a die, return NULL if not found. */
15926
15927 static char *
15928 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15929 struct obstack *obstack)
15930 {
15931 if (name && cu->language == language_cplus)
15932 {
15933 char *canon_name = cp_canonicalize_string (name);
15934
15935 if (canon_name != NULL)
15936 {
15937 if (strcmp (canon_name, name) != 0)
15938 name = obsavestring (canon_name, strlen (canon_name),
15939 obstack);
15940 xfree (canon_name);
15941 }
15942 }
15943
15944 return name;
15945 }
15946
15947 /* Get name of a die, return NULL if not found. */
15948
15949 static char *
15950 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15951 {
15952 struct attribute *attr;
15953
15954 attr = dwarf2_attr (die, DW_AT_name, cu);
15955 if ((!attr || !DW_STRING (attr))
15956 && die->tag != DW_TAG_class_type
15957 && die->tag != DW_TAG_interface_type
15958 && die->tag != DW_TAG_structure_type
15959 && die->tag != DW_TAG_union_type)
15960 return NULL;
15961
15962 switch (die->tag)
15963 {
15964 case DW_TAG_compile_unit:
15965 case DW_TAG_partial_unit:
15966 /* Compilation units have a DW_AT_name that is a filename, not
15967 a source language identifier. */
15968 case DW_TAG_enumeration_type:
15969 case DW_TAG_enumerator:
15970 /* These tags always have simple identifiers already; no need
15971 to canonicalize them. */
15972 return DW_STRING (attr);
15973
15974 case DW_TAG_subprogram:
15975 /* Java constructors will all be named "<init>", so return
15976 the class name when we see this special case. */
15977 if (cu->language == language_java
15978 && DW_STRING (attr) != NULL
15979 && strcmp (DW_STRING (attr), "<init>") == 0)
15980 {
15981 struct dwarf2_cu *spec_cu = cu;
15982 struct die_info *spec_die;
15983
15984 /* GCJ will output '<init>' for Java constructor names.
15985 For this special case, return the name of the parent class. */
15986
15987 /* GCJ may output suprogram DIEs with AT_specification set.
15988 If so, use the name of the specified DIE. */
15989 spec_die = die_specification (die, &spec_cu);
15990 if (spec_die != NULL)
15991 return dwarf2_name (spec_die, spec_cu);
15992
15993 do
15994 {
15995 die = die->parent;
15996 if (die->tag == DW_TAG_class_type)
15997 return dwarf2_name (die, cu);
15998 }
15999 while (die->tag != DW_TAG_compile_unit
16000 && die->tag != DW_TAG_partial_unit);
16001 }
16002 break;
16003
16004 case DW_TAG_class_type:
16005 case DW_TAG_interface_type:
16006 case DW_TAG_structure_type:
16007 case DW_TAG_union_type:
16008 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16009 structures or unions. These were of the form "._%d" in GCC 4.1,
16010 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16011 and GCC 4.4. We work around this problem by ignoring these. */
16012 if (attr && DW_STRING (attr)
16013 && (strncmp (DW_STRING (attr), "._", 2) == 0
16014 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16015 return NULL;
16016
16017 /* GCC might emit a nameless typedef that has a linkage name. See
16018 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16019 if (!attr || DW_STRING (attr) == NULL)
16020 {
16021 char *demangled = NULL;
16022
16023 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16024 if (attr == NULL)
16025 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16026
16027 if (attr == NULL || DW_STRING (attr) == NULL)
16028 return NULL;
16029
16030 /* Avoid demangling DW_STRING (attr) the second time on a second
16031 call for the same DIE. */
16032 if (!DW_STRING_IS_CANONICAL (attr))
16033 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16034
16035 if (demangled)
16036 {
16037 char *base;
16038
16039 /* FIXME: we already did this for the partial symbol... */
16040 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16041 &cu->objfile->objfile_obstack);
16042 DW_STRING_IS_CANONICAL (attr) = 1;
16043 xfree (demangled);
16044
16045 /* Strip any leading namespaces/classes, keep only the base name.
16046 DW_AT_name for named DIEs does not contain the prefixes. */
16047 base = strrchr (DW_STRING (attr), ':');
16048 if (base && base > DW_STRING (attr) && base[-1] == ':')
16049 return &base[1];
16050 else
16051 return DW_STRING (attr);
16052 }
16053 }
16054 break;
16055
16056 default:
16057 break;
16058 }
16059
16060 if (!DW_STRING_IS_CANONICAL (attr))
16061 {
16062 DW_STRING (attr)
16063 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16064 &cu->objfile->objfile_obstack);
16065 DW_STRING_IS_CANONICAL (attr) = 1;
16066 }
16067 return DW_STRING (attr);
16068 }
16069
16070 /* Return the die that this die in an extension of, or NULL if there
16071 is none. *EXT_CU is the CU containing DIE on input, and the CU
16072 containing the return value on output. */
16073
16074 static struct die_info *
16075 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16076 {
16077 struct attribute *attr;
16078
16079 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16080 if (attr == NULL)
16081 return NULL;
16082
16083 return follow_die_ref (die, attr, ext_cu);
16084 }
16085
16086 /* Convert a DIE tag into its string name. */
16087
16088 static const char *
16089 dwarf_tag_name (unsigned tag)
16090 {
16091 const char *name = get_DW_TAG_name (tag);
16092
16093 if (name == NULL)
16094 return "DW_TAG_<unknown>";
16095
16096 return name;
16097 }
16098
16099 /* Convert a DWARF attribute code into its string name. */
16100
16101 static const char *
16102 dwarf_attr_name (unsigned attr)
16103 {
16104 const char *name;
16105
16106 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16107 if (attr == DW_AT_MIPS_fde)
16108 return "DW_AT_MIPS_fde";
16109 #else
16110 if (attr == DW_AT_HP_block_index)
16111 return "DW_AT_HP_block_index";
16112 #endif
16113
16114 name = get_DW_AT_name (attr);
16115
16116 if (name == NULL)
16117 return "DW_AT_<unknown>";
16118
16119 return name;
16120 }
16121
16122 /* Convert a DWARF value form code into its string name. */
16123
16124 static const char *
16125 dwarf_form_name (unsigned form)
16126 {
16127 const char *name = get_DW_FORM_name (form);
16128
16129 if (name == NULL)
16130 return "DW_FORM_<unknown>";
16131
16132 return name;
16133 }
16134
16135 static char *
16136 dwarf_bool_name (unsigned mybool)
16137 {
16138 if (mybool)
16139 return "TRUE";
16140 else
16141 return "FALSE";
16142 }
16143
16144 /* Convert a DWARF type code into its string name. */
16145
16146 static const char *
16147 dwarf_type_encoding_name (unsigned enc)
16148 {
16149 const char *name = get_DW_ATE_name (enc);
16150
16151 if (name == NULL)
16152 return "DW_ATE_<unknown>";
16153
16154 return name;
16155 }
16156
16157 static void
16158 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
16159 {
16160 unsigned int i;
16161
16162 print_spaces (indent, f);
16163 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
16164 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
16165
16166 if (die->parent != NULL)
16167 {
16168 print_spaces (indent, f);
16169 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
16170 die->parent->offset.sect_off);
16171 }
16172
16173 print_spaces (indent, f);
16174 fprintf_unfiltered (f, " has children: %s\n",
16175 dwarf_bool_name (die->child != NULL));
16176
16177 print_spaces (indent, f);
16178 fprintf_unfiltered (f, " attributes:\n");
16179
16180 for (i = 0; i < die->num_attrs; ++i)
16181 {
16182 print_spaces (indent, f);
16183 fprintf_unfiltered (f, " %s (%s) ",
16184 dwarf_attr_name (die->attrs[i].name),
16185 dwarf_form_name (die->attrs[i].form));
16186
16187 switch (die->attrs[i].form)
16188 {
16189 case DW_FORM_addr:
16190 case DW_FORM_GNU_addr_index:
16191 fprintf_unfiltered (f, "address: ");
16192 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
16193 break;
16194 case DW_FORM_block2:
16195 case DW_FORM_block4:
16196 case DW_FORM_block:
16197 case DW_FORM_block1:
16198 fprintf_unfiltered (f, "block: size %d",
16199 DW_BLOCK (&die->attrs[i])->size);
16200 break;
16201 case DW_FORM_exprloc:
16202 fprintf_unfiltered (f, "expression: size %u",
16203 DW_BLOCK (&die->attrs[i])->size);
16204 break;
16205 case DW_FORM_ref_addr:
16206 fprintf_unfiltered (f, "ref address: ");
16207 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16208 break;
16209 case DW_FORM_GNU_ref_alt:
16210 fprintf_unfiltered (f, "alt ref address: ");
16211 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16212 break;
16213 case DW_FORM_ref1:
16214 case DW_FORM_ref2:
16215 case DW_FORM_ref4:
16216 case DW_FORM_ref8:
16217 case DW_FORM_ref_udata:
16218 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
16219 (long) (DW_UNSND (&die->attrs[i])));
16220 break;
16221 case DW_FORM_data1:
16222 case DW_FORM_data2:
16223 case DW_FORM_data4:
16224 case DW_FORM_data8:
16225 case DW_FORM_udata:
16226 case DW_FORM_sdata:
16227 fprintf_unfiltered (f, "constant: %s",
16228 pulongest (DW_UNSND (&die->attrs[i])));
16229 break;
16230 case DW_FORM_sec_offset:
16231 fprintf_unfiltered (f, "section offset: %s",
16232 pulongest (DW_UNSND (&die->attrs[i])));
16233 break;
16234 case DW_FORM_ref_sig8:
16235 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16236 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
16237 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
16238 else
16239 fprintf_unfiltered (f, "signatured type, offset: unknown");
16240 break;
16241 case DW_FORM_string:
16242 case DW_FORM_strp:
16243 case DW_FORM_GNU_str_index:
16244 case DW_FORM_GNU_strp_alt:
16245 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
16246 DW_STRING (&die->attrs[i])
16247 ? DW_STRING (&die->attrs[i]) : "",
16248 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
16249 break;
16250 case DW_FORM_flag:
16251 if (DW_UNSND (&die->attrs[i]))
16252 fprintf_unfiltered (f, "flag: TRUE");
16253 else
16254 fprintf_unfiltered (f, "flag: FALSE");
16255 break;
16256 case DW_FORM_flag_present:
16257 fprintf_unfiltered (f, "flag: TRUE");
16258 break;
16259 case DW_FORM_indirect:
16260 /* The reader will have reduced the indirect form to
16261 the "base form" so this form should not occur. */
16262 fprintf_unfiltered (f,
16263 "unexpected attribute form: DW_FORM_indirect");
16264 break;
16265 default:
16266 fprintf_unfiltered (f, "unsupported attribute form: %d.",
16267 die->attrs[i].form);
16268 break;
16269 }
16270 fprintf_unfiltered (f, "\n");
16271 }
16272 }
16273
16274 static void
16275 dump_die_for_error (struct die_info *die)
16276 {
16277 dump_die_shallow (gdb_stderr, 0, die);
16278 }
16279
16280 static void
16281 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16282 {
16283 int indent = level * 4;
16284
16285 gdb_assert (die != NULL);
16286
16287 if (level >= max_level)
16288 return;
16289
16290 dump_die_shallow (f, indent, die);
16291
16292 if (die->child != NULL)
16293 {
16294 print_spaces (indent, f);
16295 fprintf_unfiltered (f, " Children:");
16296 if (level + 1 < max_level)
16297 {
16298 fprintf_unfiltered (f, "\n");
16299 dump_die_1 (f, level + 1, max_level, die->child);
16300 }
16301 else
16302 {
16303 fprintf_unfiltered (f,
16304 " [not printed, max nesting level reached]\n");
16305 }
16306 }
16307
16308 if (die->sibling != NULL && level > 0)
16309 {
16310 dump_die_1 (f, level, max_level, die->sibling);
16311 }
16312 }
16313
16314 /* This is called from the pdie macro in gdbinit.in.
16315 It's not static so gcc will keep a copy callable from gdb. */
16316
16317 void
16318 dump_die (struct die_info *die, int max_level)
16319 {
16320 dump_die_1 (gdb_stdlog, 0, max_level, die);
16321 }
16322
16323 static void
16324 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16325 {
16326 void **slot;
16327
16328 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16329 INSERT);
16330
16331 *slot = die;
16332 }
16333
16334 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16335 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16336
16337 static int
16338 is_ref_attr (struct attribute *attr)
16339 {
16340 switch (attr->form)
16341 {
16342 case DW_FORM_ref_addr:
16343 case DW_FORM_ref1:
16344 case DW_FORM_ref2:
16345 case DW_FORM_ref4:
16346 case DW_FORM_ref8:
16347 case DW_FORM_ref_udata:
16348 case DW_FORM_GNU_ref_alt:
16349 return 1;
16350 default:
16351 return 0;
16352 }
16353 }
16354
16355 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16356 required kind. */
16357
16358 static sect_offset
16359 dwarf2_get_ref_die_offset (struct attribute *attr)
16360 {
16361 sect_offset retval = { DW_UNSND (attr) };
16362
16363 if (is_ref_attr (attr))
16364 return retval;
16365
16366 retval.sect_off = 0;
16367 complaint (&symfile_complaints,
16368 _("unsupported die ref attribute form: '%s'"),
16369 dwarf_form_name (attr->form));
16370 return retval;
16371 }
16372
16373 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16374 * the value held by the attribute is not constant. */
16375
16376 static LONGEST
16377 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16378 {
16379 if (attr->form == DW_FORM_sdata)
16380 return DW_SND (attr);
16381 else if (attr->form == DW_FORM_udata
16382 || attr->form == DW_FORM_data1
16383 || attr->form == DW_FORM_data2
16384 || attr->form == DW_FORM_data4
16385 || attr->form == DW_FORM_data8)
16386 return DW_UNSND (attr);
16387 else
16388 {
16389 complaint (&symfile_complaints,
16390 _("Attribute value is not a constant (%s)"),
16391 dwarf_form_name (attr->form));
16392 return default_value;
16393 }
16394 }
16395
16396 /* Follow reference or signature attribute ATTR of SRC_DIE.
16397 On entry *REF_CU is the CU of SRC_DIE.
16398 On exit *REF_CU is the CU of the result. */
16399
16400 static struct die_info *
16401 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16402 struct dwarf2_cu **ref_cu)
16403 {
16404 struct die_info *die;
16405
16406 if (is_ref_attr (attr))
16407 die = follow_die_ref (src_die, attr, ref_cu);
16408 else if (attr->form == DW_FORM_ref_sig8)
16409 die = follow_die_sig (src_die, attr, ref_cu);
16410 else
16411 {
16412 dump_die_for_error (src_die);
16413 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16414 (*ref_cu)->objfile->name);
16415 }
16416
16417 return die;
16418 }
16419
16420 /* Follow reference OFFSET.
16421 On entry *REF_CU is the CU of the source die referencing OFFSET.
16422 On exit *REF_CU is the CU of the result.
16423 Returns NULL if OFFSET is invalid. */
16424
16425 static struct die_info *
16426 follow_die_offset (sect_offset offset, int offset_in_dwz,
16427 struct dwarf2_cu **ref_cu)
16428 {
16429 struct die_info temp_die;
16430 struct dwarf2_cu *target_cu, *cu = *ref_cu;
16431
16432 gdb_assert (cu->per_cu != NULL);
16433
16434 target_cu = cu;
16435
16436 if (cu->per_cu->is_debug_types)
16437 {
16438 /* .debug_types CUs cannot reference anything outside their CU.
16439 If they need to, they have to reference a signatured type via
16440 DW_FORM_ref_sig8. */
16441 if (! offset_in_cu_p (&cu->header, offset))
16442 return NULL;
16443 }
16444 else if (offset_in_dwz != cu->per_cu->is_dwz
16445 || ! offset_in_cu_p (&cu->header, offset))
16446 {
16447 struct dwarf2_per_cu_data *per_cu;
16448
16449 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16450 cu->objfile);
16451
16452 /* If necessary, add it to the queue and load its DIEs. */
16453 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16454 load_full_comp_unit (per_cu, cu->language);
16455
16456 target_cu = per_cu->cu;
16457 }
16458 else if (cu->dies == NULL)
16459 {
16460 /* We're loading full DIEs during partial symbol reading. */
16461 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16462 load_full_comp_unit (cu->per_cu, language_minimal);
16463 }
16464
16465 *ref_cu = target_cu;
16466 temp_die.offset = offset;
16467 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16468 }
16469
16470 /* Follow reference attribute ATTR of SRC_DIE.
16471 On entry *REF_CU is the CU of SRC_DIE.
16472 On exit *REF_CU is the CU of the result. */
16473
16474 static struct die_info *
16475 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16476 struct dwarf2_cu **ref_cu)
16477 {
16478 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16479 struct dwarf2_cu *cu = *ref_cu;
16480 struct die_info *die;
16481
16482 die = follow_die_offset (offset,
16483 (attr->form == DW_FORM_GNU_ref_alt
16484 || cu->per_cu->is_dwz),
16485 ref_cu);
16486 if (!die)
16487 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16488 "at 0x%x [in module %s]"),
16489 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16490
16491 return die;
16492 }
16493
16494 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16495 Returned value is intended for DW_OP_call*. Returned
16496 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
16497
16498 struct dwarf2_locexpr_baton
16499 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16500 struct dwarf2_per_cu_data *per_cu,
16501 CORE_ADDR (*get_frame_pc) (void *baton),
16502 void *baton)
16503 {
16504 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16505 struct dwarf2_cu *cu;
16506 struct die_info *die;
16507 struct attribute *attr;
16508 struct dwarf2_locexpr_baton retval;
16509
16510 dw2_setup (per_cu->objfile);
16511
16512 if (per_cu->cu == NULL)
16513 load_cu (per_cu);
16514 cu = per_cu->cu;
16515
16516 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
16517 if (!die)
16518 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16519 offset.sect_off, per_cu->objfile->name);
16520
16521 attr = dwarf2_attr (die, DW_AT_location, cu);
16522 if (!attr)
16523 {
16524 /* DWARF: "If there is no such attribute, then there is no effect.".
16525 DATA is ignored if SIZE is 0. */
16526
16527 retval.data = NULL;
16528 retval.size = 0;
16529 }
16530 else if (attr_form_is_section_offset (attr))
16531 {
16532 struct dwarf2_loclist_baton loclist_baton;
16533 CORE_ADDR pc = (*get_frame_pc) (baton);
16534 size_t size;
16535
16536 fill_in_loclist_baton (cu, &loclist_baton, attr);
16537
16538 retval.data = dwarf2_find_location_expression (&loclist_baton,
16539 &size, pc);
16540 retval.size = size;
16541 }
16542 else
16543 {
16544 if (!attr_form_is_block (attr))
16545 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16546 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16547 offset.sect_off, per_cu->objfile->name);
16548
16549 retval.data = DW_BLOCK (attr)->data;
16550 retval.size = DW_BLOCK (attr)->size;
16551 }
16552 retval.per_cu = cu->per_cu;
16553
16554 age_cached_comp_units ();
16555
16556 return retval;
16557 }
16558
16559 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16560 PER_CU. */
16561
16562 struct type *
16563 dwarf2_get_die_type (cu_offset die_offset,
16564 struct dwarf2_per_cu_data *per_cu)
16565 {
16566 sect_offset die_offset_sect;
16567
16568 dw2_setup (per_cu->objfile);
16569
16570 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16571 return get_die_type_at_offset (die_offset_sect, per_cu);
16572 }
16573
16574 /* Follow the signature attribute ATTR in SRC_DIE.
16575 On entry *REF_CU is the CU of SRC_DIE.
16576 On exit *REF_CU is the CU of the result. */
16577
16578 static struct die_info *
16579 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16580 struct dwarf2_cu **ref_cu)
16581 {
16582 struct objfile *objfile = (*ref_cu)->objfile;
16583 struct die_info temp_die;
16584 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16585 struct dwarf2_cu *sig_cu;
16586 struct die_info *die;
16587
16588 /* sig_type will be NULL if the signatured type is missing from
16589 the debug info. */
16590 if (sig_type == NULL)
16591 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16592 "at 0x%x [in module %s]"),
16593 src_die->offset.sect_off, objfile->name);
16594
16595 /* If necessary, add it to the queue and load its DIEs. */
16596
16597 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16598 read_signatured_type (sig_type);
16599
16600 gdb_assert (sig_type->per_cu.cu != NULL);
16601
16602 sig_cu = sig_type->per_cu.cu;
16603 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16604 temp_die.offset = sig_type->type_offset_in_section;
16605 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16606 temp_die.offset.sect_off);
16607 if (die)
16608 {
16609 *ref_cu = sig_cu;
16610 return die;
16611 }
16612
16613 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16614 "from DIE at 0x%x [in module %s]"),
16615 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16616 }
16617
16618 /* Given an offset of a signatured type, return its signatured_type. */
16619
16620 static struct signatured_type *
16621 lookup_signatured_type_at_offset (struct objfile *objfile,
16622 struct dwarf2_section_info *section,
16623 sect_offset offset)
16624 {
16625 gdb_byte *info_ptr = section->buffer + offset.sect_off;
16626 unsigned int length, initial_length_size;
16627 unsigned int sig_offset;
16628 struct signatured_type find_entry, *sig_type;
16629
16630 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16631 sig_offset = (initial_length_size
16632 + 2 /*version*/
16633 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16634 + 1 /*address_size*/);
16635 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16636 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16637
16638 /* This is only used to lookup previously recorded types.
16639 If we didn't find it, it's our bug. */
16640 gdb_assert (sig_type != NULL);
16641 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16642
16643 return sig_type;
16644 }
16645
16646 /* Load the DIEs associated with type unit PER_CU into memory. */
16647
16648 static void
16649 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16650 {
16651 struct signatured_type *sig_type;
16652
16653 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16654 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16655
16656 /* We have the per_cu, but we need the signatured_type.
16657 Fortunately this is an easy translation. */
16658 gdb_assert (per_cu->is_debug_types);
16659 sig_type = (struct signatured_type *) per_cu;
16660
16661 gdb_assert (per_cu->cu == NULL);
16662
16663 read_signatured_type (sig_type);
16664
16665 gdb_assert (per_cu->cu != NULL);
16666 }
16667
16668 /* die_reader_func for read_signatured_type.
16669 This is identical to load_full_comp_unit_reader,
16670 but is kept separate for now. */
16671
16672 static void
16673 read_signatured_type_reader (const struct die_reader_specs *reader,
16674 gdb_byte *info_ptr,
16675 struct die_info *comp_unit_die,
16676 int has_children,
16677 void *data)
16678 {
16679 struct dwarf2_cu *cu = reader->cu;
16680
16681 gdb_assert (cu->die_hash == NULL);
16682 cu->die_hash =
16683 htab_create_alloc_ex (cu->header.length / 12,
16684 die_hash,
16685 die_eq,
16686 NULL,
16687 &cu->comp_unit_obstack,
16688 hashtab_obstack_allocate,
16689 dummy_obstack_deallocate);
16690
16691 if (has_children)
16692 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16693 &info_ptr, comp_unit_die);
16694 cu->dies = comp_unit_die;
16695 /* comp_unit_die is not stored in die_hash, no need. */
16696
16697 /* We try not to read any attributes in this function, because not
16698 all CUs needed for references have been loaded yet, and symbol
16699 table processing isn't initialized. But we have to set the CU language,
16700 or we won't be able to build types correctly.
16701 Similarly, if we do not read the producer, we can not apply
16702 producer-specific interpretation. */
16703 prepare_one_comp_unit (cu, cu->dies, language_minimal);
16704 }
16705
16706 /* Read in a signatured type and build its CU and DIEs.
16707 If the type is a stub for the real type in a DWO file,
16708 read in the real type from the DWO file as well. */
16709
16710 static void
16711 read_signatured_type (struct signatured_type *sig_type)
16712 {
16713 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16714
16715 gdb_assert (per_cu->is_debug_types);
16716 gdb_assert (per_cu->cu == NULL);
16717
16718 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16719 read_signatured_type_reader, NULL);
16720 }
16721
16722 /* Decode simple location descriptions.
16723 Given a pointer to a dwarf block that defines a location, compute
16724 the location and return the value.
16725
16726 NOTE drow/2003-11-18: This function is called in two situations
16727 now: for the address of static or global variables (partial symbols
16728 only) and for offsets into structures which are expected to be
16729 (more or less) constant. The partial symbol case should go away,
16730 and only the constant case should remain. That will let this
16731 function complain more accurately. A few special modes are allowed
16732 without complaint for global variables (for instance, global
16733 register values and thread-local values).
16734
16735 A location description containing no operations indicates that the
16736 object is optimized out. The return value is 0 for that case.
16737 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16738 callers will only want a very basic result and this can become a
16739 complaint.
16740
16741 Note that stack[0] is unused except as a default error return. */
16742
16743 static CORE_ADDR
16744 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16745 {
16746 struct objfile *objfile = cu->objfile;
16747 int i;
16748 int size = blk->size;
16749 gdb_byte *data = blk->data;
16750 CORE_ADDR stack[64];
16751 int stacki;
16752 unsigned int bytes_read, unsnd;
16753 gdb_byte op;
16754
16755 i = 0;
16756 stacki = 0;
16757 stack[stacki] = 0;
16758 stack[++stacki] = 0;
16759
16760 while (i < size)
16761 {
16762 op = data[i++];
16763 switch (op)
16764 {
16765 case DW_OP_lit0:
16766 case DW_OP_lit1:
16767 case DW_OP_lit2:
16768 case DW_OP_lit3:
16769 case DW_OP_lit4:
16770 case DW_OP_lit5:
16771 case DW_OP_lit6:
16772 case DW_OP_lit7:
16773 case DW_OP_lit8:
16774 case DW_OP_lit9:
16775 case DW_OP_lit10:
16776 case DW_OP_lit11:
16777 case DW_OP_lit12:
16778 case DW_OP_lit13:
16779 case DW_OP_lit14:
16780 case DW_OP_lit15:
16781 case DW_OP_lit16:
16782 case DW_OP_lit17:
16783 case DW_OP_lit18:
16784 case DW_OP_lit19:
16785 case DW_OP_lit20:
16786 case DW_OP_lit21:
16787 case DW_OP_lit22:
16788 case DW_OP_lit23:
16789 case DW_OP_lit24:
16790 case DW_OP_lit25:
16791 case DW_OP_lit26:
16792 case DW_OP_lit27:
16793 case DW_OP_lit28:
16794 case DW_OP_lit29:
16795 case DW_OP_lit30:
16796 case DW_OP_lit31:
16797 stack[++stacki] = op - DW_OP_lit0;
16798 break;
16799
16800 case DW_OP_reg0:
16801 case DW_OP_reg1:
16802 case DW_OP_reg2:
16803 case DW_OP_reg3:
16804 case DW_OP_reg4:
16805 case DW_OP_reg5:
16806 case DW_OP_reg6:
16807 case DW_OP_reg7:
16808 case DW_OP_reg8:
16809 case DW_OP_reg9:
16810 case DW_OP_reg10:
16811 case DW_OP_reg11:
16812 case DW_OP_reg12:
16813 case DW_OP_reg13:
16814 case DW_OP_reg14:
16815 case DW_OP_reg15:
16816 case DW_OP_reg16:
16817 case DW_OP_reg17:
16818 case DW_OP_reg18:
16819 case DW_OP_reg19:
16820 case DW_OP_reg20:
16821 case DW_OP_reg21:
16822 case DW_OP_reg22:
16823 case DW_OP_reg23:
16824 case DW_OP_reg24:
16825 case DW_OP_reg25:
16826 case DW_OP_reg26:
16827 case DW_OP_reg27:
16828 case DW_OP_reg28:
16829 case DW_OP_reg29:
16830 case DW_OP_reg30:
16831 case DW_OP_reg31:
16832 stack[++stacki] = op - DW_OP_reg0;
16833 if (i < size)
16834 dwarf2_complex_location_expr_complaint ();
16835 break;
16836
16837 case DW_OP_regx:
16838 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16839 i += bytes_read;
16840 stack[++stacki] = unsnd;
16841 if (i < size)
16842 dwarf2_complex_location_expr_complaint ();
16843 break;
16844
16845 case DW_OP_addr:
16846 stack[++stacki] = read_address (objfile->obfd, &data[i],
16847 cu, &bytes_read);
16848 i += bytes_read;
16849 break;
16850
16851 case DW_OP_const1u:
16852 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16853 i += 1;
16854 break;
16855
16856 case DW_OP_const1s:
16857 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16858 i += 1;
16859 break;
16860
16861 case DW_OP_const2u:
16862 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16863 i += 2;
16864 break;
16865
16866 case DW_OP_const2s:
16867 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16868 i += 2;
16869 break;
16870
16871 case DW_OP_const4u:
16872 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16873 i += 4;
16874 break;
16875
16876 case DW_OP_const4s:
16877 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16878 i += 4;
16879 break;
16880
16881 case DW_OP_const8u:
16882 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16883 i += 8;
16884 break;
16885
16886 case DW_OP_constu:
16887 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16888 &bytes_read);
16889 i += bytes_read;
16890 break;
16891
16892 case DW_OP_consts:
16893 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16894 i += bytes_read;
16895 break;
16896
16897 case DW_OP_dup:
16898 stack[stacki + 1] = stack[stacki];
16899 stacki++;
16900 break;
16901
16902 case DW_OP_plus:
16903 stack[stacki - 1] += stack[stacki];
16904 stacki--;
16905 break;
16906
16907 case DW_OP_plus_uconst:
16908 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16909 &bytes_read);
16910 i += bytes_read;
16911 break;
16912
16913 case DW_OP_minus:
16914 stack[stacki - 1] -= stack[stacki];
16915 stacki--;
16916 break;
16917
16918 case DW_OP_deref:
16919 /* If we're not the last op, then we definitely can't encode
16920 this using GDB's address_class enum. This is valid for partial
16921 global symbols, although the variable's address will be bogus
16922 in the psymtab. */
16923 if (i < size)
16924 dwarf2_complex_location_expr_complaint ();
16925 break;
16926
16927 case DW_OP_GNU_push_tls_address:
16928 /* The top of the stack has the offset from the beginning
16929 of the thread control block at which the variable is located. */
16930 /* Nothing should follow this operator, so the top of stack would
16931 be returned. */
16932 /* This is valid for partial global symbols, but the variable's
16933 address will be bogus in the psymtab. Make it always at least
16934 non-zero to not look as a variable garbage collected by linker
16935 which have DW_OP_addr 0. */
16936 if (i < size)
16937 dwarf2_complex_location_expr_complaint ();
16938 stack[stacki]++;
16939 break;
16940
16941 case DW_OP_GNU_uninit:
16942 break;
16943
16944 case DW_OP_GNU_addr_index:
16945 case DW_OP_GNU_const_index:
16946 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16947 &bytes_read);
16948 i += bytes_read;
16949 break;
16950
16951 default:
16952 {
16953 const char *name = get_DW_OP_name (op);
16954
16955 if (name)
16956 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16957 name);
16958 else
16959 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16960 op);
16961 }
16962
16963 return (stack[stacki]);
16964 }
16965
16966 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16967 outside of the allocated space. Also enforce minimum>0. */
16968 if (stacki >= ARRAY_SIZE (stack) - 1)
16969 {
16970 complaint (&symfile_complaints,
16971 _("location description stack overflow"));
16972 return 0;
16973 }
16974
16975 if (stacki <= 0)
16976 {
16977 complaint (&symfile_complaints,
16978 _("location description stack underflow"));
16979 return 0;
16980 }
16981 }
16982 return (stack[stacki]);
16983 }
16984
16985 /* memory allocation interface */
16986
16987 static struct dwarf_block *
16988 dwarf_alloc_block (struct dwarf2_cu *cu)
16989 {
16990 struct dwarf_block *blk;
16991
16992 blk = (struct dwarf_block *)
16993 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16994 return (blk);
16995 }
16996
16997 static struct die_info *
16998 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
16999 {
17000 struct die_info *die;
17001 size_t size = sizeof (struct die_info);
17002
17003 if (num_attrs > 1)
17004 size += (num_attrs - 1) * sizeof (struct attribute);
17005
17006 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17007 memset (die, 0, sizeof (struct die_info));
17008 return (die);
17009 }
17010
17011 \f
17012 /* Macro support. */
17013
17014 /* Return the full name of file number I in *LH's file name table.
17015 Use COMP_DIR as the name of the current directory of the
17016 compilation. The result is allocated using xmalloc; the caller is
17017 responsible for freeing it. */
17018 static char *
17019 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17020 {
17021 /* Is the file number a valid index into the line header's file name
17022 table? Remember that file numbers start with one, not zero. */
17023 if (1 <= file && file <= lh->num_file_names)
17024 {
17025 struct file_entry *fe = &lh->file_names[file - 1];
17026
17027 if (IS_ABSOLUTE_PATH (fe->name))
17028 return xstrdup (fe->name);
17029 else
17030 {
17031 const char *dir;
17032 int dir_len;
17033 char *full_name;
17034
17035 if (fe->dir_index)
17036 dir = lh->include_dirs[fe->dir_index - 1];
17037 else
17038 dir = comp_dir;
17039
17040 if (dir)
17041 {
17042 dir_len = strlen (dir);
17043 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17044 strcpy (full_name, dir);
17045 full_name[dir_len] = '/';
17046 strcpy (full_name + dir_len + 1, fe->name);
17047 return full_name;
17048 }
17049 else
17050 return xstrdup (fe->name);
17051 }
17052 }
17053 else
17054 {
17055 /* The compiler produced a bogus file number. We can at least
17056 record the macro definitions made in the file, even if we
17057 won't be able to find the file by name. */
17058 char fake_name[80];
17059
17060 sprintf (fake_name, "<bad macro file number %d>", file);
17061
17062 complaint (&symfile_complaints,
17063 _("bad file number in macro information (%d)"),
17064 file);
17065
17066 return xstrdup (fake_name);
17067 }
17068 }
17069
17070
17071 static struct macro_source_file *
17072 macro_start_file (int file, int line,
17073 struct macro_source_file *current_file,
17074 const char *comp_dir,
17075 struct line_header *lh, struct objfile *objfile)
17076 {
17077 /* The full name of this source file. */
17078 char *full_name = file_full_name (file, lh, comp_dir);
17079
17080 /* We don't create a macro table for this compilation unit
17081 at all until we actually get a filename. */
17082 if (! pending_macros)
17083 pending_macros = new_macro_table (&objfile->objfile_obstack,
17084 objfile->macro_cache);
17085
17086 if (! current_file)
17087 {
17088 /* If we have no current file, then this must be the start_file
17089 directive for the compilation unit's main source file. */
17090 current_file = macro_set_main (pending_macros, full_name);
17091 macro_define_special (pending_macros);
17092 }
17093 else
17094 current_file = macro_include (current_file, line, full_name);
17095
17096 xfree (full_name);
17097
17098 return current_file;
17099 }
17100
17101
17102 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17103 followed by a null byte. */
17104 static char *
17105 copy_string (const char *buf, int len)
17106 {
17107 char *s = xmalloc (len + 1);
17108
17109 memcpy (s, buf, len);
17110 s[len] = '\0';
17111 return s;
17112 }
17113
17114
17115 static const char *
17116 consume_improper_spaces (const char *p, const char *body)
17117 {
17118 if (*p == ' ')
17119 {
17120 complaint (&symfile_complaints,
17121 _("macro definition contains spaces "
17122 "in formal argument list:\n`%s'"),
17123 body);
17124
17125 while (*p == ' ')
17126 p++;
17127 }
17128
17129 return p;
17130 }
17131
17132
17133 static void
17134 parse_macro_definition (struct macro_source_file *file, int line,
17135 const char *body)
17136 {
17137 const char *p;
17138
17139 /* The body string takes one of two forms. For object-like macro
17140 definitions, it should be:
17141
17142 <macro name> " " <definition>
17143
17144 For function-like macro definitions, it should be:
17145
17146 <macro name> "() " <definition>
17147 or
17148 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17149
17150 Spaces may appear only where explicitly indicated, and in the
17151 <definition>.
17152
17153 The Dwarf 2 spec says that an object-like macro's name is always
17154 followed by a space, but versions of GCC around March 2002 omit
17155 the space when the macro's definition is the empty string.
17156
17157 The Dwarf 2 spec says that there should be no spaces between the
17158 formal arguments in a function-like macro's formal argument list,
17159 but versions of GCC around March 2002 include spaces after the
17160 commas. */
17161
17162
17163 /* Find the extent of the macro name. The macro name is terminated
17164 by either a space or null character (for an object-like macro) or
17165 an opening paren (for a function-like macro). */
17166 for (p = body; *p; p++)
17167 if (*p == ' ' || *p == '(')
17168 break;
17169
17170 if (*p == ' ' || *p == '\0')
17171 {
17172 /* It's an object-like macro. */
17173 int name_len = p - body;
17174 char *name = copy_string (body, name_len);
17175 const char *replacement;
17176
17177 if (*p == ' ')
17178 replacement = body + name_len + 1;
17179 else
17180 {
17181 dwarf2_macro_malformed_definition_complaint (body);
17182 replacement = body + name_len;
17183 }
17184
17185 macro_define_object (file, line, name, replacement);
17186
17187 xfree (name);
17188 }
17189 else if (*p == '(')
17190 {
17191 /* It's a function-like macro. */
17192 char *name = copy_string (body, p - body);
17193 int argc = 0;
17194 int argv_size = 1;
17195 char **argv = xmalloc (argv_size * sizeof (*argv));
17196
17197 p++;
17198
17199 p = consume_improper_spaces (p, body);
17200
17201 /* Parse the formal argument list. */
17202 while (*p && *p != ')')
17203 {
17204 /* Find the extent of the current argument name. */
17205 const char *arg_start = p;
17206
17207 while (*p && *p != ',' && *p != ')' && *p != ' ')
17208 p++;
17209
17210 if (! *p || p == arg_start)
17211 dwarf2_macro_malformed_definition_complaint (body);
17212 else
17213 {
17214 /* Make sure argv has room for the new argument. */
17215 if (argc >= argv_size)
17216 {
17217 argv_size *= 2;
17218 argv = xrealloc (argv, argv_size * sizeof (*argv));
17219 }
17220
17221 argv[argc++] = copy_string (arg_start, p - arg_start);
17222 }
17223
17224 p = consume_improper_spaces (p, body);
17225
17226 /* Consume the comma, if present. */
17227 if (*p == ',')
17228 {
17229 p++;
17230
17231 p = consume_improper_spaces (p, body);
17232 }
17233 }
17234
17235 if (*p == ')')
17236 {
17237 p++;
17238
17239 if (*p == ' ')
17240 /* Perfectly formed definition, no complaints. */
17241 macro_define_function (file, line, name,
17242 argc, (const char **) argv,
17243 p + 1);
17244 else if (*p == '\0')
17245 {
17246 /* Complain, but do define it. */
17247 dwarf2_macro_malformed_definition_complaint (body);
17248 macro_define_function (file, line, name,
17249 argc, (const char **) argv,
17250 p);
17251 }
17252 else
17253 /* Just complain. */
17254 dwarf2_macro_malformed_definition_complaint (body);
17255 }
17256 else
17257 /* Just complain. */
17258 dwarf2_macro_malformed_definition_complaint (body);
17259
17260 xfree (name);
17261 {
17262 int i;
17263
17264 for (i = 0; i < argc; i++)
17265 xfree (argv[i]);
17266 }
17267 xfree (argv);
17268 }
17269 else
17270 dwarf2_macro_malformed_definition_complaint (body);
17271 }
17272
17273 /* Skip some bytes from BYTES according to the form given in FORM.
17274 Returns the new pointer. */
17275
17276 static gdb_byte *
17277 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17278 enum dwarf_form form,
17279 unsigned int offset_size,
17280 struct dwarf2_section_info *section)
17281 {
17282 unsigned int bytes_read;
17283
17284 switch (form)
17285 {
17286 case DW_FORM_data1:
17287 case DW_FORM_flag:
17288 ++bytes;
17289 break;
17290
17291 case DW_FORM_data2:
17292 bytes += 2;
17293 break;
17294
17295 case DW_FORM_data4:
17296 bytes += 4;
17297 break;
17298
17299 case DW_FORM_data8:
17300 bytes += 8;
17301 break;
17302
17303 case DW_FORM_string:
17304 read_direct_string (abfd, bytes, &bytes_read);
17305 bytes += bytes_read;
17306 break;
17307
17308 case DW_FORM_sec_offset:
17309 case DW_FORM_strp:
17310 case DW_FORM_GNU_strp_alt:
17311 bytes += offset_size;
17312 break;
17313
17314 case DW_FORM_block:
17315 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17316 bytes += bytes_read;
17317 break;
17318
17319 case DW_FORM_block1:
17320 bytes += 1 + read_1_byte (abfd, bytes);
17321 break;
17322 case DW_FORM_block2:
17323 bytes += 2 + read_2_bytes (abfd, bytes);
17324 break;
17325 case DW_FORM_block4:
17326 bytes += 4 + read_4_bytes (abfd, bytes);
17327 break;
17328
17329 case DW_FORM_sdata:
17330 case DW_FORM_udata:
17331 case DW_FORM_GNU_addr_index:
17332 case DW_FORM_GNU_str_index:
17333 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17334 if (bytes == NULL)
17335 {
17336 dwarf2_section_buffer_overflow_complaint (section);
17337 return NULL;
17338 }
17339 break;
17340
17341 default:
17342 {
17343 complain:
17344 complaint (&symfile_complaints,
17345 _("invalid form 0x%x in `%s'"),
17346 form,
17347 section->asection->name);
17348 return NULL;
17349 }
17350 }
17351
17352 return bytes;
17353 }
17354
17355 /* A helper for dwarf_decode_macros that handles skipping an unknown
17356 opcode. Returns an updated pointer to the macro data buffer; or,
17357 on error, issues a complaint and returns NULL. */
17358
17359 static gdb_byte *
17360 skip_unknown_opcode (unsigned int opcode,
17361 gdb_byte **opcode_definitions,
17362 gdb_byte *mac_ptr, gdb_byte *mac_end,
17363 bfd *abfd,
17364 unsigned int offset_size,
17365 struct dwarf2_section_info *section)
17366 {
17367 unsigned int bytes_read, i;
17368 unsigned long arg;
17369 gdb_byte *defn;
17370
17371 if (opcode_definitions[opcode] == NULL)
17372 {
17373 complaint (&symfile_complaints,
17374 _("unrecognized DW_MACFINO opcode 0x%x"),
17375 opcode);
17376 return NULL;
17377 }
17378
17379 defn = opcode_definitions[opcode];
17380 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17381 defn += bytes_read;
17382
17383 for (i = 0; i < arg; ++i)
17384 {
17385 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17386 section);
17387 if (mac_ptr == NULL)
17388 {
17389 /* skip_form_bytes already issued the complaint. */
17390 return NULL;
17391 }
17392 }
17393
17394 return mac_ptr;
17395 }
17396
17397 /* A helper function which parses the header of a macro section.
17398 If the macro section is the extended (for now called "GNU") type,
17399 then this updates *OFFSET_SIZE. Returns a pointer to just after
17400 the header, or issues a complaint and returns NULL on error. */
17401
17402 static gdb_byte *
17403 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17404 bfd *abfd,
17405 gdb_byte *mac_ptr,
17406 unsigned int *offset_size,
17407 int section_is_gnu)
17408 {
17409 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17410
17411 if (section_is_gnu)
17412 {
17413 unsigned int version, flags;
17414
17415 version = read_2_bytes (abfd, mac_ptr);
17416 if (version != 4)
17417 {
17418 complaint (&symfile_complaints,
17419 _("unrecognized version `%d' in .debug_macro section"),
17420 version);
17421 return NULL;
17422 }
17423 mac_ptr += 2;
17424
17425 flags = read_1_byte (abfd, mac_ptr);
17426 ++mac_ptr;
17427 *offset_size = (flags & 1) ? 8 : 4;
17428
17429 if ((flags & 2) != 0)
17430 /* We don't need the line table offset. */
17431 mac_ptr += *offset_size;
17432
17433 /* Vendor opcode descriptions. */
17434 if ((flags & 4) != 0)
17435 {
17436 unsigned int i, count;
17437
17438 count = read_1_byte (abfd, mac_ptr);
17439 ++mac_ptr;
17440 for (i = 0; i < count; ++i)
17441 {
17442 unsigned int opcode, bytes_read;
17443 unsigned long arg;
17444
17445 opcode = read_1_byte (abfd, mac_ptr);
17446 ++mac_ptr;
17447 opcode_definitions[opcode] = mac_ptr;
17448 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17449 mac_ptr += bytes_read;
17450 mac_ptr += arg;
17451 }
17452 }
17453 }
17454
17455 return mac_ptr;
17456 }
17457
17458 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17459 including DW_MACRO_GNU_transparent_include. */
17460
17461 static void
17462 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17463 struct macro_source_file *current_file,
17464 struct line_header *lh, char *comp_dir,
17465 struct dwarf2_section_info *section,
17466 int section_is_gnu, int section_is_dwz,
17467 unsigned int offset_size,
17468 struct objfile *objfile,
17469 htab_t include_hash)
17470 {
17471 enum dwarf_macro_record_type macinfo_type;
17472 int at_commandline;
17473 gdb_byte *opcode_definitions[256];
17474
17475 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17476 &offset_size, section_is_gnu);
17477 if (mac_ptr == NULL)
17478 {
17479 /* We already issued a complaint. */
17480 return;
17481 }
17482
17483 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17484 GDB is still reading the definitions from command line. First
17485 DW_MACINFO_start_file will need to be ignored as it was already executed
17486 to create CURRENT_FILE for the main source holding also the command line
17487 definitions. On first met DW_MACINFO_start_file this flag is reset to
17488 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17489
17490 at_commandline = 1;
17491
17492 do
17493 {
17494 /* Do we at least have room for a macinfo type byte? */
17495 if (mac_ptr >= mac_end)
17496 {
17497 dwarf2_section_buffer_overflow_complaint (section);
17498 break;
17499 }
17500
17501 macinfo_type = read_1_byte (abfd, mac_ptr);
17502 mac_ptr++;
17503
17504 /* Note that we rely on the fact that the corresponding GNU and
17505 DWARF constants are the same. */
17506 switch (macinfo_type)
17507 {
17508 /* A zero macinfo type indicates the end of the macro
17509 information. */
17510 case 0:
17511 break;
17512
17513 case DW_MACRO_GNU_define:
17514 case DW_MACRO_GNU_undef:
17515 case DW_MACRO_GNU_define_indirect:
17516 case DW_MACRO_GNU_undef_indirect:
17517 case DW_MACRO_GNU_define_indirect_alt:
17518 case DW_MACRO_GNU_undef_indirect_alt:
17519 {
17520 unsigned int bytes_read;
17521 int line;
17522 char *body;
17523 int is_define;
17524
17525 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17526 mac_ptr += bytes_read;
17527
17528 if (macinfo_type == DW_MACRO_GNU_define
17529 || macinfo_type == DW_MACRO_GNU_undef)
17530 {
17531 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17532 mac_ptr += bytes_read;
17533 }
17534 else
17535 {
17536 LONGEST str_offset;
17537
17538 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17539 mac_ptr += offset_size;
17540
17541 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
17542 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17543 || section_is_dwz)
17544 {
17545 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17546
17547 body = read_indirect_string_from_dwz (dwz, str_offset);
17548 }
17549 else
17550 body = read_indirect_string_at_offset (abfd, str_offset);
17551 }
17552
17553 is_define = (macinfo_type == DW_MACRO_GNU_define
17554 || macinfo_type == DW_MACRO_GNU_define_indirect
17555 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
17556 if (! current_file)
17557 {
17558 /* DWARF violation as no main source is present. */
17559 complaint (&symfile_complaints,
17560 _("debug info with no main source gives macro %s "
17561 "on line %d: %s"),
17562 is_define ? _("definition") : _("undefinition"),
17563 line, body);
17564 break;
17565 }
17566 if ((line == 0 && !at_commandline)
17567 || (line != 0 && at_commandline))
17568 complaint (&symfile_complaints,
17569 _("debug info gives %s macro %s with %s line %d: %s"),
17570 at_commandline ? _("command-line") : _("in-file"),
17571 is_define ? _("definition") : _("undefinition"),
17572 line == 0 ? _("zero") : _("non-zero"), line, body);
17573
17574 if (is_define)
17575 parse_macro_definition (current_file, line, body);
17576 else
17577 {
17578 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17579 || macinfo_type == DW_MACRO_GNU_undef_indirect
17580 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
17581 macro_undef (current_file, line, body);
17582 }
17583 }
17584 break;
17585
17586 case DW_MACRO_GNU_start_file:
17587 {
17588 unsigned int bytes_read;
17589 int line, file;
17590
17591 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17592 mac_ptr += bytes_read;
17593 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17594 mac_ptr += bytes_read;
17595
17596 if ((line == 0 && !at_commandline)
17597 || (line != 0 && at_commandline))
17598 complaint (&symfile_complaints,
17599 _("debug info gives source %d included "
17600 "from %s at %s line %d"),
17601 file, at_commandline ? _("command-line") : _("file"),
17602 line == 0 ? _("zero") : _("non-zero"), line);
17603
17604 if (at_commandline)
17605 {
17606 /* This DW_MACRO_GNU_start_file was executed in the
17607 pass one. */
17608 at_commandline = 0;
17609 }
17610 else
17611 current_file = macro_start_file (file, line,
17612 current_file, comp_dir,
17613 lh, objfile);
17614 }
17615 break;
17616
17617 case DW_MACRO_GNU_end_file:
17618 if (! current_file)
17619 complaint (&symfile_complaints,
17620 _("macro debug info has an unmatched "
17621 "`close_file' directive"));
17622 else
17623 {
17624 current_file = current_file->included_by;
17625 if (! current_file)
17626 {
17627 enum dwarf_macro_record_type next_type;
17628
17629 /* GCC circa March 2002 doesn't produce the zero
17630 type byte marking the end of the compilation
17631 unit. Complain if it's not there, but exit no
17632 matter what. */
17633
17634 /* Do we at least have room for a macinfo type byte? */
17635 if (mac_ptr >= mac_end)
17636 {
17637 dwarf2_section_buffer_overflow_complaint (section);
17638 return;
17639 }
17640
17641 /* We don't increment mac_ptr here, so this is just
17642 a look-ahead. */
17643 next_type = read_1_byte (abfd, mac_ptr);
17644 if (next_type != 0)
17645 complaint (&symfile_complaints,
17646 _("no terminating 0-type entry for "
17647 "macros in `.debug_macinfo' section"));
17648
17649 return;
17650 }
17651 }
17652 break;
17653
17654 case DW_MACRO_GNU_transparent_include:
17655 case DW_MACRO_GNU_transparent_include_alt:
17656 {
17657 LONGEST offset;
17658 void **slot;
17659
17660 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17661 mac_ptr += offset_size;
17662
17663 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17664 if (*slot != NULL)
17665 {
17666 /* This has actually happened; see
17667 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17668 complaint (&symfile_complaints,
17669 _("recursive DW_MACRO_GNU_transparent_include in "
17670 ".debug_macro section"));
17671 }
17672 else
17673 {
17674 bfd *include_bfd = abfd;
17675 struct dwarf2_section_info *include_section = section;
17676 struct dwarf2_section_info alt_section;
17677 gdb_byte *include_mac_end = mac_end;
17678 int is_dwz = section_is_dwz;
17679
17680 *slot = mac_ptr;
17681
17682 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17683 {
17684 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17685
17686 dwarf2_read_section (dwarf2_per_objfile->objfile,
17687 &dwz->macro);
17688
17689 include_bfd = dwz->macro.asection->owner;
17690 include_section = &dwz->macro;
17691 include_mac_end = dwz->macro.buffer + dwz->macro.size;
17692 is_dwz = 1;
17693 }
17694
17695 dwarf_decode_macro_bytes (include_bfd,
17696 include_section->buffer + offset,
17697 include_mac_end, current_file,
17698 lh, comp_dir,
17699 section, section_is_gnu, is_dwz,
17700 offset_size, objfile, include_hash);
17701
17702 htab_remove_elt (include_hash, mac_ptr);
17703 }
17704 }
17705 break;
17706
17707 case DW_MACINFO_vendor_ext:
17708 if (!section_is_gnu)
17709 {
17710 unsigned int bytes_read;
17711 int constant;
17712
17713 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17714 mac_ptr += bytes_read;
17715 read_direct_string (abfd, mac_ptr, &bytes_read);
17716 mac_ptr += bytes_read;
17717
17718 /* We don't recognize any vendor extensions. */
17719 break;
17720 }
17721 /* FALLTHROUGH */
17722
17723 default:
17724 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17725 mac_ptr, mac_end, abfd, offset_size,
17726 section);
17727 if (mac_ptr == NULL)
17728 return;
17729 break;
17730 }
17731 } while (macinfo_type != 0);
17732 }
17733
17734 static void
17735 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17736 char *comp_dir, int section_is_gnu)
17737 {
17738 struct objfile *objfile = dwarf2_per_objfile->objfile;
17739 struct line_header *lh = cu->line_header;
17740 bfd *abfd;
17741 gdb_byte *mac_ptr, *mac_end;
17742 struct macro_source_file *current_file = 0;
17743 enum dwarf_macro_record_type macinfo_type;
17744 unsigned int offset_size = cu->header.offset_size;
17745 gdb_byte *opcode_definitions[256];
17746 struct cleanup *cleanup;
17747 htab_t include_hash;
17748 void **slot;
17749 struct dwarf2_section_info *section;
17750 const char *section_name;
17751
17752 if (cu->dwo_unit != NULL)
17753 {
17754 if (section_is_gnu)
17755 {
17756 section = &cu->dwo_unit->dwo_file->sections.macro;
17757 section_name = ".debug_macro.dwo";
17758 }
17759 else
17760 {
17761 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17762 section_name = ".debug_macinfo.dwo";
17763 }
17764 }
17765 else
17766 {
17767 if (section_is_gnu)
17768 {
17769 section = &dwarf2_per_objfile->macro;
17770 section_name = ".debug_macro";
17771 }
17772 else
17773 {
17774 section = &dwarf2_per_objfile->macinfo;
17775 section_name = ".debug_macinfo";
17776 }
17777 }
17778
17779 dwarf2_read_section (objfile, section);
17780 if (section->buffer == NULL)
17781 {
17782 complaint (&symfile_complaints, _("missing %s section"), section_name);
17783 return;
17784 }
17785 abfd = section->asection->owner;
17786
17787 /* First pass: Find the name of the base filename.
17788 This filename is needed in order to process all macros whose definition
17789 (or undefinition) comes from the command line. These macros are defined
17790 before the first DW_MACINFO_start_file entry, and yet still need to be
17791 associated to the base file.
17792
17793 To determine the base file name, we scan the macro definitions until we
17794 reach the first DW_MACINFO_start_file entry. We then initialize
17795 CURRENT_FILE accordingly so that any macro definition found before the
17796 first DW_MACINFO_start_file can still be associated to the base file. */
17797
17798 mac_ptr = section->buffer + offset;
17799 mac_end = section->buffer + section->size;
17800
17801 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17802 &offset_size, section_is_gnu);
17803 if (mac_ptr == NULL)
17804 {
17805 /* We already issued a complaint. */
17806 return;
17807 }
17808
17809 do
17810 {
17811 /* Do we at least have room for a macinfo type byte? */
17812 if (mac_ptr >= mac_end)
17813 {
17814 /* Complaint is printed during the second pass as GDB will probably
17815 stop the first pass earlier upon finding
17816 DW_MACINFO_start_file. */
17817 break;
17818 }
17819
17820 macinfo_type = read_1_byte (abfd, mac_ptr);
17821 mac_ptr++;
17822
17823 /* Note that we rely on the fact that the corresponding GNU and
17824 DWARF constants are the same. */
17825 switch (macinfo_type)
17826 {
17827 /* A zero macinfo type indicates the end of the macro
17828 information. */
17829 case 0:
17830 break;
17831
17832 case DW_MACRO_GNU_define:
17833 case DW_MACRO_GNU_undef:
17834 /* Only skip the data by MAC_PTR. */
17835 {
17836 unsigned int bytes_read;
17837
17838 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17839 mac_ptr += bytes_read;
17840 read_direct_string (abfd, mac_ptr, &bytes_read);
17841 mac_ptr += bytes_read;
17842 }
17843 break;
17844
17845 case DW_MACRO_GNU_start_file:
17846 {
17847 unsigned int bytes_read;
17848 int line, file;
17849
17850 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17851 mac_ptr += bytes_read;
17852 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17853 mac_ptr += bytes_read;
17854
17855 current_file = macro_start_file (file, line, current_file,
17856 comp_dir, lh, objfile);
17857 }
17858 break;
17859
17860 case DW_MACRO_GNU_end_file:
17861 /* No data to skip by MAC_PTR. */
17862 break;
17863
17864 case DW_MACRO_GNU_define_indirect:
17865 case DW_MACRO_GNU_undef_indirect:
17866 case DW_MACRO_GNU_define_indirect_alt:
17867 case DW_MACRO_GNU_undef_indirect_alt:
17868 {
17869 unsigned int bytes_read;
17870
17871 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17872 mac_ptr += bytes_read;
17873 mac_ptr += offset_size;
17874 }
17875 break;
17876
17877 case DW_MACRO_GNU_transparent_include:
17878 case DW_MACRO_GNU_transparent_include_alt:
17879 /* Note that, according to the spec, a transparent include
17880 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17881 skip this opcode. */
17882 mac_ptr += offset_size;
17883 break;
17884
17885 case DW_MACINFO_vendor_ext:
17886 /* Only skip the data by MAC_PTR. */
17887 if (!section_is_gnu)
17888 {
17889 unsigned int bytes_read;
17890
17891 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17892 mac_ptr += bytes_read;
17893 read_direct_string (abfd, mac_ptr, &bytes_read);
17894 mac_ptr += bytes_read;
17895 }
17896 /* FALLTHROUGH */
17897
17898 default:
17899 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17900 mac_ptr, mac_end, abfd, offset_size,
17901 section);
17902 if (mac_ptr == NULL)
17903 return;
17904 break;
17905 }
17906 } while (macinfo_type != 0 && current_file == NULL);
17907
17908 /* Second pass: Process all entries.
17909
17910 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17911 command-line macro definitions/undefinitions. This flag is unset when we
17912 reach the first DW_MACINFO_start_file entry. */
17913
17914 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17915 NULL, xcalloc, xfree);
17916 cleanup = make_cleanup_htab_delete (include_hash);
17917 mac_ptr = section->buffer + offset;
17918 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17919 *slot = mac_ptr;
17920 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17921 current_file, lh, comp_dir, section,
17922 section_is_gnu, 0,
17923 offset_size, objfile, include_hash);
17924 do_cleanups (cleanup);
17925 }
17926
17927 /* Check if the attribute's form is a DW_FORM_block*
17928 if so return true else false. */
17929
17930 static int
17931 attr_form_is_block (struct attribute *attr)
17932 {
17933 return (attr == NULL ? 0 :
17934 attr->form == DW_FORM_block1
17935 || attr->form == DW_FORM_block2
17936 || attr->form == DW_FORM_block4
17937 || attr->form == DW_FORM_block
17938 || attr->form == DW_FORM_exprloc);
17939 }
17940
17941 /* Return non-zero if ATTR's value is a section offset --- classes
17942 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17943 You may use DW_UNSND (attr) to retrieve such offsets.
17944
17945 Section 7.5.4, "Attribute Encodings", explains that no attribute
17946 may have a value that belongs to more than one of these classes; it
17947 would be ambiguous if we did, because we use the same forms for all
17948 of them. */
17949
17950 static int
17951 attr_form_is_section_offset (struct attribute *attr)
17952 {
17953 return (attr->form == DW_FORM_data4
17954 || attr->form == DW_FORM_data8
17955 || attr->form == DW_FORM_sec_offset);
17956 }
17957
17958 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17959 zero otherwise. When this function returns true, you can apply
17960 dwarf2_get_attr_constant_value to it.
17961
17962 However, note that for some attributes you must check
17963 attr_form_is_section_offset before using this test. DW_FORM_data4
17964 and DW_FORM_data8 are members of both the constant class, and of
17965 the classes that contain offsets into other debug sections
17966 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17967 that, if an attribute's can be either a constant or one of the
17968 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17969 taken as section offsets, not constants. */
17970
17971 static int
17972 attr_form_is_constant (struct attribute *attr)
17973 {
17974 switch (attr->form)
17975 {
17976 case DW_FORM_sdata:
17977 case DW_FORM_udata:
17978 case DW_FORM_data1:
17979 case DW_FORM_data2:
17980 case DW_FORM_data4:
17981 case DW_FORM_data8:
17982 return 1;
17983 default:
17984 return 0;
17985 }
17986 }
17987
17988 /* Return the .debug_loc section to use for CU.
17989 For DWO files use .debug_loc.dwo. */
17990
17991 static struct dwarf2_section_info *
17992 cu_debug_loc_section (struct dwarf2_cu *cu)
17993 {
17994 if (cu->dwo_unit)
17995 return &cu->dwo_unit->dwo_file->sections.loc;
17996 return &dwarf2_per_objfile->loc;
17997 }
17998
17999 /* A helper function that fills in a dwarf2_loclist_baton. */
18000
18001 static void
18002 fill_in_loclist_baton (struct dwarf2_cu *cu,
18003 struct dwarf2_loclist_baton *baton,
18004 struct attribute *attr)
18005 {
18006 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18007
18008 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18009
18010 baton->per_cu = cu->per_cu;
18011 gdb_assert (baton->per_cu);
18012 /* We don't know how long the location list is, but make sure we
18013 don't run off the edge of the section. */
18014 baton->size = section->size - DW_UNSND (attr);
18015 baton->data = section->buffer + DW_UNSND (attr);
18016 baton->base_address = cu->base_address;
18017 baton->from_dwo = cu->dwo_unit != NULL;
18018 }
18019
18020 static void
18021 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18022 struct dwarf2_cu *cu)
18023 {
18024 struct objfile *objfile = dwarf2_per_objfile->objfile;
18025 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18026
18027 if (attr_form_is_section_offset (attr)
18028 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18029 the section. If so, fall through to the complaint in the
18030 other branch. */
18031 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18032 {
18033 struct dwarf2_loclist_baton *baton;
18034
18035 baton = obstack_alloc (&objfile->objfile_obstack,
18036 sizeof (struct dwarf2_loclist_baton));
18037
18038 fill_in_loclist_baton (cu, baton, attr);
18039
18040 if (cu->base_known == 0)
18041 complaint (&symfile_complaints,
18042 _("Location list used without "
18043 "specifying the CU base address."));
18044
18045 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18046 SYMBOL_LOCATION_BATON (sym) = baton;
18047 }
18048 else
18049 {
18050 struct dwarf2_locexpr_baton *baton;
18051
18052 baton = obstack_alloc (&objfile->objfile_obstack,
18053 sizeof (struct dwarf2_locexpr_baton));
18054 baton->per_cu = cu->per_cu;
18055 gdb_assert (baton->per_cu);
18056
18057 if (attr_form_is_block (attr))
18058 {
18059 /* Note that we're just copying the block's data pointer
18060 here, not the actual data. We're still pointing into the
18061 info_buffer for SYM's objfile; right now we never release
18062 that buffer, but when we do clean up properly this may
18063 need to change. */
18064 baton->size = DW_BLOCK (attr)->size;
18065 baton->data = DW_BLOCK (attr)->data;
18066 }
18067 else
18068 {
18069 dwarf2_invalid_attrib_class_complaint ("location description",
18070 SYMBOL_NATURAL_NAME (sym));
18071 baton->size = 0;
18072 }
18073
18074 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18075 SYMBOL_LOCATION_BATON (sym) = baton;
18076 }
18077 }
18078
18079 /* Return the OBJFILE associated with the compilation unit CU. If CU
18080 came from a separate debuginfo file, then the master objfile is
18081 returned. */
18082
18083 struct objfile *
18084 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18085 {
18086 struct objfile *objfile = per_cu->objfile;
18087
18088 /* Return the master objfile, so that we can report and look up the
18089 correct file containing this variable. */
18090 if (objfile->separate_debug_objfile_backlink)
18091 objfile = objfile->separate_debug_objfile_backlink;
18092
18093 return objfile;
18094 }
18095
18096 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18097 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18098 CU_HEADERP first. */
18099
18100 static const struct comp_unit_head *
18101 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18102 struct dwarf2_per_cu_data *per_cu)
18103 {
18104 gdb_byte *info_ptr;
18105
18106 if (per_cu->cu)
18107 return &per_cu->cu->header;
18108
18109 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18110
18111 memset (cu_headerp, 0, sizeof (*cu_headerp));
18112 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18113
18114 return cu_headerp;
18115 }
18116
18117 /* Return the address size given in the compilation unit header for CU. */
18118
18119 int
18120 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18121 {
18122 struct comp_unit_head cu_header_local;
18123 const struct comp_unit_head *cu_headerp;
18124
18125 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18126
18127 return cu_headerp->addr_size;
18128 }
18129
18130 /* Return the offset size given in the compilation unit header for CU. */
18131
18132 int
18133 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18134 {
18135 struct comp_unit_head cu_header_local;
18136 const struct comp_unit_head *cu_headerp;
18137
18138 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18139
18140 return cu_headerp->offset_size;
18141 }
18142
18143 /* See its dwarf2loc.h declaration. */
18144
18145 int
18146 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18147 {
18148 struct comp_unit_head cu_header_local;
18149 const struct comp_unit_head *cu_headerp;
18150
18151 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18152
18153 if (cu_headerp->version == 2)
18154 return cu_headerp->addr_size;
18155 else
18156 return cu_headerp->offset_size;
18157 }
18158
18159 /* Return the text offset of the CU. The returned offset comes from
18160 this CU's objfile. If this objfile came from a separate debuginfo
18161 file, then the offset may be different from the corresponding
18162 offset in the parent objfile. */
18163
18164 CORE_ADDR
18165 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
18166 {
18167 struct objfile *objfile = per_cu->objfile;
18168
18169 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18170 }
18171
18172 /* Locate the .debug_info compilation unit from CU's objfile which contains
18173 the DIE at OFFSET. Raises an error on failure. */
18174
18175 static struct dwarf2_per_cu_data *
18176 dwarf2_find_containing_comp_unit (sect_offset offset,
18177 unsigned int offset_in_dwz,
18178 struct objfile *objfile)
18179 {
18180 struct dwarf2_per_cu_data *this_cu;
18181 int low, high;
18182 const sect_offset *cu_off;
18183
18184 low = 0;
18185 high = dwarf2_per_objfile->n_comp_units - 1;
18186 while (high > low)
18187 {
18188 struct dwarf2_per_cu_data *mid_cu;
18189 int mid = low + (high - low) / 2;
18190
18191 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
18192 cu_off = &mid_cu->offset;
18193 if (mid_cu->is_dwz > offset_in_dwz
18194 || (mid_cu->is_dwz == offset_in_dwz
18195 && cu_off->sect_off >= offset.sect_off))
18196 high = mid;
18197 else
18198 low = mid + 1;
18199 }
18200 gdb_assert (low == high);
18201 this_cu = dwarf2_per_objfile->all_comp_units[low];
18202 cu_off = &this_cu->offset;
18203 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
18204 {
18205 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
18206 error (_("Dwarf Error: could not find partial DIE containing "
18207 "offset 0x%lx [in module %s]"),
18208 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
18209
18210 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
18211 <= offset.sect_off);
18212 return dwarf2_per_objfile->all_comp_units[low-1];
18213 }
18214 else
18215 {
18216 this_cu = dwarf2_per_objfile->all_comp_units[low];
18217 if (low == dwarf2_per_objfile->n_comp_units - 1
18218 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
18219 error (_("invalid dwarf2 offset %u"), offset.sect_off);
18220 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
18221 return this_cu;
18222 }
18223 }
18224
18225 /* Initialize dwarf2_cu CU, owned by PER_CU. */
18226
18227 static void
18228 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
18229 {
18230 memset (cu, 0, sizeof (*cu));
18231 per_cu->cu = cu;
18232 cu->per_cu = per_cu;
18233 cu->objfile = per_cu->objfile;
18234 obstack_init (&cu->comp_unit_obstack);
18235 }
18236
18237 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
18238
18239 static void
18240 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
18241 enum language pretend_language)
18242 {
18243 struct attribute *attr;
18244
18245 /* Set the language we're debugging. */
18246 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
18247 if (attr)
18248 set_cu_language (DW_UNSND (attr), cu);
18249 else
18250 {
18251 cu->language = pretend_language;
18252 cu->language_defn = language_def (cu->language);
18253 }
18254
18255 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
18256 if (attr)
18257 cu->producer = DW_STRING (attr);
18258 }
18259
18260 /* Release one cached compilation unit, CU. We unlink it from the tree
18261 of compilation units, but we don't remove it from the read_in_chain;
18262 the caller is responsible for that.
18263 NOTE: DATA is a void * because this function is also used as a
18264 cleanup routine. */
18265
18266 static void
18267 free_heap_comp_unit (void *data)
18268 {
18269 struct dwarf2_cu *cu = data;
18270
18271 gdb_assert (cu->per_cu != NULL);
18272 cu->per_cu->cu = NULL;
18273 cu->per_cu = NULL;
18274
18275 obstack_free (&cu->comp_unit_obstack, NULL);
18276
18277 xfree (cu);
18278 }
18279
18280 /* This cleanup function is passed the address of a dwarf2_cu on the stack
18281 when we're finished with it. We can't free the pointer itself, but be
18282 sure to unlink it from the cache. Also release any associated storage. */
18283
18284 static void
18285 free_stack_comp_unit (void *data)
18286 {
18287 struct dwarf2_cu *cu = data;
18288
18289 gdb_assert (cu->per_cu != NULL);
18290 cu->per_cu->cu = NULL;
18291 cu->per_cu = NULL;
18292
18293 obstack_free (&cu->comp_unit_obstack, NULL);
18294 cu->partial_dies = NULL;
18295 }
18296
18297 /* Free all cached compilation units. */
18298
18299 static void
18300 free_cached_comp_units (void *data)
18301 {
18302 struct dwarf2_per_cu_data *per_cu, **last_chain;
18303
18304 per_cu = dwarf2_per_objfile->read_in_chain;
18305 last_chain = &dwarf2_per_objfile->read_in_chain;
18306 while (per_cu != NULL)
18307 {
18308 struct dwarf2_per_cu_data *next_cu;
18309
18310 next_cu = per_cu->cu->read_in_chain;
18311
18312 free_heap_comp_unit (per_cu->cu);
18313 *last_chain = next_cu;
18314
18315 per_cu = next_cu;
18316 }
18317 }
18318
18319 /* Increase the age counter on each cached compilation unit, and free
18320 any that are too old. */
18321
18322 static void
18323 age_cached_comp_units (void)
18324 {
18325 struct dwarf2_per_cu_data *per_cu, **last_chain;
18326
18327 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18328 per_cu = dwarf2_per_objfile->read_in_chain;
18329 while (per_cu != NULL)
18330 {
18331 per_cu->cu->last_used ++;
18332 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18333 dwarf2_mark (per_cu->cu);
18334 per_cu = per_cu->cu->read_in_chain;
18335 }
18336
18337 per_cu = dwarf2_per_objfile->read_in_chain;
18338 last_chain = &dwarf2_per_objfile->read_in_chain;
18339 while (per_cu != NULL)
18340 {
18341 struct dwarf2_per_cu_data *next_cu;
18342
18343 next_cu = per_cu->cu->read_in_chain;
18344
18345 if (!per_cu->cu->mark)
18346 {
18347 free_heap_comp_unit (per_cu->cu);
18348 *last_chain = next_cu;
18349 }
18350 else
18351 last_chain = &per_cu->cu->read_in_chain;
18352
18353 per_cu = next_cu;
18354 }
18355 }
18356
18357 /* Remove a single compilation unit from the cache. */
18358
18359 static void
18360 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
18361 {
18362 struct dwarf2_per_cu_data *per_cu, **last_chain;
18363
18364 per_cu = dwarf2_per_objfile->read_in_chain;
18365 last_chain = &dwarf2_per_objfile->read_in_chain;
18366 while (per_cu != NULL)
18367 {
18368 struct dwarf2_per_cu_data *next_cu;
18369
18370 next_cu = per_cu->cu->read_in_chain;
18371
18372 if (per_cu == target_per_cu)
18373 {
18374 free_heap_comp_unit (per_cu->cu);
18375 per_cu->cu = NULL;
18376 *last_chain = next_cu;
18377 break;
18378 }
18379 else
18380 last_chain = &per_cu->cu->read_in_chain;
18381
18382 per_cu = next_cu;
18383 }
18384 }
18385
18386 /* Release all extra memory associated with OBJFILE. */
18387
18388 void
18389 dwarf2_free_objfile (struct objfile *objfile)
18390 {
18391 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18392
18393 if (dwarf2_per_objfile == NULL)
18394 return;
18395
18396 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18397 free_cached_comp_units (NULL);
18398
18399 if (dwarf2_per_objfile->quick_file_names_table)
18400 htab_delete (dwarf2_per_objfile->quick_file_names_table);
18401
18402 /* Everything else should be on the objfile obstack. */
18403 }
18404
18405 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18406 We store these in a hash table separate from the DIEs, and preserve them
18407 when the DIEs are flushed out of cache.
18408
18409 The CU "per_cu" pointer is needed because offset alone is not enough to
18410 uniquely identify the type. A file may have multiple .debug_types sections,
18411 or the type may come from a DWO file. We have to use something in
18412 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18413 routine, get_die_type_at_offset, from outside this file, and thus won't
18414 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18415 of the objfile. */
18416
18417 struct dwarf2_per_cu_offset_and_type
18418 {
18419 const struct dwarf2_per_cu_data *per_cu;
18420 sect_offset offset;
18421 struct type *type;
18422 };
18423
18424 /* Hash function for a dwarf2_per_cu_offset_and_type. */
18425
18426 static hashval_t
18427 per_cu_offset_and_type_hash (const void *item)
18428 {
18429 const struct dwarf2_per_cu_offset_and_type *ofs = item;
18430
18431 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
18432 }
18433
18434 /* Equality function for a dwarf2_per_cu_offset_and_type. */
18435
18436 static int
18437 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
18438 {
18439 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18440 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
18441
18442 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18443 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18444 }
18445
18446 /* Set the type associated with DIE to TYPE. Save it in CU's hash
18447 table if necessary. For convenience, return TYPE.
18448
18449 The DIEs reading must have careful ordering to:
18450 * Not cause infite loops trying to read in DIEs as a prerequisite for
18451 reading current DIE.
18452 * Not trying to dereference contents of still incompletely read in types
18453 while reading in other DIEs.
18454 * Enable referencing still incompletely read in types just by a pointer to
18455 the type without accessing its fields.
18456
18457 Therefore caller should follow these rules:
18458 * Try to fetch any prerequisite types we may need to build this DIE type
18459 before building the type and calling set_die_type.
18460 * After building type call set_die_type for current DIE as soon as
18461 possible before fetching more types to complete the current type.
18462 * Make the type as complete as possible before fetching more types. */
18463
18464 static struct type *
18465 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18466 {
18467 struct dwarf2_per_cu_offset_and_type **slot, ofs;
18468 struct objfile *objfile = cu->objfile;
18469
18470 /* For Ada types, make sure that the gnat-specific data is always
18471 initialized (if not already set). There are a few types where
18472 we should not be doing so, because the type-specific area is
18473 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18474 where the type-specific area is used to store the floatformat).
18475 But this is not a problem, because the gnat-specific information
18476 is actually not needed for these types. */
18477 if (need_gnat_info (cu)
18478 && TYPE_CODE (type) != TYPE_CODE_FUNC
18479 && TYPE_CODE (type) != TYPE_CODE_FLT
18480 && !HAVE_GNAT_AUX_INFO (type))
18481 INIT_GNAT_SPECIFIC (type);
18482
18483 if (dwarf2_per_objfile->die_type_hash == NULL)
18484 {
18485 dwarf2_per_objfile->die_type_hash =
18486 htab_create_alloc_ex (127,
18487 per_cu_offset_and_type_hash,
18488 per_cu_offset_and_type_eq,
18489 NULL,
18490 &objfile->objfile_obstack,
18491 hashtab_obstack_allocate,
18492 dummy_obstack_deallocate);
18493 }
18494
18495 ofs.per_cu = cu->per_cu;
18496 ofs.offset = die->offset;
18497 ofs.type = type;
18498 slot = (struct dwarf2_per_cu_offset_and_type **)
18499 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18500 if (*slot)
18501 complaint (&symfile_complaints,
18502 _("A problem internal to GDB: DIE 0x%x has type already set"),
18503 die->offset.sect_off);
18504 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18505 **slot = ofs;
18506 return type;
18507 }
18508
18509 /* Look up the type for the die at OFFSET in the appropriate type_hash
18510 table, or return NULL if the die does not have a saved type. */
18511
18512 static struct type *
18513 get_die_type_at_offset (sect_offset offset,
18514 struct dwarf2_per_cu_data *per_cu)
18515 {
18516 struct dwarf2_per_cu_offset_and_type *slot, ofs;
18517
18518 if (dwarf2_per_objfile->die_type_hash == NULL)
18519 return NULL;
18520
18521 ofs.per_cu = per_cu;
18522 ofs.offset = offset;
18523 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18524 if (slot)
18525 return slot->type;
18526 else
18527 return NULL;
18528 }
18529
18530 /* Look up the type for DIE in the appropriate type_hash table,
18531 or return NULL if DIE does not have a saved type. */
18532
18533 static struct type *
18534 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18535 {
18536 return get_die_type_at_offset (die->offset, cu->per_cu);
18537 }
18538
18539 /* Add a dependence relationship from CU to REF_PER_CU. */
18540
18541 static void
18542 dwarf2_add_dependence (struct dwarf2_cu *cu,
18543 struct dwarf2_per_cu_data *ref_per_cu)
18544 {
18545 void **slot;
18546
18547 if (cu->dependencies == NULL)
18548 cu->dependencies
18549 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18550 NULL, &cu->comp_unit_obstack,
18551 hashtab_obstack_allocate,
18552 dummy_obstack_deallocate);
18553
18554 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18555 if (*slot == NULL)
18556 *slot = ref_per_cu;
18557 }
18558
18559 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18560 Set the mark field in every compilation unit in the
18561 cache that we must keep because we are keeping CU. */
18562
18563 static int
18564 dwarf2_mark_helper (void **slot, void *data)
18565 {
18566 struct dwarf2_per_cu_data *per_cu;
18567
18568 per_cu = (struct dwarf2_per_cu_data *) *slot;
18569
18570 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18571 reading of the chain. As such dependencies remain valid it is not much
18572 useful to track and undo them during QUIT cleanups. */
18573 if (per_cu->cu == NULL)
18574 return 1;
18575
18576 if (per_cu->cu->mark)
18577 return 1;
18578 per_cu->cu->mark = 1;
18579
18580 if (per_cu->cu->dependencies != NULL)
18581 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18582
18583 return 1;
18584 }
18585
18586 /* Set the mark field in CU and in every other compilation unit in the
18587 cache that we must keep because we are keeping CU. */
18588
18589 static void
18590 dwarf2_mark (struct dwarf2_cu *cu)
18591 {
18592 if (cu->mark)
18593 return;
18594 cu->mark = 1;
18595 if (cu->dependencies != NULL)
18596 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18597 }
18598
18599 static void
18600 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18601 {
18602 while (per_cu)
18603 {
18604 per_cu->cu->mark = 0;
18605 per_cu = per_cu->cu->read_in_chain;
18606 }
18607 }
18608
18609 /* Trivial hash function for partial_die_info: the hash value of a DIE
18610 is its offset in .debug_info for this objfile. */
18611
18612 static hashval_t
18613 partial_die_hash (const void *item)
18614 {
18615 const struct partial_die_info *part_die = item;
18616
18617 return part_die->offset.sect_off;
18618 }
18619
18620 /* Trivial comparison function for partial_die_info structures: two DIEs
18621 are equal if they have the same offset. */
18622
18623 static int
18624 partial_die_eq (const void *item_lhs, const void *item_rhs)
18625 {
18626 const struct partial_die_info *part_die_lhs = item_lhs;
18627 const struct partial_die_info *part_die_rhs = item_rhs;
18628
18629 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18630 }
18631
18632 static struct cmd_list_element *set_dwarf2_cmdlist;
18633 static struct cmd_list_element *show_dwarf2_cmdlist;
18634
18635 static void
18636 set_dwarf2_cmd (char *args, int from_tty)
18637 {
18638 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18639 }
18640
18641 static void
18642 show_dwarf2_cmd (char *args, int from_tty)
18643 {
18644 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18645 }
18646
18647 /* Free data associated with OBJFILE, if necessary. */
18648
18649 static void
18650 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18651 {
18652 struct dwarf2_per_objfile *data = d;
18653 int ix;
18654
18655 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18656 VEC_free (dwarf2_per_cu_ptr,
18657 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18658
18659 VEC_free (dwarf2_section_info_def, data->types);
18660
18661 if (data->dwo_files)
18662 free_dwo_files (data->dwo_files, objfile);
18663
18664 if (data->dwz_file && data->dwz_file->dwz_bfd)
18665 gdb_bfd_unref (data->dwz_file->dwz_bfd);
18666 }
18667
18668 \f
18669 /* The "save gdb-index" command. */
18670
18671 /* The contents of the hash table we create when building the string
18672 table. */
18673 struct strtab_entry
18674 {
18675 offset_type offset;
18676 const char *str;
18677 };
18678
18679 /* Hash function for a strtab_entry.
18680
18681 Function is used only during write_hash_table so no index format backward
18682 compatibility is needed. */
18683
18684 static hashval_t
18685 hash_strtab_entry (const void *e)
18686 {
18687 const struct strtab_entry *entry = e;
18688 return mapped_index_string_hash (INT_MAX, entry->str);
18689 }
18690
18691 /* Equality function for a strtab_entry. */
18692
18693 static int
18694 eq_strtab_entry (const void *a, const void *b)
18695 {
18696 const struct strtab_entry *ea = a;
18697 const struct strtab_entry *eb = b;
18698 return !strcmp (ea->str, eb->str);
18699 }
18700
18701 /* Create a strtab_entry hash table. */
18702
18703 static htab_t
18704 create_strtab (void)
18705 {
18706 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18707 xfree, xcalloc, xfree);
18708 }
18709
18710 /* Add a string to the constant pool. Return the string's offset in
18711 host order. */
18712
18713 static offset_type
18714 add_string (htab_t table, struct obstack *cpool, const char *str)
18715 {
18716 void **slot;
18717 struct strtab_entry entry;
18718 struct strtab_entry *result;
18719
18720 entry.str = str;
18721 slot = htab_find_slot (table, &entry, INSERT);
18722 if (*slot)
18723 result = *slot;
18724 else
18725 {
18726 result = XNEW (struct strtab_entry);
18727 result->offset = obstack_object_size (cpool);
18728 result->str = str;
18729 obstack_grow_str0 (cpool, str);
18730 *slot = result;
18731 }
18732 return result->offset;
18733 }
18734
18735 /* An entry in the symbol table. */
18736 struct symtab_index_entry
18737 {
18738 /* The name of the symbol. */
18739 const char *name;
18740 /* The offset of the name in the constant pool. */
18741 offset_type index_offset;
18742 /* A sorted vector of the indices of all the CUs that hold an object
18743 of this name. */
18744 VEC (offset_type) *cu_indices;
18745 };
18746
18747 /* The symbol table. This is a power-of-2-sized hash table. */
18748 struct mapped_symtab
18749 {
18750 offset_type n_elements;
18751 offset_type size;
18752 struct symtab_index_entry **data;
18753 };
18754
18755 /* Hash function for a symtab_index_entry. */
18756
18757 static hashval_t
18758 hash_symtab_entry (const void *e)
18759 {
18760 const struct symtab_index_entry *entry = e;
18761 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18762 sizeof (offset_type) * VEC_length (offset_type,
18763 entry->cu_indices),
18764 0);
18765 }
18766
18767 /* Equality function for a symtab_index_entry. */
18768
18769 static int
18770 eq_symtab_entry (const void *a, const void *b)
18771 {
18772 const struct symtab_index_entry *ea = a;
18773 const struct symtab_index_entry *eb = b;
18774 int len = VEC_length (offset_type, ea->cu_indices);
18775 if (len != VEC_length (offset_type, eb->cu_indices))
18776 return 0;
18777 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18778 VEC_address (offset_type, eb->cu_indices),
18779 sizeof (offset_type) * len);
18780 }
18781
18782 /* Destroy a symtab_index_entry. */
18783
18784 static void
18785 delete_symtab_entry (void *p)
18786 {
18787 struct symtab_index_entry *entry = p;
18788 VEC_free (offset_type, entry->cu_indices);
18789 xfree (entry);
18790 }
18791
18792 /* Create a hash table holding symtab_index_entry objects. */
18793
18794 static htab_t
18795 create_symbol_hash_table (void)
18796 {
18797 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18798 delete_symtab_entry, xcalloc, xfree);
18799 }
18800
18801 /* Create a new mapped symtab object. */
18802
18803 static struct mapped_symtab *
18804 create_mapped_symtab (void)
18805 {
18806 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18807 symtab->n_elements = 0;
18808 symtab->size = 1024;
18809 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18810 return symtab;
18811 }
18812
18813 /* Destroy a mapped_symtab. */
18814
18815 static void
18816 cleanup_mapped_symtab (void *p)
18817 {
18818 struct mapped_symtab *symtab = p;
18819 /* The contents of the array are freed when the other hash table is
18820 destroyed. */
18821 xfree (symtab->data);
18822 xfree (symtab);
18823 }
18824
18825 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
18826 the slot.
18827
18828 Function is used only during write_hash_table so no index format backward
18829 compatibility is needed. */
18830
18831 static struct symtab_index_entry **
18832 find_slot (struct mapped_symtab *symtab, const char *name)
18833 {
18834 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18835
18836 index = hash & (symtab->size - 1);
18837 step = ((hash * 17) & (symtab->size - 1)) | 1;
18838
18839 for (;;)
18840 {
18841 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18842 return &symtab->data[index];
18843 index = (index + step) & (symtab->size - 1);
18844 }
18845 }
18846
18847 /* Expand SYMTAB's hash table. */
18848
18849 static void
18850 hash_expand (struct mapped_symtab *symtab)
18851 {
18852 offset_type old_size = symtab->size;
18853 offset_type i;
18854 struct symtab_index_entry **old_entries = symtab->data;
18855
18856 symtab->size *= 2;
18857 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18858
18859 for (i = 0; i < old_size; ++i)
18860 {
18861 if (old_entries[i])
18862 {
18863 struct symtab_index_entry **slot = find_slot (symtab,
18864 old_entries[i]->name);
18865 *slot = old_entries[i];
18866 }
18867 }
18868
18869 xfree (old_entries);
18870 }
18871
18872 /* Add an entry to SYMTAB. NAME is the name of the symbol.
18873 CU_INDEX is the index of the CU in which the symbol appears.
18874 IS_STATIC is one if the symbol is static, otherwise zero (global). */
18875
18876 static void
18877 add_index_entry (struct mapped_symtab *symtab, const char *name,
18878 int is_static, gdb_index_symbol_kind kind,
18879 offset_type cu_index)
18880 {
18881 struct symtab_index_entry **slot;
18882 offset_type cu_index_and_attrs;
18883
18884 ++symtab->n_elements;
18885 if (4 * symtab->n_elements / 3 >= symtab->size)
18886 hash_expand (symtab);
18887
18888 slot = find_slot (symtab, name);
18889 if (!*slot)
18890 {
18891 *slot = XNEW (struct symtab_index_entry);
18892 (*slot)->name = name;
18893 /* index_offset is set later. */
18894 (*slot)->cu_indices = NULL;
18895 }
18896
18897 cu_index_and_attrs = 0;
18898 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18899 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18900 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18901
18902 /* We don't want to record an index value twice as we want to avoid the
18903 duplication.
18904 We process all global symbols and then all static symbols
18905 (which would allow us to avoid the duplication by only having to check
18906 the last entry pushed), but a symbol could have multiple kinds in one CU.
18907 To keep things simple we don't worry about the duplication here and
18908 sort and uniqufy the list after we've processed all symbols. */
18909 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18910 }
18911
18912 /* qsort helper routine for uniquify_cu_indices. */
18913
18914 static int
18915 offset_type_compare (const void *ap, const void *bp)
18916 {
18917 offset_type a = *(offset_type *) ap;
18918 offset_type b = *(offset_type *) bp;
18919
18920 return (a > b) - (b > a);
18921 }
18922
18923 /* Sort and remove duplicates of all symbols' cu_indices lists. */
18924
18925 static void
18926 uniquify_cu_indices (struct mapped_symtab *symtab)
18927 {
18928 int i;
18929
18930 for (i = 0; i < symtab->size; ++i)
18931 {
18932 struct symtab_index_entry *entry = symtab->data[i];
18933
18934 if (entry
18935 && entry->cu_indices != NULL)
18936 {
18937 unsigned int next_to_insert, next_to_check;
18938 offset_type last_value;
18939
18940 qsort (VEC_address (offset_type, entry->cu_indices),
18941 VEC_length (offset_type, entry->cu_indices),
18942 sizeof (offset_type), offset_type_compare);
18943
18944 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18945 next_to_insert = 1;
18946 for (next_to_check = 1;
18947 next_to_check < VEC_length (offset_type, entry->cu_indices);
18948 ++next_to_check)
18949 {
18950 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18951 != last_value)
18952 {
18953 last_value = VEC_index (offset_type, entry->cu_indices,
18954 next_to_check);
18955 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18956 last_value);
18957 ++next_to_insert;
18958 }
18959 }
18960 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18961 }
18962 }
18963 }
18964
18965 /* Add a vector of indices to the constant pool. */
18966
18967 static offset_type
18968 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18969 struct symtab_index_entry *entry)
18970 {
18971 void **slot;
18972
18973 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18974 if (!*slot)
18975 {
18976 offset_type len = VEC_length (offset_type, entry->cu_indices);
18977 offset_type val = MAYBE_SWAP (len);
18978 offset_type iter;
18979 int i;
18980
18981 *slot = entry;
18982 entry->index_offset = obstack_object_size (cpool);
18983
18984 obstack_grow (cpool, &val, sizeof (val));
18985 for (i = 0;
18986 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18987 ++i)
18988 {
18989 val = MAYBE_SWAP (iter);
18990 obstack_grow (cpool, &val, sizeof (val));
18991 }
18992 }
18993 else
18994 {
18995 struct symtab_index_entry *old_entry = *slot;
18996 entry->index_offset = old_entry->index_offset;
18997 entry = old_entry;
18998 }
18999 return entry->index_offset;
19000 }
19001
19002 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19003 constant pool entries going into the obstack CPOOL. */
19004
19005 static void
19006 write_hash_table (struct mapped_symtab *symtab,
19007 struct obstack *output, struct obstack *cpool)
19008 {
19009 offset_type i;
19010 htab_t symbol_hash_table;
19011 htab_t str_table;
19012
19013 symbol_hash_table = create_symbol_hash_table ();
19014 str_table = create_strtab ();
19015
19016 /* We add all the index vectors to the constant pool first, to
19017 ensure alignment is ok. */
19018 for (i = 0; i < symtab->size; ++i)
19019 {
19020 if (symtab->data[i])
19021 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19022 }
19023
19024 /* Now write out the hash table. */
19025 for (i = 0; i < symtab->size; ++i)
19026 {
19027 offset_type str_off, vec_off;
19028
19029 if (symtab->data[i])
19030 {
19031 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19032 vec_off = symtab->data[i]->index_offset;
19033 }
19034 else
19035 {
19036 /* While 0 is a valid constant pool index, it is not valid
19037 to have 0 for both offsets. */
19038 str_off = 0;
19039 vec_off = 0;
19040 }
19041
19042 str_off = MAYBE_SWAP (str_off);
19043 vec_off = MAYBE_SWAP (vec_off);
19044
19045 obstack_grow (output, &str_off, sizeof (str_off));
19046 obstack_grow (output, &vec_off, sizeof (vec_off));
19047 }
19048
19049 htab_delete (str_table);
19050 htab_delete (symbol_hash_table);
19051 }
19052
19053 /* Struct to map psymtab to CU index in the index file. */
19054 struct psymtab_cu_index_map
19055 {
19056 struct partial_symtab *psymtab;
19057 unsigned int cu_index;
19058 };
19059
19060 static hashval_t
19061 hash_psymtab_cu_index (const void *item)
19062 {
19063 const struct psymtab_cu_index_map *map = item;
19064
19065 return htab_hash_pointer (map->psymtab);
19066 }
19067
19068 static int
19069 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19070 {
19071 const struct psymtab_cu_index_map *lhs = item_lhs;
19072 const struct psymtab_cu_index_map *rhs = item_rhs;
19073
19074 return lhs->psymtab == rhs->psymtab;
19075 }
19076
19077 /* Helper struct for building the address table. */
19078 struct addrmap_index_data
19079 {
19080 struct objfile *objfile;
19081 struct obstack *addr_obstack;
19082 htab_t cu_index_htab;
19083
19084 /* Non-zero if the previous_* fields are valid.
19085 We can't write an entry until we see the next entry (since it is only then
19086 that we know the end of the entry). */
19087 int previous_valid;
19088 /* Index of the CU in the table of all CUs in the index file. */
19089 unsigned int previous_cu_index;
19090 /* Start address of the CU. */
19091 CORE_ADDR previous_cu_start;
19092 };
19093
19094 /* Write an address entry to OBSTACK. */
19095
19096 static void
19097 add_address_entry (struct objfile *objfile, struct obstack *obstack,
19098 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19099 {
19100 offset_type cu_index_to_write;
19101 char addr[8];
19102 CORE_ADDR baseaddr;
19103
19104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19105
19106 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19107 obstack_grow (obstack, addr, 8);
19108 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19109 obstack_grow (obstack, addr, 8);
19110 cu_index_to_write = MAYBE_SWAP (cu_index);
19111 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19112 }
19113
19114 /* Worker function for traversing an addrmap to build the address table. */
19115
19116 static int
19117 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19118 {
19119 struct addrmap_index_data *data = datap;
19120 struct partial_symtab *pst = obj;
19121
19122 if (data->previous_valid)
19123 add_address_entry (data->objfile, data->addr_obstack,
19124 data->previous_cu_start, start_addr,
19125 data->previous_cu_index);
19126
19127 data->previous_cu_start = start_addr;
19128 if (pst != NULL)
19129 {
19130 struct psymtab_cu_index_map find_map, *map;
19131 find_map.psymtab = pst;
19132 map = htab_find (data->cu_index_htab, &find_map);
19133 gdb_assert (map != NULL);
19134 data->previous_cu_index = map->cu_index;
19135 data->previous_valid = 1;
19136 }
19137 else
19138 data->previous_valid = 0;
19139
19140 return 0;
19141 }
19142
19143 /* Write OBJFILE's address map to OBSTACK.
19144 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19145 in the index file. */
19146
19147 static void
19148 write_address_map (struct objfile *objfile, struct obstack *obstack,
19149 htab_t cu_index_htab)
19150 {
19151 struct addrmap_index_data addrmap_index_data;
19152
19153 /* When writing the address table, we have to cope with the fact that
19154 the addrmap iterator only provides the start of a region; we have to
19155 wait until the next invocation to get the start of the next region. */
19156
19157 addrmap_index_data.objfile = objfile;
19158 addrmap_index_data.addr_obstack = obstack;
19159 addrmap_index_data.cu_index_htab = cu_index_htab;
19160 addrmap_index_data.previous_valid = 0;
19161
19162 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
19163 &addrmap_index_data);
19164
19165 /* It's highly unlikely the last entry (end address = 0xff...ff)
19166 is valid, but we should still handle it.
19167 The end address is recorded as the start of the next region, but that
19168 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
19169 anyway. */
19170 if (addrmap_index_data.previous_valid)
19171 add_address_entry (objfile, obstack,
19172 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
19173 addrmap_index_data.previous_cu_index);
19174 }
19175
19176 /* Return the symbol kind of PSYM. */
19177
19178 static gdb_index_symbol_kind
19179 symbol_kind (struct partial_symbol *psym)
19180 {
19181 domain_enum domain = PSYMBOL_DOMAIN (psym);
19182 enum address_class aclass = PSYMBOL_CLASS (psym);
19183
19184 switch (domain)
19185 {
19186 case VAR_DOMAIN:
19187 switch (aclass)
19188 {
19189 case LOC_BLOCK:
19190 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
19191 case LOC_TYPEDEF:
19192 return GDB_INDEX_SYMBOL_KIND_TYPE;
19193 case LOC_COMPUTED:
19194 case LOC_CONST_BYTES:
19195 case LOC_OPTIMIZED_OUT:
19196 case LOC_STATIC:
19197 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19198 case LOC_CONST:
19199 /* Note: It's currently impossible to recognize psyms as enum values
19200 short of reading the type info. For now punt. */
19201 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19202 default:
19203 /* There are other LOC_FOO values that one might want to classify
19204 as variables, but dwarf2read.c doesn't currently use them. */
19205 return GDB_INDEX_SYMBOL_KIND_OTHER;
19206 }
19207 case STRUCT_DOMAIN:
19208 return GDB_INDEX_SYMBOL_KIND_TYPE;
19209 default:
19210 return GDB_INDEX_SYMBOL_KIND_OTHER;
19211 }
19212 }
19213
19214 /* Add a list of partial symbols to SYMTAB. */
19215
19216 static void
19217 write_psymbols (struct mapped_symtab *symtab,
19218 htab_t psyms_seen,
19219 struct partial_symbol **psymp,
19220 int count,
19221 offset_type cu_index,
19222 int is_static)
19223 {
19224 for (; count-- > 0; ++psymp)
19225 {
19226 struct partial_symbol *psym = *psymp;
19227 void **slot;
19228
19229 if (SYMBOL_LANGUAGE (psym) == language_ada)
19230 error (_("Ada is not currently supported by the index"));
19231
19232 /* Only add a given psymbol once. */
19233 slot = htab_find_slot (psyms_seen, psym, INSERT);
19234 if (!*slot)
19235 {
19236 gdb_index_symbol_kind kind = symbol_kind (psym);
19237
19238 *slot = psym;
19239 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
19240 is_static, kind, cu_index);
19241 }
19242 }
19243 }
19244
19245 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
19246 exception if there is an error. */
19247
19248 static void
19249 write_obstack (FILE *file, struct obstack *obstack)
19250 {
19251 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
19252 file)
19253 != obstack_object_size (obstack))
19254 error (_("couldn't data write to file"));
19255 }
19256
19257 /* Unlink a file if the argument is not NULL. */
19258
19259 static void
19260 unlink_if_set (void *p)
19261 {
19262 char **filename = p;
19263 if (*filename)
19264 unlink (*filename);
19265 }
19266
19267 /* A helper struct used when iterating over debug_types. */
19268 struct signatured_type_index_data
19269 {
19270 struct objfile *objfile;
19271 struct mapped_symtab *symtab;
19272 struct obstack *types_list;
19273 htab_t psyms_seen;
19274 int cu_index;
19275 };
19276
19277 /* A helper function that writes a single signatured_type to an
19278 obstack. */
19279
19280 static int
19281 write_one_signatured_type (void **slot, void *d)
19282 {
19283 struct signatured_type_index_data *info = d;
19284 struct signatured_type *entry = (struct signatured_type *) *slot;
19285 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19286 struct partial_symtab *psymtab = per_cu->v.psymtab;
19287 gdb_byte val[8];
19288
19289 write_psymbols (info->symtab,
19290 info->psyms_seen,
19291 info->objfile->global_psymbols.list
19292 + psymtab->globals_offset,
19293 psymtab->n_global_syms, info->cu_index,
19294 0);
19295 write_psymbols (info->symtab,
19296 info->psyms_seen,
19297 info->objfile->static_psymbols.list
19298 + psymtab->statics_offset,
19299 psymtab->n_static_syms, info->cu_index,
19300 1);
19301
19302 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19303 entry->per_cu.offset.sect_off);
19304 obstack_grow (info->types_list, val, 8);
19305 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19306 entry->type_offset_in_tu.cu_off);
19307 obstack_grow (info->types_list, val, 8);
19308 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19309 obstack_grow (info->types_list, val, 8);
19310
19311 ++info->cu_index;
19312
19313 return 1;
19314 }
19315
19316 /* Recurse into all "included" dependencies and write their symbols as
19317 if they appeared in this psymtab. */
19318
19319 static void
19320 recursively_write_psymbols (struct objfile *objfile,
19321 struct partial_symtab *psymtab,
19322 struct mapped_symtab *symtab,
19323 htab_t psyms_seen,
19324 offset_type cu_index)
19325 {
19326 int i;
19327
19328 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19329 if (psymtab->dependencies[i]->user != NULL)
19330 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19331 symtab, psyms_seen, cu_index);
19332
19333 write_psymbols (symtab,
19334 psyms_seen,
19335 objfile->global_psymbols.list + psymtab->globals_offset,
19336 psymtab->n_global_syms, cu_index,
19337 0);
19338 write_psymbols (symtab,
19339 psyms_seen,
19340 objfile->static_psymbols.list + psymtab->statics_offset,
19341 psymtab->n_static_syms, cu_index,
19342 1);
19343 }
19344
19345 /* Create an index file for OBJFILE in the directory DIR. */
19346
19347 static void
19348 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19349 {
19350 struct cleanup *cleanup;
19351 char *filename, *cleanup_filename;
19352 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19353 struct obstack cu_list, types_cu_list;
19354 int i;
19355 FILE *out_file;
19356 struct mapped_symtab *symtab;
19357 offset_type val, size_of_contents, total_len;
19358 struct stat st;
19359 htab_t psyms_seen;
19360 htab_t cu_index_htab;
19361 struct psymtab_cu_index_map *psymtab_cu_index_map;
19362
19363 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
19364 return;
19365
19366 if (dwarf2_per_objfile->using_index)
19367 error (_("Cannot use an index to create the index"));
19368
19369 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19370 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19371
19372 if (stat (objfile->name, &st) < 0)
19373 perror_with_name (objfile->name);
19374
19375 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19376 INDEX_SUFFIX, (char *) NULL);
19377 cleanup = make_cleanup (xfree, filename);
19378
19379 out_file = fopen (filename, "wb");
19380 if (!out_file)
19381 error (_("Can't open `%s' for writing"), filename);
19382
19383 cleanup_filename = filename;
19384 make_cleanup (unlink_if_set, &cleanup_filename);
19385
19386 symtab = create_mapped_symtab ();
19387 make_cleanup (cleanup_mapped_symtab, symtab);
19388
19389 obstack_init (&addr_obstack);
19390 make_cleanup_obstack_free (&addr_obstack);
19391
19392 obstack_init (&cu_list);
19393 make_cleanup_obstack_free (&cu_list);
19394
19395 obstack_init (&types_cu_list);
19396 make_cleanup_obstack_free (&types_cu_list);
19397
19398 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19399 NULL, xcalloc, xfree);
19400 make_cleanup_htab_delete (psyms_seen);
19401
19402 /* While we're scanning CU's create a table that maps a psymtab pointer
19403 (which is what addrmap records) to its index (which is what is recorded
19404 in the index file). This will later be needed to write the address
19405 table. */
19406 cu_index_htab = htab_create_alloc (100,
19407 hash_psymtab_cu_index,
19408 eq_psymtab_cu_index,
19409 NULL, xcalloc, xfree);
19410 make_cleanup_htab_delete (cu_index_htab);
19411 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19412 xmalloc (sizeof (struct psymtab_cu_index_map)
19413 * dwarf2_per_objfile->n_comp_units);
19414 make_cleanup (xfree, psymtab_cu_index_map);
19415
19416 /* The CU list is already sorted, so we don't need to do additional
19417 work here. Also, the debug_types entries do not appear in
19418 all_comp_units, but only in their own hash table. */
19419 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19420 {
19421 struct dwarf2_per_cu_data *per_cu
19422 = dwarf2_per_objfile->all_comp_units[i];
19423 struct partial_symtab *psymtab = per_cu->v.psymtab;
19424 gdb_byte val[8];
19425 struct psymtab_cu_index_map *map;
19426 void **slot;
19427
19428 if (psymtab->user == NULL)
19429 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
19430
19431 map = &psymtab_cu_index_map[i];
19432 map->psymtab = psymtab;
19433 map->cu_index = i;
19434 slot = htab_find_slot (cu_index_htab, map, INSERT);
19435 gdb_assert (slot != NULL);
19436 gdb_assert (*slot == NULL);
19437 *slot = map;
19438
19439 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19440 per_cu->offset.sect_off);
19441 obstack_grow (&cu_list, val, 8);
19442 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
19443 obstack_grow (&cu_list, val, 8);
19444 }
19445
19446 /* Dump the address map. */
19447 write_address_map (objfile, &addr_obstack, cu_index_htab);
19448
19449 /* Write out the .debug_type entries, if any. */
19450 if (dwarf2_per_objfile->signatured_types)
19451 {
19452 struct signatured_type_index_data sig_data;
19453
19454 sig_data.objfile = objfile;
19455 sig_data.symtab = symtab;
19456 sig_data.types_list = &types_cu_list;
19457 sig_data.psyms_seen = psyms_seen;
19458 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19459 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19460 write_one_signatured_type, &sig_data);
19461 }
19462
19463 /* Now that we've processed all symbols we can shrink their cu_indices
19464 lists. */
19465 uniquify_cu_indices (symtab);
19466
19467 obstack_init (&constant_pool);
19468 make_cleanup_obstack_free (&constant_pool);
19469 obstack_init (&symtab_obstack);
19470 make_cleanup_obstack_free (&symtab_obstack);
19471 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19472
19473 obstack_init (&contents);
19474 make_cleanup_obstack_free (&contents);
19475 size_of_contents = 6 * sizeof (offset_type);
19476 total_len = size_of_contents;
19477
19478 /* The version number. */
19479 val = MAYBE_SWAP (7);
19480 obstack_grow (&contents, &val, sizeof (val));
19481
19482 /* The offset of the CU list from the start of the file. */
19483 val = MAYBE_SWAP (total_len);
19484 obstack_grow (&contents, &val, sizeof (val));
19485 total_len += obstack_object_size (&cu_list);
19486
19487 /* The offset of the types CU list from the start of the file. */
19488 val = MAYBE_SWAP (total_len);
19489 obstack_grow (&contents, &val, sizeof (val));
19490 total_len += obstack_object_size (&types_cu_list);
19491
19492 /* The offset of the address table from the start of the file. */
19493 val = MAYBE_SWAP (total_len);
19494 obstack_grow (&contents, &val, sizeof (val));
19495 total_len += obstack_object_size (&addr_obstack);
19496
19497 /* The offset of the symbol table from the start of the file. */
19498 val = MAYBE_SWAP (total_len);
19499 obstack_grow (&contents, &val, sizeof (val));
19500 total_len += obstack_object_size (&symtab_obstack);
19501
19502 /* The offset of the constant pool 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 (&constant_pool);
19506
19507 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19508
19509 write_obstack (out_file, &contents);
19510 write_obstack (out_file, &cu_list);
19511 write_obstack (out_file, &types_cu_list);
19512 write_obstack (out_file, &addr_obstack);
19513 write_obstack (out_file, &symtab_obstack);
19514 write_obstack (out_file, &constant_pool);
19515
19516 fclose (out_file);
19517
19518 /* We want to keep the file, so we set cleanup_filename to NULL
19519 here. See unlink_if_set. */
19520 cleanup_filename = NULL;
19521
19522 do_cleanups (cleanup);
19523 }
19524
19525 /* Implementation of the `save gdb-index' command.
19526
19527 Note that the file format used by this command is documented in the
19528 GDB manual. Any changes here must be documented there. */
19529
19530 static void
19531 save_gdb_index_command (char *arg, int from_tty)
19532 {
19533 struct objfile *objfile;
19534
19535 if (!arg || !*arg)
19536 error (_("usage: save gdb-index DIRECTORY"));
19537
19538 ALL_OBJFILES (objfile)
19539 {
19540 struct stat st;
19541
19542 /* If the objfile does not correspond to an actual file, skip it. */
19543 if (stat (objfile->name, &st) < 0)
19544 continue;
19545
19546 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19547 if (dwarf2_per_objfile)
19548 {
19549 volatile struct gdb_exception except;
19550
19551 TRY_CATCH (except, RETURN_MASK_ERROR)
19552 {
19553 write_psymtabs_to_index (objfile, arg);
19554 }
19555 if (except.reason < 0)
19556 exception_fprintf (gdb_stderr, except,
19557 _("Error while writing index for `%s': "),
19558 objfile->name);
19559 }
19560 }
19561 }
19562
19563 \f
19564
19565 int dwarf2_always_disassemble;
19566
19567 static void
19568 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19569 struct cmd_list_element *c, const char *value)
19570 {
19571 fprintf_filtered (file,
19572 _("Whether to always disassemble "
19573 "DWARF expressions is %s.\n"),
19574 value);
19575 }
19576
19577 static void
19578 show_check_physname (struct ui_file *file, int from_tty,
19579 struct cmd_list_element *c, const char *value)
19580 {
19581 fprintf_filtered (file,
19582 _("Whether to check \"physname\" is %s.\n"),
19583 value);
19584 }
19585
19586 void _initialize_dwarf2_read (void);
19587
19588 void
19589 _initialize_dwarf2_read (void)
19590 {
19591 struct cmd_list_element *c;
19592
19593 dwarf2_objfile_data_key
19594 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19595
19596 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19597 Set DWARF 2 specific variables.\n\
19598 Configure DWARF 2 variables such as the cache size"),
19599 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19600 0/*allow-unknown*/, &maintenance_set_cmdlist);
19601
19602 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19603 Show DWARF 2 specific variables\n\
19604 Show DWARF 2 variables such as the cache size"),
19605 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19606 0/*allow-unknown*/, &maintenance_show_cmdlist);
19607
19608 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19609 &dwarf2_max_cache_age, _("\
19610 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19611 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19612 A higher limit means that cached compilation units will be stored\n\
19613 in memory longer, and more total memory will be used. Zero disables\n\
19614 caching, which can slow down startup."),
19615 NULL,
19616 show_dwarf2_max_cache_age,
19617 &set_dwarf2_cmdlist,
19618 &show_dwarf2_cmdlist);
19619
19620 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19621 &dwarf2_always_disassemble, _("\
19622 Set whether `info address' always disassembles DWARF expressions."), _("\
19623 Show whether `info address' always disassembles DWARF expressions."), _("\
19624 When enabled, DWARF expressions are always printed in an assembly-like\n\
19625 syntax. When disabled, expressions will be printed in a more\n\
19626 conversational style, when possible."),
19627 NULL,
19628 show_dwarf2_always_disassemble,
19629 &set_dwarf2_cmdlist,
19630 &show_dwarf2_cmdlist);
19631
19632 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19633 Set debugging of the dwarf2 reader."), _("\
19634 Show debugging of the dwarf2 reader."), _("\
19635 When enabled, debugging messages are printed during dwarf2 reading\n\
19636 and symtab expansion."),
19637 NULL,
19638 NULL,
19639 &setdebuglist, &showdebuglist);
19640
19641 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19642 Set debugging of the dwarf2 DIE reader."), _("\
19643 Show debugging of the dwarf2 DIE reader."), _("\
19644 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19645 The value is the maximum depth to print."),
19646 NULL,
19647 NULL,
19648 &setdebuglist, &showdebuglist);
19649
19650 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19651 Set cross-checking of \"physname\" code against demangler."), _("\
19652 Show cross-checking of \"physname\" code against demangler."), _("\
19653 When enabled, GDB's internal \"physname\" code is checked against\n\
19654 the demangler."),
19655 NULL, show_check_physname,
19656 &setdebuglist, &showdebuglist);
19657
19658 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19659 _("\
19660 Save a gdb-index file.\n\
19661 Usage: save gdb-index DIRECTORY"),
19662 &save_cmdlist);
19663 set_cmd_completer (c, filename_completer);
19664 }
This page took 0.454795 seconds and 4 git commands to generate.