* dwarf2read.c (try_open_dwo_file): use gdb_bfd_open.
[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_open (absolute_name, gnutarget, desc);
8119 if (!sym_bfd)
8120 {
8121 xfree (absolute_name);
8122 return NULL;
8123 }
8124 gdb_bfd_stash_filename (sym_bfd);
8125 xfree (absolute_name);
8126 bfd_set_cacheable (sym_bfd, 1);
8127
8128 if (!bfd_check_format (sym_bfd, bfd_object))
8129 {
8130 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8131 return NULL;
8132 }
8133
8134 return sym_bfd;
8135 }
8136
8137 /* Try to open DWO file DWO_NAME.
8138 COMP_DIR is the DW_AT_comp_dir attribute.
8139 The result is the bfd handle of the file.
8140 If there is a problem finding or opening the file, return NULL.
8141 Upon success, the canonicalized path of the file is stored in the bfd,
8142 same as symfile_bfd_open. */
8143
8144 static bfd *
8145 open_dwo_file (const char *dwo_name, const char *comp_dir)
8146 {
8147 bfd *abfd;
8148
8149 if (IS_ABSOLUTE_PATH (dwo_name))
8150 return try_open_dwo_file (dwo_name);
8151
8152 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8153
8154 if (comp_dir != NULL)
8155 {
8156 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8157
8158 /* NOTE: If comp_dir is a relative path, this will also try the
8159 search path, which seems useful. */
8160 abfd = try_open_dwo_file (path_to_try);
8161 xfree (path_to_try);
8162 if (abfd != NULL)
8163 return abfd;
8164 }
8165
8166 /* That didn't work, try debug-file-directory, which, despite its name,
8167 is a list of paths. */
8168
8169 if (*debug_file_directory == '\0')
8170 return NULL;
8171
8172 return try_open_dwo_file (dwo_name);
8173 }
8174
8175 /* Initialize the use of the DWO file specified by DWO_NAME. */
8176
8177 static struct dwo_file *
8178 init_dwo_file (const char *dwo_name, const char *comp_dir)
8179 {
8180 struct objfile *objfile = dwarf2_per_objfile->objfile;
8181 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8182 struct dwo_file);
8183 bfd *abfd;
8184 struct cleanup *cleanups;
8185
8186 if (dwarf2_read_debug)
8187 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8188
8189 abfd = open_dwo_file (dwo_name, comp_dir);
8190 if (abfd == NULL)
8191 return NULL;
8192 dwo_file->dwo_name = dwo_name;
8193 dwo_file->dwo_bfd = abfd;
8194
8195 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8196
8197 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8198
8199 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8200
8201 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8202 dwo_file->sections.types);
8203
8204 discard_cleanups (cleanups);
8205
8206 return dwo_file;
8207 }
8208
8209 /* Lookup DWO file DWO_NAME. */
8210
8211 static struct dwo_file *
8212 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8213 {
8214 struct dwo_file *dwo_file;
8215 struct dwo_file find_entry;
8216 void **slot;
8217
8218 if (dwarf2_per_objfile->dwo_files == NULL)
8219 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8220
8221 /* Have we already seen this DWO file? */
8222 find_entry.dwo_name = dwo_name;
8223 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8224
8225 /* If not, read it in and build a table of the DWOs it contains. */
8226 if (*slot == NULL)
8227 *slot = init_dwo_file (dwo_name, comp_dir);
8228
8229 /* NOTE: This will be NULL if unable to open the file. */
8230 dwo_file = *slot;
8231
8232 return dwo_file;
8233 }
8234
8235 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8236 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8237 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8238 nomenclature as TUs).
8239 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8240 (dwo_id mismatch or couldn't find the DWO file). */
8241
8242 static struct dwo_unit *
8243 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8244 char *dwo_name, const char *comp_dir,
8245 ULONGEST signature)
8246 {
8247 struct objfile *objfile = dwarf2_per_objfile->objfile;
8248 struct dwo_file *dwo_file;
8249
8250 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8251 if (dwo_file == NULL)
8252 return NULL;
8253
8254 /* Look up the DWO using its signature(dwo_id). */
8255
8256 if (dwo_file->cus != NULL)
8257 {
8258 struct dwo_unit find_dwo_cu, *dwo_cu;
8259
8260 find_dwo_cu.signature = signature;
8261 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8262
8263 if (dwo_cu != NULL)
8264 return dwo_cu;
8265 }
8266
8267 /* We didn't find it. This must mean a dwo_id mismatch. */
8268
8269 complaint (&symfile_complaints,
8270 _("Could not find DWO CU referenced by CU at offset 0x%x"
8271 " [in module %s]"),
8272 this_cu->offset.sect_off, objfile->name);
8273 return NULL;
8274 }
8275
8276 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8277 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8278 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8279 (dwo_id mismatch or couldn't find the DWO file). */
8280
8281 static struct dwo_unit *
8282 lookup_dwo_type_unit (struct signatured_type *this_tu,
8283 char *dwo_name, const char *comp_dir)
8284 {
8285 struct objfile *objfile = dwarf2_per_objfile->objfile;
8286 struct dwo_file *dwo_file;
8287
8288 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8289 if (dwo_file == NULL)
8290 return NULL;
8291
8292 /* Look up the DWO using its signature(dwo_id). */
8293
8294 if (dwo_file->tus != NULL)
8295 {
8296 struct dwo_unit find_dwo_tu, *dwo_tu;
8297
8298 find_dwo_tu.signature = this_tu->signature;
8299 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8300
8301 if (dwo_tu != NULL)
8302 return dwo_tu;
8303 }
8304
8305 /* We didn't find it. This must mean a dwo_id mismatch. */
8306
8307 complaint (&symfile_complaints,
8308 _("Could not find DWO TU referenced by TU at offset 0x%x"
8309 " [in module %s]"),
8310 this_tu->per_cu.offset.sect_off, objfile->name);
8311 return NULL;
8312 }
8313
8314 /* Free all resources associated with DWO_FILE.
8315 Close the DWO file and munmap the sections.
8316 All memory should be on the objfile obstack. */
8317
8318 static void
8319 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8320 {
8321 int ix;
8322 struct dwarf2_section_info *section;
8323
8324 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8325 gdb_bfd_unref (dwo_file->dwo_bfd);
8326
8327 munmap_section_buffer (&dwo_file->sections.abbrev);
8328 munmap_section_buffer (&dwo_file->sections.info);
8329 munmap_section_buffer (&dwo_file->sections.line);
8330 munmap_section_buffer (&dwo_file->sections.loc);
8331 munmap_section_buffer (&dwo_file->sections.str);
8332 munmap_section_buffer (&dwo_file->sections.str_offsets);
8333
8334 for (ix = 0;
8335 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
8336 ix, section);
8337 ++ix)
8338 munmap_section_buffer (section);
8339
8340 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8341 }
8342
8343 /* Wrapper for free_dwo_file for use in cleanups. */
8344
8345 static void
8346 free_dwo_file_cleanup (void *arg)
8347 {
8348 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8349 struct objfile *objfile = dwarf2_per_objfile->objfile;
8350
8351 free_dwo_file (dwo_file, objfile);
8352 }
8353
8354 /* Traversal function for free_dwo_files. */
8355
8356 static int
8357 free_dwo_file_from_slot (void **slot, void *info)
8358 {
8359 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8360 struct objfile *objfile = (struct objfile *) info;
8361
8362 free_dwo_file (dwo_file, objfile);
8363
8364 return 1;
8365 }
8366
8367 /* Free all resources associated with DWO_FILES. */
8368
8369 static void
8370 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8371 {
8372 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8373 }
8374 \f
8375 /* Read in various DIEs. */
8376
8377 /* qsort helper for inherit_abstract_dies. */
8378
8379 static int
8380 unsigned_int_compar (const void *ap, const void *bp)
8381 {
8382 unsigned int a = *(unsigned int *) ap;
8383 unsigned int b = *(unsigned int *) bp;
8384
8385 return (a > b) - (b > a);
8386 }
8387
8388 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8389 Inherit only the children of the DW_AT_abstract_origin DIE not being
8390 already referenced by DW_AT_abstract_origin from the children of the
8391 current DIE. */
8392
8393 static void
8394 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8395 {
8396 struct die_info *child_die;
8397 unsigned die_children_count;
8398 /* CU offsets which were referenced by children of the current DIE. */
8399 sect_offset *offsets;
8400 sect_offset *offsets_end, *offsetp;
8401 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8402 struct die_info *origin_die;
8403 /* Iterator of the ORIGIN_DIE children. */
8404 struct die_info *origin_child_die;
8405 struct cleanup *cleanups;
8406 struct attribute *attr;
8407 struct dwarf2_cu *origin_cu;
8408 struct pending **origin_previous_list_in_scope;
8409
8410 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8411 if (!attr)
8412 return;
8413
8414 /* Note that following die references may follow to a die in a
8415 different cu. */
8416
8417 origin_cu = cu;
8418 origin_die = follow_die_ref (die, attr, &origin_cu);
8419
8420 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8421 symbols in. */
8422 origin_previous_list_in_scope = origin_cu->list_in_scope;
8423 origin_cu->list_in_scope = cu->list_in_scope;
8424
8425 if (die->tag != origin_die->tag
8426 && !(die->tag == DW_TAG_inlined_subroutine
8427 && origin_die->tag == DW_TAG_subprogram))
8428 complaint (&symfile_complaints,
8429 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8430 die->offset.sect_off, origin_die->offset.sect_off);
8431
8432 child_die = die->child;
8433 die_children_count = 0;
8434 while (child_die && child_die->tag)
8435 {
8436 child_die = sibling_die (child_die);
8437 die_children_count++;
8438 }
8439 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8440 cleanups = make_cleanup (xfree, offsets);
8441
8442 offsets_end = offsets;
8443 child_die = die->child;
8444 while (child_die && child_die->tag)
8445 {
8446 /* For each CHILD_DIE, find the corresponding child of
8447 ORIGIN_DIE. If there is more than one layer of
8448 DW_AT_abstract_origin, follow them all; there shouldn't be,
8449 but GCC versions at least through 4.4 generate this (GCC PR
8450 40573). */
8451 struct die_info *child_origin_die = child_die;
8452 struct dwarf2_cu *child_origin_cu = cu;
8453
8454 while (1)
8455 {
8456 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8457 child_origin_cu);
8458 if (attr == NULL)
8459 break;
8460 child_origin_die = follow_die_ref (child_origin_die, attr,
8461 &child_origin_cu);
8462 }
8463
8464 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8465 counterpart may exist. */
8466 if (child_origin_die != child_die)
8467 {
8468 if (child_die->tag != child_origin_die->tag
8469 && !(child_die->tag == DW_TAG_inlined_subroutine
8470 && child_origin_die->tag == DW_TAG_subprogram))
8471 complaint (&symfile_complaints,
8472 _("Child DIE 0x%x and its abstract origin 0x%x have "
8473 "different tags"), child_die->offset.sect_off,
8474 child_origin_die->offset.sect_off);
8475 if (child_origin_die->parent != origin_die)
8476 complaint (&symfile_complaints,
8477 _("Child DIE 0x%x and its abstract origin 0x%x have "
8478 "different parents"), child_die->offset.sect_off,
8479 child_origin_die->offset.sect_off);
8480 else
8481 *offsets_end++ = child_origin_die->offset;
8482 }
8483 child_die = sibling_die (child_die);
8484 }
8485 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8486 unsigned_int_compar);
8487 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8488 if (offsetp[-1].sect_off == offsetp->sect_off)
8489 complaint (&symfile_complaints,
8490 _("Multiple children of DIE 0x%x refer "
8491 "to DIE 0x%x as their abstract origin"),
8492 die->offset.sect_off, offsetp->sect_off);
8493
8494 offsetp = offsets;
8495 origin_child_die = origin_die->child;
8496 while (origin_child_die && origin_child_die->tag)
8497 {
8498 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
8499 while (offsetp < offsets_end
8500 && offsetp->sect_off < origin_child_die->offset.sect_off)
8501 offsetp++;
8502 if (offsetp >= offsets_end
8503 || offsetp->sect_off > origin_child_die->offset.sect_off)
8504 {
8505 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
8506 process_die (origin_child_die, origin_cu);
8507 }
8508 origin_child_die = sibling_die (origin_child_die);
8509 }
8510 origin_cu->list_in_scope = origin_previous_list_in_scope;
8511
8512 do_cleanups (cleanups);
8513 }
8514
8515 static void
8516 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8517 {
8518 struct objfile *objfile = cu->objfile;
8519 struct context_stack *new;
8520 CORE_ADDR lowpc;
8521 CORE_ADDR highpc;
8522 struct die_info *child_die;
8523 struct attribute *attr, *call_line, *call_file;
8524 char *name;
8525 CORE_ADDR baseaddr;
8526 struct block *block;
8527 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8528 VEC (symbolp) *template_args = NULL;
8529 struct template_symbol *templ_func = NULL;
8530
8531 if (inlined_func)
8532 {
8533 /* If we do not have call site information, we can't show the
8534 caller of this inlined function. That's too confusing, so
8535 only use the scope for local variables. */
8536 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8537 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8538 if (call_line == NULL || call_file == NULL)
8539 {
8540 read_lexical_block_scope (die, cu);
8541 return;
8542 }
8543 }
8544
8545 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8546
8547 name = dwarf2_name (die, cu);
8548
8549 /* Ignore functions with missing or empty names. These are actually
8550 illegal according to the DWARF standard. */
8551 if (name == NULL)
8552 {
8553 complaint (&symfile_complaints,
8554 _("missing name for subprogram DIE at %d"),
8555 die->offset.sect_off);
8556 return;
8557 }
8558
8559 /* Ignore functions with missing or invalid low and high pc attributes. */
8560 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8561 {
8562 attr = dwarf2_attr (die, DW_AT_external, cu);
8563 if (!attr || !DW_UNSND (attr))
8564 complaint (&symfile_complaints,
8565 _("cannot get low and high bounds "
8566 "for subprogram DIE at %d"),
8567 die->offset.sect_off);
8568 return;
8569 }
8570
8571 lowpc += baseaddr;
8572 highpc += baseaddr;
8573
8574 /* If we have any template arguments, then we must allocate a
8575 different sort of symbol. */
8576 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8577 {
8578 if (child_die->tag == DW_TAG_template_type_param
8579 || child_die->tag == DW_TAG_template_value_param)
8580 {
8581 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8582 struct template_symbol);
8583 templ_func->base.is_cplus_template_function = 1;
8584 break;
8585 }
8586 }
8587
8588 new = push_context (0, lowpc);
8589 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8590 (struct symbol *) templ_func);
8591
8592 /* If there is a location expression for DW_AT_frame_base, record
8593 it. */
8594 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8595 if (attr)
8596 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8597 expression is being recorded directly in the function's symbol
8598 and not in a separate frame-base object. I guess this hack is
8599 to avoid adding some sort of frame-base adjunct/annex to the
8600 function's symbol :-(. The problem with doing this is that it
8601 results in a function symbol with a location expression that
8602 has nothing to do with the location of the function, ouch! The
8603 relationship should be: a function's symbol has-a frame base; a
8604 frame-base has-a location expression. */
8605 dwarf2_symbol_mark_computed (attr, new->name, cu);
8606
8607 cu->list_in_scope = &local_symbols;
8608
8609 if (die->child != NULL)
8610 {
8611 child_die = die->child;
8612 while (child_die && child_die->tag)
8613 {
8614 if (child_die->tag == DW_TAG_template_type_param
8615 || child_die->tag == DW_TAG_template_value_param)
8616 {
8617 struct symbol *arg = new_symbol (child_die, NULL, cu);
8618
8619 if (arg != NULL)
8620 VEC_safe_push (symbolp, template_args, arg);
8621 }
8622 else
8623 process_die (child_die, cu);
8624 child_die = sibling_die (child_die);
8625 }
8626 }
8627
8628 inherit_abstract_dies (die, cu);
8629
8630 /* If we have a DW_AT_specification, we might need to import using
8631 directives from the context of the specification DIE. See the
8632 comment in determine_prefix. */
8633 if (cu->language == language_cplus
8634 && dwarf2_attr (die, DW_AT_specification, cu))
8635 {
8636 struct dwarf2_cu *spec_cu = cu;
8637 struct die_info *spec_die = die_specification (die, &spec_cu);
8638
8639 while (spec_die)
8640 {
8641 child_die = spec_die->child;
8642 while (child_die && child_die->tag)
8643 {
8644 if (child_die->tag == DW_TAG_imported_module)
8645 process_die (child_die, spec_cu);
8646 child_die = sibling_die (child_die);
8647 }
8648
8649 /* In some cases, GCC generates specification DIEs that
8650 themselves contain DW_AT_specification attributes. */
8651 spec_die = die_specification (spec_die, &spec_cu);
8652 }
8653 }
8654
8655 new = pop_context ();
8656 /* Make a block for the local symbols within. */
8657 block = finish_block (new->name, &local_symbols, new->old_blocks,
8658 lowpc, highpc, objfile);
8659
8660 /* For C++, set the block's scope. */
8661 if (cu->language == language_cplus || cu->language == language_fortran)
8662 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8663 determine_prefix (die, cu),
8664 processing_has_namespace_info);
8665
8666 /* If we have address ranges, record them. */
8667 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8668
8669 /* Attach template arguments to function. */
8670 if (! VEC_empty (symbolp, template_args))
8671 {
8672 gdb_assert (templ_func != NULL);
8673
8674 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8675 templ_func->template_arguments
8676 = obstack_alloc (&objfile->objfile_obstack,
8677 (templ_func->n_template_arguments
8678 * sizeof (struct symbol *)));
8679 memcpy (templ_func->template_arguments,
8680 VEC_address (symbolp, template_args),
8681 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8682 VEC_free (symbolp, template_args);
8683 }
8684
8685 /* In C++, we can have functions nested inside functions (e.g., when
8686 a function declares a class that has methods). This means that
8687 when we finish processing a function scope, we may need to go
8688 back to building a containing block's symbol lists. */
8689 local_symbols = new->locals;
8690 param_symbols = new->params;
8691 using_directives = new->using_directives;
8692
8693 /* If we've finished processing a top-level function, subsequent
8694 symbols go in the file symbol list. */
8695 if (outermost_context_p ())
8696 cu->list_in_scope = &file_symbols;
8697 }
8698
8699 /* Process all the DIES contained within a lexical block scope. Start
8700 a new scope, process the dies, and then close the scope. */
8701
8702 static void
8703 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8704 {
8705 struct objfile *objfile = cu->objfile;
8706 struct context_stack *new;
8707 CORE_ADDR lowpc, highpc;
8708 struct die_info *child_die;
8709 CORE_ADDR baseaddr;
8710
8711 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8712
8713 /* Ignore blocks with missing or invalid low and high pc attributes. */
8714 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8715 as multiple lexical blocks? Handling children in a sane way would
8716 be nasty. Might be easier to properly extend generic blocks to
8717 describe ranges. */
8718 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8719 return;
8720 lowpc += baseaddr;
8721 highpc += baseaddr;
8722
8723 push_context (0, lowpc);
8724 if (die->child != NULL)
8725 {
8726 child_die = die->child;
8727 while (child_die && child_die->tag)
8728 {
8729 process_die (child_die, cu);
8730 child_die = sibling_die (child_die);
8731 }
8732 }
8733 new = pop_context ();
8734
8735 if (local_symbols != NULL || using_directives != NULL)
8736 {
8737 struct block *block
8738 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8739 highpc, objfile);
8740
8741 /* Note that recording ranges after traversing children, as we
8742 do here, means that recording a parent's ranges entails
8743 walking across all its children's ranges as they appear in
8744 the address map, which is quadratic behavior.
8745
8746 It would be nicer to record the parent's ranges before
8747 traversing its children, simply overriding whatever you find
8748 there. But since we don't even decide whether to create a
8749 block until after we've traversed its children, that's hard
8750 to do. */
8751 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8752 }
8753 local_symbols = new->locals;
8754 using_directives = new->using_directives;
8755 }
8756
8757 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8758
8759 static void
8760 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8761 {
8762 struct objfile *objfile = cu->objfile;
8763 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8764 CORE_ADDR pc, baseaddr;
8765 struct attribute *attr;
8766 struct call_site *call_site, call_site_local;
8767 void **slot;
8768 int nparams;
8769 struct die_info *child_die;
8770
8771 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8772
8773 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8774 if (!attr)
8775 {
8776 complaint (&symfile_complaints,
8777 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8778 "DIE 0x%x [in module %s]"),
8779 die->offset.sect_off, objfile->name);
8780 return;
8781 }
8782 pc = DW_ADDR (attr) + baseaddr;
8783
8784 if (cu->call_site_htab == NULL)
8785 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8786 NULL, &objfile->objfile_obstack,
8787 hashtab_obstack_allocate, NULL);
8788 call_site_local.pc = pc;
8789 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8790 if (*slot != NULL)
8791 {
8792 complaint (&symfile_complaints,
8793 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8794 "DIE 0x%x [in module %s]"),
8795 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8796 return;
8797 }
8798
8799 /* Count parameters at the caller. */
8800
8801 nparams = 0;
8802 for (child_die = die->child; child_die && child_die->tag;
8803 child_die = sibling_die (child_die))
8804 {
8805 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8806 {
8807 complaint (&symfile_complaints,
8808 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8810 child_die->tag, child_die->offset.sect_off, objfile->name);
8811 continue;
8812 }
8813
8814 nparams++;
8815 }
8816
8817 call_site = obstack_alloc (&objfile->objfile_obstack,
8818 (sizeof (*call_site)
8819 + (sizeof (*call_site->parameter)
8820 * (nparams - 1))));
8821 *slot = call_site;
8822 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8823 call_site->pc = pc;
8824
8825 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8826 {
8827 struct die_info *func_die;
8828
8829 /* Skip also over DW_TAG_inlined_subroutine. */
8830 for (func_die = die->parent;
8831 func_die && func_die->tag != DW_TAG_subprogram
8832 && func_die->tag != DW_TAG_subroutine_type;
8833 func_die = func_die->parent);
8834
8835 /* DW_AT_GNU_all_call_sites is a superset
8836 of DW_AT_GNU_all_tail_call_sites. */
8837 if (func_die
8838 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8839 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8840 {
8841 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8842 not complete. But keep CALL_SITE for look ups via call_site_htab,
8843 both the initial caller containing the real return address PC and
8844 the final callee containing the current PC of a chain of tail
8845 calls do not need to have the tail call list complete. But any
8846 function candidate for a virtual tail call frame searched via
8847 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8848 determined unambiguously. */
8849 }
8850 else
8851 {
8852 struct type *func_type = NULL;
8853
8854 if (func_die)
8855 func_type = get_die_type (func_die, cu);
8856 if (func_type != NULL)
8857 {
8858 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
8859
8860 /* Enlist this call site to the function. */
8861 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
8862 TYPE_TAIL_CALL_LIST (func_type) = call_site;
8863 }
8864 else
8865 complaint (&symfile_complaints,
8866 _("Cannot find function owning DW_TAG_GNU_call_site "
8867 "DIE 0x%x [in module %s]"),
8868 die->offset.sect_off, objfile->name);
8869 }
8870 }
8871
8872 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
8873 if (attr == NULL)
8874 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8875 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
8876 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
8877 /* Keep NULL DWARF_BLOCK. */;
8878 else if (attr_form_is_block (attr))
8879 {
8880 struct dwarf2_locexpr_baton *dlbaton;
8881
8882 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
8883 dlbaton->data = DW_BLOCK (attr)->data;
8884 dlbaton->size = DW_BLOCK (attr)->size;
8885 dlbaton->per_cu = cu->per_cu;
8886
8887 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
8888 }
8889 else if (is_ref_attr (attr))
8890 {
8891 struct dwarf2_cu *target_cu = cu;
8892 struct die_info *target_die;
8893
8894 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
8895 gdb_assert (target_cu->objfile == objfile);
8896 if (die_is_declaration (target_die, target_cu))
8897 {
8898 const char *target_physname;
8899
8900 target_physname = dwarf2_physname (NULL, target_die, target_cu);
8901 if (target_physname == NULL)
8902 complaint (&symfile_complaints,
8903 _("DW_AT_GNU_call_site_target target DIE has invalid "
8904 "physname, for referencing DIE 0x%x [in module %s]"),
8905 die->offset.sect_off, objfile->name);
8906 else
8907 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
8908 }
8909 else
8910 {
8911 CORE_ADDR lowpc;
8912
8913 /* DW_AT_entry_pc should be preferred. */
8914 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
8915 complaint (&symfile_complaints,
8916 _("DW_AT_GNU_call_site_target target DIE has invalid "
8917 "low pc, for referencing DIE 0x%x [in module %s]"),
8918 die->offset.sect_off, objfile->name);
8919 else
8920 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
8921 }
8922 }
8923 else
8924 complaint (&symfile_complaints,
8925 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
8926 "block nor reference, for DIE 0x%x [in module %s]"),
8927 die->offset.sect_off, objfile->name);
8928
8929 call_site->per_cu = cu->per_cu;
8930
8931 for (child_die = die->child;
8932 child_die && child_die->tag;
8933 child_die = sibling_die (child_die))
8934 {
8935 struct call_site_parameter *parameter;
8936 struct attribute *loc, *origin;
8937
8938 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8939 {
8940 /* Already printed the complaint above. */
8941 continue;
8942 }
8943
8944 gdb_assert (call_site->parameter_count < nparams);
8945 parameter = &call_site->parameter[call_site->parameter_count];
8946
8947 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8948 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8949 register is contained in DW_AT_GNU_call_site_value. */
8950
8951 loc = dwarf2_attr (child_die, DW_AT_location, cu);
8952 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8953 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8954 {
8955 sect_offset offset;
8956
8957 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8958 offset = dwarf2_get_ref_die_offset (origin);
8959 if (!offset_in_cu_p (&cu->header, offset))
8960 {
8961 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
8962 binding can be done only inside one CU. Such referenced DIE
8963 therefore cannot be even moved to DW_TAG_partial_unit. */
8964 complaint (&symfile_complaints,
8965 _("DW_AT_abstract_origin offset is not in CU for "
8966 "DW_TAG_GNU_call_site child DIE 0x%x "
8967 "[in module %s]"),
8968 child_die->offset.sect_off, objfile->name);
8969 continue;
8970 }
8971 parameter->u.param_offset.cu_off = (offset.sect_off
8972 - cu->header.offset.sect_off);
8973 }
8974 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
8975 {
8976 complaint (&symfile_complaints,
8977 _("No DW_FORM_block* DW_AT_location for "
8978 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8979 child_die->offset.sect_off, objfile->name);
8980 continue;
8981 }
8982 else
8983 {
8984 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8985 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8986 if (parameter->u.dwarf_reg != -1)
8987 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8988 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8989 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8990 &parameter->u.fb_offset))
8991 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8992 else
8993 {
8994 complaint (&symfile_complaints,
8995 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8996 "for DW_FORM_block* DW_AT_location is supported for "
8997 "DW_TAG_GNU_call_site child DIE 0x%x "
8998 "[in module %s]"),
8999 child_die->offset.sect_off, objfile->name);
9000 continue;
9001 }
9002 }
9003
9004 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9005 if (!attr_form_is_block (attr))
9006 {
9007 complaint (&symfile_complaints,
9008 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9009 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9010 child_die->offset.sect_off, objfile->name);
9011 continue;
9012 }
9013 parameter->value = DW_BLOCK (attr)->data;
9014 parameter->value_size = DW_BLOCK (attr)->size;
9015
9016 /* Parameters are not pre-cleared by memset above. */
9017 parameter->data_value = NULL;
9018 parameter->data_value_size = 0;
9019 call_site->parameter_count++;
9020
9021 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9022 if (attr)
9023 {
9024 if (!attr_form_is_block (attr))
9025 complaint (&symfile_complaints,
9026 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9027 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9028 child_die->offset.sect_off, objfile->name);
9029 else
9030 {
9031 parameter->data_value = DW_BLOCK (attr)->data;
9032 parameter->data_value_size = DW_BLOCK (attr)->size;
9033 }
9034 }
9035 }
9036 }
9037
9038 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9039 Return 1 if the attributes are present and valid, otherwise, return 0.
9040 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9041
9042 static int
9043 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9044 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9045 struct partial_symtab *ranges_pst)
9046 {
9047 struct objfile *objfile = cu->objfile;
9048 struct comp_unit_head *cu_header = &cu->header;
9049 bfd *obfd = objfile->obfd;
9050 unsigned int addr_size = cu_header->addr_size;
9051 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9052 /* Base address selection entry. */
9053 CORE_ADDR base;
9054 int found_base;
9055 unsigned int dummy;
9056 gdb_byte *buffer;
9057 CORE_ADDR marker;
9058 int low_set;
9059 CORE_ADDR low = 0;
9060 CORE_ADDR high = 0;
9061 CORE_ADDR baseaddr;
9062
9063 found_base = cu->base_known;
9064 base = cu->base_address;
9065
9066 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9067 if (offset >= dwarf2_per_objfile->ranges.size)
9068 {
9069 complaint (&symfile_complaints,
9070 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9071 offset);
9072 return 0;
9073 }
9074 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9075
9076 /* Read in the largest possible address. */
9077 marker = read_address (obfd, buffer, cu, &dummy);
9078 if ((marker & mask) == mask)
9079 {
9080 /* If we found the largest possible address, then
9081 read the base address. */
9082 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9083 buffer += 2 * addr_size;
9084 offset += 2 * addr_size;
9085 found_base = 1;
9086 }
9087
9088 low_set = 0;
9089
9090 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9091
9092 while (1)
9093 {
9094 CORE_ADDR range_beginning, range_end;
9095
9096 range_beginning = read_address (obfd, buffer, cu, &dummy);
9097 buffer += addr_size;
9098 range_end = read_address (obfd, buffer, cu, &dummy);
9099 buffer += addr_size;
9100 offset += 2 * addr_size;
9101
9102 /* An end of list marker is a pair of zero addresses. */
9103 if (range_beginning == 0 && range_end == 0)
9104 /* Found the end of list entry. */
9105 break;
9106
9107 /* Each base address selection entry is a pair of 2 values.
9108 The first is the largest possible address, the second is
9109 the base address. Check for a base address here. */
9110 if ((range_beginning & mask) == mask)
9111 {
9112 /* If we found the largest possible address, then
9113 read the base address. */
9114 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9115 found_base = 1;
9116 continue;
9117 }
9118
9119 if (!found_base)
9120 {
9121 /* We have no valid base address for the ranges
9122 data. */
9123 complaint (&symfile_complaints,
9124 _("Invalid .debug_ranges data (no base address)"));
9125 return 0;
9126 }
9127
9128 if (range_beginning > range_end)
9129 {
9130 /* Inverted range entries are invalid. */
9131 complaint (&symfile_complaints,
9132 _("Invalid .debug_ranges data (inverted range)"));
9133 return 0;
9134 }
9135
9136 /* Empty range entries have no effect. */
9137 if (range_beginning == range_end)
9138 continue;
9139
9140 range_beginning += base;
9141 range_end += base;
9142
9143 if (ranges_pst != NULL)
9144 addrmap_set_empty (objfile->psymtabs_addrmap,
9145 range_beginning + baseaddr,
9146 range_end - 1 + baseaddr,
9147 ranges_pst);
9148
9149 /* FIXME: This is recording everything as a low-high
9150 segment of consecutive addresses. We should have a
9151 data structure for discontiguous block ranges
9152 instead. */
9153 if (! low_set)
9154 {
9155 low = range_beginning;
9156 high = range_end;
9157 low_set = 1;
9158 }
9159 else
9160 {
9161 if (range_beginning < low)
9162 low = range_beginning;
9163 if (range_end > high)
9164 high = range_end;
9165 }
9166 }
9167
9168 if (! low_set)
9169 /* If the first entry is an end-of-list marker, the range
9170 describes an empty scope, i.e. no instructions. */
9171 return 0;
9172
9173 if (low_return)
9174 *low_return = low;
9175 if (high_return)
9176 *high_return = high;
9177 return 1;
9178 }
9179
9180 /* Get low and high pc attributes from a die. Return 1 if the attributes
9181 are present and valid, otherwise, return 0. Return -1 if the range is
9182 discontinuous, i.e. derived from DW_AT_ranges information. */
9183
9184 static int
9185 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9186 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9187 struct partial_symtab *pst)
9188 {
9189 struct attribute *attr;
9190 struct attribute *attr_high;
9191 CORE_ADDR low = 0;
9192 CORE_ADDR high = 0;
9193 int ret = 0;
9194
9195 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9196 if (attr_high)
9197 {
9198 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9199 if (attr)
9200 {
9201 low = DW_ADDR (attr);
9202 if (attr_high->form == DW_FORM_addr
9203 || attr_high->form == DW_FORM_GNU_addr_index)
9204 high = DW_ADDR (attr_high);
9205 else
9206 high = low + DW_UNSND (attr_high);
9207 }
9208 else
9209 /* Found high w/o low attribute. */
9210 return 0;
9211
9212 /* Found consecutive range of addresses. */
9213 ret = 1;
9214 }
9215 else
9216 {
9217 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9218 if (attr != NULL)
9219 {
9220 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9221
9222 /* Value of the DW_AT_ranges attribute is the offset in the
9223 .debug_ranges section. */
9224 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9225 return 0;
9226 /* Found discontinuous range of addresses. */
9227 ret = -1;
9228 }
9229 }
9230
9231 /* read_partial_die has also the strict LOW < HIGH requirement. */
9232 if (high <= low)
9233 return 0;
9234
9235 /* When using the GNU linker, .gnu.linkonce. sections are used to
9236 eliminate duplicate copies of functions and vtables and such.
9237 The linker will arbitrarily choose one and discard the others.
9238 The AT_*_pc values for such functions refer to local labels in
9239 these sections. If the section from that file was discarded, the
9240 labels are not in the output, so the relocs get a value of 0.
9241 If this is a discarded function, mark the pc bounds as invalid,
9242 so that GDB will ignore it. */
9243 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9244 return 0;
9245
9246 *lowpc = low;
9247 if (highpc)
9248 *highpc = high;
9249 return ret;
9250 }
9251
9252 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9253 its low and high PC addresses. Do nothing if these addresses could not
9254 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9255 and HIGHPC to the high address if greater than HIGHPC. */
9256
9257 static void
9258 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9259 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9260 struct dwarf2_cu *cu)
9261 {
9262 CORE_ADDR low, high;
9263 struct die_info *child = die->child;
9264
9265 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9266 {
9267 *lowpc = min (*lowpc, low);
9268 *highpc = max (*highpc, high);
9269 }
9270
9271 /* If the language does not allow nested subprograms (either inside
9272 subprograms or lexical blocks), we're done. */
9273 if (cu->language != language_ada)
9274 return;
9275
9276 /* Check all the children of the given DIE. If it contains nested
9277 subprograms, then check their pc bounds. Likewise, we need to
9278 check lexical blocks as well, as they may also contain subprogram
9279 definitions. */
9280 while (child && child->tag)
9281 {
9282 if (child->tag == DW_TAG_subprogram
9283 || child->tag == DW_TAG_lexical_block)
9284 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9285 child = sibling_die (child);
9286 }
9287 }
9288
9289 /* Get the low and high pc's represented by the scope DIE, and store
9290 them in *LOWPC and *HIGHPC. If the correct values can't be
9291 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9292
9293 static void
9294 get_scope_pc_bounds (struct die_info *die,
9295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9296 struct dwarf2_cu *cu)
9297 {
9298 CORE_ADDR best_low = (CORE_ADDR) -1;
9299 CORE_ADDR best_high = (CORE_ADDR) 0;
9300 CORE_ADDR current_low, current_high;
9301
9302 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9303 {
9304 best_low = current_low;
9305 best_high = current_high;
9306 }
9307 else
9308 {
9309 struct die_info *child = die->child;
9310
9311 while (child && child->tag)
9312 {
9313 switch (child->tag) {
9314 case DW_TAG_subprogram:
9315 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9316 break;
9317 case DW_TAG_namespace:
9318 case DW_TAG_module:
9319 /* FIXME: carlton/2004-01-16: Should we do this for
9320 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9321 that current GCC's always emit the DIEs corresponding
9322 to definitions of methods of classes as children of a
9323 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9324 the DIEs giving the declarations, which could be
9325 anywhere). But I don't see any reason why the
9326 standards says that they have to be there. */
9327 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9328
9329 if (current_low != ((CORE_ADDR) -1))
9330 {
9331 best_low = min (best_low, current_low);
9332 best_high = max (best_high, current_high);
9333 }
9334 break;
9335 default:
9336 /* Ignore. */
9337 break;
9338 }
9339
9340 child = sibling_die (child);
9341 }
9342 }
9343
9344 *lowpc = best_low;
9345 *highpc = best_high;
9346 }
9347
9348 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9349 in DIE. */
9350
9351 static void
9352 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9353 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9354 {
9355 struct objfile *objfile = cu->objfile;
9356 struct attribute *attr;
9357 struct attribute *attr_high;
9358
9359 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9360 if (attr_high)
9361 {
9362 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9363 if (attr)
9364 {
9365 CORE_ADDR low = DW_ADDR (attr);
9366 CORE_ADDR high;
9367 if (attr_high->form == DW_FORM_addr
9368 || attr_high->form == DW_FORM_GNU_addr_index)
9369 high = DW_ADDR (attr_high);
9370 else
9371 high = low + DW_UNSND (attr_high);
9372
9373 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9374 }
9375 }
9376
9377 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9378 if (attr)
9379 {
9380 bfd *obfd = objfile->obfd;
9381
9382 /* The value of the DW_AT_ranges attribute is the offset of the
9383 address range list in the .debug_ranges section. */
9384 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9385 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9386
9387 /* For some target architectures, but not others, the
9388 read_address function sign-extends the addresses it returns.
9389 To recognize base address selection entries, we need a
9390 mask. */
9391 unsigned int addr_size = cu->header.addr_size;
9392 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9393
9394 /* The base address, to which the next pair is relative. Note
9395 that this 'base' is a DWARF concept: most entries in a range
9396 list are relative, to reduce the number of relocs against the
9397 debugging information. This is separate from this function's
9398 'baseaddr' argument, which GDB uses to relocate debugging
9399 information from a shared library based on the address at
9400 which the library was loaded. */
9401 CORE_ADDR base = cu->base_address;
9402 int base_known = cu->base_known;
9403
9404 gdb_assert (dwarf2_per_objfile->ranges.readin);
9405 if (offset >= dwarf2_per_objfile->ranges.size)
9406 {
9407 complaint (&symfile_complaints,
9408 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9409 offset);
9410 return;
9411 }
9412
9413 for (;;)
9414 {
9415 unsigned int bytes_read;
9416 CORE_ADDR start, end;
9417
9418 start = read_address (obfd, buffer, cu, &bytes_read);
9419 buffer += bytes_read;
9420 end = read_address (obfd, buffer, cu, &bytes_read);
9421 buffer += bytes_read;
9422
9423 /* Did we find the end of the range list? */
9424 if (start == 0 && end == 0)
9425 break;
9426
9427 /* Did we find a base address selection entry? */
9428 else if ((start & base_select_mask) == base_select_mask)
9429 {
9430 base = end;
9431 base_known = 1;
9432 }
9433
9434 /* We found an ordinary address range. */
9435 else
9436 {
9437 if (!base_known)
9438 {
9439 complaint (&symfile_complaints,
9440 _("Invalid .debug_ranges data "
9441 "(no base address)"));
9442 return;
9443 }
9444
9445 if (start > end)
9446 {
9447 /* Inverted range entries are invalid. */
9448 complaint (&symfile_complaints,
9449 _("Invalid .debug_ranges data "
9450 "(inverted range)"));
9451 return;
9452 }
9453
9454 /* Empty range entries have no effect. */
9455 if (start == end)
9456 continue;
9457
9458 record_block_range (block,
9459 baseaddr + base + start,
9460 baseaddr + base + end - 1);
9461 }
9462 }
9463 }
9464 }
9465
9466 /* Check whether the producer field indicates either of GCC < 4.6, or the
9467 Intel C/C++ compiler, and cache the result in CU. */
9468
9469 static void
9470 check_producer (struct dwarf2_cu *cu)
9471 {
9472 const char *cs;
9473 int major, minor, release;
9474
9475 if (cu->producer == NULL)
9476 {
9477 /* For unknown compilers expect their behavior is DWARF version
9478 compliant.
9479
9480 GCC started to support .debug_types sections by -gdwarf-4 since
9481 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9482 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9483 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9484 interpreted incorrectly by GDB now - GCC PR debug/48229. */
9485 }
9486 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9487 {
9488 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9489
9490 cs = &cu->producer[strlen ("GNU ")];
9491 while (*cs && !isdigit (*cs))
9492 cs++;
9493 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9494 {
9495 /* Not recognized as GCC. */
9496 }
9497 else
9498 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9499 }
9500 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9501 cu->producer_is_icc = 1;
9502 else
9503 {
9504 /* For other non-GCC compilers, expect their behavior is DWARF version
9505 compliant. */
9506 }
9507
9508 cu->checked_producer = 1;
9509 }
9510
9511 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9512 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9513 during 4.6.0 experimental. */
9514
9515 static int
9516 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9517 {
9518 if (!cu->checked_producer)
9519 check_producer (cu);
9520
9521 return cu->producer_is_gxx_lt_4_6;
9522 }
9523
9524 /* Return the default accessibility type if it is not overriden by
9525 DW_AT_accessibility. */
9526
9527 static enum dwarf_access_attribute
9528 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9529 {
9530 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9531 {
9532 /* The default DWARF 2 accessibility for members is public, the default
9533 accessibility for inheritance is private. */
9534
9535 if (die->tag != DW_TAG_inheritance)
9536 return DW_ACCESS_public;
9537 else
9538 return DW_ACCESS_private;
9539 }
9540 else
9541 {
9542 /* DWARF 3+ defines the default accessibility a different way. The same
9543 rules apply now for DW_TAG_inheritance as for the members and it only
9544 depends on the container kind. */
9545
9546 if (die->parent->tag == DW_TAG_class_type)
9547 return DW_ACCESS_private;
9548 else
9549 return DW_ACCESS_public;
9550 }
9551 }
9552
9553 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9554 offset. If the attribute was not found return 0, otherwise return
9555 1. If it was found but could not properly be handled, set *OFFSET
9556 to 0. */
9557
9558 static int
9559 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9560 LONGEST *offset)
9561 {
9562 struct attribute *attr;
9563
9564 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9565 if (attr != NULL)
9566 {
9567 *offset = 0;
9568
9569 /* Note that we do not check for a section offset first here.
9570 This is because DW_AT_data_member_location is new in DWARF 4,
9571 so if we see it, we can assume that a constant form is really
9572 a constant and not a section offset. */
9573 if (attr_form_is_constant (attr))
9574 *offset = dwarf2_get_attr_constant_value (attr, 0);
9575 else if (attr_form_is_section_offset (attr))
9576 dwarf2_complex_location_expr_complaint ();
9577 else if (attr_form_is_block (attr))
9578 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9579 else
9580 dwarf2_complex_location_expr_complaint ();
9581
9582 return 1;
9583 }
9584
9585 return 0;
9586 }
9587
9588 /* Add an aggregate field to the field list. */
9589
9590 static void
9591 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9592 struct dwarf2_cu *cu)
9593 {
9594 struct objfile *objfile = cu->objfile;
9595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9596 struct nextfield *new_field;
9597 struct attribute *attr;
9598 struct field *fp;
9599 char *fieldname = "";
9600
9601 /* Allocate a new field list entry and link it in. */
9602 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9603 make_cleanup (xfree, new_field);
9604 memset (new_field, 0, sizeof (struct nextfield));
9605
9606 if (die->tag == DW_TAG_inheritance)
9607 {
9608 new_field->next = fip->baseclasses;
9609 fip->baseclasses = new_field;
9610 }
9611 else
9612 {
9613 new_field->next = fip->fields;
9614 fip->fields = new_field;
9615 }
9616 fip->nfields++;
9617
9618 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9619 if (attr)
9620 new_field->accessibility = DW_UNSND (attr);
9621 else
9622 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9623 if (new_field->accessibility != DW_ACCESS_public)
9624 fip->non_public_fields = 1;
9625
9626 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9627 if (attr)
9628 new_field->virtuality = DW_UNSND (attr);
9629 else
9630 new_field->virtuality = DW_VIRTUALITY_none;
9631
9632 fp = &new_field->field;
9633
9634 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9635 {
9636 LONGEST offset;
9637
9638 /* Data member other than a C++ static data member. */
9639
9640 /* Get type of field. */
9641 fp->type = die_type (die, cu);
9642
9643 SET_FIELD_BITPOS (*fp, 0);
9644
9645 /* Get bit size of field (zero if none). */
9646 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9647 if (attr)
9648 {
9649 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9650 }
9651 else
9652 {
9653 FIELD_BITSIZE (*fp) = 0;
9654 }
9655
9656 /* Get bit offset of field. */
9657 if (handle_data_member_location (die, cu, &offset))
9658 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9659 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9660 if (attr)
9661 {
9662 if (gdbarch_bits_big_endian (gdbarch))
9663 {
9664 /* For big endian bits, the DW_AT_bit_offset gives the
9665 additional bit offset from the MSB of the containing
9666 anonymous object to the MSB of the field. We don't
9667 have to do anything special since we don't need to
9668 know the size of the anonymous object. */
9669 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9670 }
9671 else
9672 {
9673 /* For little endian bits, compute the bit offset to the
9674 MSB of the anonymous object, subtract off the number of
9675 bits from the MSB of the field to the MSB of the
9676 object, and then subtract off the number of bits of
9677 the field itself. The result is the bit offset of
9678 the LSB of the field. */
9679 int anonymous_size;
9680 int bit_offset = DW_UNSND (attr);
9681
9682 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9683 if (attr)
9684 {
9685 /* The size of the anonymous object containing
9686 the bit field is explicit, so use the
9687 indicated size (in bytes). */
9688 anonymous_size = DW_UNSND (attr);
9689 }
9690 else
9691 {
9692 /* The size of the anonymous object containing
9693 the bit field must be inferred from the type
9694 attribute of the data member containing the
9695 bit field. */
9696 anonymous_size = TYPE_LENGTH (fp->type);
9697 }
9698 SET_FIELD_BITPOS (*fp,
9699 (FIELD_BITPOS (*fp)
9700 + anonymous_size * bits_per_byte
9701 - bit_offset - FIELD_BITSIZE (*fp)));
9702 }
9703 }
9704
9705 /* Get name of field. */
9706 fieldname = dwarf2_name (die, cu);
9707 if (fieldname == NULL)
9708 fieldname = "";
9709
9710 /* The name is already allocated along with this objfile, so we don't
9711 need to duplicate it for the type. */
9712 fp->name = fieldname;
9713
9714 /* Change accessibility for artificial fields (e.g. virtual table
9715 pointer or virtual base class pointer) to private. */
9716 if (dwarf2_attr (die, DW_AT_artificial, cu))
9717 {
9718 FIELD_ARTIFICIAL (*fp) = 1;
9719 new_field->accessibility = DW_ACCESS_private;
9720 fip->non_public_fields = 1;
9721 }
9722 }
9723 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9724 {
9725 /* C++ static member. */
9726
9727 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9728 is a declaration, but all versions of G++ as of this writing
9729 (so through at least 3.2.1) incorrectly generate
9730 DW_TAG_variable tags. */
9731
9732 const char *physname;
9733
9734 /* Get name of field. */
9735 fieldname = dwarf2_name (die, cu);
9736 if (fieldname == NULL)
9737 return;
9738
9739 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9740 if (attr
9741 /* Only create a symbol if this is an external value.
9742 new_symbol checks this and puts the value in the global symbol
9743 table, which we want. If it is not external, new_symbol
9744 will try to put the value in cu->list_in_scope which is wrong. */
9745 && dwarf2_flag_true_p (die, DW_AT_external, cu))
9746 {
9747 /* A static const member, not much different than an enum as far as
9748 we're concerned, except that we can support more types. */
9749 new_symbol (die, NULL, cu);
9750 }
9751
9752 /* Get physical name. */
9753 physname = dwarf2_physname (fieldname, die, cu);
9754
9755 /* The name is already allocated along with this objfile, so we don't
9756 need to duplicate it for the type. */
9757 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9758 FIELD_TYPE (*fp) = die_type (die, cu);
9759 FIELD_NAME (*fp) = fieldname;
9760 }
9761 else if (die->tag == DW_TAG_inheritance)
9762 {
9763 LONGEST offset;
9764
9765 /* C++ base class field. */
9766 if (handle_data_member_location (die, cu, &offset))
9767 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9768 FIELD_BITSIZE (*fp) = 0;
9769 FIELD_TYPE (*fp) = die_type (die, cu);
9770 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9771 fip->nbaseclasses++;
9772 }
9773 }
9774
9775 /* Add a typedef defined in the scope of the FIP's class. */
9776
9777 static void
9778 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9779 struct dwarf2_cu *cu)
9780 {
9781 struct objfile *objfile = cu->objfile;
9782 struct typedef_field_list *new_field;
9783 struct attribute *attr;
9784 struct typedef_field *fp;
9785 char *fieldname = "";
9786
9787 /* Allocate a new field list entry and link it in. */
9788 new_field = xzalloc (sizeof (*new_field));
9789 make_cleanup (xfree, new_field);
9790
9791 gdb_assert (die->tag == DW_TAG_typedef);
9792
9793 fp = &new_field->field;
9794
9795 /* Get name of field. */
9796 fp->name = dwarf2_name (die, cu);
9797 if (fp->name == NULL)
9798 return;
9799
9800 fp->type = read_type_die (die, cu);
9801
9802 new_field->next = fip->typedef_field_list;
9803 fip->typedef_field_list = new_field;
9804 fip->typedef_field_list_count++;
9805 }
9806
9807 /* Create the vector of fields, and attach it to the type. */
9808
9809 static void
9810 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9811 struct dwarf2_cu *cu)
9812 {
9813 int nfields = fip->nfields;
9814
9815 /* Record the field count, allocate space for the array of fields,
9816 and create blank accessibility bitfields if necessary. */
9817 TYPE_NFIELDS (type) = nfields;
9818 TYPE_FIELDS (type) = (struct field *)
9819 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9820 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9821
9822 if (fip->non_public_fields && cu->language != language_ada)
9823 {
9824 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9825
9826 TYPE_FIELD_PRIVATE_BITS (type) =
9827 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9828 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9829
9830 TYPE_FIELD_PROTECTED_BITS (type) =
9831 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9832 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
9833
9834 TYPE_FIELD_IGNORE_BITS (type) =
9835 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9836 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
9837 }
9838
9839 /* If the type has baseclasses, allocate and clear a bit vector for
9840 TYPE_FIELD_VIRTUAL_BITS. */
9841 if (fip->nbaseclasses && cu->language != language_ada)
9842 {
9843 int num_bytes = B_BYTES (fip->nbaseclasses);
9844 unsigned char *pointer;
9845
9846 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9847 pointer = TYPE_ALLOC (type, num_bytes);
9848 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
9849 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
9850 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
9851 }
9852
9853 /* Copy the saved-up fields into the field vector. Start from the head of
9854 the list, adding to the tail of the field array, so that they end up in
9855 the same order in the array in which they were added to the list. */
9856 while (nfields-- > 0)
9857 {
9858 struct nextfield *fieldp;
9859
9860 if (fip->fields)
9861 {
9862 fieldp = fip->fields;
9863 fip->fields = fieldp->next;
9864 }
9865 else
9866 {
9867 fieldp = fip->baseclasses;
9868 fip->baseclasses = fieldp->next;
9869 }
9870
9871 TYPE_FIELD (type, nfields) = fieldp->field;
9872 switch (fieldp->accessibility)
9873 {
9874 case DW_ACCESS_private:
9875 if (cu->language != language_ada)
9876 SET_TYPE_FIELD_PRIVATE (type, nfields);
9877 break;
9878
9879 case DW_ACCESS_protected:
9880 if (cu->language != language_ada)
9881 SET_TYPE_FIELD_PROTECTED (type, nfields);
9882 break;
9883
9884 case DW_ACCESS_public:
9885 break;
9886
9887 default:
9888 /* Unknown accessibility. Complain and treat it as public. */
9889 {
9890 complaint (&symfile_complaints, _("unsupported accessibility %d"),
9891 fieldp->accessibility);
9892 }
9893 break;
9894 }
9895 if (nfields < fip->nbaseclasses)
9896 {
9897 switch (fieldp->virtuality)
9898 {
9899 case DW_VIRTUALITY_virtual:
9900 case DW_VIRTUALITY_pure_virtual:
9901 if (cu->language == language_ada)
9902 error (_("unexpected virtuality in component of Ada type"));
9903 SET_TYPE_FIELD_VIRTUAL (type, nfields);
9904 break;
9905 }
9906 }
9907 }
9908 }
9909
9910 /* Add a member function to the proper fieldlist. */
9911
9912 static void
9913 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
9914 struct type *type, struct dwarf2_cu *cu)
9915 {
9916 struct objfile *objfile = cu->objfile;
9917 struct attribute *attr;
9918 struct fnfieldlist *flp;
9919 int i;
9920 struct fn_field *fnp;
9921 char *fieldname;
9922 struct nextfnfield *new_fnfield;
9923 struct type *this_type;
9924 enum dwarf_access_attribute accessibility;
9925
9926 if (cu->language == language_ada)
9927 error (_("unexpected member function in Ada type"));
9928
9929 /* Get name of member function. */
9930 fieldname = dwarf2_name (die, cu);
9931 if (fieldname == NULL)
9932 return;
9933
9934 /* Look up member function name in fieldlist. */
9935 for (i = 0; i < fip->nfnfields; i++)
9936 {
9937 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
9938 break;
9939 }
9940
9941 /* Create new list element if necessary. */
9942 if (i < fip->nfnfields)
9943 flp = &fip->fnfieldlists[i];
9944 else
9945 {
9946 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9947 {
9948 fip->fnfieldlists = (struct fnfieldlist *)
9949 xrealloc (fip->fnfieldlists,
9950 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
9951 * sizeof (struct fnfieldlist));
9952 if (fip->nfnfields == 0)
9953 make_cleanup (free_current_contents, &fip->fnfieldlists);
9954 }
9955 flp = &fip->fnfieldlists[fip->nfnfields];
9956 flp->name = fieldname;
9957 flp->length = 0;
9958 flp->head = NULL;
9959 i = fip->nfnfields++;
9960 }
9961
9962 /* Create a new member function field and chain it to the field list
9963 entry. */
9964 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
9965 make_cleanup (xfree, new_fnfield);
9966 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9967 new_fnfield->next = flp->head;
9968 flp->head = new_fnfield;
9969 flp->length++;
9970
9971 /* Fill in the member function field info. */
9972 fnp = &new_fnfield->fnfield;
9973
9974 /* Delay processing of the physname until later. */
9975 if (cu->language == language_cplus || cu->language == language_java)
9976 {
9977 add_to_method_list (type, i, flp->length - 1, fieldname,
9978 die, cu);
9979 }
9980 else
9981 {
9982 const char *physname = dwarf2_physname (fieldname, die, cu);
9983 fnp->physname = physname ? physname : "";
9984 }
9985
9986 fnp->type = alloc_type (objfile);
9987 this_type = read_type_die (die, cu);
9988 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
9989 {
9990 int nparams = TYPE_NFIELDS (this_type);
9991
9992 /* TYPE is the domain of this method, and THIS_TYPE is the type
9993 of the method itself (TYPE_CODE_METHOD). */
9994 smash_to_method_type (fnp->type, type,
9995 TYPE_TARGET_TYPE (this_type),
9996 TYPE_FIELDS (this_type),
9997 TYPE_NFIELDS (this_type),
9998 TYPE_VARARGS (this_type));
9999
10000 /* Handle static member functions.
10001 Dwarf2 has no clean way to discern C++ static and non-static
10002 member functions. G++ helps GDB by marking the first
10003 parameter for non-static member functions (which is the this
10004 pointer) as artificial. We obtain this information from
10005 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10006 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10007 fnp->voffset = VOFFSET_STATIC;
10008 }
10009 else
10010 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10011 dwarf2_full_name (fieldname, die, cu));
10012
10013 /* Get fcontext from DW_AT_containing_type if present. */
10014 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10015 fnp->fcontext = die_containing_type (die, cu);
10016
10017 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10018 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10019
10020 /* Get accessibility. */
10021 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10022 if (attr)
10023 accessibility = DW_UNSND (attr);
10024 else
10025 accessibility = dwarf2_default_access_attribute (die, cu);
10026 switch (accessibility)
10027 {
10028 case DW_ACCESS_private:
10029 fnp->is_private = 1;
10030 break;
10031 case DW_ACCESS_protected:
10032 fnp->is_protected = 1;
10033 break;
10034 }
10035
10036 /* Check for artificial methods. */
10037 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10038 if (attr && DW_UNSND (attr) != 0)
10039 fnp->is_artificial = 1;
10040
10041 /* Get index in virtual function table if it is a virtual member
10042 function. For older versions of GCC, this is an offset in the
10043 appropriate virtual table, as specified by DW_AT_containing_type.
10044 For everyone else, it is an expression to be evaluated relative
10045 to the object address. */
10046
10047 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10048 if (attr)
10049 {
10050 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10051 {
10052 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10053 {
10054 /* Old-style GCC. */
10055 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10056 }
10057 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10058 || (DW_BLOCK (attr)->size > 1
10059 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10060 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10061 {
10062 struct dwarf_block blk;
10063 int offset;
10064
10065 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10066 ? 1 : 2);
10067 blk.size = DW_BLOCK (attr)->size - offset;
10068 blk.data = DW_BLOCK (attr)->data + offset;
10069 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10070 if ((fnp->voffset % cu->header.addr_size) != 0)
10071 dwarf2_complex_location_expr_complaint ();
10072 else
10073 fnp->voffset /= cu->header.addr_size;
10074 fnp->voffset += 2;
10075 }
10076 else
10077 dwarf2_complex_location_expr_complaint ();
10078
10079 if (!fnp->fcontext)
10080 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10081 }
10082 else if (attr_form_is_section_offset (attr))
10083 {
10084 dwarf2_complex_location_expr_complaint ();
10085 }
10086 else
10087 {
10088 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10089 fieldname);
10090 }
10091 }
10092 else
10093 {
10094 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10095 if (attr && DW_UNSND (attr))
10096 {
10097 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10098 complaint (&symfile_complaints,
10099 _("Member function \"%s\" (offset %d) is virtual "
10100 "but the vtable offset is not specified"),
10101 fieldname, die->offset.sect_off);
10102 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10103 TYPE_CPLUS_DYNAMIC (type) = 1;
10104 }
10105 }
10106 }
10107
10108 /* Create the vector of member function fields, and attach it to the type. */
10109
10110 static void
10111 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10112 struct dwarf2_cu *cu)
10113 {
10114 struct fnfieldlist *flp;
10115 int i;
10116
10117 if (cu->language == language_ada)
10118 error (_("unexpected member functions in Ada type"));
10119
10120 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10121 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10122 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10123
10124 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10125 {
10126 struct nextfnfield *nfp = flp->head;
10127 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10128 int k;
10129
10130 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10131 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10132 fn_flp->fn_fields = (struct fn_field *)
10133 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10134 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10135 fn_flp->fn_fields[k] = nfp->fnfield;
10136 }
10137
10138 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10139 }
10140
10141 /* Returns non-zero if NAME is the name of a vtable member in CU's
10142 language, zero otherwise. */
10143 static int
10144 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10145 {
10146 static const char vptr[] = "_vptr";
10147 static const char vtable[] = "vtable";
10148
10149 /* Look for the C++ and Java forms of the vtable. */
10150 if ((cu->language == language_java
10151 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10152 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10153 && is_cplus_marker (name[sizeof (vptr) - 1])))
10154 return 1;
10155
10156 return 0;
10157 }
10158
10159 /* GCC outputs unnamed structures that are really pointers to member
10160 functions, with the ABI-specified layout. If TYPE describes
10161 such a structure, smash it into a member function type.
10162
10163 GCC shouldn't do this; it should just output pointer to member DIEs.
10164 This is GCC PR debug/28767. */
10165
10166 static void
10167 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10168 {
10169 struct type *pfn_type, *domain_type, *new_type;
10170
10171 /* Check for a structure with no name and two children. */
10172 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10173 return;
10174
10175 /* Check for __pfn and __delta members. */
10176 if (TYPE_FIELD_NAME (type, 0) == NULL
10177 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10178 || TYPE_FIELD_NAME (type, 1) == NULL
10179 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10180 return;
10181
10182 /* Find the type of the method. */
10183 pfn_type = TYPE_FIELD_TYPE (type, 0);
10184 if (pfn_type == NULL
10185 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10186 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10187 return;
10188
10189 /* Look for the "this" argument. */
10190 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10191 if (TYPE_NFIELDS (pfn_type) == 0
10192 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10193 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10194 return;
10195
10196 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10197 new_type = alloc_type (objfile);
10198 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10199 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10200 TYPE_VARARGS (pfn_type));
10201 smash_to_methodptr_type (type, new_type);
10202 }
10203
10204 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10205 (icc). */
10206
10207 static int
10208 producer_is_icc (struct dwarf2_cu *cu)
10209 {
10210 if (!cu->checked_producer)
10211 check_producer (cu);
10212
10213 return cu->producer_is_icc;
10214 }
10215
10216 /* Called when we find the DIE that starts a structure or union scope
10217 (definition) to create a type for the structure or union. Fill in
10218 the type's name and general properties; the members will not be
10219 processed until process_structure_type.
10220
10221 NOTE: we need to call these functions regardless of whether or not the
10222 DIE has a DW_AT_name attribute, since it might be an anonymous
10223 structure or union. This gets the type entered into our set of
10224 user defined types.
10225
10226 However, if the structure is incomplete (an opaque struct/union)
10227 then suppress creating a symbol table entry for it since gdb only
10228 wants to find the one with the complete definition. Note that if
10229 it is complete, we just call new_symbol, which does it's own
10230 checking about whether the struct/union is anonymous or not (and
10231 suppresses creating a symbol table entry itself). */
10232
10233 static struct type *
10234 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10235 {
10236 struct objfile *objfile = cu->objfile;
10237 struct type *type;
10238 struct attribute *attr;
10239 char *name;
10240
10241 /* If the definition of this type lives in .debug_types, read that type.
10242 Don't follow DW_AT_specification though, that will take us back up
10243 the chain and we want to go down. */
10244 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10245 if (attr)
10246 {
10247 struct dwarf2_cu *type_cu = cu;
10248 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10249
10250 /* We could just recurse on read_structure_type, but we need to call
10251 get_die_type to ensure only one type for this DIE is created.
10252 This is important, for example, because for c++ classes we need
10253 TYPE_NAME set which is only done by new_symbol. Blech. */
10254 type = read_type_die (type_die, type_cu);
10255
10256 /* TYPE_CU may not be the same as CU.
10257 Ensure TYPE is recorded in CU's type_hash table. */
10258 return set_die_type (die, type, cu);
10259 }
10260
10261 type = alloc_type (objfile);
10262 INIT_CPLUS_SPECIFIC (type);
10263
10264 name = dwarf2_name (die, cu);
10265 if (name != NULL)
10266 {
10267 if (cu->language == language_cplus
10268 || cu->language == language_java)
10269 {
10270 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10271
10272 /* dwarf2_full_name might have already finished building the DIE's
10273 type. If so, there is no need to continue. */
10274 if (get_die_type (die, cu) != NULL)
10275 return get_die_type (die, cu);
10276
10277 TYPE_TAG_NAME (type) = full_name;
10278 if (die->tag == DW_TAG_structure_type
10279 || die->tag == DW_TAG_class_type)
10280 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10281 }
10282 else
10283 {
10284 /* The name is already allocated along with this objfile, so
10285 we don't need to duplicate it for the type. */
10286 TYPE_TAG_NAME (type) = (char *) name;
10287 if (die->tag == DW_TAG_class_type)
10288 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10289 }
10290 }
10291
10292 if (die->tag == DW_TAG_structure_type)
10293 {
10294 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10295 }
10296 else if (die->tag == DW_TAG_union_type)
10297 {
10298 TYPE_CODE (type) = TYPE_CODE_UNION;
10299 }
10300 else
10301 {
10302 TYPE_CODE (type) = TYPE_CODE_CLASS;
10303 }
10304
10305 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10306 TYPE_DECLARED_CLASS (type) = 1;
10307
10308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10309 if (attr)
10310 {
10311 TYPE_LENGTH (type) = DW_UNSND (attr);
10312 }
10313 else
10314 {
10315 TYPE_LENGTH (type) = 0;
10316 }
10317
10318 if (producer_is_icc (cu))
10319 {
10320 /* ICC does not output the required DW_AT_declaration
10321 on incomplete types, but gives them a size of zero. */
10322 }
10323 else
10324 TYPE_STUB_SUPPORTED (type) = 1;
10325
10326 if (die_is_declaration (die, cu))
10327 TYPE_STUB (type) = 1;
10328 else if (attr == NULL && die->child == NULL
10329 && producer_is_realview (cu->producer))
10330 /* RealView does not output the required DW_AT_declaration
10331 on incomplete types. */
10332 TYPE_STUB (type) = 1;
10333
10334 /* We need to add the type field to the die immediately so we don't
10335 infinitely recurse when dealing with pointers to the structure
10336 type within the structure itself. */
10337 set_die_type (die, type, cu);
10338
10339 /* set_die_type should be already done. */
10340 set_descriptive_type (type, die, cu);
10341
10342 return type;
10343 }
10344
10345 /* Finish creating a structure or union type, including filling in
10346 its members and creating a symbol for it. */
10347
10348 static void
10349 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10350 {
10351 struct objfile *objfile = cu->objfile;
10352 struct die_info *child_die = die->child;
10353 struct type *type;
10354
10355 type = get_die_type (die, cu);
10356 if (type == NULL)
10357 type = read_structure_type (die, cu);
10358
10359 if (die->child != NULL && ! die_is_declaration (die, cu))
10360 {
10361 struct field_info fi;
10362 struct die_info *child_die;
10363 VEC (symbolp) *template_args = NULL;
10364 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10365
10366 memset (&fi, 0, sizeof (struct field_info));
10367
10368 child_die = die->child;
10369
10370 while (child_die && child_die->tag)
10371 {
10372 if (child_die->tag == DW_TAG_member
10373 || child_die->tag == DW_TAG_variable)
10374 {
10375 /* NOTE: carlton/2002-11-05: A C++ static data member
10376 should be a DW_TAG_member that is a declaration, but
10377 all versions of G++ as of this writing (so through at
10378 least 3.2.1) incorrectly generate DW_TAG_variable
10379 tags for them instead. */
10380 dwarf2_add_field (&fi, child_die, cu);
10381 }
10382 else if (child_die->tag == DW_TAG_subprogram)
10383 {
10384 /* C++ member function. */
10385 dwarf2_add_member_fn (&fi, child_die, type, cu);
10386 }
10387 else if (child_die->tag == DW_TAG_inheritance)
10388 {
10389 /* C++ base class field. */
10390 dwarf2_add_field (&fi, child_die, cu);
10391 }
10392 else if (child_die->tag == DW_TAG_typedef)
10393 dwarf2_add_typedef (&fi, child_die, cu);
10394 else if (child_die->tag == DW_TAG_template_type_param
10395 || child_die->tag == DW_TAG_template_value_param)
10396 {
10397 struct symbol *arg = new_symbol (child_die, NULL, cu);
10398
10399 if (arg != NULL)
10400 VEC_safe_push (symbolp, template_args, arg);
10401 }
10402
10403 child_die = sibling_die (child_die);
10404 }
10405
10406 /* Attach template arguments to type. */
10407 if (! VEC_empty (symbolp, template_args))
10408 {
10409 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10410 TYPE_N_TEMPLATE_ARGUMENTS (type)
10411 = VEC_length (symbolp, template_args);
10412 TYPE_TEMPLATE_ARGUMENTS (type)
10413 = obstack_alloc (&objfile->objfile_obstack,
10414 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10415 * sizeof (struct symbol *)));
10416 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10417 VEC_address (symbolp, template_args),
10418 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10419 * sizeof (struct symbol *)));
10420 VEC_free (symbolp, template_args);
10421 }
10422
10423 /* Attach fields and member functions to the type. */
10424 if (fi.nfields)
10425 dwarf2_attach_fields_to_type (&fi, type, cu);
10426 if (fi.nfnfields)
10427 {
10428 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10429
10430 /* Get the type which refers to the base class (possibly this
10431 class itself) which contains the vtable pointer for the current
10432 class from the DW_AT_containing_type attribute. This use of
10433 DW_AT_containing_type is a GNU extension. */
10434
10435 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10436 {
10437 struct type *t = die_containing_type (die, cu);
10438
10439 TYPE_VPTR_BASETYPE (type) = t;
10440 if (type == t)
10441 {
10442 int i;
10443
10444 /* Our own class provides vtbl ptr. */
10445 for (i = TYPE_NFIELDS (t) - 1;
10446 i >= TYPE_N_BASECLASSES (t);
10447 --i)
10448 {
10449 const char *fieldname = TYPE_FIELD_NAME (t, i);
10450
10451 if (is_vtable_name (fieldname, cu))
10452 {
10453 TYPE_VPTR_FIELDNO (type) = i;
10454 break;
10455 }
10456 }
10457
10458 /* Complain if virtual function table field not found. */
10459 if (i < TYPE_N_BASECLASSES (t))
10460 complaint (&symfile_complaints,
10461 _("virtual function table pointer "
10462 "not found when defining class '%s'"),
10463 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10464 "");
10465 }
10466 else
10467 {
10468 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10469 }
10470 }
10471 else if (cu->producer
10472 && strncmp (cu->producer,
10473 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10474 {
10475 /* The IBM XLC compiler does not provide direct indication
10476 of the containing type, but the vtable pointer is
10477 always named __vfp. */
10478
10479 int i;
10480
10481 for (i = TYPE_NFIELDS (type) - 1;
10482 i >= TYPE_N_BASECLASSES (type);
10483 --i)
10484 {
10485 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10486 {
10487 TYPE_VPTR_FIELDNO (type) = i;
10488 TYPE_VPTR_BASETYPE (type) = type;
10489 break;
10490 }
10491 }
10492 }
10493 }
10494
10495 /* Copy fi.typedef_field_list linked list elements content into the
10496 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10497 if (fi.typedef_field_list)
10498 {
10499 int i = fi.typedef_field_list_count;
10500
10501 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10502 TYPE_TYPEDEF_FIELD_ARRAY (type)
10503 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10504 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10505
10506 /* Reverse the list order to keep the debug info elements order. */
10507 while (--i >= 0)
10508 {
10509 struct typedef_field *dest, *src;
10510
10511 dest = &TYPE_TYPEDEF_FIELD (type, i);
10512 src = &fi.typedef_field_list->field;
10513 fi.typedef_field_list = fi.typedef_field_list->next;
10514 *dest = *src;
10515 }
10516 }
10517
10518 do_cleanups (back_to);
10519
10520 if (HAVE_CPLUS_STRUCT (type))
10521 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10522 }
10523
10524 quirk_gcc_member_function_pointer (type, objfile);
10525
10526 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10527 snapshots) has been known to create a die giving a declaration
10528 for a class that has, as a child, a die giving a definition for a
10529 nested class. So we have to process our children even if the
10530 current die is a declaration. Normally, of course, a declaration
10531 won't have any children at all. */
10532
10533 while (child_die != NULL && child_die->tag)
10534 {
10535 if (child_die->tag == DW_TAG_member
10536 || child_die->tag == DW_TAG_variable
10537 || child_die->tag == DW_TAG_inheritance
10538 || child_die->tag == DW_TAG_template_value_param
10539 || child_die->tag == DW_TAG_template_type_param)
10540 {
10541 /* Do nothing. */
10542 }
10543 else
10544 process_die (child_die, cu);
10545
10546 child_die = sibling_die (child_die);
10547 }
10548
10549 /* Do not consider external references. According to the DWARF standard,
10550 these DIEs are identified by the fact that they have no byte_size
10551 attribute, and a declaration attribute. */
10552 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10553 || !die_is_declaration (die, cu))
10554 new_symbol (die, type, cu);
10555 }
10556
10557 /* Given a DW_AT_enumeration_type die, set its type. We do not
10558 complete the type's fields yet, or create any symbols. */
10559
10560 static struct type *
10561 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10562 {
10563 struct objfile *objfile = cu->objfile;
10564 struct type *type;
10565 struct attribute *attr;
10566 const char *name;
10567
10568 /* If the definition of this type lives in .debug_types, read that type.
10569 Don't follow DW_AT_specification though, that will take us back up
10570 the chain and we want to go down. */
10571 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10572 if (attr)
10573 {
10574 struct dwarf2_cu *type_cu = cu;
10575 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10576
10577 type = read_type_die (type_die, type_cu);
10578
10579 /* TYPE_CU may not be the same as CU.
10580 Ensure TYPE is recorded in CU's type_hash table. */
10581 return set_die_type (die, type, cu);
10582 }
10583
10584 type = alloc_type (objfile);
10585
10586 TYPE_CODE (type) = TYPE_CODE_ENUM;
10587 name = dwarf2_full_name (NULL, die, cu);
10588 if (name != NULL)
10589 TYPE_TAG_NAME (type) = (char *) name;
10590
10591 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10592 if (attr)
10593 {
10594 TYPE_LENGTH (type) = DW_UNSND (attr);
10595 }
10596 else
10597 {
10598 TYPE_LENGTH (type) = 0;
10599 }
10600
10601 /* The enumeration DIE can be incomplete. In Ada, any type can be
10602 declared as private in the package spec, and then defined only
10603 inside the package body. Such types are known as Taft Amendment
10604 Types. When another package uses such a type, an incomplete DIE
10605 may be generated by the compiler. */
10606 if (die_is_declaration (die, cu))
10607 TYPE_STUB (type) = 1;
10608
10609 return set_die_type (die, type, cu);
10610 }
10611
10612 /* Given a pointer to a die which begins an enumeration, process all
10613 the dies that define the members of the enumeration, and create the
10614 symbol for the enumeration type.
10615
10616 NOTE: We reverse the order of the element list. */
10617
10618 static void
10619 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 struct type *this_type;
10622
10623 this_type = get_die_type (die, cu);
10624 if (this_type == NULL)
10625 this_type = read_enumeration_type (die, cu);
10626
10627 if (die->child != NULL)
10628 {
10629 struct die_info *child_die;
10630 struct symbol *sym;
10631 struct field *fields = NULL;
10632 int num_fields = 0;
10633 int unsigned_enum = 1;
10634 char *name;
10635 int flag_enum = 1;
10636 ULONGEST mask = 0;
10637
10638 child_die = die->child;
10639 while (child_die && child_die->tag)
10640 {
10641 if (child_die->tag != DW_TAG_enumerator)
10642 {
10643 process_die (child_die, cu);
10644 }
10645 else
10646 {
10647 name = dwarf2_name (child_die, cu);
10648 if (name)
10649 {
10650 sym = new_symbol (child_die, this_type, cu);
10651 if (SYMBOL_VALUE (sym) < 0)
10652 {
10653 unsigned_enum = 0;
10654 flag_enum = 0;
10655 }
10656 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10657 flag_enum = 0;
10658 else
10659 mask |= SYMBOL_VALUE (sym);
10660
10661 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10662 {
10663 fields = (struct field *)
10664 xrealloc (fields,
10665 (num_fields + DW_FIELD_ALLOC_CHUNK)
10666 * sizeof (struct field));
10667 }
10668
10669 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10670 FIELD_TYPE (fields[num_fields]) = NULL;
10671 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10672 FIELD_BITSIZE (fields[num_fields]) = 0;
10673
10674 num_fields++;
10675 }
10676 }
10677
10678 child_die = sibling_die (child_die);
10679 }
10680
10681 if (num_fields)
10682 {
10683 TYPE_NFIELDS (this_type) = num_fields;
10684 TYPE_FIELDS (this_type) = (struct field *)
10685 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10686 memcpy (TYPE_FIELDS (this_type), fields,
10687 sizeof (struct field) * num_fields);
10688 xfree (fields);
10689 }
10690 if (unsigned_enum)
10691 TYPE_UNSIGNED (this_type) = 1;
10692 if (flag_enum)
10693 TYPE_FLAG_ENUM (this_type) = 1;
10694 }
10695
10696 /* If we are reading an enum from a .debug_types unit, and the enum
10697 is a declaration, and the enum is not the signatured type in the
10698 unit, then we do not want to add a symbol for it. Adding a
10699 symbol would in some cases obscure the true definition of the
10700 enum, giving users an incomplete type when the definition is
10701 actually available. Note that we do not want to do this for all
10702 enums which are just declarations, because C++0x allows forward
10703 enum declarations. */
10704 if (cu->per_cu->is_debug_types
10705 && die_is_declaration (die, cu))
10706 {
10707 struct signatured_type *sig_type;
10708
10709 sig_type
10710 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10711 cu->per_cu->info_or_types_section,
10712 cu->per_cu->offset);
10713 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10714 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10715 return;
10716 }
10717
10718 new_symbol (die, this_type, cu);
10719 }
10720
10721 /* Extract all information from a DW_TAG_array_type DIE and put it in
10722 the DIE's type field. For now, this only handles one dimensional
10723 arrays. */
10724
10725 static struct type *
10726 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10727 {
10728 struct objfile *objfile = cu->objfile;
10729 struct die_info *child_die;
10730 struct type *type;
10731 struct type *element_type, *range_type, *index_type;
10732 struct type **range_types = NULL;
10733 struct attribute *attr;
10734 int ndim = 0;
10735 struct cleanup *back_to;
10736 char *name;
10737
10738 element_type = die_type (die, cu);
10739
10740 /* The die_type call above may have already set the type for this DIE. */
10741 type = get_die_type (die, cu);
10742 if (type)
10743 return type;
10744
10745 /* Irix 6.2 native cc creates array types without children for
10746 arrays with unspecified length. */
10747 if (die->child == NULL)
10748 {
10749 index_type = objfile_type (objfile)->builtin_int;
10750 range_type = create_range_type (NULL, index_type, 0, -1);
10751 type = create_array_type (NULL, element_type, range_type);
10752 return set_die_type (die, type, cu);
10753 }
10754
10755 back_to = make_cleanup (null_cleanup, NULL);
10756 child_die = die->child;
10757 while (child_die && child_die->tag)
10758 {
10759 if (child_die->tag == DW_TAG_subrange_type)
10760 {
10761 struct type *child_type = read_type_die (child_die, cu);
10762
10763 if (child_type != NULL)
10764 {
10765 /* The range type was succesfully read. Save it for the
10766 array type creation. */
10767 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10768 {
10769 range_types = (struct type **)
10770 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10771 * sizeof (struct type *));
10772 if (ndim == 0)
10773 make_cleanup (free_current_contents, &range_types);
10774 }
10775 range_types[ndim++] = child_type;
10776 }
10777 }
10778 child_die = sibling_die (child_die);
10779 }
10780
10781 /* Dwarf2 dimensions are output from left to right, create the
10782 necessary array types in backwards order. */
10783
10784 type = element_type;
10785
10786 if (read_array_order (die, cu) == DW_ORD_col_major)
10787 {
10788 int i = 0;
10789
10790 while (i < ndim)
10791 type = create_array_type (NULL, type, range_types[i++]);
10792 }
10793 else
10794 {
10795 while (ndim-- > 0)
10796 type = create_array_type (NULL, type, range_types[ndim]);
10797 }
10798
10799 /* Understand Dwarf2 support for vector types (like they occur on
10800 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10801 array type. This is not part of the Dwarf2/3 standard yet, but a
10802 custom vendor extension. The main difference between a regular
10803 array and the vector variant is that vectors are passed by value
10804 to functions. */
10805 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10806 if (attr)
10807 make_vector_type (type);
10808
10809 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10810 implementation may choose to implement triple vectors using this
10811 attribute. */
10812 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10813 if (attr)
10814 {
10815 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10816 TYPE_LENGTH (type) = DW_UNSND (attr);
10817 else
10818 complaint (&symfile_complaints,
10819 _("DW_AT_byte_size for array type smaller "
10820 "than the total size of elements"));
10821 }
10822
10823 name = dwarf2_name (die, cu);
10824 if (name)
10825 TYPE_NAME (type) = name;
10826
10827 /* Install the type in the die. */
10828 set_die_type (die, type, cu);
10829
10830 /* set_die_type should be already done. */
10831 set_descriptive_type (type, die, cu);
10832
10833 do_cleanups (back_to);
10834
10835 return type;
10836 }
10837
10838 static enum dwarf_array_dim_ordering
10839 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
10840 {
10841 struct attribute *attr;
10842
10843 attr = dwarf2_attr (die, DW_AT_ordering, cu);
10844
10845 if (attr) return DW_SND (attr);
10846
10847 /* GNU F77 is a special case, as at 08/2004 array type info is the
10848 opposite order to the dwarf2 specification, but data is still
10849 laid out as per normal fortran.
10850
10851 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
10852 version checking. */
10853
10854 if (cu->language == language_fortran
10855 && cu->producer && strstr (cu->producer, "GNU F77"))
10856 {
10857 return DW_ORD_row_major;
10858 }
10859
10860 switch (cu->language_defn->la_array_ordering)
10861 {
10862 case array_column_major:
10863 return DW_ORD_col_major;
10864 case array_row_major:
10865 default:
10866 return DW_ORD_row_major;
10867 };
10868 }
10869
10870 /* Extract all information from a DW_TAG_set_type DIE and put it in
10871 the DIE's type field. */
10872
10873 static struct type *
10874 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
10875 {
10876 struct type *domain_type, *set_type;
10877 struct attribute *attr;
10878
10879 domain_type = die_type (die, cu);
10880
10881 /* The die_type call above may have already set the type for this DIE. */
10882 set_type = get_die_type (die, cu);
10883 if (set_type)
10884 return set_type;
10885
10886 set_type = create_set_type (NULL, domain_type);
10887
10888 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10889 if (attr)
10890 TYPE_LENGTH (set_type) = DW_UNSND (attr);
10891
10892 return set_die_type (die, set_type, cu);
10893 }
10894
10895 /* First cut: install each common block member as a global variable. */
10896
10897 static void
10898 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
10899 {
10900 struct die_info *child_die;
10901 struct attribute *attr;
10902 struct symbol *sym;
10903 CORE_ADDR base = (CORE_ADDR) 0;
10904
10905 attr = dwarf2_attr (die, DW_AT_location, cu);
10906 if (attr)
10907 {
10908 /* Support the .debug_loc offsets. */
10909 if (attr_form_is_block (attr))
10910 {
10911 base = decode_locdesc (DW_BLOCK (attr), cu);
10912 }
10913 else if (attr_form_is_section_offset (attr))
10914 {
10915 dwarf2_complex_location_expr_complaint ();
10916 }
10917 else
10918 {
10919 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10920 "common block member");
10921 }
10922 }
10923 if (die->child != NULL)
10924 {
10925 child_die = die->child;
10926 while (child_die && child_die->tag)
10927 {
10928 LONGEST offset;
10929
10930 sym = new_symbol (child_die, NULL, cu);
10931 if (sym != NULL
10932 && handle_data_member_location (child_die, cu, &offset))
10933 {
10934 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
10935 add_symbol_to_list (sym, &global_symbols);
10936 }
10937 child_die = sibling_die (child_die);
10938 }
10939 }
10940 }
10941
10942 /* Create a type for a C++ namespace. */
10943
10944 static struct type *
10945 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
10946 {
10947 struct objfile *objfile = cu->objfile;
10948 const char *previous_prefix, *name;
10949 int is_anonymous;
10950 struct type *type;
10951
10952 /* For extensions, reuse the type of the original namespace. */
10953 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10954 {
10955 struct die_info *ext_die;
10956 struct dwarf2_cu *ext_cu = cu;
10957
10958 ext_die = dwarf2_extension (die, &ext_cu);
10959 type = read_type_die (ext_die, ext_cu);
10960
10961 /* EXT_CU may not be the same as CU.
10962 Ensure TYPE is recorded in CU's type_hash table. */
10963 return set_die_type (die, type, cu);
10964 }
10965
10966 name = namespace_name (die, &is_anonymous, cu);
10967
10968 /* Now build the name of the current namespace. */
10969
10970 previous_prefix = determine_prefix (die, cu);
10971 if (previous_prefix[0] != '\0')
10972 name = typename_concat (&objfile->objfile_obstack,
10973 previous_prefix, name, 0, cu);
10974
10975 /* Create the type. */
10976 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10977 objfile);
10978 TYPE_NAME (type) = (char *) name;
10979 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10980
10981 return set_die_type (die, type, cu);
10982 }
10983
10984 /* Read a C++ namespace. */
10985
10986 static void
10987 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10988 {
10989 struct objfile *objfile = cu->objfile;
10990 int is_anonymous;
10991
10992 /* Add a symbol associated to this if we haven't seen the namespace
10993 before. Also, add a using directive if it's an anonymous
10994 namespace. */
10995
10996 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
10997 {
10998 struct type *type;
10999
11000 type = read_type_die (die, cu);
11001 new_symbol (die, type, cu);
11002
11003 namespace_name (die, &is_anonymous, cu);
11004 if (is_anonymous)
11005 {
11006 const char *previous_prefix = determine_prefix (die, cu);
11007
11008 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11009 NULL, NULL, &objfile->objfile_obstack);
11010 }
11011 }
11012
11013 if (die->child != NULL)
11014 {
11015 struct die_info *child_die = die->child;
11016
11017 while (child_die && child_die->tag)
11018 {
11019 process_die (child_die, cu);
11020 child_die = sibling_die (child_die);
11021 }
11022 }
11023 }
11024
11025 /* Read a Fortran module as type. This DIE can be only a declaration used for
11026 imported module. Still we need that type as local Fortran "use ... only"
11027 declaration imports depend on the created type in determine_prefix. */
11028
11029 static struct type *
11030 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11031 {
11032 struct objfile *objfile = cu->objfile;
11033 char *module_name;
11034 struct type *type;
11035
11036 module_name = dwarf2_name (die, cu);
11037 if (!module_name)
11038 complaint (&symfile_complaints,
11039 _("DW_TAG_module has no name, offset 0x%x"),
11040 die->offset.sect_off);
11041 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11042
11043 /* determine_prefix uses TYPE_TAG_NAME. */
11044 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11045
11046 return set_die_type (die, type, cu);
11047 }
11048
11049 /* Read a Fortran module. */
11050
11051 static void
11052 read_module (struct die_info *die, struct dwarf2_cu *cu)
11053 {
11054 struct die_info *child_die = die->child;
11055
11056 while (child_die && child_die->tag)
11057 {
11058 process_die (child_die, cu);
11059 child_die = sibling_die (child_die);
11060 }
11061 }
11062
11063 /* Return the name of the namespace represented by DIE. Set
11064 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11065 namespace. */
11066
11067 static const char *
11068 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11069 {
11070 struct die_info *current_die;
11071 const char *name = NULL;
11072
11073 /* Loop through the extensions until we find a name. */
11074
11075 for (current_die = die;
11076 current_die != NULL;
11077 current_die = dwarf2_extension (die, &cu))
11078 {
11079 name = dwarf2_name (current_die, cu);
11080 if (name != NULL)
11081 break;
11082 }
11083
11084 /* Is it an anonymous namespace? */
11085
11086 *is_anonymous = (name == NULL);
11087 if (*is_anonymous)
11088 name = CP_ANONYMOUS_NAMESPACE_STR;
11089
11090 return name;
11091 }
11092
11093 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11094 the user defined type vector. */
11095
11096 static struct type *
11097 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11098 {
11099 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11100 struct comp_unit_head *cu_header = &cu->header;
11101 struct type *type;
11102 struct attribute *attr_byte_size;
11103 struct attribute *attr_address_class;
11104 int byte_size, addr_class;
11105 struct type *target_type;
11106
11107 target_type = die_type (die, cu);
11108
11109 /* The die_type call above may have already set the type for this DIE. */
11110 type = get_die_type (die, cu);
11111 if (type)
11112 return type;
11113
11114 type = lookup_pointer_type (target_type);
11115
11116 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11117 if (attr_byte_size)
11118 byte_size = DW_UNSND (attr_byte_size);
11119 else
11120 byte_size = cu_header->addr_size;
11121
11122 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11123 if (attr_address_class)
11124 addr_class = DW_UNSND (attr_address_class);
11125 else
11126 addr_class = DW_ADDR_none;
11127
11128 /* If the pointer size or address class is different than the
11129 default, create a type variant marked as such and set the
11130 length accordingly. */
11131 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11132 {
11133 if (gdbarch_address_class_type_flags_p (gdbarch))
11134 {
11135 int type_flags;
11136
11137 type_flags = gdbarch_address_class_type_flags
11138 (gdbarch, byte_size, addr_class);
11139 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11140 == 0);
11141 type = make_type_with_address_space (type, type_flags);
11142 }
11143 else if (TYPE_LENGTH (type) != byte_size)
11144 {
11145 complaint (&symfile_complaints,
11146 _("invalid pointer size %d"), byte_size);
11147 }
11148 else
11149 {
11150 /* Should we also complain about unhandled address classes? */
11151 }
11152 }
11153
11154 TYPE_LENGTH (type) = byte_size;
11155 return set_die_type (die, type, cu);
11156 }
11157
11158 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11159 the user defined type vector. */
11160
11161 static struct type *
11162 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11163 {
11164 struct type *type;
11165 struct type *to_type;
11166 struct type *domain;
11167
11168 to_type = die_type (die, cu);
11169 domain = die_containing_type (die, cu);
11170
11171 /* The calls above may have already set the type for this DIE. */
11172 type = get_die_type (die, cu);
11173 if (type)
11174 return type;
11175
11176 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11177 type = lookup_methodptr_type (to_type);
11178 else
11179 type = lookup_memberptr_type (to_type, domain);
11180
11181 return set_die_type (die, type, cu);
11182 }
11183
11184 /* Extract all information from a DW_TAG_reference_type DIE and add to
11185 the user defined type vector. */
11186
11187 static struct type *
11188 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11189 {
11190 struct comp_unit_head *cu_header = &cu->header;
11191 struct type *type, *target_type;
11192 struct attribute *attr;
11193
11194 target_type = die_type (die, cu);
11195
11196 /* The die_type call above may have already set the type for this DIE. */
11197 type = get_die_type (die, cu);
11198 if (type)
11199 return type;
11200
11201 type = lookup_reference_type (target_type);
11202 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11203 if (attr)
11204 {
11205 TYPE_LENGTH (type) = DW_UNSND (attr);
11206 }
11207 else
11208 {
11209 TYPE_LENGTH (type) = cu_header->addr_size;
11210 }
11211 return set_die_type (die, type, cu);
11212 }
11213
11214 static struct type *
11215 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11216 {
11217 struct type *base_type, *cv_type;
11218
11219 base_type = die_type (die, cu);
11220
11221 /* The die_type call above may have already set the type for this DIE. */
11222 cv_type = get_die_type (die, cu);
11223 if (cv_type)
11224 return cv_type;
11225
11226 /* In case the const qualifier is applied to an array type, the element type
11227 is so qualified, not the array type (section 6.7.3 of C99). */
11228 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11229 {
11230 struct type *el_type, *inner_array;
11231
11232 base_type = copy_type (base_type);
11233 inner_array = base_type;
11234
11235 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11236 {
11237 TYPE_TARGET_TYPE (inner_array) =
11238 copy_type (TYPE_TARGET_TYPE (inner_array));
11239 inner_array = TYPE_TARGET_TYPE (inner_array);
11240 }
11241
11242 el_type = TYPE_TARGET_TYPE (inner_array);
11243 TYPE_TARGET_TYPE (inner_array) =
11244 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11245
11246 return set_die_type (die, base_type, cu);
11247 }
11248
11249 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11250 return set_die_type (die, cv_type, cu);
11251 }
11252
11253 static struct type *
11254 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11255 {
11256 struct type *base_type, *cv_type;
11257
11258 base_type = die_type (die, cu);
11259
11260 /* The die_type call above may have already set the type for this DIE. */
11261 cv_type = get_die_type (die, cu);
11262 if (cv_type)
11263 return cv_type;
11264
11265 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11266 return set_die_type (die, cv_type, cu);
11267 }
11268
11269 /* Extract all information from a DW_TAG_string_type DIE and add to
11270 the user defined type vector. It isn't really a user defined type,
11271 but it behaves like one, with other DIE's using an AT_user_def_type
11272 attribute to reference it. */
11273
11274 static struct type *
11275 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11276 {
11277 struct objfile *objfile = cu->objfile;
11278 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11279 struct type *type, *range_type, *index_type, *char_type;
11280 struct attribute *attr;
11281 unsigned int length;
11282
11283 attr = dwarf2_attr (die, DW_AT_string_length, cu);
11284 if (attr)
11285 {
11286 length = DW_UNSND (attr);
11287 }
11288 else
11289 {
11290 /* Check for the DW_AT_byte_size attribute. */
11291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11292 if (attr)
11293 {
11294 length = DW_UNSND (attr);
11295 }
11296 else
11297 {
11298 length = 1;
11299 }
11300 }
11301
11302 index_type = objfile_type (objfile)->builtin_int;
11303 range_type = create_range_type (NULL, index_type, 1, length);
11304 char_type = language_string_char_type (cu->language_defn, gdbarch);
11305 type = create_string_type (NULL, char_type, range_type);
11306
11307 return set_die_type (die, type, cu);
11308 }
11309
11310 /* Handle DIES due to C code like:
11311
11312 struct foo
11313 {
11314 int (*funcp)(int a, long l);
11315 int b;
11316 };
11317
11318 ('funcp' generates a DW_TAG_subroutine_type DIE). */
11319
11320 static struct type *
11321 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11322 {
11323 struct objfile *objfile = cu->objfile;
11324 struct type *type; /* Type that this function returns. */
11325 struct type *ftype; /* Function that returns above type. */
11326 struct attribute *attr;
11327
11328 type = die_type (die, cu);
11329
11330 /* The die_type call above may have already set the type for this DIE. */
11331 ftype = get_die_type (die, cu);
11332 if (ftype)
11333 return ftype;
11334
11335 ftype = lookup_function_type (type);
11336
11337 /* All functions in C++, Pascal and Java have prototypes. */
11338 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11339 if ((attr && (DW_UNSND (attr) != 0))
11340 || cu->language == language_cplus
11341 || cu->language == language_java
11342 || cu->language == language_pascal)
11343 TYPE_PROTOTYPED (ftype) = 1;
11344 else if (producer_is_realview (cu->producer))
11345 /* RealView does not emit DW_AT_prototyped. We can not
11346 distinguish prototyped and unprototyped functions; default to
11347 prototyped, since that is more common in modern code (and
11348 RealView warns about unprototyped functions). */
11349 TYPE_PROTOTYPED (ftype) = 1;
11350
11351 /* Store the calling convention in the type if it's available in
11352 the subroutine die. Otherwise set the calling convention to
11353 the default value DW_CC_normal. */
11354 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11355 if (attr)
11356 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11357 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11358 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11359 else
11360 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11361
11362 /* We need to add the subroutine type to the die immediately so
11363 we don't infinitely recurse when dealing with parameters
11364 declared as the same subroutine type. */
11365 set_die_type (die, ftype, cu);
11366
11367 if (die->child != NULL)
11368 {
11369 struct type *void_type = objfile_type (objfile)->builtin_void;
11370 struct die_info *child_die;
11371 int nparams, iparams;
11372
11373 /* Count the number of parameters.
11374 FIXME: GDB currently ignores vararg functions, but knows about
11375 vararg member functions. */
11376 nparams = 0;
11377 child_die = die->child;
11378 while (child_die && child_die->tag)
11379 {
11380 if (child_die->tag == DW_TAG_formal_parameter)
11381 nparams++;
11382 else if (child_die->tag == DW_TAG_unspecified_parameters)
11383 TYPE_VARARGS (ftype) = 1;
11384 child_die = sibling_die (child_die);
11385 }
11386
11387 /* Allocate storage for parameters and fill them in. */
11388 TYPE_NFIELDS (ftype) = nparams;
11389 TYPE_FIELDS (ftype) = (struct field *)
11390 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11391
11392 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11393 even if we error out during the parameters reading below. */
11394 for (iparams = 0; iparams < nparams; iparams++)
11395 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11396
11397 iparams = 0;
11398 child_die = die->child;
11399 while (child_die && child_die->tag)
11400 {
11401 if (child_die->tag == DW_TAG_formal_parameter)
11402 {
11403 struct type *arg_type;
11404
11405 /* DWARF version 2 has no clean way to discern C++
11406 static and non-static member functions. G++ helps
11407 GDB by marking the first parameter for non-static
11408 member functions (which is the this pointer) as
11409 artificial. We pass this information to
11410 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11411
11412 DWARF version 3 added DW_AT_object_pointer, which GCC
11413 4.5 does not yet generate. */
11414 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11415 if (attr)
11416 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11417 else
11418 {
11419 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11420
11421 /* GCC/43521: In java, the formal parameter
11422 "this" is sometimes not marked with DW_AT_artificial. */
11423 if (cu->language == language_java)
11424 {
11425 const char *name = dwarf2_name (child_die, cu);
11426
11427 if (name && !strcmp (name, "this"))
11428 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11429 }
11430 }
11431 arg_type = die_type (child_die, cu);
11432
11433 /* RealView does not mark THIS as const, which the testsuite
11434 expects. GCC marks THIS as const in method definitions,
11435 but not in the class specifications (GCC PR 43053). */
11436 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11437 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11438 {
11439 int is_this = 0;
11440 struct dwarf2_cu *arg_cu = cu;
11441 const char *name = dwarf2_name (child_die, cu);
11442
11443 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11444 if (attr)
11445 {
11446 /* If the compiler emits this, use it. */
11447 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11448 is_this = 1;
11449 }
11450 else if (name && strcmp (name, "this") == 0)
11451 /* Function definitions will have the argument names. */
11452 is_this = 1;
11453 else if (name == NULL && iparams == 0)
11454 /* Declarations may not have the names, so like
11455 elsewhere in GDB, assume an artificial first
11456 argument is "this". */
11457 is_this = 1;
11458
11459 if (is_this)
11460 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11461 arg_type, 0);
11462 }
11463
11464 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11465 iparams++;
11466 }
11467 child_die = sibling_die (child_die);
11468 }
11469 }
11470
11471 return ftype;
11472 }
11473
11474 static struct type *
11475 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11476 {
11477 struct objfile *objfile = cu->objfile;
11478 const char *name = NULL;
11479 struct type *this_type, *target_type;
11480
11481 name = dwarf2_full_name (NULL, die, cu);
11482 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11483 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11484 TYPE_NAME (this_type) = (char *) name;
11485 set_die_type (die, this_type, cu);
11486 target_type = die_type (die, cu);
11487 if (target_type != this_type)
11488 TYPE_TARGET_TYPE (this_type) = target_type;
11489 else
11490 {
11491 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11492 spec and cause infinite loops in GDB. */
11493 complaint (&symfile_complaints,
11494 _("Self-referential DW_TAG_typedef "
11495 "- DIE at 0x%x [in module %s]"),
11496 die->offset.sect_off, objfile->name);
11497 TYPE_TARGET_TYPE (this_type) = NULL;
11498 }
11499 return this_type;
11500 }
11501
11502 /* Find a representation of a given base type and install
11503 it in the TYPE field of the die. */
11504
11505 static struct type *
11506 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11507 {
11508 struct objfile *objfile = cu->objfile;
11509 struct type *type;
11510 struct attribute *attr;
11511 int encoding = 0, size = 0;
11512 char *name;
11513 enum type_code code = TYPE_CODE_INT;
11514 int type_flags = 0;
11515 struct type *target_type = NULL;
11516
11517 attr = dwarf2_attr (die, DW_AT_encoding, cu);
11518 if (attr)
11519 {
11520 encoding = DW_UNSND (attr);
11521 }
11522 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11523 if (attr)
11524 {
11525 size = DW_UNSND (attr);
11526 }
11527 name = dwarf2_name (die, cu);
11528 if (!name)
11529 {
11530 complaint (&symfile_complaints,
11531 _("DW_AT_name missing from DW_TAG_base_type"));
11532 }
11533
11534 switch (encoding)
11535 {
11536 case DW_ATE_address:
11537 /* Turn DW_ATE_address into a void * pointer. */
11538 code = TYPE_CODE_PTR;
11539 type_flags |= TYPE_FLAG_UNSIGNED;
11540 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11541 break;
11542 case DW_ATE_boolean:
11543 code = TYPE_CODE_BOOL;
11544 type_flags |= TYPE_FLAG_UNSIGNED;
11545 break;
11546 case DW_ATE_complex_float:
11547 code = TYPE_CODE_COMPLEX;
11548 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11549 break;
11550 case DW_ATE_decimal_float:
11551 code = TYPE_CODE_DECFLOAT;
11552 break;
11553 case DW_ATE_float:
11554 code = TYPE_CODE_FLT;
11555 break;
11556 case DW_ATE_signed:
11557 break;
11558 case DW_ATE_unsigned:
11559 type_flags |= TYPE_FLAG_UNSIGNED;
11560 if (cu->language == language_fortran
11561 && name
11562 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11563 code = TYPE_CODE_CHAR;
11564 break;
11565 case DW_ATE_signed_char:
11566 if (cu->language == language_ada || cu->language == language_m2
11567 || cu->language == language_pascal
11568 || cu->language == language_fortran)
11569 code = TYPE_CODE_CHAR;
11570 break;
11571 case DW_ATE_unsigned_char:
11572 if (cu->language == language_ada || cu->language == language_m2
11573 || cu->language == language_pascal
11574 || cu->language == language_fortran)
11575 code = TYPE_CODE_CHAR;
11576 type_flags |= TYPE_FLAG_UNSIGNED;
11577 break;
11578 case DW_ATE_UTF:
11579 /* We just treat this as an integer and then recognize the
11580 type by name elsewhere. */
11581 break;
11582
11583 default:
11584 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11585 dwarf_type_encoding_name (encoding));
11586 break;
11587 }
11588
11589 type = init_type (code, size, type_flags, NULL, objfile);
11590 TYPE_NAME (type) = name;
11591 TYPE_TARGET_TYPE (type) = target_type;
11592
11593 if (name && strcmp (name, "char") == 0)
11594 TYPE_NOSIGN (type) = 1;
11595
11596 return set_die_type (die, type, cu);
11597 }
11598
11599 /* Read the given DW_AT_subrange DIE. */
11600
11601 static struct type *
11602 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11603 {
11604 struct type *base_type;
11605 struct type *range_type;
11606 struct attribute *attr;
11607 LONGEST low, high;
11608 int low_default_is_valid;
11609 char *name;
11610 LONGEST negative_mask;
11611
11612 base_type = die_type (die, cu);
11613 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11614 check_typedef (base_type);
11615
11616 /* The die_type call above may have already set the type for this DIE. */
11617 range_type = get_die_type (die, cu);
11618 if (range_type)
11619 return range_type;
11620
11621 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11622 omitting DW_AT_lower_bound. */
11623 switch (cu->language)
11624 {
11625 case language_c:
11626 case language_cplus:
11627 low = 0;
11628 low_default_is_valid = 1;
11629 break;
11630 case language_fortran:
11631 low = 1;
11632 low_default_is_valid = 1;
11633 break;
11634 case language_d:
11635 case language_java:
11636 case language_objc:
11637 low = 0;
11638 low_default_is_valid = (cu->header.version >= 4);
11639 break;
11640 case language_ada:
11641 case language_m2:
11642 case language_pascal:
11643 low = 1;
11644 low_default_is_valid = (cu->header.version >= 4);
11645 break;
11646 default:
11647 low = 0;
11648 low_default_is_valid = 0;
11649 break;
11650 }
11651
11652 /* FIXME: For variable sized arrays either of these could be
11653 a variable rather than a constant value. We'll allow it,
11654 but we don't know how to handle it. */
11655 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11656 if (attr)
11657 low = dwarf2_get_attr_constant_value (attr, low);
11658 else if (!low_default_is_valid)
11659 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11660 "- DIE at 0x%x [in module %s]"),
11661 die->offset.sect_off, cu->objfile->name);
11662
11663 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11664 if (attr)
11665 {
11666 if (attr_form_is_block (attr) || is_ref_attr (attr))
11667 {
11668 /* GCC encodes arrays with unspecified or dynamic length
11669 with a DW_FORM_block1 attribute or a reference attribute.
11670 FIXME: GDB does not yet know how to handle dynamic
11671 arrays properly, treat them as arrays with unspecified
11672 length for now.
11673
11674 FIXME: jimb/2003-09-22: GDB does not really know
11675 how to handle arrays of unspecified length
11676 either; we just represent them as zero-length
11677 arrays. Choose an appropriate upper bound given
11678 the lower bound we've computed above. */
11679 high = low - 1;
11680 }
11681 else
11682 high = dwarf2_get_attr_constant_value (attr, 1);
11683 }
11684 else
11685 {
11686 attr = dwarf2_attr (die, DW_AT_count, cu);
11687 if (attr)
11688 {
11689 int count = dwarf2_get_attr_constant_value (attr, 1);
11690 high = low + count - 1;
11691 }
11692 else
11693 {
11694 /* Unspecified array length. */
11695 high = low - 1;
11696 }
11697 }
11698
11699 /* Dwarf-2 specifications explicitly allows to create subrange types
11700 without specifying a base type.
11701 In that case, the base type must be set to the type of
11702 the lower bound, upper bound or count, in that order, if any of these
11703 three attributes references an object that has a type.
11704 If no base type is found, the Dwarf-2 specifications say that
11705 a signed integer type of size equal to the size of an address should
11706 be used.
11707 For the following C code: `extern char gdb_int [];'
11708 GCC produces an empty range DIE.
11709 FIXME: muller/2010-05-28: Possible references to object for low bound,
11710 high bound or count are not yet handled by this code. */
11711 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11712 {
11713 struct objfile *objfile = cu->objfile;
11714 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11715 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11716 struct type *int_type = objfile_type (objfile)->builtin_int;
11717
11718 /* Test "int", "long int", and "long long int" objfile types,
11719 and select the first one having a size above or equal to the
11720 architecture address size. */
11721 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11722 base_type = int_type;
11723 else
11724 {
11725 int_type = objfile_type (objfile)->builtin_long;
11726 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11727 base_type = int_type;
11728 else
11729 {
11730 int_type = objfile_type (objfile)->builtin_long_long;
11731 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11732 base_type = int_type;
11733 }
11734 }
11735 }
11736
11737 negative_mask =
11738 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11739 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11740 low |= negative_mask;
11741 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11742 high |= negative_mask;
11743
11744 range_type = create_range_type (NULL, base_type, low, high);
11745
11746 /* Mark arrays with dynamic length at least as an array of unspecified
11747 length. GDB could check the boundary but before it gets implemented at
11748 least allow accessing the array elements. */
11749 if (attr && attr_form_is_block (attr))
11750 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11751
11752 /* Ada expects an empty array on no boundary attributes. */
11753 if (attr == NULL && cu->language != language_ada)
11754 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11755
11756 name = dwarf2_name (die, cu);
11757 if (name)
11758 TYPE_NAME (range_type) = name;
11759
11760 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11761 if (attr)
11762 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11763
11764 set_die_type (die, range_type, cu);
11765
11766 /* set_die_type should be already done. */
11767 set_descriptive_type (range_type, die, cu);
11768
11769 return range_type;
11770 }
11771
11772 static struct type *
11773 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11774 {
11775 struct type *type;
11776
11777 /* For now, we only support the C meaning of an unspecified type: void. */
11778
11779 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11780 TYPE_NAME (type) = dwarf2_name (die, cu);
11781
11782 return set_die_type (die, type, cu);
11783 }
11784
11785 /* Read a single die and all its descendents. Set the die's sibling
11786 field to NULL; set other fields in the die correctly, and set all
11787 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11788 location of the info_ptr after reading all of those dies. PARENT
11789 is the parent of the die in question. */
11790
11791 static struct die_info *
11792 read_die_and_children (const struct die_reader_specs *reader,
11793 gdb_byte *info_ptr,
11794 gdb_byte **new_info_ptr,
11795 struct die_info *parent)
11796 {
11797 struct die_info *die;
11798 gdb_byte *cur_ptr;
11799 int has_children;
11800
11801 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11802 if (die == NULL)
11803 {
11804 *new_info_ptr = cur_ptr;
11805 return NULL;
11806 }
11807 store_in_ref_table (die, reader->cu);
11808
11809 if (has_children)
11810 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11811 else
11812 {
11813 die->child = NULL;
11814 *new_info_ptr = cur_ptr;
11815 }
11816
11817 die->sibling = NULL;
11818 die->parent = parent;
11819 return die;
11820 }
11821
11822 /* Read a die, all of its descendents, and all of its siblings; set
11823 all of the fields of all of the dies correctly. Arguments are as
11824 in read_die_and_children. */
11825
11826 static struct die_info *
11827 read_die_and_siblings (const struct die_reader_specs *reader,
11828 gdb_byte *info_ptr,
11829 gdb_byte **new_info_ptr,
11830 struct die_info *parent)
11831 {
11832 struct die_info *first_die, *last_sibling;
11833 gdb_byte *cur_ptr;
11834
11835 cur_ptr = info_ptr;
11836 first_die = last_sibling = NULL;
11837
11838 while (1)
11839 {
11840 struct die_info *die
11841 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
11842
11843 if (die == NULL)
11844 {
11845 *new_info_ptr = cur_ptr;
11846 return first_die;
11847 }
11848
11849 if (!first_die)
11850 first_die = die;
11851 else
11852 last_sibling->sibling = die;
11853
11854 last_sibling = die;
11855 }
11856 }
11857
11858 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
11859 attributes.
11860 The caller is responsible for filling in the extra attributes
11861 and updating (*DIEP)->num_attrs.
11862 Set DIEP to point to a newly allocated die with its information,
11863 except for its child, sibling, and parent fields.
11864 Set HAS_CHILDREN to tell whether the die has children or not. */
11865
11866 static gdb_byte *
11867 read_full_die_1 (const struct die_reader_specs *reader,
11868 struct die_info **diep, gdb_byte *info_ptr,
11869 int *has_children, int num_extra_attrs)
11870 {
11871 unsigned int abbrev_number, bytes_read, i;
11872 sect_offset offset;
11873 struct abbrev_info *abbrev;
11874 struct die_info *die;
11875 struct dwarf2_cu *cu = reader->cu;
11876 bfd *abfd = reader->abfd;
11877
11878 offset.sect_off = info_ptr - reader->buffer;
11879 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11880 info_ptr += bytes_read;
11881 if (!abbrev_number)
11882 {
11883 *diep = NULL;
11884 *has_children = 0;
11885 return info_ptr;
11886 }
11887
11888 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
11889 if (!abbrev)
11890 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
11891 abbrev_number,
11892 bfd_get_filename (abfd));
11893
11894 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
11895 die->offset = offset;
11896 die->tag = abbrev->tag;
11897 die->abbrev = abbrev_number;
11898
11899 /* Make the result usable.
11900 The caller needs to update num_attrs after adding the extra
11901 attributes. */
11902 die->num_attrs = abbrev->num_attrs;
11903
11904 for (i = 0; i < abbrev->num_attrs; ++i)
11905 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
11906 info_ptr);
11907
11908 *diep = die;
11909 *has_children = abbrev->has_children;
11910 return info_ptr;
11911 }
11912
11913 /* Read a die and all its attributes.
11914 Set DIEP to point to a newly allocated die with its information,
11915 except for its child, sibling, and parent fields.
11916 Set HAS_CHILDREN to tell whether the die has children or not. */
11917
11918 static gdb_byte *
11919 read_full_die (const struct die_reader_specs *reader,
11920 struct die_info **diep, gdb_byte *info_ptr,
11921 int *has_children)
11922 {
11923 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
11924 }
11925 \f
11926 /* Abbreviation tables.
11927
11928 In DWARF version 2, the description of the debugging information is
11929 stored in a separate .debug_abbrev section. Before we read any
11930 dies from a section we read in all abbreviations and install them
11931 in a hash table. */
11932
11933 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
11934
11935 static struct abbrev_info *
11936 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
11937 {
11938 struct abbrev_info *abbrev;
11939
11940 abbrev = (struct abbrev_info *)
11941 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
11942 memset (abbrev, 0, sizeof (struct abbrev_info));
11943 return abbrev;
11944 }
11945
11946 /* Add an abbreviation to the table. */
11947
11948 static void
11949 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11950 unsigned int abbrev_number,
11951 struct abbrev_info *abbrev)
11952 {
11953 unsigned int hash_number;
11954
11955 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11956 abbrev->next = abbrev_table->abbrevs[hash_number];
11957 abbrev_table->abbrevs[hash_number] = abbrev;
11958 }
11959
11960 /* Look up an abbrev in the table.
11961 Returns NULL if the abbrev is not found. */
11962
11963 static struct abbrev_info *
11964 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11965 unsigned int abbrev_number)
11966 {
11967 unsigned int hash_number;
11968 struct abbrev_info *abbrev;
11969
11970 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11971 abbrev = abbrev_table->abbrevs[hash_number];
11972
11973 while (abbrev)
11974 {
11975 if (abbrev->number == abbrev_number)
11976 return abbrev;
11977 abbrev = abbrev->next;
11978 }
11979 return NULL;
11980 }
11981
11982 /* Read in an abbrev table. */
11983
11984 static struct abbrev_table *
11985 abbrev_table_read_table (struct dwarf2_section_info *section,
11986 sect_offset offset)
11987 {
11988 struct objfile *objfile = dwarf2_per_objfile->objfile;
11989 bfd *abfd = section->asection->owner;
11990 struct abbrev_table *abbrev_table;
11991 gdb_byte *abbrev_ptr;
11992 struct abbrev_info *cur_abbrev;
11993 unsigned int abbrev_number, bytes_read, abbrev_name;
11994 unsigned int abbrev_form;
11995 struct attr_abbrev *cur_attrs;
11996 unsigned int allocated_attrs;
11997
11998 abbrev_table = XMALLOC (struct abbrev_table);
11999 abbrev_table->offset = offset;
12000 obstack_init (&abbrev_table->abbrev_obstack);
12001 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12002 (ABBREV_HASH_SIZE
12003 * sizeof (struct abbrev_info *)));
12004 memset (abbrev_table->abbrevs, 0,
12005 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12006
12007 dwarf2_read_section (objfile, section);
12008 abbrev_ptr = section->buffer + offset.sect_off;
12009 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12010 abbrev_ptr += bytes_read;
12011
12012 allocated_attrs = ATTR_ALLOC_CHUNK;
12013 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12014
12015 /* Loop until we reach an abbrev number of 0. */
12016 while (abbrev_number)
12017 {
12018 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12019
12020 /* read in abbrev header */
12021 cur_abbrev->number = abbrev_number;
12022 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12023 abbrev_ptr += bytes_read;
12024 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12025 abbrev_ptr += 1;
12026
12027 /* now read in declarations */
12028 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12029 abbrev_ptr += bytes_read;
12030 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12031 abbrev_ptr += bytes_read;
12032 while (abbrev_name)
12033 {
12034 if (cur_abbrev->num_attrs == allocated_attrs)
12035 {
12036 allocated_attrs += ATTR_ALLOC_CHUNK;
12037 cur_attrs
12038 = xrealloc (cur_attrs, (allocated_attrs
12039 * sizeof (struct attr_abbrev)));
12040 }
12041
12042 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12043 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12044 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12045 abbrev_ptr += bytes_read;
12046 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12047 abbrev_ptr += bytes_read;
12048 }
12049
12050 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12051 (cur_abbrev->num_attrs
12052 * sizeof (struct attr_abbrev)));
12053 memcpy (cur_abbrev->attrs, cur_attrs,
12054 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12055
12056 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12057
12058 /* Get next abbreviation.
12059 Under Irix6 the abbreviations for a compilation unit are not
12060 always properly terminated with an abbrev number of 0.
12061 Exit loop if we encounter an abbreviation which we have
12062 already read (which means we are about to read the abbreviations
12063 for the next compile unit) or if the end of the abbreviation
12064 table is reached. */
12065 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12066 break;
12067 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12068 abbrev_ptr += bytes_read;
12069 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12070 break;
12071 }
12072
12073 xfree (cur_attrs);
12074 return abbrev_table;
12075 }
12076
12077 /* Free the resources held by ABBREV_TABLE. */
12078
12079 static void
12080 abbrev_table_free (struct abbrev_table *abbrev_table)
12081 {
12082 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12083 xfree (abbrev_table);
12084 }
12085
12086 /* Same as abbrev_table_free but as a cleanup.
12087 We pass in a pointer to the pointer to the table so that we can
12088 set the pointer to NULL when we're done. It also simplifies
12089 build_type_unit_groups. */
12090
12091 static void
12092 abbrev_table_free_cleanup (void *table_ptr)
12093 {
12094 struct abbrev_table **abbrev_table_ptr = table_ptr;
12095
12096 if (*abbrev_table_ptr != NULL)
12097 abbrev_table_free (*abbrev_table_ptr);
12098 *abbrev_table_ptr = NULL;
12099 }
12100
12101 /* Read the abbrev table for CU from ABBREV_SECTION. */
12102
12103 static void
12104 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12105 struct dwarf2_section_info *abbrev_section)
12106 {
12107 cu->abbrev_table =
12108 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12109 }
12110
12111 /* Release the memory used by the abbrev table for a compilation unit. */
12112
12113 static void
12114 dwarf2_free_abbrev_table (void *ptr_to_cu)
12115 {
12116 struct dwarf2_cu *cu = ptr_to_cu;
12117
12118 abbrev_table_free (cu->abbrev_table);
12119 /* Set this to NULL so that we SEGV if we try to read it later,
12120 and also because free_comp_unit verifies this is NULL. */
12121 cu->abbrev_table = NULL;
12122 }
12123 \f
12124 /* Returns nonzero if TAG represents a type that we might generate a partial
12125 symbol for. */
12126
12127 static int
12128 is_type_tag_for_partial (int tag)
12129 {
12130 switch (tag)
12131 {
12132 #if 0
12133 /* Some types that would be reasonable to generate partial symbols for,
12134 that we don't at present. */
12135 case DW_TAG_array_type:
12136 case DW_TAG_file_type:
12137 case DW_TAG_ptr_to_member_type:
12138 case DW_TAG_set_type:
12139 case DW_TAG_string_type:
12140 case DW_TAG_subroutine_type:
12141 #endif
12142 case DW_TAG_base_type:
12143 case DW_TAG_class_type:
12144 case DW_TAG_interface_type:
12145 case DW_TAG_enumeration_type:
12146 case DW_TAG_structure_type:
12147 case DW_TAG_subrange_type:
12148 case DW_TAG_typedef:
12149 case DW_TAG_union_type:
12150 return 1;
12151 default:
12152 return 0;
12153 }
12154 }
12155
12156 /* Load all DIEs that are interesting for partial symbols into memory. */
12157
12158 static struct partial_die_info *
12159 load_partial_dies (const struct die_reader_specs *reader,
12160 gdb_byte *info_ptr, int building_psymtab)
12161 {
12162 struct dwarf2_cu *cu = reader->cu;
12163 struct objfile *objfile = cu->objfile;
12164 struct partial_die_info *part_die;
12165 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12166 struct abbrev_info *abbrev;
12167 unsigned int bytes_read;
12168 unsigned int load_all = 0;
12169 int nesting_level = 1;
12170
12171 parent_die = NULL;
12172 last_die = NULL;
12173
12174 gdb_assert (cu->per_cu != NULL);
12175 if (cu->per_cu->load_all_dies)
12176 load_all = 1;
12177
12178 cu->partial_dies
12179 = htab_create_alloc_ex (cu->header.length / 12,
12180 partial_die_hash,
12181 partial_die_eq,
12182 NULL,
12183 &cu->comp_unit_obstack,
12184 hashtab_obstack_allocate,
12185 dummy_obstack_deallocate);
12186
12187 part_die = obstack_alloc (&cu->comp_unit_obstack,
12188 sizeof (struct partial_die_info));
12189
12190 while (1)
12191 {
12192 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12193
12194 /* A NULL abbrev means the end of a series of children. */
12195 if (abbrev == NULL)
12196 {
12197 if (--nesting_level == 0)
12198 {
12199 /* PART_DIE was probably the last thing allocated on the
12200 comp_unit_obstack, so we could call obstack_free
12201 here. We don't do that because the waste is small,
12202 and will be cleaned up when we're done with this
12203 compilation unit. This way, we're also more robust
12204 against other users of the comp_unit_obstack. */
12205 return first_die;
12206 }
12207 info_ptr += bytes_read;
12208 last_die = parent_die;
12209 parent_die = parent_die->die_parent;
12210 continue;
12211 }
12212
12213 /* Check for template arguments. We never save these; if
12214 they're seen, we just mark the parent, and go on our way. */
12215 if (parent_die != NULL
12216 && cu->language == language_cplus
12217 && (abbrev->tag == DW_TAG_template_type_param
12218 || abbrev->tag == DW_TAG_template_value_param))
12219 {
12220 parent_die->has_template_arguments = 1;
12221
12222 if (!load_all)
12223 {
12224 /* We don't need a partial DIE for the template argument. */
12225 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12226 continue;
12227 }
12228 }
12229
12230 /* We only recurse into c++ subprograms looking for template arguments.
12231 Skip their other children. */
12232 if (!load_all
12233 && cu->language == language_cplus
12234 && parent_die != NULL
12235 && parent_die->tag == DW_TAG_subprogram)
12236 {
12237 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12238 continue;
12239 }
12240
12241 /* Check whether this DIE is interesting enough to save. Normally
12242 we would not be interested in members here, but there may be
12243 later variables referencing them via DW_AT_specification (for
12244 static members). */
12245 if (!load_all
12246 && !is_type_tag_for_partial (abbrev->tag)
12247 && abbrev->tag != DW_TAG_constant
12248 && abbrev->tag != DW_TAG_enumerator
12249 && abbrev->tag != DW_TAG_subprogram
12250 && abbrev->tag != DW_TAG_lexical_block
12251 && abbrev->tag != DW_TAG_variable
12252 && abbrev->tag != DW_TAG_namespace
12253 && abbrev->tag != DW_TAG_module
12254 && abbrev->tag != DW_TAG_member
12255 && abbrev->tag != DW_TAG_imported_unit)
12256 {
12257 /* Otherwise we skip to the next sibling, if any. */
12258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12259 continue;
12260 }
12261
12262 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12263 info_ptr);
12264
12265 /* This two-pass algorithm for processing partial symbols has a
12266 high cost in cache pressure. Thus, handle some simple cases
12267 here which cover the majority of C partial symbols. DIEs
12268 which neither have specification tags in them, nor could have
12269 specification tags elsewhere pointing at them, can simply be
12270 processed and discarded.
12271
12272 This segment is also optional; scan_partial_symbols and
12273 add_partial_symbol will handle these DIEs if we chain
12274 them in normally. When compilers which do not emit large
12275 quantities of duplicate debug information are more common,
12276 this code can probably be removed. */
12277
12278 /* Any complete simple types at the top level (pretty much all
12279 of them, for a language without namespaces), can be processed
12280 directly. */
12281 if (parent_die == NULL
12282 && part_die->has_specification == 0
12283 && part_die->is_declaration == 0
12284 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12285 || part_die->tag == DW_TAG_base_type
12286 || part_die->tag == DW_TAG_subrange_type))
12287 {
12288 if (building_psymtab && part_die->name != NULL)
12289 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12290 VAR_DOMAIN, LOC_TYPEDEF,
12291 &objfile->static_psymbols,
12292 0, (CORE_ADDR) 0, cu->language, objfile);
12293 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12294 continue;
12295 }
12296
12297 /* The exception for DW_TAG_typedef with has_children above is
12298 a workaround of GCC PR debug/47510. In the case of this complaint
12299 type_name_no_tag_or_error will error on such types later.
12300
12301 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12302 it could not find the child DIEs referenced later, this is checked
12303 above. In correct DWARF DW_TAG_typedef should have no children. */
12304
12305 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12306 complaint (&symfile_complaints,
12307 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12308 "- DIE at 0x%x [in module %s]"),
12309 part_die->offset.sect_off, objfile->name);
12310
12311 /* If we're at the second level, and we're an enumerator, and
12312 our parent has no specification (meaning possibly lives in a
12313 namespace elsewhere), then we can add the partial symbol now
12314 instead of queueing it. */
12315 if (part_die->tag == DW_TAG_enumerator
12316 && parent_die != NULL
12317 && parent_die->die_parent == NULL
12318 && parent_die->tag == DW_TAG_enumeration_type
12319 && parent_die->has_specification == 0)
12320 {
12321 if (part_die->name == NULL)
12322 complaint (&symfile_complaints,
12323 _("malformed enumerator DIE ignored"));
12324 else if (building_psymtab)
12325 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12326 VAR_DOMAIN, LOC_CONST,
12327 (cu->language == language_cplus
12328 || cu->language == language_java)
12329 ? &objfile->global_psymbols
12330 : &objfile->static_psymbols,
12331 0, (CORE_ADDR) 0, cu->language, objfile);
12332
12333 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12334 continue;
12335 }
12336
12337 /* We'll save this DIE so link it in. */
12338 part_die->die_parent = parent_die;
12339 part_die->die_sibling = NULL;
12340 part_die->die_child = NULL;
12341
12342 if (last_die && last_die == parent_die)
12343 last_die->die_child = part_die;
12344 else if (last_die)
12345 last_die->die_sibling = part_die;
12346
12347 last_die = part_die;
12348
12349 if (first_die == NULL)
12350 first_die = part_die;
12351
12352 /* Maybe add the DIE to the hash table. Not all DIEs that we
12353 find interesting need to be in the hash table, because we
12354 also have the parent/sibling/child chains; only those that we
12355 might refer to by offset later during partial symbol reading.
12356
12357 For now this means things that might have be the target of a
12358 DW_AT_specification, DW_AT_abstract_origin, or
12359 DW_AT_extension. DW_AT_extension will refer only to
12360 namespaces; DW_AT_abstract_origin refers to functions (and
12361 many things under the function DIE, but we do not recurse
12362 into function DIEs during partial symbol reading) and
12363 possibly variables as well; DW_AT_specification refers to
12364 declarations. Declarations ought to have the DW_AT_declaration
12365 flag. It happens that GCC forgets to put it in sometimes, but
12366 only for functions, not for types.
12367
12368 Adding more things than necessary to the hash table is harmless
12369 except for the performance cost. Adding too few will result in
12370 wasted time in find_partial_die, when we reread the compilation
12371 unit with load_all_dies set. */
12372
12373 if (load_all
12374 || abbrev->tag == DW_TAG_constant
12375 || abbrev->tag == DW_TAG_subprogram
12376 || abbrev->tag == DW_TAG_variable
12377 || abbrev->tag == DW_TAG_namespace
12378 || part_die->is_declaration)
12379 {
12380 void **slot;
12381
12382 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12383 part_die->offset.sect_off, INSERT);
12384 *slot = part_die;
12385 }
12386
12387 part_die = obstack_alloc (&cu->comp_unit_obstack,
12388 sizeof (struct partial_die_info));
12389
12390 /* For some DIEs we want to follow their children (if any). For C
12391 we have no reason to follow the children of structures; for other
12392 languages we have to, so that we can get at method physnames
12393 to infer fully qualified class names, for DW_AT_specification,
12394 and for C++ template arguments. For C++, we also look one level
12395 inside functions to find template arguments (if the name of the
12396 function does not already contain the template arguments).
12397
12398 For Ada, we need to scan the children of subprograms and lexical
12399 blocks as well because Ada allows the definition of nested
12400 entities that could be interesting for the debugger, such as
12401 nested subprograms for instance. */
12402 if (last_die->has_children
12403 && (load_all
12404 || last_die->tag == DW_TAG_namespace
12405 || last_die->tag == DW_TAG_module
12406 || last_die->tag == DW_TAG_enumeration_type
12407 || (cu->language == language_cplus
12408 && last_die->tag == DW_TAG_subprogram
12409 && (last_die->name == NULL
12410 || strchr (last_die->name, '<') == NULL))
12411 || (cu->language != language_c
12412 && (last_die->tag == DW_TAG_class_type
12413 || last_die->tag == DW_TAG_interface_type
12414 || last_die->tag == DW_TAG_structure_type
12415 || last_die->tag == DW_TAG_union_type))
12416 || (cu->language == language_ada
12417 && (last_die->tag == DW_TAG_subprogram
12418 || last_die->tag == DW_TAG_lexical_block))))
12419 {
12420 nesting_level++;
12421 parent_die = last_die;
12422 continue;
12423 }
12424
12425 /* Otherwise we skip to the next sibling, if any. */
12426 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12427
12428 /* Back to the top, do it again. */
12429 }
12430 }
12431
12432 /* Read a minimal amount of information into the minimal die structure. */
12433
12434 static gdb_byte *
12435 read_partial_die (const struct die_reader_specs *reader,
12436 struct partial_die_info *part_die,
12437 struct abbrev_info *abbrev, unsigned int abbrev_len,
12438 gdb_byte *info_ptr)
12439 {
12440 struct dwarf2_cu *cu = reader->cu;
12441 struct objfile *objfile = cu->objfile;
12442 gdb_byte *buffer = reader->buffer;
12443 unsigned int i;
12444 struct attribute attr;
12445 int has_low_pc_attr = 0;
12446 int has_high_pc_attr = 0;
12447 int high_pc_relative = 0;
12448
12449 memset (part_die, 0, sizeof (struct partial_die_info));
12450
12451 part_die->offset.sect_off = info_ptr - buffer;
12452
12453 info_ptr += abbrev_len;
12454
12455 if (abbrev == NULL)
12456 return info_ptr;
12457
12458 part_die->tag = abbrev->tag;
12459 part_die->has_children = abbrev->has_children;
12460
12461 for (i = 0; i < abbrev->num_attrs; ++i)
12462 {
12463 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12464
12465 /* Store the data if it is of an attribute we want to keep in a
12466 partial symbol table. */
12467 switch (attr.name)
12468 {
12469 case DW_AT_name:
12470 switch (part_die->tag)
12471 {
12472 case DW_TAG_compile_unit:
12473 case DW_TAG_partial_unit:
12474 case DW_TAG_type_unit:
12475 /* Compilation units have a DW_AT_name that is a filename, not
12476 a source language identifier. */
12477 case DW_TAG_enumeration_type:
12478 case DW_TAG_enumerator:
12479 /* These tags always have simple identifiers already; no need
12480 to canonicalize them. */
12481 part_die->name = DW_STRING (&attr);
12482 break;
12483 default:
12484 part_die->name
12485 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12486 &objfile->objfile_obstack);
12487 break;
12488 }
12489 break;
12490 case DW_AT_linkage_name:
12491 case DW_AT_MIPS_linkage_name:
12492 /* Note that both forms of linkage name might appear. We
12493 assume they will be the same, and we only store the last
12494 one we see. */
12495 if (cu->language == language_ada)
12496 part_die->name = DW_STRING (&attr);
12497 part_die->linkage_name = DW_STRING (&attr);
12498 break;
12499 case DW_AT_low_pc:
12500 has_low_pc_attr = 1;
12501 part_die->lowpc = DW_ADDR (&attr);
12502 break;
12503 case DW_AT_high_pc:
12504 has_high_pc_attr = 1;
12505 if (attr.form == DW_FORM_addr
12506 || attr.form == DW_FORM_GNU_addr_index)
12507 part_die->highpc = DW_ADDR (&attr);
12508 else
12509 {
12510 high_pc_relative = 1;
12511 part_die->highpc = DW_UNSND (&attr);
12512 }
12513 break;
12514 case DW_AT_location:
12515 /* Support the .debug_loc offsets. */
12516 if (attr_form_is_block (&attr))
12517 {
12518 part_die->d.locdesc = DW_BLOCK (&attr);
12519 }
12520 else if (attr_form_is_section_offset (&attr))
12521 {
12522 dwarf2_complex_location_expr_complaint ();
12523 }
12524 else
12525 {
12526 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12527 "partial symbol information");
12528 }
12529 break;
12530 case DW_AT_external:
12531 part_die->is_external = DW_UNSND (&attr);
12532 break;
12533 case DW_AT_declaration:
12534 part_die->is_declaration = DW_UNSND (&attr);
12535 break;
12536 case DW_AT_type:
12537 part_die->has_type = 1;
12538 break;
12539 case DW_AT_abstract_origin:
12540 case DW_AT_specification:
12541 case DW_AT_extension:
12542 part_die->has_specification = 1;
12543 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12544 break;
12545 case DW_AT_sibling:
12546 /* Ignore absolute siblings, they might point outside of
12547 the current compile unit. */
12548 if (attr.form == DW_FORM_ref_addr)
12549 complaint (&symfile_complaints,
12550 _("ignoring absolute DW_AT_sibling"));
12551 else
12552 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12553 break;
12554 case DW_AT_byte_size:
12555 part_die->has_byte_size = 1;
12556 break;
12557 case DW_AT_calling_convention:
12558 /* DWARF doesn't provide a way to identify a program's source-level
12559 entry point. DW_AT_calling_convention attributes are only meant
12560 to describe functions' calling conventions.
12561
12562 However, because it's a necessary piece of information in
12563 Fortran, and because DW_CC_program is the only piece of debugging
12564 information whose definition refers to a 'main program' at all,
12565 several compilers have begun marking Fortran main programs with
12566 DW_CC_program --- even when those functions use the standard
12567 calling conventions.
12568
12569 So until DWARF specifies a way to provide this information and
12570 compilers pick up the new representation, we'll support this
12571 practice. */
12572 if (DW_UNSND (&attr) == DW_CC_program
12573 && cu->language == language_fortran)
12574 {
12575 set_main_name (part_die->name);
12576
12577 /* As this DIE has a static linkage the name would be difficult
12578 to look up later. */
12579 language_of_main = language_fortran;
12580 }
12581 break;
12582 case DW_AT_inline:
12583 if (DW_UNSND (&attr) == DW_INL_inlined
12584 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12585 part_die->may_be_inlined = 1;
12586 break;
12587
12588 case DW_AT_import:
12589 if (part_die->tag == DW_TAG_imported_unit)
12590 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12591 break;
12592
12593 default:
12594 break;
12595 }
12596 }
12597
12598 if (high_pc_relative)
12599 part_die->highpc += part_die->lowpc;
12600
12601 if (has_low_pc_attr && has_high_pc_attr)
12602 {
12603 /* When using the GNU linker, .gnu.linkonce. sections are used to
12604 eliminate duplicate copies of functions and vtables and such.
12605 The linker will arbitrarily choose one and discard the others.
12606 The AT_*_pc values for such functions refer to local labels in
12607 these sections. If the section from that file was discarded, the
12608 labels are not in the output, so the relocs get a value of 0.
12609 If this is a discarded function, mark the pc bounds as invalid,
12610 so that GDB will ignore it. */
12611 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12612 {
12613 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12614
12615 complaint (&symfile_complaints,
12616 _("DW_AT_low_pc %s is zero "
12617 "for DIE at 0x%x [in module %s]"),
12618 paddress (gdbarch, part_die->lowpc),
12619 part_die->offset.sect_off, objfile->name);
12620 }
12621 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12622 else if (part_die->lowpc >= part_die->highpc)
12623 {
12624 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12625
12626 complaint (&symfile_complaints,
12627 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12628 "for DIE at 0x%x [in module %s]"),
12629 paddress (gdbarch, part_die->lowpc),
12630 paddress (gdbarch, part_die->highpc),
12631 part_die->offset.sect_off, objfile->name);
12632 }
12633 else
12634 part_die->has_pc_info = 1;
12635 }
12636
12637 return info_ptr;
12638 }
12639
12640 /* Find a cached partial DIE at OFFSET in CU. */
12641
12642 static struct partial_die_info *
12643 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12644 {
12645 struct partial_die_info *lookup_die = NULL;
12646 struct partial_die_info part_die;
12647
12648 part_die.offset = offset;
12649 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12650 offset.sect_off);
12651
12652 return lookup_die;
12653 }
12654
12655 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12656 except in the case of .debug_types DIEs which do not reference
12657 outside their CU (they do however referencing other types via
12658 DW_FORM_ref_sig8). */
12659
12660 static struct partial_die_info *
12661 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
12662 {
12663 struct objfile *objfile = cu->objfile;
12664 struct dwarf2_per_cu_data *per_cu = NULL;
12665 struct partial_die_info *pd = NULL;
12666
12667 if (offset_in_cu_p (&cu->header, offset))
12668 {
12669 pd = find_partial_die_in_comp_unit (offset, cu);
12670 if (pd != NULL)
12671 return pd;
12672 /* We missed recording what we needed.
12673 Load all dies and try again. */
12674 per_cu = cu->per_cu;
12675 }
12676 else
12677 {
12678 /* TUs don't reference other CUs/TUs (except via type signatures). */
12679 if (cu->per_cu->is_debug_types)
12680 {
12681 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12682 " external reference to offset 0x%lx [in module %s].\n"),
12683 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12684 bfd_get_filename (objfile->obfd));
12685 }
12686 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
12687
12688 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12689 load_partial_comp_unit (per_cu);
12690
12691 per_cu->cu->last_used = 0;
12692 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12693 }
12694
12695 /* If we didn't find it, and not all dies have been loaded,
12696 load them all and try again. */
12697
12698 if (pd == NULL && per_cu->load_all_dies == 0)
12699 {
12700 per_cu->load_all_dies = 1;
12701
12702 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12703 THIS_CU->cu may already be in use. So we can't just free it and
12704 replace its DIEs with the ones we read in. Instead, we leave those
12705 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12706 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12707 set. */
12708 load_partial_comp_unit (per_cu);
12709
12710 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12711 }
12712
12713 if (pd == NULL)
12714 internal_error (__FILE__, __LINE__,
12715 _("could not find partial DIE 0x%x "
12716 "in cache [from module %s]\n"),
12717 offset.sect_off, bfd_get_filename (objfile->obfd));
12718 return pd;
12719 }
12720
12721 /* See if we can figure out if the class lives in a namespace. We do
12722 this by looking for a member function; its demangled name will
12723 contain namespace info, if there is any. */
12724
12725 static void
12726 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12727 struct dwarf2_cu *cu)
12728 {
12729 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12730 what template types look like, because the demangler
12731 frequently doesn't give the same name as the debug info. We
12732 could fix this by only using the demangled name to get the
12733 prefix (but see comment in read_structure_type). */
12734
12735 struct partial_die_info *real_pdi;
12736 struct partial_die_info *child_pdi;
12737
12738 /* If this DIE (this DIE's specification, if any) has a parent, then
12739 we should not do this. We'll prepend the parent's fully qualified
12740 name when we create the partial symbol. */
12741
12742 real_pdi = struct_pdi;
12743 while (real_pdi->has_specification)
12744 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
12745
12746 if (real_pdi->die_parent != NULL)
12747 return;
12748
12749 for (child_pdi = struct_pdi->die_child;
12750 child_pdi != NULL;
12751 child_pdi = child_pdi->die_sibling)
12752 {
12753 if (child_pdi->tag == DW_TAG_subprogram
12754 && child_pdi->linkage_name != NULL)
12755 {
12756 char *actual_class_name
12757 = language_class_name_from_physname (cu->language_defn,
12758 child_pdi->linkage_name);
12759 if (actual_class_name != NULL)
12760 {
12761 struct_pdi->name
12762 = obsavestring (actual_class_name,
12763 strlen (actual_class_name),
12764 &cu->objfile->objfile_obstack);
12765 xfree (actual_class_name);
12766 }
12767 break;
12768 }
12769 }
12770 }
12771
12772 /* Adjust PART_DIE before generating a symbol for it. This function
12773 may set the is_external flag or change the DIE's name. */
12774
12775 static void
12776 fixup_partial_die (struct partial_die_info *part_die,
12777 struct dwarf2_cu *cu)
12778 {
12779 /* Once we've fixed up a die, there's no point in doing so again.
12780 This also avoids a memory leak if we were to call
12781 guess_partial_die_structure_name multiple times. */
12782 if (part_die->fixup_called)
12783 return;
12784
12785 /* If we found a reference attribute and the DIE has no name, try
12786 to find a name in the referred to DIE. */
12787
12788 if (part_die->name == NULL && part_die->has_specification)
12789 {
12790 struct partial_die_info *spec_die;
12791
12792 spec_die = find_partial_die (part_die->spec_offset, cu);
12793
12794 fixup_partial_die (spec_die, cu);
12795
12796 if (spec_die->name)
12797 {
12798 part_die->name = spec_die->name;
12799
12800 /* Copy DW_AT_external attribute if it is set. */
12801 if (spec_die->is_external)
12802 part_die->is_external = spec_die->is_external;
12803 }
12804 }
12805
12806 /* Set default names for some unnamed DIEs. */
12807
12808 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12809 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12810
12811 /* If there is no parent die to provide a namespace, and there are
12812 children, see if we can determine the namespace from their linkage
12813 name. */
12814 if (cu->language == language_cplus
12815 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12816 && part_die->die_parent == NULL
12817 && part_die->has_children
12818 && (part_die->tag == DW_TAG_class_type
12819 || part_die->tag == DW_TAG_structure_type
12820 || part_die->tag == DW_TAG_union_type))
12821 guess_partial_die_structure_name (part_die, cu);
12822
12823 /* GCC might emit a nameless struct or union that has a linkage
12824 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12825 if (part_die->name == NULL
12826 && (part_die->tag == DW_TAG_class_type
12827 || part_die->tag == DW_TAG_interface_type
12828 || part_die->tag == DW_TAG_structure_type
12829 || part_die->tag == DW_TAG_union_type)
12830 && part_die->linkage_name != NULL)
12831 {
12832 char *demangled;
12833
12834 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
12835 if (demangled)
12836 {
12837 const char *base;
12838
12839 /* Strip any leading namespaces/classes, keep only the base name.
12840 DW_AT_name for named DIEs does not contain the prefixes. */
12841 base = strrchr (demangled, ':');
12842 if (base && base > demangled && base[-1] == ':')
12843 base++;
12844 else
12845 base = demangled;
12846
12847 part_die->name = obsavestring (base, strlen (base),
12848 &cu->objfile->objfile_obstack);
12849 xfree (demangled);
12850 }
12851 }
12852
12853 part_die->fixup_called = 1;
12854 }
12855
12856 /* Read an attribute value described by an attribute form. */
12857
12858 static gdb_byte *
12859 read_attribute_value (const struct die_reader_specs *reader,
12860 struct attribute *attr, unsigned form,
12861 gdb_byte *info_ptr)
12862 {
12863 struct dwarf2_cu *cu = reader->cu;
12864 bfd *abfd = reader->abfd;
12865 struct comp_unit_head *cu_header = &cu->header;
12866 unsigned int bytes_read;
12867 struct dwarf_block *blk;
12868
12869 attr->form = form;
12870 switch (form)
12871 {
12872 case DW_FORM_ref_addr:
12873 if (cu->header.version == 2)
12874 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
12875 else
12876 DW_UNSND (attr) = read_offset (abfd, info_ptr,
12877 &cu->header, &bytes_read);
12878 info_ptr += bytes_read;
12879 break;
12880 case DW_FORM_addr:
12881 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
12882 info_ptr += bytes_read;
12883 break;
12884 case DW_FORM_block2:
12885 blk = dwarf_alloc_block (cu);
12886 blk->size = read_2_bytes (abfd, info_ptr);
12887 info_ptr += 2;
12888 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12889 info_ptr += blk->size;
12890 DW_BLOCK (attr) = blk;
12891 break;
12892 case DW_FORM_block4:
12893 blk = dwarf_alloc_block (cu);
12894 blk->size = read_4_bytes (abfd, info_ptr);
12895 info_ptr += 4;
12896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12897 info_ptr += blk->size;
12898 DW_BLOCK (attr) = blk;
12899 break;
12900 case DW_FORM_data2:
12901 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
12902 info_ptr += 2;
12903 break;
12904 case DW_FORM_data4:
12905 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
12906 info_ptr += 4;
12907 break;
12908 case DW_FORM_data8:
12909 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
12910 info_ptr += 8;
12911 break;
12912 case DW_FORM_sec_offset:
12913 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
12914 info_ptr += bytes_read;
12915 break;
12916 case DW_FORM_string:
12917 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
12918 DW_STRING_IS_CANONICAL (attr) = 0;
12919 info_ptr += bytes_read;
12920 break;
12921 case DW_FORM_strp:
12922 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
12923 &bytes_read);
12924 DW_STRING_IS_CANONICAL (attr) = 0;
12925 info_ptr += bytes_read;
12926 break;
12927 case DW_FORM_exprloc:
12928 case DW_FORM_block:
12929 blk = dwarf_alloc_block (cu);
12930 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12931 info_ptr += bytes_read;
12932 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12933 info_ptr += blk->size;
12934 DW_BLOCK (attr) = blk;
12935 break;
12936 case DW_FORM_block1:
12937 blk = dwarf_alloc_block (cu);
12938 blk->size = read_1_byte (abfd, info_ptr);
12939 info_ptr += 1;
12940 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12941 info_ptr += blk->size;
12942 DW_BLOCK (attr) = blk;
12943 break;
12944 case DW_FORM_data1:
12945 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12946 info_ptr += 1;
12947 break;
12948 case DW_FORM_flag:
12949 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12950 info_ptr += 1;
12951 break;
12952 case DW_FORM_flag_present:
12953 DW_UNSND (attr) = 1;
12954 break;
12955 case DW_FORM_sdata:
12956 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
12957 info_ptr += bytes_read;
12958 break;
12959 case DW_FORM_udata:
12960 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12961 info_ptr += bytes_read;
12962 break;
12963 case DW_FORM_ref1:
12964 DW_UNSND (attr) = (cu->header.offset.sect_off
12965 + read_1_byte (abfd, info_ptr));
12966 info_ptr += 1;
12967 break;
12968 case DW_FORM_ref2:
12969 DW_UNSND (attr) = (cu->header.offset.sect_off
12970 + read_2_bytes (abfd, info_ptr));
12971 info_ptr += 2;
12972 break;
12973 case DW_FORM_ref4:
12974 DW_UNSND (attr) = (cu->header.offset.sect_off
12975 + read_4_bytes (abfd, info_ptr));
12976 info_ptr += 4;
12977 break;
12978 case DW_FORM_ref8:
12979 DW_UNSND (attr) = (cu->header.offset.sect_off
12980 + read_8_bytes (abfd, info_ptr));
12981 info_ptr += 8;
12982 break;
12983 case DW_FORM_ref_sig8:
12984 /* Convert the signature to something we can record in DW_UNSND
12985 for later lookup.
12986 NOTE: This is NULL if the type wasn't found. */
12987 DW_SIGNATURED_TYPE (attr) =
12988 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
12989 info_ptr += 8;
12990 break;
12991 case DW_FORM_ref_udata:
12992 DW_UNSND (attr) = (cu->header.offset.sect_off
12993 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
12994 info_ptr += bytes_read;
12995 break;
12996 case DW_FORM_indirect:
12997 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12998 info_ptr += bytes_read;
12999 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
13000 break;
13001 case DW_FORM_GNU_addr_index:
13002 if (reader->dwo_file == NULL)
13003 {
13004 /* For now flag a hard error.
13005 Later we can turn this into a complaint. */
13006 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13007 dwarf_form_name (form),
13008 bfd_get_filename (abfd));
13009 }
13010 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13011 info_ptr += bytes_read;
13012 break;
13013 case DW_FORM_GNU_str_index:
13014 if (reader->dwo_file == NULL)
13015 {
13016 /* For now flag a hard error.
13017 Later we can turn this into a complaint if warranted. */
13018 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13019 dwarf_form_name (form),
13020 bfd_get_filename (abfd));
13021 }
13022 {
13023 ULONGEST str_index =
13024 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13025
13026 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13027 DW_STRING_IS_CANONICAL (attr) = 0;
13028 info_ptr += bytes_read;
13029 }
13030 break;
13031 default:
13032 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13033 dwarf_form_name (form),
13034 bfd_get_filename (abfd));
13035 }
13036
13037 /* We have seen instances where the compiler tried to emit a byte
13038 size attribute of -1 which ended up being encoded as an unsigned
13039 0xffffffff. Although 0xffffffff is technically a valid size value,
13040 an object of this size seems pretty unlikely so we can relatively
13041 safely treat these cases as if the size attribute was invalid and
13042 treat them as zero by default. */
13043 if (attr->name == DW_AT_byte_size
13044 && form == DW_FORM_data4
13045 && DW_UNSND (attr) >= 0xffffffff)
13046 {
13047 complaint
13048 (&symfile_complaints,
13049 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13050 hex_string (DW_UNSND (attr)));
13051 DW_UNSND (attr) = 0;
13052 }
13053
13054 return info_ptr;
13055 }
13056
13057 /* Read an attribute described by an abbreviated attribute. */
13058
13059 static gdb_byte *
13060 read_attribute (const struct die_reader_specs *reader,
13061 struct attribute *attr, struct attr_abbrev *abbrev,
13062 gdb_byte *info_ptr)
13063 {
13064 attr->name = abbrev->name;
13065 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13066 }
13067
13068 /* Read dwarf information from a buffer. */
13069
13070 static unsigned int
13071 read_1_byte (bfd *abfd, gdb_byte *buf)
13072 {
13073 return bfd_get_8 (abfd, buf);
13074 }
13075
13076 static int
13077 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13078 {
13079 return bfd_get_signed_8 (abfd, buf);
13080 }
13081
13082 static unsigned int
13083 read_2_bytes (bfd *abfd, gdb_byte *buf)
13084 {
13085 return bfd_get_16 (abfd, buf);
13086 }
13087
13088 static int
13089 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13090 {
13091 return bfd_get_signed_16 (abfd, buf);
13092 }
13093
13094 static unsigned int
13095 read_4_bytes (bfd *abfd, gdb_byte *buf)
13096 {
13097 return bfd_get_32 (abfd, buf);
13098 }
13099
13100 static int
13101 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13102 {
13103 return bfd_get_signed_32 (abfd, buf);
13104 }
13105
13106 static ULONGEST
13107 read_8_bytes (bfd *abfd, gdb_byte *buf)
13108 {
13109 return bfd_get_64 (abfd, buf);
13110 }
13111
13112 static CORE_ADDR
13113 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13114 unsigned int *bytes_read)
13115 {
13116 struct comp_unit_head *cu_header = &cu->header;
13117 CORE_ADDR retval = 0;
13118
13119 if (cu_header->signed_addr_p)
13120 {
13121 switch (cu_header->addr_size)
13122 {
13123 case 2:
13124 retval = bfd_get_signed_16 (abfd, buf);
13125 break;
13126 case 4:
13127 retval = bfd_get_signed_32 (abfd, buf);
13128 break;
13129 case 8:
13130 retval = bfd_get_signed_64 (abfd, buf);
13131 break;
13132 default:
13133 internal_error (__FILE__, __LINE__,
13134 _("read_address: bad switch, signed [in module %s]"),
13135 bfd_get_filename (abfd));
13136 }
13137 }
13138 else
13139 {
13140 switch (cu_header->addr_size)
13141 {
13142 case 2:
13143 retval = bfd_get_16 (abfd, buf);
13144 break;
13145 case 4:
13146 retval = bfd_get_32 (abfd, buf);
13147 break;
13148 case 8:
13149 retval = bfd_get_64 (abfd, buf);
13150 break;
13151 default:
13152 internal_error (__FILE__, __LINE__,
13153 _("read_address: bad switch, "
13154 "unsigned [in module %s]"),
13155 bfd_get_filename (abfd));
13156 }
13157 }
13158
13159 *bytes_read = cu_header->addr_size;
13160 return retval;
13161 }
13162
13163 /* Read the initial length from a section. The (draft) DWARF 3
13164 specification allows the initial length to take up either 4 bytes
13165 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13166 bytes describe the length and all offsets will be 8 bytes in length
13167 instead of 4.
13168
13169 An older, non-standard 64-bit format is also handled by this
13170 function. The older format in question stores the initial length
13171 as an 8-byte quantity without an escape value. Lengths greater
13172 than 2^32 aren't very common which means that the initial 4 bytes
13173 is almost always zero. Since a length value of zero doesn't make
13174 sense for the 32-bit format, this initial zero can be considered to
13175 be an escape value which indicates the presence of the older 64-bit
13176 format. As written, the code can't detect (old format) lengths
13177 greater than 4GB. If it becomes necessary to handle lengths
13178 somewhat larger than 4GB, we could allow other small values (such
13179 as the non-sensical values of 1, 2, and 3) to also be used as
13180 escape values indicating the presence of the old format.
13181
13182 The value returned via bytes_read should be used to increment the
13183 relevant pointer after calling read_initial_length().
13184
13185 [ Note: read_initial_length() and read_offset() are based on the
13186 document entitled "DWARF Debugging Information Format", revision
13187 3, draft 8, dated November 19, 2001. This document was obtained
13188 from:
13189
13190 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13191
13192 This document is only a draft and is subject to change. (So beware.)
13193
13194 Details regarding the older, non-standard 64-bit format were
13195 determined empirically by examining 64-bit ELF files produced by
13196 the SGI toolchain on an IRIX 6.5 machine.
13197
13198 - Kevin, July 16, 2002
13199 ] */
13200
13201 static LONGEST
13202 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13203 {
13204 LONGEST length = bfd_get_32 (abfd, buf);
13205
13206 if (length == 0xffffffff)
13207 {
13208 length = bfd_get_64 (abfd, buf + 4);
13209 *bytes_read = 12;
13210 }
13211 else if (length == 0)
13212 {
13213 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
13214 length = bfd_get_64 (abfd, buf);
13215 *bytes_read = 8;
13216 }
13217 else
13218 {
13219 *bytes_read = 4;
13220 }
13221
13222 return length;
13223 }
13224
13225 /* Cover function for read_initial_length.
13226 Returns the length of the object at BUF, and stores the size of the
13227 initial length in *BYTES_READ and stores the size that offsets will be in
13228 *OFFSET_SIZE.
13229 If the initial length size is not equivalent to that specified in
13230 CU_HEADER then issue a complaint.
13231 This is useful when reading non-comp-unit headers. */
13232
13233 static LONGEST
13234 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13235 const struct comp_unit_head *cu_header,
13236 unsigned int *bytes_read,
13237 unsigned int *offset_size)
13238 {
13239 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13240
13241 gdb_assert (cu_header->initial_length_size == 4
13242 || cu_header->initial_length_size == 8
13243 || cu_header->initial_length_size == 12);
13244
13245 if (cu_header->initial_length_size != *bytes_read)
13246 complaint (&symfile_complaints,
13247 _("intermixed 32-bit and 64-bit DWARF sections"));
13248
13249 *offset_size = (*bytes_read == 4) ? 4 : 8;
13250 return length;
13251 }
13252
13253 /* Read an offset from the data stream. The size of the offset is
13254 given by cu_header->offset_size. */
13255
13256 static LONGEST
13257 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13258 unsigned int *bytes_read)
13259 {
13260 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13261
13262 *bytes_read = cu_header->offset_size;
13263 return offset;
13264 }
13265
13266 /* Read an offset from the data stream. */
13267
13268 static LONGEST
13269 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13270 {
13271 LONGEST retval = 0;
13272
13273 switch (offset_size)
13274 {
13275 case 4:
13276 retval = bfd_get_32 (abfd, buf);
13277 break;
13278 case 8:
13279 retval = bfd_get_64 (abfd, buf);
13280 break;
13281 default:
13282 internal_error (__FILE__, __LINE__,
13283 _("read_offset_1: bad switch [in module %s]"),
13284 bfd_get_filename (abfd));
13285 }
13286
13287 return retval;
13288 }
13289
13290 static gdb_byte *
13291 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13292 {
13293 /* If the size of a host char is 8 bits, we can return a pointer
13294 to the buffer, otherwise we have to copy the data to a buffer
13295 allocated on the temporary obstack. */
13296 gdb_assert (HOST_CHAR_BIT == 8);
13297 return buf;
13298 }
13299
13300 static char *
13301 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13302 {
13303 /* If the size of a host char is 8 bits, we can return a pointer
13304 to the string, otherwise we have to copy the string to a buffer
13305 allocated on the temporary obstack. */
13306 gdb_assert (HOST_CHAR_BIT == 8);
13307 if (*buf == '\0')
13308 {
13309 *bytes_read_ptr = 1;
13310 return NULL;
13311 }
13312 *bytes_read_ptr = strlen ((char *) buf) + 1;
13313 return (char *) buf;
13314 }
13315
13316 static char *
13317 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13318 {
13319 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13320 if (dwarf2_per_objfile->str.buffer == NULL)
13321 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13322 bfd_get_filename (abfd));
13323 if (str_offset >= dwarf2_per_objfile->str.size)
13324 error (_("DW_FORM_strp pointing outside of "
13325 ".debug_str section [in module %s]"),
13326 bfd_get_filename (abfd));
13327 gdb_assert (HOST_CHAR_BIT == 8);
13328 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13329 return NULL;
13330 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13331 }
13332
13333 static char *
13334 read_indirect_string (bfd *abfd, gdb_byte *buf,
13335 const struct comp_unit_head *cu_header,
13336 unsigned int *bytes_read_ptr)
13337 {
13338 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13339
13340 return read_indirect_string_at_offset (abfd, str_offset);
13341 }
13342
13343 static ULONGEST
13344 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13345 {
13346 ULONGEST result;
13347 unsigned int num_read;
13348 int i, shift;
13349 unsigned char byte;
13350
13351 result = 0;
13352 shift = 0;
13353 num_read = 0;
13354 i = 0;
13355 while (1)
13356 {
13357 byte = bfd_get_8 (abfd, buf);
13358 buf++;
13359 num_read++;
13360 result |= ((ULONGEST) (byte & 127) << shift);
13361 if ((byte & 128) == 0)
13362 {
13363 break;
13364 }
13365 shift += 7;
13366 }
13367 *bytes_read_ptr = num_read;
13368 return result;
13369 }
13370
13371 static LONGEST
13372 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13373 {
13374 LONGEST result;
13375 int i, shift, num_read;
13376 unsigned char byte;
13377
13378 result = 0;
13379 shift = 0;
13380 num_read = 0;
13381 i = 0;
13382 while (1)
13383 {
13384 byte = bfd_get_8 (abfd, buf);
13385 buf++;
13386 num_read++;
13387 result |= ((LONGEST) (byte & 127) << shift);
13388 shift += 7;
13389 if ((byte & 128) == 0)
13390 {
13391 break;
13392 }
13393 }
13394 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13395 result |= -(((LONGEST) 1) << shift);
13396 *bytes_read_ptr = num_read;
13397 return result;
13398 }
13399
13400 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13401 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13402 ADDR_SIZE is the size of addresses from the CU header. */
13403
13404 static CORE_ADDR
13405 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13406 {
13407 struct objfile *objfile = dwarf2_per_objfile->objfile;
13408 bfd *abfd = objfile->obfd;
13409 const gdb_byte *info_ptr;
13410
13411 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13412 if (dwarf2_per_objfile->addr.buffer == NULL)
13413 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13414 objfile->name);
13415 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13416 error (_("DW_FORM_addr_index pointing outside of "
13417 ".debug_addr section [in module %s]"),
13418 objfile->name);
13419 info_ptr = (dwarf2_per_objfile->addr.buffer
13420 + addr_base + addr_index * addr_size);
13421 if (addr_size == 4)
13422 return bfd_get_32 (abfd, info_ptr);
13423 else
13424 return bfd_get_64 (abfd, info_ptr);
13425 }
13426
13427 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13428
13429 static CORE_ADDR
13430 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13431 {
13432 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13433 }
13434
13435 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13436
13437 static CORE_ADDR
13438 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13439 unsigned int *bytes_read)
13440 {
13441 bfd *abfd = cu->objfile->obfd;
13442 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13443
13444 return read_addr_index (cu, addr_index);
13445 }
13446
13447 /* Data structure to pass results from dwarf2_read_addr_index_reader
13448 back to dwarf2_read_addr_index. */
13449
13450 struct dwarf2_read_addr_index_data
13451 {
13452 ULONGEST addr_base;
13453 int addr_size;
13454 };
13455
13456 /* die_reader_func for dwarf2_read_addr_index. */
13457
13458 static void
13459 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13460 gdb_byte *info_ptr,
13461 struct die_info *comp_unit_die,
13462 int has_children,
13463 void *data)
13464 {
13465 struct dwarf2_cu *cu = reader->cu;
13466 struct dwarf2_read_addr_index_data *aidata =
13467 (struct dwarf2_read_addr_index_data *) data;
13468
13469 aidata->addr_base = cu->addr_base;
13470 aidata->addr_size = cu->header.addr_size;
13471 }
13472
13473 /* Given an index in .debug_addr, fetch the value.
13474 NOTE: This can be called during dwarf expression evaluation,
13475 long after the debug information has been read, and thus per_cu->cu
13476 may no longer exist. */
13477
13478 CORE_ADDR
13479 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13480 unsigned int addr_index)
13481 {
13482 struct objfile *objfile = per_cu->objfile;
13483 struct dwarf2_cu *cu = per_cu->cu;
13484 ULONGEST addr_base;
13485 int addr_size;
13486
13487 /* This is intended to be called from outside this file. */
13488 dw2_setup (objfile);
13489
13490 /* We need addr_base and addr_size.
13491 If we don't have PER_CU->cu, we have to get it.
13492 Nasty, but the alternative is storing the needed info in PER_CU,
13493 which at this point doesn't seem justified: it's not clear how frequently
13494 it would get used and it would increase the size of every PER_CU.
13495 Entry points like dwarf2_per_cu_addr_size do a similar thing
13496 so we're not in uncharted territory here.
13497 Alas we need to be a bit more complicated as addr_base is contained
13498 in the DIE.
13499
13500 We don't need to read the entire CU(/TU).
13501 We just need the header and top level die.
13502 IWBN to use the aging mechanism to let us lazily later discard the CU.
13503 See however init_cutu_and_read_dies_simple. */
13504
13505 if (cu != NULL)
13506 {
13507 addr_base = cu->addr_base;
13508 addr_size = cu->header.addr_size;
13509 }
13510 else
13511 {
13512 struct dwarf2_read_addr_index_data aidata;
13513
13514 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13515 &aidata);
13516 addr_base = aidata.addr_base;
13517 addr_size = aidata.addr_size;
13518 }
13519
13520 return read_addr_index_1 (addr_index, addr_base, addr_size);
13521 }
13522
13523 /* Given a DW_AT_str_index, fetch the string. */
13524
13525 static char *
13526 read_str_index (const struct die_reader_specs *reader,
13527 struct dwarf2_cu *cu, ULONGEST str_index)
13528 {
13529 struct objfile *objfile = dwarf2_per_objfile->objfile;
13530 const char *dwo_name = objfile->name;
13531 bfd *abfd = objfile->obfd;
13532 struct dwo_sections *sections = &reader->dwo_file->sections;
13533 gdb_byte *info_ptr;
13534 ULONGEST str_offset;
13535
13536 dwarf2_read_section (objfile, &sections->str);
13537 dwarf2_read_section (objfile, &sections->str_offsets);
13538 if (sections->str.buffer == NULL)
13539 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13540 " in CU at offset 0x%lx [in module %s]"),
13541 (long) cu->header.offset.sect_off, dwo_name);
13542 if (sections->str_offsets.buffer == NULL)
13543 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13544 " in CU at offset 0x%lx [in module %s]"),
13545 (long) cu->header.offset.sect_off, dwo_name);
13546 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13547 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13548 " section in CU at offset 0x%lx [in module %s]"),
13549 (long) cu->header.offset.sect_off, dwo_name);
13550 info_ptr = (sections->str_offsets.buffer
13551 + str_index * cu->header.offset_size);
13552 if (cu->header.offset_size == 4)
13553 str_offset = bfd_get_32 (abfd, info_ptr);
13554 else
13555 str_offset = bfd_get_64 (abfd, info_ptr);
13556 if (str_offset >= sections->str.size)
13557 error (_("Offset from DW_FORM_str_index pointing outside of"
13558 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13559 (long) cu->header.offset.sect_off, dwo_name);
13560 return (char *) (sections->str.buffer + str_offset);
13561 }
13562
13563 /* Return the length of an LEB128 number in BUF. */
13564
13565 static int
13566 leb128_size (const gdb_byte *buf)
13567 {
13568 const gdb_byte *begin = buf;
13569 gdb_byte byte;
13570
13571 while (1)
13572 {
13573 byte = *buf++;
13574 if ((byte & 128) == 0)
13575 return buf - begin;
13576 }
13577 }
13578
13579 static void
13580 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13581 {
13582 switch (lang)
13583 {
13584 case DW_LANG_C89:
13585 case DW_LANG_C99:
13586 case DW_LANG_C:
13587 cu->language = language_c;
13588 break;
13589 case DW_LANG_C_plus_plus:
13590 cu->language = language_cplus;
13591 break;
13592 case DW_LANG_D:
13593 cu->language = language_d;
13594 break;
13595 case DW_LANG_Fortran77:
13596 case DW_LANG_Fortran90:
13597 case DW_LANG_Fortran95:
13598 cu->language = language_fortran;
13599 break;
13600 case DW_LANG_Go:
13601 cu->language = language_go;
13602 break;
13603 case DW_LANG_Mips_Assembler:
13604 cu->language = language_asm;
13605 break;
13606 case DW_LANG_Java:
13607 cu->language = language_java;
13608 break;
13609 case DW_LANG_Ada83:
13610 case DW_LANG_Ada95:
13611 cu->language = language_ada;
13612 break;
13613 case DW_LANG_Modula2:
13614 cu->language = language_m2;
13615 break;
13616 case DW_LANG_Pascal83:
13617 cu->language = language_pascal;
13618 break;
13619 case DW_LANG_ObjC:
13620 cu->language = language_objc;
13621 break;
13622 case DW_LANG_Cobol74:
13623 case DW_LANG_Cobol85:
13624 default:
13625 cu->language = language_minimal;
13626 break;
13627 }
13628 cu->language_defn = language_def (cu->language);
13629 }
13630
13631 /* Return the named attribute or NULL if not there. */
13632
13633 static struct attribute *
13634 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13635 {
13636 for (;;)
13637 {
13638 unsigned int i;
13639 struct attribute *spec = NULL;
13640
13641 for (i = 0; i < die->num_attrs; ++i)
13642 {
13643 if (die->attrs[i].name == name)
13644 return &die->attrs[i];
13645 if (die->attrs[i].name == DW_AT_specification
13646 || die->attrs[i].name == DW_AT_abstract_origin)
13647 spec = &die->attrs[i];
13648 }
13649
13650 if (!spec)
13651 break;
13652
13653 die = follow_die_ref (die, spec, &cu);
13654 }
13655
13656 return NULL;
13657 }
13658
13659 /* Return the named attribute or NULL if not there,
13660 but do not follow DW_AT_specification, etc.
13661 This is for use in contexts where we're reading .debug_types dies.
13662 Following DW_AT_specification, DW_AT_abstract_origin will take us
13663 back up the chain, and we want to go down. */
13664
13665 static struct attribute *
13666 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13667 {
13668 unsigned int i;
13669
13670 for (i = 0; i < die->num_attrs; ++i)
13671 if (die->attrs[i].name == name)
13672 return &die->attrs[i];
13673
13674 return NULL;
13675 }
13676
13677 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13678 and holds a non-zero value. This function should only be used for
13679 DW_FORM_flag or DW_FORM_flag_present attributes. */
13680
13681 static int
13682 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13683 {
13684 struct attribute *attr = dwarf2_attr (die, name, cu);
13685
13686 return (attr && DW_UNSND (attr));
13687 }
13688
13689 static int
13690 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13691 {
13692 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13693 which value is non-zero. However, we have to be careful with
13694 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13695 (via dwarf2_flag_true_p) follows this attribute. So we may
13696 end up accidently finding a declaration attribute that belongs
13697 to a different DIE referenced by the specification attribute,
13698 even though the given DIE does not have a declaration attribute. */
13699 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13700 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13701 }
13702
13703 /* Return the die giving the specification for DIE, if there is
13704 one. *SPEC_CU is the CU containing DIE on input, and the CU
13705 containing the return value on output. If there is no
13706 specification, but there is an abstract origin, that is
13707 returned. */
13708
13709 static struct die_info *
13710 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13711 {
13712 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13713 *spec_cu);
13714
13715 if (spec_attr == NULL)
13716 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13717
13718 if (spec_attr == NULL)
13719 return NULL;
13720 else
13721 return follow_die_ref (die, spec_attr, spec_cu);
13722 }
13723
13724 /* Free the line_header structure *LH, and any arrays and strings it
13725 refers to.
13726 NOTE: This is also used as a "cleanup" function. */
13727
13728 static void
13729 free_line_header (struct line_header *lh)
13730 {
13731 if (lh->standard_opcode_lengths)
13732 xfree (lh->standard_opcode_lengths);
13733
13734 /* Remember that all the lh->file_names[i].name pointers are
13735 pointers into debug_line_buffer, and don't need to be freed. */
13736 if (lh->file_names)
13737 xfree (lh->file_names);
13738
13739 /* Similarly for the include directory names. */
13740 if (lh->include_dirs)
13741 xfree (lh->include_dirs);
13742
13743 xfree (lh);
13744 }
13745
13746 /* Add an entry to LH's include directory table. */
13747
13748 static void
13749 add_include_dir (struct line_header *lh, char *include_dir)
13750 {
13751 /* Grow the array if necessary. */
13752 if (lh->include_dirs_size == 0)
13753 {
13754 lh->include_dirs_size = 1; /* for testing */
13755 lh->include_dirs = xmalloc (lh->include_dirs_size
13756 * sizeof (*lh->include_dirs));
13757 }
13758 else if (lh->num_include_dirs >= lh->include_dirs_size)
13759 {
13760 lh->include_dirs_size *= 2;
13761 lh->include_dirs = xrealloc (lh->include_dirs,
13762 (lh->include_dirs_size
13763 * sizeof (*lh->include_dirs)));
13764 }
13765
13766 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13767 }
13768
13769 /* Add an entry to LH's file name table. */
13770
13771 static void
13772 add_file_name (struct line_header *lh,
13773 char *name,
13774 unsigned int dir_index,
13775 unsigned int mod_time,
13776 unsigned int length)
13777 {
13778 struct file_entry *fe;
13779
13780 /* Grow the array if necessary. */
13781 if (lh->file_names_size == 0)
13782 {
13783 lh->file_names_size = 1; /* for testing */
13784 lh->file_names = xmalloc (lh->file_names_size
13785 * sizeof (*lh->file_names));
13786 }
13787 else if (lh->num_file_names >= lh->file_names_size)
13788 {
13789 lh->file_names_size *= 2;
13790 lh->file_names = xrealloc (lh->file_names,
13791 (lh->file_names_size
13792 * sizeof (*lh->file_names)));
13793 }
13794
13795 fe = &lh->file_names[lh->num_file_names++];
13796 fe->name = name;
13797 fe->dir_index = dir_index;
13798 fe->mod_time = mod_time;
13799 fe->length = length;
13800 fe->included_p = 0;
13801 fe->symtab = NULL;
13802 }
13803
13804 /* Read the statement program header starting at OFFSET in
13805 .debug_line, or .debug_line.dwo. Return a pointer
13806 to a struct line_header, allocated using xmalloc.
13807
13808 NOTE: the strings in the include directory and file name tables of
13809 the returned object point into the dwarf line section buffer,
13810 and must not be freed. */
13811
13812 static struct line_header *
13813 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
13814 {
13815 struct cleanup *back_to;
13816 struct line_header *lh;
13817 gdb_byte *line_ptr;
13818 unsigned int bytes_read, offset_size;
13819 int i;
13820 char *cur_dir, *cur_file;
13821 struct dwarf2_section_info *section;
13822 bfd *abfd;
13823
13824 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
13825 DWO file. */
13826 if (cu->dwo_unit && cu->per_cu->is_debug_types)
13827 section = &cu->dwo_unit->dwo_file->sections.line;
13828 else
13829 section = &dwarf2_per_objfile->line;
13830
13831 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
13832 if (section->buffer == NULL)
13833 {
13834 if (cu->dwo_unit && cu->per_cu->is_debug_types)
13835 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
13836 else
13837 complaint (&symfile_complaints, _("missing .debug_line section"));
13838 return 0;
13839 }
13840
13841 /* We can't do this until we know the section is non-empty.
13842 Only then do we know we have such a section. */
13843 abfd = section->asection->owner;
13844
13845 /* Make sure that at least there's room for the total_length field.
13846 That could be 12 bytes long, but we're just going to fudge that. */
13847 if (offset + 4 >= section->size)
13848 {
13849 dwarf2_statement_list_fits_in_line_number_section_complaint ();
13850 return 0;
13851 }
13852
13853 lh = xmalloc (sizeof (*lh));
13854 memset (lh, 0, sizeof (*lh));
13855 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
13856 (void *) lh);
13857
13858 line_ptr = section->buffer + offset;
13859
13860 /* Read in the header. */
13861 lh->total_length =
13862 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
13863 &bytes_read, &offset_size);
13864 line_ptr += bytes_read;
13865 if (line_ptr + lh->total_length > (section->buffer + section->size))
13866 {
13867 dwarf2_statement_list_fits_in_line_number_section_complaint ();
13868 return 0;
13869 }
13870 lh->statement_program_end = line_ptr + lh->total_length;
13871 lh->version = read_2_bytes (abfd, line_ptr);
13872 line_ptr += 2;
13873 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
13874 line_ptr += offset_size;
13875 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
13876 line_ptr += 1;
13877 if (lh->version >= 4)
13878 {
13879 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
13880 line_ptr += 1;
13881 }
13882 else
13883 lh->maximum_ops_per_instruction = 1;
13884
13885 if (lh->maximum_ops_per_instruction == 0)
13886 {
13887 lh->maximum_ops_per_instruction = 1;
13888 complaint (&symfile_complaints,
13889 _("invalid maximum_ops_per_instruction "
13890 "in `.debug_line' section"));
13891 }
13892
13893 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
13894 line_ptr += 1;
13895 lh->line_base = read_1_signed_byte (abfd, line_ptr);
13896 line_ptr += 1;
13897 lh->line_range = read_1_byte (abfd, line_ptr);
13898 line_ptr += 1;
13899 lh->opcode_base = read_1_byte (abfd, line_ptr);
13900 line_ptr += 1;
13901 lh->standard_opcode_lengths
13902 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
13903
13904 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
13905 for (i = 1; i < lh->opcode_base; ++i)
13906 {
13907 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
13908 line_ptr += 1;
13909 }
13910
13911 /* Read directory table. */
13912 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
13913 {
13914 line_ptr += bytes_read;
13915 add_include_dir (lh, cur_dir);
13916 }
13917 line_ptr += bytes_read;
13918
13919 /* Read file name table. */
13920 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
13921 {
13922 unsigned int dir_index, mod_time, length;
13923
13924 line_ptr += bytes_read;
13925 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13926 line_ptr += bytes_read;
13927 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13928 line_ptr += bytes_read;
13929 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13930 line_ptr += bytes_read;
13931
13932 add_file_name (lh, cur_file, dir_index, mod_time, length);
13933 }
13934 line_ptr += bytes_read;
13935 lh->statement_program_start = line_ptr;
13936
13937 if (line_ptr > (section->buffer + section->size))
13938 complaint (&symfile_complaints,
13939 _("line number info header doesn't "
13940 "fit in `.debug_line' section"));
13941
13942 discard_cleanups (back_to);
13943 return lh;
13944 }
13945
13946 /* Subroutine of dwarf_decode_lines to simplify it.
13947 Return the file name of the psymtab for included file FILE_INDEX
13948 in line header LH of PST.
13949 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13950 If space for the result is malloc'd, it will be freed by a cleanup.
13951 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
13952
13953 static char *
13954 psymtab_include_file_name (const struct line_header *lh, int file_index,
13955 const struct partial_symtab *pst,
13956 const char *comp_dir)
13957 {
13958 const struct file_entry fe = lh->file_names [file_index];
13959 char *include_name = fe.name;
13960 char *include_name_to_compare = include_name;
13961 char *dir_name = NULL;
13962 const char *pst_filename;
13963 char *copied_name = NULL;
13964 int file_is_pst;
13965
13966 if (fe.dir_index)
13967 dir_name = lh->include_dirs[fe.dir_index - 1];
13968
13969 if (!IS_ABSOLUTE_PATH (include_name)
13970 && (dir_name != NULL || comp_dir != NULL))
13971 {
13972 /* Avoid creating a duplicate psymtab for PST.
13973 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13974 Before we do the comparison, however, we need to account
13975 for DIR_NAME and COMP_DIR.
13976 First prepend dir_name (if non-NULL). If we still don't
13977 have an absolute path prepend comp_dir (if non-NULL).
13978 However, the directory we record in the include-file's
13979 psymtab does not contain COMP_DIR (to match the
13980 corresponding symtab(s)).
13981
13982 Example:
13983
13984 bash$ cd /tmp
13985 bash$ gcc -g ./hello.c
13986 include_name = "hello.c"
13987 dir_name = "."
13988 DW_AT_comp_dir = comp_dir = "/tmp"
13989 DW_AT_name = "./hello.c" */
13990
13991 if (dir_name != NULL)
13992 {
13993 include_name = concat (dir_name, SLASH_STRING,
13994 include_name, (char *)NULL);
13995 include_name_to_compare = include_name;
13996 make_cleanup (xfree, include_name);
13997 }
13998 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13999 {
14000 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14001 include_name, (char *)NULL);
14002 }
14003 }
14004
14005 pst_filename = pst->filename;
14006 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14007 {
14008 copied_name = concat (pst->dirname, SLASH_STRING,
14009 pst_filename, (char *)NULL);
14010 pst_filename = copied_name;
14011 }
14012
14013 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14014
14015 if (include_name_to_compare != include_name)
14016 xfree (include_name_to_compare);
14017 if (copied_name != NULL)
14018 xfree (copied_name);
14019
14020 if (file_is_pst)
14021 return NULL;
14022 return include_name;
14023 }
14024
14025 /* Ignore this record_line request. */
14026
14027 static void
14028 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14029 {
14030 return;
14031 }
14032
14033 /* Subroutine of dwarf_decode_lines to simplify it.
14034 Process the line number information in LH. */
14035
14036 static void
14037 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14038 struct dwarf2_cu *cu, struct partial_symtab *pst)
14039 {
14040 gdb_byte *line_ptr, *extended_end;
14041 gdb_byte *line_end;
14042 unsigned int bytes_read, extended_len;
14043 unsigned char op_code, extended_op, adj_opcode;
14044 CORE_ADDR baseaddr;
14045 struct objfile *objfile = cu->objfile;
14046 bfd *abfd = objfile->obfd;
14047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14048 const int decode_for_pst_p = (pst != NULL);
14049 struct subfile *last_subfile = NULL;
14050 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14051 = record_line;
14052
14053 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14054
14055 line_ptr = lh->statement_program_start;
14056 line_end = lh->statement_program_end;
14057
14058 /* Read the statement sequences until there's nothing left. */
14059 while (line_ptr < line_end)
14060 {
14061 /* state machine registers */
14062 CORE_ADDR address = 0;
14063 unsigned int file = 1;
14064 unsigned int line = 1;
14065 unsigned int column = 0;
14066 int is_stmt = lh->default_is_stmt;
14067 int basic_block = 0;
14068 int end_sequence = 0;
14069 CORE_ADDR addr;
14070 unsigned char op_index = 0;
14071
14072 if (!decode_for_pst_p && lh->num_file_names >= file)
14073 {
14074 /* Start a subfile for the current file of the state machine. */
14075 /* lh->include_dirs and lh->file_names are 0-based, but the
14076 directory and file name numbers in the statement program
14077 are 1-based. */
14078 struct file_entry *fe = &lh->file_names[file - 1];
14079 char *dir = NULL;
14080
14081 if (fe->dir_index)
14082 dir = lh->include_dirs[fe->dir_index - 1];
14083
14084 dwarf2_start_subfile (fe->name, dir, comp_dir);
14085 }
14086
14087 /* Decode the table. */
14088 while (!end_sequence)
14089 {
14090 op_code = read_1_byte (abfd, line_ptr);
14091 line_ptr += 1;
14092 if (line_ptr > line_end)
14093 {
14094 dwarf2_debug_line_missing_end_sequence_complaint ();
14095 break;
14096 }
14097
14098 if (op_code >= lh->opcode_base)
14099 {
14100 /* Special operand. */
14101 adj_opcode = op_code - lh->opcode_base;
14102 address += (((op_index + (adj_opcode / lh->line_range))
14103 / lh->maximum_ops_per_instruction)
14104 * lh->minimum_instruction_length);
14105 op_index = ((op_index + (adj_opcode / lh->line_range))
14106 % lh->maximum_ops_per_instruction);
14107 line += lh->line_base + (adj_opcode % lh->line_range);
14108 if (lh->num_file_names < file || file == 0)
14109 dwarf2_debug_line_missing_file_complaint ();
14110 /* For now we ignore lines not starting on an
14111 instruction boundary. */
14112 else if (op_index == 0)
14113 {
14114 lh->file_names[file - 1].included_p = 1;
14115 if (!decode_for_pst_p && is_stmt)
14116 {
14117 if (last_subfile != current_subfile)
14118 {
14119 addr = gdbarch_addr_bits_remove (gdbarch, address);
14120 if (last_subfile)
14121 (*p_record_line) (last_subfile, 0, addr);
14122 last_subfile = current_subfile;
14123 }
14124 /* Append row to matrix using current values. */
14125 addr = gdbarch_addr_bits_remove (gdbarch, address);
14126 (*p_record_line) (current_subfile, line, addr);
14127 }
14128 }
14129 basic_block = 0;
14130 }
14131 else switch (op_code)
14132 {
14133 case DW_LNS_extended_op:
14134 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14135 &bytes_read);
14136 line_ptr += bytes_read;
14137 extended_end = line_ptr + extended_len;
14138 extended_op = read_1_byte (abfd, line_ptr);
14139 line_ptr += 1;
14140 switch (extended_op)
14141 {
14142 case DW_LNE_end_sequence:
14143 p_record_line = record_line;
14144 end_sequence = 1;
14145 break;
14146 case DW_LNE_set_address:
14147 address = read_address (abfd, line_ptr, cu, &bytes_read);
14148
14149 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14150 {
14151 /* This line table is for a function which has been
14152 GCd by the linker. Ignore it. PR gdb/12528 */
14153
14154 long line_offset
14155 = line_ptr - dwarf2_per_objfile->line.buffer;
14156
14157 complaint (&symfile_complaints,
14158 _(".debug_line address at offset 0x%lx is 0 "
14159 "[in module %s]"),
14160 line_offset, objfile->name);
14161 p_record_line = noop_record_line;
14162 }
14163
14164 op_index = 0;
14165 line_ptr += bytes_read;
14166 address += baseaddr;
14167 break;
14168 case DW_LNE_define_file:
14169 {
14170 char *cur_file;
14171 unsigned int dir_index, mod_time, length;
14172
14173 cur_file = read_direct_string (abfd, line_ptr,
14174 &bytes_read);
14175 line_ptr += bytes_read;
14176 dir_index =
14177 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14178 line_ptr += bytes_read;
14179 mod_time =
14180 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14181 line_ptr += bytes_read;
14182 length =
14183 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14184 line_ptr += bytes_read;
14185 add_file_name (lh, cur_file, dir_index, mod_time, length);
14186 }
14187 break;
14188 case DW_LNE_set_discriminator:
14189 /* The discriminator is not interesting to the debugger;
14190 just ignore it. */
14191 line_ptr = extended_end;
14192 break;
14193 default:
14194 complaint (&symfile_complaints,
14195 _("mangled .debug_line section"));
14196 return;
14197 }
14198 /* Make sure that we parsed the extended op correctly. If e.g.
14199 we expected a different address size than the producer used,
14200 we may have read the wrong number of bytes. */
14201 if (line_ptr != extended_end)
14202 {
14203 complaint (&symfile_complaints,
14204 _("mangled .debug_line section"));
14205 return;
14206 }
14207 break;
14208 case DW_LNS_copy:
14209 if (lh->num_file_names < file || file == 0)
14210 dwarf2_debug_line_missing_file_complaint ();
14211 else
14212 {
14213 lh->file_names[file - 1].included_p = 1;
14214 if (!decode_for_pst_p && is_stmt)
14215 {
14216 if (last_subfile != current_subfile)
14217 {
14218 addr = gdbarch_addr_bits_remove (gdbarch, address);
14219 if (last_subfile)
14220 (*p_record_line) (last_subfile, 0, addr);
14221 last_subfile = current_subfile;
14222 }
14223 addr = gdbarch_addr_bits_remove (gdbarch, address);
14224 (*p_record_line) (current_subfile, line, addr);
14225 }
14226 }
14227 basic_block = 0;
14228 break;
14229 case DW_LNS_advance_pc:
14230 {
14231 CORE_ADDR adjust
14232 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14233
14234 address += (((op_index + adjust)
14235 / lh->maximum_ops_per_instruction)
14236 * lh->minimum_instruction_length);
14237 op_index = ((op_index + adjust)
14238 % lh->maximum_ops_per_instruction);
14239 line_ptr += bytes_read;
14240 }
14241 break;
14242 case DW_LNS_advance_line:
14243 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14244 line_ptr += bytes_read;
14245 break;
14246 case DW_LNS_set_file:
14247 {
14248 /* The arrays lh->include_dirs and lh->file_names are
14249 0-based, but the directory and file name numbers in
14250 the statement program are 1-based. */
14251 struct file_entry *fe;
14252 char *dir = NULL;
14253
14254 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14255 line_ptr += bytes_read;
14256 if (lh->num_file_names < file || file == 0)
14257 dwarf2_debug_line_missing_file_complaint ();
14258 else
14259 {
14260 fe = &lh->file_names[file - 1];
14261 if (fe->dir_index)
14262 dir = lh->include_dirs[fe->dir_index - 1];
14263 if (!decode_for_pst_p)
14264 {
14265 last_subfile = current_subfile;
14266 dwarf2_start_subfile (fe->name, dir, comp_dir);
14267 }
14268 }
14269 }
14270 break;
14271 case DW_LNS_set_column:
14272 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14273 line_ptr += bytes_read;
14274 break;
14275 case DW_LNS_negate_stmt:
14276 is_stmt = (!is_stmt);
14277 break;
14278 case DW_LNS_set_basic_block:
14279 basic_block = 1;
14280 break;
14281 /* Add to the address register of the state machine the
14282 address increment value corresponding to special opcode
14283 255. I.e., this value is scaled by the minimum
14284 instruction length since special opcode 255 would have
14285 scaled the increment. */
14286 case DW_LNS_const_add_pc:
14287 {
14288 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14289
14290 address += (((op_index + adjust)
14291 / lh->maximum_ops_per_instruction)
14292 * lh->minimum_instruction_length);
14293 op_index = ((op_index + adjust)
14294 % lh->maximum_ops_per_instruction);
14295 }
14296 break;
14297 case DW_LNS_fixed_advance_pc:
14298 address += read_2_bytes (abfd, line_ptr);
14299 op_index = 0;
14300 line_ptr += 2;
14301 break;
14302 default:
14303 {
14304 /* Unknown standard opcode, ignore it. */
14305 int i;
14306
14307 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14308 {
14309 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14310 line_ptr += bytes_read;
14311 }
14312 }
14313 }
14314 }
14315 if (lh->num_file_names < file || file == 0)
14316 dwarf2_debug_line_missing_file_complaint ();
14317 else
14318 {
14319 lh->file_names[file - 1].included_p = 1;
14320 if (!decode_for_pst_p)
14321 {
14322 addr = gdbarch_addr_bits_remove (gdbarch, address);
14323 (*p_record_line) (current_subfile, 0, addr);
14324 }
14325 }
14326 }
14327 }
14328
14329 /* Decode the Line Number Program (LNP) for the given line_header
14330 structure and CU. The actual information extracted and the type
14331 of structures created from the LNP depends on the value of PST.
14332
14333 1. If PST is NULL, then this procedure uses the data from the program
14334 to create all necessary symbol tables, and their linetables.
14335
14336 2. If PST is not NULL, this procedure reads the program to determine
14337 the list of files included by the unit represented by PST, and
14338 builds all the associated partial symbol tables.
14339
14340 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14341 It is used for relative paths in the line table.
14342 NOTE: When processing partial symtabs (pst != NULL),
14343 comp_dir == pst->dirname.
14344
14345 NOTE: It is important that psymtabs have the same file name (via strcmp)
14346 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14347 symtab we don't use it in the name of the psymtabs we create.
14348 E.g. expand_line_sal requires this when finding psymtabs to expand.
14349 A good testcase for this is mb-inline.exp. */
14350
14351 static void
14352 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14353 struct dwarf2_cu *cu, struct partial_symtab *pst,
14354 int want_line_info)
14355 {
14356 struct objfile *objfile = cu->objfile;
14357 const int decode_for_pst_p = (pst != NULL);
14358 struct subfile *first_subfile = current_subfile;
14359
14360 if (want_line_info)
14361 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14362
14363 if (decode_for_pst_p)
14364 {
14365 int file_index;
14366
14367 /* Now that we're done scanning the Line Header Program, we can
14368 create the psymtab of each included file. */
14369 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14370 if (lh->file_names[file_index].included_p == 1)
14371 {
14372 char *include_name =
14373 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14374 if (include_name != NULL)
14375 dwarf2_create_include_psymtab (include_name, pst, objfile);
14376 }
14377 }
14378 else
14379 {
14380 /* Make sure a symtab is created for every file, even files
14381 which contain only variables (i.e. no code with associated
14382 line numbers). */
14383 int i;
14384
14385 for (i = 0; i < lh->num_file_names; i++)
14386 {
14387 char *dir = NULL;
14388 struct file_entry *fe;
14389
14390 fe = &lh->file_names[i];
14391 if (fe->dir_index)
14392 dir = lh->include_dirs[fe->dir_index - 1];
14393 dwarf2_start_subfile (fe->name, dir, comp_dir);
14394
14395 /* Skip the main file; we don't need it, and it must be
14396 allocated last, so that it will show up before the
14397 non-primary symtabs in the objfile's symtab list. */
14398 if (current_subfile == first_subfile)
14399 continue;
14400
14401 if (current_subfile->symtab == NULL)
14402 current_subfile->symtab = allocate_symtab (current_subfile->name,
14403 objfile);
14404 fe->symtab = current_subfile->symtab;
14405 }
14406 }
14407 }
14408
14409 /* Start a subfile for DWARF. FILENAME is the name of the file and
14410 DIRNAME the name of the source directory which contains FILENAME
14411 or NULL if not known. COMP_DIR is the compilation directory for the
14412 linetable's compilation unit or NULL if not known.
14413 This routine tries to keep line numbers from identical absolute and
14414 relative file names in a common subfile.
14415
14416 Using the `list' example from the GDB testsuite, which resides in
14417 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14418 of /srcdir/list0.c yields the following debugging information for list0.c:
14419
14420 DW_AT_name: /srcdir/list0.c
14421 DW_AT_comp_dir: /compdir
14422 files.files[0].name: list0.h
14423 files.files[0].dir: /srcdir
14424 files.files[1].name: list0.c
14425 files.files[1].dir: /srcdir
14426
14427 The line number information for list0.c has to end up in a single
14428 subfile, so that `break /srcdir/list0.c:1' works as expected.
14429 start_subfile will ensure that this happens provided that we pass the
14430 concatenation of files.files[1].dir and files.files[1].name as the
14431 subfile's name. */
14432
14433 static void
14434 dwarf2_start_subfile (char *filename, const char *dirname,
14435 const char *comp_dir)
14436 {
14437 char *fullname;
14438
14439 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14440 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14441 second argument to start_subfile. To be consistent, we do the
14442 same here. In order not to lose the line information directory,
14443 we concatenate it to the filename when it makes sense.
14444 Note that the Dwarf3 standard says (speaking of filenames in line
14445 information): ``The directory index is ignored for file names
14446 that represent full path names''. Thus ignoring dirname in the
14447 `else' branch below isn't an issue. */
14448
14449 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14450 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14451 else
14452 fullname = filename;
14453
14454 start_subfile (fullname, comp_dir);
14455
14456 if (fullname != filename)
14457 xfree (fullname);
14458 }
14459
14460 /* Start a symtab for DWARF.
14461 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14462
14463 static void
14464 dwarf2_start_symtab (struct dwarf2_cu *cu,
14465 char *name, char *comp_dir, CORE_ADDR low_pc)
14466 {
14467 start_symtab (name, comp_dir, low_pc);
14468 record_debugformat ("DWARF 2");
14469 record_producer (cu->producer);
14470
14471 /* We assume that we're processing GCC output. */
14472 processing_gcc_compilation = 2;
14473
14474 processing_has_namespace_info = 0;
14475 }
14476
14477 static void
14478 var_decode_location (struct attribute *attr, struct symbol *sym,
14479 struct dwarf2_cu *cu)
14480 {
14481 struct objfile *objfile = cu->objfile;
14482 struct comp_unit_head *cu_header = &cu->header;
14483
14484 /* NOTE drow/2003-01-30: There used to be a comment and some special
14485 code here to turn a symbol with DW_AT_external and a
14486 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14487 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14488 with some versions of binutils) where shared libraries could have
14489 relocations against symbols in their debug information - the
14490 minimal symbol would have the right address, but the debug info
14491 would not. It's no longer necessary, because we will explicitly
14492 apply relocations when we read in the debug information now. */
14493
14494 /* A DW_AT_location attribute with no contents indicates that a
14495 variable has been optimized away. */
14496 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14497 {
14498 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14499 return;
14500 }
14501
14502 /* Handle one degenerate form of location expression specially, to
14503 preserve GDB's previous behavior when section offsets are
14504 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14505 then mark this symbol as LOC_STATIC. */
14506
14507 if (attr_form_is_block (attr)
14508 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14509 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14510 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14511 && (DW_BLOCK (attr)->size
14512 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14513 {
14514 unsigned int dummy;
14515
14516 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14517 SYMBOL_VALUE_ADDRESS (sym) =
14518 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14519 else
14520 SYMBOL_VALUE_ADDRESS (sym) =
14521 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14522 SYMBOL_CLASS (sym) = LOC_STATIC;
14523 fixup_symbol_section (sym, objfile);
14524 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14525 SYMBOL_SECTION (sym));
14526 return;
14527 }
14528
14529 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14530 expression evaluator, and use LOC_COMPUTED only when necessary
14531 (i.e. when the value of a register or memory location is
14532 referenced, or a thread-local block, etc.). Then again, it might
14533 not be worthwhile. I'm assuming that it isn't unless performance
14534 or memory numbers show me otherwise. */
14535
14536 dwarf2_symbol_mark_computed (attr, sym, cu);
14537 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14538
14539 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14540 cu->has_loclist = 1;
14541 }
14542
14543 /* Given a pointer to a DWARF information entry, figure out if we need
14544 to make a symbol table entry for it, and if so, create a new entry
14545 and return a pointer to it.
14546 If TYPE is NULL, determine symbol type from the die, otherwise
14547 used the passed type.
14548 If SPACE is not NULL, use it to hold the new symbol. If it is
14549 NULL, allocate a new symbol on the objfile's obstack. */
14550
14551 static struct symbol *
14552 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14553 struct symbol *space)
14554 {
14555 struct objfile *objfile = cu->objfile;
14556 struct symbol *sym = NULL;
14557 char *name;
14558 struct attribute *attr = NULL;
14559 struct attribute *attr2 = NULL;
14560 CORE_ADDR baseaddr;
14561 struct pending **list_to_add = NULL;
14562
14563 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14564
14565 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14566
14567 name = dwarf2_name (die, cu);
14568 if (name)
14569 {
14570 const char *linkagename;
14571 int suppress_add = 0;
14572
14573 if (space)
14574 sym = space;
14575 else
14576 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14577 OBJSTAT (objfile, n_syms++);
14578
14579 /* Cache this symbol's name and the name's demangled form (if any). */
14580 SYMBOL_SET_LANGUAGE (sym, cu->language);
14581 linkagename = dwarf2_physname (name, die, cu);
14582 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14583
14584 /* Fortran does not have mangling standard and the mangling does differ
14585 between gfortran, iFort etc. */
14586 if (cu->language == language_fortran
14587 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14588 symbol_set_demangled_name (&(sym->ginfo),
14589 (char *) dwarf2_full_name (name, die, cu),
14590 NULL);
14591
14592 /* Default assumptions.
14593 Use the passed type or decode it from the die. */
14594 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14595 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14596 if (type != NULL)
14597 SYMBOL_TYPE (sym) = type;
14598 else
14599 SYMBOL_TYPE (sym) = die_type (die, cu);
14600 attr = dwarf2_attr (die,
14601 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14602 cu);
14603 if (attr)
14604 {
14605 SYMBOL_LINE (sym) = DW_UNSND (attr);
14606 }
14607
14608 attr = dwarf2_attr (die,
14609 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14610 cu);
14611 if (attr)
14612 {
14613 int file_index = DW_UNSND (attr);
14614
14615 if (cu->line_header == NULL
14616 || file_index > cu->line_header->num_file_names)
14617 complaint (&symfile_complaints,
14618 _("file index out of range"));
14619 else if (file_index > 0)
14620 {
14621 struct file_entry *fe;
14622
14623 fe = &cu->line_header->file_names[file_index - 1];
14624 SYMBOL_SYMTAB (sym) = fe->symtab;
14625 }
14626 }
14627
14628 switch (die->tag)
14629 {
14630 case DW_TAG_label:
14631 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14632 if (attr)
14633 {
14634 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14635 }
14636 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14637 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14638 SYMBOL_CLASS (sym) = LOC_LABEL;
14639 add_symbol_to_list (sym, cu->list_in_scope);
14640 break;
14641 case DW_TAG_subprogram:
14642 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14643 finish_block. */
14644 SYMBOL_CLASS (sym) = LOC_BLOCK;
14645 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14646 if ((attr2 && (DW_UNSND (attr2) != 0))
14647 || cu->language == language_ada)
14648 {
14649 /* Subprograms marked external are stored as a global symbol.
14650 Ada subprograms, whether marked external or not, are always
14651 stored as a global symbol, because we want to be able to
14652 access them globally. For instance, we want to be able
14653 to break on a nested subprogram without having to
14654 specify the context. */
14655 list_to_add = &global_symbols;
14656 }
14657 else
14658 {
14659 list_to_add = cu->list_in_scope;
14660 }
14661 break;
14662 case DW_TAG_inlined_subroutine:
14663 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14664 finish_block. */
14665 SYMBOL_CLASS (sym) = LOC_BLOCK;
14666 SYMBOL_INLINED (sym) = 1;
14667 list_to_add = cu->list_in_scope;
14668 break;
14669 case DW_TAG_template_value_param:
14670 suppress_add = 1;
14671 /* Fall through. */
14672 case DW_TAG_constant:
14673 case DW_TAG_variable:
14674 case DW_TAG_member:
14675 /* Compilation with minimal debug info may result in
14676 variables with missing type entries. Change the
14677 misleading `void' type to something sensible. */
14678 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14679 SYMBOL_TYPE (sym)
14680 = objfile_type (objfile)->nodebug_data_symbol;
14681
14682 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14683 /* In the case of DW_TAG_member, we should only be called for
14684 static const members. */
14685 if (die->tag == DW_TAG_member)
14686 {
14687 /* dwarf2_add_field uses die_is_declaration,
14688 so we do the same. */
14689 gdb_assert (die_is_declaration (die, cu));
14690 gdb_assert (attr);
14691 }
14692 if (attr)
14693 {
14694 dwarf2_const_value (attr, sym, cu);
14695 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14696 if (!suppress_add)
14697 {
14698 if (attr2 && (DW_UNSND (attr2) != 0))
14699 list_to_add = &global_symbols;
14700 else
14701 list_to_add = cu->list_in_scope;
14702 }
14703 break;
14704 }
14705 attr = dwarf2_attr (die, DW_AT_location, cu);
14706 if (attr)
14707 {
14708 var_decode_location (attr, sym, cu);
14709 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14710 if (SYMBOL_CLASS (sym) == LOC_STATIC
14711 && SYMBOL_VALUE_ADDRESS (sym) == 0
14712 && !dwarf2_per_objfile->has_section_at_zero)
14713 {
14714 /* When a static variable is eliminated by the linker,
14715 the corresponding debug information is not stripped
14716 out, but the variable address is set to null;
14717 do not add such variables into symbol table. */
14718 }
14719 else if (attr2 && (DW_UNSND (attr2) != 0))
14720 {
14721 /* Workaround gfortran PR debug/40040 - it uses
14722 DW_AT_location for variables in -fPIC libraries which may
14723 get overriden by other libraries/executable and get
14724 a different address. Resolve it by the minimal symbol
14725 which may come from inferior's executable using copy
14726 relocation. Make this workaround only for gfortran as for
14727 other compilers GDB cannot guess the minimal symbol
14728 Fortran mangling kind. */
14729 if (cu->language == language_fortran && die->parent
14730 && die->parent->tag == DW_TAG_module
14731 && cu->producer
14732 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14733 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14734
14735 /* A variable with DW_AT_external is never static,
14736 but it may be block-scoped. */
14737 list_to_add = (cu->list_in_scope == &file_symbols
14738 ? &global_symbols : cu->list_in_scope);
14739 }
14740 else
14741 list_to_add = cu->list_in_scope;
14742 }
14743 else
14744 {
14745 /* We do not know the address of this symbol.
14746 If it is an external symbol and we have type information
14747 for it, enter the symbol as a LOC_UNRESOLVED symbol.
14748 The address of the variable will then be determined from
14749 the minimal symbol table whenever the variable is
14750 referenced. */
14751 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14752 if (attr2 && (DW_UNSND (attr2) != 0)
14753 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
14754 {
14755 /* A variable with DW_AT_external is never static, but it
14756 may be block-scoped. */
14757 list_to_add = (cu->list_in_scope == &file_symbols
14758 ? &global_symbols : cu->list_in_scope);
14759
14760 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14761 }
14762 else if (!die_is_declaration (die, cu))
14763 {
14764 /* Use the default LOC_OPTIMIZED_OUT class. */
14765 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
14766 if (!suppress_add)
14767 list_to_add = cu->list_in_scope;
14768 }
14769 }
14770 break;
14771 case DW_TAG_formal_parameter:
14772 /* If we are inside a function, mark this as an argument. If
14773 not, we might be looking at an argument to an inlined function
14774 when we do not have enough information to show inlined frames;
14775 pretend it's a local variable in that case so that the user can
14776 still see it. */
14777 if (context_stack_depth > 0
14778 && context_stack[context_stack_depth - 1].name != NULL)
14779 SYMBOL_IS_ARGUMENT (sym) = 1;
14780 attr = dwarf2_attr (die, DW_AT_location, cu);
14781 if (attr)
14782 {
14783 var_decode_location (attr, sym, cu);
14784 }
14785 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14786 if (attr)
14787 {
14788 dwarf2_const_value (attr, sym, cu);
14789 }
14790
14791 list_to_add = cu->list_in_scope;
14792 break;
14793 case DW_TAG_unspecified_parameters:
14794 /* From varargs functions; gdb doesn't seem to have any
14795 interest in this information, so just ignore it for now.
14796 (FIXME?) */
14797 break;
14798 case DW_TAG_template_type_param:
14799 suppress_add = 1;
14800 /* Fall through. */
14801 case DW_TAG_class_type:
14802 case DW_TAG_interface_type:
14803 case DW_TAG_structure_type:
14804 case DW_TAG_union_type:
14805 case DW_TAG_set_type:
14806 case DW_TAG_enumeration_type:
14807 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14808 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
14809
14810 {
14811 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
14812 really ever be static objects: otherwise, if you try
14813 to, say, break of a class's method and you're in a file
14814 which doesn't mention that class, it won't work unless
14815 the check for all static symbols in lookup_symbol_aux
14816 saves you. See the OtherFileClass tests in
14817 gdb.c++/namespace.exp. */
14818
14819 if (!suppress_add)
14820 {
14821 list_to_add = (cu->list_in_scope == &file_symbols
14822 && (cu->language == language_cplus
14823 || cu->language == language_java)
14824 ? &global_symbols : cu->list_in_scope);
14825
14826 /* The semantics of C++ state that "struct foo {
14827 ... }" also defines a typedef for "foo". A Java
14828 class declaration also defines a typedef for the
14829 class. */
14830 if (cu->language == language_cplus
14831 || cu->language == language_java
14832 || cu->language == language_ada)
14833 {
14834 /* The symbol's name is already allocated along
14835 with this objfile, so we don't need to
14836 duplicate it for the type. */
14837 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
14838 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
14839 }
14840 }
14841 }
14842 break;
14843 case DW_TAG_typedef:
14844 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14845 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14846 list_to_add = cu->list_in_scope;
14847 break;
14848 case DW_TAG_base_type:
14849 case DW_TAG_subrange_type:
14850 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14851 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14852 list_to_add = cu->list_in_scope;
14853 break;
14854 case DW_TAG_enumerator:
14855 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14856 if (attr)
14857 {
14858 dwarf2_const_value (attr, sym, cu);
14859 }
14860 {
14861 /* NOTE: carlton/2003-11-10: See comment above in the
14862 DW_TAG_class_type, etc. block. */
14863
14864 list_to_add = (cu->list_in_scope == &file_symbols
14865 && (cu->language == language_cplus
14866 || cu->language == language_java)
14867 ? &global_symbols : cu->list_in_scope);
14868 }
14869 break;
14870 case DW_TAG_namespace:
14871 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14872 list_to_add = &global_symbols;
14873 break;
14874 default:
14875 /* Not a tag we recognize. Hopefully we aren't processing
14876 trash data, but since we must specifically ignore things
14877 we don't recognize, there is nothing else we should do at
14878 this point. */
14879 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
14880 dwarf_tag_name (die->tag));
14881 break;
14882 }
14883
14884 if (suppress_add)
14885 {
14886 sym->hash_next = objfile->template_symbols;
14887 objfile->template_symbols = sym;
14888 list_to_add = NULL;
14889 }
14890
14891 if (list_to_add != NULL)
14892 add_symbol_to_list (sym, list_to_add);
14893
14894 /* For the benefit of old versions of GCC, check for anonymous
14895 namespaces based on the demangled name. */
14896 if (!processing_has_namespace_info
14897 && cu->language == language_cplus)
14898 cp_scan_for_anonymous_namespaces (sym, objfile);
14899 }
14900 return (sym);
14901 }
14902
14903 /* A wrapper for new_symbol_full that always allocates a new symbol. */
14904
14905 static struct symbol *
14906 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14907 {
14908 return new_symbol_full (die, type, cu, NULL);
14909 }
14910
14911 /* Given an attr with a DW_FORM_dataN value in host byte order,
14912 zero-extend it as appropriate for the symbol's type. The DWARF
14913 standard (v4) is not entirely clear about the meaning of using
14914 DW_FORM_dataN for a constant with a signed type, where the type is
14915 wider than the data. The conclusion of a discussion on the DWARF
14916 list was that this is unspecified. We choose to always zero-extend
14917 because that is the interpretation long in use by GCC. */
14918
14919 static gdb_byte *
14920 dwarf2_const_value_data (struct attribute *attr, struct type *type,
14921 const char *name, struct obstack *obstack,
14922 struct dwarf2_cu *cu, LONGEST *value, int bits)
14923 {
14924 struct objfile *objfile = cu->objfile;
14925 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
14926 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
14927 LONGEST l = DW_UNSND (attr);
14928
14929 if (bits < sizeof (*value) * 8)
14930 {
14931 l &= ((LONGEST) 1 << bits) - 1;
14932 *value = l;
14933 }
14934 else if (bits == sizeof (*value) * 8)
14935 *value = l;
14936 else
14937 {
14938 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
14939 store_unsigned_integer (bytes, bits / 8, byte_order, l);
14940 return bytes;
14941 }
14942
14943 return NULL;
14944 }
14945
14946 /* Read a constant value from an attribute. Either set *VALUE, or if
14947 the value does not fit in *VALUE, set *BYTES - either already
14948 allocated on the objfile obstack, or newly allocated on OBSTACK,
14949 or, set *BATON, if we translated the constant to a location
14950 expression. */
14951
14952 static void
14953 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
14954 const char *name, struct obstack *obstack,
14955 struct dwarf2_cu *cu,
14956 LONGEST *value, gdb_byte **bytes,
14957 struct dwarf2_locexpr_baton **baton)
14958 {
14959 struct objfile *objfile = cu->objfile;
14960 struct comp_unit_head *cu_header = &cu->header;
14961 struct dwarf_block *blk;
14962 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
14963 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
14964
14965 *value = 0;
14966 *bytes = NULL;
14967 *baton = NULL;
14968
14969 switch (attr->form)
14970 {
14971 case DW_FORM_addr:
14972 case DW_FORM_GNU_addr_index:
14973 {
14974 gdb_byte *data;
14975
14976 if (TYPE_LENGTH (type) != cu_header->addr_size)
14977 dwarf2_const_value_length_mismatch_complaint (name,
14978 cu_header->addr_size,
14979 TYPE_LENGTH (type));
14980 /* Symbols of this form are reasonably rare, so we just
14981 piggyback on the existing location code rather than writing
14982 a new implementation of symbol_computed_ops. */
14983 *baton = obstack_alloc (&objfile->objfile_obstack,
14984 sizeof (struct dwarf2_locexpr_baton));
14985 (*baton)->per_cu = cu->per_cu;
14986 gdb_assert ((*baton)->per_cu);
14987
14988 (*baton)->size = 2 + cu_header->addr_size;
14989 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14990 (*baton)->data = data;
14991
14992 data[0] = DW_OP_addr;
14993 store_unsigned_integer (&data[1], cu_header->addr_size,
14994 byte_order, DW_ADDR (attr));
14995 data[cu_header->addr_size + 1] = DW_OP_stack_value;
14996 }
14997 break;
14998 case DW_FORM_string:
14999 case DW_FORM_strp:
15000 case DW_FORM_GNU_str_index:
15001 /* DW_STRING is already allocated on the objfile obstack, point
15002 directly to it. */
15003 *bytes = (gdb_byte *) DW_STRING (attr);
15004 break;
15005 case DW_FORM_block1:
15006 case DW_FORM_block2:
15007 case DW_FORM_block4:
15008 case DW_FORM_block:
15009 case DW_FORM_exprloc:
15010 blk = DW_BLOCK (attr);
15011 if (TYPE_LENGTH (type) != blk->size)
15012 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15013 TYPE_LENGTH (type));
15014 *bytes = blk->data;
15015 break;
15016
15017 /* The DW_AT_const_value attributes are supposed to carry the
15018 symbol's value "represented as it would be on the target
15019 architecture." By the time we get here, it's already been
15020 converted to host endianness, so we just need to sign- or
15021 zero-extend it as appropriate. */
15022 case DW_FORM_data1:
15023 *bytes = dwarf2_const_value_data (attr, type, name,
15024 obstack, cu, value, 8);
15025 break;
15026 case DW_FORM_data2:
15027 *bytes = dwarf2_const_value_data (attr, type, name,
15028 obstack, cu, value, 16);
15029 break;
15030 case DW_FORM_data4:
15031 *bytes = dwarf2_const_value_data (attr, type, name,
15032 obstack, cu, value, 32);
15033 break;
15034 case DW_FORM_data8:
15035 *bytes = dwarf2_const_value_data (attr, type, name,
15036 obstack, cu, value, 64);
15037 break;
15038
15039 case DW_FORM_sdata:
15040 *value = DW_SND (attr);
15041 break;
15042
15043 case DW_FORM_udata:
15044 *value = DW_UNSND (attr);
15045 break;
15046
15047 default:
15048 complaint (&symfile_complaints,
15049 _("unsupported const value attribute form: '%s'"),
15050 dwarf_form_name (attr->form));
15051 *value = 0;
15052 break;
15053 }
15054 }
15055
15056
15057 /* Copy constant value from an attribute to a symbol. */
15058
15059 static void
15060 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15061 struct dwarf2_cu *cu)
15062 {
15063 struct objfile *objfile = cu->objfile;
15064 struct comp_unit_head *cu_header = &cu->header;
15065 LONGEST value;
15066 gdb_byte *bytes;
15067 struct dwarf2_locexpr_baton *baton;
15068
15069 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15070 SYMBOL_PRINT_NAME (sym),
15071 &objfile->objfile_obstack, cu,
15072 &value, &bytes, &baton);
15073
15074 if (baton != NULL)
15075 {
15076 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15077 SYMBOL_LOCATION_BATON (sym) = baton;
15078 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15079 }
15080 else if (bytes != NULL)
15081 {
15082 SYMBOL_VALUE_BYTES (sym) = bytes;
15083 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15084 }
15085 else
15086 {
15087 SYMBOL_VALUE (sym) = value;
15088 SYMBOL_CLASS (sym) = LOC_CONST;
15089 }
15090 }
15091
15092 /* Return the type of the die in question using its DW_AT_type attribute. */
15093
15094 static struct type *
15095 die_type (struct die_info *die, struct dwarf2_cu *cu)
15096 {
15097 struct attribute *type_attr;
15098
15099 type_attr = dwarf2_attr (die, DW_AT_type, cu);
15100 if (!type_attr)
15101 {
15102 /* A missing DW_AT_type represents a void type. */
15103 return objfile_type (cu->objfile)->builtin_void;
15104 }
15105
15106 return lookup_die_type (die, type_attr, cu);
15107 }
15108
15109 /* True iff CU's producer generates GNAT Ada auxiliary information
15110 that allows to find parallel types through that information instead
15111 of having to do expensive parallel lookups by type name. */
15112
15113 static int
15114 need_gnat_info (struct dwarf2_cu *cu)
15115 {
15116 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15117 of GNAT produces this auxiliary information, without any indication
15118 that it is produced. Part of enhancing the FSF version of GNAT
15119 to produce that information will be to put in place an indicator
15120 that we can use in order to determine whether the descriptive type
15121 info is available or not. One suggestion that has been made is
15122 to use a new attribute, attached to the CU die. For now, assume
15123 that the descriptive type info is not available. */
15124 return 0;
15125 }
15126
15127 /* Return the auxiliary type of the die in question using its
15128 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15129 attribute is not present. */
15130
15131 static struct type *
15132 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15133 {
15134 struct attribute *type_attr;
15135
15136 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15137 if (!type_attr)
15138 return NULL;
15139
15140 return lookup_die_type (die, type_attr, cu);
15141 }
15142
15143 /* If DIE has a descriptive_type attribute, then set the TYPE's
15144 descriptive type accordingly. */
15145
15146 static void
15147 set_descriptive_type (struct type *type, struct die_info *die,
15148 struct dwarf2_cu *cu)
15149 {
15150 struct type *descriptive_type = die_descriptive_type (die, cu);
15151
15152 if (descriptive_type)
15153 {
15154 ALLOCATE_GNAT_AUX_TYPE (type);
15155 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15156 }
15157 }
15158
15159 /* Return the containing type of the die in question using its
15160 DW_AT_containing_type attribute. */
15161
15162 static struct type *
15163 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15164 {
15165 struct attribute *type_attr;
15166
15167 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15168 if (!type_attr)
15169 error (_("Dwarf Error: Problem turning containing type into gdb type "
15170 "[in module %s]"), cu->objfile->name);
15171
15172 return lookup_die_type (die, type_attr, cu);
15173 }
15174
15175 /* Look up the type of DIE in CU using its type attribute ATTR.
15176 If there is no type substitute an error marker. */
15177
15178 static struct type *
15179 lookup_die_type (struct die_info *die, struct attribute *attr,
15180 struct dwarf2_cu *cu)
15181 {
15182 struct objfile *objfile = cu->objfile;
15183 struct type *this_type;
15184
15185 /* First see if we have it cached. */
15186
15187 if (is_ref_attr (attr))
15188 {
15189 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15190
15191 this_type = get_die_type_at_offset (offset, cu->per_cu);
15192 }
15193 else if (attr->form == DW_FORM_ref_sig8)
15194 {
15195 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15196
15197 /* sig_type will be NULL if the signatured type is missing from
15198 the debug info. */
15199 if (sig_type == NULL)
15200 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15201 "at 0x%x [in module %s]"),
15202 die->offset.sect_off, objfile->name);
15203
15204 gdb_assert (sig_type->per_cu.is_debug_types);
15205 /* If we haven't filled in type_offset_in_section yet, then we
15206 haven't read the type in yet. */
15207 this_type = NULL;
15208 if (sig_type->type_offset_in_section.sect_off != 0)
15209 {
15210 this_type =
15211 get_die_type_at_offset (sig_type->type_offset_in_section,
15212 &sig_type->per_cu);
15213 }
15214 }
15215 else
15216 {
15217 dump_die_for_error (die);
15218 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15219 dwarf_attr_name (attr->name), objfile->name);
15220 }
15221
15222 /* If not cached we need to read it in. */
15223
15224 if (this_type == NULL)
15225 {
15226 struct die_info *type_die;
15227 struct dwarf2_cu *type_cu = cu;
15228
15229 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15230 /* If we found the type now, it's probably because the type came
15231 from an inter-CU reference and the type's CU got expanded before
15232 ours. */
15233 this_type = get_die_type (type_die, type_cu);
15234 if (this_type == NULL)
15235 this_type = read_type_die_1 (type_die, type_cu);
15236 }
15237
15238 /* If we still don't have a type use an error marker. */
15239
15240 if (this_type == NULL)
15241 {
15242 char *message, *saved;
15243
15244 /* read_type_die already issued a complaint. */
15245 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15246 objfile->name,
15247 cu->header.offset.sect_off,
15248 die->offset.sect_off);
15249 saved = obstack_copy0 (&objfile->objfile_obstack,
15250 message, strlen (message));
15251 xfree (message);
15252
15253 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15254 }
15255
15256 return this_type;
15257 }
15258
15259 /* Return the type in DIE, CU.
15260 Returns NULL for invalid types.
15261
15262 This first does a lookup in the appropriate type_hash table,
15263 and only reads the die in if necessary.
15264
15265 NOTE: This can be called when reading in partial or full symbols. */
15266
15267 static struct type *
15268 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15269 {
15270 struct type *this_type;
15271
15272 this_type = get_die_type (die, cu);
15273 if (this_type)
15274 return this_type;
15275
15276 return read_type_die_1 (die, cu);
15277 }
15278
15279 /* Read the type in DIE, CU.
15280 Returns NULL for invalid types. */
15281
15282 static struct type *
15283 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15284 {
15285 struct type *this_type = NULL;
15286
15287 switch (die->tag)
15288 {
15289 case DW_TAG_class_type:
15290 case DW_TAG_interface_type:
15291 case DW_TAG_structure_type:
15292 case DW_TAG_union_type:
15293 this_type = read_structure_type (die, cu);
15294 break;
15295 case DW_TAG_enumeration_type:
15296 this_type = read_enumeration_type (die, cu);
15297 break;
15298 case DW_TAG_subprogram:
15299 case DW_TAG_subroutine_type:
15300 case DW_TAG_inlined_subroutine:
15301 this_type = read_subroutine_type (die, cu);
15302 break;
15303 case DW_TAG_array_type:
15304 this_type = read_array_type (die, cu);
15305 break;
15306 case DW_TAG_set_type:
15307 this_type = read_set_type (die, cu);
15308 break;
15309 case DW_TAG_pointer_type:
15310 this_type = read_tag_pointer_type (die, cu);
15311 break;
15312 case DW_TAG_ptr_to_member_type:
15313 this_type = read_tag_ptr_to_member_type (die, cu);
15314 break;
15315 case DW_TAG_reference_type:
15316 this_type = read_tag_reference_type (die, cu);
15317 break;
15318 case DW_TAG_const_type:
15319 this_type = read_tag_const_type (die, cu);
15320 break;
15321 case DW_TAG_volatile_type:
15322 this_type = read_tag_volatile_type (die, cu);
15323 break;
15324 case DW_TAG_string_type:
15325 this_type = read_tag_string_type (die, cu);
15326 break;
15327 case DW_TAG_typedef:
15328 this_type = read_typedef (die, cu);
15329 break;
15330 case DW_TAG_subrange_type:
15331 this_type = read_subrange_type (die, cu);
15332 break;
15333 case DW_TAG_base_type:
15334 this_type = read_base_type (die, cu);
15335 break;
15336 case DW_TAG_unspecified_type:
15337 this_type = read_unspecified_type (die, cu);
15338 break;
15339 case DW_TAG_namespace:
15340 this_type = read_namespace_type (die, cu);
15341 break;
15342 case DW_TAG_module:
15343 this_type = read_module_type (die, cu);
15344 break;
15345 default:
15346 complaint (&symfile_complaints,
15347 _("unexpected tag in read_type_die: '%s'"),
15348 dwarf_tag_name (die->tag));
15349 break;
15350 }
15351
15352 return this_type;
15353 }
15354
15355 /* See if we can figure out if the class lives in a namespace. We do
15356 this by looking for a member function; its demangled name will
15357 contain namespace info, if there is any.
15358 Return the computed name or NULL.
15359 Space for the result is allocated on the objfile's obstack.
15360 This is the full-die version of guess_partial_die_structure_name.
15361 In this case we know DIE has no useful parent. */
15362
15363 static char *
15364 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15365 {
15366 struct die_info *spec_die;
15367 struct dwarf2_cu *spec_cu;
15368 struct die_info *child;
15369
15370 spec_cu = cu;
15371 spec_die = die_specification (die, &spec_cu);
15372 if (spec_die != NULL)
15373 {
15374 die = spec_die;
15375 cu = spec_cu;
15376 }
15377
15378 for (child = die->child;
15379 child != NULL;
15380 child = child->sibling)
15381 {
15382 if (child->tag == DW_TAG_subprogram)
15383 {
15384 struct attribute *attr;
15385
15386 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15387 if (attr == NULL)
15388 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15389 if (attr != NULL)
15390 {
15391 char *actual_name
15392 = language_class_name_from_physname (cu->language_defn,
15393 DW_STRING (attr));
15394 char *name = NULL;
15395
15396 if (actual_name != NULL)
15397 {
15398 char *die_name = dwarf2_name (die, cu);
15399
15400 if (die_name != NULL
15401 && strcmp (die_name, actual_name) != 0)
15402 {
15403 /* Strip off the class name from the full name.
15404 We want the prefix. */
15405 int die_name_len = strlen (die_name);
15406 int actual_name_len = strlen (actual_name);
15407
15408 /* Test for '::' as a sanity check. */
15409 if (actual_name_len > die_name_len + 2
15410 && actual_name[actual_name_len
15411 - die_name_len - 1] == ':')
15412 name =
15413 obsavestring (actual_name,
15414 actual_name_len - die_name_len - 2,
15415 &cu->objfile->objfile_obstack);
15416 }
15417 }
15418 xfree (actual_name);
15419 return name;
15420 }
15421 }
15422 }
15423
15424 return NULL;
15425 }
15426
15427 /* GCC might emit a nameless typedef that has a linkage name. Determine the
15428 prefix part in such case. See
15429 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15430
15431 static char *
15432 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15433 {
15434 struct attribute *attr;
15435 char *base;
15436
15437 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15438 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15439 return NULL;
15440
15441 attr = dwarf2_attr (die, DW_AT_name, cu);
15442 if (attr != NULL && DW_STRING (attr) != NULL)
15443 return NULL;
15444
15445 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15446 if (attr == NULL)
15447 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15448 if (attr == NULL || DW_STRING (attr) == NULL)
15449 return NULL;
15450
15451 /* dwarf2_name had to be already called. */
15452 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15453
15454 /* Strip the base name, keep any leading namespaces/classes. */
15455 base = strrchr (DW_STRING (attr), ':');
15456 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15457 return "";
15458
15459 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15460 &cu->objfile->objfile_obstack);
15461 }
15462
15463 /* Return the name of the namespace/class that DIE is defined within,
15464 or "" if we can't tell. The caller should not xfree the result.
15465
15466 For example, if we're within the method foo() in the following
15467 code:
15468
15469 namespace N {
15470 class C {
15471 void foo () {
15472 }
15473 };
15474 }
15475
15476 then determine_prefix on foo's die will return "N::C". */
15477
15478 static const char *
15479 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15480 {
15481 struct die_info *parent, *spec_die;
15482 struct dwarf2_cu *spec_cu;
15483 struct type *parent_type;
15484 char *retval;
15485
15486 if (cu->language != language_cplus && cu->language != language_java
15487 && cu->language != language_fortran)
15488 return "";
15489
15490 retval = anonymous_struct_prefix (die, cu);
15491 if (retval)
15492 return retval;
15493
15494 /* We have to be careful in the presence of DW_AT_specification.
15495 For example, with GCC 3.4, given the code
15496
15497 namespace N {
15498 void foo() {
15499 // Definition of N::foo.
15500 }
15501 }
15502
15503 then we'll have a tree of DIEs like this:
15504
15505 1: DW_TAG_compile_unit
15506 2: DW_TAG_namespace // N
15507 3: DW_TAG_subprogram // declaration of N::foo
15508 4: DW_TAG_subprogram // definition of N::foo
15509 DW_AT_specification // refers to die #3
15510
15511 Thus, when processing die #4, we have to pretend that we're in
15512 the context of its DW_AT_specification, namely the contex of die
15513 #3. */
15514 spec_cu = cu;
15515 spec_die = die_specification (die, &spec_cu);
15516 if (spec_die == NULL)
15517 parent = die->parent;
15518 else
15519 {
15520 parent = spec_die->parent;
15521 cu = spec_cu;
15522 }
15523
15524 if (parent == NULL)
15525 return "";
15526 else if (parent->building_fullname)
15527 {
15528 const char *name;
15529 const char *parent_name;
15530
15531 /* It has been seen on RealView 2.2 built binaries,
15532 DW_TAG_template_type_param types actually _defined_ as
15533 children of the parent class:
15534
15535 enum E {};
15536 template class <class Enum> Class{};
15537 Class<enum E> class_e;
15538
15539 1: DW_TAG_class_type (Class)
15540 2: DW_TAG_enumeration_type (E)
15541 3: DW_TAG_enumerator (enum1:0)
15542 3: DW_TAG_enumerator (enum2:1)
15543 ...
15544 2: DW_TAG_template_type_param
15545 DW_AT_type DW_FORM_ref_udata (E)
15546
15547 Besides being broken debug info, it can put GDB into an
15548 infinite loop. Consider:
15549
15550 When we're building the full name for Class<E>, we'll start
15551 at Class, and go look over its template type parameters,
15552 finding E. We'll then try to build the full name of E, and
15553 reach here. We're now trying to build the full name of E,
15554 and look over the parent DIE for containing scope. In the
15555 broken case, if we followed the parent DIE of E, we'd again
15556 find Class, and once again go look at its template type
15557 arguments, etc., etc. Simply don't consider such parent die
15558 as source-level parent of this die (it can't be, the language
15559 doesn't allow it), and break the loop here. */
15560 name = dwarf2_name (die, cu);
15561 parent_name = dwarf2_name (parent, cu);
15562 complaint (&symfile_complaints,
15563 _("template param type '%s' defined within parent '%s'"),
15564 name ? name : "<unknown>",
15565 parent_name ? parent_name : "<unknown>");
15566 return "";
15567 }
15568 else
15569 switch (parent->tag)
15570 {
15571 case DW_TAG_namespace:
15572 parent_type = read_type_die (parent, cu);
15573 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15574 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15575 Work around this problem here. */
15576 if (cu->language == language_cplus
15577 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15578 return "";
15579 /* We give a name to even anonymous namespaces. */
15580 return TYPE_TAG_NAME (parent_type);
15581 case DW_TAG_class_type:
15582 case DW_TAG_interface_type:
15583 case DW_TAG_structure_type:
15584 case DW_TAG_union_type:
15585 case DW_TAG_module:
15586 parent_type = read_type_die (parent, cu);
15587 if (TYPE_TAG_NAME (parent_type) != NULL)
15588 return TYPE_TAG_NAME (parent_type);
15589 else
15590 /* An anonymous structure is only allowed non-static data
15591 members; no typedefs, no member functions, et cetera.
15592 So it does not need a prefix. */
15593 return "";
15594 case DW_TAG_compile_unit:
15595 case DW_TAG_partial_unit:
15596 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15597 if (cu->language == language_cplus
15598 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15599 && die->child != NULL
15600 && (die->tag == DW_TAG_class_type
15601 || die->tag == DW_TAG_structure_type
15602 || die->tag == DW_TAG_union_type))
15603 {
15604 char *name = guess_full_die_structure_name (die, cu);
15605 if (name != NULL)
15606 return name;
15607 }
15608 return "";
15609 default:
15610 return determine_prefix (parent, cu);
15611 }
15612 }
15613
15614 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15615 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15616 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15617 an obconcat, otherwise allocate storage for the result. The CU argument is
15618 used to determine the language and hence, the appropriate separator. */
15619
15620 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
15621
15622 static char *
15623 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15624 int physname, struct dwarf2_cu *cu)
15625 {
15626 const char *lead = "";
15627 const char *sep;
15628
15629 if (suffix == NULL || suffix[0] == '\0'
15630 || prefix == NULL || prefix[0] == '\0')
15631 sep = "";
15632 else if (cu->language == language_java)
15633 sep = ".";
15634 else if (cu->language == language_fortran && physname)
15635 {
15636 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15637 DW_AT_MIPS_linkage_name is preferred and used instead. */
15638
15639 lead = "__";
15640 sep = "_MOD_";
15641 }
15642 else
15643 sep = "::";
15644
15645 if (prefix == NULL)
15646 prefix = "";
15647 if (suffix == NULL)
15648 suffix = "";
15649
15650 if (obs == NULL)
15651 {
15652 char *retval
15653 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15654
15655 strcpy (retval, lead);
15656 strcat (retval, prefix);
15657 strcat (retval, sep);
15658 strcat (retval, suffix);
15659 return retval;
15660 }
15661 else
15662 {
15663 /* We have an obstack. */
15664 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15665 }
15666 }
15667
15668 /* Return sibling of die, NULL if no sibling. */
15669
15670 static struct die_info *
15671 sibling_die (struct die_info *die)
15672 {
15673 return die->sibling;
15674 }
15675
15676 /* Get name of a die, return NULL if not found. */
15677
15678 static char *
15679 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15680 struct obstack *obstack)
15681 {
15682 if (name && cu->language == language_cplus)
15683 {
15684 char *canon_name = cp_canonicalize_string (name);
15685
15686 if (canon_name != NULL)
15687 {
15688 if (strcmp (canon_name, name) != 0)
15689 name = obsavestring (canon_name, strlen (canon_name),
15690 obstack);
15691 xfree (canon_name);
15692 }
15693 }
15694
15695 return name;
15696 }
15697
15698 /* Get name of a die, return NULL if not found. */
15699
15700 static char *
15701 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15702 {
15703 struct attribute *attr;
15704
15705 attr = dwarf2_attr (die, DW_AT_name, cu);
15706 if ((!attr || !DW_STRING (attr))
15707 && die->tag != DW_TAG_class_type
15708 && die->tag != DW_TAG_interface_type
15709 && die->tag != DW_TAG_structure_type
15710 && die->tag != DW_TAG_union_type)
15711 return NULL;
15712
15713 switch (die->tag)
15714 {
15715 case DW_TAG_compile_unit:
15716 case DW_TAG_partial_unit:
15717 /* Compilation units have a DW_AT_name that is a filename, not
15718 a source language identifier. */
15719 case DW_TAG_enumeration_type:
15720 case DW_TAG_enumerator:
15721 /* These tags always have simple identifiers already; no need
15722 to canonicalize them. */
15723 return DW_STRING (attr);
15724
15725 case DW_TAG_subprogram:
15726 /* Java constructors will all be named "<init>", so return
15727 the class name when we see this special case. */
15728 if (cu->language == language_java
15729 && DW_STRING (attr) != NULL
15730 && strcmp (DW_STRING (attr), "<init>") == 0)
15731 {
15732 struct dwarf2_cu *spec_cu = cu;
15733 struct die_info *spec_die;
15734
15735 /* GCJ will output '<init>' for Java constructor names.
15736 For this special case, return the name of the parent class. */
15737
15738 /* GCJ may output suprogram DIEs with AT_specification set.
15739 If so, use the name of the specified DIE. */
15740 spec_die = die_specification (die, &spec_cu);
15741 if (spec_die != NULL)
15742 return dwarf2_name (spec_die, spec_cu);
15743
15744 do
15745 {
15746 die = die->parent;
15747 if (die->tag == DW_TAG_class_type)
15748 return dwarf2_name (die, cu);
15749 }
15750 while (die->tag != DW_TAG_compile_unit
15751 && die->tag != DW_TAG_partial_unit);
15752 }
15753 break;
15754
15755 case DW_TAG_class_type:
15756 case DW_TAG_interface_type:
15757 case DW_TAG_structure_type:
15758 case DW_TAG_union_type:
15759 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
15760 structures or unions. These were of the form "._%d" in GCC 4.1,
15761 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
15762 and GCC 4.4. We work around this problem by ignoring these. */
15763 if (attr && DW_STRING (attr)
15764 && (strncmp (DW_STRING (attr), "._", 2) == 0
15765 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
15766 return NULL;
15767
15768 /* GCC might emit a nameless typedef that has a linkage name. See
15769 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15770 if (!attr || DW_STRING (attr) == NULL)
15771 {
15772 char *demangled = NULL;
15773
15774 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15775 if (attr == NULL)
15776 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15777
15778 if (attr == NULL || DW_STRING (attr) == NULL)
15779 return NULL;
15780
15781 /* Avoid demangling DW_STRING (attr) the second time on a second
15782 call for the same DIE. */
15783 if (!DW_STRING_IS_CANONICAL (attr))
15784 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
15785
15786 if (demangled)
15787 {
15788 char *base;
15789
15790 /* FIXME: we already did this for the partial symbol... */
15791 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
15792 &cu->objfile->objfile_obstack);
15793 DW_STRING_IS_CANONICAL (attr) = 1;
15794 xfree (demangled);
15795
15796 /* Strip any leading namespaces/classes, keep only the base name.
15797 DW_AT_name for named DIEs does not contain the prefixes. */
15798 base = strrchr (DW_STRING (attr), ':');
15799 if (base && base > DW_STRING (attr) && base[-1] == ':')
15800 return &base[1];
15801 else
15802 return DW_STRING (attr);
15803 }
15804 }
15805 break;
15806
15807 default:
15808 break;
15809 }
15810
15811 if (!DW_STRING_IS_CANONICAL (attr))
15812 {
15813 DW_STRING (attr)
15814 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
15815 &cu->objfile->objfile_obstack);
15816 DW_STRING_IS_CANONICAL (attr) = 1;
15817 }
15818 return DW_STRING (attr);
15819 }
15820
15821 /* Return the die that this die in an extension of, or NULL if there
15822 is none. *EXT_CU is the CU containing DIE on input, and the CU
15823 containing the return value on output. */
15824
15825 static struct die_info *
15826 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
15827 {
15828 struct attribute *attr;
15829
15830 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
15831 if (attr == NULL)
15832 return NULL;
15833
15834 return follow_die_ref (die, attr, ext_cu);
15835 }
15836
15837 /* Convert a DIE tag into its string name. */
15838
15839 static const char *
15840 dwarf_tag_name (unsigned tag)
15841 {
15842 const char *name = get_DW_TAG_name (tag);
15843
15844 if (name == NULL)
15845 return "DW_TAG_<unknown>";
15846
15847 return name;
15848 }
15849
15850 /* Convert a DWARF attribute code into its string name. */
15851
15852 static const char *
15853 dwarf_attr_name (unsigned attr)
15854 {
15855 const char *name;
15856
15857 #ifdef MIPS /* collides with DW_AT_HP_block_index */
15858 if (attr == DW_AT_MIPS_fde)
15859 return "DW_AT_MIPS_fde";
15860 #else
15861 if (attr == DW_AT_HP_block_index)
15862 return "DW_AT_HP_block_index";
15863 #endif
15864
15865 name = get_DW_AT_name (attr);
15866
15867 if (name == NULL)
15868 return "DW_AT_<unknown>";
15869
15870 return name;
15871 }
15872
15873 /* Convert a DWARF value form code into its string name. */
15874
15875 static const char *
15876 dwarf_form_name (unsigned form)
15877 {
15878 const char *name = get_DW_FORM_name (form);
15879
15880 if (name == NULL)
15881 return "DW_FORM_<unknown>";
15882
15883 return name;
15884 }
15885
15886 static char *
15887 dwarf_bool_name (unsigned mybool)
15888 {
15889 if (mybool)
15890 return "TRUE";
15891 else
15892 return "FALSE";
15893 }
15894
15895 /* Convert a DWARF type code into its string name. */
15896
15897 static const char *
15898 dwarf_type_encoding_name (unsigned enc)
15899 {
15900 const char *name = get_DW_ATE_name (enc);
15901
15902 if (name == NULL)
15903 return "DW_ATE_<unknown>";
15904
15905 return name;
15906 }
15907
15908 static void
15909 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
15910 {
15911 unsigned int i;
15912
15913 print_spaces (indent, f);
15914 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
15915 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
15916
15917 if (die->parent != NULL)
15918 {
15919 print_spaces (indent, f);
15920 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
15921 die->parent->offset.sect_off);
15922 }
15923
15924 print_spaces (indent, f);
15925 fprintf_unfiltered (f, " has children: %s\n",
15926 dwarf_bool_name (die->child != NULL));
15927
15928 print_spaces (indent, f);
15929 fprintf_unfiltered (f, " attributes:\n");
15930
15931 for (i = 0; i < die->num_attrs; ++i)
15932 {
15933 print_spaces (indent, f);
15934 fprintf_unfiltered (f, " %s (%s) ",
15935 dwarf_attr_name (die->attrs[i].name),
15936 dwarf_form_name (die->attrs[i].form));
15937
15938 switch (die->attrs[i].form)
15939 {
15940 case DW_FORM_addr:
15941 case DW_FORM_GNU_addr_index:
15942 fprintf_unfiltered (f, "address: ");
15943 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
15944 break;
15945 case DW_FORM_block2:
15946 case DW_FORM_block4:
15947 case DW_FORM_block:
15948 case DW_FORM_block1:
15949 fprintf_unfiltered (f, "block: size %d",
15950 DW_BLOCK (&die->attrs[i])->size);
15951 break;
15952 case DW_FORM_exprloc:
15953 fprintf_unfiltered (f, "expression: size %u",
15954 DW_BLOCK (&die->attrs[i])->size);
15955 break;
15956 case DW_FORM_ref_addr:
15957 fprintf_unfiltered (f, "ref address: ");
15958 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15959 break;
15960 case DW_FORM_ref1:
15961 case DW_FORM_ref2:
15962 case DW_FORM_ref4:
15963 case DW_FORM_ref8:
15964 case DW_FORM_ref_udata:
15965 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
15966 (long) (DW_UNSND (&die->attrs[i])));
15967 break;
15968 case DW_FORM_data1:
15969 case DW_FORM_data2:
15970 case DW_FORM_data4:
15971 case DW_FORM_data8:
15972 case DW_FORM_udata:
15973 case DW_FORM_sdata:
15974 fprintf_unfiltered (f, "constant: %s",
15975 pulongest (DW_UNSND (&die->attrs[i])));
15976 break;
15977 case DW_FORM_sec_offset:
15978 fprintf_unfiltered (f, "section offset: %s",
15979 pulongest (DW_UNSND (&die->attrs[i])));
15980 break;
15981 case DW_FORM_ref_sig8:
15982 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15983 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15984 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15985 else
15986 fprintf_unfiltered (f, "signatured type, offset: unknown");
15987 break;
15988 case DW_FORM_string:
15989 case DW_FORM_strp:
15990 case DW_FORM_GNU_str_index:
15991 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15992 DW_STRING (&die->attrs[i])
15993 ? DW_STRING (&die->attrs[i]) : "",
15994 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15995 break;
15996 case DW_FORM_flag:
15997 if (DW_UNSND (&die->attrs[i]))
15998 fprintf_unfiltered (f, "flag: TRUE");
15999 else
16000 fprintf_unfiltered (f, "flag: FALSE");
16001 break;
16002 case DW_FORM_flag_present:
16003 fprintf_unfiltered (f, "flag: TRUE");
16004 break;
16005 case DW_FORM_indirect:
16006 /* The reader will have reduced the indirect form to
16007 the "base form" so this form should not occur. */
16008 fprintf_unfiltered (f,
16009 "unexpected attribute form: DW_FORM_indirect");
16010 break;
16011 default:
16012 fprintf_unfiltered (f, "unsupported attribute form: %d.",
16013 die->attrs[i].form);
16014 break;
16015 }
16016 fprintf_unfiltered (f, "\n");
16017 }
16018 }
16019
16020 static void
16021 dump_die_for_error (struct die_info *die)
16022 {
16023 dump_die_shallow (gdb_stderr, 0, die);
16024 }
16025
16026 static void
16027 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16028 {
16029 int indent = level * 4;
16030
16031 gdb_assert (die != NULL);
16032
16033 if (level >= max_level)
16034 return;
16035
16036 dump_die_shallow (f, indent, die);
16037
16038 if (die->child != NULL)
16039 {
16040 print_spaces (indent, f);
16041 fprintf_unfiltered (f, " Children:");
16042 if (level + 1 < max_level)
16043 {
16044 fprintf_unfiltered (f, "\n");
16045 dump_die_1 (f, level + 1, max_level, die->child);
16046 }
16047 else
16048 {
16049 fprintf_unfiltered (f,
16050 " [not printed, max nesting level reached]\n");
16051 }
16052 }
16053
16054 if (die->sibling != NULL && level > 0)
16055 {
16056 dump_die_1 (f, level, max_level, die->sibling);
16057 }
16058 }
16059
16060 /* This is called from the pdie macro in gdbinit.in.
16061 It's not static so gcc will keep a copy callable from gdb. */
16062
16063 void
16064 dump_die (struct die_info *die, int max_level)
16065 {
16066 dump_die_1 (gdb_stdlog, 0, max_level, die);
16067 }
16068
16069 static void
16070 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16071 {
16072 void **slot;
16073
16074 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16075 INSERT);
16076
16077 *slot = die;
16078 }
16079
16080 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16081 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16082
16083 static int
16084 is_ref_attr (struct attribute *attr)
16085 {
16086 switch (attr->form)
16087 {
16088 case DW_FORM_ref_addr:
16089 case DW_FORM_ref1:
16090 case DW_FORM_ref2:
16091 case DW_FORM_ref4:
16092 case DW_FORM_ref8:
16093 case DW_FORM_ref_udata:
16094 return 1;
16095 default:
16096 return 0;
16097 }
16098 }
16099
16100 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16101 required kind. */
16102
16103 static sect_offset
16104 dwarf2_get_ref_die_offset (struct attribute *attr)
16105 {
16106 sect_offset retval = { DW_UNSND (attr) };
16107
16108 if (is_ref_attr (attr))
16109 return retval;
16110
16111 retval.sect_off = 0;
16112 complaint (&symfile_complaints,
16113 _("unsupported die ref attribute form: '%s'"),
16114 dwarf_form_name (attr->form));
16115 return retval;
16116 }
16117
16118 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16119 * the value held by the attribute is not constant. */
16120
16121 static LONGEST
16122 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16123 {
16124 if (attr->form == DW_FORM_sdata)
16125 return DW_SND (attr);
16126 else if (attr->form == DW_FORM_udata
16127 || attr->form == DW_FORM_data1
16128 || attr->form == DW_FORM_data2
16129 || attr->form == DW_FORM_data4
16130 || attr->form == DW_FORM_data8)
16131 return DW_UNSND (attr);
16132 else
16133 {
16134 complaint (&symfile_complaints,
16135 _("Attribute value is not a constant (%s)"),
16136 dwarf_form_name (attr->form));
16137 return default_value;
16138 }
16139 }
16140
16141 /* Follow reference or signature attribute ATTR of SRC_DIE.
16142 On entry *REF_CU is the CU of SRC_DIE.
16143 On exit *REF_CU is the CU of the result. */
16144
16145 static struct die_info *
16146 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16147 struct dwarf2_cu **ref_cu)
16148 {
16149 struct die_info *die;
16150
16151 if (is_ref_attr (attr))
16152 die = follow_die_ref (src_die, attr, ref_cu);
16153 else if (attr->form == DW_FORM_ref_sig8)
16154 die = follow_die_sig (src_die, attr, ref_cu);
16155 else
16156 {
16157 dump_die_for_error (src_die);
16158 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16159 (*ref_cu)->objfile->name);
16160 }
16161
16162 return die;
16163 }
16164
16165 /* Follow reference OFFSET.
16166 On entry *REF_CU is the CU of the source die referencing OFFSET.
16167 On exit *REF_CU is the CU of the result.
16168 Returns NULL if OFFSET is invalid. */
16169
16170 static struct die_info *
16171 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
16172 {
16173 struct die_info temp_die;
16174 struct dwarf2_cu *target_cu, *cu = *ref_cu;
16175
16176 gdb_assert (cu->per_cu != NULL);
16177
16178 target_cu = cu;
16179
16180 if (cu->per_cu->is_debug_types)
16181 {
16182 /* .debug_types CUs cannot reference anything outside their CU.
16183 If they need to, they have to reference a signatured type via
16184 DW_FORM_ref_sig8. */
16185 if (! offset_in_cu_p (&cu->header, offset))
16186 return NULL;
16187 }
16188 else if (! offset_in_cu_p (&cu->header, offset))
16189 {
16190 struct dwarf2_per_cu_data *per_cu;
16191
16192 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
16193
16194 /* If necessary, add it to the queue and load its DIEs. */
16195 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16196 load_full_comp_unit (per_cu, cu->language);
16197
16198 target_cu = per_cu->cu;
16199 }
16200 else if (cu->dies == NULL)
16201 {
16202 /* We're loading full DIEs during partial symbol reading. */
16203 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16204 load_full_comp_unit (cu->per_cu, language_minimal);
16205 }
16206
16207 *ref_cu = target_cu;
16208 temp_die.offset = offset;
16209 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16210 }
16211
16212 /* Follow reference attribute ATTR of SRC_DIE.
16213 On entry *REF_CU is the CU of SRC_DIE.
16214 On exit *REF_CU is the CU of the result. */
16215
16216 static struct die_info *
16217 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16218 struct dwarf2_cu **ref_cu)
16219 {
16220 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16221 struct dwarf2_cu *cu = *ref_cu;
16222 struct die_info *die;
16223
16224 die = follow_die_offset (offset, ref_cu);
16225 if (!die)
16226 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16227 "at 0x%x [in module %s]"),
16228 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16229
16230 return die;
16231 }
16232
16233 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16234 Returned value is intended for DW_OP_call*. Returned
16235 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
16236
16237 struct dwarf2_locexpr_baton
16238 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16239 struct dwarf2_per_cu_data *per_cu,
16240 CORE_ADDR (*get_frame_pc) (void *baton),
16241 void *baton)
16242 {
16243 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16244 struct dwarf2_cu *cu;
16245 struct die_info *die;
16246 struct attribute *attr;
16247 struct dwarf2_locexpr_baton retval;
16248
16249 dw2_setup (per_cu->objfile);
16250
16251 if (per_cu->cu == NULL)
16252 load_cu (per_cu);
16253 cu = per_cu->cu;
16254
16255 die = follow_die_offset (offset, &cu);
16256 if (!die)
16257 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16258 offset.sect_off, per_cu->objfile->name);
16259
16260 attr = dwarf2_attr (die, DW_AT_location, cu);
16261 if (!attr)
16262 {
16263 /* DWARF: "If there is no such attribute, then there is no effect.".
16264 DATA is ignored if SIZE is 0. */
16265
16266 retval.data = NULL;
16267 retval.size = 0;
16268 }
16269 else if (attr_form_is_section_offset (attr))
16270 {
16271 struct dwarf2_loclist_baton loclist_baton;
16272 CORE_ADDR pc = (*get_frame_pc) (baton);
16273 size_t size;
16274
16275 fill_in_loclist_baton (cu, &loclist_baton, attr);
16276
16277 retval.data = dwarf2_find_location_expression (&loclist_baton,
16278 &size, pc);
16279 retval.size = size;
16280 }
16281 else
16282 {
16283 if (!attr_form_is_block (attr))
16284 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16285 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16286 offset.sect_off, per_cu->objfile->name);
16287
16288 retval.data = DW_BLOCK (attr)->data;
16289 retval.size = DW_BLOCK (attr)->size;
16290 }
16291 retval.per_cu = cu->per_cu;
16292
16293 age_cached_comp_units ();
16294
16295 return retval;
16296 }
16297
16298 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16299 PER_CU. */
16300
16301 struct type *
16302 dwarf2_get_die_type (cu_offset die_offset,
16303 struct dwarf2_per_cu_data *per_cu)
16304 {
16305 sect_offset die_offset_sect;
16306
16307 dw2_setup (per_cu->objfile);
16308
16309 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16310 return get_die_type_at_offset (die_offset_sect, per_cu);
16311 }
16312
16313 /* Follow the signature attribute ATTR in SRC_DIE.
16314 On entry *REF_CU is the CU of SRC_DIE.
16315 On exit *REF_CU is the CU of the result. */
16316
16317 static struct die_info *
16318 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16319 struct dwarf2_cu **ref_cu)
16320 {
16321 struct objfile *objfile = (*ref_cu)->objfile;
16322 struct die_info temp_die;
16323 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16324 struct dwarf2_cu *sig_cu;
16325 struct die_info *die;
16326
16327 /* sig_type will be NULL if the signatured type is missing from
16328 the debug info. */
16329 if (sig_type == NULL)
16330 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16331 "at 0x%x [in module %s]"),
16332 src_die->offset.sect_off, objfile->name);
16333
16334 /* If necessary, add it to the queue and load its DIEs. */
16335
16336 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16337 read_signatured_type (sig_type);
16338
16339 gdb_assert (sig_type->per_cu.cu != NULL);
16340
16341 sig_cu = sig_type->per_cu.cu;
16342 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16343 temp_die.offset = sig_type->type_offset_in_section;
16344 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16345 temp_die.offset.sect_off);
16346 if (die)
16347 {
16348 *ref_cu = sig_cu;
16349 return die;
16350 }
16351
16352 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16353 "from DIE at 0x%x [in module %s]"),
16354 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16355 }
16356
16357 /* Given an offset of a signatured type, return its signatured_type. */
16358
16359 static struct signatured_type *
16360 lookup_signatured_type_at_offset (struct objfile *objfile,
16361 struct dwarf2_section_info *section,
16362 sect_offset offset)
16363 {
16364 gdb_byte *info_ptr = section->buffer + offset.sect_off;
16365 unsigned int length, initial_length_size;
16366 unsigned int sig_offset;
16367 struct signatured_type find_entry, *sig_type;
16368
16369 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16370 sig_offset = (initial_length_size
16371 + 2 /*version*/
16372 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16373 + 1 /*address_size*/);
16374 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16375 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16376
16377 /* This is only used to lookup previously recorded types.
16378 If we didn't find it, it's our bug. */
16379 gdb_assert (sig_type != NULL);
16380 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16381
16382 return sig_type;
16383 }
16384
16385 /* Load the DIEs associated with type unit PER_CU into memory. */
16386
16387 static void
16388 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16389 {
16390 struct signatured_type *sig_type;
16391
16392 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16393 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16394
16395 /* We have the per_cu, but we need the signatured_type.
16396 Fortunately this is an easy translation. */
16397 gdb_assert (per_cu->is_debug_types);
16398 sig_type = (struct signatured_type *) per_cu;
16399
16400 gdb_assert (per_cu->cu == NULL);
16401
16402 read_signatured_type (sig_type);
16403
16404 gdb_assert (per_cu->cu != NULL);
16405 }
16406
16407 /* die_reader_func for read_signatured_type.
16408 This is identical to load_full_comp_unit_reader,
16409 but is kept separate for now. */
16410
16411 static void
16412 read_signatured_type_reader (const struct die_reader_specs *reader,
16413 gdb_byte *info_ptr,
16414 struct die_info *comp_unit_die,
16415 int has_children,
16416 void *data)
16417 {
16418 struct dwarf2_cu *cu = reader->cu;
16419
16420 gdb_assert (cu->die_hash == NULL);
16421 cu->die_hash =
16422 htab_create_alloc_ex (cu->header.length / 12,
16423 die_hash,
16424 die_eq,
16425 NULL,
16426 &cu->comp_unit_obstack,
16427 hashtab_obstack_allocate,
16428 dummy_obstack_deallocate);
16429
16430 if (has_children)
16431 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16432 &info_ptr, comp_unit_die);
16433 cu->dies = comp_unit_die;
16434 /* comp_unit_die is not stored in die_hash, no need. */
16435
16436 /* We try not to read any attributes in this function, because not
16437 all CUs needed for references have been loaded yet, and symbol
16438 table processing isn't initialized. But we have to set the CU language,
16439 or we won't be able to build types correctly.
16440 Similarly, if we do not read the producer, we can not apply
16441 producer-specific interpretation. */
16442 prepare_one_comp_unit (cu, cu->dies, language_minimal);
16443 }
16444
16445 /* Read in a signatured type and build its CU and DIEs.
16446 If the type is a stub for the real type in a DWO file,
16447 read in the real type from the DWO file as well. */
16448
16449 static void
16450 read_signatured_type (struct signatured_type *sig_type)
16451 {
16452 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16453
16454 gdb_assert (per_cu->is_debug_types);
16455 gdb_assert (per_cu->cu == NULL);
16456
16457 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16458 read_signatured_type_reader, NULL);
16459 }
16460
16461 /* Decode simple location descriptions.
16462 Given a pointer to a dwarf block that defines a location, compute
16463 the location and return the value.
16464
16465 NOTE drow/2003-11-18: This function is called in two situations
16466 now: for the address of static or global variables (partial symbols
16467 only) and for offsets into structures which are expected to be
16468 (more or less) constant. The partial symbol case should go away,
16469 and only the constant case should remain. That will let this
16470 function complain more accurately. A few special modes are allowed
16471 without complaint for global variables (for instance, global
16472 register values and thread-local values).
16473
16474 A location description containing no operations indicates that the
16475 object is optimized out. The return value is 0 for that case.
16476 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16477 callers will only want a very basic result and this can become a
16478 complaint.
16479
16480 Note that stack[0] is unused except as a default error return. */
16481
16482 static CORE_ADDR
16483 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16484 {
16485 struct objfile *objfile = cu->objfile;
16486 int i;
16487 int size = blk->size;
16488 gdb_byte *data = blk->data;
16489 CORE_ADDR stack[64];
16490 int stacki;
16491 unsigned int bytes_read, unsnd;
16492 gdb_byte op;
16493
16494 i = 0;
16495 stacki = 0;
16496 stack[stacki] = 0;
16497 stack[++stacki] = 0;
16498
16499 while (i < size)
16500 {
16501 op = data[i++];
16502 switch (op)
16503 {
16504 case DW_OP_lit0:
16505 case DW_OP_lit1:
16506 case DW_OP_lit2:
16507 case DW_OP_lit3:
16508 case DW_OP_lit4:
16509 case DW_OP_lit5:
16510 case DW_OP_lit6:
16511 case DW_OP_lit7:
16512 case DW_OP_lit8:
16513 case DW_OP_lit9:
16514 case DW_OP_lit10:
16515 case DW_OP_lit11:
16516 case DW_OP_lit12:
16517 case DW_OP_lit13:
16518 case DW_OP_lit14:
16519 case DW_OP_lit15:
16520 case DW_OP_lit16:
16521 case DW_OP_lit17:
16522 case DW_OP_lit18:
16523 case DW_OP_lit19:
16524 case DW_OP_lit20:
16525 case DW_OP_lit21:
16526 case DW_OP_lit22:
16527 case DW_OP_lit23:
16528 case DW_OP_lit24:
16529 case DW_OP_lit25:
16530 case DW_OP_lit26:
16531 case DW_OP_lit27:
16532 case DW_OP_lit28:
16533 case DW_OP_lit29:
16534 case DW_OP_lit30:
16535 case DW_OP_lit31:
16536 stack[++stacki] = op - DW_OP_lit0;
16537 break;
16538
16539 case DW_OP_reg0:
16540 case DW_OP_reg1:
16541 case DW_OP_reg2:
16542 case DW_OP_reg3:
16543 case DW_OP_reg4:
16544 case DW_OP_reg5:
16545 case DW_OP_reg6:
16546 case DW_OP_reg7:
16547 case DW_OP_reg8:
16548 case DW_OP_reg9:
16549 case DW_OP_reg10:
16550 case DW_OP_reg11:
16551 case DW_OP_reg12:
16552 case DW_OP_reg13:
16553 case DW_OP_reg14:
16554 case DW_OP_reg15:
16555 case DW_OP_reg16:
16556 case DW_OP_reg17:
16557 case DW_OP_reg18:
16558 case DW_OP_reg19:
16559 case DW_OP_reg20:
16560 case DW_OP_reg21:
16561 case DW_OP_reg22:
16562 case DW_OP_reg23:
16563 case DW_OP_reg24:
16564 case DW_OP_reg25:
16565 case DW_OP_reg26:
16566 case DW_OP_reg27:
16567 case DW_OP_reg28:
16568 case DW_OP_reg29:
16569 case DW_OP_reg30:
16570 case DW_OP_reg31:
16571 stack[++stacki] = op - DW_OP_reg0;
16572 if (i < size)
16573 dwarf2_complex_location_expr_complaint ();
16574 break;
16575
16576 case DW_OP_regx:
16577 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16578 i += bytes_read;
16579 stack[++stacki] = unsnd;
16580 if (i < size)
16581 dwarf2_complex_location_expr_complaint ();
16582 break;
16583
16584 case DW_OP_addr:
16585 stack[++stacki] = read_address (objfile->obfd, &data[i],
16586 cu, &bytes_read);
16587 i += bytes_read;
16588 break;
16589
16590 case DW_OP_const1u:
16591 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16592 i += 1;
16593 break;
16594
16595 case DW_OP_const1s:
16596 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16597 i += 1;
16598 break;
16599
16600 case DW_OP_const2u:
16601 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16602 i += 2;
16603 break;
16604
16605 case DW_OP_const2s:
16606 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16607 i += 2;
16608 break;
16609
16610 case DW_OP_const4u:
16611 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16612 i += 4;
16613 break;
16614
16615 case DW_OP_const4s:
16616 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16617 i += 4;
16618 break;
16619
16620 case DW_OP_const8u:
16621 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16622 i += 8;
16623 break;
16624
16625 case DW_OP_constu:
16626 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16627 &bytes_read);
16628 i += bytes_read;
16629 break;
16630
16631 case DW_OP_consts:
16632 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16633 i += bytes_read;
16634 break;
16635
16636 case DW_OP_dup:
16637 stack[stacki + 1] = stack[stacki];
16638 stacki++;
16639 break;
16640
16641 case DW_OP_plus:
16642 stack[stacki - 1] += stack[stacki];
16643 stacki--;
16644 break;
16645
16646 case DW_OP_plus_uconst:
16647 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16648 &bytes_read);
16649 i += bytes_read;
16650 break;
16651
16652 case DW_OP_minus:
16653 stack[stacki - 1] -= stack[stacki];
16654 stacki--;
16655 break;
16656
16657 case DW_OP_deref:
16658 /* If we're not the last op, then we definitely can't encode
16659 this using GDB's address_class enum. This is valid for partial
16660 global symbols, although the variable's address will be bogus
16661 in the psymtab. */
16662 if (i < size)
16663 dwarf2_complex_location_expr_complaint ();
16664 break;
16665
16666 case DW_OP_GNU_push_tls_address:
16667 /* The top of the stack has the offset from the beginning
16668 of the thread control block at which the variable is located. */
16669 /* Nothing should follow this operator, so the top of stack would
16670 be returned. */
16671 /* This is valid for partial global symbols, but the variable's
16672 address will be bogus in the psymtab. Make it always at least
16673 non-zero to not look as a variable garbage collected by linker
16674 which have DW_OP_addr 0. */
16675 if (i < size)
16676 dwarf2_complex_location_expr_complaint ();
16677 stack[stacki]++;
16678 break;
16679
16680 case DW_OP_GNU_uninit:
16681 break;
16682
16683 case DW_OP_GNU_addr_index:
16684 case DW_OP_GNU_const_index:
16685 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16686 &bytes_read);
16687 i += bytes_read;
16688 break;
16689
16690 default:
16691 {
16692 const char *name = get_DW_OP_name (op);
16693
16694 if (name)
16695 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16696 name);
16697 else
16698 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16699 op);
16700 }
16701
16702 return (stack[stacki]);
16703 }
16704
16705 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16706 outside of the allocated space. Also enforce minimum>0. */
16707 if (stacki >= ARRAY_SIZE (stack) - 1)
16708 {
16709 complaint (&symfile_complaints,
16710 _("location description stack overflow"));
16711 return 0;
16712 }
16713
16714 if (stacki <= 0)
16715 {
16716 complaint (&symfile_complaints,
16717 _("location description stack underflow"));
16718 return 0;
16719 }
16720 }
16721 return (stack[stacki]);
16722 }
16723
16724 /* memory allocation interface */
16725
16726 static struct dwarf_block *
16727 dwarf_alloc_block (struct dwarf2_cu *cu)
16728 {
16729 struct dwarf_block *blk;
16730
16731 blk = (struct dwarf_block *)
16732 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16733 return (blk);
16734 }
16735
16736 static struct die_info *
16737 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
16738 {
16739 struct die_info *die;
16740 size_t size = sizeof (struct die_info);
16741
16742 if (num_attrs > 1)
16743 size += (num_attrs - 1) * sizeof (struct attribute);
16744
16745 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
16746 memset (die, 0, sizeof (struct die_info));
16747 return (die);
16748 }
16749
16750 \f
16751 /* Macro support. */
16752
16753 /* Return the full name of file number I in *LH's file name table.
16754 Use COMP_DIR as the name of the current directory of the
16755 compilation. The result is allocated using xmalloc; the caller is
16756 responsible for freeing it. */
16757 static char *
16758 file_full_name (int file, struct line_header *lh, const char *comp_dir)
16759 {
16760 /* Is the file number a valid index into the line header's file name
16761 table? Remember that file numbers start with one, not zero. */
16762 if (1 <= file && file <= lh->num_file_names)
16763 {
16764 struct file_entry *fe = &lh->file_names[file - 1];
16765
16766 if (IS_ABSOLUTE_PATH (fe->name))
16767 return xstrdup (fe->name);
16768 else
16769 {
16770 const char *dir;
16771 int dir_len;
16772 char *full_name;
16773
16774 if (fe->dir_index)
16775 dir = lh->include_dirs[fe->dir_index - 1];
16776 else
16777 dir = comp_dir;
16778
16779 if (dir)
16780 {
16781 dir_len = strlen (dir);
16782 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
16783 strcpy (full_name, dir);
16784 full_name[dir_len] = '/';
16785 strcpy (full_name + dir_len + 1, fe->name);
16786 return full_name;
16787 }
16788 else
16789 return xstrdup (fe->name);
16790 }
16791 }
16792 else
16793 {
16794 /* The compiler produced a bogus file number. We can at least
16795 record the macro definitions made in the file, even if we
16796 won't be able to find the file by name. */
16797 char fake_name[80];
16798
16799 sprintf (fake_name, "<bad macro file number %d>", file);
16800
16801 complaint (&symfile_complaints,
16802 _("bad file number in macro information (%d)"),
16803 file);
16804
16805 return xstrdup (fake_name);
16806 }
16807 }
16808
16809
16810 static struct macro_source_file *
16811 macro_start_file (int file, int line,
16812 struct macro_source_file *current_file,
16813 const char *comp_dir,
16814 struct line_header *lh, struct objfile *objfile)
16815 {
16816 /* The full name of this source file. */
16817 char *full_name = file_full_name (file, lh, comp_dir);
16818
16819 /* We don't create a macro table for this compilation unit
16820 at all until we actually get a filename. */
16821 if (! pending_macros)
16822 pending_macros = new_macro_table (&objfile->objfile_obstack,
16823 objfile->macro_cache);
16824
16825 if (! current_file)
16826 {
16827 /* If we have no current file, then this must be the start_file
16828 directive for the compilation unit's main source file. */
16829 current_file = macro_set_main (pending_macros, full_name);
16830 macro_define_special (pending_macros);
16831 }
16832 else
16833 current_file = macro_include (current_file, line, full_name);
16834
16835 xfree (full_name);
16836
16837 return current_file;
16838 }
16839
16840
16841 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
16842 followed by a null byte. */
16843 static char *
16844 copy_string (const char *buf, int len)
16845 {
16846 char *s = xmalloc (len + 1);
16847
16848 memcpy (s, buf, len);
16849 s[len] = '\0';
16850 return s;
16851 }
16852
16853
16854 static const char *
16855 consume_improper_spaces (const char *p, const char *body)
16856 {
16857 if (*p == ' ')
16858 {
16859 complaint (&symfile_complaints,
16860 _("macro definition contains spaces "
16861 "in formal argument list:\n`%s'"),
16862 body);
16863
16864 while (*p == ' ')
16865 p++;
16866 }
16867
16868 return p;
16869 }
16870
16871
16872 static void
16873 parse_macro_definition (struct macro_source_file *file, int line,
16874 const char *body)
16875 {
16876 const char *p;
16877
16878 /* The body string takes one of two forms. For object-like macro
16879 definitions, it should be:
16880
16881 <macro name> " " <definition>
16882
16883 For function-like macro definitions, it should be:
16884
16885 <macro name> "() " <definition>
16886 or
16887 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16888
16889 Spaces may appear only where explicitly indicated, and in the
16890 <definition>.
16891
16892 The Dwarf 2 spec says that an object-like macro's name is always
16893 followed by a space, but versions of GCC around March 2002 omit
16894 the space when the macro's definition is the empty string.
16895
16896 The Dwarf 2 spec says that there should be no spaces between the
16897 formal arguments in a function-like macro's formal argument list,
16898 but versions of GCC around March 2002 include spaces after the
16899 commas. */
16900
16901
16902 /* Find the extent of the macro name. The macro name is terminated
16903 by either a space or null character (for an object-like macro) or
16904 an opening paren (for a function-like macro). */
16905 for (p = body; *p; p++)
16906 if (*p == ' ' || *p == '(')
16907 break;
16908
16909 if (*p == ' ' || *p == '\0')
16910 {
16911 /* It's an object-like macro. */
16912 int name_len = p - body;
16913 char *name = copy_string (body, name_len);
16914 const char *replacement;
16915
16916 if (*p == ' ')
16917 replacement = body + name_len + 1;
16918 else
16919 {
16920 dwarf2_macro_malformed_definition_complaint (body);
16921 replacement = body + name_len;
16922 }
16923
16924 macro_define_object (file, line, name, replacement);
16925
16926 xfree (name);
16927 }
16928 else if (*p == '(')
16929 {
16930 /* It's a function-like macro. */
16931 char *name = copy_string (body, p - body);
16932 int argc = 0;
16933 int argv_size = 1;
16934 char **argv = xmalloc (argv_size * sizeof (*argv));
16935
16936 p++;
16937
16938 p = consume_improper_spaces (p, body);
16939
16940 /* Parse the formal argument list. */
16941 while (*p && *p != ')')
16942 {
16943 /* Find the extent of the current argument name. */
16944 const char *arg_start = p;
16945
16946 while (*p && *p != ',' && *p != ')' && *p != ' ')
16947 p++;
16948
16949 if (! *p || p == arg_start)
16950 dwarf2_macro_malformed_definition_complaint (body);
16951 else
16952 {
16953 /* Make sure argv has room for the new argument. */
16954 if (argc >= argv_size)
16955 {
16956 argv_size *= 2;
16957 argv = xrealloc (argv, argv_size * sizeof (*argv));
16958 }
16959
16960 argv[argc++] = copy_string (arg_start, p - arg_start);
16961 }
16962
16963 p = consume_improper_spaces (p, body);
16964
16965 /* Consume the comma, if present. */
16966 if (*p == ',')
16967 {
16968 p++;
16969
16970 p = consume_improper_spaces (p, body);
16971 }
16972 }
16973
16974 if (*p == ')')
16975 {
16976 p++;
16977
16978 if (*p == ' ')
16979 /* Perfectly formed definition, no complaints. */
16980 macro_define_function (file, line, name,
16981 argc, (const char **) argv,
16982 p + 1);
16983 else if (*p == '\0')
16984 {
16985 /* Complain, but do define it. */
16986 dwarf2_macro_malformed_definition_complaint (body);
16987 macro_define_function (file, line, name,
16988 argc, (const char **) argv,
16989 p);
16990 }
16991 else
16992 /* Just complain. */
16993 dwarf2_macro_malformed_definition_complaint (body);
16994 }
16995 else
16996 /* Just complain. */
16997 dwarf2_macro_malformed_definition_complaint (body);
16998
16999 xfree (name);
17000 {
17001 int i;
17002
17003 for (i = 0; i < argc; i++)
17004 xfree (argv[i]);
17005 }
17006 xfree (argv);
17007 }
17008 else
17009 dwarf2_macro_malformed_definition_complaint (body);
17010 }
17011
17012 /* Skip some bytes from BYTES according to the form given in FORM.
17013 Returns the new pointer. */
17014
17015 static gdb_byte *
17016 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17017 enum dwarf_form form,
17018 unsigned int offset_size,
17019 struct dwarf2_section_info *section)
17020 {
17021 unsigned int bytes_read;
17022
17023 switch (form)
17024 {
17025 case DW_FORM_data1:
17026 case DW_FORM_flag:
17027 ++bytes;
17028 break;
17029
17030 case DW_FORM_data2:
17031 bytes += 2;
17032 break;
17033
17034 case DW_FORM_data4:
17035 bytes += 4;
17036 break;
17037
17038 case DW_FORM_data8:
17039 bytes += 8;
17040 break;
17041
17042 case DW_FORM_string:
17043 read_direct_string (abfd, bytes, &bytes_read);
17044 bytes += bytes_read;
17045 break;
17046
17047 case DW_FORM_sec_offset:
17048 case DW_FORM_strp:
17049 bytes += offset_size;
17050 break;
17051
17052 case DW_FORM_block:
17053 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17054 bytes += bytes_read;
17055 break;
17056
17057 case DW_FORM_block1:
17058 bytes += 1 + read_1_byte (abfd, bytes);
17059 break;
17060 case DW_FORM_block2:
17061 bytes += 2 + read_2_bytes (abfd, bytes);
17062 break;
17063 case DW_FORM_block4:
17064 bytes += 4 + read_4_bytes (abfd, bytes);
17065 break;
17066
17067 case DW_FORM_sdata:
17068 case DW_FORM_udata:
17069 case DW_FORM_GNU_addr_index:
17070 case DW_FORM_GNU_str_index:
17071 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17072 if (bytes == NULL)
17073 {
17074 dwarf2_section_buffer_overflow_complaint (section);
17075 return NULL;
17076 }
17077 break;
17078
17079 default:
17080 {
17081 complain:
17082 complaint (&symfile_complaints,
17083 _("invalid form 0x%x in `%s'"),
17084 form,
17085 section->asection->name);
17086 return NULL;
17087 }
17088 }
17089
17090 return bytes;
17091 }
17092
17093 /* A helper for dwarf_decode_macros that handles skipping an unknown
17094 opcode. Returns an updated pointer to the macro data buffer; or,
17095 on error, issues a complaint and returns NULL. */
17096
17097 static gdb_byte *
17098 skip_unknown_opcode (unsigned int opcode,
17099 gdb_byte **opcode_definitions,
17100 gdb_byte *mac_ptr, gdb_byte *mac_end,
17101 bfd *abfd,
17102 unsigned int offset_size,
17103 struct dwarf2_section_info *section)
17104 {
17105 unsigned int bytes_read, i;
17106 unsigned long arg;
17107 gdb_byte *defn;
17108
17109 if (opcode_definitions[opcode] == NULL)
17110 {
17111 complaint (&symfile_complaints,
17112 _("unrecognized DW_MACFINO opcode 0x%x"),
17113 opcode);
17114 return NULL;
17115 }
17116
17117 defn = opcode_definitions[opcode];
17118 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17119 defn += bytes_read;
17120
17121 for (i = 0; i < arg; ++i)
17122 {
17123 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17124 section);
17125 if (mac_ptr == NULL)
17126 {
17127 /* skip_form_bytes already issued the complaint. */
17128 return NULL;
17129 }
17130 }
17131
17132 return mac_ptr;
17133 }
17134
17135 /* A helper function which parses the header of a macro section.
17136 If the macro section is the extended (for now called "GNU") type,
17137 then this updates *OFFSET_SIZE. Returns a pointer to just after
17138 the header, or issues a complaint and returns NULL on error. */
17139
17140 static gdb_byte *
17141 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17142 bfd *abfd,
17143 gdb_byte *mac_ptr,
17144 unsigned int *offset_size,
17145 int section_is_gnu)
17146 {
17147 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17148
17149 if (section_is_gnu)
17150 {
17151 unsigned int version, flags;
17152
17153 version = read_2_bytes (abfd, mac_ptr);
17154 if (version != 4)
17155 {
17156 complaint (&symfile_complaints,
17157 _("unrecognized version `%d' in .debug_macro section"),
17158 version);
17159 return NULL;
17160 }
17161 mac_ptr += 2;
17162
17163 flags = read_1_byte (abfd, mac_ptr);
17164 ++mac_ptr;
17165 *offset_size = (flags & 1) ? 8 : 4;
17166
17167 if ((flags & 2) != 0)
17168 /* We don't need the line table offset. */
17169 mac_ptr += *offset_size;
17170
17171 /* Vendor opcode descriptions. */
17172 if ((flags & 4) != 0)
17173 {
17174 unsigned int i, count;
17175
17176 count = read_1_byte (abfd, mac_ptr);
17177 ++mac_ptr;
17178 for (i = 0; i < count; ++i)
17179 {
17180 unsigned int opcode, bytes_read;
17181 unsigned long arg;
17182
17183 opcode = read_1_byte (abfd, mac_ptr);
17184 ++mac_ptr;
17185 opcode_definitions[opcode] = mac_ptr;
17186 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17187 mac_ptr += bytes_read;
17188 mac_ptr += arg;
17189 }
17190 }
17191 }
17192
17193 return mac_ptr;
17194 }
17195
17196 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17197 including DW_MACRO_GNU_transparent_include. */
17198
17199 static void
17200 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17201 struct macro_source_file *current_file,
17202 struct line_header *lh, char *comp_dir,
17203 struct dwarf2_section_info *section,
17204 int section_is_gnu,
17205 unsigned int offset_size,
17206 struct objfile *objfile,
17207 htab_t include_hash)
17208 {
17209 enum dwarf_macro_record_type macinfo_type;
17210 int at_commandline;
17211 gdb_byte *opcode_definitions[256];
17212
17213 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17214 &offset_size, section_is_gnu);
17215 if (mac_ptr == NULL)
17216 {
17217 /* We already issued a complaint. */
17218 return;
17219 }
17220
17221 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17222 GDB is still reading the definitions from command line. First
17223 DW_MACINFO_start_file will need to be ignored as it was already executed
17224 to create CURRENT_FILE for the main source holding also the command line
17225 definitions. On first met DW_MACINFO_start_file this flag is reset to
17226 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17227
17228 at_commandline = 1;
17229
17230 do
17231 {
17232 /* Do we at least have room for a macinfo type byte? */
17233 if (mac_ptr >= mac_end)
17234 {
17235 dwarf2_section_buffer_overflow_complaint (section);
17236 break;
17237 }
17238
17239 macinfo_type = read_1_byte (abfd, mac_ptr);
17240 mac_ptr++;
17241
17242 /* Note that we rely on the fact that the corresponding GNU and
17243 DWARF constants are the same. */
17244 switch (macinfo_type)
17245 {
17246 /* A zero macinfo type indicates the end of the macro
17247 information. */
17248 case 0:
17249 break;
17250
17251 case DW_MACRO_GNU_define:
17252 case DW_MACRO_GNU_undef:
17253 case DW_MACRO_GNU_define_indirect:
17254 case DW_MACRO_GNU_undef_indirect:
17255 {
17256 unsigned int bytes_read;
17257 int line;
17258 char *body;
17259 int is_define;
17260
17261 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17262 mac_ptr += bytes_read;
17263
17264 if (macinfo_type == DW_MACRO_GNU_define
17265 || macinfo_type == DW_MACRO_GNU_undef)
17266 {
17267 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17268 mac_ptr += bytes_read;
17269 }
17270 else
17271 {
17272 LONGEST str_offset;
17273
17274 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17275 mac_ptr += offset_size;
17276
17277 body = read_indirect_string_at_offset (abfd, str_offset);
17278 }
17279
17280 is_define = (macinfo_type == DW_MACRO_GNU_define
17281 || macinfo_type == DW_MACRO_GNU_define_indirect);
17282 if (! current_file)
17283 {
17284 /* DWARF violation as no main source is present. */
17285 complaint (&symfile_complaints,
17286 _("debug info with no main source gives macro %s "
17287 "on line %d: %s"),
17288 is_define ? _("definition") : _("undefinition"),
17289 line, body);
17290 break;
17291 }
17292 if ((line == 0 && !at_commandline)
17293 || (line != 0 && at_commandline))
17294 complaint (&symfile_complaints,
17295 _("debug info gives %s macro %s with %s line %d: %s"),
17296 at_commandline ? _("command-line") : _("in-file"),
17297 is_define ? _("definition") : _("undefinition"),
17298 line == 0 ? _("zero") : _("non-zero"), line, body);
17299
17300 if (is_define)
17301 parse_macro_definition (current_file, line, body);
17302 else
17303 {
17304 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17305 || macinfo_type == DW_MACRO_GNU_undef_indirect);
17306 macro_undef (current_file, line, body);
17307 }
17308 }
17309 break;
17310
17311 case DW_MACRO_GNU_start_file:
17312 {
17313 unsigned int bytes_read;
17314 int line, file;
17315
17316 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17317 mac_ptr += bytes_read;
17318 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17319 mac_ptr += bytes_read;
17320
17321 if ((line == 0 && !at_commandline)
17322 || (line != 0 && at_commandline))
17323 complaint (&symfile_complaints,
17324 _("debug info gives source %d included "
17325 "from %s at %s line %d"),
17326 file, at_commandline ? _("command-line") : _("file"),
17327 line == 0 ? _("zero") : _("non-zero"), line);
17328
17329 if (at_commandline)
17330 {
17331 /* This DW_MACRO_GNU_start_file was executed in the
17332 pass one. */
17333 at_commandline = 0;
17334 }
17335 else
17336 current_file = macro_start_file (file, line,
17337 current_file, comp_dir,
17338 lh, objfile);
17339 }
17340 break;
17341
17342 case DW_MACRO_GNU_end_file:
17343 if (! current_file)
17344 complaint (&symfile_complaints,
17345 _("macro debug info has an unmatched "
17346 "`close_file' directive"));
17347 else
17348 {
17349 current_file = current_file->included_by;
17350 if (! current_file)
17351 {
17352 enum dwarf_macro_record_type next_type;
17353
17354 /* GCC circa March 2002 doesn't produce the zero
17355 type byte marking the end of the compilation
17356 unit. Complain if it's not there, but exit no
17357 matter what. */
17358
17359 /* Do we at least have room for a macinfo type byte? */
17360 if (mac_ptr >= mac_end)
17361 {
17362 dwarf2_section_buffer_overflow_complaint (section);
17363 return;
17364 }
17365
17366 /* We don't increment mac_ptr here, so this is just
17367 a look-ahead. */
17368 next_type = read_1_byte (abfd, mac_ptr);
17369 if (next_type != 0)
17370 complaint (&symfile_complaints,
17371 _("no terminating 0-type entry for "
17372 "macros in `.debug_macinfo' section"));
17373
17374 return;
17375 }
17376 }
17377 break;
17378
17379 case DW_MACRO_GNU_transparent_include:
17380 {
17381 LONGEST offset;
17382 void **slot;
17383
17384 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17385 mac_ptr += offset_size;
17386
17387 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17388 if (*slot != NULL)
17389 {
17390 /* This has actually happened; see
17391 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17392 complaint (&symfile_complaints,
17393 _("recursive DW_MACRO_GNU_transparent_include in "
17394 ".debug_macro section"));
17395 }
17396 else
17397 {
17398 *slot = mac_ptr;
17399
17400 dwarf_decode_macro_bytes (abfd,
17401 section->buffer + offset,
17402 mac_end, current_file,
17403 lh, comp_dir,
17404 section, section_is_gnu,
17405 offset_size, objfile, include_hash);
17406
17407 htab_remove_elt (include_hash, mac_ptr);
17408 }
17409 }
17410 break;
17411
17412 case DW_MACINFO_vendor_ext:
17413 if (!section_is_gnu)
17414 {
17415 unsigned int bytes_read;
17416 int constant;
17417
17418 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17419 mac_ptr += bytes_read;
17420 read_direct_string (abfd, mac_ptr, &bytes_read);
17421 mac_ptr += bytes_read;
17422
17423 /* We don't recognize any vendor extensions. */
17424 break;
17425 }
17426 /* FALLTHROUGH */
17427
17428 default:
17429 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17430 mac_ptr, mac_end, abfd, offset_size,
17431 section);
17432 if (mac_ptr == NULL)
17433 return;
17434 break;
17435 }
17436 } while (macinfo_type != 0);
17437 }
17438
17439 static void
17440 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17441 char *comp_dir, int section_is_gnu)
17442 {
17443 struct objfile *objfile = dwarf2_per_objfile->objfile;
17444 struct line_header *lh = cu->line_header;
17445 bfd *abfd;
17446 gdb_byte *mac_ptr, *mac_end;
17447 struct macro_source_file *current_file = 0;
17448 enum dwarf_macro_record_type macinfo_type;
17449 unsigned int offset_size = cu->header.offset_size;
17450 gdb_byte *opcode_definitions[256];
17451 struct cleanup *cleanup;
17452 htab_t include_hash;
17453 void **slot;
17454 struct dwarf2_section_info *section;
17455 const char *section_name;
17456
17457 if (cu->dwo_unit != NULL)
17458 {
17459 if (section_is_gnu)
17460 {
17461 section = &cu->dwo_unit->dwo_file->sections.macro;
17462 section_name = ".debug_macro.dwo";
17463 }
17464 else
17465 {
17466 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17467 section_name = ".debug_macinfo.dwo";
17468 }
17469 }
17470 else
17471 {
17472 if (section_is_gnu)
17473 {
17474 section = &dwarf2_per_objfile->macro;
17475 section_name = ".debug_macro";
17476 }
17477 else
17478 {
17479 section = &dwarf2_per_objfile->macinfo;
17480 section_name = ".debug_macinfo";
17481 }
17482 }
17483
17484 dwarf2_read_section (objfile, section);
17485 if (section->buffer == NULL)
17486 {
17487 complaint (&symfile_complaints, _("missing %s section"), section_name);
17488 return;
17489 }
17490 abfd = section->asection->owner;
17491
17492 /* First pass: Find the name of the base filename.
17493 This filename is needed in order to process all macros whose definition
17494 (or undefinition) comes from the command line. These macros are defined
17495 before the first DW_MACINFO_start_file entry, and yet still need to be
17496 associated to the base file.
17497
17498 To determine the base file name, we scan the macro definitions until we
17499 reach the first DW_MACINFO_start_file entry. We then initialize
17500 CURRENT_FILE accordingly so that any macro definition found before the
17501 first DW_MACINFO_start_file can still be associated to the base file. */
17502
17503 mac_ptr = section->buffer + offset;
17504 mac_end = section->buffer + section->size;
17505
17506 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17507 &offset_size, section_is_gnu);
17508 if (mac_ptr == NULL)
17509 {
17510 /* We already issued a complaint. */
17511 return;
17512 }
17513
17514 do
17515 {
17516 /* Do we at least have room for a macinfo type byte? */
17517 if (mac_ptr >= mac_end)
17518 {
17519 /* Complaint is printed during the second pass as GDB will probably
17520 stop the first pass earlier upon finding
17521 DW_MACINFO_start_file. */
17522 break;
17523 }
17524
17525 macinfo_type = read_1_byte (abfd, mac_ptr);
17526 mac_ptr++;
17527
17528 /* Note that we rely on the fact that the corresponding GNU and
17529 DWARF constants are the same. */
17530 switch (macinfo_type)
17531 {
17532 /* A zero macinfo type indicates the end of the macro
17533 information. */
17534 case 0:
17535 break;
17536
17537 case DW_MACRO_GNU_define:
17538 case DW_MACRO_GNU_undef:
17539 /* Only skip the data by MAC_PTR. */
17540 {
17541 unsigned int bytes_read;
17542
17543 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17544 mac_ptr += bytes_read;
17545 read_direct_string (abfd, mac_ptr, &bytes_read);
17546 mac_ptr += bytes_read;
17547 }
17548 break;
17549
17550 case DW_MACRO_GNU_start_file:
17551 {
17552 unsigned int bytes_read;
17553 int line, file;
17554
17555 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17556 mac_ptr += bytes_read;
17557 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17558 mac_ptr += bytes_read;
17559
17560 current_file = macro_start_file (file, line, current_file,
17561 comp_dir, lh, objfile);
17562 }
17563 break;
17564
17565 case DW_MACRO_GNU_end_file:
17566 /* No data to skip by MAC_PTR. */
17567 break;
17568
17569 case DW_MACRO_GNU_define_indirect:
17570 case DW_MACRO_GNU_undef_indirect:
17571 {
17572 unsigned int bytes_read;
17573
17574 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17575 mac_ptr += bytes_read;
17576 mac_ptr += offset_size;
17577 }
17578 break;
17579
17580 case DW_MACRO_GNU_transparent_include:
17581 /* Note that, according to the spec, a transparent include
17582 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17583 skip this opcode. */
17584 mac_ptr += offset_size;
17585 break;
17586
17587 case DW_MACINFO_vendor_ext:
17588 /* Only skip the data by MAC_PTR. */
17589 if (!section_is_gnu)
17590 {
17591 unsigned int bytes_read;
17592
17593 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17594 mac_ptr += bytes_read;
17595 read_direct_string (abfd, mac_ptr, &bytes_read);
17596 mac_ptr += bytes_read;
17597 }
17598 /* FALLTHROUGH */
17599
17600 default:
17601 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17602 mac_ptr, mac_end, abfd, offset_size,
17603 section);
17604 if (mac_ptr == NULL)
17605 return;
17606 break;
17607 }
17608 } while (macinfo_type != 0 && current_file == NULL);
17609
17610 /* Second pass: Process all entries.
17611
17612 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17613 command-line macro definitions/undefinitions. This flag is unset when we
17614 reach the first DW_MACINFO_start_file entry. */
17615
17616 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17617 NULL, xcalloc, xfree);
17618 cleanup = make_cleanup_htab_delete (include_hash);
17619 mac_ptr = section->buffer + offset;
17620 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17621 *slot = mac_ptr;
17622 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17623 current_file, lh, comp_dir, section, section_is_gnu,
17624 offset_size, objfile, include_hash);
17625 do_cleanups (cleanup);
17626 }
17627
17628 /* Check if the attribute's form is a DW_FORM_block*
17629 if so return true else false. */
17630
17631 static int
17632 attr_form_is_block (struct attribute *attr)
17633 {
17634 return (attr == NULL ? 0 :
17635 attr->form == DW_FORM_block1
17636 || attr->form == DW_FORM_block2
17637 || attr->form == DW_FORM_block4
17638 || attr->form == DW_FORM_block
17639 || attr->form == DW_FORM_exprloc);
17640 }
17641
17642 /* Return non-zero if ATTR's value is a section offset --- classes
17643 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17644 You may use DW_UNSND (attr) to retrieve such offsets.
17645
17646 Section 7.5.4, "Attribute Encodings", explains that no attribute
17647 may have a value that belongs to more than one of these classes; it
17648 would be ambiguous if we did, because we use the same forms for all
17649 of them. */
17650
17651 static int
17652 attr_form_is_section_offset (struct attribute *attr)
17653 {
17654 return (attr->form == DW_FORM_data4
17655 || attr->form == DW_FORM_data8
17656 || attr->form == DW_FORM_sec_offset);
17657 }
17658
17659 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17660 zero otherwise. When this function returns true, you can apply
17661 dwarf2_get_attr_constant_value to it.
17662
17663 However, note that for some attributes you must check
17664 attr_form_is_section_offset before using this test. DW_FORM_data4
17665 and DW_FORM_data8 are members of both the constant class, and of
17666 the classes that contain offsets into other debug sections
17667 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17668 that, if an attribute's can be either a constant or one of the
17669 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17670 taken as section offsets, not constants. */
17671
17672 static int
17673 attr_form_is_constant (struct attribute *attr)
17674 {
17675 switch (attr->form)
17676 {
17677 case DW_FORM_sdata:
17678 case DW_FORM_udata:
17679 case DW_FORM_data1:
17680 case DW_FORM_data2:
17681 case DW_FORM_data4:
17682 case DW_FORM_data8:
17683 return 1;
17684 default:
17685 return 0;
17686 }
17687 }
17688
17689 /* Return the .debug_loc section to use for CU.
17690 For DWO files use .debug_loc.dwo. */
17691
17692 static struct dwarf2_section_info *
17693 cu_debug_loc_section (struct dwarf2_cu *cu)
17694 {
17695 if (cu->dwo_unit)
17696 return &cu->dwo_unit->dwo_file->sections.loc;
17697 return &dwarf2_per_objfile->loc;
17698 }
17699
17700 /* A helper function that fills in a dwarf2_loclist_baton. */
17701
17702 static void
17703 fill_in_loclist_baton (struct dwarf2_cu *cu,
17704 struct dwarf2_loclist_baton *baton,
17705 struct attribute *attr)
17706 {
17707 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17708
17709 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17710
17711 baton->per_cu = cu->per_cu;
17712 gdb_assert (baton->per_cu);
17713 /* We don't know how long the location list is, but make sure we
17714 don't run off the edge of the section. */
17715 baton->size = section->size - DW_UNSND (attr);
17716 baton->data = section->buffer + DW_UNSND (attr);
17717 baton->base_address = cu->base_address;
17718 baton->from_dwo = cu->dwo_unit != NULL;
17719 }
17720
17721 static void
17722 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
17723 struct dwarf2_cu *cu)
17724 {
17725 struct objfile *objfile = dwarf2_per_objfile->objfile;
17726 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17727
17728 if (attr_form_is_section_offset (attr)
17729 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
17730 the section. If so, fall through to the complaint in the
17731 other branch. */
17732 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
17733 {
17734 struct dwarf2_loclist_baton *baton;
17735
17736 baton = obstack_alloc (&objfile->objfile_obstack,
17737 sizeof (struct dwarf2_loclist_baton));
17738
17739 fill_in_loclist_baton (cu, baton, attr);
17740
17741 if (cu->base_known == 0)
17742 complaint (&symfile_complaints,
17743 _("Location list used without "
17744 "specifying the CU base address."));
17745
17746 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
17747 SYMBOL_LOCATION_BATON (sym) = baton;
17748 }
17749 else
17750 {
17751 struct dwarf2_locexpr_baton *baton;
17752
17753 baton = obstack_alloc (&objfile->objfile_obstack,
17754 sizeof (struct dwarf2_locexpr_baton));
17755 baton->per_cu = cu->per_cu;
17756 gdb_assert (baton->per_cu);
17757
17758 if (attr_form_is_block (attr))
17759 {
17760 /* Note that we're just copying the block's data pointer
17761 here, not the actual data. We're still pointing into the
17762 info_buffer for SYM's objfile; right now we never release
17763 that buffer, but when we do clean up properly this may
17764 need to change. */
17765 baton->size = DW_BLOCK (attr)->size;
17766 baton->data = DW_BLOCK (attr)->data;
17767 }
17768 else
17769 {
17770 dwarf2_invalid_attrib_class_complaint ("location description",
17771 SYMBOL_NATURAL_NAME (sym));
17772 baton->size = 0;
17773 }
17774
17775 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
17776 SYMBOL_LOCATION_BATON (sym) = baton;
17777 }
17778 }
17779
17780 /* Return the OBJFILE associated with the compilation unit CU. If CU
17781 came from a separate debuginfo file, then the master objfile is
17782 returned. */
17783
17784 struct objfile *
17785 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
17786 {
17787 struct objfile *objfile = per_cu->objfile;
17788
17789 /* Return the master objfile, so that we can report and look up the
17790 correct file containing this variable. */
17791 if (objfile->separate_debug_objfile_backlink)
17792 objfile = objfile->separate_debug_objfile_backlink;
17793
17794 return objfile;
17795 }
17796
17797 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
17798 (CU_HEADERP is unused in such case) or prepare a temporary copy at
17799 CU_HEADERP first. */
17800
17801 static const struct comp_unit_head *
17802 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
17803 struct dwarf2_per_cu_data *per_cu)
17804 {
17805 gdb_byte *info_ptr;
17806
17807 if (per_cu->cu)
17808 return &per_cu->cu->header;
17809
17810 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
17811
17812 memset (cu_headerp, 0, sizeof (*cu_headerp));
17813 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
17814
17815 return cu_headerp;
17816 }
17817
17818 /* Return the address size given in the compilation unit header for CU. */
17819
17820 int
17821 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
17822 {
17823 struct comp_unit_head cu_header_local;
17824 const struct comp_unit_head *cu_headerp;
17825
17826 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17827
17828 return cu_headerp->addr_size;
17829 }
17830
17831 /* Return the offset size given in the compilation unit header for CU. */
17832
17833 int
17834 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
17835 {
17836 struct comp_unit_head cu_header_local;
17837 const struct comp_unit_head *cu_headerp;
17838
17839 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17840
17841 return cu_headerp->offset_size;
17842 }
17843
17844 /* See its dwarf2loc.h declaration. */
17845
17846 int
17847 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
17848 {
17849 struct comp_unit_head cu_header_local;
17850 const struct comp_unit_head *cu_headerp;
17851
17852 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17853
17854 if (cu_headerp->version == 2)
17855 return cu_headerp->addr_size;
17856 else
17857 return cu_headerp->offset_size;
17858 }
17859
17860 /* Return the text offset of the CU. The returned offset comes from
17861 this CU's objfile. If this objfile came from a separate debuginfo
17862 file, then the offset may be different from the corresponding
17863 offset in the parent objfile. */
17864
17865 CORE_ADDR
17866 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
17867 {
17868 struct objfile *objfile = per_cu->objfile;
17869
17870 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17871 }
17872
17873 /* Locate the .debug_info compilation unit from CU's objfile which contains
17874 the DIE at OFFSET. Raises an error on failure. */
17875
17876 static struct dwarf2_per_cu_data *
17877 dwarf2_find_containing_comp_unit (sect_offset offset,
17878 struct objfile *objfile)
17879 {
17880 struct dwarf2_per_cu_data *this_cu;
17881 int low, high;
17882
17883 low = 0;
17884 high = dwarf2_per_objfile->n_comp_units - 1;
17885 while (high > low)
17886 {
17887 int mid = low + (high - low) / 2;
17888
17889 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17890 >= offset.sect_off)
17891 high = mid;
17892 else
17893 low = mid + 1;
17894 }
17895 gdb_assert (low == high);
17896 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17897 > offset.sect_off)
17898 {
17899 if (low == 0)
17900 error (_("Dwarf Error: could not find partial DIE containing "
17901 "offset 0x%lx [in module %s]"),
17902 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
17903
17904 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17905 <= offset.sect_off);
17906 return dwarf2_per_objfile->all_comp_units[low-1];
17907 }
17908 else
17909 {
17910 this_cu = dwarf2_per_objfile->all_comp_units[low];
17911 if (low == dwarf2_per_objfile->n_comp_units - 1
17912 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17913 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17914 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
17915 return this_cu;
17916 }
17917 }
17918
17919 /* Initialize dwarf2_cu CU, owned by PER_CU. */
17920
17921 static void
17922 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
17923 {
17924 memset (cu, 0, sizeof (*cu));
17925 per_cu->cu = cu;
17926 cu->per_cu = per_cu;
17927 cu->objfile = per_cu->objfile;
17928 obstack_init (&cu->comp_unit_obstack);
17929 }
17930
17931 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17932
17933 static void
17934 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17935 enum language pretend_language)
17936 {
17937 struct attribute *attr;
17938
17939 /* Set the language we're debugging. */
17940 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17941 if (attr)
17942 set_cu_language (DW_UNSND (attr), cu);
17943 else
17944 {
17945 cu->language = pretend_language;
17946 cu->language_defn = language_def (cu->language);
17947 }
17948
17949 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17950 if (attr)
17951 cu->producer = DW_STRING (attr);
17952 }
17953
17954 /* Release one cached compilation unit, CU. We unlink it from the tree
17955 of compilation units, but we don't remove it from the read_in_chain;
17956 the caller is responsible for that.
17957 NOTE: DATA is a void * because this function is also used as a
17958 cleanup routine. */
17959
17960 static void
17961 free_heap_comp_unit (void *data)
17962 {
17963 struct dwarf2_cu *cu = data;
17964
17965 gdb_assert (cu->per_cu != NULL);
17966 cu->per_cu->cu = NULL;
17967 cu->per_cu = NULL;
17968
17969 obstack_free (&cu->comp_unit_obstack, NULL);
17970
17971 xfree (cu);
17972 }
17973
17974 /* This cleanup function is passed the address of a dwarf2_cu on the stack
17975 when we're finished with it. We can't free the pointer itself, but be
17976 sure to unlink it from the cache. Also release any associated storage. */
17977
17978 static void
17979 free_stack_comp_unit (void *data)
17980 {
17981 struct dwarf2_cu *cu = data;
17982
17983 gdb_assert (cu->per_cu != NULL);
17984 cu->per_cu->cu = NULL;
17985 cu->per_cu = NULL;
17986
17987 obstack_free (&cu->comp_unit_obstack, NULL);
17988 cu->partial_dies = NULL;
17989 }
17990
17991 /* Free all cached compilation units. */
17992
17993 static void
17994 free_cached_comp_units (void *data)
17995 {
17996 struct dwarf2_per_cu_data *per_cu, **last_chain;
17997
17998 per_cu = dwarf2_per_objfile->read_in_chain;
17999 last_chain = &dwarf2_per_objfile->read_in_chain;
18000 while (per_cu != NULL)
18001 {
18002 struct dwarf2_per_cu_data *next_cu;
18003
18004 next_cu = per_cu->cu->read_in_chain;
18005
18006 free_heap_comp_unit (per_cu->cu);
18007 *last_chain = next_cu;
18008
18009 per_cu = next_cu;
18010 }
18011 }
18012
18013 /* Increase the age counter on each cached compilation unit, and free
18014 any that are too old. */
18015
18016 static void
18017 age_cached_comp_units (void)
18018 {
18019 struct dwarf2_per_cu_data *per_cu, **last_chain;
18020
18021 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18022 per_cu = dwarf2_per_objfile->read_in_chain;
18023 while (per_cu != NULL)
18024 {
18025 per_cu->cu->last_used ++;
18026 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18027 dwarf2_mark (per_cu->cu);
18028 per_cu = per_cu->cu->read_in_chain;
18029 }
18030
18031 per_cu = dwarf2_per_objfile->read_in_chain;
18032 last_chain = &dwarf2_per_objfile->read_in_chain;
18033 while (per_cu != NULL)
18034 {
18035 struct dwarf2_per_cu_data *next_cu;
18036
18037 next_cu = per_cu->cu->read_in_chain;
18038
18039 if (!per_cu->cu->mark)
18040 {
18041 free_heap_comp_unit (per_cu->cu);
18042 *last_chain = next_cu;
18043 }
18044 else
18045 last_chain = &per_cu->cu->read_in_chain;
18046
18047 per_cu = next_cu;
18048 }
18049 }
18050
18051 /* Remove a single compilation unit from the cache. */
18052
18053 static void
18054 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
18055 {
18056 struct dwarf2_per_cu_data *per_cu, **last_chain;
18057
18058 per_cu = dwarf2_per_objfile->read_in_chain;
18059 last_chain = &dwarf2_per_objfile->read_in_chain;
18060 while (per_cu != NULL)
18061 {
18062 struct dwarf2_per_cu_data *next_cu;
18063
18064 next_cu = per_cu->cu->read_in_chain;
18065
18066 if (per_cu == target_per_cu)
18067 {
18068 free_heap_comp_unit (per_cu->cu);
18069 per_cu->cu = NULL;
18070 *last_chain = next_cu;
18071 break;
18072 }
18073 else
18074 last_chain = &per_cu->cu->read_in_chain;
18075
18076 per_cu = next_cu;
18077 }
18078 }
18079
18080 /* Release all extra memory associated with OBJFILE. */
18081
18082 void
18083 dwarf2_free_objfile (struct objfile *objfile)
18084 {
18085 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18086
18087 if (dwarf2_per_objfile == NULL)
18088 return;
18089
18090 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18091 free_cached_comp_units (NULL);
18092
18093 if (dwarf2_per_objfile->quick_file_names_table)
18094 htab_delete (dwarf2_per_objfile->quick_file_names_table);
18095
18096 /* Everything else should be on the objfile obstack. */
18097 }
18098
18099 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18100 We store these in a hash table separate from the DIEs, and preserve them
18101 when the DIEs are flushed out of cache.
18102
18103 The CU "per_cu" pointer is needed because offset alone is not enough to
18104 uniquely identify the type. A file may have multiple .debug_types sections,
18105 or the type may come from a DWO file. We have to use something in
18106 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18107 routine, get_die_type_at_offset, from outside this file, and thus won't
18108 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18109 of the objfile. */
18110
18111 struct dwarf2_per_cu_offset_and_type
18112 {
18113 const struct dwarf2_per_cu_data *per_cu;
18114 sect_offset offset;
18115 struct type *type;
18116 };
18117
18118 /* Hash function for a dwarf2_per_cu_offset_and_type. */
18119
18120 static hashval_t
18121 per_cu_offset_and_type_hash (const void *item)
18122 {
18123 const struct dwarf2_per_cu_offset_and_type *ofs = item;
18124
18125 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
18126 }
18127
18128 /* Equality function for a dwarf2_per_cu_offset_and_type. */
18129
18130 static int
18131 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
18132 {
18133 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18134 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
18135
18136 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18137 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18138 }
18139
18140 /* Set the type associated with DIE to TYPE. Save it in CU's hash
18141 table if necessary. For convenience, return TYPE.
18142
18143 The DIEs reading must have careful ordering to:
18144 * Not cause infite loops trying to read in DIEs as a prerequisite for
18145 reading current DIE.
18146 * Not trying to dereference contents of still incompletely read in types
18147 while reading in other DIEs.
18148 * Enable referencing still incompletely read in types just by a pointer to
18149 the type without accessing its fields.
18150
18151 Therefore caller should follow these rules:
18152 * Try to fetch any prerequisite types we may need to build this DIE type
18153 before building the type and calling set_die_type.
18154 * After building type call set_die_type for current DIE as soon as
18155 possible before fetching more types to complete the current type.
18156 * Make the type as complete as possible before fetching more types. */
18157
18158 static struct type *
18159 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18160 {
18161 struct dwarf2_per_cu_offset_and_type **slot, ofs;
18162 struct objfile *objfile = cu->objfile;
18163
18164 /* For Ada types, make sure that the gnat-specific data is always
18165 initialized (if not already set). There are a few types where
18166 we should not be doing so, because the type-specific area is
18167 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18168 where the type-specific area is used to store the floatformat).
18169 But this is not a problem, because the gnat-specific information
18170 is actually not needed for these types. */
18171 if (need_gnat_info (cu)
18172 && TYPE_CODE (type) != TYPE_CODE_FUNC
18173 && TYPE_CODE (type) != TYPE_CODE_FLT
18174 && !HAVE_GNAT_AUX_INFO (type))
18175 INIT_GNAT_SPECIFIC (type);
18176
18177 if (dwarf2_per_objfile->die_type_hash == NULL)
18178 {
18179 dwarf2_per_objfile->die_type_hash =
18180 htab_create_alloc_ex (127,
18181 per_cu_offset_and_type_hash,
18182 per_cu_offset_and_type_eq,
18183 NULL,
18184 &objfile->objfile_obstack,
18185 hashtab_obstack_allocate,
18186 dummy_obstack_deallocate);
18187 }
18188
18189 ofs.per_cu = cu->per_cu;
18190 ofs.offset = die->offset;
18191 ofs.type = type;
18192 slot = (struct dwarf2_per_cu_offset_and_type **)
18193 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18194 if (*slot)
18195 complaint (&symfile_complaints,
18196 _("A problem internal to GDB: DIE 0x%x has type already set"),
18197 die->offset.sect_off);
18198 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18199 **slot = ofs;
18200 return type;
18201 }
18202
18203 /* Look up the type for the die at OFFSET in the appropriate type_hash
18204 table, or return NULL if the die does not have a saved type. */
18205
18206 static struct type *
18207 get_die_type_at_offset (sect_offset offset,
18208 struct dwarf2_per_cu_data *per_cu)
18209 {
18210 struct dwarf2_per_cu_offset_and_type *slot, ofs;
18211
18212 if (dwarf2_per_objfile->die_type_hash == NULL)
18213 return NULL;
18214
18215 ofs.per_cu = per_cu;
18216 ofs.offset = offset;
18217 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18218 if (slot)
18219 return slot->type;
18220 else
18221 return NULL;
18222 }
18223
18224 /* Look up the type for DIE in the appropriate type_hash table,
18225 or return NULL if DIE does not have a saved type. */
18226
18227 static struct type *
18228 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18229 {
18230 return get_die_type_at_offset (die->offset, cu->per_cu);
18231 }
18232
18233 /* Add a dependence relationship from CU to REF_PER_CU. */
18234
18235 static void
18236 dwarf2_add_dependence (struct dwarf2_cu *cu,
18237 struct dwarf2_per_cu_data *ref_per_cu)
18238 {
18239 void **slot;
18240
18241 if (cu->dependencies == NULL)
18242 cu->dependencies
18243 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18244 NULL, &cu->comp_unit_obstack,
18245 hashtab_obstack_allocate,
18246 dummy_obstack_deallocate);
18247
18248 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18249 if (*slot == NULL)
18250 *slot = ref_per_cu;
18251 }
18252
18253 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18254 Set the mark field in every compilation unit in the
18255 cache that we must keep because we are keeping CU. */
18256
18257 static int
18258 dwarf2_mark_helper (void **slot, void *data)
18259 {
18260 struct dwarf2_per_cu_data *per_cu;
18261
18262 per_cu = (struct dwarf2_per_cu_data *) *slot;
18263
18264 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18265 reading of the chain. As such dependencies remain valid it is not much
18266 useful to track and undo them during QUIT cleanups. */
18267 if (per_cu->cu == NULL)
18268 return 1;
18269
18270 if (per_cu->cu->mark)
18271 return 1;
18272 per_cu->cu->mark = 1;
18273
18274 if (per_cu->cu->dependencies != NULL)
18275 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18276
18277 return 1;
18278 }
18279
18280 /* Set the mark field in CU and in every other compilation unit in the
18281 cache that we must keep because we are keeping CU. */
18282
18283 static void
18284 dwarf2_mark (struct dwarf2_cu *cu)
18285 {
18286 if (cu->mark)
18287 return;
18288 cu->mark = 1;
18289 if (cu->dependencies != NULL)
18290 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18291 }
18292
18293 static void
18294 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18295 {
18296 while (per_cu)
18297 {
18298 per_cu->cu->mark = 0;
18299 per_cu = per_cu->cu->read_in_chain;
18300 }
18301 }
18302
18303 /* Trivial hash function for partial_die_info: the hash value of a DIE
18304 is its offset in .debug_info for this objfile. */
18305
18306 static hashval_t
18307 partial_die_hash (const void *item)
18308 {
18309 const struct partial_die_info *part_die = item;
18310
18311 return part_die->offset.sect_off;
18312 }
18313
18314 /* Trivial comparison function for partial_die_info structures: two DIEs
18315 are equal if they have the same offset. */
18316
18317 static int
18318 partial_die_eq (const void *item_lhs, const void *item_rhs)
18319 {
18320 const struct partial_die_info *part_die_lhs = item_lhs;
18321 const struct partial_die_info *part_die_rhs = item_rhs;
18322
18323 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18324 }
18325
18326 static struct cmd_list_element *set_dwarf2_cmdlist;
18327 static struct cmd_list_element *show_dwarf2_cmdlist;
18328
18329 static void
18330 set_dwarf2_cmd (char *args, int from_tty)
18331 {
18332 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18333 }
18334
18335 static void
18336 show_dwarf2_cmd (char *args, int from_tty)
18337 {
18338 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18339 }
18340
18341 /* If section described by INFO was mmapped, munmap it now. */
18342
18343 static void
18344 munmap_section_buffer (struct dwarf2_section_info *info)
18345 {
18346 if (info->map_addr != NULL)
18347 {
18348 #ifdef HAVE_MMAP
18349 int res;
18350
18351 res = munmap (info->map_addr, info->map_len);
18352 gdb_assert (res == 0);
18353 #else
18354 /* Without HAVE_MMAP, we should never be here to begin with. */
18355 gdb_assert_not_reached ("no mmap support");
18356 #endif
18357 }
18358 }
18359
18360 /* munmap debug sections for OBJFILE, if necessary. */
18361
18362 static void
18363 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18364 {
18365 struct dwarf2_per_objfile *data = d;
18366 int ix;
18367 struct dwarf2_section_info *section;
18368
18369 /* This is sorted according to the order they're defined in to make it easier
18370 to keep in sync. */
18371 munmap_section_buffer (&data->info);
18372 munmap_section_buffer (&data->abbrev);
18373 munmap_section_buffer (&data->line);
18374 munmap_section_buffer (&data->loc);
18375 munmap_section_buffer (&data->macinfo);
18376 munmap_section_buffer (&data->macro);
18377 munmap_section_buffer (&data->str);
18378 munmap_section_buffer (&data->ranges);
18379 munmap_section_buffer (&data->addr);
18380 munmap_section_buffer (&data->frame);
18381 munmap_section_buffer (&data->eh_frame);
18382 munmap_section_buffer (&data->gdb_index);
18383
18384 for (ix = 0;
18385 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
18386 ++ix)
18387 munmap_section_buffer (section);
18388
18389 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18390 VEC_free (dwarf2_per_cu_ptr,
18391 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18392
18393 VEC_free (dwarf2_section_info_def, data->types);
18394
18395 if (data->dwo_files)
18396 free_dwo_files (data->dwo_files, objfile);
18397 }
18398
18399 \f
18400 /* The "save gdb-index" command. */
18401
18402 /* The contents of the hash table we create when building the string
18403 table. */
18404 struct strtab_entry
18405 {
18406 offset_type offset;
18407 const char *str;
18408 };
18409
18410 /* Hash function for a strtab_entry.
18411
18412 Function is used only during write_hash_table so no index format backward
18413 compatibility is needed. */
18414
18415 static hashval_t
18416 hash_strtab_entry (const void *e)
18417 {
18418 const struct strtab_entry *entry = e;
18419 return mapped_index_string_hash (INT_MAX, entry->str);
18420 }
18421
18422 /* Equality function for a strtab_entry. */
18423
18424 static int
18425 eq_strtab_entry (const void *a, const void *b)
18426 {
18427 const struct strtab_entry *ea = a;
18428 const struct strtab_entry *eb = b;
18429 return !strcmp (ea->str, eb->str);
18430 }
18431
18432 /* Create a strtab_entry hash table. */
18433
18434 static htab_t
18435 create_strtab (void)
18436 {
18437 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18438 xfree, xcalloc, xfree);
18439 }
18440
18441 /* Add a string to the constant pool. Return the string's offset in
18442 host order. */
18443
18444 static offset_type
18445 add_string (htab_t table, struct obstack *cpool, const char *str)
18446 {
18447 void **slot;
18448 struct strtab_entry entry;
18449 struct strtab_entry *result;
18450
18451 entry.str = str;
18452 slot = htab_find_slot (table, &entry, INSERT);
18453 if (*slot)
18454 result = *slot;
18455 else
18456 {
18457 result = XNEW (struct strtab_entry);
18458 result->offset = obstack_object_size (cpool);
18459 result->str = str;
18460 obstack_grow_str0 (cpool, str);
18461 *slot = result;
18462 }
18463 return result->offset;
18464 }
18465
18466 /* An entry in the symbol table. */
18467 struct symtab_index_entry
18468 {
18469 /* The name of the symbol. */
18470 const char *name;
18471 /* The offset of the name in the constant pool. */
18472 offset_type index_offset;
18473 /* A sorted vector of the indices of all the CUs that hold an object
18474 of this name. */
18475 VEC (offset_type) *cu_indices;
18476 };
18477
18478 /* The symbol table. This is a power-of-2-sized hash table. */
18479 struct mapped_symtab
18480 {
18481 offset_type n_elements;
18482 offset_type size;
18483 struct symtab_index_entry **data;
18484 };
18485
18486 /* Hash function for a symtab_index_entry. */
18487
18488 static hashval_t
18489 hash_symtab_entry (const void *e)
18490 {
18491 const struct symtab_index_entry *entry = e;
18492 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18493 sizeof (offset_type) * VEC_length (offset_type,
18494 entry->cu_indices),
18495 0);
18496 }
18497
18498 /* Equality function for a symtab_index_entry. */
18499
18500 static int
18501 eq_symtab_entry (const void *a, const void *b)
18502 {
18503 const struct symtab_index_entry *ea = a;
18504 const struct symtab_index_entry *eb = b;
18505 int len = VEC_length (offset_type, ea->cu_indices);
18506 if (len != VEC_length (offset_type, eb->cu_indices))
18507 return 0;
18508 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18509 VEC_address (offset_type, eb->cu_indices),
18510 sizeof (offset_type) * len);
18511 }
18512
18513 /* Destroy a symtab_index_entry. */
18514
18515 static void
18516 delete_symtab_entry (void *p)
18517 {
18518 struct symtab_index_entry *entry = p;
18519 VEC_free (offset_type, entry->cu_indices);
18520 xfree (entry);
18521 }
18522
18523 /* Create a hash table holding symtab_index_entry objects. */
18524
18525 static htab_t
18526 create_symbol_hash_table (void)
18527 {
18528 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18529 delete_symtab_entry, xcalloc, xfree);
18530 }
18531
18532 /* Create a new mapped symtab object. */
18533
18534 static struct mapped_symtab *
18535 create_mapped_symtab (void)
18536 {
18537 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18538 symtab->n_elements = 0;
18539 symtab->size = 1024;
18540 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18541 return symtab;
18542 }
18543
18544 /* Destroy a mapped_symtab. */
18545
18546 static void
18547 cleanup_mapped_symtab (void *p)
18548 {
18549 struct mapped_symtab *symtab = p;
18550 /* The contents of the array are freed when the other hash table is
18551 destroyed. */
18552 xfree (symtab->data);
18553 xfree (symtab);
18554 }
18555
18556 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
18557 the slot.
18558
18559 Function is used only during write_hash_table so no index format backward
18560 compatibility is needed. */
18561
18562 static struct symtab_index_entry **
18563 find_slot (struct mapped_symtab *symtab, const char *name)
18564 {
18565 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18566
18567 index = hash & (symtab->size - 1);
18568 step = ((hash * 17) & (symtab->size - 1)) | 1;
18569
18570 for (;;)
18571 {
18572 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18573 return &symtab->data[index];
18574 index = (index + step) & (symtab->size - 1);
18575 }
18576 }
18577
18578 /* Expand SYMTAB's hash table. */
18579
18580 static void
18581 hash_expand (struct mapped_symtab *symtab)
18582 {
18583 offset_type old_size = symtab->size;
18584 offset_type i;
18585 struct symtab_index_entry **old_entries = symtab->data;
18586
18587 symtab->size *= 2;
18588 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18589
18590 for (i = 0; i < old_size; ++i)
18591 {
18592 if (old_entries[i])
18593 {
18594 struct symtab_index_entry **slot = find_slot (symtab,
18595 old_entries[i]->name);
18596 *slot = old_entries[i];
18597 }
18598 }
18599
18600 xfree (old_entries);
18601 }
18602
18603 /* Add an entry to SYMTAB. NAME is the name of the symbol.
18604 CU_INDEX is the index of the CU in which the symbol appears.
18605 IS_STATIC is one if the symbol is static, otherwise zero (global). */
18606
18607 static void
18608 add_index_entry (struct mapped_symtab *symtab, const char *name,
18609 int is_static, gdb_index_symbol_kind kind,
18610 offset_type cu_index)
18611 {
18612 struct symtab_index_entry **slot;
18613 offset_type cu_index_and_attrs;
18614
18615 ++symtab->n_elements;
18616 if (4 * symtab->n_elements / 3 >= symtab->size)
18617 hash_expand (symtab);
18618
18619 slot = find_slot (symtab, name);
18620 if (!*slot)
18621 {
18622 *slot = XNEW (struct symtab_index_entry);
18623 (*slot)->name = name;
18624 /* index_offset is set later. */
18625 (*slot)->cu_indices = NULL;
18626 }
18627
18628 cu_index_and_attrs = 0;
18629 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18630 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18631 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18632
18633 /* We don't want to record an index value twice as we want to avoid the
18634 duplication.
18635 We process all global symbols and then all static symbols
18636 (which would allow us to avoid the duplication by only having to check
18637 the last entry pushed), but a symbol could have multiple kinds in one CU.
18638 To keep things simple we don't worry about the duplication here and
18639 sort and uniqufy the list after we've processed all symbols. */
18640 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18641 }
18642
18643 /* qsort helper routine for uniquify_cu_indices. */
18644
18645 static int
18646 offset_type_compare (const void *ap, const void *bp)
18647 {
18648 offset_type a = *(offset_type *) ap;
18649 offset_type b = *(offset_type *) bp;
18650
18651 return (a > b) - (b > a);
18652 }
18653
18654 /* Sort and remove duplicates of all symbols' cu_indices lists. */
18655
18656 static void
18657 uniquify_cu_indices (struct mapped_symtab *symtab)
18658 {
18659 int i;
18660
18661 for (i = 0; i < symtab->size; ++i)
18662 {
18663 struct symtab_index_entry *entry = symtab->data[i];
18664
18665 if (entry
18666 && entry->cu_indices != NULL)
18667 {
18668 unsigned int next_to_insert, next_to_check;
18669 offset_type last_value;
18670
18671 qsort (VEC_address (offset_type, entry->cu_indices),
18672 VEC_length (offset_type, entry->cu_indices),
18673 sizeof (offset_type), offset_type_compare);
18674
18675 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18676 next_to_insert = 1;
18677 for (next_to_check = 1;
18678 next_to_check < VEC_length (offset_type, entry->cu_indices);
18679 ++next_to_check)
18680 {
18681 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18682 != last_value)
18683 {
18684 last_value = VEC_index (offset_type, entry->cu_indices,
18685 next_to_check);
18686 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18687 last_value);
18688 ++next_to_insert;
18689 }
18690 }
18691 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18692 }
18693 }
18694 }
18695
18696 /* Add a vector of indices to the constant pool. */
18697
18698 static offset_type
18699 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18700 struct symtab_index_entry *entry)
18701 {
18702 void **slot;
18703
18704 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18705 if (!*slot)
18706 {
18707 offset_type len = VEC_length (offset_type, entry->cu_indices);
18708 offset_type val = MAYBE_SWAP (len);
18709 offset_type iter;
18710 int i;
18711
18712 *slot = entry;
18713 entry->index_offset = obstack_object_size (cpool);
18714
18715 obstack_grow (cpool, &val, sizeof (val));
18716 for (i = 0;
18717 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18718 ++i)
18719 {
18720 val = MAYBE_SWAP (iter);
18721 obstack_grow (cpool, &val, sizeof (val));
18722 }
18723 }
18724 else
18725 {
18726 struct symtab_index_entry *old_entry = *slot;
18727 entry->index_offset = old_entry->index_offset;
18728 entry = old_entry;
18729 }
18730 return entry->index_offset;
18731 }
18732
18733 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
18734 constant pool entries going into the obstack CPOOL. */
18735
18736 static void
18737 write_hash_table (struct mapped_symtab *symtab,
18738 struct obstack *output, struct obstack *cpool)
18739 {
18740 offset_type i;
18741 htab_t symbol_hash_table;
18742 htab_t str_table;
18743
18744 symbol_hash_table = create_symbol_hash_table ();
18745 str_table = create_strtab ();
18746
18747 /* We add all the index vectors to the constant pool first, to
18748 ensure alignment is ok. */
18749 for (i = 0; i < symtab->size; ++i)
18750 {
18751 if (symtab->data[i])
18752 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
18753 }
18754
18755 /* Now write out the hash table. */
18756 for (i = 0; i < symtab->size; ++i)
18757 {
18758 offset_type str_off, vec_off;
18759
18760 if (symtab->data[i])
18761 {
18762 str_off = add_string (str_table, cpool, symtab->data[i]->name);
18763 vec_off = symtab->data[i]->index_offset;
18764 }
18765 else
18766 {
18767 /* While 0 is a valid constant pool index, it is not valid
18768 to have 0 for both offsets. */
18769 str_off = 0;
18770 vec_off = 0;
18771 }
18772
18773 str_off = MAYBE_SWAP (str_off);
18774 vec_off = MAYBE_SWAP (vec_off);
18775
18776 obstack_grow (output, &str_off, sizeof (str_off));
18777 obstack_grow (output, &vec_off, sizeof (vec_off));
18778 }
18779
18780 htab_delete (str_table);
18781 htab_delete (symbol_hash_table);
18782 }
18783
18784 /* Struct to map psymtab to CU index in the index file. */
18785 struct psymtab_cu_index_map
18786 {
18787 struct partial_symtab *psymtab;
18788 unsigned int cu_index;
18789 };
18790
18791 static hashval_t
18792 hash_psymtab_cu_index (const void *item)
18793 {
18794 const struct psymtab_cu_index_map *map = item;
18795
18796 return htab_hash_pointer (map->psymtab);
18797 }
18798
18799 static int
18800 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
18801 {
18802 const struct psymtab_cu_index_map *lhs = item_lhs;
18803 const struct psymtab_cu_index_map *rhs = item_rhs;
18804
18805 return lhs->psymtab == rhs->psymtab;
18806 }
18807
18808 /* Helper struct for building the address table. */
18809 struct addrmap_index_data
18810 {
18811 struct objfile *objfile;
18812 struct obstack *addr_obstack;
18813 htab_t cu_index_htab;
18814
18815 /* Non-zero if the previous_* fields are valid.
18816 We can't write an entry until we see the next entry (since it is only then
18817 that we know the end of the entry). */
18818 int previous_valid;
18819 /* Index of the CU in the table of all CUs in the index file. */
18820 unsigned int previous_cu_index;
18821 /* Start address of the CU. */
18822 CORE_ADDR previous_cu_start;
18823 };
18824
18825 /* Write an address entry to OBSTACK. */
18826
18827 static void
18828 add_address_entry (struct objfile *objfile, struct obstack *obstack,
18829 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
18830 {
18831 offset_type cu_index_to_write;
18832 char addr[8];
18833 CORE_ADDR baseaddr;
18834
18835 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18836
18837 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
18838 obstack_grow (obstack, addr, 8);
18839 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
18840 obstack_grow (obstack, addr, 8);
18841 cu_index_to_write = MAYBE_SWAP (cu_index);
18842 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
18843 }
18844
18845 /* Worker function for traversing an addrmap to build the address table. */
18846
18847 static int
18848 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
18849 {
18850 struct addrmap_index_data *data = datap;
18851 struct partial_symtab *pst = obj;
18852
18853 if (data->previous_valid)
18854 add_address_entry (data->objfile, data->addr_obstack,
18855 data->previous_cu_start, start_addr,
18856 data->previous_cu_index);
18857
18858 data->previous_cu_start = start_addr;
18859 if (pst != NULL)
18860 {
18861 struct psymtab_cu_index_map find_map, *map;
18862 find_map.psymtab = pst;
18863 map = htab_find (data->cu_index_htab, &find_map);
18864 gdb_assert (map != NULL);
18865 data->previous_cu_index = map->cu_index;
18866 data->previous_valid = 1;
18867 }
18868 else
18869 data->previous_valid = 0;
18870
18871 return 0;
18872 }
18873
18874 /* Write OBJFILE's address map to OBSTACK.
18875 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18876 in the index file. */
18877
18878 static void
18879 write_address_map (struct objfile *objfile, struct obstack *obstack,
18880 htab_t cu_index_htab)
18881 {
18882 struct addrmap_index_data addrmap_index_data;
18883
18884 /* When writing the address table, we have to cope with the fact that
18885 the addrmap iterator only provides the start of a region; we have to
18886 wait until the next invocation to get the start of the next region. */
18887
18888 addrmap_index_data.objfile = objfile;
18889 addrmap_index_data.addr_obstack = obstack;
18890 addrmap_index_data.cu_index_htab = cu_index_htab;
18891 addrmap_index_data.previous_valid = 0;
18892
18893 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18894 &addrmap_index_data);
18895
18896 /* It's highly unlikely the last entry (end address = 0xff...ff)
18897 is valid, but we should still handle it.
18898 The end address is recorded as the start of the next region, but that
18899 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18900 anyway. */
18901 if (addrmap_index_data.previous_valid)
18902 add_address_entry (objfile, obstack,
18903 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18904 addrmap_index_data.previous_cu_index);
18905 }
18906
18907 /* Return the symbol kind of PSYM. */
18908
18909 static gdb_index_symbol_kind
18910 symbol_kind (struct partial_symbol *psym)
18911 {
18912 domain_enum domain = PSYMBOL_DOMAIN (psym);
18913 enum address_class aclass = PSYMBOL_CLASS (psym);
18914
18915 switch (domain)
18916 {
18917 case VAR_DOMAIN:
18918 switch (aclass)
18919 {
18920 case LOC_BLOCK:
18921 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
18922 case LOC_TYPEDEF:
18923 return GDB_INDEX_SYMBOL_KIND_TYPE;
18924 case LOC_COMPUTED:
18925 case LOC_CONST_BYTES:
18926 case LOC_OPTIMIZED_OUT:
18927 case LOC_STATIC:
18928 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18929 case LOC_CONST:
18930 /* Note: It's currently impossible to recognize psyms as enum values
18931 short of reading the type info. For now punt. */
18932 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18933 default:
18934 /* There are other LOC_FOO values that one might want to classify
18935 as variables, but dwarf2read.c doesn't currently use them. */
18936 return GDB_INDEX_SYMBOL_KIND_OTHER;
18937 }
18938 case STRUCT_DOMAIN:
18939 return GDB_INDEX_SYMBOL_KIND_TYPE;
18940 default:
18941 return GDB_INDEX_SYMBOL_KIND_OTHER;
18942 }
18943 }
18944
18945 /* Add a list of partial symbols to SYMTAB. */
18946
18947 static void
18948 write_psymbols (struct mapped_symtab *symtab,
18949 htab_t psyms_seen,
18950 struct partial_symbol **psymp,
18951 int count,
18952 offset_type cu_index,
18953 int is_static)
18954 {
18955 for (; count-- > 0; ++psymp)
18956 {
18957 struct partial_symbol *psym = *psymp;
18958 void **slot;
18959
18960 if (SYMBOL_LANGUAGE (psym) == language_ada)
18961 error (_("Ada is not currently supported by the index"));
18962
18963 /* Only add a given psymbol once. */
18964 slot = htab_find_slot (psyms_seen, psym, INSERT);
18965 if (!*slot)
18966 {
18967 gdb_index_symbol_kind kind = symbol_kind (psym);
18968
18969 *slot = psym;
18970 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18971 is_static, kind, cu_index);
18972 }
18973 }
18974 }
18975
18976 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
18977 exception if there is an error. */
18978
18979 static void
18980 write_obstack (FILE *file, struct obstack *obstack)
18981 {
18982 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18983 file)
18984 != obstack_object_size (obstack))
18985 error (_("couldn't data write to file"));
18986 }
18987
18988 /* Unlink a file if the argument is not NULL. */
18989
18990 static void
18991 unlink_if_set (void *p)
18992 {
18993 char **filename = p;
18994 if (*filename)
18995 unlink (*filename);
18996 }
18997
18998 /* A helper struct used when iterating over debug_types. */
18999 struct signatured_type_index_data
19000 {
19001 struct objfile *objfile;
19002 struct mapped_symtab *symtab;
19003 struct obstack *types_list;
19004 htab_t psyms_seen;
19005 int cu_index;
19006 };
19007
19008 /* A helper function that writes a single signatured_type to an
19009 obstack. */
19010
19011 static int
19012 write_one_signatured_type (void **slot, void *d)
19013 {
19014 struct signatured_type_index_data *info = d;
19015 struct signatured_type *entry = (struct signatured_type *) *slot;
19016 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19017 struct partial_symtab *psymtab = per_cu->v.psymtab;
19018 gdb_byte val[8];
19019
19020 write_psymbols (info->symtab,
19021 info->psyms_seen,
19022 info->objfile->global_psymbols.list
19023 + psymtab->globals_offset,
19024 psymtab->n_global_syms, info->cu_index,
19025 0);
19026 write_psymbols (info->symtab,
19027 info->psyms_seen,
19028 info->objfile->static_psymbols.list
19029 + psymtab->statics_offset,
19030 psymtab->n_static_syms, info->cu_index,
19031 1);
19032
19033 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19034 entry->per_cu.offset.sect_off);
19035 obstack_grow (info->types_list, val, 8);
19036 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19037 entry->type_offset_in_tu.cu_off);
19038 obstack_grow (info->types_list, val, 8);
19039 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19040 obstack_grow (info->types_list, val, 8);
19041
19042 ++info->cu_index;
19043
19044 return 1;
19045 }
19046
19047 /* Recurse into all "included" dependencies and write their symbols as
19048 if they appeared in this psymtab. */
19049
19050 static void
19051 recursively_write_psymbols (struct objfile *objfile,
19052 struct partial_symtab *psymtab,
19053 struct mapped_symtab *symtab,
19054 htab_t psyms_seen,
19055 offset_type cu_index)
19056 {
19057 int i;
19058
19059 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19060 if (psymtab->dependencies[i]->user != NULL)
19061 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19062 symtab, psyms_seen, cu_index);
19063
19064 write_psymbols (symtab,
19065 psyms_seen,
19066 objfile->global_psymbols.list + psymtab->globals_offset,
19067 psymtab->n_global_syms, cu_index,
19068 0);
19069 write_psymbols (symtab,
19070 psyms_seen,
19071 objfile->static_psymbols.list + psymtab->statics_offset,
19072 psymtab->n_static_syms, cu_index,
19073 1);
19074 }
19075
19076 /* Create an index file for OBJFILE in the directory DIR. */
19077
19078 static void
19079 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19080 {
19081 struct cleanup *cleanup;
19082 char *filename, *cleanup_filename;
19083 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19084 struct obstack cu_list, types_cu_list;
19085 int i;
19086 FILE *out_file;
19087 struct mapped_symtab *symtab;
19088 offset_type val, size_of_contents, total_len;
19089 struct stat st;
19090 htab_t psyms_seen;
19091 htab_t cu_index_htab;
19092 struct psymtab_cu_index_map *psymtab_cu_index_map;
19093
19094 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
19095 return;
19096
19097 if (dwarf2_per_objfile->using_index)
19098 error (_("Cannot use an index to create the index"));
19099
19100 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19101 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19102
19103 if (stat (objfile->name, &st) < 0)
19104 perror_with_name (objfile->name);
19105
19106 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19107 INDEX_SUFFIX, (char *) NULL);
19108 cleanup = make_cleanup (xfree, filename);
19109
19110 out_file = fopen (filename, "wb");
19111 if (!out_file)
19112 error (_("Can't open `%s' for writing"), filename);
19113
19114 cleanup_filename = filename;
19115 make_cleanup (unlink_if_set, &cleanup_filename);
19116
19117 symtab = create_mapped_symtab ();
19118 make_cleanup (cleanup_mapped_symtab, symtab);
19119
19120 obstack_init (&addr_obstack);
19121 make_cleanup_obstack_free (&addr_obstack);
19122
19123 obstack_init (&cu_list);
19124 make_cleanup_obstack_free (&cu_list);
19125
19126 obstack_init (&types_cu_list);
19127 make_cleanup_obstack_free (&types_cu_list);
19128
19129 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19130 NULL, xcalloc, xfree);
19131 make_cleanup_htab_delete (psyms_seen);
19132
19133 /* While we're scanning CU's create a table that maps a psymtab pointer
19134 (which is what addrmap records) to its index (which is what is recorded
19135 in the index file). This will later be needed to write the address
19136 table. */
19137 cu_index_htab = htab_create_alloc (100,
19138 hash_psymtab_cu_index,
19139 eq_psymtab_cu_index,
19140 NULL, xcalloc, xfree);
19141 make_cleanup_htab_delete (cu_index_htab);
19142 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19143 xmalloc (sizeof (struct psymtab_cu_index_map)
19144 * dwarf2_per_objfile->n_comp_units);
19145 make_cleanup (xfree, psymtab_cu_index_map);
19146
19147 /* The CU list is already sorted, so we don't need to do additional
19148 work here. Also, the debug_types entries do not appear in
19149 all_comp_units, but only in their own hash table. */
19150 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19151 {
19152 struct dwarf2_per_cu_data *per_cu
19153 = dwarf2_per_objfile->all_comp_units[i];
19154 struct partial_symtab *psymtab = per_cu->v.psymtab;
19155 gdb_byte val[8];
19156 struct psymtab_cu_index_map *map;
19157 void **slot;
19158
19159 if (psymtab->user == NULL)
19160 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
19161
19162 map = &psymtab_cu_index_map[i];
19163 map->psymtab = psymtab;
19164 map->cu_index = i;
19165 slot = htab_find_slot (cu_index_htab, map, INSERT);
19166 gdb_assert (slot != NULL);
19167 gdb_assert (*slot == NULL);
19168 *slot = map;
19169
19170 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19171 per_cu->offset.sect_off);
19172 obstack_grow (&cu_list, val, 8);
19173 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
19174 obstack_grow (&cu_list, val, 8);
19175 }
19176
19177 /* Dump the address map. */
19178 write_address_map (objfile, &addr_obstack, cu_index_htab);
19179
19180 /* Write out the .debug_type entries, if any. */
19181 if (dwarf2_per_objfile->signatured_types)
19182 {
19183 struct signatured_type_index_data sig_data;
19184
19185 sig_data.objfile = objfile;
19186 sig_data.symtab = symtab;
19187 sig_data.types_list = &types_cu_list;
19188 sig_data.psyms_seen = psyms_seen;
19189 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19190 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19191 write_one_signatured_type, &sig_data);
19192 }
19193
19194 /* Now that we've processed all symbols we can shrink their cu_indices
19195 lists. */
19196 uniquify_cu_indices (symtab);
19197
19198 obstack_init (&constant_pool);
19199 make_cleanup_obstack_free (&constant_pool);
19200 obstack_init (&symtab_obstack);
19201 make_cleanup_obstack_free (&symtab_obstack);
19202 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19203
19204 obstack_init (&contents);
19205 make_cleanup_obstack_free (&contents);
19206 size_of_contents = 6 * sizeof (offset_type);
19207 total_len = size_of_contents;
19208
19209 /* The version number. */
19210 val = MAYBE_SWAP (7);
19211 obstack_grow (&contents, &val, sizeof (val));
19212
19213 /* The offset of the CU list from the start of the file. */
19214 val = MAYBE_SWAP (total_len);
19215 obstack_grow (&contents, &val, sizeof (val));
19216 total_len += obstack_object_size (&cu_list);
19217
19218 /* The offset of the types CU list from the start of the file. */
19219 val = MAYBE_SWAP (total_len);
19220 obstack_grow (&contents, &val, sizeof (val));
19221 total_len += obstack_object_size (&types_cu_list);
19222
19223 /* The offset of the address table from the start of the file. */
19224 val = MAYBE_SWAP (total_len);
19225 obstack_grow (&contents, &val, sizeof (val));
19226 total_len += obstack_object_size (&addr_obstack);
19227
19228 /* The offset of the symbol table from the start of the file. */
19229 val = MAYBE_SWAP (total_len);
19230 obstack_grow (&contents, &val, sizeof (val));
19231 total_len += obstack_object_size (&symtab_obstack);
19232
19233 /* The offset of the constant pool from the start of the file. */
19234 val = MAYBE_SWAP (total_len);
19235 obstack_grow (&contents, &val, sizeof (val));
19236 total_len += obstack_object_size (&constant_pool);
19237
19238 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19239
19240 write_obstack (out_file, &contents);
19241 write_obstack (out_file, &cu_list);
19242 write_obstack (out_file, &types_cu_list);
19243 write_obstack (out_file, &addr_obstack);
19244 write_obstack (out_file, &symtab_obstack);
19245 write_obstack (out_file, &constant_pool);
19246
19247 fclose (out_file);
19248
19249 /* We want to keep the file, so we set cleanup_filename to NULL
19250 here. See unlink_if_set. */
19251 cleanup_filename = NULL;
19252
19253 do_cleanups (cleanup);
19254 }
19255
19256 /* Implementation of the `save gdb-index' command.
19257
19258 Note that the file format used by this command is documented in the
19259 GDB manual. Any changes here must be documented there. */
19260
19261 static void
19262 save_gdb_index_command (char *arg, int from_tty)
19263 {
19264 struct objfile *objfile;
19265
19266 if (!arg || !*arg)
19267 error (_("usage: save gdb-index DIRECTORY"));
19268
19269 ALL_OBJFILES (objfile)
19270 {
19271 struct stat st;
19272
19273 /* If the objfile does not correspond to an actual file, skip it. */
19274 if (stat (objfile->name, &st) < 0)
19275 continue;
19276
19277 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19278 if (dwarf2_per_objfile)
19279 {
19280 volatile struct gdb_exception except;
19281
19282 TRY_CATCH (except, RETURN_MASK_ERROR)
19283 {
19284 write_psymtabs_to_index (objfile, arg);
19285 }
19286 if (except.reason < 0)
19287 exception_fprintf (gdb_stderr, except,
19288 _("Error while writing index for `%s': "),
19289 objfile->name);
19290 }
19291 }
19292 }
19293
19294 \f
19295
19296 int dwarf2_always_disassemble;
19297
19298 static void
19299 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19300 struct cmd_list_element *c, const char *value)
19301 {
19302 fprintf_filtered (file,
19303 _("Whether to always disassemble "
19304 "DWARF expressions is %s.\n"),
19305 value);
19306 }
19307
19308 static void
19309 show_check_physname (struct ui_file *file, int from_tty,
19310 struct cmd_list_element *c, const char *value)
19311 {
19312 fprintf_filtered (file,
19313 _("Whether to check \"physname\" is %s.\n"),
19314 value);
19315 }
19316
19317 void _initialize_dwarf2_read (void);
19318
19319 void
19320 _initialize_dwarf2_read (void)
19321 {
19322 struct cmd_list_element *c;
19323
19324 dwarf2_objfile_data_key
19325 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19326
19327 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19328 Set DWARF 2 specific variables.\n\
19329 Configure DWARF 2 variables such as the cache size"),
19330 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19331 0/*allow-unknown*/, &maintenance_set_cmdlist);
19332
19333 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19334 Show DWARF 2 specific variables\n\
19335 Show DWARF 2 variables such as the cache size"),
19336 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19337 0/*allow-unknown*/, &maintenance_show_cmdlist);
19338
19339 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19340 &dwarf2_max_cache_age, _("\
19341 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19342 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19343 A higher limit means that cached compilation units will be stored\n\
19344 in memory longer, and more total memory will be used. Zero disables\n\
19345 caching, which can slow down startup."),
19346 NULL,
19347 show_dwarf2_max_cache_age,
19348 &set_dwarf2_cmdlist,
19349 &show_dwarf2_cmdlist);
19350
19351 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19352 &dwarf2_always_disassemble, _("\
19353 Set whether `info address' always disassembles DWARF expressions."), _("\
19354 Show whether `info address' always disassembles DWARF expressions."), _("\
19355 When enabled, DWARF expressions are always printed in an assembly-like\n\
19356 syntax. When disabled, expressions will be printed in a more\n\
19357 conversational style, when possible."),
19358 NULL,
19359 show_dwarf2_always_disassemble,
19360 &set_dwarf2_cmdlist,
19361 &show_dwarf2_cmdlist);
19362
19363 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19364 Set debugging of the dwarf2 reader."), _("\
19365 Show debugging of the dwarf2 reader."), _("\
19366 When enabled, debugging messages are printed during dwarf2 reading\n\
19367 and symtab expansion."),
19368 NULL,
19369 NULL,
19370 &setdebuglist, &showdebuglist);
19371
19372 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19373 Set debugging of the dwarf2 DIE reader."), _("\
19374 Show debugging of the dwarf2 DIE reader."), _("\
19375 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19376 The value is the maximum depth to print."),
19377 NULL,
19378 NULL,
19379 &setdebuglist, &showdebuglist);
19380
19381 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19382 Set cross-checking of \"physname\" code against demangler."), _("\
19383 Show cross-checking of \"physname\" code against demangler."), _("\
19384 When enabled, GDB's internal \"physname\" code is checked against\n\
19385 the demangler."),
19386 NULL, show_check_physname,
19387 &setdebuglist, &showdebuglist);
19388
19389 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19390 _("\
19391 Save a gdb-index file.\n\
19392 Usage: save gdb-index DIRECTORY"),
19393 &save_cmdlist);
19394 set_cmd_completer (c, filename_completer);
19395 }