ARI fixes: sprintf rule.
[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 can share line table entries with CUs or other TUs, and there can be
261 a lot more TUs than unique line tables, so we maintain a separate table
262 of all line table entries to support the sharing. */
263 htab_t quick_file_names_table;
264
265 /* Set during partial symbol reading, to prevent queueing of full
266 symbols. */
267 int reading_partial_symbols;
268
269 /* Table mapping type DIEs to their struct type *.
270 This is NULL if not allocated yet.
271 The mapping is done via (CU/TU signature + DIE offset) -> type. */
272 htab_t die_type_hash;
273
274 /* The CUs we recently read. */
275 VEC (dwarf2_per_cu_ptr) *just_read_cus;
276 };
277
278 static struct dwarf2_per_objfile *dwarf2_per_objfile;
279
280 /* Default names of the debugging sections. */
281
282 /* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
285 static const struct dwarf2_debug_sections dwarf2_elf_names =
286 {
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
291 { ".debug_macinfo", ".zdebug_macinfo" },
292 { ".debug_macro", ".zdebug_macro" },
293 { ".debug_str", ".zdebug_str" },
294 { ".debug_ranges", ".zdebug_ranges" },
295 { ".debug_types", ".zdebug_types" },
296 { ".debug_addr", ".zdebug_addr" },
297 { ".debug_frame", ".zdebug_frame" },
298 { ".eh_frame", NULL },
299 { ".gdb_index", ".zgdb_index" },
300 23
301 };
302
303 /* List of DWO/DWP sections. */
304
305 static const struct dwop_section_names
306 {
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
311 struct dwarf2_section_names macinfo_dwo;
312 struct dwarf2_section_names macro_dwo;
313 struct dwarf2_section_names str_dwo;
314 struct dwarf2_section_names str_offsets_dwo;
315 struct dwarf2_section_names types_dwo;
316 struct dwarf2_section_names cu_index;
317 struct dwarf2_section_names tu_index;
318 }
319 dwop_section_names =
320 {
321 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
322 { ".debug_info.dwo", ".zdebug_info.dwo" },
323 { ".debug_line.dwo", ".zdebug_line.dwo" },
324 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
325 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
326 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
327 { ".debug_str.dwo", ".zdebug_str.dwo" },
328 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
329 { ".debug_types.dwo", ".zdebug_types.dwo" },
330 { ".debug_cu_index", ".zdebug_cu_index" },
331 { ".debug_tu_index", ".zdebug_tu_index" },
332 };
333
334 /* local data types */
335
336 /* The data in a compilation unit header, after target2host
337 translation, looks like this. */
338 struct comp_unit_head
339 {
340 unsigned int length;
341 short version;
342 unsigned char addr_size;
343 unsigned char signed_addr_p;
344 sect_offset abbrev_offset;
345
346 /* Size of file offsets; either 4 or 8. */
347 unsigned int offset_size;
348
349 /* Size of the length field; either 4 or 12. */
350 unsigned int initial_length_size;
351
352 /* Offset to the first byte of this compilation unit header in the
353 .debug_info section, for resolving relative reference dies. */
354 sect_offset offset;
355
356 /* Offset to first die in this cu from the start of the cu.
357 This will be the first byte following the compilation unit header. */
358 cu_offset first_die_offset;
359 };
360
361 /* Type used for delaying computation of method physnames.
362 See comments for compute_delayed_physnames. */
363 struct delayed_method_info
364 {
365 /* The type to which the method is attached, i.e., its parent class. */
366 struct type *type;
367
368 /* The index of the method in the type's function fieldlists. */
369 int fnfield_index;
370
371 /* The index of the method in the fieldlist. */
372 int index;
373
374 /* The name of the DIE. */
375 const char *name;
376
377 /* The DIE associated with this method. */
378 struct die_info *die;
379 };
380
381 typedef struct delayed_method_info delayed_method_info;
382 DEF_VEC_O (delayed_method_info);
383
384 /* Internal state when decoding a particular compilation unit. */
385 struct dwarf2_cu
386 {
387 /* The objfile containing this compilation unit. */
388 struct objfile *objfile;
389
390 /* The header of the compilation unit. */
391 struct comp_unit_head header;
392
393 /* Base address of this compilation unit. */
394 CORE_ADDR base_address;
395
396 /* Non-zero if base_address has been set. */
397 int base_known;
398
399 /* The language we are debugging. */
400 enum language language;
401 const struct language_defn *language_defn;
402
403 const char *producer;
404
405 /* The generic symbol table building routines have separate lists for
406 file scope symbols and all all other scopes (local scopes). So
407 we need to select the right one to pass to add_symbol_to_list().
408 We do it by keeping a pointer to the correct list in list_in_scope.
409
410 FIXME: The original dwarf code just treated the file scope as the
411 first local scope, and all other local scopes as nested local
412 scopes, and worked fine. Check to see if we really need to
413 distinguish these in buildsym.c. */
414 struct pending **list_in_scope;
415
416 /* The abbrev table for this CU.
417 Normally this points to the abbrev table in the objfile.
418 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
419 struct abbrev_table *abbrev_table;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 struct obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain;
434
435 /* Backchain to our per_cu entry if the tree has been built. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header;
455
456 /* A list of methods which need to have physnames computed
457 after all type information has been read. */
458 VEC (delayed_method_info) *method_list;
459
460 /* To be copied to symtab->call_site_htab. */
461 htab_t call_site_htab;
462
463 /* Non-NULL if this CU came from a DWO file.
464 There is an invariant here that is important to remember:
465 Except for attributes copied from the top level DIE in the "main"
466 (or "stub") file in preparation for reading the DWO file
467 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
468 Either there isn't a DWO file (in which case this is NULL and the point
469 is moot), or there is and either we're not going to read it (in which
470 case this is NULL) or there is and we are reading it (in which case this
471 is non-NULL). */
472 struct dwo_unit *dwo_unit;
473
474 /* The DW_AT_addr_base attribute if present, zero otherwise
475 (zero is a valid value though).
476 Note this value comes from the stub CU/TU's DIE. */
477 ULONGEST addr_base;
478
479 /* The DW_AT_ranges_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE.
482 Also note that the value is zero in the non-DWO case so this value can
483 be used without needing to know whether DWO files are in use or not. */
484 ULONGEST ranges_base;
485
486 /* Mark used when releasing cached dies. */
487 unsigned int mark : 1;
488
489 /* This CU references .debug_loc. See the symtab->locations_valid field.
490 This test is imperfect as there may exist optimized debug code not using
491 any location list and still facing inlining issues if handled as
492 unoptimized code. For a future better test see GCC PR other/32998. */
493 unsigned int has_loclist : 1;
494
495 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
496 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
497 are valid. This information is cached because profiling CU expansion
498 showed excessive time spent in producer_is_gxx_lt_4_6. */
499 unsigned int checked_producer : 1;
500 unsigned int producer_is_gxx_lt_4_6 : 1;
501 unsigned int producer_is_icc : 1;
502 };
503
504 /* Persistent data held for a compilation unit, even when not
505 processing it. We put a pointer to this structure in the
506 read_symtab_private field of the psymtab. */
507
508 struct dwarf2_per_cu_data
509 {
510 /* The start offset and length of this compilation unit.
511 NOTE: Unlike comp_unit_head.length, this length includes
512 initial_length_size.
513 If the DIE refers to a DWO file, this is always of the original die,
514 not the DWO file. */
515 sect_offset offset;
516 unsigned int length;
517
518 /* Flag indicating this compilation unit will be read in before
519 any of the current compilation units are processed. */
520 unsigned int queued : 1;
521
522 /* This flag will be set when reading partial DIEs if we need to load
523 absolutely all DIEs for this compilation unit, instead of just the ones
524 we think are interesting. It gets set if we look for a DIE in the
525 hash table and don't find it. */
526 unsigned int load_all_dies : 1;
527
528 /* Non-zero if this CU is from .debug_types. */
529 unsigned int is_debug_types : 1;
530
531 /* Non-zero if this CU is from the .dwz file. */
532 unsigned int is_dwz : 1;
533
534 /* The section this CU/TU lives in.
535 If the DIE refers to a DWO file, this is always the original die,
536 not the DWO file. */
537 struct dwarf2_section_info *info_or_types_section;
538
539 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
540 of the CU cache it gets reset to NULL again. */
541 struct dwarf2_cu *cu;
542
543 /* The corresponding objfile.
544 Normally we can get the objfile from dwarf2_per_objfile.
545 However we can enter this file with just a "per_cu" handle. */
546 struct objfile *objfile;
547
548 /* When using partial symbol tables, the 'psymtab' field is active.
549 Otherwise the 'quick' field is active. */
550 union
551 {
552 /* The partial symbol table associated with this compilation unit,
553 or NULL for unread partial units. */
554 struct partial_symtab *psymtab;
555
556 /* Data needed by the "quick" functions. */
557 struct dwarf2_per_cu_quick_data *quick;
558 } v;
559
560 union
561 {
562 /* The CUs we import using DW_TAG_imported_unit. This is filled in
563 while reading psymtabs, used to compute the psymtab dependencies,
564 and then cleared. Then it is filled in again while reading full
565 symbols, and only deleted when the objfile is destroyed. */
566 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
567
568 /* Type units are grouped by their DW_AT_stmt_list entry so that they
569 can share them. If this is a TU, this points to the containing
570 symtab. */
571 struct type_unit_group *type_unit_group;
572 } s;
573 };
574
575 /* Entry in the signatured_types hash table. */
576
577 struct signatured_type
578 {
579 /* The "per_cu" object of this type.
580 N.B.: This is the first member so that it's easy to convert pointers
581 between them. */
582 struct dwarf2_per_cu_data per_cu;
583
584 /* The type's signature. */
585 ULONGEST signature;
586
587 /* Offset in the TU of the type's DIE, as read from the TU header.
588 If the definition lives in a DWO file, this value is unusable. */
589 cu_offset type_offset_in_tu;
590
591 /* Offset in the section of the type's DIE.
592 If the definition lives in a DWO file, this is the offset in the
593 .debug_types.dwo section.
594 The value is zero until the actual value is known.
595 Zero is otherwise not a valid section offset. */
596 sect_offset type_offset_in_section;
597 };
598
599 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
600 This includes type_unit_group and quick_file_names. */
601
602 struct stmt_list_hash
603 {
604 /* The DWO unit this table is from or NULL if there is none. */
605 struct dwo_unit *dwo_unit;
606
607 /* Offset in .debug_line or .debug_line.dwo. */
608 sect_offset line_offset;
609 };
610
611 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
612 an object of this type. */
613
614 struct type_unit_group
615 {
616 /* dwarf2read.c's main "handle" on the symtab.
617 To simplify things we create an artificial CU that "includes" all the
618 type units using this stmt_list so that the rest of the code still has
619 a "per_cu" handle on the symtab.
620 This PER_CU is recognized by having no section. */
621 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
622 struct dwarf2_per_cu_data per_cu;
623
624 union
625 {
626 /* The TUs that share this DW_AT_stmt_list entry.
627 This is added to while parsing type units to build partial symtabs,
628 and is deleted afterwards and not used again. */
629 VEC (dwarf2_per_cu_ptr) *tus;
630
631 /* When reading the line table in "quick" functions, we need a real TU.
632 Any will do, we know they all share the same DW_AT_stmt_list entry.
633 For simplicity's sake, we pick the first one. */
634 struct dwarf2_per_cu_data *first_tu;
635 } t;
636
637 /* The primary symtab.
638 Type units in a group needn't all be defined in the same source file,
639 so we create an essentially anonymous symtab as the primary symtab. */
640 struct symtab *primary_symtab;
641
642 /* The data used to construct the hash key. */
643 struct stmt_list_hash hash;
644
645 /* The number of symtabs from the line header.
646 The value here must match line_header.num_file_names. */
647 unsigned int num_symtabs;
648
649 /* The symbol tables for this TU (obtained from the files listed in
650 DW_AT_stmt_list).
651 WARNING: The order of entries here must match the order of entries
652 in the line header. After the first TU using this type_unit_group, the
653 line header for the subsequent TUs is recreated from this. This is done
654 because we need to use the same symtabs for each TU using the same
655 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
656 there's no guarantee the line header doesn't have duplicate entries. */
657 struct symtab **symtabs;
658 };
659
660 /* These sections are what may appear in a DWO file. */
661
662 struct dwo_sections
663 {
664 struct dwarf2_section_info abbrev;
665 struct dwarf2_section_info line;
666 struct dwarf2_section_info loc;
667 struct dwarf2_section_info macinfo;
668 struct dwarf2_section_info macro;
669 struct dwarf2_section_info str;
670 struct dwarf2_section_info str_offsets;
671 /* In the case of a virtual DWO file, these two are unused. */
672 struct dwarf2_section_info info;
673 VEC (dwarf2_section_info_def) *types;
674 };
675
676 /* Common bits of DWO CUs/TUs. */
677
678 struct dwo_unit
679 {
680 /* Backlink to the containing struct dwo_file. */
681 struct dwo_file *dwo_file;
682
683 /* The "id" that distinguishes this CU/TU.
684 .debug_info calls this "dwo_id", .debug_types calls this "signature".
685 Since signatures came first, we stick with it for consistency. */
686 ULONGEST signature;
687
688 /* The section this CU/TU lives in, in the DWO file. */
689 struct dwarf2_section_info *info_or_types_section;
690
691 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
692 sect_offset offset;
693 unsigned int length;
694
695 /* For types, offset in the type's DIE of the type defined by this TU. */
696 cu_offset type_offset_in_tu;
697 };
698
699 /* Data for one DWO file.
700 This includes virtual DWO files that have been packaged into a
701 DWP file. */
702
703 struct dwo_file
704 {
705 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
709 const char *name;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 bfd *dbfd;
714
715 /* Section info for this file. */
716 struct dwo_sections sections;
717
718 /* Table of CUs in the file.
719 Each element is a struct dwo_unit. */
720 htab_t cus;
721
722 /* Table of TUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t tus;
725 };
726
727 /* These sections are what may appear in a DWP file. */
728
729 struct dwp_sections
730 {
731 struct dwarf2_section_info str;
732 struct dwarf2_section_info cu_index;
733 struct dwarf2_section_info tu_index;
734 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
735 by section number. We don't need to record them here. */
736 };
737
738 /* These sections are what may appear in a virtual DWO file. */
739
740 struct virtual_dwo_sections
741 {
742 struct dwarf2_section_info abbrev;
743 struct dwarf2_section_info line;
744 struct dwarf2_section_info loc;
745 struct dwarf2_section_info macinfo;
746 struct dwarf2_section_info macro;
747 struct dwarf2_section_info str_offsets;
748 /* Each DWP hash table entry records one CU or one TU.
749 That is recorded here, and copied to dwo_unit.info_or_types_section. */
750 struct dwarf2_section_info info_or_types;
751 };
752
753 /* Contents of DWP hash tables. */
754
755 struct dwp_hash_table
756 {
757 uint32_t nr_units, nr_slots;
758 const gdb_byte *hash_table, *unit_table, *section_pool;
759 };
760
761 /* Data for one DWP file. */
762
763 struct dwp_file
764 {
765 /* Name of the file. */
766 const char *name;
767
768 /* The bfd, when the file is open. Otherwise this is NULL. */
769 bfd *dbfd;
770
771 /* Section info for this file. */
772 struct dwp_sections sections;
773
774 /* Table of CUs in the file. */
775 const struct dwp_hash_table *cus;
776
777 /* Table of TUs in the file. */
778 const struct dwp_hash_table *tus;
779
780 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
781 htab_t loaded_cutus;
782
783 /* Table to map ELF section numbers to their sections. */
784 unsigned int num_sections;
785 asection **elf_sections;
786 };
787
788 /* This represents a '.dwz' file. */
789
790 struct dwz_file
791 {
792 /* A dwz file can only contain a few sections. */
793 struct dwarf2_section_info abbrev;
794 struct dwarf2_section_info info;
795 struct dwarf2_section_info str;
796 struct dwarf2_section_info line;
797 struct dwarf2_section_info macro;
798 struct dwarf2_section_info gdb_index;
799
800 /* The dwz's BFD. */
801 bfd *dwz_bfd;
802 };
803
804 /* Struct used to pass misc. parameters to read_die_and_children, et
805 al. which are used for both .debug_info and .debug_types dies.
806 All parameters here are unchanging for the life of the call. This
807 struct exists to abstract away the constant parameters of die reading. */
808
809 struct die_reader_specs
810 {
811 /* die_section->asection->owner. */
812 bfd* abfd;
813
814 /* The CU of the DIE we are parsing. */
815 struct dwarf2_cu *cu;
816
817 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
818 struct dwo_file *dwo_file;
819
820 /* The section the die comes from.
821 This is either .debug_info or .debug_types, or the .dwo variants. */
822 struct dwarf2_section_info *die_section;
823
824 /* die_section->buffer. */
825 gdb_byte *buffer;
826
827 /* The end of the buffer. */
828 const gdb_byte *buffer_end;
829 };
830
831 /* Type of function passed to init_cutu_and_read_dies, et.al. */
832 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
833 gdb_byte *info_ptr,
834 struct die_info *comp_unit_die,
835 int has_children,
836 void *data);
837
838 /* The line number information for a compilation unit (found in the
839 .debug_line section) begins with a "statement program header",
840 which contains the following information. */
841 struct line_header
842 {
843 unsigned int total_length;
844 unsigned short version;
845 unsigned int header_length;
846 unsigned char minimum_instruction_length;
847 unsigned char maximum_ops_per_instruction;
848 unsigned char default_is_stmt;
849 int line_base;
850 unsigned char line_range;
851 unsigned char opcode_base;
852
853 /* standard_opcode_lengths[i] is the number of operands for the
854 standard opcode whose value is i. This means that
855 standard_opcode_lengths[0] is unused, and the last meaningful
856 element is standard_opcode_lengths[opcode_base - 1]. */
857 unsigned char *standard_opcode_lengths;
858
859 /* The include_directories table. NOTE! These strings are not
860 allocated with xmalloc; instead, they are pointers into
861 debug_line_buffer. If you try to free them, `free' will get
862 indigestion. */
863 unsigned int num_include_dirs, include_dirs_size;
864 char **include_dirs;
865
866 /* The file_names table. NOTE! These strings are not allocated
867 with xmalloc; instead, they are pointers into debug_line_buffer.
868 Don't try to free them directly. */
869 unsigned int num_file_names, file_names_size;
870 struct file_entry
871 {
872 char *name;
873 unsigned int dir_index;
874 unsigned int mod_time;
875 unsigned int length;
876 int included_p; /* Non-zero if referenced by the Line Number Program. */
877 struct symtab *symtab; /* The associated symbol table, if any. */
878 } *file_names;
879
880 /* The start and end of the statement program following this
881 header. These point into dwarf2_per_objfile->line_buffer. */
882 gdb_byte *statement_program_start, *statement_program_end;
883 };
884
885 /* When we construct a partial symbol table entry we only
886 need this much information. */
887 struct partial_die_info
888 {
889 /* Offset of this DIE. */
890 sect_offset offset;
891
892 /* DWARF-2 tag for this DIE. */
893 ENUM_BITFIELD(dwarf_tag) tag : 16;
894
895 /* Assorted flags describing the data found in this DIE. */
896 unsigned int has_children : 1;
897 unsigned int is_external : 1;
898 unsigned int is_declaration : 1;
899 unsigned int has_type : 1;
900 unsigned int has_specification : 1;
901 unsigned int has_pc_info : 1;
902 unsigned int may_be_inlined : 1;
903
904 /* Flag set if the SCOPE field of this structure has been
905 computed. */
906 unsigned int scope_set : 1;
907
908 /* Flag set if the DIE has a byte_size attribute. */
909 unsigned int has_byte_size : 1;
910
911 /* Flag set if any of the DIE's children are template arguments. */
912 unsigned int has_template_arguments : 1;
913
914 /* Flag set if fixup_partial_die has been called on this die. */
915 unsigned int fixup_called : 1;
916
917 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
918 unsigned int is_dwz : 1;
919
920 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
921 unsigned int spec_is_dwz : 1;
922
923 /* The name of this DIE. Normally the value of DW_AT_name, but
924 sometimes a default name for unnamed DIEs. */
925 char *name;
926
927 /* The linkage name, if present. */
928 const char *linkage_name;
929
930 /* The scope to prepend to our children. This is generally
931 allocated on the comp_unit_obstack, so will disappear
932 when this compilation unit leaves the cache. */
933 char *scope;
934
935 /* Some data associated with the partial DIE. The tag determines
936 which field is live. */
937 union
938 {
939 /* The location description associated with this DIE, if any. */
940 struct dwarf_block *locdesc;
941 /* The offset of an import, for DW_TAG_imported_unit. */
942 sect_offset offset;
943 } d;
944
945 /* If HAS_PC_INFO, the PC range associated with this DIE. */
946 CORE_ADDR lowpc;
947 CORE_ADDR highpc;
948
949 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
950 DW_AT_sibling, if any. */
951 /* NOTE: This member isn't strictly necessary, read_partial_die could
952 return DW_AT_sibling values to its caller load_partial_dies. */
953 gdb_byte *sibling;
954
955 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
956 DW_AT_specification (or DW_AT_abstract_origin or
957 DW_AT_extension). */
958 sect_offset spec_offset;
959
960 /* Pointers to this DIE's parent, first child, and next sibling,
961 if any. */
962 struct partial_die_info *die_parent, *die_child, *die_sibling;
963 };
964
965 /* This data structure holds the information of an abbrev. */
966 struct abbrev_info
967 {
968 unsigned int number; /* number identifying abbrev */
969 enum dwarf_tag tag; /* dwarf tag */
970 unsigned short has_children; /* boolean */
971 unsigned short num_attrs; /* number of attributes */
972 struct attr_abbrev *attrs; /* an array of attribute descriptions */
973 struct abbrev_info *next; /* next in chain */
974 };
975
976 struct attr_abbrev
977 {
978 ENUM_BITFIELD(dwarf_attribute) name : 16;
979 ENUM_BITFIELD(dwarf_form) form : 16;
980 };
981
982 /* Size of abbrev_table.abbrev_hash_table. */
983 #define ABBREV_HASH_SIZE 121
984
985 /* Top level data structure to contain an abbreviation table. */
986
987 struct abbrev_table
988 {
989 /* Where the abbrev table came from.
990 This is used as a sanity check when the table is used. */
991 sect_offset offset;
992
993 /* Storage for the abbrev table. */
994 struct obstack abbrev_obstack;
995
996 /* Hash table of abbrevs.
997 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
998 It could be statically allocated, but the previous code didn't so we
999 don't either. */
1000 struct abbrev_info **abbrevs;
1001 };
1002
1003 /* Attributes have a name and a value. */
1004 struct attribute
1005 {
1006 ENUM_BITFIELD(dwarf_attribute) name : 16;
1007 ENUM_BITFIELD(dwarf_form) form : 15;
1008
1009 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1010 field should be in u.str (existing only for DW_STRING) but it is kept
1011 here for better struct attribute alignment. */
1012 unsigned int string_is_canonical : 1;
1013
1014 union
1015 {
1016 char *str;
1017 struct dwarf_block *blk;
1018 ULONGEST unsnd;
1019 LONGEST snd;
1020 CORE_ADDR addr;
1021 struct signatured_type *signatured_type;
1022 }
1023 u;
1024 };
1025
1026 /* This data structure holds a complete die structure. */
1027 struct die_info
1028 {
1029 /* DWARF-2 tag for this DIE. */
1030 ENUM_BITFIELD(dwarf_tag) tag : 16;
1031
1032 /* Number of attributes */
1033 unsigned char num_attrs;
1034
1035 /* True if we're presently building the full type name for the
1036 type derived from this DIE. */
1037 unsigned char building_fullname : 1;
1038
1039 /* Abbrev number */
1040 unsigned int abbrev;
1041
1042 /* Offset in .debug_info or .debug_types section. */
1043 sect_offset offset;
1044
1045 /* The dies in a compilation unit form an n-ary tree. PARENT
1046 points to this die's parent; CHILD points to the first child of
1047 this node; and all the children of a given node are chained
1048 together via their SIBLING fields. */
1049 struct die_info *child; /* Its first child, if any. */
1050 struct die_info *sibling; /* Its next sibling, if any. */
1051 struct die_info *parent; /* Its parent, if any. */
1052
1053 /* An array of attributes, with NUM_ATTRS elements. There may be
1054 zero, but it's not common and zero-sized arrays are not
1055 sufficiently portable C. */
1056 struct attribute attrs[1];
1057 };
1058
1059 /* Get at parts of an attribute structure. */
1060
1061 #define DW_STRING(attr) ((attr)->u.str)
1062 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1063 #define DW_UNSND(attr) ((attr)->u.unsnd)
1064 #define DW_BLOCK(attr) ((attr)->u.blk)
1065 #define DW_SND(attr) ((attr)->u.snd)
1066 #define DW_ADDR(attr) ((attr)->u.addr)
1067 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1068
1069 /* Blocks are a bunch of untyped bytes. */
1070 struct dwarf_block
1071 {
1072 size_t size;
1073
1074 /* Valid only if SIZE is not zero. */
1075 gdb_byte *data;
1076 };
1077
1078 #ifndef ATTR_ALLOC_CHUNK
1079 #define ATTR_ALLOC_CHUNK 4
1080 #endif
1081
1082 /* Allocate fields for structs, unions and enums in this size. */
1083 #ifndef DW_FIELD_ALLOC_CHUNK
1084 #define DW_FIELD_ALLOC_CHUNK 4
1085 #endif
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 /* The routines that read and process dies for a C struct or C++ class
1093 pass lists of data member fields and lists of member function fields
1094 in an instance of a field_info structure, as defined below. */
1095 struct field_info
1096 {
1097 /* List of data member and baseclasses fields. */
1098 struct nextfield
1099 {
1100 struct nextfield *next;
1101 int accessibility;
1102 int virtuality;
1103 struct field field;
1104 }
1105 *fields, *baseclasses;
1106
1107 /* Number of fields (including baseclasses). */
1108 int nfields;
1109
1110 /* Number of baseclasses. */
1111 int nbaseclasses;
1112
1113 /* Set if the accesibility of one of the fields is not public. */
1114 int non_public_fields;
1115
1116 /* Member function fields array, entries are allocated in the order they
1117 are encountered in the object file. */
1118 struct nextfnfield
1119 {
1120 struct nextfnfield *next;
1121 struct fn_field fnfield;
1122 }
1123 *fnfields;
1124
1125 /* Member function fieldlist array, contains name of possibly overloaded
1126 member function, number of overloaded member functions and a pointer
1127 to the head of the member function field chain. */
1128 struct fnfieldlist
1129 {
1130 char *name;
1131 int length;
1132 struct nextfnfield *head;
1133 }
1134 *fnfieldlists;
1135
1136 /* Number of entries in the fnfieldlists array. */
1137 int nfnfields;
1138
1139 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1140 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1141 struct typedef_field_list
1142 {
1143 struct typedef_field field;
1144 struct typedef_field_list *next;
1145 }
1146 *typedef_field_list;
1147 unsigned typedef_field_list_count;
1148 };
1149
1150 /* One item on the queue of compilation units to read in full symbols
1151 for. */
1152 struct dwarf2_queue_item
1153 {
1154 struct dwarf2_per_cu_data *per_cu;
1155 enum language pretend_language;
1156 struct dwarf2_queue_item *next;
1157 };
1158
1159 /* The current queue. */
1160 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1161
1162 /* Loaded secondary compilation units are kept in memory until they
1163 have not been referenced for the processing of this many
1164 compilation units. Set this to zero to disable caching. Cache
1165 sizes of up to at least twenty will improve startup time for
1166 typical inter-CU-reference binaries, at an obvious memory cost. */
1167 static int dwarf2_max_cache_age = 5;
1168 static void
1169 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1170 struct cmd_list_element *c, const char *value)
1171 {
1172 fprintf_filtered (file, _("The upper bound on the age of cached "
1173 "dwarf2 compilation units is %s.\n"),
1174 value);
1175 }
1176
1177
1178 /* Various complaints about symbol reading that don't abort the process. */
1179
1180 static void
1181 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1182 {
1183 complaint (&symfile_complaints,
1184 _("statement list doesn't fit in .debug_line section"));
1185 }
1186
1187 static void
1188 dwarf2_debug_line_missing_file_complaint (void)
1189 {
1190 complaint (&symfile_complaints,
1191 _(".debug_line section has line data without a file"));
1192 }
1193
1194 static void
1195 dwarf2_debug_line_missing_end_sequence_complaint (void)
1196 {
1197 complaint (&symfile_complaints,
1198 _(".debug_line section has line "
1199 "program sequence without an end"));
1200 }
1201
1202 static void
1203 dwarf2_complex_location_expr_complaint (void)
1204 {
1205 complaint (&symfile_complaints, _("location expression too complex"));
1206 }
1207
1208 static void
1209 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1210 int arg3)
1211 {
1212 complaint (&symfile_complaints,
1213 _("const value length mismatch for '%s', got %d, expected %d"),
1214 arg1, arg2, arg3);
1215 }
1216
1217 static void
1218 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1219 {
1220 complaint (&symfile_complaints,
1221 _("debug info runs off end of %s section"
1222 " [in module %s]"),
1223 section->asection->name,
1224 bfd_get_filename (section->asection->owner));
1225 }
1226
1227 static void
1228 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1229 {
1230 complaint (&symfile_complaints,
1231 _("macro debug info contains a "
1232 "malformed macro definition:\n`%s'"),
1233 arg1);
1234 }
1235
1236 static void
1237 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1238 {
1239 complaint (&symfile_complaints,
1240 _("invalid attribute class or form for '%s' in '%s'"),
1241 arg1, arg2);
1242 }
1243
1244 /* local function prototypes */
1245
1246 static void dwarf2_locate_sections (bfd *, asection *, void *);
1247
1248 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1249 struct objfile *);
1250
1251 static void dwarf2_find_base_address (struct die_info *die,
1252 struct dwarf2_cu *cu);
1253
1254 static void dwarf2_build_psymtabs_hard (struct objfile *);
1255
1256 static void scan_partial_symbols (struct partial_die_info *,
1257 CORE_ADDR *, CORE_ADDR *,
1258 int, struct dwarf2_cu *);
1259
1260 static void add_partial_symbol (struct partial_die_info *,
1261 struct dwarf2_cu *);
1262
1263 static void add_partial_namespace (struct partial_die_info *pdi,
1264 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1265 int need_pc, struct dwarf2_cu *cu);
1266
1267 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1268 CORE_ADDR *highpc, int need_pc,
1269 struct dwarf2_cu *cu);
1270
1271 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1272 struct dwarf2_cu *cu);
1273
1274 static void add_partial_subprogram (struct partial_die_info *pdi,
1275 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1276 int need_pc, struct dwarf2_cu *cu);
1277
1278 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1279
1280 static void psymtab_to_symtab_1 (struct partial_symtab *);
1281
1282 static struct abbrev_info *abbrev_table_lookup_abbrev
1283 (const struct abbrev_table *, unsigned int);
1284
1285 static struct abbrev_table *abbrev_table_read_table
1286 (struct dwarf2_section_info *, sect_offset);
1287
1288 static void abbrev_table_free (struct abbrev_table *);
1289
1290 static void abbrev_table_free_cleanup (void *);
1291
1292 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1293 struct dwarf2_section_info *);
1294
1295 static void dwarf2_free_abbrev_table (void *);
1296
1297 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1298
1299 static struct partial_die_info *load_partial_dies
1300 (const struct die_reader_specs *, gdb_byte *, int);
1301
1302 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1303 struct partial_die_info *,
1304 struct abbrev_info *,
1305 unsigned int,
1306 gdb_byte *);
1307
1308 static struct partial_die_info *find_partial_die (sect_offset, int,
1309 struct dwarf2_cu *);
1310
1311 static void fixup_partial_die (struct partial_die_info *,
1312 struct dwarf2_cu *);
1313
1314 static gdb_byte *read_attribute (const struct die_reader_specs *,
1315 struct attribute *, struct attr_abbrev *,
1316 gdb_byte *);
1317
1318 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1319
1320 static int read_1_signed_byte (bfd *, const gdb_byte *);
1321
1322 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1323
1324 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1325
1326 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1327
1328 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1329 unsigned int *);
1330
1331 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1332
1333 static LONGEST read_checked_initial_length_and_offset
1334 (bfd *, gdb_byte *, const struct comp_unit_head *,
1335 unsigned int *, unsigned int *);
1336
1337 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1338 unsigned int *);
1339
1340 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1341
1342 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1343 sect_offset);
1344
1345 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1346
1347 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1348
1349 static char *read_indirect_string (bfd *, gdb_byte *,
1350 const struct comp_unit_head *,
1351 unsigned int *);
1352
1353 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1354
1355 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1356
1357 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1358
1359 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1360 unsigned int *);
1361
1362 static char *read_str_index (const struct die_reader_specs *reader,
1363 struct dwarf2_cu *cu, ULONGEST str_index);
1364
1365 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1366
1367 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1368 struct dwarf2_cu *);
1369
1370 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1371 unsigned int);
1372
1373 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1374 struct dwarf2_cu *cu);
1375
1376 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1377
1378 static struct die_info *die_specification (struct die_info *die,
1379 struct dwarf2_cu **);
1380
1381 static void free_line_header (struct line_header *lh);
1382
1383 static void add_file_name (struct line_header *, char *, unsigned int,
1384 unsigned int, unsigned int);
1385
1386 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1387 struct dwarf2_cu *cu);
1388
1389 static void dwarf_decode_lines (struct line_header *, const char *,
1390 struct dwarf2_cu *, struct partial_symtab *,
1391 int);
1392
1393 static void dwarf2_start_subfile (char *, const char *, const char *);
1394
1395 static void dwarf2_start_symtab (struct dwarf2_cu *,
1396 char *, char *, CORE_ADDR);
1397
1398 static struct symbol *new_symbol (struct die_info *, struct type *,
1399 struct dwarf2_cu *);
1400
1401 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol *);
1403
1404 static void dwarf2_const_value (struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1416
1417 static int need_gnat_info (struct dwarf2_cu *);
1418
1419 static struct type *die_descriptive_type (struct die_info *,
1420 struct dwarf2_cu *);
1421
1422 static void set_descriptive_type (struct type *, struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static struct type *die_containing_type (struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1432
1433 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1434
1435 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1436
1437 static char *typename_concat (struct obstack *obs, const char *prefix,
1438 const char *suffix, int physname,
1439 struct dwarf2_cu *cu);
1440
1441 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1442
1443 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1444
1445 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1446
1447 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1448
1449 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1450
1451 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1452 struct dwarf2_cu *, struct partial_symtab *);
1453
1454 static int dwarf2_get_pc_bounds (struct die_info *,
1455 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1456 struct partial_symtab *);
1457
1458 static void get_scope_pc_bounds (struct die_info *,
1459 CORE_ADDR *, CORE_ADDR *,
1460 struct dwarf2_cu *);
1461
1462 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1463 CORE_ADDR, struct dwarf2_cu *);
1464
1465 static void dwarf2_add_field (struct field_info *, struct die_info *,
1466 struct dwarf2_cu *);
1467
1468 static void dwarf2_attach_fields_to_type (struct field_info *,
1469 struct type *, struct dwarf2_cu *);
1470
1471 static void dwarf2_add_member_fn (struct field_info *,
1472 struct die_info *, struct type *,
1473 struct dwarf2_cu *);
1474
1475 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1476 struct type *,
1477 struct dwarf2_cu *);
1478
1479 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1480
1481 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1482
1483 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1484
1485 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1486
1487 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1488
1489 static struct type *read_module_type (struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
1492 static const char *namespace_name (struct die_info *die,
1493 int *is_anonymous, struct dwarf2_cu *);
1494
1495 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1496
1497 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1498
1499 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1500 struct dwarf2_cu *);
1501
1502 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1503 gdb_byte *info_ptr,
1504 gdb_byte **new_info_ptr,
1505 struct die_info *parent);
1506
1507 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1508 gdb_byte *info_ptr,
1509 gdb_byte **new_info_ptr,
1510 struct die_info *parent);
1511
1512 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1513 struct die_info **, gdb_byte *, int *, int);
1514
1515 static gdb_byte *read_full_die (const struct die_reader_specs *,
1516 struct die_info **, gdb_byte *, int *);
1517
1518 static void process_die (struct die_info *, struct dwarf2_cu *);
1519
1520 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1521 struct obstack *);
1522
1523 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1524
1525 static const char *dwarf2_full_name (char *name,
1526 struct die_info *die,
1527 struct dwarf2_cu *cu);
1528
1529 static struct die_info *dwarf2_extension (struct die_info *die,
1530 struct dwarf2_cu **);
1531
1532 static const char *dwarf_tag_name (unsigned int);
1533
1534 static const char *dwarf_attr_name (unsigned int);
1535
1536 static const char *dwarf_form_name (unsigned int);
1537
1538 static char *dwarf_bool_name (unsigned int);
1539
1540 static const char *dwarf_type_encoding_name (unsigned int);
1541
1542 static struct die_info *sibling_die (struct die_info *);
1543
1544 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1545
1546 static void dump_die_for_error (struct die_info *);
1547
1548 static void dump_die_1 (struct ui_file *, int level, int max_level,
1549 struct die_info *);
1550
1551 /*static*/ void dump_die (struct die_info *, int max_level);
1552
1553 static void store_in_ref_table (struct die_info *,
1554 struct dwarf2_cu *);
1555
1556 static int is_ref_attr (struct attribute *);
1557
1558 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1559
1560 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1561
1562 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1563 struct attribute *,
1564 struct dwarf2_cu **);
1565
1566 static struct die_info *follow_die_ref (struct die_info *,
1567 struct attribute *,
1568 struct dwarf2_cu **);
1569
1570 static struct die_info *follow_die_sig (struct die_info *,
1571 struct attribute *,
1572 struct dwarf2_cu **);
1573
1574 static struct signatured_type *lookup_signatured_type_at_offset
1575 (struct objfile *objfile,
1576 struct dwarf2_section_info *section, sect_offset offset);
1577
1578 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1579
1580 static void read_signatured_type (struct signatured_type *);
1581
1582 static struct type_unit_group *get_type_unit_group
1583 (struct dwarf2_cu *, struct attribute *);
1584
1585 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1586
1587 /* memory allocation interface */
1588
1589 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1590
1591 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1592
1593 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1594 char *, int);
1595
1596 static int attr_form_is_block (struct attribute *);
1597
1598 static int attr_form_is_section_offset (struct attribute *);
1599
1600 static int attr_form_is_constant (struct attribute *);
1601
1602 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1603 struct dwarf2_loclist_baton *baton,
1604 struct attribute *attr);
1605
1606 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1607 struct symbol *sym,
1608 struct dwarf2_cu *cu);
1609
1610 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1611 gdb_byte *info_ptr,
1612 struct abbrev_info *abbrev);
1613
1614 static void free_stack_comp_unit (void *);
1615
1616 static hashval_t partial_die_hash (const void *item);
1617
1618 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1619
1620 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1621 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1622
1623 static void init_one_comp_unit (struct dwarf2_cu *cu,
1624 struct dwarf2_per_cu_data *per_cu);
1625
1626 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1627 struct die_info *comp_unit_die,
1628 enum language pretend_language);
1629
1630 static void free_heap_comp_unit (void *);
1631
1632 static void free_cached_comp_units (void *);
1633
1634 static void age_cached_comp_units (void);
1635
1636 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1637
1638 static struct type *set_die_type (struct die_info *, struct type *,
1639 struct dwarf2_cu *);
1640
1641 static void create_all_comp_units (struct objfile *);
1642
1643 static int create_all_type_units (struct objfile *);
1644
1645 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1646 enum language);
1647
1648 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1649 enum language);
1650
1651 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1652 enum language);
1653
1654 static void dwarf2_add_dependence (struct dwarf2_cu *,
1655 struct dwarf2_per_cu_data *);
1656
1657 static void dwarf2_mark (struct dwarf2_cu *);
1658
1659 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1660
1661 static struct type *get_die_type_at_offset (sect_offset,
1662 struct dwarf2_per_cu_data *per_cu);
1663
1664 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1665
1666 static void dwarf2_release_queue (void *dummy);
1667
1668 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1669 enum language pretend_language);
1670
1671 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1672 struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
1674
1675 static void process_queue (void);
1676
1677 static void find_file_and_directory (struct die_info *die,
1678 struct dwarf2_cu *cu,
1679 char **name, char **comp_dir);
1680
1681 static char *file_full_name (int file, struct line_header *lh,
1682 const char *comp_dir);
1683
1684 static gdb_byte *read_and_check_comp_unit_head
1685 (struct comp_unit_head *header,
1686 struct dwarf2_section_info *section,
1687 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1688 int is_debug_types_section);
1689
1690 static void init_cutu_and_read_dies
1691 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1692 int use_existing_cu, int keep,
1693 die_reader_func_ftype *die_reader_func, void *data);
1694
1695 static void init_cutu_and_read_dies_simple
1696 (struct dwarf2_per_cu_data *this_cu,
1697 die_reader_func_ftype *die_reader_func, void *data);
1698
1699 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1700
1701 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1702
1703 static struct dwo_unit *lookup_dwo_comp_unit
1704 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1705
1706 static struct dwo_unit *lookup_dwo_type_unit
1707 (struct signatured_type *, const char *, const char *);
1708
1709 static void free_dwo_file_cleanup (void *);
1710
1711 static void process_cu_includes (void);
1712
1713 #if WORDS_BIGENDIAN
1714
1715 /* Convert VALUE between big- and little-endian. */
1716 static offset_type
1717 byte_swap (offset_type value)
1718 {
1719 offset_type result;
1720
1721 result = (value & 0xff) << 24;
1722 result |= (value & 0xff00) << 8;
1723 result |= (value & 0xff0000) >> 8;
1724 result |= (value & 0xff000000) >> 24;
1725 return result;
1726 }
1727
1728 #define MAYBE_SWAP(V) byte_swap (V)
1729
1730 #else
1731 #define MAYBE_SWAP(V) (V)
1732 #endif /* WORDS_BIGENDIAN */
1733
1734 /* The suffix for an index file. */
1735 #define INDEX_SUFFIX ".gdb-index"
1736
1737 static const char *dwarf2_physname (char *name, struct die_info *die,
1738 struct dwarf2_cu *cu);
1739
1740 /* Try to locate the sections we need for DWARF 2 debugging
1741 information and return true if we have enough to do something.
1742 NAMES points to the dwarf2 section names, or is NULL if the standard
1743 ELF names are used. */
1744
1745 int
1746 dwarf2_has_info (struct objfile *objfile,
1747 const struct dwarf2_debug_sections *names)
1748 {
1749 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1750 if (!dwarf2_per_objfile)
1751 {
1752 /* Initialize per-objfile state. */
1753 struct dwarf2_per_objfile *data
1754 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1755
1756 memset (data, 0, sizeof (*data));
1757 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1758 dwarf2_per_objfile = data;
1759
1760 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1761 (void *) names);
1762 dwarf2_per_objfile->objfile = objfile;
1763 }
1764 return (dwarf2_per_objfile->info.asection != NULL
1765 && dwarf2_per_objfile->abbrev.asection != NULL);
1766 }
1767
1768 /* When loading sections, we look either for uncompressed section or for
1769 compressed section names. */
1770
1771 static int
1772 section_is_p (const char *section_name,
1773 const struct dwarf2_section_names *names)
1774 {
1775 if (names->normal != NULL
1776 && strcmp (section_name, names->normal) == 0)
1777 return 1;
1778 if (names->compressed != NULL
1779 && strcmp (section_name, names->compressed) == 0)
1780 return 1;
1781 return 0;
1782 }
1783
1784 /* This function is mapped across the sections and remembers the
1785 offset and size of each of the debugging sections we are interested
1786 in. */
1787
1788 static void
1789 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1790 {
1791 const struct dwarf2_debug_sections *names;
1792 flagword aflag = bfd_get_section_flags (abfd, sectp);
1793
1794 if (vnames == NULL)
1795 names = &dwarf2_elf_names;
1796 else
1797 names = (const struct dwarf2_debug_sections *) vnames;
1798
1799 if ((aflag & SEC_HAS_CONTENTS) == 0)
1800 {
1801 }
1802 else if (section_is_p (sectp->name, &names->info))
1803 {
1804 dwarf2_per_objfile->info.asection = sectp;
1805 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1806 }
1807 else if (section_is_p (sectp->name, &names->abbrev))
1808 {
1809 dwarf2_per_objfile->abbrev.asection = sectp;
1810 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1811 }
1812 else if (section_is_p (sectp->name, &names->line))
1813 {
1814 dwarf2_per_objfile->line.asection = sectp;
1815 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1816 }
1817 else if (section_is_p (sectp->name, &names->loc))
1818 {
1819 dwarf2_per_objfile->loc.asection = sectp;
1820 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1821 }
1822 else if (section_is_p (sectp->name, &names->macinfo))
1823 {
1824 dwarf2_per_objfile->macinfo.asection = sectp;
1825 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1826 }
1827 else if (section_is_p (sectp->name, &names->macro))
1828 {
1829 dwarf2_per_objfile->macro.asection = sectp;
1830 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1831 }
1832 else if (section_is_p (sectp->name, &names->str))
1833 {
1834 dwarf2_per_objfile->str.asection = sectp;
1835 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1836 }
1837 else if (section_is_p (sectp->name, &names->addr))
1838 {
1839 dwarf2_per_objfile->addr.asection = sectp;
1840 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1841 }
1842 else if (section_is_p (sectp->name, &names->frame))
1843 {
1844 dwarf2_per_objfile->frame.asection = sectp;
1845 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1846 }
1847 else if (section_is_p (sectp->name, &names->eh_frame))
1848 {
1849 dwarf2_per_objfile->eh_frame.asection = sectp;
1850 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1851 }
1852 else if (section_is_p (sectp->name, &names->ranges))
1853 {
1854 dwarf2_per_objfile->ranges.asection = sectp;
1855 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1856 }
1857 else if (section_is_p (sectp->name, &names->types))
1858 {
1859 struct dwarf2_section_info type_section;
1860
1861 memset (&type_section, 0, sizeof (type_section));
1862 type_section.asection = sectp;
1863 type_section.size = bfd_get_section_size (sectp);
1864
1865 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1866 &type_section);
1867 }
1868 else if (section_is_p (sectp->name, &names->gdb_index))
1869 {
1870 dwarf2_per_objfile->gdb_index.asection = sectp;
1871 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1872 }
1873
1874 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1875 && bfd_section_vma (abfd, sectp) == 0)
1876 dwarf2_per_objfile->has_section_at_zero = 1;
1877 }
1878
1879 /* A helper function that decides whether a section is empty,
1880 or not present. */
1881
1882 static int
1883 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1884 {
1885 return info->asection == NULL || info->size == 0;
1886 }
1887
1888 /* Read the contents of the section INFO.
1889 OBJFILE is the main object file, but not necessarily the file where
1890 the section comes from. E.g., for DWO files INFO->asection->owner
1891 is the bfd of the DWO file.
1892 If the section is compressed, uncompress it before returning. */
1893
1894 static void
1895 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1896 {
1897 asection *sectp = info->asection;
1898 bfd *abfd;
1899 gdb_byte *buf, *retbuf;
1900 unsigned char header[4];
1901
1902 if (info->readin)
1903 return;
1904 info->buffer = NULL;
1905 info->readin = 1;
1906
1907 if (dwarf2_section_empty_p (info))
1908 return;
1909
1910 abfd = sectp->owner;
1911
1912 /* If the section has relocations, we must read it ourselves.
1913 Otherwise we attach it to the BFD. */
1914 if ((sectp->flags & SEC_RELOC) == 0)
1915 {
1916 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1917
1918 /* We have to cast away const here for historical reasons.
1919 Fixing dwarf2read to be const-correct would be quite nice. */
1920 info->buffer = (gdb_byte *) bytes;
1921 return;
1922 }
1923
1924 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1925 info->buffer = buf;
1926
1927 /* When debugging .o files, we may need to apply relocations; see
1928 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1929 We never compress sections in .o files, so we only need to
1930 try this when the section is not compressed. */
1931 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1932 if (retbuf != NULL)
1933 {
1934 info->buffer = retbuf;
1935 return;
1936 }
1937
1938 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1939 || bfd_bread (buf, info->size, abfd) != info->size)
1940 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1941 bfd_get_filename (abfd));
1942 }
1943
1944 /* A helper function that returns the size of a section in a safe way.
1945 If you are positive that the section has been read before using the
1946 size, then it is safe to refer to the dwarf2_section_info object's
1947 "size" field directly. In other cases, you must call this
1948 function, because for compressed sections the size field is not set
1949 correctly until the section has been read. */
1950
1951 static bfd_size_type
1952 dwarf2_section_size (struct objfile *objfile,
1953 struct dwarf2_section_info *info)
1954 {
1955 if (!info->readin)
1956 dwarf2_read_section (objfile, info);
1957 return info->size;
1958 }
1959
1960 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1961 SECTION_NAME. */
1962
1963 void
1964 dwarf2_get_section_info (struct objfile *objfile,
1965 enum dwarf2_section_enum sect,
1966 asection **sectp, gdb_byte **bufp,
1967 bfd_size_type *sizep)
1968 {
1969 struct dwarf2_per_objfile *data
1970 = objfile_data (objfile, dwarf2_objfile_data_key);
1971 struct dwarf2_section_info *info;
1972
1973 /* We may see an objfile without any DWARF, in which case we just
1974 return nothing. */
1975 if (data == NULL)
1976 {
1977 *sectp = NULL;
1978 *bufp = NULL;
1979 *sizep = 0;
1980 return;
1981 }
1982 switch (sect)
1983 {
1984 case DWARF2_DEBUG_FRAME:
1985 info = &data->frame;
1986 break;
1987 case DWARF2_EH_FRAME:
1988 info = &data->eh_frame;
1989 break;
1990 default:
1991 gdb_assert_not_reached ("unexpected section");
1992 }
1993
1994 dwarf2_read_section (objfile, info);
1995
1996 *sectp = info->asection;
1997 *bufp = info->buffer;
1998 *sizep = info->size;
1999 }
2000
2001 /* A helper function to find the sections for a .dwz file. */
2002
2003 static void
2004 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2005 {
2006 struct dwz_file *dwz_file = arg;
2007
2008 /* Note that we only support the standard ELF names, because .dwz
2009 is ELF-only (at the time of writing). */
2010 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2011 {
2012 dwz_file->abbrev.asection = sectp;
2013 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2016 {
2017 dwz_file->info.asection = sectp;
2018 dwz_file->info.size = bfd_get_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2021 {
2022 dwz_file->str.asection = sectp;
2023 dwz_file->str.size = bfd_get_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2026 {
2027 dwz_file->line.asection = sectp;
2028 dwz_file->line.size = bfd_get_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2031 {
2032 dwz_file->macro.asection = sectp;
2033 dwz_file->macro.size = bfd_get_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2036 {
2037 dwz_file->gdb_index.asection = sectp;
2038 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2039 }
2040 }
2041
2042 /* Open the separate '.dwz' debug file, if needed. Error if the file
2043 cannot be found. */
2044
2045 static struct dwz_file *
2046 dwarf2_get_dwz_file (void)
2047 {
2048 bfd *abfd, *dwz_bfd;
2049 asection *section;
2050 gdb_byte *data;
2051 struct cleanup *cleanup;
2052 const char *filename;
2053 struct dwz_file *result;
2054
2055 if (dwarf2_per_objfile->dwz_file != NULL)
2056 return dwarf2_per_objfile->dwz_file;
2057
2058 abfd = dwarf2_per_objfile->objfile->obfd;
2059 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2060 if (section == NULL)
2061 error (_("could not find '.gnu_debugaltlink' section"));
2062 if (!bfd_malloc_and_get_section (abfd, section, &data))
2063 error (_("could not read '.gnu_debugaltlink' section: %s"),
2064 bfd_errmsg (bfd_get_error ()));
2065 cleanup = make_cleanup (xfree, data);
2066
2067 filename = data;
2068 if (!IS_ABSOLUTE_PATH (filename))
2069 {
2070 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2071 char *rel;
2072
2073 make_cleanup (xfree, abs);
2074 abs = ldirname (abs);
2075 make_cleanup (xfree, abs);
2076
2077 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2078 make_cleanup (xfree, rel);
2079 filename = rel;
2080 }
2081
2082 /* The format is just a NUL-terminated file name, followed by the
2083 build-id. For now, though, we ignore the build-id. */
2084 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2085 if (dwz_bfd == NULL)
2086 error (_("could not read '%s': %s"), filename,
2087 bfd_errmsg (bfd_get_error ()));
2088
2089 if (!bfd_check_format (dwz_bfd, bfd_object))
2090 {
2091 gdb_bfd_unref (dwz_bfd);
2092 error (_("file '%s' was not usable: %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094 }
2095
2096 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2097 struct dwz_file);
2098 result->dwz_bfd = dwz_bfd;
2099
2100 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2101
2102 do_cleanups (cleanup);
2103
2104 return result;
2105 }
2106 \f
2107 /* DWARF quick_symbols_functions support. */
2108
2109 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2110 unique line tables, so we maintain a separate table of all .debug_line
2111 derived entries to support the sharing.
2112 All the quick functions need is the list of file names. We discard the
2113 line_header when we're done and don't need to record it here. */
2114 struct quick_file_names
2115 {
2116 /* The data used to construct the hash key. */
2117 struct stmt_list_hash hash;
2118
2119 /* The number of entries in file_names, real_names. */
2120 unsigned int num_file_names;
2121
2122 /* The file names from the line table, after being run through
2123 file_full_name. */
2124 const char **file_names;
2125
2126 /* The file names from the line table after being run through
2127 gdb_realpath. These are computed lazily. */
2128 const char **real_names;
2129 };
2130
2131 /* When using the index (and thus not using psymtabs), each CU has an
2132 object of this type. This is used to hold information needed by
2133 the various "quick" methods. */
2134 struct dwarf2_per_cu_quick_data
2135 {
2136 /* The file table. This can be NULL if there was no file table
2137 or it's currently not read in.
2138 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2139 struct quick_file_names *file_names;
2140
2141 /* The corresponding symbol table. This is NULL if symbols for this
2142 CU have not yet been read. */
2143 struct symtab *symtab;
2144
2145 /* A temporary mark bit used when iterating over all CUs in
2146 expand_symtabs_matching. */
2147 unsigned int mark : 1;
2148
2149 /* True if we've tried to read the file table and found there isn't one.
2150 There will be no point in trying to read it again next time. */
2151 unsigned int no_file_data : 1;
2152 };
2153
2154 /* Utility hash function for a stmt_list_hash. */
2155
2156 static hashval_t
2157 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2158 {
2159 hashval_t v = 0;
2160
2161 if (stmt_list_hash->dwo_unit != NULL)
2162 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2163 v += stmt_list_hash->line_offset.sect_off;
2164 return v;
2165 }
2166
2167 /* Utility equality function for a stmt_list_hash. */
2168
2169 static int
2170 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2171 const struct stmt_list_hash *rhs)
2172 {
2173 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2174 return 0;
2175 if (lhs->dwo_unit != NULL
2176 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2177 return 0;
2178
2179 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2180 }
2181
2182 /* Hash function for a quick_file_names. */
2183
2184 static hashval_t
2185 hash_file_name_entry (const void *e)
2186 {
2187 const struct quick_file_names *file_data = e;
2188
2189 return hash_stmt_list_entry (&file_data->hash);
2190 }
2191
2192 /* Equality function for a quick_file_names. */
2193
2194 static int
2195 eq_file_name_entry (const void *a, const void *b)
2196 {
2197 const struct quick_file_names *ea = a;
2198 const struct quick_file_names *eb = b;
2199
2200 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2201 }
2202
2203 /* Delete function for a quick_file_names. */
2204
2205 static void
2206 delete_file_name_entry (void *e)
2207 {
2208 struct quick_file_names *file_data = e;
2209 int i;
2210
2211 for (i = 0; i < file_data->num_file_names; ++i)
2212 {
2213 xfree ((void*) file_data->file_names[i]);
2214 if (file_data->real_names)
2215 xfree ((void*) file_data->real_names[i]);
2216 }
2217
2218 /* The space for the struct itself lives on objfile_obstack,
2219 so we don't free it here. */
2220 }
2221
2222 /* Create a quick_file_names hash table. */
2223
2224 static htab_t
2225 create_quick_file_names_table (unsigned int nr_initial_entries)
2226 {
2227 return htab_create_alloc (nr_initial_entries,
2228 hash_file_name_entry, eq_file_name_entry,
2229 delete_file_name_entry, xcalloc, xfree);
2230 }
2231
2232 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2233 have to be created afterwards. You should call age_cached_comp_units after
2234 processing PER_CU->CU. dw2_setup must have been already called. */
2235
2236 static void
2237 load_cu (struct dwarf2_per_cu_data *per_cu)
2238 {
2239 if (per_cu->is_debug_types)
2240 load_full_type_unit (per_cu);
2241 else
2242 load_full_comp_unit (per_cu, language_minimal);
2243
2244 gdb_assert (per_cu->cu != NULL);
2245
2246 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2247 }
2248
2249 /* Read in the symbols for PER_CU. */
2250
2251 static void
2252 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2253 {
2254 struct cleanup *back_to;
2255
2256 /* Skip type_unit_groups, reading the type units they contain
2257 is handled elsewhere. */
2258 if (IS_TYPE_UNIT_GROUP (per_cu))
2259 return;
2260
2261 back_to = make_cleanup (dwarf2_release_queue, NULL);
2262
2263 if (dwarf2_per_objfile->using_index
2264 ? per_cu->v.quick->symtab == NULL
2265 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2266 {
2267 queue_comp_unit (per_cu, language_minimal);
2268 load_cu (per_cu);
2269 }
2270
2271 process_queue ();
2272
2273 /* Age the cache, releasing compilation units that have not
2274 been used recently. */
2275 age_cached_comp_units ();
2276
2277 do_cleanups (back_to);
2278 }
2279
2280 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2281 the objfile from which this CU came. Returns the resulting symbol
2282 table. */
2283
2284 static struct symtab *
2285 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2286 {
2287 gdb_assert (dwarf2_per_objfile->using_index);
2288 if (!per_cu->v.quick->symtab)
2289 {
2290 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2291 increment_reading_symtab ();
2292 dw2_do_instantiate_symtab (per_cu);
2293 process_cu_includes ();
2294 do_cleanups (back_to);
2295 }
2296 return per_cu->v.quick->symtab;
2297 }
2298
2299 /* Return the CU given its index.
2300
2301 This is intended for loops like:
2302
2303 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2304 + dwarf2_per_objfile->n_type_units); ++i)
2305 {
2306 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2307
2308 ...;
2309 }
2310 */
2311
2312 static struct dwarf2_per_cu_data *
2313 dw2_get_cu (int index)
2314 {
2315 if (index >= dwarf2_per_objfile->n_comp_units)
2316 {
2317 index -= dwarf2_per_objfile->n_comp_units;
2318 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2319 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2320 }
2321
2322 return dwarf2_per_objfile->all_comp_units[index];
2323 }
2324
2325 /* Return the primary CU given its index.
2326 The difference between this function and dw2_get_cu is in the handling
2327 of type units (TUs). Here we return the type_unit_group object.
2328
2329 This is intended for loops like:
2330
2331 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2332 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2333 {
2334 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2335
2336 ...;
2337 }
2338 */
2339
2340 static struct dwarf2_per_cu_data *
2341 dw2_get_primary_cu (int index)
2342 {
2343 if (index >= dwarf2_per_objfile->n_comp_units)
2344 {
2345 index -= dwarf2_per_objfile->n_comp_units;
2346 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2347 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2348 }
2349
2350 return dwarf2_per_objfile->all_comp_units[index];
2351 }
2352
2353 /* A helper function that knows how to read a 64-bit value in a way
2354 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2355 otherwise. */
2356
2357 static int
2358 extract_cu_value (const char *bytes, ULONGEST *result)
2359 {
2360 if (sizeof (ULONGEST) < 8)
2361 {
2362 int i;
2363
2364 /* Ignore the upper 4 bytes if they are all zero. */
2365 for (i = 0; i < 4; ++i)
2366 if (bytes[i + 4] != 0)
2367 return 0;
2368
2369 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2370 }
2371 else
2372 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2373 return 1;
2374 }
2375
2376 /* A helper for create_cus_from_index that handles a given list of
2377 CUs. */
2378
2379 static int
2380 create_cus_from_index_list (struct objfile *objfile,
2381 const gdb_byte *cu_list, offset_type n_elements,
2382 struct dwarf2_section_info *section,
2383 int is_dwz,
2384 int base_offset)
2385 {
2386 offset_type i;
2387
2388 for (i = 0; i < n_elements; i += 2)
2389 {
2390 struct dwarf2_per_cu_data *the_cu;
2391 ULONGEST offset, length;
2392
2393 if (!extract_cu_value (cu_list, &offset)
2394 || !extract_cu_value (cu_list + 8, &length))
2395 return 0;
2396 cu_list += 2 * 8;
2397
2398 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_data);
2400 the_cu->offset.sect_off = offset;
2401 the_cu->length = length;
2402 the_cu->objfile = objfile;
2403 the_cu->info_or_types_section = section;
2404 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2406 the_cu->is_dwz = is_dwz;
2407 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2408 }
2409
2410 return 1;
2411 }
2412
2413 /* Read the CU list from the mapped index, and use it to create all
2414 the CU objects for this objfile. Return 0 if something went wrong,
2415 1 if everything went ok. */
2416
2417 static int
2418 create_cus_from_index (struct objfile *objfile,
2419 const gdb_byte *cu_list, offset_type cu_list_elements,
2420 const gdb_byte *dwz_list, offset_type dwz_elements)
2421 {
2422 struct dwz_file *dwz;
2423
2424 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2425 dwarf2_per_objfile->all_comp_units
2426 = obstack_alloc (&objfile->objfile_obstack,
2427 dwarf2_per_objfile->n_comp_units
2428 * sizeof (struct dwarf2_per_cu_data *));
2429
2430 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2431 &dwarf2_per_objfile->info, 0, 0))
2432 return 0;
2433
2434 if (dwz_elements == 0)
2435 return 1;
2436
2437 dwz = dwarf2_get_dwz_file ();
2438 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2439 &dwz->info, 1, cu_list_elements / 2);
2440 }
2441
2442 /* Create the signatured type hash table from the index. */
2443
2444 static int
2445 create_signatured_type_table_from_index (struct objfile *objfile,
2446 struct dwarf2_section_info *section,
2447 const gdb_byte *bytes,
2448 offset_type elements)
2449 {
2450 offset_type i;
2451 htab_t sig_types_hash;
2452
2453 dwarf2_per_objfile->n_type_units = elements / 3;
2454 dwarf2_per_objfile->all_type_units
2455 = obstack_alloc (&objfile->objfile_obstack,
2456 dwarf2_per_objfile->n_type_units
2457 * sizeof (struct signatured_type *));
2458
2459 sig_types_hash = allocate_signatured_type_table (objfile);
2460
2461 for (i = 0; i < elements; i += 3)
2462 {
2463 struct signatured_type *sig_type;
2464 ULONGEST offset, type_offset_in_tu, signature;
2465 void **slot;
2466
2467 if (!extract_cu_value (bytes, &offset)
2468 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2469 return 0;
2470 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2471 bytes += 3 * 8;
2472
2473 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2474 struct signatured_type);
2475 sig_type->signature = signature;
2476 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2477 sig_type->per_cu.is_debug_types = 1;
2478 sig_type->per_cu.info_or_types_section = section;
2479 sig_type->per_cu.offset.sect_off = offset;
2480 sig_type->per_cu.objfile = objfile;
2481 sig_type->per_cu.v.quick
2482 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483 struct dwarf2_per_cu_quick_data);
2484
2485 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2486 *slot = sig_type;
2487
2488 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2489 }
2490
2491 dwarf2_per_objfile->signatured_types = sig_types_hash;
2492
2493 return 1;
2494 }
2495
2496 /* Read the address map data from the mapped index, and use it to
2497 populate the objfile's psymtabs_addrmap. */
2498
2499 static void
2500 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2501 {
2502 const gdb_byte *iter, *end;
2503 struct obstack temp_obstack;
2504 struct addrmap *mutable_map;
2505 struct cleanup *cleanup;
2506 CORE_ADDR baseaddr;
2507
2508 obstack_init (&temp_obstack);
2509 cleanup = make_cleanup_obstack_free (&temp_obstack);
2510 mutable_map = addrmap_create_mutable (&temp_obstack);
2511
2512 iter = index->address_table;
2513 end = iter + index->address_table_size;
2514
2515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2516
2517 while (iter < end)
2518 {
2519 ULONGEST hi, lo, cu_index;
2520 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2521 iter += 8;
2522 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2525 iter += 4;
2526
2527 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2528 dw2_get_cu (cu_index));
2529 }
2530
2531 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2532 &objfile->objfile_obstack);
2533 do_cleanups (cleanup);
2534 }
2535
2536 /* The hash function for strings in the mapped index. This is the same as
2537 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2538 implementation. This is necessary because the hash function is tied to the
2539 format of the mapped index file. The hash values do not have to match with
2540 SYMBOL_HASH_NEXT.
2541
2542 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2543
2544 static hashval_t
2545 mapped_index_string_hash (int index_version, const void *p)
2546 {
2547 const unsigned char *str = (const unsigned char *) p;
2548 hashval_t r = 0;
2549 unsigned char c;
2550
2551 while ((c = *str++) != 0)
2552 {
2553 if (index_version >= 5)
2554 c = tolower (c);
2555 r = r * 67 + c - 113;
2556 }
2557
2558 return r;
2559 }
2560
2561 /* Find a slot in the mapped index INDEX for the object named NAME.
2562 If NAME is found, set *VEC_OUT to point to the CU vector in the
2563 constant pool and return 1. If NAME cannot be found, return 0. */
2564
2565 static int
2566 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2567 offset_type **vec_out)
2568 {
2569 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2570 offset_type hash;
2571 offset_type slot, step;
2572 int (*cmp) (const char *, const char *);
2573
2574 if (current_language->la_language == language_cplus
2575 || current_language->la_language == language_java
2576 || current_language->la_language == language_fortran)
2577 {
2578 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2579 not contain any. */
2580 const char *paren = strchr (name, '(');
2581
2582 if (paren)
2583 {
2584 char *dup;
2585
2586 dup = xmalloc (paren - name + 1);
2587 memcpy (dup, name, paren - name);
2588 dup[paren - name] = 0;
2589
2590 make_cleanup (xfree, dup);
2591 name = dup;
2592 }
2593 }
2594
2595 /* Index version 4 did not support case insensitive searches. But the
2596 indices for case insensitive languages are built in lowercase, therefore
2597 simulate our NAME being searched is also lowercased. */
2598 hash = mapped_index_string_hash ((index->version == 4
2599 && case_sensitivity == case_sensitive_off
2600 ? 5 : index->version),
2601 name);
2602
2603 slot = hash & (index->symbol_table_slots - 1);
2604 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2605 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2606
2607 for (;;)
2608 {
2609 /* Convert a slot number to an offset into the table. */
2610 offset_type i = 2 * slot;
2611 const char *str;
2612 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2613 {
2614 do_cleanups (back_to);
2615 return 0;
2616 }
2617
2618 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2619 if (!cmp (name, str))
2620 {
2621 *vec_out = (offset_type *) (index->constant_pool
2622 + MAYBE_SWAP (index->symbol_table[i + 1]));
2623 do_cleanups (back_to);
2624 return 1;
2625 }
2626
2627 slot = (slot + step) & (index->symbol_table_slots - 1);
2628 }
2629 }
2630
2631 /* A helper function that reads the .gdb_index from SECTION and fills
2632 in MAP. FILENAME is the name of the file containing the section;
2633 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2634 ok to use deprecated sections.
2635
2636 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2637 out parameters that are filled in with information about the CU and
2638 TU lists in the section.
2639
2640 Returns 1 if all went well, 0 otherwise. */
2641
2642 static int
2643 read_index_from_section (struct objfile *objfile,
2644 const char *filename,
2645 int deprecated_ok,
2646 struct dwarf2_section_info *section,
2647 struct mapped_index *map,
2648 const gdb_byte **cu_list,
2649 offset_type *cu_list_elements,
2650 const gdb_byte **types_list,
2651 offset_type *types_list_elements)
2652 {
2653 char *addr;
2654 offset_type version;
2655 offset_type *metadata;
2656 int i;
2657
2658 if (dwarf2_section_empty_p (section))
2659 return 0;
2660
2661 /* Older elfutils strip versions could keep the section in the main
2662 executable while splitting it for the separate debug info file. */
2663 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2664 return 0;
2665
2666 dwarf2_read_section (objfile, section);
2667
2668 addr = section->buffer;
2669 /* Version check. */
2670 version = MAYBE_SWAP (*(offset_type *) addr);
2671 /* Versions earlier than 3 emitted every copy of a psymbol. This
2672 causes the index to behave very poorly for certain requests. Version 3
2673 contained incomplete addrmap. So, it seems better to just ignore such
2674 indices. */
2675 if (version < 4)
2676 {
2677 static int warning_printed = 0;
2678 if (!warning_printed)
2679 {
2680 warning (_("Skipping obsolete .gdb_index section in %s."),
2681 filename);
2682 warning_printed = 1;
2683 }
2684 return 0;
2685 }
2686 /* Index version 4 uses a different hash function than index version
2687 5 and later.
2688
2689 Versions earlier than 6 did not emit psymbols for inlined
2690 functions. Using these files will cause GDB not to be able to
2691 set breakpoints on inlined functions by name, so we ignore these
2692 indices unless the user has done
2693 "set use-deprecated-index-sections on". */
2694 if (version < 6 && !deprecated_ok)
2695 {
2696 static int warning_printed = 0;
2697 if (!warning_printed)
2698 {
2699 warning (_("\
2700 Skipping deprecated .gdb_index section in %s.\n\
2701 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2702 to use the section anyway."),
2703 filename);
2704 warning_printed = 1;
2705 }
2706 return 0;
2707 }
2708 /* Indexes with higher version than the one supported by GDB may be no
2709 longer backward compatible. */
2710 if (version > 7)
2711 return 0;
2712
2713 map->version = version;
2714 map->total_size = section->size;
2715
2716 metadata = (offset_type *) (addr + sizeof (offset_type));
2717
2718 i = 0;
2719 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2720 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2721 / 8);
2722 ++i;
2723
2724 *types_list = addr + MAYBE_SWAP (metadata[i]);
2725 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2726 - MAYBE_SWAP (metadata[i]))
2727 / 8);
2728 ++i;
2729
2730 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2731 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]));
2733 ++i;
2734
2735 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2736 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2737 - MAYBE_SWAP (metadata[i]))
2738 / (2 * sizeof (offset_type)));
2739 ++i;
2740
2741 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2742
2743 return 1;
2744 }
2745
2746
2747 /* Read the index file. If everything went ok, initialize the "quick"
2748 elements of all the CUs and return 1. Otherwise, return 0. */
2749
2750 static int
2751 dwarf2_read_index (struct objfile *objfile)
2752 {
2753 struct mapped_index local_map, *map;
2754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2756
2757 if (!read_index_from_section (objfile, objfile->name,
2758 use_deprecated_index_sections,
2759 &dwarf2_per_objfile->gdb_index, &local_map,
2760 &cu_list, &cu_list_elements,
2761 &types_list, &types_list_elements))
2762 return 0;
2763
2764 /* Don't use the index if it's empty. */
2765 if (local_map.symbol_table_slots == 0)
2766 return 0;
2767
2768 /* If there is a .dwz file, read it so we can get its CU list as
2769 well. */
2770 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2771 {
2772 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2773 struct mapped_index dwz_map;
2774 const gdb_byte *dwz_types_ignore;
2775 offset_type dwz_types_elements_ignore;
2776
2777 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2778 1,
2779 &dwz->gdb_index, &dwz_map,
2780 &dwz_list, &dwz_list_elements,
2781 &dwz_types_ignore,
2782 &dwz_types_elements_ignore))
2783 {
2784 warning (_("could not read '.gdb_index' section from %s; skipping"),
2785 bfd_get_filename (dwz->dwz_bfd));
2786 return 0;
2787 }
2788 }
2789
2790 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2791 dwz_list, dwz_list_elements))
2792 return 0;
2793
2794 if (types_list_elements)
2795 {
2796 struct dwarf2_section_info *section;
2797
2798 /* We can only handle a single .debug_types when we have an
2799 index. */
2800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 return 0;
2802
2803 section = VEC_index (dwarf2_section_info_def,
2804 dwarf2_per_objfile->types, 0);
2805
2806 if (!create_signatured_type_table_from_index (objfile, section,
2807 types_list,
2808 types_list_elements))
2809 return 0;
2810 }
2811
2812 create_addrmap_from_index (objfile, &local_map);
2813
2814 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2815 *map = local_map;
2816
2817 dwarf2_per_objfile->index_table = map;
2818 dwarf2_per_objfile->using_index = 1;
2819 dwarf2_per_objfile->quick_file_names_table =
2820 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2821
2822 return 1;
2823 }
2824
2825 /* A helper for the "quick" functions which sets the global
2826 dwarf2_per_objfile according to OBJFILE. */
2827
2828 static void
2829 dw2_setup (struct objfile *objfile)
2830 {
2831 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2832 gdb_assert (dwarf2_per_objfile);
2833 }
2834
2835 /* Reader function for dw2_build_type_unit_groups. */
2836
2837 static void
2838 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2839 gdb_byte *info_ptr,
2840 struct die_info *type_unit_die,
2841 int has_children,
2842 void *data)
2843 {
2844 struct dwarf2_cu *cu = reader->cu;
2845 struct attribute *attr;
2846 struct type_unit_group *tu_group;
2847
2848 gdb_assert (data == NULL);
2849
2850 if (! has_children)
2851 return;
2852
2853 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2854 /* Call this for its side-effect of creating the associated
2855 struct type_unit_group if it doesn't already exist. */
2856 tu_group = get_type_unit_group (cu, attr);
2857 }
2858
2859 /* Build dwarf2_per_objfile->type_unit_groups.
2860 This function may be called multiple times. */
2861
2862 static void
2863 dw2_build_type_unit_groups (void)
2864 {
2865 if (dwarf2_per_objfile->type_unit_groups == NULL)
2866 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2867 }
2868
2869 /* die_reader_func for dw2_get_file_names. */
2870
2871 static void
2872 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2873 gdb_byte *info_ptr,
2874 struct die_info *comp_unit_die,
2875 int has_children,
2876 void *data)
2877 {
2878 struct dwarf2_cu *cu = reader->cu;
2879 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2880 struct objfile *objfile = dwarf2_per_objfile->objfile;
2881 struct dwarf2_per_cu_data *lh_cu;
2882 struct line_header *lh;
2883 struct attribute *attr;
2884 int i;
2885 char *name, *comp_dir;
2886 void **slot;
2887 struct quick_file_names *qfn;
2888 unsigned int line_offset;
2889
2890 /* Our callers never want to match partial units -- instead they
2891 will match the enclosing full CU. */
2892 if (comp_unit_die->tag == DW_TAG_partial_unit)
2893 {
2894 this_cu->v.quick->no_file_data = 1;
2895 return;
2896 }
2897
2898 /* If we're reading the line header for TUs, store it in the "per_cu"
2899 for tu_group. */
2900 if (this_cu->is_debug_types)
2901 {
2902 struct type_unit_group *tu_group = data;
2903
2904 gdb_assert (tu_group != NULL);
2905 lh_cu = &tu_group->per_cu;
2906 }
2907 else
2908 lh_cu = this_cu;
2909
2910 lh = NULL;
2911 slot = NULL;
2912 line_offset = 0;
2913
2914 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2915 if (attr)
2916 {
2917 struct quick_file_names find_entry;
2918
2919 line_offset = DW_UNSND (attr);
2920
2921 /* We may have already read in this line header (TU line header sharing).
2922 If we have we're done. */
2923 find_entry.hash.dwo_unit = cu->dwo_unit;
2924 find_entry.hash.line_offset.sect_off = line_offset;
2925 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2926 &find_entry, INSERT);
2927 if (*slot != NULL)
2928 {
2929 lh_cu->v.quick->file_names = *slot;
2930 return;
2931 }
2932
2933 lh = dwarf_decode_line_header (line_offset, cu);
2934 }
2935 if (lh == NULL)
2936 {
2937 lh_cu->v.quick->no_file_data = 1;
2938 return;
2939 }
2940
2941 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2942 qfn->hash.dwo_unit = cu->dwo_unit;
2943 qfn->hash.line_offset.sect_off = line_offset;
2944 gdb_assert (slot != NULL);
2945 *slot = qfn;
2946
2947 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2948
2949 qfn->num_file_names = lh->num_file_names;
2950 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2951 lh->num_file_names * sizeof (char *));
2952 for (i = 0; i < lh->num_file_names; ++i)
2953 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2954 qfn->real_names = NULL;
2955
2956 free_line_header (lh);
2957
2958 lh_cu->v.quick->file_names = qfn;
2959 }
2960
2961 /* A helper for the "quick" functions which attempts to read the line
2962 table for THIS_CU. */
2963
2964 static struct quick_file_names *
2965 dw2_get_file_names (struct objfile *objfile,
2966 struct dwarf2_per_cu_data *this_cu)
2967 {
2968 /* For TUs this should only be called on the parent group. */
2969 if (this_cu->is_debug_types)
2970 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2971
2972 if (this_cu->v.quick->file_names != NULL)
2973 return this_cu->v.quick->file_names;
2974 /* If we know there is no line data, no point in looking again. */
2975 if (this_cu->v.quick->no_file_data)
2976 return NULL;
2977
2978 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2979 in the stub for CUs, there's is no need to lookup the DWO file.
2980 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2981 DWO file. */
2982 if (this_cu->is_debug_types)
2983 {
2984 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2985
2986 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2987 dw2_get_file_names_reader, tu_group);
2988 }
2989 else
2990 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2991
2992 if (this_cu->v.quick->no_file_data)
2993 return NULL;
2994 return this_cu->v.quick->file_names;
2995 }
2996
2997 /* A helper for the "quick" functions which computes and caches the
2998 real path for a given file name from the line table. */
2999
3000 static const char *
3001 dw2_get_real_path (struct objfile *objfile,
3002 struct quick_file_names *qfn, int index)
3003 {
3004 if (qfn->real_names == NULL)
3005 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3006 qfn->num_file_names, sizeof (char *));
3007
3008 if (qfn->real_names[index] == NULL)
3009 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3010
3011 return qfn->real_names[index];
3012 }
3013
3014 static struct symtab *
3015 dw2_find_last_source_symtab (struct objfile *objfile)
3016 {
3017 int index;
3018
3019 dw2_setup (objfile);
3020 index = dwarf2_per_objfile->n_comp_units - 1;
3021 return dw2_instantiate_symtab (dw2_get_cu (index));
3022 }
3023
3024 /* Traversal function for dw2_forget_cached_source_info. */
3025
3026 static int
3027 dw2_free_cached_file_names (void **slot, void *info)
3028 {
3029 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3030
3031 if (file_data->real_names)
3032 {
3033 int i;
3034
3035 for (i = 0; i < file_data->num_file_names; ++i)
3036 {
3037 xfree ((void*) file_data->real_names[i]);
3038 file_data->real_names[i] = NULL;
3039 }
3040 }
3041
3042 return 1;
3043 }
3044
3045 static void
3046 dw2_forget_cached_source_info (struct objfile *objfile)
3047 {
3048 dw2_setup (objfile);
3049
3050 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3051 dw2_free_cached_file_names, NULL);
3052 }
3053
3054 /* Helper function for dw2_map_symtabs_matching_filename that expands
3055 the symtabs and calls the iterator. */
3056
3057 static int
3058 dw2_map_expand_apply (struct objfile *objfile,
3059 struct dwarf2_per_cu_data *per_cu,
3060 const char *name,
3061 const char *full_path, const char *real_path,
3062 int (*callback) (struct symtab *, void *),
3063 void *data)
3064 {
3065 struct symtab *last_made = objfile->symtabs;
3066
3067 /* Don't visit already-expanded CUs. */
3068 if (per_cu->v.quick->symtab)
3069 return 0;
3070
3071 /* This may expand more than one symtab, and we want to iterate over
3072 all of them. */
3073 dw2_instantiate_symtab (per_cu);
3074
3075 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3076 objfile->symtabs, last_made);
3077 }
3078
3079 /* Implementation of the map_symtabs_matching_filename method. */
3080
3081 static int
3082 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3083 const char *full_path, const char *real_path,
3084 int (*callback) (struct symtab *, void *),
3085 void *data)
3086 {
3087 int i;
3088 const char *name_basename = lbasename (name);
3089 int name_len = strlen (name);
3090 int is_abs = IS_ABSOLUTE_PATH (name);
3091
3092 dw2_setup (objfile);
3093
3094 dw2_build_type_unit_groups ();
3095
3096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3097 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3098 {
3099 int j;
3100 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3101 struct quick_file_names *file_data;
3102
3103 /* We only need to look at symtabs not already expanded. */
3104 if (per_cu->v.quick->symtab)
3105 continue;
3106
3107 file_data = dw2_get_file_names (objfile, per_cu);
3108 if (file_data == NULL)
3109 continue;
3110
3111 for (j = 0; j < file_data->num_file_names; ++j)
3112 {
3113 const char *this_name = file_data->file_names[j];
3114
3115 if (FILENAME_CMP (name, this_name) == 0
3116 || (!is_abs && compare_filenames_for_search (this_name,
3117 name, name_len)))
3118 {
3119 if (dw2_map_expand_apply (objfile, per_cu,
3120 name, full_path, real_path,
3121 callback, data))
3122 return 1;
3123 }
3124
3125 /* Before we invoke realpath, which can get expensive when many
3126 files are involved, do a quick comparison of the basenames. */
3127 if (! basenames_may_differ
3128 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3129 continue;
3130
3131 if (full_path != NULL)
3132 {
3133 const char *this_real_name = dw2_get_real_path (objfile,
3134 file_data, j);
3135
3136 if (this_real_name != NULL
3137 && (FILENAME_CMP (full_path, this_real_name) == 0
3138 || (!is_abs
3139 && compare_filenames_for_search (this_real_name,
3140 name, name_len))))
3141 {
3142 if (dw2_map_expand_apply (objfile, per_cu,
3143 name, full_path, real_path,
3144 callback, data))
3145 return 1;
3146 }
3147 }
3148
3149 if (real_path != NULL)
3150 {
3151 const char *this_real_name = dw2_get_real_path (objfile,
3152 file_data, j);
3153
3154 if (this_real_name != NULL
3155 && (FILENAME_CMP (real_path, this_real_name) == 0
3156 || (!is_abs
3157 && compare_filenames_for_search (this_real_name,
3158 name, name_len))))
3159 {
3160 if (dw2_map_expand_apply (objfile, per_cu,
3161 name, full_path, real_path,
3162 callback, data))
3163 return 1;
3164 }
3165 }
3166 }
3167 }
3168
3169 return 0;
3170 }
3171
3172 static struct symtab *
3173 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3174 const char *name, domain_enum domain)
3175 {
3176 /* We do all the work in the pre_expand_symtabs_matching hook
3177 instead. */
3178 return NULL;
3179 }
3180
3181 /* A helper function that expands all symtabs that hold an object
3182 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3183 symbols in block BLOCK_KIND. */
3184
3185 static void
3186 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3187 int want_specific_block,
3188 enum block_enum block_kind,
3189 const char *name, domain_enum domain)
3190 {
3191 struct mapped_index *index;
3192
3193 dw2_setup (objfile);
3194
3195 index = dwarf2_per_objfile->index_table;
3196
3197 /* index_table is NULL if OBJF_READNOW. */
3198 if (index)
3199 {
3200 offset_type *vec;
3201
3202 if (find_slot_in_mapped_hash (index, name, &vec))
3203 {
3204 offset_type i, len = MAYBE_SWAP (*vec);
3205 for (i = 0; i < len; ++i)
3206 {
3207 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3208 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3209 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3210 int want_static = block_kind != GLOBAL_BLOCK;
3211 /* This value is only valid for index versions >= 7. */
3212 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3213 gdb_index_symbol_kind symbol_kind =
3214 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3215 /* Only check the symbol attributes if they're present.
3216 Indices prior to version 7 don't record them,
3217 and indices >= 7 may elide them for certain symbols
3218 (gold does this). */
3219 int attrs_valid =
3220 (index->version >= 7
3221 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3222
3223 if (attrs_valid
3224 && want_specific_block
3225 && want_static != is_static)
3226 continue;
3227
3228 /* Only check the symbol's kind if it has one. */
3229 if (attrs_valid)
3230 {
3231 switch (domain)
3232 {
3233 case VAR_DOMAIN:
3234 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3235 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3236 /* Some types are also in VAR_DOMAIN. */
3237 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3238 continue;
3239 break;
3240 case STRUCT_DOMAIN:
3241 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3242 continue;
3243 break;
3244 case LABEL_DOMAIN:
3245 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3246 continue;
3247 break;
3248 default:
3249 break;
3250 }
3251 }
3252
3253 dw2_instantiate_symtab (per_cu);
3254 }
3255 }
3256 }
3257 }
3258
3259 static void
3260 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3261 enum block_enum block_kind, const char *name,
3262 domain_enum domain)
3263 {
3264 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3265 }
3266
3267 static void
3268 dw2_print_stats (struct objfile *objfile)
3269 {
3270 int i, count;
3271
3272 dw2_setup (objfile);
3273 count = 0;
3274 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3275 + dwarf2_per_objfile->n_type_units); ++i)
3276 {
3277 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3278
3279 if (!per_cu->v.quick->symtab)
3280 ++count;
3281 }
3282 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3283 }
3284
3285 static void
3286 dw2_dump (struct objfile *objfile)
3287 {
3288 /* Nothing worth printing. */
3289 }
3290
3291 static void
3292 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3293 struct section_offsets *delta)
3294 {
3295 /* There's nothing to relocate here. */
3296 }
3297
3298 static void
3299 dw2_expand_symtabs_for_function (struct objfile *objfile,
3300 const char *func_name)
3301 {
3302 /* Note: It doesn't matter what we pass for block_kind here. */
3303 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3304 VAR_DOMAIN);
3305 }
3306
3307 static void
3308 dw2_expand_all_symtabs (struct objfile *objfile)
3309 {
3310 int i;
3311
3312 dw2_setup (objfile);
3313
3314 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3315 + dwarf2_per_objfile->n_type_units); ++i)
3316 {
3317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3318
3319 dw2_instantiate_symtab (per_cu);
3320 }
3321 }
3322
3323 static void
3324 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3325 const char *filename)
3326 {
3327 int i;
3328
3329 dw2_setup (objfile);
3330
3331 /* We don't need to consider type units here.
3332 This is only called for examining code, e.g. expand_line_sal.
3333 There can be an order of magnitude (or more) more type units
3334 than comp units, and we avoid them if we can. */
3335
3336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3337 {
3338 int j;
3339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3340 struct quick_file_names *file_data;
3341
3342 /* We only need to look at symtabs not already expanded. */
3343 if (per_cu->v.quick->symtab)
3344 continue;
3345
3346 file_data = dw2_get_file_names (objfile, per_cu);
3347 if (file_data == NULL)
3348 continue;
3349
3350 for (j = 0; j < file_data->num_file_names; ++j)
3351 {
3352 const char *this_name = file_data->file_names[j];
3353 if (FILENAME_CMP (this_name, filename) == 0)
3354 {
3355 dw2_instantiate_symtab (per_cu);
3356 break;
3357 }
3358 }
3359 }
3360 }
3361
3362 /* A helper function for dw2_find_symbol_file that finds the primary
3363 file name for a given CU. This is a die_reader_func. */
3364
3365 static void
3366 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3367 gdb_byte *info_ptr,
3368 struct die_info *comp_unit_die,
3369 int has_children,
3370 void *data)
3371 {
3372 const char **result_ptr = data;
3373 struct dwarf2_cu *cu = reader->cu;
3374 struct attribute *attr;
3375
3376 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3377 if (attr == NULL)
3378 *result_ptr = NULL;
3379 else
3380 *result_ptr = DW_STRING (attr);
3381 }
3382
3383 static const char *
3384 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3385 {
3386 struct dwarf2_per_cu_data *per_cu;
3387 offset_type *vec;
3388 struct quick_file_names *file_data;
3389 const char *filename;
3390
3391 dw2_setup (objfile);
3392
3393 /* index_table is NULL if OBJF_READNOW. */
3394 if (!dwarf2_per_objfile->index_table)
3395 {
3396 struct symtab *s;
3397
3398 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3399 {
3400 struct blockvector *bv = BLOCKVECTOR (s);
3401 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3402 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3403
3404 if (sym)
3405 return sym->symtab->filename;
3406 }
3407 return NULL;
3408 }
3409
3410 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3411 name, &vec))
3412 return NULL;
3413
3414 /* Note that this just looks at the very first one named NAME -- but
3415 actually we are looking for a function. find_main_filename
3416 should be rewritten so that it doesn't require a custom hook. It
3417 could just use the ordinary symbol tables. */
3418 /* vec[0] is the length, which must always be >0. */
3419 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3420
3421 if (per_cu->v.quick->symtab != NULL)
3422 return per_cu->v.quick->symtab->filename;
3423
3424 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3425 dw2_get_primary_filename_reader, &filename);
3426
3427 return filename;
3428 }
3429
3430 static void
3431 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3432 struct objfile *objfile, int global,
3433 int (*callback) (struct block *,
3434 struct symbol *, void *),
3435 void *data, symbol_compare_ftype *match,
3436 symbol_compare_ftype *ordered_compare)
3437 {
3438 /* Currently unimplemented; used for Ada. The function can be called if the
3439 current language is Ada for a non-Ada objfile using GNU index. As Ada
3440 does not look for non-Ada symbols this function should just return. */
3441 }
3442
3443 static void
3444 dw2_expand_symtabs_matching
3445 (struct objfile *objfile,
3446 int (*file_matcher) (const char *, void *),
3447 int (*name_matcher) (const char *, void *),
3448 enum search_domain kind,
3449 void *data)
3450 {
3451 int i;
3452 offset_type iter;
3453 struct mapped_index *index;
3454
3455 dw2_setup (objfile);
3456
3457 /* index_table is NULL if OBJF_READNOW. */
3458 if (!dwarf2_per_objfile->index_table)
3459 return;
3460 index = dwarf2_per_objfile->index_table;
3461
3462 if (file_matcher != NULL)
3463 {
3464 struct cleanup *cleanup;
3465 htab_t visited_found, visited_not_found;
3466
3467 dw2_build_type_unit_groups ();
3468
3469 visited_found = htab_create_alloc (10,
3470 htab_hash_pointer, htab_eq_pointer,
3471 NULL, xcalloc, xfree);
3472 cleanup = make_cleanup_htab_delete (visited_found);
3473 visited_not_found = htab_create_alloc (10,
3474 htab_hash_pointer, htab_eq_pointer,
3475 NULL, xcalloc, xfree);
3476 make_cleanup_htab_delete (visited_not_found);
3477
3478 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3479 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3480 {
3481 int j;
3482 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3483 struct quick_file_names *file_data;
3484 void **slot;
3485
3486 per_cu->v.quick->mark = 0;
3487
3488 /* We only need to look at symtabs not already expanded. */
3489 if (per_cu->v.quick->symtab)
3490 continue;
3491
3492 file_data = dw2_get_file_names (objfile, per_cu);
3493 if (file_data == NULL)
3494 continue;
3495
3496 if (htab_find (visited_not_found, file_data) != NULL)
3497 continue;
3498 else if (htab_find (visited_found, file_data) != NULL)
3499 {
3500 per_cu->v.quick->mark = 1;
3501 continue;
3502 }
3503
3504 for (j = 0; j < file_data->num_file_names; ++j)
3505 {
3506 if (file_matcher (file_data->file_names[j], data))
3507 {
3508 per_cu->v.quick->mark = 1;
3509 break;
3510 }
3511 }
3512
3513 slot = htab_find_slot (per_cu->v.quick->mark
3514 ? visited_found
3515 : visited_not_found,
3516 file_data, INSERT);
3517 *slot = file_data;
3518 }
3519
3520 do_cleanups (cleanup);
3521 }
3522
3523 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3524 {
3525 offset_type idx = 2 * iter;
3526 const char *name;
3527 offset_type *vec, vec_len, vec_idx;
3528
3529 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3530 continue;
3531
3532 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3533
3534 if (! (*name_matcher) (name, data))
3535 continue;
3536
3537 /* The name was matched, now expand corresponding CUs that were
3538 marked. */
3539 vec = (offset_type *) (index->constant_pool
3540 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3541 vec_len = MAYBE_SWAP (vec[0]);
3542 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3543 {
3544 struct dwarf2_per_cu_data *per_cu;
3545 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3546 gdb_index_symbol_kind symbol_kind =
3547 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3548 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3549
3550 /* Don't crash on bad data. */
3551 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3552 + dwarf2_per_objfile->n_type_units))
3553 continue;
3554
3555 /* Only check the symbol's kind if it has one.
3556 Indices prior to version 7 don't record it. */
3557 if (index->version >= 7)
3558 {
3559 switch (kind)
3560 {
3561 case VARIABLES_DOMAIN:
3562 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3563 continue;
3564 break;
3565 case FUNCTIONS_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3567 continue;
3568 break;
3569 case TYPES_DOMAIN:
3570 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3571 continue;
3572 break;
3573 default:
3574 break;
3575 }
3576 }
3577
3578 per_cu = dw2_get_cu (cu_index);
3579 if (file_matcher == NULL || per_cu->v.quick->mark)
3580 dw2_instantiate_symtab (per_cu);
3581 }
3582 }
3583 }
3584
3585 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3586 symtab. */
3587
3588 static struct symtab *
3589 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3590 {
3591 int i;
3592
3593 if (BLOCKVECTOR (symtab) != NULL
3594 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3595 return symtab;
3596
3597 if (symtab->includes == NULL)
3598 return NULL;
3599
3600 for (i = 0; symtab->includes[i]; ++i)
3601 {
3602 struct symtab *s = symtab->includes[i];
3603
3604 s = recursively_find_pc_sect_symtab (s, pc);
3605 if (s != NULL)
3606 return s;
3607 }
3608
3609 return NULL;
3610 }
3611
3612 static struct symtab *
3613 dw2_find_pc_sect_symtab (struct objfile *objfile,
3614 struct minimal_symbol *msymbol,
3615 CORE_ADDR pc,
3616 struct obj_section *section,
3617 int warn_if_readin)
3618 {
3619 struct dwarf2_per_cu_data *data;
3620 struct symtab *result;
3621
3622 dw2_setup (objfile);
3623
3624 if (!objfile->psymtabs_addrmap)
3625 return NULL;
3626
3627 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3628 if (!data)
3629 return NULL;
3630
3631 if (warn_if_readin && data->v.quick->symtab)
3632 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3633 paddress (get_objfile_arch (objfile), pc));
3634
3635 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3636 gdb_assert (result != NULL);
3637 return result;
3638 }
3639
3640 static void
3641 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3642 void *data, int need_fullname)
3643 {
3644 int i;
3645 struct cleanup *cleanup;
3646 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3647 NULL, xcalloc, xfree);
3648
3649 cleanup = make_cleanup_htab_delete (visited);
3650 dw2_setup (objfile);
3651
3652 dw2_build_type_unit_groups ();
3653
3654 /* We can ignore file names coming from already-expanded CUs. */
3655 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3656 + dwarf2_per_objfile->n_type_units); ++i)
3657 {
3658 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3659
3660 if (per_cu->v.quick->symtab)
3661 {
3662 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3663 INSERT);
3664
3665 *slot = per_cu->v.quick->file_names;
3666 }
3667 }
3668
3669 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3670 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3671 {
3672 int j;
3673 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3674 struct quick_file_names *file_data;
3675 void **slot;
3676
3677 /* We only need to look at symtabs not already expanded. */
3678 if (per_cu->v.quick->symtab)
3679 continue;
3680
3681 file_data = dw2_get_file_names (objfile, per_cu);
3682 if (file_data == NULL)
3683 continue;
3684
3685 slot = htab_find_slot (visited, file_data, INSERT);
3686 if (*slot)
3687 {
3688 /* Already visited. */
3689 continue;
3690 }
3691 *slot = file_data;
3692
3693 for (j = 0; j < file_data->num_file_names; ++j)
3694 {
3695 const char *this_real_name;
3696
3697 if (need_fullname)
3698 this_real_name = dw2_get_real_path (objfile, file_data, j);
3699 else
3700 this_real_name = NULL;
3701 (*fun) (file_data->file_names[j], this_real_name, data);
3702 }
3703 }
3704
3705 do_cleanups (cleanup);
3706 }
3707
3708 static int
3709 dw2_has_symbols (struct objfile *objfile)
3710 {
3711 return 1;
3712 }
3713
3714 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3715 {
3716 dw2_has_symbols,
3717 dw2_find_last_source_symtab,
3718 dw2_forget_cached_source_info,
3719 dw2_map_symtabs_matching_filename,
3720 dw2_lookup_symbol,
3721 dw2_pre_expand_symtabs_matching,
3722 dw2_print_stats,
3723 dw2_dump,
3724 dw2_relocate,
3725 dw2_expand_symtabs_for_function,
3726 dw2_expand_all_symtabs,
3727 dw2_expand_symtabs_with_filename,
3728 dw2_find_symbol_file,
3729 dw2_map_matching_symbols,
3730 dw2_expand_symtabs_matching,
3731 dw2_find_pc_sect_symtab,
3732 dw2_map_symbol_filenames
3733 };
3734
3735 /* Initialize for reading DWARF for this objfile. Return 0 if this
3736 file will use psymtabs, or 1 if using the GNU index. */
3737
3738 int
3739 dwarf2_initialize_objfile (struct objfile *objfile)
3740 {
3741 /* If we're about to read full symbols, don't bother with the
3742 indices. In this case we also don't care if some other debug
3743 format is making psymtabs, because they are all about to be
3744 expanded anyway. */
3745 if ((objfile->flags & OBJF_READNOW))
3746 {
3747 int i;
3748
3749 dwarf2_per_objfile->using_index = 1;
3750 create_all_comp_units (objfile);
3751 create_all_type_units (objfile);
3752 dwarf2_per_objfile->quick_file_names_table =
3753 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3754
3755 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3756 + dwarf2_per_objfile->n_type_units); ++i)
3757 {
3758 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3759
3760 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3761 struct dwarf2_per_cu_quick_data);
3762 }
3763
3764 /* Return 1 so that gdb sees the "quick" functions. However,
3765 these functions will be no-ops because we will have expanded
3766 all symtabs. */
3767 return 1;
3768 }
3769
3770 if (dwarf2_read_index (objfile))
3771 return 1;
3772
3773 return 0;
3774 }
3775
3776 \f
3777
3778 /* Build a partial symbol table. */
3779
3780 void
3781 dwarf2_build_psymtabs (struct objfile *objfile)
3782 {
3783 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3784 {
3785 init_psymbol_list (objfile, 1024);
3786 }
3787
3788 dwarf2_build_psymtabs_hard (objfile);
3789 }
3790
3791 /* Return the total length of the CU described by HEADER. */
3792
3793 static unsigned int
3794 get_cu_length (const struct comp_unit_head *header)
3795 {
3796 return header->initial_length_size + header->length;
3797 }
3798
3799 /* Return TRUE if OFFSET is within CU_HEADER. */
3800
3801 static inline int
3802 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3803 {
3804 sect_offset bottom = { cu_header->offset.sect_off };
3805 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3806
3807 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3808 }
3809
3810 /* Find the base address of the compilation unit for range lists and
3811 location lists. It will normally be specified by DW_AT_low_pc.
3812 In DWARF-3 draft 4, the base address could be overridden by
3813 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3814 compilation units with discontinuous ranges. */
3815
3816 static void
3817 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3818 {
3819 struct attribute *attr;
3820
3821 cu->base_known = 0;
3822 cu->base_address = 0;
3823
3824 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3825 if (attr)
3826 {
3827 cu->base_address = DW_ADDR (attr);
3828 cu->base_known = 1;
3829 }
3830 else
3831 {
3832 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3833 if (attr)
3834 {
3835 cu->base_address = DW_ADDR (attr);
3836 cu->base_known = 1;
3837 }
3838 }
3839 }
3840
3841 /* Read in the comp unit header information from the debug_info at info_ptr.
3842 NOTE: This leaves members offset, first_die_offset to be filled in
3843 by the caller. */
3844
3845 static gdb_byte *
3846 read_comp_unit_head (struct comp_unit_head *cu_header,
3847 gdb_byte *info_ptr, bfd *abfd)
3848 {
3849 int signed_addr;
3850 unsigned int bytes_read;
3851
3852 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3853 cu_header->initial_length_size = bytes_read;
3854 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3855 info_ptr += bytes_read;
3856 cu_header->version = read_2_bytes (abfd, info_ptr);
3857 info_ptr += 2;
3858 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3859 &bytes_read);
3860 info_ptr += bytes_read;
3861 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3862 info_ptr += 1;
3863 signed_addr = bfd_get_sign_extend_vma (abfd);
3864 if (signed_addr < 0)
3865 internal_error (__FILE__, __LINE__,
3866 _("read_comp_unit_head: dwarf from non elf file"));
3867 cu_header->signed_addr_p = signed_addr;
3868
3869 return info_ptr;
3870 }
3871
3872 /* Helper function that returns the proper abbrev section for
3873 THIS_CU. */
3874
3875 static struct dwarf2_section_info *
3876 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3877 {
3878 struct dwarf2_section_info *abbrev;
3879
3880 if (this_cu->is_dwz)
3881 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3882 else
3883 abbrev = &dwarf2_per_objfile->abbrev;
3884
3885 return abbrev;
3886 }
3887
3888 /* Subroutine of read_and_check_comp_unit_head and
3889 read_and_check_type_unit_head to simplify them.
3890 Perform various error checking on the header. */
3891
3892 static void
3893 error_check_comp_unit_head (struct comp_unit_head *header,
3894 struct dwarf2_section_info *section,
3895 struct dwarf2_section_info *abbrev_section)
3896 {
3897 bfd *abfd = section->asection->owner;
3898 const char *filename = bfd_get_filename (abfd);
3899
3900 if (header->version != 2 && header->version != 3 && header->version != 4)
3901 error (_("Dwarf Error: wrong version in compilation unit header "
3902 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3903 filename);
3904
3905 if (header->abbrev_offset.sect_off
3906 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3907 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3908 "(offset 0x%lx + 6) [in module %s]"),
3909 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3910 filename);
3911
3912 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3913 avoid potential 32-bit overflow. */
3914 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3915 > section->size)
3916 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3917 "(offset 0x%lx + 0) [in module %s]"),
3918 (long) header->length, (long) header->offset.sect_off,
3919 filename);
3920 }
3921
3922 /* Read in a CU/TU header and perform some basic error checking.
3923 The contents of the header are stored in HEADER.
3924 The result is a pointer to the start of the first DIE. */
3925
3926 static gdb_byte *
3927 read_and_check_comp_unit_head (struct comp_unit_head *header,
3928 struct dwarf2_section_info *section,
3929 struct dwarf2_section_info *abbrev_section,
3930 gdb_byte *info_ptr,
3931 int is_debug_types_section)
3932 {
3933 gdb_byte *beg_of_comp_unit = info_ptr;
3934 bfd *abfd = section->asection->owner;
3935
3936 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3937
3938 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3939
3940 /* If we're reading a type unit, skip over the signature and
3941 type_offset fields. */
3942 if (is_debug_types_section)
3943 info_ptr += 8 /*signature*/ + header->offset_size;
3944
3945 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3946
3947 error_check_comp_unit_head (header, section, abbrev_section);
3948
3949 return info_ptr;
3950 }
3951
3952 /* Read in the types comp unit header information from .debug_types entry at
3953 types_ptr. The result is a pointer to one past the end of the header. */
3954
3955 static gdb_byte *
3956 read_and_check_type_unit_head (struct comp_unit_head *header,
3957 struct dwarf2_section_info *section,
3958 struct dwarf2_section_info *abbrev_section,
3959 gdb_byte *info_ptr,
3960 ULONGEST *signature,
3961 cu_offset *type_offset_in_tu)
3962 {
3963 gdb_byte *beg_of_comp_unit = info_ptr;
3964 bfd *abfd = section->asection->owner;
3965
3966 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3967
3968 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3969
3970 /* If we're reading a type unit, skip over the signature and
3971 type_offset fields. */
3972 if (signature != NULL)
3973 *signature = read_8_bytes (abfd, info_ptr);
3974 info_ptr += 8;
3975 if (type_offset_in_tu != NULL)
3976 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3977 header->offset_size);
3978 info_ptr += header->offset_size;
3979
3980 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3981
3982 error_check_comp_unit_head (header, section, abbrev_section);
3983
3984 return info_ptr;
3985 }
3986
3987 /* Fetch the abbreviation table offset from a comp or type unit header. */
3988
3989 static sect_offset
3990 read_abbrev_offset (struct dwarf2_section_info *section,
3991 sect_offset offset)
3992 {
3993 bfd *abfd = section->asection->owner;
3994 gdb_byte *info_ptr;
3995 unsigned int length, initial_length_size, offset_size;
3996 sect_offset abbrev_offset;
3997
3998 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3999 info_ptr = section->buffer + offset.sect_off;
4000 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4001 offset_size = initial_length_size == 4 ? 4 : 8;
4002 info_ptr += initial_length_size + 2 /*version*/;
4003 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4004 return abbrev_offset;
4005 }
4006
4007 /* Allocate a new partial symtab for file named NAME and mark this new
4008 partial symtab as being an include of PST. */
4009
4010 static void
4011 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4012 struct objfile *objfile)
4013 {
4014 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4015
4016 subpst->section_offsets = pst->section_offsets;
4017 subpst->textlow = 0;
4018 subpst->texthigh = 0;
4019
4020 subpst->dependencies = (struct partial_symtab **)
4021 obstack_alloc (&objfile->objfile_obstack,
4022 sizeof (struct partial_symtab *));
4023 subpst->dependencies[0] = pst;
4024 subpst->number_of_dependencies = 1;
4025
4026 subpst->globals_offset = 0;
4027 subpst->n_global_syms = 0;
4028 subpst->statics_offset = 0;
4029 subpst->n_static_syms = 0;
4030 subpst->symtab = NULL;
4031 subpst->read_symtab = pst->read_symtab;
4032 subpst->readin = 0;
4033
4034 /* No private part is necessary for include psymtabs. This property
4035 can be used to differentiate between such include psymtabs and
4036 the regular ones. */
4037 subpst->read_symtab_private = NULL;
4038 }
4039
4040 /* Read the Line Number Program data and extract the list of files
4041 included by the source file represented by PST. Build an include
4042 partial symtab for each of these included files. */
4043
4044 static void
4045 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4046 struct die_info *die,
4047 struct partial_symtab *pst)
4048 {
4049 struct line_header *lh = NULL;
4050 struct attribute *attr;
4051
4052 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4053 if (attr)
4054 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4055 if (lh == NULL)
4056 return; /* No linetable, so no includes. */
4057
4058 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4059 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4060
4061 free_line_header (lh);
4062 }
4063
4064 static hashval_t
4065 hash_signatured_type (const void *item)
4066 {
4067 const struct signatured_type *sig_type = item;
4068
4069 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4070 return sig_type->signature;
4071 }
4072
4073 static int
4074 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4075 {
4076 const struct signatured_type *lhs = item_lhs;
4077 const struct signatured_type *rhs = item_rhs;
4078
4079 return lhs->signature == rhs->signature;
4080 }
4081
4082 /* Allocate a hash table for signatured types. */
4083
4084 static htab_t
4085 allocate_signatured_type_table (struct objfile *objfile)
4086 {
4087 return htab_create_alloc_ex (41,
4088 hash_signatured_type,
4089 eq_signatured_type,
4090 NULL,
4091 &objfile->objfile_obstack,
4092 hashtab_obstack_allocate,
4093 dummy_obstack_deallocate);
4094 }
4095
4096 /* A helper function to add a signatured type CU to a table. */
4097
4098 static int
4099 add_signatured_type_cu_to_table (void **slot, void *datum)
4100 {
4101 struct signatured_type *sigt = *slot;
4102 struct signatured_type ***datap = datum;
4103
4104 **datap = sigt;
4105 ++*datap;
4106
4107 return 1;
4108 }
4109
4110 /* Create the hash table of all entries in the .debug_types section.
4111 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4112 NULL otherwise.
4113 Note: This function processes DWO files only, not DWP files.
4114 The result is a pointer to the hash table or NULL if there are
4115 no types. */
4116
4117 static htab_t
4118 create_debug_types_hash_table (struct dwo_file *dwo_file,
4119 VEC (dwarf2_section_info_def) *types)
4120 {
4121 struct objfile *objfile = dwarf2_per_objfile->objfile;
4122 htab_t types_htab = NULL;
4123 int ix;
4124 struct dwarf2_section_info *section;
4125 struct dwarf2_section_info *abbrev_section;
4126
4127 if (VEC_empty (dwarf2_section_info_def, types))
4128 return NULL;
4129
4130 abbrev_section = (dwo_file != NULL
4131 ? &dwo_file->sections.abbrev
4132 : &dwarf2_per_objfile->abbrev);
4133
4134 if (dwarf2_read_debug)
4135 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4136 dwo_file ? ".dwo" : "",
4137 bfd_get_filename (abbrev_section->asection->owner));
4138
4139 for (ix = 0;
4140 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4141 ++ix)
4142 {
4143 bfd *abfd;
4144 gdb_byte *info_ptr, *end_ptr;
4145 struct dwarf2_section_info *abbrev_section;
4146
4147 dwarf2_read_section (objfile, section);
4148 info_ptr = section->buffer;
4149
4150 if (info_ptr == NULL)
4151 continue;
4152
4153 /* We can't set abfd until now because the section may be empty or
4154 not present, in which case section->asection will be NULL. */
4155 abfd = section->asection->owner;
4156
4157 if (dwo_file)
4158 abbrev_section = &dwo_file->sections.abbrev;
4159 else
4160 abbrev_section = &dwarf2_per_objfile->abbrev;
4161
4162 if (types_htab == NULL)
4163 {
4164 if (dwo_file)
4165 types_htab = allocate_dwo_unit_table (objfile);
4166 else
4167 types_htab = allocate_signatured_type_table (objfile);
4168 }
4169
4170 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4171 because we don't need to read any dies: the signature is in the
4172 header. */
4173
4174 end_ptr = info_ptr + section->size;
4175 while (info_ptr < end_ptr)
4176 {
4177 sect_offset offset;
4178 cu_offset type_offset_in_tu;
4179 ULONGEST signature;
4180 struct signatured_type *sig_type;
4181 struct dwo_unit *dwo_tu;
4182 void **slot;
4183 gdb_byte *ptr = info_ptr;
4184 struct comp_unit_head header;
4185 unsigned int length;
4186
4187 offset.sect_off = ptr - section->buffer;
4188
4189 /* We need to read the type's signature in order to build the hash
4190 table, but we don't need anything else just yet. */
4191
4192 ptr = read_and_check_type_unit_head (&header, section,
4193 abbrev_section, ptr,
4194 &signature, &type_offset_in_tu);
4195
4196 length = get_cu_length (&header);
4197
4198 /* Skip dummy type units. */
4199 if (ptr >= info_ptr + length
4200 || peek_abbrev_code (abfd, ptr) == 0)
4201 {
4202 info_ptr += length;
4203 continue;
4204 }
4205
4206 if (dwo_file)
4207 {
4208 sig_type = NULL;
4209 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4210 struct dwo_unit);
4211 dwo_tu->dwo_file = dwo_file;
4212 dwo_tu->signature = signature;
4213 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4214 dwo_tu->info_or_types_section = section;
4215 dwo_tu->offset = offset;
4216 dwo_tu->length = length;
4217 }
4218 else
4219 {
4220 /* N.B.: type_offset is not usable if this type uses a DWO file.
4221 The real type_offset is in the DWO file. */
4222 dwo_tu = NULL;
4223 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4224 struct signatured_type);
4225 sig_type->signature = signature;
4226 sig_type->type_offset_in_tu = type_offset_in_tu;
4227 sig_type->per_cu.objfile = objfile;
4228 sig_type->per_cu.is_debug_types = 1;
4229 sig_type->per_cu.info_or_types_section = section;
4230 sig_type->per_cu.offset = offset;
4231 sig_type->per_cu.length = length;
4232 }
4233
4234 slot = htab_find_slot (types_htab,
4235 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4236 INSERT);
4237 gdb_assert (slot != NULL);
4238 if (*slot != NULL)
4239 {
4240 sect_offset dup_offset;
4241
4242 if (dwo_file)
4243 {
4244 const struct dwo_unit *dup_tu = *slot;
4245
4246 dup_offset = dup_tu->offset;
4247 }
4248 else
4249 {
4250 const struct signatured_type *dup_tu = *slot;
4251
4252 dup_offset = dup_tu->per_cu.offset;
4253 }
4254
4255 complaint (&symfile_complaints,
4256 _("debug type entry at offset 0x%x is duplicate to the "
4257 "entry at offset 0x%x, signature 0x%s"),
4258 offset.sect_off, dup_offset.sect_off,
4259 phex (signature, sizeof (signature)));
4260 }
4261 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4262
4263 if (dwarf2_read_debug)
4264 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4265 offset.sect_off,
4266 phex (signature, sizeof (signature)));
4267
4268 info_ptr += length;
4269 }
4270 }
4271
4272 return types_htab;
4273 }
4274
4275 /* Create the hash table of all entries in the .debug_types section,
4276 and initialize all_type_units.
4277 The result is zero if there is an error (e.g. missing .debug_types section),
4278 otherwise non-zero. */
4279
4280 static int
4281 create_all_type_units (struct objfile *objfile)
4282 {
4283 htab_t types_htab;
4284 struct signatured_type **iter;
4285
4286 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4287 if (types_htab == NULL)
4288 {
4289 dwarf2_per_objfile->signatured_types = NULL;
4290 return 0;
4291 }
4292
4293 dwarf2_per_objfile->signatured_types = types_htab;
4294
4295 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4296 dwarf2_per_objfile->all_type_units
4297 = obstack_alloc (&objfile->objfile_obstack,
4298 dwarf2_per_objfile->n_type_units
4299 * sizeof (struct signatured_type *));
4300 iter = &dwarf2_per_objfile->all_type_units[0];
4301 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4302 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4303 == dwarf2_per_objfile->n_type_units);
4304
4305 return 1;
4306 }
4307
4308 /* Lookup a signature based type for DW_FORM_ref_sig8.
4309 Returns NULL if signature SIG is not present in the table. */
4310
4311 static struct signatured_type *
4312 lookup_signatured_type (ULONGEST sig)
4313 {
4314 struct signatured_type find_entry, *entry;
4315
4316 if (dwarf2_per_objfile->signatured_types == NULL)
4317 {
4318 complaint (&symfile_complaints,
4319 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4320 return NULL;
4321 }
4322
4323 find_entry.signature = sig;
4324 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4325 return entry;
4326 }
4327 \f
4328 /* Low level DIE reading support. */
4329
4330 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4331
4332 static void
4333 init_cu_die_reader (struct die_reader_specs *reader,
4334 struct dwarf2_cu *cu,
4335 struct dwarf2_section_info *section,
4336 struct dwo_file *dwo_file)
4337 {
4338 gdb_assert (section->readin && section->buffer != NULL);
4339 reader->abfd = section->asection->owner;
4340 reader->cu = cu;
4341 reader->dwo_file = dwo_file;
4342 reader->die_section = section;
4343 reader->buffer = section->buffer;
4344 reader->buffer_end = section->buffer + section->size;
4345 }
4346
4347 /* Initialize a CU (or TU) and read its DIEs.
4348 If the CU defers to a DWO file, read the DWO file as well.
4349
4350 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4351 Otherwise the table specified in the comp unit header is read in and used.
4352 This is an optimization for when we already have the abbrev table.
4353
4354 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4355 Otherwise, a new CU is allocated with xmalloc.
4356
4357 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4358 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4359
4360 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4361 linker) then DIE_READER_FUNC will not get called. */
4362
4363 static void
4364 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4365 struct abbrev_table *abbrev_table,
4366 int use_existing_cu, int keep,
4367 die_reader_func_ftype *die_reader_func,
4368 void *data)
4369 {
4370 struct objfile *objfile = dwarf2_per_objfile->objfile;
4371 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4372 bfd *abfd = section->asection->owner;
4373 struct dwarf2_cu *cu;
4374 gdb_byte *begin_info_ptr, *info_ptr;
4375 struct die_reader_specs reader;
4376 struct die_info *comp_unit_die;
4377 int has_children;
4378 struct attribute *attr;
4379 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4380 struct signatured_type *sig_type = NULL;
4381 struct dwarf2_section_info *abbrev_section;
4382 /* Non-zero if CU currently points to a DWO file and we need to
4383 reread it. When this happens we need to reread the skeleton die
4384 before we can reread the DWO file. */
4385 int rereading_dwo_cu = 0;
4386
4387 if (dwarf2_die_debug)
4388 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4389 this_cu->is_debug_types ? "type" : "comp",
4390 this_cu->offset.sect_off);
4391
4392 if (use_existing_cu)
4393 gdb_assert (keep);
4394
4395 cleanups = make_cleanup (null_cleanup, NULL);
4396
4397 /* This is cheap if the section is already read in. */
4398 dwarf2_read_section (objfile, section);
4399
4400 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4401
4402 abbrev_section = get_abbrev_section_for_cu (this_cu);
4403
4404 if (use_existing_cu && this_cu->cu != NULL)
4405 {
4406 cu = this_cu->cu;
4407
4408 /* If this CU is from a DWO file we need to start over, we need to
4409 refetch the attributes from the skeleton CU.
4410 This could be optimized by retrieving those attributes from when we
4411 were here the first time: the previous comp_unit_die was stored in
4412 comp_unit_obstack. But there's no data yet that we need this
4413 optimization. */
4414 if (cu->dwo_unit != NULL)
4415 rereading_dwo_cu = 1;
4416 }
4417 else
4418 {
4419 /* If !use_existing_cu, this_cu->cu must be NULL. */
4420 gdb_assert (this_cu->cu == NULL);
4421
4422 cu = xmalloc (sizeof (*cu));
4423 init_one_comp_unit (cu, this_cu);
4424
4425 /* If an error occurs while loading, release our storage. */
4426 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4427 }
4428
4429 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4430 {
4431 /* We already have the header, there's no need to read it in again. */
4432 info_ptr += cu->header.first_die_offset.cu_off;
4433 }
4434 else
4435 {
4436 if (this_cu->is_debug_types)
4437 {
4438 ULONGEST signature;
4439 cu_offset type_offset_in_tu;
4440
4441 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4442 abbrev_section, info_ptr,
4443 &signature,
4444 &type_offset_in_tu);
4445
4446 /* Since per_cu is the first member of struct signatured_type,
4447 we can go from a pointer to one to a pointer to the other. */
4448 sig_type = (struct signatured_type *) this_cu;
4449 gdb_assert (sig_type->signature == signature);
4450 gdb_assert (sig_type->type_offset_in_tu.cu_off
4451 == type_offset_in_tu.cu_off);
4452 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4453
4454 /* LENGTH has not been set yet for type units if we're
4455 using .gdb_index. */
4456 this_cu->length = get_cu_length (&cu->header);
4457
4458 /* Establish the type offset that can be used to lookup the type. */
4459 sig_type->type_offset_in_section.sect_off =
4460 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4461 }
4462 else
4463 {
4464 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4465 abbrev_section,
4466 info_ptr, 0);
4467
4468 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4469 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4470 }
4471 }
4472
4473 /* Skip dummy compilation units. */
4474 if (info_ptr >= begin_info_ptr + this_cu->length
4475 || peek_abbrev_code (abfd, info_ptr) == 0)
4476 {
4477 do_cleanups (cleanups);
4478 return;
4479 }
4480
4481 /* If we don't have them yet, read the abbrevs for this compilation unit.
4482 And if we need to read them now, make sure they're freed when we're
4483 done. Note that it's important that if the CU had an abbrev table
4484 on entry we don't free it when we're done: Somewhere up the call stack
4485 it may be in use. */
4486 if (abbrev_table != NULL)
4487 {
4488 gdb_assert (cu->abbrev_table == NULL);
4489 gdb_assert (cu->header.abbrev_offset.sect_off
4490 == abbrev_table->offset.sect_off);
4491 cu->abbrev_table = abbrev_table;
4492 }
4493 else if (cu->abbrev_table == NULL)
4494 {
4495 dwarf2_read_abbrevs (cu, abbrev_section);
4496 make_cleanup (dwarf2_free_abbrev_table, cu);
4497 }
4498 else if (rereading_dwo_cu)
4499 {
4500 dwarf2_free_abbrev_table (cu);
4501 dwarf2_read_abbrevs (cu, abbrev_section);
4502 }
4503
4504 /* Read the top level CU/TU die. */
4505 init_cu_die_reader (&reader, cu, section, NULL);
4506 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4507
4508 /* If we have a DWO stub, process it and then read in the DWO file.
4509 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4510 a DWO CU, that this test will fail. */
4511 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4512 if (attr)
4513 {
4514 char *dwo_name = DW_STRING (attr);
4515 const char *comp_dir_string;
4516 struct dwo_unit *dwo_unit;
4517 ULONGEST signature; /* Or dwo_id. */
4518 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4519 int i,num_extra_attrs;
4520 struct dwarf2_section_info *dwo_abbrev_section;
4521
4522 if (has_children)
4523 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4524 " has children (offset 0x%x) [in module %s]"),
4525 this_cu->offset.sect_off, bfd_get_filename (abfd));
4526
4527 /* These attributes aren't processed until later:
4528 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4529 However, the attribute is found in the stub which we won't have later.
4530 In order to not impose this complication on the rest of the code,
4531 we read them here and copy them to the DWO CU/TU die. */
4532
4533 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4534 DWO file. */
4535 stmt_list = NULL;
4536 if (! this_cu->is_debug_types)
4537 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4538 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4539 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4540 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4541 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4542
4543 /* There should be a DW_AT_addr_base attribute here (if needed).
4544 We need the value before we can process DW_FORM_GNU_addr_index. */
4545 cu->addr_base = 0;
4546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4547 if (attr)
4548 cu->addr_base = DW_UNSND (attr);
4549
4550 /* There should be a DW_AT_ranges_base attribute here (if needed).
4551 We need the value before we can process DW_AT_ranges. */
4552 cu->ranges_base = 0;
4553 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4554 if (attr)
4555 cu->ranges_base = DW_UNSND (attr);
4556
4557 if (this_cu->is_debug_types)
4558 {
4559 gdb_assert (sig_type != NULL);
4560 signature = sig_type->signature;
4561 }
4562 else
4563 {
4564 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4565 if (! attr)
4566 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4567 dwo_name);
4568 signature = DW_UNSND (attr);
4569 }
4570
4571 /* We may need the comp_dir in order to find the DWO file. */
4572 comp_dir_string = NULL;
4573 if (comp_dir)
4574 comp_dir_string = DW_STRING (comp_dir);
4575
4576 if (this_cu->is_debug_types)
4577 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4578 else
4579 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4580 signature);
4581
4582 if (dwo_unit == NULL)
4583 {
4584 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4585 " with ID %s [in module %s]"),
4586 this_cu->offset.sect_off,
4587 phex (signature, sizeof (signature)),
4588 objfile->name);
4589 }
4590
4591 /* Set up for reading the DWO CU/TU. */
4592 cu->dwo_unit = dwo_unit;
4593 section = dwo_unit->info_or_types_section;
4594 dwarf2_read_section (objfile, section);
4595 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4596 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4597 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4598
4599 if (this_cu->is_debug_types)
4600 {
4601 ULONGEST signature;
4602 cu_offset type_offset_in_tu;
4603
4604 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4605 dwo_abbrev_section,
4606 info_ptr,
4607 &signature,
4608 &type_offset_in_tu);
4609 gdb_assert (sig_type->signature == signature);
4610 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4611 /* For DWOs coming from DWP files, we don't know the CU length
4612 nor the type's offset in the TU until now. */
4613 dwo_unit->length = get_cu_length (&cu->header);
4614 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4615
4616 /* Establish the type offset that can be used to lookup the type.
4617 For DWO files, we don't know it until now. */
4618 sig_type->type_offset_in_section.sect_off =
4619 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4620 }
4621 else
4622 {
4623 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4624 dwo_abbrev_section,
4625 info_ptr, 0);
4626 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4627 /* For DWOs coming from DWP files, we don't know the CU length
4628 until now. */
4629 dwo_unit->length = get_cu_length (&cu->header);
4630 }
4631
4632 /* Discard the original CU's abbrev table, and read the DWO's. */
4633 if (abbrev_table == NULL)
4634 {
4635 dwarf2_free_abbrev_table (cu);
4636 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4637 }
4638 else
4639 {
4640 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4641 make_cleanup (dwarf2_free_abbrev_table, cu);
4642 }
4643
4644 /* Read in the die, but leave space to copy over the attributes
4645 from the stub. This has the benefit of simplifying the rest of
4646 the code - all the real work is done here. */
4647 num_extra_attrs = ((stmt_list != NULL)
4648 + (low_pc != NULL)
4649 + (high_pc != NULL)
4650 + (ranges != NULL)
4651 + (comp_dir != NULL));
4652 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4653 &has_children, num_extra_attrs);
4654
4655 /* Copy over the attributes from the stub to the DWO die. */
4656 i = comp_unit_die->num_attrs;
4657 if (stmt_list != NULL)
4658 comp_unit_die->attrs[i++] = *stmt_list;
4659 if (low_pc != NULL)
4660 comp_unit_die->attrs[i++] = *low_pc;
4661 if (high_pc != NULL)
4662 comp_unit_die->attrs[i++] = *high_pc;
4663 if (ranges != NULL)
4664 comp_unit_die->attrs[i++] = *ranges;
4665 if (comp_dir != NULL)
4666 comp_unit_die->attrs[i++] = *comp_dir;
4667 comp_unit_die->num_attrs += num_extra_attrs;
4668
4669 /* Skip dummy compilation units. */
4670 if (info_ptr >= begin_info_ptr + dwo_unit->length
4671 || peek_abbrev_code (abfd, info_ptr) == 0)
4672 {
4673 do_cleanups (cleanups);
4674 return;
4675 }
4676 }
4677
4678 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4679
4680 if (free_cu_cleanup != NULL)
4681 {
4682 if (keep)
4683 {
4684 /* We've successfully allocated this compilation unit. Let our
4685 caller clean it up when finished with it. */
4686 discard_cleanups (free_cu_cleanup);
4687
4688 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4689 So we have to manually free the abbrev table. */
4690 dwarf2_free_abbrev_table (cu);
4691
4692 /* Link this CU into read_in_chain. */
4693 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4694 dwarf2_per_objfile->read_in_chain = this_cu;
4695 }
4696 else
4697 do_cleanups (free_cu_cleanup);
4698 }
4699
4700 do_cleanups (cleanups);
4701 }
4702
4703 /* Read CU/TU THIS_CU in section SECTION,
4704 but do not follow DW_AT_GNU_dwo_name if present.
4705 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4706 to have already done the lookup to find the DWO/DWP file).
4707
4708 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4709 THIS_CU->is_debug_types, but nothing else.
4710
4711 We fill in THIS_CU->length.
4712
4713 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4714 linker) then DIE_READER_FUNC will not get called.
4715
4716 THIS_CU->cu is always freed when done.
4717 This is done in order to not leave THIS_CU->cu in a state where we have
4718 to care whether it refers to the "main" CU or the DWO CU. */
4719
4720 static void
4721 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4722 struct dwarf2_section_info *abbrev_section,
4723 struct dwo_file *dwo_file,
4724 die_reader_func_ftype *die_reader_func,
4725 void *data)
4726 {
4727 struct objfile *objfile = dwarf2_per_objfile->objfile;
4728 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4729 bfd *abfd = section->asection->owner;
4730 struct dwarf2_cu cu;
4731 gdb_byte *begin_info_ptr, *info_ptr;
4732 struct die_reader_specs reader;
4733 struct cleanup *cleanups;
4734 struct die_info *comp_unit_die;
4735 int has_children;
4736
4737 if (dwarf2_die_debug)
4738 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4739 this_cu->is_debug_types ? "type" : "comp",
4740 this_cu->offset.sect_off);
4741
4742 gdb_assert (this_cu->cu == NULL);
4743
4744 /* This is cheap if the section is already read in. */
4745 dwarf2_read_section (objfile, section);
4746
4747 init_one_comp_unit (&cu, this_cu);
4748
4749 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4750
4751 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4752 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4753 abbrev_section, info_ptr,
4754 this_cu->is_debug_types);
4755
4756 this_cu->length = get_cu_length (&cu.header);
4757
4758 /* Skip dummy compilation units. */
4759 if (info_ptr >= begin_info_ptr + this_cu->length
4760 || peek_abbrev_code (abfd, info_ptr) == 0)
4761 {
4762 do_cleanups (cleanups);
4763 return;
4764 }
4765
4766 dwarf2_read_abbrevs (&cu, abbrev_section);
4767 make_cleanup (dwarf2_free_abbrev_table, &cu);
4768
4769 init_cu_die_reader (&reader, &cu, section, dwo_file);
4770 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4771
4772 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4773
4774 do_cleanups (cleanups);
4775 }
4776
4777 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4778 does not lookup the specified DWO file.
4779 This cannot be used to read DWO files.
4780
4781 THIS_CU->cu is always freed when done.
4782 This is done in order to not leave THIS_CU->cu in a state where we have
4783 to care whether it refers to the "main" CU or the DWO CU.
4784 We can revisit this if the data shows there's a performance issue. */
4785
4786 static void
4787 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4788 die_reader_func_ftype *die_reader_func,
4789 void *data)
4790 {
4791 init_cutu_and_read_dies_no_follow (this_cu,
4792 get_abbrev_section_for_cu (this_cu),
4793 NULL,
4794 die_reader_func, data);
4795 }
4796
4797 /* Create a psymtab named NAME and assign it to PER_CU.
4798
4799 The caller must fill in the following details:
4800 dirname, textlow, texthigh. */
4801
4802 static struct partial_symtab *
4803 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4804 {
4805 struct objfile *objfile = per_cu->objfile;
4806 struct partial_symtab *pst;
4807
4808 pst = start_psymtab_common (objfile, objfile->section_offsets,
4809 name, 0,
4810 objfile->global_psymbols.next,
4811 objfile->static_psymbols.next);
4812
4813 pst->psymtabs_addrmap_supported = 1;
4814
4815 /* This is the glue that links PST into GDB's symbol API. */
4816 pst->read_symtab_private = per_cu;
4817 pst->read_symtab = dwarf2_psymtab_to_symtab;
4818 per_cu->v.psymtab = pst;
4819
4820 return pst;
4821 }
4822
4823 /* die_reader_func for process_psymtab_comp_unit. */
4824
4825 static void
4826 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4827 gdb_byte *info_ptr,
4828 struct die_info *comp_unit_die,
4829 int has_children,
4830 void *data)
4831 {
4832 struct dwarf2_cu *cu = reader->cu;
4833 struct objfile *objfile = cu->objfile;
4834 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4835 struct attribute *attr;
4836 CORE_ADDR baseaddr;
4837 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4838 struct partial_symtab *pst;
4839 int has_pc_info;
4840 const char *filename;
4841 int *want_partial_unit_ptr = data;
4842
4843 if (comp_unit_die->tag == DW_TAG_partial_unit
4844 && (want_partial_unit_ptr == NULL
4845 || !*want_partial_unit_ptr))
4846 return;
4847
4848 gdb_assert (! per_cu->is_debug_types);
4849
4850 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4851
4852 cu->list_in_scope = &file_symbols;
4853
4854 /* Allocate a new partial symbol table structure. */
4855 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4856 if (attr == NULL || !DW_STRING (attr))
4857 filename = "";
4858 else
4859 filename = DW_STRING (attr);
4860
4861 pst = create_partial_symtab (per_cu, filename);
4862
4863 /* This must be done before calling dwarf2_build_include_psymtabs. */
4864 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4865 if (attr != NULL)
4866 pst->dirname = DW_STRING (attr);
4867
4868 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4869
4870 dwarf2_find_base_address (comp_unit_die, cu);
4871
4872 /* Possibly set the default values of LOWPC and HIGHPC from
4873 `DW_AT_ranges'. */
4874 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4875 &best_highpc, cu, pst);
4876 if (has_pc_info == 1 && best_lowpc < best_highpc)
4877 /* Store the contiguous range if it is not empty; it can be empty for
4878 CUs with no code. */
4879 addrmap_set_empty (objfile->psymtabs_addrmap,
4880 best_lowpc + baseaddr,
4881 best_highpc + baseaddr - 1, pst);
4882
4883 /* Check if comp unit has_children.
4884 If so, read the rest of the partial symbols from this comp unit.
4885 If not, there's no more debug_info for this comp unit. */
4886 if (has_children)
4887 {
4888 struct partial_die_info *first_die;
4889 CORE_ADDR lowpc, highpc;
4890
4891 lowpc = ((CORE_ADDR) -1);
4892 highpc = ((CORE_ADDR) 0);
4893
4894 first_die = load_partial_dies (reader, info_ptr, 1);
4895
4896 scan_partial_symbols (first_die, &lowpc, &highpc,
4897 ! has_pc_info, cu);
4898
4899 /* If we didn't find a lowpc, set it to highpc to avoid
4900 complaints from `maint check'. */
4901 if (lowpc == ((CORE_ADDR) -1))
4902 lowpc = highpc;
4903
4904 /* If the compilation unit didn't have an explicit address range,
4905 then use the information extracted from its child dies. */
4906 if (! has_pc_info)
4907 {
4908 best_lowpc = lowpc;
4909 best_highpc = highpc;
4910 }
4911 }
4912 pst->textlow = best_lowpc + baseaddr;
4913 pst->texthigh = best_highpc + baseaddr;
4914
4915 pst->n_global_syms = objfile->global_psymbols.next -
4916 (objfile->global_psymbols.list + pst->globals_offset);
4917 pst->n_static_syms = objfile->static_psymbols.next -
4918 (objfile->static_psymbols.list + pst->statics_offset);
4919 sort_pst_symbols (pst);
4920
4921 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4922 {
4923 int i;
4924 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4925 struct dwarf2_per_cu_data *iter;
4926
4927 /* Fill in 'dependencies' here; we fill in 'users' in a
4928 post-pass. */
4929 pst->number_of_dependencies = len;
4930 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4931 len * sizeof (struct symtab *));
4932 for (i = 0;
4933 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4934 i, iter);
4935 ++i)
4936 pst->dependencies[i] = iter->v.psymtab;
4937
4938 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4939 }
4940
4941 /* Get the list of files included in the current compilation unit,
4942 and build a psymtab for each of them. */
4943 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4944
4945 if (dwarf2_read_debug)
4946 {
4947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4948
4949 fprintf_unfiltered (gdb_stdlog,
4950 "Psymtab for %s unit @0x%x: %s - %s"
4951 ", %d global, %d static syms\n",
4952 per_cu->is_debug_types ? "type" : "comp",
4953 per_cu->offset.sect_off,
4954 paddress (gdbarch, pst->textlow),
4955 paddress (gdbarch, pst->texthigh),
4956 pst->n_global_syms, pst->n_static_syms);
4957 }
4958 }
4959
4960 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4961 Process compilation unit THIS_CU for a psymtab. */
4962
4963 static void
4964 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4965 int want_partial_unit)
4966 {
4967 /* If this compilation unit was already read in, free the
4968 cached copy in order to read it in again. This is
4969 necessary because we skipped some symbols when we first
4970 read in the compilation unit (see load_partial_dies).
4971 This problem could be avoided, but the benefit is unclear. */
4972 if (this_cu->cu != NULL)
4973 free_one_cached_comp_unit (this_cu);
4974
4975 gdb_assert (! this_cu->is_debug_types);
4976 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4977 process_psymtab_comp_unit_reader,
4978 &want_partial_unit);
4979
4980 /* Age out any secondary CUs. */
4981 age_cached_comp_units ();
4982 }
4983
4984 static hashval_t
4985 hash_type_unit_group (const void *item)
4986 {
4987 const struct type_unit_group *tu_group = item;
4988
4989 return hash_stmt_list_entry (&tu_group->hash);
4990 }
4991
4992 static int
4993 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4994 {
4995 const struct type_unit_group *lhs = item_lhs;
4996 const struct type_unit_group *rhs = item_rhs;
4997
4998 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4999 }
5000
5001 /* Allocate a hash table for type unit groups. */
5002
5003 static htab_t
5004 allocate_type_unit_groups_table (void)
5005 {
5006 return htab_create_alloc_ex (3,
5007 hash_type_unit_group,
5008 eq_type_unit_group,
5009 NULL,
5010 &dwarf2_per_objfile->objfile->objfile_obstack,
5011 hashtab_obstack_allocate,
5012 dummy_obstack_deallocate);
5013 }
5014
5015 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5016 partial symtabs. We combine several TUs per psymtab to not let the size
5017 of any one psymtab grow too big. */
5018 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5019 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5020
5021 /* Helper routine for get_type_unit_group.
5022 Create the type_unit_group object used to hold one or more TUs. */
5023
5024 static struct type_unit_group *
5025 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5026 {
5027 struct objfile *objfile = dwarf2_per_objfile->objfile;
5028 struct dwarf2_per_cu_data *per_cu;
5029 struct type_unit_group *tu_group;
5030
5031 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5032 struct type_unit_group);
5033 per_cu = &tu_group->per_cu;
5034 per_cu->objfile = objfile;
5035 per_cu->is_debug_types = 1;
5036 per_cu->s.type_unit_group = tu_group;
5037
5038 if (dwarf2_per_objfile->using_index)
5039 {
5040 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5041 struct dwarf2_per_cu_quick_data);
5042 tu_group->t.first_tu = cu->per_cu;
5043 }
5044 else
5045 {
5046 unsigned int line_offset = line_offset_struct.sect_off;
5047 struct partial_symtab *pst;
5048 char *name;
5049
5050 /* Give the symtab a useful name for debug purposes. */
5051 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5052 name = xstrprintf ("<type_units_%d>",
5053 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5054 else
5055 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5056
5057 pst = create_partial_symtab (per_cu, name);
5058 pst->anonymous = 1;
5059
5060 xfree (name);
5061 }
5062
5063 tu_group->hash.dwo_unit = cu->dwo_unit;
5064 tu_group->hash.line_offset = line_offset_struct;
5065
5066 return tu_group;
5067 }
5068
5069 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5070 STMT_LIST is a DW_AT_stmt_list attribute. */
5071
5072 static struct type_unit_group *
5073 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5074 {
5075 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5076 struct type_unit_group *tu_group;
5077 void **slot;
5078 unsigned int line_offset;
5079 struct type_unit_group type_unit_group_for_lookup;
5080
5081 if (dwarf2_per_objfile->type_unit_groups == NULL)
5082 {
5083 dwarf2_per_objfile->type_unit_groups =
5084 allocate_type_unit_groups_table ();
5085 }
5086
5087 /* Do we need to create a new group, or can we use an existing one? */
5088
5089 if (stmt_list)
5090 {
5091 line_offset = DW_UNSND (stmt_list);
5092 ++tu_stats->nr_symtab_sharers;
5093 }
5094 else
5095 {
5096 /* Ugh, no stmt_list. Rare, but we have to handle it.
5097 We can do various things here like create one group per TU or
5098 spread them over multiple groups to split up the expansion work.
5099 To avoid worst case scenarios (too many groups or too large groups)
5100 we, umm, group them in bunches. */
5101 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5102 | (tu_stats->nr_stmt_less_type_units
5103 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5104 ++tu_stats->nr_stmt_less_type_units;
5105 }
5106
5107 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5108 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5109 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5110 &type_unit_group_for_lookup, INSERT);
5111 if (*slot != NULL)
5112 {
5113 tu_group = *slot;
5114 gdb_assert (tu_group != NULL);
5115 }
5116 else
5117 {
5118 sect_offset line_offset_struct;
5119
5120 line_offset_struct.sect_off = line_offset;
5121 tu_group = create_type_unit_group (cu, line_offset_struct);
5122 *slot = tu_group;
5123 ++tu_stats->nr_symtabs;
5124 }
5125
5126 return tu_group;
5127 }
5128
5129 /* Struct used to sort TUs by their abbreviation table offset. */
5130
5131 struct tu_abbrev_offset
5132 {
5133 struct signatured_type *sig_type;
5134 sect_offset abbrev_offset;
5135 };
5136
5137 /* Helper routine for build_type_unit_groups, passed to qsort. */
5138
5139 static int
5140 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5141 {
5142 const struct tu_abbrev_offset * const *a = ap;
5143 const struct tu_abbrev_offset * const *b = bp;
5144 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5145 unsigned int boff = (*b)->abbrev_offset.sect_off;
5146
5147 return (aoff > boff) - (aoff < boff);
5148 }
5149
5150 /* A helper function to add a type_unit_group to a table. */
5151
5152 static int
5153 add_type_unit_group_to_table (void **slot, void *datum)
5154 {
5155 struct type_unit_group *tu_group = *slot;
5156 struct type_unit_group ***datap = datum;
5157
5158 **datap = tu_group;
5159 ++*datap;
5160
5161 return 1;
5162 }
5163
5164 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5165 each one passing FUNC,DATA.
5166
5167 The efficiency is because we sort TUs by the abbrev table they use and
5168 only read each abbrev table once. In one program there are 200K TUs
5169 sharing 8K abbrev tables.
5170
5171 The main purpose of this function is to support building the
5172 dwarf2_per_objfile->type_unit_groups table.
5173 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5174 can collapse the search space by grouping them by stmt_list.
5175 The savings can be significant, in the same program from above the 200K TUs
5176 share 8K stmt_list tables.
5177
5178 FUNC is expected to call get_type_unit_group, which will create the
5179 struct type_unit_group if necessary and add it to
5180 dwarf2_per_objfile->type_unit_groups. */
5181
5182 static void
5183 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5184 {
5185 struct objfile *objfile = dwarf2_per_objfile->objfile;
5186 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5187 struct cleanup *cleanups;
5188 struct abbrev_table *abbrev_table;
5189 sect_offset abbrev_offset;
5190 struct tu_abbrev_offset *sorted_by_abbrev;
5191 struct type_unit_group **iter;
5192 int i;
5193
5194 /* It's up to the caller to not call us multiple times. */
5195 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5196
5197 if (dwarf2_per_objfile->n_type_units == 0)
5198 return;
5199
5200 /* TUs typically share abbrev tables, and there can be way more TUs than
5201 abbrev tables. Sort by abbrev table to reduce the number of times we
5202 read each abbrev table in.
5203 Alternatives are to punt or to maintain a cache of abbrev tables.
5204 This is simpler and efficient enough for now.
5205
5206 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5207 symtab to use). Typically TUs with the same abbrev offset have the same
5208 stmt_list value too so in practice this should work well.
5209
5210 The basic algorithm here is:
5211
5212 sort TUs by abbrev table
5213 for each TU with same abbrev table:
5214 read abbrev table if first user
5215 read TU top level DIE
5216 [IWBN if DWO skeletons had DW_AT_stmt_list]
5217 call FUNC */
5218
5219 if (dwarf2_read_debug)
5220 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5221
5222 /* Sort in a separate table to maintain the order of all_type_units
5223 for .gdb_index: TU indices directly index all_type_units. */
5224 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5225 dwarf2_per_objfile->n_type_units);
5226 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5227 {
5228 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5229
5230 sorted_by_abbrev[i].sig_type = sig_type;
5231 sorted_by_abbrev[i].abbrev_offset =
5232 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5233 sig_type->per_cu.offset);
5234 }
5235 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5236 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5237 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5238
5239 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5240 called any number of times, so we don't reset tu_stats here. */
5241
5242 abbrev_offset.sect_off = ~(unsigned) 0;
5243 abbrev_table = NULL;
5244 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5245
5246 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5247 {
5248 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5249
5250 /* Switch to the next abbrev table if necessary. */
5251 if (abbrev_table == NULL
5252 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5253 {
5254 if (abbrev_table != NULL)
5255 {
5256 abbrev_table_free (abbrev_table);
5257 /* Reset to NULL in case abbrev_table_read_table throws
5258 an error: abbrev_table_free_cleanup will get called. */
5259 abbrev_table = NULL;
5260 }
5261 abbrev_offset = tu->abbrev_offset;
5262 abbrev_table =
5263 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5264 abbrev_offset);
5265 ++tu_stats->nr_uniq_abbrev_tables;
5266 }
5267
5268 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5269 func, data);
5270 }
5271
5272 /* Create a vector of pointers to primary type units to make it easy to
5273 iterate over them and CUs. See dw2_get_primary_cu. */
5274 dwarf2_per_objfile->n_type_unit_groups =
5275 htab_elements (dwarf2_per_objfile->type_unit_groups);
5276 dwarf2_per_objfile->all_type_unit_groups =
5277 obstack_alloc (&objfile->objfile_obstack,
5278 dwarf2_per_objfile->n_type_unit_groups
5279 * sizeof (struct type_unit_group *));
5280 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5281 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5282 add_type_unit_group_to_table, &iter);
5283 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5284 == dwarf2_per_objfile->n_type_unit_groups);
5285
5286 do_cleanups (cleanups);
5287
5288 if (dwarf2_read_debug)
5289 {
5290 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5291 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5292 dwarf2_per_objfile->n_type_units);
5293 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5294 tu_stats->nr_uniq_abbrev_tables);
5295 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5296 tu_stats->nr_symtabs);
5297 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5298 tu_stats->nr_symtab_sharers);
5299 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5300 tu_stats->nr_stmt_less_type_units);
5301 }
5302 }
5303
5304 /* Reader function for build_type_psymtabs. */
5305
5306 static void
5307 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5308 gdb_byte *info_ptr,
5309 struct die_info *type_unit_die,
5310 int has_children,
5311 void *data)
5312 {
5313 struct objfile *objfile = dwarf2_per_objfile->objfile;
5314 struct dwarf2_cu *cu = reader->cu;
5315 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5316 struct type_unit_group *tu_group;
5317 struct attribute *attr;
5318 struct partial_die_info *first_die;
5319 CORE_ADDR lowpc, highpc;
5320 struct partial_symtab *pst;
5321
5322 gdb_assert (data == NULL);
5323
5324 if (! has_children)
5325 return;
5326
5327 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5328 tu_group = get_type_unit_group (cu, attr);
5329
5330 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5331
5332 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5333 cu->list_in_scope = &file_symbols;
5334 pst = create_partial_symtab (per_cu, "");
5335 pst->anonymous = 1;
5336
5337 first_die = load_partial_dies (reader, info_ptr, 1);
5338
5339 lowpc = (CORE_ADDR) -1;
5340 highpc = (CORE_ADDR) 0;
5341 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5342
5343 pst->n_global_syms = objfile->global_psymbols.next -
5344 (objfile->global_psymbols.list + pst->globals_offset);
5345 pst->n_static_syms = objfile->static_psymbols.next -
5346 (objfile->static_psymbols.list + pst->statics_offset);
5347 sort_pst_symbols (pst);
5348 }
5349
5350 /* Traversal function for build_type_psymtabs. */
5351
5352 static int
5353 build_type_psymtab_dependencies (void **slot, void *info)
5354 {
5355 struct objfile *objfile = dwarf2_per_objfile->objfile;
5356 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5357 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5358 struct partial_symtab *pst = per_cu->v.psymtab;
5359 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5360 struct dwarf2_per_cu_data *iter;
5361 int i;
5362
5363 gdb_assert (len > 0);
5364
5365 pst->number_of_dependencies = len;
5366 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5367 len * sizeof (struct psymtab *));
5368 for (i = 0;
5369 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5370 ++i)
5371 {
5372 pst->dependencies[i] = iter->v.psymtab;
5373 iter->s.type_unit_group = tu_group;
5374 }
5375
5376 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5377
5378 return 1;
5379 }
5380
5381 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5382 Build partial symbol tables for the .debug_types comp-units. */
5383
5384 static void
5385 build_type_psymtabs (struct objfile *objfile)
5386 {
5387 if (! create_all_type_units (objfile))
5388 return;
5389
5390 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5391
5392 /* Now that all TUs have been processed we can fill in the dependencies. */
5393 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5394 build_type_psymtab_dependencies, NULL);
5395 }
5396
5397 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5398
5399 static void
5400 psymtabs_addrmap_cleanup (void *o)
5401 {
5402 struct objfile *objfile = o;
5403
5404 objfile->psymtabs_addrmap = NULL;
5405 }
5406
5407 /* Compute the 'user' field for each psymtab in OBJFILE. */
5408
5409 static void
5410 set_partial_user (struct objfile *objfile)
5411 {
5412 int i;
5413
5414 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5415 {
5416 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5417 struct partial_symtab *pst = per_cu->v.psymtab;
5418 int j;
5419
5420 if (pst == NULL)
5421 continue;
5422
5423 for (j = 0; j < pst->number_of_dependencies; ++j)
5424 {
5425 /* Set the 'user' field only if it is not already set. */
5426 if (pst->dependencies[j]->user == NULL)
5427 pst->dependencies[j]->user = pst;
5428 }
5429 }
5430 }
5431
5432 /* Build the partial symbol table by doing a quick pass through the
5433 .debug_info and .debug_abbrev sections. */
5434
5435 static void
5436 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5437 {
5438 struct cleanup *back_to, *addrmap_cleanup;
5439 struct obstack temp_obstack;
5440 int i;
5441
5442 if (dwarf2_read_debug)
5443 {
5444 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5445 objfile->name);
5446 }
5447
5448 dwarf2_per_objfile->reading_partial_symbols = 1;
5449
5450 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5451
5452 /* Any cached compilation units will be linked by the per-objfile
5453 read_in_chain. Make sure to free them when we're done. */
5454 back_to = make_cleanup (free_cached_comp_units, NULL);
5455
5456 build_type_psymtabs (objfile);
5457
5458 create_all_comp_units (objfile);
5459
5460 /* Create a temporary address map on a temporary obstack. We later
5461 copy this to the final obstack. */
5462 obstack_init (&temp_obstack);
5463 make_cleanup_obstack_free (&temp_obstack);
5464 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5465 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5466
5467 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5468 {
5469 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5470
5471 process_psymtab_comp_unit (per_cu, 0);
5472 }
5473
5474 set_partial_user (objfile);
5475
5476 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5477 &objfile->objfile_obstack);
5478 discard_cleanups (addrmap_cleanup);
5479
5480 do_cleanups (back_to);
5481
5482 if (dwarf2_read_debug)
5483 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5484 objfile->name);
5485 }
5486
5487 /* die_reader_func for load_partial_comp_unit. */
5488
5489 static void
5490 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5491 gdb_byte *info_ptr,
5492 struct die_info *comp_unit_die,
5493 int has_children,
5494 void *data)
5495 {
5496 struct dwarf2_cu *cu = reader->cu;
5497
5498 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5499
5500 /* Check if comp unit has_children.
5501 If so, read the rest of the partial symbols from this comp unit.
5502 If not, there's no more debug_info for this comp unit. */
5503 if (has_children)
5504 load_partial_dies (reader, info_ptr, 0);
5505 }
5506
5507 /* Load the partial DIEs for a secondary CU into memory.
5508 This is also used when rereading a primary CU with load_all_dies. */
5509
5510 static void
5511 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5512 {
5513 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5514 load_partial_comp_unit_reader, NULL);
5515 }
5516
5517 static void
5518 read_comp_units_from_section (struct objfile *objfile,
5519 struct dwarf2_section_info *section,
5520 unsigned int is_dwz,
5521 int *n_allocated,
5522 int *n_comp_units,
5523 struct dwarf2_per_cu_data ***all_comp_units)
5524 {
5525 gdb_byte *info_ptr;
5526 bfd *abfd = section->asection->owner;
5527
5528 dwarf2_read_section (objfile, section);
5529
5530 info_ptr = section->buffer;
5531
5532 while (info_ptr < section->buffer + section->size)
5533 {
5534 unsigned int length, initial_length_size;
5535 struct dwarf2_per_cu_data *this_cu;
5536 sect_offset offset;
5537
5538 offset.sect_off = info_ptr - section->buffer;
5539
5540 /* Read just enough information to find out where the next
5541 compilation unit is. */
5542 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5543
5544 /* Save the compilation unit for later lookup. */
5545 this_cu = obstack_alloc (&objfile->objfile_obstack,
5546 sizeof (struct dwarf2_per_cu_data));
5547 memset (this_cu, 0, sizeof (*this_cu));
5548 this_cu->offset = offset;
5549 this_cu->length = length + initial_length_size;
5550 this_cu->is_dwz = is_dwz;
5551 this_cu->objfile = objfile;
5552 this_cu->info_or_types_section = section;
5553
5554 if (*n_comp_units == *n_allocated)
5555 {
5556 *n_allocated *= 2;
5557 *all_comp_units = xrealloc (*all_comp_units,
5558 *n_allocated
5559 * sizeof (struct dwarf2_per_cu_data *));
5560 }
5561 (*all_comp_units)[*n_comp_units] = this_cu;
5562 ++*n_comp_units;
5563
5564 info_ptr = info_ptr + this_cu->length;
5565 }
5566 }
5567
5568 /* Create a list of all compilation units in OBJFILE.
5569 This is only done for -readnow and building partial symtabs. */
5570
5571 static void
5572 create_all_comp_units (struct objfile *objfile)
5573 {
5574 int n_allocated;
5575 int n_comp_units;
5576 struct dwarf2_per_cu_data **all_comp_units;
5577
5578 n_comp_units = 0;
5579 n_allocated = 10;
5580 all_comp_units = xmalloc (n_allocated
5581 * sizeof (struct dwarf2_per_cu_data *));
5582
5583 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5584 &n_allocated, &n_comp_units, &all_comp_units);
5585
5586 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5587 {
5588 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5589
5590 read_comp_units_from_section (objfile, &dwz->info, 1,
5591 &n_allocated, &n_comp_units,
5592 &all_comp_units);
5593 }
5594
5595 dwarf2_per_objfile->all_comp_units
5596 = obstack_alloc (&objfile->objfile_obstack,
5597 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5598 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5599 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5600 xfree (all_comp_units);
5601 dwarf2_per_objfile->n_comp_units = n_comp_units;
5602 }
5603
5604 /* Process all loaded DIEs for compilation unit CU, starting at
5605 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5606 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5607 DW_AT_ranges). If NEED_PC is set, then this function will set
5608 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5609 and record the covered ranges in the addrmap. */
5610
5611 static void
5612 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5613 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5614 {
5615 struct partial_die_info *pdi;
5616
5617 /* Now, march along the PDI's, descending into ones which have
5618 interesting children but skipping the children of the other ones,
5619 until we reach the end of the compilation unit. */
5620
5621 pdi = first_die;
5622
5623 while (pdi != NULL)
5624 {
5625 fixup_partial_die (pdi, cu);
5626
5627 /* Anonymous namespaces or modules have no name but have interesting
5628 children, so we need to look at them. Ditto for anonymous
5629 enums. */
5630
5631 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5632 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5633 || pdi->tag == DW_TAG_imported_unit)
5634 {
5635 switch (pdi->tag)
5636 {
5637 case DW_TAG_subprogram:
5638 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5639 break;
5640 case DW_TAG_constant:
5641 case DW_TAG_variable:
5642 case DW_TAG_typedef:
5643 case DW_TAG_union_type:
5644 if (!pdi->is_declaration)
5645 {
5646 add_partial_symbol (pdi, cu);
5647 }
5648 break;
5649 case DW_TAG_class_type:
5650 case DW_TAG_interface_type:
5651 case DW_TAG_structure_type:
5652 if (!pdi->is_declaration)
5653 {
5654 add_partial_symbol (pdi, cu);
5655 }
5656 break;
5657 case DW_TAG_enumeration_type:
5658 if (!pdi->is_declaration)
5659 add_partial_enumeration (pdi, cu);
5660 break;
5661 case DW_TAG_base_type:
5662 case DW_TAG_subrange_type:
5663 /* File scope base type definitions are added to the partial
5664 symbol table. */
5665 add_partial_symbol (pdi, cu);
5666 break;
5667 case DW_TAG_namespace:
5668 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5669 break;
5670 case DW_TAG_module:
5671 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5672 break;
5673 case DW_TAG_imported_unit:
5674 {
5675 struct dwarf2_per_cu_data *per_cu;
5676
5677 /* For now we don't handle imported units in type units. */
5678 if (cu->per_cu->is_debug_types)
5679 {
5680 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5681 " supported in type units [in module %s]"),
5682 cu->objfile->name);
5683 }
5684
5685 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5686 pdi->is_dwz,
5687 cu->objfile);
5688
5689 /* Go read the partial unit, if needed. */
5690 if (per_cu->v.psymtab == NULL)
5691 process_psymtab_comp_unit (per_cu, 1);
5692
5693 VEC_safe_push (dwarf2_per_cu_ptr,
5694 cu->per_cu->s.imported_symtabs, per_cu);
5695 }
5696 break;
5697 default:
5698 break;
5699 }
5700 }
5701
5702 /* If the die has a sibling, skip to the sibling. */
5703
5704 pdi = pdi->die_sibling;
5705 }
5706 }
5707
5708 /* Functions used to compute the fully scoped name of a partial DIE.
5709
5710 Normally, this is simple. For C++, the parent DIE's fully scoped
5711 name is concatenated with "::" and the partial DIE's name. For
5712 Java, the same thing occurs except that "." is used instead of "::".
5713 Enumerators are an exception; they use the scope of their parent
5714 enumeration type, i.e. the name of the enumeration type is not
5715 prepended to the enumerator.
5716
5717 There are two complexities. One is DW_AT_specification; in this
5718 case "parent" means the parent of the target of the specification,
5719 instead of the direct parent of the DIE. The other is compilers
5720 which do not emit DW_TAG_namespace; in this case we try to guess
5721 the fully qualified name of structure types from their members'
5722 linkage names. This must be done using the DIE's children rather
5723 than the children of any DW_AT_specification target. We only need
5724 to do this for structures at the top level, i.e. if the target of
5725 any DW_AT_specification (if any; otherwise the DIE itself) does not
5726 have a parent. */
5727
5728 /* Compute the scope prefix associated with PDI's parent, in
5729 compilation unit CU. The result will be allocated on CU's
5730 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5731 field. NULL is returned if no prefix is necessary. */
5732 static char *
5733 partial_die_parent_scope (struct partial_die_info *pdi,
5734 struct dwarf2_cu *cu)
5735 {
5736 char *grandparent_scope;
5737 struct partial_die_info *parent, *real_pdi;
5738
5739 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5740 then this means the parent of the specification DIE. */
5741
5742 real_pdi = pdi;
5743 while (real_pdi->has_specification)
5744 real_pdi = find_partial_die (real_pdi->spec_offset,
5745 real_pdi->spec_is_dwz, cu);
5746
5747 parent = real_pdi->die_parent;
5748 if (parent == NULL)
5749 return NULL;
5750
5751 if (parent->scope_set)
5752 return parent->scope;
5753
5754 fixup_partial_die (parent, cu);
5755
5756 grandparent_scope = partial_die_parent_scope (parent, cu);
5757
5758 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5759 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5760 Work around this problem here. */
5761 if (cu->language == language_cplus
5762 && parent->tag == DW_TAG_namespace
5763 && strcmp (parent->name, "::") == 0
5764 && grandparent_scope == NULL)
5765 {
5766 parent->scope = NULL;
5767 parent->scope_set = 1;
5768 return NULL;
5769 }
5770
5771 if (pdi->tag == DW_TAG_enumerator)
5772 /* Enumerators should not get the name of the enumeration as a prefix. */
5773 parent->scope = grandparent_scope;
5774 else if (parent->tag == DW_TAG_namespace
5775 || parent->tag == DW_TAG_module
5776 || parent->tag == DW_TAG_structure_type
5777 || parent->tag == DW_TAG_class_type
5778 || parent->tag == DW_TAG_interface_type
5779 || parent->tag == DW_TAG_union_type
5780 || parent->tag == DW_TAG_enumeration_type)
5781 {
5782 if (grandparent_scope == NULL)
5783 parent->scope = parent->name;
5784 else
5785 parent->scope = typename_concat (&cu->comp_unit_obstack,
5786 grandparent_scope,
5787 parent->name, 0, cu);
5788 }
5789 else
5790 {
5791 /* FIXME drow/2004-04-01: What should we be doing with
5792 function-local names? For partial symbols, we should probably be
5793 ignoring them. */
5794 complaint (&symfile_complaints,
5795 _("unhandled containing DIE tag %d for DIE at %d"),
5796 parent->tag, pdi->offset.sect_off);
5797 parent->scope = grandparent_scope;
5798 }
5799
5800 parent->scope_set = 1;
5801 return parent->scope;
5802 }
5803
5804 /* Return the fully scoped name associated with PDI, from compilation unit
5805 CU. The result will be allocated with malloc. */
5806
5807 static char *
5808 partial_die_full_name (struct partial_die_info *pdi,
5809 struct dwarf2_cu *cu)
5810 {
5811 char *parent_scope;
5812
5813 /* If this is a template instantiation, we can not work out the
5814 template arguments from partial DIEs. So, unfortunately, we have
5815 to go through the full DIEs. At least any work we do building
5816 types here will be reused if full symbols are loaded later. */
5817 if (pdi->has_template_arguments)
5818 {
5819 fixup_partial_die (pdi, cu);
5820
5821 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5822 {
5823 struct die_info *die;
5824 struct attribute attr;
5825 struct dwarf2_cu *ref_cu = cu;
5826
5827 /* DW_FORM_ref_addr is using section offset. */
5828 attr.name = 0;
5829 attr.form = DW_FORM_ref_addr;
5830 attr.u.unsnd = pdi->offset.sect_off;
5831 die = follow_die_ref (NULL, &attr, &ref_cu);
5832
5833 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5834 }
5835 }
5836
5837 parent_scope = partial_die_parent_scope (pdi, cu);
5838 if (parent_scope == NULL)
5839 return NULL;
5840 else
5841 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5842 }
5843
5844 static void
5845 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5846 {
5847 struct objfile *objfile = cu->objfile;
5848 CORE_ADDR addr = 0;
5849 char *actual_name = NULL;
5850 CORE_ADDR baseaddr;
5851 int built_actual_name = 0;
5852
5853 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5854
5855 actual_name = partial_die_full_name (pdi, cu);
5856 if (actual_name)
5857 built_actual_name = 1;
5858
5859 if (actual_name == NULL)
5860 actual_name = pdi->name;
5861
5862 switch (pdi->tag)
5863 {
5864 case DW_TAG_subprogram:
5865 if (pdi->is_external || cu->language == language_ada)
5866 {
5867 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5868 of the global scope. But in Ada, we want to be able to access
5869 nested procedures globally. So all Ada subprograms are stored
5870 in the global scope. */
5871 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5872 mst_text, objfile); */
5873 add_psymbol_to_list (actual_name, strlen (actual_name),
5874 built_actual_name,
5875 VAR_DOMAIN, LOC_BLOCK,
5876 &objfile->global_psymbols,
5877 0, pdi->lowpc + baseaddr,
5878 cu->language, objfile);
5879 }
5880 else
5881 {
5882 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5883 mst_file_text, objfile); */
5884 add_psymbol_to_list (actual_name, strlen (actual_name),
5885 built_actual_name,
5886 VAR_DOMAIN, LOC_BLOCK,
5887 &objfile->static_psymbols,
5888 0, pdi->lowpc + baseaddr,
5889 cu->language, objfile);
5890 }
5891 break;
5892 case DW_TAG_constant:
5893 {
5894 struct psymbol_allocation_list *list;
5895
5896 if (pdi->is_external)
5897 list = &objfile->global_psymbols;
5898 else
5899 list = &objfile->static_psymbols;
5900 add_psymbol_to_list (actual_name, strlen (actual_name),
5901 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5902 list, 0, 0, cu->language, objfile);
5903 }
5904 break;
5905 case DW_TAG_variable:
5906 if (pdi->d.locdesc)
5907 addr = decode_locdesc (pdi->d.locdesc, cu);
5908
5909 if (pdi->d.locdesc
5910 && addr == 0
5911 && !dwarf2_per_objfile->has_section_at_zero)
5912 {
5913 /* A global or static variable may also have been stripped
5914 out by the linker if unused, in which case its address
5915 will be nullified; do not add such variables into partial
5916 symbol table then. */
5917 }
5918 else if (pdi->is_external)
5919 {
5920 /* Global Variable.
5921 Don't enter into the minimal symbol tables as there is
5922 a minimal symbol table entry from the ELF symbols already.
5923 Enter into partial symbol table if it has a location
5924 descriptor or a type.
5925 If the location descriptor is missing, new_symbol will create
5926 a LOC_UNRESOLVED symbol, the address of the variable will then
5927 be determined from the minimal symbol table whenever the variable
5928 is referenced.
5929 The address for the partial symbol table entry is not
5930 used by GDB, but it comes in handy for debugging partial symbol
5931 table building. */
5932
5933 if (pdi->d.locdesc || pdi->has_type)
5934 add_psymbol_to_list (actual_name, strlen (actual_name),
5935 built_actual_name,
5936 VAR_DOMAIN, LOC_STATIC,
5937 &objfile->global_psymbols,
5938 0, addr + baseaddr,
5939 cu->language, objfile);
5940 }
5941 else
5942 {
5943 /* Static Variable. Skip symbols without location descriptors. */
5944 if (pdi->d.locdesc == NULL)
5945 {
5946 if (built_actual_name)
5947 xfree (actual_name);
5948 return;
5949 }
5950 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5951 mst_file_data, objfile); */
5952 add_psymbol_to_list (actual_name, strlen (actual_name),
5953 built_actual_name,
5954 VAR_DOMAIN, LOC_STATIC,
5955 &objfile->static_psymbols,
5956 0, addr + baseaddr,
5957 cu->language, objfile);
5958 }
5959 break;
5960 case DW_TAG_typedef:
5961 case DW_TAG_base_type:
5962 case DW_TAG_subrange_type:
5963 add_psymbol_to_list (actual_name, strlen (actual_name),
5964 built_actual_name,
5965 VAR_DOMAIN, LOC_TYPEDEF,
5966 &objfile->static_psymbols,
5967 0, (CORE_ADDR) 0, cu->language, objfile);
5968 break;
5969 case DW_TAG_namespace:
5970 add_psymbol_to_list (actual_name, strlen (actual_name),
5971 built_actual_name,
5972 VAR_DOMAIN, LOC_TYPEDEF,
5973 &objfile->global_psymbols,
5974 0, (CORE_ADDR) 0, cu->language, objfile);
5975 break;
5976 case DW_TAG_class_type:
5977 case DW_TAG_interface_type:
5978 case DW_TAG_structure_type:
5979 case DW_TAG_union_type:
5980 case DW_TAG_enumeration_type:
5981 /* Skip external references. The DWARF standard says in the section
5982 about "Structure, Union, and Class Type Entries": "An incomplete
5983 structure, union or class type is represented by a structure,
5984 union or class entry that does not have a byte size attribute
5985 and that has a DW_AT_declaration attribute." */
5986 if (!pdi->has_byte_size && pdi->is_declaration)
5987 {
5988 if (built_actual_name)
5989 xfree (actual_name);
5990 return;
5991 }
5992
5993 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5994 static vs. global. */
5995 add_psymbol_to_list (actual_name, strlen (actual_name),
5996 built_actual_name,
5997 STRUCT_DOMAIN, LOC_TYPEDEF,
5998 (cu->language == language_cplus
5999 || cu->language == language_java)
6000 ? &objfile->global_psymbols
6001 : &objfile->static_psymbols,
6002 0, (CORE_ADDR) 0, cu->language, objfile);
6003
6004 break;
6005 case DW_TAG_enumerator:
6006 add_psymbol_to_list (actual_name, strlen (actual_name),
6007 built_actual_name,
6008 VAR_DOMAIN, LOC_CONST,
6009 (cu->language == language_cplus
6010 || cu->language == language_java)
6011 ? &objfile->global_psymbols
6012 : &objfile->static_psymbols,
6013 0, (CORE_ADDR) 0, cu->language, objfile);
6014 break;
6015 default:
6016 break;
6017 }
6018
6019 if (built_actual_name)
6020 xfree (actual_name);
6021 }
6022
6023 /* Read a partial die corresponding to a namespace; also, add a symbol
6024 corresponding to that namespace to the symbol table. NAMESPACE is
6025 the name of the enclosing namespace. */
6026
6027 static void
6028 add_partial_namespace (struct partial_die_info *pdi,
6029 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6030 int need_pc, struct dwarf2_cu *cu)
6031 {
6032 /* Add a symbol for the namespace. */
6033
6034 add_partial_symbol (pdi, cu);
6035
6036 /* Now scan partial symbols in that namespace. */
6037
6038 if (pdi->has_children)
6039 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6040 }
6041
6042 /* Read a partial die corresponding to a Fortran module. */
6043
6044 static void
6045 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6046 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6047 {
6048 /* Now scan partial symbols in that module. */
6049
6050 if (pdi->has_children)
6051 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6052 }
6053
6054 /* Read a partial die corresponding to a subprogram and create a partial
6055 symbol for that subprogram. When the CU language allows it, this
6056 routine also defines a partial symbol for each nested subprogram
6057 that this subprogram contains.
6058
6059 DIE my also be a lexical block, in which case we simply search
6060 recursively for suprograms defined inside that lexical block.
6061 Again, this is only performed when the CU language allows this
6062 type of definitions. */
6063
6064 static void
6065 add_partial_subprogram (struct partial_die_info *pdi,
6066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6067 int need_pc, struct dwarf2_cu *cu)
6068 {
6069 if (pdi->tag == DW_TAG_subprogram)
6070 {
6071 if (pdi->has_pc_info)
6072 {
6073 if (pdi->lowpc < *lowpc)
6074 *lowpc = pdi->lowpc;
6075 if (pdi->highpc > *highpc)
6076 *highpc = pdi->highpc;
6077 if (need_pc)
6078 {
6079 CORE_ADDR baseaddr;
6080 struct objfile *objfile = cu->objfile;
6081
6082 baseaddr = ANOFFSET (objfile->section_offsets,
6083 SECT_OFF_TEXT (objfile));
6084 addrmap_set_empty (objfile->psymtabs_addrmap,
6085 pdi->lowpc + baseaddr,
6086 pdi->highpc - 1 + baseaddr,
6087 cu->per_cu->v.psymtab);
6088 }
6089 }
6090
6091 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6092 {
6093 if (!pdi->is_declaration)
6094 /* Ignore subprogram DIEs that do not have a name, they are
6095 illegal. Do not emit a complaint at this point, we will
6096 do so when we convert this psymtab into a symtab. */
6097 if (pdi->name)
6098 add_partial_symbol (pdi, cu);
6099 }
6100 }
6101
6102 if (! pdi->has_children)
6103 return;
6104
6105 if (cu->language == language_ada)
6106 {
6107 pdi = pdi->die_child;
6108 while (pdi != NULL)
6109 {
6110 fixup_partial_die (pdi, cu);
6111 if (pdi->tag == DW_TAG_subprogram
6112 || pdi->tag == DW_TAG_lexical_block)
6113 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6114 pdi = pdi->die_sibling;
6115 }
6116 }
6117 }
6118
6119 /* Read a partial die corresponding to an enumeration type. */
6120
6121 static void
6122 add_partial_enumeration (struct partial_die_info *enum_pdi,
6123 struct dwarf2_cu *cu)
6124 {
6125 struct partial_die_info *pdi;
6126
6127 if (enum_pdi->name != NULL)
6128 add_partial_symbol (enum_pdi, cu);
6129
6130 pdi = enum_pdi->die_child;
6131 while (pdi)
6132 {
6133 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6134 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6135 else
6136 add_partial_symbol (pdi, cu);
6137 pdi = pdi->die_sibling;
6138 }
6139 }
6140
6141 /* Return the initial uleb128 in the die at INFO_PTR. */
6142
6143 static unsigned int
6144 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6145 {
6146 unsigned int bytes_read;
6147
6148 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6149 }
6150
6151 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6152 Return the corresponding abbrev, or NULL if the number is zero (indicating
6153 an empty DIE). In either case *BYTES_READ will be set to the length of
6154 the initial number. */
6155
6156 static struct abbrev_info *
6157 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6158 struct dwarf2_cu *cu)
6159 {
6160 bfd *abfd = cu->objfile->obfd;
6161 unsigned int abbrev_number;
6162 struct abbrev_info *abbrev;
6163
6164 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6165
6166 if (abbrev_number == 0)
6167 return NULL;
6168
6169 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6170 if (!abbrev)
6171 {
6172 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6173 abbrev_number, bfd_get_filename (abfd));
6174 }
6175
6176 return abbrev;
6177 }
6178
6179 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6180 Returns a pointer to the end of a series of DIEs, terminated by an empty
6181 DIE. Any children of the skipped DIEs will also be skipped. */
6182
6183 static gdb_byte *
6184 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6185 {
6186 struct dwarf2_cu *cu = reader->cu;
6187 struct abbrev_info *abbrev;
6188 unsigned int bytes_read;
6189
6190 while (1)
6191 {
6192 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6193 if (abbrev == NULL)
6194 return info_ptr + bytes_read;
6195 else
6196 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6197 }
6198 }
6199
6200 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6201 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6202 abbrev corresponding to that skipped uleb128 should be passed in
6203 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6204 children. */
6205
6206 static gdb_byte *
6207 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6208 struct abbrev_info *abbrev)
6209 {
6210 unsigned int bytes_read;
6211 struct attribute attr;
6212 bfd *abfd = reader->abfd;
6213 struct dwarf2_cu *cu = reader->cu;
6214 gdb_byte *buffer = reader->buffer;
6215 const gdb_byte *buffer_end = reader->buffer_end;
6216 gdb_byte *start_info_ptr = info_ptr;
6217 unsigned int form, i;
6218
6219 for (i = 0; i < abbrev->num_attrs; i++)
6220 {
6221 /* The only abbrev we care about is DW_AT_sibling. */
6222 if (abbrev->attrs[i].name == DW_AT_sibling)
6223 {
6224 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6225 if (attr.form == DW_FORM_ref_addr)
6226 complaint (&symfile_complaints,
6227 _("ignoring absolute DW_AT_sibling"));
6228 else
6229 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6230 }
6231
6232 /* If it isn't DW_AT_sibling, skip this attribute. */
6233 form = abbrev->attrs[i].form;
6234 skip_attribute:
6235 switch (form)
6236 {
6237 case DW_FORM_ref_addr:
6238 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6239 and later it is offset sized. */
6240 if (cu->header.version == 2)
6241 info_ptr += cu->header.addr_size;
6242 else
6243 info_ptr += cu->header.offset_size;
6244 break;
6245 case DW_FORM_GNU_ref_alt:
6246 info_ptr += cu->header.offset_size;
6247 break;
6248 case DW_FORM_addr:
6249 info_ptr += cu->header.addr_size;
6250 break;
6251 case DW_FORM_data1:
6252 case DW_FORM_ref1:
6253 case DW_FORM_flag:
6254 info_ptr += 1;
6255 break;
6256 case DW_FORM_flag_present:
6257 break;
6258 case DW_FORM_data2:
6259 case DW_FORM_ref2:
6260 info_ptr += 2;
6261 break;
6262 case DW_FORM_data4:
6263 case DW_FORM_ref4:
6264 info_ptr += 4;
6265 break;
6266 case DW_FORM_data8:
6267 case DW_FORM_ref8:
6268 case DW_FORM_ref_sig8:
6269 info_ptr += 8;
6270 break;
6271 case DW_FORM_string:
6272 read_direct_string (abfd, info_ptr, &bytes_read);
6273 info_ptr += bytes_read;
6274 break;
6275 case DW_FORM_sec_offset:
6276 case DW_FORM_strp:
6277 case DW_FORM_GNU_strp_alt:
6278 info_ptr += cu->header.offset_size;
6279 break;
6280 case DW_FORM_exprloc:
6281 case DW_FORM_block:
6282 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6283 info_ptr += bytes_read;
6284 break;
6285 case DW_FORM_block1:
6286 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6287 break;
6288 case DW_FORM_block2:
6289 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6290 break;
6291 case DW_FORM_block4:
6292 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6293 break;
6294 case DW_FORM_sdata:
6295 case DW_FORM_udata:
6296 case DW_FORM_ref_udata:
6297 case DW_FORM_GNU_addr_index:
6298 case DW_FORM_GNU_str_index:
6299 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6300 break;
6301 case DW_FORM_indirect:
6302 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6303 info_ptr += bytes_read;
6304 /* We need to continue parsing from here, so just go back to
6305 the top. */
6306 goto skip_attribute;
6307
6308 default:
6309 error (_("Dwarf Error: Cannot handle %s "
6310 "in DWARF reader [in module %s]"),
6311 dwarf_form_name (form),
6312 bfd_get_filename (abfd));
6313 }
6314 }
6315
6316 if (abbrev->has_children)
6317 return skip_children (reader, info_ptr);
6318 else
6319 return info_ptr;
6320 }
6321
6322 /* Locate ORIG_PDI's sibling.
6323 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6324
6325 static gdb_byte *
6326 locate_pdi_sibling (const struct die_reader_specs *reader,
6327 struct partial_die_info *orig_pdi,
6328 gdb_byte *info_ptr)
6329 {
6330 /* Do we know the sibling already? */
6331
6332 if (orig_pdi->sibling)
6333 return orig_pdi->sibling;
6334
6335 /* Are there any children to deal with? */
6336
6337 if (!orig_pdi->has_children)
6338 return info_ptr;
6339
6340 /* Skip the children the long way. */
6341
6342 return skip_children (reader, info_ptr);
6343 }
6344
6345 /* Expand this partial symbol table into a full symbol table. */
6346
6347 static void
6348 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6349 {
6350 if (pst != NULL)
6351 {
6352 if (pst->readin)
6353 {
6354 warning (_("bug: psymtab for %s is already read in."),
6355 pst->filename);
6356 }
6357 else
6358 {
6359 if (info_verbose)
6360 {
6361 printf_filtered (_("Reading in symbols for %s..."),
6362 pst->filename);
6363 gdb_flush (gdb_stdout);
6364 }
6365
6366 /* Restore our global data. */
6367 dwarf2_per_objfile = objfile_data (pst->objfile,
6368 dwarf2_objfile_data_key);
6369
6370 /* If this psymtab is constructed from a debug-only objfile, the
6371 has_section_at_zero flag will not necessarily be correct. We
6372 can get the correct value for this flag by looking at the data
6373 associated with the (presumably stripped) associated objfile. */
6374 if (pst->objfile->separate_debug_objfile_backlink)
6375 {
6376 struct dwarf2_per_objfile *dpo_backlink
6377 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6378 dwarf2_objfile_data_key);
6379
6380 dwarf2_per_objfile->has_section_at_zero
6381 = dpo_backlink->has_section_at_zero;
6382 }
6383
6384 dwarf2_per_objfile->reading_partial_symbols = 0;
6385
6386 psymtab_to_symtab_1 (pst);
6387
6388 /* Finish up the debug error message. */
6389 if (info_verbose)
6390 printf_filtered (_("done.\n"));
6391 }
6392 }
6393
6394 process_cu_includes ();
6395 }
6396 \f
6397 /* Reading in full CUs. */
6398
6399 /* Add PER_CU to the queue. */
6400
6401 static void
6402 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6403 enum language pretend_language)
6404 {
6405 struct dwarf2_queue_item *item;
6406
6407 per_cu->queued = 1;
6408 item = xmalloc (sizeof (*item));
6409 item->per_cu = per_cu;
6410 item->pretend_language = pretend_language;
6411 item->next = NULL;
6412
6413 if (dwarf2_queue == NULL)
6414 dwarf2_queue = item;
6415 else
6416 dwarf2_queue_tail->next = item;
6417
6418 dwarf2_queue_tail = item;
6419 }
6420
6421 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6422 unit and add it to our queue.
6423 The result is non-zero if PER_CU was queued, otherwise the result is zero
6424 meaning either PER_CU is already queued or it is already loaded. */
6425
6426 static int
6427 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6428 struct dwarf2_per_cu_data *per_cu,
6429 enum language pretend_language)
6430 {
6431 /* We may arrive here during partial symbol reading, if we need full
6432 DIEs to process an unusual case (e.g. template arguments). Do
6433 not queue PER_CU, just tell our caller to load its DIEs. */
6434 if (dwarf2_per_objfile->reading_partial_symbols)
6435 {
6436 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6437 return 1;
6438 return 0;
6439 }
6440
6441 /* Mark the dependence relation so that we don't flush PER_CU
6442 too early. */
6443 dwarf2_add_dependence (this_cu, per_cu);
6444
6445 /* If it's already on the queue, we have nothing to do. */
6446 if (per_cu->queued)
6447 return 0;
6448
6449 /* If the compilation unit is already loaded, just mark it as
6450 used. */
6451 if (per_cu->cu != NULL)
6452 {
6453 per_cu->cu->last_used = 0;
6454 return 0;
6455 }
6456
6457 /* Add it to the queue. */
6458 queue_comp_unit (per_cu, pretend_language);
6459
6460 return 1;
6461 }
6462
6463 /* Process the queue. */
6464
6465 static void
6466 process_queue (void)
6467 {
6468 struct dwarf2_queue_item *item, *next_item;
6469
6470 if (dwarf2_read_debug)
6471 {
6472 fprintf_unfiltered (gdb_stdlog,
6473 "Expanding one or more symtabs of objfile %s ...\n",
6474 dwarf2_per_objfile->objfile->name);
6475 }
6476
6477 /* The queue starts out with one item, but following a DIE reference
6478 may load a new CU, adding it to the end of the queue. */
6479 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6480 {
6481 if (dwarf2_per_objfile->using_index
6482 ? !item->per_cu->v.quick->symtab
6483 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6484 {
6485 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6486
6487 if (dwarf2_read_debug)
6488 {
6489 fprintf_unfiltered (gdb_stdlog,
6490 "Expanding symtab of %s at offset 0x%x\n",
6491 per_cu->is_debug_types ? "TU" : "CU",
6492 per_cu->offset.sect_off);
6493 }
6494
6495 if (per_cu->is_debug_types)
6496 process_full_type_unit (per_cu, item->pretend_language);
6497 else
6498 process_full_comp_unit (per_cu, item->pretend_language);
6499
6500 if (dwarf2_read_debug)
6501 {
6502 fprintf_unfiltered (gdb_stdlog,
6503 "Done expanding %s at offset 0x%x\n",
6504 per_cu->is_debug_types ? "TU" : "CU",
6505 per_cu->offset.sect_off);
6506 }
6507 }
6508
6509 item->per_cu->queued = 0;
6510 next_item = item->next;
6511 xfree (item);
6512 }
6513
6514 dwarf2_queue_tail = NULL;
6515
6516 if (dwarf2_read_debug)
6517 {
6518 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6519 dwarf2_per_objfile->objfile->name);
6520 }
6521 }
6522
6523 /* Free all allocated queue entries. This function only releases anything if
6524 an error was thrown; if the queue was processed then it would have been
6525 freed as we went along. */
6526
6527 static void
6528 dwarf2_release_queue (void *dummy)
6529 {
6530 struct dwarf2_queue_item *item, *last;
6531
6532 item = dwarf2_queue;
6533 while (item)
6534 {
6535 /* Anything still marked queued is likely to be in an
6536 inconsistent state, so discard it. */
6537 if (item->per_cu->queued)
6538 {
6539 if (item->per_cu->cu != NULL)
6540 free_one_cached_comp_unit (item->per_cu);
6541 item->per_cu->queued = 0;
6542 }
6543
6544 last = item;
6545 item = item->next;
6546 xfree (last);
6547 }
6548
6549 dwarf2_queue = dwarf2_queue_tail = NULL;
6550 }
6551
6552 /* Read in full symbols for PST, and anything it depends on. */
6553
6554 static void
6555 psymtab_to_symtab_1 (struct partial_symtab *pst)
6556 {
6557 struct dwarf2_per_cu_data *per_cu;
6558 int i;
6559
6560 if (pst->readin)
6561 return;
6562
6563 for (i = 0; i < pst->number_of_dependencies; i++)
6564 if (!pst->dependencies[i]->readin
6565 && pst->dependencies[i]->user == NULL)
6566 {
6567 /* Inform about additional files that need to be read in. */
6568 if (info_verbose)
6569 {
6570 /* FIXME: i18n: Need to make this a single string. */
6571 fputs_filtered (" ", gdb_stdout);
6572 wrap_here ("");
6573 fputs_filtered ("and ", gdb_stdout);
6574 wrap_here ("");
6575 printf_filtered ("%s...", pst->dependencies[i]->filename);
6576 wrap_here (""); /* Flush output. */
6577 gdb_flush (gdb_stdout);
6578 }
6579 psymtab_to_symtab_1 (pst->dependencies[i]);
6580 }
6581
6582 per_cu = pst->read_symtab_private;
6583
6584 if (per_cu == NULL)
6585 {
6586 /* It's an include file, no symbols to read for it.
6587 Everything is in the parent symtab. */
6588 pst->readin = 1;
6589 return;
6590 }
6591
6592 dw2_do_instantiate_symtab (per_cu);
6593 }
6594
6595 /* Trivial hash function for die_info: the hash value of a DIE
6596 is its offset in .debug_info for this objfile. */
6597
6598 static hashval_t
6599 die_hash (const void *item)
6600 {
6601 const struct die_info *die = item;
6602
6603 return die->offset.sect_off;
6604 }
6605
6606 /* Trivial comparison function for die_info structures: two DIEs
6607 are equal if they have the same offset. */
6608
6609 static int
6610 die_eq (const void *item_lhs, const void *item_rhs)
6611 {
6612 const struct die_info *die_lhs = item_lhs;
6613 const struct die_info *die_rhs = item_rhs;
6614
6615 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6616 }
6617
6618 /* die_reader_func for load_full_comp_unit.
6619 This is identical to read_signatured_type_reader,
6620 but is kept separate for now. */
6621
6622 static void
6623 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6624 gdb_byte *info_ptr,
6625 struct die_info *comp_unit_die,
6626 int has_children,
6627 void *data)
6628 {
6629 struct dwarf2_cu *cu = reader->cu;
6630 enum language *language_ptr = data;
6631
6632 gdb_assert (cu->die_hash == NULL);
6633 cu->die_hash =
6634 htab_create_alloc_ex (cu->header.length / 12,
6635 die_hash,
6636 die_eq,
6637 NULL,
6638 &cu->comp_unit_obstack,
6639 hashtab_obstack_allocate,
6640 dummy_obstack_deallocate);
6641
6642 if (has_children)
6643 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6644 &info_ptr, comp_unit_die);
6645 cu->dies = comp_unit_die;
6646 /* comp_unit_die is not stored in die_hash, no need. */
6647
6648 /* We try not to read any attributes in this function, because not
6649 all CUs needed for references have been loaded yet, and symbol
6650 table processing isn't initialized. But we have to set the CU language,
6651 or we won't be able to build types correctly.
6652 Similarly, if we do not read the producer, we can not apply
6653 producer-specific interpretation. */
6654 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6655 }
6656
6657 /* Load the DIEs associated with PER_CU into memory. */
6658
6659 static void
6660 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6661 enum language pretend_language)
6662 {
6663 gdb_assert (! this_cu->is_debug_types);
6664
6665 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6666 load_full_comp_unit_reader, &pretend_language);
6667 }
6668
6669 /* Add a DIE to the delayed physname list. */
6670
6671 static void
6672 add_to_method_list (struct type *type, int fnfield_index, int index,
6673 const char *name, struct die_info *die,
6674 struct dwarf2_cu *cu)
6675 {
6676 struct delayed_method_info mi;
6677 mi.type = type;
6678 mi.fnfield_index = fnfield_index;
6679 mi.index = index;
6680 mi.name = name;
6681 mi.die = die;
6682 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6683 }
6684
6685 /* A cleanup for freeing the delayed method list. */
6686
6687 static void
6688 free_delayed_list (void *ptr)
6689 {
6690 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6691 if (cu->method_list != NULL)
6692 {
6693 VEC_free (delayed_method_info, cu->method_list);
6694 cu->method_list = NULL;
6695 }
6696 }
6697
6698 /* Compute the physnames of any methods on the CU's method list.
6699
6700 The computation of method physnames is delayed in order to avoid the
6701 (bad) condition that one of the method's formal parameters is of an as yet
6702 incomplete type. */
6703
6704 static void
6705 compute_delayed_physnames (struct dwarf2_cu *cu)
6706 {
6707 int i;
6708 struct delayed_method_info *mi;
6709 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6710 {
6711 const char *physname;
6712 struct fn_fieldlist *fn_flp
6713 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6714 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6715 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6716 }
6717 }
6718
6719 /* Go objects should be embedded in a DW_TAG_module DIE,
6720 and it's not clear if/how imported objects will appear.
6721 To keep Go support simple until that's worked out,
6722 go back through what we've read and create something usable.
6723 We could do this while processing each DIE, and feels kinda cleaner,
6724 but that way is more invasive.
6725 This is to, for example, allow the user to type "p var" or "b main"
6726 without having to specify the package name, and allow lookups
6727 of module.object to work in contexts that use the expression
6728 parser. */
6729
6730 static void
6731 fixup_go_packaging (struct dwarf2_cu *cu)
6732 {
6733 char *package_name = NULL;
6734 struct pending *list;
6735 int i;
6736
6737 for (list = global_symbols; list != NULL; list = list->next)
6738 {
6739 for (i = 0; i < list->nsyms; ++i)
6740 {
6741 struct symbol *sym = list->symbol[i];
6742
6743 if (SYMBOL_LANGUAGE (sym) == language_go
6744 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6745 {
6746 char *this_package_name = go_symbol_package_name (sym);
6747
6748 if (this_package_name == NULL)
6749 continue;
6750 if (package_name == NULL)
6751 package_name = this_package_name;
6752 else
6753 {
6754 if (strcmp (package_name, this_package_name) != 0)
6755 complaint (&symfile_complaints,
6756 _("Symtab %s has objects from two different Go packages: %s and %s"),
6757 (sym->symtab && sym->symtab->filename
6758 ? sym->symtab->filename
6759 : cu->objfile->name),
6760 this_package_name, package_name);
6761 xfree (this_package_name);
6762 }
6763 }
6764 }
6765 }
6766
6767 if (package_name != NULL)
6768 {
6769 struct objfile *objfile = cu->objfile;
6770 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6771 package_name, objfile);
6772 struct symbol *sym;
6773
6774 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6775
6776 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6777 SYMBOL_SET_LANGUAGE (sym, language_go);
6778 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6779 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6780 e.g., "main" finds the "main" module and not C's main(). */
6781 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6782 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6783 SYMBOL_TYPE (sym) = type;
6784
6785 add_symbol_to_list (sym, &global_symbols);
6786
6787 xfree (package_name);
6788 }
6789 }
6790
6791 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6792
6793 /* Return the symtab for PER_CU. This works properly regardless of
6794 whether we're using the index or psymtabs. */
6795
6796 static struct symtab *
6797 get_symtab (struct dwarf2_per_cu_data *per_cu)
6798 {
6799 return (dwarf2_per_objfile->using_index
6800 ? per_cu->v.quick->symtab
6801 : per_cu->v.psymtab->symtab);
6802 }
6803
6804 /* A helper function for computing the list of all symbol tables
6805 included by PER_CU. */
6806
6807 static void
6808 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6809 htab_t all_children,
6810 struct dwarf2_per_cu_data *per_cu)
6811 {
6812 void **slot;
6813 int ix;
6814 struct dwarf2_per_cu_data *iter;
6815
6816 slot = htab_find_slot (all_children, per_cu, INSERT);
6817 if (*slot != NULL)
6818 {
6819 /* This inclusion and its children have been processed. */
6820 return;
6821 }
6822
6823 *slot = per_cu;
6824 /* Only add a CU if it has a symbol table. */
6825 if (get_symtab (per_cu) != NULL)
6826 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6827
6828 for (ix = 0;
6829 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6830 ++ix)
6831 recursively_compute_inclusions (result, all_children, iter);
6832 }
6833
6834 /* Compute the symtab 'includes' fields for the symtab related to
6835 PER_CU. */
6836
6837 static void
6838 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6839 {
6840 gdb_assert (! per_cu->is_debug_types);
6841
6842 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6843 {
6844 int ix, len;
6845 struct dwarf2_per_cu_data *iter;
6846 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6847 htab_t all_children;
6848 struct symtab *symtab = get_symtab (per_cu);
6849
6850 /* If we don't have a symtab, we can just skip this case. */
6851 if (symtab == NULL)
6852 return;
6853
6854 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6855 NULL, xcalloc, xfree);
6856
6857 for (ix = 0;
6858 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6859 ix, iter);
6860 ++ix)
6861 recursively_compute_inclusions (&result_children, all_children, iter);
6862
6863 /* Now we have a transitive closure of all the included CUs, so
6864 we can convert it to a list of symtabs. */
6865 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6866 symtab->includes
6867 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6868 (len + 1) * sizeof (struct symtab *));
6869 for (ix = 0;
6870 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6871 ++ix)
6872 symtab->includes[ix] = get_symtab (iter);
6873 symtab->includes[len] = NULL;
6874
6875 VEC_free (dwarf2_per_cu_ptr, result_children);
6876 htab_delete (all_children);
6877 }
6878 }
6879
6880 /* Compute the 'includes' field for the symtabs of all the CUs we just
6881 read. */
6882
6883 static void
6884 process_cu_includes (void)
6885 {
6886 int ix;
6887 struct dwarf2_per_cu_data *iter;
6888
6889 for (ix = 0;
6890 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6891 ix, iter);
6892 ++ix)
6893 {
6894 if (! iter->is_debug_types)
6895 compute_symtab_includes (iter);
6896 }
6897
6898 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6899 }
6900
6901 /* Generate full symbol information for PER_CU, whose DIEs have
6902 already been loaded into memory. */
6903
6904 static void
6905 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6906 enum language pretend_language)
6907 {
6908 struct dwarf2_cu *cu = per_cu->cu;
6909 struct objfile *objfile = per_cu->objfile;
6910 CORE_ADDR lowpc, highpc;
6911 struct symtab *symtab;
6912 struct cleanup *back_to, *delayed_list_cleanup;
6913 CORE_ADDR baseaddr;
6914 struct block *static_block;
6915
6916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6917
6918 buildsym_init ();
6919 back_to = make_cleanup (really_free_pendings, NULL);
6920 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6921
6922 cu->list_in_scope = &file_symbols;
6923
6924 cu->language = pretend_language;
6925 cu->language_defn = language_def (cu->language);
6926
6927 /* Do line number decoding in read_file_scope () */
6928 process_die (cu->dies, cu);
6929
6930 /* For now fudge the Go package. */
6931 if (cu->language == language_go)
6932 fixup_go_packaging (cu);
6933
6934 /* Now that we have processed all the DIEs in the CU, all the types
6935 should be complete, and it should now be safe to compute all of the
6936 physnames. */
6937 compute_delayed_physnames (cu);
6938 do_cleanups (delayed_list_cleanup);
6939
6940 /* Some compilers don't define a DW_AT_high_pc attribute for the
6941 compilation unit. If the DW_AT_high_pc is missing, synthesize
6942 it, by scanning the DIE's below the compilation unit. */
6943 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6944
6945 static_block
6946 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6947 per_cu->s.imported_symtabs != NULL);
6948
6949 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6950 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6951 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6952 addrmap to help ensure it has an accurate map of pc values belonging to
6953 this comp unit. */
6954 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6955
6956 symtab = end_symtab_from_static_block (static_block, objfile,
6957 SECT_OFF_TEXT (objfile), 0);
6958
6959 if (symtab != NULL)
6960 {
6961 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6962
6963 /* Set symtab language to language from DW_AT_language. If the
6964 compilation is from a C file generated by language preprocessors, do
6965 not set the language if it was already deduced by start_subfile. */
6966 if (!(cu->language == language_c && symtab->language != language_c))
6967 symtab->language = cu->language;
6968
6969 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6970 produce DW_AT_location with location lists but it can be possibly
6971 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6972 there were bugs in prologue debug info, fixed later in GCC-4.5
6973 by "unwind info for epilogues" patch (which is not directly related).
6974
6975 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6976 needed, it would be wrong due to missing DW_AT_producer there.
6977
6978 Still one can confuse GDB by using non-standard GCC compilation
6979 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6980 */
6981 if (cu->has_loclist && gcc_4_minor >= 5)
6982 symtab->locations_valid = 1;
6983
6984 if (gcc_4_minor >= 5)
6985 symtab->epilogue_unwind_valid = 1;
6986
6987 symtab->call_site_htab = cu->call_site_htab;
6988 }
6989
6990 if (dwarf2_per_objfile->using_index)
6991 per_cu->v.quick->symtab = symtab;
6992 else
6993 {
6994 struct partial_symtab *pst = per_cu->v.psymtab;
6995 pst->symtab = symtab;
6996 pst->readin = 1;
6997 }
6998
6999 /* Push it for inclusion processing later. */
7000 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7001
7002 do_cleanups (back_to);
7003 }
7004
7005 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7006 already been loaded into memory. */
7007
7008 static void
7009 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7010 enum language pretend_language)
7011 {
7012 struct dwarf2_cu *cu = per_cu->cu;
7013 struct objfile *objfile = per_cu->objfile;
7014 struct symtab *symtab;
7015 struct cleanup *back_to, *delayed_list_cleanup;
7016
7017 buildsym_init ();
7018 back_to = make_cleanup (really_free_pendings, NULL);
7019 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7020
7021 cu->list_in_scope = &file_symbols;
7022
7023 cu->language = pretend_language;
7024 cu->language_defn = language_def (cu->language);
7025
7026 /* The symbol tables are set up in read_type_unit_scope. */
7027 process_die (cu->dies, cu);
7028
7029 /* For now fudge the Go package. */
7030 if (cu->language == language_go)
7031 fixup_go_packaging (cu);
7032
7033 /* Now that we have processed all the DIEs in the CU, all the types
7034 should be complete, and it should now be safe to compute all of the
7035 physnames. */
7036 compute_delayed_physnames (cu);
7037 do_cleanups (delayed_list_cleanup);
7038
7039 /* TUs share symbol tables.
7040 If this is the first TU to use this symtab, complete the construction
7041 of it with end_expandable_symtab. Otherwise, complete the addition of
7042 this TU's symbols to the existing symtab. */
7043 if (per_cu->s.type_unit_group->primary_symtab == NULL)
7044 {
7045 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7046 per_cu->s.type_unit_group->primary_symtab = symtab;
7047
7048 if (symtab != NULL)
7049 {
7050 /* Set symtab language to language from DW_AT_language. If the
7051 compilation is from a C file generated by language preprocessors,
7052 do not set the language if it was already deduced by
7053 start_subfile. */
7054 if (!(cu->language == language_c && symtab->language != language_c))
7055 symtab->language = cu->language;
7056 }
7057 }
7058 else
7059 {
7060 augment_type_symtab (objfile,
7061 per_cu->s.type_unit_group->primary_symtab);
7062 symtab = per_cu->s.type_unit_group->primary_symtab;
7063 }
7064
7065 if (dwarf2_per_objfile->using_index)
7066 per_cu->v.quick->symtab = symtab;
7067 else
7068 {
7069 struct partial_symtab *pst = per_cu->v.psymtab;
7070 pst->symtab = symtab;
7071 pst->readin = 1;
7072 }
7073
7074 do_cleanups (back_to);
7075 }
7076
7077 /* Process an imported unit DIE. */
7078
7079 static void
7080 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7081 {
7082 struct attribute *attr;
7083
7084 /* For now we don't handle imported units in type units. */
7085 if (cu->per_cu->is_debug_types)
7086 {
7087 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7088 " supported in type units [in module %s]"),
7089 cu->objfile->name);
7090 }
7091
7092 attr = dwarf2_attr (die, DW_AT_import, cu);
7093 if (attr != NULL)
7094 {
7095 struct dwarf2_per_cu_data *per_cu;
7096 struct symtab *imported_symtab;
7097 sect_offset offset;
7098 int is_dwz;
7099
7100 offset = dwarf2_get_ref_die_offset (attr);
7101 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7102 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7103
7104 /* Queue the unit, if needed. */
7105 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7106 load_full_comp_unit (per_cu, cu->language);
7107
7108 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7109 per_cu);
7110 }
7111 }
7112
7113 /* Process a die and its children. */
7114
7115 static void
7116 process_die (struct die_info *die, struct dwarf2_cu *cu)
7117 {
7118 switch (die->tag)
7119 {
7120 case DW_TAG_padding:
7121 break;
7122 case DW_TAG_compile_unit:
7123 case DW_TAG_partial_unit:
7124 read_file_scope (die, cu);
7125 break;
7126 case DW_TAG_type_unit:
7127 read_type_unit_scope (die, cu);
7128 break;
7129 case DW_TAG_subprogram:
7130 case DW_TAG_inlined_subroutine:
7131 read_func_scope (die, cu);
7132 break;
7133 case DW_TAG_lexical_block:
7134 case DW_TAG_try_block:
7135 case DW_TAG_catch_block:
7136 read_lexical_block_scope (die, cu);
7137 break;
7138 case DW_TAG_GNU_call_site:
7139 read_call_site_scope (die, cu);
7140 break;
7141 case DW_TAG_class_type:
7142 case DW_TAG_interface_type:
7143 case DW_TAG_structure_type:
7144 case DW_TAG_union_type:
7145 process_structure_scope (die, cu);
7146 break;
7147 case DW_TAG_enumeration_type:
7148 process_enumeration_scope (die, cu);
7149 break;
7150
7151 /* These dies have a type, but processing them does not create
7152 a symbol or recurse to process the children. Therefore we can
7153 read them on-demand through read_type_die. */
7154 case DW_TAG_subroutine_type:
7155 case DW_TAG_set_type:
7156 case DW_TAG_array_type:
7157 case DW_TAG_pointer_type:
7158 case DW_TAG_ptr_to_member_type:
7159 case DW_TAG_reference_type:
7160 case DW_TAG_string_type:
7161 break;
7162
7163 case DW_TAG_base_type:
7164 case DW_TAG_subrange_type:
7165 case DW_TAG_typedef:
7166 /* Add a typedef symbol for the type definition, if it has a
7167 DW_AT_name. */
7168 new_symbol (die, read_type_die (die, cu), cu);
7169 break;
7170 case DW_TAG_common_block:
7171 read_common_block (die, cu);
7172 break;
7173 case DW_TAG_common_inclusion:
7174 break;
7175 case DW_TAG_namespace:
7176 processing_has_namespace_info = 1;
7177 read_namespace (die, cu);
7178 break;
7179 case DW_TAG_module:
7180 processing_has_namespace_info = 1;
7181 read_module (die, cu);
7182 break;
7183 case DW_TAG_imported_declaration:
7184 case DW_TAG_imported_module:
7185 processing_has_namespace_info = 1;
7186 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7187 || cu->language != language_fortran))
7188 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7189 dwarf_tag_name (die->tag));
7190 read_import_statement (die, cu);
7191 break;
7192
7193 case DW_TAG_imported_unit:
7194 process_imported_unit_die (die, cu);
7195 break;
7196
7197 default:
7198 new_symbol (die, NULL, cu);
7199 break;
7200 }
7201 }
7202
7203 /* A helper function for dwarf2_compute_name which determines whether DIE
7204 needs to have the name of the scope prepended to the name listed in the
7205 die. */
7206
7207 static int
7208 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7209 {
7210 struct attribute *attr;
7211
7212 switch (die->tag)
7213 {
7214 case DW_TAG_namespace:
7215 case DW_TAG_typedef:
7216 case DW_TAG_class_type:
7217 case DW_TAG_interface_type:
7218 case DW_TAG_structure_type:
7219 case DW_TAG_union_type:
7220 case DW_TAG_enumeration_type:
7221 case DW_TAG_enumerator:
7222 case DW_TAG_subprogram:
7223 case DW_TAG_member:
7224 return 1;
7225
7226 case DW_TAG_variable:
7227 case DW_TAG_constant:
7228 /* We only need to prefix "globally" visible variables. These include
7229 any variable marked with DW_AT_external or any variable that
7230 lives in a namespace. [Variables in anonymous namespaces
7231 require prefixing, but they are not DW_AT_external.] */
7232
7233 if (dwarf2_attr (die, DW_AT_specification, cu))
7234 {
7235 struct dwarf2_cu *spec_cu = cu;
7236
7237 return die_needs_namespace (die_specification (die, &spec_cu),
7238 spec_cu);
7239 }
7240
7241 attr = dwarf2_attr (die, DW_AT_external, cu);
7242 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7243 && die->parent->tag != DW_TAG_module)
7244 return 0;
7245 /* A variable in a lexical block of some kind does not need a
7246 namespace, even though in C++ such variables may be external
7247 and have a mangled name. */
7248 if (die->parent->tag == DW_TAG_lexical_block
7249 || die->parent->tag == DW_TAG_try_block
7250 || die->parent->tag == DW_TAG_catch_block
7251 || die->parent->tag == DW_TAG_subprogram)
7252 return 0;
7253 return 1;
7254
7255 default:
7256 return 0;
7257 }
7258 }
7259
7260 /* Retrieve the last character from a mem_file. */
7261
7262 static void
7263 do_ui_file_peek_last (void *object, const char *buffer, long length)
7264 {
7265 char *last_char_p = (char *) object;
7266
7267 if (length > 0)
7268 *last_char_p = buffer[length - 1];
7269 }
7270
7271 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7272 compute the physname for the object, which include a method's:
7273 - formal parameters (C++/Java),
7274 - receiver type (Go),
7275 - return type (Java).
7276
7277 The term "physname" is a bit confusing.
7278 For C++, for example, it is the demangled name.
7279 For Go, for example, it's the mangled name.
7280
7281 For Ada, return the DIE's linkage name rather than the fully qualified
7282 name. PHYSNAME is ignored..
7283
7284 The result is allocated on the objfile_obstack and canonicalized. */
7285
7286 static const char *
7287 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7288 int physname)
7289 {
7290 struct objfile *objfile = cu->objfile;
7291
7292 if (name == NULL)
7293 name = dwarf2_name (die, cu);
7294
7295 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7296 compute it by typename_concat inside GDB. */
7297 if (cu->language == language_ada
7298 || (cu->language == language_fortran && physname))
7299 {
7300 /* For Ada unit, we prefer the linkage name over the name, as
7301 the former contains the exported name, which the user expects
7302 to be able to reference. Ideally, we want the user to be able
7303 to reference this entity using either natural or linkage name,
7304 but we haven't started looking at this enhancement yet. */
7305 struct attribute *attr;
7306
7307 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7308 if (attr == NULL)
7309 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7310 if (attr && DW_STRING (attr))
7311 return DW_STRING (attr);
7312 }
7313
7314 /* These are the only languages we know how to qualify names in. */
7315 if (name != NULL
7316 && (cu->language == language_cplus || cu->language == language_java
7317 || cu->language == language_fortran))
7318 {
7319 if (die_needs_namespace (die, cu))
7320 {
7321 long length;
7322 const char *prefix;
7323 struct ui_file *buf;
7324
7325 prefix = determine_prefix (die, cu);
7326 buf = mem_fileopen ();
7327 if (*prefix != '\0')
7328 {
7329 char *prefixed_name = typename_concat (NULL, prefix, name,
7330 physname, cu);
7331
7332 fputs_unfiltered (prefixed_name, buf);
7333 xfree (prefixed_name);
7334 }
7335 else
7336 fputs_unfiltered (name, buf);
7337
7338 /* Template parameters may be specified in the DIE's DW_AT_name, or
7339 as children with DW_TAG_template_type_param or
7340 DW_TAG_value_type_param. If the latter, add them to the name
7341 here. If the name already has template parameters, then
7342 skip this step; some versions of GCC emit both, and
7343 it is more efficient to use the pre-computed name.
7344
7345 Something to keep in mind about this process: it is very
7346 unlikely, or in some cases downright impossible, to produce
7347 something that will match the mangled name of a function.
7348 If the definition of the function has the same debug info,
7349 we should be able to match up with it anyway. But fallbacks
7350 using the minimal symbol, for instance to find a method
7351 implemented in a stripped copy of libstdc++, will not work.
7352 If we do not have debug info for the definition, we will have to
7353 match them up some other way.
7354
7355 When we do name matching there is a related problem with function
7356 templates; two instantiated function templates are allowed to
7357 differ only by their return types, which we do not add here. */
7358
7359 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7360 {
7361 struct attribute *attr;
7362 struct die_info *child;
7363 int first = 1;
7364
7365 die->building_fullname = 1;
7366
7367 for (child = die->child; child != NULL; child = child->sibling)
7368 {
7369 struct type *type;
7370 LONGEST value;
7371 gdb_byte *bytes;
7372 struct dwarf2_locexpr_baton *baton;
7373 struct value *v;
7374
7375 if (child->tag != DW_TAG_template_type_param
7376 && child->tag != DW_TAG_template_value_param)
7377 continue;
7378
7379 if (first)
7380 {
7381 fputs_unfiltered ("<", buf);
7382 first = 0;
7383 }
7384 else
7385 fputs_unfiltered (", ", buf);
7386
7387 attr = dwarf2_attr (child, DW_AT_type, cu);
7388 if (attr == NULL)
7389 {
7390 complaint (&symfile_complaints,
7391 _("template parameter missing DW_AT_type"));
7392 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7393 continue;
7394 }
7395 type = die_type (child, cu);
7396
7397 if (child->tag == DW_TAG_template_type_param)
7398 {
7399 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7400 continue;
7401 }
7402
7403 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7404 if (attr == NULL)
7405 {
7406 complaint (&symfile_complaints,
7407 _("template parameter missing "
7408 "DW_AT_const_value"));
7409 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7410 continue;
7411 }
7412
7413 dwarf2_const_value_attr (attr, type, name,
7414 &cu->comp_unit_obstack, cu,
7415 &value, &bytes, &baton);
7416
7417 if (TYPE_NOSIGN (type))
7418 /* GDB prints characters as NUMBER 'CHAR'. If that's
7419 changed, this can use value_print instead. */
7420 c_printchar (value, type, buf);
7421 else
7422 {
7423 struct value_print_options opts;
7424
7425 if (baton != NULL)
7426 v = dwarf2_evaluate_loc_desc (type, NULL,
7427 baton->data,
7428 baton->size,
7429 baton->per_cu);
7430 else if (bytes != NULL)
7431 {
7432 v = allocate_value (type);
7433 memcpy (value_contents_writeable (v), bytes,
7434 TYPE_LENGTH (type));
7435 }
7436 else
7437 v = value_from_longest (type, value);
7438
7439 /* Specify decimal so that we do not depend on
7440 the radix. */
7441 get_formatted_print_options (&opts, 'd');
7442 opts.raw = 1;
7443 value_print (v, buf, &opts);
7444 release_value (v);
7445 value_free (v);
7446 }
7447 }
7448
7449 die->building_fullname = 0;
7450
7451 if (!first)
7452 {
7453 /* Close the argument list, with a space if necessary
7454 (nested templates). */
7455 char last_char = '\0';
7456 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7457 if (last_char == '>')
7458 fputs_unfiltered (" >", buf);
7459 else
7460 fputs_unfiltered (">", buf);
7461 }
7462 }
7463
7464 /* For Java and C++ methods, append formal parameter type
7465 information, if PHYSNAME. */
7466
7467 if (physname && die->tag == DW_TAG_subprogram
7468 && (cu->language == language_cplus
7469 || cu->language == language_java))
7470 {
7471 struct type *type = read_type_die (die, cu);
7472
7473 c_type_print_args (type, buf, 1, cu->language,
7474 &type_print_raw_options);
7475
7476 if (cu->language == language_java)
7477 {
7478 /* For java, we must append the return type to method
7479 names. */
7480 if (die->tag == DW_TAG_subprogram)
7481 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7482 0, 0, &type_print_raw_options);
7483 }
7484 else if (cu->language == language_cplus)
7485 {
7486 /* Assume that an artificial first parameter is
7487 "this", but do not crash if it is not. RealView
7488 marks unnamed (and thus unused) parameters as
7489 artificial; there is no way to differentiate
7490 the two cases. */
7491 if (TYPE_NFIELDS (type) > 0
7492 && TYPE_FIELD_ARTIFICIAL (type, 0)
7493 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7494 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7495 0))))
7496 fputs_unfiltered (" const", buf);
7497 }
7498 }
7499
7500 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7501 &length);
7502 ui_file_delete (buf);
7503
7504 if (cu->language == language_cplus)
7505 {
7506 char *cname
7507 = dwarf2_canonicalize_name (name, cu,
7508 &objfile->objfile_obstack);
7509
7510 if (cname != NULL)
7511 name = cname;
7512 }
7513 }
7514 }
7515
7516 return name;
7517 }
7518
7519 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7520 If scope qualifiers are appropriate they will be added. The result
7521 will be allocated on the objfile_obstack, or NULL if the DIE does
7522 not have a name. NAME may either be from a previous call to
7523 dwarf2_name or NULL.
7524
7525 The output string will be canonicalized (if C++/Java). */
7526
7527 static const char *
7528 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7529 {
7530 return dwarf2_compute_name (name, die, cu, 0);
7531 }
7532
7533 /* Construct a physname for the given DIE in CU. NAME may either be
7534 from a previous call to dwarf2_name or NULL. The result will be
7535 allocated on the objfile_objstack or NULL if the DIE does not have a
7536 name.
7537
7538 The output string will be canonicalized (if C++/Java). */
7539
7540 static const char *
7541 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7542 {
7543 struct objfile *objfile = cu->objfile;
7544 struct attribute *attr;
7545 const char *retval, *mangled = NULL, *canon = NULL;
7546 struct cleanup *back_to;
7547 int need_copy = 1;
7548
7549 /* In this case dwarf2_compute_name is just a shortcut not building anything
7550 on its own. */
7551 if (!die_needs_namespace (die, cu))
7552 return dwarf2_compute_name (name, die, cu, 1);
7553
7554 back_to = make_cleanup (null_cleanup, NULL);
7555
7556 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7557 if (!attr)
7558 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7559
7560 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7561 has computed. */
7562 if (attr && DW_STRING (attr))
7563 {
7564 char *demangled;
7565
7566 mangled = DW_STRING (attr);
7567
7568 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7569 type. It is easier for GDB users to search for such functions as
7570 `name(params)' than `long name(params)'. In such case the minimal
7571 symbol names do not match the full symbol names but for template
7572 functions there is never a need to look up their definition from their
7573 declaration so the only disadvantage remains the minimal symbol
7574 variant `long name(params)' does not have the proper inferior type.
7575 */
7576
7577 if (cu->language == language_go)
7578 {
7579 /* This is a lie, but we already lie to the caller new_symbol_full.
7580 new_symbol_full assumes we return the mangled name.
7581 This just undoes that lie until things are cleaned up. */
7582 demangled = NULL;
7583 }
7584 else
7585 {
7586 demangled = cplus_demangle (mangled,
7587 (DMGL_PARAMS | DMGL_ANSI
7588 | (cu->language == language_java
7589 ? DMGL_JAVA | DMGL_RET_POSTFIX
7590 : DMGL_RET_DROP)));
7591 }
7592 if (demangled)
7593 {
7594 make_cleanup (xfree, demangled);
7595 canon = demangled;
7596 }
7597 else
7598 {
7599 canon = mangled;
7600 need_copy = 0;
7601 }
7602 }
7603
7604 if (canon == NULL || check_physname)
7605 {
7606 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7607
7608 if (canon != NULL && strcmp (physname, canon) != 0)
7609 {
7610 /* It may not mean a bug in GDB. The compiler could also
7611 compute DW_AT_linkage_name incorrectly. But in such case
7612 GDB would need to be bug-to-bug compatible. */
7613
7614 complaint (&symfile_complaints,
7615 _("Computed physname <%s> does not match demangled <%s> "
7616 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7617 physname, canon, mangled, die->offset.sect_off, objfile->name);
7618
7619 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7620 is available here - over computed PHYSNAME. It is safer
7621 against both buggy GDB and buggy compilers. */
7622
7623 retval = canon;
7624 }
7625 else
7626 {
7627 retval = physname;
7628 need_copy = 0;
7629 }
7630 }
7631 else
7632 retval = canon;
7633
7634 if (need_copy)
7635 retval = obsavestring (retval, strlen (retval),
7636 &objfile->objfile_obstack);
7637
7638 do_cleanups (back_to);
7639 return retval;
7640 }
7641
7642 /* Read the import statement specified by the given die and record it. */
7643
7644 static void
7645 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7646 {
7647 struct objfile *objfile = cu->objfile;
7648 struct attribute *import_attr;
7649 struct die_info *imported_die, *child_die;
7650 struct dwarf2_cu *imported_cu;
7651 const char *imported_name;
7652 const char *imported_name_prefix;
7653 const char *canonical_name;
7654 const char *import_alias;
7655 const char *imported_declaration = NULL;
7656 const char *import_prefix;
7657 VEC (const_char_ptr) *excludes = NULL;
7658 struct cleanup *cleanups;
7659
7660 char *temp;
7661
7662 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7663 if (import_attr == NULL)
7664 {
7665 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7666 dwarf_tag_name (die->tag));
7667 return;
7668 }
7669
7670 imported_cu = cu;
7671 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7672 imported_name = dwarf2_name (imported_die, imported_cu);
7673 if (imported_name == NULL)
7674 {
7675 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7676
7677 The import in the following code:
7678 namespace A
7679 {
7680 typedef int B;
7681 }
7682
7683 int main ()
7684 {
7685 using A::B;
7686 B b;
7687 return b;
7688 }
7689
7690 ...
7691 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7692 <52> DW_AT_decl_file : 1
7693 <53> DW_AT_decl_line : 6
7694 <54> DW_AT_import : <0x75>
7695 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7696 <59> DW_AT_name : B
7697 <5b> DW_AT_decl_file : 1
7698 <5c> DW_AT_decl_line : 2
7699 <5d> DW_AT_type : <0x6e>
7700 ...
7701 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7702 <76> DW_AT_byte_size : 4
7703 <77> DW_AT_encoding : 5 (signed)
7704
7705 imports the wrong die ( 0x75 instead of 0x58 ).
7706 This case will be ignored until the gcc bug is fixed. */
7707 return;
7708 }
7709
7710 /* Figure out the local name after import. */
7711 import_alias = dwarf2_name (die, cu);
7712
7713 /* Figure out where the statement is being imported to. */
7714 import_prefix = determine_prefix (die, cu);
7715
7716 /* Figure out what the scope of the imported die is and prepend it
7717 to the name of the imported die. */
7718 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7719
7720 if (imported_die->tag != DW_TAG_namespace
7721 && imported_die->tag != DW_TAG_module)
7722 {
7723 imported_declaration = imported_name;
7724 canonical_name = imported_name_prefix;
7725 }
7726 else if (strlen (imported_name_prefix) > 0)
7727 {
7728 temp = alloca (strlen (imported_name_prefix)
7729 + 2 + strlen (imported_name) + 1);
7730 strcpy (temp, imported_name_prefix);
7731 strcat (temp, "::");
7732 strcat (temp, imported_name);
7733 canonical_name = temp;
7734 }
7735 else
7736 canonical_name = imported_name;
7737
7738 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7739
7740 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7741 for (child_die = die->child; child_die && child_die->tag;
7742 child_die = sibling_die (child_die))
7743 {
7744 /* DWARF-4: A Fortran use statement with a “rename list” may be
7745 represented by an imported module entry with an import attribute
7746 referring to the module and owned entries corresponding to those
7747 entities that are renamed as part of being imported. */
7748
7749 if (child_die->tag != DW_TAG_imported_declaration)
7750 {
7751 complaint (&symfile_complaints,
7752 _("child DW_TAG_imported_declaration expected "
7753 "- DIE at 0x%x [in module %s]"),
7754 child_die->offset.sect_off, objfile->name);
7755 continue;
7756 }
7757
7758 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7759 if (import_attr == NULL)
7760 {
7761 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7762 dwarf_tag_name (child_die->tag));
7763 continue;
7764 }
7765
7766 imported_cu = cu;
7767 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7768 &imported_cu);
7769 imported_name = dwarf2_name (imported_die, imported_cu);
7770 if (imported_name == NULL)
7771 {
7772 complaint (&symfile_complaints,
7773 _("child DW_TAG_imported_declaration has unknown "
7774 "imported name - DIE at 0x%x [in module %s]"),
7775 child_die->offset.sect_off, objfile->name);
7776 continue;
7777 }
7778
7779 VEC_safe_push (const_char_ptr, excludes, imported_name);
7780
7781 process_die (child_die, cu);
7782 }
7783
7784 cp_add_using_directive (import_prefix,
7785 canonical_name,
7786 import_alias,
7787 imported_declaration,
7788 excludes,
7789 &objfile->objfile_obstack);
7790
7791 do_cleanups (cleanups);
7792 }
7793
7794 /* Cleanup function for handle_DW_AT_stmt_list. */
7795
7796 static void
7797 free_cu_line_header (void *arg)
7798 {
7799 struct dwarf2_cu *cu = arg;
7800
7801 free_line_header (cu->line_header);
7802 cu->line_header = NULL;
7803 }
7804
7805 static void
7806 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7807 char **name, char **comp_dir)
7808 {
7809 struct attribute *attr;
7810
7811 *name = NULL;
7812 *comp_dir = NULL;
7813
7814 /* Find the filename. Do not use dwarf2_name here, since the filename
7815 is not a source language identifier. */
7816 attr = dwarf2_attr (die, DW_AT_name, cu);
7817 if (attr)
7818 {
7819 *name = DW_STRING (attr);
7820 }
7821
7822 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7823 if (attr)
7824 *comp_dir = DW_STRING (attr);
7825 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7826 {
7827 *comp_dir = ldirname (*name);
7828 if (*comp_dir != NULL)
7829 make_cleanup (xfree, *comp_dir);
7830 }
7831 if (*comp_dir != NULL)
7832 {
7833 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7834 directory, get rid of it. */
7835 char *cp = strchr (*comp_dir, ':');
7836
7837 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7838 *comp_dir = cp + 1;
7839 }
7840
7841 if (*name == NULL)
7842 *name = "<unknown>";
7843 }
7844
7845 /* Handle DW_AT_stmt_list for a compilation unit.
7846 DIE is the DW_TAG_compile_unit die for CU.
7847 COMP_DIR is the compilation directory.
7848 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7849
7850 static void
7851 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7852 const char *comp_dir)
7853 {
7854 struct attribute *attr;
7855
7856 gdb_assert (! cu->per_cu->is_debug_types);
7857
7858 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7859 if (attr)
7860 {
7861 unsigned int line_offset = DW_UNSND (attr);
7862 struct line_header *line_header
7863 = dwarf_decode_line_header (line_offset, cu);
7864
7865 if (line_header)
7866 {
7867 cu->line_header = line_header;
7868 make_cleanup (free_cu_line_header, cu);
7869 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7870 }
7871 }
7872 }
7873
7874 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7875
7876 static void
7877 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7878 {
7879 struct objfile *objfile = dwarf2_per_objfile->objfile;
7880 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7881 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7882 CORE_ADDR highpc = ((CORE_ADDR) 0);
7883 struct attribute *attr;
7884 char *name = NULL;
7885 char *comp_dir = NULL;
7886 struct die_info *child_die;
7887 bfd *abfd = objfile->obfd;
7888 CORE_ADDR baseaddr;
7889
7890 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7891
7892 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7893
7894 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7895 from finish_block. */
7896 if (lowpc == ((CORE_ADDR) -1))
7897 lowpc = highpc;
7898 lowpc += baseaddr;
7899 highpc += baseaddr;
7900
7901 find_file_and_directory (die, cu, &name, &comp_dir);
7902
7903 prepare_one_comp_unit (cu, die, cu->language);
7904
7905 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7906 standardised yet. As a workaround for the language detection we fall
7907 back to the DW_AT_producer string. */
7908 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7909 cu->language = language_opencl;
7910
7911 /* Similar hack for Go. */
7912 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7913 set_cu_language (DW_LANG_Go, cu);
7914
7915 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7916
7917 /* Decode line number information if present. We do this before
7918 processing child DIEs, so that the line header table is available
7919 for DW_AT_decl_file. */
7920 handle_DW_AT_stmt_list (die, cu, comp_dir);
7921
7922 /* Process all dies in compilation unit. */
7923 if (die->child != NULL)
7924 {
7925 child_die = die->child;
7926 while (child_die && child_die->tag)
7927 {
7928 process_die (child_die, cu);
7929 child_die = sibling_die (child_die);
7930 }
7931 }
7932
7933 /* Decode macro information, if present. Dwarf 2 macro information
7934 refers to information in the line number info statement program
7935 header, so we can only read it if we've read the header
7936 successfully. */
7937 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7938 if (attr && cu->line_header)
7939 {
7940 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7941 complaint (&symfile_complaints,
7942 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7943
7944 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7945 }
7946 else
7947 {
7948 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7949 if (attr && cu->line_header)
7950 {
7951 unsigned int macro_offset = DW_UNSND (attr);
7952
7953 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7954 }
7955 }
7956
7957 do_cleanups (back_to);
7958 }
7959
7960 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7961 Create the set of symtabs used by this TU, or if this TU is sharing
7962 symtabs with another TU and the symtabs have already been created
7963 then restore those symtabs in the line header.
7964 We don't need the pc/line-number mapping for type units. */
7965
7966 static void
7967 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7968 {
7969 struct objfile *objfile = dwarf2_per_objfile->objfile;
7970 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7971 struct type_unit_group *tu_group;
7972 int first_time;
7973 struct line_header *lh;
7974 struct attribute *attr;
7975 unsigned int i, line_offset;
7976
7977 gdb_assert (per_cu->is_debug_types);
7978
7979 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7980
7981 /* If we're using .gdb_index (includes -readnow) then
7982 per_cu->s.type_unit_group may not have been set up yet. */
7983 if (per_cu->s.type_unit_group == NULL)
7984 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7985 tu_group = per_cu->s.type_unit_group;
7986
7987 /* If we've already processed this stmt_list there's no real need to
7988 do it again, we could fake it and just recreate the part we need
7989 (file name,index -> symtab mapping). If data shows this optimization
7990 is useful we can do it then. */
7991 first_time = tu_group->primary_symtab == NULL;
7992
7993 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7994 debug info. */
7995 lh = NULL;
7996 if (attr != NULL)
7997 {
7998 line_offset = DW_UNSND (attr);
7999 lh = dwarf_decode_line_header (line_offset, cu);
8000 }
8001 if (lh == NULL)
8002 {
8003 if (first_time)
8004 dwarf2_start_symtab (cu, "", NULL, 0);
8005 else
8006 {
8007 gdb_assert (tu_group->symtabs == NULL);
8008 restart_symtab (0);
8009 }
8010 /* Note: The primary symtab will get allocated at the end. */
8011 return;
8012 }
8013
8014 cu->line_header = lh;
8015 make_cleanup (free_cu_line_header, cu);
8016
8017 if (first_time)
8018 {
8019 dwarf2_start_symtab (cu, "", NULL, 0);
8020
8021 tu_group->num_symtabs = lh->num_file_names;
8022 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8023
8024 for (i = 0; i < lh->num_file_names; ++i)
8025 {
8026 char *dir = NULL;
8027 struct file_entry *fe = &lh->file_names[i];
8028
8029 if (fe->dir_index)
8030 dir = lh->include_dirs[fe->dir_index - 1];
8031 dwarf2_start_subfile (fe->name, dir, NULL);
8032
8033 /* Note: We don't have to watch for the main subfile here, type units
8034 don't have DW_AT_name. */
8035
8036 if (current_subfile->symtab == NULL)
8037 {
8038 /* NOTE: start_subfile will recognize when it's been passed
8039 a file it has already seen. So we can't assume there's a
8040 simple mapping from lh->file_names to subfiles,
8041 lh->file_names may contain dups. */
8042 current_subfile->symtab = allocate_symtab (current_subfile->name,
8043 objfile);
8044 }
8045
8046 fe->symtab = current_subfile->symtab;
8047 tu_group->symtabs[i] = fe->symtab;
8048 }
8049 }
8050 else
8051 {
8052 restart_symtab (0);
8053
8054 for (i = 0; i < lh->num_file_names; ++i)
8055 {
8056 struct file_entry *fe = &lh->file_names[i];
8057
8058 fe->symtab = tu_group->symtabs[i];
8059 }
8060 }
8061
8062 /* The main symtab is allocated last. Type units don't have DW_AT_name
8063 so they don't have a "real" (so to speak) symtab anyway.
8064 There is later code that will assign the main symtab to all symbols
8065 that don't have one. We need to handle the case of a symbol with a
8066 missing symtab (DW_AT_decl_file) anyway. */
8067 }
8068
8069 /* Process DW_TAG_type_unit.
8070 For TUs we want to skip the first top level sibling if it's not the
8071 actual type being defined by this TU. In this case the first top
8072 level sibling is there to provide context only. */
8073
8074 static void
8075 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8076 {
8077 struct die_info *child_die;
8078
8079 prepare_one_comp_unit (cu, die, language_minimal);
8080
8081 /* Initialize (or reinitialize) the machinery for building symtabs.
8082 We do this before processing child DIEs, so that the line header table
8083 is available for DW_AT_decl_file. */
8084 setup_type_unit_groups (die, cu);
8085
8086 if (die->child != NULL)
8087 {
8088 child_die = die->child;
8089 while (child_die && child_die->tag)
8090 {
8091 process_die (child_die, cu);
8092 child_die = sibling_die (child_die);
8093 }
8094 }
8095 }
8096 \f
8097 /* DWO/DWP files.
8098
8099 http://gcc.gnu.org/wiki/DebugFission
8100 http://gcc.gnu.org/wiki/DebugFissionDWP
8101
8102 To simplify handling of both DWO files ("object" files with the DWARF info)
8103 and DWP files (a file with the DWOs packaged up into one file), we treat
8104 DWP files as having a collection of virtual DWO files. */
8105
8106 static hashval_t
8107 hash_dwo_file (const void *item)
8108 {
8109 const struct dwo_file *dwo_file = item;
8110
8111 return htab_hash_string (dwo_file->name);
8112 }
8113
8114 static int
8115 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8116 {
8117 const struct dwo_file *lhs = item_lhs;
8118 const struct dwo_file *rhs = item_rhs;
8119
8120 return strcmp (lhs->name, rhs->name) == 0;
8121 }
8122
8123 /* Allocate a hash table for DWO files. */
8124
8125 static htab_t
8126 allocate_dwo_file_hash_table (void)
8127 {
8128 struct objfile *objfile = dwarf2_per_objfile->objfile;
8129
8130 return htab_create_alloc_ex (41,
8131 hash_dwo_file,
8132 eq_dwo_file,
8133 NULL,
8134 &objfile->objfile_obstack,
8135 hashtab_obstack_allocate,
8136 dummy_obstack_deallocate);
8137 }
8138
8139 /* Lookup DWO file DWO_NAME. */
8140
8141 static void **
8142 lookup_dwo_file_slot (const char *dwo_name)
8143 {
8144 struct dwo_file find_entry;
8145 void **slot;
8146
8147 if (dwarf2_per_objfile->dwo_files == NULL)
8148 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8149
8150 memset (&find_entry, 0, sizeof (find_entry));
8151 find_entry.name = dwo_name;
8152 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8153
8154 return slot;
8155 }
8156
8157 static hashval_t
8158 hash_dwo_unit (const void *item)
8159 {
8160 const struct dwo_unit *dwo_unit = item;
8161
8162 /* This drops the top 32 bits of the id, but is ok for a hash. */
8163 return dwo_unit->signature;
8164 }
8165
8166 static int
8167 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8168 {
8169 const struct dwo_unit *lhs = item_lhs;
8170 const struct dwo_unit *rhs = item_rhs;
8171
8172 /* The signature is assumed to be unique within the DWO file.
8173 So while object file CU dwo_id's always have the value zero,
8174 that's OK, assuming each object file DWO file has only one CU,
8175 and that's the rule for now. */
8176 return lhs->signature == rhs->signature;
8177 }
8178
8179 /* Allocate a hash table for DWO CUs,TUs.
8180 There is one of these tables for each of CUs,TUs for each DWO file. */
8181
8182 static htab_t
8183 allocate_dwo_unit_table (struct objfile *objfile)
8184 {
8185 /* Start out with a pretty small number.
8186 Generally DWO files contain only one CU and maybe some TUs. */
8187 return htab_create_alloc_ex (3,
8188 hash_dwo_unit,
8189 eq_dwo_unit,
8190 NULL,
8191 &objfile->objfile_obstack,
8192 hashtab_obstack_allocate,
8193 dummy_obstack_deallocate);
8194 }
8195
8196 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8197
8198 struct create_dwo_info_table_data
8199 {
8200 struct dwo_file *dwo_file;
8201 htab_t cu_htab;
8202 };
8203
8204 /* die_reader_func for create_dwo_debug_info_hash_table. */
8205
8206 static void
8207 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8208 gdb_byte *info_ptr,
8209 struct die_info *comp_unit_die,
8210 int has_children,
8211 void *datap)
8212 {
8213 struct dwarf2_cu *cu = reader->cu;
8214 struct objfile *objfile = dwarf2_per_objfile->objfile;
8215 sect_offset offset = cu->per_cu->offset;
8216 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8217 struct create_dwo_info_table_data *data = datap;
8218 struct dwo_file *dwo_file = data->dwo_file;
8219 htab_t cu_htab = data->cu_htab;
8220 void **slot;
8221 struct attribute *attr;
8222 struct dwo_unit *dwo_unit;
8223
8224 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8225 if (attr == NULL)
8226 {
8227 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8228 " its dwo_id [in module %s]"),
8229 offset.sect_off, dwo_file->name);
8230 return;
8231 }
8232
8233 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8234 dwo_unit->dwo_file = dwo_file;
8235 dwo_unit->signature = DW_UNSND (attr);
8236 dwo_unit->info_or_types_section = section;
8237 dwo_unit->offset = offset;
8238 dwo_unit->length = cu->per_cu->length;
8239
8240 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8241 gdb_assert (slot != NULL);
8242 if (*slot != NULL)
8243 {
8244 const struct dwo_unit *dup_dwo_unit = *slot;
8245
8246 complaint (&symfile_complaints,
8247 _("debug entry at offset 0x%x is duplicate to the entry at"
8248 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8249 offset.sect_off, dup_dwo_unit->offset.sect_off,
8250 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8251 dwo_file->name);
8252 }
8253 else
8254 *slot = dwo_unit;
8255
8256 if (dwarf2_read_debug)
8257 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8258 offset.sect_off,
8259 phex (dwo_unit->signature,
8260 sizeof (dwo_unit->signature)));
8261 }
8262
8263 /* Create a hash table to map DWO IDs to their CU entry in
8264 .debug_info.dwo in DWO_FILE.
8265 Note: This function processes DWO files only, not DWP files. */
8266
8267 static htab_t
8268 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8269 {
8270 struct objfile *objfile = dwarf2_per_objfile->objfile;
8271 struct dwarf2_section_info *section = &dwo_file->sections.info;
8272 bfd *abfd;
8273 htab_t cu_htab;
8274 gdb_byte *info_ptr, *end_ptr;
8275 struct create_dwo_info_table_data create_dwo_info_table_data;
8276
8277 dwarf2_read_section (objfile, section);
8278 info_ptr = section->buffer;
8279
8280 if (info_ptr == NULL)
8281 return NULL;
8282
8283 /* We can't set abfd until now because the section may be empty or
8284 not present, in which case section->asection will be NULL. */
8285 abfd = section->asection->owner;
8286
8287 if (dwarf2_read_debug)
8288 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8289 bfd_get_filename (abfd));
8290
8291 cu_htab = allocate_dwo_unit_table (objfile);
8292
8293 create_dwo_info_table_data.dwo_file = dwo_file;
8294 create_dwo_info_table_data.cu_htab = cu_htab;
8295
8296 end_ptr = info_ptr + section->size;
8297 while (info_ptr < end_ptr)
8298 {
8299 struct dwarf2_per_cu_data per_cu;
8300
8301 memset (&per_cu, 0, sizeof (per_cu));
8302 per_cu.objfile = objfile;
8303 per_cu.is_debug_types = 0;
8304 per_cu.offset.sect_off = info_ptr - section->buffer;
8305 per_cu.info_or_types_section = section;
8306
8307 init_cutu_and_read_dies_no_follow (&per_cu,
8308 &dwo_file->sections.abbrev,
8309 dwo_file,
8310 create_dwo_debug_info_hash_table_reader,
8311 &create_dwo_info_table_data);
8312
8313 info_ptr += per_cu.length;
8314 }
8315
8316 return cu_htab;
8317 }
8318
8319 /* DWP file .debug_{cu,tu}_index section format:
8320 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8321
8322 Both index sections have the same format, and serve to map a 64-bit
8323 signature to a set of section numbers. Each section begins with a header,
8324 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8325 indexes, and a pool of 32-bit section numbers. The index sections will be
8326 aligned at 8-byte boundaries in the file.
8327
8328 The index section header contains two unsigned 32-bit values (using the
8329 byte order of the application binary):
8330
8331 N, the number of compilation units or type units in the index
8332 M, the number of slots in the hash table
8333
8334 (We assume that N and M will not exceed 2^32 - 1.)
8335
8336 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8337
8338 The hash table begins at offset 8 in the section, and consists of an array
8339 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8340 order of the application binary). Unused slots in the hash table are 0.
8341 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8342
8343 The parallel table begins immediately after the hash table
8344 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8345 array of 32-bit indexes (using the byte order of the application binary),
8346 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8347 table contains a 32-bit index into the pool of section numbers. For unused
8348 hash table slots, the corresponding entry in the parallel table will be 0.
8349
8350 Given a 64-bit compilation unit signature or a type signature S, an entry
8351 in the hash table is located as follows:
8352
8353 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8354 the low-order k bits all set to 1.
8355
8356 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8357
8358 3) If the hash table entry at index H matches the signature, use that
8359 entry. If the hash table entry at index H is unused (all zeroes),
8360 terminate the search: the signature is not present in the table.
8361
8362 4) Let H = (H + H') modulo M. Repeat at Step 3.
8363
8364 Because M > N and H' and M are relatively prime, the search is guaranteed
8365 to stop at an unused slot or find the match.
8366
8367 The pool of section numbers begins immediately following the hash table
8368 (at offset 8 + 12 * M from the beginning of the section). The pool of
8369 section numbers consists of an array of 32-bit words (using the byte order
8370 of the application binary). Each item in the array is indexed starting
8371 from 0. The hash table entry provides the index of the first section
8372 number in the set. Additional section numbers in the set follow, and the
8373 set is terminated by a 0 entry (section number 0 is not used in ELF).
8374
8375 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8376 section must be the first entry in the set, and the .debug_abbrev.dwo must
8377 be the second entry. Other members of the set may follow in any order. */
8378
8379 /* Create a hash table to map DWO IDs to their CU/TU entry in
8380 .debug_{info,types}.dwo in DWP_FILE.
8381 Returns NULL if there isn't one.
8382 Note: This function processes DWP files only, not DWO files. */
8383
8384 static struct dwp_hash_table *
8385 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8386 {
8387 struct objfile *objfile = dwarf2_per_objfile->objfile;
8388 bfd *dbfd = dwp_file->dbfd;
8389 char *index_ptr, *index_end;
8390 struct dwarf2_section_info *index;
8391 uint32_t version, nr_units, nr_slots;
8392 struct dwp_hash_table *htab;
8393
8394 if (is_debug_types)
8395 index = &dwp_file->sections.tu_index;
8396 else
8397 index = &dwp_file->sections.cu_index;
8398
8399 if (dwarf2_section_empty_p (index))
8400 return NULL;
8401 dwarf2_read_section (objfile, index);
8402
8403 index_ptr = index->buffer;
8404 index_end = index_ptr + index->size;
8405
8406 version = read_4_bytes (dbfd, index_ptr);
8407 index_ptr += 8; /* Skip the unused word. */
8408 nr_units = read_4_bytes (dbfd, index_ptr);
8409 index_ptr += 4;
8410 nr_slots = read_4_bytes (dbfd, index_ptr);
8411 index_ptr += 4;
8412
8413 if (version != 1)
8414 {
8415 error (_("Dwarf Error: unsupported DWP file version (%u)"
8416 " [in module %s]"),
8417 version, dwp_file->name);
8418 }
8419 if (nr_slots != (nr_slots & -nr_slots))
8420 {
8421 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8422 " is not power of 2 [in module %s]"),
8423 nr_slots, dwp_file->name);
8424 }
8425
8426 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8427 htab->nr_units = nr_units;
8428 htab->nr_slots = nr_slots;
8429 htab->hash_table = index_ptr;
8430 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8431 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8432
8433 return htab;
8434 }
8435
8436 /* Update SECTIONS with the data from SECTP.
8437
8438 This function is like the other "locate" section routines that are
8439 passed to bfd_map_over_sections, but in this context the sections to
8440 read comes from the DWP hash table, not the full ELF section table.
8441
8442 The result is non-zero for success, or zero if an error was found. */
8443
8444 static int
8445 locate_virtual_dwo_sections (asection *sectp,
8446 struct virtual_dwo_sections *sections)
8447 {
8448 const struct dwop_section_names *names = &dwop_section_names;
8449
8450 if (section_is_p (sectp->name, &names->abbrev_dwo))
8451 {
8452 /* There can be only one. */
8453 if (sections->abbrev.asection != NULL)
8454 return 0;
8455 sections->abbrev.asection = sectp;
8456 sections->abbrev.size = bfd_get_section_size (sectp);
8457 }
8458 else if (section_is_p (sectp->name, &names->info_dwo)
8459 || section_is_p (sectp->name, &names->types_dwo))
8460 {
8461 /* There can be only one. */
8462 if (sections->info_or_types.asection != NULL)
8463 return 0;
8464 sections->info_or_types.asection = sectp;
8465 sections->info_or_types.size = bfd_get_section_size (sectp);
8466 }
8467 else if (section_is_p (sectp->name, &names->line_dwo))
8468 {
8469 /* There can be only one. */
8470 if (sections->line.asection != NULL)
8471 return 0;
8472 sections->line.asection = sectp;
8473 sections->line.size = bfd_get_section_size (sectp);
8474 }
8475 else if (section_is_p (sectp->name, &names->loc_dwo))
8476 {
8477 /* There can be only one. */
8478 if (sections->loc.asection != NULL)
8479 return 0;
8480 sections->loc.asection = sectp;
8481 sections->loc.size = bfd_get_section_size (sectp);
8482 }
8483 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8484 {
8485 /* There can be only one. */
8486 if (sections->macinfo.asection != NULL)
8487 return 0;
8488 sections->macinfo.asection = sectp;
8489 sections->macinfo.size = bfd_get_section_size (sectp);
8490 }
8491 else if (section_is_p (sectp->name, &names->macro_dwo))
8492 {
8493 /* There can be only one. */
8494 if (sections->macro.asection != NULL)
8495 return 0;
8496 sections->macro.asection = sectp;
8497 sections->macro.size = bfd_get_section_size (sectp);
8498 }
8499 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8500 {
8501 /* There can be only one. */
8502 if (sections->str_offsets.asection != NULL)
8503 return 0;
8504 sections->str_offsets.asection = sectp;
8505 sections->str_offsets.size = bfd_get_section_size (sectp);
8506 }
8507 else
8508 {
8509 /* No other kind of section is valid. */
8510 return 0;
8511 }
8512
8513 return 1;
8514 }
8515
8516 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8517 HTAB is the hash table from the DWP file.
8518 SECTION_INDEX is the index of the DWO in HTAB. */
8519
8520 static struct dwo_unit *
8521 create_dwo_in_dwp (struct dwp_file *dwp_file,
8522 const struct dwp_hash_table *htab,
8523 uint32_t section_index,
8524 ULONGEST signature, int is_debug_types)
8525 {
8526 struct objfile *objfile = dwarf2_per_objfile->objfile;
8527 bfd *dbfd = dwp_file->dbfd;
8528 const char *kind = is_debug_types ? "TU" : "CU";
8529 struct dwo_file *dwo_file;
8530 struct dwo_unit *dwo_unit;
8531 struct virtual_dwo_sections sections;
8532 void **dwo_file_slot;
8533 char *virtual_dwo_name;
8534 struct dwarf2_section_info *cutu;
8535 struct cleanup *cleanups;
8536 int i;
8537
8538 if (dwarf2_read_debug)
8539 {
8540 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8541 kind,
8542 section_index, phex (signature, sizeof (signature)),
8543 dwp_file->name);
8544 }
8545
8546 /* Fetch the sections of this DWO.
8547 Put a limit on the number of sections we look for so that bad data
8548 doesn't cause us to loop forever. */
8549
8550 #define MAX_NR_DWO_SECTIONS \
8551 (1 /* .debug_info or .debug_types */ \
8552 + 1 /* .debug_abbrev */ \
8553 + 1 /* .debug_line */ \
8554 + 1 /* .debug_loc */ \
8555 + 1 /* .debug_str_offsets */ \
8556 + 1 /* .debug_macro */ \
8557 + 1 /* .debug_macinfo */ \
8558 + 1 /* trailing zero */)
8559
8560 memset (&sections, 0, sizeof (sections));
8561 cleanups = make_cleanup (null_cleanup, 0);
8562
8563 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8564 {
8565 asection *sectp;
8566 uint32_t section_nr =
8567 read_4_bytes (dbfd,
8568 htab->section_pool
8569 + (section_index + i) * sizeof (uint32_t));
8570
8571 if (section_nr == 0)
8572 break;
8573 if (section_nr >= dwp_file->num_sections)
8574 {
8575 error (_("Dwarf Error: bad DWP hash table, section number too large"
8576 " [in module %s]"),
8577 dwp_file->name);
8578 }
8579
8580 sectp = dwp_file->elf_sections[section_nr];
8581 if (! locate_virtual_dwo_sections (sectp, &sections))
8582 {
8583 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8584 " [in module %s]"),
8585 dwp_file->name);
8586 }
8587 }
8588
8589 if (i < 2
8590 || sections.info_or_types.asection == NULL
8591 || sections.abbrev.asection == NULL)
8592 {
8593 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8594 " [in module %s]"),
8595 dwp_file->name);
8596 }
8597 if (i == MAX_NR_DWO_SECTIONS)
8598 {
8599 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8600 " [in module %s]"),
8601 dwp_file->name);
8602 }
8603
8604 /* It's easier for the rest of the code if we fake a struct dwo_file and
8605 have dwo_unit "live" in that. At least for now.
8606
8607 The DWP file can be made up of a random collection of CUs and TUs.
8608 However, for each CU + set of TUs that came from the same original
8609 DWO file, we want combine them back into a virtual DWO file to save space
8610 (fewer struct dwo_file objects to allocated). Remember that for really
8611 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8612
8613 virtual_dwo_name =
8614 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8615 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8616 sections.line.asection ? sections.line.asection->id : 0,
8617 sections.loc.asection ? sections.loc.asection->id : 0,
8618 (sections.str_offsets.asection
8619 ? sections.str_offsets.asection->id
8620 : 0));
8621 make_cleanup (xfree, virtual_dwo_name);
8622 /* Can we use an existing virtual DWO file? */
8623 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8624 /* Create one if necessary. */
8625 if (*dwo_file_slot == NULL)
8626 {
8627 if (dwarf2_read_debug)
8628 {
8629 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8630 virtual_dwo_name);
8631 }
8632 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8633 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8634 virtual_dwo_name,
8635 strlen (virtual_dwo_name));
8636 dwo_file->sections.abbrev = sections.abbrev;
8637 dwo_file->sections.line = sections.line;
8638 dwo_file->sections.loc = sections.loc;
8639 dwo_file->sections.macinfo = sections.macinfo;
8640 dwo_file->sections.macro = sections.macro;
8641 dwo_file->sections.str_offsets = sections.str_offsets;
8642 /* The "str" section is global to the entire DWP file. */
8643 dwo_file->sections.str = dwp_file->sections.str;
8644 /* The info or types section is assigned later to dwo_unit,
8645 there's no need to record it in dwo_file.
8646 Also, we can't simply record type sections in dwo_file because
8647 we record a pointer into the vector in dwo_unit. As we collect more
8648 types we'll grow the vector and eventually have to reallocate space
8649 for it, invalidating all the pointers into the current copy. */
8650 *dwo_file_slot = dwo_file;
8651 }
8652 else
8653 {
8654 if (dwarf2_read_debug)
8655 {
8656 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8657 virtual_dwo_name);
8658 }
8659 dwo_file = *dwo_file_slot;
8660 }
8661 do_cleanups (cleanups);
8662
8663 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8664 dwo_unit->dwo_file = dwo_file;
8665 dwo_unit->signature = signature;
8666 dwo_unit->info_or_types_section =
8667 obstack_alloc (&objfile->objfile_obstack,
8668 sizeof (struct dwarf2_section_info));
8669 *dwo_unit->info_or_types_section = sections.info_or_types;
8670 /* offset, length, type_offset_in_tu are set later. */
8671
8672 return dwo_unit;
8673 }
8674
8675 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8676
8677 static struct dwo_unit *
8678 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8679 const struct dwp_hash_table *htab,
8680 ULONGEST signature, int is_debug_types)
8681 {
8682 bfd *dbfd = dwp_file->dbfd;
8683 uint32_t mask = htab->nr_slots - 1;
8684 uint32_t hash = signature & mask;
8685 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8686 unsigned int i;
8687 void **slot;
8688 struct dwo_unit find_dwo_cu, *dwo_cu;
8689
8690 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8691 find_dwo_cu.signature = signature;
8692 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8693
8694 if (*slot != NULL)
8695 return *slot;
8696
8697 /* Use a for loop so that we don't loop forever on bad debug info. */
8698 for (i = 0; i < htab->nr_slots; ++i)
8699 {
8700 ULONGEST signature_in_table;
8701
8702 signature_in_table =
8703 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8704 if (signature_in_table == signature)
8705 {
8706 uint32_t section_index =
8707 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8708
8709 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8710 signature, is_debug_types);
8711 return *slot;
8712 }
8713 if (signature_in_table == 0)
8714 return NULL;
8715 hash = (hash + hash2) & mask;
8716 }
8717
8718 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8719 " [in module %s]"),
8720 dwp_file->name);
8721 }
8722
8723 /* Subroutine of open_dwop_file to simplify it.
8724 Open the file specified by FILE_NAME and hand it off to BFD for
8725 preliminary analysis. Return a newly initialized bfd *, which
8726 includes a canonicalized copy of FILE_NAME.
8727 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8728 In case of trouble, return NULL.
8729 NOTE: This function is derived from symfile_bfd_open. */
8730
8731 static bfd *
8732 try_open_dwop_file (const char *file_name, int is_dwp)
8733 {
8734 bfd *sym_bfd;
8735 int desc, flags;
8736 char *absolute_name;
8737
8738 flags = OPF_TRY_CWD_FIRST;
8739 if (is_dwp)
8740 flags |= OPF_SEARCH_IN_PATH;
8741 desc = openp (debug_file_directory, flags, file_name,
8742 O_RDONLY | O_BINARY, &absolute_name);
8743 if (desc < 0)
8744 return NULL;
8745
8746 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8747 if (!sym_bfd)
8748 {
8749 xfree (absolute_name);
8750 return NULL;
8751 }
8752 xfree (absolute_name);
8753 bfd_set_cacheable (sym_bfd, 1);
8754
8755 if (!bfd_check_format (sym_bfd, bfd_object))
8756 {
8757 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8758 return NULL;
8759 }
8760
8761 return sym_bfd;
8762 }
8763
8764 /* Try to open DWO/DWP file FILE_NAME.
8765 COMP_DIR is the DW_AT_comp_dir attribute.
8766 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8767 The result is the bfd handle of the file.
8768 If there is a problem finding or opening the file, return NULL.
8769 Upon success, the canonicalized path of the file is stored in the bfd,
8770 same as symfile_bfd_open. */
8771
8772 static bfd *
8773 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8774 {
8775 bfd *abfd;
8776
8777 if (IS_ABSOLUTE_PATH (file_name))
8778 return try_open_dwop_file (file_name, is_dwp);
8779
8780 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8781
8782 if (comp_dir != NULL)
8783 {
8784 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8785
8786 /* NOTE: If comp_dir is a relative path, this will also try the
8787 search path, which seems useful. */
8788 abfd = try_open_dwop_file (path_to_try, is_dwp);
8789 xfree (path_to_try);
8790 if (abfd != NULL)
8791 return abfd;
8792 }
8793
8794 /* That didn't work, try debug-file-directory, which, despite its name,
8795 is a list of paths. */
8796
8797 if (*debug_file_directory == '\0')
8798 return NULL;
8799
8800 return try_open_dwop_file (file_name, is_dwp);
8801 }
8802
8803 /* This function is mapped across the sections and remembers the offset and
8804 size of each of the DWO debugging sections we are interested in. */
8805
8806 static void
8807 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8808 {
8809 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8810 const struct dwop_section_names *names = &dwop_section_names;
8811
8812 if (section_is_p (sectp->name, &names->abbrev_dwo))
8813 {
8814 dwo_sections->abbrev.asection = sectp;
8815 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8816 }
8817 else if (section_is_p (sectp->name, &names->info_dwo))
8818 {
8819 dwo_sections->info.asection = sectp;
8820 dwo_sections->info.size = bfd_get_section_size (sectp);
8821 }
8822 else if (section_is_p (sectp->name, &names->line_dwo))
8823 {
8824 dwo_sections->line.asection = sectp;
8825 dwo_sections->line.size = bfd_get_section_size (sectp);
8826 }
8827 else if (section_is_p (sectp->name, &names->loc_dwo))
8828 {
8829 dwo_sections->loc.asection = sectp;
8830 dwo_sections->loc.size = bfd_get_section_size (sectp);
8831 }
8832 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8833 {
8834 dwo_sections->macinfo.asection = sectp;
8835 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8836 }
8837 else if (section_is_p (sectp->name, &names->macro_dwo))
8838 {
8839 dwo_sections->macro.asection = sectp;
8840 dwo_sections->macro.size = bfd_get_section_size (sectp);
8841 }
8842 else if (section_is_p (sectp->name, &names->str_dwo))
8843 {
8844 dwo_sections->str.asection = sectp;
8845 dwo_sections->str.size = bfd_get_section_size (sectp);
8846 }
8847 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8848 {
8849 dwo_sections->str_offsets.asection = sectp;
8850 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8851 }
8852 else if (section_is_p (sectp->name, &names->types_dwo))
8853 {
8854 struct dwarf2_section_info type_section;
8855
8856 memset (&type_section, 0, sizeof (type_section));
8857 type_section.asection = sectp;
8858 type_section.size = bfd_get_section_size (sectp);
8859 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8860 &type_section);
8861 }
8862 }
8863
8864 /* Initialize the use of the DWO file specified by DWO_NAME.
8865 The result is NULL if DWO_NAME can't be found. */
8866
8867 static struct dwo_file *
8868 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8869 {
8870 struct objfile *objfile = dwarf2_per_objfile->objfile;
8871 struct dwo_file *dwo_file;
8872 bfd *dbfd;
8873 struct cleanup *cleanups;
8874
8875 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8876 if (dbfd == NULL)
8877 {
8878 if (dwarf2_read_debug)
8879 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8880 return NULL;
8881 }
8882 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8883 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8884 dwo_name, strlen (dwo_name));
8885 dwo_file->dbfd = dbfd;
8886
8887 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8888
8889 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8890
8891 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8892
8893 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8894 dwo_file->sections.types);
8895
8896 discard_cleanups (cleanups);
8897
8898 if (dwarf2_read_debug)
8899 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8900
8901 return dwo_file;
8902 }
8903
8904 /* This function is mapped across the sections and remembers the offset and
8905 size of each of the DWP debugging sections we are interested in. */
8906
8907 static void
8908 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8909 {
8910 struct dwp_file *dwp_file = dwp_file_ptr;
8911 const struct dwop_section_names *names = &dwop_section_names;
8912 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8913
8914 /* Record the ELF section number for later lookup: this is what the
8915 .debug_cu_index,.debug_tu_index tables use. */
8916 gdb_assert (elf_section_nr < dwp_file->num_sections);
8917 dwp_file->elf_sections[elf_section_nr] = sectp;
8918
8919 /* Look for specific sections that we need. */
8920 if (section_is_p (sectp->name, &names->str_dwo))
8921 {
8922 dwp_file->sections.str.asection = sectp;
8923 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8924 }
8925 else if (section_is_p (sectp->name, &names->cu_index))
8926 {
8927 dwp_file->sections.cu_index.asection = sectp;
8928 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8929 }
8930 else if (section_is_p (sectp->name, &names->tu_index))
8931 {
8932 dwp_file->sections.tu_index.asection = sectp;
8933 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8934 }
8935 }
8936
8937 /* Hash function for dwp_file loaded CUs/TUs. */
8938
8939 static hashval_t
8940 hash_dwp_loaded_cutus (const void *item)
8941 {
8942 const struct dwo_unit *dwo_unit = item;
8943
8944 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8945 return dwo_unit->signature;
8946 }
8947
8948 /* Equality function for dwp_file loaded CUs/TUs. */
8949
8950 static int
8951 eq_dwp_loaded_cutus (const void *a, const void *b)
8952 {
8953 const struct dwo_unit *dua = a;
8954 const struct dwo_unit *dub = b;
8955
8956 return dua->signature == dub->signature;
8957 }
8958
8959 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
8960
8961 static htab_t
8962 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8963 {
8964 return htab_create_alloc_ex (3,
8965 hash_dwp_loaded_cutus,
8966 eq_dwp_loaded_cutus,
8967 NULL,
8968 &objfile->objfile_obstack,
8969 hashtab_obstack_allocate,
8970 dummy_obstack_deallocate);
8971 }
8972
8973 /* Initialize the use of the DWP file for the current objfile.
8974 By convention the name of the DWP file is ${objfile}.dwp.
8975 The result is NULL if it can't be found. */
8976
8977 static struct dwp_file *
8978 open_and_init_dwp_file (const char *comp_dir)
8979 {
8980 struct objfile *objfile = dwarf2_per_objfile->objfile;
8981 struct dwp_file *dwp_file;
8982 char *dwp_name;
8983 bfd *dbfd;
8984 struct cleanup *cleanups;
8985
8986 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
8987 cleanups = make_cleanup (xfree, dwp_name);
8988
8989 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8990 if (dbfd == NULL)
8991 {
8992 if (dwarf2_read_debug)
8993 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8994 do_cleanups (cleanups);
8995 return NULL;
8996 }
8997 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8998 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8999 dwp_name, strlen (dwp_name));
9000 dwp_file->dbfd = dbfd;
9001 do_cleanups (cleanups);
9002
9003 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9004
9005 /* +1: section 0 is unused */
9006 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9007 dwp_file->elf_sections =
9008 OBSTACK_CALLOC (&objfile->objfile_obstack,
9009 dwp_file->num_sections, asection *);
9010
9011 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9012
9013 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9014
9015 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9016
9017 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9018
9019 discard_cleanups (cleanups);
9020
9021 if (dwarf2_read_debug)
9022 {
9023 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9024 fprintf_unfiltered (gdb_stdlog,
9025 " %u CUs, %u TUs\n",
9026 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9027 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9028 }
9029
9030 return dwp_file;
9031 }
9032
9033 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9034 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9035 or in the DWP file for the objfile, referenced by THIS_UNIT.
9036 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9037 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9038
9039 This is called, for example, when wanting to read a variable with a
9040 complex location. Therefore we don't want to do file i/o for every call.
9041 Therefore we don't want to look for a DWO file on every call.
9042 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9043 then we check if we've already seen DWO_NAME, and only THEN do we check
9044 for a DWO file.
9045
9046 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9047 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9048
9049 static struct dwo_unit *
9050 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9051 const char *dwo_name, const char *comp_dir,
9052 ULONGEST signature, int is_debug_types)
9053 {
9054 struct objfile *objfile = dwarf2_per_objfile->objfile;
9055 const char *kind = is_debug_types ? "TU" : "CU";
9056 void **dwo_file_slot;
9057 struct dwo_file *dwo_file;
9058 struct dwp_file *dwp_file;
9059
9060 /* Have we already read SIGNATURE from a DWP file? */
9061
9062 if (! dwarf2_per_objfile->dwp_checked)
9063 {
9064 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9065 dwarf2_per_objfile->dwp_checked = 1;
9066 }
9067 dwp_file = dwarf2_per_objfile->dwp_file;
9068
9069 if (dwp_file != NULL)
9070 {
9071 const struct dwp_hash_table *dwp_htab =
9072 is_debug_types ? dwp_file->tus : dwp_file->cus;
9073
9074 if (dwp_htab != NULL)
9075 {
9076 struct dwo_unit *dwo_cutu =
9077 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9078
9079 if (dwo_cutu != NULL)
9080 {
9081 if (dwarf2_read_debug)
9082 {
9083 fprintf_unfiltered (gdb_stdlog,
9084 "Virtual DWO %s %s found: @%s\n",
9085 kind, hex_string (signature),
9086 host_address_to_string (dwo_cutu));
9087 }
9088 return dwo_cutu;
9089 }
9090 }
9091 }
9092
9093 /* Have we already seen DWO_NAME? */
9094
9095 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9096 if (*dwo_file_slot == NULL)
9097 {
9098 /* Read in the file and build a table of the DWOs it contains. */
9099 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9100 }
9101 /* NOTE: This will be NULL if unable to open the file. */
9102 dwo_file = *dwo_file_slot;
9103
9104 if (dwo_file != NULL)
9105 {
9106 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9107
9108 if (htab != NULL)
9109 {
9110 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9111
9112 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9113 find_dwo_cutu.signature = signature;
9114 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9115
9116 if (dwo_cutu != NULL)
9117 {
9118 if (dwarf2_read_debug)
9119 {
9120 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9121 kind, dwo_name, hex_string (signature),
9122 host_address_to_string (dwo_cutu));
9123 }
9124 return dwo_cutu;
9125 }
9126 }
9127 }
9128
9129 /* We didn't find it. This could mean a dwo_id mismatch, or
9130 someone deleted the DWO/DWP file, or the search path isn't set up
9131 correctly to find the file. */
9132
9133 if (dwarf2_read_debug)
9134 {
9135 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9136 kind, dwo_name, hex_string (signature));
9137 }
9138
9139 complaint (&symfile_complaints,
9140 _("Could not find DWO CU referenced by CU at offset 0x%x"
9141 " [in module %s]"),
9142 this_unit->offset.sect_off, objfile->name);
9143 return NULL;
9144 }
9145
9146 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9147 See lookup_dwo_cutu_unit for details. */
9148
9149 static struct dwo_unit *
9150 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9151 const char *dwo_name, const char *comp_dir,
9152 ULONGEST signature)
9153 {
9154 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9155 }
9156
9157 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9158 See lookup_dwo_cutu_unit for details. */
9159
9160 static struct dwo_unit *
9161 lookup_dwo_type_unit (struct signatured_type *this_tu,
9162 const char *dwo_name, const char *comp_dir)
9163 {
9164 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9165 }
9166
9167 /* Free all resources associated with DWO_FILE.
9168 Close the DWO file and munmap the sections.
9169 All memory should be on the objfile obstack. */
9170
9171 static void
9172 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9173 {
9174 int ix;
9175 struct dwarf2_section_info *section;
9176
9177 gdb_assert (dwo_file->dbfd != objfile->obfd);
9178 gdb_bfd_unref (dwo_file->dbfd);
9179
9180 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9181 }
9182
9183 /* Wrapper for free_dwo_file for use in cleanups. */
9184
9185 static void
9186 free_dwo_file_cleanup (void *arg)
9187 {
9188 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9189 struct objfile *objfile = dwarf2_per_objfile->objfile;
9190
9191 free_dwo_file (dwo_file, objfile);
9192 }
9193
9194 /* Traversal function for free_dwo_files. */
9195
9196 static int
9197 free_dwo_file_from_slot (void **slot, void *info)
9198 {
9199 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9200 struct objfile *objfile = (struct objfile *) info;
9201
9202 free_dwo_file (dwo_file, objfile);
9203
9204 return 1;
9205 }
9206
9207 /* Free all resources associated with DWO_FILES. */
9208
9209 static void
9210 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9211 {
9212 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9213 }
9214 \f
9215 /* Read in various DIEs. */
9216
9217 /* qsort helper for inherit_abstract_dies. */
9218
9219 static int
9220 unsigned_int_compar (const void *ap, const void *bp)
9221 {
9222 unsigned int a = *(unsigned int *) ap;
9223 unsigned int b = *(unsigned int *) bp;
9224
9225 return (a > b) - (b > a);
9226 }
9227
9228 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9229 Inherit only the children of the DW_AT_abstract_origin DIE not being
9230 already referenced by DW_AT_abstract_origin from the children of the
9231 current DIE. */
9232
9233 static void
9234 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9235 {
9236 struct die_info *child_die;
9237 unsigned die_children_count;
9238 /* CU offsets which were referenced by children of the current DIE. */
9239 sect_offset *offsets;
9240 sect_offset *offsets_end, *offsetp;
9241 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9242 struct die_info *origin_die;
9243 /* Iterator of the ORIGIN_DIE children. */
9244 struct die_info *origin_child_die;
9245 struct cleanup *cleanups;
9246 struct attribute *attr;
9247 struct dwarf2_cu *origin_cu;
9248 struct pending **origin_previous_list_in_scope;
9249
9250 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9251 if (!attr)
9252 return;
9253
9254 /* Note that following die references may follow to a die in a
9255 different cu. */
9256
9257 origin_cu = cu;
9258 origin_die = follow_die_ref (die, attr, &origin_cu);
9259
9260 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9261 symbols in. */
9262 origin_previous_list_in_scope = origin_cu->list_in_scope;
9263 origin_cu->list_in_scope = cu->list_in_scope;
9264
9265 if (die->tag != origin_die->tag
9266 && !(die->tag == DW_TAG_inlined_subroutine
9267 && origin_die->tag == DW_TAG_subprogram))
9268 complaint (&symfile_complaints,
9269 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9270 die->offset.sect_off, origin_die->offset.sect_off);
9271
9272 child_die = die->child;
9273 die_children_count = 0;
9274 while (child_die && child_die->tag)
9275 {
9276 child_die = sibling_die (child_die);
9277 die_children_count++;
9278 }
9279 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9280 cleanups = make_cleanup (xfree, offsets);
9281
9282 offsets_end = offsets;
9283 child_die = die->child;
9284 while (child_die && child_die->tag)
9285 {
9286 /* For each CHILD_DIE, find the corresponding child of
9287 ORIGIN_DIE. If there is more than one layer of
9288 DW_AT_abstract_origin, follow them all; there shouldn't be,
9289 but GCC versions at least through 4.4 generate this (GCC PR
9290 40573). */
9291 struct die_info *child_origin_die = child_die;
9292 struct dwarf2_cu *child_origin_cu = cu;
9293
9294 while (1)
9295 {
9296 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9297 child_origin_cu);
9298 if (attr == NULL)
9299 break;
9300 child_origin_die = follow_die_ref (child_origin_die, attr,
9301 &child_origin_cu);
9302 }
9303
9304 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9305 counterpart may exist. */
9306 if (child_origin_die != child_die)
9307 {
9308 if (child_die->tag != child_origin_die->tag
9309 && !(child_die->tag == DW_TAG_inlined_subroutine
9310 && child_origin_die->tag == DW_TAG_subprogram))
9311 complaint (&symfile_complaints,
9312 _("Child DIE 0x%x and its abstract origin 0x%x have "
9313 "different tags"), child_die->offset.sect_off,
9314 child_origin_die->offset.sect_off);
9315 if (child_origin_die->parent != origin_die)
9316 complaint (&symfile_complaints,
9317 _("Child DIE 0x%x and its abstract origin 0x%x have "
9318 "different parents"), child_die->offset.sect_off,
9319 child_origin_die->offset.sect_off);
9320 else
9321 *offsets_end++ = child_origin_die->offset;
9322 }
9323 child_die = sibling_die (child_die);
9324 }
9325 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9326 unsigned_int_compar);
9327 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9328 if (offsetp[-1].sect_off == offsetp->sect_off)
9329 complaint (&symfile_complaints,
9330 _("Multiple children of DIE 0x%x refer "
9331 "to DIE 0x%x as their abstract origin"),
9332 die->offset.sect_off, offsetp->sect_off);
9333
9334 offsetp = offsets;
9335 origin_child_die = origin_die->child;
9336 while (origin_child_die && origin_child_die->tag)
9337 {
9338 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9339 while (offsetp < offsets_end
9340 && offsetp->sect_off < origin_child_die->offset.sect_off)
9341 offsetp++;
9342 if (offsetp >= offsets_end
9343 || offsetp->sect_off > origin_child_die->offset.sect_off)
9344 {
9345 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9346 process_die (origin_child_die, origin_cu);
9347 }
9348 origin_child_die = sibling_die (origin_child_die);
9349 }
9350 origin_cu->list_in_scope = origin_previous_list_in_scope;
9351
9352 do_cleanups (cleanups);
9353 }
9354
9355 static void
9356 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9357 {
9358 struct objfile *objfile = cu->objfile;
9359 struct context_stack *new;
9360 CORE_ADDR lowpc;
9361 CORE_ADDR highpc;
9362 struct die_info *child_die;
9363 struct attribute *attr, *call_line, *call_file;
9364 char *name;
9365 CORE_ADDR baseaddr;
9366 struct block *block;
9367 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9368 VEC (symbolp) *template_args = NULL;
9369 struct template_symbol *templ_func = NULL;
9370
9371 if (inlined_func)
9372 {
9373 /* If we do not have call site information, we can't show the
9374 caller of this inlined function. That's too confusing, so
9375 only use the scope for local variables. */
9376 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9377 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9378 if (call_line == NULL || call_file == NULL)
9379 {
9380 read_lexical_block_scope (die, cu);
9381 return;
9382 }
9383 }
9384
9385 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9386
9387 name = dwarf2_name (die, cu);
9388
9389 /* Ignore functions with missing or empty names. These are actually
9390 illegal according to the DWARF standard. */
9391 if (name == NULL)
9392 {
9393 complaint (&symfile_complaints,
9394 _("missing name for subprogram DIE at %d"),
9395 die->offset.sect_off);
9396 return;
9397 }
9398
9399 /* Ignore functions with missing or invalid low and high pc attributes. */
9400 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9401 {
9402 attr = dwarf2_attr (die, DW_AT_external, cu);
9403 if (!attr || !DW_UNSND (attr))
9404 complaint (&symfile_complaints,
9405 _("cannot get low and high bounds "
9406 "for subprogram DIE at %d"),
9407 die->offset.sect_off);
9408 return;
9409 }
9410
9411 lowpc += baseaddr;
9412 highpc += baseaddr;
9413
9414 /* If we have any template arguments, then we must allocate a
9415 different sort of symbol. */
9416 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9417 {
9418 if (child_die->tag == DW_TAG_template_type_param
9419 || child_die->tag == DW_TAG_template_value_param)
9420 {
9421 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9422 struct template_symbol);
9423 templ_func->base.is_cplus_template_function = 1;
9424 break;
9425 }
9426 }
9427
9428 new = push_context (0, lowpc);
9429 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9430 (struct symbol *) templ_func);
9431
9432 /* If there is a location expression for DW_AT_frame_base, record
9433 it. */
9434 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9435 if (attr)
9436 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9437 expression is being recorded directly in the function's symbol
9438 and not in a separate frame-base object. I guess this hack is
9439 to avoid adding some sort of frame-base adjunct/annex to the
9440 function's symbol :-(. The problem with doing this is that it
9441 results in a function symbol with a location expression that
9442 has nothing to do with the location of the function, ouch! The
9443 relationship should be: a function's symbol has-a frame base; a
9444 frame-base has-a location expression. */
9445 dwarf2_symbol_mark_computed (attr, new->name, cu);
9446
9447 cu->list_in_scope = &local_symbols;
9448
9449 if (die->child != NULL)
9450 {
9451 child_die = die->child;
9452 while (child_die && child_die->tag)
9453 {
9454 if (child_die->tag == DW_TAG_template_type_param
9455 || child_die->tag == DW_TAG_template_value_param)
9456 {
9457 struct symbol *arg = new_symbol (child_die, NULL, cu);
9458
9459 if (arg != NULL)
9460 VEC_safe_push (symbolp, template_args, arg);
9461 }
9462 else
9463 process_die (child_die, cu);
9464 child_die = sibling_die (child_die);
9465 }
9466 }
9467
9468 inherit_abstract_dies (die, cu);
9469
9470 /* If we have a DW_AT_specification, we might need to import using
9471 directives from the context of the specification DIE. See the
9472 comment in determine_prefix. */
9473 if (cu->language == language_cplus
9474 && dwarf2_attr (die, DW_AT_specification, cu))
9475 {
9476 struct dwarf2_cu *spec_cu = cu;
9477 struct die_info *spec_die = die_specification (die, &spec_cu);
9478
9479 while (spec_die)
9480 {
9481 child_die = spec_die->child;
9482 while (child_die && child_die->tag)
9483 {
9484 if (child_die->tag == DW_TAG_imported_module)
9485 process_die (child_die, spec_cu);
9486 child_die = sibling_die (child_die);
9487 }
9488
9489 /* In some cases, GCC generates specification DIEs that
9490 themselves contain DW_AT_specification attributes. */
9491 spec_die = die_specification (spec_die, &spec_cu);
9492 }
9493 }
9494
9495 new = pop_context ();
9496 /* Make a block for the local symbols within. */
9497 block = finish_block (new->name, &local_symbols, new->old_blocks,
9498 lowpc, highpc, objfile);
9499
9500 /* For C++, set the block's scope. */
9501 if (cu->language == language_cplus || cu->language == language_fortran)
9502 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
9503 determine_prefix (die, cu),
9504 processing_has_namespace_info);
9505
9506 /* If we have address ranges, record them. */
9507 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9508
9509 /* Attach template arguments to function. */
9510 if (! VEC_empty (symbolp, template_args))
9511 {
9512 gdb_assert (templ_func != NULL);
9513
9514 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9515 templ_func->template_arguments
9516 = obstack_alloc (&objfile->objfile_obstack,
9517 (templ_func->n_template_arguments
9518 * sizeof (struct symbol *)));
9519 memcpy (templ_func->template_arguments,
9520 VEC_address (symbolp, template_args),
9521 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9522 VEC_free (symbolp, template_args);
9523 }
9524
9525 /* In C++, we can have functions nested inside functions (e.g., when
9526 a function declares a class that has methods). This means that
9527 when we finish processing a function scope, we may need to go
9528 back to building a containing block's symbol lists. */
9529 local_symbols = new->locals;
9530 using_directives = new->using_directives;
9531
9532 /* If we've finished processing a top-level function, subsequent
9533 symbols go in the file symbol list. */
9534 if (outermost_context_p ())
9535 cu->list_in_scope = &file_symbols;
9536 }
9537
9538 /* Process all the DIES contained within a lexical block scope. Start
9539 a new scope, process the dies, and then close the scope. */
9540
9541 static void
9542 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9543 {
9544 struct objfile *objfile = cu->objfile;
9545 struct context_stack *new;
9546 CORE_ADDR lowpc, highpc;
9547 struct die_info *child_die;
9548 CORE_ADDR baseaddr;
9549
9550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9551
9552 /* Ignore blocks with missing or invalid low and high pc attributes. */
9553 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9554 as multiple lexical blocks? Handling children in a sane way would
9555 be nasty. Might be easier to properly extend generic blocks to
9556 describe ranges. */
9557 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9558 return;
9559 lowpc += baseaddr;
9560 highpc += baseaddr;
9561
9562 push_context (0, lowpc);
9563 if (die->child != NULL)
9564 {
9565 child_die = die->child;
9566 while (child_die && child_die->tag)
9567 {
9568 process_die (child_die, cu);
9569 child_die = sibling_die (child_die);
9570 }
9571 }
9572 new = pop_context ();
9573
9574 if (local_symbols != NULL || using_directives != NULL)
9575 {
9576 struct block *block
9577 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9578 highpc, objfile);
9579
9580 /* Note that recording ranges after traversing children, as we
9581 do here, means that recording a parent's ranges entails
9582 walking across all its children's ranges as they appear in
9583 the address map, which is quadratic behavior.
9584
9585 It would be nicer to record the parent's ranges before
9586 traversing its children, simply overriding whatever you find
9587 there. But since we don't even decide whether to create a
9588 block until after we've traversed its children, that's hard
9589 to do. */
9590 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9591 }
9592 local_symbols = new->locals;
9593 using_directives = new->using_directives;
9594 }
9595
9596 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9597
9598 static void
9599 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9600 {
9601 struct objfile *objfile = cu->objfile;
9602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9603 CORE_ADDR pc, baseaddr;
9604 struct attribute *attr;
9605 struct call_site *call_site, call_site_local;
9606 void **slot;
9607 int nparams;
9608 struct die_info *child_die;
9609
9610 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9611
9612 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9613 if (!attr)
9614 {
9615 complaint (&symfile_complaints,
9616 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9617 "DIE 0x%x [in module %s]"),
9618 die->offset.sect_off, objfile->name);
9619 return;
9620 }
9621 pc = DW_ADDR (attr) + baseaddr;
9622
9623 if (cu->call_site_htab == NULL)
9624 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9625 NULL, &objfile->objfile_obstack,
9626 hashtab_obstack_allocate, NULL);
9627 call_site_local.pc = pc;
9628 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9629 if (*slot != NULL)
9630 {
9631 complaint (&symfile_complaints,
9632 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9633 "DIE 0x%x [in module %s]"),
9634 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9635 return;
9636 }
9637
9638 /* Count parameters at the caller. */
9639
9640 nparams = 0;
9641 for (child_die = die->child; child_die && child_die->tag;
9642 child_die = sibling_die (child_die))
9643 {
9644 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9645 {
9646 complaint (&symfile_complaints,
9647 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9648 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9649 child_die->tag, child_die->offset.sect_off, objfile->name);
9650 continue;
9651 }
9652
9653 nparams++;
9654 }
9655
9656 call_site = obstack_alloc (&objfile->objfile_obstack,
9657 (sizeof (*call_site)
9658 + (sizeof (*call_site->parameter)
9659 * (nparams - 1))));
9660 *slot = call_site;
9661 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9662 call_site->pc = pc;
9663
9664 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9665 {
9666 struct die_info *func_die;
9667
9668 /* Skip also over DW_TAG_inlined_subroutine. */
9669 for (func_die = die->parent;
9670 func_die && func_die->tag != DW_TAG_subprogram
9671 && func_die->tag != DW_TAG_subroutine_type;
9672 func_die = func_die->parent);
9673
9674 /* DW_AT_GNU_all_call_sites is a superset
9675 of DW_AT_GNU_all_tail_call_sites. */
9676 if (func_die
9677 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9678 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9679 {
9680 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9681 not complete. But keep CALL_SITE for look ups via call_site_htab,
9682 both the initial caller containing the real return address PC and
9683 the final callee containing the current PC of a chain of tail
9684 calls do not need to have the tail call list complete. But any
9685 function candidate for a virtual tail call frame searched via
9686 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9687 determined unambiguously. */
9688 }
9689 else
9690 {
9691 struct type *func_type = NULL;
9692
9693 if (func_die)
9694 func_type = get_die_type (func_die, cu);
9695 if (func_type != NULL)
9696 {
9697 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9698
9699 /* Enlist this call site to the function. */
9700 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9701 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9702 }
9703 else
9704 complaint (&symfile_complaints,
9705 _("Cannot find function owning DW_TAG_GNU_call_site "
9706 "DIE 0x%x [in module %s]"),
9707 die->offset.sect_off, objfile->name);
9708 }
9709 }
9710
9711 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9712 if (attr == NULL)
9713 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9714 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9715 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9716 /* Keep NULL DWARF_BLOCK. */;
9717 else if (attr_form_is_block (attr))
9718 {
9719 struct dwarf2_locexpr_baton *dlbaton;
9720
9721 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9722 dlbaton->data = DW_BLOCK (attr)->data;
9723 dlbaton->size = DW_BLOCK (attr)->size;
9724 dlbaton->per_cu = cu->per_cu;
9725
9726 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9727 }
9728 else if (is_ref_attr (attr))
9729 {
9730 struct dwarf2_cu *target_cu = cu;
9731 struct die_info *target_die;
9732
9733 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9734 gdb_assert (target_cu->objfile == objfile);
9735 if (die_is_declaration (target_die, target_cu))
9736 {
9737 const char *target_physname;
9738
9739 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9740 if (target_physname == NULL)
9741 complaint (&symfile_complaints,
9742 _("DW_AT_GNU_call_site_target target DIE has invalid "
9743 "physname, for referencing DIE 0x%x [in module %s]"),
9744 die->offset.sect_off, objfile->name);
9745 else
9746 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9747 }
9748 else
9749 {
9750 CORE_ADDR lowpc;
9751
9752 /* DW_AT_entry_pc should be preferred. */
9753 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9754 complaint (&symfile_complaints,
9755 _("DW_AT_GNU_call_site_target target DIE has invalid "
9756 "low pc, for referencing DIE 0x%x [in module %s]"),
9757 die->offset.sect_off, objfile->name);
9758 else
9759 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9760 }
9761 }
9762 else
9763 complaint (&symfile_complaints,
9764 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9765 "block nor reference, for DIE 0x%x [in module %s]"),
9766 die->offset.sect_off, objfile->name);
9767
9768 call_site->per_cu = cu->per_cu;
9769
9770 for (child_die = die->child;
9771 child_die && child_die->tag;
9772 child_die = sibling_die (child_die))
9773 {
9774 struct call_site_parameter *parameter;
9775 struct attribute *loc, *origin;
9776
9777 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9778 {
9779 /* Already printed the complaint above. */
9780 continue;
9781 }
9782
9783 gdb_assert (call_site->parameter_count < nparams);
9784 parameter = &call_site->parameter[call_site->parameter_count];
9785
9786 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9787 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9788 register is contained in DW_AT_GNU_call_site_value. */
9789
9790 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9791 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9792 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9793 {
9794 sect_offset offset;
9795
9796 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9797 offset = dwarf2_get_ref_die_offset (origin);
9798 if (!offset_in_cu_p (&cu->header, offset))
9799 {
9800 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9801 binding can be done only inside one CU. Such referenced DIE
9802 therefore cannot be even moved to DW_TAG_partial_unit. */
9803 complaint (&symfile_complaints,
9804 _("DW_AT_abstract_origin offset is not in CU for "
9805 "DW_TAG_GNU_call_site child DIE 0x%x "
9806 "[in module %s]"),
9807 child_die->offset.sect_off, objfile->name);
9808 continue;
9809 }
9810 parameter->u.param_offset.cu_off = (offset.sect_off
9811 - cu->header.offset.sect_off);
9812 }
9813 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9814 {
9815 complaint (&symfile_complaints,
9816 _("No DW_FORM_block* DW_AT_location for "
9817 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9818 child_die->offset.sect_off, objfile->name);
9819 continue;
9820 }
9821 else
9822 {
9823 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9824 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9825 if (parameter->u.dwarf_reg != -1)
9826 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9827 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9828 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9829 &parameter->u.fb_offset))
9830 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9831 else
9832 {
9833 complaint (&symfile_complaints,
9834 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9835 "for DW_FORM_block* DW_AT_location is supported for "
9836 "DW_TAG_GNU_call_site child DIE 0x%x "
9837 "[in module %s]"),
9838 child_die->offset.sect_off, objfile->name);
9839 continue;
9840 }
9841 }
9842
9843 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9844 if (!attr_form_is_block (attr))
9845 {
9846 complaint (&symfile_complaints,
9847 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9848 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9849 child_die->offset.sect_off, objfile->name);
9850 continue;
9851 }
9852 parameter->value = DW_BLOCK (attr)->data;
9853 parameter->value_size = DW_BLOCK (attr)->size;
9854
9855 /* Parameters are not pre-cleared by memset above. */
9856 parameter->data_value = NULL;
9857 parameter->data_value_size = 0;
9858 call_site->parameter_count++;
9859
9860 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9861 if (attr)
9862 {
9863 if (!attr_form_is_block (attr))
9864 complaint (&symfile_complaints,
9865 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9866 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9867 child_die->offset.sect_off, objfile->name);
9868 else
9869 {
9870 parameter->data_value = DW_BLOCK (attr)->data;
9871 parameter->data_value_size = DW_BLOCK (attr)->size;
9872 }
9873 }
9874 }
9875 }
9876
9877 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9878 Return 1 if the attributes are present and valid, otherwise, return 0.
9879 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9880
9881 static int
9882 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9883 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9884 struct partial_symtab *ranges_pst)
9885 {
9886 struct objfile *objfile = cu->objfile;
9887 struct comp_unit_head *cu_header = &cu->header;
9888 bfd *obfd = objfile->obfd;
9889 unsigned int addr_size = cu_header->addr_size;
9890 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9891 /* Base address selection entry. */
9892 CORE_ADDR base;
9893 int found_base;
9894 unsigned int dummy;
9895 gdb_byte *buffer;
9896 CORE_ADDR marker;
9897 int low_set;
9898 CORE_ADDR low = 0;
9899 CORE_ADDR high = 0;
9900 CORE_ADDR baseaddr;
9901
9902 found_base = cu->base_known;
9903 base = cu->base_address;
9904
9905 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9906 if (offset >= dwarf2_per_objfile->ranges.size)
9907 {
9908 complaint (&symfile_complaints,
9909 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9910 offset);
9911 return 0;
9912 }
9913 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9914
9915 /* Read in the largest possible address. */
9916 marker = read_address (obfd, buffer, cu, &dummy);
9917 if ((marker & mask) == mask)
9918 {
9919 /* If we found the largest possible address, then
9920 read the base address. */
9921 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9922 buffer += 2 * addr_size;
9923 offset += 2 * addr_size;
9924 found_base = 1;
9925 }
9926
9927 low_set = 0;
9928
9929 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9930
9931 while (1)
9932 {
9933 CORE_ADDR range_beginning, range_end;
9934
9935 range_beginning = read_address (obfd, buffer, cu, &dummy);
9936 buffer += addr_size;
9937 range_end = read_address (obfd, buffer, cu, &dummy);
9938 buffer += addr_size;
9939 offset += 2 * addr_size;
9940
9941 /* An end of list marker is a pair of zero addresses. */
9942 if (range_beginning == 0 && range_end == 0)
9943 /* Found the end of list entry. */
9944 break;
9945
9946 /* Each base address selection entry is a pair of 2 values.
9947 The first is the largest possible address, the second is
9948 the base address. Check for a base address here. */
9949 if ((range_beginning & mask) == mask)
9950 {
9951 /* If we found the largest possible address, then
9952 read the base address. */
9953 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9954 found_base = 1;
9955 continue;
9956 }
9957
9958 if (!found_base)
9959 {
9960 /* We have no valid base address for the ranges
9961 data. */
9962 complaint (&symfile_complaints,
9963 _("Invalid .debug_ranges data (no base address)"));
9964 return 0;
9965 }
9966
9967 if (range_beginning > range_end)
9968 {
9969 /* Inverted range entries are invalid. */
9970 complaint (&symfile_complaints,
9971 _("Invalid .debug_ranges data (inverted range)"));
9972 return 0;
9973 }
9974
9975 /* Empty range entries have no effect. */
9976 if (range_beginning == range_end)
9977 continue;
9978
9979 range_beginning += base;
9980 range_end += base;
9981
9982 /* A not-uncommon case of bad debug info.
9983 Don't pollute the addrmap with bad data. */
9984 if (range_beginning + baseaddr == 0
9985 && !dwarf2_per_objfile->has_section_at_zero)
9986 {
9987 complaint (&symfile_complaints,
9988 _(".debug_ranges entry has start address of zero"
9989 " [in module %s]"), objfile->name);
9990 continue;
9991 }
9992
9993 if (ranges_pst != NULL)
9994 addrmap_set_empty (objfile->psymtabs_addrmap,
9995 range_beginning + baseaddr,
9996 range_end - 1 + baseaddr,
9997 ranges_pst);
9998
9999 /* FIXME: This is recording everything as a low-high
10000 segment of consecutive addresses. We should have a
10001 data structure for discontiguous block ranges
10002 instead. */
10003 if (! low_set)
10004 {
10005 low = range_beginning;
10006 high = range_end;
10007 low_set = 1;
10008 }
10009 else
10010 {
10011 if (range_beginning < low)
10012 low = range_beginning;
10013 if (range_end > high)
10014 high = range_end;
10015 }
10016 }
10017
10018 if (! low_set)
10019 /* If the first entry is an end-of-list marker, the range
10020 describes an empty scope, i.e. no instructions. */
10021 return 0;
10022
10023 if (low_return)
10024 *low_return = low;
10025 if (high_return)
10026 *high_return = high;
10027 return 1;
10028 }
10029
10030 /* Get low and high pc attributes from a die. Return 1 if the attributes
10031 are present and valid, otherwise, return 0. Return -1 if the range is
10032 discontinuous, i.e. derived from DW_AT_ranges information. */
10033
10034 static int
10035 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10036 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10037 struct partial_symtab *pst)
10038 {
10039 struct attribute *attr;
10040 struct attribute *attr_high;
10041 CORE_ADDR low = 0;
10042 CORE_ADDR high = 0;
10043 int ret = 0;
10044
10045 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10046 if (attr_high)
10047 {
10048 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10049 if (attr)
10050 {
10051 low = DW_ADDR (attr);
10052 if (attr_high->form == DW_FORM_addr
10053 || attr_high->form == DW_FORM_GNU_addr_index)
10054 high = DW_ADDR (attr_high);
10055 else
10056 high = low + DW_UNSND (attr_high);
10057 }
10058 else
10059 /* Found high w/o low attribute. */
10060 return 0;
10061
10062 /* Found consecutive range of addresses. */
10063 ret = 1;
10064 }
10065 else
10066 {
10067 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10068 if (attr != NULL)
10069 {
10070 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10071
10072 /* Value of the DW_AT_ranges attribute is the offset in the
10073 .debug_ranges section. */
10074 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10075 return 0;
10076 /* Found discontinuous range of addresses. */
10077 ret = -1;
10078 }
10079 }
10080
10081 /* read_partial_die has also the strict LOW < HIGH requirement. */
10082 if (high <= low)
10083 return 0;
10084
10085 /* When using the GNU linker, .gnu.linkonce. sections are used to
10086 eliminate duplicate copies of functions and vtables and such.
10087 The linker will arbitrarily choose one and discard the others.
10088 The AT_*_pc values for such functions refer to local labels in
10089 these sections. If the section from that file was discarded, the
10090 labels are not in the output, so the relocs get a value of 0.
10091 If this is a discarded function, mark the pc bounds as invalid,
10092 so that GDB will ignore it. */
10093 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10094 return 0;
10095
10096 *lowpc = low;
10097 if (highpc)
10098 *highpc = high;
10099 return ret;
10100 }
10101
10102 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10103 its low and high PC addresses. Do nothing if these addresses could not
10104 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10105 and HIGHPC to the high address if greater than HIGHPC. */
10106
10107 static void
10108 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10109 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10110 struct dwarf2_cu *cu)
10111 {
10112 CORE_ADDR low, high;
10113 struct die_info *child = die->child;
10114
10115 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10116 {
10117 *lowpc = min (*lowpc, low);
10118 *highpc = max (*highpc, high);
10119 }
10120
10121 /* If the language does not allow nested subprograms (either inside
10122 subprograms or lexical blocks), we're done. */
10123 if (cu->language != language_ada)
10124 return;
10125
10126 /* Check all the children of the given DIE. If it contains nested
10127 subprograms, then check their pc bounds. Likewise, we need to
10128 check lexical blocks as well, as they may also contain subprogram
10129 definitions. */
10130 while (child && child->tag)
10131 {
10132 if (child->tag == DW_TAG_subprogram
10133 || child->tag == DW_TAG_lexical_block)
10134 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10135 child = sibling_die (child);
10136 }
10137 }
10138
10139 /* Get the low and high pc's represented by the scope DIE, and store
10140 them in *LOWPC and *HIGHPC. If the correct values can't be
10141 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10142
10143 static void
10144 get_scope_pc_bounds (struct die_info *die,
10145 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10146 struct dwarf2_cu *cu)
10147 {
10148 CORE_ADDR best_low = (CORE_ADDR) -1;
10149 CORE_ADDR best_high = (CORE_ADDR) 0;
10150 CORE_ADDR current_low, current_high;
10151
10152 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10153 {
10154 best_low = current_low;
10155 best_high = current_high;
10156 }
10157 else
10158 {
10159 struct die_info *child = die->child;
10160
10161 while (child && child->tag)
10162 {
10163 switch (child->tag) {
10164 case DW_TAG_subprogram:
10165 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10166 break;
10167 case DW_TAG_namespace:
10168 case DW_TAG_module:
10169 /* FIXME: carlton/2004-01-16: Should we do this for
10170 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10171 that current GCC's always emit the DIEs corresponding
10172 to definitions of methods of classes as children of a
10173 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10174 the DIEs giving the declarations, which could be
10175 anywhere). But I don't see any reason why the
10176 standards says that they have to be there. */
10177 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10178
10179 if (current_low != ((CORE_ADDR) -1))
10180 {
10181 best_low = min (best_low, current_low);
10182 best_high = max (best_high, current_high);
10183 }
10184 break;
10185 default:
10186 /* Ignore. */
10187 break;
10188 }
10189
10190 child = sibling_die (child);
10191 }
10192 }
10193
10194 *lowpc = best_low;
10195 *highpc = best_high;
10196 }
10197
10198 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10199 in DIE. */
10200
10201 static void
10202 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10203 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10204 {
10205 struct objfile *objfile = cu->objfile;
10206 struct attribute *attr;
10207 struct attribute *attr_high;
10208
10209 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10210 if (attr_high)
10211 {
10212 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10213 if (attr)
10214 {
10215 CORE_ADDR low = DW_ADDR (attr);
10216 CORE_ADDR high;
10217 if (attr_high->form == DW_FORM_addr
10218 || attr_high->form == DW_FORM_GNU_addr_index)
10219 high = DW_ADDR (attr_high);
10220 else
10221 high = low + DW_UNSND (attr_high);
10222
10223 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10224 }
10225 }
10226
10227 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10228 if (attr)
10229 {
10230 bfd *obfd = objfile->obfd;
10231
10232 /* The value of the DW_AT_ranges attribute is the offset of the
10233 address range list in the .debug_ranges section. */
10234 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
10235 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10236
10237 /* For some target architectures, but not others, the
10238 read_address function sign-extends the addresses it returns.
10239 To recognize base address selection entries, we need a
10240 mask. */
10241 unsigned int addr_size = cu->header.addr_size;
10242 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10243
10244 /* The base address, to which the next pair is relative. Note
10245 that this 'base' is a DWARF concept: most entries in a range
10246 list are relative, to reduce the number of relocs against the
10247 debugging information. This is separate from this function's
10248 'baseaddr' argument, which GDB uses to relocate debugging
10249 information from a shared library based on the address at
10250 which the library was loaded. */
10251 CORE_ADDR base = cu->base_address;
10252 int base_known = cu->base_known;
10253
10254 gdb_assert (dwarf2_per_objfile->ranges.readin);
10255 if (offset >= dwarf2_per_objfile->ranges.size)
10256 {
10257 complaint (&symfile_complaints,
10258 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10259 offset);
10260 return;
10261 }
10262
10263 for (;;)
10264 {
10265 unsigned int bytes_read;
10266 CORE_ADDR start, end;
10267
10268 start = read_address (obfd, buffer, cu, &bytes_read);
10269 buffer += bytes_read;
10270 end = read_address (obfd, buffer, cu, &bytes_read);
10271 buffer += bytes_read;
10272
10273 /* Did we find the end of the range list? */
10274 if (start == 0 && end == 0)
10275 break;
10276
10277 /* Did we find a base address selection entry? */
10278 else if ((start & base_select_mask) == base_select_mask)
10279 {
10280 base = end;
10281 base_known = 1;
10282 }
10283
10284 /* We found an ordinary address range. */
10285 else
10286 {
10287 if (!base_known)
10288 {
10289 complaint (&symfile_complaints,
10290 _("Invalid .debug_ranges data "
10291 "(no base address)"));
10292 return;
10293 }
10294
10295 if (start > end)
10296 {
10297 /* Inverted range entries are invalid. */
10298 complaint (&symfile_complaints,
10299 _("Invalid .debug_ranges data "
10300 "(inverted range)"));
10301 return;
10302 }
10303
10304 /* Empty range entries have no effect. */
10305 if (start == end)
10306 continue;
10307
10308 start += base + baseaddr;
10309 end += base + baseaddr;
10310
10311 /* A not-uncommon case of bad debug info.
10312 Don't pollute the addrmap with bad data. */
10313 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10314 {
10315 complaint (&symfile_complaints,
10316 _(".debug_ranges entry has start address of zero"
10317 " [in module %s]"), objfile->name);
10318 continue;
10319 }
10320
10321 record_block_range (block, start, end - 1);
10322 }
10323 }
10324 }
10325 }
10326
10327 /* Check whether the producer field indicates either of GCC < 4.6, or the
10328 Intel C/C++ compiler, and cache the result in CU. */
10329
10330 static void
10331 check_producer (struct dwarf2_cu *cu)
10332 {
10333 const char *cs;
10334 int major, minor, release;
10335
10336 if (cu->producer == NULL)
10337 {
10338 /* For unknown compilers expect their behavior is DWARF version
10339 compliant.
10340
10341 GCC started to support .debug_types sections by -gdwarf-4 since
10342 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10343 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10344 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10345 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10346 }
10347 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10348 {
10349 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10350
10351 cs = &cu->producer[strlen ("GNU ")];
10352 while (*cs && !isdigit (*cs))
10353 cs++;
10354 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10355 {
10356 /* Not recognized as GCC. */
10357 }
10358 else
10359 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10360 }
10361 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10362 cu->producer_is_icc = 1;
10363 else
10364 {
10365 /* For other non-GCC compilers, expect their behavior is DWARF version
10366 compliant. */
10367 }
10368
10369 cu->checked_producer = 1;
10370 }
10371
10372 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10373 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10374 during 4.6.0 experimental. */
10375
10376 static int
10377 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10378 {
10379 if (!cu->checked_producer)
10380 check_producer (cu);
10381
10382 return cu->producer_is_gxx_lt_4_6;
10383 }
10384
10385 /* Return the default accessibility type if it is not overriden by
10386 DW_AT_accessibility. */
10387
10388 static enum dwarf_access_attribute
10389 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10390 {
10391 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10392 {
10393 /* The default DWARF 2 accessibility for members is public, the default
10394 accessibility for inheritance is private. */
10395
10396 if (die->tag != DW_TAG_inheritance)
10397 return DW_ACCESS_public;
10398 else
10399 return DW_ACCESS_private;
10400 }
10401 else
10402 {
10403 /* DWARF 3+ defines the default accessibility a different way. The same
10404 rules apply now for DW_TAG_inheritance as for the members and it only
10405 depends on the container kind. */
10406
10407 if (die->parent->tag == DW_TAG_class_type)
10408 return DW_ACCESS_private;
10409 else
10410 return DW_ACCESS_public;
10411 }
10412 }
10413
10414 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10415 offset. If the attribute was not found return 0, otherwise return
10416 1. If it was found but could not properly be handled, set *OFFSET
10417 to 0. */
10418
10419 static int
10420 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10421 LONGEST *offset)
10422 {
10423 struct attribute *attr;
10424
10425 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10426 if (attr != NULL)
10427 {
10428 *offset = 0;
10429
10430 /* Note that we do not check for a section offset first here.
10431 This is because DW_AT_data_member_location is new in DWARF 4,
10432 so if we see it, we can assume that a constant form is really
10433 a constant and not a section offset. */
10434 if (attr_form_is_constant (attr))
10435 *offset = dwarf2_get_attr_constant_value (attr, 0);
10436 else if (attr_form_is_section_offset (attr))
10437 dwarf2_complex_location_expr_complaint ();
10438 else if (attr_form_is_block (attr))
10439 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10440 else
10441 dwarf2_complex_location_expr_complaint ();
10442
10443 return 1;
10444 }
10445
10446 return 0;
10447 }
10448
10449 /* Add an aggregate field to the field list. */
10450
10451 static void
10452 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10453 struct dwarf2_cu *cu)
10454 {
10455 struct objfile *objfile = cu->objfile;
10456 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10457 struct nextfield *new_field;
10458 struct attribute *attr;
10459 struct field *fp;
10460 char *fieldname = "";
10461
10462 /* Allocate a new field list entry and link it in. */
10463 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10464 make_cleanup (xfree, new_field);
10465 memset (new_field, 0, sizeof (struct nextfield));
10466
10467 if (die->tag == DW_TAG_inheritance)
10468 {
10469 new_field->next = fip->baseclasses;
10470 fip->baseclasses = new_field;
10471 }
10472 else
10473 {
10474 new_field->next = fip->fields;
10475 fip->fields = new_field;
10476 }
10477 fip->nfields++;
10478
10479 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10480 if (attr)
10481 new_field->accessibility = DW_UNSND (attr);
10482 else
10483 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10484 if (new_field->accessibility != DW_ACCESS_public)
10485 fip->non_public_fields = 1;
10486
10487 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10488 if (attr)
10489 new_field->virtuality = DW_UNSND (attr);
10490 else
10491 new_field->virtuality = DW_VIRTUALITY_none;
10492
10493 fp = &new_field->field;
10494
10495 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10496 {
10497 LONGEST offset;
10498
10499 /* Data member other than a C++ static data member. */
10500
10501 /* Get type of field. */
10502 fp->type = die_type (die, cu);
10503
10504 SET_FIELD_BITPOS (*fp, 0);
10505
10506 /* Get bit size of field (zero if none). */
10507 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10508 if (attr)
10509 {
10510 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10511 }
10512 else
10513 {
10514 FIELD_BITSIZE (*fp) = 0;
10515 }
10516
10517 /* Get bit offset of field. */
10518 if (handle_data_member_location (die, cu, &offset))
10519 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10520 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10521 if (attr)
10522 {
10523 if (gdbarch_bits_big_endian (gdbarch))
10524 {
10525 /* For big endian bits, the DW_AT_bit_offset gives the
10526 additional bit offset from the MSB of the containing
10527 anonymous object to the MSB of the field. We don't
10528 have to do anything special since we don't need to
10529 know the size of the anonymous object. */
10530 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10531 }
10532 else
10533 {
10534 /* For little endian bits, compute the bit offset to the
10535 MSB of the anonymous object, subtract off the number of
10536 bits from the MSB of the field to the MSB of the
10537 object, and then subtract off the number of bits of
10538 the field itself. The result is the bit offset of
10539 the LSB of the field. */
10540 int anonymous_size;
10541 int bit_offset = DW_UNSND (attr);
10542
10543 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10544 if (attr)
10545 {
10546 /* The size of the anonymous object containing
10547 the bit field is explicit, so use the
10548 indicated size (in bytes). */
10549 anonymous_size = DW_UNSND (attr);
10550 }
10551 else
10552 {
10553 /* The size of the anonymous object containing
10554 the bit field must be inferred from the type
10555 attribute of the data member containing the
10556 bit field. */
10557 anonymous_size = TYPE_LENGTH (fp->type);
10558 }
10559 SET_FIELD_BITPOS (*fp,
10560 (FIELD_BITPOS (*fp)
10561 + anonymous_size * bits_per_byte
10562 - bit_offset - FIELD_BITSIZE (*fp)));
10563 }
10564 }
10565
10566 /* Get name of field. */
10567 fieldname = dwarf2_name (die, cu);
10568 if (fieldname == NULL)
10569 fieldname = "";
10570
10571 /* The name is already allocated along with this objfile, so we don't
10572 need to duplicate it for the type. */
10573 fp->name = fieldname;
10574
10575 /* Change accessibility for artificial fields (e.g. virtual table
10576 pointer or virtual base class pointer) to private. */
10577 if (dwarf2_attr (die, DW_AT_artificial, cu))
10578 {
10579 FIELD_ARTIFICIAL (*fp) = 1;
10580 new_field->accessibility = DW_ACCESS_private;
10581 fip->non_public_fields = 1;
10582 }
10583 }
10584 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10585 {
10586 /* C++ static member. */
10587
10588 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10589 is a declaration, but all versions of G++ as of this writing
10590 (so through at least 3.2.1) incorrectly generate
10591 DW_TAG_variable tags. */
10592
10593 const char *physname;
10594
10595 /* Get name of field. */
10596 fieldname = dwarf2_name (die, cu);
10597 if (fieldname == NULL)
10598 return;
10599
10600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10601 if (attr
10602 /* Only create a symbol if this is an external value.
10603 new_symbol checks this and puts the value in the global symbol
10604 table, which we want. If it is not external, new_symbol
10605 will try to put the value in cu->list_in_scope which is wrong. */
10606 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10607 {
10608 /* A static const member, not much different than an enum as far as
10609 we're concerned, except that we can support more types. */
10610 new_symbol (die, NULL, cu);
10611 }
10612
10613 /* Get physical name. */
10614 physname = dwarf2_physname (fieldname, die, cu);
10615
10616 /* The name is already allocated along with this objfile, so we don't
10617 need to duplicate it for the type. */
10618 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10619 FIELD_TYPE (*fp) = die_type (die, cu);
10620 FIELD_NAME (*fp) = fieldname;
10621 }
10622 else if (die->tag == DW_TAG_inheritance)
10623 {
10624 LONGEST offset;
10625
10626 /* C++ base class field. */
10627 if (handle_data_member_location (die, cu, &offset))
10628 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10629 FIELD_BITSIZE (*fp) = 0;
10630 FIELD_TYPE (*fp) = die_type (die, cu);
10631 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10632 fip->nbaseclasses++;
10633 }
10634 }
10635
10636 /* Add a typedef defined in the scope of the FIP's class. */
10637
10638 static void
10639 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10640 struct dwarf2_cu *cu)
10641 {
10642 struct objfile *objfile = cu->objfile;
10643 struct typedef_field_list *new_field;
10644 struct attribute *attr;
10645 struct typedef_field *fp;
10646 char *fieldname = "";
10647
10648 /* Allocate a new field list entry and link it in. */
10649 new_field = xzalloc (sizeof (*new_field));
10650 make_cleanup (xfree, new_field);
10651
10652 gdb_assert (die->tag == DW_TAG_typedef);
10653
10654 fp = &new_field->field;
10655
10656 /* Get name of field. */
10657 fp->name = dwarf2_name (die, cu);
10658 if (fp->name == NULL)
10659 return;
10660
10661 fp->type = read_type_die (die, cu);
10662
10663 new_field->next = fip->typedef_field_list;
10664 fip->typedef_field_list = new_field;
10665 fip->typedef_field_list_count++;
10666 }
10667
10668 /* Create the vector of fields, and attach it to the type. */
10669
10670 static void
10671 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10672 struct dwarf2_cu *cu)
10673 {
10674 int nfields = fip->nfields;
10675
10676 /* Record the field count, allocate space for the array of fields,
10677 and create blank accessibility bitfields if necessary. */
10678 TYPE_NFIELDS (type) = nfields;
10679 TYPE_FIELDS (type) = (struct field *)
10680 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10681 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10682
10683 if (fip->non_public_fields && cu->language != language_ada)
10684 {
10685 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10686
10687 TYPE_FIELD_PRIVATE_BITS (type) =
10688 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10689 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10690
10691 TYPE_FIELD_PROTECTED_BITS (type) =
10692 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10693 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10694
10695 TYPE_FIELD_IGNORE_BITS (type) =
10696 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10697 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10698 }
10699
10700 /* If the type has baseclasses, allocate and clear a bit vector for
10701 TYPE_FIELD_VIRTUAL_BITS. */
10702 if (fip->nbaseclasses && cu->language != language_ada)
10703 {
10704 int num_bytes = B_BYTES (fip->nbaseclasses);
10705 unsigned char *pointer;
10706
10707 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10708 pointer = TYPE_ALLOC (type, num_bytes);
10709 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10710 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10711 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10712 }
10713
10714 /* Copy the saved-up fields into the field vector. Start from the head of
10715 the list, adding to the tail of the field array, so that they end up in
10716 the same order in the array in which they were added to the list. */
10717 while (nfields-- > 0)
10718 {
10719 struct nextfield *fieldp;
10720
10721 if (fip->fields)
10722 {
10723 fieldp = fip->fields;
10724 fip->fields = fieldp->next;
10725 }
10726 else
10727 {
10728 fieldp = fip->baseclasses;
10729 fip->baseclasses = fieldp->next;
10730 }
10731
10732 TYPE_FIELD (type, nfields) = fieldp->field;
10733 switch (fieldp->accessibility)
10734 {
10735 case DW_ACCESS_private:
10736 if (cu->language != language_ada)
10737 SET_TYPE_FIELD_PRIVATE (type, nfields);
10738 break;
10739
10740 case DW_ACCESS_protected:
10741 if (cu->language != language_ada)
10742 SET_TYPE_FIELD_PROTECTED (type, nfields);
10743 break;
10744
10745 case DW_ACCESS_public:
10746 break;
10747
10748 default:
10749 /* Unknown accessibility. Complain and treat it as public. */
10750 {
10751 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10752 fieldp->accessibility);
10753 }
10754 break;
10755 }
10756 if (nfields < fip->nbaseclasses)
10757 {
10758 switch (fieldp->virtuality)
10759 {
10760 case DW_VIRTUALITY_virtual:
10761 case DW_VIRTUALITY_pure_virtual:
10762 if (cu->language == language_ada)
10763 error (_("unexpected virtuality in component of Ada type"));
10764 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10765 break;
10766 }
10767 }
10768 }
10769 }
10770
10771 /* Add a member function to the proper fieldlist. */
10772
10773 static void
10774 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10775 struct type *type, struct dwarf2_cu *cu)
10776 {
10777 struct objfile *objfile = cu->objfile;
10778 struct attribute *attr;
10779 struct fnfieldlist *flp;
10780 int i;
10781 struct fn_field *fnp;
10782 char *fieldname;
10783 struct nextfnfield *new_fnfield;
10784 struct type *this_type;
10785 enum dwarf_access_attribute accessibility;
10786
10787 if (cu->language == language_ada)
10788 error (_("unexpected member function in Ada type"));
10789
10790 /* Get name of member function. */
10791 fieldname = dwarf2_name (die, cu);
10792 if (fieldname == NULL)
10793 return;
10794
10795 /* Look up member function name in fieldlist. */
10796 for (i = 0; i < fip->nfnfields; i++)
10797 {
10798 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10799 break;
10800 }
10801
10802 /* Create new list element if necessary. */
10803 if (i < fip->nfnfields)
10804 flp = &fip->fnfieldlists[i];
10805 else
10806 {
10807 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10808 {
10809 fip->fnfieldlists = (struct fnfieldlist *)
10810 xrealloc (fip->fnfieldlists,
10811 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10812 * sizeof (struct fnfieldlist));
10813 if (fip->nfnfields == 0)
10814 make_cleanup (free_current_contents, &fip->fnfieldlists);
10815 }
10816 flp = &fip->fnfieldlists[fip->nfnfields];
10817 flp->name = fieldname;
10818 flp->length = 0;
10819 flp->head = NULL;
10820 i = fip->nfnfields++;
10821 }
10822
10823 /* Create a new member function field and chain it to the field list
10824 entry. */
10825 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10826 make_cleanup (xfree, new_fnfield);
10827 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10828 new_fnfield->next = flp->head;
10829 flp->head = new_fnfield;
10830 flp->length++;
10831
10832 /* Fill in the member function field info. */
10833 fnp = &new_fnfield->fnfield;
10834
10835 /* Delay processing of the physname until later. */
10836 if (cu->language == language_cplus || cu->language == language_java)
10837 {
10838 add_to_method_list (type, i, flp->length - 1, fieldname,
10839 die, cu);
10840 }
10841 else
10842 {
10843 const char *physname = dwarf2_physname (fieldname, die, cu);
10844 fnp->physname = physname ? physname : "";
10845 }
10846
10847 fnp->type = alloc_type (objfile);
10848 this_type = read_type_die (die, cu);
10849 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10850 {
10851 int nparams = TYPE_NFIELDS (this_type);
10852
10853 /* TYPE is the domain of this method, and THIS_TYPE is the type
10854 of the method itself (TYPE_CODE_METHOD). */
10855 smash_to_method_type (fnp->type, type,
10856 TYPE_TARGET_TYPE (this_type),
10857 TYPE_FIELDS (this_type),
10858 TYPE_NFIELDS (this_type),
10859 TYPE_VARARGS (this_type));
10860
10861 /* Handle static member functions.
10862 Dwarf2 has no clean way to discern C++ static and non-static
10863 member functions. G++ helps GDB by marking the first
10864 parameter for non-static member functions (which is the this
10865 pointer) as artificial. We obtain this information from
10866 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10867 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10868 fnp->voffset = VOFFSET_STATIC;
10869 }
10870 else
10871 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10872 dwarf2_full_name (fieldname, die, cu));
10873
10874 /* Get fcontext from DW_AT_containing_type if present. */
10875 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10876 fnp->fcontext = die_containing_type (die, cu);
10877
10878 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10879 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10880
10881 /* Get accessibility. */
10882 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10883 if (attr)
10884 accessibility = DW_UNSND (attr);
10885 else
10886 accessibility = dwarf2_default_access_attribute (die, cu);
10887 switch (accessibility)
10888 {
10889 case DW_ACCESS_private:
10890 fnp->is_private = 1;
10891 break;
10892 case DW_ACCESS_protected:
10893 fnp->is_protected = 1;
10894 break;
10895 }
10896
10897 /* Check for artificial methods. */
10898 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10899 if (attr && DW_UNSND (attr) != 0)
10900 fnp->is_artificial = 1;
10901
10902 /* Get index in virtual function table if it is a virtual member
10903 function. For older versions of GCC, this is an offset in the
10904 appropriate virtual table, as specified by DW_AT_containing_type.
10905 For everyone else, it is an expression to be evaluated relative
10906 to the object address. */
10907
10908 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10909 if (attr)
10910 {
10911 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10912 {
10913 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10914 {
10915 /* Old-style GCC. */
10916 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10917 }
10918 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10919 || (DW_BLOCK (attr)->size > 1
10920 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10921 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10922 {
10923 struct dwarf_block blk;
10924 int offset;
10925
10926 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10927 ? 1 : 2);
10928 blk.size = DW_BLOCK (attr)->size - offset;
10929 blk.data = DW_BLOCK (attr)->data + offset;
10930 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10931 if ((fnp->voffset % cu->header.addr_size) != 0)
10932 dwarf2_complex_location_expr_complaint ();
10933 else
10934 fnp->voffset /= cu->header.addr_size;
10935 fnp->voffset += 2;
10936 }
10937 else
10938 dwarf2_complex_location_expr_complaint ();
10939
10940 if (!fnp->fcontext)
10941 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10942 }
10943 else if (attr_form_is_section_offset (attr))
10944 {
10945 dwarf2_complex_location_expr_complaint ();
10946 }
10947 else
10948 {
10949 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10950 fieldname);
10951 }
10952 }
10953 else
10954 {
10955 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10956 if (attr && DW_UNSND (attr))
10957 {
10958 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10959 complaint (&symfile_complaints,
10960 _("Member function \"%s\" (offset %d) is virtual "
10961 "but the vtable offset is not specified"),
10962 fieldname, die->offset.sect_off);
10963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10964 TYPE_CPLUS_DYNAMIC (type) = 1;
10965 }
10966 }
10967 }
10968
10969 /* Create the vector of member function fields, and attach it to the type. */
10970
10971 static void
10972 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10973 struct dwarf2_cu *cu)
10974 {
10975 struct fnfieldlist *flp;
10976 int i;
10977
10978 if (cu->language == language_ada)
10979 error (_("unexpected member functions in Ada type"));
10980
10981 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10982 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10983 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10984
10985 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10986 {
10987 struct nextfnfield *nfp = flp->head;
10988 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10989 int k;
10990
10991 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10992 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10993 fn_flp->fn_fields = (struct fn_field *)
10994 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10995 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10996 fn_flp->fn_fields[k] = nfp->fnfield;
10997 }
10998
10999 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11000 }
11001
11002 /* Returns non-zero if NAME is the name of a vtable member in CU's
11003 language, zero otherwise. */
11004 static int
11005 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11006 {
11007 static const char vptr[] = "_vptr";
11008 static const char vtable[] = "vtable";
11009
11010 /* Look for the C++ and Java forms of the vtable. */
11011 if ((cu->language == language_java
11012 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11013 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11014 && is_cplus_marker (name[sizeof (vptr) - 1])))
11015 return 1;
11016
11017 return 0;
11018 }
11019
11020 /* GCC outputs unnamed structures that are really pointers to member
11021 functions, with the ABI-specified layout. If TYPE describes
11022 such a structure, smash it into a member function type.
11023
11024 GCC shouldn't do this; it should just output pointer to member DIEs.
11025 This is GCC PR debug/28767. */
11026
11027 static void
11028 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11029 {
11030 struct type *pfn_type, *domain_type, *new_type;
11031
11032 /* Check for a structure with no name and two children. */
11033 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11034 return;
11035
11036 /* Check for __pfn and __delta members. */
11037 if (TYPE_FIELD_NAME (type, 0) == NULL
11038 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11039 || TYPE_FIELD_NAME (type, 1) == NULL
11040 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11041 return;
11042
11043 /* Find the type of the method. */
11044 pfn_type = TYPE_FIELD_TYPE (type, 0);
11045 if (pfn_type == NULL
11046 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11047 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11048 return;
11049
11050 /* Look for the "this" argument. */
11051 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11052 if (TYPE_NFIELDS (pfn_type) == 0
11053 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11054 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11055 return;
11056
11057 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11058 new_type = alloc_type (objfile);
11059 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11060 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11061 TYPE_VARARGS (pfn_type));
11062 smash_to_methodptr_type (type, new_type);
11063 }
11064
11065 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11066 (icc). */
11067
11068 static int
11069 producer_is_icc (struct dwarf2_cu *cu)
11070 {
11071 if (!cu->checked_producer)
11072 check_producer (cu);
11073
11074 return cu->producer_is_icc;
11075 }
11076
11077 /* Called when we find the DIE that starts a structure or union scope
11078 (definition) to create a type for the structure or union. Fill in
11079 the type's name and general properties; the members will not be
11080 processed until process_structure_type.
11081
11082 NOTE: we need to call these functions regardless of whether or not the
11083 DIE has a DW_AT_name attribute, since it might be an anonymous
11084 structure or union. This gets the type entered into our set of
11085 user defined types.
11086
11087 However, if the structure is incomplete (an opaque struct/union)
11088 then suppress creating a symbol table entry for it since gdb only
11089 wants to find the one with the complete definition. Note that if
11090 it is complete, we just call new_symbol, which does it's own
11091 checking about whether the struct/union is anonymous or not (and
11092 suppresses creating a symbol table entry itself). */
11093
11094 static struct type *
11095 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11096 {
11097 struct objfile *objfile = cu->objfile;
11098 struct type *type;
11099 struct attribute *attr;
11100 char *name;
11101
11102 /* If the definition of this type lives in .debug_types, read that type.
11103 Don't follow DW_AT_specification though, that will take us back up
11104 the chain and we want to go down. */
11105 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11106 if (attr)
11107 {
11108 struct dwarf2_cu *type_cu = cu;
11109 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11110
11111 /* We could just recurse on read_structure_type, but we need to call
11112 get_die_type to ensure only one type for this DIE is created.
11113 This is important, for example, because for c++ classes we need
11114 TYPE_NAME set which is only done by new_symbol. Blech. */
11115 type = read_type_die (type_die, type_cu);
11116
11117 /* TYPE_CU may not be the same as CU.
11118 Ensure TYPE is recorded in CU's type_hash table. */
11119 return set_die_type (die, type, cu);
11120 }
11121
11122 type = alloc_type (objfile);
11123 INIT_CPLUS_SPECIFIC (type);
11124
11125 name = dwarf2_name (die, cu);
11126 if (name != NULL)
11127 {
11128 if (cu->language == language_cplus
11129 || cu->language == language_java)
11130 {
11131 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11132
11133 /* dwarf2_full_name might have already finished building the DIE's
11134 type. If so, there is no need to continue. */
11135 if (get_die_type (die, cu) != NULL)
11136 return get_die_type (die, cu);
11137
11138 TYPE_TAG_NAME (type) = full_name;
11139 if (die->tag == DW_TAG_structure_type
11140 || die->tag == DW_TAG_class_type)
11141 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11142 }
11143 else
11144 {
11145 /* The name is already allocated along with this objfile, so
11146 we don't need to duplicate it for the type. */
11147 TYPE_TAG_NAME (type) = (char *) name;
11148 if (die->tag == DW_TAG_class_type)
11149 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11150 }
11151 }
11152
11153 if (die->tag == DW_TAG_structure_type)
11154 {
11155 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11156 }
11157 else if (die->tag == DW_TAG_union_type)
11158 {
11159 TYPE_CODE (type) = TYPE_CODE_UNION;
11160 }
11161 else
11162 {
11163 TYPE_CODE (type) = TYPE_CODE_CLASS;
11164 }
11165
11166 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11167 TYPE_DECLARED_CLASS (type) = 1;
11168
11169 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11170 if (attr)
11171 {
11172 TYPE_LENGTH (type) = DW_UNSND (attr);
11173 }
11174 else
11175 {
11176 TYPE_LENGTH (type) = 0;
11177 }
11178
11179 if (producer_is_icc (cu))
11180 {
11181 /* ICC does not output the required DW_AT_declaration
11182 on incomplete types, but gives them a size of zero. */
11183 }
11184 else
11185 TYPE_STUB_SUPPORTED (type) = 1;
11186
11187 if (die_is_declaration (die, cu))
11188 TYPE_STUB (type) = 1;
11189 else if (attr == NULL && die->child == NULL
11190 && producer_is_realview (cu->producer))
11191 /* RealView does not output the required DW_AT_declaration
11192 on incomplete types. */
11193 TYPE_STUB (type) = 1;
11194
11195 /* We need to add the type field to the die immediately so we don't
11196 infinitely recurse when dealing with pointers to the structure
11197 type within the structure itself. */
11198 set_die_type (die, type, cu);
11199
11200 /* set_die_type should be already done. */
11201 set_descriptive_type (type, die, cu);
11202
11203 return type;
11204 }
11205
11206 /* Finish creating a structure or union type, including filling in
11207 its members and creating a symbol for it. */
11208
11209 static void
11210 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11211 {
11212 struct objfile *objfile = cu->objfile;
11213 struct die_info *child_die = die->child;
11214 struct type *type;
11215
11216 type = get_die_type (die, cu);
11217 if (type == NULL)
11218 type = read_structure_type (die, cu);
11219
11220 if (die->child != NULL && ! die_is_declaration (die, cu))
11221 {
11222 struct field_info fi;
11223 struct die_info *child_die;
11224 VEC (symbolp) *template_args = NULL;
11225 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11226
11227 memset (&fi, 0, sizeof (struct field_info));
11228
11229 child_die = die->child;
11230
11231 while (child_die && child_die->tag)
11232 {
11233 if (child_die->tag == DW_TAG_member
11234 || child_die->tag == DW_TAG_variable)
11235 {
11236 /* NOTE: carlton/2002-11-05: A C++ static data member
11237 should be a DW_TAG_member that is a declaration, but
11238 all versions of G++ as of this writing (so through at
11239 least 3.2.1) incorrectly generate DW_TAG_variable
11240 tags for them instead. */
11241 dwarf2_add_field (&fi, child_die, cu);
11242 }
11243 else if (child_die->tag == DW_TAG_subprogram)
11244 {
11245 /* C++ member function. */
11246 dwarf2_add_member_fn (&fi, child_die, type, cu);
11247 }
11248 else if (child_die->tag == DW_TAG_inheritance)
11249 {
11250 /* C++ base class field. */
11251 dwarf2_add_field (&fi, child_die, cu);
11252 }
11253 else if (child_die->tag == DW_TAG_typedef)
11254 dwarf2_add_typedef (&fi, child_die, cu);
11255 else if (child_die->tag == DW_TAG_template_type_param
11256 || child_die->tag == DW_TAG_template_value_param)
11257 {
11258 struct symbol *arg = new_symbol (child_die, NULL, cu);
11259
11260 if (arg != NULL)
11261 VEC_safe_push (symbolp, template_args, arg);
11262 }
11263
11264 child_die = sibling_die (child_die);
11265 }
11266
11267 /* Attach template arguments to type. */
11268 if (! VEC_empty (symbolp, template_args))
11269 {
11270 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11271 TYPE_N_TEMPLATE_ARGUMENTS (type)
11272 = VEC_length (symbolp, template_args);
11273 TYPE_TEMPLATE_ARGUMENTS (type)
11274 = obstack_alloc (&objfile->objfile_obstack,
11275 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11276 * sizeof (struct symbol *)));
11277 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11278 VEC_address (symbolp, template_args),
11279 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11280 * sizeof (struct symbol *)));
11281 VEC_free (symbolp, template_args);
11282 }
11283
11284 /* Attach fields and member functions to the type. */
11285 if (fi.nfields)
11286 dwarf2_attach_fields_to_type (&fi, type, cu);
11287 if (fi.nfnfields)
11288 {
11289 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11290
11291 /* Get the type which refers to the base class (possibly this
11292 class itself) which contains the vtable pointer for the current
11293 class from the DW_AT_containing_type attribute. This use of
11294 DW_AT_containing_type is a GNU extension. */
11295
11296 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11297 {
11298 struct type *t = die_containing_type (die, cu);
11299
11300 TYPE_VPTR_BASETYPE (type) = t;
11301 if (type == t)
11302 {
11303 int i;
11304
11305 /* Our own class provides vtbl ptr. */
11306 for (i = TYPE_NFIELDS (t) - 1;
11307 i >= TYPE_N_BASECLASSES (t);
11308 --i)
11309 {
11310 const char *fieldname = TYPE_FIELD_NAME (t, i);
11311
11312 if (is_vtable_name (fieldname, cu))
11313 {
11314 TYPE_VPTR_FIELDNO (type) = i;
11315 break;
11316 }
11317 }
11318
11319 /* Complain if virtual function table field not found. */
11320 if (i < TYPE_N_BASECLASSES (t))
11321 complaint (&symfile_complaints,
11322 _("virtual function table pointer "
11323 "not found when defining class '%s'"),
11324 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11325 "");
11326 }
11327 else
11328 {
11329 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11330 }
11331 }
11332 else if (cu->producer
11333 && strncmp (cu->producer,
11334 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11335 {
11336 /* The IBM XLC compiler does not provide direct indication
11337 of the containing type, but the vtable pointer is
11338 always named __vfp. */
11339
11340 int i;
11341
11342 for (i = TYPE_NFIELDS (type) - 1;
11343 i >= TYPE_N_BASECLASSES (type);
11344 --i)
11345 {
11346 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11347 {
11348 TYPE_VPTR_FIELDNO (type) = i;
11349 TYPE_VPTR_BASETYPE (type) = type;
11350 break;
11351 }
11352 }
11353 }
11354 }
11355
11356 /* Copy fi.typedef_field_list linked list elements content into the
11357 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11358 if (fi.typedef_field_list)
11359 {
11360 int i = fi.typedef_field_list_count;
11361
11362 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11363 TYPE_TYPEDEF_FIELD_ARRAY (type)
11364 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11365 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11366
11367 /* Reverse the list order to keep the debug info elements order. */
11368 while (--i >= 0)
11369 {
11370 struct typedef_field *dest, *src;
11371
11372 dest = &TYPE_TYPEDEF_FIELD (type, i);
11373 src = &fi.typedef_field_list->field;
11374 fi.typedef_field_list = fi.typedef_field_list->next;
11375 *dest = *src;
11376 }
11377 }
11378
11379 do_cleanups (back_to);
11380
11381 if (HAVE_CPLUS_STRUCT (type))
11382 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11383 }
11384
11385 quirk_gcc_member_function_pointer (type, objfile);
11386
11387 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11388 snapshots) has been known to create a die giving a declaration
11389 for a class that has, as a child, a die giving a definition for a
11390 nested class. So we have to process our children even if the
11391 current die is a declaration. Normally, of course, a declaration
11392 won't have any children at all. */
11393
11394 while (child_die != NULL && child_die->tag)
11395 {
11396 if (child_die->tag == DW_TAG_member
11397 || child_die->tag == DW_TAG_variable
11398 || child_die->tag == DW_TAG_inheritance
11399 || child_die->tag == DW_TAG_template_value_param
11400 || child_die->tag == DW_TAG_template_type_param)
11401 {
11402 /* Do nothing. */
11403 }
11404 else
11405 process_die (child_die, cu);
11406
11407 child_die = sibling_die (child_die);
11408 }
11409
11410 /* Do not consider external references. According to the DWARF standard,
11411 these DIEs are identified by the fact that they have no byte_size
11412 attribute, and a declaration attribute. */
11413 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11414 || !die_is_declaration (die, cu))
11415 new_symbol (die, type, cu);
11416 }
11417
11418 /* Given a DW_AT_enumeration_type die, set its type. We do not
11419 complete the type's fields yet, or create any symbols. */
11420
11421 static struct type *
11422 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11423 {
11424 struct objfile *objfile = cu->objfile;
11425 struct type *type;
11426 struct attribute *attr;
11427 const char *name;
11428
11429 /* If the definition of this type lives in .debug_types, read that type.
11430 Don't follow DW_AT_specification though, that will take us back up
11431 the chain and we want to go down. */
11432 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11433 if (attr)
11434 {
11435 struct dwarf2_cu *type_cu = cu;
11436 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11437
11438 type = read_type_die (type_die, type_cu);
11439
11440 /* TYPE_CU may not be the same as CU.
11441 Ensure TYPE is recorded in CU's type_hash table. */
11442 return set_die_type (die, type, cu);
11443 }
11444
11445 type = alloc_type (objfile);
11446
11447 TYPE_CODE (type) = TYPE_CODE_ENUM;
11448 name = dwarf2_full_name (NULL, die, cu);
11449 if (name != NULL)
11450 TYPE_TAG_NAME (type) = (char *) name;
11451
11452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11453 if (attr)
11454 {
11455 TYPE_LENGTH (type) = DW_UNSND (attr);
11456 }
11457 else
11458 {
11459 TYPE_LENGTH (type) = 0;
11460 }
11461
11462 /* The enumeration DIE can be incomplete. In Ada, any type can be
11463 declared as private in the package spec, and then defined only
11464 inside the package body. Such types are known as Taft Amendment
11465 Types. When another package uses such a type, an incomplete DIE
11466 may be generated by the compiler. */
11467 if (die_is_declaration (die, cu))
11468 TYPE_STUB (type) = 1;
11469
11470 return set_die_type (die, type, cu);
11471 }
11472
11473 /* Given a pointer to a die which begins an enumeration, process all
11474 the dies that define the members of the enumeration, and create the
11475 symbol for the enumeration type.
11476
11477 NOTE: We reverse the order of the element list. */
11478
11479 static void
11480 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11481 {
11482 struct type *this_type;
11483
11484 this_type = get_die_type (die, cu);
11485 if (this_type == NULL)
11486 this_type = read_enumeration_type (die, cu);
11487
11488 if (die->child != NULL)
11489 {
11490 struct die_info *child_die;
11491 struct symbol *sym;
11492 struct field *fields = NULL;
11493 int num_fields = 0;
11494 int unsigned_enum = 1;
11495 char *name;
11496 int flag_enum = 1;
11497 ULONGEST mask = 0;
11498
11499 child_die = die->child;
11500 while (child_die && child_die->tag)
11501 {
11502 if (child_die->tag != DW_TAG_enumerator)
11503 {
11504 process_die (child_die, cu);
11505 }
11506 else
11507 {
11508 name = dwarf2_name (child_die, cu);
11509 if (name)
11510 {
11511 sym = new_symbol (child_die, this_type, cu);
11512 if (SYMBOL_VALUE (sym) < 0)
11513 {
11514 unsigned_enum = 0;
11515 flag_enum = 0;
11516 }
11517 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11518 flag_enum = 0;
11519 else
11520 mask |= SYMBOL_VALUE (sym);
11521
11522 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11523 {
11524 fields = (struct field *)
11525 xrealloc (fields,
11526 (num_fields + DW_FIELD_ALLOC_CHUNK)
11527 * sizeof (struct field));
11528 }
11529
11530 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11531 FIELD_TYPE (fields[num_fields]) = NULL;
11532 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11533 FIELD_BITSIZE (fields[num_fields]) = 0;
11534
11535 num_fields++;
11536 }
11537 }
11538
11539 child_die = sibling_die (child_die);
11540 }
11541
11542 if (num_fields)
11543 {
11544 TYPE_NFIELDS (this_type) = num_fields;
11545 TYPE_FIELDS (this_type) = (struct field *)
11546 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11547 memcpy (TYPE_FIELDS (this_type), fields,
11548 sizeof (struct field) * num_fields);
11549 xfree (fields);
11550 }
11551 if (unsigned_enum)
11552 TYPE_UNSIGNED (this_type) = 1;
11553 if (flag_enum)
11554 TYPE_FLAG_ENUM (this_type) = 1;
11555 }
11556
11557 /* If we are reading an enum from a .debug_types unit, and the enum
11558 is a declaration, and the enum is not the signatured type in the
11559 unit, then we do not want to add a symbol for it. Adding a
11560 symbol would in some cases obscure the true definition of the
11561 enum, giving users an incomplete type when the definition is
11562 actually available. Note that we do not want to do this for all
11563 enums which are just declarations, because C++0x allows forward
11564 enum declarations. */
11565 if (cu->per_cu->is_debug_types
11566 && die_is_declaration (die, cu))
11567 {
11568 struct signatured_type *sig_type;
11569
11570 sig_type
11571 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11572 cu->per_cu->info_or_types_section,
11573 cu->per_cu->offset);
11574 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11575 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11576 return;
11577 }
11578
11579 new_symbol (die, this_type, cu);
11580 }
11581
11582 /* Extract all information from a DW_TAG_array_type DIE and put it in
11583 the DIE's type field. For now, this only handles one dimensional
11584 arrays. */
11585
11586 static struct type *
11587 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11588 {
11589 struct objfile *objfile = cu->objfile;
11590 struct die_info *child_die;
11591 struct type *type;
11592 struct type *element_type, *range_type, *index_type;
11593 struct type **range_types = NULL;
11594 struct attribute *attr;
11595 int ndim = 0;
11596 struct cleanup *back_to;
11597 char *name;
11598
11599 element_type = die_type (die, cu);
11600
11601 /* The die_type call above may have already set the type for this DIE. */
11602 type = get_die_type (die, cu);
11603 if (type)
11604 return type;
11605
11606 /* Irix 6.2 native cc creates array types without children for
11607 arrays with unspecified length. */
11608 if (die->child == NULL)
11609 {
11610 index_type = objfile_type (objfile)->builtin_int;
11611 range_type = create_range_type (NULL, index_type, 0, -1);
11612 type = create_array_type (NULL, element_type, range_type);
11613 return set_die_type (die, type, cu);
11614 }
11615
11616 back_to = make_cleanup (null_cleanup, NULL);
11617 child_die = die->child;
11618 while (child_die && child_die->tag)
11619 {
11620 if (child_die->tag == DW_TAG_subrange_type)
11621 {
11622 struct type *child_type = read_type_die (child_die, cu);
11623
11624 if (child_type != NULL)
11625 {
11626 /* The range type was succesfully read. Save it for the
11627 array type creation. */
11628 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11629 {
11630 range_types = (struct type **)
11631 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11632 * sizeof (struct type *));
11633 if (ndim == 0)
11634 make_cleanup (free_current_contents, &range_types);
11635 }
11636 range_types[ndim++] = child_type;
11637 }
11638 }
11639 child_die = sibling_die (child_die);
11640 }
11641
11642 /* Dwarf2 dimensions are output from left to right, create the
11643 necessary array types in backwards order. */
11644
11645 type = element_type;
11646
11647 if (read_array_order (die, cu) == DW_ORD_col_major)
11648 {
11649 int i = 0;
11650
11651 while (i < ndim)
11652 type = create_array_type (NULL, type, range_types[i++]);
11653 }
11654 else
11655 {
11656 while (ndim-- > 0)
11657 type = create_array_type (NULL, type, range_types[ndim]);
11658 }
11659
11660 /* Understand Dwarf2 support for vector types (like they occur on
11661 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11662 array type. This is not part of the Dwarf2/3 standard yet, but a
11663 custom vendor extension. The main difference between a regular
11664 array and the vector variant is that vectors are passed by value
11665 to functions. */
11666 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11667 if (attr)
11668 make_vector_type (type);
11669
11670 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11671 implementation may choose to implement triple vectors using this
11672 attribute. */
11673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11674 if (attr)
11675 {
11676 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11677 TYPE_LENGTH (type) = DW_UNSND (attr);
11678 else
11679 complaint (&symfile_complaints,
11680 _("DW_AT_byte_size for array type smaller "
11681 "than the total size of elements"));
11682 }
11683
11684 name = dwarf2_name (die, cu);
11685 if (name)
11686 TYPE_NAME (type) = name;
11687
11688 /* Install the type in the die. */
11689 set_die_type (die, type, cu);
11690
11691 /* set_die_type should be already done. */
11692 set_descriptive_type (type, die, cu);
11693
11694 do_cleanups (back_to);
11695
11696 return type;
11697 }
11698
11699 static enum dwarf_array_dim_ordering
11700 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11701 {
11702 struct attribute *attr;
11703
11704 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11705
11706 if (attr) return DW_SND (attr);
11707
11708 /* GNU F77 is a special case, as at 08/2004 array type info is the
11709 opposite order to the dwarf2 specification, but data is still
11710 laid out as per normal fortran.
11711
11712 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11713 version checking. */
11714
11715 if (cu->language == language_fortran
11716 && cu->producer && strstr (cu->producer, "GNU F77"))
11717 {
11718 return DW_ORD_row_major;
11719 }
11720
11721 switch (cu->language_defn->la_array_ordering)
11722 {
11723 case array_column_major:
11724 return DW_ORD_col_major;
11725 case array_row_major:
11726 default:
11727 return DW_ORD_row_major;
11728 };
11729 }
11730
11731 /* Extract all information from a DW_TAG_set_type DIE and put it in
11732 the DIE's type field. */
11733
11734 static struct type *
11735 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11736 {
11737 struct type *domain_type, *set_type;
11738 struct attribute *attr;
11739
11740 domain_type = die_type (die, cu);
11741
11742 /* The die_type call above may have already set the type for this DIE. */
11743 set_type = get_die_type (die, cu);
11744 if (set_type)
11745 return set_type;
11746
11747 set_type = create_set_type (NULL, domain_type);
11748
11749 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11750 if (attr)
11751 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11752
11753 return set_die_type (die, set_type, cu);
11754 }
11755
11756 /* A helper for read_common_block that creates a locexpr baton.
11757 SYM is the symbol which we are marking as computed.
11758 COMMON_DIE is the DIE for the common block.
11759 COMMON_LOC is the location expression attribute for the common
11760 block itself.
11761 MEMBER_LOC is the location expression attribute for the particular
11762 member of the common block that we are processing.
11763 CU is the CU from which the above come. */
11764
11765 static void
11766 mark_common_block_symbol_computed (struct symbol *sym,
11767 struct die_info *common_die,
11768 struct attribute *common_loc,
11769 struct attribute *member_loc,
11770 struct dwarf2_cu *cu)
11771 {
11772 struct objfile *objfile = dwarf2_per_objfile->objfile;
11773 struct dwarf2_locexpr_baton *baton;
11774 gdb_byte *ptr;
11775 unsigned int cu_off;
11776 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11777 LONGEST offset = 0;
11778
11779 gdb_assert (common_loc && member_loc);
11780 gdb_assert (attr_form_is_block (common_loc));
11781 gdb_assert (attr_form_is_block (member_loc)
11782 || attr_form_is_constant (member_loc));
11783
11784 baton = obstack_alloc (&objfile->objfile_obstack,
11785 sizeof (struct dwarf2_locexpr_baton));
11786 baton->per_cu = cu->per_cu;
11787 gdb_assert (baton->per_cu);
11788
11789 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11790
11791 if (attr_form_is_constant (member_loc))
11792 {
11793 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11794 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11795 }
11796 else
11797 baton->size += DW_BLOCK (member_loc)->size;
11798
11799 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11800 baton->data = ptr;
11801
11802 *ptr++ = DW_OP_call4;
11803 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11804 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11805 ptr += 4;
11806
11807 if (attr_form_is_constant (member_loc))
11808 {
11809 *ptr++ = DW_OP_addr;
11810 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11811 ptr += cu->header.addr_size;
11812 }
11813 else
11814 {
11815 /* We have to copy the data here, because DW_OP_call4 will only
11816 use a DW_AT_location attribute. */
11817 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11818 ptr += DW_BLOCK (member_loc)->size;
11819 }
11820
11821 *ptr++ = DW_OP_plus;
11822 gdb_assert (ptr - baton->data == baton->size);
11823
11824 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11825 SYMBOL_LOCATION_BATON (sym) = baton;
11826 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11827 }
11828
11829 /* Create appropriate locally-scoped variables for all the
11830 DW_TAG_common_block entries. Also create a struct common_block
11831 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11832 is used to sepate the common blocks name namespace from regular
11833 variable names. */
11834
11835 static void
11836 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11837 {
11838 struct attribute *attr;
11839
11840 attr = dwarf2_attr (die, DW_AT_location, cu);
11841 if (attr)
11842 {
11843 /* Support the .debug_loc offsets. */
11844 if (attr_form_is_block (attr))
11845 {
11846 /* Ok. */
11847 }
11848 else if (attr_form_is_section_offset (attr))
11849 {
11850 dwarf2_complex_location_expr_complaint ();
11851 attr = NULL;
11852 }
11853 else
11854 {
11855 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11856 "common block member");
11857 attr = NULL;
11858 }
11859 }
11860
11861 if (die->child != NULL)
11862 {
11863 struct objfile *objfile = cu->objfile;
11864 struct die_info *child_die;
11865 size_t n_entries = 0, size;
11866 struct common_block *common_block;
11867 struct symbol *sym;
11868
11869 for (child_die = die->child;
11870 child_die && child_die->tag;
11871 child_die = sibling_die (child_die))
11872 ++n_entries;
11873
11874 size = (sizeof (struct common_block)
11875 + (n_entries - 1) * sizeof (struct symbol *));
11876 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11877 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11878 common_block->n_entries = 0;
11879
11880 for (child_die = die->child;
11881 child_die && child_die->tag;
11882 child_die = sibling_die (child_die))
11883 {
11884 /* Create the symbol in the DW_TAG_common_block block in the current
11885 symbol scope. */
11886 sym = new_symbol (child_die, NULL, cu);
11887 if (sym != NULL)
11888 {
11889 struct attribute *member_loc;
11890
11891 common_block->contents[common_block->n_entries++] = sym;
11892
11893 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11894 cu);
11895 if (member_loc)
11896 {
11897 /* GDB has handled this for a long time, but it is
11898 not specified by DWARF. It seems to have been
11899 emitted by gfortran at least as recently as:
11900 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11901 complaint (&symfile_complaints,
11902 _("Variable in common block has "
11903 "DW_AT_data_member_location "
11904 "- DIE at 0x%x [in module %s]"),
11905 child_die->offset.sect_off, cu->objfile->name);
11906
11907 if (attr_form_is_section_offset (member_loc))
11908 dwarf2_complex_location_expr_complaint ();
11909 else if (attr_form_is_constant (member_loc)
11910 || attr_form_is_block (member_loc))
11911 {
11912 if (attr)
11913 mark_common_block_symbol_computed (sym, die, attr,
11914 member_loc, cu);
11915 }
11916 else
11917 dwarf2_complex_location_expr_complaint ();
11918 }
11919 }
11920 }
11921
11922 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11923 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
11924 }
11925 }
11926
11927 /* Create a type for a C++ namespace. */
11928
11929 static struct type *
11930 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11931 {
11932 struct objfile *objfile = cu->objfile;
11933 const char *previous_prefix, *name;
11934 int is_anonymous;
11935 struct type *type;
11936
11937 /* For extensions, reuse the type of the original namespace. */
11938 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11939 {
11940 struct die_info *ext_die;
11941 struct dwarf2_cu *ext_cu = cu;
11942
11943 ext_die = dwarf2_extension (die, &ext_cu);
11944 type = read_type_die (ext_die, ext_cu);
11945
11946 /* EXT_CU may not be the same as CU.
11947 Ensure TYPE is recorded in CU's type_hash table. */
11948 return set_die_type (die, type, cu);
11949 }
11950
11951 name = namespace_name (die, &is_anonymous, cu);
11952
11953 /* Now build the name of the current namespace. */
11954
11955 previous_prefix = determine_prefix (die, cu);
11956 if (previous_prefix[0] != '\0')
11957 name = typename_concat (&objfile->objfile_obstack,
11958 previous_prefix, name, 0, cu);
11959
11960 /* Create the type. */
11961 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11962 objfile);
11963 TYPE_NAME (type) = (char *) name;
11964 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11965
11966 return set_die_type (die, type, cu);
11967 }
11968
11969 /* Read a C++ namespace. */
11970
11971 static void
11972 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11973 {
11974 struct objfile *objfile = cu->objfile;
11975 int is_anonymous;
11976
11977 /* Add a symbol associated to this if we haven't seen the namespace
11978 before. Also, add a using directive if it's an anonymous
11979 namespace. */
11980
11981 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11982 {
11983 struct type *type;
11984
11985 type = read_type_die (die, cu);
11986 new_symbol (die, type, cu);
11987
11988 namespace_name (die, &is_anonymous, cu);
11989 if (is_anonymous)
11990 {
11991 const char *previous_prefix = determine_prefix (die, cu);
11992
11993 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11994 NULL, NULL, &objfile->objfile_obstack);
11995 }
11996 }
11997
11998 if (die->child != NULL)
11999 {
12000 struct die_info *child_die = die->child;
12001
12002 while (child_die && child_die->tag)
12003 {
12004 process_die (child_die, cu);
12005 child_die = sibling_die (child_die);
12006 }
12007 }
12008 }
12009
12010 /* Read a Fortran module as type. This DIE can be only a declaration used for
12011 imported module. Still we need that type as local Fortran "use ... only"
12012 declaration imports depend on the created type in determine_prefix. */
12013
12014 static struct type *
12015 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12016 {
12017 struct objfile *objfile = cu->objfile;
12018 char *module_name;
12019 struct type *type;
12020
12021 module_name = dwarf2_name (die, cu);
12022 if (!module_name)
12023 complaint (&symfile_complaints,
12024 _("DW_TAG_module has no name, offset 0x%x"),
12025 die->offset.sect_off);
12026 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12027
12028 /* determine_prefix uses TYPE_TAG_NAME. */
12029 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12030
12031 return set_die_type (die, type, cu);
12032 }
12033
12034 /* Read a Fortran module. */
12035
12036 static void
12037 read_module (struct die_info *die, struct dwarf2_cu *cu)
12038 {
12039 struct die_info *child_die = die->child;
12040
12041 while (child_die && child_die->tag)
12042 {
12043 process_die (child_die, cu);
12044 child_die = sibling_die (child_die);
12045 }
12046 }
12047
12048 /* Return the name of the namespace represented by DIE. Set
12049 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12050 namespace. */
12051
12052 static const char *
12053 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12054 {
12055 struct die_info *current_die;
12056 const char *name = NULL;
12057
12058 /* Loop through the extensions until we find a name. */
12059
12060 for (current_die = die;
12061 current_die != NULL;
12062 current_die = dwarf2_extension (die, &cu))
12063 {
12064 name = dwarf2_name (current_die, cu);
12065 if (name != NULL)
12066 break;
12067 }
12068
12069 /* Is it an anonymous namespace? */
12070
12071 *is_anonymous = (name == NULL);
12072 if (*is_anonymous)
12073 name = CP_ANONYMOUS_NAMESPACE_STR;
12074
12075 return name;
12076 }
12077
12078 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12079 the user defined type vector. */
12080
12081 static struct type *
12082 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12083 {
12084 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12085 struct comp_unit_head *cu_header = &cu->header;
12086 struct type *type;
12087 struct attribute *attr_byte_size;
12088 struct attribute *attr_address_class;
12089 int byte_size, addr_class;
12090 struct type *target_type;
12091
12092 target_type = die_type (die, cu);
12093
12094 /* The die_type call above may have already set the type for this DIE. */
12095 type = get_die_type (die, cu);
12096 if (type)
12097 return type;
12098
12099 type = lookup_pointer_type (target_type);
12100
12101 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12102 if (attr_byte_size)
12103 byte_size = DW_UNSND (attr_byte_size);
12104 else
12105 byte_size = cu_header->addr_size;
12106
12107 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12108 if (attr_address_class)
12109 addr_class = DW_UNSND (attr_address_class);
12110 else
12111 addr_class = DW_ADDR_none;
12112
12113 /* If the pointer size or address class is different than the
12114 default, create a type variant marked as such and set the
12115 length accordingly. */
12116 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12117 {
12118 if (gdbarch_address_class_type_flags_p (gdbarch))
12119 {
12120 int type_flags;
12121
12122 type_flags = gdbarch_address_class_type_flags
12123 (gdbarch, byte_size, addr_class);
12124 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12125 == 0);
12126 type = make_type_with_address_space (type, type_flags);
12127 }
12128 else if (TYPE_LENGTH (type) != byte_size)
12129 {
12130 complaint (&symfile_complaints,
12131 _("invalid pointer size %d"), byte_size);
12132 }
12133 else
12134 {
12135 /* Should we also complain about unhandled address classes? */
12136 }
12137 }
12138
12139 TYPE_LENGTH (type) = byte_size;
12140 return set_die_type (die, type, cu);
12141 }
12142
12143 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12144 the user defined type vector. */
12145
12146 static struct type *
12147 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12148 {
12149 struct type *type;
12150 struct type *to_type;
12151 struct type *domain;
12152
12153 to_type = die_type (die, cu);
12154 domain = die_containing_type (die, cu);
12155
12156 /* The calls above may have already set the type for this DIE. */
12157 type = get_die_type (die, cu);
12158 if (type)
12159 return type;
12160
12161 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12162 type = lookup_methodptr_type (to_type);
12163 else
12164 type = lookup_memberptr_type (to_type, domain);
12165
12166 return set_die_type (die, type, cu);
12167 }
12168
12169 /* Extract all information from a DW_TAG_reference_type DIE and add to
12170 the user defined type vector. */
12171
12172 static struct type *
12173 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12174 {
12175 struct comp_unit_head *cu_header = &cu->header;
12176 struct type *type, *target_type;
12177 struct attribute *attr;
12178
12179 target_type = die_type (die, cu);
12180
12181 /* The die_type call above may have already set the type for this DIE. */
12182 type = get_die_type (die, cu);
12183 if (type)
12184 return type;
12185
12186 type = lookup_reference_type (target_type);
12187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12188 if (attr)
12189 {
12190 TYPE_LENGTH (type) = DW_UNSND (attr);
12191 }
12192 else
12193 {
12194 TYPE_LENGTH (type) = cu_header->addr_size;
12195 }
12196 return set_die_type (die, type, cu);
12197 }
12198
12199 static struct type *
12200 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12201 {
12202 struct type *base_type, *cv_type;
12203
12204 base_type = die_type (die, cu);
12205
12206 /* The die_type call above may have already set the type for this DIE. */
12207 cv_type = get_die_type (die, cu);
12208 if (cv_type)
12209 return cv_type;
12210
12211 /* In case the const qualifier is applied to an array type, the element type
12212 is so qualified, not the array type (section 6.7.3 of C99). */
12213 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12214 {
12215 struct type *el_type, *inner_array;
12216
12217 base_type = copy_type (base_type);
12218 inner_array = base_type;
12219
12220 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12221 {
12222 TYPE_TARGET_TYPE (inner_array) =
12223 copy_type (TYPE_TARGET_TYPE (inner_array));
12224 inner_array = TYPE_TARGET_TYPE (inner_array);
12225 }
12226
12227 el_type = TYPE_TARGET_TYPE (inner_array);
12228 TYPE_TARGET_TYPE (inner_array) =
12229 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12230
12231 return set_die_type (die, base_type, cu);
12232 }
12233
12234 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12235 return set_die_type (die, cv_type, cu);
12236 }
12237
12238 static struct type *
12239 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12240 {
12241 struct type *base_type, *cv_type;
12242
12243 base_type = die_type (die, cu);
12244
12245 /* The die_type call above may have already set the type for this DIE. */
12246 cv_type = get_die_type (die, cu);
12247 if (cv_type)
12248 return cv_type;
12249
12250 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12251 return set_die_type (die, cv_type, cu);
12252 }
12253
12254 /* Extract all information from a DW_TAG_string_type DIE and add to
12255 the user defined type vector. It isn't really a user defined type,
12256 but it behaves like one, with other DIE's using an AT_user_def_type
12257 attribute to reference it. */
12258
12259 static struct type *
12260 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12261 {
12262 struct objfile *objfile = cu->objfile;
12263 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12264 struct type *type, *range_type, *index_type, *char_type;
12265 struct attribute *attr;
12266 unsigned int length;
12267
12268 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12269 if (attr)
12270 {
12271 length = DW_UNSND (attr);
12272 }
12273 else
12274 {
12275 /* Check for the DW_AT_byte_size attribute. */
12276 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12277 if (attr)
12278 {
12279 length = DW_UNSND (attr);
12280 }
12281 else
12282 {
12283 length = 1;
12284 }
12285 }
12286
12287 index_type = objfile_type (objfile)->builtin_int;
12288 range_type = create_range_type (NULL, index_type, 1, length);
12289 char_type = language_string_char_type (cu->language_defn, gdbarch);
12290 type = create_string_type (NULL, char_type, range_type);
12291
12292 return set_die_type (die, type, cu);
12293 }
12294
12295 /* Handle DIES due to C code like:
12296
12297 struct foo
12298 {
12299 int (*funcp)(int a, long l);
12300 int b;
12301 };
12302
12303 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12304
12305 static struct type *
12306 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12307 {
12308 struct objfile *objfile = cu->objfile;
12309 struct type *type; /* Type that this function returns. */
12310 struct type *ftype; /* Function that returns above type. */
12311 struct attribute *attr;
12312
12313 type = die_type (die, cu);
12314
12315 /* The die_type call above may have already set the type for this DIE. */
12316 ftype = get_die_type (die, cu);
12317 if (ftype)
12318 return ftype;
12319
12320 ftype = lookup_function_type (type);
12321
12322 /* All functions in C++, Pascal and Java have prototypes. */
12323 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12324 if ((attr && (DW_UNSND (attr) != 0))
12325 || cu->language == language_cplus
12326 || cu->language == language_java
12327 || cu->language == language_pascal)
12328 TYPE_PROTOTYPED (ftype) = 1;
12329 else if (producer_is_realview (cu->producer))
12330 /* RealView does not emit DW_AT_prototyped. We can not
12331 distinguish prototyped and unprototyped functions; default to
12332 prototyped, since that is more common in modern code (and
12333 RealView warns about unprototyped functions). */
12334 TYPE_PROTOTYPED (ftype) = 1;
12335
12336 /* Store the calling convention in the type if it's available in
12337 the subroutine die. Otherwise set the calling convention to
12338 the default value DW_CC_normal. */
12339 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12340 if (attr)
12341 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12342 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12343 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12344 else
12345 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12346
12347 /* We need to add the subroutine type to the die immediately so
12348 we don't infinitely recurse when dealing with parameters
12349 declared as the same subroutine type. */
12350 set_die_type (die, ftype, cu);
12351
12352 if (die->child != NULL)
12353 {
12354 struct type *void_type = objfile_type (objfile)->builtin_void;
12355 struct die_info *child_die;
12356 int nparams, iparams;
12357
12358 /* Count the number of parameters.
12359 FIXME: GDB currently ignores vararg functions, but knows about
12360 vararg member functions. */
12361 nparams = 0;
12362 child_die = die->child;
12363 while (child_die && child_die->tag)
12364 {
12365 if (child_die->tag == DW_TAG_formal_parameter)
12366 nparams++;
12367 else if (child_die->tag == DW_TAG_unspecified_parameters)
12368 TYPE_VARARGS (ftype) = 1;
12369 child_die = sibling_die (child_die);
12370 }
12371
12372 /* Allocate storage for parameters and fill them in. */
12373 TYPE_NFIELDS (ftype) = nparams;
12374 TYPE_FIELDS (ftype) = (struct field *)
12375 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12376
12377 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12378 even if we error out during the parameters reading below. */
12379 for (iparams = 0; iparams < nparams; iparams++)
12380 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12381
12382 iparams = 0;
12383 child_die = die->child;
12384 while (child_die && child_die->tag)
12385 {
12386 if (child_die->tag == DW_TAG_formal_parameter)
12387 {
12388 struct type *arg_type;
12389
12390 /* DWARF version 2 has no clean way to discern C++
12391 static and non-static member functions. G++ helps
12392 GDB by marking the first parameter for non-static
12393 member functions (which is the this pointer) as
12394 artificial. We pass this information to
12395 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12396
12397 DWARF version 3 added DW_AT_object_pointer, which GCC
12398 4.5 does not yet generate. */
12399 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12400 if (attr)
12401 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12402 else
12403 {
12404 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12405
12406 /* GCC/43521: In java, the formal parameter
12407 "this" is sometimes not marked with DW_AT_artificial. */
12408 if (cu->language == language_java)
12409 {
12410 const char *name = dwarf2_name (child_die, cu);
12411
12412 if (name && !strcmp (name, "this"))
12413 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12414 }
12415 }
12416 arg_type = die_type (child_die, cu);
12417
12418 /* RealView does not mark THIS as const, which the testsuite
12419 expects. GCC marks THIS as const in method definitions,
12420 but not in the class specifications (GCC PR 43053). */
12421 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12422 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12423 {
12424 int is_this = 0;
12425 struct dwarf2_cu *arg_cu = cu;
12426 const char *name = dwarf2_name (child_die, cu);
12427
12428 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12429 if (attr)
12430 {
12431 /* If the compiler emits this, use it. */
12432 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12433 is_this = 1;
12434 }
12435 else if (name && strcmp (name, "this") == 0)
12436 /* Function definitions will have the argument names. */
12437 is_this = 1;
12438 else if (name == NULL && iparams == 0)
12439 /* Declarations may not have the names, so like
12440 elsewhere in GDB, assume an artificial first
12441 argument is "this". */
12442 is_this = 1;
12443
12444 if (is_this)
12445 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12446 arg_type, 0);
12447 }
12448
12449 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12450 iparams++;
12451 }
12452 child_die = sibling_die (child_die);
12453 }
12454 }
12455
12456 return ftype;
12457 }
12458
12459 static struct type *
12460 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12461 {
12462 struct objfile *objfile = cu->objfile;
12463 const char *name = NULL;
12464 struct type *this_type, *target_type;
12465
12466 name = dwarf2_full_name (NULL, die, cu);
12467 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12468 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12469 TYPE_NAME (this_type) = (char *) name;
12470 set_die_type (die, this_type, cu);
12471 target_type = die_type (die, cu);
12472 if (target_type != this_type)
12473 TYPE_TARGET_TYPE (this_type) = target_type;
12474 else
12475 {
12476 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12477 spec and cause infinite loops in GDB. */
12478 complaint (&symfile_complaints,
12479 _("Self-referential DW_TAG_typedef "
12480 "- DIE at 0x%x [in module %s]"),
12481 die->offset.sect_off, objfile->name);
12482 TYPE_TARGET_TYPE (this_type) = NULL;
12483 }
12484 return this_type;
12485 }
12486
12487 /* Find a representation of a given base type and install
12488 it in the TYPE field of the die. */
12489
12490 static struct type *
12491 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12492 {
12493 struct objfile *objfile = cu->objfile;
12494 struct type *type;
12495 struct attribute *attr;
12496 int encoding = 0, size = 0;
12497 char *name;
12498 enum type_code code = TYPE_CODE_INT;
12499 int type_flags = 0;
12500 struct type *target_type = NULL;
12501
12502 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12503 if (attr)
12504 {
12505 encoding = DW_UNSND (attr);
12506 }
12507 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12508 if (attr)
12509 {
12510 size = DW_UNSND (attr);
12511 }
12512 name = dwarf2_name (die, cu);
12513 if (!name)
12514 {
12515 complaint (&symfile_complaints,
12516 _("DW_AT_name missing from DW_TAG_base_type"));
12517 }
12518
12519 switch (encoding)
12520 {
12521 case DW_ATE_address:
12522 /* Turn DW_ATE_address into a void * pointer. */
12523 code = TYPE_CODE_PTR;
12524 type_flags |= TYPE_FLAG_UNSIGNED;
12525 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12526 break;
12527 case DW_ATE_boolean:
12528 code = TYPE_CODE_BOOL;
12529 type_flags |= TYPE_FLAG_UNSIGNED;
12530 break;
12531 case DW_ATE_complex_float:
12532 code = TYPE_CODE_COMPLEX;
12533 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12534 break;
12535 case DW_ATE_decimal_float:
12536 code = TYPE_CODE_DECFLOAT;
12537 break;
12538 case DW_ATE_float:
12539 code = TYPE_CODE_FLT;
12540 break;
12541 case DW_ATE_signed:
12542 break;
12543 case DW_ATE_unsigned:
12544 type_flags |= TYPE_FLAG_UNSIGNED;
12545 if (cu->language == language_fortran
12546 && name
12547 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12548 code = TYPE_CODE_CHAR;
12549 break;
12550 case DW_ATE_signed_char:
12551 if (cu->language == language_ada || cu->language == language_m2
12552 || cu->language == language_pascal
12553 || cu->language == language_fortran)
12554 code = TYPE_CODE_CHAR;
12555 break;
12556 case DW_ATE_unsigned_char:
12557 if (cu->language == language_ada || cu->language == language_m2
12558 || cu->language == language_pascal
12559 || cu->language == language_fortran)
12560 code = TYPE_CODE_CHAR;
12561 type_flags |= TYPE_FLAG_UNSIGNED;
12562 break;
12563 case DW_ATE_UTF:
12564 /* We just treat this as an integer and then recognize the
12565 type by name elsewhere. */
12566 break;
12567
12568 default:
12569 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12570 dwarf_type_encoding_name (encoding));
12571 break;
12572 }
12573
12574 type = init_type (code, size, type_flags, NULL, objfile);
12575 TYPE_NAME (type) = name;
12576 TYPE_TARGET_TYPE (type) = target_type;
12577
12578 if (name && strcmp (name, "char") == 0)
12579 TYPE_NOSIGN (type) = 1;
12580
12581 return set_die_type (die, type, cu);
12582 }
12583
12584 /* Read the given DW_AT_subrange DIE. */
12585
12586 static struct type *
12587 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12588 {
12589 struct type *base_type;
12590 struct type *range_type;
12591 struct attribute *attr;
12592 LONGEST low, high;
12593 int low_default_is_valid;
12594 char *name;
12595 LONGEST negative_mask;
12596
12597 base_type = die_type (die, cu);
12598 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12599 check_typedef (base_type);
12600
12601 /* The die_type call above may have already set the type for this DIE. */
12602 range_type = get_die_type (die, cu);
12603 if (range_type)
12604 return range_type;
12605
12606 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12607 omitting DW_AT_lower_bound. */
12608 switch (cu->language)
12609 {
12610 case language_c:
12611 case language_cplus:
12612 low = 0;
12613 low_default_is_valid = 1;
12614 break;
12615 case language_fortran:
12616 low = 1;
12617 low_default_is_valid = 1;
12618 break;
12619 case language_d:
12620 case language_java:
12621 case language_objc:
12622 low = 0;
12623 low_default_is_valid = (cu->header.version >= 4);
12624 break;
12625 case language_ada:
12626 case language_m2:
12627 case language_pascal:
12628 low = 1;
12629 low_default_is_valid = (cu->header.version >= 4);
12630 break;
12631 default:
12632 low = 0;
12633 low_default_is_valid = 0;
12634 break;
12635 }
12636
12637 /* FIXME: For variable sized arrays either of these could be
12638 a variable rather than a constant value. We'll allow it,
12639 but we don't know how to handle it. */
12640 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12641 if (attr)
12642 low = dwarf2_get_attr_constant_value (attr, low);
12643 else if (!low_default_is_valid)
12644 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12645 "- DIE at 0x%x [in module %s]"),
12646 die->offset.sect_off, cu->objfile->name);
12647
12648 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12649 if (attr)
12650 {
12651 if (attr_form_is_block (attr) || is_ref_attr (attr))
12652 {
12653 /* GCC encodes arrays with unspecified or dynamic length
12654 with a DW_FORM_block1 attribute or a reference attribute.
12655 FIXME: GDB does not yet know how to handle dynamic
12656 arrays properly, treat them as arrays with unspecified
12657 length for now.
12658
12659 FIXME: jimb/2003-09-22: GDB does not really know
12660 how to handle arrays of unspecified length
12661 either; we just represent them as zero-length
12662 arrays. Choose an appropriate upper bound given
12663 the lower bound we've computed above. */
12664 high = low - 1;
12665 }
12666 else
12667 high = dwarf2_get_attr_constant_value (attr, 1);
12668 }
12669 else
12670 {
12671 attr = dwarf2_attr (die, DW_AT_count, cu);
12672 if (attr)
12673 {
12674 int count = dwarf2_get_attr_constant_value (attr, 1);
12675 high = low + count - 1;
12676 }
12677 else
12678 {
12679 /* Unspecified array length. */
12680 high = low - 1;
12681 }
12682 }
12683
12684 /* Dwarf-2 specifications explicitly allows to create subrange types
12685 without specifying a base type.
12686 In that case, the base type must be set to the type of
12687 the lower bound, upper bound or count, in that order, if any of these
12688 three attributes references an object that has a type.
12689 If no base type is found, the Dwarf-2 specifications say that
12690 a signed integer type of size equal to the size of an address should
12691 be used.
12692 For the following C code: `extern char gdb_int [];'
12693 GCC produces an empty range DIE.
12694 FIXME: muller/2010-05-28: Possible references to object for low bound,
12695 high bound or count are not yet handled by this code. */
12696 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12697 {
12698 struct objfile *objfile = cu->objfile;
12699 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12700 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12701 struct type *int_type = objfile_type (objfile)->builtin_int;
12702
12703 /* Test "int", "long int", and "long long int" objfile types,
12704 and select the first one having a size above or equal to the
12705 architecture address size. */
12706 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12707 base_type = int_type;
12708 else
12709 {
12710 int_type = objfile_type (objfile)->builtin_long;
12711 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12712 base_type = int_type;
12713 else
12714 {
12715 int_type = objfile_type (objfile)->builtin_long_long;
12716 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12717 base_type = int_type;
12718 }
12719 }
12720 }
12721
12722 negative_mask =
12723 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12724 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12725 low |= negative_mask;
12726 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12727 high |= negative_mask;
12728
12729 range_type = create_range_type (NULL, base_type, low, high);
12730
12731 /* Mark arrays with dynamic length at least as an array of unspecified
12732 length. GDB could check the boundary but before it gets implemented at
12733 least allow accessing the array elements. */
12734 if (attr && attr_form_is_block (attr))
12735 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12736
12737 /* Ada expects an empty array on no boundary attributes. */
12738 if (attr == NULL && cu->language != language_ada)
12739 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12740
12741 name = dwarf2_name (die, cu);
12742 if (name)
12743 TYPE_NAME (range_type) = name;
12744
12745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12746 if (attr)
12747 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12748
12749 set_die_type (die, range_type, cu);
12750
12751 /* set_die_type should be already done. */
12752 set_descriptive_type (range_type, die, cu);
12753
12754 return range_type;
12755 }
12756
12757 static struct type *
12758 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12759 {
12760 struct type *type;
12761
12762 /* For now, we only support the C meaning of an unspecified type: void. */
12763
12764 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12765 TYPE_NAME (type) = dwarf2_name (die, cu);
12766
12767 return set_die_type (die, type, cu);
12768 }
12769
12770 /* Read a single die and all its descendents. Set the die's sibling
12771 field to NULL; set other fields in the die correctly, and set all
12772 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12773 location of the info_ptr after reading all of those dies. PARENT
12774 is the parent of the die in question. */
12775
12776 static struct die_info *
12777 read_die_and_children (const struct die_reader_specs *reader,
12778 gdb_byte *info_ptr,
12779 gdb_byte **new_info_ptr,
12780 struct die_info *parent)
12781 {
12782 struct die_info *die;
12783 gdb_byte *cur_ptr;
12784 int has_children;
12785
12786 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12787 if (die == NULL)
12788 {
12789 *new_info_ptr = cur_ptr;
12790 return NULL;
12791 }
12792 store_in_ref_table (die, reader->cu);
12793
12794 if (has_children)
12795 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12796 else
12797 {
12798 die->child = NULL;
12799 *new_info_ptr = cur_ptr;
12800 }
12801
12802 die->sibling = NULL;
12803 die->parent = parent;
12804 return die;
12805 }
12806
12807 /* Read a die, all of its descendents, and all of its siblings; set
12808 all of the fields of all of the dies correctly. Arguments are as
12809 in read_die_and_children. */
12810
12811 static struct die_info *
12812 read_die_and_siblings (const struct die_reader_specs *reader,
12813 gdb_byte *info_ptr,
12814 gdb_byte **new_info_ptr,
12815 struct die_info *parent)
12816 {
12817 struct die_info *first_die, *last_sibling;
12818 gdb_byte *cur_ptr;
12819
12820 cur_ptr = info_ptr;
12821 first_die = last_sibling = NULL;
12822
12823 while (1)
12824 {
12825 struct die_info *die
12826 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12827
12828 if (die == NULL)
12829 {
12830 *new_info_ptr = cur_ptr;
12831 return first_die;
12832 }
12833
12834 if (!first_die)
12835 first_die = die;
12836 else
12837 last_sibling->sibling = die;
12838
12839 last_sibling = die;
12840 }
12841 }
12842
12843 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12844 attributes.
12845 The caller is responsible for filling in the extra attributes
12846 and updating (*DIEP)->num_attrs.
12847 Set DIEP to point to a newly allocated die with its information,
12848 except for its child, sibling, and parent fields.
12849 Set HAS_CHILDREN to tell whether the die has children or not. */
12850
12851 static gdb_byte *
12852 read_full_die_1 (const struct die_reader_specs *reader,
12853 struct die_info **diep, gdb_byte *info_ptr,
12854 int *has_children, int num_extra_attrs)
12855 {
12856 unsigned int abbrev_number, bytes_read, i;
12857 sect_offset offset;
12858 struct abbrev_info *abbrev;
12859 struct die_info *die;
12860 struct dwarf2_cu *cu = reader->cu;
12861 bfd *abfd = reader->abfd;
12862
12863 offset.sect_off = info_ptr - reader->buffer;
12864 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12865 info_ptr += bytes_read;
12866 if (!abbrev_number)
12867 {
12868 *diep = NULL;
12869 *has_children = 0;
12870 return info_ptr;
12871 }
12872
12873 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12874 if (!abbrev)
12875 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12876 abbrev_number,
12877 bfd_get_filename (abfd));
12878
12879 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12880 die->offset = offset;
12881 die->tag = abbrev->tag;
12882 die->abbrev = abbrev_number;
12883
12884 /* Make the result usable.
12885 The caller needs to update num_attrs after adding the extra
12886 attributes. */
12887 die->num_attrs = abbrev->num_attrs;
12888
12889 for (i = 0; i < abbrev->num_attrs; ++i)
12890 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12891 info_ptr);
12892
12893 *diep = die;
12894 *has_children = abbrev->has_children;
12895 return info_ptr;
12896 }
12897
12898 /* Read a die and all its attributes.
12899 Set DIEP to point to a newly allocated die with its information,
12900 except for its child, sibling, and parent fields.
12901 Set HAS_CHILDREN to tell whether the die has children or not. */
12902
12903 static gdb_byte *
12904 read_full_die (const struct die_reader_specs *reader,
12905 struct die_info **diep, gdb_byte *info_ptr,
12906 int *has_children)
12907 {
12908 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12909 }
12910 \f
12911 /* Abbreviation tables.
12912
12913 In DWARF version 2, the description of the debugging information is
12914 stored in a separate .debug_abbrev section. Before we read any
12915 dies from a section we read in all abbreviations and install them
12916 in a hash table. */
12917
12918 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12919
12920 static struct abbrev_info *
12921 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12922 {
12923 struct abbrev_info *abbrev;
12924
12925 abbrev = (struct abbrev_info *)
12926 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12927 memset (abbrev, 0, sizeof (struct abbrev_info));
12928 return abbrev;
12929 }
12930
12931 /* Add an abbreviation to the table. */
12932
12933 static void
12934 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12935 unsigned int abbrev_number,
12936 struct abbrev_info *abbrev)
12937 {
12938 unsigned int hash_number;
12939
12940 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12941 abbrev->next = abbrev_table->abbrevs[hash_number];
12942 abbrev_table->abbrevs[hash_number] = abbrev;
12943 }
12944
12945 /* Look up an abbrev in the table.
12946 Returns NULL if the abbrev is not found. */
12947
12948 static struct abbrev_info *
12949 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12950 unsigned int abbrev_number)
12951 {
12952 unsigned int hash_number;
12953 struct abbrev_info *abbrev;
12954
12955 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12956 abbrev = abbrev_table->abbrevs[hash_number];
12957
12958 while (abbrev)
12959 {
12960 if (abbrev->number == abbrev_number)
12961 return abbrev;
12962 abbrev = abbrev->next;
12963 }
12964 return NULL;
12965 }
12966
12967 /* Read in an abbrev table. */
12968
12969 static struct abbrev_table *
12970 abbrev_table_read_table (struct dwarf2_section_info *section,
12971 sect_offset offset)
12972 {
12973 struct objfile *objfile = dwarf2_per_objfile->objfile;
12974 bfd *abfd = section->asection->owner;
12975 struct abbrev_table *abbrev_table;
12976 gdb_byte *abbrev_ptr;
12977 struct abbrev_info *cur_abbrev;
12978 unsigned int abbrev_number, bytes_read, abbrev_name;
12979 unsigned int abbrev_form;
12980 struct attr_abbrev *cur_attrs;
12981 unsigned int allocated_attrs;
12982
12983 abbrev_table = XMALLOC (struct abbrev_table);
12984 abbrev_table->offset = offset;
12985 obstack_init (&abbrev_table->abbrev_obstack);
12986 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12987 (ABBREV_HASH_SIZE
12988 * sizeof (struct abbrev_info *)));
12989 memset (abbrev_table->abbrevs, 0,
12990 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12991
12992 dwarf2_read_section (objfile, section);
12993 abbrev_ptr = section->buffer + offset.sect_off;
12994 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12995 abbrev_ptr += bytes_read;
12996
12997 allocated_attrs = ATTR_ALLOC_CHUNK;
12998 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12999
13000 /* Loop until we reach an abbrev number of 0. */
13001 while (abbrev_number)
13002 {
13003 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13004
13005 /* read in abbrev header */
13006 cur_abbrev->number = abbrev_number;
13007 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13008 abbrev_ptr += bytes_read;
13009 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13010 abbrev_ptr += 1;
13011
13012 /* now read in declarations */
13013 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13014 abbrev_ptr += bytes_read;
13015 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13016 abbrev_ptr += bytes_read;
13017 while (abbrev_name)
13018 {
13019 if (cur_abbrev->num_attrs == allocated_attrs)
13020 {
13021 allocated_attrs += ATTR_ALLOC_CHUNK;
13022 cur_attrs
13023 = xrealloc (cur_attrs, (allocated_attrs
13024 * sizeof (struct attr_abbrev)));
13025 }
13026
13027 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13028 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13029 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13030 abbrev_ptr += bytes_read;
13031 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13032 abbrev_ptr += bytes_read;
13033 }
13034
13035 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13036 (cur_abbrev->num_attrs
13037 * sizeof (struct attr_abbrev)));
13038 memcpy (cur_abbrev->attrs, cur_attrs,
13039 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13040
13041 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13042
13043 /* Get next abbreviation.
13044 Under Irix6 the abbreviations for a compilation unit are not
13045 always properly terminated with an abbrev number of 0.
13046 Exit loop if we encounter an abbreviation which we have
13047 already read (which means we are about to read the abbreviations
13048 for the next compile unit) or if the end of the abbreviation
13049 table is reached. */
13050 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13051 break;
13052 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13053 abbrev_ptr += bytes_read;
13054 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13055 break;
13056 }
13057
13058 xfree (cur_attrs);
13059 return abbrev_table;
13060 }
13061
13062 /* Free the resources held by ABBREV_TABLE. */
13063
13064 static void
13065 abbrev_table_free (struct abbrev_table *abbrev_table)
13066 {
13067 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13068 xfree (abbrev_table);
13069 }
13070
13071 /* Same as abbrev_table_free but as a cleanup.
13072 We pass in a pointer to the pointer to the table so that we can
13073 set the pointer to NULL when we're done. It also simplifies
13074 build_type_unit_groups. */
13075
13076 static void
13077 abbrev_table_free_cleanup (void *table_ptr)
13078 {
13079 struct abbrev_table **abbrev_table_ptr = table_ptr;
13080
13081 if (*abbrev_table_ptr != NULL)
13082 abbrev_table_free (*abbrev_table_ptr);
13083 *abbrev_table_ptr = NULL;
13084 }
13085
13086 /* Read the abbrev table for CU from ABBREV_SECTION. */
13087
13088 static void
13089 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13090 struct dwarf2_section_info *abbrev_section)
13091 {
13092 cu->abbrev_table =
13093 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13094 }
13095
13096 /* Release the memory used by the abbrev table for a compilation unit. */
13097
13098 static void
13099 dwarf2_free_abbrev_table (void *ptr_to_cu)
13100 {
13101 struct dwarf2_cu *cu = ptr_to_cu;
13102
13103 abbrev_table_free (cu->abbrev_table);
13104 /* Set this to NULL so that we SEGV if we try to read it later,
13105 and also because free_comp_unit verifies this is NULL. */
13106 cu->abbrev_table = NULL;
13107 }
13108 \f
13109 /* Returns nonzero if TAG represents a type that we might generate a partial
13110 symbol for. */
13111
13112 static int
13113 is_type_tag_for_partial (int tag)
13114 {
13115 switch (tag)
13116 {
13117 #if 0
13118 /* Some types that would be reasonable to generate partial symbols for,
13119 that we don't at present. */
13120 case DW_TAG_array_type:
13121 case DW_TAG_file_type:
13122 case DW_TAG_ptr_to_member_type:
13123 case DW_TAG_set_type:
13124 case DW_TAG_string_type:
13125 case DW_TAG_subroutine_type:
13126 #endif
13127 case DW_TAG_base_type:
13128 case DW_TAG_class_type:
13129 case DW_TAG_interface_type:
13130 case DW_TAG_enumeration_type:
13131 case DW_TAG_structure_type:
13132 case DW_TAG_subrange_type:
13133 case DW_TAG_typedef:
13134 case DW_TAG_union_type:
13135 return 1;
13136 default:
13137 return 0;
13138 }
13139 }
13140
13141 /* Load all DIEs that are interesting for partial symbols into memory. */
13142
13143 static struct partial_die_info *
13144 load_partial_dies (const struct die_reader_specs *reader,
13145 gdb_byte *info_ptr, int building_psymtab)
13146 {
13147 struct dwarf2_cu *cu = reader->cu;
13148 struct objfile *objfile = cu->objfile;
13149 struct partial_die_info *part_die;
13150 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13151 struct abbrev_info *abbrev;
13152 unsigned int bytes_read;
13153 unsigned int load_all = 0;
13154 int nesting_level = 1;
13155
13156 parent_die = NULL;
13157 last_die = NULL;
13158
13159 gdb_assert (cu->per_cu != NULL);
13160 if (cu->per_cu->load_all_dies)
13161 load_all = 1;
13162
13163 cu->partial_dies
13164 = htab_create_alloc_ex (cu->header.length / 12,
13165 partial_die_hash,
13166 partial_die_eq,
13167 NULL,
13168 &cu->comp_unit_obstack,
13169 hashtab_obstack_allocate,
13170 dummy_obstack_deallocate);
13171
13172 part_die = obstack_alloc (&cu->comp_unit_obstack,
13173 sizeof (struct partial_die_info));
13174
13175 while (1)
13176 {
13177 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13178
13179 /* A NULL abbrev means the end of a series of children. */
13180 if (abbrev == NULL)
13181 {
13182 if (--nesting_level == 0)
13183 {
13184 /* PART_DIE was probably the last thing allocated on the
13185 comp_unit_obstack, so we could call obstack_free
13186 here. We don't do that because the waste is small,
13187 and will be cleaned up when we're done with this
13188 compilation unit. This way, we're also more robust
13189 against other users of the comp_unit_obstack. */
13190 return first_die;
13191 }
13192 info_ptr += bytes_read;
13193 last_die = parent_die;
13194 parent_die = parent_die->die_parent;
13195 continue;
13196 }
13197
13198 /* Check for template arguments. We never save these; if
13199 they're seen, we just mark the parent, and go on our way. */
13200 if (parent_die != NULL
13201 && cu->language == language_cplus
13202 && (abbrev->tag == DW_TAG_template_type_param
13203 || abbrev->tag == DW_TAG_template_value_param))
13204 {
13205 parent_die->has_template_arguments = 1;
13206
13207 if (!load_all)
13208 {
13209 /* We don't need a partial DIE for the template argument. */
13210 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13211 continue;
13212 }
13213 }
13214
13215 /* We only recurse into c++ subprograms looking for template arguments.
13216 Skip their other children. */
13217 if (!load_all
13218 && cu->language == language_cplus
13219 && parent_die != NULL
13220 && parent_die->tag == DW_TAG_subprogram)
13221 {
13222 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13223 continue;
13224 }
13225
13226 /* Check whether this DIE is interesting enough to save. Normally
13227 we would not be interested in members here, but there may be
13228 later variables referencing them via DW_AT_specification (for
13229 static members). */
13230 if (!load_all
13231 && !is_type_tag_for_partial (abbrev->tag)
13232 && abbrev->tag != DW_TAG_constant
13233 && abbrev->tag != DW_TAG_enumerator
13234 && abbrev->tag != DW_TAG_subprogram
13235 && abbrev->tag != DW_TAG_lexical_block
13236 && abbrev->tag != DW_TAG_variable
13237 && abbrev->tag != DW_TAG_namespace
13238 && abbrev->tag != DW_TAG_module
13239 && abbrev->tag != DW_TAG_member
13240 && abbrev->tag != DW_TAG_imported_unit)
13241 {
13242 /* Otherwise we skip to the next sibling, if any. */
13243 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13244 continue;
13245 }
13246
13247 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13248 info_ptr);
13249
13250 /* This two-pass algorithm for processing partial symbols has a
13251 high cost in cache pressure. Thus, handle some simple cases
13252 here which cover the majority of C partial symbols. DIEs
13253 which neither have specification tags in them, nor could have
13254 specification tags elsewhere pointing at them, can simply be
13255 processed and discarded.
13256
13257 This segment is also optional; scan_partial_symbols and
13258 add_partial_symbol will handle these DIEs if we chain
13259 them in normally. When compilers which do not emit large
13260 quantities of duplicate debug information are more common,
13261 this code can probably be removed. */
13262
13263 /* Any complete simple types at the top level (pretty much all
13264 of them, for a language without namespaces), can be processed
13265 directly. */
13266 if (parent_die == NULL
13267 && part_die->has_specification == 0
13268 && part_die->is_declaration == 0
13269 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13270 || part_die->tag == DW_TAG_base_type
13271 || part_die->tag == DW_TAG_subrange_type))
13272 {
13273 if (building_psymtab && part_die->name != NULL)
13274 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13275 VAR_DOMAIN, LOC_TYPEDEF,
13276 &objfile->static_psymbols,
13277 0, (CORE_ADDR) 0, cu->language, objfile);
13278 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13279 continue;
13280 }
13281
13282 /* The exception for DW_TAG_typedef with has_children above is
13283 a workaround of GCC PR debug/47510. In the case of this complaint
13284 type_name_no_tag_or_error will error on such types later.
13285
13286 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13287 it could not find the child DIEs referenced later, this is checked
13288 above. In correct DWARF DW_TAG_typedef should have no children. */
13289
13290 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13291 complaint (&symfile_complaints,
13292 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13293 "- DIE at 0x%x [in module %s]"),
13294 part_die->offset.sect_off, objfile->name);
13295
13296 /* If we're at the second level, and we're an enumerator, and
13297 our parent has no specification (meaning possibly lives in a
13298 namespace elsewhere), then we can add the partial symbol now
13299 instead of queueing it. */
13300 if (part_die->tag == DW_TAG_enumerator
13301 && parent_die != NULL
13302 && parent_die->die_parent == NULL
13303 && parent_die->tag == DW_TAG_enumeration_type
13304 && parent_die->has_specification == 0)
13305 {
13306 if (part_die->name == NULL)
13307 complaint (&symfile_complaints,
13308 _("malformed enumerator DIE ignored"));
13309 else if (building_psymtab)
13310 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13311 VAR_DOMAIN, LOC_CONST,
13312 (cu->language == language_cplus
13313 || cu->language == language_java)
13314 ? &objfile->global_psymbols
13315 : &objfile->static_psymbols,
13316 0, (CORE_ADDR) 0, cu->language, objfile);
13317
13318 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13319 continue;
13320 }
13321
13322 /* We'll save this DIE so link it in. */
13323 part_die->die_parent = parent_die;
13324 part_die->die_sibling = NULL;
13325 part_die->die_child = NULL;
13326
13327 if (last_die && last_die == parent_die)
13328 last_die->die_child = part_die;
13329 else if (last_die)
13330 last_die->die_sibling = part_die;
13331
13332 last_die = part_die;
13333
13334 if (first_die == NULL)
13335 first_die = part_die;
13336
13337 /* Maybe add the DIE to the hash table. Not all DIEs that we
13338 find interesting need to be in the hash table, because we
13339 also have the parent/sibling/child chains; only those that we
13340 might refer to by offset later during partial symbol reading.
13341
13342 For now this means things that might have be the target of a
13343 DW_AT_specification, DW_AT_abstract_origin, or
13344 DW_AT_extension. DW_AT_extension will refer only to
13345 namespaces; DW_AT_abstract_origin refers to functions (and
13346 many things under the function DIE, but we do not recurse
13347 into function DIEs during partial symbol reading) and
13348 possibly variables as well; DW_AT_specification refers to
13349 declarations. Declarations ought to have the DW_AT_declaration
13350 flag. It happens that GCC forgets to put it in sometimes, but
13351 only for functions, not for types.
13352
13353 Adding more things than necessary to the hash table is harmless
13354 except for the performance cost. Adding too few will result in
13355 wasted time in find_partial_die, when we reread the compilation
13356 unit with load_all_dies set. */
13357
13358 if (load_all
13359 || abbrev->tag == DW_TAG_constant
13360 || abbrev->tag == DW_TAG_subprogram
13361 || abbrev->tag == DW_TAG_variable
13362 || abbrev->tag == DW_TAG_namespace
13363 || part_die->is_declaration)
13364 {
13365 void **slot;
13366
13367 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13368 part_die->offset.sect_off, INSERT);
13369 *slot = part_die;
13370 }
13371
13372 part_die = obstack_alloc (&cu->comp_unit_obstack,
13373 sizeof (struct partial_die_info));
13374
13375 /* For some DIEs we want to follow their children (if any). For C
13376 we have no reason to follow the children of structures; for other
13377 languages we have to, so that we can get at method physnames
13378 to infer fully qualified class names, for DW_AT_specification,
13379 and for C++ template arguments. For C++, we also look one level
13380 inside functions to find template arguments (if the name of the
13381 function does not already contain the template arguments).
13382
13383 For Ada, we need to scan the children of subprograms and lexical
13384 blocks as well because Ada allows the definition of nested
13385 entities that could be interesting for the debugger, such as
13386 nested subprograms for instance. */
13387 if (last_die->has_children
13388 && (load_all
13389 || last_die->tag == DW_TAG_namespace
13390 || last_die->tag == DW_TAG_module
13391 || last_die->tag == DW_TAG_enumeration_type
13392 || (cu->language == language_cplus
13393 && last_die->tag == DW_TAG_subprogram
13394 && (last_die->name == NULL
13395 || strchr (last_die->name, '<') == NULL))
13396 || (cu->language != language_c
13397 && (last_die->tag == DW_TAG_class_type
13398 || last_die->tag == DW_TAG_interface_type
13399 || last_die->tag == DW_TAG_structure_type
13400 || last_die->tag == DW_TAG_union_type))
13401 || (cu->language == language_ada
13402 && (last_die->tag == DW_TAG_subprogram
13403 || last_die->tag == DW_TAG_lexical_block))))
13404 {
13405 nesting_level++;
13406 parent_die = last_die;
13407 continue;
13408 }
13409
13410 /* Otherwise we skip to the next sibling, if any. */
13411 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13412
13413 /* Back to the top, do it again. */
13414 }
13415 }
13416
13417 /* Read a minimal amount of information into the minimal die structure. */
13418
13419 static gdb_byte *
13420 read_partial_die (const struct die_reader_specs *reader,
13421 struct partial_die_info *part_die,
13422 struct abbrev_info *abbrev, unsigned int abbrev_len,
13423 gdb_byte *info_ptr)
13424 {
13425 struct dwarf2_cu *cu = reader->cu;
13426 struct objfile *objfile = cu->objfile;
13427 gdb_byte *buffer = reader->buffer;
13428 unsigned int i;
13429 struct attribute attr;
13430 int has_low_pc_attr = 0;
13431 int has_high_pc_attr = 0;
13432 int high_pc_relative = 0;
13433
13434 memset (part_die, 0, sizeof (struct partial_die_info));
13435
13436 part_die->offset.sect_off = info_ptr - buffer;
13437
13438 info_ptr += abbrev_len;
13439
13440 if (abbrev == NULL)
13441 return info_ptr;
13442
13443 part_die->tag = abbrev->tag;
13444 part_die->has_children = abbrev->has_children;
13445
13446 for (i = 0; i < abbrev->num_attrs; ++i)
13447 {
13448 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13449
13450 /* Store the data if it is of an attribute we want to keep in a
13451 partial symbol table. */
13452 switch (attr.name)
13453 {
13454 case DW_AT_name:
13455 switch (part_die->tag)
13456 {
13457 case DW_TAG_compile_unit:
13458 case DW_TAG_partial_unit:
13459 case DW_TAG_type_unit:
13460 /* Compilation units have a DW_AT_name that is a filename, not
13461 a source language identifier. */
13462 case DW_TAG_enumeration_type:
13463 case DW_TAG_enumerator:
13464 /* These tags always have simple identifiers already; no need
13465 to canonicalize them. */
13466 part_die->name = DW_STRING (&attr);
13467 break;
13468 default:
13469 part_die->name
13470 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13471 &objfile->objfile_obstack);
13472 break;
13473 }
13474 break;
13475 case DW_AT_linkage_name:
13476 case DW_AT_MIPS_linkage_name:
13477 /* Note that both forms of linkage name might appear. We
13478 assume they will be the same, and we only store the last
13479 one we see. */
13480 if (cu->language == language_ada)
13481 part_die->name = DW_STRING (&attr);
13482 part_die->linkage_name = DW_STRING (&attr);
13483 break;
13484 case DW_AT_low_pc:
13485 has_low_pc_attr = 1;
13486 part_die->lowpc = DW_ADDR (&attr);
13487 break;
13488 case DW_AT_high_pc:
13489 has_high_pc_attr = 1;
13490 if (attr.form == DW_FORM_addr
13491 || attr.form == DW_FORM_GNU_addr_index)
13492 part_die->highpc = DW_ADDR (&attr);
13493 else
13494 {
13495 high_pc_relative = 1;
13496 part_die->highpc = DW_UNSND (&attr);
13497 }
13498 break;
13499 case DW_AT_location:
13500 /* Support the .debug_loc offsets. */
13501 if (attr_form_is_block (&attr))
13502 {
13503 part_die->d.locdesc = DW_BLOCK (&attr);
13504 }
13505 else if (attr_form_is_section_offset (&attr))
13506 {
13507 dwarf2_complex_location_expr_complaint ();
13508 }
13509 else
13510 {
13511 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13512 "partial symbol information");
13513 }
13514 break;
13515 case DW_AT_external:
13516 part_die->is_external = DW_UNSND (&attr);
13517 break;
13518 case DW_AT_declaration:
13519 part_die->is_declaration = DW_UNSND (&attr);
13520 break;
13521 case DW_AT_type:
13522 part_die->has_type = 1;
13523 break;
13524 case DW_AT_abstract_origin:
13525 case DW_AT_specification:
13526 case DW_AT_extension:
13527 part_die->has_specification = 1;
13528 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13529 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13530 || cu->per_cu->is_dwz);
13531 break;
13532 case DW_AT_sibling:
13533 /* Ignore absolute siblings, they might point outside of
13534 the current compile unit. */
13535 if (attr.form == DW_FORM_ref_addr)
13536 complaint (&symfile_complaints,
13537 _("ignoring absolute DW_AT_sibling"));
13538 else
13539 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13540 break;
13541 case DW_AT_byte_size:
13542 part_die->has_byte_size = 1;
13543 break;
13544 case DW_AT_calling_convention:
13545 /* DWARF doesn't provide a way to identify a program's source-level
13546 entry point. DW_AT_calling_convention attributes are only meant
13547 to describe functions' calling conventions.
13548
13549 However, because it's a necessary piece of information in
13550 Fortran, and because DW_CC_program is the only piece of debugging
13551 information whose definition refers to a 'main program' at all,
13552 several compilers have begun marking Fortran main programs with
13553 DW_CC_program --- even when those functions use the standard
13554 calling conventions.
13555
13556 So until DWARF specifies a way to provide this information and
13557 compilers pick up the new representation, we'll support this
13558 practice. */
13559 if (DW_UNSND (&attr) == DW_CC_program
13560 && cu->language == language_fortran)
13561 {
13562 set_main_name (part_die->name);
13563
13564 /* As this DIE has a static linkage the name would be difficult
13565 to look up later. */
13566 language_of_main = language_fortran;
13567 }
13568 break;
13569 case DW_AT_inline:
13570 if (DW_UNSND (&attr) == DW_INL_inlined
13571 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13572 part_die->may_be_inlined = 1;
13573 break;
13574
13575 case DW_AT_import:
13576 if (part_die->tag == DW_TAG_imported_unit)
13577 {
13578 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13579 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13580 || cu->per_cu->is_dwz);
13581 }
13582 break;
13583
13584 default:
13585 break;
13586 }
13587 }
13588
13589 if (high_pc_relative)
13590 part_die->highpc += part_die->lowpc;
13591
13592 if (has_low_pc_attr && has_high_pc_attr)
13593 {
13594 /* When using the GNU linker, .gnu.linkonce. sections are used to
13595 eliminate duplicate copies of functions and vtables and such.
13596 The linker will arbitrarily choose one and discard the others.
13597 The AT_*_pc values for such functions refer to local labels in
13598 these sections. If the section from that file was discarded, the
13599 labels are not in the output, so the relocs get a value of 0.
13600 If this is a discarded function, mark the pc bounds as invalid,
13601 so that GDB will ignore it. */
13602 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13603 {
13604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13605
13606 complaint (&symfile_complaints,
13607 _("DW_AT_low_pc %s is zero "
13608 "for DIE at 0x%x [in module %s]"),
13609 paddress (gdbarch, part_die->lowpc),
13610 part_die->offset.sect_off, objfile->name);
13611 }
13612 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13613 else if (part_die->lowpc >= part_die->highpc)
13614 {
13615 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13616
13617 complaint (&symfile_complaints,
13618 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13619 "for DIE at 0x%x [in module %s]"),
13620 paddress (gdbarch, part_die->lowpc),
13621 paddress (gdbarch, part_die->highpc),
13622 part_die->offset.sect_off, objfile->name);
13623 }
13624 else
13625 part_die->has_pc_info = 1;
13626 }
13627
13628 return info_ptr;
13629 }
13630
13631 /* Find a cached partial DIE at OFFSET in CU. */
13632
13633 static struct partial_die_info *
13634 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13635 {
13636 struct partial_die_info *lookup_die = NULL;
13637 struct partial_die_info part_die;
13638
13639 part_die.offset = offset;
13640 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13641 offset.sect_off);
13642
13643 return lookup_die;
13644 }
13645
13646 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13647 except in the case of .debug_types DIEs which do not reference
13648 outside their CU (they do however referencing other types via
13649 DW_FORM_ref_sig8). */
13650
13651 static struct partial_die_info *
13652 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13653 {
13654 struct objfile *objfile = cu->objfile;
13655 struct dwarf2_per_cu_data *per_cu = NULL;
13656 struct partial_die_info *pd = NULL;
13657
13658 if (offset_in_dwz == cu->per_cu->is_dwz
13659 && offset_in_cu_p (&cu->header, offset))
13660 {
13661 pd = find_partial_die_in_comp_unit (offset, cu);
13662 if (pd != NULL)
13663 return pd;
13664 /* We missed recording what we needed.
13665 Load all dies and try again. */
13666 per_cu = cu->per_cu;
13667 }
13668 else
13669 {
13670 /* TUs don't reference other CUs/TUs (except via type signatures). */
13671 if (cu->per_cu->is_debug_types)
13672 {
13673 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13674 " external reference to offset 0x%lx [in module %s].\n"),
13675 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13676 bfd_get_filename (objfile->obfd));
13677 }
13678 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13679 objfile);
13680
13681 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13682 load_partial_comp_unit (per_cu);
13683
13684 per_cu->cu->last_used = 0;
13685 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13686 }
13687
13688 /* If we didn't find it, and not all dies have been loaded,
13689 load them all and try again. */
13690
13691 if (pd == NULL && per_cu->load_all_dies == 0)
13692 {
13693 per_cu->load_all_dies = 1;
13694
13695 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13696 THIS_CU->cu may already be in use. So we can't just free it and
13697 replace its DIEs with the ones we read in. Instead, we leave those
13698 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13699 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13700 set. */
13701 load_partial_comp_unit (per_cu);
13702
13703 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13704 }
13705
13706 if (pd == NULL)
13707 internal_error (__FILE__, __LINE__,
13708 _("could not find partial DIE 0x%x "
13709 "in cache [from module %s]\n"),
13710 offset.sect_off, bfd_get_filename (objfile->obfd));
13711 return pd;
13712 }
13713
13714 /* See if we can figure out if the class lives in a namespace. We do
13715 this by looking for a member function; its demangled name will
13716 contain namespace info, if there is any. */
13717
13718 static void
13719 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13720 struct dwarf2_cu *cu)
13721 {
13722 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13723 what template types look like, because the demangler
13724 frequently doesn't give the same name as the debug info. We
13725 could fix this by only using the demangled name to get the
13726 prefix (but see comment in read_structure_type). */
13727
13728 struct partial_die_info *real_pdi;
13729 struct partial_die_info *child_pdi;
13730
13731 /* If this DIE (this DIE's specification, if any) has a parent, then
13732 we should not do this. We'll prepend the parent's fully qualified
13733 name when we create the partial symbol. */
13734
13735 real_pdi = struct_pdi;
13736 while (real_pdi->has_specification)
13737 real_pdi = find_partial_die (real_pdi->spec_offset,
13738 real_pdi->spec_is_dwz, cu);
13739
13740 if (real_pdi->die_parent != NULL)
13741 return;
13742
13743 for (child_pdi = struct_pdi->die_child;
13744 child_pdi != NULL;
13745 child_pdi = child_pdi->die_sibling)
13746 {
13747 if (child_pdi->tag == DW_TAG_subprogram
13748 && child_pdi->linkage_name != NULL)
13749 {
13750 char *actual_class_name
13751 = language_class_name_from_physname (cu->language_defn,
13752 child_pdi->linkage_name);
13753 if (actual_class_name != NULL)
13754 {
13755 struct_pdi->name
13756 = obsavestring (actual_class_name,
13757 strlen (actual_class_name),
13758 &cu->objfile->objfile_obstack);
13759 xfree (actual_class_name);
13760 }
13761 break;
13762 }
13763 }
13764 }
13765
13766 /* Adjust PART_DIE before generating a symbol for it. This function
13767 may set the is_external flag or change the DIE's name. */
13768
13769 static void
13770 fixup_partial_die (struct partial_die_info *part_die,
13771 struct dwarf2_cu *cu)
13772 {
13773 /* Once we've fixed up a die, there's no point in doing so again.
13774 This also avoids a memory leak if we were to call
13775 guess_partial_die_structure_name multiple times. */
13776 if (part_die->fixup_called)
13777 return;
13778
13779 /* If we found a reference attribute and the DIE has no name, try
13780 to find a name in the referred to DIE. */
13781
13782 if (part_die->name == NULL && part_die->has_specification)
13783 {
13784 struct partial_die_info *spec_die;
13785
13786 spec_die = find_partial_die (part_die->spec_offset,
13787 part_die->spec_is_dwz, cu);
13788
13789 fixup_partial_die (spec_die, cu);
13790
13791 if (spec_die->name)
13792 {
13793 part_die->name = spec_die->name;
13794
13795 /* Copy DW_AT_external attribute if it is set. */
13796 if (spec_die->is_external)
13797 part_die->is_external = spec_die->is_external;
13798 }
13799 }
13800
13801 /* Set default names for some unnamed DIEs. */
13802
13803 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13804 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13805
13806 /* If there is no parent die to provide a namespace, and there are
13807 children, see if we can determine the namespace from their linkage
13808 name. */
13809 if (cu->language == language_cplus
13810 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13811 && part_die->die_parent == NULL
13812 && part_die->has_children
13813 && (part_die->tag == DW_TAG_class_type
13814 || part_die->tag == DW_TAG_structure_type
13815 || part_die->tag == DW_TAG_union_type))
13816 guess_partial_die_structure_name (part_die, cu);
13817
13818 /* GCC might emit a nameless struct or union that has a linkage
13819 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13820 if (part_die->name == NULL
13821 && (part_die->tag == DW_TAG_class_type
13822 || part_die->tag == DW_TAG_interface_type
13823 || part_die->tag == DW_TAG_structure_type
13824 || part_die->tag == DW_TAG_union_type)
13825 && part_die->linkage_name != NULL)
13826 {
13827 char *demangled;
13828
13829 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13830 if (demangled)
13831 {
13832 const char *base;
13833
13834 /* Strip any leading namespaces/classes, keep only the base name.
13835 DW_AT_name for named DIEs does not contain the prefixes. */
13836 base = strrchr (demangled, ':');
13837 if (base && base > demangled && base[-1] == ':')
13838 base++;
13839 else
13840 base = demangled;
13841
13842 part_die->name = obsavestring (base, strlen (base),
13843 &cu->objfile->objfile_obstack);
13844 xfree (demangled);
13845 }
13846 }
13847
13848 part_die->fixup_called = 1;
13849 }
13850
13851 /* Read an attribute value described by an attribute form. */
13852
13853 static gdb_byte *
13854 read_attribute_value (const struct die_reader_specs *reader,
13855 struct attribute *attr, unsigned form,
13856 gdb_byte *info_ptr)
13857 {
13858 struct dwarf2_cu *cu = reader->cu;
13859 bfd *abfd = reader->abfd;
13860 struct comp_unit_head *cu_header = &cu->header;
13861 unsigned int bytes_read;
13862 struct dwarf_block *blk;
13863
13864 attr->form = form;
13865 switch (form)
13866 {
13867 case DW_FORM_ref_addr:
13868 if (cu->header.version == 2)
13869 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13870 else
13871 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13872 &cu->header, &bytes_read);
13873 info_ptr += bytes_read;
13874 break;
13875 case DW_FORM_GNU_ref_alt:
13876 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13877 info_ptr += bytes_read;
13878 break;
13879 case DW_FORM_addr:
13880 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13881 info_ptr += bytes_read;
13882 break;
13883 case DW_FORM_block2:
13884 blk = dwarf_alloc_block (cu);
13885 blk->size = read_2_bytes (abfd, info_ptr);
13886 info_ptr += 2;
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_block4:
13892 blk = dwarf_alloc_block (cu);
13893 blk->size = read_4_bytes (abfd, info_ptr);
13894 info_ptr += 4;
13895 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13896 info_ptr += blk->size;
13897 DW_BLOCK (attr) = blk;
13898 break;
13899 case DW_FORM_data2:
13900 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13901 info_ptr += 2;
13902 break;
13903 case DW_FORM_data4:
13904 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13905 info_ptr += 4;
13906 break;
13907 case DW_FORM_data8:
13908 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13909 info_ptr += 8;
13910 break;
13911 case DW_FORM_sec_offset:
13912 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13913 info_ptr += bytes_read;
13914 break;
13915 case DW_FORM_string:
13916 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13917 DW_STRING_IS_CANONICAL (attr) = 0;
13918 info_ptr += bytes_read;
13919 break;
13920 case DW_FORM_strp:
13921 if (!cu->per_cu->is_dwz)
13922 {
13923 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13924 &bytes_read);
13925 DW_STRING_IS_CANONICAL (attr) = 0;
13926 info_ptr += bytes_read;
13927 break;
13928 }
13929 /* FALLTHROUGH */
13930 case DW_FORM_GNU_strp_alt:
13931 {
13932 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13933 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13934 &bytes_read);
13935
13936 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13937 DW_STRING_IS_CANONICAL (attr) = 0;
13938 info_ptr += bytes_read;
13939 }
13940 break;
13941 case DW_FORM_exprloc:
13942 case DW_FORM_block:
13943 blk = dwarf_alloc_block (cu);
13944 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13945 info_ptr += bytes_read;
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_block1:
13951 blk = dwarf_alloc_block (cu);
13952 blk->size = read_1_byte (abfd, info_ptr);
13953 info_ptr += 1;
13954 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13955 info_ptr += blk->size;
13956 DW_BLOCK (attr) = blk;
13957 break;
13958 case DW_FORM_data1:
13959 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13960 info_ptr += 1;
13961 break;
13962 case DW_FORM_flag:
13963 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13964 info_ptr += 1;
13965 break;
13966 case DW_FORM_flag_present:
13967 DW_UNSND (attr) = 1;
13968 break;
13969 case DW_FORM_sdata:
13970 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13971 info_ptr += bytes_read;
13972 break;
13973 case DW_FORM_udata:
13974 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13975 info_ptr += bytes_read;
13976 break;
13977 case DW_FORM_ref1:
13978 DW_UNSND (attr) = (cu->header.offset.sect_off
13979 + read_1_byte (abfd, info_ptr));
13980 info_ptr += 1;
13981 break;
13982 case DW_FORM_ref2:
13983 DW_UNSND (attr) = (cu->header.offset.sect_off
13984 + read_2_bytes (abfd, info_ptr));
13985 info_ptr += 2;
13986 break;
13987 case DW_FORM_ref4:
13988 DW_UNSND (attr) = (cu->header.offset.sect_off
13989 + read_4_bytes (abfd, info_ptr));
13990 info_ptr += 4;
13991 break;
13992 case DW_FORM_ref8:
13993 DW_UNSND (attr) = (cu->header.offset.sect_off
13994 + read_8_bytes (abfd, info_ptr));
13995 info_ptr += 8;
13996 break;
13997 case DW_FORM_ref_sig8:
13998 /* Convert the signature to something we can record in DW_UNSND
13999 for later lookup.
14000 NOTE: This is NULL if the type wasn't found. */
14001 DW_SIGNATURED_TYPE (attr) =
14002 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14003 info_ptr += 8;
14004 break;
14005 case DW_FORM_ref_udata:
14006 DW_UNSND (attr) = (cu->header.offset.sect_off
14007 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14008 info_ptr += bytes_read;
14009 break;
14010 case DW_FORM_indirect:
14011 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14012 info_ptr += bytes_read;
14013 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14014 break;
14015 case DW_FORM_GNU_addr_index:
14016 if (reader->dwo_file == NULL)
14017 {
14018 /* For now flag a hard error.
14019 Later we can turn this into a complaint. */
14020 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14021 dwarf_form_name (form),
14022 bfd_get_filename (abfd));
14023 }
14024 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14025 info_ptr += bytes_read;
14026 break;
14027 case DW_FORM_GNU_str_index:
14028 if (reader->dwo_file == NULL)
14029 {
14030 /* For now flag a hard error.
14031 Later we can turn this into a complaint if warranted. */
14032 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14033 dwarf_form_name (form),
14034 bfd_get_filename (abfd));
14035 }
14036 {
14037 ULONGEST str_index =
14038 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14039
14040 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14041 DW_STRING_IS_CANONICAL (attr) = 0;
14042 info_ptr += bytes_read;
14043 }
14044 break;
14045 default:
14046 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14047 dwarf_form_name (form),
14048 bfd_get_filename (abfd));
14049 }
14050
14051 /* Super hack. */
14052 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14053 attr->form = DW_FORM_GNU_ref_alt;
14054
14055 /* We have seen instances where the compiler tried to emit a byte
14056 size attribute of -1 which ended up being encoded as an unsigned
14057 0xffffffff. Although 0xffffffff is technically a valid size value,
14058 an object of this size seems pretty unlikely so we can relatively
14059 safely treat these cases as if the size attribute was invalid and
14060 treat them as zero by default. */
14061 if (attr->name == DW_AT_byte_size
14062 && form == DW_FORM_data4
14063 && DW_UNSND (attr) >= 0xffffffff)
14064 {
14065 complaint
14066 (&symfile_complaints,
14067 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14068 hex_string (DW_UNSND (attr)));
14069 DW_UNSND (attr) = 0;
14070 }
14071
14072 return info_ptr;
14073 }
14074
14075 /* Read an attribute described by an abbreviated attribute. */
14076
14077 static gdb_byte *
14078 read_attribute (const struct die_reader_specs *reader,
14079 struct attribute *attr, struct attr_abbrev *abbrev,
14080 gdb_byte *info_ptr)
14081 {
14082 attr->name = abbrev->name;
14083 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14084 }
14085
14086 /* Read dwarf information from a buffer. */
14087
14088 static unsigned int
14089 read_1_byte (bfd *abfd, const gdb_byte *buf)
14090 {
14091 return bfd_get_8 (abfd, buf);
14092 }
14093
14094 static int
14095 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14096 {
14097 return bfd_get_signed_8 (abfd, buf);
14098 }
14099
14100 static unsigned int
14101 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14102 {
14103 return bfd_get_16 (abfd, buf);
14104 }
14105
14106 static int
14107 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14108 {
14109 return bfd_get_signed_16 (abfd, buf);
14110 }
14111
14112 static unsigned int
14113 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14114 {
14115 return bfd_get_32 (abfd, buf);
14116 }
14117
14118 static int
14119 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14120 {
14121 return bfd_get_signed_32 (abfd, buf);
14122 }
14123
14124 static ULONGEST
14125 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14126 {
14127 return bfd_get_64 (abfd, buf);
14128 }
14129
14130 static CORE_ADDR
14131 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14132 unsigned int *bytes_read)
14133 {
14134 struct comp_unit_head *cu_header = &cu->header;
14135 CORE_ADDR retval = 0;
14136
14137 if (cu_header->signed_addr_p)
14138 {
14139 switch (cu_header->addr_size)
14140 {
14141 case 2:
14142 retval = bfd_get_signed_16 (abfd, buf);
14143 break;
14144 case 4:
14145 retval = bfd_get_signed_32 (abfd, buf);
14146 break;
14147 case 8:
14148 retval = bfd_get_signed_64 (abfd, buf);
14149 break;
14150 default:
14151 internal_error (__FILE__, __LINE__,
14152 _("read_address: bad switch, signed [in module %s]"),
14153 bfd_get_filename (abfd));
14154 }
14155 }
14156 else
14157 {
14158 switch (cu_header->addr_size)
14159 {
14160 case 2:
14161 retval = bfd_get_16 (abfd, buf);
14162 break;
14163 case 4:
14164 retval = bfd_get_32 (abfd, buf);
14165 break;
14166 case 8:
14167 retval = bfd_get_64 (abfd, buf);
14168 break;
14169 default:
14170 internal_error (__FILE__, __LINE__,
14171 _("read_address: bad switch, "
14172 "unsigned [in module %s]"),
14173 bfd_get_filename (abfd));
14174 }
14175 }
14176
14177 *bytes_read = cu_header->addr_size;
14178 return retval;
14179 }
14180
14181 /* Read the initial length from a section. The (draft) DWARF 3
14182 specification allows the initial length to take up either 4 bytes
14183 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14184 bytes describe the length and all offsets will be 8 bytes in length
14185 instead of 4.
14186
14187 An older, non-standard 64-bit format is also handled by this
14188 function. The older format in question stores the initial length
14189 as an 8-byte quantity without an escape value. Lengths greater
14190 than 2^32 aren't very common which means that the initial 4 bytes
14191 is almost always zero. Since a length value of zero doesn't make
14192 sense for the 32-bit format, this initial zero can be considered to
14193 be an escape value which indicates the presence of the older 64-bit
14194 format. As written, the code can't detect (old format) lengths
14195 greater than 4GB. If it becomes necessary to handle lengths
14196 somewhat larger than 4GB, we could allow other small values (such
14197 as the non-sensical values of 1, 2, and 3) to also be used as
14198 escape values indicating the presence of the old format.
14199
14200 The value returned via bytes_read should be used to increment the
14201 relevant pointer after calling read_initial_length().
14202
14203 [ Note: read_initial_length() and read_offset() are based on the
14204 document entitled "DWARF Debugging Information Format", revision
14205 3, draft 8, dated November 19, 2001. This document was obtained
14206 from:
14207
14208 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14209
14210 This document is only a draft and is subject to change. (So beware.)
14211
14212 Details regarding the older, non-standard 64-bit format were
14213 determined empirically by examining 64-bit ELF files produced by
14214 the SGI toolchain on an IRIX 6.5 machine.
14215
14216 - Kevin, July 16, 2002
14217 ] */
14218
14219 static LONGEST
14220 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14221 {
14222 LONGEST length = bfd_get_32 (abfd, buf);
14223
14224 if (length == 0xffffffff)
14225 {
14226 length = bfd_get_64 (abfd, buf + 4);
14227 *bytes_read = 12;
14228 }
14229 else if (length == 0)
14230 {
14231 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14232 length = bfd_get_64 (abfd, buf);
14233 *bytes_read = 8;
14234 }
14235 else
14236 {
14237 *bytes_read = 4;
14238 }
14239
14240 return length;
14241 }
14242
14243 /* Cover function for read_initial_length.
14244 Returns the length of the object at BUF, and stores the size of the
14245 initial length in *BYTES_READ and stores the size that offsets will be in
14246 *OFFSET_SIZE.
14247 If the initial length size is not equivalent to that specified in
14248 CU_HEADER then issue a complaint.
14249 This is useful when reading non-comp-unit headers. */
14250
14251 static LONGEST
14252 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14253 const struct comp_unit_head *cu_header,
14254 unsigned int *bytes_read,
14255 unsigned int *offset_size)
14256 {
14257 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14258
14259 gdb_assert (cu_header->initial_length_size == 4
14260 || cu_header->initial_length_size == 8
14261 || cu_header->initial_length_size == 12);
14262
14263 if (cu_header->initial_length_size != *bytes_read)
14264 complaint (&symfile_complaints,
14265 _("intermixed 32-bit and 64-bit DWARF sections"));
14266
14267 *offset_size = (*bytes_read == 4) ? 4 : 8;
14268 return length;
14269 }
14270
14271 /* Read an offset from the data stream. The size of the offset is
14272 given by cu_header->offset_size. */
14273
14274 static LONGEST
14275 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14276 unsigned int *bytes_read)
14277 {
14278 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14279
14280 *bytes_read = cu_header->offset_size;
14281 return offset;
14282 }
14283
14284 /* Read an offset from the data stream. */
14285
14286 static LONGEST
14287 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14288 {
14289 LONGEST retval = 0;
14290
14291 switch (offset_size)
14292 {
14293 case 4:
14294 retval = bfd_get_32 (abfd, buf);
14295 break;
14296 case 8:
14297 retval = bfd_get_64 (abfd, buf);
14298 break;
14299 default:
14300 internal_error (__FILE__, __LINE__,
14301 _("read_offset_1: bad switch [in module %s]"),
14302 bfd_get_filename (abfd));
14303 }
14304
14305 return retval;
14306 }
14307
14308 static gdb_byte *
14309 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14310 {
14311 /* If the size of a host char is 8 bits, we can return a pointer
14312 to the buffer, otherwise we have to copy the data to a buffer
14313 allocated on the temporary obstack. */
14314 gdb_assert (HOST_CHAR_BIT == 8);
14315 return buf;
14316 }
14317
14318 static char *
14319 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14320 {
14321 /* If the size of a host char is 8 bits, we can return a pointer
14322 to the string, otherwise we have to copy the string to a buffer
14323 allocated on the temporary obstack. */
14324 gdb_assert (HOST_CHAR_BIT == 8);
14325 if (*buf == '\0')
14326 {
14327 *bytes_read_ptr = 1;
14328 return NULL;
14329 }
14330 *bytes_read_ptr = strlen ((char *) buf) + 1;
14331 return (char *) buf;
14332 }
14333
14334 static char *
14335 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14336 {
14337 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14338 if (dwarf2_per_objfile->str.buffer == NULL)
14339 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14340 bfd_get_filename (abfd));
14341 if (str_offset >= dwarf2_per_objfile->str.size)
14342 error (_("DW_FORM_strp pointing outside of "
14343 ".debug_str section [in module %s]"),
14344 bfd_get_filename (abfd));
14345 gdb_assert (HOST_CHAR_BIT == 8);
14346 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14347 return NULL;
14348 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14349 }
14350
14351 /* Read a string at offset STR_OFFSET in the .debug_str section from
14352 the .dwz file DWZ. Throw an error if the offset is too large. If
14353 the string consists of a single NUL byte, return NULL; otherwise
14354 return a pointer to the string. */
14355
14356 static char *
14357 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14358 {
14359 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14360
14361 if (dwz->str.buffer == NULL)
14362 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14363 "section [in module %s]"),
14364 bfd_get_filename (dwz->dwz_bfd));
14365 if (str_offset >= dwz->str.size)
14366 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14367 ".debug_str section [in module %s]"),
14368 bfd_get_filename (dwz->dwz_bfd));
14369 gdb_assert (HOST_CHAR_BIT == 8);
14370 if (dwz->str.buffer[str_offset] == '\0')
14371 return NULL;
14372 return (char *) (dwz->str.buffer + str_offset);
14373 }
14374
14375 static char *
14376 read_indirect_string (bfd *abfd, gdb_byte *buf,
14377 const struct comp_unit_head *cu_header,
14378 unsigned int *bytes_read_ptr)
14379 {
14380 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14381
14382 return read_indirect_string_at_offset (abfd, str_offset);
14383 }
14384
14385 static ULONGEST
14386 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14387 {
14388 ULONGEST result;
14389 unsigned int num_read;
14390 int i, shift;
14391 unsigned char byte;
14392
14393 result = 0;
14394 shift = 0;
14395 num_read = 0;
14396 i = 0;
14397 while (1)
14398 {
14399 byte = bfd_get_8 (abfd, buf);
14400 buf++;
14401 num_read++;
14402 result |= ((ULONGEST) (byte & 127) << shift);
14403 if ((byte & 128) == 0)
14404 {
14405 break;
14406 }
14407 shift += 7;
14408 }
14409 *bytes_read_ptr = num_read;
14410 return result;
14411 }
14412
14413 static LONGEST
14414 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14415 {
14416 LONGEST result;
14417 int i, shift, num_read;
14418 unsigned char byte;
14419
14420 result = 0;
14421 shift = 0;
14422 num_read = 0;
14423 i = 0;
14424 while (1)
14425 {
14426 byte = bfd_get_8 (abfd, buf);
14427 buf++;
14428 num_read++;
14429 result |= ((LONGEST) (byte & 127) << shift);
14430 shift += 7;
14431 if ((byte & 128) == 0)
14432 {
14433 break;
14434 }
14435 }
14436 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14437 result |= -(((LONGEST) 1) << shift);
14438 *bytes_read_ptr = num_read;
14439 return result;
14440 }
14441
14442 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14443 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14444 ADDR_SIZE is the size of addresses from the CU header. */
14445
14446 static CORE_ADDR
14447 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14448 {
14449 struct objfile *objfile = dwarf2_per_objfile->objfile;
14450 bfd *abfd = objfile->obfd;
14451 const gdb_byte *info_ptr;
14452
14453 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14454 if (dwarf2_per_objfile->addr.buffer == NULL)
14455 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14456 objfile->name);
14457 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14458 error (_("DW_FORM_addr_index pointing outside of "
14459 ".debug_addr section [in module %s]"),
14460 objfile->name);
14461 info_ptr = (dwarf2_per_objfile->addr.buffer
14462 + addr_base + addr_index * addr_size);
14463 if (addr_size == 4)
14464 return bfd_get_32 (abfd, info_ptr);
14465 else
14466 return bfd_get_64 (abfd, info_ptr);
14467 }
14468
14469 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14470
14471 static CORE_ADDR
14472 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14473 {
14474 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14475 }
14476
14477 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14478
14479 static CORE_ADDR
14480 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14481 unsigned int *bytes_read)
14482 {
14483 bfd *abfd = cu->objfile->obfd;
14484 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14485
14486 return read_addr_index (cu, addr_index);
14487 }
14488
14489 /* Data structure to pass results from dwarf2_read_addr_index_reader
14490 back to dwarf2_read_addr_index. */
14491
14492 struct dwarf2_read_addr_index_data
14493 {
14494 ULONGEST addr_base;
14495 int addr_size;
14496 };
14497
14498 /* die_reader_func for dwarf2_read_addr_index. */
14499
14500 static void
14501 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14502 gdb_byte *info_ptr,
14503 struct die_info *comp_unit_die,
14504 int has_children,
14505 void *data)
14506 {
14507 struct dwarf2_cu *cu = reader->cu;
14508 struct dwarf2_read_addr_index_data *aidata =
14509 (struct dwarf2_read_addr_index_data *) data;
14510
14511 aidata->addr_base = cu->addr_base;
14512 aidata->addr_size = cu->header.addr_size;
14513 }
14514
14515 /* Given an index in .debug_addr, fetch the value.
14516 NOTE: This can be called during dwarf expression evaluation,
14517 long after the debug information has been read, and thus per_cu->cu
14518 may no longer exist. */
14519
14520 CORE_ADDR
14521 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14522 unsigned int addr_index)
14523 {
14524 struct objfile *objfile = per_cu->objfile;
14525 struct dwarf2_cu *cu = per_cu->cu;
14526 ULONGEST addr_base;
14527 int addr_size;
14528
14529 /* This is intended to be called from outside this file. */
14530 dw2_setup (objfile);
14531
14532 /* We need addr_base and addr_size.
14533 If we don't have PER_CU->cu, we have to get it.
14534 Nasty, but the alternative is storing the needed info in PER_CU,
14535 which at this point doesn't seem justified: it's not clear how frequently
14536 it would get used and it would increase the size of every PER_CU.
14537 Entry points like dwarf2_per_cu_addr_size do a similar thing
14538 so we're not in uncharted territory here.
14539 Alas we need to be a bit more complicated as addr_base is contained
14540 in the DIE.
14541
14542 We don't need to read the entire CU(/TU).
14543 We just need the header and top level die.
14544
14545 IWBN to use the aging mechanism to let us lazily later discard the CU.
14546 For now we skip this optimization. */
14547
14548 if (cu != NULL)
14549 {
14550 addr_base = cu->addr_base;
14551 addr_size = cu->header.addr_size;
14552 }
14553 else
14554 {
14555 struct dwarf2_read_addr_index_data aidata;
14556
14557 /* Note: We can't use init_cutu_and_read_dies_simple here,
14558 we need addr_base. */
14559 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14560 dwarf2_read_addr_index_reader, &aidata);
14561 addr_base = aidata.addr_base;
14562 addr_size = aidata.addr_size;
14563 }
14564
14565 return read_addr_index_1 (addr_index, addr_base, addr_size);
14566 }
14567
14568 /* Given a DW_AT_str_index, fetch the string. */
14569
14570 static char *
14571 read_str_index (const struct die_reader_specs *reader,
14572 struct dwarf2_cu *cu, ULONGEST str_index)
14573 {
14574 struct objfile *objfile = dwarf2_per_objfile->objfile;
14575 const char *dwo_name = objfile->name;
14576 bfd *abfd = objfile->obfd;
14577 struct dwo_sections *sections = &reader->dwo_file->sections;
14578 gdb_byte *info_ptr;
14579 ULONGEST str_offset;
14580
14581 dwarf2_read_section (objfile, &sections->str);
14582 dwarf2_read_section (objfile, &sections->str_offsets);
14583 if (sections->str.buffer == NULL)
14584 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14585 " in CU at offset 0x%lx [in module %s]"),
14586 (long) cu->header.offset.sect_off, dwo_name);
14587 if (sections->str_offsets.buffer == NULL)
14588 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14589 " in CU at offset 0x%lx [in module %s]"),
14590 (long) cu->header.offset.sect_off, dwo_name);
14591 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14592 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14593 " section in CU at offset 0x%lx [in module %s]"),
14594 (long) cu->header.offset.sect_off, dwo_name);
14595 info_ptr = (sections->str_offsets.buffer
14596 + str_index * cu->header.offset_size);
14597 if (cu->header.offset_size == 4)
14598 str_offset = bfd_get_32 (abfd, info_ptr);
14599 else
14600 str_offset = bfd_get_64 (abfd, info_ptr);
14601 if (str_offset >= sections->str.size)
14602 error (_("Offset from DW_FORM_str_index pointing outside of"
14603 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14604 (long) cu->header.offset.sect_off, dwo_name);
14605 return (char *) (sections->str.buffer + str_offset);
14606 }
14607
14608 /* Return the length of an LEB128 number in BUF. */
14609
14610 static int
14611 leb128_size (const gdb_byte *buf)
14612 {
14613 const gdb_byte *begin = buf;
14614 gdb_byte byte;
14615
14616 while (1)
14617 {
14618 byte = *buf++;
14619 if ((byte & 128) == 0)
14620 return buf - begin;
14621 }
14622 }
14623
14624 static void
14625 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14626 {
14627 switch (lang)
14628 {
14629 case DW_LANG_C89:
14630 case DW_LANG_C99:
14631 case DW_LANG_C:
14632 cu->language = language_c;
14633 break;
14634 case DW_LANG_C_plus_plus:
14635 cu->language = language_cplus;
14636 break;
14637 case DW_LANG_D:
14638 cu->language = language_d;
14639 break;
14640 case DW_LANG_Fortran77:
14641 case DW_LANG_Fortran90:
14642 case DW_LANG_Fortran95:
14643 cu->language = language_fortran;
14644 break;
14645 case DW_LANG_Go:
14646 cu->language = language_go;
14647 break;
14648 case DW_LANG_Mips_Assembler:
14649 cu->language = language_asm;
14650 break;
14651 case DW_LANG_Java:
14652 cu->language = language_java;
14653 break;
14654 case DW_LANG_Ada83:
14655 case DW_LANG_Ada95:
14656 cu->language = language_ada;
14657 break;
14658 case DW_LANG_Modula2:
14659 cu->language = language_m2;
14660 break;
14661 case DW_LANG_Pascal83:
14662 cu->language = language_pascal;
14663 break;
14664 case DW_LANG_ObjC:
14665 cu->language = language_objc;
14666 break;
14667 case DW_LANG_Cobol74:
14668 case DW_LANG_Cobol85:
14669 default:
14670 cu->language = language_minimal;
14671 break;
14672 }
14673 cu->language_defn = language_def (cu->language);
14674 }
14675
14676 /* Return the named attribute or NULL if not there. */
14677
14678 static struct attribute *
14679 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14680 {
14681 for (;;)
14682 {
14683 unsigned int i;
14684 struct attribute *spec = NULL;
14685
14686 for (i = 0; i < die->num_attrs; ++i)
14687 {
14688 if (die->attrs[i].name == name)
14689 return &die->attrs[i];
14690 if (die->attrs[i].name == DW_AT_specification
14691 || die->attrs[i].name == DW_AT_abstract_origin)
14692 spec = &die->attrs[i];
14693 }
14694
14695 if (!spec)
14696 break;
14697
14698 die = follow_die_ref (die, spec, &cu);
14699 }
14700
14701 return NULL;
14702 }
14703
14704 /* Return the named attribute or NULL if not there,
14705 but do not follow DW_AT_specification, etc.
14706 This is for use in contexts where we're reading .debug_types dies.
14707 Following DW_AT_specification, DW_AT_abstract_origin will take us
14708 back up the chain, and we want to go down. */
14709
14710 static struct attribute *
14711 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14712 {
14713 unsigned int i;
14714
14715 for (i = 0; i < die->num_attrs; ++i)
14716 if (die->attrs[i].name == name)
14717 return &die->attrs[i];
14718
14719 return NULL;
14720 }
14721
14722 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14723 and holds a non-zero value. This function should only be used for
14724 DW_FORM_flag or DW_FORM_flag_present attributes. */
14725
14726 static int
14727 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14728 {
14729 struct attribute *attr = dwarf2_attr (die, name, cu);
14730
14731 return (attr && DW_UNSND (attr));
14732 }
14733
14734 static int
14735 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14736 {
14737 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14738 which value is non-zero. However, we have to be careful with
14739 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14740 (via dwarf2_flag_true_p) follows this attribute. So we may
14741 end up accidently finding a declaration attribute that belongs
14742 to a different DIE referenced by the specification attribute,
14743 even though the given DIE does not have a declaration attribute. */
14744 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14745 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14746 }
14747
14748 /* Return the die giving the specification for DIE, if there is
14749 one. *SPEC_CU is the CU containing DIE on input, and the CU
14750 containing the return value on output. If there is no
14751 specification, but there is an abstract origin, that is
14752 returned. */
14753
14754 static struct die_info *
14755 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14756 {
14757 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14758 *spec_cu);
14759
14760 if (spec_attr == NULL)
14761 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14762
14763 if (spec_attr == NULL)
14764 return NULL;
14765 else
14766 return follow_die_ref (die, spec_attr, spec_cu);
14767 }
14768
14769 /* Free the line_header structure *LH, and any arrays and strings it
14770 refers to.
14771 NOTE: This is also used as a "cleanup" function. */
14772
14773 static void
14774 free_line_header (struct line_header *lh)
14775 {
14776 if (lh->standard_opcode_lengths)
14777 xfree (lh->standard_opcode_lengths);
14778
14779 /* Remember that all the lh->file_names[i].name pointers are
14780 pointers into debug_line_buffer, and don't need to be freed. */
14781 if (lh->file_names)
14782 xfree (lh->file_names);
14783
14784 /* Similarly for the include directory names. */
14785 if (lh->include_dirs)
14786 xfree (lh->include_dirs);
14787
14788 xfree (lh);
14789 }
14790
14791 /* Add an entry to LH's include directory table. */
14792
14793 static void
14794 add_include_dir (struct line_header *lh, char *include_dir)
14795 {
14796 /* Grow the array if necessary. */
14797 if (lh->include_dirs_size == 0)
14798 {
14799 lh->include_dirs_size = 1; /* for testing */
14800 lh->include_dirs = xmalloc (lh->include_dirs_size
14801 * sizeof (*lh->include_dirs));
14802 }
14803 else if (lh->num_include_dirs >= lh->include_dirs_size)
14804 {
14805 lh->include_dirs_size *= 2;
14806 lh->include_dirs = xrealloc (lh->include_dirs,
14807 (lh->include_dirs_size
14808 * sizeof (*lh->include_dirs)));
14809 }
14810
14811 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14812 }
14813
14814 /* Add an entry to LH's file name table. */
14815
14816 static void
14817 add_file_name (struct line_header *lh,
14818 char *name,
14819 unsigned int dir_index,
14820 unsigned int mod_time,
14821 unsigned int length)
14822 {
14823 struct file_entry *fe;
14824
14825 /* Grow the array if necessary. */
14826 if (lh->file_names_size == 0)
14827 {
14828 lh->file_names_size = 1; /* for testing */
14829 lh->file_names = xmalloc (lh->file_names_size
14830 * sizeof (*lh->file_names));
14831 }
14832 else if (lh->num_file_names >= lh->file_names_size)
14833 {
14834 lh->file_names_size *= 2;
14835 lh->file_names = xrealloc (lh->file_names,
14836 (lh->file_names_size
14837 * sizeof (*lh->file_names)));
14838 }
14839
14840 fe = &lh->file_names[lh->num_file_names++];
14841 fe->name = name;
14842 fe->dir_index = dir_index;
14843 fe->mod_time = mod_time;
14844 fe->length = length;
14845 fe->included_p = 0;
14846 fe->symtab = NULL;
14847 }
14848
14849 /* A convenience function to find the proper .debug_line section for a
14850 CU. */
14851
14852 static struct dwarf2_section_info *
14853 get_debug_line_section (struct dwarf2_cu *cu)
14854 {
14855 struct dwarf2_section_info *section;
14856
14857 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14858 DWO file. */
14859 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14860 section = &cu->dwo_unit->dwo_file->sections.line;
14861 else if (cu->per_cu->is_dwz)
14862 {
14863 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14864
14865 section = &dwz->line;
14866 }
14867 else
14868 section = &dwarf2_per_objfile->line;
14869
14870 return section;
14871 }
14872
14873 /* Read the statement program header starting at OFFSET in
14874 .debug_line, or .debug_line.dwo. Return a pointer
14875 to a struct line_header, allocated using xmalloc.
14876
14877 NOTE: the strings in the include directory and file name tables of
14878 the returned object point into the dwarf line section buffer,
14879 and must not be freed. */
14880
14881 static struct line_header *
14882 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14883 {
14884 struct cleanup *back_to;
14885 struct line_header *lh;
14886 gdb_byte *line_ptr;
14887 unsigned int bytes_read, offset_size;
14888 int i;
14889 char *cur_dir, *cur_file;
14890 struct dwarf2_section_info *section;
14891 bfd *abfd;
14892
14893 section = get_debug_line_section (cu);
14894 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14895 if (section->buffer == NULL)
14896 {
14897 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14898 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14899 else
14900 complaint (&symfile_complaints, _("missing .debug_line section"));
14901 return 0;
14902 }
14903
14904 /* We can't do this until we know the section is non-empty.
14905 Only then do we know we have such a section. */
14906 abfd = section->asection->owner;
14907
14908 /* Make sure that at least there's room for the total_length field.
14909 That could be 12 bytes long, but we're just going to fudge that. */
14910 if (offset + 4 >= section->size)
14911 {
14912 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14913 return 0;
14914 }
14915
14916 lh = xmalloc (sizeof (*lh));
14917 memset (lh, 0, sizeof (*lh));
14918 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14919 (void *) lh);
14920
14921 line_ptr = section->buffer + offset;
14922
14923 /* Read in the header. */
14924 lh->total_length =
14925 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14926 &bytes_read, &offset_size);
14927 line_ptr += bytes_read;
14928 if (line_ptr + lh->total_length > (section->buffer + section->size))
14929 {
14930 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14931 return 0;
14932 }
14933 lh->statement_program_end = line_ptr + lh->total_length;
14934 lh->version = read_2_bytes (abfd, line_ptr);
14935 line_ptr += 2;
14936 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14937 line_ptr += offset_size;
14938 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14939 line_ptr += 1;
14940 if (lh->version >= 4)
14941 {
14942 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14943 line_ptr += 1;
14944 }
14945 else
14946 lh->maximum_ops_per_instruction = 1;
14947
14948 if (lh->maximum_ops_per_instruction == 0)
14949 {
14950 lh->maximum_ops_per_instruction = 1;
14951 complaint (&symfile_complaints,
14952 _("invalid maximum_ops_per_instruction "
14953 "in `.debug_line' section"));
14954 }
14955
14956 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14957 line_ptr += 1;
14958 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14959 line_ptr += 1;
14960 lh->line_range = read_1_byte (abfd, line_ptr);
14961 line_ptr += 1;
14962 lh->opcode_base = read_1_byte (abfd, line_ptr);
14963 line_ptr += 1;
14964 lh->standard_opcode_lengths
14965 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14966
14967 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14968 for (i = 1; i < lh->opcode_base; ++i)
14969 {
14970 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14971 line_ptr += 1;
14972 }
14973
14974 /* Read directory table. */
14975 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14976 {
14977 line_ptr += bytes_read;
14978 add_include_dir (lh, cur_dir);
14979 }
14980 line_ptr += bytes_read;
14981
14982 /* Read file name table. */
14983 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14984 {
14985 unsigned int dir_index, mod_time, length;
14986
14987 line_ptr += bytes_read;
14988 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14989 line_ptr += bytes_read;
14990 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14991 line_ptr += bytes_read;
14992 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14993 line_ptr += bytes_read;
14994
14995 add_file_name (lh, cur_file, dir_index, mod_time, length);
14996 }
14997 line_ptr += bytes_read;
14998 lh->statement_program_start = line_ptr;
14999
15000 if (line_ptr > (section->buffer + section->size))
15001 complaint (&symfile_complaints,
15002 _("line number info header doesn't "
15003 "fit in `.debug_line' section"));
15004
15005 discard_cleanups (back_to);
15006 return lh;
15007 }
15008
15009 /* Subroutine of dwarf_decode_lines to simplify it.
15010 Return the file name of the psymtab for included file FILE_INDEX
15011 in line header LH of PST.
15012 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15013 If space for the result is malloc'd, it will be freed by a cleanup.
15014 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15015
15016 static char *
15017 psymtab_include_file_name (const struct line_header *lh, int file_index,
15018 const struct partial_symtab *pst,
15019 const char *comp_dir)
15020 {
15021 const struct file_entry fe = lh->file_names [file_index];
15022 char *include_name = fe.name;
15023 char *include_name_to_compare = include_name;
15024 char *dir_name = NULL;
15025 const char *pst_filename;
15026 char *copied_name = NULL;
15027 int file_is_pst;
15028
15029 if (fe.dir_index)
15030 dir_name = lh->include_dirs[fe.dir_index - 1];
15031
15032 if (!IS_ABSOLUTE_PATH (include_name)
15033 && (dir_name != NULL || comp_dir != NULL))
15034 {
15035 /* Avoid creating a duplicate psymtab for PST.
15036 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15037 Before we do the comparison, however, we need to account
15038 for DIR_NAME and COMP_DIR.
15039 First prepend dir_name (if non-NULL). If we still don't
15040 have an absolute path prepend comp_dir (if non-NULL).
15041 However, the directory we record in the include-file's
15042 psymtab does not contain COMP_DIR (to match the
15043 corresponding symtab(s)).
15044
15045 Example:
15046
15047 bash$ cd /tmp
15048 bash$ gcc -g ./hello.c
15049 include_name = "hello.c"
15050 dir_name = "."
15051 DW_AT_comp_dir = comp_dir = "/tmp"
15052 DW_AT_name = "./hello.c" */
15053
15054 if (dir_name != NULL)
15055 {
15056 include_name = concat (dir_name, SLASH_STRING,
15057 include_name, (char *)NULL);
15058 include_name_to_compare = include_name;
15059 make_cleanup (xfree, include_name);
15060 }
15061 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15062 {
15063 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15064 include_name, (char *)NULL);
15065 }
15066 }
15067
15068 pst_filename = pst->filename;
15069 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15070 {
15071 copied_name = concat (pst->dirname, SLASH_STRING,
15072 pst_filename, (char *)NULL);
15073 pst_filename = copied_name;
15074 }
15075
15076 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15077
15078 if (include_name_to_compare != include_name)
15079 xfree (include_name_to_compare);
15080 if (copied_name != NULL)
15081 xfree (copied_name);
15082
15083 if (file_is_pst)
15084 return NULL;
15085 return include_name;
15086 }
15087
15088 /* Ignore this record_line request. */
15089
15090 static void
15091 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15092 {
15093 return;
15094 }
15095
15096 /* Subroutine of dwarf_decode_lines to simplify it.
15097 Process the line number information in LH. */
15098
15099 static void
15100 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15101 struct dwarf2_cu *cu, struct partial_symtab *pst)
15102 {
15103 gdb_byte *line_ptr, *extended_end;
15104 gdb_byte *line_end;
15105 unsigned int bytes_read, extended_len;
15106 unsigned char op_code, extended_op, adj_opcode;
15107 CORE_ADDR baseaddr;
15108 struct objfile *objfile = cu->objfile;
15109 bfd *abfd = objfile->obfd;
15110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15111 const int decode_for_pst_p = (pst != NULL);
15112 struct subfile *last_subfile = NULL;
15113 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15114 = record_line;
15115
15116 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15117
15118 line_ptr = lh->statement_program_start;
15119 line_end = lh->statement_program_end;
15120
15121 /* Read the statement sequences until there's nothing left. */
15122 while (line_ptr < line_end)
15123 {
15124 /* state machine registers */
15125 CORE_ADDR address = 0;
15126 unsigned int file = 1;
15127 unsigned int line = 1;
15128 unsigned int column = 0;
15129 int is_stmt = lh->default_is_stmt;
15130 int basic_block = 0;
15131 int end_sequence = 0;
15132 CORE_ADDR addr;
15133 unsigned char op_index = 0;
15134
15135 if (!decode_for_pst_p && lh->num_file_names >= file)
15136 {
15137 /* Start a subfile for the current file of the state machine. */
15138 /* lh->include_dirs and lh->file_names are 0-based, but the
15139 directory and file name numbers in the statement program
15140 are 1-based. */
15141 struct file_entry *fe = &lh->file_names[file - 1];
15142 char *dir = NULL;
15143
15144 if (fe->dir_index)
15145 dir = lh->include_dirs[fe->dir_index - 1];
15146
15147 dwarf2_start_subfile (fe->name, dir, comp_dir);
15148 }
15149
15150 /* Decode the table. */
15151 while (!end_sequence)
15152 {
15153 op_code = read_1_byte (abfd, line_ptr);
15154 line_ptr += 1;
15155 if (line_ptr > line_end)
15156 {
15157 dwarf2_debug_line_missing_end_sequence_complaint ();
15158 break;
15159 }
15160
15161 if (op_code >= lh->opcode_base)
15162 {
15163 /* Special operand. */
15164 adj_opcode = op_code - lh->opcode_base;
15165 address += (((op_index + (adj_opcode / lh->line_range))
15166 / lh->maximum_ops_per_instruction)
15167 * lh->minimum_instruction_length);
15168 op_index = ((op_index + (adj_opcode / lh->line_range))
15169 % lh->maximum_ops_per_instruction);
15170 line += lh->line_base + (adj_opcode % lh->line_range);
15171 if (lh->num_file_names < file || file == 0)
15172 dwarf2_debug_line_missing_file_complaint ();
15173 /* For now we ignore lines not starting on an
15174 instruction boundary. */
15175 else if (op_index == 0)
15176 {
15177 lh->file_names[file - 1].included_p = 1;
15178 if (!decode_for_pst_p && is_stmt)
15179 {
15180 if (last_subfile != current_subfile)
15181 {
15182 addr = gdbarch_addr_bits_remove (gdbarch, address);
15183 if (last_subfile)
15184 (*p_record_line) (last_subfile, 0, addr);
15185 last_subfile = current_subfile;
15186 }
15187 /* Append row to matrix using current values. */
15188 addr = gdbarch_addr_bits_remove (gdbarch, address);
15189 (*p_record_line) (current_subfile, line, addr);
15190 }
15191 }
15192 basic_block = 0;
15193 }
15194 else switch (op_code)
15195 {
15196 case DW_LNS_extended_op:
15197 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15198 &bytes_read);
15199 line_ptr += bytes_read;
15200 extended_end = line_ptr + extended_len;
15201 extended_op = read_1_byte (abfd, line_ptr);
15202 line_ptr += 1;
15203 switch (extended_op)
15204 {
15205 case DW_LNE_end_sequence:
15206 p_record_line = record_line;
15207 end_sequence = 1;
15208 break;
15209 case DW_LNE_set_address:
15210 address = read_address (abfd, line_ptr, cu, &bytes_read);
15211
15212 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15213 {
15214 /* This line table is for a function which has been
15215 GCd by the linker. Ignore it. PR gdb/12528 */
15216
15217 long line_offset
15218 = line_ptr - get_debug_line_section (cu)->buffer;
15219
15220 complaint (&symfile_complaints,
15221 _(".debug_line address at offset 0x%lx is 0 "
15222 "[in module %s]"),
15223 line_offset, objfile->name);
15224 p_record_line = noop_record_line;
15225 }
15226
15227 op_index = 0;
15228 line_ptr += bytes_read;
15229 address += baseaddr;
15230 break;
15231 case DW_LNE_define_file:
15232 {
15233 char *cur_file;
15234 unsigned int dir_index, mod_time, length;
15235
15236 cur_file = read_direct_string (abfd, line_ptr,
15237 &bytes_read);
15238 line_ptr += bytes_read;
15239 dir_index =
15240 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15241 line_ptr += bytes_read;
15242 mod_time =
15243 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15244 line_ptr += bytes_read;
15245 length =
15246 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15247 line_ptr += bytes_read;
15248 add_file_name (lh, cur_file, dir_index, mod_time, length);
15249 }
15250 break;
15251 case DW_LNE_set_discriminator:
15252 /* The discriminator is not interesting to the debugger;
15253 just ignore it. */
15254 line_ptr = extended_end;
15255 break;
15256 default:
15257 complaint (&symfile_complaints,
15258 _("mangled .debug_line section"));
15259 return;
15260 }
15261 /* Make sure that we parsed the extended op correctly. If e.g.
15262 we expected a different address size than the producer used,
15263 we may have read the wrong number of bytes. */
15264 if (line_ptr != extended_end)
15265 {
15266 complaint (&symfile_complaints,
15267 _("mangled .debug_line section"));
15268 return;
15269 }
15270 break;
15271 case DW_LNS_copy:
15272 if (lh->num_file_names < file || file == 0)
15273 dwarf2_debug_line_missing_file_complaint ();
15274 else
15275 {
15276 lh->file_names[file - 1].included_p = 1;
15277 if (!decode_for_pst_p && is_stmt)
15278 {
15279 if (last_subfile != current_subfile)
15280 {
15281 addr = gdbarch_addr_bits_remove (gdbarch, address);
15282 if (last_subfile)
15283 (*p_record_line) (last_subfile, 0, addr);
15284 last_subfile = current_subfile;
15285 }
15286 addr = gdbarch_addr_bits_remove (gdbarch, address);
15287 (*p_record_line) (current_subfile, line, addr);
15288 }
15289 }
15290 basic_block = 0;
15291 break;
15292 case DW_LNS_advance_pc:
15293 {
15294 CORE_ADDR adjust
15295 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15296
15297 address += (((op_index + adjust)
15298 / lh->maximum_ops_per_instruction)
15299 * lh->minimum_instruction_length);
15300 op_index = ((op_index + adjust)
15301 % lh->maximum_ops_per_instruction);
15302 line_ptr += bytes_read;
15303 }
15304 break;
15305 case DW_LNS_advance_line:
15306 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15307 line_ptr += bytes_read;
15308 break;
15309 case DW_LNS_set_file:
15310 {
15311 /* The arrays lh->include_dirs and lh->file_names are
15312 0-based, but the directory and file name numbers in
15313 the statement program are 1-based. */
15314 struct file_entry *fe;
15315 char *dir = NULL;
15316
15317 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15318 line_ptr += bytes_read;
15319 if (lh->num_file_names < file || file == 0)
15320 dwarf2_debug_line_missing_file_complaint ();
15321 else
15322 {
15323 fe = &lh->file_names[file - 1];
15324 if (fe->dir_index)
15325 dir = lh->include_dirs[fe->dir_index - 1];
15326 if (!decode_for_pst_p)
15327 {
15328 last_subfile = current_subfile;
15329 dwarf2_start_subfile (fe->name, dir, comp_dir);
15330 }
15331 }
15332 }
15333 break;
15334 case DW_LNS_set_column:
15335 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15336 line_ptr += bytes_read;
15337 break;
15338 case DW_LNS_negate_stmt:
15339 is_stmt = (!is_stmt);
15340 break;
15341 case DW_LNS_set_basic_block:
15342 basic_block = 1;
15343 break;
15344 /* Add to the address register of the state machine the
15345 address increment value corresponding to special opcode
15346 255. I.e., this value is scaled by the minimum
15347 instruction length since special opcode 255 would have
15348 scaled the increment. */
15349 case DW_LNS_const_add_pc:
15350 {
15351 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15352
15353 address += (((op_index + adjust)
15354 / lh->maximum_ops_per_instruction)
15355 * lh->minimum_instruction_length);
15356 op_index = ((op_index + adjust)
15357 % lh->maximum_ops_per_instruction);
15358 }
15359 break;
15360 case DW_LNS_fixed_advance_pc:
15361 address += read_2_bytes (abfd, line_ptr);
15362 op_index = 0;
15363 line_ptr += 2;
15364 break;
15365 default:
15366 {
15367 /* Unknown standard opcode, ignore it. */
15368 int i;
15369
15370 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15371 {
15372 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15373 line_ptr += bytes_read;
15374 }
15375 }
15376 }
15377 }
15378 if (lh->num_file_names < file || file == 0)
15379 dwarf2_debug_line_missing_file_complaint ();
15380 else
15381 {
15382 lh->file_names[file - 1].included_p = 1;
15383 if (!decode_for_pst_p)
15384 {
15385 addr = gdbarch_addr_bits_remove (gdbarch, address);
15386 (*p_record_line) (current_subfile, 0, addr);
15387 }
15388 }
15389 }
15390 }
15391
15392 /* Decode the Line Number Program (LNP) for the given line_header
15393 structure and CU. The actual information extracted and the type
15394 of structures created from the LNP depends on the value of PST.
15395
15396 1. If PST is NULL, then this procedure uses the data from the program
15397 to create all necessary symbol tables, and their linetables.
15398
15399 2. If PST is not NULL, this procedure reads the program to determine
15400 the list of files included by the unit represented by PST, and
15401 builds all the associated partial symbol tables.
15402
15403 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15404 It is used for relative paths in the line table.
15405 NOTE: When processing partial symtabs (pst != NULL),
15406 comp_dir == pst->dirname.
15407
15408 NOTE: It is important that psymtabs have the same file name (via strcmp)
15409 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15410 symtab we don't use it in the name of the psymtabs we create.
15411 E.g. expand_line_sal requires this when finding psymtabs to expand.
15412 A good testcase for this is mb-inline.exp. */
15413
15414 static void
15415 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15416 struct dwarf2_cu *cu, struct partial_symtab *pst,
15417 int want_line_info)
15418 {
15419 struct objfile *objfile = cu->objfile;
15420 const int decode_for_pst_p = (pst != NULL);
15421 struct subfile *first_subfile = current_subfile;
15422
15423 if (want_line_info)
15424 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15425
15426 if (decode_for_pst_p)
15427 {
15428 int file_index;
15429
15430 /* Now that we're done scanning the Line Header Program, we can
15431 create the psymtab of each included file. */
15432 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15433 if (lh->file_names[file_index].included_p == 1)
15434 {
15435 char *include_name =
15436 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15437 if (include_name != NULL)
15438 dwarf2_create_include_psymtab (include_name, pst, objfile);
15439 }
15440 }
15441 else
15442 {
15443 /* Make sure a symtab is created for every file, even files
15444 which contain only variables (i.e. no code with associated
15445 line numbers). */
15446 int i;
15447
15448 for (i = 0; i < lh->num_file_names; i++)
15449 {
15450 char *dir = NULL;
15451 struct file_entry *fe;
15452
15453 fe = &lh->file_names[i];
15454 if (fe->dir_index)
15455 dir = lh->include_dirs[fe->dir_index - 1];
15456 dwarf2_start_subfile (fe->name, dir, comp_dir);
15457
15458 /* Skip the main file; we don't need it, and it must be
15459 allocated last, so that it will show up before the
15460 non-primary symtabs in the objfile's symtab list. */
15461 if (current_subfile == first_subfile)
15462 continue;
15463
15464 if (current_subfile->symtab == NULL)
15465 current_subfile->symtab = allocate_symtab (current_subfile->name,
15466 objfile);
15467 fe->symtab = current_subfile->symtab;
15468 }
15469 }
15470 }
15471
15472 /* Start a subfile for DWARF. FILENAME is the name of the file and
15473 DIRNAME the name of the source directory which contains FILENAME
15474 or NULL if not known. COMP_DIR is the compilation directory for the
15475 linetable's compilation unit or NULL if not known.
15476 This routine tries to keep line numbers from identical absolute and
15477 relative file names in a common subfile.
15478
15479 Using the `list' example from the GDB testsuite, which resides in
15480 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15481 of /srcdir/list0.c yields the following debugging information for list0.c:
15482
15483 DW_AT_name: /srcdir/list0.c
15484 DW_AT_comp_dir: /compdir
15485 files.files[0].name: list0.h
15486 files.files[0].dir: /srcdir
15487 files.files[1].name: list0.c
15488 files.files[1].dir: /srcdir
15489
15490 The line number information for list0.c has to end up in a single
15491 subfile, so that `break /srcdir/list0.c:1' works as expected.
15492 start_subfile will ensure that this happens provided that we pass the
15493 concatenation of files.files[1].dir and files.files[1].name as the
15494 subfile's name. */
15495
15496 static void
15497 dwarf2_start_subfile (char *filename, const char *dirname,
15498 const char *comp_dir)
15499 {
15500 char *fullname;
15501
15502 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15503 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15504 second argument to start_subfile. To be consistent, we do the
15505 same here. In order not to lose the line information directory,
15506 we concatenate it to the filename when it makes sense.
15507 Note that the Dwarf3 standard says (speaking of filenames in line
15508 information): ``The directory index is ignored for file names
15509 that represent full path names''. Thus ignoring dirname in the
15510 `else' branch below isn't an issue. */
15511
15512 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15513 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15514 else
15515 fullname = filename;
15516
15517 start_subfile (fullname, comp_dir);
15518
15519 if (fullname != filename)
15520 xfree (fullname);
15521 }
15522
15523 /* Start a symtab for DWARF.
15524 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15525
15526 static void
15527 dwarf2_start_symtab (struct dwarf2_cu *cu,
15528 char *name, char *comp_dir, CORE_ADDR low_pc)
15529 {
15530 start_symtab (name, comp_dir, low_pc);
15531 record_debugformat ("DWARF 2");
15532 record_producer (cu->producer);
15533
15534 /* We assume that we're processing GCC output. */
15535 processing_gcc_compilation = 2;
15536
15537 processing_has_namespace_info = 0;
15538 }
15539
15540 static void
15541 var_decode_location (struct attribute *attr, struct symbol *sym,
15542 struct dwarf2_cu *cu)
15543 {
15544 struct objfile *objfile = cu->objfile;
15545 struct comp_unit_head *cu_header = &cu->header;
15546
15547 /* NOTE drow/2003-01-30: There used to be a comment and some special
15548 code here to turn a symbol with DW_AT_external and a
15549 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15550 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15551 with some versions of binutils) where shared libraries could have
15552 relocations against symbols in their debug information - the
15553 minimal symbol would have the right address, but the debug info
15554 would not. It's no longer necessary, because we will explicitly
15555 apply relocations when we read in the debug information now. */
15556
15557 /* A DW_AT_location attribute with no contents indicates that a
15558 variable has been optimized away. */
15559 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15560 {
15561 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15562 return;
15563 }
15564
15565 /* Handle one degenerate form of location expression specially, to
15566 preserve GDB's previous behavior when section offsets are
15567 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15568 then mark this symbol as LOC_STATIC. */
15569
15570 if (attr_form_is_block (attr)
15571 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15572 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15573 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15574 && (DW_BLOCK (attr)->size
15575 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15576 {
15577 unsigned int dummy;
15578
15579 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15580 SYMBOL_VALUE_ADDRESS (sym) =
15581 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15582 else
15583 SYMBOL_VALUE_ADDRESS (sym) =
15584 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15585 SYMBOL_CLASS (sym) = LOC_STATIC;
15586 fixup_symbol_section (sym, objfile);
15587 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15588 SYMBOL_SECTION (sym));
15589 return;
15590 }
15591
15592 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15593 expression evaluator, and use LOC_COMPUTED only when necessary
15594 (i.e. when the value of a register or memory location is
15595 referenced, or a thread-local block, etc.). Then again, it might
15596 not be worthwhile. I'm assuming that it isn't unless performance
15597 or memory numbers show me otherwise. */
15598
15599 dwarf2_symbol_mark_computed (attr, sym, cu);
15600 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15601
15602 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15603 cu->has_loclist = 1;
15604 }
15605
15606 /* Given a pointer to a DWARF information entry, figure out if we need
15607 to make a symbol table entry for it, and if so, create a new entry
15608 and return a pointer to it.
15609 If TYPE is NULL, determine symbol type from the die, otherwise
15610 used the passed type.
15611 If SPACE is not NULL, use it to hold the new symbol. If it is
15612 NULL, allocate a new symbol on the objfile's obstack. */
15613
15614 static struct symbol *
15615 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15616 struct symbol *space)
15617 {
15618 struct objfile *objfile = cu->objfile;
15619 struct symbol *sym = NULL;
15620 char *name;
15621 struct attribute *attr = NULL;
15622 struct attribute *attr2 = NULL;
15623 CORE_ADDR baseaddr;
15624 struct pending **list_to_add = NULL;
15625
15626 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15627
15628 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15629
15630 name = dwarf2_name (die, cu);
15631 if (name)
15632 {
15633 const char *linkagename;
15634 int suppress_add = 0;
15635
15636 if (space)
15637 sym = space;
15638 else
15639 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15640 OBJSTAT (objfile, n_syms++);
15641
15642 /* Cache this symbol's name and the name's demangled form (if any). */
15643 SYMBOL_SET_LANGUAGE (sym, cu->language);
15644 linkagename = dwarf2_physname (name, die, cu);
15645 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15646
15647 /* Fortran does not have mangling standard and the mangling does differ
15648 between gfortran, iFort etc. */
15649 if (cu->language == language_fortran
15650 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15651 symbol_set_demangled_name (&(sym->ginfo),
15652 (char *) dwarf2_full_name (name, die, cu),
15653 NULL);
15654
15655 /* Default assumptions.
15656 Use the passed type or decode it from the die. */
15657 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15658 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15659 if (type != NULL)
15660 SYMBOL_TYPE (sym) = type;
15661 else
15662 SYMBOL_TYPE (sym) = die_type (die, cu);
15663 attr = dwarf2_attr (die,
15664 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15665 cu);
15666 if (attr)
15667 {
15668 SYMBOL_LINE (sym) = DW_UNSND (attr);
15669 }
15670
15671 attr = dwarf2_attr (die,
15672 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15673 cu);
15674 if (attr)
15675 {
15676 int file_index = DW_UNSND (attr);
15677
15678 if (cu->line_header == NULL
15679 || file_index > cu->line_header->num_file_names)
15680 complaint (&symfile_complaints,
15681 _("file index out of range"));
15682 else if (file_index > 0)
15683 {
15684 struct file_entry *fe;
15685
15686 fe = &cu->line_header->file_names[file_index - 1];
15687 SYMBOL_SYMTAB (sym) = fe->symtab;
15688 }
15689 }
15690
15691 switch (die->tag)
15692 {
15693 case DW_TAG_label:
15694 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15695 if (attr)
15696 {
15697 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15698 }
15699 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15700 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15701 SYMBOL_CLASS (sym) = LOC_LABEL;
15702 add_symbol_to_list (sym, cu->list_in_scope);
15703 break;
15704 case DW_TAG_subprogram:
15705 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15706 finish_block. */
15707 SYMBOL_CLASS (sym) = LOC_BLOCK;
15708 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15709 if ((attr2 && (DW_UNSND (attr2) != 0))
15710 || cu->language == language_ada)
15711 {
15712 /* Subprograms marked external are stored as a global symbol.
15713 Ada subprograms, whether marked external or not, are always
15714 stored as a global symbol, because we want to be able to
15715 access them globally. For instance, we want to be able
15716 to break on a nested subprogram without having to
15717 specify the context. */
15718 list_to_add = &global_symbols;
15719 }
15720 else
15721 {
15722 list_to_add = cu->list_in_scope;
15723 }
15724 break;
15725 case DW_TAG_inlined_subroutine:
15726 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15727 finish_block. */
15728 SYMBOL_CLASS (sym) = LOC_BLOCK;
15729 SYMBOL_INLINED (sym) = 1;
15730 list_to_add = cu->list_in_scope;
15731 break;
15732 case DW_TAG_template_value_param:
15733 suppress_add = 1;
15734 /* Fall through. */
15735 case DW_TAG_constant:
15736 case DW_TAG_variable:
15737 case DW_TAG_member:
15738 /* Compilation with minimal debug info may result in
15739 variables with missing type entries. Change the
15740 misleading `void' type to something sensible. */
15741 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15742 SYMBOL_TYPE (sym)
15743 = objfile_type (objfile)->nodebug_data_symbol;
15744
15745 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15746 /* In the case of DW_TAG_member, we should only be called for
15747 static const members. */
15748 if (die->tag == DW_TAG_member)
15749 {
15750 /* dwarf2_add_field uses die_is_declaration,
15751 so we do the same. */
15752 gdb_assert (die_is_declaration (die, cu));
15753 gdb_assert (attr);
15754 }
15755 if (attr)
15756 {
15757 dwarf2_const_value (attr, sym, cu);
15758 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15759 if (!suppress_add)
15760 {
15761 if (attr2 && (DW_UNSND (attr2) != 0))
15762 list_to_add = &global_symbols;
15763 else
15764 list_to_add = cu->list_in_scope;
15765 }
15766 break;
15767 }
15768 attr = dwarf2_attr (die, DW_AT_location, cu);
15769 if (attr)
15770 {
15771 var_decode_location (attr, sym, cu);
15772 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15773
15774 /* Fortran explicitly imports any global symbols to the local
15775 scope by DW_TAG_common_block. */
15776 if (cu->language == language_fortran && die->parent
15777 && die->parent->tag == DW_TAG_common_block)
15778 attr2 = NULL;
15779
15780 if (SYMBOL_CLASS (sym) == LOC_STATIC
15781 && SYMBOL_VALUE_ADDRESS (sym) == 0
15782 && !dwarf2_per_objfile->has_section_at_zero)
15783 {
15784 /* When a static variable is eliminated by the linker,
15785 the corresponding debug information is not stripped
15786 out, but the variable address is set to null;
15787 do not add such variables into symbol table. */
15788 }
15789 else if (attr2 && (DW_UNSND (attr2) != 0))
15790 {
15791 /* Workaround gfortran PR debug/40040 - it uses
15792 DW_AT_location for variables in -fPIC libraries which may
15793 get overriden by other libraries/executable and get
15794 a different address. Resolve it by the minimal symbol
15795 which may come from inferior's executable using copy
15796 relocation. Make this workaround only for gfortran as for
15797 other compilers GDB cannot guess the minimal symbol
15798 Fortran mangling kind. */
15799 if (cu->language == language_fortran && die->parent
15800 && die->parent->tag == DW_TAG_module
15801 && cu->producer
15802 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15803 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15804
15805 /* A variable with DW_AT_external is never static,
15806 but it may be block-scoped. */
15807 list_to_add = (cu->list_in_scope == &file_symbols
15808 ? &global_symbols : cu->list_in_scope);
15809 }
15810 else
15811 list_to_add = cu->list_in_scope;
15812 }
15813 else
15814 {
15815 /* We do not know the address of this symbol.
15816 If it is an external symbol and we have type information
15817 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15818 The address of the variable will then be determined from
15819 the minimal symbol table whenever the variable is
15820 referenced. */
15821 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15822
15823 /* Fortran explicitly imports any global symbols to the local
15824 scope by DW_TAG_common_block. */
15825 if (cu->language == language_fortran && die->parent
15826 && die->parent->tag == DW_TAG_common_block)
15827 {
15828 /* SYMBOL_CLASS doesn't matter here because
15829 read_common_block is going to reset it. */
15830 if (!suppress_add)
15831 list_to_add = cu->list_in_scope;
15832 }
15833 else if (attr2 && (DW_UNSND (attr2) != 0)
15834 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15835 {
15836 /* A variable with DW_AT_external is never static, but it
15837 may be block-scoped. */
15838 list_to_add = (cu->list_in_scope == &file_symbols
15839 ? &global_symbols : cu->list_in_scope);
15840
15841 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15842 }
15843 else if (!die_is_declaration (die, cu))
15844 {
15845 /* Use the default LOC_OPTIMIZED_OUT class. */
15846 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15847 if (!suppress_add)
15848 list_to_add = cu->list_in_scope;
15849 }
15850 }
15851 break;
15852 case DW_TAG_formal_parameter:
15853 /* If we are inside a function, mark this as an argument. If
15854 not, we might be looking at an argument to an inlined function
15855 when we do not have enough information to show inlined frames;
15856 pretend it's a local variable in that case so that the user can
15857 still see it. */
15858 if (context_stack_depth > 0
15859 && context_stack[context_stack_depth - 1].name != NULL)
15860 SYMBOL_IS_ARGUMENT (sym) = 1;
15861 attr = dwarf2_attr (die, DW_AT_location, cu);
15862 if (attr)
15863 {
15864 var_decode_location (attr, sym, cu);
15865 }
15866 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15867 if (attr)
15868 {
15869 dwarf2_const_value (attr, sym, cu);
15870 }
15871
15872 list_to_add = cu->list_in_scope;
15873 break;
15874 case DW_TAG_unspecified_parameters:
15875 /* From varargs functions; gdb doesn't seem to have any
15876 interest in this information, so just ignore it for now.
15877 (FIXME?) */
15878 break;
15879 case DW_TAG_template_type_param:
15880 suppress_add = 1;
15881 /* Fall through. */
15882 case DW_TAG_class_type:
15883 case DW_TAG_interface_type:
15884 case DW_TAG_structure_type:
15885 case DW_TAG_union_type:
15886 case DW_TAG_set_type:
15887 case DW_TAG_enumeration_type:
15888 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15889 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15890
15891 {
15892 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15893 really ever be static objects: otherwise, if you try
15894 to, say, break of a class's method and you're in a file
15895 which doesn't mention that class, it won't work unless
15896 the check for all static symbols in lookup_symbol_aux
15897 saves you. See the OtherFileClass tests in
15898 gdb.c++/namespace.exp. */
15899
15900 if (!suppress_add)
15901 {
15902 list_to_add = (cu->list_in_scope == &file_symbols
15903 && (cu->language == language_cplus
15904 || cu->language == language_java)
15905 ? &global_symbols : cu->list_in_scope);
15906
15907 /* The semantics of C++ state that "struct foo {
15908 ... }" also defines a typedef for "foo". A Java
15909 class declaration also defines a typedef for the
15910 class. */
15911 if (cu->language == language_cplus
15912 || cu->language == language_java
15913 || cu->language == language_ada)
15914 {
15915 /* The symbol's name is already allocated along
15916 with this objfile, so we don't need to
15917 duplicate it for the type. */
15918 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15919 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15920 }
15921 }
15922 }
15923 break;
15924 case DW_TAG_typedef:
15925 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15926 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15927 list_to_add = cu->list_in_scope;
15928 break;
15929 case DW_TAG_base_type:
15930 case DW_TAG_subrange_type:
15931 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15932 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15933 list_to_add = cu->list_in_scope;
15934 break;
15935 case DW_TAG_enumerator:
15936 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15937 if (attr)
15938 {
15939 dwarf2_const_value (attr, sym, cu);
15940 }
15941 {
15942 /* NOTE: carlton/2003-11-10: See comment above in the
15943 DW_TAG_class_type, etc. block. */
15944
15945 list_to_add = (cu->list_in_scope == &file_symbols
15946 && (cu->language == language_cplus
15947 || cu->language == language_java)
15948 ? &global_symbols : cu->list_in_scope);
15949 }
15950 break;
15951 case DW_TAG_namespace:
15952 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15953 list_to_add = &global_symbols;
15954 break;
15955 case DW_TAG_common_block:
15956 SYMBOL_CLASS (sym) = LOC_STATIC;
15957 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15958 add_symbol_to_list (sym, cu->list_in_scope);
15959 break;
15960 default:
15961 /* Not a tag we recognize. Hopefully we aren't processing
15962 trash data, but since we must specifically ignore things
15963 we don't recognize, there is nothing else we should do at
15964 this point. */
15965 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15966 dwarf_tag_name (die->tag));
15967 break;
15968 }
15969
15970 if (suppress_add)
15971 {
15972 sym->hash_next = objfile->template_symbols;
15973 objfile->template_symbols = sym;
15974 list_to_add = NULL;
15975 }
15976
15977 if (list_to_add != NULL)
15978 add_symbol_to_list (sym, list_to_add);
15979
15980 /* For the benefit of old versions of GCC, check for anonymous
15981 namespaces based on the demangled name. */
15982 if (!processing_has_namespace_info
15983 && cu->language == language_cplus)
15984 cp_scan_for_anonymous_namespaces (sym, objfile);
15985 }
15986 return (sym);
15987 }
15988
15989 /* A wrapper for new_symbol_full that always allocates a new symbol. */
15990
15991 static struct symbol *
15992 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15993 {
15994 return new_symbol_full (die, type, cu, NULL);
15995 }
15996
15997 /* Given an attr with a DW_FORM_dataN value in host byte order,
15998 zero-extend it as appropriate for the symbol's type. The DWARF
15999 standard (v4) is not entirely clear about the meaning of using
16000 DW_FORM_dataN for a constant with a signed type, where the type is
16001 wider than the data. The conclusion of a discussion on the DWARF
16002 list was that this is unspecified. We choose to always zero-extend
16003 because that is the interpretation long in use by GCC. */
16004
16005 static gdb_byte *
16006 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16007 const char *name, struct obstack *obstack,
16008 struct dwarf2_cu *cu, LONGEST *value, int bits)
16009 {
16010 struct objfile *objfile = cu->objfile;
16011 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16012 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16013 LONGEST l = DW_UNSND (attr);
16014
16015 if (bits < sizeof (*value) * 8)
16016 {
16017 l &= ((LONGEST) 1 << bits) - 1;
16018 *value = l;
16019 }
16020 else if (bits == sizeof (*value) * 8)
16021 *value = l;
16022 else
16023 {
16024 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16025 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16026 return bytes;
16027 }
16028
16029 return NULL;
16030 }
16031
16032 /* Read a constant value from an attribute. Either set *VALUE, or if
16033 the value does not fit in *VALUE, set *BYTES - either already
16034 allocated on the objfile obstack, or newly allocated on OBSTACK,
16035 or, set *BATON, if we translated the constant to a location
16036 expression. */
16037
16038 static void
16039 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16040 const char *name, struct obstack *obstack,
16041 struct dwarf2_cu *cu,
16042 LONGEST *value, gdb_byte **bytes,
16043 struct dwarf2_locexpr_baton **baton)
16044 {
16045 struct objfile *objfile = cu->objfile;
16046 struct comp_unit_head *cu_header = &cu->header;
16047 struct dwarf_block *blk;
16048 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16049 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16050
16051 *value = 0;
16052 *bytes = NULL;
16053 *baton = NULL;
16054
16055 switch (attr->form)
16056 {
16057 case DW_FORM_addr:
16058 case DW_FORM_GNU_addr_index:
16059 {
16060 gdb_byte *data;
16061
16062 if (TYPE_LENGTH (type) != cu_header->addr_size)
16063 dwarf2_const_value_length_mismatch_complaint (name,
16064 cu_header->addr_size,
16065 TYPE_LENGTH (type));
16066 /* Symbols of this form are reasonably rare, so we just
16067 piggyback on the existing location code rather than writing
16068 a new implementation of symbol_computed_ops. */
16069 *baton = obstack_alloc (&objfile->objfile_obstack,
16070 sizeof (struct dwarf2_locexpr_baton));
16071 (*baton)->per_cu = cu->per_cu;
16072 gdb_assert ((*baton)->per_cu);
16073
16074 (*baton)->size = 2 + cu_header->addr_size;
16075 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16076 (*baton)->data = data;
16077
16078 data[0] = DW_OP_addr;
16079 store_unsigned_integer (&data[1], cu_header->addr_size,
16080 byte_order, DW_ADDR (attr));
16081 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16082 }
16083 break;
16084 case DW_FORM_string:
16085 case DW_FORM_strp:
16086 case DW_FORM_GNU_str_index:
16087 case DW_FORM_GNU_strp_alt:
16088 /* DW_STRING is already allocated on the objfile obstack, point
16089 directly to it. */
16090 *bytes = (gdb_byte *) DW_STRING (attr);
16091 break;
16092 case DW_FORM_block1:
16093 case DW_FORM_block2:
16094 case DW_FORM_block4:
16095 case DW_FORM_block:
16096 case DW_FORM_exprloc:
16097 blk = DW_BLOCK (attr);
16098 if (TYPE_LENGTH (type) != blk->size)
16099 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16100 TYPE_LENGTH (type));
16101 *bytes = blk->data;
16102 break;
16103
16104 /* The DW_AT_const_value attributes are supposed to carry the
16105 symbol's value "represented as it would be on the target
16106 architecture." By the time we get here, it's already been
16107 converted to host endianness, so we just need to sign- or
16108 zero-extend it as appropriate. */
16109 case DW_FORM_data1:
16110 *bytes = dwarf2_const_value_data (attr, type, name,
16111 obstack, cu, value, 8);
16112 break;
16113 case DW_FORM_data2:
16114 *bytes = dwarf2_const_value_data (attr, type, name,
16115 obstack, cu, value, 16);
16116 break;
16117 case DW_FORM_data4:
16118 *bytes = dwarf2_const_value_data (attr, type, name,
16119 obstack, cu, value, 32);
16120 break;
16121 case DW_FORM_data8:
16122 *bytes = dwarf2_const_value_data (attr, type, name,
16123 obstack, cu, value, 64);
16124 break;
16125
16126 case DW_FORM_sdata:
16127 *value = DW_SND (attr);
16128 break;
16129
16130 case DW_FORM_udata:
16131 *value = DW_UNSND (attr);
16132 break;
16133
16134 default:
16135 complaint (&symfile_complaints,
16136 _("unsupported const value attribute form: '%s'"),
16137 dwarf_form_name (attr->form));
16138 *value = 0;
16139 break;
16140 }
16141 }
16142
16143
16144 /* Copy constant value from an attribute to a symbol. */
16145
16146 static void
16147 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16148 struct dwarf2_cu *cu)
16149 {
16150 struct objfile *objfile = cu->objfile;
16151 struct comp_unit_head *cu_header = &cu->header;
16152 LONGEST value;
16153 gdb_byte *bytes;
16154 struct dwarf2_locexpr_baton *baton;
16155
16156 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16157 SYMBOL_PRINT_NAME (sym),
16158 &objfile->objfile_obstack, cu,
16159 &value, &bytes, &baton);
16160
16161 if (baton != NULL)
16162 {
16163 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16164 SYMBOL_LOCATION_BATON (sym) = baton;
16165 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16166 }
16167 else if (bytes != NULL)
16168 {
16169 SYMBOL_VALUE_BYTES (sym) = bytes;
16170 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16171 }
16172 else
16173 {
16174 SYMBOL_VALUE (sym) = value;
16175 SYMBOL_CLASS (sym) = LOC_CONST;
16176 }
16177 }
16178
16179 /* Return the type of the die in question using its DW_AT_type attribute. */
16180
16181 static struct type *
16182 die_type (struct die_info *die, struct dwarf2_cu *cu)
16183 {
16184 struct attribute *type_attr;
16185
16186 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16187 if (!type_attr)
16188 {
16189 /* A missing DW_AT_type represents a void type. */
16190 return objfile_type (cu->objfile)->builtin_void;
16191 }
16192
16193 return lookup_die_type (die, type_attr, cu);
16194 }
16195
16196 /* True iff CU's producer generates GNAT Ada auxiliary information
16197 that allows to find parallel types through that information instead
16198 of having to do expensive parallel lookups by type name. */
16199
16200 static int
16201 need_gnat_info (struct dwarf2_cu *cu)
16202 {
16203 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16204 of GNAT produces this auxiliary information, without any indication
16205 that it is produced. Part of enhancing the FSF version of GNAT
16206 to produce that information will be to put in place an indicator
16207 that we can use in order to determine whether the descriptive type
16208 info is available or not. One suggestion that has been made is
16209 to use a new attribute, attached to the CU die. For now, assume
16210 that the descriptive type info is not available. */
16211 return 0;
16212 }
16213
16214 /* Return the auxiliary type of the die in question using its
16215 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16216 attribute is not present. */
16217
16218 static struct type *
16219 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16220 {
16221 struct attribute *type_attr;
16222
16223 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16224 if (!type_attr)
16225 return NULL;
16226
16227 return lookup_die_type (die, type_attr, cu);
16228 }
16229
16230 /* If DIE has a descriptive_type attribute, then set the TYPE's
16231 descriptive type accordingly. */
16232
16233 static void
16234 set_descriptive_type (struct type *type, struct die_info *die,
16235 struct dwarf2_cu *cu)
16236 {
16237 struct type *descriptive_type = die_descriptive_type (die, cu);
16238
16239 if (descriptive_type)
16240 {
16241 ALLOCATE_GNAT_AUX_TYPE (type);
16242 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16243 }
16244 }
16245
16246 /* Return the containing type of the die in question using its
16247 DW_AT_containing_type attribute. */
16248
16249 static struct type *
16250 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16251 {
16252 struct attribute *type_attr;
16253
16254 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16255 if (!type_attr)
16256 error (_("Dwarf Error: Problem turning containing type into gdb type "
16257 "[in module %s]"), cu->objfile->name);
16258
16259 return lookup_die_type (die, type_attr, cu);
16260 }
16261
16262 /* Look up the type of DIE in CU using its type attribute ATTR.
16263 If there is no type substitute an error marker. */
16264
16265 static struct type *
16266 lookup_die_type (struct die_info *die, struct attribute *attr,
16267 struct dwarf2_cu *cu)
16268 {
16269 struct objfile *objfile = cu->objfile;
16270 struct type *this_type;
16271
16272 /* First see if we have it cached. */
16273
16274 if (attr->form == DW_FORM_GNU_ref_alt)
16275 {
16276 struct dwarf2_per_cu_data *per_cu;
16277 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16278
16279 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16280 this_type = get_die_type_at_offset (offset, per_cu);
16281 }
16282 else if (is_ref_attr (attr))
16283 {
16284 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16285
16286 this_type = get_die_type_at_offset (offset, cu->per_cu);
16287 }
16288 else if (attr->form == DW_FORM_ref_sig8)
16289 {
16290 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16291
16292 /* sig_type will be NULL if the signatured type is missing from
16293 the debug info. */
16294 if (sig_type == NULL)
16295 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16296 "at 0x%x [in module %s]"),
16297 die->offset.sect_off, objfile->name);
16298
16299 gdb_assert (sig_type->per_cu.is_debug_types);
16300 /* If we haven't filled in type_offset_in_section yet, then we
16301 haven't read the type in yet. */
16302 this_type = NULL;
16303 if (sig_type->type_offset_in_section.sect_off != 0)
16304 {
16305 this_type =
16306 get_die_type_at_offset (sig_type->type_offset_in_section,
16307 &sig_type->per_cu);
16308 }
16309 }
16310 else
16311 {
16312 dump_die_for_error (die);
16313 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16314 dwarf_attr_name (attr->name), objfile->name);
16315 }
16316
16317 /* If not cached we need to read it in. */
16318
16319 if (this_type == NULL)
16320 {
16321 struct die_info *type_die;
16322 struct dwarf2_cu *type_cu = cu;
16323
16324 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16325 /* If we found the type now, it's probably because the type came
16326 from an inter-CU reference and the type's CU got expanded before
16327 ours. */
16328 this_type = get_die_type (type_die, type_cu);
16329 if (this_type == NULL)
16330 this_type = read_type_die_1 (type_die, type_cu);
16331 }
16332
16333 /* If we still don't have a type use an error marker. */
16334
16335 if (this_type == NULL)
16336 {
16337 char *message, *saved;
16338
16339 /* read_type_die already issued a complaint. */
16340 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16341 objfile->name,
16342 cu->header.offset.sect_off,
16343 die->offset.sect_off);
16344 saved = obstack_copy0 (&objfile->objfile_obstack,
16345 message, strlen (message));
16346 xfree (message);
16347
16348 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16349 }
16350
16351 return this_type;
16352 }
16353
16354 /* Return the type in DIE, CU.
16355 Returns NULL for invalid types.
16356
16357 This first does a lookup in the appropriate type_hash table,
16358 and only reads the die in if necessary.
16359
16360 NOTE: This can be called when reading in partial or full symbols. */
16361
16362 static struct type *
16363 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16364 {
16365 struct type *this_type;
16366
16367 this_type = get_die_type (die, cu);
16368 if (this_type)
16369 return this_type;
16370
16371 return read_type_die_1 (die, cu);
16372 }
16373
16374 /* Read the type in DIE, CU.
16375 Returns NULL for invalid types. */
16376
16377 static struct type *
16378 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16379 {
16380 struct type *this_type = NULL;
16381
16382 switch (die->tag)
16383 {
16384 case DW_TAG_class_type:
16385 case DW_TAG_interface_type:
16386 case DW_TAG_structure_type:
16387 case DW_TAG_union_type:
16388 this_type = read_structure_type (die, cu);
16389 break;
16390 case DW_TAG_enumeration_type:
16391 this_type = read_enumeration_type (die, cu);
16392 break;
16393 case DW_TAG_subprogram:
16394 case DW_TAG_subroutine_type:
16395 case DW_TAG_inlined_subroutine:
16396 this_type = read_subroutine_type (die, cu);
16397 break;
16398 case DW_TAG_array_type:
16399 this_type = read_array_type (die, cu);
16400 break;
16401 case DW_TAG_set_type:
16402 this_type = read_set_type (die, cu);
16403 break;
16404 case DW_TAG_pointer_type:
16405 this_type = read_tag_pointer_type (die, cu);
16406 break;
16407 case DW_TAG_ptr_to_member_type:
16408 this_type = read_tag_ptr_to_member_type (die, cu);
16409 break;
16410 case DW_TAG_reference_type:
16411 this_type = read_tag_reference_type (die, cu);
16412 break;
16413 case DW_TAG_const_type:
16414 this_type = read_tag_const_type (die, cu);
16415 break;
16416 case DW_TAG_volatile_type:
16417 this_type = read_tag_volatile_type (die, cu);
16418 break;
16419 case DW_TAG_string_type:
16420 this_type = read_tag_string_type (die, cu);
16421 break;
16422 case DW_TAG_typedef:
16423 this_type = read_typedef (die, cu);
16424 break;
16425 case DW_TAG_subrange_type:
16426 this_type = read_subrange_type (die, cu);
16427 break;
16428 case DW_TAG_base_type:
16429 this_type = read_base_type (die, cu);
16430 break;
16431 case DW_TAG_unspecified_type:
16432 this_type = read_unspecified_type (die, cu);
16433 break;
16434 case DW_TAG_namespace:
16435 this_type = read_namespace_type (die, cu);
16436 break;
16437 case DW_TAG_module:
16438 this_type = read_module_type (die, cu);
16439 break;
16440 default:
16441 complaint (&symfile_complaints,
16442 _("unexpected tag in read_type_die: '%s'"),
16443 dwarf_tag_name (die->tag));
16444 break;
16445 }
16446
16447 return this_type;
16448 }
16449
16450 /* See if we can figure out if the class lives in a namespace. We do
16451 this by looking for a member function; its demangled name will
16452 contain namespace info, if there is any.
16453 Return the computed name or NULL.
16454 Space for the result is allocated on the objfile's obstack.
16455 This is the full-die version of guess_partial_die_structure_name.
16456 In this case we know DIE has no useful parent. */
16457
16458 static char *
16459 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16460 {
16461 struct die_info *spec_die;
16462 struct dwarf2_cu *spec_cu;
16463 struct die_info *child;
16464
16465 spec_cu = cu;
16466 spec_die = die_specification (die, &spec_cu);
16467 if (spec_die != NULL)
16468 {
16469 die = spec_die;
16470 cu = spec_cu;
16471 }
16472
16473 for (child = die->child;
16474 child != NULL;
16475 child = child->sibling)
16476 {
16477 if (child->tag == DW_TAG_subprogram)
16478 {
16479 struct attribute *attr;
16480
16481 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16482 if (attr == NULL)
16483 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16484 if (attr != NULL)
16485 {
16486 char *actual_name
16487 = language_class_name_from_physname (cu->language_defn,
16488 DW_STRING (attr));
16489 char *name = NULL;
16490
16491 if (actual_name != NULL)
16492 {
16493 char *die_name = dwarf2_name (die, cu);
16494
16495 if (die_name != NULL
16496 && strcmp (die_name, actual_name) != 0)
16497 {
16498 /* Strip off the class name from the full name.
16499 We want the prefix. */
16500 int die_name_len = strlen (die_name);
16501 int actual_name_len = strlen (actual_name);
16502
16503 /* Test for '::' as a sanity check. */
16504 if (actual_name_len > die_name_len + 2
16505 && actual_name[actual_name_len
16506 - die_name_len - 1] == ':')
16507 name =
16508 obsavestring (actual_name,
16509 actual_name_len - die_name_len - 2,
16510 &cu->objfile->objfile_obstack);
16511 }
16512 }
16513 xfree (actual_name);
16514 return name;
16515 }
16516 }
16517 }
16518
16519 return NULL;
16520 }
16521
16522 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16523 prefix part in such case. See
16524 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16525
16526 static char *
16527 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16528 {
16529 struct attribute *attr;
16530 char *base;
16531
16532 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16533 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16534 return NULL;
16535
16536 attr = dwarf2_attr (die, DW_AT_name, cu);
16537 if (attr != NULL && DW_STRING (attr) != NULL)
16538 return NULL;
16539
16540 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16541 if (attr == NULL)
16542 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16543 if (attr == NULL || DW_STRING (attr) == NULL)
16544 return NULL;
16545
16546 /* dwarf2_name had to be already called. */
16547 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16548
16549 /* Strip the base name, keep any leading namespaces/classes. */
16550 base = strrchr (DW_STRING (attr), ':');
16551 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16552 return "";
16553
16554 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16555 &cu->objfile->objfile_obstack);
16556 }
16557
16558 /* Return the name of the namespace/class that DIE is defined within,
16559 or "" if we can't tell. The caller should not xfree the result.
16560
16561 For example, if we're within the method foo() in the following
16562 code:
16563
16564 namespace N {
16565 class C {
16566 void foo () {
16567 }
16568 };
16569 }
16570
16571 then determine_prefix on foo's die will return "N::C". */
16572
16573 static const char *
16574 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16575 {
16576 struct die_info *parent, *spec_die;
16577 struct dwarf2_cu *spec_cu;
16578 struct type *parent_type;
16579 char *retval;
16580
16581 if (cu->language != language_cplus && cu->language != language_java
16582 && cu->language != language_fortran)
16583 return "";
16584
16585 retval = anonymous_struct_prefix (die, cu);
16586 if (retval)
16587 return retval;
16588
16589 /* We have to be careful in the presence of DW_AT_specification.
16590 For example, with GCC 3.4, given the code
16591
16592 namespace N {
16593 void foo() {
16594 // Definition of N::foo.
16595 }
16596 }
16597
16598 then we'll have a tree of DIEs like this:
16599
16600 1: DW_TAG_compile_unit
16601 2: DW_TAG_namespace // N
16602 3: DW_TAG_subprogram // declaration of N::foo
16603 4: DW_TAG_subprogram // definition of N::foo
16604 DW_AT_specification // refers to die #3
16605
16606 Thus, when processing die #4, we have to pretend that we're in
16607 the context of its DW_AT_specification, namely the contex of die
16608 #3. */
16609 spec_cu = cu;
16610 spec_die = die_specification (die, &spec_cu);
16611 if (spec_die == NULL)
16612 parent = die->parent;
16613 else
16614 {
16615 parent = spec_die->parent;
16616 cu = spec_cu;
16617 }
16618
16619 if (parent == NULL)
16620 return "";
16621 else if (parent->building_fullname)
16622 {
16623 const char *name;
16624 const char *parent_name;
16625
16626 /* It has been seen on RealView 2.2 built binaries,
16627 DW_TAG_template_type_param types actually _defined_ as
16628 children of the parent class:
16629
16630 enum E {};
16631 template class <class Enum> Class{};
16632 Class<enum E> class_e;
16633
16634 1: DW_TAG_class_type (Class)
16635 2: DW_TAG_enumeration_type (E)
16636 3: DW_TAG_enumerator (enum1:0)
16637 3: DW_TAG_enumerator (enum2:1)
16638 ...
16639 2: DW_TAG_template_type_param
16640 DW_AT_type DW_FORM_ref_udata (E)
16641
16642 Besides being broken debug info, it can put GDB into an
16643 infinite loop. Consider:
16644
16645 When we're building the full name for Class<E>, we'll start
16646 at Class, and go look over its template type parameters,
16647 finding E. We'll then try to build the full name of E, and
16648 reach here. We're now trying to build the full name of E,
16649 and look over the parent DIE for containing scope. In the
16650 broken case, if we followed the parent DIE of E, we'd again
16651 find Class, and once again go look at its template type
16652 arguments, etc., etc. Simply don't consider such parent die
16653 as source-level parent of this die (it can't be, the language
16654 doesn't allow it), and break the loop here. */
16655 name = dwarf2_name (die, cu);
16656 parent_name = dwarf2_name (parent, cu);
16657 complaint (&symfile_complaints,
16658 _("template param type '%s' defined within parent '%s'"),
16659 name ? name : "<unknown>",
16660 parent_name ? parent_name : "<unknown>");
16661 return "";
16662 }
16663 else
16664 switch (parent->tag)
16665 {
16666 case DW_TAG_namespace:
16667 parent_type = read_type_die (parent, cu);
16668 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16669 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16670 Work around this problem here. */
16671 if (cu->language == language_cplus
16672 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16673 return "";
16674 /* We give a name to even anonymous namespaces. */
16675 return TYPE_TAG_NAME (parent_type);
16676 case DW_TAG_class_type:
16677 case DW_TAG_interface_type:
16678 case DW_TAG_structure_type:
16679 case DW_TAG_union_type:
16680 case DW_TAG_module:
16681 parent_type = read_type_die (parent, cu);
16682 if (TYPE_TAG_NAME (parent_type) != NULL)
16683 return TYPE_TAG_NAME (parent_type);
16684 else
16685 /* An anonymous structure is only allowed non-static data
16686 members; no typedefs, no member functions, et cetera.
16687 So it does not need a prefix. */
16688 return "";
16689 case DW_TAG_compile_unit:
16690 case DW_TAG_partial_unit:
16691 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16692 if (cu->language == language_cplus
16693 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16694 && die->child != NULL
16695 && (die->tag == DW_TAG_class_type
16696 || die->tag == DW_TAG_structure_type
16697 || die->tag == DW_TAG_union_type))
16698 {
16699 char *name = guess_full_die_structure_name (die, cu);
16700 if (name != NULL)
16701 return name;
16702 }
16703 return "";
16704 default:
16705 return determine_prefix (parent, cu);
16706 }
16707 }
16708
16709 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16710 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16711 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16712 an obconcat, otherwise allocate storage for the result. The CU argument is
16713 used to determine the language and hence, the appropriate separator. */
16714
16715 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16716
16717 static char *
16718 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16719 int physname, struct dwarf2_cu *cu)
16720 {
16721 const char *lead = "";
16722 const char *sep;
16723
16724 if (suffix == NULL || suffix[0] == '\0'
16725 || prefix == NULL || prefix[0] == '\0')
16726 sep = "";
16727 else if (cu->language == language_java)
16728 sep = ".";
16729 else if (cu->language == language_fortran && physname)
16730 {
16731 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16732 DW_AT_MIPS_linkage_name is preferred and used instead. */
16733
16734 lead = "__";
16735 sep = "_MOD_";
16736 }
16737 else
16738 sep = "::";
16739
16740 if (prefix == NULL)
16741 prefix = "";
16742 if (suffix == NULL)
16743 suffix = "";
16744
16745 if (obs == NULL)
16746 {
16747 char *retval
16748 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16749
16750 strcpy (retval, lead);
16751 strcat (retval, prefix);
16752 strcat (retval, sep);
16753 strcat (retval, suffix);
16754 return retval;
16755 }
16756 else
16757 {
16758 /* We have an obstack. */
16759 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16760 }
16761 }
16762
16763 /* Return sibling of die, NULL if no sibling. */
16764
16765 static struct die_info *
16766 sibling_die (struct die_info *die)
16767 {
16768 return die->sibling;
16769 }
16770
16771 /* Get name of a die, return NULL if not found. */
16772
16773 static char *
16774 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16775 struct obstack *obstack)
16776 {
16777 if (name && cu->language == language_cplus)
16778 {
16779 char *canon_name = cp_canonicalize_string (name);
16780
16781 if (canon_name != NULL)
16782 {
16783 if (strcmp (canon_name, name) != 0)
16784 name = obsavestring (canon_name, strlen (canon_name),
16785 obstack);
16786 xfree (canon_name);
16787 }
16788 }
16789
16790 return name;
16791 }
16792
16793 /* Get name of a die, return NULL if not found. */
16794
16795 static char *
16796 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16797 {
16798 struct attribute *attr;
16799
16800 attr = dwarf2_attr (die, DW_AT_name, cu);
16801 if ((!attr || !DW_STRING (attr))
16802 && die->tag != DW_TAG_class_type
16803 && die->tag != DW_TAG_interface_type
16804 && die->tag != DW_TAG_structure_type
16805 && die->tag != DW_TAG_union_type)
16806 return NULL;
16807
16808 switch (die->tag)
16809 {
16810 case DW_TAG_compile_unit:
16811 case DW_TAG_partial_unit:
16812 /* Compilation units have a DW_AT_name that is a filename, not
16813 a source language identifier. */
16814 case DW_TAG_enumeration_type:
16815 case DW_TAG_enumerator:
16816 /* These tags always have simple identifiers already; no need
16817 to canonicalize them. */
16818 return DW_STRING (attr);
16819
16820 case DW_TAG_subprogram:
16821 /* Java constructors will all be named "<init>", so return
16822 the class name when we see this special case. */
16823 if (cu->language == language_java
16824 && DW_STRING (attr) != NULL
16825 && strcmp (DW_STRING (attr), "<init>") == 0)
16826 {
16827 struct dwarf2_cu *spec_cu = cu;
16828 struct die_info *spec_die;
16829
16830 /* GCJ will output '<init>' for Java constructor names.
16831 For this special case, return the name of the parent class. */
16832
16833 /* GCJ may output suprogram DIEs with AT_specification set.
16834 If so, use the name of the specified DIE. */
16835 spec_die = die_specification (die, &spec_cu);
16836 if (spec_die != NULL)
16837 return dwarf2_name (spec_die, spec_cu);
16838
16839 do
16840 {
16841 die = die->parent;
16842 if (die->tag == DW_TAG_class_type)
16843 return dwarf2_name (die, cu);
16844 }
16845 while (die->tag != DW_TAG_compile_unit
16846 && die->tag != DW_TAG_partial_unit);
16847 }
16848 break;
16849
16850 case DW_TAG_class_type:
16851 case DW_TAG_interface_type:
16852 case DW_TAG_structure_type:
16853 case DW_TAG_union_type:
16854 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16855 structures or unions. These were of the form "._%d" in GCC 4.1,
16856 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16857 and GCC 4.4. We work around this problem by ignoring these. */
16858 if (attr && DW_STRING (attr)
16859 && (strncmp (DW_STRING (attr), "._", 2) == 0
16860 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16861 return NULL;
16862
16863 /* GCC might emit a nameless typedef that has a linkage name. See
16864 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16865 if (!attr || DW_STRING (attr) == NULL)
16866 {
16867 char *demangled = NULL;
16868
16869 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16870 if (attr == NULL)
16871 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16872
16873 if (attr == NULL || DW_STRING (attr) == NULL)
16874 return NULL;
16875
16876 /* Avoid demangling DW_STRING (attr) the second time on a second
16877 call for the same DIE. */
16878 if (!DW_STRING_IS_CANONICAL (attr))
16879 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16880
16881 if (demangled)
16882 {
16883 char *base;
16884
16885 /* FIXME: we already did this for the partial symbol... */
16886 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16887 &cu->objfile->objfile_obstack);
16888 DW_STRING_IS_CANONICAL (attr) = 1;
16889 xfree (demangled);
16890
16891 /* Strip any leading namespaces/classes, keep only the base name.
16892 DW_AT_name for named DIEs does not contain the prefixes. */
16893 base = strrchr (DW_STRING (attr), ':');
16894 if (base && base > DW_STRING (attr) && base[-1] == ':')
16895 return &base[1];
16896 else
16897 return DW_STRING (attr);
16898 }
16899 }
16900 break;
16901
16902 default:
16903 break;
16904 }
16905
16906 if (!DW_STRING_IS_CANONICAL (attr))
16907 {
16908 DW_STRING (attr)
16909 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16910 &cu->objfile->objfile_obstack);
16911 DW_STRING_IS_CANONICAL (attr) = 1;
16912 }
16913 return DW_STRING (attr);
16914 }
16915
16916 /* Return the die that this die in an extension of, or NULL if there
16917 is none. *EXT_CU is the CU containing DIE on input, and the CU
16918 containing the return value on output. */
16919
16920 static struct die_info *
16921 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16922 {
16923 struct attribute *attr;
16924
16925 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16926 if (attr == NULL)
16927 return NULL;
16928
16929 return follow_die_ref (die, attr, ext_cu);
16930 }
16931
16932 /* Convert a DIE tag into its string name. */
16933
16934 static const char *
16935 dwarf_tag_name (unsigned tag)
16936 {
16937 const char *name = get_DW_TAG_name (tag);
16938
16939 if (name == NULL)
16940 return "DW_TAG_<unknown>";
16941
16942 return name;
16943 }
16944
16945 /* Convert a DWARF attribute code into its string name. */
16946
16947 static const char *
16948 dwarf_attr_name (unsigned attr)
16949 {
16950 const char *name;
16951
16952 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16953 if (attr == DW_AT_MIPS_fde)
16954 return "DW_AT_MIPS_fde";
16955 #else
16956 if (attr == DW_AT_HP_block_index)
16957 return "DW_AT_HP_block_index";
16958 #endif
16959
16960 name = get_DW_AT_name (attr);
16961
16962 if (name == NULL)
16963 return "DW_AT_<unknown>";
16964
16965 return name;
16966 }
16967
16968 /* Convert a DWARF value form code into its string name. */
16969
16970 static const char *
16971 dwarf_form_name (unsigned form)
16972 {
16973 const char *name = get_DW_FORM_name (form);
16974
16975 if (name == NULL)
16976 return "DW_FORM_<unknown>";
16977
16978 return name;
16979 }
16980
16981 static char *
16982 dwarf_bool_name (unsigned mybool)
16983 {
16984 if (mybool)
16985 return "TRUE";
16986 else
16987 return "FALSE";
16988 }
16989
16990 /* Convert a DWARF type code into its string name. */
16991
16992 static const char *
16993 dwarf_type_encoding_name (unsigned enc)
16994 {
16995 const char *name = get_DW_ATE_name (enc);
16996
16997 if (name == NULL)
16998 return "DW_ATE_<unknown>";
16999
17000 return name;
17001 }
17002
17003 static void
17004 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17005 {
17006 unsigned int i;
17007
17008 print_spaces (indent, f);
17009 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17010 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17011
17012 if (die->parent != NULL)
17013 {
17014 print_spaces (indent, f);
17015 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17016 die->parent->offset.sect_off);
17017 }
17018
17019 print_spaces (indent, f);
17020 fprintf_unfiltered (f, " has children: %s\n",
17021 dwarf_bool_name (die->child != NULL));
17022
17023 print_spaces (indent, f);
17024 fprintf_unfiltered (f, " attributes:\n");
17025
17026 for (i = 0; i < die->num_attrs; ++i)
17027 {
17028 print_spaces (indent, f);
17029 fprintf_unfiltered (f, " %s (%s) ",
17030 dwarf_attr_name (die->attrs[i].name),
17031 dwarf_form_name (die->attrs[i].form));
17032
17033 switch (die->attrs[i].form)
17034 {
17035 case DW_FORM_addr:
17036 case DW_FORM_GNU_addr_index:
17037 fprintf_unfiltered (f, "address: ");
17038 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17039 break;
17040 case DW_FORM_block2:
17041 case DW_FORM_block4:
17042 case DW_FORM_block:
17043 case DW_FORM_block1:
17044 fprintf_unfiltered (f, "block: size %s",
17045 pulongest (DW_BLOCK (&die->attrs[i])->size));
17046 break;
17047 case DW_FORM_exprloc:
17048 fprintf_unfiltered (f, "expression: size %s",
17049 pulongest (DW_BLOCK (&die->attrs[i])->size));
17050 break;
17051 case DW_FORM_ref_addr:
17052 fprintf_unfiltered (f, "ref address: ");
17053 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17054 break;
17055 case DW_FORM_GNU_ref_alt:
17056 fprintf_unfiltered (f, "alt ref address: ");
17057 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17058 break;
17059 case DW_FORM_ref1:
17060 case DW_FORM_ref2:
17061 case DW_FORM_ref4:
17062 case DW_FORM_ref8:
17063 case DW_FORM_ref_udata:
17064 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17065 (long) (DW_UNSND (&die->attrs[i])));
17066 break;
17067 case DW_FORM_data1:
17068 case DW_FORM_data2:
17069 case DW_FORM_data4:
17070 case DW_FORM_data8:
17071 case DW_FORM_udata:
17072 case DW_FORM_sdata:
17073 fprintf_unfiltered (f, "constant: %s",
17074 pulongest (DW_UNSND (&die->attrs[i])));
17075 break;
17076 case DW_FORM_sec_offset:
17077 fprintf_unfiltered (f, "section offset: %s",
17078 pulongest (DW_UNSND (&die->attrs[i])));
17079 break;
17080 case DW_FORM_ref_sig8:
17081 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17082 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17083 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17084 else
17085 fprintf_unfiltered (f, "signatured type, offset: unknown");
17086 break;
17087 case DW_FORM_string:
17088 case DW_FORM_strp:
17089 case DW_FORM_GNU_str_index:
17090 case DW_FORM_GNU_strp_alt:
17091 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17092 DW_STRING (&die->attrs[i])
17093 ? DW_STRING (&die->attrs[i]) : "",
17094 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17095 break;
17096 case DW_FORM_flag:
17097 if (DW_UNSND (&die->attrs[i]))
17098 fprintf_unfiltered (f, "flag: TRUE");
17099 else
17100 fprintf_unfiltered (f, "flag: FALSE");
17101 break;
17102 case DW_FORM_flag_present:
17103 fprintf_unfiltered (f, "flag: TRUE");
17104 break;
17105 case DW_FORM_indirect:
17106 /* The reader will have reduced the indirect form to
17107 the "base form" so this form should not occur. */
17108 fprintf_unfiltered (f,
17109 "unexpected attribute form: DW_FORM_indirect");
17110 break;
17111 default:
17112 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17113 die->attrs[i].form);
17114 break;
17115 }
17116 fprintf_unfiltered (f, "\n");
17117 }
17118 }
17119
17120 static void
17121 dump_die_for_error (struct die_info *die)
17122 {
17123 dump_die_shallow (gdb_stderr, 0, die);
17124 }
17125
17126 static void
17127 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17128 {
17129 int indent = level * 4;
17130
17131 gdb_assert (die != NULL);
17132
17133 if (level >= max_level)
17134 return;
17135
17136 dump_die_shallow (f, indent, die);
17137
17138 if (die->child != NULL)
17139 {
17140 print_spaces (indent, f);
17141 fprintf_unfiltered (f, " Children:");
17142 if (level + 1 < max_level)
17143 {
17144 fprintf_unfiltered (f, "\n");
17145 dump_die_1 (f, level + 1, max_level, die->child);
17146 }
17147 else
17148 {
17149 fprintf_unfiltered (f,
17150 " [not printed, max nesting level reached]\n");
17151 }
17152 }
17153
17154 if (die->sibling != NULL && level > 0)
17155 {
17156 dump_die_1 (f, level, max_level, die->sibling);
17157 }
17158 }
17159
17160 /* This is called from the pdie macro in gdbinit.in.
17161 It's not static so gcc will keep a copy callable from gdb. */
17162
17163 void
17164 dump_die (struct die_info *die, int max_level)
17165 {
17166 dump_die_1 (gdb_stdlog, 0, max_level, die);
17167 }
17168
17169 static void
17170 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17171 {
17172 void **slot;
17173
17174 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17175 INSERT);
17176
17177 *slot = die;
17178 }
17179
17180 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17181 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17182
17183 static int
17184 is_ref_attr (struct attribute *attr)
17185 {
17186 switch (attr->form)
17187 {
17188 case DW_FORM_ref_addr:
17189 case DW_FORM_ref1:
17190 case DW_FORM_ref2:
17191 case DW_FORM_ref4:
17192 case DW_FORM_ref8:
17193 case DW_FORM_ref_udata:
17194 case DW_FORM_GNU_ref_alt:
17195 return 1;
17196 default:
17197 return 0;
17198 }
17199 }
17200
17201 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17202 required kind. */
17203
17204 static sect_offset
17205 dwarf2_get_ref_die_offset (struct attribute *attr)
17206 {
17207 sect_offset retval = { DW_UNSND (attr) };
17208
17209 if (is_ref_attr (attr))
17210 return retval;
17211
17212 retval.sect_off = 0;
17213 complaint (&symfile_complaints,
17214 _("unsupported die ref attribute form: '%s'"),
17215 dwarf_form_name (attr->form));
17216 return retval;
17217 }
17218
17219 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17220 * the value held by the attribute is not constant. */
17221
17222 static LONGEST
17223 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17224 {
17225 if (attr->form == DW_FORM_sdata)
17226 return DW_SND (attr);
17227 else if (attr->form == DW_FORM_udata
17228 || attr->form == DW_FORM_data1
17229 || attr->form == DW_FORM_data2
17230 || attr->form == DW_FORM_data4
17231 || attr->form == DW_FORM_data8)
17232 return DW_UNSND (attr);
17233 else
17234 {
17235 complaint (&symfile_complaints,
17236 _("Attribute value is not a constant (%s)"),
17237 dwarf_form_name (attr->form));
17238 return default_value;
17239 }
17240 }
17241
17242 /* Follow reference or signature attribute ATTR of SRC_DIE.
17243 On entry *REF_CU is the CU of SRC_DIE.
17244 On exit *REF_CU is the CU of the result. */
17245
17246 static struct die_info *
17247 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17248 struct dwarf2_cu **ref_cu)
17249 {
17250 struct die_info *die;
17251
17252 if (is_ref_attr (attr))
17253 die = follow_die_ref (src_die, attr, ref_cu);
17254 else if (attr->form == DW_FORM_ref_sig8)
17255 die = follow_die_sig (src_die, attr, ref_cu);
17256 else
17257 {
17258 dump_die_for_error (src_die);
17259 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17260 (*ref_cu)->objfile->name);
17261 }
17262
17263 return die;
17264 }
17265
17266 /* Follow reference OFFSET.
17267 On entry *REF_CU is the CU of the source die referencing OFFSET.
17268 On exit *REF_CU is the CU of the result.
17269 Returns NULL if OFFSET is invalid. */
17270
17271 static struct die_info *
17272 follow_die_offset (sect_offset offset, int offset_in_dwz,
17273 struct dwarf2_cu **ref_cu)
17274 {
17275 struct die_info temp_die;
17276 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17277
17278 gdb_assert (cu->per_cu != NULL);
17279
17280 target_cu = cu;
17281
17282 if (cu->per_cu->is_debug_types)
17283 {
17284 /* .debug_types CUs cannot reference anything outside their CU.
17285 If they need to, they have to reference a signatured type via
17286 DW_FORM_ref_sig8. */
17287 if (! offset_in_cu_p (&cu->header, offset))
17288 return NULL;
17289 }
17290 else if (offset_in_dwz != cu->per_cu->is_dwz
17291 || ! offset_in_cu_p (&cu->header, offset))
17292 {
17293 struct dwarf2_per_cu_data *per_cu;
17294
17295 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17296 cu->objfile);
17297
17298 /* If necessary, add it to the queue and load its DIEs. */
17299 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17300 load_full_comp_unit (per_cu, cu->language);
17301
17302 target_cu = per_cu->cu;
17303 }
17304 else if (cu->dies == NULL)
17305 {
17306 /* We're loading full DIEs during partial symbol reading. */
17307 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17308 load_full_comp_unit (cu->per_cu, language_minimal);
17309 }
17310
17311 *ref_cu = target_cu;
17312 temp_die.offset = offset;
17313 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17314 }
17315
17316 /* Follow reference attribute ATTR of SRC_DIE.
17317 On entry *REF_CU is the CU of SRC_DIE.
17318 On exit *REF_CU is the CU of the result. */
17319
17320 static struct die_info *
17321 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17322 struct dwarf2_cu **ref_cu)
17323 {
17324 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17325 struct dwarf2_cu *cu = *ref_cu;
17326 struct die_info *die;
17327
17328 die = follow_die_offset (offset,
17329 (attr->form == DW_FORM_GNU_ref_alt
17330 || cu->per_cu->is_dwz),
17331 ref_cu);
17332 if (!die)
17333 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17334 "at 0x%x [in module %s]"),
17335 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17336
17337 return die;
17338 }
17339
17340 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17341 Returned value is intended for DW_OP_call*. Returned
17342 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17343
17344 struct dwarf2_locexpr_baton
17345 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
17346 struct dwarf2_per_cu_data *per_cu,
17347 CORE_ADDR (*get_frame_pc) (void *baton),
17348 void *baton)
17349 {
17350 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17351 struct dwarf2_cu *cu;
17352 struct die_info *die;
17353 struct attribute *attr;
17354 struct dwarf2_locexpr_baton retval;
17355
17356 dw2_setup (per_cu->objfile);
17357
17358 if (per_cu->cu == NULL)
17359 load_cu (per_cu);
17360 cu = per_cu->cu;
17361
17362 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17363 if (!die)
17364 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17365 offset.sect_off, per_cu->objfile->name);
17366
17367 attr = dwarf2_attr (die, DW_AT_location, cu);
17368 if (!attr)
17369 {
17370 /* DWARF: "If there is no such attribute, then there is no effect.".
17371 DATA is ignored if SIZE is 0. */
17372
17373 retval.data = NULL;
17374 retval.size = 0;
17375 }
17376 else if (attr_form_is_section_offset (attr))
17377 {
17378 struct dwarf2_loclist_baton loclist_baton;
17379 CORE_ADDR pc = (*get_frame_pc) (baton);
17380 size_t size;
17381
17382 fill_in_loclist_baton (cu, &loclist_baton, attr);
17383
17384 retval.data = dwarf2_find_location_expression (&loclist_baton,
17385 &size, pc);
17386 retval.size = size;
17387 }
17388 else
17389 {
17390 if (!attr_form_is_block (attr))
17391 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17392 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17393 offset.sect_off, per_cu->objfile->name);
17394
17395 retval.data = DW_BLOCK (attr)->data;
17396 retval.size = DW_BLOCK (attr)->size;
17397 }
17398 retval.per_cu = cu->per_cu;
17399
17400 age_cached_comp_units ();
17401
17402 return retval;
17403 }
17404
17405 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17406 PER_CU. */
17407
17408 struct type *
17409 dwarf2_get_die_type (cu_offset die_offset,
17410 struct dwarf2_per_cu_data *per_cu)
17411 {
17412 sect_offset die_offset_sect;
17413
17414 dw2_setup (per_cu->objfile);
17415
17416 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17417 return get_die_type_at_offset (die_offset_sect, per_cu);
17418 }
17419
17420 /* Follow the signature attribute ATTR in SRC_DIE.
17421 On entry *REF_CU is the CU of SRC_DIE.
17422 On exit *REF_CU is the CU of the result. */
17423
17424 static struct die_info *
17425 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17426 struct dwarf2_cu **ref_cu)
17427 {
17428 struct objfile *objfile = (*ref_cu)->objfile;
17429 struct die_info temp_die;
17430 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17431 struct dwarf2_cu *sig_cu;
17432 struct die_info *die;
17433
17434 /* sig_type will be NULL if the signatured type is missing from
17435 the debug info. */
17436 if (sig_type == NULL)
17437 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17438 "at 0x%x [in module %s]"),
17439 src_die->offset.sect_off, objfile->name);
17440
17441 /* If necessary, add it to the queue and load its DIEs. */
17442
17443 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17444 read_signatured_type (sig_type);
17445
17446 gdb_assert (sig_type->per_cu.cu != NULL);
17447
17448 sig_cu = sig_type->per_cu.cu;
17449 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17450 temp_die.offset = sig_type->type_offset_in_section;
17451 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17452 temp_die.offset.sect_off);
17453 if (die)
17454 {
17455 *ref_cu = sig_cu;
17456 return die;
17457 }
17458
17459 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17460 "from DIE at 0x%x [in module %s]"),
17461 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17462 }
17463
17464 /* Given an offset of a signatured type, return its signatured_type. */
17465
17466 static struct signatured_type *
17467 lookup_signatured_type_at_offset (struct objfile *objfile,
17468 struct dwarf2_section_info *section,
17469 sect_offset offset)
17470 {
17471 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17472 unsigned int length, initial_length_size;
17473 unsigned int sig_offset;
17474 struct signatured_type find_entry, *sig_type;
17475
17476 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17477 sig_offset = (initial_length_size
17478 + 2 /*version*/
17479 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17480 + 1 /*address_size*/);
17481 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17482 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17483
17484 /* This is only used to lookup previously recorded types.
17485 If we didn't find it, it's our bug. */
17486 gdb_assert (sig_type != NULL);
17487 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17488
17489 return sig_type;
17490 }
17491
17492 /* Load the DIEs associated with type unit PER_CU into memory. */
17493
17494 static void
17495 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17496 {
17497 struct signatured_type *sig_type;
17498
17499 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17500 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17501
17502 /* We have the per_cu, but we need the signatured_type.
17503 Fortunately this is an easy translation. */
17504 gdb_assert (per_cu->is_debug_types);
17505 sig_type = (struct signatured_type *) per_cu;
17506
17507 gdb_assert (per_cu->cu == NULL);
17508
17509 read_signatured_type (sig_type);
17510
17511 gdb_assert (per_cu->cu != NULL);
17512 }
17513
17514 /* die_reader_func for read_signatured_type.
17515 This is identical to load_full_comp_unit_reader,
17516 but is kept separate for now. */
17517
17518 static void
17519 read_signatured_type_reader (const struct die_reader_specs *reader,
17520 gdb_byte *info_ptr,
17521 struct die_info *comp_unit_die,
17522 int has_children,
17523 void *data)
17524 {
17525 struct dwarf2_cu *cu = reader->cu;
17526
17527 gdb_assert (cu->die_hash == NULL);
17528 cu->die_hash =
17529 htab_create_alloc_ex (cu->header.length / 12,
17530 die_hash,
17531 die_eq,
17532 NULL,
17533 &cu->comp_unit_obstack,
17534 hashtab_obstack_allocate,
17535 dummy_obstack_deallocate);
17536
17537 if (has_children)
17538 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17539 &info_ptr, comp_unit_die);
17540 cu->dies = comp_unit_die;
17541 /* comp_unit_die is not stored in die_hash, no need. */
17542
17543 /* We try not to read any attributes in this function, because not
17544 all CUs needed for references have been loaded yet, and symbol
17545 table processing isn't initialized. But we have to set the CU language,
17546 or we won't be able to build types correctly.
17547 Similarly, if we do not read the producer, we can not apply
17548 producer-specific interpretation. */
17549 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17550 }
17551
17552 /* Read in a signatured type and build its CU and DIEs.
17553 If the type is a stub for the real type in a DWO file,
17554 read in the real type from the DWO file as well. */
17555
17556 static void
17557 read_signatured_type (struct signatured_type *sig_type)
17558 {
17559 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17560
17561 gdb_assert (per_cu->is_debug_types);
17562 gdb_assert (per_cu->cu == NULL);
17563
17564 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17565 read_signatured_type_reader, NULL);
17566 }
17567
17568 /* Decode simple location descriptions.
17569 Given a pointer to a dwarf block that defines a location, compute
17570 the location and return the value.
17571
17572 NOTE drow/2003-11-18: This function is called in two situations
17573 now: for the address of static or global variables (partial symbols
17574 only) and for offsets into structures which are expected to be
17575 (more or less) constant. The partial symbol case should go away,
17576 and only the constant case should remain. That will let this
17577 function complain more accurately. A few special modes are allowed
17578 without complaint for global variables (for instance, global
17579 register values and thread-local values).
17580
17581 A location description containing no operations indicates that the
17582 object is optimized out. The return value is 0 for that case.
17583 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17584 callers will only want a very basic result and this can become a
17585 complaint.
17586
17587 Note that stack[0] is unused except as a default error return. */
17588
17589 static CORE_ADDR
17590 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17591 {
17592 struct objfile *objfile = cu->objfile;
17593 size_t i;
17594 size_t size = blk->size;
17595 gdb_byte *data = blk->data;
17596 CORE_ADDR stack[64];
17597 int stacki;
17598 unsigned int bytes_read, unsnd;
17599 gdb_byte op;
17600
17601 i = 0;
17602 stacki = 0;
17603 stack[stacki] = 0;
17604 stack[++stacki] = 0;
17605
17606 while (i < size)
17607 {
17608 op = data[i++];
17609 switch (op)
17610 {
17611 case DW_OP_lit0:
17612 case DW_OP_lit1:
17613 case DW_OP_lit2:
17614 case DW_OP_lit3:
17615 case DW_OP_lit4:
17616 case DW_OP_lit5:
17617 case DW_OP_lit6:
17618 case DW_OP_lit7:
17619 case DW_OP_lit8:
17620 case DW_OP_lit9:
17621 case DW_OP_lit10:
17622 case DW_OP_lit11:
17623 case DW_OP_lit12:
17624 case DW_OP_lit13:
17625 case DW_OP_lit14:
17626 case DW_OP_lit15:
17627 case DW_OP_lit16:
17628 case DW_OP_lit17:
17629 case DW_OP_lit18:
17630 case DW_OP_lit19:
17631 case DW_OP_lit20:
17632 case DW_OP_lit21:
17633 case DW_OP_lit22:
17634 case DW_OP_lit23:
17635 case DW_OP_lit24:
17636 case DW_OP_lit25:
17637 case DW_OP_lit26:
17638 case DW_OP_lit27:
17639 case DW_OP_lit28:
17640 case DW_OP_lit29:
17641 case DW_OP_lit30:
17642 case DW_OP_lit31:
17643 stack[++stacki] = op - DW_OP_lit0;
17644 break;
17645
17646 case DW_OP_reg0:
17647 case DW_OP_reg1:
17648 case DW_OP_reg2:
17649 case DW_OP_reg3:
17650 case DW_OP_reg4:
17651 case DW_OP_reg5:
17652 case DW_OP_reg6:
17653 case DW_OP_reg7:
17654 case DW_OP_reg8:
17655 case DW_OP_reg9:
17656 case DW_OP_reg10:
17657 case DW_OP_reg11:
17658 case DW_OP_reg12:
17659 case DW_OP_reg13:
17660 case DW_OP_reg14:
17661 case DW_OP_reg15:
17662 case DW_OP_reg16:
17663 case DW_OP_reg17:
17664 case DW_OP_reg18:
17665 case DW_OP_reg19:
17666 case DW_OP_reg20:
17667 case DW_OP_reg21:
17668 case DW_OP_reg22:
17669 case DW_OP_reg23:
17670 case DW_OP_reg24:
17671 case DW_OP_reg25:
17672 case DW_OP_reg26:
17673 case DW_OP_reg27:
17674 case DW_OP_reg28:
17675 case DW_OP_reg29:
17676 case DW_OP_reg30:
17677 case DW_OP_reg31:
17678 stack[++stacki] = op - DW_OP_reg0;
17679 if (i < size)
17680 dwarf2_complex_location_expr_complaint ();
17681 break;
17682
17683 case DW_OP_regx:
17684 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17685 i += bytes_read;
17686 stack[++stacki] = unsnd;
17687 if (i < size)
17688 dwarf2_complex_location_expr_complaint ();
17689 break;
17690
17691 case DW_OP_addr:
17692 stack[++stacki] = read_address (objfile->obfd, &data[i],
17693 cu, &bytes_read);
17694 i += bytes_read;
17695 break;
17696
17697 case DW_OP_const1u:
17698 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17699 i += 1;
17700 break;
17701
17702 case DW_OP_const1s:
17703 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17704 i += 1;
17705 break;
17706
17707 case DW_OP_const2u:
17708 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17709 i += 2;
17710 break;
17711
17712 case DW_OP_const2s:
17713 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17714 i += 2;
17715 break;
17716
17717 case DW_OP_const4u:
17718 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17719 i += 4;
17720 break;
17721
17722 case DW_OP_const4s:
17723 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17724 i += 4;
17725 break;
17726
17727 case DW_OP_const8u:
17728 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17729 i += 8;
17730 break;
17731
17732 case DW_OP_constu:
17733 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17734 &bytes_read);
17735 i += bytes_read;
17736 break;
17737
17738 case DW_OP_consts:
17739 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17740 i += bytes_read;
17741 break;
17742
17743 case DW_OP_dup:
17744 stack[stacki + 1] = stack[stacki];
17745 stacki++;
17746 break;
17747
17748 case DW_OP_plus:
17749 stack[stacki - 1] += stack[stacki];
17750 stacki--;
17751 break;
17752
17753 case DW_OP_plus_uconst:
17754 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17755 &bytes_read);
17756 i += bytes_read;
17757 break;
17758
17759 case DW_OP_minus:
17760 stack[stacki - 1] -= stack[stacki];
17761 stacki--;
17762 break;
17763
17764 case DW_OP_deref:
17765 /* If we're not the last op, then we definitely can't encode
17766 this using GDB's address_class enum. This is valid for partial
17767 global symbols, although the variable's address will be bogus
17768 in the psymtab. */
17769 if (i < size)
17770 dwarf2_complex_location_expr_complaint ();
17771 break;
17772
17773 case DW_OP_GNU_push_tls_address:
17774 /* The top of the stack has the offset from the beginning
17775 of the thread control block at which the variable is located. */
17776 /* Nothing should follow this operator, so the top of stack would
17777 be returned. */
17778 /* This is valid for partial global symbols, but the variable's
17779 address will be bogus in the psymtab. Make it always at least
17780 non-zero to not look as a variable garbage collected by linker
17781 which have DW_OP_addr 0. */
17782 if (i < size)
17783 dwarf2_complex_location_expr_complaint ();
17784 stack[stacki]++;
17785 break;
17786
17787 case DW_OP_GNU_uninit:
17788 break;
17789
17790 case DW_OP_GNU_addr_index:
17791 case DW_OP_GNU_const_index:
17792 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17793 &bytes_read);
17794 i += bytes_read;
17795 break;
17796
17797 default:
17798 {
17799 const char *name = get_DW_OP_name (op);
17800
17801 if (name)
17802 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17803 name);
17804 else
17805 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17806 op);
17807 }
17808
17809 return (stack[stacki]);
17810 }
17811
17812 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17813 outside of the allocated space. Also enforce minimum>0. */
17814 if (stacki >= ARRAY_SIZE (stack) - 1)
17815 {
17816 complaint (&symfile_complaints,
17817 _("location description stack overflow"));
17818 return 0;
17819 }
17820
17821 if (stacki <= 0)
17822 {
17823 complaint (&symfile_complaints,
17824 _("location description stack underflow"));
17825 return 0;
17826 }
17827 }
17828 return (stack[stacki]);
17829 }
17830
17831 /* memory allocation interface */
17832
17833 static struct dwarf_block *
17834 dwarf_alloc_block (struct dwarf2_cu *cu)
17835 {
17836 struct dwarf_block *blk;
17837
17838 blk = (struct dwarf_block *)
17839 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17840 return (blk);
17841 }
17842
17843 static struct die_info *
17844 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17845 {
17846 struct die_info *die;
17847 size_t size = sizeof (struct die_info);
17848
17849 if (num_attrs > 1)
17850 size += (num_attrs - 1) * sizeof (struct attribute);
17851
17852 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17853 memset (die, 0, sizeof (struct die_info));
17854 return (die);
17855 }
17856
17857 \f
17858 /* Macro support. */
17859
17860 /* Return the full name of file number I in *LH's file name table.
17861 Use COMP_DIR as the name of the current directory of the
17862 compilation. The result is allocated using xmalloc; the caller is
17863 responsible for freeing it. */
17864 static char *
17865 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17866 {
17867 /* Is the file number a valid index into the line header's file name
17868 table? Remember that file numbers start with one, not zero. */
17869 if (1 <= file && file <= lh->num_file_names)
17870 {
17871 struct file_entry *fe = &lh->file_names[file - 1];
17872
17873 if (IS_ABSOLUTE_PATH (fe->name))
17874 return xstrdup (fe->name);
17875 else
17876 {
17877 const char *dir;
17878 int dir_len;
17879 char *full_name;
17880
17881 if (fe->dir_index)
17882 dir = lh->include_dirs[fe->dir_index - 1];
17883 else
17884 dir = comp_dir;
17885
17886 if (dir)
17887 {
17888 dir_len = strlen (dir);
17889 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17890 strcpy (full_name, dir);
17891 full_name[dir_len] = '/';
17892 strcpy (full_name + dir_len + 1, fe->name);
17893 return full_name;
17894 }
17895 else
17896 return xstrdup (fe->name);
17897 }
17898 }
17899 else
17900 {
17901 /* The compiler produced a bogus file number. We can at least
17902 record the macro definitions made in the file, even if we
17903 won't be able to find the file by name. */
17904 char fake_name[80];
17905
17906 xsnprintf (fake_name, sizeof (fake_name),
17907 "<bad macro file number %d>", file);
17908
17909 complaint (&symfile_complaints,
17910 _("bad file number in macro information (%d)"),
17911 file);
17912
17913 return xstrdup (fake_name);
17914 }
17915 }
17916
17917
17918 static struct macro_source_file *
17919 macro_start_file (int file, int line,
17920 struct macro_source_file *current_file,
17921 const char *comp_dir,
17922 struct line_header *lh, struct objfile *objfile)
17923 {
17924 /* The full name of this source file. */
17925 char *full_name = file_full_name (file, lh, comp_dir);
17926
17927 /* We don't create a macro table for this compilation unit
17928 at all until we actually get a filename. */
17929 if (! pending_macros)
17930 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17931 objfile->per_bfd->macro_cache);
17932
17933 if (! current_file)
17934 {
17935 /* If we have no current file, then this must be the start_file
17936 directive for the compilation unit's main source file. */
17937 current_file = macro_set_main (pending_macros, full_name);
17938 macro_define_special (pending_macros);
17939 }
17940 else
17941 current_file = macro_include (current_file, line, full_name);
17942
17943 xfree (full_name);
17944
17945 return current_file;
17946 }
17947
17948
17949 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17950 followed by a null byte. */
17951 static char *
17952 copy_string (const char *buf, int len)
17953 {
17954 char *s = xmalloc (len + 1);
17955
17956 memcpy (s, buf, len);
17957 s[len] = '\0';
17958 return s;
17959 }
17960
17961
17962 static const char *
17963 consume_improper_spaces (const char *p, const char *body)
17964 {
17965 if (*p == ' ')
17966 {
17967 complaint (&symfile_complaints,
17968 _("macro definition contains spaces "
17969 "in formal argument list:\n`%s'"),
17970 body);
17971
17972 while (*p == ' ')
17973 p++;
17974 }
17975
17976 return p;
17977 }
17978
17979
17980 static void
17981 parse_macro_definition (struct macro_source_file *file, int line,
17982 const char *body)
17983 {
17984 const char *p;
17985
17986 /* The body string takes one of two forms. For object-like macro
17987 definitions, it should be:
17988
17989 <macro name> " " <definition>
17990
17991 For function-like macro definitions, it should be:
17992
17993 <macro name> "() " <definition>
17994 or
17995 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17996
17997 Spaces may appear only where explicitly indicated, and in the
17998 <definition>.
17999
18000 The Dwarf 2 spec says that an object-like macro's name is always
18001 followed by a space, but versions of GCC around March 2002 omit
18002 the space when the macro's definition is the empty string.
18003
18004 The Dwarf 2 spec says that there should be no spaces between the
18005 formal arguments in a function-like macro's formal argument list,
18006 but versions of GCC around March 2002 include spaces after the
18007 commas. */
18008
18009
18010 /* Find the extent of the macro name. The macro name is terminated
18011 by either a space or null character (for an object-like macro) or
18012 an opening paren (for a function-like macro). */
18013 for (p = body; *p; p++)
18014 if (*p == ' ' || *p == '(')
18015 break;
18016
18017 if (*p == ' ' || *p == '\0')
18018 {
18019 /* It's an object-like macro. */
18020 int name_len = p - body;
18021 char *name = copy_string (body, name_len);
18022 const char *replacement;
18023
18024 if (*p == ' ')
18025 replacement = body + name_len + 1;
18026 else
18027 {
18028 dwarf2_macro_malformed_definition_complaint (body);
18029 replacement = body + name_len;
18030 }
18031
18032 macro_define_object (file, line, name, replacement);
18033
18034 xfree (name);
18035 }
18036 else if (*p == '(')
18037 {
18038 /* It's a function-like macro. */
18039 char *name = copy_string (body, p - body);
18040 int argc = 0;
18041 int argv_size = 1;
18042 char **argv = xmalloc (argv_size * sizeof (*argv));
18043
18044 p++;
18045
18046 p = consume_improper_spaces (p, body);
18047
18048 /* Parse the formal argument list. */
18049 while (*p && *p != ')')
18050 {
18051 /* Find the extent of the current argument name. */
18052 const char *arg_start = p;
18053
18054 while (*p && *p != ',' && *p != ')' && *p != ' ')
18055 p++;
18056
18057 if (! *p || p == arg_start)
18058 dwarf2_macro_malformed_definition_complaint (body);
18059 else
18060 {
18061 /* Make sure argv has room for the new argument. */
18062 if (argc >= argv_size)
18063 {
18064 argv_size *= 2;
18065 argv = xrealloc (argv, argv_size * sizeof (*argv));
18066 }
18067
18068 argv[argc++] = copy_string (arg_start, p - arg_start);
18069 }
18070
18071 p = consume_improper_spaces (p, body);
18072
18073 /* Consume the comma, if present. */
18074 if (*p == ',')
18075 {
18076 p++;
18077
18078 p = consume_improper_spaces (p, body);
18079 }
18080 }
18081
18082 if (*p == ')')
18083 {
18084 p++;
18085
18086 if (*p == ' ')
18087 /* Perfectly formed definition, no complaints. */
18088 macro_define_function (file, line, name,
18089 argc, (const char **) argv,
18090 p + 1);
18091 else if (*p == '\0')
18092 {
18093 /* Complain, but do define it. */
18094 dwarf2_macro_malformed_definition_complaint (body);
18095 macro_define_function (file, line, name,
18096 argc, (const char **) argv,
18097 p);
18098 }
18099 else
18100 /* Just complain. */
18101 dwarf2_macro_malformed_definition_complaint (body);
18102 }
18103 else
18104 /* Just complain. */
18105 dwarf2_macro_malformed_definition_complaint (body);
18106
18107 xfree (name);
18108 {
18109 int i;
18110
18111 for (i = 0; i < argc; i++)
18112 xfree (argv[i]);
18113 }
18114 xfree (argv);
18115 }
18116 else
18117 dwarf2_macro_malformed_definition_complaint (body);
18118 }
18119
18120 /* Skip some bytes from BYTES according to the form given in FORM.
18121 Returns the new pointer. */
18122
18123 static gdb_byte *
18124 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18125 enum dwarf_form form,
18126 unsigned int offset_size,
18127 struct dwarf2_section_info *section)
18128 {
18129 unsigned int bytes_read;
18130
18131 switch (form)
18132 {
18133 case DW_FORM_data1:
18134 case DW_FORM_flag:
18135 ++bytes;
18136 break;
18137
18138 case DW_FORM_data2:
18139 bytes += 2;
18140 break;
18141
18142 case DW_FORM_data4:
18143 bytes += 4;
18144 break;
18145
18146 case DW_FORM_data8:
18147 bytes += 8;
18148 break;
18149
18150 case DW_FORM_string:
18151 read_direct_string (abfd, bytes, &bytes_read);
18152 bytes += bytes_read;
18153 break;
18154
18155 case DW_FORM_sec_offset:
18156 case DW_FORM_strp:
18157 case DW_FORM_GNU_strp_alt:
18158 bytes += offset_size;
18159 break;
18160
18161 case DW_FORM_block:
18162 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18163 bytes += bytes_read;
18164 break;
18165
18166 case DW_FORM_block1:
18167 bytes += 1 + read_1_byte (abfd, bytes);
18168 break;
18169 case DW_FORM_block2:
18170 bytes += 2 + read_2_bytes (abfd, bytes);
18171 break;
18172 case DW_FORM_block4:
18173 bytes += 4 + read_4_bytes (abfd, bytes);
18174 break;
18175
18176 case DW_FORM_sdata:
18177 case DW_FORM_udata:
18178 case DW_FORM_GNU_addr_index:
18179 case DW_FORM_GNU_str_index:
18180 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18181 if (bytes == NULL)
18182 {
18183 dwarf2_section_buffer_overflow_complaint (section);
18184 return NULL;
18185 }
18186 break;
18187
18188 default:
18189 {
18190 complain:
18191 complaint (&symfile_complaints,
18192 _("invalid form 0x%x in `%s'"),
18193 form,
18194 section->asection->name);
18195 return NULL;
18196 }
18197 }
18198
18199 return bytes;
18200 }
18201
18202 /* A helper for dwarf_decode_macros that handles skipping an unknown
18203 opcode. Returns an updated pointer to the macro data buffer; or,
18204 on error, issues a complaint and returns NULL. */
18205
18206 static gdb_byte *
18207 skip_unknown_opcode (unsigned int opcode,
18208 gdb_byte **opcode_definitions,
18209 gdb_byte *mac_ptr, gdb_byte *mac_end,
18210 bfd *abfd,
18211 unsigned int offset_size,
18212 struct dwarf2_section_info *section)
18213 {
18214 unsigned int bytes_read, i;
18215 unsigned long arg;
18216 gdb_byte *defn;
18217
18218 if (opcode_definitions[opcode] == NULL)
18219 {
18220 complaint (&symfile_complaints,
18221 _("unrecognized DW_MACFINO opcode 0x%x"),
18222 opcode);
18223 return NULL;
18224 }
18225
18226 defn = opcode_definitions[opcode];
18227 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18228 defn += bytes_read;
18229
18230 for (i = 0; i < arg; ++i)
18231 {
18232 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18233 section);
18234 if (mac_ptr == NULL)
18235 {
18236 /* skip_form_bytes already issued the complaint. */
18237 return NULL;
18238 }
18239 }
18240
18241 return mac_ptr;
18242 }
18243
18244 /* A helper function which parses the header of a macro section.
18245 If the macro section is the extended (for now called "GNU") type,
18246 then this updates *OFFSET_SIZE. Returns a pointer to just after
18247 the header, or issues a complaint and returns NULL on error. */
18248
18249 static gdb_byte *
18250 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18251 bfd *abfd,
18252 gdb_byte *mac_ptr,
18253 unsigned int *offset_size,
18254 int section_is_gnu)
18255 {
18256 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18257
18258 if (section_is_gnu)
18259 {
18260 unsigned int version, flags;
18261
18262 version = read_2_bytes (abfd, mac_ptr);
18263 if (version != 4)
18264 {
18265 complaint (&symfile_complaints,
18266 _("unrecognized version `%d' in .debug_macro section"),
18267 version);
18268 return NULL;
18269 }
18270 mac_ptr += 2;
18271
18272 flags = read_1_byte (abfd, mac_ptr);
18273 ++mac_ptr;
18274 *offset_size = (flags & 1) ? 8 : 4;
18275
18276 if ((flags & 2) != 0)
18277 /* We don't need the line table offset. */
18278 mac_ptr += *offset_size;
18279
18280 /* Vendor opcode descriptions. */
18281 if ((flags & 4) != 0)
18282 {
18283 unsigned int i, count;
18284
18285 count = read_1_byte (abfd, mac_ptr);
18286 ++mac_ptr;
18287 for (i = 0; i < count; ++i)
18288 {
18289 unsigned int opcode, bytes_read;
18290 unsigned long arg;
18291
18292 opcode = read_1_byte (abfd, mac_ptr);
18293 ++mac_ptr;
18294 opcode_definitions[opcode] = mac_ptr;
18295 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18296 mac_ptr += bytes_read;
18297 mac_ptr += arg;
18298 }
18299 }
18300 }
18301
18302 return mac_ptr;
18303 }
18304
18305 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18306 including DW_MACRO_GNU_transparent_include. */
18307
18308 static void
18309 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18310 struct macro_source_file *current_file,
18311 struct line_header *lh, char *comp_dir,
18312 struct dwarf2_section_info *section,
18313 int section_is_gnu, int section_is_dwz,
18314 unsigned int offset_size,
18315 struct objfile *objfile,
18316 htab_t include_hash)
18317 {
18318 enum dwarf_macro_record_type macinfo_type;
18319 int at_commandline;
18320 gdb_byte *opcode_definitions[256];
18321
18322 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18323 &offset_size, section_is_gnu);
18324 if (mac_ptr == NULL)
18325 {
18326 /* We already issued a complaint. */
18327 return;
18328 }
18329
18330 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18331 GDB is still reading the definitions from command line. First
18332 DW_MACINFO_start_file will need to be ignored as it was already executed
18333 to create CURRENT_FILE for the main source holding also the command line
18334 definitions. On first met DW_MACINFO_start_file this flag is reset to
18335 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18336
18337 at_commandline = 1;
18338
18339 do
18340 {
18341 /* Do we at least have room for a macinfo type byte? */
18342 if (mac_ptr >= mac_end)
18343 {
18344 dwarf2_section_buffer_overflow_complaint (section);
18345 break;
18346 }
18347
18348 macinfo_type = read_1_byte (abfd, mac_ptr);
18349 mac_ptr++;
18350
18351 /* Note that we rely on the fact that the corresponding GNU and
18352 DWARF constants are the same. */
18353 switch (macinfo_type)
18354 {
18355 /* A zero macinfo type indicates the end of the macro
18356 information. */
18357 case 0:
18358 break;
18359
18360 case DW_MACRO_GNU_define:
18361 case DW_MACRO_GNU_undef:
18362 case DW_MACRO_GNU_define_indirect:
18363 case DW_MACRO_GNU_undef_indirect:
18364 case DW_MACRO_GNU_define_indirect_alt:
18365 case DW_MACRO_GNU_undef_indirect_alt:
18366 {
18367 unsigned int bytes_read;
18368 int line;
18369 char *body;
18370 int is_define;
18371
18372 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18373 mac_ptr += bytes_read;
18374
18375 if (macinfo_type == DW_MACRO_GNU_define
18376 || macinfo_type == DW_MACRO_GNU_undef)
18377 {
18378 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18379 mac_ptr += bytes_read;
18380 }
18381 else
18382 {
18383 LONGEST str_offset;
18384
18385 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18386 mac_ptr += offset_size;
18387
18388 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18389 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18390 || section_is_dwz)
18391 {
18392 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18393
18394 body = read_indirect_string_from_dwz (dwz, str_offset);
18395 }
18396 else
18397 body = read_indirect_string_at_offset (abfd, str_offset);
18398 }
18399
18400 is_define = (macinfo_type == DW_MACRO_GNU_define
18401 || macinfo_type == DW_MACRO_GNU_define_indirect
18402 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18403 if (! current_file)
18404 {
18405 /* DWARF violation as no main source is present. */
18406 complaint (&symfile_complaints,
18407 _("debug info with no main source gives macro %s "
18408 "on line %d: %s"),
18409 is_define ? _("definition") : _("undefinition"),
18410 line, body);
18411 break;
18412 }
18413 if ((line == 0 && !at_commandline)
18414 || (line != 0 && at_commandline))
18415 complaint (&symfile_complaints,
18416 _("debug info gives %s macro %s with %s line %d: %s"),
18417 at_commandline ? _("command-line") : _("in-file"),
18418 is_define ? _("definition") : _("undefinition"),
18419 line == 0 ? _("zero") : _("non-zero"), line, body);
18420
18421 if (is_define)
18422 parse_macro_definition (current_file, line, body);
18423 else
18424 {
18425 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18426 || macinfo_type == DW_MACRO_GNU_undef_indirect
18427 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18428 macro_undef (current_file, line, body);
18429 }
18430 }
18431 break;
18432
18433 case DW_MACRO_GNU_start_file:
18434 {
18435 unsigned int bytes_read;
18436 int line, file;
18437
18438 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18439 mac_ptr += bytes_read;
18440 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18441 mac_ptr += bytes_read;
18442
18443 if ((line == 0 && !at_commandline)
18444 || (line != 0 && at_commandline))
18445 complaint (&symfile_complaints,
18446 _("debug info gives source %d included "
18447 "from %s at %s line %d"),
18448 file, at_commandline ? _("command-line") : _("file"),
18449 line == 0 ? _("zero") : _("non-zero"), line);
18450
18451 if (at_commandline)
18452 {
18453 /* This DW_MACRO_GNU_start_file was executed in the
18454 pass one. */
18455 at_commandline = 0;
18456 }
18457 else
18458 current_file = macro_start_file (file, line,
18459 current_file, comp_dir,
18460 lh, objfile);
18461 }
18462 break;
18463
18464 case DW_MACRO_GNU_end_file:
18465 if (! current_file)
18466 complaint (&symfile_complaints,
18467 _("macro debug info has an unmatched "
18468 "`close_file' directive"));
18469 else
18470 {
18471 current_file = current_file->included_by;
18472 if (! current_file)
18473 {
18474 enum dwarf_macro_record_type next_type;
18475
18476 /* GCC circa March 2002 doesn't produce the zero
18477 type byte marking the end of the compilation
18478 unit. Complain if it's not there, but exit no
18479 matter what. */
18480
18481 /* Do we at least have room for a macinfo type byte? */
18482 if (mac_ptr >= mac_end)
18483 {
18484 dwarf2_section_buffer_overflow_complaint (section);
18485 return;
18486 }
18487
18488 /* We don't increment mac_ptr here, so this is just
18489 a look-ahead. */
18490 next_type = read_1_byte (abfd, mac_ptr);
18491 if (next_type != 0)
18492 complaint (&symfile_complaints,
18493 _("no terminating 0-type entry for "
18494 "macros in `.debug_macinfo' section"));
18495
18496 return;
18497 }
18498 }
18499 break;
18500
18501 case DW_MACRO_GNU_transparent_include:
18502 case DW_MACRO_GNU_transparent_include_alt:
18503 {
18504 LONGEST offset;
18505 void **slot;
18506 bfd *include_bfd = abfd;
18507 struct dwarf2_section_info *include_section = section;
18508 struct dwarf2_section_info alt_section;
18509 gdb_byte *include_mac_end = mac_end;
18510 int is_dwz = section_is_dwz;
18511 gdb_byte *new_mac_ptr;
18512
18513 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18514 mac_ptr += offset_size;
18515
18516 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18517 {
18518 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18519
18520 dwarf2_read_section (dwarf2_per_objfile->objfile,
18521 &dwz->macro);
18522
18523 include_bfd = dwz->macro.asection->owner;
18524 include_section = &dwz->macro;
18525 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18526 is_dwz = 1;
18527 }
18528
18529 new_mac_ptr = include_section->buffer + offset;
18530 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18531
18532 if (*slot != NULL)
18533 {
18534 /* This has actually happened; see
18535 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18536 complaint (&symfile_complaints,
18537 _("recursive DW_MACRO_GNU_transparent_include in "
18538 ".debug_macro section"));
18539 }
18540 else
18541 {
18542 *slot = new_mac_ptr;
18543
18544 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18545 include_mac_end, current_file,
18546 lh, comp_dir,
18547 section, section_is_gnu, is_dwz,
18548 offset_size, objfile, include_hash);
18549
18550 htab_remove_elt (include_hash, new_mac_ptr);
18551 }
18552 }
18553 break;
18554
18555 case DW_MACINFO_vendor_ext:
18556 if (!section_is_gnu)
18557 {
18558 unsigned int bytes_read;
18559 int constant;
18560
18561 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18562 mac_ptr += bytes_read;
18563 read_direct_string (abfd, mac_ptr, &bytes_read);
18564 mac_ptr += bytes_read;
18565
18566 /* We don't recognize any vendor extensions. */
18567 break;
18568 }
18569 /* FALLTHROUGH */
18570
18571 default:
18572 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18573 mac_ptr, mac_end, abfd, offset_size,
18574 section);
18575 if (mac_ptr == NULL)
18576 return;
18577 break;
18578 }
18579 } while (macinfo_type != 0);
18580 }
18581
18582 static void
18583 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18584 char *comp_dir, int section_is_gnu)
18585 {
18586 struct objfile *objfile = dwarf2_per_objfile->objfile;
18587 struct line_header *lh = cu->line_header;
18588 bfd *abfd;
18589 gdb_byte *mac_ptr, *mac_end;
18590 struct macro_source_file *current_file = 0;
18591 enum dwarf_macro_record_type macinfo_type;
18592 unsigned int offset_size = cu->header.offset_size;
18593 gdb_byte *opcode_definitions[256];
18594 struct cleanup *cleanup;
18595 htab_t include_hash;
18596 void **slot;
18597 struct dwarf2_section_info *section;
18598 const char *section_name;
18599
18600 if (cu->dwo_unit != NULL)
18601 {
18602 if (section_is_gnu)
18603 {
18604 section = &cu->dwo_unit->dwo_file->sections.macro;
18605 section_name = ".debug_macro.dwo";
18606 }
18607 else
18608 {
18609 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18610 section_name = ".debug_macinfo.dwo";
18611 }
18612 }
18613 else
18614 {
18615 if (section_is_gnu)
18616 {
18617 section = &dwarf2_per_objfile->macro;
18618 section_name = ".debug_macro";
18619 }
18620 else
18621 {
18622 section = &dwarf2_per_objfile->macinfo;
18623 section_name = ".debug_macinfo";
18624 }
18625 }
18626
18627 dwarf2_read_section (objfile, section);
18628 if (section->buffer == NULL)
18629 {
18630 complaint (&symfile_complaints, _("missing %s section"), section_name);
18631 return;
18632 }
18633 abfd = section->asection->owner;
18634
18635 /* First pass: Find the name of the base filename.
18636 This filename is needed in order to process all macros whose definition
18637 (or undefinition) comes from the command line. These macros are defined
18638 before the first DW_MACINFO_start_file entry, and yet still need to be
18639 associated to the base file.
18640
18641 To determine the base file name, we scan the macro definitions until we
18642 reach the first DW_MACINFO_start_file entry. We then initialize
18643 CURRENT_FILE accordingly so that any macro definition found before the
18644 first DW_MACINFO_start_file can still be associated to the base file. */
18645
18646 mac_ptr = section->buffer + offset;
18647 mac_end = section->buffer + section->size;
18648
18649 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18650 &offset_size, section_is_gnu);
18651 if (mac_ptr == NULL)
18652 {
18653 /* We already issued a complaint. */
18654 return;
18655 }
18656
18657 do
18658 {
18659 /* Do we at least have room for a macinfo type byte? */
18660 if (mac_ptr >= mac_end)
18661 {
18662 /* Complaint is printed during the second pass as GDB will probably
18663 stop the first pass earlier upon finding
18664 DW_MACINFO_start_file. */
18665 break;
18666 }
18667
18668 macinfo_type = read_1_byte (abfd, mac_ptr);
18669 mac_ptr++;
18670
18671 /* Note that we rely on the fact that the corresponding GNU and
18672 DWARF constants are the same. */
18673 switch (macinfo_type)
18674 {
18675 /* A zero macinfo type indicates the end of the macro
18676 information. */
18677 case 0:
18678 break;
18679
18680 case DW_MACRO_GNU_define:
18681 case DW_MACRO_GNU_undef:
18682 /* Only skip the data by MAC_PTR. */
18683 {
18684 unsigned int bytes_read;
18685
18686 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18687 mac_ptr += bytes_read;
18688 read_direct_string (abfd, mac_ptr, &bytes_read);
18689 mac_ptr += bytes_read;
18690 }
18691 break;
18692
18693 case DW_MACRO_GNU_start_file:
18694 {
18695 unsigned int bytes_read;
18696 int line, file;
18697
18698 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18699 mac_ptr += bytes_read;
18700 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18701 mac_ptr += bytes_read;
18702
18703 current_file = macro_start_file (file, line, current_file,
18704 comp_dir, lh, objfile);
18705 }
18706 break;
18707
18708 case DW_MACRO_GNU_end_file:
18709 /* No data to skip by MAC_PTR. */
18710 break;
18711
18712 case DW_MACRO_GNU_define_indirect:
18713 case DW_MACRO_GNU_undef_indirect:
18714 case DW_MACRO_GNU_define_indirect_alt:
18715 case DW_MACRO_GNU_undef_indirect_alt:
18716 {
18717 unsigned int bytes_read;
18718
18719 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18720 mac_ptr += bytes_read;
18721 mac_ptr += offset_size;
18722 }
18723 break;
18724
18725 case DW_MACRO_GNU_transparent_include:
18726 case DW_MACRO_GNU_transparent_include_alt:
18727 /* Note that, according to the spec, a transparent include
18728 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18729 skip this opcode. */
18730 mac_ptr += offset_size;
18731 break;
18732
18733 case DW_MACINFO_vendor_ext:
18734 /* Only skip the data by MAC_PTR. */
18735 if (!section_is_gnu)
18736 {
18737 unsigned int bytes_read;
18738
18739 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18740 mac_ptr += bytes_read;
18741 read_direct_string (abfd, mac_ptr, &bytes_read);
18742 mac_ptr += bytes_read;
18743 }
18744 /* FALLTHROUGH */
18745
18746 default:
18747 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18748 mac_ptr, mac_end, abfd, offset_size,
18749 section);
18750 if (mac_ptr == NULL)
18751 return;
18752 break;
18753 }
18754 } while (macinfo_type != 0 && current_file == NULL);
18755
18756 /* Second pass: Process all entries.
18757
18758 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18759 command-line macro definitions/undefinitions. This flag is unset when we
18760 reach the first DW_MACINFO_start_file entry. */
18761
18762 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18763 NULL, xcalloc, xfree);
18764 cleanup = make_cleanup_htab_delete (include_hash);
18765 mac_ptr = section->buffer + offset;
18766 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18767 *slot = mac_ptr;
18768 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18769 current_file, lh, comp_dir, section,
18770 section_is_gnu, 0,
18771 offset_size, objfile, include_hash);
18772 do_cleanups (cleanup);
18773 }
18774
18775 /* Check if the attribute's form is a DW_FORM_block*
18776 if so return true else false. */
18777
18778 static int
18779 attr_form_is_block (struct attribute *attr)
18780 {
18781 return (attr == NULL ? 0 :
18782 attr->form == DW_FORM_block1
18783 || attr->form == DW_FORM_block2
18784 || attr->form == DW_FORM_block4
18785 || attr->form == DW_FORM_block
18786 || attr->form == DW_FORM_exprloc);
18787 }
18788
18789 /* Return non-zero if ATTR's value is a section offset --- classes
18790 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18791 You may use DW_UNSND (attr) to retrieve such offsets.
18792
18793 Section 7.5.4, "Attribute Encodings", explains that no attribute
18794 may have a value that belongs to more than one of these classes; it
18795 would be ambiguous if we did, because we use the same forms for all
18796 of them. */
18797
18798 static int
18799 attr_form_is_section_offset (struct attribute *attr)
18800 {
18801 return (attr->form == DW_FORM_data4
18802 || attr->form == DW_FORM_data8
18803 || attr->form == DW_FORM_sec_offset);
18804 }
18805
18806 /* Return non-zero if ATTR's value falls in the 'constant' class, or
18807 zero otherwise. When this function returns true, you can apply
18808 dwarf2_get_attr_constant_value to it.
18809
18810 However, note that for some attributes you must check
18811 attr_form_is_section_offset before using this test. DW_FORM_data4
18812 and DW_FORM_data8 are members of both the constant class, and of
18813 the classes that contain offsets into other debug sections
18814 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18815 that, if an attribute's can be either a constant or one of the
18816 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18817 taken as section offsets, not constants. */
18818
18819 static int
18820 attr_form_is_constant (struct attribute *attr)
18821 {
18822 switch (attr->form)
18823 {
18824 case DW_FORM_sdata:
18825 case DW_FORM_udata:
18826 case DW_FORM_data1:
18827 case DW_FORM_data2:
18828 case DW_FORM_data4:
18829 case DW_FORM_data8:
18830 return 1;
18831 default:
18832 return 0;
18833 }
18834 }
18835
18836 /* Return the .debug_loc section to use for CU.
18837 For DWO files use .debug_loc.dwo. */
18838
18839 static struct dwarf2_section_info *
18840 cu_debug_loc_section (struct dwarf2_cu *cu)
18841 {
18842 if (cu->dwo_unit)
18843 return &cu->dwo_unit->dwo_file->sections.loc;
18844 return &dwarf2_per_objfile->loc;
18845 }
18846
18847 /* A helper function that fills in a dwarf2_loclist_baton. */
18848
18849 static void
18850 fill_in_loclist_baton (struct dwarf2_cu *cu,
18851 struct dwarf2_loclist_baton *baton,
18852 struct attribute *attr)
18853 {
18854 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18855
18856 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18857
18858 baton->per_cu = cu->per_cu;
18859 gdb_assert (baton->per_cu);
18860 /* We don't know how long the location list is, but make sure we
18861 don't run off the edge of the section. */
18862 baton->size = section->size - DW_UNSND (attr);
18863 baton->data = section->buffer + DW_UNSND (attr);
18864 baton->base_address = cu->base_address;
18865 baton->from_dwo = cu->dwo_unit != NULL;
18866 }
18867
18868 static void
18869 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18870 struct dwarf2_cu *cu)
18871 {
18872 struct objfile *objfile = dwarf2_per_objfile->objfile;
18873 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18874
18875 if (attr_form_is_section_offset (attr)
18876 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18877 the section. If so, fall through to the complaint in the
18878 other branch. */
18879 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18880 {
18881 struct dwarf2_loclist_baton *baton;
18882
18883 baton = obstack_alloc (&objfile->objfile_obstack,
18884 sizeof (struct dwarf2_loclist_baton));
18885
18886 fill_in_loclist_baton (cu, baton, attr);
18887
18888 if (cu->base_known == 0)
18889 complaint (&symfile_complaints,
18890 _("Location list used without "
18891 "specifying the CU base address."));
18892
18893 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18894 SYMBOL_LOCATION_BATON (sym) = baton;
18895 }
18896 else
18897 {
18898 struct dwarf2_locexpr_baton *baton;
18899
18900 baton = obstack_alloc (&objfile->objfile_obstack,
18901 sizeof (struct dwarf2_locexpr_baton));
18902 baton->per_cu = cu->per_cu;
18903 gdb_assert (baton->per_cu);
18904
18905 if (attr_form_is_block (attr))
18906 {
18907 /* Note that we're just copying the block's data pointer
18908 here, not the actual data. We're still pointing into the
18909 info_buffer for SYM's objfile; right now we never release
18910 that buffer, but when we do clean up properly this may
18911 need to change. */
18912 baton->size = DW_BLOCK (attr)->size;
18913 baton->data = DW_BLOCK (attr)->data;
18914 }
18915 else
18916 {
18917 dwarf2_invalid_attrib_class_complaint ("location description",
18918 SYMBOL_NATURAL_NAME (sym));
18919 baton->size = 0;
18920 }
18921
18922 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18923 SYMBOL_LOCATION_BATON (sym) = baton;
18924 }
18925 }
18926
18927 /* Return the OBJFILE associated with the compilation unit CU. If CU
18928 came from a separate debuginfo file, then the master objfile is
18929 returned. */
18930
18931 struct objfile *
18932 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18933 {
18934 struct objfile *objfile = per_cu->objfile;
18935
18936 /* Return the master objfile, so that we can report and look up the
18937 correct file containing this variable. */
18938 if (objfile->separate_debug_objfile_backlink)
18939 objfile = objfile->separate_debug_objfile_backlink;
18940
18941 return objfile;
18942 }
18943
18944 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18945 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18946 CU_HEADERP first. */
18947
18948 static const struct comp_unit_head *
18949 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18950 struct dwarf2_per_cu_data *per_cu)
18951 {
18952 gdb_byte *info_ptr;
18953
18954 if (per_cu->cu)
18955 return &per_cu->cu->header;
18956
18957 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18958
18959 memset (cu_headerp, 0, sizeof (*cu_headerp));
18960 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18961
18962 return cu_headerp;
18963 }
18964
18965 /* Return the address size given in the compilation unit header for CU. */
18966
18967 int
18968 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18969 {
18970 struct comp_unit_head cu_header_local;
18971 const struct comp_unit_head *cu_headerp;
18972
18973 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18974
18975 return cu_headerp->addr_size;
18976 }
18977
18978 /* Return the offset size given in the compilation unit header for CU. */
18979
18980 int
18981 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18982 {
18983 struct comp_unit_head cu_header_local;
18984 const struct comp_unit_head *cu_headerp;
18985
18986 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18987
18988 return cu_headerp->offset_size;
18989 }
18990
18991 /* See its dwarf2loc.h declaration. */
18992
18993 int
18994 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18995 {
18996 struct comp_unit_head cu_header_local;
18997 const struct comp_unit_head *cu_headerp;
18998
18999 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19000
19001 if (cu_headerp->version == 2)
19002 return cu_headerp->addr_size;
19003 else
19004 return cu_headerp->offset_size;
19005 }
19006
19007 /* Return the text offset of the CU. The returned offset comes from
19008 this CU's objfile. If this objfile came from a separate debuginfo
19009 file, then the offset may be different from the corresponding
19010 offset in the parent objfile. */
19011
19012 CORE_ADDR
19013 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19014 {
19015 struct objfile *objfile = per_cu->objfile;
19016
19017 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19018 }
19019
19020 /* Locate the .debug_info compilation unit from CU's objfile which contains
19021 the DIE at OFFSET. Raises an error on failure. */
19022
19023 static struct dwarf2_per_cu_data *
19024 dwarf2_find_containing_comp_unit (sect_offset offset,
19025 unsigned int offset_in_dwz,
19026 struct objfile *objfile)
19027 {
19028 struct dwarf2_per_cu_data *this_cu;
19029 int low, high;
19030 const sect_offset *cu_off;
19031
19032 low = 0;
19033 high = dwarf2_per_objfile->n_comp_units - 1;
19034 while (high > low)
19035 {
19036 struct dwarf2_per_cu_data *mid_cu;
19037 int mid = low + (high - low) / 2;
19038
19039 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19040 cu_off = &mid_cu->offset;
19041 if (mid_cu->is_dwz > offset_in_dwz
19042 || (mid_cu->is_dwz == offset_in_dwz
19043 && cu_off->sect_off >= offset.sect_off))
19044 high = mid;
19045 else
19046 low = mid + 1;
19047 }
19048 gdb_assert (low == high);
19049 this_cu = dwarf2_per_objfile->all_comp_units[low];
19050 cu_off = &this_cu->offset;
19051 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19052 {
19053 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19054 error (_("Dwarf Error: could not find partial DIE containing "
19055 "offset 0x%lx [in module %s]"),
19056 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19057
19058 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19059 <= offset.sect_off);
19060 return dwarf2_per_objfile->all_comp_units[low-1];
19061 }
19062 else
19063 {
19064 this_cu = dwarf2_per_objfile->all_comp_units[low];
19065 if (low == dwarf2_per_objfile->n_comp_units - 1
19066 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19067 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19068 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19069 return this_cu;
19070 }
19071 }
19072
19073 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19074
19075 static void
19076 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19077 {
19078 memset (cu, 0, sizeof (*cu));
19079 per_cu->cu = cu;
19080 cu->per_cu = per_cu;
19081 cu->objfile = per_cu->objfile;
19082 obstack_init (&cu->comp_unit_obstack);
19083 }
19084
19085 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19086
19087 static void
19088 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19089 enum language pretend_language)
19090 {
19091 struct attribute *attr;
19092
19093 /* Set the language we're debugging. */
19094 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19095 if (attr)
19096 set_cu_language (DW_UNSND (attr), cu);
19097 else
19098 {
19099 cu->language = pretend_language;
19100 cu->language_defn = language_def (cu->language);
19101 }
19102
19103 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19104 if (attr)
19105 cu->producer = DW_STRING (attr);
19106 }
19107
19108 /* Release one cached compilation unit, CU. We unlink it from the tree
19109 of compilation units, but we don't remove it from the read_in_chain;
19110 the caller is responsible for that.
19111 NOTE: DATA is a void * because this function is also used as a
19112 cleanup routine. */
19113
19114 static void
19115 free_heap_comp_unit (void *data)
19116 {
19117 struct dwarf2_cu *cu = data;
19118
19119 gdb_assert (cu->per_cu != NULL);
19120 cu->per_cu->cu = NULL;
19121 cu->per_cu = NULL;
19122
19123 obstack_free (&cu->comp_unit_obstack, NULL);
19124
19125 xfree (cu);
19126 }
19127
19128 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19129 when we're finished with it. We can't free the pointer itself, but be
19130 sure to unlink it from the cache. Also release any associated storage. */
19131
19132 static void
19133 free_stack_comp_unit (void *data)
19134 {
19135 struct dwarf2_cu *cu = data;
19136
19137 gdb_assert (cu->per_cu != NULL);
19138 cu->per_cu->cu = NULL;
19139 cu->per_cu = NULL;
19140
19141 obstack_free (&cu->comp_unit_obstack, NULL);
19142 cu->partial_dies = NULL;
19143 }
19144
19145 /* Free all cached compilation units. */
19146
19147 static void
19148 free_cached_comp_units (void *data)
19149 {
19150 struct dwarf2_per_cu_data *per_cu, **last_chain;
19151
19152 per_cu = dwarf2_per_objfile->read_in_chain;
19153 last_chain = &dwarf2_per_objfile->read_in_chain;
19154 while (per_cu != NULL)
19155 {
19156 struct dwarf2_per_cu_data *next_cu;
19157
19158 next_cu = per_cu->cu->read_in_chain;
19159
19160 free_heap_comp_unit (per_cu->cu);
19161 *last_chain = next_cu;
19162
19163 per_cu = next_cu;
19164 }
19165 }
19166
19167 /* Increase the age counter on each cached compilation unit, and free
19168 any that are too old. */
19169
19170 static void
19171 age_cached_comp_units (void)
19172 {
19173 struct dwarf2_per_cu_data *per_cu, **last_chain;
19174
19175 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19176 per_cu = dwarf2_per_objfile->read_in_chain;
19177 while (per_cu != NULL)
19178 {
19179 per_cu->cu->last_used ++;
19180 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19181 dwarf2_mark (per_cu->cu);
19182 per_cu = per_cu->cu->read_in_chain;
19183 }
19184
19185 per_cu = dwarf2_per_objfile->read_in_chain;
19186 last_chain = &dwarf2_per_objfile->read_in_chain;
19187 while (per_cu != NULL)
19188 {
19189 struct dwarf2_per_cu_data *next_cu;
19190
19191 next_cu = per_cu->cu->read_in_chain;
19192
19193 if (!per_cu->cu->mark)
19194 {
19195 free_heap_comp_unit (per_cu->cu);
19196 *last_chain = next_cu;
19197 }
19198 else
19199 last_chain = &per_cu->cu->read_in_chain;
19200
19201 per_cu = next_cu;
19202 }
19203 }
19204
19205 /* Remove a single compilation unit from the cache. */
19206
19207 static void
19208 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19209 {
19210 struct dwarf2_per_cu_data *per_cu, **last_chain;
19211
19212 per_cu = dwarf2_per_objfile->read_in_chain;
19213 last_chain = &dwarf2_per_objfile->read_in_chain;
19214 while (per_cu != NULL)
19215 {
19216 struct dwarf2_per_cu_data *next_cu;
19217
19218 next_cu = per_cu->cu->read_in_chain;
19219
19220 if (per_cu == target_per_cu)
19221 {
19222 free_heap_comp_unit (per_cu->cu);
19223 per_cu->cu = NULL;
19224 *last_chain = next_cu;
19225 break;
19226 }
19227 else
19228 last_chain = &per_cu->cu->read_in_chain;
19229
19230 per_cu = next_cu;
19231 }
19232 }
19233
19234 /* Release all extra memory associated with OBJFILE. */
19235
19236 void
19237 dwarf2_free_objfile (struct objfile *objfile)
19238 {
19239 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19240
19241 if (dwarf2_per_objfile == NULL)
19242 return;
19243
19244 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19245 free_cached_comp_units (NULL);
19246
19247 if (dwarf2_per_objfile->quick_file_names_table)
19248 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19249
19250 /* Everything else should be on the objfile obstack. */
19251 }
19252
19253 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19254 We store these in a hash table separate from the DIEs, and preserve them
19255 when the DIEs are flushed out of cache.
19256
19257 The CU "per_cu" pointer is needed because offset alone is not enough to
19258 uniquely identify the type. A file may have multiple .debug_types sections,
19259 or the type may come from a DWO file. We have to use something in
19260 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19261 routine, get_die_type_at_offset, from outside this file, and thus won't
19262 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19263 of the objfile. */
19264
19265 struct dwarf2_per_cu_offset_and_type
19266 {
19267 const struct dwarf2_per_cu_data *per_cu;
19268 sect_offset offset;
19269 struct type *type;
19270 };
19271
19272 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19273
19274 static hashval_t
19275 per_cu_offset_and_type_hash (const void *item)
19276 {
19277 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19278
19279 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19280 }
19281
19282 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19283
19284 static int
19285 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19286 {
19287 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19288 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19289
19290 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19291 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19292 }
19293
19294 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19295 table if necessary. For convenience, return TYPE.
19296
19297 The DIEs reading must have careful ordering to:
19298 * Not cause infite loops trying to read in DIEs as a prerequisite for
19299 reading current DIE.
19300 * Not trying to dereference contents of still incompletely read in types
19301 while reading in other DIEs.
19302 * Enable referencing still incompletely read in types just by a pointer to
19303 the type without accessing its fields.
19304
19305 Therefore caller should follow these rules:
19306 * Try to fetch any prerequisite types we may need to build this DIE type
19307 before building the type and calling set_die_type.
19308 * After building type call set_die_type for current DIE as soon as
19309 possible before fetching more types to complete the current type.
19310 * Make the type as complete as possible before fetching more types. */
19311
19312 static struct type *
19313 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19314 {
19315 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19316 struct objfile *objfile = cu->objfile;
19317
19318 /* For Ada types, make sure that the gnat-specific data is always
19319 initialized (if not already set). There are a few types where
19320 we should not be doing so, because the type-specific area is
19321 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19322 where the type-specific area is used to store the floatformat).
19323 But this is not a problem, because the gnat-specific information
19324 is actually not needed for these types. */
19325 if (need_gnat_info (cu)
19326 && TYPE_CODE (type) != TYPE_CODE_FUNC
19327 && TYPE_CODE (type) != TYPE_CODE_FLT
19328 && !HAVE_GNAT_AUX_INFO (type))
19329 INIT_GNAT_SPECIFIC (type);
19330
19331 if (dwarf2_per_objfile->die_type_hash == NULL)
19332 {
19333 dwarf2_per_objfile->die_type_hash =
19334 htab_create_alloc_ex (127,
19335 per_cu_offset_and_type_hash,
19336 per_cu_offset_and_type_eq,
19337 NULL,
19338 &objfile->objfile_obstack,
19339 hashtab_obstack_allocate,
19340 dummy_obstack_deallocate);
19341 }
19342
19343 ofs.per_cu = cu->per_cu;
19344 ofs.offset = die->offset;
19345 ofs.type = type;
19346 slot = (struct dwarf2_per_cu_offset_and_type **)
19347 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19348 if (*slot)
19349 complaint (&symfile_complaints,
19350 _("A problem internal to GDB: DIE 0x%x has type already set"),
19351 die->offset.sect_off);
19352 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19353 **slot = ofs;
19354 return type;
19355 }
19356
19357 /* Look up the type for the die at OFFSET in the appropriate type_hash
19358 table, or return NULL if the die does not have a saved type. */
19359
19360 static struct type *
19361 get_die_type_at_offset (sect_offset offset,
19362 struct dwarf2_per_cu_data *per_cu)
19363 {
19364 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19365
19366 if (dwarf2_per_objfile->die_type_hash == NULL)
19367 return NULL;
19368
19369 ofs.per_cu = per_cu;
19370 ofs.offset = offset;
19371 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19372 if (slot)
19373 return slot->type;
19374 else
19375 return NULL;
19376 }
19377
19378 /* Look up the type for DIE in the appropriate type_hash table,
19379 or return NULL if DIE does not have a saved type. */
19380
19381 static struct type *
19382 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19383 {
19384 return get_die_type_at_offset (die->offset, cu->per_cu);
19385 }
19386
19387 /* Add a dependence relationship from CU to REF_PER_CU. */
19388
19389 static void
19390 dwarf2_add_dependence (struct dwarf2_cu *cu,
19391 struct dwarf2_per_cu_data *ref_per_cu)
19392 {
19393 void **slot;
19394
19395 if (cu->dependencies == NULL)
19396 cu->dependencies
19397 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19398 NULL, &cu->comp_unit_obstack,
19399 hashtab_obstack_allocate,
19400 dummy_obstack_deallocate);
19401
19402 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19403 if (*slot == NULL)
19404 *slot = ref_per_cu;
19405 }
19406
19407 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19408 Set the mark field in every compilation unit in the
19409 cache that we must keep because we are keeping CU. */
19410
19411 static int
19412 dwarf2_mark_helper (void **slot, void *data)
19413 {
19414 struct dwarf2_per_cu_data *per_cu;
19415
19416 per_cu = (struct dwarf2_per_cu_data *) *slot;
19417
19418 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19419 reading of the chain. As such dependencies remain valid it is not much
19420 useful to track and undo them during QUIT cleanups. */
19421 if (per_cu->cu == NULL)
19422 return 1;
19423
19424 if (per_cu->cu->mark)
19425 return 1;
19426 per_cu->cu->mark = 1;
19427
19428 if (per_cu->cu->dependencies != NULL)
19429 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19430
19431 return 1;
19432 }
19433
19434 /* Set the mark field in CU and in every other compilation unit in the
19435 cache that we must keep because we are keeping CU. */
19436
19437 static void
19438 dwarf2_mark (struct dwarf2_cu *cu)
19439 {
19440 if (cu->mark)
19441 return;
19442 cu->mark = 1;
19443 if (cu->dependencies != NULL)
19444 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19445 }
19446
19447 static void
19448 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19449 {
19450 while (per_cu)
19451 {
19452 per_cu->cu->mark = 0;
19453 per_cu = per_cu->cu->read_in_chain;
19454 }
19455 }
19456
19457 /* Trivial hash function for partial_die_info: the hash value of a DIE
19458 is its offset in .debug_info for this objfile. */
19459
19460 static hashval_t
19461 partial_die_hash (const void *item)
19462 {
19463 const struct partial_die_info *part_die = item;
19464
19465 return part_die->offset.sect_off;
19466 }
19467
19468 /* Trivial comparison function for partial_die_info structures: two DIEs
19469 are equal if they have the same offset. */
19470
19471 static int
19472 partial_die_eq (const void *item_lhs, const void *item_rhs)
19473 {
19474 const struct partial_die_info *part_die_lhs = item_lhs;
19475 const struct partial_die_info *part_die_rhs = item_rhs;
19476
19477 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19478 }
19479
19480 static struct cmd_list_element *set_dwarf2_cmdlist;
19481 static struct cmd_list_element *show_dwarf2_cmdlist;
19482
19483 static void
19484 set_dwarf2_cmd (char *args, int from_tty)
19485 {
19486 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19487 }
19488
19489 static void
19490 show_dwarf2_cmd (char *args, int from_tty)
19491 {
19492 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19493 }
19494
19495 /* Free data associated with OBJFILE, if necessary. */
19496
19497 static void
19498 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19499 {
19500 struct dwarf2_per_objfile *data = d;
19501 int ix;
19502
19503 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19504 VEC_free (dwarf2_per_cu_ptr,
19505 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
19506
19507 VEC_free (dwarf2_section_info_def, data->types);
19508
19509 if (data->dwo_files)
19510 free_dwo_files (data->dwo_files, objfile);
19511
19512 if (data->dwz_file && data->dwz_file->dwz_bfd)
19513 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19514 }
19515
19516 \f
19517 /* The "save gdb-index" command. */
19518
19519 /* The contents of the hash table we create when building the string
19520 table. */
19521 struct strtab_entry
19522 {
19523 offset_type offset;
19524 const char *str;
19525 };
19526
19527 /* Hash function for a strtab_entry.
19528
19529 Function is used only during write_hash_table so no index format backward
19530 compatibility is needed. */
19531
19532 static hashval_t
19533 hash_strtab_entry (const void *e)
19534 {
19535 const struct strtab_entry *entry = e;
19536 return mapped_index_string_hash (INT_MAX, entry->str);
19537 }
19538
19539 /* Equality function for a strtab_entry. */
19540
19541 static int
19542 eq_strtab_entry (const void *a, const void *b)
19543 {
19544 const struct strtab_entry *ea = a;
19545 const struct strtab_entry *eb = b;
19546 return !strcmp (ea->str, eb->str);
19547 }
19548
19549 /* Create a strtab_entry hash table. */
19550
19551 static htab_t
19552 create_strtab (void)
19553 {
19554 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19555 xfree, xcalloc, xfree);
19556 }
19557
19558 /* Add a string to the constant pool. Return the string's offset in
19559 host order. */
19560
19561 static offset_type
19562 add_string (htab_t table, struct obstack *cpool, const char *str)
19563 {
19564 void **slot;
19565 struct strtab_entry entry;
19566 struct strtab_entry *result;
19567
19568 entry.str = str;
19569 slot = htab_find_slot (table, &entry, INSERT);
19570 if (*slot)
19571 result = *slot;
19572 else
19573 {
19574 result = XNEW (struct strtab_entry);
19575 result->offset = obstack_object_size (cpool);
19576 result->str = str;
19577 obstack_grow_str0 (cpool, str);
19578 *slot = result;
19579 }
19580 return result->offset;
19581 }
19582
19583 /* An entry in the symbol table. */
19584 struct symtab_index_entry
19585 {
19586 /* The name of the symbol. */
19587 const char *name;
19588 /* The offset of the name in the constant pool. */
19589 offset_type index_offset;
19590 /* A sorted vector of the indices of all the CUs that hold an object
19591 of this name. */
19592 VEC (offset_type) *cu_indices;
19593 };
19594
19595 /* The symbol table. This is a power-of-2-sized hash table. */
19596 struct mapped_symtab
19597 {
19598 offset_type n_elements;
19599 offset_type size;
19600 struct symtab_index_entry **data;
19601 };
19602
19603 /* Hash function for a symtab_index_entry. */
19604
19605 static hashval_t
19606 hash_symtab_entry (const void *e)
19607 {
19608 const struct symtab_index_entry *entry = e;
19609 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19610 sizeof (offset_type) * VEC_length (offset_type,
19611 entry->cu_indices),
19612 0);
19613 }
19614
19615 /* Equality function for a symtab_index_entry. */
19616
19617 static int
19618 eq_symtab_entry (const void *a, const void *b)
19619 {
19620 const struct symtab_index_entry *ea = a;
19621 const struct symtab_index_entry *eb = b;
19622 int len = VEC_length (offset_type, ea->cu_indices);
19623 if (len != VEC_length (offset_type, eb->cu_indices))
19624 return 0;
19625 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19626 VEC_address (offset_type, eb->cu_indices),
19627 sizeof (offset_type) * len);
19628 }
19629
19630 /* Destroy a symtab_index_entry. */
19631
19632 static void
19633 delete_symtab_entry (void *p)
19634 {
19635 struct symtab_index_entry *entry = p;
19636 VEC_free (offset_type, entry->cu_indices);
19637 xfree (entry);
19638 }
19639
19640 /* Create a hash table holding symtab_index_entry objects. */
19641
19642 static htab_t
19643 create_symbol_hash_table (void)
19644 {
19645 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19646 delete_symtab_entry, xcalloc, xfree);
19647 }
19648
19649 /* Create a new mapped symtab object. */
19650
19651 static struct mapped_symtab *
19652 create_mapped_symtab (void)
19653 {
19654 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19655 symtab->n_elements = 0;
19656 symtab->size = 1024;
19657 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19658 return symtab;
19659 }
19660
19661 /* Destroy a mapped_symtab. */
19662
19663 static void
19664 cleanup_mapped_symtab (void *p)
19665 {
19666 struct mapped_symtab *symtab = p;
19667 /* The contents of the array are freed when the other hash table is
19668 destroyed. */
19669 xfree (symtab->data);
19670 xfree (symtab);
19671 }
19672
19673 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19674 the slot.
19675
19676 Function is used only during write_hash_table so no index format backward
19677 compatibility is needed. */
19678
19679 static struct symtab_index_entry **
19680 find_slot (struct mapped_symtab *symtab, const char *name)
19681 {
19682 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19683
19684 index = hash & (symtab->size - 1);
19685 step = ((hash * 17) & (symtab->size - 1)) | 1;
19686
19687 for (;;)
19688 {
19689 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19690 return &symtab->data[index];
19691 index = (index + step) & (symtab->size - 1);
19692 }
19693 }
19694
19695 /* Expand SYMTAB's hash table. */
19696
19697 static void
19698 hash_expand (struct mapped_symtab *symtab)
19699 {
19700 offset_type old_size = symtab->size;
19701 offset_type i;
19702 struct symtab_index_entry **old_entries = symtab->data;
19703
19704 symtab->size *= 2;
19705 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19706
19707 for (i = 0; i < old_size; ++i)
19708 {
19709 if (old_entries[i])
19710 {
19711 struct symtab_index_entry **slot = find_slot (symtab,
19712 old_entries[i]->name);
19713 *slot = old_entries[i];
19714 }
19715 }
19716
19717 xfree (old_entries);
19718 }
19719
19720 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19721 CU_INDEX is the index of the CU in which the symbol appears.
19722 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19723
19724 static void
19725 add_index_entry (struct mapped_symtab *symtab, const char *name,
19726 int is_static, gdb_index_symbol_kind kind,
19727 offset_type cu_index)
19728 {
19729 struct symtab_index_entry **slot;
19730 offset_type cu_index_and_attrs;
19731
19732 ++symtab->n_elements;
19733 if (4 * symtab->n_elements / 3 >= symtab->size)
19734 hash_expand (symtab);
19735
19736 slot = find_slot (symtab, name);
19737 if (!*slot)
19738 {
19739 *slot = XNEW (struct symtab_index_entry);
19740 (*slot)->name = name;
19741 /* index_offset is set later. */
19742 (*slot)->cu_indices = NULL;
19743 }
19744
19745 cu_index_and_attrs = 0;
19746 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19747 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19748 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19749
19750 /* We don't want to record an index value twice as we want to avoid the
19751 duplication.
19752 We process all global symbols and then all static symbols
19753 (which would allow us to avoid the duplication by only having to check
19754 the last entry pushed), but a symbol could have multiple kinds in one CU.
19755 To keep things simple we don't worry about the duplication here and
19756 sort and uniqufy the list after we've processed all symbols. */
19757 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19758 }
19759
19760 /* qsort helper routine for uniquify_cu_indices. */
19761
19762 static int
19763 offset_type_compare (const void *ap, const void *bp)
19764 {
19765 offset_type a = *(offset_type *) ap;
19766 offset_type b = *(offset_type *) bp;
19767
19768 return (a > b) - (b > a);
19769 }
19770
19771 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19772
19773 static void
19774 uniquify_cu_indices (struct mapped_symtab *symtab)
19775 {
19776 int i;
19777
19778 for (i = 0; i < symtab->size; ++i)
19779 {
19780 struct symtab_index_entry *entry = symtab->data[i];
19781
19782 if (entry
19783 && entry->cu_indices != NULL)
19784 {
19785 unsigned int next_to_insert, next_to_check;
19786 offset_type last_value;
19787
19788 qsort (VEC_address (offset_type, entry->cu_indices),
19789 VEC_length (offset_type, entry->cu_indices),
19790 sizeof (offset_type), offset_type_compare);
19791
19792 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19793 next_to_insert = 1;
19794 for (next_to_check = 1;
19795 next_to_check < VEC_length (offset_type, entry->cu_indices);
19796 ++next_to_check)
19797 {
19798 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19799 != last_value)
19800 {
19801 last_value = VEC_index (offset_type, entry->cu_indices,
19802 next_to_check);
19803 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19804 last_value);
19805 ++next_to_insert;
19806 }
19807 }
19808 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19809 }
19810 }
19811 }
19812
19813 /* Add a vector of indices to the constant pool. */
19814
19815 static offset_type
19816 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19817 struct symtab_index_entry *entry)
19818 {
19819 void **slot;
19820
19821 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19822 if (!*slot)
19823 {
19824 offset_type len = VEC_length (offset_type, entry->cu_indices);
19825 offset_type val = MAYBE_SWAP (len);
19826 offset_type iter;
19827 int i;
19828
19829 *slot = entry;
19830 entry->index_offset = obstack_object_size (cpool);
19831
19832 obstack_grow (cpool, &val, sizeof (val));
19833 for (i = 0;
19834 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19835 ++i)
19836 {
19837 val = MAYBE_SWAP (iter);
19838 obstack_grow (cpool, &val, sizeof (val));
19839 }
19840 }
19841 else
19842 {
19843 struct symtab_index_entry *old_entry = *slot;
19844 entry->index_offset = old_entry->index_offset;
19845 entry = old_entry;
19846 }
19847 return entry->index_offset;
19848 }
19849
19850 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19851 constant pool entries going into the obstack CPOOL. */
19852
19853 static void
19854 write_hash_table (struct mapped_symtab *symtab,
19855 struct obstack *output, struct obstack *cpool)
19856 {
19857 offset_type i;
19858 htab_t symbol_hash_table;
19859 htab_t str_table;
19860
19861 symbol_hash_table = create_symbol_hash_table ();
19862 str_table = create_strtab ();
19863
19864 /* We add all the index vectors to the constant pool first, to
19865 ensure alignment is ok. */
19866 for (i = 0; i < symtab->size; ++i)
19867 {
19868 if (symtab->data[i])
19869 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19870 }
19871
19872 /* Now write out the hash table. */
19873 for (i = 0; i < symtab->size; ++i)
19874 {
19875 offset_type str_off, vec_off;
19876
19877 if (symtab->data[i])
19878 {
19879 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19880 vec_off = symtab->data[i]->index_offset;
19881 }
19882 else
19883 {
19884 /* While 0 is a valid constant pool index, it is not valid
19885 to have 0 for both offsets. */
19886 str_off = 0;
19887 vec_off = 0;
19888 }
19889
19890 str_off = MAYBE_SWAP (str_off);
19891 vec_off = MAYBE_SWAP (vec_off);
19892
19893 obstack_grow (output, &str_off, sizeof (str_off));
19894 obstack_grow (output, &vec_off, sizeof (vec_off));
19895 }
19896
19897 htab_delete (str_table);
19898 htab_delete (symbol_hash_table);
19899 }
19900
19901 /* Struct to map psymtab to CU index in the index file. */
19902 struct psymtab_cu_index_map
19903 {
19904 struct partial_symtab *psymtab;
19905 unsigned int cu_index;
19906 };
19907
19908 static hashval_t
19909 hash_psymtab_cu_index (const void *item)
19910 {
19911 const struct psymtab_cu_index_map *map = item;
19912
19913 return htab_hash_pointer (map->psymtab);
19914 }
19915
19916 static int
19917 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19918 {
19919 const struct psymtab_cu_index_map *lhs = item_lhs;
19920 const struct psymtab_cu_index_map *rhs = item_rhs;
19921
19922 return lhs->psymtab == rhs->psymtab;
19923 }
19924
19925 /* Helper struct for building the address table. */
19926 struct addrmap_index_data
19927 {
19928 struct objfile *objfile;
19929 struct obstack *addr_obstack;
19930 htab_t cu_index_htab;
19931
19932 /* Non-zero if the previous_* fields are valid.
19933 We can't write an entry until we see the next entry (since it is only then
19934 that we know the end of the entry). */
19935 int previous_valid;
19936 /* Index of the CU in the table of all CUs in the index file. */
19937 unsigned int previous_cu_index;
19938 /* Start address of the CU. */
19939 CORE_ADDR previous_cu_start;
19940 };
19941
19942 /* Write an address entry to OBSTACK. */
19943
19944 static void
19945 add_address_entry (struct objfile *objfile, struct obstack *obstack,
19946 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19947 {
19948 offset_type cu_index_to_write;
19949 char addr[8];
19950 CORE_ADDR baseaddr;
19951
19952 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19953
19954 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19955 obstack_grow (obstack, addr, 8);
19956 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19957 obstack_grow (obstack, addr, 8);
19958 cu_index_to_write = MAYBE_SWAP (cu_index);
19959 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19960 }
19961
19962 /* Worker function for traversing an addrmap to build the address table. */
19963
19964 static int
19965 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19966 {
19967 struct addrmap_index_data *data = datap;
19968 struct partial_symtab *pst = obj;
19969
19970 if (data->previous_valid)
19971 add_address_entry (data->objfile, data->addr_obstack,
19972 data->previous_cu_start, start_addr,
19973 data->previous_cu_index);
19974
19975 data->previous_cu_start = start_addr;
19976 if (pst != NULL)
19977 {
19978 struct psymtab_cu_index_map find_map, *map;
19979 find_map.psymtab = pst;
19980 map = htab_find (data->cu_index_htab, &find_map);
19981 gdb_assert (map != NULL);
19982 data->previous_cu_index = map->cu_index;
19983 data->previous_valid = 1;
19984 }
19985 else
19986 data->previous_valid = 0;
19987
19988 return 0;
19989 }
19990
19991 /* Write OBJFILE's address map to OBSTACK.
19992 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19993 in the index file. */
19994
19995 static void
19996 write_address_map (struct objfile *objfile, struct obstack *obstack,
19997 htab_t cu_index_htab)
19998 {
19999 struct addrmap_index_data addrmap_index_data;
20000
20001 /* When writing the address table, we have to cope with the fact that
20002 the addrmap iterator only provides the start of a region; we have to
20003 wait until the next invocation to get the start of the next region. */
20004
20005 addrmap_index_data.objfile = objfile;
20006 addrmap_index_data.addr_obstack = obstack;
20007 addrmap_index_data.cu_index_htab = cu_index_htab;
20008 addrmap_index_data.previous_valid = 0;
20009
20010 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20011 &addrmap_index_data);
20012
20013 /* It's highly unlikely the last entry (end address = 0xff...ff)
20014 is valid, but we should still handle it.
20015 The end address is recorded as the start of the next region, but that
20016 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20017 anyway. */
20018 if (addrmap_index_data.previous_valid)
20019 add_address_entry (objfile, obstack,
20020 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20021 addrmap_index_data.previous_cu_index);
20022 }
20023
20024 /* Return the symbol kind of PSYM. */
20025
20026 static gdb_index_symbol_kind
20027 symbol_kind (struct partial_symbol *psym)
20028 {
20029 domain_enum domain = PSYMBOL_DOMAIN (psym);
20030 enum address_class aclass = PSYMBOL_CLASS (psym);
20031
20032 switch (domain)
20033 {
20034 case VAR_DOMAIN:
20035 switch (aclass)
20036 {
20037 case LOC_BLOCK:
20038 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20039 case LOC_TYPEDEF:
20040 return GDB_INDEX_SYMBOL_KIND_TYPE;
20041 case LOC_COMPUTED:
20042 case LOC_CONST_BYTES:
20043 case LOC_OPTIMIZED_OUT:
20044 case LOC_STATIC:
20045 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20046 case LOC_CONST:
20047 /* Note: It's currently impossible to recognize psyms as enum values
20048 short of reading the type info. For now punt. */
20049 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20050 default:
20051 /* There are other LOC_FOO values that one might want to classify
20052 as variables, but dwarf2read.c doesn't currently use them. */
20053 return GDB_INDEX_SYMBOL_KIND_OTHER;
20054 }
20055 case STRUCT_DOMAIN:
20056 return GDB_INDEX_SYMBOL_KIND_TYPE;
20057 default:
20058 return GDB_INDEX_SYMBOL_KIND_OTHER;
20059 }
20060 }
20061
20062 /* Add a list of partial symbols to SYMTAB. */
20063
20064 static void
20065 write_psymbols (struct mapped_symtab *symtab,
20066 htab_t psyms_seen,
20067 struct partial_symbol **psymp,
20068 int count,
20069 offset_type cu_index,
20070 int is_static)
20071 {
20072 for (; count-- > 0; ++psymp)
20073 {
20074 struct partial_symbol *psym = *psymp;
20075 void **slot;
20076
20077 if (SYMBOL_LANGUAGE (psym) == language_ada)
20078 error (_("Ada is not currently supported by the index"));
20079
20080 /* Only add a given psymbol once. */
20081 slot = htab_find_slot (psyms_seen, psym, INSERT);
20082 if (!*slot)
20083 {
20084 gdb_index_symbol_kind kind = symbol_kind (psym);
20085
20086 *slot = psym;
20087 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20088 is_static, kind, cu_index);
20089 }
20090 }
20091 }
20092
20093 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20094 exception if there is an error. */
20095
20096 static void
20097 write_obstack (FILE *file, struct obstack *obstack)
20098 {
20099 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20100 file)
20101 != obstack_object_size (obstack))
20102 error (_("couldn't data write to file"));
20103 }
20104
20105 /* Unlink a file if the argument is not NULL. */
20106
20107 static void
20108 unlink_if_set (void *p)
20109 {
20110 char **filename = p;
20111 if (*filename)
20112 unlink (*filename);
20113 }
20114
20115 /* A helper struct used when iterating over debug_types. */
20116 struct signatured_type_index_data
20117 {
20118 struct objfile *objfile;
20119 struct mapped_symtab *symtab;
20120 struct obstack *types_list;
20121 htab_t psyms_seen;
20122 int cu_index;
20123 };
20124
20125 /* A helper function that writes a single signatured_type to an
20126 obstack. */
20127
20128 static int
20129 write_one_signatured_type (void **slot, void *d)
20130 {
20131 struct signatured_type_index_data *info = d;
20132 struct signatured_type *entry = (struct signatured_type *) *slot;
20133 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20134 struct partial_symtab *psymtab = per_cu->v.psymtab;
20135 gdb_byte val[8];
20136
20137 write_psymbols (info->symtab,
20138 info->psyms_seen,
20139 info->objfile->global_psymbols.list
20140 + psymtab->globals_offset,
20141 psymtab->n_global_syms, info->cu_index,
20142 0);
20143 write_psymbols (info->symtab,
20144 info->psyms_seen,
20145 info->objfile->static_psymbols.list
20146 + psymtab->statics_offset,
20147 psymtab->n_static_syms, info->cu_index,
20148 1);
20149
20150 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20151 entry->per_cu.offset.sect_off);
20152 obstack_grow (info->types_list, val, 8);
20153 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20154 entry->type_offset_in_tu.cu_off);
20155 obstack_grow (info->types_list, val, 8);
20156 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20157 obstack_grow (info->types_list, val, 8);
20158
20159 ++info->cu_index;
20160
20161 return 1;
20162 }
20163
20164 /* Recurse into all "included" dependencies and write their symbols as
20165 if they appeared in this psymtab. */
20166
20167 static void
20168 recursively_write_psymbols (struct objfile *objfile,
20169 struct partial_symtab *psymtab,
20170 struct mapped_symtab *symtab,
20171 htab_t psyms_seen,
20172 offset_type cu_index)
20173 {
20174 int i;
20175
20176 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20177 if (psymtab->dependencies[i]->user != NULL)
20178 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20179 symtab, psyms_seen, cu_index);
20180
20181 write_psymbols (symtab,
20182 psyms_seen,
20183 objfile->global_psymbols.list + psymtab->globals_offset,
20184 psymtab->n_global_syms, cu_index,
20185 0);
20186 write_psymbols (symtab,
20187 psyms_seen,
20188 objfile->static_psymbols.list + psymtab->statics_offset,
20189 psymtab->n_static_syms, cu_index,
20190 1);
20191 }
20192
20193 /* Create an index file for OBJFILE in the directory DIR. */
20194
20195 static void
20196 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20197 {
20198 struct cleanup *cleanup;
20199 char *filename, *cleanup_filename;
20200 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20201 struct obstack cu_list, types_cu_list;
20202 int i;
20203 FILE *out_file;
20204 struct mapped_symtab *symtab;
20205 offset_type val, size_of_contents, total_len;
20206 struct stat st;
20207 htab_t psyms_seen;
20208 htab_t cu_index_htab;
20209 struct psymtab_cu_index_map *psymtab_cu_index_map;
20210
20211 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20212 return;
20213
20214 if (dwarf2_per_objfile->using_index)
20215 error (_("Cannot use an index to create the index"));
20216
20217 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20218 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20219
20220 if (stat (objfile->name, &st) < 0)
20221 perror_with_name (objfile->name);
20222
20223 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20224 INDEX_SUFFIX, (char *) NULL);
20225 cleanup = make_cleanup (xfree, filename);
20226
20227 out_file = fopen (filename, "wb");
20228 if (!out_file)
20229 error (_("Can't open `%s' for writing"), filename);
20230
20231 cleanup_filename = filename;
20232 make_cleanup (unlink_if_set, &cleanup_filename);
20233
20234 symtab = create_mapped_symtab ();
20235 make_cleanup (cleanup_mapped_symtab, symtab);
20236
20237 obstack_init (&addr_obstack);
20238 make_cleanup_obstack_free (&addr_obstack);
20239
20240 obstack_init (&cu_list);
20241 make_cleanup_obstack_free (&cu_list);
20242
20243 obstack_init (&types_cu_list);
20244 make_cleanup_obstack_free (&types_cu_list);
20245
20246 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20247 NULL, xcalloc, xfree);
20248 make_cleanup_htab_delete (psyms_seen);
20249
20250 /* While we're scanning CU's create a table that maps a psymtab pointer
20251 (which is what addrmap records) to its index (which is what is recorded
20252 in the index file). This will later be needed to write the address
20253 table. */
20254 cu_index_htab = htab_create_alloc (100,
20255 hash_psymtab_cu_index,
20256 eq_psymtab_cu_index,
20257 NULL, xcalloc, xfree);
20258 make_cleanup_htab_delete (cu_index_htab);
20259 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20260 xmalloc (sizeof (struct psymtab_cu_index_map)
20261 * dwarf2_per_objfile->n_comp_units);
20262 make_cleanup (xfree, psymtab_cu_index_map);
20263
20264 /* The CU list is already sorted, so we don't need to do additional
20265 work here. Also, the debug_types entries do not appear in
20266 all_comp_units, but only in their own hash table. */
20267 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20268 {
20269 struct dwarf2_per_cu_data *per_cu
20270 = dwarf2_per_objfile->all_comp_units[i];
20271 struct partial_symtab *psymtab = per_cu->v.psymtab;
20272 gdb_byte val[8];
20273 struct psymtab_cu_index_map *map;
20274 void **slot;
20275
20276 if (psymtab->user == NULL)
20277 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20278
20279 map = &psymtab_cu_index_map[i];
20280 map->psymtab = psymtab;
20281 map->cu_index = i;
20282 slot = htab_find_slot (cu_index_htab, map, INSERT);
20283 gdb_assert (slot != NULL);
20284 gdb_assert (*slot == NULL);
20285 *slot = map;
20286
20287 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20288 per_cu->offset.sect_off);
20289 obstack_grow (&cu_list, val, 8);
20290 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20291 obstack_grow (&cu_list, val, 8);
20292 }
20293
20294 /* Dump the address map. */
20295 write_address_map (objfile, &addr_obstack, cu_index_htab);
20296
20297 /* Write out the .debug_type entries, if any. */
20298 if (dwarf2_per_objfile->signatured_types)
20299 {
20300 struct signatured_type_index_data sig_data;
20301
20302 sig_data.objfile = objfile;
20303 sig_data.symtab = symtab;
20304 sig_data.types_list = &types_cu_list;
20305 sig_data.psyms_seen = psyms_seen;
20306 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20307 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20308 write_one_signatured_type, &sig_data);
20309 }
20310
20311 /* Now that we've processed all symbols we can shrink their cu_indices
20312 lists. */
20313 uniquify_cu_indices (symtab);
20314
20315 obstack_init (&constant_pool);
20316 make_cleanup_obstack_free (&constant_pool);
20317 obstack_init (&symtab_obstack);
20318 make_cleanup_obstack_free (&symtab_obstack);
20319 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20320
20321 obstack_init (&contents);
20322 make_cleanup_obstack_free (&contents);
20323 size_of_contents = 6 * sizeof (offset_type);
20324 total_len = size_of_contents;
20325
20326 /* The version number. */
20327 val = MAYBE_SWAP (7);
20328 obstack_grow (&contents, &val, sizeof (val));
20329
20330 /* The offset of the CU list from the start of the file. */
20331 val = MAYBE_SWAP (total_len);
20332 obstack_grow (&contents, &val, sizeof (val));
20333 total_len += obstack_object_size (&cu_list);
20334
20335 /* The offset of the types CU list from the start of the file. */
20336 val = MAYBE_SWAP (total_len);
20337 obstack_grow (&contents, &val, sizeof (val));
20338 total_len += obstack_object_size (&types_cu_list);
20339
20340 /* The offset of the address table from the start of the file. */
20341 val = MAYBE_SWAP (total_len);
20342 obstack_grow (&contents, &val, sizeof (val));
20343 total_len += obstack_object_size (&addr_obstack);
20344
20345 /* The offset of the symbol table from the start of the file. */
20346 val = MAYBE_SWAP (total_len);
20347 obstack_grow (&contents, &val, sizeof (val));
20348 total_len += obstack_object_size (&symtab_obstack);
20349
20350 /* The offset of the constant pool from the start of the file. */
20351 val = MAYBE_SWAP (total_len);
20352 obstack_grow (&contents, &val, sizeof (val));
20353 total_len += obstack_object_size (&constant_pool);
20354
20355 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20356
20357 write_obstack (out_file, &contents);
20358 write_obstack (out_file, &cu_list);
20359 write_obstack (out_file, &types_cu_list);
20360 write_obstack (out_file, &addr_obstack);
20361 write_obstack (out_file, &symtab_obstack);
20362 write_obstack (out_file, &constant_pool);
20363
20364 fclose (out_file);
20365
20366 /* We want to keep the file, so we set cleanup_filename to NULL
20367 here. See unlink_if_set. */
20368 cleanup_filename = NULL;
20369
20370 do_cleanups (cleanup);
20371 }
20372
20373 /* Implementation of the `save gdb-index' command.
20374
20375 Note that the file format used by this command is documented in the
20376 GDB manual. Any changes here must be documented there. */
20377
20378 static void
20379 save_gdb_index_command (char *arg, int from_tty)
20380 {
20381 struct objfile *objfile;
20382
20383 if (!arg || !*arg)
20384 error (_("usage: save gdb-index DIRECTORY"));
20385
20386 ALL_OBJFILES (objfile)
20387 {
20388 struct stat st;
20389
20390 /* If the objfile does not correspond to an actual file, skip it. */
20391 if (stat (objfile->name, &st) < 0)
20392 continue;
20393
20394 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20395 if (dwarf2_per_objfile)
20396 {
20397 volatile struct gdb_exception except;
20398
20399 TRY_CATCH (except, RETURN_MASK_ERROR)
20400 {
20401 write_psymtabs_to_index (objfile, arg);
20402 }
20403 if (except.reason < 0)
20404 exception_fprintf (gdb_stderr, except,
20405 _("Error while writing index for `%s': "),
20406 objfile->name);
20407 }
20408 }
20409 }
20410
20411 \f
20412
20413 int dwarf2_always_disassemble;
20414
20415 static void
20416 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20417 struct cmd_list_element *c, const char *value)
20418 {
20419 fprintf_filtered (file,
20420 _("Whether to always disassemble "
20421 "DWARF expressions is %s.\n"),
20422 value);
20423 }
20424
20425 static void
20426 show_check_physname (struct ui_file *file, int from_tty,
20427 struct cmd_list_element *c, const char *value)
20428 {
20429 fprintf_filtered (file,
20430 _("Whether to check \"physname\" is %s.\n"),
20431 value);
20432 }
20433
20434 void _initialize_dwarf2_read (void);
20435
20436 void
20437 _initialize_dwarf2_read (void)
20438 {
20439 struct cmd_list_element *c;
20440
20441 dwarf2_objfile_data_key
20442 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20443
20444 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20445 Set DWARF 2 specific variables.\n\
20446 Configure DWARF 2 variables such as the cache size"),
20447 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20448 0/*allow-unknown*/, &maintenance_set_cmdlist);
20449
20450 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20451 Show DWARF 2 specific variables\n\
20452 Show DWARF 2 variables such as the cache size"),
20453 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20454 0/*allow-unknown*/, &maintenance_show_cmdlist);
20455
20456 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20457 &dwarf2_max_cache_age, _("\
20458 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20459 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20460 A higher limit means that cached compilation units will be stored\n\
20461 in memory longer, and more total memory will be used. Zero disables\n\
20462 caching, which can slow down startup."),
20463 NULL,
20464 show_dwarf2_max_cache_age,
20465 &set_dwarf2_cmdlist,
20466 &show_dwarf2_cmdlist);
20467
20468 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20469 &dwarf2_always_disassemble, _("\
20470 Set whether `info address' always disassembles DWARF expressions."), _("\
20471 Show whether `info address' always disassembles DWARF expressions."), _("\
20472 When enabled, DWARF expressions are always printed in an assembly-like\n\
20473 syntax. When disabled, expressions will be printed in a more\n\
20474 conversational style, when possible."),
20475 NULL,
20476 show_dwarf2_always_disassemble,
20477 &set_dwarf2_cmdlist,
20478 &show_dwarf2_cmdlist);
20479
20480 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20481 Set debugging of the dwarf2 reader."), _("\
20482 Show debugging of the dwarf2 reader."), _("\
20483 When enabled, debugging messages are printed during dwarf2 reading\n\
20484 and symtab expansion."),
20485 NULL,
20486 NULL,
20487 &setdebuglist, &showdebuglist);
20488
20489 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20490 Set debugging of the dwarf2 DIE reader."), _("\
20491 Show debugging of the dwarf2 DIE reader."), _("\
20492 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20493 The value is the maximum depth to print."),
20494 NULL,
20495 NULL,
20496 &setdebuglist, &showdebuglist);
20497
20498 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20499 Set cross-checking of \"physname\" code against demangler."), _("\
20500 Show cross-checking of \"physname\" code against demangler."), _("\
20501 When enabled, GDB's internal \"physname\" code is checked against\n\
20502 the demangler."),
20503 NULL, show_check_physname,
20504 &setdebuglist, &showdebuglist);
20505
20506 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20507 no_class, &use_deprecated_index_sections, _("\
20508 Set whether to use deprecated gdb_index sections."), _("\
20509 Show whether to use deprecated gdb_index sections."), _("\
20510 When enabled, deprecated .gdb_index sections are used anyway.\n\
20511 Normally they are ignored either because of a missing feature or\n\
20512 performance issue.\n\
20513 Warning: This option must be enabled before gdb reads the file."),
20514 NULL,
20515 NULL,
20516 &setlist, &showlist);
20517
20518 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20519 _("\
20520 Save a gdb-index file.\n\
20521 Usage: save gdb-index DIRECTORY"),
20522 &save_cmdlist);
20523 set_cmd_completer (c, filename_completer);
20524 }
This page took 0.474712 seconds and 4 git commands to generate.