gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70
71 #include <fcntl.h>
72 #include "gdb_string.h"
73 #include "gdb_assert.h"
74 #include <sys/types.h>
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 /* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81 static int dwarf2_read_debug = 0;
82
83 /* When non-zero, dump DIEs after they are read in. */
84 static unsigned int dwarf2_die_debug = 0;
85
86 /* When non-zero, cross-check physname against demangler. */
87 static int check_physname = 0;
88
89 /* When non-zero, do not reject deprecated .gdb_index sections. */
90 static int use_deprecated_index_sections = 0;
91
92 /* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96 static int processing_has_namespace_info;
97
98 static const struct objfile_data *dwarf2_objfile_data_key;
99
100 struct dwarf2_section_info
101 {
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
105 /* True if we have tried to read this section. */
106 int readin;
107 };
108
109 typedef struct dwarf2_section_info dwarf2_section_info_def;
110 DEF_VEC_O (dwarf2_section_info_def);
111
112 /* All offsets in the index are of this type. It must be
113 architecture-independent. */
114 typedef uint32_t offset_type;
115
116 DEF_VEC_I (offset_type);
117
118 /* Ensure only legit values are used. */
119 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125 /* Ensure only legit values are used. */
126 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
134 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
140 /* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142 struct mapped_index
143 {
144 /* Index data format version. */
145 int version;
146
147 /* The total length of the buffer. */
148 off_t total_size;
149
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
152
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
155
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
158
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type symbol_table_slots;
161
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164 };
165
166 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167 DEF_VEC_P (dwarf2_per_cu_ptr);
168
169 /* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
172 struct dwarf2_per_objfile
173 {
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
179 struct dwarf2_section_info macro;
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
182 struct dwarf2_section_info addr;
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
185 struct dwarf2_section_info gdb_index;
186
187 VEC (dwarf2_section_info_def) *types;
188
189 /* Back link. */
190 struct objfile *objfile;
191
192 /* Table of all the compilation units. This is used to locate
193 the target compilation unit of a particular reference. */
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
199 /* The number of .debug_types-related CUs. */
200 int n_type_units;
201
202 /* The .debug_types-related CUs (TUs). */
203 struct signatured_type **all_type_units;
204
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
215
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
251
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
254 unsigned char using_index;
255
256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
257 struct mapped_index *index_table;
258
259 /* When using index_table, this keeps track of all quick_file_names entries.
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
266 htab_t quick_file_names_table;
267
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
271
272 /* Table mapping type DIEs to their struct type *.
273 This is NULL if not allocated yet.
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
279 };
280
281 static struct dwarf2_per_objfile *dwarf2_per_objfile;
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
295 { ".debug_macro", ".zdebug_macro" },
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
299 { ".debug_addr", ".zdebug_addr" },
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
302 { ".gdb_index", ".zgdb_index" },
303 23
304 };
305
306 /* List of DWO/DWP sections. */
307
308 static const struct dwop_section_names
309 {
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
321 }
322 dwop_section_names =
323 {
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
335 };
336
337 /* local data types */
338
339 /* The data in a compilation unit header, after target2host
340 translation, looks like this. */
341 struct comp_unit_head
342 {
343 unsigned int length;
344 short version;
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
347 sect_offset abbrev_offset;
348
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
351
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
354
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
357 sect_offset offset;
358
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
361 cu_offset first_die_offset;
362 };
363
364 /* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366 struct delayed_method_info
367 {
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382 };
383
384 typedef struct delayed_method_info delayed_method_info;
385 DEF_VEC_O (delayed_method_info);
386
387 /* Internal state when decoding a particular compilation unit. */
388 struct dwarf2_cu
389 {
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
393 /* The header of the compilation unit. */
394 struct comp_unit_head header;
395
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
406 const char *producer;
407
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
423
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
446 htab_t die_hash;
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not. */
487 ULONGEST ranges_base;
488
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
496 unsigned int has_loclist : 1;
497
498 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
499 if all the producer_is_* fields are valid. This information is cached
500 because profiling CU expansion showed excessive time spent in
501 producer_is_gxx_lt_4_6. */
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
504 unsigned int producer_is_gcc_lt_4_3 : 1;
505 unsigned int producer_is_icc : 1;
506 };
507
508 /* Persistent data held for a compilation unit, even when not
509 processing it. We put a pointer to this structure in the
510 read_symtab_private field of the psymtab. */
511
512 struct dwarf2_per_cu_data
513 {
514 /* The start offset and length of this compilation unit.
515 NOTE: Unlike comp_unit_head.length, this length includes
516 initial_length_size.
517 If the DIE refers to a DWO file, this is always of the original die,
518 not the DWO file. */
519 sect_offset offset;
520 unsigned int length;
521
522 /* Flag indicating this compilation unit will be read in before
523 any of the current compilation units are processed. */
524 unsigned int queued : 1;
525
526 /* This flag will be set when reading partial DIEs if we need to load
527 absolutely all DIEs for this compilation unit, instead of just the ones
528 we think are interesting. It gets set if we look for a DIE in the
529 hash table and don't find it. */
530 unsigned int load_all_dies : 1;
531
532 /* Non-zero if this CU is from .debug_types. */
533 unsigned int is_debug_types : 1;
534
535 /* Non-zero if this CU is from the .dwz file. */
536 unsigned int is_dwz : 1;
537
538 /* The section this CU/TU lives in.
539 If the DIE refers to a DWO file, this is always the original die,
540 not the DWO file. */
541 struct dwarf2_section_info *info_or_types_section;
542
543 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
544 of the CU cache it gets reset to NULL again. */
545 struct dwarf2_cu *cu;
546
547 /* The corresponding objfile.
548 Normally we can get the objfile from dwarf2_per_objfile.
549 However we can enter this file with just a "per_cu" handle. */
550 struct objfile *objfile;
551
552 /* When using partial symbol tables, the 'psymtab' field is active.
553 Otherwise the 'quick' field is active. */
554 union
555 {
556 /* The partial symbol table associated with this compilation unit,
557 or NULL for unread partial units. */
558 struct partial_symtab *psymtab;
559
560 /* Data needed by the "quick" functions. */
561 struct dwarf2_per_cu_quick_data *quick;
562 } v;
563
564 union
565 {
566 /* The CUs we import using DW_TAG_imported_unit. This is filled in
567 while reading psymtabs, used to compute the psymtab dependencies,
568 and then cleared. Then it is filled in again while reading full
569 symbols, and only deleted when the objfile is destroyed. */
570 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
571
572 /* Type units are grouped by their DW_AT_stmt_list entry so that they
573 can share them. If this is a TU, this points to the containing
574 symtab. */
575 struct type_unit_group *type_unit_group;
576 } s;
577 };
578
579 /* Entry in the signatured_types hash table. */
580
581 struct signatured_type
582 {
583 /* The "per_cu" object of this type.
584 N.B.: This is the first member so that it's easy to convert pointers
585 between them. */
586 struct dwarf2_per_cu_data per_cu;
587
588 /* The type's signature. */
589 ULONGEST signature;
590
591 /* Offset in the TU of the type's DIE, as read from the TU header.
592 If the definition lives in a DWO file, this value is unusable. */
593 cu_offset type_offset_in_tu;
594
595 /* Offset in the section of the type's DIE.
596 If the definition lives in a DWO file, this is the offset in the
597 .debug_types.dwo section.
598 The value is zero until the actual value is known.
599 Zero is otherwise not a valid section offset. */
600 sect_offset type_offset_in_section;
601 };
602
603 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
604 This includes type_unit_group and quick_file_names. */
605
606 struct stmt_list_hash
607 {
608 /* The DWO unit this table is from or NULL if there is none. */
609 struct dwo_unit *dwo_unit;
610
611 /* Offset in .debug_line or .debug_line.dwo. */
612 sect_offset line_offset;
613 };
614
615 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
616 an object of this type. */
617
618 struct type_unit_group
619 {
620 /* dwarf2read.c's main "handle" on the symtab.
621 To simplify things we create an artificial CU that "includes" all the
622 type units using this stmt_list so that the rest of the code still has
623 a "per_cu" handle on the symtab.
624 This PER_CU is recognized by having no section. */
625 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
626 struct dwarf2_per_cu_data per_cu;
627
628 union
629 {
630 /* The TUs that share this DW_AT_stmt_list entry.
631 This is added to while parsing type units to build partial symtabs,
632 and is deleted afterwards and not used again. */
633 VEC (dwarf2_per_cu_ptr) *tus;
634
635 /* When reading the line table in "quick" functions, we need a real TU.
636 Any will do, we know they all share the same DW_AT_stmt_list entry.
637 For simplicity's sake, we pick the first one. */
638 struct dwarf2_per_cu_data *first_tu;
639 } t;
640
641 /* The primary symtab.
642 Type units in a group needn't all be defined in the same source file,
643 so we create an essentially anonymous symtab as the primary symtab. */
644 struct symtab *primary_symtab;
645
646 /* The data used to construct the hash key. */
647 struct stmt_list_hash hash;
648
649 /* The number of symtabs from the line header.
650 The value here must match line_header.num_file_names. */
651 unsigned int num_symtabs;
652
653 /* The symbol tables for this TU (obtained from the files listed in
654 DW_AT_stmt_list).
655 WARNING: The order of entries here must match the order of entries
656 in the line header. After the first TU using this type_unit_group, the
657 line header for the subsequent TUs is recreated from this. This is done
658 because we need to use the same symtabs for each TU using the same
659 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
660 there's no guarantee the line header doesn't have duplicate entries. */
661 struct symtab **symtabs;
662 };
663
664 /* These sections are what may appear in a DWO file. */
665
666 struct dwo_sections
667 {
668 struct dwarf2_section_info abbrev;
669 struct dwarf2_section_info line;
670 struct dwarf2_section_info loc;
671 struct dwarf2_section_info macinfo;
672 struct dwarf2_section_info macro;
673 struct dwarf2_section_info str;
674 struct dwarf2_section_info str_offsets;
675 /* In the case of a virtual DWO file, these two are unused. */
676 struct dwarf2_section_info info;
677 VEC (dwarf2_section_info_def) *types;
678 };
679
680 /* Common bits of DWO CUs/TUs. */
681
682 struct dwo_unit
683 {
684 /* Backlink to the containing struct dwo_file. */
685 struct dwo_file *dwo_file;
686
687 /* The "id" that distinguishes this CU/TU.
688 .debug_info calls this "dwo_id", .debug_types calls this "signature".
689 Since signatures came first, we stick with it for consistency. */
690 ULONGEST signature;
691
692 /* The section this CU/TU lives in, in the DWO file. */
693 struct dwarf2_section_info *info_or_types_section;
694
695 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
696 sect_offset offset;
697 unsigned int length;
698
699 /* For types, offset in the type's DIE of the type defined by this TU. */
700 cu_offset type_offset_in_tu;
701 };
702
703 /* Data for one DWO file.
704 This includes virtual DWO files that have been packaged into a
705 DWP file. */
706
707 struct dwo_file
708 {
709 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
710 For virtual DWO files the name is constructed from the section offsets
711 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
712 from related CU+TUs. */
713 const char *name;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 bfd *dbfd;
718
719 /* Section info for this file. */
720 struct dwo_sections sections;
721
722 /* Table of CUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_t tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
738 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
739 by section number. We don't need to record them here. */
740 };
741
742 /* These sections are what may appear in a virtual DWO file. */
743
744 struct virtual_dwo_sections
745 {
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info line;
748 struct dwarf2_section_info loc;
749 struct dwarf2_section_info macinfo;
750 struct dwarf2_section_info macro;
751 struct dwarf2_section_info str_offsets;
752 /* Each DWP hash table entry records one CU or one TU.
753 That is recorded here, and copied to dwo_unit.info_or_types_section. */
754 struct dwarf2_section_info info_or_types;
755 };
756
757 /* Contents of DWP hash tables. */
758
759 struct dwp_hash_table
760 {
761 uint32_t nr_units, nr_slots;
762 const gdb_byte *hash_table, *unit_table, *section_pool;
763 };
764
765 /* Data for one DWP file. */
766
767 struct dwp_file
768 {
769 /* Name of the file. */
770 const char *name;
771
772 /* The bfd, when the file is open. Otherwise this is NULL. */
773 bfd *dbfd;
774
775 /* Section info for this file. */
776 struct dwp_sections sections;
777
778 /* Table of CUs in the file. */
779 const struct dwp_hash_table *cus;
780
781 /* Table of TUs in the file. */
782 const struct dwp_hash_table *tus;
783
784 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
785 htab_t loaded_cutus;
786
787 /* Table to map ELF section numbers to their sections. */
788 unsigned int num_sections;
789 asection **elf_sections;
790 };
791
792 /* This represents a '.dwz' file. */
793
794 struct dwz_file
795 {
796 /* A dwz file can only contain a few sections. */
797 struct dwarf2_section_info abbrev;
798 struct dwarf2_section_info info;
799 struct dwarf2_section_info str;
800 struct dwarf2_section_info line;
801 struct dwarf2_section_info macro;
802 struct dwarf2_section_info gdb_index;
803
804 /* The dwz's BFD. */
805 bfd *dwz_bfd;
806 };
807
808 /* Struct used to pass misc. parameters to read_die_and_children, et
809 al. which are used for both .debug_info and .debug_types dies.
810 All parameters here are unchanging for the life of the call. This
811 struct exists to abstract away the constant parameters of die reading. */
812
813 struct die_reader_specs
814 {
815 /* die_section->asection->owner. */
816 bfd* abfd;
817
818 /* The CU of the DIE we are parsing. */
819 struct dwarf2_cu *cu;
820
821 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
822 struct dwo_file *dwo_file;
823
824 /* The section the die comes from.
825 This is either .debug_info or .debug_types, or the .dwo variants. */
826 struct dwarf2_section_info *die_section;
827
828 /* die_section->buffer. */
829 gdb_byte *buffer;
830
831 /* The end of the buffer. */
832 const gdb_byte *buffer_end;
833 };
834
835 /* Type of function passed to init_cutu_and_read_dies, et.al. */
836 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
837 gdb_byte *info_ptr,
838 struct die_info *comp_unit_die,
839 int has_children,
840 void *data);
841
842 /* The line number information for a compilation unit (found in the
843 .debug_line section) begins with a "statement program header",
844 which contains the following information. */
845 struct line_header
846 {
847 unsigned int total_length;
848 unsigned short version;
849 unsigned int header_length;
850 unsigned char minimum_instruction_length;
851 unsigned char maximum_ops_per_instruction;
852 unsigned char default_is_stmt;
853 int line_base;
854 unsigned char line_range;
855 unsigned char opcode_base;
856
857 /* standard_opcode_lengths[i] is the number of operands for the
858 standard opcode whose value is i. This means that
859 standard_opcode_lengths[0] is unused, and the last meaningful
860 element is standard_opcode_lengths[opcode_base - 1]. */
861 unsigned char *standard_opcode_lengths;
862
863 /* The include_directories table. NOTE! These strings are not
864 allocated with xmalloc; instead, they are pointers into
865 debug_line_buffer. If you try to free them, `free' will get
866 indigestion. */
867 unsigned int num_include_dirs, include_dirs_size;
868 char **include_dirs;
869
870 /* The file_names table. NOTE! These strings are not allocated
871 with xmalloc; instead, they are pointers into debug_line_buffer.
872 Don't try to free them directly. */
873 unsigned int num_file_names, file_names_size;
874 struct file_entry
875 {
876 char *name;
877 unsigned int dir_index;
878 unsigned int mod_time;
879 unsigned int length;
880 int included_p; /* Non-zero if referenced by the Line Number Program. */
881 struct symtab *symtab; /* The associated symbol table, if any. */
882 } *file_names;
883
884 /* The start and end of the statement program following this
885 header. These point into dwarf2_per_objfile->line_buffer. */
886 gdb_byte *statement_program_start, *statement_program_end;
887 };
888
889 /* When we construct a partial symbol table entry we only
890 need this much information. */
891 struct partial_die_info
892 {
893 /* Offset of this DIE. */
894 sect_offset offset;
895
896 /* DWARF-2 tag for this DIE. */
897 ENUM_BITFIELD(dwarf_tag) tag : 16;
898
899 /* Assorted flags describing the data found in this DIE. */
900 unsigned int has_children : 1;
901 unsigned int is_external : 1;
902 unsigned int is_declaration : 1;
903 unsigned int has_type : 1;
904 unsigned int has_specification : 1;
905 unsigned int has_pc_info : 1;
906 unsigned int may_be_inlined : 1;
907
908 /* Flag set if the SCOPE field of this structure has been
909 computed. */
910 unsigned int scope_set : 1;
911
912 /* Flag set if the DIE has a byte_size attribute. */
913 unsigned int has_byte_size : 1;
914
915 /* Flag set if any of the DIE's children are template arguments. */
916 unsigned int has_template_arguments : 1;
917
918 /* Flag set if fixup_partial_die has been called on this die. */
919 unsigned int fixup_called : 1;
920
921 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
922 unsigned int is_dwz : 1;
923
924 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
925 unsigned int spec_is_dwz : 1;
926
927 /* The name of this DIE. Normally the value of DW_AT_name, but
928 sometimes a default name for unnamed DIEs. */
929 char *name;
930
931 /* The linkage name, if present. */
932 const char *linkage_name;
933
934 /* The scope to prepend to our children. This is generally
935 allocated on the comp_unit_obstack, so will disappear
936 when this compilation unit leaves the cache. */
937 char *scope;
938
939 /* Some data associated with the partial DIE. The tag determines
940 which field is live. */
941 union
942 {
943 /* The location description associated with this DIE, if any. */
944 struct dwarf_block *locdesc;
945 /* The offset of an import, for DW_TAG_imported_unit. */
946 sect_offset offset;
947 } d;
948
949 /* If HAS_PC_INFO, the PC range associated with this DIE. */
950 CORE_ADDR lowpc;
951 CORE_ADDR highpc;
952
953 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
954 DW_AT_sibling, if any. */
955 /* NOTE: This member isn't strictly necessary, read_partial_die could
956 return DW_AT_sibling values to its caller load_partial_dies. */
957 gdb_byte *sibling;
958
959 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
960 DW_AT_specification (or DW_AT_abstract_origin or
961 DW_AT_extension). */
962 sect_offset spec_offset;
963
964 /* Pointers to this DIE's parent, first child, and next sibling,
965 if any. */
966 struct partial_die_info *die_parent, *die_child, *die_sibling;
967 };
968
969 /* This data structure holds the information of an abbrev. */
970 struct abbrev_info
971 {
972 unsigned int number; /* number identifying abbrev */
973 enum dwarf_tag tag; /* dwarf tag */
974 unsigned short has_children; /* boolean */
975 unsigned short num_attrs; /* number of attributes */
976 struct attr_abbrev *attrs; /* an array of attribute descriptions */
977 struct abbrev_info *next; /* next in chain */
978 };
979
980 struct attr_abbrev
981 {
982 ENUM_BITFIELD(dwarf_attribute) name : 16;
983 ENUM_BITFIELD(dwarf_form) form : 16;
984 };
985
986 /* Size of abbrev_table.abbrev_hash_table. */
987 #define ABBREV_HASH_SIZE 121
988
989 /* Top level data structure to contain an abbreviation table. */
990
991 struct abbrev_table
992 {
993 /* Where the abbrev table came from.
994 This is used as a sanity check when the table is used. */
995 sect_offset offset;
996
997 /* Storage for the abbrev table. */
998 struct obstack abbrev_obstack;
999
1000 /* Hash table of abbrevs.
1001 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1002 It could be statically allocated, but the previous code didn't so we
1003 don't either. */
1004 struct abbrev_info **abbrevs;
1005 };
1006
1007 /* Attributes have a name and a value. */
1008 struct attribute
1009 {
1010 ENUM_BITFIELD(dwarf_attribute) name : 16;
1011 ENUM_BITFIELD(dwarf_form) form : 15;
1012
1013 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1014 field should be in u.str (existing only for DW_STRING) but it is kept
1015 here for better struct attribute alignment. */
1016 unsigned int string_is_canonical : 1;
1017
1018 union
1019 {
1020 char *str;
1021 struct dwarf_block *blk;
1022 ULONGEST unsnd;
1023 LONGEST snd;
1024 CORE_ADDR addr;
1025 struct signatured_type *signatured_type;
1026 }
1027 u;
1028 };
1029
1030 /* This data structure holds a complete die structure. */
1031 struct die_info
1032 {
1033 /* DWARF-2 tag for this DIE. */
1034 ENUM_BITFIELD(dwarf_tag) tag : 16;
1035
1036 /* Number of attributes */
1037 unsigned char num_attrs;
1038
1039 /* True if we're presently building the full type name for the
1040 type derived from this DIE. */
1041 unsigned char building_fullname : 1;
1042
1043 /* Abbrev number */
1044 unsigned int abbrev;
1045
1046 /* Offset in .debug_info or .debug_types section. */
1047 sect_offset offset;
1048
1049 /* The dies in a compilation unit form an n-ary tree. PARENT
1050 points to this die's parent; CHILD points to the first child of
1051 this node; and all the children of a given node are chained
1052 together via their SIBLING fields. */
1053 struct die_info *child; /* Its first child, if any. */
1054 struct die_info *sibling; /* Its next sibling, if any. */
1055 struct die_info *parent; /* Its parent, if any. */
1056
1057 /* An array of attributes, with NUM_ATTRS elements. There may be
1058 zero, but it's not common and zero-sized arrays are not
1059 sufficiently portable C. */
1060 struct attribute attrs[1];
1061 };
1062
1063 /* Get at parts of an attribute structure. */
1064
1065 #define DW_STRING(attr) ((attr)->u.str)
1066 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1067 #define DW_UNSND(attr) ((attr)->u.unsnd)
1068 #define DW_BLOCK(attr) ((attr)->u.blk)
1069 #define DW_SND(attr) ((attr)->u.snd)
1070 #define DW_ADDR(attr) ((attr)->u.addr)
1071 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1072
1073 /* Blocks are a bunch of untyped bytes. */
1074 struct dwarf_block
1075 {
1076 size_t size;
1077
1078 /* Valid only if SIZE is not zero. */
1079 gdb_byte *data;
1080 };
1081
1082 #ifndef ATTR_ALLOC_CHUNK
1083 #define ATTR_ALLOC_CHUNK 4
1084 #endif
1085
1086 /* Allocate fields for structs, unions and enums in this size. */
1087 #ifndef DW_FIELD_ALLOC_CHUNK
1088 #define DW_FIELD_ALLOC_CHUNK 4
1089 #endif
1090
1091 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1092 but this would require a corresponding change in unpack_field_as_long
1093 and friends. */
1094 static int bits_per_byte = 8;
1095
1096 /* The routines that read and process dies for a C struct or C++ class
1097 pass lists of data member fields and lists of member function fields
1098 in an instance of a field_info structure, as defined below. */
1099 struct field_info
1100 {
1101 /* List of data member and baseclasses fields. */
1102 struct nextfield
1103 {
1104 struct nextfield *next;
1105 int accessibility;
1106 int virtuality;
1107 struct field field;
1108 }
1109 *fields, *baseclasses;
1110
1111 /* Number of fields (including baseclasses). */
1112 int nfields;
1113
1114 /* Number of baseclasses. */
1115 int nbaseclasses;
1116
1117 /* Set if the accesibility of one of the fields is not public. */
1118 int non_public_fields;
1119
1120 /* Member function fields array, entries are allocated in the order they
1121 are encountered in the object file. */
1122 struct nextfnfield
1123 {
1124 struct nextfnfield *next;
1125 struct fn_field fnfield;
1126 }
1127 *fnfields;
1128
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 struct fnfieldlist
1133 {
1134 char *name;
1135 int length;
1136 struct nextfnfield *head;
1137 }
1138 *fnfieldlists;
1139
1140 /* Number of entries in the fnfieldlists array. */
1141 int nfnfields;
1142
1143 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1144 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1145 struct typedef_field_list
1146 {
1147 struct typedef_field field;
1148 struct typedef_field_list *next;
1149 }
1150 *typedef_field_list;
1151 unsigned typedef_field_list_count;
1152 };
1153
1154 /* One item on the queue of compilation units to read in full symbols
1155 for. */
1156 struct dwarf2_queue_item
1157 {
1158 struct dwarf2_per_cu_data *per_cu;
1159 enum language pretend_language;
1160 struct dwarf2_queue_item *next;
1161 };
1162
1163 /* The current queue. */
1164 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1165
1166 /* Loaded secondary compilation units are kept in memory until they
1167 have not been referenced for the processing of this many
1168 compilation units. Set this to zero to disable caching. Cache
1169 sizes of up to at least twenty will improve startup time for
1170 typical inter-CU-reference binaries, at an obvious memory cost. */
1171 static int dwarf2_max_cache_age = 5;
1172 static void
1173 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1174 struct cmd_list_element *c, const char *value)
1175 {
1176 fprintf_filtered (file, _("The upper bound on the age of cached "
1177 "dwarf2 compilation units is %s.\n"),
1178 value);
1179 }
1180
1181
1182 /* Various complaints about symbol reading that don't abort the process. */
1183
1184 static void
1185 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1186 {
1187 complaint (&symfile_complaints,
1188 _("statement list doesn't fit in .debug_line section"));
1189 }
1190
1191 static void
1192 dwarf2_debug_line_missing_file_complaint (void)
1193 {
1194 complaint (&symfile_complaints,
1195 _(".debug_line section has line data without a file"));
1196 }
1197
1198 static void
1199 dwarf2_debug_line_missing_end_sequence_complaint (void)
1200 {
1201 complaint (&symfile_complaints,
1202 _(".debug_line section has line "
1203 "program sequence without an end"));
1204 }
1205
1206 static void
1207 dwarf2_complex_location_expr_complaint (void)
1208 {
1209 complaint (&symfile_complaints, _("location expression too complex"));
1210 }
1211
1212 static void
1213 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1214 int arg3)
1215 {
1216 complaint (&symfile_complaints,
1217 _("const value length mismatch for '%s', got %d, expected %d"),
1218 arg1, arg2, arg3);
1219 }
1220
1221 static void
1222 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1223 {
1224 complaint (&symfile_complaints,
1225 _("debug info runs off end of %s section"
1226 " [in module %s]"),
1227 section->asection->name,
1228 bfd_get_filename (section->asection->owner));
1229 }
1230
1231 static void
1232 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1233 {
1234 complaint (&symfile_complaints,
1235 _("macro debug info contains a "
1236 "malformed macro definition:\n`%s'"),
1237 arg1);
1238 }
1239
1240 static void
1241 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1242 {
1243 complaint (&symfile_complaints,
1244 _("invalid attribute class or form for '%s' in '%s'"),
1245 arg1, arg2);
1246 }
1247
1248 /* local function prototypes */
1249
1250 static void dwarf2_locate_sections (bfd *, asection *, void *);
1251
1252 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1253 struct objfile *);
1254
1255 static void dwarf2_find_base_address (struct die_info *die,
1256 struct dwarf2_cu *cu);
1257
1258 static void dwarf2_build_psymtabs_hard (struct objfile *);
1259
1260 static void scan_partial_symbols (struct partial_die_info *,
1261 CORE_ADDR *, CORE_ADDR *,
1262 int, struct dwarf2_cu *);
1263
1264 static void add_partial_symbol (struct partial_die_info *,
1265 struct dwarf2_cu *);
1266
1267 static void add_partial_namespace (struct partial_die_info *pdi,
1268 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1269 int need_pc, struct dwarf2_cu *cu);
1270
1271 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1272 CORE_ADDR *highpc, int need_pc,
1273 struct dwarf2_cu *cu);
1274
1275 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1276 struct dwarf2_cu *cu);
1277
1278 static void add_partial_subprogram (struct partial_die_info *pdi,
1279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1280 int need_pc, struct dwarf2_cu *cu);
1281
1282 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1283
1284 static void psymtab_to_symtab_1 (struct partial_symtab *);
1285
1286 static struct abbrev_info *abbrev_table_lookup_abbrev
1287 (const struct abbrev_table *, unsigned int);
1288
1289 static struct abbrev_table *abbrev_table_read_table
1290 (struct dwarf2_section_info *, sect_offset);
1291
1292 static void abbrev_table_free (struct abbrev_table *);
1293
1294 static void abbrev_table_free_cleanup (void *);
1295
1296 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1297 struct dwarf2_section_info *);
1298
1299 static void dwarf2_free_abbrev_table (void *);
1300
1301 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1302
1303 static struct partial_die_info *load_partial_dies
1304 (const struct die_reader_specs *, gdb_byte *, int);
1305
1306 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1307 struct partial_die_info *,
1308 struct abbrev_info *,
1309 unsigned int,
1310 gdb_byte *);
1311
1312 static struct partial_die_info *find_partial_die (sect_offset, int,
1313 struct dwarf2_cu *);
1314
1315 static void fixup_partial_die (struct partial_die_info *,
1316 struct dwarf2_cu *);
1317
1318 static gdb_byte *read_attribute (const struct die_reader_specs *,
1319 struct attribute *, struct attr_abbrev *,
1320 gdb_byte *);
1321
1322 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1323
1324 static int read_1_signed_byte (bfd *, const gdb_byte *);
1325
1326 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1327
1328 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1329
1330 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1331
1332 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1333 unsigned int *);
1334
1335 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1336
1337 static LONGEST read_checked_initial_length_and_offset
1338 (bfd *, gdb_byte *, const struct comp_unit_head *,
1339 unsigned int *, unsigned int *);
1340
1341 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1342 unsigned int *);
1343
1344 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1345
1346 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1347 sect_offset);
1348
1349 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1350
1351 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1352
1353 static char *read_indirect_string (bfd *, gdb_byte *,
1354 const struct comp_unit_head *,
1355 unsigned int *);
1356
1357 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1358
1359 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1360
1361 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1364 unsigned int *);
1365
1366 static char *read_str_index (const struct die_reader_specs *reader,
1367 struct dwarf2_cu *cu, ULONGEST str_index);
1368
1369 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1370
1371 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1372 struct dwarf2_cu *);
1373
1374 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1375 unsigned int);
1376
1377 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1378 struct dwarf2_cu *cu);
1379
1380 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1381
1382 static struct die_info *die_specification (struct die_info *die,
1383 struct dwarf2_cu **);
1384
1385 static void free_line_header (struct line_header *lh);
1386
1387 static void add_file_name (struct line_header *, char *, unsigned int,
1388 unsigned int, unsigned int);
1389
1390 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1391 struct dwarf2_cu *cu);
1392
1393 static void dwarf_decode_lines (struct line_header *, const char *,
1394 struct dwarf2_cu *, struct partial_symtab *,
1395 int);
1396
1397 static void dwarf2_start_subfile (char *, const char *, const char *);
1398
1399 static void dwarf2_start_symtab (struct dwarf2_cu *,
1400 char *, char *, CORE_ADDR);
1401
1402 static struct symbol *new_symbol (struct die_info *, struct type *,
1403 struct dwarf2_cu *);
1404
1405 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1406 struct dwarf2_cu *, struct symbol *);
1407
1408 static void dwarf2_const_value (struct attribute *, struct symbol *,
1409 struct dwarf2_cu *);
1410
1411 static void dwarf2_const_value_attr (struct attribute *attr,
1412 struct type *type,
1413 const char *name,
1414 struct obstack *obstack,
1415 struct dwarf2_cu *cu, LONGEST *value,
1416 gdb_byte **bytes,
1417 struct dwarf2_locexpr_baton **baton);
1418
1419 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1420
1421 static int need_gnat_info (struct dwarf2_cu *);
1422
1423 static struct type *die_descriptive_type (struct die_info *,
1424 struct dwarf2_cu *);
1425
1426 static void set_descriptive_type (struct type *, struct die_info *,
1427 struct dwarf2_cu *);
1428
1429 static struct type *die_containing_type (struct die_info *,
1430 struct dwarf2_cu *);
1431
1432 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1433 struct dwarf2_cu *);
1434
1435 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1436
1437 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1438
1439 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1440
1441 static char *typename_concat (struct obstack *obs, const char *prefix,
1442 const char *suffix, int physname,
1443 struct dwarf2_cu *cu);
1444
1445 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1446
1447 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1448
1449 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1450
1451 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1452
1453 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1454
1455 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1456 struct dwarf2_cu *, struct partial_symtab *);
1457
1458 static int dwarf2_get_pc_bounds (struct die_info *,
1459 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1460 struct partial_symtab *);
1461
1462 static void get_scope_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *);
1465
1466 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1467 CORE_ADDR, struct dwarf2_cu *);
1468
1469 static void dwarf2_add_field (struct field_info *, struct die_info *,
1470 struct dwarf2_cu *);
1471
1472 static void dwarf2_attach_fields_to_type (struct field_info *,
1473 struct type *, struct dwarf2_cu *);
1474
1475 static void dwarf2_add_member_fn (struct field_info *,
1476 struct die_info *, struct type *,
1477 struct dwarf2_cu *);
1478
1479 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1480 struct type *,
1481 struct dwarf2_cu *);
1482
1483 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1484
1485 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1486
1487 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1488
1489 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1490
1491 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1492
1493 static struct type *read_module_type (struct die_info *die,
1494 struct dwarf2_cu *cu);
1495
1496 static const char *namespace_name (struct die_info *die,
1497 int *is_anonymous, struct dwarf2_cu *);
1498
1499 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1500
1501 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1502
1503 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1504 struct dwarf2_cu *);
1505
1506 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1507 gdb_byte *info_ptr,
1508 gdb_byte **new_info_ptr,
1509 struct die_info *parent);
1510
1511 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1512 gdb_byte *info_ptr,
1513 gdb_byte **new_info_ptr,
1514 struct die_info *parent);
1515
1516 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1517 struct die_info **, gdb_byte *, int *, int);
1518
1519 static gdb_byte *read_full_die (const struct die_reader_specs *,
1520 struct die_info **, gdb_byte *, int *);
1521
1522 static void process_die (struct die_info *, struct dwarf2_cu *);
1523
1524 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1525 struct obstack *);
1526
1527 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1528
1529 static const char *dwarf2_full_name (char *name,
1530 struct die_info *die,
1531 struct dwarf2_cu *cu);
1532
1533 static struct die_info *dwarf2_extension (struct die_info *die,
1534 struct dwarf2_cu **);
1535
1536 static const char *dwarf_tag_name (unsigned int);
1537
1538 static const char *dwarf_attr_name (unsigned int);
1539
1540 static const char *dwarf_form_name (unsigned int);
1541
1542 static char *dwarf_bool_name (unsigned int);
1543
1544 static const char *dwarf_type_encoding_name (unsigned int);
1545
1546 static struct die_info *sibling_die (struct die_info *);
1547
1548 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1549
1550 static void dump_die_for_error (struct die_info *);
1551
1552 static void dump_die_1 (struct ui_file *, int level, int max_level,
1553 struct die_info *);
1554
1555 /*static*/ void dump_die (struct die_info *, int max_level);
1556
1557 static void store_in_ref_table (struct die_info *,
1558 struct dwarf2_cu *);
1559
1560 static int is_ref_attr (struct attribute *);
1561
1562 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1563
1564 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1565
1566 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1567 struct attribute *,
1568 struct dwarf2_cu **);
1569
1570 static struct die_info *follow_die_ref (struct die_info *,
1571 struct attribute *,
1572 struct dwarf2_cu **);
1573
1574 static struct die_info *follow_die_sig (struct die_info *,
1575 struct attribute *,
1576 struct dwarf2_cu **);
1577
1578 static struct signatured_type *lookup_signatured_type_at_offset
1579 (struct objfile *objfile,
1580 struct dwarf2_section_info *section, sect_offset offset);
1581
1582 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1583
1584 static void read_signatured_type (struct signatured_type *);
1585
1586 static struct type_unit_group *get_type_unit_group
1587 (struct dwarf2_cu *, struct attribute *);
1588
1589 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1590
1591 /* memory allocation interface */
1592
1593 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1594
1595 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1596
1597 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1598 char *, int);
1599
1600 static int attr_form_is_block (struct attribute *);
1601
1602 static int attr_form_is_section_offset (struct attribute *);
1603
1604 static int attr_form_is_constant (struct attribute *);
1605
1606 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1607 struct dwarf2_loclist_baton *baton,
1608 struct attribute *attr);
1609
1610 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1611 struct symbol *sym,
1612 struct dwarf2_cu *cu);
1613
1614 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1615 gdb_byte *info_ptr,
1616 struct abbrev_info *abbrev);
1617
1618 static void free_stack_comp_unit (void *);
1619
1620 static hashval_t partial_die_hash (const void *item);
1621
1622 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1623
1624 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1625 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1626
1627 static void init_one_comp_unit (struct dwarf2_cu *cu,
1628 struct dwarf2_per_cu_data *per_cu);
1629
1630 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1631 struct die_info *comp_unit_die,
1632 enum language pretend_language);
1633
1634 static void free_heap_comp_unit (void *);
1635
1636 static void free_cached_comp_units (void *);
1637
1638 static void age_cached_comp_units (void);
1639
1640 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *);
1644
1645 static void create_all_comp_units (struct objfile *);
1646
1647 static int create_all_type_units (struct objfile *);
1648
1649 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1650 enum language);
1651
1652 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1653 enum language);
1654
1655 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1656 enum language);
1657
1658 static void dwarf2_add_dependence (struct dwarf2_cu *,
1659 struct dwarf2_per_cu_data *);
1660
1661 static void dwarf2_mark (struct dwarf2_cu *);
1662
1663 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1664
1665 static struct type *get_die_type_at_offset (sect_offset,
1666 struct dwarf2_per_cu_data *per_cu);
1667
1668 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1669
1670 static void dwarf2_release_queue (void *dummy);
1671
1672 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
1674
1675 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1676 struct dwarf2_per_cu_data *per_cu,
1677 enum language pretend_language);
1678
1679 static void process_queue (void);
1680
1681 static void find_file_and_directory (struct die_info *die,
1682 struct dwarf2_cu *cu,
1683 char **name, char **comp_dir);
1684
1685 static char *file_full_name (int file, struct line_header *lh,
1686 const char *comp_dir);
1687
1688 static gdb_byte *read_and_check_comp_unit_head
1689 (struct comp_unit_head *header,
1690 struct dwarf2_section_info *section,
1691 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1692 int is_debug_types_section);
1693
1694 static void init_cutu_and_read_dies
1695 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1696 int use_existing_cu, int keep,
1697 die_reader_func_ftype *die_reader_func, void *data);
1698
1699 static void init_cutu_and_read_dies_simple
1700 (struct dwarf2_per_cu_data *this_cu,
1701 die_reader_func_ftype *die_reader_func, void *data);
1702
1703 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1704
1705 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1706
1707 static struct dwo_unit *lookup_dwo_comp_unit
1708 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1709
1710 static struct dwo_unit *lookup_dwo_type_unit
1711 (struct signatured_type *, const char *, const char *);
1712
1713 static void free_dwo_file_cleanup (void *);
1714
1715 static void process_cu_includes (void);
1716
1717 static void check_producer (struct dwarf2_cu *cu);
1718
1719 #if WORDS_BIGENDIAN
1720
1721 /* Convert VALUE between big- and little-endian. */
1722 static offset_type
1723 byte_swap (offset_type value)
1724 {
1725 offset_type result;
1726
1727 result = (value & 0xff) << 24;
1728 result |= (value & 0xff00) << 8;
1729 result |= (value & 0xff0000) >> 8;
1730 result |= (value & 0xff000000) >> 24;
1731 return result;
1732 }
1733
1734 #define MAYBE_SWAP(V) byte_swap (V)
1735
1736 #else
1737 #define MAYBE_SWAP(V) (V)
1738 #endif /* WORDS_BIGENDIAN */
1739
1740 /* The suffix for an index file. */
1741 #define INDEX_SUFFIX ".gdb-index"
1742
1743 static const char *dwarf2_physname (char *name, struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
1746 /* Try to locate the sections we need for DWARF 2 debugging
1747 information and return true if we have enough to do something.
1748 NAMES points to the dwarf2 section names, or is NULL if the standard
1749 ELF names are used. */
1750
1751 int
1752 dwarf2_has_info (struct objfile *objfile,
1753 const struct dwarf2_debug_sections *names)
1754 {
1755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1756 if (!dwarf2_per_objfile)
1757 {
1758 /* Initialize per-objfile state. */
1759 struct dwarf2_per_objfile *data
1760 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1761
1762 memset (data, 0, sizeof (*data));
1763 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1764 dwarf2_per_objfile = data;
1765
1766 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1767 (void *) names);
1768 dwarf2_per_objfile->objfile = objfile;
1769 }
1770 return (dwarf2_per_objfile->info.asection != NULL
1771 && dwarf2_per_objfile->abbrev.asection != NULL);
1772 }
1773
1774 /* When loading sections, we look either for uncompressed section or for
1775 compressed section names. */
1776
1777 static int
1778 section_is_p (const char *section_name,
1779 const struct dwarf2_section_names *names)
1780 {
1781 if (names->normal != NULL
1782 && strcmp (section_name, names->normal) == 0)
1783 return 1;
1784 if (names->compressed != NULL
1785 && strcmp (section_name, names->compressed) == 0)
1786 return 1;
1787 return 0;
1788 }
1789
1790 /* This function is mapped across the sections and remembers the
1791 offset and size of each of the debugging sections we are interested
1792 in. */
1793
1794 static void
1795 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1796 {
1797 const struct dwarf2_debug_sections *names;
1798 flagword aflag = bfd_get_section_flags (abfd, sectp);
1799
1800 if (vnames == NULL)
1801 names = &dwarf2_elf_names;
1802 else
1803 names = (const struct dwarf2_debug_sections *) vnames;
1804
1805 if ((aflag & SEC_HAS_CONTENTS) == 0)
1806 {
1807 }
1808 else if (section_is_p (sectp->name, &names->info))
1809 {
1810 dwarf2_per_objfile->info.asection = sectp;
1811 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1812 }
1813 else if (section_is_p (sectp->name, &names->abbrev))
1814 {
1815 dwarf2_per_objfile->abbrev.asection = sectp;
1816 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1817 }
1818 else if (section_is_p (sectp->name, &names->line))
1819 {
1820 dwarf2_per_objfile->line.asection = sectp;
1821 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1822 }
1823 else if (section_is_p (sectp->name, &names->loc))
1824 {
1825 dwarf2_per_objfile->loc.asection = sectp;
1826 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1827 }
1828 else if (section_is_p (sectp->name, &names->macinfo))
1829 {
1830 dwarf2_per_objfile->macinfo.asection = sectp;
1831 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1832 }
1833 else if (section_is_p (sectp->name, &names->macro))
1834 {
1835 dwarf2_per_objfile->macro.asection = sectp;
1836 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1837 }
1838 else if (section_is_p (sectp->name, &names->str))
1839 {
1840 dwarf2_per_objfile->str.asection = sectp;
1841 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1842 }
1843 else if (section_is_p (sectp->name, &names->addr))
1844 {
1845 dwarf2_per_objfile->addr.asection = sectp;
1846 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1847 }
1848 else if (section_is_p (sectp->name, &names->frame))
1849 {
1850 dwarf2_per_objfile->frame.asection = sectp;
1851 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1852 }
1853 else if (section_is_p (sectp->name, &names->eh_frame))
1854 {
1855 dwarf2_per_objfile->eh_frame.asection = sectp;
1856 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1857 }
1858 else if (section_is_p (sectp->name, &names->ranges))
1859 {
1860 dwarf2_per_objfile->ranges.asection = sectp;
1861 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1862 }
1863 else if (section_is_p (sectp->name, &names->types))
1864 {
1865 struct dwarf2_section_info type_section;
1866
1867 memset (&type_section, 0, sizeof (type_section));
1868 type_section.asection = sectp;
1869 type_section.size = bfd_get_section_size (sectp);
1870
1871 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1872 &type_section);
1873 }
1874 else if (section_is_p (sectp->name, &names->gdb_index))
1875 {
1876 dwarf2_per_objfile->gdb_index.asection = sectp;
1877 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1878 }
1879
1880 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1881 && bfd_section_vma (abfd, sectp) == 0)
1882 dwarf2_per_objfile->has_section_at_zero = 1;
1883 }
1884
1885 /* A helper function that decides whether a section is empty,
1886 or not present. */
1887
1888 static int
1889 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1890 {
1891 return info->asection == NULL || info->size == 0;
1892 }
1893
1894 /* Read the contents of the section INFO.
1895 OBJFILE is the main object file, but not necessarily the file where
1896 the section comes from. E.g., for DWO files INFO->asection->owner
1897 is the bfd of the DWO file.
1898 If the section is compressed, uncompress it before returning. */
1899
1900 static void
1901 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1902 {
1903 asection *sectp = info->asection;
1904 bfd *abfd;
1905 gdb_byte *buf, *retbuf;
1906 unsigned char header[4];
1907
1908 if (info->readin)
1909 return;
1910 info->buffer = NULL;
1911 info->readin = 1;
1912
1913 if (dwarf2_section_empty_p (info))
1914 return;
1915
1916 abfd = sectp->owner;
1917
1918 /* If the section has relocations, we must read it ourselves.
1919 Otherwise we attach it to the BFD. */
1920 if ((sectp->flags & SEC_RELOC) == 0)
1921 {
1922 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1923
1924 /* We have to cast away const here for historical reasons.
1925 Fixing dwarf2read to be const-correct would be quite nice. */
1926 info->buffer = (gdb_byte *) bytes;
1927 return;
1928 }
1929
1930 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1931 info->buffer = buf;
1932
1933 /* When debugging .o files, we may need to apply relocations; see
1934 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1935 We never compress sections in .o files, so we only need to
1936 try this when the section is not compressed. */
1937 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1938 if (retbuf != NULL)
1939 {
1940 info->buffer = retbuf;
1941 return;
1942 }
1943
1944 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1945 || bfd_bread (buf, info->size, abfd) != info->size)
1946 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1947 bfd_get_filename (abfd));
1948 }
1949
1950 /* A helper function that returns the size of a section in a safe way.
1951 If you are positive that the section has been read before using the
1952 size, then it is safe to refer to the dwarf2_section_info object's
1953 "size" field directly. In other cases, you must call this
1954 function, because for compressed sections the size field is not set
1955 correctly until the section has been read. */
1956
1957 static bfd_size_type
1958 dwarf2_section_size (struct objfile *objfile,
1959 struct dwarf2_section_info *info)
1960 {
1961 if (!info->readin)
1962 dwarf2_read_section (objfile, info);
1963 return info->size;
1964 }
1965
1966 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1967 SECTION_NAME. */
1968
1969 void
1970 dwarf2_get_section_info (struct objfile *objfile,
1971 enum dwarf2_section_enum sect,
1972 asection **sectp, gdb_byte **bufp,
1973 bfd_size_type *sizep)
1974 {
1975 struct dwarf2_per_objfile *data
1976 = objfile_data (objfile, dwarf2_objfile_data_key);
1977 struct dwarf2_section_info *info;
1978
1979 /* We may see an objfile without any DWARF, in which case we just
1980 return nothing. */
1981 if (data == NULL)
1982 {
1983 *sectp = NULL;
1984 *bufp = NULL;
1985 *sizep = 0;
1986 return;
1987 }
1988 switch (sect)
1989 {
1990 case DWARF2_DEBUG_FRAME:
1991 info = &data->frame;
1992 break;
1993 case DWARF2_EH_FRAME:
1994 info = &data->eh_frame;
1995 break;
1996 default:
1997 gdb_assert_not_reached ("unexpected section");
1998 }
1999
2000 dwarf2_read_section (objfile, info);
2001
2002 *sectp = info->asection;
2003 *bufp = info->buffer;
2004 *sizep = info->size;
2005 }
2006
2007 /* A helper function to find the sections for a .dwz file. */
2008
2009 static void
2010 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2011 {
2012 struct dwz_file *dwz_file = arg;
2013
2014 /* Note that we only support the standard ELF names, because .dwz
2015 is ELF-only (at the time of writing). */
2016 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2017 {
2018 dwz_file->abbrev.asection = sectp;
2019 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2022 {
2023 dwz_file->info.asection = sectp;
2024 dwz_file->info.size = bfd_get_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2027 {
2028 dwz_file->str.asection = sectp;
2029 dwz_file->str.size = bfd_get_section_size (sectp);
2030 }
2031 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2032 {
2033 dwz_file->line.asection = sectp;
2034 dwz_file->line.size = bfd_get_section_size (sectp);
2035 }
2036 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2037 {
2038 dwz_file->macro.asection = sectp;
2039 dwz_file->macro.size = bfd_get_section_size (sectp);
2040 }
2041 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2042 {
2043 dwz_file->gdb_index.asection = sectp;
2044 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2045 }
2046 }
2047
2048 /* Open the separate '.dwz' debug file, if needed. Error if the file
2049 cannot be found. */
2050
2051 static struct dwz_file *
2052 dwarf2_get_dwz_file (void)
2053 {
2054 bfd *abfd, *dwz_bfd;
2055 asection *section;
2056 gdb_byte *data;
2057 struct cleanup *cleanup;
2058 const char *filename;
2059 struct dwz_file *result;
2060
2061 if (dwarf2_per_objfile->dwz_file != NULL)
2062 return dwarf2_per_objfile->dwz_file;
2063
2064 abfd = dwarf2_per_objfile->objfile->obfd;
2065 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2066 if (section == NULL)
2067 error (_("could not find '.gnu_debugaltlink' section"));
2068 if (!bfd_malloc_and_get_section (abfd, section, &data))
2069 error (_("could not read '.gnu_debugaltlink' section: %s"),
2070 bfd_errmsg (bfd_get_error ()));
2071 cleanup = make_cleanup (xfree, data);
2072
2073 filename = data;
2074 if (!IS_ABSOLUTE_PATH (filename))
2075 {
2076 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2077 char *rel;
2078
2079 make_cleanup (xfree, abs);
2080 abs = ldirname (abs);
2081 make_cleanup (xfree, abs);
2082
2083 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2084 make_cleanup (xfree, rel);
2085 filename = rel;
2086 }
2087
2088 /* The format is just a NUL-terminated file name, followed by the
2089 build-id. For now, though, we ignore the build-id. */
2090 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2091 if (dwz_bfd == NULL)
2092 error (_("could not read '%s': %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094
2095 if (!bfd_check_format (dwz_bfd, bfd_object))
2096 {
2097 gdb_bfd_unref (dwz_bfd);
2098 error (_("file '%s' was not usable: %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100 }
2101
2102 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2103 struct dwz_file);
2104 result->dwz_bfd = dwz_bfd;
2105
2106 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2107
2108 do_cleanups (cleanup);
2109
2110 return result;
2111 }
2112 \f
2113 /* DWARF quick_symbols_functions support. */
2114
2115 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2116 unique line tables, so we maintain a separate table of all .debug_line
2117 derived entries to support the sharing.
2118 All the quick functions need is the list of file names. We discard the
2119 line_header when we're done and don't need to record it here. */
2120 struct quick_file_names
2121 {
2122 /* The data used to construct the hash key. */
2123 struct stmt_list_hash hash;
2124
2125 /* The number of entries in file_names, real_names. */
2126 unsigned int num_file_names;
2127
2128 /* The file names from the line table, after being run through
2129 file_full_name. */
2130 const char **file_names;
2131
2132 /* The file names from the line table after being run through
2133 gdb_realpath. These are computed lazily. */
2134 const char **real_names;
2135 };
2136
2137 /* When using the index (and thus not using psymtabs), each CU has an
2138 object of this type. This is used to hold information needed by
2139 the various "quick" methods. */
2140 struct dwarf2_per_cu_quick_data
2141 {
2142 /* The file table. This can be NULL if there was no file table
2143 or it's currently not read in.
2144 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2145 struct quick_file_names *file_names;
2146
2147 /* The corresponding symbol table. This is NULL if symbols for this
2148 CU have not yet been read. */
2149 struct symtab *symtab;
2150
2151 /* A temporary mark bit used when iterating over all CUs in
2152 expand_symtabs_matching. */
2153 unsigned int mark : 1;
2154
2155 /* True if we've tried to read the file table and found there isn't one.
2156 There will be no point in trying to read it again next time. */
2157 unsigned int no_file_data : 1;
2158 };
2159
2160 /* Utility hash function for a stmt_list_hash. */
2161
2162 static hashval_t
2163 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2164 {
2165 hashval_t v = 0;
2166
2167 if (stmt_list_hash->dwo_unit != NULL)
2168 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2169 v += stmt_list_hash->line_offset.sect_off;
2170 return v;
2171 }
2172
2173 /* Utility equality function for a stmt_list_hash. */
2174
2175 static int
2176 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2177 const struct stmt_list_hash *rhs)
2178 {
2179 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2180 return 0;
2181 if (lhs->dwo_unit != NULL
2182 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2183 return 0;
2184
2185 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2186 }
2187
2188 /* Hash function for a quick_file_names. */
2189
2190 static hashval_t
2191 hash_file_name_entry (const void *e)
2192 {
2193 const struct quick_file_names *file_data = e;
2194
2195 return hash_stmt_list_entry (&file_data->hash);
2196 }
2197
2198 /* Equality function for a quick_file_names. */
2199
2200 static int
2201 eq_file_name_entry (const void *a, const void *b)
2202 {
2203 const struct quick_file_names *ea = a;
2204 const struct quick_file_names *eb = b;
2205
2206 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2207 }
2208
2209 /* Delete function for a quick_file_names. */
2210
2211 static void
2212 delete_file_name_entry (void *e)
2213 {
2214 struct quick_file_names *file_data = e;
2215 int i;
2216
2217 for (i = 0; i < file_data->num_file_names; ++i)
2218 {
2219 xfree ((void*) file_data->file_names[i]);
2220 if (file_data->real_names)
2221 xfree ((void*) file_data->real_names[i]);
2222 }
2223
2224 /* The space for the struct itself lives on objfile_obstack,
2225 so we don't free it here. */
2226 }
2227
2228 /* Create a quick_file_names hash table. */
2229
2230 static htab_t
2231 create_quick_file_names_table (unsigned int nr_initial_entries)
2232 {
2233 return htab_create_alloc (nr_initial_entries,
2234 hash_file_name_entry, eq_file_name_entry,
2235 delete_file_name_entry, xcalloc, xfree);
2236 }
2237
2238 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2239 have to be created afterwards. You should call age_cached_comp_units after
2240 processing PER_CU->CU. dw2_setup must have been already called. */
2241
2242 static void
2243 load_cu (struct dwarf2_per_cu_data *per_cu)
2244 {
2245 if (per_cu->is_debug_types)
2246 load_full_type_unit (per_cu);
2247 else
2248 load_full_comp_unit (per_cu, language_minimal);
2249
2250 gdb_assert (per_cu->cu != NULL);
2251
2252 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2253 }
2254
2255 /* Read in the symbols for PER_CU. */
2256
2257 static void
2258 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2259 {
2260 struct cleanup *back_to;
2261
2262 /* Skip type_unit_groups, reading the type units they contain
2263 is handled elsewhere. */
2264 if (IS_TYPE_UNIT_GROUP (per_cu))
2265 return;
2266
2267 back_to = make_cleanup (dwarf2_release_queue, NULL);
2268
2269 if (dwarf2_per_objfile->using_index
2270 ? per_cu->v.quick->symtab == NULL
2271 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2272 {
2273 queue_comp_unit (per_cu, language_minimal);
2274 load_cu (per_cu);
2275 }
2276
2277 process_queue ();
2278
2279 /* Age the cache, releasing compilation units that have not
2280 been used recently. */
2281 age_cached_comp_units ();
2282
2283 do_cleanups (back_to);
2284 }
2285
2286 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2287 the objfile from which this CU came. Returns the resulting symbol
2288 table. */
2289
2290 static struct symtab *
2291 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2292 {
2293 gdb_assert (dwarf2_per_objfile->using_index);
2294 if (!per_cu->v.quick->symtab)
2295 {
2296 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2297 increment_reading_symtab ();
2298 dw2_do_instantiate_symtab (per_cu);
2299 process_cu_includes ();
2300 do_cleanups (back_to);
2301 }
2302 return per_cu->v.quick->symtab;
2303 }
2304
2305 /* Return the CU given its index.
2306
2307 This is intended for loops like:
2308
2309 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2310 + dwarf2_per_objfile->n_type_units); ++i)
2311 {
2312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2313
2314 ...;
2315 }
2316 */
2317
2318 static struct dwarf2_per_cu_data *
2319 dw2_get_cu (int index)
2320 {
2321 if (index >= dwarf2_per_objfile->n_comp_units)
2322 {
2323 index -= dwarf2_per_objfile->n_comp_units;
2324 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2325 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2326 }
2327
2328 return dwarf2_per_objfile->all_comp_units[index];
2329 }
2330
2331 /* Return the primary CU given its index.
2332 The difference between this function and dw2_get_cu is in the handling
2333 of type units (TUs). Here we return the type_unit_group object.
2334
2335 This is intended for loops like:
2336
2337 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2338 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2339 {
2340 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2341
2342 ...;
2343 }
2344 */
2345
2346 static struct dwarf2_per_cu_data *
2347 dw2_get_primary_cu (int index)
2348 {
2349 if (index >= dwarf2_per_objfile->n_comp_units)
2350 {
2351 index -= dwarf2_per_objfile->n_comp_units;
2352 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2353 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2354 }
2355
2356 return dwarf2_per_objfile->all_comp_units[index];
2357 }
2358
2359 /* A helper for create_cus_from_index that handles a given list of
2360 CUs. */
2361
2362 static void
2363 create_cus_from_index_list (struct objfile *objfile,
2364 const gdb_byte *cu_list, offset_type n_elements,
2365 struct dwarf2_section_info *section,
2366 int is_dwz,
2367 int base_offset)
2368 {
2369 offset_type i;
2370
2371 for (i = 0; i < n_elements; i += 2)
2372 {
2373 struct dwarf2_per_cu_data *the_cu;
2374 ULONGEST offset, length;
2375
2376 gdb_static_assert (sizeof (ULONGEST) >= 8);
2377 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2378 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2379 cu_list += 2 * 8;
2380
2381 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2382 struct dwarf2_per_cu_data);
2383 the_cu->offset.sect_off = offset;
2384 the_cu->length = length;
2385 the_cu->objfile = objfile;
2386 the_cu->info_or_types_section = section;
2387 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2388 struct dwarf2_per_cu_quick_data);
2389 the_cu->is_dwz = is_dwz;
2390 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2391 }
2392 }
2393
2394 /* Read the CU list from the mapped index, and use it to create all
2395 the CU objects for this objfile. */
2396
2397 static void
2398 create_cus_from_index (struct objfile *objfile,
2399 const gdb_byte *cu_list, offset_type cu_list_elements,
2400 const gdb_byte *dwz_list, offset_type dwz_elements)
2401 {
2402 struct dwz_file *dwz;
2403
2404 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2405 dwarf2_per_objfile->all_comp_units
2406 = obstack_alloc (&objfile->objfile_obstack,
2407 dwarf2_per_objfile->n_comp_units
2408 * sizeof (struct dwarf2_per_cu_data *));
2409
2410 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2411 &dwarf2_per_objfile->info, 0, 0);
2412
2413 if (dwz_elements == 0)
2414 return;
2415
2416 dwz = dwarf2_get_dwz_file ();
2417 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2418 cu_list_elements / 2);
2419 }
2420
2421 /* Create the signatured type hash table from the index. */
2422
2423 static void
2424 create_signatured_type_table_from_index (struct objfile *objfile,
2425 struct dwarf2_section_info *section,
2426 const gdb_byte *bytes,
2427 offset_type elements)
2428 {
2429 offset_type i;
2430 htab_t sig_types_hash;
2431
2432 dwarf2_per_objfile->n_type_units = elements / 3;
2433 dwarf2_per_objfile->all_type_units
2434 = obstack_alloc (&objfile->objfile_obstack,
2435 dwarf2_per_objfile->n_type_units
2436 * sizeof (struct signatured_type *));
2437
2438 sig_types_hash = allocate_signatured_type_table (objfile);
2439
2440 for (i = 0; i < elements; i += 3)
2441 {
2442 struct signatured_type *sig_type;
2443 ULONGEST offset, type_offset_in_tu, signature;
2444 void **slot;
2445
2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
2447 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2448 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2449 BFD_ENDIAN_LITTLE);
2450 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2451 bytes += 3 * 8;
2452
2453 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2454 struct signatured_type);
2455 sig_type->signature = signature;
2456 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2457 sig_type->per_cu.is_debug_types = 1;
2458 sig_type->per_cu.info_or_types_section = section;
2459 sig_type->per_cu.offset.sect_off = offset;
2460 sig_type->per_cu.objfile = objfile;
2461 sig_type->per_cu.v.quick
2462 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
2465 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2466 *slot = sig_type;
2467
2468 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2469 }
2470
2471 dwarf2_per_objfile->signatured_types = sig_types_hash;
2472 }
2473
2474 /* Read the address map data from the mapped index, and use it to
2475 populate the objfile's psymtabs_addrmap. */
2476
2477 static void
2478 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2479 {
2480 const gdb_byte *iter, *end;
2481 struct obstack temp_obstack;
2482 struct addrmap *mutable_map;
2483 struct cleanup *cleanup;
2484 CORE_ADDR baseaddr;
2485
2486 obstack_init (&temp_obstack);
2487 cleanup = make_cleanup_obstack_free (&temp_obstack);
2488 mutable_map = addrmap_create_mutable (&temp_obstack);
2489
2490 iter = index->address_table;
2491 end = iter + index->address_table_size;
2492
2493 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2494
2495 while (iter < end)
2496 {
2497 ULONGEST hi, lo, cu_index;
2498 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2499 iter += 8;
2500 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2501 iter += 8;
2502 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2503 iter += 4;
2504
2505 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2506 dw2_get_cu (cu_index));
2507 }
2508
2509 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2510 &objfile->objfile_obstack);
2511 do_cleanups (cleanup);
2512 }
2513
2514 /* The hash function for strings in the mapped index. This is the same as
2515 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2516 implementation. This is necessary because the hash function is tied to the
2517 format of the mapped index file. The hash values do not have to match with
2518 SYMBOL_HASH_NEXT.
2519
2520 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2521
2522 static hashval_t
2523 mapped_index_string_hash (int index_version, const void *p)
2524 {
2525 const unsigned char *str = (const unsigned char *) p;
2526 hashval_t r = 0;
2527 unsigned char c;
2528
2529 while ((c = *str++) != 0)
2530 {
2531 if (index_version >= 5)
2532 c = tolower (c);
2533 r = r * 67 + c - 113;
2534 }
2535
2536 return r;
2537 }
2538
2539 /* Find a slot in the mapped index INDEX for the object named NAME.
2540 If NAME is found, set *VEC_OUT to point to the CU vector in the
2541 constant pool and return 1. If NAME cannot be found, return 0. */
2542
2543 static int
2544 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2545 offset_type **vec_out)
2546 {
2547 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2548 offset_type hash;
2549 offset_type slot, step;
2550 int (*cmp) (const char *, const char *);
2551
2552 if (current_language->la_language == language_cplus
2553 || current_language->la_language == language_java
2554 || current_language->la_language == language_fortran)
2555 {
2556 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2557 not contain any. */
2558 const char *paren = strchr (name, '(');
2559
2560 if (paren)
2561 {
2562 char *dup;
2563
2564 dup = xmalloc (paren - name + 1);
2565 memcpy (dup, name, paren - name);
2566 dup[paren - name] = 0;
2567
2568 make_cleanup (xfree, dup);
2569 name = dup;
2570 }
2571 }
2572
2573 /* Index version 4 did not support case insensitive searches. But the
2574 indices for case insensitive languages are built in lowercase, therefore
2575 simulate our NAME being searched is also lowercased. */
2576 hash = mapped_index_string_hash ((index->version == 4
2577 && case_sensitivity == case_sensitive_off
2578 ? 5 : index->version),
2579 name);
2580
2581 slot = hash & (index->symbol_table_slots - 1);
2582 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2583 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2584
2585 for (;;)
2586 {
2587 /* Convert a slot number to an offset into the table. */
2588 offset_type i = 2 * slot;
2589 const char *str;
2590 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2591 {
2592 do_cleanups (back_to);
2593 return 0;
2594 }
2595
2596 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2597 if (!cmp (name, str))
2598 {
2599 *vec_out = (offset_type *) (index->constant_pool
2600 + MAYBE_SWAP (index->symbol_table[i + 1]));
2601 do_cleanups (back_to);
2602 return 1;
2603 }
2604
2605 slot = (slot + step) & (index->symbol_table_slots - 1);
2606 }
2607 }
2608
2609 /* A helper function that reads the .gdb_index from SECTION and fills
2610 in MAP. FILENAME is the name of the file containing the section;
2611 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2612 ok to use deprecated sections.
2613
2614 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2615 out parameters that are filled in with information about the CU and
2616 TU lists in the section.
2617
2618 Returns 1 if all went well, 0 otherwise. */
2619
2620 static int
2621 read_index_from_section (struct objfile *objfile,
2622 const char *filename,
2623 int deprecated_ok,
2624 struct dwarf2_section_info *section,
2625 struct mapped_index *map,
2626 const gdb_byte **cu_list,
2627 offset_type *cu_list_elements,
2628 const gdb_byte **types_list,
2629 offset_type *types_list_elements)
2630 {
2631 char *addr;
2632 offset_type version;
2633 offset_type *metadata;
2634 int i;
2635
2636 if (dwarf2_section_empty_p (section))
2637 return 0;
2638
2639 /* Older elfutils strip versions could keep the section in the main
2640 executable while splitting it for the separate debug info file. */
2641 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2642 return 0;
2643
2644 dwarf2_read_section (objfile, section);
2645
2646 addr = section->buffer;
2647 /* Version check. */
2648 version = MAYBE_SWAP (*(offset_type *) addr);
2649 /* Versions earlier than 3 emitted every copy of a psymbol. This
2650 causes the index to behave very poorly for certain requests. Version 3
2651 contained incomplete addrmap. So, it seems better to just ignore such
2652 indices. */
2653 if (version < 4)
2654 {
2655 static int warning_printed = 0;
2656 if (!warning_printed)
2657 {
2658 warning (_("Skipping obsolete .gdb_index section in %s."),
2659 filename);
2660 warning_printed = 1;
2661 }
2662 return 0;
2663 }
2664 /* Index version 4 uses a different hash function than index version
2665 5 and later.
2666
2667 Versions earlier than 6 did not emit psymbols for inlined
2668 functions. Using these files will cause GDB not to be able to
2669 set breakpoints on inlined functions by name, so we ignore these
2670 indices unless the user has done
2671 "set use-deprecated-index-sections on". */
2672 if (version < 6 && !deprecated_ok)
2673 {
2674 static int warning_printed = 0;
2675 if (!warning_printed)
2676 {
2677 warning (_("\
2678 Skipping deprecated .gdb_index section in %s.\n\
2679 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2680 to use the section anyway."),
2681 filename);
2682 warning_printed = 1;
2683 }
2684 return 0;
2685 }
2686 /* Indexes with higher version than the one supported by GDB may be no
2687 longer backward compatible. */
2688 if (version > 7)
2689 return 0;
2690
2691 map->version = version;
2692 map->total_size = section->size;
2693
2694 metadata = (offset_type *) (addr + sizeof (offset_type));
2695
2696 i = 0;
2697 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2698 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2699 / 8);
2700 ++i;
2701
2702 *types_list = addr + MAYBE_SWAP (metadata[i]);
2703 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2704 - MAYBE_SWAP (metadata[i]))
2705 / 8);
2706 ++i;
2707
2708 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2709 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2710 - MAYBE_SWAP (metadata[i]));
2711 ++i;
2712
2713 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2714 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2715 - MAYBE_SWAP (metadata[i]))
2716 / (2 * sizeof (offset_type)));
2717 ++i;
2718
2719 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2720
2721 return 1;
2722 }
2723
2724
2725 /* Read the index file. If everything went ok, initialize the "quick"
2726 elements of all the CUs and return 1. Otherwise, return 0. */
2727
2728 static int
2729 dwarf2_read_index (struct objfile *objfile)
2730 {
2731 struct mapped_index local_map, *map;
2732 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2733 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2734
2735 if (!read_index_from_section (objfile, objfile->name,
2736 use_deprecated_index_sections,
2737 &dwarf2_per_objfile->gdb_index, &local_map,
2738 &cu_list, &cu_list_elements,
2739 &types_list, &types_list_elements))
2740 return 0;
2741
2742 /* Don't use the index if it's empty. */
2743 if (local_map.symbol_table_slots == 0)
2744 return 0;
2745
2746 /* If there is a .dwz file, read it so we can get its CU list as
2747 well. */
2748 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2749 {
2750 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2751 struct mapped_index dwz_map;
2752 const gdb_byte *dwz_types_ignore;
2753 offset_type dwz_types_elements_ignore;
2754
2755 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2756 1,
2757 &dwz->gdb_index, &dwz_map,
2758 &dwz_list, &dwz_list_elements,
2759 &dwz_types_ignore,
2760 &dwz_types_elements_ignore))
2761 {
2762 warning (_("could not read '.gdb_index' section from %s; skipping"),
2763 bfd_get_filename (dwz->dwz_bfd));
2764 return 0;
2765 }
2766 }
2767
2768 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2769 dwz_list_elements);
2770
2771 if (types_list_elements)
2772 {
2773 struct dwarf2_section_info *section;
2774
2775 /* We can only handle a single .debug_types when we have an
2776 index. */
2777 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2778 return 0;
2779
2780 section = VEC_index (dwarf2_section_info_def,
2781 dwarf2_per_objfile->types, 0);
2782
2783 create_signatured_type_table_from_index (objfile, section, types_list,
2784 types_list_elements);
2785 }
2786
2787 create_addrmap_from_index (objfile, &local_map);
2788
2789 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2790 *map = local_map;
2791
2792 dwarf2_per_objfile->index_table = map;
2793 dwarf2_per_objfile->using_index = 1;
2794 dwarf2_per_objfile->quick_file_names_table =
2795 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2796
2797 return 1;
2798 }
2799
2800 /* A helper for the "quick" functions which sets the global
2801 dwarf2_per_objfile according to OBJFILE. */
2802
2803 static void
2804 dw2_setup (struct objfile *objfile)
2805 {
2806 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2807 gdb_assert (dwarf2_per_objfile);
2808 }
2809
2810 /* Reader function for dw2_build_type_unit_groups. */
2811
2812 static void
2813 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2814 gdb_byte *info_ptr,
2815 struct die_info *type_unit_die,
2816 int has_children,
2817 void *data)
2818 {
2819 struct dwarf2_cu *cu = reader->cu;
2820 struct attribute *attr;
2821 struct type_unit_group *tu_group;
2822
2823 gdb_assert (data == NULL);
2824
2825 if (! has_children)
2826 return;
2827
2828 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2829 /* Call this for its side-effect of creating the associated
2830 struct type_unit_group if it doesn't already exist. */
2831 tu_group = get_type_unit_group (cu, attr);
2832 }
2833
2834 /* Build dwarf2_per_objfile->type_unit_groups.
2835 This function may be called multiple times. */
2836
2837 static void
2838 dw2_build_type_unit_groups (void)
2839 {
2840 if (dwarf2_per_objfile->type_unit_groups == NULL)
2841 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2842 }
2843
2844 /* die_reader_func for dw2_get_file_names. */
2845
2846 static void
2847 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2848 gdb_byte *info_ptr,
2849 struct die_info *comp_unit_die,
2850 int has_children,
2851 void *data)
2852 {
2853 struct dwarf2_cu *cu = reader->cu;
2854 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2855 struct objfile *objfile = dwarf2_per_objfile->objfile;
2856 struct dwarf2_per_cu_data *lh_cu;
2857 struct line_header *lh;
2858 struct attribute *attr;
2859 int i;
2860 char *name, *comp_dir;
2861 void **slot;
2862 struct quick_file_names *qfn;
2863 unsigned int line_offset;
2864
2865 /* Our callers never want to match partial units -- instead they
2866 will match the enclosing full CU. */
2867 if (comp_unit_die->tag == DW_TAG_partial_unit)
2868 {
2869 this_cu->v.quick->no_file_data = 1;
2870 return;
2871 }
2872
2873 /* If we're reading the line header for TUs, store it in the "per_cu"
2874 for tu_group. */
2875 if (this_cu->is_debug_types)
2876 {
2877 struct type_unit_group *tu_group = data;
2878
2879 gdb_assert (tu_group != NULL);
2880 lh_cu = &tu_group->per_cu;
2881 }
2882 else
2883 lh_cu = this_cu;
2884
2885 lh = NULL;
2886 slot = NULL;
2887 line_offset = 0;
2888
2889 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2890 if (attr)
2891 {
2892 struct quick_file_names find_entry;
2893
2894 line_offset = DW_UNSND (attr);
2895
2896 /* We may have already read in this line header (TU line header sharing).
2897 If we have we're done. */
2898 find_entry.hash.dwo_unit = cu->dwo_unit;
2899 find_entry.hash.line_offset.sect_off = line_offset;
2900 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2901 &find_entry, INSERT);
2902 if (*slot != NULL)
2903 {
2904 lh_cu->v.quick->file_names = *slot;
2905 return;
2906 }
2907
2908 lh = dwarf_decode_line_header (line_offset, cu);
2909 }
2910 if (lh == NULL)
2911 {
2912 lh_cu->v.quick->no_file_data = 1;
2913 return;
2914 }
2915
2916 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2917 qfn->hash.dwo_unit = cu->dwo_unit;
2918 qfn->hash.line_offset.sect_off = line_offset;
2919 gdb_assert (slot != NULL);
2920 *slot = qfn;
2921
2922 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2923
2924 qfn->num_file_names = lh->num_file_names;
2925 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2926 lh->num_file_names * sizeof (char *));
2927 for (i = 0; i < lh->num_file_names; ++i)
2928 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2929 qfn->real_names = NULL;
2930
2931 free_line_header (lh);
2932
2933 lh_cu->v.quick->file_names = qfn;
2934 }
2935
2936 /* A helper for the "quick" functions which attempts to read the line
2937 table for THIS_CU. */
2938
2939 static struct quick_file_names *
2940 dw2_get_file_names (struct objfile *objfile,
2941 struct dwarf2_per_cu_data *this_cu)
2942 {
2943 /* For TUs this should only be called on the parent group. */
2944 if (this_cu->is_debug_types)
2945 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2946
2947 if (this_cu->v.quick->file_names != NULL)
2948 return this_cu->v.quick->file_names;
2949 /* If we know there is no line data, no point in looking again. */
2950 if (this_cu->v.quick->no_file_data)
2951 return NULL;
2952
2953 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2954 in the stub for CUs, there's is no need to lookup the DWO file.
2955 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2956 DWO file. */
2957 if (this_cu->is_debug_types)
2958 {
2959 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2960
2961 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2962 dw2_get_file_names_reader, tu_group);
2963 }
2964 else
2965 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2966
2967 if (this_cu->v.quick->no_file_data)
2968 return NULL;
2969 return this_cu->v.quick->file_names;
2970 }
2971
2972 /* A helper for the "quick" functions which computes and caches the
2973 real path for a given file name from the line table. */
2974
2975 static const char *
2976 dw2_get_real_path (struct objfile *objfile,
2977 struct quick_file_names *qfn, int index)
2978 {
2979 if (qfn->real_names == NULL)
2980 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2981 qfn->num_file_names, sizeof (char *));
2982
2983 if (qfn->real_names[index] == NULL)
2984 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2985
2986 return qfn->real_names[index];
2987 }
2988
2989 static struct symtab *
2990 dw2_find_last_source_symtab (struct objfile *objfile)
2991 {
2992 int index;
2993
2994 dw2_setup (objfile);
2995 index = dwarf2_per_objfile->n_comp_units - 1;
2996 return dw2_instantiate_symtab (dw2_get_cu (index));
2997 }
2998
2999 /* Traversal function for dw2_forget_cached_source_info. */
3000
3001 static int
3002 dw2_free_cached_file_names (void **slot, void *info)
3003 {
3004 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3005
3006 if (file_data->real_names)
3007 {
3008 int i;
3009
3010 for (i = 0; i < file_data->num_file_names; ++i)
3011 {
3012 xfree ((void*) file_data->real_names[i]);
3013 file_data->real_names[i] = NULL;
3014 }
3015 }
3016
3017 return 1;
3018 }
3019
3020 static void
3021 dw2_forget_cached_source_info (struct objfile *objfile)
3022 {
3023 dw2_setup (objfile);
3024
3025 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3026 dw2_free_cached_file_names, NULL);
3027 }
3028
3029 /* Helper function for dw2_map_symtabs_matching_filename that expands
3030 the symtabs and calls the iterator. */
3031
3032 static int
3033 dw2_map_expand_apply (struct objfile *objfile,
3034 struct dwarf2_per_cu_data *per_cu,
3035 const char *name,
3036 const char *full_path, const char *real_path,
3037 int (*callback) (struct symtab *, void *),
3038 void *data)
3039 {
3040 struct symtab *last_made = objfile->symtabs;
3041
3042 /* Don't visit already-expanded CUs. */
3043 if (per_cu->v.quick->symtab)
3044 return 0;
3045
3046 /* This may expand more than one symtab, and we want to iterate over
3047 all of them. */
3048 dw2_instantiate_symtab (per_cu);
3049
3050 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3051 objfile->symtabs, last_made);
3052 }
3053
3054 /* Implementation of the map_symtabs_matching_filename method. */
3055
3056 static int
3057 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3058 const char *full_path, const char *real_path,
3059 int (*callback) (struct symtab *, void *),
3060 void *data)
3061 {
3062 int i;
3063 const char *name_basename = lbasename (name);
3064 int name_len = strlen (name);
3065 int is_abs = IS_ABSOLUTE_PATH (name);
3066
3067 dw2_setup (objfile);
3068
3069 dw2_build_type_unit_groups ();
3070
3071 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3072 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3073 {
3074 int j;
3075 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3076 struct quick_file_names *file_data;
3077
3078 /* We only need to look at symtabs not already expanded. */
3079 if (per_cu->v.quick->symtab)
3080 continue;
3081
3082 file_data = dw2_get_file_names (objfile, per_cu);
3083 if (file_data == NULL)
3084 continue;
3085
3086 for (j = 0; j < file_data->num_file_names; ++j)
3087 {
3088 const char *this_name = file_data->file_names[j];
3089
3090 if (FILENAME_CMP (name, this_name) == 0
3091 || (!is_abs && compare_filenames_for_search (this_name,
3092 name, name_len)))
3093 {
3094 if (dw2_map_expand_apply (objfile, per_cu,
3095 name, full_path, real_path,
3096 callback, data))
3097 return 1;
3098 }
3099
3100 /* Before we invoke realpath, which can get expensive when many
3101 files are involved, do a quick comparison of the basenames. */
3102 if (! basenames_may_differ
3103 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3104 continue;
3105
3106 if (full_path != NULL)
3107 {
3108 const char *this_real_name = dw2_get_real_path (objfile,
3109 file_data, j);
3110
3111 if (this_real_name != NULL
3112 && (FILENAME_CMP (full_path, this_real_name) == 0
3113 || (!is_abs
3114 && compare_filenames_for_search (this_real_name,
3115 name, name_len))))
3116 {
3117 if (dw2_map_expand_apply (objfile, per_cu,
3118 name, full_path, real_path,
3119 callback, data))
3120 return 1;
3121 }
3122 }
3123
3124 if (real_path != NULL)
3125 {
3126 const char *this_real_name = dw2_get_real_path (objfile,
3127 file_data, j);
3128
3129 if (this_real_name != NULL
3130 && (FILENAME_CMP (real_path, this_real_name) == 0
3131 || (!is_abs
3132 && compare_filenames_for_search (this_real_name,
3133 name, name_len))))
3134 {
3135 if (dw2_map_expand_apply (objfile, per_cu,
3136 name, full_path, real_path,
3137 callback, data))
3138 return 1;
3139 }
3140 }
3141 }
3142 }
3143
3144 return 0;
3145 }
3146
3147 static struct symtab *
3148 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3149 const char *name, domain_enum domain)
3150 {
3151 /* We do all the work in the pre_expand_symtabs_matching hook
3152 instead. */
3153 return NULL;
3154 }
3155
3156 /* A helper function that expands all symtabs that hold an object
3157 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3158 symbols in block BLOCK_KIND. */
3159
3160 static void
3161 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3162 int want_specific_block,
3163 enum block_enum block_kind,
3164 const char *name, domain_enum domain)
3165 {
3166 struct mapped_index *index;
3167
3168 dw2_setup (objfile);
3169
3170 index = dwarf2_per_objfile->index_table;
3171
3172 /* index_table is NULL if OBJF_READNOW. */
3173 if (index)
3174 {
3175 offset_type *vec;
3176
3177 if (find_slot_in_mapped_hash (index, name, &vec))
3178 {
3179 offset_type i, len = MAYBE_SWAP (*vec);
3180 for (i = 0; i < len; ++i)
3181 {
3182 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3183 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3184 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3185 int want_static = block_kind != GLOBAL_BLOCK;
3186 /* This value is only valid for index versions >= 7. */
3187 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3188 gdb_index_symbol_kind symbol_kind =
3189 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3190 /* Only check the symbol attributes if they're present.
3191 Indices prior to version 7 don't record them,
3192 and indices >= 7 may elide them for certain symbols
3193 (gold does this). */
3194 int attrs_valid =
3195 (index->version >= 7
3196 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3197
3198 if (attrs_valid
3199 && want_specific_block
3200 && want_static != is_static)
3201 continue;
3202
3203 /* Only check the symbol's kind if it has one. */
3204 if (attrs_valid)
3205 {
3206 switch (domain)
3207 {
3208 case VAR_DOMAIN:
3209 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3210 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3211 /* Some types are also in VAR_DOMAIN. */
3212 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3213 continue;
3214 break;
3215 case STRUCT_DOMAIN:
3216 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3217 continue;
3218 break;
3219 case LABEL_DOMAIN:
3220 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3221 continue;
3222 break;
3223 default:
3224 break;
3225 }
3226 }
3227
3228 dw2_instantiate_symtab (per_cu);
3229 }
3230 }
3231 }
3232 }
3233
3234 static void
3235 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3236 enum block_enum block_kind, const char *name,
3237 domain_enum domain)
3238 {
3239 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3240 }
3241
3242 static void
3243 dw2_print_stats (struct objfile *objfile)
3244 {
3245 int i, count;
3246
3247 dw2_setup (objfile);
3248 count = 0;
3249 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3250 + dwarf2_per_objfile->n_type_units); ++i)
3251 {
3252 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3253
3254 if (!per_cu->v.quick->symtab)
3255 ++count;
3256 }
3257 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3258 }
3259
3260 static void
3261 dw2_dump (struct objfile *objfile)
3262 {
3263 /* Nothing worth printing. */
3264 }
3265
3266 static void
3267 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3268 struct section_offsets *delta)
3269 {
3270 /* There's nothing to relocate here. */
3271 }
3272
3273 static void
3274 dw2_expand_symtabs_for_function (struct objfile *objfile,
3275 const char *func_name)
3276 {
3277 /* Note: It doesn't matter what we pass for block_kind here. */
3278 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3279 VAR_DOMAIN);
3280 }
3281
3282 static void
3283 dw2_expand_all_symtabs (struct objfile *objfile)
3284 {
3285 int i;
3286
3287 dw2_setup (objfile);
3288
3289 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3290 + dwarf2_per_objfile->n_type_units); ++i)
3291 {
3292 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3293
3294 dw2_instantiate_symtab (per_cu);
3295 }
3296 }
3297
3298 static void
3299 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3300 const char *filename)
3301 {
3302 int i;
3303
3304 dw2_setup (objfile);
3305
3306 /* We don't need to consider type units here.
3307 This is only called for examining code, e.g. expand_line_sal.
3308 There can be an order of magnitude (or more) more type units
3309 than comp units, and we avoid them if we can. */
3310
3311 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3312 {
3313 int j;
3314 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3315 struct quick_file_names *file_data;
3316
3317 /* We only need to look at symtabs not already expanded. */
3318 if (per_cu->v.quick->symtab)
3319 continue;
3320
3321 file_data = dw2_get_file_names (objfile, per_cu);
3322 if (file_data == NULL)
3323 continue;
3324
3325 for (j = 0; j < file_data->num_file_names; ++j)
3326 {
3327 const char *this_name = file_data->file_names[j];
3328 if (FILENAME_CMP (this_name, filename) == 0)
3329 {
3330 dw2_instantiate_symtab (per_cu);
3331 break;
3332 }
3333 }
3334 }
3335 }
3336
3337 /* A helper function for dw2_find_symbol_file that finds the primary
3338 file name for a given CU. This is a die_reader_func. */
3339
3340 static void
3341 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3342 gdb_byte *info_ptr,
3343 struct die_info *comp_unit_die,
3344 int has_children,
3345 void *data)
3346 {
3347 const char **result_ptr = data;
3348 struct dwarf2_cu *cu = reader->cu;
3349 struct attribute *attr;
3350
3351 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3352 if (attr == NULL)
3353 *result_ptr = NULL;
3354 else
3355 *result_ptr = DW_STRING (attr);
3356 }
3357
3358 static const char *
3359 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3360 {
3361 struct dwarf2_per_cu_data *per_cu;
3362 offset_type *vec;
3363 const char *filename;
3364
3365 dw2_setup (objfile);
3366
3367 /* index_table is NULL if OBJF_READNOW. */
3368 if (!dwarf2_per_objfile->index_table)
3369 {
3370 struct symtab *s;
3371
3372 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3373 {
3374 struct blockvector *bv = BLOCKVECTOR (s);
3375 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3376 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3377
3378 if (sym)
3379 return SYMBOL_SYMTAB (sym)->filename;
3380 }
3381 return NULL;
3382 }
3383
3384 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3385 name, &vec))
3386 return NULL;
3387
3388 /* Note that this just looks at the very first one named NAME -- but
3389 actually we are looking for a function. find_main_filename
3390 should be rewritten so that it doesn't require a custom hook. It
3391 could just use the ordinary symbol tables. */
3392 /* vec[0] is the length, which must always be >0. */
3393 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3394
3395 if (per_cu->v.quick->symtab != NULL)
3396 return per_cu->v.quick->symtab->filename;
3397
3398 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3399 dw2_get_primary_filename_reader, &filename);
3400
3401 return filename;
3402 }
3403
3404 static void
3405 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3406 struct objfile *objfile, int global,
3407 int (*callback) (struct block *,
3408 struct symbol *, void *),
3409 void *data, symbol_compare_ftype *match,
3410 symbol_compare_ftype *ordered_compare)
3411 {
3412 /* Currently unimplemented; used for Ada. The function can be called if the
3413 current language is Ada for a non-Ada objfile using GNU index. As Ada
3414 does not look for non-Ada symbols this function should just return. */
3415 }
3416
3417 static void
3418 dw2_expand_symtabs_matching
3419 (struct objfile *objfile,
3420 int (*file_matcher) (const char *, void *),
3421 int (*name_matcher) (const char *, void *),
3422 enum search_domain kind,
3423 void *data)
3424 {
3425 int i;
3426 offset_type iter;
3427 struct mapped_index *index;
3428
3429 dw2_setup (objfile);
3430
3431 /* index_table is NULL if OBJF_READNOW. */
3432 if (!dwarf2_per_objfile->index_table)
3433 return;
3434 index = dwarf2_per_objfile->index_table;
3435
3436 if (file_matcher != NULL)
3437 {
3438 struct cleanup *cleanup;
3439 htab_t visited_found, visited_not_found;
3440
3441 dw2_build_type_unit_groups ();
3442
3443 visited_found = htab_create_alloc (10,
3444 htab_hash_pointer, htab_eq_pointer,
3445 NULL, xcalloc, xfree);
3446 cleanup = make_cleanup_htab_delete (visited_found);
3447 visited_not_found = htab_create_alloc (10,
3448 htab_hash_pointer, htab_eq_pointer,
3449 NULL, xcalloc, xfree);
3450 make_cleanup_htab_delete (visited_not_found);
3451
3452 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3453 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3454 {
3455 int j;
3456 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3457 struct quick_file_names *file_data;
3458 void **slot;
3459
3460 per_cu->v.quick->mark = 0;
3461
3462 /* We only need to look at symtabs not already expanded. */
3463 if (per_cu->v.quick->symtab)
3464 continue;
3465
3466 file_data = dw2_get_file_names (objfile, per_cu);
3467 if (file_data == NULL)
3468 continue;
3469
3470 if (htab_find (visited_not_found, file_data) != NULL)
3471 continue;
3472 else if (htab_find (visited_found, file_data) != NULL)
3473 {
3474 per_cu->v.quick->mark = 1;
3475 continue;
3476 }
3477
3478 for (j = 0; j < file_data->num_file_names; ++j)
3479 {
3480 if (file_matcher (file_data->file_names[j], data))
3481 {
3482 per_cu->v.quick->mark = 1;
3483 break;
3484 }
3485 }
3486
3487 slot = htab_find_slot (per_cu->v.quick->mark
3488 ? visited_found
3489 : visited_not_found,
3490 file_data, INSERT);
3491 *slot = file_data;
3492 }
3493
3494 do_cleanups (cleanup);
3495 }
3496
3497 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3498 {
3499 offset_type idx = 2 * iter;
3500 const char *name;
3501 offset_type *vec, vec_len, vec_idx;
3502
3503 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3504 continue;
3505
3506 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3507
3508 if (! (*name_matcher) (name, data))
3509 continue;
3510
3511 /* The name was matched, now expand corresponding CUs that were
3512 marked. */
3513 vec = (offset_type *) (index->constant_pool
3514 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3515 vec_len = MAYBE_SWAP (vec[0]);
3516 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3517 {
3518 struct dwarf2_per_cu_data *per_cu;
3519 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3520 gdb_index_symbol_kind symbol_kind =
3521 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3522 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3523
3524 /* Don't crash on bad data. */
3525 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3526 + dwarf2_per_objfile->n_type_units))
3527 continue;
3528
3529 /* Only check the symbol's kind if it has one.
3530 Indices prior to version 7 don't record it. */
3531 if (index->version >= 7)
3532 {
3533 switch (kind)
3534 {
3535 case VARIABLES_DOMAIN:
3536 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3537 continue;
3538 break;
3539 case FUNCTIONS_DOMAIN:
3540 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3541 continue;
3542 break;
3543 case TYPES_DOMAIN:
3544 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3545 continue;
3546 break;
3547 default:
3548 break;
3549 }
3550 }
3551
3552 per_cu = dw2_get_cu (cu_index);
3553 if (file_matcher == NULL || per_cu->v.quick->mark)
3554 dw2_instantiate_symtab (per_cu);
3555 }
3556 }
3557 }
3558
3559 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3560 symtab. */
3561
3562 static struct symtab *
3563 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3564 {
3565 int i;
3566
3567 if (BLOCKVECTOR (symtab) != NULL
3568 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3569 return symtab;
3570
3571 if (symtab->includes == NULL)
3572 return NULL;
3573
3574 for (i = 0; symtab->includes[i]; ++i)
3575 {
3576 struct symtab *s = symtab->includes[i];
3577
3578 s = recursively_find_pc_sect_symtab (s, pc);
3579 if (s != NULL)
3580 return s;
3581 }
3582
3583 return NULL;
3584 }
3585
3586 static struct symtab *
3587 dw2_find_pc_sect_symtab (struct objfile *objfile,
3588 struct minimal_symbol *msymbol,
3589 CORE_ADDR pc,
3590 struct obj_section *section,
3591 int warn_if_readin)
3592 {
3593 struct dwarf2_per_cu_data *data;
3594 struct symtab *result;
3595
3596 dw2_setup (objfile);
3597
3598 if (!objfile->psymtabs_addrmap)
3599 return NULL;
3600
3601 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3602 if (!data)
3603 return NULL;
3604
3605 if (warn_if_readin && data->v.quick->symtab)
3606 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3607 paddress (get_objfile_arch (objfile), pc));
3608
3609 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3610 gdb_assert (result != NULL);
3611 return result;
3612 }
3613
3614 static void
3615 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3616 void *data, int need_fullname)
3617 {
3618 int i;
3619 struct cleanup *cleanup;
3620 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3621 NULL, xcalloc, xfree);
3622
3623 cleanup = make_cleanup_htab_delete (visited);
3624 dw2_setup (objfile);
3625
3626 dw2_build_type_unit_groups ();
3627
3628 /* We can ignore file names coming from already-expanded CUs. */
3629 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3630 + dwarf2_per_objfile->n_type_units); ++i)
3631 {
3632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3633
3634 if (per_cu->v.quick->symtab)
3635 {
3636 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3637 INSERT);
3638
3639 *slot = per_cu->v.quick->file_names;
3640 }
3641 }
3642
3643 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3644 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3645 {
3646 int j;
3647 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3648 struct quick_file_names *file_data;
3649 void **slot;
3650
3651 /* We only need to look at symtabs not already expanded. */
3652 if (per_cu->v.quick->symtab)
3653 continue;
3654
3655 file_data = dw2_get_file_names (objfile, per_cu);
3656 if (file_data == NULL)
3657 continue;
3658
3659 slot = htab_find_slot (visited, file_data, INSERT);
3660 if (*slot)
3661 {
3662 /* Already visited. */
3663 continue;
3664 }
3665 *slot = file_data;
3666
3667 for (j = 0; j < file_data->num_file_names; ++j)
3668 {
3669 const char *this_real_name;
3670
3671 if (need_fullname)
3672 this_real_name = dw2_get_real_path (objfile, file_data, j);
3673 else
3674 this_real_name = NULL;
3675 (*fun) (file_data->file_names[j], this_real_name, data);
3676 }
3677 }
3678
3679 do_cleanups (cleanup);
3680 }
3681
3682 static int
3683 dw2_has_symbols (struct objfile *objfile)
3684 {
3685 return 1;
3686 }
3687
3688 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3689 {
3690 dw2_has_symbols,
3691 dw2_find_last_source_symtab,
3692 dw2_forget_cached_source_info,
3693 dw2_map_symtabs_matching_filename,
3694 dw2_lookup_symbol,
3695 dw2_pre_expand_symtabs_matching,
3696 dw2_print_stats,
3697 dw2_dump,
3698 dw2_relocate,
3699 dw2_expand_symtabs_for_function,
3700 dw2_expand_all_symtabs,
3701 dw2_expand_symtabs_with_filename,
3702 dw2_find_symbol_file,
3703 dw2_map_matching_symbols,
3704 dw2_expand_symtabs_matching,
3705 dw2_find_pc_sect_symtab,
3706 dw2_map_symbol_filenames
3707 };
3708
3709 /* Initialize for reading DWARF for this objfile. Return 0 if this
3710 file will use psymtabs, or 1 if using the GNU index. */
3711
3712 int
3713 dwarf2_initialize_objfile (struct objfile *objfile)
3714 {
3715 /* If we're about to read full symbols, don't bother with the
3716 indices. In this case we also don't care if some other debug
3717 format is making psymtabs, because they are all about to be
3718 expanded anyway. */
3719 if ((objfile->flags & OBJF_READNOW))
3720 {
3721 int i;
3722
3723 dwarf2_per_objfile->using_index = 1;
3724 create_all_comp_units (objfile);
3725 create_all_type_units (objfile);
3726 dwarf2_per_objfile->quick_file_names_table =
3727 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3728
3729 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3730 + dwarf2_per_objfile->n_type_units); ++i)
3731 {
3732 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3733
3734 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3735 struct dwarf2_per_cu_quick_data);
3736 }
3737
3738 /* Return 1 so that gdb sees the "quick" functions. However,
3739 these functions will be no-ops because we will have expanded
3740 all symtabs. */
3741 return 1;
3742 }
3743
3744 if (dwarf2_read_index (objfile))
3745 return 1;
3746
3747 return 0;
3748 }
3749
3750 \f
3751
3752 /* Build a partial symbol table. */
3753
3754 void
3755 dwarf2_build_psymtabs (struct objfile *objfile)
3756 {
3757 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3758 {
3759 init_psymbol_list (objfile, 1024);
3760 }
3761
3762 dwarf2_build_psymtabs_hard (objfile);
3763 }
3764
3765 /* Return the total length of the CU described by HEADER. */
3766
3767 static unsigned int
3768 get_cu_length (const struct comp_unit_head *header)
3769 {
3770 return header->initial_length_size + header->length;
3771 }
3772
3773 /* Return TRUE if OFFSET is within CU_HEADER. */
3774
3775 static inline int
3776 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3777 {
3778 sect_offset bottom = { cu_header->offset.sect_off };
3779 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3780
3781 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3782 }
3783
3784 /* Find the base address of the compilation unit for range lists and
3785 location lists. It will normally be specified by DW_AT_low_pc.
3786 In DWARF-3 draft 4, the base address could be overridden by
3787 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3788 compilation units with discontinuous ranges. */
3789
3790 static void
3791 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3792 {
3793 struct attribute *attr;
3794
3795 cu->base_known = 0;
3796 cu->base_address = 0;
3797
3798 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3799 if (attr)
3800 {
3801 cu->base_address = DW_ADDR (attr);
3802 cu->base_known = 1;
3803 }
3804 else
3805 {
3806 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3807 if (attr)
3808 {
3809 cu->base_address = DW_ADDR (attr);
3810 cu->base_known = 1;
3811 }
3812 }
3813 }
3814
3815 /* Read in the comp unit header information from the debug_info at info_ptr.
3816 NOTE: This leaves members offset, first_die_offset to be filled in
3817 by the caller. */
3818
3819 static gdb_byte *
3820 read_comp_unit_head (struct comp_unit_head *cu_header,
3821 gdb_byte *info_ptr, bfd *abfd)
3822 {
3823 int signed_addr;
3824 unsigned int bytes_read;
3825
3826 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3827 cu_header->initial_length_size = bytes_read;
3828 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3829 info_ptr += bytes_read;
3830 cu_header->version = read_2_bytes (abfd, info_ptr);
3831 info_ptr += 2;
3832 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3833 &bytes_read);
3834 info_ptr += bytes_read;
3835 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3836 info_ptr += 1;
3837 signed_addr = bfd_get_sign_extend_vma (abfd);
3838 if (signed_addr < 0)
3839 internal_error (__FILE__, __LINE__,
3840 _("read_comp_unit_head: dwarf from non elf file"));
3841 cu_header->signed_addr_p = signed_addr;
3842
3843 return info_ptr;
3844 }
3845
3846 /* Helper function that returns the proper abbrev section for
3847 THIS_CU. */
3848
3849 static struct dwarf2_section_info *
3850 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3851 {
3852 struct dwarf2_section_info *abbrev;
3853
3854 if (this_cu->is_dwz)
3855 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3856 else
3857 abbrev = &dwarf2_per_objfile->abbrev;
3858
3859 return abbrev;
3860 }
3861
3862 /* Subroutine of read_and_check_comp_unit_head and
3863 read_and_check_type_unit_head to simplify them.
3864 Perform various error checking on the header. */
3865
3866 static void
3867 error_check_comp_unit_head (struct comp_unit_head *header,
3868 struct dwarf2_section_info *section,
3869 struct dwarf2_section_info *abbrev_section)
3870 {
3871 bfd *abfd = section->asection->owner;
3872 const char *filename = bfd_get_filename (abfd);
3873
3874 if (header->version != 2 && header->version != 3 && header->version != 4)
3875 error (_("Dwarf Error: wrong version in compilation unit header "
3876 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3877 filename);
3878
3879 if (header->abbrev_offset.sect_off
3880 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3881 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3882 "(offset 0x%lx + 6) [in module %s]"),
3883 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3884 filename);
3885
3886 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3887 avoid potential 32-bit overflow. */
3888 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3889 > section->size)
3890 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3891 "(offset 0x%lx + 0) [in module %s]"),
3892 (long) header->length, (long) header->offset.sect_off,
3893 filename);
3894 }
3895
3896 /* Read in a CU/TU header and perform some basic error checking.
3897 The contents of the header are stored in HEADER.
3898 The result is a pointer to the start of the first DIE. */
3899
3900 static gdb_byte *
3901 read_and_check_comp_unit_head (struct comp_unit_head *header,
3902 struct dwarf2_section_info *section,
3903 struct dwarf2_section_info *abbrev_section,
3904 gdb_byte *info_ptr,
3905 int is_debug_types_section)
3906 {
3907 gdb_byte *beg_of_comp_unit = info_ptr;
3908 bfd *abfd = section->asection->owner;
3909
3910 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3911
3912 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3913
3914 /* If we're reading a type unit, skip over the signature and
3915 type_offset fields. */
3916 if (is_debug_types_section)
3917 info_ptr += 8 /*signature*/ + header->offset_size;
3918
3919 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3920
3921 error_check_comp_unit_head (header, section, abbrev_section);
3922
3923 return info_ptr;
3924 }
3925
3926 /* Read in the types comp unit header information from .debug_types entry at
3927 types_ptr. The result is a pointer to one past the end of the header. */
3928
3929 static gdb_byte *
3930 read_and_check_type_unit_head (struct comp_unit_head *header,
3931 struct dwarf2_section_info *section,
3932 struct dwarf2_section_info *abbrev_section,
3933 gdb_byte *info_ptr,
3934 ULONGEST *signature,
3935 cu_offset *type_offset_in_tu)
3936 {
3937 gdb_byte *beg_of_comp_unit = info_ptr;
3938 bfd *abfd = section->asection->owner;
3939
3940 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3941
3942 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3943
3944 /* If we're reading a type unit, skip over the signature and
3945 type_offset fields. */
3946 if (signature != NULL)
3947 *signature = read_8_bytes (abfd, info_ptr);
3948 info_ptr += 8;
3949 if (type_offset_in_tu != NULL)
3950 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3951 header->offset_size);
3952 info_ptr += header->offset_size;
3953
3954 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3955
3956 error_check_comp_unit_head (header, section, abbrev_section);
3957
3958 return info_ptr;
3959 }
3960
3961 /* Fetch the abbreviation table offset from a comp or type unit header. */
3962
3963 static sect_offset
3964 read_abbrev_offset (struct dwarf2_section_info *section,
3965 sect_offset offset)
3966 {
3967 bfd *abfd = section->asection->owner;
3968 gdb_byte *info_ptr;
3969 unsigned int length, initial_length_size, offset_size;
3970 sect_offset abbrev_offset;
3971
3972 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3973 info_ptr = section->buffer + offset.sect_off;
3974 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3975 offset_size = initial_length_size == 4 ? 4 : 8;
3976 info_ptr += initial_length_size + 2 /*version*/;
3977 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3978 return abbrev_offset;
3979 }
3980
3981 /* Allocate a new partial symtab for file named NAME and mark this new
3982 partial symtab as being an include of PST. */
3983
3984 static void
3985 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3986 struct objfile *objfile)
3987 {
3988 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3989
3990 subpst->section_offsets = pst->section_offsets;
3991 subpst->textlow = 0;
3992 subpst->texthigh = 0;
3993
3994 subpst->dependencies = (struct partial_symtab **)
3995 obstack_alloc (&objfile->objfile_obstack,
3996 sizeof (struct partial_symtab *));
3997 subpst->dependencies[0] = pst;
3998 subpst->number_of_dependencies = 1;
3999
4000 subpst->globals_offset = 0;
4001 subpst->n_global_syms = 0;
4002 subpst->statics_offset = 0;
4003 subpst->n_static_syms = 0;
4004 subpst->symtab = NULL;
4005 subpst->read_symtab = pst->read_symtab;
4006 subpst->readin = 0;
4007
4008 /* No private part is necessary for include psymtabs. This property
4009 can be used to differentiate between such include psymtabs and
4010 the regular ones. */
4011 subpst->read_symtab_private = NULL;
4012 }
4013
4014 /* Read the Line Number Program data and extract the list of files
4015 included by the source file represented by PST. Build an include
4016 partial symtab for each of these included files. */
4017
4018 static void
4019 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4020 struct die_info *die,
4021 struct partial_symtab *pst)
4022 {
4023 struct line_header *lh = NULL;
4024 struct attribute *attr;
4025
4026 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4027 if (attr)
4028 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4029 if (lh == NULL)
4030 return; /* No linetable, so no includes. */
4031
4032 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4033 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4034
4035 free_line_header (lh);
4036 }
4037
4038 static hashval_t
4039 hash_signatured_type (const void *item)
4040 {
4041 const struct signatured_type *sig_type = item;
4042
4043 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4044 return sig_type->signature;
4045 }
4046
4047 static int
4048 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4049 {
4050 const struct signatured_type *lhs = item_lhs;
4051 const struct signatured_type *rhs = item_rhs;
4052
4053 return lhs->signature == rhs->signature;
4054 }
4055
4056 /* Allocate a hash table for signatured types. */
4057
4058 static htab_t
4059 allocate_signatured_type_table (struct objfile *objfile)
4060 {
4061 return htab_create_alloc_ex (41,
4062 hash_signatured_type,
4063 eq_signatured_type,
4064 NULL,
4065 &objfile->objfile_obstack,
4066 hashtab_obstack_allocate,
4067 dummy_obstack_deallocate);
4068 }
4069
4070 /* A helper function to add a signatured type CU to a table. */
4071
4072 static int
4073 add_signatured_type_cu_to_table (void **slot, void *datum)
4074 {
4075 struct signatured_type *sigt = *slot;
4076 struct signatured_type ***datap = datum;
4077
4078 **datap = sigt;
4079 ++*datap;
4080
4081 return 1;
4082 }
4083
4084 /* Create the hash table of all entries in the .debug_types section.
4085 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4086 NULL otherwise.
4087 Note: This function processes DWO files only, not DWP files.
4088 The result is a pointer to the hash table or NULL if there are
4089 no types. */
4090
4091 static htab_t
4092 create_debug_types_hash_table (struct dwo_file *dwo_file,
4093 VEC (dwarf2_section_info_def) *types)
4094 {
4095 struct objfile *objfile = dwarf2_per_objfile->objfile;
4096 htab_t types_htab = NULL;
4097 int ix;
4098 struct dwarf2_section_info *section;
4099 struct dwarf2_section_info *abbrev_section;
4100
4101 if (VEC_empty (dwarf2_section_info_def, types))
4102 return NULL;
4103
4104 abbrev_section = (dwo_file != NULL
4105 ? &dwo_file->sections.abbrev
4106 : &dwarf2_per_objfile->abbrev);
4107
4108 if (dwarf2_read_debug)
4109 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4110 dwo_file ? ".dwo" : "",
4111 bfd_get_filename (abbrev_section->asection->owner));
4112
4113 for (ix = 0;
4114 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4115 ++ix)
4116 {
4117 bfd *abfd;
4118 gdb_byte *info_ptr, *end_ptr;
4119 struct dwarf2_section_info *abbrev_section;
4120
4121 dwarf2_read_section (objfile, section);
4122 info_ptr = section->buffer;
4123
4124 if (info_ptr == NULL)
4125 continue;
4126
4127 /* We can't set abfd until now because the section may be empty or
4128 not present, in which case section->asection will be NULL. */
4129 abfd = section->asection->owner;
4130
4131 if (dwo_file)
4132 abbrev_section = &dwo_file->sections.abbrev;
4133 else
4134 abbrev_section = &dwarf2_per_objfile->abbrev;
4135
4136 if (types_htab == NULL)
4137 {
4138 if (dwo_file)
4139 types_htab = allocate_dwo_unit_table (objfile);
4140 else
4141 types_htab = allocate_signatured_type_table (objfile);
4142 }
4143
4144 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4145 because we don't need to read any dies: the signature is in the
4146 header. */
4147
4148 end_ptr = info_ptr + section->size;
4149 while (info_ptr < end_ptr)
4150 {
4151 sect_offset offset;
4152 cu_offset type_offset_in_tu;
4153 ULONGEST signature;
4154 struct signatured_type *sig_type;
4155 struct dwo_unit *dwo_tu;
4156 void **slot;
4157 gdb_byte *ptr = info_ptr;
4158 struct comp_unit_head header;
4159 unsigned int length;
4160
4161 offset.sect_off = ptr - section->buffer;
4162
4163 /* We need to read the type's signature in order to build the hash
4164 table, but we don't need anything else just yet. */
4165
4166 ptr = read_and_check_type_unit_head (&header, section,
4167 abbrev_section, ptr,
4168 &signature, &type_offset_in_tu);
4169
4170 length = get_cu_length (&header);
4171
4172 /* Skip dummy type units. */
4173 if (ptr >= info_ptr + length
4174 || peek_abbrev_code (abfd, ptr) == 0)
4175 {
4176 info_ptr += length;
4177 continue;
4178 }
4179
4180 if (dwo_file)
4181 {
4182 sig_type = NULL;
4183 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4184 struct dwo_unit);
4185 dwo_tu->dwo_file = dwo_file;
4186 dwo_tu->signature = signature;
4187 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4188 dwo_tu->info_or_types_section = section;
4189 dwo_tu->offset = offset;
4190 dwo_tu->length = length;
4191 }
4192 else
4193 {
4194 /* N.B.: type_offset is not usable if this type uses a DWO file.
4195 The real type_offset is in the DWO file. */
4196 dwo_tu = NULL;
4197 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4198 struct signatured_type);
4199 sig_type->signature = signature;
4200 sig_type->type_offset_in_tu = type_offset_in_tu;
4201 sig_type->per_cu.objfile = objfile;
4202 sig_type->per_cu.is_debug_types = 1;
4203 sig_type->per_cu.info_or_types_section = section;
4204 sig_type->per_cu.offset = offset;
4205 sig_type->per_cu.length = length;
4206 }
4207
4208 slot = htab_find_slot (types_htab,
4209 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4210 INSERT);
4211 gdb_assert (slot != NULL);
4212 if (*slot != NULL)
4213 {
4214 sect_offset dup_offset;
4215
4216 if (dwo_file)
4217 {
4218 const struct dwo_unit *dup_tu = *slot;
4219
4220 dup_offset = dup_tu->offset;
4221 }
4222 else
4223 {
4224 const struct signatured_type *dup_tu = *slot;
4225
4226 dup_offset = dup_tu->per_cu.offset;
4227 }
4228
4229 complaint (&symfile_complaints,
4230 _("debug type entry at offset 0x%x is duplicate to the "
4231 "entry at offset 0x%x, signature 0x%s"),
4232 offset.sect_off, dup_offset.sect_off,
4233 phex (signature, sizeof (signature)));
4234 }
4235 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4236
4237 if (dwarf2_read_debug)
4238 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4239 offset.sect_off,
4240 phex (signature, sizeof (signature)));
4241
4242 info_ptr += length;
4243 }
4244 }
4245
4246 return types_htab;
4247 }
4248
4249 /* Create the hash table of all entries in the .debug_types section,
4250 and initialize all_type_units.
4251 The result is zero if there is an error (e.g. missing .debug_types section),
4252 otherwise non-zero. */
4253
4254 static int
4255 create_all_type_units (struct objfile *objfile)
4256 {
4257 htab_t types_htab;
4258 struct signatured_type **iter;
4259
4260 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4261 if (types_htab == NULL)
4262 {
4263 dwarf2_per_objfile->signatured_types = NULL;
4264 return 0;
4265 }
4266
4267 dwarf2_per_objfile->signatured_types = types_htab;
4268
4269 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4270 dwarf2_per_objfile->all_type_units
4271 = obstack_alloc (&objfile->objfile_obstack,
4272 dwarf2_per_objfile->n_type_units
4273 * sizeof (struct signatured_type *));
4274 iter = &dwarf2_per_objfile->all_type_units[0];
4275 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4276 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4277 == dwarf2_per_objfile->n_type_units);
4278
4279 return 1;
4280 }
4281
4282 /* Lookup a signature based type for DW_FORM_ref_sig8.
4283 Returns NULL if signature SIG is not present in the table. */
4284
4285 static struct signatured_type *
4286 lookup_signatured_type (ULONGEST sig)
4287 {
4288 struct signatured_type find_entry, *entry;
4289
4290 if (dwarf2_per_objfile->signatured_types == NULL)
4291 {
4292 complaint (&symfile_complaints,
4293 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4294 return NULL;
4295 }
4296
4297 find_entry.signature = sig;
4298 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4299 return entry;
4300 }
4301 \f
4302 /* Low level DIE reading support. */
4303
4304 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4305
4306 static void
4307 init_cu_die_reader (struct die_reader_specs *reader,
4308 struct dwarf2_cu *cu,
4309 struct dwarf2_section_info *section,
4310 struct dwo_file *dwo_file)
4311 {
4312 gdb_assert (section->readin && section->buffer != NULL);
4313 reader->abfd = section->asection->owner;
4314 reader->cu = cu;
4315 reader->dwo_file = dwo_file;
4316 reader->die_section = section;
4317 reader->buffer = section->buffer;
4318 reader->buffer_end = section->buffer + section->size;
4319 }
4320
4321 /* Initialize a CU (or TU) and read its DIEs.
4322 If the CU defers to a DWO file, read the DWO file as well.
4323
4324 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4325 Otherwise the table specified in the comp unit header is read in and used.
4326 This is an optimization for when we already have the abbrev table.
4327
4328 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4329 Otherwise, a new CU is allocated with xmalloc.
4330
4331 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4332 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4333
4334 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4335 linker) then DIE_READER_FUNC will not get called. */
4336
4337 static void
4338 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4339 struct abbrev_table *abbrev_table,
4340 int use_existing_cu, int keep,
4341 die_reader_func_ftype *die_reader_func,
4342 void *data)
4343 {
4344 struct objfile *objfile = dwarf2_per_objfile->objfile;
4345 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4346 bfd *abfd = section->asection->owner;
4347 struct dwarf2_cu *cu;
4348 gdb_byte *begin_info_ptr, *info_ptr;
4349 struct die_reader_specs reader;
4350 struct die_info *comp_unit_die;
4351 int has_children;
4352 struct attribute *attr;
4353 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4354 struct signatured_type *sig_type = NULL;
4355 struct dwarf2_section_info *abbrev_section;
4356 /* Non-zero if CU currently points to a DWO file and we need to
4357 reread it. When this happens we need to reread the skeleton die
4358 before we can reread the DWO file. */
4359 int rereading_dwo_cu = 0;
4360
4361 if (dwarf2_die_debug)
4362 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4363 this_cu->is_debug_types ? "type" : "comp",
4364 this_cu->offset.sect_off);
4365
4366 if (use_existing_cu)
4367 gdb_assert (keep);
4368
4369 cleanups = make_cleanup (null_cleanup, NULL);
4370
4371 /* This is cheap if the section is already read in. */
4372 dwarf2_read_section (objfile, section);
4373
4374 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4375
4376 abbrev_section = get_abbrev_section_for_cu (this_cu);
4377
4378 if (use_existing_cu && this_cu->cu != NULL)
4379 {
4380 cu = this_cu->cu;
4381
4382 /* If this CU is from a DWO file we need to start over, we need to
4383 refetch the attributes from the skeleton CU.
4384 This could be optimized by retrieving those attributes from when we
4385 were here the first time: the previous comp_unit_die was stored in
4386 comp_unit_obstack. But there's no data yet that we need this
4387 optimization. */
4388 if (cu->dwo_unit != NULL)
4389 rereading_dwo_cu = 1;
4390 }
4391 else
4392 {
4393 /* If !use_existing_cu, this_cu->cu must be NULL. */
4394 gdb_assert (this_cu->cu == NULL);
4395
4396 cu = xmalloc (sizeof (*cu));
4397 init_one_comp_unit (cu, this_cu);
4398
4399 /* If an error occurs while loading, release our storage. */
4400 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4401 }
4402
4403 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4404 {
4405 /* We already have the header, there's no need to read it in again. */
4406 info_ptr += cu->header.first_die_offset.cu_off;
4407 }
4408 else
4409 {
4410 if (this_cu->is_debug_types)
4411 {
4412 ULONGEST signature;
4413 cu_offset type_offset_in_tu;
4414
4415 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4416 abbrev_section, info_ptr,
4417 &signature,
4418 &type_offset_in_tu);
4419
4420 /* Since per_cu is the first member of struct signatured_type,
4421 we can go from a pointer to one to a pointer to the other. */
4422 sig_type = (struct signatured_type *) this_cu;
4423 gdb_assert (sig_type->signature == signature);
4424 gdb_assert (sig_type->type_offset_in_tu.cu_off
4425 == type_offset_in_tu.cu_off);
4426 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4427
4428 /* LENGTH has not been set yet for type units if we're
4429 using .gdb_index. */
4430 this_cu->length = get_cu_length (&cu->header);
4431
4432 /* Establish the type offset that can be used to lookup the type. */
4433 sig_type->type_offset_in_section.sect_off =
4434 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4435 }
4436 else
4437 {
4438 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4439 abbrev_section,
4440 info_ptr, 0);
4441
4442 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4443 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4444 }
4445 }
4446
4447 /* Skip dummy compilation units. */
4448 if (info_ptr >= begin_info_ptr + this_cu->length
4449 || peek_abbrev_code (abfd, info_ptr) == 0)
4450 {
4451 do_cleanups (cleanups);
4452 return;
4453 }
4454
4455 /* If we don't have them yet, read the abbrevs for this compilation unit.
4456 And if we need to read them now, make sure they're freed when we're
4457 done. Note that it's important that if the CU had an abbrev table
4458 on entry we don't free it when we're done: Somewhere up the call stack
4459 it may be in use. */
4460 if (abbrev_table != NULL)
4461 {
4462 gdb_assert (cu->abbrev_table == NULL);
4463 gdb_assert (cu->header.abbrev_offset.sect_off
4464 == abbrev_table->offset.sect_off);
4465 cu->abbrev_table = abbrev_table;
4466 }
4467 else if (cu->abbrev_table == NULL)
4468 {
4469 dwarf2_read_abbrevs (cu, abbrev_section);
4470 make_cleanup (dwarf2_free_abbrev_table, cu);
4471 }
4472 else if (rereading_dwo_cu)
4473 {
4474 dwarf2_free_abbrev_table (cu);
4475 dwarf2_read_abbrevs (cu, abbrev_section);
4476 }
4477
4478 /* Read the top level CU/TU die. */
4479 init_cu_die_reader (&reader, cu, section, NULL);
4480 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4481
4482 /* If we have a DWO stub, process it and then read in the DWO file.
4483 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4484 a DWO CU, that this test will fail. */
4485 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4486 if (attr)
4487 {
4488 char *dwo_name = DW_STRING (attr);
4489 const char *comp_dir_string;
4490 struct dwo_unit *dwo_unit;
4491 ULONGEST signature; /* Or dwo_id. */
4492 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4493 int i,num_extra_attrs;
4494 struct dwarf2_section_info *dwo_abbrev_section;
4495
4496 if (has_children)
4497 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4498 " has children (offset 0x%x) [in module %s]"),
4499 this_cu->offset.sect_off, bfd_get_filename (abfd));
4500
4501 /* These attributes aren't processed until later:
4502 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4503 However, the attribute is found in the stub which we won't have later.
4504 In order to not impose this complication on the rest of the code,
4505 we read them here and copy them to the DWO CU/TU die. */
4506
4507 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4508 DWO file. */
4509 stmt_list = NULL;
4510 if (! this_cu->is_debug_types)
4511 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4512 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4513 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4514 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4515 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4516
4517 /* There should be a DW_AT_addr_base attribute here (if needed).
4518 We need the value before we can process DW_FORM_GNU_addr_index. */
4519 cu->addr_base = 0;
4520 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4521 if (attr)
4522 cu->addr_base = DW_UNSND (attr);
4523
4524 /* There should be a DW_AT_ranges_base attribute here (if needed).
4525 We need the value before we can process DW_AT_ranges. */
4526 cu->ranges_base = 0;
4527 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4528 if (attr)
4529 cu->ranges_base = DW_UNSND (attr);
4530
4531 if (this_cu->is_debug_types)
4532 {
4533 gdb_assert (sig_type != NULL);
4534 signature = sig_type->signature;
4535 }
4536 else
4537 {
4538 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4539 if (! attr)
4540 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4541 dwo_name);
4542 signature = DW_UNSND (attr);
4543 }
4544
4545 /* We may need the comp_dir in order to find the DWO file. */
4546 comp_dir_string = NULL;
4547 if (comp_dir)
4548 comp_dir_string = DW_STRING (comp_dir);
4549
4550 if (this_cu->is_debug_types)
4551 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4552 else
4553 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4554 signature);
4555
4556 if (dwo_unit == NULL)
4557 {
4558 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4559 " with ID %s [in module %s]"),
4560 this_cu->offset.sect_off,
4561 phex (signature, sizeof (signature)),
4562 objfile->name);
4563 }
4564
4565 /* Set up for reading the DWO CU/TU. */
4566 cu->dwo_unit = dwo_unit;
4567 section = dwo_unit->info_or_types_section;
4568 dwarf2_read_section (objfile, section);
4569 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4570 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4571 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4572
4573 if (this_cu->is_debug_types)
4574 {
4575 ULONGEST signature;
4576 cu_offset type_offset_in_tu;
4577
4578 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4579 dwo_abbrev_section,
4580 info_ptr,
4581 &signature,
4582 &type_offset_in_tu);
4583 gdb_assert (sig_type->signature == signature);
4584 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4585 /* For DWOs coming from DWP files, we don't know the CU length
4586 nor the type's offset in the TU until now. */
4587 dwo_unit->length = get_cu_length (&cu->header);
4588 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4589
4590 /* Establish the type offset that can be used to lookup the type.
4591 For DWO files, we don't know it until now. */
4592 sig_type->type_offset_in_section.sect_off =
4593 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4594 }
4595 else
4596 {
4597 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4598 dwo_abbrev_section,
4599 info_ptr, 0);
4600 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4601 /* For DWOs coming from DWP files, we don't know the CU length
4602 until now. */
4603 dwo_unit->length = get_cu_length (&cu->header);
4604 }
4605
4606 /* Discard the original CU's abbrev table, and read the DWO's. */
4607 if (abbrev_table == NULL)
4608 {
4609 dwarf2_free_abbrev_table (cu);
4610 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4611 }
4612 else
4613 {
4614 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4615 make_cleanup (dwarf2_free_abbrev_table, cu);
4616 }
4617
4618 /* Read in the die, but leave space to copy over the attributes
4619 from the stub. This has the benefit of simplifying the rest of
4620 the code - all the real work is done here. */
4621 num_extra_attrs = ((stmt_list != NULL)
4622 + (low_pc != NULL)
4623 + (high_pc != NULL)
4624 + (ranges != NULL)
4625 + (comp_dir != NULL));
4626 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4627 &has_children, num_extra_attrs);
4628
4629 /* Copy over the attributes from the stub to the DWO die. */
4630 i = comp_unit_die->num_attrs;
4631 if (stmt_list != NULL)
4632 comp_unit_die->attrs[i++] = *stmt_list;
4633 if (low_pc != NULL)
4634 comp_unit_die->attrs[i++] = *low_pc;
4635 if (high_pc != NULL)
4636 comp_unit_die->attrs[i++] = *high_pc;
4637 if (ranges != NULL)
4638 comp_unit_die->attrs[i++] = *ranges;
4639 if (comp_dir != NULL)
4640 comp_unit_die->attrs[i++] = *comp_dir;
4641 comp_unit_die->num_attrs += num_extra_attrs;
4642
4643 /* Skip dummy compilation units. */
4644 if (info_ptr >= begin_info_ptr + dwo_unit->length
4645 || peek_abbrev_code (abfd, info_ptr) == 0)
4646 {
4647 do_cleanups (cleanups);
4648 return;
4649 }
4650 }
4651
4652 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4653
4654 if (free_cu_cleanup != NULL)
4655 {
4656 if (keep)
4657 {
4658 /* We've successfully allocated this compilation unit. Let our
4659 caller clean it up when finished with it. */
4660 discard_cleanups (free_cu_cleanup);
4661
4662 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4663 So we have to manually free the abbrev table. */
4664 dwarf2_free_abbrev_table (cu);
4665
4666 /* Link this CU into read_in_chain. */
4667 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4668 dwarf2_per_objfile->read_in_chain = this_cu;
4669 }
4670 else
4671 do_cleanups (free_cu_cleanup);
4672 }
4673
4674 do_cleanups (cleanups);
4675 }
4676
4677 /* Read CU/TU THIS_CU in section SECTION,
4678 but do not follow DW_AT_GNU_dwo_name if present.
4679 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4680 to have already done the lookup to find the DWO/DWP file).
4681
4682 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4683 THIS_CU->is_debug_types, but nothing else.
4684
4685 We fill in THIS_CU->length.
4686
4687 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4688 linker) then DIE_READER_FUNC will not get called.
4689
4690 THIS_CU->cu is always freed when done.
4691 This is done in order to not leave THIS_CU->cu in a state where we have
4692 to care whether it refers to the "main" CU or the DWO CU. */
4693
4694 static void
4695 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4696 struct dwarf2_section_info *abbrev_section,
4697 struct dwo_file *dwo_file,
4698 die_reader_func_ftype *die_reader_func,
4699 void *data)
4700 {
4701 struct objfile *objfile = dwarf2_per_objfile->objfile;
4702 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4703 bfd *abfd = section->asection->owner;
4704 struct dwarf2_cu cu;
4705 gdb_byte *begin_info_ptr, *info_ptr;
4706 struct die_reader_specs reader;
4707 struct cleanup *cleanups;
4708 struct die_info *comp_unit_die;
4709 int has_children;
4710
4711 if (dwarf2_die_debug)
4712 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4713 this_cu->is_debug_types ? "type" : "comp",
4714 this_cu->offset.sect_off);
4715
4716 gdb_assert (this_cu->cu == NULL);
4717
4718 /* This is cheap if the section is already read in. */
4719 dwarf2_read_section (objfile, section);
4720
4721 init_one_comp_unit (&cu, this_cu);
4722
4723 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4724
4725 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4726 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4727 abbrev_section, info_ptr,
4728 this_cu->is_debug_types);
4729
4730 this_cu->length = get_cu_length (&cu.header);
4731
4732 /* Skip dummy compilation units. */
4733 if (info_ptr >= begin_info_ptr + this_cu->length
4734 || peek_abbrev_code (abfd, info_ptr) == 0)
4735 {
4736 do_cleanups (cleanups);
4737 return;
4738 }
4739
4740 dwarf2_read_abbrevs (&cu, abbrev_section);
4741 make_cleanup (dwarf2_free_abbrev_table, &cu);
4742
4743 init_cu_die_reader (&reader, &cu, section, dwo_file);
4744 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4745
4746 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4747
4748 do_cleanups (cleanups);
4749 }
4750
4751 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4752 does not lookup the specified DWO file.
4753 This cannot be used to read DWO files.
4754
4755 THIS_CU->cu is always freed when done.
4756 This is done in order to not leave THIS_CU->cu in a state where we have
4757 to care whether it refers to the "main" CU or the DWO CU.
4758 We can revisit this if the data shows there's a performance issue. */
4759
4760 static void
4761 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4762 die_reader_func_ftype *die_reader_func,
4763 void *data)
4764 {
4765 init_cutu_and_read_dies_no_follow (this_cu,
4766 get_abbrev_section_for_cu (this_cu),
4767 NULL,
4768 die_reader_func, data);
4769 }
4770
4771 /* Create a psymtab named NAME and assign it to PER_CU.
4772
4773 The caller must fill in the following details:
4774 dirname, textlow, texthigh. */
4775
4776 static struct partial_symtab *
4777 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4778 {
4779 struct objfile *objfile = per_cu->objfile;
4780 struct partial_symtab *pst;
4781
4782 pst = start_psymtab_common (objfile, objfile->section_offsets,
4783 name, 0,
4784 objfile->global_psymbols.next,
4785 objfile->static_psymbols.next);
4786
4787 pst->psymtabs_addrmap_supported = 1;
4788
4789 /* This is the glue that links PST into GDB's symbol API. */
4790 pst->read_symtab_private = per_cu;
4791 pst->read_symtab = dwarf2_psymtab_to_symtab;
4792 per_cu->v.psymtab = pst;
4793
4794 return pst;
4795 }
4796
4797 /* die_reader_func for process_psymtab_comp_unit. */
4798
4799 static void
4800 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4801 gdb_byte *info_ptr,
4802 struct die_info *comp_unit_die,
4803 int has_children,
4804 void *data)
4805 {
4806 struct dwarf2_cu *cu = reader->cu;
4807 struct objfile *objfile = cu->objfile;
4808 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4809 struct attribute *attr;
4810 CORE_ADDR baseaddr;
4811 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4812 struct partial_symtab *pst;
4813 int has_pc_info;
4814 const char *filename;
4815 int *want_partial_unit_ptr = data;
4816
4817 if (comp_unit_die->tag == DW_TAG_partial_unit
4818 && (want_partial_unit_ptr == NULL
4819 || !*want_partial_unit_ptr))
4820 return;
4821
4822 gdb_assert (! per_cu->is_debug_types);
4823
4824 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4825
4826 cu->list_in_scope = &file_symbols;
4827
4828 /* Allocate a new partial symbol table structure. */
4829 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4830 if (attr == NULL || !DW_STRING (attr))
4831 filename = "";
4832 else
4833 filename = DW_STRING (attr);
4834
4835 pst = create_partial_symtab (per_cu, filename);
4836
4837 /* This must be done before calling dwarf2_build_include_psymtabs. */
4838 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4839 if (attr != NULL)
4840 pst->dirname = DW_STRING (attr);
4841
4842 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4843
4844 dwarf2_find_base_address (comp_unit_die, cu);
4845
4846 /* Possibly set the default values of LOWPC and HIGHPC from
4847 `DW_AT_ranges'. */
4848 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4849 &best_highpc, cu, pst);
4850 if (has_pc_info == 1 && best_lowpc < best_highpc)
4851 /* Store the contiguous range if it is not empty; it can be empty for
4852 CUs with no code. */
4853 addrmap_set_empty (objfile->psymtabs_addrmap,
4854 best_lowpc + baseaddr,
4855 best_highpc + baseaddr - 1, pst);
4856
4857 /* Check if comp unit has_children.
4858 If so, read the rest of the partial symbols from this comp unit.
4859 If not, there's no more debug_info for this comp unit. */
4860 if (has_children)
4861 {
4862 struct partial_die_info *first_die;
4863 CORE_ADDR lowpc, highpc;
4864
4865 lowpc = ((CORE_ADDR) -1);
4866 highpc = ((CORE_ADDR) 0);
4867
4868 first_die = load_partial_dies (reader, info_ptr, 1);
4869
4870 scan_partial_symbols (first_die, &lowpc, &highpc,
4871 ! has_pc_info, cu);
4872
4873 /* If we didn't find a lowpc, set it to highpc to avoid
4874 complaints from `maint check'. */
4875 if (lowpc == ((CORE_ADDR) -1))
4876 lowpc = highpc;
4877
4878 /* If the compilation unit didn't have an explicit address range,
4879 then use the information extracted from its child dies. */
4880 if (! has_pc_info)
4881 {
4882 best_lowpc = lowpc;
4883 best_highpc = highpc;
4884 }
4885 }
4886 pst->textlow = best_lowpc + baseaddr;
4887 pst->texthigh = best_highpc + baseaddr;
4888
4889 pst->n_global_syms = objfile->global_psymbols.next -
4890 (objfile->global_psymbols.list + pst->globals_offset);
4891 pst->n_static_syms = objfile->static_psymbols.next -
4892 (objfile->static_psymbols.list + pst->statics_offset);
4893 sort_pst_symbols (pst);
4894
4895 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4896 {
4897 int i;
4898 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4899 struct dwarf2_per_cu_data *iter;
4900
4901 /* Fill in 'dependencies' here; we fill in 'users' in a
4902 post-pass. */
4903 pst->number_of_dependencies = len;
4904 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4905 len * sizeof (struct symtab *));
4906 for (i = 0;
4907 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4908 i, iter);
4909 ++i)
4910 pst->dependencies[i] = iter->v.psymtab;
4911
4912 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4913 }
4914
4915 /* Get the list of files included in the current compilation unit,
4916 and build a psymtab for each of them. */
4917 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4918
4919 if (dwarf2_read_debug)
4920 {
4921 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4922
4923 fprintf_unfiltered (gdb_stdlog,
4924 "Psymtab for %s unit @0x%x: %s - %s"
4925 ", %d global, %d static syms\n",
4926 per_cu->is_debug_types ? "type" : "comp",
4927 per_cu->offset.sect_off,
4928 paddress (gdbarch, pst->textlow),
4929 paddress (gdbarch, pst->texthigh),
4930 pst->n_global_syms, pst->n_static_syms);
4931 }
4932 }
4933
4934 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4935 Process compilation unit THIS_CU for a psymtab. */
4936
4937 static void
4938 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4939 int want_partial_unit)
4940 {
4941 /* If this compilation unit was already read in, free the
4942 cached copy in order to read it in again. This is
4943 necessary because we skipped some symbols when we first
4944 read in the compilation unit (see load_partial_dies).
4945 This problem could be avoided, but the benefit is unclear. */
4946 if (this_cu->cu != NULL)
4947 free_one_cached_comp_unit (this_cu);
4948
4949 gdb_assert (! this_cu->is_debug_types);
4950 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4951 process_psymtab_comp_unit_reader,
4952 &want_partial_unit);
4953
4954 /* Age out any secondary CUs. */
4955 age_cached_comp_units ();
4956 }
4957
4958 static hashval_t
4959 hash_type_unit_group (const void *item)
4960 {
4961 const struct type_unit_group *tu_group = item;
4962
4963 return hash_stmt_list_entry (&tu_group->hash);
4964 }
4965
4966 static int
4967 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4968 {
4969 const struct type_unit_group *lhs = item_lhs;
4970 const struct type_unit_group *rhs = item_rhs;
4971
4972 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4973 }
4974
4975 /* Allocate a hash table for type unit groups. */
4976
4977 static htab_t
4978 allocate_type_unit_groups_table (void)
4979 {
4980 return htab_create_alloc_ex (3,
4981 hash_type_unit_group,
4982 eq_type_unit_group,
4983 NULL,
4984 &dwarf2_per_objfile->objfile->objfile_obstack,
4985 hashtab_obstack_allocate,
4986 dummy_obstack_deallocate);
4987 }
4988
4989 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4990 partial symtabs. We combine several TUs per psymtab to not let the size
4991 of any one psymtab grow too big. */
4992 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4993 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4994
4995 /* Helper routine for get_type_unit_group.
4996 Create the type_unit_group object used to hold one or more TUs. */
4997
4998 static struct type_unit_group *
4999 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5000 {
5001 struct objfile *objfile = dwarf2_per_objfile->objfile;
5002 struct dwarf2_per_cu_data *per_cu;
5003 struct type_unit_group *tu_group;
5004
5005 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5006 struct type_unit_group);
5007 per_cu = &tu_group->per_cu;
5008 per_cu->objfile = objfile;
5009 per_cu->is_debug_types = 1;
5010 per_cu->s.type_unit_group = tu_group;
5011
5012 if (dwarf2_per_objfile->using_index)
5013 {
5014 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5015 struct dwarf2_per_cu_quick_data);
5016 tu_group->t.first_tu = cu->per_cu;
5017 }
5018 else
5019 {
5020 unsigned int line_offset = line_offset_struct.sect_off;
5021 struct partial_symtab *pst;
5022 char *name;
5023
5024 /* Give the symtab a useful name for debug purposes. */
5025 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5026 name = xstrprintf ("<type_units_%d>",
5027 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5028 else
5029 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5030
5031 pst = create_partial_symtab (per_cu, name);
5032 pst->anonymous = 1;
5033
5034 xfree (name);
5035 }
5036
5037 tu_group->hash.dwo_unit = cu->dwo_unit;
5038 tu_group->hash.line_offset = line_offset_struct;
5039
5040 return tu_group;
5041 }
5042
5043 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5044 STMT_LIST is a DW_AT_stmt_list attribute. */
5045
5046 static struct type_unit_group *
5047 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5048 {
5049 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5050 struct type_unit_group *tu_group;
5051 void **slot;
5052 unsigned int line_offset;
5053 struct type_unit_group type_unit_group_for_lookup;
5054
5055 if (dwarf2_per_objfile->type_unit_groups == NULL)
5056 {
5057 dwarf2_per_objfile->type_unit_groups =
5058 allocate_type_unit_groups_table ();
5059 }
5060
5061 /* Do we need to create a new group, or can we use an existing one? */
5062
5063 if (stmt_list)
5064 {
5065 line_offset = DW_UNSND (stmt_list);
5066 ++tu_stats->nr_symtab_sharers;
5067 }
5068 else
5069 {
5070 /* Ugh, no stmt_list. Rare, but we have to handle it.
5071 We can do various things here like create one group per TU or
5072 spread them over multiple groups to split up the expansion work.
5073 To avoid worst case scenarios (too many groups or too large groups)
5074 we, umm, group them in bunches. */
5075 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5076 | (tu_stats->nr_stmt_less_type_units
5077 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5078 ++tu_stats->nr_stmt_less_type_units;
5079 }
5080
5081 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5082 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5083 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5084 &type_unit_group_for_lookup, INSERT);
5085 if (*slot != NULL)
5086 {
5087 tu_group = *slot;
5088 gdb_assert (tu_group != NULL);
5089 }
5090 else
5091 {
5092 sect_offset line_offset_struct;
5093
5094 line_offset_struct.sect_off = line_offset;
5095 tu_group = create_type_unit_group (cu, line_offset_struct);
5096 *slot = tu_group;
5097 ++tu_stats->nr_symtabs;
5098 }
5099
5100 return tu_group;
5101 }
5102
5103 /* Struct used to sort TUs by their abbreviation table offset. */
5104
5105 struct tu_abbrev_offset
5106 {
5107 struct signatured_type *sig_type;
5108 sect_offset abbrev_offset;
5109 };
5110
5111 /* Helper routine for build_type_unit_groups, passed to qsort. */
5112
5113 static int
5114 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5115 {
5116 const struct tu_abbrev_offset * const *a = ap;
5117 const struct tu_abbrev_offset * const *b = bp;
5118 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5119 unsigned int boff = (*b)->abbrev_offset.sect_off;
5120
5121 return (aoff > boff) - (aoff < boff);
5122 }
5123
5124 /* A helper function to add a type_unit_group to a table. */
5125
5126 static int
5127 add_type_unit_group_to_table (void **slot, void *datum)
5128 {
5129 struct type_unit_group *tu_group = *slot;
5130 struct type_unit_group ***datap = datum;
5131
5132 **datap = tu_group;
5133 ++*datap;
5134
5135 return 1;
5136 }
5137
5138 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5139 each one passing FUNC,DATA.
5140
5141 The efficiency is because we sort TUs by the abbrev table they use and
5142 only read each abbrev table once. In one program there are 200K TUs
5143 sharing 8K abbrev tables.
5144
5145 The main purpose of this function is to support building the
5146 dwarf2_per_objfile->type_unit_groups table.
5147 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5148 can collapse the search space by grouping them by stmt_list.
5149 The savings can be significant, in the same program from above the 200K TUs
5150 share 8K stmt_list tables.
5151
5152 FUNC is expected to call get_type_unit_group, which will create the
5153 struct type_unit_group if necessary and add it to
5154 dwarf2_per_objfile->type_unit_groups. */
5155
5156 static void
5157 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5158 {
5159 struct objfile *objfile = dwarf2_per_objfile->objfile;
5160 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5161 struct cleanup *cleanups;
5162 struct abbrev_table *abbrev_table;
5163 sect_offset abbrev_offset;
5164 struct tu_abbrev_offset *sorted_by_abbrev;
5165 struct type_unit_group **iter;
5166 int i;
5167
5168 /* It's up to the caller to not call us multiple times. */
5169 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5170
5171 if (dwarf2_per_objfile->n_type_units == 0)
5172 return;
5173
5174 /* TUs typically share abbrev tables, and there can be way more TUs than
5175 abbrev tables. Sort by abbrev table to reduce the number of times we
5176 read each abbrev table in.
5177 Alternatives are to punt or to maintain a cache of abbrev tables.
5178 This is simpler and efficient enough for now.
5179
5180 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5181 symtab to use). Typically TUs with the same abbrev offset have the same
5182 stmt_list value too so in practice this should work well.
5183
5184 The basic algorithm here is:
5185
5186 sort TUs by abbrev table
5187 for each TU with same abbrev table:
5188 read abbrev table if first user
5189 read TU top level DIE
5190 [IWBN if DWO skeletons had DW_AT_stmt_list]
5191 call FUNC */
5192
5193 if (dwarf2_read_debug)
5194 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5195
5196 /* Sort in a separate table to maintain the order of all_type_units
5197 for .gdb_index: TU indices directly index all_type_units. */
5198 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5199 dwarf2_per_objfile->n_type_units);
5200 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5201 {
5202 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5203
5204 sorted_by_abbrev[i].sig_type = sig_type;
5205 sorted_by_abbrev[i].abbrev_offset =
5206 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5207 sig_type->per_cu.offset);
5208 }
5209 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5210 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5211 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5212
5213 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5214 called any number of times, so we don't reset tu_stats here. */
5215
5216 abbrev_offset.sect_off = ~(unsigned) 0;
5217 abbrev_table = NULL;
5218 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5219
5220 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5221 {
5222 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5223
5224 /* Switch to the next abbrev table if necessary. */
5225 if (abbrev_table == NULL
5226 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5227 {
5228 if (abbrev_table != NULL)
5229 {
5230 abbrev_table_free (abbrev_table);
5231 /* Reset to NULL in case abbrev_table_read_table throws
5232 an error: abbrev_table_free_cleanup will get called. */
5233 abbrev_table = NULL;
5234 }
5235 abbrev_offset = tu->abbrev_offset;
5236 abbrev_table =
5237 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5238 abbrev_offset);
5239 ++tu_stats->nr_uniq_abbrev_tables;
5240 }
5241
5242 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5243 func, data);
5244 }
5245
5246 /* Create a vector of pointers to primary type units to make it easy to
5247 iterate over them and CUs. See dw2_get_primary_cu. */
5248 dwarf2_per_objfile->n_type_unit_groups =
5249 htab_elements (dwarf2_per_objfile->type_unit_groups);
5250 dwarf2_per_objfile->all_type_unit_groups =
5251 obstack_alloc (&objfile->objfile_obstack,
5252 dwarf2_per_objfile->n_type_unit_groups
5253 * sizeof (struct type_unit_group *));
5254 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5255 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5256 add_type_unit_group_to_table, &iter);
5257 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5258 == dwarf2_per_objfile->n_type_unit_groups);
5259
5260 do_cleanups (cleanups);
5261
5262 if (dwarf2_read_debug)
5263 {
5264 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5265 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5266 dwarf2_per_objfile->n_type_units);
5267 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5268 tu_stats->nr_uniq_abbrev_tables);
5269 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5270 tu_stats->nr_symtabs);
5271 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5272 tu_stats->nr_symtab_sharers);
5273 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5274 tu_stats->nr_stmt_less_type_units);
5275 }
5276 }
5277
5278 /* Reader function for build_type_psymtabs. */
5279
5280 static void
5281 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5282 gdb_byte *info_ptr,
5283 struct die_info *type_unit_die,
5284 int has_children,
5285 void *data)
5286 {
5287 struct objfile *objfile = dwarf2_per_objfile->objfile;
5288 struct dwarf2_cu *cu = reader->cu;
5289 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5290 struct type_unit_group *tu_group;
5291 struct attribute *attr;
5292 struct partial_die_info *first_die;
5293 CORE_ADDR lowpc, highpc;
5294 struct partial_symtab *pst;
5295
5296 gdb_assert (data == NULL);
5297
5298 if (! has_children)
5299 return;
5300
5301 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5302 tu_group = get_type_unit_group (cu, attr);
5303
5304 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5305
5306 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5307 cu->list_in_scope = &file_symbols;
5308 pst = create_partial_symtab (per_cu, "");
5309 pst->anonymous = 1;
5310
5311 first_die = load_partial_dies (reader, info_ptr, 1);
5312
5313 lowpc = (CORE_ADDR) -1;
5314 highpc = (CORE_ADDR) 0;
5315 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5316
5317 pst->n_global_syms = objfile->global_psymbols.next -
5318 (objfile->global_psymbols.list + pst->globals_offset);
5319 pst->n_static_syms = objfile->static_psymbols.next -
5320 (objfile->static_psymbols.list + pst->statics_offset);
5321 sort_pst_symbols (pst);
5322 }
5323
5324 /* Traversal function for build_type_psymtabs. */
5325
5326 static int
5327 build_type_psymtab_dependencies (void **slot, void *info)
5328 {
5329 struct objfile *objfile = dwarf2_per_objfile->objfile;
5330 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5331 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5332 struct partial_symtab *pst = per_cu->v.psymtab;
5333 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5334 struct dwarf2_per_cu_data *iter;
5335 int i;
5336
5337 gdb_assert (len > 0);
5338
5339 pst->number_of_dependencies = len;
5340 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5341 len * sizeof (struct psymtab *));
5342 for (i = 0;
5343 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5344 ++i)
5345 {
5346 pst->dependencies[i] = iter->v.psymtab;
5347 iter->s.type_unit_group = tu_group;
5348 }
5349
5350 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5351
5352 return 1;
5353 }
5354
5355 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5356 Build partial symbol tables for the .debug_types comp-units. */
5357
5358 static void
5359 build_type_psymtabs (struct objfile *objfile)
5360 {
5361 if (! create_all_type_units (objfile))
5362 return;
5363
5364 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5365
5366 /* Now that all TUs have been processed we can fill in the dependencies. */
5367 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5368 build_type_psymtab_dependencies, NULL);
5369 }
5370
5371 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5372
5373 static void
5374 psymtabs_addrmap_cleanup (void *o)
5375 {
5376 struct objfile *objfile = o;
5377
5378 objfile->psymtabs_addrmap = NULL;
5379 }
5380
5381 /* Compute the 'user' field for each psymtab in OBJFILE. */
5382
5383 static void
5384 set_partial_user (struct objfile *objfile)
5385 {
5386 int i;
5387
5388 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5389 {
5390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5391 struct partial_symtab *pst = per_cu->v.psymtab;
5392 int j;
5393
5394 if (pst == NULL)
5395 continue;
5396
5397 for (j = 0; j < pst->number_of_dependencies; ++j)
5398 {
5399 /* Set the 'user' field only if it is not already set. */
5400 if (pst->dependencies[j]->user == NULL)
5401 pst->dependencies[j]->user = pst;
5402 }
5403 }
5404 }
5405
5406 /* Build the partial symbol table by doing a quick pass through the
5407 .debug_info and .debug_abbrev sections. */
5408
5409 static void
5410 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5411 {
5412 struct cleanup *back_to, *addrmap_cleanup;
5413 struct obstack temp_obstack;
5414 int i;
5415
5416 if (dwarf2_read_debug)
5417 {
5418 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5419 objfile->name);
5420 }
5421
5422 dwarf2_per_objfile->reading_partial_symbols = 1;
5423
5424 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5425
5426 /* Any cached compilation units will be linked by the per-objfile
5427 read_in_chain. Make sure to free them when we're done. */
5428 back_to = make_cleanup (free_cached_comp_units, NULL);
5429
5430 build_type_psymtabs (objfile);
5431
5432 create_all_comp_units (objfile);
5433
5434 /* Create a temporary address map on a temporary obstack. We later
5435 copy this to the final obstack. */
5436 obstack_init (&temp_obstack);
5437 make_cleanup_obstack_free (&temp_obstack);
5438 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5439 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5440
5441 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5442 {
5443 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5444
5445 process_psymtab_comp_unit (per_cu, 0);
5446 }
5447
5448 set_partial_user (objfile);
5449
5450 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5451 &objfile->objfile_obstack);
5452 discard_cleanups (addrmap_cleanup);
5453
5454 do_cleanups (back_to);
5455
5456 if (dwarf2_read_debug)
5457 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5458 objfile->name);
5459 }
5460
5461 /* die_reader_func for load_partial_comp_unit. */
5462
5463 static void
5464 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5465 gdb_byte *info_ptr,
5466 struct die_info *comp_unit_die,
5467 int has_children,
5468 void *data)
5469 {
5470 struct dwarf2_cu *cu = reader->cu;
5471
5472 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5473
5474 /* Check if comp unit has_children.
5475 If so, read the rest of the partial symbols from this comp unit.
5476 If not, there's no more debug_info for this comp unit. */
5477 if (has_children)
5478 load_partial_dies (reader, info_ptr, 0);
5479 }
5480
5481 /* Load the partial DIEs for a secondary CU into memory.
5482 This is also used when rereading a primary CU with load_all_dies. */
5483
5484 static void
5485 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5486 {
5487 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5488 load_partial_comp_unit_reader, NULL);
5489 }
5490
5491 static void
5492 read_comp_units_from_section (struct objfile *objfile,
5493 struct dwarf2_section_info *section,
5494 unsigned int is_dwz,
5495 int *n_allocated,
5496 int *n_comp_units,
5497 struct dwarf2_per_cu_data ***all_comp_units)
5498 {
5499 gdb_byte *info_ptr;
5500 bfd *abfd = section->asection->owner;
5501
5502 dwarf2_read_section (objfile, section);
5503
5504 info_ptr = section->buffer;
5505
5506 while (info_ptr < section->buffer + section->size)
5507 {
5508 unsigned int length, initial_length_size;
5509 struct dwarf2_per_cu_data *this_cu;
5510 sect_offset offset;
5511
5512 offset.sect_off = info_ptr - section->buffer;
5513
5514 /* Read just enough information to find out where the next
5515 compilation unit is. */
5516 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5517
5518 /* Save the compilation unit for later lookup. */
5519 this_cu = obstack_alloc (&objfile->objfile_obstack,
5520 sizeof (struct dwarf2_per_cu_data));
5521 memset (this_cu, 0, sizeof (*this_cu));
5522 this_cu->offset = offset;
5523 this_cu->length = length + initial_length_size;
5524 this_cu->is_dwz = is_dwz;
5525 this_cu->objfile = objfile;
5526 this_cu->info_or_types_section = section;
5527
5528 if (*n_comp_units == *n_allocated)
5529 {
5530 *n_allocated *= 2;
5531 *all_comp_units = xrealloc (*all_comp_units,
5532 *n_allocated
5533 * sizeof (struct dwarf2_per_cu_data *));
5534 }
5535 (*all_comp_units)[*n_comp_units] = this_cu;
5536 ++*n_comp_units;
5537
5538 info_ptr = info_ptr + this_cu->length;
5539 }
5540 }
5541
5542 /* Create a list of all compilation units in OBJFILE.
5543 This is only done for -readnow and building partial symtabs. */
5544
5545 static void
5546 create_all_comp_units (struct objfile *objfile)
5547 {
5548 int n_allocated;
5549 int n_comp_units;
5550 struct dwarf2_per_cu_data **all_comp_units;
5551
5552 n_comp_units = 0;
5553 n_allocated = 10;
5554 all_comp_units = xmalloc (n_allocated
5555 * sizeof (struct dwarf2_per_cu_data *));
5556
5557 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5558 &n_allocated, &n_comp_units, &all_comp_units);
5559
5560 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5561 {
5562 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5563
5564 read_comp_units_from_section (objfile, &dwz->info, 1,
5565 &n_allocated, &n_comp_units,
5566 &all_comp_units);
5567 }
5568
5569 dwarf2_per_objfile->all_comp_units
5570 = obstack_alloc (&objfile->objfile_obstack,
5571 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5572 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5573 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5574 xfree (all_comp_units);
5575 dwarf2_per_objfile->n_comp_units = n_comp_units;
5576 }
5577
5578 /* Process all loaded DIEs for compilation unit CU, starting at
5579 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5580 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5581 DW_AT_ranges). If NEED_PC is set, then this function will set
5582 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5583 and record the covered ranges in the addrmap. */
5584
5585 static void
5586 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5587 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5588 {
5589 struct partial_die_info *pdi;
5590
5591 /* Now, march along the PDI's, descending into ones which have
5592 interesting children but skipping the children of the other ones,
5593 until we reach the end of the compilation unit. */
5594
5595 pdi = first_die;
5596
5597 while (pdi != NULL)
5598 {
5599 fixup_partial_die (pdi, cu);
5600
5601 /* Anonymous namespaces or modules have no name but have interesting
5602 children, so we need to look at them. Ditto for anonymous
5603 enums. */
5604
5605 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5606 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5607 || pdi->tag == DW_TAG_imported_unit)
5608 {
5609 switch (pdi->tag)
5610 {
5611 case DW_TAG_subprogram:
5612 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5613 break;
5614 case DW_TAG_constant:
5615 case DW_TAG_variable:
5616 case DW_TAG_typedef:
5617 case DW_TAG_union_type:
5618 if (!pdi->is_declaration)
5619 {
5620 add_partial_symbol (pdi, cu);
5621 }
5622 break;
5623 case DW_TAG_class_type:
5624 case DW_TAG_interface_type:
5625 case DW_TAG_structure_type:
5626 if (!pdi->is_declaration)
5627 {
5628 add_partial_symbol (pdi, cu);
5629 }
5630 break;
5631 case DW_TAG_enumeration_type:
5632 if (!pdi->is_declaration)
5633 add_partial_enumeration (pdi, cu);
5634 break;
5635 case DW_TAG_base_type:
5636 case DW_TAG_subrange_type:
5637 /* File scope base type definitions are added to the partial
5638 symbol table. */
5639 add_partial_symbol (pdi, cu);
5640 break;
5641 case DW_TAG_namespace:
5642 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5643 break;
5644 case DW_TAG_module:
5645 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5646 break;
5647 case DW_TAG_imported_unit:
5648 {
5649 struct dwarf2_per_cu_data *per_cu;
5650
5651 /* For now we don't handle imported units in type units. */
5652 if (cu->per_cu->is_debug_types)
5653 {
5654 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5655 " supported in type units [in module %s]"),
5656 cu->objfile->name);
5657 }
5658
5659 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5660 pdi->is_dwz,
5661 cu->objfile);
5662
5663 /* Go read the partial unit, if needed. */
5664 if (per_cu->v.psymtab == NULL)
5665 process_psymtab_comp_unit (per_cu, 1);
5666
5667 VEC_safe_push (dwarf2_per_cu_ptr,
5668 cu->per_cu->s.imported_symtabs, per_cu);
5669 }
5670 break;
5671 default:
5672 break;
5673 }
5674 }
5675
5676 /* If the die has a sibling, skip to the sibling. */
5677
5678 pdi = pdi->die_sibling;
5679 }
5680 }
5681
5682 /* Functions used to compute the fully scoped name of a partial DIE.
5683
5684 Normally, this is simple. For C++, the parent DIE's fully scoped
5685 name is concatenated with "::" and the partial DIE's name. For
5686 Java, the same thing occurs except that "." is used instead of "::".
5687 Enumerators are an exception; they use the scope of their parent
5688 enumeration type, i.e. the name of the enumeration type is not
5689 prepended to the enumerator.
5690
5691 There are two complexities. One is DW_AT_specification; in this
5692 case "parent" means the parent of the target of the specification,
5693 instead of the direct parent of the DIE. The other is compilers
5694 which do not emit DW_TAG_namespace; in this case we try to guess
5695 the fully qualified name of structure types from their members'
5696 linkage names. This must be done using the DIE's children rather
5697 than the children of any DW_AT_specification target. We only need
5698 to do this for structures at the top level, i.e. if the target of
5699 any DW_AT_specification (if any; otherwise the DIE itself) does not
5700 have a parent. */
5701
5702 /* Compute the scope prefix associated with PDI's parent, in
5703 compilation unit CU. The result will be allocated on CU's
5704 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5705 field. NULL is returned if no prefix is necessary. */
5706 static char *
5707 partial_die_parent_scope (struct partial_die_info *pdi,
5708 struct dwarf2_cu *cu)
5709 {
5710 char *grandparent_scope;
5711 struct partial_die_info *parent, *real_pdi;
5712
5713 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5714 then this means the parent of the specification DIE. */
5715
5716 real_pdi = pdi;
5717 while (real_pdi->has_specification)
5718 real_pdi = find_partial_die (real_pdi->spec_offset,
5719 real_pdi->spec_is_dwz, cu);
5720
5721 parent = real_pdi->die_parent;
5722 if (parent == NULL)
5723 return NULL;
5724
5725 if (parent->scope_set)
5726 return parent->scope;
5727
5728 fixup_partial_die (parent, cu);
5729
5730 grandparent_scope = partial_die_parent_scope (parent, cu);
5731
5732 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5733 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5734 Work around this problem here. */
5735 if (cu->language == language_cplus
5736 && parent->tag == DW_TAG_namespace
5737 && strcmp (parent->name, "::") == 0
5738 && grandparent_scope == NULL)
5739 {
5740 parent->scope = NULL;
5741 parent->scope_set = 1;
5742 return NULL;
5743 }
5744
5745 if (pdi->tag == DW_TAG_enumerator)
5746 /* Enumerators should not get the name of the enumeration as a prefix. */
5747 parent->scope = grandparent_scope;
5748 else if (parent->tag == DW_TAG_namespace
5749 || parent->tag == DW_TAG_module
5750 || parent->tag == DW_TAG_structure_type
5751 || parent->tag == DW_TAG_class_type
5752 || parent->tag == DW_TAG_interface_type
5753 || parent->tag == DW_TAG_union_type
5754 || parent->tag == DW_TAG_enumeration_type)
5755 {
5756 if (grandparent_scope == NULL)
5757 parent->scope = parent->name;
5758 else
5759 parent->scope = typename_concat (&cu->comp_unit_obstack,
5760 grandparent_scope,
5761 parent->name, 0, cu);
5762 }
5763 else
5764 {
5765 /* FIXME drow/2004-04-01: What should we be doing with
5766 function-local names? For partial symbols, we should probably be
5767 ignoring them. */
5768 complaint (&symfile_complaints,
5769 _("unhandled containing DIE tag %d for DIE at %d"),
5770 parent->tag, pdi->offset.sect_off);
5771 parent->scope = grandparent_scope;
5772 }
5773
5774 parent->scope_set = 1;
5775 return parent->scope;
5776 }
5777
5778 /* Return the fully scoped name associated with PDI, from compilation unit
5779 CU. The result will be allocated with malloc. */
5780
5781 static char *
5782 partial_die_full_name (struct partial_die_info *pdi,
5783 struct dwarf2_cu *cu)
5784 {
5785 char *parent_scope;
5786
5787 /* If this is a template instantiation, we can not work out the
5788 template arguments from partial DIEs. So, unfortunately, we have
5789 to go through the full DIEs. At least any work we do building
5790 types here will be reused if full symbols are loaded later. */
5791 if (pdi->has_template_arguments)
5792 {
5793 fixup_partial_die (pdi, cu);
5794
5795 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5796 {
5797 struct die_info *die;
5798 struct attribute attr;
5799 struct dwarf2_cu *ref_cu = cu;
5800
5801 /* DW_FORM_ref_addr is using section offset. */
5802 attr.name = 0;
5803 attr.form = DW_FORM_ref_addr;
5804 attr.u.unsnd = pdi->offset.sect_off;
5805 die = follow_die_ref (NULL, &attr, &ref_cu);
5806
5807 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5808 }
5809 }
5810
5811 parent_scope = partial_die_parent_scope (pdi, cu);
5812 if (parent_scope == NULL)
5813 return NULL;
5814 else
5815 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5816 }
5817
5818 static void
5819 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5820 {
5821 struct objfile *objfile = cu->objfile;
5822 CORE_ADDR addr = 0;
5823 char *actual_name = NULL;
5824 CORE_ADDR baseaddr;
5825 int built_actual_name = 0;
5826
5827 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5828
5829 actual_name = partial_die_full_name (pdi, cu);
5830 if (actual_name)
5831 built_actual_name = 1;
5832
5833 if (actual_name == NULL)
5834 actual_name = pdi->name;
5835
5836 switch (pdi->tag)
5837 {
5838 case DW_TAG_subprogram:
5839 if (pdi->is_external || cu->language == language_ada)
5840 {
5841 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5842 of the global scope. But in Ada, we want to be able to access
5843 nested procedures globally. So all Ada subprograms are stored
5844 in the global scope. */
5845 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5846 mst_text, objfile); */
5847 add_psymbol_to_list (actual_name, strlen (actual_name),
5848 built_actual_name,
5849 VAR_DOMAIN, LOC_BLOCK,
5850 &objfile->global_psymbols,
5851 0, pdi->lowpc + baseaddr,
5852 cu->language, objfile);
5853 }
5854 else
5855 {
5856 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5857 mst_file_text, objfile); */
5858 add_psymbol_to_list (actual_name, strlen (actual_name),
5859 built_actual_name,
5860 VAR_DOMAIN, LOC_BLOCK,
5861 &objfile->static_psymbols,
5862 0, pdi->lowpc + baseaddr,
5863 cu->language, objfile);
5864 }
5865 break;
5866 case DW_TAG_constant:
5867 {
5868 struct psymbol_allocation_list *list;
5869
5870 if (pdi->is_external)
5871 list = &objfile->global_psymbols;
5872 else
5873 list = &objfile->static_psymbols;
5874 add_psymbol_to_list (actual_name, strlen (actual_name),
5875 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5876 list, 0, 0, cu->language, objfile);
5877 }
5878 break;
5879 case DW_TAG_variable:
5880 if (pdi->d.locdesc)
5881 addr = decode_locdesc (pdi->d.locdesc, cu);
5882
5883 if (pdi->d.locdesc
5884 && addr == 0
5885 && !dwarf2_per_objfile->has_section_at_zero)
5886 {
5887 /* A global or static variable may also have been stripped
5888 out by the linker if unused, in which case its address
5889 will be nullified; do not add such variables into partial
5890 symbol table then. */
5891 }
5892 else if (pdi->is_external)
5893 {
5894 /* Global Variable.
5895 Don't enter into the minimal symbol tables as there is
5896 a minimal symbol table entry from the ELF symbols already.
5897 Enter into partial symbol table if it has a location
5898 descriptor or a type.
5899 If the location descriptor is missing, new_symbol will create
5900 a LOC_UNRESOLVED symbol, the address of the variable will then
5901 be determined from the minimal symbol table whenever the variable
5902 is referenced.
5903 The address for the partial symbol table entry is not
5904 used by GDB, but it comes in handy for debugging partial symbol
5905 table building. */
5906
5907 if (pdi->d.locdesc || pdi->has_type)
5908 add_psymbol_to_list (actual_name, strlen (actual_name),
5909 built_actual_name,
5910 VAR_DOMAIN, LOC_STATIC,
5911 &objfile->global_psymbols,
5912 0, addr + baseaddr,
5913 cu->language, objfile);
5914 }
5915 else
5916 {
5917 /* Static Variable. Skip symbols without location descriptors. */
5918 if (pdi->d.locdesc == NULL)
5919 {
5920 if (built_actual_name)
5921 xfree (actual_name);
5922 return;
5923 }
5924 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5925 mst_file_data, objfile); */
5926 add_psymbol_to_list (actual_name, strlen (actual_name),
5927 built_actual_name,
5928 VAR_DOMAIN, LOC_STATIC,
5929 &objfile->static_psymbols,
5930 0, addr + baseaddr,
5931 cu->language, objfile);
5932 }
5933 break;
5934 case DW_TAG_typedef:
5935 case DW_TAG_base_type:
5936 case DW_TAG_subrange_type:
5937 add_psymbol_to_list (actual_name, strlen (actual_name),
5938 built_actual_name,
5939 VAR_DOMAIN, LOC_TYPEDEF,
5940 &objfile->static_psymbols,
5941 0, (CORE_ADDR) 0, cu->language, objfile);
5942 break;
5943 case DW_TAG_namespace:
5944 add_psymbol_to_list (actual_name, strlen (actual_name),
5945 built_actual_name,
5946 VAR_DOMAIN, LOC_TYPEDEF,
5947 &objfile->global_psymbols,
5948 0, (CORE_ADDR) 0, cu->language, objfile);
5949 break;
5950 case DW_TAG_class_type:
5951 case DW_TAG_interface_type:
5952 case DW_TAG_structure_type:
5953 case DW_TAG_union_type:
5954 case DW_TAG_enumeration_type:
5955 /* Skip external references. The DWARF standard says in the section
5956 about "Structure, Union, and Class Type Entries": "An incomplete
5957 structure, union or class type is represented by a structure,
5958 union or class entry that does not have a byte size attribute
5959 and that has a DW_AT_declaration attribute." */
5960 if (!pdi->has_byte_size && pdi->is_declaration)
5961 {
5962 if (built_actual_name)
5963 xfree (actual_name);
5964 return;
5965 }
5966
5967 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5968 static vs. global. */
5969 add_psymbol_to_list (actual_name, strlen (actual_name),
5970 built_actual_name,
5971 STRUCT_DOMAIN, LOC_TYPEDEF,
5972 (cu->language == language_cplus
5973 || cu->language == language_java)
5974 ? &objfile->global_psymbols
5975 : &objfile->static_psymbols,
5976 0, (CORE_ADDR) 0, cu->language, objfile);
5977
5978 break;
5979 case DW_TAG_enumerator:
5980 add_psymbol_to_list (actual_name, strlen (actual_name),
5981 built_actual_name,
5982 VAR_DOMAIN, LOC_CONST,
5983 (cu->language == language_cplus
5984 || cu->language == language_java)
5985 ? &objfile->global_psymbols
5986 : &objfile->static_psymbols,
5987 0, (CORE_ADDR) 0, cu->language, objfile);
5988 break;
5989 default:
5990 break;
5991 }
5992
5993 if (built_actual_name)
5994 xfree (actual_name);
5995 }
5996
5997 /* Read a partial die corresponding to a namespace; also, add a symbol
5998 corresponding to that namespace to the symbol table. NAMESPACE is
5999 the name of the enclosing namespace. */
6000
6001 static void
6002 add_partial_namespace (struct partial_die_info *pdi,
6003 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6004 int need_pc, struct dwarf2_cu *cu)
6005 {
6006 /* Add a symbol for the namespace. */
6007
6008 add_partial_symbol (pdi, cu);
6009
6010 /* Now scan partial symbols in that namespace. */
6011
6012 if (pdi->has_children)
6013 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6014 }
6015
6016 /* Read a partial die corresponding to a Fortran module. */
6017
6018 static void
6019 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6020 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6021 {
6022 /* Now scan partial symbols in that module. */
6023
6024 if (pdi->has_children)
6025 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6026 }
6027
6028 /* Read a partial die corresponding to a subprogram and create a partial
6029 symbol for that subprogram. When the CU language allows it, this
6030 routine also defines a partial symbol for each nested subprogram
6031 that this subprogram contains.
6032
6033 DIE my also be a lexical block, in which case we simply search
6034 recursively for suprograms defined inside that lexical block.
6035 Again, this is only performed when the CU language allows this
6036 type of definitions. */
6037
6038 static void
6039 add_partial_subprogram (struct partial_die_info *pdi,
6040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6041 int need_pc, struct dwarf2_cu *cu)
6042 {
6043 if (pdi->tag == DW_TAG_subprogram)
6044 {
6045 if (pdi->has_pc_info)
6046 {
6047 if (pdi->lowpc < *lowpc)
6048 *lowpc = pdi->lowpc;
6049 if (pdi->highpc > *highpc)
6050 *highpc = pdi->highpc;
6051 if (need_pc)
6052 {
6053 CORE_ADDR baseaddr;
6054 struct objfile *objfile = cu->objfile;
6055
6056 baseaddr = ANOFFSET (objfile->section_offsets,
6057 SECT_OFF_TEXT (objfile));
6058 addrmap_set_empty (objfile->psymtabs_addrmap,
6059 pdi->lowpc + baseaddr,
6060 pdi->highpc - 1 + baseaddr,
6061 cu->per_cu->v.psymtab);
6062 }
6063 }
6064
6065 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6066 {
6067 if (!pdi->is_declaration)
6068 /* Ignore subprogram DIEs that do not have a name, they are
6069 illegal. Do not emit a complaint at this point, we will
6070 do so when we convert this psymtab into a symtab. */
6071 if (pdi->name)
6072 add_partial_symbol (pdi, cu);
6073 }
6074 }
6075
6076 if (! pdi->has_children)
6077 return;
6078
6079 if (cu->language == language_ada)
6080 {
6081 pdi = pdi->die_child;
6082 while (pdi != NULL)
6083 {
6084 fixup_partial_die (pdi, cu);
6085 if (pdi->tag == DW_TAG_subprogram
6086 || pdi->tag == DW_TAG_lexical_block)
6087 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6088 pdi = pdi->die_sibling;
6089 }
6090 }
6091 }
6092
6093 /* Read a partial die corresponding to an enumeration type. */
6094
6095 static void
6096 add_partial_enumeration (struct partial_die_info *enum_pdi,
6097 struct dwarf2_cu *cu)
6098 {
6099 struct partial_die_info *pdi;
6100
6101 if (enum_pdi->name != NULL)
6102 add_partial_symbol (enum_pdi, cu);
6103
6104 pdi = enum_pdi->die_child;
6105 while (pdi)
6106 {
6107 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6108 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6109 else
6110 add_partial_symbol (pdi, cu);
6111 pdi = pdi->die_sibling;
6112 }
6113 }
6114
6115 /* Return the initial uleb128 in the die at INFO_PTR. */
6116
6117 static unsigned int
6118 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6119 {
6120 unsigned int bytes_read;
6121
6122 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6123 }
6124
6125 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6126 Return the corresponding abbrev, or NULL if the number is zero (indicating
6127 an empty DIE). In either case *BYTES_READ will be set to the length of
6128 the initial number. */
6129
6130 static struct abbrev_info *
6131 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6132 struct dwarf2_cu *cu)
6133 {
6134 bfd *abfd = cu->objfile->obfd;
6135 unsigned int abbrev_number;
6136 struct abbrev_info *abbrev;
6137
6138 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6139
6140 if (abbrev_number == 0)
6141 return NULL;
6142
6143 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6144 if (!abbrev)
6145 {
6146 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6147 abbrev_number, bfd_get_filename (abfd));
6148 }
6149
6150 return abbrev;
6151 }
6152
6153 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6154 Returns a pointer to the end of a series of DIEs, terminated by an empty
6155 DIE. Any children of the skipped DIEs will also be skipped. */
6156
6157 static gdb_byte *
6158 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6159 {
6160 struct dwarf2_cu *cu = reader->cu;
6161 struct abbrev_info *abbrev;
6162 unsigned int bytes_read;
6163
6164 while (1)
6165 {
6166 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6167 if (abbrev == NULL)
6168 return info_ptr + bytes_read;
6169 else
6170 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6171 }
6172 }
6173
6174 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6175 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6176 abbrev corresponding to that skipped uleb128 should be passed in
6177 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6178 children. */
6179
6180 static gdb_byte *
6181 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6182 struct abbrev_info *abbrev)
6183 {
6184 unsigned int bytes_read;
6185 struct attribute attr;
6186 bfd *abfd = reader->abfd;
6187 struct dwarf2_cu *cu = reader->cu;
6188 gdb_byte *buffer = reader->buffer;
6189 const gdb_byte *buffer_end = reader->buffer_end;
6190 gdb_byte *start_info_ptr = info_ptr;
6191 unsigned int form, i;
6192
6193 for (i = 0; i < abbrev->num_attrs; i++)
6194 {
6195 /* The only abbrev we care about is DW_AT_sibling. */
6196 if (abbrev->attrs[i].name == DW_AT_sibling)
6197 {
6198 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6199 if (attr.form == DW_FORM_ref_addr)
6200 complaint (&symfile_complaints,
6201 _("ignoring absolute DW_AT_sibling"));
6202 else
6203 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6204 }
6205
6206 /* If it isn't DW_AT_sibling, skip this attribute. */
6207 form = abbrev->attrs[i].form;
6208 skip_attribute:
6209 switch (form)
6210 {
6211 case DW_FORM_ref_addr:
6212 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6213 and later it is offset sized. */
6214 if (cu->header.version == 2)
6215 info_ptr += cu->header.addr_size;
6216 else
6217 info_ptr += cu->header.offset_size;
6218 break;
6219 case DW_FORM_GNU_ref_alt:
6220 info_ptr += cu->header.offset_size;
6221 break;
6222 case DW_FORM_addr:
6223 info_ptr += cu->header.addr_size;
6224 break;
6225 case DW_FORM_data1:
6226 case DW_FORM_ref1:
6227 case DW_FORM_flag:
6228 info_ptr += 1;
6229 break;
6230 case DW_FORM_flag_present:
6231 break;
6232 case DW_FORM_data2:
6233 case DW_FORM_ref2:
6234 info_ptr += 2;
6235 break;
6236 case DW_FORM_data4:
6237 case DW_FORM_ref4:
6238 info_ptr += 4;
6239 break;
6240 case DW_FORM_data8:
6241 case DW_FORM_ref8:
6242 case DW_FORM_ref_sig8:
6243 info_ptr += 8;
6244 break;
6245 case DW_FORM_string:
6246 read_direct_string (abfd, info_ptr, &bytes_read);
6247 info_ptr += bytes_read;
6248 break;
6249 case DW_FORM_sec_offset:
6250 case DW_FORM_strp:
6251 case DW_FORM_GNU_strp_alt:
6252 info_ptr += cu->header.offset_size;
6253 break;
6254 case DW_FORM_exprloc:
6255 case DW_FORM_block:
6256 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6257 info_ptr += bytes_read;
6258 break;
6259 case DW_FORM_block1:
6260 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6261 break;
6262 case DW_FORM_block2:
6263 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6264 break;
6265 case DW_FORM_block4:
6266 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6267 break;
6268 case DW_FORM_sdata:
6269 case DW_FORM_udata:
6270 case DW_FORM_ref_udata:
6271 case DW_FORM_GNU_addr_index:
6272 case DW_FORM_GNU_str_index:
6273 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6274 break;
6275 case DW_FORM_indirect:
6276 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6277 info_ptr += bytes_read;
6278 /* We need to continue parsing from here, so just go back to
6279 the top. */
6280 goto skip_attribute;
6281
6282 default:
6283 error (_("Dwarf Error: Cannot handle %s "
6284 "in DWARF reader [in module %s]"),
6285 dwarf_form_name (form),
6286 bfd_get_filename (abfd));
6287 }
6288 }
6289
6290 if (abbrev->has_children)
6291 return skip_children (reader, info_ptr);
6292 else
6293 return info_ptr;
6294 }
6295
6296 /* Locate ORIG_PDI's sibling.
6297 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6298
6299 static gdb_byte *
6300 locate_pdi_sibling (const struct die_reader_specs *reader,
6301 struct partial_die_info *orig_pdi,
6302 gdb_byte *info_ptr)
6303 {
6304 /* Do we know the sibling already? */
6305
6306 if (orig_pdi->sibling)
6307 return orig_pdi->sibling;
6308
6309 /* Are there any children to deal with? */
6310
6311 if (!orig_pdi->has_children)
6312 return info_ptr;
6313
6314 /* Skip the children the long way. */
6315
6316 return skip_children (reader, info_ptr);
6317 }
6318
6319 /* Expand this partial symbol table into a full symbol table. */
6320
6321 static void
6322 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6323 {
6324 if (pst != NULL)
6325 {
6326 if (pst->readin)
6327 {
6328 warning (_("bug: psymtab for %s is already read in."),
6329 pst->filename);
6330 }
6331 else
6332 {
6333 if (info_verbose)
6334 {
6335 printf_filtered (_("Reading in symbols for %s..."),
6336 pst->filename);
6337 gdb_flush (gdb_stdout);
6338 }
6339
6340 /* Restore our global data. */
6341 dwarf2_per_objfile = objfile_data (pst->objfile,
6342 dwarf2_objfile_data_key);
6343
6344 /* If this psymtab is constructed from a debug-only objfile, the
6345 has_section_at_zero flag will not necessarily be correct. We
6346 can get the correct value for this flag by looking at the data
6347 associated with the (presumably stripped) associated objfile. */
6348 if (pst->objfile->separate_debug_objfile_backlink)
6349 {
6350 struct dwarf2_per_objfile *dpo_backlink
6351 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6352 dwarf2_objfile_data_key);
6353
6354 dwarf2_per_objfile->has_section_at_zero
6355 = dpo_backlink->has_section_at_zero;
6356 }
6357
6358 dwarf2_per_objfile->reading_partial_symbols = 0;
6359
6360 psymtab_to_symtab_1 (pst);
6361
6362 /* Finish up the debug error message. */
6363 if (info_verbose)
6364 printf_filtered (_("done.\n"));
6365 }
6366 }
6367
6368 process_cu_includes ();
6369 }
6370 \f
6371 /* Reading in full CUs. */
6372
6373 /* Add PER_CU to the queue. */
6374
6375 static void
6376 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6377 enum language pretend_language)
6378 {
6379 struct dwarf2_queue_item *item;
6380
6381 per_cu->queued = 1;
6382 item = xmalloc (sizeof (*item));
6383 item->per_cu = per_cu;
6384 item->pretend_language = pretend_language;
6385 item->next = NULL;
6386
6387 if (dwarf2_queue == NULL)
6388 dwarf2_queue = item;
6389 else
6390 dwarf2_queue_tail->next = item;
6391
6392 dwarf2_queue_tail = item;
6393 }
6394
6395 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6396 unit and add it to our queue.
6397 The result is non-zero if PER_CU was queued, otherwise the result is zero
6398 meaning either PER_CU is already queued or it is already loaded. */
6399
6400 static int
6401 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6402 struct dwarf2_per_cu_data *per_cu,
6403 enum language pretend_language)
6404 {
6405 /* We may arrive here during partial symbol reading, if we need full
6406 DIEs to process an unusual case (e.g. template arguments). Do
6407 not queue PER_CU, just tell our caller to load its DIEs. */
6408 if (dwarf2_per_objfile->reading_partial_symbols)
6409 {
6410 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6411 return 1;
6412 return 0;
6413 }
6414
6415 /* Mark the dependence relation so that we don't flush PER_CU
6416 too early. */
6417 dwarf2_add_dependence (this_cu, per_cu);
6418
6419 /* If it's already on the queue, we have nothing to do. */
6420 if (per_cu->queued)
6421 return 0;
6422
6423 /* If the compilation unit is already loaded, just mark it as
6424 used. */
6425 if (per_cu->cu != NULL)
6426 {
6427 per_cu->cu->last_used = 0;
6428 return 0;
6429 }
6430
6431 /* Add it to the queue. */
6432 queue_comp_unit (per_cu, pretend_language);
6433
6434 return 1;
6435 }
6436
6437 /* Process the queue. */
6438
6439 static void
6440 process_queue (void)
6441 {
6442 struct dwarf2_queue_item *item, *next_item;
6443
6444 if (dwarf2_read_debug)
6445 {
6446 fprintf_unfiltered (gdb_stdlog,
6447 "Expanding one or more symtabs of objfile %s ...\n",
6448 dwarf2_per_objfile->objfile->name);
6449 }
6450
6451 /* The queue starts out with one item, but following a DIE reference
6452 may load a new CU, adding it to the end of the queue. */
6453 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6454 {
6455 if (dwarf2_per_objfile->using_index
6456 ? !item->per_cu->v.quick->symtab
6457 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6458 {
6459 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6460
6461 if (dwarf2_read_debug)
6462 {
6463 fprintf_unfiltered (gdb_stdlog,
6464 "Expanding symtab of %s at offset 0x%x\n",
6465 per_cu->is_debug_types ? "TU" : "CU",
6466 per_cu->offset.sect_off);
6467 }
6468
6469 if (per_cu->is_debug_types)
6470 process_full_type_unit (per_cu, item->pretend_language);
6471 else
6472 process_full_comp_unit (per_cu, item->pretend_language);
6473
6474 if (dwarf2_read_debug)
6475 {
6476 fprintf_unfiltered (gdb_stdlog,
6477 "Done expanding %s at offset 0x%x\n",
6478 per_cu->is_debug_types ? "TU" : "CU",
6479 per_cu->offset.sect_off);
6480 }
6481 }
6482
6483 item->per_cu->queued = 0;
6484 next_item = item->next;
6485 xfree (item);
6486 }
6487
6488 dwarf2_queue_tail = NULL;
6489
6490 if (dwarf2_read_debug)
6491 {
6492 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6493 dwarf2_per_objfile->objfile->name);
6494 }
6495 }
6496
6497 /* Free all allocated queue entries. This function only releases anything if
6498 an error was thrown; if the queue was processed then it would have been
6499 freed as we went along. */
6500
6501 static void
6502 dwarf2_release_queue (void *dummy)
6503 {
6504 struct dwarf2_queue_item *item, *last;
6505
6506 item = dwarf2_queue;
6507 while (item)
6508 {
6509 /* Anything still marked queued is likely to be in an
6510 inconsistent state, so discard it. */
6511 if (item->per_cu->queued)
6512 {
6513 if (item->per_cu->cu != NULL)
6514 free_one_cached_comp_unit (item->per_cu);
6515 item->per_cu->queued = 0;
6516 }
6517
6518 last = item;
6519 item = item->next;
6520 xfree (last);
6521 }
6522
6523 dwarf2_queue = dwarf2_queue_tail = NULL;
6524 }
6525
6526 /* Read in full symbols for PST, and anything it depends on. */
6527
6528 static void
6529 psymtab_to_symtab_1 (struct partial_symtab *pst)
6530 {
6531 struct dwarf2_per_cu_data *per_cu;
6532 int i;
6533
6534 if (pst->readin)
6535 return;
6536
6537 for (i = 0; i < pst->number_of_dependencies; i++)
6538 if (!pst->dependencies[i]->readin
6539 && pst->dependencies[i]->user == NULL)
6540 {
6541 /* Inform about additional files that need to be read in. */
6542 if (info_verbose)
6543 {
6544 /* FIXME: i18n: Need to make this a single string. */
6545 fputs_filtered (" ", gdb_stdout);
6546 wrap_here ("");
6547 fputs_filtered ("and ", gdb_stdout);
6548 wrap_here ("");
6549 printf_filtered ("%s...", pst->dependencies[i]->filename);
6550 wrap_here (""); /* Flush output. */
6551 gdb_flush (gdb_stdout);
6552 }
6553 psymtab_to_symtab_1 (pst->dependencies[i]);
6554 }
6555
6556 per_cu = pst->read_symtab_private;
6557
6558 if (per_cu == NULL)
6559 {
6560 /* It's an include file, no symbols to read for it.
6561 Everything is in the parent symtab. */
6562 pst->readin = 1;
6563 return;
6564 }
6565
6566 dw2_do_instantiate_symtab (per_cu);
6567 }
6568
6569 /* Trivial hash function for die_info: the hash value of a DIE
6570 is its offset in .debug_info for this objfile. */
6571
6572 static hashval_t
6573 die_hash (const void *item)
6574 {
6575 const struct die_info *die = item;
6576
6577 return die->offset.sect_off;
6578 }
6579
6580 /* Trivial comparison function for die_info structures: two DIEs
6581 are equal if they have the same offset. */
6582
6583 static int
6584 die_eq (const void *item_lhs, const void *item_rhs)
6585 {
6586 const struct die_info *die_lhs = item_lhs;
6587 const struct die_info *die_rhs = item_rhs;
6588
6589 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6590 }
6591
6592 /* die_reader_func for load_full_comp_unit.
6593 This is identical to read_signatured_type_reader,
6594 but is kept separate for now. */
6595
6596 static void
6597 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6598 gdb_byte *info_ptr,
6599 struct die_info *comp_unit_die,
6600 int has_children,
6601 void *data)
6602 {
6603 struct dwarf2_cu *cu = reader->cu;
6604 enum language *language_ptr = data;
6605
6606 gdb_assert (cu->die_hash == NULL);
6607 cu->die_hash =
6608 htab_create_alloc_ex (cu->header.length / 12,
6609 die_hash,
6610 die_eq,
6611 NULL,
6612 &cu->comp_unit_obstack,
6613 hashtab_obstack_allocate,
6614 dummy_obstack_deallocate);
6615
6616 if (has_children)
6617 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6618 &info_ptr, comp_unit_die);
6619 cu->dies = comp_unit_die;
6620 /* comp_unit_die is not stored in die_hash, no need. */
6621
6622 /* We try not to read any attributes in this function, because not
6623 all CUs needed for references have been loaded yet, and symbol
6624 table processing isn't initialized. But we have to set the CU language,
6625 or we won't be able to build types correctly.
6626 Similarly, if we do not read the producer, we can not apply
6627 producer-specific interpretation. */
6628 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6629 }
6630
6631 /* Load the DIEs associated with PER_CU into memory. */
6632
6633 static void
6634 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6635 enum language pretend_language)
6636 {
6637 gdb_assert (! this_cu->is_debug_types);
6638
6639 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6640 load_full_comp_unit_reader, &pretend_language);
6641 }
6642
6643 /* Add a DIE to the delayed physname list. */
6644
6645 static void
6646 add_to_method_list (struct type *type, int fnfield_index, int index,
6647 const char *name, struct die_info *die,
6648 struct dwarf2_cu *cu)
6649 {
6650 struct delayed_method_info mi;
6651 mi.type = type;
6652 mi.fnfield_index = fnfield_index;
6653 mi.index = index;
6654 mi.name = name;
6655 mi.die = die;
6656 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6657 }
6658
6659 /* A cleanup for freeing the delayed method list. */
6660
6661 static void
6662 free_delayed_list (void *ptr)
6663 {
6664 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6665 if (cu->method_list != NULL)
6666 {
6667 VEC_free (delayed_method_info, cu->method_list);
6668 cu->method_list = NULL;
6669 }
6670 }
6671
6672 /* Compute the physnames of any methods on the CU's method list.
6673
6674 The computation of method physnames is delayed in order to avoid the
6675 (bad) condition that one of the method's formal parameters is of an as yet
6676 incomplete type. */
6677
6678 static void
6679 compute_delayed_physnames (struct dwarf2_cu *cu)
6680 {
6681 int i;
6682 struct delayed_method_info *mi;
6683 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6684 {
6685 const char *physname;
6686 struct fn_fieldlist *fn_flp
6687 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6688 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6689 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6690 }
6691 }
6692
6693 /* Go objects should be embedded in a DW_TAG_module DIE,
6694 and it's not clear if/how imported objects will appear.
6695 To keep Go support simple until that's worked out,
6696 go back through what we've read and create something usable.
6697 We could do this while processing each DIE, and feels kinda cleaner,
6698 but that way is more invasive.
6699 This is to, for example, allow the user to type "p var" or "b main"
6700 without having to specify the package name, and allow lookups
6701 of module.object to work in contexts that use the expression
6702 parser. */
6703
6704 static void
6705 fixup_go_packaging (struct dwarf2_cu *cu)
6706 {
6707 char *package_name = NULL;
6708 struct pending *list;
6709 int i;
6710
6711 for (list = global_symbols; list != NULL; list = list->next)
6712 {
6713 for (i = 0; i < list->nsyms; ++i)
6714 {
6715 struct symbol *sym = list->symbol[i];
6716
6717 if (SYMBOL_LANGUAGE (sym) == language_go
6718 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6719 {
6720 char *this_package_name = go_symbol_package_name (sym);
6721
6722 if (this_package_name == NULL)
6723 continue;
6724 if (package_name == NULL)
6725 package_name = this_package_name;
6726 else
6727 {
6728 if (strcmp (package_name, this_package_name) != 0)
6729 complaint (&symfile_complaints,
6730 _("Symtab %s has objects from two different Go packages: %s and %s"),
6731 (SYMBOL_SYMTAB (sym)
6732 && SYMBOL_SYMTAB (sym)->filename
6733 ? SYMBOL_SYMTAB (sym)->filename
6734 : cu->objfile->name),
6735 this_package_name, package_name);
6736 xfree (this_package_name);
6737 }
6738 }
6739 }
6740 }
6741
6742 if (package_name != NULL)
6743 {
6744 struct objfile *objfile = cu->objfile;
6745 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6746 package_name, objfile);
6747 struct symbol *sym;
6748
6749 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6750
6751 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6752 SYMBOL_SET_LANGUAGE (sym, language_go);
6753 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6754 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6755 e.g., "main" finds the "main" module and not C's main(). */
6756 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6757 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6758 SYMBOL_TYPE (sym) = type;
6759
6760 add_symbol_to_list (sym, &global_symbols);
6761
6762 xfree (package_name);
6763 }
6764 }
6765
6766 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6767
6768 /* Return the symtab for PER_CU. This works properly regardless of
6769 whether we're using the index or psymtabs. */
6770
6771 static struct symtab *
6772 get_symtab (struct dwarf2_per_cu_data *per_cu)
6773 {
6774 return (dwarf2_per_objfile->using_index
6775 ? per_cu->v.quick->symtab
6776 : per_cu->v.psymtab->symtab);
6777 }
6778
6779 /* A helper function for computing the list of all symbol tables
6780 included by PER_CU. */
6781
6782 static void
6783 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6784 htab_t all_children,
6785 struct dwarf2_per_cu_data *per_cu)
6786 {
6787 void **slot;
6788 int ix;
6789 struct dwarf2_per_cu_data *iter;
6790
6791 slot = htab_find_slot (all_children, per_cu, INSERT);
6792 if (*slot != NULL)
6793 {
6794 /* This inclusion and its children have been processed. */
6795 return;
6796 }
6797
6798 *slot = per_cu;
6799 /* Only add a CU if it has a symbol table. */
6800 if (get_symtab (per_cu) != NULL)
6801 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6802
6803 for (ix = 0;
6804 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6805 ++ix)
6806 recursively_compute_inclusions (result, all_children, iter);
6807 }
6808
6809 /* Compute the symtab 'includes' fields for the symtab related to
6810 PER_CU. */
6811
6812 static void
6813 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6814 {
6815 gdb_assert (! per_cu->is_debug_types);
6816
6817 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6818 {
6819 int ix, len;
6820 struct dwarf2_per_cu_data *iter;
6821 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6822 htab_t all_children;
6823 struct symtab *symtab = get_symtab (per_cu);
6824
6825 /* If we don't have a symtab, we can just skip this case. */
6826 if (symtab == NULL)
6827 return;
6828
6829 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6830 NULL, xcalloc, xfree);
6831
6832 for (ix = 0;
6833 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6834 ix, iter);
6835 ++ix)
6836 recursively_compute_inclusions (&result_children, all_children, iter);
6837
6838 /* Now we have a transitive closure of all the included CUs, so
6839 we can convert it to a list of symtabs. */
6840 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6841 symtab->includes
6842 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6843 (len + 1) * sizeof (struct symtab *));
6844 for (ix = 0;
6845 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6846 ++ix)
6847 symtab->includes[ix] = get_symtab (iter);
6848 symtab->includes[len] = NULL;
6849
6850 VEC_free (dwarf2_per_cu_ptr, result_children);
6851 htab_delete (all_children);
6852 }
6853 }
6854
6855 /* Compute the 'includes' field for the symtabs of all the CUs we just
6856 read. */
6857
6858 static void
6859 process_cu_includes (void)
6860 {
6861 int ix;
6862 struct dwarf2_per_cu_data *iter;
6863
6864 for (ix = 0;
6865 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6866 ix, iter);
6867 ++ix)
6868 {
6869 if (! iter->is_debug_types)
6870 compute_symtab_includes (iter);
6871 }
6872
6873 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6874 }
6875
6876 /* Generate full symbol information for PER_CU, whose DIEs have
6877 already been loaded into memory. */
6878
6879 static void
6880 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6881 enum language pretend_language)
6882 {
6883 struct dwarf2_cu *cu = per_cu->cu;
6884 struct objfile *objfile = per_cu->objfile;
6885 CORE_ADDR lowpc, highpc;
6886 struct symtab *symtab;
6887 struct cleanup *back_to, *delayed_list_cleanup;
6888 CORE_ADDR baseaddr;
6889 struct block *static_block;
6890
6891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6892
6893 buildsym_init ();
6894 back_to = make_cleanup (really_free_pendings, NULL);
6895 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6896
6897 cu->list_in_scope = &file_symbols;
6898
6899 cu->language = pretend_language;
6900 cu->language_defn = language_def (cu->language);
6901
6902 /* Do line number decoding in read_file_scope () */
6903 process_die (cu->dies, cu);
6904
6905 /* For now fudge the Go package. */
6906 if (cu->language == language_go)
6907 fixup_go_packaging (cu);
6908
6909 /* Now that we have processed all the DIEs in the CU, all the types
6910 should be complete, and it should now be safe to compute all of the
6911 physnames. */
6912 compute_delayed_physnames (cu);
6913 do_cleanups (delayed_list_cleanup);
6914
6915 /* Some compilers don't define a DW_AT_high_pc attribute for the
6916 compilation unit. If the DW_AT_high_pc is missing, synthesize
6917 it, by scanning the DIE's below the compilation unit. */
6918 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6919
6920 static_block
6921 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6922 per_cu->s.imported_symtabs != NULL);
6923
6924 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6925 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6926 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6927 addrmap to help ensure it has an accurate map of pc values belonging to
6928 this comp unit. */
6929 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6930
6931 symtab = end_symtab_from_static_block (static_block, objfile,
6932 SECT_OFF_TEXT (objfile), 0);
6933
6934 if (symtab != NULL)
6935 {
6936 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6937
6938 /* Set symtab language to language from DW_AT_language. If the
6939 compilation is from a C file generated by language preprocessors, do
6940 not set the language if it was already deduced by start_subfile. */
6941 if (!(cu->language == language_c && symtab->language != language_c))
6942 symtab->language = cu->language;
6943
6944 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6945 produce DW_AT_location with location lists but it can be possibly
6946 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6947 there were bugs in prologue debug info, fixed later in GCC-4.5
6948 by "unwind info for epilogues" patch (which is not directly related).
6949
6950 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6951 needed, it would be wrong due to missing DW_AT_producer there.
6952
6953 Still one can confuse GDB by using non-standard GCC compilation
6954 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6955 */
6956 if (cu->has_loclist && gcc_4_minor >= 5)
6957 symtab->locations_valid = 1;
6958
6959 if (gcc_4_minor >= 5)
6960 symtab->epilogue_unwind_valid = 1;
6961
6962 symtab->call_site_htab = cu->call_site_htab;
6963 }
6964
6965 if (dwarf2_per_objfile->using_index)
6966 per_cu->v.quick->symtab = symtab;
6967 else
6968 {
6969 struct partial_symtab *pst = per_cu->v.psymtab;
6970 pst->symtab = symtab;
6971 pst->readin = 1;
6972 }
6973
6974 /* Push it for inclusion processing later. */
6975 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6976
6977 do_cleanups (back_to);
6978 }
6979
6980 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6981 already been loaded into memory. */
6982
6983 static void
6984 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6985 enum language pretend_language)
6986 {
6987 struct dwarf2_cu *cu = per_cu->cu;
6988 struct objfile *objfile = per_cu->objfile;
6989 struct symtab *symtab;
6990 struct cleanup *back_to, *delayed_list_cleanup;
6991
6992 buildsym_init ();
6993 back_to = make_cleanup (really_free_pendings, NULL);
6994 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6995
6996 cu->list_in_scope = &file_symbols;
6997
6998 cu->language = pretend_language;
6999 cu->language_defn = language_def (cu->language);
7000
7001 /* The symbol tables are set up in read_type_unit_scope. */
7002 process_die (cu->dies, cu);
7003
7004 /* For now fudge the Go package. */
7005 if (cu->language == language_go)
7006 fixup_go_packaging (cu);
7007
7008 /* Now that we have processed all the DIEs in the CU, all the types
7009 should be complete, and it should now be safe to compute all of the
7010 physnames. */
7011 compute_delayed_physnames (cu);
7012 do_cleanups (delayed_list_cleanup);
7013
7014 /* TUs share symbol tables.
7015 If this is the first TU to use this symtab, complete the construction
7016 of it with end_expandable_symtab. Otherwise, complete the addition of
7017 this TU's symbols to the existing symtab. */
7018 if (per_cu->s.type_unit_group->primary_symtab == NULL)
7019 {
7020 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7021 per_cu->s.type_unit_group->primary_symtab = symtab;
7022
7023 if (symtab != NULL)
7024 {
7025 /* Set symtab language to language from DW_AT_language. If the
7026 compilation is from a C file generated by language preprocessors,
7027 do not set the language if it was already deduced by
7028 start_subfile. */
7029 if (!(cu->language == language_c && symtab->language != language_c))
7030 symtab->language = cu->language;
7031 }
7032 }
7033 else
7034 {
7035 augment_type_symtab (objfile,
7036 per_cu->s.type_unit_group->primary_symtab);
7037 symtab = per_cu->s.type_unit_group->primary_symtab;
7038 }
7039
7040 if (dwarf2_per_objfile->using_index)
7041 per_cu->v.quick->symtab = symtab;
7042 else
7043 {
7044 struct partial_symtab *pst = per_cu->v.psymtab;
7045 pst->symtab = symtab;
7046 pst->readin = 1;
7047 }
7048
7049 do_cleanups (back_to);
7050 }
7051
7052 /* Process an imported unit DIE. */
7053
7054 static void
7055 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7056 {
7057 struct attribute *attr;
7058
7059 /* For now we don't handle imported units in type units. */
7060 if (cu->per_cu->is_debug_types)
7061 {
7062 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7063 " supported in type units [in module %s]"),
7064 cu->objfile->name);
7065 }
7066
7067 attr = dwarf2_attr (die, DW_AT_import, cu);
7068 if (attr != NULL)
7069 {
7070 struct dwarf2_per_cu_data *per_cu;
7071 struct symtab *imported_symtab;
7072 sect_offset offset;
7073 int is_dwz;
7074
7075 offset = dwarf2_get_ref_die_offset (attr);
7076 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7077 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7078
7079 /* Queue the unit, if needed. */
7080 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7081 load_full_comp_unit (per_cu, cu->language);
7082
7083 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7084 per_cu);
7085 }
7086 }
7087
7088 /* Process a die and its children. */
7089
7090 static void
7091 process_die (struct die_info *die, struct dwarf2_cu *cu)
7092 {
7093 switch (die->tag)
7094 {
7095 case DW_TAG_padding:
7096 break;
7097 case DW_TAG_compile_unit:
7098 case DW_TAG_partial_unit:
7099 read_file_scope (die, cu);
7100 break;
7101 case DW_TAG_type_unit:
7102 read_type_unit_scope (die, cu);
7103 break;
7104 case DW_TAG_subprogram:
7105 case DW_TAG_inlined_subroutine:
7106 read_func_scope (die, cu);
7107 break;
7108 case DW_TAG_lexical_block:
7109 case DW_TAG_try_block:
7110 case DW_TAG_catch_block:
7111 read_lexical_block_scope (die, cu);
7112 break;
7113 case DW_TAG_GNU_call_site:
7114 read_call_site_scope (die, cu);
7115 break;
7116 case DW_TAG_class_type:
7117 case DW_TAG_interface_type:
7118 case DW_TAG_structure_type:
7119 case DW_TAG_union_type:
7120 process_structure_scope (die, cu);
7121 break;
7122 case DW_TAG_enumeration_type:
7123 process_enumeration_scope (die, cu);
7124 break;
7125
7126 /* These dies have a type, but processing them does not create
7127 a symbol or recurse to process the children. Therefore we can
7128 read them on-demand through read_type_die. */
7129 case DW_TAG_subroutine_type:
7130 case DW_TAG_set_type:
7131 case DW_TAG_array_type:
7132 case DW_TAG_pointer_type:
7133 case DW_TAG_ptr_to_member_type:
7134 case DW_TAG_reference_type:
7135 case DW_TAG_string_type:
7136 break;
7137
7138 case DW_TAG_base_type:
7139 case DW_TAG_subrange_type:
7140 case DW_TAG_typedef:
7141 /* Add a typedef symbol for the type definition, if it has a
7142 DW_AT_name. */
7143 new_symbol (die, read_type_die (die, cu), cu);
7144 break;
7145 case DW_TAG_common_block:
7146 read_common_block (die, cu);
7147 break;
7148 case DW_TAG_common_inclusion:
7149 break;
7150 case DW_TAG_namespace:
7151 processing_has_namespace_info = 1;
7152 read_namespace (die, cu);
7153 break;
7154 case DW_TAG_module:
7155 processing_has_namespace_info = 1;
7156 read_module (die, cu);
7157 break;
7158 case DW_TAG_imported_declaration:
7159 case DW_TAG_imported_module:
7160 processing_has_namespace_info = 1;
7161 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7162 || cu->language != language_fortran))
7163 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7164 dwarf_tag_name (die->tag));
7165 read_import_statement (die, cu);
7166 break;
7167
7168 case DW_TAG_imported_unit:
7169 process_imported_unit_die (die, cu);
7170 break;
7171
7172 default:
7173 new_symbol (die, NULL, cu);
7174 break;
7175 }
7176 }
7177
7178 /* A helper function for dwarf2_compute_name which determines whether DIE
7179 needs to have the name of the scope prepended to the name listed in the
7180 die. */
7181
7182 static int
7183 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7184 {
7185 struct attribute *attr;
7186
7187 switch (die->tag)
7188 {
7189 case DW_TAG_namespace:
7190 case DW_TAG_typedef:
7191 case DW_TAG_class_type:
7192 case DW_TAG_interface_type:
7193 case DW_TAG_structure_type:
7194 case DW_TAG_union_type:
7195 case DW_TAG_enumeration_type:
7196 case DW_TAG_enumerator:
7197 case DW_TAG_subprogram:
7198 case DW_TAG_member:
7199 return 1;
7200
7201 case DW_TAG_variable:
7202 case DW_TAG_constant:
7203 /* We only need to prefix "globally" visible variables. These include
7204 any variable marked with DW_AT_external or any variable that
7205 lives in a namespace. [Variables in anonymous namespaces
7206 require prefixing, but they are not DW_AT_external.] */
7207
7208 if (dwarf2_attr (die, DW_AT_specification, cu))
7209 {
7210 struct dwarf2_cu *spec_cu = cu;
7211
7212 return die_needs_namespace (die_specification (die, &spec_cu),
7213 spec_cu);
7214 }
7215
7216 attr = dwarf2_attr (die, DW_AT_external, cu);
7217 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7218 && die->parent->tag != DW_TAG_module)
7219 return 0;
7220 /* A variable in a lexical block of some kind does not need a
7221 namespace, even though in C++ such variables may be external
7222 and have a mangled name. */
7223 if (die->parent->tag == DW_TAG_lexical_block
7224 || die->parent->tag == DW_TAG_try_block
7225 || die->parent->tag == DW_TAG_catch_block
7226 || die->parent->tag == DW_TAG_subprogram)
7227 return 0;
7228 return 1;
7229
7230 default:
7231 return 0;
7232 }
7233 }
7234
7235 /* Retrieve the last character from a mem_file. */
7236
7237 static void
7238 do_ui_file_peek_last (void *object, const char *buffer, long length)
7239 {
7240 char *last_char_p = (char *) object;
7241
7242 if (length > 0)
7243 *last_char_p = buffer[length - 1];
7244 }
7245
7246 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7247 compute the physname for the object, which include a method's:
7248 - formal parameters (C++/Java),
7249 - receiver type (Go),
7250 - return type (Java).
7251
7252 The term "physname" is a bit confusing.
7253 For C++, for example, it is the demangled name.
7254 For Go, for example, it's the mangled name.
7255
7256 For Ada, return the DIE's linkage name rather than the fully qualified
7257 name. PHYSNAME is ignored..
7258
7259 The result is allocated on the objfile_obstack and canonicalized. */
7260
7261 static const char *
7262 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7263 int physname)
7264 {
7265 struct objfile *objfile = cu->objfile;
7266
7267 if (name == NULL)
7268 name = dwarf2_name (die, cu);
7269
7270 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7271 compute it by typename_concat inside GDB. */
7272 if (cu->language == language_ada
7273 || (cu->language == language_fortran && physname))
7274 {
7275 /* For Ada unit, we prefer the linkage name over the name, as
7276 the former contains the exported name, which the user expects
7277 to be able to reference. Ideally, we want the user to be able
7278 to reference this entity using either natural or linkage name,
7279 but we haven't started looking at this enhancement yet. */
7280 struct attribute *attr;
7281
7282 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7283 if (attr == NULL)
7284 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7285 if (attr && DW_STRING (attr))
7286 return DW_STRING (attr);
7287 }
7288
7289 /* These are the only languages we know how to qualify names in. */
7290 if (name != NULL
7291 && (cu->language == language_cplus || cu->language == language_java
7292 || cu->language == language_fortran))
7293 {
7294 if (die_needs_namespace (die, cu))
7295 {
7296 long length;
7297 const char *prefix;
7298 struct ui_file *buf;
7299
7300 prefix = determine_prefix (die, cu);
7301 buf = mem_fileopen ();
7302 if (*prefix != '\0')
7303 {
7304 char *prefixed_name = typename_concat (NULL, prefix, name,
7305 physname, cu);
7306
7307 fputs_unfiltered (prefixed_name, buf);
7308 xfree (prefixed_name);
7309 }
7310 else
7311 fputs_unfiltered (name, buf);
7312
7313 /* Template parameters may be specified in the DIE's DW_AT_name, or
7314 as children with DW_TAG_template_type_param or
7315 DW_TAG_value_type_param. If the latter, add them to the name
7316 here. If the name already has template parameters, then
7317 skip this step; some versions of GCC emit both, and
7318 it is more efficient to use the pre-computed name.
7319
7320 Something to keep in mind about this process: it is very
7321 unlikely, or in some cases downright impossible, to produce
7322 something that will match the mangled name of a function.
7323 If the definition of the function has the same debug info,
7324 we should be able to match up with it anyway. But fallbacks
7325 using the minimal symbol, for instance to find a method
7326 implemented in a stripped copy of libstdc++, will not work.
7327 If we do not have debug info for the definition, we will have to
7328 match them up some other way.
7329
7330 When we do name matching there is a related problem with function
7331 templates; two instantiated function templates are allowed to
7332 differ only by their return types, which we do not add here. */
7333
7334 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7335 {
7336 struct attribute *attr;
7337 struct die_info *child;
7338 int first = 1;
7339
7340 die->building_fullname = 1;
7341
7342 for (child = die->child; child != NULL; child = child->sibling)
7343 {
7344 struct type *type;
7345 LONGEST value;
7346 gdb_byte *bytes;
7347 struct dwarf2_locexpr_baton *baton;
7348 struct value *v;
7349
7350 if (child->tag != DW_TAG_template_type_param
7351 && child->tag != DW_TAG_template_value_param)
7352 continue;
7353
7354 if (first)
7355 {
7356 fputs_unfiltered ("<", buf);
7357 first = 0;
7358 }
7359 else
7360 fputs_unfiltered (", ", buf);
7361
7362 attr = dwarf2_attr (child, DW_AT_type, cu);
7363 if (attr == NULL)
7364 {
7365 complaint (&symfile_complaints,
7366 _("template parameter missing DW_AT_type"));
7367 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7368 continue;
7369 }
7370 type = die_type (child, cu);
7371
7372 if (child->tag == DW_TAG_template_type_param)
7373 {
7374 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7375 continue;
7376 }
7377
7378 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7379 if (attr == NULL)
7380 {
7381 complaint (&symfile_complaints,
7382 _("template parameter missing "
7383 "DW_AT_const_value"));
7384 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7385 continue;
7386 }
7387
7388 dwarf2_const_value_attr (attr, type, name,
7389 &cu->comp_unit_obstack, cu,
7390 &value, &bytes, &baton);
7391
7392 if (TYPE_NOSIGN (type))
7393 /* GDB prints characters as NUMBER 'CHAR'. If that's
7394 changed, this can use value_print instead. */
7395 c_printchar (value, type, buf);
7396 else
7397 {
7398 struct value_print_options opts;
7399
7400 if (baton != NULL)
7401 v = dwarf2_evaluate_loc_desc (type, NULL,
7402 baton->data,
7403 baton->size,
7404 baton->per_cu);
7405 else if (bytes != NULL)
7406 {
7407 v = allocate_value (type);
7408 memcpy (value_contents_writeable (v), bytes,
7409 TYPE_LENGTH (type));
7410 }
7411 else
7412 v = value_from_longest (type, value);
7413
7414 /* Specify decimal so that we do not depend on
7415 the radix. */
7416 get_formatted_print_options (&opts, 'd');
7417 opts.raw = 1;
7418 value_print (v, buf, &opts);
7419 release_value (v);
7420 value_free (v);
7421 }
7422 }
7423
7424 die->building_fullname = 0;
7425
7426 if (!first)
7427 {
7428 /* Close the argument list, with a space if necessary
7429 (nested templates). */
7430 char last_char = '\0';
7431 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7432 if (last_char == '>')
7433 fputs_unfiltered (" >", buf);
7434 else
7435 fputs_unfiltered (">", buf);
7436 }
7437 }
7438
7439 /* For Java and C++ methods, append formal parameter type
7440 information, if PHYSNAME. */
7441
7442 if (physname && die->tag == DW_TAG_subprogram
7443 && (cu->language == language_cplus
7444 || cu->language == language_java))
7445 {
7446 struct type *type = read_type_die (die, cu);
7447
7448 c_type_print_args (type, buf, 1, cu->language,
7449 &type_print_raw_options);
7450
7451 if (cu->language == language_java)
7452 {
7453 /* For java, we must append the return type to method
7454 names. */
7455 if (die->tag == DW_TAG_subprogram)
7456 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7457 0, 0, &type_print_raw_options);
7458 }
7459 else if (cu->language == language_cplus)
7460 {
7461 /* Assume that an artificial first parameter is
7462 "this", but do not crash if it is not. RealView
7463 marks unnamed (and thus unused) parameters as
7464 artificial; there is no way to differentiate
7465 the two cases. */
7466 if (TYPE_NFIELDS (type) > 0
7467 && TYPE_FIELD_ARTIFICIAL (type, 0)
7468 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7469 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7470 0))))
7471 fputs_unfiltered (" const", buf);
7472 }
7473 }
7474
7475 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7476 &length);
7477 ui_file_delete (buf);
7478
7479 if (cu->language == language_cplus)
7480 {
7481 char *cname
7482 = dwarf2_canonicalize_name (name, cu,
7483 &objfile->objfile_obstack);
7484
7485 if (cname != NULL)
7486 name = cname;
7487 }
7488 }
7489 }
7490
7491 return name;
7492 }
7493
7494 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7495 If scope qualifiers are appropriate they will be added. The result
7496 will be allocated on the objfile_obstack, or NULL if the DIE does
7497 not have a name. NAME may either be from a previous call to
7498 dwarf2_name or NULL.
7499
7500 The output string will be canonicalized (if C++/Java). */
7501
7502 static const char *
7503 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7504 {
7505 return dwarf2_compute_name (name, die, cu, 0);
7506 }
7507
7508 /* Construct a physname for the given DIE in CU. NAME may either be
7509 from a previous call to dwarf2_name or NULL. The result will be
7510 allocated on the objfile_objstack or NULL if the DIE does not have a
7511 name.
7512
7513 The output string will be canonicalized (if C++/Java). */
7514
7515 static const char *
7516 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7517 {
7518 struct objfile *objfile = cu->objfile;
7519 struct attribute *attr;
7520 const char *retval, *mangled = NULL, *canon = NULL;
7521 struct cleanup *back_to;
7522 int need_copy = 1;
7523
7524 /* In this case dwarf2_compute_name is just a shortcut not building anything
7525 on its own. */
7526 if (!die_needs_namespace (die, cu))
7527 return dwarf2_compute_name (name, die, cu, 1);
7528
7529 back_to = make_cleanup (null_cleanup, NULL);
7530
7531 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7532 if (!attr)
7533 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7534
7535 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7536 has computed. */
7537 if (attr && DW_STRING (attr))
7538 {
7539 char *demangled;
7540
7541 mangled = DW_STRING (attr);
7542
7543 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7544 type. It is easier for GDB users to search for such functions as
7545 `name(params)' than `long name(params)'. In such case the minimal
7546 symbol names do not match the full symbol names but for template
7547 functions there is never a need to look up their definition from their
7548 declaration so the only disadvantage remains the minimal symbol
7549 variant `long name(params)' does not have the proper inferior type.
7550 */
7551
7552 if (cu->language == language_go)
7553 {
7554 /* This is a lie, but we already lie to the caller new_symbol_full.
7555 new_symbol_full assumes we return the mangled name.
7556 This just undoes that lie until things are cleaned up. */
7557 demangled = NULL;
7558 }
7559 else
7560 {
7561 demangled = cplus_demangle (mangled,
7562 (DMGL_PARAMS | DMGL_ANSI
7563 | (cu->language == language_java
7564 ? DMGL_JAVA | DMGL_RET_POSTFIX
7565 : DMGL_RET_DROP)));
7566 }
7567 if (demangled)
7568 {
7569 make_cleanup (xfree, demangled);
7570 canon = demangled;
7571 }
7572 else
7573 {
7574 canon = mangled;
7575 need_copy = 0;
7576 }
7577 }
7578
7579 if (canon == NULL || check_physname)
7580 {
7581 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7582
7583 if (canon != NULL && strcmp (physname, canon) != 0)
7584 {
7585 /* It may not mean a bug in GDB. The compiler could also
7586 compute DW_AT_linkage_name incorrectly. But in such case
7587 GDB would need to be bug-to-bug compatible. */
7588
7589 complaint (&symfile_complaints,
7590 _("Computed physname <%s> does not match demangled <%s> "
7591 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7592 physname, canon, mangled, die->offset.sect_off, objfile->name);
7593
7594 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7595 is available here - over computed PHYSNAME. It is safer
7596 against both buggy GDB and buggy compilers. */
7597
7598 retval = canon;
7599 }
7600 else
7601 {
7602 retval = physname;
7603 need_copy = 0;
7604 }
7605 }
7606 else
7607 retval = canon;
7608
7609 if (need_copy)
7610 retval = obsavestring (retval, strlen (retval),
7611 &objfile->objfile_obstack);
7612
7613 do_cleanups (back_to);
7614 return retval;
7615 }
7616
7617 /* Read the import statement specified by the given die and record it. */
7618
7619 static void
7620 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7621 {
7622 struct objfile *objfile = cu->objfile;
7623 struct attribute *import_attr;
7624 struct die_info *imported_die, *child_die;
7625 struct dwarf2_cu *imported_cu;
7626 const char *imported_name;
7627 const char *imported_name_prefix;
7628 const char *canonical_name;
7629 const char *import_alias;
7630 const char *imported_declaration = NULL;
7631 const char *import_prefix;
7632 VEC (const_char_ptr) *excludes = NULL;
7633 struct cleanup *cleanups;
7634
7635 char *temp;
7636
7637 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7638 if (import_attr == NULL)
7639 {
7640 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7641 dwarf_tag_name (die->tag));
7642 return;
7643 }
7644
7645 imported_cu = cu;
7646 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7647 imported_name = dwarf2_name (imported_die, imported_cu);
7648 if (imported_name == NULL)
7649 {
7650 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7651
7652 The import in the following code:
7653 namespace A
7654 {
7655 typedef int B;
7656 }
7657
7658 int main ()
7659 {
7660 using A::B;
7661 B b;
7662 return b;
7663 }
7664
7665 ...
7666 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7667 <52> DW_AT_decl_file : 1
7668 <53> DW_AT_decl_line : 6
7669 <54> DW_AT_import : <0x75>
7670 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7671 <59> DW_AT_name : B
7672 <5b> DW_AT_decl_file : 1
7673 <5c> DW_AT_decl_line : 2
7674 <5d> DW_AT_type : <0x6e>
7675 ...
7676 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7677 <76> DW_AT_byte_size : 4
7678 <77> DW_AT_encoding : 5 (signed)
7679
7680 imports the wrong die ( 0x75 instead of 0x58 ).
7681 This case will be ignored until the gcc bug is fixed. */
7682 return;
7683 }
7684
7685 /* Figure out the local name after import. */
7686 import_alias = dwarf2_name (die, cu);
7687
7688 /* Figure out where the statement is being imported to. */
7689 import_prefix = determine_prefix (die, cu);
7690
7691 /* Figure out what the scope of the imported die is and prepend it
7692 to the name of the imported die. */
7693 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7694
7695 if (imported_die->tag != DW_TAG_namespace
7696 && imported_die->tag != DW_TAG_module)
7697 {
7698 imported_declaration = imported_name;
7699 canonical_name = imported_name_prefix;
7700 }
7701 else if (strlen (imported_name_prefix) > 0)
7702 {
7703 temp = alloca (strlen (imported_name_prefix)
7704 + 2 + strlen (imported_name) + 1);
7705 strcpy (temp, imported_name_prefix);
7706 strcat (temp, "::");
7707 strcat (temp, imported_name);
7708 canonical_name = temp;
7709 }
7710 else
7711 canonical_name = imported_name;
7712
7713 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7714
7715 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7716 for (child_die = die->child; child_die && child_die->tag;
7717 child_die = sibling_die (child_die))
7718 {
7719 /* DWARF-4: A Fortran use statement with a “rename list” may be
7720 represented by an imported module entry with an import attribute
7721 referring to the module and owned entries corresponding to those
7722 entities that are renamed as part of being imported. */
7723
7724 if (child_die->tag != DW_TAG_imported_declaration)
7725 {
7726 complaint (&symfile_complaints,
7727 _("child DW_TAG_imported_declaration expected "
7728 "- DIE at 0x%x [in module %s]"),
7729 child_die->offset.sect_off, objfile->name);
7730 continue;
7731 }
7732
7733 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7734 if (import_attr == NULL)
7735 {
7736 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7737 dwarf_tag_name (child_die->tag));
7738 continue;
7739 }
7740
7741 imported_cu = cu;
7742 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7743 &imported_cu);
7744 imported_name = dwarf2_name (imported_die, imported_cu);
7745 if (imported_name == NULL)
7746 {
7747 complaint (&symfile_complaints,
7748 _("child DW_TAG_imported_declaration has unknown "
7749 "imported name - DIE at 0x%x [in module %s]"),
7750 child_die->offset.sect_off, objfile->name);
7751 continue;
7752 }
7753
7754 VEC_safe_push (const_char_ptr, excludes, imported_name);
7755
7756 process_die (child_die, cu);
7757 }
7758
7759 cp_add_using_directive (import_prefix,
7760 canonical_name,
7761 import_alias,
7762 imported_declaration,
7763 excludes,
7764 &objfile->objfile_obstack);
7765
7766 do_cleanups (cleanups);
7767 }
7768
7769 /* Cleanup function for handle_DW_AT_stmt_list. */
7770
7771 static void
7772 free_cu_line_header (void *arg)
7773 {
7774 struct dwarf2_cu *cu = arg;
7775
7776 free_line_header (cu->line_header);
7777 cu->line_header = NULL;
7778 }
7779
7780 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7781 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7782 this, it was first present in GCC release 4.3.0. */
7783
7784 static int
7785 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7786 {
7787 if (!cu->checked_producer)
7788 check_producer (cu);
7789
7790 return cu->producer_is_gcc_lt_4_3;
7791 }
7792
7793 static void
7794 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7795 char **name, char **comp_dir)
7796 {
7797 struct attribute *attr;
7798
7799 *name = NULL;
7800 *comp_dir = NULL;
7801
7802 /* Find the filename. Do not use dwarf2_name here, since the filename
7803 is not a source language identifier. */
7804 attr = dwarf2_attr (die, DW_AT_name, cu);
7805 if (attr)
7806 {
7807 *name = DW_STRING (attr);
7808 }
7809
7810 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7811 if (attr)
7812 *comp_dir = DW_STRING (attr);
7813 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7814 && IS_ABSOLUTE_PATH (*name))
7815 {
7816 *comp_dir = ldirname (*name);
7817 if (*comp_dir != NULL)
7818 make_cleanup (xfree, *comp_dir);
7819 }
7820 if (*comp_dir != NULL)
7821 {
7822 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7823 directory, get rid of it. */
7824 char *cp = strchr (*comp_dir, ':');
7825
7826 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7827 *comp_dir = cp + 1;
7828 }
7829
7830 if (*name == NULL)
7831 *name = "<unknown>";
7832 }
7833
7834 /* Handle DW_AT_stmt_list for a compilation unit.
7835 DIE is the DW_TAG_compile_unit die for CU.
7836 COMP_DIR is the compilation directory.
7837 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7838
7839 static void
7840 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7841 const char *comp_dir)
7842 {
7843 struct attribute *attr;
7844
7845 gdb_assert (! cu->per_cu->is_debug_types);
7846
7847 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7848 if (attr)
7849 {
7850 unsigned int line_offset = DW_UNSND (attr);
7851 struct line_header *line_header
7852 = dwarf_decode_line_header (line_offset, cu);
7853
7854 if (line_header)
7855 {
7856 cu->line_header = line_header;
7857 make_cleanup (free_cu_line_header, cu);
7858 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7859 }
7860 }
7861 }
7862
7863 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7864
7865 static void
7866 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7867 {
7868 struct objfile *objfile = dwarf2_per_objfile->objfile;
7869 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7870 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7871 CORE_ADDR highpc = ((CORE_ADDR) 0);
7872 struct attribute *attr;
7873 char *name = NULL;
7874 char *comp_dir = NULL;
7875 struct die_info *child_die;
7876 bfd *abfd = objfile->obfd;
7877 CORE_ADDR baseaddr;
7878
7879 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7880
7881 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7882
7883 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7884 from finish_block. */
7885 if (lowpc == ((CORE_ADDR) -1))
7886 lowpc = highpc;
7887 lowpc += baseaddr;
7888 highpc += baseaddr;
7889
7890 find_file_and_directory (die, cu, &name, &comp_dir);
7891
7892 prepare_one_comp_unit (cu, die, cu->language);
7893
7894 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7895 standardised yet. As a workaround for the language detection we fall
7896 back to the DW_AT_producer string. */
7897 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7898 cu->language = language_opencl;
7899
7900 /* Similar hack for Go. */
7901 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7902 set_cu_language (DW_LANG_Go, cu);
7903
7904 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7905
7906 /* Decode line number information if present. We do this before
7907 processing child DIEs, so that the line header table is available
7908 for DW_AT_decl_file. */
7909 handle_DW_AT_stmt_list (die, cu, comp_dir);
7910
7911 /* Process all dies in compilation unit. */
7912 if (die->child != NULL)
7913 {
7914 child_die = die->child;
7915 while (child_die && child_die->tag)
7916 {
7917 process_die (child_die, cu);
7918 child_die = sibling_die (child_die);
7919 }
7920 }
7921
7922 /* Decode macro information, if present. Dwarf 2 macro information
7923 refers to information in the line number info statement program
7924 header, so we can only read it if we've read the header
7925 successfully. */
7926 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7927 if (attr && cu->line_header)
7928 {
7929 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7930 complaint (&symfile_complaints,
7931 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7932
7933 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7934 }
7935 else
7936 {
7937 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7938 if (attr && cu->line_header)
7939 {
7940 unsigned int macro_offset = DW_UNSND (attr);
7941
7942 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7943 }
7944 }
7945
7946 do_cleanups (back_to);
7947 }
7948
7949 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7950 Create the set of symtabs used by this TU, or if this TU is sharing
7951 symtabs with another TU and the symtabs have already been created
7952 then restore those symtabs in the line header.
7953 We don't need the pc/line-number mapping for type units. */
7954
7955 static void
7956 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7957 {
7958 struct objfile *objfile = dwarf2_per_objfile->objfile;
7959 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7960 struct type_unit_group *tu_group;
7961 int first_time;
7962 struct line_header *lh;
7963 struct attribute *attr;
7964 unsigned int i, line_offset;
7965
7966 gdb_assert (per_cu->is_debug_types);
7967
7968 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7969
7970 /* If we're using .gdb_index (includes -readnow) then
7971 per_cu->s.type_unit_group may not have been set up yet. */
7972 if (per_cu->s.type_unit_group == NULL)
7973 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7974 tu_group = per_cu->s.type_unit_group;
7975
7976 /* If we've already processed this stmt_list there's no real need to
7977 do it again, we could fake it and just recreate the part we need
7978 (file name,index -> symtab mapping). If data shows this optimization
7979 is useful we can do it then. */
7980 first_time = tu_group->primary_symtab == NULL;
7981
7982 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7983 debug info. */
7984 lh = NULL;
7985 if (attr != NULL)
7986 {
7987 line_offset = DW_UNSND (attr);
7988 lh = dwarf_decode_line_header (line_offset, cu);
7989 }
7990 if (lh == NULL)
7991 {
7992 if (first_time)
7993 dwarf2_start_symtab (cu, "", NULL, 0);
7994 else
7995 {
7996 gdb_assert (tu_group->symtabs == NULL);
7997 restart_symtab (0);
7998 }
7999 /* Note: The primary symtab will get allocated at the end. */
8000 return;
8001 }
8002
8003 cu->line_header = lh;
8004 make_cleanup (free_cu_line_header, cu);
8005
8006 if (first_time)
8007 {
8008 dwarf2_start_symtab (cu, "", NULL, 0);
8009
8010 tu_group->num_symtabs = lh->num_file_names;
8011 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8012
8013 for (i = 0; i < lh->num_file_names; ++i)
8014 {
8015 char *dir = NULL;
8016 struct file_entry *fe = &lh->file_names[i];
8017
8018 if (fe->dir_index)
8019 dir = lh->include_dirs[fe->dir_index - 1];
8020 dwarf2_start_subfile (fe->name, dir, NULL);
8021
8022 /* Note: We don't have to watch for the main subfile here, type units
8023 don't have DW_AT_name. */
8024
8025 if (current_subfile->symtab == NULL)
8026 {
8027 /* NOTE: start_subfile will recognize when it's been passed
8028 a file it has already seen. So we can't assume there's a
8029 simple mapping from lh->file_names to subfiles,
8030 lh->file_names may contain dups. */
8031 current_subfile->symtab = allocate_symtab (current_subfile->name,
8032 objfile);
8033 }
8034
8035 fe->symtab = current_subfile->symtab;
8036 tu_group->symtabs[i] = fe->symtab;
8037 }
8038 }
8039 else
8040 {
8041 restart_symtab (0);
8042
8043 for (i = 0; i < lh->num_file_names; ++i)
8044 {
8045 struct file_entry *fe = &lh->file_names[i];
8046
8047 fe->symtab = tu_group->symtabs[i];
8048 }
8049 }
8050
8051 /* The main symtab is allocated last. Type units don't have DW_AT_name
8052 so they don't have a "real" (so to speak) symtab anyway.
8053 There is later code that will assign the main symtab to all symbols
8054 that don't have one. We need to handle the case of a symbol with a
8055 missing symtab (DW_AT_decl_file) anyway. */
8056 }
8057
8058 /* Process DW_TAG_type_unit.
8059 For TUs we want to skip the first top level sibling if it's not the
8060 actual type being defined by this TU. In this case the first top
8061 level sibling is there to provide context only. */
8062
8063 static void
8064 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8065 {
8066 struct die_info *child_die;
8067
8068 prepare_one_comp_unit (cu, die, language_minimal);
8069
8070 /* Initialize (or reinitialize) the machinery for building symtabs.
8071 We do this before processing child DIEs, so that the line header table
8072 is available for DW_AT_decl_file. */
8073 setup_type_unit_groups (die, cu);
8074
8075 if (die->child != NULL)
8076 {
8077 child_die = die->child;
8078 while (child_die && child_die->tag)
8079 {
8080 process_die (child_die, cu);
8081 child_die = sibling_die (child_die);
8082 }
8083 }
8084 }
8085 \f
8086 /* DWO/DWP files.
8087
8088 http://gcc.gnu.org/wiki/DebugFission
8089 http://gcc.gnu.org/wiki/DebugFissionDWP
8090
8091 To simplify handling of both DWO files ("object" files with the DWARF info)
8092 and DWP files (a file with the DWOs packaged up into one file), we treat
8093 DWP files as having a collection of virtual DWO files. */
8094
8095 static hashval_t
8096 hash_dwo_file (const void *item)
8097 {
8098 const struct dwo_file *dwo_file = item;
8099
8100 return htab_hash_string (dwo_file->name);
8101 }
8102
8103 static int
8104 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8105 {
8106 const struct dwo_file *lhs = item_lhs;
8107 const struct dwo_file *rhs = item_rhs;
8108
8109 return strcmp (lhs->name, rhs->name) == 0;
8110 }
8111
8112 /* Allocate a hash table for DWO files. */
8113
8114 static htab_t
8115 allocate_dwo_file_hash_table (void)
8116 {
8117 struct objfile *objfile = dwarf2_per_objfile->objfile;
8118
8119 return htab_create_alloc_ex (41,
8120 hash_dwo_file,
8121 eq_dwo_file,
8122 NULL,
8123 &objfile->objfile_obstack,
8124 hashtab_obstack_allocate,
8125 dummy_obstack_deallocate);
8126 }
8127
8128 /* Lookup DWO file DWO_NAME. */
8129
8130 static void **
8131 lookup_dwo_file_slot (const char *dwo_name)
8132 {
8133 struct dwo_file find_entry;
8134 void **slot;
8135
8136 if (dwarf2_per_objfile->dwo_files == NULL)
8137 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8138
8139 memset (&find_entry, 0, sizeof (find_entry));
8140 find_entry.name = dwo_name;
8141 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8142
8143 return slot;
8144 }
8145
8146 static hashval_t
8147 hash_dwo_unit (const void *item)
8148 {
8149 const struct dwo_unit *dwo_unit = item;
8150
8151 /* This drops the top 32 bits of the id, but is ok for a hash. */
8152 return dwo_unit->signature;
8153 }
8154
8155 static int
8156 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8157 {
8158 const struct dwo_unit *lhs = item_lhs;
8159 const struct dwo_unit *rhs = item_rhs;
8160
8161 /* The signature is assumed to be unique within the DWO file.
8162 So while object file CU dwo_id's always have the value zero,
8163 that's OK, assuming each object file DWO file has only one CU,
8164 and that's the rule for now. */
8165 return lhs->signature == rhs->signature;
8166 }
8167
8168 /* Allocate a hash table for DWO CUs,TUs.
8169 There is one of these tables for each of CUs,TUs for each DWO file. */
8170
8171 static htab_t
8172 allocate_dwo_unit_table (struct objfile *objfile)
8173 {
8174 /* Start out with a pretty small number.
8175 Generally DWO files contain only one CU and maybe some TUs. */
8176 return htab_create_alloc_ex (3,
8177 hash_dwo_unit,
8178 eq_dwo_unit,
8179 NULL,
8180 &objfile->objfile_obstack,
8181 hashtab_obstack_allocate,
8182 dummy_obstack_deallocate);
8183 }
8184
8185 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8186
8187 struct create_dwo_info_table_data
8188 {
8189 struct dwo_file *dwo_file;
8190 htab_t cu_htab;
8191 };
8192
8193 /* die_reader_func for create_dwo_debug_info_hash_table. */
8194
8195 static void
8196 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8197 gdb_byte *info_ptr,
8198 struct die_info *comp_unit_die,
8199 int has_children,
8200 void *datap)
8201 {
8202 struct dwarf2_cu *cu = reader->cu;
8203 struct objfile *objfile = dwarf2_per_objfile->objfile;
8204 sect_offset offset = cu->per_cu->offset;
8205 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8206 struct create_dwo_info_table_data *data = datap;
8207 struct dwo_file *dwo_file = data->dwo_file;
8208 htab_t cu_htab = data->cu_htab;
8209 void **slot;
8210 struct attribute *attr;
8211 struct dwo_unit *dwo_unit;
8212
8213 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8214 if (attr == NULL)
8215 {
8216 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8217 " its dwo_id [in module %s]"),
8218 offset.sect_off, dwo_file->name);
8219 return;
8220 }
8221
8222 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8223 dwo_unit->dwo_file = dwo_file;
8224 dwo_unit->signature = DW_UNSND (attr);
8225 dwo_unit->info_or_types_section = section;
8226 dwo_unit->offset = offset;
8227 dwo_unit->length = cu->per_cu->length;
8228
8229 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8230 gdb_assert (slot != NULL);
8231 if (*slot != NULL)
8232 {
8233 const struct dwo_unit *dup_dwo_unit = *slot;
8234
8235 complaint (&symfile_complaints,
8236 _("debug entry at offset 0x%x is duplicate to the entry at"
8237 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8238 offset.sect_off, dup_dwo_unit->offset.sect_off,
8239 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8240 dwo_file->name);
8241 }
8242 else
8243 *slot = dwo_unit;
8244
8245 if (dwarf2_read_debug)
8246 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8247 offset.sect_off,
8248 phex (dwo_unit->signature,
8249 sizeof (dwo_unit->signature)));
8250 }
8251
8252 /* Create a hash table to map DWO IDs to their CU entry in
8253 .debug_info.dwo in DWO_FILE.
8254 Note: This function processes DWO files only, not DWP files. */
8255
8256 static htab_t
8257 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8258 {
8259 struct objfile *objfile = dwarf2_per_objfile->objfile;
8260 struct dwarf2_section_info *section = &dwo_file->sections.info;
8261 bfd *abfd;
8262 htab_t cu_htab;
8263 gdb_byte *info_ptr, *end_ptr;
8264 struct create_dwo_info_table_data create_dwo_info_table_data;
8265
8266 dwarf2_read_section (objfile, section);
8267 info_ptr = section->buffer;
8268
8269 if (info_ptr == NULL)
8270 return NULL;
8271
8272 /* We can't set abfd until now because the section may be empty or
8273 not present, in which case section->asection will be NULL. */
8274 abfd = section->asection->owner;
8275
8276 if (dwarf2_read_debug)
8277 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8278 bfd_get_filename (abfd));
8279
8280 cu_htab = allocate_dwo_unit_table (objfile);
8281
8282 create_dwo_info_table_data.dwo_file = dwo_file;
8283 create_dwo_info_table_data.cu_htab = cu_htab;
8284
8285 end_ptr = info_ptr + section->size;
8286 while (info_ptr < end_ptr)
8287 {
8288 struct dwarf2_per_cu_data per_cu;
8289
8290 memset (&per_cu, 0, sizeof (per_cu));
8291 per_cu.objfile = objfile;
8292 per_cu.is_debug_types = 0;
8293 per_cu.offset.sect_off = info_ptr - section->buffer;
8294 per_cu.info_or_types_section = section;
8295
8296 init_cutu_and_read_dies_no_follow (&per_cu,
8297 &dwo_file->sections.abbrev,
8298 dwo_file,
8299 create_dwo_debug_info_hash_table_reader,
8300 &create_dwo_info_table_data);
8301
8302 info_ptr += per_cu.length;
8303 }
8304
8305 return cu_htab;
8306 }
8307
8308 /* DWP file .debug_{cu,tu}_index section format:
8309 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8310
8311 Both index sections have the same format, and serve to map a 64-bit
8312 signature to a set of section numbers. Each section begins with a header,
8313 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8314 indexes, and a pool of 32-bit section numbers. The index sections will be
8315 aligned at 8-byte boundaries in the file.
8316
8317 The index section header contains two unsigned 32-bit values (using the
8318 byte order of the application binary):
8319
8320 N, the number of compilation units or type units in the index
8321 M, the number of slots in the hash table
8322
8323 (We assume that N and M will not exceed 2^32 - 1.)
8324
8325 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8326
8327 The hash table begins at offset 8 in the section, and consists of an array
8328 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8329 order of the application binary). Unused slots in the hash table are 0.
8330 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8331
8332 The parallel table begins immediately after the hash table
8333 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8334 array of 32-bit indexes (using the byte order of the application binary),
8335 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8336 table contains a 32-bit index into the pool of section numbers. For unused
8337 hash table slots, the corresponding entry in the parallel table will be 0.
8338
8339 Given a 64-bit compilation unit signature or a type signature S, an entry
8340 in the hash table is located as follows:
8341
8342 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8343 the low-order k bits all set to 1.
8344
8345 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8346
8347 3) If the hash table entry at index H matches the signature, use that
8348 entry. If the hash table entry at index H is unused (all zeroes),
8349 terminate the search: the signature is not present in the table.
8350
8351 4) Let H = (H + H') modulo M. Repeat at Step 3.
8352
8353 Because M > N and H' and M are relatively prime, the search is guaranteed
8354 to stop at an unused slot or find the match.
8355
8356 The pool of section numbers begins immediately following the hash table
8357 (at offset 8 + 12 * M from the beginning of the section). The pool of
8358 section numbers consists of an array of 32-bit words (using the byte order
8359 of the application binary). Each item in the array is indexed starting
8360 from 0. The hash table entry provides the index of the first section
8361 number in the set. Additional section numbers in the set follow, and the
8362 set is terminated by a 0 entry (section number 0 is not used in ELF).
8363
8364 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8365 section must be the first entry in the set, and the .debug_abbrev.dwo must
8366 be the second entry. Other members of the set may follow in any order. */
8367
8368 /* Create a hash table to map DWO IDs to their CU/TU entry in
8369 .debug_{info,types}.dwo in DWP_FILE.
8370 Returns NULL if there isn't one.
8371 Note: This function processes DWP files only, not DWO files. */
8372
8373 static struct dwp_hash_table *
8374 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8375 {
8376 struct objfile *objfile = dwarf2_per_objfile->objfile;
8377 bfd *dbfd = dwp_file->dbfd;
8378 char *index_ptr, *index_end;
8379 struct dwarf2_section_info *index;
8380 uint32_t version, nr_units, nr_slots;
8381 struct dwp_hash_table *htab;
8382
8383 if (is_debug_types)
8384 index = &dwp_file->sections.tu_index;
8385 else
8386 index = &dwp_file->sections.cu_index;
8387
8388 if (dwarf2_section_empty_p (index))
8389 return NULL;
8390 dwarf2_read_section (objfile, index);
8391
8392 index_ptr = index->buffer;
8393 index_end = index_ptr + index->size;
8394
8395 version = read_4_bytes (dbfd, index_ptr);
8396 index_ptr += 8; /* Skip the unused word. */
8397 nr_units = read_4_bytes (dbfd, index_ptr);
8398 index_ptr += 4;
8399 nr_slots = read_4_bytes (dbfd, index_ptr);
8400 index_ptr += 4;
8401
8402 if (version != 1)
8403 {
8404 error (_("Dwarf Error: unsupported DWP file version (%u)"
8405 " [in module %s]"),
8406 version, dwp_file->name);
8407 }
8408 if (nr_slots != (nr_slots & -nr_slots))
8409 {
8410 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8411 " is not power of 2 [in module %s]"),
8412 nr_slots, dwp_file->name);
8413 }
8414
8415 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8416 htab->nr_units = nr_units;
8417 htab->nr_slots = nr_slots;
8418 htab->hash_table = index_ptr;
8419 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8420 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8421
8422 return htab;
8423 }
8424
8425 /* Update SECTIONS with the data from SECTP.
8426
8427 This function is like the other "locate" section routines that are
8428 passed to bfd_map_over_sections, but in this context the sections to
8429 read comes from the DWP hash table, not the full ELF section table.
8430
8431 The result is non-zero for success, or zero if an error was found. */
8432
8433 static int
8434 locate_virtual_dwo_sections (asection *sectp,
8435 struct virtual_dwo_sections *sections)
8436 {
8437 const struct dwop_section_names *names = &dwop_section_names;
8438
8439 if (section_is_p (sectp->name, &names->abbrev_dwo))
8440 {
8441 /* There can be only one. */
8442 if (sections->abbrev.asection != NULL)
8443 return 0;
8444 sections->abbrev.asection = sectp;
8445 sections->abbrev.size = bfd_get_section_size (sectp);
8446 }
8447 else if (section_is_p (sectp->name, &names->info_dwo)
8448 || section_is_p (sectp->name, &names->types_dwo))
8449 {
8450 /* There can be only one. */
8451 if (sections->info_or_types.asection != NULL)
8452 return 0;
8453 sections->info_or_types.asection = sectp;
8454 sections->info_or_types.size = bfd_get_section_size (sectp);
8455 }
8456 else if (section_is_p (sectp->name, &names->line_dwo))
8457 {
8458 /* There can be only one. */
8459 if (sections->line.asection != NULL)
8460 return 0;
8461 sections->line.asection = sectp;
8462 sections->line.size = bfd_get_section_size (sectp);
8463 }
8464 else if (section_is_p (sectp->name, &names->loc_dwo))
8465 {
8466 /* There can be only one. */
8467 if (sections->loc.asection != NULL)
8468 return 0;
8469 sections->loc.asection = sectp;
8470 sections->loc.size = bfd_get_section_size (sectp);
8471 }
8472 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8473 {
8474 /* There can be only one. */
8475 if (sections->macinfo.asection != NULL)
8476 return 0;
8477 sections->macinfo.asection = sectp;
8478 sections->macinfo.size = bfd_get_section_size (sectp);
8479 }
8480 else if (section_is_p (sectp->name, &names->macro_dwo))
8481 {
8482 /* There can be only one. */
8483 if (sections->macro.asection != NULL)
8484 return 0;
8485 sections->macro.asection = sectp;
8486 sections->macro.size = bfd_get_section_size (sectp);
8487 }
8488 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8489 {
8490 /* There can be only one. */
8491 if (sections->str_offsets.asection != NULL)
8492 return 0;
8493 sections->str_offsets.asection = sectp;
8494 sections->str_offsets.size = bfd_get_section_size (sectp);
8495 }
8496 else
8497 {
8498 /* No other kind of section is valid. */
8499 return 0;
8500 }
8501
8502 return 1;
8503 }
8504
8505 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8506 HTAB is the hash table from the DWP file.
8507 SECTION_INDEX is the index of the DWO in HTAB. */
8508
8509 static struct dwo_unit *
8510 create_dwo_in_dwp (struct dwp_file *dwp_file,
8511 const struct dwp_hash_table *htab,
8512 uint32_t section_index,
8513 ULONGEST signature, int is_debug_types)
8514 {
8515 struct objfile *objfile = dwarf2_per_objfile->objfile;
8516 bfd *dbfd = dwp_file->dbfd;
8517 const char *kind = is_debug_types ? "TU" : "CU";
8518 struct dwo_file *dwo_file;
8519 struct dwo_unit *dwo_unit;
8520 struct virtual_dwo_sections sections;
8521 void **dwo_file_slot;
8522 char *virtual_dwo_name;
8523 struct dwarf2_section_info *cutu;
8524 struct cleanup *cleanups;
8525 int i;
8526
8527 if (dwarf2_read_debug)
8528 {
8529 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8530 kind,
8531 section_index, phex (signature, sizeof (signature)),
8532 dwp_file->name);
8533 }
8534
8535 /* Fetch the sections of this DWO.
8536 Put a limit on the number of sections we look for so that bad data
8537 doesn't cause us to loop forever. */
8538
8539 #define MAX_NR_DWO_SECTIONS \
8540 (1 /* .debug_info or .debug_types */ \
8541 + 1 /* .debug_abbrev */ \
8542 + 1 /* .debug_line */ \
8543 + 1 /* .debug_loc */ \
8544 + 1 /* .debug_str_offsets */ \
8545 + 1 /* .debug_macro */ \
8546 + 1 /* .debug_macinfo */ \
8547 + 1 /* trailing zero */)
8548
8549 memset (&sections, 0, sizeof (sections));
8550 cleanups = make_cleanup (null_cleanup, 0);
8551
8552 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8553 {
8554 asection *sectp;
8555 uint32_t section_nr =
8556 read_4_bytes (dbfd,
8557 htab->section_pool
8558 + (section_index + i) * sizeof (uint32_t));
8559
8560 if (section_nr == 0)
8561 break;
8562 if (section_nr >= dwp_file->num_sections)
8563 {
8564 error (_("Dwarf Error: bad DWP hash table, section number too large"
8565 " [in module %s]"),
8566 dwp_file->name);
8567 }
8568
8569 sectp = dwp_file->elf_sections[section_nr];
8570 if (! locate_virtual_dwo_sections (sectp, &sections))
8571 {
8572 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8573 " [in module %s]"),
8574 dwp_file->name);
8575 }
8576 }
8577
8578 if (i < 2
8579 || sections.info_or_types.asection == NULL
8580 || sections.abbrev.asection == NULL)
8581 {
8582 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8583 " [in module %s]"),
8584 dwp_file->name);
8585 }
8586 if (i == MAX_NR_DWO_SECTIONS)
8587 {
8588 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8589 " [in module %s]"),
8590 dwp_file->name);
8591 }
8592
8593 /* It's easier for the rest of the code if we fake a struct dwo_file and
8594 have dwo_unit "live" in that. At least for now.
8595
8596 The DWP file can be made up of a random collection of CUs and TUs.
8597 However, for each CU + set of TUs that came from the same original
8598 DWO file, we want combine them back into a virtual DWO file to save space
8599 (fewer struct dwo_file objects to allocated). Remember that for really
8600 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8601
8602 virtual_dwo_name =
8603 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8604 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8605 sections.line.asection ? sections.line.asection->id : 0,
8606 sections.loc.asection ? sections.loc.asection->id : 0,
8607 (sections.str_offsets.asection
8608 ? sections.str_offsets.asection->id
8609 : 0));
8610 make_cleanup (xfree, virtual_dwo_name);
8611 /* Can we use an existing virtual DWO file? */
8612 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8613 /* Create one if necessary. */
8614 if (*dwo_file_slot == NULL)
8615 {
8616 if (dwarf2_read_debug)
8617 {
8618 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8619 virtual_dwo_name);
8620 }
8621 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8622 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8623 virtual_dwo_name,
8624 strlen (virtual_dwo_name));
8625 dwo_file->sections.abbrev = sections.abbrev;
8626 dwo_file->sections.line = sections.line;
8627 dwo_file->sections.loc = sections.loc;
8628 dwo_file->sections.macinfo = sections.macinfo;
8629 dwo_file->sections.macro = sections.macro;
8630 dwo_file->sections.str_offsets = sections.str_offsets;
8631 /* The "str" section is global to the entire DWP file. */
8632 dwo_file->sections.str = dwp_file->sections.str;
8633 /* The info or types section is assigned later to dwo_unit,
8634 there's no need to record it in dwo_file.
8635 Also, we can't simply record type sections in dwo_file because
8636 we record a pointer into the vector in dwo_unit. As we collect more
8637 types we'll grow the vector and eventually have to reallocate space
8638 for it, invalidating all the pointers into the current copy. */
8639 *dwo_file_slot = dwo_file;
8640 }
8641 else
8642 {
8643 if (dwarf2_read_debug)
8644 {
8645 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8646 virtual_dwo_name);
8647 }
8648 dwo_file = *dwo_file_slot;
8649 }
8650 do_cleanups (cleanups);
8651
8652 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8653 dwo_unit->dwo_file = dwo_file;
8654 dwo_unit->signature = signature;
8655 dwo_unit->info_or_types_section =
8656 obstack_alloc (&objfile->objfile_obstack,
8657 sizeof (struct dwarf2_section_info));
8658 *dwo_unit->info_or_types_section = sections.info_or_types;
8659 /* offset, length, type_offset_in_tu are set later. */
8660
8661 return dwo_unit;
8662 }
8663
8664 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8665
8666 static struct dwo_unit *
8667 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8668 const struct dwp_hash_table *htab,
8669 ULONGEST signature, int is_debug_types)
8670 {
8671 bfd *dbfd = dwp_file->dbfd;
8672 uint32_t mask = htab->nr_slots - 1;
8673 uint32_t hash = signature & mask;
8674 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8675 unsigned int i;
8676 void **slot;
8677 struct dwo_unit find_dwo_cu, *dwo_cu;
8678
8679 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8680 find_dwo_cu.signature = signature;
8681 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8682
8683 if (*slot != NULL)
8684 return *slot;
8685
8686 /* Use a for loop so that we don't loop forever on bad debug info. */
8687 for (i = 0; i < htab->nr_slots; ++i)
8688 {
8689 ULONGEST signature_in_table;
8690
8691 signature_in_table =
8692 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8693 if (signature_in_table == signature)
8694 {
8695 uint32_t section_index =
8696 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8697
8698 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8699 signature, is_debug_types);
8700 return *slot;
8701 }
8702 if (signature_in_table == 0)
8703 return NULL;
8704 hash = (hash + hash2) & mask;
8705 }
8706
8707 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8708 " [in module %s]"),
8709 dwp_file->name);
8710 }
8711
8712 /* Subroutine of open_dwop_file to simplify it.
8713 Open the file specified by FILE_NAME and hand it off to BFD for
8714 preliminary analysis. Return a newly initialized bfd *, which
8715 includes a canonicalized copy of FILE_NAME.
8716 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8717 In case of trouble, return NULL.
8718 NOTE: This function is derived from symfile_bfd_open. */
8719
8720 static bfd *
8721 try_open_dwop_file (const char *file_name, int is_dwp)
8722 {
8723 bfd *sym_bfd;
8724 int desc, flags;
8725 char *absolute_name;
8726
8727 flags = OPF_TRY_CWD_FIRST;
8728 if (is_dwp)
8729 flags |= OPF_SEARCH_IN_PATH;
8730 desc = openp (debug_file_directory, flags, file_name,
8731 O_RDONLY | O_BINARY, &absolute_name);
8732 if (desc < 0)
8733 return NULL;
8734
8735 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8736 if (!sym_bfd)
8737 {
8738 xfree (absolute_name);
8739 return NULL;
8740 }
8741 xfree (absolute_name);
8742 bfd_set_cacheable (sym_bfd, 1);
8743
8744 if (!bfd_check_format (sym_bfd, bfd_object))
8745 {
8746 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8747 return NULL;
8748 }
8749
8750 return sym_bfd;
8751 }
8752
8753 /* Try to open DWO/DWP file FILE_NAME.
8754 COMP_DIR is the DW_AT_comp_dir attribute.
8755 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8756 The result is the bfd handle of the file.
8757 If there is a problem finding or opening the file, return NULL.
8758 Upon success, the canonicalized path of the file is stored in the bfd,
8759 same as symfile_bfd_open. */
8760
8761 static bfd *
8762 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8763 {
8764 bfd *abfd;
8765
8766 if (IS_ABSOLUTE_PATH (file_name))
8767 return try_open_dwop_file (file_name, is_dwp);
8768
8769 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8770
8771 if (comp_dir != NULL)
8772 {
8773 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8774
8775 /* NOTE: If comp_dir is a relative path, this will also try the
8776 search path, which seems useful. */
8777 abfd = try_open_dwop_file (path_to_try, is_dwp);
8778 xfree (path_to_try);
8779 if (abfd != NULL)
8780 return abfd;
8781 }
8782
8783 /* That didn't work, try debug-file-directory, which, despite its name,
8784 is a list of paths. */
8785
8786 if (*debug_file_directory == '\0')
8787 return NULL;
8788
8789 return try_open_dwop_file (file_name, is_dwp);
8790 }
8791
8792 /* This function is mapped across the sections and remembers the offset and
8793 size of each of the DWO debugging sections we are interested in. */
8794
8795 static void
8796 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8797 {
8798 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8799 const struct dwop_section_names *names = &dwop_section_names;
8800
8801 if (section_is_p (sectp->name, &names->abbrev_dwo))
8802 {
8803 dwo_sections->abbrev.asection = sectp;
8804 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8805 }
8806 else if (section_is_p (sectp->name, &names->info_dwo))
8807 {
8808 dwo_sections->info.asection = sectp;
8809 dwo_sections->info.size = bfd_get_section_size (sectp);
8810 }
8811 else if (section_is_p (sectp->name, &names->line_dwo))
8812 {
8813 dwo_sections->line.asection = sectp;
8814 dwo_sections->line.size = bfd_get_section_size (sectp);
8815 }
8816 else if (section_is_p (sectp->name, &names->loc_dwo))
8817 {
8818 dwo_sections->loc.asection = sectp;
8819 dwo_sections->loc.size = bfd_get_section_size (sectp);
8820 }
8821 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8822 {
8823 dwo_sections->macinfo.asection = sectp;
8824 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8825 }
8826 else if (section_is_p (sectp->name, &names->macro_dwo))
8827 {
8828 dwo_sections->macro.asection = sectp;
8829 dwo_sections->macro.size = bfd_get_section_size (sectp);
8830 }
8831 else if (section_is_p (sectp->name, &names->str_dwo))
8832 {
8833 dwo_sections->str.asection = sectp;
8834 dwo_sections->str.size = bfd_get_section_size (sectp);
8835 }
8836 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8837 {
8838 dwo_sections->str_offsets.asection = sectp;
8839 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8840 }
8841 else if (section_is_p (sectp->name, &names->types_dwo))
8842 {
8843 struct dwarf2_section_info type_section;
8844
8845 memset (&type_section, 0, sizeof (type_section));
8846 type_section.asection = sectp;
8847 type_section.size = bfd_get_section_size (sectp);
8848 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8849 &type_section);
8850 }
8851 }
8852
8853 /* Initialize the use of the DWO file specified by DWO_NAME.
8854 The result is NULL if DWO_NAME can't be found. */
8855
8856 static struct dwo_file *
8857 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8858 {
8859 struct objfile *objfile = dwarf2_per_objfile->objfile;
8860 struct dwo_file *dwo_file;
8861 bfd *dbfd;
8862 struct cleanup *cleanups;
8863
8864 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8865 if (dbfd == NULL)
8866 {
8867 if (dwarf2_read_debug)
8868 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8869 return NULL;
8870 }
8871 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8872 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8873 dwo_name, strlen (dwo_name));
8874 dwo_file->dbfd = dbfd;
8875
8876 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8877
8878 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8879
8880 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8881
8882 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8883 dwo_file->sections.types);
8884
8885 discard_cleanups (cleanups);
8886
8887 if (dwarf2_read_debug)
8888 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8889
8890 return dwo_file;
8891 }
8892
8893 /* This function is mapped across the sections and remembers the offset and
8894 size of each of the DWP debugging sections we are interested in. */
8895
8896 static void
8897 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8898 {
8899 struct dwp_file *dwp_file = dwp_file_ptr;
8900 const struct dwop_section_names *names = &dwop_section_names;
8901 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8902
8903 /* Record the ELF section number for later lookup: this is what the
8904 .debug_cu_index,.debug_tu_index tables use. */
8905 gdb_assert (elf_section_nr < dwp_file->num_sections);
8906 dwp_file->elf_sections[elf_section_nr] = sectp;
8907
8908 /* Look for specific sections that we need. */
8909 if (section_is_p (sectp->name, &names->str_dwo))
8910 {
8911 dwp_file->sections.str.asection = sectp;
8912 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8913 }
8914 else if (section_is_p (sectp->name, &names->cu_index))
8915 {
8916 dwp_file->sections.cu_index.asection = sectp;
8917 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8918 }
8919 else if (section_is_p (sectp->name, &names->tu_index))
8920 {
8921 dwp_file->sections.tu_index.asection = sectp;
8922 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8923 }
8924 }
8925
8926 /* Hash function for dwp_file loaded CUs/TUs. */
8927
8928 static hashval_t
8929 hash_dwp_loaded_cutus (const void *item)
8930 {
8931 const struct dwo_unit *dwo_unit = item;
8932
8933 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8934 return dwo_unit->signature;
8935 }
8936
8937 /* Equality function for dwp_file loaded CUs/TUs. */
8938
8939 static int
8940 eq_dwp_loaded_cutus (const void *a, const void *b)
8941 {
8942 const struct dwo_unit *dua = a;
8943 const struct dwo_unit *dub = b;
8944
8945 return dua->signature == dub->signature;
8946 }
8947
8948 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
8949
8950 static htab_t
8951 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8952 {
8953 return htab_create_alloc_ex (3,
8954 hash_dwp_loaded_cutus,
8955 eq_dwp_loaded_cutus,
8956 NULL,
8957 &objfile->objfile_obstack,
8958 hashtab_obstack_allocate,
8959 dummy_obstack_deallocate);
8960 }
8961
8962 /* Initialize the use of the DWP file for the current objfile.
8963 By convention the name of the DWP file is ${objfile}.dwp.
8964 The result is NULL if it can't be found. */
8965
8966 static struct dwp_file *
8967 open_and_init_dwp_file (const char *comp_dir)
8968 {
8969 struct objfile *objfile = dwarf2_per_objfile->objfile;
8970 struct dwp_file *dwp_file;
8971 char *dwp_name;
8972 bfd *dbfd;
8973 struct cleanup *cleanups;
8974
8975 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
8976 cleanups = make_cleanup (xfree, dwp_name);
8977
8978 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8979 if (dbfd == NULL)
8980 {
8981 if (dwarf2_read_debug)
8982 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8983 do_cleanups (cleanups);
8984 return NULL;
8985 }
8986 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8987 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8988 dwp_name, strlen (dwp_name));
8989 dwp_file->dbfd = dbfd;
8990 do_cleanups (cleanups);
8991
8992 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
8993
8994 /* +1: section 0 is unused */
8995 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
8996 dwp_file->elf_sections =
8997 OBSTACK_CALLOC (&objfile->objfile_obstack,
8998 dwp_file->num_sections, asection *);
8999
9000 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9001
9002 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9003
9004 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9005
9006 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9007
9008 discard_cleanups (cleanups);
9009
9010 if (dwarf2_read_debug)
9011 {
9012 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9013 fprintf_unfiltered (gdb_stdlog,
9014 " %u CUs, %u TUs\n",
9015 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9016 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9017 }
9018
9019 return dwp_file;
9020 }
9021
9022 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9023 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9024 or in the DWP file for the objfile, referenced by THIS_UNIT.
9025 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9026 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9027
9028 This is called, for example, when wanting to read a variable with a
9029 complex location. Therefore we don't want to do file i/o for every call.
9030 Therefore we don't want to look for a DWO file on every call.
9031 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9032 then we check if we've already seen DWO_NAME, and only THEN do we check
9033 for a DWO file.
9034
9035 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9036 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9037
9038 static struct dwo_unit *
9039 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9040 const char *dwo_name, const char *comp_dir,
9041 ULONGEST signature, int is_debug_types)
9042 {
9043 struct objfile *objfile = dwarf2_per_objfile->objfile;
9044 const char *kind = is_debug_types ? "TU" : "CU";
9045 void **dwo_file_slot;
9046 struct dwo_file *dwo_file;
9047 struct dwp_file *dwp_file;
9048
9049 /* Have we already read SIGNATURE from a DWP file? */
9050
9051 if (! dwarf2_per_objfile->dwp_checked)
9052 {
9053 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9054 dwarf2_per_objfile->dwp_checked = 1;
9055 }
9056 dwp_file = dwarf2_per_objfile->dwp_file;
9057
9058 if (dwp_file != NULL)
9059 {
9060 const struct dwp_hash_table *dwp_htab =
9061 is_debug_types ? dwp_file->tus : dwp_file->cus;
9062
9063 if (dwp_htab != NULL)
9064 {
9065 struct dwo_unit *dwo_cutu =
9066 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9067
9068 if (dwo_cutu != NULL)
9069 {
9070 if (dwarf2_read_debug)
9071 {
9072 fprintf_unfiltered (gdb_stdlog,
9073 "Virtual DWO %s %s found: @%s\n",
9074 kind, hex_string (signature),
9075 host_address_to_string (dwo_cutu));
9076 }
9077 return dwo_cutu;
9078 }
9079 }
9080 }
9081
9082 /* Have we already seen DWO_NAME? */
9083
9084 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9085 if (*dwo_file_slot == NULL)
9086 {
9087 /* Read in the file and build a table of the DWOs it contains. */
9088 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9089 }
9090 /* NOTE: This will be NULL if unable to open the file. */
9091 dwo_file = *dwo_file_slot;
9092
9093 if (dwo_file != NULL)
9094 {
9095 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9096
9097 if (htab != NULL)
9098 {
9099 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9100
9101 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9102 find_dwo_cutu.signature = signature;
9103 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9104
9105 if (dwo_cutu != NULL)
9106 {
9107 if (dwarf2_read_debug)
9108 {
9109 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9110 kind, dwo_name, hex_string (signature),
9111 host_address_to_string (dwo_cutu));
9112 }
9113 return dwo_cutu;
9114 }
9115 }
9116 }
9117
9118 /* We didn't find it. This could mean a dwo_id mismatch, or
9119 someone deleted the DWO/DWP file, or the search path isn't set up
9120 correctly to find the file. */
9121
9122 if (dwarf2_read_debug)
9123 {
9124 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9125 kind, dwo_name, hex_string (signature));
9126 }
9127
9128 complaint (&symfile_complaints,
9129 _("Could not find DWO CU referenced by CU at offset 0x%x"
9130 " [in module %s]"),
9131 this_unit->offset.sect_off, objfile->name);
9132 return NULL;
9133 }
9134
9135 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9136 See lookup_dwo_cutu_unit for details. */
9137
9138 static struct dwo_unit *
9139 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9140 const char *dwo_name, const char *comp_dir,
9141 ULONGEST signature)
9142 {
9143 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9144 }
9145
9146 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9147 See lookup_dwo_cutu_unit for details. */
9148
9149 static struct dwo_unit *
9150 lookup_dwo_type_unit (struct signatured_type *this_tu,
9151 const char *dwo_name, const char *comp_dir)
9152 {
9153 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9154 }
9155
9156 /* Free all resources associated with DWO_FILE.
9157 Close the DWO file and munmap the sections.
9158 All memory should be on the objfile obstack. */
9159
9160 static void
9161 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9162 {
9163 int ix;
9164 struct dwarf2_section_info *section;
9165
9166 gdb_assert (dwo_file->dbfd != objfile->obfd);
9167 gdb_bfd_unref (dwo_file->dbfd);
9168
9169 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9170 }
9171
9172 /* Wrapper for free_dwo_file for use in cleanups. */
9173
9174 static void
9175 free_dwo_file_cleanup (void *arg)
9176 {
9177 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9178 struct objfile *objfile = dwarf2_per_objfile->objfile;
9179
9180 free_dwo_file (dwo_file, objfile);
9181 }
9182
9183 /* Traversal function for free_dwo_files. */
9184
9185 static int
9186 free_dwo_file_from_slot (void **slot, void *info)
9187 {
9188 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9189 struct objfile *objfile = (struct objfile *) info;
9190
9191 free_dwo_file (dwo_file, objfile);
9192
9193 return 1;
9194 }
9195
9196 /* Free all resources associated with DWO_FILES. */
9197
9198 static void
9199 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9200 {
9201 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9202 }
9203 \f
9204 /* Read in various DIEs. */
9205
9206 /* qsort helper for inherit_abstract_dies. */
9207
9208 static int
9209 unsigned_int_compar (const void *ap, const void *bp)
9210 {
9211 unsigned int a = *(unsigned int *) ap;
9212 unsigned int b = *(unsigned int *) bp;
9213
9214 return (a > b) - (b > a);
9215 }
9216
9217 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9218 Inherit only the children of the DW_AT_abstract_origin DIE not being
9219 already referenced by DW_AT_abstract_origin from the children of the
9220 current DIE. */
9221
9222 static void
9223 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9224 {
9225 struct die_info *child_die;
9226 unsigned die_children_count;
9227 /* CU offsets which were referenced by children of the current DIE. */
9228 sect_offset *offsets;
9229 sect_offset *offsets_end, *offsetp;
9230 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9231 struct die_info *origin_die;
9232 /* Iterator of the ORIGIN_DIE children. */
9233 struct die_info *origin_child_die;
9234 struct cleanup *cleanups;
9235 struct attribute *attr;
9236 struct dwarf2_cu *origin_cu;
9237 struct pending **origin_previous_list_in_scope;
9238
9239 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9240 if (!attr)
9241 return;
9242
9243 /* Note that following die references may follow to a die in a
9244 different cu. */
9245
9246 origin_cu = cu;
9247 origin_die = follow_die_ref (die, attr, &origin_cu);
9248
9249 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9250 symbols in. */
9251 origin_previous_list_in_scope = origin_cu->list_in_scope;
9252 origin_cu->list_in_scope = cu->list_in_scope;
9253
9254 if (die->tag != origin_die->tag
9255 && !(die->tag == DW_TAG_inlined_subroutine
9256 && origin_die->tag == DW_TAG_subprogram))
9257 complaint (&symfile_complaints,
9258 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9259 die->offset.sect_off, origin_die->offset.sect_off);
9260
9261 child_die = die->child;
9262 die_children_count = 0;
9263 while (child_die && child_die->tag)
9264 {
9265 child_die = sibling_die (child_die);
9266 die_children_count++;
9267 }
9268 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9269 cleanups = make_cleanup (xfree, offsets);
9270
9271 offsets_end = offsets;
9272 child_die = die->child;
9273 while (child_die && child_die->tag)
9274 {
9275 /* For each CHILD_DIE, find the corresponding child of
9276 ORIGIN_DIE. If there is more than one layer of
9277 DW_AT_abstract_origin, follow them all; there shouldn't be,
9278 but GCC versions at least through 4.4 generate this (GCC PR
9279 40573). */
9280 struct die_info *child_origin_die = child_die;
9281 struct dwarf2_cu *child_origin_cu = cu;
9282
9283 while (1)
9284 {
9285 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9286 child_origin_cu);
9287 if (attr == NULL)
9288 break;
9289 child_origin_die = follow_die_ref (child_origin_die, attr,
9290 &child_origin_cu);
9291 }
9292
9293 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9294 counterpart may exist. */
9295 if (child_origin_die != child_die)
9296 {
9297 if (child_die->tag != child_origin_die->tag
9298 && !(child_die->tag == DW_TAG_inlined_subroutine
9299 && child_origin_die->tag == DW_TAG_subprogram))
9300 complaint (&symfile_complaints,
9301 _("Child DIE 0x%x and its abstract origin 0x%x have "
9302 "different tags"), child_die->offset.sect_off,
9303 child_origin_die->offset.sect_off);
9304 if (child_origin_die->parent != origin_die)
9305 complaint (&symfile_complaints,
9306 _("Child DIE 0x%x and its abstract origin 0x%x have "
9307 "different parents"), child_die->offset.sect_off,
9308 child_origin_die->offset.sect_off);
9309 else
9310 *offsets_end++ = child_origin_die->offset;
9311 }
9312 child_die = sibling_die (child_die);
9313 }
9314 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9315 unsigned_int_compar);
9316 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9317 if (offsetp[-1].sect_off == offsetp->sect_off)
9318 complaint (&symfile_complaints,
9319 _("Multiple children of DIE 0x%x refer "
9320 "to DIE 0x%x as their abstract origin"),
9321 die->offset.sect_off, offsetp->sect_off);
9322
9323 offsetp = offsets;
9324 origin_child_die = origin_die->child;
9325 while (origin_child_die && origin_child_die->tag)
9326 {
9327 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9328 while (offsetp < offsets_end
9329 && offsetp->sect_off < origin_child_die->offset.sect_off)
9330 offsetp++;
9331 if (offsetp >= offsets_end
9332 || offsetp->sect_off > origin_child_die->offset.sect_off)
9333 {
9334 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9335 process_die (origin_child_die, origin_cu);
9336 }
9337 origin_child_die = sibling_die (origin_child_die);
9338 }
9339 origin_cu->list_in_scope = origin_previous_list_in_scope;
9340
9341 do_cleanups (cleanups);
9342 }
9343
9344 static void
9345 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9346 {
9347 struct objfile *objfile = cu->objfile;
9348 struct context_stack *new;
9349 CORE_ADDR lowpc;
9350 CORE_ADDR highpc;
9351 struct die_info *child_die;
9352 struct attribute *attr, *call_line, *call_file;
9353 char *name;
9354 CORE_ADDR baseaddr;
9355 struct block *block;
9356 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9357 VEC (symbolp) *template_args = NULL;
9358 struct template_symbol *templ_func = NULL;
9359
9360 if (inlined_func)
9361 {
9362 /* If we do not have call site information, we can't show the
9363 caller of this inlined function. That's too confusing, so
9364 only use the scope for local variables. */
9365 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9366 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9367 if (call_line == NULL || call_file == NULL)
9368 {
9369 read_lexical_block_scope (die, cu);
9370 return;
9371 }
9372 }
9373
9374 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9375
9376 name = dwarf2_name (die, cu);
9377
9378 /* Ignore functions with missing or empty names. These are actually
9379 illegal according to the DWARF standard. */
9380 if (name == NULL)
9381 {
9382 complaint (&symfile_complaints,
9383 _("missing name for subprogram DIE at %d"),
9384 die->offset.sect_off);
9385 return;
9386 }
9387
9388 /* Ignore functions with missing or invalid low and high pc attributes. */
9389 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9390 {
9391 attr = dwarf2_attr (die, DW_AT_external, cu);
9392 if (!attr || !DW_UNSND (attr))
9393 complaint (&symfile_complaints,
9394 _("cannot get low and high bounds "
9395 "for subprogram DIE at %d"),
9396 die->offset.sect_off);
9397 return;
9398 }
9399
9400 lowpc += baseaddr;
9401 highpc += baseaddr;
9402
9403 /* If we have any template arguments, then we must allocate a
9404 different sort of symbol. */
9405 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9406 {
9407 if (child_die->tag == DW_TAG_template_type_param
9408 || child_die->tag == DW_TAG_template_value_param)
9409 {
9410 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9411 struct template_symbol);
9412 templ_func->base.is_cplus_template_function = 1;
9413 break;
9414 }
9415 }
9416
9417 new = push_context (0, lowpc);
9418 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9419 (struct symbol *) templ_func);
9420
9421 /* If there is a location expression for DW_AT_frame_base, record
9422 it. */
9423 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9424 if (attr)
9425 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9426 expression is being recorded directly in the function's symbol
9427 and not in a separate frame-base object. I guess this hack is
9428 to avoid adding some sort of frame-base adjunct/annex to the
9429 function's symbol :-(. The problem with doing this is that it
9430 results in a function symbol with a location expression that
9431 has nothing to do with the location of the function, ouch! The
9432 relationship should be: a function's symbol has-a frame base; a
9433 frame-base has-a location expression. */
9434 dwarf2_symbol_mark_computed (attr, new->name, cu);
9435
9436 cu->list_in_scope = &local_symbols;
9437
9438 if (die->child != NULL)
9439 {
9440 child_die = die->child;
9441 while (child_die && child_die->tag)
9442 {
9443 if (child_die->tag == DW_TAG_template_type_param
9444 || child_die->tag == DW_TAG_template_value_param)
9445 {
9446 struct symbol *arg = new_symbol (child_die, NULL, cu);
9447
9448 if (arg != NULL)
9449 VEC_safe_push (symbolp, template_args, arg);
9450 }
9451 else
9452 process_die (child_die, cu);
9453 child_die = sibling_die (child_die);
9454 }
9455 }
9456
9457 inherit_abstract_dies (die, cu);
9458
9459 /* If we have a DW_AT_specification, we might need to import using
9460 directives from the context of the specification DIE. See the
9461 comment in determine_prefix. */
9462 if (cu->language == language_cplus
9463 && dwarf2_attr (die, DW_AT_specification, cu))
9464 {
9465 struct dwarf2_cu *spec_cu = cu;
9466 struct die_info *spec_die = die_specification (die, &spec_cu);
9467
9468 while (spec_die)
9469 {
9470 child_die = spec_die->child;
9471 while (child_die && child_die->tag)
9472 {
9473 if (child_die->tag == DW_TAG_imported_module)
9474 process_die (child_die, spec_cu);
9475 child_die = sibling_die (child_die);
9476 }
9477
9478 /* In some cases, GCC generates specification DIEs that
9479 themselves contain DW_AT_specification attributes. */
9480 spec_die = die_specification (spec_die, &spec_cu);
9481 }
9482 }
9483
9484 new = pop_context ();
9485 /* Make a block for the local symbols within. */
9486 block = finish_block (new->name, &local_symbols, new->old_blocks,
9487 lowpc, highpc, objfile);
9488
9489 /* For C++, set the block's scope. */
9490 if (cu->language == language_cplus || cu->language == language_fortran)
9491 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
9492 determine_prefix (die, cu),
9493 processing_has_namespace_info);
9494
9495 /* If we have address ranges, record them. */
9496 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9497
9498 /* Attach template arguments to function. */
9499 if (! VEC_empty (symbolp, template_args))
9500 {
9501 gdb_assert (templ_func != NULL);
9502
9503 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9504 templ_func->template_arguments
9505 = obstack_alloc (&objfile->objfile_obstack,
9506 (templ_func->n_template_arguments
9507 * sizeof (struct symbol *)));
9508 memcpy (templ_func->template_arguments,
9509 VEC_address (symbolp, template_args),
9510 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9511 VEC_free (symbolp, template_args);
9512 }
9513
9514 /* In C++, we can have functions nested inside functions (e.g., when
9515 a function declares a class that has methods). This means that
9516 when we finish processing a function scope, we may need to go
9517 back to building a containing block's symbol lists. */
9518 local_symbols = new->locals;
9519 using_directives = new->using_directives;
9520
9521 /* If we've finished processing a top-level function, subsequent
9522 symbols go in the file symbol list. */
9523 if (outermost_context_p ())
9524 cu->list_in_scope = &file_symbols;
9525 }
9526
9527 /* Process all the DIES contained within a lexical block scope. Start
9528 a new scope, process the dies, and then close the scope. */
9529
9530 static void
9531 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9532 {
9533 struct objfile *objfile = cu->objfile;
9534 struct context_stack *new;
9535 CORE_ADDR lowpc, highpc;
9536 struct die_info *child_die;
9537 CORE_ADDR baseaddr;
9538
9539 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9540
9541 /* Ignore blocks with missing or invalid low and high pc attributes. */
9542 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9543 as multiple lexical blocks? Handling children in a sane way would
9544 be nasty. Might be easier to properly extend generic blocks to
9545 describe ranges. */
9546 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9547 return;
9548 lowpc += baseaddr;
9549 highpc += baseaddr;
9550
9551 push_context (0, lowpc);
9552 if (die->child != NULL)
9553 {
9554 child_die = die->child;
9555 while (child_die && child_die->tag)
9556 {
9557 process_die (child_die, cu);
9558 child_die = sibling_die (child_die);
9559 }
9560 }
9561 new = pop_context ();
9562
9563 if (local_symbols != NULL || using_directives != NULL)
9564 {
9565 struct block *block
9566 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9567 highpc, objfile);
9568
9569 /* Note that recording ranges after traversing children, as we
9570 do here, means that recording a parent's ranges entails
9571 walking across all its children's ranges as they appear in
9572 the address map, which is quadratic behavior.
9573
9574 It would be nicer to record the parent's ranges before
9575 traversing its children, simply overriding whatever you find
9576 there. But since we don't even decide whether to create a
9577 block until after we've traversed its children, that's hard
9578 to do. */
9579 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9580 }
9581 local_symbols = new->locals;
9582 using_directives = new->using_directives;
9583 }
9584
9585 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9586
9587 static void
9588 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9589 {
9590 struct objfile *objfile = cu->objfile;
9591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9592 CORE_ADDR pc, baseaddr;
9593 struct attribute *attr;
9594 struct call_site *call_site, call_site_local;
9595 void **slot;
9596 int nparams;
9597 struct die_info *child_die;
9598
9599 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9600
9601 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9602 if (!attr)
9603 {
9604 complaint (&symfile_complaints,
9605 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9606 "DIE 0x%x [in module %s]"),
9607 die->offset.sect_off, objfile->name);
9608 return;
9609 }
9610 pc = DW_ADDR (attr) + baseaddr;
9611
9612 if (cu->call_site_htab == NULL)
9613 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9614 NULL, &objfile->objfile_obstack,
9615 hashtab_obstack_allocate, NULL);
9616 call_site_local.pc = pc;
9617 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9618 if (*slot != NULL)
9619 {
9620 complaint (&symfile_complaints,
9621 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9622 "DIE 0x%x [in module %s]"),
9623 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9624 return;
9625 }
9626
9627 /* Count parameters at the caller. */
9628
9629 nparams = 0;
9630 for (child_die = die->child; child_die && child_die->tag;
9631 child_die = sibling_die (child_die))
9632 {
9633 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9634 {
9635 complaint (&symfile_complaints,
9636 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9637 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9638 child_die->tag, child_die->offset.sect_off, objfile->name);
9639 continue;
9640 }
9641
9642 nparams++;
9643 }
9644
9645 call_site = obstack_alloc (&objfile->objfile_obstack,
9646 (sizeof (*call_site)
9647 + (sizeof (*call_site->parameter)
9648 * (nparams - 1))));
9649 *slot = call_site;
9650 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9651 call_site->pc = pc;
9652
9653 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9654 {
9655 struct die_info *func_die;
9656
9657 /* Skip also over DW_TAG_inlined_subroutine. */
9658 for (func_die = die->parent;
9659 func_die && func_die->tag != DW_TAG_subprogram
9660 && func_die->tag != DW_TAG_subroutine_type;
9661 func_die = func_die->parent);
9662
9663 /* DW_AT_GNU_all_call_sites is a superset
9664 of DW_AT_GNU_all_tail_call_sites. */
9665 if (func_die
9666 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9667 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9668 {
9669 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9670 not complete. But keep CALL_SITE for look ups via call_site_htab,
9671 both the initial caller containing the real return address PC and
9672 the final callee containing the current PC of a chain of tail
9673 calls do not need to have the tail call list complete. But any
9674 function candidate for a virtual tail call frame searched via
9675 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9676 determined unambiguously. */
9677 }
9678 else
9679 {
9680 struct type *func_type = NULL;
9681
9682 if (func_die)
9683 func_type = get_die_type (func_die, cu);
9684 if (func_type != NULL)
9685 {
9686 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9687
9688 /* Enlist this call site to the function. */
9689 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9690 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9691 }
9692 else
9693 complaint (&symfile_complaints,
9694 _("Cannot find function owning DW_TAG_GNU_call_site "
9695 "DIE 0x%x [in module %s]"),
9696 die->offset.sect_off, objfile->name);
9697 }
9698 }
9699
9700 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9701 if (attr == NULL)
9702 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9703 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9704 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9705 /* Keep NULL DWARF_BLOCK. */;
9706 else if (attr_form_is_block (attr))
9707 {
9708 struct dwarf2_locexpr_baton *dlbaton;
9709
9710 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9711 dlbaton->data = DW_BLOCK (attr)->data;
9712 dlbaton->size = DW_BLOCK (attr)->size;
9713 dlbaton->per_cu = cu->per_cu;
9714
9715 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9716 }
9717 else if (is_ref_attr (attr))
9718 {
9719 struct dwarf2_cu *target_cu = cu;
9720 struct die_info *target_die;
9721
9722 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9723 gdb_assert (target_cu->objfile == objfile);
9724 if (die_is_declaration (target_die, target_cu))
9725 {
9726 const char *target_physname;
9727
9728 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9729 if (target_physname == NULL)
9730 complaint (&symfile_complaints,
9731 _("DW_AT_GNU_call_site_target target DIE has invalid "
9732 "physname, for referencing DIE 0x%x [in module %s]"),
9733 die->offset.sect_off, objfile->name);
9734 else
9735 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9736 }
9737 else
9738 {
9739 CORE_ADDR lowpc;
9740
9741 /* DW_AT_entry_pc should be preferred. */
9742 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9743 complaint (&symfile_complaints,
9744 _("DW_AT_GNU_call_site_target target DIE has invalid "
9745 "low pc, for referencing DIE 0x%x [in module %s]"),
9746 die->offset.sect_off, objfile->name);
9747 else
9748 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9749 }
9750 }
9751 else
9752 complaint (&symfile_complaints,
9753 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9754 "block nor reference, for DIE 0x%x [in module %s]"),
9755 die->offset.sect_off, objfile->name);
9756
9757 call_site->per_cu = cu->per_cu;
9758
9759 for (child_die = die->child;
9760 child_die && child_die->tag;
9761 child_die = sibling_die (child_die))
9762 {
9763 struct call_site_parameter *parameter;
9764 struct attribute *loc, *origin;
9765
9766 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9767 {
9768 /* Already printed the complaint above. */
9769 continue;
9770 }
9771
9772 gdb_assert (call_site->parameter_count < nparams);
9773 parameter = &call_site->parameter[call_site->parameter_count];
9774
9775 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9776 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9777 register is contained in DW_AT_GNU_call_site_value. */
9778
9779 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9780 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9781 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9782 {
9783 sect_offset offset;
9784
9785 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9786 offset = dwarf2_get_ref_die_offset (origin);
9787 if (!offset_in_cu_p (&cu->header, offset))
9788 {
9789 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9790 binding can be done only inside one CU. Such referenced DIE
9791 therefore cannot be even moved to DW_TAG_partial_unit. */
9792 complaint (&symfile_complaints,
9793 _("DW_AT_abstract_origin offset is not in CU for "
9794 "DW_TAG_GNU_call_site child DIE 0x%x "
9795 "[in module %s]"),
9796 child_die->offset.sect_off, objfile->name);
9797 continue;
9798 }
9799 parameter->u.param_offset.cu_off = (offset.sect_off
9800 - cu->header.offset.sect_off);
9801 }
9802 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9803 {
9804 complaint (&symfile_complaints,
9805 _("No DW_FORM_block* DW_AT_location for "
9806 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9807 child_die->offset.sect_off, objfile->name);
9808 continue;
9809 }
9810 else
9811 {
9812 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9813 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9814 if (parameter->u.dwarf_reg != -1)
9815 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9816 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9817 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9818 &parameter->u.fb_offset))
9819 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9820 else
9821 {
9822 complaint (&symfile_complaints,
9823 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9824 "for DW_FORM_block* DW_AT_location is supported for "
9825 "DW_TAG_GNU_call_site child DIE 0x%x "
9826 "[in module %s]"),
9827 child_die->offset.sect_off, objfile->name);
9828 continue;
9829 }
9830 }
9831
9832 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9833 if (!attr_form_is_block (attr))
9834 {
9835 complaint (&symfile_complaints,
9836 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9838 child_die->offset.sect_off, objfile->name);
9839 continue;
9840 }
9841 parameter->value = DW_BLOCK (attr)->data;
9842 parameter->value_size = DW_BLOCK (attr)->size;
9843
9844 /* Parameters are not pre-cleared by memset above. */
9845 parameter->data_value = NULL;
9846 parameter->data_value_size = 0;
9847 call_site->parameter_count++;
9848
9849 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9850 if (attr)
9851 {
9852 if (!attr_form_is_block (attr))
9853 complaint (&symfile_complaints,
9854 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9855 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9856 child_die->offset.sect_off, objfile->name);
9857 else
9858 {
9859 parameter->data_value = DW_BLOCK (attr)->data;
9860 parameter->data_value_size = DW_BLOCK (attr)->size;
9861 }
9862 }
9863 }
9864 }
9865
9866 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9867 Return 1 if the attributes are present and valid, otherwise, return 0.
9868 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9869
9870 static int
9871 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9872 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9873 struct partial_symtab *ranges_pst)
9874 {
9875 struct objfile *objfile = cu->objfile;
9876 struct comp_unit_head *cu_header = &cu->header;
9877 bfd *obfd = objfile->obfd;
9878 unsigned int addr_size = cu_header->addr_size;
9879 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9880 /* Base address selection entry. */
9881 CORE_ADDR base;
9882 int found_base;
9883 unsigned int dummy;
9884 gdb_byte *buffer;
9885 CORE_ADDR marker;
9886 int low_set;
9887 CORE_ADDR low = 0;
9888 CORE_ADDR high = 0;
9889 CORE_ADDR baseaddr;
9890
9891 found_base = cu->base_known;
9892 base = cu->base_address;
9893
9894 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9895 if (offset >= dwarf2_per_objfile->ranges.size)
9896 {
9897 complaint (&symfile_complaints,
9898 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9899 offset);
9900 return 0;
9901 }
9902 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9903
9904 /* Read in the largest possible address. */
9905 marker = read_address (obfd, buffer, cu, &dummy);
9906 if ((marker & mask) == mask)
9907 {
9908 /* If we found the largest possible address, then
9909 read the base address. */
9910 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9911 buffer += 2 * addr_size;
9912 offset += 2 * addr_size;
9913 found_base = 1;
9914 }
9915
9916 low_set = 0;
9917
9918 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9919
9920 while (1)
9921 {
9922 CORE_ADDR range_beginning, range_end;
9923
9924 range_beginning = read_address (obfd, buffer, cu, &dummy);
9925 buffer += addr_size;
9926 range_end = read_address (obfd, buffer, cu, &dummy);
9927 buffer += addr_size;
9928 offset += 2 * addr_size;
9929
9930 /* An end of list marker is a pair of zero addresses. */
9931 if (range_beginning == 0 && range_end == 0)
9932 /* Found the end of list entry. */
9933 break;
9934
9935 /* Each base address selection entry is a pair of 2 values.
9936 The first is the largest possible address, the second is
9937 the base address. Check for a base address here. */
9938 if ((range_beginning & mask) == mask)
9939 {
9940 /* If we found the largest possible address, then
9941 read the base address. */
9942 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9943 found_base = 1;
9944 continue;
9945 }
9946
9947 if (!found_base)
9948 {
9949 /* We have no valid base address for the ranges
9950 data. */
9951 complaint (&symfile_complaints,
9952 _("Invalid .debug_ranges data (no base address)"));
9953 return 0;
9954 }
9955
9956 if (range_beginning > range_end)
9957 {
9958 /* Inverted range entries are invalid. */
9959 complaint (&symfile_complaints,
9960 _("Invalid .debug_ranges data (inverted range)"));
9961 return 0;
9962 }
9963
9964 /* Empty range entries have no effect. */
9965 if (range_beginning == range_end)
9966 continue;
9967
9968 range_beginning += base;
9969 range_end += base;
9970
9971 /* A not-uncommon case of bad debug info.
9972 Don't pollute the addrmap with bad data. */
9973 if (range_beginning + baseaddr == 0
9974 && !dwarf2_per_objfile->has_section_at_zero)
9975 {
9976 complaint (&symfile_complaints,
9977 _(".debug_ranges entry has start address of zero"
9978 " [in module %s]"), objfile->name);
9979 continue;
9980 }
9981
9982 if (ranges_pst != NULL)
9983 addrmap_set_empty (objfile->psymtabs_addrmap,
9984 range_beginning + baseaddr,
9985 range_end - 1 + baseaddr,
9986 ranges_pst);
9987
9988 /* FIXME: This is recording everything as a low-high
9989 segment of consecutive addresses. We should have a
9990 data structure for discontiguous block ranges
9991 instead. */
9992 if (! low_set)
9993 {
9994 low = range_beginning;
9995 high = range_end;
9996 low_set = 1;
9997 }
9998 else
9999 {
10000 if (range_beginning < low)
10001 low = range_beginning;
10002 if (range_end > high)
10003 high = range_end;
10004 }
10005 }
10006
10007 if (! low_set)
10008 /* If the first entry is an end-of-list marker, the range
10009 describes an empty scope, i.e. no instructions. */
10010 return 0;
10011
10012 if (low_return)
10013 *low_return = low;
10014 if (high_return)
10015 *high_return = high;
10016 return 1;
10017 }
10018
10019 /* Get low and high pc attributes from a die. Return 1 if the attributes
10020 are present and valid, otherwise, return 0. Return -1 if the range is
10021 discontinuous, i.e. derived from DW_AT_ranges information. */
10022
10023 static int
10024 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10025 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10026 struct partial_symtab *pst)
10027 {
10028 struct attribute *attr;
10029 struct attribute *attr_high;
10030 CORE_ADDR low = 0;
10031 CORE_ADDR high = 0;
10032 int ret = 0;
10033
10034 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10035 if (attr_high)
10036 {
10037 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10038 if (attr)
10039 {
10040 low = DW_ADDR (attr);
10041 if (attr_high->form == DW_FORM_addr
10042 || attr_high->form == DW_FORM_GNU_addr_index)
10043 high = DW_ADDR (attr_high);
10044 else
10045 high = low + DW_UNSND (attr_high);
10046 }
10047 else
10048 /* Found high w/o low attribute. */
10049 return 0;
10050
10051 /* Found consecutive range of addresses. */
10052 ret = 1;
10053 }
10054 else
10055 {
10056 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10057 if (attr != NULL)
10058 {
10059 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10060
10061 /* Value of the DW_AT_ranges attribute is the offset in the
10062 .debug_ranges section. */
10063 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10064 return 0;
10065 /* Found discontinuous range of addresses. */
10066 ret = -1;
10067 }
10068 }
10069
10070 /* read_partial_die has also the strict LOW < HIGH requirement. */
10071 if (high <= low)
10072 return 0;
10073
10074 /* When using the GNU linker, .gnu.linkonce. sections are used to
10075 eliminate duplicate copies of functions and vtables and such.
10076 The linker will arbitrarily choose one and discard the others.
10077 The AT_*_pc values for such functions refer to local labels in
10078 these sections. If the section from that file was discarded, the
10079 labels are not in the output, so the relocs get a value of 0.
10080 If this is a discarded function, mark the pc bounds as invalid,
10081 so that GDB will ignore it. */
10082 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10083 return 0;
10084
10085 *lowpc = low;
10086 if (highpc)
10087 *highpc = high;
10088 return ret;
10089 }
10090
10091 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10092 its low and high PC addresses. Do nothing if these addresses could not
10093 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10094 and HIGHPC to the high address if greater than HIGHPC. */
10095
10096 static void
10097 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10098 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10099 struct dwarf2_cu *cu)
10100 {
10101 CORE_ADDR low, high;
10102 struct die_info *child = die->child;
10103
10104 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10105 {
10106 *lowpc = min (*lowpc, low);
10107 *highpc = max (*highpc, high);
10108 }
10109
10110 /* If the language does not allow nested subprograms (either inside
10111 subprograms or lexical blocks), we're done. */
10112 if (cu->language != language_ada)
10113 return;
10114
10115 /* Check all the children of the given DIE. If it contains nested
10116 subprograms, then check their pc bounds. Likewise, we need to
10117 check lexical blocks as well, as they may also contain subprogram
10118 definitions. */
10119 while (child && child->tag)
10120 {
10121 if (child->tag == DW_TAG_subprogram
10122 || child->tag == DW_TAG_lexical_block)
10123 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10124 child = sibling_die (child);
10125 }
10126 }
10127
10128 /* Get the low and high pc's represented by the scope DIE, and store
10129 them in *LOWPC and *HIGHPC. If the correct values can't be
10130 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10131
10132 static void
10133 get_scope_pc_bounds (struct die_info *die,
10134 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10135 struct dwarf2_cu *cu)
10136 {
10137 CORE_ADDR best_low = (CORE_ADDR) -1;
10138 CORE_ADDR best_high = (CORE_ADDR) 0;
10139 CORE_ADDR current_low, current_high;
10140
10141 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10142 {
10143 best_low = current_low;
10144 best_high = current_high;
10145 }
10146 else
10147 {
10148 struct die_info *child = die->child;
10149
10150 while (child && child->tag)
10151 {
10152 switch (child->tag) {
10153 case DW_TAG_subprogram:
10154 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10155 break;
10156 case DW_TAG_namespace:
10157 case DW_TAG_module:
10158 /* FIXME: carlton/2004-01-16: Should we do this for
10159 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10160 that current GCC's always emit the DIEs corresponding
10161 to definitions of methods of classes as children of a
10162 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10163 the DIEs giving the declarations, which could be
10164 anywhere). But I don't see any reason why the
10165 standards says that they have to be there. */
10166 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10167
10168 if (current_low != ((CORE_ADDR) -1))
10169 {
10170 best_low = min (best_low, current_low);
10171 best_high = max (best_high, current_high);
10172 }
10173 break;
10174 default:
10175 /* Ignore. */
10176 break;
10177 }
10178
10179 child = sibling_die (child);
10180 }
10181 }
10182
10183 *lowpc = best_low;
10184 *highpc = best_high;
10185 }
10186
10187 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10188 in DIE. */
10189
10190 static void
10191 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10192 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10193 {
10194 struct objfile *objfile = cu->objfile;
10195 struct attribute *attr;
10196 struct attribute *attr_high;
10197
10198 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10199 if (attr_high)
10200 {
10201 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10202 if (attr)
10203 {
10204 CORE_ADDR low = DW_ADDR (attr);
10205 CORE_ADDR high;
10206 if (attr_high->form == DW_FORM_addr
10207 || attr_high->form == DW_FORM_GNU_addr_index)
10208 high = DW_ADDR (attr_high);
10209 else
10210 high = low + DW_UNSND (attr_high);
10211
10212 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10213 }
10214 }
10215
10216 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10217 if (attr)
10218 {
10219 bfd *obfd = objfile->obfd;
10220
10221 /* The value of the DW_AT_ranges attribute is the offset of the
10222 address range list in the .debug_ranges section. */
10223 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
10224 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10225
10226 /* For some target architectures, but not others, the
10227 read_address function sign-extends the addresses it returns.
10228 To recognize base address selection entries, we need a
10229 mask. */
10230 unsigned int addr_size = cu->header.addr_size;
10231 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10232
10233 /* The base address, to which the next pair is relative. Note
10234 that this 'base' is a DWARF concept: most entries in a range
10235 list are relative, to reduce the number of relocs against the
10236 debugging information. This is separate from this function's
10237 'baseaddr' argument, which GDB uses to relocate debugging
10238 information from a shared library based on the address at
10239 which the library was loaded. */
10240 CORE_ADDR base = cu->base_address;
10241 int base_known = cu->base_known;
10242
10243 gdb_assert (dwarf2_per_objfile->ranges.readin);
10244 if (offset >= dwarf2_per_objfile->ranges.size)
10245 {
10246 complaint (&symfile_complaints,
10247 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10248 offset);
10249 return;
10250 }
10251
10252 for (;;)
10253 {
10254 unsigned int bytes_read;
10255 CORE_ADDR start, end;
10256
10257 start = read_address (obfd, buffer, cu, &bytes_read);
10258 buffer += bytes_read;
10259 end = read_address (obfd, buffer, cu, &bytes_read);
10260 buffer += bytes_read;
10261
10262 /* Did we find the end of the range list? */
10263 if (start == 0 && end == 0)
10264 break;
10265
10266 /* Did we find a base address selection entry? */
10267 else if ((start & base_select_mask) == base_select_mask)
10268 {
10269 base = end;
10270 base_known = 1;
10271 }
10272
10273 /* We found an ordinary address range. */
10274 else
10275 {
10276 if (!base_known)
10277 {
10278 complaint (&symfile_complaints,
10279 _("Invalid .debug_ranges data "
10280 "(no base address)"));
10281 return;
10282 }
10283
10284 if (start > end)
10285 {
10286 /* Inverted range entries are invalid. */
10287 complaint (&symfile_complaints,
10288 _("Invalid .debug_ranges data "
10289 "(inverted range)"));
10290 return;
10291 }
10292
10293 /* Empty range entries have no effect. */
10294 if (start == end)
10295 continue;
10296
10297 start += base + baseaddr;
10298 end += base + baseaddr;
10299
10300 /* A not-uncommon case of bad debug info.
10301 Don't pollute the addrmap with bad data. */
10302 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10303 {
10304 complaint (&symfile_complaints,
10305 _(".debug_ranges entry has start address of zero"
10306 " [in module %s]"), objfile->name);
10307 continue;
10308 }
10309
10310 record_block_range (block, start, end - 1);
10311 }
10312 }
10313 }
10314 }
10315
10316 /* Check whether the producer field indicates either of GCC < 4.6, or the
10317 Intel C/C++ compiler, and cache the result in CU. */
10318
10319 static void
10320 check_producer (struct dwarf2_cu *cu)
10321 {
10322 const char *cs;
10323 int major, minor, release;
10324
10325 if (cu->producer == NULL)
10326 {
10327 /* For unknown compilers expect their behavior is DWARF version
10328 compliant.
10329
10330 GCC started to support .debug_types sections by -gdwarf-4 since
10331 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10332 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10333 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10334 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10335 }
10336 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10337 {
10338 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10339
10340 cs = &cu->producer[strlen ("GNU ")];
10341 while (*cs && !isdigit (*cs))
10342 cs++;
10343 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10344 {
10345 /* Not recognized as GCC. */
10346 }
10347 else
10348 {
10349 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10350 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10351 }
10352 }
10353 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10354 cu->producer_is_icc = 1;
10355 else
10356 {
10357 /* For other non-GCC compilers, expect their behavior is DWARF version
10358 compliant. */
10359 }
10360
10361 cu->checked_producer = 1;
10362 }
10363
10364 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10365 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10366 during 4.6.0 experimental. */
10367
10368 static int
10369 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10370 {
10371 if (!cu->checked_producer)
10372 check_producer (cu);
10373
10374 return cu->producer_is_gxx_lt_4_6;
10375 }
10376
10377 /* Return the default accessibility type if it is not overriden by
10378 DW_AT_accessibility. */
10379
10380 static enum dwarf_access_attribute
10381 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10382 {
10383 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10384 {
10385 /* The default DWARF 2 accessibility for members is public, the default
10386 accessibility for inheritance is private. */
10387
10388 if (die->tag != DW_TAG_inheritance)
10389 return DW_ACCESS_public;
10390 else
10391 return DW_ACCESS_private;
10392 }
10393 else
10394 {
10395 /* DWARF 3+ defines the default accessibility a different way. The same
10396 rules apply now for DW_TAG_inheritance as for the members and it only
10397 depends on the container kind. */
10398
10399 if (die->parent->tag == DW_TAG_class_type)
10400 return DW_ACCESS_private;
10401 else
10402 return DW_ACCESS_public;
10403 }
10404 }
10405
10406 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10407 offset. If the attribute was not found return 0, otherwise return
10408 1. If it was found but could not properly be handled, set *OFFSET
10409 to 0. */
10410
10411 static int
10412 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10413 LONGEST *offset)
10414 {
10415 struct attribute *attr;
10416
10417 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10418 if (attr != NULL)
10419 {
10420 *offset = 0;
10421
10422 /* Note that we do not check for a section offset first here.
10423 This is because DW_AT_data_member_location is new in DWARF 4,
10424 so if we see it, we can assume that a constant form is really
10425 a constant and not a section offset. */
10426 if (attr_form_is_constant (attr))
10427 *offset = dwarf2_get_attr_constant_value (attr, 0);
10428 else if (attr_form_is_section_offset (attr))
10429 dwarf2_complex_location_expr_complaint ();
10430 else if (attr_form_is_block (attr))
10431 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10432 else
10433 dwarf2_complex_location_expr_complaint ();
10434
10435 return 1;
10436 }
10437
10438 return 0;
10439 }
10440
10441 /* Add an aggregate field to the field list. */
10442
10443 static void
10444 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10445 struct dwarf2_cu *cu)
10446 {
10447 struct objfile *objfile = cu->objfile;
10448 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10449 struct nextfield *new_field;
10450 struct attribute *attr;
10451 struct field *fp;
10452 char *fieldname = "";
10453
10454 /* Allocate a new field list entry and link it in. */
10455 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10456 make_cleanup (xfree, new_field);
10457 memset (new_field, 0, sizeof (struct nextfield));
10458
10459 if (die->tag == DW_TAG_inheritance)
10460 {
10461 new_field->next = fip->baseclasses;
10462 fip->baseclasses = new_field;
10463 }
10464 else
10465 {
10466 new_field->next = fip->fields;
10467 fip->fields = new_field;
10468 }
10469 fip->nfields++;
10470
10471 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10472 if (attr)
10473 new_field->accessibility = DW_UNSND (attr);
10474 else
10475 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10476 if (new_field->accessibility != DW_ACCESS_public)
10477 fip->non_public_fields = 1;
10478
10479 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10480 if (attr)
10481 new_field->virtuality = DW_UNSND (attr);
10482 else
10483 new_field->virtuality = DW_VIRTUALITY_none;
10484
10485 fp = &new_field->field;
10486
10487 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10488 {
10489 LONGEST offset;
10490
10491 /* Data member other than a C++ static data member. */
10492
10493 /* Get type of field. */
10494 fp->type = die_type (die, cu);
10495
10496 SET_FIELD_BITPOS (*fp, 0);
10497
10498 /* Get bit size of field (zero if none). */
10499 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10500 if (attr)
10501 {
10502 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10503 }
10504 else
10505 {
10506 FIELD_BITSIZE (*fp) = 0;
10507 }
10508
10509 /* Get bit offset of field. */
10510 if (handle_data_member_location (die, cu, &offset))
10511 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10512 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10513 if (attr)
10514 {
10515 if (gdbarch_bits_big_endian (gdbarch))
10516 {
10517 /* For big endian bits, the DW_AT_bit_offset gives the
10518 additional bit offset from the MSB of the containing
10519 anonymous object to the MSB of the field. We don't
10520 have to do anything special since we don't need to
10521 know the size of the anonymous object. */
10522 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10523 }
10524 else
10525 {
10526 /* For little endian bits, compute the bit offset to the
10527 MSB of the anonymous object, subtract off the number of
10528 bits from the MSB of the field to the MSB of the
10529 object, and then subtract off the number of bits of
10530 the field itself. The result is the bit offset of
10531 the LSB of the field. */
10532 int anonymous_size;
10533 int bit_offset = DW_UNSND (attr);
10534
10535 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10536 if (attr)
10537 {
10538 /* The size of the anonymous object containing
10539 the bit field is explicit, so use the
10540 indicated size (in bytes). */
10541 anonymous_size = DW_UNSND (attr);
10542 }
10543 else
10544 {
10545 /* The size of the anonymous object containing
10546 the bit field must be inferred from the type
10547 attribute of the data member containing the
10548 bit field. */
10549 anonymous_size = TYPE_LENGTH (fp->type);
10550 }
10551 SET_FIELD_BITPOS (*fp,
10552 (FIELD_BITPOS (*fp)
10553 + anonymous_size * bits_per_byte
10554 - bit_offset - FIELD_BITSIZE (*fp)));
10555 }
10556 }
10557
10558 /* Get name of field. */
10559 fieldname = dwarf2_name (die, cu);
10560 if (fieldname == NULL)
10561 fieldname = "";
10562
10563 /* The name is already allocated along with this objfile, so we don't
10564 need to duplicate it for the type. */
10565 fp->name = fieldname;
10566
10567 /* Change accessibility for artificial fields (e.g. virtual table
10568 pointer or virtual base class pointer) to private. */
10569 if (dwarf2_attr (die, DW_AT_artificial, cu))
10570 {
10571 FIELD_ARTIFICIAL (*fp) = 1;
10572 new_field->accessibility = DW_ACCESS_private;
10573 fip->non_public_fields = 1;
10574 }
10575 }
10576 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10577 {
10578 /* C++ static member. */
10579
10580 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10581 is a declaration, but all versions of G++ as of this writing
10582 (so through at least 3.2.1) incorrectly generate
10583 DW_TAG_variable tags. */
10584
10585 const char *physname;
10586
10587 /* Get name of field. */
10588 fieldname = dwarf2_name (die, cu);
10589 if (fieldname == NULL)
10590 return;
10591
10592 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10593 if (attr
10594 /* Only create a symbol if this is an external value.
10595 new_symbol checks this and puts the value in the global symbol
10596 table, which we want. If it is not external, new_symbol
10597 will try to put the value in cu->list_in_scope which is wrong. */
10598 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10599 {
10600 /* A static const member, not much different than an enum as far as
10601 we're concerned, except that we can support more types. */
10602 new_symbol (die, NULL, cu);
10603 }
10604
10605 /* Get physical name. */
10606 physname = dwarf2_physname (fieldname, die, cu);
10607
10608 /* The name is already allocated along with this objfile, so we don't
10609 need to duplicate it for the type. */
10610 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10611 FIELD_TYPE (*fp) = die_type (die, cu);
10612 FIELD_NAME (*fp) = fieldname;
10613 }
10614 else if (die->tag == DW_TAG_inheritance)
10615 {
10616 LONGEST offset;
10617
10618 /* C++ base class field. */
10619 if (handle_data_member_location (die, cu, &offset))
10620 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10621 FIELD_BITSIZE (*fp) = 0;
10622 FIELD_TYPE (*fp) = die_type (die, cu);
10623 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10624 fip->nbaseclasses++;
10625 }
10626 }
10627
10628 /* Add a typedef defined in the scope of the FIP's class. */
10629
10630 static void
10631 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10632 struct dwarf2_cu *cu)
10633 {
10634 struct objfile *objfile = cu->objfile;
10635 struct typedef_field_list *new_field;
10636 struct attribute *attr;
10637 struct typedef_field *fp;
10638 char *fieldname = "";
10639
10640 /* Allocate a new field list entry and link it in. */
10641 new_field = xzalloc (sizeof (*new_field));
10642 make_cleanup (xfree, new_field);
10643
10644 gdb_assert (die->tag == DW_TAG_typedef);
10645
10646 fp = &new_field->field;
10647
10648 /* Get name of field. */
10649 fp->name = dwarf2_name (die, cu);
10650 if (fp->name == NULL)
10651 return;
10652
10653 fp->type = read_type_die (die, cu);
10654
10655 new_field->next = fip->typedef_field_list;
10656 fip->typedef_field_list = new_field;
10657 fip->typedef_field_list_count++;
10658 }
10659
10660 /* Create the vector of fields, and attach it to the type. */
10661
10662 static void
10663 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10664 struct dwarf2_cu *cu)
10665 {
10666 int nfields = fip->nfields;
10667
10668 /* Record the field count, allocate space for the array of fields,
10669 and create blank accessibility bitfields if necessary. */
10670 TYPE_NFIELDS (type) = nfields;
10671 TYPE_FIELDS (type) = (struct field *)
10672 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10673 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10674
10675 if (fip->non_public_fields && cu->language != language_ada)
10676 {
10677 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10678
10679 TYPE_FIELD_PRIVATE_BITS (type) =
10680 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10681 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10682
10683 TYPE_FIELD_PROTECTED_BITS (type) =
10684 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10685 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10686
10687 TYPE_FIELD_IGNORE_BITS (type) =
10688 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10689 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10690 }
10691
10692 /* If the type has baseclasses, allocate and clear a bit vector for
10693 TYPE_FIELD_VIRTUAL_BITS. */
10694 if (fip->nbaseclasses && cu->language != language_ada)
10695 {
10696 int num_bytes = B_BYTES (fip->nbaseclasses);
10697 unsigned char *pointer;
10698
10699 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10700 pointer = TYPE_ALLOC (type, num_bytes);
10701 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10702 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10703 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10704 }
10705
10706 /* Copy the saved-up fields into the field vector. Start from the head of
10707 the list, adding to the tail of the field array, so that they end up in
10708 the same order in the array in which they were added to the list. */
10709 while (nfields-- > 0)
10710 {
10711 struct nextfield *fieldp;
10712
10713 if (fip->fields)
10714 {
10715 fieldp = fip->fields;
10716 fip->fields = fieldp->next;
10717 }
10718 else
10719 {
10720 fieldp = fip->baseclasses;
10721 fip->baseclasses = fieldp->next;
10722 }
10723
10724 TYPE_FIELD (type, nfields) = fieldp->field;
10725 switch (fieldp->accessibility)
10726 {
10727 case DW_ACCESS_private:
10728 if (cu->language != language_ada)
10729 SET_TYPE_FIELD_PRIVATE (type, nfields);
10730 break;
10731
10732 case DW_ACCESS_protected:
10733 if (cu->language != language_ada)
10734 SET_TYPE_FIELD_PROTECTED (type, nfields);
10735 break;
10736
10737 case DW_ACCESS_public:
10738 break;
10739
10740 default:
10741 /* Unknown accessibility. Complain and treat it as public. */
10742 {
10743 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10744 fieldp->accessibility);
10745 }
10746 break;
10747 }
10748 if (nfields < fip->nbaseclasses)
10749 {
10750 switch (fieldp->virtuality)
10751 {
10752 case DW_VIRTUALITY_virtual:
10753 case DW_VIRTUALITY_pure_virtual:
10754 if (cu->language == language_ada)
10755 error (_("unexpected virtuality in component of Ada type"));
10756 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10757 break;
10758 }
10759 }
10760 }
10761 }
10762
10763 /* Add a member function to the proper fieldlist. */
10764
10765 static void
10766 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10767 struct type *type, struct dwarf2_cu *cu)
10768 {
10769 struct objfile *objfile = cu->objfile;
10770 struct attribute *attr;
10771 struct fnfieldlist *flp;
10772 int i;
10773 struct fn_field *fnp;
10774 char *fieldname;
10775 struct nextfnfield *new_fnfield;
10776 struct type *this_type;
10777 enum dwarf_access_attribute accessibility;
10778
10779 if (cu->language == language_ada)
10780 error (_("unexpected member function in Ada type"));
10781
10782 /* Get name of member function. */
10783 fieldname = dwarf2_name (die, cu);
10784 if (fieldname == NULL)
10785 return;
10786
10787 /* Look up member function name in fieldlist. */
10788 for (i = 0; i < fip->nfnfields; i++)
10789 {
10790 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10791 break;
10792 }
10793
10794 /* Create new list element if necessary. */
10795 if (i < fip->nfnfields)
10796 flp = &fip->fnfieldlists[i];
10797 else
10798 {
10799 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10800 {
10801 fip->fnfieldlists = (struct fnfieldlist *)
10802 xrealloc (fip->fnfieldlists,
10803 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10804 * sizeof (struct fnfieldlist));
10805 if (fip->nfnfields == 0)
10806 make_cleanup (free_current_contents, &fip->fnfieldlists);
10807 }
10808 flp = &fip->fnfieldlists[fip->nfnfields];
10809 flp->name = fieldname;
10810 flp->length = 0;
10811 flp->head = NULL;
10812 i = fip->nfnfields++;
10813 }
10814
10815 /* Create a new member function field and chain it to the field list
10816 entry. */
10817 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10818 make_cleanup (xfree, new_fnfield);
10819 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10820 new_fnfield->next = flp->head;
10821 flp->head = new_fnfield;
10822 flp->length++;
10823
10824 /* Fill in the member function field info. */
10825 fnp = &new_fnfield->fnfield;
10826
10827 /* Delay processing of the physname until later. */
10828 if (cu->language == language_cplus || cu->language == language_java)
10829 {
10830 add_to_method_list (type, i, flp->length - 1, fieldname,
10831 die, cu);
10832 }
10833 else
10834 {
10835 const char *physname = dwarf2_physname (fieldname, die, cu);
10836 fnp->physname = physname ? physname : "";
10837 }
10838
10839 fnp->type = alloc_type (objfile);
10840 this_type = read_type_die (die, cu);
10841 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10842 {
10843 int nparams = TYPE_NFIELDS (this_type);
10844
10845 /* TYPE is the domain of this method, and THIS_TYPE is the type
10846 of the method itself (TYPE_CODE_METHOD). */
10847 smash_to_method_type (fnp->type, type,
10848 TYPE_TARGET_TYPE (this_type),
10849 TYPE_FIELDS (this_type),
10850 TYPE_NFIELDS (this_type),
10851 TYPE_VARARGS (this_type));
10852
10853 /* Handle static member functions.
10854 Dwarf2 has no clean way to discern C++ static and non-static
10855 member functions. G++ helps GDB by marking the first
10856 parameter for non-static member functions (which is the this
10857 pointer) as artificial. We obtain this information from
10858 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10859 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10860 fnp->voffset = VOFFSET_STATIC;
10861 }
10862 else
10863 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10864 dwarf2_full_name (fieldname, die, cu));
10865
10866 /* Get fcontext from DW_AT_containing_type if present. */
10867 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10868 fnp->fcontext = die_containing_type (die, cu);
10869
10870 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10871 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10872
10873 /* Get accessibility. */
10874 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10875 if (attr)
10876 accessibility = DW_UNSND (attr);
10877 else
10878 accessibility = dwarf2_default_access_attribute (die, cu);
10879 switch (accessibility)
10880 {
10881 case DW_ACCESS_private:
10882 fnp->is_private = 1;
10883 break;
10884 case DW_ACCESS_protected:
10885 fnp->is_protected = 1;
10886 break;
10887 }
10888
10889 /* Check for artificial methods. */
10890 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10891 if (attr && DW_UNSND (attr) != 0)
10892 fnp->is_artificial = 1;
10893
10894 /* Get index in virtual function table if it is a virtual member
10895 function. For older versions of GCC, this is an offset in the
10896 appropriate virtual table, as specified by DW_AT_containing_type.
10897 For everyone else, it is an expression to be evaluated relative
10898 to the object address. */
10899
10900 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10901 if (attr)
10902 {
10903 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10904 {
10905 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10906 {
10907 /* Old-style GCC. */
10908 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10909 }
10910 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10911 || (DW_BLOCK (attr)->size > 1
10912 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10913 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10914 {
10915 struct dwarf_block blk;
10916 int offset;
10917
10918 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10919 ? 1 : 2);
10920 blk.size = DW_BLOCK (attr)->size - offset;
10921 blk.data = DW_BLOCK (attr)->data + offset;
10922 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10923 if ((fnp->voffset % cu->header.addr_size) != 0)
10924 dwarf2_complex_location_expr_complaint ();
10925 else
10926 fnp->voffset /= cu->header.addr_size;
10927 fnp->voffset += 2;
10928 }
10929 else
10930 dwarf2_complex_location_expr_complaint ();
10931
10932 if (!fnp->fcontext)
10933 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10934 }
10935 else if (attr_form_is_section_offset (attr))
10936 {
10937 dwarf2_complex_location_expr_complaint ();
10938 }
10939 else
10940 {
10941 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10942 fieldname);
10943 }
10944 }
10945 else
10946 {
10947 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10948 if (attr && DW_UNSND (attr))
10949 {
10950 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10951 complaint (&symfile_complaints,
10952 _("Member function \"%s\" (offset %d) is virtual "
10953 "but the vtable offset is not specified"),
10954 fieldname, die->offset.sect_off);
10955 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10956 TYPE_CPLUS_DYNAMIC (type) = 1;
10957 }
10958 }
10959 }
10960
10961 /* Create the vector of member function fields, and attach it to the type. */
10962
10963 static void
10964 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10965 struct dwarf2_cu *cu)
10966 {
10967 struct fnfieldlist *flp;
10968 int i;
10969
10970 if (cu->language == language_ada)
10971 error (_("unexpected member functions in Ada type"));
10972
10973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10974 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10975 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10976
10977 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10978 {
10979 struct nextfnfield *nfp = flp->head;
10980 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10981 int k;
10982
10983 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10984 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10985 fn_flp->fn_fields = (struct fn_field *)
10986 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10987 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10988 fn_flp->fn_fields[k] = nfp->fnfield;
10989 }
10990
10991 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10992 }
10993
10994 /* Returns non-zero if NAME is the name of a vtable member in CU's
10995 language, zero otherwise. */
10996 static int
10997 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10998 {
10999 static const char vptr[] = "_vptr";
11000 static const char vtable[] = "vtable";
11001
11002 /* Look for the C++ and Java forms of the vtable. */
11003 if ((cu->language == language_java
11004 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11005 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11006 && is_cplus_marker (name[sizeof (vptr) - 1])))
11007 return 1;
11008
11009 return 0;
11010 }
11011
11012 /* GCC outputs unnamed structures that are really pointers to member
11013 functions, with the ABI-specified layout. If TYPE describes
11014 such a structure, smash it into a member function type.
11015
11016 GCC shouldn't do this; it should just output pointer to member DIEs.
11017 This is GCC PR debug/28767. */
11018
11019 static void
11020 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11021 {
11022 struct type *pfn_type, *domain_type, *new_type;
11023
11024 /* Check for a structure with no name and two children. */
11025 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11026 return;
11027
11028 /* Check for __pfn and __delta members. */
11029 if (TYPE_FIELD_NAME (type, 0) == NULL
11030 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11031 || TYPE_FIELD_NAME (type, 1) == NULL
11032 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11033 return;
11034
11035 /* Find the type of the method. */
11036 pfn_type = TYPE_FIELD_TYPE (type, 0);
11037 if (pfn_type == NULL
11038 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11039 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11040 return;
11041
11042 /* Look for the "this" argument. */
11043 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11044 if (TYPE_NFIELDS (pfn_type) == 0
11045 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11046 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11047 return;
11048
11049 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11050 new_type = alloc_type (objfile);
11051 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11052 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11053 TYPE_VARARGS (pfn_type));
11054 smash_to_methodptr_type (type, new_type);
11055 }
11056
11057 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11058 (icc). */
11059
11060 static int
11061 producer_is_icc (struct dwarf2_cu *cu)
11062 {
11063 if (!cu->checked_producer)
11064 check_producer (cu);
11065
11066 return cu->producer_is_icc;
11067 }
11068
11069 /* Called when we find the DIE that starts a structure or union scope
11070 (definition) to create a type for the structure or union. Fill in
11071 the type's name and general properties; the members will not be
11072 processed until process_structure_type.
11073
11074 NOTE: we need to call these functions regardless of whether or not the
11075 DIE has a DW_AT_name attribute, since it might be an anonymous
11076 structure or union. This gets the type entered into our set of
11077 user defined types.
11078
11079 However, if the structure is incomplete (an opaque struct/union)
11080 then suppress creating a symbol table entry for it since gdb only
11081 wants to find the one with the complete definition. Note that if
11082 it is complete, we just call new_symbol, which does it's own
11083 checking about whether the struct/union is anonymous or not (and
11084 suppresses creating a symbol table entry itself). */
11085
11086 static struct type *
11087 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11088 {
11089 struct objfile *objfile = cu->objfile;
11090 struct type *type;
11091 struct attribute *attr;
11092 char *name;
11093
11094 /* If the definition of this type lives in .debug_types, read that type.
11095 Don't follow DW_AT_specification though, that will take us back up
11096 the chain and we want to go down. */
11097 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11098 if (attr)
11099 {
11100 struct dwarf2_cu *type_cu = cu;
11101 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11102
11103 /* We could just recurse on read_structure_type, but we need to call
11104 get_die_type to ensure only one type for this DIE is created.
11105 This is important, for example, because for c++ classes we need
11106 TYPE_NAME set which is only done by new_symbol. Blech. */
11107 type = read_type_die (type_die, type_cu);
11108
11109 /* TYPE_CU may not be the same as CU.
11110 Ensure TYPE is recorded in CU's type_hash table. */
11111 return set_die_type (die, type, cu);
11112 }
11113
11114 type = alloc_type (objfile);
11115 INIT_CPLUS_SPECIFIC (type);
11116
11117 name = dwarf2_name (die, cu);
11118 if (name != NULL)
11119 {
11120 if (cu->language == language_cplus
11121 || cu->language == language_java)
11122 {
11123 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11124
11125 /* dwarf2_full_name might have already finished building the DIE's
11126 type. If so, there is no need to continue. */
11127 if (get_die_type (die, cu) != NULL)
11128 return get_die_type (die, cu);
11129
11130 TYPE_TAG_NAME (type) = full_name;
11131 if (die->tag == DW_TAG_structure_type
11132 || die->tag == DW_TAG_class_type)
11133 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11134 }
11135 else
11136 {
11137 /* The name is already allocated along with this objfile, so
11138 we don't need to duplicate it for the type. */
11139 TYPE_TAG_NAME (type) = (char *) name;
11140 if (die->tag == DW_TAG_class_type)
11141 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11142 }
11143 }
11144
11145 if (die->tag == DW_TAG_structure_type)
11146 {
11147 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11148 }
11149 else if (die->tag == DW_TAG_union_type)
11150 {
11151 TYPE_CODE (type) = TYPE_CODE_UNION;
11152 }
11153 else
11154 {
11155 TYPE_CODE (type) = TYPE_CODE_CLASS;
11156 }
11157
11158 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11159 TYPE_DECLARED_CLASS (type) = 1;
11160
11161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11162 if (attr)
11163 {
11164 TYPE_LENGTH (type) = DW_UNSND (attr);
11165 }
11166 else
11167 {
11168 TYPE_LENGTH (type) = 0;
11169 }
11170
11171 if (producer_is_icc (cu))
11172 {
11173 /* ICC does not output the required DW_AT_declaration
11174 on incomplete types, but gives them a size of zero. */
11175 }
11176 else
11177 TYPE_STUB_SUPPORTED (type) = 1;
11178
11179 if (die_is_declaration (die, cu))
11180 TYPE_STUB (type) = 1;
11181 else if (attr == NULL && die->child == NULL
11182 && producer_is_realview (cu->producer))
11183 /* RealView does not output the required DW_AT_declaration
11184 on incomplete types. */
11185 TYPE_STUB (type) = 1;
11186
11187 /* We need to add the type field to the die immediately so we don't
11188 infinitely recurse when dealing with pointers to the structure
11189 type within the structure itself. */
11190 set_die_type (die, type, cu);
11191
11192 /* set_die_type should be already done. */
11193 set_descriptive_type (type, die, cu);
11194
11195 return type;
11196 }
11197
11198 /* Finish creating a structure or union type, including filling in
11199 its members and creating a symbol for it. */
11200
11201 static void
11202 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11203 {
11204 struct objfile *objfile = cu->objfile;
11205 struct die_info *child_die = die->child;
11206 struct type *type;
11207
11208 type = get_die_type (die, cu);
11209 if (type == NULL)
11210 type = read_structure_type (die, cu);
11211
11212 if (die->child != NULL && ! die_is_declaration (die, cu))
11213 {
11214 struct field_info fi;
11215 struct die_info *child_die;
11216 VEC (symbolp) *template_args = NULL;
11217 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11218
11219 memset (&fi, 0, sizeof (struct field_info));
11220
11221 child_die = die->child;
11222
11223 while (child_die && child_die->tag)
11224 {
11225 if (child_die->tag == DW_TAG_member
11226 || child_die->tag == DW_TAG_variable)
11227 {
11228 /* NOTE: carlton/2002-11-05: A C++ static data member
11229 should be a DW_TAG_member that is a declaration, but
11230 all versions of G++ as of this writing (so through at
11231 least 3.2.1) incorrectly generate DW_TAG_variable
11232 tags for them instead. */
11233 dwarf2_add_field (&fi, child_die, cu);
11234 }
11235 else if (child_die->tag == DW_TAG_subprogram)
11236 {
11237 /* C++ member function. */
11238 dwarf2_add_member_fn (&fi, child_die, type, cu);
11239 }
11240 else if (child_die->tag == DW_TAG_inheritance)
11241 {
11242 /* C++ base class field. */
11243 dwarf2_add_field (&fi, child_die, cu);
11244 }
11245 else if (child_die->tag == DW_TAG_typedef)
11246 dwarf2_add_typedef (&fi, child_die, cu);
11247 else if (child_die->tag == DW_TAG_template_type_param
11248 || child_die->tag == DW_TAG_template_value_param)
11249 {
11250 struct symbol *arg = new_symbol (child_die, NULL, cu);
11251
11252 if (arg != NULL)
11253 VEC_safe_push (symbolp, template_args, arg);
11254 }
11255
11256 child_die = sibling_die (child_die);
11257 }
11258
11259 /* Attach template arguments to type. */
11260 if (! VEC_empty (symbolp, template_args))
11261 {
11262 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11263 TYPE_N_TEMPLATE_ARGUMENTS (type)
11264 = VEC_length (symbolp, template_args);
11265 TYPE_TEMPLATE_ARGUMENTS (type)
11266 = obstack_alloc (&objfile->objfile_obstack,
11267 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11268 * sizeof (struct symbol *)));
11269 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11270 VEC_address (symbolp, template_args),
11271 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11272 * sizeof (struct symbol *)));
11273 VEC_free (symbolp, template_args);
11274 }
11275
11276 /* Attach fields and member functions to the type. */
11277 if (fi.nfields)
11278 dwarf2_attach_fields_to_type (&fi, type, cu);
11279 if (fi.nfnfields)
11280 {
11281 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11282
11283 /* Get the type which refers to the base class (possibly this
11284 class itself) which contains the vtable pointer for the current
11285 class from the DW_AT_containing_type attribute. This use of
11286 DW_AT_containing_type is a GNU extension. */
11287
11288 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11289 {
11290 struct type *t = die_containing_type (die, cu);
11291
11292 TYPE_VPTR_BASETYPE (type) = t;
11293 if (type == t)
11294 {
11295 int i;
11296
11297 /* Our own class provides vtbl ptr. */
11298 for (i = TYPE_NFIELDS (t) - 1;
11299 i >= TYPE_N_BASECLASSES (t);
11300 --i)
11301 {
11302 const char *fieldname = TYPE_FIELD_NAME (t, i);
11303
11304 if (is_vtable_name (fieldname, cu))
11305 {
11306 TYPE_VPTR_FIELDNO (type) = i;
11307 break;
11308 }
11309 }
11310
11311 /* Complain if virtual function table field not found. */
11312 if (i < TYPE_N_BASECLASSES (t))
11313 complaint (&symfile_complaints,
11314 _("virtual function table pointer "
11315 "not found when defining class '%s'"),
11316 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11317 "");
11318 }
11319 else
11320 {
11321 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11322 }
11323 }
11324 else if (cu->producer
11325 && strncmp (cu->producer,
11326 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11327 {
11328 /* The IBM XLC compiler does not provide direct indication
11329 of the containing type, but the vtable pointer is
11330 always named __vfp. */
11331
11332 int i;
11333
11334 for (i = TYPE_NFIELDS (type) - 1;
11335 i >= TYPE_N_BASECLASSES (type);
11336 --i)
11337 {
11338 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11339 {
11340 TYPE_VPTR_FIELDNO (type) = i;
11341 TYPE_VPTR_BASETYPE (type) = type;
11342 break;
11343 }
11344 }
11345 }
11346 }
11347
11348 /* Copy fi.typedef_field_list linked list elements content into the
11349 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11350 if (fi.typedef_field_list)
11351 {
11352 int i = fi.typedef_field_list_count;
11353
11354 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11355 TYPE_TYPEDEF_FIELD_ARRAY (type)
11356 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11357 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11358
11359 /* Reverse the list order to keep the debug info elements order. */
11360 while (--i >= 0)
11361 {
11362 struct typedef_field *dest, *src;
11363
11364 dest = &TYPE_TYPEDEF_FIELD (type, i);
11365 src = &fi.typedef_field_list->field;
11366 fi.typedef_field_list = fi.typedef_field_list->next;
11367 *dest = *src;
11368 }
11369 }
11370
11371 do_cleanups (back_to);
11372
11373 if (HAVE_CPLUS_STRUCT (type))
11374 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11375 }
11376
11377 quirk_gcc_member_function_pointer (type, objfile);
11378
11379 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11380 snapshots) has been known to create a die giving a declaration
11381 for a class that has, as a child, a die giving a definition for a
11382 nested class. So we have to process our children even if the
11383 current die is a declaration. Normally, of course, a declaration
11384 won't have any children at all. */
11385
11386 while (child_die != NULL && child_die->tag)
11387 {
11388 if (child_die->tag == DW_TAG_member
11389 || child_die->tag == DW_TAG_variable
11390 || child_die->tag == DW_TAG_inheritance
11391 || child_die->tag == DW_TAG_template_value_param
11392 || child_die->tag == DW_TAG_template_type_param)
11393 {
11394 /* Do nothing. */
11395 }
11396 else
11397 process_die (child_die, cu);
11398
11399 child_die = sibling_die (child_die);
11400 }
11401
11402 /* Do not consider external references. According to the DWARF standard,
11403 these DIEs are identified by the fact that they have no byte_size
11404 attribute, and a declaration attribute. */
11405 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11406 || !die_is_declaration (die, cu))
11407 new_symbol (die, type, cu);
11408 }
11409
11410 /* Given a DW_AT_enumeration_type die, set its type. We do not
11411 complete the type's fields yet, or create any symbols. */
11412
11413 static struct type *
11414 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11415 {
11416 struct objfile *objfile = cu->objfile;
11417 struct type *type;
11418 struct attribute *attr;
11419 const char *name;
11420
11421 /* If the definition of this type lives in .debug_types, read that type.
11422 Don't follow DW_AT_specification though, that will take us back up
11423 the chain and we want to go down. */
11424 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11425 if (attr)
11426 {
11427 struct dwarf2_cu *type_cu = cu;
11428 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11429
11430 type = read_type_die (type_die, type_cu);
11431
11432 /* TYPE_CU may not be the same as CU.
11433 Ensure TYPE is recorded in CU's type_hash table. */
11434 return set_die_type (die, type, cu);
11435 }
11436
11437 type = alloc_type (objfile);
11438
11439 TYPE_CODE (type) = TYPE_CODE_ENUM;
11440 name = dwarf2_full_name (NULL, die, cu);
11441 if (name != NULL)
11442 TYPE_TAG_NAME (type) = (char *) name;
11443
11444 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11445 if (attr)
11446 {
11447 TYPE_LENGTH (type) = DW_UNSND (attr);
11448 }
11449 else
11450 {
11451 TYPE_LENGTH (type) = 0;
11452 }
11453
11454 /* The enumeration DIE can be incomplete. In Ada, any type can be
11455 declared as private in the package spec, and then defined only
11456 inside the package body. Such types are known as Taft Amendment
11457 Types. When another package uses such a type, an incomplete DIE
11458 may be generated by the compiler. */
11459 if (die_is_declaration (die, cu))
11460 TYPE_STUB (type) = 1;
11461
11462 return set_die_type (die, type, cu);
11463 }
11464
11465 /* Given a pointer to a die which begins an enumeration, process all
11466 the dies that define the members of the enumeration, and create the
11467 symbol for the enumeration type.
11468
11469 NOTE: We reverse the order of the element list. */
11470
11471 static void
11472 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11473 {
11474 struct type *this_type;
11475
11476 this_type = get_die_type (die, cu);
11477 if (this_type == NULL)
11478 this_type = read_enumeration_type (die, cu);
11479
11480 if (die->child != NULL)
11481 {
11482 struct die_info *child_die;
11483 struct symbol *sym;
11484 struct field *fields = NULL;
11485 int num_fields = 0;
11486 int unsigned_enum = 1;
11487 char *name;
11488 int flag_enum = 1;
11489 ULONGEST mask = 0;
11490
11491 child_die = die->child;
11492 while (child_die && child_die->tag)
11493 {
11494 if (child_die->tag != DW_TAG_enumerator)
11495 {
11496 process_die (child_die, cu);
11497 }
11498 else
11499 {
11500 name = dwarf2_name (child_die, cu);
11501 if (name)
11502 {
11503 sym = new_symbol (child_die, this_type, cu);
11504 if (SYMBOL_VALUE (sym) < 0)
11505 {
11506 unsigned_enum = 0;
11507 flag_enum = 0;
11508 }
11509 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11510 flag_enum = 0;
11511 else
11512 mask |= SYMBOL_VALUE (sym);
11513
11514 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11515 {
11516 fields = (struct field *)
11517 xrealloc (fields,
11518 (num_fields + DW_FIELD_ALLOC_CHUNK)
11519 * sizeof (struct field));
11520 }
11521
11522 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11523 FIELD_TYPE (fields[num_fields]) = NULL;
11524 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11525 FIELD_BITSIZE (fields[num_fields]) = 0;
11526
11527 num_fields++;
11528 }
11529 }
11530
11531 child_die = sibling_die (child_die);
11532 }
11533
11534 if (num_fields)
11535 {
11536 TYPE_NFIELDS (this_type) = num_fields;
11537 TYPE_FIELDS (this_type) = (struct field *)
11538 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11539 memcpy (TYPE_FIELDS (this_type), fields,
11540 sizeof (struct field) * num_fields);
11541 xfree (fields);
11542 }
11543 if (unsigned_enum)
11544 TYPE_UNSIGNED (this_type) = 1;
11545 if (flag_enum)
11546 TYPE_FLAG_ENUM (this_type) = 1;
11547 }
11548
11549 /* If we are reading an enum from a .debug_types unit, and the enum
11550 is a declaration, and the enum is not the signatured type in the
11551 unit, then we do not want to add a symbol for it. Adding a
11552 symbol would in some cases obscure the true definition of the
11553 enum, giving users an incomplete type when the definition is
11554 actually available. Note that we do not want to do this for all
11555 enums which are just declarations, because C++0x allows forward
11556 enum declarations. */
11557 if (cu->per_cu->is_debug_types
11558 && die_is_declaration (die, cu))
11559 {
11560 struct signatured_type *sig_type;
11561
11562 sig_type
11563 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11564 cu->per_cu->info_or_types_section,
11565 cu->per_cu->offset);
11566 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11567 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11568 return;
11569 }
11570
11571 new_symbol (die, this_type, cu);
11572 }
11573
11574 /* Extract all information from a DW_TAG_array_type DIE and put it in
11575 the DIE's type field. For now, this only handles one dimensional
11576 arrays. */
11577
11578 static struct type *
11579 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11580 {
11581 struct objfile *objfile = cu->objfile;
11582 struct die_info *child_die;
11583 struct type *type;
11584 struct type *element_type, *range_type, *index_type;
11585 struct type **range_types = NULL;
11586 struct attribute *attr;
11587 int ndim = 0;
11588 struct cleanup *back_to;
11589 char *name;
11590
11591 element_type = die_type (die, cu);
11592
11593 /* The die_type call above may have already set the type for this DIE. */
11594 type = get_die_type (die, cu);
11595 if (type)
11596 return type;
11597
11598 /* Irix 6.2 native cc creates array types without children for
11599 arrays with unspecified length. */
11600 if (die->child == NULL)
11601 {
11602 index_type = objfile_type (objfile)->builtin_int;
11603 range_type = create_range_type (NULL, index_type, 0, -1);
11604 type = create_array_type (NULL, element_type, range_type);
11605 return set_die_type (die, type, cu);
11606 }
11607
11608 back_to = make_cleanup (null_cleanup, NULL);
11609 child_die = die->child;
11610 while (child_die && child_die->tag)
11611 {
11612 if (child_die->tag == DW_TAG_subrange_type)
11613 {
11614 struct type *child_type = read_type_die (child_die, cu);
11615
11616 if (child_type != NULL)
11617 {
11618 /* The range type was succesfully read. Save it for the
11619 array type creation. */
11620 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11621 {
11622 range_types = (struct type **)
11623 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11624 * sizeof (struct type *));
11625 if (ndim == 0)
11626 make_cleanup (free_current_contents, &range_types);
11627 }
11628 range_types[ndim++] = child_type;
11629 }
11630 }
11631 child_die = sibling_die (child_die);
11632 }
11633
11634 /* Dwarf2 dimensions are output from left to right, create the
11635 necessary array types in backwards order. */
11636
11637 type = element_type;
11638
11639 if (read_array_order (die, cu) == DW_ORD_col_major)
11640 {
11641 int i = 0;
11642
11643 while (i < ndim)
11644 type = create_array_type (NULL, type, range_types[i++]);
11645 }
11646 else
11647 {
11648 while (ndim-- > 0)
11649 type = create_array_type (NULL, type, range_types[ndim]);
11650 }
11651
11652 /* Understand Dwarf2 support for vector types (like they occur on
11653 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11654 array type. This is not part of the Dwarf2/3 standard yet, but a
11655 custom vendor extension. The main difference between a regular
11656 array and the vector variant is that vectors are passed by value
11657 to functions. */
11658 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11659 if (attr)
11660 make_vector_type (type);
11661
11662 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11663 implementation may choose to implement triple vectors using this
11664 attribute. */
11665 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11666 if (attr)
11667 {
11668 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11669 TYPE_LENGTH (type) = DW_UNSND (attr);
11670 else
11671 complaint (&symfile_complaints,
11672 _("DW_AT_byte_size for array type smaller "
11673 "than the total size of elements"));
11674 }
11675
11676 name = dwarf2_name (die, cu);
11677 if (name)
11678 TYPE_NAME (type) = name;
11679
11680 /* Install the type in the die. */
11681 set_die_type (die, type, cu);
11682
11683 /* set_die_type should be already done. */
11684 set_descriptive_type (type, die, cu);
11685
11686 do_cleanups (back_to);
11687
11688 return type;
11689 }
11690
11691 static enum dwarf_array_dim_ordering
11692 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11693 {
11694 struct attribute *attr;
11695
11696 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11697
11698 if (attr) return DW_SND (attr);
11699
11700 /* GNU F77 is a special case, as at 08/2004 array type info is the
11701 opposite order to the dwarf2 specification, but data is still
11702 laid out as per normal fortran.
11703
11704 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11705 version checking. */
11706
11707 if (cu->language == language_fortran
11708 && cu->producer && strstr (cu->producer, "GNU F77"))
11709 {
11710 return DW_ORD_row_major;
11711 }
11712
11713 switch (cu->language_defn->la_array_ordering)
11714 {
11715 case array_column_major:
11716 return DW_ORD_col_major;
11717 case array_row_major:
11718 default:
11719 return DW_ORD_row_major;
11720 };
11721 }
11722
11723 /* Extract all information from a DW_TAG_set_type DIE and put it in
11724 the DIE's type field. */
11725
11726 static struct type *
11727 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11728 {
11729 struct type *domain_type, *set_type;
11730 struct attribute *attr;
11731
11732 domain_type = die_type (die, cu);
11733
11734 /* The die_type call above may have already set the type for this DIE. */
11735 set_type = get_die_type (die, cu);
11736 if (set_type)
11737 return set_type;
11738
11739 set_type = create_set_type (NULL, domain_type);
11740
11741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11742 if (attr)
11743 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11744
11745 return set_die_type (die, set_type, cu);
11746 }
11747
11748 /* A helper for read_common_block that creates a locexpr baton.
11749 SYM is the symbol which we are marking as computed.
11750 COMMON_DIE is the DIE for the common block.
11751 COMMON_LOC is the location expression attribute for the common
11752 block itself.
11753 MEMBER_LOC is the location expression attribute for the particular
11754 member of the common block that we are processing.
11755 CU is the CU from which the above come. */
11756
11757 static void
11758 mark_common_block_symbol_computed (struct symbol *sym,
11759 struct die_info *common_die,
11760 struct attribute *common_loc,
11761 struct attribute *member_loc,
11762 struct dwarf2_cu *cu)
11763 {
11764 struct objfile *objfile = dwarf2_per_objfile->objfile;
11765 struct dwarf2_locexpr_baton *baton;
11766 gdb_byte *ptr;
11767 unsigned int cu_off;
11768 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11769 LONGEST offset = 0;
11770
11771 gdb_assert (common_loc && member_loc);
11772 gdb_assert (attr_form_is_block (common_loc));
11773 gdb_assert (attr_form_is_block (member_loc)
11774 || attr_form_is_constant (member_loc));
11775
11776 baton = obstack_alloc (&objfile->objfile_obstack,
11777 sizeof (struct dwarf2_locexpr_baton));
11778 baton->per_cu = cu->per_cu;
11779 gdb_assert (baton->per_cu);
11780
11781 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11782
11783 if (attr_form_is_constant (member_loc))
11784 {
11785 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11786 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11787 }
11788 else
11789 baton->size += DW_BLOCK (member_loc)->size;
11790
11791 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11792 baton->data = ptr;
11793
11794 *ptr++ = DW_OP_call4;
11795 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11796 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11797 ptr += 4;
11798
11799 if (attr_form_is_constant (member_loc))
11800 {
11801 *ptr++ = DW_OP_addr;
11802 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11803 ptr += cu->header.addr_size;
11804 }
11805 else
11806 {
11807 /* We have to copy the data here, because DW_OP_call4 will only
11808 use a DW_AT_location attribute. */
11809 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11810 ptr += DW_BLOCK (member_loc)->size;
11811 }
11812
11813 *ptr++ = DW_OP_plus;
11814 gdb_assert (ptr - baton->data == baton->size);
11815
11816 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11817 SYMBOL_LOCATION_BATON (sym) = baton;
11818 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11819 }
11820
11821 /* Create appropriate locally-scoped variables for all the
11822 DW_TAG_common_block entries. Also create a struct common_block
11823 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11824 is used to sepate the common blocks name namespace from regular
11825 variable names. */
11826
11827 static void
11828 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11829 {
11830 struct attribute *attr;
11831
11832 attr = dwarf2_attr (die, DW_AT_location, cu);
11833 if (attr)
11834 {
11835 /* Support the .debug_loc offsets. */
11836 if (attr_form_is_block (attr))
11837 {
11838 /* Ok. */
11839 }
11840 else if (attr_form_is_section_offset (attr))
11841 {
11842 dwarf2_complex_location_expr_complaint ();
11843 attr = NULL;
11844 }
11845 else
11846 {
11847 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11848 "common block member");
11849 attr = NULL;
11850 }
11851 }
11852
11853 if (die->child != NULL)
11854 {
11855 struct objfile *objfile = cu->objfile;
11856 struct die_info *child_die;
11857 size_t n_entries = 0, size;
11858 struct common_block *common_block;
11859 struct symbol *sym;
11860
11861 for (child_die = die->child;
11862 child_die && child_die->tag;
11863 child_die = sibling_die (child_die))
11864 ++n_entries;
11865
11866 size = (sizeof (struct common_block)
11867 + (n_entries - 1) * sizeof (struct symbol *));
11868 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11869 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11870 common_block->n_entries = 0;
11871
11872 for (child_die = die->child;
11873 child_die && child_die->tag;
11874 child_die = sibling_die (child_die))
11875 {
11876 /* Create the symbol in the DW_TAG_common_block block in the current
11877 symbol scope. */
11878 sym = new_symbol (child_die, NULL, cu);
11879 if (sym != NULL)
11880 {
11881 struct attribute *member_loc;
11882
11883 common_block->contents[common_block->n_entries++] = sym;
11884
11885 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11886 cu);
11887 if (member_loc)
11888 {
11889 /* GDB has handled this for a long time, but it is
11890 not specified by DWARF. It seems to have been
11891 emitted by gfortran at least as recently as:
11892 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11893 complaint (&symfile_complaints,
11894 _("Variable in common block has "
11895 "DW_AT_data_member_location "
11896 "- DIE at 0x%x [in module %s]"),
11897 child_die->offset.sect_off, cu->objfile->name);
11898
11899 if (attr_form_is_section_offset (member_loc))
11900 dwarf2_complex_location_expr_complaint ();
11901 else if (attr_form_is_constant (member_loc)
11902 || attr_form_is_block (member_loc))
11903 {
11904 if (attr)
11905 mark_common_block_symbol_computed (sym, die, attr,
11906 member_loc, cu);
11907 }
11908 else
11909 dwarf2_complex_location_expr_complaint ();
11910 }
11911 }
11912 }
11913
11914 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11915 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
11916 }
11917 }
11918
11919 /* Create a type for a C++ namespace. */
11920
11921 static struct type *
11922 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11923 {
11924 struct objfile *objfile = cu->objfile;
11925 const char *previous_prefix, *name;
11926 int is_anonymous;
11927 struct type *type;
11928
11929 /* For extensions, reuse the type of the original namespace. */
11930 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11931 {
11932 struct die_info *ext_die;
11933 struct dwarf2_cu *ext_cu = cu;
11934
11935 ext_die = dwarf2_extension (die, &ext_cu);
11936 type = read_type_die (ext_die, ext_cu);
11937
11938 /* EXT_CU may not be the same as CU.
11939 Ensure TYPE is recorded in CU's type_hash table. */
11940 return set_die_type (die, type, cu);
11941 }
11942
11943 name = namespace_name (die, &is_anonymous, cu);
11944
11945 /* Now build the name of the current namespace. */
11946
11947 previous_prefix = determine_prefix (die, cu);
11948 if (previous_prefix[0] != '\0')
11949 name = typename_concat (&objfile->objfile_obstack,
11950 previous_prefix, name, 0, cu);
11951
11952 /* Create the type. */
11953 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11954 objfile);
11955 TYPE_NAME (type) = (char *) name;
11956 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11957
11958 return set_die_type (die, type, cu);
11959 }
11960
11961 /* Read a C++ namespace. */
11962
11963 static void
11964 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11965 {
11966 struct objfile *objfile = cu->objfile;
11967 int is_anonymous;
11968
11969 /* Add a symbol associated to this if we haven't seen the namespace
11970 before. Also, add a using directive if it's an anonymous
11971 namespace. */
11972
11973 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11974 {
11975 struct type *type;
11976
11977 type = read_type_die (die, cu);
11978 new_symbol (die, type, cu);
11979
11980 namespace_name (die, &is_anonymous, cu);
11981 if (is_anonymous)
11982 {
11983 const char *previous_prefix = determine_prefix (die, cu);
11984
11985 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11986 NULL, NULL, &objfile->objfile_obstack);
11987 }
11988 }
11989
11990 if (die->child != NULL)
11991 {
11992 struct die_info *child_die = die->child;
11993
11994 while (child_die && child_die->tag)
11995 {
11996 process_die (child_die, cu);
11997 child_die = sibling_die (child_die);
11998 }
11999 }
12000 }
12001
12002 /* Read a Fortran module as type. This DIE can be only a declaration used for
12003 imported module. Still we need that type as local Fortran "use ... only"
12004 declaration imports depend on the created type in determine_prefix. */
12005
12006 static struct type *
12007 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12008 {
12009 struct objfile *objfile = cu->objfile;
12010 char *module_name;
12011 struct type *type;
12012
12013 module_name = dwarf2_name (die, cu);
12014 if (!module_name)
12015 complaint (&symfile_complaints,
12016 _("DW_TAG_module has no name, offset 0x%x"),
12017 die->offset.sect_off);
12018 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12019
12020 /* determine_prefix uses TYPE_TAG_NAME. */
12021 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12022
12023 return set_die_type (die, type, cu);
12024 }
12025
12026 /* Read a Fortran module. */
12027
12028 static void
12029 read_module (struct die_info *die, struct dwarf2_cu *cu)
12030 {
12031 struct die_info *child_die = die->child;
12032
12033 while (child_die && child_die->tag)
12034 {
12035 process_die (child_die, cu);
12036 child_die = sibling_die (child_die);
12037 }
12038 }
12039
12040 /* Return the name of the namespace represented by DIE. Set
12041 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12042 namespace. */
12043
12044 static const char *
12045 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12046 {
12047 struct die_info *current_die;
12048 const char *name = NULL;
12049
12050 /* Loop through the extensions until we find a name. */
12051
12052 for (current_die = die;
12053 current_die != NULL;
12054 current_die = dwarf2_extension (die, &cu))
12055 {
12056 name = dwarf2_name (current_die, cu);
12057 if (name != NULL)
12058 break;
12059 }
12060
12061 /* Is it an anonymous namespace? */
12062
12063 *is_anonymous = (name == NULL);
12064 if (*is_anonymous)
12065 name = CP_ANONYMOUS_NAMESPACE_STR;
12066
12067 return name;
12068 }
12069
12070 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12071 the user defined type vector. */
12072
12073 static struct type *
12074 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12075 {
12076 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12077 struct comp_unit_head *cu_header = &cu->header;
12078 struct type *type;
12079 struct attribute *attr_byte_size;
12080 struct attribute *attr_address_class;
12081 int byte_size, addr_class;
12082 struct type *target_type;
12083
12084 target_type = die_type (die, cu);
12085
12086 /* The die_type call above may have already set the type for this DIE. */
12087 type = get_die_type (die, cu);
12088 if (type)
12089 return type;
12090
12091 type = lookup_pointer_type (target_type);
12092
12093 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12094 if (attr_byte_size)
12095 byte_size = DW_UNSND (attr_byte_size);
12096 else
12097 byte_size = cu_header->addr_size;
12098
12099 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12100 if (attr_address_class)
12101 addr_class = DW_UNSND (attr_address_class);
12102 else
12103 addr_class = DW_ADDR_none;
12104
12105 /* If the pointer size or address class is different than the
12106 default, create a type variant marked as such and set the
12107 length accordingly. */
12108 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12109 {
12110 if (gdbarch_address_class_type_flags_p (gdbarch))
12111 {
12112 int type_flags;
12113
12114 type_flags = gdbarch_address_class_type_flags
12115 (gdbarch, byte_size, addr_class);
12116 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12117 == 0);
12118 type = make_type_with_address_space (type, type_flags);
12119 }
12120 else if (TYPE_LENGTH (type) != byte_size)
12121 {
12122 complaint (&symfile_complaints,
12123 _("invalid pointer size %d"), byte_size);
12124 }
12125 else
12126 {
12127 /* Should we also complain about unhandled address classes? */
12128 }
12129 }
12130
12131 TYPE_LENGTH (type) = byte_size;
12132 return set_die_type (die, type, cu);
12133 }
12134
12135 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12136 the user defined type vector. */
12137
12138 static struct type *
12139 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12140 {
12141 struct type *type;
12142 struct type *to_type;
12143 struct type *domain;
12144
12145 to_type = die_type (die, cu);
12146 domain = die_containing_type (die, cu);
12147
12148 /* The calls above may have already set the type for this DIE. */
12149 type = get_die_type (die, cu);
12150 if (type)
12151 return type;
12152
12153 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12154 type = lookup_methodptr_type (to_type);
12155 else
12156 type = lookup_memberptr_type (to_type, domain);
12157
12158 return set_die_type (die, type, cu);
12159 }
12160
12161 /* Extract all information from a DW_TAG_reference_type DIE and add to
12162 the user defined type vector. */
12163
12164 static struct type *
12165 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12166 {
12167 struct comp_unit_head *cu_header = &cu->header;
12168 struct type *type, *target_type;
12169 struct attribute *attr;
12170
12171 target_type = die_type (die, cu);
12172
12173 /* The die_type call above may have already set the type for this DIE. */
12174 type = get_die_type (die, cu);
12175 if (type)
12176 return type;
12177
12178 type = lookup_reference_type (target_type);
12179 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12180 if (attr)
12181 {
12182 TYPE_LENGTH (type) = DW_UNSND (attr);
12183 }
12184 else
12185 {
12186 TYPE_LENGTH (type) = cu_header->addr_size;
12187 }
12188 return set_die_type (die, type, cu);
12189 }
12190
12191 static struct type *
12192 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12193 {
12194 struct type *base_type, *cv_type;
12195
12196 base_type = die_type (die, cu);
12197
12198 /* The die_type call above may have already set the type for this DIE. */
12199 cv_type = get_die_type (die, cu);
12200 if (cv_type)
12201 return cv_type;
12202
12203 /* In case the const qualifier is applied to an array type, the element type
12204 is so qualified, not the array type (section 6.7.3 of C99). */
12205 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12206 {
12207 struct type *el_type, *inner_array;
12208
12209 base_type = copy_type (base_type);
12210 inner_array = base_type;
12211
12212 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12213 {
12214 TYPE_TARGET_TYPE (inner_array) =
12215 copy_type (TYPE_TARGET_TYPE (inner_array));
12216 inner_array = TYPE_TARGET_TYPE (inner_array);
12217 }
12218
12219 el_type = TYPE_TARGET_TYPE (inner_array);
12220 TYPE_TARGET_TYPE (inner_array) =
12221 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12222
12223 return set_die_type (die, base_type, cu);
12224 }
12225
12226 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12227 return set_die_type (die, cv_type, cu);
12228 }
12229
12230 static struct type *
12231 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12232 {
12233 struct type *base_type, *cv_type;
12234
12235 base_type = die_type (die, cu);
12236
12237 /* The die_type call above may have already set the type for this DIE. */
12238 cv_type = get_die_type (die, cu);
12239 if (cv_type)
12240 return cv_type;
12241
12242 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12243 return set_die_type (die, cv_type, cu);
12244 }
12245
12246 /* Extract all information from a DW_TAG_string_type DIE and add to
12247 the user defined type vector. It isn't really a user defined type,
12248 but it behaves like one, with other DIE's using an AT_user_def_type
12249 attribute to reference it. */
12250
12251 static struct type *
12252 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12253 {
12254 struct objfile *objfile = cu->objfile;
12255 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12256 struct type *type, *range_type, *index_type, *char_type;
12257 struct attribute *attr;
12258 unsigned int length;
12259
12260 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12261 if (attr)
12262 {
12263 length = DW_UNSND (attr);
12264 }
12265 else
12266 {
12267 /* Check for the DW_AT_byte_size attribute. */
12268 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12269 if (attr)
12270 {
12271 length = DW_UNSND (attr);
12272 }
12273 else
12274 {
12275 length = 1;
12276 }
12277 }
12278
12279 index_type = objfile_type (objfile)->builtin_int;
12280 range_type = create_range_type (NULL, index_type, 1, length);
12281 char_type = language_string_char_type (cu->language_defn, gdbarch);
12282 type = create_string_type (NULL, char_type, range_type);
12283
12284 return set_die_type (die, type, cu);
12285 }
12286
12287 /* Handle DIES due to C code like:
12288
12289 struct foo
12290 {
12291 int (*funcp)(int a, long l);
12292 int b;
12293 };
12294
12295 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12296
12297 static struct type *
12298 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12299 {
12300 struct objfile *objfile = cu->objfile;
12301 struct type *type; /* Type that this function returns. */
12302 struct type *ftype; /* Function that returns above type. */
12303 struct attribute *attr;
12304
12305 type = die_type (die, cu);
12306
12307 /* The die_type call above may have already set the type for this DIE. */
12308 ftype = get_die_type (die, cu);
12309 if (ftype)
12310 return ftype;
12311
12312 ftype = lookup_function_type (type);
12313
12314 /* All functions in C++, Pascal and Java have prototypes. */
12315 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12316 if ((attr && (DW_UNSND (attr) != 0))
12317 || cu->language == language_cplus
12318 || cu->language == language_java
12319 || cu->language == language_pascal)
12320 TYPE_PROTOTYPED (ftype) = 1;
12321 else if (producer_is_realview (cu->producer))
12322 /* RealView does not emit DW_AT_prototyped. We can not
12323 distinguish prototyped and unprototyped functions; default to
12324 prototyped, since that is more common in modern code (and
12325 RealView warns about unprototyped functions). */
12326 TYPE_PROTOTYPED (ftype) = 1;
12327
12328 /* Store the calling convention in the type if it's available in
12329 the subroutine die. Otherwise set the calling convention to
12330 the default value DW_CC_normal. */
12331 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12332 if (attr)
12333 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12334 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12335 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12336 else
12337 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12338
12339 /* We need to add the subroutine type to the die immediately so
12340 we don't infinitely recurse when dealing with parameters
12341 declared as the same subroutine type. */
12342 set_die_type (die, ftype, cu);
12343
12344 if (die->child != NULL)
12345 {
12346 struct type *void_type = objfile_type (objfile)->builtin_void;
12347 struct die_info *child_die;
12348 int nparams, iparams;
12349
12350 /* Count the number of parameters.
12351 FIXME: GDB currently ignores vararg functions, but knows about
12352 vararg member functions. */
12353 nparams = 0;
12354 child_die = die->child;
12355 while (child_die && child_die->tag)
12356 {
12357 if (child_die->tag == DW_TAG_formal_parameter)
12358 nparams++;
12359 else if (child_die->tag == DW_TAG_unspecified_parameters)
12360 TYPE_VARARGS (ftype) = 1;
12361 child_die = sibling_die (child_die);
12362 }
12363
12364 /* Allocate storage for parameters and fill them in. */
12365 TYPE_NFIELDS (ftype) = nparams;
12366 TYPE_FIELDS (ftype) = (struct field *)
12367 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12368
12369 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12370 even if we error out during the parameters reading below. */
12371 for (iparams = 0; iparams < nparams; iparams++)
12372 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12373
12374 iparams = 0;
12375 child_die = die->child;
12376 while (child_die && child_die->tag)
12377 {
12378 if (child_die->tag == DW_TAG_formal_parameter)
12379 {
12380 struct type *arg_type;
12381
12382 /* DWARF version 2 has no clean way to discern C++
12383 static and non-static member functions. G++ helps
12384 GDB by marking the first parameter for non-static
12385 member functions (which is the this pointer) as
12386 artificial. We pass this information to
12387 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12388
12389 DWARF version 3 added DW_AT_object_pointer, which GCC
12390 4.5 does not yet generate. */
12391 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12392 if (attr)
12393 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12394 else
12395 {
12396 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12397
12398 /* GCC/43521: In java, the formal parameter
12399 "this" is sometimes not marked with DW_AT_artificial. */
12400 if (cu->language == language_java)
12401 {
12402 const char *name = dwarf2_name (child_die, cu);
12403
12404 if (name && !strcmp (name, "this"))
12405 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12406 }
12407 }
12408 arg_type = die_type (child_die, cu);
12409
12410 /* RealView does not mark THIS as const, which the testsuite
12411 expects. GCC marks THIS as const in method definitions,
12412 but not in the class specifications (GCC PR 43053). */
12413 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12414 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12415 {
12416 int is_this = 0;
12417 struct dwarf2_cu *arg_cu = cu;
12418 const char *name = dwarf2_name (child_die, cu);
12419
12420 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12421 if (attr)
12422 {
12423 /* If the compiler emits this, use it. */
12424 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12425 is_this = 1;
12426 }
12427 else if (name && strcmp (name, "this") == 0)
12428 /* Function definitions will have the argument names. */
12429 is_this = 1;
12430 else if (name == NULL && iparams == 0)
12431 /* Declarations may not have the names, so like
12432 elsewhere in GDB, assume an artificial first
12433 argument is "this". */
12434 is_this = 1;
12435
12436 if (is_this)
12437 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12438 arg_type, 0);
12439 }
12440
12441 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12442 iparams++;
12443 }
12444 child_die = sibling_die (child_die);
12445 }
12446 }
12447
12448 return ftype;
12449 }
12450
12451 static struct type *
12452 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12453 {
12454 struct objfile *objfile = cu->objfile;
12455 const char *name = NULL;
12456 struct type *this_type, *target_type;
12457
12458 name = dwarf2_full_name (NULL, die, cu);
12459 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12460 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12461 TYPE_NAME (this_type) = (char *) name;
12462 set_die_type (die, this_type, cu);
12463 target_type = die_type (die, cu);
12464 if (target_type != this_type)
12465 TYPE_TARGET_TYPE (this_type) = target_type;
12466 else
12467 {
12468 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12469 spec and cause infinite loops in GDB. */
12470 complaint (&symfile_complaints,
12471 _("Self-referential DW_TAG_typedef "
12472 "- DIE at 0x%x [in module %s]"),
12473 die->offset.sect_off, objfile->name);
12474 TYPE_TARGET_TYPE (this_type) = NULL;
12475 }
12476 return this_type;
12477 }
12478
12479 /* Find a representation of a given base type and install
12480 it in the TYPE field of the die. */
12481
12482 static struct type *
12483 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12484 {
12485 struct objfile *objfile = cu->objfile;
12486 struct type *type;
12487 struct attribute *attr;
12488 int encoding = 0, size = 0;
12489 char *name;
12490 enum type_code code = TYPE_CODE_INT;
12491 int type_flags = 0;
12492 struct type *target_type = NULL;
12493
12494 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12495 if (attr)
12496 {
12497 encoding = DW_UNSND (attr);
12498 }
12499 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12500 if (attr)
12501 {
12502 size = DW_UNSND (attr);
12503 }
12504 name = dwarf2_name (die, cu);
12505 if (!name)
12506 {
12507 complaint (&symfile_complaints,
12508 _("DW_AT_name missing from DW_TAG_base_type"));
12509 }
12510
12511 switch (encoding)
12512 {
12513 case DW_ATE_address:
12514 /* Turn DW_ATE_address into a void * pointer. */
12515 code = TYPE_CODE_PTR;
12516 type_flags |= TYPE_FLAG_UNSIGNED;
12517 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12518 break;
12519 case DW_ATE_boolean:
12520 code = TYPE_CODE_BOOL;
12521 type_flags |= TYPE_FLAG_UNSIGNED;
12522 break;
12523 case DW_ATE_complex_float:
12524 code = TYPE_CODE_COMPLEX;
12525 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12526 break;
12527 case DW_ATE_decimal_float:
12528 code = TYPE_CODE_DECFLOAT;
12529 break;
12530 case DW_ATE_float:
12531 code = TYPE_CODE_FLT;
12532 break;
12533 case DW_ATE_signed:
12534 break;
12535 case DW_ATE_unsigned:
12536 type_flags |= TYPE_FLAG_UNSIGNED;
12537 if (cu->language == language_fortran
12538 && name
12539 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12540 code = TYPE_CODE_CHAR;
12541 break;
12542 case DW_ATE_signed_char:
12543 if (cu->language == language_ada || cu->language == language_m2
12544 || cu->language == language_pascal
12545 || cu->language == language_fortran)
12546 code = TYPE_CODE_CHAR;
12547 break;
12548 case DW_ATE_unsigned_char:
12549 if (cu->language == language_ada || cu->language == language_m2
12550 || cu->language == language_pascal
12551 || cu->language == language_fortran)
12552 code = TYPE_CODE_CHAR;
12553 type_flags |= TYPE_FLAG_UNSIGNED;
12554 break;
12555 case DW_ATE_UTF:
12556 /* We just treat this as an integer and then recognize the
12557 type by name elsewhere. */
12558 break;
12559
12560 default:
12561 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12562 dwarf_type_encoding_name (encoding));
12563 break;
12564 }
12565
12566 type = init_type (code, size, type_flags, NULL, objfile);
12567 TYPE_NAME (type) = name;
12568 TYPE_TARGET_TYPE (type) = target_type;
12569
12570 if (name && strcmp (name, "char") == 0)
12571 TYPE_NOSIGN (type) = 1;
12572
12573 return set_die_type (die, type, cu);
12574 }
12575
12576 /* Read the given DW_AT_subrange DIE. */
12577
12578 static struct type *
12579 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12580 {
12581 struct type *base_type;
12582 struct type *range_type;
12583 struct attribute *attr;
12584 LONGEST low, high;
12585 int low_default_is_valid;
12586 char *name;
12587 LONGEST negative_mask;
12588
12589 base_type = die_type (die, cu);
12590 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12591 check_typedef (base_type);
12592
12593 /* The die_type call above may have already set the type for this DIE. */
12594 range_type = get_die_type (die, cu);
12595 if (range_type)
12596 return range_type;
12597
12598 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12599 omitting DW_AT_lower_bound. */
12600 switch (cu->language)
12601 {
12602 case language_c:
12603 case language_cplus:
12604 low = 0;
12605 low_default_is_valid = 1;
12606 break;
12607 case language_fortran:
12608 low = 1;
12609 low_default_is_valid = 1;
12610 break;
12611 case language_d:
12612 case language_java:
12613 case language_objc:
12614 low = 0;
12615 low_default_is_valid = (cu->header.version >= 4);
12616 break;
12617 case language_ada:
12618 case language_m2:
12619 case language_pascal:
12620 low = 1;
12621 low_default_is_valid = (cu->header.version >= 4);
12622 break;
12623 default:
12624 low = 0;
12625 low_default_is_valid = 0;
12626 break;
12627 }
12628
12629 /* FIXME: For variable sized arrays either of these could be
12630 a variable rather than a constant value. We'll allow it,
12631 but we don't know how to handle it. */
12632 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12633 if (attr)
12634 low = dwarf2_get_attr_constant_value (attr, low);
12635 else if (!low_default_is_valid)
12636 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12637 "- DIE at 0x%x [in module %s]"),
12638 die->offset.sect_off, cu->objfile->name);
12639
12640 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12641 if (attr)
12642 {
12643 if (attr_form_is_block (attr) || is_ref_attr (attr))
12644 {
12645 /* GCC encodes arrays with unspecified or dynamic length
12646 with a DW_FORM_block1 attribute or a reference attribute.
12647 FIXME: GDB does not yet know how to handle dynamic
12648 arrays properly, treat them as arrays with unspecified
12649 length for now.
12650
12651 FIXME: jimb/2003-09-22: GDB does not really know
12652 how to handle arrays of unspecified length
12653 either; we just represent them as zero-length
12654 arrays. Choose an appropriate upper bound given
12655 the lower bound we've computed above. */
12656 high = low - 1;
12657 }
12658 else
12659 high = dwarf2_get_attr_constant_value (attr, 1);
12660 }
12661 else
12662 {
12663 attr = dwarf2_attr (die, DW_AT_count, cu);
12664 if (attr)
12665 {
12666 int count = dwarf2_get_attr_constant_value (attr, 1);
12667 high = low + count - 1;
12668 }
12669 else
12670 {
12671 /* Unspecified array length. */
12672 high = low - 1;
12673 }
12674 }
12675
12676 /* Dwarf-2 specifications explicitly allows to create subrange types
12677 without specifying a base type.
12678 In that case, the base type must be set to the type of
12679 the lower bound, upper bound or count, in that order, if any of these
12680 three attributes references an object that has a type.
12681 If no base type is found, the Dwarf-2 specifications say that
12682 a signed integer type of size equal to the size of an address should
12683 be used.
12684 For the following C code: `extern char gdb_int [];'
12685 GCC produces an empty range DIE.
12686 FIXME: muller/2010-05-28: Possible references to object for low bound,
12687 high bound or count are not yet handled by this code. */
12688 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12689 {
12690 struct objfile *objfile = cu->objfile;
12691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12692 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12693 struct type *int_type = objfile_type (objfile)->builtin_int;
12694
12695 /* Test "int", "long int", and "long long int" objfile types,
12696 and select the first one having a size above or equal to the
12697 architecture address size. */
12698 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12699 base_type = int_type;
12700 else
12701 {
12702 int_type = objfile_type (objfile)->builtin_long;
12703 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12704 base_type = int_type;
12705 else
12706 {
12707 int_type = objfile_type (objfile)->builtin_long_long;
12708 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12709 base_type = int_type;
12710 }
12711 }
12712 }
12713
12714 negative_mask =
12715 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12716 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12717 low |= negative_mask;
12718 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12719 high |= negative_mask;
12720
12721 range_type = create_range_type (NULL, base_type, low, high);
12722
12723 /* Mark arrays with dynamic length at least as an array of unspecified
12724 length. GDB could check the boundary but before it gets implemented at
12725 least allow accessing the array elements. */
12726 if (attr && attr_form_is_block (attr))
12727 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12728
12729 /* Ada expects an empty array on no boundary attributes. */
12730 if (attr == NULL && cu->language != language_ada)
12731 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12732
12733 name = dwarf2_name (die, cu);
12734 if (name)
12735 TYPE_NAME (range_type) = name;
12736
12737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12738 if (attr)
12739 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12740
12741 set_die_type (die, range_type, cu);
12742
12743 /* set_die_type should be already done. */
12744 set_descriptive_type (range_type, die, cu);
12745
12746 return range_type;
12747 }
12748
12749 static struct type *
12750 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12751 {
12752 struct type *type;
12753
12754 /* For now, we only support the C meaning of an unspecified type: void. */
12755
12756 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12757 TYPE_NAME (type) = dwarf2_name (die, cu);
12758
12759 return set_die_type (die, type, cu);
12760 }
12761
12762 /* Read a single die and all its descendents. Set the die's sibling
12763 field to NULL; set other fields in the die correctly, and set all
12764 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12765 location of the info_ptr after reading all of those dies. PARENT
12766 is the parent of the die in question. */
12767
12768 static struct die_info *
12769 read_die_and_children (const struct die_reader_specs *reader,
12770 gdb_byte *info_ptr,
12771 gdb_byte **new_info_ptr,
12772 struct die_info *parent)
12773 {
12774 struct die_info *die;
12775 gdb_byte *cur_ptr;
12776 int has_children;
12777
12778 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12779 if (die == NULL)
12780 {
12781 *new_info_ptr = cur_ptr;
12782 return NULL;
12783 }
12784 store_in_ref_table (die, reader->cu);
12785
12786 if (has_children)
12787 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12788 else
12789 {
12790 die->child = NULL;
12791 *new_info_ptr = cur_ptr;
12792 }
12793
12794 die->sibling = NULL;
12795 die->parent = parent;
12796 return die;
12797 }
12798
12799 /* Read a die, all of its descendents, and all of its siblings; set
12800 all of the fields of all of the dies correctly. Arguments are as
12801 in read_die_and_children. */
12802
12803 static struct die_info *
12804 read_die_and_siblings (const struct die_reader_specs *reader,
12805 gdb_byte *info_ptr,
12806 gdb_byte **new_info_ptr,
12807 struct die_info *parent)
12808 {
12809 struct die_info *first_die, *last_sibling;
12810 gdb_byte *cur_ptr;
12811
12812 cur_ptr = info_ptr;
12813 first_die = last_sibling = NULL;
12814
12815 while (1)
12816 {
12817 struct die_info *die
12818 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12819
12820 if (die == NULL)
12821 {
12822 *new_info_ptr = cur_ptr;
12823 return first_die;
12824 }
12825
12826 if (!first_die)
12827 first_die = die;
12828 else
12829 last_sibling->sibling = die;
12830
12831 last_sibling = die;
12832 }
12833 }
12834
12835 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12836 attributes.
12837 The caller is responsible for filling in the extra attributes
12838 and updating (*DIEP)->num_attrs.
12839 Set DIEP to point to a newly allocated die with its information,
12840 except for its child, sibling, and parent fields.
12841 Set HAS_CHILDREN to tell whether the die has children or not. */
12842
12843 static gdb_byte *
12844 read_full_die_1 (const struct die_reader_specs *reader,
12845 struct die_info **diep, gdb_byte *info_ptr,
12846 int *has_children, int num_extra_attrs)
12847 {
12848 unsigned int abbrev_number, bytes_read, i;
12849 sect_offset offset;
12850 struct abbrev_info *abbrev;
12851 struct die_info *die;
12852 struct dwarf2_cu *cu = reader->cu;
12853 bfd *abfd = reader->abfd;
12854
12855 offset.sect_off = info_ptr - reader->buffer;
12856 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12857 info_ptr += bytes_read;
12858 if (!abbrev_number)
12859 {
12860 *diep = NULL;
12861 *has_children = 0;
12862 return info_ptr;
12863 }
12864
12865 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12866 if (!abbrev)
12867 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12868 abbrev_number,
12869 bfd_get_filename (abfd));
12870
12871 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12872 die->offset = offset;
12873 die->tag = abbrev->tag;
12874 die->abbrev = abbrev_number;
12875
12876 /* Make the result usable.
12877 The caller needs to update num_attrs after adding the extra
12878 attributes. */
12879 die->num_attrs = abbrev->num_attrs;
12880
12881 for (i = 0; i < abbrev->num_attrs; ++i)
12882 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12883 info_ptr);
12884
12885 *diep = die;
12886 *has_children = abbrev->has_children;
12887 return info_ptr;
12888 }
12889
12890 /* Read a die and all its attributes.
12891 Set DIEP to point to a newly allocated die with its information,
12892 except for its child, sibling, and parent fields.
12893 Set HAS_CHILDREN to tell whether the die has children or not. */
12894
12895 static gdb_byte *
12896 read_full_die (const struct die_reader_specs *reader,
12897 struct die_info **diep, gdb_byte *info_ptr,
12898 int *has_children)
12899 {
12900 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12901 }
12902 \f
12903 /* Abbreviation tables.
12904
12905 In DWARF version 2, the description of the debugging information is
12906 stored in a separate .debug_abbrev section. Before we read any
12907 dies from a section we read in all abbreviations and install them
12908 in a hash table. */
12909
12910 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12911
12912 static struct abbrev_info *
12913 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12914 {
12915 struct abbrev_info *abbrev;
12916
12917 abbrev = (struct abbrev_info *)
12918 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12919 memset (abbrev, 0, sizeof (struct abbrev_info));
12920 return abbrev;
12921 }
12922
12923 /* Add an abbreviation to the table. */
12924
12925 static void
12926 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12927 unsigned int abbrev_number,
12928 struct abbrev_info *abbrev)
12929 {
12930 unsigned int hash_number;
12931
12932 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12933 abbrev->next = abbrev_table->abbrevs[hash_number];
12934 abbrev_table->abbrevs[hash_number] = abbrev;
12935 }
12936
12937 /* Look up an abbrev in the table.
12938 Returns NULL if the abbrev is not found. */
12939
12940 static struct abbrev_info *
12941 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12942 unsigned int abbrev_number)
12943 {
12944 unsigned int hash_number;
12945 struct abbrev_info *abbrev;
12946
12947 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12948 abbrev = abbrev_table->abbrevs[hash_number];
12949
12950 while (abbrev)
12951 {
12952 if (abbrev->number == abbrev_number)
12953 return abbrev;
12954 abbrev = abbrev->next;
12955 }
12956 return NULL;
12957 }
12958
12959 /* Read in an abbrev table. */
12960
12961 static struct abbrev_table *
12962 abbrev_table_read_table (struct dwarf2_section_info *section,
12963 sect_offset offset)
12964 {
12965 struct objfile *objfile = dwarf2_per_objfile->objfile;
12966 bfd *abfd = section->asection->owner;
12967 struct abbrev_table *abbrev_table;
12968 gdb_byte *abbrev_ptr;
12969 struct abbrev_info *cur_abbrev;
12970 unsigned int abbrev_number, bytes_read, abbrev_name;
12971 unsigned int abbrev_form;
12972 struct attr_abbrev *cur_attrs;
12973 unsigned int allocated_attrs;
12974
12975 abbrev_table = XMALLOC (struct abbrev_table);
12976 abbrev_table->offset = offset;
12977 obstack_init (&abbrev_table->abbrev_obstack);
12978 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12979 (ABBREV_HASH_SIZE
12980 * sizeof (struct abbrev_info *)));
12981 memset (abbrev_table->abbrevs, 0,
12982 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12983
12984 dwarf2_read_section (objfile, section);
12985 abbrev_ptr = section->buffer + offset.sect_off;
12986 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12987 abbrev_ptr += bytes_read;
12988
12989 allocated_attrs = ATTR_ALLOC_CHUNK;
12990 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12991
12992 /* Loop until we reach an abbrev number of 0. */
12993 while (abbrev_number)
12994 {
12995 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12996
12997 /* read in abbrev header */
12998 cur_abbrev->number = abbrev_number;
12999 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13000 abbrev_ptr += bytes_read;
13001 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13002 abbrev_ptr += 1;
13003
13004 /* now read in declarations */
13005 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13006 abbrev_ptr += bytes_read;
13007 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13008 abbrev_ptr += bytes_read;
13009 while (abbrev_name)
13010 {
13011 if (cur_abbrev->num_attrs == allocated_attrs)
13012 {
13013 allocated_attrs += ATTR_ALLOC_CHUNK;
13014 cur_attrs
13015 = xrealloc (cur_attrs, (allocated_attrs
13016 * sizeof (struct attr_abbrev)));
13017 }
13018
13019 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13020 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13021 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13022 abbrev_ptr += bytes_read;
13023 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13024 abbrev_ptr += bytes_read;
13025 }
13026
13027 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13028 (cur_abbrev->num_attrs
13029 * sizeof (struct attr_abbrev)));
13030 memcpy (cur_abbrev->attrs, cur_attrs,
13031 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13032
13033 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13034
13035 /* Get next abbreviation.
13036 Under Irix6 the abbreviations for a compilation unit are not
13037 always properly terminated with an abbrev number of 0.
13038 Exit loop if we encounter an abbreviation which we have
13039 already read (which means we are about to read the abbreviations
13040 for the next compile unit) or if the end of the abbreviation
13041 table is reached. */
13042 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13043 break;
13044 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13045 abbrev_ptr += bytes_read;
13046 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13047 break;
13048 }
13049
13050 xfree (cur_attrs);
13051 return abbrev_table;
13052 }
13053
13054 /* Free the resources held by ABBREV_TABLE. */
13055
13056 static void
13057 abbrev_table_free (struct abbrev_table *abbrev_table)
13058 {
13059 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13060 xfree (abbrev_table);
13061 }
13062
13063 /* Same as abbrev_table_free but as a cleanup.
13064 We pass in a pointer to the pointer to the table so that we can
13065 set the pointer to NULL when we're done. It also simplifies
13066 build_type_unit_groups. */
13067
13068 static void
13069 abbrev_table_free_cleanup (void *table_ptr)
13070 {
13071 struct abbrev_table **abbrev_table_ptr = table_ptr;
13072
13073 if (*abbrev_table_ptr != NULL)
13074 abbrev_table_free (*abbrev_table_ptr);
13075 *abbrev_table_ptr = NULL;
13076 }
13077
13078 /* Read the abbrev table for CU from ABBREV_SECTION. */
13079
13080 static void
13081 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13082 struct dwarf2_section_info *abbrev_section)
13083 {
13084 cu->abbrev_table =
13085 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13086 }
13087
13088 /* Release the memory used by the abbrev table for a compilation unit. */
13089
13090 static void
13091 dwarf2_free_abbrev_table (void *ptr_to_cu)
13092 {
13093 struct dwarf2_cu *cu = ptr_to_cu;
13094
13095 abbrev_table_free (cu->abbrev_table);
13096 /* Set this to NULL so that we SEGV if we try to read it later,
13097 and also because free_comp_unit verifies this is NULL. */
13098 cu->abbrev_table = NULL;
13099 }
13100 \f
13101 /* Returns nonzero if TAG represents a type that we might generate a partial
13102 symbol for. */
13103
13104 static int
13105 is_type_tag_for_partial (int tag)
13106 {
13107 switch (tag)
13108 {
13109 #if 0
13110 /* Some types that would be reasonable to generate partial symbols for,
13111 that we don't at present. */
13112 case DW_TAG_array_type:
13113 case DW_TAG_file_type:
13114 case DW_TAG_ptr_to_member_type:
13115 case DW_TAG_set_type:
13116 case DW_TAG_string_type:
13117 case DW_TAG_subroutine_type:
13118 #endif
13119 case DW_TAG_base_type:
13120 case DW_TAG_class_type:
13121 case DW_TAG_interface_type:
13122 case DW_TAG_enumeration_type:
13123 case DW_TAG_structure_type:
13124 case DW_TAG_subrange_type:
13125 case DW_TAG_typedef:
13126 case DW_TAG_union_type:
13127 return 1;
13128 default:
13129 return 0;
13130 }
13131 }
13132
13133 /* Load all DIEs that are interesting for partial symbols into memory. */
13134
13135 static struct partial_die_info *
13136 load_partial_dies (const struct die_reader_specs *reader,
13137 gdb_byte *info_ptr, int building_psymtab)
13138 {
13139 struct dwarf2_cu *cu = reader->cu;
13140 struct objfile *objfile = cu->objfile;
13141 struct partial_die_info *part_die;
13142 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13143 struct abbrev_info *abbrev;
13144 unsigned int bytes_read;
13145 unsigned int load_all = 0;
13146 int nesting_level = 1;
13147
13148 parent_die = NULL;
13149 last_die = NULL;
13150
13151 gdb_assert (cu->per_cu != NULL);
13152 if (cu->per_cu->load_all_dies)
13153 load_all = 1;
13154
13155 cu->partial_dies
13156 = htab_create_alloc_ex (cu->header.length / 12,
13157 partial_die_hash,
13158 partial_die_eq,
13159 NULL,
13160 &cu->comp_unit_obstack,
13161 hashtab_obstack_allocate,
13162 dummy_obstack_deallocate);
13163
13164 part_die = obstack_alloc (&cu->comp_unit_obstack,
13165 sizeof (struct partial_die_info));
13166
13167 while (1)
13168 {
13169 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13170
13171 /* A NULL abbrev means the end of a series of children. */
13172 if (abbrev == NULL)
13173 {
13174 if (--nesting_level == 0)
13175 {
13176 /* PART_DIE was probably the last thing allocated on the
13177 comp_unit_obstack, so we could call obstack_free
13178 here. We don't do that because the waste is small,
13179 and will be cleaned up when we're done with this
13180 compilation unit. This way, we're also more robust
13181 against other users of the comp_unit_obstack. */
13182 return first_die;
13183 }
13184 info_ptr += bytes_read;
13185 last_die = parent_die;
13186 parent_die = parent_die->die_parent;
13187 continue;
13188 }
13189
13190 /* Check for template arguments. We never save these; if
13191 they're seen, we just mark the parent, and go on our way. */
13192 if (parent_die != NULL
13193 && cu->language == language_cplus
13194 && (abbrev->tag == DW_TAG_template_type_param
13195 || abbrev->tag == DW_TAG_template_value_param))
13196 {
13197 parent_die->has_template_arguments = 1;
13198
13199 if (!load_all)
13200 {
13201 /* We don't need a partial DIE for the template argument. */
13202 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13203 continue;
13204 }
13205 }
13206
13207 /* We only recurse into c++ subprograms looking for template arguments.
13208 Skip their other children. */
13209 if (!load_all
13210 && cu->language == language_cplus
13211 && parent_die != NULL
13212 && parent_die->tag == DW_TAG_subprogram)
13213 {
13214 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13215 continue;
13216 }
13217
13218 /* Check whether this DIE is interesting enough to save. Normally
13219 we would not be interested in members here, but there may be
13220 later variables referencing them via DW_AT_specification (for
13221 static members). */
13222 if (!load_all
13223 && !is_type_tag_for_partial (abbrev->tag)
13224 && abbrev->tag != DW_TAG_constant
13225 && abbrev->tag != DW_TAG_enumerator
13226 && abbrev->tag != DW_TAG_subprogram
13227 && abbrev->tag != DW_TAG_lexical_block
13228 && abbrev->tag != DW_TAG_variable
13229 && abbrev->tag != DW_TAG_namespace
13230 && abbrev->tag != DW_TAG_module
13231 && abbrev->tag != DW_TAG_member
13232 && abbrev->tag != DW_TAG_imported_unit)
13233 {
13234 /* Otherwise we skip to the next sibling, if any. */
13235 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13236 continue;
13237 }
13238
13239 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13240 info_ptr);
13241
13242 /* This two-pass algorithm for processing partial symbols has a
13243 high cost in cache pressure. Thus, handle some simple cases
13244 here which cover the majority of C partial symbols. DIEs
13245 which neither have specification tags in them, nor could have
13246 specification tags elsewhere pointing at them, can simply be
13247 processed and discarded.
13248
13249 This segment is also optional; scan_partial_symbols and
13250 add_partial_symbol will handle these DIEs if we chain
13251 them in normally. When compilers which do not emit large
13252 quantities of duplicate debug information are more common,
13253 this code can probably be removed. */
13254
13255 /* Any complete simple types at the top level (pretty much all
13256 of them, for a language without namespaces), can be processed
13257 directly. */
13258 if (parent_die == NULL
13259 && part_die->has_specification == 0
13260 && part_die->is_declaration == 0
13261 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13262 || part_die->tag == DW_TAG_base_type
13263 || part_die->tag == DW_TAG_subrange_type))
13264 {
13265 if (building_psymtab && part_die->name != NULL)
13266 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13267 VAR_DOMAIN, LOC_TYPEDEF,
13268 &objfile->static_psymbols,
13269 0, (CORE_ADDR) 0, cu->language, objfile);
13270 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13271 continue;
13272 }
13273
13274 /* The exception for DW_TAG_typedef with has_children above is
13275 a workaround of GCC PR debug/47510. In the case of this complaint
13276 type_name_no_tag_or_error will error on such types later.
13277
13278 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13279 it could not find the child DIEs referenced later, this is checked
13280 above. In correct DWARF DW_TAG_typedef should have no children. */
13281
13282 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13283 complaint (&symfile_complaints,
13284 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13285 "- DIE at 0x%x [in module %s]"),
13286 part_die->offset.sect_off, objfile->name);
13287
13288 /* If we're at the second level, and we're an enumerator, and
13289 our parent has no specification (meaning possibly lives in a
13290 namespace elsewhere), then we can add the partial symbol now
13291 instead of queueing it. */
13292 if (part_die->tag == DW_TAG_enumerator
13293 && parent_die != NULL
13294 && parent_die->die_parent == NULL
13295 && parent_die->tag == DW_TAG_enumeration_type
13296 && parent_die->has_specification == 0)
13297 {
13298 if (part_die->name == NULL)
13299 complaint (&symfile_complaints,
13300 _("malformed enumerator DIE ignored"));
13301 else if (building_psymtab)
13302 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13303 VAR_DOMAIN, LOC_CONST,
13304 (cu->language == language_cplus
13305 || cu->language == language_java)
13306 ? &objfile->global_psymbols
13307 : &objfile->static_psymbols,
13308 0, (CORE_ADDR) 0, cu->language, objfile);
13309
13310 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13311 continue;
13312 }
13313
13314 /* We'll save this DIE so link it in. */
13315 part_die->die_parent = parent_die;
13316 part_die->die_sibling = NULL;
13317 part_die->die_child = NULL;
13318
13319 if (last_die && last_die == parent_die)
13320 last_die->die_child = part_die;
13321 else if (last_die)
13322 last_die->die_sibling = part_die;
13323
13324 last_die = part_die;
13325
13326 if (first_die == NULL)
13327 first_die = part_die;
13328
13329 /* Maybe add the DIE to the hash table. Not all DIEs that we
13330 find interesting need to be in the hash table, because we
13331 also have the parent/sibling/child chains; only those that we
13332 might refer to by offset later during partial symbol reading.
13333
13334 For now this means things that might have be the target of a
13335 DW_AT_specification, DW_AT_abstract_origin, or
13336 DW_AT_extension. DW_AT_extension will refer only to
13337 namespaces; DW_AT_abstract_origin refers to functions (and
13338 many things under the function DIE, but we do not recurse
13339 into function DIEs during partial symbol reading) and
13340 possibly variables as well; DW_AT_specification refers to
13341 declarations. Declarations ought to have the DW_AT_declaration
13342 flag. It happens that GCC forgets to put it in sometimes, but
13343 only for functions, not for types.
13344
13345 Adding more things than necessary to the hash table is harmless
13346 except for the performance cost. Adding too few will result in
13347 wasted time in find_partial_die, when we reread the compilation
13348 unit with load_all_dies set. */
13349
13350 if (load_all
13351 || abbrev->tag == DW_TAG_constant
13352 || abbrev->tag == DW_TAG_subprogram
13353 || abbrev->tag == DW_TAG_variable
13354 || abbrev->tag == DW_TAG_namespace
13355 || part_die->is_declaration)
13356 {
13357 void **slot;
13358
13359 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13360 part_die->offset.sect_off, INSERT);
13361 *slot = part_die;
13362 }
13363
13364 part_die = obstack_alloc (&cu->comp_unit_obstack,
13365 sizeof (struct partial_die_info));
13366
13367 /* For some DIEs we want to follow their children (if any). For C
13368 we have no reason to follow the children of structures; for other
13369 languages we have to, so that we can get at method physnames
13370 to infer fully qualified class names, for DW_AT_specification,
13371 and for C++ template arguments. For C++, we also look one level
13372 inside functions to find template arguments (if the name of the
13373 function does not already contain the template arguments).
13374
13375 For Ada, we need to scan the children of subprograms and lexical
13376 blocks as well because Ada allows the definition of nested
13377 entities that could be interesting for the debugger, such as
13378 nested subprograms for instance. */
13379 if (last_die->has_children
13380 && (load_all
13381 || last_die->tag == DW_TAG_namespace
13382 || last_die->tag == DW_TAG_module
13383 || last_die->tag == DW_TAG_enumeration_type
13384 || (cu->language == language_cplus
13385 && last_die->tag == DW_TAG_subprogram
13386 && (last_die->name == NULL
13387 || strchr (last_die->name, '<') == NULL))
13388 || (cu->language != language_c
13389 && (last_die->tag == DW_TAG_class_type
13390 || last_die->tag == DW_TAG_interface_type
13391 || last_die->tag == DW_TAG_structure_type
13392 || last_die->tag == DW_TAG_union_type))
13393 || (cu->language == language_ada
13394 && (last_die->tag == DW_TAG_subprogram
13395 || last_die->tag == DW_TAG_lexical_block))))
13396 {
13397 nesting_level++;
13398 parent_die = last_die;
13399 continue;
13400 }
13401
13402 /* Otherwise we skip to the next sibling, if any. */
13403 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13404
13405 /* Back to the top, do it again. */
13406 }
13407 }
13408
13409 /* Read a minimal amount of information into the minimal die structure. */
13410
13411 static gdb_byte *
13412 read_partial_die (const struct die_reader_specs *reader,
13413 struct partial_die_info *part_die,
13414 struct abbrev_info *abbrev, unsigned int abbrev_len,
13415 gdb_byte *info_ptr)
13416 {
13417 struct dwarf2_cu *cu = reader->cu;
13418 struct objfile *objfile = cu->objfile;
13419 gdb_byte *buffer = reader->buffer;
13420 unsigned int i;
13421 struct attribute attr;
13422 int has_low_pc_attr = 0;
13423 int has_high_pc_attr = 0;
13424 int high_pc_relative = 0;
13425
13426 memset (part_die, 0, sizeof (struct partial_die_info));
13427
13428 part_die->offset.sect_off = info_ptr - buffer;
13429
13430 info_ptr += abbrev_len;
13431
13432 if (abbrev == NULL)
13433 return info_ptr;
13434
13435 part_die->tag = abbrev->tag;
13436 part_die->has_children = abbrev->has_children;
13437
13438 for (i = 0; i < abbrev->num_attrs; ++i)
13439 {
13440 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13441
13442 /* Store the data if it is of an attribute we want to keep in a
13443 partial symbol table. */
13444 switch (attr.name)
13445 {
13446 case DW_AT_name:
13447 switch (part_die->tag)
13448 {
13449 case DW_TAG_compile_unit:
13450 case DW_TAG_partial_unit:
13451 case DW_TAG_type_unit:
13452 /* Compilation units have a DW_AT_name that is a filename, not
13453 a source language identifier. */
13454 case DW_TAG_enumeration_type:
13455 case DW_TAG_enumerator:
13456 /* These tags always have simple identifiers already; no need
13457 to canonicalize them. */
13458 part_die->name = DW_STRING (&attr);
13459 break;
13460 default:
13461 part_die->name
13462 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13463 &objfile->objfile_obstack);
13464 break;
13465 }
13466 break;
13467 case DW_AT_linkage_name:
13468 case DW_AT_MIPS_linkage_name:
13469 /* Note that both forms of linkage name might appear. We
13470 assume they will be the same, and we only store the last
13471 one we see. */
13472 if (cu->language == language_ada)
13473 part_die->name = DW_STRING (&attr);
13474 part_die->linkage_name = DW_STRING (&attr);
13475 break;
13476 case DW_AT_low_pc:
13477 has_low_pc_attr = 1;
13478 part_die->lowpc = DW_ADDR (&attr);
13479 break;
13480 case DW_AT_high_pc:
13481 has_high_pc_attr = 1;
13482 if (attr.form == DW_FORM_addr
13483 || attr.form == DW_FORM_GNU_addr_index)
13484 part_die->highpc = DW_ADDR (&attr);
13485 else
13486 {
13487 high_pc_relative = 1;
13488 part_die->highpc = DW_UNSND (&attr);
13489 }
13490 break;
13491 case DW_AT_location:
13492 /* Support the .debug_loc offsets. */
13493 if (attr_form_is_block (&attr))
13494 {
13495 part_die->d.locdesc = DW_BLOCK (&attr);
13496 }
13497 else if (attr_form_is_section_offset (&attr))
13498 {
13499 dwarf2_complex_location_expr_complaint ();
13500 }
13501 else
13502 {
13503 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13504 "partial symbol information");
13505 }
13506 break;
13507 case DW_AT_external:
13508 part_die->is_external = DW_UNSND (&attr);
13509 break;
13510 case DW_AT_declaration:
13511 part_die->is_declaration = DW_UNSND (&attr);
13512 break;
13513 case DW_AT_type:
13514 part_die->has_type = 1;
13515 break;
13516 case DW_AT_abstract_origin:
13517 case DW_AT_specification:
13518 case DW_AT_extension:
13519 part_die->has_specification = 1;
13520 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13521 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13522 || cu->per_cu->is_dwz);
13523 break;
13524 case DW_AT_sibling:
13525 /* Ignore absolute siblings, they might point outside of
13526 the current compile unit. */
13527 if (attr.form == DW_FORM_ref_addr)
13528 complaint (&symfile_complaints,
13529 _("ignoring absolute DW_AT_sibling"));
13530 else
13531 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13532 break;
13533 case DW_AT_byte_size:
13534 part_die->has_byte_size = 1;
13535 break;
13536 case DW_AT_calling_convention:
13537 /* DWARF doesn't provide a way to identify a program's source-level
13538 entry point. DW_AT_calling_convention attributes are only meant
13539 to describe functions' calling conventions.
13540
13541 However, because it's a necessary piece of information in
13542 Fortran, and because DW_CC_program is the only piece of debugging
13543 information whose definition refers to a 'main program' at all,
13544 several compilers have begun marking Fortran main programs with
13545 DW_CC_program --- even when those functions use the standard
13546 calling conventions.
13547
13548 So until DWARF specifies a way to provide this information and
13549 compilers pick up the new representation, we'll support this
13550 practice. */
13551 if (DW_UNSND (&attr) == DW_CC_program
13552 && cu->language == language_fortran)
13553 {
13554 set_main_name (part_die->name);
13555
13556 /* As this DIE has a static linkage the name would be difficult
13557 to look up later. */
13558 language_of_main = language_fortran;
13559 }
13560 break;
13561 case DW_AT_inline:
13562 if (DW_UNSND (&attr) == DW_INL_inlined
13563 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13564 part_die->may_be_inlined = 1;
13565 break;
13566
13567 case DW_AT_import:
13568 if (part_die->tag == DW_TAG_imported_unit)
13569 {
13570 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13571 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13572 || cu->per_cu->is_dwz);
13573 }
13574 break;
13575
13576 default:
13577 break;
13578 }
13579 }
13580
13581 if (high_pc_relative)
13582 part_die->highpc += part_die->lowpc;
13583
13584 if (has_low_pc_attr && has_high_pc_attr)
13585 {
13586 /* When using the GNU linker, .gnu.linkonce. sections are used to
13587 eliminate duplicate copies of functions and vtables and such.
13588 The linker will arbitrarily choose one and discard the others.
13589 The AT_*_pc values for such functions refer to local labels in
13590 these sections. If the section from that file was discarded, the
13591 labels are not in the output, so the relocs get a value of 0.
13592 If this is a discarded function, mark the pc bounds as invalid,
13593 so that GDB will ignore it. */
13594 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13595 {
13596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13597
13598 complaint (&symfile_complaints,
13599 _("DW_AT_low_pc %s is zero "
13600 "for DIE at 0x%x [in module %s]"),
13601 paddress (gdbarch, part_die->lowpc),
13602 part_die->offset.sect_off, objfile->name);
13603 }
13604 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13605 else if (part_die->lowpc >= part_die->highpc)
13606 {
13607 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13608
13609 complaint (&symfile_complaints,
13610 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13611 "for DIE at 0x%x [in module %s]"),
13612 paddress (gdbarch, part_die->lowpc),
13613 paddress (gdbarch, part_die->highpc),
13614 part_die->offset.sect_off, objfile->name);
13615 }
13616 else
13617 part_die->has_pc_info = 1;
13618 }
13619
13620 return info_ptr;
13621 }
13622
13623 /* Find a cached partial DIE at OFFSET in CU. */
13624
13625 static struct partial_die_info *
13626 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13627 {
13628 struct partial_die_info *lookup_die = NULL;
13629 struct partial_die_info part_die;
13630
13631 part_die.offset = offset;
13632 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13633 offset.sect_off);
13634
13635 return lookup_die;
13636 }
13637
13638 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13639 except in the case of .debug_types DIEs which do not reference
13640 outside their CU (they do however referencing other types via
13641 DW_FORM_ref_sig8). */
13642
13643 static struct partial_die_info *
13644 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13645 {
13646 struct objfile *objfile = cu->objfile;
13647 struct dwarf2_per_cu_data *per_cu = NULL;
13648 struct partial_die_info *pd = NULL;
13649
13650 if (offset_in_dwz == cu->per_cu->is_dwz
13651 && offset_in_cu_p (&cu->header, offset))
13652 {
13653 pd = find_partial_die_in_comp_unit (offset, cu);
13654 if (pd != NULL)
13655 return pd;
13656 /* We missed recording what we needed.
13657 Load all dies and try again. */
13658 per_cu = cu->per_cu;
13659 }
13660 else
13661 {
13662 /* TUs don't reference other CUs/TUs (except via type signatures). */
13663 if (cu->per_cu->is_debug_types)
13664 {
13665 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13666 " external reference to offset 0x%lx [in module %s].\n"),
13667 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13668 bfd_get_filename (objfile->obfd));
13669 }
13670 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13671 objfile);
13672
13673 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13674 load_partial_comp_unit (per_cu);
13675
13676 per_cu->cu->last_used = 0;
13677 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13678 }
13679
13680 /* If we didn't find it, and not all dies have been loaded,
13681 load them all and try again. */
13682
13683 if (pd == NULL && per_cu->load_all_dies == 0)
13684 {
13685 per_cu->load_all_dies = 1;
13686
13687 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13688 THIS_CU->cu may already be in use. So we can't just free it and
13689 replace its DIEs with the ones we read in. Instead, we leave those
13690 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13691 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13692 set. */
13693 load_partial_comp_unit (per_cu);
13694
13695 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13696 }
13697
13698 if (pd == NULL)
13699 internal_error (__FILE__, __LINE__,
13700 _("could not find partial DIE 0x%x "
13701 "in cache [from module %s]\n"),
13702 offset.sect_off, bfd_get_filename (objfile->obfd));
13703 return pd;
13704 }
13705
13706 /* See if we can figure out if the class lives in a namespace. We do
13707 this by looking for a member function; its demangled name will
13708 contain namespace info, if there is any. */
13709
13710 static void
13711 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13712 struct dwarf2_cu *cu)
13713 {
13714 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13715 what template types look like, because the demangler
13716 frequently doesn't give the same name as the debug info. We
13717 could fix this by only using the demangled name to get the
13718 prefix (but see comment in read_structure_type). */
13719
13720 struct partial_die_info *real_pdi;
13721 struct partial_die_info *child_pdi;
13722
13723 /* If this DIE (this DIE's specification, if any) has a parent, then
13724 we should not do this. We'll prepend the parent's fully qualified
13725 name when we create the partial symbol. */
13726
13727 real_pdi = struct_pdi;
13728 while (real_pdi->has_specification)
13729 real_pdi = find_partial_die (real_pdi->spec_offset,
13730 real_pdi->spec_is_dwz, cu);
13731
13732 if (real_pdi->die_parent != NULL)
13733 return;
13734
13735 for (child_pdi = struct_pdi->die_child;
13736 child_pdi != NULL;
13737 child_pdi = child_pdi->die_sibling)
13738 {
13739 if (child_pdi->tag == DW_TAG_subprogram
13740 && child_pdi->linkage_name != NULL)
13741 {
13742 char *actual_class_name
13743 = language_class_name_from_physname (cu->language_defn,
13744 child_pdi->linkage_name);
13745 if (actual_class_name != NULL)
13746 {
13747 struct_pdi->name
13748 = obsavestring (actual_class_name,
13749 strlen (actual_class_name),
13750 &cu->objfile->objfile_obstack);
13751 xfree (actual_class_name);
13752 }
13753 break;
13754 }
13755 }
13756 }
13757
13758 /* Adjust PART_DIE before generating a symbol for it. This function
13759 may set the is_external flag or change the DIE's name. */
13760
13761 static void
13762 fixup_partial_die (struct partial_die_info *part_die,
13763 struct dwarf2_cu *cu)
13764 {
13765 /* Once we've fixed up a die, there's no point in doing so again.
13766 This also avoids a memory leak if we were to call
13767 guess_partial_die_structure_name multiple times. */
13768 if (part_die->fixup_called)
13769 return;
13770
13771 /* If we found a reference attribute and the DIE has no name, try
13772 to find a name in the referred to DIE. */
13773
13774 if (part_die->name == NULL && part_die->has_specification)
13775 {
13776 struct partial_die_info *spec_die;
13777
13778 spec_die = find_partial_die (part_die->spec_offset,
13779 part_die->spec_is_dwz, cu);
13780
13781 fixup_partial_die (spec_die, cu);
13782
13783 if (spec_die->name)
13784 {
13785 part_die->name = spec_die->name;
13786
13787 /* Copy DW_AT_external attribute if it is set. */
13788 if (spec_die->is_external)
13789 part_die->is_external = spec_die->is_external;
13790 }
13791 }
13792
13793 /* Set default names for some unnamed DIEs. */
13794
13795 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13796 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13797
13798 /* If there is no parent die to provide a namespace, and there are
13799 children, see if we can determine the namespace from their linkage
13800 name. */
13801 if (cu->language == language_cplus
13802 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13803 && part_die->die_parent == NULL
13804 && part_die->has_children
13805 && (part_die->tag == DW_TAG_class_type
13806 || part_die->tag == DW_TAG_structure_type
13807 || part_die->tag == DW_TAG_union_type))
13808 guess_partial_die_structure_name (part_die, cu);
13809
13810 /* GCC might emit a nameless struct or union that has a linkage
13811 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13812 if (part_die->name == NULL
13813 && (part_die->tag == DW_TAG_class_type
13814 || part_die->tag == DW_TAG_interface_type
13815 || part_die->tag == DW_TAG_structure_type
13816 || part_die->tag == DW_TAG_union_type)
13817 && part_die->linkage_name != NULL)
13818 {
13819 char *demangled;
13820
13821 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13822 if (demangled)
13823 {
13824 const char *base;
13825
13826 /* Strip any leading namespaces/classes, keep only the base name.
13827 DW_AT_name for named DIEs does not contain the prefixes. */
13828 base = strrchr (demangled, ':');
13829 if (base && base > demangled && base[-1] == ':')
13830 base++;
13831 else
13832 base = demangled;
13833
13834 part_die->name = obsavestring (base, strlen (base),
13835 &cu->objfile->objfile_obstack);
13836 xfree (demangled);
13837 }
13838 }
13839
13840 part_die->fixup_called = 1;
13841 }
13842
13843 /* Read an attribute value described by an attribute form. */
13844
13845 static gdb_byte *
13846 read_attribute_value (const struct die_reader_specs *reader,
13847 struct attribute *attr, unsigned form,
13848 gdb_byte *info_ptr)
13849 {
13850 struct dwarf2_cu *cu = reader->cu;
13851 bfd *abfd = reader->abfd;
13852 struct comp_unit_head *cu_header = &cu->header;
13853 unsigned int bytes_read;
13854 struct dwarf_block *blk;
13855
13856 attr->form = form;
13857 switch (form)
13858 {
13859 case DW_FORM_ref_addr:
13860 if (cu->header.version == 2)
13861 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13862 else
13863 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13864 &cu->header, &bytes_read);
13865 info_ptr += bytes_read;
13866 break;
13867 case DW_FORM_GNU_ref_alt:
13868 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13869 info_ptr += bytes_read;
13870 break;
13871 case DW_FORM_addr:
13872 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13873 info_ptr += bytes_read;
13874 break;
13875 case DW_FORM_block2:
13876 blk = dwarf_alloc_block (cu);
13877 blk->size = read_2_bytes (abfd, info_ptr);
13878 info_ptr += 2;
13879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13880 info_ptr += blk->size;
13881 DW_BLOCK (attr) = blk;
13882 break;
13883 case DW_FORM_block4:
13884 blk = dwarf_alloc_block (cu);
13885 blk->size = read_4_bytes (abfd, info_ptr);
13886 info_ptr += 4;
13887 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13888 info_ptr += blk->size;
13889 DW_BLOCK (attr) = blk;
13890 break;
13891 case DW_FORM_data2:
13892 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13893 info_ptr += 2;
13894 break;
13895 case DW_FORM_data4:
13896 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13897 info_ptr += 4;
13898 break;
13899 case DW_FORM_data8:
13900 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13901 info_ptr += 8;
13902 break;
13903 case DW_FORM_sec_offset:
13904 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13905 info_ptr += bytes_read;
13906 break;
13907 case DW_FORM_string:
13908 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13909 DW_STRING_IS_CANONICAL (attr) = 0;
13910 info_ptr += bytes_read;
13911 break;
13912 case DW_FORM_strp:
13913 if (!cu->per_cu->is_dwz)
13914 {
13915 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13916 &bytes_read);
13917 DW_STRING_IS_CANONICAL (attr) = 0;
13918 info_ptr += bytes_read;
13919 break;
13920 }
13921 /* FALLTHROUGH */
13922 case DW_FORM_GNU_strp_alt:
13923 {
13924 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13925 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13926 &bytes_read);
13927
13928 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13929 DW_STRING_IS_CANONICAL (attr) = 0;
13930 info_ptr += bytes_read;
13931 }
13932 break;
13933 case DW_FORM_exprloc:
13934 case DW_FORM_block:
13935 blk = dwarf_alloc_block (cu);
13936 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13937 info_ptr += bytes_read;
13938 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13939 info_ptr += blk->size;
13940 DW_BLOCK (attr) = blk;
13941 break;
13942 case DW_FORM_block1:
13943 blk = dwarf_alloc_block (cu);
13944 blk->size = read_1_byte (abfd, info_ptr);
13945 info_ptr += 1;
13946 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13947 info_ptr += blk->size;
13948 DW_BLOCK (attr) = blk;
13949 break;
13950 case DW_FORM_data1:
13951 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13952 info_ptr += 1;
13953 break;
13954 case DW_FORM_flag:
13955 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13956 info_ptr += 1;
13957 break;
13958 case DW_FORM_flag_present:
13959 DW_UNSND (attr) = 1;
13960 break;
13961 case DW_FORM_sdata:
13962 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13963 info_ptr += bytes_read;
13964 break;
13965 case DW_FORM_udata:
13966 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13967 info_ptr += bytes_read;
13968 break;
13969 case DW_FORM_ref1:
13970 DW_UNSND (attr) = (cu->header.offset.sect_off
13971 + read_1_byte (abfd, info_ptr));
13972 info_ptr += 1;
13973 break;
13974 case DW_FORM_ref2:
13975 DW_UNSND (attr) = (cu->header.offset.sect_off
13976 + read_2_bytes (abfd, info_ptr));
13977 info_ptr += 2;
13978 break;
13979 case DW_FORM_ref4:
13980 DW_UNSND (attr) = (cu->header.offset.sect_off
13981 + read_4_bytes (abfd, info_ptr));
13982 info_ptr += 4;
13983 break;
13984 case DW_FORM_ref8:
13985 DW_UNSND (attr) = (cu->header.offset.sect_off
13986 + read_8_bytes (abfd, info_ptr));
13987 info_ptr += 8;
13988 break;
13989 case DW_FORM_ref_sig8:
13990 /* Convert the signature to something we can record in DW_UNSND
13991 for later lookup.
13992 NOTE: This is NULL if the type wasn't found. */
13993 DW_SIGNATURED_TYPE (attr) =
13994 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
13995 info_ptr += 8;
13996 break;
13997 case DW_FORM_ref_udata:
13998 DW_UNSND (attr) = (cu->header.offset.sect_off
13999 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14000 info_ptr += bytes_read;
14001 break;
14002 case DW_FORM_indirect:
14003 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14004 info_ptr += bytes_read;
14005 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14006 break;
14007 case DW_FORM_GNU_addr_index:
14008 if (reader->dwo_file == NULL)
14009 {
14010 /* For now flag a hard error.
14011 Later we can turn this into a complaint. */
14012 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14013 dwarf_form_name (form),
14014 bfd_get_filename (abfd));
14015 }
14016 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14017 info_ptr += bytes_read;
14018 break;
14019 case DW_FORM_GNU_str_index:
14020 if (reader->dwo_file == NULL)
14021 {
14022 /* For now flag a hard error.
14023 Later we can turn this into a complaint if warranted. */
14024 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14025 dwarf_form_name (form),
14026 bfd_get_filename (abfd));
14027 }
14028 {
14029 ULONGEST str_index =
14030 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14031
14032 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14033 DW_STRING_IS_CANONICAL (attr) = 0;
14034 info_ptr += bytes_read;
14035 }
14036 break;
14037 default:
14038 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14039 dwarf_form_name (form),
14040 bfd_get_filename (abfd));
14041 }
14042
14043 /* Super hack. */
14044 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14045 attr->form = DW_FORM_GNU_ref_alt;
14046
14047 /* We have seen instances where the compiler tried to emit a byte
14048 size attribute of -1 which ended up being encoded as an unsigned
14049 0xffffffff. Although 0xffffffff is technically a valid size value,
14050 an object of this size seems pretty unlikely so we can relatively
14051 safely treat these cases as if the size attribute was invalid and
14052 treat them as zero by default. */
14053 if (attr->name == DW_AT_byte_size
14054 && form == DW_FORM_data4
14055 && DW_UNSND (attr) >= 0xffffffff)
14056 {
14057 complaint
14058 (&symfile_complaints,
14059 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14060 hex_string (DW_UNSND (attr)));
14061 DW_UNSND (attr) = 0;
14062 }
14063
14064 return info_ptr;
14065 }
14066
14067 /* Read an attribute described by an abbreviated attribute. */
14068
14069 static gdb_byte *
14070 read_attribute (const struct die_reader_specs *reader,
14071 struct attribute *attr, struct attr_abbrev *abbrev,
14072 gdb_byte *info_ptr)
14073 {
14074 attr->name = abbrev->name;
14075 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14076 }
14077
14078 /* Read dwarf information from a buffer. */
14079
14080 static unsigned int
14081 read_1_byte (bfd *abfd, const gdb_byte *buf)
14082 {
14083 return bfd_get_8 (abfd, buf);
14084 }
14085
14086 static int
14087 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14088 {
14089 return bfd_get_signed_8 (abfd, buf);
14090 }
14091
14092 static unsigned int
14093 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14094 {
14095 return bfd_get_16 (abfd, buf);
14096 }
14097
14098 static int
14099 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14100 {
14101 return bfd_get_signed_16 (abfd, buf);
14102 }
14103
14104 static unsigned int
14105 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14106 {
14107 return bfd_get_32 (abfd, buf);
14108 }
14109
14110 static int
14111 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14112 {
14113 return bfd_get_signed_32 (abfd, buf);
14114 }
14115
14116 static ULONGEST
14117 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14118 {
14119 return bfd_get_64 (abfd, buf);
14120 }
14121
14122 static CORE_ADDR
14123 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14124 unsigned int *bytes_read)
14125 {
14126 struct comp_unit_head *cu_header = &cu->header;
14127 CORE_ADDR retval = 0;
14128
14129 if (cu_header->signed_addr_p)
14130 {
14131 switch (cu_header->addr_size)
14132 {
14133 case 2:
14134 retval = bfd_get_signed_16 (abfd, buf);
14135 break;
14136 case 4:
14137 retval = bfd_get_signed_32 (abfd, buf);
14138 break;
14139 case 8:
14140 retval = bfd_get_signed_64 (abfd, buf);
14141 break;
14142 default:
14143 internal_error (__FILE__, __LINE__,
14144 _("read_address: bad switch, signed [in module %s]"),
14145 bfd_get_filename (abfd));
14146 }
14147 }
14148 else
14149 {
14150 switch (cu_header->addr_size)
14151 {
14152 case 2:
14153 retval = bfd_get_16 (abfd, buf);
14154 break;
14155 case 4:
14156 retval = bfd_get_32 (abfd, buf);
14157 break;
14158 case 8:
14159 retval = bfd_get_64 (abfd, buf);
14160 break;
14161 default:
14162 internal_error (__FILE__, __LINE__,
14163 _("read_address: bad switch, "
14164 "unsigned [in module %s]"),
14165 bfd_get_filename (abfd));
14166 }
14167 }
14168
14169 *bytes_read = cu_header->addr_size;
14170 return retval;
14171 }
14172
14173 /* Read the initial length from a section. The (draft) DWARF 3
14174 specification allows the initial length to take up either 4 bytes
14175 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14176 bytes describe the length and all offsets will be 8 bytes in length
14177 instead of 4.
14178
14179 An older, non-standard 64-bit format is also handled by this
14180 function. The older format in question stores the initial length
14181 as an 8-byte quantity without an escape value. Lengths greater
14182 than 2^32 aren't very common which means that the initial 4 bytes
14183 is almost always zero. Since a length value of zero doesn't make
14184 sense for the 32-bit format, this initial zero can be considered to
14185 be an escape value which indicates the presence of the older 64-bit
14186 format. As written, the code can't detect (old format) lengths
14187 greater than 4GB. If it becomes necessary to handle lengths
14188 somewhat larger than 4GB, we could allow other small values (such
14189 as the non-sensical values of 1, 2, and 3) to also be used as
14190 escape values indicating the presence of the old format.
14191
14192 The value returned via bytes_read should be used to increment the
14193 relevant pointer after calling read_initial_length().
14194
14195 [ Note: read_initial_length() and read_offset() are based on the
14196 document entitled "DWARF Debugging Information Format", revision
14197 3, draft 8, dated November 19, 2001. This document was obtained
14198 from:
14199
14200 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14201
14202 This document is only a draft and is subject to change. (So beware.)
14203
14204 Details regarding the older, non-standard 64-bit format were
14205 determined empirically by examining 64-bit ELF files produced by
14206 the SGI toolchain on an IRIX 6.5 machine.
14207
14208 - Kevin, July 16, 2002
14209 ] */
14210
14211 static LONGEST
14212 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14213 {
14214 LONGEST length = bfd_get_32 (abfd, buf);
14215
14216 if (length == 0xffffffff)
14217 {
14218 length = bfd_get_64 (abfd, buf + 4);
14219 *bytes_read = 12;
14220 }
14221 else if (length == 0)
14222 {
14223 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14224 length = bfd_get_64 (abfd, buf);
14225 *bytes_read = 8;
14226 }
14227 else
14228 {
14229 *bytes_read = 4;
14230 }
14231
14232 return length;
14233 }
14234
14235 /* Cover function for read_initial_length.
14236 Returns the length of the object at BUF, and stores the size of the
14237 initial length in *BYTES_READ and stores the size that offsets will be in
14238 *OFFSET_SIZE.
14239 If the initial length size is not equivalent to that specified in
14240 CU_HEADER then issue a complaint.
14241 This is useful when reading non-comp-unit headers. */
14242
14243 static LONGEST
14244 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14245 const struct comp_unit_head *cu_header,
14246 unsigned int *bytes_read,
14247 unsigned int *offset_size)
14248 {
14249 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14250
14251 gdb_assert (cu_header->initial_length_size == 4
14252 || cu_header->initial_length_size == 8
14253 || cu_header->initial_length_size == 12);
14254
14255 if (cu_header->initial_length_size != *bytes_read)
14256 complaint (&symfile_complaints,
14257 _("intermixed 32-bit and 64-bit DWARF sections"));
14258
14259 *offset_size = (*bytes_read == 4) ? 4 : 8;
14260 return length;
14261 }
14262
14263 /* Read an offset from the data stream. The size of the offset is
14264 given by cu_header->offset_size. */
14265
14266 static LONGEST
14267 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14268 unsigned int *bytes_read)
14269 {
14270 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14271
14272 *bytes_read = cu_header->offset_size;
14273 return offset;
14274 }
14275
14276 /* Read an offset from the data stream. */
14277
14278 static LONGEST
14279 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14280 {
14281 LONGEST retval = 0;
14282
14283 switch (offset_size)
14284 {
14285 case 4:
14286 retval = bfd_get_32 (abfd, buf);
14287 break;
14288 case 8:
14289 retval = bfd_get_64 (abfd, buf);
14290 break;
14291 default:
14292 internal_error (__FILE__, __LINE__,
14293 _("read_offset_1: bad switch [in module %s]"),
14294 bfd_get_filename (abfd));
14295 }
14296
14297 return retval;
14298 }
14299
14300 static gdb_byte *
14301 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14302 {
14303 /* If the size of a host char is 8 bits, we can return a pointer
14304 to the buffer, otherwise we have to copy the data to a buffer
14305 allocated on the temporary obstack. */
14306 gdb_assert (HOST_CHAR_BIT == 8);
14307 return buf;
14308 }
14309
14310 static char *
14311 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14312 {
14313 /* If the size of a host char is 8 bits, we can return a pointer
14314 to the string, otherwise we have to copy the string to a buffer
14315 allocated on the temporary obstack. */
14316 gdb_assert (HOST_CHAR_BIT == 8);
14317 if (*buf == '\0')
14318 {
14319 *bytes_read_ptr = 1;
14320 return NULL;
14321 }
14322 *bytes_read_ptr = strlen ((char *) buf) + 1;
14323 return (char *) buf;
14324 }
14325
14326 static char *
14327 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14328 {
14329 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14330 if (dwarf2_per_objfile->str.buffer == NULL)
14331 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14332 bfd_get_filename (abfd));
14333 if (str_offset >= dwarf2_per_objfile->str.size)
14334 error (_("DW_FORM_strp pointing outside of "
14335 ".debug_str section [in module %s]"),
14336 bfd_get_filename (abfd));
14337 gdb_assert (HOST_CHAR_BIT == 8);
14338 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14339 return NULL;
14340 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14341 }
14342
14343 /* Read a string at offset STR_OFFSET in the .debug_str section from
14344 the .dwz file DWZ. Throw an error if the offset is too large. If
14345 the string consists of a single NUL byte, return NULL; otherwise
14346 return a pointer to the string. */
14347
14348 static char *
14349 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14350 {
14351 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14352
14353 if (dwz->str.buffer == NULL)
14354 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14355 "section [in module %s]"),
14356 bfd_get_filename (dwz->dwz_bfd));
14357 if (str_offset >= dwz->str.size)
14358 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14359 ".debug_str section [in module %s]"),
14360 bfd_get_filename (dwz->dwz_bfd));
14361 gdb_assert (HOST_CHAR_BIT == 8);
14362 if (dwz->str.buffer[str_offset] == '\0')
14363 return NULL;
14364 return (char *) (dwz->str.buffer + str_offset);
14365 }
14366
14367 static char *
14368 read_indirect_string (bfd *abfd, gdb_byte *buf,
14369 const struct comp_unit_head *cu_header,
14370 unsigned int *bytes_read_ptr)
14371 {
14372 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14373
14374 return read_indirect_string_at_offset (abfd, str_offset);
14375 }
14376
14377 static ULONGEST
14378 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14379 {
14380 ULONGEST result;
14381 unsigned int num_read;
14382 int i, shift;
14383 unsigned char byte;
14384
14385 result = 0;
14386 shift = 0;
14387 num_read = 0;
14388 i = 0;
14389 while (1)
14390 {
14391 byte = bfd_get_8 (abfd, buf);
14392 buf++;
14393 num_read++;
14394 result |= ((ULONGEST) (byte & 127) << shift);
14395 if ((byte & 128) == 0)
14396 {
14397 break;
14398 }
14399 shift += 7;
14400 }
14401 *bytes_read_ptr = num_read;
14402 return result;
14403 }
14404
14405 static LONGEST
14406 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14407 {
14408 LONGEST result;
14409 int i, shift, num_read;
14410 unsigned char byte;
14411
14412 result = 0;
14413 shift = 0;
14414 num_read = 0;
14415 i = 0;
14416 while (1)
14417 {
14418 byte = bfd_get_8 (abfd, buf);
14419 buf++;
14420 num_read++;
14421 result |= ((LONGEST) (byte & 127) << shift);
14422 shift += 7;
14423 if ((byte & 128) == 0)
14424 {
14425 break;
14426 }
14427 }
14428 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14429 result |= -(((LONGEST) 1) << shift);
14430 *bytes_read_ptr = num_read;
14431 return result;
14432 }
14433
14434 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14435 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14436 ADDR_SIZE is the size of addresses from the CU header. */
14437
14438 static CORE_ADDR
14439 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14440 {
14441 struct objfile *objfile = dwarf2_per_objfile->objfile;
14442 bfd *abfd = objfile->obfd;
14443 const gdb_byte *info_ptr;
14444
14445 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14446 if (dwarf2_per_objfile->addr.buffer == NULL)
14447 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14448 objfile->name);
14449 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14450 error (_("DW_FORM_addr_index pointing outside of "
14451 ".debug_addr section [in module %s]"),
14452 objfile->name);
14453 info_ptr = (dwarf2_per_objfile->addr.buffer
14454 + addr_base + addr_index * addr_size);
14455 if (addr_size == 4)
14456 return bfd_get_32 (abfd, info_ptr);
14457 else
14458 return bfd_get_64 (abfd, info_ptr);
14459 }
14460
14461 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14462
14463 static CORE_ADDR
14464 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14465 {
14466 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14467 }
14468
14469 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14470
14471 static CORE_ADDR
14472 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14473 unsigned int *bytes_read)
14474 {
14475 bfd *abfd = cu->objfile->obfd;
14476 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14477
14478 return read_addr_index (cu, addr_index);
14479 }
14480
14481 /* Data structure to pass results from dwarf2_read_addr_index_reader
14482 back to dwarf2_read_addr_index. */
14483
14484 struct dwarf2_read_addr_index_data
14485 {
14486 ULONGEST addr_base;
14487 int addr_size;
14488 };
14489
14490 /* die_reader_func for dwarf2_read_addr_index. */
14491
14492 static void
14493 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14494 gdb_byte *info_ptr,
14495 struct die_info *comp_unit_die,
14496 int has_children,
14497 void *data)
14498 {
14499 struct dwarf2_cu *cu = reader->cu;
14500 struct dwarf2_read_addr_index_data *aidata =
14501 (struct dwarf2_read_addr_index_data *) data;
14502
14503 aidata->addr_base = cu->addr_base;
14504 aidata->addr_size = cu->header.addr_size;
14505 }
14506
14507 /* Given an index in .debug_addr, fetch the value.
14508 NOTE: This can be called during dwarf expression evaluation,
14509 long after the debug information has been read, and thus per_cu->cu
14510 may no longer exist. */
14511
14512 CORE_ADDR
14513 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14514 unsigned int addr_index)
14515 {
14516 struct objfile *objfile = per_cu->objfile;
14517 struct dwarf2_cu *cu = per_cu->cu;
14518 ULONGEST addr_base;
14519 int addr_size;
14520
14521 /* This is intended to be called from outside this file. */
14522 dw2_setup (objfile);
14523
14524 /* We need addr_base and addr_size.
14525 If we don't have PER_CU->cu, we have to get it.
14526 Nasty, but the alternative is storing the needed info in PER_CU,
14527 which at this point doesn't seem justified: it's not clear how frequently
14528 it would get used and it would increase the size of every PER_CU.
14529 Entry points like dwarf2_per_cu_addr_size do a similar thing
14530 so we're not in uncharted territory here.
14531 Alas we need to be a bit more complicated as addr_base is contained
14532 in the DIE.
14533
14534 We don't need to read the entire CU(/TU).
14535 We just need the header and top level die.
14536
14537 IWBN to use the aging mechanism to let us lazily later discard the CU.
14538 For now we skip this optimization. */
14539
14540 if (cu != NULL)
14541 {
14542 addr_base = cu->addr_base;
14543 addr_size = cu->header.addr_size;
14544 }
14545 else
14546 {
14547 struct dwarf2_read_addr_index_data aidata;
14548
14549 /* Note: We can't use init_cutu_and_read_dies_simple here,
14550 we need addr_base. */
14551 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14552 dwarf2_read_addr_index_reader, &aidata);
14553 addr_base = aidata.addr_base;
14554 addr_size = aidata.addr_size;
14555 }
14556
14557 return read_addr_index_1 (addr_index, addr_base, addr_size);
14558 }
14559
14560 /* Given a DW_AT_str_index, fetch the string. */
14561
14562 static char *
14563 read_str_index (const struct die_reader_specs *reader,
14564 struct dwarf2_cu *cu, ULONGEST str_index)
14565 {
14566 struct objfile *objfile = dwarf2_per_objfile->objfile;
14567 const char *dwo_name = objfile->name;
14568 bfd *abfd = objfile->obfd;
14569 struct dwo_sections *sections = &reader->dwo_file->sections;
14570 gdb_byte *info_ptr;
14571 ULONGEST str_offset;
14572
14573 dwarf2_read_section (objfile, &sections->str);
14574 dwarf2_read_section (objfile, &sections->str_offsets);
14575 if (sections->str.buffer == NULL)
14576 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14577 " in CU at offset 0x%lx [in module %s]"),
14578 (long) cu->header.offset.sect_off, dwo_name);
14579 if (sections->str_offsets.buffer == NULL)
14580 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14581 " in CU at offset 0x%lx [in module %s]"),
14582 (long) cu->header.offset.sect_off, dwo_name);
14583 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14584 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14585 " section in CU at offset 0x%lx [in module %s]"),
14586 (long) cu->header.offset.sect_off, dwo_name);
14587 info_ptr = (sections->str_offsets.buffer
14588 + str_index * cu->header.offset_size);
14589 if (cu->header.offset_size == 4)
14590 str_offset = bfd_get_32 (abfd, info_ptr);
14591 else
14592 str_offset = bfd_get_64 (abfd, info_ptr);
14593 if (str_offset >= sections->str.size)
14594 error (_("Offset from DW_FORM_str_index pointing outside of"
14595 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14596 (long) cu->header.offset.sect_off, dwo_name);
14597 return (char *) (sections->str.buffer + str_offset);
14598 }
14599
14600 /* Return the length of an LEB128 number in BUF. */
14601
14602 static int
14603 leb128_size (const gdb_byte *buf)
14604 {
14605 const gdb_byte *begin = buf;
14606 gdb_byte byte;
14607
14608 while (1)
14609 {
14610 byte = *buf++;
14611 if ((byte & 128) == 0)
14612 return buf - begin;
14613 }
14614 }
14615
14616 static void
14617 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14618 {
14619 switch (lang)
14620 {
14621 case DW_LANG_C89:
14622 case DW_LANG_C99:
14623 case DW_LANG_C:
14624 cu->language = language_c;
14625 break;
14626 case DW_LANG_C_plus_plus:
14627 cu->language = language_cplus;
14628 break;
14629 case DW_LANG_D:
14630 cu->language = language_d;
14631 break;
14632 case DW_LANG_Fortran77:
14633 case DW_LANG_Fortran90:
14634 case DW_LANG_Fortran95:
14635 cu->language = language_fortran;
14636 break;
14637 case DW_LANG_Go:
14638 cu->language = language_go;
14639 break;
14640 case DW_LANG_Mips_Assembler:
14641 cu->language = language_asm;
14642 break;
14643 case DW_LANG_Java:
14644 cu->language = language_java;
14645 break;
14646 case DW_LANG_Ada83:
14647 case DW_LANG_Ada95:
14648 cu->language = language_ada;
14649 break;
14650 case DW_LANG_Modula2:
14651 cu->language = language_m2;
14652 break;
14653 case DW_LANG_Pascal83:
14654 cu->language = language_pascal;
14655 break;
14656 case DW_LANG_ObjC:
14657 cu->language = language_objc;
14658 break;
14659 case DW_LANG_Cobol74:
14660 case DW_LANG_Cobol85:
14661 default:
14662 cu->language = language_minimal;
14663 break;
14664 }
14665 cu->language_defn = language_def (cu->language);
14666 }
14667
14668 /* Return the named attribute or NULL if not there. */
14669
14670 static struct attribute *
14671 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14672 {
14673 for (;;)
14674 {
14675 unsigned int i;
14676 struct attribute *spec = NULL;
14677
14678 for (i = 0; i < die->num_attrs; ++i)
14679 {
14680 if (die->attrs[i].name == name)
14681 return &die->attrs[i];
14682 if (die->attrs[i].name == DW_AT_specification
14683 || die->attrs[i].name == DW_AT_abstract_origin)
14684 spec = &die->attrs[i];
14685 }
14686
14687 if (!spec)
14688 break;
14689
14690 die = follow_die_ref (die, spec, &cu);
14691 }
14692
14693 return NULL;
14694 }
14695
14696 /* Return the named attribute or NULL if not there,
14697 but do not follow DW_AT_specification, etc.
14698 This is for use in contexts where we're reading .debug_types dies.
14699 Following DW_AT_specification, DW_AT_abstract_origin will take us
14700 back up the chain, and we want to go down. */
14701
14702 static struct attribute *
14703 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14704 {
14705 unsigned int i;
14706
14707 for (i = 0; i < die->num_attrs; ++i)
14708 if (die->attrs[i].name == name)
14709 return &die->attrs[i];
14710
14711 return NULL;
14712 }
14713
14714 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14715 and holds a non-zero value. This function should only be used for
14716 DW_FORM_flag or DW_FORM_flag_present attributes. */
14717
14718 static int
14719 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14720 {
14721 struct attribute *attr = dwarf2_attr (die, name, cu);
14722
14723 return (attr && DW_UNSND (attr));
14724 }
14725
14726 static int
14727 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14728 {
14729 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14730 which value is non-zero. However, we have to be careful with
14731 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14732 (via dwarf2_flag_true_p) follows this attribute. So we may
14733 end up accidently finding a declaration attribute that belongs
14734 to a different DIE referenced by the specification attribute,
14735 even though the given DIE does not have a declaration attribute. */
14736 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14737 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14738 }
14739
14740 /* Return the die giving the specification for DIE, if there is
14741 one. *SPEC_CU is the CU containing DIE on input, and the CU
14742 containing the return value on output. If there is no
14743 specification, but there is an abstract origin, that is
14744 returned. */
14745
14746 static struct die_info *
14747 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14748 {
14749 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14750 *spec_cu);
14751
14752 if (spec_attr == NULL)
14753 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14754
14755 if (spec_attr == NULL)
14756 return NULL;
14757 else
14758 return follow_die_ref (die, spec_attr, spec_cu);
14759 }
14760
14761 /* Free the line_header structure *LH, and any arrays and strings it
14762 refers to.
14763 NOTE: This is also used as a "cleanup" function. */
14764
14765 static void
14766 free_line_header (struct line_header *lh)
14767 {
14768 if (lh->standard_opcode_lengths)
14769 xfree (lh->standard_opcode_lengths);
14770
14771 /* Remember that all the lh->file_names[i].name pointers are
14772 pointers into debug_line_buffer, and don't need to be freed. */
14773 if (lh->file_names)
14774 xfree (lh->file_names);
14775
14776 /* Similarly for the include directory names. */
14777 if (lh->include_dirs)
14778 xfree (lh->include_dirs);
14779
14780 xfree (lh);
14781 }
14782
14783 /* Add an entry to LH's include directory table. */
14784
14785 static void
14786 add_include_dir (struct line_header *lh, char *include_dir)
14787 {
14788 /* Grow the array if necessary. */
14789 if (lh->include_dirs_size == 0)
14790 {
14791 lh->include_dirs_size = 1; /* for testing */
14792 lh->include_dirs = xmalloc (lh->include_dirs_size
14793 * sizeof (*lh->include_dirs));
14794 }
14795 else if (lh->num_include_dirs >= lh->include_dirs_size)
14796 {
14797 lh->include_dirs_size *= 2;
14798 lh->include_dirs = xrealloc (lh->include_dirs,
14799 (lh->include_dirs_size
14800 * sizeof (*lh->include_dirs)));
14801 }
14802
14803 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14804 }
14805
14806 /* Add an entry to LH's file name table. */
14807
14808 static void
14809 add_file_name (struct line_header *lh,
14810 char *name,
14811 unsigned int dir_index,
14812 unsigned int mod_time,
14813 unsigned int length)
14814 {
14815 struct file_entry *fe;
14816
14817 /* Grow the array if necessary. */
14818 if (lh->file_names_size == 0)
14819 {
14820 lh->file_names_size = 1; /* for testing */
14821 lh->file_names = xmalloc (lh->file_names_size
14822 * sizeof (*lh->file_names));
14823 }
14824 else if (lh->num_file_names >= lh->file_names_size)
14825 {
14826 lh->file_names_size *= 2;
14827 lh->file_names = xrealloc (lh->file_names,
14828 (lh->file_names_size
14829 * sizeof (*lh->file_names)));
14830 }
14831
14832 fe = &lh->file_names[lh->num_file_names++];
14833 fe->name = name;
14834 fe->dir_index = dir_index;
14835 fe->mod_time = mod_time;
14836 fe->length = length;
14837 fe->included_p = 0;
14838 fe->symtab = NULL;
14839 }
14840
14841 /* A convenience function to find the proper .debug_line section for a
14842 CU. */
14843
14844 static struct dwarf2_section_info *
14845 get_debug_line_section (struct dwarf2_cu *cu)
14846 {
14847 struct dwarf2_section_info *section;
14848
14849 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14850 DWO file. */
14851 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14852 section = &cu->dwo_unit->dwo_file->sections.line;
14853 else if (cu->per_cu->is_dwz)
14854 {
14855 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14856
14857 section = &dwz->line;
14858 }
14859 else
14860 section = &dwarf2_per_objfile->line;
14861
14862 return section;
14863 }
14864
14865 /* Read the statement program header starting at OFFSET in
14866 .debug_line, or .debug_line.dwo. Return a pointer
14867 to a struct line_header, allocated using xmalloc.
14868
14869 NOTE: the strings in the include directory and file name tables of
14870 the returned object point into the dwarf line section buffer,
14871 and must not be freed. */
14872
14873 static struct line_header *
14874 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14875 {
14876 struct cleanup *back_to;
14877 struct line_header *lh;
14878 gdb_byte *line_ptr;
14879 unsigned int bytes_read, offset_size;
14880 int i;
14881 char *cur_dir, *cur_file;
14882 struct dwarf2_section_info *section;
14883 bfd *abfd;
14884
14885 section = get_debug_line_section (cu);
14886 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14887 if (section->buffer == NULL)
14888 {
14889 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14890 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14891 else
14892 complaint (&symfile_complaints, _("missing .debug_line section"));
14893 return 0;
14894 }
14895
14896 /* We can't do this until we know the section is non-empty.
14897 Only then do we know we have such a section. */
14898 abfd = section->asection->owner;
14899
14900 /* Make sure that at least there's room for the total_length field.
14901 That could be 12 bytes long, but we're just going to fudge that. */
14902 if (offset + 4 >= section->size)
14903 {
14904 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14905 return 0;
14906 }
14907
14908 lh = xmalloc (sizeof (*lh));
14909 memset (lh, 0, sizeof (*lh));
14910 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14911 (void *) lh);
14912
14913 line_ptr = section->buffer + offset;
14914
14915 /* Read in the header. */
14916 lh->total_length =
14917 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14918 &bytes_read, &offset_size);
14919 line_ptr += bytes_read;
14920 if (line_ptr + lh->total_length > (section->buffer + section->size))
14921 {
14922 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14923 return 0;
14924 }
14925 lh->statement_program_end = line_ptr + lh->total_length;
14926 lh->version = read_2_bytes (abfd, line_ptr);
14927 line_ptr += 2;
14928 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14929 line_ptr += offset_size;
14930 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14931 line_ptr += 1;
14932 if (lh->version >= 4)
14933 {
14934 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14935 line_ptr += 1;
14936 }
14937 else
14938 lh->maximum_ops_per_instruction = 1;
14939
14940 if (lh->maximum_ops_per_instruction == 0)
14941 {
14942 lh->maximum_ops_per_instruction = 1;
14943 complaint (&symfile_complaints,
14944 _("invalid maximum_ops_per_instruction "
14945 "in `.debug_line' section"));
14946 }
14947
14948 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14949 line_ptr += 1;
14950 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14951 line_ptr += 1;
14952 lh->line_range = read_1_byte (abfd, line_ptr);
14953 line_ptr += 1;
14954 lh->opcode_base = read_1_byte (abfd, line_ptr);
14955 line_ptr += 1;
14956 lh->standard_opcode_lengths
14957 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14958
14959 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14960 for (i = 1; i < lh->opcode_base; ++i)
14961 {
14962 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14963 line_ptr += 1;
14964 }
14965
14966 /* Read directory table. */
14967 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14968 {
14969 line_ptr += bytes_read;
14970 add_include_dir (lh, cur_dir);
14971 }
14972 line_ptr += bytes_read;
14973
14974 /* Read file name table. */
14975 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14976 {
14977 unsigned int dir_index, mod_time, length;
14978
14979 line_ptr += bytes_read;
14980 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14981 line_ptr += bytes_read;
14982 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14983 line_ptr += bytes_read;
14984 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14985 line_ptr += bytes_read;
14986
14987 add_file_name (lh, cur_file, dir_index, mod_time, length);
14988 }
14989 line_ptr += bytes_read;
14990 lh->statement_program_start = line_ptr;
14991
14992 if (line_ptr > (section->buffer + section->size))
14993 complaint (&symfile_complaints,
14994 _("line number info header doesn't "
14995 "fit in `.debug_line' section"));
14996
14997 discard_cleanups (back_to);
14998 return lh;
14999 }
15000
15001 /* Subroutine of dwarf_decode_lines to simplify it.
15002 Return the file name of the psymtab for included file FILE_INDEX
15003 in line header LH of PST.
15004 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15005 If space for the result is malloc'd, it will be freed by a cleanup.
15006 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15007
15008 static char *
15009 psymtab_include_file_name (const struct line_header *lh, int file_index,
15010 const struct partial_symtab *pst,
15011 const char *comp_dir)
15012 {
15013 const struct file_entry fe = lh->file_names [file_index];
15014 char *include_name = fe.name;
15015 char *include_name_to_compare = include_name;
15016 char *dir_name = NULL;
15017 const char *pst_filename;
15018 char *copied_name = NULL;
15019 int file_is_pst;
15020
15021 if (fe.dir_index)
15022 dir_name = lh->include_dirs[fe.dir_index - 1];
15023
15024 if (!IS_ABSOLUTE_PATH (include_name)
15025 && (dir_name != NULL || comp_dir != NULL))
15026 {
15027 /* Avoid creating a duplicate psymtab for PST.
15028 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15029 Before we do the comparison, however, we need to account
15030 for DIR_NAME and COMP_DIR.
15031 First prepend dir_name (if non-NULL). If we still don't
15032 have an absolute path prepend comp_dir (if non-NULL).
15033 However, the directory we record in the include-file's
15034 psymtab does not contain COMP_DIR (to match the
15035 corresponding symtab(s)).
15036
15037 Example:
15038
15039 bash$ cd /tmp
15040 bash$ gcc -g ./hello.c
15041 include_name = "hello.c"
15042 dir_name = "."
15043 DW_AT_comp_dir = comp_dir = "/tmp"
15044 DW_AT_name = "./hello.c" */
15045
15046 if (dir_name != NULL)
15047 {
15048 include_name = concat (dir_name, SLASH_STRING,
15049 include_name, (char *)NULL);
15050 include_name_to_compare = include_name;
15051 make_cleanup (xfree, include_name);
15052 }
15053 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15054 {
15055 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15056 include_name, (char *)NULL);
15057 }
15058 }
15059
15060 pst_filename = pst->filename;
15061 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15062 {
15063 copied_name = concat (pst->dirname, SLASH_STRING,
15064 pst_filename, (char *)NULL);
15065 pst_filename = copied_name;
15066 }
15067
15068 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15069
15070 if (include_name_to_compare != include_name)
15071 xfree (include_name_to_compare);
15072 if (copied_name != NULL)
15073 xfree (copied_name);
15074
15075 if (file_is_pst)
15076 return NULL;
15077 return include_name;
15078 }
15079
15080 /* Ignore this record_line request. */
15081
15082 static void
15083 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15084 {
15085 return;
15086 }
15087
15088 /* Subroutine of dwarf_decode_lines to simplify it.
15089 Process the line number information in LH. */
15090
15091 static void
15092 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15093 struct dwarf2_cu *cu, struct partial_symtab *pst)
15094 {
15095 gdb_byte *line_ptr, *extended_end;
15096 gdb_byte *line_end;
15097 unsigned int bytes_read, extended_len;
15098 unsigned char op_code, extended_op, adj_opcode;
15099 CORE_ADDR baseaddr;
15100 struct objfile *objfile = cu->objfile;
15101 bfd *abfd = objfile->obfd;
15102 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15103 const int decode_for_pst_p = (pst != NULL);
15104 struct subfile *last_subfile = NULL;
15105 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15106 = record_line;
15107
15108 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15109
15110 line_ptr = lh->statement_program_start;
15111 line_end = lh->statement_program_end;
15112
15113 /* Read the statement sequences until there's nothing left. */
15114 while (line_ptr < line_end)
15115 {
15116 /* state machine registers */
15117 CORE_ADDR address = 0;
15118 unsigned int file = 1;
15119 unsigned int line = 1;
15120 unsigned int column = 0;
15121 int is_stmt = lh->default_is_stmt;
15122 int basic_block = 0;
15123 int end_sequence = 0;
15124 CORE_ADDR addr;
15125 unsigned char op_index = 0;
15126
15127 if (!decode_for_pst_p && lh->num_file_names >= file)
15128 {
15129 /* Start a subfile for the current file of the state machine. */
15130 /* lh->include_dirs and lh->file_names are 0-based, but the
15131 directory and file name numbers in the statement program
15132 are 1-based. */
15133 struct file_entry *fe = &lh->file_names[file - 1];
15134 char *dir = NULL;
15135
15136 if (fe->dir_index)
15137 dir = lh->include_dirs[fe->dir_index - 1];
15138
15139 dwarf2_start_subfile (fe->name, dir, comp_dir);
15140 }
15141
15142 /* Decode the table. */
15143 while (!end_sequence)
15144 {
15145 op_code = read_1_byte (abfd, line_ptr);
15146 line_ptr += 1;
15147 if (line_ptr > line_end)
15148 {
15149 dwarf2_debug_line_missing_end_sequence_complaint ();
15150 break;
15151 }
15152
15153 if (op_code >= lh->opcode_base)
15154 {
15155 /* Special operand. */
15156 adj_opcode = op_code - lh->opcode_base;
15157 address += (((op_index + (adj_opcode / lh->line_range))
15158 / lh->maximum_ops_per_instruction)
15159 * lh->minimum_instruction_length);
15160 op_index = ((op_index + (adj_opcode / lh->line_range))
15161 % lh->maximum_ops_per_instruction);
15162 line += lh->line_base + (adj_opcode % lh->line_range);
15163 if (lh->num_file_names < file || file == 0)
15164 dwarf2_debug_line_missing_file_complaint ();
15165 /* For now we ignore lines not starting on an
15166 instruction boundary. */
15167 else if (op_index == 0)
15168 {
15169 lh->file_names[file - 1].included_p = 1;
15170 if (!decode_for_pst_p && is_stmt)
15171 {
15172 if (last_subfile != current_subfile)
15173 {
15174 addr = gdbarch_addr_bits_remove (gdbarch, address);
15175 if (last_subfile)
15176 (*p_record_line) (last_subfile, 0, addr);
15177 last_subfile = current_subfile;
15178 }
15179 /* Append row to matrix using current values. */
15180 addr = gdbarch_addr_bits_remove (gdbarch, address);
15181 (*p_record_line) (current_subfile, line, addr);
15182 }
15183 }
15184 basic_block = 0;
15185 }
15186 else switch (op_code)
15187 {
15188 case DW_LNS_extended_op:
15189 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15190 &bytes_read);
15191 line_ptr += bytes_read;
15192 extended_end = line_ptr + extended_len;
15193 extended_op = read_1_byte (abfd, line_ptr);
15194 line_ptr += 1;
15195 switch (extended_op)
15196 {
15197 case DW_LNE_end_sequence:
15198 p_record_line = record_line;
15199 end_sequence = 1;
15200 break;
15201 case DW_LNE_set_address:
15202 address = read_address (abfd, line_ptr, cu, &bytes_read);
15203
15204 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15205 {
15206 /* This line table is for a function which has been
15207 GCd by the linker. Ignore it. PR gdb/12528 */
15208
15209 long line_offset
15210 = line_ptr - get_debug_line_section (cu)->buffer;
15211
15212 complaint (&symfile_complaints,
15213 _(".debug_line address at offset 0x%lx is 0 "
15214 "[in module %s]"),
15215 line_offset, objfile->name);
15216 p_record_line = noop_record_line;
15217 }
15218
15219 op_index = 0;
15220 line_ptr += bytes_read;
15221 address += baseaddr;
15222 break;
15223 case DW_LNE_define_file:
15224 {
15225 char *cur_file;
15226 unsigned int dir_index, mod_time, length;
15227
15228 cur_file = read_direct_string (abfd, line_ptr,
15229 &bytes_read);
15230 line_ptr += bytes_read;
15231 dir_index =
15232 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15233 line_ptr += bytes_read;
15234 mod_time =
15235 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15236 line_ptr += bytes_read;
15237 length =
15238 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15239 line_ptr += bytes_read;
15240 add_file_name (lh, cur_file, dir_index, mod_time, length);
15241 }
15242 break;
15243 case DW_LNE_set_discriminator:
15244 /* The discriminator is not interesting to the debugger;
15245 just ignore it. */
15246 line_ptr = extended_end;
15247 break;
15248 default:
15249 complaint (&symfile_complaints,
15250 _("mangled .debug_line section"));
15251 return;
15252 }
15253 /* Make sure that we parsed the extended op correctly. If e.g.
15254 we expected a different address size than the producer used,
15255 we may have read the wrong number of bytes. */
15256 if (line_ptr != extended_end)
15257 {
15258 complaint (&symfile_complaints,
15259 _("mangled .debug_line section"));
15260 return;
15261 }
15262 break;
15263 case DW_LNS_copy:
15264 if (lh->num_file_names < file || file == 0)
15265 dwarf2_debug_line_missing_file_complaint ();
15266 else
15267 {
15268 lh->file_names[file - 1].included_p = 1;
15269 if (!decode_for_pst_p && is_stmt)
15270 {
15271 if (last_subfile != current_subfile)
15272 {
15273 addr = gdbarch_addr_bits_remove (gdbarch, address);
15274 if (last_subfile)
15275 (*p_record_line) (last_subfile, 0, addr);
15276 last_subfile = current_subfile;
15277 }
15278 addr = gdbarch_addr_bits_remove (gdbarch, address);
15279 (*p_record_line) (current_subfile, line, addr);
15280 }
15281 }
15282 basic_block = 0;
15283 break;
15284 case DW_LNS_advance_pc:
15285 {
15286 CORE_ADDR adjust
15287 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15288
15289 address += (((op_index + adjust)
15290 / lh->maximum_ops_per_instruction)
15291 * lh->minimum_instruction_length);
15292 op_index = ((op_index + adjust)
15293 % lh->maximum_ops_per_instruction);
15294 line_ptr += bytes_read;
15295 }
15296 break;
15297 case DW_LNS_advance_line:
15298 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15299 line_ptr += bytes_read;
15300 break;
15301 case DW_LNS_set_file:
15302 {
15303 /* The arrays lh->include_dirs and lh->file_names are
15304 0-based, but the directory and file name numbers in
15305 the statement program are 1-based. */
15306 struct file_entry *fe;
15307 char *dir = NULL;
15308
15309 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15310 line_ptr += bytes_read;
15311 if (lh->num_file_names < file || file == 0)
15312 dwarf2_debug_line_missing_file_complaint ();
15313 else
15314 {
15315 fe = &lh->file_names[file - 1];
15316 if (fe->dir_index)
15317 dir = lh->include_dirs[fe->dir_index - 1];
15318 if (!decode_for_pst_p)
15319 {
15320 last_subfile = current_subfile;
15321 dwarf2_start_subfile (fe->name, dir, comp_dir);
15322 }
15323 }
15324 }
15325 break;
15326 case DW_LNS_set_column:
15327 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15328 line_ptr += bytes_read;
15329 break;
15330 case DW_LNS_negate_stmt:
15331 is_stmt = (!is_stmt);
15332 break;
15333 case DW_LNS_set_basic_block:
15334 basic_block = 1;
15335 break;
15336 /* Add to the address register of the state machine the
15337 address increment value corresponding to special opcode
15338 255. I.e., this value is scaled by the minimum
15339 instruction length since special opcode 255 would have
15340 scaled the increment. */
15341 case DW_LNS_const_add_pc:
15342 {
15343 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15344
15345 address += (((op_index + adjust)
15346 / lh->maximum_ops_per_instruction)
15347 * lh->minimum_instruction_length);
15348 op_index = ((op_index + adjust)
15349 % lh->maximum_ops_per_instruction);
15350 }
15351 break;
15352 case DW_LNS_fixed_advance_pc:
15353 address += read_2_bytes (abfd, line_ptr);
15354 op_index = 0;
15355 line_ptr += 2;
15356 break;
15357 default:
15358 {
15359 /* Unknown standard opcode, ignore it. */
15360 int i;
15361
15362 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15363 {
15364 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15365 line_ptr += bytes_read;
15366 }
15367 }
15368 }
15369 }
15370 if (lh->num_file_names < file || file == 0)
15371 dwarf2_debug_line_missing_file_complaint ();
15372 else
15373 {
15374 lh->file_names[file - 1].included_p = 1;
15375 if (!decode_for_pst_p)
15376 {
15377 addr = gdbarch_addr_bits_remove (gdbarch, address);
15378 (*p_record_line) (current_subfile, 0, addr);
15379 }
15380 }
15381 }
15382 }
15383
15384 /* Decode the Line Number Program (LNP) for the given line_header
15385 structure and CU. The actual information extracted and the type
15386 of structures created from the LNP depends on the value of PST.
15387
15388 1. If PST is NULL, then this procedure uses the data from the program
15389 to create all necessary symbol tables, and their linetables.
15390
15391 2. If PST is not NULL, this procedure reads the program to determine
15392 the list of files included by the unit represented by PST, and
15393 builds all the associated partial symbol tables.
15394
15395 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15396 It is used for relative paths in the line table.
15397 NOTE: When processing partial symtabs (pst != NULL),
15398 comp_dir == pst->dirname.
15399
15400 NOTE: It is important that psymtabs have the same file name (via strcmp)
15401 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15402 symtab we don't use it in the name of the psymtabs we create.
15403 E.g. expand_line_sal requires this when finding psymtabs to expand.
15404 A good testcase for this is mb-inline.exp. */
15405
15406 static void
15407 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15408 struct dwarf2_cu *cu, struct partial_symtab *pst,
15409 int want_line_info)
15410 {
15411 struct objfile *objfile = cu->objfile;
15412 const int decode_for_pst_p = (pst != NULL);
15413 struct subfile *first_subfile = current_subfile;
15414
15415 if (want_line_info)
15416 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15417
15418 if (decode_for_pst_p)
15419 {
15420 int file_index;
15421
15422 /* Now that we're done scanning the Line Header Program, we can
15423 create the psymtab of each included file. */
15424 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15425 if (lh->file_names[file_index].included_p == 1)
15426 {
15427 char *include_name =
15428 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15429 if (include_name != NULL)
15430 dwarf2_create_include_psymtab (include_name, pst, objfile);
15431 }
15432 }
15433 else
15434 {
15435 /* Make sure a symtab is created for every file, even files
15436 which contain only variables (i.e. no code with associated
15437 line numbers). */
15438 int i;
15439
15440 for (i = 0; i < lh->num_file_names; i++)
15441 {
15442 char *dir = NULL;
15443 struct file_entry *fe;
15444
15445 fe = &lh->file_names[i];
15446 if (fe->dir_index)
15447 dir = lh->include_dirs[fe->dir_index - 1];
15448 dwarf2_start_subfile (fe->name, dir, comp_dir);
15449
15450 /* Skip the main file; we don't need it, and it must be
15451 allocated last, so that it will show up before the
15452 non-primary symtabs in the objfile's symtab list. */
15453 if (current_subfile == first_subfile)
15454 continue;
15455
15456 if (current_subfile->symtab == NULL)
15457 current_subfile->symtab = allocate_symtab (current_subfile->name,
15458 objfile);
15459 fe->symtab = current_subfile->symtab;
15460 }
15461 }
15462 }
15463
15464 /* Start a subfile for DWARF. FILENAME is the name of the file and
15465 DIRNAME the name of the source directory which contains FILENAME
15466 or NULL if not known. COMP_DIR is the compilation directory for the
15467 linetable's compilation unit or NULL if not known.
15468 This routine tries to keep line numbers from identical absolute and
15469 relative file names in a common subfile.
15470
15471 Using the `list' example from the GDB testsuite, which resides in
15472 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15473 of /srcdir/list0.c yields the following debugging information for list0.c:
15474
15475 DW_AT_name: /srcdir/list0.c
15476 DW_AT_comp_dir: /compdir
15477 files.files[0].name: list0.h
15478 files.files[0].dir: /srcdir
15479 files.files[1].name: list0.c
15480 files.files[1].dir: /srcdir
15481
15482 The line number information for list0.c has to end up in a single
15483 subfile, so that `break /srcdir/list0.c:1' works as expected.
15484 start_subfile will ensure that this happens provided that we pass the
15485 concatenation of files.files[1].dir and files.files[1].name as the
15486 subfile's name. */
15487
15488 static void
15489 dwarf2_start_subfile (char *filename, const char *dirname,
15490 const char *comp_dir)
15491 {
15492 char *fullname;
15493
15494 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15495 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15496 second argument to start_subfile. To be consistent, we do the
15497 same here. In order not to lose the line information directory,
15498 we concatenate it to the filename when it makes sense.
15499 Note that the Dwarf3 standard says (speaking of filenames in line
15500 information): ``The directory index is ignored for file names
15501 that represent full path names''. Thus ignoring dirname in the
15502 `else' branch below isn't an issue. */
15503
15504 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15505 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15506 else
15507 fullname = filename;
15508
15509 start_subfile (fullname, comp_dir);
15510
15511 if (fullname != filename)
15512 xfree (fullname);
15513 }
15514
15515 /* Start a symtab for DWARF.
15516 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15517
15518 static void
15519 dwarf2_start_symtab (struct dwarf2_cu *cu,
15520 char *name, char *comp_dir, CORE_ADDR low_pc)
15521 {
15522 start_symtab (name, comp_dir, low_pc);
15523 record_debugformat ("DWARF 2");
15524 record_producer (cu->producer);
15525
15526 /* We assume that we're processing GCC output. */
15527 processing_gcc_compilation = 2;
15528
15529 processing_has_namespace_info = 0;
15530 }
15531
15532 static void
15533 var_decode_location (struct attribute *attr, struct symbol *sym,
15534 struct dwarf2_cu *cu)
15535 {
15536 struct objfile *objfile = cu->objfile;
15537 struct comp_unit_head *cu_header = &cu->header;
15538
15539 /* NOTE drow/2003-01-30: There used to be a comment and some special
15540 code here to turn a symbol with DW_AT_external and a
15541 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15542 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15543 with some versions of binutils) where shared libraries could have
15544 relocations against symbols in their debug information - the
15545 minimal symbol would have the right address, but the debug info
15546 would not. It's no longer necessary, because we will explicitly
15547 apply relocations when we read in the debug information now. */
15548
15549 /* A DW_AT_location attribute with no contents indicates that a
15550 variable has been optimized away. */
15551 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15552 {
15553 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15554 return;
15555 }
15556
15557 /* Handle one degenerate form of location expression specially, to
15558 preserve GDB's previous behavior when section offsets are
15559 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15560 then mark this symbol as LOC_STATIC. */
15561
15562 if (attr_form_is_block (attr)
15563 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15564 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15565 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15566 && (DW_BLOCK (attr)->size
15567 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15568 {
15569 unsigned int dummy;
15570
15571 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15572 SYMBOL_VALUE_ADDRESS (sym) =
15573 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15574 else
15575 SYMBOL_VALUE_ADDRESS (sym) =
15576 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15577 SYMBOL_CLASS (sym) = LOC_STATIC;
15578 fixup_symbol_section (sym, objfile);
15579 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15580 SYMBOL_SECTION (sym));
15581 return;
15582 }
15583
15584 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15585 expression evaluator, and use LOC_COMPUTED only when necessary
15586 (i.e. when the value of a register or memory location is
15587 referenced, or a thread-local block, etc.). Then again, it might
15588 not be worthwhile. I'm assuming that it isn't unless performance
15589 or memory numbers show me otherwise. */
15590
15591 dwarf2_symbol_mark_computed (attr, sym, cu);
15592 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15593
15594 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15595 cu->has_loclist = 1;
15596 }
15597
15598 /* Given a pointer to a DWARF information entry, figure out if we need
15599 to make a symbol table entry for it, and if so, create a new entry
15600 and return a pointer to it.
15601 If TYPE is NULL, determine symbol type from the die, otherwise
15602 used the passed type.
15603 If SPACE is not NULL, use it to hold the new symbol. If it is
15604 NULL, allocate a new symbol on the objfile's obstack. */
15605
15606 static struct symbol *
15607 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15608 struct symbol *space)
15609 {
15610 struct objfile *objfile = cu->objfile;
15611 struct symbol *sym = NULL;
15612 char *name;
15613 struct attribute *attr = NULL;
15614 struct attribute *attr2 = NULL;
15615 CORE_ADDR baseaddr;
15616 struct pending **list_to_add = NULL;
15617
15618 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15619
15620 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15621
15622 name = dwarf2_name (die, cu);
15623 if (name)
15624 {
15625 const char *linkagename;
15626 int suppress_add = 0;
15627
15628 if (space)
15629 sym = space;
15630 else
15631 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15632 OBJSTAT (objfile, n_syms++);
15633
15634 /* Cache this symbol's name and the name's demangled form (if any). */
15635 SYMBOL_SET_LANGUAGE (sym, cu->language);
15636 linkagename = dwarf2_physname (name, die, cu);
15637 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15638
15639 /* Fortran does not have mangling standard and the mangling does differ
15640 between gfortran, iFort etc. */
15641 if (cu->language == language_fortran
15642 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15643 symbol_set_demangled_name (&(sym->ginfo),
15644 (char *) dwarf2_full_name (name, die, cu),
15645 NULL);
15646
15647 /* Default assumptions.
15648 Use the passed type or decode it from the die. */
15649 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15650 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15651 if (type != NULL)
15652 SYMBOL_TYPE (sym) = type;
15653 else
15654 SYMBOL_TYPE (sym) = die_type (die, cu);
15655 attr = dwarf2_attr (die,
15656 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15657 cu);
15658 if (attr)
15659 {
15660 SYMBOL_LINE (sym) = DW_UNSND (attr);
15661 }
15662
15663 attr = dwarf2_attr (die,
15664 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15665 cu);
15666 if (attr)
15667 {
15668 int file_index = DW_UNSND (attr);
15669
15670 if (cu->line_header == NULL
15671 || file_index > cu->line_header->num_file_names)
15672 complaint (&symfile_complaints,
15673 _("file index out of range"));
15674 else if (file_index > 0)
15675 {
15676 struct file_entry *fe;
15677
15678 fe = &cu->line_header->file_names[file_index - 1];
15679 SYMBOL_SYMTAB (sym) = fe->symtab;
15680 }
15681 }
15682
15683 switch (die->tag)
15684 {
15685 case DW_TAG_label:
15686 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15687 if (attr)
15688 {
15689 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15690 }
15691 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15692 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15693 SYMBOL_CLASS (sym) = LOC_LABEL;
15694 add_symbol_to_list (sym, cu->list_in_scope);
15695 break;
15696 case DW_TAG_subprogram:
15697 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15698 finish_block. */
15699 SYMBOL_CLASS (sym) = LOC_BLOCK;
15700 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15701 if ((attr2 && (DW_UNSND (attr2) != 0))
15702 || cu->language == language_ada)
15703 {
15704 /* Subprograms marked external are stored as a global symbol.
15705 Ada subprograms, whether marked external or not, are always
15706 stored as a global symbol, because we want to be able to
15707 access them globally. For instance, we want to be able
15708 to break on a nested subprogram without having to
15709 specify the context. */
15710 list_to_add = &global_symbols;
15711 }
15712 else
15713 {
15714 list_to_add = cu->list_in_scope;
15715 }
15716 break;
15717 case DW_TAG_inlined_subroutine:
15718 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15719 finish_block. */
15720 SYMBOL_CLASS (sym) = LOC_BLOCK;
15721 SYMBOL_INLINED (sym) = 1;
15722 list_to_add = cu->list_in_scope;
15723 break;
15724 case DW_TAG_template_value_param:
15725 suppress_add = 1;
15726 /* Fall through. */
15727 case DW_TAG_constant:
15728 case DW_TAG_variable:
15729 case DW_TAG_member:
15730 /* Compilation with minimal debug info may result in
15731 variables with missing type entries. Change the
15732 misleading `void' type to something sensible. */
15733 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15734 SYMBOL_TYPE (sym)
15735 = objfile_type (objfile)->nodebug_data_symbol;
15736
15737 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15738 /* In the case of DW_TAG_member, we should only be called for
15739 static const members. */
15740 if (die->tag == DW_TAG_member)
15741 {
15742 /* dwarf2_add_field uses die_is_declaration,
15743 so we do the same. */
15744 gdb_assert (die_is_declaration (die, cu));
15745 gdb_assert (attr);
15746 }
15747 if (attr)
15748 {
15749 dwarf2_const_value (attr, sym, cu);
15750 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15751 if (!suppress_add)
15752 {
15753 if (attr2 && (DW_UNSND (attr2) != 0))
15754 list_to_add = &global_symbols;
15755 else
15756 list_to_add = cu->list_in_scope;
15757 }
15758 break;
15759 }
15760 attr = dwarf2_attr (die, DW_AT_location, cu);
15761 if (attr)
15762 {
15763 var_decode_location (attr, sym, cu);
15764 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15765
15766 /* Fortran explicitly imports any global symbols to the local
15767 scope by DW_TAG_common_block. */
15768 if (cu->language == language_fortran && die->parent
15769 && die->parent->tag == DW_TAG_common_block)
15770 attr2 = NULL;
15771
15772 if (SYMBOL_CLASS (sym) == LOC_STATIC
15773 && SYMBOL_VALUE_ADDRESS (sym) == 0
15774 && !dwarf2_per_objfile->has_section_at_zero)
15775 {
15776 /* When a static variable is eliminated by the linker,
15777 the corresponding debug information is not stripped
15778 out, but the variable address is set to null;
15779 do not add such variables into symbol table. */
15780 }
15781 else if (attr2 && (DW_UNSND (attr2) != 0))
15782 {
15783 /* Workaround gfortran PR debug/40040 - it uses
15784 DW_AT_location for variables in -fPIC libraries which may
15785 get overriden by other libraries/executable and get
15786 a different address. Resolve it by the minimal symbol
15787 which may come from inferior's executable using copy
15788 relocation. Make this workaround only for gfortran as for
15789 other compilers GDB cannot guess the minimal symbol
15790 Fortran mangling kind. */
15791 if (cu->language == language_fortran && die->parent
15792 && die->parent->tag == DW_TAG_module
15793 && cu->producer
15794 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15795 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15796
15797 /* A variable with DW_AT_external is never static,
15798 but it may be block-scoped. */
15799 list_to_add = (cu->list_in_scope == &file_symbols
15800 ? &global_symbols : cu->list_in_scope);
15801 }
15802 else
15803 list_to_add = cu->list_in_scope;
15804 }
15805 else
15806 {
15807 /* We do not know the address of this symbol.
15808 If it is an external symbol and we have type information
15809 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15810 The address of the variable will then be determined from
15811 the minimal symbol table whenever the variable is
15812 referenced. */
15813 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15814
15815 /* Fortran explicitly imports any global symbols to the local
15816 scope by DW_TAG_common_block. */
15817 if (cu->language == language_fortran && die->parent
15818 && die->parent->tag == DW_TAG_common_block)
15819 {
15820 /* SYMBOL_CLASS doesn't matter here because
15821 read_common_block is going to reset it. */
15822 if (!suppress_add)
15823 list_to_add = cu->list_in_scope;
15824 }
15825 else if (attr2 && (DW_UNSND (attr2) != 0)
15826 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15827 {
15828 /* A variable with DW_AT_external is never static, but it
15829 may be block-scoped. */
15830 list_to_add = (cu->list_in_scope == &file_symbols
15831 ? &global_symbols : cu->list_in_scope);
15832
15833 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15834 }
15835 else if (!die_is_declaration (die, cu))
15836 {
15837 /* Use the default LOC_OPTIMIZED_OUT class. */
15838 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15839 if (!suppress_add)
15840 list_to_add = cu->list_in_scope;
15841 }
15842 }
15843 break;
15844 case DW_TAG_formal_parameter:
15845 /* If we are inside a function, mark this as an argument. If
15846 not, we might be looking at an argument to an inlined function
15847 when we do not have enough information to show inlined frames;
15848 pretend it's a local variable in that case so that the user can
15849 still see it. */
15850 if (context_stack_depth > 0
15851 && context_stack[context_stack_depth - 1].name != NULL)
15852 SYMBOL_IS_ARGUMENT (sym) = 1;
15853 attr = dwarf2_attr (die, DW_AT_location, cu);
15854 if (attr)
15855 {
15856 var_decode_location (attr, sym, cu);
15857 }
15858 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15859 if (attr)
15860 {
15861 dwarf2_const_value (attr, sym, cu);
15862 }
15863
15864 list_to_add = cu->list_in_scope;
15865 break;
15866 case DW_TAG_unspecified_parameters:
15867 /* From varargs functions; gdb doesn't seem to have any
15868 interest in this information, so just ignore it for now.
15869 (FIXME?) */
15870 break;
15871 case DW_TAG_template_type_param:
15872 suppress_add = 1;
15873 /* Fall through. */
15874 case DW_TAG_class_type:
15875 case DW_TAG_interface_type:
15876 case DW_TAG_structure_type:
15877 case DW_TAG_union_type:
15878 case DW_TAG_set_type:
15879 case DW_TAG_enumeration_type:
15880 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15881 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15882
15883 {
15884 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15885 really ever be static objects: otherwise, if you try
15886 to, say, break of a class's method and you're in a file
15887 which doesn't mention that class, it won't work unless
15888 the check for all static symbols in lookup_symbol_aux
15889 saves you. See the OtherFileClass tests in
15890 gdb.c++/namespace.exp. */
15891
15892 if (!suppress_add)
15893 {
15894 list_to_add = (cu->list_in_scope == &file_symbols
15895 && (cu->language == language_cplus
15896 || cu->language == language_java)
15897 ? &global_symbols : cu->list_in_scope);
15898
15899 /* The semantics of C++ state that "struct foo {
15900 ... }" also defines a typedef for "foo". A Java
15901 class declaration also defines a typedef for the
15902 class. */
15903 if (cu->language == language_cplus
15904 || cu->language == language_java
15905 || cu->language == language_ada)
15906 {
15907 /* The symbol's name is already allocated along
15908 with this objfile, so we don't need to
15909 duplicate it for the type. */
15910 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15911 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15912 }
15913 }
15914 }
15915 break;
15916 case DW_TAG_typedef:
15917 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15918 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15919 list_to_add = cu->list_in_scope;
15920 break;
15921 case DW_TAG_base_type:
15922 case DW_TAG_subrange_type:
15923 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15924 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15925 list_to_add = cu->list_in_scope;
15926 break;
15927 case DW_TAG_enumerator:
15928 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15929 if (attr)
15930 {
15931 dwarf2_const_value (attr, sym, cu);
15932 }
15933 {
15934 /* NOTE: carlton/2003-11-10: See comment above in the
15935 DW_TAG_class_type, etc. block. */
15936
15937 list_to_add = (cu->list_in_scope == &file_symbols
15938 && (cu->language == language_cplus
15939 || cu->language == language_java)
15940 ? &global_symbols : cu->list_in_scope);
15941 }
15942 break;
15943 case DW_TAG_namespace:
15944 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15945 list_to_add = &global_symbols;
15946 break;
15947 case DW_TAG_common_block:
15948 SYMBOL_CLASS (sym) = LOC_STATIC;
15949 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15950 add_symbol_to_list (sym, cu->list_in_scope);
15951 break;
15952 default:
15953 /* Not a tag we recognize. Hopefully we aren't processing
15954 trash data, but since we must specifically ignore things
15955 we don't recognize, there is nothing else we should do at
15956 this point. */
15957 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15958 dwarf_tag_name (die->tag));
15959 break;
15960 }
15961
15962 if (suppress_add)
15963 {
15964 sym->hash_next = objfile->template_symbols;
15965 objfile->template_symbols = sym;
15966 list_to_add = NULL;
15967 }
15968
15969 if (list_to_add != NULL)
15970 add_symbol_to_list (sym, list_to_add);
15971
15972 /* For the benefit of old versions of GCC, check for anonymous
15973 namespaces based on the demangled name. */
15974 if (!processing_has_namespace_info
15975 && cu->language == language_cplus)
15976 cp_scan_for_anonymous_namespaces (sym, objfile);
15977 }
15978 return (sym);
15979 }
15980
15981 /* A wrapper for new_symbol_full that always allocates a new symbol. */
15982
15983 static struct symbol *
15984 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15985 {
15986 return new_symbol_full (die, type, cu, NULL);
15987 }
15988
15989 /* Given an attr with a DW_FORM_dataN value in host byte order,
15990 zero-extend it as appropriate for the symbol's type. The DWARF
15991 standard (v4) is not entirely clear about the meaning of using
15992 DW_FORM_dataN for a constant with a signed type, where the type is
15993 wider than the data. The conclusion of a discussion on the DWARF
15994 list was that this is unspecified. We choose to always zero-extend
15995 because that is the interpretation long in use by GCC. */
15996
15997 static gdb_byte *
15998 dwarf2_const_value_data (struct attribute *attr, struct type *type,
15999 const char *name, struct obstack *obstack,
16000 struct dwarf2_cu *cu, LONGEST *value, int bits)
16001 {
16002 struct objfile *objfile = cu->objfile;
16003 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16004 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16005 LONGEST l = DW_UNSND (attr);
16006
16007 if (bits < sizeof (*value) * 8)
16008 {
16009 l &= ((LONGEST) 1 << bits) - 1;
16010 *value = l;
16011 }
16012 else if (bits == sizeof (*value) * 8)
16013 *value = l;
16014 else
16015 {
16016 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16017 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16018 return bytes;
16019 }
16020
16021 return NULL;
16022 }
16023
16024 /* Read a constant value from an attribute. Either set *VALUE, or if
16025 the value does not fit in *VALUE, set *BYTES - either already
16026 allocated on the objfile obstack, or newly allocated on OBSTACK,
16027 or, set *BATON, if we translated the constant to a location
16028 expression. */
16029
16030 static void
16031 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16032 const char *name, struct obstack *obstack,
16033 struct dwarf2_cu *cu,
16034 LONGEST *value, gdb_byte **bytes,
16035 struct dwarf2_locexpr_baton **baton)
16036 {
16037 struct objfile *objfile = cu->objfile;
16038 struct comp_unit_head *cu_header = &cu->header;
16039 struct dwarf_block *blk;
16040 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16041 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16042
16043 *value = 0;
16044 *bytes = NULL;
16045 *baton = NULL;
16046
16047 switch (attr->form)
16048 {
16049 case DW_FORM_addr:
16050 case DW_FORM_GNU_addr_index:
16051 {
16052 gdb_byte *data;
16053
16054 if (TYPE_LENGTH (type) != cu_header->addr_size)
16055 dwarf2_const_value_length_mismatch_complaint (name,
16056 cu_header->addr_size,
16057 TYPE_LENGTH (type));
16058 /* Symbols of this form are reasonably rare, so we just
16059 piggyback on the existing location code rather than writing
16060 a new implementation of symbol_computed_ops. */
16061 *baton = obstack_alloc (&objfile->objfile_obstack,
16062 sizeof (struct dwarf2_locexpr_baton));
16063 (*baton)->per_cu = cu->per_cu;
16064 gdb_assert ((*baton)->per_cu);
16065
16066 (*baton)->size = 2 + cu_header->addr_size;
16067 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16068 (*baton)->data = data;
16069
16070 data[0] = DW_OP_addr;
16071 store_unsigned_integer (&data[1], cu_header->addr_size,
16072 byte_order, DW_ADDR (attr));
16073 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16074 }
16075 break;
16076 case DW_FORM_string:
16077 case DW_FORM_strp:
16078 case DW_FORM_GNU_str_index:
16079 case DW_FORM_GNU_strp_alt:
16080 /* DW_STRING is already allocated on the objfile obstack, point
16081 directly to it. */
16082 *bytes = (gdb_byte *) DW_STRING (attr);
16083 break;
16084 case DW_FORM_block1:
16085 case DW_FORM_block2:
16086 case DW_FORM_block4:
16087 case DW_FORM_block:
16088 case DW_FORM_exprloc:
16089 blk = DW_BLOCK (attr);
16090 if (TYPE_LENGTH (type) != blk->size)
16091 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16092 TYPE_LENGTH (type));
16093 *bytes = blk->data;
16094 break;
16095
16096 /* The DW_AT_const_value attributes are supposed to carry the
16097 symbol's value "represented as it would be on the target
16098 architecture." By the time we get here, it's already been
16099 converted to host endianness, so we just need to sign- or
16100 zero-extend it as appropriate. */
16101 case DW_FORM_data1:
16102 *bytes = dwarf2_const_value_data (attr, type, name,
16103 obstack, cu, value, 8);
16104 break;
16105 case DW_FORM_data2:
16106 *bytes = dwarf2_const_value_data (attr, type, name,
16107 obstack, cu, value, 16);
16108 break;
16109 case DW_FORM_data4:
16110 *bytes = dwarf2_const_value_data (attr, type, name,
16111 obstack, cu, value, 32);
16112 break;
16113 case DW_FORM_data8:
16114 *bytes = dwarf2_const_value_data (attr, type, name,
16115 obstack, cu, value, 64);
16116 break;
16117
16118 case DW_FORM_sdata:
16119 *value = DW_SND (attr);
16120 break;
16121
16122 case DW_FORM_udata:
16123 *value = DW_UNSND (attr);
16124 break;
16125
16126 default:
16127 complaint (&symfile_complaints,
16128 _("unsupported const value attribute form: '%s'"),
16129 dwarf_form_name (attr->form));
16130 *value = 0;
16131 break;
16132 }
16133 }
16134
16135
16136 /* Copy constant value from an attribute to a symbol. */
16137
16138 static void
16139 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16140 struct dwarf2_cu *cu)
16141 {
16142 struct objfile *objfile = cu->objfile;
16143 struct comp_unit_head *cu_header = &cu->header;
16144 LONGEST value;
16145 gdb_byte *bytes;
16146 struct dwarf2_locexpr_baton *baton;
16147
16148 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16149 SYMBOL_PRINT_NAME (sym),
16150 &objfile->objfile_obstack, cu,
16151 &value, &bytes, &baton);
16152
16153 if (baton != NULL)
16154 {
16155 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16156 SYMBOL_LOCATION_BATON (sym) = baton;
16157 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16158 }
16159 else if (bytes != NULL)
16160 {
16161 SYMBOL_VALUE_BYTES (sym) = bytes;
16162 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16163 }
16164 else
16165 {
16166 SYMBOL_VALUE (sym) = value;
16167 SYMBOL_CLASS (sym) = LOC_CONST;
16168 }
16169 }
16170
16171 /* Return the type of the die in question using its DW_AT_type attribute. */
16172
16173 static struct type *
16174 die_type (struct die_info *die, struct dwarf2_cu *cu)
16175 {
16176 struct attribute *type_attr;
16177
16178 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16179 if (!type_attr)
16180 {
16181 /* A missing DW_AT_type represents a void type. */
16182 return objfile_type (cu->objfile)->builtin_void;
16183 }
16184
16185 return lookup_die_type (die, type_attr, cu);
16186 }
16187
16188 /* True iff CU's producer generates GNAT Ada auxiliary information
16189 that allows to find parallel types through that information instead
16190 of having to do expensive parallel lookups by type name. */
16191
16192 static int
16193 need_gnat_info (struct dwarf2_cu *cu)
16194 {
16195 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16196 of GNAT produces this auxiliary information, without any indication
16197 that it is produced. Part of enhancing the FSF version of GNAT
16198 to produce that information will be to put in place an indicator
16199 that we can use in order to determine whether the descriptive type
16200 info is available or not. One suggestion that has been made is
16201 to use a new attribute, attached to the CU die. For now, assume
16202 that the descriptive type info is not available. */
16203 return 0;
16204 }
16205
16206 /* Return the auxiliary type of the die in question using its
16207 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16208 attribute is not present. */
16209
16210 static struct type *
16211 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16212 {
16213 struct attribute *type_attr;
16214
16215 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16216 if (!type_attr)
16217 return NULL;
16218
16219 return lookup_die_type (die, type_attr, cu);
16220 }
16221
16222 /* If DIE has a descriptive_type attribute, then set the TYPE's
16223 descriptive type accordingly. */
16224
16225 static void
16226 set_descriptive_type (struct type *type, struct die_info *die,
16227 struct dwarf2_cu *cu)
16228 {
16229 struct type *descriptive_type = die_descriptive_type (die, cu);
16230
16231 if (descriptive_type)
16232 {
16233 ALLOCATE_GNAT_AUX_TYPE (type);
16234 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16235 }
16236 }
16237
16238 /* Return the containing type of the die in question using its
16239 DW_AT_containing_type attribute. */
16240
16241 static struct type *
16242 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16243 {
16244 struct attribute *type_attr;
16245
16246 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16247 if (!type_attr)
16248 error (_("Dwarf Error: Problem turning containing type into gdb type "
16249 "[in module %s]"), cu->objfile->name);
16250
16251 return lookup_die_type (die, type_attr, cu);
16252 }
16253
16254 /* Look up the type of DIE in CU using its type attribute ATTR.
16255 If there is no type substitute an error marker. */
16256
16257 static struct type *
16258 lookup_die_type (struct die_info *die, struct attribute *attr,
16259 struct dwarf2_cu *cu)
16260 {
16261 struct objfile *objfile = cu->objfile;
16262 struct type *this_type;
16263
16264 /* First see if we have it cached. */
16265
16266 if (attr->form == DW_FORM_GNU_ref_alt)
16267 {
16268 struct dwarf2_per_cu_data *per_cu;
16269 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16270
16271 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16272 this_type = get_die_type_at_offset (offset, per_cu);
16273 }
16274 else if (is_ref_attr (attr))
16275 {
16276 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16277
16278 this_type = get_die_type_at_offset (offset, cu->per_cu);
16279 }
16280 else if (attr->form == DW_FORM_ref_sig8)
16281 {
16282 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16283
16284 /* sig_type will be NULL if the signatured type is missing from
16285 the debug info. */
16286 if (sig_type == NULL)
16287 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16288 "at 0x%x [in module %s]"),
16289 die->offset.sect_off, objfile->name);
16290
16291 gdb_assert (sig_type->per_cu.is_debug_types);
16292 /* If we haven't filled in type_offset_in_section yet, then we
16293 haven't read the type in yet. */
16294 this_type = NULL;
16295 if (sig_type->type_offset_in_section.sect_off != 0)
16296 {
16297 this_type =
16298 get_die_type_at_offset (sig_type->type_offset_in_section,
16299 &sig_type->per_cu);
16300 }
16301 }
16302 else
16303 {
16304 dump_die_for_error (die);
16305 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16306 dwarf_attr_name (attr->name), objfile->name);
16307 }
16308
16309 /* If not cached we need to read it in. */
16310
16311 if (this_type == NULL)
16312 {
16313 struct die_info *type_die;
16314 struct dwarf2_cu *type_cu = cu;
16315
16316 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16317 /* If we found the type now, it's probably because the type came
16318 from an inter-CU reference and the type's CU got expanded before
16319 ours. */
16320 this_type = get_die_type (type_die, type_cu);
16321 if (this_type == NULL)
16322 this_type = read_type_die_1 (type_die, type_cu);
16323 }
16324
16325 /* If we still don't have a type use an error marker. */
16326
16327 if (this_type == NULL)
16328 {
16329 char *message, *saved;
16330
16331 /* read_type_die already issued a complaint. */
16332 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16333 objfile->name,
16334 cu->header.offset.sect_off,
16335 die->offset.sect_off);
16336 saved = obstack_copy0 (&objfile->objfile_obstack,
16337 message, strlen (message));
16338 xfree (message);
16339
16340 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16341 }
16342
16343 return this_type;
16344 }
16345
16346 /* Return the type in DIE, CU.
16347 Returns NULL for invalid types.
16348
16349 This first does a lookup in the appropriate type_hash table,
16350 and only reads the die in if necessary.
16351
16352 NOTE: This can be called when reading in partial or full symbols. */
16353
16354 static struct type *
16355 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16356 {
16357 struct type *this_type;
16358
16359 this_type = get_die_type (die, cu);
16360 if (this_type)
16361 return this_type;
16362
16363 return read_type_die_1 (die, cu);
16364 }
16365
16366 /* Read the type in DIE, CU.
16367 Returns NULL for invalid types. */
16368
16369 static struct type *
16370 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16371 {
16372 struct type *this_type = NULL;
16373
16374 switch (die->tag)
16375 {
16376 case DW_TAG_class_type:
16377 case DW_TAG_interface_type:
16378 case DW_TAG_structure_type:
16379 case DW_TAG_union_type:
16380 this_type = read_structure_type (die, cu);
16381 break;
16382 case DW_TAG_enumeration_type:
16383 this_type = read_enumeration_type (die, cu);
16384 break;
16385 case DW_TAG_subprogram:
16386 case DW_TAG_subroutine_type:
16387 case DW_TAG_inlined_subroutine:
16388 this_type = read_subroutine_type (die, cu);
16389 break;
16390 case DW_TAG_array_type:
16391 this_type = read_array_type (die, cu);
16392 break;
16393 case DW_TAG_set_type:
16394 this_type = read_set_type (die, cu);
16395 break;
16396 case DW_TAG_pointer_type:
16397 this_type = read_tag_pointer_type (die, cu);
16398 break;
16399 case DW_TAG_ptr_to_member_type:
16400 this_type = read_tag_ptr_to_member_type (die, cu);
16401 break;
16402 case DW_TAG_reference_type:
16403 this_type = read_tag_reference_type (die, cu);
16404 break;
16405 case DW_TAG_const_type:
16406 this_type = read_tag_const_type (die, cu);
16407 break;
16408 case DW_TAG_volatile_type:
16409 this_type = read_tag_volatile_type (die, cu);
16410 break;
16411 case DW_TAG_string_type:
16412 this_type = read_tag_string_type (die, cu);
16413 break;
16414 case DW_TAG_typedef:
16415 this_type = read_typedef (die, cu);
16416 break;
16417 case DW_TAG_subrange_type:
16418 this_type = read_subrange_type (die, cu);
16419 break;
16420 case DW_TAG_base_type:
16421 this_type = read_base_type (die, cu);
16422 break;
16423 case DW_TAG_unspecified_type:
16424 this_type = read_unspecified_type (die, cu);
16425 break;
16426 case DW_TAG_namespace:
16427 this_type = read_namespace_type (die, cu);
16428 break;
16429 case DW_TAG_module:
16430 this_type = read_module_type (die, cu);
16431 break;
16432 default:
16433 complaint (&symfile_complaints,
16434 _("unexpected tag in read_type_die: '%s'"),
16435 dwarf_tag_name (die->tag));
16436 break;
16437 }
16438
16439 return this_type;
16440 }
16441
16442 /* See if we can figure out if the class lives in a namespace. We do
16443 this by looking for a member function; its demangled name will
16444 contain namespace info, if there is any.
16445 Return the computed name or NULL.
16446 Space for the result is allocated on the objfile's obstack.
16447 This is the full-die version of guess_partial_die_structure_name.
16448 In this case we know DIE has no useful parent. */
16449
16450 static char *
16451 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16452 {
16453 struct die_info *spec_die;
16454 struct dwarf2_cu *spec_cu;
16455 struct die_info *child;
16456
16457 spec_cu = cu;
16458 spec_die = die_specification (die, &spec_cu);
16459 if (spec_die != NULL)
16460 {
16461 die = spec_die;
16462 cu = spec_cu;
16463 }
16464
16465 for (child = die->child;
16466 child != NULL;
16467 child = child->sibling)
16468 {
16469 if (child->tag == DW_TAG_subprogram)
16470 {
16471 struct attribute *attr;
16472
16473 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16474 if (attr == NULL)
16475 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16476 if (attr != NULL)
16477 {
16478 char *actual_name
16479 = language_class_name_from_physname (cu->language_defn,
16480 DW_STRING (attr));
16481 char *name = NULL;
16482
16483 if (actual_name != NULL)
16484 {
16485 char *die_name = dwarf2_name (die, cu);
16486
16487 if (die_name != NULL
16488 && strcmp (die_name, actual_name) != 0)
16489 {
16490 /* Strip off the class name from the full name.
16491 We want the prefix. */
16492 int die_name_len = strlen (die_name);
16493 int actual_name_len = strlen (actual_name);
16494
16495 /* Test for '::' as a sanity check. */
16496 if (actual_name_len > die_name_len + 2
16497 && actual_name[actual_name_len
16498 - die_name_len - 1] == ':')
16499 name =
16500 obsavestring (actual_name,
16501 actual_name_len - die_name_len - 2,
16502 &cu->objfile->objfile_obstack);
16503 }
16504 }
16505 xfree (actual_name);
16506 return name;
16507 }
16508 }
16509 }
16510
16511 return NULL;
16512 }
16513
16514 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16515 prefix part in such case. See
16516 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16517
16518 static char *
16519 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16520 {
16521 struct attribute *attr;
16522 char *base;
16523
16524 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16525 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16526 return NULL;
16527
16528 attr = dwarf2_attr (die, DW_AT_name, cu);
16529 if (attr != NULL && DW_STRING (attr) != NULL)
16530 return NULL;
16531
16532 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16533 if (attr == NULL)
16534 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16535 if (attr == NULL || DW_STRING (attr) == NULL)
16536 return NULL;
16537
16538 /* dwarf2_name had to be already called. */
16539 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16540
16541 /* Strip the base name, keep any leading namespaces/classes. */
16542 base = strrchr (DW_STRING (attr), ':');
16543 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16544 return "";
16545
16546 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16547 &cu->objfile->objfile_obstack);
16548 }
16549
16550 /* Return the name of the namespace/class that DIE is defined within,
16551 or "" if we can't tell. The caller should not xfree the result.
16552
16553 For example, if we're within the method foo() in the following
16554 code:
16555
16556 namespace N {
16557 class C {
16558 void foo () {
16559 }
16560 };
16561 }
16562
16563 then determine_prefix on foo's die will return "N::C". */
16564
16565 static const char *
16566 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16567 {
16568 struct die_info *parent, *spec_die;
16569 struct dwarf2_cu *spec_cu;
16570 struct type *parent_type;
16571 char *retval;
16572
16573 if (cu->language != language_cplus && cu->language != language_java
16574 && cu->language != language_fortran)
16575 return "";
16576
16577 retval = anonymous_struct_prefix (die, cu);
16578 if (retval)
16579 return retval;
16580
16581 /* We have to be careful in the presence of DW_AT_specification.
16582 For example, with GCC 3.4, given the code
16583
16584 namespace N {
16585 void foo() {
16586 // Definition of N::foo.
16587 }
16588 }
16589
16590 then we'll have a tree of DIEs like this:
16591
16592 1: DW_TAG_compile_unit
16593 2: DW_TAG_namespace // N
16594 3: DW_TAG_subprogram // declaration of N::foo
16595 4: DW_TAG_subprogram // definition of N::foo
16596 DW_AT_specification // refers to die #3
16597
16598 Thus, when processing die #4, we have to pretend that we're in
16599 the context of its DW_AT_specification, namely the contex of die
16600 #3. */
16601 spec_cu = cu;
16602 spec_die = die_specification (die, &spec_cu);
16603 if (spec_die == NULL)
16604 parent = die->parent;
16605 else
16606 {
16607 parent = spec_die->parent;
16608 cu = spec_cu;
16609 }
16610
16611 if (parent == NULL)
16612 return "";
16613 else if (parent->building_fullname)
16614 {
16615 const char *name;
16616 const char *parent_name;
16617
16618 /* It has been seen on RealView 2.2 built binaries,
16619 DW_TAG_template_type_param types actually _defined_ as
16620 children of the parent class:
16621
16622 enum E {};
16623 template class <class Enum> Class{};
16624 Class<enum E> class_e;
16625
16626 1: DW_TAG_class_type (Class)
16627 2: DW_TAG_enumeration_type (E)
16628 3: DW_TAG_enumerator (enum1:0)
16629 3: DW_TAG_enumerator (enum2:1)
16630 ...
16631 2: DW_TAG_template_type_param
16632 DW_AT_type DW_FORM_ref_udata (E)
16633
16634 Besides being broken debug info, it can put GDB into an
16635 infinite loop. Consider:
16636
16637 When we're building the full name for Class<E>, we'll start
16638 at Class, and go look over its template type parameters,
16639 finding E. We'll then try to build the full name of E, and
16640 reach here. We're now trying to build the full name of E,
16641 and look over the parent DIE for containing scope. In the
16642 broken case, if we followed the parent DIE of E, we'd again
16643 find Class, and once again go look at its template type
16644 arguments, etc., etc. Simply don't consider such parent die
16645 as source-level parent of this die (it can't be, the language
16646 doesn't allow it), and break the loop here. */
16647 name = dwarf2_name (die, cu);
16648 parent_name = dwarf2_name (parent, cu);
16649 complaint (&symfile_complaints,
16650 _("template param type '%s' defined within parent '%s'"),
16651 name ? name : "<unknown>",
16652 parent_name ? parent_name : "<unknown>");
16653 return "";
16654 }
16655 else
16656 switch (parent->tag)
16657 {
16658 case DW_TAG_namespace:
16659 parent_type = read_type_die (parent, cu);
16660 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16661 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16662 Work around this problem here. */
16663 if (cu->language == language_cplus
16664 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16665 return "";
16666 /* We give a name to even anonymous namespaces. */
16667 return TYPE_TAG_NAME (parent_type);
16668 case DW_TAG_class_type:
16669 case DW_TAG_interface_type:
16670 case DW_TAG_structure_type:
16671 case DW_TAG_union_type:
16672 case DW_TAG_module:
16673 parent_type = read_type_die (parent, cu);
16674 if (TYPE_TAG_NAME (parent_type) != NULL)
16675 return TYPE_TAG_NAME (parent_type);
16676 else
16677 /* An anonymous structure is only allowed non-static data
16678 members; no typedefs, no member functions, et cetera.
16679 So it does not need a prefix. */
16680 return "";
16681 case DW_TAG_compile_unit:
16682 case DW_TAG_partial_unit:
16683 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16684 if (cu->language == language_cplus
16685 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16686 && die->child != NULL
16687 && (die->tag == DW_TAG_class_type
16688 || die->tag == DW_TAG_structure_type
16689 || die->tag == DW_TAG_union_type))
16690 {
16691 char *name = guess_full_die_structure_name (die, cu);
16692 if (name != NULL)
16693 return name;
16694 }
16695 return "";
16696 default:
16697 return determine_prefix (parent, cu);
16698 }
16699 }
16700
16701 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16702 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16703 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16704 an obconcat, otherwise allocate storage for the result. The CU argument is
16705 used to determine the language and hence, the appropriate separator. */
16706
16707 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16708
16709 static char *
16710 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16711 int physname, struct dwarf2_cu *cu)
16712 {
16713 const char *lead = "";
16714 const char *sep;
16715
16716 if (suffix == NULL || suffix[0] == '\0'
16717 || prefix == NULL || prefix[0] == '\0')
16718 sep = "";
16719 else if (cu->language == language_java)
16720 sep = ".";
16721 else if (cu->language == language_fortran && physname)
16722 {
16723 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16724 DW_AT_MIPS_linkage_name is preferred and used instead. */
16725
16726 lead = "__";
16727 sep = "_MOD_";
16728 }
16729 else
16730 sep = "::";
16731
16732 if (prefix == NULL)
16733 prefix = "";
16734 if (suffix == NULL)
16735 suffix = "";
16736
16737 if (obs == NULL)
16738 {
16739 char *retval
16740 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16741
16742 strcpy (retval, lead);
16743 strcat (retval, prefix);
16744 strcat (retval, sep);
16745 strcat (retval, suffix);
16746 return retval;
16747 }
16748 else
16749 {
16750 /* We have an obstack. */
16751 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16752 }
16753 }
16754
16755 /* Return sibling of die, NULL if no sibling. */
16756
16757 static struct die_info *
16758 sibling_die (struct die_info *die)
16759 {
16760 return die->sibling;
16761 }
16762
16763 /* Get name of a die, return NULL if not found. */
16764
16765 static char *
16766 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16767 struct obstack *obstack)
16768 {
16769 if (name && cu->language == language_cplus)
16770 {
16771 char *canon_name = cp_canonicalize_string (name);
16772
16773 if (canon_name != NULL)
16774 {
16775 if (strcmp (canon_name, name) != 0)
16776 name = obsavestring (canon_name, strlen (canon_name),
16777 obstack);
16778 xfree (canon_name);
16779 }
16780 }
16781
16782 return name;
16783 }
16784
16785 /* Get name of a die, return NULL if not found. */
16786
16787 static char *
16788 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16789 {
16790 struct attribute *attr;
16791
16792 attr = dwarf2_attr (die, DW_AT_name, cu);
16793 if ((!attr || !DW_STRING (attr))
16794 && die->tag != DW_TAG_class_type
16795 && die->tag != DW_TAG_interface_type
16796 && die->tag != DW_TAG_structure_type
16797 && die->tag != DW_TAG_union_type)
16798 return NULL;
16799
16800 switch (die->tag)
16801 {
16802 case DW_TAG_compile_unit:
16803 case DW_TAG_partial_unit:
16804 /* Compilation units have a DW_AT_name that is a filename, not
16805 a source language identifier. */
16806 case DW_TAG_enumeration_type:
16807 case DW_TAG_enumerator:
16808 /* These tags always have simple identifiers already; no need
16809 to canonicalize them. */
16810 return DW_STRING (attr);
16811
16812 case DW_TAG_subprogram:
16813 /* Java constructors will all be named "<init>", so return
16814 the class name when we see this special case. */
16815 if (cu->language == language_java
16816 && DW_STRING (attr) != NULL
16817 && strcmp (DW_STRING (attr), "<init>") == 0)
16818 {
16819 struct dwarf2_cu *spec_cu = cu;
16820 struct die_info *spec_die;
16821
16822 /* GCJ will output '<init>' for Java constructor names.
16823 For this special case, return the name of the parent class. */
16824
16825 /* GCJ may output suprogram DIEs with AT_specification set.
16826 If so, use the name of the specified DIE. */
16827 spec_die = die_specification (die, &spec_cu);
16828 if (spec_die != NULL)
16829 return dwarf2_name (spec_die, spec_cu);
16830
16831 do
16832 {
16833 die = die->parent;
16834 if (die->tag == DW_TAG_class_type)
16835 return dwarf2_name (die, cu);
16836 }
16837 while (die->tag != DW_TAG_compile_unit
16838 && die->tag != DW_TAG_partial_unit);
16839 }
16840 break;
16841
16842 case DW_TAG_class_type:
16843 case DW_TAG_interface_type:
16844 case DW_TAG_structure_type:
16845 case DW_TAG_union_type:
16846 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16847 structures or unions. These were of the form "._%d" in GCC 4.1,
16848 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16849 and GCC 4.4. We work around this problem by ignoring these. */
16850 if (attr && DW_STRING (attr)
16851 && (strncmp (DW_STRING (attr), "._", 2) == 0
16852 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16853 return NULL;
16854
16855 /* GCC might emit a nameless typedef that has a linkage name. See
16856 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16857 if (!attr || DW_STRING (attr) == NULL)
16858 {
16859 char *demangled = NULL;
16860
16861 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16862 if (attr == NULL)
16863 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16864
16865 if (attr == NULL || DW_STRING (attr) == NULL)
16866 return NULL;
16867
16868 /* Avoid demangling DW_STRING (attr) the second time on a second
16869 call for the same DIE. */
16870 if (!DW_STRING_IS_CANONICAL (attr))
16871 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16872
16873 if (demangled)
16874 {
16875 char *base;
16876
16877 /* FIXME: we already did this for the partial symbol... */
16878 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16879 &cu->objfile->objfile_obstack);
16880 DW_STRING_IS_CANONICAL (attr) = 1;
16881 xfree (demangled);
16882
16883 /* Strip any leading namespaces/classes, keep only the base name.
16884 DW_AT_name for named DIEs does not contain the prefixes. */
16885 base = strrchr (DW_STRING (attr), ':');
16886 if (base && base > DW_STRING (attr) && base[-1] == ':')
16887 return &base[1];
16888 else
16889 return DW_STRING (attr);
16890 }
16891 }
16892 break;
16893
16894 default:
16895 break;
16896 }
16897
16898 if (!DW_STRING_IS_CANONICAL (attr))
16899 {
16900 DW_STRING (attr)
16901 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16902 &cu->objfile->objfile_obstack);
16903 DW_STRING_IS_CANONICAL (attr) = 1;
16904 }
16905 return DW_STRING (attr);
16906 }
16907
16908 /* Return the die that this die in an extension of, or NULL if there
16909 is none. *EXT_CU is the CU containing DIE on input, and the CU
16910 containing the return value on output. */
16911
16912 static struct die_info *
16913 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16914 {
16915 struct attribute *attr;
16916
16917 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16918 if (attr == NULL)
16919 return NULL;
16920
16921 return follow_die_ref (die, attr, ext_cu);
16922 }
16923
16924 /* Convert a DIE tag into its string name. */
16925
16926 static const char *
16927 dwarf_tag_name (unsigned tag)
16928 {
16929 const char *name = get_DW_TAG_name (tag);
16930
16931 if (name == NULL)
16932 return "DW_TAG_<unknown>";
16933
16934 return name;
16935 }
16936
16937 /* Convert a DWARF attribute code into its string name. */
16938
16939 static const char *
16940 dwarf_attr_name (unsigned attr)
16941 {
16942 const char *name;
16943
16944 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16945 if (attr == DW_AT_MIPS_fde)
16946 return "DW_AT_MIPS_fde";
16947 #else
16948 if (attr == DW_AT_HP_block_index)
16949 return "DW_AT_HP_block_index";
16950 #endif
16951
16952 name = get_DW_AT_name (attr);
16953
16954 if (name == NULL)
16955 return "DW_AT_<unknown>";
16956
16957 return name;
16958 }
16959
16960 /* Convert a DWARF value form code into its string name. */
16961
16962 static const char *
16963 dwarf_form_name (unsigned form)
16964 {
16965 const char *name = get_DW_FORM_name (form);
16966
16967 if (name == NULL)
16968 return "DW_FORM_<unknown>";
16969
16970 return name;
16971 }
16972
16973 static char *
16974 dwarf_bool_name (unsigned mybool)
16975 {
16976 if (mybool)
16977 return "TRUE";
16978 else
16979 return "FALSE";
16980 }
16981
16982 /* Convert a DWARF type code into its string name. */
16983
16984 static const char *
16985 dwarf_type_encoding_name (unsigned enc)
16986 {
16987 const char *name = get_DW_ATE_name (enc);
16988
16989 if (name == NULL)
16990 return "DW_ATE_<unknown>";
16991
16992 return name;
16993 }
16994
16995 static void
16996 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
16997 {
16998 unsigned int i;
16999
17000 print_spaces (indent, f);
17001 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17002 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17003
17004 if (die->parent != NULL)
17005 {
17006 print_spaces (indent, f);
17007 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17008 die->parent->offset.sect_off);
17009 }
17010
17011 print_spaces (indent, f);
17012 fprintf_unfiltered (f, " has children: %s\n",
17013 dwarf_bool_name (die->child != NULL));
17014
17015 print_spaces (indent, f);
17016 fprintf_unfiltered (f, " attributes:\n");
17017
17018 for (i = 0; i < die->num_attrs; ++i)
17019 {
17020 print_spaces (indent, f);
17021 fprintf_unfiltered (f, " %s (%s) ",
17022 dwarf_attr_name (die->attrs[i].name),
17023 dwarf_form_name (die->attrs[i].form));
17024
17025 switch (die->attrs[i].form)
17026 {
17027 case DW_FORM_addr:
17028 case DW_FORM_GNU_addr_index:
17029 fprintf_unfiltered (f, "address: ");
17030 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17031 break;
17032 case DW_FORM_block2:
17033 case DW_FORM_block4:
17034 case DW_FORM_block:
17035 case DW_FORM_block1:
17036 fprintf_unfiltered (f, "block: size %s",
17037 pulongest (DW_BLOCK (&die->attrs[i])->size));
17038 break;
17039 case DW_FORM_exprloc:
17040 fprintf_unfiltered (f, "expression: size %s",
17041 pulongest (DW_BLOCK (&die->attrs[i])->size));
17042 break;
17043 case DW_FORM_ref_addr:
17044 fprintf_unfiltered (f, "ref address: ");
17045 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17046 break;
17047 case DW_FORM_GNU_ref_alt:
17048 fprintf_unfiltered (f, "alt ref address: ");
17049 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17050 break;
17051 case DW_FORM_ref1:
17052 case DW_FORM_ref2:
17053 case DW_FORM_ref4:
17054 case DW_FORM_ref8:
17055 case DW_FORM_ref_udata:
17056 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17057 (long) (DW_UNSND (&die->attrs[i])));
17058 break;
17059 case DW_FORM_data1:
17060 case DW_FORM_data2:
17061 case DW_FORM_data4:
17062 case DW_FORM_data8:
17063 case DW_FORM_udata:
17064 case DW_FORM_sdata:
17065 fprintf_unfiltered (f, "constant: %s",
17066 pulongest (DW_UNSND (&die->attrs[i])));
17067 break;
17068 case DW_FORM_sec_offset:
17069 fprintf_unfiltered (f, "section offset: %s",
17070 pulongest (DW_UNSND (&die->attrs[i])));
17071 break;
17072 case DW_FORM_ref_sig8:
17073 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17074 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17075 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17076 else
17077 fprintf_unfiltered (f, "signatured type, offset: unknown");
17078 break;
17079 case DW_FORM_string:
17080 case DW_FORM_strp:
17081 case DW_FORM_GNU_str_index:
17082 case DW_FORM_GNU_strp_alt:
17083 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17084 DW_STRING (&die->attrs[i])
17085 ? DW_STRING (&die->attrs[i]) : "",
17086 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17087 break;
17088 case DW_FORM_flag:
17089 if (DW_UNSND (&die->attrs[i]))
17090 fprintf_unfiltered (f, "flag: TRUE");
17091 else
17092 fprintf_unfiltered (f, "flag: FALSE");
17093 break;
17094 case DW_FORM_flag_present:
17095 fprintf_unfiltered (f, "flag: TRUE");
17096 break;
17097 case DW_FORM_indirect:
17098 /* The reader will have reduced the indirect form to
17099 the "base form" so this form should not occur. */
17100 fprintf_unfiltered (f,
17101 "unexpected attribute form: DW_FORM_indirect");
17102 break;
17103 default:
17104 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17105 die->attrs[i].form);
17106 break;
17107 }
17108 fprintf_unfiltered (f, "\n");
17109 }
17110 }
17111
17112 static void
17113 dump_die_for_error (struct die_info *die)
17114 {
17115 dump_die_shallow (gdb_stderr, 0, die);
17116 }
17117
17118 static void
17119 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17120 {
17121 int indent = level * 4;
17122
17123 gdb_assert (die != NULL);
17124
17125 if (level >= max_level)
17126 return;
17127
17128 dump_die_shallow (f, indent, die);
17129
17130 if (die->child != NULL)
17131 {
17132 print_spaces (indent, f);
17133 fprintf_unfiltered (f, " Children:");
17134 if (level + 1 < max_level)
17135 {
17136 fprintf_unfiltered (f, "\n");
17137 dump_die_1 (f, level + 1, max_level, die->child);
17138 }
17139 else
17140 {
17141 fprintf_unfiltered (f,
17142 " [not printed, max nesting level reached]\n");
17143 }
17144 }
17145
17146 if (die->sibling != NULL && level > 0)
17147 {
17148 dump_die_1 (f, level, max_level, die->sibling);
17149 }
17150 }
17151
17152 /* This is called from the pdie macro in gdbinit.in.
17153 It's not static so gcc will keep a copy callable from gdb. */
17154
17155 void
17156 dump_die (struct die_info *die, int max_level)
17157 {
17158 dump_die_1 (gdb_stdlog, 0, max_level, die);
17159 }
17160
17161 static void
17162 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17163 {
17164 void **slot;
17165
17166 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17167 INSERT);
17168
17169 *slot = die;
17170 }
17171
17172 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17173 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17174
17175 static int
17176 is_ref_attr (struct attribute *attr)
17177 {
17178 switch (attr->form)
17179 {
17180 case DW_FORM_ref_addr:
17181 case DW_FORM_ref1:
17182 case DW_FORM_ref2:
17183 case DW_FORM_ref4:
17184 case DW_FORM_ref8:
17185 case DW_FORM_ref_udata:
17186 case DW_FORM_GNU_ref_alt:
17187 return 1;
17188 default:
17189 return 0;
17190 }
17191 }
17192
17193 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17194 required kind. */
17195
17196 static sect_offset
17197 dwarf2_get_ref_die_offset (struct attribute *attr)
17198 {
17199 sect_offset retval = { DW_UNSND (attr) };
17200
17201 if (is_ref_attr (attr))
17202 return retval;
17203
17204 retval.sect_off = 0;
17205 complaint (&symfile_complaints,
17206 _("unsupported die ref attribute form: '%s'"),
17207 dwarf_form_name (attr->form));
17208 return retval;
17209 }
17210
17211 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17212 * the value held by the attribute is not constant. */
17213
17214 static LONGEST
17215 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17216 {
17217 if (attr->form == DW_FORM_sdata)
17218 return DW_SND (attr);
17219 else if (attr->form == DW_FORM_udata
17220 || attr->form == DW_FORM_data1
17221 || attr->form == DW_FORM_data2
17222 || attr->form == DW_FORM_data4
17223 || attr->form == DW_FORM_data8)
17224 return DW_UNSND (attr);
17225 else
17226 {
17227 complaint (&symfile_complaints,
17228 _("Attribute value is not a constant (%s)"),
17229 dwarf_form_name (attr->form));
17230 return default_value;
17231 }
17232 }
17233
17234 /* Follow reference or signature attribute ATTR of SRC_DIE.
17235 On entry *REF_CU is the CU of SRC_DIE.
17236 On exit *REF_CU is the CU of the result. */
17237
17238 static struct die_info *
17239 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17240 struct dwarf2_cu **ref_cu)
17241 {
17242 struct die_info *die;
17243
17244 if (is_ref_attr (attr))
17245 die = follow_die_ref (src_die, attr, ref_cu);
17246 else if (attr->form == DW_FORM_ref_sig8)
17247 die = follow_die_sig (src_die, attr, ref_cu);
17248 else
17249 {
17250 dump_die_for_error (src_die);
17251 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17252 (*ref_cu)->objfile->name);
17253 }
17254
17255 return die;
17256 }
17257
17258 /* Follow reference OFFSET.
17259 On entry *REF_CU is the CU of the source die referencing OFFSET.
17260 On exit *REF_CU is the CU of the result.
17261 Returns NULL if OFFSET is invalid. */
17262
17263 static struct die_info *
17264 follow_die_offset (sect_offset offset, int offset_in_dwz,
17265 struct dwarf2_cu **ref_cu)
17266 {
17267 struct die_info temp_die;
17268 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17269
17270 gdb_assert (cu->per_cu != NULL);
17271
17272 target_cu = cu;
17273
17274 if (cu->per_cu->is_debug_types)
17275 {
17276 /* .debug_types CUs cannot reference anything outside their CU.
17277 If they need to, they have to reference a signatured type via
17278 DW_FORM_ref_sig8. */
17279 if (! offset_in_cu_p (&cu->header, offset))
17280 return NULL;
17281 }
17282 else if (offset_in_dwz != cu->per_cu->is_dwz
17283 || ! offset_in_cu_p (&cu->header, offset))
17284 {
17285 struct dwarf2_per_cu_data *per_cu;
17286
17287 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17288 cu->objfile);
17289
17290 /* If necessary, add it to the queue and load its DIEs. */
17291 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17292 load_full_comp_unit (per_cu, cu->language);
17293
17294 target_cu = per_cu->cu;
17295 }
17296 else if (cu->dies == NULL)
17297 {
17298 /* We're loading full DIEs during partial symbol reading. */
17299 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17300 load_full_comp_unit (cu->per_cu, language_minimal);
17301 }
17302
17303 *ref_cu = target_cu;
17304 temp_die.offset = offset;
17305 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17306 }
17307
17308 /* Follow reference attribute ATTR of SRC_DIE.
17309 On entry *REF_CU is the CU of SRC_DIE.
17310 On exit *REF_CU is the CU of the result. */
17311
17312 static struct die_info *
17313 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17314 struct dwarf2_cu **ref_cu)
17315 {
17316 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17317 struct dwarf2_cu *cu = *ref_cu;
17318 struct die_info *die;
17319
17320 die = follow_die_offset (offset,
17321 (attr->form == DW_FORM_GNU_ref_alt
17322 || cu->per_cu->is_dwz),
17323 ref_cu);
17324 if (!die)
17325 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17326 "at 0x%x [in module %s]"),
17327 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17328
17329 return die;
17330 }
17331
17332 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17333 Returned value is intended for DW_OP_call*. Returned
17334 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17335
17336 struct dwarf2_locexpr_baton
17337 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
17338 struct dwarf2_per_cu_data *per_cu,
17339 CORE_ADDR (*get_frame_pc) (void *baton),
17340 void *baton)
17341 {
17342 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17343 struct dwarf2_cu *cu;
17344 struct die_info *die;
17345 struct attribute *attr;
17346 struct dwarf2_locexpr_baton retval;
17347
17348 dw2_setup (per_cu->objfile);
17349
17350 if (per_cu->cu == NULL)
17351 load_cu (per_cu);
17352 cu = per_cu->cu;
17353
17354 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17355 if (!die)
17356 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17357 offset.sect_off, per_cu->objfile->name);
17358
17359 attr = dwarf2_attr (die, DW_AT_location, cu);
17360 if (!attr)
17361 {
17362 /* DWARF: "If there is no such attribute, then there is no effect.".
17363 DATA is ignored if SIZE is 0. */
17364
17365 retval.data = NULL;
17366 retval.size = 0;
17367 }
17368 else if (attr_form_is_section_offset (attr))
17369 {
17370 struct dwarf2_loclist_baton loclist_baton;
17371 CORE_ADDR pc = (*get_frame_pc) (baton);
17372 size_t size;
17373
17374 fill_in_loclist_baton (cu, &loclist_baton, attr);
17375
17376 retval.data = dwarf2_find_location_expression (&loclist_baton,
17377 &size, pc);
17378 retval.size = size;
17379 }
17380 else
17381 {
17382 if (!attr_form_is_block (attr))
17383 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17384 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17385 offset.sect_off, per_cu->objfile->name);
17386
17387 retval.data = DW_BLOCK (attr)->data;
17388 retval.size = DW_BLOCK (attr)->size;
17389 }
17390 retval.per_cu = cu->per_cu;
17391
17392 age_cached_comp_units ();
17393
17394 return retval;
17395 }
17396
17397 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17398 PER_CU. */
17399
17400 struct type *
17401 dwarf2_get_die_type (cu_offset die_offset,
17402 struct dwarf2_per_cu_data *per_cu)
17403 {
17404 sect_offset die_offset_sect;
17405
17406 dw2_setup (per_cu->objfile);
17407
17408 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17409 return get_die_type_at_offset (die_offset_sect, per_cu);
17410 }
17411
17412 /* Follow the signature attribute ATTR in SRC_DIE.
17413 On entry *REF_CU is the CU of SRC_DIE.
17414 On exit *REF_CU is the CU of the result. */
17415
17416 static struct die_info *
17417 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17418 struct dwarf2_cu **ref_cu)
17419 {
17420 struct objfile *objfile = (*ref_cu)->objfile;
17421 struct die_info temp_die;
17422 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17423 struct dwarf2_cu *sig_cu;
17424 struct die_info *die;
17425
17426 /* sig_type will be NULL if the signatured type is missing from
17427 the debug info. */
17428 if (sig_type == NULL)
17429 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17430 "at 0x%x [in module %s]"),
17431 src_die->offset.sect_off, objfile->name);
17432
17433 /* If necessary, add it to the queue and load its DIEs. */
17434
17435 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17436 read_signatured_type (sig_type);
17437
17438 gdb_assert (sig_type->per_cu.cu != NULL);
17439
17440 sig_cu = sig_type->per_cu.cu;
17441 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17442 temp_die.offset = sig_type->type_offset_in_section;
17443 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17444 temp_die.offset.sect_off);
17445 if (die)
17446 {
17447 *ref_cu = sig_cu;
17448 return die;
17449 }
17450
17451 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17452 "from DIE at 0x%x [in module %s]"),
17453 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17454 }
17455
17456 /* Given an offset of a signatured type, return its signatured_type. */
17457
17458 static struct signatured_type *
17459 lookup_signatured_type_at_offset (struct objfile *objfile,
17460 struct dwarf2_section_info *section,
17461 sect_offset offset)
17462 {
17463 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17464 unsigned int length, initial_length_size;
17465 unsigned int sig_offset;
17466 struct signatured_type find_entry, *sig_type;
17467
17468 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17469 sig_offset = (initial_length_size
17470 + 2 /*version*/
17471 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17472 + 1 /*address_size*/);
17473 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17474 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17475
17476 /* This is only used to lookup previously recorded types.
17477 If we didn't find it, it's our bug. */
17478 gdb_assert (sig_type != NULL);
17479 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17480
17481 return sig_type;
17482 }
17483
17484 /* Load the DIEs associated with type unit PER_CU into memory. */
17485
17486 static void
17487 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17488 {
17489 struct signatured_type *sig_type;
17490
17491 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17492 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17493
17494 /* We have the per_cu, but we need the signatured_type.
17495 Fortunately this is an easy translation. */
17496 gdb_assert (per_cu->is_debug_types);
17497 sig_type = (struct signatured_type *) per_cu;
17498
17499 gdb_assert (per_cu->cu == NULL);
17500
17501 read_signatured_type (sig_type);
17502
17503 gdb_assert (per_cu->cu != NULL);
17504 }
17505
17506 /* die_reader_func for read_signatured_type.
17507 This is identical to load_full_comp_unit_reader,
17508 but is kept separate for now. */
17509
17510 static void
17511 read_signatured_type_reader (const struct die_reader_specs *reader,
17512 gdb_byte *info_ptr,
17513 struct die_info *comp_unit_die,
17514 int has_children,
17515 void *data)
17516 {
17517 struct dwarf2_cu *cu = reader->cu;
17518
17519 gdb_assert (cu->die_hash == NULL);
17520 cu->die_hash =
17521 htab_create_alloc_ex (cu->header.length / 12,
17522 die_hash,
17523 die_eq,
17524 NULL,
17525 &cu->comp_unit_obstack,
17526 hashtab_obstack_allocate,
17527 dummy_obstack_deallocate);
17528
17529 if (has_children)
17530 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17531 &info_ptr, comp_unit_die);
17532 cu->dies = comp_unit_die;
17533 /* comp_unit_die is not stored in die_hash, no need. */
17534
17535 /* We try not to read any attributes in this function, because not
17536 all CUs needed for references have been loaded yet, and symbol
17537 table processing isn't initialized. But we have to set the CU language,
17538 or we won't be able to build types correctly.
17539 Similarly, if we do not read the producer, we can not apply
17540 producer-specific interpretation. */
17541 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17542 }
17543
17544 /* Read in a signatured type and build its CU and DIEs.
17545 If the type is a stub for the real type in a DWO file,
17546 read in the real type from the DWO file as well. */
17547
17548 static void
17549 read_signatured_type (struct signatured_type *sig_type)
17550 {
17551 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17552
17553 gdb_assert (per_cu->is_debug_types);
17554 gdb_assert (per_cu->cu == NULL);
17555
17556 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17557 read_signatured_type_reader, NULL);
17558 }
17559
17560 /* Decode simple location descriptions.
17561 Given a pointer to a dwarf block that defines a location, compute
17562 the location and return the value.
17563
17564 NOTE drow/2003-11-18: This function is called in two situations
17565 now: for the address of static or global variables (partial symbols
17566 only) and for offsets into structures which are expected to be
17567 (more or less) constant. The partial symbol case should go away,
17568 and only the constant case should remain. That will let this
17569 function complain more accurately. A few special modes are allowed
17570 without complaint for global variables (for instance, global
17571 register values and thread-local values).
17572
17573 A location description containing no operations indicates that the
17574 object is optimized out. The return value is 0 for that case.
17575 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17576 callers will only want a very basic result and this can become a
17577 complaint.
17578
17579 Note that stack[0] is unused except as a default error return. */
17580
17581 static CORE_ADDR
17582 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17583 {
17584 struct objfile *objfile = cu->objfile;
17585 size_t i;
17586 size_t size = blk->size;
17587 gdb_byte *data = blk->data;
17588 CORE_ADDR stack[64];
17589 int stacki;
17590 unsigned int bytes_read, unsnd;
17591 gdb_byte op;
17592
17593 i = 0;
17594 stacki = 0;
17595 stack[stacki] = 0;
17596 stack[++stacki] = 0;
17597
17598 while (i < size)
17599 {
17600 op = data[i++];
17601 switch (op)
17602 {
17603 case DW_OP_lit0:
17604 case DW_OP_lit1:
17605 case DW_OP_lit2:
17606 case DW_OP_lit3:
17607 case DW_OP_lit4:
17608 case DW_OP_lit5:
17609 case DW_OP_lit6:
17610 case DW_OP_lit7:
17611 case DW_OP_lit8:
17612 case DW_OP_lit9:
17613 case DW_OP_lit10:
17614 case DW_OP_lit11:
17615 case DW_OP_lit12:
17616 case DW_OP_lit13:
17617 case DW_OP_lit14:
17618 case DW_OP_lit15:
17619 case DW_OP_lit16:
17620 case DW_OP_lit17:
17621 case DW_OP_lit18:
17622 case DW_OP_lit19:
17623 case DW_OP_lit20:
17624 case DW_OP_lit21:
17625 case DW_OP_lit22:
17626 case DW_OP_lit23:
17627 case DW_OP_lit24:
17628 case DW_OP_lit25:
17629 case DW_OP_lit26:
17630 case DW_OP_lit27:
17631 case DW_OP_lit28:
17632 case DW_OP_lit29:
17633 case DW_OP_lit30:
17634 case DW_OP_lit31:
17635 stack[++stacki] = op - DW_OP_lit0;
17636 break;
17637
17638 case DW_OP_reg0:
17639 case DW_OP_reg1:
17640 case DW_OP_reg2:
17641 case DW_OP_reg3:
17642 case DW_OP_reg4:
17643 case DW_OP_reg5:
17644 case DW_OP_reg6:
17645 case DW_OP_reg7:
17646 case DW_OP_reg8:
17647 case DW_OP_reg9:
17648 case DW_OP_reg10:
17649 case DW_OP_reg11:
17650 case DW_OP_reg12:
17651 case DW_OP_reg13:
17652 case DW_OP_reg14:
17653 case DW_OP_reg15:
17654 case DW_OP_reg16:
17655 case DW_OP_reg17:
17656 case DW_OP_reg18:
17657 case DW_OP_reg19:
17658 case DW_OP_reg20:
17659 case DW_OP_reg21:
17660 case DW_OP_reg22:
17661 case DW_OP_reg23:
17662 case DW_OP_reg24:
17663 case DW_OP_reg25:
17664 case DW_OP_reg26:
17665 case DW_OP_reg27:
17666 case DW_OP_reg28:
17667 case DW_OP_reg29:
17668 case DW_OP_reg30:
17669 case DW_OP_reg31:
17670 stack[++stacki] = op - DW_OP_reg0;
17671 if (i < size)
17672 dwarf2_complex_location_expr_complaint ();
17673 break;
17674
17675 case DW_OP_regx:
17676 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17677 i += bytes_read;
17678 stack[++stacki] = unsnd;
17679 if (i < size)
17680 dwarf2_complex_location_expr_complaint ();
17681 break;
17682
17683 case DW_OP_addr:
17684 stack[++stacki] = read_address (objfile->obfd, &data[i],
17685 cu, &bytes_read);
17686 i += bytes_read;
17687 break;
17688
17689 case DW_OP_const1u:
17690 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17691 i += 1;
17692 break;
17693
17694 case DW_OP_const1s:
17695 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17696 i += 1;
17697 break;
17698
17699 case DW_OP_const2u:
17700 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17701 i += 2;
17702 break;
17703
17704 case DW_OP_const2s:
17705 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17706 i += 2;
17707 break;
17708
17709 case DW_OP_const4u:
17710 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17711 i += 4;
17712 break;
17713
17714 case DW_OP_const4s:
17715 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17716 i += 4;
17717 break;
17718
17719 case DW_OP_const8u:
17720 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17721 i += 8;
17722 break;
17723
17724 case DW_OP_constu:
17725 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17726 &bytes_read);
17727 i += bytes_read;
17728 break;
17729
17730 case DW_OP_consts:
17731 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17732 i += bytes_read;
17733 break;
17734
17735 case DW_OP_dup:
17736 stack[stacki + 1] = stack[stacki];
17737 stacki++;
17738 break;
17739
17740 case DW_OP_plus:
17741 stack[stacki - 1] += stack[stacki];
17742 stacki--;
17743 break;
17744
17745 case DW_OP_plus_uconst:
17746 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17747 &bytes_read);
17748 i += bytes_read;
17749 break;
17750
17751 case DW_OP_minus:
17752 stack[stacki - 1] -= stack[stacki];
17753 stacki--;
17754 break;
17755
17756 case DW_OP_deref:
17757 /* If we're not the last op, then we definitely can't encode
17758 this using GDB's address_class enum. This is valid for partial
17759 global symbols, although the variable's address will be bogus
17760 in the psymtab. */
17761 if (i < size)
17762 dwarf2_complex_location_expr_complaint ();
17763 break;
17764
17765 case DW_OP_GNU_push_tls_address:
17766 /* The top of the stack has the offset from the beginning
17767 of the thread control block at which the variable is located. */
17768 /* Nothing should follow this operator, so the top of stack would
17769 be returned. */
17770 /* This is valid for partial global symbols, but the variable's
17771 address will be bogus in the psymtab. Make it always at least
17772 non-zero to not look as a variable garbage collected by linker
17773 which have DW_OP_addr 0. */
17774 if (i < size)
17775 dwarf2_complex_location_expr_complaint ();
17776 stack[stacki]++;
17777 break;
17778
17779 case DW_OP_GNU_uninit:
17780 break;
17781
17782 case DW_OP_GNU_addr_index:
17783 case DW_OP_GNU_const_index:
17784 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17785 &bytes_read);
17786 i += bytes_read;
17787 break;
17788
17789 default:
17790 {
17791 const char *name = get_DW_OP_name (op);
17792
17793 if (name)
17794 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17795 name);
17796 else
17797 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17798 op);
17799 }
17800
17801 return (stack[stacki]);
17802 }
17803
17804 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17805 outside of the allocated space. Also enforce minimum>0. */
17806 if (stacki >= ARRAY_SIZE (stack) - 1)
17807 {
17808 complaint (&symfile_complaints,
17809 _("location description stack overflow"));
17810 return 0;
17811 }
17812
17813 if (stacki <= 0)
17814 {
17815 complaint (&symfile_complaints,
17816 _("location description stack underflow"));
17817 return 0;
17818 }
17819 }
17820 return (stack[stacki]);
17821 }
17822
17823 /* memory allocation interface */
17824
17825 static struct dwarf_block *
17826 dwarf_alloc_block (struct dwarf2_cu *cu)
17827 {
17828 struct dwarf_block *blk;
17829
17830 blk = (struct dwarf_block *)
17831 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17832 return (blk);
17833 }
17834
17835 static struct die_info *
17836 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17837 {
17838 struct die_info *die;
17839 size_t size = sizeof (struct die_info);
17840
17841 if (num_attrs > 1)
17842 size += (num_attrs - 1) * sizeof (struct attribute);
17843
17844 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17845 memset (die, 0, sizeof (struct die_info));
17846 return (die);
17847 }
17848
17849 \f
17850 /* Macro support. */
17851
17852 /* Return the full name of file number I in *LH's file name table.
17853 Use COMP_DIR as the name of the current directory of the
17854 compilation. The result is allocated using xmalloc; the caller is
17855 responsible for freeing it. */
17856 static char *
17857 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17858 {
17859 /* Is the file number a valid index into the line header's file name
17860 table? Remember that file numbers start with one, not zero. */
17861 if (1 <= file && file <= lh->num_file_names)
17862 {
17863 struct file_entry *fe = &lh->file_names[file - 1];
17864
17865 if (IS_ABSOLUTE_PATH (fe->name))
17866 return xstrdup (fe->name);
17867 else
17868 {
17869 const char *dir;
17870 int dir_len;
17871 char *full_name;
17872
17873 if (fe->dir_index)
17874 dir = lh->include_dirs[fe->dir_index - 1];
17875 else
17876 dir = comp_dir;
17877
17878 if (dir)
17879 {
17880 dir_len = strlen (dir);
17881 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17882 strcpy (full_name, dir);
17883 full_name[dir_len] = '/';
17884 strcpy (full_name + dir_len + 1, fe->name);
17885 return full_name;
17886 }
17887 else
17888 return xstrdup (fe->name);
17889 }
17890 }
17891 else
17892 {
17893 /* The compiler produced a bogus file number. We can at least
17894 record the macro definitions made in the file, even if we
17895 won't be able to find the file by name. */
17896 char fake_name[80];
17897
17898 xsnprintf (fake_name, sizeof (fake_name),
17899 "<bad macro file number %d>", file);
17900
17901 complaint (&symfile_complaints,
17902 _("bad file number in macro information (%d)"),
17903 file);
17904
17905 return xstrdup (fake_name);
17906 }
17907 }
17908
17909
17910 static struct macro_source_file *
17911 macro_start_file (int file, int line,
17912 struct macro_source_file *current_file,
17913 const char *comp_dir,
17914 struct line_header *lh, struct objfile *objfile)
17915 {
17916 /* The full name of this source file. */
17917 char *full_name = file_full_name (file, lh, comp_dir);
17918
17919 /* We don't create a macro table for this compilation unit
17920 at all until we actually get a filename. */
17921 if (! pending_macros)
17922 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17923 objfile->per_bfd->macro_cache);
17924
17925 if (! current_file)
17926 {
17927 /* If we have no current file, then this must be the start_file
17928 directive for the compilation unit's main source file. */
17929 current_file = macro_set_main (pending_macros, full_name);
17930 macro_define_special (pending_macros);
17931 }
17932 else
17933 current_file = macro_include (current_file, line, full_name);
17934
17935 xfree (full_name);
17936
17937 return current_file;
17938 }
17939
17940
17941 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17942 followed by a null byte. */
17943 static char *
17944 copy_string (const char *buf, int len)
17945 {
17946 char *s = xmalloc (len + 1);
17947
17948 memcpy (s, buf, len);
17949 s[len] = '\0';
17950 return s;
17951 }
17952
17953
17954 static const char *
17955 consume_improper_spaces (const char *p, const char *body)
17956 {
17957 if (*p == ' ')
17958 {
17959 complaint (&symfile_complaints,
17960 _("macro definition contains spaces "
17961 "in formal argument list:\n`%s'"),
17962 body);
17963
17964 while (*p == ' ')
17965 p++;
17966 }
17967
17968 return p;
17969 }
17970
17971
17972 static void
17973 parse_macro_definition (struct macro_source_file *file, int line,
17974 const char *body)
17975 {
17976 const char *p;
17977
17978 /* The body string takes one of two forms. For object-like macro
17979 definitions, it should be:
17980
17981 <macro name> " " <definition>
17982
17983 For function-like macro definitions, it should be:
17984
17985 <macro name> "() " <definition>
17986 or
17987 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17988
17989 Spaces may appear only where explicitly indicated, and in the
17990 <definition>.
17991
17992 The Dwarf 2 spec says that an object-like macro's name is always
17993 followed by a space, but versions of GCC around March 2002 omit
17994 the space when the macro's definition is the empty string.
17995
17996 The Dwarf 2 spec says that there should be no spaces between the
17997 formal arguments in a function-like macro's formal argument list,
17998 but versions of GCC around March 2002 include spaces after the
17999 commas. */
18000
18001
18002 /* Find the extent of the macro name. The macro name is terminated
18003 by either a space or null character (for an object-like macro) or
18004 an opening paren (for a function-like macro). */
18005 for (p = body; *p; p++)
18006 if (*p == ' ' || *p == '(')
18007 break;
18008
18009 if (*p == ' ' || *p == '\0')
18010 {
18011 /* It's an object-like macro. */
18012 int name_len = p - body;
18013 char *name = copy_string (body, name_len);
18014 const char *replacement;
18015
18016 if (*p == ' ')
18017 replacement = body + name_len + 1;
18018 else
18019 {
18020 dwarf2_macro_malformed_definition_complaint (body);
18021 replacement = body + name_len;
18022 }
18023
18024 macro_define_object (file, line, name, replacement);
18025
18026 xfree (name);
18027 }
18028 else if (*p == '(')
18029 {
18030 /* It's a function-like macro. */
18031 char *name = copy_string (body, p - body);
18032 int argc = 0;
18033 int argv_size = 1;
18034 char **argv = xmalloc (argv_size * sizeof (*argv));
18035
18036 p++;
18037
18038 p = consume_improper_spaces (p, body);
18039
18040 /* Parse the formal argument list. */
18041 while (*p && *p != ')')
18042 {
18043 /* Find the extent of the current argument name. */
18044 const char *arg_start = p;
18045
18046 while (*p && *p != ',' && *p != ')' && *p != ' ')
18047 p++;
18048
18049 if (! *p || p == arg_start)
18050 dwarf2_macro_malformed_definition_complaint (body);
18051 else
18052 {
18053 /* Make sure argv has room for the new argument. */
18054 if (argc >= argv_size)
18055 {
18056 argv_size *= 2;
18057 argv = xrealloc (argv, argv_size * sizeof (*argv));
18058 }
18059
18060 argv[argc++] = copy_string (arg_start, p - arg_start);
18061 }
18062
18063 p = consume_improper_spaces (p, body);
18064
18065 /* Consume the comma, if present. */
18066 if (*p == ',')
18067 {
18068 p++;
18069
18070 p = consume_improper_spaces (p, body);
18071 }
18072 }
18073
18074 if (*p == ')')
18075 {
18076 p++;
18077
18078 if (*p == ' ')
18079 /* Perfectly formed definition, no complaints. */
18080 macro_define_function (file, line, name,
18081 argc, (const char **) argv,
18082 p + 1);
18083 else if (*p == '\0')
18084 {
18085 /* Complain, but do define it. */
18086 dwarf2_macro_malformed_definition_complaint (body);
18087 macro_define_function (file, line, name,
18088 argc, (const char **) argv,
18089 p);
18090 }
18091 else
18092 /* Just complain. */
18093 dwarf2_macro_malformed_definition_complaint (body);
18094 }
18095 else
18096 /* Just complain. */
18097 dwarf2_macro_malformed_definition_complaint (body);
18098
18099 xfree (name);
18100 {
18101 int i;
18102
18103 for (i = 0; i < argc; i++)
18104 xfree (argv[i]);
18105 }
18106 xfree (argv);
18107 }
18108 else
18109 dwarf2_macro_malformed_definition_complaint (body);
18110 }
18111
18112 /* Skip some bytes from BYTES according to the form given in FORM.
18113 Returns the new pointer. */
18114
18115 static gdb_byte *
18116 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18117 enum dwarf_form form,
18118 unsigned int offset_size,
18119 struct dwarf2_section_info *section)
18120 {
18121 unsigned int bytes_read;
18122
18123 switch (form)
18124 {
18125 case DW_FORM_data1:
18126 case DW_FORM_flag:
18127 ++bytes;
18128 break;
18129
18130 case DW_FORM_data2:
18131 bytes += 2;
18132 break;
18133
18134 case DW_FORM_data4:
18135 bytes += 4;
18136 break;
18137
18138 case DW_FORM_data8:
18139 bytes += 8;
18140 break;
18141
18142 case DW_FORM_string:
18143 read_direct_string (abfd, bytes, &bytes_read);
18144 bytes += bytes_read;
18145 break;
18146
18147 case DW_FORM_sec_offset:
18148 case DW_FORM_strp:
18149 case DW_FORM_GNU_strp_alt:
18150 bytes += offset_size;
18151 break;
18152
18153 case DW_FORM_block:
18154 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18155 bytes += bytes_read;
18156 break;
18157
18158 case DW_FORM_block1:
18159 bytes += 1 + read_1_byte (abfd, bytes);
18160 break;
18161 case DW_FORM_block2:
18162 bytes += 2 + read_2_bytes (abfd, bytes);
18163 break;
18164 case DW_FORM_block4:
18165 bytes += 4 + read_4_bytes (abfd, bytes);
18166 break;
18167
18168 case DW_FORM_sdata:
18169 case DW_FORM_udata:
18170 case DW_FORM_GNU_addr_index:
18171 case DW_FORM_GNU_str_index:
18172 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18173 if (bytes == NULL)
18174 {
18175 dwarf2_section_buffer_overflow_complaint (section);
18176 return NULL;
18177 }
18178 break;
18179
18180 default:
18181 {
18182 complain:
18183 complaint (&symfile_complaints,
18184 _("invalid form 0x%x in `%s'"),
18185 form,
18186 section->asection->name);
18187 return NULL;
18188 }
18189 }
18190
18191 return bytes;
18192 }
18193
18194 /* A helper for dwarf_decode_macros that handles skipping an unknown
18195 opcode. Returns an updated pointer to the macro data buffer; or,
18196 on error, issues a complaint and returns NULL. */
18197
18198 static gdb_byte *
18199 skip_unknown_opcode (unsigned int opcode,
18200 gdb_byte **opcode_definitions,
18201 gdb_byte *mac_ptr, gdb_byte *mac_end,
18202 bfd *abfd,
18203 unsigned int offset_size,
18204 struct dwarf2_section_info *section)
18205 {
18206 unsigned int bytes_read, i;
18207 unsigned long arg;
18208 gdb_byte *defn;
18209
18210 if (opcode_definitions[opcode] == NULL)
18211 {
18212 complaint (&symfile_complaints,
18213 _("unrecognized DW_MACFINO opcode 0x%x"),
18214 opcode);
18215 return NULL;
18216 }
18217
18218 defn = opcode_definitions[opcode];
18219 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18220 defn += bytes_read;
18221
18222 for (i = 0; i < arg; ++i)
18223 {
18224 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18225 section);
18226 if (mac_ptr == NULL)
18227 {
18228 /* skip_form_bytes already issued the complaint. */
18229 return NULL;
18230 }
18231 }
18232
18233 return mac_ptr;
18234 }
18235
18236 /* A helper function which parses the header of a macro section.
18237 If the macro section is the extended (for now called "GNU") type,
18238 then this updates *OFFSET_SIZE. Returns a pointer to just after
18239 the header, or issues a complaint and returns NULL on error. */
18240
18241 static gdb_byte *
18242 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18243 bfd *abfd,
18244 gdb_byte *mac_ptr,
18245 unsigned int *offset_size,
18246 int section_is_gnu)
18247 {
18248 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18249
18250 if (section_is_gnu)
18251 {
18252 unsigned int version, flags;
18253
18254 version = read_2_bytes (abfd, mac_ptr);
18255 if (version != 4)
18256 {
18257 complaint (&symfile_complaints,
18258 _("unrecognized version `%d' in .debug_macro section"),
18259 version);
18260 return NULL;
18261 }
18262 mac_ptr += 2;
18263
18264 flags = read_1_byte (abfd, mac_ptr);
18265 ++mac_ptr;
18266 *offset_size = (flags & 1) ? 8 : 4;
18267
18268 if ((flags & 2) != 0)
18269 /* We don't need the line table offset. */
18270 mac_ptr += *offset_size;
18271
18272 /* Vendor opcode descriptions. */
18273 if ((flags & 4) != 0)
18274 {
18275 unsigned int i, count;
18276
18277 count = read_1_byte (abfd, mac_ptr);
18278 ++mac_ptr;
18279 for (i = 0; i < count; ++i)
18280 {
18281 unsigned int opcode, bytes_read;
18282 unsigned long arg;
18283
18284 opcode = read_1_byte (abfd, mac_ptr);
18285 ++mac_ptr;
18286 opcode_definitions[opcode] = mac_ptr;
18287 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18288 mac_ptr += bytes_read;
18289 mac_ptr += arg;
18290 }
18291 }
18292 }
18293
18294 return mac_ptr;
18295 }
18296
18297 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18298 including DW_MACRO_GNU_transparent_include. */
18299
18300 static void
18301 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18302 struct macro_source_file *current_file,
18303 struct line_header *lh, char *comp_dir,
18304 struct dwarf2_section_info *section,
18305 int section_is_gnu, int section_is_dwz,
18306 unsigned int offset_size,
18307 struct objfile *objfile,
18308 htab_t include_hash)
18309 {
18310 enum dwarf_macro_record_type macinfo_type;
18311 int at_commandline;
18312 gdb_byte *opcode_definitions[256];
18313
18314 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18315 &offset_size, section_is_gnu);
18316 if (mac_ptr == NULL)
18317 {
18318 /* We already issued a complaint. */
18319 return;
18320 }
18321
18322 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18323 GDB is still reading the definitions from command line. First
18324 DW_MACINFO_start_file will need to be ignored as it was already executed
18325 to create CURRENT_FILE for the main source holding also the command line
18326 definitions. On first met DW_MACINFO_start_file this flag is reset to
18327 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18328
18329 at_commandline = 1;
18330
18331 do
18332 {
18333 /* Do we at least have room for a macinfo type byte? */
18334 if (mac_ptr >= mac_end)
18335 {
18336 dwarf2_section_buffer_overflow_complaint (section);
18337 break;
18338 }
18339
18340 macinfo_type = read_1_byte (abfd, mac_ptr);
18341 mac_ptr++;
18342
18343 /* Note that we rely on the fact that the corresponding GNU and
18344 DWARF constants are the same. */
18345 switch (macinfo_type)
18346 {
18347 /* A zero macinfo type indicates the end of the macro
18348 information. */
18349 case 0:
18350 break;
18351
18352 case DW_MACRO_GNU_define:
18353 case DW_MACRO_GNU_undef:
18354 case DW_MACRO_GNU_define_indirect:
18355 case DW_MACRO_GNU_undef_indirect:
18356 case DW_MACRO_GNU_define_indirect_alt:
18357 case DW_MACRO_GNU_undef_indirect_alt:
18358 {
18359 unsigned int bytes_read;
18360 int line;
18361 char *body;
18362 int is_define;
18363
18364 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18365 mac_ptr += bytes_read;
18366
18367 if (macinfo_type == DW_MACRO_GNU_define
18368 || macinfo_type == DW_MACRO_GNU_undef)
18369 {
18370 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18371 mac_ptr += bytes_read;
18372 }
18373 else
18374 {
18375 LONGEST str_offset;
18376
18377 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18378 mac_ptr += offset_size;
18379
18380 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18381 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18382 || section_is_dwz)
18383 {
18384 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18385
18386 body = read_indirect_string_from_dwz (dwz, str_offset);
18387 }
18388 else
18389 body = read_indirect_string_at_offset (abfd, str_offset);
18390 }
18391
18392 is_define = (macinfo_type == DW_MACRO_GNU_define
18393 || macinfo_type == DW_MACRO_GNU_define_indirect
18394 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18395 if (! current_file)
18396 {
18397 /* DWARF violation as no main source is present. */
18398 complaint (&symfile_complaints,
18399 _("debug info with no main source gives macro %s "
18400 "on line %d: %s"),
18401 is_define ? _("definition") : _("undefinition"),
18402 line, body);
18403 break;
18404 }
18405 if ((line == 0 && !at_commandline)
18406 || (line != 0 && at_commandline))
18407 complaint (&symfile_complaints,
18408 _("debug info gives %s macro %s with %s line %d: %s"),
18409 at_commandline ? _("command-line") : _("in-file"),
18410 is_define ? _("definition") : _("undefinition"),
18411 line == 0 ? _("zero") : _("non-zero"), line, body);
18412
18413 if (is_define)
18414 parse_macro_definition (current_file, line, body);
18415 else
18416 {
18417 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18418 || macinfo_type == DW_MACRO_GNU_undef_indirect
18419 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18420 macro_undef (current_file, line, body);
18421 }
18422 }
18423 break;
18424
18425 case DW_MACRO_GNU_start_file:
18426 {
18427 unsigned int bytes_read;
18428 int line, file;
18429
18430 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18431 mac_ptr += bytes_read;
18432 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18433 mac_ptr += bytes_read;
18434
18435 if ((line == 0 && !at_commandline)
18436 || (line != 0 && at_commandline))
18437 complaint (&symfile_complaints,
18438 _("debug info gives source %d included "
18439 "from %s at %s line %d"),
18440 file, at_commandline ? _("command-line") : _("file"),
18441 line == 0 ? _("zero") : _("non-zero"), line);
18442
18443 if (at_commandline)
18444 {
18445 /* This DW_MACRO_GNU_start_file was executed in the
18446 pass one. */
18447 at_commandline = 0;
18448 }
18449 else
18450 current_file = macro_start_file (file, line,
18451 current_file, comp_dir,
18452 lh, objfile);
18453 }
18454 break;
18455
18456 case DW_MACRO_GNU_end_file:
18457 if (! current_file)
18458 complaint (&symfile_complaints,
18459 _("macro debug info has an unmatched "
18460 "`close_file' directive"));
18461 else
18462 {
18463 current_file = current_file->included_by;
18464 if (! current_file)
18465 {
18466 enum dwarf_macro_record_type next_type;
18467
18468 /* GCC circa March 2002 doesn't produce the zero
18469 type byte marking the end of the compilation
18470 unit. Complain if it's not there, but exit no
18471 matter what. */
18472
18473 /* Do we at least have room for a macinfo type byte? */
18474 if (mac_ptr >= mac_end)
18475 {
18476 dwarf2_section_buffer_overflow_complaint (section);
18477 return;
18478 }
18479
18480 /* We don't increment mac_ptr here, so this is just
18481 a look-ahead. */
18482 next_type = read_1_byte (abfd, mac_ptr);
18483 if (next_type != 0)
18484 complaint (&symfile_complaints,
18485 _("no terminating 0-type entry for "
18486 "macros in `.debug_macinfo' section"));
18487
18488 return;
18489 }
18490 }
18491 break;
18492
18493 case DW_MACRO_GNU_transparent_include:
18494 case DW_MACRO_GNU_transparent_include_alt:
18495 {
18496 LONGEST offset;
18497 void **slot;
18498 bfd *include_bfd = abfd;
18499 struct dwarf2_section_info *include_section = section;
18500 struct dwarf2_section_info alt_section;
18501 gdb_byte *include_mac_end = mac_end;
18502 int is_dwz = section_is_dwz;
18503 gdb_byte *new_mac_ptr;
18504
18505 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18506 mac_ptr += offset_size;
18507
18508 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18509 {
18510 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18511
18512 dwarf2_read_section (dwarf2_per_objfile->objfile,
18513 &dwz->macro);
18514
18515 include_bfd = dwz->macro.asection->owner;
18516 include_section = &dwz->macro;
18517 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18518 is_dwz = 1;
18519 }
18520
18521 new_mac_ptr = include_section->buffer + offset;
18522 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18523
18524 if (*slot != NULL)
18525 {
18526 /* This has actually happened; see
18527 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18528 complaint (&symfile_complaints,
18529 _("recursive DW_MACRO_GNU_transparent_include in "
18530 ".debug_macro section"));
18531 }
18532 else
18533 {
18534 *slot = new_mac_ptr;
18535
18536 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18537 include_mac_end, current_file,
18538 lh, comp_dir,
18539 section, section_is_gnu, is_dwz,
18540 offset_size, objfile, include_hash);
18541
18542 htab_remove_elt (include_hash, new_mac_ptr);
18543 }
18544 }
18545 break;
18546
18547 case DW_MACINFO_vendor_ext:
18548 if (!section_is_gnu)
18549 {
18550 unsigned int bytes_read;
18551 int constant;
18552
18553 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18554 mac_ptr += bytes_read;
18555 read_direct_string (abfd, mac_ptr, &bytes_read);
18556 mac_ptr += bytes_read;
18557
18558 /* We don't recognize any vendor extensions. */
18559 break;
18560 }
18561 /* FALLTHROUGH */
18562
18563 default:
18564 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18565 mac_ptr, mac_end, abfd, offset_size,
18566 section);
18567 if (mac_ptr == NULL)
18568 return;
18569 break;
18570 }
18571 } while (macinfo_type != 0);
18572 }
18573
18574 static void
18575 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18576 char *comp_dir, int section_is_gnu)
18577 {
18578 struct objfile *objfile = dwarf2_per_objfile->objfile;
18579 struct line_header *lh = cu->line_header;
18580 bfd *abfd;
18581 gdb_byte *mac_ptr, *mac_end;
18582 struct macro_source_file *current_file = 0;
18583 enum dwarf_macro_record_type macinfo_type;
18584 unsigned int offset_size = cu->header.offset_size;
18585 gdb_byte *opcode_definitions[256];
18586 struct cleanup *cleanup;
18587 htab_t include_hash;
18588 void **slot;
18589 struct dwarf2_section_info *section;
18590 const char *section_name;
18591
18592 if (cu->dwo_unit != NULL)
18593 {
18594 if (section_is_gnu)
18595 {
18596 section = &cu->dwo_unit->dwo_file->sections.macro;
18597 section_name = ".debug_macro.dwo";
18598 }
18599 else
18600 {
18601 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18602 section_name = ".debug_macinfo.dwo";
18603 }
18604 }
18605 else
18606 {
18607 if (section_is_gnu)
18608 {
18609 section = &dwarf2_per_objfile->macro;
18610 section_name = ".debug_macro";
18611 }
18612 else
18613 {
18614 section = &dwarf2_per_objfile->macinfo;
18615 section_name = ".debug_macinfo";
18616 }
18617 }
18618
18619 dwarf2_read_section (objfile, section);
18620 if (section->buffer == NULL)
18621 {
18622 complaint (&symfile_complaints, _("missing %s section"), section_name);
18623 return;
18624 }
18625 abfd = section->asection->owner;
18626
18627 /* First pass: Find the name of the base filename.
18628 This filename is needed in order to process all macros whose definition
18629 (or undefinition) comes from the command line. These macros are defined
18630 before the first DW_MACINFO_start_file entry, and yet still need to be
18631 associated to the base file.
18632
18633 To determine the base file name, we scan the macro definitions until we
18634 reach the first DW_MACINFO_start_file entry. We then initialize
18635 CURRENT_FILE accordingly so that any macro definition found before the
18636 first DW_MACINFO_start_file can still be associated to the base file. */
18637
18638 mac_ptr = section->buffer + offset;
18639 mac_end = section->buffer + section->size;
18640
18641 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18642 &offset_size, section_is_gnu);
18643 if (mac_ptr == NULL)
18644 {
18645 /* We already issued a complaint. */
18646 return;
18647 }
18648
18649 do
18650 {
18651 /* Do we at least have room for a macinfo type byte? */
18652 if (mac_ptr >= mac_end)
18653 {
18654 /* Complaint is printed during the second pass as GDB will probably
18655 stop the first pass earlier upon finding
18656 DW_MACINFO_start_file. */
18657 break;
18658 }
18659
18660 macinfo_type = read_1_byte (abfd, mac_ptr);
18661 mac_ptr++;
18662
18663 /* Note that we rely on the fact that the corresponding GNU and
18664 DWARF constants are the same. */
18665 switch (macinfo_type)
18666 {
18667 /* A zero macinfo type indicates the end of the macro
18668 information. */
18669 case 0:
18670 break;
18671
18672 case DW_MACRO_GNU_define:
18673 case DW_MACRO_GNU_undef:
18674 /* Only skip the data by MAC_PTR. */
18675 {
18676 unsigned int bytes_read;
18677
18678 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18679 mac_ptr += bytes_read;
18680 read_direct_string (abfd, mac_ptr, &bytes_read);
18681 mac_ptr += bytes_read;
18682 }
18683 break;
18684
18685 case DW_MACRO_GNU_start_file:
18686 {
18687 unsigned int bytes_read;
18688 int line, file;
18689
18690 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18691 mac_ptr += bytes_read;
18692 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18693 mac_ptr += bytes_read;
18694
18695 current_file = macro_start_file (file, line, current_file,
18696 comp_dir, lh, objfile);
18697 }
18698 break;
18699
18700 case DW_MACRO_GNU_end_file:
18701 /* No data to skip by MAC_PTR. */
18702 break;
18703
18704 case DW_MACRO_GNU_define_indirect:
18705 case DW_MACRO_GNU_undef_indirect:
18706 case DW_MACRO_GNU_define_indirect_alt:
18707 case DW_MACRO_GNU_undef_indirect_alt:
18708 {
18709 unsigned int bytes_read;
18710
18711 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18712 mac_ptr += bytes_read;
18713 mac_ptr += offset_size;
18714 }
18715 break;
18716
18717 case DW_MACRO_GNU_transparent_include:
18718 case DW_MACRO_GNU_transparent_include_alt:
18719 /* Note that, according to the spec, a transparent include
18720 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18721 skip this opcode. */
18722 mac_ptr += offset_size;
18723 break;
18724
18725 case DW_MACINFO_vendor_ext:
18726 /* Only skip the data by MAC_PTR. */
18727 if (!section_is_gnu)
18728 {
18729 unsigned int bytes_read;
18730
18731 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18732 mac_ptr += bytes_read;
18733 read_direct_string (abfd, mac_ptr, &bytes_read);
18734 mac_ptr += bytes_read;
18735 }
18736 /* FALLTHROUGH */
18737
18738 default:
18739 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18740 mac_ptr, mac_end, abfd, offset_size,
18741 section);
18742 if (mac_ptr == NULL)
18743 return;
18744 break;
18745 }
18746 } while (macinfo_type != 0 && current_file == NULL);
18747
18748 /* Second pass: Process all entries.
18749
18750 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18751 command-line macro definitions/undefinitions. This flag is unset when we
18752 reach the first DW_MACINFO_start_file entry. */
18753
18754 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18755 NULL, xcalloc, xfree);
18756 cleanup = make_cleanup_htab_delete (include_hash);
18757 mac_ptr = section->buffer + offset;
18758 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18759 *slot = mac_ptr;
18760 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18761 current_file, lh, comp_dir, section,
18762 section_is_gnu, 0,
18763 offset_size, objfile, include_hash);
18764 do_cleanups (cleanup);
18765 }
18766
18767 /* Check if the attribute's form is a DW_FORM_block*
18768 if so return true else false. */
18769
18770 static int
18771 attr_form_is_block (struct attribute *attr)
18772 {
18773 return (attr == NULL ? 0 :
18774 attr->form == DW_FORM_block1
18775 || attr->form == DW_FORM_block2
18776 || attr->form == DW_FORM_block4
18777 || attr->form == DW_FORM_block
18778 || attr->form == DW_FORM_exprloc);
18779 }
18780
18781 /* Return non-zero if ATTR's value is a section offset --- classes
18782 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18783 You may use DW_UNSND (attr) to retrieve such offsets.
18784
18785 Section 7.5.4, "Attribute Encodings", explains that no attribute
18786 may have a value that belongs to more than one of these classes; it
18787 would be ambiguous if we did, because we use the same forms for all
18788 of them. */
18789
18790 static int
18791 attr_form_is_section_offset (struct attribute *attr)
18792 {
18793 return (attr->form == DW_FORM_data4
18794 || attr->form == DW_FORM_data8
18795 || attr->form == DW_FORM_sec_offset);
18796 }
18797
18798 /* Return non-zero if ATTR's value falls in the 'constant' class, or
18799 zero otherwise. When this function returns true, you can apply
18800 dwarf2_get_attr_constant_value to it.
18801
18802 However, note that for some attributes you must check
18803 attr_form_is_section_offset before using this test. DW_FORM_data4
18804 and DW_FORM_data8 are members of both the constant class, and of
18805 the classes that contain offsets into other debug sections
18806 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18807 that, if an attribute's can be either a constant or one of the
18808 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18809 taken as section offsets, not constants. */
18810
18811 static int
18812 attr_form_is_constant (struct attribute *attr)
18813 {
18814 switch (attr->form)
18815 {
18816 case DW_FORM_sdata:
18817 case DW_FORM_udata:
18818 case DW_FORM_data1:
18819 case DW_FORM_data2:
18820 case DW_FORM_data4:
18821 case DW_FORM_data8:
18822 return 1;
18823 default:
18824 return 0;
18825 }
18826 }
18827
18828 /* Return the .debug_loc section to use for CU.
18829 For DWO files use .debug_loc.dwo. */
18830
18831 static struct dwarf2_section_info *
18832 cu_debug_loc_section (struct dwarf2_cu *cu)
18833 {
18834 if (cu->dwo_unit)
18835 return &cu->dwo_unit->dwo_file->sections.loc;
18836 return &dwarf2_per_objfile->loc;
18837 }
18838
18839 /* A helper function that fills in a dwarf2_loclist_baton. */
18840
18841 static void
18842 fill_in_loclist_baton (struct dwarf2_cu *cu,
18843 struct dwarf2_loclist_baton *baton,
18844 struct attribute *attr)
18845 {
18846 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18847
18848 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18849
18850 baton->per_cu = cu->per_cu;
18851 gdb_assert (baton->per_cu);
18852 /* We don't know how long the location list is, but make sure we
18853 don't run off the edge of the section. */
18854 baton->size = section->size - DW_UNSND (attr);
18855 baton->data = section->buffer + DW_UNSND (attr);
18856 baton->base_address = cu->base_address;
18857 baton->from_dwo = cu->dwo_unit != NULL;
18858 }
18859
18860 static void
18861 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18862 struct dwarf2_cu *cu)
18863 {
18864 struct objfile *objfile = dwarf2_per_objfile->objfile;
18865 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18866
18867 if (attr_form_is_section_offset (attr)
18868 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18869 the section. If so, fall through to the complaint in the
18870 other branch. */
18871 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18872 {
18873 struct dwarf2_loclist_baton *baton;
18874
18875 baton = obstack_alloc (&objfile->objfile_obstack,
18876 sizeof (struct dwarf2_loclist_baton));
18877
18878 fill_in_loclist_baton (cu, baton, attr);
18879
18880 if (cu->base_known == 0)
18881 complaint (&symfile_complaints,
18882 _("Location list used without "
18883 "specifying the CU base address."));
18884
18885 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18886 SYMBOL_LOCATION_BATON (sym) = baton;
18887 }
18888 else
18889 {
18890 struct dwarf2_locexpr_baton *baton;
18891
18892 baton = obstack_alloc (&objfile->objfile_obstack,
18893 sizeof (struct dwarf2_locexpr_baton));
18894 baton->per_cu = cu->per_cu;
18895 gdb_assert (baton->per_cu);
18896
18897 if (attr_form_is_block (attr))
18898 {
18899 /* Note that we're just copying the block's data pointer
18900 here, not the actual data. We're still pointing into the
18901 info_buffer for SYM's objfile; right now we never release
18902 that buffer, but when we do clean up properly this may
18903 need to change. */
18904 baton->size = DW_BLOCK (attr)->size;
18905 baton->data = DW_BLOCK (attr)->data;
18906 }
18907 else
18908 {
18909 dwarf2_invalid_attrib_class_complaint ("location description",
18910 SYMBOL_NATURAL_NAME (sym));
18911 baton->size = 0;
18912 }
18913
18914 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18915 SYMBOL_LOCATION_BATON (sym) = baton;
18916 }
18917 }
18918
18919 /* Return the OBJFILE associated with the compilation unit CU. If CU
18920 came from a separate debuginfo file, then the master objfile is
18921 returned. */
18922
18923 struct objfile *
18924 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18925 {
18926 struct objfile *objfile = per_cu->objfile;
18927
18928 /* Return the master objfile, so that we can report and look up the
18929 correct file containing this variable. */
18930 if (objfile->separate_debug_objfile_backlink)
18931 objfile = objfile->separate_debug_objfile_backlink;
18932
18933 return objfile;
18934 }
18935
18936 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18937 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18938 CU_HEADERP first. */
18939
18940 static const struct comp_unit_head *
18941 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18942 struct dwarf2_per_cu_data *per_cu)
18943 {
18944 gdb_byte *info_ptr;
18945
18946 if (per_cu->cu)
18947 return &per_cu->cu->header;
18948
18949 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18950
18951 memset (cu_headerp, 0, sizeof (*cu_headerp));
18952 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18953
18954 return cu_headerp;
18955 }
18956
18957 /* Return the address size given in the compilation unit header for CU. */
18958
18959 int
18960 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18961 {
18962 struct comp_unit_head cu_header_local;
18963 const struct comp_unit_head *cu_headerp;
18964
18965 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18966
18967 return cu_headerp->addr_size;
18968 }
18969
18970 /* Return the offset size given in the compilation unit header for CU. */
18971
18972 int
18973 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18974 {
18975 struct comp_unit_head cu_header_local;
18976 const struct comp_unit_head *cu_headerp;
18977
18978 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18979
18980 return cu_headerp->offset_size;
18981 }
18982
18983 /* See its dwarf2loc.h declaration. */
18984
18985 int
18986 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18987 {
18988 struct comp_unit_head cu_header_local;
18989 const struct comp_unit_head *cu_headerp;
18990
18991 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18992
18993 if (cu_headerp->version == 2)
18994 return cu_headerp->addr_size;
18995 else
18996 return cu_headerp->offset_size;
18997 }
18998
18999 /* Return the text offset of the CU. The returned offset comes from
19000 this CU's objfile. If this objfile came from a separate debuginfo
19001 file, then the offset may be different from the corresponding
19002 offset in the parent objfile. */
19003
19004 CORE_ADDR
19005 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19006 {
19007 struct objfile *objfile = per_cu->objfile;
19008
19009 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19010 }
19011
19012 /* Locate the .debug_info compilation unit from CU's objfile which contains
19013 the DIE at OFFSET. Raises an error on failure. */
19014
19015 static struct dwarf2_per_cu_data *
19016 dwarf2_find_containing_comp_unit (sect_offset offset,
19017 unsigned int offset_in_dwz,
19018 struct objfile *objfile)
19019 {
19020 struct dwarf2_per_cu_data *this_cu;
19021 int low, high;
19022 const sect_offset *cu_off;
19023
19024 low = 0;
19025 high = dwarf2_per_objfile->n_comp_units - 1;
19026 while (high > low)
19027 {
19028 struct dwarf2_per_cu_data *mid_cu;
19029 int mid = low + (high - low) / 2;
19030
19031 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19032 cu_off = &mid_cu->offset;
19033 if (mid_cu->is_dwz > offset_in_dwz
19034 || (mid_cu->is_dwz == offset_in_dwz
19035 && cu_off->sect_off >= offset.sect_off))
19036 high = mid;
19037 else
19038 low = mid + 1;
19039 }
19040 gdb_assert (low == high);
19041 this_cu = dwarf2_per_objfile->all_comp_units[low];
19042 cu_off = &this_cu->offset;
19043 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19044 {
19045 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19046 error (_("Dwarf Error: could not find partial DIE containing "
19047 "offset 0x%lx [in module %s]"),
19048 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19049
19050 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19051 <= offset.sect_off);
19052 return dwarf2_per_objfile->all_comp_units[low-1];
19053 }
19054 else
19055 {
19056 this_cu = dwarf2_per_objfile->all_comp_units[low];
19057 if (low == dwarf2_per_objfile->n_comp_units - 1
19058 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19059 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19060 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19061 return this_cu;
19062 }
19063 }
19064
19065 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19066
19067 static void
19068 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19069 {
19070 memset (cu, 0, sizeof (*cu));
19071 per_cu->cu = cu;
19072 cu->per_cu = per_cu;
19073 cu->objfile = per_cu->objfile;
19074 obstack_init (&cu->comp_unit_obstack);
19075 }
19076
19077 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19078
19079 static void
19080 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19081 enum language pretend_language)
19082 {
19083 struct attribute *attr;
19084
19085 /* Set the language we're debugging. */
19086 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19087 if (attr)
19088 set_cu_language (DW_UNSND (attr), cu);
19089 else
19090 {
19091 cu->language = pretend_language;
19092 cu->language_defn = language_def (cu->language);
19093 }
19094
19095 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19096 if (attr)
19097 cu->producer = DW_STRING (attr);
19098 }
19099
19100 /* Release one cached compilation unit, CU. We unlink it from the tree
19101 of compilation units, but we don't remove it from the read_in_chain;
19102 the caller is responsible for that.
19103 NOTE: DATA is a void * because this function is also used as a
19104 cleanup routine. */
19105
19106 static void
19107 free_heap_comp_unit (void *data)
19108 {
19109 struct dwarf2_cu *cu = data;
19110
19111 gdb_assert (cu->per_cu != NULL);
19112 cu->per_cu->cu = NULL;
19113 cu->per_cu = NULL;
19114
19115 obstack_free (&cu->comp_unit_obstack, NULL);
19116
19117 xfree (cu);
19118 }
19119
19120 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19121 when we're finished with it. We can't free the pointer itself, but be
19122 sure to unlink it from the cache. Also release any associated storage. */
19123
19124 static void
19125 free_stack_comp_unit (void *data)
19126 {
19127 struct dwarf2_cu *cu = data;
19128
19129 gdb_assert (cu->per_cu != NULL);
19130 cu->per_cu->cu = NULL;
19131 cu->per_cu = NULL;
19132
19133 obstack_free (&cu->comp_unit_obstack, NULL);
19134 cu->partial_dies = NULL;
19135 }
19136
19137 /* Free all cached compilation units. */
19138
19139 static void
19140 free_cached_comp_units (void *data)
19141 {
19142 struct dwarf2_per_cu_data *per_cu, **last_chain;
19143
19144 per_cu = dwarf2_per_objfile->read_in_chain;
19145 last_chain = &dwarf2_per_objfile->read_in_chain;
19146 while (per_cu != NULL)
19147 {
19148 struct dwarf2_per_cu_data *next_cu;
19149
19150 next_cu = per_cu->cu->read_in_chain;
19151
19152 free_heap_comp_unit (per_cu->cu);
19153 *last_chain = next_cu;
19154
19155 per_cu = next_cu;
19156 }
19157 }
19158
19159 /* Increase the age counter on each cached compilation unit, and free
19160 any that are too old. */
19161
19162 static void
19163 age_cached_comp_units (void)
19164 {
19165 struct dwarf2_per_cu_data *per_cu, **last_chain;
19166
19167 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19168 per_cu = dwarf2_per_objfile->read_in_chain;
19169 while (per_cu != NULL)
19170 {
19171 per_cu->cu->last_used ++;
19172 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19173 dwarf2_mark (per_cu->cu);
19174 per_cu = per_cu->cu->read_in_chain;
19175 }
19176
19177 per_cu = dwarf2_per_objfile->read_in_chain;
19178 last_chain = &dwarf2_per_objfile->read_in_chain;
19179 while (per_cu != NULL)
19180 {
19181 struct dwarf2_per_cu_data *next_cu;
19182
19183 next_cu = per_cu->cu->read_in_chain;
19184
19185 if (!per_cu->cu->mark)
19186 {
19187 free_heap_comp_unit (per_cu->cu);
19188 *last_chain = next_cu;
19189 }
19190 else
19191 last_chain = &per_cu->cu->read_in_chain;
19192
19193 per_cu = next_cu;
19194 }
19195 }
19196
19197 /* Remove a single compilation unit from the cache. */
19198
19199 static void
19200 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19201 {
19202 struct dwarf2_per_cu_data *per_cu, **last_chain;
19203
19204 per_cu = dwarf2_per_objfile->read_in_chain;
19205 last_chain = &dwarf2_per_objfile->read_in_chain;
19206 while (per_cu != NULL)
19207 {
19208 struct dwarf2_per_cu_data *next_cu;
19209
19210 next_cu = per_cu->cu->read_in_chain;
19211
19212 if (per_cu == target_per_cu)
19213 {
19214 free_heap_comp_unit (per_cu->cu);
19215 per_cu->cu = NULL;
19216 *last_chain = next_cu;
19217 break;
19218 }
19219 else
19220 last_chain = &per_cu->cu->read_in_chain;
19221
19222 per_cu = next_cu;
19223 }
19224 }
19225
19226 /* Release all extra memory associated with OBJFILE. */
19227
19228 void
19229 dwarf2_free_objfile (struct objfile *objfile)
19230 {
19231 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19232
19233 if (dwarf2_per_objfile == NULL)
19234 return;
19235
19236 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19237 free_cached_comp_units (NULL);
19238
19239 if (dwarf2_per_objfile->quick_file_names_table)
19240 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19241
19242 /* Everything else should be on the objfile obstack. */
19243 }
19244
19245 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19246 We store these in a hash table separate from the DIEs, and preserve them
19247 when the DIEs are flushed out of cache.
19248
19249 The CU "per_cu" pointer is needed because offset alone is not enough to
19250 uniquely identify the type. A file may have multiple .debug_types sections,
19251 or the type may come from a DWO file. We have to use something in
19252 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19253 routine, get_die_type_at_offset, from outside this file, and thus won't
19254 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19255 of the objfile. */
19256
19257 struct dwarf2_per_cu_offset_and_type
19258 {
19259 const struct dwarf2_per_cu_data *per_cu;
19260 sect_offset offset;
19261 struct type *type;
19262 };
19263
19264 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19265
19266 static hashval_t
19267 per_cu_offset_and_type_hash (const void *item)
19268 {
19269 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19270
19271 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19272 }
19273
19274 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19275
19276 static int
19277 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19278 {
19279 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19280 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19281
19282 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19283 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19284 }
19285
19286 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19287 table if necessary. For convenience, return TYPE.
19288
19289 The DIEs reading must have careful ordering to:
19290 * Not cause infite loops trying to read in DIEs as a prerequisite for
19291 reading current DIE.
19292 * Not trying to dereference contents of still incompletely read in types
19293 while reading in other DIEs.
19294 * Enable referencing still incompletely read in types just by a pointer to
19295 the type without accessing its fields.
19296
19297 Therefore caller should follow these rules:
19298 * Try to fetch any prerequisite types we may need to build this DIE type
19299 before building the type and calling set_die_type.
19300 * After building type call set_die_type for current DIE as soon as
19301 possible before fetching more types to complete the current type.
19302 * Make the type as complete as possible before fetching more types. */
19303
19304 static struct type *
19305 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19306 {
19307 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19308 struct objfile *objfile = cu->objfile;
19309
19310 /* For Ada types, make sure that the gnat-specific data is always
19311 initialized (if not already set). There are a few types where
19312 we should not be doing so, because the type-specific area is
19313 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19314 where the type-specific area is used to store the floatformat).
19315 But this is not a problem, because the gnat-specific information
19316 is actually not needed for these types. */
19317 if (need_gnat_info (cu)
19318 && TYPE_CODE (type) != TYPE_CODE_FUNC
19319 && TYPE_CODE (type) != TYPE_CODE_FLT
19320 && !HAVE_GNAT_AUX_INFO (type))
19321 INIT_GNAT_SPECIFIC (type);
19322
19323 if (dwarf2_per_objfile->die_type_hash == NULL)
19324 {
19325 dwarf2_per_objfile->die_type_hash =
19326 htab_create_alloc_ex (127,
19327 per_cu_offset_and_type_hash,
19328 per_cu_offset_and_type_eq,
19329 NULL,
19330 &objfile->objfile_obstack,
19331 hashtab_obstack_allocate,
19332 dummy_obstack_deallocate);
19333 }
19334
19335 ofs.per_cu = cu->per_cu;
19336 ofs.offset = die->offset;
19337 ofs.type = type;
19338 slot = (struct dwarf2_per_cu_offset_and_type **)
19339 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19340 if (*slot)
19341 complaint (&symfile_complaints,
19342 _("A problem internal to GDB: DIE 0x%x has type already set"),
19343 die->offset.sect_off);
19344 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19345 **slot = ofs;
19346 return type;
19347 }
19348
19349 /* Look up the type for the die at OFFSET in the appropriate type_hash
19350 table, or return NULL if the die does not have a saved type. */
19351
19352 static struct type *
19353 get_die_type_at_offset (sect_offset offset,
19354 struct dwarf2_per_cu_data *per_cu)
19355 {
19356 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19357
19358 if (dwarf2_per_objfile->die_type_hash == NULL)
19359 return NULL;
19360
19361 ofs.per_cu = per_cu;
19362 ofs.offset = offset;
19363 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19364 if (slot)
19365 return slot->type;
19366 else
19367 return NULL;
19368 }
19369
19370 /* Look up the type for DIE in the appropriate type_hash table,
19371 or return NULL if DIE does not have a saved type. */
19372
19373 static struct type *
19374 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19375 {
19376 return get_die_type_at_offset (die->offset, cu->per_cu);
19377 }
19378
19379 /* Add a dependence relationship from CU to REF_PER_CU. */
19380
19381 static void
19382 dwarf2_add_dependence (struct dwarf2_cu *cu,
19383 struct dwarf2_per_cu_data *ref_per_cu)
19384 {
19385 void **slot;
19386
19387 if (cu->dependencies == NULL)
19388 cu->dependencies
19389 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19390 NULL, &cu->comp_unit_obstack,
19391 hashtab_obstack_allocate,
19392 dummy_obstack_deallocate);
19393
19394 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19395 if (*slot == NULL)
19396 *slot = ref_per_cu;
19397 }
19398
19399 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19400 Set the mark field in every compilation unit in the
19401 cache that we must keep because we are keeping CU. */
19402
19403 static int
19404 dwarf2_mark_helper (void **slot, void *data)
19405 {
19406 struct dwarf2_per_cu_data *per_cu;
19407
19408 per_cu = (struct dwarf2_per_cu_data *) *slot;
19409
19410 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19411 reading of the chain. As such dependencies remain valid it is not much
19412 useful to track and undo them during QUIT cleanups. */
19413 if (per_cu->cu == NULL)
19414 return 1;
19415
19416 if (per_cu->cu->mark)
19417 return 1;
19418 per_cu->cu->mark = 1;
19419
19420 if (per_cu->cu->dependencies != NULL)
19421 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19422
19423 return 1;
19424 }
19425
19426 /* Set the mark field in CU and in every other compilation unit in the
19427 cache that we must keep because we are keeping CU. */
19428
19429 static void
19430 dwarf2_mark (struct dwarf2_cu *cu)
19431 {
19432 if (cu->mark)
19433 return;
19434 cu->mark = 1;
19435 if (cu->dependencies != NULL)
19436 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19437 }
19438
19439 static void
19440 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19441 {
19442 while (per_cu)
19443 {
19444 per_cu->cu->mark = 0;
19445 per_cu = per_cu->cu->read_in_chain;
19446 }
19447 }
19448
19449 /* Trivial hash function for partial_die_info: the hash value of a DIE
19450 is its offset in .debug_info for this objfile. */
19451
19452 static hashval_t
19453 partial_die_hash (const void *item)
19454 {
19455 const struct partial_die_info *part_die = item;
19456
19457 return part_die->offset.sect_off;
19458 }
19459
19460 /* Trivial comparison function for partial_die_info structures: two DIEs
19461 are equal if they have the same offset. */
19462
19463 static int
19464 partial_die_eq (const void *item_lhs, const void *item_rhs)
19465 {
19466 const struct partial_die_info *part_die_lhs = item_lhs;
19467 const struct partial_die_info *part_die_rhs = item_rhs;
19468
19469 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19470 }
19471
19472 static struct cmd_list_element *set_dwarf2_cmdlist;
19473 static struct cmd_list_element *show_dwarf2_cmdlist;
19474
19475 static void
19476 set_dwarf2_cmd (char *args, int from_tty)
19477 {
19478 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19479 }
19480
19481 static void
19482 show_dwarf2_cmd (char *args, int from_tty)
19483 {
19484 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19485 }
19486
19487 /* Free data associated with OBJFILE, if necessary. */
19488
19489 static void
19490 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19491 {
19492 struct dwarf2_per_objfile *data = d;
19493 int ix;
19494
19495 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19496 VEC_free (dwarf2_per_cu_ptr,
19497 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
19498
19499 VEC_free (dwarf2_section_info_def, data->types);
19500
19501 if (data->dwo_files)
19502 free_dwo_files (data->dwo_files, objfile);
19503
19504 if (data->dwz_file && data->dwz_file->dwz_bfd)
19505 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19506 }
19507
19508 \f
19509 /* The "save gdb-index" command. */
19510
19511 /* The contents of the hash table we create when building the string
19512 table. */
19513 struct strtab_entry
19514 {
19515 offset_type offset;
19516 const char *str;
19517 };
19518
19519 /* Hash function for a strtab_entry.
19520
19521 Function is used only during write_hash_table so no index format backward
19522 compatibility is needed. */
19523
19524 static hashval_t
19525 hash_strtab_entry (const void *e)
19526 {
19527 const struct strtab_entry *entry = e;
19528 return mapped_index_string_hash (INT_MAX, entry->str);
19529 }
19530
19531 /* Equality function for a strtab_entry. */
19532
19533 static int
19534 eq_strtab_entry (const void *a, const void *b)
19535 {
19536 const struct strtab_entry *ea = a;
19537 const struct strtab_entry *eb = b;
19538 return !strcmp (ea->str, eb->str);
19539 }
19540
19541 /* Create a strtab_entry hash table. */
19542
19543 static htab_t
19544 create_strtab (void)
19545 {
19546 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19547 xfree, xcalloc, xfree);
19548 }
19549
19550 /* Add a string to the constant pool. Return the string's offset in
19551 host order. */
19552
19553 static offset_type
19554 add_string (htab_t table, struct obstack *cpool, const char *str)
19555 {
19556 void **slot;
19557 struct strtab_entry entry;
19558 struct strtab_entry *result;
19559
19560 entry.str = str;
19561 slot = htab_find_slot (table, &entry, INSERT);
19562 if (*slot)
19563 result = *slot;
19564 else
19565 {
19566 result = XNEW (struct strtab_entry);
19567 result->offset = obstack_object_size (cpool);
19568 result->str = str;
19569 obstack_grow_str0 (cpool, str);
19570 *slot = result;
19571 }
19572 return result->offset;
19573 }
19574
19575 /* An entry in the symbol table. */
19576 struct symtab_index_entry
19577 {
19578 /* The name of the symbol. */
19579 const char *name;
19580 /* The offset of the name in the constant pool. */
19581 offset_type index_offset;
19582 /* A sorted vector of the indices of all the CUs that hold an object
19583 of this name. */
19584 VEC (offset_type) *cu_indices;
19585 };
19586
19587 /* The symbol table. This is a power-of-2-sized hash table. */
19588 struct mapped_symtab
19589 {
19590 offset_type n_elements;
19591 offset_type size;
19592 struct symtab_index_entry **data;
19593 };
19594
19595 /* Hash function for a symtab_index_entry. */
19596
19597 static hashval_t
19598 hash_symtab_entry (const void *e)
19599 {
19600 const struct symtab_index_entry *entry = e;
19601 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19602 sizeof (offset_type) * VEC_length (offset_type,
19603 entry->cu_indices),
19604 0);
19605 }
19606
19607 /* Equality function for a symtab_index_entry. */
19608
19609 static int
19610 eq_symtab_entry (const void *a, const void *b)
19611 {
19612 const struct symtab_index_entry *ea = a;
19613 const struct symtab_index_entry *eb = b;
19614 int len = VEC_length (offset_type, ea->cu_indices);
19615 if (len != VEC_length (offset_type, eb->cu_indices))
19616 return 0;
19617 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19618 VEC_address (offset_type, eb->cu_indices),
19619 sizeof (offset_type) * len);
19620 }
19621
19622 /* Destroy a symtab_index_entry. */
19623
19624 static void
19625 delete_symtab_entry (void *p)
19626 {
19627 struct symtab_index_entry *entry = p;
19628 VEC_free (offset_type, entry->cu_indices);
19629 xfree (entry);
19630 }
19631
19632 /* Create a hash table holding symtab_index_entry objects. */
19633
19634 static htab_t
19635 create_symbol_hash_table (void)
19636 {
19637 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19638 delete_symtab_entry, xcalloc, xfree);
19639 }
19640
19641 /* Create a new mapped symtab object. */
19642
19643 static struct mapped_symtab *
19644 create_mapped_symtab (void)
19645 {
19646 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19647 symtab->n_elements = 0;
19648 symtab->size = 1024;
19649 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19650 return symtab;
19651 }
19652
19653 /* Destroy a mapped_symtab. */
19654
19655 static void
19656 cleanup_mapped_symtab (void *p)
19657 {
19658 struct mapped_symtab *symtab = p;
19659 /* The contents of the array are freed when the other hash table is
19660 destroyed. */
19661 xfree (symtab->data);
19662 xfree (symtab);
19663 }
19664
19665 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19666 the slot.
19667
19668 Function is used only during write_hash_table so no index format backward
19669 compatibility is needed. */
19670
19671 static struct symtab_index_entry **
19672 find_slot (struct mapped_symtab *symtab, const char *name)
19673 {
19674 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19675
19676 index = hash & (symtab->size - 1);
19677 step = ((hash * 17) & (symtab->size - 1)) | 1;
19678
19679 for (;;)
19680 {
19681 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19682 return &symtab->data[index];
19683 index = (index + step) & (symtab->size - 1);
19684 }
19685 }
19686
19687 /* Expand SYMTAB's hash table. */
19688
19689 static void
19690 hash_expand (struct mapped_symtab *symtab)
19691 {
19692 offset_type old_size = symtab->size;
19693 offset_type i;
19694 struct symtab_index_entry **old_entries = symtab->data;
19695
19696 symtab->size *= 2;
19697 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19698
19699 for (i = 0; i < old_size; ++i)
19700 {
19701 if (old_entries[i])
19702 {
19703 struct symtab_index_entry **slot = find_slot (symtab,
19704 old_entries[i]->name);
19705 *slot = old_entries[i];
19706 }
19707 }
19708
19709 xfree (old_entries);
19710 }
19711
19712 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19713 CU_INDEX is the index of the CU in which the symbol appears.
19714 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19715
19716 static void
19717 add_index_entry (struct mapped_symtab *symtab, const char *name,
19718 int is_static, gdb_index_symbol_kind kind,
19719 offset_type cu_index)
19720 {
19721 struct symtab_index_entry **slot;
19722 offset_type cu_index_and_attrs;
19723
19724 ++symtab->n_elements;
19725 if (4 * symtab->n_elements / 3 >= symtab->size)
19726 hash_expand (symtab);
19727
19728 slot = find_slot (symtab, name);
19729 if (!*slot)
19730 {
19731 *slot = XNEW (struct symtab_index_entry);
19732 (*slot)->name = name;
19733 /* index_offset is set later. */
19734 (*slot)->cu_indices = NULL;
19735 }
19736
19737 cu_index_and_attrs = 0;
19738 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19739 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19740 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19741
19742 /* We don't want to record an index value twice as we want to avoid the
19743 duplication.
19744 We process all global symbols and then all static symbols
19745 (which would allow us to avoid the duplication by only having to check
19746 the last entry pushed), but a symbol could have multiple kinds in one CU.
19747 To keep things simple we don't worry about the duplication here and
19748 sort and uniqufy the list after we've processed all symbols. */
19749 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19750 }
19751
19752 /* qsort helper routine for uniquify_cu_indices. */
19753
19754 static int
19755 offset_type_compare (const void *ap, const void *bp)
19756 {
19757 offset_type a = *(offset_type *) ap;
19758 offset_type b = *(offset_type *) bp;
19759
19760 return (a > b) - (b > a);
19761 }
19762
19763 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19764
19765 static void
19766 uniquify_cu_indices (struct mapped_symtab *symtab)
19767 {
19768 int i;
19769
19770 for (i = 0; i < symtab->size; ++i)
19771 {
19772 struct symtab_index_entry *entry = symtab->data[i];
19773
19774 if (entry
19775 && entry->cu_indices != NULL)
19776 {
19777 unsigned int next_to_insert, next_to_check;
19778 offset_type last_value;
19779
19780 qsort (VEC_address (offset_type, entry->cu_indices),
19781 VEC_length (offset_type, entry->cu_indices),
19782 sizeof (offset_type), offset_type_compare);
19783
19784 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19785 next_to_insert = 1;
19786 for (next_to_check = 1;
19787 next_to_check < VEC_length (offset_type, entry->cu_indices);
19788 ++next_to_check)
19789 {
19790 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19791 != last_value)
19792 {
19793 last_value = VEC_index (offset_type, entry->cu_indices,
19794 next_to_check);
19795 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19796 last_value);
19797 ++next_to_insert;
19798 }
19799 }
19800 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19801 }
19802 }
19803 }
19804
19805 /* Add a vector of indices to the constant pool. */
19806
19807 static offset_type
19808 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19809 struct symtab_index_entry *entry)
19810 {
19811 void **slot;
19812
19813 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19814 if (!*slot)
19815 {
19816 offset_type len = VEC_length (offset_type, entry->cu_indices);
19817 offset_type val = MAYBE_SWAP (len);
19818 offset_type iter;
19819 int i;
19820
19821 *slot = entry;
19822 entry->index_offset = obstack_object_size (cpool);
19823
19824 obstack_grow (cpool, &val, sizeof (val));
19825 for (i = 0;
19826 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19827 ++i)
19828 {
19829 val = MAYBE_SWAP (iter);
19830 obstack_grow (cpool, &val, sizeof (val));
19831 }
19832 }
19833 else
19834 {
19835 struct symtab_index_entry *old_entry = *slot;
19836 entry->index_offset = old_entry->index_offset;
19837 entry = old_entry;
19838 }
19839 return entry->index_offset;
19840 }
19841
19842 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19843 constant pool entries going into the obstack CPOOL. */
19844
19845 static void
19846 write_hash_table (struct mapped_symtab *symtab,
19847 struct obstack *output, struct obstack *cpool)
19848 {
19849 offset_type i;
19850 htab_t symbol_hash_table;
19851 htab_t str_table;
19852
19853 symbol_hash_table = create_symbol_hash_table ();
19854 str_table = create_strtab ();
19855
19856 /* We add all the index vectors to the constant pool first, to
19857 ensure alignment is ok. */
19858 for (i = 0; i < symtab->size; ++i)
19859 {
19860 if (symtab->data[i])
19861 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19862 }
19863
19864 /* Now write out the hash table. */
19865 for (i = 0; i < symtab->size; ++i)
19866 {
19867 offset_type str_off, vec_off;
19868
19869 if (symtab->data[i])
19870 {
19871 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19872 vec_off = symtab->data[i]->index_offset;
19873 }
19874 else
19875 {
19876 /* While 0 is a valid constant pool index, it is not valid
19877 to have 0 for both offsets. */
19878 str_off = 0;
19879 vec_off = 0;
19880 }
19881
19882 str_off = MAYBE_SWAP (str_off);
19883 vec_off = MAYBE_SWAP (vec_off);
19884
19885 obstack_grow (output, &str_off, sizeof (str_off));
19886 obstack_grow (output, &vec_off, sizeof (vec_off));
19887 }
19888
19889 htab_delete (str_table);
19890 htab_delete (symbol_hash_table);
19891 }
19892
19893 /* Struct to map psymtab to CU index in the index file. */
19894 struct psymtab_cu_index_map
19895 {
19896 struct partial_symtab *psymtab;
19897 unsigned int cu_index;
19898 };
19899
19900 static hashval_t
19901 hash_psymtab_cu_index (const void *item)
19902 {
19903 const struct psymtab_cu_index_map *map = item;
19904
19905 return htab_hash_pointer (map->psymtab);
19906 }
19907
19908 static int
19909 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19910 {
19911 const struct psymtab_cu_index_map *lhs = item_lhs;
19912 const struct psymtab_cu_index_map *rhs = item_rhs;
19913
19914 return lhs->psymtab == rhs->psymtab;
19915 }
19916
19917 /* Helper struct for building the address table. */
19918 struct addrmap_index_data
19919 {
19920 struct objfile *objfile;
19921 struct obstack *addr_obstack;
19922 htab_t cu_index_htab;
19923
19924 /* Non-zero if the previous_* fields are valid.
19925 We can't write an entry until we see the next entry (since it is only then
19926 that we know the end of the entry). */
19927 int previous_valid;
19928 /* Index of the CU in the table of all CUs in the index file. */
19929 unsigned int previous_cu_index;
19930 /* Start address of the CU. */
19931 CORE_ADDR previous_cu_start;
19932 };
19933
19934 /* Write an address entry to OBSTACK. */
19935
19936 static void
19937 add_address_entry (struct objfile *objfile, struct obstack *obstack,
19938 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19939 {
19940 offset_type cu_index_to_write;
19941 char addr[8];
19942 CORE_ADDR baseaddr;
19943
19944 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19945
19946 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19947 obstack_grow (obstack, addr, 8);
19948 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19949 obstack_grow (obstack, addr, 8);
19950 cu_index_to_write = MAYBE_SWAP (cu_index);
19951 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19952 }
19953
19954 /* Worker function for traversing an addrmap to build the address table. */
19955
19956 static int
19957 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19958 {
19959 struct addrmap_index_data *data = datap;
19960 struct partial_symtab *pst = obj;
19961
19962 if (data->previous_valid)
19963 add_address_entry (data->objfile, data->addr_obstack,
19964 data->previous_cu_start, start_addr,
19965 data->previous_cu_index);
19966
19967 data->previous_cu_start = start_addr;
19968 if (pst != NULL)
19969 {
19970 struct psymtab_cu_index_map find_map, *map;
19971 find_map.psymtab = pst;
19972 map = htab_find (data->cu_index_htab, &find_map);
19973 gdb_assert (map != NULL);
19974 data->previous_cu_index = map->cu_index;
19975 data->previous_valid = 1;
19976 }
19977 else
19978 data->previous_valid = 0;
19979
19980 return 0;
19981 }
19982
19983 /* Write OBJFILE's address map to OBSTACK.
19984 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19985 in the index file. */
19986
19987 static void
19988 write_address_map (struct objfile *objfile, struct obstack *obstack,
19989 htab_t cu_index_htab)
19990 {
19991 struct addrmap_index_data addrmap_index_data;
19992
19993 /* When writing the address table, we have to cope with the fact that
19994 the addrmap iterator only provides the start of a region; we have to
19995 wait until the next invocation to get the start of the next region. */
19996
19997 addrmap_index_data.objfile = objfile;
19998 addrmap_index_data.addr_obstack = obstack;
19999 addrmap_index_data.cu_index_htab = cu_index_htab;
20000 addrmap_index_data.previous_valid = 0;
20001
20002 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20003 &addrmap_index_data);
20004
20005 /* It's highly unlikely the last entry (end address = 0xff...ff)
20006 is valid, but we should still handle it.
20007 The end address is recorded as the start of the next region, but that
20008 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20009 anyway. */
20010 if (addrmap_index_data.previous_valid)
20011 add_address_entry (objfile, obstack,
20012 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20013 addrmap_index_data.previous_cu_index);
20014 }
20015
20016 /* Return the symbol kind of PSYM. */
20017
20018 static gdb_index_symbol_kind
20019 symbol_kind (struct partial_symbol *psym)
20020 {
20021 domain_enum domain = PSYMBOL_DOMAIN (psym);
20022 enum address_class aclass = PSYMBOL_CLASS (psym);
20023
20024 switch (domain)
20025 {
20026 case VAR_DOMAIN:
20027 switch (aclass)
20028 {
20029 case LOC_BLOCK:
20030 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20031 case LOC_TYPEDEF:
20032 return GDB_INDEX_SYMBOL_KIND_TYPE;
20033 case LOC_COMPUTED:
20034 case LOC_CONST_BYTES:
20035 case LOC_OPTIMIZED_OUT:
20036 case LOC_STATIC:
20037 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20038 case LOC_CONST:
20039 /* Note: It's currently impossible to recognize psyms as enum values
20040 short of reading the type info. For now punt. */
20041 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20042 default:
20043 /* There are other LOC_FOO values that one might want to classify
20044 as variables, but dwarf2read.c doesn't currently use them. */
20045 return GDB_INDEX_SYMBOL_KIND_OTHER;
20046 }
20047 case STRUCT_DOMAIN:
20048 return GDB_INDEX_SYMBOL_KIND_TYPE;
20049 default:
20050 return GDB_INDEX_SYMBOL_KIND_OTHER;
20051 }
20052 }
20053
20054 /* Add a list of partial symbols to SYMTAB. */
20055
20056 static void
20057 write_psymbols (struct mapped_symtab *symtab,
20058 htab_t psyms_seen,
20059 struct partial_symbol **psymp,
20060 int count,
20061 offset_type cu_index,
20062 int is_static)
20063 {
20064 for (; count-- > 0; ++psymp)
20065 {
20066 struct partial_symbol *psym = *psymp;
20067 void **slot;
20068
20069 if (SYMBOL_LANGUAGE (psym) == language_ada)
20070 error (_("Ada is not currently supported by the index"));
20071
20072 /* Only add a given psymbol once. */
20073 slot = htab_find_slot (psyms_seen, psym, INSERT);
20074 if (!*slot)
20075 {
20076 gdb_index_symbol_kind kind = symbol_kind (psym);
20077
20078 *slot = psym;
20079 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20080 is_static, kind, cu_index);
20081 }
20082 }
20083 }
20084
20085 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20086 exception if there is an error. */
20087
20088 static void
20089 write_obstack (FILE *file, struct obstack *obstack)
20090 {
20091 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20092 file)
20093 != obstack_object_size (obstack))
20094 error (_("couldn't data write to file"));
20095 }
20096
20097 /* Unlink a file if the argument is not NULL. */
20098
20099 static void
20100 unlink_if_set (void *p)
20101 {
20102 char **filename = p;
20103 if (*filename)
20104 unlink (*filename);
20105 }
20106
20107 /* A helper struct used when iterating over debug_types. */
20108 struct signatured_type_index_data
20109 {
20110 struct objfile *objfile;
20111 struct mapped_symtab *symtab;
20112 struct obstack *types_list;
20113 htab_t psyms_seen;
20114 int cu_index;
20115 };
20116
20117 /* A helper function that writes a single signatured_type to an
20118 obstack. */
20119
20120 static int
20121 write_one_signatured_type (void **slot, void *d)
20122 {
20123 struct signatured_type_index_data *info = d;
20124 struct signatured_type *entry = (struct signatured_type *) *slot;
20125 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20126 struct partial_symtab *psymtab = per_cu->v.psymtab;
20127 gdb_byte val[8];
20128
20129 write_psymbols (info->symtab,
20130 info->psyms_seen,
20131 info->objfile->global_psymbols.list
20132 + psymtab->globals_offset,
20133 psymtab->n_global_syms, info->cu_index,
20134 0);
20135 write_psymbols (info->symtab,
20136 info->psyms_seen,
20137 info->objfile->static_psymbols.list
20138 + psymtab->statics_offset,
20139 psymtab->n_static_syms, info->cu_index,
20140 1);
20141
20142 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20143 entry->per_cu.offset.sect_off);
20144 obstack_grow (info->types_list, val, 8);
20145 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20146 entry->type_offset_in_tu.cu_off);
20147 obstack_grow (info->types_list, val, 8);
20148 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20149 obstack_grow (info->types_list, val, 8);
20150
20151 ++info->cu_index;
20152
20153 return 1;
20154 }
20155
20156 /* Recurse into all "included" dependencies and write their symbols as
20157 if they appeared in this psymtab. */
20158
20159 static void
20160 recursively_write_psymbols (struct objfile *objfile,
20161 struct partial_symtab *psymtab,
20162 struct mapped_symtab *symtab,
20163 htab_t psyms_seen,
20164 offset_type cu_index)
20165 {
20166 int i;
20167
20168 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20169 if (psymtab->dependencies[i]->user != NULL)
20170 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20171 symtab, psyms_seen, cu_index);
20172
20173 write_psymbols (symtab,
20174 psyms_seen,
20175 objfile->global_psymbols.list + psymtab->globals_offset,
20176 psymtab->n_global_syms, cu_index,
20177 0);
20178 write_psymbols (symtab,
20179 psyms_seen,
20180 objfile->static_psymbols.list + psymtab->statics_offset,
20181 psymtab->n_static_syms, cu_index,
20182 1);
20183 }
20184
20185 /* Create an index file for OBJFILE in the directory DIR. */
20186
20187 static void
20188 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20189 {
20190 struct cleanup *cleanup;
20191 char *filename, *cleanup_filename;
20192 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20193 struct obstack cu_list, types_cu_list;
20194 int i;
20195 FILE *out_file;
20196 struct mapped_symtab *symtab;
20197 offset_type val, size_of_contents, total_len;
20198 struct stat st;
20199 htab_t psyms_seen;
20200 htab_t cu_index_htab;
20201 struct psymtab_cu_index_map *psymtab_cu_index_map;
20202
20203 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20204 return;
20205
20206 if (dwarf2_per_objfile->using_index)
20207 error (_("Cannot use an index to create the index"));
20208
20209 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20210 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20211
20212 if (stat (objfile->name, &st) < 0)
20213 perror_with_name (objfile->name);
20214
20215 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20216 INDEX_SUFFIX, (char *) NULL);
20217 cleanup = make_cleanup (xfree, filename);
20218
20219 out_file = fopen (filename, "wb");
20220 if (!out_file)
20221 error (_("Can't open `%s' for writing"), filename);
20222
20223 cleanup_filename = filename;
20224 make_cleanup (unlink_if_set, &cleanup_filename);
20225
20226 symtab = create_mapped_symtab ();
20227 make_cleanup (cleanup_mapped_symtab, symtab);
20228
20229 obstack_init (&addr_obstack);
20230 make_cleanup_obstack_free (&addr_obstack);
20231
20232 obstack_init (&cu_list);
20233 make_cleanup_obstack_free (&cu_list);
20234
20235 obstack_init (&types_cu_list);
20236 make_cleanup_obstack_free (&types_cu_list);
20237
20238 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20239 NULL, xcalloc, xfree);
20240 make_cleanup_htab_delete (psyms_seen);
20241
20242 /* While we're scanning CU's create a table that maps a psymtab pointer
20243 (which is what addrmap records) to its index (which is what is recorded
20244 in the index file). This will later be needed to write the address
20245 table. */
20246 cu_index_htab = htab_create_alloc (100,
20247 hash_psymtab_cu_index,
20248 eq_psymtab_cu_index,
20249 NULL, xcalloc, xfree);
20250 make_cleanup_htab_delete (cu_index_htab);
20251 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20252 xmalloc (sizeof (struct psymtab_cu_index_map)
20253 * dwarf2_per_objfile->n_comp_units);
20254 make_cleanup (xfree, psymtab_cu_index_map);
20255
20256 /* The CU list is already sorted, so we don't need to do additional
20257 work here. Also, the debug_types entries do not appear in
20258 all_comp_units, but only in their own hash table. */
20259 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20260 {
20261 struct dwarf2_per_cu_data *per_cu
20262 = dwarf2_per_objfile->all_comp_units[i];
20263 struct partial_symtab *psymtab = per_cu->v.psymtab;
20264 gdb_byte val[8];
20265 struct psymtab_cu_index_map *map;
20266 void **slot;
20267
20268 if (psymtab->user == NULL)
20269 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20270
20271 map = &psymtab_cu_index_map[i];
20272 map->psymtab = psymtab;
20273 map->cu_index = i;
20274 slot = htab_find_slot (cu_index_htab, map, INSERT);
20275 gdb_assert (slot != NULL);
20276 gdb_assert (*slot == NULL);
20277 *slot = map;
20278
20279 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20280 per_cu->offset.sect_off);
20281 obstack_grow (&cu_list, val, 8);
20282 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20283 obstack_grow (&cu_list, val, 8);
20284 }
20285
20286 /* Dump the address map. */
20287 write_address_map (objfile, &addr_obstack, cu_index_htab);
20288
20289 /* Write out the .debug_type entries, if any. */
20290 if (dwarf2_per_objfile->signatured_types)
20291 {
20292 struct signatured_type_index_data sig_data;
20293
20294 sig_data.objfile = objfile;
20295 sig_data.symtab = symtab;
20296 sig_data.types_list = &types_cu_list;
20297 sig_data.psyms_seen = psyms_seen;
20298 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20299 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20300 write_one_signatured_type, &sig_data);
20301 }
20302
20303 /* Now that we've processed all symbols we can shrink their cu_indices
20304 lists. */
20305 uniquify_cu_indices (symtab);
20306
20307 obstack_init (&constant_pool);
20308 make_cleanup_obstack_free (&constant_pool);
20309 obstack_init (&symtab_obstack);
20310 make_cleanup_obstack_free (&symtab_obstack);
20311 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20312
20313 obstack_init (&contents);
20314 make_cleanup_obstack_free (&contents);
20315 size_of_contents = 6 * sizeof (offset_type);
20316 total_len = size_of_contents;
20317
20318 /* The version number. */
20319 val = MAYBE_SWAP (7);
20320 obstack_grow (&contents, &val, sizeof (val));
20321
20322 /* The offset of the CU list from the start of the file. */
20323 val = MAYBE_SWAP (total_len);
20324 obstack_grow (&contents, &val, sizeof (val));
20325 total_len += obstack_object_size (&cu_list);
20326
20327 /* The offset of the types CU list from the start of the file. */
20328 val = MAYBE_SWAP (total_len);
20329 obstack_grow (&contents, &val, sizeof (val));
20330 total_len += obstack_object_size (&types_cu_list);
20331
20332 /* The offset of the address table from the start of the file. */
20333 val = MAYBE_SWAP (total_len);
20334 obstack_grow (&contents, &val, sizeof (val));
20335 total_len += obstack_object_size (&addr_obstack);
20336
20337 /* The offset of the symbol table from the start of the file. */
20338 val = MAYBE_SWAP (total_len);
20339 obstack_grow (&contents, &val, sizeof (val));
20340 total_len += obstack_object_size (&symtab_obstack);
20341
20342 /* The offset of the constant pool from the start of the file. */
20343 val = MAYBE_SWAP (total_len);
20344 obstack_grow (&contents, &val, sizeof (val));
20345 total_len += obstack_object_size (&constant_pool);
20346
20347 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20348
20349 write_obstack (out_file, &contents);
20350 write_obstack (out_file, &cu_list);
20351 write_obstack (out_file, &types_cu_list);
20352 write_obstack (out_file, &addr_obstack);
20353 write_obstack (out_file, &symtab_obstack);
20354 write_obstack (out_file, &constant_pool);
20355
20356 fclose (out_file);
20357
20358 /* We want to keep the file, so we set cleanup_filename to NULL
20359 here. See unlink_if_set. */
20360 cleanup_filename = NULL;
20361
20362 do_cleanups (cleanup);
20363 }
20364
20365 /* Implementation of the `save gdb-index' command.
20366
20367 Note that the file format used by this command is documented in the
20368 GDB manual. Any changes here must be documented there. */
20369
20370 static void
20371 save_gdb_index_command (char *arg, int from_tty)
20372 {
20373 struct objfile *objfile;
20374
20375 if (!arg || !*arg)
20376 error (_("usage: save gdb-index DIRECTORY"));
20377
20378 ALL_OBJFILES (objfile)
20379 {
20380 struct stat st;
20381
20382 /* If the objfile does not correspond to an actual file, skip it. */
20383 if (stat (objfile->name, &st) < 0)
20384 continue;
20385
20386 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20387 if (dwarf2_per_objfile)
20388 {
20389 volatile struct gdb_exception except;
20390
20391 TRY_CATCH (except, RETURN_MASK_ERROR)
20392 {
20393 write_psymtabs_to_index (objfile, arg);
20394 }
20395 if (except.reason < 0)
20396 exception_fprintf (gdb_stderr, except,
20397 _("Error while writing index for `%s': "),
20398 objfile->name);
20399 }
20400 }
20401 }
20402
20403 \f
20404
20405 int dwarf2_always_disassemble;
20406
20407 static void
20408 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20409 struct cmd_list_element *c, const char *value)
20410 {
20411 fprintf_filtered (file,
20412 _("Whether to always disassemble "
20413 "DWARF expressions is %s.\n"),
20414 value);
20415 }
20416
20417 static void
20418 show_check_physname (struct ui_file *file, int from_tty,
20419 struct cmd_list_element *c, const char *value)
20420 {
20421 fprintf_filtered (file,
20422 _("Whether to check \"physname\" is %s.\n"),
20423 value);
20424 }
20425
20426 void _initialize_dwarf2_read (void);
20427
20428 void
20429 _initialize_dwarf2_read (void)
20430 {
20431 struct cmd_list_element *c;
20432
20433 dwarf2_objfile_data_key
20434 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20435
20436 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20437 Set DWARF 2 specific variables.\n\
20438 Configure DWARF 2 variables such as the cache size"),
20439 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20440 0/*allow-unknown*/, &maintenance_set_cmdlist);
20441
20442 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20443 Show DWARF 2 specific variables\n\
20444 Show DWARF 2 variables such as the cache size"),
20445 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20446 0/*allow-unknown*/, &maintenance_show_cmdlist);
20447
20448 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20449 &dwarf2_max_cache_age, _("\
20450 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20451 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20452 A higher limit means that cached compilation units will be stored\n\
20453 in memory longer, and more total memory will be used. Zero disables\n\
20454 caching, which can slow down startup."),
20455 NULL,
20456 show_dwarf2_max_cache_age,
20457 &set_dwarf2_cmdlist,
20458 &show_dwarf2_cmdlist);
20459
20460 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20461 &dwarf2_always_disassemble, _("\
20462 Set whether `info address' always disassembles DWARF expressions."), _("\
20463 Show whether `info address' always disassembles DWARF expressions."), _("\
20464 When enabled, DWARF expressions are always printed in an assembly-like\n\
20465 syntax. When disabled, expressions will be printed in a more\n\
20466 conversational style, when possible."),
20467 NULL,
20468 show_dwarf2_always_disassemble,
20469 &set_dwarf2_cmdlist,
20470 &show_dwarf2_cmdlist);
20471
20472 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20473 Set debugging of the dwarf2 reader."), _("\
20474 Show debugging of the dwarf2 reader."), _("\
20475 When enabled, debugging messages are printed during dwarf2 reading\n\
20476 and symtab expansion."),
20477 NULL,
20478 NULL,
20479 &setdebuglist, &showdebuglist);
20480
20481 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20482 Set debugging of the dwarf2 DIE reader."), _("\
20483 Show debugging of the dwarf2 DIE reader."), _("\
20484 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20485 The value is the maximum depth to print."),
20486 NULL,
20487 NULL,
20488 &setdebuglist, &showdebuglist);
20489
20490 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20491 Set cross-checking of \"physname\" code against demangler."), _("\
20492 Show cross-checking of \"physname\" code against demangler."), _("\
20493 When enabled, GDB's internal \"physname\" code is checked against\n\
20494 the demangler."),
20495 NULL, show_check_physname,
20496 &setdebuglist, &showdebuglist);
20497
20498 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20499 no_class, &use_deprecated_index_sections, _("\
20500 Set whether to use deprecated gdb_index sections."), _("\
20501 Show whether to use deprecated gdb_index sections."), _("\
20502 When enabled, deprecated .gdb_index sections are used anyway.\n\
20503 Normally they are ignored either because of a missing feature or\n\
20504 performance issue.\n\
20505 Warning: This option must be enabled before gdb reads the file."),
20506 NULL,
20507 NULL,
20508 &setlist, &showlist);
20509
20510 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20511 _("\
20512 Save a gdb-index file.\n\
20513 Usage: save gdb-index DIRECTORY"),
20514 &save_cmdlist);
20515 set_cmd_completer (c, filename_completer);
20516 }
This page took 0.561932 seconds and 5 git commands to generate.