* dwarf2read.c (dw2_do_expand_symtabs_matching): Don't examine
[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 unsigned 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 static 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 size_t 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 flagword aflag = bfd_get_section_flags (abfd, sectp);
1714
1715 if (vnames == NULL)
1716 names = &dwarf2_elf_names;
1717 else
1718 names = (const struct dwarf2_debug_sections *) vnames;
1719
1720 if ((aflag & SEC_HAS_CONTENTS) == 0)
1721 {
1722 }
1723 else if (section_is_p (sectp->name, &names->info))
1724 {
1725 dwarf2_per_objfile->info.asection = sectp;
1726 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1727 }
1728 else if (section_is_p (sectp->name, &names->abbrev))
1729 {
1730 dwarf2_per_objfile->abbrev.asection = sectp;
1731 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1732 }
1733 else if (section_is_p (sectp->name, &names->line))
1734 {
1735 dwarf2_per_objfile->line.asection = sectp;
1736 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1737 }
1738 else if (section_is_p (sectp->name, &names->loc))
1739 {
1740 dwarf2_per_objfile->loc.asection = sectp;
1741 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1742 }
1743 else if (section_is_p (sectp->name, &names->macinfo))
1744 {
1745 dwarf2_per_objfile->macinfo.asection = sectp;
1746 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1747 }
1748 else if (section_is_p (sectp->name, &names->macro))
1749 {
1750 dwarf2_per_objfile->macro.asection = sectp;
1751 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1752 }
1753 else if (section_is_p (sectp->name, &names->str))
1754 {
1755 dwarf2_per_objfile->str.asection = sectp;
1756 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1757 }
1758 else if (section_is_p (sectp->name, &names->addr))
1759 {
1760 dwarf2_per_objfile->addr.asection = sectp;
1761 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1762 }
1763 else if (section_is_p (sectp->name, &names->frame))
1764 {
1765 dwarf2_per_objfile->frame.asection = sectp;
1766 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1767 }
1768 else if (section_is_p (sectp->name, &names->eh_frame))
1769 {
1770 dwarf2_per_objfile->eh_frame.asection = sectp;
1771 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1772 }
1773 else if (section_is_p (sectp->name, &names->ranges))
1774 {
1775 dwarf2_per_objfile->ranges.asection = sectp;
1776 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1777 }
1778 else if (section_is_p (sectp->name, &names->types))
1779 {
1780 struct dwarf2_section_info type_section;
1781
1782 memset (&type_section, 0, sizeof (type_section));
1783 type_section.asection = sectp;
1784 type_section.size = bfd_get_section_size (sectp);
1785
1786 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1787 &type_section);
1788 }
1789 else if (section_is_p (sectp->name, &names->gdb_index))
1790 {
1791 dwarf2_per_objfile->gdb_index.asection = sectp;
1792 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1793 }
1794
1795 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1796 && bfd_section_vma (abfd, sectp) == 0)
1797 dwarf2_per_objfile->has_section_at_zero = 1;
1798 }
1799
1800 /* A helper function that decides whether a section is empty,
1801 or not present. */
1802
1803 static int
1804 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1805 {
1806 return info->asection == NULL || info->size == 0;
1807 }
1808
1809 /* Read the contents of the section INFO.
1810 OBJFILE is the main object file, but not necessarily the file where
1811 the section comes from. E.g., for DWO files INFO->asection->owner
1812 is the bfd of the DWO file.
1813 If the section is compressed, uncompress it before returning. */
1814
1815 static void
1816 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1817 {
1818 asection *sectp = info->asection;
1819 bfd *abfd;
1820 gdb_byte *buf, *retbuf;
1821 unsigned char header[4];
1822
1823 if (info->readin)
1824 return;
1825 info->buffer = NULL;
1826 info->readin = 1;
1827
1828 if (dwarf2_section_empty_p (info))
1829 return;
1830
1831 abfd = sectp->owner;
1832
1833 /* If the section has relocations, we must read it ourselves.
1834 Otherwise we attach it to the BFD. */
1835 if ((sectp->flags & SEC_RELOC) == 0)
1836 {
1837 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1838
1839 /* We have to cast away const here for historical reasons.
1840 Fixing dwarf2read to be const-correct would be quite nice. */
1841 info->buffer = (gdb_byte *) bytes;
1842 return;
1843 }
1844
1845 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1846 info->buffer = buf;
1847
1848 /* When debugging .o files, we may need to apply relocations; see
1849 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1850 We never compress sections in .o files, so we only need to
1851 try this when the section is not compressed. */
1852 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1853 if (retbuf != NULL)
1854 {
1855 info->buffer = retbuf;
1856 return;
1857 }
1858
1859 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1860 || bfd_bread (buf, info->size, abfd) != info->size)
1861 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1862 bfd_get_filename (abfd));
1863 }
1864
1865 /* A helper function that returns the size of a section in a safe way.
1866 If you are positive that the section has been read before using the
1867 size, then it is safe to refer to the dwarf2_section_info object's
1868 "size" field directly. In other cases, you must call this
1869 function, because for compressed sections the size field is not set
1870 correctly until the section has been read. */
1871
1872 static bfd_size_type
1873 dwarf2_section_size (struct objfile *objfile,
1874 struct dwarf2_section_info *info)
1875 {
1876 if (!info->readin)
1877 dwarf2_read_section (objfile, info);
1878 return info->size;
1879 }
1880
1881 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1882 SECTION_NAME. */
1883
1884 void
1885 dwarf2_get_section_info (struct objfile *objfile,
1886 enum dwarf2_section_enum sect,
1887 asection **sectp, gdb_byte **bufp,
1888 bfd_size_type *sizep)
1889 {
1890 struct dwarf2_per_objfile *data
1891 = objfile_data (objfile, dwarf2_objfile_data_key);
1892 struct dwarf2_section_info *info;
1893
1894 /* We may see an objfile without any DWARF, in which case we just
1895 return nothing. */
1896 if (data == NULL)
1897 {
1898 *sectp = NULL;
1899 *bufp = NULL;
1900 *sizep = 0;
1901 return;
1902 }
1903 switch (sect)
1904 {
1905 case DWARF2_DEBUG_FRAME:
1906 info = &data->frame;
1907 break;
1908 case DWARF2_EH_FRAME:
1909 info = &data->eh_frame;
1910 break;
1911 default:
1912 gdb_assert_not_reached ("unexpected section");
1913 }
1914
1915 dwarf2_read_section (objfile, info);
1916
1917 *sectp = info->asection;
1918 *bufp = info->buffer;
1919 *sizep = info->size;
1920 }
1921
1922 /* A helper function to find the sections for a .dwz file. */
1923
1924 static void
1925 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1926 {
1927 struct dwz_file *dwz_file = arg;
1928
1929 /* Note that we only support the standard ELF names, because .dwz
1930 is ELF-only (at the time of writing). */
1931 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1932 {
1933 dwz_file->abbrev.asection = sectp;
1934 dwz_file->abbrev.size = bfd_get_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1937 {
1938 dwz_file->info.asection = sectp;
1939 dwz_file->info.size = bfd_get_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1942 {
1943 dwz_file->str.asection = sectp;
1944 dwz_file->str.size = bfd_get_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1947 {
1948 dwz_file->line.asection = sectp;
1949 dwz_file->line.size = bfd_get_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1952 {
1953 dwz_file->macro.asection = sectp;
1954 dwz_file->macro.size = bfd_get_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1957 {
1958 dwz_file->gdb_index.asection = sectp;
1959 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1960 }
1961 }
1962
1963 /* Open the separate '.dwz' debug file, if needed. Error if the file
1964 cannot be found. */
1965
1966 static struct dwz_file *
1967 dwarf2_get_dwz_file (void)
1968 {
1969 bfd *abfd, *dwz_bfd;
1970 asection *section;
1971 gdb_byte *data;
1972 struct cleanup *cleanup;
1973 const char *filename;
1974 struct dwz_file *result;
1975
1976 if (dwarf2_per_objfile->dwz_file != NULL)
1977 return dwarf2_per_objfile->dwz_file;
1978
1979 abfd = dwarf2_per_objfile->objfile->obfd;
1980 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1981 if (section == NULL)
1982 error (_("could not find '.gnu_debugaltlink' section"));
1983 if (!bfd_malloc_and_get_section (abfd, section, &data))
1984 error (_("could not read '.gnu_debugaltlink' section: %s"),
1985 bfd_errmsg (bfd_get_error ()));
1986 cleanup = make_cleanup (xfree, data);
1987
1988 filename = data;
1989 if (!IS_ABSOLUTE_PATH (filename))
1990 {
1991 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
1992 char *rel;
1993
1994 make_cleanup (xfree, abs);
1995 abs = ldirname (abs);
1996 make_cleanup (xfree, abs);
1997
1998 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
1999 make_cleanup (xfree, rel);
2000 filename = rel;
2001 }
2002
2003 /* The format is just a NUL-terminated file name, followed by the
2004 build-id. For now, though, we ignore the build-id. */
2005 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2006 if (dwz_bfd == NULL)
2007 error (_("could not read '%s': %s"), filename,
2008 bfd_errmsg (bfd_get_error ()));
2009
2010 if (!bfd_check_format (dwz_bfd, bfd_object))
2011 {
2012 gdb_bfd_unref (dwz_bfd);
2013 error (_("file '%s' was not usable: %s"), filename,
2014 bfd_errmsg (bfd_get_error ()));
2015 }
2016
2017 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2018 struct dwz_file);
2019 result->dwz_bfd = dwz_bfd;
2020
2021 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2022
2023 do_cleanups (cleanup);
2024
2025 return result;
2026 }
2027 \f
2028 /* DWARF quick_symbols_functions support. */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031 unique line tables, so we maintain a separate table of all .debug_line
2032 derived entries to support the sharing.
2033 All the quick functions need is the list of file names. We discard the
2034 line_header when we're done and don't need to record it here. */
2035 struct quick_file_names
2036 {
2037 /* The data used to construct the hash key. */
2038 struct stmt_list_hash hash;
2039
2040 /* The number of entries in file_names, real_names. */
2041 unsigned int num_file_names;
2042
2043 /* The file names from the line table, after being run through
2044 file_full_name. */
2045 const char **file_names;
2046
2047 /* The file names from the line table after being run through
2048 gdb_realpath. These are computed lazily. */
2049 const char **real_names;
2050 };
2051
2052 /* When using the index (and thus not using psymtabs), each CU has an
2053 object of this type. This is used to hold information needed by
2054 the various "quick" methods. */
2055 struct dwarf2_per_cu_quick_data
2056 {
2057 /* The file table. This can be NULL if there was no file table
2058 or it's currently not read in.
2059 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2060 struct quick_file_names *file_names;
2061
2062 /* The corresponding symbol table. This is NULL if symbols for this
2063 CU have not yet been read. */
2064 struct symtab *symtab;
2065
2066 /* A temporary mark bit used when iterating over all CUs in
2067 expand_symtabs_matching. */
2068 unsigned int mark : 1;
2069
2070 /* True if we've tried to read the file table and found there isn't one.
2071 There will be no point in trying to read it again next time. */
2072 unsigned int no_file_data : 1;
2073 };
2074
2075 /* Utility hash function for a stmt_list_hash. */
2076
2077 static hashval_t
2078 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2079 {
2080 hashval_t v = 0;
2081
2082 if (stmt_list_hash->dwo_unit != NULL)
2083 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2084 v += stmt_list_hash->line_offset.sect_off;
2085 return v;
2086 }
2087
2088 /* Utility equality function for a stmt_list_hash. */
2089
2090 static int
2091 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2092 const struct stmt_list_hash *rhs)
2093 {
2094 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2095 return 0;
2096 if (lhs->dwo_unit != NULL
2097 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2098 return 0;
2099
2100 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2101 }
2102
2103 /* Hash function for a quick_file_names. */
2104
2105 static hashval_t
2106 hash_file_name_entry (const void *e)
2107 {
2108 const struct quick_file_names *file_data = e;
2109
2110 return hash_stmt_list_entry (&file_data->hash);
2111 }
2112
2113 /* Equality function for a quick_file_names. */
2114
2115 static int
2116 eq_file_name_entry (const void *a, const void *b)
2117 {
2118 const struct quick_file_names *ea = a;
2119 const struct quick_file_names *eb = b;
2120
2121 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2122 }
2123
2124 /* Delete function for a quick_file_names. */
2125
2126 static void
2127 delete_file_name_entry (void *e)
2128 {
2129 struct quick_file_names *file_data = e;
2130 int i;
2131
2132 for (i = 0; i < file_data->num_file_names; ++i)
2133 {
2134 xfree ((void*) file_data->file_names[i]);
2135 if (file_data->real_names)
2136 xfree ((void*) file_data->real_names[i]);
2137 }
2138
2139 /* The space for the struct itself lives on objfile_obstack,
2140 so we don't free it here. */
2141 }
2142
2143 /* Create a quick_file_names hash table. */
2144
2145 static htab_t
2146 create_quick_file_names_table (unsigned int nr_initial_entries)
2147 {
2148 return htab_create_alloc (nr_initial_entries,
2149 hash_file_name_entry, eq_file_name_entry,
2150 delete_file_name_entry, xcalloc, xfree);
2151 }
2152
2153 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2154 have to be created afterwards. You should call age_cached_comp_units after
2155 processing PER_CU->CU. dw2_setup must have been already called. */
2156
2157 static void
2158 load_cu (struct dwarf2_per_cu_data *per_cu)
2159 {
2160 if (per_cu->is_debug_types)
2161 load_full_type_unit (per_cu);
2162 else
2163 load_full_comp_unit (per_cu, language_minimal);
2164
2165 gdb_assert (per_cu->cu != NULL);
2166
2167 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2168 }
2169
2170 /* Read in the symbols for PER_CU. */
2171
2172 static void
2173 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2174 {
2175 struct cleanup *back_to;
2176
2177 /* Skip type_unit_groups, reading the type units they contain
2178 is handled elsewhere. */
2179 if (IS_TYPE_UNIT_GROUP (per_cu))
2180 return;
2181
2182 back_to = make_cleanup (dwarf2_release_queue, NULL);
2183
2184 if (dwarf2_per_objfile->using_index
2185 ? per_cu->v.quick->symtab == NULL
2186 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2187 {
2188 queue_comp_unit (per_cu, language_minimal);
2189 load_cu (per_cu);
2190 }
2191
2192 process_queue ();
2193
2194 /* Age the cache, releasing compilation units that have not
2195 been used recently. */
2196 age_cached_comp_units ();
2197
2198 do_cleanups (back_to);
2199 }
2200
2201 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2202 the objfile from which this CU came. Returns the resulting symbol
2203 table. */
2204
2205 static struct symtab *
2206 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2207 {
2208 gdb_assert (dwarf2_per_objfile->using_index);
2209 if (!per_cu->v.quick->symtab)
2210 {
2211 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2212 increment_reading_symtab ();
2213 dw2_do_instantiate_symtab (per_cu);
2214 process_cu_includes ();
2215 do_cleanups (back_to);
2216 }
2217 return per_cu->v.quick->symtab;
2218 }
2219
2220 /* Return the CU given its index.
2221
2222 This is intended for loops like:
2223
2224 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2225 + dwarf2_per_objfile->n_type_units); ++i)
2226 {
2227 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2228
2229 ...;
2230 }
2231 */
2232
2233 static struct dwarf2_per_cu_data *
2234 dw2_get_cu (int index)
2235 {
2236 if (index >= dwarf2_per_objfile->n_comp_units)
2237 {
2238 index -= dwarf2_per_objfile->n_comp_units;
2239 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2240 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2241 }
2242
2243 return dwarf2_per_objfile->all_comp_units[index];
2244 }
2245
2246 /* Return the primary CU given its index.
2247 The difference between this function and dw2_get_cu is in the handling
2248 of type units (TUs). Here we return the type_unit_group object.
2249
2250 This is intended for loops like:
2251
2252 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2253 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2254 {
2255 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2256
2257 ...;
2258 }
2259 */
2260
2261 static struct dwarf2_per_cu_data *
2262 dw2_get_primary_cu (int index)
2263 {
2264 if (index >= dwarf2_per_objfile->n_comp_units)
2265 {
2266 index -= dwarf2_per_objfile->n_comp_units;
2267 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2268 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2269 }
2270
2271 return dwarf2_per_objfile->all_comp_units[index];
2272 }
2273
2274 /* A helper function that knows how to read a 64-bit value in a way
2275 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2276 otherwise. */
2277
2278 static int
2279 extract_cu_value (const char *bytes, ULONGEST *result)
2280 {
2281 if (sizeof (ULONGEST) < 8)
2282 {
2283 int i;
2284
2285 /* Ignore the upper 4 bytes if they are all zero. */
2286 for (i = 0; i < 4; ++i)
2287 if (bytes[i + 4] != 0)
2288 return 0;
2289
2290 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2291 }
2292 else
2293 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2294 return 1;
2295 }
2296
2297 /* A helper for create_cus_from_index that handles a given list of
2298 CUs. */
2299
2300 static int
2301 create_cus_from_index_list (struct objfile *objfile,
2302 const gdb_byte *cu_list, offset_type n_elements,
2303 struct dwarf2_section_info *section,
2304 int is_dwz,
2305 int base_offset)
2306 {
2307 offset_type i;
2308
2309 for (i = 0; i < n_elements; i += 2)
2310 {
2311 struct dwarf2_per_cu_data *the_cu;
2312 ULONGEST offset, length;
2313
2314 if (!extract_cu_value (cu_list, &offset)
2315 || !extract_cu_value (cu_list + 8, &length))
2316 return 0;
2317 cu_list += 2 * 8;
2318
2319 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2320 struct dwarf2_per_cu_data);
2321 the_cu->offset.sect_off = offset;
2322 the_cu->length = length;
2323 the_cu->objfile = objfile;
2324 the_cu->info_or_types_section = section;
2325 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2326 struct dwarf2_per_cu_quick_data);
2327 the_cu->is_dwz = is_dwz;
2328 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2329 }
2330
2331 return 1;
2332 }
2333
2334 /* Read the CU list from the mapped index, and use it to create all
2335 the CU objects for this objfile. Return 0 if something went wrong,
2336 1 if everything went ok. */
2337
2338 static int
2339 create_cus_from_index (struct objfile *objfile,
2340 const gdb_byte *cu_list, offset_type cu_list_elements,
2341 const gdb_byte *dwz_list, offset_type dwz_elements)
2342 {
2343 struct dwz_file *dwz;
2344
2345 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2346 dwarf2_per_objfile->all_comp_units
2347 = obstack_alloc (&objfile->objfile_obstack,
2348 dwarf2_per_objfile->n_comp_units
2349 * sizeof (struct dwarf2_per_cu_data *));
2350
2351 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2352 &dwarf2_per_objfile->info, 0, 0))
2353 return 0;
2354
2355 if (dwz_elements == 0)
2356 return 1;
2357
2358 dwz = dwarf2_get_dwz_file ();
2359 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2360 &dwz->info, 1, cu_list_elements / 2);
2361 }
2362
2363 /* Create the signatured type hash table from the index. */
2364
2365 static int
2366 create_signatured_type_table_from_index (struct objfile *objfile,
2367 struct dwarf2_section_info *section,
2368 const gdb_byte *bytes,
2369 offset_type elements)
2370 {
2371 offset_type i;
2372 htab_t sig_types_hash;
2373
2374 dwarf2_per_objfile->n_type_units = elements / 3;
2375 dwarf2_per_objfile->all_type_units
2376 = obstack_alloc (&objfile->objfile_obstack,
2377 dwarf2_per_objfile->n_type_units
2378 * sizeof (struct signatured_type *));
2379
2380 sig_types_hash = allocate_signatured_type_table (objfile);
2381
2382 for (i = 0; i < elements; i += 3)
2383 {
2384 struct signatured_type *sig_type;
2385 ULONGEST offset, type_offset_in_tu, signature;
2386 void **slot;
2387
2388 if (!extract_cu_value (bytes, &offset)
2389 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2390 return 0;
2391 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2392 bytes += 3 * 8;
2393
2394 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2395 struct signatured_type);
2396 sig_type->signature = signature;
2397 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2398 sig_type->per_cu.is_debug_types = 1;
2399 sig_type->per_cu.info_or_types_section = section;
2400 sig_type->per_cu.offset.sect_off = offset;
2401 sig_type->per_cu.objfile = objfile;
2402 sig_type->per_cu.v.quick
2403 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2404 struct dwarf2_per_cu_quick_data);
2405
2406 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2407 *slot = sig_type;
2408
2409 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2410 }
2411
2412 dwarf2_per_objfile->signatured_types = sig_types_hash;
2413
2414 return 1;
2415 }
2416
2417 /* Read the address map data from the mapped index, and use it to
2418 populate the objfile's psymtabs_addrmap. */
2419
2420 static void
2421 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2422 {
2423 const gdb_byte *iter, *end;
2424 struct obstack temp_obstack;
2425 struct addrmap *mutable_map;
2426 struct cleanup *cleanup;
2427 CORE_ADDR baseaddr;
2428
2429 obstack_init (&temp_obstack);
2430 cleanup = make_cleanup_obstack_free (&temp_obstack);
2431 mutable_map = addrmap_create_mutable (&temp_obstack);
2432
2433 iter = index->address_table;
2434 end = iter + index->address_table_size;
2435
2436 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2437
2438 while (iter < end)
2439 {
2440 ULONGEST hi, lo, cu_index;
2441 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2442 iter += 8;
2443 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2444 iter += 8;
2445 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2446 iter += 4;
2447
2448 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2449 dw2_get_cu (cu_index));
2450 }
2451
2452 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2453 &objfile->objfile_obstack);
2454 do_cleanups (cleanup);
2455 }
2456
2457 /* The hash function for strings in the mapped index. This is the same as
2458 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2459 implementation. This is necessary because the hash function is tied to the
2460 format of the mapped index file. The hash values do not have to match with
2461 SYMBOL_HASH_NEXT.
2462
2463 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2464
2465 static hashval_t
2466 mapped_index_string_hash (int index_version, const void *p)
2467 {
2468 const unsigned char *str = (const unsigned char *) p;
2469 hashval_t r = 0;
2470 unsigned char c;
2471
2472 while ((c = *str++) != 0)
2473 {
2474 if (index_version >= 5)
2475 c = tolower (c);
2476 r = r * 67 + c - 113;
2477 }
2478
2479 return r;
2480 }
2481
2482 /* Find a slot in the mapped index INDEX for the object named NAME.
2483 If NAME is found, set *VEC_OUT to point to the CU vector in the
2484 constant pool and return 1. If NAME cannot be found, return 0. */
2485
2486 static int
2487 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2488 offset_type **vec_out)
2489 {
2490 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2491 offset_type hash;
2492 offset_type slot, step;
2493 int (*cmp) (const char *, const char *);
2494
2495 if (current_language->la_language == language_cplus
2496 || current_language->la_language == language_java
2497 || current_language->la_language == language_fortran)
2498 {
2499 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2500 not contain any. */
2501 const char *paren = strchr (name, '(');
2502
2503 if (paren)
2504 {
2505 char *dup;
2506
2507 dup = xmalloc (paren - name + 1);
2508 memcpy (dup, name, paren - name);
2509 dup[paren - name] = 0;
2510
2511 make_cleanup (xfree, dup);
2512 name = dup;
2513 }
2514 }
2515
2516 /* Index version 4 did not support case insensitive searches. But the
2517 indices for case insensitive languages are built in lowercase, therefore
2518 simulate our NAME being searched is also lowercased. */
2519 hash = mapped_index_string_hash ((index->version == 4
2520 && case_sensitivity == case_sensitive_off
2521 ? 5 : index->version),
2522 name);
2523
2524 slot = hash & (index->symbol_table_slots - 1);
2525 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2526 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2527
2528 for (;;)
2529 {
2530 /* Convert a slot number to an offset into the table. */
2531 offset_type i = 2 * slot;
2532 const char *str;
2533 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2534 {
2535 do_cleanups (back_to);
2536 return 0;
2537 }
2538
2539 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2540 if (!cmp (name, str))
2541 {
2542 *vec_out = (offset_type *) (index->constant_pool
2543 + MAYBE_SWAP (index->symbol_table[i + 1]));
2544 do_cleanups (back_to);
2545 return 1;
2546 }
2547
2548 slot = (slot + step) & (index->symbol_table_slots - 1);
2549 }
2550 }
2551
2552 /* A helper function that reads the .gdb_index from SECTION and fills
2553 in MAP. FILENAME is the name of the file containing the section;
2554 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2555 ok to use deprecated sections.
2556
2557 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2558 out parameters that are filled in with information about the CU and
2559 TU lists in the section.
2560
2561 Returns 1 if all went well, 0 otherwise. */
2562
2563 static int
2564 read_index_from_section (struct objfile *objfile,
2565 const char *filename,
2566 int deprecated_ok,
2567 struct dwarf2_section_info *section,
2568 struct mapped_index *map,
2569 const gdb_byte **cu_list,
2570 offset_type *cu_list_elements,
2571 const gdb_byte **types_list,
2572 offset_type *types_list_elements)
2573 {
2574 char *addr;
2575 offset_type version;
2576 offset_type *metadata;
2577 int i;
2578
2579 if (dwarf2_section_empty_p (section))
2580 return 0;
2581
2582 /* Older elfutils strip versions could keep the section in the main
2583 executable while splitting it for the separate debug info file. */
2584 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2585 return 0;
2586
2587 dwarf2_read_section (objfile, section);
2588
2589 addr = section->buffer;
2590 /* Version check. */
2591 version = MAYBE_SWAP (*(offset_type *) addr);
2592 /* Versions earlier than 3 emitted every copy of a psymbol. This
2593 causes the index to behave very poorly for certain requests. Version 3
2594 contained incomplete addrmap. So, it seems better to just ignore such
2595 indices. */
2596 if (version < 4)
2597 {
2598 static int warning_printed = 0;
2599 if (!warning_printed)
2600 {
2601 warning (_("Skipping obsolete .gdb_index section in %s."),
2602 filename);
2603 warning_printed = 1;
2604 }
2605 return 0;
2606 }
2607 /* Index version 4 uses a different hash function than index version
2608 5 and later.
2609
2610 Versions earlier than 6 did not emit psymbols for inlined
2611 functions. Using these files will cause GDB not to be able to
2612 set breakpoints on inlined functions by name, so we ignore these
2613 indices unless the user has done
2614 "set use-deprecated-index-sections on". */
2615 if (version < 6 && !deprecated_ok)
2616 {
2617 static int warning_printed = 0;
2618 if (!warning_printed)
2619 {
2620 warning (_("\
2621 Skipping deprecated .gdb_index section in %s.\n\
2622 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2623 to use the section anyway."),
2624 filename);
2625 warning_printed = 1;
2626 }
2627 return 0;
2628 }
2629 /* Indexes with higher version than the one supported by GDB may be no
2630 longer backward compatible. */
2631 if (version > 7)
2632 return 0;
2633
2634 map->version = version;
2635 map->total_size = section->size;
2636
2637 metadata = (offset_type *) (addr + sizeof (offset_type));
2638
2639 i = 0;
2640 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2641 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2642 / 8);
2643 ++i;
2644
2645 *types_list = addr + MAYBE_SWAP (metadata[i]);
2646 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2647 - MAYBE_SWAP (metadata[i]))
2648 / 8);
2649 ++i;
2650
2651 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2652 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2653 - MAYBE_SWAP (metadata[i]));
2654 ++i;
2655
2656 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2657 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2658 - MAYBE_SWAP (metadata[i]))
2659 / (2 * sizeof (offset_type)));
2660 ++i;
2661
2662 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2663
2664 return 1;
2665 }
2666
2667
2668 /* Read the index file. If everything went ok, initialize the "quick"
2669 elements of all the CUs and return 1. Otherwise, return 0. */
2670
2671 static int
2672 dwarf2_read_index (struct objfile *objfile)
2673 {
2674 struct mapped_index local_map, *map;
2675 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2676 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2677
2678 if (!read_index_from_section (objfile, objfile->name,
2679 use_deprecated_index_sections,
2680 &dwarf2_per_objfile->gdb_index, &local_map,
2681 &cu_list, &cu_list_elements,
2682 &types_list, &types_list_elements))
2683 return 0;
2684
2685 /* Don't use the index if it's empty. */
2686 if (local_map.symbol_table_slots == 0)
2687 return 0;
2688
2689 /* If there is a .dwz file, read it so we can get its CU list as
2690 well. */
2691 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2692 {
2693 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2694 struct mapped_index dwz_map;
2695 const gdb_byte *dwz_types_ignore;
2696 offset_type dwz_types_elements_ignore;
2697
2698 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2699 1,
2700 &dwz->gdb_index, &dwz_map,
2701 &dwz_list, &dwz_list_elements,
2702 &dwz_types_ignore,
2703 &dwz_types_elements_ignore))
2704 {
2705 warning (_("could not read '.gdb_index' section from %s; skipping"),
2706 bfd_get_filename (dwz->dwz_bfd));
2707 return 0;
2708 }
2709 }
2710
2711 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2712 dwz_list, dwz_list_elements))
2713 return 0;
2714
2715 if (types_list_elements)
2716 {
2717 struct dwarf2_section_info *section;
2718
2719 /* We can only handle a single .debug_types when we have an
2720 index. */
2721 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2722 return 0;
2723
2724 section = VEC_index (dwarf2_section_info_def,
2725 dwarf2_per_objfile->types, 0);
2726
2727 if (!create_signatured_type_table_from_index (objfile, section,
2728 types_list,
2729 types_list_elements))
2730 return 0;
2731 }
2732
2733 create_addrmap_from_index (objfile, &local_map);
2734
2735 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2736 *map = local_map;
2737
2738 dwarf2_per_objfile->index_table = map;
2739 dwarf2_per_objfile->using_index = 1;
2740 dwarf2_per_objfile->quick_file_names_table =
2741 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2742
2743 return 1;
2744 }
2745
2746 /* A helper for the "quick" functions which sets the global
2747 dwarf2_per_objfile according to OBJFILE. */
2748
2749 static void
2750 dw2_setup (struct objfile *objfile)
2751 {
2752 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2753 gdb_assert (dwarf2_per_objfile);
2754 }
2755
2756 /* Reader function for dw2_build_type_unit_groups. */
2757
2758 static void
2759 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2760 gdb_byte *info_ptr,
2761 struct die_info *type_unit_die,
2762 int has_children,
2763 void *data)
2764 {
2765 struct dwarf2_cu *cu = reader->cu;
2766 struct attribute *attr;
2767 struct type_unit_group *tu_group;
2768
2769 gdb_assert (data == NULL);
2770
2771 if (! has_children)
2772 return;
2773
2774 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2775 /* Call this for its side-effect of creating the associated
2776 struct type_unit_group if it doesn't already exist. */
2777 tu_group = get_type_unit_group (cu, attr);
2778 }
2779
2780 /* Build dwarf2_per_objfile->type_unit_groups.
2781 This function may be called multiple times. */
2782
2783 static void
2784 dw2_build_type_unit_groups (void)
2785 {
2786 if (dwarf2_per_objfile->type_unit_groups == NULL)
2787 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2788 }
2789
2790 /* die_reader_func for dw2_get_file_names. */
2791
2792 static void
2793 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2794 gdb_byte *info_ptr,
2795 struct die_info *comp_unit_die,
2796 int has_children,
2797 void *data)
2798 {
2799 struct dwarf2_cu *cu = reader->cu;
2800 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2801 struct objfile *objfile = dwarf2_per_objfile->objfile;
2802 struct dwarf2_per_cu_data *lh_cu;
2803 struct line_header *lh;
2804 struct attribute *attr;
2805 int i;
2806 char *name, *comp_dir;
2807 void **slot;
2808 struct quick_file_names *qfn;
2809 unsigned int line_offset;
2810
2811 /* Our callers never want to match partial units -- instead they
2812 will match the enclosing full CU. */
2813 if (comp_unit_die->tag == DW_TAG_partial_unit)
2814 {
2815 this_cu->v.quick->no_file_data = 1;
2816 return;
2817 }
2818
2819 /* If we're reading the line header for TUs, store it in the "per_cu"
2820 for tu_group. */
2821 if (this_cu->is_debug_types)
2822 {
2823 struct type_unit_group *tu_group = data;
2824
2825 gdb_assert (tu_group != NULL);
2826 lh_cu = &tu_group->per_cu;
2827 }
2828 else
2829 lh_cu = this_cu;
2830
2831 lh = NULL;
2832 slot = NULL;
2833 line_offset = 0;
2834
2835 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2836 if (attr)
2837 {
2838 struct quick_file_names find_entry;
2839
2840 line_offset = DW_UNSND (attr);
2841
2842 /* We may have already read in this line header (TU line header sharing).
2843 If we have we're done. */
2844 find_entry.hash.dwo_unit = cu->dwo_unit;
2845 find_entry.hash.line_offset.sect_off = line_offset;
2846 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2847 &find_entry, INSERT);
2848 if (*slot != NULL)
2849 {
2850 lh_cu->v.quick->file_names = *slot;
2851 return;
2852 }
2853
2854 lh = dwarf_decode_line_header (line_offset, cu);
2855 }
2856 if (lh == NULL)
2857 {
2858 lh_cu->v.quick->no_file_data = 1;
2859 return;
2860 }
2861
2862 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2863 qfn->hash.dwo_unit = cu->dwo_unit;
2864 qfn->hash.line_offset.sect_off = line_offset;
2865 gdb_assert (slot != NULL);
2866 *slot = qfn;
2867
2868 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2869
2870 qfn->num_file_names = lh->num_file_names;
2871 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2872 lh->num_file_names * sizeof (char *));
2873 for (i = 0; i < lh->num_file_names; ++i)
2874 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2875 qfn->real_names = NULL;
2876
2877 free_line_header (lh);
2878
2879 lh_cu->v.quick->file_names = qfn;
2880 }
2881
2882 /* A helper for the "quick" functions which attempts to read the line
2883 table for THIS_CU. */
2884
2885 static struct quick_file_names *
2886 dw2_get_file_names (struct objfile *objfile,
2887 struct dwarf2_per_cu_data *this_cu)
2888 {
2889 /* For TUs this should only be called on the parent group. */
2890 if (this_cu->is_debug_types)
2891 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2892
2893 if (this_cu->v.quick->file_names != NULL)
2894 return this_cu->v.quick->file_names;
2895 /* If we know there is no line data, no point in looking again. */
2896 if (this_cu->v.quick->no_file_data)
2897 return NULL;
2898
2899 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2900 in the stub for CUs, there's is no need to lookup the DWO file.
2901 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2902 DWO file. */
2903 if (this_cu->is_debug_types)
2904 {
2905 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2906
2907 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2908 dw2_get_file_names_reader, tu_group);
2909 }
2910 else
2911 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2912
2913 if (this_cu->v.quick->no_file_data)
2914 return NULL;
2915 return this_cu->v.quick->file_names;
2916 }
2917
2918 /* A helper for the "quick" functions which computes and caches the
2919 real path for a given file name from the line table. */
2920
2921 static const char *
2922 dw2_get_real_path (struct objfile *objfile,
2923 struct quick_file_names *qfn, int index)
2924 {
2925 if (qfn->real_names == NULL)
2926 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2927 qfn->num_file_names, sizeof (char *));
2928
2929 if (qfn->real_names[index] == NULL)
2930 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2931
2932 return qfn->real_names[index];
2933 }
2934
2935 static struct symtab *
2936 dw2_find_last_source_symtab (struct objfile *objfile)
2937 {
2938 int index;
2939
2940 dw2_setup (objfile);
2941 index = dwarf2_per_objfile->n_comp_units - 1;
2942 return dw2_instantiate_symtab (dw2_get_cu (index));
2943 }
2944
2945 /* Traversal function for dw2_forget_cached_source_info. */
2946
2947 static int
2948 dw2_free_cached_file_names (void **slot, void *info)
2949 {
2950 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2951
2952 if (file_data->real_names)
2953 {
2954 int i;
2955
2956 for (i = 0; i < file_data->num_file_names; ++i)
2957 {
2958 xfree ((void*) file_data->real_names[i]);
2959 file_data->real_names[i] = NULL;
2960 }
2961 }
2962
2963 return 1;
2964 }
2965
2966 static void
2967 dw2_forget_cached_source_info (struct objfile *objfile)
2968 {
2969 dw2_setup (objfile);
2970
2971 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2972 dw2_free_cached_file_names, NULL);
2973 }
2974
2975 /* Helper function for dw2_map_symtabs_matching_filename that expands
2976 the symtabs and calls the iterator. */
2977
2978 static int
2979 dw2_map_expand_apply (struct objfile *objfile,
2980 struct dwarf2_per_cu_data *per_cu,
2981 const char *name,
2982 const char *full_path, const char *real_path,
2983 int (*callback) (struct symtab *, void *),
2984 void *data)
2985 {
2986 struct symtab *last_made = objfile->symtabs;
2987
2988 /* Don't visit already-expanded CUs. */
2989 if (per_cu->v.quick->symtab)
2990 return 0;
2991
2992 /* This may expand more than one symtab, and we want to iterate over
2993 all of them. */
2994 dw2_instantiate_symtab (per_cu);
2995
2996 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2997 objfile->symtabs, last_made);
2998 }
2999
3000 /* Implementation of the map_symtabs_matching_filename method. */
3001
3002 static int
3003 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3004 const char *full_path, const char *real_path,
3005 int (*callback) (struct symtab *, void *),
3006 void *data)
3007 {
3008 int i;
3009 const char *name_basename = lbasename (name);
3010 int name_len = strlen (name);
3011 int is_abs = IS_ABSOLUTE_PATH (name);
3012
3013 dw2_setup (objfile);
3014
3015 dw2_build_type_unit_groups ();
3016
3017 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3018 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3019 {
3020 int j;
3021 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3022 struct quick_file_names *file_data;
3023
3024 /* We only need to look at symtabs not already expanded. */
3025 if (per_cu->v.quick->symtab)
3026 continue;
3027
3028 file_data = dw2_get_file_names (objfile, per_cu);
3029 if (file_data == NULL)
3030 continue;
3031
3032 for (j = 0; j < file_data->num_file_names; ++j)
3033 {
3034 const char *this_name = file_data->file_names[j];
3035
3036 if (FILENAME_CMP (name, this_name) == 0
3037 || (!is_abs && compare_filenames_for_search (this_name,
3038 name, name_len)))
3039 {
3040 if (dw2_map_expand_apply (objfile, per_cu,
3041 name, full_path, real_path,
3042 callback, data))
3043 return 1;
3044 }
3045
3046 /* Before we invoke realpath, which can get expensive when many
3047 files are involved, do a quick comparison of the basenames. */
3048 if (! basenames_may_differ
3049 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3050 continue;
3051
3052 if (full_path != NULL)
3053 {
3054 const char *this_real_name = dw2_get_real_path (objfile,
3055 file_data, j);
3056
3057 if (this_real_name != NULL
3058 && (FILENAME_CMP (full_path, this_real_name) == 0
3059 || (!is_abs
3060 && compare_filenames_for_search (this_real_name,
3061 name, name_len))))
3062 {
3063 if (dw2_map_expand_apply (objfile, per_cu,
3064 name, full_path, real_path,
3065 callback, data))
3066 return 1;
3067 }
3068 }
3069
3070 if (real_path != NULL)
3071 {
3072 const char *this_real_name = dw2_get_real_path (objfile,
3073 file_data, j);
3074
3075 if (this_real_name != NULL
3076 && (FILENAME_CMP (real_path, this_real_name) == 0
3077 || (!is_abs
3078 && compare_filenames_for_search (this_real_name,
3079 name, name_len))))
3080 {
3081 if (dw2_map_expand_apply (objfile, per_cu,
3082 name, full_path, real_path,
3083 callback, data))
3084 return 1;
3085 }
3086 }
3087 }
3088 }
3089
3090 return 0;
3091 }
3092
3093 static struct symtab *
3094 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3095 const char *name, domain_enum domain)
3096 {
3097 /* We do all the work in the pre_expand_symtabs_matching hook
3098 instead. */
3099 return NULL;
3100 }
3101
3102 /* A helper function that expands all symtabs that hold an object
3103 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3104 symbols in block BLOCK_KIND. */
3105
3106 static void
3107 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3108 int want_specific_block,
3109 enum block_enum block_kind,
3110 const char *name, domain_enum domain)
3111 {
3112 struct mapped_index *index;
3113
3114 dw2_setup (objfile);
3115
3116 index = dwarf2_per_objfile->index_table;
3117
3118 /* index_table is NULL if OBJF_READNOW. */
3119 if (index)
3120 {
3121 offset_type *vec;
3122
3123 if (find_slot_in_mapped_hash (index, name, &vec))
3124 {
3125 offset_type i, len = MAYBE_SWAP (*vec);
3126 for (i = 0; i < len; ++i)
3127 {
3128 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3129 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3130 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3131 int want_static = block_kind != GLOBAL_BLOCK;
3132 /* This value is only valid for index versions >= 7. */
3133 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3134 gdb_index_symbol_kind symbol_kind =
3135 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3136 /* Only check the symbol attributes if they're present.
3137 Indices prior to version 7 don't record them,
3138 and indices >= 7 may elide them for certain symbols
3139 (gold does this). */
3140 int attrs_valid =
3141 (index->version >= 7
3142 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3143
3144 if (attrs_valid
3145 && want_specific_block
3146 && want_static != is_static)
3147 continue;
3148
3149 /* Only check the symbol's kind if it has one. */
3150 if (attrs_valid)
3151 {
3152 switch (domain)
3153 {
3154 case VAR_DOMAIN:
3155 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3156 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3157 /* Some types are also in VAR_DOMAIN. */
3158 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3159 continue;
3160 break;
3161 case STRUCT_DOMAIN:
3162 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3163 continue;
3164 break;
3165 case LABEL_DOMAIN:
3166 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3167 continue;
3168 break;
3169 default:
3170 break;
3171 }
3172 }
3173
3174 dw2_instantiate_symtab (per_cu);
3175 }
3176 }
3177 }
3178 }
3179
3180 static void
3181 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3182 enum block_enum block_kind, const char *name,
3183 domain_enum domain)
3184 {
3185 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3186 }
3187
3188 static void
3189 dw2_print_stats (struct objfile *objfile)
3190 {
3191 int i, count;
3192
3193 dw2_setup (objfile);
3194 count = 0;
3195 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3196 + dwarf2_per_objfile->n_type_units); ++i)
3197 {
3198 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3199
3200 if (!per_cu->v.quick->symtab)
3201 ++count;
3202 }
3203 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3204 }
3205
3206 static void
3207 dw2_dump (struct objfile *objfile)
3208 {
3209 /* Nothing worth printing. */
3210 }
3211
3212 static void
3213 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3214 struct section_offsets *delta)
3215 {
3216 /* There's nothing to relocate here. */
3217 }
3218
3219 static void
3220 dw2_expand_symtabs_for_function (struct objfile *objfile,
3221 const char *func_name)
3222 {
3223 /* Note: It doesn't matter what we pass for block_kind here. */
3224 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3225 VAR_DOMAIN);
3226 }
3227
3228 static void
3229 dw2_expand_all_symtabs (struct objfile *objfile)
3230 {
3231 int i;
3232
3233 dw2_setup (objfile);
3234
3235 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3236 + dwarf2_per_objfile->n_type_units); ++i)
3237 {
3238 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3239
3240 dw2_instantiate_symtab (per_cu);
3241 }
3242 }
3243
3244 static void
3245 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3246 const char *filename)
3247 {
3248 int i;
3249
3250 dw2_setup (objfile);
3251
3252 /* We don't need to consider type units here.
3253 This is only called for examining code, e.g. expand_line_sal.
3254 There can be an order of magnitude (or more) more type units
3255 than comp units, and we avoid them if we can. */
3256
3257 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3258 {
3259 int j;
3260 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3261 struct quick_file_names *file_data;
3262
3263 /* We only need to look at symtabs not already expanded. */
3264 if (per_cu->v.quick->symtab)
3265 continue;
3266
3267 file_data = dw2_get_file_names (objfile, per_cu);
3268 if (file_data == NULL)
3269 continue;
3270
3271 for (j = 0; j < file_data->num_file_names; ++j)
3272 {
3273 const char *this_name = file_data->file_names[j];
3274 if (FILENAME_CMP (this_name, filename) == 0)
3275 {
3276 dw2_instantiate_symtab (per_cu);
3277 break;
3278 }
3279 }
3280 }
3281 }
3282
3283 /* A helper function for dw2_find_symbol_file that finds the primary
3284 file name for a given CU. This is a die_reader_func. */
3285
3286 static void
3287 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3288 gdb_byte *info_ptr,
3289 struct die_info *comp_unit_die,
3290 int has_children,
3291 void *data)
3292 {
3293 const char **result_ptr = data;
3294 struct dwarf2_cu *cu = reader->cu;
3295 struct attribute *attr;
3296
3297 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3298 if (attr == NULL)
3299 *result_ptr = NULL;
3300 else
3301 *result_ptr = DW_STRING (attr);
3302 }
3303
3304 static const char *
3305 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3306 {
3307 struct dwarf2_per_cu_data *per_cu;
3308 offset_type *vec;
3309 struct quick_file_names *file_data;
3310 const char *filename;
3311
3312 dw2_setup (objfile);
3313
3314 /* index_table is NULL if OBJF_READNOW. */
3315 if (!dwarf2_per_objfile->index_table)
3316 {
3317 struct symtab *s;
3318
3319 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3320 {
3321 struct blockvector *bv = BLOCKVECTOR (s);
3322 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3323 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3324
3325 if (sym)
3326 return sym->symtab->filename;
3327 }
3328 return NULL;
3329 }
3330
3331 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3332 name, &vec))
3333 return NULL;
3334
3335 /* Note that this just looks at the very first one named NAME -- but
3336 actually we are looking for a function. find_main_filename
3337 should be rewritten so that it doesn't require a custom hook. It
3338 could just use the ordinary symbol tables. */
3339 /* vec[0] is the length, which must always be >0. */
3340 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3341
3342 if (per_cu->v.quick->symtab != NULL)
3343 return per_cu->v.quick->symtab->filename;
3344
3345 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3346 dw2_get_primary_filename_reader, &filename);
3347
3348 return filename;
3349 }
3350
3351 static void
3352 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3353 struct objfile *objfile, int global,
3354 int (*callback) (struct block *,
3355 struct symbol *, void *),
3356 void *data, symbol_compare_ftype *match,
3357 symbol_compare_ftype *ordered_compare)
3358 {
3359 /* Currently unimplemented; used for Ada. The function can be called if the
3360 current language is Ada for a non-Ada objfile using GNU index. As Ada
3361 does not look for non-Ada symbols this function should just return. */
3362 }
3363
3364 static void
3365 dw2_expand_symtabs_matching
3366 (struct objfile *objfile,
3367 int (*file_matcher) (const char *, void *),
3368 int (*name_matcher) (const char *, void *),
3369 enum search_domain kind,
3370 void *data)
3371 {
3372 int i;
3373 offset_type iter;
3374 struct mapped_index *index;
3375
3376 dw2_setup (objfile);
3377
3378 /* index_table is NULL if OBJF_READNOW. */
3379 if (!dwarf2_per_objfile->index_table)
3380 return;
3381 index = dwarf2_per_objfile->index_table;
3382
3383 if (file_matcher != NULL)
3384 {
3385 struct cleanup *cleanup;
3386 htab_t visited_found, visited_not_found;
3387
3388 dw2_build_type_unit_groups ();
3389
3390 visited_found = htab_create_alloc (10,
3391 htab_hash_pointer, htab_eq_pointer,
3392 NULL, xcalloc, xfree);
3393 cleanup = make_cleanup_htab_delete (visited_found);
3394 visited_not_found = htab_create_alloc (10,
3395 htab_hash_pointer, htab_eq_pointer,
3396 NULL, xcalloc, xfree);
3397 make_cleanup_htab_delete (visited_not_found);
3398
3399 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3400 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3401 {
3402 int j;
3403 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3404 struct quick_file_names *file_data;
3405 void **slot;
3406
3407 per_cu->v.quick->mark = 0;
3408
3409 /* We only need to look at symtabs not already expanded. */
3410 if (per_cu->v.quick->symtab)
3411 continue;
3412
3413 file_data = dw2_get_file_names (objfile, per_cu);
3414 if (file_data == NULL)
3415 continue;
3416
3417 if (htab_find (visited_not_found, file_data) != NULL)
3418 continue;
3419 else if (htab_find (visited_found, file_data) != NULL)
3420 {
3421 per_cu->v.quick->mark = 1;
3422 continue;
3423 }
3424
3425 for (j = 0; j < file_data->num_file_names; ++j)
3426 {
3427 if (file_matcher (file_data->file_names[j], data))
3428 {
3429 per_cu->v.quick->mark = 1;
3430 break;
3431 }
3432 }
3433
3434 slot = htab_find_slot (per_cu->v.quick->mark
3435 ? visited_found
3436 : visited_not_found,
3437 file_data, INSERT);
3438 *slot = file_data;
3439 }
3440
3441 do_cleanups (cleanup);
3442 }
3443
3444 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3445 {
3446 offset_type idx = 2 * iter;
3447 const char *name;
3448 offset_type *vec, vec_len, vec_idx;
3449
3450 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3451 continue;
3452
3453 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3454
3455 if (! (*name_matcher) (name, data))
3456 continue;
3457
3458 /* The name was matched, now expand corresponding CUs that were
3459 marked. */
3460 vec = (offset_type *) (index->constant_pool
3461 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3462 vec_len = MAYBE_SWAP (vec[0]);
3463 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3464 {
3465 struct dwarf2_per_cu_data *per_cu;
3466 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3467 gdb_index_symbol_kind symbol_kind =
3468 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3469 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3470
3471 /* Don't crash on bad data. */
3472 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3473 + dwarf2_per_objfile->n_type_units))
3474 continue;
3475
3476 /* Only check the symbol's kind if it has one.
3477 Indices prior to version 7 don't record it. */
3478 if (index->version >= 7)
3479 {
3480 switch (kind)
3481 {
3482 case VARIABLES_DOMAIN:
3483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3484 continue;
3485 break;
3486 case FUNCTIONS_DOMAIN:
3487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3488 continue;
3489 break;
3490 case TYPES_DOMAIN:
3491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3492 continue;
3493 break;
3494 default:
3495 break;
3496 }
3497 }
3498
3499 per_cu = dw2_get_cu (cu_index);
3500 if (file_matcher == NULL || per_cu->v.quick->mark)
3501 dw2_instantiate_symtab (per_cu);
3502 }
3503 }
3504 }
3505
3506 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3507 symtab. */
3508
3509 static struct symtab *
3510 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3511 {
3512 int i;
3513
3514 if (BLOCKVECTOR (symtab) != NULL
3515 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3516 return symtab;
3517
3518 if (symtab->includes == NULL)
3519 return NULL;
3520
3521 for (i = 0; symtab->includes[i]; ++i)
3522 {
3523 struct symtab *s = symtab->includes[i];
3524
3525 s = recursively_find_pc_sect_symtab (s, pc);
3526 if (s != NULL)
3527 return s;
3528 }
3529
3530 return NULL;
3531 }
3532
3533 static struct symtab *
3534 dw2_find_pc_sect_symtab (struct objfile *objfile,
3535 struct minimal_symbol *msymbol,
3536 CORE_ADDR pc,
3537 struct obj_section *section,
3538 int warn_if_readin)
3539 {
3540 struct dwarf2_per_cu_data *data;
3541 struct symtab *result;
3542
3543 dw2_setup (objfile);
3544
3545 if (!objfile->psymtabs_addrmap)
3546 return NULL;
3547
3548 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3549 if (!data)
3550 return NULL;
3551
3552 if (warn_if_readin && data->v.quick->symtab)
3553 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3554 paddress (get_objfile_arch (objfile), pc));
3555
3556 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3557 gdb_assert (result != NULL);
3558 return result;
3559 }
3560
3561 static void
3562 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3563 void *data, int need_fullname)
3564 {
3565 int i;
3566 struct cleanup *cleanup;
3567 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3568 NULL, xcalloc, xfree);
3569
3570 cleanup = make_cleanup_htab_delete (visited);
3571 dw2_setup (objfile);
3572
3573 dw2_build_type_unit_groups ();
3574
3575 /* We can ignore file names coming from already-expanded CUs. */
3576 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3577 + dwarf2_per_objfile->n_type_units); ++i)
3578 {
3579 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3580
3581 if (per_cu->v.quick->symtab)
3582 {
3583 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3584 INSERT);
3585
3586 *slot = per_cu->v.quick->file_names;
3587 }
3588 }
3589
3590 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3591 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3592 {
3593 int j;
3594 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3595 struct quick_file_names *file_data;
3596 void **slot;
3597
3598 /* We only need to look at symtabs not already expanded. */
3599 if (per_cu->v.quick->symtab)
3600 continue;
3601
3602 file_data = dw2_get_file_names (objfile, per_cu);
3603 if (file_data == NULL)
3604 continue;
3605
3606 slot = htab_find_slot (visited, file_data, INSERT);
3607 if (*slot)
3608 {
3609 /* Already visited. */
3610 continue;
3611 }
3612 *slot = file_data;
3613
3614 for (j = 0; j < file_data->num_file_names; ++j)
3615 {
3616 const char *this_real_name;
3617
3618 if (need_fullname)
3619 this_real_name = dw2_get_real_path (objfile, file_data, j);
3620 else
3621 this_real_name = NULL;
3622 (*fun) (file_data->file_names[j], this_real_name, data);
3623 }
3624 }
3625
3626 do_cleanups (cleanup);
3627 }
3628
3629 static int
3630 dw2_has_symbols (struct objfile *objfile)
3631 {
3632 return 1;
3633 }
3634
3635 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3636 {
3637 dw2_has_symbols,
3638 dw2_find_last_source_symtab,
3639 dw2_forget_cached_source_info,
3640 dw2_map_symtabs_matching_filename,
3641 dw2_lookup_symbol,
3642 dw2_pre_expand_symtabs_matching,
3643 dw2_print_stats,
3644 dw2_dump,
3645 dw2_relocate,
3646 dw2_expand_symtabs_for_function,
3647 dw2_expand_all_symtabs,
3648 dw2_expand_symtabs_with_filename,
3649 dw2_find_symbol_file,
3650 dw2_map_matching_symbols,
3651 dw2_expand_symtabs_matching,
3652 dw2_find_pc_sect_symtab,
3653 dw2_map_symbol_filenames
3654 };
3655
3656 /* Initialize for reading DWARF for this objfile. Return 0 if this
3657 file will use psymtabs, or 1 if using the GNU index. */
3658
3659 int
3660 dwarf2_initialize_objfile (struct objfile *objfile)
3661 {
3662 /* If we're about to read full symbols, don't bother with the
3663 indices. In this case we also don't care if some other debug
3664 format is making psymtabs, because they are all about to be
3665 expanded anyway. */
3666 if ((objfile->flags & OBJF_READNOW))
3667 {
3668 int i;
3669
3670 dwarf2_per_objfile->using_index = 1;
3671 create_all_comp_units (objfile);
3672 create_all_type_units (objfile);
3673 dwarf2_per_objfile->quick_file_names_table =
3674 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3675
3676 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3677 + dwarf2_per_objfile->n_type_units); ++i)
3678 {
3679 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3680
3681 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3682 struct dwarf2_per_cu_quick_data);
3683 }
3684
3685 /* Return 1 so that gdb sees the "quick" functions. However,
3686 these functions will be no-ops because we will have expanded
3687 all symtabs. */
3688 return 1;
3689 }
3690
3691 if (dwarf2_read_index (objfile))
3692 return 1;
3693
3694 return 0;
3695 }
3696
3697 \f
3698
3699 /* Build a partial symbol table. */
3700
3701 void
3702 dwarf2_build_psymtabs (struct objfile *objfile)
3703 {
3704 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3705 {
3706 init_psymbol_list (objfile, 1024);
3707 }
3708
3709 dwarf2_build_psymtabs_hard (objfile);
3710 }
3711
3712 /* Return the total length of the CU described by HEADER. */
3713
3714 static unsigned int
3715 get_cu_length (const struct comp_unit_head *header)
3716 {
3717 return header->initial_length_size + header->length;
3718 }
3719
3720 /* Return TRUE if OFFSET is within CU_HEADER. */
3721
3722 static inline int
3723 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3724 {
3725 sect_offset bottom = { cu_header->offset.sect_off };
3726 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3727
3728 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3729 }
3730
3731 /* Find the base address of the compilation unit for range lists and
3732 location lists. It will normally be specified by DW_AT_low_pc.
3733 In DWARF-3 draft 4, the base address could be overridden by
3734 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3735 compilation units with discontinuous ranges. */
3736
3737 static void
3738 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3739 {
3740 struct attribute *attr;
3741
3742 cu->base_known = 0;
3743 cu->base_address = 0;
3744
3745 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3746 if (attr)
3747 {
3748 cu->base_address = DW_ADDR (attr);
3749 cu->base_known = 1;
3750 }
3751 else
3752 {
3753 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3754 if (attr)
3755 {
3756 cu->base_address = DW_ADDR (attr);
3757 cu->base_known = 1;
3758 }
3759 }
3760 }
3761
3762 /* Read in the comp unit header information from the debug_info at info_ptr.
3763 NOTE: This leaves members offset, first_die_offset to be filled in
3764 by the caller. */
3765
3766 static gdb_byte *
3767 read_comp_unit_head (struct comp_unit_head *cu_header,
3768 gdb_byte *info_ptr, bfd *abfd)
3769 {
3770 int signed_addr;
3771 unsigned int bytes_read;
3772
3773 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3774 cu_header->initial_length_size = bytes_read;
3775 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3776 info_ptr += bytes_read;
3777 cu_header->version = read_2_bytes (abfd, info_ptr);
3778 info_ptr += 2;
3779 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3780 &bytes_read);
3781 info_ptr += bytes_read;
3782 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3783 info_ptr += 1;
3784 signed_addr = bfd_get_sign_extend_vma (abfd);
3785 if (signed_addr < 0)
3786 internal_error (__FILE__, __LINE__,
3787 _("read_comp_unit_head: dwarf from non elf file"));
3788 cu_header->signed_addr_p = signed_addr;
3789
3790 return info_ptr;
3791 }
3792
3793 /* Helper function that returns the proper abbrev section for
3794 THIS_CU. */
3795
3796 static struct dwarf2_section_info *
3797 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3798 {
3799 struct dwarf2_section_info *abbrev;
3800
3801 if (this_cu->is_dwz)
3802 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3803 else
3804 abbrev = &dwarf2_per_objfile->abbrev;
3805
3806 return abbrev;
3807 }
3808
3809 /* Subroutine of read_and_check_comp_unit_head and
3810 read_and_check_type_unit_head to simplify them.
3811 Perform various error checking on the header. */
3812
3813 static void
3814 error_check_comp_unit_head (struct comp_unit_head *header,
3815 struct dwarf2_section_info *section,
3816 struct dwarf2_section_info *abbrev_section)
3817 {
3818 bfd *abfd = section->asection->owner;
3819 const char *filename = bfd_get_filename (abfd);
3820
3821 if (header->version != 2 && header->version != 3 && header->version != 4)
3822 error (_("Dwarf Error: wrong version in compilation unit header "
3823 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3824 filename);
3825
3826 if (header->abbrev_offset.sect_off
3827 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3828 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3829 "(offset 0x%lx + 6) [in module %s]"),
3830 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3831 filename);
3832
3833 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3834 avoid potential 32-bit overflow. */
3835 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3836 > section->size)
3837 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3838 "(offset 0x%lx + 0) [in module %s]"),
3839 (long) header->length, (long) header->offset.sect_off,
3840 filename);
3841 }
3842
3843 /* Read in a CU/TU header and perform some basic error checking.
3844 The contents of the header are stored in HEADER.
3845 The result is a pointer to the start of the first DIE. */
3846
3847 static gdb_byte *
3848 read_and_check_comp_unit_head (struct comp_unit_head *header,
3849 struct dwarf2_section_info *section,
3850 struct dwarf2_section_info *abbrev_section,
3851 gdb_byte *info_ptr,
3852 int is_debug_types_section)
3853 {
3854 gdb_byte *beg_of_comp_unit = info_ptr;
3855 bfd *abfd = section->asection->owner;
3856
3857 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3858
3859 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3860
3861 /* If we're reading a type unit, skip over the signature and
3862 type_offset fields. */
3863 if (is_debug_types_section)
3864 info_ptr += 8 /*signature*/ + header->offset_size;
3865
3866 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3867
3868 error_check_comp_unit_head (header, section, abbrev_section);
3869
3870 return info_ptr;
3871 }
3872
3873 /* Read in the types comp unit header information from .debug_types entry at
3874 types_ptr. The result is a pointer to one past the end of the header. */
3875
3876 static gdb_byte *
3877 read_and_check_type_unit_head (struct comp_unit_head *header,
3878 struct dwarf2_section_info *section,
3879 struct dwarf2_section_info *abbrev_section,
3880 gdb_byte *info_ptr,
3881 ULONGEST *signature,
3882 cu_offset *type_offset_in_tu)
3883 {
3884 gdb_byte *beg_of_comp_unit = info_ptr;
3885 bfd *abfd = section->asection->owner;
3886
3887 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3888
3889 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3890
3891 /* If we're reading a type unit, skip over the signature and
3892 type_offset fields. */
3893 if (signature != NULL)
3894 *signature = read_8_bytes (abfd, info_ptr);
3895 info_ptr += 8;
3896 if (type_offset_in_tu != NULL)
3897 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3898 header->offset_size);
3899 info_ptr += header->offset_size;
3900
3901 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3902
3903 error_check_comp_unit_head (header, section, abbrev_section);
3904
3905 return info_ptr;
3906 }
3907
3908 /* Fetch the abbreviation table offset from a comp or type unit header. */
3909
3910 static sect_offset
3911 read_abbrev_offset (struct dwarf2_section_info *section,
3912 sect_offset offset)
3913 {
3914 bfd *abfd = section->asection->owner;
3915 gdb_byte *info_ptr;
3916 unsigned int length, initial_length_size, offset_size;
3917 sect_offset abbrev_offset;
3918
3919 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3920 info_ptr = section->buffer + offset.sect_off;
3921 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3922 offset_size = initial_length_size == 4 ? 4 : 8;
3923 info_ptr += initial_length_size + 2 /*version*/;
3924 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3925 return abbrev_offset;
3926 }
3927
3928 /* Allocate a new partial symtab for file named NAME and mark this new
3929 partial symtab as being an include of PST. */
3930
3931 static void
3932 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3933 struct objfile *objfile)
3934 {
3935 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3936
3937 subpst->section_offsets = pst->section_offsets;
3938 subpst->textlow = 0;
3939 subpst->texthigh = 0;
3940
3941 subpst->dependencies = (struct partial_symtab **)
3942 obstack_alloc (&objfile->objfile_obstack,
3943 sizeof (struct partial_symtab *));
3944 subpst->dependencies[0] = pst;
3945 subpst->number_of_dependencies = 1;
3946
3947 subpst->globals_offset = 0;
3948 subpst->n_global_syms = 0;
3949 subpst->statics_offset = 0;
3950 subpst->n_static_syms = 0;
3951 subpst->symtab = NULL;
3952 subpst->read_symtab = pst->read_symtab;
3953 subpst->readin = 0;
3954
3955 /* No private part is necessary for include psymtabs. This property
3956 can be used to differentiate between such include psymtabs and
3957 the regular ones. */
3958 subpst->read_symtab_private = NULL;
3959 }
3960
3961 /* Read the Line Number Program data and extract the list of files
3962 included by the source file represented by PST. Build an include
3963 partial symtab for each of these included files. */
3964
3965 static void
3966 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3967 struct die_info *die,
3968 struct partial_symtab *pst)
3969 {
3970 struct line_header *lh = NULL;
3971 struct attribute *attr;
3972
3973 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3974 if (attr)
3975 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3976 if (lh == NULL)
3977 return; /* No linetable, so no includes. */
3978
3979 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3980 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3981
3982 free_line_header (lh);
3983 }
3984
3985 static hashval_t
3986 hash_signatured_type (const void *item)
3987 {
3988 const struct signatured_type *sig_type = item;
3989
3990 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3991 return sig_type->signature;
3992 }
3993
3994 static int
3995 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3996 {
3997 const struct signatured_type *lhs = item_lhs;
3998 const struct signatured_type *rhs = item_rhs;
3999
4000 return lhs->signature == rhs->signature;
4001 }
4002
4003 /* Allocate a hash table for signatured types. */
4004
4005 static htab_t
4006 allocate_signatured_type_table (struct objfile *objfile)
4007 {
4008 return htab_create_alloc_ex (41,
4009 hash_signatured_type,
4010 eq_signatured_type,
4011 NULL,
4012 &objfile->objfile_obstack,
4013 hashtab_obstack_allocate,
4014 dummy_obstack_deallocate);
4015 }
4016
4017 /* A helper function to add a signatured type CU to a table. */
4018
4019 static int
4020 add_signatured_type_cu_to_table (void **slot, void *datum)
4021 {
4022 struct signatured_type *sigt = *slot;
4023 struct signatured_type ***datap = datum;
4024
4025 **datap = sigt;
4026 ++*datap;
4027
4028 return 1;
4029 }
4030
4031 /* Create the hash table of all entries in the .debug_types section.
4032 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4033 The result is a pointer to the hash table or NULL if there are
4034 no types. */
4035
4036 static htab_t
4037 create_debug_types_hash_table (struct dwo_file *dwo_file,
4038 VEC (dwarf2_section_info_def) *types)
4039 {
4040 struct objfile *objfile = dwarf2_per_objfile->objfile;
4041 htab_t types_htab = NULL;
4042 int ix;
4043 struct dwarf2_section_info *section;
4044 struct dwarf2_section_info *abbrev_section;
4045
4046 if (VEC_empty (dwarf2_section_info_def, types))
4047 return NULL;
4048
4049 abbrev_section = (dwo_file != NULL
4050 ? &dwo_file->sections.abbrev
4051 : &dwarf2_per_objfile->abbrev);
4052
4053 if (dwarf2_read_debug)
4054 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4055 dwo_file ? ".dwo" : "",
4056 bfd_get_filename (abbrev_section->asection->owner));
4057
4058 for (ix = 0;
4059 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4060 ++ix)
4061 {
4062 bfd *abfd;
4063 gdb_byte *info_ptr, *end_ptr;
4064 struct dwarf2_section_info *abbrev_section;
4065
4066 dwarf2_read_section (objfile, section);
4067 info_ptr = section->buffer;
4068
4069 if (info_ptr == NULL)
4070 continue;
4071
4072 /* We can't set abfd until now because the section may be empty or
4073 not present, in which case section->asection will be NULL. */
4074 abfd = section->asection->owner;
4075
4076 if (dwo_file)
4077 abbrev_section = &dwo_file->sections.abbrev;
4078 else
4079 abbrev_section = &dwarf2_per_objfile->abbrev;
4080
4081 if (types_htab == NULL)
4082 {
4083 if (dwo_file)
4084 types_htab = allocate_dwo_unit_table (objfile);
4085 else
4086 types_htab = allocate_signatured_type_table (objfile);
4087 }
4088
4089 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4090 because we don't need to read any dies: the signature is in the
4091 header. */
4092
4093 end_ptr = info_ptr + section->size;
4094 while (info_ptr < end_ptr)
4095 {
4096 sect_offset offset;
4097 cu_offset type_offset_in_tu;
4098 ULONGEST signature;
4099 struct signatured_type *sig_type;
4100 struct dwo_unit *dwo_tu;
4101 void **slot;
4102 gdb_byte *ptr = info_ptr;
4103 struct comp_unit_head header;
4104 unsigned int length;
4105
4106 offset.sect_off = ptr - section->buffer;
4107
4108 /* We need to read the type's signature in order to build the hash
4109 table, but we don't need anything else just yet. */
4110
4111 ptr = read_and_check_type_unit_head (&header, section,
4112 abbrev_section, ptr,
4113 &signature, &type_offset_in_tu);
4114
4115 length = get_cu_length (&header);
4116
4117 /* Skip dummy type units. */
4118 if (ptr >= info_ptr + length
4119 || peek_abbrev_code (abfd, ptr) == 0)
4120 {
4121 info_ptr += length;
4122 continue;
4123 }
4124
4125 if (dwo_file)
4126 {
4127 sig_type = NULL;
4128 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4129 struct dwo_unit);
4130 dwo_tu->dwo_file = dwo_file;
4131 dwo_tu->signature = signature;
4132 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4133 dwo_tu->info_or_types_section = section;
4134 dwo_tu->offset = offset;
4135 dwo_tu->length = length;
4136 }
4137 else
4138 {
4139 /* N.B.: type_offset is not usable if this type uses a DWO file.
4140 The real type_offset is in the DWO file. */
4141 dwo_tu = NULL;
4142 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4143 struct signatured_type);
4144 sig_type->signature = signature;
4145 sig_type->type_offset_in_tu = type_offset_in_tu;
4146 sig_type->per_cu.objfile = objfile;
4147 sig_type->per_cu.is_debug_types = 1;
4148 sig_type->per_cu.info_or_types_section = section;
4149 sig_type->per_cu.offset = offset;
4150 sig_type->per_cu.length = length;
4151 }
4152
4153 slot = htab_find_slot (types_htab,
4154 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4155 INSERT);
4156 gdb_assert (slot != NULL);
4157 if (*slot != NULL)
4158 {
4159 sect_offset dup_offset;
4160
4161 if (dwo_file)
4162 {
4163 const struct dwo_unit *dup_tu = *slot;
4164
4165 dup_offset = dup_tu->offset;
4166 }
4167 else
4168 {
4169 const struct signatured_type *dup_tu = *slot;
4170
4171 dup_offset = dup_tu->per_cu.offset;
4172 }
4173
4174 complaint (&symfile_complaints,
4175 _("debug type entry at offset 0x%x is duplicate to the "
4176 "entry at offset 0x%x, signature 0x%s"),
4177 offset.sect_off, dup_offset.sect_off,
4178 phex (signature, sizeof (signature)));
4179 }
4180 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4181
4182 if (dwarf2_read_debug)
4183 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4184 offset.sect_off,
4185 phex (signature, sizeof (signature)));
4186
4187 info_ptr += length;
4188 }
4189 }
4190
4191 return types_htab;
4192 }
4193
4194 /* Create the hash table of all entries in the .debug_types section,
4195 and initialize all_type_units.
4196 The result is zero if there is an error (e.g. missing .debug_types section),
4197 otherwise non-zero. */
4198
4199 static int
4200 create_all_type_units (struct objfile *objfile)
4201 {
4202 htab_t types_htab;
4203 struct signatured_type **iter;
4204
4205 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4206 if (types_htab == NULL)
4207 {
4208 dwarf2_per_objfile->signatured_types = NULL;
4209 return 0;
4210 }
4211
4212 dwarf2_per_objfile->signatured_types = types_htab;
4213
4214 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4215 dwarf2_per_objfile->all_type_units
4216 = obstack_alloc (&objfile->objfile_obstack,
4217 dwarf2_per_objfile->n_type_units
4218 * sizeof (struct signatured_type *));
4219 iter = &dwarf2_per_objfile->all_type_units[0];
4220 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4221 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4222 == dwarf2_per_objfile->n_type_units);
4223
4224 return 1;
4225 }
4226
4227 /* Lookup a signature based type for DW_FORM_ref_sig8.
4228 Returns NULL if signature SIG is not present in the table. */
4229
4230 static struct signatured_type *
4231 lookup_signatured_type (ULONGEST sig)
4232 {
4233 struct signatured_type find_entry, *entry;
4234
4235 if (dwarf2_per_objfile->signatured_types == NULL)
4236 {
4237 complaint (&symfile_complaints,
4238 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4239 return NULL;
4240 }
4241
4242 find_entry.signature = sig;
4243 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4244 return entry;
4245 }
4246 \f
4247 /* Low level DIE reading support. */
4248
4249 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4250
4251 static void
4252 init_cu_die_reader (struct die_reader_specs *reader,
4253 struct dwarf2_cu *cu,
4254 struct dwarf2_section_info *section,
4255 struct dwo_file *dwo_file)
4256 {
4257 gdb_assert (section->readin && section->buffer != NULL);
4258 reader->abfd = section->asection->owner;
4259 reader->cu = cu;
4260 reader->dwo_file = dwo_file;
4261 reader->die_section = section;
4262 reader->buffer = section->buffer;
4263 reader->buffer_end = section->buffer + section->size;
4264 }
4265
4266 /* Initialize a CU (or TU) and read its DIEs.
4267 If the CU defers to a DWO file, read the DWO file as well.
4268
4269 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4270 Otherwise the table specified in the comp unit header is read in and used.
4271 This is an optimization for when we already have the abbrev table.
4272
4273 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4274 Otherwise, a new CU is allocated with xmalloc.
4275
4276 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4277 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4278
4279 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4280 linker) then DIE_READER_FUNC will not get called. */
4281
4282 static void
4283 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4284 struct abbrev_table *abbrev_table,
4285 int use_existing_cu, int keep,
4286 die_reader_func_ftype *die_reader_func,
4287 void *data)
4288 {
4289 struct objfile *objfile = dwarf2_per_objfile->objfile;
4290 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4291 bfd *abfd = section->asection->owner;
4292 struct dwarf2_cu *cu;
4293 gdb_byte *begin_info_ptr, *info_ptr;
4294 struct die_reader_specs reader;
4295 struct die_info *comp_unit_die;
4296 int has_children;
4297 struct attribute *attr;
4298 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4299 struct signatured_type *sig_type = NULL;
4300 struct dwarf2_section_info *abbrev_section;
4301 /* Non-zero if CU currently points to a DWO file and we need to
4302 reread it. When this happens we need to reread the skeleton die
4303 before we can reread the DWO file. */
4304 int rereading_dwo_cu = 0;
4305
4306 if (dwarf2_die_debug)
4307 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4308 this_cu->is_debug_types ? "type" : "comp",
4309 this_cu->offset.sect_off);
4310
4311 if (use_existing_cu)
4312 gdb_assert (keep);
4313
4314 cleanups = make_cleanup (null_cleanup, NULL);
4315
4316 /* This is cheap if the section is already read in. */
4317 dwarf2_read_section (objfile, section);
4318
4319 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4320
4321 abbrev_section = get_abbrev_section_for_cu (this_cu);
4322
4323 if (use_existing_cu && this_cu->cu != NULL)
4324 {
4325 cu = this_cu->cu;
4326
4327 /* If this CU is from a DWO file we need to start over, we need to
4328 refetch the attributes from the skeleton CU.
4329 This could be optimized by retrieving those attributes from when we
4330 were here the first time: the previous comp_unit_die was stored in
4331 comp_unit_obstack. But there's no data yet that we need this
4332 optimization. */
4333 if (cu->dwo_unit != NULL)
4334 rereading_dwo_cu = 1;
4335 }
4336 else
4337 {
4338 /* If !use_existing_cu, this_cu->cu must be NULL. */
4339 gdb_assert (this_cu->cu == NULL);
4340
4341 cu = xmalloc (sizeof (*cu));
4342 init_one_comp_unit (cu, this_cu);
4343
4344 /* If an error occurs while loading, release our storage. */
4345 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4346 }
4347
4348 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4349 {
4350 /* We already have the header, there's no need to read it in again. */
4351 info_ptr += cu->header.first_die_offset.cu_off;
4352 }
4353 else
4354 {
4355 if (this_cu->is_debug_types)
4356 {
4357 ULONGEST signature;
4358 cu_offset type_offset_in_tu;
4359
4360 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4361 abbrev_section, info_ptr,
4362 &signature,
4363 &type_offset_in_tu);
4364
4365 /* Since per_cu is the first member of struct signatured_type,
4366 we can go from a pointer to one to a pointer to the other. */
4367 sig_type = (struct signatured_type *) this_cu;
4368 gdb_assert (sig_type->signature == signature);
4369 gdb_assert (sig_type->type_offset_in_tu.cu_off
4370 == type_offset_in_tu.cu_off);
4371 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4372
4373 /* LENGTH has not been set yet for type units if we're
4374 using .gdb_index. */
4375 this_cu->length = get_cu_length (&cu->header);
4376
4377 /* Establish the type offset that can be used to lookup the type. */
4378 sig_type->type_offset_in_section.sect_off =
4379 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4380 }
4381 else
4382 {
4383 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4384 abbrev_section,
4385 info_ptr, 0);
4386
4387 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4388 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4389 }
4390 }
4391
4392 /* Skip dummy compilation units. */
4393 if (info_ptr >= begin_info_ptr + this_cu->length
4394 || peek_abbrev_code (abfd, info_ptr) == 0)
4395 {
4396 do_cleanups (cleanups);
4397 return;
4398 }
4399
4400 /* If we don't have them yet, read the abbrevs for this compilation unit.
4401 And if we need to read them now, make sure they're freed when we're
4402 done. Note that it's important that if the CU had an abbrev table
4403 on entry we don't free it when we're done: Somewhere up the call stack
4404 it may be in use. */
4405 if (abbrev_table != NULL)
4406 {
4407 gdb_assert (cu->abbrev_table == NULL);
4408 gdb_assert (cu->header.abbrev_offset.sect_off
4409 == abbrev_table->offset.sect_off);
4410 cu->abbrev_table = abbrev_table;
4411 }
4412 else if (cu->abbrev_table == NULL)
4413 {
4414 dwarf2_read_abbrevs (cu, abbrev_section);
4415 make_cleanup (dwarf2_free_abbrev_table, cu);
4416 }
4417 else if (rereading_dwo_cu)
4418 {
4419 dwarf2_free_abbrev_table (cu);
4420 dwarf2_read_abbrevs (cu, abbrev_section);
4421 }
4422
4423 /* Read the top level CU/TU die. */
4424 init_cu_die_reader (&reader, cu, section, NULL);
4425 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4426
4427 /* If we have a DWO stub, process it and then read in the DWO file.
4428 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4429 a DWO CU, that this test will fail. */
4430 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4431 if (attr)
4432 {
4433 char *dwo_name = DW_STRING (attr);
4434 const char *comp_dir_string;
4435 struct dwo_unit *dwo_unit;
4436 ULONGEST signature; /* Or dwo_id. */
4437 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4438 int i,num_extra_attrs;
4439 struct dwarf2_section_info *dwo_abbrev_section;
4440
4441 if (has_children)
4442 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4443 " has children (offset 0x%x) [in module %s]"),
4444 this_cu->offset.sect_off, bfd_get_filename (abfd));
4445
4446 /* These attributes aren't processed until later:
4447 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4448 However, the attribute is found in the stub which we won't have later.
4449 In order to not impose this complication on the rest of the code,
4450 we read them here and copy them to the DWO CU/TU die. */
4451
4452 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4453 DWO file. */
4454 stmt_list = NULL;
4455 if (! this_cu->is_debug_types)
4456 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4457 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4458 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4459 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4460 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4461
4462 /* There should be a DW_AT_addr_base attribute here (if needed).
4463 We need the value before we can process DW_FORM_GNU_addr_index. */
4464 cu->addr_base = 0;
4465 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4466 if (attr)
4467 cu->addr_base = DW_UNSND (attr);
4468
4469 /* There should be a DW_AT_ranges_base attribute here (if needed).
4470 We need the value before we can process DW_AT_ranges. */
4471 cu->ranges_base = 0;
4472 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4473 if (attr)
4474 cu->ranges_base = DW_UNSND (attr);
4475
4476 if (this_cu->is_debug_types)
4477 {
4478 gdb_assert (sig_type != NULL);
4479 signature = sig_type->signature;
4480 }
4481 else
4482 {
4483 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4484 if (! attr)
4485 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4486 dwo_name);
4487 signature = DW_UNSND (attr);
4488 }
4489
4490 /* We may need the comp_dir in order to find the DWO file. */
4491 comp_dir_string = NULL;
4492 if (comp_dir)
4493 comp_dir_string = DW_STRING (comp_dir);
4494
4495 if (this_cu->is_debug_types)
4496 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4497 else
4498 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4499 signature);
4500
4501 if (dwo_unit == NULL)
4502 {
4503 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4504 " with ID %s [in module %s]"),
4505 this_cu->offset.sect_off,
4506 phex (signature, sizeof (signature)),
4507 objfile->name);
4508 }
4509
4510 /* Set up for reading the DWO CU/TU. */
4511 cu->dwo_unit = dwo_unit;
4512 section = dwo_unit->info_or_types_section;
4513 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4514 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4515 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4516
4517 if (this_cu->is_debug_types)
4518 {
4519 ULONGEST signature;
4520
4521 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4522 dwo_abbrev_section,
4523 info_ptr,
4524 &signature, NULL);
4525 gdb_assert (sig_type->signature == signature);
4526 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4527 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4528
4529 /* Establish the type offset that can be used to lookup the type.
4530 For DWO files, we don't know it until now. */
4531 sig_type->type_offset_in_section.sect_off =
4532 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4533 }
4534 else
4535 {
4536 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4537 dwo_abbrev_section,
4538 info_ptr, 0);
4539 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4540 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4541 }
4542
4543 /* Discard the original CU's abbrev table, and read the DWO's. */
4544 if (abbrev_table == NULL)
4545 {
4546 dwarf2_free_abbrev_table (cu);
4547 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4548 }
4549 else
4550 {
4551 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4552 make_cleanup (dwarf2_free_abbrev_table, cu);
4553 }
4554
4555 /* Read in the die, but leave space to copy over the attributes
4556 from the stub. This has the benefit of simplifying the rest of
4557 the code - all the real work is done here. */
4558 num_extra_attrs = ((stmt_list != NULL)
4559 + (low_pc != NULL)
4560 + (high_pc != NULL)
4561 + (ranges != NULL)
4562 + (comp_dir != NULL));
4563 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4564 &has_children, num_extra_attrs);
4565
4566 /* Copy over the attributes from the stub to the DWO die. */
4567 i = comp_unit_die->num_attrs;
4568 if (stmt_list != NULL)
4569 comp_unit_die->attrs[i++] = *stmt_list;
4570 if (low_pc != NULL)
4571 comp_unit_die->attrs[i++] = *low_pc;
4572 if (high_pc != NULL)
4573 comp_unit_die->attrs[i++] = *high_pc;
4574 if (ranges != NULL)
4575 comp_unit_die->attrs[i++] = *ranges;
4576 if (comp_dir != NULL)
4577 comp_unit_die->attrs[i++] = *comp_dir;
4578 comp_unit_die->num_attrs += num_extra_attrs;
4579
4580 /* Skip dummy compilation units. */
4581 if (info_ptr >= begin_info_ptr + dwo_unit->length
4582 || peek_abbrev_code (abfd, info_ptr) == 0)
4583 {
4584 do_cleanups (cleanups);
4585 return;
4586 }
4587 }
4588
4589 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4590
4591 if (free_cu_cleanup != NULL)
4592 {
4593 if (keep)
4594 {
4595 /* We've successfully allocated this compilation unit. Let our
4596 caller clean it up when finished with it. */
4597 discard_cleanups (free_cu_cleanup);
4598
4599 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4600 So we have to manually free the abbrev table. */
4601 dwarf2_free_abbrev_table (cu);
4602
4603 /* Link this CU into read_in_chain. */
4604 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4605 dwarf2_per_objfile->read_in_chain = this_cu;
4606 }
4607 else
4608 do_cleanups (free_cu_cleanup);
4609 }
4610
4611 do_cleanups (cleanups);
4612 }
4613
4614 /* Read CU/TU THIS_CU in section SECTION,
4615 but do not follow DW_AT_GNU_dwo_name if present.
4616 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4617 have already done the lookup to find the DWO file).
4618
4619 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4620 THIS_CU->is_debug_types, but nothing else.
4621
4622 We fill in THIS_CU->length.
4623
4624 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4625 linker) then DIE_READER_FUNC will not get called.
4626
4627 THIS_CU->cu is always freed when done.
4628 This is done in order to not leave THIS_CU->cu in a state where we have
4629 to care whether it refers to the "main" CU or the DWO CU. */
4630
4631 static void
4632 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4633 struct dwarf2_section_info *abbrev_section,
4634 struct dwo_file *dwo_file,
4635 die_reader_func_ftype *die_reader_func,
4636 void *data)
4637 {
4638 struct objfile *objfile = dwarf2_per_objfile->objfile;
4639 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4640 bfd *abfd = section->asection->owner;
4641 struct dwarf2_cu cu;
4642 gdb_byte *begin_info_ptr, *info_ptr;
4643 struct die_reader_specs reader;
4644 struct cleanup *cleanups;
4645 struct die_info *comp_unit_die;
4646 int has_children;
4647
4648 if (dwarf2_die_debug)
4649 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4650 this_cu->is_debug_types ? "type" : "comp",
4651 this_cu->offset.sect_off);
4652
4653 gdb_assert (this_cu->cu == NULL);
4654
4655 /* This is cheap if the section is already read in. */
4656 dwarf2_read_section (objfile, section);
4657
4658 init_one_comp_unit (&cu, this_cu);
4659
4660 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4661
4662 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4663 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4664 abbrev_section, info_ptr,
4665 this_cu->is_debug_types);
4666
4667 this_cu->length = get_cu_length (&cu.header);
4668
4669 /* Skip dummy compilation units. */
4670 if (info_ptr >= begin_info_ptr + this_cu->length
4671 || peek_abbrev_code (abfd, info_ptr) == 0)
4672 {
4673 do_cleanups (cleanups);
4674 return;
4675 }
4676
4677 dwarf2_read_abbrevs (&cu, abbrev_section);
4678 make_cleanup (dwarf2_free_abbrev_table, &cu);
4679
4680 init_cu_die_reader (&reader, &cu, section, dwo_file);
4681 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4682
4683 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4684
4685 do_cleanups (cleanups);
4686 }
4687
4688 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4689 does not lookup the specified DWO file.
4690 This cannot be used to read DWO files.
4691
4692 THIS_CU->cu is always freed when done.
4693 This is done in order to not leave THIS_CU->cu in a state where we have
4694 to care whether it refers to the "main" CU or the DWO CU.
4695 We can revisit this if the data shows there's a performance issue. */
4696
4697 static void
4698 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4699 die_reader_func_ftype *die_reader_func,
4700 void *data)
4701 {
4702 init_cutu_and_read_dies_no_follow (this_cu,
4703 get_abbrev_section_for_cu (this_cu),
4704 NULL,
4705 die_reader_func, data);
4706 }
4707
4708 /* Create a psymtab named NAME and assign it to PER_CU.
4709
4710 The caller must fill in the following details:
4711 dirname, textlow, texthigh. */
4712
4713 static struct partial_symtab *
4714 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4715 {
4716 struct objfile *objfile = per_cu->objfile;
4717 struct partial_symtab *pst;
4718
4719 pst = start_psymtab_common (objfile, objfile->section_offsets,
4720 name, 0,
4721 objfile->global_psymbols.next,
4722 objfile->static_psymbols.next);
4723
4724 pst->psymtabs_addrmap_supported = 1;
4725
4726 /* This is the glue that links PST into GDB's symbol API. */
4727 pst->read_symtab_private = per_cu;
4728 pst->read_symtab = dwarf2_psymtab_to_symtab;
4729 per_cu->v.psymtab = pst;
4730
4731 return pst;
4732 }
4733
4734 /* die_reader_func for process_psymtab_comp_unit. */
4735
4736 static void
4737 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4738 gdb_byte *info_ptr,
4739 struct die_info *comp_unit_die,
4740 int has_children,
4741 void *data)
4742 {
4743 struct dwarf2_cu *cu = reader->cu;
4744 struct objfile *objfile = cu->objfile;
4745 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4746 struct attribute *attr;
4747 CORE_ADDR baseaddr;
4748 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4749 struct partial_symtab *pst;
4750 int has_pc_info;
4751 const char *filename;
4752 int *want_partial_unit_ptr = data;
4753
4754 if (comp_unit_die->tag == DW_TAG_partial_unit
4755 && (want_partial_unit_ptr == NULL
4756 || !*want_partial_unit_ptr))
4757 return;
4758
4759 gdb_assert (! per_cu->is_debug_types);
4760
4761 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4762
4763 cu->list_in_scope = &file_symbols;
4764
4765 /* Allocate a new partial symbol table structure. */
4766 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4767 if (attr == NULL || !DW_STRING (attr))
4768 filename = "";
4769 else
4770 filename = DW_STRING (attr);
4771
4772 pst = create_partial_symtab (per_cu, filename);
4773
4774 /* This must be done before calling dwarf2_build_include_psymtabs. */
4775 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4776 if (attr != NULL)
4777 pst->dirname = DW_STRING (attr);
4778
4779 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4780
4781 dwarf2_find_base_address (comp_unit_die, cu);
4782
4783 /* Possibly set the default values of LOWPC and HIGHPC from
4784 `DW_AT_ranges'. */
4785 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4786 &best_highpc, cu, pst);
4787 if (has_pc_info == 1 && best_lowpc < best_highpc)
4788 /* Store the contiguous range if it is not empty; it can be empty for
4789 CUs with no code. */
4790 addrmap_set_empty (objfile->psymtabs_addrmap,
4791 best_lowpc + baseaddr,
4792 best_highpc + baseaddr - 1, pst);
4793
4794 /* Check if comp unit has_children.
4795 If so, read the rest of the partial symbols from this comp unit.
4796 If not, there's no more debug_info for this comp unit. */
4797 if (has_children)
4798 {
4799 struct partial_die_info *first_die;
4800 CORE_ADDR lowpc, highpc;
4801
4802 lowpc = ((CORE_ADDR) -1);
4803 highpc = ((CORE_ADDR) 0);
4804
4805 first_die = load_partial_dies (reader, info_ptr, 1);
4806
4807 scan_partial_symbols (first_die, &lowpc, &highpc,
4808 ! has_pc_info, cu);
4809
4810 /* If we didn't find a lowpc, set it to highpc to avoid
4811 complaints from `maint check'. */
4812 if (lowpc == ((CORE_ADDR) -1))
4813 lowpc = highpc;
4814
4815 /* If the compilation unit didn't have an explicit address range,
4816 then use the information extracted from its child dies. */
4817 if (! has_pc_info)
4818 {
4819 best_lowpc = lowpc;
4820 best_highpc = highpc;
4821 }
4822 }
4823 pst->textlow = best_lowpc + baseaddr;
4824 pst->texthigh = best_highpc + baseaddr;
4825
4826 pst->n_global_syms = objfile->global_psymbols.next -
4827 (objfile->global_psymbols.list + pst->globals_offset);
4828 pst->n_static_syms = objfile->static_psymbols.next -
4829 (objfile->static_psymbols.list + pst->statics_offset);
4830 sort_pst_symbols (pst);
4831
4832 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4833 {
4834 int i;
4835 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4836 struct dwarf2_per_cu_data *iter;
4837
4838 /* Fill in 'dependencies' here; we fill in 'users' in a
4839 post-pass. */
4840 pst->number_of_dependencies = len;
4841 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4842 len * sizeof (struct symtab *));
4843 for (i = 0;
4844 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4845 i, iter);
4846 ++i)
4847 pst->dependencies[i] = iter->v.psymtab;
4848
4849 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4850 }
4851
4852 /* Get the list of files included in the current compilation unit,
4853 and build a psymtab for each of them. */
4854 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4855
4856 if (dwarf2_read_debug)
4857 {
4858 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4859
4860 fprintf_unfiltered (gdb_stdlog,
4861 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4862 ", %d global, %d static syms\n",
4863 per_cu->is_debug_types ? "type" : "comp",
4864 per_cu->offset.sect_off,
4865 paddress (gdbarch, pst->textlow),
4866 paddress (gdbarch, pst->texthigh),
4867 pst->n_global_syms, pst->n_static_syms);
4868 }
4869 }
4870
4871 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4872 Process compilation unit THIS_CU for a psymtab. */
4873
4874 static void
4875 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4876 int want_partial_unit)
4877 {
4878 /* If this compilation unit was already read in, free the
4879 cached copy in order to read it in again. This is
4880 necessary because we skipped some symbols when we first
4881 read in the compilation unit (see load_partial_dies).
4882 This problem could be avoided, but the benefit is unclear. */
4883 if (this_cu->cu != NULL)
4884 free_one_cached_comp_unit (this_cu);
4885
4886 gdb_assert (! this_cu->is_debug_types);
4887 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4888 process_psymtab_comp_unit_reader,
4889 &want_partial_unit);
4890
4891 /* Age out any secondary CUs. */
4892 age_cached_comp_units ();
4893 }
4894
4895 static hashval_t
4896 hash_type_unit_group (const void *item)
4897 {
4898 const struct type_unit_group *tu_group = item;
4899
4900 return hash_stmt_list_entry (&tu_group->hash);
4901 }
4902
4903 static int
4904 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4905 {
4906 const struct type_unit_group *lhs = item_lhs;
4907 const struct type_unit_group *rhs = item_rhs;
4908
4909 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4910 }
4911
4912 /* Allocate a hash table for type unit groups. */
4913
4914 static htab_t
4915 allocate_type_unit_groups_table (void)
4916 {
4917 return htab_create_alloc_ex (3,
4918 hash_type_unit_group,
4919 eq_type_unit_group,
4920 NULL,
4921 &dwarf2_per_objfile->objfile->objfile_obstack,
4922 hashtab_obstack_allocate,
4923 dummy_obstack_deallocate);
4924 }
4925
4926 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4927 partial symtabs. We combine several TUs per psymtab to not let the size
4928 of any one psymtab grow too big. */
4929 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4930 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4931
4932 /* Helper routine for get_type_unit_group.
4933 Create the type_unit_group object used to hold one or more TUs. */
4934
4935 static struct type_unit_group *
4936 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4937 {
4938 struct objfile *objfile = dwarf2_per_objfile->objfile;
4939 struct dwarf2_per_cu_data *per_cu;
4940 struct type_unit_group *tu_group;
4941
4942 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4943 struct type_unit_group);
4944 per_cu = &tu_group->per_cu;
4945 per_cu->objfile = objfile;
4946 per_cu->is_debug_types = 1;
4947 per_cu->s.type_unit_group = tu_group;
4948
4949 if (dwarf2_per_objfile->using_index)
4950 {
4951 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4952 struct dwarf2_per_cu_quick_data);
4953 tu_group->t.first_tu = cu->per_cu;
4954 }
4955 else
4956 {
4957 unsigned int line_offset = line_offset_struct.sect_off;
4958 struct partial_symtab *pst;
4959 char *name;
4960
4961 /* Give the symtab a useful name for debug purposes. */
4962 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4963 name = xstrprintf ("<type_units_%d>",
4964 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4965 else
4966 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4967
4968 pst = create_partial_symtab (per_cu, name);
4969 pst->anonymous = 1;
4970
4971 xfree (name);
4972 }
4973
4974 tu_group->hash.dwo_unit = cu->dwo_unit;
4975 tu_group->hash.line_offset = line_offset_struct;
4976
4977 return tu_group;
4978 }
4979
4980 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4981 STMT_LIST is a DW_AT_stmt_list attribute. */
4982
4983 static struct type_unit_group *
4984 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4985 {
4986 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4987 struct type_unit_group *tu_group;
4988 void **slot;
4989 unsigned int line_offset;
4990 struct type_unit_group type_unit_group_for_lookup;
4991
4992 if (dwarf2_per_objfile->type_unit_groups == NULL)
4993 {
4994 dwarf2_per_objfile->type_unit_groups =
4995 allocate_type_unit_groups_table ();
4996 }
4997
4998 /* Do we need to create a new group, or can we use an existing one? */
4999
5000 if (stmt_list)
5001 {
5002 line_offset = DW_UNSND (stmt_list);
5003 ++tu_stats->nr_symtab_sharers;
5004 }
5005 else
5006 {
5007 /* Ugh, no stmt_list. Rare, but we have to handle it.
5008 We can do various things here like create one group per TU or
5009 spread them over multiple groups to split up the expansion work.
5010 To avoid worst case scenarios (too many groups or too large groups)
5011 we, umm, group them in bunches. */
5012 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5013 | (tu_stats->nr_stmt_less_type_units
5014 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5015 ++tu_stats->nr_stmt_less_type_units;
5016 }
5017
5018 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5019 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5020 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5021 &type_unit_group_for_lookup, INSERT);
5022 if (*slot != NULL)
5023 {
5024 tu_group = *slot;
5025 gdb_assert (tu_group != NULL);
5026 }
5027 else
5028 {
5029 sect_offset line_offset_struct;
5030
5031 line_offset_struct.sect_off = line_offset;
5032 tu_group = create_type_unit_group (cu, line_offset_struct);
5033 *slot = tu_group;
5034 ++tu_stats->nr_symtabs;
5035 }
5036
5037 return tu_group;
5038 }
5039
5040 /* Struct used to sort TUs by their abbreviation table offset. */
5041
5042 struct tu_abbrev_offset
5043 {
5044 struct signatured_type *sig_type;
5045 sect_offset abbrev_offset;
5046 };
5047
5048 /* Helper routine for build_type_unit_groups, passed to qsort. */
5049
5050 static int
5051 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5052 {
5053 const struct tu_abbrev_offset * const *a = ap;
5054 const struct tu_abbrev_offset * const *b = bp;
5055 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5056 unsigned int boff = (*b)->abbrev_offset.sect_off;
5057
5058 return (aoff > boff) - (aoff < boff);
5059 }
5060
5061 /* A helper function to add a type_unit_group to a table. */
5062
5063 static int
5064 add_type_unit_group_to_table (void **slot, void *datum)
5065 {
5066 struct type_unit_group *tu_group = *slot;
5067 struct type_unit_group ***datap = datum;
5068
5069 **datap = tu_group;
5070 ++*datap;
5071
5072 return 1;
5073 }
5074
5075 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5076 each one passing FUNC,DATA.
5077
5078 The efficiency is because we sort TUs by the abbrev table they use and
5079 only read each abbrev table once. In one program there are 200K TUs
5080 sharing 8K abbrev tables.
5081
5082 The main purpose of this function is to support building the
5083 dwarf2_per_objfile->type_unit_groups table.
5084 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5085 can collapse the search space by grouping them by stmt_list.
5086 The savings can be significant, in the same program from above the 200K TUs
5087 share 8K stmt_list tables.
5088
5089 FUNC is expected to call get_type_unit_group, which will create the
5090 struct type_unit_group if necessary and add it to
5091 dwarf2_per_objfile->type_unit_groups. */
5092
5093 static void
5094 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5095 {
5096 struct objfile *objfile = dwarf2_per_objfile->objfile;
5097 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5098 struct cleanup *cleanups;
5099 struct abbrev_table *abbrev_table;
5100 sect_offset abbrev_offset;
5101 struct tu_abbrev_offset *sorted_by_abbrev;
5102 struct type_unit_group **iter;
5103 int i;
5104
5105 /* It's up to the caller to not call us multiple times. */
5106 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5107
5108 if (dwarf2_per_objfile->n_type_units == 0)
5109 return;
5110
5111 /* TUs typically share abbrev tables, and there can be way more TUs than
5112 abbrev tables. Sort by abbrev table to reduce the number of times we
5113 read each abbrev table in.
5114 Alternatives are to punt or to maintain a cache of abbrev tables.
5115 This is simpler and efficient enough for now.
5116
5117 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5118 symtab to use). Typically TUs with the same abbrev offset have the same
5119 stmt_list value too so in practice this should work well.
5120
5121 The basic algorithm here is:
5122
5123 sort TUs by abbrev table
5124 for each TU with same abbrev table:
5125 read abbrev table if first user
5126 read TU top level DIE
5127 [IWBN if DWO skeletons had DW_AT_stmt_list]
5128 call FUNC */
5129
5130 if (dwarf2_read_debug)
5131 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5132
5133 /* Sort in a separate table to maintain the order of all_type_units
5134 for .gdb_index: TU indices directly index all_type_units. */
5135 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5136 dwarf2_per_objfile->n_type_units);
5137 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5138 {
5139 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5140
5141 sorted_by_abbrev[i].sig_type = sig_type;
5142 sorted_by_abbrev[i].abbrev_offset =
5143 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5144 sig_type->per_cu.offset);
5145 }
5146 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5147 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5148 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5149
5150 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5151 called any number of times, so we don't reset tu_stats here. */
5152
5153 abbrev_offset.sect_off = ~(unsigned) 0;
5154 abbrev_table = NULL;
5155 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5156
5157 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5158 {
5159 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5160
5161 /* Switch to the next abbrev table if necessary. */
5162 if (abbrev_table == NULL
5163 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5164 {
5165 if (abbrev_table != NULL)
5166 {
5167 abbrev_table_free (abbrev_table);
5168 /* Reset to NULL in case abbrev_table_read_table throws
5169 an error: abbrev_table_free_cleanup will get called. */
5170 abbrev_table = NULL;
5171 }
5172 abbrev_offset = tu->abbrev_offset;
5173 abbrev_table =
5174 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5175 abbrev_offset);
5176 ++tu_stats->nr_uniq_abbrev_tables;
5177 }
5178
5179 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5180 func, data);
5181 }
5182
5183 /* Create a vector of pointers to primary type units to make it easy to
5184 iterate over them and CUs. See dw2_get_primary_cu. */
5185 dwarf2_per_objfile->n_type_unit_groups =
5186 htab_elements (dwarf2_per_objfile->type_unit_groups);
5187 dwarf2_per_objfile->all_type_unit_groups =
5188 obstack_alloc (&objfile->objfile_obstack,
5189 dwarf2_per_objfile->n_type_unit_groups
5190 * sizeof (struct type_unit_group *));
5191 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5192 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5193 add_type_unit_group_to_table, &iter);
5194 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5195 == dwarf2_per_objfile->n_type_unit_groups);
5196
5197 do_cleanups (cleanups);
5198
5199 if (dwarf2_read_debug)
5200 {
5201 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5202 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5203 dwarf2_per_objfile->n_type_units);
5204 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5205 tu_stats->nr_uniq_abbrev_tables);
5206 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5207 tu_stats->nr_symtabs);
5208 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5209 tu_stats->nr_symtab_sharers);
5210 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5211 tu_stats->nr_stmt_less_type_units);
5212 }
5213 }
5214
5215 /* Reader function for build_type_psymtabs. */
5216
5217 static void
5218 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5219 gdb_byte *info_ptr,
5220 struct die_info *type_unit_die,
5221 int has_children,
5222 void *data)
5223 {
5224 struct objfile *objfile = dwarf2_per_objfile->objfile;
5225 struct dwarf2_cu *cu = reader->cu;
5226 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5227 struct type_unit_group *tu_group;
5228 struct attribute *attr;
5229 struct partial_die_info *first_die;
5230 CORE_ADDR lowpc, highpc;
5231 struct partial_symtab *pst;
5232
5233 gdb_assert (data == NULL);
5234
5235 if (! has_children)
5236 return;
5237
5238 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5239 tu_group = get_type_unit_group (cu, attr);
5240
5241 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5242
5243 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5244 cu->list_in_scope = &file_symbols;
5245 pst = create_partial_symtab (per_cu, "");
5246 pst->anonymous = 1;
5247
5248 first_die = load_partial_dies (reader, info_ptr, 1);
5249
5250 lowpc = (CORE_ADDR) -1;
5251 highpc = (CORE_ADDR) 0;
5252 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5253
5254 pst->n_global_syms = objfile->global_psymbols.next -
5255 (objfile->global_psymbols.list + pst->globals_offset);
5256 pst->n_static_syms = objfile->static_psymbols.next -
5257 (objfile->static_psymbols.list + pst->statics_offset);
5258 sort_pst_symbols (pst);
5259 }
5260
5261 /* Traversal function for build_type_psymtabs. */
5262
5263 static int
5264 build_type_psymtab_dependencies (void **slot, void *info)
5265 {
5266 struct objfile *objfile = dwarf2_per_objfile->objfile;
5267 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5268 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5269 struct partial_symtab *pst = per_cu->v.psymtab;
5270 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5271 struct dwarf2_per_cu_data *iter;
5272 int i;
5273
5274 gdb_assert (len > 0);
5275
5276 pst->number_of_dependencies = len;
5277 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5278 len * sizeof (struct psymtab *));
5279 for (i = 0;
5280 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5281 ++i)
5282 {
5283 pst->dependencies[i] = iter->v.psymtab;
5284 iter->s.type_unit_group = tu_group;
5285 }
5286
5287 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5288
5289 return 1;
5290 }
5291
5292 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5293 Build partial symbol tables for the .debug_types comp-units. */
5294
5295 static void
5296 build_type_psymtabs (struct objfile *objfile)
5297 {
5298 if (! create_all_type_units (objfile))
5299 return;
5300
5301 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5302
5303 /* Now that all TUs have been processed we can fill in the dependencies. */
5304 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5305 build_type_psymtab_dependencies, NULL);
5306 }
5307
5308 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5309
5310 static void
5311 psymtabs_addrmap_cleanup (void *o)
5312 {
5313 struct objfile *objfile = o;
5314
5315 objfile->psymtabs_addrmap = NULL;
5316 }
5317
5318 /* Compute the 'user' field for each psymtab in OBJFILE. */
5319
5320 static void
5321 set_partial_user (struct objfile *objfile)
5322 {
5323 int i;
5324
5325 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5326 {
5327 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5328 struct partial_symtab *pst = per_cu->v.psymtab;
5329 int j;
5330
5331 if (pst == NULL)
5332 continue;
5333
5334 for (j = 0; j < pst->number_of_dependencies; ++j)
5335 {
5336 /* Set the 'user' field only if it is not already set. */
5337 if (pst->dependencies[j]->user == NULL)
5338 pst->dependencies[j]->user = pst;
5339 }
5340 }
5341 }
5342
5343 /* Build the partial symbol table by doing a quick pass through the
5344 .debug_info and .debug_abbrev sections. */
5345
5346 static void
5347 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5348 {
5349 struct cleanup *back_to, *addrmap_cleanup;
5350 struct obstack temp_obstack;
5351 int i;
5352
5353 if (dwarf2_read_debug)
5354 {
5355 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5356 objfile->name);
5357 }
5358
5359 dwarf2_per_objfile->reading_partial_symbols = 1;
5360
5361 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5362
5363 /* Any cached compilation units will be linked by the per-objfile
5364 read_in_chain. Make sure to free them when we're done. */
5365 back_to = make_cleanup (free_cached_comp_units, NULL);
5366
5367 build_type_psymtabs (objfile);
5368
5369 create_all_comp_units (objfile);
5370
5371 /* Create a temporary address map on a temporary obstack. We later
5372 copy this to the final obstack. */
5373 obstack_init (&temp_obstack);
5374 make_cleanup_obstack_free (&temp_obstack);
5375 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5376 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5377
5378 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5379 {
5380 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5381
5382 process_psymtab_comp_unit (per_cu, 0);
5383 }
5384
5385 set_partial_user (objfile);
5386
5387 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5388 &objfile->objfile_obstack);
5389 discard_cleanups (addrmap_cleanup);
5390
5391 do_cleanups (back_to);
5392
5393 if (dwarf2_read_debug)
5394 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5395 objfile->name);
5396 }
5397
5398 /* die_reader_func for load_partial_comp_unit. */
5399
5400 static void
5401 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5402 gdb_byte *info_ptr,
5403 struct die_info *comp_unit_die,
5404 int has_children,
5405 void *data)
5406 {
5407 struct dwarf2_cu *cu = reader->cu;
5408
5409 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5410
5411 /* Check if comp unit has_children.
5412 If so, read the rest of the partial symbols from this comp unit.
5413 If not, there's no more debug_info for this comp unit. */
5414 if (has_children)
5415 load_partial_dies (reader, info_ptr, 0);
5416 }
5417
5418 /* Load the partial DIEs for a secondary CU into memory.
5419 This is also used when rereading a primary CU with load_all_dies. */
5420
5421 static void
5422 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5423 {
5424 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5425 load_partial_comp_unit_reader, NULL);
5426 }
5427
5428 static void
5429 read_comp_units_from_section (struct objfile *objfile,
5430 struct dwarf2_section_info *section,
5431 unsigned int is_dwz,
5432 int *n_allocated,
5433 int *n_comp_units,
5434 struct dwarf2_per_cu_data ***all_comp_units)
5435 {
5436 gdb_byte *info_ptr;
5437 bfd *abfd = section->asection->owner;
5438
5439 dwarf2_read_section (objfile, section);
5440
5441 info_ptr = section->buffer;
5442
5443 while (info_ptr < section->buffer + section->size)
5444 {
5445 unsigned int length, initial_length_size;
5446 struct dwarf2_per_cu_data *this_cu;
5447 sect_offset offset;
5448
5449 offset.sect_off = info_ptr - section->buffer;
5450
5451 /* Read just enough information to find out where the next
5452 compilation unit is. */
5453 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5454
5455 /* Save the compilation unit for later lookup. */
5456 this_cu = obstack_alloc (&objfile->objfile_obstack,
5457 sizeof (struct dwarf2_per_cu_data));
5458 memset (this_cu, 0, sizeof (*this_cu));
5459 this_cu->offset = offset;
5460 this_cu->length = length + initial_length_size;
5461 this_cu->is_dwz = is_dwz;
5462 this_cu->objfile = objfile;
5463 this_cu->info_or_types_section = section;
5464
5465 if (*n_comp_units == *n_allocated)
5466 {
5467 *n_allocated *= 2;
5468 *all_comp_units = xrealloc (*all_comp_units,
5469 *n_allocated
5470 * sizeof (struct dwarf2_per_cu_data *));
5471 }
5472 (*all_comp_units)[*n_comp_units] = this_cu;
5473 ++*n_comp_units;
5474
5475 info_ptr = info_ptr + this_cu->length;
5476 }
5477 }
5478
5479 /* Create a list of all compilation units in OBJFILE.
5480 This is only done for -readnow and building partial symtabs. */
5481
5482 static void
5483 create_all_comp_units (struct objfile *objfile)
5484 {
5485 int n_allocated;
5486 int n_comp_units;
5487 struct dwarf2_per_cu_data **all_comp_units;
5488
5489 n_comp_units = 0;
5490 n_allocated = 10;
5491 all_comp_units = xmalloc (n_allocated
5492 * sizeof (struct dwarf2_per_cu_data *));
5493
5494 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5495 &n_allocated, &n_comp_units, &all_comp_units);
5496
5497 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5498 {
5499 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5500
5501 read_comp_units_from_section (objfile, &dwz->info, 1,
5502 &n_allocated, &n_comp_units,
5503 &all_comp_units);
5504 }
5505
5506 dwarf2_per_objfile->all_comp_units
5507 = obstack_alloc (&objfile->objfile_obstack,
5508 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5509 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5510 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5511 xfree (all_comp_units);
5512 dwarf2_per_objfile->n_comp_units = n_comp_units;
5513 }
5514
5515 /* Process all loaded DIEs for compilation unit CU, starting at
5516 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5517 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5518 DW_AT_ranges). If NEED_PC is set, then this function will set
5519 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5520 and record the covered ranges in the addrmap. */
5521
5522 static void
5523 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5524 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5525 {
5526 struct partial_die_info *pdi;
5527
5528 /* Now, march along the PDI's, descending into ones which have
5529 interesting children but skipping the children of the other ones,
5530 until we reach the end of the compilation unit. */
5531
5532 pdi = first_die;
5533
5534 while (pdi != NULL)
5535 {
5536 fixup_partial_die (pdi, cu);
5537
5538 /* Anonymous namespaces or modules have no name but have interesting
5539 children, so we need to look at them. Ditto for anonymous
5540 enums. */
5541
5542 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5543 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5544 || pdi->tag == DW_TAG_imported_unit)
5545 {
5546 switch (pdi->tag)
5547 {
5548 case DW_TAG_subprogram:
5549 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5550 break;
5551 case DW_TAG_constant:
5552 case DW_TAG_variable:
5553 case DW_TAG_typedef:
5554 case DW_TAG_union_type:
5555 if (!pdi->is_declaration)
5556 {
5557 add_partial_symbol (pdi, cu);
5558 }
5559 break;
5560 case DW_TAG_class_type:
5561 case DW_TAG_interface_type:
5562 case DW_TAG_structure_type:
5563 if (!pdi->is_declaration)
5564 {
5565 add_partial_symbol (pdi, cu);
5566 }
5567 break;
5568 case DW_TAG_enumeration_type:
5569 if (!pdi->is_declaration)
5570 add_partial_enumeration (pdi, cu);
5571 break;
5572 case DW_TAG_base_type:
5573 case DW_TAG_subrange_type:
5574 /* File scope base type definitions are added to the partial
5575 symbol table. */
5576 add_partial_symbol (pdi, cu);
5577 break;
5578 case DW_TAG_namespace:
5579 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5580 break;
5581 case DW_TAG_module:
5582 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5583 break;
5584 case DW_TAG_imported_unit:
5585 {
5586 struct dwarf2_per_cu_data *per_cu;
5587
5588 /* For now we don't handle imported units in type units. */
5589 if (cu->per_cu->is_debug_types)
5590 {
5591 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5592 " supported in type units [in module %s]"),
5593 cu->objfile->name);
5594 }
5595
5596 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5597 pdi->is_dwz,
5598 cu->objfile);
5599
5600 /* Go read the partial unit, if needed. */
5601 if (per_cu->v.psymtab == NULL)
5602 process_psymtab_comp_unit (per_cu, 1);
5603
5604 VEC_safe_push (dwarf2_per_cu_ptr,
5605 cu->per_cu->s.imported_symtabs, per_cu);
5606 }
5607 break;
5608 default:
5609 break;
5610 }
5611 }
5612
5613 /* If the die has a sibling, skip to the sibling. */
5614
5615 pdi = pdi->die_sibling;
5616 }
5617 }
5618
5619 /* Functions used to compute the fully scoped name of a partial DIE.
5620
5621 Normally, this is simple. For C++, the parent DIE's fully scoped
5622 name is concatenated with "::" and the partial DIE's name. For
5623 Java, the same thing occurs except that "." is used instead of "::".
5624 Enumerators are an exception; they use the scope of their parent
5625 enumeration type, i.e. the name of the enumeration type is not
5626 prepended to the enumerator.
5627
5628 There are two complexities. One is DW_AT_specification; in this
5629 case "parent" means the parent of the target of the specification,
5630 instead of the direct parent of the DIE. The other is compilers
5631 which do not emit DW_TAG_namespace; in this case we try to guess
5632 the fully qualified name of structure types from their members'
5633 linkage names. This must be done using the DIE's children rather
5634 than the children of any DW_AT_specification target. We only need
5635 to do this for structures at the top level, i.e. if the target of
5636 any DW_AT_specification (if any; otherwise the DIE itself) does not
5637 have a parent. */
5638
5639 /* Compute the scope prefix associated with PDI's parent, in
5640 compilation unit CU. The result will be allocated on CU's
5641 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5642 field. NULL is returned if no prefix is necessary. */
5643 static char *
5644 partial_die_parent_scope (struct partial_die_info *pdi,
5645 struct dwarf2_cu *cu)
5646 {
5647 char *grandparent_scope;
5648 struct partial_die_info *parent, *real_pdi;
5649
5650 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5651 then this means the parent of the specification DIE. */
5652
5653 real_pdi = pdi;
5654 while (real_pdi->has_specification)
5655 real_pdi = find_partial_die (real_pdi->spec_offset,
5656 real_pdi->spec_is_dwz, cu);
5657
5658 parent = real_pdi->die_parent;
5659 if (parent == NULL)
5660 return NULL;
5661
5662 if (parent->scope_set)
5663 return parent->scope;
5664
5665 fixup_partial_die (parent, cu);
5666
5667 grandparent_scope = partial_die_parent_scope (parent, cu);
5668
5669 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5670 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5671 Work around this problem here. */
5672 if (cu->language == language_cplus
5673 && parent->tag == DW_TAG_namespace
5674 && strcmp (parent->name, "::") == 0
5675 && grandparent_scope == NULL)
5676 {
5677 parent->scope = NULL;
5678 parent->scope_set = 1;
5679 return NULL;
5680 }
5681
5682 if (pdi->tag == DW_TAG_enumerator)
5683 /* Enumerators should not get the name of the enumeration as a prefix. */
5684 parent->scope = grandparent_scope;
5685 else if (parent->tag == DW_TAG_namespace
5686 || parent->tag == DW_TAG_module
5687 || parent->tag == DW_TAG_structure_type
5688 || parent->tag == DW_TAG_class_type
5689 || parent->tag == DW_TAG_interface_type
5690 || parent->tag == DW_TAG_union_type
5691 || parent->tag == DW_TAG_enumeration_type)
5692 {
5693 if (grandparent_scope == NULL)
5694 parent->scope = parent->name;
5695 else
5696 parent->scope = typename_concat (&cu->comp_unit_obstack,
5697 grandparent_scope,
5698 parent->name, 0, cu);
5699 }
5700 else
5701 {
5702 /* FIXME drow/2004-04-01: What should we be doing with
5703 function-local names? For partial symbols, we should probably be
5704 ignoring them. */
5705 complaint (&symfile_complaints,
5706 _("unhandled containing DIE tag %d for DIE at %d"),
5707 parent->tag, pdi->offset.sect_off);
5708 parent->scope = grandparent_scope;
5709 }
5710
5711 parent->scope_set = 1;
5712 return parent->scope;
5713 }
5714
5715 /* Return the fully scoped name associated with PDI, from compilation unit
5716 CU. The result will be allocated with malloc. */
5717
5718 static char *
5719 partial_die_full_name (struct partial_die_info *pdi,
5720 struct dwarf2_cu *cu)
5721 {
5722 char *parent_scope;
5723
5724 /* If this is a template instantiation, we can not work out the
5725 template arguments from partial DIEs. So, unfortunately, we have
5726 to go through the full DIEs. At least any work we do building
5727 types here will be reused if full symbols are loaded later. */
5728 if (pdi->has_template_arguments)
5729 {
5730 fixup_partial_die (pdi, cu);
5731
5732 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5733 {
5734 struct die_info *die;
5735 struct attribute attr;
5736 struct dwarf2_cu *ref_cu = cu;
5737
5738 /* DW_FORM_ref_addr is using section offset. */
5739 attr.name = 0;
5740 attr.form = DW_FORM_ref_addr;
5741 attr.u.unsnd = pdi->offset.sect_off;
5742 die = follow_die_ref (NULL, &attr, &ref_cu);
5743
5744 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5745 }
5746 }
5747
5748 parent_scope = partial_die_parent_scope (pdi, cu);
5749 if (parent_scope == NULL)
5750 return NULL;
5751 else
5752 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5753 }
5754
5755 static void
5756 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5757 {
5758 struct objfile *objfile = cu->objfile;
5759 CORE_ADDR addr = 0;
5760 char *actual_name = NULL;
5761 CORE_ADDR baseaddr;
5762 int built_actual_name = 0;
5763
5764 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5765
5766 actual_name = partial_die_full_name (pdi, cu);
5767 if (actual_name)
5768 built_actual_name = 1;
5769
5770 if (actual_name == NULL)
5771 actual_name = pdi->name;
5772
5773 switch (pdi->tag)
5774 {
5775 case DW_TAG_subprogram:
5776 if (pdi->is_external || cu->language == language_ada)
5777 {
5778 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5779 of the global scope. But in Ada, we want to be able to access
5780 nested procedures globally. So all Ada subprograms are stored
5781 in the global scope. */
5782 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5783 mst_text, objfile); */
5784 add_psymbol_to_list (actual_name, strlen (actual_name),
5785 built_actual_name,
5786 VAR_DOMAIN, LOC_BLOCK,
5787 &objfile->global_psymbols,
5788 0, pdi->lowpc + baseaddr,
5789 cu->language, objfile);
5790 }
5791 else
5792 {
5793 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5794 mst_file_text, objfile); */
5795 add_psymbol_to_list (actual_name, strlen (actual_name),
5796 built_actual_name,
5797 VAR_DOMAIN, LOC_BLOCK,
5798 &objfile->static_psymbols,
5799 0, pdi->lowpc + baseaddr,
5800 cu->language, objfile);
5801 }
5802 break;
5803 case DW_TAG_constant:
5804 {
5805 struct psymbol_allocation_list *list;
5806
5807 if (pdi->is_external)
5808 list = &objfile->global_psymbols;
5809 else
5810 list = &objfile->static_psymbols;
5811 add_psymbol_to_list (actual_name, strlen (actual_name),
5812 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5813 list, 0, 0, cu->language, objfile);
5814 }
5815 break;
5816 case DW_TAG_variable:
5817 if (pdi->d.locdesc)
5818 addr = decode_locdesc (pdi->d.locdesc, cu);
5819
5820 if (pdi->d.locdesc
5821 && addr == 0
5822 && !dwarf2_per_objfile->has_section_at_zero)
5823 {
5824 /* A global or static variable may also have been stripped
5825 out by the linker if unused, in which case its address
5826 will be nullified; do not add such variables into partial
5827 symbol table then. */
5828 }
5829 else if (pdi->is_external)
5830 {
5831 /* Global Variable.
5832 Don't enter into the minimal symbol tables as there is
5833 a minimal symbol table entry from the ELF symbols already.
5834 Enter into partial symbol table if it has a location
5835 descriptor or a type.
5836 If the location descriptor is missing, new_symbol will create
5837 a LOC_UNRESOLVED symbol, the address of the variable will then
5838 be determined from the minimal symbol table whenever the variable
5839 is referenced.
5840 The address for the partial symbol table entry is not
5841 used by GDB, but it comes in handy for debugging partial symbol
5842 table building. */
5843
5844 if (pdi->d.locdesc || pdi->has_type)
5845 add_psymbol_to_list (actual_name, strlen (actual_name),
5846 built_actual_name,
5847 VAR_DOMAIN, LOC_STATIC,
5848 &objfile->global_psymbols,
5849 0, addr + baseaddr,
5850 cu->language, objfile);
5851 }
5852 else
5853 {
5854 /* Static Variable. Skip symbols without location descriptors. */
5855 if (pdi->d.locdesc == NULL)
5856 {
5857 if (built_actual_name)
5858 xfree (actual_name);
5859 return;
5860 }
5861 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5862 mst_file_data, objfile); */
5863 add_psymbol_to_list (actual_name, strlen (actual_name),
5864 built_actual_name,
5865 VAR_DOMAIN, LOC_STATIC,
5866 &objfile->static_psymbols,
5867 0, addr + baseaddr,
5868 cu->language, objfile);
5869 }
5870 break;
5871 case DW_TAG_typedef:
5872 case DW_TAG_base_type:
5873 case DW_TAG_subrange_type:
5874 add_psymbol_to_list (actual_name, strlen (actual_name),
5875 built_actual_name,
5876 VAR_DOMAIN, LOC_TYPEDEF,
5877 &objfile->static_psymbols,
5878 0, (CORE_ADDR) 0, cu->language, objfile);
5879 break;
5880 case DW_TAG_namespace:
5881 add_psymbol_to_list (actual_name, strlen (actual_name),
5882 built_actual_name,
5883 VAR_DOMAIN, LOC_TYPEDEF,
5884 &objfile->global_psymbols,
5885 0, (CORE_ADDR) 0, cu->language, objfile);
5886 break;
5887 case DW_TAG_class_type:
5888 case DW_TAG_interface_type:
5889 case DW_TAG_structure_type:
5890 case DW_TAG_union_type:
5891 case DW_TAG_enumeration_type:
5892 /* Skip external references. The DWARF standard says in the section
5893 about "Structure, Union, and Class Type Entries": "An incomplete
5894 structure, union or class type is represented by a structure,
5895 union or class entry that does not have a byte size attribute
5896 and that has a DW_AT_declaration attribute." */
5897 if (!pdi->has_byte_size && pdi->is_declaration)
5898 {
5899 if (built_actual_name)
5900 xfree (actual_name);
5901 return;
5902 }
5903
5904 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5905 static vs. global. */
5906 add_psymbol_to_list (actual_name, strlen (actual_name),
5907 built_actual_name,
5908 STRUCT_DOMAIN, LOC_TYPEDEF,
5909 (cu->language == language_cplus
5910 || cu->language == language_java)
5911 ? &objfile->global_psymbols
5912 : &objfile->static_psymbols,
5913 0, (CORE_ADDR) 0, cu->language, objfile);
5914
5915 break;
5916 case DW_TAG_enumerator:
5917 add_psymbol_to_list (actual_name, strlen (actual_name),
5918 built_actual_name,
5919 VAR_DOMAIN, LOC_CONST,
5920 (cu->language == language_cplus
5921 || cu->language == language_java)
5922 ? &objfile->global_psymbols
5923 : &objfile->static_psymbols,
5924 0, (CORE_ADDR) 0, cu->language, objfile);
5925 break;
5926 default:
5927 break;
5928 }
5929
5930 if (built_actual_name)
5931 xfree (actual_name);
5932 }
5933
5934 /* Read a partial die corresponding to a namespace; also, add a symbol
5935 corresponding to that namespace to the symbol table. NAMESPACE is
5936 the name of the enclosing namespace. */
5937
5938 static void
5939 add_partial_namespace (struct partial_die_info *pdi,
5940 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5941 int need_pc, struct dwarf2_cu *cu)
5942 {
5943 /* Add a symbol for the namespace. */
5944
5945 add_partial_symbol (pdi, cu);
5946
5947 /* Now scan partial symbols in that namespace. */
5948
5949 if (pdi->has_children)
5950 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5951 }
5952
5953 /* Read a partial die corresponding to a Fortran module. */
5954
5955 static void
5956 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5957 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5958 {
5959 /* Now scan partial symbols in that module. */
5960
5961 if (pdi->has_children)
5962 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5963 }
5964
5965 /* Read a partial die corresponding to a subprogram and create a partial
5966 symbol for that subprogram. When the CU language allows it, this
5967 routine also defines a partial symbol for each nested subprogram
5968 that this subprogram contains.
5969
5970 DIE my also be a lexical block, in which case we simply search
5971 recursively for suprograms defined inside that lexical block.
5972 Again, this is only performed when the CU language allows this
5973 type of definitions. */
5974
5975 static void
5976 add_partial_subprogram (struct partial_die_info *pdi,
5977 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5978 int need_pc, struct dwarf2_cu *cu)
5979 {
5980 if (pdi->tag == DW_TAG_subprogram)
5981 {
5982 if (pdi->has_pc_info)
5983 {
5984 if (pdi->lowpc < *lowpc)
5985 *lowpc = pdi->lowpc;
5986 if (pdi->highpc > *highpc)
5987 *highpc = pdi->highpc;
5988 if (need_pc)
5989 {
5990 CORE_ADDR baseaddr;
5991 struct objfile *objfile = cu->objfile;
5992
5993 baseaddr = ANOFFSET (objfile->section_offsets,
5994 SECT_OFF_TEXT (objfile));
5995 addrmap_set_empty (objfile->psymtabs_addrmap,
5996 pdi->lowpc + baseaddr,
5997 pdi->highpc - 1 + baseaddr,
5998 cu->per_cu->v.psymtab);
5999 }
6000 }
6001
6002 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6003 {
6004 if (!pdi->is_declaration)
6005 /* Ignore subprogram DIEs that do not have a name, they are
6006 illegal. Do not emit a complaint at this point, we will
6007 do so when we convert this psymtab into a symtab. */
6008 if (pdi->name)
6009 add_partial_symbol (pdi, cu);
6010 }
6011 }
6012
6013 if (! pdi->has_children)
6014 return;
6015
6016 if (cu->language == language_ada)
6017 {
6018 pdi = pdi->die_child;
6019 while (pdi != NULL)
6020 {
6021 fixup_partial_die (pdi, cu);
6022 if (pdi->tag == DW_TAG_subprogram
6023 || pdi->tag == DW_TAG_lexical_block)
6024 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6025 pdi = pdi->die_sibling;
6026 }
6027 }
6028 }
6029
6030 /* Read a partial die corresponding to an enumeration type. */
6031
6032 static void
6033 add_partial_enumeration (struct partial_die_info *enum_pdi,
6034 struct dwarf2_cu *cu)
6035 {
6036 struct partial_die_info *pdi;
6037
6038 if (enum_pdi->name != NULL)
6039 add_partial_symbol (enum_pdi, cu);
6040
6041 pdi = enum_pdi->die_child;
6042 while (pdi)
6043 {
6044 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6045 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6046 else
6047 add_partial_symbol (pdi, cu);
6048 pdi = pdi->die_sibling;
6049 }
6050 }
6051
6052 /* Return the initial uleb128 in the die at INFO_PTR. */
6053
6054 static unsigned int
6055 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6056 {
6057 unsigned int bytes_read;
6058
6059 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6060 }
6061
6062 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6063 Return the corresponding abbrev, or NULL if the number is zero (indicating
6064 an empty DIE). In either case *BYTES_READ will be set to the length of
6065 the initial number. */
6066
6067 static struct abbrev_info *
6068 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6069 struct dwarf2_cu *cu)
6070 {
6071 bfd *abfd = cu->objfile->obfd;
6072 unsigned int abbrev_number;
6073 struct abbrev_info *abbrev;
6074
6075 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6076
6077 if (abbrev_number == 0)
6078 return NULL;
6079
6080 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6081 if (!abbrev)
6082 {
6083 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6084 abbrev_number, bfd_get_filename (abfd));
6085 }
6086
6087 return abbrev;
6088 }
6089
6090 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6091 Returns a pointer to the end of a series of DIEs, terminated by an empty
6092 DIE. Any children of the skipped DIEs will also be skipped. */
6093
6094 static gdb_byte *
6095 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6096 {
6097 struct dwarf2_cu *cu = reader->cu;
6098 struct abbrev_info *abbrev;
6099 unsigned int bytes_read;
6100
6101 while (1)
6102 {
6103 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6104 if (abbrev == NULL)
6105 return info_ptr + bytes_read;
6106 else
6107 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6108 }
6109 }
6110
6111 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6112 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6113 abbrev corresponding to that skipped uleb128 should be passed in
6114 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6115 children. */
6116
6117 static gdb_byte *
6118 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6119 struct abbrev_info *abbrev)
6120 {
6121 unsigned int bytes_read;
6122 struct attribute attr;
6123 bfd *abfd = reader->abfd;
6124 struct dwarf2_cu *cu = reader->cu;
6125 gdb_byte *buffer = reader->buffer;
6126 const gdb_byte *buffer_end = reader->buffer_end;
6127 gdb_byte *start_info_ptr = info_ptr;
6128 unsigned int form, i;
6129
6130 for (i = 0; i < abbrev->num_attrs; i++)
6131 {
6132 /* The only abbrev we care about is DW_AT_sibling. */
6133 if (abbrev->attrs[i].name == DW_AT_sibling)
6134 {
6135 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6136 if (attr.form == DW_FORM_ref_addr)
6137 complaint (&symfile_complaints,
6138 _("ignoring absolute DW_AT_sibling"));
6139 else
6140 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6141 }
6142
6143 /* If it isn't DW_AT_sibling, skip this attribute. */
6144 form = abbrev->attrs[i].form;
6145 skip_attribute:
6146 switch (form)
6147 {
6148 case DW_FORM_ref_addr:
6149 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6150 and later it is offset sized. */
6151 if (cu->header.version == 2)
6152 info_ptr += cu->header.addr_size;
6153 else
6154 info_ptr += cu->header.offset_size;
6155 break;
6156 case DW_FORM_GNU_ref_alt:
6157 info_ptr += cu->header.offset_size;
6158 break;
6159 case DW_FORM_addr:
6160 info_ptr += cu->header.addr_size;
6161 break;
6162 case DW_FORM_data1:
6163 case DW_FORM_ref1:
6164 case DW_FORM_flag:
6165 info_ptr += 1;
6166 break;
6167 case DW_FORM_flag_present:
6168 break;
6169 case DW_FORM_data2:
6170 case DW_FORM_ref2:
6171 info_ptr += 2;
6172 break;
6173 case DW_FORM_data4:
6174 case DW_FORM_ref4:
6175 info_ptr += 4;
6176 break;
6177 case DW_FORM_data8:
6178 case DW_FORM_ref8:
6179 case DW_FORM_ref_sig8:
6180 info_ptr += 8;
6181 break;
6182 case DW_FORM_string:
6183 read_direct_string (abfd, info_ptr, &bytes_read);
6184 info_ptr += bytes_read;
6185 break;
6186 case DW_FORM_sec_offset:
6187 case DW_FORM_strp:
6188 case DW_FORM_GNU_strp_alt:
6189 info_ptr += cu->header.offset_size;
6190 break;
6191 case DW_FORM_exprloc:
6192 case DW_FORM_block:
6193 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6194 info_ptr += bytes_read;
6195 break;
6196 case DW_FORM_block1:
6197 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6198 break;
6199 case DW_FORM_block2:
6200 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6201 break;
6202 case DW_FORM_block4:
6203 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6204 break;
6205 case DW_FORM_sdata:
6206 case DW_FORM_udata:
6207 case DW_FORM_ref_udata:
6208 case DW_FORM_GNU_addr_index:
6209 case DW_FORM_GNU_str_index:
6210 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6211 break;
6212 case DW_FORM_indirect:
6213 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6214 info_ptr += bytes_read;
6215 /* We need to continue parsing from here, so just go back to
6216 the top. */
6217 goto skip_attribute;
6218
6219 default:
6220 error (_("Dwarf Error: Cannot handle %s "
6221 "in DWARF reader [in module %s]"),
6222 dwarf_form_name (form),
6223 bfd_get_filename (abfd));
6224 }
6225 }
6226
6227 if (abbrev->has_children)
6228 return skip_children (reader, info_ptr);
6229 else
6230 return info_ptr;
6231 }
6232
6233 /* Locate ORIG_PDI's sibling.
6234 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6235
6236 static gdb_byte *
6237 locate_pdi_sibling (const struct die_reader_specs *reader,
6238 struct partial_die_info *orig_pdi,
6239 gdb_byte *info_ptr)
6240 {
6241 /* Do we know the sibling already? */
6242
6243 if (orig_pdi->sibling)
6244 return orig_pdi->sibling;
6245
6246 /* Are there any children to deal with? */
6247
6248 if (!orig_pdi->has_children)
6249 return info_ptr;
6250
6251 /* Skip the children the long way. */
6252
6253 return skip_children (reader, info_ptr);
6254 }
6255
6256 /* Expand this partial symbol table into a full symbol table. */
6257
6258 static void
6259 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6260 {
6261 if (pst != NULL)
6262 {
6263 if (pst->readin)
6264 {
6265 warning (_("bug: psymtab for %s is already read in."),
6266 pst->filename);
6267 }
6268 else
6269 {
6270 if (info_verbose)
6271 {
6272 printf_filtered (_("Reading in symbols for %s..."),
6273 pst->filename);
6274 gdb_flush (gdb_stdout);
6275 }
6276
6277 /* Restore our global data. */
6278 dwarf2_per_objfile = objfile_data (pst->objfile,
6279 dwarf2_objfile_data_key);
6280
6281 /* If this psymtab is constructed from a debug-only objfile, the
6282 has_section_at_zero flag will not necessarily be correct. We
6283 can get the correct value for this flag by looking at the data
6284 associated with the (presumably stripped) associated objfile. */
6285 if (pst->objfile->separate_debug_objfile_backlink)
6286 {
6287 struct dwarf2_per_objfile *dpo_backlink
6288 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6289 dwarf2_objfile_data_key);
6290
6291 dwarf2_per_objfile->has_section_at_zero
6292 = dpo_backlink->has_section_at_zero;
6293 }
6294
6295 dwarf2_per_objfile->reading_partial_symbols = 0;
6296
6297 psymtab_to_symtab_1 (pst);
6298
6299 /* Finish up the debug error message. */
6300 if (info_verbose)
6301 printf_filtered (_("done.\n"));
6302 }
6303 }
6304
6305 process_cu_includes ();
6306 }
6307 \f
6308 /* Reading in full CUs. */
6309
6310 /* Add PER_CU to the queue. */
6311
6312 static void
6313 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6314 enum language pretend_language)
6315 {
6316 struct dwarf2_queue_item *item;
6317
6318 per_cu->queued = 1;
6319 item = xmalloc (sizeof (*item));
6320 item->per_cu = per_cu;
6321 item->pretend_language = pretend_language;
6322 item->next = NULL;
6323
6324 if (dwarf2_queue == NULL)
6325 dwarf2_queue = item;
6326 else
6327 dwarf2_queue_tail->next = item;
6328
6329 dwarf2_queue_tail = item;
6330 }
6331
6332 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6333 unit and add it to our queue.
6334 The result is non-zero if PER_CU was queued, otherwise the result is zero
6335 meaning either PER_CU is already queued or it is already loaded. */
6336
6337 static int
6338 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6339 struct dwarf2_per_cu_data *per_cu,
6340 enum language pretend_language)
6341 {
6342 /* We may arrive here during partial symbol reading, if we need full
6343 DIEs to process an unusual case (e.g. template arguments). Do
6344 not queue PER_CU, just tell our caller to load its DIEs. */
6345 if (dwarf2_per_objfile->reading_partial_symbols)
6346 {
6347 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6348 return 1;
6349 return 0;
6350 }
6351
6352 /* Mark the dependence relation so that we don't flush PER_CU
6353 too early. */
6354 dwarf2_add_dependence (this_cu, per_cu);
6355
6356 /* If it's already on the queue, we have nothing to do. */
6357 if (per_cu->queued)
6358 return 0;
6359
6360 /* If the compilation unit is already loaded, just mark it as
6361 used. */
6362 if (per_cu->cu != NULL)
6363 {
6364 per_cu->cu->last_used = 0;
6365 return 0;
6366 }
6367
6368 /* Add it to the queue. */
6369 queue_comp_unit (per_cu, pretend_language);
6370
6371 return 1;
6372 }
6373
6374 /* Process the queue. */
6375
6376 static void
6377 process_queue (void)
6378 {
6379 struct dwarf2_queue_item *item, *next_item;
6380
6381 if (dwarf2_read_debug)
6382 {
6383 fprintf_unfiltered (gdb_stdlog,
6384 "Expanding one or more symtabs of objfile %s ...\n",
6385 dwarf2_per_objfile->objfile->name);
6386 }
6387
6388 /* The queue starts out with one item, but following a DIE reference
6389 may load a new CU, adding it to the end of the queue. */
6390 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6391 {
6392 if (dwarf2_per_objfile->using_index
6393 ? !item->per_cu->v.quick->symtab
6394 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6395 {
6396 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6397
6398 if (dwarf2_read_debug)
6399 {
6400 fprintf_unfiltered (gdb_stdlog,
6401 "Expanding symtab of %s at offset 0x%x\n",
6402 per_cu->is_debug_types ? "TU" : "CU",
6403 per_cu->offset.sect_off);
6404 }
6405
6406 if (per_cu->is_debug_types)
6407 process_full_type_unit (per_cu, item->pretend_language);
6408 else
6409 process_full_comp_unit (per_cu, item->pretend_language);
6410
6411 if (dwarf2_read_debug)
6412 {
6413 fprintf_unfiltered (gdb_stdlog,
6414 "Done expanding %s at offset 0x%x\n",
6415 per_cu->is_debug_types ? "TU" : "CU",
6416 per_cu->offset.sect_off);
6417 }
6418 }
6419
6420 item->per_cu->queued = 0;
6421 next_item = item->next;
6422 xfree (item);
6423 }
6424
6425 dwarf2_queue_tail = NULL;
6426
6427 if (dwarf2_read_debug)
6428 {
6429 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6430 dwarf2_per_objfile->objfile->name);
6431 }
6432 }
6433
6434 /* Free all allocated queue entries. This function only releases anything if
6435 an error was thrown; if the queue was processed then it would have been
6436 freed as we went along. */
6437
6438 static void
6439 dwarf2_release_queue (void *dummy)
6440 {
6441 struct dwarf2_queue_item *item, *last;
6442
6443 item = dwarf2_queue;
6444 while (item)
6445 {
6446 /* Anything still marked queued is likely to be in an
6447 inconsistent state, so discard it. */
6448 if (item->per_cu->queued)
6449 {
6450 if (item->per_cu->cu != NULL)
6451 free_one_cached_comp_unit (item->per_cu);
6452 item->per_cu->queued = 0;
6453 }
6454
6455 last = item;
6456 item = item->next;
6457 xfree (last);
6458 }
6459
6460 dwarf2_queue = dwarf2_queue_tail = NULL;
6461 }
6462
6463 /* Read in full symbols for PST, and anything it depends on. */
6464
6465 static void
6466 psymtab_to_symtab_1 (struct partial_symtab *pst)
6467 {
6468 struct dwarf2_per_cu_data *per_cu;
6469 int i;
6470
6471 if (pst->readin)
6472 return;
6473
6474 for (i = 0; i < pst->number_of_dependencies; i++)
6475 if (!pst->dependencies[i]->readin
6476 && pst->dependencies[i]->user == NULL)
6477 {
6478 /* Inform about additional files that need to be read in. */
6479 if (info_verbose)
6480 {
6481 /* FIXME: i18n: Need to make this a single string. */
6482 fputs_filtered (" ", gdb_stdout);
6483 wrap_here ("");
6484 fputs_filtered ("and ", gdb_stdout);
6485 wrap_here ("");
6486 printf_filtered ("%s...", pst->dependencies[i]->filename);
6487 wrap_here (""); /* Flush output. */
6488 gdb_flush (gdb_stdout);
6489 }
6490 psymtab_to_symtab_1 (pst->dependencies[i]);
6491 }
6492
6493 per_cu = pst->read_symtab_private;
6494
6495 if (per_cu == NULL)
6496 {
6497 /* It's an include file, no symbols to read for it.
6498 Everything is in the parent symtab. */
6499 pst->readin = 1;
6500 return;
6501 }
6502
6503 dw2_do_instantiate_symtab (per_cu);
6504 }
6505
6506 /* Trivial hash function for die_info: the hash value of a DIE
6507 is its offset in .debug_info for this objfile. */
6508
6509 static hashval_t
6510 die_hash (const void *item)
6511 {
6512 const struct die_info *die = item;
6513
6514 return die->offset.sect_off;
6515 }
6516
6517 /* Trivial comparison function for die_info structures: two DIEs
6518 are equal if they have the same offset. */
6519
6520 static int
6521 die_eq (const void *item_lhs, const void *item_rhs)
6522 {
6523 const struct die_info *die_lhs = item_lhs;
6524 const struct die_info *die_rhs = item_rhs;
6525
6526 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6527 }
6528
6529 /* die_reader_func for load_full_comp_unit.
6530 This is identical to read_signatured_type_reader,
6531 but is kept separate for now. */
6532
6533 static void
6534 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6535 gdb_byte *info_ptr,
6536 struct die_info *comp_unit_die,
6537 int has_children,
6538 void *data)
6539 {
6540 struct dwarf2_cu *cu = reader->cu;
6541 enum language *language_ptr = data;
6542
6543 gdb_assert (cu->die_hash == NULL);
6544 cu->die_hash =
6545 htab_create_alloc_ex (cu->header.length / 12,
6546 die_hash,
6547 die_eq,
6548 NULL,
6549 &cu->comp_unit_obstack,
6550 hashtab_obstack_allocate,
6551 dummy_obstack_deallocate);
6552
6553 if (has_children)
6554 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6555 &info_ptr, comp_unit_die);
6556 cu->dies = comp_unit_die;
6557 /* comp_unit_die is not stored in die_hash, no need. */
6558
6559 /* We try not to read any attributes in this function, because not
6560 all CUs needed for references have been loaded yet, and symbol
6561 table processing isn't initialized. But we have to set the CU language,
6562 or we won't be able to build types correctly.
6563 Similarly, if we do not read the producer, we can not apply
6564 producer-specific interpretation. */
6565 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6566 }
6567
6568 /* Load the DIEs associated with PER_CU into memory. */
6569
6570 static void
6571 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6572 enum language pretend_language)
6573 {
6574 gdb_assert (! this_cu->is_debug_types);
6575
6576 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6577 load_full_comp_unit_reader, &pretend_language);
6578 }
6579
6580 /* Add a DIE to the delayed physname list. */
6581
6582 static void
6583 add_to_method_list (struct type *type, int fnfield_index, int index,
6584 const char *name, struct die_info *die,
6585 struct dwarf2_cu *cu)
6586 {
6587 struct delayed_method_info mi;
6588 mi.type = type;
6589 mi.fnfield_index = fnfield_index;
6590 mi.index = index;
6591 mi.name = name;
6592 mi.die = die;
6593 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6594 }
6595
6596 /* A cleanup for freeing the delayed method list. */
6597
6598 static void
6599 free_delayed_list (void *ptr)
6600 {
6601 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6602 if (cu->method_list != NULL)
6603 {
6604 VEC_free (delayed_method_info, cu->method_list);
6605 cu->method_list = NULL;
6606 }
6607 }
6608
6609 /* Compute the physnames of any methods on the CU's method list.
6610
6611 The computation of method physnames is delayed in order to avoid the
6612 (bad) condition that one of the method's formal parameters is of an as yet
6613 incomplete type. */
6614
6615 static void
6616 compute_delayed_physnames (struct dwarf2_cu *cu)
6617 {
6618 int i;
6619 struct delayed_method_info *mi;
6620 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6621 {
6622 const char *physname;
6623 struct fn_fieldlist *fn_flp
6624 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6625 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6626 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6627 }
6628 }
6629
6630 /* Go objects should be embedded in a DW_TAG_module DIE,
6631 and it's not clear if/how imported objects will appear.
6632 To keep Go support simple until that's worked out,
6633 go back through what we've read and create something usable.
6634 We could do this while processing each DIE, and feels kinda cleaner,
6635 but that way is more invasive.
6636 This is to, for example, allow the user to type "p var" or "b main"
6637 without having to specify the package name, and allow lookups
6638 of module.object to work in contexts that use the expression
6639 parser. */
6640
6641 static void
6642 fixup_go_packaging (struct dwarf2_cu *cu)
6643 {
6644 char *package_name = NULL;
6645 struct pending *list;
6646 int i;
6647
6648 for (list = global_symbols; list != NULL; list = list->next)
6649 {
6650 for (i = 0; i < list->nsyms; ++i)
6651 {
6652 struct symbol *sym = list->symbol[i];
6653
6654 if (SYMBOL_LANGUAGE (sym) == language_go
6655 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6656 {
6657 char *this_package_name = go_symbol_package_name (sym);
6658
6659 if (this_package_name == NULL)
6660 continue;
6661 if (package_name == NULL)
6662 package_name = this_package_name;
6663 else
6664 {
6665 if (strcmp (package_name, this_package_name) != 0)
6666 complaint (&symfile_complaints,
6667 _("Symtab %s has objects from two different Go packages: %s and %s"),
6668 (sym->symtab && sym->symtab->filename
6669 ? sym->symtab->filename
6670 : cu->objfile->name),
6671 this_package_name, package_name);
6672 xfree (this_package_name);
6673 }
6674 }
6675 }
6676 }
6677
6678 if (package_name != NULL)
6679 {
6680 struct objfile *objfile = cu->objfile;
6681 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6682 package_name, objfile);
6683 struct symbol *sym;
6684
6685 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6686
6687 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6688 SYMBOL_SET_LANGUAGE (sym, language_go);
6689 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6690 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6691 e.g., "main" finds the "main" module and not C's main(). */
6692 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6693 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6694 SYMBOL_TYPE (sym) = type;
6695
6696 add_symbol_to_list (sym, &global_symbols);
6697
6698 xfree (package_name);
6699 }
6700 }
6701
6702 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6703
6704 /* Return the symtab for PER_CU. This works properly regardless of
6705 whether we're using the index or psymtabs. */
6706
6707 static struct symtab *
6708 get_symtab (struct dwarf2_per_cu_data *per_cu)
6709 {
6710 return (dwarf2_per_objfile->using_index
6711 ? per_cu->v.quick->symtab
6712 : per_cu->v.psymtab->symtab);
6713 }
6714
6715 /* A helper function for computing the list of all symbol tables
6716 included by PER_CU. */
6717
6718 static void
6719 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6720 htab_t all_children,
6721 struct dwarf2_per_cu_data *per_cu)
6722 {
6723 void **slot;
6724 int ix;
6725 struct dwarf2_per_cu_data *iter;
6726
6727 slot = htab_find_slot (all_children, per_cu, INSERT);
6728 if (*slot != NULL)
6729 {
6730 /* This inclusion and its children have been processed. */
6731 return;
6732 }
6733
6734 *slot = per_cu;
6735 /* Only add a CU if it has a symbol table. */
6736 if (get_symtab (per_cu) != NULL)
6737 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6738
6739 for (ix = 0;
6740 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6741 ++ix)
6742 recursively_compute_inclusions (result, all_children, iter);
6743 }
6744
6745 /* Compute the symtab 'includes' fields for the symtab related to
6746 PER_CU. */
6747
6748 static void
6749 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6750 {
6751 gdb_assert (! per_cu->is_debug_types);
6752
6753 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6754 {
6755 int ix, len;
6756 struct dwarf2_per_cu_data *iter;
6757 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6758 htab_t all_children;
6759 struct symtab *symtab = get_symtab (per_cu);
6760
6761 /* If we don't have a symtab, we can just skip this case. */
6762 if (symtab == NULL)
6763 return;
6764
6765 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6766 NULL, xcalloc, xfree);
6767
6768 for (ix = 0;
6769 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6770 ix, iter);
6771 ++ix)
6772 recursively_compute_inclusions (&result_children, all_children, iter);
6773
6774 /* Now we have a transitive closure of all the included CUs, so
6775 we can convert it to a list of symtabs. */
6776 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6777 symtab->includes
6778 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6779 (len + 1) * sizeof (struct symtab *));
6780 for (ix = 0;
6781 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6782 ++ix)
6783 symtab->includes[ix] = get_symtab (iter);
6784 symtab->includes[len] = NULL;
6785
6786 VEC_free (dwarf2_per_cu_ptr, result_children);
6787 htab_delete (all_children);
6788 }
6789 }
6790
6791 /* Compute the 'includes' field for the symtabs of all the CUs we just
6792 read. */
6793
6794 static void
6795 process_cu_includes (void)
6796 {
6797 int ix;
6798 struct dwarf2_per_cu_data *iter;
6799
6800 for (ix = 0;
6801 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6802 ix, iter);
6803 ++ix)
6804 {
6805 if (! iter->is_debug_types)
6806 compute_symtab_includes (iter);
6807 }
6808
6809 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6810 }
6811
6812 /* Generate full symbol information for PER_CU, whose DIEs have
6813 already been loaded into memory. */
6814
6815 static void
6816 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6817 enum language pretend_language)
6818 {
6819 struct dwarf2_cu *cu = per_cu->cu;
6820 struct objfile *objfile = per_cu->objfile;
6821 CORE_ADDR lowpc, highpc;
6822 struct symtab *symtab;
6823 struct cleanup *back_to, *delayed_list_cleanup;
6824 CORE_ADDR baseaddr;
6825 struct block *static_block;
6826
6827 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6828
6829 buildsym_init ();
6830 back_to = make_cleanup (really_free_pendings, NULL);
6831 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6832
6833 cu->list_in_scope = &file_symbols;
6834
6835 cu->language = pretend_language;
6836 cu->language_defn = language_def (cu->language);
6837
6838 /* Do line number decoding in read_file_scope () */
6839 process_die (cu->dies, cu);
6840
6841 /* For now fudge the Go package. */
6842 if (cu->language == language_go)
6843 fixup_go_packaging (cu);
6844
6845 /* Now that we have processed all the DIEs in the CU, all the types
6846 should be complete, and it should now be safe to compute all of the
6847 physnames. */
6848 compute_delayed_physnames (cu);
6849 do_cleanups (delayed_list_cleanup);
6850
6851 /* Some compilers don't define a DW_AT_high_pc attribute for the
6852 compilation unit. If the DW_AT_high_pc is missing, synthesize
6853 it, by scanning the DIE's below the compilation unit. */
6854 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6855
6856 static_block
6857 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6858 per_cu->s.imported_symtabs != NULL);
6859
6860 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6861 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6862 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6863 addrmap to help ensure it has an accurate map of pc values belonging to
6864 this comp unit. */
6865 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6866
6867 symtab = end_symtab_from_static_block (static_block, objfile,
6868 SECT_OFF_TEXT (objfile), 0);
6869
6870 if (symtab != NULL)
6871 {
6872 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6873
6874 /* Set symtab language to language from DW_AT_language. If the
6875 compilation is from a C file generated by language preprocessors, do
6876 not set the language if it was already deduced by start_subfile. */
6877 if (!(cu->language == language_c && symtab->language != language_c))
6878 symtab->language = cu->language;
6879
6880 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6881 produce DW_AT_location with location lists but it can be possibly
6882 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6883 there were bugs in prologue debug info, fixed later in GCC-4.5
6884 by "unwind info for epilogues" patch (which is not directly related).
6885
6886 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6887 needed, it would be wrong due to missing DW_AT_producer there.
6888
6889 Still one can confuse GDB by using non-standard GCC compilation
6890 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6891 */
6892 if (cu->has_loclist && gcc_4_minor >= 5)
6893 symtab->locations_valid = 1;
6894
6895 if (gcc_4_minor >= 5)
6896 symtab->epilogue_unwind_valid = 1;
6897
6898 symtab->call_site_htab = cu->call_site_htab;
6899 }
6900
6901 if (dwarf2_per_objfile->using_index)
6902 per_cu->v.quick->symtab = symtab;
6903 else
6904 {
6905 struct partial_symtab *pst = per_cu->v.psymtab;
6906 pst->symtab = symtab;
6907 pst->readin = 1;
6908 }
6909
6910 /* Push it for inclusion processing later. */
6911 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6912
6913 do_cleanups (back_to);
6914 }
6915
6916 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6917 already been loaded into memory. */
6918
6919 static void
6920 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6921 enum language pretend_language)
6922 {
6923 struct dwarf2_cu *cu = per_cu->cu;
6924 struct objfile *objfile = per_cu->objfile;
6925 struct symtab *symtab;
6926 struct cleanup *back_to, *delayed_list_cleanup;
6927
6928 buildsym_init ();
6929 back_to = make_cleanup (really_free_pendings, NULL);
6930 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6931
6932 cu->list_in_scope = &file_symbols;
6933
6934 cu->language = pretend_language;
6935 cu->language_defn = language_def (cu->language);
6936
6937 /* The symbol tables are set up in read_type_unit_scope. */
6938 process_die (cu->dies, cu);
6939
6940 /* For now fudge the Go package. */
6941 if (cu->language == language_go)
6942 fixup_go_packaging (cu);
6943
6944 /* Now that we have processed all the DIEs in the CU, all the types
6945 should be complete, and it should now be safe to compute all of the
6946 physnames. */
6947 compute_delayed_physnames (cu);
6948 do_cleanups (delayed_list_cleanup);
6949
6950 /* TUs share symbol tables.
6951 If this is the first TU to use this symtab, complete the construction
6952 of it with end_expandable_symtab. Otherwise, complete the addition of
6953 this TU's symbols to the existing symtab. */
6954 if (per_cu->s.type_unit_group->primary_symtab == NULL)
6955 {
6956 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6957 per_cu->s.type_unit_group->primary_symtab = symtab;
6958
6959 if (symtab != NULL)
6960 {
6961 /* Set symtab language to language from DW_AT_language. If the
6962 compilation is from a C file generated by language preprocessors,
6963 do not set the language if it was already deduced by
6964 start_subfile. */
6965 if (!(cu->language == language_c && symtab->language != language_c))
6966 symtab->language = cu->language;
6967 }
6968 }
6969 else
6970 {
6971 augment_type_symtab (objfile,
6972 per_cu->s.type_unit_group->primary_symtab);
6973 symtab = per_cu->s.type_unit_group->primary_symtab;
6974 }
6975
6976 if (dwarf2_per_objfile->using_index)
6977 per_cu->v.quick->symtab = symtab;
6978 else
6979 {
6980 struct partial_symtab *pst = per_cu->v.psymtab;
6981 pst->symtab = symtab;
6982 pst->readin = 1;
6983 }
6984
6985 do_cleanups (back_to);
6986 }
6987
6988 /* Process an imported unit DIE. */
6989
6990 static void
6991 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6992 {
6993 struct attribute *attr;
6994
6995 /* For now we don't handle imported units in type units. */
6996 if (cu->per_cu->is_debug_types)
6997 {
6998 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6999 " supported in type units [in module %s]"),
7000 cu->objfile->name);
7001 }
7002
7003 attr = dwarf2_attr (die, DW_AT_import, cu);
7004 if (attr != NULL)
7005 {
7006 struct dwarf2_per_cu_data *per_cu;
7007 struct symtab *imported_symtab;
7008 sect_offset offset;
7009 int is_dwz;
7010
7011 offset = dwarf2_get_ref_die_offset (attr);
7012 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7013 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7014
7015 /* Queue the unit, if needed. */
7016 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7017 load_full_comp_unit (per_cu, cu->language);
7018
7019 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7020 per_cu);
7021 }
7022 }
7023
7024 /* Process a die and its children. */
7025
7026 static void
7027 process_die (struct die_info *die, struct dwarf2_cu *cu)
7028 {
7029 switch (die->tag)
7030 {
7031 case DW_TAG_padding:
7032 break;
7033 case DW_TAG_compile_unit:
7034 case DW_TAG_partial_unit:
7035 read_file_scope (die, cu);
7036 break;
7037 case DW_TAG_type_unit:
7038 read_type_unit_scope (die, cu);
7039 break;
7040 case DW_TAG_subprogram:
7041 case DW_TAG_inlined_subroutine:
7042 read_func_scope (die, cu);
7043 break;
7044 case DW_TAG_lexical_block:
7045 case DW_TAG_try_block:
7046 case DW_TAG_catch_block:
7047 read_lexical_block_scope (die, cu);
7048 break;
7049 case DW_TAG_GNU_call_site:
7050 read_call_site_scope (die, cu);
7051 break;
7052 case DW_TAG_class_type:
7053 case DW_TAG_interface_type:
7054 case DW_TAG_structure_type:
7055 case DW_TAG_union_type:
7056 process_structure_scope (die, cu);
7057 break;
7058 case DW_TAG_enumeration_type:
7059 process_enumeration_scope (die, cu);
7060 break;
7061
7062 /* These dies have a type, but processing them does not create
7063 a symbol or recurse to process the children. Therefore we can
7064 read them on-demand through read_type_die. */
7065 case DW_TAG_subroutine_type:
7066 case DW_TAG_set_type:
7067 case DW_TAG_array_type:
7068 case DW_TAG_pointer_type:
7069 case DW_TAG_ptr_to_member_type:
7070 case DW_TAG_reference_type:
7071 case DW_TAG_string_type:
7072 break;
7073
7074 case DW_TAG_base_type:
7075 case DW_TAG_subrange_type:
7076 case DW_TAG_typedef:
7077 /* Add a typedef symbol for the type definition, if it has a
7078 DW_AT_name. */
7079 new_symbol (die, read_type_die (die, cu), cu);
7080 break;
7081 case DW_TAG_common_block:
7082 read_common_block (die, cu);
7083 break;
7084 case DW_TAG_common_inclusion:
7085 break;
7086 case DW_TAG_namespace:
7087 processing_has_namespace_info = 1;
7088 read_namespace (die, cu);
7089 break;
7090 case DW_TAG_module:
7091 processing_has_namespace_info = 1;
7092 read_module (die, cu);
7093 break;
7094 case DW_TAG_imported_declaration:
7095 case DW_TAG_imported_module:
7096 processing_has_namespace_info = 1;
7097 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7098 || cu->language != language_fortran))
7099 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7100 dwarf_tag_name (die->tag));
7101 read_import_statement (die, cu);
7102 break;
7103
7104 case DW_TAG_imported_unit:
7105 process_imported_unit_die (die, cu);
7106 break;
7107
7108 default:
7109 new_symbol (die, NULL, cu);
7110 break;
7111 }
7112 }
7113
7114 /* A helper function for dwarf2_compute_name which determines whether DIE
7115 needs to have the name of the scope prepended to the name listed in the
7116 die. */
7117
7118 static int
7119 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7120 {
7121 struct attribute *attr;
7122
7123 switch (die->tag)
7124 {
7125 case DW_TAG_namespace:
7126 case DW_TAG_typedef:
7127 case DW_TAG_class_type:
7128 case DW_TAG_interface_type:
7129 case DW_TAG_structure_type:
7130 case DW_TAG_union_type:
7131 case DW_TAG_enumeration_type:
7132 case DW_TAG_enumerator:
7133 case DW_TAG_subprogram:
7134 case DW_TAG_member:
7135 return 1;
7136
7137 case DW_TAG_variable:
7138 case DW_TAG_constant:
7139 /* We only need to prefix "globally" visible variables. These include
7140 any variable marked with DW_AT_external or any variable that
7141 lives in a namespace. [Variables in anonymous namespaces
7142 require prefixing, but they are not DW_AT_external.] */
7143
7144 if (dwarf2_attr (die, DW_AT_specification, cu))
7145 {
7146 struct dwarf2_cu *spec_cu = cu;
7147
7148 return die_needs_namespace (die_specification (die, &spec_cu),
7149 spec_cu);
7150 }
7151
7152 attr = dwarf2_attr (die, DW_AT_external, cu);
7153 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7154 && die->parent->tag != DW_TAG_module)
7155 return 0;
7156 /* A variable in a lexical block of some kind does not need a
7157 namespace, even though in C++ such variables may be external
7158 and have a mangled name. */
7159 if (die->parent->tag == DW_TAG_lexical_block
7160 || die->parent->tag == DW_TAG_try_block
7161 || die->parent->tag == DW_TAG_catch_block
7162 || die->parent->tag == DW_TAG_subprogram)
7163 return 0;
7164 return 1;
7165
7166 default:
7167 return 0;
7168 }
7169 }
7170
7171 /* Retrieve the last character from a mem_file. */
7172
7173 static void
7174 do_ui_file_peek_last (void *object, const char *buffer, long length)
7175 {
7176 char *last_char_p = (char *) object;
7177
7178 if (length > 0)
7179 *last_char_p = buffer[length - 1];
7180 }
7181
7182 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7183 compute the physname for the object, which include a method's:
7184 - formal parameters (C++/Java),
7185 - receiver type (Go),
7186 - return type (Java).
7187
7188 The term "physname" is a bit confusing.
7189 For C++, for example, it is the demangled name.
7190 For Go, for example, it's the mangled name.
7191
7192 For Ada, return the DIE's linkage name rather than the fully qualified
7193 name. PHYSNAME is ignored..
7194
7195 The result is allocated on the objfile_obstack and canonicalized. */
7196
7197 static const char *
7198 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7199 int physname)
7200 {
7201 struct objfile *objfile = cu->objfile;
7202
7203 if (name == NULL)
7204 name = dwarf2_name (die, cu);
7205
7206 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7207 compute it by typename_concat inside GDB. */
7208 if (cu->language == language_ada
7209 || (cu->language == language_fortran && physname))
7210 {
7211 /* For Ada unit, we prefer the linkage name over the name, as
7212 the former contains the exported name, which the user expects
7213 to be able to reference. Ideally, we want the user to be able
7214 to reference this entity using either natural or linkage name,
7215 but we haven't started looking at this enhancement yet. */
7216 struct attribute *attr;
7217
7218 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7219 if (attr == NULL)
7220 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7221 if (attr && DW_STRING (attr))
7222 return DW_STRING (attr);
7223 }
7224
7225 /* These are the only languages we know how to qualify names in. */
7226 if (name != NULL
7227 && (cu->language == language_cplus || cu->language == language_java
7228 || cu->language == language_fortran))
7229 {
7230 if (die_needs_namespace (die, cu))
7231 {
7232 long length;
7233 const char *prefix;
7234 struct ui_file *buf;
7235
7236 prefix = determine_prefix (die, cu);
7237 buf = mem_fileopen ();
7238 if (*prefix != '\0')
7239 {
7240 char *prefixed_name = typename_concat (NULL, prefix, name,
7241 physname, cu);
7242
7243 fputs_unfiltered (prefixed_name, buf);
7244 xfree (prefixed_name);
7245 }
7246 else
7247 fputs_unfiltered (name, buf);
7248
7249 /* Template parameters may be specified in the DIE's DW_AT_name, or
7250 as children with DW_TAG_template_type_param or
7251 DW_TAG_value_type_param. If the latter, add them to the name
7252 here. If the name already has template parameters, then
7253 skip this step; some versions of GCC emit both, and
7254 it is more efficient to use the pre-computed name.
7255
7256 Something to keep in mind about this process: it is very
7257 unlikely, or in some cases downright impossible, to produce
7258 something that will match the mangled name of a function.
7259 If the definition of the function has the same debug info,
7260 we should be able to match up with it anyway. But fallbacks
7261 using the minimal symbol, for instance to find a method
7262 implemented in a stripped copy of libstdc++, will not work.
7263 If we do not have debug info for the definition, we will have to
7264 match them up some other way.
7265
7266 When we do name matching there is a related problem with function
7267 templates; two instantiated function templates are allowed to
7268 differ only by their return types, which we do not add here. */
7269
7270 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7271 {
7272 struct attribute *attr;
7273 struct die_info *child;
7274 int first = 1;
7275
7276 die->building_fullname = 1;
7277
7278 for (child = die->child; child != NULL; child = child->sibling)
7279 {
7280 struct type *type;
7281 LONGEST value;
7282 gdb_byte *bytes;
7283 struct dwarf2_locexpr_baton *baton;
7284 struct value *v;
7285
7286 if (child->tag != DW_TAG_template_type_param
7287 && child->tag != DW_TAG_template_value_param)
7288 continue;
7289
7290 if (first)
7291 {
7292 fputs_unfiltered ("<", buf);
7293 first = 0;
7294 }
7295 else
7296 fputs_unfiltered (", ", buf);
7297
7298 attr = dwarf2_attr (child, DW_AT_type, cu);
7299 if (attr == NULL)
7300 {
7301 complaint (&symfile_complaints,
7302 _("template parameter missing DW_AT_type"));
7303 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7304 continue;
7305 }
7306 type = die_type (child, cu);
7307
7308 if (child->tag == DW_TAG_template_type_param)
7309 {
7310 c_print_type (type, "", buf, -1, 0);
7311 continue;
7312 }
7313
7314 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7315 if (attr == NULL)
7316 {
7317 complaint (&symfile_complaints,
7318 _("template parameter missing "
7319 "DW_AT_const_value"));
7320 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7321 continue;
7322 }
7323
7324 dwarf2_const_value_attr (attr, type, name,
7325 &cu->comp_unit_obstack, cu,
7326 &value, &bytes, &baton);
7327
7328 if (TYPE_NOSIGN (type))
7329 /* GDB prints characters as NUMBER 'CHAR'. If that's
7330 changed, this can use value_print instead. */
7331 c_printchar (value, type, buf);
7332 else
7333 {
7334 struct value_print_options opts;
7335
7336 if (baton != NULL)
7337 v = dwarf2_evaluate_loc_desc (type, NULL,
7338 baton->data,
7339 baton->size,
7340 baton->per_cu);
7341 else if (bytes != NULL)
7342 {
7343 v = allocate_value (type);
7344 memcpy (value_contents_writeable (v), bytes,
7345 TYPE_LENGTH (type));
7346 }
7347 else
7348 v = value_from_longest (type, value);
7349
7350 /* Specify decimal so that we do not depend on
7351 the radix. */
7352 get_formatted_print_options (&opts, 'd');
7353 opts.raw = 1;
7354 value_print (v, buf, &opts);
7355 release_value (v);
7356 value_free (v);
7357 }
7358 }
7359
7360 die->building_fullname = 0;
7361
7362 if (!first)
7363 {
7364 /* Close the argument list, with a space if necessary
7365 (nested templates). */
7366 char last_char = '\0';
7367 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7368 if (last_char == '>')
7369 fputs_unfiltered (" >", buf);
7370 else
7371 fputs_unfiltered (">", buf);
7372 }
7373 }
7374
7375 /* For Java and C++ methods, append formal parameter type
7376 information, if PHYSNAME. */
7377
7378 if (physname && die->tag == DW_TAG_subprogram
7379 && (cu->language == language_cplus
7380 || cu->language == language_java))
7381 {
7382 struct type *type = read_type_die (die, cu);
7383
7384 c_type_print_args (type, buf, 1, cu->language);
7385
7386 if (cu->language == language_java)
7387 {
7388 /* For java, we must append the return type to method
7389 names. */
7390 if (die->tag == DW_TAG_subprogram)
7391 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7392 0, 0);
7393 }
7394 else if (cu->language == language_cplus)
7395 {
7396 /* Assume that an artificial first parameter is
7397 "this", but do not crash if it is not. RealView
7398 marks unnamed (and thus unused) parameters as
7399 artificial; there is no way to differentiate
7400 the two cases. */
7401 if (TYPE_NFIELDS (type) > 0
7402 && TYPE_FIELD_ARTIFICIAL (type, 0)
7403 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7404 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7405 0))))
7406 fputs_unfiltered (" const", buf);
7407 }
7408 }
7409
7410 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7411 &length);
7412 ui_file_delete (buf);
7413
7414 if (cu->language == language_cplus)
7415 {
7416 char *cname
7417 = dwarf2_canonicalize_name (name, cu,
7418 &objfile->objfile_obstack);
7419
7420 if (cname != NULL)
7421 name = cname;
7422 }
7423 }
7424 }
7425
7426 return name;
7427 }
7428
7429 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7430 If scope qualifiers are appropriate they will be added. The result
7431 will be allocated on the objfile_obstack, or NULL if the DIE does
7432 not have a name. NAME may either be from a previous call to
7433 dwarf2_name or NULL.
7434
7435 The output string will be canonicalized (if C++/Java). */
7436
7437 static const char *
7438 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7439 {
7440 return dwarf2_compute_name (name, die, cu, 0);
7441 }
7442
7443 /* Construct a physname for the given DIE in CU. NAME may either be
7444 from a previous call to dwarf2_name or NULL. The result will be
7445 allocated on the objfile_objstack or NULL if the DIE does not have a
7446 name.
7447
7448 The output string will be canonicalized (if C++/Java). */
7449
7450 static const char *
7451 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7452 {
7453 struct objfile *objfile = cu->objfile;
7454 struct attribute *attr;
7455 const char *retval, *mangled = NULL, *canon = NULL;
7456 struct cleanup *back_to;
7457 int need_copy = 1;
7458
7459 /* In this case dwarf2_compute_name is just a shortcut not building anything
7460 on its own. */
7461 if (!die_needs_namespace (die, cu))
7462 return dwarf2_compute_name (name, die, cu, 1);
7463
7464 back_to = make_cleanup (null_cleanup, NULL);
7465
7466 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7467 if (!attr)
7468 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7469
7470 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7471 has computed. */
7472 if (attr && DW_STRING (attr))
7473 {
7474 char *demangled;
7475
7476 mangled = DW_STRING (attr);
7477
7478 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7479 type. It is easier for GDB users to search for such functions as
7480 `name(params)' than `long name(params)'. In such case the minimal
7481 symbol names do not match the full symbol names but for template
7482 functions there is never a need to look up their definition from their
7483 declaration so the only disadvantage remains the minimal symbol
7484 variant `long name(params)' does not have the proper inferior type.
7485 */
7486
7487 if (cu->language == language_go)
7488 {
7489 /* This is a lie, but we already lie to the caller new_symbol_full.
7490 new_symbol_full assumes we return the mangled name.
7491 This just undoes that lie until things are cleaned up. */
7492 demangled = NULL;
7493 }
7494 else
7495 {
7496 demangled = cplus_demangle (mangled,
7497 (DMGL_PARAMS | DMGL_ANSI
7498 | (cu->language == language_java
7499 ? DMGL_JAVA | DMGL_RET_POSTFIX
7500 : DMGL_RET_DROP)));
7501 }
7502 if (demangled)
7503 {
7504 make_cleanup (xfree, demangled);
7505 canon = demangled;
7506 }
7507 else
7508 {
7509 canon = mangled;
7510 need_copy = 0;
7511 }
7512 }
7513
7514 if (canon == NULL || check_physname)
7515 {
7516 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7517
7518 if (canon != NULL && strcmp (physname, canon) != 0)
7519 {
7520 /* It may not mean a bug in GDB. The compiler could also
7521 compute DW_AT_linkage_name incorrectly. But in such case
7522 GDB would need to be bug-to-bug compatible. */
7523
7524 complaint (&symfile_complaints,
7525 _("Computed physname <%s> does not match demangled <%s> "
7526 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7527 physname, canon, mangled, die->offset.sect_off, objfile->name);
7528
7529 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7530 is available here - over computed PHYSNAME. It is safer
7531 against both buggy GDB and buggy compilers. */
7532
7533 retval = canon;
7534 }
7535 else
7536 {
7537 retval = physname;
7538 need_copy = 0;
7539 }
7540 }
7541 else
7542 retval = canon;
7543
7544 if (need_copy)
7545 retval = obsavestring (retval, strlen (retval),
7546 &objfile->objfile_obstack);
7547
7548 do_cleanups (back_to);
7549 return retval;
7550 }
7551
7552 /* Read the import statement specified by the given die and record it. */
7553
7554 static void
7555 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7556 {
7557 struct objfile *objfile = cu->objfile;
7558 struct attribute *import_attr;
7559 struct die_info *imported_die, *child_die;
7560 struct dwarf2_cu *imported_cu;
7561 const char *imported_name;
7562 const char *imported_name_prefix;
7563 const char *canonical_name;
7564 const char *import_alias;
7565 const char *imported_declaration = NULL;
7566 const char *import_prefix;
7567 VEC (const_char_ptr) *excludes = NULL;
7568 struct cleanup *cleanups;
7569
7570 char *temp;
7571
7572 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7573 if (import_attr == NULL)
7574 {
7575 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7576 dwarf_tag_name (die->tag));
7577 return;
7578 }
7579
7580 imported_cu = cu;
7581 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7582 imported_name = dwarf2_name (imported_die, imported_cu);
7583 if (imported_name == NULL)
7584 {
7585 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7586
7587 The import in the following code:
7588 namespace A
7589 {
7590 typedef int B;
7591 }
7592
7593 int main ()
7594 {
7595 using A::B;
7596 B b;
7597 return b;
7598 }
7599
7600 ...
7601 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7602 <52> DW_AT_decl_file : 1
7603 <53> DW_AT_decl_line : 6
7604 <54> DW_AT_import : <0x75>
7605 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7606 <59> DW_AT_name : B
7607 <5b> DW_AT_decl_file : 1
7608 <5c> DW_AT_decl_line : 2
7609 <5d> DW_AT_type : <0x6e>
7610 ...
7611 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7612 <76> DW_AT_byte_size : 4
7613 <77> DW_AT_encoding : 5 (signed)
7614
7615 imports the wrong die ( 0x75 instead of 0x58 ).
7616 This case will be ignored until the gcc bug is fixed. */
7617 return;
7618 }
7619
7620 /* Figure out the local name after import. */
7621 import_alias = dwarf2_name (die, cu);
7622
7623 /* Figure out where the statement is being imported to. */
7624 import_prefix = determine_prefix (die, cu);
7625
7626 /* Figure out what the scope of the imported die is and prepend it
7627 to the name of the imported die. */
7628 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7629
7630 if (imported_die->tag != DW_TAG_namespace
7631 && imported_die->tag != DW_TAG_module)
7632 {
7633 imported_declaration = imported_name;
7634 canonical_name = imported_name_prefix;
7635 }
7636 else if (strlen (imported_name_prefix) > 0)
7637 {
7638 temp = alloca (strlen (imported_name_prefix)
7639 + 2 + strlen (imported_name) + 1);
7640 strcpy (temp, imported_name_prefix);
7641 strcat (temp, "::");
7642 strcat (temp, imported_name);
7643 canonical_name = temp;
7644 }
7645 else
7646 canonical_name = imported_name;
7647
7648 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7649
7650 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7651 for (child_die = die->child; child_die && child_die->tag;
7652 child_die = sibling_die (child_die))
7653 {
7654 /* DWARF-4: A Fortran use statement with a “rename list” may be
7655 represented by an imported module entry with an import attribute
7656 referring to the module and owned entries corresponding to those
7657 entities that are renamed as part of being imported. */
7658
7659 if (child_die->tag != DW_TAG_imported_declaration)
7660 {
7661 complaint (&symfile_complaints,
7662 _("child DW_TAG_imported_declaration expected "
7663 "- DIE at 0x%x [in module %s]"),
7664 child_die->offset.sect_off, objfile->name);
7665 continue;
7666 }
7667
7668 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7669 if (import_attr == NULL)
7670 {
7671 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7672 dwarf_tag_name (child_die->tag));
7673 continue;
7674 }
7675
7676 imported_cu = cu;
7677 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7678 &imported_cu);
7679 imported_name = dwarf2_name (imported_die, imported_cu);
7680 if (imported_name == NULL)
7681 {
7682 complaint (&symfile_complaints,
7683 _("child DW_TAG_imported_declaration has unknown "
7684 "imported name - DIE at 0x%x [in module %s]"),
7685 child_die->offset.sect_off, objfile->name);
7686 continue;
7687 }
7688
7689 VEC_safe_push (const_char_ptr, excludes, imported_name);
7690
7691 process_die (child_die, cu);
7692 }
7693
7694 cp_add_using_directive (import_prefix,
7695 canonical_name,
7696 import_alias,
7697 imported_declaration,
7698 excludes,
7699 &objfile->objfile_obstack);
7700
7701 do_cleanups (cleanups);
7702 }
7703
7704 /* Cleanup function for handle_DW_AT_stmt_list. */
7705
7706 static void
7707 free_cu_line_header (void *arg)
7708 {
7709 struct dwarf2_cu *cu = arg;
7710
7711 free_line_header (cu->line_header);
7712 cu->line_header = NULL;
7713 }
7714
7715 static void
7716 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7717 char **name, char **comp_dir)
7718 {
7719 struct attribute *attr;
7720
7721 *name = NULL;
7722 *comp_dir = NULL;
7723
7724 /* Find the filename. Do not use dwarf2_name here, since the filename
7725 is not a source language identifier. */
7726 attr = dwarf2_attr (die, DW_AT_name, cu);
7727 if (attr)
7728 {
7729 *name = DW_STRING (attr);
7730 }
7731
7732 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7733 if (attr)
7734 *comp_dir = DW_STRING (attr);
7735 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7736 {
7737 *comp_dir = ldirname (*name);
7738 if (*comp_dir != NULL)
7739 make_cleanup (xfree, *comp_dir);
7740 }
7741 if (*comp_dir != NULL)
7742 {
7743 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7744 directory, get rid of it. */
7745 char *cp = strchr (*comp_dir, ':');
7746
7747 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7748 *comp_dir = cp + 1;
7749 }
7750
7751 if (*name == NULL)
7752 *name = "<unknown>";
7753 }
7754
7755 /* Handle DW_AT_stmt_list for a compilation unit.
7756 DIE is the DW_TAG_compile_unit die for CU.
7757 COMP_DIR is the compilation directory.
7758 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7759
7760 static void
7761 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7762 const char *comp_dir)
7763 {
7764 struct attribute *attr;
7765
7766 gdb_assert (! cu->per_cu->is_debug_types);
7767
7768 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7769 if (attr)
7770 {
7771 unsigned int line_offset = DW_UNSND (attr);
7772 struct line_header *line_header
7773 = dwarf_decode_line_header (line_offset, cu);
7774
7775 if (line_header)
7776 {
7777 cu->line_header = line_header;
7778 make_cleanup (free_cu_line_header, cu);
7779 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7780 }
7781 }
7782 }
7783
7784 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7785
7786 static void
7787 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7788 {
7789 struct objfile *objfile = dwarf2_per_objfile->objfile;
7790 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7791 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7792 CORE_ADDR highpc = ((CORE_ADDR) 0);
7793 struct attribute *attr;
7794 char *name = NULL;
7795 char *comp_dir = NULL;
7796 struct die_info *child_die;
7797 bfd *abfd = objfile->obfd;
7798 CORE_ADDR baseaddr;
7799
7800 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7801
7802 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7803
7804 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7805 from finish_block. */
7806 if (lowpc == ((CORE_ADDR) -1))
7807 lowpc = highpc;
7808 lowpc += baseaddr;
7809 highpc += baseaddr;
7810
7811 find_file_and_directory (die, cu, &name, &comp_dir);
7812
7813 prepare_one_comp_unit (cu, die, cu->language);
7814
7815 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7816 standardised yet. As a workaround for the language detection we fall
7817 back to the DW_AT_producer string. */
7818 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7819 cu->language = language_opencl;
7820
7821 /* Similar hack for Go. */
7822 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7823 set_cu_language (DW_LANG_Go, cu);
7824
7825 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7826
7827 /* Decode line number information if present. We do this before
7828 processing child DIEs, so that the line header table is available
7829 for DW_AT_decl_file. */
7830 handle_DW_AT_stmt_list (die, cu, comp_dir);
7831
7832 /* Process all dies in compilation unit. */
7833 if (die->child != NULL)
7834 {
7835 child_die = die->child;
7836 while (child_die && child_die->tag)
7837 {
7838 process_die (child_die, cu);
7839 child_die = sibling_die (child_die);
7840 }
7841 }
7842
7843 /* Decode macro information, if present. Dwarf 2 macro information
7844 refers to information in the line number info statement program
7845 header, so we can only read it if we've read the header
7846 successfully. */
7847 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7848 if (attr && cu->line_header)
7849 {
7850 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7851 complaint (&symfile_complaints,
7852 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7853
7854 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7855 }
7856 else
7857 {
7858 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7859 if (attr && cu->line_header)
7860 {
7861 unsigned int macro_offset = DW_UNSND (attr);
7862
7863 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7864 }
7865 }
7866
7867 do_cleanups (back_to);
7868 }
7869
7870 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7871 Create the set of symtabs used by this TU, or if this TU is sharing
7872 symtabs with another TU and the symtabs have already been created
7873 then restore those symtabs in the line header.
7874 We don't need the pc/line-number mapping for type units. */
7875
7876 static void
7877 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7878 {
7879 struct objfile *objfile = dwarf2_per_objfile->objfile;
7880 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7881 struct type_unit_group *tu_group;
7882 int first_time;
7883 struct line_header *lh;
7884 struct attribute *attr;
7885 unsigned int i, line_offset;
7886
7887 gdb_assert (per_cu->is_debug_types);
7888
7889 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7890
7891 /* If we're using .gdb_index (includes -readnow) then
7892 per_cu->s.type_unit_group may not have been set up yet. */
7893 if (per_cu->s.type_unit_group == NULL)
7894 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7895 tu_group = per_cu->s.type_unit_group;
7896
7897 /* If we've already processed this stmt_list there's no real need to
7898 do it again, we could fake it and just recreate the part we need
7899 (file name,index -> symtab mapping). If data shows this optimization
7900 is useful we can do it then. */
7901 first_time = tu_group->primary_symtab == NULL;
7902
7903 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7904 debug info. */
7905 lh = NULL;
7906 if (attr != NULL)
7907 {
7908 line_offset = DW_UNSND (attr);
7909 lh = dwarf_decode_line_header (line_offset, cu);
7910 }
7911 if (lh == NULL)
7912 {
7913 if (first_time)
7914 dwarf2_start_symtab (cu, "", NULL, 0);
7915 else
7916 {
7917 gdb_assert (tu_group->symtabs == NULL);
7918 restart_symtab (0);
7919 }
7920 /* Note: The primary symtab will get allocated at the end. */
7921 return;
7922 }
7923
7924 cu->line_header = lh;
7925 make_cleanup (free_cu_line_header, cu);
7926
7927 if (first_time)
7928 {
7929 dwarf2_start_symtab (cu, "", NULL, 0);
7930
7931 tu_group->num_symtabs = lh->num_file_names;
7932 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7933
7934 for (i = 0; i < lh->num_file_names; ++i)
7935 {
7936 char *dir = NULL;
7937 struct file_entry *fe = &lh->file_names[i];
7938
7939 if (fe->dir_index)
7940 dir = lh->include_dirs[fe->dir_index - 1];
7941 dwarf2_start_subfile (fe->name, dir, NULL);
7942
7943 /* Note: We don't have to watch for the main subfile here, type units
7944 don't have DW_AT_name. */
7945
7946 if (current_subfile->symtab == NULL)
7947 {
7948 /* NOTE: start_subfile will recognize when it's been passed
7949 a file it has already seen. So we can't assume there's a
7950 simple mapping from lh->file_names to subfiles,
7951 lh->file_names may contain dups. */
7952 current_subfile->symtab = allocate_symtab (current_subfile->name,
7953 objfile);
7954 }
7955
7956 fe->symtab = current_subfile->symtab;
7957 tu_group->symtabs[i] = fe->symtab;
7958 }
7959 }
7960 else
7961 {
7962 restart_symtab (0);
7963
7964 for (i = 0; i < lh->num_file_names; ++i)
7965 {
7966 struct file_entry *fe = &lh->file_names[i];
7967
7968 fe->symtab = tu_group->symtabs[i];
7969 }
7970 }
7971
7972 /* The main symtab is allocated last. Type units don't have DW_AT_name
7973 so they don't have a "real" (so to speak) symtab anyway.
7974 There is later code that will assign the main symtab to all symbols
7975 that don't have one. We need to handle the case of a symbol with a
7976 missing symtab (DW_AT_decl_file) anyway. */
7977 }
7978
7979 /* Process DW_TAG_type_unit.
7980 For TUs we want to skip the first top level sibling if it's not the
7981 actual type being defined by this TU. In this case the first top
7982 level sibling is there to provide context only. */
7983
7984 static void
7985 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7986 {
7987 struct die_info *child_die;
7988
7989 prepare_one_comp_unit (cu, die, language_minimal);
7990
7991 /* Initialize (or reinitialize) the machinery for building symtabs.
7992 We do this before processing child DIEs, so that the line header table
7993 is available for DW_AT_decl_file. */
7994 setup_type_unit_groups (die, cu);
7995
7996 if (die->child != NULL)
7997 {
7998 child_die = die->child;
7999 while (child_die && child_die->tag)
8000 {
8001 process_die (child_die, cu);
8002 child_die = sibling_die (child_die);
8003 }
8004 }
8005 }
8006 \f
8007 /* DWO files. */
8008
8009 static hashval_t
8010 hash_dwo_file (const void *item)
8011 {
8012 const struct dwo_file *dwo_file = item;
8013
8014 return htab_hash_string (dwo_file->dwo_name);
8015 }
8016
8017 static int
8018 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8019 {
8020 const struct dwo_file *lhs = item_lhs;
8021 const struct dwo_file *rhs = item_rhs;
8022
8023 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8024 }
8025
8026 /* Allocate a hash table for DWO files. */
8027
8028 static htab_t
8029 allocate_dwo_file_hash_table (void)
8030 {
8031 struct objfile *objfile = dwarf2_per_objfile->objfile;
8032
8033 return htab_create_alloc_ex (41,
8034 hash_dwo_file,
8035 eq_dwo_file,
8036 NULL,
8037 &objfile->objfile_obstack,
8038 hashtab_obstack_allocate,
8039 dummy_obstack_deallocate);
8040 }
8041
8042 static hashval_t
8043 hash_dwo_unit (const void *item)
8044 {
8045 const struct dwo_unit *dwo_unit = item;
8046
8047 /* This drops the top 32 bits of the id, but is ok for a hash. */
8048 return dwo_unit->signature;
8049 }
8050
8051 static int
8052 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8053 {
8054 const struct dwo_unit *lhs = item_lhs;
8055 const struct dwo_unit *rhs = item_rhs;
8056
8057 /* The signature is assumed to be unique within the DWO file.
8058 So while object file CU dwo_id's always have the value zero,
8059 that's OK, assuming each object file DWO file has only one CU,
8060 and that's the rule for now. */
8061 return lhs->signature == rhs->signature;
8062 }
8063
8064 /* Allocate a hash table for DWO CUs,TUs.
8065 There is one of these tables for each of CUs,TUs for each DWO file. */
8066
8067 static htab_t
8068 allocate_dwo_unit_table (struct objfile *objfile)
8069 {
8070 /* Start out with a pretty small number.
8071 Generally DWO files contain only one CU and maybe some TUs. */
8072 return htab_create_alloc_ex (3,
8073 hash_dwo_unit,
8074 eq_dwo_unit,
8075 NULL,
8076 &objfile->objfile_obstack,
8077 hashtab_obstack_allocate,
8078 dummy_obstack_deallocate);
8079 }
8080
8081 /* This function is mapped across the sections and remembers the offset and
8082 size of each of the DWO debugging sections we are interested in. */
8083
8084 static void
8085 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8086 {
8087 struct dwo_file *dwo_file = dwo_file_ptr;
8088 const struct dwo_section_names *names = &dwo_section_names;
8089
8090 if (section_is_p (sectp->name, &names->abbrev_dwo))
8091 {
8092 dwo_file->sections.abbrev.asection = sectp;
8093 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8094 }
8095 else if (section_is_p (sectp->name, &names->info_dwo))
8096 {
8097 dwo_file->sections.info.asection = sectp;
8098 dwo_file->sections.info.size = bfd_get_section_size (sectp);
8099 }
8100 else if (section_is_p (sectp->name, &names->line_dwo))
8101 {
8102 dwo_file->sections.line.asection = sectp;
8103 dwo_file->sections.line.size = bfd_get_section_size (sectp);
8104 }
8105 else if (section_is_p (sectp->name, &names->loc_dwo))
8106 {
8107 dwo_file->sections.loc.asection = sectp;
8108 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8109 }
8110 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8111 {
8112 dwo_file->sections.macinfo.asection = sectp;
8113 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8114 }
8115 else if (section_is_p (sectp->name, &names->macro_dwo))
8116 {
8117 dwo_file->sections.macro.asection = sectp;
8118 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8119 }
8120 else if (section_is_p (sectp->name, &names->str_dwo))
8121 {
8122 dwo_file->sections.str.asection = sectp;
8123 dwo_file->sections.str.size = bfd_get_section_size (sectp);
8124 }
8125 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8126 {
8127 dwo_file->sections.str_offsets.asection = sectp;
8128 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8129 }
8130 else if (section_is_p (sectp->name, &names->types_dwo))
8131 {
8132 struct dwarf2_section_info type_section;
8133
8134 memset (&type_section, 0, sizeof (type_section));
8135 type_section.asection = sectp;
8136 type_section.size = bfd_get_section_size (sectp);
8137 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8138 &type_section);
8139 }
8140 }
8141
8142 /* Structure used to pass data to create_debug_info_hash_table_reader. */
8143
8144 struct create_dwo_info_table_data
8145 {
8146 struct dwo_file *dwo_file;
8147 htab_t cu_htab;
8148 };
8149
8150 /* die_reader_func for create_debug_info_hash_table. */
8151
8152 static void
8153 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8154 gdb_byte *info_ptr,
8155 struct die_info *comp_unit_die,
8156 int has_children,
8157 void *datap)
8158 {
8159 struct dwarf2_cu *cu = reader->cu;
8160 struct objfile *objfile = dwarf2_per_objfile->objfile;
8161 sect_offset offset = cu->per_cu->offset;
8162 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8163 struct create_dwo_info_table_data *data = datap;
8164 struct dwo_file *dwo_file = data->dwo_file;
8165 htab_t cu_htab = data->cu_htab;
8166 void **slot;
8167 struct attribute *attr;
8168 struct dwo_unit *dwo_unit;
8169
8170 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8171 if (attr == NULL)
8172 {
8173 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8174 " its dwo_id [in module %s]"),
8175 offset.sect_off, dwo_file->dwo_name);
8176 return;
8177 }
8178
8179 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8180 dwo_unit->dwo_file = dwo_file;
8181 dwo_unit->signature = DW_UNSND (attr);
8182 dwo_unit->info_or_types_section = section;
8183 dwo_unit->offset = offset;
8184 dwo_unit->length = cu->per_cu->length;
8185
8186 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8187 gdb_assert (slot != NULL);
8188 if (*slot != NULL)
8189 {
8190 const struct dwo_unit *dup_dwo_unit = *slot;
8191
8192 complaint (&symfile_complaints,
8193 _("debug entry at offset 0x%x is duplicate to the entry at"
8194 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8195 offset.sect_off, dup_dwo_unit->offset.sect_off,
8196 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8197 dwo_file->dwo_name);
8198 }
8199 else
8200 *slot = dwo_unit;
8201
8202 if (dwarf2_read_debug)
8203 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8204 offset.sect_off,
8205 phex (dwo_unit->signature,
8206 sizeof (dwo_unit->signature)));
8207 }
8208
8209 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
8210
8211 static htab_t
8212 create_debug_info_hash_table (struct dwo_file *dwo_file)
8213 {
8214 struct objfile *objfile = dwarf2_per_objfile->objfile;
8215 struct dwarf2_section_info *section = &dwo_file->sections.info;
8216 bfd *abfd;
8217 htab_t cu_htab;
8218 gdb_byte *info_ptr, *end_ptr;
8219 struct create_dwo_info_table_data create_dwo_info_table_data;
8220
8221 dwarf2_read_section (objfile, section);
8222 info_ptr = section->buffer;
8223
8224 if (info_ptr == NULL)
8225 return NULL;
8226
8227 /* We can't set abfd until now because the section may be empty or
8228 not present, in which case section->asection will be NULL. */
8229 abfd = section->asection->owner;
8230
8231 if (dwarf2_read_debug)
8232 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8233 bfd_get_filename (abfd));
8234
8235 cu_htab = allocate_dwo_unit_table (objfile);
8236
8237 create_dwo_info_table_data.dwo_file = dwo_file;
8238 create_dwo_info_table_data.cu_htab = cu_htab;
8239
8240 end_ptr = info_ptr + section->size;
8241 while (info_ptr < end_ptr)
8242 {
8243 struct dwarf2_per_cu_data per_cu;
8244
8245 memset (&per_cu, 0, sizeof (per_cu));
8246 per_cu.objfile = objfile;
8247 per_cu.is_debug_types = 0;
8248 per_cu.offset.sect_off = info_ptr - section->buffer;
8249 per_cu.info_or_types_section = section;
8250
8251 init_cutu_and_read_dies_no_follow (&per_cu,
8252 &dwo_file->sections.abbrev,
8253 dwo_file,
8254 create_debug_info_hash_table_reader,
8255 &create_dwo_info_table_data);
8256
8257 info_ptr += per_cu.length;
8258 }
8259
8260 return cu_htab;
8261 }
8262
8263 /* Subroutine of open_dwo_file to simplify it.
8264 Open the file specified by FILE_NAME and hand it off to BFD for
8265 preliminary analysis. Return a newly initialized bfd *, which
8266 includes a canonicalized copy of FILE_NAME.
8267 In case of trouble, return NULL.
8268 NOTE: This function is derived from symfile_bfd_open. */
8269
8270 static bfd *
8271 try_open_dwo_file (const char *file_name)
8272 {
8273 bfd *sym_bfd;
8274 int desc;
8275 char *absolute_name;
8276
8277 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8278 O_RDONLY | O_BINARY, &absolute_name);
8279 if (desc < 0)
8280 return NULL;
8281
8282 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8283 if (!sym_bfd)
8284 {
8285 xfree (absolute_name);
8286 return NULL;
8287 }
8288 xfree (absolute_name);
8289 bfd_set_cacheable (sym_bfd, 1);
8290
8291 if (!bfd_check_format (sym_bfd, bfd_object))
8292 {
8293 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8294 return NULL;
8295 }
8296
8297 return sym_bfd;
8298 }
8299
8300 /* Try to open DWO file DWO_NAME.
8301 COMP_DIR is the DW_AT_comp_dir attribute.
8302 The result is the bfd handle of the file.
8303 If there is a problem finding or opening the file, return NULL.
8304 Upon success, the canonicalized path of the file is stored in the bfd,
8305 same as symfile_bfd_open. */
8306
8307 static bfd *
8308 open_dwo_file (const char *dwo_name, const char *comp_dir)
8309 {
8310 bfd *abfd;
8311
8312 if (IS_ABSOLUTE_PATH (dwo_name))
8313 return try_open_dwo_file (dwo_name);
8314
8315 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8316
8317 if (comp_dir != NULL)
8318 {
8319 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8320
8321 /* NOTE: If comp_dir is a relative path, this will also try the
8322 search path, which seems useful. */
8323 abfd = try_open_dwo_file (path_to_try);
8324 xfree (path_to_try);
8325 if (abfd != NULL)
8326 return abfd;
8327 }
8328
8329 /* That didn't work, try debug-file-directory, which, despite its name,
8330 is a list of paths. */
8331
8332 if (*debug_file_directory == '\0')
8333 return NULL;
8334
8335 return try_open_dwo_file (dwo_name);
8336 }
8337
8338 /* Initialize the use of the DWO file specified by DWO_NAME. */
8339
8340 static struct dwo_file *
8341 init_dwo_file (const char *dwo_name, const char *comp_dir)
8342 {
8343 struct objfile *objfile = dwarf2_per_objfile->objfile;
8344 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8345 struct dwo_file);
8346 bfd *abfd;
8347 struct cleanup *cleanups;
8348
8349 if (dwarf2_read_debug)
8350 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8351
8352 abfd = open_dwo_file (dwo_name, comp_dir);
8353 if (abfd == NULL)
8354 return NULL;
8355 dwo_file->dwo_name = dwo_name;
8356 dwo_file->dwo_bfd = abfd;
8357
8358 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8359
8360 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8361
8362 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8363
8364 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8365 dwo_file->sections.types);
8366
8367 discard_cleanups (cleanups);
8368
8369 return dwo_file;
8370 }
8371
8372 /* Lookup DWO file DWO_NAME. */
8373
8374 static struct dwo_file *
8375 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8376 {
8377 struct dwo_file *dwo_file;
8378 struct dwo_file find_entry;
8379 void **slot;
8380
8381 if (dwarf2_per_objfile->dwo_files == NULL)
8382 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8383
8384 /* Have we already seen this DWO file? */
8385 find_entry.dwo_name = dwo_name;
8386 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8387
8388 /* If not, read it in and build a table of the DWOs it contains. */
8389 if (*slot == NULL)
8390 *slot = init_dwo_file (dwo_name, comp_dir);
8391
8392 /* NOTE: This will be NULL if unable to open the file. */
8393 dwo_file = *slot;
8394
8395 return dwo_file;
8396 }
8397
8398 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8399 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8400 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8401 nomenclature as TUs).
8402 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8403 (dwo_id mismatch or couldn't find the DWO file). */
8404
8405 static struct dwo_unit *
8406 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8407 char *dwo_name, const char *comp_dir,
8408 ULONGEST signature)
8409 {
8410 struct objfile *objfile = dwarf2_per_objfile->objfile;
8411 struct dwo_file *dwo_file;
8412
8413 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8414 if (dwo_file == NULL)
8415 return NULL;
8416
8417 /* Look up the DWO using its signature(dwo_id). */
8418
8419 if (dwo_file->cus != NULL)
8420 {
8421 struct dwo_unit find_dwo_cu, *dwo_cu;
8422
8423 find_dwo_cu.signature = signature;
8424 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8425
8426 if (dwo_cu != NULL)
8427 return dwo_cu;
8428 }
8429
8430 /* We didn't find it. This must mean a dwo_id mismatch. */
8431
8432 complaint (&symfile_complaints,
8433 _("Could not find DWO CU referenced by CU at offset 0x%x"
8434 " [in module %s]"),
8435 this_cu->offset.sect_off, objfile->name);
8436 return NULL;
8437 }
8438
8439 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8440 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8441 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8442 (dwo_id mismatch or couldn't find the DWO file). */
8443
8444 static struct dwo_unit *
8445 lookup_dwo_type_unit (struct signatured_type *this_tu,
8446 char *dwo_name, const char *comp_dir)
8447 {
8448 struct objfile *objfile = dwarf2_per_objfile->objfile;
8449 struct dwo_file *dwo_file;
8450
8451 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8452 if (dwo_file == NULL)
8453 return NULL;
8454
8455 /* Look up the DWO using its signature(dwo_id). */
8456
8457 if (dwo_file->tus != NULL)
8458 {
8459 struct dwo_unit find_dwo_tu, *dwo_tu;
8460
8461 find_dwo_tu.signature = this_tu->signature;
8462 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8463
8464 if (dwo_tu != NULL)
8465 return dwo_tu;
8466 }
8467
8468 /* We didn't find it. This must mean a dwo_id mismatch. */
8469
8470 complaint (&symfile_complaints,
8471 _("Could not find DWO TU referenced by TU at offset 0x%x"
8472 " [in module %s]"),
8473 this_tu->per_cu.offset.sect_off, objfile->name);
8474 return NULL;
8475 }
8476
8477 /* Free all resources associated with DWO_FILE.
8478 Close the DWO file and munmap the sections.
8479 All memory should be on the objfile obstack. */
8480
8481 static void
8482 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8483 {
8484 int ix;
8485 struct dwarf2_section_info *section;
8486
8487 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8488 gdb_bfd_unref (dwo_file->dwo_bfd);
8489
8490 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8491 }
8492
8493 /* Wrapper for free_dwo_file for use in cleanups. */
8494
8495 static void
8496 free_dwo_file_cleanup (void *arg)
8497 {
8498 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8499 struct objfile *objfile = dwarf2_per_objfile->objfile;
8500
8501 free_dwo_file (dwo_file, objfile);
8502 }
8503
8504 /* Traversal function for free_dwo_files. */
8505
8506 static int
8507 free_dwo_file_from_slot (void **slot, void *info)
8508 {
8509 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8510 struct objfile *objfile = (struct objfile *) info;
8511
8512 free_dwo_file (dwo_file, objfile);
8513
8514 return 1;
8515 }
8516
8517 /* Free all resources associated with DWO_FILES. */
8518
8519 static void
8520 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8521 {
8522 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8523 }
8524 \f
8525 /* Read in various DIEs. */
8526
8527 /* qsort helper for inherit_abstract_dies. */
8528
8529 static int
8530 unsigned_int_compar (const void *ap, const void *bp)
8531 {
8532 unsigned int a = *(unsigned int *) ap;
8533 unsigned int b = *(unsigned int *) bp;
8534
8535 return (a > b) - (b > a);
8536 }
8537
8538 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8539 Inherit only the children of the DW_AT_abstract_origin DIE not being
8540 already referenced by DW_AT_abstract_origin from the children of the
8541 current DIE. */
8542
8543 static void
8544 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8545 {
8546 struct die_info *child_die;
8547 unsigned die_children_count;
8548 /* CU offsets which were referenced by children of the current DIE. */
8549 sect_offset *offsets;
8550 sect_offset *offsets_end, *offsetp;
8551 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8552 struct die_info *origin_die;
8553 /* Iterator of the ORIGIN_DIE children. */
8554 struct die_info *origin_child_die;
8555 struct cleanup *cleanups;
8556 struct attribute *attr;
8557 struct dwarf2_cu *origin_cu;
8558 struct pending **origin_previous_list_in_scope;
8559
8560 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8561 if (!attr)
8562 return;
8563
8564 /* Note that following die references may follow to a die in a
8565 different cu. */
8566
8567 origin_cu = cu;
8568 origin_die = follow_die_ref (die, attr, &origin_cu);
8569
8570 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8571 symbols in. */
8572 origin_previous_list_in_scope = origin_cu->list_in_scope;
8573 origin_cu->list_in_scope = cu->list_in_scope;
8574
8575 if (die->tag != origin_die->tag
8576 && !(die->tag == DW_TAG_inlined_subroutine
8577 && origin_die->tag == DW_TAG_subprogram))
8578 complaint (&symfile_complaints,
8579 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8580 die->offset.sect_off, origin_die->offset.sect_off);
8581
8582 child_die = die->child;
8583 die_children_count = 0;
8584 while (child_die && child_die->tag)
8585 {
8586 child_die = sibling_die (child_die);
8587 die_children_count++;
8588 }
8589 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8590 cleanups = make_cleanup (xfree, offsets);
8591
8592 offsets_end = offsets;
8593 child_die = die->child;
8594 while (child_die && child_die->tag)
8595 {
8596 /* For each CHILD_DIE, find the corresponding child of
8597 ORIGIN_DIE. If there is more than one layer of
8598 DW_AT_abstract_origin, follow them all; there shouldn't be,
8599 but GCC versions at least through 4.4 generate this (GCC PR
8600 40573). */
8601 struct die_info *child_origin_die = child_die;
8602 struct dwarf2_cu *child_origin_cu = cu;
8603
8604 while (1)
8605 {
8606 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8607 child_origin_cu);
8608 if (attr == NULL)
8609 break;
8610 child_origin_die = follow_die_ref (child_origin_die, attr,
8611 &child_origin_cu);
8612 }
8613
8614 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8615 counterpart may exist. */
8616 if (child_origin_die != child_die)
8617 {
8618 if (child_die->tag != child_origin_die->tag
8619 && !(child_die->tag == DW_TAG_inlined_subroutine
8620 && child_origin_die->tag == DW_TAG_subprogram))
8621 complaint (&symfile_complaints,
8622 _("Child DIE 0x%x and its abstract origin 0x%x have "
8623 "different tags"), child_die->offset.sect_off,
8624 child_origin_die->offset.sect_off);
8625 if (child_origin_die->parent != origin_die)
8626 complaint (&symfile_complaints,
8627 _("Child DIE 0x%x and its abstract origin 0x%x have "
8628 "different parents"), child_die->offset.sect_off,
8629 child_origin_die->offset.sect_off);
8630 else
8631 *offsets_end++ = child_origin_die->offset;
8632 }
8633 child_die = sibling_die (child_die);
8634 }
8635 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8636 unsigned_int_compar);
8637 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8638 if (offsetp[-1].sect_off == offsetp->sect_off)
8639 complaint (&symfile_complaints,
8640 _("Multiple children of DIE 0x%x refer "
8641 "to DIE 0x%x as their abstract origin"),
8642 die->offset.sect_off, offsetp->sect_off);
8643
8644 offsetp = offsets;
8645 origin_child_die = origin_die->child;
8646 while (origin_child_die && origin_child_die->tag)
8647 {
8648 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
8649 while (offsetp < offsets_end
8650 && offsetp->sect_off < origin_child_die->offset.sect_off)
8651 offsetp++;
8652 if (offsetp >= offsets_end
8653 || offsetp->sect_off > origin_child_die->offset.sect_off)
8654 {
8655 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
8656 process_die (origin_child_die, origin_cu);
8657 }
8658 origin_child_die = sibling_die (origin_child_die);
8659 }
8660 origin_cu->list_in_scope = origin_previous_list_in_scope;
8661
8662 do_cleanups (cleanups);
8663 }
8664
8665 static void
8666 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8667 {
8668 struct objfile *objfile = cu->objfile;
8669 struct context_stack *new;
8670 CORE_ADDR lowpc;
8671 CORE_ADDR highpc;
8672 struct die_info *child_die;
8673 struct attribute *attr, *call_line, *call_file;
8674 char *name;
8675 CORE_ADDR baseaddr;
8676 struct block *block;
8677 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8678 VEC (symbolp) *template_args = NULL;
8679 struct template_symbol *templ_func = NULL;
8680
8681 if (inlined_func)
8682 {
8683 /* If we do not have call site information, we can't show the
8684 caller of this inlined function. That's too confusing, so
8685 only use the scope for local variables. */
8686 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8687 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8688 if (call_line == NULL || call_file == NULL)
8689 {
8690 read_lexical_block_scope (die, cu);
8691 return;
8692 }
8693 }
8694
8695 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8696
8697 name = dwarf2_name (die, cu);
8698
8699 /* Ignore functions with missing or empty names. These are actually
8700 illegal according to the DWARF standard. */
8701 if (name == NULL)
8702 {
8703 complaint (&symfile_complaints,
8704 _("missing name for subprogram DIE at %d"),
8705 die->offset.sect_off);
8706 return;
8707 }
8708
8709 /* Ignore functions with missing or invalid low and high pc attributes. */
8710 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8711 {
8712 attr = dwarf2_attr (die, DW_AT_external, cu);
8713 if (!attr || !DW_UNSND (attr))
8714 complaint (&symfile_complaints,
8715 _("cannot get low and high bounds "
8716 "for subprogram DIE at %d"),
8717 die->offset.sect_off);
8718 return;
8719 }
8720
8721 lowpc += baseaddr;
8722 highpc += baseaddr;
8723
8724 /* If we have any template arguments, then we must allocate a
8725 different sort of symbol. */
8726 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8727 {
8728 if (child_die->tag == DW_TAG_template_type_param
8729 || child_die->tag == DW_TAG_template_value_param)
8730 {
8731 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8732 struct template_symbol);
8733 templ_func->base.is_cplus_template_function = 1;
8734 break;
8735 }
8736 }
8737
8738 new = push_context (0, lowpc);
8739 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8740 (struct symbol *) templ_func);
8741
8742 /* If there is a location expression for DW_AT_frame_base, record
8743 it. */
8744 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8745 if (attr)
8746 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8747 expression is being recorded directly in the function's symbol
8748 and not in a separate frame-base object. I guess this hack is
8749 to avoid adding some sort of frame-base adjunct/annex to the
8750 function's symbol :-(. The problem with doing this is that it
8751 results in a function symbol with a location expression that
8752 has nothing to do with the location of the function, ouch! The
8753 relationship should be: a function's symbol has-a frame base; a
8754 frame-base has-a location expression. */
8755 dwarf2_symbol_mark_computed (attr, new->name, cu);
8756
8757 cu->list_in_scope = &local_symbols;
8758
8759 if (die->child != NULL)
8760 {
8761 child_die = die->child;
8762 while (child_die && child_die->tag)
8763 {
8764 if (child_die->tag == DW_TAG_template_type_param
8765 || child_die->tag == DW_TAG_template_value_param)
8766 {
8767 struct symbol *arg = new_symbol (child_die, NULL, cu);
8768
8769 if (arg != NULL)
8770 VEC_safe_push (symbolp, template_args, arg);
8771 }
8772 else
8773 process_die (child_die, cu);
8774 child_die = sibling_die (child_die);
8775 }
8776 }
8777
8778 inherit_abstract_dies (die, cu);
8779
8780 /* If we have a DW_AT_specification, we might need to import using
8781 directives from the context of the specification DIE. See the
8782 comment in determine_prefix. */
8783 if (cu->language == language_cplus
8784 && dwarf2_attr (die, DW_AT_specification, cu))
8785 {
8786 struct dwarf2_cu *spec_cu = cu;
8787 struct die_info *spec_die = die_specification (die, &spec_cu);
8788
8789 while (spec_die)
8790 {
8791 child_die = spec_die->child;
8792 while (child_die && child_die->tag)
8793 {
8794 if (child_die->tag == DW_TAG_imported_module)
8795 process_die (child_die, spec_cu);
8796 child_die = sibling_die (child_die);
8797 }
8798
8799 /* In some cases, GCC generates specification DIEs that
8800 themselves contain DW_AT_specification attributes. */
8801 spec_die = die_specification (spec_die, &spec_cu);
8802 }
8803 }
8804
8805 new = pop_context ();
8806 /* Make a block for the local symbols within. */
8807 block = finish_block (new->name, &local_symbols, new->old_blocks,
8808 lowpc, highpc, objfile);
8809
8810 /* For C++, set the block's scope. */
8811 if (cu->language == language_cplus || cu->language == language_fortran)
8812 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8813 determine_prefix (die, cu),
8814 processing_has_namespace_info);
8815
8816 /* If we have address ranges, record them. */
8817 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8818
8819 /* Attach template arguments to function. */
8820 if (! VEC_empty (symbolp, template_args))
8821 {
8822 gdb_assert (templ_func != NULL);
8823
8824 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8825 templ_func->template_arguments
8826 = obstack_alloc (&objfile->objfile_obstack,
8827 (templ_func->n_template_arguments
8828 * sizeof (struct symbol *)));
8829 memcpy (templ_func->template_arguments,
8830 VEC_address (symbolp, template_args),
8831 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8832 VEC_free (symbolp, template_args);
8833 }
8834
8835 /* In C++, we can have functions nested inside functions (e.g., when
8836 a function declares a class that has methods). This means that
8837 when we finish processing a function scope, we may need to go
8838 back to building a containing block's symbol lists. */
8839 local_symbols = new->locals;
8840 param_symbols = new->params;
8841 using_directives = new->using_directives;
8842
8843 /* If we've finished processing a top-level function, subsequent
8844 symbols go in the file symbol list. */
8845 if (outermost_context_p ())
8846 cu->list_in_scope = &file_symbols;
8847 }
8848
8849 /* Process all the DIES contained within a lexical block scope. Start
8850 a new scope, process the dies, and then close the scope. */
8851
8852 static void
8853 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8854 {
8855 struct objfile *objfile = cu->objfile;
8856 struct context_stack *new;
8857 CORE_ADDR lowpc, highpc;
8858 struct die_info *child_die;
8859 CORE_ADDR baseaddr;
8860
8861 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8862
8863 /* Ignore blocks with missing or invalid low and high pc attributes. */
8864 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8865 as multiple lexical blocks? Handling children in a sane way would
8866 be nasty. Might be easier to properly extend generic blocks to
8867 describe ranges. */
8868 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8869 return;
8870 lowpc += baseaddr;
8871 highpc += baseaddr;
8872
8873 push_context (0, lowpc);
8874 if (die->child != NULL)
8875 {
8876 child_die = die->child;
8877 while (child_die && child_die->tag)
8878 {
8879 process_die (child_die, cu);
8880 child_die = sibling_die (child_die);
8881 }
8882 }
8883 new = pop_context ();
8884
8885 if (local_symbols != NULL || using_directives != NULL)
8886 {
8887 struct block *block
8888 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8889 highpc, objfile);
8890
8891 /* Note that recording ranges after traversing children, as we
8892 do here, means that recording a parent's ranges entails
8893 walking across all its children's ranges as they appear in
8894 the address map, which is quadratic behavior.
8895
8896 It would be nicer to record the parent's ranges before
8897 traversing its children, simply overriding whatever you find
8898 there. But since we don't even decide whether to create a
8899 block until after we've traversed its children, that's hard
8900 to do. */
8901 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8902 }
8903 local_symbols = new->locals;
8904 using_directives = new->using_directives;
8905 }
8906
8907 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8908
8909 static void
8910 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8911 {
8912 struct objfile *objfile = cu->objfile;
8913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8914 CORE_ADDR pc, baseaddr;
8915 struct attribute *attr;
8916 struct call_site *call_site, call_site_local;
8917 void **slot;
8918 int nparams;
8919 struct die_info *child_die;
8920
8921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8922
8923 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8924 if (!attr)
8925 {
8926 complaint (&symfile_complaints,
8927 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8928 "DIE 0x%x [in module %s]"),
8929 die->offset.sect_off, objfile->name);
8930 return;
8931 }
8932 pc = DW_ADDR (attr) + baseaddr;
8933
8934 if (cu->call_site_htab == NULL)
8935 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8936 NULL, &objfile->objfile_obstack,
8937 hashtab_obstack_allocate, NULL);
8938 call_site_local.pc = pc;
8939 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8940 if (*slot != NULL)
8941 {
8942 complaint (&symfile_complaints,
8943 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8944 "DIE 0x%x [in module %s]"),
8945 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8946 return;
8947 }
8948
8949 /* Count parameters at the caller. */
8950
8951 nparams = 0;
8952 for (child_die = die->child; child_die && child_die->tag;
8953 child_die = sibling_die (child_die))
8954 {
8955 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8956 {
8957 complaint (&symfile_complaints,
8958 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8959 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8960 child_die->tag, child_die->offset.sect_off, objfile->name);
8961 continue;
8962 }
8963
8964 nparams++;
8965 }
8966
8967 call_site = obstack_alloc (&objfile->objfile_obstack,
8968 (sizeof (*call_site)
8969 + (sizeof (*call_site->parameter)
8970 * (nparams - 1))));
8971 *slot = call_site;
8972 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8973 call_site->pc = pc;
8974
8975 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8976 {
8977 struct die_info *func_die;
8978
8979 /* Skip also over DW_TAG_inlined_subroutine. */
8980 for (func_die = die->parent;
8981 func_die && func_die->tag != DW_TAG_subprogram
8982 && func_die->tag != DW_TAG_subroutine_type;
8983 func_die = func_die->parent);
8984
8985 /* DW_AT_GNU_all_call_sites is a superset
8986 of DW_AT_GNU_all_tail_call_sites. */
8987 if (func_die
8988 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8989 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8990 {
8991 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8992 not complete. But keep CALL_SITE for look ups via call_site_htab,
8993 both the initial caller containing the real return address PC and
8994 the final callee containing the current PC of a chain of tail
8995 calls do not need to have the tail call list complete. But any
8996 function candidate for a virtual tail call frame searched via
8997 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8998 determined unambiguously. */
8999 }
9000 else
9001 {
9002 struct type *func_type = NULL;
9003
9004 if (func_die)
9005 func_type = get_die_type (func_die, cu);
9006 if (func_type != NULL)
9007 {
9008 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9009
9010 /* Enlist this call site to the function. */
9011 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9012 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9013 }
9014 else
9015 complaint (&symfile_complaints,
9016 _("Cannot find function owning DW_TAG_GNU_call_site "
9017 "DIE 0x%x [in module %s]"),
9018 die->offset.sect_off, objfile->name);
9019 }
9020 }
9021
9022 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9023 if (attr == NULL)
9024 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9025 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9026 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9027 /* Keep NULL DWARF_BLOCK. */;
9028 else if (attr_form_is_block (attr))
9029 {
9030 struct dwarf2_locexpr_baton *dlbaton;
9031
9032 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9033 dlbaton->data = DW_BLOCK (attr)->data;
9034 dlbaton->size = DW_BLOCK (attr)->size;
9035 dlbaton->per_cu = cu->per_cu;
9036
9037 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9038 }
9039 else if (is_ref_attr (attr))
9040 {
9041 struct dwarf2_cu *target_cu = cu;
9042 struct die_info *target_die;
9043
9044 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9045 gdb_assert (target_cu->objfile == objfile);
9046 if (die_is_declaration (target_die, target_cu))
9047 {
9048 const char *target_physname;
9049
9050 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9051 if (target_physname == NULL)
9052 complaint (&symfile_complaints,
9053 _("DW_AT_GNU_call_site_target target DIE has invalid "
9054 "physname, for referencing DIE 0x%x [in module %s]"),
9055 die->offset.sect_off, objfile->name);
9056 else
9057 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9058 }
9059 else
9060 {
9061 CORE_ADDR lowpc;
9062
9063 /* DW_AT_entry_pc should be preferred. */
9064 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9065 complaint (&symfile_complaints,
9066 _("DW_AT_GNU_call_site_target target DIE has invalid "
9067 "low pc, for referencing DIE 0x%x [in module %s]"),
9068 die->offset.sect_off, objfile->name);
9069 else
9070 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9071 }
9072 }
9073 else
9074 complaint (&symfile_complaints,
9075 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9076 "block nor reference, for DIE 0x%x [in module %s]"),
9077 die->offset.sect_off, objfile->name);
9078
9079 call_site->per_cu = cu->per_cu;
9080
9081 for (child_die = die->child;
9082 child_die && child_die->tag;
9083 child_die = sibling_die (child_die))
9084 {
9085 struct call_site_parameter *parameter;
9086 struct attribute *loc, *origin;
9087
9088 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9089 {
9090 /* Already printed the complaint above. */
9091 continue;
9092 }
9093
9094 gdb_assert (call_site->parameter_count < nparams);
9095 parameter = &call_site->parameter[call_site->parameter_count];
9096
9097 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9098 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9099 register is contained in DW_AT_GNU_call_site_value. */
9100
9101 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9102 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9103 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9104 {
9105 sect_offset offset;
9106
9107 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9108 offset = dwarf2_get_ref_die_offset (origin);
9109 if (!offset_in_cu_p (&cu->header, offset))
9110 {
9111 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9112 binding can be done only inside one CU. Such referenced DIE
9113 therefore cannot be even moved to DW_TAG_partial_unit. */
9114 complaint (&symfile_complaints,
9115 _("DW_AT_abstract_origin offset is not in CU for "
9116 "DW_TAG_GNU_call_site child DIE 0x%x "
9117 "[in module %s]"),
9118 child_die->offset.sect_off, objfile->name);
9119 continue;
9120 }
9121 parameter->u.param_offset.cu_off = (offset.sect_off
9122 - cu->header.offset.sect_off);
9123 }
9124 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9125 {
9126 complaint (&symfile_complaints,
9127 _("No DW_FORM_block* DW_AT_location for "
9128 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9129 child_die->offset.sect_off, objfile->name);
9130 continue;
9131 }
9132 else
9133 {
9134 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9135 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9136 if (parameter->u.dwarf_reg != -1)
9137 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9138 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9139 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9140 &parameter->u.fb_offset))
9141 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9142 else
9143 {
9144 complaint (&symfile_complaints,
9145 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9146 "for DW_FORM_block* DW_AT_location is supported for "
9147 "DW_TAG_GNU_call_site child DIE 0x%x "
9148 "[in module %s]"),
9149 child_die->offset.sect_off, objfile->name);
9150 continue;
9151 }
9152 }
9153
9154 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9155 if (!attr_form_is_block (attr))
9156 {
9157 complaint (&symfile_complaints,
9158 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9159 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9160 child_die->offset.sect_off, objfile->name);
9161 continue;
9162 }
9163 parameter->value = DW_BLOCK (attr)->data;
9164 parameter->value_size = DW_BLOCK (attr)->size;
9165
9166 /* Parameters are not pre-cleared by memset above. */
9167 parameter->data_value = NULL;
9168 parameter->data_value_size = 0;
9169 call_site->parameter_count++;
9170
9171 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9172 if (attr)
9173 {
9174 if (!attr_form_is_block (attr))
9175 complaint (&symfile_complaints,
9176 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9177 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9178 child_die->offset.sect_off, objfile->name);
9179 else
9180 {
9181 parameter->data_value = DW_BLOCK (attr)->data;
9182 parameter->data_value_size = DW_BLOCK (attr)->size;
9183 }
9184 }
9185 }
9186 }
9187
9188 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9189 Return 1 if the attributes are present and valid, otherwise, return 0.
9190 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9191
9192 static int
9193 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9194 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9195 struct partial_symtab *ranges_pst)
9196 {
9197 struct objfile *objfile = cu->objfile;
9198 struct comp_unit_head *cu_header = &cu->header;
9199 bfd *obfd = objfile->obfd;
9200 unsigned int addr_size = cu_header->addr_size;
9201 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9202 /* Base address selection entry. */
9203 CORE_ADDR base;
9204 int found_base;
9205 unsigned int dummy;
9206 gdb_byte *buffer;
9207 CORE_ADDR marker;
9208 int low_set;
9209 CORE_ADDR low = 0;
9210 CORE_ADDR high = 0;
9211 CORE_ADDR baseaddr;
9212
9213 found_base = cu->base_known;
9214 base = cu->base_address;
9215
9216 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9217 if (offset >= dwarf2_per_objfile->ranges.size)
9218 {
9219 complaint (&symfile_complaints,
9220 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9221 offset);
9222 return 0;
9223 }
9224 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9225
9226 /* Read in the largest possible address. */
9227 marker = read_address (obfd, buffer, cu, &dummy);
9228 if ((marker & mask) == mask)
9229 {
9230 /* If we found the largest possible address, then
9231 read the base address. */
9232 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9233 buffer += 2 * addr_size;
9234 offset += 2 * addr_size;
9235 found_base = 1;
9236 }
9237
9238 low_set = 0;
9239
9240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9241
9242 while (1)
9243 {
9244 CORE_ADDR range_beginning, range_end;
9245
9246 range_beginning = read_address (obfd, buffer, cu, &dummy);
9247 buffer += addr_size;
9248 range_end = read_address (obfd, buffer, cu, &dummy);
9249 buffer += addr_size;
9250 offset += 2 * addr_size;
9251
9252 /* An end of list marker is a pair of zero addresses. */
9253 if (range_beginning == 0 && range_end == 0)
9254 /* Found the end of list entry. */
9255 break;
9256
9257 /* Each base address selection entry is a pair of 2 values.
9258 The first is the largest possible address, the second is
9259 the base address. Check for a base address here. */
9260 if ((range_beginning & mask) == mask)
9261 {
9262 /* If we found the largest possible address, then
9263 read the base address. */
9264 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9265 found_base = 1;
9266 continue;
9267 }
9268
9269 if (!found_base)
9270 {
9271 /* We have no valid base address for the ranges
9272 data. */
9273 complaint (&symfile_complaints,
9274 _("Invalid .debug_ranges data (no base address)"));
9275 return 0;
9276 }
9277
9278 if (range_beginning > range_end)
9279 {
9280 /* Inverted range entries are invalid. */
9281 complaint (&symfile_complaints,
9282 _("Invalid .debug_ranges data (inverted range)"));
9283 return 0;
9284 }
9285
9286 /* Empty range entries have no effect. */
9287 if (range_beginning == range_end)
9288 continue;
9289
9290 range_beginning += base;
9291 range_end += base;
9292
9293 /* A not-uncommon case of bad debug info.
9294 Don't pollute the addrmap with bad data. */
9295 if (range_beginning + baseaddr == 0
9296 && !dwarf2_per_objfile->has_section_at_zero)
9297 {
9298 complaint (&symfile_complaints,
9299 _(".debug_ranges entry has start address of zero"
9300 " [in module %s]"), objfile->name);
9301 continue;
9302 }
9303
9304 if (ranges_pst != NULL)
9305 addrmap_set_empty (objfile->psymtabs_addrmap,
9306 range_beginning + baseaddr,
9307 range_end - 1 + baseaddr,
9308 ranges_pst);
9309
9310 /* FIXME: This is recording everything as a low-high
9311 segment of consecutive addresses. We should have a
9312 data structure for discontiguous block ranges
9313 instead. */
9314 if (! low_set)
9315 {
9316 low = range_beginning;
9317 high = range_end;
9318 low_set = 1;
9319 }
9320 else
9321 {
9322 if (range_beginning < low)
9323 low = range_beginning;
9324 if (range_end > high)
9325 high = range_end;
9326 }
9327 }
9328
9329 if (! low_set)
9330 /* If the first entry is an end-of-list marker, the range
9331 describes an empty scope, i.e. no instructions. */
9332 return 0;
9333
9334 if (low_return)
9335 *low_return = low;
9336 if (high_return)
9337 *high_return = high;
9338 return 1;
9339 }
9340
9341 /* Get low and high pc attributes from a die. Return 1 if the attributes
9342 are present and valid, otherwise, return 0. Return -1 if the range is
9343 discontinuous, i.e. derived from DW_AT_ranges information. */
9344
9345 static int
9346 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9347 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9348 struct partial_symtab *pst)
9349 {
9350 struct attribute *attr;
9351 struct attribute *attr_high;
9352 CORE_ADDR low = 0;
9353 CORE_ADDR high = 0;
9354 int ret = 0;
9355
9356 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9357 if (attr_high)
9358 {
9359 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9360 if (attr)
9361 {
9362 low = DW_ADDR (attr);
9363 if (attr_high->form == DW_FORM_addr
9364 || attr_high->form == DW_FORM_GNU_addr_index)
9365 high = DW_ADDR (attr_high);
9366 else
9367 high = low + DW_UNSND (attr_high);
9368 }
9369 else
9370 /* Found high w/o low attribute. */
9371 return 0;
9372
9373 /* Found consecutive range of addresses. */
9374 ret = 1;
9375 }
9376 else
9377 {
9378 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9379 if (attr != NULL)
9380 {
9381 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9382
9383 /* Value of the DW_AT_ranges attribute is the offset in the
9384 .debug_ranges section. */
9385 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9386 return 0;
9387 /* Found discontinuous range of addresses. */
9388 ret = -1;
9389 }
9390 }
9391
9392 /* read_partial_die has also the strict LOW < HIGH requirement. */
9393 if (high <= low)
9394 return 0;
9395
9396 /* When using the GNU linker, .gnu.linkonce. sections are used to
9397 eliminate duplicate copies of functions and vtables and such.
9398 The linker will arbitrarily choose one and discard the others.
9399 The AT_*_pc values for such functions refer to local labels in
9400 these sections. If the section from that file was discarded, the
9401 labels are not in the output, so the relocs get a value of 0.
9402 If this is a discarded function, mark the pc bounds as invalid,
9403 so that GDB will ignore it. */
9404 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9405 return 0;
9406
9407 *lowpc = low;
9408 if (highpc)
9409 *highpc = high;
9410 return ret;
9411 }
9412
9413 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9414 its low and high PC addresses. Do nothing if these addresses could not
9415 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9416 and HIGHPC to the high address if greater than HIGHPC. */
9417
9418 static void
9419 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9420 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9421 struct dwarf2_cu *cu)
9422 {
9423 CORE_ADDR low, high;
9424 struct die_info *child = die->child;
9425
9426 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9427 {
9428 *lowpc = min (*lowpc, low);
9429 *highpc = max (*highpc, high);
9430 }
9431
9432 /* If the language does not allow nested subprograms (either inside
9433 subprograms or lexical blocks), we're done. */
9434 if (cu->language != language_ada)
9435 return;
9436
9437 /* Check all the children of the given DIE. If it contains nested
9438 subprograms, then check their pc bounds. Likewise, we need to
9439 check lexical blocks as well, as they may also contain subprogram
9440 definitions. */
9441 while (child && child->tag)
9442 {
9443 if (child->tag == DW_TAG_subprogram
9444 || child->tag == DW_TAG_lexical_block)
9445 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9446 child = sibling_die (child);
9447 }
9448 }
9449
9450 /* Get the low and high pc's represented by the scope DIE, and store
9451 them in *LOWPC and *HIGHPC. If the correct values can't be
9452 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9453
9454 static void
9455 get_scope_pc_bounds (struct die_info *die,
9456 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9457 struct dwarf2_cu *cu)
9458 {
9459 CORE_ADDR best_low = (CORE_ADDR) -1;
9460 CORE_ADDR best_high = (CORE_ADDR) 0;
9461 CORE_ADDR current_low, current_high;
9462
9463 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9464 {
9465 best_low = current_low;
9466 best_high = current_high;
9467 }
9468 else
9469 {
9470 struct die_info *child = die->child;
9471
9472 while (child && child->tag)
9473 {
9474 switch (child->tag) {
9475 case DW_TAG_subprogram:
9476 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9477 break;
9478 case DW_TAG_namespace:
9479 case DW_TAG_module:
9480 /* FIXME: carlton/2004-01-16: Should we do this for
9481 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9482 that current GCC's always emit the DIEs corresponding
9483 to definitions of methods of classes as children of a
9484 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9485 the DIEs giving the declarations, which could be
9486 anywhere). But I don't see any reason why the
9487 standards says that they have to be there. */
9488 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9489
9490 if (current_low != ((CORE_ADDR) -1))
9491 {
9492 best_low = min (best_low, current_low);
9493 best_high = max (best_high, current_high);
9494 }
9495 break;
9496 default:
9497 /* Ignore. */
9498 break;
9499 }
9500
9501 child = sibling_die (child);
9502 }
9503 }
9504
9505 *lowpc = best_low;
9506 *highpc = best_high;
9507 }
9508
9509 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9510 in DIE. */
9511
9512 static void
9513 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9514 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9515 {
9516 struct objfile *objfile = cu->objfile;
9517 struct attribute *attr;
9518 struct attribute *attr_high;
9519
9520 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9521 if (attr_high)
9522 {
9523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9524 if (attr)
9525 {
9526 CORE_ADDR low = DW_ADDR (attr);
9527 CORE_ADDR high;
9528 if (attr_high->form == DW_FORM_addr
9529 || attr_high->form == DW_FORM_GNU_addr_index)
9530 high = DW_ADDR (attr_high);
9531 else
9532 high = low + DW_UNSND (attr_high);
9533
9534 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9535 }
9536 }
9537
9538 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9539 if (attr)
9540 {
9541 bfd *obfd = objfile->obfd;
9542
9543 /* The value of the DW_AT_ranges attribute is the offset of the
9544 address range list in the .debug_ranges section. */
9545 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9546 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9547
9548 /* For some target architectures, but not others, the
9549 read_address function sign-extends the addresses it returns.
9550 To recognize base address selection entries, we need a
9551 mask. */
9552 unsigned int addr_size = cu->header.addr_size;
9553 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9554
9555 /* The base address, to which the next pair is relative. Note
9556 that this 'base' is a DWARF concept: most entries in a range
9557 list are relative, to reduce the number of relocs against the
9558 debugging information. This is separate from this function's
9559 'baseaddr' argument, which GDB uses to relocate debugging
9560 information from a shared library based on the address at
9561 which the library was loaded. */
9562 CORE_ADDR base = cu->base_address;
9563 int base_known = cu->base_known;
9564
9565 gdb_assert (dwarf2_per_objfile->ranges.readin);
9566 if (offset >= dwarf2_per_objfile->ranges.size)
9567 {
9568 complaint (&symfile_complaints,
9569 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9570 offset);
9571 return;
9572 }
9573
9574 for (;;)
9575 {
9576 unsigned int bytes_read;
9577 CORE_ADDR start, end;
9578
9579 start = read_address (obfd, buffer, cu, &bytes_read);
9580 buffer += bytes_read;
9581 end = read_address (obfd, buffer, cu, &bytes_read);
9582 buffer += bytes_read;
9583
9584 /* Did we find the end of the range list? */
9585 if (start == 0 && end == 0)
9586 break;
9587
9588 /* Did we find a base address selection entry? */
9589 else if ((start & base_select_mask) == base_select_mask)
9590 {
9591 base = end;
9592 base_known = 1;
9593 }
9594
9595 /* We found an ordinary address range. */
9596 else
9597 {
9598 if (!base_known)
9599 {
9600 complaint (&symfile_complaints,
9601 _("Invalid .debug_ranges data "
9602 "(no base address)"));
9603 return;
9604 }
9605
9606 if (start > end)
9607 {
9608 /* Inverted range entries are invalid. */
9609 complaint (&symfile_complaints,
9610 _("Invalid .debug_ranges data "
9611 "(inverted range)"));
9612 return;
9613 }
9614
9615 /* Empty range entries have no effect. */
9616 if (start == end)
9617 continue;
9618
9619 start += base + baseaddr;
9620 end += base + baseaddr;
9621
9622 /* A not-uncommon case of bad debug info.
9623 Don't pollute the addrmap with bad data. */
9624 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9625 {
9626 complaint (&symfile_complaints,
9627 _(".debug_ranges entry has start address of zero"
9628 " [in module %s]"), objfile->name);
9629 continue;
9630 }
9631
9632 record_block_range (block, start, end - 1);
9633 }
9634 }
9635 }
9636 }
9637
9638 /* Check whether the producer field indicates either of GCC < 4.6, or the
9639 Intel C/C++ compiler, and cache the result in CU. */
9640
9641 static void
9642 check_producer (struct dwarf2_cu *cu)
9643 {
9644 const char *cs;
9645 int major, minor, release;
9646
9647 if (cu->producer == NULL)
9648 {
9649 /* For unknown compilers expect their behavior is DWARF version
9650 compliant.
9651
9652 GCC started to support .debug_types sections by -gdwarf-4 since
9653 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9654 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9655 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9656 interpreted incorrectly by GDB now - GCC PR debug/48229. */
9657 }
9658 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9659 {
9660 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9661
9662 cs = &cu->producer[strlen ("GNU ")];
9663 while (*cs && !isdigit (*cs))
9664 cs++;
9665 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9666 {
9667 /* Not recognized as GCC. */
9668 }
9669 else
9670 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9671 }
9672 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9673 cu->producer_is_icc = 1;
9674 else
9675 {
9676 /* For other non-GCC compilers, expect their behavior is DWARF version
9677 compliant. */
9678 }
9679
9680 cu->checked_producer = 1;
9681 }
9682
9683 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9684 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9685 during 4.6.0 experimental. */
9686
9687 static int
9688 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9689 {
9690 if (!cu->checked_producer)
9691 check_producer (cu);
9692
9693 return cu->producer_is_gxx_lt_4_6;
9694 }
9695
9696 /* Return the default accessibility type if it is not overriden by
9697 DW_AT_accessibility. */
9698
9699 static enum dwarf_access_attribute
9700 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9701 {
9702 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9703 {
9704 /* The default DWARF 2 accessibility for members is public, the default
9705 accessibility for inheritance is private. */
9706
9707 if (die->tag != DW_TAG_inheritance)
9708 return DW_ACCESS_public;
9709 else
9710 return DW_ACCESS_private;
9711 }
9712 else
9713 {
9714 /* DWARF 3+ defines the default accessibility a different way. The same
9715 rules apply now for DW_TAG_inheritance as for the members and it only
9716 depends on the container kind. */
9717
9718 if (die->parent->tag == DW_TAG_class_type)
9719 return DW_ACCESS_private;
9720 else
9721 return DW_ACCESS_public;
9722 }
9723 }
9724
9725 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9726 offset. If the attribute was not found return 0, otherwise return
9727 1. If it was found but could not properly be handled, set *OFFSET
9728 to 0. */
9729
9730 static int
9731 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9732 LONGEST *offset)
9733 {
9734 struct attribute *attr;
9735
9736 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9737 if (attr != NULL)
9738 {
9739 *offset = 0;
9740
9741 /* Note that we do not check for a section offset first here.
9742 This is because DW_AT_data_member_location is new in DWARF 4,
9743 so if we see it, we can assume that a constant form is really
9744 a constant and not a section offset. */
9745 if (attr_form_is_constant (attr))
9746 *offset = dwarf2_get_attr_constant_value (attr, 0);
9747 else if (attr_form_is_section_offset (attr))
9748 dwarf2_complex_location_expr_complaint ();
9749 else if (attr_form_is_block (attr))
9750 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9751 else
9752 dwarf2_complex_location_expr_complaint ();
9753
9754 return 1;
9755 }
9756
9757 return 0;
9758 }
9759
9760 /* Add an aggregate field to the field list. */
9761
9762 static void
9763 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9764 struct dwarf2_cu *cu)
9765 {
9766 struct objfile *objfile = cu->objfile;
9767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9768 struct nextfield *new_field;
9769 struct attribute *attr;
9770 struct field *fp;
9771 char *fieldname = "";
9772
9773 /* Allocate a new field list entry and link it in. */
9774 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9775 make_cleanup (xfree, new_field);
9776 memset (new_field, 0, sizeof (struct nextfield));
9777
9778 if (die->tag == DW_TAG_inheritance)
9779 {
9780 new_field->next = fip->baseclasses;
9781 fip->baseclasses = new_field;
9782 }
9783 else
9784 {
9785 new_field->next = fip->fields;
9786 fip->fields = new_field;
9787 }
9788 fip->nfields++;
9789
9790 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9791 if (attr)
9792 new_field->accessibility = DW_UNSND (attr);
9793 else
9794 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9795 if (new_field->accessibility != DW_ACCESS_public)
9796 fip->non_public_fields = 1;
9797
9798 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9799 if (attr)
9800 new_field->virtuality = DW_UNSND (attr);
9801 else
9802 new_field->virtuality = DW_VIRTUALITY_none;
9803
9804 fp = &new_field->field;
9805
9806 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9807 {
9808 LONGEST offset;
9809
9810 /* Data member other than a C++ static data member. */
9811
9812 /* Get type of field. */
9813 fp->type = die_type (die, cu);
9814
9815 SET_FIELD_BITPOS (*fp, 0);
9816
9817 /* Get bit size of field (zero if none). */
9818 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9819 if (attr)
9820 {
9821 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9822 }
9823 else
9824 {
9825 FIELD_BITSIZE (*fp) = 0;
9826 }
9827
9828 /* Get bit offset of field. */
9829 if (handle_data_member_location (die, cu, &offset))
9830 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9831 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9832 if (attr)
9833 {
9834 if (gdbarch_bits_big_endian (gdbarch))
9835 {
9836 /* For big endian bits, the DW_AT_bit_offset gives the
9837 additional bit offset from the MSB of the containing
9838 anonymous object to the MSB of the field. We don't
9839 have to do anything special since we don't need to
9840 know the size of the anonymous object. */
9841 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9842 }
9843 else
9844 {
9845 /* For little endian bits, compute the bit offset to the
9846 MSB of the anonymous object, subtract off the number of
9847 bits from the MSB of the field to the MSB of the
9848 object, and then subtract off the number of bits of
9849 the field itself. The result is the bit offset of
9850 the LSB of the field. */
9851 int anonymous_size;
9852 int bit_offset = DW_UNSND (attr);
9853
9854 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9855 if (attr)
9856 {
9857 /* The size of the anonymous object containing
9858 the bit field is explicit, so use the
9859 indicated size (in bytes). */
9860 anonymous_size = DW_UNSND (attr);
9861 }
9862 else
9863 {
9864 /* The size of the anonymous object containing
9865 the bit field must be inferred from the type
9866 attribute of the data member containing the
9867 bit field. */
9868 anonymous_size = TYPE_LENGTH (fp->type);
9869 }
9870 SET_FIELD_BITPOS (*fp,
9871 (FIELD_BITPOS (*fp)
9872 + anonymous_size * bits_per_byte
9873 - bit_offset - FIELD_BITSIZE (*fp)));
9874 }
9875 }
9876
9877 /* Get name of field. */
9878 fieldname = dwarf2_name (die, cu);
9879 if (fieldname == NULL)
9880 fieldname = "";
9881
9882 /* The name is already allocated along with this objfile, so we don't
9883 need to duplicate it for the type. */
9884 fp->name = fieldname;
9885
9886 /* Change accessibility for artificial fields (e.g. virtual table
9887 pointer or virtual base class pointer) to private. */
9888 if (dwarf2_attr (die, DW_AT_artificial, cu))
9889 {
9890 FIELD_ARTIFICIAL (*fp) = 1;
9891 new_field->accessibility = DW_ACCESS_private;
9892 fip->non_public_fields = 1;
9893 }
9894 }
9895 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9896 {
9897 /* C++ static member. */
9898
9899 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9900 is a declaration, but all versions of G++ as of this writing
9901 (so through at least 3.2.1) incorrectly generate
9902 DW_TAG_variable tags. */
9903
9904 const char *physname;
9905
9906 /* Get name of field. */
9907 fieldname = dwarf2_name (die, cu);
9908 if (fieldname == NULL)
9909 return;
9910
9911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9912 if (attr
9913 /* Only create a symbol if this is an external value.
9914 new_symbol checks this and puts the value in the global symbol
9915 table, which we want. If it is not external, new_symbol
9916 will try to put the value in cu->list_in_scope which is wrong. */
9917 && dwarf2_flag_true_p (die, DW_AT_external, cu))
9918 {
9919 /* A static const member, not much different than an enum as far as
9920 we're concerned, except that we can support more types. */
9921 new_symbol (die, NULL, cu);
9922 }
9923
9924 /* Get physical name. */
9925 physname = dwarf2_physname (fieldname, die, cu);
9926
9927 /* The name is already allocated along with this objfile, so we don't
9928 need to duplicate it for the type. */
9929 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9930 FIELD_TYPE (*fp) = die_type (die, cu);
9931 FIELD_NAME (*fp) = fieldname;
9932 }
9933 else if (die->tag == DW_TAG_inheritance)
9934 {
9935 LONGEST offset;
9936
9937 /* C++ base class field. */
9938 if (handle_data_member_location (die, cu, &offset))
9939 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9940 FIELD_BITSIZE (*fp) = 0;
9941 FIELD_TYPE (*fp) = die_type (die, cu);
9942 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9943 fip->nbaseclasses++;
9944 }
9945 }
9946
9947 /* Add a typedef defined in the scope of the FIP's class. */
9948
9949 static void
9950 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9951 struct dwarf2_cu *cu)
9952 {
9953 struct objfile *objfile = cu->objfile;
9954 struct typedef_field_list *new_field;
9955 struct attribute *attr;
9956 struct typedef_field *fp;
9957 char *fieldname = "";
9958
9959 /* Allocate a new field list entry and link it in. */
9960 new_field = xzalloc (sizeof (*new_field));
9961 make_cleanup (xfree, new_field);
9962
9963 gdb_assert (die->tag == DW_TAG_typedef);
9964
9965 fp = &new_field->field;
9966
9967 /* Get name of field. */
9968 fp->name = dwarf2_name (die, cu);
9969 if (fp->name == NULL)
9970 return;
9971
9972 fp->type = read_type_die (die, cu);
9973
9974 new_field->next = fip->typedef_field_list;
9975 fip->typedef_field_list = new_field;
9976 fip->typedef_field_list_count++;
9977 }
9978
9979 /* Create the vector of fields, and attach it to the type. */
9980
9981 static void
9982 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9983 struct dwarf2_cu *cu)
9984 {
9985 int nfields = fip->nfields;
9986
9987 /* Record the field count, allocate space for the array of fields,
9988 and create blank accessibility bitfields if necessary. */
9989 TYPE_NFIELDS (type) = nfields;
9990 TYPE_FIELDS (type) = (struct field *)
9991 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9992 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9993
9994 if (fip->non_public_fields && cu->language != language_ada)
9995 {
9996 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9997
9998 TYPE_FIELD_PRIVATE_BITS (type) =
9999 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10000 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10001
10002 TYPE_FIELD_PROTECTED_BITS (type) =
10003 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10004 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10005
10006 TYPE_FIELD_IGNORE_BITS (type) =
10007 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10008 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10009 }
10010
10011 /* If the type has baseclasses, allocate and clear a bit vector for
10012 TYPE_FIELD_VIRTUAL_BITS. */
10013 if (fip->nbaseclasses && cu->language != language_ada)
10014 {
10015 int num_bytes = B_BYTES (fip->nbaseclasses);
10016 unsigned char *pointer;
10017
10018 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10019 pointer = TYPE_ALLOC (type, num_bytes);
10020 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10021 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10022 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10023 }
10024
10025 /* Copy the saved-up fields into the field vector. Start from the head of
10026 the list, adding to the tail of the field array, so that they end up in
10027 the same order in the array in which they were added to the list. */
10028 while (nfields-- > 0)
10029 {
10030 struct nextfield *fieldp;
10031
10032 if (fip->fields)
10033 {
10034 fieldp = fip->fields;
10035 fip->fields = fieldp->next;
10036 }
10037 else
10038 {
10039 fieldp = fip->baseclasses;
10040 fip->baseclasses = fieldp->next;
10041 }
10042
10043 TYPE_FIELD (type, nfields) = fieldp->field;
10044 switch (fieldp->accessibility)
10045 {
10046 case DW_ACCESS_private:
10047 if (cu->language != language_ada)
10048 SET_TYPE_FIELD_PRIVATE (type, nfields);
10049 break;
10050
10051 case DW_ACCESS_protected:
10052 if (cu->language != language_ada)
10053 SET_TYPE_FIELD_PROTECTED (type, nfields);
10054 break;
10055
10056 case DW_ACCESS_public:
10057 break;
10058
10059 default:
10060 /* Unknown accessibility. Complain and treat it as public. */
10061 {
10062 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10063 fieldp->accessibility);
10064 }
10065 break;
10066 }
10067 if (nfields < fip->nbaseclasses)
10068 {
10069 switch (fieldp->virtuality)
10070 {
10071 case DW_VIRTUALITY_virtual:
10072 case DW_VIRTUALITY_pure_virtual:
10073 if (cu->language == language_ada)
10074 error (_("unexpected virtuality in component of Ada type"));
10075 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10076 break;
10077 }
10078 }
10079 }
10080 }
10081
10082 /* Add a member function to the proper fieldlist. */
10083
10084 static void
10085 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10086 struct type *type, struct dwarf2_cu *cu)
10087 {
10088 struct objfile *objfile = cu->objfile;
10089 struct attribute *attr;
10090 struct fnfieldlist *flp;
10091 int i;
10092 struct fn_field *fnp;
10093 char *fieldname;
10094 struct nextfnfield *new_fnfield;
10095 struct type *this_type;
10096 enum dwarf_access_attribute accessibility;
10097
10098 if (cu->language == language_ada)
10099 error (_("unexpected member function in Ada type"));
10100
10101 /* Get name of member function. */
10102 fieldname = dwarf2_name (die, cu);
10103 if (fieldname == NULL)
10104 return;
10105
10106 /* Look up member function name in fieldlist. */
10107 for (i = 0; i < fip->nfnfields; i++)
10108 {
10109 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10110 break;
10111 }
10112
10113 /* Create new list element if necessary. */
10114 if (i < fip->nfnfields)
10115 flp = &fip->fnfieldlists[i];
10116 else
10117 {
10118 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10119 {
10120 fip->fnfieldlists = (struct fnfieldlist *)
10121 xrealloc (fip->fnfieldlists,
10122 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10123 * sizeof (struct fnfieldlist));
10124 if (fip->nfnfields == 0)
10125 make_cleanup (free_current_contents, &fip->fnfieldlists);
10126 }
10127 flp = &fip->fnfieldlists[fip->nfnfields];
10128 flp->name = fieldname;
10129 flp->length = 0;
10130 flp->head = NULL;
10131 i = fip->nfnfields++;
10132 }
10133
10134 /* Create a new member function field and chain it to the field list
10135 entry. */
10136 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10137 make_cleanup (xfree, new_fnfield);
10138 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10139 new_fnfield->next = flp->head;
10140 flp->head = new_fnfield;
10141 flp->length++;
10142
10143 /* Fill in the member function field info. */
10144 fnp = &new_fnfield->fnfield;
10145
10146 /* Delay processing of the physname until later. */
10147 if (cu->language == language_cplus || cu->language == language_java)
10148 {
10149 add_to_method_list (type, i, flp->length - 1, fieldname,
10150 die, cu);
10151 }
10152 else
10153 {
10154 const char *physname = dwarf2_physname (fieldname, die, cu);
10155 fnp->physname = physname ? physname : "";
10156 }
10157
10158 fnp->type = alloc_type (objfile);
10159 this_type = read_type_die (die, cu);
10160 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10161 {
10162 int nparams = TYPE_NFIELDS (this_type);
10163
10164 /* TYPE is the domain of this method, and THIS_TYPE is the type
10165 of the method itself (TYPE_CODE_METHOD). */
10166 smash_to_method_type (fnp->type, type,
10167 TYPE_TARGET_TYPE (this_type),
10168 TYPE_FIELDS (this_type),
10169 TYPE_NFIELDS (this_type),
10170 TYPE_VARARGS (this_type));
10171
10172 /* Handle static member functions.
10173 Dwarf2 has no clean way to discern C++ static and non-static
10174 member functions. G++ helps GDB by marking the first
10175 parameter for non-static member functions (which is the this
10176 pointer) as artificial. We obtain this information from
10177 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10178 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10179 fnp->voffset = VOFFSET_STATIC;
10180 }
10181 else
10182 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10183 dwarf2_full_name (fieldname, die, cu));
10184
10185 /* Get fcontext from DW_AT_containing_type if present. */
10186 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10187 fnp->fcontext = die_containing_type (die, cu);
10188
10189 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10190 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10191
10192 /* Get accessibility. */
10193 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10194 if (attr)
10195 accessibility = DW_UNSND (attr);
10196 else
10197 accessibility = dwarf2_default_access_attribute (die, cu);
10198 switch (accessibility)
10199 {
10200 case DW_ACCESS_private:
10201 fnp->is_private = 1;
10202 break;
10203 case DW_ACCESS_protected:
10204 fnp->is_protected = 1;
10205 break;
10206 }
10207
10208 /* Check for artificial methods. */
10209 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10210 if (attr && DW_UNSND (attr) != 0)
10211 fnp->is_artificial = 1;
10212
10213 /* Get index in virtual function table if it is a virtual member
10214 function. For older versions of GCC, this is an offset in the
10215 appropriate virtual table, as specified by DW_AT_containing_type.
10216 For everyone else, it is an expression to be evaluated relative
10217 to the object address. */
10218
10219 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10220 if (attr)
10221 {
10222 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10223 {
10224 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10225 {
10226 /* Old-style GCC. */
10227 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10228 }
10229 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10230 || (DW_BLOCK (attr)->size > 1
10231 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10232 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10233 {
10234 struct dwarf_block blk;
10235 int offset;
10236
10237 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10238 ? 1 : 2);
10239 blk.size = DW_BLOCK (attr)->size - offset;
10240 blk.data = DW_BLOCK (attr)->data + offset;
10241 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10242 if ((fnp->voffset % cu->header.addr_size) != 0)
10243 dwarf2_complex_location_expr_complaint ();
10244 else
10245 fnp->voffset /= cu->header.addr_size;
10246 fnp->voffset += 2;
10247 }
10248 else
10249 dwarf2_complex_location_expr_complaint ();
10250
10251 if (!fnp->fcontext)
10252 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10253 }
10254 else if (attr_form_is_section_offset (attr))
10255 {
10256 dwarf2_complex_location_expr_complaint ();
10257 }
10258 else
10259 {
10260 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10261 fieldname);
10262 }
10263 }
10264 else
10265 {
10266 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10267 if (attr && DW_UNSND (attr))
10268 {
10269 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10270 complaint (&symfile_complaints,
10271 _("Member function \"%s\" (offset %d) is virtual "
10272 "but the vtable offset is not specified"),
10273 fieldname, die->offset.sect_off);
10274 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10275 TYPE_CPLUS_DYNAMIC (type) = 1;
10276 }
10277 }
10278 }
10279
10280 /* Create the vector of member function fields, and attach it to the type. */
10281
10282 static void
10283 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10284 struct dwarf2_cu *cu)
10285 {
10286 struct fnfieldlist *flp;
10287 int i;
10288
10289 if (cu->language == language_ada)
10290 error (_("unexpected member functions in Ada type"));
10291
10292 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10293 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10294 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10295
10296 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10297 {
10298 struct nextfnfield *nfp = flp->head;
10299 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10300 int k;
10301
10302 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10303 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10304 fn_flp->fn_fields = (struct fn_field *)
10305 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10306 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10307 fn_flp->fn_fields[k] = nfp->fnfield;
10308 }
10309
10310 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10311 }
10312
10313 /* Returns non-zero if NAME is the name of a vtable member in CU's
10314 language, zero otherwise. */
10315 static int
10316 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10317 {
10318 static const char vptr[] = "_vptr";
10319 static const char vtable[] = "vtable";
10320
10321 /* Look for the C++ and Java forms of the vtable. */
10322 if ((cu->language == language_java
10323 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10324 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10325 && is_cplus_marker (name[sizeof (vptr) - 1])))
10326 return 1;
10327
10328 return 0;
10329 }
10330
10331 /* GCC outputs unnamed structures that are really pointers to member
10332 functions, with the ABI-specified layout. If TYPE describes
10333 such a structure, smash it into a member function type.
10334
10335 GCC shouldn't do this; it should just output pointer to member DIEs.
10336 This is GCC PR debug/28767. */
10337
10338 static void
10339 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10340 {
10341 struct type *pfn_type, *domain_type, *new_type;
10342
10343 /* Check for a structure with no name and two children. */
10344 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10345 return;
10346
10347 /* Check for __pfn and __delta members. */
10348 if (TYPE_FIELD_NAME (type, 0) == NULL
10349 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10350 || TYPE_FIELD_NAME (type, 1) == NULL
10351 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10352 return;
10353
10354 /* Find the type of the method. */
10355 pfn_type = TYPE_FIELD_TYPE (type, 0);
10356 if (pfn_type == NULL
10357 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10358 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10359 return;
10360
10361 /* Look for the "this" argument. */
10362 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10363 if (TYPE_NFIELDS (pfn_type) == 0
10364 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10365 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10366 return;
10367
10368 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10369 new_type = alloc_type (objfile);
10370 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10371 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10372 TYPE_VARARGS (pfn_type));
10373 smash_to_methodptr_type (type, new_type);
10374 }
10375
10376 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10377 (icc). */
10378
10379 static int
10380 producer_is_icc (struct dwarf2_cu *cu)
10381 {
10382 if (!cu->checked_producer)
10383 check_producer (cu);
10384
10385 return cu->producer_is_icc;
10386 }
10387
10388 /* Called when we find the DIE that starts a structure or union scope
10389 (definition) to create a type for the structure or union. Fill in
10390 the type's name and general properties; the members will not be
10391 processed until process_structure_type.
10392
10393 NOTE: we need to call these functions regardless of whether or not the
10394 DIE has a DW_AT_name attribute, since it might be an anonymous
10395 structure or union. This gets the type entered into our set of
10396 user defined types.
10397
10398 However, if the structure is incomplete (an opaque struct/union)
10399 then suppress creating a symbol table entry for it since gdb only
10400 wants to find the one with the complete definition. Note that if
10401 it is complete, we just call new_symbol, which does it's own
10402 checking about whether the struct/union is anonymous or not (and
10403 suppresses creating a symbol table entry itself). */
10404
10405 static struct type *
10406 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10407 {
10408 struct objfile *objfile = cu->objfile;
10409 struct type *type;
10410 struct attribute *attr;
10411 char *name;
10412
10413 /* If the definition of this type lives in .debug_types, read that type.
10414 Don't follow DW_AT_specification though, that will take us back up
10415 the chain and we want to go down. */
10416 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10417 if (attr)
10418 {
10419 struct dwarf2_cu *type_cu = cu;
10420 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10421
10422 /* We could just recurse on read_structure_type, but we need to call
10423 get_die_type to ensure only one type for this DIE is created.
10424 This is important, for example, because for c++ classes we need
10425 TYPE_NAME set which is only done by new_symbol. Blech. */
10426 type = read_type_die (type_die, type_cu);
10427
10428 /* TYPE_CU may not be the same as CU.
10429 Ensure TYPE is recorded in CU's type_hash table. */
10430 return set_die_type (die, type, cu);
10431 }
10432
10433 type = alloc_type (objfile);
10434 INIT_CPLUS_SPECIFIC (type);
10435
10436 name = dwarf2_name (die, cu);
10437 if (name != NULL)
10438 {
10439 if (cu->language == language_cplus
10440 || cu->language == language_java)
10441 {
10442 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10443
10444 /* dwarf2_full_name might have already finished building the DIE's
10445 type. If so, there is no need to continue. */
10446 if (get_die_type (die, cu) != NULL)
10447 return get_die_type (die, cu);
10448
10449 TYPE_TAG_NAME (type) = full_name;
10450 if (die->tag == DW_TAG_structure_type
10451 || die->tag == DW_TAG_class_type)
10452 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10453 }
10454 else
10455 {
10456 /* The name is already allocated along with this objfile, so
10457 we don't need to duplicate it for the type. */
10458 TYPE_TAG_NAME (type) = (char *) name;
10459 if (die->tag == DW_TAG_class_type)
10460 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10461 }
10462 }
10463
10464 if (die->tag == DW_TAG_structure_type)
10465 {
10466 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10467 }
10468 else if (die->tag == DW_TAG_union_type)
10469 {
10470 TYPE_CODE (type) = TYPE_CODE_UNION;
10471 }
10472 else
10473 {
10474 TYPE_CODE (type) = TYPE_CODE_CLASS;
10475 }
10476
10477 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10478 TYPE_DECLARED_CLASS (type) = 1;
10479
10480 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10481 if (attr)
10482 {
10483 TYPE_LENGTH (type) = DW_UNSND (attr);
10484 }
10485 else
10486 {
10487 TYPE_LENGTH (type) = 0;
10488 }
10489
10490 if (producer_is_icc (cu))
10491 {
10492 /* ICC does not output the required DW_AT_declaration
10493 on incomplete types, but gives them a size of zero. */
10494 }
10495 else
10496 TYPE_STUB_SUPPORTED (type) = 1;
10497
10498 if (die_is_declaration (die, cu))
10499 TYPE_STUB (type) = 1;
10500 else if (attr == NULL && die->child == NULL
10501 && producer_is_realview (cu->producer))
10502 /* RealView does not output the required DW_AT_declaration
10503 on incomplete types. */
10504 TYPE_STUB (type) = 1;
10505
10506 /* We need to add the type field to the die immediately so we don't
10507 infinitely recurse when dealing with pointers to the structure
10508 type within the structure itself. */
10509 set_die_type (die, type, cu);
10510
10511 /* set_die_type should be already done. */
10512 set_descriptive_type (type, die, cu);
10513
10514 return type;
10515 }
10516
10517 /* Finish creating a structure or union type, including filling in
10518 its members and creating a symbol for it. */
10519
10520 static void
10521 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10522 {
10523 struct objfile *objfile = cu->objfile;
10524 struct die_info *child_die = die->child;
10525 struct type *type;
10526
10527 type = get_die_type (die, cu);
10528 if (type == NULL)
10529 type = read_structure_type (die, cu);
10530
10531 if (die->child != NULL && ! die_is_declaration (die, cu))
10532 {
10533 struct field_info fi;
10534 struct die_info *child_die;
10535 VEC (symbolp) *template_args = NULL;
10536 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10537
10538 memset (&fi, 0, sizeof (struct field_info));
10539
10540 child_die = die->child;
10541
10542 while (child_die && child_die->tag)
10543 {
10544 if (child_die->tag == DW_TAG_member
10545 || child_die->tag == DW_TAG_variable)
10546 {
10547 /* NOTE: carlton/2002-11-05: A C++ static data member
10548 should be a DW_TAG_member that is a declaration, but
10549 all versions of G++ as of this writing (so through at
10550 least 3.2.1) incorrectly generate DW_TAG_variable
10551 tags for them instead. */
10552 dwarf2_add_field (&fi, child_die, cu);
10553 }
10554 else if (child_die->tag == DW_TAG_subprogram)
10555 {
10556 /* C++ member function. */
10557 dwarf2_add_member_fn (&fi, child_die, type, cu);
10558 }
10559 else if (child_die->tag == DW_TAG_inheritance)
10560 {
10561 /* C++ base class field. */
10562 dwarf2_add_field (&fi, child_die, cu);
10563 }
10564 else if (child_die->tag == DW_TAG_typedef)
10565 dwarf2_add_typedef (&fi, child_die, cu);
10566 else if (child_die->tag == DW_TAG_template_type_param
10567 || child_die->tag == DW_TAG_template_value_param)
10568 {
10569 struct symbol *arg = new_symbol (child_die, NULL, cu);
10570
10571 if (arg != NULL)
10572 VEC_safe_push (symbolp, template_args, arg);
10573 }
10574
10575 child_die = sibling_die (child_die);
10576 }
10577
10578 /* Attach template arguments to type. */
10579 if (! VEC_empty (symbolp, template_args))
10580 {
10581 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10582 TYPE_N_TEMPLATE_ARGUMENTS (type)
10583 = VEC_length (symbolp, template_args);
10584 TYPE_TEMPLATE_ARGUMENTS (type)
10585 = obstack_alloc (&objfile->objfile_obstack,
10586 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10587 * sizeof (struct symbol *)));
10588 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10589 VEC_address (symbolp, template_args),
10590 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10591 * sizeof (struct symbol *)));
10592 VEC_free (symbolp, template_args);
10593 }
10594
10595 /* Attach fields and member functions to the type. */
10596 if (fi.nfields)
10597 dwarf2_attach_fields_to_type (&fi, type, cu);
10598 if (fi.nfnfields)
10599 {
10600 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10601
10602 /* Get the type which refers to the base class (possibly this
10603 class itself) which contains the vtable pointer for the current
10604 class from the DW_AT_containing_type attribute. This use of
10605 DW_AT_containing_type is a GNU extension. */
10606
10607 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10608 {
10609 struct type *t = die_containing_type (die, cu);
10610
10611 TYPE_VPTR_BASETYPE (type) = t;
10612 if (type == t)
10613 {
10614 int i;
10615
10616 /* Our own class provides vtbl ptr. */
10617 for (i = TYPE_NFIELDS (t) - 1;
10618 i >= TYPE_N_BASECLASSES (t);
10619 --i)
10620 {
10621 const char *fieldname = TYPE_FIELD_NAME (t, i);
10622
10623 if (is_vtable_name (fieldname, cu))
10624 {
10625 TYPE_VPTR_FIELDNO (type) = i;
10626 break;
10627 }
10628 }
10629
10630 /* Complain if virtual function table field not found. */
10631 if (i < TYPE_N_BASECLASSES (t))
10632 complaint (&symfile_complaints,
10633 _("virtual function table pointer "
10634 "not found when defining class '%s'"),
10635 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10636 "");
10637 }
10638 else
10639 {
10640 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10641 }
10642 }
10643 else if (cu->producer
10644 && strncmp (cu->producer,
10645 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10646 {
10647 /* The IBM XLC compiler does not provide direct indication
10648 of the containing type, but the vtable pointer is
10649 always named __vfp. */
10650
10651 int i;
10652
10653 for (i = TYPE_NFIELDS (type) - 1;
10654 i >= TYPE_N_BASECLASSES (type);
10655 --i)
10656 {
10657 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10658 {
10659 TYPE_VPTR_FIELDNO (type) = i;
10660 TYPE_VPTR_BASETYPE (type) = type;
10661 break;
10662 }
10663 }
10664 }
10665 }
10666
10667 /* Copy fi.typedef_field_list linked list elements content into the
10668 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10669 if (fi.typedef_field_list)
10670 {
10671 int i = fi.typedef_field_list_count;
10672
10673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10674 TYPE_TYPEDEF_FIELD_ARRAY (type)
10675 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10676 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10677
10678 /* Reverse the list order to keep the debug info elements order. */
10679 while (--i >= 0)
10680 {
10681 struct typedef_field *dest, *src;
10682
10683 dest = &TYPE_TYPEDEF_FIELD (type, i);
10684 src = &fi.typedef_field_list->field;
10685 fi.typedef_field_list = fi.typedef_field_list->next;
10686 *dest = *src;
10687 }
10688 }
10689
10690 do_cleanups (back_to);
10691
10692 if (HAVE_CPLUS_STRUCT (type))
10693 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10694 }
10695
10696 quirk_gcc_member_function_pointer (type, objfile);
10697
10698 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10699 snapshots) has been known to create a die giving a declaration
10700 for a class that has, as a child, a die giving a definition for a
10701 nested class. So we have to process our children even if the
10702 current die is a declaration. Normally, of course, a declaration
10703 won't have any children at all. */
10704
10705 while (child_die != NULL && child_die->tag)
10706 {
10707 if (child_die->tag == DW_TAG_member
10708 || child_die->tag == DW_TAG_variable
10709 || child_die->tag == DW_TAG_inheritance
10710 || child_die->tag == DW_TAG_template_value_param
10711 || child_die->tag == DW_TAG_template_type_param)
10712 {
10713 /* Do nothing. */
10714 }
10715 else
10716 process_die (child_die, cu);
10717
10718 child_die = sibling_die (child_die);
10719 }
10720
10721 /* Do not consider external references. According to the DWARF standard,
10722 these DIEs are identified by the fact that they have no byte_size
10723 attribute, and a declaration attribute. */
10724 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10725 || !die_is_declaration (die, cu))
10726 new_symbol (die, type, cu);
10727 }
10728
10729 /* Given a DW_AT_enumeration_type die, set its type. We do not
10730 complete the type's fields yet, or create any symbols. */
10731
10732 static struct type *
10733 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735 struct objfile *objfile = cu->objfile;
10736 struct type *type;
10737 struct attribute *attr;
10738 const char *name;
10739
10740 /* If the definition of this type lives in .debug_types, read that type.
10741 Don't follow DW_AT_specification though, that will take us back up
10742 the chain and we want to go down. */
10743 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10744 if (attr)
10745 {
10746 struct dwarf2_cu *type_cu = cu;
10747 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10748
10749 type = read_type_die (type_die, type_cu);
10750
10751 /* TYPE_CU may not be the same as CU.
10752 Ensure TYPE is recorded in CU's type_hash table. */
10753 return set_die_type (die, type, cu);
10754 }
10755
10756 type = alloc_type (objfile);
10757
10758 TYPE_CODE (type) = TYPE_CODE_ENUM;
10759 name = dwarf2_full_name (NULL, die, cu);
10760 if (name != NULL)
10761 TYPE_TAG_NAME (type) = (char *) name;
10762
10763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10764 if (attr)
10765 {
10766 TYPE_LENGTH (type) = DW_UNSND (attr);
10767 }
10768 else
10769 {
10770 TYPE_LENGTH (type) = 0;
10771 }
10772
10773 /* The enumeration DIE can be incomplete. In Ada, any type can be
10774 declared as private in the package spec, and then defined only
10775 inside the package body. Such types are known as Taft Amendment
10776 Types. When another package uses such a type, an incomplete DIE
10777 may be generated by the compiler. */
10778 if (die_is_declaration (die, cu))
10779 TYPE_STUB (type) = 1;
10780
10781 return set_die_type (die, type, cu);
10782 }
10783
10784 /* Given a pointer to a die which begins an enumeration, process all
10785 the dies that define the members of the enumeration, and create the
10786 symbol for the enumeration type.
10787
10788 NOTE: We reverse the order of the element list. */
10789
10790 static void
10791 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10792 {
10793 struct type *this_type;
10794
10795 this_type = get_die_type (die, cu);
10796 if (this_type == NULL)
10797 this_type = read_enumeration_type (die, cu);
10798
10799 if (die->child != NULL)
10800 {
10801 struct die_info *child_die;
10802 struct symbol *sym;
10803 struct field *fields = NULL;
10804 int num_fields = 0;
10805 int unsigned_enum = 1;
10806 char *name;
10807 int flag_enum = 1;
10808 ULONGEST mask = 0;
10809
10810 child_die = die->child;
10811 while (child_die && child_die->tag)
10812 {
10813 if (child_die->tag != DW_TAG_enumerator)
10814 {
10815 process_die (child_die, cu);
10816 }
10817 else
10818 {
10819 name = dwarf2_name (child_die, cu);
10820 if (name)
10821 {
10822 sym = new_symbol (child_die, this_type, cu);
10823 if (SYMBOL_VALUE (sym) < 0)
10824 {
10825 unsigned_enum = 0;
10826 flag_enum = 0;
10827 }
10828 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10829 flag_enum = 0;
10830 else
10831 mask |= SYMBOL_VALUE (sym);
10832
10833 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10834 {
10835 fields = (struct field *)
10836 xrealloc (fields,
10837 (num_fields + DW_FIELD_ALLOC_CHUNK)
10838 * sizeof (struct field));
10839 }
10840
10841 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10842 FIELD_TYPE (fields[num_fields]) = NULL;
10843 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10844 FIELD_BITSIZE (fields[num_fields]) = 0;
10845
10846 num_fields++;
10847 }
10848 }
10849
10850 child_die = sibling_die (child_die);
10851 }
10852
10853 if (num_fields)
10854 {
10855 TYPE_NFIELDS (this_type) = num_fields;
10856 TYPE_FIELDS (this_type) = (struct field *)
10857 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10858 memcpy (TYPE_FIELDS (this_type), fields,
10859 sizeof (struct field) * num_fields);
10860 xfree (fields);
10861 }
10862 if (unsigned_enum)
10863 TYPE_UNSIGNED (this_type) = 1;
10864 if (flag_enum)
10865 TYPE_FLAG_ENUM (this_type) = 1;
10866 }
10867
10868 /* If we are reading an enum from a .debug_types unit, and the enum
10869 is a declaration, and the enum is not the signatured type in the
10870 unit, then we do not want to add a symbol for it. Adding a
10871 symbol would in some cases obscure the true definition of the
10872 enum, giving users an incomplete type when the definition is
10873 actually available. Note that we do not want to do this for all
10874 enums which are just declarations, because C++0x allows forward
10875 enum declarations. */
10876 if (cu->per_cu->is_debug_types
10877 && die_is_declaration (die, cu))
10878 {
10879 struct signatured_type *sig_type;
10880
10881 sig_type
10882 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10883 cu->per_cu->info_or_types_section,
10884 cu->per_cu->offset);
10885 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10886 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10887 return;
10888 }
10889
10890 new_symbol (die, this_type, cu);
10891 }
10892
10893 /* Extract all information from a DW_TAG_array_type DIE and put it in
10894 the DIE's type field. For now, this only handles one dimensional
10895 arrays. */
10896
10897 static struct type *
10898 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10899 {
10900 struct objfile *objfile = cu->objfile;
10901 struct die_info *child_die;
10902 struct type *type;
10903 struct type *element_type, *range_type, *index_type;
10904 struct type **range_types = NULL;
10905 struct attribute *attr;
10906 int ndim = 0;
10907 struct cleanup *back_to;
10908 char *name;
10909
10910 element_type = die_type (die, cu);
10911
10912 /* The die_type call above may have already set the type for this DIE. */
10913 type = get_die_type (die, cu);
10914 if (type)
10915 return type;
10916
10917 /* Irix 6.2 native cc creates array types without children for
10918 arrays with unspecified length. */
10919 if (die->child == NULL)
10920 {
10921 index_type = objfile_type (objfile)->builtin_int;
10922 range_type = create_range_type (NULL, index_type, 0, -1);
10923 type = create_array_type (NULL, element_type, range_type);
10924 return set_die_type (die, type, cu);
10925 }
10926
10927 back_to = make_cleanup (null_cleanup, NULL);
10928 child_die = die->child;
10929 while (child_die && child_die->tag)
10930 {
10931 if (child_die->tag == DW_TAG_subrange_type)
10932 {
10933 struct type *child_type = read_type_die (child_die, cu);
10934
10935 if (child_type != NULL)
10936 {
10937 /* The range type was succesfully read. Save it for the
10938 array type creation. */
10939 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10940 {
10941 range_types = (struct type **)
10942 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10943 * sizeof (struct type *));
10944 if (ndim == 0)
10945 make_cleanup (free_current_contents, &range_types);
10946 }
10947 range_types[ndim++] = child_type;
10948 }
10949 }
10950 child_die = sibling_die (child_die);
10951 }
10952
10953 /* Dwarf2 dimensions are output from left to right, create the
10954 necessary array types in backwards order. */
10955
10956 type = element_type;
10957
10958 if (read_array_order (die, cu) == DW_ORD_col_major)
10959 {
10960 int i = 0;
10961
10962 while (i < ndim)
10963 type = create_array_type (NULL, type, range_types[i++]);
10964 }
10965 else
10966 {
10967 while (ndim-- > 0)
10968 type = create_array_type (NULL, type, range_types[ndim]);
10969 }
10970
10971 /* Understand Dwarf2 support for vector types (like they occur on
10972 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10973 array type. This is not part of the Dwarf2/3 standard yet, but a
10974 custom vendor extension. The main difference between a regular
10975 array and the vector variant is that vectors are passed by value
10976 to functions. */
10977 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10978 if (attr)
10979 make_vector_type (type);
10980
10981 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10982 implementation may choose to implement triple vectors using this
10983 attribute. */
10984 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10985 if (attr)
10986 {
10987 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10988 TYPE_LENGTH (type) = DW_UNSND (attr);
10989 else
10990 complaint (&symfile_complaints,
10991 _("DW_AT_byte_size for array type smaller "
10992 "than the total size of elements"));
10993 }
10994
10995 name = dwarf2_name (die, cu);
10996 if (name)
10997 TYPE_NAME (type) = name;
10998
10999 /* Install the type in the die. */
11000 set_die_type (die, type, cu);
11001
11002 /* set_die_type should be already done. */
11003 set_descriptive_type (type, die, cu);
11004
11005 do_cleanups (back_to);
11006
11007 return type;
11008 }
11009
11010 static enum dwarf_array_dim_ordering
11011 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11012 {
11013 struct attribute *attr;
11014
11015 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11016
11017 if (attr) return DW_SND (attr);
11018
11019 /* GNU F77 is a special case, as at 08/2004 array type info is the
11020 opposite order to the dwarf2 specification, but data is still
11021 laid out as per normal fortran.
11022
11023 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11024 version checking. */
11025
11026 if (cu->language == language_fortran
11027 && cu->producer && strstr (cu->producer, "GNU F77"))
11028 {
11029 return DW_ORD_row_major;
11030 }
11031
11032 switch (cu->language_defn->la_array_ordering)
11033 {
11034 case array_column_major:
11035 return DW_ORD_col_major;
11036 case array_row_major:
11037 default:
11038 return DW_ORD_row_major;
11039 };
11040 }
11041
11042 /* Extract all information from a DW_TAG_set_type DIE and put it in
11043 the DIE's type field. */
11044
11045 static struct type *
11046 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11047 {
11048 struct type *domain_type, *set_type;
11049 struct attribute *attr;
11050
11051 domain_type = die_type (die, cu);
11052
11053 /* The die_type call above may have already set the type for this DIE. */
11054 set_type = get_die_type (die, cu);
11055 if (set_type)
11056 return set_type;
11057
11058 set_type = create_set_type (NULL, domain_type);
11059
11060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11061 if (attr)
11062 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11063
11064 return set_die_type (die, set_type, cu);
11065 }
11066
11067 /* First cut: install each common block member as a global variable. */
11068
11069 static void
11070 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11071 {
11072 struct die_info *child_die;
11073 struct attribute *attr;
11074 struct symbol *sym;
11075 CORE_ADDR base = (CORE_ADDR) 0;
11076
11077 attr = dwarf2_attr (die, DW_AT_location, cu);
11078 if (attr)
11079 {
11080 /* Support the .debug_loc offsets. */
11081 if (attr_form_is_block (attr))
11082 {
11083 base = decode_locdesc (DW_BLOCK (attr), cu);
11084 }
11085 else if (attr_form_is_section_offset (attr))
11086 {
11087 dwarf2_complex_location_expr_complaint ();
11088 }
11089 else
11090 {
11091 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11092 "common block member");
11093 }
11094 }
11095 if (die->child != NULL)
11096 {
11097 child_die = die->child;
11098 while (child_die && child_die->tag)
11099 {
11100 LONGEST offset;
11101
11102 sym = new_symbol (child_die, NULL, cu);
11103 if (sym != NULL
11104 && handle_data_member_location (child_die, cu, &offset))
11105 {
11106 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
11107 add_symbol_to_list (sym, &global_symbols);
11108 }
11109 child_die = sibling_die (child_die);
11110 }
11111 }
11112 }
11113
11114 /* Create a type for a C++ namespace. */
11115
11116 static struct type *
11117 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11118 {
11119 struct objfile *objfile = cu->objfile;
11120 const char *previous_prefix, *name;
11121 int is_anonymous;
11122 struct type *type;
11123
11124 /* For extensions, reuse the type of the original namespace. */
11125 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11126 {
11127 struct die_info *ext_die;
11128 struct dwarf2_cu *ext_cu = cu;
11129
11130 ext_die = dwarf2_extension (die, &ext_cu);
11131 type = read_type_die (ext_die, ext_cu);
11132
11133 /* EXT_CU may not be the same as CU.
11134 Ensure TYPE is recorded in CU's type_hash table. */
11135 return set_die_type (die, type, cu);
11136 }
11137
11138 name = namespace_name (die, &is_anonymous, cu);
11139
11140 /* Now build the name of the current namespace. */
11141
11142 previous_prefix = determine_prefix (die, cu);
11143 if (previous_prefix[0] != '\0')
11144 name = typename_concat (&objfile->objfile_obstack,
11145 previous_prefix, name, 0, cu);
11146
11147 /* Create the type. */
11148 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11149 objfile);
11150 TYPE_NAME (type) = (char *) name;
11151 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11152
11153 return set_die_type (die, type, cu);
11154 }
11155
11156 /* Read a C++ namespace. */
11157
11158 static void
11159 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11160 {
11161 struct objfile *objfile = cu->objfile;
11162 int is_anonymous;
11163
11164 /* Add a symbol associated to this if we haven't seen the namespace
11165 before. Also, add a using directive if it's an anonymous
11166 namespace. */
11167
11168 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11169 {
11170 struct type *type;
11171
11172 type = read_type_die (die, cu);
11173 new_symbol (die, type, cu);
11174
11175 namespace_name (die, &is_anonymous, cu);
11176 if (is_anonymous)
11177 {
11178 const char *previous_prefix = determine_prefix (die, cu);
11179
11180 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11181 NULL, NULL, &objfile->objfile_obstack);
11182 }
11183 }
11184
11185 if (die->child != NULL)
11186 {
11187 struct die_info *child_die = die->child;
11188
11189 while (child_die && child_die->tag)
11190 {
11191 process_die (child_die, cu);
11192 child_die = sibling_die (child_die);
11193 }
11194 }
11195 }
11196
11197 /* Read a Fortran module as type. This DIE can be only a declaration used for
11198 imported module. Still we need that type as local Fortran "use ... only"
11199 declaration imports depend on the created type in determine_prefix. */
11200
11201 static struct type *
11202 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11203 {
11204 struct objfile *objfile = cu->objfile;
11205 char *module_name;
11206 struct type *type;
11207
11208 module_name = dwarf2_name (die, cu);
11209 if (!module_name)
11210 complaint (&symfile_complaints,
11211 _("DW_TAG_module has no name, offset 0x%x"),
11212 die->offset.sect_off);
11213 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11214
11215 /* determine_prefix uses TYPE_TAG_NAME. */
11216 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11217
11218 return set_die_type (die, type, cu);
11219 }
11220
11221 /* Read a Fortran module. */
11222
11223 static void
11224 read_module (struct die_info *die, struct dwarf2_cu *cu)
11225 {
11226 struct die_info *child_die = die->child;
11227
11228 while (child_die && child_die->tag)
11229 {
11230 process_die (child_die, cu);
11231 child_die = sibling_die (child_die);
11232 }
11233 }
11234
11235 /* Return the name of the namespace represented by DIE. Set
11236 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11237 namespace. */
11238
11239 static const char *
11240 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11241 {
11242 struct die_info *current_die;
11243 const char *name = NULL;
11244
11245 /* Loop through the extensions until we find a name. */
11246
11247 for (current_die = die;
11248 current_die != NULL;
11249 current_die = dwarf2_extension (die, &cu))
11250 {
11251 name = dwarf2_name (current_die, cu);
11252 if (name != NULL)
11253 break;
11254 }
11255
11256 /* Is it an anonymous namespace? */
11257
11258 *is_anonymous = (name == NULL);
11259 if (*is_anonymous)
11260 name = CP_ANONYMOUS_NAMESPACE_STR;
11261
11262 return name;
11263 }
11264
11265 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11266 the user defined type vector. */
11267
11268 static struct type *
11269 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11270 {
11271 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11272 struct comp_unit_head *cu_header = &cu->header;
11273 struct type *type;
11274 struct attribute *attr_byte_size;
11275 struct attribute *attr_address_class;
11276 int byte_size, addr_class;
11277 struct type *target_type;
11278
11279 target_type = die_type (die, cu);
11280
11281 /* The die_type call above may have already set the type for this DIE. */
11282 type = get_die_type (die, cu);
11283 if (type)
11284 return type;
11285
11286 type = lookup_pointer_type (target_type);
11287
11288 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11289 if (attr_byte_size)
11290 byte_size = DW_UNSND (attr_byte_size);
11291 else
11292 byte_size = cu_header->addr_size;
11293
11294 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11295 if (attr_address_class)
11296 addr_class = DW_UNSND (attr_address_class);
11297 else
11298 addr_class = DW_ADDR_none;
11299
11300 /* If the pointer size or address class is different than the
11301 default, create a type variant marked as such and set the
11302 length accordingly. */
11303 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11304 {
11305 if (gdbarch_address_class_type_flags_p (gdbarch))
11306 {
11307 int type_flags;
11308
11309 type_flags = gdbarch_address_class_type_flags
11310 (gdbarch, byte_size, addr_class);
11311 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11312 == 0);
11313 type = make_type_with_address_space (type, type_flags);
11314 }
11315 else if (TYPE_LENGTH (type) != byte_size)
11316 {
11317 complaint (&symfile_complaints,
11318 _("invalid pointer size %d"), byte_size);
11319 }
11320 else
11321 {
11322 /* Should we also complain about unhandled address classes? */
11323 }
11324 }
11325
11326 TYPE_LENGTH (type) = byte_size;
11327 return set_die_type (die, type, cu);
11328 }
11329
11330 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11331 the user defined type vector. */
11332
11333 static struct type *
11334 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11335 {
11336 struct type *type;
11337 struct type *to_type;
11338 struct type *domain;
11339
11340 to_type = die_type (die, cu);
11341 domain = die_containing_type (die, cu);
11342
11343 /* The calls above may have already set the type for this DIE. */
11344 type = get_die_type (die, cu);
11345 if (type)
11346 return type;
11347
11348 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11349 type = lookup_methodptr_type (to_type);
11350 else
11351 type = lookup_memberptr_type (to_type, domain);
11352
11353 return set_die_type (die, type, cu);
11354 }
11355
11356 /* Extract all information from a DW_TAG_reference_type DIE and add to
11357 the user defined type vector. */
11358
11359 static struct type *
11360 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11361 {
11362 struct comp_unit_head *cu_header = &cu->header;
11363 struct type *type, *target_type;
11364 struct attribute *attr;
11365
11366 target_type = die_type (die, cu);
11367
11368 /* The die_type call above may have already set the type for this DIE. */
11369 type = get_die_type (die, cu);
11370 if (type)
11371 return type;
11372
11373 type = lookup_reference_type (target_type);
11374 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11375 if (attr)
11376 {
11377 TYPE_LENGTH (type) = DW_UNSND (attr);
11378 }
11379 else
11380 {
11381 TYPE_LENGTH (type) = cu_header->addr_size;
11382 }
11383 return set_die_type (die, type, cu);
11384 }
11385
11386 static struct type *
11387 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11388 {
11389 struct type *base_type, *cv_type;
11390
11391 base_type = die_type (die, cu);
11392
11393 /* The die_type call above may have already set the type for this DIE. */
11394 cv_type = get_die_type (die, cu);
11395 if (cv_type)
11396 return cv_type;
11397
11398 /* In case the const qualifier is applied to an array type, the element type
11399 is so qualified, not the array type (section 6.7.3 of C99). */
11400 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11401 {
11402 struct type *el_type, *inner_array;
11403
11404 base_type = copy_type (base_type);
11405 inner_array = base_type;
11406
11407 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11408 {
11409 TYPE_TARGET_TYPE (inner_array) =
11410 copy_type (TYPE_TARGET_TYPE (inner_array));
11411 inner_array = TYPE_TARGET_TYPE (inner_array);
11412 }
11413
11414 el_type = TYPE_TARGET_TYPE (inner_array);
11415 TYPE_TARGET_TYPE (inner_array) =
11416 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11417
11418 return set_die_type (die, base_type, cu);
11419 }
11420
11421 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11422 return set_die_type (die, cv_type, cu);
11423 }
11424
11425 static struct type *
11426 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11427 {
11428 struct type *base_type, *cv_type;
11429
11430 base_type = die_type (die, cu);
11431
11432 /* The die_type call above may have already set the type for this DIE. */
11433 cv_type = get_die_type (die, cu);
11434 if (cv_type)
11435 return cv_type;
11436
11437 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11438 return set_die_type (die, cv_type, cu);
11439 }
11440
11441 /* Extract all information from a DW_TAG_string_type DIE and add to
11442 the user defined type vector. It isn't really a user defined type,
11443 but it behaves like one, with other DIE's using an AT_user_def_type
11444 attribute to reference it. */
11445
11446 static struct type *
11447 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11448 {
11449 struct objfile *objfile = cu->objfile;
11450 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11451 struct type *type, *range_type, *index_type, *char_type;
11452 struct attribute *attr;
11453 unsigned int length;
11454
11455 attr = dwarf2_attr (die, DW_AT_string_length, cu);
11456 if (attr)
11457 {
11458 length = DW_UNSND (attr);
11459 }
11460 else
11461 {
11462 /* Check for the DW_AT_byte_size attribute. */
11463 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11464 if (attr)
11465 {
11466 length = DW_UNSND (attr);
11467 }
11468 else
11469 {
11470 length = 1;
11471 }
11472 }
11473
11474 index_type = objfile_type (objfile)->builtin_int;
11475 range_type = create_range_type (NULL, index_type, 1, length);
11476 char_type = language_string_char_type (cu->language_defn, gdbarch);
11477 type = create_string_type (NULL, char_type, range_type);
11478
11479 return set_die_type (die, type, cu);
11480 }
11481
11482 /* Handle DIES due to C code like:
11483
11484 struct foo
11485 {
11486 int (*funcp)(int a, long l);
11487 int b;
11488 };
11489
11490 ('funcp' generates a DW_TAG_subroutine_type DIE). */
11491
11492 static struct type *
11493 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11494 {
11495 struct objfile *objfile = cu->objfile;
11496 struct type *type; /* Type that this function returns. */
11497 struct type *ftype; /* Function that returns above type. */
11498 struct attribute *attr;
11499
11500 type = die_type (die, cu);
11501
11502 /* The die_type call above may have already set the type for this DIE. */
11503 ftype = get_die_type (die, cu);
11504 if (ftype)
11505 return ftype;
11506
11507 ftype = lookup_function_type (type);
11508
11509 /* All functions in C++, Pascal and Java have prototypes. */
11510 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11511 if ((attr && (DW_UNSND (attr) != 0))
11512 || cu->language == language_cplus
11513 || cu->language == language_java
11514 || cu->language == language_pascal)
11515 TYPE_PROTOTYPED (ftype) = 1;
11516 else if (producer_is_realview (cu->producer))
11517 /* RealView does not emit DW_AT_prototyped. We can not
11518 distinguish prototyped and unprototyped functions; default to
11519 prototyped, since that is more common in modern code (and
11520 RealView warns about unprototyped functions). */
11521 TYPE_PROTOTYPED (ftype) = 1;
11522
11523 /* Store the calling convention in the type if it's available in
11524 the subroutine die. Otherwise set the calling convention to
11525 the default value DW_CC_normal. */
11526 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11527 if (attr)
11528 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11529 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11530 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11531 else
11532 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11533
11534 /* We need to add the subroutine type to the die immediately so
11535 we don't infinitely recurse when dealing with parameters
11536 declared as the same subroutine type. */
11537 set_die_type (die, ftype, cu);
11538
11539 if (die->child != NULL)
11540 {
11541 struct type *void_type = objfile_type (objfile)->builtin_void;
11542 struct die_info *child_die;
11543 int nparams, iparams;
11544
11545 /* Count the number of parameters.
11546 FIXME: GDB currently ignores vararg functions, but knows about
11547 vararg member functions. */
11548 nparams = 0;
11549 child_die = die->child;
11550 while (child_die && child_die->tag)
11551 {
11552 if (child_die->tag == DW_TAG_formal_parameter)
11553 nparams++;
11554 else if (child_die->tag == DW_TAG_unspecified_parameters)
11555 TYPE_VARARGS (ftype) = 1;
11556 child_die = sibling_die (child_die);
11557 }
11558
11559 /* Allocate storage for parameters and fill them in. */
11560 TYPE_NFIELDS (ftype) = nparams;
11561 TYPE_FIELDS (ftype) = (struct field *)
11562 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11563
11564 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11565 even if we error out during the parameters reading below. */
11566 for (iparams = 0; iparams < nparams; iparams++)
11567 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11568
11569 iparams = 0;
11570 child_die = die->child;
11571 while (child_die && child_die->tag)
11572 {
11573 if (child_die->tag == DW_TAG_formal_parameter)
11574 {
11575 struct type *arg_type;
11576
11577 /* DWARF version 2 has no clean way to discern C++
11578 static and non-static member functions. G++ helps
11579 GDB by marking the first parameter for non-static
11580 member functions (which is the this pointer) as
11581 artificial. We pass this information to
11582 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11583
11584 DWARF version 3 added DW_AT_object_pointer, which GCC
11585 4.5 does not yet generate. */
11586 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11587 if (attr)
11588 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11589 else
11590 {
11591 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11592
11593 /* GCC/43521: In java, the formal parameter
11594 "this" is sometimes not marked with DW_AT_artificial. */
11595 if (cu->language == language_java)
11596 {
11597 const char *name = dwarf2_name (child_die, cu);
11598
11599 if (name && !strcmp (name, "this"))
11600 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11601 }
11602 }
11603 arg_type = die_type (child_die, cu);
11604
11605 /* RealView does not mark THIS as const, which the testsuite
11606 expects. GCC marks THIS as const in method definitions,
11607 but not in the class specifications (GCC PR 43053). */
11608 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11609 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11610 {
11611 int is_this = 0;
11612 struct dwarf2_cu *arg_cu = cu;
11613 const char *name = dwarf2_name (child_die, cu);
11614
11615 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11616 if (attr)
11617 {
11618 /* If the compiler emits this, use it. */
11619 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11620 is_this = 1;
11621 }
11622 else if (name && strcmp (name, "this") == 0)
11623 /* Function definitions will have the argument names. */
11624 is_this = 1;
11625 else if (name == NULL && iparams == 0)
11626 /* Declarations may not have the names, so like
11627 elsewhere in GDB, assume an artificial first
11628 argument is "this". */
11629 is_this = 1;
11630
11631 if (is_this)
11632 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11633 arg_type, 0);
11634 }
11635
11636 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11637 iparams++;
11638 }
11639 child_die = sibling_die (child_die);
11640 }
11641 }
11642
11643 return ftype;
11644 }
11645
11646 static struct type *
11647 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11648 {
11649 struct objfile *objfile = cu->objfile;
11650 const char *name = NULL;
11651 struct type *this_type, *target_type;
11652
11653 name = dwarf2_full_name (NULL, die, cu);
11654 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11655 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11656 TYPE_NAME (this_type) = (char *) name;
11657 set_die_type (die, this_type, cu);
11658 target_type = die_type (die, cu);
11659 if (target_type != this_type)
11660 TYPE_TARGET_TYPE (this_type) = target_type;
11661 else
11662 {
11663 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11664 spec and cause infinite loops in GDB. */
11665 complaint (&symfile_complaints,
11666 _("Self-referential DW_TAG_typedef "
11667 "- DIE at 0x%x [in module %s]"),
11668 die->offset.sect_off, objfile->name);
11669 TYPE_TARGET_TYPE (this_type) = NULL;
11670 }
11671 return this_type;
11672 }
11673
11674 /* Find a representation of a given base type and install
11675 it in the TYPE field of the die. */
11676
11677 static struct type *
11678 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11679 {
11680 struct objfile *objfile = cu->objfile;
11681 struct type *type;
11682 struct attribute *attr;
11683 int encoding = 0, size = 0;
11684 char *name;
11685 enum type_code code = TYPE_CODE_INT;
11686 int type_flags = 0;
11687 struct type *target_type = NULL;
11688
11689 attr = dwarf2_attr (die, DW_AT_encoding, cu);
11690 if (attr)
11691 {
11692 encoding = DW_UNSND (attr);
11693 }
11694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11695 if (attr)
11696 {
11697 size = DW_UNSND (attr);
11698 }
11699 name = dwarf2_name (die, cu);
11700 if (!name)
11701 {
11702 complaint (&symfile_complaints,
11703 _("DW_AT_name missing from DW_TAG_base_type"));
11704 }
11705
11706 switch (encoding)
11707 {
11708 case DW_ATE_address:
11709 /* Turn DW_ATE_address into a void * pointer. */
11710 code = TYPE_CODE_PTR;
11711 type_flags |= TYPE_FLAG_UNSIGNED;
11712 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11713 break;
11714 case DW_ATE_boolean:
11715 code = TYPE_CODE_BOOL;
11716 type_flags |= TYPE_FLAG_UNSIGNED;
11717 break;
11718 case DW_ATE_complex_float:
11719 code = TYPE_CODE_COMPLEX;
11720 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11721 break;
11722 case DW_ATE_decimal_float:
11723 code = TYPE_CODE_DECFLOAT;
11724 break;
11725 case DW_ATE_float:
11726 code = TYPE_CODE_FLT;
11727 break;
11728 case DW_ATE_signed:
11729 break;
11730 case DW_ATE_unsigned:
11731 type_flags |= TYPE_FLAG_UNSIGNED;
11732 if (cu->language == language_fortran
11733 && name
11734 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11735 code = TYPE_CODE_CHAR;
11736 break;
11737 case DW_ATE_signed_char:
11738 if (cu->language == language_ada || cu->language == language_m2
11739 || cu->language == language_pascal
11740 || cu->language == language_fortran)
11741 code = TYPE_CODE_CHAR;
11742 break;
11743 case DW_ATE_unsigned_char:
11744 if (cu->language == language_ada || cu->language == language_m2
11745 || cu->language == language_pascal
11746 || cu->language == language_fortran)
11747 code = TYPE_CODE_CHAR;
11748 type_flags |= TYPE_FLAG_UNSIGNED;
11749 break;
11750 case DW_ATE_UTF:
11751 /* We just treat this as an integer and then recognize the
11752 type by name elsewhere. */
11753 break;
11754
11755 default:
11756 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11757 dwarf_type_encoding_name (encoding));
11758 break;
11759 }
11760
11761 type = init_type (code, size, type_flags, NULL, objfile);
11762 TYPE_NAME (type) = name;
11763 TYPE_TARGET_TYPE (type) = target_type;
11764
11765 if (name && strcmp (name, "char") == 0)
11766 TYPE_NOSIGN (type) = 1;
11767
11768 return set_die_type (die, type, cu);
11769 }
11770
11771 /* Read the given DW_AT_subrange DIE. */
11772
11773 static struct type *
11774 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11775 {
11776 struct type *base_type;
11777 struct type *range_type;
11778 struct attribute *attr;
11779 LONGEST low, high;
11780 int low_default_is_valid;
11781 char *name;
11782 LONGEST negative_mask;
11783
11784 base_type = die_type (die, cu);
11785 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11786 check_typedef (base_type);
11787
11788 /* The die_type call above may have already set the type for this DIE. */
11789 range_type = get_die_type (die, cu);
11790 if (range_type)
11791 return range_type;
11792
11793 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11794 omitting DW_AT_lower_bound. */
11795 switch (cu->language)
11796 {
11797 case language_c:
11798 case language_cplus:
11799 low = 0;
11800 low_default_is_valid = 1;
11801 break;
11802 case language_fortran:
11803 low = 1;
11804 low_default_is_valid = 1;
11805 break;
11806 case language_d:
11807 case language_java:
11808 case language_objc:
11809 low = 0;
11810 low_default_is_valid = (cu->header.version >= 4);
11811 break;
11812 case language_ada:
11813 case language_m2:
11814 case language_pascal:
11815 low = 1;
11816 low_default_is_valid = (cu->header.version >= 4);
11817 break;
11818 default:
11819 low = 0;
11820 low_default_is_valid = 0;
11821 break;
11822 }
11823
11824 /* FIXME: For variable sized arrays either of these could be
11825 a variable rather than a constant value. We'll allow it,
11826 but we don't know how to handle it. */
11827 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11828 if (attr)
11829 low = dwarf2_get_attr_constant_value (attr, low);
11830 else if (!low_default_is_valid)
11831 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11832 "- DIE at 0x%x [in module %s]"),
11833 die->offset.sect_off, cu->objfile->name);
11834
11835 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11836 if (attr)
11837 {
11838 if (attr_form_is_block (attr) || is_ref_attr (attr))
11839 {
11840 /* GCC encodes arrays with unspecified or dynamic length
11841 with a DW_FORM_block1 attribute or a reference attribute.
11842 FIXME: GDB does not yet know how to handle dynamic
11843 arrays properly, treat them as arrays with unspecified
11844 length for now.
11845
11846 FIXME: jimb/2003-09-22: GDB does not really know
11847 how to handle arrays of unspecified length
11848 either; we just represent them as zero-length
11849 arrays. Choose an appropriate upper bound given
11850 the lower bound we've computed above. */
11851 high = low - 1;
11852 }
11853 else
11854 high = dwarf2_get_attr_constant_value (attr, 1);
11855 }
11856 else
11857 {
11858 attr = dwarf2_attr (die, DW_AT_count, cu);
11859 if (attr)
11860 {
11861 int count = dwarf2_get_attr_constant_value (attr, 1);
11862 high = low + count - 1;
11863 }
11864 else
11865 {
11866 /* Unspecified array length. */
11867 high = low - 1;
11868 }
11869 }
11870
11871 /* Dwarf-2 specifications explicitly allows to create subrange types
11872 without specifying a base type.
11873 In that case, the base type must be set to the type of
11874 the lower bound, upper bound or count, in that order, if any of these
11875 three attributes references an object that has a type.
11876 If no base type is found, the Dwarf-2 specifications say that
11877 a signed integer type of size equal to the size of an address should
11878 be used.
11879 For the following C code: `extern char gdb_int [];'
11880 GCC produces an empty range DIE.
11881 FIXME: muller/2010-05-28: Possible references to object for low bound,
11882 high bound or count are not yet handled by this code. */
11883 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11884 {
11885 struct objfile *objfile = cu->objfile;
11886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11887 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11888 struct type *int_type = objfile_type (objfile)->builtin_int;
11889
11890 /* Test "int", "long int", and "long long int" objfile types,
11891 and select the first one having a size above or equal to the
11892 architecture address size. */
11893 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11894 base_type = int_type;
11895 else
11896 {
11897 int_type = objfile_type (objfile)->builtin_long;
11898 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11899 base_type = int_type;
11900 else
11901 {
11902 int_type = objfile_type (objfile)->builtin_long_long;
11903 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11904 base_type = int_type;
11905 }
11906 }
11907 }
11908
11909 negative_mask =
11910 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11911 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11912 low |= negative_mask;
11913 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11914 high |= negative_mask;
11915
11916 range_type = create_range_type (NULL, base_type, low, high);
11917
11918 /* Mark arrays with dynamic length at least as an array of unspecified
11919 length. GDB could check the boundary but before it gets implemented at
11920 least allow accessing the array elements. */
11921 if (attr && attr_form_is_block (attr))
11922 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11923
11924 /* Ada expects an empty array on no boundary attributes. */
11925 if (attr == NULL && cu->language != language_ada)
11926 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11927
11928 name = dwarf2_name (die, cu);
11929 if (name)
11930 TYPE_NAME (range_type) = name;
11931
11932 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11933 if (attr)
11934 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11935
11936 set_die_type (die, range_type, cu);
11937
11938 /* set_die_type should be already done. */
11939 set_descriptive_type (range_type, die, cu);
11940
11941 return range_type;
11942 }
11943
11944 static struct type *
11945 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11946 {
11947 struct type *type;
11948
11949 /* For now, we only support the C meaning of an unspecified type: void. */
11950
11951 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11952 TYPE_NAME (type) = dwarf2_name (die, cu);
11953
11954 return set_die_type (die, type, cu);
11955 }
11956
11957 /* Read a single die and all its descendents. Set the die's sibling
11958 field to NULL; set other fields in the die correctly, and set all
11959 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11960 location of the info_ptr after reading all of those dies. PARENT
11961 is the parent of the die in question. */
11962
11963 static struct die_info *
11964 read_die_and_children (const struct die_reader_specs *reader,
11965 gdb_byte *info_ptr,
11966 gdb_byte **new_info_ptr,
11967 struct die_info *parent)
11968 {
11969 struct die_info *die;
11970 gdb_byte *cur_ptr;
11971 int has_children;
11972
11973 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11974 if (die == NULL)
11975 {
11976 *new_info_ptr = cur_ptr;
11977 return NULL;
11978 }
11979 store_in_ref_table (die, reader->cu);
11980
11981 if (has_children)
11982 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11983 else
11984 {
11985 die->child = NULL;
11986 *new_info_ptr = cur_ptr;
11987 }
11988
11989 die->sibling = NULL;
11990 die->parent = parent;
11991 return die;
11992 }
11993
11994 /* Read a die, all of its descendents, and all of its siblings; set
11995 all of the fields of all of the dies correctly. Arguments are as
11996 in read_die_and_children. */
11997
11998 static struct die_info *
11999 read_die_and_siblings (const struct die_reader_specs *reader,
12000 gdb_byte *info_ptr,
12001 gdb_byte **new_info_ptr,
12002 struct die_info *parent)
12003 {
12004 struct die_info *first_die, *last_sibling;
12005 gdb_byte *cur_ptr;
12006
12007 cur_ptr = info_ptr;
12008 first_die = last_sibling = NULL;
12009
12010 while (1)
12011 {
12012 struct die_info *die
12013 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12014
12015 if (die == NULL)
12016 {
12017 *new_info_ptr = cur_ptr;
12018 return first_die;
12019 }
12020
12021 if (!first_die)
12022 first_die = die;
12023 else
12024 last_sibling->sibling = die;
12025
12026 last_sibling = die;
12027 }
12028 }
12029
12030 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12031 attributes.
12032 The caller is responsible for filling in the extra attributes
12033 and updating (*DIEP)->num_attrs.
12034 Set DIEP to point to a newly allocated die with its information,
12035 except for its child, sibling, and parent fields.
12036 Set HAS_CHILDREN to tell whether the die has children or not. */
12037
12038 static gdb_byte *
12039 read_full_die_1 (const struct die_reader_specs *reader,
12040 struct die_info **diep, gdb_byte *info_ptr,
12041 int *has_children, int num_extra_attrs)
12042 {
12043 unsigned int abbrev_number, bytes_read, i;
12044 sect_offset offset;
12045 struct abbrev_info *abbrev;
12046 struct die_info *die;
12047 struct dwarf2_cu *cu = reader->cu;
12048 bfd *abfd = reader->abfd;
12049
12050 offset.sect_off = info_ptr - reader->buffer;
12051 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12052 info_ptr += bytes_read;
12053 if (!abbrev_number)
12054 {
12055 *diep = NULL;
12056 *has_children = 0;
12057 return info_ptr;
12058 }
12059
12060 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12061 if (!abbrev)
12062 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12063 abbrev_number,
12064 bfd_get_filename (abfd));
12065
12066 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12067 die->offset = offset;
12068 die->tag = abbrev->tag;
12069 die->abbrev = abbrev_number;
12070
12071 /* Make the result usable.
12072 The caller needs to update num_attrs after adding the extra
12073 attributes. */
12074 die->num_attrs = abbrev->num_attrs;
12075
12076 for (i = 0; i < abbrev->num_attrs; ++i)
12077 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12078 info_ptr);
12079
12080 *diep = die;
12081 *has_children = abbrev->has_children;
12082 return info_ptr;
12083 }
12084
12085 /* Read a die and all its attributes.
12086 Set DIEP to point to a newly allocated die with its information,
12087 except for its child, sibling, and parent fields.
12088 Set HAS_CHILDREN to tell whether the die has children or not. */
12089
12090 static gdb_byte *
12091 read_full_die (const struct die_reader_specs *reader,
12092 struct die_info **diep, gdb_byte *info_ptr,
12093 int *has_children)
12094 {
12095 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12096 }
12097 \f
12098 /* Abbreviation tables.
12099
12100 In DWARF version 2, the description of the debugging information is
12101 stored in a separate .debug_abbrev section. Before we read any
12102 dies from a section we read in all abbreviations and install them
12103 in a hash table. */
12104
12105 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12106
12107 static struct abbrev_info *
12108 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12109 {
12110 struct abbrev_info *abbrev;
12111
12112 abbrev = (struct abbrev_info *)
12113 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12114 memset (abbrev, 0, sizeof (struct abbrev_info));
12115 return abbrev;
12116 }
12117
12118 /* Add an abbreviation to the table. */
12119
12120 static void
12121 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12122 unsigned int abbrev_number,
12123 struct abbrev_info *abbrev)
12124 {
12125 unsigned int hash_number;
12126
12127 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12128 abbrev->next = abbrev_table->abbrevs[hash_number];
12129 abbrev_table->abbrevs[hash_number] = abbrev;
12130 }
12131
12132 /* Look up an abbrev in the table.
12133 Returns NULL if the abbrev is not found. */
12134
12135 static struct abbrev_info *
12136 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12137 unsigned int abbrev_number)
12138 {
12139 unsigned int hash_number;
12140 struct abbrev_info *abbrev;
12141
12142 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12143 abbrev = abbrev_table->abbrevs[hash_number];
12144
12145 while (abbrev)
12146 {
12147 if (abbrev->number == abbrev_number)
12148 return abbrev;
12149 abbrev = abbrev->next;
12150 }
12151 return NULL;
12152 }
12153
12154 /* Read in an abbrev table. */
12155
12156 static struct abbrev_table *
12157 abbrev_table_read_table (struct dwarf2_section_info *section,
12158 sect_offset offset)
12159 {
12160 struct objfile *objfile = dwarf2_per_objfile->objfile;
12161 bfd *abfd = section->asection->owner;
12162 struct abbrev_table *abbrev_table;
12163 gdb_byte *abbrev_ptr;
12164 struct abbrev_info *cur_abbrev;
12165 unsigned int abbrev_number, bytes_read, abbrev_name;
12166 unsigned int abbrev_form;
12167 struct attr_abbrev *cur_attrs;
12168 unsigned int allocated_attrs;
12169
12170 abbrev_table = XMALLOC (struct abbrev_table);
12171 abbrev_table->offset = offset;
12172 obstack_init (&abbrev_table->abbrev_obstack);
12173 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12174 (ABBREV_HASH_SIZE
12175 * sizeof (struct abbrev_info *)));
12176 memset (abbrev_table->abbrevs, 0,
12177 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12178
12179 dwarf2_read_section (objfile, section);
12180 abbrev_ptr = section->buffer + offset.sect_off;
12181 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12182 abbrev_ptr += bytes_read;
12183
12184 allocated_attrs = ATTR_ALLOC_CHUNK;
12185 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12186
12187 /* Loop until we reach an abbrev number of 0. */
12188 while (abbrev_number)
12189 {
12190 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12191
12192 /* read in abbrev header */
12193 cur_abbrev->number = abbrev_number;
12194 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12195 abbrev_ptr += bytes_read;
12196 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12197 abbrev_ptr += 1;
12198
12199 /* now read in declarations */
12200 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12201 abbrev_ptr += bytes_read;
12202 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12203 abbrev_ptr += bytes_read;
12204 while (abbrev_name)
12205 {
12206 if (cur_abbrev->num_attrs == allocated_attrs)
12207 {
12208 allocated_attrs += ATTR_ALLOC_CHUNK;
12209 cur_attrs
12210 = xrealloc (cur_attrs, (allocated_attrs
12211 * sizeof (struct attr_abbrev)));
12212 }
12213
12214 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12215 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12216 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12217 abbrev_ptr += bytes_read;
12218 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12219 abbrev_ptr += bytes_read;
12220 }
12221
12222 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12223 (cur_abbrev->num_attrs
12224 * sizeof (struct attr_abbrev)));
12225 memcpy (cur_abbrev->attrs, cur_attrs,
12226 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12227
12228 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12229
12230 /* Get next abbreviation.
12231 Under Irix6 the abbreviations for a compilation unit are not
12232 always properly terminated with an abbrev number of 0.
12233 Exit loop if we encounter an abbreviation which we have
12234 already read (which means we are about to read the abbreviations
12235 for the next compile unit) or if the end of the abbreviation
12236 table is reached. */
12237 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12238 break;
12239 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12240 abbrev_ptr += bytes_read;
12241 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12242 break;
12243 }
12244
12245 xfree (cur_attrs);
12246 return abbrev_table;
12247 }
12248
12249 /* Free the resources held by ABBREV_TABLE. */
12250
12251 static void
12252 abbrev_table_free (struct abbrev_table *abbrev_table)
12253 {
12254 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12255 xfree (abbrev_table);
12256 }
12257
12258 /* Same as abbrev_table_free but as a cleanup.
12259 We pass in a pointer to the pointer to the table so that we can
12260 set the pointer to NULL when we're done. It also simplifies
12261 build_type_unit_groups. */
12262
12263 static void
12264 abbrev_table_free_cleanup (void *table_ptr)
12265 {
12266 struct abbrev_table **abbrev_table_ptr = table_ptr;
12267
12268 if (*abbrev_table_ptr != NULL)
12269 abbrev_table_free (*abbrev_table_ptr);
12270 *abbrev_table_ptr = NULL;
12271 }
12272
12273 /* Read the abbrev table for CU from ABBREV_SECTION. */
12274
12275 static void
12276 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12277 struct dwarf2_section_info *abbrev_section)
12278 {
12279 cu->abbrev_table =
12280 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12281 }
12282
12283 /* Release the memory used by the abbrev table for a compilation unit. */
12284
12285 static void
12286 dwarf2_free_abbrev_table (void *ptr_to_cu)
12287 {
12288 struct dwarf2_cu *cu = ptr_to_cu;
12289
12290 abbrev_table_free (cu->abbrev_table);
12291 /* Set this to NULL so that we SEGV if we try to read it later,
12292 and also because free_comp_unit verifies this is NULL. */
12293 cu->abbrev_table = NULL;
12294 }
12295 \f
12296 /* Returns nonzero if TAG represents a type that we might generate a partial
12297 symbol for. */
12298
12299 static int
12300 is_type_tag_for_partial (int tag)
12301 {
12302 switch (tag)
12303 {
12304 #if 0
12305 /* Some types that would be reasonable to generate partial symbols for,
12306 that we don't at present. */
12307 case DW_TAG_array_type:
12308 case DW_TAG_file_type:
12309 case DW_TAG_ptr_to_member_type:
12310 case DW_TAG_set_type:
12311 case DW_TAG_string_type:
12312 case DW_TAG_subroutine_type:
12313 #endif
12314 case DW_TAG_base_type:
12315 case DW_TAG_class_type:
12316 case DW_TAG_interface_type:
12317 case DW_TAG_enumeration_type:
12318 case DW_TAG_structure_type:
12319 case DW_TAG_subrange_type:
12320 case DW_TAG_typedef:
12321 case DW_TAG_union_type:
12322 return 1;
12323 default:
12324 return 0;
12325 }
12326 }
12327
12328 /* Load all DIEs that are interesting for partial symbols into memory. */
12329
12330 static struct partial_die_info *
12331 load_partial_dies (const struct die_reader_specs *reader,
12332 gdb_byte *info_ptr, int building_psymtab)
12333 {
12334 struct dwarf2_cu *cu = reader->cu;
12335 struct objfile *objfile = cu->objfile;
12336 struct partial_die_info *part_die;
12337 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12338 struct abbrev_info *abbrev;
12339 unsigned int bytes_read;
12340 unsigned int load_all = 0;
12341 int nesting_level = 1;
12342
12343 parent_die = NULL;
12344 last_die = NULL;
12345
12346 gdb_assert (cu->per_cu != NULL);
12347 if (cu->per_cu->load_all_dies)
12348 load_all = 1;
12349
12350 cu->partial_dies
12351 = htab_create_alloc_ex (cu->header.length / 12,
12352 partial_die_hash,
12353 partial_die_eq,
12354 NULL,
12355 &cu->comp_unit_obstack,
12356 hashtab_obstack_allocate,
12357 dummy_obstack_deallocate);
12358
12359 part_die = obstack_alloc (&cu->comp_unit_obstack,
12360 sizeof (struct partial_die_info));
12361
12362 while (1)
12363 {
12364 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12365
12366 /* A NULL abbrev means the end of a series of children. */
12367 if (abbrev == NULL)
12368 {
12369 if (--nesting_level == 0)
12370 {
12371 /* PART_DIE was probably the last thing allocated on the
12372 comp_unit_obstack, so we could call obstack_free
12373 here. We don't do that because the waste is small,
12374 and will be cleaned up when we're done with this
12375 compilation unit. This way, we're also more robust
12376 against other users of the comp_unit_obstack. */
12377 return first_die;
12378 }
12379 info_ptr += bytes_read;
12380 last_die = parent_die;
12381 parent_die = parent_die->die_parent;
12382 continue;
12383 }
12384
12385 /* Check for template arguments. We never save these; if
12386 they're seen, we just mark the parent, and go on our way. */
12387 if (parent_die != NULL
12388 && cu->language == language_cplus
12389 && (abbrev->tag == DW_TAG_template_type_param
12390 || abbrev->tag == DW_TAG_template_value_param))
12391 {
12392 parent_die->has_template_arguments = 1;
12393
12394 if (!load_all)
12395 {
12396 /* We don't need a partial DIE for the template argument. */
12397 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12398 continue;
12399 }
12400 }
12401
12402 /* We only recurse into c++ subprograms looking for template arguments.
12403 Skip their other children. */
12404 if (!load_all
12405 && cu->language == language_cplus
12406 && parent_die != NULL
12407 && parent_die->tag == DW_TAG_subprogram)
12408 {
12409 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12410 continue;
12411 }
12412
12413 /* Check whether this DIE is interesting enough to save. Normally
12414 we would not be interested in members here, but there may be
12415 later variables referencing them via DW_AT_specification (for
12416 static members). */
12417 if (!load_all
12418 && !is_type_tag_for_partial (abbrev->tag)
12419 && abbrev->tag != DW_TAG_constant
12420 && abbrev->tag != DW_TAG_enumerator
12421 && abbrev->tag != DW_TAG_subprogram
12422 && abbrev->tag != DW_TAG_lexical_block
12423 && abbrev->tag != DW_TAG_variable
12424 && abbrev->tag != DW_TAG_namespace
12425 && abbrev->tag != DW_TAG_module
12426 && abbrev->tag != DW_TAG_member
12427 && abbrev->tag != DW_TAG_imported_unit)
12428 {
12429 /* Otherwise we skip to the next sibling, if any. */
12430 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12431 continue;
12432 }
12433
12434 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12435 info_ptr);
12436
12437 /* This two-pass algorithm for processing partial symbols has a
12438 high cost in cache pressure. Thus, handle some simple cases
12439 here which cover the majority of C partial symbols. DIEs
12440 which neither have specification tags in them, nor could have
12441 specification tags elsewhere pointing at them, can simply be
12442 processed and discarded.
12443
12444 This segment is also optional; scan_partial_symbols and
12445 add_partial_symbol will handle these DIEs if we chain
12446 them in normally. When compilers which do not emit large
12447 quantities of duplicate debug information are more common,
12448 this code can probably be removed. */
12449
12450 /* Any complete simple types at the top level (pretty much all
12451 of them, for a language without namespaces), can be processed
12452 directly. */
12453 if (parent_die == NULL
12454 && part_die->has_specification == 0
12455 && part_die->is_declaration == 0
12456 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12457 || part_die->tag == DW_TAG_base_type
12458 || part_die->tag == DW_TAG_subrange_type))
12459 {
12460 if (building_psymtab && part_die->name != NULL)
12461 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12462 VAR_DOMAIN, LOC_TYPEDEF,
12463 &objfile->static_psymbols,
12464 0, (CORE_ADDR) 0, cu->language, objfile);
12465 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12466 continue;
12467 }
12468
12469 /* The exception for DW_TAG_typedef with has_children above is
12470 a workaround of GCC PR debug/47510. In the case of this complaint
12471 type_name_no_tag_or_error will error on such types later.
12472
12473 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12474 it could not find the child DIEs referenced later, this is checked
12475 above. In correct DWARF DW_TAG_typedef should have no children. */
12476
12477 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12478 complaint (&symfile_complaints,
12479 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12480 "- DIE at 0x%x [in module %s]"),
12481 part_die->offset.sect_off, objfile->name);
12482
12483 /* If we're at the second level, and we're an enumerator, and
12484 our parent has no specification (meaning possibly lives in a
12485 namespace elsewhere), then we can add the partial symbol now
12486 instead of queueing it. */
12487 if (part_die->tag == DW_TAG_enumerator
12488 && parent_die != NULL
12489 && parent_die->die_parent == NULL
12490 && parent_die->tag == DW_TAG_enumeration_type
12491 && parent_die->has_specification == 0)
12492 {
12493 if (part_die->name == NULL)
12494 complaint (&symfile_complaints,
12495 _("malformed enumerator DIE ignored"));
12496 else if (building_psymtab)
12497 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12498 VAR_DOMAIN, LOC_CONST,
12499 (cu->language == language_cplus
12500 || cu->language == language_java)
12501 ? &objfile->global_psymbols
12502 : &objfile->static_psymbols,
12503 0, (CORE_ADDR) 0, cu->language, objfile);
12504
12505 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12506 continue;
12507 }
12508
12509 /* We'll save this DIE so link it in. */
12510 part_die->die_parent = parent_die;
12511 part_die->die_sibling = NULL;
12512 part_die->die_child = NULL;
12513
12514 if (last_die && last_die == parent_die)
12515 last_die->die_child = part_die;
12516 else if (last_die)
12517 last_die->die_sibling = part_die;
12518
12519 last_die = part_die;
12520
12521 if (first_die == NULL)
12522 first_die = part_die;
12523
12524 /* Maybe add the DIE to the hash table. Not all DIEs that we
12525 find interesting need to be in the hash table, because we
12526 also have the parent/sibling/child chains; only those that we
12527 might refer to by offset later during partial symbol reading.
12528
12529 For now this means things that might have be the target of a
12530 DW_AT_specification, DW_AT_abstract_origin, or
12531 DW_AT_extension. DW_AT_extension will refer only to
12532 namespaces; DW_AT_abstract_origin refers to functions (and
12533 many things under the function DIE, but we do not recurse
12534 into function DIEs during partial symbol reading) and
12535 possibly variables as well; DW_AT_specification refers to
12536 declarations. Declarations ought to have the DW_AT_declaration
12537 flag. It happens that GCC forgets to put it in sometimes, but
12538 only for functions, not for types.
12539
12540 Adding more things than necessary to the hash table is harmless
12541 except for the performance cost. Adding too few will result in
12542 wasted time in find_partial_die, when we reread the compilation
12543 unit with load_all_dies set. */
12544
12545 if (load_all
12546 || abbrev->tag == DW_TAG_constant
12547 || abbrev->tag == DW_TAG_subprogram
12548 || abbrev->tag == DW_TAG_variable
12549 || abbrev->tag == DW_TAG_namespace
12550 || part_die->is_declaration)
12551 {
12552 void **slot;
12553
12554 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12555 part_die->offset.sect_off, INSERT);
12556 *slot = part_die;
12557 }
12558
12559 part_die = obstack_alloc (&cu->comp_unit_obstack,
12560 sizeof (struct partial_die_info));
12561
12562 /* For some DIEs we want to follow their children (if any). For C
12563 we have no reason to follow the children of structures; for other
12564 languages we have to, so that we can get at method physnames
12565 to infer fully qualified class names, for DW_AT_specification,
12566 and for C++ template arguments. For C++, we also look one level
12567 inside functions to find template arguments (if the name of the
12568 function does not already contain the template arguments).
12569
12570 For Ada, we need to scan the children of subprograms and lexical
12571 blocks as well because Ada allows the definition of nested
12572 entities that could be interesting for the debugger, such as
12573 nested subprograms for instance. */
12574 if (last_die->has_children
12575 && (load_all
12576 || last_die->tag == DW_TAG_namespace
12577 || last_die->tag == DW_TAG_module
12578 || last_die->tag == DW_TAG_enumeration_type
12579 || (cu->language == language_cplus
12580 && last_die->tag == DW_TAG_subprogram
12581 && (last_die->name == NULL
12582 || strchr (last_die->name, '<') == NULL))
12583 || (cu->language != language_c
12584 && (last_die->tag == DW_TAG_class_type
12585 || last_die->tag == DW_TAG_interface_type
12586 || last_die->tag == DW_TAG_structure_type
12587 || last_die->tag == DW_TAG_union_type))
12588 || (cu->language == language_ada
12589 && (last_die->tag == DW_TAG_subprogram
12590 || last_die->tag == DW_TAG_lexical_block))))
12591 {
12592 nesting_level++;
12593 parent_die = last_die;
12594 continue;
12595 }
12596
12597 /* Otherwise we skip to the next sibling, if any. */
12598 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12599
12600 /* Back to the top, do it again. */
12601 }
12602 }
12603
12604 /* Read a minimal amount of information into the minimal die structure. */
12605
12606 static gdb_byte *
12607 read_partial_die (const struct die_reader_specs *reader,
12608 struct partial_die_info *part_die,
12609 struct abbrev_info *abbrev, unsigned int abbrev_len,
12610 gdb_byte *info_ptr)
12611 {
12612 struct dwarf2_cu *cu = reader->cu;
12613 struct objfile *objfile = cu->objfile;
12614 gdb_byte *buffer = reader->buffer;
12615 unsigned int i;
12616 struct attribute attr;
12617 int has_low_pc_attr = 0;
12618 int has_high_pc_attr = 0;
12619 int high_pc_relative = 0;
12620
12621 memset (part_die, 0, sizeof (struct partial_die_info));
12622
12623 part_die->offset.sect_off = info_ptr - buffer;
12624
12625 info_ptr += abbrev_len;
12626
12627 if (abbrev == NULL)
12628 return info_ptr;
12629
12630 part_die->tag = abbrev->tag;
12631 part_die->has_children = abbrev->has_children;
12632
12633 for (i = 0; i < abbrev->num_attrs; ++i)
12634 {
12635 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12636
12637 /* Store the data if it is of an attribute we want to keep in a
12638 partial symbol table. */
12639 switch (attr.name)
12640 {
12641 case DW_AT_name:
12642 switch (part_die->tag)
12643 {
12644 case DW_TAG_compile_unit:
12645 case DW_TAG_partial_unit:
12646 case DW_TAG_type_unit:
12647 /* Compilation units have a DW_AT_name that is a filename, not
12648 a source language identifier. */
12649 case DW_TAG_enumeration_type:
12650 case DW_TAG_enumerator:
12651 /* These tags always have simple identifiers already; no need
12652 to canonicalize them. */
12653 part_die->name = DW_STRING (&attr);
12654 break;
12655 default:
12656 part_die->name
12657 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12658 &objfile->objfile_obstack);
12659 break;
12660 }
12661 break;
12662 case DW_AT_linkage_name:
12663 case DW_AT_MIPS_linkage_name:
12664 /* Note that both forms of linkage name might appear. We
12665 assume they will be the same, and we only store the last
12666 one we see. */
12667 if (cu->language == language_ada)
12668 part_die->name = DW_STRING (&attr);
12669 part_die->linkage_name = DW_STRING (&attr);
12670 break;
12671 case DW_AT_low_pc:
12672 has_low_pc_attr = 1;
12673 part_die->lowpc = DW_ADDR (&attr);
12674 break;
12675 case DW_AT_high_pc:
12676 has_high_pc_attr = 1;
12677 if (attr.form == DW_FORM_addr
12678 || attr.form == DW_FORM_GNU_addr_index)
12679 part_die->highpc = DW_ADDR (&attr);
12680 else
12681 {
12682 high_pc_relative = 1;
12683 part_die->highpc = DW_UNSND (&attr);
12684 }
12685 break;
12686 case DW_AT_location:
12687 /* Support the .debug_loc offsets. */
12688 if (attr_form_is_block (&attr))
12689 {
12690 part_die->d.locdesc = DW_BLOCK (&attr);
12691 }
12692 else if (attr_form_is_section_offset (&attr))
12693 {
12694 dwarf2_complex_location_expr_complaint ();
12695 }
12696 else
12697 {
12698 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12699 "partial symbol information");
12700 }
12701 break;
12702 case DW_AT_external:
12703 part_die->is_external = DW_UNSND (&attr);
12704 break;
12705 case DW_AT_declaration:
12706 part_die->is_declaration = DW_UNSND (&attr);
12707 break;
12708 case DW_AT_type:
12709 part_die->has_type = 1;
12710 break;
12711 case DW_AT_abstract_origin:
12712 case DW_AT_specification:
12713 case DW_AT_extension:
12714 part_die->has_specification = 1;
12715 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12716 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12717 || cu->per_cu->is_dwz);
12718 break;
12719 case DW_AT_sibling:
12720 /* Ignore absolute siblings, they might point outside of
12721 the current compile unit. */
12722 if (attr.form == DW_FORM_ref_addr)
12723 complaint (&symfile_complaints,
12724 _("ignoring absolute DW_AT_sibling"));
12725 else
12726 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12727 break;
12728 case DW_AT_byte_size:
12729 part_die->has_byte_size = 1;
12730 break;
12731 case DW_AT_calling_convention:
12732 /* DWARF doesn't provide a way to identify a program's source-level
12733 entry point. DW_AT_calling_convention attributes are only meant
12734 to describe functions' calling conventions.
12735
12736 However, because it's a necessary piece of information in
12737 Fortran, and because DW_CC_program is the only piece of debugging
12738 information whose definition refers to a 'main program' at all,
12739 several compilers have begun marking Fortran main programs with
12740 DW_CC_program --- even when those functions use the standard
12741 calling conventions.
12742
12743 So until DWARF specifies a way to provide this information and
12744 compilers pick up the new representation, we'll support this
12745 practice. */
12746 if (DW_UNSND (&attr) == DW_CC_program
12747 && cu->language == language_fortran)
12748 {
12749 set_main_name (part_die->name);
12750
12751 /* As this DIE has a static linkage the name would be difficult
12752 to look up later. */
12753 language_of_main = language_fortran;
12754 }
12755 break;
12756 case DW_AT_inline:
12757 if (DW_UNSND (&attr) == DW_INL_inlined
12758 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12759 part_die->may_be_inlined = 1;
12760 break;
12761
12762 case DW_AT_import:
12763 if (part_die->tag == DW_TAG_imported_unit)
12764 {
12765 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12766 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12767 || cu->per_cu->is_dwz);
12768 }
12769 break;
12770
12771 default:
12772 break;
12773 }
12774 }
12775
12776 if (high_pc_relative)
12777 part_die->highpc += part_die->lowpc;
12778
12779 if (has_low_pc_attr && has_high_pc_attr)
12780 {
12781 /* When using the GNU linker, .gnu.linkonce. sections are used to
12782 eliminate duplicate copies of functions and vtables and such.
12783 The linker will arbitrarily choose one and discard the others.
12784 The AT_*_pc values for such functions refer to local labels in
12785 these sections. If the section from that file was discarded, the
12786 labels are not in the output, so the relocs get a value of 0.
12787 If this is a discarded function, mark the pc bounds as invalid,
12788 so that GDB will ignore it. */
12789 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12790 {
12791 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12792
12793 complaint (&symfile_complaints,
12794 _("DW_AT_low_pc %s is zero "
12795 "for DIE at 0x%x [in module %s]"),
12796 paddress (gdbarch, part_die->lowpc),
12797 part_die->offset.sect_off, objfile->name);
12798 }
12799 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12800 else if (part_die->lowpc >= part_die->highpc)
12801 {
12802 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12803
12804 complaint (&symfile_complaints,
12805 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12806 "for DIE at 0x%x [in module %s]"),
12807 paddress (gdbarch, part_die->lowpc),
12808 paddress (gdbarch, part_die->highpc),
12809 part_die->offset.sect_off, objfile->name);
12810 }
12811 else
12812 part_die->has_pc_info = 1;
12813 }
12814
12815 return info_ptr;
12816 }
12817
12818 /* Find a cached partial DIE at OFFSET in CU. */
12819
12820 static struct partial_die_info *
12821 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12822 {
12823 struct partial_die_info *lookup_die = NULL;
12824 struct partial_die_info part_die;
12825
12826 part_die.offset = offset;
12827 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12828 offset.sect_off);
12829
12830 return lookup_die;
12831 }
12832
12833 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12834 except in the case of .debug_types DIEs which do not reference
12835 outside their CU (they do however referencing other types via
12836 DW_FORM_ref_sig8). */
12837
12838 static struct partial_die_info *
12839 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
12840 {
12841 struct objfile *objfile = cu->objfile;
12842 struct dwarf2_per_cu_data *per_cu = NULL;
12843 struct partial_die_info *pd = NULL;
12844
12845 if (offset_in_dwz == cu->per_cu->is_dwz
12846 && offset_in_cu_p (&cu->header, offset))
12847 {
12848 pd = find_partial_die_in_comp_unit (offset, cu);
12849 if (pd != NULL)
12850 return pd;
12851 /* We missed recording what we needed.
12852 Load all dies and try again. */
12853 per_cu = cu->per_cu;
12854 }
12855 else
12856 {
12857 /* TUs don't reference other CUs/TUs (except via type signatures). */
12858 if (cu->per_cu->is_debug_types)
12859 {
12860 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12861 " external reference to offset 0x%lx [in module %s].\n"),
12862 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12863 bfd_get_filename (objfile->obfd));
12864 }
12865 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12866 objfile);
12867
12868 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12869 load_partial_comp_unit (per_cu);
12870
12871 per_cu->cu->last_used = 0;
12872 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12873 }
12874
12875 /* If we didn't find it, and not all dies have been loaded,
12876 load them all and try again. */
12877
12878 if (pd == NULL && per_cu->load_all_dies == 0)
12879 {
12880 per_cu->load_all_dies = 1;
12881
12882 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12883 THIS_CU->cu may already be in use. So we can't just free it and
12884 replace its DIEs with the ones we read in. Instead, we leave those
12885 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12886 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12887 set. */
12888 load_partial_comp_unit (per_cu);
12889
12890 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12891 }
12892
12893 if (pd == NULL)
12894 internal_error (__FILE__, __LINE__,
12895 _("could not find partial DIE 0x%x "
12896 "in cache [from module %s]\n"),
12897 offset.sect_off, bfd_get_filename (objfile->obfd));
12898 return pd;
12899 }
12900
12901 /* See if we can figure out if the class lives in a namespace. We do
12902 this by looking for a member function; its demangled name will
12903 contain namespace info, if there is any. */
12904
12905 static void
12906 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12907 struct dwarf2_cu *cu)
12908 {
12909 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12910 what template types look like, because the demangler
12911 frequently doesn't give the same name as the debug info. We
12912 could fix this by only using the demangled name to get the
12913 prefix (but see comment in read_structure_type). */
12914
12915 struct partial_die_info *real_pdi;
12916 struct partial_die_info *child_pdi;
12917
12918 /* If this DIE (this DIE's specification, if any) has a parent, then
12919 we should not do this. We'll prepend the parent's fully qualified
12920 name when we create the partial symbol. */
12921
12922 real_pdi = struct_pdi;
12923 while (real_pdi->has_specification)
12924 real_pdi = find_partial_die (real_pdi->spec_offset,
12925 real_pdi->spec_is_dwz, cu);
12926
12927 if (real_pdi->die_parent != NULL)
12928 return;
12929
12930 for (child_pdi = struct_pdi->die_child;
12931 child_pdi != NULL;
12932 child_pdi = child_pdi->die_sibling)
12933 {
12934 if (child_pdi->tag == DW_TAG_subprogram
12935 && child_pdi->linkage_name != NULL)
12936 {
12937 char *actual_class_name
12938 = language_class_name_from_physname (cu->language_defn,
12939 child_pdi->linkage_name);
12940 if (actual_class_name != NULL)
12941 {
12942 struct_pdi->name
12943 = obsavestring (actual_class_name,
12944 strlen (actual_class_name),
12945 &cu->objfile->objfile_obstack);
12946 xfree (actual_class_name);
12947 }
12948 break;
12949 }
12950 }
12951 }
12952
12953 /* Adjust PART_DIE before generating a symbol for it. This function
12954 may set the is_external flag or change the DIE's name. */
12955
12956 static void
12957 fixup_partial_die (struct partial_die_info *part_die,
12958 struct dwarf2_cu *cu)
12959 {
12960 /* Once we've fixed up a die, there's no point in doing so again.
12961 This also avoids a memory leak if we were to call
12962 guess_partial_die_structure_name multiple times. */
12963 if (part_die->fixup_called)
12964 return;
12965
12966 /* If we found a reference attribute and the DIE has no name, try
12967 to find a name in the referred to DIE. */
12968
12969 if (part_die->name == NULL && part_die->has_specification)
12970 {
12971 struct partial_die_info *spec_die;
12972
12973 spec_die = find_partial_die (part_die->spec_offset,
12974 part_die->spec_is_dwz, cu);
12975
12976 fixup_partial_die (spec_die, cu);
12977
12978 if (spec_die->name)
12979 {
12980 part_die->name = spec_die->name;
12981
12982 /* Copy DW_AT_external attribute if it is set. */
12983 if (spec_die->is_external)
12984 part_die->is_external = spec_die->is_external;
12985 }
12986 }
12987
12988 /* Set default names for some unnamed DIEs. */
12989
12990 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12991 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12992
12993 /* If there is no parent die to provide a namespace, and there are
12994 children, see if we can determine the namespace from their linkage
12995 name. */
12996 if (cu->language == language_cplus
12997 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12998 && part_die->die_parent == NULL
12999 && part_die->has_children
13000 && (part_die->tag == DW_TAG_class_type
13001 || part_die->tag == DW_TAG_structure_type
13002 || part_die->tag == DW_TAG_union_type))
13003 guess_partial_die_structure_name (part_die, cu);
13004
13005 /* GCC might emit a nameless struct or union that has a linkage
13006 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13007 if (part_die->name == NULL
13008 && (part_die->tag == DW_TAG_class_type
13009 || part_die->tag == DW_TAG_interface_type
13010 || part_die->tag == DW_TAG_structure_type
13011 || part_die->tag == DW_TAG_union_type)
13012 && part_die->linkage_name != NULL)
13013 {
13014 char *demangled;
13015
13016 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13017 if (demangled)
13018 {
13019 const char *base;
13020
13021 /* Strip any leading namespaces/classes, keep only the base name.
13022 DW_AT_name for named DIEs does not contain the prefixes. */
13023 base = strrchr (demangled, ':');
13024 if (base && base > demangled && base[-1] == ':')
13025 base++;
13026 else
13027 base = demangled;
13028
13029 part_die->name = obsavestring (base, strlen (base),
13030 &cu->objfile->objfile_obstack);
13031 xfree (demangled);
13032 }
13033 }
13034
13035 part_die->fixup_called = 1;
13036 }
13037
13038 /* Read an attribute value described by an attribute form. */
13039
13040 static gdb_byte *
13041 read_attribute_value (const struct die_reader_specs *reader,
13042 struct attribute *attr, unsigned form,
13043 gdb_byte *info_ptr)
13044 {
13045 struct dwarf2_cu *cu = reader->cu;
13046 bfd *abfd = reader->abfd;
13047 struct comp_unit_head *cu_header = &cu->header;
13048 unsigned int bytes_read;
13049 struct dwarf_block *blk;
13050
13051 attr->form = form;
13052 switch (form)
13053 {
13054 case DW_FORM_ref_addr:
13055 if (cu->header.version == 2)
13056 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13057 else
13058 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13059 &cu->header, &bytes_read);
13060 info_ptr += bytes_read;
13061 break;
13062 case DW_FORM_GNU_ref_alt:
13063 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13064 info_ptr += bytes_read;
13065 break;
13066 case DW_FORM_addr:
13067 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13068 info_ptr += bytes_read;
13069 break;
13070 case DW_FORM_block2:
13071 blk = dwarf_alloc_block (cu);
13072 blk->size = read_2_bytes (abfd, info_ptr);
13073 info_ptr += 2;
13074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13075 info_ptr += blk->size;
13076 DW_BLOCK (attr) = blk;
13077 break;
13078 case DW_FORM_block4:
13079 blk = dwarf_alloc_block (cu);
13080 blk->size = read_4_bytes (abfd, info_ptr);
13081 info_ptr += 4;
13082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13083 info_ptr += blk->size;
13084 DW_BLOCK (attr) = blk;
13085 break;
13086 case DW_FORM_data2:
13087 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13088 info_ptr += 2;
13089 break;
13090 case DW_FORM_data4:
13091 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13092 info_ptr += 4;
13093 break;
13094 case DW_FORM_data8:
13095 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13096 info_ptr += 8;
13097 break;
13098 case DW_FORM_sec_offset:
13099 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13100 info_ptr += bytes_read;
13101 break;
13102 case DW_FORM_string:
13103 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13104 DW_STRING_IS_CANONICAL (attr) = 0;
13105 info_ptr += bytes_read;
13106 break;
13107 case DW_FORM_strp:
13108 if (!cu->per_cu->is_dwz)
13109 {
13110 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13111 &bytes_read);
13112 DW_STRING_IS_CANONICAL (attr) = 0;
13113 info_ptr += bytes_read;
13114 break;
13115 }
13116 /* FALLTHROUGH */
13117 case DW_FORM_GNU_strp_alt:
13118 {
13119 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13120 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13121 &bytes_read);
13122
13123 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13124 DW_STRING_IS_CANONICAL (attr) = 0;
13125 info_ptr += bytes_read;
13126 }
13127 break;
13128 case DW_FORM_exprloc:
13129 case DW_FORM_block:
13130 blk = dwarf_alloc_block (cu);
13131 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13132 info_ptr += bytes_read;
13133 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13134 info_ptr += blk->size;
13135 DW_BLOCK (attr) = blk;
13136 break;
13137 case DW_FORM_block1:
13138 blk = dwarf_alloc_block (cu);
13139 blk->size = read_1_byte (abfd, info_ptr);
13140 info_ptr += 1;
13141 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13142 info_ptr += blk->size;
13143 DW_BLOCK (attr) = blk;
13144 break;
13145 case DW_FORM_data1:
13146 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13147 info_ptr += 1;
13148 break;
13149 case DW_FORM_flag:
13150 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13151 info_ptr += 1;
13152 break;
13153 case DW_FORM_flag_present:
13154 DW_UNSND (attr) = 1;
13155 break;
13156 case DW_FORM_sdata:
13157 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13158 info_ptr += bytes_read;
13159 break;
13160 case DW_FORM_udata:
13161 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13162 info_ptr += bytes_read;
13163 break;
13164 case DW_FORM_ref1:
13165 DW_UNSND (attr) = (cu->header.offset.sect_off
13166 + read_1_byte (abfd, info_ptr));
13167 info_ptr += 1;
13168 break;
13169 case DW_FORM_ref2:
13170 DW_UNSND (attr) = (cu->header.offset.sect_off
13171 + read_2_bytes (abfd, info_ptr));
13172 info_ptr += 2;
13173 break;
13174 case DW_FORM_ref4:
13175 DW_UNSND (attr) = (cu->header.offset.sect_off
13176 + read_4_bytes (abfd, info_ptr));
13177 info_ptr += 4;
13178 break;
13179 case DW_FORM_ref8:
13180 DW_UNSND (attr) = (cu->header.offset.sect_off
13181 + read_8_bytes (abfd, info_ptr));
13182 info_ptr += 8;
13183 break;
13184 case DW_FORM_ref_sig8:
13185 /* Convert the signature to something we can record in DW_UNSND
13186 for later lookup.
13187 NOTE: This is NULL if the type wasn't found. */
13188 DW_SIGNATURED_TYPE (attr) =
13189 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
13190 info_ptr += 8;
13191 break;
13192 case DW_FORM_ref_udata:
13193 DW_UNSND (attr) = (cu->header.offset.sect_off
13194 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
13195 info_ptr += bytes_read;
13196 break;
13197 case DW_FORM_indirect:
13198 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13199 info_ptr += bytes_read;
13200 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
13201 break;
13202 case DW_FORM_GNU_addr_index:
13203 if (reader->dwo_file == NULL)
13204 {
13205 /* For now flag a hard error.
13206 Later we can turn this into a complaint. */
13207 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13208 dwarf_form_name (form),
13209 bfd_get_filename (abfd));
13210 }
13211 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13212 info_ptr += bytes_read;
13213 break;
13214 case DW_FORM_GNU_str_index:
13215 if (reader->dwo_file == NULL)
13216 {
13217 /* For now flag a hard error.
13218 Later we can turn this into a complaint if warranted. */
13219 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13220 dwarf_form_name (form),
13221 bfd_get_filename (abfd));
13222 }
13223 {
13224 ULONGEST str_index =
13225 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13226
13227 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13228 DW_STRING_IS_CANONICAL (attr) = 0;
13229 info_ptr += bytes_read;
13230 }
13231 break;
13232 default:
13233 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13234 dwarf_form_name (form),
13235 bfd_get_filename (abfd));
13236 }
13237
13238 /* Super hack. */
13239 if (cu->per_cu->is_dwz && is_ref_attr (attr))
13240 attr->form = DW_FORM_GNU_ref_alt;
13241
13242 /* We have seen instances where the compiler tried to emit a byte
13243 size attribute of -1 which ended up being encoded as an unsigned
13244 0xffffffff. Although 0xffffffff is technically a valid size value,
13245 an object of this size seems pretty unlikely so we can relatively
13246 safely treat these cases as if the size attribute was invalid and
13247 treat them as zero by default. */
13248 if (attr->name == DW_AT_byte_size
13249 && form == DW_FORM_data4
13250 && DW_UNSND (attr) >= 0xffffffff)
13251 {
13252 complaint
13253 (&symfile_complaints,
13254 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13255 hex_string (DW_UNSND (attr)));
13256 DW_UNSND (attr) = 0;
13257 }
13258
13259 return info_ptr;
13260 }
13261
13262 /* Read an attribute described by an abbreviated attribute. */
13263
13264 static gdb_byte *
13265 read_attribute (const struct die_reader_specs *reader,
13266 struct attribute *attr, struct attr_abbrev *abbrev,
13267 gdb_byte *info_ptr)
13268 {
13269 attr->name = abbrev->name;
13270 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13271 }
13272
13273 /* Read dwarf information from a buffer. */
13274
13275 static unsigned int
13276 read_1_byte (bfd *abfd, gdb_byte *buf)
13277 {
13278 return bfd_get_8 (abfd, buf);
13279 }
13280
13281 static int
13282 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13283 {
13284 return bfd_get_signed_8 (abfd, buf);
13285 }
13286
13287 static unsigned int
13288 read_2_bytes (bfd *abfd, gdb_byte *buf)
13289 {
13290 return bfd_get_16 (abfd, buf);
13291 }
13292
13293 static int
13294 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13295 {
13296 return bfd_get_signed_16 (abfd, buf);
13297 }
13298
13299 static unsigned int
13300 read_4_bytes (bfd *abfd, gdb_byte *buf)
13301 {
13302 return bfd_get_32 (abfd, buf);
13303 }
13304
13305 static int
13306 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13307 {
13308 return bfd_get_signed_32 (abfd, buf);
13309 }
13310
13311 static ULONGEST
13312 read_8_bytes (bfd *abfd, gdb_byte *buf)
13313 {
13314 return bfd_get_64 (abfd, buf);
13315 }
13316
13317 static CORE_ADDR
13318 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13319 unsigned int *bytes_read)
13320 {
13321 struct comp_unit_head *cu_header = &cu->header;
13322 CORE_ADDR retval = 0;
13323
13324 if (cu_header->signed_addr_p)
13325 {
13326 switch (cu_header->addr_size)
13327 {
13328 case 2:
13329 retval = bfd_get_signed_16 (abfd, buf);
13330 break;
13331 case 4:
13332 retval = bfd_get_signed_32 (abfd, buf);
13333 break;
13334 case 8:
13335 retval = bfd_get_signed_64 (abfd, buf);
13336 break;
13337 default:
13338 internal_error (__FILE__, __LINE__,
13339 _("read_address: bad switch, signed [in module %s]"),
13340 bfd_get_filename (abfd));
13341 }
13342 }
13343 else
13344 {
13345 switch (cu_header->addr_size)
13346 {
13347 case 2:
13348 retval = bfd_get_16 (abfd, buf);
13349 break;
13350 case 4:
13351 retval = bfd_get_32 (abfd, buf);
13352 break;
13353 case 8:
13354 retval = bfd_get_64 (abfd, buf);
13355 break;
13356 default:
13357 internal_error (__FILE__, __LINE__,
13358 _("read_address: bad switch, "
13359 "unsigned [in module %s]"),
13360 bfd_get_filename (abfd));
13361 }
13362 }
13363
13364 *bytes_read = cu_header->addr_size;
13365 return retval;
13366 }
13367
13368 /* Read the initial length from a section. The (draft) DWARF 3
13369 specification allows the initial length to take up either 4 bytes
13370 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13371 bytes describe the length and all offsets will be 8 bytes in length
13372 instead of 4.
13373
13374 An older, non-standard 64-bit format is also handled by this
13375 function. The older format in question stores the initial length
13376 as an 8-byte quantity without an escape value. Lengths greater
13377 than 2^32 aren't very common which means that the initial 4 bytes
13378 is almost always zero. Since a length value of zero doesn't make
13379 sense for the 32-bit format, this initial zero can be considered to
13380 be an escape value which indicates the presence of the older 64-bit
13381 format. As written, the code can't detect (old format) lengths
13382 greater than 4GB. If it becomes necessary to handle lengths
13383 somewhat larger than 4GB, we could allow other small values (such
13384 as the non-sensical values of 1, 2, and 3) to also be used as
13385 escape values indicating the presence of the old format.
13386
13387 The value returned via bytes_read should be used to increment the
13388 relevant pointer after calling read_initial_length().
13389
13390 [ Note: read_initial_length() and read_offset() are based on the
13391 document entitled "DWARF Debugging Information Format", revision
13392 3, draft 8, dated November 19, 2001. This document was obtained
13393 from:
13394
13395 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13396
13397 This document is only a draft and is subject to change. (So beware.)
13398
13399 Details regarding the older, non-standard 64-bit format were
13400 determined empirically by examining 64-bit ELF files produced by
13401 the SGI toolchain on an IRIX 6.5 machine.
13402
13403 - Kevin, July 16, 2002
13404 ] */
13405
13406 static LONGEST
13407 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13408 {
13409 LONGEST length = bfd_get_32 (abfd, buf);
13410
13411 if (length == 0xffffffff)
13412 {
13413 length = bfd_get_64 (abfd, buf + 4);
13414 *bytes_read = 12;
13415 }
13416 else if (length == 0)
13417 {
13418 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
13419 length = bfd_get_64 (abfd, buf);
13420 *bytes_read = 8;
13421 }
13422 else
13423 {
13424 *bytes_read = 4;
13425 }
13426
13427 return length;
13428 }
13429
13430 /* Cover function for read_initial_length.
13431 Returns the length of the object at BUF, and stores the size of the
13432 initial length in *BYTES_READ and stores the size that offsets will be in
13433 *OFFSET_SIZE.
13434 If the initial length size is not equivalent to that specified in
13435 CU_HEADER then issue a complaint.
13436 This is useful when reading non-comp-unit headers. */
13437
13438 static LONGEST
13439 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13440 const struct comp_unit_head *cu_header,
13441 unsigned int *bytes_read,
13442 unsigned int *offset_size)
13443 {
13444 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13445
13446 gdb_assert (cu_header->initial_length_size == 4
13447 || cu_header->initial_length_size == 8
13448 || cu_header->initial_length_size == 12);
13449
13450 if (cu_header->initial_length_size != *bytes_read)
13451 complaint (&symfile_complaints,
13452 _("intermixed 32-bit and 64-bit DWARF sections"));
13453
13454 *offset_size = (*bytes_read == 4) ? 4 : 8;
13455 return length;
13456 }
13457
13458 /* Read an offset from the data stream. The size of the offset is
13459 given by cu_header->offset_size. */
13460
13461 static LONGEST
13462 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13463 unsigned int *bytes_read)
13464 {
13465 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13466
13467 *bytes_read = cu_header->offset_size;
13468 return offset;
13469 }
13470
13471 /* Read an offset from the data stream. */
13472
13473 static LONGEST
13474 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13475 {
13476 LONGEST retval = 0;
13477
13478 switch (offset_size)
13479 {
13480 case 4:
13481 retval = bfd_get_32 (abfd, buf);
13482 break;
13483 case 8:
13484 retval = bfd_get_64 (abfd, buf);
13485 break;
13486 default:
13487 internal_error (__FILE__, __LINE__,
13488 _("read_offset_1: bad switch [in module %s]"),
13489 bfd_get_filename (abfd));
13490 }
13491
13492 return retval;
13493 }
13494
13495 static gdb_byte *
13496 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13497 {
13498 /* If the size of a host char is 8 bits, we can return a pointer
13499 to the buffer, otherwise we have to copy the data to a buffer
13500 allocated on the temporary obstack. */
13501 gdb_assert (HOST_CHAR_BIT == 8);
13502 return buf;
13503 }
13504
13505 static char *
13506 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13507 {
13508 /* If the size of a host char is 8 bits, we can return a pointer
13509 to the string, otherwise we have to copy the string to a buffer
13510 allocated on the temporary obstack. */
13511 gdb_assert (HOST_CHAR_BIT == 8);
13512 if (*buf == '\0')
13513 {
13514 *bytes_read_ptr = 1;
13515 return NULL;
13516 }
13517 *bytes_read_ptr = strlen ((char *) buf) + 1;
13518 return (char *) buf;
13519 }
13520
13521 static char *
13522 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13523 {
13524 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13525 if (dwarf2_per_objfile->str.buffer == NULL)
13526 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13527 bfd_get_filename (abfd));
13528 if (str_offset >= dwarf2_per_objfile->str.size)
13529 error (_("DW_FORM_strp pointing outside of "
13530 ".debug_str section [in module %s]"),
13531 bfd_get_filename (abfd));
13532 gdb_assert (HOST_CHAR_BIT == 8);
13533 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13534 return NULL;
13535 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13536 }
13537
13538 /* Read a string at offset STR_OFFSET in the .debug_str section from
13539 the .dwz file DWZ. Throw an error if the offset is too large. If
13540 the string consists of a single NUL byte, return NULL; otherwise
13541 return a pointer to the string. */
13542
13543 static char *
13544 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13545 {
13546 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13547
13548 if (dwz->str.buffer == NULL)
13549 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13550 "section [in module %s]"),
13551 bfd_get_filename (dwz->dwz_bfd));
13552 if (str_offset >= dwz->str.size)
13553 error (_("DW_FORM_GNU_strp_alt pointing outside of "
13554 ".debug_str section [in module %s]"),
13555 bfd_get_filename (dwz->dwz_bfd));
13556 gdb_assert (HOST_CHAR_BIT == 8);
13557 if (dwz->str.buffer[str_offset] == '\0')
13558 return NULL;
13559 return (char *) (dwz->str.buffer + str_offset);
13560 }
13561
13562 static char *
13563 read_indirect_string (bfd *abfd, gdb_byte *buf,
13564 const struct comp_unit_head *cu_header,
13565 unsigned int *bytes_read_ptr)
13566 {
13567 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13568
13569 return read_indirect_string_at_offset (abfd, str_offset);
13570 }
13571
13572 static ULONGEST
13573 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13574 {
13575 ULONGEST result;
13576 unsigned int num_read;
13577 int i, shift;
13578 unsigned char byte;
13579
13580 result = 0;
13581 shift = 0;
13582 num_read = 0;
13583 i = 0;
13584 while (1)
13585 {
13586 byte = bfd_get_8 (abfd, buf);
13587 buf++;
13588 num_read++;
13589 result |= ((ULONGEST) (byte & 127) << shift);
13590 if ((byte & 128) == 0)
13591 {
13592 break;
13593 }
13594 shift += 7;
13595 }
13596 *bytes_read_ptr = num_read;
13597 return result;
13598 }
13599
13600 static LONGEST
13601 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13602 {
13603 LONGEST result;
13604 int i, shift, num_read;
13605 unsigned char byte;
13606
13607 result = 0;
13608 shift = 0;
13609 num_read = 0;
13610 i = 0;
13611 while (1)
13612 {
13613 byte = bfd_get_8 (abfd, buf);
13614 buf++;
13615 num_read++;
13616 result |= ((LONGEST) (byte & 127) << shift);
13617 shift += 7;
13618 if ((byte & 128) == 0)
13619 {
13620 break;
13621 }
13622 }
13623 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13624 result |= -(((LONGEST) 1) << shift);
13625 *bytes_read_ptr = num_read;
13626 return result;
13627 }
13628
13629 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13630 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13631 ADDR_SIZE is the size of addresses from the CU header. */
13632
13633 static CORE_ADDR
13634 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13635 {
13636 struct objfile *objfile = dwarf2_per_objfile->objfile;
13637 bfd *abfd = objfile->obfd;
13638 const gdb_byte *info_ptr;
13639
13640 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13641 if (dwarf2_per_objfile->addr.buffer == NULL)
13642 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13643 objfile->name);
13644 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13645 error (_("DW_FORM_addr_index pointing outside of "
13646 ".debug_addr section [in module %s]"),
13647 objfile->name);
13648 info_ptr = (dwarf2_per_objfile->addr.buffer
13649 + addr_base + addr_index * addr_size);
13650 if (addr_size == 4)
13651 return bfd_get_32 (abfd, info_ptr);
13652 else
13653 return bfd_get_64 (abfd, info_ptr);
13654 }
13655
13656 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13657
13658 static CORE_ADDR
13659 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13660 {
13661 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13662 }
13663
13664 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13665
13666 static CORE_ADDR
13667 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13668 unsigned int *bytes_read)
13669 {
13670 bfd *abfd = cu->objfile->obfd;
13671 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13672
13673 return read_addr_index (cu, addr_index);
13674 }
13675
13676 /* Data structure to pass results from dwarf2_read_addr_index_reader
13677 back to dwarf2_read_addr_index. */
13678
13679 struct dwarf2_read_addr_index_data
13680 {
13681 ULONGEST addr_base;
13682 int addr_size;
13683 };
13684
13685 /* die_reader_func for dwarf2_read_addr_index. */
13686
13687 static void
13688 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13689 gdb_byte *info_ptr,
13690 struct die_info *comp_unit_die,
13691 int has_children,
13692 void *data)
13693 {
13694 struct dwarf2_cu *cu = reader->cu;
13695 struct dwarf2_read_addr_index_data *aidata =
13696 (struct dwarf2_read_addr_index_data *) data;
13697
13698 aidata->addr_base = cu->addr_base;
13699 aidata->addr_size = cu->header.addr_size;
13700 }
13701
13702 /* Given an index in .debug_addr, fetch the value.
13703 NOTE: This can be called during dwarf expression evaluation,
13704 long after the debug information has been read, and thus per_cu->cu
13705 may no longer exist. */
13706
13707 CORE_ADDR
13708 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13709 unsigned int addr_index)
13710 {
13711 struct objfile *objfile = per_cu->objfile;
13712 struct dwarf2_cu *cu = per_cu->cu;
13713 ULONGEST addr_base;
13714 int addr_size;
13715
13716 /* This is intended to be called from outside this file. */
13717 dw2_setup (objfile);
13718
13719 /* We need addr_base and addr_size.
13720 If we don't have PER_CU->cu, we have to get it.
13721 Nasty, but the alternative is storing the needed info in PER_CU,
13722 which at this point doesn't seem justified: it's not clear how frequently
13723 it would get used and it would increase the size of every PER_CU.
13724 Entry points like dwarf2_per_cu_addr_size do a similar thing
13725 so we're not in uncharted territory here.
13726 Alas we need to be a bit more complicated as addr_base is contained
13727 in the DIE.
13728
13729 We don't need to read the entire CU(/TU).
13730 We just need the header and top level die.
13731 IWBN to use the aging mechanism to let us lazily later discard the CU.
13732 See however init_cutu_and_read_dies_simple. */
13733
13734 if (cu != NULL)
13735 {
13736 addr_base = cu->addr_base;
13737 addr_size = cu->header.addr_size;
13738 }
13739 else
13740 {
13741 struct dwarf2_read_addr_index_data aidata;
13742
13743 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13744 &aidata);
13745 addr_base = aidata.addr_base;
13746 addr_size = aidata.addr_size;
13747 }
13748
13749 return read_addr_index_1 (addr_index, addr_base, addr_size);
13750 }
13751
13752 /* Given a DW_AT_str_index, fetch the string. */
13753
13754 static char *
13755 read_str_index (const struct die_reader_specs *reader,
13756 struct dwarf2_cu *cu, ULONGEST str_index)
13757 {
13758 struct objfile *objfile = dwarf2_per_objfile->objfile;
13759 const char *dwo_name = objfile->name;
13760 bfd *abfd = objfile->obfd;
13761 struct dwo_sections *sections = &reader->dwo_file->sections;
13762 gdb_byte *info_ptr;
13763 ULONGEST str_offset;
13764
13765 dwarf2_read_section (objfile, &sections->str);
13766 dwarf2_read_section (objfile, &sections->str_offsets);
13767 if (sections->str.buffer == NULL)
13768 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13769 " in CU at offset 0x%lx [in module %s]"),
13770 (long) cu->header.offset.sect_off, dwo_name);
13771 if (sections->str_offsets.buffer == NULL)
13772 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13773 " in CU at offset 0x%lx [in module %s]"),
13774 (long) cu->header.offset.sect_off, dwo_name);
13775 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13776 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13777 " section in CU at offset 0x%lx [in module %s]"),
13778 (long) cu->header.offset.sect_off, dwo_name);
13779 info_ptr = (sections->str_offsets.buffer
13780 + str_index * cu->header.offset_size);
13781 if (cu->header.offset_size == 4)
13782 str_offset = bfd_get_32 (abfd, info_ptr);
13783 else
13784 str_offset = bfd_get_64 (abfd, info_ptr);
13785 if (str_offset >= sections->str.size)
13786 error (_("Offset from DW_FORM_str_index pointing outside of"
13787 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13788 (long) cu->header.offset.sect_off, dwo_name);
13789 return (char *) (sections->str.buffer + str_offset);
13790 }
13791
13792 /* Return the length of an LEB128 number in BUF. */
13793
13794 static int
13795 leb128_size (const gdb_byte *buf)
13796 {
13797 const gdb_byte *begin = buf;
13798 gdb_byte byte;
13799
13800 while (1)
13801 {
13802 byte = *buf++;
13803 if ((byte & 128) == 0)
13804 return buf - begin;
13805 }
13806 }
13807
13808 static void
13809 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13810 {
13811 switch (lang)
13812 {
13813 case DW_LANG_C89:
13814 case DW_LANG_C99:
13815 case DW_LANG_C:
13816 cu->language = language_c;
13817 break;
13818 case DW_LANG_C_plus_plus:
13819 cu->language = language_cplus;
13820 break;
13821 case DW_LANG_D:
13822 cu->language = language_d;
13823 break;
13824 case DW_LANG_Fortran77:
13825 case DW_LANG_Fortran90:
13826 case DW_LANG_Fortran95:
13827 cu->language = language_fortran;
13828 break;
13829 case DW_LANG_Go:
13830 cu->language = language_go;
13831 break;
13832 case DW_LANG_Mips_Assembler:
13833 cu->language = language_asm;
13834 break;
13835 case DW_LANG_Java:
13836 cu->language = language_java;
13837 break;
13838 case DW_LANG_Ada83:
13839 case DW_LANG_Ada95:
13840 cu->language = language_ada;
13841 break;
13842 case DW_LANG_Modula2:
13843 cu->language = language_m2;
13844 break;
13845 case DW_LANG_Pascal83:
13846 cu->language = language_pascal;
13847 break;
13848 case DW_LANG_ObjC:
13849 cu->language = language_objc;
13850 break;
13851 case DW_LANG_Cobol74:
13852 case DW_LANG_Cobol85:
13853 default:
13854 cu->language = language_minimal;
13855 break;
13856 }
13857 cu->language_defn = language_def (cu->language);
13858 }
13859
13860 /* Return the named attribute or NULL if not there. */
13861
13862 static struct attribute *
13863 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13864 {
13865 for (;;)
13866 {
13867 unsigned int i;
13868 struct attribute *spec = NULL;
13869
13870 for (i = 0; i < die->num_attrs; ++i)
13871 {
13872 if (die->attrs[i].name == name)
13873 return &die->attrs[i];
13874 if (die->attrs[i].name == DW_AT_specification
13875 || die->attrs[i].name == DW_AT_abstract_origin)
13876 spec = &die->attrs[i];
13877 }
13878
13879 if (!spec)
13880 break;
13881
13882 die = follow_die_ref (die, spec, &cu);
13883 }
13884
13885 return NULL;
13886 }
13887
13888 /* Return the named attribute or NULL if not there,
13889 but do not follow DW_AT_specification, etc.
13890 This is for use in contexts where we're reading .debug_types dies.
13891 Following DW_AT_specification, DW_AT_abstract_origin will take us
13892 back up the chain, and we want to go down. */
13893
13894 static struct attribute *
13895 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13896 {
13897 unsigned int i;
13898
13899 for (i = 0; i < die->num_attrs; ++i)
13900 if (die->attrs[i].name == name)
13901 return &die->attrs[i];
13902
13903 return NULL;
13904 }
13905
13906 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13907 and holds a non-zero value. This function should only be used for
13908 DW_FORM_flag or DW_FORM_flag_present attributes. */
13909
13910 static int
13911 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13912 {
13913 struct attribute *attr = dwarf2_attr (die, name, cu);
13914
13915 return (attr && DW_UNSND (attr));
13916 }
13917
13918 static int
13919 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13920 {
13921 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13922 which value is non-zero. However, we have to be careful with
13923 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13924 (via dwarf2_flag_true_p) follows this attribute. So we may
13925 end up accidently finding a declaration attribute that belongs
13926 to a different DIE referenced by the specification attribute,
13927 even though the given DIE does not have a declaration attribute. */
13928 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13929 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13930 }
13931
13932 /* Return the die giving the specification for DIE, if there is
13933 one. *SPEC_CU is the CU containing DIE on input, and the CU
13934 containing the return value on output. If there is no
13935 specification, but there is an abstract origin, that is
13936 returned. */
13937
13938 static struct die_info *
13939 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13940 {
13941 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13942 *spec_cu);
13943
13944 if (spec_attr == NULL)
13945 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13946
13947 if (spec_attr == NULL)
13948 return NULL;
13949 else
13950 return follow_die_ref (die, spec_attr, spec_cu);
13951 }
13952
13953 /* Free the line_header structure *LH, and any arrays and strings it
13954 refers to.
13955 NOTE: This is also used as a "cleanup" function. */
13956
13957 static void
13958 free_line_header (struct line_header *lh)
13959 {
13960 if (lh->standard_opcode_lengths)
13961 xfree (lh->standard_opcode_lengths);
13962
13963 /* Remember that all the lh->file_names[i].name pointers are
13964 pointers into debug_line_buffer, and don't need to be freed. */
13965 if (lh->file_names)
13966 xfree (lh->file_names);
13967
13968 /* Similarly for the include directory names. */
13969 if (lh->include_dirs)
13970 xfree (lh->include_dirs);
13971
13972 xfree (lh);
13973 }
13974
13975 /* Add an entry to LH's include directory table. */
13976
13977 static void
13978 add_include_dir (struct line_header *lh, char *include_dir)
13979 {
13980 /* Grow the array if necessary. */
13981 if (lh->include_dirs_size == 0)
13982 {
13983 lh->include_dirs_size = 1; /* for testing */
13984 lh->include_dirs = xmalloc (lh->include_dirs_size
13985 * sizeof (*lh->include_dirs));
13986 }
13987 else if (lh->num_include_dirs >= lh->include_dirs_size)
13988 {
13989 lh->include_dirs_size *= 2;
13990 lh->include_dirs = xrealloc (lh->include_dirs,
13991 (lh->include_dirs_size
13992 * sizeof (*lh->include_dirs)));
13993 }
13994
13995 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13996 }
13997
13998 /* Add an entry to LH's file name table. */
13999
14000 static void
14001 add_file_name (struct line_header *lh,
14002 char *name,
14003 unsigned int dir_index,
14004 unsigned int mod_time,
14005 unsigned int length)
14006 {
14007 struct file_entry *fe;
14008
14009 /* Grow the array if necessary. */
14010 if (lh->file_names_size == 0)
14011 {
14012 lh->file_names_size = 1; /* for testing */
14013 lh->file_names = xmalloc (lh->file_names_size
14014 * sizeof (*lh->file_names));
14015 }
14016 else if (lh->num_file_names >= lh->file_names_size)
14017 {
14018 lh->file_names_size *= 2;
14019 lh->file_names = xrealloc (lh->file_names,
14020 (lh->file_names_size
14021 * sizeof (*lh->file_names)));
14022 }
14023
14024 fe = &lh->file_names[lh->num_file_names++];
14025 fe->name = name;
14026 fe->dir_index = dir_index;
14027 fe->mod_time = mod_time;
14028 fe->length = length;
14029 fe->included_p = 0;
14030 fe->symtab = NULL;
14031 }
14032
14033 /* A convenience function to find the proper .debug_line section for a
14034 CU. */
14035
14036 static struct dwarf2_section_info *
14037 get_debug_line_section (struct dwarf2_cu *cu)
14038 {
14039 struct dwarf2_section_info *section;
14040
14041 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14042 DWO file. */
14043 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14044 section = &cu->dwo_unit->dwo_file->sections.line;
14045 else if (cu->per_cu->is_dwz)
14046 {
14047 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14048
14049 section = &dwz->line;
14050 }
14051 else
14052 section = &dwarf2_per_objfile->line;
14053
14054 return section;
14055 }
14056
14057 /* Read the statement program header starting at OFFSET in
14058 .debug_line, or .debug_line.dwo. Return a pointer
14059 to a struct line_header, allocated using xmalloc.
14060
14061 NOTE: the strings in the include directory and file name tables of
14062 the returned object point into the dwarf line section buffer,
14063 and must not be freed. */
14064
14065 static struct line_header *
14066 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14067 {
14068 struct cleanup *back_to;
14069 struct line_header *lh;
14070 gdb_byte *line_ptr;
14071 unsigned int bytes_read, offset_size;
14072 int i;
14073 char *cur_dir, *cur_file;
14074 struct dwarf2_section_info *section;
14075 bfd *abfd;
14076
14077 section = get_debug_line_section (cu);
14078 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14079 if (section->buffer == NULL)
14080 {
14081 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14082 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14083 else
14084 complaint (&symfile_complaints, _("missing .debug_line section"));
14085 return 0;
14086 }
14087
14088 /* We can't do this until we know the section is non-empty.
14089 Only then do we know we have such a section. */
14090 abfd = section->asection->owner;
14091
14092 /* Make sure that at least there's room for the total_length field.
14093 That could be 12 bytes long, but we're just going to fudge that. */
14094 if (offset + 4 >= section->size)
14095 {
14096 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14097 return 0;
14098 }
14099
14100 lh = xmalloc (sizeof (*lh));
14101 memset (lh, 0, sizeof (*lh));
14102 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14103 (void *) lh);
14104
14105 line_ptr = section->buffer + offset;
14106
14107 /* Read in the header. */
14108 lh->total_length =
14109 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14110 &bytes_read, &offset_size);
14111 line_ptr += bytes_read;
14112 if (line_ptr + lh->total_length > (section->buffer + section->size))
14113 {
14114 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14115 return 0;
14116 }
14117 lh->statement_program_end = line_ptr + lh->total_length;
14118 lh->version = read_2_bytes (abfd, line_ptr);
14119 line_ptr += 2;
14120 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14121 line_ptr += offset_size;
14122 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14123 line_ptr += 1;
14124 if (lh->version >= 4)
14125 {
14126 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14127 line_ptr += 1;
14128 }
14129 else
14130 lh->maximum_ops_per_instruction = 1;
14131
14132 if (lh->maximum_ops_per_instruction == 0)
14133 {
14134 lh->maximum_ops_per_instruction = 1;
14135 complaint (&symfile_complaints,
14136 _("invalid maximum_ops_per_instruction "
14137 "in `.debug_line' section"));
14138 }
14139
14140 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14141 line_ptr += 1;
14142 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14143 line_ptr += 1;
14144 lh->line_range = read_1_byte (abfd, line_ptr);
14145 line_ptr += 1;
14146 lh->opcode_base = read_1_byte (abfd, line_ptr);
14147 line_ptr += 1;
14148 lh->standard_opcode_lengths
14149 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14150
14151 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14152 for (i = 1; i < lh->opcode_base; ++i)
14153 {
14154 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14155 line_ptr += 1;
14156 }
14157
14158 /* Read directory table. */
14159 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14160 {
14161 line_ptr += bytes_read;
14162 add_include_dir (lh, cur_dir);
14163 }
14164 line_ptr += bytes_read;
14165
14166 /* Read file name table. */
14167 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14168 {
14169 unsigned int dir_index, mod_time, length;
14170
14171 line_ptr += bytes_read;
14172 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14173 line_ptr += bytes_read;
14174 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14175 line_ptr += bytes_read;
14176 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14177 line_ptr += bytes_read;
14178
14179 add_file_name (lh, cur_file, dir_index, mod_time, length);
14180 }
14181 line_ptr += bytes_read;
14182 lh->statement_program_start = line_ptr;
14183
14184 if (line_ptr > (section->buffer + section->size))
14185 complaint (&symfile_complaints,
14186 _("line number info header doesn't "
14187 "fit in `.debug_line' section"));
14188
14189 discard_cleanups (back_to);
14190 return lh;
14191 }
14192
14193 /* Subroutine of dwarf_decode_lines to simplify it.
14194 Return the file name of the psymtab for included file FILE_INDEX
14195 in line header LH of PST.
14196 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14197 If space for the result is malloc'd, it will be freed by a cleanup.
14198 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
14199
14200 static char *
14201 psymtab_include_file_name (const struct line_header *lh, int file_index,
14202 const struct partial_symtab *pst,
14203 const char *comp_dir)
14204 {
14205 const struct file_entry fe = lh->file_names [file_index];
14206 char *include_name = fe.name;
14207 char *include_name_to_compare = include_name;
14208 char *dir_name = NULL;
14209 const char *pst_filename;
14210 char *copied_name = NULL;
14211 int file_is_pst;
14212
14213 if (fe.dir_index)
14214 dir_name = lh->include_dirs[fe.dir_index - 1];
14215
14216 if (!IS_ABSOLUTE_PATH (include_name)
14217 && (dir_name != NULL || comp_dir != NULL))
14218 {
14219 /* Avoid creating a duplicate psymtab for PST.
14220 We do this by comparing INCLUDE_NAME and PST_FILENAME.
14221 Before we do the comparison, however, we need to account
14222 for DIR_NAME and COMP_DIR.
14223 First prepend dir_name (if non-NULL). If we still don't
14224 have an absolute path prepend comp_dir (if non-NULL).
14225 However, the directory we record in the include-file's
14226 psymtab does not contain COMP_DIR (to match the
14227 corresponding symtab(s)).
14228
14229 Example:
14230
14231 bash$ cd /tmp
14232 bash$ gcc -g ./hello.c
14233 include_name = "hello.c"
14234 dir_name = "."
14235 DW_AT_comp_dir = comp_dir = "/tmp"
14236 DW_AT_name = "./hello.c" */
14237
14238 if (dir_name != NULL)
14239 {
14240 include_name = concat (dir_name, SLASH_STRING,
14241 include_name, (char *)NULL);
14242 include_name_to_compare = include_name;
14243 make_cleanup (xfree, include_name);
14244 }
14245 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14246 {
14247 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14248 include_name, (char *)NULL);
14249 }
14250 }
14251
14252 pst_filename = pst->filename;
14253 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14254 {
14255 copied_name = concat (pst->dirname, SLASH_STRING,
14256 pst_filename, (char *)NULL);
14257 pst_filename = copied_name;
14258 }
14259
14260 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14261
14262 if (include_name_to_compare != include_name)
14263 xfree (include_name_to_compare);
14264 if (copied_name != NULL)
14265 xfree (copied_name);
14266
14267 if (file_is_pst)
14268 return NULL;
14269 return include_name;
14270 }
14271
14272 /* Ignore this record_line request. */
14273
14274 static void
14275 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14276 {
14277 return;
14278 }
14279
14280 /* Subroutine of dwarf_decode_lines to simplify it.
14281 Process the line number information in LH. */
14282
14283 static void
14284 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14285 struct dwarf2_cu *cu, struct partial_symtab *pst)
14286 {
14287 gdb_byte *line_ptr, *extended_end;
14288 gdb_byte *line_end;
14289 unsigned int bytes_read, extended_len;
14290 unsigned char op_code, extended_op, adj_opcode;
14291 CORE_ADDR baseaddr;
14292 struct objfile *objfile = cu->objfile;
14293 bfd *abfd = objfile->obfd;
14294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14295 const int decode_for_pst_p = (pst != NULL);
14296 struct subfile *last_subfile = NULL;
14297 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14298 = record_line;
14299
14300 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14301
14302 line_ptr = lh->statement_program_start;
14303 line_end = lh->statement_program_end;
14304
14305 /* Read the statement sequences until there's nothing left. */
14306 while (line_ptr < line_end)
14307 {
14308 /* state machine registers */
14309 CORE_ADDR address = 0;
14310 unsigned int file = 1;
14311 unsigned int line = 1;
14312 unsigned int column = 0;
14313 int is_stmt = lh->default_is_stmt;
14314 int basic_block = 0;
14315 int end_sequence = 0;
14316 CORE_ADDR addr;
14317 unsigned char op_index = 0;
14318
14319 if (!decode_for_pst_p && lh->num_file_names >= file)
14320 {
14321 /* Start a subfile for the current file of the state machine. */
14322 /* lh->include_dirs and lh->file_names are 0-based, but the
14323 directory and file name numbers in the statement program
14324 are 1-based. */
14325 struct file_entry *fe = &lh->file_names[file - 1];
14326 char *dir = NULL;
14327
14328 if (fe->dir_index)
14329 dir = lh->include_dirs[fe->dir_index - 1];
14330
14331 dwarf2_start_subfile (fe->name, dir, comp_dir);
14332 }
14333
14334 /* Decode the table. */
14335 while (!end_sequence)
14336 {
14337 op_code = read_1_byte (abfd, line_ptr);
14338 line_ptr += 1;
14339 if (line_ptr > line_end)
14340 {
14341 dwarf2_debug_line_missing_end_sequence_complaint ();
14342 break;
14343 }
14344
14345 if (op_code >= lh->opcode_base)
14346 {
14347 /* Special operand. */
14348 adj_opcode = op_code - lh->opcode_base;
14349 address += (((op_index + (adj_opcode / lh->line_range))
14350 / lh->maximum_ops_per_instruction)
14351 * lh->minimum_instruction_length);
14352 op_index = ((op_index + (adj_opcode / lh->line_range))
14353 % lh->maximum_ops_per_instruction);
14354 line += lh->line_base + (adj_opcode % lh->line_range);
14355 if (lh->num_file_names < file || file == 0)
14356 dwarf2_debug_line_missing_file_complaint ();
14357 /* For now we ignore lines not starting on an
14358 instruction boundary. */
14359 else if (op_index == 0)
14360 {
14361 lh->file_names[file - 1].included_p = 1;
14362 if (!decode_for_pst_p && is_stmt)
14363 {
14364 if (last_subfile != current_subfile)
14365 {
14366 addr = gdbarch_addr_bits_remove (gdbarch, address);
14367 if (last_subfile)
14368 (*p_record_line) (last_subfile, 0, addr);
14369 last_subfile = current_subfile;
14370 }
14371 /* Append row to matrix using current values. */
14372 addr = gdbarch_addr_bits_remove (gdbarch, address);
14373 (*p_record_line) (current_subfile, line, addr);
14374 }
14375 }
14376 basic_block = 0;
14377 }
14378 else switch (op_code)
14379 {
14380 case DW_LNS_extended_op:
14381 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14382 &bytes_read);
14383 line_ptr += bytes_read;
14384 extended_end = line_ptr + extended_len;
14385 extended_op = read_1_byte (abfd, line_ptr);
14386 line_ptr += 1;
14387 switch (extended_op)
14388 {
14389 case DW_LNE_end_sequence:
14390 p_record_line = record_line;
14391 end_sequence = 1;
14392 break;
14393 case DW_LNE_set_address:
14394 address = read_address (abfd, line_ptr, cu, &bytes_read);
14395
14396 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14397 {
14398 /* This line table is for a function which has been
14399 GCd by the linker. Ignore it. PR gdb/12528 */
14400
14401 long line_offset
14402 = line_ptr - get_debug_line_section (cu)->buffer;
14403
14404 complaint (&symfile_complaints,
14405 _(".debug_line address at offset 0x%lx is 0 "
14406 "[in module %s]"),
14407 line_offset, objfile->name);
14408 p_record_line = noop_record_line;
14409 }
14410
14411 op_index = 0;
14412 line_ptr += bytes_read;
14413 address += baseaddr;
14414 break;
14415 case DW_LNE_define_file:
14416 {
14417 char *cur_file;
14418 unsigned int dir_index, mod_time, length;
14419
14420 cur_file = read_direct_string (abfd, line_ptr,
14421 &bytes_read);
14422 line_ptr += bytes_read;
14423 dir_index =
14424 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14425 line_ptr += bytes_read;
14426 mod_time =
14427 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14428 line_ptr += bytes_read;
14429 length =
14430 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14431 line_ptr += bytes_read;
14432 add_file_name (lh, cur_file, dir_index, mod_time, length);
14433 }
14434 break;
14435 case DW_LNE_set_discriminator:
14436 /* The discriminator is not interesting to the debugger;
14437 just ignore it. */
14438 line_ptr = extended_end;
14439 break;
14440 default:
14441 complaint (&symfile_complaints,
14442 _("mangled .debug_line section"));
14443 return;
14444 }
14445 /* Make sure that we parsed the extended op correctly. If e.g.
14446 we expected a different address size than the producer used,
14447 we may have read the wrong number of bytes. */
14448 if (line_ptr != extended_end)
14449 {
14450 complaint (&symfile_complaints,
14451 _("mangled .debug_line section"));
14452 return;
14453 }
14454 break;
14455 case DW_LNS_copy:
14456 if (lh->num_file_names < file || file == 0)
14457 dwarf2_debug_line_missing_file_complaint ();
14458 else
14459 {
14460 lh->file_names[file - 1].included_p = 1;
14461 if (!decode_for_pst_p && is_stmt)
14462 {
14463 if (last_subfile != current_subfile)
14464 {
14465 addr = gdbarch_addr_bits_remove (gdbarch, address);
14466 if (last_subfile)
14467 (*p_record_line) (last_subfile, 0, addr);
14468 last_subfile = current_subfile;
14469 }
14470 addr = gdbarch_addr_bits_remove (gdbarch, address);
14471 (*p_record_line) (current_subfile, line, addr);
14472 }
14473 }
14474 basic_block = 0;
14475 break;
14476 case DW_LNS_advance_pc:
14477 {
14478 CORE_ADDR adjust
14479 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14480
14481 address += (((op_index + adjust)
14482 / lh->maximum_ops_per_instruction)
14483 * lh->minimum_instruction_length);
14484 op_index = ((op_index + adjust)
14485 % lh->maximum_ops_per_instruction);
14486 line_ptr += bytes_read;
14487 }
14488 break;
14489 case DW_LNS_advance_line:
14490 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14491 line_ptr += bytes_read;
14492 break;
14493 case DW_LNS_set_file:
14494 {
14495 /* The arrays lh->include_dirs and lh->file_names are
14496 0-based, but the directory and file name numbers in
14497 the statement program are 1-based. */
14498 struct file_entry *fe;
14499 char *dir = NULL;
14500
14501 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14502 line_ptr += bytes_read;
14503 if (lh->num_file_names < file || file == 0)
14504 dwarf2_debug_line_missing_file_complaint ();
14505 else
14506 {
14507 fe = &lh->file_names[file - 1];
14508 if (fe->dir_index)
14509 dir = lh->include_dirs[fe->dir_index - 1];
14510 if (!decode_for_pst_p)
14511 {
14512 last_subfile = current_subfile;
14513 dwarf2_start_subfile (fe->name, dir, comp_dir);
14514 }
14515 }
14516 }
14517 break;
14518 case DW_LNS_set_column:
14519 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14520 line_ptr += bytes_read;
14521 break;
14522 case DW_LNS_negate_stmt:
14523 is_stmt = (!is_stmt);
14524 break;
14525 case DW_LNS_set_basic_block:
14526 basic_block = 1;
14527 break;
14528 /* Add to the address register of the state machine the
14529 address increment value corresponding to special opcode
14530 255. I.e., this value is scaled by the minimum
14531 instruction length since special opcode 255 would have
14532 scaled the increment. */
14533 case DW_LNS_const_add_pc:
14534 {
14535 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14536
14537 address += (((op_index + adjust)
14538 / lh->maximum_ops_per_instruction)
14539 * lh->minimum_instruction_length);
14540 op_index = ((op_index + adjust)
14541 % lh->maximum_ops_per_instruction);
14542 }
14543 break;
14544 case DW_LNS_fixed_advance_pc:
14545 address += read_2_bytes (abfd, line_ptr);
14546 op_index = 0;
14547 line_ptr += 2;
14548 break;
14549 default:
14550 {
14551 /* Unknown standard opcode, ignore it. */
14552 int i;
14553
14554 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14555 {
14556 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14557 line_ptr += bytes_read;
14558 }
14559 }
14560 }
14561 }
14562 if (lh->num_file_names < file || file == 0)
14563 dwarf2_debug_line_missing_file_complaint ();
14564 else
14565 {
14566 lh->file_names[file - 1].included_p = 1;
14567 if (!decode_for_pst_p)
14568 {
14569 addr = gdbarch_addr_bits_remove (gdbarch, address);
14570 (*p_record_line) (current_subfile, 0, addr);
14571 }
14572 }
14573 }
14574 }
14575
14576 /* Decode the Line Number Program (LNP) for the given line_header
14577 structure and CU. The actual information extracted and the type
14578 of structures created from the LNP depends on the value of PST.
14579
14580 1. If PST is NULL, then this procedure uses the data from the program
14581 to create all necessary symbol tables, and their linetables.
14582
14583 2. If PST is not NULL, this procedure reads the program to determine
14584 the list of files included by the unit represented by PST, and
14585 builds all the associated partial symbol tables.
14586
14587 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14588 It is used for relative paths in the line table.
14589 NOTE: When processing partial symtabs (pst != NULL),
14590 comp_dir == pst->dirname.
14591
14592 NOTE: It is important that psymtabs have the same file name (via strcmp)
14593 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14594 symtab we don't use it in the name of the psymtabs we create.
14595 E.g. expand_line_sal requires this when finding psymtabs to expand.
14596 A good testcase for this is mb-inline.exp. */
14597
14598 static void
14599 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14600 struct dwarf2_cu *cu, struct partial_symtab *pst,
14601 int want_line_info)
14602 {
14603 struct objfile *objfile = cu->objfile;
14604 const int decode_for_pst_p = (pst != NULL);
14605 struct subfile *first_subfile = current_subfile;
14606
14607 if (want_line_info)
14608 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14609
14610 if (decode_for_pst_p)
14611 {
14612 int file_index;
14613
14614 /* Now that we're done scanning the Line Header Program, we can
14615 create the psymtab of each included file. */
14616 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14617 if (lh->file_names[file_index].included_p == 1)
14618 {
14619 char *include_name =
14620 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14621 if (include_name != NULL)
14622 dwarf2_create_include_psymtab (include_name, pst, objfile);
14623 }
14624 }
14625 else
14626 {
14627 /* Make sure a symtab is created for every file, even files
14628 which contain only variables (i.e. no code with associated
14629 line numbers). */
14630 int i;
14631
14632 for (i = 0; i < lh->num_file_names; i++)
14633 {
14634 char *dir = NULL;
14635 struct file_entry *fe;
14636
14637 fe = &lh->file_names[i];
14638 if (fe->dir_index)
14639 dir = lh->include_dirs[fe->dir_index - 1];
14640 dwarf2_start_subfile (fe->name, dir, comp_dir);
14641
14642 /* Skip the main file; we don't need it, and it must be
14643 allocated last, so that it will show up before the
14644 non-primary symtabs in the objfile's symtab list. */
14645 if (current_subfile == first_subfile)
14646 continue;
14647
14648 if (current_subfile->symtab == NULL)
14649 current_subfile->symtab = allocate_symtab (current_subfile->name,
14650 objfile);
14651 fe->symtab = current_subfile->symtab;
14652 }
14653 }
14654 }
14655
14656 /* Start a subfile for DWARF. FILENAME is the name of the file and
14657 DIRNAME the name of the source directory which contains FILENAME
14658 or NULL if not known. COMP_DIR is the compilation directory for the
14659 linetable's compilation unit or NULL if not known.
14660 This routine tries to keep line numbers from identical absolute and
14661 relative file names in a common subfile.
14662
14663 Using the `list' example from the GDB testsuite, which resides in
14664 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14665 of /srcdir/list0.c yields the following debugging information for list0.c:
14666
14667 DW_AT_name: /srcdir/list0.c
14668 DW_AT_comp_dir: /compdir
14669 files.files[0].name: list0.h
14670 files.files[0].dir: /srcdir
14671 files.files[1].name: list0.c
14672 files.files[1].dir: /srcdir
14673
14674 The line number information for list0.c has to end up in a single
14675 subfile, so that `break /srcdir/list0.c:1' works as expected.
14676 start_subfile will ensure that this happens provided that we pass the
14677 concatenation of files.files[1].dir and files.files[1].name as the
14678 subfile's name. */
14679
14680 static void
14681 dwarf2_start_subfile (char *filename, const char *dirname,
14682 const char *comp_dir)
14683 {
14684 char *fullname;
14685
14686 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14687 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14688 second argument to start_subfile. To be consistent, we do the
14689 same here. In order not to lose the line information directory,
14690 we concatenate it to the filename when it makes sense.
14691 Note that the Dwarf3 standard says (speaking of filenames in line
14692 information): ``The directory index is ignored for file names
14693 that represent full path names''. Thus ignoring dirname in the
14694 `else' branch below isn't an issue. */
14695
14696 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14697 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14698 else
14699 fullname = filename;
14700
14701 start_subfile (fullname, comp_dir);
14702
14703 if (fullname != filename)
14704 xfree (fullname);
14705 }
14706
14707 /* Start a symtab for DWARF.
14708 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14709
14710 static void
14711 dwarf2_start_symtab (struct dwarf2_cu *cu,
14712 char *name, char *comp_dir, CORE_ADDR low_pc)
14713 {
14714 start_symtab (name, comp_dir, low_pc);
14715 record_debugformat ("DWARF 2");
14716 record_producer (cu->producer);
14717
14718 /* We assume that we're processing GCC output. */
14719 processing_gcc_compilation = 2;
14720
14721 processing_has_namespace_info = 0;
14722 }
14723
14724 static void
14725 var_decode_location (struct attribute *attr, struct symbol *sym,
14726 struct dwarf2_cu *cu)
14727 {
14728 struct objfile *objfile = cu->objfile;
14729 struct comp_unit_head *cu_header = &cu->header;
14730
14731 /* NOTE drow/2003-01-30: There used to be a comment and some special
14732 code here to turn a symbol with DW_AT_external and a
14733 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14734 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14735 with some versions of binutils) where shared libraries could have
14736 relocations against symbols in their debug information - the
14737 minimal symbol would have the right address, but the debug info
14738 would not. It's no longer necessary, because we will explicitly
14739 apply relocations when we read in the debug information now. */
14740
14741 /* A DW_AT_location attribute with no contents indicates that a
14742 variable has been optimized away. */
14743 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14744 {
14745 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14746 return;
14747 }
14748
14749 /* Handle one degenerate form of location expression specially, to
14750 preserve GDB's previous behavior when section offsets are
14751 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14752 then mark this symbol as LOC_STATIC. */
14753
14754 if (attr_form_is_block (attr)
14755 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14756 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14757 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14758 && (DW_BLOCK (attr)->size
14759 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14760 {
14761 unsigned int dummy;
14762
14763 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14764 SYMBOL_VALUE_ADDRESS (sym) =
14765 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14766 else
14767 SYMBOL_VALUE_ADDRESS (sym) =
14768 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14769 SYMBOL_CLASS (sym) = LOC_STATIC;
14770 fixup_symbol_section (sym, objfile);
14771 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14772 SYMBOL_SECTION (sym));
14773 return;
14774 }
14775
14776 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14777 expression evaluator, and use LOC_COMPUTED only when necessary
14778 (i.e. when the value of a register or memory location is
14779 referenced, or a thread-local block, etc.). Then again, it might
14780 not be worthwhile. I'm assuming that it isn't unless performance
14781 or memory numbers show me otherwise. */
14782
14783 dwarf2_symbol_mark_computed (attr, sym, cu);
14784 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14785
14786 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14787 cu->has_loclist = 1;
14788 }
14789
14790 /* Given a pointer to a DWARF information entry, figure out if we need
14791 to make a symbol table entry for it, and if so, create a new entry
14792 and return a pointer to it.
14793 If TYPE is NULL, determine symbol type from the die, otherwise
14794 used the passed type.
14795 If SPACE is not NULL, use it to hold the new symbol. If it is
14796 NULL, allocate a new symbol on the objfile's obstack. */
14797
14798 static struct symbol *
14799 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14800 struct symbol *space)
14801 {
14802 struct objfile *objfile = cu->objfile;
14803 struct symbol *sym = NULL;
14804 char *name;
14805 struct attribute *attr = NULL;
14806 struct attribute *attr2 = NULL;
14807 CORE_ADDR baseaddr;
14808 struct pending **list_to_add = NULL;
14809
14810 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14811
14812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14813
14814 name = dwarf2_name (die, cu);
14815 if (name)
14816 {
14817 const char *linkagename;
14818 int suppress_add = 0;
14819
14820 if (space)
14821 sym = space;
14822 else
14823 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14824 OBJSTAT (objfile, n_syms++);
14825
14826 /* Cache this symbol's name and the name's demangled form (if any). */
14827 SYMBOL_SET_LANGUAGE (sym, cu->language);
14828 linkagename = dwarf2_physname (name, die, cu);
14829 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14830
14831 /* Fortran does not have mangling standard and the mangling does differ
14832 between gfortran, iFort etc. */
14833 if (cu->language == language_fortran
14834 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14835 symbol_set_demangled_name (&(sym->ginfo),
14836 (char *) dwarf2_full_name (name, die, cu),
14837 NULL);
14838
14839 /* Default assumptions.
14840 Use the passed type or decode it from the die. */
14841 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14842 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14843 if (type != NULL)
14844 SYMBOL_TYPE (sym) = type;
14845 else
14846 SYMBOL_TYPE (sym) = die_type (die, cu);
14847 attr = dwarf2_attr (die,
14848 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14849 cu);
14850 if (attr)
14851 {
14852 SYMBOL_LINE (sym) = DW_UNSND (attr);
14853 }
14854
14855 attr = dwarf2_attr (die,
14856 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14857 cu);
14858 if (attr)
14859 {
14860 int file_index = DW_UNSND (attr);
14861
14862 if (cu->line_header == NULL
14863 || file_index > cu->line_header->num_file_names)
14864 complaint (&symfile_complaints,
14865 _("file index out of range"));
14866 else if (file_index > 0)
14867 {
14868 struct file_entry *fe;
14869
14870 fe = &cu->line_header->file_names[file_index - 1];
14871 SYMBOL_SYMTAB (sym) = fe->symtab;
14872 }
14873 }
14874
14875 switch (die->tag)
14876 {
14877 case DW_TAG_label:
14878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14879 if (attr)
14880 {
14881 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14882 }
14883 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14884 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14885 SYMBOL_CLASS (sym) = LOC_LABEL;
14886 add_symbol_to_list (sym, cu->list_in_scope);
14887 break;
14888 case DW_TAG_subprogram:
14889 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14890 finish_block. */
14891 SYMBOL_CLASS (sym) = LOC_BLOCK;
14892 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14893 if ((attr2 && (DW_UNSND (attr2) != 0))
14894 || cu->language == language_ada)
14895 {
14896 /* Subprograms marked external are stored as a global symbol.
14897 Ada subprograms, whether marked external or not, are always
14898 stored as a global symbol, because we want to be able to
14899 access them globally. For instance, we want to be able
14900 to break on a nested subprogram without having to
14901 specify the context. */
14902 list_to_add = &global_symbols;
14903 }
14904 else
14905 {
14906 list_to_add = cu->list_in_scope;
14907 }
14908 break;
14909 case DW_TAG_inlined_subroutine:
14910 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14911 finish_block. */
14912 SYMBOL_CLASS (sym) = LOC_BLOCK;
14913 SYMBOL_INLINED (sym) = 1;
14914 list_to_add = cu->list_in_scope;
14915 break;
14916 case DW_TAG_template_value_param:
14917 suppress_add = 1;
14918 /* Fall through. */
14919 case DW_TAG_constant:
14920 case DW_TAG_variable:
14921 case DW_TAG_member:
14922 /* Compilation with minimal debug info may result in
14923 variables with missing type entries. Change the
14924 misleading `void' type to something sensible. */
14925 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14926 SYMBOL_TYPE (sym)
14927 = objfile_type (objfile)->nodebug_data_symbol;
14928
14929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14930 /* In the case of DW_TAG_member, we should only be called for
14931 static const members. */
14932 if (die->tag == DW_TAG_member)
14933 {
14934 /* dwarf2_add_field uses die_is_declaration,
14935 so we do the same. */
14936 gdb_assert (die_is_declaration (die, cu));
14937 gdb_assert (attr);
14938 }
14939 if (attr)
14940 {
14941 dwarf2_const_value (attr, sym, cu);
14942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14943 if (!suppress_add)
14944 {
14945 if (attr2 && (DW_UNSND (attr2) != 0))
14946 list_to_add = &global_symbols;
14947 else
14948 list_to_add = cu->list_in_scope;
14949 }
14950 break;
14951 }
14952 attr = dwarf2_attr (die, DW_AT_location, cu);
14953 if (attr)
14954 {
14955 var_decode_location (attr, sym, cu);
14956 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14957 if (SYMBOL_CLASS (sym) == LOC_STATIC
14958 && SYMBOL_VALUE_ADDRESS (sym) == 0
14959 && !dwarf2_per_objfile->has_section_at_zero)
14960 {
14961 /* When a static variable is eliminated by the linker,
14962 the corresponding debug information is not stripped
14963 out, but the variable address is set to null;
14964 do not add such variables into symbol table. */
14965 }
14966 else if (attr2 && (DW_UNSND (attr2) != 0))
14967 {
14968 /* Workaround gfortran PR debug/40040 - it uses
14969 DW_AT_location for variables in -fPIC libraries which may
14970 get overriden by other libraries/executable and get
14971 a different address. Resolve it by the minimal symbol
14972 which may come from inferior's executable using copy
14973 relocation. Make this workaround only for gfortran as for
14974 other compilers GDB cannot guess the minimal symbol
14975 Fortran mangling kind. */
14976 if (cu->language == language_fortran && die->parent
14977 && die->parent->tag == DW_TAG_module
14978 && cu->producer
14979 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14980 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14981
14982 /* A variable with DW_AT_external is never static,
14983 but it may be block-scoped. */
14984 list_to_add = (cu->list_in_scope == &file_symbols
14985 ? &global_symbols : cu->list_in_scope);
14986 }
14987 else
14988 list_to_add = cu->list_in_scope;
14989 }
14990 else
14991 {
14992 /* We do not know the address of this symbol.
14993 If it is an external symbol and we have type information
14994 for it, enter the symbol as a LOC_UNRESOLVED symbol.
14995 The address of the variable will then be determined from
14996 the minimal symbol table whenever the variable is
14997 referenced. */
14998 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14999 if (attr2 && (DW_UNSND (attr2) != 0)
15000 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15001 {
15002 /* A variable with DW_AT_external is never static, but it
15003 may be block-scoped. */
15004 list_to_add = (cu->list_in_scope == &file_symbols
15005 ? &global_symbols : cu->list_in_scope);
15006
15007 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15008 }
15009 else if (!die_is_declaration (die, cu))
15010 {
15011 /* Use the default LOC_OPTIMIZED_OUT class. */
15012 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15013 if (!suppress_add)
15014 list_to_add = cu->list_in_scope;
15015 }
15016 }
15017 break;
15018 case DW_TAG_formal_parameter:
15019 /* If we are inside a function, mark this as an argument. If
15020 not, we might be looking at an argument to an inlined function
15021 when we do not have enough information to show inlined frames;
15022 pretend it's a local variable in that case so that the user can
15023 still see it. */
15024 if (context_stack_depth > 0
15025 && context_stack[context_stack_depth - 1].name != NULL)
15026 SYMBOL_IS_ARGUMENT (sym) = 1;
15027 attr = dwarf2_attr (die, DW_AT_location, cu);
15028 if (attr)
15029 {
15030 var_decode_location (attr, sym, cu);
15031 }
15032 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15033 if (attr)
15034 {
15035 dwarf2_const_value (attr, sym, cu);
15036 }
15037
15038 list_to_add = cu->list_in_scope;
15039 break;
15040 case DW_TAG_unspecified_parameters:
15041 /* From varargs functions; gdb doesn't seem to have any
15042 interest in this information, so just ignore it for now.
15043 (FIXME?) */
15044 break;
15045 case DW_TAG_template_type_param:
15046 suppress_add = 1;
15047 /* Fall through. */
15048 case DW_TAG_class_type:
15049 case DW_TAG_interface_type:
15050 case DW_TAG_structure_type:
15051 case DW_TAG_union_type:
15052 case DW_TAG_set_type:
15053 case DW_TAG_enumeration_type:
15054 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15055 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15056
15057 {
15058 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15059 really ever be static objects: otherwise, if you try
15060 to, say, break of a class's method and you're in a file
15061 which doesn't mention that class, it won't work unless
15062 the check for all static symbols in lookup_symbol_aux
15063 saves you. See the OtherFileClass tests in
15064 gdb.c++/namespace.exp. */
15065
15066 if (!suppress_add)
15067 {
15068 list_to_add = (cu->list_in_scope == &file_symbols
15069 && (cu->language == language_cplus
15070 || cu->language == language_java)
15071 ? &global_symbols : cu->list_in_scope);
15072
15073 /* The semantics of C++ state that "struct foo {
15074 ... }" also defines a typedef for "foo". A Java
15075 class declaration also defines a typedef for the
15076 class. */
15077 if (cu->language == language_cplus
15078 || cu->language == language_java
15079 || cu->language == language_ada)
15080 {
15081 /* The symbol's name is already allocated along
15082 with this objfile, so we don't need to
15083 duplicate it for the type. */
15084 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15085 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15086 }
15087 }
15088 }
15089 break;
15090 case DW_TAG_typedef:
15091 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15092 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15093 list_to_add = cu->list_in_scope;
15094 break;
15095 case DW_TAG_base_type:
15096 case DW_TAG_subrange_type:
15097 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15098 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15099 list_to_add = cu->list_in_scope;
15100 break;
15101 case DW_TAG_enumerator:
15102 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15103 if (attr)
15104 {
15105 dwarf2_const_value (attr, sym, cu);
15106 }
15107 {
15108 /* NOTE: carlton/2003-11-10: See comment above in the
15109 DW_TAG_class_type, etc. block. */
15110
15111 list_to_add = (cu->list_in_scope == &file_symbols
15112 && (cu->language == language_cplus
15113 || cu->language == language_java)
15114 ? &global_symbols : cu->list_in_scope);
15115 }
15116 break;
15117 case DW_TAG_namespace:
15118 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15119 list_to_add = &global_symbols;
15120 break;
15121 default:
15122 /* Not a tag we recognize. Hopefully we aren't processing
15123 trash data, but since we must specifically ignore things
15124 we don't recognize, there is nothing else we should do at
15125 this point. */
15126 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15127 dwarf_tag_name (die->tag));
15128 break;
15129 }
15130
15131 if (suppress_add)
15132 {
15133 sym->hash_next = objfile->template_symbols;
15134 objfile->template_symbols = sym;
15135 list_to_add = NULL;
15136 }
15137
15138 if (list_to_add != NULL)
15139 add_symbol_to_list (sym, list_to_add);
15140
15141 /* For the benefit of old versions of GCC, check for anonymous
15142 namespaces based on the demangled name. */
15143 if (!processing_has_namespace_info
15144 && cu->language == language_cplus)
15145 cp_scan_for_anonymous_namespaces (sym, objfile);
15146 }
15147 return (sym);
15148 }
15149
15150 /* A wrapper for new_symbol_full that always allocates a new symbol. */
15151
15152 static struct symbol *
15153 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15154 {
15155 return new_symbol_full (die, type, cu, NULL);
15156 }
15157
15158 /* Given an attr with a DW_FORM_dataN value in host byte order,
15159 zero-extend it as appropriate for the symbol's type. The DWARF
15160 standard (v4) is not entirely clear about the meaning of using
15161 DW_FORM_dataN for a constant with a signed type, where the type is
15162 wider than the data. The conclusion of a discussion on the DWARF
15163 list was that this is unspecified. We choose to always zero-extend
15164 because that is the interpretation long in use by GCC. */
15165
15166 static gdb_byte *
15167 dwarf2_const_value_data (struct attribute *attr, struct type *type,
15168 const char *name, struct obstack *obstack,
15169 struct dwarf2_cu *cu, LONGEST *value, int bits)
15170 {
15171 struct objfile *objfile = cu->objfile;
15172 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15173 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
15174 LONGEST l = DW_UNSND (attr);
15175
15176 if (bits < sizeof (*value) * 8)
15177 {
15178 l &= ((LONGEST) 1 << bits) - 1;
15179 *value = l;
15180 }
15181 else if (bits == sizeof (*value) * 8)
15182 *value = l;
15183 else
15184 {
15185 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15186 store_unsigned_integer (bytes, bits / 8, byte_order, l);
15187 return bytes;
15188 }
15189
15190 return NULL;
15191 }
15192
15193 /* Read a constant value from an attribute. Either set *VALUE, or if
15194 the value does not fit in *VALUE, set *BYTES - either already
15195 allocated on the objfile obstack, or newly allocated on OBSTACK,
15196 or, set *BATON, if we translated the constant to a location
15197 expression. */
15198
15199 static void
15200 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15201 const char *name, struct obstack *obstack,
15202 struct dwarf2_cu *cu,
15203 LONGEST *value, gdb_byte **bytes,
15204 struct dwarf2_locexpr_baton **baton)
15205 {
15206 struct objfile *objfile = cu->objfile;
15207 struct comp_unit_head *cu_header = &cu->header;
15208 struct dwarf_block *blk;
15209 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15211
15212 *value = 0;
15213 *bytes = NULL;
15214 *baton = NULL;
15215
15216 switch (attr->form)
15217 {
15218 case DW_FORM_addr:
15219 case DW_FORM_GNU_addr_index:
15220 {
15221 gdb_byte *data;
15222
15223 if (TYPE_LENGTH (type) != cu_header->addr_size)
15224 dwarf2_const_value_length_mismatch_complaint (name,
15225 cu_header->addr_size,
15226 TYPE_LENGTH (type));
15227 /* Symbols of this form are reasonably rare, so we just
15228 piggyback on the existing location code rather than writing
15229 a new implementation of symbol_computed_ops. */
15230 *baton = obstack_alloc (&objfile->objfile_obstack,
15231 sizeof (struct dwarf2_locexpr_baton));
15232 (*baton)->per_cu = cu->per_cu;
15233 gdb_assert ((*baton)->per_cu);
15234
15235 (*baton)->size = 2 + cu_header->addr_size;
15236 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15237 (*baton)->data = data;
15238
15239 data[0] = DW_OP_addr;
15240 store_unsigned_integer (&data[1], cu_header->addr_size,
15241 byte_order, DW_ADDR (attr));
15242 data[cu_header->addr_size + 1] = DW_OP_stack_value;
15243 }
15244 break;
15245 case DW_FORM_string:
15246 case DW_FORM_strp:
15247 case DW_FORM_GNU_str_index:
15248 case DW_FORM_GNU_strp_alt:
15249 /* DW_STRING is already allocated on the objfile obstack, point
15250 directly to it. */
15251 *bytes = (gdb_byte *) DW_STRING (attr);
15252 break;
15253 case DW_FORM_block1:
15254 case DW_FORM_block2:
15255 case DW_FORM_block4:
15256 case DW_FORM_block:
15257 case DW_FORM_exprloc:
15258 blk = DW_BLOCK (attr);
15259 if (TYPE_LENGTH (type) != blk->size)
15260 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15261 TYPE_LENGTH (type));
15262 *bytes = blk->data;
15263 break;
15264
15265 /* The DW_AT_const_value attributes are supposed to carry the
15266 symbol's value "represented as it would be on the target
15267 architecture." By the time we get here, it's already been
15268 converted to host endianness, so we just need to sign- or
15269 zero-extend it as appropriate. */
15270 case DW_FORM_data1:
15271 *bytes = dwarf2_const_value_data (attr, type, name,
15272 obstack, cu, value, 8);
15273 break;
15274 case DW_FORM_data2:
15275 *bytes = dwarf2_const_value_data (attr, type, name,
15276 obstack, cu, value, 16);
15277 break;
15278 case DW_FORM_data4:
15279 *bytes = dwarf2_const_value_data (attr, type, name,
15280 obstack, cu, value, 32);
15281 break;
15282 case DW_FORM_data8:
15283 *bytes = dwarf2_const_value_data (attr, type, name,
15284 obstack, cu, value, 64);
15285 break;
15286
15287 case DW_FORM_sdata:
15288 *value = DW_SND (attr);
15289 break;
15290
15291 case DW_FORM_udata:
15292 *value = DW_UNSND (attr);
15293 break;
15294
15295 default:
15296 complaint (&symfile_complaints,
15297 _("unsupported const value attribute form: '%s'"),
15298 dwarf_form_name (attr->form));
15299 *value = 0;
15300 break;
15301 }
15302 }
15303
15304
15305 /* Copy constant value from an attribute to a symbol. */
15306
15307 static void
15308 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15309 struct dwarf2_cu *cu)
15310 {
15311 struct objfile *objfile = cu->objfile;
15312 struct comp_unit_head *cu_header = &cu->header;
15313 LONGEST value;
15314 gdb_byte *bytes;
15315 struct dwarf2_locexpr_baton *baton;
15316
15317 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15318 SYMBOL_PRINT_NAME (sym),
15319 &objfile->objfile_obstack, cu,
15320 &value, &bytes, &baton);
15321
15322 if (baton != NULL)
15323 {
15324 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15325 SYMBOL_LOCATION_BATON (sym) = baton;
15326 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15327 }
15328 else if (bytes != NULL)
15329 {
15330 SYMBOL_VALUE_BYTES (sym) = bytes;
15331 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15332 }
15333 else
15334 {
15335 SYMBOL_VALUE (sym) = value;
15336 SYMBOL_CLASS (sym) = LOC_CONST;
15337 }
15338 }
15339
15340 /* Return the type of the die in question using its DW_AT_type attribute. */
15341
15342 static struct type *
15343 die_type (struct die_info *die, struct dwarf2_cu *cu)
15344 {
15345 struct attribute *type_attr;
15346
15347 type_attr = dwarf2_attr (die, DW_AT_type, cu);
15348 if (!type_attr)
15349 {
15350 /* A missing DW_AT_type represents a void type. */
15351 return objfile_type (cu->objfile)->builtin_void;
15352 }
15353
15354 return lookup_die_type (die, type_attr, cu);
15355 }
15356
15357 /* True iff CU's producer generates GNAT Ada auxiliary information
15358 that allows to find parallel types through that information instead
15359 of having to do expensive parallel lookups by type name. */
15360
15361 static int
15362 need_gnat_info (struct dwarf2_cu *cu)
15363 {
15364 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15365 of GNAT produces this auxiliary information, without any indication
15366 that it is produced. Part of enhancing the FSF version of GNAT
15367 to produce that information will be to put in place an indicator
15368 that we can use in order to determine whether the descriptive type
15369 info is available or not. One suggestion that has been made is
15370 to use a new attribute, attached to the CU die. For now, assume
15371 that the descriptive type info is not available. */
15372 return 0;
15373 }
15374
15375 /* Return the auxiliary type of the die in question using its
15376 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15377 attribute is not present. */
15378
15379 static struct type *
15380 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15381 {
15382 struct attribute *type_attr;
15383
15384 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15385 if (!type_attr)
15386 return NULL;
15387
15388 return lookup_die_type (die, type_attr, cu);
15389 }
15390
15391 /* If DIE has a descriptive_type attribute, then set the TYPE's
15392 descriptive type accordingly. */
15393
15394 static void
15395 set_descriptive_type (struct type *type, struct die_info *die,
15396 struct dwarf2_cu *cu)
15397 {
15398 struct type *descriptive_type = die_descriptive_type (die, cu);
15399
15400 if (descriptive_type)
15401 {
15402 ALLOCATE_GNAT_AUX_TYPE (type);
15403 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15404 }
15405 }
15406
15407 /* Return the containing type of the die in question using its
15408 DW_AT_containing_type attribute. */
15409
15410 static struct type *
15411 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15412 {
15413 struct attribute *type_attr;
15414
15415 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15416 if (!type_attr)
15417 error (_("Dwarf Error: Problem turning containing type into gdb type "
15418 "[in module %s]"), cu->objfile->name);
15419
15420 return lookup_die_type (die, type_attr, cu);
15421 }
15422
15423 /* Look up the type of DIE in CU using its type attribute ATTR.
15424 If there is no type substitute an error marker. */
15425
15426 static struct type *
15427 lookup_die_type (struct die_info *die, struct attribute *attr,
15428 struct dwarf2_cu *cu)
15429 {
15430 struct objfile *objfile = cu->objfile;
15431 struct type *this_type;
15432
15433 /* First see if we have it cached. */
15434
15435 if (attr->form == DW_FORM_GNU_ref_alt)
15436 {
15437 struct dwarf2_per_cu_data *per_cu;
15438 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15439
15440 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15441 this_type = get_die_type_at_offset (offset, per_cu);
15442 }
15443 else if (is_ref_attr (attr))
15444 {
15445 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15446
15447 this_type = get_die_type_at_offset (offset, cu->per_cu);
15448 }
15449 else if (attr->form == DW_FORM_ref_sig8)
15450 {
15451 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15452
15453 /* sig_type will be NULL if the signatured type is missing from
15454 the debug info. */
15455 if (sig_type == NULL)
15456 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15457 "at 0x%x [in module %s]"),
15458 die->offset.sect_off, objfile->name);
15459
15460 gdb_assert (sig_type->per_cu.is_debug_types);
15461 /* If we haven't filled in type_offset_in_section yet, then we
15462 haven't read the type in yet. */
15463 this_type = NULL;
15464 if (sig_type->type_offset_in_section.sect_off != 0)
15465 {
15466 this_type =
15467 get_die_type_at_offset (sig_type->type_offset_in_section,
15468 &sig_type->per_cu);
15469 }
15470 }
15471 else
15472 {
15473 dump_die_for_error (die);
15474 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15475 dwarf_attr_name (attr->name), objfile->name);
15476 }
15477
15478 /* If not cached we need to read it in. */
15479
15480 if (this_type == NULL)
15481 {
15482 struct die_info *type_die;
15483 struct dwarf2_cu *type_cu = cu;
15484
15485 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15486 /* If we found the type now, it's probably because the type came
15487 from an inter-CU reference and the type's CU got expanded before
15488 ours. */
15489 this_type = get_die_type (type_die, type_cu);
15490 if (this_type == NULL)
15491 this_type = read_type_die_1 (type_die, type_cu);
15492 }
15493
15494 /* If we still don't have a type use an error marker. */
15495
15496 if (this_type == NULL)
15497 {
15498 char *message, *saved;
15499
15500 /* read_type_die already issued a complaint. */
15501 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15502 objfile->name,
15503 cu->header.offset.sect_off,
15504 die->offset.sect_off);
15505 saved = obstack_copy0 (&objfile->objfile_obstack,
15506 message, strlen (message));
15507 xfree (message);
15508
15509 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15510 }
15511
15512 return this_type;
15513 }
15514
15515 /* Return the type in DIE, CU.
15516 Returns NULL for invalid types.
15517
15518 This first does a lookup in the appropriate type_hash table,
15519 and only reads the die in if necessary.
15520
15521 NOTE: This can be called when reading in partial or full symbols. */
15522
15523 static struct type *
15524 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15525 {
15526 struct type *this_type;
15527
15528 this_type = get_die_type (die, cu);
15529 if (this_type)
15530 return this_type;
15531
15532 return read_type_die_1 (die, cu);
15533 }
15534
15535 /* Read the type in DIE, CU.
15536 Returns NULL for invalid types. */
15537
15538 static struct type *
15539 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15540 {
15541 struct type *this_type = NULL;
15542
15543 switch (die->tag)
15544 {
15545 case DW_TAG_class_type:
15546 case DW_TAG_interface_type:
15547 case DW_TAG_structure_type:
15548 case DW_TAG_union_type:
15549 this_type = read_structure_type (die, cu);
15550 break;
15551 case DW_TAG_enumeration_type:
15552 this_type = read_enumeration_type (die, cu);
15553 break;
15554 case DW_TAG_subprogram:
15555 case DW_TAG_subroutine_type:
15556 case DW_TAG_inlined_subroutine:
15557 this_type = read_subroutine_type (die, cu);
15558 break;
15559 case DW_TAG_array_type:
15560 this_type = read_array_type (die, cu);
15561 break;
15562 case DW_TAG_set_type:
15563 this_type = read_set_type (die, cu);
15564 break;
15565 case DW_TAG_pointer_type:
15566 this_type = read_tag_pointer_type (die, cu);
15567 break;
15568 case DW_TAG_ptr_to_member_type:
15569 this_type = read_tag_ptr_to_member_type (die, cu);
15570 break;
15571 case DW_TAG_reference_type:
15572 this_type = read_tag_reference_type (die, cu);
15573 break;
15574 case DW_TAG_const_type:
15575 this_type = read_tag_const_type (die, cu);
15576 break;
15577 case DW_TAG_volatile_type:
15578 this_type = read_tag_volatile_type (die, cu);
15579 break;
15580 case DW_TAG_string_type:
15581 this_type = read_tag_string_type (die, cu);
15582 break;
15583 case DW_TAG_typedef:
15584 this_type = read_typedef (die, cu);
15585 break;
15586 case DW_TAG_subrange_type:
15587 this_type = read_subrange_type (die, cu);
15588 break;
15589 case DW_TAG_base_type:
15590 this_type = read_base_type (die, cu);
15591 break;
15592 case DW_TAG_unspecified_type:
15593 this_type = read_unspecified_type (die, cu);
15594 break;
15595 case DW_TAG_namespace:
15596 this_type = read_namespace_type (die, cu);
15597 break;
15598 case DW_TAG_module:
15599 this_type = read_module_type (die, cu);
15600 break;
15601 default:
15602 complaint (&symfile_complaints,
15603 _("unexpected tag in read_type_die: '%s'"),
15604 dwarf_tag_name (die->tag));
15605 break;
15606 }
15607
15608 return this_type;
15609 }
15610
15611 /* See if we can figure out if the class lives in a namespace. We do
15612 this by looking for a member function; its demangled name will
15613 contain namespace info, if there is any.
15614 Return the computed name or NULL.
15615 Space for the result is allocated on the objfile's obstack.
15616 This is the full-die version of guess_partial_die_structure_name.
15617 In this case we know DIE has no useful parent. */
15618
15619 static char *
15620 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15621 {
15622 struct die_info *spec_die;
15623 struct dwarf2_cu *spec_cu;
15624 struct die_info *child;
15625
15626 spec_cu = cu;
15627 spec_die = die_specification (die, &spec_cu);
15628 if (spec_die != NULL)
15629 {
15630 die = spec_die;
15631 cu = spec_cu;
15632 }
15633
15634 for (child = die->child;
15635 child != NULL;
15636 child = child->sibling)
15637 {
15638 if (child->tag == DW_TAG_subprogram)
15639 {
15640 struct attribute *attr;
15641
15642 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15643 if (attr == NULL)
15644 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15645 if (attr != NULL)
15646 {
15647 char *actual_name
15648 = language_class_name_from_physname (cu->language_defn,
15649 DW_STRING (attr));
15650 char *name = NULL;
15651
15652 if (actual_name != NULL)
15653 {
15654 char *die_name = dwarf2_name (die, cu);
15655
15656 if (die_name != NULL
15657 && strcmp (die_name, actual_name) != 0)
15658 {
15659 /* Strip off the class name from the full name.
15660 We want the prefix. */
15661 int die_name_len = strlen (die_name);
15662 int actual_name_len = strlen (actual_name);
15663
15664 /* Test for '::' as a sanity check. */
15665 if (actual_name_len > die_name_len + 2
15666 && actual_name[actual_name_len
15667 - die_name_len - 1] == ':')
15668 name =
15669 obsavestring (actual_name,
15670 actual_name_len - die_name_len - 2,
15671 &cu->objfile->objfile_obstack);
15672 }
15673 }
15674 xfree (actual_name);
15675 return name;
15676 }
15677 }
15678 }
15679
15680 return NULL;
15681 }
15682
15683 /* GCC might emit a nameless typedef that has a linkage name. Determine the
15684 prefix part in such case. See
15685 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15686
15687 static char *
15688 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15689 {
15690 struct attribute *attr;
15691 char *base;
15692
15693 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15694 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15695 return NULL;
15696
15697 attr = dwarf2_attr (die, DW_AT_name, cu);
15698 if (attr != NULL && DW_STRING (attr) != NULL)
15699 return NULL;
15700
15701 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15702 if (attr == NULL)
15703 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15704 if (attr == NULL || DW_STRING (attr) == NULL)
15705 return NULL;
15706
15707 /* dwarf2_name had to be already called. */
15708 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15709
15710 /* Strip the base name, keep any leading namespaces/classes. */
15711 base = strrchr (DW_STRING (attr), ':');
15712 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15713 return "";
15714
15715 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15716 &cu->objfile->objfile_obstack);
15717 }
15718
15719 /* Return the name of the namespace/class that DIE is defined within,
15720 or "" if we can't tell. The caller should not xfree the result.
15721
15722 For example, if we're within the method foo() in the following
15723 code:
15724
15725 namespace N {
15726 class C {
15727 void foo () {
15728 }
15729 };
15730 }
15731
15732 then determine_prefix on foo's die will return "N::C". */
15733
15734 static const char *
15735 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15736 {
15737 struct die_info *parent, *spec_die;
15738 struct dwarf2_cu *spec_cu;
15739 struct type *parent_type;
15740 char *retval;
15741
15742 if (cu->language != language_cplus && cu->language != language_java
15743 && cu->language != language_fortran)
15744 return "";
15745
15746 retval = anonymous_struct_prefix (die, cu);
15747 if (retval)
15748 return retval;
15749
15750 /* We have to be careful in the presence of DW_AT_specification.
15751 For example, with GCC 3.4, given the code
15752
15753 namespace N {
15754 void foo() {
15755 // Definition of N::foo.
15756 }
15757 }
15758
15759 then we'll have a tree of DIEs like this:
15760
15761 1: DW_TAG_compile_unit
15762 2: DW_TAG_namespace // N
15763 3: DW_TAG_subprogram // declaration of N::foo
15764 4: DW_TAG_subprogram // definition of N::foo
15765 DW_AT_specification // refers to die #3
15766
15767 Thus, when processing die #4, we have to pretend that we're in
15768 the context of its DW_AT_specification, namely the contex of die
15769 #3. */
15770 spec_cu = cu;
15771 spec_die = die_specification (die, &spec_cu);
15772 if (spec_die == NULL)
15773 parent = die->parent;
15774 else
15775 {
15776 parent = spec_die->parent;
15777 cu = spec_cu;
15778 }
15779
15780 if (parent == NULL)
15781 return "";
15782 else if (parent->building_fullname)
15783 {
15784 const char *name;
15785 const char *parent_name;
15786
15787 /* It has been seen on RealView 2.2 built binaries,
15788 DW_TAG_template_type_param types actually _defined_ as
15789 children of the parent class:
15790
15791 enum E {};
15792 template class <class Enum> Class{};
15793 Class<enum E> class_e;
15794
15795 1: DW_TAG_class_type (Class)
15796 2: DW_TAG_enumeration_type (E)
15797 3: DW_TAG_enumerator (enum1:0)
15798 3: DW_TAG_enumerator (enum2:1)
15799 ...
15800 2: DW_TAG_template_type_param
15801 DW_AT_type DW_FORM_ref_udata (E)
15802
15803 Besides being broken debug info, it can put GDB into an
15804 infinite loop. Consider:
15805
15806 When we're building the full name for Class<E>, we'll start
15807 at Class, and go look over its template type parameters,
15808 finding E. We'll then try to build the full name of E, and
15809 reach here. We're now trying to build the full name of E,
15810 and look over the parent DIE for containing scope. In the
15811 broken case, if we followed the parent DIE of E, we'd again
15812 find Class, and once again go look at its template type
15813 arguments, etc., etc. Simply don't consider such parent die
15814 as source-level parent of this die (it can't be, the language
15815 doesn't allow it), and break the loop here. */
15816 name = dwarf2_name (die, cu);
15817 parent_name = dwarf2_name (parent, cu);
15818 complaint (&symfile_complaints,
15819 _("template param type '%s' defined within parent '%s'"),
15820 name ? name : "<unknown>",
15821 parent_name ? parent_name : "<unknown>");
15822 return "";
15823 }
15824 else
15825 switch (parent->tag)
15826 {
15827 case DW_TAG_namespace:
15828 parent_type = read_type_die (parent, cu);
15829 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15830 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15831 Work around this problem here. */
15832 if (cu->language == language_cplus
15833 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15834 return "";
15835 /* We give a name to even anonymous namespaces. */
15836 return TYPE_TAG_NAME (parent_type);
15837 case DW_TAG_class_type:
15838 case DW_TAG_interface_type:
15839 case DW_TAG_structure_type:
15840 case DW_TAG_union_type:
15841 case DW_TAG_module:
15842 parent_type = read_type_die (parent, cu);
15843 if (TYPE_TAG_NAME (parent_type) != NULL)
15844 return TYPE_TAG_NAME (parent_type);
15845 else
15846 /* An anonymous structure is only allowed non-static data
15847 members; no typedefs, no member functions, et cetera.
15848 So it does not need a prefix. */
15849 return "";
15850 case DW_TAG_compile_unit:
15851 case DW_TAG_partial_unit:
15852 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15853 if (cu->language == language_cplus
15854 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15855 && die->child != NULL
15856 && (die->tag == DW_TAG_class_type
15857 || die->tag == DW_TAG_structure_type
15858 || die->tag == DW_TAG_union_type))
15859 {
15860 char *name = guess_full_die_structure_name (die, cu);
15861 if (name != NULL)
15862 return name;
15863 }
15864 return "";
15865 default:
15866 return determine_prefix (parent, cu);
15867 }
15868 }
15869
15870 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15871 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15872 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15873 an obconcat, otherwise allocate storage for the result. The CU argument is
15874 used to determine the language and hence, the appropriate separator. */
15875
15876 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
15877
15878 static char *
15879 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15880 int physname, struct dwarf2_cu *cu)
15881 {
15882 const char *lead = "";
15883 const char *sep;
15884
15885 if (suffix == NULL || suffix[0] == '\0'
15886 || prefix == NULL || prefix[0] == '\0')
15887 sep = "";
15888 else if (cu->language == language_java)
15889 sep = ".";
15890 else if (cu->language == language_fortran && physname)
15891 {
15892 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15893 DW_AT_MIPS_linkage_name is preferred and used instead. */
15894
15895 lead = "__";
15896 sep = "_MOD_";
15897 }
15898 else
15899 sep = "::";
15900
15901 if (prefix == NULL)
15902 prefix = "";
15903 if (suffix == NULL)
15904 suffix = "";
15905
15906 if (obs == NULL)
15907 {
15908 char *retval
15909 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15910
15911 strcpy (retval, lead);
15912 strcat (retval, prefix);
15913 strcat (retval, sep);
15914 strcat (retval, suffix);
15915 return retval;
15916 }
15917 else
15918 {
15919 /* We have an obstack. */
15920 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15921 }
15922 }
15923
15924 /* Return sibling of die, NULL if no sibling. */
15925
15926 static struct die_info *
15927 sibling_die (struct die_info *die)
15928 {
15929 return die->sibling;
15930 }
15931
15932 /* Get name of a die, return NULL if not found. */
15933
15934 static char *
15935 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15936 struct obstack *obstack)
15937 {
15938 if (name && cu->language == language_cplus)
15939 {
15940 char *canon_name = cp_canonicalize_string (name);
15941
15942 if (canon_name != NULL)
15943 {
15944 if (strcmp (canon_name, name) != 0)
15945 name = obsavestring (canon_name, strlen (canon_name),
15946 obstack);
15947 xfree (canon_name);
15948 }
15949 }
15950
15951 return name;
15952 }
15953
15954 /* Get name of a die, return NULL if not found. */
15955
15956 static char *
15957 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15958 {
15959 struct attribute *attr;
15960
15961 attr = dwarf2_attr (die, DW_AT_name, cu);
15962 if ((!attr || !DW_STRING (attr))
15963 && die->tag != DW_TAG_class_type
15964 && die->tag != DW_TAG_interface_type
15965 && die->tag != DW_TAG_structure_type
15966 && die->tag != DW_TAG_union_type)
15967 return NULL;
15968
15969 switch (die->tag)
15970 {
15971 case DW_TAG_compile_unit:
15972 case DW_TAG_partial_unit:
15973 /* Compilation units have a DW_AT_name that is a filename, not
15974 a source language identifier. */
15975 case DW_TAG_enumeration_type:
15976 case DW_TAG_enumerator:
15977 /* These tags always have simple identifiers already; no need
15978 to canonicalize them. */
15979 return DW_STRING (attr);
15980
15981 case DW_TAG_subprogram:
15982 /* Java constructors will all be named "<init>", so return
15983 the class name when we see this special case. */
15984 if (cu->language == language_java
15985 && DW_STRING (attr) != NULL
15986 && strcmp (DW_STRING (attr), "<init>") == 0)
15987 {
15988 struct dwarf2_cu *spec_cu = cu;
15989 struct die_info *spec_die;
15990
15991 /* GCJ will output '<init>' for Java constructor names.
15992 For this special case, return the name of the parent class. */
15993
15994 /* GCJ may output suprogram DIEs with AT_specification set.
15995 If so, use the name of the specified DIE. */
15996 spec_die = die_specification (die, &spec_cu);
15997 if (spec_die != NULL)
15998 return dwarf2_name (spec_die, spec_cu);
15999
16000 do
16001 {
16002 die = die->parent;
16003 if (die->tag == DW_TAG_class_type)
16004 return dwarf2_name (die, cu);
16005 }
16006 while (die->tag != DW_TAG_compile_unit
16007 && die->tag != DW_TAG_partial_unit);
16008 }
16009 break;
16010
16011 case DW_TAG_class_type:
16012 case DW_TAG_interface_type:
16013 case DW_TAG_structure_type:
16014 case DW_TAG_union_type:
16015 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16016 structures or unions. These were of the form "._%d" in GCC 4.1,
16017 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16018 and GCC 4.4. We work around this problem by ignoring these. */
16019 if (attr && DW_STRING (attr)
16020 && (strncmp (DW_STRING (attr), "._", 2) == 0
16021 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16022 return NULL;
16023
16024 /* GCC might emit a nameless typedef that has a linkage name. See
16025 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16026 if (!attr || DW_STRING (attr) == NULL)
16027 {
16028 char *demangled = NULL;
16029
16030 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16031 if (attr == NULL)
16032 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16033
16034 if (attr == NULL || DW_STRING (attr) == NULL)
16035 return NULL;
16036
16037 /* Avoid demangling DW_STRING (attr) the second time on a second
16038 call for the same DIE. */
16039 if (!DW_STRING_IS_CANONICAL (attr))
16040 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16041
16042 if (demangled)
16043 {
16044 char *base;
16045
16046 /* FIXME: we already did this for the partial symbol... */
16047 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16048 &cu->objfile->objfile_obstack);
16049 DW_STRING_IS_CANONICAL (attr) = 1;
16050 xfree (demangled);
16051
16052 /* Strip any leading namespaces/classes, keep only the base name.
16053 DW_AT_name for named DIEs does not contain the prefixes. */
16054 base = strrchr (DW_STRING (attr), ':');
16055 if (base && base > DW_STRING (attr) && base[-1] == ':')
16056 return &base[1];
16057 else
16058 return DW_STRING (attr);
16059 }
16060 }
16061 break;
16062
16063 default:
16064 break;
16065 }
16066
16067 if (!DW_STRING_IS_CANONICAL (attr))
16068 {
16069 DW_STRING (attr)
16070 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16071 &cu->objfile->objfile_obstack);
16072 DW_STRING_IS_CANONICAL (attr) = 1;
16073 }
16074 return DW_STRING (attr);
16075 }
16076
16077 /* Return the die that this die in an extension of, or NULL if there
16078 is none. *EXT_CU is the CU containing DIE on input, and the CU
16079 containing the return value on output. */
16080
16081 static struct die_info *
16082 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16083 {
16084 struct attribute *attr;
16085
16086 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16087 if (attr == NULL)
16088 return NULL;
16089
16090 return follow_die_ref (die, attr, ext_cu);
16091 }
16092
16093 /* Convert a DIE tag into its string name. */
16094
16095 static const char *
16096 dwarf_tag_name (unsigned tag)
16097 {
16098 const char *name = get_DW_TAG_name (tag);
16099
16100 if (name == NULL)
16101 return "DW_TAG_<unknown>";
16102
16103 return name;
16104 }
16105
16106 /* Convert a DWARF attribute code into its string name. */
16107
16108 static const char *
16109 dwarf_attr_name (unsigned attr)
16110 {
16111 const char *name;
16112
16113 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16114 if (attr == DW_AT_MIPS_fde)
16115 return "DW_AT_MIPS_fde";
16116 #else
16117 if (attr == DW_AT_HP_block_index)
16118 return "DW_AT_HP_block_index";
16119 #endif
16120
16121 name = get_DW_AT_name (attr);
16122
16123 if (name == NULL)
16124 return "DW_AT_<unknown>";
16125
16126 return name;
16127 }
16128
16129 /* Convert a DWARF value form code into its string name. */
16130
16131 static const char *
16132 dwarf_form_name (unsigned form)
16133 {
16134 const char *name = get_DW_FORM_name (form);
16135
16136 if (name == NULL)
16137 return "DW_FORM_<unknown>";
16138
16139 return name;
16140 }
16141
16142 static char *
16143 dwarf_bool_name (unsigned mybool)
16144 {
16145 if (mybool)
16146 return "TRUE";
16147 else
16148 return "FALSE";
16149 }
16150
16151 /* Convert a DWARF type code into its string name. */
16152
16153 static const char *
16154 dwarf_type_encoding_name (unsigned enc)
16155 {
16156 const char *name = get_DW_ATE_name (enc);
16157
16158 if (name == NULL)
16159 return "DW_ATE_<unknown>";
16160
16161 return name;
16162 }
16163
16164 static void
16165 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
16166 {
16167 unsigned int i;
16168
16169 print_spaces (indent, f);
16170 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
16171 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
16172
16173 if (die->parent != NULL)
16174 {
16175 print_spaces (indent, f);
16176 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
16177 die->parent->offset.sect_off);
16178 }
16179
16180 print_spaces (indent, f);
16181 fprintf_unfiltered (f, " has children: %s\n",
16182 dwarf_bool_name (die->child != NULL));
16183
16184 print_spaces (indent, f);
16185 fprintf_unfiltered (f, " attributes:\n");
16186
16187 for (i = 0; i < die->num_attrs; ++i)
16188 {
16189 print_spaces (indent, f);
16190 fprintf_unfiltered (f, " %s (%s) ",
16191 dwarf_attr_name (die->attrs[i].name),
16192 dwarf_form_name (die->attrs[i].form));
16193
16194 switch (die->attrs[i].form)
16195 {
16196 case DW_FORM_addr:
16197 case DW_FORM_GNU_addr_index:
16198 fprintf_unfiltered (f, "address: ");
16199 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
16200 break;
16201 case DW_FORM_block2:
16202 case DW_FORM_block4:
16203 case DW_FORM_block:
16204 case DW_FORM_block1:
16205 fprintf_unfiltered (f, "block: size %s",
16206 pulongest (DW_BLOCK (&die->attrs[i])->size));
16207 break;
16208 case DW_FORM_exprloc:
16209 fprintf_unfiltered (f, "expression: size %s",
16210 pulongest (DW_BLOCK (&die->attrs[i])->size));
16211 break;
16212 case DW_FORM_ref_addr:
16213 fprintf_unfiltered (f, "ref address: ");
16214 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16215 break;
16216 case DW_FORM_GNU_ref_alt:
16217 fprintf_unfiltered (f, "alt ref address: ");
16218 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16219 break;
16220 case DW_FORM_ref1:
16221 case DW_FORM_ref2:
16222 case DW_FORM_ref4:
16223 case DW_FORM_ref8:
16224 case DW_FORM_ref_udata:
16225 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
16226 (long) (DW_UNSND (&die->attrs[i])));
16227 break;
16228 case DW_FORM_data1:
16229 case DW_FORM_data2:
16230 case DW_FORM_data4:
16231 case DW_FORM_data8:
16232 case DW_FORM_udata:
16233 case DW_FORM_sdata:
16234 fprintf_unfiltered (f, "constant: %s",
16235 pulongest (DW_UNSND (&die->attrs[i])));
16236 break;
16237 case DW_FORM_sec_offset:
16238 fprintf_unfiltered (f, "section offset: %s",
16239 pulongest (DW_UNSND (&die->attrs[i])));
16240 break;
16241 case DW_FORM_ref_sig8:
16242 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16243 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
16244 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
16245 else
16246 fprintf_unfiltered (f, "signatured type, offset: unknown");
16247 break;
16248 case DW_FORM_string:
16249 case DW_FORM_strp:
16250 case DW_FORM_GNU_str_index:
16251 case DW_FORM_GNU_strp_alt:
16252 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
16253 DW_STRING (&die->attrs[i])
16254 ? DW_STRING (&die->attrs[i]) : "",
16255 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
16256 break;
16257 case DW_FORM_flag:
16258 if (DW_UNSND (&die->attrs[i]))
16259 fprintf_unfiltered (f, "flag: TRUE");
16260 else
16261 fprintf_unfiltered (f, "flag: FALSE");
16262 break;
16263 case DW_FORM_flag_present:
16264 fprintf_unfiltered (f, "flag: TRUE");
16265 break;
16266 case DW_FORM_indirect:
16267 /* The reader will have reduced the indirect form to
16268 the "base form" so this form should not occur. */
16269 fprintf_unfiltered (f,
16270 "unexpected attribute form: DW_FORM_indirect");
16271 break;
16272 default:
16273 fprintf_unfiltered (f, "unsupported attribute form: %d.",
16274 die->attrs[i].form);
16275 break;
16276 }
16277 fprintf_unfiltered (f, "\n");
16278 }
16279 }
16280
16281 static void
16282 dump_die_for_error (struct die_info *die)
16283 {
16284 dump_die_shallow (gdb_stderr, 0, die);
16285 }
16286
16287 static void
16288 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16289 {
16290 int indent = level * 4;
16291
16292 gdb_assert (die != NULL);
16293
16294 if (level >= max_level)
16295 return;
16296
16297 dump_die_shallow (f, indent, die);
16298
16299 if (die->child != NULL)
16300 {
16301 print_spaces (indent, f);
16302 fprintf_unfiltered (f, " Children:");
16303 if (level + 1 < max_level)
16304 {
16305 fprintf_unfiltered (f, "\n");
16306 dump_die_1 (f, level + 1, max_level, die->child);
16307 }
16308 else
16309 {
16310 fprintf_unfiltered (f,
16311 " [not printed, max nesting level reached]\n");
16312 }
16313 }
16314
16315 if (die->sibling != NULL && level > 0)
16316 {
16317 dump_die_1 (f, level, max_level, die->sibling);
16318 }
16319 }
16320
16321 /* This is called from the pdie macro in gdbinit.in.
16322 It's not static so gcc will keep a copy callable from gdb. */
16323
16324 void
16325 dump_die (struct die_info *die, int max_level)
16326 {
16327 dump_die_1 (gdb_stdlog, 0, max_level, die);
16328 }
16329
16330 static void
16331 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16332 {
16333 void **slot;
16334
16335 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16336 INSERT);
16337
16338 *slot = die;
16339 }
16340
16341 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16342 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16343
16344 static int
16345 is_ref_attr (struct attribute *attr)
16346 {
16347 switch (attr->form)
16348 {
16349 case DW_FORM_ref_addr:
16350 case DW_FORM_ref1:
16351 case DW_FORM_ref2:
16352 case DW_FORM_ref4:
16353 case DW_FORM_ref8:
16354 case DW_FORM_ref_udata:
16355 case DW_FORM_GNU_ref_alt:
16356 return 1;
16357 default:
16358 return 0;
16359 }
16360 }
16361
16362 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16363 required kind. */
16364
16365 static sect_offset
16366 dwarf2_get_ref_die_offset (struct attribute *attr)
16367 {
16368 sect_offset retval = { DW_UNSND (attr) };
16369
16370 if (is_ref_attr (attr))
16371 return retval;
16372
16373 retval.sect_off = 0;
16374 complaint (&symfile_complaints,
16375 _("unsupported die ref attribute form: '%s'"),
16376 dwarf_form_name (attr->form));
16377 return retval;
16378 }
16379
16380 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16381 * the value held by the attribute is not constant. */
16382
16383 static LONGEST
16384 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16385 {
16386 if (attr->form == DW_FORM_sdata)
16387 return DW_SND (attr);
16388 else if (attr->form == DW_FORM_udata
16389 || attr->form == DW_FORM_data1
16390 || attr->form == DW_FORM_data2
16391 || attr->form == DW_FORM_data4
16392 || attr->form == DW_FORM_data8)
16393 return DW_UNSND (attr);
16394 else
16395 {
16396 complaint (&symfile_complaints,
16397 _("Attribute value is not a constant (%s)"),
16398 dwarf_form_name (attr->form));
16399 return default_value;
16400 }
16401 }
16402
16403 /* Follow reference or signature attribute ATTR of SRC_DIE.
16404 On entry *REF_CU is the CU of SRC_DIE.
16405 On exit *REF_CU is the CU of the result. */
16406
16407 static struct die_info *
16408 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16409 struct dwarf2_cu **ref_cu)
16410 {
16411 struct die_info *die;
16412
16413 if (is_ref_attr (attr))
16414 die = follow_die_ref (src_die, attr, ref_cu);
16415 else if (attr->form == DW_FORM_ref_sig8)
16416 die = follow_die_sig (src_die, attr, ref_cu);
16417 else
16418 {
16419 dump_die_for_error (src_die);
16420 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16421 (*ref_cu)->objfile->name);
16422 }
16423
16424 return die;
16425 }
16426
16427 /* Follow reference OFFSET.
16428 On entry *REF_CU is the CU of the source die referencing OFFSET.
16429 On exit *REF_CU is the CU of the result.
16430 Returns NULL if OFFSET is invalid. */
16431
16432 static struct die_info *
16433 follow_die_offset (sect_offset offset, int offset_in_dwz,
16434 struct dwarf2_cu **ref_cu)
16435 {
16436 struct die_info temp_die;
16437 struct dwarf2_cu *target_cu, *cu = *ref_cu;
16438
16439 gdb_assert (cu->per_cu != NULL);
16440
16441 target_cu = cu;
16442
16443 if (cu->per_cu->is_debug_types)
16444 {
16445 /* .debug_types CUs cannot reference anything outside their CU.
16446 If they need to, they have to reference a signatured type via
16447 DW_FORM_ref_sig8. */
16448 if (! offset_in_cu_p (&cu->header, offset))
16449 return NULL;
16450 }
16451 else if (offset_in_dwz != cu->per_cu->is_dwz
16452 || ! offset_in_cu_p (&cu->header, offset))
16453 {
16454 struct dwarf2_per_cu_data *per_cu;
16455
16456 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16457 cu->objfile);
16458
16459 /* If necessary, add it to the queue and load its DIEs. */
16460 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16461 load_full_comp_unit (per_cu, cu->language);
16462
16463 target_cu = per_cu->cu;
16464 }
16465 else if (cu->dies == NULL)
16466 {
16467 /* We're loading full DIEs during partial symbol reading. */
16468 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16469 load_full_comp_unit (cu->per_cu, language_minimal);
16470 }
16471
16472 *ref_cu = target_cu;
16473 temp_die.offset = offset;
16474 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16475 }
16476
16477 /* Follow reference attribute ATTR of SRC_DIE.
16478 On entry *REF_CU is the CU of SRC_DIE.
16479 On exit *REF_CU is the CU of the result. */
16480
16481 static struct die_info *
16482 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16483 struct dwarf2_cu **ref_cu)
16484 {
16485 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16486 struct dwarf2_cu *cu = *ref_cu;
16487 struct die_info *die;
16488
16489 die = follow_die_offset (offset,
16490 (attr->form == DW_FORM_GNU_ref_alt
16491 || cu->per_cu->is_dwz),
16492 ref_cu);
16493 if (!die)
16494 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16495 "at 0x%x [in module %s]"),
16496 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16497
16498 return die;
16499 }
16500
16501 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16502 Returned value is intended for DW_OP_call*. Returned
16503 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
16504
16505 struct dwarf2_locexpr_baton
16506 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16507 struct dwarf2_per_cu_data *per_cu,
16508 CORE_ADDR (*get_frame_pc) (void *baton),
16509 void *baton)
16510 {
16511 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16512 struct dwarf2_cu *cu;
16513 struct die_info *die;
16514 struct attribute *attr;
16515 struct dwarf2_locexpr_baton retval;
16516
16517 dw2_setup (per_cu->objfile);
16518
16519 if (per_cu->cu == NULL)
16520 load_cu (per_cu);
16521 cu = per_cu->cu;
16522
16523 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
16524 if (!die)
16525 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16526 offset.sect_off, per_cu->objfile->name);
16527
16528 attr = dwarf2_attr (die, DW_AT_location, cu);
16529 if (!attr)
16530 {
16531 /* DWARF: "If there is no such attribute, then there is no effect.".
16532 DATA is ignored if SIZE is 0. */
16533
16534 retval.data = NULL;
16535 retval.size = 0;
16536 }
16537 else if (attr_form_is_section_offset (attr))
16538 {
16539 struct dwarf2_loclist_baton loclist_baton;
16540 CORE_ADDR pc = (*get_frame_pc) (baton);
16541 size_t size;
16542
16543 fill_in_loclist_baton (cu, &loclist_baton, attr);
16544
16545 retval.data = dwarf2_find_location_expression (&loclist_baton,
16546 &size, pc);
16547 retval.size = size;
16548 }
16549 else
16550 {
16551 if (!attr_form_is_block (attr))
16552 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16553 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16554 offset.sect_off, per_cu->objfile->name);
16555
16556 retval.data = DW_BLOCK (attr)->data;
16557 retval.size = DW_BLOCK (attr)->size;
16558 }
16559 retval.per_cu = cu->per_cu;
16560
16561 age_cached_comp_units ();
16562
16563 return retval;
16564 }
16565
16566 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16567 PER_CU. */
16568
16569 struct type *
16570 dwarf2_get_die_type (cu_offset die_offset,
16571 struct dwarf2_per_cu_data *per_cu)
16572 {
16573 sect_offset die_offset_sect;
16574
16575 dw2_setup (per_cu->objfile);
16576
16577 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16578 return get_die_type_at_offset (die_offset_sect, per_cu);
16579 }
16580
16581 /* Follow the signature attribute ATTR in SRC_DIE.
16582 On entry *REF_CU is the CU of SRC_DIE.
16583 On exit *REF_CU is the CU of the result. */
16584
16585 static struct die_info *
16586 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16587 struct dwarf2_cu **ref_cu)
16588 {
16589 struct objfile *objfile = (*ref_cu)->objfile;
16590 struct die_info temp_die;
16591 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16592 struct dwarf2_cu *sig_cu;
16593 struct die_info *die;
16594
16595 /* sig_type will be NULL if the signatured type is missing from
16596 the debug info. */
16597 if (sig_type == NULL)
16598 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16599 "at 0x%x [in module %s]"),
16600 src_die->offset.sect_off, objfile->name);
16601
16602 /* If necessary, add it to the queue and load its DIEs. */
16603
16604 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16605 read_signatured_type (sig_type);
16606
16607 gdb_assert (sig_type->per_cu.cu != NULL);
16608
16609 sig_cu = sig_type->per_cu.cu;
16610 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16611 temp_die.offset = sig_type->type_offset_in_section;
16612 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16613 temp_die.offset.sect_off);
16614 if (die)
16615 {
16616 *ref_cu = sig_cu;
16617 return die;
16618 }
16619
16620 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16621 "from DIE at 0x%x [in module %s]"),
16622 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16623 }
16624
16625 /* Given an offset of a signatured type, return its signatured_type. */
16626
16627 static struct signatured_type *
16628 lookup_signatured_type_at_offset (struct objfile *objfile,
16629 struct dwarf2_section_info *section,
16630 sect_offset offset)
16631 {
16632 gdb_byte *info_ptr = section->buffer + offset.sect_off;
16633 unsigned int length, initial_length_size;
16634 unsigned int sig_offset;
16635 struct signatured_type find_entry, *sig_type;
16636
16637 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16638 sig_offset = (initial_length_size
16639 + 2 /*version*/
16640 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16641 + 1 /*address_size*/);
16642 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16643 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16644
16645 /* This is only used to lookup previously recorded types.
16646 If we didn't find it, it's our bug. */
16647 gdb_assert (sig_type != NULL);
16648 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16649
16650 return sig_type;
16651 }
16652
16653 /* Load the DIEs associated with type unit PER_CU into memory. */
16654
16655 static void
16656 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16657 {
16658 struct signatured_type *sig_type;
16659
16660 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16661 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16662
16663 /* We have the per_cu, but we need the signatured_type.
16664 Fortunately this is an easy translation. */
16665 gdb_assert (per_cu->is_debug_types);
16666 sig_type = (struct signatured_type *) per_cu;
16667
16668 gdb_assert (per_cu->cu == NULL);
16669
16670 read_signatured_type (sig_type);
16671
16672 gdb_assert (per_cu->cu != NULL);
16673 }
16674
16675 /* die_reader_func for read_signatured_type.
16676 This is identical to load_full_comp_unit_reader,
16677 but is kept separate for now. */
16678
16679 static void
16680 read_signatured_type_reader (const struct die_reader_specs *reader,
16681 gdb_byte *info_ptr,
16682 struct die_info *comp_unit_die,
16683 int has_children,
16684 void *data)
16685 {
16686 struct dwarf2_cu *cu = reader->cu;
16687
16688 gdb_assert (cu->die_hash == NULL);
16689 cu->die_hash =
16690 htab_create_alloc_ex (cu->header.length / 12,
16691 die_hash,
16692 die_eq,
16693 NULL,
16694 &cu->comp_unit_obstack,
16695 hashtab_obstack_allocate,
16696 dummy_obstack_deallocate);
16697
16698 if (has_children)
16699 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16700 &info_ptr, comp_unit_die);
16701 cu->dies = comp_unit_die;
16702 /* comp_unit_die is not stored in die_hash, no need. */
16703
16704 /* We try not to read any attributes in this function, because not
16705 all CUs needed for references have been loaded yet, and symbol
16706 table processing isn't initialized. But we have to set the CU language,
16707 or we won't be able to build types correctly.
16708 Similarly, if we do not read the producer, we can not apply
16709 producer-specific interpretation. */
16710 prepare_one_comp_unit (cu, cu->dies, language_minimal);
16711 }
16712
16713 /* Read in a signatured type and build its CU and DIEs.
16714 If the type is a stub for the real type in a DWO file,
16715 read in the real type from the DWO file as well. */
16716
16717 static void
16718 read_signatured_type (struct signatured_type *sig_type)
16719 {
16720 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16721
16722 gdb_assert (per_cu->is_debug_types);
16723 gdb_assert (per_cu->cu == NULL);
16724
16725 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16726 read_signatured_type_reader, NULL);
16727 }
16728
16729 /* Decode simple location descriptions.
16730 Given a pointer to a dwarf block that defines a location, compute
16731 the location and return the value.
16732
16733 NOTE drow/2003-11-18: This function is called in two situations
16734 now: for the address of static or global variables (partial symbols
16735 only) and for offsets into structures which are expected to be
16736 (more or less) constant. The partial symbol case should go away,
16737 and only the constant case should remain. That will let this
16738 function complain more accurately. A few special modes are allowed
16739 without complaint for global variables (for instance, global
16740 register values and thread-local values).
16741
16742 A location description containing no operations indicates that the
16743 object is optimized out. The return value is 0 for that case.
16744 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16745 callers will only want a very basic result and this can become a
16746 complaint.
16747
16748 Note that stack[0] is unused except as a default error return. */
16749
16750 static CORE_ADDR
16751 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16752 {
16753 struct objfile *objfile = cu->objfile;
16754 size_t i;
16755 size_t size = blk->size;
16756 gdb_byte *data = blk->data;
16757 CORE_ADDR stack[64];
16758 int stacki;
16759 unsigned int bytes_read, unsnd;
16760 gdb_byte op;
16761
16762 i = 0;
16763 stacki = 0;
16764 stack[stacki] = 0;
16765 stack[++stacki] = 0;
16766
16767 while (i < size)
16768 {
16769 op = data[i++];
16770 switch (op)
16771 {
16772 case DW_OP_lit0:
16773 case DW_OP_lit1:
16774 case DW_OP_lit2:
16775 case DW_OP_lit3:
16776 case DW_OP_lit4:
16777 case DW_OP_lit5:
16778 case DW_OP_lit6:
16779 case DW_OP_lit7:
16780 case DW_OP_lit8:
16781 case DW_OP_lit9:
16782 case DW_OP_lit10:
16783 case DW_OP_lit11:
16784 case DW_OP_lit12:
16785 case DW_OP_lit13:
16786 case DW_OP_lit14:
16787 case DW_OP_lit15:
16788 case DW_OP_lit16:
16789 case DW_OP_lit17:
16790 case DW_OP_lit18:
16791 case DW_OP_lit19:
16792 case DW_OP_lit20:
16793 case DW_OP_lit21:
16794 case DW_OP_lit22:
16795 case DW_OP_lit23:
16796 case DW_OP_lit24:
16797 case DW_OP_lit25:
16798 case DW_OP_lit26:
16799 case DW_OP_lit27:
16800 case DW_OP_lit28:
16801 case DW_OP_lit29:
16802 case DW_OP_lit30:
16803 case DW_OP_lit31:
16804 stack[++stacki] = op - DW_OP_lit0;
16805 break;
16806
16807 case DW_OP_reg0:
16808 case DW_OP_reg1:
16809 case DW_OP_reg2:
16810 case DW_OP_reg3:
16811 case DW_OP_reg4:
16812 case DW_OP_reg5:
16813 case DW_OP_reg6:
16814 case DW_OP_reg7:
16815 case DW_OP_reg8:
16816 case DW_OP_reg9:
16817 case DW_OP_reg10:
16818 case DW_OP_reg11:
16819 case DW_OP_reg12:
16820 case DW_OP_reg13:
16821 case DW_OP_reg14:
16822 case DW_OP_reg15:
16823 case DW_OP_reg16:
16824 case DW_OP_reg17:
16825 case DW_OP_reg18:
16826 case DW_OP_reg19:
16827 case DW_OP_reg20:
16828 case DW_OP_reg21:
16829 case DW_OP_reg22:
16830 case DW_OP_reg23:
16831 case DW_OP_reg24:
16832 case DW_OP_reg25:
16833 case DW_OP_reg26:
16834 case DW_OP_reg27:
16835 case DW_OP_reg28:
16836 case DW_OP_reg29:
16837 case DW_OP_reg30:
16838 case DW_OP_reg31:
16839 stack[++stacki] = op - DW_OP_reg0;
16840 if (i < size)
16841 dwarf2_complex_location_expr_complaint ();
16842 break;
16843
16844 case DW_OP_regx:
16845 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16846 i += bytes_read;
16847 stack[++stacki] = unsnd;
16848 if (i < size)
16849 dwarf2_complex_location_expr_complaint ();
16850 break;
16851
16852 case DW_OP_addr:
16853 stack[++stacki] = read_address (objfile->obfd, &data[i],
16854 cu, &bytes_read);
16855 i += bytes_read;
16856 break;
16857
16858 case DW_OP_const1u:
16859 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16860 i += 1;
16861 break;
16862
16863 case DW_OP_const1s:
16864 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16865 i += 1;
16866 break;
16867
16868 case DW_OP_const2u:
16869 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16870 i += 2;
16871 break;
16872
16873 case DW_OP_const2s:
16874 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16875 i += 2;
16876 break;
16877
16878 case DW_OP_const4u:
16879 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16880 i += 4;
16881 break;
16882
16883 case DW_OP_const4s:
16884 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16885 i += 4;
16886 break;
16887
16888 case DW_OP_const8u:
16889 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16890 i += 8;
16891 break;
16892
16893 case DW_OP_constu:
16894 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16895 &bytes_read);
16896 i += bytes_read;
16897 break;
16898
16899 case DW_OP_consts:
16900 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16901 i += bytes_read;
16902 break;
16903
16904 case DW_OP_dup:
16905 stack[stacki + 1] = stack[stacki];
16906 stacki++;
16907 break;
16908
16909 case DW_OP_plus:
16910 stack[stacki - 1] += stack[stacki];
16911 stacki--;
16912 break;
16913
16914 case DW_OP_plus_uconst:
16915 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16916 &bytes_read);
16917 i += bytes_read;
16918 break;
16919
16920 case DW_OP_minus:
16921 stack[stacki - 1] -= stack[stacki];
16922 stacki--;
16923 break;
16924
16925 case DW_OP_deref:
16926 /* If we're not the last op, then we definitely can't encode
16927 this using GDB's address_class enum. This is valid for partial
16928 global symbols, although the variable's address will be bogus
16929 in the psymtab. */
16930 if (i < size)
16931 dwarf2_complex_location_expr_complaint ();
16932 break;
16933
16934 case DW_OP_GNU_push_tls_address:
16935 /* The top of the stack has the offset from the beginning
16936 of the thread control block at which the variable is located. */
16937 /* Nothing should follow this operator, so the top of stack would
16938 be returned. */
16939 /* This is valid for partial global symbols, but the variable's
16940 address will be bogus in the psymtab. Make it always at least
16941 non-zero to not look as a variable garbage collected by linker
16942 which have DW_OP_addr 0. */
16943 if (i < size)
16944 dwarf2_complex_location_expr_complaint ();
16945 stack[stacki]++;
16946 break;
16947
16948 case DW_OP_GNU_uninit:
16949 break;
16950
16951 case DW_OP_GNU_addr_index:
16952 case DW_OP_GNU_const_index:
16953 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16954 &bytes_read);
16955 i += bytes_read;
16956 break;
16957
16958 default:
16959 {
16960 const char *name = get_DW_OP_name (op);
16961
16962 if (name)
16963 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16964 name);
16965 else
16966 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16967 op);
16968 }
16969
16970 return (stack[stacki]);
16971 }
16972
16973 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16974 outside of the allocated space. Also enforce minimum>0. */
16975 if (stacki >= ARRAY_SIZE (stack) - 1)
16976 {
16977 complaint (&symfile_complaints,
16978 _("location description stack overflow"));
16979 return 0;
16980 }
16981
16982 if (stacki <= 0)
16983 {
16984 complaint (&symfile_complaints,
16985 _("location description stack underflow"));
16986 return 0;
16987 }
16988 }
16989 return (stack[stacki]);
16990 }
16991
16992 /* memory allocation interface */
16993
16994 static struct dwarf_block *
16995 dwarf_alloc_block (struct dwarf2_cu *cu)
16996 {
16997 struct dwarf_block *blk;
16998
16999 blk = (struct dwarf_block *)
17000 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17001 return (blk);
17002 }
17003
17004 static struct die_info *
17005 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17006 {
17007 struct die_info *die;
17008 size_t size = sizeof (struct die_info);
17009
17010 if (num_attrs > 1)
17011 size += (num_attrs - 1) * sizeof (struct attribute);
17012
17013 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17014 memset (die, 0, sizeof (struct die_info));
17015 return (die);
17016 }
17017
17018 \f
17019 /* Macro support. */
17020
17021 /* Return the full name of file number I in *LH's file name table.
17022 Use COMP_DIR as the name of the current directory of the
17023 compilation. The result is allocated using xmalloc; the caller is
17024 responsible for freeing it. */
17025 static char *
17026 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17027 {
17028 /* Is the file number a valid index into the line header's file name
17029 table? Remember that file numbers start with one, not zero. */
17030 if (1 <= file && file <= lh->num_file_names)
17031 {
17032 struct file_entry *fe = &lh->file_names[file - 1];
17033
17034 if (IS_ABSOLUTE_PATH (fe->name))
17035 return xstrdup (fe->name);
17036 else
17037 {
17038 const char *dir;
17039 int dir_len;
17040 char *full_name;
17041
17042 if (fe->dir_index)
17043 dir = lh->include_dirs[fe->dir_index - 1];
17044 else
17045 dir = comp_dir;
17046
17047 if (dir)
17048 {
17049 dir_len = strlen (dir);
17050 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17051 strcpy (full_name, dir);
17052 full_name[dir_len] = '/';
17053 strcpy (full_name + dir_len + 1, fe->name);
17054 return full_name;
17055 }
17056 else
17057 return xstrdup (fe->name);
17058 }
17059 }
17060 else
17061 {
17062 /* The compiler produced a bogus file number. We can at least
17063 record the macro definitions made in the file, even if we
17064 won't be able to find the file by name. */
17065 char fake_name[80];
17066
17067 sprintf (fake_name, "<bad macro file number %d>", file);
17068
17069 complaint (&symfile_complaints,
17070 _("bad file number in macro information (%d)"),
17071 file);
17072
17073 return xstrdup (fake_name);
17074 }
17075 }
17076
17077
17078 static struct macro_source_file *
17079 macro_start_file (int file, int line,
17080 struct macro_source_file *current_file,
17081 const char *comp_dir,
17082 struct line_header *lh, struct objfile *objfile)
17083 {
17084 /* The full name of this source file. */
17085 char *full_name = file_full_name (file, lh, comp_dir);
17086
17087 /* We don't create a macro table for this compilation unit
17088 at all until we actually get a filename. */
17089 if (! pending_macros)
17090 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17091 objfile->per_bfd->macro_cache);
17092
17093 if (! current_file)
17094 {
17095 /* If we have no current file, then this must be the start_file
17096 directive for the compilation unit's main source file. */
17097 current_file = macro_set_main (pending_macros, full_name);
17098 macro_define_special (pending_macros);
17099 }
17100 else
17101 current_file = macro_include (current_file, line, full_name);
17102
17103 xfree (full_name);
17104
17105 return current_file;
17106 }
17107
17108
17109 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17110 followed by a null byte. */
17111 static char *
17112 copy_string (const char *buf, int len)
17113 {
17114 char *s = xmalloc (len + 1);
17115
17116 memcpy (s, buf, len);
17117 s[len] = '\0';
17118 return s;
17119 }
17120
17121
17122 static const char *
17123 consume_improper_spaces (const char *p, const char *body)
17124 {
17125 if (*p == ' ')
17126 {
17127 complaint (&symfile_complaints,
17128 _("macro definition contains spaces "
17129 "in formal argument list:\n`%s'"),
17130 body);
17131
17132 while (*p == ' ')
17133 p++;
17134 }
17135
17136 return p;
17137 }
17138
17139
17140 static void
17141 parse_macro_definition (struct macro_source_file *file, int line,
17142 const char *body)
17143 {
17144 const char *p;
17145
17146 /* The body string takes one of two forms. For object-like macro
17147 definitions, it should be:
17148
17149 <macro name> " " <definition>
17150
17151 For function-like macro definitions, it should be:
17152
17153 <macro name> "() " <definition>
17154 or
17155 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17156
17157 Spaces may appear only where explicitly indicated, and in the
17158 <definition>.
17159
17160 The Dwarf 2 spec says that an object-like macro's name is always
17161 followed by a space, but versions of GCC around March 2002 omit
17162 the space when the macro's definition is the empty string.
17163
17164 The Dwarf 2 spec says that there should be no spaces between the
17165 formal arguments in a function-like macro's formal argument list,
17166 but versions of GCC around March 2002 include spaces after the
17167 commas. */
17168
17169
17170 /* Find the extent of the macro name. The macro name is terminated
17171 by either a space or null character (for an object-like macro) or
17172 an opening paren (for a function-like macro). */
17173 for (p = body; *p; p++)
17174 if (*p == ' ' || *p == '(')
17175 break;
17176
17177 if (*p == ' ' || *p == '\0')
17178 {
17179 /* It's an object-like macro. */
17180 int name_len = p - body;
17181 char *name = copy_string (body, name_len);
17182 const char *replacement;
17183
17184 if (*p == ' ')
17185 replacement = body + name_len + 1;
17186 else
17187 {
17188 dwarf2_macro_malformed_definition_complaint (body);
17189 replacement = body + name_len;
17190 }
17191
17192 macro_define_object (file, line, name, replacement);
17193
17194 xfree (name);
17195 }
17196 else if (*p == '(')
17197 {
17198 /* It's a function-like macro. */
17199 char *name = copy_string (body, p - body);
17200 int argc = 0;
17201 int argv_size = 1;
17202 char **argv = xmalloc (argv_size * sizeof (*argv));
17203
17204 p++;
17205
17206 p = consume_improper_spaces (p, body);
17207
17208 /* Parse the formal argument list. */
17209 while (*p && *p != ')')
17210 {
17211 /* Find the extent of the current argument name. */
17212 const char *arg_start = p;
17213
17214 while (*p && *p != ',' && *p != ')' && *p != ' ')
17215 p++;
17216
17217 if (! *p || p == arg_start)
17218 dwarf2_macro_malformed_definition_complaint (body);
17219 else
17220 {
17221 /* Make sure argv has room for the new argument. */
17222 if (argc >= argv_size)
17223 {
17224 argv_size *= 2;
17225 argv = xrealloc (argv, argv_size * sizeof (*argv));
17226 }
17227
17228 argv[argc++] = copy_string (arg_start, p - arg_start);
17229 }
17230
17231 p = consume_improper_spaces (p, body);
17232
17233 /* Consume the comma, if present. */
17234 if (*p == ',')
17235 {
17236 p++;
17237
17238 p = consume_improper_spaces (p, body);
17239 }
17240 }
17241
17242 if (*p == ')')
17243 {
17244 p++;
17245
17246 if (*p == ' ')
17247 /* Perfectly formed definition, no complaints. */
17248 macro_define_function (file, line, name,
17249 argc, (const char **) argv,
17250 p + 1);
17251 else if (*p == '\0')
17252 {
17253 /* Complain, but do define it. */
17254 dwarf2_macro_malformed_definition_complaint (body);
17255 macro_define_function (file, line, name,
17256 argc, (const char **) argv,
17257 p);
17258 }
17259 else
17260 /* Just complain. */
17261 dwarf2_macro_malformed_definition_complaint (body);
17262 }
17263 else
17264 /* Just complain. */
17265 dwarf2_macro_malformed_definition_complaint (body);
17266
17267 xfree (name);
17268 {
17269 int i;
17270
17271 for (i = 0; i < argc; i++)
17272 xfree (argv[i]);
17273 }
17274 xfree (argv);
17275 }
17276 else
17277 dwarf2_macro_malformed_definition_complaint (body);
17278 }
17279
17280 /* Skip some bytes from BYTES according to the form given in FORM.
17281 Returns the new pointer. */
17282
17283 static gdb_byte *
17284 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17285 enum dwarf_form form,
17286 unsigned int offset_size,
17287 struct dwarf2_section_info *section)
17288 {
17289 unsigned int bytes_read;
17290
17291 switch (form)
17292 {
17293 case DW_FORM_data1:
17294 case DW_FORM_flag:
17295 ++bytes;
17296 break;
17297
17298 case DW_FORM_data2:
17299 bytes += 2;
17300 break;
17301
17302 case DW_FORM_data4:
17303 bytes += 4;
17304 break;
17305
17306 case DW_FORM_data8:
17307 bytes += 8;
17308 break;
17309
17310 case DW_FORM_string:
17311 read_direct_string (abfd, bytes, &bytes_read);
17312 bytes += bytes_read;
17313 break;
17314
17315 case DW_FORM_sec_offset:
17316 case DW_FORM_strp:
17317 case DW_FORM_GNU_strp_alt:
17318 bytes += offset_size;
17319 break;
17320
17321 case DW_FORM_block:
17322 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17323 bytes += bytes_read;
17324 break;
17325
17326 case DW_FORM_block1:
17327 bytes += 1 + read_1_byte (abfd, bytes);
17328 break;
17329 case DW_FORM_block2:
17330 bytes += 2 + read_2_bytes (abfd, bytes);
17331 break;
17332 case DW_FORM_block4:
17333 bytes += 4 + read_4_bytes (abfd, bytes);
17334 break;
17335
17336 case DW_FORM_sdata:
17337 case DW_FORM_udata:
17338 case DW_FORM_GNU_addr_index:
17339 case DW_FORM_GNU_str_index:
17340 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17341 if (bytes == NULL)
17342 {
17343 dwarf2_section_buffer_overflow_complaint (section);
17344 return NULL;
17345 }
17346 break;
17347
17348 default:
17349 {
17350 complain:
17351 complaint (&symfile_complaints,
17352 _("invalid form 0x%x in `%s'"),
17353 form,
17354 section->asection->name);
17355 return NULL;
17356 }
17357 }
17358
17359 return bytes;
17360 }
17361
17362 /* A helper for dwarf_decode_macros that handles skipping an unknown
17363 opcode. Returns an updated pointer to the macro data buffer; or,
17364 on error, issues a complaint and returns NULL. */
17365
17366 static gdb_byte *
17367 skip_unknown_opcode (unsigned int opcode,
17368 gdb_byte **opcode_definitions,
17369 gdb_byte *mac_ptr, gdb_byte *mac_end,
17370 bfd *abfd,
17371 unsigned int offset_size,
17372 struct dwarf2_section_info *section)
17373 {
17374 unsigned int bytes_read, i;
17375 unsigned long arg;
17376 gdb_byte *defn;
17377
17378 if (opcode_definitions[opcode] == NULL)
17379 {
17380 complaint (&symfile_complaints,
17381 _("unrecognized DW_MACFINO opcode 0x%x"),
17382 opcode);
17383 return NULL;
17384 }
17385
17386 defn = opcode_definitions[opcode];
17387 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17388 defn += bytes_read;
17389
17390 for (i = 0; i < arg; ++i)
17391 {
17392 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17393 section);
17394 if (mac_ptr == NULL)
17395 {
17396 /* skip_form_bytes already issued the complaint. */
17397 return NULL;
17398 }
17399 }
17400
17401 return mac_ptr;
17402 }
17403
17404 /* A helper function which parses the header of a macro section.
17405 If the macro section is the extended (for now called "GNU") type,
17406 then this updates *OFFSET_SIZE. Returns a pointer to just after
17407 the header, or issues a complaint and returns NULL on error. */
17408
17409 static gdb_byte *
17410 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17411 bfd *abfd,
17412 gdb_byte *mac_ptr,
17413 unsigned int *offset_size,
17414 int section_is_gnu)
17415 {
17416 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17417
17418 if (section_is_gnu)
17419 {
17420 unsigned int version, flags;
17421
17422 version = read_2_bytes (abfd, mac_ptr);
17423 if (version != 4)
17424 {
17425 complaint (&symfile_complaints,
17426 _("unrecognized version `%d' in .debug_macro section"),
17427 version);
17428 return NULL;
17429 }
17430 mac_ptr += 2;
17431
17432 flags = read_1_byte (abfd, mac_ptr);
17433 ++mac_ptr;
17434 *offset_size = (flags & 1) ? 8 : 4;
17435
17436 if ((flags & 2) != 0)
17437 /* We don't need the line table offset. */
17438 mac_ptr += *offset_size;
17439
17440 /* Vendor opcode descriptions. */
17441 if ((flags & 4) != 0)
17442 {
17443 unsigned int i, count;
17444
17445 count = read_1_byte (abfd, mac_ptr);
17446 ++mac_ptr;
17447 for (i = 0; i < count; ++i)
17448 {
17449 unsigned int opcode, bytes_read;
17450 unsigned long arg;
17451
17452 opcode = read_1_byte (abfd, mac_ptr);
17453 ++mac_ptr;
17454 opcode_definitions[opcode] = mac_ptr;
17455 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17456 mac_ptr += bytes_read;
17457 mac_ptr += arg;
17458 }
17459 }
17460 }
17461
17462 return mac_ptr;
17463 }
17464
17465 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17466 including DW_MACRO_GNU_transparent_include. */
17467
17468 static void
17469 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17470 struct macro_source_file *current_file,
17471 struct line_header *lh, char *comp_dir,
17472 struct dwarf2_section_info *section,
17473 int section_is_gnu, int section_is_dwz,
17474 unsigned int offset_size,
17475 struct objfile *objfile,
17476 htab_t include_hash)
17477 {
17478 enum dwarf_macro_record_type macinfo_type;
17479 int at_commandline;
17480 gdb_byte *opcode_definitions[256];
17481
17482 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17483 &offset_size, section_is_gnu);
17484 if (mac_ptr == NULL)
17485 {
17486 /* We already issued a complaint. */
17487 return;
17488 }
17489
17490 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17491 GDB is still reading the definitions from command line. First
17492 DW_MACINFO_start_file will need to be ignored as it was already executed
17493 to create CURRENT_FILE for the main source holding also the command line
17494 definitions. On first met DW_MACINFO_start_file this flag is reset to
17495 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17496
17497 at_commandline = 1;
17498
17499 do
17500 {
17501 /* Do we at least have room for a macinfo type byte? */
17502 if (mac_ptr >= mac_end)
17503 {
17504 dwarf2_section_buffer_overflow_complaint (section);
17505 break;
17506 }
17507
17508 macinfo_type = read_1_byte (abfd, mac_ptr);
17509 mac_ptr++;
17510
17511 /* Note that we rely on the fact that the corresponding GNU and
17512 DWARF constants are the same. */
17513 switch (macinfo_type)
17514 {
17515 /* A zero macinfo type indicates the end of the macro
17516 information. */
17517 case 0:
17518 break;
17519
17520 case DW_MACRO_GNU_define:
17521 case DW_MACRO_GNU_undef:
17522 case DW_MACRO_GNU_define_indirect:
17523 case DW_MACRO_GNU_undef_indirect:
17524 case DW_MACRO_GNU_define_indirect_alt:
17525 case DW_MACRO_GNU_undef_indirect_alt:
17526 {
17527 unsigned int bytes_read;
17528 int line;
17529 char *body;
17530 int is_define;
17531
17532 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17533 mac_ptr += bytes_read;
17534
17535 if (macinfo_type == DW_MACRO_GNU_define
17536 || macinfo_type == DW_MACRO_GNU_undef)
17537 {
17538 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17539 mac_ptr += bytes_read;
17540 }
17541 else
17542 {
17543 LONGEST str_offset;
17544
17545 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17546 mac_ptr += offset_size;
17547
17548 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
17549 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17550 || section_is_dwz)
17551 {
17552 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17553
17554 body = read_indirect_string_from_dwz (dwz, str_offset);
17555 }
17556 else
17557 body = read_indirect_string_at_offset (abfd, str_offset);
17558 }
17559
17560 is_define = (macinfo_type == DW_MACRO_GNU_define
17561 || macinfo_type == DW_MACRO_GNU_define_indirect
17562 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
17563 if (! current_file)
17564 {
17565 /* DWARF violation as no main source is present. */
17566 complaint (&symfile_complaints,
17567 _("debug info with no main source gives macro %s "
17568 "on line %d: %s"),
17569 is_define ? _("definition") : _("undefinition"),
17570 line, body);
17571 break;
17572 }
17573 if ((line == 0 && !at_commandline)
17574 || (line != 0 && at_commandline))
17575 complaint (&symfile_complaints,
17576 _("debug info gives %s macro %s with %s line %d: %s"),
17577 at_commandline ? _("command-line") : _("in-file"),
17578 is_define ? _("definition") : _("undefinition"),
17579 line == 0 ? _("zero") : _("non-zero"), line, body);
17580
17581 if (is_define)
17582 parse_macro_definition (current_file, line, body);
17583 else
17584 {
17585 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17586 || macinfo_type == DW_MACRO_GNU_undef_indirect
17587 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
17588 macro_undef (current_file, line, body);
17589 }
17590 }
17591 break;
17592
17593 case DW_MACRO_GNU_start_file:
17594 {
17595 unsigned int bytes_read;
17596 int line, file;
17597
17598 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17599 mac_ptr += bytes_read;
17600 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17601 mac_ptr += bytes_read;
17602
17603 if ((line == 0 && !at_commandline)
17604 || (line != 0 && at_commandline))
17605 complaint (&symfile_complaints,
17606 _("debug info gives source %d included "
17607 "from %s at %s line %d"),
17608 file, at_commandline ? _("command-line") : _("file"),
17609 line == 0 ? _("zero") : _("non-zero"), line);
17610
17611 if (at_commandline)
17612 {
17613 /* This DW_MACRO_GNU_start_file was executed in the
17614 pass one. */
17615 at_commandline = 0;
17616 }
17617 else
17618 current_file = macro_start_file (file, line,
17619 current_file, comp_dir,
17620 lh, objfile);
17621 }
17622 break;
17623
17624 case DW_MACRO_GNU_end_file:
17625 if (! current_file)
17626 complaint (&symfile_complaints,
17627 _("macro debug info has an unmatched "
17628 "`close_file' directive"));
17629 else
17630 {
17631 current_file = current_file->included_by;
17632 if (! current_file)
17633 {
17634 enum dwarf_macro_record_type next_type;
17635
17636 /* GCC circa March 2002 doesn't produce the zero
17637 type byte marking the end of the compilation
17638 unit. Complain if it's not there, but exit no
17639 matter what. */
17640
17641 /* Do we at least have room for a macinfo type byte? */
17642 if (mac_ptr >= mac_end)
17643 {
17644 dwarf2_section_buffer_overflow_complaint (section);
17645 return;
17646 }
17647
17648 /* We don't increment mac_ptr here, so this is just
17649 a look-ahead. */
17650 next_type = read_1_byte (abfd, mac_ptr);
17651 if (next_type != 0)
17652 complaint (&symfile_complaints,
17653 _("no terminating 0-type entry for "
17654 "macros in `.debug_macinfo' section"));
17655
17656 return;
17657 }
17658 }
17659 break;
17660
17661 case DW_MACRO_GNU_transparent_include:
17662 case DW_MACRO_GNU_transparent_include_alt:
17663 {
17664 LONGEST offset;
17665 void **slot;
17666 bfd *include_bfd = abfd;
17667 struct dwarf2_section_info *include_section = section;
17668 struct dwarf2_section_info alt_section;
17669 gdb_byte *include_mac_end = mac_end;
17670 int is_dwz = section_is_dwz;
17671 gdb_byte *new_mac_ptr;
17672
17673 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17674 mac_ptr += offset_size;
17675
17676 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17677 {
17678 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17679
17680 dwarf2_read_section (dwarf2_per_objfile->objfile,
17681 &dwz->macro);
17682
17683 include_bfd = dwz->macro.asection->owner;
17684 include_section = &dwz->macro;
17685 include_mac_end = dwz->macro.buffer + dwz->macro.size;
17686 is_dwz = 1;
17687 }
17688
17689 new_mac_ptr = include_section->buffer + offset;
17690 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
17691
17692 if (*slot != NULL)
17693 {
17694 /* This has actually happened; see
17695 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17696 complaint (&symfile_complaints,
17697 _("recursive DW_MACRO_GNU_transparent_include in "
17698 ".debug_macro section"));
17699 }
17700 else
17701 {
17702 *slot = new_mac_ptr;
17703
17704 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
17705 include_mac_end, current_file,
17706 lh, comp_dir,
17707 section, section_is_gnu, is_dwz,
17708 offset_size, objfile, include_hash);
17709
17710 htab_remove_elt (include_hash, new_mac_ptr);
17711 }
17712 }
17713 break;
17714
17715 case DW_MACINFO_vendor_ext:
17716 if (!section_is_gnu)
17717 {
17718 unsigned int bytes_read;
17719 int constant;
17720
17721 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17722 mac_ptr += bytes_read;
17723 read_direct_string (abfd, mac_ptr, &bytes_read);
17724 mac_ptr += bytes_read;
17725
17726 /* We don't recognize any vendor extensions. */
17727 break;
17728 }
17729 /* FALLTHROUGH */
17730
17731 default:
17732 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17733 mac_ptr, mac_end, abfd, offset_size,
17734 section);
17735 if (mac_ptr == NULL)
17736 return;
17737 break;
17738 }
17739 } while (macinfo_type != 0);
17740 }
17741
17742 static void
17743 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17744 char *comp_dir, int section_is_gnu)
17745 {
17746 struct objfile *objfile = dwarf2_per_objfile->objfile;
17747 struct line_header *lh = cu->line_header;
17748 bfd *abfd;
17749 gdb_byte *mac_ptr, *mac_end;
17750 struct macro_source_file *current_file = 0;
17751 enum dwarf_macro_record_type macinfo_type;
17752 unsigned int offset_size = cu->header.offset_size;
17753 gdb_byte *opcode_definitions[256];
17754 struct cleanup *cleanup;
17755 htab_t include_hash;
17756 void **slot;
17757 struct dwarf2_section_info *section;
17758 const char *section_name;
17759
17760 if (cu->dwo_unit != NULL)
17761 {
17762 if (section_is_gnu)
17763 {
17764 section = &cu->dwo_unit->dwo_file->sections.macro;
17765 section_name = ".debug_macro.dwo";
17766 }
17767 else
17768 {
17769 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17770 section_name = ".debug_macinfo.dwo";
17771 }
17772 }
17773 else
17774 {
17775 if (section_is_gnu)
17776 {
17777 section = &dwarf2_per_objfile->macro;
17778 section_name = ".debug_macro";
17779 }
17780 else
17781 {
17782 section = &dwarf2_per_objfile->macinfo;
17783 section_name = ".debug_macinfo";
17784 }
17785 }
17786
17787 dwarf2_read_section (objfile, section);
17788 if (section->buffer == NULL)
17789 {
17790 complaint (&symfile_complaints, _("missing %s section"), section_name);
17791 return;
17792 }
17793 abfd = section->asection->owner;
17794
17795 /* First pass: Find the name of the base filename.
17796 This filename is needed in order to process all macros whose definition
17797 (or undefinition) comes from the command line. These macros are defined
17798 before the first DW_MACINFO_start_file entry, and yet still need to be
17799 associated to the base file.
17800
17801 To determine the base file name, we scan the macro definitions until we
17802 reach the first DW_MACINFO_start_file entry. We then initialize
17803 CURRENT_FILE accordingly so that any macro definition found before the
17804 first DW_MACINFO_start_file can still be associated to the base file. */
17805
17806 mac_ptr = section->buffer + offset;
17807 mac_end = section->buffer + section->size;
17808
17809 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17810 &offset_size, section_is_gnu);
17811 if (mac_ptr == NULL)
17812 {
17813 /* We already issued a complaint. */
17814 return;
17815 }
17816
17817 do
17818 {
17819 /* Do we at least have room for a macinfo type byte? */
17820 if (mac_ptr >= mac_end)
17821 {
17822 /* Complaint is printed during the second pass as GDB will probably
17823 stop the first pass earlier upon finding
17824 DW_MACINFO_start_file. */
17825 break;
17826 }
17827
17828 macinfo_type = read_1_byte (abfd, mac_ptr);
17829 mac_ptr++;
17830
17831 /* Note that we rely on the fact that the corresponding GNU and
17832 DWARF constants are the same. */
17833 switch (macinfo_type)
17834 {
17835 /* A zero macinfo type indicates the end of the macro
17836 information. */
17837 case 0:
17838 break;
17839
17840 case DW_MACRO_GNU_define:
17841 case DW_MACRO_GNU_undef:
17842 /* Only skip the data by MAC_PTR. */
17843 {
17844 unsigned int bytes_read;
17845
17846 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17847 mac_ptr += bytes_read;
17848 read_direct_string (abfd, mac_ptr, &bytes_read);
17849 mac_ptr += bytes_read;
17850 }
17851 break;
17852
17853 case DW_MACRO_GNU_start_file:
17854 {
17855 unsigned int bytes_read;
17856 int line, file;
17857
17858 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17859 mac_ptr += bytes_read;
17860 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17861 mac_ptr += bytes_read;
17862
17863 current_file = macro_start_file (file, line, current_file,
17864 comp_dir, lh, objfile);
17865 }
17866 break;
17867
17868 case DW_MACRO_GNU_end_file:
17869 /* No data to skip by MAC_PTR. */
17870 break;
17871
17872 case DW_MACRO_GNU_define_indirect:
17873 case DW_MACRO_GNU_undef_indirect:
17874 case DW_MACRO_GNU_define_indirect_alt:
17875 case DW_MACRO_GNU_undef_indirect_alt:
17876 {
17877 unsigned int bytes_read;
17878
17879 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17880 mac_ptr += bytes_read;
17881 mac_ptr += offset_size;
17882 }
17883 break;
17884
17885 case DW_MACRO_GNU_transparent_include:
17886 case DW_MACRO_GNU_transparent_include_alt:
17887 /* Note that, according to the spec, a transparent include
17888 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17889 skip this opcode. */
17890 mac_ptr += offset_size;
17891 break;
17892
17893 case DW_MACINFO_vendor_ext:
17894 /* Only skip the data by MAC_PTR. */
17895 if (!section_is_gnu)
17896 {
17897 unsigned int bytes_read;
17898
17899 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17900 mac_ptr += bytes_read;
17901 read_direct_string (abfd, mac_ptr, &bytes_read);
17902 mac_ptr += bytes_read;
17903 }
17904 /* FALLTHROUGH */
17905
17906 default:
17907 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17908 mac_ptr, mac_end, abfd, offset_size,
17909 section);
17910 if (mac_ptr == NULL)
17911 return;
17912 break;
17913 }
17914 } while (macinfo_type != 0 && current_file == NULL);
17915
17916 /* Second pass: Process all entries.
17917
17918 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17919 command-line macro definitions/undefinitions. This flag is unset when we
17920 reach the first DW_MACINFO_start_file entry. */
17921
17922 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17923 NULL, xcalloc, xfree);
17924 cleanup = make_cleanup_htab_delete (include_hash);
17925 mac_ptr = section->buffer + offset;
17926 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17927 *slot = mac_ptr;
17928 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17929 current_file, lh, comp_dir, section,
17930 section_is_gnu, 0,
17931 offset_size, objfile, include_hash);
17932 do_cleanups (cleanup);
17933 }
17934
17935 /* Check if the attribute's form is a DW_FORM_block*
17936 if so return true else false. */
17937
17938 static int
17939 attr_form_is_block (struct attribute *attr)
17940 {
17941 return (attr == NULL ? 0 :
17942 attr->form == DW_FORM_block1
17943 || attr->form == DW_FORM_block2
17944 || attr->form == DW_FORM_block4
17945 || attr->form == DW_FORM_block
17946 || attr->form == DW_FORM_exprloc);
17947 }
17948
17949 /* Return non-zero if ATTR's value is a section offset --- classes
17950 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17951 You may use DW_UNSND (attr) to retrieve such offsets.
17952
17953 Section 7.5.4, "Attribute Encodings", explains that no attribute
17954 may have a value that belongs to more than one of these classes; it
17955 would be ambiguous if we did, because we use the same forms for all
17956 of them. */
17957
17958 static int
17959 attr_form_is_section_offset (struct attribute *attr)
17960 {
17961 return (attr->form == DW_FORM_data4
17962 || attr->form == DW_FORM_data8
17963 || attr->form == DW_FORM_sec_offset);
17964 }
17965
17966 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17967 zero otherwise. When this function returns true, you can apply
17968 dwarf2_get_attr_constant_value to it.
17969
17970 However, note that for some attributes you must check
17971 attr_form_is_section_offset before using this test. DW_FORM_data4
17972 and DW_FORM_data8 are members of both the constant class, and of
17973 the classes that contain offsets into other debug sections
17974 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17975 that, if an attribute's can be either a constant or one of the
17976 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17977 taken as section offsets, not constants. */
17978
17979 static int
17980 attr_form_is_constant (struct attribute *attr)
17981 {
17982 switch (attr->form)
17983 {
17984 case DW_FORM_sdata:
17985 case DW_FORM_udata:
17986 case DW_FORM_data1:
17987 case DW_FORM_data2:
17988 case DW_FORM_data4:
17989 case DW_FORM_data8:
17990 return 1;
17991 default:
17992 return 0;
17993 }
17994 }
17995
17996 /* Return the .debug_loc section to use for CU.
17997 For DWO files use .debug_loc.dwo. */
17998
17999 static struct dwarf2_section_info *
18000 cu_debug_loc_section (struct dwarf2_cu *cu)
18001 {
18002 if (cu->dwo_unit)
18003 return &cu->dwo_unit->dwo_file->sections.loc;
18004 return &dwarf2_per_objfile->loc;
18005 }
18006
18007 /* A helper function that fills in a dwarf2_loclist_baton. */
18008
18009 static void
18010 fill_in_loclist_baton (struct dwarf2_cu *cu,
18011 struct dwarf2_loclist_baton *baton,
18012 struct attribute *attr)
18013 {
18014 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18015
18016 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18017
18018 baton->per_cu = cu->per_cu;
18019 gdb_assert (baton->per_cu);
18020 /* We don't know how long the location list is, but make sure we
18021 don't run off the edge of the section. */
18022 baton->size = section->size - DW_UNSND (attr);
18023 baton->data = section->buffer + DW_UNSND (attr);
18024 baton->base_address = cu->base_address;
18025 baton->from_dwo = cu->dwo_unit != NULL;
18026 }
18027
18028 static void
18029 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18030 struct dwarf2_cu *cu)
18031 {
18032 struct objfile *objfile = dwarf2_per_objfile->objfile;
18033 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18034
18035 if (attr_form_is_section_offset (attr)
18036 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18037 the section. If so, fall through to the complaint in the
18038 other branch. */
18039 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18040 {
18041 struct dwarf2_loclist_baton *baton;
18042
18043 baton = obstack_alloc (&objfile->objfile_obstack,
18044 sizeof (struct dwarf2_loclist_baton));
18045
18046 fill_in_loclist_baton (cu, baton, attr);
18047
18048 if (cu->base_known == 0)
18049 complaint (&symfile_complaints,
18050 _("Location list used without "
18051 "specifying the CU base address."));
18052
18053 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18054 SYMBOL_LOCATION_BATON (sym) = baton;
18055 }
18056 else
18057 {
18058 struct dwarf2_locexpr_baton *baton;
18059
18060 baton = obstack_alloc (&objfile->objfile_obstack,
18061 sizeof (struct dwarf2_locexpr_baton));
18062 baton->per_cu = cu->per_cu;
18063 gdb_assert (baton->per_cu);
18064
18065 if (attr_form_is_block (attr))
18066 {
18067 /* Note that we're just copying the block's data pointer
18068 here, not the actual data. We're still pointing into the
18069 info_buffer for SYM's objfile; right now we never release
18070 that buffer, but when we do clean up properly this may
18071 need to change. */
18072 baton->size = DW_BLOCK (attr)->size;
18073 baton->data = DW_BLOCK (attr)->data;
18074 }
18075 else
18076 {
18077 dwarf2_invalid_attrib_class_complaint ("location description",
18078 SYMBOL_NATURAL_NAME (sym));
18079 baton->size = 0;
18080 }
18081
18082 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18083 SYMBOL_LOCATION_BATON (sym) = baton;
18084 }
18085 }
18086
18087 /* Return the OBJFILE associated with the compilation unit CU. If CU
18088 came from a separate debuginfo file, then the master objfile is
18089 returned. */
18090
18091 struct objfile *
18092 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18093 {
18094 struct objfile *objfile = per_cu->objfile;
18095
18096 /* Return the master objfile, so that we can report and look up the
18097 correct file containing this variable. */
18098 if (objfile->separate_debug_objfile_backlink)
18099 objfile = objfile->separate_debug_objfile_backlink;
18100
18101 return objfile;
18102 }
18103
18104 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18105 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18106 CU_HEADERP first. */
18107
18108 static const struct comp_unit_head *
18109 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18110 struct dwarf2_per_cu_data *per_cu)
18111 {
18112 gdb_byte *info_ptr;
18113
18114 if (per_cu->cu)
18115 return &per_cu->cu->header;
18116
18117 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18118
18119 memset (cu_headerp, 0, sizeof (*cu_headerp));
18120 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18121
18122 return cu_headerp;
18123 }
18124
18125 /* Return the address size given in the compilation unit header for CU. */
18126
18127 int
18128 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18129 {
18130 struct comp_unit_head cu_header_local;
18131 const struct comp_unit_head *cu_headerp;
18132
18133 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18134
18135 return cu_headerp->addr_size;
18136 }
18137
18138 /* Return the offset size given in the compilation unit header for CU. */
18139
18140 int
18141 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18142 {
18143 struct comp_unit_head cu_header_local;
18144 const struct comp_unit_head *cu_headerp;
18145
18146 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18147
18148 return cu_headerp->offset_size;
18149 }
18150
18151 /* See its dwarf2loc.h declaration. */
18152
18153 int
18154 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18155 {
18156 struct comp_unit_head cu_header_local;
18157 const struct comp_unit_head *cu_headerp;
18158
18159 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18160
18161 if (cu_headerp->version == 2)
18162 return cu_headerp->addr_size;
18163 else
18164 return cu_headerp->offset_size;
18165 }
18166
18167 /* Return the text offset of the CU. The returned offset comes from
18168 this CU's objfile. If this objfile came from a separate debuginfo
18169 file, then the offset may be different from the corresponding
18170 offset in the parent objfile. */
18171
18172 CORE_ADDR
18173 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
18174 {
18175 struct objfile *objfile = per_cu->objfile;
18176
18177 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18178 }
18179
18180 /* Locate the .debug_info compilation unit from CU's objfile which contains
18181 the DIE at OFFSET. Raises an error on failure. */
18182
18183 static struct dwarf2_per_cu_data *
18184 dwarf2_find_containing_comp_unit (sect_offset offset,
18185 unsigned int offset_in_dwz,
18186 struct objfile *objfile)
18187 {
18188 struct dwarf2_per_cu_data *this_cu;
18189 int low, high;
18190 const sect_offset *cu_off;
18191
18192 low = 0;
18193 high = dwarf2_per_objfile->n_comp_units - 1;
18194 while (high > low)
18195 {
18196 struct dwarf2_per_cu_data *mid_cu;
18197 int mid = low + (high - low) / 2;
18198
18199 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
18200 cu_off = &mid_cu->offset;
18201 if (mid_cu->is_dwz > offset_in_dwz
18202 || (mid_cu->is_dwz == offset_in_dwz
18203 && cu_off->sect_off >= offset.sect_off))
18204 high = mid;
18205 else
18206 low = mid + 1;
18207 }
18208 gdb_assert (low == high);
18209 this_cu = dwarf2_per_objfile->all_comp_units[low];
18210 cu_off = &this_cu->offset;
18211 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
18212 {
18213 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
18214 error (_("Dwarf Error: could not find partial DIE containing "
18215 "offset 0x%lx [in module %s]"),
18216 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
18217
18218 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
18219 <= offset.sect_off);
18220 return dwarf2_per_objfile->all_comp_units[low-1];
18221 }
18222 else
18223 {
18224 this_cu = dwarf2_per_objfile->all_comp_units[low];
18225 if (low == dwarf2_per_objfile->n_comp_units - 1
18226 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
18227 error (_("invalid dwarf2 offset %u"), offset.sect_off);
18228 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
18229 return this_cu;
18230 }
18231 }
18232
18233 /* Initialize dwarf2_cu CU, owned by PER_CU. */
18234
18235 static void
18236 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
18237 {
18238 memset (cu, 0, sizeof (*cu));
18239 per_cu->cu = cu;
18240 cu->per_cu = per_cu;
18241 cu->objfile = per_cu->objfile;
18242 obstack_init (&cu->comp_unit_obstack);
18243 }
18244
18245 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
18246
18247 static void
18248 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
18249 enum language pretend_language)
18250 {
18251 struct attribute *attr;
18252
18253 /* Set the language we're debugging. */
18254 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
18255 if (attr)
18256 set_cu_language (DW_UNSND (attr), cu);
18257 else
18258 {
18259 cu->language = pretend_language;
18260 cu->language_defn = language_def (cu->language);
18261 }
18262
18263 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
18264 if (attr)
18265 cu->producer = DW_STRING (attr);
18266 }
18267
18268 /* Release one cached compilation unit, CU. We unlink it from the tree
18269 of compilation units, but we don't remove it from the read_in_chain;
18270 the caller is responsible for that.
18271 NOTE: DATA is a void * because this function is also used as a
18272 cleanup routine. */
18273
18274 static void
18275 free_heap_comp_unit (void *data)
18276 {
18277 struct dwarf2_cu *cu = data;
18278
18279 gdb_assert (cu->per_cu != NULL);
18280 cu->per_cu->cu = NULL;
18281 cu->per_cu = NULL;
18282
18283 obstack_free (&cu->comp_unit_obstack, NULL);
18284
18285 xfree (cu);
18286 }
18287
18288 /* This cleanup function is passed the address of a dwarf2_cu on the stack
18289 when we're finished with it. We can't free the pointer itself, but be
18290 sure to unlink it from the cache. Also release any associated storage. */
18291
18292 static void
18293 free_stack_comp_unit (void *data)
18294 {
18295 struct dwarf2_cu *cu = data;
18296
18297 gdb_assert (cu->per_cu != NULL);
18298 cu->per_cu->cu = NULL;
18299 cu->per_cu = NULL;
18300
18301 obstack_free (&cu->comp_unit_obstack, NULL);
18302 cu->partial_dies = NULL;
18303 }
18304
18305 /* Free all cached compilation units. */
18306
18307 static void
18308 free_cached_comp_units (void *data)
18309 {
18310 struct dwarf2_per_cu_data *per_cu, **last_chain;
18311
18312 per_cu = dwarf2_per_objfile->read_in_chain;
18313 last_chain = &dwarf2_per_objfile->read_in_chain;
18314 while (per_cu != NULL)
18315 {
18316 struct dwarf2_per_cu_data *next_cu;
18317
18318 next_cu = per_cu->cu->read_in_chain;
18319
18320 free_heap_comp_unit (per_cu->cu);
18321 *last_chain = next_cu;
18322
18323 per_cu = next_cu;
18324 }
18325 }
18326
18327 /* Increase the age counter on each cached compilation unit, and free
18328 any that are too old. */
18329
18330 static void
18331 age_cached_comp_units (void)
18332 {
18333 struct dwarf2_per_cu_data *per_cu, **last_chain;
18334
18335 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18336 per_cu = dwarf2_per_objfile->read_in_chain;
18337 while (per_cu != NULL)
18338 {
18339 per_cu->cu->last_used ++;
18340 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18341 dwarf2_mark (per_cu->cu);
18342 per_cu = per_cu->cu->read_in_chain;
18343 }
18344
18345 per_cu = dwarf2_per_objfile->read_in_chain;
18346 last_chain = &dwarf2_per_objfile->read_in_chain;
18347 while (per_cu != NULL)
18348 {
18349 struct dwarf2_per_cu_data *next_cu;
18350
18351 next_cu = per_cu->cu->read_in_chain;
18352
18353 if (!per_cu->cu->mark)
18354 {
18355 free_heap_comp_unit (per_cu->cu);
18356 *last_chain = next_cu;
18357 }
18358 else
18359 last_chain = &per_cu->cu->read_in_chain;
18360
18361 per_cu = next_cu;
18362 }
18363 }
18364
18365 /* Remove a single compilation unit from the cache. */
18366
18367 static void
18368 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
18369 {
18370 struct dwarf2_per_cu_data *per_cu, **last_chain;
18371
18372 per_cu = dwarf2_per_objfile->read_in_chain;
18373 last_chain = &dwarf2_per_objfile->read_in_chain;
18374 while (per_cu != NULL)
18375 {
18376 struct dwarf2_per_cu_data *next_cu;
18377
18378 next_cu = per_cu->cu->read_in_chain;
18379
18380 if (per_cu == target_per_cu)
18381 {
18382 free_heap_comp_unit (per_cu->cu);
18383 per_cu->cu = NULL;
18384 *last_chain = next_cu;
18385 break;
18386 }
18387 else
18388 last_chain = &per_cu->cu->read_in_chain;
18389
18390 per_cu = next_cu;
18391 }
18392 }
18393
18394 /* Release all extra memory associated with OBJFILE. */
18395
18396 void
18397 dwarf2_free_objfile (struct objfile *objfile)
18398 {
18399 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18400
18401 if (dwarf2_per_objfile == NULL)
18402 return;
18403
18404 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18405 free_cached_comp_units (NULL);
18406
18407 if (dwarf2_per_objfile->quick_file_names_table)
18408 htab_delete (dwarf2_per_objfile->quick_file_names_table);
18409
18410 /* Everything else should be on the objfile obstack. */
18411 }
18412
18413 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18414 We store these in a hash table separate from the DIEs, and preserve them
18415 when the DIEs are flushed out of cache.
18416
18417 The CU "per_cu" pointer is needed because offset alone is not enough to
18418 uniquely identify the type. A file may have multiple .debug_types sections,
18419 or the type may come from a DWO file. We have to use something in
18420 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18421 routine, get_die_type_at_offset, from outside this file, and thus won't
18422 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18423 of the objfile. */
18424
18425 struct dwarf2_per_cu_offset_and_type
18426 {
18427 const struct dwarf2_per_cu_data *per_cu;
18428 sect_offset offset;
18429 struct type *type;
18430 };
18431
18432 /* Hash function for a dwarf2_per_cu_offset_and_type. */
18433
18434 static hashval_t
18435 per_cu_offset_and_type_hash (const void *item)
18436 {
18437 const struct dwarf2_per_cu_offset_and_type *ofs = item;
18438
18439 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
18440 }
18441
18442 /* Equality function for a dwarf2_per_cu_offset_and_type. */
18443
18444 static int
18445 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
18446 {
18447 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18448 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
18449
18450 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18451 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18452 }
18453
18454 /* Set the type associated with DIE to TYPE. Save it in CU's hash
18455 table if necessary. For convenience, return TYPE.
18456
18457 The DIEs reading must have careful ordering to:
18458 * Not cause infite loops trying to read in DIEs as a prerequisite for
18459 reading current DIE.
18460 * Not trying to dereference contents of still incompletely read in types
18461 while reading in other DIEs.
18462 * Enable referencing still incompletely read in types just by a pointer to
18463 the type without accessing its fields.
18464
18465 Therefore caller should follow these rules:
18466 * Try to fetch any prerequisite types we may need to build this DIE type
18467 before building the type and calling set_die_type.
18468 * After building type call set_die_type for current DIE as soon as
18469 possible before fetching more types to complete the current type.
18470 * Make the type as complete as possible before fetching more types. */
18471
18472 static struct type *
18473 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18474 {
18475 struct dwarf2_per_cu_offset_and_type **slot, ofs;
18476 struct objfile *objfile = cu->objfile;
18477
18478 /* For Ada types, make sure that the gnat-specific data is always
18479 initialized (if not already set). There are a few types where
18480 we should not be doing so, because the type-specific area is
18481 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18482 where the type-specific area is used to store the floatformat).
18483 But this is not a problem, because the gnat-specific information
18484 is actually not needed for these types. */
18485 if (need_gnat_info (cu)
18486 && TYPE_CODE (type) != TYPE_CODE_FUNC
18487 && TYPE_CODE (type) != TYPE_CODE_FLT
18488 && !HAVE_GNAT_AUX_INFO (type))
18489 INIT_GNAT_SPECIFIC (type);
18490
18491 if (dwarf2_per_objfile->die_type_hash == NULL)
18492 {
18493 dwarf2_per_objfile->die_type_hash =
18494 htab_create_alloc_ex (127,
18495 per_cu_offset_and_type_hash,
18496 per_cu_offset_and_type_eq,
18497 NULL,
18498 &objfile->objfile_obstack,
18499 hashtab_obstack_allocate,
18500 dummy_obstack_deallocate);
18501 }
18502
18503 ofs.per_cu = cu->per_cu;
18504 ofs.offset = die->offset;
18505 ofs.type = type;
18506 slot = (struct dwarf2_per_cu_offset_and_type **)
18507 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18508 if (*slot)
18509 complaint (&symfile_complaints,
18510 _("A problem internal to GDB: DIE 0x%x has type already set"),
18511 die->offset.sect_off);
18512 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18513 **slot = ofs;
18514 return type;
18515 }
18516
18517 /* Look up the type for the die at OFFSET in the appropriate type_hash
18518 table, or return NULL if the die does not have a saved type. */
18519
18520 static struct type *
18521 get_die_type_at_offset (sect_offset offset,
18522 struct dwarf2_per_cu_data *per_cu)
18523 {
18524 struct dwarf2_per_cu_offset_and_type *slot, ofs;
18525
18526 if (dwarf2_per_objfile->die_type_hash == NULL)
18527 return NULL;
18528
18529 ofs.per_cu = per_cu;
18530 ofs.offset = offset;
18531 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18532 if (slot)
18533 return slot->type;
18534 else
18535 return NULL;
18536 }
18537
18538 /* Look up the type for DIE in the appropriate type_hash table,
18539 or return NULL if DIE does not have a saved type. */
18540
18541 static struct type *
18542 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18543 {
18544 return get_die_type_at_offset (die->offset, cu->per_cu);
18545 }
18546
18547 /* Add a dependence relationship from CU to REF_PER_CU. */
18548
18549 static void
18550 dwarf2_add_dependence (struct dwarf2_cu *cu,
18551 struct dwarf2_per_cu_data *ref_per_cu)
18552 {
18553 void **slot;
18554
18555 if (cu->dependencies == NULL)
18556 cu->dependencies
18557 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18558 NULL, &cu->comp_unit_obstack,
18559 hashtab_obstack_allocate,
18560 dummy_obstack_deallocate);
18561
18562 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18563 if (*slot == NULL)
18564 *slot = ref_per_cu;
18565 }
18566
18567 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18568 Set the mark field in every compilation unit in the
18569 cache that we must keep because we are keeping CU. */
18570
18571 static int
18572 dwarf2_mark_helper (void **slot, void *data)
18573 {
18574 struct dwarf2_per_cu_data *per_cu;
18575
18576 per_cu = (struct dwarf2_per_cu_data *) *slot;
18577
18578 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18579 reading of the chain. As such dependencies remain valid it is not much
18580 useful to track and undo them during QUIT cleanups. */
18581 if (per_cu->cu == NULL)
18582 return 1;
18583
18584 if (per_cu->cu->mark)
18585 return 1;
18586 per_cu->cu->mark = 1;
18587
18588 if (per_cu->cu->dependencies != NULL)
18589 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18590
18591 return 1;
18592 }
18593
18594 /* Set the mark field in CU and in every other compilation unit in the
18595 cache that we must keep because we are keeping CU. */
18596
18597 static void
18598 dwarf2_mark (struct dwarf2_cu *cu)
18599 {
18600 if (cu->mark)
18601 return;
18602 cu->mark = 1;
18603 if (cu->dependencies != NULL)
18604 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18605 }
18606
18607 static void
18608 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18609 {
18610 while (per_cu)
18611 {
18612 per_cu->cu->mark = 0;
18613 per_cu = per_cu->cu->read_in_chain;
18614 }
18615 }
18616
18617 /* Trivial hash function for partial_die_info: the hash value of a DIE
18618 is its offset in .debug_info for this objfile. */
18619
18620 static hashval_t
18621 partial_die_hash (const void *item)
18622 {
18623 const struct partial_die_info *part_die = item;
18624
18625 return part_die->offset.sect_off;
18626 }
18627
18628 /* Trivial comparison function for partial_die_info structures: two DIEs
18629 are equal if they have the same offset. */
18630
18631 static int
18632 partial_die_eq (const void *item_lhs, const void *item_rhs)
18633 {
18634 const struct partial_die_info *part_die_lhs = item_lhs;
18635 const struct partial_die_info *part_die_rhs = item_rhs;
18636
18637 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18638 }
18639
18640 static struct cmd_list_element *set_dwarf2_cmdlist;
18641 static struct cmd_list_element *show_dwarf2_cmdlist;
18642
18643 static void
18644 set_dwarf2_cmd (char *args, int from_tty)
18645 {
18646 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18647 }
18648
18649 static void
18650 show_dwarf2_cmd (char *args, int from_tty)
18651 {
18652 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18653 }
18654
18655 /* Free data associated with OBJFILE, if necessary. */
18656
18657 static void
18658 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18659 {
18660 struct dwarf2_per_objfile *data = d;
18661 int ix;
18662
18663 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18664 VEC_free (dwarf2_per_cu_ptr,
18665 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18666
18667 VEC_free (dwarf2_section_info_def, data->types);
18668
18669 if (data->dwo_files)
18670 free_dwo_files (data->dwo_files, objfile);
18671
18672 if (data->dwz_file && data->dwz_file->dwz_bfd)
18673 gdb_bfd_unref (data->dwz_file->dwz_bfd);
18674 }
18675
18676 \f
18677 /* The "save gdb-index" command. */
18678
18679 /* The contents of the hash table we create when building the string
18680 table. */
18681 struct strtab_entry
18682 {
18683 offset_type offset;
18684 const char *str;
18685 };
18686
18687 /* Hash function for a strtab_entry.
18688
18689 Function is used only during write_hash_table so no index format backward
18690 compatibility is needed. */
18691
18692 static hashval_t
18693 hash_strtab_entry (const void *e)
18694 {
18695 const struct strtab_entry *entry = e;
18696 return mapped_index_string_hash (INT_MAX, entry->str);
18697 }
18698
18699 /* Equality function for a strtab_entry. */
18700
18701 static int
18702 eq_strtab_entry (const void *a, const void *b)
18703 {
18704 const struct strtab_entry *ea = a;
18705 const struct strtab_entry *eb = b;
18706 return !strcmp (ea->str, eb->str);
18707 }
18708
18709 /* Create a strtab_entry hash table. */
18710
18711 static htab_t
18712 create_strtab (void)
18713 {
18714 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18715 xfree, xcalloc, xfree);
18716 }
18717
18718 /* Add a string to the constant pool. Return the string's offset in
18719 host order. */
18720
18721 static offset_type
18722 add_string (htab_t table, struct obstack *cpool, const char *str)
18723 {
18724 void **slot;
18725 struct strtab_entry entry;
18726 struct strtab_entry *result;
18727
18728 entry.str = str;
18729 slot = htab_find_slot (table, &entry, INSERT);
18730 if (*slot)
18731 result = *slot;
18732 else
18733 {
18734 result = XNEW (struct strtab_entry);
18735 result->offset = obstack_object_size (cpool);
18736 result->str = str;
18737 obstack_grow_str0 (cpool, str);
18738 *slot = result;
18739 }
18740 return result->offset;
18741 }
18742
18743 /* An entry in the symbol table. */
18744 struct symtab_index_entry
18745 {
18746 /* The name of the symbol. */
18747 const char *name;
18748 /* The offset of the name in the constant pool. */
18749 offset_type index_offset;
18750 /* A sorted vector of the indices of all the CUs that hold an object
18751 of this name. */
18752 VEC (offset_type) *cu_indices;
18753 };
18754
18755 /* The symbol table. This is a power-of-2-sized hash table. */
18756 struct mapped_symtab
18757 {
18758 offset_type n_elements;
18759 offset_type size;
18760 struct symtab_index_entry **data;
18761 };
18762
18763 /* Hash function for a symtab_index_entry. */
18764
18765 static hashval_t
18766 hash_symtab_entry (const void *e)
18767 {
18768 const struct symtab_index_entry *entry = e;
18769 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18770 sizeof (offset_type) * VEC_length (offset_type,
18771 entry->cu_indices),
18772 0);
18773 }
18774
18775 /* Equality function for a symtab_index_entry. */
18776
18777 static int
18778 eq_symtab_entry (const void *a, const void *b)
18779 {
18780 const struct symtab_index_entry *ea = a;
18781 const struct symtab_index_entry *eb = b;
18782 int len = VEC_length (offset_type, ea->cu_indices);
18783 if (len != VEC_length (offset_type, eb->cu_indices))
18784 return 0;
18785 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18786 VEC_address (offset_type, eb->cu_indices),
18787 sizeof (offset_type) * len);
18788 }
18789
18790 /* Destroy a symtab_index_entry. */
18791
18792 static void
18793 delete_symtab_entry (void *p)
18794 {
18795 struct symtab_index_entry *entry = p;
18796 VEC_free (offset_type, entry->cu_indices);
18797 xfree (entry);
18798 }
18799
18800 /* Create a hash table holding symtab_index_entry objects. */
18801
18802 static htab_t
18803 create_symbol_hash_table (void)
18804 {
18805 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18806 delete_symtab_entry, xcalloc, xfree);
18807 }
18808
18809 /* Create a new mapped symtab object. */
18810
18811 static struct mapped_symtab *
18812 create_mapped_symtab (void)
18813 {
18814 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18815 symtab->n_elements = 0;
18816 symtab->size = 1024;
18817 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18818 return symtab;
18819 }
18820
18821 /* Destroy a mapped_symtab. */
18822
18823 static void
18824 cleanup_mapped_symtab (void *p)
18825 {
18826 struct mapped_symtab *symtab = p;
18827 /* The contents of the array are freed when the other hash table is
18828 destroyed. */
18829 xfree (symtab->data);
18830 xfree (symtab);
18831 }
18832
18833 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
18834 the slot.
18835
18836 Function is used only during write_hash_table so no index format backward
18837 compatibility is needed. */
18838
18839 static struct symtab_index_entry **
18840 find_slot (struct mapped_symtab *symtab, const char *name)
18841 {
18842 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18843
18844 index = hash & (symtab->size - 1);
18845 step = ((hash * 17) & (symtab->size - 1)) | 1;
18846
18847 for (;;)
18848 {
18849 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18850 return &symtab->data[index];
18851 index = (index + step) & (symtab->size - 1);
18852 }
18853 }
18854
18855 /* Expand SYMTAB's hash table. */
18856
18857 static void
18858 hash_expand (struct mapped_symtab *symtab)
18859 {
18860 offset_type old_size = symtab->size;
18861 offset_type i;
18862 struct symtab_index_entry **old_entries = symtab->data;
18863
18864 symtab->size *= 2;
18865 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18866
18867 for (i = 0; i < old_size; ++i)
18868 {
18869 if (old_entries[i])
18870 {
18871 struct symtab_index_entry **slot = find_slot (symtab,
18872 old_entries[i]->name);
18873 *slot = old_entries[i];
18874 }
18875 }
18876
18877 xfree (old_entries);
18878 }
18879
18880 /* Add an entry to SYMTAB. NAME is the name of the symbol.
18881 CU_INDEX is the index of the CU in which the symbol appears.
18882 IS_STATIC is one if the symbol is static, otherwise zero (global). */
18883
18884 static void
18885 add_index_entry (struct mapped_symtab *symtab, const char *name,
18886 int is_static, gdb_index_symbol_kind kind,
18887 offset_type cu_index)
18888 {
18889 struct symtab_index_entry **slot;
18890 offset_type cu_index_and_attrs;
18891
18892 ++symtab->n_elements;
18893 if (4 * symtab->n_elements / 3 >= symtab->size)
18894 hash_expand (symtab);
18895
18896 slot = find_slot (symtab, name);
18897 if (!*slot)
18898 {
18899 *slot = XNEW (struct symtab_index_entry);
18900 (*slot)->name = name;
18901 /* index_offset is set later. */
18902 (*slot)->cu_indices = NULL;
18903 }
18904
18905 cu_index_and_attrs = 0;
18906 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18907 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18908 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18909
18910 /* We don't want to record an index value twice as we want to avoid the
18911 duplication.
18912 We process all global symbols and then all static symbols
18913 (which would allow us to avoid the duplication by only having to check
18914 the last entry pushed), but a symbol could have multiple kinds in one CU.
18915 To keep things simple we don't worry about the duplication here and
18916 sort and uniqufy the list after we've processed all symbols. */
18917 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18918 }
18919
18920 /* qsort helper routine for uniquify_cu_indices. */
18921
18922 static int
18923 offset_type_compare (const void *ap, const void *bp)
18924 {
18925 offset_type a = *(offset_type *) ap;
18926 offset_type b = *(offset_type *) bp;
18927
18928 return (a > b) - (b > a);
18929 }
18930
18931 /* Sort and remove duplicates of all symbols' cu_indices lists. */
18932
18933 static void
18934 uniquify_cu_indices (struct mapped_symtab *symtab)
18935 {
18936 int i;
18937
18938 for (i = 0; i < symtab->size; ++i)
18939 {
18940 struct symtab_index_entry *entry = symtab->data[i];
18941
18942 if (entry
18943 && entry->cu_indices != NULL)
18944 {
18945 unsigned int next_to_insert, next_to_check;
18946 offset_type last_value;
18947
18948 qsort (VEC_address (offset_type, entry->cu_indices),
18949 VEC_length (offset_type, entry->cu_indices),
18950 sizeof (offset_type), offset_type_compare);
18951
18952 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18953 next_to_insert = 1;
18954 for (next_to_check = 1;
18955 next_to_check < VEC_length (offset_type, entry->cu_indices);
18956 ++next_to_check)
18957 {
18958 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18959 != last_value)
18960 {
18961 last_value = VEC_index (offset_type, entry->cu_indices,
18962 next_to_check);
18963 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18964 last_value);
18965 ++next_to_insert;
18966 }
18967 }
18968 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18969 }
18970 }
18971 }
18972
18973 /* Add a vector of indices to the constant pool. */
18974
18975 static offset_type
18976 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18977 struct symtab_index_entry *entry)
18978 {
18979 void **slot;
18980
18981 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18982 if (!*slot)
18983 {
18984 offset_type len = VEC_length (offset_type, entry->cu_indices);
18985 offset_type val = MAYBE_SWAP (len);
18986 offset_type iter;
18987 int i;
18988
18989 *slot = entry;
18990 entry->index_offset = obstack_object_size (cpool);
18991
18992 obstack_grow (cpool, &val, sizeof (val));
18993 for (i = 0;
18994 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18995 ++i)
18996 {
18997 val = MAYBE_SWAP (iter);
18998 obstack_grow (cpool, &val, sizeof (val));
18999 }
19000 }
19001 else
19002 {
19003 struct symtab_index_entry *old_entry = *slot;
19004 entry->index_offset = old_entry->index_offset;
19005 entry = old_entry;
19006 }
19007 return entry->index_offset;
19008 }
19009
19010 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19011 constant pool entries going into the obstack CPOOL. */
19012
19013 static void
19014 write_hash_table (struct mapped_symtab *symtab,
19015 struct obstack *output, struct obstack *cpool)
19016 {
19017 offset_type i;
19018 htab_t symbol_hash_table;
19019 htab_t str_table;
19020
19021 symbol_hash_table = create_symbol_hash_table ();
19022 str_table = create_strtab ();
19023
19024 /* We add all the index vectors to the constant pool first, to
19025 ensure alignment is ok. */
19026 for (i = 0; i < symtab->size; ++i)
19027 {
19028 if (symtab->data[i])
19029 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19030 }
19031
19032 /* Now write out the hash table. */
19033 for (i = 0; i < symtab->size; ++i)
19034 {
19035 offset_type str_off, vec_off;
19036
19037 if (symtab->data[i])
19038 {
19039 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19040 vec_off = symtab->data[i]->index_offset;
19041 }
19042 else
19043 {
19044 /* While 0 is a valid constant pool index, it is not valid
19045 to have 0 for both offsets. */
19046 str_off = 0;
19047 vec_off = 0;
19048 }
19049
19050 str_off = MAYBE_SWAP (str_off);
19051 vec_off = MAYBE_SWAP (vec_off);
19052
19053 obstack_grow (output, &str_off, sizeof (str_off));
19054 obstack_grow (output, &vec_off, sizeof (vec_off));
19055 }
19056
19057 htab_delete (str_table);
19058 htab_delete (symbol_hash_table);
19059 }
19060
19061 /* Struct to map psymtab to CU index in the index file. */
19062 struct psymtab_cu_index_map
19063 {
19064 struct partial_symtab *psymtab;
19065 unsigned int cu_index;
19066 };
19067
19068 static hashval_t
19069 hash_psymtab_cu_index (const void *item)
19070 {
19071 const struct psymtab_cu_index_map *map = item;
19072
19073 return htab_hash_pointer (map->psymtab);
19074 }
19075
19076 static int
19077 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19078 {
19079 const struct psymtab_cu_index_map *lhs = item_lhs;
19080 const struct psymtab_cu_index_map *rhs = item_rhs;
19081
19082 return lhs->psymtab == rhs->psymtab;
19083 }
19084
19085 /* Helper struct for building the address table. */
19086 struct addrmap_index_data
19087 {
19088 struct objfile *objfile;
19089 struct obstack *addr_obstack;
19090 htab_t cu_index_htab;
19091
19092 /* Non-zero if the previous_* fields are valid.
19093 We can't write an entry until we see the next entry (since it is only then
19094 that we know the end of the entry). */
19095 int previous_valid;
19096 /* Index of the CU in the table of all CUs in the index file. */
19097 unsigned int previous_cu_index;
19098 /* Start address of the CU. */
19099 CORE_ADDR previous_cu_start;
19100 };
19101
19102 /* Write an address entry to OBSTACK. */
19103
19104 static void
19105 add_address_entry (struct objfile *objfile, struct obstack *obstack,
19106 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19107 {
19108 offset_type cu_index_to_write;
19109 char addr[8];
19110 CORE_ADDR baseaddr;
19111
19112 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19113
19114 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19115 obstack_grow (obstack, addr, 8);
19116 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19117 obstack_grow (obstack, addr, 8);
19118 cu_index_to_write = MAYBE_SWAP (cu_index);
19119 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19120 }
19121
19122 /* Worker function for traversing an addrmap to build the address table. */
19123
19124 static int
19125 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19126 {
19127 struct addrmap_index_data *data = datap;
19128 struct partial_symtab *pst = obj;
19129
19130 if (data->previous_valid)
19131 add_address_entry (data->objfile, data->addr_obstack,
19132 data->previous_cu_start, start_addr,
19133 data->previous_cu_index);
19134
19135 data->previous_cu_start = start_addr;
19136 if (pst != NULL)
19137 {
19138 struct psymtab_cu_index_map find_map, *map;
19139 find_map.psymtab = pst;
19140 map = htab_find (data->cu_index_htab, &find_map);
19141 gdb_assert (map != NULL);
19142 data->previous_cu_index = map->cu_index;
19143 data->previous_valid = 1;
19144 }
19145 else
19146 data->previous_valid = 0;
19147
19148 return 0;
19149 }
19150
19151 /* Write OBJFILE's address map to OBSTACK.
19152 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19153 in the index file. */
19154
19155 static void
19156 write_address_map (struct objfile *objfile, struct obstack *obstack,
19157 htab_t cu_index_htab)
19158 {
19159 struct addrmap_index_data addrmap_index_data;
19160
19161 /* When writing the address table, we have to cope with the fact that
19162 the addrmap iterator only provides the start of a region; we have to
19163 wait until the next invocation to get the start of the next region. */
19164
19165 addrmap_index_data.objfile = objfile;
19166 addrmap_index_data.addr_obstack = obstack;
19167 addrmap_index_data.cu_index_htab = cu_index_htab;
19168 addrmap_index_data.previous_valid = 0;
19169
19170 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
19171 &addrmap_index_data);
19172
19173 /* It's highly unlikely the last entry (end address = 0xff...ff)
19174 is valid, but we should still handle it.
19175 The end address is recorded as the start of the next region, but that
19176 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
19177 anyway. */
19178 if (addrmap_index_data.previous_valid)
19179 add_address_entry (objfile, obstack,
19180 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
19181 addrmap_index_data.previous_cu_index);
19182 }
19183
19184 /* Return the symbol kind of PSYM. */
19185
19186 static gdb_index_symbol_kind
19187 symbol_kind (struct partial_symbol *psym)
19188 {
19189 domain_enum domain = PSYMBOL_DOMAIN (psym);
19190 enum address_class aclass = PSYMBOL_CLASS (psym);
19191
19192 switch (domain)
19193 {
19194 case VAR_DOMAIN:
19195 switch (aclass)
19196 {
19197 case LOC_BLOCK:
19198 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
19199 case LOC_TYPEDEF:
19200 return GDB_INDEX_SYMBOL_KIND_TYPE;
19201 case LOC_COMPUTED:
19202 case LOC_CONST_BYTES:
19203 case LOC_OPTIMIZED_OUT:
19204 case LOC_STATIC:
19205 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19206 case LOC_CONST:
19207 /* Note: It's currently impossible to recognize psyms as enum values
19208 short of reading the type info. For now punt. */
19209 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19210 default:
19211 /* There are other LOC_FOO values that one might want to classify
19212 as variables, but dwarf2read.c doesn't currently use them. */
19213 return GDB_INDEX_SYMBOL_KIND_OTHER;
19214 }
19215 case STRUCT_DOMAIN:
19216 return GDB_INDEX_SYMBOL_KIND_TYPE;
19217 default:
19218 return GDB_INDEX_SYMBOL_KIND_OTHER;
19219 }
19220 }
19221
19222 /* Add a list of partial symbols to SYMTAB. */
19223
19224 static void
19225 write_psymbols (struct mapped_symtab *symtab,
19226 htab_t psyms_seen,
19227 struct partial_symbol **psymp,
19228 int count,
19229 offset_type cu_index,
19230 int is_static)
19231 {
19232 for (; count-- > 0; ++psymp)
19233 {
19234 struct partial_symbol *psym = *psymp;
19235 void **slot;
19236
19237 if (SYMBOL_LANGUAGE (psym) == language_ada)
19238 error (_("Ada is not currently supported by the index"));
19239
19240 /* Only add a given psymbol once. */
19241 slot = htab_find_slot (psyms_seen, psym, INSERT);
19242 if (!*slot)
19243 {
19244 gdb_index_symbol_kind kind = symbol_kind (psym);
19245
19246 *slot = psym;
19247 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
19248 is_static, kind, cu_index);
19249 }
19250 }
19251 }
19252
19253 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
19254 exception if there is an error. */
19255
19256 static void
19257 write_obstack (FILE *file, struct obstack *obstack)
19258 {
19259 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
19260 file)
19261 != obstack_object_size (obstack))
19262 error (_("couldn't data write to file"));
19263 }
19264
19265 /* Unlink a file if the argument is not NULL. */
19266
19267 static void
19268 unlink_if_set (void *p)
19269 {
19270 char **filename = p;
19271 if (*filename)
19272 unlink (*filename);
19273 }
19274
19275 /* A helper struct used when iterating over debug_types. */
19276 struct signatured_type_index_data
19277 {
19278 struct objfile *objfile;
19279 struct mapped_symtab *symtab;
19280 struct obstack *types_list;
19281 htab_t psyms_seen;
19282 int cu_index;
19283 };
19284
19285 /* A helper function that writes a single signatured_type to an
19286 obstack. */
19287
19288 static int
19289 write_one_signatured_type (void **slot, void *d)
19290 {
19291 struct signatured_type_index_data *info = d;
19292 struct signatured_type *entry = (struct signatured_type *) *slot;
19293 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19294 struct partial_symtab *psymtab = per_cu->v.psymtab;
19295 gdb_byte val[8];
19296
19297 write_psymbols (info->symtab,
19298 info->psyms_seen,
19299 info->objfile->global_psymbols.list
19300 + psymtab->globals_offset,
19301 psymtab->n_global_syms, info->cu_index,
19302 0);
19303 write_psymbols (info->symtab,
19304 info->psyms_seen,
19305 info->objfile->static_psymbols.list
19306 + psymtab->statics_offset,
19307 psymtab->n_static_syms, info->cu_index,
19308 1);
19309
19310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19311 entry->per_cu.offset.sect_off);
19312 obstack_grow (info->types_list, val, 8);
19313 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19314 entry->type_offset_in_tu.cu_off);
19315 obstack_grow (info->types_list, val, 8);
19316 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19317 obstack_grow (info->types_list, val, 8);
19318
19319 ++info->cu_index;
19320
19321 return 1;
19322 }
19323
19324 /* Recurse into all "included" dependencies and write their symbols as
19325 if they appeared in this psymtab. */
19326
19327 static void
19328 recursively_write_psymbols (struct objfile *objfile,
19329 struct partial_symtab *psymtab,
19330 struct mapped_symtab *symtab,
19331 htab_t psyms_seen,
19332 offset_type cu_index)
19333 {
19334 int i;
19335
19336 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19337 if (psymtab->dependencies[i]->user != NULL)
19338 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19339 symtab, psyms_seen, cu_index);
19340
19341 write_psymbols (symtab,
19342 psyms_seen,
19343 objfile->global_psymbols.list + psymtab->globals_offset,
19344 psymtab->n_global_syms, cu_index,
19345 0);
19346 write_psymbols (symtab,
19347 psyms_seen,
19348 objfile->static_psymbols.list + psymtab->statics_offset,
19349 psymtab->n_static_syms, cu_index,
19350 1);
19351 }
19352
19353 /* Create an index file for OBJFILE in the directory DIR. */
19354
19355 static void
19356 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19357 {
19358 struct cleanup *cleanup;
19359 char *filename, *cleanup_filename;
19360 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19361 struct obstack cu_list, types_cu_list;
19362 int i;
19363 FILE *out_file;
19364 struct mapped_symtab *symtab;
19365 offset_type val, size_of_contents, total_len;
19366 struct stat st;
19367 htab_t psyms_seen;
19368 htab_t cu_index_htab;
19369 struct psymtab_cu_index_map *psymtab_cu_index_map;
19370
19371 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
19372 return;
19373
19374 if (dwarf2_per_objfile->using_index)
19375 error (_("Cannot use an index to create the index"));
19376
19377 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19378 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19379
19380 if (stat (objfile->name, &st) < 0)
19381 perror_with_name (objfile->name);
19382
19383 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19384 INDEX_SUFFIX, (char *) NULL);
19385 cleanup = make_cleanup (xfree, filename);
19386
19387 out_file = fopen (filename, "wb");
19388 if (!out_file)
19389 error (_("Can't open `%s' for writing"), filename);
19390
19391 cleanup_filename = filename;
19392 make_cleanup (unlink_if_set, &cleanup_filename);
19393
19394 symtab = create_mapped_symtab ();
19395 make_cleanup (cleanup_mapped_symtab, symtab);
19396
19397 obstack_init (&addr_obstack);
19398 make_cleanup_obstack_free (&addr_obstack);
19399
19400 obstack_init (&cu_list);
19401 make_cleanup_obstack_free (&cu_list);
19402
19403 obstack_init (&types_cu_list);
19404 make_cleanup_obstack_free (&types_cu_list);
19405
19406 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19407 NULL, xcalloc, xfree);
19408 make_cleanup_htab_delete (psyms_seen);
19409
19410 /* While we're scanning CU's create a table that maps a psymtab pointer
19411 (which is what addrmap records) to its index (which is what is recorded
19412 in the index file). This will later be needed to write the address
19413 table. */
19414 cu_index_htab = htab_create_alloc (100,
19415 hash_psymtab_cu_index,
19416 eq_psymtab_cu_index,
19417 NULL, xcalloc, xfree);
19418 make_cleanup_htab_delete (cu_index_htab);
19419 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19420 xmalloc (sizeof (struct psymtab_cu_index_map)
19421 * dwarf2_per_objfile->n_comp_units);
19422 make_cleanup (xfree, psymtab_cu_index_map);
19423
19424 /* The CU list is already sorted, so we don't need to do additional
19425 work here. Also, the debug_types entries do not appear in
19426 all_comp_units, but only in their own hash table. */
19427 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19428 {
19429 struct dwarf2_per_cu_data *per_cu
19430 = dwarf2_per_objfile->all_comp_units[i];
19431 struct partial_symtab *psymtab = per_cu->v.psymtab;
19432 gdb_byte val[8];
19433 struct psymtab_cu_index_map *map;
19434 void **slot;
19435
19436 if (psymtab->user == NULL)
19437 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
19438
19439 map = &psymtab_cu_index_map[i];
19440 map->psymtab = psymtab;
19441 map->cu_index = i;
19442 slot = htab_find_slot (cu_index_htab, map, INSERT);
19443 gdb_assert (slot != NULL);
19444 gdb_assert (*slot == NULL);
19445 *slot = map;
19446
19447 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19448 per_cu->offset.sect_off);
19449 obstack_grow (&cu_list, val, 8);
19450 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
19451 obstack_grow (&cu_list, val, 8);
19452 }
19453
19454 /* Dump the address map. */
19455 write_address_map (objfile, &addr_obstack, cu_index_htab);
19456
19457 /* Write out the .debug_type entries, if any. */
19458 if (dwarf2_per_objfile->signatured_types)
19459 {
19460 struct signatured_type_index_data sig_data;
19461
19462 sig_data.objfile = objfile;
19463 sig_data.symtab = symtab;
19464 sig_data.types_list = &types_cu_list;
19465 sig_data.psyms_seen = psyms_seen;
19466 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19467 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19468 write_one_signatured_type, &sig_data);
19469 }
19470
19471 /* Now that we've processed all symbols we can shrink their cu_indices
19472 lists. */
19473 uniquify_cu_indices (symtab);
19474
19475 obstack_init (&constant_pool);
19476 make_cleanup_obstack_free (&constant_pool);
19477 obstack_init (&symtab_obstack);
19478 make_cleanup_obstack_free (&symtab_obstack);
19479 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19480
19481 obstack_init (&contents);
19482 make_cleanup_obstack_free (&contents);
19483 size_of_contents = 6 * sizeof (offset_type);
19484 total_len = size_of_contents;
19485
19486 /* The version number. */
19487 val = MAYBE_SWAP (7);
19488 obstack_grow (&contents, &val, sizeof (val));
19489
19490 /* The offset of the CU list from the start of the file. */
19491 val = MAYBE_SWAP (total_len);
19492 obstack_grow (&contents, &val, sizeof (val));
19493 total_len += obstack_object_size (&cu_list);
19494
19495 /* The offset of the types CU list from the start of the file. */
19496 val = MAYBE_SWAP (total_len);
19497 obstack_grow (&contents, &val, sizeof (val));
19498 total_len += obstack_object_size (&types_cu_list);
19499
19500 /* The offset of the address table from the start of the file. */
19501 val = MAYBE_SWAP (total_len);
19502 obstack_grow (&contents, &val, sizeof (val));
19503 total_len += obstack_object_size (&addr_obstack);
19504
19505 /* The offset of the symbol table from the start of the file. */
19506 val = MAYBE_SWAP (total_len);
19507 obstack_grow (&contents, &val, sizeof (val));
19508 total_len += obstack_object_size (&symtab_obstack);
19509
19510 /* The offset of the constant pool from the start of the file. */
19511 val = MAYBE_SWAP (total_len);
19512 obstack_grow (&contents, &val, sizeof (val));
19513 total_len += obstack_object_size (&constant_pool);
19514
19515 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19516
19517 write_obstack (out_file, &contents);
19518 write_obstack (out_file, &cu_list);
19519 write_obstack (out_file, &types_cu_list);
19520 write_obstack (out_file, &addr_obstack);
19521 write_obstack (out_file, &symtab_obstack);
19522 write_obstack (out_file, &constant_pool);
19523
19524 fclose (out_file);
19525
19526 /* We want to keep the file, so we set cleanup_filename to NULL
19527 here. See unlink_if_set. */
19528 cleanup_filename = NULL;
19529
19530 do_cleanups (cleanup);
19531 }
19532
19533 /* Implementation of the `save gdb-index' command.
19534
19535 Note that the file format used by this command is documented in the
19536 GDB manual. Any changes here must be documented there. */
19537
19538 static void
19539 save_gdb_index_command (char *arg, int from_tty)
19540 {
19541 struct objfile *objfile;
19542
19543 if (!arg || !*arg)
19544 error (_("usage: save gdb-index DIRECTORY"));
19545
19546 ALL_OBJFILES (objfile)
19547 {
19548 struct stat st;
19549
19550 /* If the objfile does not correspond to an actual file, skip it. */
19551 if (stat (objfile->name, &st) < 0)
19552 continue;
19553
19554 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19555 if (dwarf2_per_objfile)
19556 {
19557 volatile struct gdb_exception except;
19558
19559 TRY_CATCH (except, RETURN_MASK_ERROR)
19560 {
19561 write_psymtabs_to_index (objfile, arg);
19562 }
19563 if (except.reason < 0)
19564 exception_fprintf (gdb_stderr, except,
19565 _("Error while writing index for `%s': "),
19566 objfile->name);
19567 }
19568 }
19569 }
19570
19571 \f
19572
19573 int dwarf2_always_disassemble;
19574
19575 static void
19576 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19577 struct cmd_list_element *c, const char *value)
19578 {
19579 fprintf_filtered (file,
19580 _("Whether to always disassemble "
19581 "DWARF expressions is %s.\n"),
19582 value);
19583 }
19584
19585 static void
19586 show_check_physname (struct ui_file *file, int from_tty,
19587 struct cmd_list_element *c, const char *value)
19588 {
19589 fprintf_filtered (file,
19590 _("Whether to check \"physname\" is %s.\n"),
19591 value);
19592 }
19593
19594 void _initialize_dwarf2_read (void);
19595
19596 void
19597 _initialize_dwarf2_read (void)
19598 {
19599 struct cmd_list_element *c;
19600
19601 dwarf2_objfile_data_key
19602 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19603
19604 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19605 Set DWARF 2 specific variables.\n\
19606 Configure DWARF 2 variables such as the cache size"),
19607 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19608 0/*allow-unknown*/, &maintenance_set_cmdlist);
19609
19610 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19611 Show DWARF 2 specific variables\n\
19612 Show DWARF 2 variables such as the cache size"),
19613 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19614 0/*allow-unknown*/, &maintenance_show_cmdlist);
19615
19616 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19617 &dwarf2_max_cache_age, _("\
19618 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19619 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19620 A higher limit means that cached compilation units will be stored\n\
19621 in memory longer, and more total memory will be used. Zero disables\n\
19622 caching, which can slow down startup."),
19623 NULL,
19624 show_dwarf2_max_cache_age,
19625 &set_dwarf2_cmdlist,
19626 &show_dwarf2_cmdlist);
19627
19628 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19629 &dwarf2_always_disassemble, _("\
19630 Set whether `info address' always disassembles DWARF expressions."), _("\
19631 Show whether `info address' always disassembles DWARF expressions."), _("\
19632 When enabled, DWARF expressions are always printed in an assembly-like\n\
19633 syntax. When disabled, expressions will be printed in a more\n\
19634 conversational style, when possible."),
19635 NULL,
19636 show_dwarf2_always_disassemble,
19637 &set_dwarf2_cmdlist,
19638 &show_dwarf2_cmdlist);
19639
19640 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19641 Set debugging of the dwarf2 reader."), _("\
19642 Show debugging of the dwarf2 reader."), _("\
19643 When enabled, debugging messages are printed during dwarf2 reading\n\
19644 and symtab expansion."),
19645 NULL,
19646 NULL,
19647 &setdebuglist, &showdebuglist);
19648
19649 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19650 Set debugging of the dwarf2 DIE reader."), _("\
19651 Show debugging of the dwarf2 DIE reader."), _("\
19652 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19653 The value is the maximum depth to print."),
19654 NULL,
19655 NULL,
19656 &setdebuglist, &showdebuglist);
19657
19658 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19659 Set cross-checking of \"physname\" code against demangler."), _("\
19660 Show cross-checking of \"physname\" code against demangler."), _("\
19661 When enabled, GDB's internal \"physname\" code is checked against\n\
19662 the demangler."),
19663 NULL, show_check_physname,
19664 &setdebuglist, &showdebuglist);
19665
19666 add_setshow_boolean_cmd ("use-deprecated-index-sections",
19667 no_class, &use_deprecated_index_sections, _("\
19668 Set whether to use deprecated gdb_index sections."), _("\
19669 Show whether to use deprecated gdb_index sections."), _("\
19670 When enabled, deprecated .gdb_index sections are used anyway.\n\
19671 Normally they are ignored either because of a missing feature or\n\
19672 performance issue.\n\
19673 Warning: This option must be enabled before gdb reads the file."),
19674 NULL,
19675 NULL,
19676 &setlist, &showlist);
19677
19678 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19679 _("\
19680 Save a gdb-index file.\n\
19681 Usage: save gdb-index DIRECTORY"),
19682 &save_cmdlist);
19683 set_cmd_completer (c, filename_completer);
19684 }