* dwarf2read.c: Don't include zlib.h or sys/mman.h.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67 #include "gdb_bfd.h"
68
69 #include <fcntl.h>
70 #include "gdb_string.h"
71 #include "gdb_assert.h"
72 #include <sys/types.h>
73
74 typedef struct symbol *symbolp;
75 DEF_VEC_P (symbolp);
76
77 /* When non-zero, print basic high level tracing messages.
78 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
79 static int dwarf2_read_debug = 0;
80
81 /* When non-zero, dump DIEs after they are read in. */
82 static int dwarf2_die_debug = 0;
83
84 /* When non-zero, cross-check physname against demangler. */
85 static int check_physname = 0;
86
87 /* When non-zero, do not reject deprecated .gdb_index sections. */
88 int use_deprecated_index_sections = 0;
89
90 /* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94 static int processing_has_namespace_info;
95
96 static const struct objfile_data *dwarf2_objfile_data_key;
97
98 struct dwarf2_section_info
99 {
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
103 /* True if we have tried to read this section. */
104 int readin;
105 };
106
107 typedef struct dwarf2_section_info dwarf2_section_info_def;
108 DEF_VEC_O (dwarf2_section_info_def);
109
110 /* All offsets in the index are of this type. It must be
111 architecture-independent. */
112 typedef uint32_t offset_type;
113
114 DEF_VEC_I (offset_type);
115
116 /* Ensure only legit values are used. */
117 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
118 do { \
119 gdb_assert ((unsigned int) (value) <= 1); \
120 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
121 } while (0)
122
123 /* Ensure only legit values are used. */
124 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
125 do { \
126 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
127 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
128 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
129 } while (0)
130
131 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
132 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
133 do { \
134 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
135 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
136 } while (0)
137
138 /* A description of the mapped index. The file format is described in
139 a comment by the code that writes the index. */
140 struct mapped_index
141 {
142 /* Index data format version. */
143 int version;
144
145 /* The total length of the buffer. */
146 off_t total_size;
147
148 /* A pointer to the address table data. */
149 const gdb_byte *address_table;
150
151 /* Size of the address table data in bytes. */
152 offset_type address_table_size;
153
154 /* The symbol table, implemented as a hash table. */
155 const offset_type *symbol_table;
156
157 /* Size in slots, each slot is 2 offset_types. */
158 offset_type symbol_table_slots;
159
160 /* A pointer to the constant pool. */
161 const char *constant_pool;
162 };
163
164 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
165 DEF_VEC_P (dwarf2_per_cu_ptr);
166
167 /* Collection of data recorded per objfile.
168 This hangs off of dwarf2_objfile_data_key. */
169
170 struct dwarf2_per_objfile
171 {
172 struct dwarf2_section_info info;
173 struct dwarf2_section_info abbrev;
174 struct dwarf2_section_info line;
175 struct dwarf2_section_info loc;
176 struct dwarf2_section_info macinfo;
177 struct dwarf2_section_info macro;
178 struct dwarf2_section_info str;
179 struct dwarf2_section_info ranges;
180 struct dwarf2_section_info addr;
181 struct dwarf2_section_info frame;
182 struct dwarf2_section_info eh_frame;
183 struct dwarf2_section_info gdb_index;
184
185 VEC (dwarf2_section_info_def) *types;
186
187 /* Back link. */
188 struct objfile *objfile;
189
190 /* Table of all the compilation units. This is used to locate
191 the target compilation unit of a particular reference. */
192 struct dwarf2_per_cu_data **all_comp_units;
193
194 /* The number of compilation units in ALL_COMP_UNITS. */
195 int n_comp_units;
196
197 /* The number of .debug_types-related CUs. */
198 int n_type_units;
199
200 /* The .debug_types-related CUs (TUs). */
201 struct signatured_type **all_type_units;
202
203 /* The number of entries in all_type_unit_groups. */
204 int n_type_unit_groups;
205
206 /* Table of type unit groups.
207 This exists to make it easy to iterate over all CUs and TU groups. */
208 struct type_unit_group **all_type_unit_groups;
209
210 /* Table of struct type_unit_group objects.
211 The hash key is the DW_AT_stmt_list value. */
212 htab_t type_unit_groups;
213
214 /* A table mapping .debug_types signatures to its signatured_type entry.
215 This is NULL if the .debug_types section hasn't been read in yet. */
216 htab_t signatured_types;
217
218 /* Type unit statistics, to see how well the scaling improvements
219 are doing. */
220 struct tu_stats
221 {
222 int nr_uniq_abbrev_tables;
223 int nr_symtabs;
224 int nr_symtab_sharers;
225 int nr_stmt_less_type_units;
226 } tu_stats;
227
228 /* A chain of compilation units that are currently read in, so that
229 they can be freed later. */
230 struct dwarf2_per_cu_data *read_in_chain;
231
232 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
233 This is NULL if the table hasn't been allocated yet. */
234 htab_t dwo_files;
235
236 /* A flag indicating wether this objfile has a section loaded at a
237 VMA of 0. */
238 int has_section_at_zero;
239
240 /* True if we are using the mapped index,
241 or we are faking it for OBJF_READNOW's sake. */
242 unsigned char using_index;
243
244 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
245 struct mapped_index *index_table;
246
247 /* When using index_table, this keeps track of all quick_file_names entries.
248 TUs can share line table entries with CUs or other TUs, and there can be
249 a lot more TUs than unique line tables, so we maintain a separate table
250 of all line table entries to support the sharing. */
251 htab_t quick_file_names_table;
252
253 /* Set during partial symbol reading, to prevent queueing of full
254 symbols. */
255 int reading_partial_symbols;
256
257 /* Table mapping type DIEs to their struct type *.
258 This is NULL if not allocated yet.
259 The mapping is done via (CU/TU signature + DIE offset) -> type. */
260 htab_t die_type_hash;
261
262 /* The CUs we recently read. */
263 VEC (dwarf2_per_cu_ptr) *just_read_cus;
264 };
265
266 static struct dwarf2_per_objfile *dwarf2_per_objfile;
267
268 /* Default names of the debugging sections. */
269
270 /* Note that if the debugging section has been compressed, it might
271 have a name like .zdebug_info. */
272
273 static const struct dwarf2_debug_sections dwarf2_elf_names =
274 {
275 { ".debug_info", ".zdebug_info" },
276 { ".debug_abbrev", ".zdebug_abbrev" },
277 { ".debug_line", ".zdebug_line" },
278 { ".debug_loc", ".zdebug_loc" },
279 { ".debug_macinfo", ".zdebug_macinfo" },
280 { ".debug_macro", ".zdebug_macro" },
281 { ".debug_str", ".zdebug_str" },
282 { ".debug_ranges", ".zdebug_ranges" },
283 { ".debug_types", ".zdebug_types" },
284 { ".debug_addr", ".zdebug_addr" },
285 { ".debug_frame", ".zdebug_frame" },
286 { ".eh_frame", NULL },
287 { ".gdb_index", ".zgdb_index" },
288 23
289 };
290
291 /* List of DWO sections. */
292
293 static const struct dwo_section_names
294 {
295 struct dwarf2_section_names abbrev_dwo;
296 struct dwarf2_section_names info_dwo;
297 struct dwarf2_section_names line_dwo;
298 struct dwarf2_section_names loc_dwo;
299 struct dwarf2_section_names macinfo_dwo;
300 struct dwarf2_section_names macro_dwo;
301 struct dwarf2_section_names str_dwo;
302 struct dwarf2_section_names str_offsets_dwo;
303 struct dwarf2_section_names types_dwo;
304 }
305 dwo_section_names =
306 {
307 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
308 { ".debug_info.dwo", ".zdebug_info.dwo" },
309 { ".debug_line.dwo", ".zdebug_line.dwo" },
310 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
311 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
312 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
313 { ".debug_str.dwo", ".zdebug_str.dwo" },
314 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
315 { ".debug_types.dwo", ".zdebug_types.dwo" },
316 };
317
318 /* local data types */
319
320 /* The data in a compilation unit header, after target2host
321 translation, looks like this. */
322 struct comp_unit_head
323 {
324 unsigned int length;
325 short version;
326 unsigned char addr_size;
327 unsigned char signed_addr_p;
328 sect_offset abbrev_offset;
329
330 /* Size of file offsets; either 4 or 8. */
331 unsigned int offset_size;
332
333 /* Size of the length field; either 4 or 12. */
334 unsigned int initial_length_size;
335
336 /* Offset to the first byte of this compilation unit header in the
337 .debug_info section, for resolving relative reference dies. */
338 sect_offset offset;
339
340 /* Offset to first die in this cu from the start of the cu.
341 This will be the first byte following the compilation unit header. */
342 cu_offset first_die_offset;
343 };
344
345 /* Type used for delaying computation of method physnames.
346 See comments for compute_delayed_physnames. */
347 struct delayed_method_info
348 {
349 /* The type to which the method is attached, i.e., its parent class. */
350 struct type *type;
351
352 /* The index of the method in the type's function fieldlists. */
353 int fnfield_index;
354
355 /* The index of the method in the fieldlist. */
356 int index;
357
358 /* The name of the DIE. */
359 const char *name;
360
361 /* The DIE associated with this method. */
362 struct die_info *die;
363 };
364
365 typedef struct delayed_method_info delayed_method_info;
366 DEF_VEC_O (delayed_method_info);
367
368 /* Internal state when decoding a particular compilation unit. */
369 struct dwarf2_cu
370 {
371 /* The objfile containing this compilation unit. */
372 struct objfile *objfile;
373
374 /* The header of the compilation unit. */
375 struct comp_unit_head header;
376
377 /* Base address of this compilation unit. */
378 CORE_ADDR base_address;
379
380 /* Non-zero if base_address has been set. */
381 int base_known;
382
383 /* The language we are debugging. */
384 enum language language;
385 const struct language_defn *language_defn;
386
387 const char *producer;
388
389 /* The generic symbol table building routines have separate lists for
390 file scope symbols and all all other scopes (local scopes). So
391 we need to select the right one to pass to add_symbol_to_list().
392 We do it by keeping a pointer to the correct list in list_in_scope.
393
394 FIXME: The original dwarf code just treated the file scope as the
395 first local scope, and all other local scopes as nested local
396 scopes, and worked fine. Check to see if we really need to
397 distinguish these in buildsym.c. */
398 struct pending **list_in_scope;
399
400 /* The abbrev table for this CU.
401 Normally this points to the abbrev table in the objfile.
402 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
403 struct abbrev_table *abbrev_table;
404
405 /* Hash table holding all the loaded partial DIEs
406 with partial_die->offset.SECT_OFF as hash. */
407 htab_t partial_dies;
408
409 /* Storage for things with the same lifetime as this read-in compilation
410 unit, including partial DIEs. */
411 struct obstack comp_unit_obstack;
412
413 /* When multiple dwarf2_cu structures are living in memory, this field
414 chains them all together, so that they can be released efficiently.
415 We will probably also want a generation counter so that most-recently-used
416 compilation units are cached... */
417 struct dwarf2_per_cu_data *read_in_chain;
418
419 /* Backchain to our per_cu entry if the tree has been built. */
420 struct dwarf2_per_cu_data *per_cu;
421
422 /* How many compilation units ago was this CU last referenced? */
423 int last_used;
424
425 /* A hash table of DIE cu_offset for following references with
426 die_info->offset.sect_off as hash. */
427 htab_t die_hash;
428
429 /* Full DIEs if read in. */
430 struct die_info *dies;
431
432 /* A set of pointers to dwarf2_per_cu_data objects for compilation
433 units referenced by this one. Only set during full symbol processing;
434 partial symbol tables do not have dependencies. */
435 htab_t dependencies;
436
437 /* Header data from the line table, during full symbol processing. */
438 struct line_header *line_header;
439
440 /* A list of methods which need to have physnames computed
441 after all type information has been read. */
442 VEC (delayed_method_info) *method_list;
443
444 /* To be copied to symtab->call_site_htab. */
445 htab_t call_site_htab;
446
447 /* Non-NULL if this CU came from a DWO file.
448 There is an invariant here that is important to remember:
449 Except for attributes copied from the top level DIE in the "main"
450 (or "stub") file in preparation for reading the DWO file
451 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
452 Either there isn't a DWO file (in which case this is NULL and the point
453 is moot), or there is and either we're not going to read it (in which
454 case this is NULL) or there is and we are reading it (in which case this
455 is non-NULL). */
456 struct dwo_unit *dwo_unit;
457
458 /* The DW_AT_addr_base attribute if present, zero otherwise
459 (zero is a valid value though).
460 Note this value comes from the stub CU/TU's DIE. */
461 ULONGEST addr_base;
462
463 /* The DW_AT_ranges_base attribute if present, zero otherwise
464 (zero is a valid value though).
465 Note this value comes from the stub CU/TU's DIE.
466 Also note that the value is zero in the non-DWO case so this value can
467 be used without needing to know whether DWO files are in use or not. */
468 ULONGEST ranges_base;
469
470 /* Mark used when releasing cached dies. */
471 unsigned int mark : 1;
472
473 /* This CU references .debug_loc. See the symtab->locations_valid field.
474 This test is imperfect as there may exist optimized debug code not using
475 any location list and still facing inlining issues if handled as
476 unoptimized code. For a future better test see GCC PR other/32998. */
477 unsigned int has_loclist : 1;
478
479 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
480 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
481 are valid. This information is cached because profiling CU expansion
482 showed excessive time spent in producer_is_gxx_lt_4_6. */
483 unsigned int checked_producer : 1;
484 unsigned int producer_is_gxx_lt_4_6 : 1;
485 unsigned int producer_is_icc : 1;
486 };
487
488 /* Persistent data held for a compilation unit, even when not
489 processing it. We put a pointer to this structure in the
490 read_symtab_private field of the psymtab. */
491
492 struct dwarf2_per_cu_data
493 {
494 /* The start offset and length of this compilation unit. 2**29-1
495 bytes should suffice to store the length of any compilation unit
496 - if it doesn't, GDB will fall over anyway.
497 NOTE: Unlike comp_unit_head.length, this length includes
498 initial_length_size.
499 If the DIE refers to a DWO file, this is always of the original die,
500 not the DWO file. */
501 sect_offset offset;
502 unsigned int length : 29;
503
504 /* Flag indicating this compilation unit will be read in before
505 any of the current compilation units are processed. */
506 unsigned int queued : 1;
507
508 /* This flag will be set when reading partial DIEs if we need to load
509 absolutely all DIEs for this compilation unit, instead of just the ones
510 we think are interesting. It gets set if we look for a DIE in the
511 hash table and don't find it. */
512 unsigned int load_all_dies : 1;
513
514 /* Non-zero if this CU is from .debug_types. */
515 unsigned int is_debug_types : 1;
516
517 /* The section this CU/TU lives in.
518 If the DIE refers to a DWO file, this is always the original die,
519 not the DWO file. */
520 struct dwarf2_section_info *info_or_types_section;
521
522 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
523 of the CU cache it gets reset to NULL again. */
524 struct dwarf2_cu *cu;
525
526 /* The corresponding objfile.
527 Normally we can get the objfile from dwarf2_per_objfile.
528 However we can enter this file with just a "per_cu" handle. */
529 struct objfile *objfile;
530
531 /* When using partial symbol tables, the 'psymtab' field is active.
532 Otherwise the 'quick' field is active. */
533 union
534 {
535 /* The partial symbol table associated with this compilation unit,
536 or NULL for unread partial units. */
537 struct partial_symtab *psymtab;
538
539 /* Data needed by the "quick" functions. */
540 struct dwarf2_per_cu_quick_data *quick;
541 } v;
542
543 union
544 {
545 /* The CUs we import using DW_TAG_imported_unit. This is filled in
546 while reading psymtabs, used to compute the psymtab dependencies,
547 and then cleared. Then it is filled in again while reading full
548 symbols, and only deleted when the objfile is destroyed. */
549 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
550
551 /* Type units are grouped by their DW_AT_stmt_list entry so that they
552 can share them. If this is a TU, this points to the containing
553 symtab. */
554 struct type_unit_group *type_unit_group;
555 } s;
556 };
557
558 /* Entry in the signatured_types hash table. */
559
560 struct signatured_type
561 {
562 /* The "per_cu" object of this type.
563 N.B.: This is the first member so that it's easy to convert pointers
564 between them. */
565 struct dwarf2_per_cu_data per_cu;
566
567 /* The type's signature. */
568 ULONGEST signature;
569
570 /* Offset in the TU of the type's DIE, as read from the TU header.
571 If the definition lives in a DWO file, this value is unusable. */
572 cu_offset type_offset_in_tu;
573
574 /* Offset in the section of the type's DIE.
575 If the definition lives in a DWO file, this is the offset in the
576 .debug_types.dwo section.
577 The value is zero until the actual value is known.
578 Zero is otherwise not a valid section offset. */
579 sect_offset type_offset_in_section;
580 };
581
582 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
584
585 struct stmt_list_hash
586 {
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
589
590 /* Offset in .debug_line or .debug_line.dwo. */
591 sect_offset line_offset;
592 };
593
594 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597 struct type_unit_group
598 {
599 /* dwarf2read.c's main "handle" on the symtab.
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
604 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
605 struct dwarf2_per_cu_data per_cu;
606
607 union
608 {
609 /* The TUs that share this DW_AT_stmt_list entry.
610 This is added to while parsing type units to build partial symtabs,
611 and is deleted afterwards and not used again. */
612 VEC (dwarf2_per_cu_ptr) *tus;
613
614 /* When reading the line table in "quick" functions, we need a real TU.
615 Any will do, we know they all share the same DW_AT_stmt_list entry.
616 For simplicity's sake, we pick the first one. */
617 struct dwarf2_per_cu_data *first_tu;
618 } t;
619
620 /* The primary symtab.
621 Type units in a group needn't all be defined in the same source file,
622 so we create an essentially anonymous symtab as the primary symtab. */
623 struct symtab *primary_symtab;
624
625 /* The data used to construct the hash key. */
626 struct stmt_list_hash hash;
627
628 /* The number of symtabs from the line header.
629 The value here must match line_header.num_file_names. */
630 unsigned int num_symtabs;
631
632 /* The symbol tables for this TU (obtained from the files listed in
633 DW_AT_stmt_list).
634 WARNING: The order of entries here must match the order of entries
635 in the line header. After the first TU using this type_unit_group, the
636 line header for the subsequent TUs is recreated from this. This is done
637 because we need to use the same symtabs for each TU using the same
638 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
639 there's no guarantee the line header doesn't have duplicate entries. */
640 struct symtab **symtabs;
641 };
642
643 /* These sections are what may appear in a "dwo" file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info info;
649 struct dwarf2_section_info line;
650 struct dwarf2_section_info loc;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 VEC (dwarf2_section_info_def) *types;
656 };
657
658 /* Common bits of DWO CUs/TUs. */
659
660 struct dwo_unit
661 {
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *info_or_types_section;
672
673 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
674 sect_offset offset;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679 };
680
681 /* Data for one DWO file. */
682
683 struct dwo_file
684 {
685 /* The DW_AT_GNU_dwo_name attribute.
686 We don't manage space for this, it's an attribute. */
687 const char *dwo_name;
688
689 /* The bfd, when the file is open. Otherwise this is NULL. */
690 bfd *dwo_bfd;
691
692 /* Section info for this file. */
693 struct dwo_sections sections;
694
695 /* Table of CUs in the file.
696 Each element is a struct dwo_unit. */
697 htab_t cus;
698
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
701 htab_t tus;
702 };
703
704 /* Struct used to pass misc. parameters to read_die_and_children, et
705 al. which are used for both .debug_info and .debug_types dies.
706 All parameters here are unchanging for the life of the call. This
707 struct exists to abstract away the constant parameters of die reading. */
708
709 struct die_reader_specs
710 {
711 /* die_section->asection->owner. */
712 bfd* abfd;
713
714 /* The CU of the DIE we are parsing. */
715 struct dwarf2_cu *cu;
716
717 /* Non-NULL if reading a DWO file. */
718 struct dwo_file *dwo_file;
719
720 /* The section the die comes from.
721 This is either .debug_info or .debug_types, or the .dwo variants. */
722 struct dwarf2_section_info *die_section;
723
724 /* die_section->buffer. */
725 gdb_byte *buffer;
726
727 /* The end of the buffer. */
728 const gdb_byte *buffer_end;
729 };
730
731 /* Type of function passed to init_cutu_and_read_dies, et.al. */
732 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
733 gdb_byte *info_ptr,
734 struct die_info *comp_unit_die,
735 int has_children,
736 void *data);
737
738 /* The line number information for a compilation unit (found in the
739 .debug_line section) begins with a "statement program header",
740 which contains the following information. */
741 struct line_header
742 {
743 unsigned int total_length;
744 unsigned short version;
745 unsigned int header_length;
746 unsigned char minimum_instruction_length;
747 unsigned char maximum_ops_per_instruction;
748 unsigned char default_is_stmt;
749 int line_base;
750 unsigned char line_range;
751 unsigned char opcode_base;
752
753 /* standard_opcode_lengths[i] is the number of operands for the
754 standard opcode whose value is i. This means that
755 standard_opcode_lengths[0] is unused, and the last meaningful
756 element is standard_opcode_lengths[opcode_base - 1]. */
757 unsigned char *standard_opcode_lengths;
758
759 /* The include_directories table. NOTE! These strings are not
760 allocated with xmalloc; instead, they are pointers into
761 debug_line_buffer. If you try to free them, `free' will get
762 indigestion. */
763 unsigned int num_include_dirs, include_dirs_size;
764 char **include_dirs;
765
766 /* The file_names table. NOTE! These strings are not allocated
767 with xmalloc; instead, they are pointers into debug_line_buffer.
768 Don't try to free them directly. */
769 unsigned int num_file_names, file_names_size;
770 struct file_entry
771 {
772 char *name;
773 unsigned int dir_index;
774 unsigned int mod_time;
775 unsigned int length;
776 int included_p; /* Non-zero if referenced by the Line Number Program. */
777 struct symtab *symtab; /* The associated symbol table, if any. */
778 } *file_names;
779
780 /* The start and end of the statement program following this
781 header. These point into dwarf2_per_objfile->line_buffer. */
782 gdb_byte *statement_program_start, *statement_program_end;
783 };
784
785 /* When we construct a partial symbol table entry we only
786 need this much information. */
787 struct partial_die_info
788 {
789 /* Offset of this DIE. */
790 sect_offset offset;
791
792 /* DWARF-2 tag for this DIE. */
793 ENUM_BITFIELD(dwarf_tag) tag : 16;
794
795 /* Assorted flags describing the data found in this DIE. */
796 unsigned int has_children : 1;
797 unsigned int is_external : 1;
798 unsigned int is_declaration : 1;
799 unsigned int has_type : 1;
800 unsigned int has_specification : 1;
801 unsigned int has_pc_info : 1;
802 unsigned int may_be_inlined : 1;
803
804 /* Flag set if the SCOPE field of this structure has been
805 computed. */
806 unsigned int scope_set : 1;
807
808 /* Flag set if the DIE has a byte_size attribute. */
809 unsigned int has_byte_size : 1;
810
811 /* Flag set if any of the DIE's children are template arguments. */
812 unsigned int has_template_arguments : 1;
813
814 /* Flag set if fixup_partial_die has been called on this die. */
815 unsigned int fixup_called : 1;
816
817 /* The name of this DIE. Normally the value of DW_AT_name, but
818 sometimes a default name for unnamed DIEs. */
819 char *name;
820
821 /* The linkage name, if present. */
822 const char *linkage_name;
823
824 /* The scope to prepend to our children. This is generally
825 allocated on the comp_unit_obstack, so will disappear
826 when this compilation unit leaves the cache. */
827 char *scope;
828
829 /* Some data associated with the partial DIE. The tag determines
830 which field is live. */
831 union
832 {
833 /* The location description associated with this DIE, if any. */
834 struct dwarf_block *locdesc;
835 /* The offset of an import, for DW_TAG_imported_unit. */
836 sect_offset offset;
837 } d;
838
839 /* If HAS_PC_INFO, the PC range associated with this DIE. */
840 CORE_ADDR lowpc;
841 CORE_ADDR highpc;
842
843 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
844 DW_AT_sibling, if any. */
845 /* NOTE: This member isn't strictly necessary, read_partial_die could
846 return DW_AT_sibling values to its caller load_partial_dies. */
847 gdb_byte *sibling;
848
849 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
850 DW_AT_specification (or DW_AT_abstract_origin or
851 DW_AT_extension). */
852 sect_offset spec_offset;
853
854 /* Pointers to this DIE's parent, first child, and next sibling,
855 if any. */
856 struct partial_die_info *die_parent, *die_child, *die_sibling;
857 };
858
859 /* This data structure holds the information of an abbrev. */
860 struct abbrev_info
861 {
862 unsigned int number; /* number identifying abbrev */
863 enum dwarf_tag tag; /* dwarf tag */
864 unsigned short has_children; /* boolean */
865 unsigned short num_attrs; /* number of attributes */
866 struct attr_abbrev *attrs; /* an array of attribute descriptions */
867 struct abbrev_info *next; /* next in chain */
868 };
869
870 struct attr_abbrev
871 {
872 ENUM_BITFIELD(dwarf_attribute) name : 16;
873 ENUM_BITFIELD(dwarf_form) form : 16;
874 };
875
876 /* Size of abbrev_table.abbrev_hash_table. */
877 #define ABBREV_HASH_SIZE 121
878
879 /* Top level data structure to contain an abbreviation table. */
880
881 struct abbrev_table
882 {
883 /* Where the abbrev table came from.
884 This is used as a sanity check when the table is used. */
885 sect_offset offset;
886
887 /* Storage for the abbrev table. */
888 struct obstack abbrev_obstack;
889
890 /* Hash table of abbrevs.
891 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
892 It could be statically allocated, but the previous code didn't so we
893 don't either. */
894 struct abbrev_info **abbrevs;
895 };
896
897 /* Attributes have a name and a value. */
898 struct attribute
899 {
900 ENUM_BITFIELD(dwarf_attribute) name : 16;
901 ENUM_BITFIELD(dwarf_form) form : 15;
902
903 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
904 field should be in u.str (existing only for DW_STRING) but it is kept
905 here for better struct attribute alignment. */
906 unsigned int string_is_canonical : 1;
907
908 union
909 {
910 char *str;
911 struct dwarf_block *blk;
912 ULONGEST unsnd;
913 LONGEST snd;
914 CORE_ADDR addr;
915 struct signatured_type *signatured_type;
916 }
917 u;
918 };
919
920 /* This data structure holds a complete die structure. */
921 struct die_info
922 {
923 /* DWARF-2 tag for this DIE. */
924 ENUM_BITFIELD(dwarf_tag) tag : 16;
925
926 /* Number of attributes */
927 unsigned char num_attrs;
928
929 /* True if we're presently building the full type name for the
930 type derived from this DIE. */
931 unsigned char building_fullname : 1;
932
933 /* Abbrev number */
934 unsigned int abbrev;
935
936 /* Offset in .debug_info or .debug_types section. */
937 sect_offset offset;
938
939 /* The dies in a compilation unit form an n-ary tree. PARENT
940 points to this die's parent; CHILD points to the first child of
941 this node; and all the children of a given node are chained
942 together via their SIBLING fields. */
943 struct die_info *child; /* Its first child, if any. */
944 struct die_info *sibling; /* Its next sibling, if any. */
945 struct die_info *parent; /* Its parent, if any. */
946
947 /* An array of attributes, with NUM_ATTRS elements. There may be
948 zero, but it's not common and zero-sized arrays are not
949 sufficiently portable C. */
950 struct attribute attrs[1];
951 };
952
953 /* Get at parts of an attribute structure. */
954
955 #define DW_STRING(attr) ((attr)->u.str)
956 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
957 #define DW_UNSND(attr) ((attr)->u.unsnd)
958 #define DW_BLOCK(attr) ((attr)->u.blk)
959 #define DW_SND(attr) ((attr)->u.snd)
960 #define DW_ADDR(attr) ((attr)->u.addr)
961 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
962
963 /* Blocks are a bunch of untyped bytes. */
964 struct dwarf_block
965 {
966 unsigned int size;
967
968 /* Valid only if SIZE is not zero. */
969 gdb_byte *data;
970 };
971
972 #ifndef ATTR_ALLOC_CHUNK
973 #define ATTR_ALLOC_CHUNK 4
974 #endif
975
976 /* Allocate fields for structs, unions and enums in this size. */
977 #ifndef DW_FIELD_ALLOC_CHUNK
978 #define DW_FIELD_ALLOC_CHUNK 4
979 #endif
980
981 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
982 but this would require a corresponding change in unpack_field_as_long
983 and friends. */
984 static int bits_per_byte = 8;
985
986 /* The routines that read and process dies for a C struct or C++ class
987 pass lists of data member fields and lists of member function fields
988 in an instance of a field_info structure, as defined below. */
989 struct field_info
990 {
991 /* List of data member and baseclasses fields. */
992 struct nextfield
993 {
994 struct nextfield *next;
995 int accessibility;
996 int virtuality;
997 struct field field;
998 }
999 *fields, *baseclasses;
1000
1001 /* Number of fields (including baseclasses). */
1002 int nfields;
1003
1004 /* Number of baseclasses. */
1005 int nbaseclasses;
1006
1007 /* Set if the accesibility of one of the fields is not public. */
1008 int non_public_fields;
1009
1010 /* Member function fields array, entries are allocated in the order they
1011 are encountered in the object file. */
1012 struct nextfnfield
1013 {
1014 struct nextfnfield *next;
1015 struct fn_field fnfield;
1016 }
1017 *fnfields;
1018
1019 /* Member function fieldlist array, contains name of possibly overloaded
1020 member function, number of overloaded member functions and a pointer
1021 to the head of the member function field chain. */
1022 struct fnfieldlist
1023 {
1024 char *name;
1025 int length;
1026 struct nextfnfield *head;
1027 }
1028 *fnfieldlists;
1029
1030 /* Number of entries in the fnfieldlists array. */
1031 int nfnfields;
1032
1033 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1034 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1035 struct typedef_field_list
1036 {
1037 struct typedef_field field;
1038 struct typedef_field_list *next;
1039 }
1040 *typedef_field_list;
1041 unsigned typedef_field_list_count;
1042 };
1043
1044 /* One item on the queue of compilation units to read in full symbols
1045 for. */
1046 struct dwarf2_queue_item
1047 {
1048 struct dwarf2_per_cu_data *per_cu;
1049 enum language pretend_language;
1050 struct dwarf2_queue_item *next;
1051 };
1052
1053 /* The current queue. */
1054 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1055
1056 /* Loaded secondary compilation units are kept in memory until they
1057 have not been referenced for the processing of this many
1058 compilation units. Set this to zero to disable caching. Cache
1059 sizes of up to at least twenty will improve startup time for
1060 typical inter-CU-reference binaries, at an obvious memory cost. */
1061 static int dwarf2_max_cache_age = 5;
1062 static void
1063 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1064 struct cmd_list_element *c, const char *value)
1065 {
1066 fprintf_filtered (file, _("The upper bound on the age of cached "
1067 "dwarf2 compilation units is %s.\n"),
1068 value);
1069 }
1070
1071
1072 /* Various complaints about symbol reading that don't abort the process. */
1073
1074 static void
1075 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1076 {
1077 complaint (&symfile_complaints,
1078 _("statement list doesn't fit in .debug_line section"));
1079 }
1080
1081 static void
1082 dwarf2_debug_line_missing_file_complaint (void)
1083 {
1084 complaint (&symfile_complaints,
1085 _(".debug_line section has line data without a file"));
1086 }
1087
1088 static void
1089 dwarf2_debug_line_missing_end_sequence_complaint (void)
1090 {
1091 complaint (&symfile_complaints,
1092 _(".debug_line section has line "
1093 "program sequence without an end"));
1094 }
1095
1096 static void
1097 dwarf2_complex_location_expr_complaint (void)
1098 {
1099 complaint (&symfile_complaints, _("location expression too complex"));
1100 }
1101
1102 static void
1103 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1104 int arg3)
1105 {
1106 complaint (&symfile_complaints,
1107 _("const value length mismatch for '%s', got %d, expected %d"),
1108 arg1, arg2, arg3);
1109 }
1110
1111 static void
1112 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1113 {
1114 complaint (&symfile_complaints,
1115 _("debug info runs off end of %s section"
1116 " [in module %s]"),
1117 section->asection->name,
1118 bfd_get_filename (section->asection->owner));
1119 }
1120
1121 static void
1122 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1123 {
1124 complaint (&symfile_complaints,
1125 _("macro debug info contains a "
1126 "malformed macro definition:\n`%s'"),
1127 arg1);
1128 }
1129
1130 static void
1131 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1132 {
1133 complaint (&symfile_complaints,
1134 _("invalid attribute class or form for '%s' in '%s'"),
1135 arg1, arg2);
1136 }
1137
1138 /* local function prototypes */
1139
1140 static void dwarf2_locate_sections (bfd *, asection *, void *);
1141
1142 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1143 struct objfile *);
1144
1145 static void dwarf2_find_base_address (struct die_info *die,
1146 struct dwarf2_cu *cu);
1147
1148 static void dwarf2_build_psymtabs_hard (struct objfile *);
1149
1150 static void scan_partial_symbols (struct partial_die_info *,
1151 CORE_ADDR *, CORE_ADDR *,
1152 int, struct dwarf2_cu *);
1153
1154 static void add_partial_symbol (struct partial_die_info *,
1155 struct dwarf2_cu *);
1156
1157 static void add_partial_namespace (struct partial_die_info *pdi,
1158 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1159 int need_pc, struct dwarf2_cu *cu);
1160
1161 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1162 CORE_ADDR *highpc, int need_pc,
1163 struct dwarf2_cu *cu);
1164
1165 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1166 struct dwarf2_cu *cu);
1167
1168 static void add_partial_subprogram (struct partial_die_info *pdi,
1169 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1170 int need_pc, struct dwarf2_cu *cu);
1171
1172 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1173
1174 static void psymtab_to_symtab_1 (struct partial_symtab *);
1175
1176 static struct abbrev_info *abbrev_table_lookup_abbrev
1177 (const struct abbrev_table *, unsigned int);
1178
1179 static struct abbrev_table *abbrev_table_read_table
1180 (struct dwarf2_section_info *, sect_offset);
1181
1182 static void abbrev_table_free (struct abbrev_table *);
1183
1184 static void abbrev_table_free_cleanup (void *);
1185
1186 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1187 struct dwarf2_section_info *);
1188
1189 static void dwarf2_free_abbrev_table (void *);
1190
1191 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1192
1193 static struct partial_die_info *load_partial_dies
1194 (const struct die_reader_specs *, gdb_byte *, int);
1195
1196 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1197 struct partial_die_info *,
1198 struct abbrev_info *,
1199 unsigned int,
1200 gdb_byte *);
1201
1202 static struct partial_die_info *find_partial_die (sect_offset,
1203 struct dwarf2_cu *);
1204
1205 static void fixup_partial_die (struct partial_die_info *,
1206 struct dwarf2_cu *);
1207
1208 static gdb_byte *read_attribute (const struct die_reader_specs *,
1209 struct attribute *, struct attr_abbrev *,
1210 gdb_byte *);
1211
1212 static unsigned int read_1_byte (bfd *, gdb_byte *);
1213
1214 static int read_1_signed_byte (bfd *, gdb_byte *);
1215
1216 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1217
1218 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1219
1220 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1221
1222 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1223 unsigned int *);
1224
1225 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1226
1227 static LONGEST read_checked_initial_length_and_offset
1228 (bfd *, gdb_byte *, const struct comp_unit_head *,
1229 unsigned int *, unsigned int *);
1230
1231 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1232 unsigned int *);
1233
1234 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1235
1236 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1237 sect_offset);
1238
1239 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1240
1241 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1242
1243 static char *read_indirect_string (bfd *, gdb_byte *,
1244 const struct comp_unit_head *,
1245 unsigned int *);
1246
1247 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1248
1249 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1250
1251 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1252 unsigned int *);
1253
1254 static char *read_str_index (const struct die_reader_specs *reader,
1255 struct dwarf2_cu *cu, ULONGEST str_index);
1256
1257 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1258
1259 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1260 struct dwarf2_cu *);
1261
1262 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1263 unsigned int);
1264
1265 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1266 struct dwarf2_cu *cu);
1267
1268 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1269
1270 static struct die_info *die_specification (struct die_info *die,
1271 struct dwarf2_cu **);
1272
1273 static void free_line_header (struct line_header *lh);
1274
1275 static void add_file_name (struct line_header *, char *, unsigned int,
1276 unsigned int, unsigned int);
1277
1278 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1279 struct dwarf2_cu *cu);
1280
1281 static void dwarf_decode_lines (struct line_header *, const char *,
1282 struct dwarf2_cu *, struct partial_symtab *,
1283 int);
1284
1285 static void dwarf2_start_subfile (char *, const char *, const char *);
1286
1287 static void dwarf2_start_symtab (struct dwarf2_cu *,
1288 char *, char *, CORE_ADDR);
1289
1290 static struct symbol *new_symbol (struct die_info *, struct type *,
1291 struct dwarf2_cu *);
1292
1293 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1294 struct dwarf2_cu *, struct symbol *);
1295
1296 static void dwarf2_const_value (struct attribute *, struct symbol *,
1297 struct dwarf2_cu *);
1298
1299 static void dwarf2_const_value_attr (struct attribute *attr,
1300 struct type *type,
1301 const char *name,
1302 struct obstack *obstack,
1303 struct dwarf2_cu *cu, LONGEST *value,
1304 gdb_byte **bytes,
1305 struct dwarf2_locexpr_baton **baton);
1306
1307 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1308
1309 static int need_gnat_info (struct dwarf2_cu *);
1310
1311 static struct type *die_descriptive_type (struct die_info *,
1312 struct dwarf2_cu *);
1313
1314 static void set_descriptive_type (struct type *, struct die_info *,
1315 struct dwarf2_cu *);
1316
1317 static struct type *die_containing_type (struct die_info *,
1318 struct dwarf2_cu *);
1319
1320 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1321 struct dwarf2_cu *);
1322
1323 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1324
1325 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1326
1327 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1328
1329 static char *typename_concat (struct obstack *obs, const char *prefix,
1330 const char *suffix, int physname,
1331 struct dwarf2_cu *cu);
1332
1333 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1334
1335 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1336
1337 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1338
1339 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1340
1341 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1342
1343 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1344 struct dwarf2_cu *, struct partial_symtab *);
1345
1346 static int dwarf2_get_pc_bounds (struct die_info *,
1347 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1348 struct partial_symtab *);
1349
1350 static void get_scope_pc_bounds (struct die_info *,
1351 CORE_ADDR *, CORE_ADDR *,
1352 struct dwarf2_cu *);
1353
1354 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1355 CORE_ADDR, struct dwarf2_cu *);
1356
1357 static void dwarf2_add_field (struct field_info *, struct die_info *,
1358 struct dwarf2_cu *);
1359
1360 static void dwarf2_attach_fields_to_type (struct field_info *,
1361 struct type *, struct dwarf2_cu *);
1362
1363 static void dwarf2_add_member_fn (struct field_info *,
1364 struct die_info *, struct type *,
1365 struct dwarf2_cu *);
1366
1367 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1368 struct type *,
1369 struct dwarf2_cu *);
1370
1371 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1372
1373 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1374
1375 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1376
1377 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1378
1379 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1380
1381 static struct type *read_module_type (struct die_info *die,
1382 struct dwarf2_cu *cu);
1383
1384 static const char *namespace_name (struct die_info *die,
1385 int *is_anonymous, struct dwarf2_cu *);
1386
1387 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1388
1389 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1390
1391 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1392 struct dwarf2_cu *);
1393
1394 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1395 gdb_byte *info_ptr,
1396 gdb_byte **new_info_ptr,
1397 struct die_info *parent);
1398
1399 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1400 gdb_byte *info_ptr,
1401 gdb_byte **new_info_ptr,
1402 struct die_info *parent);
1403
1404 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1405 struct die_info **, gdb_byte *, int *, int);
1406
1407 static gdb_byte *read_full_die (const struct die_reader_specs *,
1408 struct die_info **, gdb_byte *, int *);
1409
1410 static void process_die (struct die_info *, struct dwarf2_cu *);
1411
1412 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1413 struct obstack *);
1414
1415 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1416
1417 static const char *dwarf2_full_name (char *name,
1418 struct die_info *die,
1419 struct dwarf2_cu *cu);
1420
1421 static struct die_info *dwarf2_extension (struct die_info *die,
1422 struct dwarf2_cu **);
1423
1424 static const char *dwarf_tag_name (unsigned int);
1425
1426 static const char *dwarf_attr_name (unsigned int);
1427
1428 static const char *dwarf_form_name (unsigned int);
1429
1430 static char *dwarf_bool_name (unsigned int);
1431
1432 static const char *dwarf_type_encoding_name (unsigned int);
1433
1434 static struct die_info *sibling_die (struct die_info *);
1435
1436 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1437
1438 static void dump_die_for_error (struct die_info *);
1439
1440 static void dump_die_1 (struct ui_file *, int level, int max_level,
1441 struct die_info *);
1442
1443 /*static*/ void dump_die (struct die_info *, int max_level);
1444
1445 static void store_in_ref_table (struct die_info *,
1446 struct dwarf2_cu *);
1447
1448 static int is_ref_attr (struct attribute *);
1449
1450 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1451
1452 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1453
1454 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1455 struct attribute *,
1456 struct dwarf2_cu **);
1457
1458 static struct die_info *follow_die_ref (struct die_info *,
1459 struct attribute *,
1460 struct dwarf2_cu **);
1461
1462 static struct die_info *follow_die_sig (struct die_info *,
1463 struct attribute *,
1464 struct dwarf2_cu **);
1465
1466 static struct signatured_type *lookup_signatured_type_at_offset
1467 (struct objfile *objfile,
1468 struct dwarf2_section_info *section, sect_offset offset);
1469
1470 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1471
1472 static void read_signatured_type (struct signatured_type *);
1473
1474 static struct type_unit_group *get_type_unit_group
1475 (struct dwarf2_cu *, struct attribute *);
1476
1477 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1478
1479 /* memory allocation interface */
1480
1481 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1482
1483 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1484
1485 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1486 char *, int);
1487
1488 static int attr_form_is_block (struct attribute *);
1489
1490 static int attr_form_is_section_offset (struct attribute *);
1491
1492 static int attr_form_is_constant (struct attribute *);
1493
1494 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1495 struct dwarf2_loclist_baton *baton,
1496 struct attribute *attr);
1497
1498 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1499 struct symbol *sym,
1500 struct dwarf2_cu *cu);
1501
1502 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1503 gdb_byte *info_ptr,
1504 struct abbrev_info *abbrev);
1505
1506 static void free_stack_comp_unit (void *);
1507
1508 static hashval_t partial_die_hash (const void *item);
1509
1510 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1511
1512 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1513 (sect_offset offset, struct objfile *objfile);
1514
1515 static void init_one_comp_unit (struct dwarf2_cu *cu,
1516 struct dwarf2_per_cu_data *per_cu);
1517
1518 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1519 struct die_info *comp_unit_die,
1520 enum language pretend_language);
1521
1522 static void free_heap_comp_unit (void *);
1523
1524 static void free_cached_comp_units (void *);
1525
1526 static void age_cached_comp_units (void);
1527
1528 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1529
1530 static struct type *set_die_type (struct die_info *, struct type *,
1531 struct dwarf2_cu *);
1532
1533 static void create_all_comp_units (struct objfile *);
1534
1535 static int create_all_type_units (struct objfile *);
1536
1537 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1538 enum language);
1539
1540 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1541 enum language);
1542
1543 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1544 enum language);
1545
1546 static void dwarf2_add_dependence (struct dwarf2_cu *,
1547 struct dwarf2_per_cu_data *);
1548
1549 static void dwarf2_mark (struct dwarf2_cu *);
1550
1551 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1552
1553 static struct type *get_die_type_at_offset (sect_offset,
1554 struct dwarf2_per_cu_data *per_cu);
1555
1556 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1557
1558 static void dwarf2_release_queue (void *dummy);
1559
1560 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1561 enum language pretend_language);
1562
1563 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1564 struct dwarf2_per_cu_data *per_cu,
1565 enum language pretend_language);
1566
1567 static void process_queue (void);
1568
1569 static void find_file_and_directory (struct die_info *die,
1570 struct dwarf2_cu *cu,
1571 char **name, char **comp_dir);
1572
1573 static char *file_full_name (int file, struct line_header *lh,
1574 const char *comp_dir);
1575
1576 static void init_cutu_and_read_dies
1577 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1578 int use_existing_cu, int keep,
1579 die_reader_func_ftype *die_reader_func, void *data);
1580
1581 static void init_cutu_and_read_dies_simple
1582 (struct dwarf2_per_cu_data *this_cu,
1583 die_reader_func_ftype *die_reader_func, void *data);
1584
1585 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1586
1587 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1588
1589 static struct dwo_unit *lookup_dwo_comp_unit
1590 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1591
1592 static struct dwo_unit *lookup_dwo_type_unit
1593 (struct signatured_type *, char *, const char *);
1594
1595 static void free_dwo_file_cleanup (void *);
1596
1597 static void process_cu_includes (void);
1598
1599 #if WORDS_BIGENDIAN
1600
1601 /* Convert VALUE between big- and little-endian. */
1602 static offset_type
1603 byte_swap (offset_type value)
1604 {
1605 offset_type result;
1606
1607 result = (value & 0xff) << 24;
1608 result |= (value & 0xff00) << 8;
1609 result |= (value & 0xff0000) >> 8;
1610 result |= (value & 0xff000000) >> 24;
1611 return result;
1612 }
1613
1614 #define MAYBE_SWAP(V) byte_swap (V)
1615
1616 #else
1617 #define MAYBE_SWAP(V) (V)
1618 #endif /* WORDS_BIGENDIAN */
1619
1620 /* The suffix for an index file. */
1621 #define INDEX_SUFFIX ".gdb-index"
1622
1623 static const char *dwarf2_physname (char *name, struct die_info *die,
1624 struct dwarf2_cu *cu);
1625
1626 /* Try to locate the sections we need for DWARF 2 debugging
1627 information and return true if we have enough to do something.
1628 NAMES points to the dwarf2 section names, or is NULL if the standard
1629 ELF names are used. */
1630
1631 int
1632 dwarf2_has_info (struct objfile *objfile,
1633 const struct dwarf2_debug_sections *names)
1634 {
1635 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1636 if (!dwarf2_per_objfile)
1637 {
1638 /* Initialize per-objfile state. */
1639 struct dwarf2_per_objfile *data
1640 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1641
1642 memset (data, 0, sizeof (*data));
1643 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1644 dwarf2_per_objfile = data;
1645
1646 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1647 (void *) names);
1648 dwarf2_per_objfile->objfile = objfile;
1649 }
1650 return (dwarf2_per_objfile->info.asection != NULL
1651 && dwarf2_per_objfile->abbrev.asection != NULL);
1652 }
1653
1654 /* When loading sections, we look either for uncompressed section or for
1655 compressed section names. */
1656
1657 static int
1658 section_is_p (const char *section_name,
1659 const struct dwarf2_section_names *names)
1660 {
1661 if (names->normal != NULL
1662 && strcmp (section_name, names->normal) == 0)
1663 return 1;
1664 if (names->compressed != NULL
1665 && strcmp (section_name, names->compressed) == 0)
1666 return 1;
1667 return 0;
1668 }
1669
1670 /* This function is mapped across the sections and remembers the
1671 offset and size of each of the debugging sections we are interested
1672 in. */
1673
1674 static void
1675 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1676 {
1677 const struct dwarf2_debug_sections *names;
1678
1679 if (vnames == NULL)
1680 names = &dwarf2_elf_names;
1681 else
1682 names = (const struct dwarf2_debug_sections *) vnames;
1683
1684 if (section_is_p (sectp->name, &names->info))
1685 {
1686 dwarf2_per_objfile->info.asection = sectp;
1687 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1688 }
1689 else if (section_is_p (sectp->name, &names->abbrev))
1690 {
1691 dwarf2_per_objfile->abbrev.asection = sectp;
1692 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1693 }
1694 else if (section_is_p (sectp->name, &names->line))
1695 {
1696 dwarf2_per_objfile->line.asection = sectp;
1697 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1698 }
1699 else if (section_is_p (sectp->name, &names->loc))
1700 {
1701 dwarf2_per_objfile->loc.asection = sectp;
1702 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1703 }
1704 else if (section_is_p (sectp->name, &names->macinfo))
1705 {
1706 dwarf2_per_objfile->macinfo.asection = sectp;
1707 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1708 }
1709 else if (section_is_p (sectp->name, &names->macro))
1710 {
1711 dwarf2_per_objfile->macro.asection = sectp;
1712 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1713 }
1714 else if (section_is_p (sectp->name, &names->str))
1715 {
1716 dwarf2_per_objfile->str.asection = sectp;
1717 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1718 }
1719 else if (section_is_p (sectp->name, &names->addr))
1720 {
1721 dwarf2_per_objfile->addr.asection = sectp;
1722 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1723 }
1724 else if (section_is_p (sectp->name, &names->frame))
1725 {
1726 dwarf2_per_objfile->frame.asection = sectp;
1727 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1728 }
1729 else if (section_is_p (sectp->name, &names->eh_frame))
1730 {
1731 flagword aflag = bfd_get_section_flags (abfd, sectp);
1732
1733 if (aflag & SEC_HAS_CONTENTS)
1734 {
1735 dwarf2_per_objfile->eh_frame.asection = sectp;
1736 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1737 }
1738 }
1739 else if (section_is_p (sectp->name, &names->ranges))
1740 {
1741 dwarf2_per_objfile->ranges.asection = sectp;
1742 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1743 }
1744 else if (section_is_p (sectp->name, &names->types))
1745 {
1746 struct dwarf2_section_info type_section;
1747
1748 memset (&type_section, 0, sizeof (type_section));
1749 type_section.asection = sectp;
1750 type_section.size = bfd_get_section_size (sectp);
1751
1752 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1753 &type_section);
1754 }
1755 else if (section_is_p (sectp->name, &names->gdb_index))
1756 {
1757 dwarf2_per_objfile->gdb_index.asection = sectp;
1758 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1759 }
1760
1761 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1762 && bfd_section_vma (abfd, sectp) == 0)
1763 dwarf2_per_objfile->has_section_at_zero = 1;
1764 }
1765
1766 /* A helper function that decides whether a section is empty,
1767 or not present. */
1768
1769 static int
1770 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1771 {
1772 return info->asection == NULL || info->size == 0;
1773 }
1774
1775 /* Read the contents of the section INFO.
1776 OBJFILE is the main object file, but not necessarily the file where
1777 the section comes from. E.g., for DWO files INFO->asection->owner
1778 is the bfd of the DWO file.
1779 If the section is compressed, uncompress it before returning. */
1780
1781 static void
1782 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1783 {
1784 asection *sectp = info->asection;
1785 bfd *abfd;
1786 gdb_byte *buf, *retbuf;
1787 unsigned char header[4];
1788
1789 if (info->readin)
1790 return;
1791 info->buffer = NULL;
1792 info->readin = 1;
1793
1794 if (dwarf2_section_empty_p (info))
1795 return;
1796
1797 abfd = sectp->owner;
1798
1799 /* If the section has relocations, we must read it ourselves.
1800 Otherwise we attach it to the BFD. */
1801 if ((sectp->flags & SEC_RELOC) == 0)
1802 {
1803 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1804
1805 /* We have to cast away const here for historical reasons.
1806 Fixing dwarf2read to be const-correct would be quite nice. */
1807 info->buffer = (gdb_byte *) bytes;
1808 return;
1809 }
1810
1811 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1812 info->buffer = buf;
1813
1814 /* When debugging .o files, we may need to apply relocations; see
1815 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1816 We never compress sections in .o files, so we only need to
1817 try this when the section is not compressed. */
1818 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1819 if (retbuf != NULL)
1820 {
1821 info->buffer = retbuf;
1822 return;
1823 }
1824
1825 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1826 || bfd_bread (buf, info->size, abfd) != info->size)
1827 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1828 bfd_get_filename (abfd));
1829 }
1830
1831 /* A helper function that returns the size of a section in a safe way.
1832 If you are positive that the section has been read before using the
1833 size, then it is safe to refer to the dwarf2_section_info object's
1834 "size" field directly. In other cases, you must call this
1835 function, because for compressed sections the size field is not set
1836 correctly until the section has been read. */
1837
1838 static bfd_size_type
1839 dwarf2_section_size (struct objfile *objfile,
1840 struct dwarf2_section_info *info)
1841 {
1842 if (!info->readin)
1843 dwarf2_read_section (objfile, info);
1844 return info->size;
1845 }
1846
1847 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1848 SECTION_NAME. */
1849
1850 void
1851 dwarf2_get_section_info (struct objfile *objfile,
1852 enum dwarf2_section_enum sect,
1853 asection **sectp, gdb_byte **bufp,
1854 bfd_size_type *sizep)
1855 {
1856 struct dwarf2_per_objfile *data
1857 = objfile_data (objfile, dwarf2_objfile_data_key);
1858 struct dwarf2_section_info *info;
1859
1860 /* We may see an objfile without any DWARF, in which case we just
1861 return nothing. */
1862 if (data == NULL)
1863 {
1864 *sectp = NULL;
1865 *bufp = NULL;
1866 *sizep = 0;
1867 return;
1868 }
1869 switch (sect)
1870 {
1871 case DWARF2_DEBUG_FRAME:
1872 info = &data->frame;
1873 break;
1874 case DWARF2_EH_FRAME:
1875 info = &data->eh_frame;
1876 break;
1877 default:
1878 gdb_assert_not_reached ("unexpected section");
1879 }
1880
1881 dwarf2_read_section (objfile, info);
1882
1883 *sectp = info->asection;
1884 *bufp = info->buffer;
1885 *sizep = info->size;
1886 }
1887
1888 \f
1889 /* DWARF quick_symbols_functions support. */
1890
1891 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1892 unique line tables, so we maintain a separate table of all .debug_line
1893 derived entries to support the sharing.
1894 All the quick functions need is the list of file names. We discard the
1895 line_header when we're done and don't need to record it here. */
1896 struct quick_file_names
1897 {
1898 /* The data used to construct the hash key. */
1899 struct stmt_list_hash hash;
1900
1901 /* The number of entries in file_names, real_names. */
1902 unsigned int num_file_names;
1903
1904 /* The file names from the line table, after being run through
1905 file_full_name. */
1906 const char **file_names;
1907
1908 /* The file names from the line table after being run through
1909 gdb_realpath. These are computed lazily. */
1910 const char **real_names;
1911 };
1912
1913 /* When using the index (and thus not using psymtabs), each CU has an
1914 object of this type. This is used to hold information needed by
1915 the various "quick" methods. */
1916 struct dwarf2_per_cu_quick_data
1917 {
1918 /* The file table. This can be NULL if there was no file table
1919 or it's currently not read in.
1920 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1921 struct quick_file_names *file_names;
1922
1923 /* The corresponding symbol table. This is NULL if symbols for this
1924 CU have not yet been read. */
1925 struct symtab *symtab;
1926
1927 /* A temporary mark bit used when iterating over all CUs in
1928 expand_symtabs_matching. */
1929 unsigned int mark : 1;
1930
1931 /* True if we've tried to read the file table and found there isn't one.
1932 There will be no point in trying to read it again next time. */
1933 unsigned int no_file_data : 1;
1934 };
1935
1936 /* Utility hash function for a stmt_list_hash. */
1937
1938 static hashval_t
1939 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1940 {
1941 hashval_t v = 0;
1942
1943 if (stmt_list_hash->dwo_unit != NULL)
1944 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1945 v += stmt_list_hash->line_offset.sect_off;
1946 return v;
1947 }
1948
1949 /* Utility equality function for a stmt_list_hash. */
1950
1951 static int
1952 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1953 const struct stmt_list_hash *rhs)
1954 {
1955 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1956 return 0;
1957 if (lhs->dwo_unit != NULL
1958 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1959 return 0;
1960
1961 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
1962 }
1963
1964 /* Hash function for a quick_file_names. */
1965
1966 static hashval_t
1967 hash_file_name_entry (const void *e)
1968 {
1969 const struct quick_file_names *file_data = e;
1970
1971 return hash_stmt_list_entry (&file_data->hash);
1972 }
1973
1974 /* Equality function for a quick_file_names. */
1975
1976 static int
1977 eq_file_name_entry (const void *a, const void *b)
1978 {
1979 const struct quick_file_names *ea = a;
1980 const struct quick_file_names *eb = b;
1981
1982 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1983 }
1984
1985 /* Delete function for a quick_file_names. */
1986
1987 static void
1988 delete_file_name_entry (void *e)
1989 {
1990 struct quick_file_names *file_data = e;
1991 int i;
1992
1993 for (i = 0; i < file_data->num_file_names; ++i)
1994 {
1995 xfree ((void*) file_data->file_names[i]);
1996 if (file_data->real_names)
1997 xfree ((void*) file_data->real_names[i]);
1998 }
1999
2000 /* The space for the struct itself lives on objfile_obstack,
2001 so we don't free it here. */
2002 }
2003
2004 /* Create a quick_file_names hash table. */
2005
2006 static htab_t
2007 create_quick_file_names_table (unsigned int nr_initial_entries)
2008 {
2009 return htab_create_alloc (nr_initial_entries,
2010 hash_file_name_entry, eq_file_name_entry,
2011 delete_file_name_entry, xcalloc, xfree);
2012 }
2013
2014 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2015 have to be created afterwards. You should call age_cached_comp_units after
2016 processing PER_CU->CU. dw2_setup must have been already called. */
2017
2018 static void
2019 load_cu (struct dwarf2_per_cu_data *per_cu)
2020 {
2021 if (per_cu->is_debug_types)
2022 load_full_type_unit (per_cu);
2023 else
2024 load_full_comp_unit (per_cu, language_minimal);
2025
2026 gdb_assert (per_cu->cu != NULL);
2027
2028 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2029 }
2030
2031 /* Read in the symbols for PER_CU. */
2032
2033 static void
2034 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2035 {
2036 struct cleanup *back_to;
2037
2038 /* Skip type_unit_groups, reading the type units they contain
2039 is handled elsewhere. */
2040 if (IS_TYPE_UNIT_GROUP (per_cu))
2041 return;
2042
2043 back_to = make_cleanup (dwarf2_release_queue, NULL);
2044
2045 if (dwarf2_per_objfile->using_index
2046 ? per_cu->v.quick->symtab == NULL
2047 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2048 {
2049 queue_comp_unit (per_cu, language_minimal);
2050 load_cu (per_cu);
2051 }
2052
2053 process_queue ();
2054
2055 /* Age the cache, releasing compilation units that have not
2056 been used recently. */
2057 age_cached_comp_units ();
2058
2059 do_cleanups (back_to);
2060 }
2061
2062 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2063 the objfile from which this CU came. Returns the resulting symbol
2064 table. */
2065
2066 static struct symtab *
2067 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2068 {
2069 gdb_assert (dwarf2_per_objfile->using_index);
2070 if (!per_cu->v.quick->symtab)
2071 {
2072 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2073 increment_reading_symtab ();
2074 dw2_do_instantiate_symtab (per_cu);
2075 process_cu_includes ();
2076 do_cleanups (back_to);
2077 }
2078 return per_cu->v.quick->symtab;
2079 }
2080
2081 /* Return the CU given its index.
2082
2083 This is intended for loops like:
2084
2085 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2086 + dwarf2_per_objfile->n_type_units); ++i)
2087 {
2088 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2089
2090 ...;
2091 }
2092 */
2093
2094 static struct dwarf2_per_cu_data *
2095 dw2_get_cu (int index)
2096 {
2097 if (index >= dwarf2_per_objfile->n_comp_units)
2098 {
2099 index -= dwarf2_per_objfile->n_comp_units;
2100 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2101 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2102 }
2103
2104 return dwarf2_per_objfile->all_comp_units[index];
2105 }
2106
2107 /* Return the primary CU given its index.
2108 The difference between this function and dw2_get_cu is in the handling
2109 of type units (TUs). Here we return the type_unit_group object.
2110
2111 This is intended for loops like:
2112
2113 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2114 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2115 {
2116 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2117
2118 ...;
2119 }
2120 */
2121
2122 static struct dwarf2_per_cu_data *
2123 dw2_get_primary_cu (int index)
2124 {
2125 if (index >= dwarf2_per_objfile->n_comp_units)
2126 {
2127 index -= dwarf2_per_objfile->n_comp_units;
2128 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2129 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2130 }
2131
2132 return dwarf2_per_objfile->all_comp_units[index];
2133 }
2134
2135 /* A helper function that knows how to read a 64-bit value in a way
2136 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2137 otherwise. */
2138
2139 static int
2140 extract_cu_value (const char *bytes, ULONGEST *result)
2141 {
2142 if (sizeof (ULONGEST) < 8)
2143 {
2144 int i;
2145
2146 /* Ignore the upper 4 bytes if they are all zero. */
2147 for (i = 0; i < 4; ++i)
2148 if (bytes[i + 4] != 0)
2149 return 0;
2150
2151 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2152 }
2153 else
2154 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2155 return 1;
2156 }
2157
2158 /* Read the CU list from the mapped index, and use it to create all
2159 the CU objects for this objfile. Return 0 if something went wrong,
2160 1 if everything went ok. */
2161
2162 static int
2163 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2164 offset_type cu_list_elements)
2165 {
2166 offset_type i;
2167
2168 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2169 dwarf2_per_objfile->all_comp_units
2170 = obstack_alloc (&objfile->objfile_obstack,
2171 dwarf2_per_objfile->n_comp_units
2172 * sizeof (struct dwarf2_per_cu_data *));
2173
2174 for (i = 0; i < cu_list_elements; i += 2)
2175 {
2176 struct dwarf2_per_cu_data *the_cu;
2177 ULONGEST offset, length;
2178
2179 if (!extract_cu_value (cu_list, &offset)
2180 || !extract_cu_value (cu_list + 8, &length))
2181 return 0;
2182 cu_list += 2 * 8;
2183
2184 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2185 struct dwarf2_per_cu_data);
2186 the_cu->offset.sect_off = offset;
2187 the_cu->length = length;
2188 the_cu->objfile = objfile;
2189 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2190 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2191 struct dwarf2_per_cu_quick_data);
2192 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2193 }
2194
2195 return 1;
2196 }
2197
2198 /* Create the signatured type hash table from the index. */
2199
2200 static int
2201 create_signatured_type_table_from_index (struct objfile *objfile,
2202 struct dwarf2_section_info *section,
2203 const gdb_byte *bytes,
2204 offset_type elements)
2205 {
2206 offset_type i;
2207 htab_t sig_types_hash;
2208
2209 dwarf2_per_objfile->n_type_units = elements / 3;
2210 dwarf2_per_objfile->all_type_units
2211 = obstack_alloc (&objfile->objfile_obstack,
2212 dwarf2_per_objfile->n_type_units
2213 * sizeof (struct signatured_type *));
2214
2215 sig_types_hash = allocate_signatured_type_table (objfile);
2216
2217 for (i = 0; i < elements; i += 3)
2218 {
2219 struct signatured_type *sig_type;
2220 ULONGEST offset, type_offset_in_tu, signature;
2221 void **slot;
2222
2223 if (!extract_cu_value (bytes, &offset)
2224 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2225 return 0;
2226 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2227 bytes += 3 * 8;
2228
2229 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2230 struct signatured_type);
2231 sig_type->signature = signature;
2232 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2233 sig_type->per_cu.is_debug_types = 1;
2234 sig_type->per_cu.info_or_types_section = section;
2235 sig_type->per_cu.offset.sect_off = offset;
2236 sig_type->per_cu.objfile = objfile;
2237 sig_type->per_cu.v.quick
2238 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2239 struct dwarf2_per_cu_quick_data);
2240
2241 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2242 *slot = sig_type;
2243
2244 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2245 }
2246
2247 dwarf2_per_objfile->signatured_types = sig_types_hash;
2248
2249 return 1;
2250 }
2251
2252 /* Read the address map data from the mapped index, and use it to
2253 populate the objfile's psymtabs_addrmap. */
2254
2255 static void
2256 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2257 {
2258 const gdb_byte *iter, *end;
2259 struct obstack temp_obstack;
2260 struct addrmap *mutable_map;
2261 struct cleanup *cleanup;
2262 CORE_ADDR baseaddr;
2263
2264 obstack_init (&temp_obstack);
2265 cleanup = make_cleanup_obstack_free (&temp_obstack);
2266 mutable_map = addrmap_create_mutable (&temp_obstack);
2267
2268 iter = index->address_table;
2269 end = iter + index->address_table_size;
2270
2271 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2272
2273 while (iter < end)
2274 {
2275 ULONGEST hi, lo, cu_index;
2276 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2277 iter += 8;
2278 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2279 iter += 8;
2280 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2281 iter += 4;
2282
2283 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2284 dw2_get_cu (cu_index));
2285 }
2286
2287 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2288 &objfile->objfile_obstack);
2289 do_cleanups (cleanup);
2290 }
2291
2292 /* The hash function for strings in the mapped index. This is the same as
2293 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2294 implementation. This is necessary because the hash function is tied to the
2295 format of the mapped index file. The hash values do not have to match with
2296 SYMBOL_HASH_NEXT.
2297
2298 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2299
2300 static hashval_t
2301 mapped_index_string_hash (int index_version, const void *p)
2302 {
2303 const unsigned char *str = (const unsigned char *) p;
2304 hashval_t r = 0;
2305 unsigned char c;
2306
2307 while ((c = *str++) != 0)
2308 {
2309 if (index_version >= 5)
2310 c = tolower (c);
2311 r = r * 67 + c - 113;
2312 }
2313
2314 return r;
2315 }
2316
2317 /* Find a slot in the mapped index INDEX for the object named NAME.
2318 If NAME is found, set *VEC_OUT to point to the CU vector in the
2319 constant pool and return 1. If NAME cannot be found, return 0. */
2320
2321 static int
2322 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2323 offset_type **vec_out)
2324 {
2325 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2326 offset_type hash;
2327 offset_type slot, step;
2328 int (*cmp) (const char *, const char *);
2329
2330 if (current_language->la_language == language_cplus
2331 || current_language->la_language == language_java
2332 || current_language->la_language == language_fortran)
2333 {
2334 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2335 not contain any. */
2336 const char *paren = strchr (name, '(');
2337
2338 if (paren)
2339 {
2340 char *dup;
2341
2342 dup = xmalloc (paren - name + 1);
2343 memcpy (dup, name, paren - name);
2344 dup[paren - name] = 0;
2345
2346 make_cleanup (xfree, dup);
2347 name = dup;
2348 }
2349 }
2350
2351 /* Index version 4 did not support case insensitive searches. But the
2352 indices for case insensitive languages are built in lowercase, therefore
2353 simulate our NAME being searched is also lowercased. */
2354 hash = mapped_index_string_hash ((index->version == 4
2355 && case_sensitivity == case_sensitive_off
2356 ? 5 : index->version),
2357 name);
2358
2359 slot = hash & (index->symbol_table_slots - 1);
2360 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2361 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2362
2363 for (;;)
2364 {
2365 /* Convert a slot number to an offset into the table. */
2366 offset_type i = 2 * slot;
2367 const char *str;
2368 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2369 {
2370 do_cleanups (back_to);
2371 return 0;
2372 }
2373
2374 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2375 if (!cmp (name, str))
2376 {
2377 *vec_out = (offset_type *) (index->constant_pool
2378 + MAYBE_SWAP (index->symbol_table[i + 1]));
2379 do_cleanups (back_to);
2380 return 1;
2381 }
2382
2383 slot = (slot + step) & (index->symbol_table_slots - 1);
2384 }
2385 }
2386
2387 /* Read the index file. If everything went ok, initialize the "quick"
2388 elements of all the CUs and return 1. Otherwise, return 0. */
2389
2390 static int
2391 dwarf2_read_index (struct objfile *objfile)
2392 {
2393 char *addr;
2394 struct mapped_index *map;
2395 offset_type *metadata;
2396 const gdb_byte *cu_list;
2397 const gdb_byte *types_list = NULL;
2398 offset_type version, cu_list_elements;
2399 offset_type types_list_elements = 0;
2400 int i;
2401
2402 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2403 return 0;
2404
2405 /* Older elfutils strip versions could keep the section in the main
2406 executable while splitting it for the separate debug info file. */
2407 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2408 & SEC_HAS_CONTENTS) == 0)
2409 return 0;
2410
2411 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2412
2413 addr = dwarf2_per_objfile->gdb_index.buffer;
2414 /* Version check. */
2415 version = MAYBE_SWAP (*(offset_type *) addr);
2416 /* Versions earlier than 3 emitted every copy of a psymbol. This
2417 causes the index to behave very poorly for certain requests. Version 3
2418 contained incomplete addrmap. So, it seems better to just ignore such
2419 indices. */
2420 if (version < 4)
2421 {
2422 static int warning_printed = 0;
2423 if (!warning_printed)
2424 {
2425 warning (_("Skipping obsolete .gdb_index section in %s."),
2426 objfile->name);
2427 warning_printed = 1;
2428 }
2429 return 0;
2430 }
2431 /* Index version 4 uses a different hash function than index version
2432 5 and later.
2433
2434 Versions earlier than 6 did not emit psymbols for inlined
2435 functions. Using these files will cause GDB not to be able to
2436 set breakpoints on inlined functions by name, so we ignore these
2437 indices unless the --use-deprecated-index-sections command line
2438 option was supplied. */
2439 if (version < 6 && !use_deprecated_index_sections)
2440 {
2441 static int warning_printed = 0;
2442 if (!warning_printed)
2443 {
2444 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2445 "--use-deprecated-index-sections to use them anyway"),
2446 objfile->name);
2447 warning_printed = 1;
2448 }
2449 return 0;
2450 }
2451 /* Indexes with higher version than the one supported by GDB may be no
2452 longer backward compatible. */
2453 if (version > 7)
2454 return 0;
2455
2456 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2457 map->version = version;
2458 map->total_size = dwarf2_per_objfile->gdb_index.size;
2459
2460 metadata = (offset_type *) (addr + sizeof (offset_type));
2461
2462 i = 0;
2463 cu_list = addr + MAYBE_SWAP (metadata[i]);
2464 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2465 / 8);
2466 ++i;
2467
2468 types_list = addr + MAYBE_SWAP (metadata[i]);
2469 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2470 - MAYBE_SWAP (metadata[i]))
2471 / 8);
2472 ++i;
2473
2474 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2475 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2476 - MAYBE_SWAP (metadata[i]));
2477 ++i;
2478
2479 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2480 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2481 - MAYBE_SWAP (metadata[i]))
2482 / (2 * sizeof (offset_type)));
2483 ++i;
2484
2485 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2486
2487 /* Don't use the index if it's empty. */
2488 if (map->symbol_table_slots == 0)
2489 return 0;
2490
2491 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2492 return 0;
2493
2494 if (types_list_elements)
2495 {
2496 struct dwarf2_section_info *section;
2497
2498 /* We can only handle a single .debug_types when we have an
2499 index. */
2500 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2501 return 0;
2502
2503 section = VEC_index (dwarf2_section_info_def,
2504 dwarf2_per_objfile->types, 0);
2505
2506 if (!create_signatured_type_table_from_index (objfile, section,
2507 types_list,
2508 types_list_elements))
2509 return 0;
2510 }
2511
2512 create_addrmap_from_index (objfile, map);
2513
2514 dwarf2_per_objfile->index_table = map;
2515 dwarf2_per_objfile->using_index = 1;
2516 dwarf2_per_objfile->quick_file_names_table =
2517 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2518
2519 return 1;
2520 }
2521
2522 /* A helper for the "quick" functions which sets the global
2523 dwarf2_per_objfile according to OBJFILE. */
2524
2525 static void
2526 dw2_setup (struct objfile *objfile)
2527 {
2528 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2529 gdb_assert (dwarf2_per_objfile);
2530 }
2531
2532 /* Reader function for dw2_build_type_unit_groups. */
2533
2534 static void
2535 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2536 gdb_byte *info_ptr,
2537 struct die_info *type_unit_die,
2538 int has_children,
2539 void *data)
2540 {
2541 struct dwarf2_cu *cu = reader->cu;
2542 struct attribute *attr;
2543 struct type_unit_group *tu_group;
2544
2545 gdb_assert (data == NULL);
2546
2547 if (! has_children)
2548 return;
2549
2550 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2551 /* Call this for its side-effect of creating the associated
2552 struct type_unit_group if it doesn't already exist. */
2553 tu_group = get_type_unit_group (cu, attr);
2554 }
2555
2556 /* Build dwarf2_per_objfile->type_unit_groups.
2557 This function may be called multiple times. */
2558
2559 static void
2560 dw2_build_type_unit_groups (void)
2561 {
2562 if (dwarf2_per_objfile->type_unit_groups == NULL)
2563 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2564 }
2565
2566 /* die_reader_func for dw2_get_file_names. */
2567
2568 static void
2569 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2570 gdb_byte *info_ptr,
2571 struct die_info *comp_unit_die,
2572 int has_children,
2573 void *data)
2574 {
2575 struct dwarf2_cu *cu = reader->cu;
2576 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2577 struct objfile *objfile = dwarf2_per_objfile->objfile;
2578 struct dwarf2_per_cu_data *lh_cu;
2579 struct line_header *lh;
2580 struct attribute *attr;
2581 int i;
2582 char *name, *comp_dir;
2583 void **slot;
2584 struct quick_file_names *qfn;
2585 unsigned int line_offset;
2586
2587 /* Our callers never want to match partial units -- instead they
2588 will match the enclosing full CU. */
2589 if (comp_unit_die->tag == DW_TAG_partial_unit)
2590 {
2591 this_cu->v.quick->no_file_data = 1;
2592 return;
2593 }
2594
2595 /* If we're reading the line header for TUs, store it in the "per_cu"
2596 for tu_group. */
2597 if (this_cu->is_debug_types)
2598 {
2599 struct type_unit_group *tu_group = data;
2600
2601 gdb_assert (tu_group != NULL);
2602 lh_cu = &tu_group->per_cu;
2603 }
2604 else
2605 lh_cu = this_cu;
2606
2607 lh = NULL;
2608 slot = NULL;
2609 line_offset = 0;
2610
2611 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2612 if (attr)
2613 {
2614 struct quick_file_names find_entry;
2615
2616 line_offset = DW_UNSND (attr);
2617
2618 /* We may have already read in this line header (TU line header sharing).
2619 If we have we're done. */
2620 find_entry.hash.dwo_unit = cu->dwo_unit;
2621 find_entry.hash.line_offset.sect_off = line_offset;
2622 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2623 &find_entry, INSERT);
2624 if (*slot != NULL)
2625 {
2626 lh_cu->v.quick->file_names = *slot;
2627 return;
2628 }
2629
2630 lh = dwarf_decode_line_header (line_offset, cu);
2631 }
2632 if (lh == NULL)
2633 {
2634 lh_cu->v.quick->no_file_data = 1;
2635 return;
2636 }
2637
2638 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2639 qfn->hash.dwo_unit = cu->dwo_unit;
2640 qfn->hash.line_offset.sect_off = line_offset;
2641 gdb_assert (slot != NULL);
2642 *slot = qfn;
2643
2644 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2645
2646 qfn->num_file_names = lh->num_file_names;
2647 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2648 lh->num_file_names * sizeof (char *));
2649 for (i = 0; i < lh->num_file_names; ++i)
2650 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2651 qfn->real_names = NULL;
2652
2653 free_line_header (lh);
2654
2655 lh_cu->v.quick->file_names = qfn;
2656 }
2657
2658 /* A helper for the "quick" functions which attempts to read the line
2659 table for THIS_CU. */
2660
2661 static struct quick_file_names *
2662 dw2_get_file_names (struct objfile *objfile,
2663 struct dwarf2_per_cu_data *this_cu)
2664 {
2665 /* For TUs this should only be called on the parent group. */
2666 if (this_cu->is_debug_types)
2667 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2668
2669 if (this_cu->v.quick->file_names != NULL)
2670 return this_cu->v.quick->file_names;
2671 /* If we know there is no line data, no point in looking again. */
2672 if (this_cu->v.quick->no_file_data)
2673 return NULL;
2674
2675 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2676 in the stub for CUs, there's is no need to lookup the DWO file.
2677 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2678 DWO file. */
2679 if (this_cu->is_debug_types)
2680 {
2681 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2682
2683 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2684 dw2_get_file_names_reader, tu_group);
2685 }
2686 else
2687 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2688
2689 if (this_cu->v.quick->no_file_data)
2690 return NULL;
2691 return this_cu->v.quick->file_names;
2692 }
2693
2694 /* A helper for the "quick" functions which computes and caches the
2695 real path for a given file name from the line table. */
2696
2697 static const char *
2698 dw2_get_real_path (struct objfile *objfile,
2699 struct quick_file_names *qfn, int index)
2700 {
2701 if (qfn->real_names == NULL)
2702 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2703 qfn->num_file_names, sizeof (char *));
2704
2705 if (qfn->real_names[index] == NULL)
2706 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2707
2708 return qfn->real_names[index];
2709 }
2710
2711 static struct symtab *
2712 dw2_find_last_source_symtab (struct objfile *objfile)
2713 {
2714 int index;
2715
2716 dw2_setup (objfile);
2717 index = dwarf2_per_objfile->n_comp_units - 1;
2718 return dw2_instantiate_symtab (dw2_get_cu (index));
2719 }
2720
2721 /* Traversal function for dw2_forget_cached_source_info. */
2722
2723 static int
2724 dw2_free_cached_file_names (void **slot, void *info)
2725 {
2726 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2727
2728 if (file_data->real_names)
2729 {
2730 int i;
2731
2732 for (i = 0; i < file_data->num_file_names; ++i)
2733 {
2734 xfree ((void*) file_data->real_names[i]);
2735 file_data->real_names[i] = NULL;
2736 }
2737 }
2738
2739 return 1;
2740 }
2741
2742 static void
2743 dw2_forget_cached_source_info (struct objfile *objfile)
2744 {
2745 dw2_setup (objfile);
2746
2747 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2748 dw2_free_cached_file_names, NULL);
2749 }
2750
2751 /* Helper function for dw2_map_symtabs_matching_filename that expands
2752 the symtabs and calls the iterator. */
2753
2754 static int
2755 dw2_map_expand_apply (struct objfile *objfile,
2756 struct dwarf2_per_cu_data *per_cu,
2757 const char *name,
2758 const char *full_path, const char *real_path,
2759 int (*callback) (struct symtab *, void *),
2760 void *data)
2761 {
2762 struct symtab *last_made = objfile->symtabs;
2763
2764 /* Don't visit already-expanded CUs. */
2765 if (per_cu->v.quick->symtab)
2766 return 0;
2767
2768 /* This may expand more than one symtab, and we want to iterate over
2769 all of them. */
2770 dw2_instantiate_symtab (per_cu);
2771
2772 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2773 objfile->symtabs, last_made);
2774 }
2775
2776 /* Implementation of the map_symtabs_matching_filename method. */
2777
2778 static int
2779 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2780 const char *full_path, const char *real_path,
2781 int (*callback) (struct symtab *, void *),
2782 void *data)
2783 {
2784 int i;
2785 const char *name_basename = lbasename (name);
2786 int name_len = strlen (name);
2787 int is_abs = IS_ABSOLUTE_PATH (name);
2788
2789 dw2_setup (objfile);
2790
2791 dw2_build_type_unit_groups ();
2792
2793 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2794 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2795 {
2796 int j;
2797 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2798 struct quick_file_names *file_data;
2799
2800 /* We only need to look at symtabs not already expanded. */
2801 if (per_cu->v.quick->symtab)
2802 continue;
2803
2804 file_data = dw2_get_file_names (objfile, per_cu);
2805 if (file_data == NULL)
2806 continue;
2807
2808 for (j = 0; j < file_data->num_file_names; ++j)
2809 {
2810 const char *this_name = file_data->file_names[j];
2811
2812 if (FILENAME_CMP (name, this_name) == 0
2813 || (!is_abs && compare_filenames_for_search (this_name,
2814 name, name_len)))
2815 {
2816 if (dw2_map_expand_apply (objfile, per_cu,
2817 name, full_path, real_path,
2818 callback, data))
2819 return 1;
2820 }
2821
2822 /* Before we invoke realpath, which can get expensive when many
2823 files are involved, do a quick comparison of the basenames. */
2824 if (! basenames_may_differ
2825 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2826 continue;
2827
2828 if (full_path != NULL)
2829 {
2830 const char *this_real_name = dw2_get_real_path (objfile,
2831 file_data, j);
2832
2833 if (this_real_name != NULL
2834 && (FILENAME_CMP (full_path, this_real_name) == 0
2835 || (!is_abs
2836 && compare_filenames_for_search (this_real_name,
2837 name, name_len))))
2838 {
2839 if (dw2_map_expand_apply (objfile, per_cu,
2840 name, full_path, real_path,
2841 callback, data))
2842 return 1;
2843 }
2844 }
2845
2846 if (real_path != NULL)
2847 {
2848 const char *this_real_name = dw2_get_real_path (objfile,
2849 file_data, j);
2850
2851 if (this_real_name != NULL
2852 && (FILENAME_CMP (real_path, this_real_name) == 0
2853 || (!is_abs
2854 && compare_filenames_for_search (this_real_name,
2855 name, name_len))))
2856 {
2857 if (dw2_map_expand_apply (objfile, per_cu,
2858 name, full_path, real_path,
2859 callback, data))
2860 return 1;
2861 }
2862 }
2863 }
2864 }
2865
2866 return 0;
2867 }
2868
2869 static struct symtab *
2870 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2871 const char *name, domain_enum domain)
2872 {
2873 /* We do all the work in the pre_expand_symtabs_matching hook
2874 instead. */
2875 return NULL;
2876 }
2877
2878 /* A helper function that expands all symtabs that hold an object
2879 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2880 symbols in block BLOCK_KIND. */
2881
2882 static void
2883 dw2_do_expand_symtabs_matching (struct objfile *objfile,
2884 int want_specific_block,
2885 enum block_enum block_kind,
2886 const char *name, domain_enum domain)
2887 {
2888 struct mapped_index *index;
2889
2890 dw2_setup (objfile);
2891
2892 index = dwarf2_per_objfile->index_table;
2893
2894 /* index_table is NULL if OBJF_READNOW. */
2895 if (index)
2896 {
2897 offset_type *vec;
2898
2899 if (find_slot_in_mapped_hash (index, name, &vec))
2900 {
2901 offset_type i, len = MAYBE_SWAP (*vec);
2902 for (i = 0; i < len; ++i)
2903 {
2904 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2905 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2906 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2907 int want_static = block_kind != GLOBAL_BLOCK;
2908 /* This value is only valid for index versions >= 7. */
2909 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2910 gdb_index_symbol_kind symbol_kind =
2911 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2912
2913 if (want_specific_block
2914 && index->version >= 7
2915 && want_static != is_static)
2916 continue;
2917
2918 /* Only check the symbol's kind if it has one.
2919 Indices prior to version 7 don't record it. */
2920 if (index->version >= 7)
2921 {
2922 switch (domain)
2923 {
2924 case VAR_DOMAIN:
2925 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2926 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2927 /* Some types are also in VAR_DOMAIN. */
2928 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2929 continue;
2930 break;
2931 case STRUCT_DOMAIN:
2932 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2933 continue;
2934 break;
2935 case LABEL_DOMAIN:
2936 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2937 continue;
2938 break;
2939 default:
2940 break;
2941 }
2942 }
2943
2944 dw2_instantiate_symtab (per_cu);
2945 }
2946 }
2947 }
2948 }
2949
2950 static void
2951 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2952 enum block_enum block_kind, const char *name,
2953 domain_enum domain)
2954 {
2955 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
2956 }
2957
2958 static void
2959 dw2_print_stats (struct objfile *objfile)
2960 {
2961 int i, count;
2962
2963 dw2_setup (objfile);
2964 count = 0;
2965 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2966 + dwarf2_per_objfile->n_type_units); ++i)
2967 {
2968 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2969
2970 if (!per_cu->v.quick->symtab)
2971 ++count;
2972 }
2973 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2974 }
2975
2976 static void
2977 dw2_dump (struct objfile *objfile)
2978 {
2979 /* Nothing worth printing. */
2980 }
2981
2982 static void
2983 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2984 struct section_offsets *delta)
2985 {
2986 /* There's nothing to relocate here. */
2987 }
2988
2989 static void
2990 dw2_expand_symtabs_for_function (struct objfile *objfile,
2991 const char *func_name)
2992 {
2993 /* Note: It doesn't matter what we pass for block_kind here. */
2994 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2995 VAR_DOMAIN);
2996 }
2997
2998 static void
2999 dw2_expand_all_symtabs (struct objfile *objfile)
3000 {
3001 int i;
3002
3003 dw2_setup (objfile);
3004
3005 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3006 + dwarf2_per_objfile->n_type_units); ++i)
3007 {
3008 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3009
3010 dw2_instantiate_symtab (per_cu);
3011 }
3012 }
3013
3014 static void
3015 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3016 const char *filename)
3017 {
3018 int i;
3019
3020 dw2_setup (objfile);
3021
3022 /* We don't need to consider type units here.
3023 This is only called for examining code, e.g. expand_line_sal.
3024 There can be an order of magnitude (or more) more type units
3025 than comp units, and we avoid them if we can. */
3026
3027 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3028 {
3029 int j;
3030 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3031 struct quick_file_names *file_data;
3032
3033 /* We only need to look at symtabs not already expanded. */
3034 if (per_cu->v.quick->symtab)
3035 continue;
3036
3037 file_data = dw2_get_file_names (objfile, per_cu);
3038 if (file_data == NULL)
3039 continue;
3040
3041 for (j = 0; j < file_data->num_file_names; ++j)
3042 {
3043 const char *this_name = file_data->file_names[j];
3044 if (FILENAME_CMP (this_name, filename) == 0)
3045 {
3046 dw2_instantiate_symtab (per_cu);
3047 break;
3048 }
3049 }
3050 }
3051 }
3052
3053 /* A helper function for dw2_find_symbol_file that finds the primary
3054 file name for a given CU. This is a die_reader_func. */
3055
3056 static void
3057 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3058 gdb_byte *info_ptr,
3059 struct die_info *comp_unit_die,
3060 int has_children,
3061 void *data)
3062 {
3063 const char **result_ptr = data;
3064 struct dwarf2_cu *cu = reader->cu;
3065 struct attribute *attr;
3066
3067 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3068 if (attr == NULL)
3069 *result_ptr = NULL;
3070 else
3071 *result_ptr = DW_STRING (attr);
3072 }
3073
3074 static const char *
3075 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3076 {
3077 struct dwarf2_per_cu_data *per_cu;
3078 offset_type *vec;
3079 struct quick_file_names *file_data;
3080 const char *filename;
3081
3082 dw2_setup (objfile);
3083
3084 /* index_table is NULL if OBJF_READNOW. */
3085 if (!dwarf2_per_objfile->index_table)
3086 {
3087 struct symtab *s;
3088
3089 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3090 {
3091 struct blockvector *bv = BLOCKVECTOR (s);
3092 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3093 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3094
3095 if (sym)
3096 return sym->symtab->filename;
3097 }
3098 return NULL;
3099 }
3100
3101 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3102 name, &vec))
3103 return NULL;
3104
3105 /* Note that this just looks at the very first one named NAME -- but
3106 actually we are looking for a function. find_main_filename
3107 should be rewritten so that it doesn't require a custom hook. It
3108 could just use the ordinary symbol tables. */
3109 /* vec[0] is the length, which must always be >0. */
3110 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3111
3112 if (per_cu->v.quick->symtab != NULL)
3113 return per_cu->v.quick->symtab->filename;
3114
3115 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3116 dw2_get_primary_filename_reader, &filename);
3117
3118 return filename;
3119 }
3120
3121 static void
3122 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3123 struct objfile *objfile, int global,
3124 int (*callback) (struct block *,
3125 struct symbol *, void *),
3126 void *data, symbol_compare_ftype *match,
3127 symbol_compare_ftype *ordered_compare)
3128 {
3129 /* Currently unimplemented; used for Ada. The function can be called if the
3130 current language is Ada for a non-Ada objfile using GNU index. As Ada
3131 does not look for non-Ada symbols this function should just return. */
3132 }
3133
3134 static void
3135 dw2_expand_symtabs_matching
3136 (struct objfile *objfile,
3137 int (*file_matcher) (const char *, void *),
3138 int (*name_matcher) (const char *, void *),
3139 enum search_domain kind,
3140 void *data)
3141 {
3142 int i;
3143 offset_type iter;
3144 struct mapped_index *index;
3145
3146 dw2_setup (objfile);
3147
3148 /* index_table is NULL if OBJF_READNOW. */
3149 if (!dwarf2_per_objfile->index_table)
3150 return;
3151 index = dwarf2_per_objfile->index_table;
3152
3153 if (file_matcher != NULL)
3154 {
3155 struct cleanup *cleanup;
3156 htab_t visited_found, visited_not_found;
3157
3158 dw2_build_type_unit_groups ();
3159
3160 visited_found = htab_create_alloc (10,
3161 htab_hash_pointer, htab_eq_pointer,
3162 NULL, xcalloc, xfree);
3163 cleanup = make_cleanup_htab_delete (visited_found);
3164 visited_not_found = htab_create_alloc (10,
3165 htab_hash_pointer, htab_eq_pointer,
3166 NULL, xcalloc, xfree);
3167 make_cleanup_htab_delete (visited_not_found);
3168
3169 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3170 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3171 {
3172 int j;
3173 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3174 struct quick_file_names *file_data;
3175 void **slot;
3176
3177 per_cu->v.quick->mark = 0;
3178
3179 /* We only need to look at symtabs not already expanded. */
3180 if (per_cu->v.quick->symtab)
3181 continue;
3182
3183 file_data = dw2_get_file_names (objfile, per_cu);
3184 if (file_data == NULL)
3185 continue;
3186
3187 if (htab_find (visited_not_found, file_data) != NULL)
3188 continue;
3189 else if (htab_find (visited_found, file_data) != NULL)
3190 {
3191 per_cu->v.quick->mark = 1;
3192 continue;
3193 }
3194
3195 for (j = 0; j < file_data->num_file_names; ++j)
3196 {
3197 if (file_matcher (file_data->file_names[j], data))
3198 {
3199 per_cu->v.quick->mark = 1;
3200 break;
3201 }
3202 }
3203
3204 slot = htab_find_slot (per_cu->v.quick->mark
3205 ? visited_found
3206 : visited_not_found,
3207 file_data, INSERT);
3208 *slot = file_data;
3209 }
3210
3211 do_cleanups (cleanup);
3212 }
3213
3214 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3215 {
3216 offset_type idx = 2 * iter;
3217 const char *name;
3218 offset_type *vec, vec_len, vec_idx;
3219
3220 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3221 continue;
3222
3223 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3224
3225 if (! (*name_matcher) (name, data))
3226 continue;
3227
3228 /* The name was matched, now expand corresponding CUs that were
3229 marked. */
3230 vec = (offset_type *) (index->constant_pool
3231 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3232 vec_len = MAYBE_SWAP (vec[0]);
3233 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3234 {
3235 struct dwarf2_per_cu_data *per_cu;
3236 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3237 gdb_index_symbol_kind symbol_kind =
3238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3239 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3240
3241 /* Don't crash on bad data. */
3242 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3243 + dwarf2_per_objfile->n_type_units))
3244 continue;
3245
3246 /* Only check the symbol's kind if it has one.
3247 Indices prior to version 7 don't record it. */
3248 if (index->version >= 7)
3249 {
3250 switch (kind)
3251 {
3252 case VARIABLES_DOMAIN:
3253 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3254 continue;
3255 break;
3256 case FUNCTIONS_DOMAIN:
3257 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3258 continue;
3259 break;
3260 case TYPES_DOMAIN:
3261 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3262 continue;
3263 break;
3264 default:
3265 break;
3266 }
3267 }
3268
3269 per_cu = dw2_get_cu (cu_index);
3270 if (file_matcher == NULL || per_cu->v.quick->mark)
3271 dw2_instantiate_symtab (per_cu);
3272 }
3273 }
3274 }
3275
3276 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3277 symtab. */
3278
3279 static struct symtab *
3280 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3281 {
3282 int i;
3283
3284 if (BLOCKVECTOR (symtab) != NULL
3285 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3286 return symtab;
3287
3288 if (symtab->includes == NULL)
3289 return NULL;
3290
3291 for (i = 0; symtab->includes[i]; ++i)
3292 {
3293 struct symtab *s = symtab->includes[i];
3294
3295 s = recursively_find_pc_sect_symtab (s, pc);
3296 if (s != NULL)
3297 return s;
3298 }
3299
3300 return NULL;
3301 }
3302
3303 static struct symtab *
3304 dw2_find_pc_sect_symtab (struct objfile *objfile,
3305 struct minimal_symbol *msymbol,
3306 CORE_ADDR pc,
3307 struct obj_section *section,
3308 int warn_if_readin)
3309 {
3310 struct dwarf2_per_cu_data *data;
3311 struct symtab *result;
3312
3313 dw2_setup (objfile);
3314
3315 if (!objfile->psymtabs_addrmap)
3316 return NULL;
3317
3318 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3319 if (!data)
3320 return NULL;
3321
3322 if (warn_if_readin && data->v.quick->symtab)
3323 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3324 paddress (get_objfile_arch (objfile), pc));
3325
3326 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3327 gdb_assert (result != NULL);
3328 return result;
3329 }
3330
3331 static void
3332 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3333 void *data, int need_fullname)
3334 {
3335 int i;
3336 struct cleanup *cleanup;
3337 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3338 NULL, xcalloc, xfree);
3339
3340 cleanup = make_cleanup_htab_delete (visited);
3341 dw2_setup (objfile);
3342
3343 dw2_build_type_unit_groups ();
3344
3345 /* We can ignore file names coming from already-expanded CUs. */
3346 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3347 + dwarf2_per_objfile->n_type_units); ++i)
3348 {
3349 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3350
3351 if (per_cu->v.quick->symtab)
3352 {
3353 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3354 INSERT);
3355
3356 *slot = per_cu->v.quick->file_names;
3357 }
3358 }
3359
3360 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3361 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3362 {
3363 int j;
3364 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3365 struct quick_file_names *file_data;
3366 void **slot;
3367
3368 /* We only need to look at symtabs not already expanded. */
3369 if (per_cu->v.quick->symtab)
3370 continue;
3371
3372 file_data = dw2_get_file_names (objfile, per_cu);
3373 if (file_data == NULL)
3374 continue;
3375
3376 slot = htab_find_slot (visited, file_data, INSERT);
3377 if (*slot)
3378 {
3379 /* Already visited. */
3380 continue;
3381 }
3382 *slot = file_data;
3383
3384 for (j = 0; j < file_data->num_file_names; ++j)
3385 {
3386 const char *this_real_name;
3387
3388 if (need_fullname)
3389 this_real_name = dw2_get_real_path (objfile, file_data, j);
3390 else
3391 this_real_name = NULL;
3392 (*fun) (file_data->file_names[j], this_real_name, data);
3393 }
3394 }
3395
3396 do_cleanups (cleanup);
3397 }
3398
3399 static int
3400 dw2_has_symbols (struct objfile *objfile)
3401 {
3402 return 1;
3403 }
3404
3405 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3406 {
3407 dw2_has_symbols,
3408 dw2_find_last_source_symtab,
3409 dw2_forget_cached_source_info,
3410 dw2_map_symtabs_matching_filename,
3411 dw2_lookup_symbol,
3412 dw2_pre_expand_symtabs_matching,
3413 dw2_print_stats,
3414 dw2_dump,
3415 dw2_relocate,
3416 dw2_expand_symtabs_for_function,
3417 dw2_expand_all_symtabs,
3418 dw2_expand_symtabs_with_filename,
3419 dw2_find_symbol_file,
3420 dw2_map_matching_symbols,
3421 dw2_expand_symtabs_matching,
3422 dw2_find_pc_sect_symtab,
3423 dw2_map_symbol_filenames
3424 };
3425
3426 /* Initialize for reading DWARF for this objfile. Return 0 if this
3427 file will use psymtabs, or 1 if using the GNU index. */
3428
3429 int
3430 dwarf2_initialize_objfile (struct objfile *objfile)
3431 {
3432 /* If we're about to read full symbols, don't bother with the
3433 indices. In this case we also don't care if some other debug
3434 format is making psymtabs, because they are all about to be
3435 expanded anyway. */
3436 if ((objfile->flags & OBJF_READNOW))
3437 {
3438 int i;
3439
3440 dwarf2_per_objfile->using_index = 1;
3441 create_all_comp_units (objfile);
3442 create_all_type_units (objfile);
3443 dwarf2_per_objfile->quick_file_names_table =
3444 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3445
3446 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3447 + dwarf2_per_objfile->n_type_units); ++i)
3448 {
3449 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3450
3451 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3452 struct dwarf2_per_cu_quick_data);
3453 }
3454
3455 /* Return 1 so that gdb sees the "quick" functions. However,
3456 these functions will be no-ops because we will have expanded
3457 all symtabs. */
3458 return 1;
3459 }
3460
3461 if (dwarf2_read_index (objfile))
3462 return 1;
3463
3464 return 0;
3465 }
3466
3467 \f
3468
3469 /* Build a partial symbol table. */
3470
3471 void
3472 dwarf2_build_psymtabs (struct objfile *objfile)
3473 {
3474 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3475 {
3476 init_psymbol_list (objfile, 1024);
3477 }
3478
3479 dwarf2_build_psymtabs_hard (objfile);
3480 }
3481
3482 /* Return the total length of the CU described by HEADER. */
3483
3484 static unsigned int
3485 get_cu_length (const struct comp_unit_head *header)
3486 {
3487 return header->initial_length_size + header->length;
3488 }
3489
3490 /* Return TRUE if OFFSET is within CU_HEADER. */
3491
3492 static inline int
3493 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3494 {
3495 sect_offset bottom = { cu_header->offset.sect_off };
3496 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3497
3498 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3499 }
3500
3501 /* Find the base address of the compilation unit for range lists and
3502 location lists. It will normally be specified by DW_AT_low_pc.
3503 In DWARF-3 draft 4, the base address could be overridden by
3504 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3505 compilation units with discontinuous ranges. */
3506
3507 static void
3508 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3509 {
3510 struct attribute *attr;
3511
3512 cu->base_known = 0;
3513 cu->base_address = 0;
3514
3515 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3516 if (attr)
3517 {
3518 cu->base_address = DW_ADDR (attr);
3519 cu->base_known = 1;
3520 }
3521 else
3522 {
3523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3524 if (attr)
3525 {
3526 cu->base_address = DW_ADDR (attr);
3527 cu->base_known = 1;
3528 }
3529 }
3530 }
3531
3532 /* Read in the comp unit header information from the debug_info at info_ptr.
3533 NOTE: This leaves members offset, first_die_offset to be filled in
3534 by the caller. */
3535
3536 static gdb_byte *
3537 read_comp_unit_head (struct comp_unit_head *cu_header,
3538 gdb_byte *info_ptr, bfd *abfd)
3539 {
3540 int signed_addr;
3541 unsigned int bytes_read;
3542
3543 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3544 cu_header->initial_length_size = bytes_read;
3545 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3546 info_ptr += bytes_read;
3547 cu_header->version = read_2_bytes (abfd, info_ptr);
3548 info_ptr += 2;
3549 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3550 &bytes_read);
3551 info_ptr += bytes_read;
3552 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3553 info_ptr += 1;
3554 signed_addr = bfd_get_sign_extend_vma (abfd);
3555 if (signed_addr < 0)
3556 internal_error (__FILE__, __LINE__,
3557 _("read_comp_unit_head: dwarf from non elf file"));
3558 cu_header->signed_addr_p = signed_addr;
3559
3560 return info_ptr;
3561 }
3562
3563 /* Subroutine of read_and_check_comp_unit_head and
3564 read_and_check_type_unit_head to simplify them.
3565 Perform various error checking on the header. */
3566
3567 static void
3568 error_check_comp_unit_head (struct comp_unit_head *header,
3569 struct dwarf2_section_info *section,
3570 struct dwarf2_section_info *abbrev_section)
3571 {
3572 bfd *abfd = section->asection->owner;
3573 const char *filename = bfd_get_filename (abfd);
3574
3575 if (header->version != 2 && header->version != 3 && header->version != 4)
3576 error (_("Dwarf Error: wrong version in compilation unit header "
3577 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3578 filename);
3579
3580 if (header->abbrev_offset.sect_off
3581 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3582 &dwarf2_per_objfile->abbrev))
3583 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3584 "(offset 0x%lx + 6) [in module %s]"),
3585 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3586 filename);
3587
3588 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3589 avoid potential 32-bit overflow. */
3590 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3591 > section->size)
3592 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3593 "(offset 0x%lx + 0) [in module %s]"),
3594 (long) header->length, (long) header->offset.sect_off,
3595 filename);
3596 }
3597
3598 /* Read in a CU/TU header and perform some basic error checking.
3599 The contents of the header are stored in HEADER.
3600 The result is a pointer to the start of the first DIE. */
3601
3602 static gdb_byte *
3603 read_and_check_comp_unit_head (struct comp_unit_head *header,
3604 struct dwarf2_section_info *section,
3605 struct dwarf2_section_info *abbrev_section,
3606 gdb_byte *info_ptr,
3607 int is_debug_types_section)
3608 {
3609 gdb_byte *beg_of_comp_unit = info_ptr;
3610 bfd *abfd = section->asection->owner;
3611
3612 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3613
3614 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3615
3616 /* If we're reading a type unit, skip over the signature and
3617 type_offset fields. */
3618 if (is_debug_types_section)
3619 info_ptr += 8 /*signature*/ + header->offset_size;
3620
3621 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3622
3623 error_check_comp_unit_head (header, section, abbrev_section);
3624
3625 return info_ptr;
3626 }
3627
3628 /* Read in the types comp unit header information from .debug_types entry at
3629 types_ptr. The result is a pointer to one past the end of the header. */
3630
3631 static gdb_byte *
3632 read_and_check_type_unit_head (struct comp_unit_head *header,
3633 struct dwarf2_section_info *section,
3634 struct dwarf2_section_info *abbrev_section,
3635 gdb_byte *info_ptr,
3636 ULONGEST *signature,
3637 cu_offset *type_offset_in_tu)
3638 {
3639 gdb_byte *beg_of_comp_unit = info_ptr;
3640 bfd *abfd = section->asection->owner;
3641
3642 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3643
3644 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3645
3646 /* If we're reading a type unit, skip over the signature and
3647 type_offset fields. */
3648 if (signature != NULL)
3649 *signature = read_8_bytes (abfd, info_ptr);
3650 info_ptr += 8;
3651 if (type_offset_in_tu != NULL)
3652 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3653 header->offset_size);
3654 info_ptr += header->offset_size;
3655
3656 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3657
3658 error_check_comp_unit_head (header, section, abbrev_section);
3659
3660 return info_ptr;
3661 }
3662
3663 /* Fetch the abbreviation table offset from a comp or type unit header. */
3664
3665 static sect_offset
3666 read_abbrev_offset (struct dwarf2_section_info *section,
3667 sect_offset offset)
3668 {
3669 bfd *abfd = section->asection->owner;
3670 gdb_byte *info_ptr;
3671 unsigned int length, initial_length_size, offset_size;
3672 sect_offset abbrev_offset;
3673
3674 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3675 info_ptr = section->buffer + offset.sect_off;
3676 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3677 offset_size = initial_length_size == 4 ? 4 : 8;
3678 info_ptr += initial_length_size + 2 /*version*/;
3679 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3680 return abbrev_offset;
3681 }
3682
3683 /* Allocate a new partial symtab for file named NAME and mark this new
3684 partial symtab as being an include of PST. */
3685
3686 static void
3687 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3688 struct objfile *objfile)
3689 {
3690 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3691
3692 subpst->section_offsets = pst->section_offsets;
3693 subpst->textlow = 0;
3694 subpst->texthigh = 0;
3695
3696 subpst->dependencies = (struct partial_symtab **)
3697 obstack_alloc (&objfile->objfile_obstack,
3698 sizeof (struct partial_symtab *));
3699 subpst->dependencies[0] = pst;
3700 subpst->number_of_dependencies = 1;
3701
3702 subpst->globals_offset = 0;
3703 subpst->n_global_syms = 0;
3704 subpst->statics_offset = 0;
3705 subpst->n_static_syms = 0;
3706 subpst->symtab = NULL;
3707 subpst->read_symtab = pst->read_symtab;
3708 subpst->readin = 0;
3709
3710 /* No private part is necessary for include psymtabs. This property
3711 can be used to differentiate between such include psymtabs and
3712 the regular ones. */
3713 subpst->read_symtab_private = NULL;
3714 }
3715
3716 /* Read the Line Number Program data and extract the list of files
3717 included by the source file represented by PST. Build an include
3718 partial symtab for each of these included files. */
3719
3720 static void
3721 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3722 struct die_info *die,
3723 struct partial_symtab *pst)
3724 {
3725 struct line_header *lh = NULL;
3726 struct attribute *attr;
3727
3728 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3729 if (attr)
3730 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3731 if (lh == NULL)
3732 return; /* No linetable, so no includes. */
3733
3734 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3735 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3736
3737 free_line_header (lh);
3738 }
3739
3740 static hashval_t
3741 hash_signatured_type (const void *item)
3742 {
3743 const struct signatured_type *sig_type = item;
3744
3745 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3746 return sig_type->signature;
3747 }
3748
3749 static int
3750 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3751 {
3752 const struct signatured_type *lhs = item_lhs;
3753 const struct signatured_type *rhs = item_rhs;
3754
3755 return lhs->signature == rhs->signature;
3756 }
3757
3758 /* Allocate a hash table for signatured types. */
3759
3760 static htab_t
3761 allocate_signatured_type_table (struct objfile *objfile)
3762 {
3763 return htab_create_alloc_ex (41,
3764 hash_signatured_type,
3765 eq_signatured_type,
3766 NULL,
3767 &objfile->objfile_obstack,
3768 hashtab_obstack_allocate,
3769 dummy_obstack_deallocate);
3770 }
3771
3772 /* A helper function to add a signatured type CU to a table. */
3773
3774 static int
3775 add_signatured_type_cu_to_table (void **slot, void *datum)
3776 {
3777 struct signatured_type *sigt = *slot;
3778 struct signatured_type ***datap = datum;
3779
3780 **datap = sigt;
3781 ++*datap;
3782
3783 return 1;
3784 }
3785
3786 /* Create the hash table of all entries in the .debug_types section.
3787 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3788 The result is a pointer to the hash table or NULL if there are
3789 no types. */
3790
3791 static htab_t
3792 create_debug_types_hash_table (struct dwo_file *dwo_file,
3793 VEC (dwarf2_section_info_def) *types)
3794 {
3795 struct objfile *objfile = dwarf2_per_objfile->objfile;
3796 htab_t types_htab = NULL;
3797 int ix;
3798 struct dwarf2_section_info *section;
3799 struct dwarf2_section_info *abbrev_section;
3800
3801 if (VEC_empty (dwarf2_section_info_def, types))
3802 return NULL;
3803
3804 abbrev_section = (dwo_file != NULL
3805 ? &dwo_file->sections.abbrev
3806 : &dwarf2_per_objfile->abbrev);
3807
3808 if (dwarf2_read_debug)
3809 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
3810 dwo_file ? ".dwo" : "",
3811 bfd_get_filename (abbrev_section->asection->owner));
3812
3813 for (ix = 0;
3814 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3815 ++ix)
3816 {
3817 bfd *abfd;
3818 gdb_byte *info_ptr, *end_ptr;
3819
3820 dwarf2_read_section (objfile, section);
3821 info_ptr = section->buffer;
3822
3823 if (info_ptr == NULL)
3824 continue;
3825
3826 /* We can't set abfd until now because the section may be empty or
3827 not present, in which case section->asection will be NULL. */
3828 abfd = section->asection->owner;
3829
3830 if (types_htab == NULL)
3831 {
3832 if (dwo_file)
3833 types_htab = allocate_dwo_unit_table (objfile);
3834 else
3835 types_htab = allocate_signatured_type_table (objfile);
3836 }
3837
3838 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3839 because we don't need to read any dies: the signature is in the
3840 header. */
3841
3842 end_ptr = info_ptr + section->size;
3843 while (info_ptr < end_ptr)
3844 {
3845 sect_offset offset;
3846 cu_offset type_offset_in_tu;
3847 ULONGEST signature;
3848 struct signatured_type *sig_type;
3849 struct dwo_unit *dwo_tu;
3850 void **slot;
3851 gdb_byte *ptr = info_ptr;
3852 struct comp_unit_head header;
3853 unsigned int length;
3854
3855 offset.sect_off = ptr - section->buffer;
3856
3857 /* We need to read the type's signature in order to build the hash
3858 table, but we don't need anything else just yet. */
3859
3860 ptr = read_and_check_type_unit_head (&header, section,
3861 abbrev_section, ptr,
3862 &signature, &type_offset_in_tu);
3863
3864 length = get_cu_length (&header);
3865
3866 /* Skip dummy type units. */
3867 if (ptr >= info_ptr + length
3868 || peek_abbrev_code (abfd, ptr) == 0)
3869 {
3870 info_ptr += length;
3871 continue;
3872 }
3873
3874 if (dwo_file)
3875 {
3876 sig_type = NULL;
3877 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3878 struct dwo_unit);
3879 dwo_tu->dwo_file = dwo_file;
3880 dwo_tu->signature = signature;
3881 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3882 dwo_tu->info_or_types_section = section;
3883 dwo_tu->offset = offset;
3884 dwo_tu->length = length;
3885 }
3886 else
3887 {
3888 /* N.B.: type_offset is not usable if this type uses a DWO file.
3889 The real type_offset is in the DWO file. */
3890 dwo_tu = NULL;
3891 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3892 struct signatured_type);
3893 sig_type->signature = signature;
3894 sig_type->type_offset_in_tu = type_offset_in_tu;
3895 sig_type->per_cu.objfile = objfile;
3896 sig_type->per_cu.is_debug_types = 1;
3897 sig_type->per_cu.info_or_types_section = section;
3898 sig_type->per_cu.offset = offset;
3899 sig_type->per_cu.length = length;
3900 }
3901
3902 slot = htab_find_slot (types_htab,
3903 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3904 INSERT);
3905 gdb_assert (slot != NULL);
3906 if (*slot != NULL)
3907 {
3908 sect_offset dup_offset;
3909
3910 if (dwo_file)
3911 {
3912 const struct dwo_unit *dup_tu = *slot;
3913
3914 dup_offset = dup_tu->offset;
3915 }
3916 else
3917 {
3918 const struct signatured_type *dup_tu = *slot;
3919
3920 dup_offset = dup_tu->per_cu.offset;
3921 }
3922
3923 complaint (&symfile_complaints,
3924 _("debug type entry at offset 0x%x is duplicate to the "
3925 "entry at offset 0x%x, signature 0x%s"),
3926 offset.sect_off, dup_offset.sect_off,
3927 phex (signature, sizeof (signature)));
3928 }
3929 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3930
3931 if (dwarf2_read_debug)
3932 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3933 offset.sect_off,
3934 phex (signature, sizeof (signature)));
3935
3936 info_ptr += length;
3937 }
3938 }
3939
3940 return types_htab;
3941 }
3942
3943 /* Create the hash table of all entries in the .debug_types section,
3944 and initialize all_type_units.
3945 The result is zero if there is an error (e.g. missing .debug_types section),
3946 otherwise non-zero. */
3947
3948 static int
3949 create_all_type_units (struct objfile *objfile)
3950 {
3951 htab_t types_htab;
3952 struct signatured_type **iter;
3953
3954 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3955 if (types_htab == NULL)
3956 {
3957 dwarf2_per_objfile->signatured_types = NULL;
3958 return 0;
3959 }
3960
3961 dwarf2_per_objfile->signatured_types = types_htab;
3962
3963 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3964 dwarf2_per_objfile->all_type_units
3965 = obstack_alloc (&objfile->objfile_obstack,
3966 dwarf2_per_objfile->n_type_units
3967 * sizeof (struct signatured_type *));
3968 iter = &dwarf2_per_objfile->all_type_units[0];
3969 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3970 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3971 == dwarf2_per_objfile->n_type_units);
3972
3973 return 1;
3974 }
3975
3976 /* Lookup a signature based type for DW_FORM_ref_sig8.
3977 Returns NULL if signature SIG is not present in the table. */
3978
3979 static struct signatured_type *
3980 lookup_signatured_type (ULONGEST sig)
3981 {
3982 struct signatured_type find_entry, *entry;
3983
3984 if (dwarf2_per_objfile->signatured_types == NULL)
3985 {
3986 complaint (&symfile_complaints,
3987 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3988 return NULL;
3989 }
3990
3991 find_entry.signature = sig;
3992 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3993 return entry;
3994 }
3995 \f
3996 /* Low level DIE reading support. */
3997
3998 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3999
4000 static void
4001 init_cu_die_reader (struct die_reader_specs *reader,
4002 struct dwarf2_cu *cu,
4003 struct dwarf2_section_info *section,
4004 struct dwo_file *dwo_file)
4005 {
4006 gdb_assert (section->readin && section->buffer != NULL);
4007 reader->abfd = section->asection->owner;
4008 reader->cu = cu;
4009 reader->dwo_file = dwo_file;
4010 reader->die_section = section;
4011 reader->buffer = section->buffer;
4012 reader->buffer_end = section->buffer + section->size;
4013 }
4014
4015 /* Initialize a CU (or TU) and read its DIEs.
4016 If the CU defers to a DWO file, read the DWO file as well.
4017
4018 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4019 Otherwise the table specified in the comp unit header is read in and used.
4020 This is an optimization for when we already have the abbrev table.
4021
4022 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4023 Otherwise, a new CU is allocated with xmalloc.
4024
4025 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4026 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4027
4028 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4029 linker) then DIE_READER_FUNC will not get called. */
4030
4031 static void
4032 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4033 struct abbrev_table *abbrev_table,
4034 int use_existing_cu, int keep,
4035 die_reader_func_ftype *die_reader_func,
4036 void *data)
4037 {
4038 struct objfile *objfile = dwarf2_per_objfile->objfile;
4039 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4040 bfd *abfd = section->asection->owner;
4041 struct dwarf2_cu *cu;
4042 gdb_byte *begin_info_ptr, *info_ptr;
4043 struct die_reader_specs reader;
4044 struct die_info *comp_unit_die;
4045 int has_children;
4046 struct attribute *attr;
4047 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4048 struct signatured_type *sig_type = NULL;
4049 struct dwarf2_section_info *abbrev_section;
4050 /* Non-zero if CU currently points to a DWO file and we need to
4051 reread it. When this happens we need to reread the skeleton die
4052 before we can reread the DWO file. */
4053 int rereading_dwo_cu = 0;
4054
4055 if (dwarf2_die_debug)
4056 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4057 this_cu->is_debug_types ? "type" : "comp",
4058 this_cu->offset.sect_off);
4059
4060 if (use_existing_cu)
4061 gdb_assert (keep);
4062
4063 cleanups = make_cleanup (null_cleanup, NULL);
4064
4065 /* This is cheap if the section is already read in. */
4066 dwarf2_read_section (objfile, section);
4067
4068 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4069 abbrev_section = &dwarf2_per_objfile->abbrev;
4070
4071 if (use_existing_cu && this_cu->cu != NULL)
4072 {
4073 cu = this_cu->cu;
4074
4075 /* If this CU is from a DWO file we need to start over, we need to
4076 refetch the attributes from the skeleton CU.
4077 This could be optimized by retrieving those attributes from when we
4078 were here the first time: the previous comp_unit_die was stored in
4079 comp_unit_obstack. But there's no data yet that we need this
4080 optimization. */
4081 if (cu->dwo_unit != NULL)
4082 rereading_dwo_cu = 1;
4083 }
4084 else
4085 {
4086 /* If !use_existing_cu, this_cu->cu must be NULL. */
4087 gdb_assert (this_cu->cu == NULL);
4088
4089 cu = xmalloc (sizeof (*cu));
4090 init_one_comp_unit (cu, this_cu);
4091
4092 /* If an error occurs while loading, release our storage. */
4093 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4094 }
4095
4096 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4097 {
4098 /* We already have the header, there's no need to read it in again. */
4099 info_ptr += cu->header.first_die_offset.cu_off;
4100 }
4101 else
4102 {
4103 if (this_cu->is_debug_types)
4104 {
4105 ULONGEST signature;
4106 cu_offset type_offset_in_tu;
4107
4108 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4109 abbrev_section, info_ptr,
4110 &signature,
4111 &type_offset_in_tu);
4112
4113 /* Since per_cu is the first member of struct signatured_type,
4114 we can go from a pointer to one to a pointer to the other. */
4115 sig_type = (struct signatured_type *) this_cu;
4116 gdb_assert (sig_type->signature == signature);
4117 gdb_assert (sig_type->type_offset_in_tu.cu_off
4118 == type_offset_in_tu.cu_off);
4119 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4120
4121 /* LENGTH has not been set yet for type units if we're
4122 using .gdb_index. */
4123 this_cu->length = get_cu_length (&cu->header);
4124
4125 /* Establish the type offset that can be used to lookup the type. */
4126 sig_type->type_offset_in_section.sect_off =
4127 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4128 }
4129 else
4130 {
4131 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4132 abbrev_section,
4133 info_ptr, 0);
4134
4135 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4136 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4137 }
4138 }
4139
4140 /* Skip dummy compilation units. */
4141 if (info_ptr >= begin_info_ptr + this_cu->length
4142 || peek_abbrev_code (abfd, info_ptr) == 0)
4143 {
4144 do_cleanups (cleanups);
4145 return;
4146 }
4147
4148 /* If we don't have them yet, read the abbrevs for this compilation unit.
4149 And if we need to read them now, make sure they're freed when we're
4150 done. Note that it's important that if the CU had an abbrev table
4151 on entry we don't free it when we're done: Somewhere up the call stack
4152 it may be in use. */
4153 if (abbrev_table != NULL)
4154 {
4155 gdb_assert (cu->abbrev_table == NULL);
4156 gdb_assert (cu->header.abbrev_offset.sect_off
4157 == abbrev_table->offset.sect_off);
4158 cu->abbrev_table = abbrev_table;
4159 }
4160 else if (cu->abbrev_table == NULL)
4161 {
4162 dwarf2_read_abbrevs (cu, abbrev_section);
4163 make_cleanup (dwarf2_free_abbrev_table, cu);
4164 }
4165 else if (rereading_dwo_cu)
4166 {
4167 dwarf2_free_abbrev_table (cu);
4168 dwarf2_read_abbrevs (cu, abbrev_section);
4169 }
4170
4171 /* Read the top level CU/TU die. */
4172 init_cu_die_reader (&reader, cu, section, NULL);
4173 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4174
4175 /* If we have a DWO stub, process it and then read in the DWO file.
4176 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4177 a DWO CU, that this test will fail. */
4178 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4179 if (attr)
4180 {
4181 char *dwo_name = DW_STRING (attr);
4182 const char *comp_dir_string;
4183 struct dwo_unit *dwo_unit;
4184 ULONGEST signature; /* Or dwo_id. */
4185 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4186 int i,num_extra_attrs;
4187 struct dwarf2_section_info *dwo_abbrev_section;
4188
4189 if (has_children)
4190 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4191 " has children (offset 0x%x) [in module %s]"),
4192 this_cu->offset.sect_off, bfd_get_filename (abfd));
4193
4194 /* These attributes aren't processed until later:
4195 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4196 However, the attribute is found in the stub which we won't have later.
4197 In order to not impose this complication on the rest of the code,
4198 we read them here and copy them to the DWO CU/TU die. */
4199
4200 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4201 DWO file. */
4202 stmt_list = NULL;
4203 if (! this_cu->is_debug_types)
4204 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4205 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4206 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4207 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4208 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4209
4210 /* There should be a DW_AT_addr_base attribute here (if needed).
4211 We need the value before we can process DW_FORM_GNU_addr_index. */
4212 cu->addr_base = 0;
4213 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4214 if (attr)
4215 cu->addr_base = DW_UNSND (attr);
4216
4217 /* There should be a DW_AT_ranges_base attribute here (if needed).
4218 We need the value before we can process DW_AT_ranges. */
4219 cu->ranges_base = 0;
4220 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4221 if (attr)
4222 cu->ranges_base = DW_UNSND (attr);
4223
4224 if (this_cu->is_debug_types)
4225 {
4226 gdb_assert (sig_type != NULL);
4227 signature = sig_type->signature;
4228 }
4229 else
4230 {
4231 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4232 if (! attr)
4233 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4234 dwo_name);
4235 signature = DW_UNSND (attr);
4236 }
4237
4238 /* We may need the comp_dir in order to find the DWO file. */
4239 comp_dir_string = NULL;
4240 if (comp_dir)
4241 comp_dir_string = DW_STRING (comp_dir);
4242
4243 if (this_cu->is_debug_types)
4244 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4245 else
4246 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4247 signature);
4248
4249 if (dwo_unit == NULL)
4250 {
4251 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4252 " with ID %s [in module %s]"),
4253 this_cu->offset.sect_off,
4254 phex (signature, sizeof (signature)),
4255 objfile->name);
4256 }
4257
4258 /* Set up for reading the DWO CU/TU. */
4259 cu->dwo_unit = dwo_unit;
4260 section = dwo_unit->info_or_types_section;
4261 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4262 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4263 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4264
4265 if (this_cu->is_debug_types)
4266 {
4267 ULONGEST signature;
4268
4269 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4270 dwo_abbrev_section,
4271 info_ptr,
4272 &signature, NULL);
4273 gdb_assert (sig_type->signature == signature);
4274 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4275 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4276
4277 /* Establish the type offset that can be used to lookup the type.
4278 For DWO files, we don't know it until now. */
4279 sig_type->type_offset_in_section.sect_off =
4280 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4281 }
4282 else
4283 {
4284 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4285 dwo_abbrev_section,
4286 info_ptr, 0);
4287 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4288 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4289 }
4290
4291 /* Discard the original CU's abbrev table, and read the DWO's. */
4292 if (abbrev_table == NULL)
4293 {
4294 dwarf2_free_abbrev_table (cu);
4295 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4296 }
4297 else
4298 {
4299 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4300 make_cleanup (dwarf2_free_abbrev_table, cu);
4301 }
4302
4303 /* Read in the die, but leave space to copy over the attributes
4304 from the stub. This has the benefit of simplifying the rest of
4305 the code - all the real work is done here. */
4306 num_extra_attrs = ((stmt_list != NULL)
4307 + (low_pc != NULL)
4308 + (high_pc != NULL)
4309 + (ranges != NULL)
4310 + (comp_dir != NULL));
4311 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4312 &has_children, num_extra_attrs);
4313
4314 /* Copy over the attributes from the stub to the DWO die. */
4315 i = comp_unit_die->num_attrs;
4316 if (stmt_list != NULL)
4317 comp_unit_die->attrs[i++] = *stmt_list;
4318 if (low_pc != NULL)
4319 comp_unit_die->attrs[i++] = *low_pc;
4320 if (high_pc != NULL)
4321 comp_unit_die->attrs[i++] = *high_pc;
4322 if (ranges != NULL)
4323 comp_unit_die->attrs[i++] = *ranges;
4324 if (comp_dir != NULL)
4325 comp_unit_die->attrs[i++] = *comp_dir;
4326 comp_unit_die->num_attrs += num_extra_attrs;
4327
4328 /* Skip dummy compilation units. */
4329 if (info_ptr >= begin_info_ptr + dwo_unit->length
4330 || peek_abbrev_code (abfd, info_ptr) == 0)
4331 {
4332 do_cleanups (cleanups);
4333 return;
4334 }
4335 }
4336
4337 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4338
4339 if (free_cu_cleanup != NULL)
4340 {
4341 if (keep)
4342 {
4343 /* We've successfully allocated this compilation unit. Let our
4344 caller clean it up when finished with it. */
4345 discard_cleanups (free_cu_cleanup);
4346
4347 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4348 So we have to manually free the abbrev table. */
4349 dwarf2_free_abbrev_table (cu);
4350
4351 /* Link this CU into read_in_chain. */
4352 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4353 dwarf2_per_objfile->read_in_chain = this_cu;
4354 }
4355 else
4356 do_cleanups (free_cu_cleanup);
4357 }
4358
4359 do_cleanups (cleanups);
4360 }
4361
4362 /* Read CU/TU THIS_CU in section SECTION,
4363 but do not follow DW_AT_GNU_dwo_name if present.
4364 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4365 have already done the lookup to find the DWO file).
4366
4367 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4368 THIS_CU->is_debug_types, but nothing else.
4369
4370 We fill in THIS_CU->length.
4371
4372 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4373 linker) then DIE_READER_FUNC will not get called.
4374
4375 THIS_CU->cu is always freed when done.
4376 This is done in order to not leave THIS_CU->cu in a state where we have
4377 to care whether it refers to the "main" CU or the DWO CU. */
4378
4379 static void
4380 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4381 struct dwarf2_section_info *abbrev_section,
4382 struct dwo_file *dwo_file,
4383 die_reader_func_ftype *die_reader_func,
4384 void *data)
4385 {
4386 struct objfile *objfile = dwarf2_per_objfile->objfile;
4387 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4388 bfd *abfd = section->asection->owner;
4389 struct dwarf2_cu cu;
4390 gdb_byte *begin_info_ptr, *info_ptr;
4391 struct die_reader_specs reader;
4392 struct cleanup *cleanups;
4393 struct die_info *comp_unit_die;
4394 int has_children;
4395
4396 if (dwarf2_die_debug)
4397 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4398 this_cu->is_debug_types ? "type" : "comp",
4399 this_cu->offset.sect_off);
4400
4401 gdb_assert (this_cu->cu == NULL);
4402
4403 /* This is cheap if the section is already read in. */
4404 dwarf2_read_section (objfile, section);
4405
4406 init_one_comp_unit (&cu, this_cu);
4407
4408 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4409
4410 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4411 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4412 abbrev_section, info_ptr,
4413 this_cu->is_debug_types);
4414
4415 this_cu->length = get_cu_length (&cu.header);
4416
4417 /* Skip dummy compilation units. */
4418 if (info_ptr >= begin_info_ptr + this_cu->length
4419 || peek_abbrev_code (abfd, info_ptr) == 0)
4420 {
4421 do_cleanups (cleanups);
4422 return;
4423 }
4424
4425 dwarf2_read_abbrevs (&cu, abbrev_section);
4426 make_cleanup (dwarf2_free_abbrev_table, &cu);
4427
4428 init_cu_die_reader (&reader, &cu, section, dwo_file);
4429 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4430
4431 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4432
4433 do_cleanups (cleanups);
4434 }
4435
4436 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4437 does not lookup the specified DWO file.
4438 This cannot be used to read DWO files.
4439
4440 THIS_CU->cu is always freed when done.
4441 This is done in order to not leave THIS_CU->cu in a state where we have
4442 to care whether it refers to the "main" CU or the DWO CU.
4443 We can revisit this if the data shows there's a performance issue. */
4444
4445 static void
4446 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4447 die_reader_func_ftype *die_reader_func,
4448 void *data)
4449 {
4450 init_cutu_and_read_dies_no_follow (this_cu,
4451 &dwarf2_per_objfile->abbrev,
4452 NULL,
4453 die_reader_func, data);
4454 }
4455
4456 /* Create a psymtab named NAME and assign it to PER_CU.
4457
4458 The caller must fill in the following details:
4459 dirname, textlow, texthigh. */
4460
4461 static struct partial_symtab *
4462 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4463 {
4464 struct objfile *objfile = per_cu->objfile;
4465 struct partial_symtab *pst;
4466
4467 pst = start_psymtab_common (objfile, objfile->section_offsets,
4468 name, 0,
4469 objfile->global_psymbols.next,
4470 objfile->static_psymbols.next);
4471
4472 pst->psymtabs_addrmap_supported = 1;
4473
4474 /* This is the glue that links PST into GDB's symbol API. */
4475 pst->read_symtab_private = per_cu;
4476 pst->read_symtab = dwarf2_psymtab_to_symtab;
4477 per_cu->v.psymtab = pst;
4478
4479 return pst;
4480 }
4481
4482 /* die_reader_func for process_psymtab_comp_unit. */
4483
4484 static void
4485 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4486 gdb_byte *info_ptr,
4487 struct die_info *comp_unit_die,
4488 int has_children,
4489 void *data)
4490 {
4491 struct dwarf2_cu *cu = reader->cu;
4492 struct objfile *objfile = cu->objfile;
4493 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4494 struct attribute *attr;
4495 CORE_ADDR baseaddr;
4496 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4497 struct partial_symtab *pst;
4498 int has_pc_info;
4499 const char *filename;
4500 int *want_partial_unit_ptr = data;
4501
4502 if (comp_unit_die->tag == DW_TAG_partial_unit
4503 && (want_partial_unit_ptr == NULL
4504 || !*want_partial_unit_ptr))
4505 return;
4506
4507 gdb_assert (! per_cu->is_debug_types);
4508
4509 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4510
4511 cu->list_in_scope = &file_symbols;
4512
4513 /* Allocate a new partial symbol table structure. */
4514 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4515 if (attr == NULL || !DW_STRING (attr))
4516 filename = "";
4517 else
4518 filename = DW_STRING (attr);
4519
4520 pst = create_partial_symtab (per_cu, filename);
4521
4522 /* This must be done before calling dwarf2_build_include_psymtabs. */
4523 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4524 if (attr != NULL)
4525 pst->dirname = DW_STRING (attr);
4526
4527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4528
4529 dwarf2_find_base_address (comp_unit_die, cu);
4530
4531 /* Possibly set the default values of LOWPC and HIGHPC from
4532 `DW_AT_ranges'. */
4533 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4534 &best_highpc, cu, pst);
4535 if (has_pc_info == 1 && best_lowpc < best_highpc)
4536 /* Store the contiguous range if it is not empty; it can be empty for
4537 CUs with no code. */
4538 addrmap_set_empty (objfile->psymtabs_addrmap,
4539 best_lowpc + baseaddr,
4540 best_highpc + baseaddr - 1, pst);
4541
4542 /* Check if comp unit has_children.
4543 If so, read the rest of the partial symbols from this comp unit.
4544 If not, there's no more debug_info for this comp unit. */
4545 if (has_children)
4546 {
4547 struct partial_die_info *first_die;
4548 CORE_ADDR lowpc, highpc;
4549
4550 lowpc = ((CORE_ADDR) -1);
4551 highpc = ((CORE_ADDR) 0);
4552
4553 first_die = load_partial_dies (reader, info_ptr, 1);
4554
4555 scan_partial_symbols (first_die, &lowpc, &highpc,
4556 ! has_pc_info, cu);
4557
4558 /* If we didn't find a lowpc, set it to highpc to avoid
4559 complaints from `maint check'. */
4560 if (lowpc == ((CORE_ADDR) -1))
4561 lowpc = highpc;
4562
4563 /* If the compilation unit didn't have an explicit address range,
4564 then use the information extracted from its child dies. */
4565 if (! has_pc_info)
4566 {
4567 best_lowpc = lowpc;
4568 best_highpc = highpc;
4569 }
4570 }
4571 pst->textlow = best_lowpc + baseaddr;
4572 pst->texthigh = best_highpc + baseaddr;
4573
4574 pst->n_global_syms = objfile->global_psymbols.next -
4575 (objfile->global_psymbols.list + pst->globals_offset);
4576 pst->n_static_syms = objfile->static_psymbols.next -
4577 (objfile->static_psymbols.list + pst->statics_offset);
4578 sort_pst_symbols (pst);
4579
4580 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4581 {
4582 int i;
4583 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4584 struct dwarf2_per_cu_data *iter;
4585
4586 /* Fill in 'dependencies' here; we fill in 'users' in a
4587 post-pass. */
4588 pst->number_of_dependencies = len;
4589 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4590 len * sizeof (struct symtab *));
4591 for (i = 0;
4592 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4593 i, iter);
4594 ++i)
4595 pst->dependencies[i] = iter->v.psymtab;
4596
4597 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4598 }
4599
4600 /* Get the list of files included in the current compilation unit,
4601 and build a psymtab for each of them. */
4602 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4603
4604 if (dwarf2_read_debug)
4605 {
4606 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4607
4608 fprintf_unfiltered (gdb_stdlog,
4609 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4610 ", %d global, %d static syms\n",
4611 per_cu->is_debug_types ? "type" : "comp",
4612 per_cu->offset.sect_off,
4613 paddress (gdbarch, pst->textlow),
4614 paddress (gdbarch, pst->texthigh),
4615 pst->n_global_syms, pst->n_static_syms);
4616 }
4617 }
4618
4619 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4620 Process compilation unit THIS_CU for a psymtab. */
4621
4622 static void
4623 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4624 int want_partial_unit)
4625 {
4626 /* If this compilation unit was already read in, free the
4627 cached copy in order to read it in again. This is
4628 necessary because we skipped some symbols when we first
4629 read in the compilation unit (see load_partial_dies).
4630 This problem could be avoided, but the benefit is unclear. */
4631 if (this_cu->cu != NULL)
4632 free_one_cached_comp_unit (this_cu);
4633
4634 gdb_assert (! this_cu->is_debug_types);
4635 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4636 process_psymtab_comp_unit_reader,
4637 &want_partial_unit);
4638
4639 /* Age out any secondary CUs. */
4640 age_cached_comp_units ();
4641 }
4642
4643 static hashval_t
4644 hash_type_unit_group (const void *item)
4645 {
4646 const struct type_unit_group *tu_group = item;
4647
4648 return hash_stmt_list_entry (&tu_group->hash);
4649 }
4650
4651 static int
4652 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4653 {
4654 const struct type_unit_group *lhs = item_lhs;
4655 const struct type_unit_group *rhs = item_rhs;
4656
4657 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4658 }
4659
4660 /* Allocate a hash table for type unit groups. */
4661
4662 static htab_t
4663 allocate_type_unit_groups_table (void)
4664 {
4665 return htab_create_alloc_ex (3,
4666 hash_type_unit_group,
4667 eq_type_unit_group,
4668 NULL,
4669 &dwarf2_per_objfile->objfile->objfile_obstack,
4670 hashtab_obstack_allocate,
4671 dummy_obstack_deallocate);
4672 }
4673
4674 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4675 partial symtabs. We combine several TUs per psymtab to not let the size
4676 of any one psymtab grow too big. */
4677 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4678 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4679
4680 /* Helper routine for get_type_unit_group.
4681 Create the type_unit_group object used to hold one or more TUs. */
4682
4683 static struct type_unit_group *
4684 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4685 {
4686 struct objfile *objfile = dwarf2_per_objfile->objfile;
4687 struct dwarf2_per_cu_data *per_cu;
4688 struct type_unit_group *tu_group;
4689
4690 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4691 struct type_unit_group);
4692 per_cu = &tu_group->per_cu;
4693 per_cu->objfile = objfile;
4694 per_cu->is_debug_types = 1;
4695 per_cu->s.type_unit_group = tu_group;
4696
4697 if (dwarf2_per_objfile->using_index)
4698 {
4699 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4700 struct dwarf2_per_cu_quick_data);
4701 tu_group->t.first_tu = cu->per_cu;
4702 }
4703 else
4704 {
4705 unsigned int line_offset = line_offset_struct.sect_off;
4706 struct partial_symtab *pst;
4707 char *name;
4708
4709 /* Give the symtab a useful name for debug purposes. */
4710 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4711 name = xstrprintf ("<type_units_%d>",
4712 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4713 else
4714 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4715
4716 pst = create_partial_symtab (per_cu, name);
4717 pst->anonymous = 1;
4718
4719 xfree (name);
4720 }
4721
4722 tu_group->hash.dwo_unit = cu->dwo_unit;
4723 tu_group->hash.line_offset = line_offset_struct;
4724
4725 return tu_group;
4726 }
4727
4728 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4729 STMT_LIST is a DW_AT_stmt_list attribute. */
4730
4731 static struct type_unit_group *
4732 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4733 {
4734 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4735 struct type_unit_group *tu_group;
4736 void **slot;
4737 unsigned int line_offset;
4738 struct type_unit_group type_unit_group_for_lookup;
4739
4740 if (dwarf2_per_objfile->type_unit_groups == NULL)
4741 {
4742 dwarf2_per_objfile->type_unit_groups =
4743 allocate_type_unit_groups_table ();
4744 }
4745
4746 /* Do we need to create a new group, or can we use an existing one? */
4747
4748 if (stmt_list)
4749 {
4750 line_offset = DW_UNSND (stmt_list);
4751 ++tu_stats->nr_symtab_sharers;
4752 }
4753 else
4754 {
4755 /* Ugh, no stmt_list. Rare, but we have to handle it.
4756 We can do various things here like create one group per TU or
4757 spread them over multiple groups to split up the expansion work.
4758 To avoid worst case scenarios (too many groups or too large groups)
4759 we, umm, group them in bunches. */
4760 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4761 | (tu_stats->nr_stmt_less_type_units
4762 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
4763 ++tu_stats->nr_stmt_less_type_units;
4764 }
4765
4766 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
4767 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
4768 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
4769 &type_unit_group_for_lookup, INSERT);
4770 if (*slot != NULL)
4771 {
4772 tu_group = *slot;
4773 gdb_assert (tu_group != NULL);
4774 }
4775 else
4776 {
4777 sect_offset line_offset_struct;
4778
4779 line_offset_struct.sect_off = line_offset;
4780 tu_group = create_type_unit_group (cu, line_offset_struct);
4781 *slot = tu_group;
4782 ++tu_stats->nr_symtabs;
4783 }
4784
4785 return tu_group;
4786 }
4787
4788 /* Struct used to sort TUs by their abbreviation table offset. */
4789
4790 struct tu_abbrev_offset
4791 {
4792 struct signatured_type *sig_type;
4793 sect_offset abbrev_offset;
4794 };
4795
4796 /* Helper routine for build_type_unit_groups, passed to qsort. */
4797
4798 static int
4799 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
4800 {
4801 const struct tu_abbrev_offset * const *a = ap;
4802 const struct tu_abbrev_offset * const *b = bp;
4803 unsigned int aoff = (*a)->abbrev_offset.sect_off;
4804 unsigned int boff = (*b)->abbrev_offset.sect_off;
4805
4806 return (aoff > boff) - (aoff < boff);
4807 }
4808
4809 /* A helper function to add a type_unit_group to a table. */
4810
4811 static int
4812 add_type_unit_group_to_table (void **slot, void *datum)
4813 {
4814 struct type_unit_group *tu_group = *slot;
4815 struct type_unit_group ***datap = datum;
4816
4817 **datap = tu_group;
4818 ++*datap;
4819
4820 return 1;
4821 }
4822
4823 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
4824 each one passing FUNC,DATA.
4825
4826 The efficiency is because we sort TUs by the abbrev table they use and
4827 only read each abbrev table once. In one program there are 200K TUs
4828 sharing 8K abbrev tables.
4829
4830 The main purpose of this function is to support building the
4831 dwarf2_per_objfile->type_unit_groups table.
4832 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4833 can collapse the search space by grouping them by stmt_list.
4834 The savings can be significant, in the same program from above the 200K TUs
4835 share 8K stmt_list tables.
4836
4837 FUNC is expected to call get_type_unit_group, which will create the
4838 struct type_unit_group if necessary and add it to
4839 dwarf2_per_objfile->type_unit_groups. */
4840
4841 static void
4842 build_type_unit_groups (die_reader_func_ftype *func, void *data)
4843 {
4844 struct objfile *objfile = dwarf2_per_objfile->objfile;
4845 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4846 struct cleanup *cleanups;
4847 struct abbrev_table *abbrev_table;
4848 sect_offset abbrev_offset;
4849 struct tu_abbrev_offset *sorted_by_abbrev;
4850 struct type_unit_group **iter;
4851 int i;
4852
4853 /* It's up to the caller to not call us multiple times. */
4854 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
4855
4856 if (dwarf2_per_objfile->n_type_units == 0)
4857 return;
4858
4859 /* TUs typically share abbrev tables, and there can be way more TUs than
4860 abbrev tables. Sort by abbrev table to reduce the number of times we
4861 read each abbrev table in.
4862 Alternatives are to punt or to maintain a cache of abbrev tables.
4863 This is simpler and efficient enough for now.
4864
4865 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4866 symtab to use). Typically TUs with the same abbrev offset have the same
4867 stmt_list value too so in practice this should work well.
4868
4869 The basic algorithm here is:
4870
4871 sort TUs by abbrev table
4872 for each TU with same abbrev table:
4873 read abbrev table if first user
4874 read TU top level DIE
4875 [IWBN if DWO skeletons had DW_AT_stmt_list]
4876 call FUNC */
4877
4878 if (dwarf2_read_debug)
4879 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
4880
4881 /* Sort in a separate table to maintain the order of all_type_units
4882 for .gdb_index: TU indices directly index all_type_units. */
4883 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
4884 dwarf2_per_objfile->n_type_units);
4885 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
4886 {
4887 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
4888
4889 sorted_by_abbrev[i].sig_type = sig_type;
4890 sorted_by_abbrev[i].abbrev_offset =
4891 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
4892 sig_type->per_cu.offset);
4893 }
4894 cleanups = make_cleanup (xfree, sorted_by_abbrev);
4895 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
4896 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
4897
4898 /* Note: In the .gdb_index case, get_type_unit_group may have already been
4899 called any number of times, so we don't reset tu_stats here. */
4900
4901 abbrev_offset.sect_off = ~(unsigned) 0;
4902 abbrev_table = NULL;
4903 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
4904
4905 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
4906 {
4907 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
4908
4909 /* Switch to the next abbrev table if necessary. */
4910 if (abbrev_table == NULL
4911 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
4912 {
4913 if (abbrev_table != NULL)
4914 {
4915 abbrev_table_free (abbrev_table);
4916 /* Reset to NULL in case abbrev_table_read_table throws
4917 an error: abbrev_table_free_cleanup will get called. */
4918 abbrev_table = NULL;
4919 }
4920 abbrev_offset = tu->abbrev_offset;
4921 abbrev_table =
4922 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
4923 abbrev_offset);
4924 ++tu_stats->nr_uniq_abbrev_tables;
4925 }
4926
4927 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
4928 func, data);
4929 }
4930
4931 /* Create a vector of pointers to primary type units to make it easy to
4932 iterate over them and CUs. See dw2_get_primary_cu. */
4933 dwarf2_per_objfile->n_type_unit_groups =
4934 htab_elements (dwarf2_per_objfile->type_unit_groups);
4935 dwarf2_per_objfile->all_type_unit_groups =
4936 obstack_alloc (&objfile->objfile_obstack,
4937 dwarf2_per_objfile->n_type_unit_groups
4938 * sizeof (struct type_unit_group *));
4939 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
4940 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
4941 add_type_unit_group_to_table, &iter);
4942 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
4943 == dwarf2_per_objfile->n_type_unit_groups);
4944
4945 do_cleanups (cleanups);
4946
4947 if (dwarf2_read_debug)
4948 {
4949 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
4950 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
4951 dwarf2_per_objfile->n_type_units);
4952 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
4953 tu_stats->nr_uniq_abbrev_tables);
4954 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
4955 tu_stats->nr_symtabs);
4956 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
4957 tu_stats->nr_symtab_sharers);
4958 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
4959 tu_stats->nr_stmt_less_type_units);
4960 }
4961 }
4962
4963 /* Reader function for build_type_psymtabs. */
4964
4965 static void
4966 build_type_psymtabs_reader (const struct die_reader_specs *reader,
4967 gdb_byte *info_ptr,
4968 struct die_info *type_unit_die,
4969 int has_children,
4970 void *data)
4971 {
4972 struct objfile *objfile = dwarf2_per_objfile->objfile;
4973 struct dwarf2_cu *cu = reader->cu;
4974 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4975 struct type_unit_group *tu_group;
4976 struct attribute *attr;
4977 struct partial_die_info *first_die;
4978 CORE_ADDR lowpc, highpc;
4979 struct partial_symtab *pst;
4980
4981 gdb_assert (data == NULL);
4982
4983 if (! has_children)
4984 return;
4985
4986 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
4987 tu_group = get_type_unit_group (cu, attr);
4988
4989 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
4990
4991 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
4992 cu->list_in_scope = &file_symbols;
4993 pst = create_partial_symtab (per_cu, "");
4994 pst->anonymous = 1;
4995
4996 first_die = load_partial_dies (reader, info_ptr, 1);
4997
4998 lowpc = (CORE_ADDR) -1;
4999 highpc = (CORE_ADDR) 0;
5000 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5001
5002 pst->n_global_syms = objfile->global_psymbols.next -
5003 (objfile->global_psymbols.list + pst->globals_offset);
5004 pst->n_static_syms = objfile->static_psymbols.next -
5005 (objfile->static_psymbols.list + pst->statics_offset);
5006 sort_pst_symbols (pst);
5007 }
5008
5009 /* Traversal function for build_type_psymtabs. */
5010
5011 static int
5012 build_type_psymtab_dependencies (void **slot, void *info)
5013 {
5014 struct objfile *objfile = dwarf2_per_objfile->objfile;
5015 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5016 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5017 struct partial_symtab *pst = per_cu->v.psymtab;
5018 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5019 struct dwarf2_per_cu_data *iter;
5020 int i;
5021
5022 gdb_assert (len > 0);
5023
5024 pst->number_of_dependencies = len;
5025 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5026 len * sizeof (struct psymtab *));
5027 for (i = 0;
5028 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5029 ++i)
5030 {
5031 pst->dependencies[i] = iter->v.psymtab;
5032 iter->s.type_unit_group = tu_group;
5033 }
5034
5035 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5036
5037 return 1;
5038 }
5039
5040 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5041 Build partial symbol tables for the .debug_types comp-units. */
5042
5043 static void
5044 build_type_psymtabs (struct objfile *objfile)
5045 {
5046 if (! create_all_type_units (objfile))
5047 return;
5048
5049 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5050
5051 /* Now that all TUs have been processed we can fill in the dependencies. */
5052 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5053 build_type_psymtab_dependencies, NULL);
5054 }
5055
5056 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5057
5058 static void
5059 psymtabs_addrmap_cleanup (void *o)
5060 {
5061 struct objfile *objfile = o;
5062
5063 objfile->psymtabs_addrmap = NULL;
5064 }
5065
5066 /* Compute the 'user' field for each psymtab in OBJFILE. */
5067
5068 static void
5069 set_partial_user (struct objfile *objfile)
5070 {
5071 int i;
5072
5073 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5074 {
5075 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5076 struct partial_symtab *pst = per_cu->v.psymtab;
5077 int j;
5078
5079 for (j = 0; j < pst->number_of_dependencies; ++j)
5080 {
5081 /* Set the 'user' field only if it is not already set. */
5082 if (pst->dependencies[j]->user == NULL)
5083 pst->dependencies[j]->user = pst;
5084 }
5085 }
5086 }
5087
5088 /* Build the partial symbol table by doing a quick pass through the
5089 .debug_info and .debug_abbrev sections. */
5090
5091 static void
5092 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5093 {
5094 struct cleanup *back_to, *addrmap_cleanup;
5095 struct obstack temp_obstack;
5096 int i;
5097
5098 if (dwarf2_read_debug)
5099 {
5100 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5101 objfile->name);
5102 }
5103
5104 dwarf2_per_objfile->reading_partial_symbols = 1;
5105
5106 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5107
5108 /* Any cached compilation units will be linked by the per-objfile
5109 read_in_chain. Make sure to free them when we're done. */
5110 back_to = make_cleanup (free_cached_comp_units, NULL);
5111
5112 build_type_psymtabs (objfile);
5113
5114 create_all_comp_units (objfile);
5115
5116 /* Create a temporary address map on a temporary obstack. We later
5117 copy this to the final obstack. */
5118 obstack_init (&temp_obstack);
5119 make_cleanup_obstack_free (&temp_obstack);
5120 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5121 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5122
5123 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5124 {
5125 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5126
5127 process_psymtab_comp_unit (per_cu, 0);
5128 }
5129
5130 set_partial_user (objfile);
5131
5132 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5133 &objfile->objfile_obstack);
5134 discard_cleanups (addrmap_cleanup);
5135
5136 do_cleanups (back_to);
5137
5138 if (dwarf2_read_debug)
5139 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5140 objfile->name);
5141 }
5142
5143 /* die_reader_func for load_partial_comp_unit. */
5144
5145 static void
5146 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5147 gdb_byte *info_ptr,
5148 struct die_info *comp_unit_die,
5149 int has_children,
5150 void *data)
5151 {
5152 struct dwarf2_cu *cu = reader->cu;
5153
5154 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5155
5156 /* Check if comp unit has_children.
5157 If so, read the rest of the partial symbols from this comp unit.
5158 If not, there's no more debug_info for this comp unit. */
5159 if (has_children)
5160 load_partial_dies (reader, info_ptr, 0);
5161 }
5162
5163 /* Load the partial DIEs for a secondary CU into memory.
5164 This is also used when rereading a primary CU with load_all_dies. */
5165
5166 static void
5167 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5168 {
5169 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5170 load_partial_comp_unit_reader, NULL);
5171 }
5172
5173 /* Create a list of all compilation units in OBJFILE.
5174 This is only done for -readnow and building partial symtabs. */
5175
5176 static void
5177 create_all_comp_units (struct objfile *objfile)
5178 {
5179 int n_allocated;
5180 int n_comp_units;
5181 struct dwarf2_per_cu_data **all_comp_units;
5182 gdb_byte *info_ptr;
5183
5184 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5185 info_ptr = dwarf2_per_objfile->info.buffer;
5186
5187 n_comp_units = 0;
5188 n_allocated = 10;
5189 all_comp_units = xmalloc (n_allocated
5190 * sizeof (struct dwarf2_per_cu_data *));
5191
5192 while (info_ptr < dwarf2_per_objfile->info.buffer
5193 + dwarf2_per_objfile->info.size)
5194 {
5195 unsigned int length, initial_length_size;
5196 struct dwarf2_per_cu_data *this_cu;
5197 sect_offset offset;
5198
5199 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
5200
5201 /* Read just enough information to find out where the next
5202 compilation unit is. */
5203 length = read_initial_length (objfile->obfd, info_ptr,
5204 &initial_length_size);
5205
5206 /* Save the compilation unit for later lookup. */
5207 this_cu = obstack_alloc (&objfile->objfile_obstack,
5208 sizeof (struct dwarf2_per_cu_data));
5209 memset (this_cu, 0, sizeof (*this_cu));
5210 this_cu->offset = offset;
5211 this_cu->length = length + initial_length_size;
5212 this_cu->objfile = objfile;
5213 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
5214
5215 if (n_comp_units == n_allocated)
5216 {
5217 n_allocated *= 2;
5218 all_comp_units = xrealloc (all_comp_units,
5219 n_allocated
5220 * sizeof (struct dwarf2_per_cu_data *));
5221 }
5222 all_comp_units[n_comp_units++] = this_cu;
5223
5224 info_ptr = info_ptr + this_cu->length;
5225 }
5226
5227 dwarf2_per_objfile->all_comp_units
5228 = obstack_alloc (&objfile->objfile_obstack,
5229 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5230 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5231 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5232 xfree (all_comp_units);
5233 dwarf2_per_objfile->n_comp_units = n_comp_units;
5234 }
5235
5236 /* Process all loaded DIEs for compilation unit CU, starting at
5237 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5238 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5239 DW_AT_ranges). If NEED_PC is set, then this function will set
5240 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5241 and record the covered ranges in the addrmap. */
5242
5243 static void
5244 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5245 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5246 {
5247 struct partial_die_info *pdi;
5248
5249 /* Now, march along the PDI's, descending into ones which have
5250 interesting children but skipping the children of the other ones,
5251 until we reach the end of the compilation unit. */
5252
5253 pdi = first_die;
5254
5255 while (pdi != NULL)
5256 {
5257 fixup_partial_die (pdi, cu);
5258
5259 /* Anonymous namespaces or modules have no name but have interesting
5260 children, so we need to look at them. Ditto for anonymous
5261 enums. */
5262
5263 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5264 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5265 || pdi->tag == DW_TAG_imported_unit)
5266 {
5267 switch (pdi->tag)
5268 {
5269 case DW_TAG_subprogram:
5270 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5271 break;
5272 case DW_TAG_constant:
5273 case DW_TAG_variable:
5274 case DW_TAG_typedef:
5275 case DW_TAG_union_type:
5276 if (!pdi->is_declaration)
5277 {
5278 add_partial_symbol (pdi, cu);
5279 }
5280 break;
5281 case DW_TAG_class_type:
5282 case DW_TAG_interface_type:
5283 case DW_TAG_structure_type:
5284 if (!pdi->is_declaration)
5285 {
5286 add_partial_symbol (pdi, cu);
5287 }
5288 break;
5289 case DW_TAG_enumeration_type:
5290 if (!pdi->is_declaration)
5291 add_partial_enumeration (pdi, cu);
5292 break;
5293 case DW_TAG_base_type:
5294 case DW_TAG_subrange_type:
5295 /* File scope base type definitions are added to the partial
5296 symbol table. */
5297 add_partial_symbol (pdi, cu);
5298 break;
5299 case DW_TAG_namespace:
5300 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5301 break;
5302 case DW_TAG_module:
5303 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5304 break;
5305 case DW_TAG_imported_unit:
5306 {
5307 struct dwarf2_per_cu_data *per_cu;
5308
5309 /* For now we don't handle imported units in type units. */
5310 if (cu->per_cu->is_debug_types)
5311 {
5312 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5313 " supported in type units [in module %s]"),
5314 cu->objfile->name);
5315 }
5316
5317 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5318 cu->objfile);
5319
5320 /* Go read the partial unit, if needed. */
5321 if (per_cu->v.psymtab == NULL)
5322 process_psymtab_comp_unit (per_cu, 1);
5323
5324 VEC_safe_push (dwarf2_per_cu_ptr,
5325 cu->per_cu->s.imported_symtabs, per_cu);
5326 }
5327 break;
5328 default:
5329 break;
5330 }
5331 }
5332
5333 /* If the die has a sibling, skip to the sibling. */
5334
5335 pdi = pdi->die_sibling;
5336 }
5337 }
5338
5339 /* Functions used to compute the fully scoped name of a partial DIE.
5340
5341 Normally, this is simple. For C++, the parent DIE's fully scoped
5342 name is concatenated with "::" and the partial DIE's name. For
5343 Java, the same thing occurs except that "." is used instead of "::".
5344 Enumerators are an exception; they use the scope of their parent
5345 enumeration type, i.e. the name of the enumeration type is not
5346 prepended to the enumerator.
5347
5348 There are two complexities. One is DW_AT_specification; in this
5349 case "parent" means the parent of the target of the specification,
5350 instead of the direct parent of the DIE. The other is compilers
5351 which do not emit DW_TAG_namespace; in this case we try to guess
5352 the fully qualified name of structure types from their members'
5353 linkage names. This must be done using the DIE's children rather
5354 than the children of any DW_AT_specification target. We only need
5355 to do this for structures at the top level, i.e. if the target of
5356 any DW_AT_specification (if any; otherwise the DIE itself) does not
5357 have a parent. */
5358
5359 /* Compute the scope prefix associated with PDI's parent, in
5360 compilation unit CU. The result will be allocated on CU's
5361 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5362 field. NULL is returned if no prefix is necessary. */
5363 static char *
5364 partial_die_parent_scope (struct partial_die_info *pdi,
5365 struct dwarf2_cu *cu)
5366 {
5367 char *grandparent_scope;
5368 struct partial_die_info *parent, *real_pdi;
5369
5370 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5371 then this means the parent of the specification DIE. */
5372
5373 real_pdi = pdi;
5374 while (real_pdi->has_specification)
5375 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
5376
5377 parent = real_pdi->die_parent;
5378 if (parent == NULL)
5379 return NULL;
5380
5381 if (parent->scope_set)
5382 return parent->scope;
5383
5384 fixup_partial_die (parent, cu);
5385
5386 grandparent_scope = partial_die_parent_scope (parent, cu);
5387
5388 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5389 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5390 Work around this problem here. */
5391 if (cu->language == language_cplus
5392 && parent->tag == DW_TAG_namespace
5393 && strcmp (parent->name, "::") == 0
5394 && grandparent_scope == NULL)
5395 {
5396 parent->scope = NULL;
5397 parent->scope_set = 1;
5398 return NULL;
5399 }
5400
5401 if (pdi->tag == DW_TAG_enumerator)
5402 /* Enumerators should not get the name of the enumeration as a prefix. */
5403 parent->scope = grandparent_scope;
5404 else if (parent->tag == DW_TAG_namespace
5405 || parent->tag == DW_TAG_module
5406 || parent->tag == DW_TAG_structure_type
5407 || parent->tag == DW_TAG_class_type
5408 || parent->tag == DW_TAG_interface_type
5409 || parent->tag == DW_TAG_union_type
5410 || parent->tag == DW_TAG_enumeration_type)
5411 {
5412 if (grandparent_scope == NULL)
5413 parent->scope = parent->name;
5414 else
5415 parent->scope = typename_concat (&cu->comp_unit_obstack,
5416 grandparent_scope,
5417 parent->name, 0, cu);
5418 }
5419 else
5420 {
5421 /* FIXME drow/2004-04-01: What should we be doing with
5422 function-local names? For partial symbols, we should probably be
5423 ignoring them. */
5424 complaint (&symfile_complaints,
5425 _("unhandled containing DIE tag %d for DIE at %d"),
5426 parent->tag, pdi->offset.sect_off);
5427 parent->scope = grandparent_scope;
5428 }
5429
5430 parent->scope_set = 1;
5431 return parent->scope;
5432 }
5433
5434 /* Return the fully scoped name associated with PDI, from compilation unit
5435 CU. The result will be allocated with malloc. */
5436
5437 static char *
5438 partial_die_full_name (struct partial_die_info *pdi,
5439 struct dwarf2_cu *cu)
5440 {
5441 char *parent_scope;
5442
5443 /* If this is a template instantiation, we can not work out the
5444 template arguments from partial DIEs. So, unfortunately, we have
5445 to go through the full DIEs. At least any work we do building
5446 types here will be reused if full symbols are loaded later. */
5447 if (pdi->has_template_arguments)
5448 {
5449 fixup_partial_die (pdi, cu);
5450
5451 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5452 {
5453 struct die_info *die;
5454 struct attribute attr;
5455 struct dwarf2_cu *ref_cu = cu;
5456
5457 /* DW_FORM_ref_addr is using section offset. */
5458 attr.name = 0;
5459 attr.form = DW_FORM_ref_addr;
5460 attr.u.unsnd = pdi->offset.sect_off;
5461 die = follow_die_ref (NULL, &attr, &ref_cu);
5462
5463 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5464 }
5465 }
5466
5467 parent_scope = partial_die_parent_scope (pdi, cu);
5468 if (parent_scope == NULL)
5469 return NULL;
5470 else
5471 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5472 }
5473
5474 static void
5475 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5476 {
5477 struct objfile *objfile = cu->objfile;
5478 CORE_ADDR addr = 0;
5479 char *actual_name = NULL;
5480 CORE_ADDR baseaddr;
5481 int built_actual_name = 0;
5482
5483 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5484
5485 actual_name = partial_die_full_name (pdi, cu);
5486 if (actual_name)
5487 built_actual_name = 1;
5488
5489 if (actual_name == NULL)
5490 actual_name = pdi->name;
5491
5492 switch (pdi->tag)
5493 {
5494 case DW_TAG_subprogram:
5495 if (pdi->is_external || cu->language == language_ada)
5496 {
5497 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5498 of the global scope. But in Ada, we want to be able to access
5499 nested procedures globally. So all Ada subprograms are stored
5500 in the global scope. */
5501 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5502 mst_text, objfile); */
5503 add_psymbol_to_list (actual_name, strlen (actual_name),
5504 built_actual_name,
5505 VAR_DOMAIN, LOC_BLOCK,
5506 &objfile->global_psymbols,
5507 0, pdi->lowpc + baseaddr,
5508 cu->language, objfile);
5509 }
5510 else
5511 {
5512 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5513 mst_file_text, objfile); */
5514 add_psymbol_to_list (actual_name, strlen (actual_name),
5515 built_actual_name,
5516 VAR_DOMAIN, LOC_BLOCK,
5517 &objfile->static_psymbols,
5518 0, pdi->lowpc + baseaddr,
5519 cu->language, objfile);
5520 }
5521 break;
5522 case DW_TAG_constant:
5523 {
5524 struct psymbol_allocation_list *list;
5525
5526 if (pdi->is_external)
5527 list = &objfile->global_psymbols;
5528 else
5529 list = &objfile->static_psymbols;
5530 add_psymbol_to_list (actual_name, strlen (actual_name),
5531 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5532 list, 0, 0, cu->language, objfile);
5533 }
5534 break;
5535 case DW_TAG_variable:
5536 if (pdi->d.locdesc)
5537 addr = decode_locdesc (pdi->d.locdesc, cu);
5538
5539 if (pdi->d.locdesc
5540 && addr == 0
5541 && !dwarf2_per_objfile->has_section_at_zero)
5542 {
5543 /* A global or static variable may also have been stripped
5544 out by the linker if unused, in which case its address
5545 will be nullified; do not add such variables into partial
5546 symbol table then. */
5547 }
5548 else if (pdi->is_external)
5549 {
5550 /* Global Variable.
5551 Don't enter into the minimal symbol tables as there is
5552 a minimal symbol table entry from the ELF symbols already.
5553 Enter into partial symbol table if it has a location
5554 descriptor or a type.
5555 If the location descriptor is missing, new_symbol will create
5556 a LOC_UNRESOLVED symbol, the address of the variable will then
5557 be determined from the minimal symbol table whenever the variable
5558 is referenced.
5559 The address for the partial symbol table entry is not
5560 used by GDB, but it comes in handy for debugging partial symbol
5561 table building. */
5562
5563 if (pdi->d.locdesc || pdi->has_type)
5564 add_psymbol_to_list (actual_name, strlen (actual_name),
5565 built_actual_name,
5566 VAR_DOMAIN, LOC_STATIC,
5567 &objfile->global_psymbols,
5568 0, addr + baseaddr,
5569 cu->language, objfile);
5570 }
5571 else
5572 {
5573 /* Static Variable. Skip symbols without location descriptors. */
5574 if (pdi->d.locdesc == NULL)
5575 {
5576 if (built_actual_name)
5577 xfree (actual_name);
5578 return;
5579 }
5580 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5581 mst_file_data, objfile); */
5582 add_psymbol_to_list (actual_name, strlen (actual_name),
5583 built_actual_name,
5584 VAR_DOMAIN, LOC_STATIC,
5585 &objfile->static_psymbols,
5586 0, addr + baseaddr,
5587 cu->language, objfile);
5588 }
5589 break;
5590 case DW_TAG_typedef:
5591 case DW_TAG_base_type:
5592 case DW_TAG_subrange_type:
5593 add_psymbol_to_list (actual_name, strlen (actual_name),
5594 built_actual_name,
5595 VAR_DOMAIN, LOC_TYPEDEF,
5596 &objfile->static_psymbols,
5597 0, (CORE_ADDR) 0, cu->language, objfile);
5598 break;
5599 case DW_TAG_namespace:
5600 add_psymbol_to_list (actual_name, strlen (actual_name),
5601 built_actual_name,
5602 VAR_DOMAIN, LOC_TYPEDEF,
5603 &objfile->global_psymbols,
5604 0, (CORE_ADDR) 0, cu->language, objfile);
5605 break;
5606 case DW_TAG_class_type:
5607 case DW_TAG_interface_type:
5608 case DW_TAG_structure_type:
5609 case DW_TAG_union_type:
5610 case DW_TAG_enumeration_type:
5611 /* Skip external references. The DWARF standard says in the section
5612 about "Structure, Union, and Class Type Entries": "An incomplete
5613 structure, union or class type is represented by a structure,
5614 union or class entry that does not have a byte size attribute
5615 and that has a DW_AT_declaration attribute." */
5616 if (!pdi->has_byte_size && pdi->is_declaration)
5617 {
5618 if (built_actual_name)
5619 xfree (actual_name);
5620 return;
5621 }
5622
5623 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5624 static vs. global. */
5625 add_psymbol_to_list (actual_name, strlen (actual_name),
5626 built_actual_name,
5627 STRUCT_DOMAIN, LOC_TYPEDEF,
5628 (cu->language == language_cplus
5629 || cu->language == language_java)
5630 ? &objfile->global_psymbols
5631 : &objfile->static_psymbols,
5632 0, (CORE_ADDR) 0, cu->language, objfile);
5633
5634 break;
5635 case DW_TAG_enumerator:
5636 add_psymbol_to_list (actual_name, strlen (actual_name),
5637 built_actual_name,
5638 VAR_DOMAIN, LOC_CONST,
5639 (cu->language == language_cplus
5640 || cu->language == language_java)
5641 ? &objfile->global_psymbols
5642 : &objfile->static_psymbols,
5643 0, (CORE_ADDR) 0, cu->language, objfile);
5644 break;
5645 default:
5646 break;
5647 }
5648
5649 if (built_actual_name)
5650 xfree (actual_name);
5651 }
5652
5653 /* Read a partial die corresponding to a namespace; also, add a symbol
5654 corresponding to that namespace to the symbol table. NAMESPACE is
5655 the name of the enclosing namespace. */
5656
5657 static void
5658 add_partial_namespace (struct partial_die_info *pdi,
5659 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5660 int need_pc, struct dwarf2_cu *cu)
5661 {
5662 /* Add a symbol for the namespace. */
5663
5664 add_partial_symbol (pdi, cu);
5665
5666 /* Now scan partial symbols in that namespace. */
5667
5668 if (pdi->has_children)
5669 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5670 }
5671
5672 /* Read a partial die corresponding to a Fortran module. */
5673
5674 static void
5675 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5676 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5677 {
5678 /* Now scan partial symbols in that module. */
5679
5680 if (pdi->has_children)
5681 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5682 }
5683
5684 /* Read a partial die corresponding to a subprogram and create a partial
5685 symbol for that subprogram. When the CU language allows it, this
5686 routine also defines a partial symbol for each nested subprogram
5687 that this subprogram contains.
5688
5689 DIE my also be a lexical block, in which case we simply search
5690 recursively for suprograms defined inside that lexical block.
5691 Again, this is only performed when the CU language allows this
5692 type of definitions. */
5693
5694 static void
5695 add_partial_subprogram (struct partial_die_info *pdi,
5696 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5697 int need_pc, struct dwarf2_cu *cu)
5698 {
5699 if (pdi->tag == DW_TAG_subprogram)
5700 {
5701 if (pdi->has_pc_info)
5702 {
5703 if (pdi->lowpc < *lowpc)
5704 *lowpc = pdi->lowpc;
5705 if (pdi->highpc > *highpc)
5706 *highpc = pdi->highpc;
5707 if (need_pc)
5708 {
5709 CORE_ADDR baseaddr;
5710 struct objfile *objfile = cu->objfile;
5711
5712 baseaddr = ANOFFSET (objfile->section_offsets,
5713 SECT_OFF_TEXT (objfile));
5714 addrmap_set_empty (objfile->psymtabs_addrmap,
5715 pdi->lowpc + baseaddr,
5716 pdi->highpc - 1 + baseaddr,
5717 cu->per_cu->v.psymtab);
5718 }
5719 }
5720
5721 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5722 {
5723 if (!pdi->is_declaration)
5724 /* Ignore subprogram DIEs that do not have a name, they are
5725 illegal. Do not emit a complaint at this point, we will
5726 do so when we convert this psymtab into a symtab. */
5727 if (pdi->name)
5728 add_partial_symbol (pdi, cu);
5729 }
5730 }
5731
5732 if (! pdi->has_children)
5733 return;
5734
5735 if (cu->language == language_ada)
5736 {
5737 pdi = pdi->die_child;
5738 while (pdi != NULL)
5739 {
5740 fixup_partial_die (pdi, cu);
5741 if (pdi->tag == DW_TAG_subprogram
5742 || pdi->tag == DW_TAG_lexical_block)
5743 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5744 pdi = pdi->die_sibling;
5745 }
5746 }
5747 }
5748
5749 /* Read a partial die corresponding to an enumeration type. */
5750
5751 static void
5752 add_partial_enumeration (struct partial_die_info *enum_pdi,
5753 struct dwarf2_cu *cu)
5754 {
5755 struct partial_die_info *pdi;
5756
5757 if (enum_pdi->name != NULL)
5758 add_partial_symbol (enum_pdi, cu);
5759
5760 pdi = enum_pdi->die_child;
5761 while (pdi)
5762 {
5763 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
5764 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5765 else
5766 add_partial_symbol (pdi, cu);
5767 pdi = pdi->die_sibling;
5768 }
5769 }
5770
5771 /* Return the initial uleb128 in the die at INFO_PTR. */
5772
5773 static unsigned int
5774 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5775 {
5776 unsigned int bytes_read;
5777
5778 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5779 }
5780
5781 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5782 Return the corresponding abbrev, or NULL if the number is zero (indicating
5783 an empty DIE). In either case *BYTES_READ will be set to the length of
5784 the initial number. */
5785
5786 static struct abbrev_info *
5787 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5788 struct dwarf2_cu *cu)
5789 {
5790 bfd *abfd = cu->objfile->obfd;
5791 unsigned int abbrev_number;
5792 struct abbrev_info *abbrev;
5793
5794 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5795
5796 if (abbrev_number == 0)
5797 return NULL;
5798
5799 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
5800 if (!abbrev)
5801 {
5802 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5803 abbrev_number, bfd_get_filename (abfd));
5804 }
5805
5806 return abbrev;
5807 }
5808
5809 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5810 Returns a pointer to the end of a series of DIEs, terminated by an empty
5811 DIE. Any children of the skipped DIEs will also be skipped. */
5812
5813 static gdb_byte *
5814 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5815 {
5816 struct dwarf2_cu *cu = reader->cu;
5817 struct abbrev_info *abbrev;
5818 unsigned int bytes_read;
5819
5820 while (1)
5821 {
5822 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5823 if (abbrev == NULL)
5824 return info_ptr + bytes_read;
5825 else
5826 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5827 }
5828 }
5829
5830 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5831 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5832 abbrev corresponding to that skipped uleb128 should be passed in
5833 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5834 children. */
5835
5836 static gdb_byte *
5837 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5838 struct abbrev_info *abbrev)
5839 {
5840 unsigned int bytes_read;
5841 struct attribute attr;
5842 bfd *abfd = reader->abfd;
5843 struct dwarf2_cu *cu = reader->cu;
5844 gdb_byte *buffer = reader->buffer;
5845 const gdb_byte *buffer_end = reader->buffer_end;
5846 gdb_byte *start_info_ptr = info_ptr;
5847 unsigned int form, i;
5848
5849 for (i = 0; i < abbrev->num_attrs; i++)
5850 {
5851 /* The only abbrev we care about is DW_AT_sibling. */
5852 if (abbrev->attrs[i].name == DW_AT_sibling)
5853 {
5854 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5855 if (attr.form == DW_FORM_ref_addr)
5856 complaint (&symfile_complaints,
5857 _("ignoring absolute DW_AT_sibling"));
5858 else
5859 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5860 }
5861
5862 /* If it isn't DW_AT_sibling, skip this attribute. */
5863 form = abbrev->attrs[i].form;
5864 skip_attribute:
5865 switch (form)
5866 {
5867 case DW_FORM_ref_addr:
5868 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5869 and later it is offset sized. */
5870 if (cu->header.version == 2)
5871 info_ptr += cu->header.addr_size;
5872 else
5873 info_ptr += cu->header.offset_size;
5874 break;
5875 case DW_FORM_addr:
5876 info_ptr += cu->header.addr_size;
5877 break;
5878 case DW_FORM_data1:
5879 case DW_FORM_ref1:
5880 case DW_FORM_flag:
5881 info_ptr += 1;
5882 break;
5883 case DW_FORM_flag_present:
5884 break;
5885 case DW_FORM_data2:
5886 case DW_FORM_ref2:
5887 info_ptr += 2;
5888 break;
5889 case DW_FORM_data4:
5890 case DW_FORM_ref4:
5891 info_ptr += 4;
5892 break;
5893 case DW_FORM_data8:
5894 case DW_FORM_ref8:
5895 case DW_FORM_ref_sig8:
5896 info_ptr += 8;
5897 break;
5898 case DW_FORM_string:
5899 read_direct_string (abfd, info_ptr, &bytes_read);
5900 info_ptr += bytes_read;
5901 break;
5902 case DW_FORM_sec_offset:
5903 case DW_FORM_strp:
5904 info_ptr += cu->header.offset_size;
5905 break;
5906 case DW_FORM_exprloc:
5907 case DW_FORM_block:
5908 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5909 info_ptr += bytes_read;
5910 break;
5911 case DW_FORM_block1:
5912 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5913 break;
5914 case DW_FORM_block2:
5915 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5916 break;
5917 case DW_FORM_block4:
5918 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5919 break;
5920 case DW_FORM_sdata:
5921 case DW_FORM_udata:
5922 case DW_FORM_ref_udata:
5923 case DW_FORM_GNU_addr_index:
5924 case DW_FORM_GNU_str_index:
5925 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5926 break;
5927 case DW_FORM_indirect:
5928 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5929 info_ptr += bytes_read;
5930 /* We need to continue parsing from here, so just go back to
5931 the top. */
5932 goto skip_attribute;
5933
5934 default:
5935 error (_("Dwarf Error: Cannot handle %s "
5936 "in DWARF reader [in module %s]"),
5937 dwarf_form_name (form),
5938 bfd_get_filename (abfd));
5939 }
5940 }
5941
5942 if (abbrev->has_children)
5943 return skip_children (reader, info_ptr);
5944 else
5945 return info_ptr;
5946 }
5947
5948 /* Locate ORIG_PDI's sibling.
5949 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5950
5951 static gdb_byte *
5952 locate_pdi_sibling (const struct die_reader_specs *reader,
5953 struct partial_die_info *orig_pdi,
5954 gdb_byte *info_ptr)
5955 {
5956 /* Do we know the sibling already? */
5957
5958 if (orig_pdi->sibling)
5959 return orig_pdi->sibling;
5960
5961 /* Are there any children to deal with? */
5962
5963 if (!orig_pdi->has_children)
5964 return info_ptr;
5965
5966 /* Skip the children the long way. */
5967
5968 return skip_children (reader, info_ptr);
5969 }
5970
5971 /* Expand this partial symbol table into a full symbol table. */
5972
5973 static void
5974 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5975 {
5976 if (pst != NULL)
5977 {
5978 if (pst->readin)
5979 {
5980 warning (_("bug: psymtab for %s is already read in."),
5981 pst->filename);
5982 }
5983 else
5984 {
5985 if (info_verbose)
5986 {
5987 printf_filtered (_("Reading in symbols for %s..."),
5988 pst->filename);
5989 gdb_flush (gdb_stdout);
5990 }
5991
5992 /* Restore our global data. */
5993 dwarf2_per_objfile = objfile_data (pst->objfile,
5994 dwarf2_objfile_data_key);
5995
5996 /* If this psymtab is constructed from a debug-only objfile, the
5997 has_section_at_zero flag will not necessarily be correct. We
5998 can get the correct value for this flag by looking at the data
5999 associated with the (presumably stripped) associated objfile. */
6000 if (pst->objfile->separate_debug_objfile_backlink)
6001 {
6002 struct dwarf2_per_objfile *dpo_backlink
6003 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6004 dwarf2_objfile_data_key);
6005
6006 dwarf2_per_objfile->has_section_at_zero
6007 = dpo_backlink->has_section_at_zero;
6008 }
6009
6010 dwarf2_per_objfile->reading_partial_symbols = 0;
6011
6012 psymtab_to_symtab_1 (pst);
6013
6014 /* Finish up the debug error message. */
6015 if (info_verbose)
6016 printf_filtered (_("done.\n"));
6017 }
6018 }
6019
6020 process_cu_includes ();
6021 }
6022 \f
6023 /* Reading in full CUs. */
6024
6025 /* Add PER_CU to the queue. */
6026
6027 static void
6028 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6029 enum language pretend_language)
6030 {
6031 struct dwarf2_queue_item *item;
6032
6033 per_cu->queued = 1;
6034 item = xmalloc (sizeof (*item));
6035 item->per_cu = per_cu;
6036 item->pretend_language = pretend_language;
6037 item->next = NULL;
6038
6039 if (dwarf2_queue == NULL)
6040 dwarf2_queue = item;
6041 else
6042 dwarf2_queue_tail->next = item;
6043
6044 dwarf2_queue_tail = item;
6045 }
6046
6047 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6048 unit and add it to our queue.
6049 The result is non-zero if PER_CU was queued, otherwise the result is zero
6050 meaning either PER_CU is already queued or it is already loaded. */
6051
6052 static int
6053 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6054 struct dwarf2_per_cu_data *per_cu,
6055 enum language pretend_language)
6056 {
6057 /* We may arrive here during partial symbol reading, if we need full
6058 DIEs to process an unusual case (e.g. template arguments). Do
6059 not queue PER_CU, just tell our caller to load its DIEs. */
6060 if (dwarf2_per_objfile->reading_partial_symbols)
6061 {
6062 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6063 return 1;
6064 return 0;
6065 }
6066
6067 /* Mark the dependence relation so that we don't flush PER_CU
6068 too early. */
6069 dwarf2_add_dependence (this_cu, per_cu);
6070
6071 /* If it's already on the queue, we have nothing to do. */
6072 if (per_cu->queued)
6073 return 0;
6074
6075 /* If the compilation unit is already loaded, just mark it as
6076 used. */
6077 if (per_cu->cu != NULL)
6078 {
6079 per_cu->cu->last_used = 0;
6080 return 0;
6081 }
6082
6083 /* Add it to the queue. */
6084 queue_comp_unit (per_cu, pretend_language);
6085
6086 return 1;
6087 }
6088
6089 /* Process the queue. */
6090
6091 static void
6092 process_queue (void)
6093 {
6094 struct dwarf2_queue_item *item, *next_item;
6095
6096 if (dwarf2_read_debug)
6097 {
6098 fprintf_unfiltered (gdb_stdlog,
6099 "Expanding one or more symtabs of objfile %s ...\n",
6100 dwarf2_per_objfile->objfile->name);
6101 }
6102
6103 /* The queue starts out with one item, but following a DIE reference
6104 may load a new CU, adding it to the end of the queue. */
6105 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6106 {
6107 if (dwarf2_per_objfile->using_index
6108 ? !item->per_cu->v.quick->symtab
6109 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6110 {
6111 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6112
6113 if (dwarf2_read_debug)
6114 {
6115 fprintf_unfiltered (gdb_stdlog,
6116 "Expanding symtab of %s at offset 0x%x\n",
6117 per_cu->is_debug_types ? "TU" : "CU",
6118 per_cu->offset.sect_off);
6119 }
6120
6121 if (per_cu->is_debug_types)
6122 process_full_type_unit (per_cu, item->pretend_language);
6123 else
6124 process_full_comp_unit (per_cu, item->pretend_language);
6125
6126 if (dwarf2_read_debug)
6127 {
6128 fprintf_unfiltered (gdb_stdlog,
6129 "Done expanding %s at offset 0x%x\n",
6130 per_cu->is_debug_types ? "TU" : "CU",
6131 per_cu->offset.sect_off);
6132 }
6133 }
6134
6135 item->per_cu->queued = 0;
6136 next_item = item->next;
6137 xfree (item);
6138 }
6139
6140 dwarf2_queue_tail = NULL;
6141
6142 if (dwarf2_read_debug)
6143 {
6144 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6145 dwarf2_per_objfile->objfile->name);
6146 }
6147 }
6148
6149 /* Free all allocated queue entries. This function only releases anything if
6150 an error was thrown; if the queue was processed then it would have been
6151 freed as we went along. */
6152
6153 static void
6154 dwarf2_release_queue (void *dummy)
6155 {
6156 struct dwarf2_queue_item *item, *last;
6157
6158 item = dwarf2_queue;
6159 while (item)
6160 {
6161 /* Anything still marked queued is likely to be in an
6162 inconsistent state, so discard it. */
6163 if (item->per_cu->queued)
6164 {
6165 if (item->per_cu->cu != NULL)
6166 free_one_cached_comp_unit (item->per_cu);
6167 item->per_cu->queued = 0;
6168 }
6169
6170 last = item;
6171 item = item->next;
6172 xfree (last);
6173 }
6174
6175 dwarf2_queue = dwarf2_queue_tail = NULL;
6176 }
6177
6178 /* Read in full symbols for PST, and anything it depends on. */
6179
6180 static void
6181 psymtab_to_symtab_1 (struct partial_symtab *pst)
6182 {
6183 struct dwarf2_per_cu_data *per_cu;
6184 int i;
6185
6186 if (pst->readin)
6187 return;
6188
6189 for (i = 0; i < pst->number_of_dependencies; i++)
6190 if (!pst->dependencies[i]->readin
6191 && pst->dependencies[i]->user == NULL)
6192 {
6193 /* Inform about additional files that need to be read in. */
6194 if (info_verbose)
6195 {
6196 /* FIXME: i18n: Need to make this a single string. */
6197 fputs_filtered (" ", gdb_stdout);
6198 wrap_here ("");
6199 fputs_filtered ("and ", gdb_stdout);
6200 wrap_here ("");
6201 printf_filtered ("%s...", pst->dependencies[i]->filename);
6202 wrap_here (""); /* Flush output. */
6203 gdb_flush (gdb_stdout);
6204 }
6205 psymtab_to_symtab_1 (pst->dependencies[i]);
6206 }
6207
6208 per_cu = pst->read_symtab_private;
6209
6210 if (per_cu == NULL)
6211 {
6212 /* It's an include file, no symbols to read for it.
6213 Everything is in the parent symtab. */
6214 pst->readin = 1;
6215 return;
6216 }
6217
6218 dw2_do_instantiate_symtab (per_cu);
6219 }
6220
6221 /* Trivial hash function for die_info: the hash value of a DIE
6222 is its offset in .debug_info for this objfile. */
6223
6224 static hashval_t
6225 die_hash (const void *item)
6226 {
6227 const struct die_info *die = item;
6228
6229 return die->offset.sect_off;
6230 }
6231
6232 /* Trivial comparison function for die_info structures: two DIEs
6233 are equal if they have the same offset. */
6234
6235 static int
6236 die_eq (const void *item_lhs, const void *item_rhs)
6237 {
6238 const struct die_info *die_lhs = item_lhs;
6239 const struct die_info *die_rhs = item_rhs;
6240
6241 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6242 }
6243
6244 /* die_reader_func for load_full_comp_unit.
6245 This is identical to read_signatured_type_reader,
6246 but is kept separate for now. */
6247
6248 static void
6249 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6250 gdb_byte *info_ptr,
6251 struct die_info *comp_unit_die,
6252 int has_children,
6253 void *data)
6254 {
6255 struct dwarf2_cu *cu = reader->cu;
6256 enum language *language_ptr = data;
6257
6258 gdb_assert (cu->die_hash == NULL);
6259 cu->die_hash =
6260 htab_create_alloc_ex (cu->header.length / 12,
6261 die_hash,
6262 die_eq,
6263 NULL,
6264 &cu->comp_unit_obstack,
6265 hashtab_obstack_allocate,
6266 dummy_obstack_deallocate);
6267
6268 if (has_children)
6269 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6270 &info_ptr, comp_unit_die);
6271 cu->dies = comp_unit_die;
6272 /* comp_unit_die is not stored in die_hash, no need. */
6273
6274 /* We try not to read any attributes in this function, because not
6275 all CUs needed for references have been loaded yet, and symbol
6276 table processing isn't initialized. But we have to set the CU language,
6277 or we won't be able to build types correctly.
6278 Similarly, if we do not read the producer, we can not apply
6279 producer-specific interpretation. */
6280 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6281 }
6282
6283 /* Load the DIEs associated with PER_CU into memory. */
6284
6285 static void
6286 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6287 enum language pretend_language)
6288 {
6289 gdb_assert (! this_cu->is_debug_types);
6290
6291 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6292 load_full_comp_unit_reader, &pretend_language);
6293 }
6294
6295 /* Add a DIE to the delayed physname list. */
6296
6297 static void
6298 add_to_method_list (struct type *type, int fnfield_index, int index,
6299 const char *name, struct die_info *die,
6300 struct dwarf2_cu *cu)
6301 {
6302 struct delayed_method_info mi;
6303 mi.type = type;
6304 mi.fnfield_index = fnfield_index;
6305 mi.index = index;
6306 mi.name = name;
6307 mi.die = die;
6308 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6309 }
6310
6311 /* A cleanup for freeing the delayed method list. */
6312
6313 static void
6314 free_delayed_list (void *ptr)
6315 {
6316 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6317 if (cu->method_list != NULL)
6318 {
6319 VEC_free (delayed_method_info, cu->method_list);
6320 cu->method_list = NULL;
6321 }
6322 }
6323
6324 /* Compute the physnames of any methods on the CU's method list.
6325
6326 The computation of method physnames is delayed in order to avoid the
6327 (bad) condition that one of the method's formal parameters is of an as yet
6328 incomplete type. */
6329
6330 static void
6331 compute_delayed_physnames (struct dwarf2_cu *cu)
6332 {
6333 int i;
6334 struct delayed_method_info *mi;
6335 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6336 {
6337 const char *physname;
6338 struct fn_fieldlist *fn_flp
6339 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6340 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6341 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6342 }
6343 }
6344
6345 /* Go objects should be embedded in a DW_TAG_module DIE,
6346 and it's not clear if/how imported objects will appear.
6347 To keep Go support simple until that's worked out,
6348 go back through what we've read and create something usable.
6349 We could do this while processing each DIE, and feels kinda cleaner,
6350 but that way is more invasive.
6351 This is to, for example, allow the user to type "p var" or "b main"
6352 without having to specify the package name, and allow lookups
6353 of module.object to work in contexts that use the expression
6354 parser. */
6355
6356 static void
6357 fixup_go_packaging (struct dwarf2_cu *cu)
6358 {
6359 char *package_name = NULL;
6360 struct pending *list;
6361 int i;
6362
6363 for (list = global_symbols; list != NULL; list = list->next)
6364 {
6365 for (i = 0; i < list->nsyms; ++i)
6366 {
6367 struct symbol *sym = list->symbol[i];
6368
6369 if (SYMBOL_LANGUAGE (sym) == language_go
6370 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6371 {
6372 char *this_package_name = go_symbol_package_name (sym);
6373
6374 if (this_package_name == NULL)
6375 continue;
6376 if (package_name == NULL)
6377 package_name = this_package_name;
6378 else
6379 {
6380 if (strcmp (package_name, this_package_name) != 0)
6381 complaint (&symfile_complaints,
6382 _("Symtab %s has objects from two different Go packages: %s and %s"),
6383 (sym->symtab && sym->symtab->filename
6384 ? sym->symtab->filename
6385 : cu->objfile->name),
6386 this_package_name, package_name);
6387 xfree (this_package_name);
6388 }
6389 }
6390 }
6391 }
6392
6393 if (package_name != NULL)
6394 {
6395 struct objfile *objfile = cu->objfile;
6396 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6397 package_name, objfile);
6398 struct symbol *sym;
6399
6400 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6401
6402 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6403 SYMBOL_SET_LANGUAGE (sym, language_go);
6404 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6405 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6406 e.g., "main" finds the "main" module and not C's main(). */
6407 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6408 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6409 SYMBOL_TYPE (sym) = type;
6410
6411 add_symbol_to_list (sym, &global_symbols);
6412
6413 xfree (package_name);
6414 }
6415 }
6416
6417 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6418
6419 /* Return the symtab for PER_CU. This works properly regardless of
6420 whether we're using the index or psymtabs. */
6421
6422 static struct symtab *
6423 get_symtab (struct dwarf2_per_cu_data *per_cu)
6424 {
6425 return (dwarf2_per_objfile->using_index
6426 ? per_cu->v.quick->symtab
6427 : per_cu->v.psymtab->symtab);
6428 }
6429
6430 /* A helper function for computing the list of all symbol tables
6431 included by PER_CU. */
6432
6433 static void
6434 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6435 htab_t all_children,
6436 struct dwarf2_per_cu_data *per_cu)
6437 {
6438 void **slot;
6439 int ix;
6440 struct dwarf2_per_cu_data *iter;
6441
6442 slot = htab_find_slot (all_children, per_cu, INSERT);
6443 if (*slot != NULL)
6444 {
6445 /* This inclusion and its children have been processed. */
6446 return;
6447 }
6448
6449 *slot = per_cu;
6450 /* Only add a CU if it has a symbol table. */
6451 if (get_symtab (per_cu) != NULL)
6452 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6453
6454 for (ix = 0;
6455 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6456 ++ix)
6457 recursively_compute_inclusions (result, all_children, iter);
6458 }
6459
6460 /* Compute the symtab 'includes' fields for the symtab related to
6461 PER_CU. */
6462
6463 static void
6464 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6465 {
6466 gdb_assert (! per_cu->is_debug_types);
6467
6468 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6469 {
6470 int ix, len;
6471 struct dwarf2_per_cu_data *iter;
6472 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6473 htab_t all_children;
6474 struct symtab *symtab = get_symtab (per_cu);
6475
6476 /* If we don't have a symtab, we can just skip this case. */
6477 if (symtab == NULL)
6478 return;
6479
6480 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6481 NULL, xcalloc, xfree);
6482
6483 for (ix = 0;
6484 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6485 ix, iter);
6486 ++ix)
6487 recursively_compute_inclusions (&result_children, all_children, iter);
6488
6489 /* Now we have a transitive closure of all the included CUs, so
6490 we can convert it to a list of symtabs. */
6491 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6492 symtab->includes
6493 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6494 (len + 1) * sizeof (struct symtab *));
6495 for (ix = 0;
6496 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6497 ++ix)
6498 symtab->includes[ix] = get_symtab (iter);
6499 symtab->includes[len] = NULL;
6500
6501 VEC_free (dwarf2_per_cu_ptr, result_children);
6502 htab_delete (all_children);
6503 }
6504 }
6505
6506 /* Compute the 'includes' field for the symtabs of all the CUs we just
6507 read. */
6508
6509 static void
6510 process_cu_includes (void)
6511 {
6512 int ix;
6513 struct dwarf2_per_cu_data *iter;
6514
6515 for (ix = 0;
6516 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6517 ix, iter);
6518 ++ix)
6519 {
6520 if (! iter->is_debug_types)
6521 compute_symtab_includes (iter);
6522 }
6523
6524 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6525 }
6526
6527 /* Generate full symbol information for PER_CU, whose DIEs have
6528 already been loaded into memory. */
6529
6530 static void
6531 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6532 enum language pretend_language)
6533 {
6534 struct dwarf2_cu *cu = per_cu->cu;
6535 struct objfile *objfile = per_cu->objfile;
6536 CORE_ADDR lowpc, highpc;
6537 struct symtab *symtab;
6538 struct cleanup *back_to, *delayed_list_cleanup;
6539 CORE_ADDR baseaddr;
6540 struct block *static_block;
6541
6542 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6543
6544 buildsym_init ();
6545 back_to = make_cleanup (really_free_pendings, NULL);
6546 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6547
6548 cu->list_in_scope = &file_symbols;
6549
6550 cu->language = pretend_language;
6551 cu->language_defn = language_def (cu->language);
6552
6553 /* Do line number decoding in read_file_scope () */
6554 process_die (cu->dies, cu);
6555
6556 /* For now fudge the Go package. */
6557 if (cu->language == language_go)
6558 fixup_go_packaging (cu);
6559
6560 /* Now that we have processed all the DIEs in the CU, all the types
6561 should be complete, and it should now be safe to compute all of the
6562 physnames. */
6563 compute_delayed_physnames (cu);
6564 do_cleanups (delayed_list_cleanup);
6565
6566 /* Some compilers don't define a DW_AT_high_pc attribute for the
6567 compilation unit. If the DW_AT_high_pc is missing, synthesize
6568 it, by scanning the DIE's below the compilation unit. */
6569 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6570
6571 static_block = end_symtab_get_static_block (highpc + baseaddr, objfile, 0);
6572
6573 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6574 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6575 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6576 addrmap to help ensure it has an accurate map of pc values belonging to
6577 this comp unit. */
6578 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6579
6580 symtab = end_symtab_from_static_block (static_block, objfile,
6581 SECT_OFF_TEXT (objfile), 0);
6582
6583 if (symtab != NULL)
6584 {
6585 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6586
6587 /* Set symtab language to language from DW_AT_language. If the
6588 compilation is from a C file generated by language preprocessors, do
6589 not set the language if it was already deduced by start_subfile. */
6590 if (!(cu->language == language_c && symtab->language != language_c))
6591 symtab->language = cu->language;
6592
6593 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6594 produce DW_AT_location with location lists but it can be possibly
6595 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6596 there were bugs in prologue debug info, fixed later in GCC-4.5
6597 by "unwind info for epilogues" patch (which is not directly related).
6598
6599 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6600 needed, it would be wrong due to missing DW_AT_producer there.
6601
6602 Still one can confuse GDB by using non-standard GCC compilation
6603 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6604 */
6605 if (cu->has_loclist && gcc_4_minor >= 5)
6606 symtab->locations_valid = 1;
6607
6608 if (gcc_4_minor >= 5)
6609 symtab->epilogue_unwind_valid = 1;
6610
6611 symtab->call_site_htab = cu->call_site_htab;
6612 }
6613
6614 if (dwarf2_per_objfile->using_index)
6615 per_cu->v.quick->symtab = symtab;
6616 else
6617 {
6618 struct partial_symtab *pst = per_cu->v.psymtab;
6619 pst->symtab = symtab;
6620 pst->readin = 1;
6621 }
6622
6623 /* Push it for inclusion processing later. */
6624 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6625
6626 do_cleanups (back_to);
6627 }
6628
6629 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6630 already been loaded into memory. */
6631
6632 static void
6633 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6634 enum language pretend_language)
6635 {
6636 struct dwarf2_cu *cu = per_cu->cu;
6637 struct objfile *objfile = per_cu->objfile;
6638 struct symtab *symtab;
6639 struct cleanup *back_to, *delayed_list_cleanup;
6640
6641 buildsym_init ();
6642 back_to = make_cleanup (really_free_pendings, NULL);
6643 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6644
6645 cu->list_in_scope = &file_symbols;
6646
6647 cu->language = pretend_language;
6648 cu->language_defn = language_def (cu->language);
6649
6650 /* The symbol tables are set up in read_type_unit_scope. */
6651 process_die (cu->dies, cu);
6652
6653 /* For now fudge the Go package. */
6654 if (cu->language == language_go)
6655 fixup_go_packaging (cu);
6656
6657 /* Now that we have processed all the DIEs in the CU, all the types
6658 should be complete, and it should now be safe to compute all of the
6659 physnames. */
6660 compute_delayed_physnames (cu);
6661 do_cleanups (delayed_list_cleanup);
6662
6663 /* TUs share symbol tables.
6664 If this is the first TU to use this symtab, complete the construction
6665 of it with end_expandable_symtab. Otherwise, complete the addition of
6666 this TU's symbols to the existing symtab. */
6667 if (per_cu->s.type_unit_group->primary_symtab == NULL)
6668 {
6669 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6670 per_cu->s.type_unit_group->primary_symtab = symtab;
6671
6672 if (symtab != NULL)
6673 {
6674 /* Set symtab language to language from DW_AT_language. If the
6675 compilation is from a C file generated by language preprocessors,
6676 do not set the language if it was already deduced by
6677 start_subfile. */
6678 if (!(cu->language == language_c && symtab->language != language_c))
6679 symtab->language = cu->language;
6680 }
6681 }
6682 else
6683 {
6684 augment_type_symtab (objfile,
6685 per_cu->s.type_unit_group->primary_symtab);
6686 symtab = per_cu->s.type_unit_group->primary_symtab;
6687 }
6688
6689 if (dwarf2_per_objfile->using_index)
6690 per_cu->v.quick->symtab = symtab;
6691 else
6692 {
6693 struct partial_symtab *pst = per_cu->v.psymtab;
6694 pst->symtab = symtab;
6695 pst->readin = 1;
6696 }
6697
6698 do_cleanups (back_to);
6699 }
6700
6701 /* Process an imported unit DIE. */
6702
6703 static void
6704 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6705 {
6706 struct attribute *attr;
6707
6708 /* For now we don't handle imported units in type units. */
6709 if (cu->per_cu->is_debug_types)
6710 {
6711 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6712 " supported in type units [in module %s]"),
6713 cu->objfile->name);
6714 }
6715
6716 attr = dwarf2_attr (die, DW_AT_import, cu);
6717 if (attr != NULL)
6718 {
6719 struct dwarf2_per_cu_data *per_cu;
6720 struct symtab *imported_symtab;
6721 sect_offset offset;
6722
6723 offset = dwarf2_get_ref_die_offset (attr);
6724 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6725
6726 /* Queue the unit, if needed. */
6727 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
6728 load_full_comp_unit (per_cu, cu->language);
6729
6730 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
6731 per_cu);
6732 }
6733 }
6734
6735 /* Process a die and its children. */
6736
6737 static void
6738 process_die (struct die_info *die, struct dwarf2_cu *cu)
6739 {
6740 switch (die->tag)
6741 {
6742 case DW_TAG_padding:
6743 break;
6744 case DW_TAG_compile_unit:
6745 case DW_TAG_partial_unit:
6746 read_file_scope (die, cu);
6747 break;
6748 case DW_TAG_type_unit:
6749 read_type_unit_scope (die, cu);
6750 break;
6751 case DW_TAG_subprogram:
6752 case DW_TAG_inlined_subroutine:
6753 read_func_scope (die, cu);
6754 break;
6755 case DW_TAG_lexical_block:
6756 case DW_TAG_try_block:
6757 case DW_TAG_catch_block:
6758 read_lexical_block_scope (die, cu);
6759 break;
6760 case DW_TAG_GNU_call_site:
6761 read_call_site_scope (die, cu);
6762 break;
6763 case DW_TAG_class_type:
6764 case DW_TAG_interface_type:
6765 case DW_TAG_structure_type:
6766 case DW_TAG_union_type:
6767 process_structure_scope (die, cu);
6768 break;
6769 case DW_TAG_enumeration_type:
6770 process_enumeration_scope (die, cu);
6771 break;
6772
6773 /* These dies have a type, but processing them does not create
6774 a symbol or recurse to process the children. Therefore we can
6775 read them on-demand through read_type_die. */
6776 case DW_TAG_subroutine_type:
6777 case DW_TAG_set_type:
6778 case DW_TAG_array_type:
6779 case DW_TAG_pointer_type:
6780 case DW_TAG_ptr_to_member_type:
6781 case DW_TAG_reference_type:
6782 case DW_TAG_string_type:
6783 break;
6784
6785 case DW_TAG_base_type:
6786 case DW_TAG_subrange_type:
6787 case DW_TAG_typedef:
6788 /* Add a typedef symbol for the type definition, if it has a
6789 DW_AT_name. */
6790 new_symbol (die, read_type_die (die, cu), cu);
6791 break;
6792 case DW_TAG_common_block:
6793 read_common_block (die, cu);
6794 break;
6795 case DW_TAG_common_inclusion:
6796 break;
6797 case DW_TAG_namespace:
6798 processing_has_namespace_info = 1;
6799 read_namespace (die, cu);
6800 break;
6801 case DW_TAG_module:
6802 processing_has_namespace_info = 1;
6803 read_module (die, cu);
6804 break;
6805 case DW_TAG_imported_declaration:
6806 case DW_TAG_imported_module:
6807 processing_has_namespace_info = 1;
6808 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6809 || cu->language != language_fortran))
6810 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6811 dwarf_tag_name (die->tag));
6812 read_import_statement (die, cu);
6813 break;
6814
6815 case DW_TAG_imported_unit:
6816 process_imported_unit_die (die, cu);
6817 break;
6818
6819 default:
6820 new_symbol (die, NULL, cu);
6821 break;
6822 }
6823 }
6824
6825 /* A helper function for dwarf2_compute_name which determines whether DIE
6826 needs to have the name of the scope prepended to the name listed in the
6827 die. */
6828
6829 static int
6830 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6831 {
6832 struct attribute *attr;
6833
6834 switch (die->tag)
6835 {
6836 case DW_TAG_namespace:
6837 case DW_TAG_typedef:
6838 case DW_TAG_class_type:
6839 case DW_TAG_interface_type:
6840 case DW_TAG_structure_type:
6841 case DW_TAG_union_type:
6842 case DW_TAG_enumeration_type:
6843 case DW_TAG_enumerator:
6844 case DW_TAG_subprogram:
6845 case DW_TAG_member:
6846 return 1;
6847
6848 case DW_TAG_variable:
6849 case DW_TAG_constant:
6850 /* We only need to prefix "globally" visible variables. These include
6851 any variable marked with DW_AT_external or any variable that
6852 lives in a namespace. [Variables in anonymous namespaces
6853 require prefixing, but they are not DW_AT_external.] */
6854
6855 if (dwarf2_attr (die, DW_AT_specification, cu))
6856 {
6857 struct dwarf2_cu *spec_cu = cu;
6858
6859 return die_needs_namespace (die_specification (die, &spec_cu),
6860 spec_cu);
6861 }
6862
6863 attr = dwarf2_attr (die, DW_AT_external, cu);
6864 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6865 && die->parent->tag != DW_TAG_module)
6866 return 0;
6867 /* A variable in a lexical block of some kind does not need a
6868 namespace, even though in C++ such variables may be external
6869 and have a mangled name. */
6870 if (die->parent->tag == DW_TAG_lexical_block
6871 || die->parent->tag == DW_TAG_try_block
6872 || die->parent->tag == DW_TAG_catch_block
6873 || die->parent->tag == DW_TAG_subprogram)
6874 return 0;
6875 return 1;
6876
6877 default:
6878 return 0;
6879 }
6880 }
6881
6882 /* Retrieve the last character from a mem_file. */
6883
6884 static void
6885 do_ui_file_peek_last (void *object, const char *buffer, long length)
6886 {
6887 char *last_char_p = (char *) object;
6888
6889 if (length > 0)
6890 *last_char_p = buffer[length - 1];
6891 }
6892
6893 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6894 compute the physname for the object, which include a method's:
6895 - formal parameters (C++/Java),
6896 - receiver type (Go),
6897 - return type (Java).
6898
6899 The term "physname" is a bit confusing.
6900 For C++, for example, it is the demangled name.
6901 For Go, for example, it's the mangled name.
6902
6903 For Ada, return the DIE's linkage name rather than the fully qualified
6904 name. PHYSNAME is ignored..
6905
6906 The result is allocated on the objfile_obstack and canonicalized. */
6907
6908 static const char *
6909 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6910 int physname)
6911 {
6912 struct objfile *objfile = cu->objfile;
6913
6914 if (name == NULL)
6915 name = dwarf2_name (die, cu);
6916
6917 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6918 compute it by typename_concat inside GDB. */
6919 if (cu->language == language_ada
6920 || (cu->language == language_fortran && physname))
6921 {
6922 /* For Ada unit, we prefer the linkage name over the name, as
6923 the former contains the exported name, which the user expects
6924 to be able to reference. Ideally, we want the user to be able
6925 to reference this entity using either natural or linkage name,
6926 but we haven't started looking at this enhancement yet. */
6927 struct attribute *attr;
6928
6929 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6930 if (attr == NULL)
6931 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6932 if (attr && DW_STRING (attr))
6933 return DW_STRING (attr);
6934 }
6935
6936 /* These are the only languages we know how to qualify names in. */
6937 if (name != NULL
6938 && (cu->language == language_cplus || cu->language == language_java
6939 || cu->language == language_fortran))
6940 {
6941 if (die_needs_namespace (die, cu))
6942 {
6943 long length;
6944 const char *prefix;
6945 struct ui_file *buf;
6946
6947 prefix = determine_prefix (die, cu);
6948 buf = mem_fileopen ();
6949 if (*prefix != '\0')
6950 {
6951 char *prefixed_name = typename_concat (NULL, prefix, name,
6952 physname, cu);
6953
6954 fputs_unfiltered (prefixed_name, buf);
6955 xfree (prefixed_name);
6956 }
6957 else
6958 fputs_unfiltered (name, buf);
6959
6960 /* Template parameters may be specified in the DIE's DW_AT_name, or
6961 as children with DW_TAG_template_type_param or
6962 DW_TAG_value_type_param. If the latter, add them to the name
6963 here. If the name already has template parameters, then
6964 skip this step; some versions of GCC emit both, and
6965 it is more efficient to use the pre-computed name.
6966
6967 Something to keep in mind about this process: it is very
6968 unlikely, or in some cases downright impossible, to produce
6969 something that will match the mangled name of a function.
6970 If the definition of the function has the same debug info,
6971 we should be able to match up with it anyway. But fallbacks
6972 using the minimal symbol, for instance to find a method
6973 implemented in a stripped copy of libstdc++, will not work.
6974 If we do not have debug info for the definition, we will have to
6975 match them up some other way.
6976
6977 When we do name matching there is a related problem with function
6978 templates; two instantiated function templates are allowed to
6979 differ only by their return types, which we do not add here. */
6980
6981 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6982 {
6983 struct attribute *attr;
6984 struct die_info *child;
6985 int first = 1;
6986
6987 die->building_fullname = 1;
6988
6989 for (child = die->child; child != NULL; child = child->sibling)
6990 {
6991 struct type *type;
6992 LONGEST value;
6993 gdb_byte *bytes;
6994 struct dwarf2_locexpr_baton *baton;
6995 struct value *v;
6996
6997 if (child->tag != DW_TAG_template_type_param
6998 && child->tag != DW_TAG_template_value_param)
6999 continue;
7000
7001 if (first)
7002 {
7003 fputs_unfiltered ("<", buf);
7004 first = 0;
7005 }
7006 else
7007 fputs_unfiltered (", ", buf);
7008
7009 attr = dwarf2_attr (child, DW_AT_type, cu);
7010 if (attr == NULL)
7011 {
7012 complaint (&symfile_complaints,
7013 _("template parameter missing DW_AT_type"));
7014 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7015 continue;
7016 }
7017 type = die_type (child, cu);
7018
7019 if (child->tag == DW_TAG_template_type_param)
7020 {
7021 c_print_type (type, "", buf, -1, 0);
7022 continue;
7023 }
7024
7025 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7026 if (attr == NULL)
7027 {
7028 complaint (&symfile_complaints,
7029 _("template parameter missing "
7030 "DW_AT_const_value"));
7031 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7032 continue;
7033 }
7034
7035 dwarf2_const_value_attr (attr, type, name,
7036 &cu->comp_unit_obstack, cu,
7037 &value, &bytes, &baton);
7038
7039 if (TYPE_NOSIGN (type))
7040 /* GDB prints characters as NUMBER 'CHAR'. If that's
7041 changed, this can use value_print instead. */
7042 c_printchar (value, type, buf);
7043 else
7044 {
7045 struct value_print_options opts;
7046
7047 if (baton != NULL)
7048 v = dwarf2_evaluate_loc_desc (type, NULL,
7049 baton->data,
7050 baton->size,
7051 baton->per_cu);
7052 else if (bytes != NULL)
7053 {
7054 v = allocate_value (type);
7055 memcpy (value_contents_writeable (v), bytes,
7056 TYPE_LENGTH (type));
7057 }
7058 else
7059 v = value_from_longest (type, value);
7060
7061 /* Specify decimal so that we do not depend on
7062 the radix. */
7063 get_formatted_print_options (&opts, 'd');
7064 opts.raw = 1;
7065 value_print (v, buf, &opts);
7066 release_value (v);
7067 value_free (v);
7068 }
7069 }
7070
7071 die->building_fullname = 0;
7072
7073 if (!first)
7074 {
7075 /* Close the argument list, with a space if necessary
7076 (nested templates). */
7077 char last_char = '\0';
7078 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7079 if (last_char == '>')
7080 fputs_unfiltered (" >", buf);
7081 else
7082 fputs_unfiltered (">", buf);
7083 }
7084 }
7085
7086 /* For Java and C++ methods, append formal parameter type
7087 information, if PHYSNAME. */
7088
7089 if (physname && die->tag == DW_TAG_subprogram
7090 && (cu->language == language_cplus
7091 || cu->language == language_java))
7092 {
7093 struct type *type = read_type_die (die, cu);
7094
7095 c_type_print_args (type, buf, 1, cu->language);
7096
7097 if (cu->language == language_java)
7098 {
7099 /* For java, we must append the return type to method
7100 names. */
7101 if (die->tag == DW_TAG_subprogram)
7102 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7103 0, 0);
7104 }
7105 else if (cu->language == language_cplus)
7106 {
7107 /* Assume that an artificial first parameter is
7108 "this", but do not crash if it is not. RealView
7109 marks unnamed (and thus unused) parameters as
7110 artificial; there is no way to differentiate
7111 the two cases. */
7112 if (TYPE_NFIELDS (type) > 0
7113 && TYPE_FIELD_ARTIFICIAL (type, 0)
7114 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7115 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7116 0))))
7117 fputs_unfiltered (" const", buf);
7118 }
7119 }
7120
7121 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7122 &length);
7123 ui_file_delete (buf);
7124
7125 if (cu->language == language_cplus)
7126 {
7127 char *cname
7128 = dwarf2_canonicalize_name (name, cu,
7129 &objfile->objfile_obstack);
7130
7131 if (cname != NULL)
7132 name = cname;
7133 }
7134 }
7135 }
7136
7137 return name;
7138 }
7139
7140 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7141 If scope qualifiers are appropriate they will be added. The result
7142 will be allocated on the objfile_obstack, or NULL if the DIE does
7143 not have a name. NAME may either be from a previous call to
7144 dwarf2_name or NULL.
7145
7146 The output string will be canonicalized (if C++/Java). */
7147
7148 static const char *
7149 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7150 {
7151 return dwarf2_compute_name (name, die, cu, 0);
7152 }
7153
7154 /* Construct a physname for the given DIE in CU. NAME may either be
7155 from a previous call to dwarf2_name or NULL. The result will be
7156 allocated on the objfile_objstack or NULL if the DIE does not have a
7157 name.
7158
7159 The output string will be canonicalized (if C++/Java). */
7160
7161 static const char *
7162 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7163 {
7164 struct objfile *objfile = cu->objfile;
7165 struct attribute *attr;
7166 const char *retval, *mangled = NULL, *canon = NULL;
7167 struct cleanup *back_to;
7168 int need_copy = 1;
7169
7170 /* In this case dwarf2_compute_name is just a shortcut not building anything
7171 on its own. */
7172 if (!die_needs_namespace (die, cu))
7173 return dwarf2_compute_name (name, die, cu, 1);
7174
7175 back_to = make_cleanup (null_cleanup, NULL);
7176
7177 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7178 if (!attr)
7179 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7180
7181 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7182 has computed. */
7183 if (attr && DW_STRING (attr))
7184 {
7185 char *demangled;
7186
7187 mangled = DW_STRING (attr);
7188
7189 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7190 type. It is easier for GDB users to search for such functions as
7191 `name(params)' than `long name(params)'. In such case the minimal
7192 symbol names do not match the full symbol names but for template
7193 functions there is never a need to look up their definition from their
7194 declaration so the only disadvantage remains the minimal symbol
7195 variant `long name(params)' does not have the proper inferior type.
7196 */
7197
7198 if (cu->language == language_go)
7199 {
7200 /* This is a lie, but we already lie to the caller new_symbol_full.
7201 new_symbol_full assumes we return the mangled name.
7202 This just undoes that lie until things are cleaned up. */
7203 demangled = NULL;
7204 }
7205 else
7206 {
7207 demangled = cplus_demangle (mangled,
7208 (DMGL_PARAMS | DMGL_ANSI
7209 | (cu->language == language_java
7210 ? DMGL_JAVA | DMGL_RET_POSTFIX
7211 : DMGL_RET_DROP)));
7212 }
7213 if (demangled)
7214 {
7215 make_cleanup (xfree, demangled);
7216 canon = demangled;
7217 }
7218 else
7219 {
7220 canon = mangled;
7221 need_copy = 0;
7222 }
7223 }
7224
7225 if (canon == NULL || check_physname)
7226 {
7227 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7228
7229 if (canon != NULL && strcmp (physname, canon) != 0)
7230 {
7231 /* It may not mean a bug in GDB. The compiler could also
7232 compute DW_AT_linkage_name incorrectly. But in such case
7233 GDB would need to be bug-to-bug compatible. */
7234
7235 complaint (&symfile_complaints,
7236 _("Computed physname <%s> does not match demangled <%s> "
7237 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7238 physname, canon, mangled, die->offset.sect_off, objfile->name);
7239
7240 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7241 is available here - over computed PHYSNAME. It is safer
7242 against both buggy GDB and buggy compilers. */
7243
7244 retval = canon;
7245 }
7246 else
7247 {
7248 retval = physname;
7249 need_copy = 0;
7250 }
7251 }
7252 else
7253 retval = canon;
7254
7255 if (need_copy)
7256 retval = obsavestring (retval, strlen (retval),
7257 &objfile->objfile_obstack);
7258
7259 do_cleanups (back_to);
7260 return retval;
7261 }
7262
7263 /* Read the import statement specified by the given die and record it. */
7264
7265 static void
7266 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7267 {
7268 struct objfile *objfile = cu->objfile;
7269 struct attribute *import_attr;
7270 struct die_info *imported_die, *child_die;
7271 struct dwarf2_cu *imported_cu;
7272 const char *imported_name;
7273 const char *imported_name_prefix;
7274 const char *canonical_name;
7275 const char *import_alias;
7276 const char *imported_declaration = NULL;
7277 const char *import_prefix;
7278 VEC (const_char_ptr) *excludes = NULL;
7279 struct cleanup *cleanups;
7280
7281 char *temp;
7282
7283 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7284 if (import_attr == NULL)
7285 {
7286 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7287 dwarf_tag_name (die->tag));
7288 return;
7289 }
7290
7291 imported_cu = cu;
7292 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7293 imported_name = dwarf2_name (imported_die, imported_cu);
7294 if (imported_name == NULL)
7295 {
7296 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7297
7298 The import in the following code:
7299 namespace A
7300 {
7301 typedef int B;
7302 }
7303
7304 int main ()
7305 {
7306 using A::B;
7307 B b;
7308 return b;
7309 }
7310
7311 ...
7312 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7313 <52> DW_AT_decl_file : 1
7314 <53> DW_AT_decl_line : 6
7315 <54> DW_AT_import : <0x75>
7316 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7317 <59> DW_AT_name : B
7318 <5b> DW_AT_decl_file : 1
7319 <5c> DW_AT_decl_line : 2
7320 <5d> DW_AT_type : <0x6e>
7321 ...
7322 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7323 <76> DW_AT_byte_size : 4
7324 <77> DW_AT_encoding : 5 (signed)
7325
7326 imports the wrong die ( 0x75 instead of 0x58 ).
7327 This case will be ignored until the gcc bug is fixed. */
7328 return;
7329 }
7330
7331 /* Figure out the local name after import. */
7332 import_alias = dwarf2_name (die, cu);
7333
7334 /* Figure out where the statement is being imported to. */
7335 import_prefix = determine_prefix (die, cu);
7336
7337 /* Figure out what the scope of the imported die is and prepend it
7338 to the name of the imported die. */
7339 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7340
7341 if (imported_die->tag != DW_TAG_namespace
7342 && imported_die->tag != DW_TAG_module)
7343 {
7344 imported_declaration = imported_name;
7345 canonical_name = imported_name_prefix;
7346 }
7347 else if (strlen (imported_name_prefix) > 0)
7348 {
7349 temp = alloca (strlen (imported_name_prefix)
7350 + 2 + strlen (imported_name) + 1);
7351 strcpy (temp, imported_name_prefix);
7352 strcat (temp, "::");
7353 strcat (temp, imported_name);
7354 canonical_name = temp;
7355 }
7356 else
7357 canonical_name = imported_name;
7358
7359 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7360
7361 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7362 for (child_die = die->child; child_die && child_die->tag;
7363 child_die = sibling_die (child_die))
7364 {
7365 /* DWARF-4: A Fortran use statement with a “rename list” may be
7366 represented by an imported module entry with an import attribute
7367 referring to the module and owned entries corresponding to those
7368 entities that are renamed as part of being imported. */
7369
7370 if (child_die->tag != DW_TAG_imported_declaration)
7371 {
7372 complaint (&symfile_complaints,
7373 _("child DW_TAG_imported_declaration expected "
7374 "- DIE at 0x%x [in module %s]"),
7375 child_die->offset.sect_off, objfile->name);
7376 continue;
7377 }
7378
7379 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7380 if (import_attr == NULL)
7381 {
7382 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7383 dwarf_tag_name (child_die->tag));
7384 continue;
7385 }
7386
7387 imported_cu = cu;
7388 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7389 &imported_cu);
7390 imported_name = dwarf2_name (imported_die, imported_cu);
7391 if (imported_name == NULL)
7392 {
7393 complaint (&symfile_complaints,
7394 _("child DW_TAG_imported_declaration has unknown "
7395 "imported name - DIE at 0x%x [in module %s]"),
7396 child_die->offset.sect_off, objfile->name);
7397 continue;
7398 }
7399
7400 VEC_safe_push (const_char_ptr, excludes, imported_name);
7401
7402 process_die (child_die, cu);
7403 }
7404
7405 cp_add_using_directive (import_prefix,
7406 canonical_name,
7407 import_alias,
7408 imported_declaration,
7409 excludes,
7410 &objfile->objfile_obstack);
7411
7412 do_cleanups (cleanups);
7413 }
7414
7415 /* Cleanup function for handle_DW_AT_stmt_list. */
7416
7417 static void
7418 free_cu_line_header (void *arg)
7419 {
7420 struct dwarf2_cu *cu = arg;
7421
7422 free_line_header (cu->line_header);
7423 cu->line_header = NULL;
7424 }
7425
7426 static void
7427 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7428 char **name, char **comp_dir)
7429 {
7430 struct attribute *attr;
7431
7432 *name = NULL;
7433 *comp_dir = NULL;
7434
7435 /* Find the filename. Do not use dwarf2_name here, since the filename
7436 is not a source language identifier. */
7437 attr = dwarf2_attr (die, DW_AT_name, cu);
7438 if (attr)
7439 {
7440 *name = DW_STRING (attr);
7441 }
7442
7443 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7444 if (attr)
7445 *comp_dir = DW_STRING (attr);
7446 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7447 {
7448 *comp_dir = ldirname (*name);
7449 if (*comp_dir != NULL)
7450 make_cleanup (xfree, *comp_dir);
7451 }
7452 if (*comp_dir != NULL)
7453 {
7454 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7455 directory, get rid of it. */
7456 char *cp = strchr (*comp_dir, ':');
7457
7458 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7459 *comp_dir = cp + 1;
7460 }
7461
7462 if (*name == NULL)
7463 *name = "<unknown>";
7464 }
7465
7466 /* Handle DW_AT_stmt_list for a compilation unit.
7467 DIE is the DW_TAG_compile_unit die for CU.
7468 COMP_DIR is the compilation directory.
7469 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7470
7471 static void
7472 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7473 const char *comp_dir)
7474 {
7475 struct attribute *attr;
7476
7477 gdb_assert (! cu->per_cu->is_debug_types);
7478
7479 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7480 if (attr)
7481 {
7482 unsigned int line_offset = DW_UNSND (attr);
7483 struct line_header *line_header
7484 = dwarf_decode_line_header (line_offset, cu);
7485
7486 if (line_header)
7487 {
7488 cu->line_header = line_header;
7489 make_cleanup (free_cu_line_header, cu);
7490 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7491 }
7492 }
7493 }
7494
7495 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7496
7497 static void
7498 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7499 {
7500 struct objfile *objfile = dwarf2_per_objfile->objfile;
7501 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7502 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7503 CORE_ADDR highpc = ((CORE_ADDR) 0);
7504 struct attribute *attr;
7505 char *name = NULL;
7506 char *comp_dir = NULL;
7507 struct die_info *child_die;
7508 bfd *abfd = objfile->obfd;
7509 CORE_ADDR baseaddr;
7510
7511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7512
7513 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7514
7515 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7516 from finish_block. */
7517 if (lowpc == ((CORE_ADDR) -1))
7518 lowpc = highpc;
7519 lowpc += baseaddr;
7520 highpc += baseaddr;
7521
7522 find_file_and_directory (die, cu, &name, &comp_dir);
7523
7524 prepare_one_comp_unit (cu, die, cu->language);
7525
7526 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7527 standardised yet. As a workaround for the language detection we fall
7528 back to the DW_AT_producer string. */
7529 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7530 cu->language = language_opencl;
7531
7532 /* Similar hack for Go. */
7533 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7534 set_cu_language (DW_LANG_Go, cu);
7535
7536 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7537
7538 /* Decode line number information if present. We do this before
7539 processing child DIEs, so that the line header table is available
7540 for DW_AT_decl_file. */
7541 handle_DW_AT_stmt_list (die, cu, comp_dir);
7542
7543 /* Process all dies in compilation unit. */
7544 if (die->child != NULL)
7545 {
7546 child_die = die->child;
7547 while (child_die && child_die->tag)
7548 {
7549 process_die (child_die, cu);
7550 child_die = sibling_die (child_die);
7551 }
7552 }
7553
7554 /* Decode macro information, if present. Dwarf 2 macro information
7555 refers to information in the line number info statement program
7556 header, so we can only read it if we've read the header
7557 successfully. */
7558 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7559 if (attr && cu->line_header)
7560 {
7561 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7562 complaint (&symfile_complaints,
7563 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7564
7565 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7566 }
7567 else
7568 {
7569 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7570 if (attr && cu->line_header)
7571 {
7572 unsigned int macro_offset = DW_UNSND (attr);
7573
7574 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7575 }
7576 }
7577
7578 do_cleanups (back_to);
7579 }
7580
7581 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7582 Create the set of symtabs used by this TU, or if this TU is sharing
7583 symtabs with another TU and the symtabs have already been created
7584 then restore those symtabs in the line header.
7585 We don't need the pc/line-number mapping for type units. */
7586
7587 static void
7588 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7589 {
7590 struct objfile *objfile = dwarf2_per_objfile->objfile;
7591 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7592 struct type_unit_group *tu_group;
7593 int first_time;
7594 struct line_header *lh;
7595 struct attribute *attr;
7596 unsigned int i, line_offset;
7597
7598 gdb_assert (per_cu->is_debug_types);
7599
7600 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7601
7602 /* If we're using .gdb_index (includes -readnow) then
7603 per_cu->s.type_unit_group may not have been set up yet. */
7604 if (per_cu->s.type_unit_group == NULL)
7605 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7606 tu_group = per_cu->s.type_unit_group;
7607
7608 /* If we've already processed this stmt_list there's no real need to
7609 do it again, we could fake it and just recreate the part we need
7610 (file name,index -> symtab mapping). If data shows this optimization
7611 is useful we can do it then. */
7612 first_time = tu_group->primary_symtab == NULL;
7613
7614 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7615 debug info. */
7616 lh = NULL;
7617 if (attr != NULL)
7618 {
7619 line_offset = DW_UNSND (attr);
7620 lh = dwarf_decode_line_header (line_offset, cu);
7621 }
7622 if (lh == NULL)
7623 {
7624 if (first_time)
7625 dwarf2_start_symtab (cu, "", NULL, 0);
7626 else
7627 {
7628 gdb_assert (tu_group->symtabs == NULL);
7629 restart_symtab (0);
7630 }
7631 /* Note: The primary symtab will get allocated at the end. */
7632 return;
7633 }
7634
7635 cu->line_header = lh;
7636 make_cleanup (free_cu_line_header, cu);
7637
7638 if (first_time)
7639 {
7640 dwarf2_start_symtab (cu, "", NULL, 0);
7641
7642 tu_group->num_symtabs = lh->num_file_names;
7643 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7644
7645 for (i = 0; i < lh->num_file_names; ++i)
7646 {
7647 char *dir = NULL;
7648 struct file_entry *fe = &lh->file_names[i];
7649
7650 if (fe->dir_index)
7651 dir = lh->include_dirs[fe->dir_index - 1];
7652 dwarf2_start_subfile (fe->name, dir, NULL);
7653
7654 /* Note: We don't have to watch for the main subfile here, type units
7655 don't have DW_AT_name. */
7656
7657 if (current_subfile->symtab == NULL)
7658 {
7659 /* NOTE: start_subfile will recognize when it's been passed
7660 a file it has already seen. So we can't assume there's a
7661 simple mapping from lh->file_names to subfiles,
7662 lh->file_names may contain dups. */
7663 current_subfile->symtab = allocate_symtab (current_subfile->name,
7664 objfile);
7665 }
7666
7667 fe->symtab = current_subfile->symtab;
7668 tu_group->symtabs[i] = fe->symtab;
7669 }
7670 }
7671 else
7672 {
7673 restart_symtab (0);
7674
7675 for (i = 0; i < lh->num_file_names; ++i)
7676 {
7677 struct file_entry *fe = &lh->file_names[i];
7678
7679 fe->symtab = tu_group->symtabs[i];
7680 }
7681 }
7682
7683 /* The main symtab is allocated last. Type units don't have DW_AT_name
7684 so they don't have a "real" (so to speak) symtab anyway.
7685 There is later code that will assign the main symtab to all symbols
7686 that don't have one. We need to handle the case of a symbol with a
7687 missing symtab (DW_AT_decl_file) anyway. */
7688 }
7689
7690 /* Process DW_TAG_type_unit.
7691 For TUs we want to skip the first top level sibling if it's not the
7692 actual type being defined by this TU. In this case the first top
7693 level sibling is there to provide context only. */
7694
7695 static void
7696 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7697 {
7698 struct die_info *child_die;
7699
7700 prepare_one_comp_unit (cu, die, language_minimal);
7701
7702 /* Initialize (or reinitialize) the machinery for building symtabs.
7703 We do this before processing child DIEs, so that the line header table
7704 is available for DW_AT_decl_file. */
7705 setup_type_unit_groups (die, cu);
7706
7707 if (die->child != NULL)
7708 {
7709 child_die = die->child;
7710 while (child_die && child_die->tag)
7711 {
7712 process_die (child_die, cu);
7713 child_die = sibling_die (child_die);
7714 }
7715 }
7716 }
7717 \f
7718 /* DWO files. */
7719
7720 static hashval_t
7721 hash_dwo_file (const void *item)
7722 {
7723 const struct dwo_file *dwo_file = item;
7724
7725 return htab_hash_string (dwo_file->dwo_name);
7726 }
7727
7728 static int
7729 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7730 {
7731 const struct dwo_file *lhs = item_lhs;
7732 const struct dwo_file *rhs = item_rhs;
7733
7734 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
7735 }
7736
7737 /* Allocate a hash table for DWO files. */
7738
7739 static htab_t
7740 allocate_dwo_file_hash_table (void)
7741 {
7742 struct objfile *objfile = dwarf2_per_objfile->objfile;
7743
7744 return htab_create_alloc_ex (41,
7745 hash_dwo_file,
7746 eq_dwo_file,
7747 NULL,
7748 &objfile->objfile_obstack,
7749 hashtab_obstack_allocate,
7750 dummy_obstack_deallocate);
7751 }
7752
7753 static hashval_t
7754 hash_dwo_unit (const void *item)
7755 {
7756 const struct dwo_unit *dwo_unit = item;
7757
7758 /* This drops the top 32 bits of the id, but is ok for a hash. */
7759 return dwo_unit->signature;
7760 }
7761
7762 static int
7763 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7764 {
7765 const struct dwo_unit *lhs = item_lhs;
7766 const struct dwo_unit *rhs = item_rhs;
7767
7768 /* The signature is assumed to be unique within the DWO file.
7769 So while object file CU dwo_id's always have the value zero,
7770 that's OK, assuming each object file DWO file has only one CU,
7771 and that's the rule for now. */
7772 return lhs->signature == rhs->signature;
7773 }
7774
7775 /* Allocate a hash table for DWO CUs,TUs.
7776 There is one of these tables for each of CUs,TUs for each DWO file. */
7777
7778 static htab_t
7779 allocate_dwo_unit_table (struct objfile *objfile)
7780 {
7781 /* Start out with a pretty small number.
7782 Generally DWO files contain only one CU and maybe some TUs. */
7783 return htab_create_alloc_ex (3,
7784 hash_dwo_unit,
7785 eq_dwo_unit,
7786 NULL,
7787 &objfile->objfile_obstack,
7788 hashtab_obstack_allocate,
7789 dummy_obstack_deallocate);
7790 }
7791
7792 /* This function is mapped across the sections and remembers the offset and
7793 size of each of the DWO debugging sections we are interested in. */
7794
7795 static void
7796 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
7797 {
7798 struct dwo_file *dwo_file = dwo_file_ptr;
7799 const struct dwo_section_names *names = &dwo_section_names;
7800
7801 if (section_is_p (sectp->name, &names->abbrev_dwo))
7802 {
7803 dwo_file->sections.abbrev.asection = sectp;
7804 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
7805 }
7806 else if (section_is_p (sectp->name, &names->info_dwo))
7807 {
7808 dwo_file->sections.info.asection = sectp;
7809 dwo_file->sections.info.size = bfd_get_section_size (sectp);
7810 }
7811 else if (section_is_p (sectp->name, &names->line_dwo))
7812 {
7813 dwo_file->sections.line.asection = sectp;
7814 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7815 }
7816 else if (section_is_p (sectp->name, &names->loc_dwo))
7817 {
7818 dwo_file->sections.loc.asection = sectp;
7819 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7820 }
7821 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7822 {
7823 dwo_file->sections.macinfo.asection = sectp;
7824 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7825 }
7826 else if (section_is_p (sectp->name, &names->macro_dwo))
7827 {
7828 dwo_file->sections.macro.asection = sectp;
7829 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7830 }
7831 else if (section_is_p (sectp->name, &names->str_dwo))
7832 {
7833 dwo_file->sections.str.asection = sectp;
7834 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7835 }
7836 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7837 {
7838 dwo_file->sections.str_offsets.asection = sectp;
7839 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7840 }
7841 else if (section_is_p (sectp->name, &names->types_dwo))
7842 {
7843 struct dwarf2_section_info type_section;
7844
7845 memset (&type_section, 0, sizeof (type_section));
7846 type_section.asection = sectp;
7847 type_section.size = bfd_get_section_size (sectp);
7848 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7849 &type_section);
7850 }
7851 }
7852
7853 /* Structure used to pass data to create_debug_info_hash_table_reader. */
7854
7855 struct create_dwo_info_table_data
7856 {
7857 struct dwo_file *dwo_file;
7858 htab_t cu_htab;
7859 };
7860
7861 /* die_reader_func for create_debug_info_hash_table. */
7862
7863 static void
7864 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7865 gdb_byte *info_ptr,
7866 struct die_info *comp_unit_die,
7867 int has_children,
7868 void *datap)
7869 {
7870 struct dwarf2_cu *cu = reader->cu;
7871 struct objfile *objfile = dwarf2_per_objfile->objfile;
7872 sect_offset offset = cu->per_cu->offset;
7873 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7874 struct create_dwo_info_table_data *data = datap;
7875 struct dwo_file *dwo_file = data->dwo_file;
7876 htab_t cu_htab = data->cu_htab;
7877 void **slot;
7878 struct attribute *attr;
7879 struct dwo_unit *dwo_unit;
7880
7881 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7882 if (attr == NULL)
7883 {
7884 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7885 " its dwo_id [in module %s]"),
7886 offset.sect_off, dwo_file->dwo_name);
7887 return;
7888 }
7889
7890 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7891 dwo_unit->dwo_file = dwo_file;
7892 dwo_unit->signature = DW_UNSND (attr);
7893 dwo_unit->info_or_types_section = section;
7894 dwo_unit->offset = offset;
7895 dwo_unit->length = cu->per_cu->length;
7896
7897 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7898 gdb_assert (slot != NULL);
7899 if (*slot != NULL)
7900 {
7901 const struct dwo_unit *dup_dwo_unit = *slot;
7902
7903 complaint (&symfile_complaints,
7904 _("debug entry at offset 0x%x is duplicate to the entry at"
7905 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7906 offset.sect_off, dup_dwo_unit->offset.sect_off,
7907 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7908 dwo_file->dwo_name);
7909 }
7910 else
7911 *slot = dwo_unit;
7912
7913 if (dwarf2_read_debug)
7914 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7915 offset.sect_off,
7916 phex (dwo_unit->signature,
7917 sizeof (dwo_unit->signature)));
7918 }
7919
7920 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7921
7922 static htab_t
7923 create_debug_info_hash_table (struct dwo_file *dwo_file)
7924 {
7925 struct objfile *objfile = dwarf2_per_objfile->objfile;
7926 struct dwarf2_section_info *section = &dwo_file->sections.info;
7927 bfd *abfd;
7928 htab_t cu_htab;
7929 gdb_byte *info_ptr, *end_ptr;
7930 struct create_dwo_info_table_data create_dwo_info_table_data;
7931
7932 dwarf2_read_section (objfile, section);
7933 info_ptr = section->buffer;
7934
7935 if (info_ptr == NULL)
7936 return NULL;
7937
7938 /* We can't set abfd until now because the section may be empty or
7939 not present, in which case section->asection will be NULL. */
7940 abfd = section->asection->owner;
7941
7942 if (dwarf2_read_debug)
7943 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7944 bfd_get_filename (abfd));
7945
7946 cu_htab = allocate_dwo_unit_table (objfile);
7947
7948 create_dwo_info_table_data.dwo_file = dwo_file;
7949 create_dwo_info_table_data.cu_htab = cu_htab;
7950
7951 end_ptr = info_ptr + section->size;
7952 while (info_ptr < end_ptr)
7953 {
7954 struct dwarf2_per_cu_data per_cu;
7955
7956 memset (&per_cu, 0, sizeof (per_cu));
7957 per_cu.objfile = objfile;
7958 per_cu.is_debug_types = 0;
7959 per_cu.offset.sect_off = info_ptr - section->buffer;
7960 per_cu.info_or_types_section = section;
7961
7962 init_cutu_and_read_dies_no_follow (&per_cu,
7963 &dwo_file->sections.abbrev,
7964 dwo_file,
7965 create_debug_info_hash_table_reader,
7966 &create_dwo_info_table_data);
7967
7968 info_ptr += per_cu.length;
7969 }
7970
7971 return cu_htab;
7972 }
7973
7974 /* Subroutine of open_dwo_file to simplify it.
7975 Open the file specified by FILE_NAME and hand it off to BFD for
7976 preliminary analysis. Return a newly initialized bfd *, which
7977 includes a canonicalized copy of FILE_NAME.
7978 In case of trouble, return NULL.
7979 NOTE: This function is derived from symfile_bfd_open. */
7980
7981 static bfd *
7982 try_open_dwo_file (const char *file_name)
7983 {
7984 bfd *sym_bfd;
7985 int desc;
7986 char *absolute_name;
7987
7988 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7989 O_RDONLY | O_BINARY, &absolute_name);
7990 if (desc < 0)
7991 return NULL;
7992
7993 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
7994 if (!sym_bfd)
7995 {
7996 xfree (absolute_name);
7997 return NULL;
7998 }
7999 gdb_bfd_stash_filename (sym_bfd);
8000 xfree (absolute_name);
8001 bfd_set_cacheable (sym_bfd, 1);
8002
8003 if (!bfd_check_format (sym_bfd, bfd_object))
8004 {
8005 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8006 return NULL;
8007 }
8008
8009 return sym_bfd;
8010 }
8011
8012 /* Try to open DWO file DWO_NAME.
8013 COMP_DIR is the DW_AT_comp_dir attribute.
8014 The result is the bfd handle of the file.
8015 If there is a problem finding or opening the file, return NULL.
8016 Upon success, the canonicalized path of the file is stored in the bfd,
8017 same as symfile_bfd_open. */
8018
8019 static bfd *
8020 open_dwo_file (const char *dwo_name, const char *comp_dir)
8021 {
8022 bfd *abfd;
8023
8024 if (IS_ABSOLUTE_PATH (dwo_name))
8025 return try_open_dwo_file (dwo_name);
8026
8027 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8028
8029 if (comp_dir != NULL)
8030 {
8031 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8032
8033 /* NOTE: If comp_dir is a relative path, this will also try the
8034 search path, which seems useful. */
8035 abfd = try_open_dwo_file (path_to_try);
8036 xfree (path_to_try);
8037 if (abfd != NULL)
8038 return abfd;
8039 }
8040
8041 /* That didn't work, try debug-file-directory, which, despite its name,
8042 is a list of paths. */
8043
8044 if (*debug_file_directory == '\0')
8045 return NULL;
8046
8047 return try_open_dwo_file (dwo_name);
8048 }
8049
8050 /* Initialize the use of the DWO file specified by DWO_NAME. */
8051
8052 static struct dwo_file *
8053 init_dwo_file (const char *dwo_name, const char *comp_dir)
8054 {
8055 struct objfile *objfile = dwarf2_per_objfile->objfile;
8056 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8057 struct dwo_file);
8058 bfd *abfd;
8059 struct cleanup *cleanups;
8060
8061 if (dwarf2_read_debug)
8062 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8063
8064 abfd = open_dwo_file (dwo_name, comp_dir);
8065 if (abfd == NULL)
8066 return NULL;
8067 dwo_file->dwo_name = dwo_name;
8068 dwo_file->dwo_bfd = abfd;
8069
8070 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8071
8072 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8073
8074 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8075
8076 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8077 dwo_file->sections.types);
8078
8079 discard_cleanups (cleanups);
8080
8081 return dwo_file;
8082 }
8083
8084 /* Lookup DWO file DWO_NAME. */
8085
8086 static struct dwo_file *
8087 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8088 {
8089 struct dwo_file *dwo_file;
8090 struct dwo_file find_entry;
8091 void **slot;
8092
8093 if (dwarf2_per_objfile->dwo_files == NULL)
8094 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8095
8096 /* Have we already seen this DWO file? */
8097 find_entry.dwo_name = dwo_name;
8098 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8099
8100 /* If not, read it in and build a table of the DWOs it contains. */
8101 if (*slot == NULL)
8102 *slot = init_dwo_file (dwo_name, comp_dir);
8103
8104 /* NOTE: This will be NULL if unable to open the file. */
8105 dwo_file = *slot;
8106
8107 return dwo_file;
8108 }
8109
8110 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8111 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8112 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8113 nomenclature as TUs).
8114 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8115 (dwo_id mismatch or couldn't find the DWO file). */
8116
8117 static struct dwo_unit *
8118 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8119 char *dwo_name, const char *comp_dir,
8120 ULONGEST signature)
8121 {
8122 struct objfile *objfile = dwarf2_per_objfile->objfile;
8123 struct dwo_file *dwo_file;
8124
8125 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8126 if (dwo_file == NULL)
8127 return NULL;
8128
8129 /* Look up the DWO using its signature(dwo_id). */
8130
8131 if (dwo_file->cus != NULL)
8132 {
8133 struct dwo_unit find_dwo_cu, *dwo_cu;
8134
8135 find_dwo_cu.signature = signature;
8136 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8137
8138 if (dwo_cu != NULL)
8139 return dwo_cu;
8140 }
8141
8142 /* We didn't find it. This must mean a dwo_id mismatch. */
8143
8144 complaint (&symfile_complaints,
8145 _("Could not find DWO CU referenced by CU at offset 0x%x"
8146 " [in module %s]"),
8147 this_cu->offset.sect_off, objfile->name);
8148 return NULL;
8149 }
8150
8151 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8152 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8153 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8154 (dwo_id mismatch or couldn't find the DWO file). */
8155
8156 static struct dwo_unit *
8157 lookup_dwo_type_unit (struct signatured_type *this_tu,
8158 char *dwo_name, const char *comp_dir)
8159 {
8160 struct objfile *objfile = dwarf2_per_objfile->objfile;
8161 struct dwo_file *dwo_file;
8162
8163 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8164 if (dwo_file == NULL)
8165 return NULL;
8166
8167 /* Look up the DWO using its signature(dwo_id). */
8168
8169 if (dwo_file->tus != NULL)
8170 {
8171 struct dwo_unit find_dwo_tu, *dwo_tu;
8172
8173 find_dwo_tu.signature = this_tu->signature;
8174 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8175
8176 if (dwo_tu != NULL)
8177 return dwo_tu;
8178 }
8179
8180 /* We didn't find it. This must mean a dwo_id mismatch. */
8181
8182 complaint (&symfile_complaints,
8183 _("Could not find DWO TU referenced by TU at offset 0x%x"
8184 " [in module %s]"),
8185 this_tu->per_cu.offset.sect_off, objfile->name);
8186 return NULL;
8187 }
8188
8189 /* Free all resources associated with DWO_FILE.
8190 Close the DWO file and munmap the sections.
8191 All memory should be on the objfile obstack. */
8192
8193 static void
8194 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8195 {
8196 int ix;
8197 struct dwarf2_section_info *section;
8198
8199 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8200 gdb_bfd_unref (dwo_file->dwo_bfd);
8201
8202 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8203 }
8204
8205 /* Wrapper for free_dwo_file for use in cleanups. */
8206
8207 static void
8208 free_dwo_file_cleanup (void *arg)
8209 {
8210 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8211 struct objfile *objfile = dwarf2_per_objfile->objfile;
8212
8213 free_dwo_file (dwo_file, objfile);
8214 }
8215
8216 /* Traversal function for free_dwo_files. */
8217
8218 static int
8219 free_dwo_file_from_slot (void **slot, void *info)
8220 {
8221 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8222 struct objfile *objfile = (struct objfile *) info;
8223
8224 free_dwo_file (dwo_file, objfile);
8225
8226 return 1;
8227 }
8228
8229 /* Free all resources associated with DWO_FILES. */
8230
8231 static void
8232 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8233 {
8234 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8235 }
8236 \f
8237 /* Read in various DIEs. */
8238
8239 /* qsort helper for inherit_abstract_dies. */
8240
8241 static int
8242 unsigned_int_compar (const void *ap, const void *bp)
8243 {
8244 unsigned int a = *(unsigned int *) ap;
8245 unsigned int b = *(unsigned int *) bp;
8246
8247 return (a > b) - (b > a);
8248 }
8249
8250 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8251 Inherit only the children of the DW_AT_abstract_origin DIE not being
8252 already referenced by DW_AT_abstract_origin from the children of the
8253 current DIE. */
8254
8255 static void
8256 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8257 {
8258 struct die_info *child_die;
8259 unsigned die_children_count;
8260 /* CU offsets which were referenced by children of the current DIE. */
8261 sect_offset *offsets;
8262 sect_offset *offsets_end, *offsetp;
8263 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8264 struct die_info *origin_die;
8265 /* Iterator of the ORIGIN_DIE children. */
8266 struct die_info *origin_child_die;
8267 struct cleanup *cleanups;
8268 struct attribute *attr;
8269 struct dwarf2_cu *origin_cu;
8270 struct pending **origin_previous_list_in_scope;
8271
8272 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8273 if (!attr)
8274 return;
8275
8276 /* Note that following die references may follow to a die in a
8277 different cu. */
8278
8279 origin_cu = cu;
8280 origin_die = follow_die_ref (die, attr, &origin_cu);
8281
8282 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8283 symbols in. */
8284 origin_previous_list_in_scope = origin_cu->list_in_scope;
8285 origin_cu->list_in_scope = cu->list_in_scope;
8286
8287 if (die->tag != origin_die->tag
8288 && !(die->tag == DW_TAG_inlined_subroutine
8289 && origin_die->tag == DW_TAG_subprogram))
8290 complaint (&symfile_complaints,
8291 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8292 die->offset.sect_off, origin_die->offset.sect_off);
8293
8294 child_die = die->child;
8295 die_children_count = 0;
8296 while (child_die && child_die->tag)
8297 {
8298 child_die = sibling_die (child_die);
8299 die_children_count++;
8300 }
8301 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8302 cleanups = make_cleanup (xfree, offsets);
8303
8304 offsets_end = offsets;
8305 child_die = die->child;
8306 while (child_die && child_die->tag)
8307 {
8308 /* For each CHILD_DIE, find the corresponding child of
8309 ORIGIN_DIE. If there is more than one layer of
8310 DW_AT_abstract_origin, follow them all; there shouldn't be,
8311 but GCC versions at least through 4.4 generate this (GCC PR
8312 40573). */
8313 struct die_info *child_origin_die = child_die;
8314 struct dwarf2_cu *child_origin_cu = cu;
8315
8316 while (1)
8317 {
8318 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8319 child_origin_cu);
8320 if (attr == NULL)
8321 break;
8322 child_origin_die = follow_die_ref (child_origin_die, attr,
8323 &child_origin_cu);
8324 }
8325
8326 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8327 counterpart may exist. */
8328 if (child_origin_die != child_die)
8329 {
8330 if (child_die->tag != child_origin_die->tag
8331 && !(child_die->tag == DW_TAG_inlined_subroutine
8332 && child_origin_die->tag == DW_TAG_subprogram))
8333 complaint (&symfile_complaints,
8334 _("Child DIE 0x%x and its abstract origin 0x%x have "
8335 "different tags"), child_die->offset.sect_off,
8336 child_origin_die->offset.sect_off);
8337 if (child_origin_die->parent != origin_die)
8338 complaint (&symfile_complaints,
8339 _("Child DIE 0x%x and its abstract origin 0x%x have "
8340 "different parents"), child_die->offset.sect_off,
8341 child_origin_die->offset.sect_off);
8342 else
8343 *offsets_end++ = child_origin_die->offset;
8344 }
8345 child_die = sibling_die (child_die);
8346 }
8347 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8348 unsigned_int_compar);
8349 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8350 if (offsetp[-1].sect_off == offsetp->sect_off)
8351 complaint (&symfile_complaints,
8352 _("Multiple children of DIE 0x%x refer "
8353 "to DIE 0x%x as their abstract origin"),
8354 die->offset.sect_off, offsetp->sect_off);
8355
8356 offsetp = offsets;
8357 origin_child_die = origin_die->child;
8358 while (origin_child_die && origin_child_die->tag)
8359 {
8360 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
8361 while (offsetp < offsets_end
8362 && offsetp->sect_off < origin_child_die->offset.sect_off)
8363 offsetp++;
8364 if (offsetp >= offsets_end
8365 || offsetp->sect_off > origin_child_die->offset.sect_off)
8366 {
8367 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
8368 process_die (origin_child_die, origin_cu);
8369 }
8370 origin_child_die = sibling_die (origin_child_die);
8371 }
8372 origin_cu->list_in_scope = origin_previous_list_in_scope;
8373
8374 do_cleanups (cleanups);
8375 }
8376
8377 static void
8378 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8379 {
8380 struct objfile *objfile = cu->objfile;
8381 struct context_stack *new;
8382 CORE_ADDR lowpc;
8383 CORE_ADDR highpc;
8384 struct die_info *child_die;
8385 struct attribute *attr, *call_line, *call_file;
8386 char *name;
8387 CORE_ADDR baseaddr;
8388 struct block *block;
8389 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8390 VEC (symbolp) *template_args = NULL;
8391 struct template_symbol *templ_func = NULL;
8392
8393 if (inlined_func)
8394 {
8395 /* If we do not have call site information, we can't show the
8396 caller of this inlined function. That's too confusing, so
8397 only use the scope for local variables. */
8398 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8399 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8400 if (call_line == NULL || call_file == NULL)
8401 {
8402 read_lexical_block_scope (die, cu);
8403 return;
8404 }
8405 }
8406
8407 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8408
8409 name = dwarf2_name (die, cu);
8410
8411 /* Ignore functions with missing or empty names. These are actually
8412 illegal according to the DWARF standard. */
8413 if (name == NULL)
8414 {
8415 complaint (&symfile_complaints,
8416 _("missing name for subprogram DIE at %d"),
8417 die->offset.sect_off);
8418 return;
8419 }
8420
8421 /* Ignore functions with missing or invalid low and high pc attributes. */
8422 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8423 {
8424 attr = dwarf2_attr (die, DW_AT_external, cu);
8425 if (!attr || !DW_UNSND (attr))
8426 complaint (&symfile_complaints,
8427 _("cannot get low and high bounds "
8428 "for subprogram DIE at %d"),
8429 die->offset.sect_off);
8430 return;
8431 }
8432
8433 lowpc += baseaddr;
8434 highpc += baseaddr;
8435
8436 /* If we have any template arguments, then we must allocate a
8437 different sort of symbol. */
8438 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8439 {
8440 if (child_die->tag == DW_TAG_template_type_param
8441 || child_die->tag == DW_TAG_template_value_param)
8442 {
8443 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8444 struct template_symbol);
8445 templ_func->base.is_cplus_template_function = 1;
8446 break;
8447 }
8448 }
8449
8450 new = push_context (0, lowpc);
8451 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8452 (struct symbol *) templ_func);
8453
8454 /* If there is a location expression for DW_AT_frame_base, record
8455 it. */
8456 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8457 if (attr)
8458 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8459 expression is being recorded directly in the function's symbol
8460 and not in a separate frame-base object. I guess this hack is
8461 to avoid adding some sort of frame-base adjunct/annex to the
8462 function's symbol :-(. The problem with doing this is that it
8463 results in a function symbol with a location expression that
8464 has nothing to do with the location of the function, ouch! The
8465 relationship should be: a function's symbol has-a frame base; a
8466 frame-base has-a location expression. */
8467 dwarf2_symbol_mark_computed (attr, new->name, cu);
8468
8469 cu->list_in_scope = &local_symbols;
8470
8471 if (die->child != NULL)
8472 {
8473 child_die = die->child;
8474 while (child_die && child_die->tag)
8475 {
8476 if (child_die->tag == DW_TAG_template_type_param
8477 || child_die->tag == DW_TAG_template_value_param)
8478 {
8479 struct symbol *arg = new_symbol (child_die, NULL, cu);
8480
8481 if (arg != NULL)
8482 VEC_safe_push (symbolp, template_args, arg);
8483 }
8484 else
8485 process_die (child_die, cu);
8486 child_die = sibling_die (child_die);
8487 }
8488 }
8489
8490 inherit_abstract_dies (die, cu);
8491
8492 /* If we have a DW_AT_specification, we might need to import using
8493 directives from the context of the specification DIE. See the
8494 comment in determine_prefix. */
8495 if (cu->language == language_cplus
8496 && dwarf2_attr (die, DW_AT_specification, cu))
8497 {
8498 struct dwarf2_cu *spec_cu = cu;
8499 struct die_info *spec_die = die_specification (die, &spec_cu);
8500
8501 while (spec_die)
8502 {
8503 child_die = spec_die->child;
8504 while (child_die && child_die->tag)
8505 {
8506 if (child_die->tag == DW_TAG_imported_module)
8507 process_die (child_die, spec_cu);
8508 child_die = sibling_die (child_die);
8509 }
8510
8511 /* In some cases, GCC generates specification DIEs that
8512 themselves contain DW_AT_specification attributes. */
8513 spec_die = die_specification (spec_die, &spec_cu);
8514 }
8515 }
8516
8517 new = pop_context ();
8518 /* Make a block for the local symbols within. */
8519 block = finish_block (new->name, &local_symbols, new->old_blocks,
8520 lowpc, highpc, objfile);
8521
8522 /* For C++, set the block's scope. */
8523 if (cu->language == language_cplus || cu->language == language_fortran)
8524 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8525 determine_prefix (die, cu),
8526 processing_has_namespace_info);
8527
8528 /* If we have address ranges, record them. */
8529 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8530
8531 /* Attach template arguments to function. */
8532 if (! VEC_empty (symbolp, template_args))
8533 {
8534 gdb_assert (templ_func != NULL);
8535
8536 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8537 templ_func->template_arguments
8538 = obstack_alloc (&objfile->objfile_obstack,
8539 (templ_func->n_template_arguments
8540 * sizeof (struct symbol *)));
8541 memcpy (templ_func->template_arguments,
8542 VEC_address (symbolp, template_args),
8543 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8544 VEC_free (symbolp, template_args);
8545 }
8546
8547 /* In C++, we can have functions nested inside functions (e.g., when
8548 a function declares a class that has methods). This means that
8549 when we finish processing a function scope, we may need to go
8550 back to building a containing block's symbol lists. */
8551 local_symbols = new->locals;
8552 param_symbols = new->params;
8553 using_directives = new->using_directives;
8554
8555 /* If we've finished processing a top-level function, subsequent
8556 symbols go in the file symbol list. */
8557 if (outermost_context_p ())
8558 cu->list_in_scope = &file_symbols;
8559 }
8560
8561 /* Process all the DIES contained within a lexical block scope. Start
8562 a new scope, process the dies, and then close the scope. */
8563
8564 static void
8565 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8566 {
8567 struct objfile *objfile = cu->objfile;
8568 struct context_stack *new;
8569 CORE_ADDR lowpc, highpc;
8570 struct die_info *child_die;
8571 CORE_ADDR baseaddr;
8572
8573 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8574
8575 /* Ignore blocks with missing or invalid low and high pc attributes. */
8576 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8577 as multiple lexical blocks? Handling children in a sane way would
8578 be nasty. Might be easier to properly extend generic blocks to
8579 describe ranges. */
8580 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8581 return;
8582 lowpc += baseaddr;
8583 highpc += baseaddr;
8584
8585 push_context (0, lowpc);
8586 if (die->child != NULL)
8587 {
8588 child_die = die->child;
8589 while (child_die && child_die->tag)
8590 {
8591 process_die (child_die, cu);
8592 child_die = sibling_die (child_die);
8593 }
8594 }
8595 new = pop_context ();
8596
8597 if (local_symbols != NULL || using_directives != NULL)
8598 {
8599 struct block *block
8600 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8601 highpc, objfile);
8602
8603 /* Note that recording ranges after traversing children, as we
8604 do here, means that recording a parent's ranges entails
8605 walking across all its children's ranges as they appear in
8606 the address map, which is quadratic behavior.
8607
8608 It would be nicer to record the parent's ranges before
8609 traversing its children, simply overriding whatever you find
8610 there. But since we don't even decide whether to create a
8611 block until after we've traversed its children, that's hard
8612 to do. */
8613 dwarf2_record_block_ranges (die, block, baseaddr, cu);
8614 }
8615 local_symbols = new->locals;
8616 using_directives = new->using_directives;
8617 }
8618
8619 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8620
8621 static void
8622 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8623 {
8624 struct objfile *objfile = cu->objfile;
8625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8626 CORE_ADDR pc, baseaddr;
8627 struct attribute *attr;
8628 struct call_site *call_site, call_site_local;
8629 void **slot;
8630 int nparams;
8631 struct die_info *child_die;
8632
8633 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8634
8635 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8636 if (!attr)
8637 {
8638 complaint (&symfile_complaints,
8639 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8640 "DIE 0x%x [in module %s]"),
8641 die->offset.sect_off, objfile->name);
8642 return;
8643 }
8644 pc = DW_ADDR (attr) + baseaddr;
8645
8646 if (cu->call_site_htab == NULL)
8647 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8648 NULL, &objfile->objfile_obstack,
8649 hashtab_obstack_allocate, NULL);
8650 call_site_local.pc = pc;
8651 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8652 if (*slot != NULL)
8653 {
8654 complaint (&symfile_complaints,
8655 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8656 "DIE 0x%x [in module %s]"),
8657 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8658 return;
8659 }
8660
8661 /* Count parameters at the caller. */
8662
8663 nparams = 0;
8664 for (child_die = die->child; child_die && child_die->tag;
8665 child_die = sibling_die (child_die))
8666 {
8667 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8668 {
8669 complaint (&symfile_complaints,
8670 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8671 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8672 child_die->tag, child_die->offset.sect_off, objfile->name);
8673 continue;
8674 }
8675
8676 nparams++;
8677 }
8678
8679 call_site = obstack_alloc (&objfile->objfile_obstack,
8680 (sizeof (*call_site)
8681 + (sizeof (*call_site->parameter)
8682 * (nparams - 1))));
8683 *slot = call_site;
8684 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8685 call_site->pc = pc;
8686
8687 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8688 {
8689 struct die_info *func_die;
8690
8691 /* Skip also over DW_TAG_inlined_subroutine. */
8692 for (func_die = die->parent;
8693 func_die && func_die->tag != DW_TAG_subprogram
8694 && func_die->tag != DW_TAG_subroutine_type;
8695 func_die = func_die->parent);
8696
8697 /* DW_AT_GNU_all_call_sites is a superset
8698 of DW_AT_GNU_all_tail_call_sites. */
8699 if (func_die
8700 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8701 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8702 {
8703 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8704 not complete. But keep CALL_SITE for look ups via call_site_htab,
8705 both the initial caller containing the real return address PC and
8706 the final callee containing the current PC of a chain of tail
8707 calls do not need to have the tail call list complete. But any
8708 function candidate for a virtual tail call frame searched via
8709 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8710 determined unambiguously. */
8711 }
8712 else
8713 {
8714 struct type *func_type = NULL;
8715
8716 if (func_die)
8717 func_type = get_die_type (func_die, cu);
8718 if (func_type != NULL)
8719 {
8720 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
8721
8722 /* Enlist this call site to the function. */
8723 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
8724 TYPE_TAIL_CALL_LIST (func_type) = call_site;
8725 }
8726 else
8727 complaint (&symfile_complaints,
8728 _("Cannot find function owning DW_TAG_GNU_call_site "
8729 "DIE 0x%x [in module %s]"),
8730 die->offset.sect_off, objfile->name);
8731 }
8732 }
8733
8734 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
8735 if (attr == NULL)
8736 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8737 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
8738 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
8739 /* Keep NULL DWARF_BLOCK. */;
8740 else if (attr_form_is_block (attr))
8741 {
8742 struct dwarf2_locexpr_baton *dlbaton;
8743
8744 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
8745 dlbaton->data = DW_BLOCK (attr)->data;
8746 dlbaton->size = DW_BLOCK (attr)->size;
8747 dlbaton->per_cu = cu->per_cu;
8748
8749 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
8750 }
8751 else if (is_ref_attr (attr))
8752 {
8753 struct dwarf2_cu *target_cu = cu;
8754 struct die_info *target_die;
8755
8756 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
8757 gdb_assert (target_cu->objfile == objfile);
8758 if (die_is_declaration (target_die, target_cu))
8759 {
8760 const char *target_physname;
8761
8762 target_physname = dwarf2_physname (NULL, target_die, target_cu);
8763 if (target_physname == NULL)
8764 complaint (&symfile_complaints,
8765 _("DW_AT_GNU_call_site_target target DIE has invalid "
8766 "physname, for referencing DIE 0x%x [in module %s]"),
8767 die->offset.sect_off, objfile->name);
8768 else
8769 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
8770 }
8771 else
8772 {
8773 CORE_ADDR lowpc;
8774
8775 /* DW_AT_entry_pc should be preferred. */
8776 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
8777 complaint (&symfile_complaints,
8778 _("DW_AT_GNU_call_site_target target DIE has invalid "
8779 "low pc, for referencing DIE 0x%x [in module %s]"),
8780 die->offset.sect_off, objfile->name);
8781 else
8782 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
8783 }
8784 }
8785 else
8786 complaint (&symfile_complaints,
8787 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
8788 "block nor reference, for DIE 0x%x [in module %s]"),
8789 die->offset.sect_off, objfile->name);
8790
8791 call_site->per_cu = cu->per_cu;
8792
8793 for (child_die = die->child;
8794 child_die && child_die->tag;
8795 child_die = sibling_die (child_die))
8796 {
8797 struct call_site_parameter *parameter;
8798 struct attribute *loc, *origin;
8799
8800 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8801 {
8802 /* Already printed the complaint above. */
8803 continue;
8804 }
8805
8806 gdb_assert (call_site->parameter_count < nparams);
8807 parameter = &call_site->parameter[call_site->parameter_count];
8808
8809 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8810 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8811 register is contained in DW_AT_GNU_call_site_value. */
8812
8813 loc = dwarf2_attr (child_die, DW_AT_location, cu);
8814 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8815 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8816 {
8817 sect_offset offset;
8818
8819 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8820 offset = dwarf2_get_ref_die_offset (origin);
8821 if (!offset_in_cu_p (&cu->header, offset))
8822 {
8823 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
8824 binding can be done only inside one CU. Such referenced DIE
8825 therefore cannot be even moved to DW_TAG_partial_unit. */
8826 complaint (&symfile_complaints,
8827 _("DW_AT_abstract_origin offset is not in CU for "
8828 "DW_TAG_GNU_call_site child DIE 0x%x "
8829 "[in module %s]"),
8830 child_die->offset.sect_off, objfile->name);
8831 continue;
8832 }
8833 parameter->u.param_offset.cu_off = (offset.sect_off
8834 - cu->header.offset.sect_off);
8835 }
8836 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
8837 {
8838 complaint (&symfile_complaints,
8839 _("No DW_FORM_block* DW_AT_location for "
8840 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8841 child_die->offset.sect_off, objfile->name);
8842 continue;
8843 }
8844 else
8845 {
8846 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8847 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8848 if (parameter->u.dwarf_reg != -1)
8849 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8850 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8851 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8852 &parameter->u.fb_offset))
8853 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8854 else
8855 {
8856 complaint (&symfile_complaints,
8857 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8858 "for DW_FORM_block* DW_AT_location is supported for "
8859 "DW_TAG_GNU_call_site child DIE 0x%x "
8860 "[in module %s]"),
8861 child_die->offset.sect_off, objfile->name);
8862 continue;
8863 }
8864 }
8865
8866 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8867 if (!attr_form_is_block (attr))
8868 {
8869 complaint (&symfile_complaints,
8870 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8871 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8872 child_die->offset.sect_off, objfile->name);
8873 continue;
8874 }
8875 parameter->value = DW_BLOCK (attr)->data;
8876 parameter->value_size = DW_BLOCK (attr)->size;
8877
8878 /* Parameters are not pre-cleared by memset above. */
8879 parameter->data_value = NULL;
8880 parameter->data_value_size = 0;
8881 call_site->parameter_count++;
8882
8883 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8884 if (attr)
8885 {
8886 if (!attr_form_is_block (attr))
8887 complaint (&symfile_complaints,
8888 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8889 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8890 child_die->offset.sect_off, objfile->name);
8891 else
8892 {
8893 parameter->data_value = DW_BLOCK (attr)->data;
8894 parameter->data_value_size = DW_BLOCK (attr)->size;
8895 }
8896 }
8897 }
8898 }
8899
8900 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
8901 Return 1 if the attributes are present and valid, otherwise, return 0.
8902 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
8903
8904 static int
8905 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8906 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8907 struct partial_symtab *ranges_pst)
8908 {
8909 struct objfile *objfile = cu->objfile;
8910 struct comp_unit_head *cu_header = &cu->header;
8911 bfd *obfd = objfile->obfd;
8912 unsigned int addr_size = cu_header->addr_size;
8913 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8914 /* Base address selection entry. */
8915 CORE_ADDR base;
8916 int found_base;
8917 unsigned int dummy;
8918 gdb_byte *buffer;
8919 CORE_ADDR marker;
8920 int low_set;
8921 CORE_ADDR low = 0;
8922 CORE_ADDR high = 0;
8923 CORE_ADDR baseaddr;
8924
8925 found_base = cu->base_known;
8926 base = cu->base_address;
8927
8928 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
8929 if (offset >= dwarf2_per_objfile->ranges.size)
8930 {
8931 complaint (&symfile_complaints,
8932 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8933 offset);
8934 return 0;
8935 }
8936 buffer = dwarf2_per_objfile->ranges.buffer + offset;
8937
8938 /* Read in the largest possible address. */
8939 marker = read_address (obfd, buffer, cu, &dummy);
8940 if ((marker & mask) == mask)
8941 {
8942 /* If we found the largest possible address, then
8943 read the base address. */
8944 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8945 buffer += 2 * addr_size;
8946 offset += 2 * addr_size;
8947 found_base = 1;
8948 }
8949
8950 low_set = 0;
8951
8952 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8953
8954 while (1)
8955 {
8956 CORE_ADDR range_beginning, range_end;
8957
8958 range_beginning = read_address (obfd, buffer, cu, &dummy);
8959 buffer += addr_size;
8960 range_end = read_address (obfd, buffer, cu, &dummy);
8961 buffer += addr_size;
8962 offset += 2 * addr_size;
8963
8964 /* An end of list marker is a pair of zero addresses. */
8965 if (range_beginning == 0 && range_end == 0)
8966 /* Found the end of list entry. */
8967 break;
8968
8969 /* Each base address selection entry is a pair of 2 values.
8970 The first is the largest possible address, the second is
8971 the base address. Check for a base address here. */
8972 if ((range_beginning & mask) == mask)
8973 {
8974 /* If we found the largest possible address, then
8975 read the base address. */
8976 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8977 found_base = 1;
8978 continue;
8979 }
8980
8981 if (!found_base)
8982 {
8983 /* We have no valid base address for the ranges
8984 data. */
8985 complaint (&symfile_complaints,
8986 _("Invalid .debug_ranges data (no base address)"));
8987 return 0;
8988 }
8989
8990 if (range_beginning > range_end)
8991 {
8992 /* Inverted range entries are invalid. */
8993 complaint (&symfile_complaints,
8994 _("Invalid .debug_ranges data (inverted range)"));
8995 return 0;
8996 }
8997
8998 /* Empty range entries have no effect. */
8999 if (range_beginning == range_end)
9000 continue;
9001
9002 range_beginning += base;
9003 range_end += base;
9004
9005 if (ranges_pst != NULL)
9006 addrmap_set_empty (objfile->psymtabs_addrmap,
9007 range_beginning + baseaddr,
9008 range_end - 1 + baseaddr,
9009 ranges_pst);
9010
9011 /* FIXME: This is recording everything as a low-high
9012 segment of consecutive addresses. We should have a
9013 data structure for discontiguous block ranges
9014 instead. */
9015 if (! low_set)
9016 {
9017 low = range_beginning;
9018 high = range_end;
9019 low_set = 1;
9020 }
9021 else
9022 {
9023 if (range_beginning < low)
9024 low = range_beginning;
9025 if (range_end > high)
9026 high = range_end;
9027 }
9028 }
9029
9030 if (! low_set)
9031 /* If the first entry is an end-of-list marker, the range
9032 describes an empty scope, i.e. no instructions. */
9033 return 0;
9034
9035 if (low_return)
9036 *low_return = low;
9037 if (high_return)
9038 *high_return = high;
9039 return 1;
9040 }
9041
9042 /* Get low and high pc attributes from a die. Return 1 if the attributes
9043 are present and valid, otherwise, return 0. Return -1 if the range is
9044 discontinuous, i.e. derived from DW_AT_ranges information. */
9045
9046 static int
9047 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9048 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9049 struct partial_symtab *pst)
9050 {
9051 struct attribute *attr;
9052 struct attribute *attr_high;
9053 CORE_ADDR low = 0;
9054 CORE_ADDR high = 0;
9055 int ret = 0;
9056
9057 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9058 if (attr_high)
9059 {
9060 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9061 if (attr)
9062 {
9063 low = DW_ADDR (attr);
9064 if (attr_high->form == DW_FORM_addr
9065 || attr_high->form == DW_FORM_GNU_addr_index)
9066 high = DW_ADDR (attr_high);
9067 else
9068 high = low + DW_UNSND (attr_high);
9069 }
9070 else
9071 /* Found high w/o low attribute. */
9072 return 0;
9073
9074 /* Found consecutive range of addresses. */
9075 ret = 1;
9076 }
9077 else
9078 {
9079 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9080 if (attr != NULL)
9081 {
9082 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9083
9084 /* Value of the DW_AT_ranges attribute is the offset in the
9085 .debug_ranges section. */
9086 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9087 return 0;
9088 /* Found discontinuous range of addresses. */
9089 ret = -1;
9090 }
9091 }
9092
9093 /* read_partial_die has also the strict LOW < HIGH requirement. */
9094 if (high <= low)
9095 return 0;
9096
9097 /* When using the GNU linker, .gnu.linkonce. sections are used to
9098 eliminate duplicate copies of functions and vtables and such.
9099 The linker will arbitrarily choose one and discard the others.
9100 The AT_*_pc values for such functions refer to local labels in
9101 these sections. If the section from that file was discarded, the
9102 labels are not in the output, so the relocs get a value of 0.
9103 If this is a discarded function, mark the pc bounds as invalid,
9104 so that GDB will ignore it. */
9105 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9106 return 0;
9107
9108 *lowpc = low;
9109 if (highpc)
9110 *highpc = high;
9111 return ret;
9112 }
9113
9114 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9115 its low and high PC addresses. Do nothing if these addresses could not
9116 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9117 and HIGHPC to the high address if greater than HIGHPC. */
9118
9119 static void
9120 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9122 struct dwarf2_cu *cu)
9123 {
9124 CORE_ADDR low, high;
9125 struct die_info *child = die->child;
9126
9127 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9128 {
9129 *lowpc = min (*lowpc, low);
9130 *highpc = max (*highpc, high);
9131 }
9132
9133 /* If the language does not allow nested subprograms (either inside
9134 subprograms or lexical blocks), we're done. */
9135 if (cu->language != language_ada)
9136 return;
9137
9138 /* Check all the children of the given DIE. If it contains nested
9139 subprograms, then check their pc bounds. Likewise, we need to
9140 check lexical blocks as well, as they may also contain subprogram
9141 definitions. */
9142 while (child && child->tag)
9143 {
9144 if (child->tag == DW_TAG_subprogram
9145 || child->tag == DW_TAG_lexical_block)
9146 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9147 child = sibling_die (child);
9148 }
9149 }
9150
9151 /* Get the low and high pc's represented by the scope DIE, and store
9152 them in *LOWPC and *HIGHPC. If the correct values can't be
9153 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9154
9155 static void
9156 get_scope_pc_bounds (struct die_info *die,
9157 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9158 struct dwarf2_cu *cu)
9159 {
9160 CORE_ADDR best_low = (CORE_ADDR) -1;
9161 CORE_ADDR best_high = (CORE_ADDR) 0;
9162 CORE_ADDR current_low, current_high;
9163
9164 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9165 {
9166 best_low = current_low;
9167 best_high = current_high;
9168 }
9169 else
9170 {
9171 struct die_info *child = die->child;
9172
9173 while (child && child->tag)
9174 {
9175 switch (child->tag) {
9176 case DW_TAG_subprogram:
9177 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9178 break;
9179 case DW_TAG_namespace:
9180 case DW_TAG_module:
9181 /* FIXME: carlton/2004-01-16: Should we do this for
9182 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9183 that current GCC's always emit the DIEs corresponding
9184 to definitions of methods of classes as children of a
9185 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9186 the DIEs giving the declarations, which could be
9187 anywhere). But I don't see any reason why the
9188 standards says that they have to be there. */
9189 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9190
9191 if (current_low != ((CORE_ADDR) -1))
9192 {
9193 best_low = min (best_low, current_low);
9194 best_high = max (best_high, current_high);
9195 }
9196 break;
9197 default:
9198 /* Ignore. */
9199 break;
9200 }
9201
9202 child = sibling_die (child);
9203 }
9204 }
9205
9206 *lowpc = best_low;
9207 *highpc = best_high;
9208 }
9209
9210 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9211 in DIE. */
9212
9213 static void
9214 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9215 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9216 {
9217 struct objfile *objfile = cu->objfile;
9218 struct attribute *attr;
9219 struct attribute *attr_high;
9220
9221 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9222 if (attr_high)
9223 {
9224 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9225 if (attr)
9226 {
9227 CORE_ADDR low = DW_ADDR (attr);
9228 CORE_ADDR high;
9229 if (attr_high->form == DW_FORM_addr
9230 || attr_high->form == DW_FORM_GNU_addr_index)
9231 high = DW_ADDR (attr_high);
9232 else
9233 high = low + DW_UNSND (attr_high);
9234
9235 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9236 }
9237 }
9238
9239 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9240 if (attr)
9241 {
9242 bfd *obfd = objfile->obfd;
9243
9244 /* The value of the DW_AT_ranges attribute is the offset of the
9245 address range list in the .debug_ranges section. */
9246 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9247 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9248
9249 /* For some target architectures, but not others, the
9250 read_address function sign-extends the addresses it returns.
9251 To recognize base address selection entries, we need a
9252 mask. */
9253 unsigned int addr_size = cu->header.addr_size;
9254 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9255
9256 /* The base address, to which the next pair is relative. Note
9257 that this 'base' is a DWARF concept: most entries in a range
9258 list are relative, to reduce the number of relocs against the
9259 debugging information. This is separate from this function's
9260 'baseaddr' argument, which GDB uses to relocate debugging
9261 information from a shared library based on the address at
9262 which the library was loaded. */
9263 CORE_ADDR base = cu->base_address;
9264 int base_known = cu->base_known;
9265
9266 gdb_assert (dwarf2_per_objfile->ranges.readin);
9267 if (offset >= dwarf2_per_objfile->ranges.size)
9268 {
9269 complaint (&symfile_complaints,
9270 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9271 offset);
9272 return;
9273 }
9274
9275 for (;;)
9276 {
9277 unsigned int bytes_read;
9278 CORE_ADDR start, end;
9279
9280 start = read_address (obfd, buffer, cu, &bytes_read);
9281 buffer += bytes_read;
9282 end = read_address (obfd, buffer, cu, &bytes_read);
9283 buffer += bytes_read;
9284
9285 /* Did we find the end of the range list? */
9286 if (start == 0 && end == 0)
9287 break;
9288
9289 /* Did we find a base address selection entry? */
9290 else if ((start & base_select_mask) == base_select_mask)
9291 {
9292 base = end;
9293 base_known = 1;
9294 }
9295
9296 /* We found an ordinary address range. */
9297 else
9298 {
9299 if (!base_known)
9300 {
9301 complaint (&symfile_complaints,
9302 _("Invalid .debug_ranges data "
9303 "(no base address)"));
9304 return;
9305 }
9306
9307 if (start > end)
9308 {
9309 /* Inverted range entries are invalid. */
9310 complaint (&symfile_complaints,
9311 _("Invalid .debug_ranges data "
9312 "(inverted range)"));
9313 return;
9314 }
9315
9316 /* Empty range entries have no effect. */
9317 if (start == end)
9318 continue;
9319
9320 record_block_range (block,
9321 baseaddr + base + start,
9322 baseaddr + base + end - 1);
9323 }
9324 }
9325 }
9326 }
9327
9328 /* Check whether the producer field indicates either of GCC < 4.6, or the
9329 Intel C/C++ compiler, and cache the result in CU. */
9330
9331 static void
9332 check_producer (struct dwarf2_cu *cu)
9333 {
9334 const char *cs;
9335 int major, minor, release;
9336
9337 if (cu->producer == NULL)
9338 {
9339 /* For unknown compilers expect their behavior is DWARF version
9340 compliant.
9341
9342 GCC started to support .debug_types sections by -gdwarf-4 since
9343 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9344 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9345 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9346 interpreted incorrectly by GDB now - GCC PR debug/48229. */
9347 }
9348 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9349 {
9350 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9351
9352 cs = &cu->producer[strlen ("GNU ")];
9353 while (*cs && !isdigit (*cs))
9354 cs++;
9355 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9356 {
9357 /* Not recognized as GCC. */
9358 }
9359 else
9360 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9361 }
9362 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9363 cu->producer_is_icc = 1;
9364 else
9365 {
9366 /* For other non-GCC compilers, expect their behavior is DWARF version
9367 compliant. */
9368 }
9369
9370 cu->checked_producer = 1;
9371 }
9372
9373 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9374 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9375 during 4.6.0 experimental. */
9376
9377 static int
9378 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9379 {
9380 if (!cu->checked_producer)
9381 check_producer (cu);
9382
9383 return cu->producer_is_gxx_lt_4_6;
9384 }
9385
9386 /* Return the default accessibility type if it is not overriden by
9387 DW_AT_accessibility. */
9388
9389 static enum dwarf_access_attribute
9390 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9391 {
9392 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9393 {
9394 /* The default DWARF 2 accessibility for members is public, the default
9395 accessibility for inheritance is private. */
9396
9397 if (die->tag != DW_TAG_inheritance)
9398 return DW_ACCESS_public;
9399 else
9400 return DW_ACCESS_private;
9401 }
9402 else
9403 {
9404 /* DWARF 3+ defines the default accessibility a different way. The same
9405 rules apply now for DW_TAG_inheritance as for the members and it only
9406 depends on the container kind. */
9407
9408 if (die->parent->tag == DW_TAG_class_type)
9409 return DW_ACCESS_private;
9410 else
9411 return DW_ACCESS_public;
9412 }
9413 }
9414
9415 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9416 offset. If the attribute was not found return 0, otherwise return
9417 1. If it was found but could not properly be handled, set *OFFSET
9418 to 0. */
9419
9420 static int
9421 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9422 LONGEST *offset)
9423 {
9424 struct attribute *attr;
9425
9426 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9427 if (attr != NULL)
9428 {
9429 *offset = 0;
9430
9431 /* Note that we do not check for a section offset first here.
9432 This is because DW_AT_data_member_location is new in DWARF 4,
9433 so if we see it, we can assume that a constant form is really
9434 a constant and not a section offset. */
9435 if (attr_form_is_constant (attr))
9436 *offset = dwarf2_get_attr_constant_value (attr, 0);
9437 else if (attr_form_is_section_offset (attr))
9438 dwarf2_complex_location_expr_complaint ();
9439 else if (attr_form_is_block (attr))
9440 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9441 else
9442 dwarf2_complex_location_expr_complaint ();
9443
9444 return 1;
9445 }
9446
9447 return 0;
9448 }
9449
9450 /* Add an aggregate field to the field list. */
9451
9452 static void
9453 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9454 struct dwarf2_cu *cu)
9455 {
9456 struct objfile *objfile = cu->objfile;
9457 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9458 struct nextfield *new_field;
9459 struct attribute *attr;
9460 struct field *fp;
9461 char *fieldname = "";
9462
9463 /* Allocate a new field list entry and link it in. */
9464 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9465 make_cleanup (xfree, new_field);
9466 memset (new_field, 0, sizeof (struct nextfield));
9467
9468 if (die->tag == DW_TAG_inheritance)
9469 {
9470 new_field->next = fip->baseclasses;
9471 fip->baseclasses = new_field;
9472 }
9473 else
9474 {
9475 new_field->next = fip->fields;
9476 fip->fields = new_field;
9477 }
9478 fip->nfields++;
9479
9480 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9481 if (attr)
9482 new_field->accessibility = DW_UNSND (attr);
9483 else
9484 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9485 if (new_field->accessibility != DW_ACCESS_public)
9486 fip->non_public_fields = 1;
9487
9488 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9489 if (attr)
9490 new_field->virtuality = DW_UNSND (attr);
9491 else
9492 new_field->virtuality = DW_VIRTUALITY_none;
9493
9494 fp = &new_field->field;
9495
9496 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9497 {
9498 LONGEST offset;
9499
9500 /* Data member other than a C++ static data member. */
9501
9502 /* Get type of field. */
9503 fp->type = die_type (die, cu);
9504
9505 SET_FIELD_BITPOS (*fp, 0);
9506
9507 /* Get bit size of field (zero if none). */
9508 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9509 if (attr)
9510 {
9511 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9512 }
9513 else
9514 {
9515 FIELD_BITSIZE (*fp) = 0;
9516 }
9517
9518 /* Get bit offset of field. */
9519 if (handle_data_member_location (die, cu, &offset))
9520 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9521 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9522 if (attr)
9523 {
9524 if (gdbarch_bits_big_endian (gdbarch))
9525 {
9526 /* For big endian bits, the DW_AT_bit_offset gives the
9527 additional bit offset from the MSB of the containing
9528 anonymous object to the MSB of the field. We don't
9529 have to do anything special since we don't need to
9530 know the size of the anonymous object. */
9531 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9532 }
9533 else
9534 {
9535 /* For little endian bits, compute the bit offset to the
9536 MSB of the anonymous object, subtract off the number of
9537 bits from the MSB of the field to the MSB of the
9538 object, and then subtract off the number of bits of
9539 the field itself. The result is the bit offset of
9540 the LSB of the field. */
9541 int anonymous_size;
9542 int bit_offset = DW_UNSND (attr);
9543
9544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9545 if (attr)
9546 {
9547 /* The size of the anonymous object containing
9548 the bit field is explicit, so use the
9549 indicated size (in bytes). */
9550 anonymous_size = DW_UNSND (attr);
9551 }
9552 else
9553 {
9554 /* The size of the anonymous object containing
9555 the bit field must be inferred from the type
9556 attribute of the data member containing the
9557 bit field. */
9558 anonymous_size = TYPE_LENGTH (fp->type);
9559 }
9560 SET_FIELD_BITPOS (*fp,
9561 (FIELD_BITPOS (*fp)
9562 + anonymous_size * bits_per_byte
9563 - bit_offset - FIELD_BITSIZE (*fp)));
9564 }
9565 }
9566
9567 /* Get name of field. */
9568 fieldname = dwarf2_name (die, cu);
9569 if (fieldname == NULL)
9570 fieldname = "";
9571
9572 /* The name is already allocated along with this objfile, so we don't
9573 need to duplicate it for the type. */
9574 fp->name = fieldname;
9575
9576 /* Change accessibility for artificial fields (e.g. virtual table
9577 pointer or virtual base class pointer) to private. */
9578 if (dwarf2_attr (die, DW_AT_artificial, cu))
9579 {
9580 FIELD_ARTIFICIAL (*fp) = 1;
9581 new_field->accessibility = DW_ACCESS_private;
9582 fip->non_public_fields = 1;
9583 }
9584 }
9585 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9586 {
9587 /* C++ static member. */
9588
9589 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9590 is a declaration, but all versions of G++ as of this writing
9591 (so through at least 3.2.1) incorrectly generate
9592 DW_TAG_variable tags. */
9593
9594 const char *physname;
9595
9596 /* Get name of field. */
9597 fieldname = dwarf2_name (die, cu);
9598 if (fieldname == NULL)
9599 return;
9600
9601 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9602 if (attr
9603 /* Only create a symbol if this is an external value.
9604 new_symbol checks this and puts the value in the global symbol
9605 table, which we want. If it is not external, new_symbol
9606 will try to put the value in cu->list_in_scope which is wrong. */
9607 && dwarf2_flag_true_p (die, DW_AT_external, cu))
9608 {
9609 /* A static const member, not much different than an enum as far as
9610 we're concerned, except that we can support more types. */
9611 new_symbol (die, NULL, cu);
9612 }
9613
9614 /* Get physical name. */
9615 physname = dwarf2_physname (fieldname, die, cu);
9616
9617 /* The name is already allocated along with this objfile, so we don't
9618 need to duplicate it for the type. */
9619 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9620 FIELD_TYPE (*fp) = die_type (die, cu);
9621 FIELD_NAME (*fp) = fieldname;
9622 }
9623 else if (die->tag == DW_TAG_inheritance)
9624 {
9625 LONGEST offset;
9626
9627 /* C++ base class field. */
9628 if (handle_data_member_location (die, cu, &offset))
9629 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9630 FIELD_BITSIZE (*fp) = 0;
9631 FIELD_TYPE (*fp) = die_type (die, cu);
9632 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9633 fip->nbaseclasses++;
9634 }
9635 }
9636
9637 /* Add a typedef defined in the scope of the FIP's class. */
9638
9639 static void
9640 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9641 struct dwarf2_cu *cu)
9642 {
9643 struct objfile *objfile = cu->objfile;
9644 struct typedef_field_list *new_field;
9645 struct attribute *attr;
9646 struct typedef_field *fp;
9647 char *fieldname = "";
9648
9649 /* Allocate a new field list entry and link it in. */
9650 new_field = xzalloc (sizeof (*new_field));
9651 make_cleanup (xfree, new_field);
9652
9653 gdb_assert (die->tag == DW_TAG_typedef);
9654
9655 fp = &new_field->field;
9656
9657 /* Get name of field. */
9658 fp->name = dwarf2_name (die, cu);
9659 if (fp->name == NULL)
9660 return;
9661
9662 fp->type = read_type_die (die, cu);
9663
9664 new_field->next = fip->typedef_field_list;
9665 fip->typedef_field_list = new_field;
9666 fip->typedef_field_list_count++;
9667 }
9668
9669 /* Create the vector of fields, and attach it to the type. */
9670
9671 static void
9672 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9673 struct dwarf2_cu *cu)
9674 {
9675 int nfields = fip->nfields;
9676
9677 /* Record the field count, allocate space for the array of fields,
9678 and create blank accessibility bitfields if necessary. */
9679 TYPE_NFIELDS (type) = nfields;
9680 TYPE_FIELDS (type) = (struct field *)
9681 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9682 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9683
9684 if (fip->non_public_fields && cu->language != language_ada)
9685 {
9686 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9687
9688 TYPE_FIELD_PRIVATE_BITS (type) =
9689 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9690 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9691
9692 TYPE_FIELD_PROTECTED_BITS (type) =
9693 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9694 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
9695
9696 TYPE_FIELD_IGNORE_BITS (type) =
9697 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9698 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
9699 }
9700
9701 /* If the type has baseclasses, allocate and clear a bit vector for
9702 TYPE_FIELD_VIRTUAL_BITS. */
9703 if (fip->nbaseclasses && cu->language != language_ada)
9704 {
9705 int num_bytes = B_BYTES (fip->nbaseclasses);
9706 unsigned char *pointer;
9707
9708 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9709 pointer = TYPE_ALLOC (type, num_bytes);
9710 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
9711 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
9712 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
9713 }
9714
9715 /* Copy the saved-up fields into the field vector. Start from the head of
9716 the list, adding to the tail of the field array, so that they end up in
9717 the same order in the array in which they were added to the list. */
9718 while (nfields-- > 0)
9719 {
9720 struct nextfield *fieldp;
9721
9722 if (fip->fields)
9723 {
9724 fieldp = fip->fields;
9725 fip->fields = fieldp->next;
9726 }
9727 else
9728 {
9729 fieldp = fip->baseclasses;
9730 fip->baseclasses = fieldp->next;
9731 }
9732
9733 TYPE_FIELD (type, nfields) = fieldp->field;
9734 switch (fieldp->accessibility)
9735 {
9736 case DW_ACCESS_private:
9737 if (cu->language != language_ada)
9738 SET_TYPE_FIELD_PRIVATE (type, nfields);
9739 break;
9740
9741 case DW_ACCESS_protected:
9742 if (cu->language != language_ada)
9743 SET_TYPE_FIELD_PROTECTED (type, nfields);
9744 break;
9745
9746 case DW_ACCESS_public:
9747 break;
9748
9749 default:
9750 /* Unknown accessibility. Complain and treat it as public. */
9751 {
9752 complaint (&symfile_complaints, _("unsupported accessibility %d"),
9753 fieldp->accessibility);
9754 }
9755 break;
9756 }
9757 if (nfields < fip->nbaseclasses)
9758 {
9759 switch (fieldp->virtuality)
9760 {
9761 case DW_VIRTUALITY_virtual:
9762 case DW_VIRTUALITY_pure_virtual:
9763 if (cu->language == language_ada)
9764 error (_("unexpected virtuality in component of Ada type"));
9765 SET_TYPE_FIELD_VIRTUAL (type, nfields);
9766 break;
9767 }
9768 }
9769 }
9770 }
9771
9772 /* Add a member function to the proper fieldlist. */
9773
9774 static void
9775 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
9776 struct type *type, struct dwarf2_cu *cu)
9777 {
9778 struct objfile *objfile = cu->objfile;
9779 struct attribute *attr;
9780 struct fnfieldlist *flp;
9781 int i;
9782 struct fn_field *fnp;
9783 char *fieldname;
9784 struct nextfnfield *new_fnfield;
9785 struct type *this_type;
9786 enum dwarf_access_attribute accessibility;
9787
9788 if (cu->language == language_ada)
9789 error (_("unexpected member function in Ada type"));
9790
9791 /* Get name of member function. */
9792 fieldname = dwarf2_name (die, cu);
9793 if (fieldname == NULL)
9794 return;
9795
9796 /* Look up member function name in fieldlist. */
9797 for (i = 0; i < fip->nfnfields; i++)
9798 {
9799 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
9800 break;
9801 }
9802
9803 /* Create new list element if necessary. */
9804 if (i < fip->nfnfields)
9805 flp = &fip->fnfieldlists[i];
9806 else
9807 {
9808 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9809 {
9810 fip->fnfieldlists = (struct fnfieldlist *)
9811 xrealloc (fip->fnfieldlists,
9812 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
9813 * sizeof (struct fnfieldlist));
9814 if (fip->nfnfields == 0)
9815 make_cleanup (free_current_contents, &fip->fnfieldlists);
9816 }
9817 flp = &fip->fnfieldlists[fip->nfnfields];
9818 flp->name = fieldname;
9819 flp->length = 0;
9820 flp->head = NULL;
9821 i = fip->nfnfields++;
9822 }
9823
9824 /* Create a new member function field and chain it to the field list
9825 entry. */
9826 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
9827 make_cleanup (xfree, new_fnfield);
9828 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9829 new_fnfield->next = flp->head;
9830 flp->head = new_fnfield;
9831 flp->length++;
9832
9833 /* Fill in the member function field info. */
9834 fnp = &new_fnfield->fnfield;
9835
9836 /* Delay processing of the physname until later. */
9837 if (cu->language == language_cplus || cu->language == language_java)
9838 {
9839 add_to_method_list (type, i, flp->length - 1, fieldname,
9840 die, cu);
9841 }
9842 else
9843 {
9844 const char *physname = dwarf2_physname (fieldname, die, cu);
9845 fnp->physname = physname ? physname : "";
9846 }
9847
9848 fnp->type = alloc_type (objfile);
9849 this_type = read_type_die (die, cu);
9850 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
9851 {
9852 int nparams = TYPE_NFIELDS (this_type);
9853
9854 /* TYPE is the domain of this method, and THIS_TYPE is the type
9855 of the method itself (TYPE_CODE_METHOD). */
9856 smash_to_method_type (fnp->type, type,
9857 TYPE_TARGET_TYPE (this_type),
9858 TYPE_FIELDS (this_type),
9859 TYPE_NFIELDS (this_type),
9860 TYPE_VARARGS (this_type));
9861
9862 /* Handle static member functions.
9863 Dwarf2 has no clean way to discern C++ static and non-static
9864 member functions. G++ helps GDB by marking the first
9865 parameter for non-static member functions (which is the this
9866 pointer) as artificial. We obtain this information from
9867 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
9868 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
9869 fnp->voffset = VOFFSET_STATIC;
9870 }
9871 else
9872 complaint (&symfile_complaints, _("member function type missing for '%s'"),
9873 dwarf2_full_name (fieldname, die, cu));
9874
9875 /* Get fcontext from DW_AT_containing_type if present. */
9876 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9877 fnp->fcontext = die_containing_type (die, cu);
9878
9879 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9880 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
9881
9882 /* Get accessibility. */
9883 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9884 if (attr)
9885 accessibility = DW_UNSND (attr);
9886 else
9887 accessibility = dwarf2_default_access_attribute (die, cu);
9888 switch (accessibility)
9889 {
9890 case DW_ACCESS_private:
9891 fnp->is_private = 1;
9892 break;
9893 case DW_ACCESS_protected:
9894 fnp->is_protected = 1;
9895 break;
9896 }
9897
9898 /* Check for artificial methods. */
9899 attr = dwarf2_attr (die, DW_AT_artificial, cu);
9900 if (attr && DW_UNSND (attr) != 0)
9901 fnp->is_artificial = 1;
9902
9903 /* Get index in virtual function table if it is a virtual member
9904 function. For older versions of GCC, this is an offset in the
9905 appropriate virtual table, as specified by DW_AT_containing_type.
9906 For everyone else, it is an expression to be evaluated relative
9907 to the object address. */
9908
9909 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
9910 if (attr)
9911 {
9912 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
9913 {
9914 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9915 {
9916 /* Old-style GCC. */
9917 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9918 }
9919 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9920 || (DW_BLOCK (attr)->size > 1
9921 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9922 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9923 {
9924 struct dwarf_block blk;
9925 int offset;
9926
9927 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9928 ? 1 : 2);
9929 blk.size = DW_BLOCK (attr)->size - offset;
9930 blk.data = DW_BLOCK (attr)->data + offset;
9931 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9932 if ((fnp->voffset % cu->header.addr_size) != 0)
9933 dwarf2_complex_location_expr_complaint ();
9934 else
9935 fnp->voffset /= cu->header.addr_size;
9936 fnp->voffset += 2;
9937 }
9938 else
9939 dwarf2_complex_location_expr_complaint ();
9940
9941 if (!fnp->fcontext)
9942 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9943 }
9944 else if (attr_form_is_section_offset (attr))
9945 {
9946 dwarf2_complex_location_expr_complaint ();
9947 }
9948 else
9949 {
9950 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9951 fieldname);
9952 }
9953 }
9954 else
9955 {
9956 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9957 if (attr && DW_UNSND (attr))
9958 {
9959 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9960 complaint (&symfile_complaints,
9961 _("Member function \"%s\" (offset %d) is virtual "
9962 "but the vtable offset is not specified"),
9963 fieldname, die->offset.sect_off);
9964 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9965 TYPE_CPLUS_DYNAMIC (type) = 1;
9966 }
9967 }
9968 }
9969
9970 /* Create the vector of member function fields, and attach it to the type. */
9971
9972 static void
9973 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
9974 struct dwarf2_cu *cu)
9975 {
9976 struct fnfieldlist *flp;
9977 int i;
9978
9979 if (cu->language == language_ada)
9980 error (_("unexpected member functions in Ada type"));
9981
9982 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9983 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9984 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9985
9986 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9987 {
9988 struct nextfnfield *nfp = flp->head;
9989 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9990 int k;
9991
9992 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9993 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9994 fn_flp->fn_fields = (struct fn_field *)
9995 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9996 for (k = flp->length; (k--, nfp); nfp = nfp->next)
9997 fn_flp->fn_fields[k] = nfp->fnfield;
9998 }
9999
10000 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10001 }
10002
10003 /* Returns non-zero if NAME is the name of a vtable member in CU's
10004 language, zero otherwise. */
10005 static int
10006 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10007 {
10008 static const char vptr[] = "_vptr";
10009 static const char vtable[] = "vtable";
10010
10011 /* Look for the C++ and Java forms of the vtable. */
10012 if ((cu->language == language_java
10013 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10014 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10015 && is_cplus_marker (name[sizeof (vptr) - 1])))
10016 return 1;
10017
10018 return 0;
10019 }
10020
10021 /* GCC outputs unnamed structures that are really pointers to member
10022 functions, with the ABI-specified layout. If TYPE describes
10023 such a structure, smash it into a member function type.
10024
10025 GCC shouldn't do this; it should just output pointer to member DIEs.
10026 This is GCC PR debug/28767. */
10027
10028 static void
10029 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10030 {
10031 struct type *pfn_type, *domain_type, *new_type;
10032
10033 /* Check for a structure with no name and two children. */
10034 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10035 return;
10036
10037 /* Check for __pfn and __delta members. */
10038 if (TYPE_FIELD_NAME (type, 0) == NULL
10039 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10040 || TYPE_FIELD_NAME (type, 1) == NULL
10041 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10042 return;
10043
10044 /* Find the type of the method. */
10045 pfn_type = TYPE_FIELD_TYPE (type, 0);
10046 if (pfn_type == NULL
10047 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10048 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10049 return;
10050
10051 /* Look for the "this" argument. */
10052 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10053 if (TYPE_NFIELDS (pfn_type) == 0
10054 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10055 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10056 return;
10057
10058 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10059 new_type = alloc_type (objfile);
10060 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10061 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10062 TYPE_VARARGS (pfn_type));
10063 smash_to_methodptr_type (type, new_type);
10064 }
10065
10066 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10067 (icc). */
10068
10069 static int
10070 producer_is_icc (struct dwarf2_cu *cu)
10071 {
10072 if (!cu->checked_producer)
10073 check_producer (cu);
10074
10075 return cu->producer_is_icc;
10076 }
10077
10078 /* Called when we find the DIE that starts a structure or union scope
10079 (definition) to create a type for the structure or union. Fill in
10080 the type's name and general properties; the members will not be
10081 processed until process_structure_type.
10082
10083 NOTE: we need to call these functions regardless of whether or not the
10084 DIE has a DW_AT_name attribute, since it might be an anonymous
10085 structure or union. This gets the type entered into our set of
10086 user defined types.
10087
10088 However, if the structure is incomplete (an opaque struct/union)
10089 then suppress creating a symbol table entry for it since gdb only
10090 wants to find the one with the complete definition. Note that if
10091 it is complete, we just call new_symbol, which does it's own
10092 checking about whether the struct/union is anonymous or not (and
10093 suppresses creating a symbol table entry itself). */
10094
10095 static struct type *
10096 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10097 {
10098 struct objfile *objfile = cu->objfile;
10099 struct type *type;
10100 struct attribute *attr;
10101 char *name;
10102
10103 /* If the definition of this type lives in .debug_types, read that type.
10104 Don't follow DW_AT_specification though, that will take us back up
10105 the chain and we want to go down. */
10106 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10107 if (attr)
10108 {
10109 struct dwarf2_cu *type_cu = cu;
10110 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10111
10112 /* We could just recurse on read_structure_type, but we need to call
10113 get_die_type to ensure only one type for this DIE is created.
10114 This is important, for example, because for c++ classes we need
10115 TYPE_NAME set which is only done by new_symbol. Blech. */
10116 type = read_type_die (type_die, type_cu);
10117
10118 /* TYPE_CU may not be the same as CU.
10119 Ensure TYPE is recorded in CU's type_hash table. */
10120 return set_die_type (die, type, cu);
10121 }
10122
10123 type = alloc_type (objfile);
10124 INIT_CPLUS_SPECIFIC (type);
10125
10126 name = dwarf2_name (die, cu);
10127 if (name != NULL)
10128 {
10129 if (cu->language == language_cplus
10130 || cu->language == language_java)
10131 {
10132 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10133
10134 /* dwarf2_full_name might have already finished building the DIE's
10135 type. If so, there is no need to continue. */
10136 if (get_die_type (die, cu) != NULL)
10137 return get_die_type (die, cu);
10138
10139 TYPE_TAG_NAME (type) = full_name;
10140 if (die->tag == DW_TAG_structure_type
10141 || die->tag == DW_TAG_class_type)
10142 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10143 }
10144 else
10145 {
10146 /* The name is already allocated along with this objfile, so
10147 we don't need to duplicate it for the type. */
10148 TYPE_TAG_NAME (type) = (char *) name;
10149 if (die->tag == DW_TAG_class_type)
10150 TYPE_NAME (type) = TYPE_TAG_NAME (type);
10151 }
10152 }
10153
10154 if (die->tag == DW_TAG_structure_type)
10155 {
10156 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10157 }
10158 else if (die->tag == DW_TAG_union_type)
10159 {
10160 TYPE_CODE (type) = TYPE_CODE_UNION;
10161 }
10162 else
10163 {
10164 TYPE_CODE (type) = TYPE_CODE_CLASS;
10165 }
10166
10167 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10168 TYPE_DECLARED_CLASS (type) = 1;
10169
10170 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10171 if (attr)
10172 {
10173 TYPE_LENGTH (type) = DW_UNSND (attr);
10174 }
10175 else
10176 {
10177 TYPE_LENGTH (type) = 0;
10178 }
10179
10180 if (producer_is_icc (cu))
10181 {
10182 /* ICC does not output the required DW_AT_declaration
10183 on incomplete types, but gives them a size of zero. */
10184 }
10185 else
10186 TYPE_STUB_SUPPORTED (type) = 1;
10187
10188 if (die_is_declaration (die, cu))
10189 TYPE_STUB (type) = 1;
10190 else if (attr == NULL && die->child == NULL
10191 && producer_is_realview (cu->producer))
10192 /* RealView does not output the required DW_AT_declaration
10193 on incomplete types. */
10194 TYPE_STUB (type) = 1;
10195
10196 /* We need to add the type field to the die immediately so we don't
10197 infinitely recurse when dealing with pointers to the structure
10198 type within the structure itself. */
10199 set_die_type (die, type, cu);
10200
10201 /* set_die_type should be already done. */
10202 set_descriptive_type (type, die, cu);
10203
10204 return type;
10205 }
10206
10207 /* Finish creating a structure or union type, including filling in
10208 its members and creating a symbol for it. */
10209
10210 static void
10211 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10212 {
10213 struct objfile *objfile = cu->objfile;
10214 struct die_info *child_die = die->child;
10215 struct type *type;
10216
10217 type = get_die_type (die, cu);
10218 if (type == NULL)
10219 type = read_structure_type (die, cu);
10220
10221 if (die->child != NULL && ! die_is_declaration (die, cu))
10222 {
10223 struct field_info fi;
10224 struct die_info *child_die;
10225 VEC (symbolp) *template_args = NULL;
10226 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10227
10228 memset (&fi, 0, sizeof (struct field_info));
10229
10230 child_die = die->child;
10231
10232 while (child_die && child_die->tag)
10233 {
10234 if (child_die->tag == DW_TAG_member
10235 || child_die->tag == DW_TAG_variable)
10236 {
10237 /* NOTE: carlton/2002-11-05: A C++ static data member
10238 should be a DW_TAG_member that is a declaration, but
10239 all versions of G++ as of this writing (so through at
10240 least 3.2.1) incorrectly generate DW_TAG_variable
10241 tags for them instead. */
10242 dwarf2_add_field (&fi, child_die, cu);
10243 }
10244 else if (child_die->tag == DW_TAG_subprogram)
10245 {
10246 /* C++ member function. */
10247 dwarf2_add_member_fn (&fi, child_die, type, cu);
10248 }
10249 else if (child_die->tag == DW_TAG_inheritance)
10250 {
10251 /* C++ base class field. */
10252 dwarf2_add_field (&fi, child_die, cu);
10253 }
10254 else if (child_die->tag == DW_TAG_typedef)
10255 dwarf2_add_typedef (&fi, child_die, cu);
10256 else if (child_die->tag == DW_TAG_template_type_param
10257 || child_die->tag == DW_TAG_template_value_param)
10258 {
10259 struct symbol *arg = new_symbol (child_die, NULL, cu);
10260
10261 if (arg != NULL)
10262 VEC_safe_push (symbolp, template_args, arg);
10263 }
10264
10265 child_die = sibling_die (child_die);
10266 }
10267
10268 /* Attach template arguments to type. */
10269 if (! VEC_empty (symbolp, template_args))
10270 {
10271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10272 TYPE_N_TEMPLATE_ARGUMENTS (type)
10273 = VEC_length (symbolp, template_args);
10274 TYPE_TEMPLATE_ARGUMENTS (type)
10275 = obstack_alloc (&objfile->objfile_obstack,
10276 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10277 * sizeof (struct symbol *)));
10278 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10279 VEC_address (symbolp, template_args),
10280 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10281 * sizeof (struct symbol *)));
10282 VEC_free (symbolp, template_args);
10283 }
10284
10285 /* Attach fields and member functions to the type. */
10286 if (fi.nfields)
10287 dwarf2_attach_fields_to_type (&fi, type, cu);
10288 if (fi.nfnfields)
10289 {
10290 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10291
10292 /* Get the type which refers to the base class (possibly this
10293 class itself) which contains the vtable pointer for the current
10294 class from the DW_AT_containing_type attribute. This use of
10295 DW_AT_containing_type is a GNU extension. */
10296
10297 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10298 {
10299 struct type *t = die_containing_type (die, cu);
10300
10301 TYPE_VPTR_BASETYPE (type) = t;
10302 if (type == t)
10303 {
10304 int i;
10305
10306 /* Our own class provides vtbl ptr. */
10307 for (i = TYPE_NFIELDS (t) - 1;
10308 i >= TYPE_N_BASECLASSES (t);
10309 --i)
10310 {
10311 const char *fieldname = TYPE_FIELD_NAME (t, i);
10312
10313 if (is_vtable_name (fieldname, cu))
10314 {
10315 TYPE_VPTR_FIELDNO (type) = i;
10316 break;
10317 }
10318 }
10319
10320 /* Complain if virtual function table field not found. */
10321 if (i < TYPE_N_BASECLASSES (t))
10322 complaint (&symfile_complaints,
10323 _("virtual function table pointer "
10324 "not found when defining class '%s'"),
10325 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10326 "");
10327 }
10328 else
10329 {
10330 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10331 }
10332 }
10333 else if (cu->producer
10334 && strncmp (cu->producer,
10335 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10336 {
10337 /* The IBM XLC compiler does not provide direct indication
10338 of the containing type, but the vtable pointer is
10339 always named __vfp. */
10340
10341 int i;
10342
10343 for (i = TYPE_NFIELDS (type) - 1;
10344 i >= TYPE_N_BASECLASSES (type);
10345 --i)
10346 {
10347 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10348 {
10349 TYPE_VPTR_FIELDNO (type) = i;
10350 TYPE_VPTR_BASETYPE (type) = type;
10351 break;
10352 }
10353 }
10354 }
10355 }
10356
10357 /* Copy fi.typedef_field_list linked list elements content into the
10358 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10359 if (fi.typedef_field_list)
10360 {
10361 int i = fi.typedef_field_list_count;
10362
10363 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10364 TYPE_TYPEDEF_FIELD_ARRAY (type)
10365 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10366 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10367
10368 /* Reverse the list order to keep the debug info elements order. */
10369 while (--i >= 0)
10370 {
10371 struct typedef_field *dest, *src;
10372
10373 dest = &TYPE_TYPEDEF_FIELD (type, i);
10374 src = &fi.typedef_field_list->field;
10375 fi.typedef_field_list = fi.typedef_field_list->next;
10376 *dest = *src;
10377 }
10378 }
10379
10380 do_cleanups (back_to);
10381
10382 if (HAVE_CPLUS_STRUCT (type))
10383 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10384 }
10385
10386 quirk_gcc_member_function_pointer (type, objfile);
10387
10388 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10389 snapshots) has been known to create a die giving a declaration
10390 for a class that has, as a child, a die giving a definition for a
10391 nested class. So we have to process our children even if the
10392 current die is a declaration. Normally, of course, a declaration
10393 won't have any children at all. */
10394
10395 while (child_die != NULL && child_die->tag)
10396 {
10397 if (child_die->tag == DW_TAG_member
10398 || child_die->tag == DW_TAG_variable
10399 || child_die->tag == DW_TAG_inheritance
10400 || child_die->tag == DW_TAG_template_value_param
10401 || child_die->tag == DW_TAG_template_type_param)
10402 {
10403 /* Do nothing. */
10404 }
10405 else
10406 process_die (child_die, cu);
10407
10408 child_die = sibling_die (child_die);
10409 }
10410
10411 /* Do not consider external references. According to the DWARF standard,
10412 these DIEs are identified by the fact that they have no byte_size
10413 attribute, and a declaration attribute. */
10414 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10415 || !die_is_declaration (die, cu))
10416 new_symbol (die, type, cu);
10417 }
10418
10419 /* Given a DW_AT_enumeration_type die, set its type. We do not
10420 complete the type's fields yet, or create any symbols. */
10421
10422 static struct type *
10423 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10424 {
10425 struct objfile *objfile = cu->objfile;
10426 struct type *type;
10427 struct attribute *attr;
10428 const char *name;
10429
10430 /* If the definition of this type lives in .debug_types, read that type.
10431 Don't follow DW_AT_specification though, that will take us back up
10432 the chain and we want to go down. */
10433 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10434 if (attr)
10435 {
10436 struct dwarf2_cu *type_cu = cu;
10437 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10438
10439 type = read_type_die (type_die, type_cu);
10440
10441 /* TYPE_CU may not be the same as CU.
10442 Ensure TYPE is recorded in CU's type_hash table. */
10443 return set_die_type (die, type, cu);
10444 }
10445
10446 type = alloc_type (objfile);
10447
10448 TYPE_CODE (type) = TYPE_CODE_ENUM;
10449 name = dwarf2_full_name (NULL, die, cu);
10450 if (name != NULL)
10451 TYPE_TAG_NAME (type) = (char *) name;
10452
10453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10454 if (attr)
10455 {
10456 TYPE_LENGTH (type) = DW_UNSND (attr);
10457 }
10458 else
10459 {
10460 TYPE_LENGTH (type) = 0;
10461 }
10462
10463 /* The enumeration DIE can be incomplete. In Ada, any type can be
10464 declared as private in the package spec, and then defined only
10465 inside the package body. Such types are known as Taft Amendment
10466 Types. When another package uses such a type, an incomplete DIE
10467 may be generated by the compiler. */
10468 if (die_is_declaration (die, cu))
10469 TYPE_STUB (type) = 1;
10470
10471 return set_die_type (die, type, cu);
10472 }
10473
10474 /* Given a pointer to a die which begins an enumeration, process all
10475 the dies that define the members of the enumeration, and create the
10476 symbol for the enumeration type.
10477
10478 NOTE: We reverse the order of the element list. */
10479
10480 static void
10481 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10482 {
10483 struct type *this_type;
10484
10485 this_type = get_die_type (die, cu);
10486 if (this_type == NULL)
10487 this_type = read_enumeration_type (die, cu);
10488
10489 if (die->child != NULL)
10490 {
10491 struct die_info *child_die;
10492 struct symbol *sym;
10493 struct field *fields = NULL;
10494 int num_fields = 0;
10495 int unsigned_enum = 1;
10496 char *name;
10497 int flag_enum = 1;
10498 ULONGEST mask = 0;
10499
10500 child_die = die->child;
10501 while (child_die && child_die->tag)
10502 {
10503 if (child_die->tag != DW_TAG_enumerator)
10504 {
10505 process_die (child_die, cu);
10506 }
10507 else
10508 {
10509 name = dwarf2_name (child_die, cu);
10510 if (name)
10511 {
10512 sym = new_symbol (child_die, this_type, cu);
10513 if (SYMBOL_VALUE (sym) < 0)
10514 {
10515 unsigned_enum = 0;
10516 flag_enum = 0;
10517 }
10518 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10519 flag_enum = 0;
10520 else
10521 mask |= SYMBOL_VALUE (sym);
10522
10523 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10524 {
10525 fields = (struct field *)
10526 xrealloc (fields,
10527 (num_fields + DW_FIELD_ALLOC_CHUNK)
10528 * sizeof (struct field));
10529 }
10530
10531 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10532 FIELD_TYPE (fields[num_fields]) = NULL;
10533 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10534 FIELD_BITSIZE (fields[num_fields]) = 0;
10535
10536 num_fields++;
10537 }
10538 }
10539
10540 child_die = sibling_die (child_die);
10541 }
10542
10543 if (num_fields)
10544 {
10545 TYPE_NFIELDS (this_type) = num_fields;
10546 TYPE_FIELDS (this_type) = (struct field *)
10547 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10548 memcpy (TYPE_FIELDS (this_type), fields,
10549 sizeof (struct field) * num_fields);
10550 xfree (fields);
10551 }
10552 if (unsigned_enum)
10553 TYPE_UNSIGNED (this_type) = 1;
10554 if (flag_enum)
10555 TYPE_FLAG_ENUM (this_type) = 1;
10556 }
10557
10558 /* If we are reading an enum from a .debug_types unit, and the enum
10559 is a declaration, and the enum is not the signatured type in the
10560 unit, then we do not want to add a symbol for it. Adding a
10561 symbol would in some cases obscure the true definition of the
10562 enum, giving users an incomplete type when the definition is
10563 actually available. Note that we do not want to do this for all
10564 enums which are just declarations, because C++0x allows forward
10565 enum declarations. */
10566 if (cu->per_cu->is_debug_types
10567 && die_is_declaration (die, cu))
10568 {
10569 struct signatured_type *sig_type;
10570
10571 sig_type
10572 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10573 cu->per_cu->info_or_types_section,
10574 cu->per_cu->offset);
10575 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10576 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10577 return;
10578 }
10579
10580 new_symbol (die, this_type, cu);
10581 }
10582
10583 /* Extract all information from a DW_TAG_array_type DIE and put it in
10584 the DIE's type field. For now, this only handles one dimensional
10585 arrays. */
10586
10587 static struct type *
10588 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10589 {
10590 struct objfile *objfile = cu->objfile;
10591 struct die_info *child_die;
10592 struct type *type;
10593 struct type *element_type, *range_type, *index_type;
10594 struct type **range_types = NULL;
10595 struct attribute *attr;
10596 int ndim = 0;
10597 struct cleanup *back_to;
10598 char *name;
10599
10600 element_type = die_type (die, cu);
10601
10602 /* The die_type call above may have already set the type for this DIE. */
10603 type = get_die_type (die, cu);
10604 if (type)
10605 return type;
10606
10607 /* Irix 6.2 native cc creates array types without children for
10608 arrays with unspecified length. */
10609 if (die->child == NULL)
10610 {
10611 index_type = objfile_type (objfile)->builtin_int;
10612 range_type = create_range_type (NULL, index_type, 0, -1);
10613 type = create_array_type (NULL, element_type, range_type);
10614 return set_die_type (die, type, cu);
10615 }
10616
10617 back_to = make_cleanup (null_cleanup, NULL);
10618 child_die = die->child;
10619 while (child_die && child_die->tag)
10620 {
10621 if (child_die->tag == DW_TAG_subrange_type)
10622 {
10623 struct type *child_type = read_type_die (child_die, cu);
10624
10625 if (child_type != NULL)
10626 {
10627 /* The range type was succesfully read. Save it for the
10628 array type creation. */
10629 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10630 {
10631 range_types = (struct type **)
10632 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10633 * sizeof (struct type *));
10634 if (ndim == 0)
10635 make_cleanup (free_current_contents, &range_types);
10636 }
10637 range_types[ndim++] = child_type;
10638 }
10639 }
10640 child_die = sibling_die (child_die);
10641 }
10642
10643 /* Dwarf2 dimensions are output from left to right, create the
10644 necessary array types in backwards order. */
10645
10646 type = element_type;
10647
10648 if (read_array_order (die, cu) == DW_ORD_col_major)
10649 {
10650 int i = 0;
10651
10652 while (i < ndim)
10653 type = create_array_type (NULL, type, range_types[i++]);
10654 }
10655 else
10656 {
10657 while (ndim-- > 0)
10658 type = create_array_type (NULL, type, range_types[ndim]);
10659 }
10660
10661 /* Understand Dwarf2 support for vector types (like they occur on
10662 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10663 array type. This is not part of the Dwarf2/3 standard yet, but a
10664 custom vendor extension. The main difference between a regular
10665 array and the vector variant is that vectors are passed by value
10666 to functions. */
10667 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10668 if (attr)
10669 make_vector_type (type);
10670
10671 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10672 implementation may choose to implement triple vectors using this
10673 attribute. */
10674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10675 if (attr)
10676 {
10677 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10678 TYPE_LENGTH (type) = DW_UNSND (attr);
10679 else
10680 complaint (&symfile_complaints,
10681 _("DW_AT_byte_size for array type smaller "
10682 "than the total size of elements"));
10683 }
10684
10685 name = dwarf2_name (die, cu);
10686 if (name)
10687 TYPE_NAME (type) = name;
10688
10689 /* Install the type in the die. */
10690 set_die_type (die, type, cu);
10691
10692 /* set_die_type should be already done. */
10693 set_descriptive_type (type, die, cu);
10694
10695 do_cleanups (back_to);
10696
10697 return type;
10698 }
10699
10700 static enum dwarf_array_dim_ordering
10701 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
10702 {
10703 struct attribute *attr;
10704
10705 attr = dwarf2_attr (die, DW_AT_ordering, cu);
10706
10707 if (attr) return DW_SND (attr);
10708
10709 /* GNU F77 is a special case, as at 08/2004 array type info is the
10710 opposite order to the dwarf2 specification, but data is still
10711 laid out as per normal fortran.
10712
10713 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
10714 version checking. */
10715
10716 if (cu->language == language_fortran
10717 && cu->producer && strstr (cu->producer, "GNU F77"))
10718 {
10719 return DW_ORD_row_major;
10720 }
10721
10722 switch (cu->language_defn->la_array_ordering)
10723 {
10724 case array_column_major:
10725 return DW_ORD_col_major;
10726 case array_row_major:
10727 default:
10728 return DW_ORD_row_major;
10729 };
10730 }
10731
10732 /* Extract all information from a DW_TAG_set_type DIE and put it in
10733 the DIE's type field. */
10734
10735 static struct type *
10736 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
10737 {
10738 struct type *domain_type, *set_type;
10739 struct attribute *attr;
10740
10741 domain_type = die_type (die, cu);
10742
10743 /* The die_type call above may have already set the type for this DIE. */
10744 set_type = get_die_type (die, cu);
10745 if (set_type)
10746 return set_type;
10747
10748 set_type = create_set_type (NULL, domain_type);
10749
10750 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10751 if (attr)
10752 TYPE_LENGTH (set_type) = DW_UNSND (attr);
10753
10754 return set_die_type (die, set_type, cu);
10755 }
10756
10757 /* First cut: install each common block member as a global variable. */
10758
10759 static void
10760 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
10761 {
10762 struct die_info *child_die;
10763 struct attribute *attr;
10764 struct symbol *sym;
10765 CORE_ADDR base = (CORE_ADDR) 0;
10766
10767 attr = dwarf2_attr (die, DW_AT_location, cu);
10768 if (attr)
10769 {
10770 /* Support the .debug_loc offsets. */
10771 if (attr_form_is_block (attr))
10772 {
10773 base = decode_locdesc (DW_BLOCK (attr), cu);
10774 }
10775 else if (attr_form_is_section_offset (attr))
10776 {
10777 dwarf2_complex_location_expr_complaint ();
10778 }
10779 else
10780 {
10781 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10782 "common block member");
10783 }
10784 }
10785 if (die->child != NULL)
10786 {
10787 child_die = die->child;
10788 while (child_die && child_die->tag)
10789 {
10790 LONGEST offset;
10791
10792 sym = new_symbol (child_die, NULL, cu);
10793 if (sym != NULL
10794 && handle_data_member_location (child_die, cu, &offset))
10795 {
10796 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
10797 add_symbol_to_list (sym, &global_symbols);
10798 }
10799 child_die = sibling_die (child_die);
10800 }
10801 }
10802 }
10803
10804 /* Create a type for a C++ namespace. */
10805
10806 static struct type *
10807 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
10808 {
10809 struct objfile *objfile = cu->objfile;
10810 const char *previous_prefix, *name;
10811 int is_anonymous;
10812 struct type *type;
10813
10814 /* For extensions, reuse the type of the original namespace. */
10815 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10816 {
10817 struct die_info *ext_die;
10818 struct dwarf2_cu *ext_cu = cu;
10819
10820 ext_die = dwarf2_extension (die, &ext_cu);
10821 type = read_type_die (ext_die, ext_cu);
10822
10823 /* EXT_CU may not be the same as CU.
10824 Ensure TYPE is recorded in CU's type_hash table. */
10825 return set_die_type (die, type, cu);
10826 }
10827
10828 name = namespace_name (die, &is_anonymous, cu);
10829
10830 /* Now build the name of the current namespace. */
10831
10832 previous_prefix = determine_prefix (die, cu);
10833 if (previous_prefix[0] != '\0')
10834 name = typename_concat (&objfile->objfile_obstack,
10835 previous_prefix, name, 0, cu);
10836
10837 /* Create the type. */
10838 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10839 objfile);
10840 TYPE_NAME (type) = (char *) name;
10841 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10842
10843 return set_die_type (die, type, cu);
10844 }
10845
10846 /* Read a C++ namespace. */
10847
10848 static void
10849 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10850 {
10851 struct objfile *objfile = cu->objfile;
10852 int is_anonymous;
10853
10854 /* Add a symbol associated to this if we haven't seen the namespace
10855 before. Also, add a using directive if it's an anonymous
10856 namespace. */
10857
10858 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
10859 {
10860 struct type *type;
10861
10862 type = read_type_die (die, cu);
10863 new_symbol (die, type, cu);
10864
10865 namespace_name (die, &is_anonymous, cu);
10866 if (is_anonymous)
10867 {
10868 const char *previous_prefix = determine_prefix (die, cu);
10869
10870 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
10871 NULL, NULL, &objfile->objfile_obstack);
10872 }
10873 }
10874
10875 if (die->child != NULL)
10876 {
10877 struct die_info *child_die = die->child;
10878
10879 while (child_die && child_die->tag)
10880 {
10881 process_die (child_die, cu);
10882 child_die = sibling_die (child_die);
10883 }
10884 }
10885 }
10886
10887 /* Read a Fortran module as type. This DIE can be only a declaration used for
10888 imported module. Still we need that type as local Fortran "use ... only"
10889 declaration imports depend on the created type in determine_prefix. */
10890
10891 static struct type *
10892 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10893 {
10894 struct objfile *objfile = cu->objfile;
10895 char *module_name;
10896 struct type *type;
10897
10898 module_name = dwarf2_name (die, cu);
10899 if (!module_name)
10900 complaint (&symfile_complaints,
10901 _("DW_TAG_module has no name, offset 0x%x"),
10902 die->offset.sect_off);
10903 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10904
10905 /* determine_prefix uses TYPE_TAG_NAME. */
10906 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10907
10908 return set_die_type (die, type, cu);
10909 }
10910
10911 /* Read a Fortran module. */
10912
10913 static void
10914 read_module (struct die_info *die, struct dwarf2_cu *cu)
10915 {
10916 struct die_info *child_die = die->child;
10917
10918 while (child_die && child_die->tag)
10919 {
10920 process_die (child_die, cu);
10921 child_die = sibling_die (child_die);
10922 }
10923 }
10924
10925 /* Return the name of the namespace represented by DIE. Set
10926 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10927 namespace. */
10928
10929 static const char *
10930 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
10931 {
10932 struct die_info *current_die;
10933 const char *name = NULL;
10934
10935 /* Loop through the extensions until we find a name. */
10936
10937 for (current_die = die;
10938 current_die != NULL;
10939 current_die = dwarf2_extension (die, &cu))
10940 {
10941 name = dwarf2_name (current_die, cu);
10942 if (name != NULL)
10943 break;
10944 }
10945
10946 /* Is it an anonymous namespace? */
10947
10948 *is_anonymous = (name == NULL);
10949 if (*is_anonymous)
10950 name = CP_ANONYMOUS_NAMESPACE_STR;
10951
10952 return name;
10953 }
10954
10955 /* Extract all information from a DW_TAG_pointer_type DIE and add to
10956 the user defined type vector. */
10957
10958 static struct type *
10959 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
10960 {
10961 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
10962 struct comp_unit_head *cu_header = &cu->header;
10963 struct type *type;
10964 struct attribute *attr_byte_size;
10965 struct attribute *attr_address_class;
10966 int byte_size, addr_class;
10967 struct type *target_type;
10968
10969 target_type = die_type (die, cu);
10970
10971 /* The die_type call above may have already set the type for this DIE. */
10972 type = get_die_type (die, cu);
10973 if (type)
10974 return type;
10975
10976 type = lookup_pointer_type (target_type);
10977
10978 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
10979 if (attr_byte_size)
10980 byte_size = DW_UNSND (attr_byte_size);
10981 else
10982 byte_size = cu_header->addr_size;
10983
10984 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
10985 if (attr_address_class)
10986 addr_class = DW_UNSND (attr_address_class);
10987 else
10988 addr_class = DW_ADDR_none;
10989
10990 /* If the pointer size or address class is different than the
10991 default, create a type variant marked as such and set the
10992 length accordingly. */
10993 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
10994 {
10995 if (gdbarch_address_class_type_flags_p (gdbarch))
10996 {
10997 int type_flags;
10998
10999 type_flags = gdbarch_address_class_type_flags
11000 (gdbarch, byte_size, addr_class);
11001 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11002 == 0);
11003 type = make_type_with_address_space (type, type_flags);
11004 }
11005 else if (TYPE_LENGTH (type) != byte_size)
11006 {
11007 complaint (&symfile_complaints,
11008 _("invalid pointer size %d"), byte_size);
11009 }
11010 else
11011 {
11012 /* Should we also complain about unhandled address classes? */
11013 }
11014 }
11015
11016 TYPE_LENGTH (type) = byte_size;
11017 return set_die_type (die, type, cu);
11018 }
11019
11020 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11021 the user defined type vector. */
11022
11023 static struct type *
11024 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11025 {
11026 struct type *type;
11027 struct type *to_type;
11028 struct type *domain;
11029
11030 to_type = die_type (die, cu);
11031 domain = die_containing_type (die, cu);
11032
11033 /* The calls above may have already set the type for this DIE. */
11034 type = get_die_type (die, cu);
11035 if (type)
11036 return type;
11037
11038 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11039 type = lookup_methodptr_type (to_type);
11040 else
11041 type = lookup_memberptr_type (to_type, domain);
11042
11043 return set_die_type (die, type, cu);
11044 }
11045
11046 /* Extract all information from a DW_TAG_reference_type DIE and add to
11047 the user defined type vector. */
11048
11049 static struct type *
11050 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11051 {
11052 struct comp_unit_head *cu_header = &cu->header;
11053 struct type *type, *target_type;
11054 struct attribute *attr;
11055
11056 target_type = die_type (die, cu);
11057
11058 /* The die_type call above may have already set the type for this DIE. */
11059 type = get_die_type (die, cu);
11060 if (type)
11061 return type;
11062
11063 type = lookup_reference_type (target_type);
11064 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11065 if (attr)
11066 {
11067 TYPE_LENGTH (type) = DW_UNSND (attr);
11068 }
11069 else
11070 {
11071 TYPE_LENGTH (type) = cu_header->addr_size;
11072 }
11073 return set_die_type (die, type, cu);
11074 }
11075
11076 static struct type *
11077 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11078 {
11079 struct type *base_type, *cv_type;
11080
11081 base_type = die_type (die, cu);
11082
11083 /* The die_type call above may have already set the type for this DIE. */
11084 cv_type = get_die_type (die, cu);
11085 if (cv_type)
11086 return cv_type;
11087
11088 /* In case the const qualifier is applied to an array type, the element type
11089 is so qualified, not the array type (section 6.7.3 of C99). */
11090 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11091 {
11092 struct type *el_type, *inner_array;
11093
11094 base_type = copy_type (base_type);
11095 inner_array = base_type;
11096
11097 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11098 {
11099 TYPE_TARGET_TYPE (inner_array) =
11100 copy_type (TYPE_TARGET_TYPE (inner_array));
11101 inner_array = TYPE_TARGET_TYPE (inner_array);
11102 }
11103
11104 el_type = TYPE_TARGET_TYPE (inner_array);
11105 TYPE_TARGET_TYPE (inner_array) =
11106 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11107
11108 return set_die_type (die, base_type, cu);
11109 }
11110
11111 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11112 return set_die_type (die, cv_type, cu);
11113 }
11114
11115 static struct type *
11116 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11117 {
11118 struct type *base_type, *cv_type;
11119
11120 base_type = die_type (die, cu);
11121
11122 /* The die_type call above may have already set the type for this DIE. */
11123 cv_type = get_die_type (die, cu);
11124 if (cv_type)
11125 return cv_type;
11126
11127 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11128 return set_die_type (die, cv_type, cu);
11129 }
11130
11131 /* Extract all information from a DW_TAG_string_type DIE and add to
11132 the user defined type vector. It isn't really a user defined type,
11133 but it behaves like one, with other DIE's using an AT_user_def_type
11134 attribute to reference it. */
11135
11136 static struct type *
11137 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11138 {
11139 struct objfile *objfile = cu->objfile;
11140 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11141 struct type *type, *range_type, *index_type, *char_type;
11142 struct attribute *attr;
11143 unsigned int length;
11144
11145 attr = dwarf2_attr (die, DW_AT_string_length, cu);
11146 if (attr)
11147 {
11148 length = DW_UNSND (attr);
11149 }
11150 else
11151 {
11152 /* Check for the DW_AT_byte_size attribute. */
11153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11154 if (attr)
11155 {
11156 length = DW_UNSND (attr);
11157 }
11158 else
11159 {
11160 length = 1;
11161 }
11162 }
11163
11164 index_type = objfile_type (objfile)->builtin_int;
11165 range_type = create_range_type (NULL, index_type, 1, length);
11166 char_type = language_string_char_type (cu->language_defn, gdbarch);
11167 type = create_string_type (NULL, char_type, range_type);
11168
11169 return set_die_type (die, type, cu);
11170 }
11171
11172 /* Handle DIES due to C code like:
11173
11174 struct foo
11175 {
11176 int (*funcp)(int a, long l);
11177 int b;
11178 };
11179
11180 ('funcp' generates a DW_TAG_subroutine_type DIE). */
11181
11182 static struct type *
11183 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11184 {
11185 struct objfile *objfile = cu->objfile;
11186 struct type *type; /* Type that this function returns. */
11187 struct type *ftype; /* Function that returns above type. */
11188 struct attribute *attr;
11189
11190 type = die_type (die, cu);
11191
11192 /* The die_type call above may have already set the type for this DIE. */
11193 ftype = get_die_type (die, cu);
11194 if (ftype)
11195 return ftype;
11196
11197 ftype = lookup_function_type (type);
11198
11199 /* All functions in C++, Pascal and Java have prototypes. */
11200 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11201 if ((attr && (DW_UNSND (attr) != 0))
11202 || cu->language == language_cplus
11203 || cu->language == language_java
11204 || cu->language == language_pascal)
11205 TYPE_PROTOTYPED (ftype) = 1;
11206 else if (producer_is_realview (cu->producer))
11207 /* RealView does not emit DW_AT_prototyped. We can not
11208 distinguish prototyped and unprototyped functions; default to
11209 prototyped, since that is more common in modern code (and
11210 RealView warns about unprototyped functions). */
11211 TYPE_PROTOTYPED (ftype) = 1;
11212
11213 /* Store the calling convention in the type if it's available in
11214 the subroutine die. Otherwise set the calling convention to
11215 the default value DW_CC_normal. */
11216 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11217 if (attr)
11218 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11219 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11220 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11221 else
11222 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11223
11224 /* We need to add the subroutine type to the die immediately so
11225 we don't infinitely recurse when dealing with parameters
11226 declared as the same subroutine type. */
11227 set_die_type (die, ftype, cu);
11228
11229 if (die->child != NULL)
11230 {
11231 struct type *void_type = objfile_type (objfile)->builtin_void;
11232 struct die_info *child_die;
11233 int nparams, iparams;
11234
11235 /* Count the number of parameters.
11236 FIXME: GDB currently ignores vararg functions, but knows about
11237 vararg member functions. */
11238 nparams = 0;
11239 child_die = die->child;
11240 while (child_die && child_die->tag)
11241 {
11242 if (child_die->tag == DW_TAG_formal_parameter)
11243 nparams++;
11244 else if (child_die->tag == DW_TAG_unspecified_parameters)
11245 TYPE_VARARGS (ftype) = 1;
11246 child_die = sibling_die (child_die);
11247 }
11248
11249 /* Allocate storage for parameters and fill them in. */
11250 TYPE_NFIELDS (ftype) = nparams;
11251 TYPE_FIELDS (ftype) = (struct field *)
11252 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11253
11254 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11255 even if we error out during the parameters reading below. */
11256 for (iparams = 0; iparams < nparams; iparams++)
11257 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11258
11259 iparams = 0;
11260 child_die = die->child;
11261 while (child_die && child_die->tag)
11262 {
11263 if (child_die->tag == DW_TAG_formal_parameter)
11264 {
11265 struct type *arg_type;
11266
11267 /* DWARF version 2 has no clean way to discern C++
11268 static and non-static member functions. G++ helps
11269 GDB by marking the first parameter for non-static
11270 member functions (which is the this pointer) as
11271 artificial. We pass this information to
11272 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11273
11274 DWARF version 3 added DW_AT_object_pointer, which GCC
11275 4.5 does not yet generate. */
11276 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11277 if (attr)
11278 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11279 else
11280 {
11281 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11282
11283 /* GCC/43521: In java, the formal parameter
11284 "this" is sometimes not marked with DW_AT_artificial. */
11285 if (cu->language == language_java)
11286 {
11287 const char *name = dwarf2_name (child_die, cu);
11288
11289 if (name && !strcmp (name, "this"))
11290 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11291 }
11292 }
11293 arg_type = die_type (child_die, cu);
11294
11295 /* RealView does not mark THIS as const, which the testsuite
11296 expects. GCC marks THIS as const in method definitions,
11297 but not in the class specifications (GCC PR 43053). */
11298 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11299 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11300 {
11301 int is_this = 0;
11302 struct dwarf2_cu *arg_cu = cu;
11303 const char *name = dwarf2_name (child_die, cu);
11304
11305 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11306 if (attr)
11307 {
11308 /* If the compiler emits this, use it. */
11309 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11310 is_this = 1;
11311 }
11312 else if (name && strcmp (name, "this") == 0)
11313 /* Function definitions will have the argument names. */
11314 is_this = 1;
11315 else if (name == NULL && iparams == 0)
11316 /* Declarations may not have the names, so like
11317 elsewhere in GDB, assume an artificial first
11318 argument is "this". */
11319 is_this = 1;
11320
11321 if (is_this)
11322 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11323 arg_type, 0);
11324 }
11325
11326 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11327 iparams++;
11328 }
11329 child_die = sibling_die (child_die);
11330 }
11331 }
11332
11333 return ftype;
11334 }
11335
11336 static struct type *
11337 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11338 {
11339 struct objfile *objfile = cu->objfile;
11340 const char *name = NULL;
11341 struct type *this_type, *target_type;
11342
11343 name = dwarf2_full_name (NULL, die, cu);
11344 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11345 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11346 TYPE_NAME (this_type) = (char *) name;
11347 set_die_type (die, this_type, cu);
11348 target_type = die_type (die, cu);
11349 if (target_type != this_type)
11350 TYPE_TARGET_TYPE (this_type) = target_type;
11351 else
11352 {
11353 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11354 spec and cause infinite loops in GDB. */
11355 complaint (&symfile_complaints,
11356 _("Self-referential DW_TAG_typedef "
11357 "- DIE at 0x%x [in module %s]"),
11358 die->offset.sect_off, objfile->name);
11359 TYPE_TARGET_TYPE (this_type) = NULL;
11360 }
11361 return this_type;
11362 }
11363
11364 /* Find a representation of a given base type and install
11365 it in the TYPE field of the die. */
11366
11367 static struct type *
11368 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11369 {
11370 struct objfile *objfile = cu->objfile;
11371 struct type *type;
11372 struct attribute *attr;
11373 int encoding = 0, size = 0;
11374 char *name;
11375 enum type_code code = TYPE_CODE_INT;
11376 int type_flags = 0;
11377 struct type *target_type = NULL;
11378
11379 attr = dwarf2_attr (die, DW_AT_encoding, cu);
11380 if (attr)
11381 {
11382 encoding = DW_UNSND (attr);
11383 }
11384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11385 if (attr)
11386 {
11387 size = DW_UNSND (attr);
11388 }
11389 name = dwarf2_name (die, cu);
11390 if (!name)
11391 {
11392 complaint (&symfile_complaints,
11393 _("DW_AT_name missing from DW_TAG_base_type"));
11394 }
11395
11396 switch (encoding)
11397 {
11398 case DW_ATE_address:
11399 /* Turn DW_ATE_address into a void * pointer. */
11400 code = TYPE_CODE_PTR;
11401 type_flags |= TYPE_FLAG_UNSIGNED;
11402 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11403 break;
11404 case DW_ATE_boolean:
11405 code = TYPE_CODE_BOOL;
11406 type_flags |= TYPE_FLAG_UNSIGNED;
11407 break;
11408 case DW_ATE_complex_float:
11409 code = TYPE_CODE_COMPLEX;
11410 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11411 break;
11412 case DW_ATE_decimal_float:
11413 code = TYPE_CODE_DECFLOAT;
11414 break;
11415 case DW_ATE_float:
11416 code = TYPE_CODE_FLT;
11417 break;
11418 case DW_ATE_signed:
11419 break;
11420 case DW_ATE_unsigned:
11421 type_flags |= TYPE_FLAG_UNSIGNED;
11422 if (cu->language == language_fortran
11423 && name
11424 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11425 code = TYPE_CODE_CHAR;
11426 break;
11427 case DW_ATE_signed_char:
11428 if (cu->language == language_ada || cu->language == language_m2
11429 || cu->language == language_pascal
11430 || cu->language == language_fortran)
11431 code = TYPE_CODE_CHAR;
11432 break;
11433 case DW_ATE_unsigned_char:
11434 if (cu->language == language_ada || cu->language == language_m2
11435 || cu->language == language_pascal
11436 || cu->language == language_fortran)
11437 code = TYPE_CODE_CHAR;
11438 type_flags |= TYPE_FLAG_UNSIGNED;
11439 break;
11440 case DW_ATE_UTF:
11441 /* We just treat this as an integer and then recognize the
11442 type by name elsewhere. */
11443 break;
11444
11445 default:
11446 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11447 dwarf_type_encoding_name (encoding));
11448 break;
11449 }
11450
11451 type = init_type (code, size, type_flags, NULL, objfile);
11452 TYPE_NAME (type) = name;
11453 TYPE_TARGET_TYPE (type) = target_type;
11454
11455 if (name && strcmp (name, "char") == 0)
11456 TYPE_NOSIGN (type) = 1;
11457
11458 return set_die_type (die, type, cu);
11459 }
11460
11461 /* Read the given DW_AT_subrange DIE. */
11462
11463 static struct type *
11464 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11465 {
11466 struct type *base_type;
11467 struct type *range_type;
11468 struct attribute *attr;
11469 LONGEST low, high;
11470 int low_default_is_valid;
11471 char *name;
11472 LONGEST negative_mask;
11473
11474 base_type = die_type (die, cu);
11475 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11476 check_typedef (base_type);
11477
11478 /* The die_type call above may have already set the type for this DIE. */
11479 range_type = get_die_type (die, cu);
11480 if (range_type)
11481 return range_type;
11482
11483 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11484 omitting DW_AT_lower_bound. */
11485 switch (cu->language)
11486 {
11487 case language_c:
11488 case language_cplus:
11489 low = 0;
11490 low_default_is_valid = 1;
11491 break;
11492 case language_fortran:
11493 low = 1;
11494 low_default_is_valid = 1;
11495 break;
11496 case language_d:
11497 case language_java:
11498 case language_objc:
11499 low = 0;
11500 low_default_is_valid = (cu->header.version >= 4);
11501 break;
11502 case language_ada:
11503 case language_m2:
11504 case language_pascal:
11505 low = 1;
11506 low_default_is_valid = (cu->header.version >= 4);
11507 break;
11508 default:
11509 low = 0;
11510 low_default_is_valid = 0;
11511 break;
11512 }
11513
11514 /* FIXME: For variable sized arrays either of these could be
11515 a variable rather than a constant value. We'll allow it,
11516 but we don't know how to handle it. */
11517 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11518 if (attr)
11519 low = dwarf2_get_attr_constant_value (attr, low);
11520 else if (!low_default_is_valid)
11521 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11522 "- DIE at 0x%x [in module %s]"),
11523 die->offset.sect_off, cu->objfile->name);
11524
11525 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11526 if (attr)
11527 {
11528 if (attr_form_is_block (attr) || is_ref_attr (attr))
11529 {
11530 /* GCC encodes arrays with unspecified or dynamic length
11531 with a DW_FORM_block1 attribute or a reference attribute.
11532 FIXME: GDB does not yet know how to handle dynamic
11533 arrays properly, treat them as arrays with unspecified
11534 length for now.
11535
11536 FIXME: jimb/2003-09-22: GDB does not really know
11537 how to handle arrays of unspecified length
11538 either; we just represent them as zero-length
11539 arrays. Choose an appropriate upper bound given
11540 the lower bound we've computed above. */
11541 high = low - 1;
11542 }
11543 else
11544 high = dwarf2_get_attr_constant_value (attr, 1);
11545 }
11546 else
11547 {
11548 attr = dwarf2_attr (die, DW_AT_count, cu);
11549 if (attr)
11550 {
11551 int count = dwarf2_get_attr_constant_value (attr, 1);
11552 high = low + count - 1;
11553 }
11554 else
11555 {
11556 /* Unspecified array length. */
11557 high = low - 1;
11558 }
11559 }
11560
11561 /* Dwarf-2 specifications explicitly allows to create subrange types
11562 without specifying a base type.
11563 In that case, the base type must be set to the type of
11564 the lower bound, upper bound or count, in that order, if any of these
11565 three attributes references an object that has a type.
11566 If no base type is found, the Dwarf-2 specifications say that
11567 a signed integer type of size equal to the size of an address should
11568 be used.
11569 For the following C code: `extern char gdb_int [];'
11570 GCC produces an empty range DIE.
11571 FIXME: muller/2010-05-28: Possible references to object for low bound,
11572 high bound or count are not yet handled by this code. */
11573 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11574 {
11575 struct objfile *objfile = cu->objfile;
11576 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11577 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11578 struct type *int_type = objfile_type (objfile)->builtin_int;
11579
11580 /* Test "int", "long int", and "long long int" objfile types,
11581 and select the first one having a size above or equal to the
11582 architecture address size. */
11583 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11584 base_type = int_type;
11585 else
11586 {
11587 int_type = objfile_type (objfile)->builtin_long;
11588 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11589 base_type = int_type;
11590 else
11591 {
11592 int_type = objfile_type (objfile)->builtin_long_long;
11593 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11594 base_type = int_type;
11595 }
11596 }
11597 }
11598
11599 negative_mask =
11600 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11601 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11602 low |= negative_mask;
11603 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11604 high |= negative_mask;
11605
11606 range_type = create_range_type (NULL, base_type, low, high);
11607
11608 /* Mark arrays with dynamic length at least as an array of unspecified
11609 length. GDB could check the boundary but before it gets implemented at
11610 least allow accessing the array elements. */
11611 if (attr && attr_form_is_block (attr))
11612 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11613
11614 /* Ada expects an empty array on no boundary attributes. */
11615 if (attr == NULL && cu->language != language_ada)
11616 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11617
11618 name = dwarf2_name (die, cu);
11619 if (name)
11620 TYPE_NAME (range_type) = name;
11621
11622 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11623 if (attr)
11624 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11625
11626 set_die_type (die, range_type, cu);
11627
11628 /* set_die_type should be already done. */
11629 set_descriptive_type (range_type, die, cu);
11630
11631 return range_type;
11632 }
11633
11634 static struct type *
11635 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11636 {
11637 struct type *type;
11638
11639 /* For now, we only support the C meaning of an unspecified type: void. */
11640
11641 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11642 TYPE_NAME (type) = dwarf2_name (die, cu);
11643
11644 return set_die_type (die, type, cu);
11645 }
11646
11647 /* Read a single die and all its descendents. Set the die's sibling
11648 field to NULL; set other fields in the die correctly, and set all
11649 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11650 location of the info_ptr after reading all of those dies. PARENT
11651 is the parent of the die in question. */
11652
11653 static struct die_info *
11654 read_die_and_children (const struct die_reader_specs *reader,
11655 gdb_byte *info_ptr,
11656 gdb_byte **new_info_ptr,
11657 struct die_info *parent)
11658 {
11659 struct die_info *die;
11660 gdb_byte *cur_ptr;
11661 int has_children;
11662
11663 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11664 if (die == NULL)
11665 {
11666 *new_info_ptr = cur_ptr;
11667 return NULL;
11668 }
11669 store_in_ref_table (die, reader->cu);
11670
11671 if (has_children)
11672 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11673 else
11674 {
11675 die->child = NULL;
11676 *new_info_ptr = cur_ptr;
11677 }
11678
11679 die->sibling = NULL;
11680 die->parent = parent;
11681 return die;
11682 }
11683
11684 /* Read a die, all of its descendents, and all of its siblings; set
11685 all of the fields of all of the dies correctly. Arguments are as
11686 in read_die_and_children. */
11687
11688 static struct die_info *
11689 read_die_and_siblings (const struct die_reader_specs *reader,
11690 gdb_byte *info_ptr,
11691 gdb_byte **new_info_ptr,
11692 struct die_info *parent)
11693 {
11694 struct die_info *first_die, *last_sibling;
11695 gdb_byte *cur_ptr;
11696
11697 cur_ptr = info_ptr;
11698 first_die = last_sibling = NULL;
11699
11700 while (1)
11701 {
11702 struct die_info *die
11703 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
11704
11705 if (die == NULL)
11706 {
11707 *new_info_ptr = cur_ptr;
11708 return first_die;
11709 }
11710
11711 if (!first_die)
11712 first_die = die;
11713 else
11714 last_sibling->sibling = die;
11715
11716 last_sibling = die;
11717 }
11718 }
11719
11720 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
11721 attributes.
11722 The caller is responsible for filling in the extra attributes
11723 and updating (*DIEP)->num_attrs.
11724 Set DIEP to point to a newly allocated die with its information,
11725 except for its child, sibling, and parent fields.
11726 Set HAS_CHILDREN to tell whether the die has children or not. */
11727
11728 static gdb_byte *
11729 read_full_die_1 (const struct die_reader_specs *reader,
11730 struct die_info **diep, gdb_byte *info_ptr,
11731 int *has_children, int num_extra_attrs)
11732 {
11733 unsigned int abbrev_number, bytes_read, i;
11734 sect_offset offset;
11735 struct abbrev_info *abbrev;
11736 struct die_info *die;
11737 struct dwarf2_cu *cu = reader->cu;
11738 bfd *abfd = reader->abfd;
11739
11740 offset.sect_off = info_ptr - reader->buffer;
11741 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11742 info_ptr += bytes_read;
11743 if (!abbrev_number)
11744 {
11745 *diep = NULL;
11746 *has_children = 0;
11747 return info_ptr;
11748 }
11749
11750 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
11751 if (!abbrev)
11752 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
11753 abbrev_number,
11754 bfd_get_filename (abfd));
11755
11756 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
11757 die->offset = offset;
11758 die->tag = abbrev->tag;
11759 die->abbrev = abbrev_number;
11760
11761 /* Make the result usable.
11762 The caller needs to update num_attrs after adding the extra
11763 attributes. */
11764 die->num_attrs = abbrev->num_attrs;
11765
11766 for (i = 0; i < abbrev->num_attrs; ++i)
11767 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
11768 info_ptr);
11769
11770 *diep = die;
11771 *has_children = abbrev->has_children;
11772 return info_ptr;
11773 }
11774
11775 /* Read a die and all its attributes.
11776 Set DIEP to point to a newly allocated die with its information,
11777 except for its child, sibling, and parent fields.
11778 Set HAS_CHILDREN to tell whether the die has children or not. */
11779
11780 static gdb_byte *
11781 read_full_die (const struct die_reader_specs *reader,
11782 struct die_info **diep, gdb_byte *info_ptr,
11783 int *has_children)
11784 {
11785 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
11786 }
11787 \f
11788 /* Abbreviation tables.
11789
11790 In DWARF version 2, the description of the debugging information is
11791 stored in a separate .debug_abbrev section. Before we read any
11792 dies from a section we read in all abbreviations and install them
11793 in a hash table. */
11794
11795 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
11796
11797 static struct abbrev_info *
11798 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
11799 {
11800 struct abbrev_info *abbrev;
11801
11802 abbrev = (struct abbrev_info *)
11803 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
11804 memset (abbrev, 0, sizeof (struct abbrev_info));
11805 return abbrev;
11806 }
11807
11808 /* Add an abbreviation to the table. */
11809
11810 static void
11811 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11812 unsigned int abbrev_number,
11813 struct abbrev_info *abbrev)
11814 {
11815 unsigned int hash_number;
11816
11817 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11818 abbrev->next = abbrev_table->abbrevs[hash_number];
11819 abbrev_table->abbrevs[hash_number] = abbrev;
11820 }
11821
11822 /* Look up an abbrev in the table.
11823 Returns NULL if the abbrev is not found. */
11824
11825 static struct abbrev_info *
11826 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11827 unsigned int abbrev_number)
11828 {
11829 unsigned int hash_number;
11830 struct abbrev_info *abbrev;
11831
11832 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11833 abbrev = abbrev_table->abbrevs[hash_number];
11834
11835 while (abbrev)
11836 {
11837 if (abbrev->number == abbrev_number)
11838 return abbrev;
11839 abbrev = abbrev->next;
11840 }
11841 return NULL;
11842 }
11843
11844 /* Read in an abbrev table. */
11845
11846 static struct abbrev_table *
11847 abbrev_table_read_table (struct dwarf2_section_info *section,
11848 sect_offset offset)
11849 {
11850 struct objfile *objfile = dwarf2_per_objfile->objfile;
11851 bfd *abfd = section->asection->owner;
11852 struct abbrev_table *abbrev_table;
11853 gdb_byte *abbrev_ptr;
11854 struct abbrev_info *cur_abbrev;
11855 unsigned int abbrev_number, bytes_read, abbrev_name;
11856 unsigned int abbrev_form;
11857 struct attr_abbrev *cur_attrs;
11858 unsigned int allocated_attrs;
11859
11860 abbrev_table = XMALLOC (struct abbrev_table);
11861 abbrev_table->offset = offset;
11862 obstack_init (&abbrev_table->abbrev_obstack);
11863 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
11864 (ABBREV_HASH_SIZE
11865 * sizeof (struct abbrev_info *)));
11866 memset (abbrev_table->abbrevs, 0,
11867 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
11868
11869 dwarf2_read_section (objfile, section);
11870 abbrev_ptr = section->buffer + offset.sect_off;
11871 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11872 abbrev_ptr += bytes_read;
11873
11874 allocated_attrs = ATTR_ALLOC_CHUNK;
11875 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
11876
11877 /* Loop until we reach an abbrev number of 0. */
11878 while (abbrev_number)
11879 {
11880 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
11881
11882 /* read in abbrev header */
11883 cur_abbrev->number = abbrev_number;
11884 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11885 abbrev_ptr += bytes_read;
11886 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11887 abbrev_ptr += 1;
11888
11889 /* now read in declarations */
11890 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11891 abbrev_ptr += bytes_read;
11892 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11893 abbrev_ptr += bytes_read;
11894 while (abbrev_name)
11895 {
11896 if (cur_abbrev->num_attrs == allocated_attrs)
11897 {
11898 allocated_attrs += ATTR_ALLOC_CHUNK;
11899 cur_attrs
11900 = xrealloc (cur_attrs, (allocated_attrs
11901 * sizeof (struct attr_abbrev)));
11902 }
11903
11904 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11905 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
11906 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11907 abbrev_ptr += bytes_read;
11908 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11909 abbrev_ptr += bytes_read;
11910 }
11911
11912 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
11913 (cur_abbrev->num_attrs
11914 * sizeof (struct attr_abbrev)));
11915 memcpy (cur_abbrev->attrs, cur_attrs,
11916 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11917
11918 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
11919
11920 /* Get next abbreviation.
11921 Under Irix6 the abbreviations for a compilation unit are not
11922 always properly terminated with an abbrev number of 0.
11923 Exit loop if we encounter an abbreviation which we have
11924 already read (which means we are about to read the abbreviations
11925 for the next compile unit) or if the end of the abbreviation
11926 table is reached. */
11927 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
11928 break;
11929 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11930 abbrev_ptr += bytes_read;
11931 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
11932 break;
11933 }
11934
11935 xfree (cur_attrs);
11936 return abbrev_table;
11937 }
11938
11939 /* Free the resources held by ABBREV_TABLE. */
11940
11941 static void
11942 abbrev_table_free (struct abbrev_table *abbrev_table)
11943 {
11944 obstack_free (&abbrev_table->abbrev_obstack, NULL);
11945 xfree (abbrev_table);
11946 }
11947
11948 /* Same as abbrev_table_free but as a cleanup.
11949 We pass in a pointer to the pointer to the table so that we can
11950 set the pointer to NULL when we're done. It also simplifies
11951 build_type_unit_groups. */
11952
11953 static void
11954 abbrev_table_free_cleanup (void *table_ptr)
11955 {
11956 struct abbrev_table **abbrev_table_ptr = table_ptr;
11957
11958 if (*abbrev_table_ptr != NULL)
11959 abbrev_table_free (*abbrev_table_ptr);
11960 *abbrev_table_ptr = NULL;
11961 }
11962
11963 /* Read the abbrev table for CU from ABBREV_SECTION. */
11964
11965 static void
11966 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
11967 struct dwarf2_section_info *abbrev_section)
11968 {
11969 cu->abbrev_table =
11970 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11971 }
11972
11973 /* Release the memory used by the abbrev table for a compilation unit. */
11974
11975 static void
11976 dwarf2_free_abbrev_table (void *ptr_to_cu)
11977 {
11978 struct dwarf2_cu *cu = ptr_to_cu;
11979
11980 abbrev_table_free (cu->abbrev_table);
11981 /* Set this to NULL so that we SEGV if we try to read it later,
11982 and also because free_comp_unit verifies this is NULL. */
11983 cu->abbrev_table = NULL;
11984 }
11985 \f
11986 /* Returns nonzero if TAG represents a type that we might generate a partial
11987 symbol for. */
11988
11989 static int
11990 is_type_tag_for_partial (int tag)
11991 {
11992 switch (tag)
11993 {
11994 #if 0
11995 /* Some types that would be reasonable to generate partial symbols for,
11996 that we don't at present. */
11997 case DW_TAG_array_type:
11998 case DW_TAG_file_type:
11999 case DW_TAG_ptr_to_member_type:
12000 case DW_TAG_set_type:
12001 case DW_TAG_string_type:
12002 case DW_TAG_subroutine_type:
12003 #endif
12004 case DW_TAG_base_type:
12005 case DW_TAG_class_type:
12006 case DW_TAG_interface_type:
12007 case DW_TAG_enumeration_type:
12008 case DW_TAG_structure_type:
12009 case DW_TAG_subrange_type:
12010 case DW_TAG_typedef:
12011 case DW_TAG_union_type:
12012 return 1;
12013 default:
12014 return 0;
12015 }
12016 }
12017
12018 /* Load all DIEs that are interesting for partial symbols into memory. */
12019
12020 static struct partial_die_info *
12021 load_partial_dies (const struct die_reader_specs *reader,
12022 gdb_byte *info_ptr, int building_psymtab)
12023 {
12024 struct dwarf2_cu *cu = reader->cu;
12025 struct objfile *objfile = cu->objfile;
12026 struct partial_die_info *part_die;
12027 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12028 struct abbrev_info *abbrev;
12029 unsigned int bytes_read;
12030 unsigned int load_all = 0;
12031 int nesting_level = 1;
12032
12033 parent_die = NULL;
12034 last_die = NULL;
12035
12036 gdb_assert (cu->per_cu != NULL);
12037 if (cu->per_cu->load_all_dies)
12038 load_all = 1;
12039
12040 cu->partial_dies
12041 = htab_create_alloc_ex (cu->header.length / 12,
12042 partial_die_hash,
12043 partial_die_eq,
12044 NULL,
12045 &cu->comp_unit_obstack,
12046 hashtab_obstack_allocate,
12047 dummy_obstack_deallocate);
12048
12049 part_die = obstack_alloc (&cu->comp_unit_obstack,
12050 sizeof (struct partial_die_info));
12051
12052 while (1)
12053 {
12054 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12055
12056 /* A NULL abbrev means the end of a series of children. */
12057 if (abbrev == NULL)
12058 {
12059 if (--nesting_level == 0)
12060 {
12061 /* PART_DIE was probably the last thing allocated on the
12062 comp_unit_obstack, so we could call obstack_free
12063 here. We don't do that because the waste is small,
12064 and will be cleaned up when we're done with this
12065 compilation unit. This way, we're also more robust
12066 against other users of the comp_unit_obstack. */
12067 return first_die;
12068 }
12069 info_ptr += bytes_read;
12070 last_die = parent_die;
12071 parent_die = parent_die->die_parent;
12072 continue;
12073 }
12074
12075 /* Check for template arguments. We never save these; if
12076 they're seen, we just mark the parent, and go on our way. */
12077 if (parent_die != NULL
12078 && cu->language == language_cplus
12079 && (abbrev->tag == DW_TAG_template_type_param
12080 || abbrev->tag == DW_TAG_template_value_param))
12081 {
12082 parent_die->has_template_arguments = 1;
12083
12084 if (!load_all)
12085 {
12086 /* We don't need a partial DIE for the template argument. */
12087 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12088 continue;
12089 }
12090 }
12091
12092 /* We only recurse into c++ subprograms looking for template arguments.
12093 Skip their other children. */
12094 if (!load_all
12095 && cu->language == language_cplus
12096 && parent_die != NULL
12097 && parent_die->tag == DW_TAG_subprogram)
12098 {
12099 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12100 continue;
12101 }
12102
12103 /* Check whether this DIE is interesting enough to save. Normally
12104 we would not be interested in members here, but there may be
12105 later variables referencing them via DW_AT_specification (for
12106 static members). */
12107 if (!load_all
12108 && !is_type_tag_for_partial (abbrev->tag)
12109 && abbrev->tag != DW_TAG_constant
12110 && abbrev->tag != DW_TAG_enumerator
12111 && abbrev->tag != DW_TAG_subprogram
12112 && abbrev->tag != DW_TAG_lexical_block
12113 && abbrev->tag != DW_TAG_variable
12114 && abbrev->tag != DW_TAG_namespace
12115 && abbrev->tag != DW_TAG_module
12116 && abbrev->tag != DW_TAG_member
12117 && abbrev->tag != DW_TAG_imported_unit)
12118 {
12119 /* Otherwise we skip to the next sibling, if any. */
12120 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12121 continue;
12122 }
12123
12124 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12125 info_ptr);
12126
12127 /* This two-pass algorithm for processing partial symbols has a
12128 high cost in cache pressure. Thus, handle some simple cases
12129 here which cover the majority of C partial symbols. DIEs
12130 which neither have specification tags in them, nor could have
12131 specification tags elsewhere pointing at them, can simply be
12132 processed and discarded.
12133
12134 This segment is also optional; scan_partial_symbols and
12135 add_partial_symbol will handle these DIEs if we chain
12136 them in normally. When compilers which do not emit large
12137 quantities of duplicate debug information are more common,
12138 this code can probably be removed. */
12139
12140 /* Any complete simple types at the top level (pretty much all
12141 of them, for a language without namespaces), can be processed
12142 directly. */
12143 if (parent_die == NULL
12144 && part_die->has_specification == 0
12145 && part_die->is_declaration == 0
12146 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12147 || part_die->tag == DW_TAG_base_type
12148 || part_die->tag == DW_TAG_subrange_type))
12149 {
12150 if (building_psymtab && part_die->name != NULL)
12151 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12152 VAR_DOMAIN, LOC_TYPEDEF,
12153 &objfile->static_psymbols,
12154 0, (CORE_ADDR) 0, cu->language, objfile);
12155 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12156 continue;
12157 }
12158
12159 /* The exception for DW_TAG_typedef with has_children above is
12160 a workaround of GCC PR debug/47510. In the case of this complaint
12161 type_name_no_tag_or_error will error on such types later.
12162
12163 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12164 it could not find the child DIEs referenced later, this is checked
12165 above. In correct DWARF DW_TAG_typedef should have no children. */
12166
12167 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12168 complaint (&symfile_complaints,
12169 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12170 "- DIE at 0x%x [in module %s]"),
12171 part_die->offset.sect_off, objfile->name);
12172
12173 /* If we're at the second level, and we're an enumerator, and
12174 our parent has no specification (meaning possibly lives in a
12175 namespace elsewhere), then we can add the partial symbol now
12176 instead of queueing it. */
12177 if (part_die->tag == DW_TAG_enumerator
12178 && parent_die != NULL
12179 && parent_die->die_parent == NULL
12180 && parent_die->tag == DW_TAG_enumeration_type
12181 && parent_die->has_specification == 0)
12182 {
12183 if (part_die->name == NULL)
12184 complaint (&symfile_complaints,
12185 _("malformed enumerator DIE ignored"));
12186 else if (building_psymtab)
12187 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12188 VAR_DOMAIN, LOC_CONST,
12189 (cu->language == language_cplus
12190 || cu->language == language_java)
12191 ? &objfile->global_psymbols
12192 : &objfile->static_psymbols,
12193 0, (CORE_ADDR) 0, cu->language, objfile);
12194
12195 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12196 continue;
12197 }
12198
12199 /* We'll save this DIE so link it in. */
12200 part_die->die_parent = parent_die;
12201 part_die->die_sibling = NULL;
12202 part_die->die_child = NULL;
12203
12204 if (last_die && last_die == parent_die)
12205 last_die->die_child = part_die;
12206 else if (last_die)
12207 last_die->die_sibling = part_die;
12208
12209 last_die = part_die;
12210
12211 if (first_die == NULL)
12212 first_die = part_die;
12213
12214 /* Maybe add the DIE to the hash table. Not all DIEs that we
12215 find interesting need to be in the hash table, because we
12216 also have the parent/sibling/child chains; only those that we
12217 might refer to by offset later during partial symbol reading.
12218
12219 For now this means things that might have be the target of a
12220 DW_AT_specification, DW_AT_abstract_origin, or
12221 DW_AT_extension. DW_AT_extension will refer only to
12222 namespaces; DW_AT_abstract_origin refers to functions (and
12223 many things under the function DIE, but we do not recurse
12224 into function DIEs during partial symbol reading) and
12225 possibly variables as well; DW_AT_specification refers to
12226 declarations. Declarations ought to have the DW_AT_declaration
12227 flag. It happens that GCC forgets to put it in sometimes, but
12228 only for functions, not for types.
12229
12230 Adding more things than necessary to the hash table is harmless
12231 except for the performance cost. Adding too few will result in
12232 wasted time in find_partial_die, when we reread the compilation
12233 unit with load_all_dies set. */
12234
12235 if (load_all
12236 || abbrev->tag == DW_TAG_constant
12237 || abbrev->tag == DW_TAG_subprogram
12238 || abbrev->tag == DW_TAG_variable
12239 || abbrev->tag == DW_TAG_namespace
12240 || part_die->is_declaration)
12241 {
12242 void **slot;
12243
12244 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12245 part_die->offset.sect_off, INSERT);
12246 *slot = part_die;
12247 }
12248
12249 part_die = obstack_alloc (&cu->comp_unit_obstack,
12250 sizeof (struct partial_die_info));
12251
12252 /* For some DIEs we want to follow their children (if any). For C
12253 we have no reason to follow the children of structures; for other
12254 languages we have to, so that we can get at method physnames
12255 to infer fully qualified class names, for DW_AT_specification,
12256 and for C++ template arguments. For C++, we also look one level
12257 inside functions to find template arguments (if the name of the
12258 function does not already contain the template arguments).
12259
12260 For Ada, we need to scan the children of subprograms and lexical
12261 blocks as well because Ada allows the definition of nested
12262 entities that could be interesting for the debugger, such as
12263 nested subprograms for instance. */
12264 if (last_die->has_children
12265 && (load_all
12266 || last_die->tag == DW_TAG_namespace
12267 || last_die->tag == DW_TAG_module
12268 || last_die->tag == DW_TAG_enumeration_type
12269 || (cu->language == language_cplus
12270 && last_die->tag == DW_TAG_subprogram
12271 && (last_die->name == NULL
12272 || strchr (last_die->name, '<') == NULL))
12273 || (cu->language != language_c
12274 && (last_die->tag == DW_TAG_class_type
12275 || last_die->tag == DW_TAG_interface_type
12276 || last_die->tag == DW_TAG_structure_type
12277 || last_die->tag == DW_TAG_union_type))
12278 || (cu->language == language_ada
12279 && (last_die->tag == DW_TAG_subprogram
12280 || last_die->tag == DW_TAG_lexical_block))))
12281 {
12282 nesting_level++;
12283 parent_die = last_die;
12284 continue;
12285 }
12286
12287 /* Otherwise we skip to the next sibling, if any. */
12288 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12289
12290 /* Back to the top, do it again. */
12291 }
12292 }
12293
12294 /* Read a minimal amount of information into the minimal die structure. */
12295
12296 static gdb_byte *
12297 read_partial_die (const struct die_reader_specs *reader,
12298 struct partial_die_info *part_die,
12299 struct abbrev_info *abbrev, unsigned int abbrev_len,
12300 gdb_byte *info_ptr)
12301 {
12302 struct dwarf2_cu *cu = reader->cu;
12303 struct objfile *objfile = cu->objfile;
12304 gdb_byte *buffer = reader->buffer;
12305 unsigned int i;
12306 struct attribute attr;
12307 int has_low_pc_attr = 0;
12308 int has_high_pc_attr = 0;
12309 int high_pc_relative = 0;
12310
12311 memset (part_die, 0, sizeof (struct partial_die_info));
12312
12313 part_die->offset.sect_off = info_ptr - buffer;
12314
12315 info_ptr += abbrev_len;
12316
12317 if (abbrev == NULL)
12318 return info_ptr;
12319
12320 part_die->tag = abbrev->tag;
12321 part_die->has_children = abbrev->has_children;
12322
12323 for (i = 0; i < abbrev->num_attrs; ++i)
12324 {
12325 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12326
12327 /* Store the data if it is of an attribute we want to keep in a
12328 partial symbol table. */
12329 switch (attr.name)
12330 {
12331 case DW_AT_name:
12332 switch (part_die->tag)
12333 {
12334 case DW_TAG_compile_unit:
12335 case DW_TAG_partial_unit:
12336 case DW_TAG_type_unit:
12337 /* Compilation units have a DW_AT_name that is a filename, not
12338 a source language identifier. */
12339 case DW_TAG_enumeration_type:
12340 case DW_TAG_enumerator:
12341 /* These tags always have simple identifiers already; no need
12342 to canonicalize them. */
12343 part_die->name = DW_STRING (&attr);
12344 break;
12345 default:
12346 part_die->name
12347 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12348 &objfile->objfile_obstack);
12349 break;
12350 }
12351 break;
12352 case DW_AT_linkage_name:
12353 case DW_AT_MIPS_linkage_name:
12354 /* Note that both forms of linkage name might appear. We
12355 assume they will be the same, and we only store the last
12356 one we see. */
12357 if (cu->language == language_ada)
12358 part_die->name = DW_STRING (&attr);
12359 part_die->linkage_name = DW_STRING (&attr);
12360 break;
12361 case DW_AT_low_pc:
12362 has_low_pc_attr = 1;
12363 part_die->lowpc = DW_ADDR (&attr);
12364 break;
12365 case DW_AT_high_pc:
12366 has_high_pc_attr = 1;
12367 if (attr.form == DW_FORM_addr
12368 || attr.form == DW_FORM_GNU_addr_index)
12369 part_die->highpc = DW_ADDR (&attr);
12370 else
12371 {
12372 high_pc_relative = 1;
12373 part_die->highpc = DW_UNSND (&attr);
12374 }
12375 break;
12376 case DW_AT_location:
12377 /* Support the .debug_loc offsets. */
12378 if (attr_form_is_block (&attr))
12379 {
12380 part_die->d.locdesc = DW_BLOCK (&attr);
12381 }
12382 else if (attr_form_is_section_offset (&attr))
12383 {
12384 dwarf2_complex_location_expr_complaint ();
12385 }
12386 else
12387 {
12388 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12389 "partial symbol information");
12390 }
12391 break;
12392 case DW_AT_external:
12393 part_die->is_external = DW_UNSND (&attr);
12394 break;
12395 case DW_AT_declaration:
12396 part_die->is_declaration = DW_UNSND (&attr);
12397 break;
12398 case DW_AT_type:
12399 part_die->has_type = 1;
12400 break;
12401 case DW_AT_abstract_origin:
12402 case DW_AT_specification:
12403 case DW_AT_extension:
12404 part_die->has_specification = 1;
12405 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12406 break;
12407 case DW_AT_sibling:
12408 /* Ignore absolute siblings, they might point outside of
12409 the current compile unit. */
12410 if (attr.form == DW_FORM_ref_addr)
12411 complaint (&symfile_complaints,
12412 _("ignoring absolute DW_AT_sibling"));
12413 else
12414 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12415 break;
12416 case DW_AT_byte_size:
12417 part_die->has_byte_size = 1;
12418 break;
12419 case DW_AT_calling_convention:
12420 /* DWARF doesn't provide a way to identify a program's source-level
12421 entry point. DW_AT_calling_convention attributes are only meant
12422 to describe functions' calling conventions.
12423
12424 However, because it's a necessary piece of information in
12425 Fortran, and because DW_CC_program is the only piece of debugging
12426 information whose definition refers to a 'main program' at all,
12427 several compilers have begun marking Fortran main programs with
12428 DW_CC_program --- even when those functions use the standard
12429 calling conventions.
12430
12431 So until DWARF specifies a way to provide this information and
12432 compilers pick up the new representation, we'll support this
12433 practice. */
12434 if (DW_UNSND (&attr) == DW_CC_program
12435 && cu->language == language_fortran)
12436 {
12437 set_main_name (part_die->name);
12438
12439 /* As this DIE has a static linkage the name would be difficult
12440 to look up later. */
12441 language_of_main = language_fortran;
12442 }
12443 break;
12444 case DW_AT_inline:
12445 if (DW_UNSND (&attr) == DW_INL_inlined
12446 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12447 part_die->may_be_inlined = 1;
12448 break;
12449
12450 case DW_AT_import:
12451 if (part_die->tag == DW_TAG_imported_unit)
12452 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12453 break;
12454
12455 default:
12456 break;
12457 }
12458 }
12459
12460 if (high_pc_relative)
12461 part_die->highpc += part_die->lowpc;
12462
12463 if (has_low_pc_attr && has_high_pc_attr)
12464 {
12465 /* When using the GNU linker, .gnu.linkonce. sections are used to
12466 eliminate duplicate copies of functions and vtables and such.
12467 The linker will arbitrarily choose one and discard the others.
12468 The AT_*_pc values for such functions refer to local labels in
12469 these sections. If the section from that file was discarded, the
12470 labels are not in the output, so the relocs get a value of 0.
12471 If this is a discarded function, mark the pc bounds as invalid,
12472 so that GDB will ignore it. */
12473 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12474 {
12475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12476
12477 complaint (&symfile_complaints,
12478 _("DW_AT_low_pc %s is zero "
12479 "for DIE at 0x%x [in module %s]"),
12480 paddress (gdbarch, part_die->lowpc),
12481 part_die->offset.sect_off, objfile->name);
12482 }
12483 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12484 else if (part_die->lowpc >= part_die->highpc)
12485 {
12486 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12487
12488 complaint (&symfile_complaints,
12489 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12490 "for DIE at 0x%x [in module %s]"),
12491 paddress (gdbarch, part_die->lowpc),
12492 paddress (gdbarch, part_die->highpc),
12493 part_die->offset.sect_off, objfile->name);
12494 }
12495 else
12496 part_die->has_pc_info = 1;
12497 }
12498
12499 return info_ptr;
12500 }
12501
12502 /* Find a cached partial DIE at OFFSET in CU. */
12503
12504 static struct partial_die_info *
12505 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12506 {
12507 struct partial_die_info *lookup_die = NULL;
12508 struct partial_die_info part_die;
12509
12510 part_die.offset = offset;
12511 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12512 offset.sect_off);
12513
12514 return lookup_die;
12515 }
12516
12517 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12518 except in the case of .debug_types DIEs which do not reference
12519 outside their CU (they do however referencing other types via
12520 DW_FORM_ref_sig8). */
12521
12522 static struct partial_die_info *
12523 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
12524 {
12525 struct objfile *objfile = cu->objfile;
12526 struct dwarf2_per_cu_data *per_cu = NULL;
12527 struct partial_die_info *pd = NULL;
12528
12529 if (offset_in_cu_p (&cu->header, offset))
12530 {
12531 pd = find_partial_die_in_comp_unit (offset, cu);
12532 if (pd != NULL)
12533 return pd;
12534 /* We missed recording what we needed.
12535 Load all dies and try again. */
12536 per_cu = cu->per_cu;
12537 }
12538 else
12539 {
12540 /* TUs don't reference other CUs/TUs (except via type signatures). */
12541 if (cu->per_cu->is_debug_types)
12542 {
12543 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12544 " external reference to offset 0x%lx [in module %s].\n"),
12545 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12546 bfd_get_filename (objfile->obfd));
12547 }
12548 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
12549
12550 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12551 load_partial_comp_unit (per_cu);
12552
12553 per_cu->cu->last_used = 0;
12554 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12555 }
12556
12557 /* If we didn't find it, and not all dies have been loaded,
12558 load them all and try again. */
12559
12560 if (pd == NULL && per_cu->load_all_dies == 0)
12561 {
12562 per_cu->load_all_dies = 1;
12563
12564 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12565 THIS_CU->cu may already be in use. So we can't just free it and
12566 replace its DIEs with the ones we read in. Instead, we leave those
12567 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12568 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12569 set. */
12570 load_partial_comp_unit (per_cu);
12571
12572 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12573 }
12574
12575 if (pd == NULL)
12576 internal_error (__FILE__, __LINE__,
12577 _("could not find partial DIE 0x%x "
12578 "in cache [from module %s]\n"),
12579 offset.sect_off, bfd_get_filename (objfile->obfd));
12580 return pd;
12581 }
12582
12583 /* See if we can figure out if the class lives in a namespace. We do
12584 this by looking for a member function; its demangled name will
12585 contain namespace info, if there is any. */
12586
12587 static void
12588 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12589 struct dwarf2_cu *cu)
12590 {
12591 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12592 what template types look like, because the demangler
12593 frequently doesn't give the same name as the debug info. We
12594 could fix this by only using the demangled name to get the
12595 prefix (but see comment in read_structure_type). */
12596
12597 struct partial_die_info *real_pdi;
12598 struct partial_die_info *child_pdi;
12599
12600 /* If this DIE (this DIE's specification, if any) has a parent, then
12601 we should not do this. We'll prepend the parent's fully qualified
12602 name when we create the partial symbol. */
12603
12604 real_pdi = struct_pdi;
12605 while (real_pdi->has_specification)
12606 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
12607
12608 if (real_pdi->die_parent != NULL)
12609 return;
12610
12611 for (child_pdi = struct_pdi->die_child;
12612 child_pdi != NULL;
12613 child_pdi = child_pdi->die_sibling)
12614 {
12615 if (child_pdi->tag == DW_TAG_subprogram
12616 && child_pdi->linkage_name != NULL)
12617 {
12618 char *actual_class_name
12619 = language_class_name_from_physname (cu->language_defn,
12620 child_pdi->linkage_name);
12621 if (actual_class_name != NULL)
12622 {
12623 struct_pdi->name
12624 = obsavestring (actual_class_name,
12625 strlen (actual_class_name),
12626 &cu->objfile->objfile_obstack);
12627 xfree (actual_class_name);
12628 }
12629 break;
12630 }
12631 }
12632 }
12633
12634 /* Adjust PART_DIE before generating a symbol for it. This function
12635 may set the is_external flag or change the DIE's name. */
12636
12637 static void
12638 fixup_partial_die (struct partial_die_info *part_die,
12639 struct dwarf2_cu *cu)
12640 {
12641 /* Once we've fixed up a die, there's no point in doing so again.
12642 This also avoids a memory leak if we were to call
12643 guess_partial_die_structure_name multiple times. */
12644 if (part_die->fixup_called)
12645 return;
12646
12647 /* If we found a reference attribute and the DIE has no name, try
12648 to find a name in the referred to DIE. */
12649
12650 if (part_die->name == NULL && part_die->has_specification)
12651 {
12652 struct partial_die_info *spec_die;
12653
12654 spec_die = find_partial_die (part_die->spec_offset, cu);
12655
12656 fixup_partial_die (spec_die, cu);
12657
12658 if (spec_die->name)
12659 {
12660 part_die->name = spec_die->name;
12661
12662 /* Copy DW_AT_external attribute if it is set. */
12663 if (spec_die->is_external)
12664 part_die->is_external = spec_die->is_external;
12665 }
12666 }
12667
12668 /* Set default names for some unnamed DIEs. */
12669
12670 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12671 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12672
12673 /* If there is no parent die to provide a namespace, and there are
12674 children, see if we can determine the namespace from their linkage
12675 name. */
12676 if (cu->language == language_cplus
12677 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12678 && part_die->die_parent == NULL
12679 && part_die->has_children
12680 && (part_die->tag == DW_TAG_class_type
12681 || part_die->tag == DW_TAG_structure_type
12682 || part_die->tag == DW_TAG_union_type))
12683 guess_partial_die_structure_name (part_die, cu);
12684
12685 /* GCC might emit a nameless struct or union that has a linkage
12686 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12687 if (part_die->name == NULL
12688 && (part_die->tag == DW_TAG_class_type
12689 || part_die->tag == DW_TAG_interface_type
12690 || part_die->tag == DW_TAG_structure_type
12691 || part_die->tag == DW_TAG_union_type)
12692 && part_die->linkage_name != NULL)
12693 {
12694 char *demangled;
12695
12696 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
12697 if (demangled)
12698 {
12699 const char *base;
12700
12701 /* Strip any leading namespaces/classes, keep only the base name.
12702 DW_AT_name for named DIEs does not contain the prefixes. */
12703 base = strrchr (demangled, ':');
12704 if (base && base > demangled && base[-1] == ':')
12705 base++;
12706 else
12707 base = demangled;
12708
12709 part_die->name = obsavestring (base, strlen (base),
12710 &cu->objfile->objfile_obstack);
12711 xfree (demangled);
12712 }
12713 }
12714
12715 part_die->fixup_called = 1;
12716 }
12717
12718 /* Read an attribute value described by an attribute form. */
12719
12720 static gdb_byte *
12721 read_attribute_value (const struct die_reader_specs *reader,
12722 struct attribute *attr, unsigned form,
12723 gdb_byte *info_ptr)
12724 {
12725 struct dwarf2_cu *cu = reader->cu;
12726 bfd *abfd = reader->abfd;
12727 struct comp_unit_head *cu_header = &cu->header;
12728 unsigned int bytes_read;
12729 struct dwarf_block *blk;
12730
12731 attr->form = form;
12732 switch (form)
12733 {
12734 case DW_FORM_ref_addr:
12735 if (cu->header.version == 2)
12736 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
12737 else
12738 DW_UNSND (attr) = read_offset (abfd, info_ptr,
12739 &cu->header, &bytes_read);
12740 info_ptr += bytes_read;
12741 break;
12742 case DW_FORM_addr:
12743 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
12744 info_ptr += bytes_read;
12745 break;
12746 case DW_FORM_block2:
12747 blk = dwarf_alloc_block (cu);
12748 blk->size = read_2_bytes (abfd, info_ptr);
12749 info_ptr += 2;
12750 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12751 info_ptr += blk->size;
12752 DW_BLOCK (attr) = blk;
12753 break;
12754 case DW_FORM_block4:
12755 blk = dwarf_alloc_block (cu);
12756 blk->size = read_4_bytes (abfd, info_ptr);
12757 info_ptr += 4;
12758 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12759 info_ptr += blk->size;
12760 DW_BLOCK (attr) = blk;
12761 break;
12762 case DW_FORM_data2:
12763 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
12764 info_ptr += 2;
12765 break;
12766 case DW_FORM_data4:
12767 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
12768 info_ptr += 4;
12769 break;
12770 case DW_FORM_data8:
12771 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
12772 info_ptr += 8;
12773 break;
12774 case DW_FORM_sec_offset:
12775 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
12776 info_ptr += bytes_read;
12777 break;
12778 case DW_FORM_string:
12779 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
12780 DW_STRING_IS_CANONICAL (attr) = 0;
12781 info_ptr += bytes_read;
12782 break;
12783 case DW_FORM_strp:
12784 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
12785 &bytes_read);
12786 DW_STRING_IS_CANONICAL (attr) = 0;
12787 info_ptr += bytes_read;
12788 break;
12789 case DW_FORM_exprloc:
12790 case DW_FORM_block:
12791 blk = dwarf_alloc_block (cu);
12792 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12793 info_ptr += bytes_read;
12794 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12795 info_ptr += blk->size;
12796 DW_BLOCK (attr) = blk;
12797 break;
12798 case DW_FORM_block1:
12799 blk = dwarf_alloc_block (cu);
12800 blk->size = read_1_byte (abfd, info_ptr);
12801 info_ptr += 1;
12802 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12803 info_ptr += blk->size;
12804 DW_BLOCK (attr) = blk;
12805 break;
12806 case DW_FORM_data1:
12807 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12808 info_ptr += 1;
12809 break;
12810 case DW_FORM_flag:
12811 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12812 info_ptr += 1;
12813 break;
12814 case DW_FORM_flag_present:
12815 DW_UNSND (attr) = 1;
12816 break;
12817 case DW_FORM_sdata:
12818 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
12819 info_ptr += bytes_read;
12820 break;
12821 case DW_FORM_udata:
12822 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12823 info_ptr += bytes_read;
12824 break;
12825 case DW_FORM_ref1:
12826 DW_UNSND (attr) = (cu->header.offset.sect_off
12827 + read_1_byte (abfd, info_ptr));
12828 info_ptr += 1;
12829 break;
12830 case DW_FORM_ref2:
12831 DW_UNSND (attr) = (cu->header.offset.sect_off
12832 + read_2_bytes (abfd, info_ptr));
12833 info_ptr += 2;
12834 break;
12835 case DW_FORM_ref4:
12836 DW_UNSND (attr) = (cu->header.offset.sect_off
12837 + read_4_bytes (abfd, info_ptr));
12838 info_ptr += 4;
12839 break;
12840 case DW_FORM_ref8:
12841 DW_UNSND (attr) = (cu->header.offset.sect_off
12842 + read_8_bytes (abfd, info_ptr));
12843 info_ptr += 8;
12844 break;
12845 case DW_FORM_ref_sig8:
12846 /* Convert the signature to something we can record in DW_UNSND
12847 for later lookup.
12848 NOTE: This is NULL if the type wasn't found. */
12849 DW_SIGNATURED_TYPE (attr) =
12850 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
12851 info_ptr += 8;
12852 break;
12853 case DW_FORM_ref_udata:
12854 DW_UNSND (attr) = (cu->header.offset.sect_off
12855 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
12856 info_ptr += bytes_read;
12857 break;
12858 case DW_FORM_indirect:
12859 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12860 info_ptr += bytes_read;
12861 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
12862 break;
12863 case DW_FORM_GNU_addr_index:
12864 if (reader->dwo_file == NULL)
12865 {
12866 /* For now flag a hard error.
12867 Later we can turn this into a complaint. */
12868 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12869 dwarf_form_name (form),
12870 bfd_get_filename (abfd));
12871 }
12872 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12873 info_ptr += bytes_read;
12874 break;
12875 case DW_FORM_GNU_str_index:
12876 if (reader->dwo_file == NULL)
12877 {
12878 /* For now flag a hard error.
12879 Later we can turn this into a complaint if warranted. */
12880 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12881 dwarf_form_name (form),
12882 bfd_get_filename (abfd));
12883 }
12884 {
12885 ULONGEST str_index =
12886 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12887
12888 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12889 DW_STRING_IS_CANONICAL (attr) = 0;
12890 info_ptr += bytes_read;
12891 }
12892 break;
12893 default:
12894 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
12895 dwarf_form_name (form),
12896 bfd_get_filename (abfd));
12897 }
12898
12899 /* We have seen instances where the compiler tried to emit a byte
12900 size attribute of -1 which ended up being encoded as an unsigned
12901 0xffffffff. Although 0xffffffff is technically a valid size value,
12902 an object of this size seems pretty unlikely so we can relatively
12903 safely treat these cases as if the size attribute was invalid and
12904 treat them as zero by default. */
12905 if (attr->name == DW_AT_byte_size
12906 && form == DW_FORM_data4
12907 && DW_UNSND (attr) >= 0xffffffff)
12908 {
12909 complaint
12910 (&symfile_complaints,
12911 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12912 hex_string (DW_UNSND (attr)));
12913 DW_UNSND (attr) = 0;
12914 }
12915
12916 return info_ptr;
12917 }
12918
12919 /* Read an attribute described by an abbreviated attribute. */
12920
12921 static gdb_byte *
12922 read_attribute (const struct die_reader_specs *reader,
12923 struct attribute *attr, struct attr_abbrev *abbrev,
12924 gdb_byte *info_ptr)
12925 {
12926 attr->name = abbrev->name;
12927 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
12928 }
12929
12930 /* Read dwarf information from a buffer. */
12931
12932 static unsigned int
12933 read_1_byte (bfd *abfd, gdb_byte *buf)
12934 {
12935 return bfd_get_8 (abfd, buf);
12936 }
12937
12938 static int
12939 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
12940 {
12941 return bfd_get_signed_8 (abfd, buf);
12942 }
12943
12944 static unsigned int
12945 read_2_bytes (bfd *abfd, gdb_byte *buf)
12946 {
12947 return bfd_get_16 (abfd, buf);
12948 }
12949
12950 static int
12951 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12952 {
12953 return bfd_get_signed_16 (abfd, buf);
12954 }
12955
12956 static unsigned int
12957 read_4_bytes (bfd *abfd, gdb_byte *buf)
12958 {
12959 return bfd_get_32 (abfd, buf);
12960 }
12961
12962 static int
12963 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12964 {
12965 return bfd_get_signed_32 (abfd, buf);
12966 }
12967
12968 static ULONGEST
12969 read_8_bytes (bfd *abfd, gdb_byte *buf)
12970 {
12971 return bfd_get_64 (abfd, buf);
12972 }
12973
12974 static CORE_ADDR
12975 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
12976 unsigned int *bytes_read)
12977 {
12978 struct comp_unit_head *cu_header = &cu->header;
12979 CORE_ADDR retval = 0;
12980
12981 if (cu_header->signed_addr_p)
12982 {
12983 switch (cu_header->addr_size)
12984 {
12985 case 2:
12986 retval = bfd_get_signed_16 (abfd, buf);
12987 break;
12988 case 4:
12989 retval = bfd_get_signed_32 (abfd, buf);
12990 break;
12991 case 8:
12992 retval = bfd_get_signed_64 (abfd, buf);
12993 break;
12994 default:
12995 internal_error (__FILE__, __LINE__,
12996 _("read_address: bad switch, signed [in module %s]"),
12997 bfd_get_filename (abfd));
12998 }
12999 }
13000 else
13001 {
13002 switch (cu_header->addr_size)
13003 {
13004 case 2:
13005 retval = bfd_get_16 (abfd, buf);
13006 break;
13007 case 4:
13008 retval = bfd_get_32 (abfd, buf);
13009 break;
13010 case 8:
13011 retval = bfd_get_64 (abfd, buf);
13012 break;
13013 default:
13014 internal_error (__FILE__, __LINE__,
13015 _("read_address: bad switch, "
13016 "unsigned [in module %s]"),
13017 bfd_get_filename (abfd));
13018 }
13019 }
13020
13021 *bytes_read = cu_header->addr_size;
13022 return retval;
13023 }
13024
13025 /* Read the initial length from a section. The (draft) DWARF 3
13026 specification allows the initial length to take up either 4 bytes
13027 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13028 bytes describe the length and all offsets will be 8 bytes in length
13029 instead of 4.
13030
13031 An older, non-standard 64-bit format is also handled by this
13032 function. The older format in question stores the initial length
13033 as an 8-byte quantity without an escape value. Lengths greater
13034 than 2^32 aren't very common which means that the initial 4 bytes
13035 is almost always zero. Since a length value of zero doesn't make
13036 sense for the 32-bit format, this initial zero can be considered to
13037 be an escape value which indicates the presence of the older 64-bit
13038 format. As written, the code can't detect (old format) lengths
13039 greater than 4GB. If it becomes necessary to handle lengths
13040 somewhat larger than 4GB, we could allow other small values (such
13041 as the non-sensical values of 1, 2, and 3) to also be used as
13042 escape values indicating the presence of the old format.
13043
13044 The value returned via bytes_read should be used to increment the
13045 relevant pointer after calling read_initial_length().
13046
13047 [ Note: read_initial_length() and read_offset() are based on the
13048 document entitled "DWARF Debugging Information Format", revision
13049 3, draft 8, dated November 19, 2001. This document was obtained
13050 from:
13051
13052 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13053
13054 This document is only a draft and is subject to change. (So beware.)
13055
13056 Details regarding the older, non-standard 64-bit format were
13057 determined empirically by examining 64-bit ELF files produced by
13058 the SGI toolchain on an IRIX 6.5 machine.
13059
13060 - Kevin, July 16, 2002
13061 ] */
13062
13063 static LONGEST
13064 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13065 {
13066 LONGEST length = bfd_get_32 (abfd, buf);
13067
13068 if (length == 0xffffffff)
13069 {
13070 length = bfd_get_64 (abfd, buf + 4);
13071 *bytes_read = 12;
13072 }
13073 else if (length == 0)
13074 {
13075 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
13076 length = bfd_get_64 (abfd, buf);
13077 *bytes_read = 8;
13078 }
13079 else
13080 {
13081 *bytes_read = 4;
13082 }
13083
13084 return length;
13085 }
13086
13087 /* Cover function for read_initial_length.
13088 Returns the length of the object at BUF, and stores the size of the
13089 initial length in *BYTES_READ and stores the size that offsets will be in
13090 *OFFSET_SIZE.
13091 If the initial length size is not equivalent to that specified in
13092 CU_HEADER then issue a complaint.
13093 This is useful when reading non-comp-unit headers. */
13094
13095 static LONGEST
13096 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13097 const struct comp_unit_head *cu_header,
13098 unsigned int *bytes_read,
13099 unsigned int *offset_size)
13100 {
13101 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13102
13103 gdb_assert (cu_header->initial_length_size == 4
13104 || cu_header->initial_length_size == 8
13105 || cu_header->initial_length_size == 12);
13106
13107 if (cu_header->initial_length_size != *bytes_read)
13108 complaint (&symfile_complaints,
13109 _("intermixed 32-bit and 64-bit DWARF sections"));
13110
13111 *offset_size = (*bytes_read == 4) ? 4 : 8;
13112 return length;
13113 }
13114
13115 /* Read an offset from the data stream. The size of the offset is
13116 given by cu_header->offset_size. */
13117
13118 static LONGEST
13119 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13120 unsigned int *bytes_read)
13121 {
13122 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13123
13124 *bytes_read = cu_header->offset_size;
13125 return offset;
13126 }
13127
13128 /* Read an offset from the data stream. */
13129
13130 static LONGEST
13131 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13132 {
13133 LONGEST retval = 0;
13134
13135 switch (offset_size)
13136 {
13137 case 4:
13138 retval = bfd_get_32 (abfd, buf);
13139 break;
13140 case 8:
13141 retval = bfd_get_64 (abfd, buf);
13142 break;
13143 default:
13144 internal_error (__FILE__, __LINE__,
13145 _("read_offset_1: bad switch [in module %s]"),
13146 bfd_get_filename (abfd));
13147 }
13148
13149 return retval;
13150 }
13151
13152 static gdb_byte *
13153 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13154 {
13155 /* If the size of a host char is 8 bits, we can return a pointer
13156 to the buffer, otherwise we have to copy the data to a buffer
13157 allocated on the temporary obstack. */
13158 gdb_assert (HOST_CHAR_BIT == 8);
13159 return buf;
13160 }
13161
13162 static char *
13163 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13164 {
13165 /* If the size of a host char is 8 bits, we can return a pointer
13166 to the string, otherwise we have to copy the string to a buffer
13167 allocated on the temporary obstack. */
13168 gdb_assert (HOST_CHAR_BIT == 8);
13169 if (*buf == '\0')
13170 {
13171 *bytes_read_ptr = 1;
13172 return NULL;
13173 }
13174 *bytes_read_ptr = strlen ((char *) buf) + 1;
13175 return (char *) buf;
13176 }
13177
13178 static char *
13179 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13180 {
13181 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13182 if (dwarf2_per_objfile->str.buffer == NULL)
13183 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13184 bfd_get_filename (abfd));
13185 if (str_offset >= dwarf2_per_objfile->str.size)
13186 error (_("DW_FORM_strp pointing outside of "
13187 ".debug_str section [in module %s]"),
13188 bfd_get_filename (abfd));
13189 gdb_assert (HOST_CHAR_BIT == 8);
13190 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13191 return NULL;
13192 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13193 }
13194
13195 static char *
13196 read_indirect_string (bfd *abfd, gdb_byte *buf,
13197 const struct comp_unit_head *cu_header,
13198 unsigned int *bytes_read_ptr)
13199 {
13200 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13201
13202 return read_indirect_string_at_offset (abfd, str_offset);
13203 }
13204
13205 static ULONGEST
13206 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13207 {
13208 ULONGEST result;
13209 unsigned int num_read;
13210 int i, shift;
13211 unsigned char byte;
13212
13213 result = 0;
13214 shift = 0;
13215 num_read = 0;
13216 i = 0;
13217 while (1)
13218 {
13219 byte = bfd_get_8 (abfd, buf);
13220 buf++;
13221 num_read++;
13222 result |= ((ULONGEST) (byte & 127) << shift);
13223 if ((byte & 128) == 0)
13224 {
13225 break;
13226 }
13227 shift += 7;
13228 }
13229 *bytes_read_ptr = num_read;
13230 return result;
13231 }
13232
13233 static LONGEST
13234 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13235 {
13236 LONGEST result;
13237 int i, shift, num_read;
13238 unsigned char byte;
13239
13240 result = 0;
13241 shift = 0;
13242 num_read = 0;
13243 i = 0;
13244 while (1)
13245 {
13246 byte = bfd_get_8 (abfd, buf);
13247 buf++;
13248 num_read++;
13249 result |= ((LONGEST) (byte & 127) << shift);
13250 shift += 7;
13251 if ((byte & 128) == 0)
13252 {
13253 break;
13254 }
13255 }
13256 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13257 result |= -(((LONGEST) 1) << shift);
13258 *bytes_read_ptr = num_read;
13259 return result;
13260 }
13261
13262 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13263 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13264 ADDR_SIZE is the size of addresses from the CU header. */
13265
13266 static CORE_ADDR
13267 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13268 {
13269 struct objfile *objfile = dwarf2_per_objfile->objfile;
13270 bfd *abfd = objfile->obfd;
13271 const gdb_byte *info_ptr;
13272
13273 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13274 if (dwarf2_per_objfile->addr.buffer == NULL)
13275 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13276 objfile->name);
13277 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13278 error (_("DW_FORM_addr_index pointing outside of "
13279 ".debug_addr section [in module %s]"),
13280 objfile->name);
13281 info_ptr = (dwarf2_per_objfile->addr.buffer
13282 + addr_base + addr_index * addr_size);
13283 if (addr_size == 4)
13284 return bfd_get_32 (abfd, info_ptr);
13285 else
13286 return bfd_get_64 (abfd, info_ptr);
13287 }
13288
13289 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13290
13291 static CORE_ADDR
13292 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13293 {
13294 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13295 }
13296
13297 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13298
13299 static CORE_ADDR
13300 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13301 unsigned int *bytes_read)
13302 {
13303 bfd *abfd = cu->objfile->obfd;
13304 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13305
13306 return read_addr_index (cu, addr_index);
13307 }
13308
13309 /* Data structure to pass results from dwarf2_read_addr_index_reader
13310 back to dwarf2_read_addr_index. */
13311
13312 struct dwarf2_read_addr_index_data
13313 {
13314 ULONGEST addr_base;
13315 int addr_size;
13316 };
13317
13318 /* die_reader_func for dwarf2_read_addr_index. */
13319
13320 static void
13321 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13322 gdb_byte *info_ptr,
13323 struct die_info *comp_unit_die,
13324 int has_children,
13325 void *data)
13326 {
13327 struct dwarf2_cu *cu = reader->cu;
13328 struct dwarf2_read_addr_index_data *aidata =
13329 (struct dwarf2_read_addr_index_data *) data;
13330
13331 aidata->addr_base = cu->addr_base;
13332 aidata->addr_size = cu->header.addr_size;
13333 }
13334
13335 /* Given an index in .debug_addr, fetch the value.
13336 NOTE: This can be called during dwarf expression evaluation,
13337 long after the debug information has been read, and thus per_cu->cu
13338 may no longer exist. */
13339
13340 CORE_ADDR
13341 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13342 unsigned int addr_index)
13343 {
13344 struct objfile *objfile = per_cu->objfile;
13345 struct dwarf2_cu *cu = per_cu->cu;
13346 ULONGEST addr_base;
13347 int addr_size;
13348
13349 /* This is intended to be called from outside this file. */
13350 dw2_setup (objfile);
13351
13352 /* We need addr_base and addr_size.
13353 If we don't have PER_CU->cu, we have to get it.
13354 Nasty, but the alternative is storing the needed info in PER_CU,
13355 which at this point doesn't seem justified: it's not clear how frequently
13356 it would get used and it would increase the size of every PER_CU.
13357 Entry points like dwarf2_per_cu_addr_size do a similar thing
13358 so we're not in uncharted territory here.
13359 Alas we need to be a bit more complicated as addr_base is contained
13360 in the DIE.
13361
13362 We don't need to read the entire CU(/TU).
13363 We just need the header and top level die.
13364 IWBN to use the aging mechanism to let us lazily later discard the CU.
13365 See however init_cutu_and_read_dies_simple. */
13366
13367 if (cu != NULL)
13368 {
13369 addr_base = cu->addr_base;
13370 addr_size = cu->header.addr_size;
13371 }
13372 else
13373 {
13374 struct dwarf2_read_addr_index_data aidata;
13375
13376 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13377 &aidata);
13378 addr_base = aidata.addr_base;
13379 addr_size = aidata.addr_size;
13380 }
13381
13382 return read_addr_index_1 (addr_index, addr_base, addr_size);
13383 }
13384
13385 /* Given a DW_AT_str_index, fetch the string. */
13386
13387 static char *
13388 read_str_index (const struct die_reader_specs *reader,
13389 struct dwarf2_cu *cu, ULONGEST str_index)
13390 {
13391 struct objfile *objfile = dwarf2_per_objfile->objfile;
13392 const char *dwo_name = objfile->name;
13393 bfd *abfd = objfile->obfd;
13394 struct dwo_sections *sections = &reader->dwo_file->sections;
13395 gdb_byte *info_ptr;
13396 ULONGEST str_offset;
13397
13398 dwarf2_read_section (objfile, &sections->str);
13399 dwarf2_read_section (objfile, &sections->str_offsets);
13400 if (sections->str.buffer == NULL)
13401 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13402 " in CU at offset 0x%lx [in module %s]"),
13403 (long) cu->header.offset.sect_off, dwo_name);
13404 if (sections->str_offsets.buffer == NULL)
13405 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13406 " in CU at offset 0x%lx [in module %s]"),
13407 (long) cu->header.offset.sect_off, dwo_name);
13408 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13409 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13410 " section in CU at offset 0x%lx [in module %s]"),
13411 (long) cu->header.offset.sect_off, dwo_name);
13412 info_ptr = (sections->str_offsets.buffer
13413 + str_index * cu->header.offset_size);
13414 if (cu->header.offset_size == 4)
13415 str_offset = bfd_get_32 (abfd, info_ptr);
13416 else
13417 str_offset = bfd_get_64 (abfd, info_ptr);
13418 if (str_offset >= sections->str.size)
13419 error (_("Offset from DW_FORM_str_index pointing outside of"
13420 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13421 (long) cu->header.offset.sect_off, dwo_name);
13422 return (char *) (sections->str.buffer + str_offset);
13423 }
13424
13425 /* Return the length of an LEB128 number in BUF. */
13426
13427 static int
13428 leb128_size (const gdb_byte *buf)
13429 {
13430 const gdb_byte *begin = buf;
13431 gdb_byte byte;
13432
13433 while (1)
13434 {
13435 byte = *buf++;
13436 if ((byte & 128) == 0)
13437 return buf - begin;
13438 }
13439 }
13440
13441 static void
13442 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13443 {
13444 switch (lang)
13445 {
13446 case DW_LANG_C89:
13447 case DW_LANG_C99:
13448 case DW_LANG_C:
13449 cu->language = language_c;
13450 break;
13451 case DW_LANG_C_plus_plus:
13452 cu->language = language_cplus;
13453 break;
13454 case DW_LANG_D:
13455 cu->language = language_d;
13456 break;
13457 case DW_LANG_Fortran77:
13458 case DW_LANG_Fortran90:
13459 case DW_LANG_Fortran95:
13460 cu->language = language_fortran;
13461 break;
13462 case DW_LANG_Go:
13463 cu->language = language_go;
13464 break;
13465 case DW_LANG_Mips_Assembler:
13466 cu->language = language_asm;
13467 break;
13468 case DW_LANG_Java:
13469 cu->language = language_java;
13470 break;
13471 case DW_LANG_Ada83:
13472 case DW_LANG_Ada95:
13473 cu->language = language_ada;
13474 break;
13475 case DW_LANG_Modula2:
13476 cu->language = language_m2;
13477 break;
13478 case DW_LANG_Pascal83:
13479 cu->language = language_pascal;
13480 break;
13481 case DW_LANG_ObjC:
13482 cu->language = language_objc;
13483 break;
13484 case DW_LANG_Cobol74:
13485 case DW_LANG_Cobol85:
13486 default:
13487 cu->language = language_minimal;
13488 break;
13489 }
13490 cu->language_defn = language_def (cu->language);
13491 }
13492
13493 /* Return the named attribute or NULL if not there. */
13494
13495 static struct attribute *
13496 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13497 {
13498 for (;;)
13499 {
13500 unsigned int i;
13501 struct attribute *spec = NULL;
13502
13503 for (i = 0; i < die->num_attrs; ++i)
13504 {
13505 if (die->attrs[i].name == name)
13506 return &die->attrs[i];
13507 if (die->attrs[i].name == DW_AT_specification
13508 || die->attrs[i].name == DW_AT_abstract_origin)
13509 spec = &die->attrs[i];
13510 }
13511
13512 if (!spec)
13513 break;
13514
13515 die = follow_die_ref (die, spec, &cu);
13516 }
13517
13518 return NULL;
13519 }
13520
13521 /* Return the named attribute or NULL if not there,
13522 but do not follow DW_AT_specification, etc.
13523 This is for use in contexts where we're reading .debug_types dies.
13524 Following DW_AT_specification, DW_AT_abstract_origin will take us
13525 back up the chain, and we want to go down. */
13526
13527 static struct attribute *
13528 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13529 {
13530 unsigned int i;
13531
13532 for (i = 0; i < die->num_attrs; ++i)
13533 if (die->attrs[i].name == name)
13534 return &die->attrs[i];
13535
13536 return NULL;
13537 }
13538
13539 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13540 and holds a non-zero value. This function should only be used for
13541 DW_FORM_flag or DW_FORM_flag_present attributes. */
13542
13543 static int
13544 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13545 {
13546 struct attribute *attr = dwarf2_attr (die, name, cu);
13547
13548 return (attr && DW_UNSND (attr));
13549 }
13550
13551 static int
13552 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13553 {
13554 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13555 which value is non-zero. However, we have to be careful with
13556 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13557 (via dwarf2_flag_true_p) follows this attribute. So we may
13558 end up accidently finding a declaration attribute that belongs
13559 to a different DIE referenced by the specification attribute,
13560 even though the given DIE does not have a declaration attribute. */
13561 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13562 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13563 }
13564
13565 /* Return the die giving the specification for DIE, if there is
13566 one. *SPEC_CU is the CU containing DIE on input, and the CU
13567 containing the return value on output. If there is no
13568 specification, but there is an abstract origin, that is
13569 returned. */
13570
13571 static struct die_info *
13572 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13573 {
13574 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13575 *spec_cu);
13576
13577 if (spec_attr == NULL)
13578 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13579
13580 if (spec_attr == NULL)
13581 return NULL;
13582 else
13583 return follow_die_ref (die, spec_attr, spec_cu);
13584 }
13585
13586 /* Free the line_header structure *LH, and any arrays and strings it
13587 refers to.
13588 NOTE: This is also used as a "cleanup" function. */
13589
13590 static void
13591 free_line_header (struct line_header *lh)
13592 {
13593 if (lh->standard_opcode_lengths)
13594 xfree (lh->standard_opcode_lengths);
13595
13596 /* Remember that all the lh->file_names[i].name pointers are
13597 pointers into debug_line_buffer, and don't need to be freed. */
13598 if (lh->file_names)
13599 xfree (lh->file_names);
13600
13601 /* Similarly for the include directory names. */
13602 if (lh->include_dirs)
13603 xfree (lh->include_dirs);
13604
13605 xfree (lh);
13606 }
13607
13608 /* Add an entry to LH's include directory table. */
13609
13610 static void
13611 add_include_dir (struct line_header *lh, char *include_dir)
13612 {
13613 /* Grow the array if necessary. */
13614 if (lh->include_dirs_size == 0)
13615 {
13616 lh->include_dirs_size = 1; /* for testing */
13617 lh->include_dirs = xmalloc (lh->include_dirs_size
13618 * sizeof (*lh->include_dirs));
13619 }
13620 else if (lh->num_include_dirs >= lh->include_dirs_size)
13621 {
13622 lh->include_dirs_size *= 2;
13623 lh->include_dirs = xrealloc (lh->include_dirs,
13624 (lh->include_dirs_size
13625 * sizeof (*lh->include_dirs)));
13626 }
13627
13628 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13629 }
13630
13631 /* Add an entry to LH's file name table. */
13632
13633 static void
13634 add_file_name (struct line_header *lh,
13635 char *name,
13636 unsigned int dir_index,
13637 unsigned int mod_time,
13638 unsigned int length)
13639 {
13640 struct file_entry *fe;
13641
13642 /* Grow the array if necessary. */
13643 if (lh->file_names_size == 0)
13644 {
13645 lh->file_names_size = 1; /* for testing */
13646 lh->file_names = xmalloc (lh->file_names_size
13647 * sizeof (*lh->file_names));
13648 }
13649 else if (lh->num_file_names >= lh->file_names_size)
13650 {
13651 lh->file_names_size *= 2;
13652 lh->file_names = xrealloc (lh->file_names,
13653 (lh->file_names_size
13654 * sizeof (*lh->file_names)));
13655 }
13656
13657 fe = &lh->file_names[lh->num_file_names++];
13658 fe->name = name;
13659 fe->dir_index = dir_index;
13660 fe->mod_time = mod_time;
13661 fe->length = length;
13662 fe->included_p = 0;
13663 fe->symtab = NULL;
13664 }
13665
13666 /* Read the statement program header starting at OFFSET in
13667 .debug_line, or .debug_line.dwo. Return a pointer
13668 to a struct line_header, allocated using xmalloc.
13669
13670 NOTE: the strings in the include directory and file name tables of
13671 the returned object point into the dwarf line section buffer,
13672 and must not be freed. */
13673
13674 static struct line_header *
13675 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
13676 {
13677 struct cleanup *back_to;
13678 struct line_header *lh;
13679 gdb_byte *line_ptr;
13680 unsigned int bytes_read, offset_size;
13681 int i;
13682 char *cur_dir, *cur_file;
13683 struct dwarf2_section_info *section;
13684 bfd *abfd;
13685
13686 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
13687 DWO file. */
13688 if (cu->dwo_unit && cu->per_cu->is_debug_types)
13689 section = &cu->dwo_unit->dwo_file->sections.line;
13690 else
13691 section = &dwarf2_per_objfile->line;
13692
13693 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
13694 if (section->buffer == NULL)
13695 {
13696 if (cu->dwo_unit && cu->per_cu->is_debug_types)
13697 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
13698 else
13699 complaint (&symfile_complaints, _("missing .debug_line section"));
13700 return 0;
13701 }
13702
13703 /* We can't do this until we know the section is non-empty.
13704 Only then do we know we have such a section. */
13705 abfd = section->asection->owner;
13706
13707 /* Make sure that at least there's room for the total_length field.
13708 That could be 12 bytes long, but we're just going to fudge that. */
13709 if (offset + 4 >= section->size)
13710 {
13711 dwarf2_statement_list_fits_in_line_number_section_complaint ();
13712 return 0;
13713 }
13714
13715 lh = xmalloc (sizeof (*lh));
13716 memset (lh, 0, sizeof (*lh));
13717 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
13718 (void *) lh);
13719
13720 line_ptr = section->buffer + offset;
13721
13722 /* Read in the header. */
13723 lh->total_length =
13724 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
13725 &bytes_read, &offset_size);
13726 line_ptr += bytes_read;
13727 if (line_ptr + lh->total_length > (section->buffer + section->size))
13728 {
13729 dwarf2_statement_list_fits_in_line_number_section_complaint ();
13730 return 0;
13731 }
13732 lh->statement_program_end = line_ptr + lh->total_length;
13733 lh->version = read_2_bytes (abfd, line_ptr);
13734 line_ptr += 2;
13735 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
13736 line_ptr += offset_size;
13737 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
13738 line_ptr += 1;
13739 if (lh->version >= 4)
13740 {
13741 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
13742 line_ptr += 1;
13743 }
13744 else
13745 lh->maximum_ops_per_instruction = 1;
13746
13747 if (lh->maximum_ops_per_instruction == 0)
13748 {
13749 lh->maximum_ops_per_instruction = 1;
13750 complaint (&symfile_complaints,
13751 _("invalid maximum_ops_per_instruction "
13752 "in `.debug_line' section"));
13753 }
13754
13755 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
13756 line_ptr += 1;
13757 lh->line_base = read_1_signed_byte (abfd, line_ptr);
13758 line_ptr += 1;
13759 lh->line_range = read_1_byte (abfd, line_ptr);
13760 line_ptr += 1;
13761 lh->opcode_base = read_1_byte (abfd, line_ptr);
13762 line_ptr += 1;
13763 lh->standard_opcode_lengths
13764 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
13765
13766 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
13767 for (i = 1; i < lh->opcode_base; ++i)
13768 {
13769 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
13770 line_ptr += 1;
13771 }
13772
13773 /* Read directory table. */
13774 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
13775 {
13776 line_ptr += bytes_read;
13777 add_include_dir (lh, cur_dir);
13778 }
13779 line_ptr += bytes_read;
13780
13781 /* Read file name table. */
13782 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
13783 {
13784 unsigned int dir_index, mod_time, length;
13785
13786 line_ptr += bytes_read;
13787 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13788 line_ptr += bytes_read;
13789 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13790 line_ptr += bytes_read;
13791 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13792 line_ptr += bytes_read;
13793
13794 add_file_name (lh, cur_file, dir_index, mod_time, length);
13795 }
13796 line_ptr += bytes_read;
13797 lh->statement_program_start = line_ptr;
13798
13799 if (line_ptr > (section->buffer + section->size))
13800 complaint (&symfile_complaints,
13801 _("line number info header doesn't "
13802 "fit in `.debug_line' section"));
13803
13804 discard_cleanups (back_to);
13805 return lh;
13806 }
13807
13808 /* Subroutine of dwarf_decode_lines to simplify it.
13809 Return the file name of the psymtab for included file FILE_INDEX
13810 in line header LH of PST.
13811 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13812 If space for the result is malloc'd, it will be freed by a cleanup.
13813 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
13814
13815 static char *
13816 psymtab_include_file_name (const struct line_header *lh, int file_index,
13817 const struct partial_symtab *pst,
13818 const char *comp_dir)
13819 {
13820 const struct file_entry fe = lh->file_names [file_index];
13821 char *include_name = fe.name;
13822 char *include_name_to_compare = include_name;
13823 char *dir_name = NULL;
13824 const char *pst_filename;
13825 char *copied_name = NULL;
13826 int file_is_pst;
13827
13828 if (fe.dir_index)
13829 dir_name = lh->include_dirs[fe.dir_index - 1];
13830
13831 if (!IS_ABSOLUTE_PATH (include_name)
13832 && (dir_name != NULL || comp_dir != NULL))
13833 {
13834 /* Avoid creating a duplicate psymtab for PST.
13835 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13836 Before we do the comparison, however, we need to account
13837 for DIR_NAME and COMP_DIR.
13838 First prepend dir_name (if non-NULL). If we still don't
13839 have an absolute path prepend comp_dir (if non-NULL).
13840 However, the directory we record in the include-file's
13841 psymtab does not contain COMP_DIR (to match the
13842 corresponding symtab(s)).
13843
13844 Example:
13845
13846 bash$ cd /tmp
13847 bash$ gcc -g ./hello.c
13848 include_name = "hello.c"
13849 dir_name = "."
13850 DW_AT_comp_dir = comp_dir = "/tmp"
13851 DW_AT_name = "./hello.c" */
13852
13853 if (dir_name != NULL)
13854 {
13855 include_name = concat (dir_name, SLASH_STRING,
13856 include_name, (char *)NULL);
13857 include_name_to_compare = include_name;
13858 make_cleanup (xfree, include_name);
13859 }
13860 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13861 {
13862 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13863 include_name, (char *)NULL);
13864 }
13865 }
13866
13867 pst_filename = pst->filename;
13868 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13869 {
13870 copied_name = concat (pst->dirname, SLASH_STRING,
13871 pst_filename, (char *)NULL);
13872 pst_filename = copied_name;
13873 }
13874
13875 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
13876
13877 if (include_name_to_compare != include_name)
13878 xfree (include_name_to_compare);
13879 if (copied_name != NULL)
13880 xfree (copied_name);
13881
13882 if (file_is_pst)
13883 return NULL;
13884 return include_name;
13885 }
13886
13887 /* Ignore this record_line request. */
13888
13889 static void
13890 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13891 {
13892 return;
13893 }
13894
13895 /* Subroutine of dwarf_decode_lines to simplify it.
13896 Process the line number information in LH. */
13897
13898 static void
13899 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13900 struct dwarf2_cu *cu, struct partial_symtab *pst)
13901 {
13902 gdb_byte *line_ptr, *extended_end;
13903 gdb_byte *line_end;
13904 unsigned int bytes_read, extended_len;
13905 unsigned char op_code, extended_op, adj_opcode;
13906 CORE_ADDR baseaddr;
13907 struct objfile *objfile = cu->objfile;
13908 bfd *abfd = objfile->obfd;
13909 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13910 const int decode_for_pst_p = (pst != NULL);
13911 struct subfile *last_subfile = NULL;
13912 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13913 = record_line;
13914
13915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13916
13917 line_ptr = lh->statement_program_start;
13918 line_end = lh->statement_program_end;
13919
13920 /* Read the statement sequences until there's nothing left. */
13921 while (line_ptr < line_end)
13922 {
13923 /* state machine registers */
13924 CORE_ADDR address = 0;
13925 unsigned int file = 1;
13926 unsigned int line = 1;
13927 unsigned int column = 0;
13928 int is_stmt = lh->default_is_stmt;
13929 int basic_block = 0;
13930 int end_sequence = 0;
13931 CORE_ADDR addr;
13932 unsigned char op_index = 0;
13933
13934 if (!decode_for_pst_p && lh->num_file_names >= file)
13935 {
13936 /* Start a subfile for the current file of the state machine. */
13937 /* lh->include_dirs and lh->file_names are 0-based, but the
13938 directory and file name numbers in the statement program
13939 are 1-based. */
13940 struct file_entry *fe = &lh->file_names[file - 1];
13941 char *dir = NULL;
13942
13943 if (fe->dir_index)
13944 dir = lh->include_dirs[fe->dir_index - 1];
13945
13946 dwarf2_start_subfile (fe->name, dir, comp_dir);
13947 }
13948
13949 /* Decode the table. */
13950 while (!end_sequence)
13951 {
13952 op_code = read_1_byte (abfd, line_ptr);
13953 line_ptr += 1;
13954 if (line_ptr > line_end)
13955 {
13956 dwarf2_debug_line_missing_end_sequence_complaint ();
13957 break;
13958 }
13959
13960 if (op_code >= lh->opcode_base)
13961 {
13962 /* Special operand. */
13963 adj_opcode = op_code - lh->opcode_base;
13964 address += (((op_index + (adj_opcode / lh->line_range))
13965 / lh->maximum_ops_per_instruction)
13966 * lh->minimum_instruction_length);
13967 op_index = ((op_index + (adj_opcode / lh->line_range))
13968 % lh->maximum_ops_per_instruction);
13969 line += lh->line_base + (adj_opcode % lh->line_range);
13970 if (lh->num_file_names < file || file == 0)
13971 dwarf2_debug_line_missing_file_complaint ();
13972 /* For now we ignore lines not starting on an
13973 instruction boundary. */
13974 else if (op_index == 0)
13975 {
13976 lh->file_names[file - 1].included_p = 1;
13977 if (!decode_for_pst_p && is_stmt)
13978 {
13979 if (last_subfile != current_subfile)
13980 {
13981 addr = gdbarch_addr_bits_remove (gdbarch, address);
13982 if (last_subfile)
13983 (*p_record_line) (last_subfile, 0, addr);
13984 last_subfile = current_subfile;
13985 }
13986 /* Append row to matrix using current values. */
13987 addr = gdbarch_addr_bits_remove (gdbarch, address);
13988 (*p_record_line) (current_subfile, line, addr);
13989 }
13990 }
13991 basic_block = 0;
13992 }
13993 else switch (op_code)
13994 {
13995 case DW_LNS_extended_op:
13996 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13997 &bytes_read);
13998 line_ptr += bytes_read;
13999 extended_end = line_ptr + extended_len;
14000 extended_op = read_1_byte (abfd, line_ptr);
14001 line_ptr += 1;
14002 switch (extended_op)
14003 {
14004 case DW_LNE_end_sequence:
14005 p_record_line = record_line;
14006 end_sequence = 1;
14007 break;
14008 case DW_LNE_set_address:
14009 address = read_address (abfd, line_ptr, cu, &bytes_read);
14010
14011 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14012 {
14013 /* This line table is for a function which has been
14014 GCd by the linker. Ignore it. PR gdb/12528 */
14015
14016 long line_offset
14017 = line_ptr - dwarf2_per_objfile->line.buffer;
14018
14019 complaint (&symfile_complaints,
14020 _(".debug_line address at offset 0x%lx is 0 "
14021 "[in module %s]"),
14022 line_offset, objfile->name);
14023 p_record_line = noop_record_line;
14024 }
14025
14026 op_index = 0;
14027 line_ptr += bytes_read;
14028 address += baseaddr;
14029 break;
14030 case DW_LNE_define_file:
14031 {
14032 char *cur_file;
14033 unsigned int dir_index, mod_time, length;
14034
14035 cur_file = read_direct_string (abfd, line_ptr,
14036 &bytes_read);
14037 line_ptr += bytes_read;
14038 dir_index =
14039 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14040 line_ptr += bytes_read;
14041 mod_time =
14042 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14043 line_ptr += bytes_read;
14044 length =
14045 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14046 line_ptr += bytes_read;
14047 add_file_name (lh, cur_file, dir_index, mod_time, length);
14048 }
14049 break;
14050 case DW_LNE_set_discriminator:
14051 /* The discriminator is not interesting to the debugger;
14052 just ignore it. */
14053 line_ptr = extended_end;
14054 break;
14055 default:
14056 complaint (&symfile_complaints,
14057 _("mangled .debug_line section"));
14058 return;
14059 }
14060 /* Make sure that we parsed the extended op correctly. If e.g.
14061 we expected a different address size than the producer used,
14062 we may have read the wrong number of bytes. */
14063 if (line_ptr != extended_end)
14064 {
14065 complaint (&symfile_complaints,
14066 _("mangled .debug_line section"));
14067 return;
14068 }
14069 break;
14070 case DW_LNS_copy:
14071 if (lh->num_file_names < file || file == 0)
14072 dwarf2_debug_line_missing_file_complaint ();
14073 else
14074 {
14075 lh->file_names[file - 1].included_p = 1;
14076 if (!decode_for_pst_p && is_stmt)
14077 {
14078 if (last_subfile != current_subfile)
14079 {
14080 addr = gdbarch_addr_bits_remove (gdbarch, address);
14081 if (last_subfile)
14082 (*p_record_line) (last_subfile, 0, addr);
14083 last_subfile = current_subfile;
14084 }
14085 addr = gdbarch_addr_bits_remove (gdbarch, address);
14086 (*p_record_line) (current_subfile, line, addr);
14087 }
14088 }
14089 basic_block = 0;
14090 break;
14091 case DW_LNS_advance_pc:
14092 {
14093 CORE_ADDR adjust
14094 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14095
14096 address += (((op_index + adjust)
14097 / lh->maximum_ops_per_instruction)
14098 * lh->minimum_instruction_length);
14099 op_index = ((op_index + adjust)
14100 % lh->maximum_ops_per_instruction);
14101 line_ptr += bytes_read;
14102 }
14103 break;
14104 case DW_LNS_advance_line:
14105 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14106 line_ptr += bytes_read;
14107 break;
14108 case DW_LNS_set_file:
14109 {
14110 /* The arrays lh->include_dirs and lh->file_names are
14111 0-based, but the directory and file name numbers in
14112 the statement program are 1-based. */
14113 struct file_entry *fe;
14114 char *dir = NULL;
14115
14116 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14117 line_ptr += bytes_read;
14118 if (lh->num_file_names < file || file == 0)
14119 dwarf2_debug_line_missing_file_complaint ();
14120 else
14121 {
14122 fe = &lh->file_names[file - 1];
14123 if (fe->dir_index)
14124 dir = lh->include_dirs[fe->dir_index - 1];
14125 if (!decode_for_pst_p)
14126 {
14127 last_subfile = current_subfile;
14128 dwarf2_start_subfile (fe->name, dir, comp_dir);
14129 }
14130 }
14131 }
14132 break;
14133 case DW_LNS_set_column:
14134 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14135 line_ptr += bytes_read;
14136 break;
14137 case DW_LNS_negate_stmt:
14138 is_stmt = (!is_stmt);
14139 break;
14140 case DW_LNS_set_basic_block:
14141 basic_block = 1;
14142 break;
14143 /* Add to the address register of the state machine the
14144 address increment value corresponding to special opcode
14145 255. I.e., this value is scaled by the minimum
14146 instruction length since special opcode 255 would have
14147 scaled the increment. */
14148 case DW_LNS_const_add_pc:
14149 {
14150 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14151
14152 address += (((op_index + adjust)
14153 / lh->maximum_ops_per_instruction)
14154 * lh->minimum_instruction_length);
14155 op_index = ((op_index + adjust)
14156 % lh->maximum_ops_per_instruction);
14157 }
14158 break;
14159 case DW_LNS_fixed_advance_pc:
14160 address += read_2_bytes (abfd, line_ptr);
14161 op_index = 0;
14162 line_ptr += 2;
14163 break;
14164 default:
14165 {
14166 /* Unknown standard opcode, ignore it. */
14167 int i;
14168
14169 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14170 {
14171 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14172 line_ptr += bytes_read;
14173 }
14174 }
14175 }
14176 }
14177 if (lh->num_file_names < file || file == 0)
14178 dwarf2_debug_line_missing_file_complaint ();
14179 else
14180 {
14181 lh->file_names[file - 1].included_p = 1;
14182 if (!decode_for_pst_p)
14183 {
14184 addr = gdbarch_addr_bits_remove (gdbarch, address);
14185 (*p_record_line) (current_subfile, 0, addr);
14186 }
14187 }
14188 }
14189 }
14190
14191 /* Decode the Line Number Program (LNP) for the given line_header
14192 structure and CU. The actual information extracted and the type
14193 of structures created from the LNP depends on the value of PST.
14194
14195 1. If PST is NULL, then this procedure uses the data from the program
14196 to create all necessary symbol tables, and their linetables.
14197
14198 2. If PST is not NULL, this procedure reads the program to determine
14199 the list of files included by the unit represented by PST, and
14200 builds all the associated partial symbol tables.
14201
14202 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14203 It is used for relative paths in the line table.
14204 NOTE: When processing partial symtabs (pst != NULL),
14205 comp_dir == pst->dirname.
14206
14207 NOTE: It is important that psymtabs have the same file name (via strcmp)
14208 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14209 symtab we don't use it in the name of the psymtabs we create.
14210 E.g. expand_line_sal requires this when finding psymtabs to expand.
14211 A good testcase for this is mb-inline.exp. */
14212
14213 static void
14214 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14215 struct dwarf2_cu *cu, struct partial_symtab *pst,
14216 int want_line_info)
14217 {
14218 struct objfile *objfile = cu->objfile;
14219 const int decode_for_pst_p = (pst != NULL);
14220 struct subfile *first_subfile = current_subfile;
14221
14222 if (want_line_info)
14223 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14224
14225 if (decode_for_pst_p)
14226 {
14227 int file_index;
14228
14229 /* Now that we're done scanning the Line Header Program, we can
14230 create the psymtab of each included file. */
14231 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14232 if (lh->file_names[file_index].included_p == 1)
14233 {
14234 char *include_name =
14235 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14236 if (include_name != NULL)
14237 dwarf2_create_include_psymtab (include_name, pst, objfile);
14238 }
14239 }
14240 else
14241 {
14242 /* Make sure a symtab is created for every file, even files
14243 which contain only variables (i.e. no code with associated
14244 line numbers). */
14245 int i;
14246
14247 for (i = 0; i < lh->num_file_names; i++)
14248 {
14249 char *dir = NULL;
14250 struct file_entry *fe;
14251
14252 fe = &lh->file_names[i];
14253 if (fe->dir_index)
14254 dir = lh->include_dirs[fe->dir_index - 1];
14255 dwarf2_start_subfile (fe->name, dir, comp_dir);
14256
14257 /* Skip the main file; we don't need it, and it must be
14258 allocated last, so that it will show up before the
14259 non-primary symtabs in the objfile's symtab list. */
14260 if (current_subfile == first_subfile)
14261 continue;
14262
14263 if (current_subfile->symtab == NULL)
14264 current_subfile->symtab = allocate_symtab (current_subfile->name,
14265 objfile);
14266 fe->symtab = current_subfile->symtab;
14267 }
14268 }
14269 }
14270
14271 /* Start a subfile for DWARF. FILENAME is the name of the file and
14272 DIRNAME the name of the source directory which contains FILENAME
14273 or NULL if not known. COMP_DIR is the compilation directory for the
14274 linetable's compilation unit or NULL if not known.
14275 This routine tries to keep line numbers from identical absolute and
14276 relative file names in a common subfile.
14277
14278 Using the `list' example from the GDB testsuite, which resides in
14279 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14280 of /srcdir/list0.c yields the following debugging information for list0.c:
14281
14282 DW_AT_name: /srcdir/list0.c
14283 DW_AT_comp_dir: /compdir
14284 files.files[0].name: list0.h
14285 files.files[0].dir: /srcdir
14286 files.files[1].name: list0.c
14287 files.files[1].dir: /srcdir
14288
14289 The line number information for list0.c has to end up in a single
14290 subfile, so that `break /srcdir/list0.c:1' works as expected.
14291 start_subfile will ensure that this happens provided that we pass the
14292 concatenation of files.files[1].dir and files.files[1].name as the
14293 subfile's name. */
14294
14295 static void
14296 dwarf2_start_subfile (char *filename, const char *dirname,
14297 const char *comp_dir)
14298 {
14299 char *fullname;
14300
14301 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14302 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14303 second argument to start_subfile. To be consistent, we do the
14304 same here. In order not to lose the line information directory,
14305 we concatenate it to the filename when it makes sense.
14306 Note that the Dwarf3 standard says (speaking of filenames in line
14307 information): ``The directory index is ignored for file names
14308 that represent full path names''. Thus ignoring dirname in the
14309 `else' branch below isn't an issue. */
14310
14311 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14312 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14313 else
14314 fullname = filename;
14315
14316 start_subfile (fullname, comp_dir);
14317
14318 if (fullname != filename)
14319 xfree (fullname);
14320 }
14321
14322 /* Start a symtab for DWARF.
14323 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14324
14325 static void
14326 dwarf2_start_symtab (struct dwarf2_cu *cu,
14327 char *name, char *comp_dir, CORE_ADDR low_pc)
14328 {
14329 start_symtab (name, comp_dir, low_pc);
14330 record_debugformat ("DWARF 2");
14331 record_producer (cu->producer);
14332
14333 /* We assume that we're processing GCC output. */
14334 processing_gcc_compilation = 2;
14335
14336 processing_has_namespace_info = 0;
14337 }
14338
14339 static void
14340 var_decode_location (struct attribute *attr, struct symbol *sym,
14341 struct dwarf2_cu *cu)
14342 {
14343 struct objfile *objfile = cu->objfile;
14344 struct comp_unit_head *cu_header = &cu->header;
14345
14346 /* NOTE drow/2003-01-30: There used to be a comment and some special
14347 code here to turn a symbol with DW_AT_external and a
14348 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14349 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14350 with some versions of binutils) where shared libraries could have
14351 relocations against symbols in their debug information - the
14352 minimal symbol would have the right address, but the debug info
14353 would not. It's no longer necessary, because we will explicitly
14354 apply relocations when we read in the debug information now. */
14355
14356 /* A DW_AT_location attribute with no contents indicates that a
14357 variable has been optimized away. */
14358 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14359 {
14360 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14361 return;
14362 }
14363
14364 /* Handle one degenerate form of location expression specially, to
14365 preserve GDB's previous behavior when section offsets are
14366 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14367 then mark this symbol as LOC_STATIC. */
14368
14369 if (attr_form_is_block (attr)
14370 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14371 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14372 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14373 && (DW_BLOCK (attr)->size
14374 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14375 {
14376 unsigned int dummy;
14377
14378 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14379 SYMBOL_VALUE_ADDRESS (sym) =
14380 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14381 else
14382 SYMBOL_VALUE_ADDRESS (sym) =
14383 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14384 SYMBOL_CLASS (sym) = LOC_STATIC;
14385 fixup_symbol_section (sym, objfile);
14386 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14387 SYMBOL_SECTION (sym));
14388 return;
14389 }
14390
14391 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14392 expression evaluator, and use LOC_COMPUTED only when necessary
14393 (i.e. when the value of a register or memory location is
14394 referenced, or a thread-local block, etc.). Then again, it might
14395 not be worthwhile. I'm assuming that it isn't unless performance
14396 or memory numbers show me otherwise. */
14397
14398 dwarf2_symbol_mark_computed (attr, sym, cu);
14399 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14400
14401 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14402 cu->has_loclist = 1;
14403 }
14404
14405 /* Given a pointer to a DWARF information entry, figure out if we need
14406 to make a symbol table entry for it, and if so, create a new entry
14407 and return a pointer to it.
14408 If TYPE is NULL, determine symbol type from the die, otherwise
14409 used the passed type.
14410 If SPACE is not NULL, use it to hold the new symbol. If it is
14411 NULL, allocate a new symbol on the objfile's obstack. */
14412
14413 static struct symbol *
14414 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14415 struct symbol *space)
14416 {
14417 struct objfile *objfile = cu->objfile;
14418 struct symbol *sym = NULL;
14419 char *name;
14420 struct attribute *attr = NULL;
14421 struct attribute *attr2 = NULL;
14422 CORE_ADDR baseaddr;
14423 struct pending **list_to_add = NULL;
14424
14425 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14426
14427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14428
14429 name = dwarf2_name (die, cu);
14430 if (name)
14431 {
14432 const char *linkagename;
14433 int suppress_add = 0;
14434
14435 if (space)
14436 sym = space;
14437 else
14438 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14439 OBJSTAT (objfile, n_syms++);
14440
14441 /* Cache this symbol's name and the name's demangled form (if any). */
14442 SYMBOL_SET_LANGUAGE (sym, cu->language);
14443 linkagename = dwarf2_physname (name, die, cu);
14444 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14445
14446 /* Fortran does not have mangling standard and the mangling does differ
14447 between gfortran, iFort etc. */
14448 if (cu->language == language_fortran
14449 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14450 symbol_set_demangled_name (&(sym->ginfo),
14451 (char *) dwarf2_full_name (name, die, cu),
14452 NULL);
14453
14454 /* Default assumptions.
14455 Use the passed type or decode it from the die. */
14456 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14457 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14458 if (type != NULL)
14459 SYMBOL_TYPE (sym) = type;
14460 else
14461 SYMBOL_TYPE (sym) = die_type (die, cu);
14462 attr = dwarf2_attr (die,
14463 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14464 cu);
14465 if (attr)
14466 {
14467 SYMBOL_LINE (sym) = DW_UNSND (attr);
14468 }
14469
14470 attr = dwarf2_attr (die,
14471 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14472 cu);
14473 if (attr)
14474 {
14475 int file_index = DW_UNSND (attr);
14476
14477 if (cu->line_header == NULL
14478 || file_index > cu->line_header->num_file_names)
14479 complaint (&symfile_complaints,
14480 _("file index out of range"));
14481 else if (file_index > 0)
14482 {
14483 struct file_entry *fe;
14484
14485 fe = &cu->line_header->file_names[file_index - 1];
14486 SYMBOL_SYMTAB (sym) = fe->symtab;
14487 }
14488 }
14489
14490 switch (die->tag)
14491 {
14492 case DW_TAG_label:
14493 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14494 if (attr)
14495 {
14496 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14497 }
14498 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14499 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14500 SYMBOL_CLASS (sym) = LOC_LABEL;
14501 add_symbol_to_list (sym, cu->list_in_scope);
14502 break;
14503 case DW_TAG_subprogram:
14504 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14505 finish_block. */
14506 SYMBOL_CLASS (sym) = LOC_BLOCK;
14507 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14508 if ((attr2 && (DW_UNSND (attr2) != 0))
14509 || cu->language == language_ada)
14510 {
14511 /* Subprograms marked external are stored as a global symbol.
14512 Ada subprograms, whether marked external or not, are always
14513 stored as a global symbol, because we want to be able to
14514 access them globally. For instance, we want to be able
14515 to break on a nested subprogram without having to
14516 specify the context. */
14517 list_to_add = &global_symbols;
14518 }
14519 else
14520 {
14521 list_to_add = cu->list_in_scope;
14522 }
14523 break;
14524 case DW_TAG_inlined_subroutine:
14525 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14526 finish_block. */
14527 SYMBOL_CLASS (sym) = LOC_BLOCK;
14528 SYMBOL_INLINED (sym) = 1;
14529 list_to_add = cu->list_in_scope;
14530 break;
14531 case DW_TAG_template_value_param:
14532 suppress_add = 1;
14533 /* Fall through. */
14534 case DW_TAG_constant:
14535 case DW_TAG_variable:
14536 case DW_TAG_member:
14537 /* Compilation with minimal debug info may result in
14538 variables with missing type entries. Change the
14539 misleading `void' type to something sensible. */
14540 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14541 SYMBOL_TYPE (sym)
14542 = objfile_type (objfile)->nodebug_data_symbol;
14543
14544 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14545 /* In the case of DW_TAG_member, we should only be called for
14546 static const members. */
14547 if (die->tag == DW_TAG_member)
14548 {
14549 /* dwarf2_add_field uses die_is_declaration,
14550 so we do the same. */
14551 gdb_assert (die_is_declaration (die, cu));
14552 gdb_assert (attr);
14553 }
14554 if (attr)
14555 {
14556 dwarf2_const_value (attr, sym, cu);
14557 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14558 if (!suppress_add)
14559 {
14560 if (attr2 && (DW_UNSND (attr2) != 0))
14561 list_to_add = &global_symbols;
14562 else
14563 list_to_add = cu->list_in_scope;
14564 }
14565 break;
14566 }
14567 attr = dwarf2_attr (die, DW_AT_location, cu);
14568 if (attr)
14569 {
14570 var_decode_location (attr, sym, cu);
14571 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14572 if (SYMBOL_CLASS (sym) == LOC_STATIC
14573 && SYMBOL_VALUE_ADDRESS (sym) == 0
14574 && !dwarf2_per_objfile->has_section_at_zero)
14575 {
14576 /* When a static variable is eliminated by the linker,
14577 the corresponding debug information is not stripped
14578 out, but the variable address is set to null;
14579 do not add such variables into symbol table. */
14580 }
14581 else if (attr2 && (DW_UNSND (attr2) != 0))
14582 {
14583 /* Workaround gfortran PR debug/40040 - it uses
14584 DW_AT_location for variables in -fPIC libraries which may
14585 get overriden by other libraries/executable and get
14586 a different address. Resolve it by the minimal symbol
14587 which may come from inferior's executable using copy
14588 relocation. Make this workaround only for gfortran as for
14589 other compilers GDB cannot guess the minimal symbol
14590 Fortran mangling kind. */
14591 if (cu->language == language_fortran && die->parent
14592 && die->parent->tag == DW_TAG_module
14593 && cu->producer
14594 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14595 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14596
14597 /* A variable with DW_AT_external is never static,
14598 but it may be block-scoped. */
14599 list_to_add = (cu->list_in_scope == &file_symbols
14600 ? &global_symbols : cu->list_in_scope);
14601 }
14602 else
14603 list_to_add = cu->list_in_scope;
14604 }
14605 else
14606 {
14607 /* We do not know the address of this symbol.
14608 If it is an external symbol and we have type information
14609 for it, enter the symbol as a LOC_UNRESOLVED symbol.
14610 The address of the variable will then be determined from
14611 the minimal symbol table whenever the variable is
14612 referenced. */
14613 attr2 = dwarf2_attr (die, DW_AT_external, cu);
14614 if (attr2 && (DW_UNSND (attr2) != 0)
14615 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
14616 {
14617 /* A variable with DW_AT_external is never static, but it
14618 may be block-scoped. */
14619 list_to_add = (cu->list_in_scope == &file_symbols
14620 ? &global_symbols : cu->list_in_scope);
14621
14622 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14623 }
14624 else if (!die_is_declaration (die, cu))
14625 {
14626 /* Use the default LOC_OPTIMIZED_OUT class. */
14627 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
14628 if (!suppress_add)
14629 list_to_add = cu->list_in_scope;
14630 }
14631 }
14632 break;
14633 case DW_TAG_formal_parameter:
14634 /* If we are inside a function, mark this as an argument. If
14635 not, we might be looking at an argument to an inlined function
14636 when we do not have enough information to show inlined frames;
14637 pretend it's a local variable in that case so that the user can
14638 still see it. */
14639 if (context_stack_depth > 0
14640 && context_stack[context_stack_depth - 1].name != NULL)
14641 SYMBOL_IS_ARGUMENT (sym) = 1;
14642 attr = dwarf2_attr (die, DW_AT_location, cu);
14643 if (attr)
14644 {
14645 var_decode_location (attr, sym, cu);
14646 }
14647 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14648 if (attr)
14649 {
14650 dwarf2_const_value (attr, sym, cu);
14651 }
14652
14653 list_to_add = cu->list_in_scope;
14654 break;
14655 case DW_TAG_unspecified_parameters:
14656 /* From varargs functions; gdb doesn't seem to have any
14657 interest in this information, so just ignore it for now.
14658 (FIXME?) */
14659 break;
14660 case DW_TAG_template_type_param:
14661 suppress_add = 1;
14662 /* Fall through. */
14663 case DW_TAG_class_type:
14664 case DW_TAG_interface_type:
14665 case DW_TAG_structure_type:
14666 case DW_TAG_union_type:
14667 case DW_TAG_set_type:
14668 case DW_TAG_enumeration_type:
14669 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14670 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
14671
14672 {
14673 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
14674 really ever be static objects: otherwise, if you try
14675 to, say, break of a class's method and you're in a file
14676 which doesn't mention that class, it won't work unless
14677 the check for all static symbols in lookup_symbol_aux
14678 saves you. See the OtherFileClass tests in
14679 gdb.c++/namespace.exp. */
14680
14681 if (!suppress_add)
14682 {
14683 list_to_add = (cu->list_in_scope == &file_symbols
14684 && (cu->language == language_cplus
14685 || cu->language == language_java)
14686 ? &global_symbols : cu->list_in_scope);
14687
14688 /* The semantics of C++ state that "struct foo {
14689 ... }" also defines a typedef for "foo". A Java
14690 class declaration also defines a typedef for the
14691 class. */
14692 if (cu->language == language_cplus
14693 || cu->language == language_java
14694 || cu->language == language_ada)
14695 {
14696 /* The symbol's name is already allocated along
14697 with this objfile, so we don't need to
14698 duplicate it for the type. */
14699 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
14700 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
14701 }
14702 }
14703 }
14704 break;
14705 case DW_TAG_typedef:
14706 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14707 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14708 list_to_add = cu->list_in_scope;
14709 break;
14710 case DW_TAG_base_type:
14711 case DW_TAG_subrange_type:
14712 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14713 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14714 list_to_add = cu->list_in_scope;
14715 break;
14716 case DW_TAG_enumerator:
14717 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14718 if (attr)
14719 {
14720 dwarf2_const_value (attr, sym, cu);
14721 }
14722 {
14723 /* NOTE: carlton/2003-11-10: See comment above in the
14724 DW_TAG_class_type, etc. block. */
14725
14726 list_to_add = (cu->list_in_scope == &file_symbols
14727 && (cu->language == language_cplus
14728 || cu->language == language_java)
14729 ? &global_symbols : cu->list_in_scope);
14730 }
14731 break;
14732 case DW_TAG_namespace:
14733 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
14734 list_to_add = &global_symbols;
14735 break;
14736 default:
14737 /* Not a tag we recognize. Hopefully we aren't processing
14738 trash data, but since we must specifically ignore things
14739 we don't recognize, there is nothing else we should do at
14740 this point. */
14741 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
14742 dwarf_tag_name (die->tag));
14743 break;
14744 }
14745
14746 if (suppress_add)
14747 {
14748 sym->hash_next = objfile->template_symbols;
14749 objfile->template_symbols = sym;
14750 list_to_add = NULL;
14751 }
14752
14753 if (list_to_add != NULL)
14754 add_symbol_to_list (sym, list_to_add);
14755
14756 /* For the benefit of old versions of GCC, check for anonymous
14757 namespaces based on the demangled name. */
14758 if (!processing_has_namespace_info
14759 && cu->language == language_cplus)
14760 cp_scan_for_anonymous_namespaces (sym, objfile);
14761 }
14762 return (sym);
14763 }
14764
14765 /* A wrapper for new_symbol_full that always allocates a new symbol. */
14766
14767 static struct symbol *
14768 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14769 {
14770 return new_symbol_full (die, type, cu, NULL);
14771 }
14772
14773 /* Given an attr with a DW_FORM_dataN value in host byte order,
14774 zero-extend it as appropriate for the symbol's type. The DWARF
14775 standard (v4) is not entirely clear about the meaning of using
14776 DW_FORM_dataN for a constant with a signed type, where the type is
14777 wider than the data. The conclusion of a discussion on the DWARF
14778 list was that this is unspecified. We choose to always zero-extend
14779 because that is the interpretation long in use by GCC. */
14780
14781 static gdb_byte *
14782 dwarf2_const_value_data (struct attribute *attr, struct type *type,
14783 const char *name, struct obstack *obstack,
14784 struct dwarf2_cu *cu, LONGEST *value, int bits)
14785 {
14786 struct objfile *objfile = cu->objfile;
14787 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
14788 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
14789 LONGEST l = DW_UNSND (attr);
14790
14791 if (bits < sizeof (*value) * 8)
14792 {
14793 l &= ((LONGEST) 1 << bits) - 1;
14794 *value = l;
14795 }
14796 else if (bits == sizeof (*value) * 8)
14797 *value = l;
14798 else
14799 {
14800 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
14801 store_unsigned_integer (bytes, bits / 8, byte_order, l);
14802 return bytes;
14803 }
14804
14805 return NULL;
14806 }
14807
14808 /* Read a constant value from an attribute. Either set *VALUE, or if
14809 the value does not fit in *VALUE, set *BYTES - either already
14810 allocated on the objfile obstack, or newly allocated on OBSTACK,
14811 or, set *BATON, if we translated the constant to a location
14812 expression. */
14813
14814 static void
14815 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
14816 const char *name, struct obstack *obstack,
14817 struct dwarf2_cu *cu,
14818 LONGEST *value, gdb_byte **bytes,
14819 struct dwarf2_locexpr_baton **baton)
14820 {
14821 struct objfile *objfile = cu->objfile;
14822 struct comp_unit_head *cu_header = &cu->header;
14823 struct dwarf_block *blk;
14824 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
14825 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
14826
14827 *value = 0;
14828 *bytes = NULL;
14829 *baton = NULL;
14830
14831 switch (attr->form)
14832 {
14833 case DW_FORM_addr:
14834 case DW_FORM_GNU_addr_index:
14835 {
14836 gdb_byte *data;
14837
14838 if (TYPE_LENGTH (type) != cu_header->addr_size)
14839 dwarf2_const_value_length_mismatch_complaint (name,
14840 cu_header->addr_size,
14841 TYPE_LENGTH (type));
14842 /* Symbols of this form are reasonably rare, so we just
14843 piggyback on the existing location code rather than writing
14844 a new implementation of symbol_computed_ops. */
14845 *baton = obstack_alloc (&objfile->objfile_obstack,
14846 sizeof (struct dwarf2_locexpr_baton));
14847 (*baton)->per_cu = cu->per_cu;
14848 gdb_assert ((*baton)->per_cu);
14849
14850 (*baton)->size = 2 + cu_header->addr_size;
14851 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14852 (*baton)->data = data;
14853
14854 data[0] = DW_OP_addr;
14855 store_unsigned_integer (&data[1], cu_header->addr_size,
14856 byte_order, DW_ADDR (attr));
14857 data[cu_header->addr_size + 1] = DW_OP_stack_value;
14858 }
14859 break;
14860 case DW_FORM_string:
14861 case DW_FORM_strp:
14862 case DW_FORM_GNU_str_index:
14863 /* DW_STRING is already allocated on the objfile obstack, point
14864 directly to it. */
14865 *bytes = (gdb_byte *) DW_STRING (attr);
14866 break;
14867 case DW_FORM_block1:
14868 case DW_FORM_block2:
14869 case DW_FORM_block4:
14870 case DW_FORM_block:
14871 case DW_FORM_exprloc:
14872 blk = DW_BLOCK (attr);
14873 if (TYPE_LENGTH (type) != blk->size)
14874 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14875 TYPE_LENGTH (type));
14876 *bytes = blk->data;
14877 break;
14878
14879 /* The DW_AT_const_value attributes are supposed to carry the
14880 symbol's value "represented as it would be on the target
14881 architecture." By the time we get here, it's already been
14882 converted to host endianness, so we just need to sign- or
14883 zero-extend it as appropriate. */
14884 case DW_FORM_data1:
14885 *bytes = dwarf2_const_value_data (attr, type, name,
14886 obstack, cu, value, 8);
14887 break;
14888 case DW_FORM_data2:
14889 *bytes = dwarf2_const_value_data (attr, type, name,
14890 obstack, cu, value, 16);
14891 break;
14892 case DW_FORM_data4:
14893 *bytes = dwarf2_const_value_data (attr, type, name,
14894 obstack, cu, value, 32);
14895 break;
14896 case DW_FORM_data8:
14897 *bytes = dwarf2_const_value_data (attr, type, name,
14898 obstack, cu, value, 64);
14899 break;
14900
14901 case DW_FORM_sdata:
14902 *value = DW_SND (attr);
14903 break;
14904
14905 case DW_FORM_udata:
14906 *value = DW_UNSND (attr);
14907 break;
14908
14909 default:
14910 complaint (&symfile_complaints,
14911 _("unsupported const value attribute form: '%s'"),
14912 dwarf_form_name (attr->form));
14913 *value = 0;
14914 break;
14915 }
14916 }
14917
14918
14919 /* Copy constant value from an attribute to a symbol. */
14920
14921 static void
14922 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14923 struct dwarf2_cu *cu)
14924 {
14925 struct objfile *objfile = cu->objfile;
14926 struct comp_unit_head *cu_header = &cu->header;
14927 LONGEST value;
14928 gdb_byte *bytes;
14929 struct dwarf2_locexpr_baton *baton;
14930
14931 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14932 SYMBOL_PRINT_NAME (sym),
14933 &objfile->objfile_obstack, cu,
14934 &value, &bytes, &baton);
14935
14936 if (baton != NULL)
14937 {
14938 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14939 SYMBOL_LOCATION_BATON (sym) = baton;
14940 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14941 }
14942 else if (bytes != NULL)
14943 {
14944 SYMBOL_VALUE_BYTES (sym) = bytes;
14945 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14946 }
14947 else
14948 {
14949 SYMBOL_VALUE (sym) = value;
14950 SYMBOL_CLASS (sym) = LOC_CONST;
14951 }
14952 }
14953
14954 /* Return the type of the die in question using its DW_AT_type attribute. */
14955
14956 static struct type *
14957 die_type (struct die_info *die, struct dwarf2_cu *cu)
14958 {
14959 struct attribute *type_attr;
14960
14961 type_attr = dwarf2_attr (die, DW_AT_type, cu);
14962 if (!type_attr)
14963 {
14964 /* A missing DW_AT_type represents a void type. */
14965 return objfile_type (cu->objfile)->builtin_void;
14966 }
14967
14968 return lookup_die_type (die, type_attr, cu);
14969 }
14970
14971 /* True iff CU's producer generates GNAT Ada auxiliary information
14972 that allows to find parallel types through that information instead
14973 of having to do expensive parallel lookups by type name. */
14974
14975 static int
14976 need_gnat_info (struct dwarf2_cu *cu)
14977 {
14978 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14979 of GNAT produces this auxiliary information, without any indication
14980 that it is produced. Part of enhancing the FSF version of GNAT
14981 to produce that information will be to put in place an indicator
14982 that we can use in order to determine whether the descriptive type
14983 info is available or not. One suggestion that has been made is
14984 to use a new attribute, attached to the CU die. For now, assume
14985 that the descriptive type info is not available. */
14986 return 0;
14987 }
14988
14989 /* Return the auxiliary type of the die in question using its
14990 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14991 attribute is not present. */
14992
14993 static struct type *
14994 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14995 {
14996 struct attribute *type_attr;
14997
14998 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14999 if (!type_attr)
15000 return NULL;
15001
15002 return lookup_die_type (die, type_attr, cu);
15003 }
15004
15005 /* If DIE has a descriptive_type attribute, then set the TYPE's
15006 descriptive type accordingly. */
15007
15008 static void
15009 set_descriptive_type (struct type *type, struct die_info *die,
15010 struct dwarf2_cu *cu)
15011 {
15012 struct type *descriptive_type = die_descriptive_type (die, cu);
15013
15014 if (descriptive_type)
15015 {
15016 ALLOCATE_GNAT_AUX_TYPE (type);
15017 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15018 }
15019 }
15020
15021 /* Return the containing type of the die in question using its
15022 DW_AT_containing_type attribute. */
15023
15024 static struct type *
15025 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15026 {
15027 struct attribute *type_attr;
15028
15029 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15030 if (!type_attr)
15031 error (_("Dwarf Error: Problem turning containing type into gdb type "
15032 "[in module %s]"), cu->objfile->name);
15033
15034 return lookup_die_type (die, type_attr, cu);
15035 }
15036
15037 /* Look up the type of DIE in CU using its type attribute ATTR.
15038 If there is no type substitute an error marker. */
15039
15040 static struct type *
15041 lookup_die_type (struct die_info *die, struct attribute *attr,
15042 struct dwarf2_cu *cu)
15043 {
15044 struct objfile *objfile = cu->objfile;
15045 struct type *this_type;
15046
15047 /* First see if we have it cached. */
15048
15049 if (is_ref_attr (attr))
15050 {
15051 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15052
15053 this_type = get_die_type_at_offset (offset, cu->per_cu);
15054 }
15055 else if (attr->form == DW_FORM_ref_sig8)
15056 {
15057 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15058
15059 /* sig_type will be NULL if the signatured type is missing from
15060 the debug info. */
15061 if (sig_type == NULL)
15062 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15063 "at 0x%x [in module %s]"),
15064 die->offset.sect_off, objfile->name);
15065
15066 gdb_assert (sig_type->per_cu.is_debug_types);
15067 /* If we haven't filled in type_offset_in_section yet, then we
15068 haven't read the type in yet. */
15069 this_type = NULL;
15070 if (sig_type->type_offset_in_section.sect_off != 0)
15071 {
15072 this_type =
15073 get_die_type_at_offset (sig_type->type_offset_in_section,
15074 &sig_type->per_cu);
15075 }
15076 }
15077 else
15078 {
15079 dump_die_for_error (die);
15080 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15081 dwarf_attr_name (attr->name), objfile->name);
15082 }
15083
15084 /* If not cached we need to read it in. */
15085
15086 if (this_type == NULL)
15087 {
15088 struct die_info *type_die;
15089 struct dwarf2_cu *type_cu = cu;
15090
15091 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15092 /* If we found the type now, it's probably because the type came
15093 from an inter-CU reference and the type's CU got expanded before
15094 ours. */
15095 this_type = get_die_type (type_die, type_cu);
15096 if (this_type == NULL)
15097 this_type = read_type_die_1 (type_die, type_cu);
15098 }
15099
15100 /* If we still don't have a type use an error marker. */
15101
15102 if (this_type == NULL)
15103 {
15104 char *message, *saved;
15105
15106 /* read_type_die already issued a complaint. */
15107 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15108 objfile->name,
15109 cu->header.offset.sect_off,
15110 die->offset.sect_off);
15111 saved = obstack_copy0 (&objfile->objfile_obstack,
15112 message, strlen (message));
15113 xfree (message);
15114
15115 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15116 }
15117
15118 return this_type;
15119 }
15120
15121 /* Return the type in DIE, CU.
15122 Returns NULL for invalid types.
15123
15124 This first does a lookup in the appropriate type_hash table,
15125 and only reads the die in if necessary.
15126
15127 NOTE: This can be called when reading in partial or full symbols. */
15128
15129 static struct type *
15130 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15131 {
15132 struct type *this_type;
15133
15134 this_type = get_die_type (die, cu);
15135 if (this_type)
15136 return this_type;
15137
15138 return read_type_die_1 (die, cu);
15139 }
15140
15141 /* Read the type in DIE, CU.
15142 Returns NULL for invalid types. */
15143
15144 static struct type *
15145 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15146 {
15147 struct type *this_type = NULL;
15148
15149 switch (die->tag)
15150 {
15151 case DW_TAG_class_type:
15152 case DW_TAG_interface_type:
15153 case DW_TAG_structure_type:
15154 case DW_TAG_union_type:
15155 this_type = read_structure_type (die, cu);
15156 break;
15157 case DW_TAG_enumeration_type:
15158 this_type = read_enumeration_type (die, cu);
15159 break;
15160 case DW_TAG_subprogram:
15161 case DW_TAG_subroutine_type:
15162 case DW_TAG_inlined_subroutine:
15163 this_type = read_subroutine_type (die, cu);
15164 break;
15165 case DW_TAG_array_type:
15166 this_type = read_array_type (die, cu);
15167 break;
15168 case DW_TAG_set_type:
15169 this_type = read_set_type (die, cu);
15170 break;
15171 case DW_TAG_pointer_type:
15172 this_type = read_tag_pointer_type (die, cu);
15173 break;
15174 case DW_TAG_ptr_to_member_type:
15175 this_type = read_tag_ptr_to_member_type (die, cu);
15176 break;
15177 case DW_TAG_reference_type:
15178 this_type = read_tag_reference_type (die, cu);
15179 break;
15180 case DW_TAG_const_type:
15181 this_type = read_tag_const_type (die, cu);
15182 break;
15183 case DW_TAG_volatile_type:
15184 this_type = read_tag_volatile_type (die, cu);
15185 break;
15186 case DW_TAG_string_type:
15187 this_type = read_tag_string_type (die, cu);
15188 break;
15189 case DW_TAG_typedef:
15190 this_type = read_typedef (die, cu);
15191 break;
15192 case DW_TAG_subrange_type:
15193 this_type = read_subrange_type (die, cu);
15194 break;
15195 case DW_TAG_base_type:
15196 this_type = read_base_type (die, cu);
15197 break;
15198 case DW_TAG_unspecified_type:
15199 this_type = read_unspecified_type (die, cu);
15200 break;
15201 case DW_TAG_namespace:
15202 this_type = read_namespace_type (die, cu);
15203 break;
15204 case DW_TAG_module:
15205 this_type = read_module_type (die, cu);
15206 break;
15207 default:
15208 complaint (&symfile_complaints,
15209 _("unexpected tag in read_type_die: '%s'"),
15210 dwarf_tag_name (die->tag));
15211 break;
15212 }
15213
15214 return this_type;
15215 }
15216
15217 /* See if we can figure out if the class lives in a namespace. We do
15218 this by looking for a member function; its demangled name will
15219 contain namespace info, if there is any.
15220 Return the computed name or NULL.
15221 Space for the result is allocated on the objfile's obstack.
15222 This is the full-die version of guess_partial_die_structure_name.
15223 In this case we know DIE has no useful parent. */
15224
15225 static char *
15226 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15227 {
15228 struct die_info *spec_die;
15229 struct dwarf2_cu *spec_cu;
15230 struct die_info *child;
15231
15232 spec_cu = cu;
15233 spec_die = die_specification (die, &spec_cu);
15234 if (spec_die != NULL)
15235 {
15236 die = spec_die;
15237 cu = spec_cu;
15238 }
15239
15240 for (child = die->child;
15241 child != NULL;
15242 child = child->sibling)
15243 {
15244 if (child->tag == DW_TAG_subprogram)
15245 {
15246 struct attribute *attr;
15247
15248 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15249 if (attr == NULL)
15250 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15251 if (attr != NULL)
15252 {
15253 char *actual_name
15254 = language_class_name_from_physname (cu->language_defn,
15255 DW_STRING (attr));
15256 char *name = NULL;
15257
15258 if (actual_name != NULL)
15259 {
15260 char *die_name = dwarf2_name (die, cu);
15261
15262 if (die_name != NULL
15263 && strcmp (die_name, actual_name) != 0)
15264 {
15265 /* Strip off the class name from the full name.
15266 We want the prefix. */
15267 int die_name_len = strlen (die_name);
15268 int actual_name_len = strlen (actual_name);
15269
15270 /* Test for '::' as a sanity check. */
15271 if (actual_name_len > die_name_len + 2
15272 && actual_name[actual_name_len
15273 - die_name_len - 1] == ':')
15274 name =
15275 obsavestring (actual_name,
15276 actual_name_len - die_name_len - 2,
15277 &cu->objfile->objfile_obstack);
15278 }
15279 }
15280 xfree (actual_name);
15281 return name;
15282 }
15283 }
15284 }
15285
15286 return NULL;
15287 }
15288
15289 /* GCC might emit a nameless typedef that has a linkage name. Determine the
15290 prefix part in such case. See
15291 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15292
15293 static char *
15294 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15295 {
15296 struct attribute *attr;
15297 char *base;
15298
15299 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15300 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15301 return NULL;
15302
15303 attr = dwarf2_attr (die, DW_AT_name, cu);
15304 if (attr != NULL && DW_STRING (attr) != NULL)
15305 return NULL;
15306
15307 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15308 if (attr == NULL)
15309 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15310 if (attr == NULL || DW_STRING (attr) == NULL)
15311 return NULL;
15312
15313 /* dwarf2_name had to be already called. */
15314 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15315
15316 /* Strip the base name, keep any leading namespaces/classes. */
15317 base = strrchr (DW_STRING (attr), ':');
15318 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15319 return "";
15320
15321 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15322 &cu->objfile->objfile_obstack);
15323 }
15324
15325 /* Return the name of the namespace/class that DIE is defined within,
15326 or "" if we can't tell. The caller should not xfree the result.
15327
15328 For example, if we're within the method foo() in the following
15329 code:
15330
15331 namespace N {
15332 class C {
15333 void foo () {
15334 }
15335 };
15336 }
15337
15338 then determine_prefix on foo's die will return "N::C". */
15339
15340 static const char *
15341 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15342 {
15343 struct die_info *parent, *spec_die;
15344 struct dwarf2_cu *spec_cu;
15345 struct type *parent_type;
15346 char *retval;
15347
15348 if (cu->language != language_cplus && cu->language != language_java
15349 && cu->language != language_fortran)
15350 return "";
15351
15352 retval = anonymous_struct_prefix (die, cu);
15353 if (retval)
15354 return retval;
15355
15356 /* We have to be careful in the presence of DW_AT_specification.
15357 For example, with GCC 3.4, given the code
15358
15359 namespace N {
15360 void foo() {
15361 // Definition of N::foo.
15362 }
15363 }
15364
15365 then we'll have a tree of DIEs like this:
15366
15367 1: DW_TAG_compile_unit
15368 2: DW_TAG_namespace // N
15369 3: DW_TAG_subprogram // declaration of N::foo
15370 4: DW_TAG_subprogram // definition of N::foo
15371 DW_AT_specification // refers to die #3
15372
15373 Thus, when processing die #4, we have to pretend that we're in
15374 the context of its DW_AT_specification, namely the contex of die
15375 #3. */
15376 spec_cu = cu;
15377 spec_die = die_specification (die, &spec_cu);
15378 if (spec_die == NULL)
15379 parent = die->parent;
15380 else
15381 {
15382 parent = spec_die->parent;
15383 cu = spec_cu;
15384 }
15385
15386 if (parent == NULL)
15387 return "";
15388 else if (parent->building_fullname)
15389 {
15390 const char *name;
15391 const char *parent_name;
15392
15393 /* It has been seen on RealView 2.2 built binaries,
15394 DW_TAG_template_type_param types actually _defined_ as
15395 children of the parent class:
15396
15397 enum E {};
15398 template class <class Enum> Class{};
15399 Class<enum E> class_e;
15400
15401 1: DW_TAG_class_type (Class)
15402 2: DW_TAG_enumeration_type (E)
15403 3: DW_TAG_enumerator (enum1:0)
15404 3: DW_TAG_enumerator (enum2:1)
15405 ...
15406 2: DW_TAG_template_type_param
15407 DW_AT_type DW_FORM_ref_udata (E)
15408
15409 Besides being broken debug info, it can put GDB into an
15410 infinite loop. Consider:
15411
15412 When we're building the full name for Class<E>, we'll start
15413 at Class, and go look over its template type parameters,
15414 finding E. We'll then try to build the full name of E, and
15415 reach here. We're now trying to build the full name of E,
15416 and look over the parent DIE for containing scope. In the
15417 broken case, if we followed the parent DIE of E, we'd again
15418 find Class, and once again go look at its template type
15419 arguments, etc., etc. Simply don't consider such parent die
15420 as source-level parent of this die (it can't be, the language
15421 doesn't allow it), and break the loop here. */
15422 name = dwarf2_name (die, cu);
15423 parent_name = dwarf2_name (parent, cu);
15424 complaint (&symfile_complaints,
15425 _("template param type '%s' defined within parent '%s'"),
15426 name ? name : "<unknown>",
15427 parent_name ? parent_name : "<unknown>");
15428 return "";
15429 }
15430 else
15431 switch (parent->tag)
15432 {
15433 case DW_TAG_namespace:
15434 parent_type = read_type_die (parent, cu);
15435 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15436 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15437 Work around this problem here. */
15438 if (cu->language == language_cplus
15439 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15440 return "";
15441 /* We give a name to even anonymous namespaces. */
15442 return TYPE_TAG_NAME (parent_type);
15443 case DW_TAG_class_type:
15444 case DW_TAG_interface_type:
15445 case DW_TAG_structure_type:
15446 case DW_TAG_union_type:
15447 case DW_TAG_module:
15448 parent_type = read_type_die (parent, cu);
15449 if (TYPE_TAG_NAME (parent_type) != NULL)
15450 return TYPE_TAG_NAME (parent_type);
15451 else
15452 /* An anonymous structure is only allowed non-static data
15453 members; no typedefs, no member functions, et cetera.
15454 So it does not need a prefix. */
15455 return "";
15456 case DW_TAG_compile_unit:
15457 case DW_TAG_partial_unit:
15458 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15459 if (cu->language == language_cplus
15460 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15461 && die->child != NULL
15462 && (die->tag == DW_TAG_class_type
15463 || die->tag == DW_TAG_structure_type
15464 || die->tag == DW_TAG_union_type))
15465 {
15466 char *name = guess_full_die_structure_name (die, cu);
15467 if (name != NULL)
15468 return name;
15469 }
15470 return "";
15471 default:
15472 return determine_prefix (parent, cu);
15473 }
15474 }
15475
15476 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15477 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15478 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15479 an obconcat, otherwise allocate storage for the result. The CU argument is
15480 used to determine the language and hence, the appropriate separator. */
15481
15482 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
15483
15484 static char *
15485 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15486 int physname, struct dwarf2_cu *cu)
15487 {
15488 const char *lead = "";
15489 const char *sep;
15490
15491 if (suffix == NULL || suffix[0] == '\0'
15492 || prefix == NULL || prefix[0] == '\0')
15493 sep = "";
15494 else if (cu->language == language_java)
15495 sep = ".";
15496 else if (cu->language == language_fortran && physname)
15497 {
15498 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15499 DW_AT_MIPS_linkage_name is preferred and used instead. */
15500
15501 lead = "__";
15502 sep = "_MOD_";
15503 }
15504 else
15505 sep = "::";
15506
15507 if (prefix == NULL)
15508 prefix = "";
15509 if (suffix == NULL)
15510 suffix = "";
15511
15512 if (obs == NULL)
15513 {
15514 char *retval
15515 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15516
15517 strcpy (retval, lead);
15518 strcat (retval, prefix);
15519 strcat (retval, sep);
15520 strcat (retval, suffix);
15521 return retval;
15522 }
15523 else
15524 {
15525 /* We have an obstack. */
15526 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15527 }
15528 }
15529
15530 /* Return sibling of die, NULL if no sibling. */
15531
15532 static struct die_info *
15533 sibling_die (struct die_info *die)
15534 {
15535 return die->sibling;
15536 }
15537
15538 /* Get name of a die, return NULL if not found. */
15539
15540 static char *
15541 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15542 struct obstack *obstack)
15543 {
15544 if (name && cu->language == language_cplus)
15545 {
15546 char *canon_name = cp_canonicalize_string (name);
15547
15548 if (canon_name != NULL)
15549 {
15550 if (strcmp (canon_name, name) != 0)
15551 name = obsavestring (canon_name, strlen (canon_name),
15552 obstack);
15553 xfree (canon_name);
15554 }
15555 }
15556
15557 return name;
15558 }
15559
15560 /* Get name of a die, return NULL if not found. */
15561
15562 static char *
15563 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15564 {
15565 struct attribute *attr;
15566
15567 attr = dwarf2_attr (die, DW_AT_name, cu);
15568 if ((!attr || !DW_STRING (attr))
15569 && die->tag != DW_TAG_class_type
15570 && die->tag != DW_TAG_interface_type
15571 && die->tag != DW_TAG_structure_type
15572 && die->tag != DW_TAG_union_type)
15573 return NULL;
15574
15575 switch (die->tag)
15576 {
15577 case DW_TAG_compile_unit:
15578 case DW_TAG_partial_unit:
15579 /* Compilation units have a DW_AT_name that is a filename, not
15580 a source language identifier. */
15581 case DW_TAG_enumeration_type:
15582 case DW_TAG_enumerator:
15583 /* These tags always have simple identifiers already; no need
15584 to canonicalize them. */
15585 return DW_STRING (attr);
15586
15587 case DW_TAG_subprogram:
15588 /* Java constructors will all be named "<init>", so return
15589 the class name when we see this special case. */
15590 if (cu->language == language_java
15591 && DW_STRING (attr) != NULL
15592 && strcmp (DW_STRING (attr), "<init>") == 0)
15593 {
15594 struct dwarf2_cu *spec_cu = cu;
15595 struct die_info *spec_die;
15596
15597 /* GCJ will output '<init>' for Java constructor names.
15598 For this special case, return the name of the parent class. */
15599
15600 /* GCJ may output suprogram DIEs with AT_specification set.
15601 If so, use the name of the specified DIE. */
15602 spec_die = die_specification (die, &spec_cu);
15603 if (spec_die != NULL)
15604 return dwarf2_name (spec_die, spec_cu);
15605
15606 do
15607 {
15608 die = die->parent;
15609 if (die->tag == DW_TAG_class_type)
15610 return dwarf2_name (die, cu);
15611 }
15612 while (die->tag != DW_TAG_compile_unit
15613 && die->tag != DW_TAG_partial_unit);
15614 }
15615 break;
15616
15617 case DW_TAG_class_type:
15618 case DW_TAG_interface_type:
15619 case DW_TAG_structure_type:
15620 case DW_TAG_union_type:
15621 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
15622 structures or unions. These were of the form "._%d" in GCC 4.1,
15623 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
15624 and GCC 4.4. We work around this problem by ignoring these. */
15625 if (attr && DW_STRING (attr)
15626 && (strncmp (DW_STRING (attr), "._", 2) == 0
15627 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
15628 return NULL;
15629
15630 /* GCC might emit a nameless typedef that has a linkage name. See
15631 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15632 if (!attr || DW_STRING (attr) == NULL)
15633 {
15634 char *demangled = NULL;
15635
15636 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15637 if (attr == NULL)
15638 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15639
15640 if (attr == NULL || DW_STRING (attr) == NULL)
15641 return NULL;
15642
15643 /* Avoid demangling DW_STRING (attr) the second time on a second
15644 call for the same DIE. */
15645 if (!DW_STRING_IS_CANONICAL (attr))
15646 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
15647
15648 if (demangled)
15649 {
15650 char *base;
15651
15652 /* FIXME: we already did this for the partial symbol... */
15653 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
15654 &cu->objfile->objfile_obstack);
15655 DW_STRING_IS_CANONICAL (attr) = 1;
15656 xfree (demangled);
15657
15658 /* Strip any leading namespaces/classes, keep only the base name.
15659 DW_AT_name for named DIEs does not contain the prefixes. */
15660 base = strrchr (DW_STRING (attr), ':');
15661 if (base && base > DW_STRING (attr) && base[-1] == ':')
15662 return &base[1];
15663 else
15664 return DW_STRING (attr);
15665 }
15666 }
15667 break;
15668
15669 default:
15670 break;
15671 }
15672
15673 if (!DW_STRING_IS_CANONICAL (attr))
15674 {
15675 DW_STRING (attr)
15676 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
15677 &cu->objfile->objfile_obstack);
15678 DW_STRING_IS_CANONICAL (attr) = 1;
15679 }
15680 return DW_STRING (attr);
15681 }
15682
15683 /* Return the die that this die in an extension of, or NULL if there
15684 is none. *EXT_CU is the CU containing DIE on input, and the CU
15685 containing the return value on output. */
15686
15687 static struct die_info *
15688 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
15689 {
15690 struct attribute *attr;
15691
15692 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
15693 if (attr == NULL)
15694 return NULL;
15695
15696 return follow_die_ref (die, attr, ext_cu);
15697 }
15698
15699 /* Convert a DIE tag into its string name. */
15700
15701 static const char *
15702 dwarf_tag_name (unsigned tag)
15703 {
15704 const char *name = get_DW_TAG_name (tag);
15705
15706 if (name == NULL)
15707 return "DW_TAG_<unknown>";
15708
15709 return name;
15710 }
15711
15712 /* Convert a DWARF attribute code into its string name. */
15713
15714 static const char *
15715 dwarf_attr_name (unsigned attr)
15716 {
15717 const char *name;
15718
15719 #ifdef MIPS /* collides with DW_AT_HP_block_index */
15720 if (attr == DW_AT_MIPS_fde)
15721 return "DW_AT_MIPS_fde";
15722 #else
15723 if (attr == DW_AT_HP_block_index)
15724 return "DW_AT_HP_block_index";
15725 #endif
15726
15727 name = get_DW_AT_name (attr);
15728
15729 if (name == NULL)
15730 return "DW_AT_<unknown>";
15731
15732 return name;
15733 }
15734
15735 /* Convert a DWARF value form code into its string name. */
15736
15737 static const char *
15738 dwarf_form_name (unsigned form)
15739 {
15740 const char *name = get_DW_FORM_name (form);
15741
15742 if (name == NULL)
15743 return "DW_FORM_<unknown>";
15744
15745 return name;
15746 }
15747
15748 static char *
15749 dwarf_bool_name (unsigned mybool)
15750 {
15751 if (mybool)
15752 return "TRUE";
15753 else
15754 return "FALSE";
15755 }
15756
15757 /* Convert a DWARF type code into its string name. */
15758
15759 static const char *
15760 dwarf_type_encoding_name (unsigned enc)
15761 {
15762 const char *name = get_DW_ATE_name (enc);
15763
15764 if (name == NULL)
15765 return "DW_ATE_<unknown>";
15766
15767 return name;
15768 }
15769
15770 static void
15771 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
15772 {
15773 unsigned int i;
15774
15775 print_spaces (indent, f);
15776 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
15777 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
15778
15779 if (die->parent != NULL)
15780 {
15781 print_spaces (indent, f);
15782 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
15783 die->parent->offset.sect_off);
15784 }
15785
15786 print_spaces (indent, f);
15787 fprintf_unfiltered (f, " has children: %s\n",
15788 dwarf_bool_name (die->child != NULL));
15789
15790 print_spaces (indent, f);
15791 fprintf_unfiltered (f, " attributes:\n");
15792
15793 for (i = 0; i < die->num_attrs; ++i)
15794 {
15795 print_spaces (indent, f);
15796 fprintf_unfiltered (f, " %s (%s) ",
15797 dwarf_attr_name (die->attrs[i].name),
15798 dwarf_form_name (die->attrs[i].form));
15799
15800 switch (die->attrs[i].form)
15801 {
15802 case DW_FORM_addr:
15803 case DW_FORM_GNU_addr_index:
15804 fprintf_unfiltered (f, "address: ");
15805 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
15806 break;
15807 case DW_FORM_block2:
15808 case DW_FORM_block4:
15809 case DW_FORM_block:
15810 case DW_FORM_block1:
15811 fprintf_unfiltered (f, "block: size %d",
15812 DW_BLOCK (&die->attrs[i])->size);
15813 break;
15814 case DW_FORM_exprloc:
15815 fprintf_unfiltered (f, "expression: size %u",
15816 DW_BLOCK (&die->attrs[i])->size);
15817 break;
15818 case DW_FORM_ref_addr:
15819 fprintf_unfiltered (f, "ref address: ");
15820 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15821 break;
15822 case DW_FORM_ref1:
15823 case DW_FORM_ref2:
15824 case DW_FORM_ref4:
15825 case DW_FORM_ref8:
15826 case DW_FORM_ref_udata:
15827 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
15828 (long) (DW_UNSND (&die->attrs[i])));
15829 break;
15830 case DW_FORM_data1:
15831 case DW_FORM_data2:
15832 case DW_FORM_data4:
15833 case DW_FORM_data8:
15834 case DW_FORM_udata:
15835 case DW_FORM_sdata:
15836 fprintf_unfiltered (f, "constant: %s",
15837 pulongest (DW_UNSND (&die->attrs[i])));
15838 break;
15839 case DW_FORM_sec_offset:
15840 fprintf_unfiltered (f, "section offset: %s",
15841 pulongest (DW_UNSND (&die->attrs[i])));
15842 break;
15843 case DW_FORM_ref_sig8:
15844 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15845 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15846 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15847 else
15848 fprintf_unfiltered (f, "signatured type, offset: unknown");
15849 break;
15850 case DW_FORM_string:
15851 case DW_FORM_strp:
15852 case DW_FORM_GNU_str_index:
15853 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15854 DW_STRING (&die->attrs[i])
15855 ? DW_STRING (&die->attrs[i]) : "",
15856 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15857 break;
15858 case DW_FORM_flag:
15859 if (DW_UNSND (&die->attrs[i]))
15860 fprintf_unfiltered (f, "flag: TRUE");
15861 else
15862 fprintf_unfiltered (f, "flag: FALSE");
15863 break;
15864 case DW_FORM_flag_present:
15865 fprintf_unfiltered (f, "flag: TRUE");
15866 break;
15867 case DW_FORM_indirect:
15868 /* The reader will have reduced the indirect form to
15869 the "base form" so this form should not occur. */
15870 fprintf_unfiltered (f,
15871 "unexpected attribute form: DW_FORM_indirect");
15872 break;
15873 default:
15874 fprintf_unfiltered (f, "unsupported attribute form: %d.",
15875 die->attrs[i].form);
15876 break;
15877 }
15878 fprintf_unfiltered (f, "\n");
15879 }
15880 }
15881
15882 static void
15883 dump_die_for_error (struct die_info *die)
15884 {
15885 dump_die_shallow (gdb_stderr, 0, die);
15886 }
15887
15888 static void
15889 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15890 {
15891 int indent = level * 4;
15892
15893 gdb_assert (die != NULL);
15894
15895 if (level >= max_level)
15896 return;
15897
15898 dump_die_shallow (f, indent, die);
15899
15900 if (die->child != NULL)
15901 {
15902 print_spaces (indent, f);
15903 fprintf_unfiltered (f, " Children:");
15904 if (level + 1 < max_level)
15905 {
15906 fprintf_unfiltered (f, "\n");
15907 dump_die_1 (f, level + 1, max_level, die->child);
15908 }
15909 else
15910 {
15911 fprintf_unfiltered (f,
15912 " [not printed, max nesting level reached]\n");
15913 }
15914 }
15915
15916 if (die->sibling != NULL && level > 0)
15917 {
15918 dump_die_1 (f, level, max_level, die->sibling);
15919 }
15920 }
15921
15922 /* This is called from the pdie macro in gdbinit.in.
15923 It's not static so gcc will keep a copy callable from gdb. */
15924
15925 void
15926 dump_die (struct die_info *die, int max_level)
15927 {
15928 dump_die_1 (gdb_stdlog, 0, max_level, die);
15929 }
15930
15931 static void
15932 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15933 {
15934 void **slot;
15935
15936 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15937 INSERT);
15938
15939 *slot = die;
15940 }
15941
15942 /* DW_ADDR is always stored already as sect_offset; despite for the forms
15943 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15944
15945 static int
15946 is_ref_attr (struct attribute *attr)
15947 {
15948 switch (attr->form)
15949 {
15950 case DW_FORM_ref_addr:
15951 case DW_FORM_ref1:
15952 case DW_FORM_ref2:
15953 case DW_FORM_ref4:
15954 case DW_FORM_ref8:
15955 case DW_FORM_ref_udata:
15956 return 1;
15957 default:
15958 return 0;
15959 }
15960 }
15961
15962 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15963 required kind. */
15964
15965 static sect_offset
15966 dwarf2_get_ref_die_offset (struct attribute *attr)
15967 {
15968 sect_offset retval = { DW_UNSND (attr) };
15969
15970 if (is_ref_attr (attr))
15971 return retval;
15972
15973 retval.sect_off = 0;
15974 complaint (&symfile_complaints,
15975 _("unsupported die ref attribute form: '%s'"),
15976 dwarf_form_name (attr->form));
15977 return retval;
15978 }
15979
15980 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15981 * the value held by the attribute is not constant. */
15982
15983 static LONGEST
15984 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15985 {
15986 if (attr->form == DW_FORM_sdata)
15987 return DW_SND (attr);
15988 else if (attr->form == DW_FORM_udata
15989 || attr->form == DW_FORM_data1
15990 || attr->form == DW_FORM_data2
15991 || attr->form == DW_FORM_data4
15992 || attr->form == DW_FORM_data8)
15993 return DW_UNSND (attr);
15994 else
15995 {
15996 complaint (&symfile_complaints,
15997 _("Attribute value is not a constant (%s)"),
15998 dwarf_form_name (attr->form));
15999 return default_value;
16000 }
16001 }
16002
16003 /* Follow reference or signature attribute ATTR of SRC_DIE.
16004 On entry *REF_CU is the CU of SRC_DIE.
16005 On exit *REF_CU is the CU of the result. */
16006
16007 static struct die_info *
16008 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16009 struct dwarf2_cu **ref_cu)
16010 {
16011 struct die_info *die;
16012
16013 if (is_ref_attr (attr))
16014 die = follow_die_ref (src_die, attr, ref_cu);
16015 else if (attr->form == DW_FORM_ref_sig8)
16016 die = follow_die_sig (src_die, attr, ref_cu);
16017 else
16018 {
16019 dump_die_for_error (src_die);
16020 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16021 (*ref_cu)->objfile->name);
16022 }
16023
16024 return die;
16025 }
16026
16027 /* Follow reference OFFSET.
16028 On entry *REF_CU is the CU of the source die referencing OFFSET.
16029 On exit *REF_CU is the CU of the result.
16030 Returns NULL if OFFSET is invalid. */
16031
16032 static struct die_info *
16033 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
16034 {
16035 struct die_info temp_die;
16036 struct dwarf2_cu *target_cu, *cu = *ref_cu;
16037
16038 gdb_assert (cu->per_cu != NULL);
16039
16040 target_cu = cu;
16041
16042 if (cu->per_cu->is_debug_types)
16043 {
16044 /* .debug_types CUs cannot reference anything outside their CU.
16045 If they need to, they have to reference a signatured type via
16046 DW_FORM_ref_sig8. */
16047 if (! offset_in_cu_p (&cu->header, offset))
16048 return NULL;
16049 }
16050 else if (! offset_in_cu_p (&cu->header, offset))
16051 {
16052 struct dwarf2_per_cu_data *per_cu;
16053
16054 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
16055
16056 /* If necessary, add it to the queue and load its DIEs. */
16057 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16058 load_full_comp_unit (per_cu, cu->language);
16059
16060 target_cu = per_cu->cu;
16061 }
16062 else if (cu->dies == NULL)
16063 {
16064 /* We're loading full DIEs during partial symbol reading. */
16065 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16066 load_full_comp_unit (cu->per_cu, language_minimal);
16067 }
16068
16069 *ref_cu = target_cu;
16070 temp_die.offset = offset;
16071 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16072 }
16073
16074 /* Follow reference attribute ATTR of SRC_DIE.
16075 On entry *REF_CU is the CU of SRC_DIE.
16076 On exit *REF_CU is the CU of the result. */
16077
16078 static struct die_info *
16079 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16080 struct dwarf2_cu **ref_cu)
16081 {
16082 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16083 struct dwarf2_cu *cu = *ref_cu;
16084 struct die_info *die;
16085
16086 die = follow_die_offset (offset, ref_cu);
16087 if (!die)
16088 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16089 "at 0x%x [in module %s]"),
16090 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16091
16092 return die;
16093 }
16094
16095 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16096 Returned value is intended for DW_OP_call*. Returned
16097 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
16098
16099 struct dwarf2_locexpr_baton
16100 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16101 struct dwarf2_per_cu_data *per_cu,
16102 CORE_ADDR (*get_frame_pc) (void *baton),
16103 void *baton)
16104 {
16105 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16106 struct dwarf2_cu *cu;
16107 struct die_info *die;
16108 struct attribute *attr;
16109 struct dwarf2_locexpr_baton retval;
16110
16111 dw2_setup (per_cu->objfile);
16112
16113 if (per_cu->cu == NULL)
16114 load_cu (per_cu);
16115 cu = per_cu->cu;
16116
16117 die = follow_die_offset (offset, &cu);
16118 if (!die)
16119 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16120 offset.sect_off, per_cu->objfile->name);
16121
16122 attr = dwarf2_attr (die, DW_AT_location, cu);
16123 if (!attr)
16124 {
16125 /* DWARF: "If there is no such attribute, then there is no effect.".
16126 DATA is ignored if SIZE is 0. */
16127
16128 retval.data = NULL;
16129 retval.size = 0;
16130 }
16131 else if (attr_form_is_section_offset (attr))
16132 {
16133 struct dwarf2_loclist_baton loclist_baton;
16134 CORE_ADDR pc = (*get_frame_pc) (baton);
16135 size_t size;
16136
16137 fill_in_loclist_baton (cu, &loclist_baton, attr);
16138
16139 retval.data = dwarf2_find_location_expression (&loclist_baton,
16140 &size, pc);
16141 retval.size = size;
16142 }
16143 else
16144 {
16145 if (!attr_form_is_block (attr))
16146 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16147 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16148 offset.sect_off, per_cu->objfile->name);
16149
16150 retval.data = DW_BLOCK (attr)->data;
16151 retval.size = DW_BLOCK (attr)->size;
16152 }
16153 retval.per_cu = cu->per_cu;
16154
16155 age_cached_comp_units ();
16156
16157 return retval;
16158 }
16159
16160 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16161 PER_CU. */
16162
16163 struct type *
16164 dwarf2_get_die_type (cu_offset die_offset,
16165 struct dwarf2_per_cu_data *per_cu)
16166 {
16167 sect_offset die_offset_sect;
16168
16169 dw2_setup (per_cu->objfile);
16170
16171 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16172 return get_die_type_at_offset (die_offset_sect, per_cu);
16173 }
16174
16175 /* Follow the signature attribute ATTR in SRC_DIE.
16176 On entry *REF_CU is the CU of SRC_DIE.
16177 On exit *REF_CU is the CU of the result. */
16178
16179 static struct die_info *
16180 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16181 struct dwarf2_cu **ref_cu)
16182 {
16183 struct objfile *objfile = (*ref_cu)->objfile;
16184 struct die_info temp_die;
16185 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16186 struct dwarf2_cu *sig_cu;
16187 struct die_info *die;
16188
16189 /* sig_type will be NULL if the signatured type is missing from
16190 the debug info. */
16191 if (sig_type == NULL)
16192 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16193 "at 0x%x [in module %s]"),
16194 src_die->offset.sect_off, objfile->name);
16195
16196 /* If necessary, add it to the queue and load its DIEs. */
16197
16198 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16199 read_signatured_type (sig_type);
16200
16201 gdb_assert (sig_type->per_cu.cu != NULL);
16202
16203 sig_cu = sig_type->per_cu.cu;
16204 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16205 temp_die.offset = sig_type->type_offset_in_section;
16206 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16207 temp_die.offset.sect_off);
16208 if (die)
16209 {
16210 *ref_cu = sig_cu;
16211 return die;
16212 }
16213
16214 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16215 "from DIE at 0x%x [in module %s]"),
16216 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16217 }
16218
16219 /* Given an offset of a signatured type, return its signatured_type. */
16220
16221 static struct signatured_type *
16222 lookup_signatured_type_at_offset (struct objfile *objfile,
16223 struct dwarf2_section_info *section,
16224 sect_offset offset)
16225 {
16226 gdb_byte *info_ptr = section->buffer + offset.sect_off;
16227 unsigned int length, initial_length_size;
16228 unsigned int sig_offset;
16229 struct signatured_type find_entry, *sig_type;
16230
16231 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16232 sig_offset = (initial_length_size
16233 + 2 /*version*/
16234 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16235 + 1 /*address_size*/);
16236 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16237 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16238
16239 /* This is only used to lookup previously recorded types.
16240 If we didn't find it, it's our bug. */
16241 gdb_assert (sig_type != NULL);
16242 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16243
16244 return sig_type;
16245 }
16246
16247 /* Load the DIEs associated with type unit PER_CU into memory. */
16248
16249 static void
16250 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16251 {
16252 struct signatured_type *sig_type;
16253
16254 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16255 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16256
16257 /* We have the per_cu, but we need the signatured_type.
16258 Fortunately this is an easy translation. */
16259 gdb_assert (per_cu->is_debug_types);
16260 sig_type = (struct signatured_type *) per_cu;
16261
16262 gdb_assert (per_cu->cu == NULL);
16263
16264 read_signatured_type (sig_type);
16265
16266 gdb_assert (per_cu->cu != NULL);
16267 }
16268
16269 /* die_reader_func for read_signatured_type.
16270 This is identical to load_full_comp_unit_reader,
16271 but is kept separate for now. */
16272
16273 static void
16274 read_signatured_type_reader (const struct die_reader_specs *reader,
16275 gdb_byte *info_ptr,
16276 struct die_info *comp_unit_die,
16277 int has_children,
16278 void *data)
16279 {
16280 struct dwarf2_cu *cu = reader->cu;
16281
16282 gdb_assert (cu->die_hash == NULL);
16283 cu->die_hash =
16284 htab_create_alloc_ex (cu->header.length / 12,
16285 die_hash,
16286 die_eq,
16287 NULL,
16288 &cu->comp_unit_obstack,
16289 hashtab_obstack_allocate,
16290 dummy_obstack_deallocate);
16291
16292 if (has_children)
16293 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16294 &info_ptr, comp_unit_die);
16295 cu->dies = comp_unit_die;
16296 /* comp_unit_die is not stored in die_hash, no need. */
16297
16298 /* We try not to read any attributes in this function, because not
16299 all CUs needed for references have been loaded yet, and symbol
16300 table processing isn't initialized. But we have to set the CU language,
16301 or we won't be able to build types correctly.
16302 Similarly, if we do not read the producer, we can not apply
16303 producer-specific interpretation. */
16304 prepare_one_comp_unit (cu, cu->dies, language_minimal);
16305 }
16306
16307 /* Read in a signatured type and build its CU and DIEs.
16308 If the type is a stub for the real type in a DWO file,
16309 read in the real type from the DWO file as well. */
16310
16311 static void
16312 read_signatured_type (struct signatured_type *sig_type)
16313 {
16314 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16315
16316 gdb_assert (per_cu->is_debug_types);
16317 gdb_assert (per_cu->cu == NULL);
16318
16319 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16320 read_signatured_type_reader, NULL);
16321 }
16322
16323 /* Decode simple location descriptions.
16324 Given a pointer to a dwarf block that defines a location, compute
16325 the location and return the value.
16326
16327 NOTE drow/2003-11-18: This function is called in two situations
16328 now: for the address of static or global variables (partial symbols
16329 only) and for offsets into structures which are expected to be
16330 (more or less) constant. The partial symbol case should go away,
16331 and only the constant case should remain. That will let this
16332 function complain more accurately. A few special modes are allowed
16333 without complaint for global variables (for instance, global
16334 register values and thread-local values).
16335
16336 A location description containing no operations indicates that the
16337 object is optimized out. The return value is 0 for that case.
16338 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16339 callers will only want a very basic result and this can become a
16340 complaint.
16341
16342 Note that stack[0] is unused except as a default error return. */
16343
16344 static CORE_ADDR
16345 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16346 {
16347 struct objfile *objfile = cu->objfile;
16348 int i;
16349 int size = blk->size;
16350 gdb_byte *data = blk->data;
16351 CORE_ADDR stack[64];
16352 int stacki;
16353 unsigned int bytes_read, unsnd;
16354 gdb_byte op;
16355
16356 i = 0;
16357 stacki = 0;
16358 stack[stacki] = 0;
16359 stack[++stacki] = 0;
16360
16361 while (i < size)
16362 {
16363 op = data[i++];
16364 switch (op)
16365 {
16366 case DW_OP_lit0:
16367 case DW_OP_lit1:
16368 case DW_OP_lit2:
16369 case DW_OP_lit3:
16370 case DW_OP_lit4:
16371 case DW_OP_lit5:
16372 case DW_OP_lit6:
16373 case DW_OP_lit7:
16374 case DW_OP_lit8:
16375 case DW_OP_lit9:
16376 case DW_OP_lit10:
16377 case DW_OP_lit11:
16378 case DW_OP_lit12:
16379 case DW_OP_lit13:
16380 case DW_OP_lit14:
16381 case DW_OP_lit15:
16382 case DW_OP_lit16:
16383 case DW_OP_lit17:
16384 case DW_OP_lit18:
16385 case DW_OP_lit19:
16386 case DW_OP_lit20:
16387 case DW_OP_lit21:
16388 case DW_OP_lit22:
16389 case DW_OP_lit23:
16390 case DW_OP_lit24:
16391 case DW_OP_lit25:
16392 case DW_OP_lit26:
16393 case DW_OP_lit27:
16394 case DW_OP_lit28:
16395 case DW_OP_lit29:
16396 case DW_OP_lit30:
16397 case DW_OP_lit31:
16398 stack[++stacki] = op - DW_OP_lit0;
16399 break;
16400
16401 case DW_OP_reg0:
16402 case DW_OP_reg1:
16403 case DW_OP_reg2:
16404 case DW_OP_reg3:
16405 case DW_OP_reg4:
16406 case DW_OP_reg5:
16407 case DW_OP_reg6:
16408 case DW_OP_reg7:
16409 case DW_OP_reg8:
16410 case DW_OP_reg9:
16411 case DW_OP_reg10:
16412 case DW_OP_reg11:
16413 case DW_OP_reg12:
16414 case DW_OP_reg13:
16415 case DW_OP_reg14:
16416 case DW_OP_reg15:
16417 case DW_OP_reg16:
16418 case DW_OP_reg17:
16419 case DW_OP_reg18:
16420 case DW_OP_reg19:
16421 case DW_OP_reg20:
16422 case DW_OP_reg21:
16423 case DW_OP_reg22:
16424 case DW_OP_reg23:
16425 case DW_OP_reg24:
16426 case DW_OP_reg25:
16427 case DW_OP_reg26:
16428 case DW_OP_reg27:
16429 case DW_OP_reg28:
16430 case DW_OP_reg29:
16431 case DW_OP_reg30:
16432 case DW_OP_reg31:
16433 stack[++stacki] = op - DW_OP_reg0;
16434 if (i < size)
16435 dwarf2_complex_location_expr_complaint ();
16436 break;
16437
16438 case DW_OP_regx:
16439 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16440 i += bytes_read;
16441 stack[++stacki] = unsnd;
16442 if (i < size)
16443 dwarf2_complex_location_expr_complaint ();
16444 break;
16445
16446 case DW_OP_addr:
16447 stack[++stacki] = read_address (objfile->obfd, &data[i],
16448 cu, &bytes_read);
16449 i += bytes_read;
16450 break;
16451
16452 case DW_OP_const1u:
16453 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16454 i += 1;
16455 break;
16456
16457 case DW_OP_const1s:
16458 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16459 i += 1;
16460 break;
16461
16462 case DW_OP_const2u:
16463 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16464 i += 2;
16465 break;
16466
16467 case DW_OP_const2s:
16468 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16469 i += 2;
16470 break;
16471
16472 case DW_OP_const4u:
16473 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16474 i += 4;
16475 break;
16476
16477 case DW_OP_const4s:
16478 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16479 i += 4;
16480 break;
16481
16482 case DW_OP_const8u:
16483 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16484 i += 8;
16485 break;
16486
16487 case DW_OP_constu:
16488 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16489 &bytes_read);
16490 i += bytes_read;
16491 break;
16492
16493 case DW_OP_consts:
16494 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16495 i += bytes_read;
16496 break;
16497
16498 case DW_OP_dup:
16499 stack[stacki + 1] = stack[stacki];
16500 stacki++;
16501 break;
16502
16503 case DW_OP_plus:
16504 stack[stacki - 1] += stack[stacki];
16505 stacki--;
16506 break;
16507
16508 case DW_OP_plus_uconst:
16509 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16510 &bytes_read);
16511 i += bytes_read;
16512 break;
16513
16514 case DW_OP_minus:
16515 stack[stacki - 1] -= stack[stacki];
16516 stacki--;
16517 break;
16518
16519 case DW_OP_deref:
16520 /* If we're not the last op, then we definitely can't encode
16521 this using GDB's address_class enum. This is valid for partial
16522 global symbols, although the variable's address will be bogus
16523 in the psymtab. */
16524 if (i < size)
16525 dwarf2_complex_location_expr_complaint ();
16526 break;
16527
16528 case DW_OP_GNU_push_tls_address:
16529 /* The top of the stack has the offset from the beginning
16530 of the thread control block at which the variable is located. */
16531 /* Nothing should follow this operator, so the top of stack would
16532 be returned. */
16533 /* This is valid for partial global symbols, but the variable's
16534 address will be bogus in the psymtab. Make it always at least
16535 non-zero to not look as a variable garbage collected by linker
16536 which have DW_OP_addr 0. */
16537 if (i < size)
16538 dwarf2_complex_location_expr_complaint ();
16539 stack[stacki]++;
16540 break;
16541
16542 case DW_OP_GNU_uninit:
16543 break;
16544
16545 case DW_OP_GNU_addr_index:
16546 case DW_OP_GNU_const_index:
16547 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16548 &bytes_read);
16549 i += bytes_read;
16550 break;
16551
16552 default:
16553 {
16554 const char *name = get_DW_OP_name (op);
16555
16556 if (name)
16557 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16558 name);
16559 else
16560 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16561 op);
16562 }
16563
16564 return (stack[stacki]);
16565 }
16566
16567 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16568 outside of the allocated space. Also enforce minimum>0. */
16569 if (stacki >= ARRAY_SIZE (stack) - 1)
16570 {
16571 complaint (&symfile_complaints,
16572 _("location description stack overflow"));
16573 return 0;
16574 }
16575
16576 if (stacki <= 0)
16577 {
16578 complaint (&symfile_complaints,
16579 _("location description stack underflow"));
16580 return 0;
16581 }
16582 }
16583 return (stack[stacki]);
16584 }
16585
16586 /* memory allocation interface */
16587
16588 static struct dwarf_block *
16589 dwarf_alloc_block (struct dwarf2_cu *cu)
16590 {
16591 struct dwarf_block *blk;
16592
16593 blk = (struct dwarf_block *)
16594 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16595 return (blk);
16596 }
16597
16598 static struct die_info *
16599 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
16600 {
16601 struct die_info *die;
16602 size_t size = sizeof (struct die_info);
16603
16604 if (num_attrs > 1)
16605 size += (num_attrs - 1) * sizeof (struct attribute);
16606
16607 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
16608 memset (die, 0, sizeof (struct die_info));
16609 return (die);
16610 }
16611
16612 \f
16613 /* Macro support. */
16614
16615 /* Return the full name of file number I in *LH's file name table.
16616 Use COMP_DIR as the name of the current directory of the
16617 compilation. The result is allocated using xmalloc; the caller is
16618 responsible for freeing it. */
16619 static char *
16620 file_full_name (int file, struct line_header *lh, const char *comp_dir)
16621 {
16622 /* Is the file number a valid index into the line header's file name
16623 table? Remember that file numbers start with one, not zero. */
16624 if (1 <= file && file <= lh->num_file_names)
16625 {
16626 struct file_entry *fe = &lh->file_names[file - 1];
16627
16628 if (IS_ABSOLUTE_PATH (fe->name))
16629 return xstrdup (fe->name);
16630 else
16631 {
16632 const char *dir;
16633 int dir_len;
16634 char *full_name;
16635
16636 if (fe->dir_index)
16637 dir = lh->include_dirs[fe->dir_index - 1];
16638 else
16639 dir = comp_dir;
16640
16641 if (dir)
16642 {
16643 dir_len = strlen (dir);
16644 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
16645 strcpy (full_name, dir);
16646 full_name[dir_len] = '/';
16647 strcpy (full_name + dir_len + 1, fe->name);
16648 return full_name;
16649 }
16650 else
16651 return xstrdup (fe->name);
16652 }
16653 }
16654 else
16655 {
16656 /* The compiler produced a bogus file number. We can at least
16657 record the macro definitions made in the file, even if we
16658 won't be able to find the file by name. */
16659 char fake_name[80];
16660
16661 sprintf (fake_name, "<bad macro file number %d>", file);
16662
16663 complaint (&symfile_complaints,
16664 _("bad file number in macro information (%d)"),
16665 file);
16666
16667 return xstrdup (fake_name);
16668 }
16669 }
16670
16671
16672 static struct macro_source_file *
16673 macro_start_file (int file, int line,
16674 struct macro_source_file *current_file,
16675 const char *comp_dir,
16676 struct line_header *lh, struct objfile *objfile)
16677 {
16678 /* The full name of this source file. */
16679 char *full_name = file_full_name (file, lh, comp_dir);
16680
16681 /* We don't create a macro table for this compilation unit
16682 at all until we actually get a filename. */
16683 if (! pending_macros)
16684 pending_macros = new_macro_table (&objfile->objfile_obstack,
16685 objfile->macro_cache);
16686
16687 if (! current_file)
16688 {
16689 /* If we have no current file, then this must be the start_file
16690 directive for the compilation unit's main source file. */
16691 current_file = macro_set_main (pending_macros, full_name);
16692 macro_define_special (pending_macros);
16693 }
16694 else
16695 current_file = macro_include (current_file, line, full_name);
16696
16697 xfree (full_name);
16698
16699 return current_file;
16700 }
16701
16702
16703 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
16704 followed by a null byte. */
16705 static char *
16706 copy_string (const char *buf, int len)
16707 {
16708 char *s = xmalloc (len + 1);
16709
16710 memcpy (s, buf, len);
16711 s[len] = '\0';
16712 return s;
16713 }
16714
16715
16716 static const char *
16717 consume_improper_spaces (const char *p, const char *body)
16718 {
16719 if (*p == ' ')
16720 {
16721 complaint (&symfile_complaints,
16722 _("macro definition contains spaces "
16723 "in formal argument list:\n`%s'"),
16724 body);
16725
16726 while (*p == ' ')
16727 p++;
16728 }
16729
16730 return p;
16731 }
16732
16733
16734 static void
16735 parse_macro_definition (struct macro_source_file *file, int line,
16736 const char *body)
16737 {
16738 const char *p;
16739
16740 /* The body string takes one of two forms. For object-like macro
16741 definitions, it should be:
16742
16743 <macro name> " " <definition>
16744
16745 For function-like macro definitions, it should be:
16746
16747 <macro name> "() " <definition>
16748 or
16749 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16750
16751 Spaces may appear only where explicitly indicated, and in the
16752 <definition>.
16753
16754 The Dwarf 2 spec says that an object-like macro's name is always
16755 followed by a space, but versions of GCC around March 2002 omit
16756 the space when the macro's definition is the empty string.
16757
16758 The Dwarf 2 spec says that there should be no spaces between the
16759 formal arguments in a function-like macro's formal argument list,
16760 but versions of GCC around March 2002 include spaces after the
16761 commas. */
16762
16763
16764 /* Find the extent of the macro name. The macro name is terminated
16765 by either a space or null character (for an object-like macro) or
16766 an opening paren (for a function-like macro). */
16767 for (p = body; *p; p++)
16768 if (*p == ' ' || *p == '(')
16769 break;
16770
16771 if (*p == ' ' || *p == '\0')
16772 {
16773 /* It's an object-like macro. */
16774 int name_len = p - body;
16775 char *name = copy_string (body, name_len);
16776 const char *replacement;
16777
16778 if (*p == ' ')
16779 replacement = body + name_len + 1;
16780 else
16781 {
16782 dwarf2_macro_malformed_definition_complaint (body);
16783 replacement = body + name_len;
16784 }
16785
16786 macro_define_object (file, line, name, replacement);
16787
16788 xfree (name);
16789 }
16790 else if (*p == '(')
16791 {
16792 /* It's a function-like macro. */
16793 char *name = copy_string (body, p - body);
16794 int argc = 0;
16795 int argv_size = 1;
16796 char **argv = xmalloc (argv_size * sizeof (*argv));
16797
16798 p++;
16799
16800 p = consume_improper_spaces (p, body);
16801
16802 /* Parse the formal argument list. */
16803 while (*p && *p != ')')
16804 {
16805 /* Find the extent of the current argument name. */
16806 const char *arg_start = p;
16807
16808 while (*p && *p != ',' && *p != ')' && *p != ' ')
16809 p++;
16810
16811 if (! *p || p == arg_start)
16812 dwarf2_macro_malformed_definition_complaint (body);
16813 else
16814 {
16815 /* Make sure argv has room for the new argument. */
16816 if (argc >= argv_size)
16817 {
16818 argv_size *= 2;
16819 argv = xrealloc (argv, argv_size * sizeof (*argv));
16820 }
16821
16822 argv[argc++] = copy_string (arg_start, p - arg_start);
16823 }
16824
16825 p = consume_improper_spaces (p, body);
16826
16827 /* Consume the comma, if present. */
16828 if (*p == ',')
16829 {
16830 p++;
16831
16832 p = consume_improper_spaces (p, body);
16833 }
16834 }
16835
16836 if (*p == ')')
16837 {
16838 p++;
16839
16840 if (*p == ' ')
16841 /* Perfectly formed definition, no complaints. */
16842 macro_define_function (file, line, name,
16843 argc, (const char **) argv,
16844 p + 1);
16845 else if (*p == '\0')
16846 {
16847 /* Complain, but do define it. */
16848 dwarf2_macro_malformed_definition_complaint (body);
16849 macro_define_function (file, line, name,
16850 argc, (const char **) argv,
16851 p);
16852 }
16853 else
16854 /* Just complain. */
16855 dwarf2_macro_malformed_definition_complaint (body);
16856 }
16857 else
16858 /* Just complain. */
16859 dwarf2_macro_malformed_definition_complaint (body);
16860
16861 xfree (name);
16862 {
16863 int i;
16864
16865 for (i = 0; i < argc; i++)
16866 xfree (argv[i]);
16867 }
16868 xfree (argv);
16869 }
16870 else
16871 dwarf2_macro_malformed_definition_complaint (body);
16872 }
16873
16874 /* Skip some bytes from BYTES according to the form given in FORM.
16875 Returns the new pointer. */
16876
16877 static gdb_byte *
16878 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
16879 enum dwarf_form form,
16880 unsigned int offset_size,
16881 struct dwarf2_section_info *section)
16882 {
16883 unsigned int bytes_read;
16884
16885 switch (form)
16886 {
16887 case DW_FORM_data1:
16888 case DW_FORM_flag:
16889 ++bytes;
16890 break;
16891
16892 case DW_FORM_data2:
16893 bytes += 2;
16894 break;
16895
16896 case DW_FORM_data4:
16897 bytes += 4;
16898 break;
16899
16900 case DW_FORM_data8:
16901 bytes += 8;
16902 break;
16903
16904 case DW_FORM_string:
16905 read_direct_string (abfd, bytes, &bytes_read);
16906 bytes += bytes_read;
16907 break;
16908
16909 case DW_FORM_sec_offset:
16910 case DW_FORM_strp:
16911 bytes += offset_size;
16912 break;
16913
16914 case DW_FORM_block:
16915 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16916 bytes += bytes_read;
16917 break;
16918
16919 case DW_FORM_block1:
16920 bytes += 1 + read_1_byte (abfd, bytes);
16921 break;
16922 case DW_FORM_block2:
16923 bytes += 2 + read_2_bytes (abfd, bytes);
16924 break;
16925 case DW_FORM_block4:
16926 bytes += 4 + read_4_bytes (abfd, bytes);
16927 break;
16928
16929 case DW_FORM_sdata:
16930 case DW_FORM_udata:
16931 case DW_FORM_GNU_addr_index:
16932 case DW_FORM_GNU_str_index:
16933 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16934 if (bytes == NULL)
16935 {
16936 dwarf2_section_buffer_overflow_complaint (section);
16937 return NULL;
16938 }
16939 break;
16940
16941 default:
16942 {
16943 complain:
16944 complaint (&symfile_complaints,
16945 _("invalid form 0x%x in `%s'"),
16946 form,
16947 section->asection->name);
16948 return NULL;
16949 }
16950 }
16951
16952 return bytes;
16953 }
16954
16955 /* A helper for dwarf_decode_macros that handles skipping an unknown
16956 opcode. Returns an updated pointer to the macro data buffer; or,
16957 on error, issues a complaint and returns NULL. */
16958
16959 static gdb_byte *
16960 skip_unknown_opcode (unsigned int opcode,
16961 gdb_byte **opcode_definitions,
16962 gdb_byte *mac_ptr, gdb_byte *mac_end,
16963 bfd *abfd,
16964 unsigned int offset_size,
16965 struct dwarf2_section_info *section)
16966 {
16967 unsigned int bytes_read, i;
16968 unsigned long arg;
16969 gdb_byte *defn;
16970
16971 if (opcode_definitions[opcode] == NULL)
16972 {
16973 complaint (&symfile_complaints,
16974 _("unrecognized DW_MACFINO opcode 0x%x"),
16975 opcode);
16976 return NULL;
16977 }
16978
16979 defn = opcode_definitions[opcode];
16980 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16981 defn += bytes_read;
16982
16983 for (i = 0; i < arg; ++i)
16984 {
16985 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16986 section);
16987 if (mac_ptr == NULL)
16988 {
16989 /* skip_form_bytes already issued the complaint. */
16990 return NULL;
16991 }
16992 }
16993
16994 return mac_ptr;
16995 }
16996
16997 /* A helper function which parses the header of a macro section.
16998 If the macro section is the extended (for now called "GNU") type,
16999 then this updates *OFFSET_SIZE. Returns a pointer to just after
17000 the header, or issues a complaint and returns NULL on error. */
17001
17002 static gdb_byte *
17003 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17004 bfd *abfd,
17005 gdb_byte *mac_ptr,
17006 unsigned int *offset_size,
17007 int section_is_gnu)
17008 {
17009 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17010
17011 if (section_is_gnu)
17012 {
17013 unsigned int version, flags;
17014
17015 version = read_2_bytes (abfd, mac_ptr);
17016 if (version != 4)
17017 {
17018 complaint (&symfile_complaints,
17019 _("unrecognized version `%d' in .debug_macro section"),
17020 version);
17021 return NULL;
17022 }
17023 mac_ptr += 2;
17024
17025 flags = read_1_byte (abfd, mac_ptr);
17026 ++mac_ptr;
17027 *offset_size = (flags & 1) ? 8 : 4;
17028
17029 if ((flags & 2) != 0)
17030 /* We don't need the line table offset. */
17031 mac_ptr += *offset_size;
17032
17033 /* Vendor opcode descriptions. */
17034 if ((flags & 4) != 0)
17035 {
17036 unsigned int i, count;
17037
17038 count = read_1_byte (abfd, mac_ptr);
17039 ++mac_ptr;
17040 for (i = 0; i < count; ++i)
17041 {
17042 unsigned int opcode, bytes_read;
17043 unsigned long arg;
17044
17045 opcode = read_1_byte (abfd, mac_ptr);
17046 ++mac_ptr;
17047 opcode_definitions[opcode] = mac_ptr;
17048 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17049 mac_ptr += bytes_read;
17050 mac_ptr += arg;
17051 }
17052 }
17053 }
17054
17055 return mac_ptr;
17056 }
17057
17058 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17059 including DW_MACRO_GNU_transparent_include. */
17060
17061 static void
17062 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17063 struct macro_source_file *current_file,
17064 struct line_header *lh, char *comp_dir,
17065 struct dwarf2_section_info *section,
17066 int section_is_gnu,
17067 unsigned int offset_size,
17068 struct objfile *objfile,
17069 htab_t include_hash)
17070 {
17071 enum dwarf_macro_record_type macinfo_type;
17072 int at_commandline;
17073 gdb_byte *opcode_definitions[256];
17074
17075 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17076 &offset_size, section_is_gnu);
17077 if (mac_ptr == NULL)
17078 {
17079 /* We already issued a complaint. */
17080 return;
17081 }
17082
17083 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17084 GDB is still reading the definitions from command line. First
17085 DW_MACINFO_start_file will need to be ignored as it was already executed
17086 to create CURRENT_FILE for the main source holding also the command line
17087 definitions. On first met DW_MACINFO_start_file this flag is reset to
17088 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17089
17090 at_commandline = 1;
17091
17092 do
17093 {
17094 /* Do we at least have room for a macinfo type byte? */
17095 if (mac_ptr >= mac_end)
17096 {
17097 dwarf2_section_buffer_overflow_complaint (section);
17098 break;
17099 }
17100
17101 macinfo_type = read_1_byte (abfd, mac_ptr);
17102 mac_ptr++;
17103
17104 /* Note that we rely on the fact that the corresponding GNU and
17105 DWARF constants are the same. */
17106 switch (macinfo_type)
17107 {
17108 /* A zero macinfo type indicates the end of the macro
17109 information. */
17110 case 0:
17111 break;
17112
17113 case DW_MACRO_GNU_define:
17114 case DW_MACRO_GNU_undef:
17115 case DW_MACRO_GNU_define_indirect:
17116 case DW_MACRO_GNU_undef_indirect:
17117 {
17118 unsigned int bytes_read;
17119 int line;
17120 char *body;
17121 int is_define;
17122
17123 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17124 mac_ptr += bytes_read;
17125
17126 if (macinfo_type == DW_MACRO_GNU_define
17127 || macinfo_type == DW_MACRO_GNU_undef)
17128 {
17129 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17130 mac_ptr += bytes_read;
17131 }
17132 else
17133 {
17134 LONGEST str_offset;
17135
17136 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17137 mac_ptr += offset_size;
17138
17139 body = read_indirect_string_at_offset (abfd, str_offset);
17140 }
17141
17142 is_define = (macinfo_type == DW_MACRO_GNU_define
17143 || macinfo_type == DW_MACRO_GNU_define_indirect);
17144 if (! current_file)
17145 {
17146 /* DWARF violation as no main source is present. */
17147 complaint (&symfile_complaints,
17148 _("debug info with no main source gives macro %s "
17149 "on line %d: %s"),
17150 is_define ? _("definition") : _("undefinition"),
17151 line, body);
17152 break;
17153 }
17154 if ((line == 0 && !at_commandline)
17155 || (line != 0 && at_commandline))
17156 complaint (&symfile_complaints,
17157 _("debug info gives %s macro %s with %s line %d: %s"),
17158 at_commandline ? _("command-line") : _("in-file"),
17159 is_define ? _("definition") : _("undefinition"),
17160 line == 0 ? _("zero") : _("non-zero"), line, body);
17161
17162 if (is_define)
17163 parse_macro_definition (current_file, line, body);
17164 else
17165 {
17166 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17167 || macinfo_type == DW_MACRO_GNU_undef_indirect);
17168 macro_undef (current_file, line, body);
17169 }
17170 }
17171 break;
17172
17173 case DW_MACRO_GNU_start_file:
17174 {
17175 unsigned int bytes_read;
17176 int line, file;
17177
17178 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17179 mac_ptr += bytes_read;
17180 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17181 mac_ptr += bytes_read;
17182
17183 if ((line == 0 && !at_commandline)
17184 || (line != 0 && at_commandline))
17185 complaint (&symfile_complaints,
17186 _("debug info gives source %d included "
17187 "from %s at %s line %d"),
17188 file, at_commandline ? _("command-line") : _("file"),
17189 line == 0 ? _("zero") : _("non-zero"), line);
17190
17191 if (at_commandline)
17192 {
17193 /* This DW_MACRO_GNU_start_file was executed in the
17194 pass one. */
17195 at_commandline = 0;
17196 }
17197 else
17198 current_file = macro_start_file (file, line,
17199 current_file, comp_dir,
17200 lh, objfile);
17201 }
17202 break;
17203
17204 case DW_MACRO_GNU_end_file:
17205 if (! current_file)
17206 complaint (&symfile_complaints,
17207 _("macro debug info has an unmatched "
17208 "`close_file' directive"));
17209 else
17210 {
17211 current_file = current_file->included_by;
17212 if (! current_file)
17213 {
17214 enum dwarf_macro_record_type next_type;
17215
17216 /* GCC circa March 2002 doesn't produce the zero
17217 type byte marking the end of the compilation
17218 unit. Complain if it's not there, but exit no
17219 matter what. */
17220
17221 /* Do we at least have room for a macinfo type byte? */
17222 if (mac_ptr >= mac_end)
17223 {
17224 dwarf2_section_buffer_overflow_complaint (section);
17225 return;
17226 }
17227
17228 /* We don't increment mac_ptr here, so this is just
17229 a look-ahead. */
17230 next_type = read_1_byte (abfd, mac_ptr);
17231 if (next_type != 0)
17232 complaint (&symfile_complaints,
17233 _("no terminating 0-type entry for "
17234 "macros in `.debug_macinfo' section"));
17235
17236 return;
17237 }
17238 }
17239 break;
17240
17241 case DW_MACRO_GNU_transparent_include:
17242 {
17243 LONGEST offset;
17244 void **slot;
17245
17246 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17247 mac_ptr += offset_size;
17248
17249 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17250 if (*slot != NULL)
17251 {
17252 /* This has actually happened; see
17253 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17254 complaint (&symfile_complaints,
17255 _("recursive DW_MACRO_GNU_transparent_include in "
17256 ".debug_macro section"));
17257 }
17258 else
17259 {
17260 *slot = mac_ptr;
17261
17262 dwarf_decode_macro_bytes (abfd,
17263 section->buffer + offset,
17264 mac_end, current_file,
17265 lh, comp_dir,
17266 section, section_is_gnu,
17267 offset_size, objfile, include_hash);
17268
17269 htab_remove_elt (include_hash, mac_ptr);
17270 }
17271 }
17272 break;
17273
17274 case DW_MACINFO_vendor_ext:
17275 if (!section_is_gnu)
17276 {
17277 unsigned int bytes_read;
17278 int constant;
17279
17280 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17281 mac_ptr += bytes_read;
17282 read_direct_string (abfd, mac_ptr, &bytes_read);
17283 mac_ptr += bytes_read;
17284
17285 /* We don't recognize any vendor extensions. */
17286 break;
17287 }
17288 /* FALLTHROUGH */
17289
17290 default:
17291 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17292 mac_ptr, mac_end, abfd, offset_size,
17293 section);
17294 if (mac_ptr == NULL)
17295 return;
17296 break;
17297 }
17298 } while (macinfo_type != 0);
17299 }
17300
17301 static void
17302 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17303 char *comp_dir, int section_is_gnu)
17304 {
17305 struct objfile *objfile = dwarf2_per_objfile->objfile;
17306 struct line_header *lh = cu->line_header;
17307 bfd *abfd;
17308 gdb_byte *mac_ptr, *mac_end;
17309 struct macro_source_file *current_file = 0;
17310 enum dwarf_macro_record_type macinfo_type;
17311 unsigned int offset_size = cu->header.offset_size;
17312 gdb_byte *opcode_definitions[256];
17313 struct cleanup *cleanup;
17314 htab_t include_hash;
17315 void **slot;
17316 struct dwarf2_section_info *section;
17317 const char *section_name;
17318
17319 if (cu->dwo_unit != NULL)
17320 {
17321 if (section_is_gnu)
17322 {
17323 section = &cu->dwo_unit->dwo_file->sections.macro;
17324 section_name = ".debug_macro.dwo";
17325 }
17326 else
17327 {
17328 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17329 section_name = ".debug_macinfo.dwo";
17330 }
17331 }
17332 else
17333 {
17334 if (section_is_gnu)
17335 {
17336 section = &dwarf2_per_objfile->macro;
17337 section_name = ".debug_macro";
17338 }
17339 else
17340 {
17341 section = &dwarf2_per_objfile->macinfo;
17342 section_name = ".debug_macinfo";
17343 }
17344 }
17345
17346 dwarf2_read_section (objfile, section);
17347 if (section->buffer == NULL)
17348 {
17349 complaint (&symfile_complaints, _("missing %s section"), section_name);
17350 return;
17351 }
17352 abfd = section->asection->owner;
17353
17354 /* First pass: Find the name of the base filename.
17355 This filename is needed in order to process all macros whose definition
17356 (or undefinition) comes from the command line. These macros are defined
17357 before the first DW_MACINFO_start_file entry, and yet still need to be
17358 associated to the base file.
17359
17360 To determine the base file name, we scan the macro definitions until we
17361 reach the first DW_MACINFO_start_file entry. We then initialize
17362 CURRENT_FILE accordingly so that any macro definition found before the
17363 first DW_MACINFO_start_file can still be associated to the base file. */
17364
17365 mac_ptr = section->buffer + offset;
17366 mac_end = section->buffer + section->size;
17367
17368 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17369 &offset_size, section_is_gnu);
17370 if (mac_ptr == NULL)
17371 {
17372 /* We already issued a complaint. */
17373 return;
17374 }
17375
17376 do
17377 {
17378 /* Do we at least have room for a macinfo type byte? */
17379 if (mac_ptr >= mac_end)
17380 {
17381 /* Complaint is printed during the second pass as GDB will probably
17382 stop the first pass earlier upon finding
17383 DW_MACINFO_start_file. */
17384 break;
17385 }
17386
17387 macinfo_type = read_1_byte (abfd, mac_ptr);
17388 mac_ptr++;
17389
17390 /* Note that we rely on the fact that the corresponding GNU and
17391 DWARF constants are the same. */
17392 switch (macinfo_type)
17393 {
17394 /* A zero macinfo type indicates the end of the macro
17395 information. */
17396 case 0:
17397 break;
17398
17399 case DW_MACRO_GNU_define:
17400 case DW_MACRO_GNU_undef:
17401 /* Only skip the data by MAC_PTR. */
17402 {
17403 unsigned int bytes_read;
17404
17405 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17406 mac_ptr += bytes_read;
17407 read_direct_string (abfd, mac_ptr, &bytes_read);
17408 mac_ptr += bytes_read;
17409 }
17410 break;
17411
17412 case DW_MACRO_GNU_start_file:
17413 {
17414 unsigned int bytes_read;
17415 int line, file;
17416
17417 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17418 mac_ptr += bytes_read;
17419 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17420 mac_ptr += bytes_read;
17421
17422 current_file = macro_start_file (file, line, current_file,
17423 comp_dir, lh, objfile);
17424 }
17425 break;
17426
17427 case DW_MACRO_GNU_end_file:
17428 /* No data to skip by MAC_PTR. */
17429 break;
17430
17431 case DW_MACRO_GNU_define_indirect:
17432 case DW_MACRO_GNU_undef_indirect:
17433 {
17434 unsigned int bytes_read;
17435
17436 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17437 mac_ptr += bytes_read;
17438 mac_ptr += offset_size;
17439 }
17440 break;
17441
17442 case DW_MACRO_GNU_transparent_include:
17443 /* Note that, according to the spec, a transparent include
17444 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17445 skip this opcode. */
17446 mac_ptr += offset_size;
17447 break;
17448
17449 case DW_MACINFO_vendor_ext:
17450 /* Only skip the data by MAC_PTR. */
17451 if (!section_is_gnu)
17452 {
17453 unsigned int bytes_read;
17454
17455 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17456 mac_ptr += bytes_read;
17457 read_direct_string (abfd, mac_ptr, &bytes_read);
17458 mac_ptr += bytes_read;
17459 }
17460 /* FALLTHROUGH */
17461
17462 default:
17463 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17464 mac_ptr, mac_end, abfd, offset_size,
17465 section);
17466 if (mac_ptr == NULL)
17467 return;
17468 break;
17469 }
17470 } while (macinfo_type != 0 && current_file == NULL);
17471
17472 /* Second pass: Process all entries.
17473
17474 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17475 command-line macro definitions/undefinitions. This flag is unset when we
17476 reach the first DW_MACINFO_start_file entry. */
17477
17478 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17479 NULL, xcalloc, xfree);
17480 cleanup = make_cleanup_htab_delete (include_hash);
17481 mac_ptr = section->buffer + offset;
17482 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17483 *slot = mac_ptr;
17484 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17485 current_file, lh, comp_dir, section, section_is_gnu,
17486 offset_size, objfile, include_hash);
17487 do_cleanups (cleanup);
17488 }
17489
17490 /* Check if the attribute's form is a DW_FORM_block*
17491 if so return true else false. */
17492
17493 static int
17494 attr_form_is_block (struct attribute *attr)
17495 {
17496 return (attr == NULL ? 0 :
17497 attr->form == DW_FORM_block1
17498 || attr->form == DW_FORM_block2
17499 || attr->form == DW_FORM_block4
17500 || attr->form == DW_FORM_block
17501 || attr->form == DW_FORM_exprloc);
17502 }
17503
17504 /* Return non-zero if ATTR's value is a section offset --- classes
17505 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17506 You may use DW_UNSND (attr) to retrieve such offsets.
17507
17508 Section 7.5.4, "Attribute Encodings", explains that no attribute
17509 may have a value that belongs to more than one of these classes; it
17510 would be ambiguous if we did, because we use the same forms for all
17511 of them. */
17512
17513 static int
17514 attr_form_is_section_offset (struct attribute *attr)
17515 {
17516 return (attr->form == DW_FORM_data4
17517 || attr->form == DW_FORM_data8
17518 || attr->form == DW_FORM_sec_offset);
17519 }
17520
17521 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17522 zero otherwise. When this function returns true, you can apply
17523 dwarf2_get_attr_constant_value to it.
17524
17525 However, note that for some attributes you must check
17526 attr_form_is_section_offset before using this test. DW_FORM_data4
17527 and DW_FORM_data8 are members of both the constant class, and of
17528 the classes that contain offsets into other debug sections
17529 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17530 that, if an attribute's can be either a constant or one of the
17531 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17532 taken as section offsets, not constants. */
17533
17534 static int
17535 attr_form_is_constant (struct attribute *attr)
17536 {
17537 switch (attr->form)
17538 {
17539 case DW_FORM_sdata:
17540 case DW_FORM_udata:
17541 case DW_FORM_data1:
17542 case DW_FORM_data2:
17543 case DW_FORM_data4:
17544 case DW_FORM_data8:
17545 return 1;
17546 default:
17547 return 0;
17548 }
17549 }
17550
17551 /* Return the .debug_loc section to use for CU.
17552 For DWO files use .debug_loc.dwo. */
17553
17554 static struct dwarf2_section_info *
17555 cu_debug_loc_section (struct dwarf2_cu *cu)
17556 {
17557 if (cu->dwo_unit)
17558 return &cu->dwo_unit->dwo_file->sections.loc;
17559 return &dwarf2_per_objfile->loc;
17560 }
17561
17562 /* A helper function that fills in a dwarf2_loclist_baton. */
17563
17564 static void
17565 fill_in_loclist_baton (struct dwarf2_cu *cu,
17566 struct dwarf2_loclist_baton *baton,
17567 struct attribute *attr)
17568 {
17569 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17570
17571 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17572
17573 baton->per_cu = cu->per_cu;
17574 gdb_assert (baton->per_cu);
17575 /* We don't know how long the location list is, but make sure we
17576 don't run off the edge of the section. */
17577 baton->size = section->size - DW_UNSND (attr);
17578 baton->data = section->buffer + DW_UNSND (attr);
17579 baton->base_address = cu->base_address;
17580 baton->from_dwo = cu->dwo_unit != NULL;
17581 }
17582
17583 static void
17584 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
17585 struct dwarf2_cu *cu)
17586 {
17587 struct objfile *objfile = dwarf2_per_objfile->objfile;
17588 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17589
17590 if (attr_form_is_section_offset (attr)
17591 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
17592 the section. If so, fall through to the complaint in the
17593 other branch. */
17594 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
17595 {
17596 struct dwarf2_loclist_baton *baton;
17597
17598 baton = obstack_alloc (&objfile->objfile_obstack,
17599 sizeof (struct dwarf2_loclist_baton));
17600
17601 fill_in_loclist_baton (cu, baton, attr);
17602
17603 if (cu->base_known == 0)
17604 complaint (&symfile_complaints,
17605 _("Location list used without "
17606 "specifying the CU base address."));
17607
17608 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
17609 SYMBOL_LOCATION_BATON (sym) = baton;
17610 }
17611 else
17612 {
17613 struct dwarf2_locexpr_baton *baton;
17614
17615 baton = obstack_alloc (&objfile->objfile_obstack,
17616 sizeof (struct dwarf2_locexpr_baton));
17617 baton->per_cu = cu->per_cu;
17618 gdb_assert (baton->per_cu);
17619
17620 if (attr_form_is_block (attr))
17621 {
17622 /* Note that we're just copying the block's data pointer
17623 here, not the actual data. We're still pointing into the
17624 info_buffer for SYM's objfile; right now we never release
17625 that buffer, but when we do clean up properly this may
17626 need to change. */
17627 baton->size = DW_BLOCK (attr)->size;
17628 baton->data = DW_BLOCK (attr)->data;
17629 }
17630 else
17631 {
17632 dwarf2_invalid_attrib_class_complaint ("location description",
17633 SYMBOL_NATURAL_NAME (sym));
17634 baton->size = 0;
17635 }
17636
17637 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
17638 SYMBOL_LOCATION_BATON (sym) = baton;
17639 }
17640 }
17641
17642 /* Return the OBJFILE associated with the compilation unit CU. If CU
17643 came from a separate debuginfo file, then the master objfile is
17644 returned. */
17645
17646 struct objfile *
17647 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
17648 {
17649 struct objfile *objfile = per_cu->objfile;
17650
17651 /* Return the master objfile, so that we can report and look up the
17652 correct file containing this variable. */
17653 if (objfile->separate_debug_objfile_backlink)
17654 objfile = objfile->separate_debug_objfile_backlink;
17655
17656 return objfile;
17657 }
17658
17659 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
17660 (CU_HEADERP is unused in such case) or prepare a temporary copy at
17661 CU_HEADERP first. */
17662
17663 static const struct comp_unit_head *
17664 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
17665 struct dwarf2_per_cu_data *per_cu)
17666 {
17667 gdb_byte *info_ptr;
17668
17669 if (per_cu->cu)
17670 return &per_cu->cu->header;
17671
17672 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
17673
17674 memset (cu_headerp, 0, sizeof (*cu_headerp));
17675 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
17676
17677 return cu_headerp;
17678 }
17679
17680 /* Return the address size given in the compilation unit header for CU. */
17681
17682 int
17683 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
17684 {
17685 struct comp_unit_head cu_header_local;
17686 const struct comp_unit_head *cu_headerp;
17687
17688 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17689
17690 return cu_headerp->addr_size;
17691 }
17692
17693 /* Return the offset size given in the compilation unit header for CU. */
17694
17695 int
17696 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
17697 {
17698 struct comp_unit_head cu_header_local;
17699 const struct comp_unit_head *cu_headerp;
17700
17701 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17702
17703 return cu_headerp->offset_size;
17704 }
17705
17706 /* See its dwarf2loc.h declaration. */
17707
17708 int
17709 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
17710 {
17711 struct comp_unit_head cu_header_local;
17712 const struct comp_unit_head *cu_headerp;
17713
17714 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17715
17716 if (cu_headerp->version == 2)
17717 return cu_headerp->addr_size;
17718 else
17719 return cu_headerp->offset_size;
17720 }
17721
17722 /* Return the text offset of the CU. The returned offset comes from
17723 this CU's objfile. If this objfile came from a separate debuginfo
17724 file, then the offset may be different from the corresponding
17725 offset in the parent objfile. */
17726
17727 CORE_ADDR
17728 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
17729 {
17730 struct objfile *objfile = per_cu->objfile;
17731
17732 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17733 }
17734
17735 /* Locate the .debug_info compilation unit from CU's objfile which contains
17736 the DIE at OFFSET. Raises an error on failure. */
17737
17738 static struct dwarf2_per_cu_data *
17739 dwarf2_find_containing_comp_unit (sect_offset offset,
17740 struct objfile *objfile)
17741 {
17742 struct dwarf2_per_cu_data *this_cu;
17743 int low, high;
17744
17745 low = 0;
17746 high = dwarf2_per_objfile->n_comp_units - 1;
17747 while (high > low)
17748 {
17749 int mid = low + (high - low) / 2;
17750
17751 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17752 >= offset.sect_off)
17753 high = mid;
17754 else
17755 low = mid + 1;
17756 }
17757 gdb_assert (low == high);
17758 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17759 > offset.sect_off)
17760 {
17761 if (low == 0)
17762 error (_("Dwarf Error: could not find partial DIE containing "
17763 "offset 0x%lx [in module %s]"),
17764 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
17765
17766 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17767 <= offset.sect_off);
17768 return dwarf2_per_objfile->all_comp_units[low-1];
17769 }
17770 else
17771 {
17772 this_cu = dwarf2_per_objfile->all_comp_units[low];
17773 if (low == dwarf2_per_objfile->n_comp_units - 1
17774 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17775 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17776 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
17777 return this_cu;
17778 }
17779 }
17780
17781 /* Initialize dwarf2_cu CU, owned by PER_CU. */
17782
17783 static void
17784 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
17785 {
17786 memset (cu, 0, sizeof (*cu));
17787 per_cu->cu = cu;
17788 cu->per_cu = per_cu;
17789 cu->objfile = per_cu->objfile;
17790 obstack_init (&cu->comp_unit_obstack);
17791 }
17792
17793 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17794
17795 static void
17796 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17797 enum language pretend_language)
17798 {
17799 struct attribute *attr;
17800
17801 /* Set the language we're debugging. */
17802 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17803 if (attr)
17804 set_cu_language (DW_UNSND (attr), cu);
17805 else
17806 {
17807 cu->language = pretend_language;
17808 cu->language_defn = language_def (cu->language);
17809 }
17810
17811 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17812 if (attr)
17813 cu->producer = DW_STRING (attr);
17814 }
17815
17816 /* Release one cached compilation unit, CU. We unlink it from the tree
17817 of compilation units, but we don't remove it from the read_in_chain;
17818 the caller is responsible for that.
17819 NOTE: DATA is a void * because this function is also used as a
17820 cleanup routine. */
17821
17822 static void
17823 free_heap_comp_unit (void *data)
17824 {
17825 struct dwarf2_cu *cu = data;
17826
17827 gdb_assert (cu->per_cu != NULL);
17828 cu->per_cu->cu = NULL;
17829 cu->per_cu = NULL;
17830
17831 obstack_free (&cu->comp_unit_obstack, NULL);
17832
17833 xfree (cu);
17834 }
17835
17836 /* This cleanup function is passed the address of a dwarf2_cu on the stack
17837 when we're finished with it. We can't free the pointer itself, but be
17838 sure to unlink it from the cache. Also release any associated storage. */
17839
17840 static void
17841 free_stack_comp_unit (void *data)
17842 {
17843 struct dwarf2_cu *cu = data;
17844
17845 gdb_assert (cu->per_cu != NULL);
17846 cu->per_cu->cu = NULL;
17847 cu->per_cu = NULL;
17848
17849 obstack_free (&cu->comp_unit_obstack, NULL);
17850 cu->partial_dies = NULL;
17851 }
17852
17853 /* Free all cached compilation units. */
17854
17855 static void
17856 free_cached_comp_units (void *data)
17857 {
17858 struct dwarf2_per_cu_data *per_cu, **last_chain;
17859
17860 per_cu = dwarf2_per_objfile->read_in_chain;
17861 last_chain = &dwarf2_per_objfile->read_in_chain;
17862 while (per_cu != NULL)
17863 {
17864 struct dwarf2_per_cu_data *next_cu;
17865
17866 next_cu = per_cu->cu->read_in_chain;
17867
17868 free_heap_comp_unit (per_cu->cu);
17869 *last_chain = next_cu;
17870
17871 per_cu = next_cu;
17872 }
17873 }
17874
17875 /* Increase the age counter on each cached compilation unit, and free
17876 any that are too old. */
17877
17878 static void
17879 age_cached_comp_units (void)
17880 {
17881 struct dwarf2_per_cu_data *per_cu, **last_chain;
17882
17883 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17884 per_cu = dwarf2_per_objfile->read_in_chain;
17885 while (per_cu != NULL)
17886 {
17887 per_cu->cu->last_used ++;
17888 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17889 dwarf2_mark (per_cu->cu);
17890 per_cu = per_cu->cu->read_in_chain;
17891 }
17892
17893 per_cu = dwarf2_per_objfile->read_in_chain;
17894 last_chain = &dwarf2_per_objfile->read_in_chain;
17895 while (per_cu != NULL)
17896 {
17897 struct dwarf2_per_cu_data *next_cu;
17898
17899 next_cu = per_cu->cu->read_in_chain;
17900
17901 if (!per_cu->cu->mark)
17902 {
17903 free_heap_comp_unit (per_cu->cu);
17904 *last_chain = next_cu;
17905 }
17906 else
17907 last_chain = &per_cu->cu->read_in_chain;
17908
17909 per_cu = next_cu;
17910 }
17911 }
17912
17913 /* Remove a single compilation unit from the cache. */
17914
17915 static void
17916 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17917 {
17918 struct dwarf2_per_cu_data *per_cu, **last_chain;
17919
17920 per_cu = dwarf2_per_objfile->read_in_chain;
17921 last_chain = &dwarf2_per_objfile->read_in_chain;
17922 while (per_cu != NULL)
17923 {
17924 struct dwarf2_per_cu_data *next_cu;
17925
17926 next_cu = per_cu->cu->read_in_chain;
17927
17928 if (per_cu == target_per_cu)
17929 {
17930 free_heap_comp_unit (per_cu->cu);
17931 per_cu->cu = NULL;
17932 *last_chain = next_cu;
17933 break;
17934 }
17935 else
17936 last_chain = &per_cu->cu->read_in_chain;
17937
17938 per_cu = next_cu;
17939 }
17940 }
17941
17942 /* Release all extra memory associated with OBJFILE. */
17943
17944 void
17945 dwarf2_free_objfile (struct objfile *objfile)
17946 {
17947 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17948
17949 if (dwarf2_per_objfile == NULL)
17950 return;
17951
17952 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17953 free_cached_comp_units (NULL);
17954
17955 if (dwarf2_per_objfile->quick_file_names_table)
17956 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17957
17958 /* Everything else should be on the objfile obstack. */
17959 }
17960
17961 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17962 We store these in a hash table separate from the DIEs, and preserve them
17963 when the DIEs are flushed out of cache.
17964
17965 The CU "per_cu" pointer is needed because offset alone is not enough to
17966 uniquely identify the type. A file may have multiple .debug_types sections,
17967 or the type may come from a DWO file. We have to use something in
17968 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17969 routine, get_die_type_at_offset, from outside this file, and thus won't
17970 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17971 of the objfile. */
17972
17973 struct dwarf2_per_cu_offset_and_type
17974 {
17975 const struct dwarf2_per_cu_data *per_cu;
17976 sect_offset offset;
17977 struct type *type;
17978 };
17979
17980 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17981
17982 static hashval_t
17983 per_cu_offset_and_type_hash (const void *item)
17984 {
17985 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17986
17987 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17988 }
17989
17990 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17991
17992 static int
17993 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17994 {
17995 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17996 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17997
17998 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17999 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18000 }
18001
18002 /* Set the type associated with DIE to TYPE. Save it in CU's hash
18003 table if necessary. For convenience, return TYPE.
18004
18005 The DIEs reading must have careful ordering to:
18006 * Not cause infite loops trying to read in DIEs as a prerequisite for
18007 reading current DIE.
18008 * Not trying to dereference contents of still incompletely read in types
18009 while reading in other DIEs.
18010 * Enable referencing still incompletely read in types just by a pointer to
18011 the type without accessing its fields.
18012
18013 Therefore caller should follow these rules:
18014 * Try to fetch any prerequisite types we may need to build this DIE type
18015 before building the type and calling set_die_type.
18016 * After building type call set_die_type for current DIE as soon as
18017 possible before fetching more types to complete the current type.
18018 * Make the type as complete as possible before fetching more types. */
18019
18020 static struct type *
18021 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18022 {
18023 struct dwarf2_per_cu_offset_and_type **slot, ofs;
18024 struct objfile *objfile = cu->objfile;
18025
18026 /* For Ada types, make sure that the gnat-specific data is always
18027 initialized (if not already set). There are a few types where
18028 we should not be doing so, because the type-specific area is
18029 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18030 where the type-specific area is used to store the floatformat).
18031 But this is not a problem, because the gnat-specific information
18032 is actually not needed for these types. */
18033 if (need_gnat_info (cu)
18034 && TYPE_CODE (type) != TYPE_CODE_FUNC
18035 && TYPE_CODE (type) != TYPE_CODE_FLT
18036 && !HAVE_GNAT_AUX_INFO (type))
18037 INIT_GNAT_SPECIFIC (type);
18038
18039 if (dwarf2_per_objfile->die_type_hash == NULL)
18040 {
18041 dwarf2_per_objfile->die_type_hash =
18042 htab_create_alloc_ex (127,
18043 per_cu_offset_and_type_hash,
18044 per_cu_offset_and_type_eq,
18045 NULL,
18046 &objfile->objfile_obstack,
18047 hashtab_obstack_allocate,
18048 dummy_obstack_deallocate);
18049 }
18050
18051 ofs.per_cu = cu->per_cu;
18052 ofs.offset = die->offset;
18053 ofs.type = type;
18054 slot = (struct dwarf2_per_cu_offset_and_type **)
18055 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18056 if (*slot)
18057 complaint (&symfile_complaints,
18058 _("A problem internal to GDB: DIE 0x%x has type already set"),
18059 die->offset.sect_off);
18060 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18061 **slot = ofs;
18062 return type;
18063 }
18064
18065 /* Look up the type for the die at OFFSET in the appropriate type_hash
18066 table, or return NULL if the die does not have a saved type. */
18067
18068 static struct type *
18069 get_die_type_at_offset (sect_offset offset,
18070 struct dwarf2_per_cu_data *per_cu)
18071 {
18072 struct dwarf2_per_cu_offset_and_type *slot, ofs;
18073
18074 if (dwarf2_per_objfile->die_type_hash == NULL)
18075 return NULL;
18076
18077 ofs.per_cu = per_cu;
18078 ofs.offset = offset;
18079 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18080 if (slot)
18081 return slot->type;
18082 else
18083 return NULL;
18084 }
18085
18086 /* Look up the type for DIE in the appropriate type_hash table,
18087 or return NULL if DIE does not have a saved type. */
18088
18089 static struct type *
18090 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18091 {
18092 return get_die_type_at_offset (die->offset, cu->per_cu);
18093 }
18094
18095 /* Add a dependence relationship from CU to REF_PER_CU. */
18096
18097 static void
18098 dwarf2_add_dependence (struct dwarf2_cu *cu,
18099 struct dwarf2_per_cu_data *ref_per_cu)
18100 {
18101 void **slot;
18102
18103 if (cu->dependencies == NULL)
18104 cu->dependencies
18105 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18106 NULL, &cu->comp_unit_obstack,
18107 hashtab_obstack_allocate,
18108 dummy_obstack_deallocate);
18109
18110 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18111 if (*slot == NULL)
18112 *slot = ref_per_cu;
18113 }
18114
18115 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18116 Set the mark field in every compilation unit in the
18117 cache that we must keep because we are keeping CU. */
18118
18119 static int
18120 dwarf2_mark_helper (void **slot, void *data)
18121 {
18122 struct dwarf2_per_cu_data *per_cu;
18123
18124 per_cu = (struct dwarf2_per_cu_data *) *slot;
18125
18126 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18127 reading of the chain. As such dependencies remain valid it is not much
18128 useful to track and undo them during QUIT cleanups. */
18129 if (per_cu->cu == NULL)
18130 return 1;
18131
18132 if (per_cu->cu->mark)
18133 return 1;
18134 per_cu->cu->mark = 1;
18135
18136 if (per_cu->cu->dependencies != NULL)
18137 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18138
18139 return 1;
18140 }
18141
18142 /* Set the mark field in CU and in every other compilation unit in the
18143 cache that we must keep because we are keeping CU. */
18144
18145 static void
18146 dwarf2_mark (struct dwarf2_cu *cu)
18147 {
18148 if (cu->mark)
18149 return;
18150 cu->mark = 1;
18151 if (cu->dependencies != NULL)
18152 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18153 }
18154
18155 static void
18156 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18157 {
18158 while (per_cu)
18159 {
18160 per_cu->cu->mark = 0;
18161 per_cu = per_cu->cu->read_in_chain;
18162 }
18163 }
18164
18165 /* Trivial hash function for partial_die_info: the hash value of a DIE
18166 is its offset in .debug_info for this objfile. */
18167
18168 static hashval_t
18169 partial_die_hash (const void *item)
18170 {
18171 const struct partial_die_info *part_die = item;
18172
18173 return part_die->offset.sect_off;
18174 }
18175
18176 /* Trivial comparison function for partial_die_info structures: two DIEs
18177 are equal if they have the same offset. */
18178
18179 static int
18180 partial_die_eq (const void *item_lhs, const void *item_rhs)
18181 {
18182 const struct partial_die_info *part_die_lhs = item_lhs;
18183 const struct partial_die_info *part_die_rhs = item_rhs;
18184
18185 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18186 }
18187
18188 static struct cmd_list_element *set_dwarf2_cmdlist;
18189 static struct cmd_list_element *show_dwarf2_cmdlist;
18190
18191 static void
18192 set_dwarf2_cmd (char *args, int from_tty)
18193 {
18194 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18195 }
18196
18197 static void
18198 show_dwarf2_cmd (char *args, int from_tty)
18199 {
18200 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18201 }
18202
18203 /* Free data associated with OBJFILE, if necessary. */
18204
18205 static void
18206 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18207 {
18208 struct dwarf2_per_objfile *data = d;
18209 int ix;
18210
18211 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18212 VEC_free (dwarf2_per_cu_ptr,
18213 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18214
18215 VEC_free (dwarf2_section_info_def, data->types);
18216
18217 if (data->dwo_files)
18218 free_dwo_files (data->dwo_files, objfile);
18219 }
18220
18221 \f
18222 /* The "save gdb-index" command. */
18223
18224 /* The contents of the hash table we create when building the string
18225 table. */
18226 struct strtab_entry
18227 {
18228 offset_type offset;
18229 const char *str;
18230 };
18231
18232 /* Hash function for a strtab_entry.
18233
18234 Function is used only during write_hash_table so no index format backward
18235 compatibility is needed. */
18236
18237 static hashval_t
18238 hash_strtab_entry (const void *e)
18239 {
18240 const struct strtab_entry *entry = e;
18241 return mapped_index_string_hash (INT_MAX, entry->str);
18242 }
18243
18244 /* Equality function for a strtab_entry. */
18245
18246 static int
18247 eq_strtab_entry (const void *a, const void *b)
18248 {
18249 const struct strtab_entry *ea = a;
18250 const struct strtab_entry *eb = b;
18251 return !strcmp (ea->str, eb->str);
18252 }
18253
18254 /* Create a strtab_entry hash table. */
18255
18256 static htab_t
18257 create_strtab (void)
18258 {
18259 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18260 xfree, xcalloc, xfree);
18261 }
18262
18263 /* Add a string to the constant pool. Return the string's offset in
18264 host order. */
18265
18266 static offset_type
18267 add_string (htab_t table, struct obstack *cpool, const char *str)
18268 {
18269 void **slot;
18270 struct strtab_entry entry;
18271 struct strtab_entry *result;
18272
18273 entry.str = str;
18274 slot = htab_find_slot (table, &entry, INSERT);
18275 if (*slot)
18276 result = *slot;
18277 else
18278 {
18279 result = XNEW (struct strtab_entry);
18280 result->offset = obstack_object_size (cpool);
18281 result->str = str;
18282 obstack_grow_str0 (cpool, str);
18283 *slot = result;
18284 }
18285 return result->offset;
18286 }
18287
18288 /* An entry in the symbol table. */
18289 struct symtab_index_entry
18290 {
18291 /* The name of the symbol. */
18292 const char *name;
18293 /* The offset of the name in the constant pool. */
18294 offset_type index_offset;
18295 /* A sorted vector of the indices of all the CUs that hold an object
18296 of this name. */
18297 VEC (offset_type) *cu_indices;
18298 };
18299
18300 /* The symbol table. This is a power-of-2-sized hash table. */
18301 struct mapped_symtab
18302 {
18303 offset_type n_elements;
18304 offset_type size;
18305 struct symtab_index_entry **data;
18306 };
18307
18308 /* Hash function for a symtab_index_entry. */
18309
18310 static hashval_t
18311 hash_symtab_entry (const void *e)
18312 {
18313 const struct symtab_index_entry *entry = e;
18314 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18315 sizeof (offset_type) * VEC_length (offset_type,
18316 entry->cu_indices),
18317 0);
18318 }
18319
18320 /* Equality function for a symtab_index_entry. */
18321
18322 static int
18323 eq_symtab_entry (const void *a, const void *b)
18324 {
18325 const struct symtab_index_entry *ea = a;
18326 const struct symtab_index_entry *eb = b;
18327 int len = VEC_length (offset_type, ea->cu_indices);
18328 if (len != VEC_length (offset_type, eb->cu_indices))
18329 return 0;
18330 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18331 VEC_address (offset_type, eb->cu_indices),
18332 sizeof (offset_type) * len);
18333 }
18334
18335 /* Destroy a symtab_index_entry. */
18336
18337 static void
18338 delete_symtab_entry (void *p)
18339 {
18340 struct symtab_index_entry *entry = p;
18341 VEC_free (offset_type, entry->cu_indices);
18342 xfree (entry);
18343 }
18344
18345 /* Create a hash table holding symtab_index_entry objects. */
18346
18347 static htab_t
18348 create_symbol_hash_table (void)
18349 {
18350 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18351 delete_symtab_entry, xcalloc, xfree);
18352 }
18353
18354 /* Create a new mapped symtab object. */
18355
18356 static struct mapped_symtab *
18357 create_mapped_symtab (void)
18358 {
18359 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18360 symtab->n_elements = 0;
18361 symtab->size = 1024;
18362 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18363 return symtab;
18364 }
18365
18366 /* Destroy a mapped_symtab. */
18367
18368 static void
18369 cleanup_mapped_symtab (void *p)
18370 {
18371 struct mapped_symtab *symtab = p;
18372 /* The contents of the array are freed when the other hash table is
18373 destroyed. */
18374 xfree (symtab->data);
18375 xfree (symtab);
18376 }
18377
18378 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
18379 the slot.
18380
18381 Function is used only during write_hash_table so no index format backward
18382 compatibility is needed. */
18383
18384 static struct symtab_index_entry **
18385 find_slot (struct mapped_symtab *symtab, const char *name)
18386 {
18387 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18388
18389 index = hash & (symtab->size - 1);
18390 step = ((hash * 17) & (symtab->size - 1)) | 1;
18391
18392 for (;;)
18393 {
18394 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18395 return &symtab->data[index];
18396 index = (index + step) & (symtab->size - 1);
18397 }
18398 }
18399
18400 /* Expand SYMTAB's hash table. */
18401
18402 static void
18403 hash_expand (struct mapped_symtab *symtab)
18404 {
18405 offset_type old_size = symtab->size;
18406 offset_type i;
18407 struct symtab_index_entry **old_entries = symtab->data;
18408
18409 symtab->size *= 2;
18410 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18411
18412 for (i = 0; i < old_size; ++i)
18413 {
18414 if (old_entries[i])
18415 {
18416 struct symtab_index_entry **slot = find_slot (symtab,
18417 old_entries[i]->name);
18418 *slot = old_entries[i];
18419 }
18420 }
18421
18422 xfree (old_entries);
18423 }
18424
18425 /* Add an entry to SYMTAB. NAME is the name of the symbol.
18426 CU_INDEX is the index of the CU in which the symbol appears.
18427 IS_STATIC is one if the symbol is static, otherwise zero (global). */
18428
18429 static void
18430 add_index_entry (struct mapped_symtab *symtab, const char *name,
18431 int is_static, gdb_index_symbol_kind kind,
18432 offset_type cu_index)
18433 {
18434 struct symtab_index_entry **slot;
18435 offset_type cu_index_and_attrs;
18436
18437 ++symtab->n_elements;
18438 if (4 * symtab->n_elements / 3 >= symtab->size)
18439 hash_expand (symtab);
18440
18441 slot = find_slot (symtab, name);
18442 if (!*slot)
18443 {
18444 *slot = XNEW (struct symtab_index_entry);
18445 (*slot)->name = name;
18446 /* index_offset is set later. */
18447 (*slot)->cu_indices = NULL;
18448 }
18449
18450 cu_index_and_attrs = 0;
18451 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18452 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18453 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18454
18455 /* We don't want to record an index value twice as we want to avoid the
18456 duplication.
18457 We process all global symbols and then all static symbols
18458 (which would allow us to avoid the duplication by only having to check
18459 the last entry pushed), but a symbol could have multiple kinds in one CU.
18460 To keep things simple we don't worry about the duplication here and
18461 sort and uniqufy the list after we've processed all symbols. */
18462 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18463 }
18464
18465 /* qsort helper routine for uniquify_cu_indices. */
18466
18467 static int
18468 offset_type_compare (const void *ap, const void *bp)
18469 {
18470 offset_type a = *(offset_type *) ap;
18471 offset_type b = *(offset_type *) bp;
18472
18473 return (a > b) - (b > a);
18474 }
18475
18476 /* Sort and remove duplicates of all symbols' cu_indices lists. */
18477
18478 static void
18479 uniquify_cu_indices (struct mapped_symtab *symtab)
18480 {
18481 int i;
18482
18483 for (i = 0; i < symtab->size; ++i)
18484 {
18485 struct symtab_index_entry *entry = symtab->data[i];
18486
18487 if (entry
18488 && entry->cu_indices != NULL)
18489 {
18490 unsigned int next_to_insert, next_to_check;
18491 offset_type last_value;
18492
18493 qsort (VEC_address (offset_type, entry->cu_indices),
18494 VEC_length (offset_type, entry->cu_indices),
18495 sizeof (offset_type), offset_type_compare);
18496
18497 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18498 next_to_insert = 1;
18499 for (next_to_check = 1;
18500 next_to_check < VEC_length (offset_type, entry->cu_indices);
18501 ++next_to_check)
18502 {
18503 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18504 != last_value)
18505 {
18506 last_value = VEC_index (offset_type, entry->cu_indices,
18507 next_to_check);
18508 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18509 last_value);
18510 ++next_to_insert;
18511 }
18512 }
18513 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18514 }
18515 }
18516 }
18517
18518 /* Add a vector of indices to the constant pool. */
18519
18520 static offset_type
18521 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18522 struct symtab_index_entry *entry)
18523 {
18524 void **slot;
18525
18526 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18527 if (!*slot)
18528 {
18529 offset_type len = VEC_length (offset_type, entry->cu_indices);
18530 offset_type val = MAYBE_SWAP (len);
18531 offset_type iter;
18532 int i;
18533
18534 *slot = entry;
18535 entry->index_offset = obstack_object_size (cpool);
18536
18537 obstack_grow (cpool, &val, sizeof (val));
18538 for (i = 0;
18539 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18540 ++i)
18541 {
18542 val = MAYBE_SWAP (iter);
18543 obstack_grow (cpool, &val, sizeof (val));
18544 }
18545 }
18546 else
18547 {
18548 struct symtab_index_entry *old_entry = *slot;
18549 entry->index_offset = old_entry->index_offset;
18550 entry = old_entry;
18551 }
18552 return entry->index_offset;
18553 }
18554
18555 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
18556 constant pool entries going into the obstack CPOOL. */
18557
18558 static void
18559 write_hash_table (struct mapped_symtab *symtab,
18560 struct obstack *output, struct obstack *cpool)
18561 {
18562 offset_type i;
18563 htab_t symbol_hash_table;
18564 htab_t str_table;
18565
18566 symbol_hash_table = create_symbol_hash_table ();
18567 str_table = create_strtab ();
18568
18569 /* We add all the index vectors to the constant pool first, to
18570 ensure alignment is ok. */
18571 for (i = 0; i < symtab->size; ++i)
18572 {
18573 if (symtab->data[i])
18574 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
18575 }
18576
18577 /* Now write out the hash table. */
18578 for (i = 0; i < symtab->size; ++i)
18579 {
18580 offset_type str_off, vec_off;
18581
18582 if (symtab->data[i])
18583 {
18584 str_off = add_string (str_table, cpool, symtab->data[i]->name);
18585 vec_off = symtab->data[i]->index_offset;
18586 }
18587 else
18588 {
18589 /* While 0 is a valid constant pool index, it is not valid
18590 to have 0 for both offsets. */
18591 str_off = 0;
18592 vec_off = 0;
18593 }
18594
18595 str_off = MAYBE_SWAP (str_off);
18596 vec_off = MAYBE_SWAP (vec_off);
18597
18598 obstack_grow (output, &str_off, sizeof (str_off));
18599 obstack_grow (output, &vec_off, sizeof (vec_off));
18600 }
18601
18602 htab_delete (str_table);
18603 htab_delete (symbol_hash_table);
18604 }
18605
18606 /* Struct to map psymtab to CU index in the index file. */
18607 struct psymtab_cu_index_map
18608 {
18609 struct partial_symtab *psymtab;
18610 unsigned int cu_index;
18611 };
18612
18613 static hashval_t
18614 hash_psymtab_cu_index (const void *item)
18615 {
18616 const struct psymtab_cu_index_map *map = item;
18617
18618 return htab_hash_pointer (map->psymtab);
18619 }
18620
18621 static int
18622 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
18623 {
18624 const struct psymtab_cu_index_map *lhs = item_lhs;
18625 const struct psymtab_cu_index_map *rhs = item_rhs;
18626
18627 return lhs->psymtab == rhs->psymtab;
18628 }
18629
18630 /* Helper struct for building the address table. */
18631 struct addrmap_index_data
18632 {
18633 struct objfile *objfile;
18634 struct obstack *addr_obstack;
18635 htab_t cu_index_htab;
18636
18637 /* Non-zero if the previous_* fields are valid.
18638 We can't write an entry until we see the next entry (since it is only then
18639 that we know the end of the entry). */
18640 int previous_valid;
18641 /* Index of the CU in the table of all CUs in the index file. */
18642 unsigned int previous_cu_index;
18643 /* Start address of the CU. */
18644 CORE_ADDR previous_cu_start;
18645 };
18646
18647 /* Write an address entry to OBSTACK. */
18648
18649 static void
18650 add_address_entry (struct objfile *objfile, struct obstack *obstack,
18651 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
18652 {
18653 offset_type cu_index_to_write;
18654 char addr[8];
18655 CORE_ADDR baseaddr;
18656
18657 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18658
18659 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
18660 obstack_grow (obstack, addr, 8);
18661 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
18662 obstack_grow (obstack, addr, 8);
18663 cu_index_to_write = MAYBE_SWAP (cu_index);
18664 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
18665 }
18666
18667 /* Worker function for traversing an addrmap to build the address table. */
18668
18669 static int
18670 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
18671 {
18672 struct addrmap_index_data *data = datap;
18673 struct partial_symtab *pst = obj;
18674
18675 if (data->previous_valid)
18676 add_address_entry (data->objfile, data->addr_obstack,
18677 data->previous_cu_start, start_addr,
18678 data->previous_cu_index);
18679
18680 data->previous_cu_start = start_addr;
18681 if (pst != NULL)
18682 {
18683 struct psymtab_cu_index_map find_map, *map;
18684 find_map.psymtab = pst;
18685 map = htab_find (data->cu_index_htab, &find_map);
18686 gdb_assert (map != NULL);
18687 data->previous_cu_index = map->cu_index;
18688 data->previous_valid = 1;
18689 }
18690 else
18691 data->previous_valid = 0;
18692
18693 return 0;
18694 }
18695
18696 /* Write OBJFILE's address map to OBSTACK.
18697 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18698 in the index file. */
18699
18700 static void
18701 write_address_map (struct objfile *objfile, struct obstack *obstack,
18702 htab_t cu_index_htab)
18703 {
18704 struct addrmap_index_data addrmap_index_data;
18705
18706 /* When writing the address table, we have to cope with the fact that
18707 the addrmap iterator only provides the start of a region; we have to
18708 wait until the next invocation to get the start of the next region. */
18709
18710 addrmap_index_data.objfile = objfile;
18711 addrmap_index_data.addr_obstack = obstack;
18712 addrmap_index_data.cu_index_htab = cu_index_htab;
18713 addrmap_index_data.previous_valid = 0;
18714
18715 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18716 &addrmap_index_data);
18717
18718 /* It's highly unlikely the last entry (end address = 0xff...ff)
18719 is valid, but we should still handle it.
18720 The end address is recorded as the start of the next region, but that
18721 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18722 anyway. */
18723 if (addrmap_index_data.previous_valid)
18724 add_address_entry (objfile, obstack,
18725 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18726 addrmap_index_data.previous_cu_index);
18727 }
18728
18729 /* Return the symbol kind of PSYM. */
18730
18731 static gdb_index_symbol_kind
18732 symbol_kind (struct partial_symbol *psym)
18733 {
18734 domain_enum domain = PSYMBOL_DOMAIN (psym);
18735 enum address_class aclass = PSYMBOL_CLASS (psym);
18736
18737 switch (domain)
18738 {
18739 case VAR_DOMAIN:
18740 switch (aclass)
18741 {
18742 case LOC_BLOCK:
18743 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
18744 case LOC_TYPEDEF:
18745 return GDB_INDEX_SYMBOL_KIND_TYPE;
18746 case LOC_COMPUTED:
18747 case LOC_CONST_BYTES:
18748 case LOC_OPTIMIZED_OUT:
18749 case LOC_STATIC:
18750 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18751 case LOC_CONST:
18752 /* Note: It's currently impossible to recognize psyms as enum values
18753 short of reading the type info. For now punt. */
18754 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18755 default:
18756 /* There are other LOC_FOO values that one might want to classify
18757 as variables, but dwarf2read.c doesn't currently use them. */
18758 return GDB_INDEX_SYMBOL_KIND_OTHER;
18759 }
18760 case STRUCT_DOMAIN:
18761 return GDB_INDEX_SYMBOL_KIND_TYPE;
18762 default:
18763 return GDB_INDEX_SYMBOL_KIND_OTHER;
18764 }
18765 }
18766
18767 /* Add a list of partial symbols to SYMTAB. */
18768
18769 static void
18770 write_psymbols (struct mapped_symtab *symtab,
18771 htab_t psyms_seen,
18772 struct partial_symbol **psymp,
18773 int count,
18774 offset_type cu_index,
18775 int is_static)
18776 {
18777 for (; count-- > 0; ++psymp)
18778 {
18779 struct partial_symbol *psym = *psymp;
18780 void **slot;
18781
18782 if (SYMBOL_LANGUAGE (psym) == language_ada)
18783 error (_("Ada is not currently supported by the index"));
18784
18785 /* Only add a given psymbol once. */
18786 slot = htab_find_slot (psyms_seen, psym, INSERT);
18787 if (!*slot)
18788 {
18789 gdb_index_symbol_kind kind = symbol_kind (psym);
18790
18791 *slot = psym;
18792 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18793 is_static, kind, cu_index);
18794 }
18795 }
18796 }
18797
18798 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
18799 exception if there is an error. */
18800
18801 static void
18802 write_obstack (FILE *file, struct obstack *obstack)
18803 {
18804 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18805 file)
18806 != obstack_object_size (obstack))
18807 error (_("couldn't data write to file"));
18808 }
18809
18810 /* Unlink a file if the argument is not NULL. */
18811
18812 static void
18813 unlink_if_set (void *p)
18814 {
18815 char **filename = p;
18816 if (*filename)
18817 unlink (*filename);
18818 }
18819
18820 /* A helper struct used when iterating over debug_types. */
18821 struct signatured_type_index_data
18822 {
18823 struct objfile *objfile;
18824 struct mapped_symtab *symtab;
18825 struct obstack *types_list;
18826 htab_t psyms_seen;
18827 int cu_index;
18828 };
18829
18830 /* A helper function that writes a single signatured_type to an
18831 obstack. */
18832
18833 static int
18834 write_one_signatured_type (void **slot, void *d)
18835 {
18836 struct signatured_type_index_data *info = d;
18837 struct signatured_type *entry = (struct signatured_type *) *slot;
18838 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18839 struct partial_symtab *psymtab = per_cu->v.psymtab;
18840 gdb_byte val[8];
18841
18842 write_psymbols (info->symtab,
18843 info->psyms_seen,
18844 info->objfile->global_psymbols.list
18845 + psymtab->globals_offset,
18846 psymtab->n_global_syms, info->cu_index,
18847 0);
18848 write_psymbols (info->symtab,
18849 info->psyms_seen,
18850 info->objfile->static_psymbols.list
18851 + psymtab->statics_offset,
18852 psymtab->n_static_syms, info->cu_index,
18853 1);
18854
18855 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18856 entry->per_cu.offset.sect_off);
18857 obstack_grow (info->types_list, val, 8);
18858 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18859 entry->type_offset_in_tu.cu_off);
18860 obstack_grow (info->types_list, val, 8);
18861 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18862 obstack_grow (info->types_list, val, 8);
18863
18864 ++info->cu_index;
18865
18866 return 1;
18867 }
18868
18869 /* Recurse into all "included" dependencies and write their symbols as
18870 if they appeared in this psymtab. */
18871
18872 static void
18873 recursively_write_psymbols (struct objfile *objfile,
18874 struct partial_symtab *psymtab,
18875 struct mapped_symtab *symtab,
18876 htab_t psyms_seen,
18877 offset_type cu_index)
18878 {
18879 int i;
18880
18881 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18882 if (psymtab->dependencies[i]->user != NULL)
18883 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18884 symtab, psyms_seen, cu_index);
18885
18886 write_psymbols (symtab,
18887 psyms_seen,
18888 objfile->global_psymbols.list + psymtab->globals_offset,
18889 psymtab->n_global_syms, cu_index,
18890 0);
18891 write_psymbols (symtab,
18892 psyms_seen,
18893 objfile->static_psymbols.list + psymtab->statics_offset,
18894 psymtab->n_static_syms, cu_index,
18895 1);
18896 }
18897
18898 /* Create an index file for OBJFILE in the directory DIR. */
18899
18900 static void
18901 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18902 {
18903 struct cleanup *cleanup;
18904 char *filename, *cleanup_filename;
18905 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18906 struct obstack cu_list, types_cu_list;
18907 int i;
18908 FILE *out_file;
18909 struct mapped_symtab *symtab;
18910 offset_type val, size_of_contents, total_len;
18911 struct stat st;
18912 htab_t psyms_seen;
18913 htab_t cu_index_htab;
18914 struct psymtab_cu_index_map *psymtab_cu_index_map;
18915
18916 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18917 return;
18918
18919 if (dwarf2_per_objfile->using_index)
18920 error (_("Cannot use an index to create the index"));
18921
18922 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18923 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18924
18925 if (stat (objfile->name, &st) < 0)
18926 perror_with_name (objfile->name);
18927
18928 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18929 INDEX_SUFFIX, (char *) NULL);
18930 cleanup = make_cleanup (xfree, filename);
18931
18932 out_file = fopen (filename, "wb");
18933 if (!out_file)
18934 error (_("Can't open `%s' for writing"), filename);
18935
18936 cleanup_filename = filename;
18937 make_cleanup (unlink_if_set, &cleanup_filename);
18938
18939 symtab = create_mapped_symtab ();
18940 make_cleanup (cleanup_mapped_symtab, symtab);
18941
18942 obstack_init (&addr_obstack);
18943 make_cleanup_obstack_free (&addr_obstack);
18944
18945 obstack_init (&cu_list);
18946 make_cleanup_obstack_free (&cu_list);
18947
18948 obstack_init (&types_cu_list);
18949 make_cleanup_obstack_free (&types_cu_list);
18950
18951 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18952 NULL, xcalloc, xfree);
18953 make_cleanup_htab_delete (psyms_seen);
18954
18955 /* While we're scanning CU's create a table that maps a psymtab pointer
18956 (which is what addrmap records) to its index (which is what is recorded
18957 in the index file). This will later be needed to write the address
18958 table. */
18959 cu_index_htab = htab_create_alloc (100,
18960 hash_psymtab_cu_index,
18961 eq_psymtab_cu_index,
18962 NULL, xcalloc, xfree);
18963 make_cleanup_htab_delete (cu_index_htab);
18964 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18965 xmalloc (sizeof (struct psymtab_cu_index_map)
18966 * dwarf2_per_objfile->n_comp_units);
18967 make_cleanup (xfree, psymtab_cu_index_map);
18968
18969 /* The CU list is already sorted, so we don't need to do additional
18970 work here. Also, the debug_types entries do not appear in
18971 all_comp_units, but only in their own hash table. */
18972 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18973 {
18974 struct dwarf2_per_cu_data *per_cu
18975 = dwarf2_per_objfile->all_comp_units[i];
18976 struct partial_symtab *psymtab = per_cu->v.psymtab;
18977 gdb_byte val[8];
18978 struct psymtab_cu_index_map *map;
18979 void **slot;
18980
18981 if (psymtab->user == NULL)
18982 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
18983
18984 map = &psymtab_cu_index_map[i];
18985 map->psymtab = psymtab;
18986 map->cu_index = i;
18987 slot = htab_find_slot (cu_index_htab, map, INSERT);
18988 gdb_assert (slot != NULL);
18989 gdb_assert (*slot == NULL);
18990 *slot = map;
18991
18992 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18993 per_cu->offset.sect_off);
18994 obstack_grow (&cu_list, val, 8);
18995 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18996 obstack_grow (&cu_list, val, 8);
18997 }
18998
18999 /* Dump the address map. */
19000 write_address_map (objfile, &addr_obstack, cu_index_htab);
19001
19002 /* Write out the .debug_type entries, if any. */
19003 if (dwarf2_per_objfile->signatured_types)
19004 {
19005 struct signatured_type_index_data sig_data;
19006
19007 sig_data.objfile = objfile;
19008 sig_data.symtab = symtab;
19009 sig_data.types_list = &types_cu_list;
19010 sig_data.psyms_seen = psyms_seen;
19011 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19012 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19013 write_one_signatured_type, &sig_data);
19014 }
19015
19016 /* Now that we've processed all symbols we can shrink their cu_indices
19017 lists. */
19018 uniquify_cu_indices (symtab);
19019
19020 obstack_init (&constant_pool);
19021 make_cleanup_obstack_free (&constant_pool);
19022 obstack_init (&symtab_obstack);
19023 make_cleanup_obstack_free (&symtab_obstack);
19024 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19025
19026 obstack_init (&contents);
19027 make_cleanup_obstack_free (&contents);
19028 size_of_contents = 6 * sizeof (offset_type);
19029 total_len = size_of_contents;
19030
19031 /* The version number. */
19032 val = MAYBE_SWAP (7);
19033 obstack_grow (&contents, &val, sizeof (val));
19034
19035 /* The offset of the CU list from the start of the file. */
19036 val = MAYBE_SWAP (total_len);
19037 obstack_grow (&contents, &val, sizeof (val));
19038 total_len += obstack_object_size (&cu_list);
19039
19040 /* The offset of the types CU list from the start of the file. */
19041 val = MAYBE_SWAP (total_len);
19042 obstack_grow (&contents, &val, sizeof (val));
19043 total_len += obstack_object_size (&types_cu_list);
19044
19045 /* The offset of the address table from the start of the file. */
19046 val = MAYBE_SWAP (total_len);
19047 obstack_grow (&contents, &val, sizeof (val));
19048 total_len += obstack_object_size (&addr_obstack);
19049
19050 /* The offset of the symbol table from the start of the file. */
19051 val = MAYBE_SWAP (total_len);
19052 obstack_grow (&contents, &val, sizeof (val));
19053 total_len += obstack_object_size (&symtab_obstack);
19054
19055 /* The offset of the constant pool from the start of the file. */
19056 val = MAYBE_SWAP (total_len);
19057 obstack_grow (&contents, &val, sizeof (val));
19058 total_len += obstack_object_size (&constant_pool);
19059
19060 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19061
19062 write_obstack (out_file, &contents);
19063 write_obstack (out_file, &cu_list);
19064 write_obstack (out_file, &types_cu_list);
19065 write_obstack (out_file, &addr_obstack);
19066 write_obstack (out_file, &symtab_obstack);
19067 write_obstack (out_file, &constant_pool);
19068
19069 fclose (out_file);
19070
19071 /* We want to keep the file, so we set cleanup_filename to NULL
19072 here. See unlink_if_set. */
19073 cleanup_filename = NULL;
19074
19075 do_cleanups (cleanup);
19076 }
19077
19078 /* Implementation of the `save gdb-index' command.
19079
19080 Note that the file format used by this command is documented in the
19081 GDB manual. Any changes here must be documented there. */
19082
19083 static void
19084 save_gdb_index_command (char *arg, int from_tty)
19085 {
19086 struct objfile *objfile;
19087
19088 if (!arg || !*arg)
19089 error (_("usage: save gdb-index DIRECTORY"));
19090
19091 ALL_OBJFILES (objfile)
19092 {
19093 struct stat st;
19094
19095 /* If the objfile does not correspond to an actual file, skip it. */
19096 if (stat (objfile->name, &st) < 0)
19097 continue;
19098
19099 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19100 if (dwarf2_per_objfile)
19101 {
19102 volatile struct gdb_exception except;
19103
19104 TRY_CATCH (except, RETURN_MASK_ERROR)
19105 {
19106 write_psymtabs_to_index (objfile, arg);
19107 }
19108 if (except.reason < 0)
19109 exception_fprintf (gdb_stderr, except,
19110 _("Error while writing index for `%s': "),
19111 objfile->name);
19112 }
19113 }
19114 }
19115
19116 \f
19117
19118 int dwarf2_always_disassemble;
19119
19120 static void
19121 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19122 struct cmd_list_element *c, const char *value)
19123 {
19124 fprintf_filtered (file,
19125 _("Whether to always disassemble "
19126 "DWARF expressions is %s.\n"),
19127 value);
19128 }
19129
19130 static void
19131 show_check_physname (struct ui_file *file, int from_tty,
19132 struct cmd_list_element *c, const char *value)
19133 {
19134 fprintf_filtered (file,
19135 _("Whether to check \"physname\" is %s.\n"),
19136 value);
19137 }
19138
19139 void _initialize_dwarf2_read (void);
19140
19141 void
19142 _initialize_dwarf2_read (void)
19143 {
19144 struct cmd_list_element *c;
19145
19146 dwarf2_objfile_data_key
19147 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19148
19149 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19150 Set DWARF 2 specific variables.\n\
19151 Configure DWARF 2 variables such as the cache size"),
19152 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19153 0/*allow-unknown*/, &maintenance_set_cmdlist);
19154
19155 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19156 Show DWARF 2 specific variables\n\
19157 Show DWARF 2 variables such as the cache size"),
19158 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19159 0/*allow-unknown*/, &maintenance_show_cmdlist);
19160
19161 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19162 &dwarf2_max_cache_age, _("\
19163 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19164 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19165 A higher limit means that cached compilation units will be stored\n\
19166 in memory longer, and more total memory will be used. Zero disables\n\
19167 caching, which can slow down startup."),
19168 NULL,
19169 show_dwarf2_max_cache_age,
19170 &set_dwarf2_cmdlist,
19171 &show_dwarf2_cmdlist);
19172
19173 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19174 &dwarf2_always_disassemble, _("\
19175 Set whether `info address' always disassembles DWARF expressions."), _("\
19176 Show whether `info address' always disassembles DWARF expressions."), _("\
19177 When enabled, DWARF expressions are always printed in an assembly-like\n\
19178 syntax. When disabled, expressions will be printed in a more\n\
19179 conversational style, when possible."),
19180 NULL,
19181 show_dwarf2_always_disassemble,
19182 &set_dwarf2_cmdlist,
19183 &show_dwarf2_cmdlist);
19184
19185 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19186 Set debugging of the dwarf2 reader."), _("\
19187 Show debugging of the dwarf2 reader."), _("\
19188 When enabled, debugging messages are printed during dwarf2 reading\n\
19189 and symtab expansion."),
19190 NULL,
19191 NULL,
19192 &setdebuglist, &showdebuglist);
19193
19194 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19195 Set debugging of the dwarf2 DIE reader."), _("\
19196 Show debugging of the dwarf2 DIE reader."), _("\
19197 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19198 The value is the maximum depth to print."),
19199 NULL,
19200 NULL,
19201 &setdebuglist, &showdebuglist);
19202
19203 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19204 Set cross-checking of \"physname\" code against demangler."), _("\
19205 Show cross-checking of \"physname\" code against demangler."), _("\
19206 When enabled, GDB's internal \"physname\" code is checked against\n\
19207 the demangler."),
19208 NULL, show_check_physname,
19209 &setdebuglist, &showdebuglist);
19210
19211 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19212 _("\
19213 Save a gdb-index file.\n\
19214 Usage: save gdb-index DIRECTORY"),
19215 &save_cmdlist);
19216 set_cmd_completer (c, filename_completer);
19217 }
This page took 0.519173 seconds and 4 git commands to generate.