* dwarf2read.c (try_open_dwo_file): Use gdb_bfd_ref and
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67 #include "gdb_bfd.h"
68
69 #include <fcntl.h>
70 #include "gdb_string.h"
71 #include "gdb_assert.h"
72 #include <sys/types.h>
73 #ifdef HAVE_ZLIB_H
74 #include <zlib.h>
75 #endif
76 #ifdef HAVE_MMAP
77 #include <sys/mman.h>
78 #ifndef MAP_FAILED
79 #define MAP_FAILED ((void *) -1)
80 #endif
81 #endif
82
83 typedef struct symbol *symbolp;
84 DEF_VEC_P (symbolp);
85
86 /* When non-zero, print basic high level tracing messages.
87 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
88 static int dwarf2_read_debug = 0;
89
90 /* When non-zero, dump DIEs after they are read in. */
91 static int dwarf2_die_debug = 0;
92
93 /* When non-zero, cross-check physname against demangler. */
94 static int check_physname = 0;
95
96 /* When non-zero, do not reject deprecated .gdb_index sections. */
97 int use_deprecated_index_sections = 0;
98
99 static int pagesize;
100
101 /* When set, the file that we're processing is known to have debugging
102 info for C++ namespaces. GCC 3.3.x did not produce this information,
103 but later versions do. */
104
105 static int processing_has_namespace_info;
106
107 static const struct objfile_data *dwarf2_objfile_data_key;
108
109 struct dwarf2_section_info
110 {
111 asection *asection;
112 gdb_byte *buffer;
113 bfd_size_type size;
114 /* Not NULL if the section was actually mmapped. */
115 void *map_addr;
116 /* Page aligned size of mmapped area. */
117 bfd_size_type map_len;
118 /* True if we have tried to read this section. */
119 int readin;
120 };
121
122 typedef struct dwarf2_section_info dwarf2_section_info_def;
123 DEF_VEC_O (dwarf2_section_info_def);
124
125 /* All offsets in the index are of this type. It must be
126 architecture-independent. */
127 typedef uint32_t offset_type;
128
129 DEF_VEC_I (offset_type);
130
131 /* Ensure only legit values are used. */
132 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
133 do { \
134 gdb_assert ((unsigned int) (value) <= 1); \
135 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
136 } while (0)
137
138 /* Ensure only legit values are used. */
139 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
140 do { \
141 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
142 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
143 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
144 } while (0)
145
146 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
147 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
148 do { \
149 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
150 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
151 } while (0)
152
153 /* A description of the mapped index. The file format is described in
154 a comment by the code that writes the index. */
155 struct mapped_index
156 {
157 /* Index data format version. */
158 int version;
159
160 /* The total length of the buffer. */
161 off_t total_size;
162
163 /* A pointer to the address table data. */
164 const gdb_byte *address_table;
165
166 /* Size of the address table data in bytes. */
167 offset_type address_table_size;
168
169 /* The symbol table, implemented as a hash table. */
170 const offset_type *symbol_table;
171
172 /* Size in slots, each slot is 2 offset_types. */
173 offset_type symbol_table_slots;
174
175 /* A pointer to the constant pool. */
176 const char *constant_pool;
177 };
178
179 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
180 DEF_VEC_P (dwarf2_per_cu_ptr);
181
182 /* Collection of data recorded per objfile.
183 This hangs off of dwarf2_objfile_data_key. */
184
185 struct dwarf2_per_objfile
186 {
187 struct dwarf2_section_info info;
188 struct dwarf2_section_info abbrev;
189 struct dwarf2_section_info line;
190 struct dwarf2_section_info loc;
191 struct dwarf2_section_info macinfo;
192 struct dwarf2_section_info macro;
193 struct dwarf2_section_info str;
194 struct dwarf2_section_info ranges;
195 struct dwarf2_section_info addr;
196 struct dwarf2_section_info frame;
197 struct dwarf2_section_info eh_frame;
198 struct dwarf2_section_info gdb_index;
199
200 VEC (dwarf2_section_info_def) *types;
201
202 /* Back link. */
203 struct objfile *objfile;
204
205 /* Table of all the compilation units. This is used to locate
206 the target compilation unit of a particular reference. */
207 struct dwarf2_per_cu_data **all_comp_units;
208
209 /* The number of compilation units in ALL_COMP_UNITS. */
210 int n_comp_units;
211
212 /* The number of .debug_types-related CUs. */
213 int n_type_units;
214
215 /* The .debug_types-related CUs (TUs). */
216 struct signatured_type **all_type_units;
217
218 /* The number of entries in all_type_unit_groups. */
219 int n_type_unit_groups;
220
221 /* Table of type unit groups.
222 This exists to make it easy to iterate over all CUs and TU groups. */
223 struct type_unit_group **all_type_unit_groups;
224
225 /* Table of struct type_unit_group objects.
226 The hash key is the DW_AT_stmt_list value. */
227 htab_t type_unit_groups;
228
229 /* A table mapping .debug_types signatures to its signatured_type entry.
230 This is NULL if the .debug_types section hasn't been read in yet. */
231 htab_t signatured_types;
232
233 /* Type unit statistics, to see how well the scaling improvements
234 are doing. */
235 struct tu_stats
236 {
237 int nr_uniq_abbrev_tables;
238 int nr_symtabs;
239 int nr_symtab_sharers;
240 int nr_stmt_less_type_units;
241 } tu_stats;
242
243 /* A chain of compilation units that are currently read in, so that
244 they can be freed later. */
245 struct dwarf2_per_cu_data *read_in_chain;
246
247 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
248 This is NULL if the table hasn't been allocated yet. */
249 htab_t dwo_files;
250
251 /* A flag indicating wether this objfile has a section loaded at a
252 VMA of 0. */
253 int has_section_at_zero;
254
255 /* True if we are using the mapped index,
256 or we are faking it for OBJF_READNOW's sake. */
257 unsigned char using_index;
258
259 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
260 struct mapped_index *index_table;
261
262 /* When using index_table, this keeps track of all quick_file_names entries.
263 TUs can share line table entries with CUs or other TUs, and there can be
264 a lot more TUs than unique line tables, so we maintain a separate table
265 of all line table entries to support the sharing. */
266 htab_t quick_file_names_table;
267
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
271
272 /* Table mapping type DIEs to their struct type *.
273 This is NULL if not allocated yet.
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
279 };
280
281 static struct dwarf2_per_objfile *dwarf2_per_objfile;
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
295 { ".debug_macro", ".zdebug_macro" },
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
299 { ".debug_addr", ".zdebug_addr" },
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
302 { ".gdb_index", ".zgdb_index" },
303 23
304 };
305
306 /* List of DWO sections. */
307
308 static const struct dwo_section_names
309 {
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
319 }
320 dwo_section_names =
321 {
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
326 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
327 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
328 { ".debug_str.dwo", ".zdebug_str.dwo" },
329 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
330 { ".debug_types.dwo", ".zdebug_types.dwo" },
331 };
332
333 /* local data types */
334
335 /* The data in a compilation unit header, after target2host
336 translation, looks like this. */
337 struct comp_unit_head
338 {
339 unsigned int length;
340 short version;
341 unsigned char addr_size;
342 unsigned char signed_addr_p;
343 sect_offset abbrev_offset;
344
345 /* Size of file offsets; either 4 or 8. */
346 unsigned int offset_size;
347
348 /* Size of the length field; either 4 or 12. */
349 unsigned int initial_length_size;
350
351 /* Offset to the first byte of this compilation unit header in the
352 .debug_info section, for resolving relative reference dies. */
353 sect_offset offset;
354
355 /* Offset to first die in this cu from the start of the cu.
356 This will be the first byte following the compilation unit header. */
357 cu_offset first_die_offset;
358 };
359
360 /* Type used for delaying computation of method physnames.
361 See comments for compute_delayed_physnames. */
362 struct delayed_method_info
363 {
364 /* The type to which the method is attached, i.e., its parent class. */
365 struct type *type;
366
367 /* The index of the method in the type's function fieldlists. */
368 int fnfield_index;
369
370 /* The index of the method in the fieldlist. */
371 int index;
372
373 /* The name of the DIE. */
374 const char *name;
375
376 /* The DIE associated with this method. */
377 struct die_info *die;
378 };
379
380 typedef struct delayed_method_info delayed_method_info;
381 DEF_VEC_O (delayed_method_info);
382
383 /* Internal state when decoding a particular compilation unit. */
384 struct dwarf2_cu
385 {
386 /* The objfile containing this compilation unit. */
387 struct objfile *objfile;
388
389 /* The header of the compilation unit. */
390 struct comp_unit_head header;
391
392 /* Base address of this compilation unit. */
393 CORE_ADDR base_address;
394
395 /* Non-zero if base_address has been set. */
396 int base_known;
397
398 /* The language we are debugging. */
399 enum language language;
400 const struct language_defn *language_defn;
401
402 const char *producer;
403
404 /* The generic symbol table building routines have separate lists for
405 file scope symbols and all all other scopes (local scopes). So
406 we need to select the right one to pass to add_symbol_to_list().
407 We do it by keeping a pointer to the correct list in list_in_scope.
408
409 FIXME: The original dwarf code just treated the file scope as the
410 first local scope, and all other local scopes as nested local
411 scopes, and worked fine. Check to see if we really need to
412 distinguish these in buildsym.c. */
413 struct pending **list_in_scope;
414
415 /* The abbrev table for this CU.
416 Normally this points to the abbrev table in the objfile.
417 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
418 struct abbrev_table *abbrev_table;
419
420 /* Hash table holding all the loaded partial DIEs
421 with partial_die->offset.SECT_OFF as hash. */
422 htab_t partial_dies;
423
424 /* Storage for things with the same lifetime as this read-in compilation
425 unit, including partial DIEs. */
426 struct obstack comp_unit_obstack;
427
428 /* When multiple dwarf2_cu structures are living in memory, this field
429 chains them all together, so that they can be released efficiently.
430 We will probably also want a generation counter so that most-recently-used
431 compilation units are cached... */
432 struct dwarf2_per_cu_data *read_in_chain;
433
434 /* Backchain to our per_cu entry if the tree has been built. */
435 struct dwarf2_per_cu_data *per_cu;
436
437 /* How many compilation units ago was this CU last referenced? */
438 int last_used;
439
440 /* A hash table of DIE cu_offset for following references with
441 die_info->offset.sect_off as hash. */
442 htab_t die_hash;
443
444 /* Full DIEs if read in. */
445 struct die_info *dies;
446
447 /* A set of pointers to dwarf2_per_cu_data objects for compilation
448 units referenced by this one. Only set during full symbol processing;
449 partial symbol tables do not have dependencies. */
450 htab_t dependencies;
451
452 /* Header data from the line table, during full symbol processing. */
453 struct line_header *line_header;
454
455 /* A list of methods which need to have physnames computed
456 after all type information has been read. */
457 VEC (delayed_method_info) *method_list;
458
459 /* To be copied to symtab->call_site_htab. */
460 htab_t call_site_htab;
461
462 /* Non-NULL if this CU came from a DWO file.
463 There is an invariant here that is important to remember:
464 Except for attributes copied from the top level DIE in the "main"
465 (or "stub") file in preparation for reading the DWO file
466 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
467 Either there isn't a DWO file (in which case this is NULL and the point
468 is moot), or there is and either we're not going to read it (in which
469 case this is NULL) or there is and we are reading it (in which case this
470 is non-NULL). */
471 struct dwo_unit *dwo_unit;
472
473 /* The DW_AT_addr_base attribute if present, zero otherwise
474 (zero is a valid value though).
475 Note this value comes from the stub CU/TU's DIE. */
476 ULONGEST addr_base;
477
478 /* The DW_AT_ranges_base attribute if present, zero otherwise
479 (zero is a valid value though).
480 Note this value comes from the stub CU/TU's DIE.
481 Also note that the value is zero in the non-DWO case so this value can
482 be used without needing to know whether DWO files are in use or not. */
483 ULONGEST ranges_base;
484
485 /* Mark used when releasing cached dies. */
486 unsigned int mark : 1;
487
488 /* This CU references .debug_loc. See the symtab->locations_valid field.
489 This test is imperfect as there may exist optimized debug code not using
490 any location list and still facing inlining issues if handled as
491 unoptimized code. For a future better test see GCC PR other/32998. */
492 unsigned int has_loclist : 1;
493
494 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
495 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
496 are valid. This information is cached because profiling CU expansion
497 showed excessive time spent in producer_is_gxx_lt_4_6. */
498 unsigned int checked_producer : 1;
499 unsigned int producer_is_gxx_lt_4_6 : 1;
500 unsigned int producer_is_icc : 1;
501 };
502
503 /* Persistent data held for a compilation unit, even when not
504 processing it. We put a pointer to this structure in the
505 read_symtab_private field of the psymtab. */
506
507 struct dwarf2_per_cu_data
508 {
509 /* The start offset and length of this compilation unit. 2**29-1
510 bytes should suffice to store the length of any compilation unit
511 - if it doesn't, GDB will fall over anyway.
512 NOTE: Unlike comp_unit_head.length, this length includes
513 initial_length_size.
514 If the DIE refers to a DWO file, this is always of the original die,
515 not the DWO file. */
516 sect_offset offset;
517 unsigned int length : 29;
518
519 /* Flag indicating this compilation unit will be read in before
520 any of the current compilation units are processed. */
521 unsigned int queued : 1;
522
523 /* This flag will be set when reading partial DIEs if we need to load
524 absolutely all DIEs for this compilation unit, instead of just the ones
525 we think are interesting. It gets set if we look for a DIE in the
526 hash table and don't find it. */
527 unsigned int load_all_dies : 1;
528
529 /* Non-zero if this CU is from .debug_types. */
530 unsigned int is_debug_types : 1;
531
532 /* The section this CU/TU lives in.
533 If the DIE refers to a DWO file, this is always the original die,
534 not the DWO file. */
535 struct dwarf2_section_info *info_or_types_section;
536
537 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
538 of the CU cache it gets reset to NULL again. */
539 struct dwarf2_cu *cu;
540
541 /* The corresponding objfile.
542 Normally we can get the objfile from dwarf2_per_objfile.
543 However we can enter this file with just a "per_cu" handle. */
544 struct objfile *objfile;
545
546 /* When using partial symbol tables, the 'psymtab' field is active.
547 Otherwise the 'quick' field is active. */
548 union
549 {
550 /* The partial symbol table associated with this compilation unit,
551 or NULL for unread partial units. */
552 struct partial_symtab *psymtab;
553
554 /* Data needed by the "quick" functions. */
555 struct dwarf2_per_cu_quick_data *quick;
556 } v;
557
558 union
559 {
560 /* The CUs we import using DW_TAG_imported_unit. This is filled in
561 while reading psymtabs, used to compute the psymtab dependencies,
562 and then cleared. Then it is filled in again while reading full
563 symbols, and only deleted when the objfile is destroyed. */
564 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
565
566 /* Type units are grouped by their DW_AT_stmt_list entry so that they
567 can share them. If this is a TU, this points to the containing
568 symtab. */
569 struct type_unit_group *type_unit_group;
570 } s;
571 };
572
573 /* Entry in the signatured_types hash table. */
574
575 struct signatured_type
576 {
577 /* The "per_cu" object of this type.
578 N.B.: This is the first member so that it's easy to convert pointers
579 between them. */
580 struct dwarf2_per_cu_data per_cu;
581
582 /* The type's signature. */
583 ULONGEST signature;
584
585 /* Offset in the TU of the type's DIE, as read from the TU header.
586 If the definition lives in a DWO file, this value is unusable. */
587 cu_offset type_offset_in_tu;
588
589 /* Offset in the section of the type's DIE.
590 If the definition lives in a DWO file, this is the offset in the
591 .debug_types.dwo section.
592 The value is zero until the actual value is known.
593 Zero is otherwise not a valid section offset. */
594 sect_offset type_offset_in_section;
595 };
596
597 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
598 This includes type_unit_group and quick_file_names. */
599
600 struct stmt_list_hash
601 {
602 /* The DWO unit this table is from or NULL if there is none. */
603 struct dwo_unit *dwo_unit;
604
605 /* Offset in .debug_line or .debug_line.dwo. */
606 sect_offset line_offset;
607 };
608
609 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
610 an object of this type. */
611
612 struct type_unit_group
613 {
614 /* dwarf2read.c's main "handle" on the symtab.
615 To simplify things we create an artificial CU that "includes" all the
616 type units using this stmt_list so that the rest of the code still has
617 a "per_cu" handle on the symtab.
618 This PER_CU is recognized by having no section. */
619 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
620 struct dwarf2_per_cu_data per_cu;
621
622 union
623 {
624 /* The TUs that share this DW_AT_stmt_list entry.
625 This is added to while parsing type units to build partial symtabs,
626 and is deleted afterwards and not used again. */
627 VEC (dwarf2_per_cu_ptr) *tus;
628
629 /* When reading the line table in "quick" functions, we need a real TU.
630 Any will do, we know they all share the same DW_AT_stmt_list entry.
631 For simplicity's sake, we pick the first one. */
632 struct dwarf2_per_cu_data *first_tu;
633 } t;
634
635 /* The primary symtab.
636 Type units in a group needn't all be defined in the same source file,
637 so we create an essentially anonymous symtab as the primary symtab. */
638 struct symtab *primary_symtab;
639
640 /* The data used to construct the hash key. */
641 struct stmt_list_hash hash;
642
643 /* The number of symtabs from the line header.
644 The value here must match line_header.num_file_names. */
645 unsigned int num_symtabs;
646
647 /* The symbol tables for this TU (obtained from the files listed in
648 DW_AT_stmt_list).
649 WARNING: The order of entries here must match the order of entries
650 in the line header. After the first TU using this type_unit_group, the
651 line header for the subsequent TUs is recreated from this. This is done
652 because we need to use the same symtabs for each TU using the same
653 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
654 there's no guarantee the line header doesn't have duplicate entries. */
655 struct symtab **symtabs;
656 };
657
658 /* These sections are what may appear in a "dwo" file. */
659
660 struct dwo_sections
661 {
662 struct dwarf2_section_info abbrev;
663 struct dwarf2_section_info info;
664 struct dwarf2_section_info line;
665 struct dwarf2_section_info loc;
666 struct dwarf2_section_info macinfo;
667 struct dwarf2_section_info macro;
668 struct dwarf2_section_info str;
669 struct dwarf2_section_info str_offsets;
670 VEC (dwarf2_section_info_def) *types;
671 };
672
673 /* Common bits of DWO CUs/TUs. */
674
675 struct dwo_unit
676 {
677 /* Backlink to the containing struct dwo_file. */
678 struct dwo_file *dwo_file;
679
680 /* The "id" that distinguishes this CU/TU.
681 .debug_info calls this "dwo_id", .debug_types calls this "signature".
682 Since signatures came first, we stick with it for consistency. */
683 ULONGEST signature;
684
685 /* The section this CU/TU lives in, in the DWO file. */
686 struct dwarf2_section_info *info_or_types_section;
687
688 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
689 sect_offset offset;
690 unsigned int length;
691
692 /* For types, offset in the type's DIE of the type defined by this TU. */
693 cu_offset type_offset_in_tu;
694 };
695
696 /* Data for one DWO file. */
697
698 struct dwo_file
699 {
700 /* The DW_AT_GNU_dwo_name attribute.
701 We don't manage space for this, it's an attribute. */
702 const char *dwo_name;
703
704 /* The bfd, when the file is open. Otherwise this is NULL. */
705 bfd *dwo_bfd;
706
707 /* Section info for this file. */
708 struct dwo_sections sections;
709
710 /* Table of CUs in the file.
711 Each element is a struct dwo_unit. */
712 htab_t cus;
713
714 /* Table of TUs in the file.
715 Each element is a struct dwo_unit. */
716 htab_t tus;
717 };
718
719 /* Struct used to pass misc. parameters to read_die_and_children, et
720 al. which are used for both .debug_info and .debug_types dies.
721 All parameters here are unchanging for the life of the call. This
722 struct exists to abstract away the constant parameters of die reading. */
723
724 struct die_reader_specs
725 {
726 /* die_section->asection->owner. */
727 bfd* abfd;
728
729 /* The CU of the DIE we are parsing. */
730 struct dwarf2_cu *cu;
731
732 /* Non-NULL if reading a DWO file. */
733 struct dwo_file *dwo_file;
734
735 /* The section the die comes from.
736 This is either .debug_info or .debug_types, or the .dwo variants. */
737 struct dwarf2_section_info *die_section;
738
739 /* die_section->buffer. */
740 gdb_byte *buffer;
741
742 /* The end of the buffer. */
743 const gdb_byte *buffer_end;
744 };
745
746 /* Type of function passed to init_cutu_and_read_dies, et.al. */
747 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
748 gdb_byte *info_ptr,
749 struct die_info *comp_unit_die,
750 int has_children,
751 void *data);
752
753 /* The line number information for a compilation unit (found in the
754 .debug_line section) begins with a "statement program header",
755 which contains the following information. */
756 struct line_header
757 {
758 unsigned int total_length;
759 unsigned short version;
760 unsigned int header_length;
761 unsigned char minimum_instruction_length;
762 unsigned char maximum_ops_per_instruction;
763 unsigned char default_is_stmt;
764 int line_base;
765 unsigned char line_range;
766 unsigned char opcode_base;
767
768 /* standard_opcode_lengths[i] is the number of operands for the
769 standard opcode whose value is i. This means that
770 standard_opcode_lengths[0] is unused, and the last meaningful
771 element is standard_opcode_lengths[opcode_base - 1]. */
772 unsigned char *standard_opcode_lengths;
773
774 /* The include_directories table. NOTE! These strings are not
775 allocated with xmalloc; instead, they are pointers into
776 debug_line_buffer. If you try to free them, `free' will get
777 indigestion. */
778 unsigned int num_include_dirs, include_dirs_size;
779 char **include_dirs;
780
781 /* The file_names table. NOTE! These strings are not allocated
782 with xmalloc; instead, they are pointers into debug_line_buffer.
783 Don't try to free them directly. */
784 unsigned int num_file_names, file_names_size;
785 struct file_entry
786 {
787 char *name;
788 unsigned int dir_index;
789 unsigned int mod_time;
790 unsigned int length;
791 int included_p; /* Non-zero if referenced by the Line Number Program. */
792 struct symtab *symtab; /* The associated symbol table, if any. */
793 } *file_names;
794
795 /* The start and end of the statement program following this
796 header. These point into dwarf2_per_objfile->line_buffer. */
797 gdb_byte *statement_program_start, *statement_program_end;
798 };
799
800 /* When we construct a partial symbol table entry we only
801 need this much information. */
802 struct partial_die_info
803 {
804 /* Offset of this DIE. */
805 sect_offset offset;
806
807 /* DWARF-2 tag for this DIE. */
808 ENUM_BITFIELD(dwarf_tag) tag : 16;
809
810 /* Assorted flags describing the data found in this DIE. */
811 unsigned int has_children : 1;
812 unsigned int is_external : 1;
813 unsigned int is_declaration : 1;
814 unsigned int has_type : 1;
815 unsigned int has_specification : 1;
816 unsigned int has_pc_info : 1;
817 unsigned int may_be_inlined : 1;
818
819 /* Flag set if the SCOPE field of this structure has been
820 computed. */
821 unsigned int scope_set : 1;
822
823 /* Flag set if the DIE has a byte_size attribute. */
824 unsigned int has_byte_size : 1;
825
826 /* Flag set if any of the DIE's children are template arguments. */
827 unsigned int has_template_arguments : 1;
828
829 /* Flag set if fixup_partial_die has been called on this die. */
830 unsigned int fixup_called : 1;
831
832 /* The name of this DIE. Normally the value of DW_AT_name, but
833 sometimes a default name for unnamed DIEs. */
834 char *name;
835
836 /* The linkage name, if present. */
837 const char *linkage_name;
838
839 /* The scope to prepend to our children. This is generally
840 allocated on the comp_unit_obstack, so will disappear
841 when this compilation unit leaves the cache. */
842 char *scope;
843
844 /* Some data associated with the partial DIE. The tag determines
845 which field is live. */
846 union
847 {
848 /* The location description associated with this DIE, if any. */
849 struct dwarf_block *locdesc;
850 /* The offset of an import, for DW_TAG_imported_unit. */
851 sect_offset offset;
852 } d;
853
854 /* If HAS_PC_INFO, the PC range associated with this DIE. */
855 CORE_ADDR lowpc;
856 CORE_ADDR highpc;
857
858 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
859 DW_AT_sibling, if any. */
860 /* NOTE: This member isn't strictly necessary, read_partial_die could
861 return DW_AT_sibling values to its caller load_partial_dies. */
862 gdb_byte *sibling;
863
864 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
865 DW_AT_specification (or DW_AT_abstract_origin or
866 DW_AT_extension). */
867 sect_offset spec_offset;
868
869 /* Pointers to this DIE's parent, first child, and next sibling,
870 if any. */
871 struct partial_die_info *die_parent, *die_child, *die_sibling;
872 };
873
874 /* This data structure holds the information of an abbrev. */
875 struct abbrev_info
876 {
877 unsigned int number; /* number identifying abbrev */
878 enum dwarf_tag tag; /* dwarf tag */
879 unsigned short has_children; /* boolean */
880 unsigned short num_attrs; /* number of attributes */
881 struct attr_abbrev *attrs; /* an array of attribute descriptions */
882 struct abbrev_info *next; /* next in chain */
883 };
884
885 struct attr_abbrev
886 {
887 ENUM_BITFIELD(dwarf_attribute) name : 16;
888 ENUM_BITFIELD(dwarf_form) form : 16;
889 };
890
891 /* Size of abbrev_table.abbrev_hash_table. */
892 #define ABBREV_HASH_SIZE 121
893
894 /* Top level data structure to contain an abbreviation table. */
895
896 struct abbrev_table
897 {
898 /* Where the abbrev table came from.
899 This is used as a sanity check when the table is used. */
900 sect_offset offset;
901
902 /* Storage for the abbrev table. */
903 struct obstack abbrev_obstack;
904
905 /* Hash table of abbrevs.
906 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
907 It could be statically allocated, but the previous code didn't so we
908 don't either. */
909 struct abbrev_info **abbrevs;
910 };
911
912 /* Attributes have a name and a value. */
913 struct attribute
914 {
915 ENUM_BITFIELD(dwarf_attribute) name : 16;
916 ENUM_BITFIELD(dwarf_form) form : 15;
917
918 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
919 field should be in u.str (existing only for DW_STRING) but it is kept
920 here for better struct attribute alignment. */
921 unsigned int string_is_canonical : 1;
922
923 union
924 {
925 char *str;
926 struct dwarf_block *blk;
927 ULONGEST unsnd;
928 LONGEST snd;
929 CORE_ADDR addr;
930 struct signatured_type *signatured_type;
931 }
932 u;
933 };
934
935 /* This data structure holds a complete die structure. */
936 struct die_info
937 {
938 /* DWARF-2 tag for this DIE. */
939 ENUM_BITFIELD(dwarf_tag) tag : 16;
940
941 /* Number of attributes */
942 unsigned char num_attrs;
943
944 /* True if we're presently building the full type name for the
945 type derived from this DIE. */
946 unsigned char building_fullname : 1;
947
948 /* Abbrev number */
949 unsigned int abbrev;
950
951 /* Offset in .debug_info or .debug_types section. */
952 sect_offset offset;
953
954 /* The dies in a compilation unit form an n-ary tree. PARENT
955 points to this die's parent; CHILD points to the first child of
956 this node; and all the children of a given node are chained
957 together via their SIBLING fields. */
958 struct die_info *child; /* Its first child, if any. */
959 struct die_info *sibling; /* Its next sibling, if any. */
960 struct die_info *parent; /* Its parent, if any. */
961
962 /* An array of attributes, with NUM_ATTRS elements. There may be
963 zero, but it's not common and zero-sized arrays are not
964 sufficiently portable C. */
965 struct attribute attrs[1];
966 };
967
968 /* Get at parts of an attribute structure. */
969
970 #define DW_STRING(attr) ((attr)->u.str)
971 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
972 #define DW_UNSND(attr) ((attr)->u.unsnd)
973 #define DW_BLOCK(attr) ((attr)->u.blk)
974 #define DW_SND(attr) ((attr)->u.snd)
975 #define DW_ADDR(attr) ((attr)->u.addr)
976 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
977
978 /* Blocks are a bunch of untyped bytes. */
979 struct dwarf_block
980 {
981 unsigned int size;
982
983 /* Valid only if SIZE is not zero. */
984 gdb_byte *data;
985 };
986
987 #ifndef ATTR_ALLOC_CHUNK
988 #define ATTR_ALLOC_CHUNK 4
989 #endif
990
991 /* Allocate fields for structs, unions and enums in this size. */
992 #ifndef DW_FIELD_ALLOC_CHUNK
993 #define DW_FIELD_ALLOC_CHUNK 4
994 #endif
995
996 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
997 but this would require a corresponding change in unpack_field_as_long
998 and friends. */
999 static int bits_per_byte = 8;
1000
1001 /* The routines that read and process dies for a C struct or C++ class
1002 pass lists of data member fields and lists of member function fields
1003 in an instance of a field_info structure, as defined below. */
1004 struct field_info
1005 {
1006 /* List of data member and baseclasses fields. */
1007 struct nextfield
1008 {
1009 struct nextfield *next;
1010 int accessibility;
1011 int virtuality;
1012 struct field field;
1013 }
1014 *fields, *baseclasses;
1015
1016 /* Number of fields (including baseclasses). */
1017 int nfields;
1018
1019 /* Number of baseclasses. */
1020 int nbaseclasses;
1021
1022 /* Set if the accesibility of one of the fields is not public. */
1023 int non_public_fields;
1024
1025 /* Member function fields array, entries are allocated in the order they
1026 are encountered in the object file. */
1027 struct nextfnfield
1028 {
1029 struct nextfnfield *next;
1030 struct fn_field fnfield;
1031 }
1032 *fnfields;
1033
1034 /* Member function fieldlist array, contains name of possibly overloaded
1035 member function, number of overloaded member functions and a pointer
1036 to the head of the member function field chain. */
1037 struct fnfieldlist
1038 {
1039 char *name;
1040 int length;
1041 struct nextfnfield *head;
1042 }
1043 *fnfieldlists;
1044
1045 /* Number of entries in the fnfieldlists array. */
1046 int nfnfields;
1047
1048 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1049 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1050 struct typedef_field_list
1051 {
1052 struct typedef_field field;
1053 struct typedef_field_list *next;
1054 }
1055 *typedef_field_list;
1056 unsigned typedef_field_list_count;
1057 };
1058
1059 /* One item on the queue of compilation units to read in full symbols
1060 for. */
1061 struct dwarf2_queue_item
1062 {
1063 struct dwarf2_per_cu_data *per_cu;
1064 enum language pretend_language;
1065 struct dwarf2_queue_item *next;
1066 };
1067
1068 /* The current queue. */
1069 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1070
1071 /* Loaded secondary compilation units are kept in memory until they
1072 have not been referenced for the processing of this many
1073 compilation units. Set this to zero to disable caching. Cache
1074 sizes of up to at least twenty will improve startup time for
1075 typical inter-CU-reference binaries, at an obvious memory cost. */
1076 static int dwarf2_max_cache_age = 5;
1077 static void
1078 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1079 struct cmd_list_element *c, const char *value)
1080 {
1081 fprintf_filtered (file, _("The upper bound on the age of cached "
1082 "dwarf2 compilation units is %s.\n"),
1083 value);
1084 }
1085
1086
1087 /* Various complaints about symbol reading that don't abort the process. */
1088
1089 static void
1090 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1091 {
1092 complaint (&symfile_complaints,
1093 _("statement list doesn't fit in .debug_line section"));
1094 }
1095
1096 static void
1097 dwarf2_debug_line_missing_file_complaint (void)
1098 {
1099 complaint (&symfile_complaints,
1100 _(".debug_line section has line data without a file"));
1101 }
1102
1103 static void
1104 dwarf2_debug_line_missing_end_sequence_complaint (void)
1105 {
1106 complaint (&symfile_complaints,
1107 _(".debug_line section has line "
1108 "program sequence without an end"));
1109 }
1110
1111 static void
1112 dwarf2_complex_location_expr_complaint (void)
1113 {
1114 complaint (&symfile_complaints, _("location expression too complex"));
1115 }
1116
1117 static void
1118 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1119 int arg3)
1120 {
1121 complaint (&symfile_complaints,
1122 _("const value length mismatch for '%s', got %d, expected %d"),
1123 arg1, arg2, arg3);
1124 }
1125
1126 static void
1127 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1128 {
1129 complaint (&symfile_complaints,
1130 _("debug info runs off end of %s section"
1131 " [in module %s]"),
1132 section->asection->name,
1133 bfd_get_filename (section->asection->owner));
1134 }
1135
1136 static void
1137 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1138 {
1139 complaint (&symfile_complaints,
1140 _("macro debug info contains a "
1141 "malformed macro definition:\n`%s'"),
1142 arg1);
1143 }
1144
1145 static void
1146 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1147 {
1148 complaint (&symfile_complaints,
1149 _("invalid attribute class or form for '%s' in '%s'"),
1150 arg1, arg2);
1151 }
1152
1153 /* local function prototypes */
1154
1155 static void dwarf2_locate_sections (bfd *, asection *, void *);
1156
1157 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1158 struct objfile *);
1159
1160 static void dwarf2_find_base_address (struct die_info *die,
1161 struct dwarf2_cu *cu);
1162
1163 static void dwarf2_build_psymtabs_hard (struct objfile *);
1164
1165 static void scan_partial_symbols (struct partial_die_info *,
1166 CORE_ADDR *, CORE_ADDR *,
1167 int, struct dwarf2_cu *);
1168
1169 static void add_partial_symbol (struct partial_die_info *,
1170 struct dwarf2_cu *);
1171
1172 static void add_partial_namespace (struct partial_die_info *pdi,
1173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1174 int need_pc, struct dwarf2_cu *cu);
1175
1176 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1177 CORE_ADDR *highpc, int need_pc,
1178 struct dwarf2_cu *cu);
1179
1180 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1181 struct dwarf2_cu *cu);
1182
1183 static void add_partial_subprogram (struct partial_die_info *pdi,
1184 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1185 int need_pc, struct dwarf2_cu *cu);
1186
1187 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1188
1189 static void psymtab_to_symtab_1 (struct partial_symtab *);
1190
1191 static struct abbrev_info *abbrev_table_lookup_abbrev
1192 (const struct abbrev_table *, unsigned int);
1193
1194 static struct abbrev_table *abbrev_table_read_table
1195 (struct dwarf2_section_info *, sect_offset);
1196
1197 static void abbrev_table_free (struct abbrev_table *);
1198
1199 static void abbrev_table_free_cleanup (void *);
1200
1201 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1202 struct dwarf2_section_info *);
1203
1204 static void dwarf2_free_abbrev_table (void *);
1205
1206 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1207
1208 static struct partial_die_info *load_partial_dies
1209 (const struct die_reader_specs *, gdb_byte *, int);
1210
1211 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1212 struct partial_die_info *,
1213 struct abbrev_info *,
1214 unsigned int,
1215 gdb_byte *);
1216
1217 static struct partial_die_info *find_partial_die (sect_offset,
1218 struct dwarf2_cu *);
1219
1220 static void fixup_partial_die (struct partial_die_info *,
1221 struct dwarf2_cu *);
1222
1223 static gdb_byte *read_attribute (const struct die_reader_specs *,
1224 struct attribute *, struct attr_abbrev *,
1225 gdb_byte *);
1226
1227 static unsigned int read_1_byte (bfd *, gdb_byte *);
1228
1229 static int read_1_signed_byte (bfd *, gdb_byte *);
1230
1231 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1232
1233 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1234
1235 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1236
1237 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1238 unsigned int *);
1239
1240 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1241
1242 static LONGEST read_checked_initial_length_and_offset
1243 (bfd *, gdb_byte *, const struct comp_unit_head *,
1244 unsigned int *, unsigned int *);
1245
1246 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1247 unsigned int *);
1248
1249 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1250
1251 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1252 sect_offset);
1253
1254 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1255
1256 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1257
1258 static char *read_indirect_string (bfd *, gdb_byte *,
1259 const struct comp_unit_head *,
1260 unsigned int *);
1261
1262 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1263
1264 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1265
1266 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1267 unsigned int *);
1268
1269 static char *read_str_index (const struct die_reader_specs *reader,
1270 struct dwarf2_cu *cu, ULONGEST str_index);
1271
1272 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1273
1274 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1275 struct dwarf2_cu *);
1276
1277 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1278 unsigned int);
1279
1280 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1281 struct dwarf2_cu *cu);
1282
1283 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1284
1285 static struct die_info *die_specification (struct die_info *die,
1286 struct dwarf2_cu **);
1287
1288 static void free_line_header (struct line_header *lh);
1289
1290 static void add_file_name (struct line_header *, char *, unsigned int,
1291 unsigned int, unsigned int);
1292
1293 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1294 struct dwarf2_cu *cu);
1295
1296 static void dwarf_decode_lines (struct line_header *, const char *,
1297 struct dwarf2_cu *, struct partial_symtab *,
1298 int);
1299
1300 static void dwarf2_start_subfile (char *, const char *, const char *);
1301
1302 static void dwarf2_start_symtab (struct dwarf2_cu *,
1303 char *, char *, CORE_ADDR);
1304
1305 static struct symbol *new_symbol (struct die_info *, struct type *,
1306 struct dwarf2_cu *);
1307
1308 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1309 struct dwarf2_cu *, struct symbol *);
1310
1311 static void dwarf2_const_value (struct attribute *, struct symbol *,
1312 struct dwarf2_cu *);
1313
1314 static void dwarf2_const_value_attr (struct attribute *attr,
1315 struct type *type,
1316 const char *name,
1317 struct obstack *obstack,
1318 struct dwarf2_cu *cu, LONGEST *value,
1319 gdb_byte **bytes,
1320 struct dwarf2_locexpr_baton **baton);
1321
1322 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1323
1324 static int need_gnat_info (struct dwarf2_cu *);
1325
1326 static struct type *die_descriptive_type (struct die_info *,
1327 struct dwarf2_cu *);
1328
1329 static void set_descriptive_type (struct type *, struct die_info *,
1330 struct dwarf2_cu *);
1331
1332 static struct type *die_containing_type (struct die_info *,
1333 struct dwarf2_cu *);
1334
1335 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1336 struct dwarf2_cu *);
1337
1338 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1339
1340 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1341
1342 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1343
1344 static char *typename_concat (struct obstack *obs, const char *prefix,
1345 const char *suffix, int physname,
1346 struct dwarf2_cu *cu);
1347
1348 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1349
1350 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1351
1352 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1357
1358 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1359 struct dwarf2_cu *, struct partial_symtab *);
1360
1361 static int dwarf2_get_pc_bounds (struct die_info *,
1362 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1363 struct partial_symtab *);
1364
1365 static void get_scope_pc_bounds (struct die_info *,
1366 CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *);
1368
1369 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1370 CORE_ADDR, struct dwarf2_cu *);
1371
1372 static void dwarf2_add_field (struct field_info *, struct die_info *,
1373 struct dwarf2_cu *);
1374
1375 static void dwarf2_attach_fields_to_type (struct field_info *,
1376 struct type *, struct dwarf2_cu *);
1377
1378 static void dwarf2_add_member_fn (struct field_info *,
1379 struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1381
1382 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1383 struct type *,
1384 struct dwarf2_cu *);
1385
1386 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1387
1388 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1389
1390 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1391
1392 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1393
1394 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1395
1396 static struct type *read_module_type (struct die_info *die,
1397 struct dwarf2_cu *cu);
1398
1399 static const char *namespace_name (struct die_info *die,
1400 int *is_anonymous, struct dwarf2_cu *);
1401
1402 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1403
1404 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1405
1406 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1407 struct dwarf2_cu *);
1408
1409 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1410 gdb_byte *info_ptr,
1411 gdb_byte **new_info_ptr,
1412 struct die_info *parent);
1413
1414 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1415 gdb_byte *info_ptr,
1416 gdb_byte **new_info_ptr,
1417 struct die_info *parent);
1418
1419 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1420 struct die_info **, gdb_byte *, int *, int);
1421
1422 static gdb_byte *read_full_die (const struct die_reader_specs *,
1423 struct die_info **, gdb_byte *, int *);
1424
1425 static void process_die (struct die_info *, struct dwarf2_cu *);
1426
1427 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1428 struct obstack *);
1429
1430 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1431
1432 static const char *dwarf2_full_name (char *name,
1433 struct die_info *die,
1434 struct dwarf2_cu *cu);
1435
1436 static struct die_info *dwarf2_extension (struct die_info *die,
1437 struct dwarf2_cu **);
1438
1439 static const char *dwarf_tag_name (unsigned int);
1440
1441 static const char *dwarf_attr_name (unsigned int);
1442
1443 static const char *dwarf_form_name (unsigned int);
1444
1445 static char *dwarf_bool_name (unsigned int);
1446
1447 static const char *dwarf_type_encoding_name (unsigned int);
1448
1449 static struct die_info *sibling_die (struct die_info *);
1450
1451 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1452
1453 static void dump_die_for_error (struct die_info *);
1454
1455 static void dump_die_1 (struct ui_file *, int level, int max_level,
1456 struct die_info *);
1457
1458 /*static*/ void dump_die (struct die_info *, int max_level);
1459
1460 static void store_in_ref_table (struct die_info *,
1461 struct dwarf2_cu *);
1462
1463 static int is_ref_attr (struct attribute *);
1464
1465 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1466
1467 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1468
1469 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1470 struct attribute *,
1471 struct dwarf2_cu **);
1472
1473 static struct die_info *follow_die_ref (struct die_info *,
1474 struct attribute *,
1475 struct dwarf2_cu **);
1476
1477 static struct die_info *follow_die_sig (struct die_info *,
1478 struct attribute *,
1479 struct dwarf2_cu **);
1480
1481 static struct signatured_type *lookup_signatured_type_at_offset
1482 (struct objfile *objfile,
1483 struct dwarf2_section_info *section, sect_offset offset);
1484
1485 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1486
1487 static void read_signatured_type (struct signatured_type *);
1488
1489 static struct type_unit_group *get_type_unit_group
1490 (struct dwarf2_cu *, struct attribute *);
1491
1492 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1493
1494 /* memory allocation interface */
1495
1496 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1497
1498 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1499
1500 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1501 char *, int);
1502
1503 static int attr_form_is_block (struct attribute *);
1504
1505 static int attr_form_is_section_offset (struct attribute *);
1506
1507 static int attr_form_is_constant (struct attribute *);
1508
1509 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1510 struct dwarf2_loclist_baton *baton,
1511 struct attribute *attr);
1512
1513 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1514 struct symbol *sym,
1515 struct dwarf2_cu *cu);
1516
1517 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1518 gdb_byte *info_ptr,
1519 struct abbrev_info *abbrev);
1520
1521 static void free_stack_comp_unit (void *);
1522
1523 static hashval_t partial_die_hash (const void *item);
1524
1525 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1526
1527 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1528 (sect_offset offset, struct objfile *objfile);
1529
1530 static void init_one_comp_unit (struct dwarf2_cu *cu,
1531 struct dwarf2_per_cu_data *per_cu);
1532
1533 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1534 struct die_info *comp_unit_die,
1535 enum language pretend_language);
1536
1537 static void free_heap_comp_unit (void *);
1538
1539 static void free_cached_comp_units (void *);
1540
1541 static void age_cached_comp_units (void);
1542
1543 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1544
1545 static struct type *set_die_type (struct die_info *, struct type *,
1546 struct dwarf2_cu *);
1547
1548 static void create_all_comp_units (struct objfile *);
1549
1550 static int create_all_type_units (struct objfile *);
1551
1552 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1553 enum language);
1554
1555 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1556 enum language);
1557
1558 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1559 enum language);
1560
1561 static void dwarf2_add_dependence (struct dwarf2_cu *,
1562 struct dwarf2_per_cu_data *);
1563
1564 static void dwarf2_mark (struct dwarf2_cu *);
1565
1566 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1567
1568 static struct type *get_die_type_at_offset (sect_offset,
1569 struct dwarf2_per_cu_data *per_cu);
1570
1571 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1572
1573 static void dwarf2_release_queue (void *dummy);
1574
1575 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1576 enum language pretend_language);
1577
1578 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1579 struct dwarf2_per_cu_data *per_cu,
1580 enum language pretend_language);
1581
1582 static void process_queue (void);
1583
1584 static void find_file_and_directory (struct die_info *die,
1585 struct dwarf2_cu *cu,
1586 char **name, char **comp_dir);
1587
1588 static char *file_full_name (int file, struct line_header *lh,
1589 const char *comp_dir);
1590
1591 static void init_cutu_and_read_dies
1592 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1593 int use_existing_cu, int keep,
1594 die_reader_func_ftype *die_reader_func, void *data);
1595
1596 static void init_cutu_and_read_dies_simple
1597 (struct dwarf2_per_cu_data *this_cu,
1598 die_reader_func_ftype *die_reader_func, void *data);
1599
1600 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1601
1602 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1603
1604 static struct dwo_unit *lookup_dwo_comp_unit
1605 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1606
1607 static struct dwo_unit *lookup_dwo_type_unit
1608 (struct signatured_type *, char *, const char *);
1609
1610 static void free_dwo_file_cleanup (void *);
1611
1612 static void munmap_section_buffer (struct dwarf2_section_info *);
1613
1614 static void process_cu_includes (void);
1615
1616 #if WORDS_BIGENDIAN
1617
1618 /* Convert VALUE between big- and little-endian. */
1619 static offset_type
1620 byte_swap (offset_type value)
1621 {
1622 offset_type result;
1623
1624 result = (value & 0xff) << 24;
1625 result |= (value & 0xff00) << 8;
1626 result |= (value & 0xff0000) >> 8;
1627 result |= (value & 0xff000000) >> 24;
1628 return result;
1629 }
1630
1631 #define MAYBE_SWAP(V) byte_swap (V)
1632
1633 #else
1634 #define MAYBE_SWAP(V) (V)
1635 #endif /* WORDS_BIGENDIAN */
1636
1637 /* The suffix for an index file. */
1638 #define INDEX_SUFFIX ".gdb-index"
1639
1640 static const char *dwarf2_physname (char *name, struct die_info *die,
1641 struct dwarf2_cu *cu);
1642
1643 /* Try to locate the sections we need for DWARF 2 debugging
1644 information and return true if we have enough to do something.
1645 NAMES points to the dwarf2 section names, or is NULL if the standard
1646 ELF names are used. */
1647
1648 int
1649 dwarf2_has_info (struct objfile *objfile,
1650 const struct dwarf2_debug_sections *names)
1651 {
1652 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1653 if (!dwarf2_per_objfile)
1654 {
1655 /* Initialize per-objfile state. */
1656 struct dwarf2_per_objfile *data
1657 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1658
1659 memset (data, 0, sizeof (*data));
1660 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1661 dwarf2_per_objfile = data;
1662
1663 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1664 (void *) names);
1665 dwarf2_per_objfile->objfile = objfile;
1666 }
1667 return (dwarf2_per_objfile->info.asection != NULL
1668 && dwarf2_per_objfile->abbrev.asection != NULL);
1669 }
1670
1671 /* When loading sections, we look either for uncompressed section or for
1672 compressed section names. */
1673
1674 static int
1675 section_is_p (const char *section_name,
1676 const struct dwarf2_section_names *names)
1677 {
1678 if (names->normal != NULL
1679 && strcmp (section_name, names->normal) == 0)
1680 return 1;
1681 if (names->compressed != NULL
1682 && strcmp (section_name, names->compressed) == 0)
1683 return 1;
1684 return 0;
1685 }
1686
1687 /* This function is mapped across the sections and remembers the
1688 offset and size of each of the debugging sections we are interested
1689 in. */
1690
1691 static void
1692 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1693 {
1694 const struct dwarf2_debug_sections *names;
1695
1696 if (vnames == NULL)
1697 names = &dwarf2_elf_names;
1698 else
1699 names = (const struct dwarf2_debug_sections *) vnames;
1700
1701 if (section_is_p (sectp->name, &names->info))
1702 {
1703 dwarf2_per_objfile->info.asection = sectp;
1704 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1705 }
1706 else if (section_is_p (sectp->name, &names->abbrev))
1707 {
1708 dwarf2_per_objfile->abbrev.asection = sectp;
1709 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1710 }
1711 else if (section_is_p (sectp->name, &names->line))
1712 {
1713 dwarf2_per_objfile->line.asection = sectp;
1714 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1715 }
1716 else if (section_is_p (sectp->name, &names->loc))
1717 {
1718 dwarf2_per_objfile->loc.asection = sectp;
1719 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1720 }
1721 else if (section_is_p (sectp->name, &names->macinfo))
1722 {
1723 dwarf2_per_objfile->macinfo.asection = sectp;
1724 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1725 }
1726 else if (section_is_p (sectp->name, &names->macro))
1727 {
1728 dwarf2_per_objfile->macro.asection = sectp;
1729 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1730 }
1731 else if (section_is_p (sectp->name, &names->str))
1732 {
1733 dwarf2_per_objfile->str.asection = sectp;
1734 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1735 }
1736 else if (section_is_p (sectp->name, &names->addr))
1737 {
1738 dwarf2_per_objfile->addr.asection = sectp;
1739 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1740 }
1741 else if (section_is_p (sectp->name, &names->frame))
1742 {
1743 dwarf2_per_objfile->frame.asection = sectp;
1744 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1745 }
1746 else if (section_is_p (sectp->name, &names->eh_frame))
1747 {
1748 flagword aflag = bfd_get_section_flags (abfd, sectp);
1749
1750 if (aflag & SEC_HAS_CONTENTS)
1751 {
1752 dwarf2_per_objfile->eh_frame.asection = sectp;
1753 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1754 }
1755 }
1756 else if (section_is_p (sectp->name, &names->ranges))
1757 {
1758 dwarf2_per_objfile->ranges.asection = sectp;
1759 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1760 }
1761 else if (section_is_p (sectp->name, &names->types))
1762 {
1763 struct dwarf2_section_info type_section;
1764
1765 memset (&type_section, 0, sizeof (type_section));
1766 type_section.asection = sectp;
1767 type_section.size = bfd_get_section_size (sectp);
1768
1769 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1770 &type_section);
1771 }
1772 else if (section_is_p (sectp->name, &names->gdb_index))
1773 {
1774 dwarf2_per_objfile->gdb_index.asection = sectp;
1775 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1776 }
1777
1778 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1779 && bfd_section_vma (abfd, sectp) == 0)
1780 dwarf2_per_objfile->has_section_at_zero = 1;
1781 }
1782
1783 /* Decompress a section that was compressed using zlib. Store the
1784 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1785
1786 static void
1787 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1788 gdb_byte **outbuf, bfd_size_type *outsize)
1789 {
1790 bfd *abfd = sectp->owner;
1791 #ifndef HAVE_ZLIB_H
1792 error (_("Support for zlib-compressed DWARF data (from '%s') "
1793 "is disabled in this copy of GDB"),
1794 bfd_get_filename (abfd));
1795 #else
1796 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1797 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1798 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1799 bfd_size_type uncompressed_size;
1800 gdb_byte *uncompressed_buffer;
1801 z_stream strm;
1802 int rc;
1803 int header_size = 12;
1804
1805 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1806 || bfd_bread (compressed_buffer,
1807 compressed_size, abfd) != compressed_size)
1808 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1809 bfd_get_filename (abfd));
1810
1811 /* Read the zlib header. In this case, it should be "ZLIB" followed
1812 by the uncompressed section size, 8 bytes in big-endian order. */
1813 if (compressed_size < header_size
1814 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1815 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1816 bfd_get_filename (abfd));
1817 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1818 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1819 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1820 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1821 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1822 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1823 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1824 uncompressed_size += compressed_buffer[11];
1825
1826 /* It is possible the section consists of several compressed
1827 buffers concatenated together, so we uncompress in a loop. */
1828 strm.zalloc = NULL;
1829 strm.zfree = NULL;
1830 strm.opaque = NULL;
1831 strm.avail_in = compressed_size - header_size;
1832 strm.next_in = (Bytef*) compressed_buffer + header_size;
1833 strm.avail_out = uncompressed_size;
1834 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1835 uncompressed_size);
1836 rc = inflateInit (&strm);
1837 while (strm.avail_in > 0)
1838 {
1839 if (rc != Z_OK)
1840 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1841 bfd_get_filename (abfd), rc);
1842 strm.next_out = ((Bytef*) uncompressed_buffer
1843 + (uncompressed_size - strm.avail_out));
1844 rc = inflate (&strm, Z_FINISH);
1845 if (rc != Z_STREAM_END)
1846 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1847 bfd_get_filename (abfd), rc);
1848 rc = inflateReset (&strm);
1849 }
1850 rc = inflateEnd (&strm);
1851 if (rc != Z_OK
1852 || strm.avail_out != 0)
1853 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1854 bfd_get_filename (abfd), rc);
1855
1856 do_cleanups (cleanup);
1857 *outbuf = uncompressed_buffer;
1858 *outsize = uncompressed_size;
1859 #endif
1860 }
1861
1862 /* A helper function that decides whether a section is empty,
1863 or not present. */
1864
1865 static int
1866 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1867 {
1868 return info->asection == NULL || info->size == 0;
1869 }
1870
1871 /* Read the contents of the section INFO.
1872 OBJFILE is the main object file, but not necessarily the file where
1873 the section comes from. E.g., for DWO files INFO->asection->owner
1874 is the bfd of the DWO file.
1875 If the section is compressed, uncompress it before returning. */
1876
1877 static void
1878 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1879 {
1880 asection *sectp = info->asection;
1881 bfd *abfd;
1882 gdb_byte *buf, *retbuf;
1883 unsigned char header[4];
1884
1885 if (info->readin)
1886 return;
1887 info->buffer = NULL;
1888 info->map_addr = NULL;
1889 info->readin = 1;
1890
1891 if (dwarf2_section_empty_p (info))
1892 return;
1893
1894 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1895 abfd = sectp->owner;
1896
1897 /* Check if the file has a 4-byte header indicating compression. */
1898 if (info->size > sizeof (header)
1899 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1900 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1901 {
1902 /* Upon decompression, update the buffer and its size. */
1903 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1904 {
1905 zlib_decompress_section (objfile, sectp, &info->buffer,
1906 &info->size);
1907 return;
1908 }
1909 }
1910
1911 #ifdef HAVE_MMAP
1912 if (pagesize == 0)
1913 pagesize = getpagesize ();
1914
1915 /* Only try to mmap sections which are large enough: we don't want to
1916 waste space due to fragmentation. Also, only try mmap for sections
1917 without relocations. */
1918
1919 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1920 {
1921 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1922 MAP_PRIVATE, sectp->filepos,
1923 &info->map_addr, &info->map_len);
1924
1925 if ((caddr_t)info->buffer != MAP_FAILED)
1926 {
1927 #if HAVE_POSIX_MADVISE
1928 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1929 #endif
1930 return;
1931 }
1932 }
1933 #endif
1934
1935 /* If we get here, we are a normal, not-compressed section. */
1936 info->buffer = buf
1937 = obstack_alloc (&objfile->objfile_obstack, info->size);
1938
1939 /* When debugging .o files, we may need to apply relocations; see
1940 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1941 We never compress sections in .o files, so we only need to
1942 try this when the section is not compressed. */
1943 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1944 if (retbuf != NULL)
1945 {
1946 info->buffer = retbuf;
1947 return;
1948 }
1949
1950 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1951 || bfd_bread (buf, info->size, abfd) != info->size)
1952 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1953 bfd_get_filename (abfd));
1954 }
1955
1956 /* A helper function that returns the size of a section in a safe way.
1957 If you are positive that the section has been read before using the
1958 size, then it is safe to refer to the dwarf2_section_info object's
1959 "size" field directly. In other cases, you must call this
1960 function, because for compressed sections the size field is not set
1961 correctly until the section has been read. */
1962
1963 static bfd_size_type
1964 dwarf2_section_size (struct objfile *objfile,
1965 struct dwarf2_section_info *info)
1966 {
1967 if (!info->readin)
1968 dwarf2_read_section (objfile, info);
1969 return info->size;
1970 }
1971
1972 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1973 SECTION_NAME. */
1974
1975 void
1976 dwarf2_get_section_info (struct objfile *objfile,
1977 enum dwarf2_section_enum sect,
1978 asection **sectp, gdb_byte **bufp,
1979 bfd_size_type *sizep)
1980 {
1981 struct dwarf2_per_objfile *data
1982 = objfile_data (objfile, dwarf2_objfile_data_key);
1983 struct dwarf2_section_info *info;
1984
1985 /* We may see an objfile without any DWARF, in which case we just
1986 return nothing. */
1987 if (data == NULL)
1988 {
1989 *sectp = NULL;
1990 *bufp = NULL;
1991 *sizep = 0;
1992 return;
1993 }
1994 switch (sect)
1995 {
1996 case DWARF2_DEBUG_FRAME:
1997 info = &data->frame;
1998 break;
1999 case DWARF2_EH_FRAME:
2000 info = &data->eh_frame;
2001 break;
2002 default:
2003 gdb_assert_not_reached ("unexpected section");
2004 }
2005
2006 dwarf2_read_section (objfile, info);
2007
2008 *sectp = info->asection;
2009 *bufp = info->buffer;
2010 *sizep = info->size;
2011 }
2012
2013 \f
2014 /* DWARF quick_symbols_functions support. */
2015
2016 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2017 unique line tables, so we maintain a separate table of all .debug_line
2018 derived entries to support the sharing.
2019 All the quick functions need is the list of file names. We discard the
2020 line_header when we're done and don't need to record it here. */
2021 struct quick_file_names
2022 {
2023 /* The data used to construct the hash key. */
2024 struct stmt_list_hash hash;
2025
2026 /* The number of entries in file_names, real_names. */
2027 unsigned int num_file_names;
2028
2029 /* The file names from the line table, after being run through
2030 file_full_name. */
2031 const char **file_names;
2032
2033 /* The file names from the line table after being run through
2034 gdb_realpath. These are computed lazily. */
2035 const char **real_names;
2036 };
2037
2038 /* When using the index (and thus not using psymtabs), each CU has an
2039 object of this type. This is used to hold information needed by
2040 the various "quick" methods. */
2041 struct dwarf2_per_cu_quick_data
2042 {
2043 /* The file table. This can be NULL if there was no file table
2044 or it's currently not read in.
2045 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2046 struct quick_file_names *file_names;
2047
2048 /* The corresponding symbol table. This is NULL if symbols for this
2049 CU have not yet been read. */
2050 struct symtab *symtab;
2051
2052 /* A temporary mark bit used when iterating over all CUs in
2053 expand_symtabs_matching. */
2054 unsigned int mark : 1;
2055
2056 /* True if we've tried to read the file table and found there isn't one.
2057 There will be no point in trying to read it again next time. */
2058 unsigned int no_file_data : 1;
2059 };
2060
2061 /* Utility hash function for a stmt_list_hash. */
2062
2063 static hashval_t
2064 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2065 {
2066 hashval_t v = 0;
2067
2068 if (stmt_list_hash->dwo_unit != NULL)
2069 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2070 v += stmt_list_hash->line_offset.sect_off;
2071 return v;
2072 }
2073
2074 /* Utility equality function for a stmt_list_hash. */
2075
2076 static int
2077 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2078 const struct stmt_list_hash *rhs)
2079 {
2080 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2081 return 0;
2082 if (lhs->dwo_unit != NULL
2083 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2084 return 0;
2085
2086 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2087 }
2088
2089 /* Hash function for a quick_file_names. */
2090
2091 static hashval_t
2092 hash_file_name_entry (const void *e)
2093 {
2094 const struct quick_file_names *file_data = e;
2095
2096 return hash_stmt_list_entry (&file_data->hash);
2097 }
2098
2099 /* Equality function for a quick_file_names. */
2100
2101 static int
2102 eq_file_name_entry (const void *a, const void *b)
2103 {
2104 const struct quick_file_names *ea = a;
2105 const struct quick_file_names *eb = b;
2106
2107 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2108 }
2109
2110 /* Delete function for a quick_file_names. */
2111
2112 static void
2113 delete_file_name_entry (void *e)
2114 {
2115 struct quick_file_names *file_data = e;
2116 int i;
2117
2118 for (i = 0; i < file_data->num_file_names; ++i)
2119 {
2120 xfree ((void*) file_data->file_names[i]);
2121 if (file_data->real_names)
2122 xfree ((void*) file_data->real_names[i]);
2123 }
2124
2125 /* The space for the struct itself lives on objfile_obstack,
2126 so we don't free it here. */
2127 }
2128
2129 /* Create a quick_file_names hash table. */
2130
2131 static htab_t
2132 create_quick_file_names_table (unsigned int nr_initial_entries)
2133 {
2134 return htab_create_alloc (nr_initial_entries,
2135 hash_file_name_entry, eq_file_name_entry,
2136 delete_file_name_entry, xcalloc, xfree);
2137 }
2138
2139 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2140 have to be created afterwards. You should call age_cached_comp_units after
2141 processing PER_CU->CU. dw2_setup must have been already called. */
2142
2143 static void
2144 load_cu (struct dwarf2_per_cu_data *per_cu)
2145 {
2146 if (per_cu->is_debug_types)
2147 load_full_type_unit (per_cu);
2148 else
2149 load_full_comp_unit (per_cu, language_minimal);
2150
2151 gdb_assert (per_cu->cu != NULL);
2152
2153 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2154 }
2155
2156 /* Read in the symbols for PER_CU. */
2157
2158 static void
2159 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2160 {
2161 struct cleanup *back_to;
2162
2163 /* Skip type_unit_groups, reading the type units they contain
2164 is handled elsewhere. */
2165 if (IS_TYPE_UNIT_GROUP (per_cu))
2166 return;
2167
2168 back_to = make_cleanup (dwarf2_release_queue, NULL);
2169
2170 if (dwarf2_per_objfile->using_index
2171 ? per_cu->v.quick->symtab == NULL
2172 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2173 {
2174 queue_comp_unit (per_cu, language_minimal);
2175 load_cu (per_cu);
2176 }
2177
2178 process_queue ();
2179
2180 /* Age the cache, releasing compilation units that have not
2181 been used recently. */
2182 age_cached_comp_units ();
2183
2184 do_cleanups (back_to);
2185 }
2186
2187 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2188 the objfile from which this CU came. Returns the resulting symbol
2189 table. */
2190
2191 static struct symtab *
2192 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2193 {
2194 gdb_assert (dwarf2_per_objfile->using_index);
2195 if (!per_cu->v.quick->symtab)
2196 {
2197 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2198 increment_reading_symtab ();
2199 dw2_do_instantiate_symtab (per_cu);
2200 process_cu_includes ();
2201 do_cleanups (back_to);
2202 }
2203 return per_cu->v.quick->symtab;
2204 }
2205
2206 /* Return the CU given its index.
2207
2208 This is intended for loops like:
2209
2210 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2211 + dwarf2_per_objfile->n_type_units); ++i)
2212 {
2213 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2214
2215 ...;
2216 }
2217 */
2218
2219 static struct dwarf2_per_cu_data *
2220 dw2_get_cu (int index)
2221 {
2222 if (index >= dwarf2_per_objfile->n_comp_units)
2223 {
2224 index -= dwarf2_per_objfile->n_comp_units;
2225 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2226 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2227 }
2228
2229 return dwarf2_per_objfile->all_comp_units[index];
2230 }
2231
2232 /* Return the primary CU given its index.
2233 The difference between this function and dw2_get_cu is in the handling
2234 of type units (TUs). Here we return the type_unit_group object.
2235
2236 This is intended for loops like:
2237
2238 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2239 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2240 {
2241 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2242
2243 ...;
2244 }
2245 */
2246
2247 static struct dwarf2_per_cu_data *
2248 dw2_get_primary_cu (int index)
2249 {
2250 if (index >= dwarf2_per_objfile->n_comp_units)
2251 {
2252 index -= dwarf2_per_objfile->n_comp_units;
2253 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2254 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2255 }
2256
2257 return dwarf2_per_objfile->all_comp_units[index];
2258 }
2259
2260 /* A helper function that knows how to read a 64-bit value in a way
2261 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2262 otherwise. */
2263
2264 static int
2265 extract_cu_value (const char *bytes, ULONGEST *result)
2266 {
2267 if (sizeof (ULONGEST) < 8)
2268 {
2269 int i;
2270
2271 /* Ignore the upper 4 bytes if they are all zero. */
2272 for (i = 0; i < 4; ++i)
2273 if (bytes[i + 4] != 0)
2274 return 0;
2275
2276 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2277 }
2278 else
2279 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2280 return 1;
2281 }
2282
2283 /* Read the CU list from the mapped index, and use it to create all
2284 the CU objects for this objfile. Return 0 if something went wrong,
2285 1 if everything went ok. */
2286
2287 static int
2288 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2289 offset_type cu_list_elements)
2290 {
2291 offset_type i;
2292
2293 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2294 dwarf2_per_objfile->all_comp_units
2295 = obstack_alloc (&objfile->objfile_obstack,
2296 dwarf2_per_objfile->n_comp_units
2297 * sizeof (struct dwarf2_per_cu_data *));
2298
2299 for (i = 0; i < cu_list_elements; i += 2)
2300 {
2301 struct dwarf2_per_cu_data *the_cu;
2302 ULONGEST offset, length;
2303
2304 if (!extract_cu_value (cu_list, &offset)
2305 || !extract_cu_value (cu_list + 8, &length))
2306 return 0;
2307 cu_list += 2 * 8;
2308
2309 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2310 struct dwarf2_per_cu_data);
2311 the_cu->offset.sect_off = offset;
2312 the_cu->length = length;
2313 the_cu->objfile = objfile;
2314 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2315 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2316 struct dwarf2_per_cu_quick_data);
2317 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2318 }
2319
2320 return 1;
2321 }
2322
2323 /* Create the signatured type hash table from the index. */
2324
2325 static int
2326 create_signatured_type_table_from_index (struct objfile *objfile,
2327 struct dwarf2_section_info *section,
2328 const gdb_byte *bytes,
2329 offset_type elements)
2330 {
2331 offset_type i;
2332 htab_t sig_types_hash;
2333
2334 dwarf2_per_objfile->n_type_units = elements / 3;
2335 dwarf2_per_objfile->all_type_units
2336 = obstack_alloc (&objfile->objfile_obstack,
2337 dwarf2_per_objfile->n_type_units
2338 * sizeof (struct signatured_type *));
2339
2340 sig_types_hash = allocate_signatured_type_table (objfile);
2341
2342 for (i = 0; i < elements; i += 3)
2343 {
2344 struct signatured_type *sig_type;
2345 ULONGEST offset, type_offset_in_tu, signature;
2346 void **slot;
2347
2348 if (!extract_cu_value (bytes, &offset)
2349 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2350 return 0;
2351 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2352 bytes += 3 * 8;
2353
2354 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2355 struct signatured_type);
2356 sig_type->signature = signature;
2357 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2358 sig_type->per_cu.is_debug_types = 1;
2359 sig_type->per_cu.info_or_types_section = section;
2360 sig_type->per_cu.offset.sect_off = offset;
2361 sig_type->per_cu.objfile = objfile;
2362 sig_type->per_cu.v.quick
2363 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2364 struct dwarf2_per_cu_quick_data);
2365
2366 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2367 *slot = sig_type;
2368
2369 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2370 }
2371
2372 dwarf2_per_objfile->signatured_types = sig_types_hash;
2373
2374 return 1;
2375 }
2376
2377 /* Read the address map data from the mapped index, and use it to
2378 populate the objfile's psymtabs_addrmap. */
2379
2380 static void
2381 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2382 {
2383 const gdb_byte *iter, *end;
2384 struct obstack temp_obstack;
2385 struct addrmap *mutable_map;
2386 struct cleanup *cleanup;
2387 CORE_ADDR baseaddr;
2388
2389 obstack_init (&temp_obstack);
2390 cleanup = make_cleanup_obstack_free (&temp_obstack);
2391 mutable_map = addrmap_create_mutable (&temp_obstack);
2392
2393 iter = index->address_table;
2394 end = iter + index->address_table_size;
2395
2396 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2397
2398 while (iter < end)
2399 {
2400 ULONGEST hi, lo, cu_index;
2401 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2402 iter += 8;
2403 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2404 iter += 8;
2405 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2406 iter += 4;
2407
2408 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2409 dw2_get_cu (cu_index));
2410 }
2411
2412 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2413 &objfile->objfile_obstack);
2414 do_cleanups (cleanup);
2415 }
2416
2417 /* The hash function for strings in the mapped index. This is the same as
2418 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2419 implementation. This is necessary because the hash function is tied to the
2420 format of the mapped index file. The hash values do not have to match with
2421 SYMBOL_HASH_NEXT.
2422
2423 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2424
2425 static hashval_t
2426 mapped_index_string_hash (int index_version, const void *p)
2427 {
2428 const unsigned char *str = (const unsigned char *) p;
2429 hashval_t r = 0;
2430 unsigned char c;
2431
2432 while ((c = *str++) != 0)
2433 {
2434 if (index_version >= 5)
2435 c = tolower (c);
2436 r = r * 67 + c - 113;
2437 }
2438
2439 return r;
2440 }
2441
2442 /* Find a slot in the mapped index INDEX for the object named NAME.
2443 If NAME is found, set *VEC_OUT to point to the CU vector in the
2444 constant pool and return 1. If NAME cannot be found, return 0. */
2445
2446 static int
2447 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2448 offset_type **vec_out)
2449 {
2450 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2451 offset_type hash;
2452 offset_type slot, step;
2453 int (*cmp) (const char *, const char *);
2454
2455 if (current_language->la_language == language_cplus
2456 || current_language->la_language == language_java
2457 || current_language->la_language == language_fortran)
2458 {
2459 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2460 not contain any. */
2461 const char *paren = strchr (name, '(');
2462
2463 if (paren)
2464 {
2465 char *dup;
2466
2467 dup = xmalloc (paren - name + 1);
2468 memcpy (dup, name, paren - name);
2469 dup[paren - name] = 0;
2470
2471 make_cleanup (xfree, dup);
2472 name = dup;
2473 }
2474 }
2475
2476 /* Index version 4 did not support case insensitive searches. But the
2477 indices for case insensitive languages are built in lowercase, therefore
2478 simulate our NAME being searched is also lowercased. */
2479 hash = mapped_index_string_hash ((index->version == 4
2480 && case_sensitivity == case_sensitive_off
2481 ? 5 : index->version),
2482 name);
2483
2484 slot = hash & (index->symbol_table_slots - 1);
2485 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2486 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2487
2488 for (;;)
2489 {
2490 /* Convert a slot number to an offset into the table. */
2491 offset_type i = 2 * slot;
2492 const char *str;
2493 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2494 {
2495 do_cleanups (back_to);
2496 return 0;
2497 }
2498
2499 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2500 if (!cmp (name, str))
2501 {
2502 *vec_out = (offset_type *) (index->constant_pool
2503 + MAYBE_SWAP (index->symbol_table[i + 1]));
2504 do_cleanups (back_to);
2505 return 1;
2506 }
2507
2508 slot = (slot + step) & (index->symbol_table_slots - 1);
2509 }
2510 }
2511
2512 /* Read the index file. If everything went ok, initialize the "quick"
2513 elements of all the CUs and return 1. Otherwise, return 0. */
2514
2515 static int
2516 dwarf2_read_index (struct objfile *objfile)
2517 {
2518 char *addr;
2519 struct mapped_index *map;
2520 offset_type *metadata;
2521 const gdb_byte *cu_list;
2522 const gdb_byte *types_list = NULL;
2523 offset_type version, cu_list_elements;
2524 offset_type types_list_elements = 0;
2525 int i;
2526
2527 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2528 return 0;
2529
2530 /* Older elfutils strip versions could keep the section in the main
2531 executable while splitting it for the separate debug info file. */
2532 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2533 & SEC_HAS_CONTENTS) == 0)
2534 return 0;
2535
2536 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2537
2538 addr = dwarf2_per_objfile->gdb_index.buffer;
2539 /* Version check. */
2540 version = MAYBE_SWAP (*(offset_type *) addr);
2541 /* Versions earlier than 3 emitted every copy of a psymbol. This
2542 causes the index to behave very poorly for certain requests. Version 3
2543 contained incomplete addrmap. So, it seems better to just ignore such
2544 indices. */
2545 if (version < 4)
2546 {
2547 static int warning_printed = 0;
2548 if (!warning_printed)
2549 {
2550 warning (_("Skipping obsolete .gdb_index section in %s."),
2551 objfile->name);
2552 warning_printed = 1;
2553 }
2554 return 0;
2555 }
2556 /* Index version 4 uses a different hash function than index version
2557 5 and later.
2558
2559 Versions earlier than 6 did not emit psymbols for inlined
2560 functions. Using these files will cause GDB not to be able to
2561 set breakpoints on inlined functions by name, so we ignore these
2562 indices unless the --use-deprecated-index-sections command line
2563 option was supplied. */
2564 if (version < 6 && !use_deprecated_index_sections)
2565 {
2566 static int warning_printed = 0;
2567 if (!warning_printed)
2568 {
2569 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2570 "--use-deprecated-index-sections to use them anyway"),
2571 objfile->name);
2572 warning_printed = 1;
2573 }
2574 return 0;
2575 }
2576 /* Indexes with higher version than the one supported by GDB may be no
2577 longer backward compatible. */
2578 if (version > 7)
2579 return 0;
2580
2581 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2582 map->version = version;
2583 map->total_size = dwarf2_per_objfile->gdb_index.size;
2584
2585 metadata = (offset_type *) (addr + sizeof (offset_type));
2586
2587 i = 0;
2588 cu_list = addr + MAYBE_SWAP (metadata[i]);
2589 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2590 / 8);
2591 ++i;
2592
2593 types_list = addr + MAYBE_SWAP (metadata[i]);
2594 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2595 - MAYBE_SWAP (metadata[i]))
2596 / 8);
2597 ++i;
2598
2599 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2600 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2601 - MAYBE_SWAP (metadata[i]));
2602 ++i;
2603
2604 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2605 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2606 - MAYBE_SWAP (metadata[i]))
2607 / (2 * sizeof (offset_type)));
2608 ++i;
2609
2610 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2611
2612 /* Don't use the index if it's empty. */
2613 if (map->symbol_table_slots == 0)
2614 return 0;
2615
2616 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2617 return 0;
2618
2619 if (types_list_elements)
2620 {
2621 struct dwarf2_section_info *section;
2622
2623 /* We can only handle a single .debug_types when we have an
2624 index. */
2625 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2626 return 0;
2627
2628 section = VEC_index (dwarf2_section_info_def,
2629 dwarf2_per_objfile->types, 0);
2630
2631 if (!create_signatured_type_table_from_index (objfile, section,
2632 types_list,
2633 types_list_elements))
2634 return 0;
2635 }
2636
2637 create_addrmap_from_index (objfile, map);
2638
2639 dwarf2_per_objfile->index_table = map;
2640 dwarf2_per_objfile->using_index = 1;
2641 dwarf2_per_objfile->quick_file_names_table =
2642 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2643
2644 return 1;
2645 }
2646
2647 /* A helper for the "quick" functions which sets the global
2648 dwarf2_per_objfile according to OBJFILE. */
2649
2650 static void
2651 dw2_setup (struct objfile *objfile)
2652 {
2653 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2654 gdb_assert (dwarf2_per_objfile);
2655 }
2656
2657 /* Reader function for dw2_build_type_unit_groups. */
2658
2659 static void
2660 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2661 gdb_byte *info_ptr,
2662 struct die_info *type_unit_die,
2663 int has_children,
2664 void *data)
2665 {
2666 struct dwarf2_cu *cu = reader->cu;
2667 struct attribute *attr;
2668 struct type_unit_group *tu_group;
2669
2670 gdb_assert (data == NULL);
2671
2672 if (! has_children)
2673 return;
2674
2675 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2676 /* Call this for its side-effect of creating the associated
2677 struct type_unit_group if it doesn't already exist. */
2678 tu_group = get_type_unit_group (cu, attr);
2679 }
2680
2681 /* Build dwarf2_per_objfile->type_unit_groups.
2682 This function may be called multiple times. */
2683
2684 static void
2685 dw2_build_type_unit_groups (void)
2686 {
2687 if (dwarf2_per_objfile->type_unit_groups == NULL)
2688 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2689 }
2690
2691 /* die_reader_func for dw2_get_file_names. */
2692
2693 static void
2694 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2695 gdb_byte *info_ptr,
2696 struct die_info *comp_unit_die,
2697 int has_children,
2698 void *data)
2699 {
2700 struct dwarf2_cu *cu = reader->cu;
2701 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2702 struct objfile *objfile = dwarf2_per_objfile->objfile;
2703 struct dwarf2_per_cu_data *lh_cu;
2704 struct line_header *lh;
2705 struct attribute *attr;
2706 int i;
2707 char *name, *comp_dir;
2708 void **slot;
2709 struct quick_file_names *qfn;
2710 unsigned int line_offset;
2711
2712 /* Our callers never want to match partial units -- instead they
2713 will match the enclosing full CU. */
2714 if (comp_unit_die->tag == DW_TAG_partial_unit)
2715 {
2716 this_cu->v.quick->no_file_data = 1;
2717 return;
2718 }
2719
2720 /* If we're reading the line header for TUs, store it in the "per_cu"
2721 for tu_group. */
2722 if (this_cu->is_debug_types)
2723 {
2724 struct type_unit_group *tu_group = data;
2725
2726 gdb_assert (tu_group != NULL);
2727 lh_cu = &tu_group->per_cu;
2728 }
2729 else
2730 lh_cu = this_cu;
2731
2732 lh = NULL;
2733 slot = NULL;
2734 line_offset = 0;
2735
2736 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2737 if (attr)
2738 {
2739 struct quick_file_names find_entry;
2740
2741 line_offset = DW_UNSND (attr);
2742
2743 /* We may have already read in this line header (TU line header sharing).
2744 If we have we're done. */
2745 find_entry.hash.dwo_unit = cu->dwo_unit;
2746 find_entry.hash.line_offset.sect_off = line_offset;
2747 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2748 &find_entry, INSERT);
2749 if (*slot != NULL)
2750 {
2751 lh_cu->v.quick->file_names = *slot;
2752 return;
2753 }
2754
2755 lh = dwarf_decode_line_header (line_offset, cu);
2756 }
2757 if (lh == NULL)
2758 {
2759 lh_cu->v.quick->no_file_data = 1;
2760 return;
2761 }
2762
2763 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2764 qfn->hash.dwo_unit = cu->dwo_unit;
2765 qfn->hash.line_offset.sect_off = line_offset;
2766 gdb_assert (slot != NULL);
2767 *slot = qfn;
2768
2769 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2770
2771 qfn->num_file_names = lh->num_file_names;
2772 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2773 lh->num_file_names * sizeof (char *));
2774 for (i = 0; i < lh->num_file_names; ++i)
2775 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2776 qfn->real_names = NULL;
2777
2778 free_line_header (lh);
2779
2780 lh_cu->v.quick->file_names = qfn;
2781 }
2782
2783 /* A helper for the "quick" functions which attempts to read the line
2784 table for THIS_CU. */
2785
2786 static struct quick_file_names *
2787 dw2_get_file_names (struct objfile *objfile,
2788 struct dwarf2_per_cu_data *this_cu)
2789 {
2790 /* For TUs this should only be called on the parent group. */
2791 if (this_cu->is_debug_types)
2792 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2793
2794 if (this_cu->v.quick->file_names != NULL)
2795 return this_cu->v.quick->file_names;
2796 /* If we know there is no line data, no point in looking again. */
2797 if (this_cu->v.quick->no_file_data)
2798 return NULL;
2799
2800 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2801 in the stub for CUs, there's is no need to lookup the DWO file.
2802 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2803 DWO file. */
2804 if (this_cu->is_debug_types)
2805 {
2806 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2807
2808 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2809 dw2_get_file_names_reader, tu_group);
2810 }
2811 else
2812 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2813
2814 if (this_cu->v.quick->no_file_data)
2815 return NULL;
2816 return this_cu->v.quick->file_names;
2817 }
2818
2819 /* A helper for the "quick" functions which computes and caches the
2820 real path for a given file name from the line table. */
2821
2822 static const char *
2823 dw2_get_real_path (struct objfile *objfile,
2824 struct quick_file_names *qfn, int index)
2825 {
2826 if (qfn->real_names == NULL)
2827 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2828 qfn->num_file_names, sizeof (char *));
2829
2830 if (qfn->real_names[index] == NULL)
2831 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2832
2833 return qfn->real_names[index];
2834 }
2835
2836 static struct symtab *
2837 dw2_find_last_source_symtab (struct objfile *objfile)
2838 {
2839 int index;
2840
2841 dw2_setup (objfile);
2842 index = dwarf2_per_objfile->n_comp_units - 1;
2843 return dw2_instantiate_symtab (dw2_get_cu (index));
2844 }
2845
2846 /* Traversal function for dw2_forget_cached_source_info. */
2847
2848 static int
2849 dw2_free_cached_file_names (void **slot, void *info)
2850 {
2851 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2852
2853 if (file_data->real_names)
2854 {
2855 int i;
2856
2857 for (i = 0; i < file_data->num_file_names; ++i)
2858 {
2859 xfree ((void*) file_data->real_names[i]);
2860 file_data->real_names[i] = NULL;
2861 }
2862 }
2863
2864 return 1;
2865 }
2866
2867 static void
2868 dw2_forget_cached_source_info (struct objfile *objfile)
2869 {
2870 dw2_setup (objfile);
2871
2872 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2873 dw2_free_cached_file_names, NULL);
2874 }
2875
2876 /* Helper function for dw2_map_symtabs_matching_filename that expands
2877 the symtabs and calls the iterator. */
2878
2879 static int
2880 dw2_map_expand_apply (struct objfile *objfile,
2881 struct dwarf2_per_cu_data *per_cu,
2882 const char *name,
2883 const char *full_path, const char *real_path,
2884 int (*callback) (struct symtab *, void *),
2885 void *data)
2886 {
2887 struct symtab *last_made = objfile->symtabs;
2888
2889 /* Don't visit already-expanded CUs. */
2890 if (per_cu->v.quick->symtab)
2891 return 0;
2892
2893 /* This may expand more than one symtab, and we want to iterate over
2894 all of them. */
2895 dw2_instantiate_symtab (per_cu);
2896
2897 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2898 objfile->symtabs, last_made);
2899 }
2900
2901 /* Implementation of the map_symtabs_matching_filename method. */
2902
2903 static int
2904 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2905 const char *full_path, const char *real_path,
2906 int (*callback) (struct symtab *, void *),
2907 void *data)
2908 {
2909 int i;
2910 const char *name_basename = lbasename (name);
2911 int name_len = strlen (name);
2912 int is_abs = IS_ABSOLUTE_PATH (name);
2913
2914 dw2_setup (objfile);
2915
2916 dw2_build_type_unit_groups ();
2917
2918 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2919 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2920 {
2921 int j;
2922 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2923 struct quick_file_names *file_data;
2924
2925 /* We only need to look at symtabs not already expanded. */
2926 if (per_cu->v.quick->symtab)
2927 continue;
2928
2929 file_data = dw2_get_file_names (objfile, per_cu);
2930 if (file_data == NULL)
2931 continue;
2932
2933 for (j = 0; j < file_data->num_file_names; ++j)
2934 {
2935 const char *this_name = file_data->file_names[j];
2936
2937 if (FILENAME_CMP (name, this_name) == 0
2938 || (!is_abs && compare_filenames_for_search (this_name,
2939 name, name_len)))
2940 {
2941 if (dw2_map_expand_apply (objfile, per_cu,
2942 name, full_path, real_path,
2943 callback, data))
2944 return 1;
2945 }
2946
2947 /* Before we invoke realpath, which can get expensive when many
2948 files are involved, do a quick comparison of the basenames. */
2949 if (! basenames_may_differ
2950 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2951 continue;
2952
2953 if (full_path != NULL)
2954 {
2955 const char *this_real_name = dw2_get_real_path (objfile,
2956 file_data, j);
2957
2958 if (this_real_name != NULL
2959 && (FILENAME_CMP (full_path, this_real_name) == 0
2960 || (!is_abs
2961 && compare_filenames_for_search (this_real_name,
2962 name, name_len))))
2963 {
2964 if (dw2_map_expand_apply (objfile, per_cu,
2965 name, full_path, real_path,
2966 callback, data))
2967 return 1;
2968 }
2969 }
2970
2971 if (real_path != NULL)
2972 {
2973 const char *this_real_name = dw2_get_real_path (objfile,
2974 file_data, j);
2975
2976 if (this_real_name != NULL
2977 && (FILENAME_CMP (real_path, this_real_name) == 0
2978 || (!is_abs
2979 && compare_filenames_for_search (this_real_name,
2980 name, name_len))))
2981 {
2982 if (dw2_map_expand_apply (objfile, per_cu,
2983 name, full_path, real_path,
2984 callback, data))
2985 return 1;
2986 }
2987 }
2988 }
2989 }
2990
2991 return 0;
2992 }
2993
2994 static struct symtab *
2995 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2996 const char *name, domain_enum domain)
2997 {
2998 /* We do all the work in the pre_expand_symtabs_matching hook
2999 instead. */
3000 return NULL;
3001 }
3002
3003 /* A helper function that expands all symtabs that hold an object
3004 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3005 symbols in block BLOCK_KIND. */
3006
3007 static void
3008 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3009 int want_specific_block,
3010 enum block_enum block_kind,
3011 const char *name, domain_enum domain)
3012 {
3013 struct mapped_index *index;
3014
3015 dw2_setup (objfile);
3016
3017 index = dwarf2_per_objfile->index_table;
3018
3019 /* index_table is NULL if OBJF_READNOW. */
3020 if (index)
3021 {
3022 offset_type *vec;
3023
3024 if (find_slot_in_mapped_hash (index, name, &vec))
3025 {
3026 offset_type i, len = MAYBE_SWAP (*vec);
3027 for (i = 0; i < len; ++i)
3028 {
3029 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3030 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3031 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3032 int want_static = block_kind != GLOBAL_BLOCK;
3033 /* This value is only valid for index versions >= 7. */
3034 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3035 gdb_index_symbol_kind symbol_kind =
3036 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3037
3038 if (want_specific_block
3039 && index->version >= 7
3040 && want_static != is_static)
3041 continue;
3042
3043 /* Only check the symbol's kind if it has one.
3044 Indices prior to version 7 don't record it. */
3045 if (index->version >= 7)
3046 {
3047 switch (domain)
3048 {
3049 case VAR_DOMAIN:
3050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3051 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3052 /* Some types are also in VAR_DOMAIN. */
3053 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3054 continue;
3055 break;
3056 case STRUCT_DOMAIN:
3057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3058 continue;
3059 break;
3060 case LABEL_DOMAIN:
3061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3062 continue;
3063 break;
3064 default:
3065 break;
3066 }
3067 }
3068
3069 dw2_instantiate_symtab (per_cu);
3070 }
3071 }
3072 }
3073 }
3074
3075 static void
3076 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3077 enum block_enum block_kind, const char *name,
3078 domain_enum domain)
3079 {
3080 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3081 }
3082
3083 static void
3084 dw2_print_stats (struct objfile *objfile)
3085 {
3086 int i, count;
3087
3088 dw2_setup (objfile);
3089 count = 0;
3090 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3091 + dwarf2_per_objfile->n_type_units); ++i)
3092 {
3093 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3094
3095 if (!per_cu->v.quick->symtab)
3096 ++count;
3097 }
3098 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3099 }
3100
3101 static void
3102 dw2_dump (struct objfile *objfile)
3103 {
3104 /* Nothing worth printing. */
3105 }
3106
3107 static void
3108 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3109 struct section_offsets *delta)
3110 {
3111 /* There's nothing to relocate here. */
3112 }
3113
3114 static void
3115 dw2_expand_symtabs_for_function (struct objfile *objfile,
3116 const char *func_name)
3117 {
3118 /* Note: It doesn't matter what we pass for block_kind here. */
3119 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3120 VAR_DOMAIN);
3121 }
3122
3123 static void
3124 dw2_expand_all_symtabs (struct objfile *objfile)
3125 {
3126 int i;
3127
3128 dw2_setup (objfile);
3129
3130 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3131 + dwarf2_per_objfile->n_type_units); ++i)
3132 {
3133 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3134
3135 dw2_instantiate_symtab (per_cu);
3136 }
3137 }
3138
3139 static void
3140 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3141 const char *filename)
3142 {
3143 int i;
3144
3145 dw2_setup (objfile);
3146
3147 /* We don't need to consider type units here.
3148 This is only called for examining code, e.g. expand_line_sal.
3149 There can be an order of magnitude (or more) more type units
3150 than comp units, and we avoid them if we can. */
3151
3152 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3153 {
3154 int j;
3155 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3156 struct quick_file_names *file_data;
3157
3158 /* We only need to look at symtabs not already expanded. */
3159 if (per_cu->v.quick->symtab)
3160 continue;
3161
3162 file_data = dw2_get_file_names (objfile, per_cu);
3163 if (file_data == NULL)
3164 continue;
3165
3166 for (j = 0; j < file_data->num_file_names; ++j)
3167 {
3168 const char *this_name = file_data->file_names[j];
3169 if (FILENAME_CMP (this_name, filename) == 0)
3170 {
3171 dw2_instantiate_symtab (per_cu);
3172 break;
3173 }
3174 }
3175 }
3176 }
3177
3178 /* A helper function for dw2_find_symbol_file that finds the primary
3179 file name for a given CU. This is a die_reader_func. */
3180
3181 static void
3182 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3183 gdb_byte *info_ptr,
3184 struct die_info *comp_unit_die,
3185 int has_children,
3186 void *data)
3187 {
3188 const char **result_ptr = data;
3189 struct dwarf2_cu *cu = reader->cu;
3190 struct attribute *attr;
3191
3192 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3193 if (attr == NULL)
3194 *result_ptr = NULL;
3195 else
3196 *result_ptr = DW_STRING (attr);
3197 }
3198
3199 static const char *
3200 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3201 {
3202 struct dwarf2_per_cu_data *per_cu;
3203 offset_type *vec;
3204 struct quick_file_names *file_data;
3205 const char *filename;
3206
3207 dw2_setup (objfile);
3208
3209 /* index_table is NULL if OBJF_READNOW. */
3210 if (!dwarf2_per_objfile->index_table)
3211 {
3212 struct symtab *s;
3213
3214 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3215 {
3216 struct blockvector *bv = BLOCKVECTOR (s);
3217 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3218 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3219
3220 if (sym)
3221 return sym->symtab->filename;
3222 }
3223 return NULL;
3224 }
3225
3226 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3227 name, &vec))
3228 return NULL;
3229
3230 /* Note that this just looks at the very first one named NAME -- but
3231 actually we are looking for a function. find_main_filename
3232 should be rewritten so that it doesn't require a custom hook. It
3233 could just use the ordinary symbol tables. */
3234 /* vec[0] is the length, which must always be >0. */
3235 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3236
3237 if (per_cu->v.quick->symtab != NULL)
3238 return per_cu->v.quick->symtab->filename;
3239
3240 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3241 dw2_get_primary_filename_reader, &filename);
3242
3243 return filename;
3244 }
3245
3246 static void
3247 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3248 struct objfile *objfile, int global,
3249 int (*callback) (struct block *,
3250 struct symbol *, void *),
3251 void *data, symbol_compare_ftype *match,
3252 symbol_compare_ftype *ordered_compare)
3253 {
3254 /* Currently unimplemented; used for Ada. The function can be called if the
3255 current language is Ada for a non-Ada objfile using GNU index. As Ada
3256 does not look for non-Ada symbols this function should just return. */
3257 }
3258
3259 static void
3260 dw2_expand_symtabs_matching
3261 (struct objfile *objfile,
3262 int (*file_matcher) (const char *, void *),
3263 int (*name_matcher) (const char *, void *),
3264 enum search_domain kind,
3265 void *data)
3266 {
3267 int i;
3268 offset_type iter;
3269 struct mapped_index *index;
3270
3271 dw2_setup (objfile);
3272
3273 /* index_table is NULL if OBJF_READNOW. */
3274 if (!dwarf2_per_objfile->index_table)
3275 return;
3276 index = dwarf2_per_objfile->index_table;
3277
3278 if (file_matcher != NULL)
3279 {
3280 struct cleanup *cleanup;
3281 htab_t visited_found, visited_not_found;
3282
3283 dw2_build_type_unit_groups ();
3284
3285 visited_found = htab_create_alloc (10,
3286 htab_hash_pointer, htab_eq_pointer,
3287 NULL, xcalloc, xfree);
3288 cleanup = make_cleanup_htab_delete (visited_found);
3289 visited_not_found = htab_create_alloc (10,
3290 htab_hash_pointer, htab_eq_pointer,
3291 NULL, xcalloc, xfree);
3292 make_cleanup_htab_delete (visited_not_found);
3293
3294 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3295 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3296 {
3297 int j;
3298 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3299 struct quick_file_names *file_data;
3300 void **slot;
3301
3302 per_cu->v.quick->mark = 0;
3303
3304 /* We only need to look at symtabs not already expanded. */
3305 if (per_cu->v.quick->symtab)
3306 continue;
3307
3308 file_data = dw2_get_file_names (objfile, per_cu);
3309 if (file_data == NULL)
3310 continue;
3311
3312 if (htab_find (visited_not_found, file_data) != NULL)
3313 continue;
3314 else if (htab_find (visited_found, file_data) != NULL)
3315 {
3316 per_cu->v.quick->mark = 1;
3317 continue;
3318 }
3319
3320 for (j = 0; j < file_data->num_file_names; ++j)
3321 {
3322 if (file_matcher (file_data->file_names[j], data))
3323 {
3324 per_cu->v.quick->mark = 1;
3325 break;
3326 }
3327 }
3328
3329 slot = htab_find_slot (per_cu->v.quick->mark
3330 ? visited_found
3331 : visited_not_found,
3332 file_data, INSERT);
3333 *slot = file_data;
3334 }
3335
3336 do_cleanups (cleanup);
3337 }
3338
3339 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3340 {
3341 offset_type idx = 2 * iter;
3342 const char *name;
3343 offset_type *vec, vec_len, vec_idx;
3344
3345 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3346 continue;
3347
3348 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3349
3350 if (! (*name_matcher) (name, data))
3351 continue;
3352
3353 /* The name was matched, now expand corresponding CUs that were
3354 marked. */
3355 vec = (offset_type *) (index->constant_pool
3356 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3357 vec_len = MAYBE_SWAP (vec[0]);
3358 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3359 {
3360 struct dwarf2_per_cu_data *per_cu;
3361 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3362 gdb_index_symbol_kind symbol_kind =
3363 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3364 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3365
3366 /* Don't crash on bad data. */
3367 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3368 + dwarf2_per_objfile->n_type_units))
3369 continue;
3370
3371 /* Only check the symbol's kind if it has one.
3372 Indices prior to version 7 don't record it. */
3373 if (index->version >= 7)
3374 {
3375 switch (kind)
3376 {
3377 case VARIABLES_DOMAIN:
3378 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3379 continue;
3380 break;
3381 case FUNCTIONS_DOMAIN:
3382 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3383 continue;
3384 break;
3385 case TYPES_DOMAIN:
3386 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3387 continue;
3388 break;
3389 default:
3390 break;
3391 }
3392 }
3393
3394 per_cu = dw2_get_cu (cu_index);
3395 if (file_matcher == NULL || per_cu->v.quick->mark)
3396 dw2_instantiate_symtab (per_cu);
3397 }
3398 }
3399 }
3400
3401 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3402 symtab. */
3403
3404 static struct symtab *
3405 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3406 {
3407 int i;
3408
3409 if (BLOCKVECTOR (symtab) != NULL
3410 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3411 return symtab;
3412
3413 if (symtab->includes == NULL)
3414 return NULL;
3415
3416 for (i = 0; symtab->includes[i]; ++i)
3417 {
3418 struct symtab *s = symtab->includes[i];
3419
3420 s = recursively_find_pc_sect_symtab (s, pc);
3421 if (s != NULL)
3422 return s;
3423 }
3424
3425 return NULL;
3426 }
3427
3428 static struct symtab *
3429 dw2_find_pc_sect_symtab (struct objfile *objfile,
3430 struct minimal_symbol *msymbol,
3431 CORE_ADDR pc,
3432 struct obj_section *section,
3433 int warn_if_readin)
3434 {
3435 struct dwarf2_per_cu_data *data;
3436 struct symtab *result;
3437
3438 dw2_setup (objfile);
3439
3440 if (!objfile->psymtabs_addrmap)
3441 return NULL;
3442
3443 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3444 if (!data)
3445 return NULL;
3446
3447 if (warn_if_readin && data->v.quick->symtab)
3448 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3449 paddress (get_objfile_arch (objfile), pc));
3450
3451 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3452 gdb_assert (result != NULL);
3453 return result;
3454 }
3455
3456 static void
3457 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3458 void *data, int need_fullname)
3459 {
3460 int i;
3461 struct cleanup *cleanup;
3462 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3463 NULL, xcalloc, xfree);
3464
3465 cleanup = make_cleanup_htab_delete (visited);
3466 dw2_setup (objfile);
3467
3468 dw2_build_type_unit_groups ();
3469
3470 /* We can ignore file names coming from already-expanded CUs. */
3471 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3472 + dwarf2_per_objfile->n_type_units); ++i)
3473 {
3474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3475
3476 if (per_cu->v.quick->symtab)
3477 {
3478 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3479 INSERT);
3480
3481 *slot = per_cu->v.quick->file_names;
3482 }
3483 }
3484
3485 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3486 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3487 {
3488 int j;
3489 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3490 struct quick_file_names *file_data;
3491 void **slot;
3492
3493 /* We only need to look at symtabs not already expanded. */
3494 if (per_cu->v.quick->symtab)
3495 continue;
3496
3497 file_data = dw2_get_file_names (objfile, per_cu);
3498 if (file_data == NULL)
3499 continue;
3500
3501 slot = htab_find_slot (visited, file_data, INSERT);
3502 if (*slot)
3503 {
3504 /* Already visited. */
3505 continue;
3506 }
3507 *slot = file_data;
3508
3509 for (j = 0; j < file_data->num_file_names; ++j)
3510 {
3511 const char *this_real_name;
3512
3513 if (need_fullname)
3514 this_real_name = dw2_get_real_path (objfile, file_data, j);
3515 else
3516 this_real_name = NULL;
3517 (*fun) (file_data->file_names[j], this_real_name, data);
3518 }
3519 }
3520
3521 do_cleanups (cleanup);
3522 }
3523
3524 static int
3525 dw2_has_symbols (struct objfile *objfile)
3526 {
3527 return 1;
3528 }
3529
3530 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3531 {
3532 dw2_has_symbols,
3533 dw2_find_last_source_symtab,
3534 dw2_forget_cached_source_info,
3535 dw2_map_symtabs_matching_filename,
3536 dw2_lookup_symbol,
3537 dw2_pre_expand_symtabs_matching,
3538 dw2_print_stats,
3539 dw2_dump,
3540 dw2_relocate,
3541 dw2_expand_symtabs_for_function,
3542 dw2_expand_all_symtabs,
3543 dw2_expand_symtabs_with_filename,
3544 dw2_find_symbol_file,
3545 dw2_map_matching_symbols,
3546 dw2_expand_symtabs_matching,
3547 dw2_find_pc_sect_symtab,
3548 dw2_map_symbol_filenames
3549 };
3550
3551 /* Initialize for reading DWARF for this objfile. Return 0 if this
3552 file will use psymtabs, or 1 if using the GNU index. */
3553
3554 int
3555 dwarf2_initialize_objfile (struct objfile *objfile)
3556 {
3557 /* If we're about to read full symbols, don't bother with the
3558 indices. In this case we also don't care if some other debug
3559 format is making psymtabs, because they are all about to be
3560 expanded anyway. */
3561 if ((objfile->flags & OBJF_READNOW))
3562 {
3563 int i;
3564
3565 dwarf2_per_objfile->using_index = 1;
3566 create_all_comp_units (objfile);
3567 create_all_type_units (objfile);
3568 dwarf2_per_objfile->quick_file_names_table =
3569 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3570
3571 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3572 + dwarf2_per_objfile->n_type_units); ++i)
3573 {
3574 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3575
3576 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3577 struct dwarf2_per_cu_quick_data);
3578 }
3579
3580 /* Return 1 so that gdb sees the "quick" functions. However,
3581 these functions will be no-ops because we will have expanded
3582 all symtabs. */
3583 return 1;
3584 }
3585
3586 if (dwarf2_read_index (objfile))
3587 return 1;
3588
3589 return 0;
3590 }
3591
3592 \f
3593
3594 /* Build a partial symbol table. */
3595
3596 void
3597 dwarf2_build_psymtabs (struct objfile *objfile)
3598 {
3599 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3600 {
3601 init_psymbol_list (objfile, 1024);
3602 }
3603
3604 dwarf2_build_psymtabs_hard (objfile);
3605 }
3606
3607 /* Return the total length of the CU described by HEADER. */
3608
3609 static unsigned int
3610 get_cu_length (const struct comp_unit_head *header)
3611 {
3612 return header->initial_length_size + header->length;
3613 }
3614
3615 /* Return TRUE if OFFSET is within CU_HEADER. */
3616
3617 static inline int
3618 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3619 {
3620 sect_offset bottom = { cu_header->offset.sect_off };
3621 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3622
3623 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3624 }
3625
3626 /* Find the base address of the compilation unit for range lists and
3627 location lists. It will normally be specified by DW_AT_low_pc.
3628 In DWARF-3 draft 4, the base address could be overridden by
3629 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3630 compilation units with discontinuous ranges. */
3631
3632 static void
3633 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3634 {
3635 struct attribute *attr;
3636
3637 cu->base_known = 0;
3638 cu->base_address = 0;
3639
3640 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3641 if (attr)
3642 {
3643 cu->base_address = DW_ADDR (attr);
3644 cu->base_known = 1;
3645 }
3646 else
3647 {
3648 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3649 if (attr)
3650 {
3651 cu->base_address = DW_ADDR (attr);
3652 cu->base_known = 1;
3653 }
3654 }
3655 }
3656
3657 /* Read in the comp unit header information from the debug_info at info_ptr.
3658 NOTE: This leaves members offset, first_die_offset to be filled in
3659 by the caller. */
3660
3661 static gdb_byte *
3662 read_comp_unit_head (struct comp_unit_head *cu_header,
3663 gdb_byte *info_ptr, bfd *abfd)
3664 {
3665 int signed_addr;
3666 unsigned int bytes_read;
3667
3668 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3669 cu_header->initial_length_size = bytes_read;
3670 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3671 info_ptr += bytes_read;
3672 cu_header->version = read_2_bytes (abfd, info_ptr);
3673 info_ptr += 2;
3674 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3675 &bytes_read);
3676 info_ptr += bytes_read;
3677 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3678 info_ptr += 1;
3679 signed_addr = bfd_get_sign_extend_vma (abfd);
3680 if (signed_addr < 0)
3681 internal_error (__FILE__, __LINE__,
3682 _("read_comp_unit_head: dwarf from non elf file"));
3683 cu_header->signed_addr_p = signed_addr;
3684
3685 return info_ptr;
3686 }
3687
3688 /* Subroutine of read_and_check_comp_unit_head and
3689 read_and_check_type_unit_head to simplify them.
3690 Perform various error checking on the header. */
3691
3692 static void
3693 error_check_comp_unit_head (struct comp_unit_head *header,
3694 struct dwarf2_section_info *section,
3695 struct dwarf2_section_info *abbrev_section)
3696 {
3697 bfd *abfd = section->asection->owner;
3698 const char *filename = bfd_get_filename (abfd);
3699
3700 if (header->version != 2 && header->version != 3 && header->version != 4)
3701 error (_("Dwarf Error: wrong version in compilation unit header "
3702 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3703 filename);
3704
3705 if (header->abbrev_offset.sect_off
3706 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3707 &dwarf2_per_objfile->abbrev))
3708 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3709 "(offset 0x%lx + 6) [in module %s]"),
3710 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3711 filename);
3712
3713 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3714 avoid potential 32-bit overflow. */
3715 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3716 > section->size)
3717 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3718 "(offset 0x%lx + 0) [in module %s]"),
3719 (long) header->length, (long) header->offset.sect_off,
3720 filename);
3721 }
3722
3723 /* Read in a CU/TU header and perform some basic error checking.
3724 The contents of the header are stored in HEADER.
3725 The result is a pointer to the start of the first DIE. */
3726
3727 static gdb_byte *
3728 read_and_check_comp_unit_head (struct comp_unit_head *header,
3729 struct dwarf2_section_info *section,
3730 struct dwarf2_section_info *abbrev_section,
3731 gdb_byte *info_ptr,
3732 int is_debug_types_section)
3733 {
3734 gdb_byte *beg_of_comp_unit = info_ptr;
3735 bfd *abfd = section->asection->owner;
3736
3737 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3738
3739 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3740
3741 /* If we're reading a type unit, skip over the signature and
3742 type_offset fields. */
3743 if (is_debug_types_section)
3744 info_ptr += 8 /*signature*/ + header->offset_size;
3745
3746 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3747
3748 error_check_comp_unit_head (header, section, abbrev_section);
3749
3750 return info_ptr;
3751 }
3752
3753 /* Read in the types comp unit header information from .debug_types entry at
3754 types_ptr. The result is a pointer to one past the end of the header. */
3755
3756 static gdb_byte *
3757 read_and_check_type_unit_head (struct comp_unit_head *header,
3758 struct dwarf2_section_info *section,
3759 struct dwarf2_section_info *abbrev_section,
3760 gdb_byte *info_ptr,
3761 ULONGEST *signature,
3762 cu_offset *type_offset_in_tu)
3763 {
3764 gdb_byte *beg_of_comp_unit = info_ptr;
3765 bfd *abfd = section->asection->owner;
3766
3767 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3768
3769 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3770
3771 /* If we're reading a type unit, skip over the signature and
3772 type_offset fields. */
3773 if (signature != NULL)
3774 *signature = read_8_bytes (abfd, info_ptr);
3775 info_ptr += 8;
3776 if (type_offset_in_tu != NULL)
3777 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3778 header->offset_size);
3779 info_ptr += header->offset_size;
3780
3781 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3782
3783 error_check_comp_unit_head (header, section, abbrev_section);
3784
3785 return info_ptr;
3786 }
3787
3788 /* Fetch the abbreviation table offset from a comp or type unit header. */
3789
3790 static sect_offset
3791 read_abbrev_offset (struct dwarf2_section_info *section,
3792 sect_offset offset)
3793 {
3794 bfd *abfd = section->asection->owner;
3795 gdb_byte *info_ptr;
3796 unsigned int length, initial_length_size, offset_size;
3797 sect_offset abbrev_offset;
3798
3799 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3800 info_ptr = section->buffer + offset.sect_off;
3801 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3802 offset_size = initial_length_size == 4 ? 4 : 8;
3803 info_ptr += initial_length_size + 2 /*version*/;
3804 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3805 return abbrev_offset;
3806 }
3807
3808 /* Allocate a new partial symtab for file named NAME and mark this new
3809 partial symtab as being an include of PST. */
3810
3811 static void
3812 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3813 struct objfile *objfile)
3814 {
3815 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3816
3817 subpst->section_offsets = pst->section_offsets;
3818 subpst->textlow = 0;
3819 subpst->texthigh = 0;
3820
3821 subpst->dependencies = (struct partial_symtab **)
3822 obstack_alloc (&objfile->objfile_obstack,
3823 sizeof (struct partial_symtab *));
3824 subpst->dependencies[0] = pst;
3825 subpst->number_of_dependencies = 1;
3826
3827 subpst->globals_offset = 0;
3828 subpst->n_global_syms = 0;
3829 subpst->statics_offset = 0;
3830 subpst->n_static_syms = 0;
3831 subpst->symtab = NULL;
3832 subpst->read_symtab = pst->read_symtab;
3833 subpst->readin = 0;
3834
3835 /* No private part is necessary for include psymtabs. This property
3836 can be used to differentiate between such include psymtabs and
3837 the regular ones. */
3838 subpst->read_symtab_private = NULL;
3839 }
3840
3841 /* Read the Line Number Program data and extract the list of files
3842 included by the source file represented by PST. Build an include
3843 partial symtab for each of these included files. */
3844
3845 static void
3846 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3847 struct die_info *die,
3848 struct partial_symtab *pst)
3849 {
3850 struct line_header *lh = NULL;
3851 struct attribute *attr;
3852
3853 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3854 if (attr)
3855 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3856 if (lh == NULL)
3857 return; /* No linetable, so no includes. */
3858
3859 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3860 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3861
3862 free_line_header (lh);
3863 }
3864
3865 static hashval_t
3866 hash_signatured_type (const void *item)
3867 {
3868 const struct signatured_type *sig_type = item;
3869
3870 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3871 return sig_type->signature;
3872 }
3873
3874 static int
3875 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3876 {
3877 const struct signatured_type *lhs = item_lhs;
3878 const struct signatured_type *rhs = item_rhs;
3879
3880 return lhs->signature == rhs->signature;
3881 }
3882
3883 /* Allocate a hash table for signatured types. */
3884
3885 static htab_t
3886 allocate_signatured_type_table (struct objfile *objfile)
3887 {
3888 return htab_create_alloc_ex (41,
3889 hash_signatured_type,
3890 eq_signatured_type,
3891 NULL,
3892 &objfile->objfile_obstack,
3893 hashtab_obstack_allocate,
3894 dummy_obstack_deallocate);
3895 }
3896
3897 /* A helper function to add a signatured type CU to a table. */
3898
3899 static int
3900 add_signatured_type_cu_to_table (void **slot, void *datum)
3901 {
3902 struct signatured_type *sigt = *slot;
3903 struct signatured_type ***datap = datum;
3904
3905 **datap = sigt;
3906 ++*datap;
3907
3908 return 1;
3909 }
3910
3911 /* Create the hash table of all entries in the .debug_types section.
3912 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3913 The result is a pointer to the hash table or NULL if there are
3914 no types. */
3915
3916 static htab_t
3917 create_debug_types_hash_table (struct dwo_file *dwo_file,
3918 VEC (dwarf2_section_info_def) *types)
3919 {
3920 struct objfile *objfile = dwarf2_per_objfile->objfile;
3921 htab_t types_htab = NULL;
3922 int ix;
3923 struct dwarf2_section_info *section;
3924 struct dwarf2_section_info *abbrev_section;
3925
3926 if (VEC_empty (dwarf2_section_info_def, types))
3927 return NULL;
3928
3929 abbrev_section = (dwo_file != NULL
3930 ? &dwo_file->sections.abbrev
3931 : &dwarf2_per_objfile->abbrev);
3932
3933 if (dwarf2_read_debug)
3934 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
3935 dwo_file ? ".dwo" : "",
3936 bfd_get_filename (abbrev_section->asection->owner));
3937
3938 for (ix = 0;
3939 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3940 ++ix)
3941 {
3942 bfd *abfd;
3943 gdb_byte *info_ptr, *end_ptr;
3944
3945 dwarf2_read_section (objfile, section);
3946 info_ptr = section->buffer;
3947
3948 if (info_ptr == NULL)
3949 continue;
3950
3951 /* We can't set abfd until now because the section may be empty or
3952 not present, in which case section->asection will be NULL. */
3953 abfd = section->asection->owner;
3954
3955 if (types_htab == NULL)
3956 {
3957 if (dwo_file)
3958 types_htab = allocate_dwo_unit_table (objfile);
3959 else
3960 types_htab = allocate_signatured_type_table (objfile);
3961 }
3962
3963 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3964 because we don't need to read any dies: the signature is in the
3965 header. */
3966
3967 end_ptr = info_ptr + section->size;
3968 while (info_ptr < end_ptr)
3969 {
3970 sect_offset offset;
3971 cu_offset type_offset_in_tu;
3972 ULONGEST signature;
3973 struct signatured_type *sig_type;
3974 struct dwo_unit *dwo_tu;
3975 void **slot;
3976 gdb_byte *ptr = info_ptr;
3977 struct comp_unit_head header;
3978 unsigned int length;
3979
3980 offset.sect_off = ptr - section->buffer;
3981
3982 /* We need to read the type's signature in order to build the hash
3983 table, but we don't need anything else just yet. */
3984
3985 ptr = read_and_check_type_unit_head (&header, section,
3986 abbrev_section, ptr,
3987 &signature, &type_offset_in_tu);
3988
3989 length = get_cu_length (&header);
3990
3991 /* Skip dummy type units. */
3992 if (ptr >= info_ptr + length
3993 || peek_abbrev_code (abfd, ptr) == 0)
3994 {
3995 info_ptr += length;
3996 continue;
3997 }
3998
3999 if (dwo_file)
4000 {
4001 sig_type = NULL;
4002 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4003 struct dwo_unit);
4004 dwo_tu->dwo_file = dwo_file;
4005 dwo_tu->signature = signature;
4006 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4007 dwo_tu->info_or_types_section = section;
4008 dwo_tu->offset = offset;
4009 dwo_tu->length = length;
4010 }
4011 else
4012 {
4013 /* N.B.: type_offset is not usable if this type uses a DWO file.
4014 The real type_offset is in the DWO file. */
4015 dwo_tu = NULL;
4016 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4017 struct signatured_type);
4018 sig_type->signature = signature;
4019 sig_type->type_offset_in_tu = type_offset_in_tu;
4020 sig_type->per_cu.objfile = objfile;
4021 sig_type->per_cu.is_debug_types = 1;
4022 sig_type->per_cu.info_or_types_section = section;
4023 sig_type->per_cu.offset = offset;
4024 sig_type->per_cu.length = length;
4025 }
4026
4027 slot = htab_find_slot (types_htab,
4028 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4029 INSERT);
4030 gdb_assert (slot != NULL);
4031 if (*slot != NULL)
4032 {
4033 sect_offset dup_offset;
4034
4035 if (dwo_file)
4036 {
4037 const struct dwo_unit *dup_tu = *slot;
4038
4039 dup_offset = dup_tu->offset;
4040 }
4041 else
4042 {
4043 const struct signatured_type *dup_tu = *slot;
4044
4045 dup_offset = dup_tu->per_cu.offset;
4046 }
4047
4048 complaint (&symfile_complaints,
4049 _("debug type entry at offset 0x%x is duplicate to the "
4050 "entry at offset 0x%x, signature 0x%s"),
4051 offset.sect_off, dup_offset.sect_off,
4052 phex (signature, sizeof (signature)));
4053 }
4054 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4055
4056 if (dwarf2_read_debug)
4057 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4058 offset.sect_off,
4059 phex (signature, sizeof (signature)));
4060
4061 info_ptr += length;
4062 }
4063 }
4064
4065 return types_htab;
4066 }
4067
4068 /* Create the hash table of all entries in the .debug_types section,
4069 and initialize all_type_units.
4070 The result is zero if there is an error (e.g. missing .debug_types section),
4071 otherwise non-zero. */
4072
4073 static int
4074 create_all_type_units (struct objfile *objfile)
4075 {
4076 htab_t types_htab;
4077 struct signatured_type **iter;
4078
4079 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4080 if (types_htab == NULL)
4081 {
4082 dwarf2_per_objfile->signatured_types = NULL;
4083 return 0;
4084 }
4085
4086 dwarf2_per_objfile->signatured_types = types_htab;
4087
4088 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4089 dwarf2_per_objfile->all_type_units
4090 = obstack_alloc (&objfile->objfile_obstack,
4091 dwarf2_per_objfile->n_type_units
4092 * sizeof (struct signatured_type *));
4093 iter = &dwarf2_per_objfile->all_type_units[0];
4094 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4095 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4096 == dwarf2_per_objfile->n_type_units);
4097
4098 return 1;
4099 }
4100
4101 /* Lookup a signature based type for DW_FORM_ref_sig8.
4102 Returns NULL if signature SIG is not present in the table. */
4103
4104 static struct signatured_type *
4105 lookup_signatured_type (ULONGEST sig)
4106 {
4107 struct signatured_type find_entry, *entry;
4108
4109 if (dwarf2_per_objfile->signatured_types == NULL)
4110 {
4111 complaint (&symfile_complaints,
4112 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4113 return NULL;
4114 }
4115
4116 find_entry.signature = sig;
4117 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4118 return entry;
4119 }
4120 \f
4121 /* Low level DIE reading support. */
4122
4123 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4124
4125 static void
4126 init_cu_die_reader (struct die_reader_specs *reader,
4127 struct dwarf2_cu *cu,
4128 struct dwarf2_section_info *section,
4129 struct dwo_file *dwo_file)
4130 {
4131 gdb_assert (section->readin && section->buffer != NULL);
4132 reader->abfd = section->asection->owner;
4133 reader->cu = cu;
4134 reader->dwo_file = dwo_file;
4135 reader->die_section = section;
4136 reader->buffer = section->buffer;
4137 reader->buffer_end = section->buffer + section->size;
4138 }
4139
4140 /* Initialize a CU (or TU) and read its DIEs.
4141 If the CU defers to a DWO file, read the DWO file as well.
4142
4143 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4144 Otherwise the table specified in the comp unit header is read in and used.
4145 This is an optimization for when we already have the abbrev table.
4146
4147 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4148 Otherwise, a new CU is allocated with xmalloc.
4149
4150 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4151 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4152
4153 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4154 linker) then DIE_READER_FUNC will not get called. */
4155
4156 static void
4157 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4158 struct abbrev_table *abbrev_table,
4159 int use_existing_cu, int keep,
4160 die_reader_func_ftype *die_reader_func,
4161 void *data)
4162 {
4163 struct objfile *objfile = dwarf2_per_objfile->objfile;
4164 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4165 bfd *abfd = section->asection->owner;
4166 struct dwarf2_cu *cu;
4167 gdb_byte *begin_info_ptr, *info_ptr;
4168 struct die_reader_specs reader;
4169 struct die_info *comp_unit_die;
4170 int has_children;
4171 struct attribute *attr;
4172 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4173 struct signatured_type *sig_type = NULL;
4174 struct dwarf2_section_info *abbrev_section;
4175 /* Non-zero if CU currently points to a DWO file and we need to
4176 reread it. When this happens we need to reread the skeleton die
4177 before we can reread the DWO file. */
4178 int rereading_dwo_cu = 0;
4179
4180 if (dwarf2_die_debug)
4181 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4182 this_cu->is_debug_types ? "type" : "comp",
4183 this_cu->offset.sect_off);
4184
4185 if (use_existing_cu)
4186 gdb_assert (keep);
4187
4188 cleanups = make_cleanup (null_cleanup, NULL);
4189
4190 /* This is cheap if the section is already read in. */
4191 dwarf2_read_section (objfile, section);
4192
4193 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4194 abbrev_section = &dwarf2_per_objfile->abbrev;
4195
4196 if (use_existing_cu && this_cu->cu != NULL)
4197 {
4198 cu = this_cu->cu;
4199
4200 /* If this CU is from a DWO file we need to start over, we need to
4201 refetch the attributes from the skeleton CU.
4202 This could be optimized by retrieving those attributes from when we
4203 were here the first time: the previous comp_unit_die was stored in
4204 comp_unit_obstack. But there's no data yet that we need this
4205 optimization. */
4206 if (cu->dwo_unit != NULL)
4207 rereading_dwo_cu = 1;
4208 }
4209 else
4210 {
4211 /* If !use_existing_cu, this_cu->cu must be NULL. */
4212 gdb_assert (this_cu->cu == NULL);
4213
4214 cu = xmalloc (sizeof (*cu));
4215 init_one_comp_unit (cu, this_cu);
4216
4217 /* If an error occurs while loading, release our storage. */
4218 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4219 }
4220
4221 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4222 {
4223 /* We already have the header, there's no need to read it in again. */
4224 info_ptr += cu->header.first_die_offset.cu_off;
4225 }
4226 else
4227 {
4228 if (this_cu->is_debug_types)
4229 {
4230 ULONGEST signature;
4231 cu_offset type_offset_in_tu;
4232
4233 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4234 abbrev_section, info_ptr,
4235 &signature,
4236 &type_offset_in_tu);
4237
4238 /* Since per_cu is the first member of struct signatured_type,
4239 we can go from a pointer to one to a pointer to the other. */
4240 sig_type = (struct signatured_type *) this_cu;
4241 gdb_assert (sig_type->signature == signature);
4242 gdb_assert (sig_type->type_offset_in_tu.cu_off
4243 == type_offset_in_tu.cu_off);
4244 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4245
4246 /* LENGTH has not been set yet for type units if we're
4247 using .gdb_index. */
4248 this_cu->length = get_cu_length (&cu->header);
4249
4250 /* Establish the type offset that can be used to lookup the type. */
4251 sig_type->type_offset_in_section.sect_off =
4252 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4253 }
4254 else
4255 {
4256 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4257 abbrev_section,
4258 info_ptr, 0);
4259
4260 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4261 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4262 }
4263 }
4264
4265 /* Skip dummy compilation units. */
4266 if (info_ptr >= begin_info_ptr + this_cu->length
4267 || peek_abbrev_code (abfd, info_ptr) == 0)
4268 {
4269 do_cleanups (cleanups);
4270 return;
4271 }
4272
4273 /* If we don't have them yet, read the abbrevs for this compilation unit.
4274 And if we need to read them now, make sure they're freed when we're
4275 done. Note that it's important that if the CU had an abbrev table
4276 on entry we don't free it when we're done: Somewhere up the call stack
4277 it may be in use. */
4278 if (abbrev_table != NULL)
4279 {
4280 gdb_assert (cu->abbrev_table == NULL);
4281 gdb_assert (cu->header.abbrev_offset.sect_off
4282 == abbrev_table->offset.sect_off);
4283 cu->abbrev_table = abbrev_table;
4284 }
4285 else if (cu->abbrev_table == NULL)
4286 {
4287 dwarf2_read_abbrevs (cu, abbrev_section);
4288 make_cleanup (dwarf2_free_abbrev_table, cu);
4289 }
4290 else if (rereading_dwo_cu)
4291 {
4292 dwarf2_free_abbrev_table (cu);
4293 dwarf2_read_abbrevs (cu, abbrev_section);
4294 }
4295
4296 /* Read the top level CU/TU die. */
4297 init_cu_die_reader (&reader, cu, section, NULL);
4298 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4299
4300 /* If we have a DWO stub, process it and then read in the DWO file.
4301 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4302 a DWO CU, that this test will fail. */
4303 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4304 if (attr)
4305 {
4306 char *dwo_name = DW_STRING (attr);
4307 const char *comp_dir_string;
4308 struct dwo_unit *dwo_unit;
4309 ULONGEST signature; /* Or dwo_id. */
4310 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4311 int i,num_extra_attrs;
4312 struct dwarf2_section_info *dwo_abbrev_section;
4313
4314 if (has_children)
4315 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4316 " has children (offset 0x%x) [in module %s]"),
4317 this_cu->offset.sect_off, bfd_get_filename (abfd));
4318
4319 /* These attributes aren't processed until later:
4320 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4321 However, the attribute is found in the stub which we won't have later.
4322 In order to not impose this complication on the rest of the code,
4323 we read them here and copy them to the DWO CU/TU die. */
4324
4325 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4326 DWO file. */
4327 stmt_list = NULL;
4328 if (! this_cu->is_debug_types)
4329 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4330 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4331 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4332 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4333 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4334
4335 /* There should be a DW_AT_addr_base attribute here (if needed).
4336 We need the value before we can process DW_FORM_GNU_addr_index. */
4337 cu->addr_base = 0;
4338 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4339 if (attr)
4340 cu->addr_base = DW_UNSND (attr);
4341
4342 /* There should be a DW_AT_ranges_base attribute here (if needed).
4343 We need the value before we can process DW_AT_ranges. */
4344 cu->ranges_base = 0;
4345 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4346 if (attr)
4347 cu->ranges_base = DW_UNSND (attr);
4348
4349 if (this_cu->is_debug_types)
4350 {
4351 gdb_assert (sig_type != NULL);
4352 signature = sig_type->signature;
4353 }
4354 else
4355 {
4356 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4357 if (! attr)
4358 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4359 dwo_name);
4360 signature = DW_UNSND (attr);
4361 }
4362
4363 /* We may need the comp_dir in order to find the DWO file. */
4364 comp_dir_string = NULL;
4365 if (comp_dir)
4366 comp_dir_string = DW_STRING (comp_dir);
4367
4368 if (this_cu->is_debug_types)
4369 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4370 else
4371 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4372 signature);
4373
4374 if (dwo_unit == NULL)
4375 {
4376 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4377 " with ID %s [in module %s]"),
4378 this_cu->offset.sect_off,
4379 phex (signature, sizeof (signature)),
4380 objfile->name);
4381 }
4382
4383 /* Set up for reading the DWO CU/TU. */
4384 cu->dwo_unit = dwo_unit;
4385 section = dwo_unit->info_or_types_section;
4386 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4387 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4388 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4389
4390 if (this_cu->is_debug_types)
4391 {
4392 ULONGEST signature;
4393
4394 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4395 dwo_abbrev_section,
4396 info_ptr,
4397 &signature, NULL);
4398 gdb_assert (sig_type->signature == signature);
4399 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4400 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4401
4402 /* Establish the type offset that can be used to lookup the type.
4403 For DWO files, we don't know it until now. */
4404 sig_type->type_offset_in_section.sect_off =
4405 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4406 }
4407 else
4408 {
4409 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4410 dwo_abbrev_section,
4411 info_ptr, 0);
4412 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4413 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4414 }
4415
4416 /* Discard the original CU's abbrev table, and read the DWO's. */
4417 if (abbrev_table == NULL)
4418 {
4419 dwarf2_free_abbrev_table (cu);
4420 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4421 }
4422 else
4423 {
4424 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4425 make_cleanup (dwarf2_free_abbrev_table, cu);
4426 }
4427
4428 /* Read in the die, but leave space to copy over the attributes
4429 from the stub. This has the benefit of simplifying the rest of
4430 the code - all the real work is done here. */
4431 num_extra_attrs = ((stmt_list != NULL)
4432 + (low_pc != NULL)
4433 + (high_pc != NULL)
4434 + (ranges != NULL)
4435 + (comp_dir != NULL));
4436 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4437 &has_children, num_extra_attrs);
4438
4439 /* Copy over the attributes from the stub to the DWO die. */
4440 i = comp_unit_die->num_attrs;
4441 if (stmt_list != NULL)
4442 comp_unit_die->attrs[i++] = *stmt_list;
4443 if (low_pc != NULL)
4444 comp_unit_die->attrs[i++] = *low_pc;
4445 if (high_pc != NULL)
4446 comp_unit_die->attrs[i++] = *high_pc;
4447 if (ranges != NULL)
4448 comp_unit_die->attrs[i++] = *ranges;
4449 if (comp_dir != NULL)
4450 comp_unit_die->attrs[i++] = *comp_dir;
4451 comp_unit_die->num_attrs += num_extra_attrs;
4452
4453 /* Skip dummy compilation units. */
4454 if (info_ptr >= begin_info_ptr + dwo_unit->length
4455 || peek_abbrev_code (abfd, info_ptr) == 0)
4456 {
4457 do_cleanups (cleanups);
4458 return;
4459 }
4460 }
4461
4462 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4463
4464 if (free_cu_cleanup != NULL)
4465 {
4466 if (keep)
4467 {
4468 /* We've successfully allocated this compilation unit. Let our
4469 caller clean it up when finished with it. */
4470 discard_cleanups (free_cu_cleanup);
4471
4472 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4473 So we have to manually free the abbrev table. */
4474 dwarf2_free_abbrev_table (cu);
4475
4476 /* Link this CU into read_in_chain. */
4477 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4478 dwarf2_per_objfile->read_in_chain = this_cu;
4479 }
4480 else
4481 do_cleanups (free_cu_cleanup);
4482 }
4483
4484 do_cleanups (cleanups);
4485 }
4486
4487 /* Read CU/TU THIS_CU in section SECTION,
4488 but do not follow DW_AT_GNU_dwo_name if present.
4489 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4490 have already done the lookup to find the DWO file).
4491
4492 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4493 THIS_CU->is_debug_types, but nothing else.
4494
4495 We fill in THIS_CU->length.
4496
4497 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4498 linker) then DIE_READER_FUNC will not get called.
4499
4500 THIS_CU->cu is always freed when done.
4501 This is done in order to not leave THIS_CU->cu in a state where we have
4502 to care whether it refers to the "main" CU or the DWO CU. */
4503
4504 static void
4505 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4506 struct dwarf2_section_info *abbrev_section,
4507 struct dwo_file *dwo_file,
4508 die_reader_func_ftype *die_reader_func,
4509 void *data)
4510 {
4511 struct objfile *objfile = dwarf2_per_objfile->objfile;
4512 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4513 bfd *abfd = section->asection->owner;
4514 struct dwarf2_cu cu;
4515 gdb_byte *begin_info_ptr, *info_ptr;
4516 struct die_reader_specs reader;
4517 struct cleanup *cleanups;
4518 struct die_info *comp_unit_die;
4519 int has_children;
4520
4521 if (dwarf2_die_debug)
4522 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4523 this_cu->is_debug_types ? "type" : "comp",
4524 this_cu->offset.sect_off);
4525
4526 gdb_assert (this_cu->cu == NULL);
4527
4528 /* This is cheap if the section is already read in. */
4529 dwarf2_read_section (objfile, section);
4530
4531 init_one_comp_unit (&cu, this_cu);
4532
4533 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4534
4535 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4536 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4537 abbrev_section, info_ptr,
4538 this_cu->is_debug_types);
4539
4540 this_cu->length = get_cu_length (&cu.header);
4541
4542 /* Skip dummy compilation units. */
4543 if (info_ptr >= begin_info_ptr + this_cu->length
4544 || peek_abbrev_code (abfd, info_ptr) == 0)
4545 {
4546 do_cleanups (cleanups);
4547 return;
4548 }
4549
4550 dwarf2_read_abbrevs (&cu, abbrev_section);
4551 make_cleanup (dwarf2_free_abbrev_table, &cu);
4552
4553 init_cu_die_reader (&reader, &cu, section, dwo_file);
4554 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4555
4556 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4557
4558 do_cleanups (cleanups);
4559 }
4560
4561 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4562 does not lookup the specified DWO file.
4563 This cannot be used to read DWO files.
4564
4565 THIS_CU->cu is always freed when done.
4566 This is done in order to not leave THIS_CU->cu in a state where we have
4567 to care whether it refers to the "main" CU or the DWO CU.
4568 We can revisit this if the data shows there's a performance issue. */
4569
4570 static void
4571 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4572 die_reader_func_ftype *die_reader_func,
4573 void *data)
4574 {
4575 init_cutu_and_read_dies_no_follow (this_cu,
4576 &dwarf2_per_objfile->abbrev,
4577 NULL,
4578 die_reader_func, data);
4579 }
4580
4581 /* Create a psymtab named NAME and assign it to PER_CU.
4582
4583 The caller must fill in the following details:
4584 dirname, textlow, texthigh. */
4585
4586 static struct partial_symtab *
4587 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4588 {
4589 struct objfile *objfile = per_cu->objfile;
4590 struct partial_symtab *pst;
4591
4592 pst = start_psymtab_common (objfile, objfile->section_offsets,
4593 name, 0,
4594 objfile->global_psymbols.next,
4595 objfile->static_psymbols.next);
4596
4597 pst->psymtabs_addrmap_supported = 1;
4598
4599 /* This is the glue that links PST into GDB's symbol API. */
4600 pst->read_symtab_private = per_cu;
4601 pst->read_symtab = dwarf2_psymtab_to_symtab;
4602 per_cu->v.psymtab = pst;
4603
4604 return pst;
4605 }
4606
4607 /* die_reader_func for process_psymtab_comp_unit. */
4608
4609 static void
4610 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4611 gdb_byte *info_ptr,
4612 struct die_info *comp_unit_die,
4613 int has_children,
4614 void *data)
4615 {
4616 struct dwarf2_cu *cu = reader->cu;
4617 struct objfile *objfile = cu->objfile;
4618 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4619 struct attribute *attr;
4620 CORE_ADDR baseaddr;
4621 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4622 struct partial_symtab *pst;
4623 int has_pc_info;
4624 const char *filename;
4625 int *want_partial_unit_ptr = data;
4626
4627 if (comp_unit_die->tag == DW_TAG_partial_unit
4628 && (want_partial_unit_ptr == NULL
4629 || !*want_partial_unit_ptr))
4630 return;
4631
4632 gdb_assert (! per_cu->is_debug_types);
4633
4634 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4635
4636 cu->list_in_scope = &file_symbols;
4637
4638 /* Allocate a new partial symbol table structure. */
4639 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4640 if (attr == NULL || !DW_STRING (attr))
4641 filename = "";
4642 else
4643 filename = DW_STRING (attr);
4644
4645 pst = create_partial_symtab (per_cu, filename);
4646
4647 /* This must be done before calling dwarf2_build_include_psymtabs. */
4648 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4649 if (attr != NULL)
4650 pst->dirname = DW_STRING (attr);
4651
4652 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4653
4654 dwarf2_find_base_address (comp_unit_die, cu);
4655
4656 /* Possibly set the default values of LOWPC and HIGHPC from
4657 `DW_AT_ranges'. */
4658 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4659 &best_highpc, cu, pst);
4660 if (has_pc_info == 1 && best_lowpc < best_highpc)
4661 /* Store the contiguous range if it is not empty; it can be empty for
4662 CUs with no code. */
4663 addrmap_set_empty (objfile->psymtabs_addrmap,
4664 best_lowpc + baseaddr,
4665 best_highpc + baseaddr - 1, pst);
4666
4667 /* Check if comp unit has_children.
4668 If so, read the rest of the partial symbols from this comp unit.
4669 If not, there's no more debug_info for this comp unit. */
4670 if (has_children)
4671 {
4672 struct partial_die_info *first_die;
4673 CORE_ADDR lowpc, highpc;
4674
4675 lowpc = ((CORE_ADDR) -1);
4676 highpc = ((CORE_ADDR) 0);
4677
4678 first_die = load_partial_dies (reader, info_ptr, 1);
4679
4680 scan_partial_symbols (first_die, &lowpc, &highpc,
4681 ! has_pc_info, cu);
4682
4683 /* If we didn't find a lowpc, set it to highpc to avoid
4684 complaints from `maint check'. */
4685 if (lowpc == ((CORE_ADDR) -1))
4686 lowpc = highpc;
4687
4688 /* If the compilation unit didn't have an explicit address range,
4689 then use the information extracted from its child dies. */
4690 if (! has_pc_info)
4691 {
4692 best_lowpc = lowpc;
4693 best_highpc = highpc;
4694 }
4695 }
4696 pst->textlow = best_lowpc + baseaddr;
4697 pst->texthigh = best_highpc + baseaddr;
4698
4699 pst->n_global_syms = objfile->global_psymbols.next -
4700 (objfile->global_psymbols.list + pst->globals_offset);
4701 pst->n_static_syms = objfile->static_psymbols.next -
4702 (objfile->static_psymbols.list + pst->statics_offset);
4703 sort_pst_symbols (pst);
4704
4705 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4706 {
4707 int i;
4708 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4709 struct dwarf2_per_cu_data *iter;
4710
4711 /* Fill in 'dependencies' here; we fill in 'users' in a
4712 post-pass. */
4713 pst->number_of_dependencies = len;
4714 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4715 len * sizeof (struct symtab *));
4716 for (i = 0;
4717 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4718 i, iter);
4719 ++i)
4720 pst->dependencies[i] = iter->v.psymtab;
4721
4722 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4723 }
4724
4725 /* Get the list of files included in the current compilation unit,
4726 and build a psymtab for each of them. */
4727 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4728
4729 if (dwarf2_read_debug)
4730 {
4731 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4732
4733 fprintf_unfiltered (gdb_stdlog,
4734 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4735 ", %d global, %d static syms\n",
4736 per_cu->is_debug_types ? "type" : "comp",
4737 per_cu->offset.sect_off,
4738 paddress (gdbarch, pst->textlow),
4739 paddress (gdbarch, pst->texthigh),
4740 pst->n_global_syms, pst->n_static_syms);
4741 }
4742 }
4743
4744 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4745 Process compilation unit THIS_CU for a psymtab. */
4746
4747 static void
4748 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4749 int want_partial_unit)
4750 {
4751 /* If this compilation unit was already read in, free the
4752 cached copy in order to read it in again. This is
4753 necessary because we skipped some symbols when we first
4754 read in the compilation unit (see load_partial_dies).
4755 This problem could be avoided, but the benefit is unclear. */
4756 if (this_cu->cu != NULL)
4757 free_one_cached_comp_unit (this_cu);
4758
4759 gdb_assert (! this_cu->is_debug_types);
4760 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4761 process_psymtab_comp_unit_reader,
4762 &want_partial_unit);
4763
4764 /* Age out any secondary CUs. */
4765 age_cached_comp_units ();
4766 }
4767
4768 static hashval_t
4769 hash_type_unit_group (const void *item)
4770 {
4771 const struct type_unit_group *tu_group = item;
4772
4773 return hash_stmt_list_entry (&tu_group->hash);
4774 }
4775
4776 static int
4777 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4778 {
4779 const struct type_unit_group *lhs = item_lhs;
4780 const struct type_unit_group *rhs = item_rhs;
4781
4782 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4783 }
4784
4785 /* Allocate a hash table for type unit groups. */
4786
4787 static htab_t
4788 allocate_type_unit_groups_table (void)
4789 {
4790 return htab_create_alloc_ex (3,
4791 hash_type_unit_group,
4792 eq_type_unit_group,
4793 NULL,
4794 &dwarf2_per_objfile->objfile->objfile_obstack,
4795 hashtab_obstack_allocate,
4796 dummy_obstack_deallocate);
4797 }
4798
4799 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4800 partial symtabs. We combine several TUs per psymtab to not let the size
4801 of any one psymtab grow too big. */
4802 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4803 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4804
4805 /* Helper routine for get_type_unit_group.
4806 Create the type_unit_group object used to hold one or more TUs. */
4807
4808 static struct type_unit_group *
4809 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4810 {
4811 struct objfile *objfile = dwarf2_per_objfile->objfile;
4812 struct dwarf2_per_cu_data *per_cu;
4813 struct type_unit_group *tu_group;
4814
4815 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4816 struct type_unit_group);
4817 per_cu = &tu_group->per_cu;
4818 per_cu->objfile = objfile;
4819 per_cu->is_debug_types = 1;
4820 per_cu->s.type_unit_group = tu_group;
4821
4822 if (dwarf2_per_objfile->using_index)
4823 {
4824 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4825 struct dwarf2_per_cu_quick_data);
4826 tu_group->t.first_tu = cu->per_cu;
4827 }
4828 else
4829 {
4830 unsigned int line_offset = line_offset_struct.sect_off;
4831 struct partial_symtab *pst;
4832 char *name;
4833
4834 /* Give the symtab a useful name for debug purposes. */
4835 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4836 name = xstrprintf ("<type_units_%d>",
4837 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4838 else
4839 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4840
4841 pst = create_partial_symtab (per_cu, name);
4842 pst->anonymous = 1;
4843
4844 xfree (name);
4845 }
4846
4847 tu_group->hash.dwo_unit = cu->dwo_unit;
4848 tu_group->hash.line_offset = line_offset_struct;
4849
4850 return tu_group;
4851 }
4852
4853 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4854 STMT_LIST is a DW_AT_stmt_list attribute. */
4855
4856 static struct type_unit_group *
4857 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4858 {
4859 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4860 struct type_unit_group *tu_group;
4861 void **slot;
4862 unsigned int line_offset;
4863 struct type_unit_group type_unit_group_for_lookup;
4864
4865 if (dwarf2_per_objfile->type_unit_groups == NULL)
4866 {
4867 dwarf2_per_objfile->type_unit_groups =
4868 allocate_type_unit_groups_table ();
4869 }
4870
4871 /* Do we need to create a new group, or can we use an existing one? */
4872
4873 if (stmt_list)
4874 {
4875 line_offset = DW_UNSND (stmt_list);
4876 ++tu_stats->nr_symtab_sharers;
4877 }
4878 else
4879 {
4880 /* Ugh, no stmt_list. Rare, but we have to handle it.
4881 We can do various things here like create one group per TU or
4882 spread them over multiple groups to split up the expansion work.
4883 To avoid worst case scenarios (too many groups or too large groups)
4884 we, umm, group them in bunches. */
4885 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4886 | (tu_stats->nr_stmt_less_type_units
4887 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
4888 ++tu_stats->nr_stmt_less_type_units;
4889 }
4890
4891 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
4892 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
4893 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
4894 &type_unit_group_for_lookup, INSERT);
4895 if (*slot != NULL)
4896 {
4897 tu_group = *slot;
4898 gdb_assert (tu_group != NULL);
4899 }
4900 else
4901 {
4902 sect_offset line_offset_struct;
4903
4904 line_offset_struct.sect_off = line_offset;
4905 tu_group = create_type_unit_group (cu, line_offset_struct);
4906 *slot = tu_group;
4907 ++tu_stats->nr_symtabs;
4908 }
4909
4910 return tu_group;
4911 }
4912
4913 /* Struct used to sort TUs by their abbreviation table offset. */
4914
4915 struct tu_abbrev_offset
4916 {
4917 struct signatured_type *sig_type;
4918 sect_offset abbrev_offset;
4919 };
4920
4921 /* Helper routine for build_type_unit_groups, passed to qsort. */
4922
4923 static int
4924 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
4925 {
4926 const struct tu_abbrev_offset * const *a = ap;
4927 const struct tu_abbrev_offset * const *b = bp;
4928 unsigned int aoff = (*a)->abbrev_offset.sect_off;
4929 unsigned int boff = (*b)->abbrev_offset.sect_off;
4930
4931 return (aoff > boff) - (aoff < boff);
4932 }
4933
4934 /* A helper function to add a type_unit_group to a table. */
4935
4936 static int
4937 add_type_unit_group_to_table (void **slot, void *datum)
4938 {
4939 struct type_unit_group *tu_group = *slot;
4940 struct type_unit_group ***datap = datum;
4941
4942 **datap = tu_group;
4943 ++*datap;
4944
4945 return 1;
4946 }
4947
4948 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
4949 each one passing FUNC,DATA.
4950
4951 The efficiency is because we sort TUs by the abbrev table they use and
4952 only read each abbrev table once. In one program there are 200K TUs
4953 sharing 8K abbrev tables.
4954
4955 The main purpose of this function is to support building the
4956 dwarf2_per_objfile->type_unit_groups table.
4957 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4958 can collapse the search space by grouping them by stmt_list.
4959 The savings can be significant, in the same program from above the 200K TUs
4960 share 8K stmt_list tables.
4961
4962 FUNC is expected to call get_type_unit_group, which will create the
4963 struct type_unit_group if necessary and add it to
4964 dwarf2_per_objfile->type_unit_groups. */
4965
4966 static void
4967 build_type_unit_groups (die_reader_func_ftype *func, void *data)
4968 {
4969 struct objfile *objfile = dwarf2_per_objfile->objfile;
4970 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4971 struct cleanup *cleanups;
4972 struct abbrev_table *abbrev_table;
4973 sect_offset abbrev_offset;
4974 struct tu_abbrev_offset *sorted_by_abbrev;
4975 struct type_unit_group **iter;
4976 int i;
4977
4978 /* It's up to the caller to not call us multiple times. */
4979 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
4980
4981 if (dwarf2_per_objfile->n_type_units == 0)
4982 return;
4983
4984 /* TUs typically share abbrev tables, and there can be way more TUs than
4985 abbrev tables. Sort by abbrev table to reduce the number of times we
4986 read each abbrev table in.
4987 Alternatives are to punt or to maintain a cache of abbrev tables.
4988 This is simpler and efficient enough for now.
4989
4990 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4991 symtab to use). Typically TUs with the same abbrev offset have the same
4992 stmt_list value too so in practice this should work well.
4993
4994 The basic algorithm here is:
4995
4996 sort TUs by abbrev table
4997 for each TU with same abbrev table:
4998 read abbrev table if first user
4999 read TU top level DIE
5000 [IWBN if DWO skeletons had DW_AT_stmt_list]
5001 call FUNC */
5002
5003 if (dwarf2_read_debug)
5004 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5005
5006 /* Sort in a separate table to maintain the order of all_type_units
5007 for .gdb_index: TU indices directly index all_type_units. */
5008 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5009 dwarf2_per_objfile->n_type_units);
5010 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5011 {
5012 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5013
5014 sorted_by_abbrev[i].sig_type = sig_type;
5015 sorted_by_abbrev[i].abbrev_offset =
5016 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5017 sig_type->per_cu.offset);
5018 }
5019 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5020 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5021 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5022
5023 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5024 called any number of times, so we don't reset tu_stats here. */
5025
5026 abbrev_offset.sect_off = ~(unsigned) 0;
5027 abbrev_table = NULL;
5028 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5029
5030 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5031 {
5032 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5033
5034 /* Switch to the next abbrev table if necessary. */
5035 if (abbrev_table == NULL
5036 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5037 {
5038 if (abbrev_table != NULL)
5039 {
5040 abbrev_table_free (abbrev_table);
5041 /* Reset to NULL in case abbrev_table_read_table throws
5042 an error: abbrev_table_free_cleanup will get called. */
5043 abbrev_table = NULL;
5044 }
5045 abbrev_offset = tu->abbrev_offset;
5046 abbrev_table =
5047 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5048 abbrev_offset);
5049 ++tu_stats->nr_uniq_abbrev_tables;
5050 }
5051
5052 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5053 func, data);
5054 }
5055
5056 /* Create a vector of pointers to primary type units to make it easy to
5057 iterate over them and CUs. See dw2_get_primary_cu. */
5058 dwarf2_per_objfile->n_type_unit_groups =
5059 htab_elements (dwarf2_per_objfile->type_unit_groups);
5060 dwarf2_per_objfile->all_type_unit_groups =
5061 obstack_alloc (&objfile->objfile_obstack,
5062 dwarf2_per_objfile->n_type_unit_groups
5063 * sizeof (struct type_unit_group *));
5064 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5065 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5066 add_type_unit_group_to_table, &iter);
5067 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5068 == dwarf2_per_objfile->n_type_unit_groups);
5069
5070 do_cleanups (cleanups);
5071
5072 if (dwarf2_read_debug)
5073 {
5074 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5075 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5076 dwarf2_per_objfile->n_type_units);
5077 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5078 tu_stats->nr_uniq_abbrev_tables);
5079 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5080 tu_stats->nr_symtabs);
5081 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5082 tu_stats->nr_symtab_sharers);
5083 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5084 tu_stats->nr_stmt_less_type_units);
5085 }
5086 }
5087
5088 /* Reader function for build_type_psymtabs. */
5089
5090 static void
5091 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5092 gdb_byte *info_ptr,
5093 struct die_info *type_unit_die,
5094 int has_children,
5095 void *data)
5096 {
5097 struct objfile *objfile = dwarf2_per_objfile->objfile;
5098 struct dwarf2_cu *cu = reader->cu;
5099 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5100 struct type_unit_group *tu_group;
5101 struct attribute *attr;
5102 struct partial_die_info *first_die;
5103 CORE_ADDR lowpc, highpc;
5104 struct partial_symtab *pst;
5105
5106 gdb_assert (data == NULL);
5107
5108 if (! has_children)
5109 return;
5110
5111 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5112 tu_group = get_type_unit_group (cu, attr);
5113
5114 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5115
5116 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5117 cu->list_in_scope = &file_symbols;
5118 pst = create_partial_symtab (per_cu, "");
5119 pst->anonymous = 1;
5120
5121 first_die = load_partial_dies (reader, info_ptr, 1);
5122
5123 lowpc = (CORE_ADDR) -1;
5124 highpc = (CORE_ADDR) 0;
5125 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5126
5127 pst->n_global_syms = objfile->global_psymbols.next -
5128 (objfile->global_psymbols.list + pst->globals_offset);
5129 pst->n_static_syms = objfile->static_psymbols.next -
5130 (objfile->static_psymbols.list + pst->statics_offset);
5131 sort_pst_symbols (pst);
5132 }
5133
5134 /* Traversal function for build_type_psymtabs. */
5135
5136 static int
5137 build_type_psymtab_dependencies (void **slot, void *info)
5138 {
5139 struct objfile *objfile = dwarf2_per_objfile->objfile;
5140 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5141 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5142 struct partial_symtab *pst = per_cu->v.psymtab;
5143 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5144 struct dwarf2_per_cu_data *iter;
5145 int i;
5146
5147 gdb_assert (len > 0);
5148
5149 pst->number_of_dependencies = len;
5150 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5151 len * sizeof (struct psymtab *));
5152 for (i = 0;
5153 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5154 ++i)
5155 {
5156 pst->dependencies[i] = iter->v.psymtab;
5157 iter->s.type_unit_group = tu_group;
5158 }
5159
5160 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5161
5162 return 1;
5163 }
5164
5165 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5166 Build partial symbol tables for the .debug_types comp-units. */
5167
5168 static void
5169 build_type_psymtabs (struct objfile *objfile)
5170 {
5171 if (! create_all_type_units (objfile))
5172 return;
5173
5174 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5175
5176 /* Now that all TUs have been processed we can fill in the dependencies. */
5177 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5178 build_type_psymtab_dependencies, NULL);
5179 }
5180
5181 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5182
5183 static void
5184 psymtabs_addrmap_cleanup (void *o)
5185 {
5186 struct objfile *objfile = o;
5187
5188 objfile->psymtabs_addrmap = NULL;
5189 }
5190
5191 /* Compute the 'user' field for each psymtab in OBJFILE. */
5192
5193 static void
5194 set_partial_user (struct objfile *objfile)
5195 {
5196 int i;
5197
5198 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5199 {
5200 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5201 struct partial_symtab *pst = per_cu->v.psymtab;
5202 int j;
5203
5204 for (j = 0; j < pst->number_of_dependencies; ++j)
5205 {
5206 /* Set the 'user' field only if it is not already set. */
5207 if (pst->dependencies[j]->user == NULL)
5208 pst->dependencies[j]->user = pst;
5209 }
5210 }
5211 }
5212
5213 /* Build the partial symbol table by doing a quick pass through the
5214 .debug_info and .debug_abbrev sections. */
5215
5216 static void
5217 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5218 {
5219 struct cleanup *back_to, *addrmap_cleanup;
5220 struct obstack temp_obstack;
5221 int i;
5222
5223 if (dwarf2_read_debug)
5224 {
5225 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5226 objfile->name);
5227 }
5228
5229 dwarf2_per_objfile->reading_partial_symbols = 1;
5230
5231 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5232
5233 /* Any cached compilation units will be linked by the per-objfile
5234 read_in_chain. Make sure to free them when we're done. */
5235 back_to = make_cleanup (free_cached_comp_units, NULL);
5236
5237 build_type_psymtabs (objfile);
5238
5239 create_all_comp_units (objfile);
5240
5241 /* Create a temporary address map on a temporary obstack. We later
5242 copy this to the final obstack. */
5243 obstack_init (&temp_obstack);
5244 make_cleanup_obstack_free (&temp_obstack);
5245 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5246 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5247
5248 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5249 {
5250 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5251
5252 process_psymtab_comp_unit (per_cu, 0);
5253 }
5254
5255 set_partial_user (objfile);
5256
5257 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5258 &objfile->objfile_obstack);
5259 discard_cleanups (addrmap_cleanup);
5260
5261 do_cleanups (back_to);
5262
5263 if (dwarf2_read_debug)
5264 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5265 objfile->name);
5266 }
5267
5268 /* die_reader_func for load_partial_comp_unit. */
5269
5270 static void
5271 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5272 gdb_byte *info_ptr,
5273 struct die_info *comp_unit_die,
5274 int has_children,
5275 void *data)
5276 {
5277 struct dwarf2_cu *cu = reader->cu;
5278
5279 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5280
5281 /* Check if comp unit has_children.
5282 If so, read the rest of the partial symbols from this comp unit.
5283 If not, there's no more debug_info for this comp unit. */
5284 if (has_children)
5285 load_partial_dies (reader, info_ptr, 0);
5286 }
5287
5288 /* Load the partial DIEs for a secondary CU into memory.
5289 This is also used when rereading a primary CU with load_all_dies. */
5290
5291 static void
5292 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5293 {
5294 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5295 load_partial_comp_unit_reader, NULL);
5296 }
5297
5298 /* Create a list of all compilation units in OBJFILE.
5299 This is only done for -readnow and building partial symtabs. */
5300
5301 static void
5302 create_all_comp_units (struct objfile *objfile)
5303 {
5304 int n_allocated;
5305 int n_comp_units;
5306 struct dwarf2_per_cu_data **all_comp_units;
5307 gdb_byte *info_ptr;
5308
5309 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5310 info_ptr = dwarf2_per_objfile->info.buffer;
5311
5312 n_comp_units = 0;
5313 n_allocated = 10;
5314 all_comp_units = xmalloc (n_allocated
5315 * sizeof (struct dwarf2_per_cu_data *));
5316
5317 while (info_ptr < dwarf2_per_objfile->info.buffer
5318 + dwarf2_per_objfile->info.size)
5319 {
5320 unsigned int length, initial_length_size;
5321 struct dwarf2_per_cu_data *this_cu;
5322 sect_offset offset;
5323
5324 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
5325
5326 /* Read just enough information to find out where the next
5327 compilation unit is. */
5328 length = read_initial_length (objfile->obfd, info_ptr,
5329 &initial_length_size);
5330
5331 /* Save the compilation unit for later lookup. */
5332 this_cu = obstack_alloc (&objfile->objfile_obstack,
5333 sizeof (struct dwarf2_per_cu_data));
5334 memset (this_cu, 0, sizeof (*this_cu));
5335 this_cu->offset = offset;
5336 this_cu->length = length + initial_length_size;
5337 this_cu->objfile = objfile;
5338 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
5339
5340 if (n_comp_units == n_allocated)
5341 {
5342 n_allocated *= 2;
5343 all_comp_units = xrealloc (all_comp_units,
5344 n_allocated
5345 * sizeof (struct dwarf2_per_cu_data *));
5346 }
5347 all_comp_units[n_comp_units++] = this_cu;
5348
5349 info_ptr = info_ptr + this_cu->length;
5350 }
5351
5352 dwarf2_per_objfile->all_comp_units
5353 = obstack_alloc (&objfile->objfile_obstack,
5354 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5355 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5356 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5357 xfree (all_comp_units);
5358 dwarf2_per_objfile->n_comp_units = n_comp_units;
5359 }
5360
5361 /* Process all loaded DIEs for compilation unit CU, starting at
5362 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5363 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5364 DW_AT_ranges). If NEED_PC is set, then this function will set
5365 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5366 and record the covered ranges in the addrmap. */
5367
5368 static void
5369 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5370 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5371 {
5372 struct partial_die_info *pdi;
5373
5374 /* Now, march along the PDI's, descending into ones which have
5375 interesting children but skipping the children of the other ones,
5376 until we reach the end of the compilation unit. */
5377
5378 pdi = first_die;
5379
5380 while (pdi != NULL)
5381 {
5382 fixup_partial_die (pdi, cu);
5383
5384 /* Anonymous namespaces or modules have no name but have interesting
5385 children, so we need to look at them. Ditto for anonymous
5386 enums. */
5387
5388 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5389 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5390 || pdi->tag == DW_TAG_imported_unit)
5391 {
5392 switch (pdi->tag)
5393 {
5394 case DW_TAG_subprogram:
5395 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5396 break;
5397 case DW_TAG_constant:
5398 case DW_TAG_variable:
5399 case DW_TAG_typedef:
5400 case DW_TAG_union_type:
5401 if (!pdi->is_declaration)
5402 {
5403 add_partial_symbol (pdi, cu);
5404 }
5405 break;
5406 case DW_TAG_class_type:
5407 case DW_TAG_interface_type:
5408 case DW_TAG_structure_type:
5409 if (!pdi->is_declaration)
5410 {
5411 add_partial_symbol (pdi, cu);
5412 }
5413 break;
5414 case DW_TAG_enumeration_type:
5415 if (!pdi->is_declaration)
5416 add_partial_enumeration (pdi, cu);
5417 break;
5418 case DW_TAG_base_type:
5419 case DW_TAG_subrange_type:
5420 /* File scope base type definitions are added to the partial
5421 symbol table. */
5422 add_partial_symbol (pdi, cu);
5423 break;
5424 case DW_TAG_namespace:
5425 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5426 break;
5427 case DW_TAG_module:
5428 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5429 break;
5430 case DW_TAG_imported_unit:
5431 {
5432 struct dwarf2_per_cu_data *per_cu;
5433
5434 /* For now we don't handle imported units in type units. */
5435 if (cu->per_cu->is_debug_types)
5436 {
5437 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5438 " supported in type units [in module %s]"),
5439 cu->objfile->name);
5440 }
5441
5442 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5443 cu->objfile);
5444
5445 /* Go read the partial unit, if needed. */
5446 if (per_cu->v.psymtab == NULL)
5447 process_psymtab_comp_unit (per_cu, 1);
5448
5449 VEC_safe_push (dwarf2_per_cu_ptr,
5450 cu->per_cu->s.imported_symtabs, per_cu);
5451 }
5452 break;
5453 default:
5454 break;
5455 }
5456 }
5457
5458 /* If the die has a sibling, skip to the sibling. */
5459
5460 pdi = pdi->die_sibling;
5461 }
5462 }
5463
5464 /* Functions used to compute the fully scoped name of a partial DIE.
5465
5466 Normally, this is simple. For C++, the parent DIE's fully scoped
5467 name is concatenated with "::" and the partial DIE's name. For
5468 Java, the same thing occurs except that "." is used instead of "::".
5469 Enumerators are an exception; they use the scope of their parent
5470 enumeration type, i.e. the name of the enumeration type is not
5471 prepended to the enumerator.
5472
5473 There are two complexities. One is DW_AT_specification; in this
5474 case "parent" means the parent of the target of the specification,
5475 instead of the direct parent of the DIE. The other is compilers
5476 which do not emit DW_TAG_namespace; in this case we try to guess
5477 the fully qualified name of structure types from their members'
5478 linkage names. This must be done using the DIE's children rather
5479 than the children of any DW_AT_specification target. We only need
5480 to do this for structures at the top level, i.e. if the target of
5481 any DW_AT_specification (if any; otherwise the DIE itself) does not
5482 have a parent. */
5483
5484 /* Compute the scope prefix associated with PDI's parent, in
5485 compilation unit CU. The result will be allocated on CU's
5486 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5487 field. NULL is returned if no prefix is necessary. */
5488 static char *
5489 partial_die_parent_scope (struct partial_die_info *pdi,
5490 struct dwarf2_cu *cu)
5491 {
5492 char *grandparent_scope;
5493 struct partial_die_info *parent, *real_pdi;
5494
5495 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5496 then this means the parent of the specification DIE. */
5497
5498 real_pdi = pdi;
5499 while (real_pdi->has_specification)
5500 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
5501
5502 parent = real_pdi->die_parent;
5503 if (parent == NULL)
5504 return NULL;
5505
5506 if (parent->scope_set)
5507 return parent->scope;
5508
5509 fixup_partial_die (parent, cu);
5510
5511 grandparent_scope = partial_die_parent_scope (parent, cu);
5512
5513 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5514 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5515 Work around this problem here. */
5516 if (cu->language == language_cplus
5517 && parent->tag == DW_TAG_namespace
5518 && strcmp (parent->name, "::") == 0
5519 && grandparent_scope == NULL)
5520 {
5521 parent->scope = NULL;
5522 parent->scope_set = 1;
5523 return NULL;
5524 }
5525
5526 if (pdi->tag == DW_TAG_enumerator)
5527 /* Enumerators should not get the name of the enumeration as a prefix. */
5528 parent->scope = grandparent_scope;
5529 else if (parent->tag == DW_TAG_namespace
5530 || parent->tag == DW_TAG_module
5531 || parent->tag == DW_TAG_structure_type
5532 || parent->tag == DW_TAG_class_type
5533 || parent->tag == DW_TAG_interface_type
5534 || parent->tag == DW_TAG_union_type
5535 || parent->tag == DW_TAG_enumeration_type)
5536 {
5537 if (grandparent_scope == NULL)
5538 parent->scope = parent->name;
5539 else
5540 parent->scope = typename_concat (&cu->comp_unit_obstack,
5541 grandparent_scope,
5542 parent->name, 0, cu);
5543 }
5544 else
5545 {
5546 /* FIXME drow/2004-04-01: What should we be doing with
5547 function-local names? For partial symbols, we should probably be
5548 ignoring them. */
5549 complaint (&symfile_complaints,
5550 _("unhandled containing DIE tag %d for DIE at %d"),
5551 parent->tag, pdi->offset.sect_off);
5552 parent->scope = grandparent_scope;
5553 }
5554
5555 parent->scope_set = 1;
5556 return parent->scope;
5557 }
5558
5559 /* Return the fully scoped name associated with PDI, from compilation unit
5560 CU. The result will be allocated with malloc. */
5561
5562 static char *
5563 partial_die_full_name (struct partial_die_info *pdi,
5564 struct dwarf2_cu *cu)
5565 {
5566 char *parent_scope;
5567
5568 /* If this is a template instantiation, we can not work out the
5569 template arguments from partial DIEs. So, unfortunately, we have
5570 to go through the full DIEs. At least any work we do building
5571 types here will be reused if full symbols are loaded later. */
5572 if (pdi->has_template_arguments)
5573 {
5574 fixup_partial_die (pdi, cu);
5575
5576 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5577 {
5578 struct die_info *die;
5579 struct attribute attr;
5580 struct dwarf2_cu *ref_cu = cu;
5581
5582 /* DW_FORM_ref_addr is using section offset. */
5583 attr.name = 0;
5584 attr.form = DW_FORM_ref_addr;
5585 attr.u.unsnd = pdi->offset.sect_off;
5586 die = follow_die_ref (NULL, &attr, &ref_cu);
5587
5588 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5589 }
5590 }
5591
5592 parent_scope = partial_die_parent_scope (pdi, cu);
5593 if (parent_scope == NULL)
5594 return NULL;
5595 else
5596 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5597 }
5598
5599 static void
5600 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5601 {
5602 struct objfile *objfile = cu->objfile;
5603 CORE_ADDR addr = 0;
5604 char *actual_name = NULL;
5605 CORE_ADDR baseaddr;
5606 int built_actual_name = 0;
5607
5608 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5609
5610 actual_name = partial_die_full_name (pdi, cu);
5611 if (actual_name)
5612 built_actual_name = 1;
5613
5614 if (actual_name == NULL)
5615 actual_name = pdi->name;
5616
5617 switch (pdi->tag)
5618 {
5619 case DW_TAG_subprogram:
5620 if (pdi->is_external || cu->language == language_ada)
5621 {
5622 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5623 of the global scope. But in Ada, we want to be able to access
5624 nested procedures globally. So all Ada subprograms are stored
5625 in the global scope. */
5626 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5627 mst_text, objfile); */
5628 add_psymbol_to_list (actual_name, strlen (actual_name),
5629 built_actual_name,
5630 VAR_DOMAIN, LOC_BLOCK,
5631 &objfile->global_psymbols,
5632 0, pdi->lowpc + baseaddr,
5633 cu->language, objfile);
5634 }
5635 else
5636 {
5637 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5638 mst_file_text, objfile); */
5639 add_psymbol_to_list (actual_name, strlen (actual_name),
5640 built_actual_name,
5641 VAR_DOMAIN, LOC_BLOCK,
5642 &objfile->static_psymbols,
5643 0, pdi->lowpc + baseaddr,
5644 cu->language, objfile);
5645 }
5646 break;
5647 case DW_TAG_constant:
5648 {
5649 struct psymbol_allocation_list *list;
5650
5651 if (pdi->is_external)
5652 list = &objfile->global_psymbols;
5653 else
5654 list = &objfile->static_psymbols;
5655 add_psymbol_to_list (actual_name, strlen (actual_name),
5656 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5657 list, 0, 0, cu->language, objfile);
5658 }
5659 break;
5660 case DW_TAG_variable:
5661 if (pdi->d.locdesc)
5662 addr = decode_locdesc (pdi->d.locdesc, cu);
5663
5664 if (pdi->d.locdesc
5665 && addr == 0
5666 && !dwarf2_per_objfile->has_section_at_zero)
5667 {
5668 /* A global or static variable may also have been stripped
5669 out by the linker if unused, in which case its address
5670 will be nullified; do not add such variables into partial
5671 symbol table then. */
5672 }
5673 else if (pdi->is_external)
5674 {
5675 /* Global Variable.
5676 Don't enter into the minimal symbol tables as there is
5677 a minimal symbol table entry from the ELF symbols already.
5678 Enter into partial symbol table if it has a location
5679 descriptor or a type.
5680 If the location descriptor is missing, new_symbol will create
5681 a LOC_UNRESOLVED symbol, the address of the variable will then
5682 be determined from the minimal symbol table whenever the variable
5683 is referenced.
5684 The address for the partial symbol table entry is not
5685 used by GDB, but it comes in handy for debugging partial symbol
5686 table building. */
5687
5688 if (pdi->d.locdesc || pdi->has_type)
5689 add_psymbol_to_list (actual_name, strlen (actual_name),
5690 built_actual_name,
5691 VAR_DOMAIN, LOC_STATIC,
5692 &objfile->global_psymbols,
5693 0, addr + baseaddr,
5694 cu->language, objfile);
5695 }
5696 else
5697 {
5698 /* Static Variable. Skip symbols without location descriptors. */
5699 if (pdi->d.locdesc == NULL)
5700 {
5701 if (built_actual_name)
5702 xfree (actual_name);
5703 return;
5704 }
5705 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5706 mst_file_data, objfile); */
5707 add_psymbol_to_list (actual_name, strlen (actual_name),
5708 built_actual_name,
5709 VAR_DOMAIN, LOC_STATIC,
5710 &objfile->static_psymbols,
5711 0, addr + baseaddr,
5712 cu->language, objfile);
5713 }
5714 break;
5715 case DW_TAG_typedef:
5716 case DW_TAG_base_type:
5717 case DW_TAG_subrange_type:
5718 add_psymbol_to_list (actual_name, strlen (actual_name),
5719 built_actual_name,
5720 VAR_DOMAIN, LOC_TYPEDEF,
5721 &objfile->static_psymbols,
5722 0, (CORE_ADDR) 0, cu->language, objfile);
5723 break;
5724 case DW_TAG_namespace:
5725 add_psymbol_to_list (actual_name, strlen (actual_name),
5726 built_actual_name,
5727 VAR_DOMAIN, LOC_TYPEDEF,
5728 &objfile->global_psymbols,
5729 0, (CORE_ADDR) 0, cu->language, objfile);
5730 break;
5731 case DW_TAG_class_type:
5732 case DW_TAG_interface_type:
5733 case DW_TAG_structure_type:
5734 case DW_TAG_union_type:
5735 case DW_TAG_enumeration_type:
5736 /* Skip external references. The DWARF standard says in the section
5737 about "Structure, Union, and Class Type Entries": "An incomplete
5738 structure, union or class type is represented by a structure,
5739 union or class entry that does not have a byte size attribute
5740 and that has a DW_AT_declaration attribute." */
5741 if (!pdi->has_byte_size && pdi->is_declaration)
5742 {
5743 if (built_actual_name)
5744 xfree (actual_name);
5745 return;
5746 }
5747
5748 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5749 static vs. global. */
5750 add_psymbol_to_list (actual_name, strlen (actual_name),
5751 built_actual_name,
5752 STRUCT_DOMAIN, LOC_TYPEDEF,
5753 (cu->language == language_cplus
5754 || cu->language == language_java)
5755 ? &objfile->global_psymbols
5756 : &objfile->static_psymbols,
5757 0, (CORE_ADDR) 0, cu->language, objfile);
5758
5759 break;
5760 case DW_TAG_enumerator:
5761 add_psymbol_to_list (actual_name, strlen (actual_name),
5762 built_actual_name,
5763 VAR_DOMAIN, LOC_CONST,
5764 (cu->language == language_cplus
5765 || cu->language == language_java)
5766 ? &objfile->global_psymbols
5767 : &objfile->static_psymbols,
5768 0, (CORE_ADDR) 0, cu->language, objfile);
5769 break;
5770 default:
5771 break;
5772 }
5773
5774 if (built_actual_name)
5775 xfree (actual_name);
5776 }
5777
5778 /* Read a partial die corresponding to a namespace; also, add a symbol
5779 corresponding to that namespace to the symbol table. NAMESPACE is
5780 the name of the enclosing namespace. */
5781
5782 static void
5783 add_partial_namespace (struct partial_die_info *pdi,
5784 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5785 int need_pc, struct dwarf2_cu *cu)
5786 {
5787 /* Add a symbol for the namespace. */
5788
5789 add_partial_symbol (pdi, cu);
5790
5791 /* Now scan partial symbols in that namespace. */
5792
5793 if (pdi->has_children)
5794 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5795 }
5796
5797 /* Read a partial die corresponding to a Fortran module. */
5798
5799 static void
5800 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5801 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5802 {
5803 /* Now scan partial symbols in that module. */
5804
5805 if (pdi->has_children)
5806 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5807 }
5808
5809 /* Read a partial die corresponding to a subprogram and create a partial
5810 symbol for that subprogram. When the CU language allows it, this
5811 routine also defines a partial symbol for each nested subprogram
5812 that this subprogram contains.
5813
5814 DIE my also be a lexical block, in which case we simply search
5815 recursively for suprograms defined inside that lexical block.
5816 Again, this is only performed when the CU language allows this
5817 type of definitions. */
5818
5819 static void
5820 add_partial_subprogram (struct partial_die_info *pdi,
5821 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5822 int need_pc, struct dwarf2_cu *cu)
5823 {
5824 if (pdi->tag == DW_TAG_subprogram)
5825 {
5826 if (pdi->has_pc_info)
5827 {
5828 if (pdi->lowpc < *lowpc)
5829 *lowpc = pdi->lowpc;
5830 if (pdi->highpc > *highpc)
5831 *highpc = pdi->highpc;
5832 if (need_pc)
5833 {
5834 CORE_ADDR baseaddr;
5835 struct objfile *objfile = cu->objfile;
5836
5837 baseaddr = ANOFFSET (objfile->section_offsets,
5838 SECT_OFF_TEXT (objfile));
5839 addrmap_set_empty (objfile->psymtabs_addrmap,
5840 pdi->lowpc + baseaddr,
5841 pdi->highpc - 1 + baseaddr,
5842 cu->per_cu->v.psymtab);
5843 }
5844 }
5845
5846 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5847 {
5848 if (!pdi->is_declaration)
5849 /* Ignore subprogram DIEs that do not have a name, they are
5850 illegal. Do not emit a complaint at this point, we will
5851 do so when we convert this psymtab into a symtab. */
5852 if (pdi->name)
5853 add_partial_symbol (pdi, cu);
5854 }
5855 }
5856
5857 if (! pdi->has_children)
5858 return;
5859
5860 if (cu->language == language_ada)
5861 {
5862 pdi = pdi->die_child;
5863 while (pdi != NULL)
5864 {
5865 fixup_partial_die (pdi, cu);
5866 if (pdi->tag == DW_TAG_subprogram
5867 || pdi->tag == DW_TAG_lexical_block)
5868 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5869 pdi = pdi->die_sibling;
5870 }
5871 }
5872 }
5873
5874 /* Read a partial die corresponding to an enumeration type. */
5875
5876 static void
5877 add_partial_enumeration (struct partial_die_info *enum_pdi,
5878 struct dwarf2_cu *cu)
5879 {
5880 struct partial_die_info *pdi;
5881
5882 if (enum_pdi->name != NULL)
5883 add_partial_symbol (enum_pdi, cu);
5884
5885 pdi = enum_pdi->die_child;
5886 while (pdi)
5887 {
5888 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
5889 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5890 else
5891 add_partial_symbol (pdi, cu);
5892 pdi = pdi->die_sibling;
5893 }
5894 }
5895
5896 /* Return the initial uleb128 in the die at INFO_PTR. */
5897
5898 static unsigned int
5899 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5900 {
5901 unsigned int bytes_read;
5902
5903 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5904 }
5905
5906 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5907 Return the corresponding abbrev, or NULL if the number is zero (indicating
5908 an empty DIE). In either case *BYTES_READ will be set to the length of
5909 the initial number. */
5910
5911 static struct abbrev_info *
5912 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5913 struct dwarf2_cu *cu)
5914 {
5915 bfd *abfd = cu->objfile->obfd;
5916 unsigned int abbrev_number;
5917 struct abbrev_info *abbrev;
5918
5919 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5920
5921 if (abbrev_number == 0)
5922 return NULL;
5923
5924 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
5925 if (!abbrev)
5926 {
5927 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5928 abbrev_number, bfd_get_filename (abfd));
5929 }
5930
5931 return abbrev;
5932 }
5933
5934 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5935 Returns a pointer to the end of a series of DIEs, terminated by an empty
5936 DIE. Any children of the skipped DIEs will also be skipped. */
5937
5938 static gdb_byte *
5939 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5940 {
5941 struct dwarf2_cu *cu = reader->cu;
5942 struct abbrev_info *abbrev;
5943 unsigned int bytes_read;
5944
5945 while (1)
5946 {
5947 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5948 if (abbrev == NULL)
5949 return info_ptr + bytes_read;
5950 else
5951 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5952 }
5953 }
5954
5955 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5956 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5957 abbrev corresponding to that skipped uleb128 should be passed in
5958 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5959 children. */
5960
5961 static gdb_byte *
5962 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5963 struct abbrev_info *abbrev)
5964 {
5965 unsigned int bytes_read;
5966 struct attribute attr;
5967 bfd *abfd = reader->abfd;
5968 struct dwarf2_cu *cu = reader->cu;
5969 gdb_byte *buffer = reader->buffer;
5970 const gdb_byte *buffer_end = reader->buffer_end;
5971 gdb_byte *start_info_ptr = info_ptr;
5972 unsigned int form, i;
5973
5974 for (i = 0; i < abbrev->num_attrs; i++)
5975 {
5976 /* The only abbrev we care about is DW_AT_sibling. */
5977 if (abbrev->attrs[i].name == DW_AT_sibling)
5978 {
5979 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5980 if (attr.form == DW_FORM_ref_addr)
5981 complaint (&symfile_complaints,
5982 _("ignoring absolute DW_AT_sibling"));
5983 else
5984 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5985 }
5986
5987 /* If it isn't DW_AT_sibling, skip this attribute. */
5988 form = abbrev->attrs[i].form;
5989 skip_attribute:
5990 switch (form)
5991 {
5992 case DW_FORM_ref_addr:
5993 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5994 and later it is offset sized. */
5995 if (cu->header.version == 2)
5996 info_ptr += cu->header.addr_size;
5997 else
5998 info_ptr += cu->header.offset_size;
5999 break;
6000 case DW_FORM_addr:
6001 info_ptr += cu->header.addr_size;
6002 break;
6003 case DW_FORM_data1:
6004 case DW_FORM_ref1:
6005 case DW_FORM_flag:
6006 info_ptr += 1;
6007 break;
6008 case DW_FORM_flag_present:
6009 break;
6010 case DW_FORM_data2:
6011 case DW_FORM_ref2:
6012 info_ptr += 2;
6013 break;
6014 case DW_FORM_data4:
6015 case DW_FORM_ref4:
6016 info_ptr += 4;
6017 break;
6018 case DW_FORM_data8:
6019 case DW_FORM_ref8:
6020 case DW_FORM_ref_sig8:
6021 info_ptr += 8;
6022 break;
6023 case DW_FORM_string:
6024 read_direct_string (abfd, info_ptr, &bytes_read);
6025 info_ptr += bytes_read;
6026 break;
6027 case DW_FORM_sec_offset:
6028 case DW_FORM_strp:
6029 info_ptr += cu->header.offset_size;
6030 break;
6031 case DW_FORM_exprloc:
6032 case DW_FORM_block:
6033 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6034 info_ptr += bytes_read;
6035 break;
6036 case DW_FORM_block1:
6037 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6038 break;
6039 case DW_FORM_block2:
6040 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6041 break;
6042 case DW_FORM_block4:
6043 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6044 break;
6045 case DW_FORM_sdata:
6046 case DW_FORM_udata:
6047 case DW_FORM_ref_udata:
6048 case DW_FORM_GNU_addr_index:
6049 case DW_FORM_GNU_str_index:
6050 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6051 break;
6052 case DW_FORM_indirect:
6053 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6054 info_ptr += bytes_read;
6055 /* We need to continue parsing from here, so just go back to
6056 the top. */
6057 goto skip_attribute;
6058
6059 default:
6060 error (_("Dwarf Error: Cannot handle %s "
6061 "in DWARF reader [in module %s]"),
6062 dwarf_form_name (form),
6063 bfd_get_filename (abfd));
6064 }
6065 }
6066
6067 if (abbrev->has_children)
6068 return skip_children (reader, info_ptr);
6069 else
6070 return info_ptr;
6071 }
6072
6073 /* Locate ORIG_PDI's sibling.
6074 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6075
6076 static gdb_byte *
6077 locate_pdi_sibling (const struct die_reader_specs *reader,
6078 struct partial_die_info *orig_pdi,
6079 gdb_byte *info_ptr)
6080 {
6081 /* Do we know the sibling already? */
6082
6083 if (orig_pdi->sibling)
6084 return orig_pdi->sibling;
6085
6086 /* Are there any children to deal with? */
6087
6088 if (!orig_pdi->has_children)
6089 return info_ptr;
6090
6091 /* Skip the children the long way. */
6092
6093 return skip_children (reader, info_ptr);
6094 }
6095
6096 /* Expand this partial symbol table into a full symbol table. */
6097
6098 static void
6099 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6100 {
6101 if (pst != NULL)
6102 {
6103 if (pst->readin)
6104 {
6105 warning (_("bug: psymtab for %s is already read in."),
6106 pst->filename);
6107 }
6108 else
6109 {
6110 if (info_verbose)
6111 {
6112 printf_filtered (_("Reading in symbols for %s..."),
6113 pst->filename);
6114 gdb_flush (gdb_stdout);
6115 }
6116
6117 /* Restore our global data. */
6118 dwarf2_per_objfile = objfile_data (pst->objfile,
6119 dwarf2_objfile_data_key);
6120
6121 /* If this psymtab is constructed from a debug-only objfile, the
6122 has_section_at_zero flag will not necessarily be correct. We
6123 can get the correct value for this flag by looking at the data
6124 associated with the (presumably stripped) associated objfile. */
6125 if (pst->objfile->separate_debug_objfile_backlink)
6126 {
6127 struct dwarf2_per_objfile *dpo_backlink
6128 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6129 dwarf2_objfile_data_key);
6130
6131 dwarf2_per_objfile->has_section_at_zero
6132 = dpo_backlink->has_section_at_zero;
6133 }
6134
6135 dwarf2_per_objfile->reading_partial_symbols = 0;
6136
6137 psymtab_to_symtab_1 (pst);
6138
6139 /* Finish up the debug error message. */
6140 if (info_verbose)
6141 printf_filtered (_("done.\n"));
6142 }
6143 }
6144
6145 process_cu_includes ();
6146 }
6147 \f
6148 /* Reading in full CUs. */
6149
6150 /* Add PER_CU to the queue. */
6151
6152 static void
6153 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6154 enum language pretend_language)
6155 {
6156 struct dwarf2_queue_item *item;
6157
6158 per_cu->queued = 1;
6159 item = xmalloc (sizeof (*item));
6160 item->per_cu = per_cu;
6161 item->pretend_language = pretend_language;
6162 item->next = NULL;
6163
6164 if (dwarf2_queue == NULL)
6165 dwarf2_queue = item;
6166 else
6167 dwarf2_queue_tail->next = item;
6168
6169 dwarf2_queue_tail = item;
6170 }
6171
6172 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6173 unit and add it to our queue.
6174 The result is non-zero if PER_CU was queued, otherwise the result is zero
6175 meaning either PER_CU is already queued or it is already loaded. */
6176
6177 static int
6178 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6179 struct dwarf2_per_cu_data *per_cu,
6180 enum language pretend_language)
6181 {
6182 /* We may arrive here during partial symbol reading, if we need full
6183 DIEs to process an unusual case (e.g. template arguments). Do
6184 not queue PER_CU, just tell our caller to load its DIEs. */
6185 if (dwarf2_per_objfile->reading_partial_symbols)
6186 {
6187 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6188 return 1;
6189 return 0;
6190 }
6191
6192 /* Mark the dependence relation so that we don't flush PER_CU
6193 too early. */
6194 dwarf2_add_dependence (this_cu, per_cu);
6195
6196 /* If it's already on the queue, we have nothing to do. */
6197 if (per_cu->queued)
6198 return 0;
6199
6200 /* If the compilation unit is already loaded, just mark it as
6201 used. */
6202 if (per_cu->cu != NULL)
6203 {
6204 per_cu->cu->last_used = 0;
6205 return 0;
6206 }
6207
6208 /* Add it to the queue. */
6209 queue_comp_unit (per_cu, pretend_language);
6210
6211 return 1;
6212 }
6213
6214 /* Process the queue. */
6215
6216 static void
6217 process_queue (void)
6218 {
6219 struct dwarf2_queue_item *item, *next_item;
6220
6221 if (dwarf2_read_debug)
6222 {
6223 fprintf_unfiltered (gdb_stdlog,
6224 "Expanding one or more symtabs of objfile %s ...\n",
6225 dwarf2_per_objfile->objfile->name);
6226 }
6227
6228 /* The queue starts out with one item, but following a DIE reference
6229 may load a new CU, adding it to the end of the queue. */
6230 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6231 {
6232 if (dwarf2_per_objfile->using_index
6233 ? !item->per_cu->v.quick->symtab
6234 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6235 {
6236 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6237
6238 if (dwarf2_read_debug)
6239 {
6240 fprintf_unfiltered (gdb_stdlog,
6241 "Expanding symtab of %s at offset 0x%x\n",
6242 per_cu->is_debug_types ? "TU" : "CU",
6243 per_cu->offset.sect_off);
6244 }
6245
6246 if (per_cu->is_debug_types)
6247 process_full_type_unit (per_cu, item->pretend_language);
6248 else
6249 process_full_comp_unit (per_cu, item->pretend_language);
6250
6251 if (dwarf2_read_debug)
6252 {
6253 fprintf_unfiltered (gdb_stdlog,
6254 "Done expanding %s at offset 0x%x\n",
6255 per_cu->is_debug_types ? "TU" : "CU",
6256 per_cu->offset.sect_off);
6257 }
6258 }
6259
6260 item->per_cu->queued = 0;
6261 next_item = item->next;
6262 xfree (item);
6263 }
6264
6265 dwarf2_queue_tail = NULL;
6266
6267 if (dwarf2_read_debug)
6268 {
6269 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6270 dwarf2_per_objfile->objfile->name);
6271 }
6272 }
6273
6274 /* Free all allocated queue entries. This function only releases anything if
6275 an error was thrown; if the queue was processed then it would have been
6276 freed as we went along. */
6277
6278 static void
6279 dwarf2_release_queue (void *dummy)
6280 {
6281 struct dwarf2_queue_item *item, *last;
6282
6283 item = dwarf2_queue;
6284 while (item)
6285 {
6286 /* Anything still marked queued is likely to be in an
6287 inconsistent state, so discard it. */
6288 if (item->per_cu->queued)
6289 {
6290 if (item->per_cu->cu != NULL)
6291 free_one_cached_comp_unit (item->per_cu);
6292 item->per_cu->queued = 0;
6293 }
6294
6295 last = item;
6296 item = item->next;
6297 xfree (last);
6298 }
6299
6300 dwarf2_queue = dwarf2_queue_tail = NULL;
6301 }
6302
6303 /* Read in full symbols for PST, and anything it depends on. */
6304
6305 static void
6306 psymtab_to_symtab_1 (struct partial_symtab *pst)
6307 {
6308 struct dwarf2_per_cu_data *per_cu;
6309 int i;
6310
6311 if (pst->readin)
6312 return;
6313
6314 for (i = 0; i < pst->number_of_dependencies; i++)
6315 if (!pst->dependencies[i]->readin
6316 && pst->dependencies[i]->user == NULL)
6317 {
6318 /* Inform about additional files that need to be read in. */
6319 if (info_verbose)
6320 {
6321 /* FIXME: i18n: Need to make this a single string. */
6322 fputs_filtered (" ", gdb_stdout);
6323 wrap_here ("");
6324 fputs_filtered ("and ", gdb_stdout);
6325 wrap_here ("");
6326 printf_filtered ("%s...", pst->dependencies[i]->filename);
6327 wrap_here (""); /* Flush output. */
6328 gdb_flush (gdb_stdout);
6329 }
6330 psymtab_to_symtab_1 (pst->dependencies[i]);
6331 }
6332
6333 per_cu = pst->read_symtab_private;
6334
6335 if (per_cu == NULL)
6336 {
6337 /* It's an include file, no symbols to read for it.
6338 Everything is in the parent symtab. */
6339 pst->readin = 1;
6340 return;
6341 }
6342
6343 dw2_do_instantiate_symtab (per_cu);
6344 }
6345
6346 /* Trivial hash function for die_info: the hash value of a DIE
6347 is its offset in .debug_info for this objfile. */
6348
6349 static hashval_t
6350 die_hash (const void *item)
6351 {
6352 const struct die_info *die = item;
6353
6354 return die->offset.sect_off;
6355 }
6356
6357 /* Trivial comparison function for die_info structures: two DIEs
6358 are equal if they have the same offset. */
6359
6360 static int
6361 die_eq (const void *item_lhs, const void *item_rhs)
6362 {
6363 const struct die_info *die_lhs = item_lhs;
6364 const struct die_info *die_rhs = item_rhs;
6365
6366 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6367 }
6368
6369 /* die_reader_func for load_full_comp_unit.
6370 This is identical to read_signatured_type_reader,
6371 but is kept separate for now. */
6372
6373 static void
6374 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6375 gdb_byte *info_ptr,
6376 struct die_info *comp_unit_die,
6377 int has_children,
6378 void *data)
6379 {
6380 struct dwarf2_cu *cu = reader->cu;
6381 enum language *language_ptr = data;
6382
6383 gdb_assert (cu->die_hash == NULL);
6384 cu->die_hash =
6385 htab_create_alloc_ex (cu->header.length / 12,
6386 die_hash,
6387 die_eq,
6388 NULL,
6389 &cu->comp_unit_obstack,
6390 hashtab_obstack_allocate,
6391 dummy_obstack_deallocate);
6392
6393 if (has_children)
6394 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6395 &info_ptr, comp_unit_die);
6396 cu->dies = comp_unit_die;
6397 /* comp_unit_die is not stored in die_hash, no need. */
6398
6399 /* We try not to read any attributes in this function, because not
6400 all CUs needed for references have been loaded yet, and symbol
6401 table processing isn't initialized. But we have to set the CU language,
6402 or we won't be able to build types correctly.
6403 Similarly, if we do not read the producer, we can not apply
6404 producer-specific interpretation. */
6405 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6406 }
6407
6408 /* Load the DIEs associated with PER_CU into memory. */
6409
6410 static void
6411 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6412 enum language pretend_language)
6413 {
6414 gdb_assert (! this_cu->is_debug_types);
6415
6416 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6417 load_full_comp_unit_reader, &pretend_language);
6418 }
6419
6420 /* Add a DIE to the delayed physname list. */
6421
6422 static void
6423 add_to_method_list (struct type *type, int fnfield_index, int index,
6424 const char *name, struct die_info *die,
6425 struct dwarf2_cu *cu)
6426 {
6427 struct delayed_method_info mi;
6428 mi.type = type;
6429 mi.fnfield_index = fnfield_index;
6430 mi.index = index;
6431 mi.name = name;
6432 mi.die = die;
6433 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6434 }
6435
6436 /* A cleanup for freeing the delayed method list. */
6437
6438 static void
6439 free_delayed_list (void *ptr)
6440 {
6441 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6442 if (cu->method_list != NULL)
6443 {
6444 VEC_free (delayed_method_info, cu->method_list);
6445 cu->method_list = NULL;
6446 }
6447 }
6448
6449 /* Compute the physnames of any methods on the CU's method list.
6450
6451 The computation of method physnames is delayed in order to avoid the
6452 (bad) condition that one of the method's formal parameters is of an as yet
6453 incomplete type. */
6454
6455 static void
6456 compute_delayed_physnames (struct dwarf2_cu *cu)
6457 {
6458 int i;
6459 struct delayed_method_info *mi;
6460 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6461 {
6462 const char *physname;
6463 struct fn_fieldlist *fn_flp
6464 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6465 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6466 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6467 }
6468 }
6469
6470 /* Go objects should be embedded in a DW_TAG_module DIE,
6471 and it's not clear if/how imported objects will appear.
6472 To keep Go support simple until that's worked out,
6473 go back through what we've read and create something usable.
6474 We could do this while processing each DIE, and feels kinda cleaner,
6475 but that way is more invasive.
6476 This is to, for example, allow the user to type "p var" or "b main"
6477 without having to specify the package name, and allow lookups
6478 of module.object to work in contexts that use the expression
6479 parser. */
6480
6481 static void
6482 fixup_go_packaging (struct dwarf2_cu *cu)
6483 {
6484 char *package_name = NULL;
6485 struct pending *list;
6486 int i;
6487
6488 for (list = global_symbols; list != NULL; list = list->next)
6489 {
6490 for (i = 0; i < list->nsyms; ++i)
6491 {
6492 struct symbol *sym = list->symbol[i];
6493
6494 if (SYMBOL_LANGUAGE (sym) == language_go
6495 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6496 {
6497 char *this_package_name = go_symbol_package_name (sym);
6498
6499 if (this_package_name == NULL)
6500 continue;
6501 if (package_name == NULL)
6502 package_name = this_package_name;
6503 else
6504 {
6505 if (strcmp (package_name, this_package_name) != 0)
6506 complaint (&symfile_complaints,
6507 _("Symtab %s has objects from two different Go packages: %s and %s"),
6508 (sym->symtab && sym->symtab->filename
6509 ? sym->symtab->filename
6510 : cu->objfile->name),
6511 this_package_name, package_name);
6512 xfree (this_package_name);
6513 }
6514 }
6515 }
6516 }
6517
6518 if (package_name != NULL)
6519 {
6520 struct objfile *objfile = cu->objfile;
6521 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6522 package_name, objfile);
6523 struct symbol *sym;
6524
6525 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6526
6527 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6528 SYMBOL_SET_LANGUAGE (sym, language_go);
6529 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6530 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6531 e.g., "main" finds the "main" module and not C's main(). */
6532 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6533 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6534 SYMBOL_TYPE (sym) = type;
6535
6536 add_symbol_to_list (sym, &global_symbols);
6537
6538 xfree (package_name);
6539 }
6540 }
6541
6542 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6543
6544 /* Return the symtab for PER_CU. This works properly regardless of
6545 whether we're using the index or psymtabs. */
6546
6547 static struct symtab *
6548 get_symtab (struct dwarf2_per_cu_data *per_cu)
6549 {
6550 return (dwarf2_per_objfile->using_index
6551 ? per_cu->v.quick->symtab
6552 : per_cu->v.psymtab->symtab);
6553 }
6554
6555 /* A helper function for computing the list of all symbol tables
6556 included by PER_CU. */
6557
6558 static void
6559 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6560 htab_t all_children,
6561 struct dwarf2_per_cu_data *per_cu)
6562 {
6563 void **slot;
6564 int ix;
6565 struct dwarf2_per_cu_data *iter;
6566
6567 slot = htab_find_slot (all_children, per_cu, INSERT);
6568 if (*slot != NULL)
6569 {
6570 /* This inclusion and its children have been processed. */
6571 return;
6572 }
6573
6574 *slot = per_cu;
6575 /* Only add a CU if it has a symbol table. */
6576 if (get_symtab (per_cu) != NULL)
6577 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6578
6579 for (ix = 0;
6580 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6581 ++ix)
6582 recursively_compute_inclusions (result, all_children, iter);
6583 }
6584
6585 /* Compute the symtab 'includes' fields for the symtab related to
6586 PER_CU. */
6587
6588 static void
6589 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6590 {
6591 gdb_assert (! per_cu->is_debug_types);
6592
6593 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6594 {
6595 int ix, len;
6596 struct dwarf2_per_cu_data *iter;
6597 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6598 htab_t all_children;
6599 struct symtab *symtab = get_symtab (per_cu);
6600
6601 /* If we don't have a symtab, we can just skip this case. */
6602 if (symtab == NULL)
6603 return;
6604
6605 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6606 NULL, xcalloc, xfree);
6607
6608 for (ix = 0;
6609 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6610 ix, iter);
6611 ++ix)
6612 recursively_compute_inclusions (&result_children, all_children, iter);
6613
6614 /* Now we have a transitive closure of all the included CUs, so
6615 we can convert it to a list of symtabs. */
6616 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6617 symtab->includes
6618 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6619 (len + 1) * sizeof (struct symtab *));
6620 for (ix = 0;
6621 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6622 ++ix)
6623 symtab->includes[ix] = get_symtab (iter);
6624 symtab->includes[len] = NULL;
6625
6626 VEC_free (dwarf2_per_cu_ptr, result_children);
6627 htab_delete (all_children);
6628 }
6629 }
6630
6631 /* Compute the 'includes' field for the symtabs of all the CUs we just
6632 read. */
6633
6634 static void
6635 process_cu_includes (void)
6636 {
6637 int ix;
6638 struct dwarf2_per_cu_data *iter;
6639
6640 for (ix = 0;
6641 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6642 ix, iter);
6643 ++ix)
6644 {
6645 if (! iter->is_debug_types)
6646 compute_symtab_includes (iter);
6647 }
6648
6649 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6650 }
6651
6652 /* Generate full symbol information for PER_CU, whose DIEs have
6653 already been loaded into memory. */
6654
6655 static void
6656 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6657 enum language pretend_language)
6658 {
6659 struct dwarf2_cu *cu = per_cu->cu;
6660 struct objfile *objfile = per_cu->objfile;
6661 CORE_ADDR lowpc, highpc;
6662 struct symtab *symtab;
6663 struct cleanup *back_to, *delayed_list_cleanup;
6664 CORE_ADDR baseaddr;
6665 struct block *static_block;
6666
6667 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6668
6669 buildsym_init ();
6670 back_to = make_cleanup (really_free_pendings, NULL);
6671 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6672
6673 cu->list_in_scope = &file_symbols;
6674
6675 cu->language = pretend_language;
6676 cu->language_defn = language_def (cu->language);
6677
6678 /* Do line number decoding in read_file_scope () */
6679 process_die (cu->dies, cu);
6680
6681 /* For now fudge the Go package. */
6682 if (cu->language == language_go)
6683 fixup_go_packaging (cu);
6684
6685 /* Now that we have processed all the DIEs in the CU, all the types
6686 should be complete, and it should now be safe to compute all of the
6687 physnames. */
6688 compute_delayed_physnames (cu);
6689 do_cleanups (delayed_list_cleanup);
6690
6691 /* Some compilers don't define a DW_AT_high_pc attribute for the
6692 compilation unit. If the DW_AT_high_pc is missing, synthesize
6693 it, by scanning the DIE's below the compilation unit. */
6694 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6695
6696 static_block = end_symtab_get_static_block (highpc + baseaddr, objfile, 0);
6697
6698 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6699 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6700 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6701 addrmap to help ensure it has an accurate map of pc values belonging to
6702 this comp unit. */
6703 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6704
6705 symtab = end_symtab_from_static_block (static_block, objfile,
6706 SECT_OFF_TEXT (objfile), 0);
6707
6708 if (symtab != NULL)
6709 {
6710 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6711
6712 /* Set symtab language to language from DW_AT_language. If the
6713 compilation is from a C file generated by language preprocessors, do
6714 not set the language if it was already deduced by start_subfile. */
6715 if (!(cu->language == language_c && symtab->language != language_c))
6716 symtab->language = cu->language;
6717
6718 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6719 produce DW_AT_location with location lists but it can be possibly
6720 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6721 there were bugs in prologue debug info, fixed later in GCC-4.5
6722 by "unwind info for epilogues" patch (which is not directly related).
6723
6724 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6725 needed, it would be wrong due to missing DW_AT_producer there.
6726
6727 Still one can confuse GDB by using non-standard GCC compilation
6728 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6729 */
6730 if (cu->has_loclist && gcc_4_minor >= 5)
6731 symtab->locations_valid = 1;
6732
6733 if (gcc_4_minor >= 5)
6734 symtab->epilogue_unwind_valid = 1;
6735
6736 symtab->call_site_htab = cu->call_site_htab;
6737 }
6738
6739 if (dwarf2_per_objfile->using_index)
6740 per_cu->v.quick->symtab = symtab;
6741 else
6742 {
6743 struct partial_symtab *pst = per_cu->v.psymtab;
6744 pst->symtab = symtab;
6745 pst->readin = 1;
6746 }
6747
6748 /* Push it for inclusion processing later. */
6749 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6750
6751 do_cleanups (back_to);
6752 }
6753
6754 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6755 already been loaded into memory. */
6756
6757 static void
6758 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6759 enum language pretend_language)
6760 {
6761 struct dwarf2_cu *cu = per_cu->cu;
6762 struct objfile *objfile = per_cu->objfile;
6763 struct symtab *symtab;
6764 struct cleanup *back_to, *delayed_list_cleanup;
6765
6766 buildsym_init ();
6767 back_to = make_cleanup (really_free_pendings, NULL);
6768 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6769
6770 cu->list_in_scope = &file_symbols;
6771
6772 cu->language = pretend_language;
6773 cu->language_defn = language_def (cu->language);
6774
6775 /* The symbol tables are set up in read_type_unit_scope. */
6776 process_die (cu->dies, cu);
6777
6778 /* For now fudge the Go package. */
6779 if (cu->language == language_go)
6780 fixup_go_packaging (cu);
6781
6782 /* Now that we have processed all the DIEs in the CU, all the types
6783 should be complete, and it should now be safe to compute all of the
6784 physnames. */
6785 compute_delayed_physnames (cu);
6786 do_cleanups (delayed_list_cleanup);
6787
6788 /* TUs share symbol tables.
6789 If this is the first TU to use this symtab, complete the construction
6790 of it with end_expandable_symtab. Otherwise, complete the addition of
6791 this TU's symbols to the existing symtab. */
6792 if (per_cu->s.type_unit_group->primary_symtab == NULL)
6793 {
6794 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6795 per_cu->s.type_unit_group->primary_symtab = symtab;
6796
6797 if (symtab != NULL)
6798 {
6799 /* Set symtab language to language from DW_AT_language. If the
6800 compilation is from a C file generated by language preprocessors,
6801 do not set the language if it was already deduced by
6802 start_subfile. */
6803 if (!(cu->language == language_c && symtab->language != language_c))
6804 symtab->language = cu->language;
6805 }
6806 }
6807 else
6808 {
6809 augment_type_symtab (objfile,
6810 per_cu->s.type_unit_group->primary_symtab);
6811 symtab = per_cu->s.type_unit_group->primary_symtab;
6812 }
6813
6814 if (dwarf2_per_objfile->using_index)
6815 per_cu->v.quick->symtab = symtab;
6816 else
6817 {
6818 struct partial_symtab *pst = per_cu->v.psymtab;
6819 pst->symtab = symtab;
6820 pst->readin = 1;
6821 }
6822
6823 do_cleanups (back_to);
6824 }
6825
6826 /* Process an imported unit DIE. */
6827
6828 static void
6829 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6830 {
6831 struct attribute *attr;
6832
6833 /* For now we don't handle imported units in type units. */
6834 if (cu->per_cu->is_debug_types)
6835 {
6836 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6837 " supported in type units [in module %s]"),
6838 cu->objfile->name);
6839 }
6840
6841 attr = dwarf2_attr (die, DW_AT_import, cu);
6842 if (attr != NULL)
6843 {
6844 struct dwarf2_per_cu_data *per_cu;
6845 struct symtab *imported_symtab;
6846 sect_offset offset;
6847
6848 offset = dwarf2_get_ref_die_offset (attr);
6849 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6850
6851 /* Queue the unit, if needed. */
6852 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
6853 load_full_comp_unit (per_cu, cu->language);
6854
6855 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
6856 per_cu);
6857 }
6858 }
6859
6860 /* Process a die and its children. */
6861
6862 static void
6863 process_die (struct die_info *die, struct dwarf2_cu *cu)
6864 {
6865 switch (die->tag)
6866 {
6867 case DW_TAG_padding:
6868 break;
6869 case DW_TAG_compile_unit:
6870 case DW_TAG_partial_unit:
6871 read_file_scope (die, cu);
6872 break;
6873 case DW_TAG_type_unit:
6874 read_type_unit_scope (die, cu);
6875 break;
6876 case DW_TAG_subprogram:
6877 case DW_TAG_inlined_subroutine:
6878 read_func_scope (die, cu);
6879 break;
6880 case DW_TAG_lexical_block:
6881 case DW_TAG_try_block:
6882 case DW_TAG_catch_block:
6883 read_lexical_block_scope (die, cu);
6884 break;
6885 case DW_TAG_GNU_call_site:
6886 read_call_site_scope (die, cu);
6887 break;
6888 case DW_TAG_class_type:
6889 case DW_TAG_interface_type:
6890 case DW_TAG_structure_type:
6891 case DW_TAG_union_type:
6892 process_structure_scope (die, cu);
6893 break;
6894 case DW_TAG_enumeration_type:
6895 process_enumeration_scope (die, cu);
6896 break;
6897
6898 /* These dies have a type, but processing them does not create
6899 a symbol or recurse to process the children. Therefore we can
6900 read them on-demand through read_type_die. */
6901 case DW_TAG_subroutine_type:
6902 case DW_TAG_set_type:
6903 case DW_TAG_array_type:
6904 case DW_TAG_pointer_type:
6905 case DW_TAG_ptr_to_member_type:
6906 case DW_TAG_reference_type:
6907 case DW_TAG_string_type:
6908 break;
6909
6910 case DW_TAG_base_type:
6911 case DW_TAG_subrange_type:
6912 case DW_TAG_typedef:
6913 /* Add a typedef symbol for the type definition, if it has a
6914 DW_AT_name. */
6915 new_symbol (die, read_type_die (die, cu), cu);
6916 break;
6917 case DW_TAG_common_block:
6918 read_common_block (die, cu);
6919 break;
6920 case DW_TAG_common_inclusion:
6921 break;
6922 case DW_TAG_namespace:
6923 processing_has_namespace_info = 1;
6924 read_namespace (die, cu);
6925 break;
6926 case DW_TAG_module:
6927 processing_has_namespace_info = 1;
6928 read_module (die, cu);
6929 break;
6930 case DW_TAG_imported_declaration:
6931 case DW_TAG_imported_module:
6932 processing_has_namespace_info = 1;
6933 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6934 || cu->language != language_fortran))
6935 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6936 dwarf_tag_name (die->tag));
6937 read_import_statement (die, cu);
6938 break;
6939
6940 case DW_TAG_imported_unit:
6941 process_imported_unit_die (die, cu);
6942 break;
6943
6944 default:
6945 new_symbol (die, NULL, cu);
6946 break;
6947 }
6948 }
6949
6950 /* A helper function for dwarf2_compute_name which determines whether DIE
6951 needs to have the name of the scope prepended to the name listed in the
6952 die. */
6953
6954 static int
6955 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6956 {
6957 struct attribute *attr;
6958
6959 switch (die->tag)
6960 {
6961 case DW_TAG_namespace:
6962 case DW_TAG_typedef:
6963 case DW_TAG_class_type:
6964 case DW_TAG_interface_type:
6965 case DW_TAG_structure_type:
6966 case DW_TAG_union_type:
6967 case DW_TAG_enumeration_type:
6968 case DW_TAG_enumerator:
6969 case DW_TAG_subprogram:
6970 case DW_TAG_member:
6971 return 1;
6972
6973 case DW_TAG_variable:
6974 case DW_TAG_constant:
6975 /* We only need to prefix "globally" visible variables. These include
6976 any variable marked with DW_AT_external or any variable that
6977 lives in a namespace. [Variables in anonymous namespaces
6978 require prefixing, but they are not DW_AT_external.] */
6979
6980 if (dwarf2_attr (die, DW_AT_specification, cu))
6981 {
6982 struct dwarf2_cu *spec_cu = cu;
6983
6984 return die_needs_namespace (die_specification (die, &spec_cu),
6985 spec_cu);
6986 }
6987
6988 attr = dwarf2_attr (die, DW_AT_external, cu);
6989 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6990 && die->parent->tag != DW_TAG_module)
6991 return 0;
6992 /* A variable in a lexical block of some kind does not need a
6993 namespace, even though in C++ such variables may be external
6994 and have a mangled name. */
6995 if (die->parent->tag == DW_TAG_lexical_block
6996 || die->parent->tag == DW_TAG_try_block
6997 || die->parent->tag == DW_TAG_catch_block
6998 || die->parent->tag == DW_TAG_subprogram)
6999 return 0;
7000 return 1;
7001
7002 default:
7003 return 0;
7004 }
7005 }
7006
7007 /* Retrieve the last character from a mem_file. */
7008
7009 static void
7010 do_ui_file_peek_last (void *object, const char *buffer, long length)
7011 {
7012 char *last_char_p = (char *) object;
7013
7014 if (length > 0)
7015 *last_char_p = buffer[length - 1];
7016 }
7017
7018 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7019 compute the physname for the object, which include a method's:
7020 - formal parameters (C++/Java),
7021 - receiver type (Go),
7022 - return type (Java).
7023
7024 The term "physname" is a bit confusing.
7025 For C++, for example, it is the demangled name.
7026 For Go, for example, it's the mangled name.
7027
7028 For Ada, return the DIE's linkage name rather than the fully qualified
7029 name. PHYSNAME is ignored..
7030
7031 The result is allocated on the objfile_obstack and canonicalized. */
7032
7033 static const char *
7034 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7035 int physname)
7036 {
7037 struct objfile *objfile = cu->objfile;
7038
7039 if (name == NULL)
7040 name = dwarf2_name (die, cu);
7041
7042 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7043 compute it by typename_concat inside GDB. */
7044 if (cu->language == language_ada
7045 || (cu->language == language_fortran && physname))
7046 {
7047 /* For Ada unit, we prefer the linkage name over the name, as
7048 the former contains the exported name, which the user expects
7049 to be able to reference. Ideally, we want the user to be able
7050 to reference this entity using either natural or linkage name,
7051 but we haven't started looking at this enhancement yet. */
7052 struct attribute *attr;
7053
7054 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7055 if (attr == NULL)
7056 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7057 if (attr && DW_STRING (attr))
7058 return DW_STRING (attr);
7059 }
7060
7061 /* These are the only languages we know how to qualify names in. */
7062 if (name != NULL
7063 && (cu->language == language_cplus || cu->language == language_java
7064 || cu->language == language_fortran))
7065 {
7066 if (die_needs_namespace (die, cu))
7067 {
7068 long length;
7069 const char *prefix;
7070 struct ui_file *buf;
7071
7072 prefix = determine_prefix (die, cu);
7073 buf = mem_fileopen ();
7074 if (*prefix != '\0')
7075 {
7076 char *prefixed_name = typename_concat (NULL, prefix, name,
7077 physname, cu);
7078
7079 fputs_unfiltered (prefixed_name, buf);
7080 xfree (prefixed_name);
7081 }
7082 else
7083 fputs_unfiltered (name, buf);
7084
7085 /* Template parameters may be specified in the DIE's DW_AT_name, or
7086 as children with DW_TAG_template_type_param or
7087 DW_TAG_value_type_param. If the latter, add them to the name
7088 here. If the name already has template parameters, then
7089 skip this step; some versions of GCC emit both, and
7090 it is more efficient to use the pre-computed name.
7091
7092 Something to keep in mind about this process: it is very
7093 unlikely, or in some cases downright impossible, to produce
7094 something that will match the mangled name of a function.
7095 If the definition of the function has the same debug info,
7096 we should be able to match up with it anyway. But fallbacks
7097 using the minimal symbol, for instance to find a method
7098 implemented in a stripped copy of libstdc++, will not work.
7099 If we do not have debug info for the definition, we will have to
7100 match them up some other way.
7101
7102 When we do name matching there is a related problem with function
7103 templates; two instantiated function templates are allowed to
7104 differ only by their return types, which we do not add here. */
7105
7106 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7107 {
7108 struct attribute *attr;
7109 struct die_info *child;
7110 int first = 1;
7111
7112 die->building_fullname = 1;
7113
7114 for (child = die->child; child != NULL; child = child->sibling)
7115 {
7116 struct type *type;
7117 LONGEST value;
7118 gdb_byte *bytes;
7119 struct dwarf2_locexpr_baton *baton;
7120 struct value *v;
7121
7122 if (child->tag != DW_TAG_template_type_param
7123 && child->tag != DW_TAG_template_value_param)
7124 continue;
7125
7126 if (first)
7127 {
7128 fputs_unfiltered ("<", buf);
7129 first = 0;
7130 }
7131 else
7132 fputs_unfiltered (", ", buf);
7133
7134 attr = dwarf2_attr (child, DW_AT_type, cu);
7135 if (attr == NULL)
7136 {
7137 complaint (&symfile_complaints,
7138 _("template parameter missing DW_AT_type"));
7139 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7140 continue;
7141 }
7142 type = die_type (child, cu);
7143
7144 if (child->tag == DW_TAG_template_type_param)
7145 {
7146 c_print_type (type, "", buf, -1, 0);
7147 continue;
7148 }
7149
7150 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7151 if (attr == NULL)
7152 {
7153 complaint (&symfile_complaints,
7154 _("template parameter missing "
7155 "DW_AT_const_value"));
7156 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7157 continue;
7158 }
7159
7160 dwarf2_const_value_attr (attr, type, name,
7161 &cu->comp_unit_obstack, cu,
7162 &value, &bytes, &baton);
7163
7164 if (TYPE_NOSIGN (type))
7165 /* GDB prints characters as NUMBER 'CHAR'. If that's
7166 changed, this can use value_print instead. */
7167 c_printchar (value, type, buf);
7168 else
7169 {
7170 struct value_print_options opts;
7171
7172 if (baton != NULL)
7173 v = dwarf2_evaluate_loc_desc (type, NULL,
7174 baton->data,
7175 baton->size,
7176 baton->per_cu);
7177 else if (bytes != NULL)
7178 {
7179 v = allocate_value (type);
7180 memcpy (value_contents_writeable (v), bytes,
7181 TYPE_LENGTH (type));
7182 }
7183 else
7184 v = value_from_longest (type, value);
7185
7186 /* Specify decimal so that we do not depend on
7187 the radix. */
7188 get_formatted_print_options (&opts, 'd');
7189 opts.raw = 1;
7190 value_print (v, buf, &opts);
7191 release_value (v);
7192 value_free (v);
7193 }
7194 }
7195
7196 die->building_fullname = 0;
7197
7198 if (!first)
7199 {
7200 /* Close the argument list, with a space if necessary
7201 (nested templates). */
7202 char last_char = '\0';
7203 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7204 if (last_char == '>')
7205 fputs_unfiltered (" >", buf);
7206 else
7207 fputs_unfiltered (">", buf);
7208 }
7209 }
7210
7211 /* For Java and C++ methods, append formal parameter type
7212 information, if PHYSNAME. */
7213
7214 if (physname && die->tag == DW_TAG_subprogram
7215 && (cu->language == language_cplus
7216 || cu->language == language_java))
7217 {
7218 struct type *type = read_type_die (die, cu);
7219
7220 c_type_print_args (type, buf, 1, cu->language);
7221
7222 if (cu->language == language_java)
7223 {
7224 /* For java, we must append the return type to method
7225 names. */
7226 if (die->tag == DW_TAG_subprogram)
7227 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7228 0, 0);
7229 }
7230 else if (cu->language == language_cplus)
7231 {
7232 /* Assume that an artificial first parameter is
7233 "this", but do not crash if it is not. RealView
7234 marks unnamed (and thus unused) parameters as
7235 artificial; there is no way to differentiate
7236 the two cases. */
7237 if (TYPE_NFIELDS (type) > 0
7238 && TYPE_FIELD_ARTIFICIAL (type, 0)
7239 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7240 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7241 0))))
7242 fputs_unfiltered (" const", buf);
7243 }
7244 }
7245
7246 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7247 &length);
7248 ui_file_delete (buf);
7249
7250 if (cu->language == language_cplus)
7251 {
7252 char *cname
7253 = dwarf2_canonicalize_name (name, cu,
7254 &objfile->objfile_obstack);
7255
7256 if (cname != NULL)
7257 name = cname;
7258 }
7259 }
7260 }
7261
7262 return name;
7263 }
7264
7265 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7266 If scope qualifiers are appropriate they will be added. The result
7267 will be allocated on the objfile_obstack, or NULL if the DIE does
7268 not have a name. NAME may either be from a previous call to
7269 dwarf2_name or NULL.
7270
7271 The output string will be canonicalized (if C++/Java). */
7272
7273 static const char *
7274 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7275 {
7276 return dwarf2_compute_name (name, die, cu, 0);
7277 }
7278
7279 /* Construct a physname for the given DIE in CU. NAME may either be
7280 from a previous call to dwarf2_name or NULL. The result will be
7281 allocated on the objfile_objstack or NULL if the DIE does not have a
7282 name.
7283
7284 The output string will be canonicalized (if C++/Java). */
7285
7286 static const char *
7287 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7288 {
7289 struct objfile *objfile = cu->objfile;
7290 struct attribute *attr;
7291 const char *retval, *mangled = NULL, *canon = NULL;
7292 struct cleanup *back_to;
7293 int need_copy = 1;
7294
7295 /* In this case dwarf2_compute_name is just a shortcut not building anything
7296 on its own. */
7297 if (!die_needs_namespace (die, cu))
7298 return dwarf2_compute_name (name, die, cu, 1);
7299
7300 back_to = make_cleanup (null_cleanup, NULL);
7301
7302 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7303 if (!attr)
7304 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7305
7306 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7307 has computed. */
7308 if (attr && DW_STRING (attr))
7309 {
7310 char *demangled;
7311
7312 mangled = DW_STRING (attr);
7313
7314 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7315 type. It is easier for GDB users to search for such functions as
7316 `name(params)' than `long name(params)'. In such case the minimal
7317 symbol names do not match the full symbol names but for template
7318 functions there is never a need to look up their definition from their
7319 declaration so the only disadvantage remains the minimal symbol
7320 variant `long name(params)' does not have the proper inferior type.
7321 */
7322
7323 if (cu->language == language_go)
7324 {
7325 /* This is a lie, but we already lie to the caller new_symbol_full.
7326 new_symbol_full assumes we return the mangled name.
7327 This just undoes that lie until things are cleaned up. */
7328 demangled = NULL;
7329 }
7330 else
7331 {
7332 demangled = cplus_demangle (mangled,
7333 (DMGL_PARAMS | DMGL_ANSI
7334 | (cu->language == language_java
7335 ? DMGL_JAVA | DMGL_RET_POSTFIX
7336 : DMGL_RET_DROP)));
7337 }
7338 if (demangled)
7339 {
7340 make_cleanup (xfree, demangled);
7341 canon = demangled;
7342 }
7343 else
7344 {
7345 canon = mangled;
7346 need_copy = 0;
7347 }
7348 }
7349
7350 if (canon == NULL || check_physname)
7351 {
7352 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7353
7354 if (canon != NULL && strcmp (physname, canon) != 0)
7355 {
7356 /* It may not mean a bug in GDB. The compiler could also
7357 compute DW_AT_linkage_name incorrectly. But in such case
7358 GDB would need to be bug-to-bug compatible. */
7359
7360 complaint (&symfile_complaints,
7361 _("Computed physname <%s> does not match demangled <%s> "
7362 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7363 physname, canon, mangled, die->offset.sect_off, objfile->name);
7364
7365 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7366 is available here - over computed PHYSNAME. It is safer
7367 against both buggy GDB and buggy compilers. */
7368
7369 retval = canon;
7370 }
7371 else
7372 {
7373 retval = physname;
7374 need_copy = 0;
7375 }
7376 }
7377 else
7378 retval = canon;
7379
7380 if (need_copy)
7381 retval = obsavestring (retval, strlen (retval),
7382 &objfile->objfile_obstack);
7383
7384 do_cleanups (back_to);
7385 return retval;
7386 }
7387
7388 /* Read the import statement specified by the given die and record it. */
7389
7390 static void
7391 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7392 {
7393 struct objfile *objfile = cu->objfile;
7394 struct attribute *import_attr;
7395 struct die_info *imported_die, *child_die;
7396 struct dwarf2_cu *imported_cu;
7397 const char *imported_name;
7398 const char *imported_name_prefix;
7399 const char *canonical_name;
7400 const char *import_alias;
7401 const char *imported_declaration = NULL;
7402 const char *import_prefix;
7403 VEC (const_char_ptr) *excludes = NULL;
7404 struct cleanup *cleanups;
7405
7406 char *temp;
7407
7408 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7409 if (import_attr == NULL)
7410 {
7411 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7412 dwarf_tag_name (die->tag));
7413 return;
7414 }
7415
7416 imported_cu = cu;
7417 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7418 imported_name = dwarf2_name (imported_die, imported_cu);
7419 if (imported_name == NULL)
7420 {
7421 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7422
7423 The import in the following code:
7424 namespace A
7425 {
7426 typedef int B;
7427 }
7428
7429 int main ()
7430 {
7431 using A::B;
7432 B b;
7433 return b;
7434 }
7435
7436 ...
7437 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7438 <52> DW_AT_decl_file : 1
7439 <53> DW_AT_decl_line : 6
7440 <54> DW_AT_import : <0x75>
7441 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7442 <59> DW_AT_name : B
7443 <5b> DW_AT_decl_file : 1
7444 <5c> DW_AT_decl_line : 2
7445 <5d> DW_AT_type : <0x6e>
7446 ...
7447 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7448 <76> DW_AT_byte_size : 4
7449 <77> DW_AT_encoding : 5 (signed)
7450
7451 imports the wrong die ( 0x75 instead of 0x58 ).
7452 This case will be ignored until the gcc bug is fixed. */
7453 return;
7454 }
7455
7456 /* Figure out the local name after import. */
7457 import_alias = dwarf2_name (die, cu);
7458
7459 /* Figure out where the statement is being imported to. */
7460 import_prefix = determine_prefix (die, cu);
7461
7462 /* Figure out what the scope of the imported die is and prepend it
7463 to the name of the imported die. */
7464 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7465
7466 if (imported_die->tag != DW_TAG_namespace
7467 && imported_die->tag != DW_TAG_module)
7468 {
7469 imported_declaration = imported_name;
7470 canonical_name = imported_name_prefix;
7471 }
7472 else if (strlen (imported_name_prefix) > 0)
7473 {
7474 temp = alloca (strlen (imported_name_prefix)
7475 + 2 + strlen (imported_name) + 1);
7476 strcpy (temp, imported_name_prefix);
7477 strcat (temp, "::");
7478 strcat (temp, imported_name);
7479 canonical_name = temp;
7480 }
7481 else
7482 canonical_name = imported_name;
7483
7484 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7485
7486 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7487 for (child_die = die->child; child_die && child_die->tag;
7488 child_die = sibling_die (child_die))
7489 {
7490 /* DWARF-4: A Fortran use statement with a “rename list” may be
7491 represented by an imported module entry with an import attribute
7492 referring to the module and owned entries corresponding to those
7493 entities that are renamed as part of being imported. */
7494
7495 if (child_die->tag != DW_TAG_imported_declaration)
7496 {
7497 complaint (&symfile_complaints,
7498 _("child DW_TAG_imported_declaration expected "
7499 "- DIE at 0x%x [in module %s]"),
7500 child_die->offset.sect_off, objfile->name);
7501 continue;
7502 }
7503
7504 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7505 if (import_attr == NULL)
7506 {
7507 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7508 dwarf_tag_name (child_die->tag));
7509 continue;
7510 }
7511
7512 imported_cu = cu;
7513 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7514 &imported_cu);
7515 imported_name = dwarf2_name (imported_die, imported_cu);
7516 if (imported_name == NULL)
7517 {
7518 complaint (&symfile_complaints,
7519 _("child DW_TAG_imported_declaration has unknown "
7520 "imported name - DIE at 0x%x [in module %s]"),
7521 child_die->offset.sect_off, objfile->name);
7522 continue;
7523 }
7524
7525 VEC_safe_push (const_char_ptr, excludes, imported_name);
7526
7527 process_die (child_die, cu);
7528 }
7529
7530 cp_add_using_directive (import_prefix,
7531 canonical_name,
7532 import_alias,
7533 imported_declaration,
7534 excludes,
7535 &objfile->objfile_obstack);
7536
7537 do_cleanups (cleanups);
7538 }
7539
7540 /* Cleanup function for handle_DW_AT_stmt_list. */
7541
7542 static void
7543 free_cu_line_header (void *arg)
7544 {
7545 struct dwarf2_cu *cu = arg;
7546
7547 free_line_header (cu->line_header);
7548 cu->line_header = NULL;
7549 }
7550
7551 static void
7552 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7553 char **name, char **comp_dir)
7554 {
7555 struct attribute *attr;
7556
7557 *name = NULL;
7558 *comp_dir = NULL;
7559
7560 /* Find the filename. Do not use dwarf2_name here, since the filename
7561 is not a source language identifier. */
7562 attr = dwarf2_attr (die, DW_AT_name, cu);
7563 if (attr)
7564 {
7565 *name = DW_STRING (attr);
7566 }
7567
7568 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7569 if (attr)
7570 *comp_dir = DW_STRING (attr);
7571 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7572 {
7573 *comp_dir = ldirname (*name);
7574 if (*comp_dir != NULL)
7575 make_cleanup (xfree, *comp_dir);
7576 }
7577 if (*comp_dir != NULL)
7578 {
7579 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7580 directory, get rid of it. */
7581 char *cp = strchr (*comp_dir, ':');
7582
7583 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7584 *comp_dir = cp + 1;
7585 }
7586
7587 if (*name == NULL)
7588 *name = "<unknown>";
7589 }
7590
7591 /* Handle DW_AT_stmt_list for a compilation unit.
7592 DIE is the DW_TAG_compile_unit die for CU.
7593 COMP_DIR is the compilation directory.
7594 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7595
7596 static void
7597 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7598 const char *comp_dir)
7599 {
7600 struct attribute *attr;
7601
7602 gdb_assert (! cu->per_cu->is_debug_types);
7603
7604 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7605 if (attr)
7606 {
7607 unsigned int line_offset = DW_UNSND (attr);
7608 struct line_header *line_header
7609 = dwarf_decode_line_header (line_offset, cu);
7610
7611 if (line_header)
7612 {
7613 cu->line_header = line_header;
7614 make_cleanup (free_cu_line_header, cu);
7615 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7616 }
7617 }
7618 }
7619
7620 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7621
7622 static void
7623 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7624 {
7625 struct objfile *objfile = dwarf2_per_objfile->objfile;
7626 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7627 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7628 CORE_ADDR highpc = ((CORE_ADDR) 0);
7629 struct attribute *attr;
7630 char *name = NULL;
7631 char *comp_dir = NULL;
7632 struct die_info *child_die;
7633 bfd *abfd = objfile->obfd;
7634 CORE_ADDR baseaddr;
7635
7636 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7637
7638 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7639
7640 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7641 from finish_block. */
7642 if (lowpc == ((CORE_ADDR) -1))
7643 lowpc = highpc;
7644 lowpc += baseaddr;
7645 highpc += baseaddr;
7646
7647 find_file_and_directory (die, cu, &name, &comp_dir);
7648
7649 prepare_one_comp_unit (cu, die, cu->language);
7650
7651 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7652 standardised yet. As a workaround for the language detection we fall
7653 back to the DW_AT_producer string. */
7654 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7655 cu->language = language_opencl;
7656
7657 /* Similar hack for Go. */
7658 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7659 set_cu_language (DW_LANG_Go, cu);
7660
7661 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7662
7663 /* Decode line number information if present. We do this before
7664 processing child DIEs, so that the line header table is available
7665 for DW_AT_decl_file. */
7666 handle_DW_AT_stmt_list (die, cu, comp_dir);
7667
7668 /* Process all dies in compilation unit. */
7669 if (die->child != NULL)
7670 {
7671 child_die = die->child;
7672 while (child_die && child_die->tag)
7673 {
7674 process_die (child_die, cu);
7675 child_die = sibling_die (child_die);
7676 }
7677 }
7678
7679 /* Decode macro information, if present. Dwarf 2 macro information
7680 refers to information in the line number info statement program
7681 header, so we can only read it if we've read the header
7682 successfully. */
7683 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7684 if (attr && cu->line_header)
7685 {
7686 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7687 complaint (&symfile_complaints,
7688 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7689
7690 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7691 }
7692 else
7693 {
7694 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7695 if (attr && cu->line_header)
7696 {
7697 unsigned int macro_offset = DW_UNSND (attr);
7698
7699 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7700 }
7701 }
7702
7703 do_cleanups (back_to);
7704 }
7705
7706 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7707 Create the set of symtabs used by this TU, or if this TU is sharing
7708 symtabs with another TU and the symtabs have already been created
7709 then restore those symtabs in the line header.
7710 We don't need the pc/line-number mapping for type units. */
7711
7712 static void
7713 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7714 {
7715 struct objfile *objfile = dwarf2_per_objfile->objfile;
7716 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7717 struct type_unit_group *tu_group;
7718 int first_time;
7719 struct line_header *lh;
7720 struct attribute *attr;
7721 unsigned int i, line_offset;
7722
7723 gdb_assert (per_cu->is_debug_types);
7724
7725 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7726
7727 /* If we're using .gdb_index (includes -readnow) then
7728 per_cu->s.type_unit_group may not have been set up yet. */
7729 if (per_cu->s.type_unit_group == NULL)
7730 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7731 tu_group = per_cu->s.type_unit_group;
7732
7733 /* If we've already processed this stmt_list there's no real need to
7734 do it again, we could fake it and just recreate the part we need
7735 (file name,index -> symtab mapping). If data shows this optimization
7736 is useful we can do it then. */
7737 first_time = tu_group->primary_symtab == NULL;
7738
7739 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7740 debug info. */
7741 lh = NULL;
7742 if (attr != NULL)
7743 {
7744 line_offset = DW_UNSND (attr);
7745 lh = dwarf_decode_line_header (line_offset, cu);
7746 }
7747 if (lh == NULL)
7748 {
7749 if (first_time)
7750 dwarf2_start_symtab (cu, "", NULL, 0);
7751 else
7752 {
7753 gdb_assert (tu_group->symtabs == NULL);
7754 restart_symtab (0);
7755 }
7756 /* Note: The primary symtab will get allocated at the end. */
7757 return;
7758 }
7759
7760 cu->line_header = lh;
7761 make_cleanup (free_cu_line_header, cu);
7762
7763 if (first_time)
7764 {
7765 dwarf2_start_symtab (cu, "", NULL, 0);
7766
7767 tu_group->num_symtabs = lh->num_file_names;
7768 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7769
7770 for (i = 0; i < lh->num_file_names; ++i)
7771 {
7772 char *dir = NULL;
7773 struct file_entry *fe = &lh->file_names[i];
7774
7775 if (fe->dir_index)
7776 dir = lh->include_dirs[fe->dir_index - 1];
7777 dwarf2_start_subfile (fe->name, dir, NULL);
7778
7779 /* Note: We don't have to watch for the main subfile here, type units
7780 don't have DW_AT_name. */
7781
7782 if (current_subfile->symtab == NULL)
7783 {
7784 /* NOTE: start_subfile will recognize when it's been passed
7785 a file it has already seen. So we can't assume there's a
7786 simple mapping from lh->file_names to subfiles,
7787 lh->file_names may contain dups. */
7788 current_subfile->symtab = allocate_symtab (current_subfile->name,
7789 objfile);
7790 }
7791
7792 fe->symtab = current_subfile->symtab;
7793 tu_group->symtabs[i] = fe->symtab;
7794 }
7795 }
7796 else
7797 {
7798 restart_symtab (0);
7799
7800 for (i = 0; i < lh->num_file_names; ++i)
7801 {
7802 struct file_entry *fe = &lh->file_names[i];
7803
7804 fe->symtab = tu_group->symtabs[i];
7805 }
7806 }
7807
7808 /* The main symtab is allocated last. Type units don't have DW_AT_name
7809 so they don't have a "real" (so to speak) symtab anyway.
7810 There is later code that will assign the main symtab to all symbols
7811 that don't have one. We need to handle the case of a symbol with a
7812 missing symtab (DW_AT_decl_file) anyway. */
7813 }
7814
7815 /* Process DW_TAG_type_unit.
7816 For TUs we want to skip the first top level sibling if it's not the
7817 actual type being defined by this TU. In this case the first top
7818 level sibling is there to provide context only. */
7819
7820 static void
7821 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7822 {
7823 struct die_info *child_die;
7824
7825 prepare_one_comp_unit (cu, die, language_minimal);
7826
7827 /* Initialize (or reinitialize) the machinery for building symtabs.
7828 We do this before processing child DIEs, so that the line header table
7829 is available for DW_AT_decl_file. */
7830 setup_type_unit_groups (die, cu);
7831
7832 if (die->child != NULL)
7833 {
7834 child_die = die->child;
7835 while (child_die && child_die->tag)
7836 {
7837 process_die (child_die, cu);
7838 child_die = sibling_die (child_die);
7839 }
7840 }
7841 }
7842 \f
7843 /* DWO files. */
7844
7845 static hashval_t
7846 hash_dwo_file (const void *item)
7847 {
7848 const struct dwo_file *dwo_file = item;
7849
7850 return htab_hash_string (dwo_file->dwo_name);
7851 }
7852
7853 static int
7854 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7855 {
7856 const struct dwo_file *lhs = item_lhs;
7857 const struct dwo_file *rhs = item_rhs;
7858
7859 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
7860 }
7861
7862 /* Allocate a hash table for DWO files. */
7863
7864 static htab_t
7865 allocate_dwo_file_hash_table (void)
7866 {
7867 struct objfile *objfile = dwarf2_per_objfile->objfile;
7868
7869 return htab_create_alloc_ex (41,
7870 hash_dwo_file,
7871 eq_dwo_file,
7872 NULL,
7873 &objfile->objfile_obstack,
7874 hashtab_obstack_allocate,
7875 dummy_obstack_deallocate);
7876 }
7877
7878 static hashval_t
7879 hash_dwo_unit (const void *item)
7880 {
7881 const struct dwo_unit *dwo_unit = item;
7882
7883 /* This drops the top 32 bits of the id, but is ok for a hash. */
7884 return dwo_unit->signature;
7885 }
7886
7887 static int
7888 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7889 {
7890 const struct dwo_unit *lhs = item_lhs;
7891 const struct dwo_unit *rhs = item_rhs;
7892
7893 /* The signature is assumed to be unique within the DWO file.
7894 So while object file CU dwo_id's always have the value zero,
7895 that's OK, assuming each object file DWO file has only one CU,
7896 and that's the rule for now. */
7897 return lhs->signature == rhs->signature;
7898 }
7899
7900 /* Allocate a hash table for DWO CUs,TUs.
7901 There is one of these tables for each of CUs,TUs for each DWO file. */
7902
7903 static htab_t
7904 allocate_dwo_unit_table (struct objfile *objfile)
7905 {
7906 /* Start out with a pretty small number.
7907 Generally DWO files contain only one CU and maybe some TUs. */
7908 return htab_create_alloc_ex (3,
7909 hash_dwo_unit,
7910 eq_dwo_unit,
7911 NULL,
7912 &objfile->objfile_obstack,
7913 hashtab_obstack_allocate,
7914 dummy_obstack_deallocate);
7915 }
7916
7917 /* This function is mapped across the sections and remembers the offset and
7918 size of each of the DWO debugging sections we are interested in. */
7919
7920 static void
7921 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
7922 {
7923 struct dwo_file *dwo_file = dwo_file_ptr;
7924 const struct dwo_section_names *names = &dwo_section_names;
7925
7926 if (section_is_p (sectp->name, &names->abbrev_dwo))
7927 {
7928 dwo_file->sections.abbrev.asection = sectp;
7929 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
7930 }
7931 else if (section_is_p (sectp->name, &names->info_dwo))
7932 {
7933 dwo_file->sections.info.asection = sectp;
7934 dwo_file->sections.info.size = bfd_get_section_size (sectp);
7935 }
7936 else if (section_is_p (sectp->name, &names->line_dwo))
7937 {
7938 dwo_file->sections.line.asection = sectp;
7939 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7940 }
7941 else if (section_is_p (sectp->name, &names->loc_dwo))
7942 {
7943 dwo_file->sections.loc.asection = sectp;
7944 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7945 }
7946 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7947 {
7948 dwo_file->sections.macinfo.asection = sectp;
7949 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7950 }
7951 else if (section_is_p (sectp->name, &names->macro_dwo))
7952 {
7953 dwo_file->sections.macro.asection = sectp;
7954 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7955 }
7956 else if (section_is_p (sectp->name, &names->str_dwo))
7957 {
7958 dwo_file->sections.str.asection = sectp;
7959 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7960 }
7961 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7962 {
7963 dwo_file->sections.str_offsets.asection = sectp;
7964 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7965 }
7966 else if (section_is_p (sectp->name, &names->types_dwo))
7967 {
7968 struct dwarf2_section_info type_section;
7969
7970 memset (&type_section, 0, sizeof (type_section));
7971 type_section.asection = sectp;
7972 type_section.size = bfd_get_section_size (sectp);
7973 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7974 &type_section);
7975 }
7976 }
7977
7978 /* Structure used to pass data to create_debug_info_hash_table_reader. */
7979
7980 struct create_dwo_info_table_data
7981 {
7982 struct dwo_file *dwo_file;
7983 htab_t cu_htab;
7984 };
7985
7986 /* die_reader_func for create_debug_info_hash_table. */
7987
7988 static void
7989 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7990 gdb_byte *info_ptr,
7991 struct die_info *comp_unit_die,
7992 int has_children,
7993 void *datap)
7994 {
7995 struct dwarf2_cu *cu = reader->cu;
7996 struct objfile *objfile = dwarf2_per_objfile->objfile;
7997 sect_offset offset = cu->per_cu->offset;
7998 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7999 struct create_dwo_info_table_data *data = datap;
8000 struct dwo_file *dwo_file = data->dwo_file;
8001 htab_t cu_htab = data->cu_htab;
8002 void **slot;
8003 struct attribute *attr;
8004 struct dwo_unit *dwo_unit;
8005
8006 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8007 if (attr == NULL)
8008 {
8009 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8010 " its dwo_id [in module %s]"),
8011 offset.sect_off, dwo_file->dwo_name);
8012 return;
8013 }
8014
8015 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8016 dwo_unit->dwo_file = dwo_file;
8017 dwo_unit->signature = DW_UNSND (attr);
8018 dwo_unit->info_or_types_section = section;
8019 dwo_unit->offset = offset;
8020 dwo_unit->length = cu->per_cu->length;
8021
8022 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8023 gdb_assert (slot != NULL);
8024 if (*slot != NULL)
8025 {
8026 const struct dwo_unit *dup_dwo_unit = *slot;
8027
8028 complaint (&symfile_complaints,
8029 _("debug entry at offset 0x%x is duplicate to the entry at"
8030 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8031 offset.sect_off, dup_dwo_unit->offset.sect_off,
8032 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8033 dwo_file->dwo_name);
8034 }
8035 else
8036 *slot = dwo_unit;
8037
8038 if (dwarf2_read_debug)
8039 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8040 offset.sect_off,
8041 phex (dwo_unit->signature,
8042 sizeof (dwo_unit->signature)));
8043 }
8044
8045 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
8046
8047 static htab_t
8048 create_debug_info_hash_table (struct dwo_file *dwo_file)
8049 {
8050 struct objfile *objfile = dwarf2_per_objfile->objfile;
8051 struct dwarf2_section_info *section = &dwo_file->sections.info;
8052 bfd *abfd;
8053 htab_t cu_htab;
8054 gdb_byte *info_ptr, *end_ptr;
8055 struct create_dwo_info_table_data create_dwo_info_table_data;
8056
8057 dwarf2_read_section (objfile, section);
8058 info_ptr = section->buffer;
8059
8060 if (info_ptr == NULL)
8061 return NULL;
8062
8063 /* We can't set abfd until now because the section may be empty or
8064 not present, in which case section->asection will be NULL. */
8065 abfd = section->asection->owner;
8066
8067 if (dwarf2_read_debug)
8068 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8069 bfd_get_filename (abfd));
8070
8071 cu_htab = allocate_dwo_unit_table (objfile);
8072
8073 create_dwo_info_table_data.dwo_file = dwo_file;
8074 create_dwo_info_table_data.cu_htab = cu_htab;
8075
8076 end_ptr = info_ptr + section->size;
8077 while (info_ptr < end_ptr)
8078 {
8079 struct dwarf2_per_cu_data per_cu;
8080
8081 memset (&per_cu, 0, sizeof (per_cu));
8082 per_cu.objfile = objfile;
8083 per_cu.is_debug_types = 0;
8084 per_cu.offset.sect_off = info_ptr - section->buffer;
8085 per_cu.info_or_types_section = section;
8086
8087 init_cutu_and_read_dies_no_follow (&per_cu,
8088 &dwo_file->sections.abbrev,
8089 dwo_file,
8090 create_debug_info_hash_table_reader,
8091 &create_dwo_info_table_data);
8092
8093 info_ptr += per_cu.length;
8094 }
8095
8096 return cu_htab;
8097 }
8098
8099 /* Subroutine of open_dwo_file to simplify it.
8100 Open the file specified by FILE_NAME and hand it off to BFD for
8101 preliminary analysis. Return a newly initialized bfd *, which
8102 includes a canonicalized copy of FILE_NAME.
8103 In case of trouble, return NULL.
8104 NOTE: This function is derived from symfile_bfd_open. */
8105
8106 static bfd *
8107 try_open_dwo_file (const char *file_name)
8108 {
8109 bfd *sym_bfd;
8110 int desc;
8111 char *absolute_name;
8112
8113 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8114 O_RDONLY | O_BINARY, &absolute_name);
8115 if (desc < 0)
8116 return NULL;
8117
8118 sym_bfd = gdb_bfd_ref (bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc));
8119 if (!sym_bfd)
8120 {
8121 xfree (absolute_name);
8122 return NULL;
8123 }
8124 bfd_set_cacheable (sym_bfd, 1);
8125
8126 if (!bfd_check_format (sym_bfd, bfd_object))
8127 {
8128 xfree (absolute_name);
8129 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8130 return NULL;
8131 }
8132
8133 return sym_bfd;
8134 }
8135
8136 /* Try to open DWO file DWO_NAME.
8137 COMP_DIR is the DW_AT_comp_dir attribute.
8138 The result is the bfd handle of the file.
8139 If there is a problem finding or opening the file, return NULL.
8140 Upon success, the canonicalized path of the file is stored in the bfd,
8141 same as symfile_bfd_open. */
8142
8143 static bfd *
8144 open_dwo_file (const char *dwo_name, const char *comp_dir)
8145 {
8146 bfd *abfd;
8147
8148 if (IS_ABSOLUTE_PATH (dwo_name))
8149 return try_open_dwo_file (dwo_name);
8150
8151 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8152
8153 if (comp_dir != NULL)
8154 {
8155 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8156
8157 /* NOTE: If comp_dir is a relative path, this will also try the
8158 search path, which seems useful. */
8159 abfd = try_open_dwo_file (path_to_try);
8160 xfree (path_to_try);
8161 if (abfd != NULL)
8162 return abfd;
8163 }
8164
8165 /* That didn't work, try debug-file-directory, which, despite its name,
8166 is a list of paths. */
8167
8168 if (*debug_file_directory == '\0')
8169 return NULL;
8170
8171 return try_open_dwo_file (dwo_name);
8172 }
8173
8174 /* Initialize the use of the DWO file specified by DWO_NAME. */
8175
8176 static struct dwo_file *
8177 init_dwo_file (const char *dwo_name, const char *comp_dir)
8178 {
8179 struct objfile *objfile = dwarf2_per_objfile->objfile;
8180 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8181 struct dwo_file);
8182 bfd *abfd;
8183 struct cleanup *cleanups;
8184
8185 if (dwarf2_read_debug)
8186 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8187
8188 abfd = open_dwo_file (dwo_name, comp_dir);
8189 if (abfd == NULL)
8190 return NULL;
8191 dwo_file->dwo_name = dwo_name;
8192 dwo_file->dwo_bfd = abfd;
8193
8194 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8195
8196 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8197
8198 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8199
8200 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8201 dwo_file->sections.types);
8202
8203 discard_cleanups (cleanups);
8204
8205 return dwo_file;
8206 }
8207
8208 /* Lookup DWO file DWO_NAME. */
8209
8210 static struct dwo_file *
8211 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8212 {
8213 struct dwo_file *dwo_file;
8214 struct dwo_file find_entry;
8215 void **slot;
8216
8217 if (dwarf2_per_objfile->dwo_files == NULL)
8218 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8219
8220 /* Have we already seen this DWO file? */
8221 find_entry.dwo_name = dwo_name;
8222 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8223
8224 /* If not, read it in and build a table of the DWOs it contains. */
8225 if (*slot == NULL)
8226 *slot = init_dwo_file (dwo_name, comp_dir);
8227
8228 /* NOTE: This will be NULL if unable to open the file. */
8229 dwo_file = *slot;
8230
8231 return dwo_file;
8232 }
8233
8234 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8235 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8236 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8237 nomenclature as TUs).
8238 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8239 (dwo_id mismatch or couldn't find the DWO file). */
8240
8241 static struct dwo_unit *
8242 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8243 char *dwo_name, const char *comp_dir,
8244 ULONGEST signature)
8245 {
8246 struct objfile *objfile = dwarf2_per_objfile->objfile;
8247 struct dwo_file *dwo_file;
8248
8249 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8250 if (dwo_file == NULL)
8251 return NULL;
8252
8253 /* Look up the DWO using its signature(dwo_id). */
8254
8255 if (dwo_file->cus != NULL)
8256 {
8257 struct dwo_unit find_dwo_cu, *dwo_cu;
8258
8259 find_dwo_cu.signature = signature;
8260 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8261
8262 if (dwo_cu != NULL)
8263 return dwo_cu;
8264 }
8265
8266 /* We didn't find it. This must mean a dwo_id mismatch. */
8267
8268 complaint (&symfile_complaints,
8269 _("Could not find DWO CU referenced by CU at offset 0x%x"
8270 " [in module %s]"),
8271 this_cu->offset.sect_off, objfile->name);
8272 return NULL;
8273 }
8274
8275 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8276 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8277 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8278 (dwo_id mismatch or couldn't find the DWO file). */
8279
8280 static struct dwo_unit *
8281 lookup_dwo_type_unit (struct signatured_type *this_tu,
8282 char *dwo_name, const char *comp_dir)
8283 {
8284 struct objfile *objfile = dwarf2_per_objfile->objfile;
8285 struct dwo_file *dwo_file;
8286
8287 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8288 if (dwo_file == NULL)
8289 return NULL;
8290
8291 /* Look up the DWO using its signature(dwo_id). */
8292
8293 if (dwo_file->tus != NULL)
8294 {
8295 struct dwo_unit find_dwo_tu, *dwo_tu;
8296
8297 find_dwo_tu.signature = this_tu->signature;
8298 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8299
8300 if (dwo_tu != NULL)
8301 return dwo_tu;
8302 }
8303
8304 /* We didn't find it. This must mean a dwo_id mismatch. */
8305
8306 complaint (&symfile_complaints,
8307 _("Could not find DWO TU referenced by TU at offset 0x%x"
8308 " [in module %s]"),
8309 this_tu->per_cu.offset.sect_off, objfile->name);
8310 return NULL;
8311 }
8312
8313 /* Free all resources associated with DWO_FILE.
8314 Close the DWO file and munmap the sections.
8315 All memory should be on the objfile obstack. */
8316
8317 static void
8318 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8319 {
8320 int ix;
8321 struct dwarf2_section_info *section;
8322
8323 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8324 gdb_bfd_unref (dwo_file->dwo_bfd);
8325
8326 munmap_section_buffer (&dwo_file->sections.abbrev);
8327 munmap_section_buffer (&dwo_file->sections.info);
8328 munmap_section_buffer (&dwo_file->sections.line);
8329 munmap_section_buffer (&dwo_file->sections.loc);
8330 munmap_section_buffer (&dwo_file->sections.str);
8331 munmap_section_buffer (&dwo_file->sections.str_offsets);
8332
8333 for (ix = 0;
8334 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
8335 ix, section);
8336 ++ix)
8337 munmap_section_buffer (section);
8338
8339 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8340 }
8341
8342 /* Wrapper for free_dwo_file for use in cleanups. */
8343
8344 static void
8345 free_dwo_file_cleanup (void *arg)
8346 {
8347 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8348 struct objfile *objfile = dwarf2_per_objfile->objfile;
8349
8350 free_dwo_file (dwo_file, objfile);
8351 }
8352
8353 /* Traversal function for free_dwo_files. */
8354
8355 static int
8356 free_dwo_file_from_slot (void **slot, void *info)
8357 {
8358 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8359 struct objfile *objfile = (struct objfile *) info;
8360
8361 free_dwo_file (dwo_file, objfile);
8362
8363 return 1;
8364 }
8365
8366 /* Free all resources associated with DWO_FILES. */
8367
8368 static void
8369 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8370 {
8371 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8372 }
8373 \f
8374 /* Read in various DIEs. */
8375
8376 /* qsort helper for inherit_abstract_dies. */
8377
8378 static int
8379 unsigned_int_compar (const void *ap, const void *bp)
8380 {
8381 unsigned int a = *(unsigned int *) ap;
8382 unsigned int b = *(unsigned int *) bp;
8383
8384 return (a > b) - (b > a);
8385 }
8386
8387 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8388 Inherit only the children of the DW_AT_abstract_origin DIE not being
8389 already referenced by DW_AT_abstract_origin from the children of the
8390 current DIE. */
8391
8392 static void
8393 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8394 {
8395 struct die_info *child_die;
8396 unsigned die_children_count;
8397 /* CU offsets which were referenced by children of the current DIE. */
8398 sect_offset *offsets;
8399 sect_offset *offsets_end, *offsetp;
8400 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8401 struct die_info *origin_die;
8402 /* Iterator of the ORIGIN_DIE children. */
8403 struct die_info *origin_child_die;
8404 struct cleanup *cleanups;
8405 struct attribute *attr;
8406 struct dwarf2_cu *origin_cu;
8407 struct pending **origin_previous_list_in_scope;
8408
8409 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8410 if (!attr)
8411 return;
8412
8413 /* Note that following die references may follow to a die in a
8414 different cu. */
8415
8416 origin_cu = cu;
8417 origin_die = follow_die_ref (die, attr, &origin_cu);
8418
8419 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8420 symbols in. */
8421 origin_previous_list_in_scope = origin_cu->list_in_scope;
8422 origin_cu->list_in_scope = cu->list_in_scope;
8423
8424 if (die->tag != origin_die->tag
8425 && !(die->tag == DW_TAG_inlined_subroutine
8426 && origin_die->tag == DW_TAG_subprogram))
8427 complaint (&symfile_complaints,
8428 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8429 die->offset.sect_off, origin_die->offset.sect_off);
8430
8431 child_die = die->child;
8432 die_children_count = 0;
8433 while (child_die && child_die->tag)
8434 {
8435 child_die = sibling_die (child_die);
8436 die_children_count++;
8437 }
8438 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8439 cleanups = make_cleanup (xfree, offsets);
8440
8441 offsets_end = offsets;
8442 child_die = die->child;
8443 while (child_die && child_die->tag)
8444 {
8445 /* For each CHILD_DIE, find the corresponding child of
8446 ORIGIN_DIE. If there is more than one layer of
8447 DW_AT_abstract_origin, follow them all; there shouldn't be,
8448 but GCC versions at least through 4.4 generate this (GCC PR
8449 40573). */
8450 struct die_info *child_origin_die = child_die;
8451 struct dwarf2_cu *child_origin_cu = cu;
8452
8453 while (1)
8454 {
8455 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8456 child_origin_cu);
8457 if (attr == NULL)
8458 break;
8459 child_origin_die = follow_die_ref (child_origin_die, attr,
8460 &child_origin_cu);
8461 }
8462
8463 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8464 counterpart may exist. */
8465 if (child_origin_die != child_die)
8466 {
8467 if (child_die->tag != child_origin_die->tag
8468 && !(child_die->tag == DW_TAG_inlined_subroutine
8469 && child_origin_die->tag == DW_TAG_subprogram))
8470 complaint (&symfile_complaints,
8471 _("Child DIE 0x%x and its abstract origin 0x%x have "
8472 "different tags"), child_die->offset.sect_off,
8473 child_origin_die->offset.sect_off);
8474 if (child_origin_die->parent != origin_die)
8475 complaint (&symfile_complaints,
8476 _("Child DIE 0x%x and its abstract origin 0x%x have "
8477 "different parents"), child_die->offset.sect_off,
8478 child_origin_die->offset.sect_off);
8479 else
8480 *offsets_end++ = child_origin_die->offset;
8481 }
8482 child_die = sibling_die (child_die);
8483 }
8484 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8485 unsigned_int_compar);
8486 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8487 if (offsetp[-1].sect_off == offsetp->sect_off)
8488 complaint (&symfile_complaints,
8489 _("Multiple children of DIE 0x%x refer "
8490 "to DIE 0x%x as their abstract origin"),
8491 die->offset.sect_off, offsetp->sect_off);
8492
8493 offsetp = offsets;
8494 origin_child_die = origin_die->child;
8495 while (origin_child_die && origin_child_die->tag)
8496 {
8497 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
8498 while (offsetp < offsets_end
8499 && offsetp->sect_off < origin_child_die->offset.sect_off)
8500 offsetp++;
8501 if (offsetp >= offsets_end
8502 || offsetp->sect_off > origin_child_die->offset.sect_off)
8503 {
8504 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
8505 process_die (origin_child_die, origin_cu);
8506 }
8507 origin_child_die = sibling_die (origin_child_die);
8508 }
8509 origin_cu->list_in_scope = origin_previous_list_in_scope;
8510
8511 do_cleanups (cleanups);
8512 }
8513
8514 static void
8515 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8516 {
8517 struct objfile *objfile = cu->objfile;
8518 struct context_stack *new;
8519 CORE_ADDR lowpc;
8520 CORE_ADDR highpc;
8521 struct die_info *child_die;
8522 struct attribute *attr, *call_line, *call_file;
8523 char *name;
8524 CORE_ADDR baseaddr;
8525 struct block *block;
8526 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8527 VEC (symbolp) *template_args = NULL;
8528 struct template_symbol *templ_func = NULL;
8529
8530 if (inlined_func)
8531 {
8532 /* If we do not have call site information, we can't show the
8533 caller of this inlined function. That's too confusing, so
8534 only use the scope for local variables. */
8535 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8536 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8537 if (call_line == NULL || call_file == NULL)
8538 {
8539 read_lexical_block_scope (die, cu);
8540 return;
8541 }
8542 }
8543
8544 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8545
8546 name = dwarf2_name (die, cu);
8547
8548 /* Ignore functions with missing or empty names. These are actually
8549 illegal according to the DWARF standard. */
8550 if (name == NULL)
8551 {
8552 complaint (&symfile_complaints,
8553 _("missing name for subprogram DIE at %d"),
8554 die->offset.sect_off);
8555 return;
8556 }
8557
8558 /* Ignore functions with missing or invalid low and high pc attributes. */
8559 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8560 {
8561 attr = dwarf2_attr (die, DW_AT_external, cu);
8562 if (!attr || !DW_UNSND (attr))
8563 complaint (&symfile_complaints,
8564 _("cannot get low and high bounds "
8565 "for subprogram DIE at %d"),
8566 die->offset.sect_off);
8567 return;
8568 }
8569
8570 lowpc += baseaddr;
8571 highpc += baseaddr;
8572
8573 /* If we have any template arguments, then we must allocate a
8574 different sort of symbol. */
8575 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8576 {
8577 if (child_die->tag == DW_TAG_template_type_param
8578 || child_die->tag == DW_TAG_template_value_param)
8579 {
8580 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8581 struct template_symbol);
8582 templ_func->base.is_cplus_template_function = 1;
8583 break;
8584 }
8585 }
8586
8587 new = push_context (0, lowpc);
8588 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8589 (struct symbol *) templ_func);
8590
8591 /* If there is a location expression for DW_AT_frame_base, record
8592 it. */
8593 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8594 if (attr)
8595 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8596 expression is being recorded directly in the function's symbol
8597 and not in a separate frame-base object. I guess this hack is
8598 to avoid adding some sort of frame-base adjunct/annex to the
8599 function's symbol :-(. The problem with doing this is that it
8600 results in a function symbol with a location expression that
8601 has nothing to do with the location of the function, ouch! The
8602 relationship should be: a function's symbol has-a frame base; a
8603 frame-base has-a location expression. */
8604 dwarf2_symbol_mark_computed (attr, new->name, cu);
8605
8606 cu->list_in_scope = &local_symbols;
8607
8608 if (die->child != NULL)
8609 {
8610 child_die = die->child;
8611 while (child_die && child_die->tag)
8612 {
8613 if (child_die->tag == DW_TAG_template_type_param
8614 || child_die->tag == DW_TAG_template_value_param)
8615 {
8616 struct symbol *arg = new_symbol (child_die, NULL, cu);
8617
8618 if (arg != NULL)
8619 VEC_safe_push (symbolp, template_args, arg);
8620 }
8621 else
8622 process_die (child_die, cu);
8623 child_die = sibling_die (child_die);
8624 }
8625 }
8626
8627 inherit_abstract_dies (die, cu);
8628
8629 /* If we have a DW_AT_specification, we might need to import using
8630 directives from the context of the specification DIE. See the
8631 comment in determine_prefix. */
8632 if (cu->language == language_cplus
8633 && dwarf2_attr (die, DW_AT_specification, cu))
8634 {
8635 struct dwarf2_cu *spec_cu = cu;
8636 struct die_info *spec_die = die_specification (die, &spec_cu);
8637
8638 while (spec_die)
8639 {
8640 child_die = spec_die->child;
8641 while (child_die && child_die->tag)
8642 {
8643 if (child_die->tag == DW_TAG_imported_module)
8644 process_die (child_die, spec_cu);
8645 child_die = sibling_die (child_die);
8646 }
8647
8648 /* In some cases, GCC generates specification DIEs that
8649 themselves contain DW_AT_specification attributes. */
8650 spec_die = die_specification (spec_die, &spec_cu);
8651 }
8652 }
8653
8654 new = pop_context ();
8655 /* Make a block for the local symbols within. */
8656 block = finish_block (new->name, &local_symbols, new->old_blocks,
8657 lowpc, highpc, objfile);
8658
8659 /* For C++, set the block's scope. */
8660 if (cu->language == language_cplus || cu->language == language_fortran)
8661 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8662 determine_prefix (die, cu),
8663 processing_has_namespace_info);
8664
8665 /* If we have address ranges, record them. */
8666 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8667
8668 /* Attach template arguments to function. */
8669 if (! VEC_empty (symbolp, template_args))
8670 {
8671 gdb_assert (templ_func != NULL);
8672
8673 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8674 templ_func->template_arguments
8675 = obstack_alloc (&objfile->objfile_obstack,
8676 (templ_func->n_template_arguments
8677 * sizeof (struct symbol *)));
8678 memcpy (templ_func->template_arguments,
8679 VEC_address (symbolp, template_args),
8680 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8681 VEC_free (symbolp, template_args);
8682 }
8683
8684 /* In C++, we can have functions nested inside functions (e.g., when
8685 a function declares a class that has methods). This means that
8686 when we finish processing a function scope, we may need to go
8687 back to building a containing block's symbol lists. */
8688 local_symbols = new->locals;
8689 param_symbols = new->params;
8690 using_directives = new->using_directives;
8691
8692 /* If we've finished processing a top-level function, subsequent
8693 symbols go in the file symbol list. */
8694 if (outermost_context_p ())
8695 cu->list_in_scope = &file_symbols;
8696 }
8697
8698 /* Process all the DIES contained within a lexical block scope. Start
8699 a new scope, process the dies, and then close the scope. */
8700
8701 static void
8702 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8703 {
8704 struct objfile *objfile = cu->objfile;
8705 struct context_stack *new;
8706 CORE_ADDR lowpc, highpc;
8707 struct die_info *child_die;
8708 CORE_ADDR baseaddr;
8709
8710 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8711
8712 /* Ignore blocks with missing or invalid low and high pc attributes. */
8713 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8714 as multiple lexical blocks? Handling children in a sane way would
8715 be nasty. Might be easier to properly extend generic blocks to
8716 describe ranges. */
8717 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8718 return;
8719 lowpc += baseaddr;
8720 highpc += baseaddr;
8721
8722 push_context (0, lowpc);
8723 if (die->child != NULL)
8724 {
8725 child_die = die->child;
8726 while (child_die && child_die->tag)
8727 {
8728 process_die (child_die, cu);
8729 child_die = sibling_die (child_die);
8730 }
8731 }
8732 new = pop_context ();
8733
8734 if (local_symbols != NULL || using_directives != NULL)
8735 {
8736 struct block *block
8737 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8738 highpc, objfile);
8739
8740 /* Note that recording ranges after traversing children, as we
8741 do here, means that recording a parent's ranges entails
8742 walking across all its children's ranges as they appear in
8743 the address map, which is quadratic behavior.
8744
8745 It would be nicer to record the parent's ranges before
8746 traversing its children, simply overriding whatever you find
8747 there. But since we don't even decide whether to create a
8748 block until after we've traversed its children, that's hard
8749 to do. */
8750 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8751 }
8752 local_symbols = new->locals;
8753 using_directives = new->using_directives;
8754 }
8755
8756 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8757
8758 static void
8759 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8760 {
8761 struct objfile *objfile = cu->objfile;
8762 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8763 CORE_ADDR pc, baseaddr;
8764 struct attribute *attr;
8765 struct call_site *call_site, call_site_local;
8766 void **slot;
8767 int nparams;
8768 struct die_info *child_die;
8769
8770 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8771
8772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8773 if (!attr)
8774 {
8775 complaint (&symfile_complaints,
8776 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8777 "DIE 0x%x [in module %s]"),
8778 die->offset.sect_off, objfile->name);
8779 return;
8780 }
8781 pc = DW_ADDR (attr) + baseaddr;
8782
8783 if (cu->call_site_htab == NULL)
8784 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8785 NULL, &objfile->objfile_obstack,
8786 hashtab_obstack_allocate, NULL);
8787 call_site_local.pc = pc;
8788 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8789 if (*slot != NULL)
8790 {
8791 complaint (&symfile_complaints,
8792 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8793 "DIE 0x%x [in module %s]"),
8794 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8795 return;
8796 }
8797
8798 /* Count parameters at the caller. */
8799
8800 nparams = 0;
8801 for (child_die = die->child; child_die && child_die->tag;
8802 child_die = sibling_die (child_die))
8803 {
8804 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8805 {
8806 complaint (&symfile_complaints,
8807 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8808 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8809 child_die->tag, child_die->offset.sect_off, objfile->name);
8810 continue;
8811 }
8812
8813 nparams++;
8814 }
8815
8816 call_site = obstack_alloc (&objfile->objfile_obstack,
8817 (sizeof (*call_site)
8818 + (sizeof (*call_site->parameter)
8819 * (nparams - 1))));
8820 *slot = call_site;
8821 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8822 call_site->pc = pc;
8823
8824 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8825 {
8826 struct die_info *func_die;
8827
8828 /* Skip also over DW_TAG_inlined_subroutine. */
8829 for (func_die = die->parent;
8830 func_die && func_die->tag != DW_TAG_subprogram
8831 && func_die->tag != DW_TAG_subroutine_type;
8832 func_die = func_die->parent);
8833
8834 /* DW_AT_GNU_all_call_sites is a superset
8835 of DW_AT_GNU_all_tail_call_sites. */
8836 if (func_die
8837 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8838 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8839 {
8840 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8841 not complete. But keep CALL_SITE for look ups via call_site_htab,
8842 both the initial caller containing the real return address PC and
8843 the final callee containing the current PC of a chain of tail
8844 calls do not need to have the tail call list complete. But any
8845 function candidate for a virtual tail call frame searched via
8846 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8847 determined unambiguously. */
8848 }
8849 else
8850 {
8851 struct type *func_type = NULL;
8852
8853 if (func_die)
8854 func_type = get_die_type (func_die, cu);
8855 if (func_type != NULL)
8856 {
8857 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
8858
8859 /* Enlist this call site to the function. */
8860 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
8861 TYPE_TAIL_CALL_LIST (func_type) = call_site;
8862 }
8863 else
8864 complaint (&symfile_complaints,
8865 _("Cannot find function owning DW_TAG_GNU_call_site "
8866 "DIE 0x%x [in module %s]"),
8867 die->offset.sect_off, objfile->name);
8868 }
8869 }
8870
8871 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
8872 if (attr == NULL)
8873 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8874 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
8875 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
8876 /* Keep NULL DWARF_BLOCK. */;
8877 else if (attr_form_is_block (attr))
8878 {
8879 struct dwarf2_locexpr_baton *dlbaton;
8880
8881 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
8882 dlbaton->data = DW_BLOCK (attr)->data;
8883 dlbaton->size = DW_BLOCK (attr)->size;
8884 dlbaton->per_cu = cu->per_cu;
8885
8886 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
8887 }
8888 else if (is_ref_attr (attr))
8889 {
8890 struct dwarf2_cu *target_cu = cu;
8891 struct die_info *target_die;
8892
8893 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
8894 gdb_assert (target_cu->objfile == objfile);
8895 if (die_is_declaration (target_die, target_cu))
8896 {
8897 const char *target_physname;
8898
8899 target_physname = dwarf2_physname (NULL, target_die, target_cu);
8900 if (target_physname == NULL)
8901 complaint (&symfile_complaints,
8902 _("DW_AT_GNU_call_site_target target DIE has invalid "
8903 "physname, for referencing DIE 0x%x [in module %s]"),
8904 die->offset.sect_off, objfile->name);
8905 else
8906 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
8907 }
8908 else
8909 {
8910 CORE_ADDR lowpc;
8911
8912 /* DW_AT_entry_pc should be preferred. */
8913 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
8914 complaint (&symfile_complaints,
8915 _("DW_AT_GNU_call_site_target target DIE has invalid "
8916 "low pc, for referencing DIE 0x%x [in module %s]"),
8917 die->offset.sect_off, objfile->name);
8918 else
8919 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
8920 }
8921 }
8922 else
8923 complaint (&symfile_complaints,
8924 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
8925 "block nor reference, for DIE 0x%x [in module %s]"),
8926 die->offset.sect_off, objfile->name);
8927
8928 call_site->per_cu = cu->per_cu;
8929
8930 for (child_die = die->child;
8931 child_die && child_die->tag;
8932 child_die = sibling_die (child_die))
8933 {
8934 struct call_site_parameter *parameter;
8935 struct attribute *loc, *origin;
8936
8937 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8938 {
8939 /* Already printed the complaint above. */
8940 continue;
8941 }
8942
8943 gdb_assert (call_site->parameter_count < nparams);
8944 parameter = &call_site->parameter[call_site->parameter_count];
8945
8946 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8947 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8948 register is contained in DW_AT_GNU_call_site_value. */
8949
8950 loc = dwarf2_attr (child_die, DW_AT_location, cu);
8951 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8952 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8953 {
8954 sect_offset offset;
8955
8956 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8957 offset = dwarf2_get_ref_die_offset (origin);
8958 if (!offset_in_cu_p (&cu->header, offset))
8959 {
8960 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
8961 binding can be done only inside one CU. Such referenced DIE
8962 therefore cannot be even moved to DW_TAG_partial_unit. */
8963 complaint (&symfile_complaints,
8964 _("DW_AT_abstract_origin offset is not in CU for "
8965 "DW_TAG_GNU_call_site child DIE 0x%x "
8966 "[in module %s]"),
8967 child_die->offset.sect_off, objfile->name);
8968 continue;
8969 }
8970 parameter->u.param_offset.cu_off = (offset.sect_off
8971 - cu->header.offset.sect_off);
8972 }
8973 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
8974 {
8975 complaint (&symfile_complaints,
8976 _("No DW_FORM_block* DW_AT_location for "
8977 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8978 child_die->offset.sect_off, objfile->name);
8979 continue;
8980 }
8981 else
8982 {
8983 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8984 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8985 if (parameter->u.dwarf_reg != -1)
8986 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8987 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8988 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8989 &parameter->u.fb_offset))
8990 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8991 else
8992 {
8993 complaint (&symfile_complaints,
8994 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8995 "for DW_FORM_block* DW_AT_location is supported for "
8996 "DW_TAG_GNU_call_site child DIE 0x%x "
8997 "[in module %s]"),
8998 child_die->offset.sect_off, objfile->name);
8999 continue;
9000 }
9001 }
9002
9003 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9004 if (!attr_form_is_block (attr))
9005 {
9006 complaint (&symfile_complaints,
9007 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9008 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9009 child_die->offset.sect_off, objfile->name);
9010 continue;
9011 }
9012 parameter->value = DW_BLOCK (attr)->data;
9013 parameter->value_size = DW_BLOCK (attr)->size;
9014
9015 /* Parameters are not pre-cleared by memset above. */
9016 parameter->data_value = NULL;
9017 parameter->data_value_size = 0;
9018 call_site->parameter_count++;
9019
9020 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9021 if (attr)
9022 {
9023 if (!attr_form_is_block (attr))
9024 complaint (&symfile_complaints,
9025 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9026 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9027 child_die->offset.sect_off, objfile->name);
9028 else
9029 {
9030 parameter->data_value = DW_BLOCK (attr)->data;
9031 parameter->data_value_size = DW_BLOCK (attr)->size;
9032 }
9033 }
9034 }
9035 }
9036
9037 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9038 Return 1 if the attributes are present and valid, otherwise, return 0.
9039 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9040
9041 static int
9042 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9043 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9044 struct partial_symtab *ranges_pst)
9045 {
9046 struct objfile *objfile = cu->objfile;
9047 struct comp_unit_head *cu_header = &cu->header;
9048 bfd *obfd = objfile->obfd;
9049 unsigned int addr_size = cu_header->addr_size;
9050 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9051 /* Base address selection entry. */
9052 CORE_ADDR base;
9053 int found_base;
9054 unsigned int dummy;
9055 gdb_byte *buffer;
9056 CORE_ADDR marker;
9057 int low_set;
9058 CORE_ADDR low = 0;
9059 CORE_ADDR high = 0;
9060 CORE_ADDR baseaddr;
9061
9062 found_base = cu->base_known;
9063 base = cu->base_address;
9064
9065 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9066 if (offset >= dwarf2_per_objfile->ranges.size)
9067 {
9068 complaint (&symfile_complaints,
9069 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9070 offset);
9071 return 0;
9072 }
9073 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9074
9075 /* Read in the largest possible address. */
9076 marker = read_address (obfd, buffer, cu, &dummy);
9077 if ((marker & mask) == mask)
9078 {
9079 /* If we found the largest possible address, then
9080 read the base address. */
9081 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9082 buffer += 2 * addr_size;
9083 offset += 2 * addr_size;
9084 found_base = 1;
9085 }
9086
9087 low_set = 0;
9088
9089 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9090
9091 while (1)
9092 {
9093 CORE_ADDR range_beginning, range_end;
9094
9095 range_beginning = read_address (obfd, buffer, cu, &dummy);
9096 buffer += addr_size;
9097 range_end = read_address (obfd, buffer, cu, &dummy);
9098 buffer += addr_size;
9099 offset += 2 * addr_size;
9100
9101 /* An end of list marker is a pair of zero addresses. */
9102 if (range_beginning == 0 && range_end == 0)
9103 /* Found the end of list entry. */
9104 break;
9105
9106 /* Each base address selection entry is a pair of 2 values.
9107 The first is the largest possible address, the second is
9108 the base address. Check for a base address here. */
9109 if ((range_beginning & mask) == mask)
9110 {
9111 /* If we found the largest possible address, then
9112 read the base address. */
9113 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9114 found_base = 1;
9115 continue;
9116 }
9117
9118 if (!found_base)
9119 {
9120 /* We have no valid base address for the ranges
9121 data. */
9122 complaint (&symfile_complaints,
9123 _("Invalid .debug_ranges data (no base address)"));
9124 return 0;
9125 }
9126
9127 if (range_beginning > range_end)
9128 {
9129 /* Inverted range entries are invalid. */
9130 complaint (&symfile_complaints,
9131 _("Invalid .debug_ranges data (inverted range)"));
9132 return 0;
9133 }
9134
9135 /* Empty range entries have no effect. */
9136 if (range_beginning == range_end)
9137 continue;
9138
9139 range_beginning += base;
9140 range_end += base;
9141
9142 if (ranges_pst != NULL)
9143 addrmap_set_empty (objfile->psymtabs_addrmap,
9144 range_beginning + baseaddr,
9145 range_end - 1 + baseaddr,
9146 ranges_pst);
9147
9148 /* FIXME: This is recording everything as a low-high
9149 segment of consecutive addresses. We should have a
9150 data structure for discontiguous block ranges
9151 instead. */
9152 if (! low_set)
9153 {
9154 low = range_beginning;
9155 high = range_end;
9156 low_set = 1;
9157 }
9158 else
9159 {
9160 if (range_beginning < low)
9161 low = range_beginning;
9162 if (range_end > high)
9163 high = range_end;
9164 }
9165 }
9166
9167 if (! low_set)
9168 /* If the first entry is an end-of-list marker, the range
9169 describes an empty scope, i.e. no instructions. */
9170 return 0;
9171
9172 if (low_return)
9173 *low_return = low;
9174 if (high_return)
9175 *high_return = high;
9176 return 1;
9177 }
9178
9179 /* Get low and high pc attributes from a die. Return 1 if the attributes
9180 are present and valid, otherwise, return 0. Return -1 if the range is
9181 discontinuous, i.e. derived from DW_AT_ranges information. */
9182
9183 static int
9184 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9185 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9186 struct partial_symtab *pst)
9187 {
9188 struct attribute *attr;
9189 struct attribute *attr_high;
9190 CORE_ADDR low = 0;
9191 CORE_ADDR high = 0;
9192 int ret = 0;
9193
9194 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9195 if (attr_high)
9196 {
9197 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9198 if (attr)
9199 {
9200 low = DW_ADDR (attr);
9201 if (attr_high->form == DW_FORM_addr
9202 || attr_high->form == DW_FORM_GNU_addr_index)
9203 high = DW_ADDR (attr_high);
9204 else
9205 high = low + DW_UNSND (attr_high);
9206 }
9207 else
9208 /* Found high w/o low attribute. */
9209 return 0;
9210
9211 /* Found consecutive range of addresses. */
9212 ret = 1;
9213 }
9214 else
9215 {
9216 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9217 if (attr != NULL)
9218 {
9219 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9220
9221 /* Value of the DW_AT_ranges attribute is the offset in the
9222 .debug_ranges section. */
9223 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9224 return 0;
9225 /* Found discontinuous range of addresses. */
9226 ret = -1;
9227 }
9228 }
9229
9230 /* read_partial_die has also the strict LOW < HIGH requirement. */
9231 if (high <= low)
9232 return 0;
9233
9234 /* When using the GNU linker, .gnu.linkonce. sections are used to
9235 eliminate duplicate copies of functions and vtables and such.
9236 The linker will arbitrarily choose one and discard the others.
9237 The AT_*_pc values for such functions refer to local labels in
9238 these sections. If the section from that file was discarded, the
9239 labels are not in the output, so the relocs get a value of 0.
9240 If this is a discarded function, mark the pc bounds as invalid,
9241 so that GDB will ignore it. */
9242 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9243 return 0;
9244
9245 *lowpc = low;
9246 if (highpc)
9247 *highpc = high;
9248 return ret;
9249 }
9250
9251 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9252 its low and high PC addresses. Do nothing if these addresses could not
9253 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9254 and HIGHPC to the high address if greater than HIGHPC. */
9255
9256 static void
9257 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9258 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9259 struct dwarf2_cu *cu)
9260 {
9261 CORE_ADDR low, high;
9262 struct die_info *child = die->child;
9263
9264 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9265 {
9266 *lowpc = min (*lowpc, low);
9267 *highpc = max (*highpc, high);
9268 }
9269
9270 /* If the language does not allow nested subprograms (either inside
9271 subprograms or lexical blocks), we're done. */
9272 if (cu->language != language_ada)
9273 return;
9274
9275 /* Check all the children of the given DIE. If it contains nested
9276 subprograms, then check their pc bounds. Likewise, we need to
9277 check lexical blocks as well, as they may also contain subprogram
9278 definitions. */
9279 while (child && child->tag)
9280 {
9281 if (child->tag == DW_TAG_subprogram
9282 || child->tag == DW_TAG_lexical_block)
9283 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9284 child = sibling_die (child);
9285 }
9286 }
9287
9288 /* Get the low and high pc's represented by the scope DIE, and store
9289 them in *LOWPC and *HIGHPC. If the correct values can't be
9290 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9291
9292 static void
9293 get_scope_pc_bounds (struct die_info *die,
9294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9295 struct dwarf2_cu *cu)
9296 {
9297 CORE_ADDR best_low = (CORE_ADDR) -1;
9298 CORE_ADDR best_high = (CORE_ADDR) 0;
9299 CORE_ADDR current_low, current_high;
9300
9301 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9302 {
9303 best_low = current_low;
9304 best_high = current_high;
9305 }
9306 else
9307 {
9308 struct die_info *child = die->child;
9309
9310 while (child && child->tag)
9311 {
9312 switch (child->tag) {
9313 case DW_TAG_subprogram:
9314 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9315 break;
9316 case DW_TAG_namespace:
9317 case DW_TAG_module:
9318 /* FIXME: carlton/2004-01-16: Should we do this for
9319 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9320 that current GCC's always emit the DIEs corresponding
9321 to definitions of methods of classes as children of a
9322 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9323 the DIEs giving the declarations, which could be
9324 anywhere). But I don't see any reason why the
9325 standards says that they have to be there. */
9326 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9327
9328 if (current_low != ((CORE_ADDR) -1))
9329 {
9330 best_low = min (best_low, current_low);
9331 best_high = max (best_high, current_high);
9332 }
9333 break;
9334 default:
9335 /* Ignore. */
9336 break;
9337 }
9338
9339 child = sibling_die (child);
9340 }
9341 }
9342
9343 *lowpc = best_low;
9344 *highpc = best_high;
9345 }
9346
9347 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9348 in DIE. */
9349
9350 static void
9351 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9352 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9353 {
9354 struct objfile *objfile = cu->objfile;
9355 struct attribute *attr;
9356 struct attribute *attr_high;
9357
9358 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9359 if (attr_high)
9360 {
9361 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9362 if (attr)
9363 {
9364 CORE_ADDR low = DW_ADDR (attr);
9365 CORE_ADDR high;
9366 if (attr_high->form == DW_FORM_addr
9367 || attr_high->form == DW_FORM_GNU_addr_index)
9368 high = DW_ADDR (attr_high);
9369 else
9370 high = low + DW_UNSND (attr_high);
9371
9372 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9373 }
9374 }
9375
9376 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9377 if (attr)
9378 {
9379 bfd *obfd = objfile->obfd;
9380
9381 /* The value of the DW_AT_ranges attribute is the offset of the
9382 address range list in the .debug_ranges section. */
9383 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9384 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9385
9386 /* For some target architectures, but not others, the
9387 read_address function sign-extends the addresses it returns.
9388 To recognize base address selection entries, we need a
9389 mask. */
9390 unsigned int addr_size = cu->header.addr_size;
9391 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9392
9393 /* The base address, to which the next pair is relative. Note
9394 that this 'base' is a DWARF concept: most entries in a range
9395 list are relative, to reduce the number of relocs against the
9396 debugging information. This is separate from this function's
9397 'baseaddr' argument, which GDB uses to relocate debugging
9398 information from a shared library based on the address at
9399 which the library was loaded. */
9400 CORE_ADDR base = cu->base_address;
9401 int base_known = cu->base_known;
9402
9403 gdb_assert (dwarf2_per_objfile->ranges.readin);
9404 if (offset >= dwarf2_per_objfile->ranges.size)
9405 {
9406 complaint (&symfile_complaints,
9407 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9408 offset);
9409 return;
9410 }
9411
9412 for (;;)
9413 {
9414 unsigned int bytes_read;
9415 CORE_ADDR start, end;
9416
9417 start = read_address (obfd, buffer, cu, &bytes_read);
9418 buffer += bytes_read;
9419 end = read_address (obfd, buffer, cu, &bytes_read);
9420 buffer += bytes_read;
9421
9422 /* Did we find the end of the range list? */
9423 if (start == 0 && end == 0)
9424 break;
9425
9426 /* Did we find a base address selection entry? */
9427 else if ((start & base_select_mask) == base_select_mask)
9428 {
9429 base = end;
9430 base_known = 1;
9431 }
9432
9433 /* We found an ordinary address range. */
9434 else
9435 {
9436 if (!base_known)
9437 {
9438 complaint (&symfile_complaints,
9439 _("Invalid .debug_ranges data "
9440 "(no base address)"));
9441 return;
9442 }
9443
9444 if (start > end)
9445 {
9446 /* Inverted range entries are invalid. */
9447 complaint (&symfile_complaints,
9448 _("Invalid .debug_ranges data "
9449 "(inverted range)"));
9450 return;
9451 }
9452
9453 /* Empty range entries have no effect. */
9454 if (start == end)
9455 continue;
9456
9457 record_block_range (block,
9458 baseaddr + base + start,
9459 baseaddr + base + end - 1);
9460 }
9461 }
9462 }
9463 }
9464
9465 /* Check whether the producer field indicates either of GCC < 4.6, or the
9466 Intel C/C++ compiler, and cache the result in CU. */
9467
9468 static void
9469 check_producer (struct dwarf2_cu *cu)
9470 {
9471 const char *cs;
9472 int major, minor, release;
9473
9474 if (cu->producer == NULL)
9475 {
9476 /* For unknown compilers expect their behavior is DWARF version
9477 compliant.
9478
9479 GCC started to support .debug_types sections by -gdwarf-4 since
9480 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9481 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9482 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9483 interpreted incorrectly by GDB now - GCC PR debug/48229. */
9484 }
9485 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9486 {
9487 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9488
9489 cs = &cu->producer[strlen ("GNU ")];
9490 while (*cs && !isdigit (*cs))
9491 cs++;
9492 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9493 {
9494 /* Not recognized as GCC. */
9495 }
9496 else
9497 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9498 }
9499 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9500 cu->producer_is_icc = 1;
9501 else
9502 {
9503 /* For other non-GCC compilers, expect their behavior is DWARF version
9504 compliant. */
9505 }
9506
9507 cu->checked_producer = 1;
9508 }
9509
9510 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9511 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9512 during 4.6.0 experimental. */
9513
9514 static int
9515 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9516 {
9517 if (!cu->checked_producer)
9518 check_producer (cu);
9519
9520 return cu->producer_is_gxx_lt_4_6;
9521 }
9522
9523 /* Return the default accessibility type if it is not overriden by
9524 DW_AT_accessibility. */
9525
9526 static enum dwarf_access_attribute
9527 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9528 {
9529 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9530 {
9531 /* The default DWARF 2 accessibility for members is public, the default
9532 accessibility for inheritance is private. */
9533
9534 if (die->tag != DW_TAG_inheritance)
9535 return DW_ACCESS_public;
9536 else
9537 return DW_ACCESS_private;
9538 }
9539 else
9540 {
9541 /* DWARF 3+ defines the default accessibility a different way. The same
9542 rules apply now for DW_TAG_inheritance as for the members and it only
9543 depends on the container kind. */
9544
9545 if (die->parent->tag == DW_TAG_class_type)
9546 return DW_ACCESS_private;
9547 else
9548 return DW_ACCESS_public;
9549 }
9550 }
9551
9552 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9553 offset. If the attribute was not found return 0, otherwise return
9554 1. If it was found but could not properly be handled, set *OFFSET
9555 to 0. */
9556
9557 static int
9558 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9559 LONGEST *offset)
9560 {
9561 struct attribute *attr;
9562
9563 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9564 if (attr != NULL)
9565 {
9566 *offset = 0;
9567
9568 /* Note that we do not check for a section offset first here.
9569 This is because DW_AT_data_member_location is new in DWARF 4,
9570 so if we see it, we can assume that a constant form is really
9571 a constant and not a section offset. */
9572 if (attr_form_is_constant (attr))
9573 *offset = dwarf2_get_attr_constant_value (attr, 0);
9574 else if (attr_form_is_section_offset (attr))
9575 dwarf2_complex_location_expr_complaint ();
9576 else if (attr_form_is_block (attr))
9577 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9578 else
9579 dwarf2_complex_location_expr_complaint ();
9580
9581 return 1;
9582 }
9583
9584 return 0;
9585 }
9586
9587 /* Add an aggregate field to the field list. */
9588
9589 static void
9590 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9591 struct dwarf2_cu *cu)
9592 {
9593 struct objfile *objfile = cu->objfile;
9594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9595 struct nextfield *new_field;
9596 struct attribute *attr;
9597 struct field *fp;
9598 char *fieldname = "";
9599
9600 /* Allocate a new field list entry and link it in. */
9601 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9602 make_cleanup (xfree, new_field);
9603 memset (new_field, 0, sizeof (struct nextfield));
9604
9605 if (die->tag == DW_TAG_inheritance)
9606 {
9607 new_field->next = fip->baseclasses;
9608 fip->baseclasses = new_field;
9609 }
9610 else
9611 {
9612 new_field->next = fip->fields;
9613 fip->fields = new_field;
9614 }
9615 fip->nfields++;
9616
9617 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9618 if (attr)
9619 new_field->accessibility = DW_UNSND (attr);
9620 else
9621 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9622 if (new_field->accessibility != DW_ACCESS_public)
9623 fip->non_public_fields = 1;
9624
9625 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9626 if (attr)
9627 new_field->virtuality = DW_UNSND (attr);
9628 else
9629 new_field->virtuality = DW_VIRTUALITY_none;
9630
9631 fp = &new_field->field;
9632
9633 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9634 {
9635 LONGEST offset;
9636
9637 /* Data member other than a C++ static data member. */
9638
9639 /* Get type of field. */
9640 fp->type = die_type (die, cu);
9641
9642 SET_FIELD_BITPOS (*fp, 0);
9643
9644 /* Get bit size of field (zero if none). */
9645 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9646 if (attr)
9647 {
9648 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9649 }
9650 else
9651 {
9652 FIELD_BITSIZE (*fp) = 0;
9653 }
9654
9655 /* Get bit offset of field. */
9656 if (handle_data_member_location (die, cu, &offset))
9657 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9658 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9659 if (attr)
9660 {
9661 if (gdbarch_bits_big_endian (gdbarch))
9662 {
9663 /* For big endian bits, the DW_AT_bit_offset gives the
9664 additional bit offset from the MSB of the containing
9665 anonymous object to the MSB of the field. We don't
9666 have to do anything special since we don't need to
9667 know the size of the anonymous object. */
9668 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9669 }
9670 else
9671 {
9672 /* For little endian bits, compute the bit offset to the
9673 MSB of the anonymous object, subtract off the number of
9674 bits from the MSB of the field to the MSB of the
9675 object, and then subtract off the number of bits of
9676 the field itself. The result is the bit offset of
9677 the LSB of the field. */
9678 int anonymous_size;
9679 int bit_offset = DW_UNSND (attr);
9680
9681 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9682 if (attr)
9683 {
9684 /* The size of the anonymous object containing
9685 the bit field is explicit, so use the
9686 indicated size (in bytes). */
9687 anonymous_size = DW_UNSND (attr);
9688 }
9689 else
9690 {
9691 /* The size of the anonymous object containing
9692 the bit field must be inferred from the type
9693 attribute of the data member containing the
9694 bit field. */
9695 anonymous_size = TYPE_LENGTH (fp->type);
9696 }
9697 SET_FIELD_BITPOS (*fp,
9698 (FIELD_BITPOS (*fp)
9699 + anonymous_size * bits_per_byte
9700 - bit_offset - FIELD_BITSIZE (*fp)));
9701 }
9702 }
9703
9704 /* Get name of field. */
9705 fieldname = dwarf2_name (die, cu);
9706 if (fieldname == NULL)
9707 fieldname = "";
9708
9709 /* The name is already allocated along with this objfile, so we don't
9710 need to duplicate it for the type. */
9711 fp->name = fieldname;
9712
9713 /* Change accessibility for artificial fields (e.g. virtual table
9714 pointer or virtual base class pointer) to private. */
9715 if (dwarf2_attr (die, DW_AT_artificial, cu))
9716 {
9717 FIELD_ARTIFICIAL (*fp) = 1;
9718 new_field->accessibility = DW_ACCESS_private;
9719 fip->non_public_fields = 1;
9720 }
9721 }
9722 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9723 {
9724 /* C++ static member. */
9725
9726 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9727 is a declaration, but all versions of G++ as of this writing
9728 (so through at least 3.2.1) incorrectly generate
9729 DW_TAG_variable tags. */
9730
9731 const char *physname;
9732
9733 /* Get name of field. */
9734 fieldname = dwarf2_name (die, cu);
9735 if (fieldname == NULL)
9736 return;
9737
9738 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9739 if (attr
9740 /* Only create a symbol if this is an external value.
9741 new_symbol checks this and puts the value in the global symbol
9742 table, which we want. If it is not external, new_symbol
9743 will try to put the value in cu->list_in_scope which is wrong. */
9744 && dwarf2_flag_true_p (die, DW_AT_external, cu))
9745 {
9746 /* A static const member, not much different than an enum as far as
9747 we're concerned, except that we can support more types. */
9748 new_symbol (die, NULL, cu);
9749 }
9750
9751 /* Get physical name. */
9752 physname = dwarf2_physname (fieldname, die, cu);
9753
9754 /* The name is already allocated along with this objfile, so we don't
9755 need to duplicate it for the type. */
9756 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9757 FIELD_TYPE (*fp) = die_type (die, cu);
9758 FIELD_NAME (*fp) = fieldname;
9759 }
9760 else if (die->tag == DW_TAG_inheritance)
9761 {
9762 LONGEST offset;
9763
9764 /* C++ base class field. */
9765 if (handle_data_member_location (die, cu, &offset))
9766 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9767 FIELD_BITSIZE (*fp) = 0;
9768 FIELD_TYPE (*fp) = die_type (die, cu);
9769 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9770 fip->nbaseclasses++;
9771 }
9772 }
9773
9774 /* Add a typedef defined in the scope of the FIP's class. */
9775
9776 static void
9777 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9778 struct dwarf2_cu *cu)
9779 {
9780 struct objfile *objfile = cu->objfile;
9781 struct typedef_field_list *new_field;
9782 struct attribute *attr;
9783 struct typedef_field *fp;
9784 char *fieldname = "";
9785
9786 /* Allocate a new field list entry and link it in. */
9787 new_field = xzalloc (sizeof (*new_field));
9788 make_cleanup (xfree, new_field);
9789
9790 gdb_assert (die->tag == DW_TAG_typedef);
9791
9792 fp = &new_field->field;
9793
9794 /* Get name of field. */
9795 fp->name = dwarf2_name (die, cu);
9796 if (fp->name == NULL)
9797 return;
9798
9799 fp->type = read_type_die (die, cu);
9800
9801 new_field->next = fip->typedef_field_list;
9802 fip->typedef_field_list = new_field;
9803 fip->typedef_field_list_count++;
9804 }
9805
9806 /* Create the vector of fields, and attach it to the type. */
9807
9808 static void
9809 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9810 struct dwarf2_cu *cu)
9811 {
9812 int nfields = fip->nfields;
9813
9814 /* Record the field count, allocate space for the array of fields,
9815 and create blank accessibility bitfields if necessary. */
9816 TYPE_NFIELDS (type) = nfields;
9817 TYPE_FIELDS (type) = (struct field *)
9818 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9819 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9820
9821 if (fip->non_public_fields && cu->language != language_ada)
9822 {
9823 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9824
9825 TYPE_FIELD_PRIVATE_BITS (type) =
9826 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9827 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9828
9829 TYPE_FIELD_PROTECTED_BITS (type) =
9830 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9831 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
9832
9833 TYPE_FIELD_IGNORE_BITS (type) =
9834 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9835 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
9836 }
9837
9838 /* If the type has baseclasses, allocate and clear a bit vector for
9839 TYPE_FIELD_VIRTUAL_BITS. */
9840 if (fip->nbaseclasses && cu->language != language_ada)
9841 {
9842 int num_bytes = B_BYTES (fip->nbaseclasses);
9843 unsigned char *pointer;
9844
9845 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9846 pointer = TYPE_ALLOC (type, num_bytes);
9847 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
9848 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
9849 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
9850 }
9851
9852 /* Copy the saved-up fields into the field vector. Start from the head of
9853 the list, adding to the tail of the field array, so that they end up in
9854 the same order in the array in which they were added to the list. */
9855 while (nfields-- > 0)
9856 {
9857 struct nextfield *fieldp;
9858
9859 if (fip->fields)
9860 {
9861 fieldp = fip->fields;
9862 fip->fields = fieldp->next;
9863 }
9864 else
9865 {
9866 fieldp = fip->baseclasses;
9867 fip->baseclasses = fieldp->next;
9868 }
9869
9870 TYPE_FIELD (type, nfields) = fieldp->field;
9871 switch (fieldp->accessibility)
9872 {
9873 case DW_ACCESS_private:
9874 if (cu->language != language_ada)
9875 SET_TYPE_FIELD_PRIVATE (type, nfields);
9876 break;
9877
9878 case DW_ACCESS_protected:
9879 if (cu->language != language_ada)
9880 SET_TYPE_FIELD_PROTECTED (type, nfields);
9881 break;
9882
9883 case DW_ACCESS_public:
9884 break;
9885
9886 default:
9887 /* Unknown accessibility. Complain and treat it as public. */
9888 {
9889 complaint (&symfile_complaints, _("unsupported accessibility %d"),
9890 fieldp->accessibility);
9891 }
9892 break;
9893 }
9894 if (nfields < fip->nbaseclasses)
9895 {
9896 switch (fieldp->virtuality)
9897 {
9898 case DW_VIRTUALITY_virtual:
9899 case DW_VIRTUALITY_pure_virtual:
9900 if (cu->language == language_ada)
9901 error (_("unexpected virtuality in component of Ada type"));
9902 SET_TYPE_FIELD_VIRTUAL (type, nfields);
9903 break;
9904 }
9905 }
9906 }
9907 }
9908
9909 /* Add a member function to the proper fieldlist. */
9910
9911 static void
9912 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
9913 struct type *type, struct dwarf2_cu *cu)
9914 {
9915 struct objfile *objfile = cu->objfile;
9916 struct attribute *attr;
9917 struct fnfieldlist *flp;
9918 int i;
9919 struct fn_field *fnp;
9920 char *fieldname;
9921 struct nextfnfield *new_fnfield;
9922 struct type *this_type;
9923 enum dwarf_access_attribute accessibility;
9924
9925 if (cu->language == language_ada)
9926 error (_("unexpected member function in Ada type"));
9927
9928 /* Get name of member function. */
9929 fieldname = dwarf2_name (die, cu);
9930 if (fieldname == NULL)
9931 return;
9932
9933 /* Look up member function name in fieldlist. */
9934 for (i = 0; i < fip->nfnfields; i++)
9935 {
9936 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
9937 break;
9938 }
9939
9940 /* Create new list element if necessary. */
9941 if (i < fip->nfnfields)
9942 flp = &fip->fnfieldlists[i];
9943 else
9944 {
9945 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9946 {
9947 fip->fnfieldlists = (struct fnfieldlist *)
9948 xrealloc (fip->fnfieldlists,
9949 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
9950 * sizeof (struct fnfieldlist));
9951 if (fip->nfnfields == 0)
9952 make_cleanup (free_current_contents, &fip->fnfieldlists);
9953 }
9954 flp = &fip->fnfieldlists[fip->nfnfields];
9955 flp->name = fieldname;
9956 flp->length = 0;
9957 flp->head = NULL;
9958 i = fip->nfnfields++;
9959 }
9960
9961 /* Create a new member function field and chain it to the field list
9962 entry. */
9963 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
9964 make_cleanup (xfree, new_fnfield);
9965 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9966 new_fnfield->next = flp->head;
9967 flp->head = new_fnfield;
9968 flp->length++;
9969
9970 /* Fill in the member function field info. */
9971 fnp = &new_fnfield->fnfield;
9972
9973 /* Delay processing of the physname until later. */
9974 if (cu->language == language_cplus || cu->language == language_java)
9975 {
9976 add_to_method_list (type, i, flp->length - 1, fieldname,
9977 die, cu);
9978 }
9979 else
9980 {
9981 const char *physname = dwarf2_physname (fieldname, die, cu);
9982 fnp->physname = physname ? physname : "";
9983 }
9984
9985 fnp->type = alloc_type (objfile);
9986 this_type = read_type_die (die, cu);
9987 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
9988 {
9989 int nparams = TYPE_NFIELDS (this_type);
9990
9991 /* TYPE is the domain of this method, and THIS_TYPE is the type
9992 of the method itself (TYPE_CODE_METHOD). */
9993 smash_to_method_type (fnp->type, type,
9994 TYPE_TARGET_TYPE (this_type),
9995 TYPE_FIELDS (this_type),
9996 TYPE_NFIELDS (this_type),
9997 TYPE_VARARGS (this_type));
9998
9999 /* Handle static member functions.
10000 Dwarf2 has no clean way to discern C++ static and non-static
10001 member functions. G++ helps GDB by marking the first
10002 parameter for non-static member functions (which is the this
10003 pointer) as artificial. We obtain this information from
10004 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10005 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10006 fnp->voffset = VOFFSET_STATIC;
10007 }
10008 else
10009 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10010 dwarf2_full_name (fieldname, die, cu));
10011
10012 /* Get fcontext from DW_AT_containing_type if present. */
10013 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10014 fnp->fcontext = die_containing_type (die, cu);
10015
10016 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10017 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10018
10019 /* Get accessibility. */
10020 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10021 if (attr)
10022 accessibility = DW_UNSND (attr);
10023 else
10024 accessibility = dwarf2_default_access_attribute (die, cu);
10025 switch (accessibility)
10026 {
10027 case DW_ACCESS_private:
10028 fnp->is_private = 1;
10029 break;
10030 case DW_ACCESS_protected:
10031 fnp->is_protected = 1;
10032 break;
10033 }
10034
10035 /* Check for artificial methods. */
10036 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10037 if (attr && DW_UNSND (attr) != 0)
10038 fnp->is_artificial = 1;
10039
10040 /* Get index in virtual function table if it is a virtual member
10041 function. For older versions of GCC, this is an offset in the
10042 appropriate virtual table, as specified by DW_AT_containing_type.
10043 For everyone else, it is an expression to be evaluated relative
10044 to the object address. */
10045
10046 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10047 if (attr)
10048 {
10049 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10050 {
10051 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10052 {
10053 /* Old-style GCC. */
10054 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10055 }
10056 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10057 || (DW_BLOCK (attr)->size > 1
10058 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10059 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10060 {
10061 struct dwarf_block blk;
10062 int offset;
10063
10064 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10065 ? 1 : 2);
10066 blk.size = DW_BLOCK (attr)->size - offset;
10067 blk.data = DW_BLOCK (attr)->data + offset;
10068 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10069 if ((fnp->voffset % cu->header.addr_size) != 0)
10070 dwarf2_complex_location_expr_complaint ();
10071 else
10072 fnp->voffset /= cu->header.addr_size;
10073 fnp->voffset += 2;
10074 }
10075 else
10076 dwarf2_complex_location_expr_complaint ();
10077
10078 if (!fnp->fcontext)
10079 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10080 }
10081 else if (attr_form_is_section_offset (attr))
10082 {
10083 dwarf2_complex_location_expr_complaint ();
10084 }
10085 else
10086 {
10087 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10088 fieldname);
10089 }
10090 }
10091 else
10092 {
10093 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10094 if (attr && DW_UNSND (attr))
10095 {
10096 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10097 complaint (&symfile_complaints,
10098 _("Member function \"%s\" (offset %d) is virtual "
10099 "but the vtable offset is not specified"),
10100 fieldname, die->offset.sect_off);
10101 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10102 TYPE_CPLUS_DYNAMIC (type) = 1;
10103 }
10104 }
10105 }
10106
10107 /* Create the vector of member function fields, and attach it to the type. */
10108
10109 static void
10110 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10111 struct dwarf2_cu *cu)
10112 {
10113 struct fnfieldlist *flp;
10114 int i;
10115
10116 if (cu->language == language_ada)
10117 error (_("unexpected member functions in Ada type"));
10118
10119 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10120 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10121 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10122
10123 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10124 {
10125 struct nextfnfield *nfp = flp->head;
10126 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10127 int k;
10128
10129 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10130 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10131 fn_flp->fn_fields = (struct fn_field *)
10132 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10133 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10134 fn_flp->fn_fields[k] = nfp->fnfield;
10135 }
10136
10137 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10138 }
10139
10140 /* Returns non-zero if NAME is the name of a vtable member in CU's
10141 language, zero otherwise. */
10142 static int
10143 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10144 {
10145 static const char vptr[] = "_vptr";
10146 static const char vtable[] = "vtable";
10147
10148 /* Look for the C++ and Java forms of the vtable. */
10149 if ((cu->language == language_java
10150 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10151 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10152 && is_cplus_marker (name[sizeof (vptr) - 1])))
10153 return 1;
10154
10155 return 0;
10156 }
10157
10158 /* GCC outputs unnamed structures that are really pointers to member
10159 functions, with the ABI-specified layout. If TYPE describes
10160 such a structure, smash it into a member function type.
10161
10162 GCC shouldn't do this; it should just output pointer to member DIEs.
10163 This is GCC PR debug/28767. */
10164
10165 static void
10166 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10167 {
10168 struct type *pfn_type, *domain_type, *new_type;
10169
10170 /* Check for a structure with no name and two children. */
10171 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10172 return;
10173
10174 /* Check for __pfn and __delta members. */
10175 if (TYPE_FIELD_NAME (type, 0) == NULL
10176 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10177 || TYPE_FIELD_NAME (type, 1) == NULL
10178 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10179 return;
10180
10181 /* Find the type of the method. */
10182 pfn_type = TYPE_FIELD_TYPE (type, 0);
10183 if (pfn_type == NULL
10184 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10185 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10186 return;
10187
10188 /* Look for the "this" argument. */
10189 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10190 if (TYPE_NFIELDS (pfn_type) == 0
10191 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10192 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10193 return;
10194
10195 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10196 new_type = alloc_type (objfile);
10197 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10198 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10199 TYPE_VARARGS (pfn_type));
10200 smash_to_methodptr_type (type, new_type);
10201 }
10202
10203 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10204 (icc). */
10205
10206 static int
10207 producer_is_icc (struct dwarf2_cu *cu)
10208 {
10209 if (!cu->checked_producer)
10210 check_producer (cu);
10211
10212 return cu->producer_is_icc;
10213 }
10214
10215 /* Called when we find the DIE that starts a structure or union scope
10216 (definition) to create a type for the structure or union. Fill in
10217 the type's name and general properties; the members will not be
10218 processed until process_structure_type.
10219
10220 NOTE: we need to call these functions regardless of whether or not the
10221 DIE has a DW_AT_name attribute, since it might be an anonymous
10222 structure or union. This gets the type entered into our set of
10223 user defined types.
10224
10225 However, if the structure is incomplete (an opaque struct/union)
10226 then suppress creating a symbol table entry for it since gdb only
10227 wants to find the one with the complete definition. Note that if
10228 it is complete, we just call new_symbol, which does it's own
10229 checking about whether the struct/union is anonymous or not (and
10230 suppresses creating a symbol table entry itself). */
10231
10232 static struct type *
10233 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10234 {
10235 struct objfile *objfile = cu->objfile;
10236 struct type *type;
10237 struct attribute *attr;
10238 char *name;
10239
10240 /* If the definition of this type lives in .debug_types, read that type.
10241 Don't follow DW_AT_specification though, that will take us back up
10242 the chain and we want to go down. */
10243 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10244 if (attr)
10245 {
10246 struct dwarf2_cu *type_cu = cu;
10247 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10248
10249 /* We could just recurse on read_structure_type, but we need to call
10250 get_die_type to ensure only one type for this DIE is created.
10251 This is important, for example, because for c++ classes we need
10252 TYPE_NAME set which is only done by new_symbol. Blech. */
10253 type = read_type_die (type_die, type_cu);
10254
10255 /* TYPE_CU may not be the same as CU.
10256 Ensure TYPE is recorded in CU's type_hash table. */
10257 return set_die_type (die, type, cu);
10258 }
10259
10260 type = alloc_type (objfile);
10261 INIT_CPLUS_SPECIFIC (type);
10262
10263 name = dwarf2_name (die, cu);
10264 if (name != NULL)
10265 {
10266 if (cu->language == language_cplus
10267 || cu->language == language_java)
10268 {
10269 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10270
10271 /* dwarf2_full_name might have already finished building the DIE's
10272 type. If so, there is no need to continue. */
10273 if (get_die_type (die, cu) != NULL)
10274 return get_die_type (die, cu);
10275
10276 TYPE_TAG_NAME (type) = full_name;
10277 if (die->tag == DW_TAG_structure_type
10278 || die->tag == DW_TAG_class_type)
10279 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10280 }
10281 else
10282 {
10283 /* The name is already allocated along with this objfile, so
10284 we don't need to duplicate it for the type. */
10285 TYPE_TAG_NAME (type) = (char *) name;
10286 if (die->tag == DW_TAG_class_type)
10287 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10288 }
10289 }
10290
10291 if (die->tag == DW_TAG_structure_type)
10292 {
10293 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10294 }
10295 else if (die->tag == DW_TAG_union_type)
10296 {
10297 TYPE_CODE (type) = TYPE_CODE_UNION;
10298 }
10299 else
10300 {
10301 TYPE_CODE (type) = TYPE_CODE_CLASS;
10302 }
10303
10304 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10305 TYPE_DECLARED_CLASS (type) = 1;
10306
10307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10308 if (attr)
10309 {
10310 TYPE_LENGTH (type) = DW_UNSND (attr);
10311 }
10312 else
10313 {
10314 TYPE_LENGTH (type) = 0;
10315 }
10316
10317 if (producer_is_icc (cu))
10318 {
10319 /* ICC does not output the required DW_AT_declaration
10320 on incomplete types, but gives them a size of zero. */
10321 }
10322 else
10323 TYPE_STUB_SUPPORTED (type) = 1;
10324
10325 if (die_is_declaration (die, cu))
10326 TYPE_STUB (type) = 1;
10327 else if (attr == NULL && die->child == NULL
10328 && producer_is_realview (cu->producer))
10329 /* RealView does not output the required DW_AT_declaration
10330 on incomplete types. */
10331 TYPE_STUB (type) = 1;
10332
10333 /* We need to add the type field to the die immediately so we don't
10334 infinitely recurse when dealing with pointers to the structure
10335 type within the structure itself. */
10336 set_die_type (die, type, cu);
10337
10338 /* set_die_type should be already done. */
10339 set_descriptive_type (type, die, cu);
10340
10341 return type;
10342 }
10343
10344 /* Finish creating a structure or union type, including filling in
10345 its members and creating a symbol for it. */
10346
10347 static void
10348 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10349 {
10350 struct objfile *objfile = cu->objfile;
10351 struct die_info *child_die = die->child;
10352 struct type *type;
10353
10354 type = get_die_type (die, cu);
10355 if (type == NULL)
10356 type = read_structure_type (die, cu);
10357
10358 if (die->child != NULL && ! die_is_declaration (die, cu))
10359 {
10360 struct field_info fi;
10361 struct die_info *child_die;
10362 VEC (symbolp) *template_args = NULL;
10363 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10364
10365 memset (&fi, 0, sizeof (struct field_info));
10366
10367 child_die = die->child;
10368
10369 while (child_die && child_die->tag)
10370 {
10371 if (child_die->tag == DW_TAG_member
10372 || child_die->tag == DW_TAG_variable)
10373 {
10374 /* NOTE: carlton/2002-11-05: A C++ static data member
10375 should be a DW_TAG_member that is a declaration, but
10376 all versions of G++ as of this writing (so through at
10377 least 3.2.1) incorrectly generate DW_TAG_variable
10378 tags for them instead. */
10379 dwarf2_add_field (&fi, child_die, cu);
10380 }
10381 else if (child_die->tag == DW_TAG_subprogram)
10382 {
10383 /* C++ member function. */
10384 dwarf2_add_member_fn (&fi, child_die, type, cu);
10385 }
10386 else if (child_die->tag == DW_TAG_inheritance)
10387 {
10388 /* C++ base class field. */
10389 dwarf2_add_field (&fi, child_die, cu);
10390 }
10391 else if (child_die->tag == DW_TAG_typedef)
10392 dwarf2_add_typedef (&fi, child_die, cu);
10393 else if (child_die->tag == DW_TAG_template_type_param
10394 || child_die->tag == DW_TAG_template_value_param)
10395 {
10396 struct symbol *arg = new_symbol (child_die, NULL, cu);
10397
10398 if (arg != NULL)
10399 VEC_safe_push (symbolp, template_args, arg);
10400 }
10401
10402 child_die = sibling_die (child_die);
10403 }
10404
10405 /* Attach template arguments to type. */
10406 if (! VEC_empty (symbolp, template_args))
10407 {
10408 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10409 TYPE_N_TEMPLATE_ARGUMENTS (type)
10410 = VEC_length (symbolp, template_args);
10411 TYPE_TEMPLATE_ARGUMENTS (type)
10412 = obstack_alloc (&objfile->objfile_obstack,
10413 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10414 * sizeof (struct symbol *)));
10415 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10416 VEC_address (symbolp, template_args),
10417 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10418 * sizeof (struct symbol *)));
10419 VEC_free (symbolp, template_args);
10420 }
10421
10422 /* Attach fields and member functions to the type. */
10423 if (fi.nfields)
10424 dwarf2_attach_fields_to_type (&fi, type, cu);
10425 if (fi.nfnfields)
10426 {
10427 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10428
10429 /* Get the type which refers to the base class (possibly this
10430 class itself) which contains the vtable pointer for the current
10431 class from the DW_AT_containing_type attribute. This use of
10432 DW_AT_containing_type is a GNU extension. */
10433
10434 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10435 {
10436 struct type *t = die_containing_type (die, cu);
10437
10438 TYPE_VPTR_BASETYPE (type) = t;
10439 if (type == t)
10440 {
10441 int i;
10442
10443 /* Our own class provides vtbl ptr. */
10444 for (i = TYPE_NFIELDS (t) - 1;
10445 i >= TYPE_N_BASECLASSES (t);
10446 --i)
10447 {
10448 const char *fieldname = TYPE_FIELD_NAME (t, i);
10449
10450 if (is_vtable_name (fieldname, cu))
10451 {
10452 TYPE_VPTR_FIELDNO (type) = i;
10453 break;
10454 }
10455 }
10456
10457 /* Complain if virtual function table field not found. */
10458 if (i < TYPE_N_BASECLASSES (t))
10459 complaint (&symfile_complaints,
10460 _("virtual function table pointer "
10461 "not found when defining class '%s'"),
10462 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10463 "");
10464 }
10465 else
10466 {
10467 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10468 }
10469 }
10470 else if (cu->producer
10471 && strncmp (cu->producer,
10472 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10473 {
10474 /* The IBM XLC compiler does not provide direct indication
10475 of the containing type, but the vtable pointer is
10476 always named __vfp. */
10477
10478 int i;
10479
10480 for (i = TYPE_NFIELDS (type) - 1;
10481 i >= TYPE_N_BASECLASSES (type);
10482 --i)
10483 {
10484 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10485 {
10486 TYPE_VPTR_FIELDNO (type) = i;
10487 TYPE_VPTR_BASETYPE (type) = type;
10488 break;
10489 }
10490 }
10491 }
10492 }
10493
10494 /* Copy fi.typedef_field_list linked list elements content into the
10495 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10496 if (fi.typedef_field_list)
10497 {
10498 int i = fi.typedef_field_list_count;
10499
10500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10501 TYPE_TYPEDEF_FIELD_ARRAY (type)
10502 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10503 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10504
10505 /* Reverse the list order to keep the debug info elements order. */
10506 while (--i >= 0)
10507 {
10508 struct typedef_field *dest, *src;
10509
10510 dest = &TYPE_TYPEDEF_FIELD (type, i);
10511 src = &fi.typedef_field_list->field;
10512 fi.typedef_field_list = fi.typedef_field_list->next;
10513 *dest = *src;
10514 }
10515 }
10516
10517 do_cleanups (back_to);
10518
10519 if (HAVE_CPLUS_STRUCT (type))
10520 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10521 }
10522
10523 quirk_gcc_member_function_pointer (type, objfile);
10524
10525 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10526 snapshots) has been known to create a die giving a declaration
10527 for a class that has, as a child, a die giving a definition for a
10528 nested class. So we have to process our children even if the
10529 current die is a declaration. Normally, of course, a declaration
10530 won't have any children at all. */
10531
10532 while (child_die != NULL && child_die->tag)
10533 {
10534 if (child_die->tag == DW_TAG_member
10535 || child_die->tag == DW_TAG_variable
10536 || child_die->tag == DW_TAG_inheritance
10537 || child_die->tag == DW_TAG_template_value_param
10538 || child_die->tag == DW_TAG_template_type_param)
10539 {
10540 /* Do nothing. */
10541 }
10542 else
10543 process_die (child_die, cu);
10544
10545 child_die = sibling_die (child_die);
10546 }
10547
10548 /* Do not consider external references. According to the DWARF standard,
10549 these DIEs are identified by the fact that they have no byte_size
10550 attribute, and a declaration attribute. */
10551 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10552 || !die_is_declaration (die, cu))
10553 new_symbol (die, type, cu);
10554 }
10555
10556 /* Given a DW_AT_enumeration_type die, set its type. We do not
10557 complete the type's fields yet, or create any symbols. */
10558
10559 static struct type *
10560 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10561 {
10562 struct objfile *objfile = cu->objfile;
10563 struct type *type;
10564 struct attribute *attr;
10565 const char *name;
10566
10567 /* If the definition of this type lives in .debug_types, read that type.
10568 Don't follow DW_AT_specification though, that will take us back up
10569 the chain and we want to go down. */
10570 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10571 if (attr)
10572 {
10573 struct dwarf2_cu *type_cu = cu;
10574 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10575
10576 type = read_type_die (type_die, type_cu);
10577
10578 /* TYPE_CU may not be the same as CU.
10579 Ensure TYPE is recorded in CU's type_hash table. */
10580 return set_die_type (die, type, cu);
10581 }
10582
10583 type = alloc_type (objfile);
10584
10585 TYPE_CODE (type) = TYPE_CODE_ENUM;
10586 name = dwarf2_full_name (NULL, die, cu);
10587 if (name != NULL)
10588 TYPE_TAG_NAME (type) = (char *) name;
10589
10590 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10591 if (attr)
10592 {
10593 TYPE_LENGTH (type) = DW_UNSND (attr);
10594 }
10595 else
10596 {
10597 TYPE_LENGTH (type) = 0;
10598 }
10599
10600 /* The enumeration DIE can be incomplete. In Ada, any type can be
10601 declared as private in the package spec, and then defined only
10602 inside the package body. Such types are known as Taft Amendment
10603 Types. When another package uses such a type, an incomplete DIE
10604 may be generated by the compiler. */
10605 if (die_is_declaration (die, cu))
10606 TYPE_STUB (type) = 1;
10607
10608 return set_die_type (die, type, cu);
10609 }
10610
10611 /* Given a pointer to a die which begins an enumeration, process all
10612 the dies that define the members of the enumeration, and create the
10613 symbol for the enumeration type.
10614
10615 NOTE: We reverse the order of the element list. */
10616
10617 static void
10618 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10619 {
10620 struct type *this_type;
10621
10622 this_type = get_die_type (die, cu);
10623 if (this_type == NULL)
10624 this_type = read_enumeration_type (die, cu);
10625
10626 if (die->child != NULL)
10627 {
10628 struct die_info *child_die;
10629 struct symbol *sym;
10630 struct field *fields = NULL;
10631 int num_fields = 0;
10632 int unsigned_enum = 1;
10633 char *name;
10634 int flag_enum = 1;
10635 ULONGEST mask = 0;
10636
10637 child_die = die->child;
10638 while (child_die && child_die->tag)
10639 {
10640 if (child_die->tag != DW_TAG_enumerator)
10641 {
10642 process_die (child_die, cu);
10643 }
10644 else
10645 {
10646 name = dwarf2_name (child_die, cu);
10647 if (name)
10648 {
10649 sym = new_symbol (child_die, this_type, cu);
10650 if (SYMBOL_VALUE (sym) < 0)
10651 {
10652 unsigned_enum = 0;
10653 flag_enum = 0;
10654 }
10655 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10656 flag_enum = 0;
10657 else
10658 mask |= SYMBOL_VALUE (sym);
10659
10660 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10661 {
10662 fields = (struct field *)
10663 xrealloc (fields,
10664 (num_fields + DW_FIELD_ALLOC_CHUNK)
10665 * sizeof (struct field));
10666 }
10667
10668 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10669 FIELD_TYPE (fields[num_fields]) = NULL;
10670 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10671 FIELD_BITSIZE (fields[num_fields]) = 0;
10672
10673 num_fields++;
10674 }
10675 }
10676
10677 child_die = sibling_die (child_die);
10678 }
10679
10680 if (num_fields)
10681 {
10682 TYPE_NFIELDS (this_type) = num_fields;
10683 TYPE_FIELDS (this_type) = (struct field *)
10684 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10685 memcpy (TYPE_FIELDS (this_type), fields,
10686 sizeof (struct field) * num_fields);
10687 xfree (fields);
10688 }
10689 if (unsigned_enum)
10690 TYPE_UNSIGNED (this_type) = 1;
10691 if (flag_enum)
10692 TYPE_FLAG_ENUM (this_type) = 1;
10693 }
10694
10695 /* If we are reading an enum from a .debug_types unit, and the enum
10696 is a declaration, and the enum is not the signatured type in the
10697 unit, then we do not want to add a symbol for it. Adding a
10698 symbol would in some cases obscure the true definition of the
10699 enum, giving users an incomplete type when the definition is
10700 actually available. Note that we do not want to do this for all
10701 enums which are just declarations, because C++0x allows forward
10702 enum declarations. */
10703 if (cu->per_cu->is_debug_types
10704 && die_is_declaration (die, cu))
10705 {
10706 struct signatured_type *sig_type;
10707
10708 sig_type
10709 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10710 cu->per_cu->info_or_types_section,
10711 cu->per_cu->offset);
10712 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10713 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10714 return;
10715 }
10716
10717 new_symbol (die, this_type, cu);
10718 }
10719
10720 /* Extract all information from a DW_TAG_array_type DIE and put it in
10721 the DIE's type field. For now, this only handles one dimensional
10722 arrays. */
10723
10724 static struct type *
10725 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10726 {
10727 struct objfile *objfile = cu->objfile;
10728 struct die_info *child_die;
10729 struct type *type;
10730 struct type *element_type, *range_type, *index_type;
10731 struct type **range_types = NULL;
10732 struct attribute *attr;
10733 int ndim = 0;
10734 struct cleanup *back_to;
10735 char *name;
10736
10737 element_type = die_type (die, cu);
10738
10739 /* The die_type call above may have already set the type for this DIE. */
10740 type = get_die_type (die, cu);
10741 if (type)
10742 return type;
10743
10744 /* Irix 6.2 native cc creates array types without children for
10745 arrays with unspecified length. */
10746 if (die->child == NULL)
10747 {
10748 index_type = objfile_type (objfile)->builtin_int;
10749 range_type = create_range_type (NULL, index_type, 0, -1);
10750 type = create_array_type (NULL, element_type, range_type);
10751 return set_die_type (die, type, cu);
10752 }
10753
10754 back_to = make_cleanup (null_cleanup, NULL);
10755 child_die = die->child;
10756 while (child_die && child_die->tag)
10757 {
10758 if (child_die->tag == DW_TAG_subrange_type)
10759 {
10760 struct type *child_type = read_type_die (child_die, cu);
10761
10762 if (child_type != NULL)
10763 {
10764 /* The range type was succesfully read. Save it for the
10765 array type creation. */
10766 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10767 {
10768 range_types = (struct type **)
10769 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10770 * sizeof (struct type *));
10771 if (ndim == 0)
10772 make_cleanup (free_current_contents, &range_types);
10773 }
10774 range_types[ndim++] = child_type;
10775 }
10776 }
10777 child_die = sibling_die (child_die);
10778 }
10779
10780 /* Dwarf2 dimensions are output from left to right, create the
10781 necessary array types in backwards order. */
10782
10783 type = element_type;
10784
10785 if (read_array_order (die, cu) == DW_ORD_col_major)
10786 {
10787 int i = 0;
10788
10789 while (i < ndim)
10790 type = create_array_type (NULL, type, range_types[i++]);
10791 }
10792 else
10793 {
10794 while (ndim-- > 0)
10795 type = create_array_type (NULL, type, range_types[ndim]);
10796 }
10797
10798 /* Understand Dwarf2 support for vector types (like they occur on
10799 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10800 array type. This is not part of the Dwarf2/3 standard yet, but a
10801 custom vendor extension. The main difference between a regular
10802 array and the vector variant is that vectors are passed by value
10803 to functions. */
10804 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10805 if (attr)
10806 make_vector_type (type);
10807
10808 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10809 implementation may choose to implement triple vectors using this
10810 attribute. */
10811 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10812 if (attr)
10813 {
10814 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10815 TYPE_LENGTH (type) = DW_UNSND (attr);
10816 else
10817 complaint (&symfile_complaints,
10818 _("DW_AT_byte_size for array type smaller "
10819 "than the total size of elements"));
10820 }
10821
10822 name = dwarf2_name (die, cu);
10823 if (name)
10824 TYPE_NAME (type) = name;
10825
10826 /* Install the type in the die. */
10827 set_die_type (die, type, cu);
10828
10829 /* set_die_type should be already done. */
10830 set_descriptive_type (type, die, cu);
10831
10832 do_cleanups (back_to);
10833
10834 return type;
10835 }
10836
10837 static enum dwarf_array_dim_ordering
10838 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
10839 {
10840 struct attribute *attr;
10841
10842 attr = dwarf2_attr (die, DW_AT_ordering, cu);
10843
10844 if (attr) return DW_SND (attr);
10845
10846 /* GNU F77 is a special case, as at 08/2004 array type info is the
10847 opposite order to the dwarf2 specification, but data is still
10848 laid out as per normal fortran.
10849
10850 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
10851 version checking. */
10852
10853 if (cu->language == language_fortran
10854 && cu->producer && strstr (cu->producer, "GNU F77"))
10855 {
10856 return DW_ORD_row_major;
10857 }
10858
10859 switch (cu->language_defn->la_array_ordering)
10860 {
10861 case array_column_major:
10862 return DW_ORD_col_major;
10863 case array_row_major:
10864 default:
10865 return DW_ORD_row_major;
10866 };
10867 }
10868
10869 /* Extract all information from a DW_TAG_set_type DIE and put it in
10870 the DIE's type field. */
10871
10872 static struct type *
10873 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
10874 {
10875 struct type *domain_type, *set_type;
10876 struct attribute *attr;
10877
10878 domain_type = die_type (die, cu);
10879
10880 /* The die_type call above may have already set the type for this DIE. */
10881 set_type = get_die_type (die, cu);
10882 if (set_type)
10883 return set_type;
10884
10885 set_type = create_set_type (NULL, domain_type);
10886
10887 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10888 if (attr)
10889 TYPE_LENGTH (set_type) = DW_UNSND (attr);
10890
10891 return set_die_type (die, set_type, cu);
10892 }
10893
10894 /* First cut: install each common block member as a global variable. */
10895
10896 static void
10897 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
10898 {
10899 struct die_info *child_die;
10900 struct attribute *attr;
10901 struct symbol *sym;
10902 CORE_ADDR base = (CORE_ADDR) 0;
10903
10904 attr = dwarf2_attr (die, DW_AT_location, cu);
10905 if (attr)
10906 {
10907 /* Support the .debug_loc offsets. */
10908 if (attr_form_is_block (attr))
10909 {
10910 base = decode_locdesc (DW_BLOCK (attr), cu);
10911 }
10912 else if (attr_form_is_section_offset (attr))
10913 {
10914 dwarf2_complex_location_expr_complaint ();
10915 }
10916 else
10917 {
10918 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10919 "common block member");
10920 }
10921 }
10922 if (die->child != NULL)
10923 {
10924 child_die = die->child;
10925 while (child_die && child_die->tag)
10926 {
10927 LONGEST offset;
10928
10929 sym = new_symbol (child_die, NULL, cu);
10930 if (sym != NULL
10931 && handle_data_member_location (child_die, cu, &offset))
10932 {
10933 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
10934 add_symbol_to_list (sym, &global_symbols);
10935 }
10936 child_die = sibling_die (child_die);
10937 }
10938 }
10939 }
10940
10941 /* Create a type for a C++ namespace. */
10942
10943 static struct type *
10944 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
10945 {
10946 struct objfile *objfile = cu->objfile;
10947 const char *previous_prefix, *name;
10948 int is_anonymous;
10949 struct type *type;
10950
10951 /* For extensions, reuse the type of the original namespace. */
10952 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10953 {
10954 struct die_info *ext_die;
10955 struct dwarf2_cu *ext_cu = cu;
10956
10957 ext_die = dwarf2_extension (die, &ext_cu);
10958 type = read_type_die (ext_die, ext_cu);
10959
10960 /* EXT_CU may not be the same as CU.
10961 Ensure TYPE is recorded in CU's type_hash table. */
10962 return set_die_type (die, type, cu);
10963 }
10964
10965 name = namespace_name (die, &is_anonymous, cu);
10966
10967 /* Now build the name of the current namespace. */
10968
10969 previous_prefix = determine_prefix (die, cu);
10970 if (previous_prefix[0] != '\0')
10971 name = typename_concat (&objfile->objfile_obstack,
10972 previous_prefix, name, 0, cu);
10973
10974 /* Create the type. */
10975 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10976 objfile);
10977 TYPE_NAME (type) = (char *) name;
10978 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10979
10980 return set_die_type (die, type, cu);
10981 }
10982
10983 /* Read a C++ namespace. */
10984
10985 static void
10986 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10987 {
10988 struct objfile *objfile = cu->objfile;
10989 int is_anonymous;
10990
10991 /* Add a symbol associated to this if we haven't seen the namespace
10992 before. Also, add a using directive if it's an anonymous
10993 namespace. */
10994
10995 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
10996 {
10997 struct type *type;
10998
10999 type = read_type_die (die, cu);
11000 new_symbol (die, type, cu);
11001
11002 namespace_name (die, &is_anonymous, cu);
11003 if (is_anonymous)
11004 {
11005 const char *previous_prefix = determine_prefix (die, cu);
11006
11007 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11008 NULL, NULL, &objfile->objfile_obstack);
11009 }
11010 }
11011
11012 if (die->child != NULL)
11013 {
11014 struct die_info *child_die = die->child;
11015
11016 while (child_die && child_die->tag)
11017 {
11018 process_die (child_die, cu);
11019 child_die = sibling_die (child_die);
11020 }
11021 }
11022 }
11023
11024 /* Read a Fortran module as type. This DIE can be only a declaration used for
11025 imported module. Still we need that type as local Fortran "use ... only"
11026 declaration imports depend on the created type in determine_prefix. */
11027
11028 static struct type *
11029 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11030 {
11031 struct objfile *objfile = cu->objfile;
11032 char *module_name;
11033 struct type *type;
11034
11035 module_name = dwarf2_name (die, cu);
11036 if (!module_name)
11037 complaint (&symfile_complaints,
11038 _("DW_TAG_module has no name, offset 0x%x"),
11039 die->offset.sect_off);
11040 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11041
11042 /* determine_prefix uses TYPE_TAG_NAME. */
11043 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11044
11045 return set_die_type (die, type, cu);
11046 }
11047
11048 /* Read a Fortran module. */
11049
11050 static void
11051 read_module (struct die_info *die, struct dwarf2_cu *cu)
11052 {
11053 struct die_info *child_die = die->child;
11054
11055 while (child_die && child_die->tag)
11056 {
11057 process_die (child_die, cu);
11058 child_die = sibling_die (child_die);
11059 }
11060 }
11061
11062 /* Return the name of the namespace represented by DIE. Set
11063 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11064 namespace. */
11065
11066 static const char *
11067 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11068 {
11069 struct die_info *current_die;
11070 const char *name = NULL;
11071
11072 /* Loop through the extensions until we find a name. */
11073
11074 for (current_die = die;
11075 current_die != NULL;
11076 current_die = dwarf2_extension (die, &cu))
11077 {
11078 name = dwarf2_name (current_die, cu);
11079 if (name != NULL)
11080 break;
11081 }
11082
11083 /* Is it an anonymous namespace? */
11084
11085 *is_anonymous = (name == NULL);
11086 if (*is_anonymous)
11087 name = CP_ANONYMOUS_NAMESPACE_STR;
11088
11089 return name;
11090 }
11091
11092 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11093 the user defined type vector. */
11094
11095 static struct type *
11096 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11097 {
11098 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11099 struct comp_unit_head *cu_header = &cu->header;
11100 struct type *type;
11101 struct attribute *attr_byte_size;
11102 struct attribute *attr_address_class;
11103 int byte_size, addr_class;
11104 struct type *target_type;
11105
11106 target_type = die_type (die, cu);
11107
11108 /* The die_type call above may have already set the type for this DIE. */
11109 type = get_die_type (die, cu);
11110 if (type)
11111 return type;
11112
11113 type = lookup_pointer_type (target_type);
11114
11115 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11116 if (attr_byte_size)
11117 byte_size = DW_UNSND (attr_byte_size);
11118 else
11119 byte_size = cu_header->addr_size;
11120
11121 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11122 if (attr_address_class)
11123 addr_class = DW_UNSND (attr_address_class);
11124 else
11125 addr_class = DW_ADDR_none;
11126
11127 /* If the pointer size or address class is different than the
11128 default, create a type variant marked as such and set the
11129 length accordingly. */
11130 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11131 {
11132 if (gdbarch_address_class_type_flags_p (gdbarch))
11133 {
11134 int type_flags;
11135
11136 type_flags = gdbarch_address_class_type_flags
11137 (gdbarch, byte_size, addr_class);
11138 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11139 == 0);
11140 type = make_type_with_address_space (type, type_flags);
11141 }
11142 else if (TYPE_LENGTH (type) != byte_size)
11143 {
11144 complaint (&symfile_complaints,
11145 _("invalid pointer size %d"), byte_size);
11146 }
11147 else
11148 {
11149 /* Should we also complain about unhandled address classes? */
11150 }
11151 }
11152
11153 TYPE_LENGTH (type) = byte_size;
11154 return set_die_type (die, type, cu);
11155 }
11156
11157 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11158 the user defined type vector. */
11159
11160 static struct type *
11161 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11162 {
11163 struct type *type;
11164 struct type *to_type;
11165 struct type *domain;
11166
11167 to_type = die_type (die, cu);
11168 domain = die_containing_type (die, cu);
11169
11170 /* The calls above may have already set the type for this DIE. */
11171 type = get_die_type (die, cu);
11172 if (type)
11173 return type;
11174
11175 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11176 type = lookup_methodptr_type (to_type);
11177 else
11178 type = lookup_memberptr_type (to_type, domain);
11179
11180 return set_die_type (die, type, cu);
11181 }
11182
11183 /* Extract all information from a DW_TAG_reference_type DIE and add to
11184 the user defined type vector. */
11185
11186 static struct type *
11187 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11188 {
11189 struct comp_unit_head *cu_header = &cu->header;
11190 struct type *type, *target_type;
11191 struct attribute *attr;
11192
11193 target_type = die_type (die, cu);
11194
11195 /* The die_type call above may have already set the type for this DIE. */
11196 type = get_die_type (die, cu);
11197 if (type)
11198 return type;
11199
11200 type = lookup_reference_type (target_type);
11201 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11202 if (attr)
11203 {
11204 TYPE_LENGTH (type) = DW_UNSND (attr);
11205 }
11206 else
11207 {
11208 TYPE_LENGTH (type) = cu_header->addr_size;
11209 }
11210 return set_die_type (die, type, cu);
11211 }
11212
11213 static struct type *
11214 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11215 {
11216 struct type *base_type, *cv_type;
11217
11218 base_type = die_type (die, cu);
11219
11220 /* The die_type call above may have already set the type for this DIE. */
11221 cv_type = get_die_type (die, cu);
11222 if (cv_type)
11223 return cv_type;
11224
11225 /* In case the const qualifier is applied to an array type, the element type
11226 is so qualified, not the array type (section 6.7.3 of C99). */
11227 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11228 {
11229 struct type *el_type, *inner_array;
11230
11231 base_type = copy_type (base_type);
11232 inner_array = base_type;
11233
11234 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11235 {
11236 TYPE_TARGET_TYPE (inner_array) =
11237 copy_type (TYPE_TARGET_TYPE (inner_array));
11238 inner_array = TYPE_TARGET_TYPE (inner_array);
11239 }
11240
11241 el_type = TYPE_TARGET_TYPE (inner_array);
11242 TYPE_TARGET_TYPE (inner_array) =
11243 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11244
11245 return set_die_type (die, base_type, cu);
11246 }
11247
11248 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11249 return set_die_type (die, cv_type, cu);
11250 }
11251
11252 static struct type *
11253 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11254 {
11255 struct type *base_type, *cv_type;
11256
11257 base_type = die_type (die, cu);
11258
11259 /* The die_type call above may have already set the type for this DIE. */
11260 cv_type = get_die_type (die, cu);
11261 if (cv_type)
11262 return cv_type;
11263
11264 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11265 return set_die_type (die, cv_type, cu);
11266 }
11267
11268 /* Extract all information from a DW_TAG_string_type DIE and add to
11269 the user defined type vector. It isn't really a user defined type,
11270 but it behaves like one, with other DIE's using an AT_user_def_type
11271 attribute to reference it. */
11272
11273 static struct type *
11274 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11275 {
11276 struct objfile *objfile = cu->objfile;
11277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11278 struct type *type, *range_type, *index_type, *char_type;
11279 struct attribute *attr;
11280 unsigned int length;
11281
11282 attr = dwarf2_attr (die, DW_AT_string_length, cu);
11283 if (attr)
11284 {
11285 length = DW_UNSND (attr);
11286 }
11287 else
11288 {
11289 /* Check for the DW_AT_byte_size attribute. */
11290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11291 if (attr)
11292 {
11293 length = DW_UNSND (attr);
11294 }
11295 else
11296 {
11297 length = 1;
11298 }
11299 }
11300
11301 index_type = objfile_type (objfile)->builtin_int;
11302 range_type = create_range_type (NULL, index_type, 1, length);
11303 char_type = language_string_char_type (cu->language_defn, gdbarch);
11304 type = create_string_type (NULL, char_type, range_type);
11305
11306 return set_die_type (die, type, cu);
11307 }
11308
11309 /* Handle DIES due to C code like:
11310
11311 struct foo
11312 {
11313 int (*funcp)(int a, long l);
11314 int b;
11315 };
11316
11317 ('funcp' generates a DW_TAG_subroutine_type DIE). */
11318
11319 static struct type *
11320 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11321 {
11322 struct objfile *objfile = cu->objfile;
11323 struct type *type; /* Type that this function returns. */
11324 struct type *ftype; /* Function that returns above type. */
11325 struct attribute *attr;
11326
11327 type = die_type (die, cu);
11328
11329 /* The die_type call above may have already set the type for this DIE. */
11330 ftype = get_die_type (die, cu);
11331 if (ftype)
11332 return ftype;
11333
11334 ftype = lookup_function_type (type);
11335
11336 /* All functions in C++, Pascal and Java have prototypes. */
11337 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11338 if ((attr && (DW_UNSND (attr) != 0))
11339 || cu->language == language_cplus
11340 || cu->language == language_java
11341 || cu->language == language_pascal)
11342 TYPE_PROTOTYPED (ftype) = 1;
11343 else if (producer_is_realview (cu->producer))
11344 /* RealView does not emit DW_AT_prototyped. We can not
11345 distinguish prototyped and unprototyped functions; default to
11346 prototyped, since that is more common in modern code (and
11347 RealView warns about unprototyped functions). */
11348 TYPE_PROTOTYPED (ftype) = 1;
11349
11350 /* Store the calling convention in the type if it's available in
11351 the subroutine die. Otherwise set the calling convention to
11352 the default value DW_CC_normal. */
11353 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11354 if (attr)
11355 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11356 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11357 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11358 else
11359 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11360
11361 /* We need to add the subroutine type to the die immediately so
11362 we don't infinitely recurse when dealing with parameters
11363 declared as the same subroutine type. */
11364 set_die_type (die, ftype, cu);
11365
11366 if (die->child != NULL)
11367 {
11368 struct type *void_type = objfile_type (objfile)->builtin_void;
11369 struct die_info *child_die;
11370 int nparams, iparams;
11371
11372 /* Count the number of parameters.
11373 FIXME: GDB currently ignores vararg functions, but knows about
11374 vararg member functions. */
11375 nparams = 0;
11376 child_die = die->child;
11377 while (child_die && child_die->tag)
11378 {
11379 if (child_die->tag == DW_TAG_formal_parameter)
11380 nparams++;
11381 else if (child_die->tag == DW_TAG_unspecified_parameters)
11382 TYPE_VARARGS (ftype) = 1;
11383 child_die = sibling_die (child_die);
11384 }
11385
11386 /* Allocate storage for parameters and fill them in. */
11387 TYPE_NFIELDS (ftype) = nparams;
11388 TYPE_FIELDS (ftype) = (struct field *)
11389 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11390
11391 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11392 even if we error out during the parameters reading below. */
11393 for (iparams = 0; iparams < nparams; iparams++)
11394 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11395
11396 iparams = 0;
11397 child_die = die->child;
11398 while (child_die && child_die->tag)
11399 {
11400 if (child_die->tag == DW_TAG_formal_parameter)
11401 {
11402 struct type *arg_type;
11403
11404 /* DWARF version 2 has no clean way to discern C++
11405 static and non-static member functions. G++ helps
11406 GDB by marking the first parameter for non-static
11407 member functions (which is the this pointer) as
11408 artificial. We pass this information to
11409 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11410
11411 DWARF version 3 added DW_AT_object_pointer, which GCC
11412 4.5 does not yet generate. */
11413 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11414 if (attr)
11415 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11416 else
11417 {
11418 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11419
11420 /* GCC/43521: In java, the formal parameter
11421 "this" is sometimes not marked with DW_AT_artificial. */
11422 if (cu->language == language_java)
11423 {
11424 const char *name = dwarf2_name (child_die, cu);
11425
11426 if (name && !strcmp (name, "this"))
11427 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11428 }
11429 }
11430 arg_type = die_type (child_die, cu);
11431
11432 /* RealView does not mark THIS as const, which the testsuite
11433 expects. GCC marks THIS as const in method definitions,
11434 but not in the class specifications (GCC PR 43053). */
11435 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11436 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11437 {
11438 int is_this = 0;
11439 struct dwarf2_cu *arg_cu = cu;
11440 const char *name = dwarf2_name (child_die, cu);
11441
11442 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11443 if (attr)
11444 {
11445 /* If the compiler emits this, use it. */
11446 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11447 is_this = 1;
11448 }
11449 else if (name && strcmp (name, "this") == 0)
11450 /* Function definitions will have the argument names. */
11451 is_this = 1;
11452 else if (name == NULL && iparams == 0)
11453 /* Declarations may not have the names, so like
11454 elsewhere in GDB, assume an artificial first
11455 argument is "this". */
11456 is_this = 1;
11457
11458 if (is_this)
11459 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11460 arg_type, 0);
11461 }
11462
11463 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11464 iparams++;
11465 }
11466 child_die = sibling_die (child_die);
11467 }
11468 }
11469
11470 return ftype;
11471 }
11472
11473 static struct type *
11474 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11475 {
11476 struct objfile *objfile = cu->objfile;
11477 const char *name = NULL;
11478 struct type *this_type, *target_type;
11479
11480 name = dwarf2_full_name (NULL, die, cu);
11481 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11482 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11483 TYPE_NAME (this_type) = (char *) name;
11484 set_die_type (die, this_type, cu);
11485 target_type = die_type (die, cu);
11486 if (target_type != this_type)
11487 TYPE_TARGET_TYPE (this_type) = target_type;
11488 else
11489 {
11490 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11491 spec and cause infinite loops in GDB. */
11492 complaint (&symfile_complaints,
11493 _("Self-referential DW_TAG_typedef "
11494 "- DIE at 0x%x [in module %s]"),
11495 die->offset.sect_off, objfile->name);
11496 TYPE_TARGET_TYPE (this_type) = NULL;
11497 }
11498 return this_type;
11499 }
11500
11501 /* Find a representation of a given base type and install
11502 it in the TYPE field of the die. */
11503
11504 static struct type *
11505 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11506 {
11507 struct objfile *objfile = cu->objfile;
11508 struct type *type;
11509 struct attribute *attr;
11510 int encoding = 0, size = 0;
11511 char *name;
11512 enum type_code code = TYPE_CODE_INT;
11513 int type_flags = 0;
11514 struct type *target_type = NULL;
11515
11516 attr = dwarf2_attr (die, DW_AT_encoding, cu);
11517 if (attr)
11518 {
11519 encoding = DW_UNSND (attr);
11520 }
11521 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11522 if (attr)
11523 {
11524 size = DW_UNSND (attr);
11525 }
11526 name = dwarf2_name (die, cu);
11527 if (!name)
11528 {
11529 complaint (&symfile_complaints,
11530 _("DW_AT_name missing from DW_TAG_base_type"));
11531 }
11532
11533 switch (encoding)
11534 {
11535 case DW_ATE_address:
11536 /* Turn DW_ATE_address into a void * pointer. */
11537 code = TYPE_CODE_PTR;
11538 type_flags |= TYPE_FLAG_UNSIGNED;
11539 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11540 break;
11541 case DW_ATE_boolean:
11542 code = TYPE_CODE_BOOL;
11543 type_flags |= TYPE_FLAG_UNSIGNED;
11544 break;
11545 case DW_ATE_complex_float:
11546 code = TYPE_CODE_COMPLEX;
11547 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11548 break;
11549 case DW_ATE_decimal_float:
11550 code = TYPE_CODE_DECFLOAT;
11551 break;
11552 case DW_ATE_float:
11553 code = TYPE_CODE_FLT;
11554 break;
11555 case DW_ATE_signed:
11556 break;
11557 case DW_ATE_unsigned:
11558 type_flags |= TYPE_FLAG_UNSIGNED;
11559 if (cu->language == language_fortran
11560 && name
11561 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11562 code = TYPE_CODE_CHAR;
11563 break;
11564 case DW_ATE_signed_char:
11565 if (cu->language == language_ada || cu->language == language_m2
11566 || cu->language == language_pascal
11567 || cu->language == language_fortran)
11568 code = TYPE_CODE_CHAR;
11569 break;
11570 case DW_ATE_unsigned_char:
11571 if (cu->language == language_ada || cu->language == language_m2
11572 || cu->language == language_pascal
11573 || cu->language == language_fortran)
11574 code = TYPE_CODE_CHAR;
11575 type_flags |= TYPE_FLAG_UNSIGNED;
11576 break;
11577 case DW_ATE_UTF:
11578 /* We just treat this as an integer and then recognize the
11579 type by name elsewhere. */
11580 break;
11581
11582 default:
11583 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11584 dwarf_type_encoding_name (encoding));
11585 break;
11586 }
11587
11588 type = init_type (code, size, type_flags, NULL, objfile);
11589 TYPE_NAME (type) = name;
11590 TYPE_TARGET_TYPE (type) = target_type;
11591
11592 if (name && strcmp (name, "char") == 0)
11593 TYPE_NOSIGN (type) = 1;
11594
11595 return set_die_type (die, type, cu);
11596 }
11597
11598 /* Read the given DW_AT_subrange DIE. */
11599
11600 static struct type *
11601 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11602 {
11603 struct type *base_type;
11604 struct type *range_type;
11605 struct attribute *attr;
11606 LONGEST low, high;
11607 int low_default_is_valid;
11608 char *name;
11609 LONGEST negative_mask;
11610
11611 base_type = die_type (die, cu);
11612 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11613 check_typedef (base_type);
11614
11615 /* The die_type call above may have already set the type for this DIE. */
11616 range_type = get_die_type (die, cu);
11617 if (range_type)
11618 return range_type;
11619
11620 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11621 omitting DW_AT_lower_bound. */
11622 switch (cu->language)
11623 {
11624 case language_c:
11625 case language_cplus:
11626 low = 0;
11627 low_default_is_valid = 1;
11628 break;
11629 case language_fortran:
11630 low = 1;
11631 low_default_is_valid = 1;
11632 break;
11633 case language_d:
11634 case language_java:
11635 case language_objc:
11636 low = 0;
11637 low_default_is_valid = (cu->header.version >= 4);
11638 break;
11639 case language_ada:
11640 case language_m2:
11641 case language_pascal:
11642 low = 1;
11643 low_default_is_valid = (cu->header.version >= 4);
11644 break;
11645 default:
11646 low = 0;
11647 low_default_is_valid = 0;
11648 break;
11649 }
11650
11651 /* FIXME: For variable sized arrays either of these could be
11652 a variable rather than a constant value. We'll allow it,
11653 but we don't know how to handle it. */
11654 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11655 if (attr)
11656 low = dwarf2_get_attr_constant_value (attr, low);
11657 else if (!low_default_is_valid)
11658 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11659 "- DIE at 0x%x [in module %s]"),
11660 die->offset.sect_off, cu->objfile->name);
11661
11662 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11663 if (attr)
11664 {
11665 if (attr_form_is_block (attr) || is_ref_attr (attr))
11666 {
11667 /* GCC encodes arrays with unspecified or dynamic length
11668 with a DW_FORM_block1 attribute or a reference attribute.
11669 FIXME: GDB does not yet know how to handle dynamic
11670 arrays properly, treat them as arrays with unspecified
11671 length for now.
11672
11673 FIXME: jimb/2003-09-22: GDB does not really know
11674 how to handle arrays of unspecified length
11675 either; we just represent them as zero-length
11676 arrays. Choose an appropriate upper bound given
11677 the lower bound we've computed above. */
11678 high = low - 1;
11679 }
11680 else
11681 high = dwarf2_get_attr_constant_value (attr, 1);
11682 }
11683 else
11684 {
11685 attr = dwarf2_attr (die, DW_AT_count, cu);
11686 if (attr)
11687 {
11688 int count = dwarf2_get_attr_constant_value (attr, 1);
11689 high = low + count - 1;
11690 }
11691 else
11692 {
11693 /* Unspecified array length. */
11694 high = low - 1;
11695 }
11696 }
11697
11698 /* Dwarf-2 specifications explicitly allows to create subrange types
11699 without specifying a base type.
11700 In that case, the base type must be set to the type of
11701 the lower bound, upper bound or count, in that order, if any of these
11702 three attributes references an object that has a type.
11703 If no base type is found, the Dwarf-2 specifications say that
11704 a signed integer type of size equal to the size of an address should
11705 be used.
11706 For the following C code: `extern char gdb_int [];'
11707 GCC produces an empty range DIE.
11708 FIXME: muller/2010-05-28: Possible references to object for low bound,
11709 high bound or count are not yet handled by this code. */
11710 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11711 {
11712 struct objfile *objfile = cu->objfile;
11713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11714 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11715 struct type *int_type = objfile_type (objfile)->builtin_int;
11716
11717 /* Test "int", "long int", and "long long int" objfile types,
11718 and select the first one having a size above or equal to the
11719 architecture address size. */
11720 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11721 base_type = int_type;
11722 else
11723 {
11724 int_type = objfile_type (objfile)->builtin_long;
11725 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11726 base_type = int_type;
11727 else
11728 {
11729 int_type = objfile_type (objfile)->builtin_long_long;
11730 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11731 base_type = int_type;
11732 }
11733 }
11734 }
11735
11736 negative_mask =
11737 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11738 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11739 low |= negative_mask;
11740 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11741 high |= negative_mask;
11742
11743 range_type = create_range_type (NULL, base_type, low, high);
11744
11745 /* Mark arrays with dynamic length at least as an array of unspecified
11746 length. GDB could check the boundary but before it gets implemented at
11747 least allow accessing the array elements. */
11748 if (attr && attr_form_is_block (attr))
11749 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11750
11751 /* Ada expects an empty array on no boundary attributes. */
11752 if (attr == NULL && cu->language != language_ada)
11753 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11754
11755 name = dwarf2_name (die, cu);
11756 if (name)
11757 TYPE_NAME (range_type) = name;
11758
11759 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11760 if (attr)
11761 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11762
11763 set_die_type (die, range_type, cu);
11764
11765 /* set_die_type should be already done. */
11766 set_descriptive_type (range_type, die, cu);
11767
11768 return range_type;
11769 }
11770
11771 static struct type *
11772 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11773 {
11774 struct type *type;
11775
11776 /* For now, we only support the C meaning of an unspecified type: void. */
11777
11778 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11779 TYPE_NAME (type) = dwarf2_name (die, cu);
11780
11781 return set_die_type (die, type, cu);
11782 }
11783
11784 /* Read a single die and all its descendents. Set the die's sibling
11785 field to NULL; set other fields in the die correctly, and set all
11786 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11787 location of the info_ptr after reading all of those dies. PARENT
11788 is the parent of the die in question. */
11789
11790 static struct die_info *
11791 read_die_and_children (const struct die_reader_specs *reader,
11792 gdb_byte *info_ptr,
11793 gdb_byte **new_info_ptr,
11794 struct die_info *parent)
11795 {
11796 struct die_info *die;
11797 gdb_byte *cur_ptr;
11798 int has_children;
11799
11800 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11801 if (die == NULL)
11802 {
11803 *new_info_ptr = cur_ptr;
11804 return NULL;
11805 }
11806 store_in_ref_table (die, reader->cu);
11807
11808 if (has_children)
11809 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11810 else
11811 {
11812 die->child = NULL;
11813 *new_info_ptr = cur_ptr;
11814 }
11815
11816 die->sibling = NULL;
11817 die->parent = parent;
11818 return die;
11819 }
11820
11821 /* Read a die, all of its descendents, and all of its siblings; set
11822 all of the fields of all of the dies correctly. Arguments are as
11823 in read_die_and_children. */
11824
11825 static struct die_info *
11826 read_die_and_siblings (const struct die_reader_specs *reader,
11827 gdb_byte *info_ptr,
11828 gdb_byte **new_info_ptr,
11829 struct die_info *parent)
11830 {
11831 struct die_info *first_die, *last_sibling;
11832 gdb_byte *cur_ptr;
11833
11834 cur_ptr = info_ptr;
11835 first_die = last_sibling = NULL;
11836
11837 while (1)
11838 {
11839 struct die_info *die
11840 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
11841
11842 if (die == NULL)
11843 {
11844 *new_info_ptr = cur_ptr;
11845 return first_die;
11846 }
11847
11848 if (!first_die)
11849 first_die = die;
11850 else
11851 last_sibling->sibling = die;
11852
11853 last_sibling = die;
11854 }
11855 }
11856
11857 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
11858 attributes.
11859 The caller is responsible for filling in the extra attributes
11860 and updating (*DIEP)->num_attrs.
11861 Set DIEP to point to a newly allocated die with its information,
11862 except for its child, sibling, and parent fields.
11863 Set HAS_CHILDREN to tell whether the die has children or not. */
11864
11865 static gdb_byte *
11866 read_full_die_1 (const struct die_reader_specs *reader,
11867 struct die_info **diep, gdb_byte *info_ptr,
11868 int *has_children, int num_extra_attrs)
11869 {
11870 unsigned int abbrev_number, bytes_read, i;
11871 sect_offset offset;
11872 struct abbrev_info *abbrev;
11873 struct die_info *die;
11874 struct dwarf2_cu *cu = reader->cu;
11875 bfd *abfd = reader->abfd;
11876
11877 offset.sect_off = info_ptr - reader->buffer;
11878 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11879 info_ptr += bytes_read;
11880 if (!abbrev_number)
11881 {
11882 *diep = NULL;
11883 *has_children = 0;
11884 return info_ptr;
11885 }
11886
11887 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
11888 if (!abbrev)
11889 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
11890 abbrev_number,
11891 bfd_get_filename (abfd));
11892
11893 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
11894 die->offset = offset;
11895 die->tag = abbrev->tag;
11896 die->abbrev = abbrev_number;
11897
11898 /* Make the result usable.
11899 The caller needs to update num_attrs after adding the extra
11900 attributes. */
11901 die->num_attrs = abbrev->num_attrs;
11902
11903 for (i = 0; i < abbrev->num_attrs; ++i)
11904 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
11905 info_ptr);
11906
11907 *diep = die;
11908 *has_children = abbrev->has_children;
11909 return info_ptr;
11910 }
11911
11912 /* Read a die and all its attributes.
11913 Set DIEP to point to a newly allocated die with its information,
11914 except for its child, sibling, and parent fields.
11915 Set HAS_CHILDREN to tell whether the die has children or not. */
11916
11917 static gdb_byte *
11918 read_full_die (const struct die_reader_specs *reader,
11919 struct die_info **diep, gdb_byte *info_ptr,
11920 int *has_children)
11921 {
11922 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
11923 }
11924 \f
11925 /* Abbreviation tables.
11926
11927 In DWARF version 2, the description of the debugging information is
11928 stored in a separate .debug_abbrev section. Before we read any
11929 dies from a section we read in all abbreviations and install them
11930 in a hash table. */
11931
11932 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
11933
11934 static struct abbrev_info *
11935 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
11936 {
11937 struct abbrev_info *abbrev;
11938
11939 abbrev = (struct abbrev_info *)
11940 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
11941 memset (abbrev, 0, sizeof (struct abbrev_info));
11942 return abbrev;
11943 }
11944
11945 /* Add an abbreviation to the table. */
11946
11947 static void
11948 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11949 unsigned int abbrev_number,
11950 struct abbrev_info *abbrev)
11951 {
11952 unsigned int hash_number;
11953
11954 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11955 abbrev->next = abbrev_table->abbrevs[hash_number];
11956 abbrev_table->abbrevs[hash_number] = abbrev;
11957 }
11958
11959 /* Look up an abbrev in the table.
11960 Returns NULL if the abbrev is not found. */
11961
11962 static struct abbrev_info *
11963 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11964 unsigned int abbrev_number)
11965 {
11966 unsigned int hash_number;
11967 struct abbrev_info *abbrev;
11968
11969 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11970 abbrev = abbrev_table->abbrevs[hash_number];
11971
11972 while (abbrev)
11973 {
11974 if (abbrev->number == abbrev_number)
11975 return abbrev;
11976 abbrev = abbrev->next;
11977 }
11978 return NULL;
11979 }
11980
11981 /* Read in an abbrev table. */
11982
11983 static struct abbrev_table *
11984 abbrev_table_read_table (struct dwarf2_section_info *section,
11985 sect_offset offset)
11986 {
11987 struct objfile *objfile = dwarf2_per_objfile->objfile;
11988 bfd *abfd = section->asection->owner;
11989 struct abbrev_table *abbrev_table;
11990 gdb_byte *abbrev_ptr;
11991 struct abbrev_info *cur_abbrev;
11992 unsigned int abbrev_number, bytes_read, abbrev_name;
11993 unsigned int abbrev_form;
11994 struct attr_abbrev *cur_attrs;
11995 unsigned int allocated_attrs;
11996
11997 abbrev_table = XMALLOC (struct abbrev_table);
11998 abbrev_table->offset = offset;
11999 obstack_init (&abbrev_table->abbrev_obstack);
12000 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12001 (ABBREV_HASH_SIZE
12002 * sizeof (struct abbrev_info *)));
12003 memset (abbrev_table->abbrevs, 0,
12004 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12005
12006 dwarf2_read_section (objfile, section);
12007 abbrev_ptr = section->buffer + offset.sect_off;
12008 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12009 abbrev_ptr += bytes_read;
12010
12011 allocated_attrs = ATTR_ALLOC_CHUNK;
12012 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12013
12014 /* Loop until we reach an abbrev number of 0. */
12015 while (abbrev_number)
12016 {
12017 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12018
12019 /* read in abbrev header */
12020 cur_abbrev->number = abbrev_number;
12021 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12022 abbrev_ptr += bytes_read;
12023 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12024 abbrev_ptr += 1;
12025
12026 /* now read in declarations */
12027 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12028 abbrev_ptr += bytes_read;
12029 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12030 abbrev_ptr += bytes_read;
12031 while (abbrev_name)
12032 {
12033 if (cur_abbrev->num_attrs == allocated_attrs)
12034 {
12035 allocated_attrs += ATTR_ALLOC_CHUNK;
12036 cur_attrs
12037 = xrealloc (cur_attrs, (allocated_attrs
12038 * sizeof (struct attr_abbrev)));
12039 }
12040
12041 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12042 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12043 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12044 abbrev_ptr += bytes_read;
12045 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12046 abbrev_ptr += bytes_read;
12047 }
12048
12049 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12050 (cur_abbrev->num_attrs
12051 * sizeof (struct attr_abbrev)));
12052 memcpy (cur_abbrev->attrs, cur_attrs,
12053 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12054
12055 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12056
12057 /* Get next abbreviation.
12058 Under Irix6 the abbreviations for a compilation unit are not
12059 always properly terminated with an abbrev number of 0.
12060 Exit loop if we encounter an abbreviation which we have
12061 already read (which means we are about to read the abbreviations
12062 for the next compile unit) or if the end of the abbreviation
12063 table is reached. */
12064 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12065 break;
12066 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12067 abbrev_ptr += bytes_read;
12068 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12069 break;
12070 }
12071
12072 xfree (cur_attrs);
12073 return abbrev_table;
12074 }
12075
12076 /* Free the resources held by ABBREV_TABLE. */
12077
12078 static void
12079 abbrev_table_free (struct abbrev_table *abbrev_table)
12080 {
12081 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12082 xfree (abbrev_table);
12083 }
12084
12085 /* Same as abbrev_table_free but as a cleanup.
12086 We pass in a pointer to the pointer to the table so that we can
12087 set the pointer to NULL when we're done. It also simplifies
12088 build_type_unit_groups. */
12089
12090 static void
12091 abbrev_table_free_cleanup (void *table_ptr)
12092 {
12093 struct abbrev_table **abbrev_table_ptr = table_ptr;
12094
12095 if (*abbrev_table_ptr != NULL)
12096 abbrev_table_free (*abbrev_table_ptr);
12097 *abbrev_table_ptr = NULL;
12098 }
12099
12100 /* Read the abbrev table for CU from ABBREV_SECTION. */
12101
12102 static void
12103 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12104 struct dwarf2_section_info *abbrev_section)
12105 {
12106 cu->abbrev_table =
12107 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12108 }
12109
12110 /* Release the memory used by the abbrev table for a compilation unit. */
12111
12112 static void
12113 dwarf2_free_abbrev_table (void *ptr_to_cu)
12114 {
12115 struct dwarf2_cu *cu = ptr_to_cu;
12116
12117 abbrev_table_free (cu->abbrev_table);
12118 /* Set this to NULL so that we SEGV if we try to read it later,
12119 and also because free_comp_unit verifies this is NULL. */
12120 cu->abbrev_table = NULL;
12121 }
12122 \f
12123 /* Returns nonzero if TAG represents a type that we might generate a partial
12124 symbol for. */
12125
12126 static int
12127 is_type_tag_for_partial (int tag)
12128 {
12129 switch (tag)
12130 {
12131 #if 0
12132 /* Some types that would be reasonable to generate partial symbols for,
12133 that we don't at present. */
12134 case DW_TAG_array_type:
12135 case DW_TAG_file_type:
12136 case DW_TAG_ptr_to_member_type:
12137 case DW_TAG_set_type:
12138 case DW_TAG_string_type:
12139 case DW_TAG_subroutine_type:
12140 #endif
12141 case DW_TAG_base_type:
12142 case DW_TAG_class_type:
12143 case DW_TAG_interface_type:
12144 case DW_TAG_enumeration_type:
12145 case DW_TAG_structure_type:
12146 case DW_TAG_subrange_type:
12147 case DW_TAG_typedef:
12148 case DW_TAG_union_type:
12149 return 1;
12150 default:
12151 return 0;
12152 }
12153 }
12154
12155 /* Load all DIEs that are interesting for partial symbols into memory. */
12156
12157 static struct partial_die_info *
12158 load_partial_dies (const struct die_reader_specs *reader,
12159 gdb_byte *info_ptr, int building_psymtab)
12160 {
12161 struct dwarf2_cu *cu = reader->cu;
12162 struct objfile *objfile = cu->objfile;
12163 struct partial_die_info *part_die;
12164 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12165 struct abbrev_info *abbrev;
12166 unsigned int bytes_read;
12167 unsigned int load_all = 0;
12168 int nesting_level = 1;
12169
12170 parent_die = NULL;
12171 last_die = NULL;
12172
12173 gdb_assert (cu->per_cu != NULL);
12174 if (cu->per_cu->load_all_dies)
12175 load_all = 1;
12176
12177 cu->partial_dies
12178 = htab_create_alloc_ex (cu->header.length / 12,
12179 partial_die_hash,
12180 partial_die_eq,
12181 NULL,
12182 &cu->comp_unit_obstack,
12183 hashtab_obstack_allocate,
12184 dummy_obstack_deallocate);
12185
12186 part_die = obstack_alloc (&cu->comp_unit_obstack,
12187 sizeof (struct partial_die_info));
12188
12189 while (1)
12190 {
12191 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12192
12193 /* A NULL abbrev means the end of a series of children. */
12194 if (abbrev == NULL)
12195 {
12196 if (--nesting_level == 0)
12197 {
12198 /* PART_DIE was probably the last thing allocated on the
12199 comp_unit_obstack, so we could call obstack_free
12200 here. We don't do that because the waste is small,
12201 and will be cleaned up when we're done with this
12202 compilation unit. This way, we're also more robust
12203 against other users of the comp_unit_obstack. */
12204 return first_die;
12205 }
12206 info_ptr += bytes_read;
12207 last_die = parent_die;
12208 parent_die = parent_die->die_parent;
12209 continue;
12210 }
12211
12212 /* Check for template arguments. We never save these; if
12213 they're seen, we just mark the parent, and go on our way. */
12214 if (parent_die != NULL
12215 && cu->language == language_cplus
12216 && (abbrev->tag == DW_TAG_template_type_param
12217 || abbrev->tag == DW_TAG_template_value_param))
12218 {
12219 parent_die->has_template_arguments = 1;
12220
12221 if (!load_all)
12222 {
12223 /* We don't need a partial DIE for the template argument. */
12224 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12225 continue;
12226 }
12227 }
12228
12229 /* We only recurse into c++ subprograms looking for template arguments.
12230 Skip their other children. */
12231 if (!load_all
12232 && cu->language == language_cplus
12233 && parent_die != NULL
12234 && parent_die->tag == DW_TAG_subprogram)
12235 {
12236 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12237 continue;
12238 }
12239
12240 /* Check whether this DIE is interesting enough to save. Normally
12241 we would not be interested in members here, but there may be
12242 later variables referencing them via DW_AT_specification (for
12243 static members). */
12244 if (!load_all
12245 && !is_type_tag_for_partial (abbrev->tag)
12246 && abbrev->tag != DW_TAG_constant
12247 && abbrev->tag != DW_TAG_enumerator
12248 && abbrev->tag != DW_TAG_subprogram
12249 && abbrev->tag != DW_TAG_lexical_block
12250 && abbrev->tag != DW_TAG_variable
12251 && abbrev->tag != DW_TAG_namespace
12252 && abbrev->tag != DW_TAG_module
12253 && abbrev->tag != DW_TAG_member
12254 && abbrev->tag != DW_TAG_imported_unit)
12255 {
12256 /* Otherwise we skip to the next sibling, if any. */
12257 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12258 continue;
12259 }
12260
12261 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12262 info_ptr);
12263
12264 /* This two-pass algorithm for processing partial symbols has a
12265 high cost in cache pressure. Thus, handle some simple cases
12266 here which cover the majority of C partial symbols. DIEs
12267 which neither have specification tags in them, nor could have
12268 specification tags elsewhere pointing at them, can simply be
12269 processed and discarded.
12270
12271 This segment is also optional; scan_partial_symbols and
12272 add_partial_symbol will handle these DIEs if we chain
12273 them in normally. When compilers which do not emit large
12274 quantities of duplicate debug information are more common,
12275 this code can probably be removed. */
12276
12277 /* Any complete simple types at the top level (pretty much all
12278 of them, for a language without namespaces), can be processed
12279 directly. */
12280 if (parent_die == NULL
12281 && part_die->has_specification == 0
12282 && part_die->is_declaration == 0
12283 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12284 || part_die->tag == DW_TAG_base_type
12285 || part_die->tag == DW_TAG_subrange_type))
12286 {
12287 if (building_psymtab && part_die->name != NULL)
12288 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12289 VAR_DOMAIN, LOC_TYPEDEF,
12290 &objfile->static_psymbols,
12291 0, (CORE_ADDR) 0, cu->language, objfile);
12292 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12293 continue;
12294 }
12295
12296 /* The exception for DW_TAG_typedef with has_children above is
12297 a workaround of GCC PR debug/47510. In the case of this complaint
12298 type_name_no_tag_or_error will error on such types later.
12299
12300 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12301 it could not find the child DIEs referenced later, this is checked
12302 above. In correct DWARF DW_TAG_typedef should have no children. */
12303
12304 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12305 complaint (&symfile_complaints,
12306 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12307 "- DIE at 0x%x [in module %s]"),
12308 part_die->offset.sect_off, objfile->name);
12309
12310 /* If we're at the second level, and we're an enumerator, and
12311 our parent has no specification (meaning possibly lives in a
12312 namespace elsewhere), then we can add the partial symbol now
12313 instead of queueing it. */
12314 if (part_die->tag == DW_TAG_enumerator
12315 && parent_die != NULL
12316 && parent_die->die_parent == NULL
12317 && parent_die->tag == DW_TAG_enumeration_type
12318 && parent_die->has_specification == 0)
12319 {
12320 if (part_die->name == NULL)
12321 complaint (&symfile_complaints,
12322 _("malformed enumerator DIE ignored"));
12323 else if (building_psymtab)
12324 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12325 VAR_DOMAIN, LOC_CONST,
12326 (cu->language == language_cplus
12327 || cu->language == language_java)
12328 ? &objfile->global_psymbols
12329 : &objfile->static_psymbols,
12330 0, (CORE_ADDR) 0, cu->language, objfile);
12331
12332 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12333 continue;
12334 }
12335
12336 /* We'll save this DIE so link it in. */
12337 part_die->die_parent = parent_die;
12338 part_die->die_sibling = NULL;
12339 part_die->die_child = NULL;
12340
12341 if (last_die && last_die == parent_die)
12342 last_die->die_child = part_die;
12343 else if (last_die)
12344 last_die->die_sibling = part_die;
12345
12346 last_die = part_die;
12347
12348 if (first_die == NULL)
12349 first_die = part_die;
12350
12351 /* Maybe add the DIE to the hash table. Not all DIEs that we
12352 find interesting need to be in the hash table, because we
12353 also have the parent/sibling/child chains; only those that we
12354 might refer to by offset later during partial symbol reading.
12355
12356 For now this means things that might have be the target of a
12357 DW_AT_specification, DW_AT_abstract_origin, or
12358 DW_AT_extension. DW_AT_extension will refer only to
12359 namespaces; DW_AT_abstract_origin refers to functions (and
12360 many things under the function DIE, but we do not recurse
12361 into function DIEs during partial symbol reading) and
12362 possibly variables as well; DW_AT_specification refers to
12363 declarations. Declarations ought to have the DW_AT_declaration
12364 flag. It happens that GCC forgets to put it in sometimes, but
12365 only for functions, not for types.
12366
12367 Adding more things than necessary to the hash table is harmless
12368 except for the performance cost. Adding too few will result in
12369 wasted time in find_partial_die, when we reread the compilation
12370 unit with load_all_dies set. */
12371
12372 if (load_all
12373 || abbrev->tag == DW_TAG_constant
12374 || abbrev->tag == DW_TAG_subprogram
12375 || abbrev->tag == DW_TAG_variable
12376 || abbrev->tag == DW_TAG_namespace
12377 || part_die->is_declaration)
12378 {
12379 void **slot;
12380
12381 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12382 part_die->offset.sect_off, INSERT);
12383 *slot = part_die;
12384 }
12385
12386 part_die = obstack_alloc (&cu->comp_unit_obstack,
12387 sizeof (struct partial_die_info));
12388
12389 /* For some DIEs we want to follow their children (if any). For C
12390 we have no reason to follow the children of structures; for other
12391 languages we have to, so that we can get at method physnames
12392 to infer fully qualified class names, for DW_AT_specification,
12393 and for C++ template arguments. For C++, we also look one level
12394 inside functions to find template arguments (if the name of the
12395 function does not already contain the template arguments).
12396
12397 For Ada, we need to scan the children of subprograms and lexical
12398 blocks as well because Ada allows the definition of nested
12399 entities that could be interesting for the debugger, such as
12400 nested subprograms for instance. */
12401 if (last_die->has_children
12402 && (load_all
12403 || last_die->tag == DW_TAG_namespace
12404 || last_die->tag == DW_TAG_module
12405 || last_die->tag == DW_TAG_enumeration_type
12406 || (cu->language == language_cplus
12407 && last_die->tag == DW_TAG_subprogram
12408 && (last_die->name == NULL
12409 || strchr (last_die->name, '<') == NULL))
12410 || (cu->language != language_c
12411 && (last_die->tag == DW_TAG_class_type
12412 || last_die->tag == DW_TAG_interface_type
12413 || last_die->tag == DW_TAG_structure_type
12414 || last_die->tag == DW_TAG_union_type))
12415 || (cu->language == language_ada
12416 && (last_die->tag == DW_TAG_subprogram
12417 || last_die->tag == DW_TAG_lexical_block))))
12418 {
12419 nesting_level++;
12420 parent_die = last_die;
12421 continue;
12422 }
12423
12424 /* Otherwise we skip to the next sibling, if any. */
12425 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12426
12427 /* Back to the top, do it again. */
12428 }
12429 }
12430
12431 /* Read a minimal amount of information into the minimal die structure. */
12432
12433 static gdb_byte *
12434 read_partial_die (const struct die_reader_specs *reader,
12435 struct partial_die_info *part_die,
12436 struct abbrev_info *abbrev, unsigned int abbrev_len,
12437 gdb_byte *info_ptr)
12438 {
12439 struct dwarf2_cu *cu = reader->cu;
12440 struct objfile *objfile = cu->objfile;
12441 gdb_byte *buffer = reader->buffer;
12442 unsigned int i;
12443 struct attribute attr;
12444 int has_low_pc_attr = 0;
12445 int has_high_pc_attr = 0;
12446 int high_pc_relative = 0;
12447
12448 memset (part_die, 0, sizeof (struct partial_die_info));
12449
12450 part_die->offset.sect_off = info_ptr - buffer;
12451
12452 info_ptr += abbrev_len;
12453
12454 if (abbrev == NULL)
12455 return info_ptr;
12456
12457 part_die->tag = abbrev->tag;
12458 part_die->has_children = abbrev->has_children;
12459
12460 for (i = 0; i < abbrev->num_attrs; ++i)
12461 {
12462 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12463
12464 /* Store the data if it is of an attribute we want to keep in a
12465 partial symbol table. */
12466 switch (attr.name)
12467 {
12468 case DW_AT_name:
12469 switch (part_die->tag)
12470 {
12471 case DW_TAG_compile_unit:
12472 case DW_TAG_partial_unit:
12473 case DW_TAG_type_unit:
12474 /* Compilation units have a DW_AT_name that is a filename, not
12475 a source language identifier. */
12476 case DW_TAG_enumeration_type:
12477 case DW_TAG_enumerator:
12478 /* These tags always have simple identifiers already; no need
12479 to canonicalize them. */
12480 part_die->name = DW_STRING (&attr);
12481 break;
12482 default:
12483 part_die->name
12484 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12485 &objfile->objfile_obstack);
12486 break;
12487 }
12488 break;
12489 case DW_AT_linkage_name:
12490 case DW_AT_MIPS_linkage_name:
12491 /* Note that both forms of linkage name might appear. We
12492 assume they will be the same, and we only store the last
12493 one we see. */
12494 if (cu->language == language_ada)
12495 part_die->name = DW_STRING (&attr);
12496 part_die->linkage_name = DW_STRING (&attr);
12497 break;
12498 case DW_AT_low_pc:
12499 has_low_pc_attr = 1;
12500 part_die->lowpc = DW_ADDR (&attr);
12501 break;
12502 case DW_AT_high_pc:
12503 has_high_pc_attr = 1;
12504 if (attr.form == DW_FORM_addr
12505 || attr.form == DW_FORM_GNU_addr_index)
12506 part_die->highpc = DW_ADDR (&attr);
12507 else
12508 {
12509 high_pc_relative = 1;
12510 part_die->highpc = DW_UNSND (&attr);
12511 }
12512 break;
12513 case DW_AT_location:
12514 /* Support the .debug_loc offsets. */
12515 if (attr_form_is_block (&attr))
12516 {
12517 part_die->d.locdesc = DW_BLOCK (&attr);
12518 }
12519 else if (attr_form_is_section_offset (&attr))
12520 {
12521 dwarf2_complex_location_expr_complaint ();
12522 }
12523 else
12524 {
12525 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12526 "partial symbol information");
12527 }
12528 break;
12529 case DW_AT_external:
12530 part_die->is_external = DW_UNSND (&attr);
12531 break;
12532 case DW_AT_declaration:
12533 part_die->is_declaration = DW_UNSND (&attr);
12534 break;
12535 case DW_AT_type:
12536 part_die->has_type = 1;
12537 break;
12538 case DW_AT_abstract_origin:
12539 case DW_AT_specification:
12540 case DW_AT_extension:
12541 part_die->has_specification = 1;
12542 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12543 break;
12544 case DW_AT_sibling:
12545 /* Ignore absolute siblings, they might point outside of
12546 the current compile unit. */
12547 if (attr.form == DW_FORM_ref_addr)
12548 complaint (&symfile_complaints,
12549 _("ignoring absolute DW_AT_sibling"));
12550 else
12551 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12552 break;
12553 case DW_AT_byte_size:
12554 part_die->has_byte_size = 1;
12555 break;
12556 case DW_AT_calling_convention:
12557 /* DWARF doesn't provide a way to identify a program's source-level
12558 entry point. DW_AT_calling_convention attributes are only meant
12559 to describe functions' calling conventions.
12560
12561 However, because it's a necessary piece of information in
12562 Fortran, and because DW_CC_program is the only piece of debugging
12563 information whose definition refers to a 'main program' at all,
12564 several compilers have begun marking Fortran main programs with
12565 DW_CC_program --- even when those functions use the standard
12566 calling conventions.
12567
12568 So until DWARF specifies a way to provide this information and
12569 compilers pick up the new representation, we'll support this
12570 practice. */
12571 if (DW_UNSND (&attr) == DW_CC_program
12572 && cu->language == language_fortran)
12573 {
12574 set_main_name (part_die->name);
12575
12576 /* As this DIE has a static linkage the name would be difficult
12577 to look up later. */
12578 language_of_main = language_fortran;
12579 }
12580 break;
12581 case DW_AT_inline:
12582 if (DW_UNSND (&attr) == DW_INL_inlined
12583 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12584 part_die->may_be_inlined = 1;
12585 break;
12586
12587 case DW_AT_import:
12588 if (part_die->tag == DW_TAG_imported_unit)
12589 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12590 break;
12591
12592 default:
12593 break;
12594 }
12595 }
12596
12597 if (high_pc_relative)
12598 part_die->highpc += part_die->lowpc;
12599
12600 if (has_low_pc_attr && has_high_pc_attr)
12601 {
12602 /* When using the GNU linker, .gnu.linkonce. sections are used to
12603 eliminate duplicate copies of functions and vtables and such.
12604 The linker will arbitrarily choose one and discard the others.
12605 The AT_*_pc values for such functions refer to local labels in
12606 these sections. If the section from that file was discarded, the
12607 labels are not in the output, so the relocs get a value of 0.
12608 If this is a discarded function, mark the pc bounds as invalid,
12609 so that GDB will ignore it. */
12610 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12611 {
12612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12613
12614 complaint (&symfile_complaints,
12615 _("DW_AT_low_pc %s is zero "
12616 "for DIE at 0x%x [in module %s]"),
12617 paddress (gdbarch, part_die->lowpc),
12618 part_die->offset.sect_off, objfile->name);
12619 }
12620 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12621 else if (part_die->lowpc >= part_die->highpc)
12622 {
12623 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12624
12625 complaint (&symfile_complaints,
12626 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12627 "for DIE at 0x%x [in module %s]"),
12628 paddress (gdbarch, part_die->lowpc),
12629 paddress (gdbarch, part_die->highpc),
12630 part_die->offset.sect_off, objfile->name);
12631 }
12632 else
12633 part_die->has_pc_info = 1;
12634 }
12635
12636 return info_ptr;
12637 }
12638
12639 /* Find a cached partial DIE at OFFSET in CU. */
12640
12641 static struct partial_die_info *
12642 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12643 {
12644 struct partial_die_info *lookup_die = NULL;
12645 struct partial_die_info part_die;
12646
12647 part_die.offset = offset;
12648 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12649 offset.sect_off);
12650
12651 return lookup_die;
12652 }
12653
12654 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12655 except in the case of .debug_types DIEs which do not reference
12656 outside their CU (they do however referencing other types via
12657 DW_FORM_ref_sig8). */
12658
12659 static struct partial_die_info *
12660 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
12661 {
12662 struct objfile *objfile = cu->objfile;
12663 struct dwarf2_per_cu_data *per_cu = NULL;
12664 struct partial_die_info *pd = NULL;
12665
12666 if (offset_in_cu_p (&cu->header, offset))
12667 {
12668 pd = find_partial_die_in_comp_unit (offset, cu);
12669 if (pd != NULL)
12670 return pd;
12671 /* We missed recording what we needed.
12672 Load all dies and try again. */
12673 per_cu = cu->per_cu;
12674 }
12675 else
12676 {
12677 /* TUs don't reference other CUs/TUs (except via type signatures). */
12678 if (cu->per_cu->is_debug_types)
12679 {
12680 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12681 " external reference to offset 0x%lx [in module %s].\n"),
12682 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12683 bfd_get_filename (objfile->obfd));
12684 }
12685 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
12686
12687 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12688 load_partial_comp_unit (per_cu);
12689
12690 per_cu->cu->last_used = 0;
12691 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12692 }
12693
12694 /* If we didn't find it, and not all dies have been loaded,
12695 load them all and try again. */
12696
12697 if (pd == NULL && per_cu->load_all_dies == 0)
12698 {
12699 per_cu->load_all_dies = 1;
12700
12701 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12702 THIS_CU->cu may already be in use. So we can't just free it and
12703 replace its DIEs with the ones we read in. Instead, we leave those
12704 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12705 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12706 set. */
12707 load_partial_comp_unit (per_cu);
12708
12709 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12710 }
12711
12712 if (pd == NULL)
12713 internal_error (__FILE__, __LINE__,
12714 _("could not find partial DIE 0x%x "
12715 "in cache [from module %s]\n"),
12716 offset.sect_off, bfd_get_filename (objfile->obfd));
12717 return pd;
12718 }
12719
12720 /* See if we can figure out if the class lives in a namespace. We do
12721 this by looking for a member function; its demangled name will
12722 contain namespace info, if there is any. */
12723
12724 static void
12725 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12726 struct dwarf2_cu *cu)
12727 {
12728 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12729 what template types look like, because the demangler
12730 frequently doesn't give the same name as the debug info. We
12731 could fix this by only using the demangled name to get the
12732 prefix (but see comment in read_structure_type). */
12733
12734 struct partial_die_info *real_pdi;
12735 struct partial_die_info *child_pdi;
12736
12737 /* If this DIE (this DIE's specification, if any) has a parent, then
12738 we should not do this. We'll prepend the parent's fully qualified
12739 name when we create the partial symbol. */
12740
12741 real_pdi = struct_pdi;
12742 while (real_pdi->has_specification)
12743 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
12744
12745 if (real_pdi->die_parent != NULL)
12746 return;
12747
12748 for (child_pdi = struct_pdi->die_child;
12749 child_pdi != NULL;
12750 child_pdi = child_pdi->die_sibling)
12751 {
12752 if (child_pdi->tag == DW_TAG_subprogram
12753 && child_pdi->linkage_name != NULL)
12754 {
12755 char *actual_class_name
12756 = language_class_name_from_physname (cu->language_defn,
12757 child_pdi->linkage_name);
12758 if (actual_class_name != NULL)
12759 {
12760 struct_pdi->name
12761 = obsavestring (actual_class_name,
12762 strlen (actual_class_name),
12763 &cu->objfile->objfile_obstack);
12764 xfree (actual_class_name);
12765 }
12766 break;
12767 }
12768 }
12769 }
12770
12771 /* Adjust PART_DIE before generating a symbol for it. This function
12772 may set the is_external flag or change the DIE's name. */
12773
12774 static void
12775 fixup_partial_die (struct partial_die_info *part_die,
12776 struct dwarf2_cu *cu)
12777 {
12778 /* Once we've fixed up a die, there's no point in doing so again.
12779 This also avoids a memory leak if we were to call
12780 guess_partial_die_structure_name multiple times. */
12781 if (part_die->fixup_called)
12782 return;
12783
12784 /* If we found a reference attribute and the DIE has no name, try
12785 to find a name in the referred to DIE. */
12786
12787 if (part_die->name == NULL && part_die->has_specification)
12788 {
12789 struct partial_die_info *spec_die;
12790
12791 spec_die = find_partial_die (part_die->spec_offset, cu);
12792
12793 fixup_partial_die (spec_die, cu);
12794
12795 if (spec_die->name)
12796 {
12797 part_die->name = spec_die->name;
12798
12799 /* Copy DW_AT_external attribute if it is set. */
12800 if (spec_die->is_external)
12801 part_die->is_external = spec_die->is_external;
12802 }
12803 }
12804
12805 /* Set default names for some unnamed DIEs. */
12806
12807 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12808 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12809
12810 /* If there is no parent die to provide a namespace, and there are
12811 children, see if we can determine the namespace from their linkage
12812 name. */
12813 if (cu->language == language_cplus
12814 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12815 && part_die->die_parent == NULL
12816 && part_die->has_children
12817 && (part_die->tag == DW_TAG_class_type
12818 || part_die->tag == DW_TAG_structure_type
12819 || part_die->tag == DW_TAG_union_type))
12820 guess_partial_die_structure_name (part_die, cu);
12821
12822 /* GCC might emit a nameless struct or union that has a linkage
12823 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12824 if (part_die->name == NULL
12825 && (part_die->tag == DW_TAG_class_type
12826 || part_die->tag == DW_TAG_interface_type
12827 || part_die->tag == DW_TAG_structure_type
12828 || part_die->tag == DW_TAG_union_type)
12829 && part_die->linkage_name != NULL)
12830 {
12831 char *demangled;
12832
12833 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
12834 if (demangled)
12835 {
12836 const char *base;
12837
12838 /* Strip any leading namespaces/classes, keep only the base name.
12839 DW_AT_name for named DIEs does not contain the prefixes. */
12840 base = strrchr (demangled, ':');
12841 if (base && base > demangled && base[-1] == ':')
12842 base++;
12843 else
12844 base = demangled;
12845
12846 part_die->name = obsavestring (base, strlen (base),
12847 &cu->objfile->objfile_obstack);
12848 xfree (demangled);
12849 }
12850 }
12851
12852 part_die->fixup_called = 1;
12853 }
12854
12855 /* Read an attribute value described by an attribute form. */
12856
12857 static gdb_byte *
12858 read_attribute_value (const struct die_reader_specs *reader,
12859 struct attribute *attr, unsigned form,
12860 gdb_byte *info_ptr)
12861 {
12862 struct dwarf2_cu *cu = reader->cu;
12863 bfd *abfd = reader->abfd;
12864 struct comp_unit_head *cu_header = &cu->header;
12865 unsigned int bytes_read;
12866 struct dwarf_block *blk;
12867
12868 attr->form = form;
12869 switch (form)
12870 {
12871 case DW_FORM_ref_addr:
12872 if (cu->header.version == 2)
12873 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
12874 else
12875 DW_UNSND (attr) = read_offset (abfd, info_ptr,
12876 &cu->header, &bytes_read);
12877 info_ptr += bytes_read;
12878 break;
12879 case DW_FORM_addr:
12880 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
12881 info_ptr += bytes_read;
12882 break;
12883 case DW_FORM_block2:
12884 blk = dwarf_alloc_block (cu);
12885 blk->size = read_2_bytes (abfd, info_ptr);
12886 info_ptr += 2;
12887 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12888 info_ptr += blk->size;
12889 DW_BLOCK (attr) = blk;
12890 break;
12891 case DW_FORM_block4:
12892 blk = dwarf_alloc_block (cu);
12893 blk->size = read_4_bytes (abfd, info_ptr);
12894 info_ptr += 4;
12895 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12896 info_ptr += blk->size;
12897 DW_BLOCK (attr) = blk;
12898 break;
12899 case DW_FORM_data2:
12900 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
12901 info_ptr += 2;
12902 break;
12903 case DW_FORM_data4:
12904 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
12905 info_ptr += 4;
12906 break;
12907 case DW_FORM_data8:
12908 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
12909 info_ptr += 8;
12910 break;
12911 case DW_FORM_sec_offset:
12912 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
12913 info_ptr += bytes_read;
12914 break;
12915 case DW_FORM_string:
12916 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
12917 DW_STRING_IS_CANONICAL (attr) = 0;
12918 info_ptr += bytes_read;
12919 break;
12920 case DW_FORM_strp:
12921 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
12922 &bytes_read);
12923 DW_STRING_IS_CANONICAL (attr) = 0;
12924 info_ptr += bytes_read;
12925 break;
12926 case DW_FORM_exprloc:
12927 case DW_FORM_block:
12928 blk = dwarf_alloc_block (cu);
12929 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12930 info_ptr += bytes_read;
12931 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12932 info_ptr += blk->size;
12933 DW_BLOCK (attr) = blk;
12934 break;
12935 case DW_FORM_block1:
12936 blk = dwarf_alloc_block (cu);
12937 blk->size = read_1_byte (abfd, info_ptr);
12938 info_ptr += 1;
12939 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12940 info_ptr += blk->size;
12941 DW_BLOCK (attr) = blk;
12942 break;
12943 case DW_FORM_data1:
12944 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12945 info_ptr += 1;
12946 break;
12947 case DW_FORM_flag:
12948 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12949 info_ptr += 1;
12950 break;
12951 case DW_FORM_flag_present:
12952 DW_UNSND (attr) = 1;
12953 break;
12954 case DW_FORM_sdata:
12955 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
12956 info_ptr += bytes_read;
12957 break;
12958 case DW_FORM_udata:
12959 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12960 info_ptr += bytes_read;
12961 break;
12962 case DW_FORM_ref1:
12963 DW_UNSND (attr) = (cu->header.offset.sect_off
12964 + read_1_byte (abfd, info_ptr));
12965 info_ptr += 1;
12966 break;
12967 case DW_FORM_ref2:
12968 DW_UNSND (attr) = (cu->header.offset.sect_off
12969 + read_2_bytes (abfd, info_ptr));
12970 info_ptr += 2;
12971 break;
12972 case DW_FORM_ref4:
12973 DW_UNSND (attr) = (cu->header.offset.sect_off
12974 + read_4_bytes (abfd, info_ptr));
12975 info_ptr += 4;
12976 break;
12977 case DW_FORM_ref8:
12978 DW_UNSND (attr) = (cu->header.offset.sect_off
12979 + read_8_bytes (abfd, info_ptr));
12980 info_ptr += 8;
12981 break;
12982 case DW_FORM_ref_sig8:
12983 /* Convert the signature to something we can record in DW_UNSND
12984 for later lookup.
12985 NOTE: This is NULL if the type wasn't found. */
12986 DW_SIGNATURED_TYPE (attr) =
12987 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
12988 info_ptr += 8;
12989 break;
12990 case DW_FORM_ref_udata:
12991 DW_UNSND (attr) = (cu->header.offset.sect_off
12992 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
12993 info_ptr += bytes_read;
12994 break;
12995 case DW_FORM_indirect:
12996 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12997 info_ptr += bytes_read;
12998 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
12999 break;
13000 case DW_FORM_GNU_addr_index:
13001 if (reader->dwo_file == NULL)
13002 {
13003 /* For now flag a hard error.
13004 Later we can turn this into a complaint. */
13005 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13006 dwarf_form_name (form),
13007 bfd_get_filename (abfd));
13008 }
13009 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13010 info_ptr += bytes_read;
13011 break;
13012 case DW_FORM_GNU_str_index:
13013 if (reader->dwo_file == NULL)
13014 {
13015 /* For now flag a hard error.
13016 Later we can turn this into a complaint if warranted. */
13017 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13018 dwarf_form_name (form),
13019 bfd_get_filename (abfd));
13020 }
13021 {
13022 ULONGEST str_index =
13023 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13024
13025 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13026 DW_STRING_IS_CANONICAL (attr) = 0;
13027 info_ptr += bytes_read;
13028 }
13029 break;
13030 default:
13031 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13032 dwarf_form_name (form),
13033 bfd_get_filename (abfd));
13034 }
13035
13036 /* We have seen instances where the compiler tried to emit a byte
13037 size attribute of -1 which ended up being encoded as an unsigned
13038 0xffffffff. Although 0xffffffff is technically a valid size value,
13039 an object of this size seems pretty unlikely so we can relatively
13040 safely treat these cases as if the size attribute was invalid and
13041 treat them as zero by default. */
13042 if (attr->name == DW_AT_byte_size
13043 && form == DW_FORM_data4
13044 && DW_UNSND (attr) >= 0xffffffff)
13045 {
13046 complaint
13047 (&symfile_complaints,
13048 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13049 hex_string (DW_UNSND (attr)));
13050 DW_UNSND (attr) = 0;
13051 }
13052
13053 return info_ptr;
13054 }
13055
13056 /* Read an attribute described by an abbreviated attribute. */
13057
13058 static gdb_byte *
13059 read_attribute (const struct die_reader_specs *reader,
13060 struct attribute *attr, struct attr_abbrev *abbrev,
13061 gdb_byte *info_ptr)
13062 {
13063 attr->name = abbrev->name;
13064 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13065 }
13066
13067 /* Read dwarf information from a buffer. */
13068
13069 static unsigned int
13070 read_1_byte (bfd *abfd, gdb_byte *buf)
13071 {
13072 return bfd_get_8 (abfd, buf);
13073 }
13074
13075 static int
13076 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13077 {
13078 return bfd_get_signed_8 (abfd, buf);
13079 }
13080
13081 static unsigned int
13082 read_2_bytes (bfd *abfd, gdb_byte *buf)
13083 {
13084 return bfd_get_16 (abfd, buf);
13085 }
13086
13087 static int
13088 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13089 {
13090 return bfd_get_signed_16 (abfd, buf);
13091 }
13092
13093 static unsigned int
13094 read_4_bytes (bfd *abfd, gdb_byte *buf)
13095 {
13096 return bfd_get_32 (abfd, buf);
13097 }
13098
13099 static int
13100 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13101 {
13102 return bfd_get_signed_32 (abfd, buf);
13103 }
13104
13105 static ULONGEST
13106 read_8_bytes (bfd *abfd, gdb_byte *buf)
13107 {
13108 return bfd_get_64 (abfd, buf);
13109 }
13110
13111 static CORE_ADDR
13112 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13113 unsigned int *bytes_read)
13114 {
13115 struct comp_unit_head *cu_header = &cu->header;
13116 CORE_ADDR retval = 0;
13117
13118 if (cu_header->signed_addr_p)
13119 {
13120 switch (cu_header->addr_size)
13121 {
13122 case 2:
13123 retval = bfd_get_signed_16 (abfd, buf);
13124 break;
13125 case 4:
13126 retval = bfd_get_signed_32 (abfd, buf);
13127 break;
13128 case 8:
13129 retval = bfd_get_signed_64 (abfd, buf);
13130 break;
13131 default:
13132 internal_error (__FILE__, __LINE__,
13133 _("read_address: bad switch, signed [in module %s]"),
13134 bfd_get_filename (abfd));
13135 }
13136 }
13137 else
13138 {
13139 switch (cu_header->addr_size)
13140 {
13141 case 2:
13142 retval = bfd_get_16 (abfd, buf);
13143 break;
13144 case 4:
13145 retval = bfd_get_32 (abfd, buf);
13146 break;
13147 case 8:
13148 retval = bfd_get_64 (abfd, buf);
13149 break;
13150 default:
13151 internal_error (__FILE__, __LINE__,
13152 _("read_address: bad switch, "
13153 "unsigned [in module %s]"),
13154 bfd_get_filename (abfd));
13155 }
13156 }
13157
13158 *bytes_read = cu_header->addr_size;
13159 return retval;
13160 }
13161
13162 /* Read the initial length from a section. The (draft) DWARF 3
13163 specification allows the initial length to take up either 4 bytes
13164 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13165 bytes describe the length and all offsets will be 8 bytes in length
13166 instead of 4.
13167
13168 An older, non-standard 64-bit format is also handled by this
13169 function. The older format in question stores the initial length
13170 as an 8-byte quantity without an escape value. Lengths greater
13171 than 2^32 aren't very common which means that the initial 4 bytes
13172 is almost always zero. Since a length value of zero doesn't make
13173 sense for the 32-bit format, this initial zero can be considered to
13174 be an escape value which indicates the presence of the older 64-bit
13175 format. As written, the code can't detect (old format) lengths
13176 greater than 4GB. If it becomes necessary to handle lengths
13177 somewhat larger than 4GB, we could allow other small values (such
13178 as the non-sensical values of 1, 2, and 3) to also be used as
13179 escape values indicating the presence of the old format.
13180
13181 The value returned via bytes_read should be used to increment the
13182 relevant pointer after calling read_initial_length().
13183
13184 [ Note: read_initial_length() and read_offset() are based on the
13185 document entitled "DWARF Debugging Information Format", revision
13186 3, draft 8, dated November 19, 2001. This document was obtained
13187 from:
13188
13189 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13190
13191 This document is only a draft and is subject to change. (So beware.)
13192
13193 Details regarding the older, non-standard 64-bit format were
13194 determined empirically by examining 64-bit ELF files produced by
13195 the SGI toolchain on an IRIX 6.5 machine.
13196
13197 - Kevin, July 16, 2002
13198 ] */
13199
13200 static LONGEST
13201 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13202 {
13203 LONGEST length = bfd_get_32 (abfd, buf);
13204
13205 if (length == 0xffffffff)
13206 {
13207 length = bfd_get_64 (abfd, buf + 4);
13208 *bytes_read = 12;
13209 }
13210 else if (length == 0)
13211 {
13212 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
13213 length = bfd_get_64 (abfd, buf);
13214 *bytes_read = 8;
13215 }
13216 else
13217 {
13218 *bytes_read = 4;
13219 }
13220
13221 return length;
13222 }
13223
13224 /* Cover function for read_initial_length.
13225 Returns the length of the object at BUF, and stores the size of the
13226 initial length in *BYTES_READ and stores the size that offsets will be in
13227 *OFFSET_SIZE.
13228 If the initial length size is not equivalent to that specified in
13229 CU_HEADER then issue a complaint.
13230 This is useful when reading non-comp-unit headers. */
13231
13232 static LONGEST
13233 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13234 const struct comp_unit_head *cu_header,
13235 unsigned int *bytes_read,
13236 unsigned int *offset_size)
13237 {
13238 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13239
13240 gdb_assert (cu_header->initial_length_size == 4
13241 || cu_header->initial_length_size == 8
13242 || cu_header->initial_length_size == 12);
13243
13244 if (cu_header->initial_length_size != *bytes_read)
13245 complaint (&symfile_complaints,
13246 _("intermixed 32-bit and 64-bit DWARF sections"));
13247
13248 *offset_size = (*bytes_read == 4) ? 4 : 8;
13249 return length;
13250 }
13251
13252 /* Read an offset from the data stream. The size of the offset is
13253 given by cu_header->offset_size. */
13254
13255 static LONGEST
13256 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13257 unsigned int *bytes_read)
13258 {
13259 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13260
13261 *bytes_read = cu_header->offset_size;
13262 return offset;
13263 }
13264
13265 /* Read an offset from the data stream. */
13266
13267 static LONGEST
13268 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13269 {
13270 LONGEST retval = 0;
13271
13272 switch (offset_size)
13273 {
13274 case 4:
13275 retval = bfd_get_32 (abfd, buf);
13276 break;
13277 case 8:
13278 retval = bfd_get_64 (abfd, buf);
13279 break;
13280 default:
13281 internal_error (__FILE__, __LINE__,
13282 _("read_offset_1: bad switch [in module %s]"),
13283 bfd_get_filename (abfd));
13284 }
13285
13286 return retval;
13287 }
13288
13289 static gdb_byte *
13290 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13291 {
13292 /* If the size of a host char is 8 bits, we can return a pointer
13293 to the buffer, otherwise we have to copy the data to a buffer
13294 allocated on the temporary obstack. */
13295 gdb_assert (HOST_CHAR_BIT == 8);
13296 return buf;
13297 }
13298
13299 static char *
13300 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13301 {
13302 /* If the size of a host char is 8 bits, we can return a pointer
13303 to the string, otherwise we have to copy the string to a buffer
13304 allocated on the temporary obstack. */
13305 gdb_assert (HOST_CHAR_BIT == 8);
13306 if (*buf == '\0')
13307 {
13308 *bytes_read_ptr = 1;
13309 return NULL;
13310 }
13311 *bytes_read_ptr = strlen ((char *) buf) + 1;
13312 return (char *) buf;
13313 }
13314
13315 static char *
13316 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13317 {
13318 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13319 if (dwarf2_per_objfile->str.buffer == NULL)
13320 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13321 bfd_get_filename (abfd));
13322 if (str_offset >= dwarf2_per_objfile->str.size)
13323 error (_("DW_FORM_strp pointing outside of "
13324 ".debug_str section [in module %s]"),
13325 bfd_get_filename (abfd));
13326 gdb_assert (HOST_CHAR_BIT == 8);
13327 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13328 return NULL;
13329 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13330 }
13331
13332 static char *
13333 read_indirect_string (bfd *abfd, gdb_byte *buf,
13334 const struct comp_unit_head *cu_header,
13335 unsigned int *bytes_read_ptr)
13336 {
13337 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13338
13339 return read_indirect_string_at_offset (abfd, str_offset);
13340 }
13341
13342 static ULONGEST
13343 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13344 {
13345 ULONGEST result;
13346 unsigned int num_read;
13347 int i, shift;
13348 unsigned char byte;
13349
13350 result = 0;
13351 shift = 0;
13352 num_read = 0;
13353 i = 0;
13354 while (1)
13355 {
13356 byte = bfd_get_8 (abfd, buf);
13357 buf++;
13358 num_read++;
13359 result |= ((ULONGEST) (byte & 127) << shift);
13360 if ((byte & 128) == 0)
13361 {
13362 break;
13363 }
13364 shift += 7;
13365 }
13366 *bytes_read_ptr = num_read;
13367 return result;
13368 }
13369
13370 static LONGEST
13371 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13372 {
13373 LONGEST result;
13374 int i, shift, num_read;
13375 unsigned char byte;
13376
13377 result = 0;
13378 shift = 0;
13379 num_read = 0;
13380 i = 0;
13381 while (1)
13382 {
13383 byte = bfd_get_8 (abfd, buf);
13384 buf++;
13385 num_read++;
13386 result |= ((LONGEST) (byte & 127) << shift);
13387 shift += 7;
13388 if ((byte & 128) == 0)
13389 {
13390 break;
13391 }
13392 }
13393 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13394 result |= -(((LONGEST) 1) << shift);
13395 *bytes_read_ptr = num_read;
13396 return result;
13397 }
13398
13399 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13400 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13401 ADDR_SIZE is the size of addresses from the CU header. */
13402
13403 static CORE_ADDR
13404 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13405 {
13406 struct objfile *objfile = dwarf2_per_objfile->objfile;
13407 bfd *abfd = objfile->obfd;
13408 const gdb_byte *info_ptr;
13409
13410 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13411 if (dwarf2_per_objfile->addr.buffer == NULL)
13412 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13413 objfile->name);
13414 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13415 error (_("DW_FORM_addr_index pointing outside of "
13416 ".debug_addr section [in module %s]"),
13417 objfile->name);
13418 info_ptr = (dwarf2_per_objfile->addr.buffer
13419 + addr_base + addr_index * addr_size);
13420 if (addr_size == 4)
13421 return bfd_get_32 (abfd, info_ptr);
13422 else
13423 return bfd_get_64 (abfd, info_ptr);
13424 }
13425
13426 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13427
13428 static CORE_ADDR
13429 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13430 {
13431 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13432 }
13433
13434 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13435
13436 static CORE_ADDR
13437 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13438 unsigned int *bytes_read)
13439 {
13440 bfd *abfd = cu->objfile->obfd;
13441 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13442
13443 return read_addr_index (cu, addr_index);
13444 }
13445
13446 /* Data structure to pass results from dwarf2_read_addr_index_reader
13447 back to dwarf2_read_addr_index. */
13448
13449 struct dwarf2_read_addr_index_data
13450 {
13451 ULONGEST addr_base;
13452 int addr_size;
13453 };
13454
13455 /* die_reader_func for dwarf2_read_addr_index. */
13456
13457 static void
13458 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13459 gdb_byte *info_ptr,
13460 struct die_info *comp_unit_die,
13461 int has_children,
13462 void *data)
13463 {
13464 struct dwarf2_cu *cu = reader->cu;
13465 struct dwarf2_read_addr_index_data *aidata =
13466 (struct dwarf2_read_addr_index_data *) data;
13467
13468 aidata->addr_base = cu->addr_base;
13469 aidata->addr_size = cu->header.addr_size;
13470 }
13471
13472 /* Given an index in .debug_addr, fetch the value.
13473 NOTE: This can be called during dwarf expression evaluation,
13474 long after the debug information has been read, and thus per_cu->cu
13475 may no longer exist. */
13476
13477 CORE_ADDR
13478 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13479 unsigned int addr_index)
13480 {
13481 struct objfile *objfile = per_cu->objfile;
13482 struct dwarf2_cu *cu = per_cu->cu;
13483 ULONGEST addr_base;
13484 int addr_size;
13485
13486 /* This is intended to be called from outside this file. */
13487 dw2_setup (objfile);
13488
13489 /* We need addr_base and addr_size.
13490 If we don't have PER_CU->cu, we have to get it.
13491 Nasty, but the alternative is storing the needed info in PER_CU,
13492 which at this point doesn't seem justified: it's not clear how frequently
13493 it would get used and it would increase the size of every PER_CU.
13494 Entry points like dwarf2_per_cu_addr_size do a similar thing
13495 so we're not in uncharted territory here.
13496 Alas we need to be a bit more complicated as addr_base is contained
13497 in the DIE.
13498
13499 We don't need to read the entire CU(/TU).
13500 We just need the header and top level die.
13501 IWBN to use the aging mechanism to let us lazily later discard the CU.
13502 See however init_cutu_and_read_dies_simple. */
13503
13504 if (cu != NULL)
13505 {
13506 addr_base = cu->addr_base;
13507 addr_size = cu->header.addr_size;
13508 }
13509 else
13510 {
13511 struct dwarf2_read_addr_index_data aidata;
13512
13513 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13514 &aidata);
13515 addr_base = aidata.addr_base;
13516 addr_size = aidata.addr_size;
13517 }
13518
13519 return read_addr_index_1 (addr_index, addr_base, addr_size);
13520 }
13521
13522 /* Given a DW_AT_str_index, fetch the string. */
13523
13524 static char *
13525 read_str_index (const struct die_reader_specs *reader,
13526 struct dwarf2_cu *cu, ULONGEST str_index)
13527 {
13528 struct objfile *objfile = dwarf2_per_objfile->objfile;
13529 const char *dwo_name = objfile->name;
13530 bfd *abfd = objfile->obfd;
13531 struct dwo_sections *sections = &reader->dwo_file->sections;
13532 gdb_byte *info_ptr;
13533 ULONGEST str_offset;
13534
13535 dwarf2_read_section (objfile, &sections->str);
13536 dwarf2_read_section (objfile, &sections->str_offsets);
13537 if (sections->str.buffer == NULL)
13538 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13539 " in CU at offset 0x%lx [in module %s]"),
13540 (long) cu->header.offset.sect_off, dwo_name);
13541 if (sections->str_offsets.buffer == NULL)
13542 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13543 " in CU at offset 0x%lx [in module %s]"),
13544 (long) cu->header.offset.sect_off, dwo_name);
13545 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13546 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13547 " section in CU at offset 0x%lx [in module %s]"),
13548 (long) cu->header.offset.sect_off, dwo_name);
13549 info_ptr = (sections->str_offsets.buffer
13550 + str_index * cu->header.offset_size);
13551 if (cu->header.offset_size == 4)
13552 str_offset = bfd_get_32 (abfd, info_ptr);
13553 else
13554 str_offset = bfd_get_64 (abfd, info_ptr);
13555 if (str_offset >= sections->str.size)
13556 error (_("Offset from DW_FORM_str_index pointing outside of"
13557 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13558 (long) cu->header.offset.sect_off, dwo_name);
13559 return (char *) (sections->str.buffer + str_offset);
13560 }
13561
13562 /* Return the length of an LEB128 number in BUF. */
13563
13564 static int
13565 leb128_size (const gdb_byte *buf)
13566 {
13567 const gdb_byte *begin = buf;
13568 gdb_byte byte;
13569
13570 while (1)
13571 {
13572 byte = *buf++;
13573 if ((byte & 128) == 0)
13574 return buf - begin;
13575 }
13576 }
13577
13578 static void
13579 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13580 {
13581 switch (lang)
13582 {
13583 case DW_LANG_C89:
13584 case DW_LANG_C99:
13585 case DW_LANG_C:
13586 cu->language = language_c;
13587 break;
13588 case DW_LANG_C_plus_plus:
13589 cu->language = language_cplus;
13590 break;
13591 case DW_LANG_D:
13592 cu->language = language_d;
13593 break;
13594 case DW_LANG_Fortran77:
13595 case DW_LANG_Fortran90:
13596 case DW_LANG_Fortran95:
13597 cu->language = language_fortran;
13598 break;
13599 case DW_LANG_Go:
13600 cu->language = language_go;
13601 break;
13602 case DW_LANG_Mips_Assembler:
13603 cu->language = language_asm;
13604 break;
13605 case DW_LANG_Java:
13606 cu->language = language_java;
13607 break;
13608 case DW_LANG_Ada83:
13609 case DW_LANG_Ada95:
13610 cu->language = language_ada;
13611 break;
13612 case DW_LANG_Modula2:
13613 cu->language = language_m2;
13614 break;
13615 case DW_LANG_Pascal83:
13616 cu->language = language_pascal;
13617 break;
13618 case DW_LANG_ObjC:
13619 cu->language = language_objc;
13620 break;
13621 case DW_LANG_Cobol74:
13622 case DW_LANG_Cobol85:
13623 default:
13624 cu->language = language_minimal;
13625 break;
13626 }
13627 cu->language_defn = language_def (cu->language);
13628 }
13629
13630 /* Return the named attribute or NULL if not there. */
13631
13632 static struct attribute *
13633 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13634 {
13635 for (;;)
13636 {
13637 unsigned int i;
13638 struct attribute *spec = NULL;
13639
13640 for (i = 0; i < die->num_attrs; ++i)
13641 {
13642 if (die->attrs[i].name == name)
13643 return &die->attrs[i];
13644 if (die->attrs[i].name == DW_AT_specification
13645 || die->attrs[i].name == DW_AT_abstract_origin)
13646 spec = &die->attrs[i];
13647 }
13648
13649 if (!spec)
13650 break;
13651
13652 die = follow_die_ref (die, spec, &cu);
13653 }
13654
13655 return NULL;
13656 }
13657
13658 /* Return the named attribute or NULL if not there,
13659 but do not follow DW_AT_specification, etc.
13660 This is for use in contexts where we're reading .debug_types dies.
13661 Following DW_AT_specification, DW_AT_abstract_origin will take us
13662 back up the chain, and we want to go down. */
13663
13664 static struct attribute *
13665 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13666 {
13667 unsigned int i;
13668
13669 for (i = 0; i < die->num_attrs; ++i)
13670 if (die->attrs[i].name == name)
13671 return &die->attrs[i];
13672
13673 return NULL;
13674 }
13675
13676 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13677 and holds a non-zero value. This function should only be used for
13678 DW_FORM_flag or DW_FORM_flag_present attributes. */
13679
13680 static int
13681 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13682 {
13683 struct attribute *attr = dwarf2_attr (die, name, cu);
13684
13685 return (attr && DW_UNSND (attr));
13686 }
13687
13688 static int
13689 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13690 {
13691 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13692 which value is non-zero. However, we have to be careful with
13693 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13694 (via dwarf2_flag_true_p) follows this attribute. So we may
13695 end up accidently finding a declaration attribute that belongs
13696 to a different DIE referenced by the specification attribute,
13697 even though the given DIE does not have a declaration attribute. */
13698 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13699 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13700 }
13701
13702 /* Return the die giving the specification for DIE, if there is
13703 one. *SPEC_CU is the CU containing DIE on input, and the CU
13704 containing the return value on output. If there is no
13705 specification, but there is an abstract origin, that is
13706 returned. */
13707
13708 static struct die_info *
13709 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13710 {
13711 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13712 *spec_cu);
13713
13714 if (spec_attr == NULL)
13715 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13716
13717 if (spec_attr == NULL)
13718 return NULL;
13719 else
13720 return follow_die_ref (die, spec_attr, spec_cu);
13721 }
13722
13723 /* Free the line_header structure *LH, and any arrays and strings it
13724 refers to.
13725 NOTE: This is also used as a "cleanup" function. */
13726
13727 static void
13728 free_line_header (struct line_header *lh)
13729 {
13730 if (lh->standard_opcode_lengths)
13731 xfree (lh->standard_opcode_lengths);
13732
13733 /* Remember that all the lh->file_names[i].name pointers are
13734 pointers into debug_line_buffer, and don't need to be freed. */
13735 if (lh->file_names)
13736 xfree (lh->file_names);
13737
13738 /* Similarly for the include directory names. */
13739 if (lh->include_dirs)
13740 xfree (lh->include_dirs);
13741
13742 xfree (lh);
13743 }
13744
13745 /* Add an entry to LH's include directory table. */
13746
13747 static void
13748 add_include_dir (struct line_header *lh, char *include_dir)
13749 {
13750 /* Grow the array if necessary. */
13751 if (lh->include_dirs_size == 0)
13752 {
13753 lh->include_dirs_size = 1; /* for testing */
13754 lh->include_dirs = xmalloc (lh->include_dirs_size
13755 * sizeof (*lh->include_dirs));
13756 }
13757 else if (lh->num_include_dirs >= lh->include_dirs_size)
13758 {
13759 lh->include_dirs_size *= 2;
13760 lh->include_dirs = xrealloc (lh->include_dirs,
13761 (lh->include_dirs_size
13762 * sizeof (*lh->include_dirs)));
13763 }
13764
13765 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13766 }
13767
13768 /* Add an entry to LH's file name table. */
13769
13770 static void
13771 add_file_name (struct line_header *lh,
13772 char *name,
13773 unsigned int dir_index,
13774 unsigned int mod_time,
13775 unsigned int length)
13776 {
13777 struct file_entry *fe;
13778
13779 /* Grow the array if necessary. */
13780 if (lh->file_names_size == 0)
13781 {
13782 lh->file_names_size = 1; /* for testing */
13783 lh->file_names = xmalloc (lh->file_names_size
13784 * sizeof (*lh->file_names));
13785 }
13786 else if (lh->num_file_names >= lh->file_names_size)
13787 {
13788 lh->file_names_size *= 2;
13789 lh->file_names = xrealloc (lh->file_names,
13790 (lh->file_names_size
13791 * sizeof (*lh->file_names)));
13792 }
13793
13794 fe = &lh->file_names[lh->num_file_names++];
13795 fe->name = name;
13796 fe->dir_index = dir_index;
13797 fe->mod_time = mod_time;
13798 fe->length = length;
13799 fe->included_p = 0;
13800 fe->symtab = NULL;
13801 }
13802
13803 /* Read the statement program header starting at OFFSET in
13804 .debug_line, or .debug_line.dwo. Return a pointer
13805 to a struct line_header, allocated using xmalloc.
13806
13807 NOTE: the strings in the include directory and file name tables of
13808 the returned object point into the dwarf line section buffer,
13809 and must not be freed. */
13810
13811 static struct line_header *
13812 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
13813 {
13814 struct cleanup *back_to;
13815 struct line_header *lh;
13816 gdb_byte *line_ptr;
13817 unsigned int bytes_read, offset_size;
13818 int i;
13819 char *cur_dir, *cur_file;
13820 struct dwarf2_section_info *section;
13821 bfd *abfd;
13822
13823 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
13824 DWO file. */
13825 if (cu->dwo_unit && cu->per_cu->is_debug_types)
13826 section = &cu->dwo_unit->dwo_file->sections.line;
13827 else
13828 section = &dwarf2_per_objfile->line;
13829
13830 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
13831 if (section->buffer == NULL)
13832 {
13833 if (cu->dwo_unit && cu->per_cu->is_debug_types)
13834 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
13835 else
13836 complaint (&symfile_complaints, _("missing .debug_line section"));
13837 return 0;
13838 }
13839
13840 /* We can't do this until we know the section is non-empty.
13841 Only then do we know we have such a section. */
13842 abfd = section->asection->owner;
13843
13844 /* Make sure that at least there's room for the total_length field.
13845 That could be 12 bytes long, but we're just going to fudge that. */
13846 if (offset + 4 >= section->size)
13847 {
13848 dwarf2_statement_list_fits_in_line_number_section_complaint ();
13849 return 0;
13850 }
13851
13852 lh = xmalloc (sizeof (*lh));
13853 memset (lh, 0, sizeof (*lh));
13854 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
13855 (void *) lh);
13856
13857 line_ptr = section->buffer + offset;
13858
13859 /* Read in the header. */
13860 lh->total_length =
13861 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
13862 &bytes_read, &offset_size);
13863 line_ptr += bytes_read;
13864 if (line_ptr + lh->total_length > (section->buffer + section->size))
13865 {
13866 dwarf2_statement_list_fits_in_line_number_section_complaint ();
13867 return 0;
13868 }
13869 lh->statement_program_end = line_ptr + lh->total_length;
13870 lh->version = read_2_bytes (abfd, line_ptr);
13871 line_ptr += 2;
13872 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
13873 line_ptr += offset_size;
13874 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
13875 line_ptr += 1;
13876 if (lh->version >= 4)
13877 {
13878 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
13879 line_ptr += 1;
13880 }
13881 else
13882 lh->maximum_ops_per_instruction = 1;
13883
13884 if (lh->maximum_ops_per_instruction == 0)
13885 {
13886 lh->maximum_ops_per_instruction = 1;
13887 complaint (&symfile_complaints,
13888 _("invalid maximum_ops_per_instruction "
13889 "in `.debug_line' section"));
13890 }
13891
13892 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
13893 line_ptr += 1;
13894 lh->line_base = read_1_signed_byte (abfd, line_ptr);
13895 line_ptr += 1;
13896 lh->line_range = read_1_byte (abfd, line_ptr);
13897 line_ptr += 1;
13898 lh->opcode_base = read_1_byte (abfd, line_ptr);
13899 line_ptr += 1;
13900 lh->standard_opcode_lengths
13901 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
13902
13903 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
13904 for (i = 1; i < lh->opcode_base; ++i)
13905 {
13906 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
13907 line_ptr += 1;
13908 }
13909
13910 /* Read directory table. */
13911 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
13912 {
13913 line_ptr += bytes_read;
13914 add_include_dir (lh, cur_dir);
13915 }
13916 line_ptr += bytes_read;
13917
13918 /* Read file name table. */
13919 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
13920 {
13921 unsigned int dir_index, mod_time, length;
13922
13923 line_ptr += bytes_read;
13924 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13925 line_ptr += bytes_read;
13926 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13927 line_ptr += bytes_read;
13928 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13929 line_ptr += bytes_read;
13930
13931 add_file_name (lh, cur_file, dir_index, mod_time, length);
13932 }
13933 line_ptr += bytes_read;
13934 lh->statement_program_start = line_ptr;
13935
13936 if (line_ptr > (section->buffer + section->size))
13937 complaint (&symfile_complaints,
13938 _("line number info header doesn't "
13939 "fit in `.debug_line' section"));
13940
13941 discard_cleanups (back_to);
13942 return lh;
13943 }
13944
13945 /* Subroutine of dwarf_decode_lines to simplify it.
13946 Return the file name of the psymtab for included file FILE_INDEX
13947 in line header LH of PST.
13948 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13949 If space for the result is malloc'd, it will be freed by a cleanup.
13950 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
13951
13952 static char *
13953 psymtab_include_file_name (const struct line_header *lh, int file_index,
13954 const struct partial_symtab *pst,
13955 const char *comp_dir)
13956 {
13957 const struct file_entry fe = lh->file_names [file_index];
13958 char *include_name = fe.name;
13959 char *include_name_to_compare = include_name;
13960 char *dir_name = NULL;
13961 const char *pst_filename;
13962 char *copied_name = NULL;
13963 int file_is_pst;
13964
13965 if (fe.dir_index)
13966 dir_name = lh->include_dirs[fe.dir_index - 1];
13967
13968 if (!IS_ABSOLUTE_PATH (include_name)
13969 && (dir_name != NULL || comp_dir != NULL))
13970 {
13971 /* Avoid creating a duplicate psymtab for PST.
13972 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13973 Before we do the comparison, however, we need to account
13974 for DIR_NAME and COMP_DIR.
13975 First prepend dir_name (if non-NULL). If we still don't
13976 have an absolute path prepend comp_dir (if non-NULL).
13977 However, the directory we record in the include-file's
13978 psymtab does not contain COMP_DIR (to match the
13979 corresponding symtab(s)).
13980
13981 Example:
13982
13983 bash$ cd /tmp
13984 bash$ gcc -g ./hello.c
13985 include_name = "hello.c"
13986 dir_name = "."
13987 DW_AT_comp_dir = comp_dir = "/tmp"
13988 DW_AT_name = "./hello.c" */
13989
13990 if (dir_name != NULL)
13991 {
13992 include_name = concat (dir_name, SLASH_STRING,
13993 include_name, (char *)NULL);
13994 include_name_to_compare = include_name;
13995 make_cleanup (xfree, include_name);
13996 }
13997 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13998 {
13999 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14000 include_name, (char *)NULL);
14001 }
14002 }
14003
14004 pst_filename = pst->filename;
14005 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14006 {
14007 copied_name = concat (pst->dirname, SLASH_STRING,
14008 pst_filename, (char *)NULL);
14009 pst_filename = copied_name;
14010 }
14011
14012 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14013
14014 if (include_name_to_compare != include_name)
14015 xfree (include_name_to_compare);
14016 if (copied_name != NULL)
14017 xfree (copied_name);
14018
14019 if (file_is_pst)
14020 return NULL;
14021 return include_name;
14022 }
14023
14024 /* Ignore this record_line request. */
14025
14026 static void
14027 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14028 {
14029 return;
14030 }
14031
14032 /* Subroutine of dwarf_decode_lines to simplify it.
14033 Process the line number information in LH. */
14034
14035 static void
14036 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14037 struct dwarf2_cu *cu, struct partial_symtab *pst)
14038 {
14039 gdb_byte *line_ptr, *extended_end;
14040 gdb_byte *line_end;
14041 unsigned int bytes_read, extended_len;
14042 unsigned char op_code, extended_op, adj_opcode;
14043 CORE_ADDR baseaddr;
14044 struct objfile *objfile = cu->objfile;
14045 bfd *abfd = objfile->obfd;
14046 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14047 const int decode_for_pst_p = (pst != NULL);
14048 struct subfile *last_subfile = NULL;
14049 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14050 = record_line;
14051
14052 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14053
14054 line_ptr = lh->statement_program_start;
14055 line_end = lh->statement_program_end;
14056
14057 /* Read the statement sequences until there's nothing left. */
14058 while (line_ptr < line_end)
14059 {
14060 /* state machine registers */
14061 CORE_ADDR address = 0;
14062 unsigned int file = 1;
14063 unsigned int line = 1;
14064 unsigned int column = 0;
14065 int is_stmt = lh->default_is_stmt;
14066 int basic_block = 0;
14067 int end_sequence = 0;
14068 CORE_ADDR addr;
14069 unsigned char op_index = 0;
14070
14071 if (!decode_for_pst_p && lh->num_file_names >= file)
14072 {
14073 /* Start a subfile for the current file of the state machine. */
14074 /* lh->include_dirs and lh->file_names are 0-based, but the
14075 directory and file name numbers in the statement program
14076 are 1-based. */
14077 struct file_entry *fe = &lh->file_names[file - 1];
14078 char *dir = NULL;
14079
14080 if (fe->dir_index)
14081 dir = lh->include_dirs[fe->dir_index - 1];
14082
14083 dwarf2_start_subfile (fe->name, dir, comp_dir);
14084 }
14085
14086 /* Decode the table. */
14087 while (!end_sequence)
14088 {
14089 op_code = read_1_byte (abfd, line_ptr);
14090 line_ptr += 1;
14091 if (line_ptr > line_end)
14092 {
14093 dwarf2_debug_line_missing_end_sequence_complaint ();
14094 break;
14095 }
14096
14097 if (op_code >= lh->opcode_base)
14098 {
14099 /* Special operand. */
14100 adj_opcode = op_code - lh->opcode_base;
14101 address += (((op_index + (adj_opcode / lh->line_range))
14102 / lh->maximum_ops_per_instruction)
14103 * lh->minimum_instruction_length);
14104 op_index = ((op_index + (adj_opcode / lh->line_range))
14105 % lh->maximum_ops_per_instruction);
14106 line += lh->line_base + (adj_opcode % lh->line_range);
14107 if (lh->num_file_names < file || file == 0)
14108 dwarf2_debug_line_missing_file_complaint ();
14109 /* For now we ignore lines not starting on an
14110 instruction boundary. */
14111 else if (op_index == 0)
14112 {
14113 lh->file_names[file - 1].included_p = 1;
14114 if (!decode_for_pst_p && is_stmt)
14115 {
14116 if (last_subfile != current_subfile)
14117 {
14118 addr = gdbarch_addr_bits_remove (gdbarch, address);
14119 if (last_subfile)
14120 (*p_record_line) (last_subfile, 0, addr);
14121 last_subfile = current_subfile;
14122 }
14123 /* Append row to matrix using current values. */
14124 addr = gdbarch_addr_bits_remove (gdbarch, address);
14125 (*p_record_line) (current_subfile, line, addr);
14126 }
14127 }
14128 basic_block = 0;
14129 }
14130 else switch (op_code)
14131 {
14132 case DW_LNS_extended_op:
14133 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14134 &bytes_read);
14135 line_ptr += bytes_read;
14136 extended_end = line_ptr + extended_len;
14137 extended_op = read_1_byte (abfd, line_ptr);
14138 line_ptr += 1;
14139 switch (extended_op)
14140 {
14141 case DW_LNE_end_sequence:
14142 p_record_line = record_line;
14143 end_sequence = 1;
14144 break;
14145 case DW_LNE_set_address:
14146 address = read_address (abfd, line_ptr, cu, &bytes_read);
14147
14148 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14149 {
14150 /* This line table is for a function which has been
14151 GCd by the linker. Ignore it. PR gdb/12528 */
14152
14153 long line_offset
14154 = line_ptr - dwarf2_per_objfile->line.buffer;
14155
14156 complaint (&symfile_complaints,
14157 _(".debug_line address at offset 0x%lx is 0 "
14158 "[in module %s]"),
14159 line_offset, objfile->name);
14160 p_record_line = noop_record_line;
14161 }
14162
14163 op_index = 0;
14164 line_ptr += bytes_read;
14165 address += baseaddr;
14166 break;
14167 case DW_LNE_define_file:
14168 {
14169 char *cur_file;
14170 unsigned int dir_index, mod_time, length;
14171
14172 cur_file = read_direct_string (abfd, line_ptr,
14173 &bytes_read);
14174 line_ptr += bytes_read;
14175 dir_index =
14176 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14177 line_ptr += bytes_read;
14178 mod_time =
14179 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14180 line_ptr += bytes_read;
14181 length =
14182 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14183 line_ptr += bytes_read;
14184 add_file_name (lh, cur_file, dir_index, mod_time, length);
14185 }
14186 break;
14187 case DW_LNE_set_discriminator:
14188 /* The discriminator is not interesting to the debugger;
14189 just ignore it. */
14190 line_ptr = extended_end;
14191 break;
14192 default:
14193 complaint (&symfile_complaints,
14194 _("mangled .debug_line section"));
14195 return;
14196 }
14197 /* Make sure that we parsed the extended op correctly. If e.g.
14198 we expected a different address size than the producer used,
14199 we may have read the wrong number of bytes. */
14200 if (line_ptr != extended_end)
14201 {
14202 complaint (&symfile_complaints,
14203 _("mangled .debug_line section"));
14204 return;
14205 }
14206 break;
14207 case DW_LNS_copy:
14208 if (lh->num_file_names < file || file == 0)
14209 dwarf2_debug_line_missing_file_complaint ();
14210 else
14211 {
14212 lh->file_names[file - 1].included_p = 1;
14213 if (!decode_for_pst_p && is_stmt)
14214 {
14215 if (last_subfile != current_subfile)
14216 {
14217 addr = gdbarch_addr_bits_remove (gdbarch, address);
14218 if (last_subfile)
14219 (*p_record_line) (last_subfile, 0, addr);
14220 last_subfile = current_subfile;
14221 }
14222 addr = gdbarch_addr_bits_remove (gdbarch, address);
14223 (*p_record_line) (current_subfile, line, addr);
14224 }
14225 }
14226 basic_block = 0;
14227 break;
14228 case DW_LNS_advance_pc:
14229 {
14230 CORE_ADDR adjust
14231 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14232
14233 address += (((op_index + adjust)
14234 / lh->maximum_ops_per_instruction)
14235 * lh->minimum_instruction_length);
14236 op_index = ((op_index + adjust)
14237 % lh->maximum_ops_per_instruction);
14238 line_ptr += bytes_read;
14239 }
14240 break;
14241 case DW_LNS_advance_line:
14242 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14243 line_ptr += bytes_read;
14244 break;
14245 case DW_LNS_set_file:
14246 {
14247 /* The arrays lh->include_dirs and lh->file_names are
14248 0-based, but the directory and file name numbers in
14249 the statement program are 1-based. */
14250 struct file_entry *fe;
14251 char *dir = NULL;
14252
14253 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14254 line_ptr += bytes_read;
14255 if (lh->num_file_names < file || file == 0)
14256 dwarf2_debug_line_missing_file_complaint ();
14257 else
14258 {
14259 fe = &lh->file_names[file - 1];
14260 if (fe->dir_index)
14261 dir = lh->include_dirs[fe->dir_index - 1];
14262 if (!decode_for_pst_p)
14263 {
14264 last_subfile = current_subfile;
14265 dwarf2_start_subfile (fe->name, dir, comp_dir);
14266 }
14267 }
14268 }
14269 break;
14270 case DW_LNS_set_column:
14271 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14272 line_ptr += bytes_read;
14273 break;
14274 case DW_LNS_negate_stmt:
14275 is_stmt = (!is_stmt);
14276 break;
14277 case DW_LNS_set_basic_block:
14278 basic_block = 1;
14279 break;
14280 /* Add to the address register of the state machine the
14281 address increment value corresponding to special opcode
14282 255. I.e., this value is scaled by the minimum
14283 instruction length since special opcode 255 would have
14284 scaled the increment. */
14285 case DW_LNS_const_add_pc:
14286 {
14287 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14288
14289 address += (((op_index + adjust)
14290 / lh->maximum_ops_per_instruction)
14291 * lh->minimum_instruction_length);
14292 op_index = ((op_index + adjust)
14293 % lh->maximum_ops_per_instruction);
14294 }
14295 break;
14296 case DW_LNS_fixed_advance_pc:
14297 address += read_2_bytes (abfd, line_ptr);
14298 op_index = 0;
14299 line_ptr += 2;
14300 break;
14301 default:
14302 {
14303 /* Unknown standard opcode, ignore it. */
14304 int i;
14305
14306 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14307 {
14308 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14309 line_ptr += bytes_read;
14310 }
14311 }
14312 }
14313 }
14314 if (lh->num_file_names < file || file == 0)
14315 dwarf2_debug_line_missing_file_complaint ();
14316 else
14317 {
14318 lh->file_names[file - 1].included_p = 1;
14319 if (!decode_for_pst_p)
14320 {
14321 addr = gdbarch_addr_bits_remove (gdbarch, address);
14322 (*p_record_line) (current_subfile, 0, addr);
14323 }
14324 }
14325 }
14326 }
14327
14328 /* Decode the Line Number Program (LNP) for the given line_header
14329 structure and CU. The actual information extracted and the type
14330 of structures created from the LNP depends on the value of PST.
14331
14332 1. If PST is NULL, then this procedure uses the data from the program
14333 to create all necessary symbol tables, and their linetables.
14334
14335 2. If PST is not NULL, this procedure reads the program to determine
14336 the list of files included by the unit represented by PST, and
14337 builds all the associated partial symbol tables.
14338
14339 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14340 It is used for relative paths in the line table.
14341 NOTE: When processing partial symtabs (pst != NULL),
14342 comp_dir == pst->dirname.
14343
14344 NOTE: It is important that psymtabs have the same file name (via strcmp)
14345 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14346 symtab we don't use it in the name of the psymtabs we create.
14347 E.g. expand_line_sal requires this when finding psymtabs to expand.
14348 A good testcase for this is mb-inline.exp. */
14349
14350 static void
14351 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14352 struct dwarf2_cu *cu, struct partial_symtab *pst,
14353 int want_line_info)
14354 {
14355 struct objfile *objfile = cu->objfile;
14356 const int decode_for_pst_p = (pst != NULL);
14357 struct subfile *first_subfile = current_subfile;
14358
14359 if (want_line_info)
14360 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14361
14362 if (decode_for_pst_p)
14363 {
14364 int file_index;
14365
14366 /* Now that we're done scanning the Line Header Program, we can
14367 create the psymtab of each included file. */
14368 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14369 if (lh->file_names[file_index].included_p == 1)
14370 {
14371 char *include_name =
14372 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14373 if (include_name != NULL)
14374 dwarf2_create_include_psymtab (include_name, pst, objfile);
14375 }
14376 }
14377 else
14378 {
14379 /* Make sure a symtab is created for every file, even files
14380 which contain only variables (i.e. no code with associated
14381 line numbers). */
14382 int i;
14383
14384 for (i = 0; i < lh->num_file_names; i++)
14385 {
14386 char *dir = NULL;
14387 struct file_entry *fe;
14388
14389 fe = &lh->file_names[i];
14390 if (fe->dir_index)
14391 dir = lh->include_dirs[fe->dir_index - 1];
14392 dwarf2_start_subfile (fe->name, dir, comp_dir);
14393
14394 /* Skip the main file; we don't need it, and it must be
14395 allocated last, so that it will show up before the
14396 non-primary symtabs in the objfile's symtab list. */
14397 if (current_subfile == first_subfile)
14398 continue;
14399
14400 if (current_subfile->symtab == NULL)
14401 current_subfile->symtab = allocate_symtab (current_subfile->name,
14402 objfile);
14403 fe->symtab = current_subfile->symtab;
14404 }
14405 }
14406 }
14407
14408 /* Start a subfile for DWARF. FILENAME is the name of the file and
14409 DIRNAME the name of the source directory which contains FILENAME
14410 or NULL if not known. COMP_DIR is the compilation directory for the
14411 linetable's compilation unit or NULL if not known.
14412 This routine tries to keep line numbers from identical absolute and
14413 relative file names in a common subfile.
14414
14415 Using the `list' example from the GDB testsuite, which resides in
14416 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14417 of /srcdir/list0.c yields the following debugging information for list0.c:
14418
14419 DW_AT_name: /srcdir/list0.c
14420 DW_AT_comp_dir: /compdir
14421 files.files[0].name: list0.h
14422 files.files[0].dir: /srcdir
14423 files.files[1].name: list0.c
14424 files.files[1].dir: /srcdir
14425
14426 The line number information for list0.c has to end up in a single
14427 subfile, so that `break /srcdir/list0.c:1' works as expected.
14428 start_subfile will ensure that this happens provided that we pass the
14429 concatenation of files.files[1].dir and files.files[1].name as the
14430 subfile's name. */
14431
14432 static void
14433 dwarf2_start_subfile (char *filename, const char *dirname,
14434 const char *comp_dir)
14435 {
14436 char *fullname;
14437
14438 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14439 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14440 second argument to start_subfile. To be consistent, we do the
14441 same here. In order not to lose the line information directory,
14442 we concatenate it to the filename when it makes sense.
14443 Note that the Dwarf3 standard says (speaking of filenames in line
14444 information): ``The directory index is ignored for file names
14445 that represent full path names''. Thus ignoring dirname in the
14446 `else' branch below isn't an issue. */
14447
14448 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14449 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14450 else
14451 fullname = filename;
14452
14453 start_subfile (fullname, comp_dir);
14454
14455 if (fullname != filename)
14456 xfree (fullname);
14457 }
14458
14459 /* Start a symtab for DWARF.
14460 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14461
14462 static void
14463 dwarf2_start_symtab (struct dwarf2_cu *cu,
14464 char *name, char *comp_dir, CORE_ADDR low_pc)
14465 {
14466 start_symtab (name, comp_dir, low_pc);
14467 record_debugformat ("DWARF 2");
14468 record_producer (cu->producer);
14469
14470 /* We assume that we're processing GCC output. */
14471 processing_gcc_compilation = 2;
14472
14473 processing_has_namespace_info = 0;
14474 }
14475
14476 static void
14477 var_decode_location (struct attribute *attr, struct symbol *sym,
14478 struct dwarf2_cu *cu)
14479 {
14480 struct objfile *objfile = cu->objfile;
14481 struct comp_unit_head *cu_header = &cu->header;
14482
14483 /* NOTE drow/2003-01-30: There used to be a comment and some special
14484 code here to turn a symbol with DW_AT_external and a
14485 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14486 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14487 with some versions of binutils) where shared libraries could have
14488 relocations against symbols in their debug information - the
14489 minimal symbol would have the right address, but the debug info
14490 would not. It's no longer necessary, because we will explicitly
14491 apply relocations when we read in the debug information now. */
14492
14493 /* A DW_AT_location attribute with no contents indicates that a
14494 variable has been optimized away. */
14495 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14496 {
14497 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14498 return;
14499 }
14500
14501 /* Handle one degenerate form of location expression specially, to
14502 preserve GDB's previous behavior when section offsets are
14503 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14504 then mark this symbol as LOC_STATIC. */
14505
14506 if (attr_form_is_block (attr)
14507 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14508 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14509 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14510 && (DW_BLOCK (attr)->size
14511 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14512 {
14513 unsigned int dummy;
14514
14515 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14516 SYMBOL_VALUE_ADDRESS (sym) =
14517 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14518 else
14519 SYMBOL_VALUE_ADDRESS (sym) =
14520 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14521 SYMBOL_CLASS (sym) = LOC_STATIC;
14522 fixup_symbol_section (sym, objfile);
14523 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14524 SYMBOL_SECTION (sym));
14525 return;
14526 }
14527
14528 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14529 expression evaluator, and use LOC_COMPUTED only when necessary
14530 (i.e. when the value of a register or memory location is
14531 referenced, or a thread-local block, etc.). Then again, it might
14532 not be worthwhile. I'm assuming that it isn't unless performance
14533 or memory numbers show me otherwise. */
14534
14535 dwarf2_symbol_mark_computed (attr, sym, cu);
14536 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14537
14538 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14539 cu->has_loclist = 1;
14540 }
14541
14542 /* Given a pointer to a DWARF information entry, figure out if we need
14543 to make a symbol table entry for it, and if so, create a new entry
14544 and return a pointer to it.
14545 If TYPE is NULL, determine symbol type from the die, otherwise
14546 used the passed type.
14547 If SPACE is not NULL, use it to hold the new symbol. If it is
14548 NULL, allocate a new symbol on the objfile's obstack. */
14549
14550 static struct symbol *
14551 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14552 struct symbol *space)
14553 {
14554 struct objfile *objfile = cu->objfile;
14555 struct symbol *sym = NULL;
14556 char *name;
14557 struct attribute *attr = NULL;
14558 struct attribute *attr2 = NULL;
14559 CORE_ADDR baseaddr;
14560 struct pending **list_to_add = NULL;
14561
14562 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14563
14564 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14565
14566 name = dwarf2_name (die, cu);
14567 if (name)
14568 {
14569 const char *linkagename;
14570 int suppress_add = 0;
14571
14572 if (space)
14573 sym = space;
14574 else
14575 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14576 OBJSTAT (objfile, n_syms++);
14577
14578 /* Cache this symbol's name and the name's demangled form (if any). */
14579 SYMBOL_SET_LANGUAGE (sym, cu->language);
14580 linkagename = dwarf2_physname (name, die, cu);
14581 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14582
14583 /* Fortran does not have mangling standard and the mangling does differ
14584 between gfortran, iFort etc. */
14585 if (cu->language == language_fortran
14586 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14587 symbol_set_demangled_name (&(sym->ginfo),
14588 (char *) dwarf2_full_name (name, die, cu),
14589 NULL);
14590
14591 /* Default assumptions.
14592 Use the passed type or decode it from the die. */
14593 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14594 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14595 if (type != NULL)
14596 SYMBOL_TYPE (sym) = type;
14597 else
14598 SYMBOL_TYPE (sym) = die_type (die, cu);
14599 attr = dwarf2_attr (die,
14600 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14601 cu);
14602 if (attr)
14603 {
14604 SYMBOL_LINE (sym) = DW_UNSND (attr);
14605 }
14606
14607 attr = dwarf2_attr (die,
14608 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14609 cu);
14610 if (attr)
14611 {
14612 int file_index = DW_UNSND (attr);
14613
14614 if (cu->line_header == NULL
14615 || file_index > cu->line_header->num_file_names)
14616 complaint (&symfile_complaints,
14617 _("file index out of range"));
14618 else if (file_index > 0)
14619 {
14620 struct file_entry *fe;
14621
14622 fe = &cu->line_header->file_names[file_index - 1];
14623 SYMBOL_SYMTAB (sym) = fe->symtab;
14624 }
14625 }
14626
14627 switch (die->tag)
14628 {
14629 case DW_TAG_label:
14630 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14631 if (attr)
14632 {
14633 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14634 }
14635 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14636 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14637 SYMBOL_CLASS (sym) = LOC_LABEL;
14638 add_symbol_to_list (sym, cu->list_in_scope);
14639 break;
14640 case DW_TAG_subprogram:
14641 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14642 finish_block. */
14643 SYMBOL_CLASS (sym) = LOC_BLOCK;
14644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14645 if ((attr2 && (DW_UNSND (attr2) != 0))
14646 || cu->language == language_ada)
14647 {
14648 /* Subprograms marked external are stored as a global symbol.
14649 Ada subprograms, whether marked external or not, are always
14650 stored as a global symbol, because we want to be able to
14651 access them globally. For instance, we want to be able
14652 to break on a nested subprogram without having to
14653 specify the context. */
14654 list_to_add = &global_symbols;
14655 }
14656 else
14657 {
14658 list_to_add = cu->list_in_scope;
14659 }
14660 break;
14661 case DW_TAG_inlined_subroutine:
14662 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14663 finish_block. */
14664 SYMBOL_CLASS (sym) = LOC_BLOCK;
14665 SYMBOL_INLINED (sym) = 1;
14666 list_to_add = cu->list_in_scope;
14667 break;
14668 case DW_TAG_template_value_param:
14669 suppress_add = 1;
14670 /* Fall through. */
14671 case DW_TAG_constant:
14672 case DW_TAG_variable:
14673 case DW_TAG_member:
14674 /* Compilation with minimal debug info may result in
14675 variables with missing type entries. Change the
14676 misleading `void' type to something sensible. */
14677 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14678 SYMBOL_TYPE (sym)
14679 = objfile_type (objfile)->nodebug_data_symbol;
14680
14681 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14682 /* In the case of DW_TAG_member, we should only be called for
14683 static const members. */
14684 if (die->tag == DW_TAG_member)
14685 {
14686 /* dwarf2_add_field uses die_is_declaration,
14687 so we do the same. */
14688 gdb_assert (die_is_declaration (die, cu));
14689 gdb_assert (attr);
14690 }
14691 if (attr)
14692 {
14693 dwarf2_const_value (attr, sym, cu);
14694 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14695 if (!suppress_add)
14696 {
14697 if (attr2 && (DW_UNSND (attr2) != 0))
14698 list_to_add = &global_symbols;
14699 else
14700 list_to_add = cu->list_in_scope;
14701 }
14702 break;
14703 }
14704 attr = dwarf2_attr (die, DW_AT_location, cu);
14705 if (attr)
14706 {
14707 var_decode_location (attr, sym, cu);
14708 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14709 if (SYMBOL_CLASS (sym) == LOC_STATIC
14710 && SYMBOL_VALUE_ADDRESS (sym) == 0
14711 && !dwarf2_per_objfile->has_section_at_zero)
14712 {
14713 /* When a static variable is eliminated by the linker,
14714 the corresponding debug information is not stripped
14715 out, but the variable address is set to null;
14716 do not add such variables into symbol table. */
14717 }
14718 else if (attr2 && (DW_UNSND (attr2) != 0))
14719 {
14720 /* Workaround gfortran PR debug/40040 - it uses
14721 DW_AT_location for variables in -fPIC libraries which may
14722 get overriden by other libraries/executable and get
14723 a different address. Resolve it by the minimal symbol
14724 which may come from inferior's executable using copy
14725 relocation. Make this workaround only for gfortran as for
14726 other compilers GDB cannot guess the minimal symbol
14727 Fortran mangling kind. */
14728 if (cu->language == language_fortran && die->parent
14729 && die->parent->tag == DW_TAG_module
14730 && cu->producer
14731 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14732 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14733
14734 /* A variable with DW_AT_external is never static,
14735 but it may be block-scoped. */
14736 list_to_add = (cu->list_in_scope == &file_symbols
14737 ? &global_symbols : cu->list_in_scope);
14738 }
14739 else
14740 list_to_add = cu->list_in_scope;
14741 }
14742 else
14743 {
14744 /* We do not know the address of this symbol.
14745 If it is an external symbol and we have type information
14746 for it, enter the symbol as a LOC_UNRESOLVED symbol.
14747 The address of the variable will then be determined from
14748 the minimal symbol table whenever the variable is
14749 referenced. */
14750 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14751 if (attr2 && (DW_UNSND (attr2) != 0)
14752 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
14753 {
14754 /* A variable with DW_AT_external is never static, but it
14755 may be block-scoped. */
14756 list_to_add = (cu->list_in_scope == &file_symbols
14757 ? &global_symbols : cu->list_in_scope);
14758
14759 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14760 }
14761 else if (!die_is_declaration (die, cu))
14762 {
14763 /* Use the default LOC_OPTIMIZED_OUT class. */
14764 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
14765 if (!suppress_add)
14766 list_to_add = cu->list_in_scope;
14767 }
14768 }
14769 break;
14770 case DW_TAG_formal_parameter:
14771 /* If we are inside a function, mark this as an argument. If
14772 not, we might be looking at an argument to an inlined function
14773 when we do not have enough information to show inlined frames;
14774 pretend it's a local variable in that case so that the user can
14775 still see it. */
14776 if (context_stack_depth > 0
14777 && context_stack[context_stack_depth - 1].name != NULL)
14778 SYMBOL_IS_ARGUMENT (sym) = 1;
14779 attr = dwarf2_attr (die, DW_AT_location, cu);
14780 if (attr)
14781 {
14782 var_decode_location (attr, sym, cu);
14783 }
14784 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14785 if (attr)
14786 {
14787 dwarf2_const_value (attr, sym, cu);
14788 }
14789
14790 list_to_add = cu->list_in_scope;
14791 break;
14792 case DW_TAG_unspecified_parameters:
14793 /* From varargs functions; gdb doesn't seem to have any
14794 interest in this information, so just ignore it for now.
14795 (FIXME?) */
14796 break;
14797 case DW_TAG_template_type_param:
14798 suppress_add = 1;
14799 /* Fall through. */
14800 case DW_TAG_class_type:
14801 case DW_TAG_interface_type:
14802 case DW_TAG_structure_type:
14803 case DW_TAG_union_type:
14804 case DW_TAG_set_type:
14805 case DW_TAG_enumeration_type:
14806 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14807 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
14808
14809 {
14810 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
14811 really ever be static objects: otherwise, if you try
14812 to, say, break of a class's method and you're in a file
14813 which doesn't mention that class, it won't work unless
14814 the check for all static symbols in lookup_symbol_aux
14815 saves you. See the OtherFileClass tests in
14816 gdb.c++/namespace.exp. */
14817
14818 if (!suppress_add)
14819 {
14820 list_to_add = (cu->list_in_scope == &file_symbols
14821 && (cu->language == language_cplus
14822 || cu->language == language_java)
14823 ? &global_symbols : cu->list_in_scope);
14824
14825 /* The semantics of C++ state that "struct foo {
14826 ... }" also defines a typedef for "foo". A Java
14827 class declaration also defines a typedef for the
14828 class. */
14829 if (cu->language == language_cplus
14830 || cu->language == language_java
14831 || cu->language == language_ada)
14832 {
14833 /* The symbol's name is already allocated along
14834 with this objfile, so we don't need to
14835 duplicate it for the type. */
14836 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
14837 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
14838 }
14839 }
14840 }
14841 break;
14842 case DW_TAG_typedef:
14843 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14844 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14845 list_to_add = cu->list_in_scope;
14846 break;
14847 case DW_TAG_base_type:
14848 case DW_TAG_subrange_type:
14849 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14850 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14851 list_to_add = cu->list_in_scope;
14852 break;
14853 case DW_TAG_enumerator:
14854 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14855 if (attr)
14856 {
14857 dwarf2_const_value (attr, sym, cu);
14858 }
14859 {
14860 /* NOTE: carlton/2003-11-10: See comment above in the
14861 DW_TAG_class_type, etc. block. */
14862
14863 list_to_add = (cu->list_in_scope == &file_symbols
14864 && (cu->language == language_cplus
14865 || cu->language == language_java)
14866 ? &global_symbols : cu->list_in_scope);
14867 }
14868 break;
14869 case DW_TAG_namespace:
14870 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14871 list_to_add = &global_symbols;
14872 break;
14873 default:
14874 /* Not a tag we recognize. Hopefully we aren't processing
14875 trash data, but since we must specifically ignore things
14876 we don't recognize, there is nothing else we should do at
14877 this point. */
14878 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
14879 dwarf_tag_name (die->tag));
14880 break;
14881 }
14882
14883 if (suppress_add)
14884 {
14885 sym->hash_next = objfile->template_symbols;
14886 objfile->template_symbols = sym;
14887 list_to_add = NULL;
14888 }
14889
14890 if (list_to_add != NULL)
14891 add_symbol_to_list (sym, list_to_add);
14892
14893 /* For the benefit of old versions of GCC, check for anonymous
14894 namespaces based on the demangled name. */
14895 if (!processing_has_namespace_info
14896 && cu->language == language_cplus)
14897 cp_scan_for_anonymous_namespaces (sym, objfile);
14898 }
14899 return (sym);
14900 }
14901
14902 /* A wrapper for new_symbol_full that always allocates a new symbol. */
14903
14904 static struct symbol *
14905 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14906 {
14907 return new_symbol_full (die, type, cu, NULL);
14908 }
14909
14910 /* Given an attr with a DW_FORM_dataN value in host byte order,
14911 zero-extend it as appropriate for the symbol's type. The DWARF
14912 standard (v4) is not entirely clear about the meaning of using
14913 DW_FORM_dataN for a constant with a signed type, where the type is
14914 wider than the data. The conclusion of a discussion on the DWARF
14915 list was that this is unspecified. We choose to always zero-extend
14916 because that is the interpretation long in use by GCC. */
14917
14918 static gdb_byte *
14919 dwarf2_const_value_data (struct attribute *attr, struct type *type,
14920 const char *name, struct obstack *obstack,
14921 struct dwarf2_cu *cu, LONGEST *value, int bits)
14922 {
14923 struct objfile *objfile = cu->objfile;
14924 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
14925 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
14926 LONGEST l = DW_UNSND (attr);
14927
14928 if (bits < sizeof (*value) * 8)
14929 {
14930 l &= ((LONGEST) 1 << bits) - 1;
14931 *value = l;
14932 }
14933 else if (bits == sizeof (*value) * 8)
14934 *value = l;
14935 else
14936 {
14937 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
14938 store_unsigned_integer (bytes, bits / 8, byte_order, l);
14939 return bytes;
14940 }
14941
14942 return NULL;
14943 }
14944
14945 /* Read a constant value from an attribute. Either set *VALUE, or if
14946 the value does not fit in *VALUE, set *BYTES - either already
14947 allocated on the objfile obstack, or newly allocated on OBSTACK,
14948 or, set *BATON, if we translated the constant to a location
14949 expression. */
14950
14951 static void
14952 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
14953 const char *name, struct obstack *obstack,
14954 struct dwarf2_cu *cu,
14955 LONGEST *value, gdb_byte **bytes,
14956 struct dwarf2_locexpr_baton **baton)
14957 {
14958 struct objfile *objfile = cu->objfile;
14959 struct comp_unit_head *cu_header = &cu->header;
14960 struct dwarf_block *blk;
14961 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
14962 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
14963
14964 *value = 0;
14965 *bytes = NULL;
14966 *baton = NULL;
14967
14968 switch (attr->form)
14969 {
14970 case DW_FORM_addr:
14971 case DW_FORM_GNU_addr_index:
14972 {
14973 gdb_byte *data;
14974
14975 if (TYPE_LENGTH (type) != cu_header->addr_size)
14976 dwarf2_const_value_length_mismatch_complaint (name,
14977 cu_header->addr_size,
14978 TYPE_LENGTH (type));
14979 /* Symbols of this form are reasonably rare, so we just
14980 piggyback on the existing location code rather than writing
14981 a new implementation of symbol_computed_ops. */
14982 *baton = obstack_alloc (&objfile->objfile_obstack,
14983 sizeof (struct dwarf2_locexpr_baton));
14984 (*baton)->per_cu = cu->per_cu;
14985 gdb_assert ((*baton)->per_cu);
14986
14987 (*baton)->size = 2 + cu_header->addr_size;
14988 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14989 (*baton)->data = data;
14990
14991 data[0] = DW_OP_addr;
14992 store_unsigned_integer (&data[1], cu_header->addr_size,
14993 byte_order, DW_ADDR (attr));
14994 data[cu_header->addr_size + 1] = DW_OP_stack_value;
14995 }
14996 break;
14997 case DW_FORM_string:
14998 case DW_FORM_strp:
14999 case DW_FORM_GNU_str_index:
15000 /* DW_STRING is already allocated on the objfile obstack, point
15001 directly to it. */
15002 *bytes = (gdb_byte *) DW_STRING (attr);
15003 break;
15004 case DW_FORM_block1:
15005 case DW_FORM_block2:
15006 case DW_FORM_block4:
15007 case DW_FORM_block:
15008 case DW_FORM_exprloc:
15009 blk = DW_BLOCK (attr);
15010 if (TYPE_LENGTH (type) != blk->size)
15011 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15012 TYPE_LENGTH (type));
15013 *bytes = blk->data;
15014 break;
15015
15016 /* The DW_AT_const_value attributes are supposed to carry the
15017 symbol's value "represented as it would be on the target
15018 architecture." By the time we get here, it's already been
15019 converted to host endianness, so we just need to sign- or
15020 zero-extend it as appropriate. */
15021 case DW_FORM_data1:
15022 *bytes = dwarf2_const_value_data (attr, type, name,
15023 obstack, cu, value, 8);
15024 break;
15025 case DW_FORM_data2:
15026 *bytes = dwarf2_const_value_data (attr, type, name,
15027 obstack, cu, value, 16);
15028 break;
15029 case DW_FORM_data4:
15030 *bytes = dwarf2_const_value_data (attr, type, name,
15031 obstack, cu, value, 32);
15032 break;
15033 case DW_FORM_data8:
15034 *bytes = dwarf2_const_value_data (attr, type, name,
15035 obstack, cu, value, 64);
15036 break;
15037
15038 case DW_FORM_sdata:
15039 *value = DW_SND (attr);
15040 break;
15041
15042 case DW_FORM_udata:
15043 *value = DW_UNSND (attr);
15044 break;
15045
15046 default:
15047 complaint (&symfile_complaints,
15048 _("unsupported const value attribute form: '%s'"),
15049 dwarf_form_name (attr->form));
15050 *value = 0;
15051 break;
15052 }
15053 }
15054
15055
15056 /* Copy constant value from an attribute to a symbol. */
15057
15058 static void
15059 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15060 struct dwarf2_cu *cu)
15061 {
15062 struct objfile *objfile = cu->objfile;
15063 struct comp_unit_head *cu_header = &cu->header;
15064 LONGEST value;
15065 gdb_byte *bytes;
15066 struct dwarf2_locexpr_baton *baton;
15067
15068 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15069 SYMBOL_PRINT_NAME (sym),
15070 &objfile->objfile_obstack, cu,
15071 &value, &bytes, &baton);
15072
15073 if (baton != NULL)
15074 {
15075 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15076 SYMBOL_LOCATION_BATON (sym) = baton;
15077 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15078 }
15079 else if (bytes != NULL)
15080 {
15081 SYMBOL_VALUE_BYTES (sym) = bytes;
15082 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15083 }
15084 else
15085 {
15086 SYMBOL_VALUE (sym) = value;
15087 SYMBOL_CLASS (sym) = LOC_CONST;
15088 }
15089 }
15090
15091 /* Return the type of the die in question using its DW_AT_type attribute. */
15092
15093 static struct type *
15094 die_type (struct die_info *die, struct dwarf2_cu *cu)
15095 {
15096 struct attribute *type_attr;
15097
15098 type_attr = dwarf2_attr (die, DW_AT_type, cu);
15099 if (!type_attr)
15100 {
15101 /* A missing DW_AT_type represents a void type. */
15102 return objfile_type (cu->objfile)->builtin_void;
15103 }
15104
15105 return lookup_die_type (die, type_attr, cu);
15106 }
15107
15108 /* True iff CU's producer generates GNAT Ada auxiliary information
15109 that allows to find parallel types through that information instead
15110 of having to do expensive parallel lookups by type name. */
15111
15112 static int
15113 need_gnat_info (struct dwarf2_cu *cu)
15114 {
15115 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15116 of GNAT produces this auxiliary information, without any indication
15117 that it is produced. Part of enhancing the FSF version of GNAT
15118 to produce that information will be to put in place an indicator
15119 that we can use in order to determine whether the descriptive type
15120 info is available or not. One suggestion that has been made is
15121 to use a new attribute, attached to the CU die. For now, assume
15122 that the descriptive type info is not available. */
15123 return 0;
15124 }
15125
15126 /* Return the auxiliary type of the die in question using its
15127 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15128 attribute is not present. */
15129
15130 static struct type *
15131 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15132 {
15133 struct attribute *type_attr;
15134
15135 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15136 if (!type_attr)
15137 return NULL;
15138
15139 return lookup_die_type (die, type_attr, cu);
15140 }
15141
15142 /* If DIE has a descriptive_type attribute, then set the TYPE's
15143 descriptive type accordingly. */
15144
15145 static void
15146 set_descriptive_type (struct type *type, struct die_info *die,
15147 struct dwarf2_cu *cu)
15148 {
15149 struct type *descriptive_type = die_descriptive_type (die, cu);
15150
15151 if (descriptive_type)
15152 {
15153 ALLOCATE_GNAT_AUX_TYPE (type);
15154 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15155 }
15156 }
15157
15158 /* Return the containing type of the die in question using its
15159 DW_AT_containing_type attribute. */
15160
15161 static struct type *
15162 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15163 {
15164 struct attribute *type_attr;
15165
15166 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15167 if (!type_attr)
15168 error (_("Dwarf Error: Problem turning containing type into gdb type "
15169 "[in module %s]"), cu->objfile->name);
15170
15171 return lookup_die_type (die, type_attr, cu);
15172 }
15173
15174 /* Look up the type of DIE in CU using its type attribute ATTR.
15175 If there is no type substitute an error marker. */
15176
15177 static struct type *
15178 lookup_die_type (struct die_info *die, struct attribute *attr,
15179 struct dwarf2_cu *cu)
15180 {
15181 struct objfile *objfile = cu->objfile;
15182 struct type *this_type;
15183
15184 /* First see if we have it cached. */
15185
15186 if (is_ref_attr (attr))
15187 {
15188 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15189
15190 this_type = get_die_type_at_offset (offset, cu->per_cu);
15191 }
15192 else if (attr->form == DW_FORM_ref_sig8)
15193 {
15194 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15195
15196 /* sig_type will be NULL if the signatured type is missing from
15197 the debug info. */
15198 if (sig_type == NULL)
15199 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15200 "at 0x%x [in module %s]"),
15201 die->offset.sect_off, objfile->name);
15202
15203 gdb_assert (sig_type->per_cu.is_debug_types);
15204 /* If we haven't filled in type_offset_in_section yet, then we
15205 haven't read the type in yet. */
15206 this_type = NULL;
15207 if (sig_type->type_offset_in_section.sect_off != 0)
15208 {
15209 this_type =
15210 get_die_type_at_offset (sig_type->type_offset_in_section,
15211 &sig_type->per_cu);
15212 }
15213 }
15214 else
15215 {
15216 dump_die_for_error (die);
15217 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15218 dwarf_attr_name (attr->name), objfile->name);
15219 }
15220
15221 /* If not cached we need to read it in. */
15222
15223 if (this_type == NULL)
15224 {
15225 struct die_info *type_die;
15226 struct dwarf2_cu *type_cu = cu;
15227
15228 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15229 /* If we found the type now, it's probably because the type came
15230 from an inter-CU reference and the type's CU got expanded before
15231 ours. */
15232 this_type = get_die_type (type_die, type_cu);
15233 if (this_type == NULL)
15234 this_type = read_type_die_1 (type_die, type_cu);
15235 }
15236
15237 /* If we still don't have a type use an error marker. */
15238
15239 if (this_type == NULL)
15240 {
15241 char *message, *saved;
15242
15243 /* read_type_die already issued a complaint. */
15244 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15245 objfile->name,
15246 cu->header.offset.sect_off,
15247 die->offset.sect_off);
15248 saved = obstack_copy0 (&objfile->objfile_obstack,
15249 message, strlen (message));
15250 xfree (message);
15251
15252 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15253 }
15254
15255 return this_type;
15256 }
15257
15258 /* Return the type in DIE, CU.
15259 Returns NULL for invalid types.
15260
15261 This first does a lookup in the appropriate type_hash table,
15262 and only reads the die in if necessary.
15263
15264 NOTE: This can be called when reading in partial or full symbols. */
15265
15266 static struct type *
15267 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15268 {
15269 struct type *this_type;
15270
15271 this_type = get_die_type (die, cu);
15272 if (this_type)
15273 return this_type;
15274
15275 return read_type_die_1 (die, cu);
15276 }
15277
15278 /* Read the type in DIE, CU.
15279 Returns NULL for invalid types. */
15280
15281 static struct type *
15282 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15283 {
15284 struct type *this_type = NULL;
15285
15286 switch (die->tag)
15287 {
15288 case DW_TAG_class_type:
15289 case DW_TAG_interface_type:
15290 case DW_TAG_structure_type:
15291 case DW_TAG_union_type:
15292 this_type = read_structure_type (die, cu);
15293 break;
15294 case DW_TAG_enumeration_type:
15295 this_type = read_enumeration_type (die, cu);
15296 break;
15297 case DW_TAG_subprogram:
15298 case DW_TAG_subroutine_type:
15299 case DW_TAG_inlined_subroutine:
15300 this_type = read_subroutine_type (die, cu);
15301 break;
15302 case DW_TAG_array_type:
15303 this_type = read_array_type (die, cu);
15304 break;
15305 case DW_TAG_set_type:
15306 this_type = read_set_type (die, cu);
15307 break;
15308 case DW_TAG_pointer_type:
15309 this_type = read_tag_pointer_type (die, cu);
15310 break;
15311 case DW_TAG_ptr_to_member_type:
15312 this_type = read_tag_ptr_to_member_type (die, cu);
15313 break;
15314 case DW_TAG_reference_type:
15315 this_type = read_tag_reference_type (die, cu);
15316 break;
15317 case DW_TAG_const_type:
15318 this_type = read_tag_const_type (die, cu);
15319 break;
15320 case DW_TAG_volatile_type:
15321 this_type = read_tag_volatile_type (die, cu);
15322 break;
15323 case DW_TAG_string_type:
15324 this_type = read_tag_string_type (die, cu);
15325 break;
15326 case DW_TAG_typedef:
15327 this_type = read_typedef (die, cu);
15328 break;
15329 case DW_TAG_subrange_type:
15330 this_type = read_subrange_type (die, cu);
15331 break;
15332 case DW_TAG_base_type:
15333 this_type = read_base_type (die, cu);
15334 break;
15335 case DW_TAG_unspecified_type:
15336 this_type = read_unspecified_type (die, cu);
15337 break;
15338 case DW_TAG_namespace:
15339 this_type = read_namespace_type (die, cu);
15340 break;
15341 case DW_TAG_module:
15342 this_type = read_module_type (die, cu);
15343 break;
15344 default:
15345 complaint (&symfile_complaints,
15346 _("unexpected tag in read_type_die: '%s'"),
15347 dwarf_tag_name (die->tag));
15348 break;
15349 }
15350
15351 return this_type;
15352 }
15353
15354 /* See if we can figure out if the class lives in a namespace. We do
15355 this by looking for a member function; its demangled name will
15356 contain namespace info, if there is any.
15357 Return the computed name or NULL.
15358 Space for the result is allocated on the objfile's obstack.
15359 This is the full-die version of guess_partial_die_structure_name.
15360 In this case we know DIE has no useful parent. */
15361
15362 static char *
15363 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15364 {
15365 struct die_info *spec_die;
15366 struct dwarf2_cu *spec_cu;
15367 struct die_info *child;
15368
15369 spec_cu = cu;
15370 spec_die = die_specification (die, &spec_cu);
15371 if (spec_die != NULL)
15372 {
15373 die = spec_die;
15374 cu = spec_cu;
15375 }
15376
15377 for (child = die->child;
15378 child != NULL;
15379 child = child->sibling)
15380 {
15381 if (child->tag == DW_TAG_subprogram)
15382 {
15383 struct attribute *attr;
15384
15385 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15386 if (attr == NULL)
15387 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15388 if (attr != NULL)
15389 {
15390 char *actual_name
15391 = language_class_name_from_physname (cu->language_defn,
15392 DW_STRING (attr));
15393 char *name = NULL;
15394
15395 if (actual_name != NULL)
15396 {
15397 char *die_name = dwarf2_name (die, cu);
15398
15399 if (die_name != NULL
15400 && strcmp (die_name, actual_name) != 0)
15401 {
15402 /* Strip off the class name from the full name.
15403 We want the prefix. */
15404 int die_name_len = strlen (die_name);
15405 int actual_name_len = strlen (actual_name);
15406
15407 /* Test for '::' as a sanity check. */
15408 if (actual_name_len > die_name_len + 2
15409 && actual_name[actual_name_len
15410 - die_name_len - 1] == ':')
15411 name =
15412 obsavestring (actual_name,
15413 actual_name_len - die_name_len - 2,
15414 &cu->objfile->objfile_obstack);
15415 }
15416 }
15417 xfree (actual_name);
15418 return name;
15419 }
15420 }
15421 }
15422
15423 return NULL;
15424 }
15425
15426 /* GCC might emit a nameless typedef that has a linkage name. Determine the
15427 prefix part in such case. See
15428 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15429
15430 static char *
15431 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15432 {
15433 struct attribute *attr;
15434 char *base;
15435
15436 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15437 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15438 return NULL;
15439
15440 attr = dwarf2_attr (die, DW_AT_name, cu);
15441 if (attr != NULL && DW_STRING (attr) != NULL)
15442 return NULL;
15443
15444 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15445 if (attr == NULL)
15446 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15447 if (attr == NULL || DW_STRING (attr) == NULL)
15448 return NULL;
15449
15450 /* dwarf2_name had to be already called. */
15451 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15452
15453 /* Strip the base name, keep any leading namespaces/classes. */
15454 base = strrchr (DW_STRING (attr), ':');
15455 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15456 return "";
15457
15458 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15459 &cu->objfile->objfile_obstack);
15460 }
15461
15462 /* Return the name of the namespace/class that DIE is defined within,
15463 or "" if we can't tell. The caller should not xfree the result.
15464
15465 For example, if we're within the method foo() in the following
15466 code:
15467
15468 namespace N {
15469 class C {
15470 void foo () {
15471 }
15472 };
15473 }
15474
15475 then determine_prefix on foo's die will return "N::C". */
15476
15477 static const char *
15478 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15479 {
15480 struct die_info *parent, *spec_die;
15481 struct dwarf2_cu *spec_cu;
15482 struct type *parent_type;
15483 char *retval;
15484
15485 if (cu->language != language_cplus && cu->language != language_java
15486 && cu->language != language_fortran)
15487 return "";
15488
15489 retval = anonymous_struct_prefix (die, cu);
15490 if (retval)
15491 return retval;
15492
15493 /* We have to be careful in the presence of DW_AT_specification.
15494 For example, with GCC 3.4, given the code
15495
15496 namespace N {
15497 void foo() {
15498 // Definition of N::foo.
15499 }
15500 }
15501
15502 then we'll have a tree of DIEs like this:
15503
15504 1: DW_TAG_compile_unit
15505 2: DW_TAG_namespace // N
15506 3: DW_TAG_subprogram // declaration of N::foo
15507 4: DW_TAG_subprogram // definition of N::foo
15508 DW_AT_specification // refers to die #3
15509
15510 Thus, when processing die #4, we have to pretend that we're in
15511 the context of its DW_AT_specification, namely the contex of die
15512 #3. */
15513 spec_cu = cu;
15514 spec_die = die_specification (die, &spec_cu);
15515 if (spec_die == NULL)
15516 parent = die->parent;
15517 else
15518 {
15519 parent = spec_die->parent;
15520 cu = spec_cu;
15521 }
15522
15523 if (parent == NULL)
15524 return "";
15525 else if (parent->building_fullname)
15526 {
15527 const char *name;
15528 const char *parent_name;
15529
15530 /* It has been seen on RealView 2.2 built binaries,
15531 DW_TAG_template_type_param types actually _defined_ as
15532 children of the parent class:
15533
15534 enum E {};
15535 template class <class Enum> Class{};
15536 Class<enum E> class_e;
15537
15538 1: DW_TAG_class_type (Class)
15539 2: DW_TAG_enumeration_type (E)
15540 3: DW_TAG_enumerator (enum1:0)
15541 3: DW_TAG_enumerator (enum2:1)
15542 ...
15543 2: DW_TAG_template_type_param
15544 DW_AT_type DW_FORM_ref_udata (E)
15545
15546 Besides being broken debug info, it can put GDB into an
15547 infinite loop. Consider:
15548
15549 When we're building the full name for Class<E>, we'll start
15550 at Class, and go look over its template type parameters,
15551 finding E. We'll then try to build the full name of E, and
15552 reach here. We're now trying to build the full name of E,
15553 and look over the parent DIE for containing scope. In the
15554 broken case, if we followed the parent DIE of E, we'd again
15555 find Class, and once again go look at its template type
15556 arguments, etc., etc. Simply don't consider such parent die
15557 as source-level parent of this die (it can't be, the language
15558 doesn't allow it), and break the loop here. */
15559 name = dwarf2_name (die, cu);
15560 parent_name = dwarf2_name (parent, cu);
15561 complaint (&symfile_complaints,
15562 _("template param type '%s' defined within parent '%s'"),
15563 name ? name : "<unknown>",
15564 parent_name ? parent_name : "<unknown>");
15565 return "";
15566 }
15567 else
15568 switch (parent->tag)
15569 {
15570 case DW_TAG_namespace:
15571 parent_type = read_type_die (parent, cu);
15572 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15573 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15574 Work around this problem here. */
15575 if (cu->language == language_cplus
15576 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15577 return "";
15578 /* We give a name to even anonymous namespaces. */
15579 return TYPE_TAG_NAME (parent_type);
15580 case DW_TAG_class_type:
15581 case DW_TAG_interface_type:
15582 case DW_TAG_structure_type:
15583 case DW_TAG_union_type:
15584 case DW_TAG_module:
15585 parent_type = read_type_die (parent, cu);
15586 if (TYPE_TAG_NAME (parent_type) != NULL)
15587 return TYPE_TAG_NAME (parent_type);
15588 else
15589 /* An anonymous structure is only allowed non-static data
15590 members; no typedefs, no member functions, et cetera.
15591 So it does not need a prefix. */
15592 return "";
15593 case DW_TAG_compile_unit:
15594 case DW_TAG_partial_unit:
15595 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15596 if (cu->language == language_cplus
15597 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15598 && die->child != NULL
15599 && (die->tag == DW_TAG_class_type
15600 || die->tag == DW_TAG_structure_type
15601 || die->tag == DW_TAG_union_type))
15602 {
15603 char *name = guess_full_die_structure_name (die, cu);
15604 if (name != NULL)
15605 return name;
15606 }
15607 return "";
15608 default:
15609 return determine_prefix (parent, cu);
15610 }
15611 }
15612
15613 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15614 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15615 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15616 an obconcat, otherwise allocate storage for the result. The CU argument is
15617 used to determine the language and hence, the appropriate separator. */
15618
15619 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
15620
15621 static char *
15622 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15623 int physname, struct dwarf2_cu *cu)
15624 {
15625 const char *lead = "";
15626 const char *sep;
15627
15628 if (suffix == NULL || suffix[0] == '\0'
15629 || prefix == NULL || prefix[0] == '\0')
15630 sep = "";
15631 else if (cu->language == language_java)
15632 sep = ".";
15633 else if (cu->language == language_fortran && physname)
15634 {
15635 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15636 DW_AT_MIPS_linkage_name is preferred and used instead. */
15637
15638 lead = "__";
15639 sep = "_MOD_";
15640 }
15641 else
15642 sep = "::";
15643
15644 if (prefix == NULL)
15645 prefix = "";
15646 if (suffix == NULL)
15647 suffix = "";
15648
15649 if (obs == NULL)
15650 {
15651 char *retval
15652 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15653
15654 strcpy (retval, lead);
15655 strcat (retval, prefix);
15656 strcat (retval, sep);
15657 strcat (retval, suffix);
15658 return retval;
15659 }
15660 else
15661 {
15662 /* We have an obstack. */
15663 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15664 }
15665 }
15666
15667 /* Return sibling of die, NULL if no sibling. */
15668
15669 static struct die_info *
15670 sibling_die (struct die_info *die)
15671 {
15672 return die->sibling;
15673 }
15674
15675 /* Get name of a die, return NULL if not found. */
15676
15677 static char *
15678 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15679 struct obstack *obstack)
15680 {
15681 if (name && cu->language == language_cplus)
15682 {
15683 char *canon_name = cp_canonicalize_string (name);
15684
15685 if (canon_name != NULL)
15686 {
15687 if (strcmp (canon_name, name) != 0)
15688 name = obsavestring (canon_name, strlen (canon_name),
15689 obstack);
15690 xfree (canon_name);
15691 }
15692 }
15693
15694 return name;
15695 }
15696
15697 /* Get name of a die, return NULL if not found. */
15698
15699 static char *
15700 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15701 {
15702 struct attribute *attr;
15703
15704 attr = dwarf2_attr (die, DW_AT_name, cu);
15705 if ((!attr || !DW_STRING (attr))
15706 && die->tag != DW_TAG_class_type
15707 && die->tag != DW_TAG_interface_type
15708 && die->tag != DW_TAG_structure_type
15709 && die->tag != DW_TAG_union_type)
15710 return NULL;
15711
15712 switch (die->tag)
15713 {
15714 case DW_TAG_compile_unit:
15715 case DW_TAG_partial_unit:
15716 /* Compilation units have a DW_AT_name that is a filename, not
15717 a source language identifier. */
15718 case DW_TAG_enumeration_type:
15719 case DW_TAG_enumerator:
15720 /* These tags always have simple identifiers already; no need
15721 to canonicalize them. */
15722 return DW_STRING (attr);
15723
15724 case DW_TAG_subprogram:
15725 /* Java constructors will all be named "<init>", so return
15726 the class name when we see this special case. */
15727 if (cu->language == language_java
15728 && DW_STRING (attr) != NULL
15729 && strcmp (DW_STRING (attr), "<init>") == 0)
15730 {
15731 struct dwarf2_cu *spec_cu = cu;
15732 struct die_info *spec_die;
15733
15734 /* GCJ will output '<init>' for Java constructor names.
15735 For this special case, return the name of the parent class. */
15736
15737 /* GCJ may output suprogram DIEs with AT_specification set.
15738 If so, use the name of the specified DIE. */
15739 spec_die = die_specification (die, &spec_cu);
15740 if (spec_die != NULL)
15741 return dwarf2_name (spec_die, spec_cu);
15742
15743 do
15744 {
15745 die = die->parent;
15746 if (die->tag == DW_TAG_class_type)
15747 return dwarf2_name (die, cu);
15748 }
15749 while (die->tag != DW_TAG_compile_unit
15750 && die->tag != DW_TAG_partial_unit);
15751 }
15752 break;
15753
15754 case DW_TAG_class_type:
15755 case DW_TAG_interface_type:
15756 case DW_TAG_structure_type:
15757 case DW_TAG_union_type:
15758 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
15759 structures or unions. These were of the form "._%d" in GCC 4.1,
15760 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
15761 and GCC 4.4. We work around this problem by ignoring these. */
15762 if (attr && DW_STRING (attr)
15763 && (strncmp (DW_STRING (attr), "._", 2) == 0
15764 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
15765 return NULL;
15766
15767 /* GCC might emit a nameless typedef that has a linkage name. See
15768 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15769 if (!attr || DW_STRING (attr) == NULL)
15770 {
15771 char *demangled = NULL;
15772
15773 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15774 if (attr == NULL)
15775 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15776
15777 if (attr == NULL || DW_STRING (attr) == NULL)
15778 return NULL;
15779
15780 /* Avoid demangling DW_STRING (attr) the second time on a second
15781 call for the same DIE. */
15782 if (!DW_STRING_IS_CANONICAL (attr))
15783 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
15784
15785 if (demangled)
15786 {
15787 char *base;
15788
15789 /* FIXME: we already did this for the partial symbol... */
15790 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
15791 &cu->objfile->objfile_obstack);
15792 DW_STRING_IS_CANONICAL (attr) = 1;
15793 xfree (demangled);
15794
15795 /* Strip any leading namespaces/classes, keep only the base name.
15796 DW_AT_name for named DIEs does not contain the prefixes. */
15797 base = strrchr (DW_STRING (attr), ':');
15798 if (base && base > DW_STRING (attr) && base[-1] == ':')
15799 return &base[1];
15800 else
15801 return DW_STRING (attr);
15802 }
15803 }
15804 break;
15805
15806 default:
15807 break;
15808 }
15809
15810 if (!DW_STRING_IS_CANONICAL (attr))
15811 {
15812 DW_STRING (attr)
15813 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
15814 &cu->objfile->objfile_obstack);
15815 DW_STRING_IS_CANONICAL (attr) = 1;
15816 }
15817 return DW_STRING (attr);
15818 }
15819
15820 /* Return the die that this die in an extension of, or NULL if there
15821 is none. *EXT_CU is the CU containing DIE on input, and the CU
15822 containing the return value on output. */
15823
15824 static struct die_info *
15825 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
15826 {
15827 struct attribute *attr;
15828
15829 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
15830 if (attr == NULL)
15831 return NULL;
15832
15833 return follow_die_ref (die, attr, ext_cu);
15834 }
15835
15836 /* Convert a DIE tag into its string name. */
15837
15838 static const char *
15839 dwarf_tag_name (unsigned tag)
15840 {
15841 const char *name = get_DW_TAG_name (tag);
15842
15843 if (name == NULL)
15844 return "DW_TAG_<unknown>";
15845
15846 return name;
15847 }
15848
15849 /* Convert a DWARF attribute code into its string name. */
15850
15851 static const char *
15852 dwarf_attr_name (unsigned attr)
15853 {
15854 const char *name;
15855
15856 #ifdef MIPS /* collides with DW_AT_HP_block_index */
15857 if (attr == DW_AT_MIPS_fde)
15858 return "DW_AT_MIPS_fde";
15859 #else
15860 if (attr == DW_AT_HP_block_index)
15861 return "DW_AT_HP_block_index";
15862 #endif
15863
15864 name = get_DW_AT_name (attr);
15865
15866 if (name == NULL)
15867 return "DW_AT_<unknown>";
15868
15869 return name;
15870 }
15871
15872 /* Convert a DWARF value form code into its string name. */
15873
15874 static const char *
15875 dwarf_form_name (unsigned form)
15876 {
15877 const char *name = get_DW_FORM_name (form);
15878
15879 if (name == NULL)
15880 return "DW_FORM_<unknown>";
15881
15882 return name;
15883 }
15884
15885 static char *
15886 dwarf_bool_name (unsigned mybool)
15887 {
15888 if (mybool)
15889 return "TRUE";
15890 else
15891 return "FALSE";
15892 }
15893
15894 /* Convert a DWARF type code into its string name. */
15895
15896 static const char *
15897 dwarf_type_encoding_name (unsigned enc)
15898 {
15899 const char *name = get_DW_ATE_name (enc);
15900
15901 if (name == NULL)
15902 return "DW_ATE_<unknown>";
15903
15904 return name;
15905 }
15906
15907 static void
15908 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
15909 {
15910 unsigned int i;
15911
15912 print_spaces (indent, f);
15913 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
15914 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
15915
15916 if (die->parent != NULL)
15917 {
15918 print_spaces (indent, f);
15919 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
15920 die->parent->offset.sect_off);
15921 }
15922
15923 print_spaces (indent, f);
15924 fprintf_unfiltered (f, " has children: %s\n",
15925 dwarf_bool_name (die->child != NULL));
15926
15927 print_spaces (indent, f);
15928 fprintf_unfiltered (f, " attributes:\n");
15929
15930 for (i = 0; i < die->num_attrs; ++i)
15931 {
15932 print_spaces (indent, f);
15933 fprintf_unfiltered (f, " %s (%s) ",
15934 dwarf_attr_name (die->attrs[i].name),
15935 dwarf_form_name (die->attrs[i].form));
15936
15937 switch (die->attrs[i].form)
15938 {
15939 case DW_FORM_addr:
15940 case DW_FORM_GNU_addr_index:
15941 fprintf_unfiltered (f, "address: ");
15942 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
15943 break;
15944 case DW_FORM_block2:
15945 case DW_FORM_block4:
15946 case DW_FORM_block:
15947 case DW_FORM_block1:
15948 fprintf_unfiltered (f, "block: size %d",
15949 DW_BLOCK (&die->attrs[i])->size);
15950 break;
15951 case DW_FORM_exprloc:
15952 fprintf_unfiltered (f, "expression: size %u",
15953 DW_BLOCK (&die->attrs[i])->size);
15954 break;
15955 case DW_FORM_ref_addr:
15956 fprintf_unfiltered (f, "ref address: ");
15957 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15958 break;
15959 case DW_FORM_ref1:
15960 case DW_FORM_ref2:
15961 case DW_FORM_ref4:
15962 case DW_FORM_ref8:
15963 case DW_FORM_ref_udata:
15964 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
15965 (long) (DW_UNSND (&die->attrs[i])));
15966 break;
15967 case DW_FORM_data1:
15968 case DW_FORM_data2:
15969 case DW_FORM_data4:
15970 case DW_FORM_data8:
15971 case DW_FORM_udata:
15972 case DW_FORM_sdata:
15973 fprintf_unfiltered (f, "constant: %s",
15974 pulongest (DW_UNSND (&die->attrs[i])));
15975 break;
15976 case DW_FORM_sec_offset:
15977 fprintf_unfiltered (f, "section offset: %s",
15978 pulongest (DW_UNSND (&die->attrs[i])));
15979 break;
15980 case DW_FORM_ref_sig8:
15981 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15982 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15983 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15984 else
15985 fprintf_unfiltered (f, "signatured type, offset: unknown");
15986 break;
15987 case DW_FORM_string:
15988 case DW_FORM_strp:
15989 case DW_FORM_GNU_str_index:
15990 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15991 DW_STRING (&die->attrs[i])
15992 ? DW_STRING (&die->attrs[i]) : "",
15993 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15994 break;
15995 case DW_FORM_flag:
15996 if (DW_UNSND (&die->attrs[i]))
15997 fprintf_unfiltered (f, "flag: TRUE");
15998 else
15999 fprintf_unfiltered (f, "flag: FALSE");
16000 break;
16001 case DW_FORM_flag_present:
16002 fprintf_unfiltered (f, "flag: TRUE");
16003 break;
16004 case DW_FORM_indirect:
16005 /* The reader will have reduced the indirect form to
16006 the "base form" so this form should not occur. */
16007 fprintf_unfiltered (f,
16008 "unexpected attribute form: DW_FORM_indirect");
16009 break;
16010 default:
16011 fprintf_unfiltered (f, "unsupported attribute form: %d.",
16012 die->attrs[i].form);
16013 break;
16014 }
16015 fprintf_unfiltered (f, "\n");
16016 }
16017 }
16018
16019 static void
16020 dump_die_for_error (struct die_info *die)
16021 {
16022 dump_die_shallow (gdb_stderr, 0, die);
16023 }
16024
16025 static void
16026 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16027 {
16028 int indent = level * 4;
16029
16030 gdb_assert (die != NULL);
16031
16032 if (level >= max_level)
16033 return;
16034
16035 dump_die_shallow (f, indent, die);
16036
16037 if (die->child != NULL)
16038 {
16039 print_spaces (indent, f);
16040 fprintf_unfiltered (f, " Children:");
16041 if (level + 1 < max_level)
16042 {
16043 fprintf_unfiltered (f, "\n");
16044 dump_die_1 (f, level + 1, max_level, die->child);
16045 }
16046 else
16047 {
16048 fprintf_unfiltered (f,
16049 " [not printed, max nesting level reached]\n");
16050 }
16051 }
16052
16053 if (die->sibling != NULL && level > 0)
16054 {
16055 dump_die_1 (f, level, max_level, die->sibling);
16056 }
16057 }
16058
16059 /* This is called from the pdie macro in gdbinit.in.
16060 It's not static so gcc will keep a copy callable from gdb. */
16061
16062 void
16063 dump_die (struct die_info *die, int max_level)
16064 {
16065 dump_die_1 (gdb_stdlog, 0, max_level, die);
16066 }
16067
16068 static void
16069 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16070 {
16071 void **slot;
16072
16073 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16074 INSERT);
16075
16076 *slot = die;
16077 }
16078
16079 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16080 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16081
16082 static int
16083 is_ref_attr (struct attribute *attr)
16084 {
16085 switch (attr->form)
16086 {
16087 case DW_FORM_ref_addr:
16088 case DW_FORM_ref1:
16089 case DW_FORM_ref2:
16090 case DW_FORM_ref4:
16091 case DW_FORM_ref8:
16092 case DW_FORM_ref_udata:
16093 return 1;
16094 default:
16095 return 0;
16096 }
16097 }
16098
16099 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16100 required kind. */
16101
16102 static sect_offset
16103 dwarf2_get_ref_die_offset (struct attribute *attr)
16104 {
16105 sect_offset retval = { DW_UNSND (attr) };
16106
16107 if (is_ref_attr (attr))
16108 return retval;
16109
16110 retval.sect_off = 0;
16111 complaint (&symfile_complaints,
16112 _("unsupported die ref attribute form: '%s'"),
16113 dwarf_form_name (attr->form));
16114 return retval;
16115 }
16116
16117 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16118 * the value held by the attribute is not constant. */
16119
16120 static LONGEST
16121 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16122 {
16123 if (attr->form == DW_FORM_sdata)
16124 return DW_SND (attr);
16125 else if (attr->form == DW_FORM_udata
16126 || attr->form == DW_FORM_data1
16127 || attr->form == DW_FORM_data2
16128 || attr->form == DW_FORM_data4
16129 || attr->form == DW_FORM_data8)
16130 return DW_UNSND (attr);
16131 else
16132 {
16133 complaint (&symfile_complaints,
16134 _("Attribute value is not a constant (%s)"),
16135 dwarf_form_name (attr->form));
16136 return default_value;
16137 }
16138 }
16139
16140 /* Follow reference or signature attribute ATTR of SRC_DIE.
16141 On entry *REF_CU is the CU of SRC_DIE.
16142 On exit *REF_CU is the CU of the result. */
16143
16144 static struct die_info *
16145 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16146 struct dwarf2_cu **ref_cu)
16147 {
16148 struct die_info *die;
16149
16150 if (is_ref_attr (attr))
16151 die = follow_die_ref (src_die, attr, ref_cu);
16152 else if (attr->form == DW_FORM_ref_sig8)
16153 die = follow_die_sig (src_die, attr, ref_cu);
16154 else
16155 {
16156 dump_die_for_error (src_die);
16157 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16158 (*ref_cu)->objfile->name);
16159 }
16160
16161 return die;
16162 }
16163
16164 /* Follow reference OFFSET.
16165 On entry *REF_CU is the CU of the source die referencing OFFSET.
16166 On exit *REF_CU is the CU of the result.
16167 Returns NULL if OFFSET is invalid. */
16168
16169 static struct die_info *
16170 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
16171 {
16172 struct die_info temp_die;
16173 struct dwarf2_cu *target_cu, *cu = *ref_cu;
16174
16175 gdb_assert (cu->per_cu != NULL);
16176
16177 target_cu = cu;
16178
16179 if (cu->per_cu->is_debug_types)
16180 {
16181 /* .debug_types CUs cannot reference anything outside their CU.
16182 If they need to, they have to reference a signatured type via
16183 DW_FORM_ref_sig8. */
16184 if (! offset_in_cu_p (&cu->header, offset))
16185 return NULL;
16186 }
16187 else if (! offset_in_cu_p (&cu->header, offset))
16188 {
16189 struct dwarf2_per_cu_data *per_cu;
16190
16191 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
16192
16193 /* If necessary, add it to the queue and load its DIEs. */
16194 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16195 load_full_comp_unit (per_cu, cu->language);
16196
16197 target_cu = per_cu->cu;
16198 }
16199 else if (cu->dies == NULL)
16200 {
16201 /* We're loading full DIEs during partial symbol reading. */
16202 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16203 load_full_comp_unit (cu->per_cu, language_minimal);
16204 }
16205
16206 *ref_cu = target_cu;
16207 temp_die.offset = offset;
16208 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16209 }
16210
16211 /* Follow reference attribute ATTR of SRC_DIE.
16212 On entry *REF_CU is the CU of SRC_DIE.
16213 On exit *REF_CU is the CU of the result. */
16214
16215 static struct die_info *
16216 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16217 struct dwarf2_cu **ref_cu)
16218 {
16219 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16220 struct dwarf2_cu *cu = *ref_cu;
16221 struct die_info *die;
16222
16223 die = follow_die_offset (offset, ref_cu);
16224 if (!die)
16225 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16226 "at 0x%x [in module %s]"),
16227 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16228
16229 return die;
16230 }
16231
16232 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16233 Returned value is intended for DW_OP_call*. Returned
16234 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
16235
16236 struct dwarf2_locexpr_baton
16237 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16238 struct dwarf2_per_cu_data *per_cu,
16239 CORE_ADDR (*get_frame_pc) (void *baton),
16240 void *baton)
16241 {
16242 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16243 struct dwarf2_cu *cu;
16244 struct die_info *die;
16245 struct attribute *attr;
16246 struct dwarf2_locexpr_baton retval;
16247
16248 dw2_setup (per_cu->objfile);
16249
16250 if (per_cu->cu == NULL)
16251 load_cu (per_cu);
16252 cu = per_cu->cu;
16253
16254 die = follow_die_offset (offset, &cu);
16255 if (!die)
16256 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16257 offset.sect_off, per_cu->objfile->name);
16258
16259 attr = dwarf2_attr (die, DW_AT_location, cu);
16260 if (!attr)
16261 {
16262 /* DWARF: "If there is no such attribute, then there is no effect.".
16263 DATA is ignored if SIZE is 0. */
16264
16265 retval.data = NULL;
16266 retval.size = 0;
16267 }
16268 else if (attr_form_is_section_offset (attr))
16269 {
16270 struct dwarf2_loclist_baton loclist_baton;
16271 CORE_ADDR pc = (*get_frame_pc) (baton);
16272 size_t size;
16273
16274 fill_in_loclist_baton (cu, &loclist_baton, attr);
16275
16276 retval.data = dwarf2_find_location_expression (&loclist_baton,
16277 &size, pc);
16278 retval.size = size;
16279 }
16280 else
16281 {
16282 if (!attr_form_is_block (attr))
16283 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16284 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16285 offset.sect_off, per_cu->objfile->name);
16286
16287 retval.data = DW_BLOCK (attr)->data;
16288 retval.size = DW_BLOCK (attr)->size;
16289 }
16290 retval.per_cu = cu->per_cu;
16291
16292 age_cached_comp_units ();
16293
16294 return retval;
16295 }
16296
16297 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16298 PER_CU. */
16299
16300 struct type *
16301 dwarf2_get_die_type (cu_offset die_offset,
16302 struct dwarf2_per_cu_data *per_cu)
16303 {
16304 sect_offset die_offset_sect;
16305
16306 dw2_setup (per_cu->objfile);
16307
16308 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16309 return get_die_type_at_offset (die_offset_sect, per_cu);
16310 }
16311
16312 /* Follow the signature attribute ATTR in SRC_DIE.
16313 On entry *REF_CU is the CU of SRC_DIE.
16314 On exit *REF_CU is the CU of the result. */
16315
16316 static struct die_info *
16317 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16318 struct dwarf2_cu **ref_cu)
16319 {
16320 struct objfile *objfile = (*ref_cu)->objfile;
16321 struct die_info temp_die;
16322 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16323 struct dwarf2_cu *sig_cu;
16324 struct die_info *die;
16325
16326 /* sig_type will be NULL if the signatured type is missing from
16327 the debug info. */
16328 if (sig_type == NULL)
16329 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16330 "at 0x%x [in module %s]"),
16331 src_die->offset.sect_off, objfile->name);
16332
16333 /* If necessary, add it to the queue and load its DIEs. */
16334
16335 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16336 read_signatured_type (sig_type);
16337
16338 gdb_assert (sig_type->per_cu.cu != NULL);
16339
16340 sig_cu = sig_type->per_cu.cu;
16341 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16342 temp_die.offset = sig_type->type_offset_in_section;
16343 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16344 temp_die.offset.sect_off);
16345 if (die)
16346 {
16347 *ref_cu = sig_cu;
16348 return die;
16349 }
16350
16351 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16352 "from DIE at 0x%x [in module %s]"),
16353 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16354 }
16355
16356 /* Given an offset of a signatured type, return its signatured_type. */
16357
16358 static struct signatured_type *
16359 lookup_signatured_type_at_offset (struct objfile *objfile,
16360 struct dwarf2_section_info *section,
16361 sect_offset offset)
16362 {
16363 gdb_byte *info_ptr = section->buffer + offset.sect_off;
16364 unsigned int length, initial_length_size;
16365 unsigned int sig_offset;
16366 struct signatured_type find_entry, *sig_type;
16367
16368 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16369 sig_offset = (initial_length_size
16370 + 2 /*version*/
16371 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16372 + 1 /*address_size*/);
16373 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16374 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16375
16376 /* This is only used to lookup previously recorded types.
16377 If we didn't find it, it's our bug. */
16378 gdb_assert (sig_type != NULL);
16379 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16380
16381 return sig_type;
16382 }
16383
16384 /* Load the DIEs associated with type unit PER_CU into memory. */
16385
16386 static void
16387 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16388 {
16389 struct signatured_type *sig_type;
16390
16391 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16392 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16393
16394 /* We have the per_cu, but we need the signatured_type.
16395 Fortunately this is an easy translation. */
16396 gdb_assert (per_cu->is_debug_types);
16397 sig_type = (struct signatured_type *) per_cu;
16398
16399 gdb_assert (per_cu->cu == NULL);
16400
16401 read_signatured_type (sig_type);
16402
16403 gdb_assert (per_cu->cu != NULL);
16404 }
16405
16406 /* die_reader_func for read_signatured_type.
16407 This is identical to load_full_comp_unit_reader,
16408 but is kept separate for now. */
16409
16410 static void
16411 read_signatured_type_reader (const struct die_reader_specs *reader,
16412 gdb_byte *info_ptr,
16413 struct die_info *comp_unit_die,
16414 int has_children,
16415 void *data)
16416 {
16417 struct dwarf2_cu *cu = reader->cu;
16418
16419 gdb_assert (cu->die_hash == NULL);
16420 cu->die_hash =
16421 htab_create_alloc_ex (cu->header.length / 12,
16422 die_hash,
16423 die_eq,
16424 NULL,
16425 &cu->comp_unit_obstack,
16426 hashtab_obstack_allocate,
16427 dummy_obstack_deallocate);
16428
16429 if (has_children)
16430 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16431 &info_ptr, comp_unit_die);
16432 cu->dies = comp_unit_die;
16433 /* comp_unit_die is not stored in die_hash, no need. */
16434
16435 /* We try not to read any attributes in this function, because not
16436 all CUs needed for references have been loaded yet, and symbol
16437 table processing isn't initialized. But we have to set the CU language,
16438 or we won't be able to build types correctly.
16439 Similarly, if we do not read the producer, we can not apply
16440 producer-specific interpretation. */
16441 prepare_one_comp_unit (cu, cu->dies, language_minimal);
16442 }
16443
16444 /* Read in a signatured type and build its CU and DIEs.
16445 If the type is a stub for the real type in a DWO file,
16446 read in the real type from the DWO file as well. */
16447
16448 static void
16449 read_signatured_type (struct signatured_type *sig_type)
16450 {
16451 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16452
16453 gdb_assert (per_cu->is_debug_types);
16454 gdb_assert (per_cu->cu == NULL);
16455
16456 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16457 read_signatured_type_reader, NULL);
16458 }
16459
16460 /* Decode simple location descriptions.
16461 Given a pointer to a dwarf block that defines a location, compute
16462 the location and return the value.
16463
16464 NOTE drow/2003-11-18: This function is called in two situations
16465 now: for the address of static or global variables (partial symbols
16466 only) and for offsets into structures which are expected to be
16467 (more or less) constant. The partial symbol case should go away,
16468 and only the constant case should remain. That will let this
16469 function complain more accurately. A few special modes are allowed
16470 without complaint for global variables (for instance, global
16471 register values and thread-local values).
16472
16473 A location description containing no operations indicates that the
16474 object is optimized out. The return value is 0 for that case.
16475 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16476 callers will only want a very basic result and this can become a
16477 complaint.
16478
16479 Note that stack[0] is unused except as a default error return. */
16480
16481 static CORE_ADDR
16482 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16483 {
16484 struct objfile *objfile = cu->objfile;
16485 int i;
16486 int size = blk->size;
16487 gdb_byte *data = blk->data;
16488 CORE_ADDR stack[64];
16489 int stacki;
16490 unsigned int bytes_read, unsnd;
16491 gdb_byte op;
16492
16493 i = 0;
16494 stacki = 0;
16495 stack[stacki] = 0;
16496 stack[++stacki] = 0;
16497
16498 while (i < size)
16499 {
16500 op = data[i++];
16501 switch (op)
16502 {
16503 case DW_OP_lit0:
16504 case DW_OP_lit1:
16505 case DW_OP_lit2:
16506 case DW_OP_lit3:
16507 case DW_OP_lit4:
16508 case DW_OP_lit5:
16509 case DW_OP_lit6:
16510 case DW_OP_lit7:
16511 case DW_OP_lit8:
16512 case DW_OP_lit9:
16513 case DW_OP_lit10:
16514 case DW_OP_lit11:
16515 case DW_OP_lit12:
16516 case DW_OP_lit13:
16517 case DW_OP_lit14:
16518 case DW_OP_lit15:
16519 case DW_OP_lit16:
16520 case DW_OP_lit17:
16521 case DW_OP_lit18:
16522 case DW_OP_lit19:
16523 case DW_OP_lit20:
16524 case DW_OP_lit21:
16525 case DW_OP_lit22:
16526 case DW_OP_lit23:
16527 case DW_OP_lit24:
16528 case DW_OP_lit25:
16529 case DW_OP_lit26:
16530 case DW_OP_lit27:
16531 case DW_OP_lit28:
16532 case DW_OP_lit29:
16533 case DW_OP_lit30:
16534 case DW_OP_lit31:
16535 stack[++stacki] = op - DW_OP_lit0;
16536 break;
16537
16538 case DW_OP_reg0:
16539 case DW_OP_reg1:
16540 case DW_OP_reg2:
16541 case DW_OP_reg3:
16542 case DW_OP_reg4:
16543 case DW_OP_reg5:
16544 case DW_OP_reg6:
16545 case DW_OP_reg7:
16546 case DW_OP_reg8:
16547 case DW_OP_reg9:
16548 case DW_OP_reg10:
16549 case DW_OP_reg11:
16550 case DW_OP_reg12:
16551 case DW_OP_reg13:
16552 case DW_OP_reg14:
16553 case DW_OP_reg15:
16554 case DW_OP_reg16:
16555 case DW_OP_reg17:
16556 case DW_OP_reg18:
16557 case DW_OP_reg19:
16558 case DW_OP_reg20:
16559 case DW_OP_reg21:
16560 case DW_OP_reg22:
16561 case DW_OP_reg23:
16562 case DW_OP_reg24:
16563 case DW_OP_reg25:
16564 case DW_OP_reg26:
16565 case DW_OP_reg27:
16566 case DW_OP_reg28:
16567 case DW_OP_reg29:
16568 case DW_OP_reg30:
16569 case DW_OP_reg31:
16570 stack[++stacki] = op - DW_OP_reg0;
16571 if (i < size)
16572 dwarf2_complex_location_expr_complaint ();
16573 break;
16574
16575 case DW_OP_regx:
16576 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16577 i += bytes_read;
16578 stack[++stacki] = unsnd;
16579 if (i < size)
16580 dwarf2_complex_location_expr_complaint ();
16581 break;
16582
16583 case DW_OP_addr:
16584 stack[++stacki] = read_address (objfile->obfd, &data[i],
16585 cu, &bytes_read);
16586 i += bytes_read;
16587 break;
16588
16589 case DW_OP_const1u:
16590 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16591 i += 1;
16592 break;
16593
16594 case DW_OP_const1s:
16595 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16596 i += 1;
16597 break;
16598
16599 case DW_OP_const2u:
16600 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16601 i += 2;
16602 break;
16603
16604 case DW_OP_const2s:
16605 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16606 i += 2;
16607 break;
16608
16609 case DW_OP_const4u:
16610 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16611 i += 4;
16612 break;
16613
16614 case DW_OP_const4s:
16615 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16616 i += 4;
16617 break;
16618
16619 case DW_OP_const8u:
16620 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16621 i += 8;
16622 break;
16623
16624 case DW_OP_constu:
16625 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16626 &bytes_read);
16627 i += bytes_read;
16628 break;
16629
16630 case DW_OP_consts:
16631 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16632 i += bytes_read;
16633 break;
16634
16635 case DW_OP_dup:
16636 stack[stacki + 1] = stack[stacki];
16637 stacki++;
16638 break;
16639
16640 case DW_OP_plus:
16641 stack[stacki - 1] += stack[stacki];
16642 stacki--;
16643 break;
16644
16645 case DW_OP_plus_uconst:
16646 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16647 &bytes_read);
16648 i += bytes_read;
16649 break;
16650
16651 case DW_OP_minus:
16652 stack[stacki - 1] -= stack[stacki];
16653 stacki--;
16654 break;
16655
16656 case DW_OP_deref:
16657 /* If we're not the last op, then we definitely can't encode
16658 this using GDB's address_class enum. This is valid for partial
16659 global symbols, although the variable's address will be bogus
16660 in the psymtab. */
16661 if (i < size)
16662 dwarf2_complex_location_expr_complaint ();
16663 break;
16664
16665 case DW_OP_GNU_push_tls_address:
16666 /* The top of the stack has the offset from the beginning
16667 of the thread control block at which the variable is located. */
16668 /* Nothing should follow this operator, so the top of stack would
16669 be returned. */
16670 /* This is valid for partial global symbols, but the variable's
16671 address will be bogus in the psymtab. Make it always at least
16672 non-zero to not look as a variable garbage collected by linker
16673 which have DW_OP_addr 0. */
16674 if (i < size)
16675 dwarf2_complex_location_expr_complaint ();
16676 stack[stacki]++;
16677 break;
16678
16679 case DW_OP_GNU_uninit:
16680 break;
16681
16682 case DW_OP_GNU_addr_index:
16683 case DW_OP_GNU_const_index:
16684 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16685 &bytes_read);
16686 i += bytes_read;
16687 break;
16688
16689 default:
16690 {
16691 const char *name = get_DW_OP_name (op);
16692
16693 if (name)
16694 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16695 name);
16696 else
16697 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16698 op);
16699 }
16700
16701 return (stack[stacki]);
16702 }
16703
16704 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16705 outside of the allocated space. Also enforce minimum>0. */
16706 if (stacki >= ARRAY_SIZE (stack) - 1)
16707 {
16708 complaint (&symfile_complaints,
16709 _("location description stack overflow"));
16710 return 0;
16711 }
16712
16713 if (stacki <= 0)
16714 {
16715 complaint (&symfile_complaints,
16716 _("location description stack underflow"));
16717 return 0;
16718 }
16719 }
16720 return (stack[stacki]);
16721 }
16722
16723 /* memory allocation interface */
16724
16725 static struct dwarf_block *
16726 dwarf_alloc_block (struct dwarf2_cu *cu)
16727 {
16728 struct dwarf_block *blk;
16729
16730 blk = (struct dwarf_block *)
16731 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16732 return (blk);
16733 }
16734
16735 static struct die_info *
16736 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
16737 {
16738 struct die_info *die;
16739 size_t size = sizeof (struct die_info);
16740
16741 if (num_attrs > 1)
16742 size += (num_attrs - 1) * sizeof (struct attribute);
16743
16744 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
16745 memset (die, 0, sizeof (struct die_info));
16746 return (die);
16747 }
16748
16749 \f
16750 /* Macro support. */
16751
16752 /* Return the full name of file number I in *LH's file name table.
16753 Use COMP_DIR as the name of the current directory of the
16754 compilation. The result is allocated using xmalloc; the caller is
16755 responsible for freeing it. */
16756 static char *
16757 file_full_name (int file, struct line_header *lh, const char *comp_dir)
16758 {
16759 /* Is the file number a valid index into the line header's file name
16760 table? Remember that file numbers start with one, not zero. */
16761 if (1 <= file && file <= lh->num_file_names)
16762 {
16763 struct file_entry *fe = &lh->file_names[file - 1];
16764
16765 if (IS_ABSOLUTE_PATH (fe->name))
16766 return xstrdup (fe->name);
16767 else
16768 {
16769 const char *dir;
16770 int dir_len;
16771 char *full_name;
16772
16773 if (fe->dir_index)
16774 dir = lh->include_dirs[fe->dir_index - 1];
16775 else
16776 dir = comp_dir;
16777
16778 if (dir)
16779 {
16780 dir_len = strlen (dir);
16781 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
16782 strcpy (full_name, dir);
16783 full_name[dir_len] = '/';
16784 strcpy (full_name + dir_len + 1, fe->name);
16785 return full_name;
16786 }
16787 else
16788 return xstrdup (fe->name);
16789 }
16790 }
16791 else
16792 {
16793 /* The compiler produced a bogus file number. We can at least
16794 record the macro definitions made in the file, even if we
16795 won't be able to find the file by name. */
16796 char fake_name[80];
16797
16798 sprintf (fake_name, "<bad macro file number %d>", file);
16799
16800 complaint (&symfile_complaints,
16801 _("bad file number in macro information (%d)"),
16802 file);
16803
16804 return xstrdup (fake_name);
16805 }
16806 }
16807
16808
16809 static struct macro_source_file *
16810 macro_start_file (int file, int line,
16811 struct macro_source_file *current_file,
16812 const char *comp_dir,
16813 struct line_header *lh, struct objfile *objfile)
16814 {
16815 /* The full name of this source file. */
16816 char *full_name = file_full_name (file, lh, comp_dir);
16817
16818 /* We don't create a macro table for this compilation unit
16819 at all until we actually get a filename. */
16820 if (! pending_macros)
16821 pending_macros = new_macro_table (&objfile->objfile_obstack,
16822 objfile->macro_cache);
16823
16824 if (! current_file)
16825 {
16826 /* If we have no current file, then this must be the start_file
16827 directive for the compilation unit's main source file. */
16828 current_file = macro_set_main (pending_macros, full_name);
16829 macro_define_special (pending_macros);
16830 }
16831 else
16832 current_file = macro_include (current_file, line, full_name);
16833
16834 xfree (full_name);
16835
16836 return current_file;
16837 }
16838
16839
16840 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
16841 followed by a null byte. */
16842 static char *
16843 copy_string (const char *buf, int len)
16844 {
16845 char *s = xmalloc (len + 1);
16846
16847 memcpy (s, buf, len);
16848 s[len] = '\0';
16849 return s;
16850 }
16851
16852
16853 static const char *
16854 consume_improper_spaces (const char *p, const char *body)
16855 {
16856 if (*p == ' ')
16857 {
16858 complaint (&symfile_complaints,
16859 _("macro definition contains spaces "
16860 "in formal argument list:\n`%s'"),
16861 body);
16862
16863 while (*p == ' ')
16864 p++;
16865 }
16866
16867 return p;
16868 }
16869
16870
16871 static void
16872 parse_macro_definition (struct macro_source_file *file, int line,
16873 const char *body)
16874 {
16875 const char *p;
16876
16877 /* The body string takes one of two forms. For object-like macro
16878 definitions, it should be:
16879
16880 <macro name> " " <definition>
16881
16882 For function-like macro definitions, it should be:
16883
16884 <macro name> "() " <definition>
16885 or
16886 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16887
16888 Spaces may appear only where explicitly indicated, and in the
16889 <definition>.
16890
16891 The Dwarf 2 spec says that an object-like macro's name is always
16892 followed by a space, but versions of GCC around March 2002 omit
16893 the space when the macro's definition is the empty string.
16894
16895 The Dwarf 2 spec says that there should be no spaces between the
16896 formal arguments in a function-like macro's formal argument list,
16897 but versions of GCC around March 2002 include spaces after the
16898 commas. */
16899
16900
16901 /* Find the extent of the macro name. The macro name is terminated
16902 by either a space or null character (for an object-like macro) or
16903 an opening paren (for a function-like macro). */
16904 for (p = body; *p; p++)
16905 if (*p == ' ' || *p == '(')
16906 break;
16907
16908 if (*p == ' ' || *p == '\0')
16909 {
16910 /* It's an object-like macro. */
16911 int name_len = p - body;
16912 char *name = copy_string (body, name_len);
16913 const char *replacement;
16914
16915 if (*p == ' ')
16916 replacement = body + name_len + 1;
16917 else
16918 {
16919 dwarf2_macro_malformed_definition_complaint (body);
16920 replacement = body + name_len;
16921 }
16922
16923 macro_define_object (file, line, name, replacement);
16924
16925 xfree (name);
16926 }
16927 else if (*p == '(')
16928 {
16929 /* It's a function-like macro. */
16930 char *name = copy_string (body, p - body);
16931 int argc = 0;
16932 int argv_size = 1;
16933 char **argv = xmalloc (argv_size * sizeof (*argv));
16934
16935 p++;
16936
16937 p = consume_improper_spaces (p, body);
16938
16939 /* Parse the formal argument list. */
16940 while (*p && *p != ')')
16941 {
16942 /* Find the extent of the current argument name. */
16943 const char *arg_start = p;
16944
16945 while (*p && *p != ',' && *p != ')' && *p != ' ')
16946 p++;
16947
16948 if (! *p || p == arg_start)
16949 dwarf2_macro_malformed_definition_complaint (body);
16950 else
16951 {
16952 /* Make sure argv has room for the new argument. */
16953 if (argc >= argv_size)
16954 {
16955 argv_size *= 2;
16956 argv = xrealloc (argv, argv_size * sizeof (*argv));
16957 }
16958
16959 argv[argc++] = copy_string (arg_start, p - arg_start);
16960 }
16961
16962 p = consume_improper_spaces (p, body);
16963
16964 /* Consume the comma, if present. */
16965 if (*p == ',')
16966 {
16967 p++;
16968
16969 p = consume_improper_spaces (p, body);
16970 }
16971 }
16972
16973 if (*p == ')')
16974 {
16975 p++;
16976
16977 if (*p == ' ')
16978 /* Perfectly formed definition, no complaints. */
16979 macro_define_function (file, line, name,
16980 argc, (const char **) argv,
16981 p + 1);
16982 else if (*p == '\0')
16983 {
16984 /* Complain, but do define it. */
16985 dwarf2_macro_malformed_definition_complaint (body);
16986 macro_define_function (file, line, name,
16987 argc, (const char **) argv,
16988 p);
16989 }
16990 else
16991 /* Just complain. */
16992 dwarf2_macro_malformed_definition_complaint (body);
16993 }
16994 else
16995 /* Just complain. */
16996 dwarf2_macro_malformed_definition_complaint (body);
16997
16998 xfree (name);
16999 {
17000 int i;
17001
17002 for (i = 0; i < argc; i++)
17003 xfree (argv[i]);
17004 }
17005 xfree (argv);
17006 }
17007 else
17008 dwarf2_macro_malformed_definition_complaint (body);
17009 }
17010
17011 /* Skip some bytes from BYTES according to the form given in FORM.
17012 Returns the new pointer. */
17013
17014 static gdb_byte *
17015 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17016 enum dwarf_form form,
17017 unsigned int offset_size,
17018 struct dwarf2_section_info *section)
17019 {
17020 unsigned int bytes_read;
17021
17022 switch (form)
17023 {
17024 case DW_FORM_data1:
17025 case DW_FORM_flag:
17026 ++bytes;
17027 break;
17028
17029 case DW_FORM_data2:
17030 bytes += 2;
17031 break;
17032
17033 case DW_FORM_data4:
17034 bytes += 4;
17035 break;
17036
17037 case DW_FORM_data8:
17038 bytes += 8;
17039 break;
17040
17041 case DW_FORM_string:
17042 read_direct_string (abfd, bytes, &bytes_read);
17043 bytes += bytes_read;
17044 break;
17045
17046 case DW_FORM_sec_offset:
17047 case DW_FORM_strp:
17048 bytes += offset_size;
17049 break;
17050
17051 case DW_FORM_block:
17052 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17053 bytes += bytes_read;
17054 break;
17055
17056 case DW_FORM_block1:
17057 bytes += 1 + read_1_byte (abfd, bytes);
17058 break;
17059 case DW_FORM_block2:
17060 bytes += 2 + read_2_bytes (abfd, bytes);
17061 break;
17062 case DW_FORM_block4:
17063 bytes += 4 + read_4_bytes (abfd, bytes);
17064 break;
17065
17066 case DW_FORM_sdata:
17067 case DW_FORM_udata:
17068 case DW_FORM_GNU_addr_index:
17069 case DW_FORM_GNU_str_index:
17070 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17071 if (bytes == NULL)
17072 {
17073 dwarf2_section_buffer_overflow_complaint (section);
17074 return NULL;
17075 }
17076 break;
17077
17078 default:
17079 {
17080 complain:
17081 complaint (&symfile_complaints,
17082 _("invalid form 0x%x in `%s'"),
17083 form,
17084 section->asection->name);
17085 return NULL;
17086 }
17087 }
17088
17089 return bytes;
17090 }
17091
17092 /* A helper for dwarf_decode_macros that handles skipping an unknown
17093 opcode. Returns an updated pointer to the macro data buffer; or,
17094 on error, issues a complaint and returns NULL. */
17095
17096 static gdb_byte *
17097 skip_unknown_opcode (unsigned int opcode,
17098 gdb_byte **opcode_definitions,
17099 gdb_byte *mac_ptr, gdb_byte *mac_end,
17100 bfd *abfd,
17101 unsigned int offset_size,
17102 struct dwarf2_section_info *section)
17103 {
17104 unsigned int bytes_read, i;
17105 unsigned long arg;
17106 gdb_byte *defn;
17107
17108 if (opcode_definitions[opcode] == NULL)
17109 {
17110 complaint (&symfile_complaints,
17111 _("unrecognized DW_MACFINO opcode 0x%x"),
17112 opcode);
17113 return NULL;
17114 }
17115
17116 defn = opcode_definitions[opcode];
17117 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17118 defn += bytes_read;
17119
17120 for (i = 0; i < arg; ++i)
17121 {
17122 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17123 section);
17124 if (mac_ptr == NULL)
17125 {
17126 /* skip_form_bytes already issued the complaint. */
17127 return NULL;
17128 }
17129 }
17130
17131 return mac_ptr;
17132 }
17133
17134 /* A helper function which parses the header of a macro section.
17135 If the macro section is the extended (for now called "GNU") type,
17136 then this updates *OFFSET_SIZE. Returns a pointer to just after
17137 the header, or issues a complaint and returns NULL on error. */
17138
17139 static gdb_byte *
17140 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17141 bfd *abfd,
17142 gdb_byte *mac_ptr,
17143 unsigned int *offset_size,
17144 int section_is_gnu)
17145 {
17146 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17147
17148 if (section_is_gnu)
17149 {
17150 unsigned int version, flags;
17151
17152 version = read_2_bytes (abfd, mac_ptr);
17153 if (version != 4)
17154 {
17155 complaint (&symfile_complaints,
17156 _("unrecognized version `%d' in .debug_macro section"),
17157 version);
17158 return NULL;
17159 }
17160 mac_ptr += 2;
17161
17162 flags = read_1_byte (abfd, mac_ptr);
17163 ++mac_ptr;
17164 *offset_size = (flags & 1) ? 8 : 4;
17165
17166 if ((flags & 2) != 0)
17167 /* We don't need the line table offset. */
17168 mac_ptr += *offset_size;
17169
17170 /* Vendor opcode descriptions. */
17171 if ((flags & 4) != 0)
17172 {
17173 unsigned int i, count;
17174
17175 count = read_1_byte (abfd, mac_ptr);
17176 ++mac_ptr;
17177 for (i = 0; i < count; ++i)
17178 {
17179 unsigned int opcode, bytes_read;
17180 unsigned long arg;
17181
17182 opcode = read_1_byte (abfd, mac_ptr);
17183 ++mac_ptr;
17184 opcode_definitions[opcode] = mac_ptr;
17185 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17186 mac_ptr += bytes_read;
17187 mac_ptr += arg;
17188 }
17189 }
17190 }
17191
17192 return mac_ptr;
17193 }
17194
17195 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17196 including DW_MACRO_GNU_transparent_include. */
17197
17198 static void
17199 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17200 struct macro_source_file *current_file,
17201 struct line_header *lh, char *comp_dir,
17202 struct dwarf2_section_info *section,
17203 int section_is_gnu,
17204 unsigned int offset_size,
17205 struct objfile *objfile,
17206 htab_t include_hash)
17207 {
17208 enum dwarf_macro_record_type macinfo_type;
17209 int at_commandline;
17210 gdb_byte *opcode_definitions[256];
17211
17212 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17213 &offset_size, section_is_gnu);
17214 if (mac_ptr == NULL)
17215 {
17216 /* We already issued a complaint. */
17217 return;
17218 }
17219
17220 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17221 GDB is still reading the definitions from command line. First
17222 DW_MACINFO_start_file will need to be ignored as it was already executed
17223 to create CURRENT_FILE for the main source holding also the command line
17224 definitions. On first met DW_MACINFO_start_file this flag is reset to
17225 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17226
17227 at_commandline = 1;
17228
17229 do
17230 {
17231 /* Do we at least have room for a macinfo type byte? */
17232 if (mac_ptr >= mac_end)
17233 {
17234 dwarf2_section_buffer_overflow_complaint (section);
17235 break;
17236 }
17237
17238 macinfo_type = read_1_byte (abfd, mac_ptr);
17239 mac_ptr++;
17240
17241 /* Note that we rely on the fact that the corresponding GNU and
17242 DWARF constants are the same. */
17243 switch (macinfo_type)
17244 {
17245 /* A zero macinfo type indicates the end of the macro
17246 information. */
17247 case 0:
17248 break;
17249
17250 case DW_MACRO_GNU_define:
17251 case DW_MACRO_GNU_undef:
17252 case DW_MACRO_GNU_define_indirect:
17253 case DW_MACRO_GNU_undef_indirect:
17254 {
17255 unsigned int bytes_read;
17256 int line;
17257 char *body;
17258 int is_define;
17259
17260 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17261 mac_ptr += bytes_read;
17262
17263 if (macinfo_type == DW_MACRO_GNU_define
17264 || macinfo_type == DW_MACRO_GNU_undef)
17265 {
17266 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17267 mac_ptr += bytes_read;
17268 }
17269 else
17270 {
17271 LONGEST str_offset;
17272
17273 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17274 mac_ptr += offset_size;
17275
17276 body = read_indirect_string_at_offset (abfd, str_offset);
17277 }
17278
17279 is_define = (macinfo_type == DW_MACRO_GNU_define
17280 || macinfo_type == DW_MACRO_GNU_define_indirect);
17281 if (! current_file)
17282 {
17283 /* DWARF violation as no main source is present. */
17284 complaint (&symfile_complaints,
17285 _("debug info with no main source gives macro %s "
17286 "on line %d: %s"),
17287 is_define ? _("definition") : _("undefinition"),
17288 line, body);
17289 break;
17290 }
17291 if ((line == 0 && !at_commandline)
17292 || (line != 0 && at_commandline))
17293 complaint (&symfile_complaints,
17294 _("debug info gives %s macro %s with %s line %d: %s"),
17295 at_commandline ? _("command-line") : _("in-file"),
17296 is_define ? _("definition") : _("undefinition"),
17297 line == 0 ? _("zero") : _("non-zero"), line, body);
17298
17299 if (is_define)
17300 parse_macro_definition (current_file, line, body);
17301 else
17302 {
17303 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17304 || macinfo_type == DW_MACRO_GNU_undef_indirect);
17305 macro_undef (current_file, line, body);
17306 }
17307 }
17308 break;
17309
17310 case DW_MACRO_GNU_start_file:
17311 {
17312 unsigned int bytes_read;
17313 int line, file;
17314
17315 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17316 mac_ptr += bytes_read;
17317 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17318 mac_ptr += bytes_read;
17319
17320 if ((line == 0 && !at_commandline)
17321 || (line != 0 && at_commandline))
17322 complaint (&symfile_complaints,
17323 _("debug info gives source %d included "
17324 "from %s at %s line %d"),
17325 file, at_commandline ? _("command-line") : _("file"),
17326 line == 0 ? _("zero") : _("non-zero"), line);
17327
17328 if (at_commandline)
17329 {
17330 /* This DW_MACRO_GNU_start_file was executed in the
17331 pass one. */
17332 at_commandline = 0;
17333 }
17334 else
17335 current_file = macro_start_file (file, line,
17336 current_file, comp_dir,
17337 lh, objfile);
17338 }
17339 break;
17340
17341 case DW_MACRO_GNU_end_file:
17342 if (! current_file)
17343 complaint (&symfile_complaints,
17344 _("macro debug info has an unmatched "
17345 "`close_file' directive"));
17346 else
17347 {
17348 current_file = current_file->included_by;
17349 if (! current_file)
17350 {
17351 enum dwarf_macro_record_type next_type;
17352
17353 /* GCC circa March 2002 doesn't produce the zero
17354 type byte marking the end of the compilation
17355 unit. Complain if it's not there, but exit no
17356 matter what. */
17357
17358 /* Do we at least have room for a macinfo type byte? */
17359 if (mac_ptr >= mac_end)
17360 {
17361 dwarf2_section_buffer_overflow_complaint (section);
17362 return;
17363 }
17364
17365 /* We don't increment mac_ptr here, so this is just
17366 a look-ahead. */
17367 next_type = read_1_byte (abfd, mac_ptr);
17368 if (next_type != 0)
17369 complaint (&symfile_complaints,
17370 _("no terminating 0-type entry for "
17371 "macros in `.debug_macinfo' section"));
17372
17373 return;
17374 }
17375 }
17376 break;
17377
17378 case DW_MACRO_GNU_transparent_include:
17379 {
17380 LONGEST offset;
17381 void **slot;
17382
17383 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17384 mac_ptr += offset_size;
17385
17386 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17387 if (*slot != NULL)
17388 {
17389 /* This has actually happened; see
17390 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17391 complaint (&symfile_complaints,
17392 _("recursive DW_MACRO_GNU_transparent_include in "
17393 ".debug_macro section"));
17394 }
17395 else
17396 {
17397 *slot = mac_ptr;
17398
17399 dwarf_decode_macro_bytes (abfd,
17400 section->buffer + offset,
17401 mac_end, current_file,
17402 lh, comp_dir,
17403 section, section_is_gnu,
17404 offset_size, objfile, include_hash);
17405
17406 htab_remove_elt (include_hash, mac_ptr);
17407 }
17408 }
17409 break;
17410
17411 case DW_MACINFO_vendor_ext:
17412 if (!section_is_gnu)
17413 {
17414 unsigned int bytes_read;
17415 int constant;
17416
17417 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17418 mac_ptr += bytes_read;
17419 read_direct_string (abfd, mac_ptr, &bytes_read);
17420 mac_ptr += bytes_read;
17421
17422 /* We don't recognize any vendor extensions. */
17423 break;
17424 }
17425 /* FALLTHROUGH */
17426
17427 default:
17428 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17429 mac_ptr, mac_end, abfd, offset_size,
17430 section);
17431 if (mac_ptr == NULL)
17432 return;
17433 break;
17434 }
17435 } while (macinfo_type != 0);
17436 }
17437
17438 static void
17439 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17440 char *comp_dir, int section_is_gnu)
17441 {
17442 struct objfile *objfile = dwarf2_per_objfile->objfile;
17443 struct line_header *lh = cu->line_header;
17444 bfd *abfd;
17445 gdb_byte *mac_ptr, *mac_end;
17446 struct macro_source_file *current_file = 0;
17447 enum dwarf_macro_record_type macinfo_type;
17448 unsigned int offset_size = cu->header.offset_size;
17449 gdb_byte *opcode_definitions[256];
17450 struct cleanup *cleanup;
17451 htab_t include_hash;
17452 void **slot;
17453 struct dwarf2_section_info *section;
17454 const char *section_name;
17455
17456 if (cu->dwo_unit != NULL)
17457 {
17458 if (section_is_gnu)
17459 {
17460 section = &cu->dwo_unit->dwo_file->sections.macro;
17461 section_name = ".debug_macro.dwo";
17462 }
17463 else
17464 {
17465 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17466 section_name = ".debug_macinfo.dwo";
17467 }
17468 }
17469 else
17470 {
17471 if (section_is_gnu)
17472 {
17473 section = &dwarf2_per_objfile->macro;
17474 section_name = ".debug_macro";
17475 }
17476 else
17477 {
17478 section = &dwarf2_per_objfile->macinfo;
17479 section_name = ".debug_macinfo";
17480 }
17481 }
17482
17483 dwarf2_read_section (objfile, section);
17484 if (section->buffer == NULL)
17485 {
17486 complaint (&symfile_complaints, _("missing %s section"), section_name);
17487 return;
17488 }
17489 abfd = section->asection->owner;
17490
17491 /* First pass: Find the name of the base filename.
17492 This filename is needed in order to process all macros whose definition
17493 (or undefinition) comes from the command line. These macros are defined
17494 before the first DW_MACINFO_start_file entry, and yet still need to be
17495 associated to the base file.
17496
17497 To determine the base file name, we scan the macro definitions until we
17498 reach the first DW_MACINFO_start_file entry. We then initialize
17499 CURRENT_FILE accordingly so that any macro definition found before the
17500 first DW_MACINFO_start_file can still be associated to the base file. */
17501
17502 mac_ptr = section->buffer + offset;
17503 mac_end = section->buffer + section->size;
17504
17505 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17506 &offset_size, section_is_gnu);
17507 if (mac_ptr == NULL)
17508 {
17509 /* We already issued a complaint. */
17510 return;
17511 }
17512
17513 do
17514 {
17515 /* Do we at least have room for a macinfo type byte? */
17516 if (mac_ptr >= mac_end)
17517 {
17518 /* Complaint is printed during the second pass as GDB will probably
17519 stop the first pass earlier upon finding
17520 DW_MACINFO_start_file. */
17521 break;
17522 }
17523
17524 macinfo_type = read_1_byte (abfd, mac_ptr);
17525 mac_ptr++;
17526
17527 /* Note that we rely on the fact that the corresponding GNU and
17528 DWARF constants are the same. */
17529 switch (macinfo_type)
17530 {
17531 /* A zero macinfo type indicates the end of the macro
17532 information. */
17533 case 0:
17534 break;
17535
17536 case DW_MACRO_GNU_define:
17537 case DW_MACRO_GNU_undef:
17538 /* Only skip the data by MAC_PTR. */
17539 {
17540 unsigned int bytes_read;
17541
17542 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17543 mac_ptr += bytes_read;
17544 read_direct_string (abfd, mac_ptr, &bytes_read);
17545 mac_ptr += bytes_read;
17546 }
17547 break;
17548
17549 case DW_MACRO_GNU_start_file:
17550 {
17551 unsigned int bytes_read;
17552 int line, file;
17553
17554 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17555 mac_ptr += bytes_read;
17556 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17557 mac_ptr += bytes_read;
17558
17559 current_file = macro_start_file (file, line, current_file,
17560 comp_dir, lh, objfile);
17561 }
17562 break;
17563
17564 case DW_MACRO_GNU_end_file:
17565 /* No data to skip by MAC_PTR. */
17566 break;
17567
17568 case DW_MACRO_GNU_define_indirect:
17569 case DW_MACRO_GNU_undef_indirect:
17570 {
17571 unsigned int bytes_read;
17572
17573 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17574 mac_ptr += bytes_read;
17575 mac_ptr += offset_size;
17576 }
17577 break;
17578
17579 case DW_MACRO_GNU_transparent_include:
17580 /* Note that, according to the spec, a transparent include
17581 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17582 skip this opcode. */
17583 mac_ptr += offset_size;
17584 break;
17585
17586 case DW_MACINFO_vendor_ext:
17587 /* Only skip the data by MAC_PTR. */
17588 if (!section_is_gnu)
17589 {
17590 unsigned int bytes_read;
17591
17592 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17593 mac_ptr += bytes_read;
17594 read_direct_string (abfd, mac_ptr, &bytes_read);
17595 mac_ptr += bytes_read;
17596 }
17597 /* FALLTHROUGH */
17598
17599 default:
17600 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17601 mac_ptr, mac_end, abfd, offset_size,
17602 section);
17603 if (mac_ptr == NULL)
17604 return;
17605 break;
17606 }
17607 } while (macinfo_type != 0 && current_file == NULL);
17608
17609 /* Second pass: Process all entries.
17610
17611 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17612 command-line macro definitions/undefinitions. This flag is unset when we
17613 reach the first DW_MACINFO_start_file entry. */
17614
17615 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17616 NULL, xcalloc, xfree);
17617 cleanup = make_cleanup_htab_delete (include_hash);
17618 mac_ptr = section->buffer + offset;
17619 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17620 *slot = mac_ptr;
17621 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17622 current_file, lh, comp_dir, section, section_is_gnu,
17623 offset_size, objfile, include_hash);
17624 do_cleanups (cleanup);
17625 }
17626
17627 /* Check if the attribute's form is a DW_FORM_block*
17628 if so return true else false. */
17629
17630 static int
17631 attr_form_is_block (struct attribute *attr)
17632 {
17633 return (attr == NULL ? 0 :
17634 attr->form == DW_FORM_block1
17635 || attr->form == DW_FORM_block2
17636 || attr->form == DW_FORM_block4
17637 || attr->form == DW_FORM_block
17638 || attr->form == DW_FORM_exprloc);
17639 }
17640
17641 /* Return non-zero if ATTR's value is a section offset --- classes
17642 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17643 You may use DW_UNSND (attr) to retrieve such offsets.
17644
17645 Section 7.5.4, "Attribute Encodings", explains that no attribute
17646 may have a value that belongs to more than one of these classes; it
17647 would be ambiguous if we did, because we use the same forms for all
17648 of them. */
17649
17650 static int
17651 attr_form_is_section_offset (struct attribute *attr)
17652 {
17653 return (attr->form == DW_FORM_data4
17654 || attr->form == DW_FORM_data8
17655 || attr->form == DW_FORM_sec_offset);
17656 }
17657
17658 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17659 zero otherwise. When this function returns true, you can apply
17660 dwarf2_get_attr_constant_value to it.
17661
17662 However, note that for some attributes you must check
17663 attr_form_is_section_offset before using this test. DW_FORM_data4
17664 and DW_FORM_data8 are members of both the constant class, and of
17665 the classes that contain offsets into other debug sections
17666 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17667 that, if an attribute's can be either a constant or one of the
17668 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17669 taken as section offsets, not constants. */
17670
17671 static int
17672 attr_form_is_constant (struct attribute *attr)
17673 {
17674 switch (attr->form)
17675 {
17676 case DW_FORM_sdata:
17677 case DW_FORM_udata:
17678 case DW_FORM_data1:
17679 case DW_FORM_data2:
17680 case DW_FORM_data4:
17681 case DW_FORM_data8:
17682 return 1;
17683 default:
17684 return 0;
17685 }
17686 }
17687
17688 /* Return the .debug_loc section to use for CU.
17689 For DWO files use .debug_loc.dwo. */
17690
17691 static struct dwarf2_section_info *
17692 cu_debug_loc_section (struct dwarf2_cu *cu)
17693 {
17694 if (cu->dwo_unit)
17695 return &cu->dwo_unit->dwo_file->sections.loc;
17696 return &dwarf2_per_objfile->loc;
17697 }
17698
17699 /* A helper function that fills in a dwarf2_loclist_baton. */
17700
17701 static void
17702 fill_in_loclist_baton (struct dwarf2_cu *cu,
17703 struct dwarf2_loclist_baton *baton,
17704 struct attribute *attr)
17705 {
17706 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17707
17708 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17709
17710 baton->per_cu = cu->per_cu;
17711 gdb_assert (baton->per_cu);
17712 /* We don't know how long the location list is, but make sure we
17713 don't run off the edge of the section. */
17714 baton->size = section->size - DW_UNSND (attr);
17715 baton->data = section->buffer + DW_UNSND (attr);
17716 baton->base_address = cu->base_address;
17717 baton->from_dwo = cu->dwo_unit != NULL;
17718 }
17719
17720 static void
17721 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
17722 struct dwarf2_cu *cu)
17723 {
17724 struct objfile *objfile = dwarf2_per_objfile->objfile;
17725 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17726
17727 if (attr_form_is_section_offset (attr)
17728 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
17729 the section. If so, fall through to the complaint in the
17730 other branch. */
17731 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
17732 {
17733 struct dwarf2_loclist_baton *baton;
17734
17735 baton = obstack_alloc (&objfile->objfile_obstack,
17736 sizeof (struct dwarf2_loclist_baton));
17737
17738 fill_in_loclist_baton (cu, baton, attr);
17739
17740 if (cu->base_known == 0)
17741 complaint (&symfile_complaints,
17742 _("Location list used without "
17743 "specifying the CU base address."));
17744
17745 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
17746 SYMBOL_LOCATION_BATON (sym) = baton;
17747 }
17748 else
17749 {
17750 struct dwarf2_locexpr_baton *baton;
17751
17752 baton = obstack_alloc (&objfile->objfile_obstack,
17753 sizeof (struct dwarf2_locexpr_baton));
17754 baton->per_cu = cu->per_cu;
17755 gdb_assert (baton->per_cu);
17756
17757 if (attr_form_is_block (attr))
17758 {
17759 /* Note that we're just copying the block's data pointer
17760 here, not the actual data. We're still pointing into the
17761 info_buffer for SYM's objfile; right now we never release
17762 that buffer, but when we do clean up properly this may
17763 need to change. */
17764 baton->size = DW_BLOCK (attr)->size;
17765 baton->data = DW_BLOCK (attr)->data;
17766 }
17767 else
17768 {
17769 dwarf2_invalid_attrib_class_complaint ("location description",
17770 SYMBOL_NATURAL_NAME (sym));
17771 baton->size = 0;
17772 }
17773
17774 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
17775 SYMBOL_LOCATION_BATON (sym) = baton;
17776 }
17777 }
17778
17779 /* Return the OBJFILE associated with the compilation unit CU. If CU
17780 came from a separate debuginfo file, then the master objfile is
17781 returned. */
17782
17783 struct objfile *
17784 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
17785 {
17786 struct objfile *objfile = per_cu->objfile;
17787
17788 /* Return the master objfile, so that we can report and look up the
17789 correct file containing this variable. */
17790 if (objfile->separate_debug_objfile_backlink)
17791 objfile = objfile->separate_debug_objfile_backlink;
17792
17793 return objfile;
17794 }
17795
17796 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
17797 (CU_HEADERP is unused in such case) or prepare a temporary copy at
17798 CU_HEADERP first. */
17799
17800 static const struct comp_unit_head *
17801 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
17802 struct dwarf2_per_cu_data *per_cu)
17803 {
17804 gdb_byte *info_ptr;
17805
17806 if (per_cu->cu)
17807 return &per_cu->cu->header;
17808
17809 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
17810
17811 memset (cu_headerp, 0, sizeof (*cu_headerp));
17812 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
17813
17814 return cu_headerp;
17815 }
17816
17817 /* Return the address size given in the compilation unit header for CU. */
17818
17819 int
17820 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
17821 {
17822 struct comp_unit_head cu_header_local;
17823 const struct comp_unit_head *cu_headerp;
17824
17825 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17826
17827 return cu_headerp->addr_size;
17828 }
17829
17830 /* Return the offset size given in the compilation unit header for CU. */
17831
17832 int
17833 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
17834 {
17835 struct comp_unit_head cu_header_local;
17836 const struct comp_unit_head *cu_headerp;
17837
17838 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17839
17840 return cu_headerp->offset_size;
17841 }
17842
17843 /* See its dwarf2loc.h declaration. */
17844
17845 int
17846 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
17847 {
17848 struct comp_unit_head cu_header_local;
17849 const struct comp_unit_head *cu_headerp;
17850
17851 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17852
17853 if (cu_headerp->version == 2)
17854 return cu_headerp->addr_size;
17855 else
17856 return cu_headerp->offset_size;
17857 }
17858
17859 /* Return the text offset of the CU. The returned offset comes from
17860 this CU's objfile. If this objfile came from a separate debuginfo
17861 file, then the offset may be different from the corresponding
17862 offset in the parent objfile. */
17863
17864 CORE_ADDR
17865 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
17866 {
17867 struct objfile *objfile = per_cu->objfile;
17868
17869 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17870 }
17871
17872 /* Locate the .debug_info compilation unit from CU's objfile which contains
17873 the DIE at OFFSET. Raises an error on failure. */
17874
17875 static struct dwarf2_per_cu_data *
17876 dwarf2_find_containing_comp_unit (sect_offset offset,
17877 struct objfile *objfile)
17878 {
17879 struct dwarf2_per_cu_data *this_cu;
17880 int low, high;
17881
17882 low = 0;
17883 high = dwarf2_per_objfile->n_comp_units - 1;
17884 while (high > low)
17885 {
17886 int mid = low + (high - low) / 2;
17887
17888 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17889 >= offset.sect_off)
17890 high = mid;
17891 else
17892 low = mid + 1;
17893 }
17894 gdb_assert (low == high);
17895 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17896 > offset.sect_off)
17897 {
17898 if (low == 0)
17899 error (_("Dwarf Error: could not find partial DIE containing "
17900 "offset 0x%lx [in module %s]"),
17901 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
17902
17903 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17904 <= offset.sect_off);
17905 return dwarf2_per_objfile->all_comp_units[low-1];
17906 }
17907 else
17908 {
17909 this_cu = dwarf2_per_objfile->all_comp_units[low];
17910 if (low == dwarf2_per_objfile->n_comp_units - 1
17911 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17912 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17913 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
17914 return this_cu;
17915 }
17916 }
17917
17918 /* Initialize dwarf2_cu CU, owned by PER_CU. */
17919
17920 static void
17921 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
17922 {
17923 memset (cu, 0, sizeof (*cu));
17924 per_cu->cu = cu;
17925 cu->per_cu = per_cu;
17926 cu->objfile = per_cu->objfile;
17927 obstack_init (&cu->comp_unit_obstack);
17928 }
17929
17930 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17931
17932 static void
17933 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17934 enum language pretend_language)
17935 {
17936 struct attribute *attr;
17937
17938 /* Set the language we're debugging. */
17939 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17940 if (attr)
17941 set_cu_language (DW_UNSND (attr), cu);
17942 else
17943 {
17944 cu->language = pretend_language;
17945 cu->language_defn = language_def (cu->language);
17946 }
17947
17948 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17949 if (attr)
17950 cu->producer = DW_STRING (attr);
17951 }
17952
17953 /* Release one cached compilation unit, CU. We unlink it from the tree
17954 of compilation units, but we don't remove it from the read_in_chain;
17955 the caller is responsible for that.
17956 NOTE: DATA is a void * because this function is also used as a
17957 cleanup routine. */
17958
17959 static void
17960 free_heap_comp_unit (void *data)
17961 {
17962 struct dwarf2_cu *cu = data;
17963
17964 gdb_assert (cu->per_cu != NULL);
17965 cu->per_cu->cu = NULL;
17966 cu->per_cu = NULL;
17967
17968 obstack_free (&cu->comp_unit_obstack, NULL);
17969
17970 xfree (cu);
17971 }
17972
17973 /* This cleanup function is passed the address of a dwarf2_cu on the stack
17974 when we're finished with it. We can't free the pointer itself, but be
17975 sure to unlink it from the cache. Also release any associated storage. */
17976
17977 static void
17978 free_stack_comp_unit (void *data)
17979 {
17980 struct dwarf2_cu *cu = data;
17981
17982 gdb_assert (cu->per_cu != NULL);
17983 cu->per_cu->cu = NULL;
17984 cu->per_cu = NULL;
17985
17986 obstack_free (&cu->comp_unit_obstack, NULL);
17987 cu->partial_dies = NULL;
17988 }
17989
17990 /* Free all cached compilation units. */
17991
17992 static void
17993 free_cached_comp_units (void *data)
17994 {
17995 struct dwarf2_per_cu_data *per_cu, **last_chain;
17996
17997 per_cu = dwarf2_per_objfile->read_in_chain;
17998 last_chain = &dwarf2_per_objfile->read_in_chain;
17999 while (per_cu != NULL)
18000 {
18001 struct dwarf2_per_cu_data *next_cu;
18002
18003 next_cu = per_cu->cu->read_in_chain;
18004
18005 free_heap_comp_unit (per_cu->cu);
18006 *last_chain = next_cu;
18007
18008 per_cu = next_cu;
18009 }
18010 }
18011
18012 /* Increase the age counter on each cached compilation unit, and free
18013 any that are too old. */
18014
18015 static void
18016 age_cached_comp_units (void)
18017 {
18018 struct dwarf2_per_cu_data *per_cu, **last_chain;
18019
18020 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18021 per_cu = dwarf2_per_objfile->read_in_chain;
18022 while (per_cu != NULL)
18023 {
18024 per_cu->cu->last_used ++;
18025 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18026 dwarf2_mark (per_cu->cu);
18027 per_cu = per_cu->cu->read_in_chain;
18028 }
18029
18030 per_cu = dwarf2_per_objfile->read_in_chain;
18031 last_chain = &dwarf2_per_objfile->read_in_chain;
18032 while (per_cu != NULL)
18033 {
18034 struct dwarf2_per_cu_data *next_cu;
18035
18036 next_cu = per_cu->cu->read_in_chain;
18037
18038 if (!per_cu->cu->mark)
18039 {
18040 free_heap_comp_unit (per_cu->cu);
18041 *last_chain = next_cu;
18042 }
18043 else
18044 last_chain = &per_cu->cu->read_in_chain;
18045
18046 per_cu = next_cu;
18047 }
18048 }
18049
18050 /* Remove a single compilation unit from the cache. */
18051
18052 static void
18053 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
18054 {
18055 struct dwarf2_per_cu_data *per_cu, **last_chain;
18056
18057 per_cu = dwarf2_per_objfile->read_in_chain;
18058 last_chain = &dwarf2_per_objfile->read_in_chain;
18059 while (per_cu != NULL)
18060 {
18061 struct dwarf2_per_cu_data *next_cu;
18062
18063 next_cu = per_cu->cu->read_in_chain;
18064
18065 if (per_cu == target_per_cu)
18066 {
18067 free_heap_comp_unit (per_cu->cu);
18068 per_cu->cu = NULL;
18069 *last_chain = next_cu;
18070 break;
18071 }
18072 else
18073 last_chain = &per_cu->cu->read_in_chain;
18074
18075 per_cu = next_cu;
18076 }
18077 }
18078
18079 /* Release all extra memory associated with OBJFILE. */
18080
18081 void
18082 dwarf2_free_objfile (struct objfile *objfile)
18083 {
18084 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18085
18086 if (dwarf2_per_objfile == NULL)
18087 return;
18088
18089 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18090 free_cached_comp_units (NULL);
18091
18092 if (dwarf2_per_objfile->quick_file_names_table)
18093 htab_delete (dwarf2_per_objfile->quick_file_names_table);
18094
18095 /* Everything else should be on the objfile obstack. */
18096 }
18097
18098 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18099 We store these in a hash table separate from the DIEs, and preserve them
18100 when the DIEs are flushed out of cache.
18101
18102 The CU "per_cu" pointer is needed because offset alone is not enough to
18103 uniquely identify the type. A file may have multiple .debug_types sections,
18104 or the type may come from a DWO file. We have to use something in
18105 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18106 routine, get_die_type_at_offset, from outside this file, and thus won't
18107 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18108 of the objfile. */
18109
18110 struct dwarf2_per_cu_offset_and_type
18111 {
18112 const struct dwarf2_per_cu_data *per_cu;
18113 sect_offset offset;
18114 struct type *type;
18115 };
18116
18117 /* Hash function for a dwarf2_per_cu_offset_and_type. */
18118
18119 static hashval_t
18120 per_cu_offset_and_type_hash (const void *item)
18121 {
18122 const struct dwarf2_per_cu_offset_and_type *ofs = item;
18123
18124 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
18125 }
18126
18127 /* Equality function for a dwarf2_per_cu_offset_and_type. */
18128
18129 static int
18130 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
18131 {
18132 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18133 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
18134
18135 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18136 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18137 }
18138
18139 /* Set the type associated with DIE to TYPE. Save it in CU's hash
18140 table if necessary. For convenience, return TYPE.
18141
18142 The DIEs reading must have careful ordering to:
18143 * Not cause infite loops trying to read in DIEs as a prerequisite for
18144 reading current DIE.
18145 * Not trying to dereference contents of still incompletely read in types
18146 while reading in other DIEs.
18147 * Enable referencing still incompletely read in types just by a pointer to
18148 the type without accessing its fields.
18149
18150 Therefore caller should follow these rules:
18151 * Try to fetch any prerequisite types we may need to build this DIE type
18152 before building the type and calling set_die_type.
18153 * After building type call set_die_type for current DIE as soon as
18154 possible before fetching more types to complete the current type.
18155 * Make the type as complete as possible before fetching more types. */
18156
18157 static struct type *
18158 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18159 {
18160 struct dwarf2_per_cu_offset_and_type **slot, ofs;
18161 struct objfile *objfile = cu->objfile;
18162
18163 /* For Ada types, make sure that the gnat-specific data is always
18164 initialized (if not already set). There are a few types where
18165 we should not be doing so, because the type-specific area is
18166 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18167 where the type-specific area is used to store the floatformat).
18168 But this is not a problem, because the gnat-specific information
18169 is actually not needed for these types. */
18170 if (need_gnat_info (cu)
18171 && TYPE_CODE (type) != TYPE_CODE_FUNC
18172 && TYPE_CODE (type) != TYPE_CODE_FLT
18173 && !HAVE_GNAT_AUX_INFO (type))
18174 INIT_GNAT_SPECIFIC (type);
18175
18176 if (dwarf2_per_objfile->die_type_hash == NULL)
18177 {
18178 dwarf2_per_objfile->die_type_hash =
18179 htab_create_alloc_ex (127,
18180 per_cu_offset_and_type_hash,
18181 per_cu_offset_and_type_eq,
18182 NULL,
18183 &objfile->objfile_obstack,
18184 hashtab_obstack_allocate,
18185 dummy_obstack_deallocate);
18186 }
18187
18188 ofs.per_cu = cu->per_cu;
18189 ofs.offset = die->offset;
18190 ofs.type = type;
18191 slot = (struct dwarf2_per_cu_offset_and_type **)
18192 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18193 if (*slot)
18194 complaint (&symfile_complaints,
18195 _("A problem internal to GDB: DIE 0x%x has type already set"),
18196 die->offset.sect_off);
18197 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18198 **slot = ofs;
18199 return type;
18200 }
18201
18202 /* Look up the type for the die at OFFSET in the appropriate type_hash
18203 table, or return NULL if the die does not have a saved type. */
18204
18205 static struct type *
18206 get_die_type_at_offset (sect_offset offset,
18207 struct dwarf2_per_cu_data *per_cu)
18208 {
18209 struct dwarf2_per_cu_offset_and_type *slot, ofs;
18210
18211 if (dwarf2_per_objfile->die_type_hash == NULL)
18212 return NULL;
18213
18214 ofs.per_cu = per_cu;
18215 ofs.offset = offset;
18216 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18217 if (slot)
18218 return slot->type;
18219 else
18220 return NULL;
18221 }
18222
18223 /* Look up the type for DIE in the appropriate type_hash table,
18224 or return NULL if DIE does not have a saved type. */
18225
18226 static struct type *
18227 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18228 {
18229 return get_die_type_at_offset (die->offset, cu->per_cu);
18230 }
18231
18232 /* Add a dependence relationship from CU to REF_PER_CU. */
18233
18234 static void
18235 dwarf2_add_dependence (struct dwarf2_cu *cu,
18236 struct dwarf2_per_cu_data *ref_per_cu)
18237 {
18238 void **slot;
18239
18240 if (cu->dependencies == NULL)
18241 cu->dependencies
18242 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18243 NULL, &cu->comp_unit_obstack,
18244 hashtab_obstack_allocate,
18245 dummy_obstack_deallocate);
18246
18247 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18248 if (*slot == NULL)
18249 *slot = ref_per_cu;
18250 }
18251
18252 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18253 Set the mark field in every compilation unit in the
18254 cache that we must keep because we are keeping CU. */
18255
18256 static int
18257 dwarf2_mark_helper (void **slot, void *data)
18258 {
18259 struct dwarf2_per_cu_data *per_cu;
18260
18261 per_cu = (struct dwarf2_per_cu_data *) *slot;
18262
18263 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18264 reading of the chain. As such dependencies remain valid it is not much
18265 useful to track and undo them during QUIT cleanups. */
18266 if (per_cu->cu == NULL)
18267 return 1;
18268
18269 if (per_cu->cu->mark)
18270 return 1;
18271 per_cu->cu->mark = 1;
18272
18273 if (per_cu->cu->dependencies != NULL)
18274 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18275
18276 return 1;
18277 }
18278
18279 /* Set the mark field in CU and in every other compilation unit in the
18280 cache that we must keep because we are keeping CU. */
18281
18282 static void
18283 dwarf2_mark (struct dwarf2_cu *cu)
18284 {
18285 if (cu->mark)
18286 return;
18287 cu->mark = 1;
18288 if (cu->dependencies != NULL)
18289 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18290 }
18291
18292 static void
18293 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18294 {
18295 while (per_cu)
18296 {
18297 per_cu->cu->mark = 0;
18298 per_cu = per_cu->cu->read_in_chain;
18299 }
18300 }
18301
18302 /* Trivial hash function for partial_die_info: the hash value of a DIE
18303 is its offset in .debug_info for this objfile. */
18304
18305 static hashval_t
18306 partial_die_hash (const void *item)
18307 {
18308 const struct partial_die_info *part_die = item;
18309
18310 return part_die->offset.sect_off;
18311 }
18312
18313 /* Trivial comparison function for partial_die_info structures: two DIEs
18314 are equal if they have the same offset. */
18315
18316 static int
18317 partial_die_eq (const void *item_lhs, const void *item_rhs)
18318 {
18319 const struct partial_die_info *part_die_lhs = item_lhs;
18320 const struct partial_die_info *part_die_rhs = item_rhs;
18321
18322 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18323 }
18324
18325 static struct cmd_list_element *set_dwarf2_cmdlist;
18326 static struct cmd_list_element *show_dwarf2_cmdlist;
18327
18328 static void
18329 set_dwarf2_cmd (char *args, int from_tty)
18330 {
18331 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18332 }
18333
18334 static void
18335 show_dwarf2_cmd (char *args, int from_tty)
18336 {
18337 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18338 }
18339
18340 /* If section described by INFO was mmapped, munmap it now. */
18341
18342 static void
18343 munmap_section_buffer (struct dwarf2_section_info *info)
18344 {
18345 if (info->map_addr != NULL)
18346 {
18347 #ifdef HAVE_MMAP
18348 int res;
18349
18350 res = munmap (info->map_addr, info->map_len);
18351 gdb_assert (res == 0);
18352 #else
18353 /* Without HAVE_MMAP, we should never be here to begin with. */
18354 gdb_assert_not_reached ("no mmap support");
18355 #endif
18356 }
18357 }
18358
18359 /* munmap debug sections for OBJFILE, if necessary. */
18360
18361 static void
18362 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18363 {
18364 struct dwarf2_per_objfile *data = d;
18365 int ix;
18366 struct dwarf2_section_info *section;
18367
18368 /* This is sorted according to the order they're defined in to make it easier
18369 to keep in sync. */
18370 munmap_section_buffer (&data->info);
18371 munmap_section_buffer (&data->abbrev);
18372 munmap_section_buffer (&data->line);
18373 munmap_section_buffer (&data->loc);
18374 munmap_section_buffer (&data->macinfo);
18375 munmap_section_buffer (&data->macro);
18376 munmap_section_buffer (&data->str);
18377 munmap_section_buffer (&data->ranges);
18378 munmap_section_buffer (&data->addr);
18379 munmap_section_buffer (&data->frame);
18380 munmap_section_buffer (&data->eh_frame);
18381 munmap_section_buffer (&data->gdb_index);
18382
18383 for (ix = 0;
18384 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
18385 ++ix)
18386 munmap_section_buffer (section);
18387
18388 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18389 VEC_free (dwarf2_per_cu_ptr,
18390 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18391
18392 VEC_free (dwarf2_section_info_def, data->types);
18393
18394 if (data->dwo_files)
18395 free_dwo_files (data->dwo_files, objfile);
18396 }
18397
18398 \f
18399 /* The "save gdb-index" command. */
18400
18401 /* The contents of the hash table we create when building the string
18402 table. */
18403 struct strtab_entry
18404 {
18405 offset_type offset;
18406 const char *str;
18407 };
18408
18409 /* Hash function for a strtab_entry.
18410
18411 Function is used only during write_hash_table so no index format backward
18412 compatibility is needed. */
18413
18414 static hashval_t
18415 hash_strtab_entry (const void *e)
18416 {
18417 const struct strtab_entry *entry = e;
18418 return mapped_index_string_hash (INT_MAX, entry->str);
18419 }
18420
18421 /* Equality function for a strtab_entry. */
18422
18423 static int
18424 eq_strtab_entry (const void *a, const void *b)
18425 {
18426 const struct strtab_entry *ea = a;
18427 const struct strtab_entry *eb = b;
18428 return !strcmp (ea->str, eb->str);
18429 }
18430
18431 /* Create a strtab_entry hash table. */
18432
18433 static htab_t
18434 create_strtab (void)
18435 {
18436 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18437 xfree, xcalloc, xfree);
18438 }
18439
18440 /* Add a string to the constant pool. Return the string's offset in
18441 host order. */
18442
18443 static offset_type
18444 add_string (htab_t table, struct obstack *cpool, const char *str)
18445 {
18446 void **slot;
18447 struct strtab_entry entry;
18448 struct strtab_entry *result;
18449
18450 entry.str = str;
18451 slot = htab_find_slot (table, &entry, INSERT);
18452 if (*slot)
18453 result = *slot;
18454 else
18455 {
18456 result = XNEW (struct strtab_entry);
18457 result->offset = obstack_object_size (cpool);
18458 result->str = str;
18459 obstack_grow_str0 (cpool, str);
18460 *slot = result;
18461 }
18462 return result->offset;
18463 }
18464
18465 /* An entry in the symbol table. */
18466 struct symtab_index_entry
18467 {
18468 /* The name of the symbol. */
18469 const char *name;
18470 /* The offset of the name in the constant pool. */
18471 offset_type index_offset;
18472 /* A sorted vector of the indices of all the CUs that hold an object
18473 of this name. */
18474 VEC (offset_type) *cu_indices;
18475 };
18476
18477 /* The symbol table. This is a power-of-2-sized hash table. */
18478 struct mapped_symtab
18479 {
18480 offset_type n_elements;
18481 offset_type size;
18482 struct symtab_index_entry **data;
18483 };
18484
18485 /* Hash function for a symtab_index_entry. */
18486
18487 static hashval_t
18488 hash_symtab_entry (const void *e)
18489 {
18490 const struct symtab_index_entry *entry = e;
18491 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18492 sizeof (offset_type) * VEC_length (offset_type,
18493 entry->cu_indices),
18494 0);
18495 }
18496
18497 /* Equality function for a symtab_index_entry. */
18498
18499 static int
18500 eq_symtab_entry (const void *a, const void *b)
18501 {
18502 const struct symtab_index_entry *ea = a;
18503 const struct symtab_index_entry *eb = b;
18504 int len = VEC_length (offset_type, ea->cu_indices);
18505 if (len != VEC_length (offset_type, eb->cu_indices))
18506 return 0;
18507 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18508 VEC_address (offset_type, eb->cu_indices),
18509 sizeof (offset_type) * len);
18510 }
18511
18512 /* Destroy a symtab_index_entry. */
18513
18514 static void
18515 delete_symtab_entry (void *p)
18516 {
18517 struct symtab_index_entry *entry = p;
18518 VEC_free (offset_type, entry->cu_indices);
18519 xfree (entry);
18520 }
18521
18522 /* Create a hash table holding symtab_index_entry objects. */
18523
18524 static htab_t
18525 create_symbol_hash_table (void)
18526 {
18527 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18528 delete_symtab_entry, xcalloc, xfree);
18529 }
18530
18531 /* Create a new mapped symtab object. */
18532
18533 static struct mapped_symtab *
18534 create_mapped_symtab (void)
18535 {
18536 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18537 symtab->n_elements = 0;
18538 symtab->size = 1024;
18539 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18540 return symtab;
18541 }
18542
18543 /* Destroy a mapped_symtab. */
18544
18545 static void
18546 cleanup_mapped_symtab (void *p)
18547 {
18548 struct mapped_symtab *symtab = p;
18549 /* The contents of the array are freed when the other hash table is
18550 destroyed. */
18551 xfree (symtab->data);
18552 xfree (symtab);
18553 }
18554
18555 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
18556 the slot.
18557
18558 Function is used only during write_hash_table so no index format backward
18559 compatibility is needed. */
18560
18561 static struct symtab_index_entry **
18562 find_slot (struct mapped_symtab *symtab, const char *name)
18563 {
18564 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18565
18566 index = hash & (symtab->size - 1);
18567 step = ((hash * 17) & (symtab->size - 1)) | 1;
18568
18569 for (;;)
18570 {
18571 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18572 return &symtab->data[index];
18573 index = (index + step) & (symtab->size - 1);
18574 }
18575 }
18576
18577 /* Expand SYMTAB's hash table. */
18578
18579 static void
18580 hash_expand (struct mapped_symtab *symtab)
18581 {
18582 offset_type old_size = symtab->size;
18583 offset_type i;
18584 struct symtab_index_entry **old_entries = symtab->data;
18585
18586 symtab->size *= 2;
18587 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18588
18589 for (i = 0; i < old_size; ++i)
18590 {
18591 if (old_entries[i])
18592 {
18593 struct symtab_index_entry **slot = find_slot (symtab,
18594 old_entries[i]->name);
18595 *slot = old_entries[i];
18596 }
18597 }
18598
18599 xfree (old_entries);
18600 }
18601
18602 /* Add an entry to SYMTAB. NAME is the name of the symbol.
18603 CU_INDEX is the index of the CU in which the symbol appears.
18604 IS_STATIC is one if the symbol is static, otherwise zero (global). */
18605
18606 static void
18607 add_index_entry (struct mapped_symtab *symtab, const char *name,
18608 int is_static, gdb_index_symbol_kind kind,
18609 offset_type cu_index)
18610 {
18611 struct symtab_index_entry **slot;
18612 offset_type cu_index_and_attrs;
18613
18614 ++symtab->n_elements;
18615 if (4 * symtab->n_elements / 3 >= symtab->size)
18616 hash_expand (symtab);
18617
18618 slot = find_slot (symtab, name);
18619 if (!*slot)
18620 {
18621 *slot = XNEW (struct symtab_index_entry);
18622 (*slot)->name = name;
18623 /* index_offset is set later. */
18624 (*slot)->cu_indices = NULL;
18625 }
18626
18627 cu_index_and_attrs = 0;
18628 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18629 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18630 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18631
18632 /* We don't want to record an index value twice as we want to avoid the
18633 duplication.
18634 We process all global symbols and then all static symbols
18635 (which would allow us to avoid the duplication by only having to check
18636 the last entry pushed), but a symbol could have multiple kinds in one CU.
18637 To keep things simple we don't worry about the duplication here and
18638 sort and uniqufy the list after we've processed all symbols. */
18639 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18640 }
18641
18642 /* qsort helper routine for uniquify_cu_indices. */
18643
18644 static int
18645 offset_type_compare (const void *ap, const void *bp)
18646 {
18647 offset_type a = *(offset_type *) ap;
18648 offset_type b = *(offset_type *) bp;
18649
18650 return (a > b) - (b > a);
18651 }
18652
18653 /* Sort and remove duplicates of all symbols' cu_indices lists. */
18654
18655 static void
18656 uniquify_cu_indices (struct mapped_symtab *symtab)
18657 {
18658 int i;
18659
18660 for (i = 0; i < symtab->size; ++i)
18661 {
18662 struct symtab_index_entry *entry = symtab->data[i];
18663
18664 if (entry
18665 && entry->cu_indices != NULL)
18666 {
18667 unsigned int next_to_insert, next_to_check;
18668 offset_type last_value;
18669
18670 qsort (VEC_address (offset_type, entry->cu_indices),
18671 VEC_length (offset_type, entry->cu_indices),
18672 sizeof (offset_type), offset_type_compare);
18673
18674 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18675 next_to_insert = 1;
18676 for (next_to_check = 1;
18677 next_to_check < VEC_length (offset_type, entry->cu_indices);
18678 ++next_to_check)
18679 {
18680 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18681 != last_value)
18682 {
18683 last_value = VEC_index (offset_type, entry->cu_indices,
18684 next_to_check);
18685 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18686 last_value);
18687 ++next_to_insert;
18688 }
18689 }
18690 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18691 }
18692 }
18693 }
18694
18695 /* Add a vector of indices to the constant pool. */
18696
18697 static offset_type
18698 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18699 struct symtab_index_entry *entry)
18700 {
18701 void **slot;
18702
18703 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18704 if (!*slot)
18705 {
18706 offset_type len = VEC_length (offset_type, entry->cu_indices);
18707 offset_type val = MAYBE_SWAP (len);
18708 offset_type iter;
18709 int i;
18710
18711 *slot = entry;
18712 entry->index_offset = obstack_object_size (cpool);
18713
18714 obstack_grow (cpool, &val, sizeof (val));
18715 for (i = 0;
18716 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18717 ++i)
18718 {
18719 val = MAYBE_SWAP (iter);
18720 obstack_grow (cpool, &val, sizeof (val));
18721 }
18722 }
18723 else
18724 {
18725 struct symtab_index_entry *old_entry = *slot;
18726 entry->index_offset = old_entry->index_offset;
18727 entry = old_entry;
18728 }
18729 return entry->index_offset;
18730 }
18731
18732 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
18733 constant pool entries going into the obstack CPOOL. */
18734
18735 static void
18736 write_hash_table (struct mapped_symtab *symtab,
18737 struct obstack *output, struct obstack *cpool)
18738 {
18739 offset_type i;
18740 htab_t symbol_hash_table;
18741 htab_t str_table;
18742
18743 symbol_hash_table = create_symbol_hash_table ();
18744 str_table = create_strtab ();
18745
18746 /* We add all the index vectors to the constant pool first, to
18747 ensure alignment is ok. */
18748 for (i = 0; i < symtab->size; ++i)
18749 {
18750 if (symtab->data[i])
18751 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
18752 }
18753
18754 /* Now write out the hash table. */
18755 for (i = 0; i < symtab->size; ++i)
18756 {
18757 offset_type str_off, vec_off;
18758
18759 if (symtab->data[i])
18760 {
18761 str_off = add_string (str_table, cpool, symtab->data[i]->name);
18762 vec_off = symtab->data[i]->index_offset;
18763 }
18764 else
18765 {
18766 /* While 0 is a valid constant pool index, it is not valid
18767 to have 0 for both offsets. */
18768 str_off = 0;
18769 vec_off = 0;
18770 }
18771
18772 str_off = MAYBE_SWAP (str_off);
18773 vec_off = MAYBE_SWAP (vec_off);
18774
18775 obstack_grow (output, &str_off, sizeof (str_off));
18776 obstack_grow (output, &vec_off, sizeof (vec_off));
18777 }
18778
18779 htab_delete (str_table);
18780 htab_delete (symbol_hash_table);
18781 }
18782
18783 /* Struct to map psymtab to CU index in the index file. */
18784 struct psymtab_cu_index_map
18785 {
18786 struct partial_symtab *psymtab;
18787 unsigned int cu_index;
18788 };
18789
18790 static hashval_t
18791 hash_psymtab_cu_index (const void *item)
18792 {
18793 const struct psymtab_cu_index_map *map = item;
18794
18795 return htab_hash_pointer (map->psymtab);
18796 }
18797
18798 static int
18799 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
18800 {
18801 const struct psymtab_cu_index_map *lhs = item_lhs;
18802 const struct psymtab_cu_index_map *rhs = item_rhs;
18803
18804 return lhs->psymtab == rhs->psymtab;
18805 }
18806
18807 /* Helper struct for building the address table. */
18808 struct addrmap_index_data
18809 {
18810 struct objfile *objfile;
18811 struct obstack *addr_obstack;
18812 htab_t cu_index_htab;
18813
18814 /* Non-zero if the previous_* fields are valid.
18815 We can't write an entry until we see the next entry (since it is only then
18816 that we know the end of the entry). */
18817 int previous_valid;
18818 /* Index of the CU in the table of all CUs in the index file. */
18819 unsigned int previous_cu_index;
18820 /* Start address of the CU. */
18821 CORE_ADDR previous_cu_start;
18822 };
18823
18824 /* Write an address entry to OBSTACK. */
18825
18826 static void
18827 add_address_entry (struct objfile *objfile, struct obstack *obstack,
18828 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
18829 {
18830 offset_type cu_index_to_write;
18831 char addr[8];
18832 CORE_ADDR baseaddr;
18833
18834 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18835
18836 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
18837 obstack_grow (obstack, addr, 8);
18838 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
18839 obstack_grow (obstack, addr, 8);
18840 cu_index_to_write = MAYBE_SWAP (cu_index);
18841 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
18842 }
18843
18844 /* Worker function for traversing an addrmap to build the address table. */
18845
18846 static int
18847 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
18848 {
18849 struct addrmap_index_data *data = datap;
18850 struct partial_symtab *pst = obj;
18851
18852 if (data->previous_valid)
18853 add_address_entry (data->objfile, data->addr_obstack,
18854 data->previous_cu_start, start_addr,
18855 data->previous_cu_index);
18856
18857 data->previous_cu_start = start_addr;
18858 if (pst != NULL)
18859 {
18860 struct psymtab_cu_index_map find_map, *map;
18861 find_map.psymtab = pst;
18862 map = htab_find (data->cu_index_htab, &find_map);
18863 gdb_assert (map != NULL);
18864 data->previous_cu_index = map->cu_index;
18865 data->previous_valid = 1;
18866 }
18867 else
18868 data->previous_valid = 0;
18869
18870 return 0;
18871 }
18872
18873 /* Write OBJFILE's address map to OBSTACK.
18874 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18875 in the index file. */
18876
18877 static void
18878 write_address_map (struct objfile *objfile, struct obstack *obstack,
18879 htab_t cu_index_htab)
18880 {
18881 struct addrmap_index_data addrmap_index_data;
18882
18883 /* When writing the address table, we have to cope with the fact that
18884 the addrmap iterator only provides the start of a region; we have to
18885 wait until the next invocation to get the start of the next region. */
18886
18887 addrmap_index_data.objfile = objfile;
18888 addrmap_index_data.addr_obstack = obstack;
18889 addrmap_index_data.cu_index_htab = cu_index_htab;
18890 addrmap_index_data.previous_valid = 0;
18891
18892 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18893 &addrmap_index_data);
18894
18895 /* It's highly unlikely the last entry (end address = 0xff...ff)
18896 is valid, but we should still handle it.
18897 The end address is recorded as the start of the next region, but that
18898 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18899 anyway. */
18900 if (addrmap_index_data.previous_valid)
18901 add_address_entry (objfile, obstack,
18902 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18903 addrmap_index_data.previous_cu_index);
18904 }
18905
18906 /* Return the symbol kind of PSYM. */
18907
18908 static gdb_index_symbol_kind
18909 symbol_kind (struct partial_symbol *psym)
18910 {
18911 domain_enum domain = PSYMBOL_DOMAIN (psym);
18912 enum address_class aclass = PSYMBOL_CLASS (psym);
18913
18914 switch (domain)
18915 {
18916 case VAR_DOMAIN:
18917 switch (aclass)
18918 {
18919 case LOC_BLOCK:
18920 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
18921 case LOC_TYPEDEF:
18922 return GDB_INDEX_SYMBOL_KIND_TYPE;
18923 case LOC_COMPUTED:
18924 case LOC_CONST_BYTES:
18925 case LOC_OPTIMIZED_OUT:
18926 case LOC_STATIC:
18927 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18928 case LOC_CONST:
18929 /* Note: It's currently impossible to recognize psyms as enum values
18930 short of reading the type info. For now punt. */
18931 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18932 default:
18933 /* There are other LOC_FOO values that one might want to classify
18934 as variables, but dwarf2read.c doesn't currently use them. */
18935 return GDB_INDEX_SYMBOL_KIND_OTHER;
18936 }
18937 case STRUCT_DOMAIN:
18938 return GDB_INDEX_SYMBOL_KIND_TYPE;
18939 default:
18940 return GDB_INDEX_SYMBOL_KIND_OTHER;
18941 }
18942 }
18943
18944 /* Add a list of partial symbols to SYMTAB. */
18945
18946 static void
18947 write_psymbols (struct mapped_symtab *symtab,
18948 htab_t psyms_seen,
18949 struct partial_symbol **psymp,
18950 int count,
18951 offset_type cu_index,
18952 int is_static)
18953 {
18954 for (; count-- > 0; ++psymp)
18955 {
18956 struct partial_symbol *psym = *psymp;
18957 void **slot;
18958
18959 if (SYMBOL_LANGUAGE (psym) == language_ada)
18960 error (_("Ada is not currently supported by the index"));
18961
18962 /* Only add a given psymbol once. */
18963 slot = htab_find_slot (psyms_seen, psym, INSERT);
18964 if (!*slot)
18965 {
18966 gdb_index_symbol_kind kind = symbol_kind (psym);
18967
18968 *slot = psym;
18969 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18970 is_static, kind, cu_index);
18971 }
18972 }
18973 }
18974
18975 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
18976 exception if there is an error. */
18977
18978 static void
18979 write_obstack (FILE *file, struct obstack *obstack)
18980 {
18981 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18982 file)
18983 != obstack_object_size (obstack))
18984 error (_("couldn't data write to file"));
18985 }
18986
18987 /* Unlink a file if the argument is not NULL. */
18988
18989 static void
18990 unlink_if_set (void *p)
18991 {
18992 char **filename = p;
18993 if (*filename)
18994 unlink (*filename);
18995 }
18996
18997 /* A helper struct used when iterating over debug_types. */
18998 struct signatured_type_index_data
18999 {
19000 struct objfile *objfile;
19001 struct mapped_symtab *symtab;
19002 struct obstack *types_list;
19003 htab_t psyms_seen;
19004 int cu_index;
19005 };
19006
19007 /* A helper function that writes a single signatured_type to an
19008 obstack. */
19009
19010 static int
19011 write_one_signatured_type (void **slot, void *d)
19012 {
19013 struct signatured_type_index_data *info = d;
19014 struct signatured_type *entry = (struct signatured_type *) *slot;
19015 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19016 struct partial_symtab *psymtab = per_cu->v.psymtab;
19017 gdb_byte val[8];
19018
19019 write_psymbols (info->symtab,
19020 info->psyms_seen,
19021 info->objfile->global_psymbols.list
19022 + psymtab->globals_offset,
19023 psymtab->n_global_syms, info->cu_index,
19024 0);
19025 write_psymbols (info->symtab,
19026 info->psyms_seen,
19027 info->objfile->static_psymbols.list
19028 + psymtab->statics_offset,
19029 psymtab->n_static_syms, info->cu_index,
19030 1);
19031
19032 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19033 entry->per_cu.offset.sect_off);
19034 obstack_grow (info->types_list, val, 8);
19035 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19036 entry->type_offset_in_tu.cu_off);
19037 obstack_grow (info->types_list, val, 8);
19038 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19039 obstack_grow (info->types_list, val, 8);
19040
19041 ++info->cu_index;
19042
19043 return 1;
19044 }
19045
19046 /* Recurse into all "included" dependencies and write their symbols as
19047 if they appeared in this psymtab. */
19048
19049 static void
19050 recursively_write_psymbols (struct objfile *objfile,
19051 struct partial_symtab *psymtab,
19052 struct mapped_symtab *symtab,
19053 htab_t psyms_seen,
19054 offset_type cu_index)
19055 {
19056 int i;
19057
19058 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19059 if (psymtab->dependencies[i]->user != NULL)
19060 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19061 symtab, psyms_seen, cu_index);
19062
19063 write_psymbols (symtab,
19064 psyms_seen,
19065 objfile->global_psymbols.list + psymtab->globals_offset,
19066 psymtab->n_global_syms, cu_index,
19067 0);
19068 write_psymbols (symtab,
19069 psyms_seen,
19070 objfile->static_psymbols.list + psymtab->statics_offset,
19071 psymtab->n_static_syms, cu_index,
19072 1);
19073 }
19074
19075 /* Create an index file for OBJFILE in the directory DIR. */
19076
19077 static void
19078 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19079 {
19080 struct cleanup *cleanup;
19081 char *filename, *cleanup_filename;
19082 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19083 struct obstack cu_list, types_cu_list;
19084 int i;
19085 FILE *out_file;
19086 struct mapped_symtab *symtab;
19087 offset_type val, size_of_contents, total_len;
19088 struct stat st;
19089 htab_t psyms_seen;
19090 htab_t cu_index_htab;
19091 struct psymtab_cu_index_map *psymtab_cu_index_map;
19092
19093 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
19094 return;
19095
19096 if (dwarf2_per_objfile->using_index)
19097 error (_("Cannot use an index to create the index"));
19098
19099 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19100 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19101
19102 if (stat (objfile->name, &st) < 0)
19103 perror_with_name (objfile->name);
19104
19105 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19106 INDEX_SUFFIX, (char *) NULL);
19107 cleanup = make_cleanup (xfree, filename);
19108
19109 out_file = fopen (filename, "wb");
19110 if (!out_file)
19111 error (_("Can't open `%s' for writing"), filename);
19112
19113 cleanup_filename = filename;
19114 make_cleanup (unlink_if_set, &cleanup_filename);
19115
19116 symtab = create_mapped_symtab ();
19117 make_cleanup (cleanup_mapped_symtab, symtab);
19118
19119 obstack_init (&addr_obstack);
19120 make_cleanup_obstack_free (&addr_obstack);
19121
19122 obstack_init (&cu_list);
19123 make_cleanup_obstack_free (&cu_list);
19124
19125 obstack_init (&types_cu_list);
19126 make_cleanup_obstack_free (&types_cu_list);
19127
19128 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19129 NULL, xcalloc, xfree);
19130 make_cleanup_htab_delete (psyms_seen);
19131
19132 /* While we're scanning CU's create a table that maps a psymtab pointer
19133 (which is what addrmap records) to its index (which is what is recorded
19134 in the index file). This will later be needed to write the address
19135 table. */
19136 cu_index_htab = htab_create_alloc (100,
19137 hash_psymtab_cu_index,
19138 eq_psymtab_cu_index,
19139 NULL, xcalloc, xfree);
19140 make_cleanup_htab_delete (cu_index_htab);
19141 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19142 xmalloc (sizeof (struct psymtab_cu_index_map)
19143 * dwarf2_per_objfile->n_comp_units);
19144 make_cleanup (xfree, psymtab_cu_index_map);
19145
19146 /* The CU list is already sorted, so we don't need to do additional
19147 work here. Also, the debug_types entries do not appear in
19148 all_comp_units, but only in their own hash table. */
19149 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19150 {
19151 struct dwarf2_per_cu_data *per_cu
19152 = dwarf2_per_objfile->all_comp_units[i];
19153 struct partial_symtab *psymtab = per_cu->v.psymtab;
19154 gdb_byte val[8];
19155 struct psymtab_cu_index_map *map;
19156 void **slot;
19157
19158 if (psymtab->user == NULL)
19159 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
19160
19161 map = &psymtab_cu_index_map[i];
19162 map->psymtab = psymtab;
19163 map->cu_index = i;
19164 slot = htab_find_slot (cu_index_htab, map, INSERT);
19165 gdb_assert (slot != NULL);
19166 gdb_assert (*slot == NULL);
19167 *slot = map;
19168
19169 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19170 per_cu->offset.sect_off);
19171 obstack_grow (&cu_list, val, 8);
19172 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
19173 obstack_grow (&cu_list, val, 8);
19174 }
19175
19176 /* Dump the address map. */
19177 write_address_map (objfile, &addr_obstack, cu_index_htab);
19178
19179 /* Write out the .debug_type entries, if any. */
19180 if (dwarf2_per_objfile->signatured_types)
19181 {
19182 struct signatured_type_index_data sig_data;
19183
19184 sig_data.objfile = objfile;
19185 sig_data.symtab = symtab;
19186 sig_data.types_list = &types_cu_list;
19187 sig_data.psyms_seen = psyms_seen;
19188 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19189 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19190 write_one_signatured_type, &sig_data);
19191 }
19192
19193 /* Now that we've processed all symbols we can shrink their cu_indices
19194 lists. */
19195 uniquify_cu_indices (symtab);
19196
19197 obstack_init (&constant_pool);
19198 make_cleanup_obstack_free (&constant_pool);
19199 obstack_init (&symtab_obstack);
19200 make_cleanup_obstack_free (&symtab_obstack);
19201 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19202
19203 obstack_init (&contents);
19204 make_cleanup_obstack_free (&contents);
19205 size_of_contents = 6 * sizeof (offset_type);
19206 total_len = size_of_contents;
19207
19208 /* The version number. */
19209 val = MAYBE_SWAP (7);
19210 obstack_grow (&contents, &val, sizeof (val));
19211
19212 /* The offset of the CU list from the start of the file. */
19213 val = MAYBE_SWAP (total_len);
19214 obstack_grow (&contents, &val, sizeof (val));
19215 total_len += obstack_object_size (&cu_list);
19216
19217 /* The offset of the types CU list from the start of the file. */
19218 val = MAYBE_SWAP (total_len);
19219 obstack_grow (&contents, &val, sizeof (val));
19220 total_len += obstack_object_size (&types_cu_list);
19221
19222 /* The offset of the address table from the start of the file. */
19223 val = MAYBE_SWAP (total_len);
19224 obstack_grow (&contents, &val, sizeof (val));
19225 total_len += obstack_object_size (&addr_obstack);
19226
19227 /* The offset of the symbol table from the start of the file. */
19228 val = MAYBE_SWAP (total_len);
19229 obstack_grow (&contents, &val, sizeof (val));
19230 total_len += obstack_object_size (&symtab_obstack);
19231
19232 /* The offset of the constant pool from the start of the file. */
19233 val = MAYBE_SWAP (total_len);
19234 obstack_grow (&contents, &val, sizeof (val));
19235 total_len += obstack_object_size (&constant_pool);
19236
19237 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19238
19239 write_obstack (out_file, &contents);
19240 write_obstack (out_file, &cu_list);
19241 write_obstack (out_file, &types_cu_list);
19242 write_obstack (out_file, &addr_obstack);
19243 write_obstack (out_file, &symtab_obstack);
19244 write_obstack (out_file, &constant_pool);
19245
19246 fclose (out_file);
19247
19248 /* We want to keep the file, so we set cleanup_filename to NULL
19249 here. See unlink_if_set. */
19250 cleanup_filename = NULL;
19251
19252 do_cleanups (cleanup);
19253 }
19254
19255 /* Implementation of the `save gdb-index' command.
19256
19257 Note that the file format used by this command is documented in the
19258 GDB manual. Any changes here must be documented there. */
19259
19260 static void
19261 save_gdb_index_command (char *arg, int from_tty)
19262 {
19263 struct objfile *objfile;
19264
19265 if (!arg || !*arg)
19266 error (_("usage: save gdb-index DIRECTORY"));
19267
19268 ALL_OBJFILES (objfile)
19269 {
19270 struct stat st;
19271
19272 /* If the objfile does not correspond to an actual file, skip it. */
19273 if (stat (objfile->name, &st) < 0)
19274 continue;
19275
19276 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19277 if (dwarf2_per_objfile)
19278 {
19279 volatile struct gdb_exception except;
19280
19281 TRY_CATCH (except, RETURN_MASK_ERROR)
19282 {
19283 write_psymtabs_to_index (objfile, arg);
19284 }
19285 if (except.reason < 0)
19286 exception_fprintf (gdb_stderr, except,
19287 _("Error while writing index for `%s': "),
19288 objfile->name);
19289 }
19290 }
19291 }
19292
19293 \f
19294
19295 int dwarf2_always_disassemble;
19296
19297 static void
19298 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19299 struct cmd_list_element *c, const char *value)
19300 {
19301 fprintf_filtered (file,
19302 _("Whether to always disassemble "
19303 "DWARF expressions is %s.\n"),
19304 value);
19305 }
19306
19307 static void
19308 show_check_physname (struct ui_file *file, int from_tty,
19309 struct cmd_list_element *c, const char *value)
19310 {
19311 fprintf_filtered (file,
19312 _("Whether to check \"physname\" is %s.\n"),
19313 value);
19314 }
19315
19316 void _initialize_dwarf2_read (void);
19317
19318 void
19319 _initialize_dwarf2_read (void)
19320 {
19321 struct cmd_list_element *c;
19322
19323 dwarf2_objfile_data_key
19324 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19325
19326 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19327 Set DWARF 2 specific variables.\n\
19328 Configure DWARF 2 variables such as the cache size"),
19329 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19330 0/*allow-unknown*/, &maintenance_set_cmdlist);
19331
19332 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19333 Show DWARF 2 specific variables\n\
19334 Show DWARF 2 variables such as the cache size"),
19335 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19336 0/*allow-unknown*/, &maintenance_show_cmdlist);
19337
19338 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19339 &dwarf2_max_cache_age, _("\
19340 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19341 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19342 A higher limit means that cached compilation units will be stored\n\
19343 in memory longer, and more total memory will be used. Zero disables\n\
19344 caching, which can slow down startup."),
19345 NULL,
19346 show_dwarf2_max_cache_age,
19347 &set_dwarf2_cmdlist,
19348 &show_dwarf2_cmdlist);
19349
19350 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19351 &dwarf2_always_disassemble, _("\
19352 Set whether `info address' always disassembles DWARF expressions."), _("\
19353 Show whether `info address' always disassembles DWARF expressions."), _("\
19354 When enabled, DWARF expressions are always printed in an assembly-like\n\
19355 syntax. When disabled, expressions will be printed in a more\n\
19356 conversational style, when possible."),
19357 NULL,
19358 show_dwarf2_always_disassemble,
19359 &set_dwarf2_cmdlist,
19360 &show_dwarf2_cmdlist);
19361
19362 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19363 Set debugging of the dwarf2 reader."), _("\
19364 Show debugging of the dwarf2 reader."), _("\
19365 When enabled, debugging messages are printed during dwarf2 reading\n\
19366 and symtab expansion."),
19367 NULL,
19368 NULL,
19369 &setdebuglist, &showdebuglist);
19370
19371 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19372 Set debugging of the dwarf2 DIE reader."), _("\
19373 Show debugging of the dwarf2 DIE reader."), _("\
19374 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19375 The value is the maximum depth to print."),
19376 NULL,
19377 NULL,
19378 &setdebuglist, &showdebuglist);
19379
19380 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19381 Set cross-checking of \"physname\" code against demangler."), _("\
19382 Show cross-checking of \"physname\" code against demangler."), _("\
19383 When enabled, GDB's internal \"physname\" code is checked against\n\
19384 the demangler."),
19385 NULL, show_check_physname,
19386 &setdebuglist, &showdebuglist);
19387
19388 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19389 _("\
19390 Save a gdb-index file.\n\
19391 Usage: save gdb-index DIRECTORY"),
19392 &save_cmdlist);
19393 set_cmd_completer (c, filename_completer);
19394 }