PR macros/13205:
[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 <ctype.h>
66
67 #include <fcntl.h>
68 #include "gdb_string.h"
69 #include "gdb_assert.h"
70 #include <sys/types.h>
71 #ifdef HAVE_ZLIB_H
72 #include <zlib.h>
73 #endif
74 #ifdef HAVE_MMAP
75 #include <sys/mman.h>
76 #ifndef MAP_FAILED
77 #define MAP_FAILED ((void *) -1)
78 #endif
79 #endif
80
81 typedef struct symbol *symbolp;
82 DEF_VEC_P (symbolp);
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 int use_deprecated_index_sections = 0;
92
93 static int pagesize;
94
95 /* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99 static int processing_has_namespace_info;
100
101 static const struct objfile_data *dwarf2_objfile_data_key;
102
103 struct dwarf2_section_info
104 {
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
112 /* True if we have tried to read this section. */
113 int readin;
114 };
115
116 typedef struct dwarf2_section_info dwarf2_section_info_def;
117 DEF_VEC_O (dwarf2_section_info_def);
118
119 /* All offsets in the index are of this type. It must be
120 architecture-independent. */
121 typedef uint32_t offset_type;
122
123 DEF_VEC_I (offset_type);
124
125 /* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127 struct mapped_index
128 {
129 /* Index data format version. */
130 int version;
131
132 /* The total length of the buffer. */
133 off_t total_size;
134
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
137
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
140
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
143
144 /* Size in slots, each slot is 2 offset_types. */
145 offset_type symbol_table_slots;
146
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149 };
150
151 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152 DEF_VEC_P (dwarf2_per_cu_ptr);
153
154 /* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
157 struct dwarf2_per_objfile
158 {
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
164 struct dwarf2_section_info macro;
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
167 struct dwarf2_section_info addr;
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
170 struct dwarf2_section_info gdb_index;
171
172 VEC (dwarf2_section_info_def) *types;
173
174 /* Back link. */
175 struct objfile *objfile;
176
177 /* Table of all the compilation units. This is used to locate
178 the target compilation unit of a particular reference. */
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
184 /* The number of .debug_types-related CUs. */
185 int n_type_units;
186
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
189
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
193
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
205
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
208 unsigned char using_index;
209
210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
211 struct mapped_index *index_table;
212
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
222
223 /* Table mapping type DIEs to their struct type *.
224 This is NULL if not allocated yet.
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
230 };
231
232 static struct dwarf2_per_objfile *dwarf2_per_objfile;
233
234 /* Default names of the debugging sections. */
235
236 /* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
239 static const struct dwarf2_debug_sections dwarf2_elf_names =
240 {
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
246 { ".debug_macro", ".zdebug_macro" },
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
250 { ".debug_addr", ".zdebug_addr" },
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
253 { ".gdb_index", ".zgdb_index" },
254 23
255 };
256
257 /* List of DWO sections. */
258
259 static const struct dwo_section_names
260 {
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268 }
269 dwo_section_names =
270 {
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278 };
279
280 /* local data types */
281
282 /* We hold several abbreviation tables in memory at the same time. */
283 #ifndef ABBREV_HASH_SIZE
284 #define ABBREV_HASH_SIZE 121
285 #endif
286
287 /* The data in a compilation unit header, after target2host
288 translation, looks like this. */
289 struct comp_unit_head
290 {
291 unsigned int length;
292 short version;
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
295 sect_offset abbrev_offset;
296
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
299
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
302
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
305 sect_offset offset;
306
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
309 cu_offset first_die_offset;
310 };
311
312 /* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314 struct delayed_method_info
315 {
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330 };
331
332 typedef struct delayed_method_info delayed_method_info;
333 DEF_VEC_O (delayed_method_info);
334
335 /* Internal state when decoding a particular compilation unit. */
336 struct dwarf2_cu
337 {
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
341 /* The header of the compilation unit. */
342 struct comp_unit_head header;
343
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
354 const char *producer;
355
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
372
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
395 htab_t die_hash;
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
415 /* Non-NULL if this CU came from a DWO file. */
416 struct dwo_unit *dwo_unit;
417
418 /* The DW_AT_addr_base attribute if present, zero otherwise
419 (zero is a valid value though).
420 Note this value comes from the stub CU/TU's DIE. */
421 ULONGEST addr_base;
422
423 /* Mark used when releasing cached dies. */
424 unsigned int mark : 1;
425
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
430 unsigned int has_loclist : 1;
431
432 /* These cache the results of producer_is_gxx_lt_4_6.
433 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
434 information is cached because profiling CU expansion showed
435 excessive time spent in producer_is_gxx_lt_4_6. */
436 unsigned int checked_producer : 1;
437 unsigned int producer_is_gxx_lt_4_6 : 1;
438
439 /* Non-zero if DW_AT_addr_base was found.
440 Used when processing DWO files. */
441 unsigned int have_addr_base : 1;
442 };
443
444 /* Persistent data held for a compilation unit, even when not
445 processing it. We put a pointer to this structure in the
446 read_symtab_private field of the psymtab. */
447
448 struct dwarf2_per_cu_data
449 {
450 /* The start offset and length of this compilation unit. 2**29-1
451 bytes should suffice to store the length of any compilation unit
452 - if it doesn't, GDB will fall over anyway.
453 NOTE: Unlike comp_unit_head.length, this length includes
454 initial_length_size.
455 If the DIE refers to a DWO file, this is always of the original die,
456 not the DWO file. */
457 sect_offset offset;
458 unsigned int length : 29;
459
460 /* Flag indicating this compilation unit will be read in before
461 any of the current compilation units are processed. */
462 unsigned int queued : 1;
463
464 /* This flag will be set when reading partial DIEs if we need to load
465 absolutely all DIEs for this compilation unit, instead of just the ones
466 we think are interesting. It gets set if we look for a DIE in the
467 hash table and don't find it. */
468 unsigned int load_all_dies : 1;
469
470 /* Non-zero if this CU is from .debug_types. */
471 unsigned int is_debug_types : 1;
472
473 /* The section this CU/TU lives in.
474 If the DIE refers to a DWO file, this is always the original die,
475 not the DWO file. */
476 struct dwarf2_section_info *info_or_types_section;
477
478 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
479 of the CU cache it gets reset to NULL again. */
480 struct dwarf2_cu *cu;
481
482 /* The corresponding objfile.
483 Normally we can get the objfile from dwarf2_per_objfile.
484 However we can enter this file with just a "per_cu" handle. */
485 struct objfile *objfile;
486
487 /* When using partial symbol tables, the 'psymtab' field is active.
488 Otherwise the 'quick' field is active. */
489 union
490 {
491 /* The partial symbol table associated with this compilation unit,
492 or NULL for unread partial units. */
493 struct partial_symtab *psymtab;
494
495 /* Data needed by the "quick" functions. */
496 struct dwarf2_per_cu_quick_data *quick;
497 } v;
498
499 /* The CUs we import using DW_TAG_imported_unit. This is filled in
500 while reading psymtabs, used to compute the psymtab dependencies,
501 and then cleared. Then it is filled in again while reading full
502 symbols, and only deleted when the objfile is destroyed. */
503 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
504 };
505
506 /* Entry in the signatured_types hash table. */
507
508 struct signatured_type
509 {
510 /* The type's signature. */
511 ULONGEST signature;
512
513 /* Offset in the TU of the type's DIE, as read from the TU header.
514 If the definition lives in a DWO file, this value is unusable. */
515 cu_offset type_offset_in_tu;
516
517 /* Offset in the section of the type's DIE.
518 If the definition lives in a DWO file, this is the offset in the
519 .debug_types.dwo section.
520 The value is zero until the actual value is known.
521 Zero is otherwise not a valid section offset. */
522 sect_offset type_offset_in_section;
523
524 /* The CU(/TU) of this type. */
525 struct dwarf2_per_cu_data per_cu;
526 };
527
528 /* These sections are what may appear in a "dwo" file. */
529
530 struct dwo_sections
531 {
532 struct dwarf2_section_info abbrev;
533 struct dwarf2_section_info info;
534 struct dwarf2_section_info line;
535 struct dwarf2_section_info loc;
536 struct dwarf2_section_info str;
537 struct dwarf2_section_info str_offsets;
538 VEC (dwarf2_section_info_def) *types;
539 };
540
541 /* Common bits of DWO CUs/TUs. */
542
543 struct dwo_unit
544 {
545 /* Backlink to the containing struct dwo_file. */
546 struct dwo_file *dwo_file;
547
548 /* The "id" that distinguishes this CU/TU.
549 .debug_info calls this "dwo_id", .debug_types calls this "signature".
550 Since signatures came first, we stick with it for consistency. */
551 ULONGEST signature;
552
553 /* The section this CU/TU lives in, in the DWO file. */
554 struct dwarf2_section_info *info_or_types_section;
555
556 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
557 sect_offset offset;
558 unsigned int length;
559
560 /* For types, offset in the type's DIE of the type defined by this TU. */
561 cu_offset type_offset_in_tu;
562 };
563
564 /* Data for one DWO file. */
565
566 struct dwo_file
567 {
568 /* The DW_AT_GNU_dwo_name attribute.
569 We don't manage space for this, it's an attribute. */
570 const char *dwo_name;
571
572 /* The bfd, when the file is open. Otherwise this is NULL. */
573 bfd *dwo_bfd;
574
575 /* Section info for this file. */
576 struct dwo_sections sections;
577
578 /* Table of CUs in the file.
579 Each element is a struct dwo_unit. */
580 htab_t cus;
581
582 /* Table of TUs in the file.
583 Each element is a struct dwo_unit. */
584 htab_t tus;
585 };
586
587 /* Struct used to pass misc. parameters to read_die_and_children, et
588 al. which are used for both .debug_info and .debug_types dies.
589 All parameters here are unchanging for the life of the call. This
590 struct exists to abstract away the constant parameters of die reading. */
591
592 struct die_reader_specs
593 {
594 /* die_section->asection->owner. */
595 bfd* abfd;
596
597 /* The CU of the DIE we are parsing. */
598 struct dwarf2_cu *cu;
599
600 /* Non-NULL if reading a DWO file. */
601 struct dwo_file *dwo_file;
602
603 /* The section the die comes from.
604 This is either .debug_info or .debug_types, or the .dwo variants. */
605 struct dwarf2_section_info *die_section;
606
607 /* die_section->buffer. */
608 gdb_byte *buffer;
609 };
610
611 /* Type of function passed to init_cutu_and_read_dies, et.al. */
612 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
613 gdb_byte *info_ptr,
614 struct die_info *comp_unit_die,
615 int has_children,
616 void *data);
617
618 /* The line number information for a compilation unit (found in the
619 .debug_line section) begins with a "statement program header",
620 which contains the following information. */
621 struct line_header
622 {
623 unsigned int total_length;
624 unsigned short version;
625 unsigned int header_length;
626 unsigned char minimum_instruction_length;
627 unsigned char maximum_ops_per_instruction;
628 unsigned char default_is_stmt;
629 int line_base;
630 unsigned char line_range;
631 unsigned char opcode_base;
632
633 /* standard_opcode_lengths[i] is the number of operands for the
634 standard opcode whose value is i. This means that
635 standard_opcode_lengths[0] is unused, and the last meaningful
636 element is standard_opcode_lengths[opcode_base - 1]. */
637 unsigned char *standard_opcode_lengths;
638
639 /* The include_directories table. NOTE! These strings are not
640 allocated with xmalloc; instead, they are pointers into
641 debug_line_buffer. If you try to free them, `free' will get
642 indigestion. */
643 unsigned int num_include_dirs, include_dirs_size;
644 char **include_dirs;
645
646 /* The file_names table. NOTE! These strings are not allocated
647 with xmalloc; instead, they are pointers into debug_line_buffer.
648 Don't try to free them directly. */
649 unsigned int num_file_names, file_names_size;
650 struct file_entry
651 {
652 char *name;
653 unsigned int dir_index;
654 unsigned int mod_time;
655 unsigned int length;
656 int included_p; /* Non-zero if referenced by the Line Number Program. */
657 struct symtab *symtab; /* The associated symbol table, if any. */
658 } *file_names;
659
660 /* The start and end of the statement program following this
661 header. These point into dwarf2_per_objfile->line_buffer. */
662 gdb_byte *statement_program_start, *statement_program_end;
663 };
664
665 /* When we construct a partial symbol table entry we only
666 need this much information. */
667 struct partial_die_info
668 {
669 /* Offset of this DIE. */
670 sect_offset offset;
671
672 /* DWARF-2 tag for this DIE. */
673 ENUM_BITFIELD(dwarf_tag) tag : 16;
674
675 /* Assorted flags describing the data found in this DIE. */
676 unsigned int has_children : 1;
677 unsigned int is_external : 1;
678 unsigned int is_declaration : 1;
679 unsigned int has_type : 1;
680 unsigned int has_specification : 1;
681 unsigned int has_pc_info : 1;
682 unsigned int may_be_inlined : 1;
683
684 /* Flag set if the SCOPE field of this structure has been
685 computed. */
686 unsigned int scope_set : 1;
687
688 /* Flag set if the DIE has a byte_size attribute. */
689 unsigned int has_byte_size : 1;
690
691 /* Flag set if any of the DIE's children are template arguments. */
692 unsigned int has_template_arguments : 1;
693
694 /* Flag set if fixup_partial_die has been called on this die. */
695 unsigned int fixup_called : 1;
696
697 /* The name of this DIE. Normally the value of DW_AT_name, but
698 sometimes a default name for unnamed DIEs. */
699 char *name;
700
701 /* The linkage name, if present. */
702 const char *linkage_name;
703
704 /* The scope to prepend to our children. This is generally
705 allocated on the comp_unit_obstack, so will disappear
706 when this compilation unit leaves the cache. */
707 char *scope;
708
709 /* Some data associated with the partial DIE. The tag determines
710 which field is live. */
711 union
712 {
713 /* The location description associated with this DIE, if any. */
714 struct dwarf_block *locdesc;
715 /* The offset of an import, for DW_TAG_imported_unit. */
716 sect_offset offset;
717 } d;
718
719 /* If HAS_PC_INFO, the PC range associated with this DIE. */
720 CORE_ADDR lowpc;
721 CORE_ADDR highpc;
722
723 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
724 DW_AT_sibling, if any. */
725 /* NOTE: This member isn't strictly necessary, read_partial_die could
726 return DW_AT_sibling values to its caller load_partial_dies. */
727 gdb_byte *sibling;
728
729 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
730 DW_AT_specification (or DW_AT_abstract_origin or
731 DW_AT_extension). */
732 sect_offset spec_offset;
733
734 /* Pointers to this DIE's parent, first child, and next sibling,
735 if any. */
736 struct partial_die_info *die_parent, *die_child, *die_sibling;
737 };
738
739 /* This data structure holds the information of an abbrev. */
740 struct abbrev_info
741 {
742 unsigned int number; /* number identifying abbrev */
743 enum dwarf_tag tag; /* dwarf tag */
744 unsigned short has_children; /* boolean */
745 unsigned short num_attrs; /* number of attributes */
746 struct attr_abbrev *attrs; /* an array of attribute descriptions */
747 struct abbrev_info *next; /* next in chain */
748 };
749
750 struct attr_abbrev
751 {
752 ENUM_BITFIELD(dwarf_attribute) name : 16;
753 ENUM_BITFIELD(dwarf_form) form : 16;
754 };
755
756 /* Attributes have a name and a value. */
757 struct attribute
758 {
759 ENUM_BITFIELD(dwarf_attribute) name : 16;
760 ENUM_BITFIELD(dwarf_form) form : 15;
761
762 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
763 field should be in u.str (existing only for DW_STRING) but it is kept
764 here for better struct attribute alignment. */
765 unsigned int string_is_canonical : 1;
766
767 union
768 {
769 char *str;
770 struct dwarf_block *blk;
771 ULONGEST unsnd;
772 LONGEST snd;
773 CORE_ADDR addr;
774 struct signatured_type *signatured_type;
775 }
776 u;
777 };
778
779 /* This data structure holds a complete die structure. */
780 struct die_info
781 {
782 /* DWARF-2 tag for this DIE. */
783 ENUM_BITFIELD(dwarf_tag) tag : 16;
784
785 /* Number of attributes */
786 unsigned char num_attrs;
787
788 /* True if we're presently building the full type name for the
789 type derived from this DIE. */
790 unsigned char building_fullname : 1;
791
792 /* Abbrev number */
793 unsigned int abbrev;
794
795 /* Offset in .debug_info or .debug_types section. */
796 sect_offset offset;
797
798 /* The dies in a compilation unit form an n-ary tree. PARENT
799 points to this die's parent; CHILD points to the first child of
800 this node; and all the children of a given node are chained
801 together via their SIBLING fields. */
802 struct die_info *child; /* Its first child, if any. */
803 struct die_info *sibling; /* Its next sibling, if any. */
804 struct die_info *parent; /* Its parent, if any. */
805
806 /* An array of attributes, with NUM_ATTRS elements. There may be
807 zero, but it's not common and zero-sized arrays are not
808 sufficiently portable C. */
809 struct attribute attrs[1];
810 };
811
812 /* Get at parts of an attribute structure. */
813
814 #define DW_STRING(attr) ((attr)->u.str)
815 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
816 #define DW_UNSND(attr) ((attr)->u.unsnd)
817 #define DW_BLOCK(attr) ((attr)->u.blk)
818 #define DW_SND(attr) ((attr)->u.snd)
819 #define DW_ADDR(attr) ((attr)->u.addr)
820 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
821
822 /* Blocks are a bunch of untyped bytes. */
823 struct dwarf_block
824 {
825 unsigned int size;
826
827 /* Valid only if SIZE is not zero. */
828 gdb_byte *data;
829 };
830
831 #ifndef ATTR_ALLOC_CHUNK
832 #define ATTR_ALLOC_CHUNK 4
833 #endif
834
835 /* Allocate fields for structs, unions and enums in this size. */
836 #ifndef DW_FIELD_ALLOC_CHUNK
837 #define DW_FIELD_ALLOC_CHUNK 4
838 #endif
839
840 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
841 but this would require a corresponding change in unpack_field_as_long
842 and friends. */
843 static int bits_per_byte = 8;
844
845 /* The routines that read and process dies for a C struct or C++ class
846 pass lists of data member fields and lists of member function fields
847 in an instance of a field_info structure, as defined below. */
848 struct field_info
849 {
850 /* List of data member and baseclasses fields. */
851 struct nextfield
852 {
853 struct nextfield *next;
854 int accessibility;
855 int virtuality;
856 struct field field;
857 }
858 *fields, *baseclasses;
859
860 /* Number of fields (including baseclasses). */
861 int nfields;
862
863 /* Number of baseclasses. */
864 int nbaseclasses;
865
866 /* Set if the accesibility of one of the fields is not public. */
867 int non_public_fields;
868
869 /* Member function fields array, entries are allocated in the order they
870 are encountered in the object file. */
871 struct nextfnfield
872 {
873 struct nextfnfield *next;
874 struct fn_field fnfield;
875 }
876 *fnfields;
877
878 /* Member function fieldlist array, contains name of possibly overloaded
879 member function, number of overloaded member functions and a pointer
880 to the head of the member function field chain. */
881 struct fnfieldlist
882 {
883 char *name;
884 int length;
885 struct nextfnfield *head;
886 }
887 *fnfieldlists;
888
889 /* Number of entries in the fnfieldlists array. */
890 int nfnfields;
891
892 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
893 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
894 struct typedef_field_list
895 {
896 struct typedef_field field;
897 struct typedef_field_list *next;
898 }
899 *typedef_field_list;
900 unsigned typedef_field_list_count;
901 };
902
903 /* One item on the queue of compilation units to read in full symbols
904 for. */
905 struct dwarf2_queue_item
906 {
907 struct dwarf2_per_cu_data *per_cu;
908 enum language pretend_language;
909 struct dwarf2_queue_item *next;
910 };
911
912 /* The current queue. */
913 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
914
915 /* Loaded secondary compilation units are kept in memory until they
916 have not been referenced for the processing of this many
917 compilation units. Set this to zero to disable caching. Cache
918 sizes of up to at least twenty will improve startup time for
919 typical inter-CU-reference binaries, at an obvious memory cost. */
920 static int dwarf2_max_cache_age = 5;
921 static void
922 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
923 struct cmd_list_element *c, const char *value)
924 {
925 fprintf_filtered (file, _("The upper bound on the age of cached "
926 "dwarf2 compilation units is %s.\n"),
927 value);
928 }
929
930
931 /* Various complaints about symbol reading that don't abort the process. */
932
933 static void
934 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
935 {
936 complaint (&symfile_complaints,
937 _("statement list doesn't fit in .debug_line section"));
938 }
939
940 static void
941 dwarf2_debug_line_missing_file_complaint (void)
942 {
943 complaint (&symfile_complaints,
944 _(".debug_line section has line data without a file"));
945 }
946
947 static void
948 dwarf2_debug_line_missing_end_sequence_complaint (void)
949 {
950 complaint (&symfile_complaints,
951 _(".debug_line section has line "
952 "program sequence without an end"));
953 }
954
955 static void
956 dwarf2_complex_location_expr_complaint (void)
957 {
958 complaint (&symfile_complaints, _("location expression too complex"));
959 }
960
961 static void
962 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
963 int arg3)
964 {
965 complaint (&symfile_complaints,
966 _("const value length mismatch for '%s', got %d, expected %d"),
967 arg1, arg2, arg3);
968 }
969
970 static void
971 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
972 {
973 complaint (&symfile_complaints,
974 _("macro info runs off end of `%s' section"),
975 section->asection->name);
976 }
977
978 static void
979 dwarf2_macro_malformed_definition_complaint (const char *arg1)
980 {
981 complaint (&symfile_complaints,
982 _("macro debug info contains a "
983 "malformed macro definition:\n`%s'"),
984 arg1);
985 }
986
987 static void
988 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
989 {
990 complaint (&symfile_complaints,
991 _("invalid attribute class or form for '%s' in '%s'"),
992 arg1, arg2);
993 }
994
995 /* local function prototypes */
996
997 static void dwarf2_locate_sections (bfd *, asection *, void *);
998
999 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1000 struct objfile *);
1001
1002 static void dwarf2_find_base_address (struct die_info *die,
1003 struct dwarf2_cu *cu);
1004
1005 static void dwarf2_build_psymtabs_hard (struct objfile *);
1006
1007 static void scan_partial_symbols (struct partial_die_info *,
1008 CORE_ADDR *, CORE_ADDR *,
1009 int, struct dwarf2_cu *);
1010
1011 static void add_partial_symbol (struct partial_die_info *,
1012 struct dwarf2_cu *);
1013
1014 static void add_partial_namespace (struct partial_die_info *pdi,
1015 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1016 int need_pc, struct dwarf2_cu *cu);
1017
1018 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1019 CORE_ADDR *highpc, int need_pc,
1020 struct dwarf2_cu *cu);
1021
1022 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1023 struct dwarf2_cu *cu);
1024
1025 static void add_partial_subprogram (struct partial_die_info *pdi,
1026 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1027 int need_pc, struct dwarf2_cu *cu);
1028
1029 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1030
1031 static void psymtab_to_symtab_1 (struct partial_symtab *);
1032
1033 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1034 struct dwarf2_section_info *);
1035
1036 static void dwarf2_free_abbrev_table (void *);
1037
1038 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1039
1040 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1041 struct dwarf2_cu *);
1042
1043 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1044 struct dwarf2_cu *);
1045
1046 static struct partial_die_info *load_partial_dies
1047 (const struct die_reader_specs *, gdb_byte *, int);
1048
1049 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1050 struct partial_die_info *,
1051 struct abbrev_info *,
1052 unsigned int,
1053 gdb_byte *);
1054
1055 static struct partial_die_info *find_partial_die (sect_offset,
1056 struct dwarf2_cu *);
1057
1058 static void fixup_partial_die (struct partial_die_info *,
1059 struct dwarf2_cu *);
1060
1061 static gdb_byte *read_attribute (const struct die_reader_specs *,
1062 struct attribute *, struct attr_abbrev *,
1063 gdb_byte *);
1064
1065 static unsigned int read_1_byte (bfd *, gdb_byte *);
1066
1067 static int read_1_signed_byte (bfd *, gdb_byte *);
1068
1069 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1070
1071 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1072
1073 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1074
1075 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1076 unsigned int *);
1077
1078 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1079
1080 static LONGEST read_checked_initial_length_and_offset
1081 (bfd *, gdb_byte *, const struct comp_unit_head *,
1082 unsigned int *, unsigned int *);
1083
1084 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1085 unsigned int *);
1086
1087 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1088
1089 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1090
1091 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1092
1093 static char *read_indirect_string (bfd *, gdb_byte *,
1094 const struct comp_unit_head *,
1095 unsigned int *);
1096
1097 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1098
1099 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1100
1101 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1102 unsigned int *);
1103
1104 static char *read_str_index (const struct die_reader_specs *reader,
1105 struct dwarf2_cu *cu, ULONGEST str_index);
1106
1107 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1108
1109 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1110
1111 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1112 struct dwarf2_cu *);
1113
1114 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1115 unsigned int,
1116 struct dwarf2_cu *);
1117
1118 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1119 struct dwarf2_cu *cu);
1120
1121 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1122
1123 static struct die_info *die_specification (struct die_info *die,
1124 struct dwarf2_cu **);
1125
1126 static void free_line_header (struct line_header *lh);
1127
1128 static void add_file_name (struct line_header *, char *, unsigned int,
1129 unsigned int, unsigned int);
1130
1131 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1132 struct dwarf2_cu *cu);
1133
1134 static void dwarf_decode_lines (struct line_header *, const char *,
1135 struct dwarf2_cu *, struct partial_symtab *,
1136 int);
1137
1138 static void dwarf2_start_subfile (char *, const char *, const char *);
1139
1140 static struct symbol *new_symbol (struct die_info *, struct type *,
1141 struct dwarf2_cu *);
1142
1143 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1144 struct dwarf2_cu *, struct symbol *);
1145
1146 static void dwarf2_const_value (struct attribute *, struct symbol *,
1147 struct dwarf2_cu *);
1148
1149 static void dwarf2_const_value_attr (struct attribute *attr,
1150 struct type *type,
1151 const char *name,
1152 struct obstack *obstack,
1153 struct dwarf2_cu *cu, LONGEST *value,
1154 gdb_byte **bytes,
1155 struct dwarf2_locexpr_baton **baton);
1156
1157 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1158
1159 static int need_gnat_info (struct dwarf2_cu *);
1160
1161 static struct type *die_descriptive_type (struct die_info *,
1162 struct dwarf2_cu *);
1163
1164 static void set_descriptive_type (struct type *, struct die_info *,
1165 struct dwarf2_cu *);
1166
1167 static struct type *die_containing_type (struct die_info *,
1168 struct dwarf2_cu *);
1169
1170 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1171 struct dwarf2_cu *);
1172
1173 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1174
1175 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1176
1177 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1178
1179 static char *typename_concat (struct obstack *obs, const char *prefix,
1180 const char *suffix, int physname,
1181 struct dwarf2_cu *cu);
1182
1183 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1184
1185 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1186
1187 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1188
1189 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1190
1191 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1192
1193 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1194 struct dwarf2_cu *, struct partial_symtab *);
1195
1196 static int dwarf2_get_pc_bounds (struct die_info *,
1197 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1198 struct partial_symtab *);
1199
1200 static void get_scope_pc_bounds (struct die_info *,
1201 CORE_ADDR *, CORE_ADDR *,
1202 struct dwarf2_cu *);
1203
1204 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1205 CORE_ADDR, struct dwarf2_cu *);
1206
1207 static void dwarf2_add_field (struct field_info *, struct die_info *,
1208 struct dwarf2_cu *);
1209
1210 static void dwarf2_attach_fields_to_type (struct field_info *,
1211 struct type *, struct dwarf2_cu *);
1212
1213 static void dwarf2_add_member_fn (struct field_info *,
1214 struct die_info *, struct type *,
1215 struct dwarf2_cu *);
1216
1217 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1218 struct type *,
1219 struct dwarf2_cu *);
1220
1221 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1222
1223 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1224
1225 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1226
1227 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1228
1229 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1230
1231 static struct type *read_module_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
1234 static const char *namespace_name (struct die_info *die,
1235 int *is_anonymous, struct dwarf2_cu *);
1236
1237 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1238
1239 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1240
1241 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1242 struct dwarf2_cu *);
1243
1244 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1245 gdb_byte *info_ptr,
1246 gdb_byte **new_info_ptr,
1247 struct die_info *parent);
1248
1249 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1250 gdb_byte *info_ptr,
1251 gdb_byte **new_info_ptr,
1252 struct die_info *parent);
1253
1254 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1255 struct die_info **, gdb_byte *, int *, int);
1256
1257 static gdb_byte *read_full_die (const struct die_reader_specs *,
1258 struct die_info **, gdb_byte *, int *);
1259
1260 static void process_die (struct die_info *, struct dwarf2_cu *);
1261
1262 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1263 struct obstack *);
1264
1265 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1266
1267 static const char *dwarf2_full_name (char *name,
1268 struct die_info *die,
1269 struct dwarf2_cu *cu);
1270
1271 static struct die_info *dwarf2_extension (struct die_info *die,
1272 struct dwarf2_cu **);
1273
1274 static const char *dwarf_tag_name (unsigned int);
1275
1276 static const char *dwarf_attr_name (unsigned int);
1277
1278 static const char *dwarf_form_name (unsigned int);
1279
1280 static char *dwarf_bool_name (unsigned int);
1281
1282 static const char *dwarf_type_encoding_name (unsigned int);
1283
1284 static struct die_info *sibling_die (struct die_info *);
1285
1286 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1287
1288 static void dump_die_for_error (struct die_info *);
1289
1290 static void dump_die_1 (struct ui_file *, int level, int max_level,
1291 struct die_info *);
1292
1293 /*static*/ void dump_die (struct die_info *, int max_level);
1294
1295 static void store_in_ref_table (struct die_info *,
1296 struct dwarf2_cu *);
1297
1298 static int is_ref_attr (struct attribute *);
1299
1300 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1301
1302 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1303
1304 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1305 struct attribute *,
1306 struct dwarf2_cu **);
1307
1308 static struct die_info *follow_die_ref (struct die_info *,
1309 struct attribute *,
1310 struct dwarf2_cu **);
1311
1312 static struct die_info *follow_die_sig (struct die_info *,
1313 struct attribute *,
1314 struct dwarf2_cu **);
1315
1316 static struct signatured_type *lookup_signatured_type_at_offset
1317 (struct objfile *objfile,
1318 struct dwarf2_section_info *section, sect_offset offset);
1319
1320 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1321
1322 static void read_signatured_type (struct signatured_type *);
1323
1324 /* memory allocation interface */
1325
1326 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1327
1328 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1329
1330 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1331
1332 static void dwarf_decode_macros (struct line_header *, unsigned int,
1333 char *, bfd *, struct dwarf2_cu *,
1334 struct dwarf2_section_info *,
1335 int, const char *);
1336
1337 static int attr_form_is_block (struct attribute *);
1338
1339 static int attr_form_is_section_offset (struct attribute *);
1340
1341 static int attr_form_is_constant (struct attribute *);
1342
1343 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1344 struct dwarf2_loclist_baton *baton,
1345 struct attribute *attr);
1346
1347 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1348 struct symbol *sym,
1349 struct dwarf2_cu *cu);
1350
1351 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1352 gdb_byte *info_ptr,
1353 struct abbrev_info *abbrev);
1354
1355 static void free_stack_comp_unit (void *);
1356
1357 static hashval_t partial_die_hash (const void *item);
1358
1359 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1360
1361 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1362 (sect_offset offset, struct objfile *objfile);
1363
1364 static void init_one_comp_unit (struct dwarf2_cu *cu,
1365 struct dwarf2_per_cu_data *per_cu);
1366
1367 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1368 struct die_info *comp_unit_die,
1369 enum language pretend_language);
1370
1371 static void free_heap_comp_unit (void *);
1372
1373 static void free_cached_comp_units (void *);
1374
1375 static void age_cached_comp_units (void);
1376
1377 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1378
1379 static struct type *set_die_type (struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1381
1382 static void create_all_comp_units (struct objfile *);
1383
1384 static int create_all_type_units (struct objfile *);
1385
1386 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1387 enum language);
1388
1389 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1390 enum language);
1391
1392 static void dwarf2_add_dependence (struct dwarf2_cu *,
1393 struct dwarf2_per_cu_data *);
1394
1395 static void dwarf2_mark (struct dwarf2_cu *);
1396
1397 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1398
1399 static struct type *get_die_type_at_offset (sect_offset,
1400 struct dwarf2_per_cu_data *per_cu);
1401
1402 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1403
1404 static void dwarf2_release_queue (void *dummy);
1405
1406 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1407 enum language pretend_language);
1408
1409 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1410 struct dwarf2_per_cu_data *per_cu,
1411 enum language pretend_language);
1412
1413 static void process_queue (void);
1414
1415 static void find_file_and_directory (struct die_info *die,
1416 struct dwarf2_cu *cu,
1417 char **name, char **comp_dir);
1418
1419 static char *file_full_name (int file, struct line_header *lh,
1420 const char *comp_dir);
1421
1422 static gdb_byte *read_and_check_comp_unit_head
1423 (struct comp_unit_head *header,
1424 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1425 int is_debug_types_section);
1426
1427 static void init_cutu_and_read_dies
1428 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1429 die_reader_func_ftype *die_reader_func, void *data);
1430
1431 static void init_cutu_and_read_dies_simple
1432 (struct dwarf2_per_cu_data *this_cu,
1433 die_reader_func_ftype *die_reader_func, void *data);
1434
1435 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1436
1437 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1438
1439 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1440
1441 static struct dwo_unit *lookup_dwo_comp_unit
1442 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1443
1444 static struct dwo_unit *lookup_dwo_type_unit
1445 (struct signatured_type *, char *, const char *);
1446
1447 static void free_dwo_file_cleanup (void *);
1448
1449 static void munmap_section_buffer (struct dwarf2_section_info *);
1450
1451 static void process_cu_includes (void);
1452
1453 #if WORDS_BIGENDIAN
1454
1455 /* Convert VALUE between big- and little-endian. */
1456 static offset_type
1457 byte_swap (offset_type value)
1458 {
1459 offset_type result;
1460
1461 result = (value & 0xff) << 24;
1462 result |= (value & 0xff00) << 8;
1463 result |= (value & 0xff0000) >> 8;
1464 result |= (value & 0xff000000) >> 24;
1465 return result;
1466 }
1467
1468 #define MAYBE_SWAP(V) byte_swap (V)
1469
1470 #else
1471 #define MAYBE_SWAP(V) (V)
1472 #endif /* WORDS_BIGENDIAN */
1473
1474 /* The suffix for an index file. */
1475 #define INDEX_SUFFIX ".gdb-index"
1476
1477 static const char *dwarf2_physname (char *name, struct die_info *die,
1478 struct dwarf2_cu *cu);
1479
1480 /* Try to locate the sections we need for DWARF 2 debugging
1481 information and return true if we have enough to do something.
1482 NAMES points to the dwarf2 section names, or is NULL if the standard
1483 ELF names are used. */
1484
1485 int
1486 dwarf2_has_info (struct objfile *objfile,
1487 const struct dwarf2_debug_sections *names)
1488 {
1489 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1490 if (!dwarf2_per_objfile)
1491 {
1492 /* Initialize per-objfile state. */
1493 struct dwarf2_per_objfile *data
1494 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1495
1496 memset (data, 0, sizeof (*data));
1497 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1498 dwarf2_per_objfile = data;
1499
1500 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1501 (void *) names);
1502 dwarf2_per_objfile->objfile = objfile;
1503 }
1504 return (dwarf2_per_objfile->info.asection != NULL
1505 && dwarf2_per_objfile->abbrev.asection != NULL);
1506 }
1507
1508 /* When loading sections, we look either for uncompressed section or for
1509 compressed section names. */
1510
1511 static int
1512 section_is_p (const char *section_name,
1513 const struct dwarf2_section_names *names)
1514 {
1515 if (names->normal != NULL
1516 && strcmp (section_name, names->normal) == 0)
1517 return 1;
1518 if (names->compressed != NULL
1519 && strcmp (section_name, names->compressed) == 0)
1520 return 1;
1521 return 0;
1522 }
1523
1524 /* This function is mapped across the sections and remembers the
1525 offset and size of each of the debugging sections we are interested
1526 in. */
1527
1528 static void
1529 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1530 {
1531 const struct dwarf2_debug_sections *names;
1532
1533 if (vnames == NULL)
1534 names = &dwarf2_elf_names;
1535 else
1536 names = (const struct dwarf2_debug_sections *) vnames;
1537
1538 if (section_is_p (sectp->name, &names->info))
1539 {
1540 dwarf2_per_objfile->info.asection = sectp;
1541 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1542 }
1543 else if (section_is_p (sectp->name, &names->abbrev))
1544 {
1545 dwarf2_per_objfile->abbrev.asection = sectp;
1546 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1547 }
1548 else if (section_is_p (sectp->name, &names->line))
1549 {
1550 dwarf2_per_objfile->line.asection = sectp;
1551 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1552 }
1553 else if (section_is_p (sectp->name, &names->loc))
1554 {
1555 dwarf2_per_objfile->loc.asection = sectp;
1556 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1557 }
1558 else if (section_is_p (sectp->name, &names->macinfo))
1559 {
1560 dwarf2_per_objfile->macinfo.asection = sectp;
1561 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1562 }
1563 else if (section_is_p (sectp->name, &names->macro))
1564 {
1565 dwarf2_per_objfile->macro.asection = sectp;
1566 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1567 }
1568 else if (section_is_p (sectp->name, &names->str))
1569 {
1570 dwarf2_per_objfile->str.asection = sectp;
1571 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1572 }
1573 else if (section_is_p (sectp->name, &names->addr))
1574 {
1575 dwarf2_per_objfile->addr.asection = sectp;
1576 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1577 }
1578 else if (section_is_p (sectp->name, &names->frame))
1579 {
1580 dwarf2_per_objfile->frame.asection = sectp;
1581 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1582 }
1583 else if (section_is_p (sectp->name, &names->eh_frame))
1584 {
1585 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1586
1587 if (aflag & SEC_HAS_CONTENTS)
1588 {
1589 dwarf2_per_objfile->eh_frame.asection = sectp;
1590 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1591 }
1592 }
1593 else if (section_is_p (sectp->name, &names->ranges))
1594 {
1595 dwarf2_per_objfile->ranges.asection = sectp;
1596 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1597 }
1598 else if (section_is_p (sectp->name, &names->types))
1599 {
1600 struct dwarf2_section_info type_section;
1601
1602 memset (&type_section, 0, sizeof (type_section));
1603 type_section.asection = sectp;
1604 type_section.size = bfd_get_section_size (sectp);
1605
1606 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1607 &type_section);
1608 }
1609 else if (section_is_p (sectp->name, &names->gdb_index))
1610 {
1611 dwarf2_per_objfile->gdb_index.asection = sectp;
1612 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1613 }
1614
1615 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1616 && bfd_section_vma (abfd, sectp) == 0)
1617 dwarf2_per_objfile->has_section_at_zero = 1;
1618 }
1619
1620 /* Decompress a section that was compressed using zlib. Store the
1621 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1622
1623 static void
1624 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1625 gdb_byte **outbuf, bfd_size_type *outsize)
1626 {
1627 bfd *abfd = sectp->owner;
1628 #ifndef HAVE_ZLIB_H
1629 error (_("Support for zlib-compressed DWARF data (from '%s') "
1630 "is disabled in this copy of GDB"),
1631 bfd_get_filename (abfd));
1632 #else
1633 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1634 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1635 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1636 bfd_size_type uncompressed_size;
1637 gdb_byte *uncompressed_buffer;
1638 z_stream strm;
1639 int rc;
1640 int header_size = 12;
1641
1642 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1643 || bfd_bread (compressed_buffer,
1644 compressed_size, abfd) != compressed_size)
1645 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1646 bfd_get_filename (abfd));
1647
1648 /* Read the zlib header. In this case, it should be "ZLIB" followed
1649 by the uncompressed section size, 8 bytes in big-endian order. */
1650 if (compressed_size < header_size
1651 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1652 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1653 bfd_get_filename (abfd));
1654 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1655 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1656 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1657 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1658 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[11];
1662
1663 /* It is possible the section consists of several compressed
1664 buffers concatenated together, so we uncompress in a loop. */
1665 strm.zalloc = NULL;
1666 strm.zfree = NULL;
1667 strm.opaque = NULL;
1668 strm.avail_in = compressed_size - header_size;
1669 strm.next_in = (Bytef*) compressed_buffer + header_size;
1670 strm.avail_out = uncompressed_size;
1671 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1672 uncompressed_size);
1673 rc = inflateInit (&strm);
1674 while (strm.avail_in > 0)
1675 {
1676 if (rc != Z_OK)
1677 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1678 bfd_get_filename (abfd), rc);
1679 strm.next_out = ((Bytef*) uncompressed_buffer
1680 + (uncompressed_size - strm.avail_out));
1681 rc = inflate (&strm, Z_FINISH);
1682 if (rc != Z_STREAM_END)
1683 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1684 bfd_get_filename (abfd), rc);
1685 rc = inflateReset (&strm);
1686 }
1687 rc = inflateEnd (&strm);
1688 if (rc != Z_OK
1689 || strm.avail_out != 0)
1690 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1691 bfd_get_filename (abfd), rc);
1692
1693 do_cleanups (cleanup);
1694 *outbuf = uncompressed_buffer;
1695 *outsize = uncompressed_size;
1696 #endif
1697 }
1698
1699 /* A helper function that decides whether a section is empty,
1700 or not present. */
1701
1702 static int
1703 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1704 {
1705 return info->asection == NULL || info->size == 0;
1706 }
1707
1708 /* Read the contents of the section INFO.
1709 OBJFILE is the main object file, but not necessarily the file where
1710 the section comes from. E.g., for DWO files INFO->asection->owner
1711 is the bfd of the DWO file.
1712 If the section is compressed, uncompress it before returning. */
1713
1714 static void
1715 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1716 {
1717 asection *sectp = info->asection;
1718 bfd *abfd;
1719 gdb_byte *buf, *retbuf;
1720 unsigned char header[4];
1721
1722 if (info->readin)
1723 return;
1724 info->buffer = NULL;
1725 info->map_addr = NULL;
1726 info->readin = 1;
1727
1728 if (dwarf2_section_empty_p (info))
1729 return;
1730
1731 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1732 abfd = sectp->owner;
1733
1734 /* Check if the file has a 4-byte header indicating compression. */
1735 if (info->size > sizeof (header)
1736 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1737 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1738 {
1739 /* Upon decompression, update the buffer and its size. */
1740 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1741 {
1742 zlib_decompress_section (objfile, sectp, &info->buffer,
1743 &info->size);
1744 return;
1745 }
1746 }
1747
1748 #ifdef HAVE_MMAP
1749 if (pagesize == 0)
1750 pagesize = getpagesize ();
1751
1752 /* Only try to mmap sections which are large enough: we don't want to
1753 waste space due to fragmentation. Also, only try mmap for sections
1754 without relocations. */
1755
1756 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1757 {
1758 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1759 MAP_PRIVATE, sectp->filepos,
1760 &info->map_addr, &info->map_len);
1761
1762 if ((caddr_t)info->buffer != MAP_FAILED)
1763 {
1764 #if HAVE_POSIX_MADVISE
1765 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1766 #endif
1767 return;
1768 }
1769 }
1770 #endif
1771
1772 /* If we get here, we are a normal, not-compressed section. */
1773 info->buffer = buf
1774 = obstack_alloc (&objfile->objfile_obstack, info->size);
1775
1776 /* When debugging .o files, we may need to apply relocations; see
1777 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1778 We never compress sections in .o files, so we only need to
1779 try this when the section is not compressed. */
1780 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1781 if (retbuf != NULL)
1782 {
1783 info->buffer = retbuf;
1784 return;
1785 }
1786
1787 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1788 || bfd_bread (buf, info->size, abfd) != info->size)
1789 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1790 bfd_get_filename (abfd));
1791 }
1792
1793 /* A helper function that returns the size of a section in a safe way.
1794 If you are positive that the section has been read before using the
1795 size, then it is safe to refer to the dwarf2_section_info object's
1796 "size" field directly. In other cases, you must call this
1797 function, because for compressed sections the size field is not set
1798 correctly until the section has been read. */
1799
1800 static bfd_size_type
1801 dwarf2_section_size (struct objfile *objfile,
1802 struct dwarf2_section_info *info)
1803 {
1804 if (!info->readin)
1805 dwarf2_read_section (objfile, info);
1806 return info->size;
1807 }
1808
1809 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1810 SECTION_NAME. */
1811
1812 void
1813 dwarf2_get_section_info (struct objfile *objfile,
1814 enum dwarf2_section_enum sect,
1815 asection **sectp, gdb_byte **bufp,
1816 bfd_size_type *sizep)
1817 {
1818 struct dwarf2_per_objfile *data
1819 = objfile_data (objfile, dwarf2_objfile_data_key);
1820 struct dwarf2_section_info *info;
1821
1822 /* We may see an objfile without any DWARF, in which case we just
1823 return nothing. */
1824 if (data == NULL)
1825 {
1826 *sectp = NULL;
1827 *bufp = NULL;
1828 *sizep = 0;
1829 return;
1830 }
1831 switch (sect)
1832 {
1833 case DWARF2_DEBUG_FRAME:
1834 info = &data->frame;
1835 break;
1836 case DWARF2_EH_FRAME:
1837 info = &data->eh_frame;
1838 break;
1839 default:
1840 gdb_assert_not_reached ("unexpected section");
1841 }
1842
1843 dwarf2_read_section (objfile, info);
1844
1845 *sectp = info->asection;
1846 *bufp = info->buffer;
1847 *sizep = info->size;
1848 }
1849
1850 \f
1851 /* DWARF quick_symbols_functions support. */
1852
1853 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1854 unique line tables, so we maintain a separate table of all .debug_line
1855 derived entries to support the sharing.
1856 All the quick functions need is the list of file names. We discard the
1857 line_header when we're done and don't need to record it here. */
1858 struct quick_file_names
1859 {
1860 /* The offset in .debug_line of the line table. We hash on this. */
1861 unsigned int offset;
1862
1863 /* The number of entries in file_names, real_names. */
1864 unsigned int num_file_names;
1865
1866 /* The file names from the line table, after being run through
1867 file_full_name. */
1868 const char **file_names;
1869
1870 /* The file names from the line table after being run through
1871 gdb_realpath. These are computed lazily. */
1872 const char **real_names;
1873 };
1874
1875 /* When using the index (and thus not using psymtabs), each CU has an
1876 object of this type. This is used to hold information needed by
1877 the various "quick" methods. */
1878 struct dwarf2_per_cu_quick_data
1879 {
1880 /* The file table. This can be NULL if there was no file table
1881 or it's currently not read in.
1882 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1883 struct quick_file_names *file_names;
1884
1885 /* The corresponding symbol table. This is NULL if symbols for this
1886 CU have not yet been read. */
1887 struct symtab *symtab;
1888
1889 /* A temporary mark bit used when iterating over all CUs in
1890 expand_symtabs_matching. */
1891 unsigned int mark : 1;
1892
1893 /* True if we've tried to read the file table and found there isn't one.
1894 There will be no point in trying to read it again next time. */
1895 unsigned int no_file_data : 1;
1896 };
1897
1898 /* Hash function for a quick_file_names. */
1899
1900 static hashval_t
1901 hash_file_name_entry (const void *e)
1902 {
1903 const struct quick_file_names *file_data = e;
1904
1905 return file_data->offset;
1906 }
1907
1908 /* Equality function for a quick_file_names. */
1909
1910 static int
1911 eq_file_name_entry (const void *a, const void *b)
1912 {
1913 const struct quick_file_names *ea = a;
1914 const struct quick_file_names *eb = b;
1915
1916 return ea->offset == eb->offset;
1917 }
1918
1919 /* Delete function for a quick_file_names. */
1920
1921 static void
1922 delete_file_name_entry (void *e)
1923 {
1924 struct quick_file_names *file_data = e;
1925 int i;
1926
1927 for (i = 0; i < file_data->num_file_names; ++i)
1928 {
1929 xfree ((void*) file_data->file_names[i]);
1930 if (file_data->real_names)
1931 xfree ((void*) file_data->real_names[i]);
1932 }
1933
1934 /* The space for the struct itself lives on objfile_obstack,
1935 so we don't free it here. */
1936 }
1937
1938 /* Create a quick_file_names hash table. */
1939
1940 static htab_t
1941 create_quick_file_names_table (unsigned int nr_initial_entries)
1942 {
1943 return htab_create_alloc (nr_initial_entries,
1944 hash_file_name_entry, eq_file_name_entry,
1945 delete_file_name_entry, xcalloc, xfree);
1946 }
1947
1948 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1949 have to be created afterwards. You should call age_cached_comp_units after
1950 processing PER_CU->CU. dw2_setup must have been already called. */
1951
1952 static void
1953 load_cu (struct dwarf2_per_cu_data *per_cu)
1954 {
1955 if (per_cu->is_debug_types)
1956 load_full_type_unit (per_cu);
1957 else
1958 load_full_comp_unit (per_cu, language_minimal);
1959
1960 gdb_assert (per_cu->cu != NULL);
1961
1962 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1963 }
1964
1965 /* Read in the symbols for PER_CU. */
1966
1967 static void
1968 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1969 {
1970 struct cleanup *back_to;
1971
1972 back_to = make_cleanup (dwarf2_release_queue, NULL);
1973
1974 if (dwarf2_per_objfile->using_index
1975 ? per_cu->v.quick->symtab == NULL
1976 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1977 {
1978 queue_comp_unit (per_cu, language_minimal);
1979 load_cu (per_cu);
1980 }
1981
1982 process_queue ();
1983
1984 /* Age the cache, releasing compilation units that have not
1985 been used recently. */
1986 age_cached_comp_units ();
1987
1988 do_cleanups (back_to);
1989 }
1990
1991 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1992 the objfile from which this CU came. Returns the resulting symbol
1993 table. */
1994
1995 static struct symtab *
1996 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1997 {
1998 gdb_assert (dwarf2_per_objfile->using_index);
1999 if (!per_cu->v.quick->symtab)
2000 {
2001 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2002 increment_reading_symtab ();
2003 dw2_do_instantiate_symtab (per_cu);
2004 process_cu_includes ();
2005 do_cleanups (back_to);
2006 }
2007 return per_cu->v.quick->symtab;
2008 }
2009
2010 /* Return the CU given its index. */
2011
2012 static struct dwarf2_per_cu_data *
2013 dw2_get_cu (int index)
2014 {
2015 if (index >= dwarf2_per_objfile->n_comp_units)
2016 {
2017 index -= dwarf2_per_objfile->n_comp_units;
2018 return dwarf2_per_objfile->all_type_units[index];
2019 }
2020 return dwarf2_per_objfile->all_comp_units[index];
2021 }
2022
2023 /* A helper function that knows how to read a 64-bit value in a way
2024 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2025 otherwise. */
2026
2027 static int
2028 extract_cu_value (const char *bytes, ULONGEST *result)
2029 {
2030 if (sizeof (ULONGEST) < 8)
2031 {
2032 int i;
2033
2034 /* Ignore the upper 4 bytes if they are all zero. */
2035 for (i = 0; i < 4; ++i)
2036 if (bytes[i + 4] != 0)
2037 return 0;
2038
2039 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2040 }
2041 else
2042 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2043 return 1;
2044 }
2045
2046 /* Read the CU list from the mapped index, and use it to create all
2047 the CU objects for this objfile. Return 0 if something went wrong,
2048 1 if everything went ok. */
2049
2050 static int
2051 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2052 offset_type cu_list_elements)
2053 {
2054 offset_type i;
2055
2056 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2057 dwarf2_per_objfile->all_comp_units
2058 = obstack_alloc (&objfile->objfile_obstack,
2059 dwarf2_per_objfile->n_comp_units
2060 * sizeof (struct dwarf2_per_cu_data *));
2061
2062 for (i = 0; i < cu_list_elements; i += 2)
2063 {
2064 struct dwarf2_per_cu_data *the_cu;
2065 ULONGEST offset, length;
2066
2067 if (!extract_cu_value (cu_list, &offset)
2068 || !extract_cu_value (cu_list + 8, &length))
2069 return 0;
2070 cu_list += 2 * 8;
2071
2072 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2073 struct dwarf2_per_cu_data);
2074 the_cu->offset.sect_off = offset;
2075 the_cu->length = length;
2076 the_cu->objfile = objfile;
2077 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2078 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2079 struct dwarf2_per_cu_quick_data);
2080 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2081 }
2082
2083 return 1;
2084 }
2085
2086 /* Create the signatured type hash table from the index. */
2087
2088 static int
2089 create_signatured_type_table_from_index (struct objfile *objfile,
2090 struct dwarf2_section_info *section,
2091 const gdb_byte *bytes,
2092 offset_type elements)
2093 {
2094 offset_type i;
2095 htab_t sig_types_hash;
2096
2097 dwarf2_per_objfile->n_type_units = elements / 3;
2098 dwarf2_per_objfile->all_type_units
2099 = obstack_alloc (&objfile->objfile_obstack,
2100 dwarf2_per_objfile->n_type_units
2101 * sizeof (struct dwarf2_per_cu_data *));
2102
2103 sig_types_hash = allocate_signatured_type_table (objfile);
2104
2105 for (i = 0; i < elements; i += 3)
2106 {
2107 struct signatured_type *sig_type;
2108 ULONGEST offset, type_offset_in_tu, signature;
2109 void **slot;
2110
2111 if (!extract_cu_value (bytes, &offset)
2112 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2113 return 0;
2114 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2115 bytes += 3 * 8;
2116
2117 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2118 struct signatured_type);
2119 sig_type->signature = signature;
2120 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2121 sig_type->per_cu.is_debug_types = 1;
2122 sig_type->per_cu.info_or_types_section = section;
2123 sig_type->per_cu.offset.sect_off = offset;
2124 sig_type->per_cu.objfile = objfile;
2125 sig_type->per_cu.v.quick
2126 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2127 struct dwarf2_per_cu_quick_data);
2128
2129 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2130 *slot = sig_type;
2131
2132 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2133 }
2134
2135 dwarf2_per_objfile->signatured_types = sig_types_hash;
2136
2137 return 1;
2138 }
2139
2140 /* Read the address map data from the mapped index, and use it to
2141 populate the objfile's psymtabs_addrmap. */
2142
2143 static void
2144 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2145 {
2146 const gdb_byte *iter, *end;
2147 struct obstack temp_obstack;
2148 struct addrmap *mutable_map;
2149 struct cleanup *cleanup;
2150 CORE_ADDR baseaddr;
2151
2152 obstack_init (&temp_obstack);
2153 cleanup = make_cleanup_obstack_free (&temp_obstack);
2154 mutable_map = addrmap_create_mutable (&temp_obstack);
2155
2156 iter = index->address_table;
2157 end = iter + index->address_table_size;
2158
2159 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2160
2161 while (iter < end)
2162 {
2163 ULONGEST hi, lo, cu_index;
2164 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2165 iter += 8;
2166 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2167 iter += 8;
2168 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2169 iter += 4;
2170
2171 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2172 dw2_get_cu (cu_index));
2173 }
2174
2175 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2176 &objfile->objfile_obstack);
2177 do_cleanups (cleanup);
2178 }
2179
2180 /* The hash function for strings in the mapped index. This is the same as
2181 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2182 implementation. This is necessary because the hash function is tied to the
2183 format of the mapped index file. The hash values do not have to match with
2184 SYMBOL_HASH_NEXT.
2185
2186 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2187
2188 static hashval_t
2189 mapped_index_string_hash (int index_version, const void *p)
2190 {
2191 const unsigned char *str = (const unsigned char *) p;
2192 hashval_t r = 0;
2193 unsigned char c;
2194
2195 while ((c = *str++) != 0)
2196 {
2197 if (index_version >= 5)
2198 c = tolower (c);
2199 r = r * 67 + c - 113;
2200 }
2201
2202 return r;
2203 }
2204
2205 /* Find a slot in the mapped index INDEX for the object named NAME.
2206 If NAME is found, set *VEC_OUT to point to the CU vector in the
2207 constant pool and return 1. If NAME cannot be found, return 0. */
2208
2209 static int
2210 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2211 offset_type **vec_out)
2212 {
2213 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2214 offset_type hash;
2215 offset_type slot, step;
2216 int (*cmp) (const char *, const char *);
2217
2218 if (current_language->la_language == language_cplus
2219 || current_language->la_language == language_java
2220 || current_language->la_language == language_fortran)
2221 {
2222 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2223 not contain any. */
2224 const char *paren = strchr (name, '(');
2225
2226 if (paren)
2227 {
2228 char *dup;
2229
2230 dup = xmalloc (paren - name + 1);
2231 memcpy (dup, name, paren - name);
2232 dup[paren - name] = 0;
2233
2234 make_cleanup (xfree, dup);
2235 name = dup;
2236 }
2237 }
2238
2239 /* Index version 4 did not support case insensitive searches. But the
2240 indices for case insensitive languages are built in lowercase, therefore
2241 simulate our NAME being searched is also lowercased. */
2242 hash = mapped_index_string_hash ((index->version == 4
2243 && case_sensitivity == case_sensitive_off
2244 ? 5 : index->version),
2245 name);
2246
2247 slot = hash & (index->symbol_table_slots - 1);
2248 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2249 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2250
2251 for (;;)
2252 {
2253 /* Convert a slot number to an offset into the table. */
2254 offset_type i = 2 * slot;
2255 const char *str;
2256 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2257 {
2258 do_cleanups (back_to);
2259 return 0;
2260 }
2261
2262 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2263 if (!cmp (name, str))
2264 {
2265 *vec_out = (offset_type *) (index->constant_pool
2266 + MAYBE_SWAP (index->symbol_table[i + 1]));
2267 do_cleanups (back_to);
2268 return 1;
2269 }
2270
2271 slot = (slot + step) & (index->symbol_table_slots - 1);
2272 }
2273 }
2274
2275 /* Read the index file. If everything went ok, initialize the "quick"
2276 elements of all the CUs and return 1. Otherwise, return 0. */
2277
2278 static int
2279 dwarf2_read_index (struct objfile *objfile)
2280 {
2281 char *addr;
2282 struct mapped_index *map;
2283 offset_type *metadata;
2284 const gdb_byte *cu_list;
2285 const gdb_byte *types_list = NULL;
2286 offset_type version, cu_list_elements;
2287 offset_type types_list_elements = 0;
2288 int i;
2289
2290 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2291 return 0;
2292
2293 /* Older elfutils strip versions could keep the section in the main
2294 executable while splitting it for the separate debug info file. */
2295 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2296 & SEC_HAS_CONTENTS) == 0)
2297 return 0;
2298
2299 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2300
2301 addr = dwarf2_per_objfile->gdb_index.buffer;
2302 /* Version check. */
2303 version = MAYBE_SWAP (*(offset_type *) addr);
2304 /* Versions earlier than 3 emitted every copy of a psymbol. This
2305 causes the index to behave very poorly for certain requests. Version 3
2306 contained incomplete addrmap. So, it seems better to just ignore such
2307 indices. */
2308 if (version < 4)
2309 {
2310 static int warning_printed = 0;
2311 if (!warning_printed)
2312 {
2313 warning (_("Skipping obsolete .gdb_index section in %s."),
2314 objfile->name);
2315 warning_printed = 1;
2316 }
2317 return 0;
2318 }
2319 /* Index version 4 uses a different hash function than index version
2320 5 and later.
2321
2322 Versions earlier than 6 did not emit psymbols for inlined
2323 functions. Using these files will cause GDB not to be able to
2324 set breakpoints on inlined functions by name, so we ignore these
2325 indices unless the --use-deprecated-index-sections command line
2326 option was supplied. */
2327 if (version < 6 && !use_deprecated_index_sections)
2328 {
2329 static int warning_printed = 0;
2330 if (!warning_printed)
2331 {
2332 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2333 "--use-deprecated-index-sections to use them anyway"),
2334 objfile->name);
2335 warning_printed = 1;
2336 }
2337 return 0;
2338 }
2339 /* Indexes with higher version than the one supported by GDB may be no
2340 longer backward compatible. */
2341 if (version > 6)
2342 return 0;
2343
2344 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2345 map->version = version;
2346 map->total_size = dwarf2_per_objfile->gdb_index.size;
2347
2348 metadata = (offset_type *) (addr + sizeof (offset_type));
2349
2350 i = 0;
2351 cu_list = addr + MAYBE_SWAP (metadata[i]);
2352 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2353 / 8);
2354 ++i;
2355
2356 types_list = addr + MAYBE_SWAP (metadata[i]);
2357 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2358 - MAYBE_SWAP (metadata[i]))
2359 / 8);
2360 ++i;
2361
2362 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2363 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2364 - MAYBE_SWAP (metadata[i]));
2365 ++i;
2366
2367 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2368 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2369 - MAYBE_SWAP (metadata[i]))
2370 / (2 * sizeof (offset_type)));
2371 ++i;
2372
2373 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2374
2375 /* Don't use the index if it's empty. */
2376 if (map->symbol_table_slots == 0)
2377 return 0;
2378
2379 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2380 return 0;
2381
2382 if (types_list_elements)
2383 {
2384 struct dwarf2_section_info *section;
2385
2386 /* We can only handle a single .debug_types when we have an
2387 index. */
2388 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2389 return 0;
2390
2391 section = VEC_index (dwarf2_section_info_def,
2392 dwarf2_per_objfile->types, 0);
2393
2394 if (!create_signatured_type_table_from_index (objfile, section,
2395 types_list,
2396 types_list_elements))
2397 return 0;
2398 }
2399
2400 create_addrmap_from_index (objfile, map);
2401
2402 dwarf2_per_objfile->index_table = map;
2403 dwarf2_per_objfile->using_index = 1;
2404 dwarf2_per_objfile->quick_file_names_table =
2405 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2406
2407 return 1;
2408 }
2409
2410 /* A helper for the "quick" functions which sets the global
2411 dwarf2_per_objfile according to OBJFILE. */
2412
2413 static void
2414 dw2_setup (struct objfile *objfile)
2415 {
2416 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2417 gdb_assert (dwarf2_per_objfile);
2418 }
2419
2420 /* die_reader_func for dw2_get_file_names. */
2421
2422 static void
2423 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2424 gdb_byte *info_ptr,
2425 struct die_info *comp_unit_die,
2426 int has_children,
2427 void *data)
2428 {
2429 struct dwarf2_cu *cu = reader->cu;
2430 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2431 struct objfile *objfile = dwarf2_per_objfile->objfile;
2432 struct line_header *lh;
2433 struct attribute *attr;
2434 int i;
2435 unsigned int bytes_read;
2436 char *name, *comp_dir;
2437 void **slot;
2438 struct quick_file_names *qfn;
2439 unsigned int line_offset;
2440
2441 lh = NULL;
2442 slot = NULL;
2443 line_offset = 0;
2444
2445 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2446 if (attr)
2447 {
2448 struct quick_file_names find_entry;
2449
2450 line_offset = DW_UNSND (attr);
2451
2452 /* We may have already read in this line header (TU line header sharing).
2453 If we have we're done. */
2454 find_entry.offset = line_offset;
2455 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2456 &find_entry, INSERT);
2457 if (*slot != NULL)
2458 {
2459 this_cu->v.quick->file_names = *slot;
2460 return;
2461 }
2462
2463 lh = dwarf_decode_line_header (line_offset, cu);
2464 }
2465 if (lh == NULL)
2466 {
2467 this_cu->v.quick->no_file_data = 1;
2468 return;
2469 }
2470
2471 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2472 qfn->offset = line_offset;
2473 gdb_assert (slot != NULL);
2474 *slot = qfn;
2475
2476 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2477
2478 qfn->num_file_names = lh->num_file_names;
2479 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2480 lh->num_file_names * sizeof (char *));
2481 for (i = 0; i < lh->num_file_names; ++i)
2482 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2483 qfn->real_names = NULL;
2484
2485 free_line_header (lh);
2486
2487 this_cu->v.quick->file_names = qfn;
2488 }
2489
2490 /* A helper for the "quick" functions which attempts to read the line
2491 table for THIS_CU. */
2492
2493 static struct quick_file_names *
2494 dw2_get_file_names (struct objfile *objfile,
2495 struct dwarf2_per_cu_data *this_cu)
2496 {
2497 if (this_cu->v.quick->file_names != NULL)
2498 return this_cu->v.quick->file_names;
2499 /* If we know there is no line data, no point in looking again. */
2500 if (this_cu->v.quick->no_file_data)
2501 return NULL;
2502
2503 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2504 in the stub for CUs, there's is no need to lookup the DWO file.
2505 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2506 DWO file. */
2507 if (this_cu->is_debug_types)
2508 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2509 else
2510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2511
2512 if (this_cu->v.quick->no_file_data)
2513 return NULL;
2514 return this_cu->v.quick->file_names;
2515 }
2516
2517 /* A helper for the "quick" functions which computes and caches the
2518 real path for a given file name from the line table. */
2519
2520 static const char *
2521 dw2_get_real_path (struct objfile *objfile,
2522 struct quick_file_names *qfn, int index)
2523 {
2524 if (qfn->real_names == NULL)
2525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2526 qfn->num_file_names, sizeof (char *));
2527
2528 if (qfn->real_names[index] == NULL)
2529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2530
2531 return qfn->real_names[index];
2532 }
2533
2534 static struct symtab *
2535 dw2_find_last_source_symtab (struct objfile *objfile)
2536 {
2537 int index;
2538
2539 dw2_setup (objfile);
2540 index = dwarf2_per_objfile->n_comp_units - 1;
2541 return dw2_instantiate_symtab (dw2_get_cu (index));
2542 }
2543
2544 /* Traversal function for dw2_forget_cached_source_info. */
2545
2546 static int
2547 dw2_free_cached_file_names (void **slot, void *info)
2548 {
2549 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2550
2551 if (file_data->real_names)
2552 {
2553 int i;
2554
2555 for (i = 0; i < file_data->num_file_names; ++i)
2556 {
2557 xfree ((void*) file_data->real_names[i]);
2558 file_data->real_names[i] = NULL;
2559 }
2560 }
2561
2562 return 1;
2563 }
2564
2565 static void
2566 dw2_forget_cached_source_info (struct objfile *objfile)
2567 {
2568 dw2_setup (objfile);
2569
2570 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2571 dw2_free_cached_file_names, NULL);
2572 }
2573
2574 /* Helper function for dw2_map_symtabs_matching_filename that expands
2575 the symtabs and calls the iterator. */
2576
2577 static int
2578 dw2_map_expand_apply (struct objfile *objfile,
2579 struct dwarf2_per_cu_data *per_cu,
2580 const char *name,
2581 const char *full_path, const char *real_path,
2582 int (*callback) (struct symtab *, void *),
2583 void *data)
2584 {
2585 struct symtab *last_made = objfile->symtabs;
2586
2587 /* Don't visit already-expanded CUs. */
2588 if (per_cu->v.quick->symtab)
2589 return 0;
2590
2591 /* This may expand more than one symtab, and we want to iterate over
2592 all of them. */
2593 dw2_instantiate_symtab (per_cu);
2594
2595 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2596 objfile->symtabs, last_made);
2597 }
2598
2599 /* Implementation of the map_symtabs_matching_filename method. */
2600
2601 static int
2602 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2603 const char *full_path, const char *real_path,
2604 int (*callback) (struct symtab *, void *),
2605 void *data)
2606 {
2607 int i;
2608 const char *name_basename = lbasename (name);
2609 int name_len = strlen (name);
2610 int is_abs = IS_ABSOLUTE_PATH (name);
2611
2612 dw2_setup (objfile);
2613
2614 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2615 + dwarf2_per_objfile->n_type_units); ++i)
2616 {
2617 int j;
2618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2619 struct quick_file_names *file_data;
2620
2621 /* We only need to look at symtabs not already expanded. */
2622 if (per_cu->v.quick->symtab)
2623 continue;
2624
2625 file_data = dw2_get_file_names (objfile, per_cu);
2626 if (file_data == NULL)
2627 continue;
2628
2629 for (j = 0; j < file_data->num_file_names; ++j)
2630 {
2631 const char *this_name = file_data->file_names[j];
2632
2633 if (FILENAME_CMP (name, this_name) == 0
2634 || (!is_abs && compare_filenames_for_search (this_name,
2635 name, name_len)))
2636 {
2637 if (dw2_map_expand_apply (objfile, per_cu,
2638 name, full_path, real_path,
2639 callback, data))
2640 return 1;
2641 }
2642
2643 /* Before we invoke realpath, which can get expensive when many
2644 files are involved, do a quick comparison of the basenames. */
2645 if (! basenames_may_differ
2646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2647 continue;
2648
2649 if (full_path != NULL)
2650 {
2651 const char *this_real_name = dw2_get_real_path (objfile,
2652 file_data, j);
2653
2654 if (this_real_name != NULL
2655 && (FILENAME_CMP (full_path, this_real_name) == 0
2656 || (!is_abs
2657 && compare_filenames_for_search (this_real_name,
2658 name, name_len))))
2659 {
2660 if (dw2_map_expand_apply (objfile, per_cu,
2661 name, full_path, real_path,
2662 callback, data))
2663 return 1;
2664 }
2665 }
2666
2667 if (real_path != NULL)
2668 {
2669 const char *this_real_name = dw2_get_real_path (objfile,
2670 file_data, j);
2671
2672 if (this_real_name != NULL
2673 && (FILENAME_CMP (real_path, this_real_name) == 0
2674 || (!is_abs
2675 && compare_filenames_for_search (this_real_name,
2676 name, name_len))))
2677 {
2678 if (dw2_map_expand_apply (objfile, per_cu,
2679 name, full_path, real_path,
2680 callback, data))
2681 return 1;
2682 }
2683 }
2684 }
2685 }
2686
2687 return 0;
2688 }
2689
2690 static struct symtab *
2691 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2692 const char *name, domain_enum domain)
2693 {
2694 /* We do all the work in the pre_expand_symtabs_matching hook
2695 instead. */
2696 return NULL;
2697 }
2698
2699 /* A helper function that expands all symtabs that hold an object
2700 named NAME. */
2701
2702 static void
2703 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2704 {
2705 dw2_setup (objfile);
2706
2707 /* index_table is NULL if OBJF_READNOW. */
2708 if (dwarf2_per_objfile->index_table)
2709 {
2710 offset_type *vec;
2711
2712 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2713 name, &vec))
2714 {
2715 offset_type i, len = MAYBE_SWAP (*vec);
2716 for (i = 0; i < len; ++i)
2717 {
2718 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2719 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2720
2721 dw2_instantiate_symtab (per_cu);
2722 }
2723 }
2724 }
2725 }
2726
2727 static void
2728 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2729 enum block_enum block_kind, const char *name,
2730 domain_enum domain)
2731 {
2732 dw2_do_expand_symtabs_matching (objfile, name);
2733 }
2734
2735 static void
2736 dw2_print_stats (struct objfile *objfile)
2737 {
2738 int i, count;
2739
2740 dw2_setup (objfile);
2741 count = 0;
2742 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2743 + dwarf2_per_objfile->n_type_units); ++i)
2744 {
2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2746
2747 if (!per_cu->v.quick->symtab)
2748 ++count;
2749 }
2750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2751 }
2752
2753 static void
2754 dw2_dump (struct objfile *objfile)
2755 {
2756 /* Nothing worth printing. */
2757 }
2758
2759 static void
2760 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2761 struct section_offsets *delta)
2762 {
2763 /* There's nothing to relocate here. */
2764 }
2765
2766 static void
2767 dw2_expand_symtabs_for_function (struct objfile *objfile,
2768 const char *func_name)
2769 {
2770 dw2_do_expand_symtabs_matching (objfile, func_name);
2771 }
2772
2773 static void
2774 dw2_expand_all_symtabs (struct objfile *objfile)
2775 {
2776 int i;
2777
2778 dw2_setup (objfile);
2779
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2781 + dwarf2_per_objfile->n_type_units); ++i)
2782 {
2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2784
2785 dw2_instantiate_symtab (per_cu);
2786 }
2787 }
2788
2789 static void
2790 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2791 const char *filename)
2792 {
2793 int i;
2794
2795 dw2_setup (objfile);
2796
2797 /* We don't need to consider type units here.
2798 This is only called for examining code, e.g. expand_line_sal.
2799 There can be an order of magnitude (or more) more type units
2800 than comp units, and we avoid them if we can. */
2801
2802 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2803 {
2804 int j;
2805 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2806 struct quick_file_names *file_data;
2807
2808 /* We only need to look at symtabs not already expanded. */
2809 if (per_cu->v.quick->symtab)
2810 continue;
2811
2812 file_data = dw2_get_file_names (objfile, per_cu);
2813 if (file_data == NULL)
2814 continue;
2815
2816 for (j = 0; j < file_data->num_file_names; ++j)
2817 {
2818 const char *this_name = file_data->file_names[j];
2819 if (FILENAME_CMP (this_name, filename) == 0)
2820 {
2821 dw2_instantiate_symtab (per_cu);
2822 break;
2823 }
2824 }
2825 }
2826 }
2827
2828 static const char *
2829 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2830 {
2831 struct dwarf2_per_cu_data *per_cu;
2832 offset_type *vec;
2833 struct quick_file_names *file_data;
2834
2835 dw2_setup (objfile);
2836
2837 /* index_table is NULL if OBJF_READNOW. */
2838 if (!dwarf2_per_objfile->index_table)
2839 {
2840 struct symtab *s;
2841
2842 ALL_OBJFILE_SYMTABS (objfile, s)
2843 if (s->primary)
2844 {
2845 struct blockvector *bv = BLOCKVECTOR (s);
2846 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2847 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2848
2849 if (sym)
2850 return sym->symtab->filename;
2851 }
2852 return NULL;
2853 }
2854
2855 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2856 name, &vec))
2857 return NULL;
2858
2859 /* Note that this just looks at the very first one named NAME -- but
2860 actually we are looking for a function. find_main_filename
2861 should be rewritten so that it doesn't require a custom hook. It
2862 could just use the ordinary symbol tables. */
2863 /* vec[0] is the length, which must always be >0. */
2864 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2865
2866 file_data = dw2_get_file_names (objfile, per_cu);
2867 if (file_data == NULL
2868 || file_data->num_file_names == 0)
2869 return NULL;
2870
2871 return file_data->file_names[file_data->num_file_names - 1];
2872 }
2873
2874 static void
2875 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2876 struct objfile *objfile, int global,
2877 int (*callback) (struct block *,
2878 struct symbol *, void *),
2879 void *data, symbol_compare_ftype *match,
2880 symbol_compare_ftype *ordered_compare)
2881 {
2882 /* Currently unimplemented; used for Ada. The function can be called if the
2883 current language is Ada for a non-Ada objfile using GNU index. As Ada
2884 does not look for non-Ada symbols this function should just return. */
2885 }
2886
2887 static void
2888 dw2_expand_symtabs_matching
2889 (struct objfile *objfile,
2890 int (*file_matcher) (const char *, void *),
2891 int (*name_matcher) (const char *, void *),
2892 enum search_domain kind,
2893 void *data)
2894 {
2895 int i;
2896 offset_type iter;
2897 struct mapped_index *index;
2898
2899 dw2_setup (objfile);
2900
2901 /* index_table is NULL if OBJF_READNOW. */
2902 if (!dwarf2_per_objfile->index_table)
2903 return;
2904 index = dwarf2_per_objfile->index_table;
2905
2906 if (file_matcher != NULL)
2907 {
2908 struct cleanup *cleanup;
2909 htab_t visited_found, visited_not_found;
2910
2911 visited_found = htab_create_alloc (10,
2912 htab_hash_pointer, htab_eq_pointer,
2913 NULL, xcalloc, xfree);
2914 cleanup = make_cleanup_htab_delete (visited_found);
2915 visited_not_found = htab_create_alloc (10,
2916 htab_hash_pointer, htab_eq_pointer,
2917 NULL, xcalloc, xfree);
2918 make_cleanup_htab_delete (visited_not_found);
2919
2920 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2921 + dwarf2_per_objfile->n_type_units); ++i)
2922 {
2923 int j;
2924 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2925 struct quick_file_names *file_data;
2926 void **slot;
2927
2928 per_cu->v.quick->mark = 0;
2929
2930 /* We only need to look at symtabs not already expanded. */
2931 if (per_cu->v.quick->symtab)
2932 continue;
2933
2934 file_data = dw2_get_file_names (objfile, per_cu);
2935 if (file_data == NULL)
2936 continue;
2937
2938 if (htab_find (visited_not_found, file_data) != NULL)
2939 continue;
2940 else if (htab_find (visited_found, file_data) != NULL)
2941 {
2942 per_cu->v.quick->mark = 1;
2943 continue;
2944 }
2945
2946 for (j = 0; j < file_data->num_file_names; ++j)
2947 {
2948 if (file_matcher (file_data->file_names[j], data))
2949 {
2950 per_cu->v.quick->mark = 1;
2951 break;
2952 }
2953 }
2954
2955 slot = htab_find_slot (per_cu->v.quick->mark
2956 ? visited_found
2957 : visited_not_found,
2958 file_data, INSERT);
2959 *slot = file_data;
2960 }
2961
2962 do_cleanups (cleanup);
2963 }
2964
2965 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2966 {
2967 offset_type idx = 2 * iter;
2968 const char *name;
2969 offset_type *vec, vec_len, vec_idx;
2970
2971 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2972 continue;
2973
2974 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2975
2976 if (! (*name_matcher) (name, data))
2977 continue;
2978
2979 /* The name was matched, now expand corresponding CUs that were
2980 marked. */
2981 vec = (offset_type *) (index->constant_pool
2982 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2983 vec_len = MAYBE_SWAP (vec[0]);
2984 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2985 {
2986 struct dwarf2_per_cu_data *per_cu;
2987
2988 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2989 if (file_matcher == NULL || per_cu->v.quick->mark)
2990 dw2_instantiate_symtab (per_cu);
2991 }
2992 }
2993 }
2994
2995 static struct symtab *
2996 dw2_find_pc_sect_symtab (struct objfile *objfile,
2997 struct minimal_symbol *msymbol,
2998 CORE_ADDR pc,
2999 struct obj_section *section,
3000 int warn_if_readin)
3001 {
3002 struct dwarf2_per_cu_data *data;
3003
3004 dw2_setup (objfile);
3005
3006 if (!objfile->psymtabs_addrmap)
3007 return NULL;
3008
3009 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3010 if (!data)
3011 return NULL;
3012
3013 if (warn_if_readin && data->v.quick->symtab)
3014 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3015 paddress (get_objfile_arch (objfile), pc));
3016
3017 return dw2_instantiate_symtab (data);
3018 }
3019
3020 static void
3021 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3022 void *data, int need_fullname)
3023 {
3024 int i;
3025 struct cleanup *cleanup;
3026 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3027 NULL, xcalloc, xfree);
3028
3029 cleanup = make_cleanup_htab_delete (visited);
3030 dw2_setup (objfile);
3031
3032 /* We can ignore file names coming from already-expanded CUs. */
3033 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3034 + dwarf2_per_objfile->n_type_units); ++i)
3035 {
3036 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3037
3038 if (per_cu->v.quick->symtab)
3039 {
3040 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3041 INSERT);
3042
3043 *slot = per_cu->v.quick->file_names;
3044 }
3045 }
3046
3047 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3048 + dwarf2_per_objfile->n_type_units); ++i)
3049 {
3050 int j;
3051 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3052 struct quick_file_names *file_data;
3053 void **slot;
3054
3055 /* We only need to look at symtabs not already expanded. */
3056 if (per_cu->v.quick->symtab)
3057 continue;
3058
3059 file_data = dw2_get_file_names (objfile, per_cu);
3060 if (file_data == NULL)
3061 continue;
3062
3063 slot = htab_find_slot (visited, file_data, INSERT);
3064 if (*slot)
3065 {
3066 /* Already visited. */
3067 continue;
3068 }
3069 *slot = file_data;
3070
3071 for (j = 0; j < file_data->num_file_names; ++j)
3072 {
3073 const char *this_real_name;
3074
3075 if (need_fullname)
3076 this_real_name = dw2_get_real_path (objfile, file_data, j);
3077 else
3078 this_real_name = NULL;
3079 (*fun) (file_data->file_names[j], this_real_name, data);
3080 }
3081 }
3082
3083 do_cleanups (cleanup);
3084 }
3085
3086 static int
3087 dw2_has_symbols (struct objfile *objfile)
3088 {
3089 return 1;
3090 }
3091
3092 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3093 {
3094 dw2_has_symbols,
3095 dw2_find_last_source_symtab,
3096 dw2_forget_cached_source_info,
3097 dw2_map_symtabs_matching_filename,
3098 dw2_lookup_symbol,
3099 dw2_pre_expand_symtabs_matching,
3100 dw2_print_stats,
3101 dw2_dump,
3102 dw2_relocate,
3103 dw2_expand_symtabs_for_function,
3104 dw2_expand_all_symtabs,
3105 dw2_expand_symtabs_with_filename,
3106 dw2_find_symbol_file,
3107 dw2_map_matching_symbols,
3108 dw2_expand_symtabs_matching,
3109 dw2_find_pc_sect_symtab,
3110 dw2_map_symbol_filenames
3111 };
3112
3113 /* Initialize for reading DWARF for this objfile. Return 0 if this
3114 file will use psymtabs, or 1 if using the GNU index. */
3115
3116 int
3117 dwarf2_initialize_objfile (struct objfile *objfile)
3118 {
3119 /* If we're about to read full symbols, don't bother with the
3120 indices. In this case we also don't care if some other debug
3121 format is making psymtabs, because they are all about to be
3122 expanded anyway. */
3123 if ((objfile->flags & OBJF_READNOW))
3124 {
3125 int i;
3126
3127 dwarf2_per_objfile->using_index = 1;
3128 create_all_comp_units (objfile);
3129 create_all_type_units (objfile);
3130 dwarf2_per_objfile->quick_file_names_table =
3131 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3132
3133 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3134 + dwarf2_per_objfile->n_type_units); ++i)
3135 {
3136 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3137
3138 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct dwarf2_per_cu_quick_data);
3140 }
3141
3142 /* Return 1 so that gdb sees the "quick" functions. However,
3143 these functions will be no-ops because we will have expanded
3144 all symtabs. */
3145 return 1;
3146 }
3147
3148 if (dwarf2_read_index (objfile))
3149 return 1;
3150
3151 return 0;
3152 }
3153
3154 \f
3155
3156 /* Build a partial symbol table. */
3157
3158 void
3159 dwarf2_build_psymtabs (struct objfile *objfile)
3160 {
3161 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3162 {
3163 init_psymbol_list (objfile, 1024);
3164 }
3165
3166 dwarf2_build_psymtabs_hard (objfile);
3167 }
3168
3169 /* Return TRUE if OFFSET is within CU_HEADER. */
3170
3171 static inline int
3172 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3173 {
3174 sect_offset bottom = { cu_header->offset.sect_off };
3175 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3176 + cu_header->initial_length_size) };
3177
3178 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3179 }
3180
3181 /* Read in the comp unit header information from the debug_info at info_ptr.
3182 NOTE: This leaves members offset, first_die_offset to be filled in
3183 by the caller. */
3184
3185 static gdb_byte *
3186 read_comp_unit_head (struct comp_unit_head *cu_header,
3187 gdb_byte *info_ptr, bfd *abfd)
3188 {
3189 int signed_addr;
3190 unsigned int bytes_read;
3191
3192 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3193 cu_header->initial_length_size = bytes_read;
3194 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3195 info_ptr += bytes_read;
3196 cu_header->version = read_2_bytes (abfd, info_ptr);
3197 info_ptr += 2;
3198 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3199 &bytes_read);
3200 info_ptr += bytes_read;
3201 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3202 info_ptr += 1;
3203 signed_addr = bfd_get_sign_extend_vma (abfd);
3204 if (signed_addr < 0)
3205 internal_error (__FILE__, __LINE__,
3206 _("read_comp_unit_head: dwarf from non elf file"));
3207 cu_header->signed_addr_p = signed_addr;
3208
3209 return info_ptr;
3210 }
3211
3212 /* Subroutine of read_and_check_comp_unit_head and
3213 read_and_check_type_unit_head to simplify them.
3214 Perform various error checking on the header. */
3215
3216 static void
3217 error_check_comp_unit_head (struct comp_unit_head *header,
3218 struct dwarf2_section_info *section)
3219 {
3220 bfd *abfd = section->asection->owner;
3221 const char *filename = bfd_get_filename (abfd);
3222
3223 if (header->version != 2 && header->version != 3 && header->version != 4)
3224 error (_("Dwarf Error: wrong version in compilation unit header "
3225 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3226 filename);
3227
3228 if (header->abbrev_offset.sect_off
3229 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3230 &dwarf2_per_objfile->abbrev))
3231 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3232 "(offset 0x%lx + 6) [in module %s]"),
3233 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3234 filename);
3235
3236 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3237 avoid potential 32-bit overflow. */
3238 if (((unsigned long) header->offset.sect_off
3239 + header->length + header->initial_length_size)
3240 > section->size)
3241 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3242 "(offset 0x%lx + 0) [in module %s]"),
3243 (long) header->length, (long) header->offset.sect_off,
3244 filename);
3245 }
3246
3247 /* Read in a CU/TU header and perform some basic error checking.
3248 The contents of the header are stored in HEADER.
3249 The result is a pointer to the start of the first DIE. */
3250
3251 static gdb_byte *
3252 read_and_check_comp_unit_head (struct comp_unit_head *header,
3253 struct dwarf2_section_info *section,
3254 gdb_byte *info_ptr,
3255 int is_debug_types_section)
3256 {
3257 gdb_byte *beg_of_comp_unit = info_ptr;
3258 bfd *abfd = section->asection->owner;
3259
3260 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3261
3262 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3263
3264 /* If we're reading a type unit, skip over the signature and
3265 type_offset fields. */
3266 if (is_debug_types_section)
3267 info_ptr += 8 /*signature*/ + header->offset_size;
3268
3269 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3270
3271 error_check_comp_unit_head (header, section);
3272
3273 return info_ptr;
3274 }
3275
3276 /* Read in the types comp unit header information from .debug_types entry at
3277 types_ptr. The result is a pointer to one past the end of the header. */
3278
3279 static gdb_byte *
3280 read_and_check_type_unit_head (struct comp_unit_head *header,
3281 struct dwarf2_section_info *section,
3282 gdb_byte *info_ptr,
3283 ULONGEST *signature,
3284 cu_offset *type_offset_in_tu)
3285 {
3286 gdb_byte *beg_of_comp_unit = info_ptr;
3287 bfd *abfd = section->asection->owner;
3288
3289 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3290
3291 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3292
3293 /* If we're reading a type unit, skip over the signature and
3294 type_offset fields. */
3295 if (signature != NULL)
3296 *signature = read_8_bytes (abfd, info_ptr);
3297 info_ptr += 8;
3298 if (type_offset_in_tu != NULL)
3299 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3300 header->offset_size);
3301 info_ptr += header->offset_size;
3302
3303 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3304
3305 error_check_comp_unit_head (header, section);
3306
3307 return info_ptr;
3308 }
3309
3310 /* Allocate a new partial symtab for file named NAME and mark this new
3311 partial symtab as being an include of PST. */
3312
3313 static void
3314 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3315 struct objfile *objfile)
3316 {
3317 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3318
3319 subpst->section_offsets = pst->section_offsets;
3320 subpst->textlow = 0;
3321 subpst->texthigh = 0;
3322
3323 subpst->dependencies = (struct partial_symtab **)
3324 obstack_alloc (&objfile->objfile_obstack,
3325 sizeof (struct partial_symtab *));
3326 subpst->dependencies[0] = pst;
3327 subpst->number_of_dependencies = 1;
3328
3329 subpst->globals_offset = 0;
3330 subpst->n_global_syms = 0;
3331 subpst->statics_offset = 0;
3332 subpst->n_static_syms = 0;
3333 subpst->symtab = NULL;
3334 subpst->read_symtab = pst->read_symtab;
3335 subpst->readin = 0;
3336
3337 /* No private part is necessary for include psymtabs. This property
3338 can be used to differentiate between such include psymtabs and
3339 the regular ones. */
3340 subpst->read_symtab_private = NULL;
3341 }
3342
3343 /* Read the Line Number Program data and extract the list of files
3344 included by the source file represented by PST. Build an include
3345 partial symtab for each of these included files. */
3346
3347 static void
3348 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3349 struct die_info *die,
3350 struct partial_symtab *pst)
3351 {
3352 struct line_header *lh = NULL;
3353 struct attribute *attr;
3354
3355 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3356 if (attr)
3357 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3358 if (lh == NULL)
3359 return; /* No linetable, so no includes. */
3360
3361 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3362 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3363
3364 free_line_header (lh);
3365 }
3366
3367 static hashval_t
3368 hash_signatured_type (const void *item)
3369 {
3370 const struct signatured_type *sig_type = item;
3371
3372 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3373 return sig_type->signature;
3374 }
3375
3376 static int
3377 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3378 {
3379 const struct signatured_type *lhs = item_lhs;
3380 const struct signatured_type *rhs = item_rhs;
3381
3382 return lhs->signature == rhs->signature;
3383 }
3384
3385 /* Allocate a hash table for signatured types. */
3386
3387 static htab_t
3388 allocate_signatured_type_table (struct objfile *objfile)
3389 {
3390 return htab_create_alloc_ex (41,
3391 hash_signatured_type,
3392 eq_signatured_type,
3393 NULL,
3394 &objfile->objfile_obstack,
3395 hashtab_obstack_allocate,
3396 dummy_obstack_deallocate);
3397 }
3398
3399 /* A helper function to add a signatured type CU to a table. */
3400
3401 static int
3402 add_signatured_type_cu_to_table (void **slot, void *datum)
3403 {
3404 struct signatured_type *sigt = *slot;
3405 struct dwarf2_per_cu_data ***datap = datum;
3406
3407 **datap = &sigt->per_cu;
3408 ++*datap;
3409
3410 return 1;
3411 }
3412
3413 /* Create the hash table of all entries in the .debug_types section.
3414 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3415 The result is a pointer to the hash table or NULL if there are
3416 no types. */
3417
3418 static htab_t
3419 create_debug_types_hash_table (struct dwo_file *dwo_file,
3420 VEC (dwarf2_section_info_def) *types)
3421 {
3422 struct objfile *objfile = dwarf2_per_objfile->objfile;
3423 htab_t types_htab = NULL;
3424 int ix;
3425 struct dwarf2_section_info *section;
3426
3427 if (VEC_empty (dwarf2_section_info_def, types))
3428 return NULL;
3429
3430 for (ix = 0;
3431 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3432 ++ix)
3433 {
3434 bfd *abfd;
3435 gdb_byte *info_ptr, *end_ptr;
3436
3437 dwarf2_read_section (objfile, section);
3438 info_ptr = section->buffer;
3439
3440 if (info_ptr == NULL)
3441 continue;
3442
3443 /* We can't set abfd until now because the section may be empty or
3444 not present, in which case section->asection will be NULL. */
3445 abfd = section->asection->owner;
3446
3447 if (types_htab == NULL)
3448 {
3449 if (dwo_file)
3450 types_htab = allocate_dwo_unit_table (objfile);
3451 else
3452 types_htab = allocate_signatured_type_table (objfile);
3453 }
3454
3455 if (dwarf2_die_debug)
3456 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3457 bfd_get_filename (abfd));
3458
3459 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3460 because we don't need to read any dies: the signature is in the
3461 header. */
3462
3463 end_ptr = info_ptr + section->size;
3464 while (info_ptr < end_ptr)
3465 {
3466 sect_offset offset;
3467 cu_offset type_offset_in_tu;
3468 ULONGEST signature;
3469 struct signatured_type *sig_type;
3470 struct dwo_unit *dwo_tu;
3471 void **slot;
3472 gdb_byte *ptr = info_ptr;
3473 struct comp_unit_head header;
3474 unsigned int length;
3475
3476 offset.sect_off = ptr - section->buffer;
3477
3478 /* We need to read the type's signature in order to build the hash
3479 table, but we don't need anything else just yet. */
3480
3481 ptr = read_and_check_type_unit_head (&header, section, ptr,
3482 &signature, &type_offset_in_tu);
3483
3484 length = header.initial_length_size + header.length;
3485
3486 /* Skip dummy type units. */
3487 if (ptr >= info_ptr + length
3488 || peek_abbrev_code (abfd, ptr) == 0)
3489 {
3490 info_ptr += header.initial_length_size + header.length;
3491 continue;
3492 }
3493
3494 if (dwo_file)
3495 {
3496 sig_type = NULL;
3497 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3498 struct dwo_unit);
3499 dwo_tu->dwo_file = dwo_file;
3500 dwo_tu->signature = signature;
3501 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3502 dwo_tu->info_or_types_section = section;
3503 dwo_tu->offset = offset;
3504 dwo_tu->length = length;
3505 }
3506 else
3507 {
3508 /* N.B.: type_offset is not usable if this type uses a DWO file.
3509 The real type_offset is in the DWO file. */
3510 dwo_tu = NULL;
3511 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3512 struct signatured_type);
3513 sig_type->signature = signature;
3514 sig_type->type_offset_in_tu = type_offset_in_tu;
3515 sig_type->per_cu.objfile = objfile;
3516 sig_type->per_cu.is_debug_types = 1;
3517 sig_type->per_cu.info_or_types_section = section;
3518 sig_type->per_cu.offset = offset;
3519 sig_type->per_cu.length = length;
3520 }
3521
3522 slot = htab_find_slot (types_htab,
3523 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3524 INSERT);
3525 gdb_assert (slot != NULL);
3526 if (*slot != NULL)
3527 {
3528 sect_offset dup_offset;
3529
3530 if (dwo_file)
3531 {
3532 const struct dwo_unit *dup_tu = *slot;
3533
3534 dup_offset = dup_tu->offset;
3535 }
3536 else
3537 {
3538 const struct signatured_type *dup_tu = *slot;
3539
3540 dup_offset = dup_tu->per_cu.offset;
3541 }
3542
3543 complaint (&symfile_complaints,
3544 _("debug type entry at offset 0x%x is duplicate to the "
3545 "entry at offset 0x%x, signature 0x%s"),
3546 offset.sect_off, dup_offset.sect_off,
3547 phex (signature, sizeof (signature)));
3548 }
3549 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3550
3551 if (dwarf2_die_debug)
3552 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3553 offset.sect_off,
3554 phex (signature, sizeof (signature)));
3555
3556 info_ptr += length;
3557 }
3558 }
3559
3560 return types_htab;
3561 }
3562
3563 /* Create the hash table of all entries in the .debug_types section,
3564 and initialize all_type_units.
3565 The result is zero if there is an error (e.g. missing .debug_types section),
3566 otherwise non-zero. */
3567
3568 static int
3569 create_all_type_units (struct objfile *objfile)
3570 {
3571 htab_t types_htab;
3572 struct dwarf2_per_cu_data **iter;
3573
3574 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3575 if (types_htab == NULL)
3576 {
3577 dwarf2_per_objfile->signatured_types = NULL;
3578 return 0;
3579 }
3580
3581 dwarf2_per_objfile->signatured_types = types_htab;
3582
3583 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3584 dwarf2_per_objfile->all_type_units
3585 = obstack_alloc (&objfile->objfile_obstack,
3586 dwarf2_per_objfile->n_type_units
3587 * sizeof (struct dwarf2_per_cu_data *));
3588 iter = &dwarf2_per_objfile->all_type_units[0];
3589 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3590 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3591 == dwarf2_per_objfile->n_type_units);
3592
3593 return 1;
3594 }
3595
3596 /* Lookup a signature based type for DW_FORM_ref_sig8.
3597 Returns NULL if signature SIG is not present in the table. */
3598
3599 static struct signatured_type *
3600 lookup_signatured_type (ULONGEST sig)
3601 {
3602 struct signatured_type find_entry, *entry;
3603
3604 if (dwarf2_per_objfile->signatured_types == NULL)
3605 {
3606 complaint (&symfile_complaints,
3607 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3608 return NULL;
3609 }
3610
3611 find_entry.signature = sig;
3612 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3613 return entry;
3614 }
3615
3616 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3617
3618 static void
3619 init_cu_die_reader (struct die_reader_specs *reader,
3620 struct dwarf2_cu *cu,
3621 struct dwarf2_section_info *section,
3622 struct dwo_file *dwo_file)
3623 {
3624 gdb_assert (section->readin && section->buffer != NULL);
3625 reader->abfd = section->asection->owner;
3626 reader->cu = cu;
3627 reader->dwo_file = dwo_file;
3628 reader->die_section = section;
3629 reader->buffer = section->buffer;
3630 }
3631
3632 /* Find the base address of the compilation unit for range lists and
3633 location lists. It will normally be specified by DW_AT_low_pc.
3634 In DWARF-3 draft 4, the base address could be overridden by
3635 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3636 compilation units with discontinuous ranges. */
3637
3638 static void
3639 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3640 {
3641 struct attribute *attr;
3642
3643 cu->base_known = 0;
3644 cu->base_address = 0;
3645
3646 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3647 if (attr)
3648 {
3649 cu->base_address = DW_ADDR (attr);
3650 cu->base_known = 1;
3651 }
3652 else
3653 {
3654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3655 if (attr)
3656 {
3657 cu->base_address = DW_ADDR (attr);
3658 cu->base_known = 1;
3659 }
3660 }
3661 }
3662
3663 /* Initialize a CU (or TU) and read its DIEs.
3664 If the CU defers to a DWO file, read the DWO file as well.
3665
3666 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3667 Otherwise, a new CU is allocated with xmalloc.
3668
3669 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3670 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3671
3672 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3673 linker) then DIE_READER_FUNC will not get called. */
3674
3675 static void
3676 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3677 int use_existing_cu, int keep,
3678 die_reader_func_ftype *die_reader_func,
3679 void *data)
3680 {
3681 struct objfile *objfile = dwarf2_per_objfile->objfile;
3682 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3683 bfd *abfd = section->asection->owner;
3684 struct dwarf2_cu *cu;
3685 gdb_byte *begin_info_ptr, *info_ptr;
3686 struct die_reader_specs reader;
3687 struct die_info *comp_unit_die;
3688 int has_children;
3689 struct attribute *attr;
3690 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3691 struct signatured_type *sig_type = NULL;
3692
3693 if (use_existing_cu)
3694 gdb_assert (keep);
3695
3696 cleanups = make_cleanup (null_cleanup, NULL);
3697
3698 /* This is cheap if the section is already read in. */
3699 dwarf2_read_section (objfile, section);
3700
3701 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3702
3703 if (use_existing_cu && this_cu->cu != NULL)
3704 {
3705 cu = this_cu->cu;
3706 info_ptr += cu->header.first_die_offset.cu_off;
3707 }
3708 else
3709 {
3710 /* If !use_existing_cu, this_cu->cu must be NULL. */
3711 gdb_assert (this_cu->cu == NULL);
3712
3713 cu = xmalloc (sizeof (*cu));
3714 init_one_comp_unit (cu, this_cu);
3715
3716 /* If an error occurs while loading, release our storage. */
3717 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3718
3719 if (this_cu->is_debug_types)
3720 {
3721 ULONGEST signature;
3722
3723 info_ptr = read_and_check_type_unit_head (&cu->header,
3724 section, info_ptr,
3725 &signature, NULL);
3726
3727 /* There's no way to get from PER_CU to its containing
3728 struct signatured_type.
3729 But we have the signature so we can use that. */
3730 sig_type = lookup_signatured_type (signature);
3731 /* We've already scanned all the signatured types,
3732 this must succeed. */
3733 gdb_assert (sig_type != NULL);
3734 gdb_assert (&sig_type->per_cu == this_cu);
3735 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3736
3737 /* LENGTH has not been set yet for type units. */
3738 this_cu->length = cu->header.length + cu->header.initial_length_size;
3739
3740 /* Establish the type offset that can be used to lookup the type. */
3741 sig_type->type_offset_in_section.sect_off =
3742 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3743 }
3744 else
3745 {
3746 info_ptr = read_and_check_comp_unit_head (&cu->header,
3747 section, info_ptr, 0);
3748
3749 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3750 gdb_assert (this_cu->length
3751 == cu->header.length + cu->header.initial_length_size);
3752 }
3753 }
3754
3755 /* Skip dummy compilation units. */
3756 if (info_ptr >= begin_info_ptr + this_cu->length
3757 || peek_abbrev_code (abfd, info_ptr) == 0)
3758 {
3759 do_cleanups (cleanups);
3760 return;
3761 }
3762
3763 /* Read the abbrevs for this compilation unit into a table. */
3764 if (cu->dwarf2_abbrevs == NULL)
3765 {
3766 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3767 make_cleanup (dwarf2_free_abbrev_table, cu);
3768 }
3769
3770 /* Read the top level CU/TU die. */
3771 init_cu_die_reader (&reader, cu, section, NULL);
3772 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3773
3774 /* If we have a DWO stub, process it and then read in the DWO file.
3775 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3776 a DWO CU, that this test will fail. */
3777 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3778 if (attr)
3779 {
3780 char *dwo_name = DW_STRING (attr);
3781 const char *comp_dir;
3782 struct dwo_unit *dwo_unit;
3783 ULONGEST signature; /* Or dwo_id. */
3784 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3785 int i,num_extra_attrs;
3786
3787 if (has_children)
3788 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3789 " has children (offset 0x%x) [in module %s]"),
3790 this_cu->offset.sect_off, bfd_get_filename (abfd));
3791
3792 /* These attributes aren't processed until later:
3793 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3794 However, the attribute is found in the stub which we won't have later.
3795 In order to not impose this complication on the rest of the code,
3796 we read them here and copy them to the DWO CU/TU die. */
3797 stmt_list = low_pc = high_pc = ranges = NULL;
3798
3799 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3800 DWO file. */
3801 if (! this_cu->is_debug_types)
3802 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3803 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3804 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3805 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3806
3807 /* There should be a DW_AT_addr_base attribute here (if needed).
3808 We need the value before we can process DW_FORM_GNU_addr_index. */
3809 cu->addr_base = 0;
3810 cu->have_addr_base = 0;
3811 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3812 if (attr)
3813 {
3814 cu->addr_base = DW_UNSND (attr);
3815 cu->have_addr_base = 1;
3816 }
3817
3818 if (this_cu->is_debug_types)
3819 {
3820 gdb_assert (sig_type != NULL);
3821 signature = sig_type->signature;
3822 }
3823 else
3824 {
3825 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3826 if (! attr)
3827 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3828 dwo_name);
3829 signature = DW_UNSND (attr);
3830 }
3831
3832 /* We may need the comp_dir in order to find the DWO file. */
3833 comp_dir = NULL;
3834 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3835 if (attr)
3836 comp_dir = DW_STRING (attr);
3837
3838 if (this_cu->is_debug_types)
3839 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3840 else
3841 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3842 signature);
3843
3844 if (dwo_unit == NULL)
3845 {
3846 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3847 " with ID %s [in module %s]"),
3848 this_cu->offset.sect_off,
3849 phex (signature, sizeof (signature)),
3850 objfile->name);
3851 }
3852
3853 /* Set up for reading the DWO CU/TU. */
3854 cu->dwo_unit = dwo_unit;
3855 section = dwo_unit->info_or_types_section;
3856 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3857 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3858
3859 if (this_cu->is_debug_types)
3860 {
3861 ULONGEST signature;
3862
3863 info_ptr = read_and_check_type_unit_head (&cu->header,
3864 section, info_ptr,
3865 &signature, NULL);
3866 gdb_assert (sig_type->signature == signature);
3867 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3868 gdb_assert (dwo_unit->length
3869 == cu->header.length + cu->header.initial_length_size);
3870
3871 /* Establish the type offset that can be used to lookup the type.
3872 For DWO files, we don't know it until now. */
3873 sig_type->type_offset_in_section.sect_off =
3874 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3875 }
3876 else
3877 {
3878 info_ptr = read_and_check_comp_unit_head (&cu->header,
3879 section, info_ptr, 0);
3880 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3881 gdb_assert (dwo_unit->length
3882 == cu->header.length + cu->header.initial_length_size);
3883 }
3884
3885 /* Discard the original CU's abbrev table, and read the DWO's. */
3886 dwarf2_free_abbrev_table (cu);
3887 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3888
3889 /* Read in the die, but leave space to copy over the attributes
3890 from the stub. This has the benefit of simplifying the rest of
3891 the code - all the real work is done here. */
3892 num_extra_attrs = ((stmt_list != NULL)
3893 + (low_pc != NULL)
3894 + (high_pc != NULL)
3895 + (ranges != NULL));
3896 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3897 &has_children, num_extra_attrs);
3898
3899 /* Copy over the attributes from the stub to the DWO die. */
3900 i = comp_unit_die->num_attrs;
3901 if (stmt_list != NULL)
3902 comp_unit_die->attrs[i++] = *stmt_list;
3903 if (low_pc != NULL)
3904 comp_unit_die->attrs[i++] = *low_pc;
3905 if (high_pc != NULL)
3906 comp_unit_die->attrs[i++] = *high_pc;
3907 if (ranges != NULL)
3908 comp_unit_die->attrs[i++] = *ranges;
3909 comp_unit_die->num_attrs += num_extra_attrs;
3910
3911 /* Skip dummy compilation units. */
3912 if (info_ptr >= begin_info_ptr + dwo_unit->length
3913 || peek_abbrev_code (abfd, info_ptr) == 0)
3914 {
3915 do_cleanups (cleanups);
3916 return;
3917 }
3918 }
3919
3920 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3921
3922 if (free_cu_cleanup != NULL)
3923 {
3924 if (keep)
3925 {
3926 /* We've successfully allocated this compilation unit. Let our
3927 caller clean it up when finished with it. */
3928 discard_cleanups (free_cu_cleanup);
3929
3930 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3931 So we have to manually free the abbrev table. */
3932 dwarf2_free_abbrev_table (cu);
3933
3934 /* Link this CU into read_in_chain. */
3935 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3936 dwarf2_per_objfile->read_in_chain = this_cu;
3937 }
3938 else
3939 do_cleanups (free_cu_cleanup);
3940 }
3941
3942 do_cleanups (cleanups);
3943 }
3944
3945 /* Read CU/TU THIS_CU in section SECTION,
3946 but do not follow DW_AT_GNU_dwo_name if present.
3947 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3948 have already done the lookup to find the DWO file).
3949
3950 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3951 THIS_CU->is_debug_types, but nothing else.
3952
3953 We fill in THIS_CU->length.
3954
3955 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3956 linker) then DIE_READER_FUNC will not get called.
3957
3958 THIS_CU->cu is always freed when done.
3959 This is done in order to not leave THIS_CU->cu in a state where we have
3960 to care whether it refers to the "main" CU or the DWO CU. */
3961
3962 static void
3963 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3964 struct dwarf2_section_info *abbrev_section,
3965 struct dwo_file *dwo_file,
3966 die_reader_func_ftype *die_reader_func,
3967 void *data)
3968 {
3969 struct objfile *objfile = dwarf2_per_objfile->objfile;
3970 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3971 bfd *abfd = section->asection->owner;
3972 struct dwarf2_cu cu;
3973 gdb_byte *begin_info_ptr, *info_ptr;
3974 struct die_reader_specs reader;
3975 struct cleanup *cleanups;
3976 struct die_info *comp_unit_die;
3977 int has_children;
3978
3979 gdb_assert (this_cu->cu == NULL);
3980
3981 /* This is cheap if the section is already read in. */
3982 dwarf2_read_section (objfile, section);
3983
3984 init_one_comp_unit (&cu, this_cu);
3985
3986 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3987
3988 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3989 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3990 this_cu->is_debug_types);
3991
3992 this_cu->length = cu.header.length + cu.header.initial_length_size;
3993
3994 /* Skip dummy compilation units. */
3995 if (info_ptr >= begin_info_ptr + this_cu->length
3996 || peek_abbrev_code (abfd, info_ptr) == 0)
3997 {
3998 do_cleanups (cleanups);
3999 return;
4000 }
4001
4002 dwarf2_read_abbrevs (&cu, abbrev_section);
4003 make_cleanup (dwarf2_free_abbrev_table, &cu);
4004
4005 init_cu_die_reader (&reader, &cu, section, dwo_file);
4006 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4007
4008 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4009
4010 do_cleanups (cleanups);
4011 }
4012
4013 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4014 does not lookup the specified DWO file.
4015 This cannot be used to read DWO files.
4016
4017 THIS_CU->cu is always freed when done.
4018 This is done in order to not leave THIS_CU->cu in a state where we have
4019 to care whether it refers to the "main" CU or the DWO CU.
4020 We can revisit this if the data shows there's a performance issue. */
4021
4022 static void
4023 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4024 die_reader_func_ftype *die_reader_func,
4025 void *data)
4026 {
4027 init_cutu_and_read_dies_no_follow (this_cu,
4028 &dwarf2_per_objfile->abbrev,
4029 NULL,
4030 die_reader_func, data);
4031 }
4032
4033 /* die_reader_func for process_psymtab_comp_unit. */
4034
4035 static void
4036 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4037 gdb_byte *info_ptr,
4038 struct die_info *comp_unit_die,
4039 int has_children,
4040 void *data)
4041 {
4042 struct dwarf2_cu *cu = reader->cu;
4043 struct objfile *objfile = cu->objfile;
4044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4045 bfd *abfd = objfile->obfd;
4046 struct attribute *attr;
4047 CORE_ADDR baseaddr;
4048 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4049 struct partial_symtab *pst;
4050 int has_pc_info;
4051 const char *filename;
4052 int *want_partial_unit_ptr = data;
4053
4054 if (comp_unit_die->tag == DW_TAG_partial_unit
4055 && (want_partial_unit_ptr == NULL
4056 || !*want_partial_unit_ptr))
4057 return;
4058
4059 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4060
4061 cu->list_in_scope = &file_symbols;
4062
4063 /* Allocate a new partial symbol table structure. */
4064 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4065 if (attr == NULL || !DW_STRING (attr))
4066 filename = "";
4067 else
4068 filename = DW_STRING (attr);
4069 pst = start_psymtab_common (objfile, objfile->section_offsets,
4070 filename,
4071 /* TEXTLOW and TEXTHIGH are set below. */
4072 0,
4073 objfile->global_psymbols.next,
4074 objfile->static_psymbols.next);
4075 pst->psymtabs_addrmap_supported = 1;
4076
4077 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4078 if (attr != NULL)
4079 pst->dirname = DW_STRING (attr);
4080
4081 pst->read_symtab_private = per_cu;
4082
4083 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4084
4085 /* Store the function that reads in the rest of the symbol table. */
4086 pst->read_symtab = dwarf2_psymtab_to_symtab;
4087
4088 per_cu->v.psymtab = pst;
4089
4090 dwarf2_find_base_address (comp_unit_die, cu);
4091
4092 /* Possibly set the default values of LOWPC and HIGHPC from
4093 `DW_AT_ranges'. */
4094 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4095 &best_highpc, cu, pst);
4096 if (has_pc_info == 1 && best_lowpc < best_highpc)
4097 /* Store the contiguous range if it is not empty; it can be empty for
4098 CUs with no code. */
4099 addrmap_set_empty (objfile->psymtabs_addrmap,
4100 best_lowpc + baseaddr,
4101 best_highpc + baseaddr - 1, pst);
4102
4103 /* Check if comp unit has_children.
4104 If so, read the rest of the partial symbols from this comp unit.
4105 If not, there's no more debug_info for this comp unit. */
4106 if (has_children)
4107 {
4108 struct partial_die_info *first_die;
4109 CORE_ADDR lowpc, highpc;
4110
4111 lowpc = ((CORE_ADDR) -1);
4112 highpc = ((CORE_ADDR) 0);
4113
4114 first_die = load_partial_dies (reader, info_ptr, 1);
4115
4116 scan_partial_symbols (first_die, &lowpc, &highpc,
4117 ! has_pc_info, cu);
4118
4119 /* If we didn't find a lowpc, set it to highpc to avoid
4120 complaints from `maint check'. */
4121 if (lowpc == ((CORE_ADDR) -1))
4122 lowpc = highpc;
4123
4124 /* If the compilation unit didn't have an explicit address range,
4125 then use the information extracted from its child dies. */
4126 if (! has_pc_info)
4127 {
4128 best_lowpc = lowpc;
4129 best_highpc = highpc;
4130 }
4131 }
4132 pst->textlow = best_lowpc + baseaddr;
4133 pst->texthigh = best_highpc + baseaddr;
4134
4135 pst->n_global_syms = objfile->global_psymbols.next -
4136 (objfile->global_psymbols.list + pst->globals_offset);
4137 pst->n_static_syms = objfile->static_psymbols.next -
4138 (objfile->static_psymbols.list + pst->statics_offset);
4139 sort_pst_symbols (pst);
4140
4141 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4142 {
4143 int i;
4144 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4145 struct dwarf2_per_cu_data *iter;
4146
4147 /* Fill in 'dependencies' here; we fill in 'users' in a
4148 post-pass. */
4149 pst->number_of_dependencies = len;
4150 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4151 len * sizeof (struct symtab *));
4152 for (i = 0;
4153 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4154 i, iter);
4155 ++i)
4156 pst->dependencies[i] = iter->v.psymtab;
4157
4158 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4159 }
4160
4161 if (per_cu->is_debug_types)
4162 {
4163 /* It's not clear we want to do anything with stmt lists here.
4164 Waiting to see what gcc ultimately does. */
4165 }
4166 else
4167 {
4168 /* Get the list of files included in the current compilation unit,
4169 and build a psymtab for each of them. */
4170 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4171 }
4172 }
4173
4174 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4175 Process compilation unit THIS_CU for a psymtab. */
4176
4177 static void
4178 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4179 int want_partial_unit)
4180 {
4181 /* If this compilation unit was already read in, free the
4182 cached copy in order to read it in again. This is
4183 necessary because we skipped some symbols when we first
4184 read in the compilation unit (see load_partial_dies).
4185 This problem could be avoided, but the benefit is unclear. */
4186 if (this_cu->cu != NULL)
4187 free_one_cached_comp_unit (this_cu);
4188
4189 gdb_assert (! this_cu->is_debug_types);
4190 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4191 &want_partial_unit);
4192
4193 /* Age out any secondary CUs. */
4194 age_cached_comp_units ();
4195 }
4196
4197 /* Traversal function for htab_traverse_noresize.
4198 Process one .debug_types comp-unit. */
4199
4200 static int
4201 process_psymtab_type_unit (void **slot, void *info)
4202 {
4203 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4204 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4205
4206 gdb_assert (per_cu->is_debug_types);
4207 gdb_assert (info == NULL);
4208
4209 /* If this compilation unit was already read in, free the
4210 cached copy in order to read it in again. This is
4211 necessary because we skipped some symbols when we first
4212 read in the compilation unit (see load_partial_dies).
4213 This problem could be avoided, but the benefit is unclear. */
4214 if (per_cu->cu != NULL)
4215 free_one_cached_comp_unit (per_cu);
4216
4217 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4218 NULL);
4219
4220 /* Age out any secondary CUs. */
4221 age_cached_comp_units ();
4222
4223 return 1;
4224 }
4225
4226 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4227 Build partial symbol tables for the .debug_types comp-units. */
4228
4229 static void
4230 build_type_psymtabs (struct objfile *objfile)
4231 {
4232 if (! create_all_type_units (objfile))
4233 return;
4234
4235 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4236 process_psymtab_type_unit, NULL);
4237 }
4238
4239 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4240
4241 static void
4242 psymtabs_addrmap_cleanup (void *o)
4243 {
4244 struct objfile *objfile = o;
4245
4246 objfile->psymtabs_addrmap = NULL;
4247 }
4248
4249 /* Compute the 'user' field for each psymtab in OBJFILE. */
4250
4251 static void
4252 set_partial_user (struct objfile *objfile)
4253 {
4254 int i;
4255
4256 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4257 {
4258 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4259 struct partial_symtab *pst = per_cu->v.psymtab;
4260 int j;
4261
4262 for (j = 0; j < pst->number_of_dependencies; ++j)
4263 {
4264 /* Set the 'user' field only if it is not already set. */
4265 if (pst->dependencies[j]->user == NULL)
4266 pst->dependencies[j]->user = pst;
4267 }
4268 }
4269 }
4270
4271 /* Build the partial symbol table by doing a quick pass through the
4272 .debug_info and .debug_abbrev sections. */
4273
4274 static void
4275 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4276 {
4277 struct cleanup *back_to, *addrmap_cleanup;
4278 struct obstack temp_obstack;
4279 int i;
4280
4281 dwarf2_per_objfile->reading_partial_symbols = 1;
4282
4283 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4284
4285 /* Any cached compilation units will be linked by the per-objfile
4286 read_in_chain. Make sure to free them when we're done. */
4287 back_to = make_cleanup (free_cached_comp_units, NULL);
4288
4289 build_type_psymtabs (objfile);
4290
4291 create_all_comp_units (objfile);
4292
4293 /* Create a temporary address map on a temporary obstack. We later
4294 copy this to the final obstack. */
4295 obstack_init (&temp_obstack);
4296 make_cleanup_obstack_free (&temp_obstack);
4297 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4298 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4299
4300 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4301 {
4302 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4303
4304 process_psymtab_comp_unit (per_cu, 0);
4305 }
4306
4307 set_partial_user (objfile);
4308
4309 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4310 &objfile->objfile_obstack);
4311 discard_cleanups (addrmap_cleanup);
4312
4313 do_cleanups (back_to);
4314 }
4315
4316 /* die_reader_func for load_partial_comp_unit. */
4317
4318 static void
4319 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4320 gdb_byte *info_ptr,
4321 struct die_info *comp_unit_die,
4322 int has_children,
4323 void *data)
4324 {
4325 struct dwarf2_cu *cu = reader->cu;
4326
4327 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4328
4329 /* Check if comp unit has_children.
4330 If so, read the rest of the partial symbols from this comp unit.
4331 If not, there's no more debug_info for this comp unit. */
4332 if (has_children)
4333 load_partial_dies (reader, info_ptr, 0);
4334 }
4335
4336 /* Load the partial DIEs for a secondary CU into memory.
4337 This is also used when rereading a primary CU with load_all_dies. */
4338
4339 static void
4340 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4341 {
4342 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4343 }
4344
4345 /* Create a list of all compilation units in OBJFILE.
4346 This is only done for -readnow and building partial symtabs. */
4347
4348 static void
4349 create_all_comp_units (struct objfile *objfile)
4350 {
4351 int n_allocated;
4352 int n_comp_units;
4353 struct dwarf2_per_cu_data **all_comp_units;
4354 gdb_byte *info_ptr;
4355
4356 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4357 info_ptr = dwarf2_per_objfile->info.buffer;
4358
4359 n_comp_units = 0;
4360 n_allocated = 10;
4361 all_comp_units = xmalloc (n_allocated
4362 * sizeof (struct dwarf2_per_cu_data *));
4363
4364 while (info_ptr < dwarf2_per_objfile->info.buffer
4365 + dwarf2_per_objfile->info.size)
4366 {
4367 unsigned int length, initial_length_size;
4368 struct dwarf2_per_cu_data *this_cu;
4369 sect_offset offset;
4370
4371 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4372
4373 /* Read just enough information to find out where the next
4374 compilation unit is. */
4375 length = read_initial_length (objfile->obfd, info_ptr,
4376 &initial_length_size);
4377
4378 /* Save the compilation unit for later lookup. */
4379 this_cu = obstack_alloc (&objfile->objfile_obstack,
4380 sizeof (struct dwarf2_per_cu_data));
4381 memset (this_cu, 0, sizeof (*this_cu));
4382 this_cu->offset = offset;
4383 this_cu->length = length + initial_length_size;
4384 this_cu->objfile = objfile;
4385 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4386
4387 if (n_comp_units == n_allocated)
4388 {
4389 n_allocated *= 2;
4390 all_comp_units = xrealloc (all_comp_units,
4391 n_allocated
4392 * sizeof (struct dwarf2_per_cu_data *));
4393 }
4394 all_comp_units[n_comp_units++] = this_cu;
4395
4396 info_ptr = info_ptr + this_cu->length;
4397 }
4398
4399 dwarf2_per_objfile->all_comp_units
4400 = obstack_alloc (&objfile->objfile_obstack,
4401 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4402 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4403 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4404 xfree (all_comp_units);
4405 dwarf2_per_objfile->n_comp_units = n_comp_units;
4406 }
4407
4408 /* Process all loaded DIEs for compilation unit CU, starting at
4409 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4410 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4411 DW_AT_ranges). If NEED_PC is set, then this function will set
4412 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4413 and record the covered ranges in the addrmap. */
4414
4415 static void
4416 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4417 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4418 {
4419 struct partial_die_info *pdi;
4420
4421 /* Now, march along the PDI's, descending into ones which have
4422 interesting children but skipping the children of the other ones,
4423 until we reach the end of the compilation unit. */
4424
4425 pdi = first_die;
4426
4427 while (pdi != NULL)
4428 {
4429 fixup_partial_die (pdi, cu);
4430
4431 /* Anonymous namespaces or modules have no name but have interesting
4432 children, so we need to look at them. Ditto for anonymous
4433 enums. */
4434
4435 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4436 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4437 || pdi->tag == DW_TAG_imported_unit)
4438 {
4439 switch (pdi->tag)
4440 {
4441 case DW_TAG_subprogram:
4442 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4443 break;
4444 case DW_TAG_constant:
4445 case DW_TAG_variable:
4446 case DW_TAG_typedef:
4447 case DW_TAG_union_type:
4448 if (!pdi->is_declaration)
4449 {
4450 add_partial_symbol (pdi, cu);
4451 }
4452 break;
4453 case DW_TAG_class_type:
4454 case DW_TAG_interface_type:
4455 case DW_TAG_structure_type:
4456 if (!pdi->is_declaration)
4457 {
4458 add_partial_symbol (pdi, cu);
4459 }
4460 break;
4461 case DW_TAG_enumeration_type:
4462 if (!pdi->is_declaration)
4463 add_partial_enumeration (pdi, cu);
4464 break;
4465 case DW_TAG_base_type:
4466 case DW_TAG_subrange_type:
4467 /* File scope base type definitions are added to the partial
4468 symbol table. */
4469 add_partial_symbol (pdi, cu);
4470 break;
4471 case DW_TAG_namespace:
4472 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4473 break;
4474 case DW_TAG_module:
4475 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4476 break;
4477 case DW_TAG_imported_unit:
4478 {
4479 struct dwarf2_per_cu_data *per_cu;
4480
4481 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4482 cu->objfile);
4483
4484 /* Go read the partial unit, if needed. */
4485 if (per_cu->v.psymtab == NULL)
4486 process_psymtab_comp_unit (per_cu, 1);
4487
4488 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4489 per_cu);
4490 }
4491 break;
4492 default:
4493 break;
4494 }
4495 }
4496
4497 /* If the die has a sibling, skip to the sibling. */
4498
4499 pdi = pdi->die_sibling;
4500 }
4501 }
4502
4503 /* Functions used to compute the fully scoped name of a partial DIE.
4504
4505 Normally, this is simple. For C++, the parent DIE's fully scoped
4506 name is concatenated with "::" and the partial DIE's name. For
4507 Java, the same thing occurs except that "." is used instead of "::".
4508 Enumerators are an exception; they use the scope of their parent
4509 enumeration type, i.e. the name of the enumeration type is not
4510 prepended to the enumerator.
4511
4512 There are two complexities. One is DW_AT_specification; in this
4513 case "parent" means the parent of the target of the specification,
4514 instead of the direct parent of the DIE. The other is compilers
4515 which do not emit DW_TAG_namespace; in this case we try to guess
4516 the fully qualified name of structure types from their members'
4517 linkage names. This must be done using the DIE's children rather
4518 than the children of any DW_AT_specification target. We only need
4519 to do this for structures at the top level, i.e. if the target of
4520 any DW_AT_specification (if any; otherwise the DIE itself) does not
4521 have a parent. */
4522
4523 /* Compute the scope prefix associated with PDI's parent, in
4524 compilation unit CU. The result will be allocated on CU's
4525 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4526 field. NULL is returned if no prefix is necessary. */
4527 static char *
4528 partial_die_parent_scope (struct partial_die_info *pdi,
4529 struct dwarf2_cu *cu)
4530 {
4531 char *grandparent_scope;
4532 struct partial_die_info *parent, *real_pdi;
4533
4534 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4535 then this means the parent of the specification DIE. */
4536
4537 real_pdi = pdi;
4538 while (real_pdi->has_specification)
4539 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4540
4541 parent = real_pdi->die_parent;
4542 if (parent == NULL)
4543 return NULL;
4544
4545 if (parent->scope_set)
4546 return parent->scope;
4547
4548 fixup_partial_die (parent, cu);
4549
4550 grandparent_scope = partial_die_parent_scope (parent, cu);
4551
4552 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4553 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4554 Work around this problem here. */
4555 if (cu->language == language_cplus
4556 && parent->tag == DW_TAG_namespace
4557 && strcmp (parent->name, "::") == 0
4558 && grandparent_scope == NULL)
4559 {
4560 parent->scope = NULL;
4561 parent->scope_set = 1;
4562 return NULL;
4563 }
4564
4565 if (pdi->tag == DW_TAG_enumerator)
4566 /* Enumerators should not get the name of the enumeration as a prefix. */
4567 parent->scope = grandparent_scope;
4568 else if (parent->tag == DW_TAG_namespace
4569 || parent->tag == DW_TAG_module
4570 || parent->tag == DW_TAG_structure_type
4571 || parent->tag == DW_TAG_class_type
4572 || parent->tag == DW_TAG_interface_type
4573 || parent->tag == DW_TAG_union_type
4574 || parent->tag == DW_TAG_enumeration_type)
4575 {
4576 if (grandparent_scope == NULL)
4577 parent->scope = parent->name;
4578 else
4579 parent->scope = typename_concat (&cu->comp_unit_obstack,
4580 grandparent_scope,
4581 parent->name, 0, cu);
4582 }
4583 else
4584 {
4585 /* FIXME drow/2004-04-01: What should we be doing with
4586 function-local names? For partial symbols, we should probably be
4587 ignoring them. */
4588 complaint (&symfile_complaints,
4589 _("unhandled containing DIE tag %d for DIE at %d"),
4590 parent->tag, pdi->offset.sect_off);
4591 parent->scope = grandparent_scope;
4592 }
4593
4594 parent->scope_set = 1;
4595 return parent->scope;
4596 }
4597
4598 /* Return the fully scoped name associated with PDI, from compilation unit
4599 CU. The result will be allocated with malloc. */
4600
4601 static char *
4602 partial_die_full_name (struct partial_die_info *pdi,
4603 struct dwarf2_cu *cu)
4604 {
4605 char *parent_scope;
4606
4607 /* If this is a template instantiation, we can not work out the
4608 template arguments from partial DIEs. So, unfortunately, we have
4609 to go through the full DIEs. At least any work we do building
4610 types here will be reused if full symbols are loaded later. */
4611 if (pdi->has_template_arguments)
4612 {
4613 fixup_partial_die (pdi, cu);
4614
4615 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4616 {
4617 struct die_info *die;
4618 struct attribute attr;
4619 struct dwarf2_cu *ref_cu = cu;
4620
4621 /* DW_FORM_ref_addr is using section offset. */
4622 attr.name = 0;
4623 attr.form = DW_FORM_ref_addr;
4624 attr.u.unsnd = pdi->offset.sect_off;
4625 die = follow_die_ref (NULL, &attr, &ref_cu);
4626
4627 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4628 }
4629 }
4630
4631 parent_scope = partial_die_parent_scope (pdi, cu);
4632 if (parent_scope == NULL)
4633 return NULL;
4634 else
4635 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4636 }
4637
4638 static void
4639 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4640 {
4641 struct objfile *objfile = cu->objfile;
4642 CORE_ADDR addr = 0;
4643 char *actual_name = NULL;
4644 CORE_ADDR baseaddr;
4645 int built_actual_name = 0;
4646
4647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4648
4649 actual_name = partial_die_full_name (pdi, cu);
4650 if (actual_name)
4651 built_actual_name = 1;
4652
4653 if (actual_name == NULL)
4654 actual_name = pdi->name;
4655
4656 switch (pdi->tag)
4657 {
4658 case DW_TAG_subprogram:
4659 if (pdi->is_external || cu->language == language_ada)
4660 {
4661 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4662 of the global scope. But in Ada, we want to be able to access
4663 nested procedures globally. So all Ada subprograms are stored
4664 in the global scope. */
4665 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4666 mst_text, objfile); */
4667 add_psymbol_to_list (actual_name, strlen (actual_name),
4668 built_actual_name,
4669 VAR_DOMAIN, LOC_BLOCK,
4670 &objfile->global_psymbols,
4671 0, pdi->lowpc + baseaddr,
4672 cu->language, objfile);
4673 }
4674 else
4675 {
4676 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4677 mst_file_text, objfile); */
4678 add_psymbol_to_list (actual_name, strlen (actual_name),
4679 built_actual_name,
4680 VAR_DOMAIN, LOC_BLOCK,
4681 &objfile->static_psymbols,
4682 0, pdi->lowpc + baseaddr,
4683 cu->language, objfile);
4684 }
4685 break;
4686 case DW_TAG_constant:
4687 {
4688 struct psymbol_allocation_list *list;
4689
4690 if (pdi->is_external)
4691 list = &objfile->global_psymbols;
4692 else
4693 list = &objfile->static_psymbols;
4694 add_psymbol_to_list (actual_name, strlen (actual_name),
4695 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4696 list, 0, 0, cu->language, objfile);
4697 }
4698 break;
4699 case DW_TAG_variable:
4700 if (pdi->d.locdesc)
4701 addr = decode_locdesc (pdi->d.locdesc, cu);
4702
4703 if (pdi->d.locdesc
4704 && addr == 0
4705 && !dwarf2_per_objfile->has_section_at_zero)
4706 {
4707 /* A global or static variable may also have been stripped
4708 out by the linker if unused, in which case its address
4709 will be nullified; do not add such variables into partial
4710 symbol table then. */
4711 }
4712 else if (pdi->is_external)
4713 {
4714 /* Global Variable.
4715 Don't enter into the minimal symbol tables as there is
4716 a minimal symbol table entry from the ELF symbols already.
4717 Enter into partial symbol table if it has a location
4718 descriptor or a type.
4719 If the location descriptor is missing, new_symbol will create
4720 a LOC_UNRESOLVED symbol, the address of the variable will then
4721 be determined from the minimal symbol table whenever the variable
4722 is referenced.
4723 The address for the partial symbol table entry is not
4724 used by GDB, but it comes in handy for debugging partial symbol
4725 table building. */
4726
4727 if (pdi->d.locdesc || pdi->has_type)
4728 add_psymbol_to_list (actual_name, strlen (actual_name),
4729 built_actual_name,
4730 VAR_DOMAIN, LOC_STATIC,
4731 &objfile->global_psymbols,
4732 0, addr + baseaddr,
4733 cu->language, objfile);
4734 }
4735 else
4736 {
4737 /* Static Variable. Skip symbols without location descriptors. */
4738 if (pdi->d.locdesc == NULL)
4739 {
4740 if (built_actual_name)
4741 xfree (actual_name);
4742 return;
4743 }
4744 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4745 mst_file_data, objfile); */
4746 add_psymbol_to_list (actual_name, strlen (actual_name),
4747 built_actual_name,
4748 VAR_DOMAIN, LOC_STATIC,
4749 &objfile->static_psymbols,
4750 0, addr + baseaddr,
4751 cu->language, objfile);
4752 }
4753 break;
4754 case DW_TAG_typedef:
4755 case DW_TAG_base_type:
4756 case DW_TAG_subrange_type:
4757 add_psymbol_to_list (actual_name, strlen (actual_name),
4758 built_actual_name,
4759 VAR_DOMAIN, LOC_TYPEDEF,
4760 &objfile->static_psymbols,
4761 0, (CORE_ADDR) 0, cu->language, objfile);
4762 break;
4763 case DW_TAG_namespace:
4764 add_psymbol_to_list (actual_name, strlen (actual_name),
4765 built_actual_name,
4766 VAR_DOMAIN, LOC_TYPEDEF,
4767 &objfile->global_psymbols,
4768 0, (CORE_ADDR) 0, cu->language, objfile);
4769 break;
4770 case DW_TAG_class_type:
4771 case DW_TAG_interface_type:
4772 case DW_TAG_structure_type:
4773 case DW_TAG_union_type:
4774 case DW_TAG_enumeration_type:
4775 /* Skip external references. The DWARF standard says in the section
4776 about "Structure, Union, and Class Type Entries": "An incomplete
4777 structure, union or class type is represented by a structure,
4778 union or class entry that does not have a byte size attribute
4779 and that has a DW_AT_declaration attribute." */
4780 if (!pdi->has_byte_size && pdi->is_declaration)
4781 {
4782 if (built_actual_name)
4783 xfree (actual_name);
4784 return;
4785 }
4786
4787 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4788 static vs. global. */
4789 add_psymbol_to_list (actual_name, strlen (actual_name),
4790 built_actual_name,
4791 STRUCT_DOMAIN, LOC_TYPEDEF,
4792 (cu->language == language_cplus
4793 || cu->language == language_java)
4794 ? &objfile->global_psymbols
4795 : &objfile->static_psymbols,
4796 0, (CORE_ADDR) 0, cu->language, objfile);
4797
4798 break;
4799 case DW_TAG_enumerator:
4800 add_psymbol_to_list (actual_name, strlen (actual_name),
4801 built_actual_name,
4802 VAR_DOMAIN, LOC_CONST,
4803 (cu->language == language_cplus
4804 || cu->language == language_java)
4805 ? &objfile->global_psymbols
4806 : &objfile->static_psymbols,
4807 0, (CORE_ADDR) 0, cu->language, objfile);
4808 break;
4809 default:
4810 break;
4811 }
4812
4813 if (built_actual_name)
4814 xfree (actual_name);
4815 }
4816
4817 /* Read a partial die corresponding to a namespace; also, add a symbol
4818 corresponding to that namespace to the symbol table. NAMESPACE is
4819 the name of the enclosing namespace. */
4820
4821 static void
4822 add_partial_namespace (struct partial_die_info *pdi,
4823 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4824 int need_pc, struct dwarf2_cu *cu)
4825 {
4826 /* Add a symbol for the namespace. */
4827
4828 add_partial_symbol (pdi, cu);
4829
4830 /* Now scan partial symbols in that namespace. */
4831
4832 if (pdi->has_children)
4833 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4834 }
4835
4836 /* Read a partial die corresponding to a Fortran module. */
4837
4838 static void
4839 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4840 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4841 {
4842 /* Now scan partial symbols in that module. */
4843
4844 if (pdi->has_children)
4845 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4846 }
4847
4848 /* Read a partial die corresponding to a subprogram and create a partial
4849 symbol for that subprogram. When the CU language allows it, this
4850 routine also defines a partial symbol for each nested subprogram
4851 that this subprogram contains.
4852
4853 DIE my also be a lexical block, in which case we simply search
4854 recursively for suprograms defined inside that lexical block.
4855 Again, this is only performed when the CU language allows this
4856 type of definitions. */
4857
4858 static void
4859 add_partial_subprogram (struct partial_die_info *pdi,
4860 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4861 int need_pc, struct dwarf2_cu *cu)
4862 {
4863 if (pdi->tag == DW_TAG_subprogram)
4864 {
4865 if (pdi->has_pc_info)
4866 {
4867 if (pdi->lowpc < *lowpc)
4868 *lowpc = pdi->lowpc;
4869 if (pdi->highpc > *highpc)
4870 *highpc = pdi->highpc;
4871 if (need_pc)
4872 {
4873 CORE_ADDR baseaddr;
4874 struct objfile *objfile = cu->objfile;
4875
4876 baseaddr = ANOFFSET (objfile->section_offsets,
4877 SECT_OFF_TEXT (objfile));
4878 addrmap_set_empty (objfile->psymtabs_addrmap,
4879 pdi->lowpc + baseaddr,
4880 pdi->highpc - 1 + baseaddr,
4881 cu->per_cu->v.psymtab);
4882 }
4883 }
4884
4885 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4886 {
4887 if (!pdi->is_declaration)
4888 /* Ignore subprogram DIEs that do not have a name, they are
4889 illegal. Do not emit a complaint at this point, we will
4890 do so when we convert this psymtab into a symtab. */
4891 if (pdi->name)
4892 add_partial_symbol (pdi, cu);
4893 }
4894 }
4895
4896 if (! pdi->has_children)
4897 return;
4898
4899 if (cu->language == language_ada)
4900 {
4901 pdi = pdi->die_child;
4902 while (pdi != NULL)
4903 {
4904 fixup_partial_die (pdi, cu);
4905 if (pdi->tag == DW_TAG_subprogram
4906 || pdi->tag == DW_TAG_lexical_block)
4907 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4908 pdi = pdi->die_sibling;
4909 }
4910 }
4911 }
4912
4913 /* Read a partial die corresponding to an enumeration type. */
4914
4915 static void
4916 add_partial_enumeration (struct partial_die_info *enum_pdi,
4917 struct dwarf2_cu *cu)
4918 {
4919 struct partial_die_info *pdi;
4920
4921 if (enum_pdi->name != NULL)
4922 add_partial_symbol (enum_pdi, cu);
4923
4924 pdi = enum_pdi->die_child;
4925 while (pdi)
4926 {
4927 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4928 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4929 else
4930 add_partial_symbol (pdi, cu);
4931 pdi = pdi->die_sibling;
4932 }
4933 }
4934
4935 /* Return the initial uleb128 in the die at INFO_PTR. */
4936
4937 static unsigned int
4938 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4939 {
4940 unsigned int bytes_read;
4941
4942 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4943 }
4944
4945 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4946 Return the corresponding abbrev, or NULL if the number is zero (indicating
4947 an empty DIE). In either case *BYTES_READ will be set to the length of
4948 the initial number. */
4949
4950 static struct abbrev_info *
4951 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4952 struct dwarf2_cu *cu)
4953 {
4954 bfd *abfd = cu->objfile->obfd;
4955 unsigned int abbrev_number;
4956 struct abbrev_info *abbrev;
4957
4958 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4959
4960 if (abbrev_number == 0)
4961 return NULL;
4962
4963 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4964 if (!abbrev)
4965 {
4966 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4967 abbrev_number, bfd_get_filename (abfd));
4968 }
4969
4970 return abbrev;
4971 }
4972
4973 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4974 Returns a pointer to the end of a series of DIEs, terminated by an empty
4975 DIE. Any children of the skipped DIEs will also be skipped. */
4976
4977 static gdb_byte *
4978 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4979 {
4980 struct dwarf2_cu *cu = reader->cu;
4981 struct abbrev_info *abbrev;
4982 unsigned int bytes_read;
4983
4984 while (1)
4985 {
4986 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4987 if (abbrev == NULL)
4988 return info_ptr + bytes_read;
4989 else
4990 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4991 }
4992 }
4993
4994 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4995 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4996 abbrev corresponding to that skipped uleb128 should be passed in
4997 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4998 children. */
4999
5000 static gdb_byte *
5001 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5002 struct abbrev_info *abbrev)
5003 {
5004 unsigned int bytes_read;
5005 struct attribute attr;
5006 bfd *abfd = reader->abfd;
5007 struct dwarf2_cu *cu = reader->cu;
5008 gdb_byte *buffer = reader->buffer;
5009 unsigned int form, i;
5010
5011 for (i = 0; i < abbrev->num_attrs; i++)
5012 {
5013 /* The only abbrev we care about is DW_AT_sibling. */
5014 if (abbrev->attrs[i].name == DW_AT_sibling)
5015 {
5016 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5017 if (attr.form == DW_FORM_ref_addr)
5018 complaint (&symfile_complaints,
5019 _("ignoring absolute DW_AT_sibling"));
5020 else
5021 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5022 }
5023
5024 /* If it isn't DW_AT_sibling, skip this attribute. */
5025 form = abbrev->attrs[i].form;
5026 skip_attribute:
5027 switch (form)
5028 {
5029 case DW_FORM_ref_addr:
5030 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5031 and later it is offset sized. */
5032 if (cu->header.version == 2)
5033 info_ptr += cu->header.addr_size;
5034 else
5035 info_ptr += cu->header.offset_size;
5036 break;
5037 case DW_FORM_addr:
5038 info_ptr += cu->header.addr_size;
5039 break;
5040 case DW_FORM_data1:
5041 case DW_FORM_ref1:
5042 case DW_FORM_flag:
5043 info_ptr += 1;
5044 break;
5045 case DW_FORM_flag_present:
5046 break;
5047 case DW_FORM_data2:
5048 case DW_FORM_ref2:
5049 info_ptr += 2;
5050 break;
5051 case DW_FORM_data4:
5052 case DW_FORM_ref4:
5053 info_ptr += 4;
5054 break;
5055 case DW_FORM_data8:
5056 case DW_FORM_ref8:
5057 case DW_FORM_ref_sig8:
5058 info_ptr += 8;
5059 break;
5060 case DW_FORM_string:
5061 read_direct_string (abfd, info_ptr, &bytes_read);
5062 info_ptr += bytes_read;
5063 break;
5064 case DW_FORM_sec_offset:
5065 case DW_FORM_strp:
5066 info_ptr += cu->header.offset_size;
5067 break;
5068 case DW_FORM_exprloc:
5069 case DW_FORM_block:
5070 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5071 info_ptr += bytes_read;
5072 break;
5073 case DW_FORM_block1:
5074 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5075 break;
5076 case DW_FORM_block2:
5077 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5078 break;
5079 case DW_FORM_block4:
5080 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5081 break;
5082 case DW_FORM_sdata:
5083 case DW_FORM_udata:
5084 case DW_FORM_ref_udata:
5085 case DW_FORM_GNU_addr_index:
5086 case DW_FORM_GNU_str_index:
5087 info_ptr = skip_leb128 (abfd, info_ptr);
5088 break;
5089 case DW_FORM_indirect:
5090 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5091 info_ptr += bytes_read;
5092 /* We need to continue parsing from here, so just go back to
5093 the top. */
5094 goto skip_attribute;
5095
5096 default:
5097 error (_("Dwarf Error: Cannot handle %s "
5098 "in DWARF reader [in module %s]"),
5099 dwarf_form_name (form),
5100 bfd_get_filename (abfd));
5101 }
5102 }
5103
5104 if (abbrev->has_children)
5105 return skip_children (reader, info_ptr);
5106 else
5107 return info_ptr;
5108 }
5109
5110 /* Locate ORIG_PDI's sibling.
5111 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5112
5113 static gdb_byte *
5114 locate_pdi_sibling (const struct die_reader_specs *reader,
5115 struct partial_die_info *orig_pdi,
5116 gdb_byte *info_ptr)
5117 {
5118 /* Do we know the sibling already? */
5119
5120 if (orig_pdi->sibling)
5121 return orig_pdi->sibling;
5122
5123 /* Are there any children to deal with? */
5124
5125 if (!orig_pdi->has_children)
5126 return info_ptr;
5127
5128 /* Skip the children the long way. */
5129
5130 return skip_children (reader, info_ptr);
5131 }
5132
5133 /* Expand this partial symbol table into a full symbol table. */
5134
5135 static void
5136 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5137 {
5138 if (pst != NULL)
5139 {
5140 if (pst->readin)
5141 {
5142 warning (_("bug: psymtab for %s is already read in."),
5143 pst->filename);
5144 }
5145 else
5146 {
5147 if (info_verbose)
5148 {
5149 printf_filtered (_("Reading in symbols for %s..."),
5150 pst->filename);
5151 gdb_flush (gdb_stdout);
5152 }
5153
5154 /* Restore our global data. */
5155 dwarf2_per_objfile = objfile_data (pst->objfile,
5156 dwarf2_objfile_data_key);
5157
5158 /* If this psymtab is constructed from a debug-only objfile, the
5159 has_section_at_zero flag will not necessarily be correct. We
5160 can get the correct value for this flag by looking at the data
5161 associated with the (presumably stripped) associated objfile. */
5162 if (pst->objfile->separate_debug_objfile_backlink)
5163 {
5164 struct dwarf2_per_objfile *dpo_backlink
5165 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5166 dwarf2_objfile_data_key);
5167
5168 dwarf2_per_objfile->has_section_at_zero
5169 = dpo_backlink->has_section_at_zero;
5170 }
5171
5172 dwarf2_per_objfile->reading_partial_symbols = 0;
5173
5174 psymtab_to_symtab_1 (pst);
5175
5176 /* Finish up the debug error message. */
5177 if (info_verbose)
5178 printf_filtered (_("done.\n"));
5179 }
5180 }
5181
5182 process_cu_includes ();
5183 }
5184 \f
5185 /* Reading in full CUs. */
5186
5187 /* Add PER_CU to the queue. */
5188
5189 static void
5190 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5191 enum language pretend_language)
5192 {
5193 struct dwarf2_queue_item *item;
5194
5195 per_cu->queued = 1;
5196 item = xmalloc (sizeof (*item));
5197 item->per_cu = per_cu;
5198 item->pretend_language = pretend_language;
5199 item->next = NULL;
5200
5201 if (dwarf2_queue == NULL)
5202 dwarf2_queue = item;
5203 else
5204 dwarf2_queue_tail->next = item;
5205
5206 dwarf2_queue_tail = item;
5207 }
5208
5209 /* Process the queue. */
5210
5211 static void
5212 process_queue (void)
5213 {
5214 struct dwarf2_queue_item *item, *next_item;
5215
5216 /* The queue starts out with one item, but following a DIE reference
5217 may load a new CU, adding it to the end of the queue. */
5218 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5219 {
5220 if (dwarf2_per_objfile->using_index
5221 ? !item->per_cu->v.quick->symtab
5222 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5223 process_full_comp_unit (item->per_cu, item->pretend_language);
5224
5225 item->per_cu->queued = 0;
5226 next_item = item->next;
5227 xfree (item);
5228 }
5229
5230 dwarf2_queue_tail = NULL;
5231 }
5232
5233 /* Free all allocated queue entries. This function only releases anything if
5234 an error was thrown; if the queue was processed then it would have been
5235 freed as we went along. */
5236
5237 static void
5238 dwarf2_release_queue (void *dummy)
5239 {
5240 struct dwarf2_queue_item *item, *last;
5241
5242 item = dwarf2_queue;
5243 while (item)
5244 {
5245 /* Anything still marked queued is likely to be in an
5246 inconsistent state, so discard it. */
5247 if (item->per_cu->queued)
5248 {
5249 if (item->per_cu->cu != NULL)
5250 free_one_cached_comp_unit (item->per_cu);
5251 item->per_cu->queued = 0;
5252 }
5253
5254 last = item;
5255 item = item->next;
5256 xfree (last);
5257 }
5258
5259 dwarf2_queue = dwarf2_queue_tail = NULL;
5260 }
5261
5262 /* Read in full symbols for PST, and anything it depends on. */
5263
5264 static void
5265 psymtab_to_symtab_1 (struct partial_symtab *pst)
5266 {
5267 struct dwarf2_per_cu_data *per_cu;
5268 struct cleanup *back_to;
5269 int i;
5270
5271 if (pst->readin)
5272 return;
5273
5274 for (i = 0; i < pst->number_of_dependencies; i++)
5275 if (!pst->dependencies[i]->readin
5276 && pst->dependencies[i]->user == NULL)
5277 {
5278 /* Inform about additional files that need to be read in. */
5279 if (info_verbose)
5280 {
5281 /* FIXME: i18n: Need to make this a single string. */
5282 fputs_filtered (" ", gdb_stdout);
5283 wrap_here ("");
5284 fputs_filtered ("and ", gdb_stdout);
5285 wrap_here ("");
5286 printf_filtered ("%s...", pst->dependencies[i]->filename);
5287 wrap_here (""); /* Flush output. */
5288 gdb_flush (gdb_stdout);
5289 }
5290 psymtab_to_symtab_1 (pst->dependencies[i]);
5291 }
5292
5293 per_cu = pst->read_symtab_private;
5294
5295 if (per_cu == NULL)
5296 {
5297 /* It's an include file, no symbols to read for it.
5298 Everything is in the parent symtab. */
5299 pst->readin = 1;
5300 return;
5301 }
5302
5303 dw2_do_instantiate_symtab (per_cu);
5304 }
5305
5306 /* Trivial hash function for die_info: the hash value of a DIE
5307 is its offset in .debug_info for this objfile. */
5308
5309 static hashval_t
5310 die_hash (const void *item)
5311 {
5312 const struct die_info *die = item;
5313
5314 return die->offset.sect_off;
5315 }
5316
5317 /* Trivial comparison function for die_info structures: two DIEs
5318 are equal if they have the same offset. */
5319
5320 static int
5321 die_eq (const void *item_lhs, const void *item_rhs)
5322 {
5323 const struct die_info *die_lhs = item_lhs;
5324 const struct die_info *die_rhs = item_rhs;
5325
5326 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5327 }
5328
5329 /* die_reader_func for load_full_comp_unit.
5330 This is identical to read_signatured_type_reader,
5331 but is kept separate for now. */
5332
5333 static void
5334 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5335 gdb_byte *info_ptr,
5336 struct die_info *comp_unit_die,
5337 int has_children,
5338 void *data)
5339 {
5340 struct dwarf2_cu *cu = reader->cu;
5341 struct attribute *attr;
5342 enum language *language_ptr = data;
5343
5344 gdb_assert (cu->die_hash == NULL);
5345 cu->die_hash =
5346 htab_create_alloc_ex (cu->header.length / 12,
5347 die_hash,
5348 die_eq,
5349 NULL,
5350 &cu->comp_unit_obstack,
5351 hashtab_obstack_allocate,
5352 dummy_obstack_deallocate);
5353
5354 if (has_children)
5355 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5356 &info_ptr, comp_unit_die);
5357 cu->dies = comp_unit_die;
5358 /* comp_unit_die is not stored in die_hash, no need. */
5359
5360 /* We try not to read any attributes in this function, because not
5361 all CUs needed for references have been loaded yet, and symbol
5362 table processing isn't initialized. But we have to set the CU language,
5363 or we won't be able to build types correctly.
5364 Similarly, if we do not read the producer, we can not apply
5365 producer-specific interpretation. */
5366 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5367 }
5368
5369 /* Load the DIEs associated with PER_CU into memory. */
5370
5371 static void
5372 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5373 enum language pretend_language)
5374 {
5375 gdb_assert (! this_cu->is_debug_types);
5376
5377 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5378 &pretend_language);
5379 }
5380
5381 /* Add a DIE to the delayed physname list. */
5382
5383 static void
5384 add_to_method_list (struct type *type, int fnfield_index, int index,
5385 const char *name, struct die_info *die,
5386 struct dwarf2_cu *cu)
5387 {
5388 struct delayed_method_info mi;
5389 mi.type = type;
5390 mi.fnfield_index = fnfield_index;
5391 mi.index = index;
5392 mi.name = name;
5393 mi.die = die;
5394 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5395 }
5396
5397 /* A cleanup for freeing the delayed method list. */
5398
5399 static void
5400 free_delayed_list (void *ptr)
5401 {
5402 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5403 if (cu->method_list != NULL)
5404 {
5405 VEC_free (delayed_method_info, cu->method_list);
5406 cu->method_list = NULL;
5407 }
5408 }
5409
5410 /* Compute the physnames of any methods on the CU's method list.
5411
5412 The computation of method physnames is delayed in order to avoid the
5413 (bad) condition that one of the method's formal parameters is of an as yet
5414 incomplete type. */
5415
5416 static void
5417 compute_delayed_physnames (struct dwarf2_cu *cu)
5418 {
5419 int i;
5420 struct delayed_method_info *mi;
5421 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5422 {
5423 const char *physname;
5424 struct fn_fieldlist *fn_flp
5425 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5426 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5427 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5428 }
5429 }
5430
5431 /* Go objects should be embedded in a DW_TAG_module DIE,
5432 and it's not clear if/how imported objects will appear.
5433 To keep Go support simple until that's worked out,
5434 go back through what we've read and create something usable.
5435 We could do this while processing each DIE, and feels kinda cleaner,
5436 but that way is more invasive.
5437 This is to, for example, allow the user to type "p var" or "b main"
5438 without having to specify the package name, and allow lookups
5439 of module.object to work in contexts that use the expression
5440 parser. */
5441
5442 static void
5443 fixup_go_packaging (struct dwarf2_cu *cu)
5444 {
5445 char *package_name = NULL;
5446 struct pending *list;
5447 int i;
5448
5449 for (list = global_symbols; list != NULL; list = list->next)
5450 {
5451 for (i = 0; i < list->nsyms; ++i)
5452 {
5453 struct symbol *sym = list->symbol[i];
5454
5455 if (SYMBOL_LANGUAGE (sym) == language_go
5456 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5457 {
5458 char *this_package_name = go_symbol_package_name (sym);
5459
5460 if (this_package_name == NULL)
5461 continue;
5462 if (package_name == NULL)
5463 package_name = this_package_name;
5464 else
5465 {
5466 if (strcmp (package_name, this_package_name) != 0)
5467 complaint (&symfile_complaints,
5468 _("Symtab %s has objects from two different Go packages: %s and %s"),
5469 (sym->symtab && sym->symtab->filename
5470 ? sym->symtab->filename
5471 : cu->objfile->name),
5472 this_package_name, package_name);
5473 xfree (this_package_name);
5474 }
5475 }
5476 }
5477 }
5478
5479 if (package_name != NULL)
5480 {
5481 struct objfile *objfile = cu->objfile;
5482 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5483 package_name, objfile);
5484 struct symbol *sym;
5485
5486 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5487
5488 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5489 SYMBOL_SET_LANGUAGE (sym, language_go);
5490 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5491 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5492 e.g., "main" finds the "main" module and not C's main(). */
5493 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5494 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5495 SYMBOL_TYPE (sym) = type;
5496
5497 add_symbol_to_list (sym, &global_symbols);
5498
5499 xfree (package_name);
5500 }
5501 }
5502
5503 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5504
5505 /* Return the symtab for PER_CU. This works properly regardless of
5506 whether we're using the index or psymtabs. */
5507
5508 static struct symtab *
5509 get_symtab (struct dwarf2_per_cu_data *per_cu)
5510 {
5511 return (dwarf2_per_objfile->using_index
5512 ? per_cu->v.quick->symtab
5513 : per_cu->v.psymtab->symtab);
5514 }
5515
5516 /* A helper function for computing the list of all symbol tables
5517 included by PER_CU. */
5518
5519 static void
5520 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5521 htab_t all_children,
5522 struct dwarf2_per_cu_data *per_cu)
5523 {
5524 void **slot;
5525 int ix;
5526 struct dwarf2_per_cu_data *iter;
5527
5528 slot = htab_find_slot (all_children, per_cu, INSERT);
5529 if (*slot != NULL)
5530 {
5531 /* This inclusion and its children have been processed. */
5532 return;
5533 }
5534
5535 *slot = per_cu;
5536 /* Only add a CU if it has a symbol table. */
5537 if (get_symtab (per_cu) != NULL)
5538 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5539
5540 for (ix = 0;
5541 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5542 ++ix)
5543 recursively_compute_inclusions (result, all_children, iter);
5544 }
5545
5546 /* Compute the symtab 'includes' fields for the symtab related to
5547 PER_CU. */
5548
5549 static void
5550 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5551 {
5552 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5553 {
5554 int ix, len;
5555 struct dwarf2_per_cu_data *iter;
5556 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5557 htab_t all_children;
5558 struct symtab *symtab = get_symtab (per_cu);
5559
5560 /* If we don't have a symtab, we can just skip this case. */
5561 if (symtab == NULL)
5562 return;
5563
5564 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5565 NULL, xcalloc, xfree);
5566
5567 for (ix = 0;
5568 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5569 ix, iter);
5570 ++ix)
5571 recursively_compute_inclusions (&result_children, all_children, iter);
5572
5573 /* Now we have a transitive closure of all the included CUs, so
5574 we can convert it to a list of symtabs. */
5575 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5576 symtab->includes
5577 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5578 (len + 1) * sizeof (struct symtab *));
5579 for (ix = 0;
5580 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5581 ++ix)
5582 symtab->includes[ix] = get_symtab (iter);
5583 symtab->includes[len] = NULL;
5584
5585 VEC_free (dwarf2_per_cu_ptr, result_children);
5586 htab_delete (all_children);
5587 }
5588 }
5589
5590 /* Compute the 'includes' field for the symtabs of all the CUs we just
5591 read. */
5592
5593 static void
5594 process_cu_includes (void)
5595 {
5596 int ix;
5597 struct dwarf2_per_cu_data *iter;
5598
5599 for (ix = 0;
5600 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5601 ix, iter);
5602 ++ix)
5603 compute_symtab_includes (iter);
5604
5605 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5606 }
5607
5608 /* Generate full symbol information for PER_CU, whose DIEs have
5609 already been loaded into memory. */
5610
5611 static void
5612 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5613 enum language pretend_language)
5614 {
5615 struct dwarf2_cu *cu = per_cu->cu;
5616 struct objfile *objfile = per_cu->objfile;
5617 CORE_ADDR lowpc, highpc;
5618 struct symtab *symtab;
5619 struct cleanup *back_to, *delayed_list_cleanup;
5620 CORE_ADDR baseaddr;
5621
5622 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5623
5624 buildsym_init ();
5625 back_to = make_cleanup (really_free_pendings, NULL);
5626 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5627
5628 cu->list_in_scope = &file_symbols;
5629
5630 cu->language = pretend_language;
5631 cu->language_defn = language_def (cu->language);
5632
5633 /* Do line number decoding in read_file_scope () */
5634 process_die (cu->dies, cu);
5635
5636 /* For now fudge the Go package. */
5637 if (cu->language == language_go)
5638 fixup_go_packaging (cu);
5639
5640 /* Now that we have processed all the DIEs in the CU, all the types
5641 should be complete, and it should now be safe to compute all of the
5642 physnames. */
5643 compute_delayed_physnames (cu);
5644 do_cleanups (delayed_list_cleanup);
5645
5646 /* Some compilers don't define a DW_AT_high_pc attribute for the
5647 compilation unit. If the DW_AT_high_pc is missing, synthesize
5648 it, by scanning the DIE's below the compilation unit. */
5649 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5650
5651 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5652
5653 if (symtab != NULL)
5654 {
5655 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5656
5657 /* Set symtab language to language from DW_AT_language. If the
5658 compilation is from a C file generated by language preprocessors, do
5659 not set the language if it was already deduced by start_subfile. */
5660 if (!(cu->language == language_c && symtab->language != language_c))
5661 symtab->language = cu->language;
5662
5663 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5664 produce DW_AT_location with location lists but it can be possibly
5665 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5666 there were bugs in prologue debug info, fixed later in GCC-4.5
5667 by "unwind info for epilogues" patch (which is not directly related).
5668
5669 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5670 needed, it would be wrong due to missing DW_AT_producer there.
5671
5672 Still one can confuse GDB by using non-standard GCC compilation
5673 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5674 */
5675 if (cu->has_loclist && gcc_4_minor >= 5)
5676 symtab->locations_valid = 1;
5677
5678 if (gcc_4_minor >= 5)
5679 symtab->epilogue_unwind_valid = 1;
5680
5681 symtab->call_site_htab = cu->call_site_htab;
5682 }
5683
5684 if (dwarf2_per_objfile->using_index)
5685 per_cu->v.quick->symtab = symtab;
5686 else
5687 {
5688 struct partial_symtab *pst = per_cu->v.psymtab;
5689 pst->symtab = symtab;
5690 pst->readin = 1;
5691 }
5692
5693 /* Push it for inclusion processing later. */
5694 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5695
5696 do_cleanups (back_to);
5697 }
5698
5699 /* Process an imported unit DIE. */
5700
5701 static void
5702 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5703 {
5704 struct attribute *attr;
5705
5706 attr = dwarf2_attr (die, DW_AT_import, cu);
5707 if (attr != NULL)
5708 {
5709 struct dwarf2_per_cu_data *per_cu;
5710 struct symtab *imported_symtab;
5711 sect_offset offset;
5712
5713 offset = dwarf2_get_ref_die_offset (attr);
5714 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5715
5716 /* Queue the unit, if needed. */
5717 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5718 load_full_comp_unit (per_cu, cu->language);
5719
5720 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5721 per_cu);
5722 }
5723 }
5724
5725 /* Process a die and its children. */
5726
5727 static void
5728 process_die (struct die_info *die, struct dwarf2_cu *cu)
5729 {
5730 switch (die->tag)
5731 {
5732 case DW_TAG_padding:
5733 break;
5734 case DW_TAG_compile_unit:
5735 case DW_TAG_partial_unit:
5736 read_file_scope (die, cu);
5737 break;
5738 case DW_TAG_type_unit:
5739 read_type_unit_scope (die, cu);
5740 break;
5741 case DW_TAG_subprogram:
5742 case DW_TAG_inlined_subroutine:
5743 read_func_scope (die, cu);
5744 break;
5745 case DW_TAG_lexical_block:
5746 case DW_TAG_try_block:
5747 case DW_TAG_catch_block:
5748 read_lexical_block_scope (die, cu);
5749 break;
5750 case DW_TAG_GNU_call_site:
5751 read_call_site_scope (die, cu);
5752 break;
5753 case DW_TAG_class_type:
5754 case DW_TAG_interface_type:
5755 case DW_TAG_structure_type:
5756 case DW_TAG_union_type:
5757 process_structure_scope (die, cu);
5758 break;
5759 case DW_TAG_enumeration_type:
5760 process_enumeration_scope (die, cu);
5761 break;
5762
5763 /* These dies have a type, but processing them does not create
5764 a symbol or recurse to process the children. Therefore we can
5765 read them on-demand through read_type_die. */
5766 case DW_TAG_subroutine_type:
5767 case DW_TAG_set_type:
5768 case DW_TAG_array_type:
5769 case DW_TAG_pointer_type:
5770 case DW_TAG_ptr_to_member_type:
5771 case DW_TAG_reference_type:
5772 case DW_TAG_string_type:
5773 break;
5774
5775 case DW_TAG_base_type:
5776 case DW_TAG_subrange_type:
5777 case DW_TAG_typedef:
5778 /* Add a typedef symbol for the type definition, if it has a
5779 DW_AT_name. */
5780 new_symbol (die, read_type_die (die, cu), cu);
5781 break;
5782 case DW_TAG_common_block:
5783 read_common_block (die, cu);
5784 break;
5785 case DW_TAG_common_inclusion:
5786 break;
5787 case DW_TAG_namespace:
5788 processing_has_namespace_info = 1;
5789 read_namespace (die, cu);
5790 break;
5791 case DW_TAG_module:
5792 processing_has_namespace_info = 1;
5793 read_module (die, cu);
5794 break;
5795 case DW_TAG_imported_declaration:
5796 case DW_TAG_imported_module:
5797 processing_has_namespace_info = 1;
5798 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5799 || cu->language != language_fortran))
5800 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5801 dwarf_tag_name (die->tag));
5802 read_import_statement (die, cu);
5803 break;
5804
5805 case DW_TAG_imported_unit:
5806 process_imported_unit_die (die, cu);
5807 break;
5808
5809 default:
5810 new_symbol (die, NULL, cu);
5811 break;
5812 }
5813 }
5814
5815 /* A helper function for dwarf2_compute_name which determines whether DIE
5816 needs to have the name of the scope prepended to the name listed in the
5817 die. */
5818
5819 static int
5820 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5821 {
5822 struct attribute *attr;
5823
5824 switch (die->tag)
5825 {
5826 case DW_TAG_namespace:
5827 case DW_TAG_typedef:
5828 case DW_TAG_class_type:
5829 case DW_TAG_interface_type:
5830 case DW_TAG_structure_type:
5831 case DW_TAG_union_type:
5832 case DW_TAG_enumeration_type:
5833 case DW_TAG_enumerator:
5834 case DW_TAG_subprogram:
5835 case DW_TAG_member:
5836 return 1;
5837
5838 case DW_TAG_variable:
5839 case DW_TAG_constant:
5840 /* We only need to prefix "globally" visible variables. These include
5841 any variable marked with DW_AT_external or any variable that
5842 lives in a namespace. [Variables in anonymous namespaces
5843 require prefixing, but they are not DW_AT_external.] */
5844
5845 if (dwarf2_attr (die, DW_AT_specification, cu))
5846 {
5847 struct dwarf2_cu *spec_cu = cu;
5848
5849 return die_needs_namespace (die_specification (die, &spec_cu),
5850 spec_cu);
5851 }
5852
5853 attr = dwarf2_attr (die, DW_AT_external, cu);
5854 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5855 && die->parent->tag != DW_TAG_module)
5856 return 0;
5857 /* A variable in a lexical block of some kind does not need a
5858 namespace, even though in C++ such variables may be external
5859 and have a mangled name. */
5860 if (die->parent->tag == DW_TAG_lexical_block
5861 || die->parent->tag == DW_TAG_try_block
5862 || die->parent->tag == DW_TAG_catch_block
5863 || die->parent->tag == DW_TAG_subprogram)
5864 return 0;
5865 return 1;
5866
5867 default:
5868 return 0;
5869 }
5870 }
5871
5872 /* Retrieve the last character from a mem_file. */
5873
5874 static void
5875 do_ui_file_peek_last (void *object, const char *buffer, long length)
5876 {
5877 char *last_char_p = (char *) object;
5878
5879 if (length > 0)
5880 *last_char_p = buffer[length - 1];
5881 }
5882
5883 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5884 compute the physname for the object, which include a method's:
5885 - formal parameters (C++/Java),
5886 - receiver type (Go),
5887 - return type (Java).
5888
5889 The term "physname" is a bit confusing.
5890 For C++, for example, it is the demangled name.
5891 For Go, for example, it's the mangled name.
5892
5893 For Ada, return the DIE's linkage name rather than the fully qualified
5894 name. PHYSNAME is ignored..
5895
5896 The result is allocated on the objfile_obstack and canonicalized. */
5897
5898 static const char *
5899 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5900 int physname)
5901 {
5902 struct objfile *objfile = cu->objfile;
5903
5904 if (name == NULL)
5905 name = dwarf2_name (die, cu);
5906
5907 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5908 compute it by typename_concat inside GDB. */
5909 if (cu->language == language_ada
5910 || (cu->language == language_fortran && physname))
5911 {
5912 /* For Ada unit, we prefer the linkage name over the name, as
5913 the former contains the exported name, which the user expects
5914 to be able to reference. Ideally, we want the user to be able
5915 to reference this entity using either natural or linkage name,
5916 but we haven't started looking at this enhancement yet. */
5917 struct attribute *attr;
5918
5919 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5920 if (attr == NULL)
5921 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5922 if (attr && DW_STRING (attr))
5923 return DW_STRING (attr);
5924 }
5925
5926 /* These are the only languages we know how to qualify names in. */
5927 if (name != NULL
5928 && (cu->language == language_cplus || cu->language == language_java
5929 || cu->language == language_fortran))
5930 {
5931 if (die_needs_namespace (die, cu))
5932 {
5933 long length;
5934 const char *prefix;
5935 struct ui_file *buf;
5936
5937 prefix = determine_prefix (die, cu);
5938 buf = mem_fileopen ();
5939 if (*prefix != '\0')
5940 {
5941 char *prefixed_name = typename_concat (NULL, prefix, name,
5942 physname, cu);
5943
5944 fputs_unfiltered (prefixed_name, buf);
5945 xfree (prefixed_name);
5946 }
5947 else
5948 fputs_unfiltered (name, buf);
5949
5950 /* Template parameters may be specified in the DIE's DW_AT_name, or
5951 as children with DW_TAG_template_type_param or
5952 DW_TAG_value_type_param. If the latter, add them to the name
5953 here. If the name already has template parameters, then
5954 skip this step; some versions of GCC emit both, and
5955 it is more efficient to use the pre-computed name.
5956
5957 Something to keep in mind about this process: it is very
5958 unlikely, or in some cases downright impossible, to produce
5959 something that will match the mangled name of a function.
5960 If the definition of the function has the same debug info,
5961 we should be able to match up with it anyway. But fallbacks
5962 using the minimal symbol, for instance to find a method
5963 implemented in a stripped copy of libstdc++, will not work.
5964 If we do not have debug info for the definition, we will have to
5965 match them up some other way.
5966
5967 When we do name matching there is a related problem with function
5968 templates; two instantiated function templates are allowed to
5969 differ only by their return types, which we do not add here. */
5970
5971 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5972 {
5973 struct attribute *attr;
5974 struct die_info *child;
5975 int first = 1;
5976
5977 die->building_fullname = 1;
5978
5979 for (child = die->child; child != NULL; child = child->sibling)
5980 {
5981 struct type *type;
5982 LONGEST value;
5983 gdb_byte *bytes;
5984 struct dwarf2_locexpr_baton *baton;
5985 struct value *v;
5986
5987 if (child->tag != DW_TAG_template_type_param
5988 && child->tag != DW_TAG_template_value_param)
5989 continue;
5990
5991 if (first)
5992 {
5993 fputs_unfiltered ("<", buf);
5994 first = 0;
5995 }
5996 else
5997 fputs_unfiltered (", ", buf);
5998
5999 attr = dwarf2_attr (child, DW_AT_type, cu);
6000 if (attr == NULL)
6001 {
6002 complaint (&symfile_complaints,
6003 _("template parameter missing DW_AT_type"));
6004 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6005 continue;
6006 }
6007 type = die_type (child, cu);
6008
6009 if (child->tag == DW_TAG_template_type_param)
6010 {
6011 c_print_type (type, "", buf, -1, 0);
6012 continue;
6013 }
6014
6015 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6016 if (attr == NULL)
6017 {
6018 complaint (&symfile_complaints,
6019 _("template parameter missing "
6020 "DW_AT_const_value"));
6021 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6022 continue;
6023 }
6024
6025 dwarf2_const_value_attr (attr, type, name,
6026 &cu->comp_unit_obstack, cu,
6027 &value, &bytes, &baton);
6028
6029 if (TYPE_NOSIGN (type))
6030 /* GDB prints characters as NUMBER 'CHAR'. If that's
6031 changed, this can use value_print instead. */
6032 c_printchar (value, type, buf);
6033 else
6034 {
6035 struct value_print_options opts;
6036
6037 if (baton != NULL)
6038 v = dwarf2_evaluate_loc_desc (type, NULL,
6039 baton->data,
6040 baton->size,
6041 baton->per_cu);
6042 else if (bytes != NULL)
6043 {
6044 v = allocate_value (type);
6045 memcpy (value_contents_writeable (v), bytes,
6046 TYPE_LENGTH (type));
6047 }
6048 else
6049 v = value_from_longest (type, value);
6050
6051 /* Specify decimal so that we do not depend on
6052 the radix. */
6053 get_formatted_print_options (&opts, 'd');
6054 opts.raw = 1;
6055 value_print (v, buf, &opts);
6056 release_value (v);
6057 value_free (v);
6058 }
6059 }
6060
6061 die->building_fullname = 0;
6062
6063 if (!first)
6064 {
6065 /* Close the argument list, with a space if necessary
6066 (nested templates). */
6067 char last_char = '\0';
6068 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6069 if (last_char == '>')
6070 fputs_unfiltered (" >", buf);
6071 else
6072 fputs_unfiltered (">", buf);
6073 }
6074 }
6075
6076 /* For Java and C++ methods, append formal parameter type
6077 information, if PHYSNAME. */
6078
6079 if (physname && die->tag == DW_TAG_subprogram
6080 && (cu->language == language_cplus
6081 || cu->language == language_java))
6082 {
6083 struct type *type = read_type_die (die, cu);
6084
6085 c_type_print_args (type, buf, 1, cu->language);
6086
6087 if (cu->language == language_java)
6088 {
6089 /* For java, we must append the return type to method
6090 names. */
6091 if (die->tag == DW_TAG_subprogram)
6092 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6093 0, 0);
6094 }
6095 else if (cu->language == language_cplus)
6096 {
6097 /* Assume that an artificial first parameter is
6098 "this", but do not crash if it is not. RealView
6099 marks unnamed (and thus unused) parameters as
6100 artificial; there is no way to differentiate
6101 the two cases. */
6102 if (TYPE_NFIELDS (type) > 0
6103 && TYPE_FIELD_ARTIFICIAL (type, 0)
6104 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6105 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6106 0))))
6107 fputs_unfiltered (" const", buf);
6108 }
6109 }
6110
6111 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6112 &length);
6113 ui_file_delete (buf);
6114
6115 if (cu->language == language_cplus)
6116 {
6117 char *cname
6118 = dwarf2_canonicalize_name (name, cu,
6119 &objfile->objfile_obstack);
6120
6121 if (cname != NULL)
6122 name = cname;
6123 }
6124 }
6125 }
6126
6127 return name;
6128 }
6129
6130 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6131 If scope qualifiers are appropriate they will be added. The result
6132 will be allocated on the objfile_obstack, or NULL if the DIE does
6133 not have a name. NAME may either be from a previous call to
6134 dwarf2_name or NULL.
6135
6136 The output string will be canonicalized (if C++/Java). */
6137
6138 static const char *
6139 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6140 {
6141 return dwarf2_compute_name (name, die, cu, 0);
6142 }
6143
6144 /* Construct a physname for the given DIE in CU. NAME may either be
6145 from a previous call to dwarf2_name or NULL. The result will be
6146 allocated on the objfile_objstack or NULL if the DIE does not have a
6147 name.
6148
6149 The output string will be canonicalized (if C++/Java). */
6150
6151 static const char *
6152 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6153 {
6154 struct objfile *objfile = cu->objfile;
6155 struct attribute *attr;
6156 const char *retval, *mangled = NULL, *canon = NULL;
6157 struct cleanup *back_to;
6158 int need_copy = 1;
6159
6160 /* In this case dwarf2_compute_name is just a shortcut not building anything
6161 on its own. */
6162 if (!die_needs_namespace (die, cu))
6163 return dwarf2_compute_name (name, die, cu, 1);
6164
6165 back_to = make_cleanup (null_cleanup, NULL);
6166
6167 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6168 if (!attr)
6169 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6170
6171 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6172 has computed. */
6173 if (attr && DW_STRING (attr))
6174 {
6175 char *demangled;
6176
6177 mangled = DW_STRING (attr);
6178
6179 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6180 type. It is easier for GDB users to search for such functions as
6181 `name(params)' than `long name(params)'. In such case the minimal
6182 symbol names do not match the full symbol names but for template
6183 functions there is never a need to look up their definition from their
6184 declaration so the only disadvantage remains the minimal symbol
6185 variant `long name(params)' does not have the proper inferior type.
6186 */
6187
6188 if (cu->language == language_go)
6189 {
6190 /* This is a lie, but we already lie to the caller new_symbol_full.
6191 new_symbol_full assumes we return the mangled name.
6192 This just undoes that lie until things are cleaned up. */
6193 demangled = NULL;
6194 }
6195 else
6196 {
6197 demangled = cplus_demangle (mangled,
6198 (DMGL_PARAMS | DMGL_ANSI
6199 | (cu->language == language_java
6200 ? DMGL_JAVA | DMGL_RET_POSTFIX
6201 : DMGL_RET_DROP)));
6202 }
6203 if (demangled)
6204 {
6205 make_cleanup (xfree, demangled);
6206 canon = demangled;
6207 }
6208 else
6209 {
6210 canon = mangled;
6211 need_copy = 0;
6212 }
6213 }
6214
6215 if (canon == NULL || check_physname)
6216 {
6217 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6218
6219 if (canon != NULL && strcmp (physname, canon) != 0)
6220 {
6221 /* It may not mean a bug in GDB. The compiler could also
6222 compute DW_AT_linkage_name incorrectly. But in such case
6223 GDB would need to be bug-to-bug compatible. */
6224
6225 complaint (&symfile_complaints,
6226 _("Computed physname <%s> does not match demangled <%s> "
6227 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6228 physname, canon, mangled, die->offset.sect_off, objfile->name);
6229
6230 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6231 is available here - over computed PHYSNAME. It is safer
6232 against both buggy GDB and buggy compilers. */
6233
6234 retval = canon;
6235 }
6236 else
6237 {
6238 retval = physname;
6239 need_copy = 0;
6240 }
6241 }
6242 else
6243 retval = canon;
6244
6245 if (need_copy)
6246 retval = obsavestring (retval, strlen (retval),
6247 &objfile->objfile_obstack);
6248
6249 do_cleanups (back_to);
6250 return retval;
6251 }
6252
6253 /* Read the import statement specified by the given die and record it. */
6254
6255 static void
6256 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6257 {
6258 struct objfile *objfile = cu->objfile;
6259 struct attribute *import_attr;
6260 struct die_info *imported_die, *child_die;
6261 struct dwarf2_cu *imported_cu;
6262 const char *imported_name;
6263 const char *imported_name_prefix;
6264 const char *canonical_name;
6265 const char *import_alias;
6266 const char *imported_declaration = NULL;
6267 const char *import_prefix;
6268 VEC (const_char_ptr) *excludes = NULL;
6269 struct cleanup *cleanups;
6270
6271 char *temp;
6272
6273 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6274 if (import_attr == NULL)
6275 {
6276 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6277 dwarf_tag_name (die->tag));
6278 return;
6279 }
6280
6281 imported_cu = cu;
6282 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6283 imported_name = dwarf2_name (imported_die, imported_cu);
6284 if (imported_name == NULL)
6285 {
6286 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6287
6288 The import in the following code:
6289 namespace A
6290 {
6291 typedef int B;
6292 }
6293
6294 int main ()
6295 {
6296 using A::B;
6297 B b;
6298 return b;
6299 }
6300
6301 ...
6302 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6303 <52> DW_AT_decl_file : 1
6304 <53> DW_AT_decl_line : 6
6305 <54> DW_AT_import : <0x75>
6306 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6307 <59> DW_AT_name : B
6308 <5b> DW_AT_decl_file : 1
6309 <5c> DW_AT_decl_line : 2
6310 <5d> DW_AT_type : <0x6e>
6311 ...
6312 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6313 <76> DW_AT_byte_size : 4
6314 <77> DW_AT_encoding : 5 (signed)
6315
6316 imports the wrong die ( 0x75 instead of 0x58 ).
6317 This case will be ignored until the gcc bug is fixed. */
6318 return;
6319 }
6320
6321 /* Figure out the local name after import. */
6322 import_alias = dwarf2_name (die, cu);
6323
6324 /* Figure out where the statement is being imported to. */
6325 import_prefix = determine_prefix (die, cu);
6326
6327 /* Figure out what the scope of the imported die is and prepend it
6328 to the name of the imported die. */
6329 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6330
6331 if (imported_die->tag != DW_TAG_namespace
6332 && imported_die->tag != DW_TAG_module)
6333 {
6334 imported_declaration = imported_name;
6335 canonical_name = imported_name_prefix;
6336 }
6337 else if (strlen (imported_name_prefix) > 0)
6338 {
6339 temp = alloca (strlen (imported_name_prefix)
6340 + 2 + strlen (imported_name) + 1);
6341 strcpy (temp, imported_name_prefix);
6342 strcat (temp, "::");
6343 strcat (temp, imported_name);
6344 canonical_name = temp;
6345 }
6346 else
6347 canonical_name = imported_name;
6348
6349 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6350
6351 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6352 for (child_die = die->child; child_die && child_die->tag;
6353 child_die = sibling_die (child_die))
6354 {
6355 /* DWARF-4: A Fortran use statement with a “rename list” may be
6356 represented by an imported module entry with an import attribute
6357 referring to the module and owned entries corresponding to those
6358 entities that are renamed as part of being imported. */
6359
6360 if (child_die->tag != DW_TAG_imported_declaration)
6361 {
6362 complaint (&symfile_complaints,
6363 _("child DW_TAG_imported_declaration expected "
6364 "- DIE at 0x%x [in module %s]"),
6365 child_die->offset.sect_off, objfile->name);
6366 continue;
6367 }
6368
6369 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6370 if (import_attr == NULL)
6371 {
6372 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6373 dwarf_tag_name (child_die->tag));
6374 continue;
6375 }
6376
6377 imported_cu = cu;
6378 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6379 &imported_cu);
6380 imported_name = dwarf2_name (imported_die, imported_cu);
6381 if (imported_name == NULL)
6382 {
6383 complaint (&symfile_complaints,
6384 _("child DW_TAG_imported_declaration has unknown "
6385 "imported name - DIE at 0x%x [in module %s]"),
6386 child_die->offset.sect_off, objfile->name);
6387 continue;
6388 }
6389
6390 VEC_safe_push (const_char_ptr, excludes, imported_name);
6391
6392 process_die (child_die, cu);
6393 }
6394
6395 cp_add_using_directive (import_prefix,
6396 canonical_name,
6397 import_alias,
6398 imported_declaration,
6399 excludes,
6400 &objfile->objfile_obstack);
6401
6402 do_cleanups (cleanups);
6403 }
6404
6405 /* Cleanup function for read_file_scope. */
6406
6407 static void
6408 free_cu_line_header (void *arg)
6409 {
6410 struct dwarf2_cu *cu = arg;
6411
6412 free_line_header (cu->line_header);
6413 cu->line_header = NULL;
6414 }
6415
6416 static void
6417 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6418 char **name, char **comp_dir)
6419 {
6420 struct attribute *attr;
6421
6422 *name = NULL;
6423 *comp_dir = NULL;
6424
6425 /* Find the filename. Do not use dwarf2_name here, since the filename
6426 is not a source language identifier. */
6427 attr = dwarf2_attr (die, DW_AT_name, cu);
6428 if (attr)
6429 {
6430 *name = DW_STRING (attr);
6431 }
6432
6433 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6434 if (attr)
6435 *comp_dir = DW_STRING (attr);
6436 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6437 {
6438 *comp_dir = ldirname (*name);
6439 if (*comp_dir != NULL)
6440 make_cleanup (xfree, *comp_dir);
6441 }
6442 if (*comp_dir != NULL)
6443 {
6444 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6445 directory, get rid of it. */
6446 char *cp = strchr (*comp_dir, ':');
6447
6448 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6449 *comp_dir = cp + 1;
6450 }
6451
6452 if (*name == NULL)
6453 *name = "<unknown>";
6454 }
6455
6456 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6457 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6458 COMP_DIR is the compilation directory.
6459 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6460
6461 static void
6462 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6463 const char *comp_dir, int want_line_info)
6464 {
6465 struct attribute *attr;
6466
6467 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6468 if (attr)
6469 {
6470 unsigned int line_offset = DW_UNSND (attr);
6471 struct line_header *line_header
6472 = dwarf_decode_line_header (line_offset, cu);
6473
6474 if (line_header)
6475 {
6476 cu->line_header = line_header;
6477 make_cleanup (free_cu_line_header, cu);
6478 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6479 }
6480 }
6481 }
6482
6483 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6484
6485 static void
6486 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6487 {
6488 struct objfile *objfile = dwarf2_per_objfile->objfile;
6489 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6490 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6491 CORE_ADDR highpc = ((CORE_ADDR) 0);
6492 struct attribute *attr;
6493 char *name = NULL;
6494 char *comp_dir = NULL;
6495 struct die_info *child_die;
6496 bfd *abfd = objfile->obfd;
6497 CORE_ADDR baseaddr;
6498
6499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6500
6501 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6502
6503 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6504 from finish_block. */
6505 if (lowpc == ((CORE_ADDR) -1))
6506 lowpc = highpc;
6507 lowpc += baseaddr;
6508 highpc += baseaddr;
6509
6510 find_file_and_directory (die, cu, &name, &comp_dir);
6511
6512 prepare_one_comp_unit (cu, die, cu->language);
6513
6514 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6515 standardised yet. As a workaround for the language detection we fall
6516 back to the DW_AT_producer string. */
6517 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6518 cu->language = language_opencl;
6519
6520 /* Similar hack for Go. */
6521 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6522 set_cu_language (DW_LANG_Go, cu);
6523
6524 /* We assume that we're processing GCC output. */
6525 processing_gcc_compilation = 2;
6526
6527 processing_has_namespace_info = 0;
6528
6529 start_symtab (name, comp_dir, lowpc);
6530 record_debugformat ("DWARF 2");
6531 record_producer (cu->producer);
6532
6533 /* Decode line number information if present. We do this before
6534 processing child DIEs, so that the line header table is available
6535 for DW_AT_decl_file. */
6536 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6537
6538 /* Process all dies in compilation unit. */
6539 if (die->child != NULL)
6540 {
6541 child_die = die->child;
6542 while (child_die && child_die->tag)
6543 {
6544 process_die (child_die, cu);
6545 child_die = sibling_die (child_die);
6546 }
6547 }
6548
6549 /* Decode macro information, if present. Dwarf 2 macro information
6550 refers to information in the line number info statement program
6551 header, so we can only read it if we've read the header
6552 successfully. */
6553 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6554 if (attr && cu->line_header)
6555 {
6556 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6557 complaint (&symfile_complaints,
6558 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6559
6560 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6561 comp_dir, abfd, cu,
6562 &dwarf2_per_objfile->macro, 1,
6563 ".debug_macro");
6564 }
6565 else
6566 {
6567 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6568 if (attr && cu->line_header)
6569 {
6570 unsigned int macro_offset = DW_UNSND (attr);
6571
6572 dwarf_decode_macros (cu->line_header, macro_offset,
6573 comp_dir, abfd, cu,
6574 &dwarf2_per_objfile->macinfo, 0,
6575 ".debug_macinfo");
6576 }
6577 }
6578
6579 do_cleanups (back_to);
6580 }
6581
6582 /* Process DW_TAG_type_unit.
6583 For TUs we want to skip the first top level sibling if it's not the
6584 actual type being defined by this TU. In this case the first top
6585 level sibling is there to provide context only. */
6586
6587 static void
6588 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6589 {
6590 struct objfile *objfile = cu->objfile;
6591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6592 CORE_ADDR lowpc;
6593 struct attribute *attr;
6594 char *name = NULL;
6595 char *comp_dir = NULL;
6596 struct die_info *child_die;
6597 bfd *abfd = objfile->obfd;
6598
6599 /* start_symtab needs a low pc, but we don't really have one.
6600 Do what read_file_scope would do in the absence of such info. */
6601 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6602
6603 /* Find the filename. Do not use dwarf2_name here, since the filename
6604 is not a source language identifier. */
6605 attr = dwarf2_attr (die, DW_AT_name, cu);
6606 if (attr)
6607 name = DW_STRING (attr);
6608
6609 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6610 if (attr)
6611 comp_dir = DW_STRING (attr);
6612 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6613 {
6614 comp_dir = ldirname (name);
6615 if (comp_dir != NULL)
6616 make_cleanup (xfree, comp_dir);
6617 }
6618
6619 if (name == NULL)
6620 name = "<unknown>";
6621
6622 prepare_one_comp_unit (cu, die, language_minimal);
6623
6624 /* We assume that we're processing GCC output. */
6625 processing_gcc_compilation = 2;
6626
6627 processing_has_namespace_info = 0;
6628
6629 start_symtab (name, comp_dir, lowpc);
6630 record_debugformat ("DWARF 2");
6631 record_producer (cu->producer);
6632
6633 /* Decode line number information if present. We do this before
6634 processing child DIEs, so that the line header table is available
6635 for DW_AT_decl_file.
6636 We don't need the pc/line-number mapping for type units. */
6637 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6638
6639 /* Process the dies in the type unit. */
6640 if (die->child == NULL)
6641 {
6642 dump_die_for_error (die);
6643 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6644 bfd_get_filename (abfd));
6645 }
6646
6647 child_die = die->child;
6648
6649 while (child_die && child_die->tag)
6650 {
6651 process_die (child_die, cu);
6652
6653 child_die = sibling_die (child_die);
6654 }
6655
6656 do_cleanups (back_to);
6657 }
6658 \f
6659 /* DWO files. */
6660
6661 static hashval_t
6662 hash_dwo_file (const void *item)
6663 {
6664 const struct dwo_file *dwo_file = item;
6665
6666 return htab_hash_string (dwo_file->dwo_name);
6667 }
6668
6669 static int
6670 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6671 {
6672 const struct dwo_file *lhs = item_lhs;
6673 const struct dwo_file *rhs = item_rhs;
6674
6675 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6676 }
6677
6678 /* Allocate a hash table for DWO files. */
6679
6680 static htab_t
6681 allocate_dwo_file_hash_table (void)
6682 {
6683 struct objfile *objfile = dwarf2_per_objfile->objfile;
6684
6685 return htab_create_alloc_ex (41,
6686 hash_dwo_file,
6687 eq_dwo_file,
6688 NULL,
6689 &objfile->objfile_obstack,
6690 hashtab_obstack_allocate,
6691 dummy_obstack_deallocate);
6692 }
6693
6694 static hashval_t
6695 hash_dwo_unit (const void *item)
6696 {
6697 const struct dwo_unit *dwo_unit = item;
6698
6699 /* This drops the top 32 bits of the id, but is ok for a hash. */
6700 return dwo_unit->signature;
6701 }
6702
6703 static int
6704 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6705 {
6706 const struct dwo_unit *lhs = item_lhs;
6707 const struct dwo_unit *rhs = item_rhs;
6708
6709 /* The signature is assumed to be unique within the DWO file.
6710 So while object file CU dwo_id's always have the value zero,
6711 that's OK, assuming each object file DWO file has only one CU,
6712 and that's the rule for now. */
6713 return lhs->signature == rhs->signature;
6714 }
6715
6716 /* Allocate a hash table for DWO CUs,TUs.
6717 There is one of these tables for each of CUs,TUs for each DWO file. */
6718
6719 static htab_t
6720 allocate_dwo_unit_table (struct objfile *objfile)
6721 {
6722 /* Start out with a pretty small number.
6723 Generally DWO files contain only one CU and maybe some TUs. */
6724 return htab_create_alloc_ex (3,
6725 hash_dwo_unit,
6726 eq_dwo_unit,
6727 NULL,
6728 &objfile->objfile_obstack,
6729 hashtab_obstack_allocate,
6730 dummy_obstack_deallocate);
6731 }
6732
6733 /* This function is mapped across the sections and remembers the offset and
6734 size of each of the DWO debugging sections we are interested in. */
6735
6736 static void
6737 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6738 {
6739 struct dwo_file *dwo_file = dwo_file_ptr;
6740 const struct dwo_section_names *names = &dwo_section_names;
6741
6742 if (section_is_p (sectp->name, &names->abbrev_dwo))
6743 {
6744 dwo_file->sections.abbrev.asection = sectp;
6745 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6746 }
6747 else if (section_is_p (sectp->name, &names->info_dwo))
6748 {
6749 dwo_file->sections.info.asection = sectp;
6750 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6751 }
6752 else if (section_is_p (sectp->name, &names->line_dwo))
6753 {
6754 dwo_file->sections.line.asection = sectp;
6755 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6756 }
6757 else if (section_is_p (sectp->name, &names->loc_dwo))
6758 {
6759 dwo_file->sections.loc.asection = sectp;
6760 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6761 }
6762 else if (section_is_p (sectp->name, &names->str_dwo))
6763 {
6764 dwo_file->sections.str.asection = sectp;
6765 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6766 }
6767 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6768 {
6769 dwo_file->sections.str_offsets.asection = sectp;
6770 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6771 }
6772 else if (section_is_p (sectp->name, &names->types_dwo))
6773 {
6774 struct dwarf2_section_info type_section;
6775
6776 memset (&type_section, 0, sizeof (type_section));
6777 type_section.asection = sectp;
6778 type_section.size = bfd_get_section_size (sectp);
6779 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6780 &type_section);
6781 }
6782 }
6783
6784 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6785
6786 struct create_dwo_info_table_data
6787 {
6788 struct dwo_file *dwo_file;
6789 htab_t cu_htab;
6790 };
6791
6792 /* die_reader_func for create_debug_info_hash_table. */
6793
6794 static void
6795 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6796 gdb_byte *info_ptr,
6797 struct die_info *comp_unit_die,
6798 int has_children,
6799 void *datap)
6800 {
6801 struct dwarf2_cu *cu = reader->cu;
6802 struct objfile *objfile = dwarf2_per_objfile->objfile;
6803 sect_offset offset = cu->per_cu->offset;
6804 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6805 struct create_dwo_info_table_data *data = datap;
6806 struct dwo_file *dwo_file = data->dwo_file;
6807 htab_t cu_htab = data->cu_htab;
6808 void **slot;
6809 struct attribute *attr;
6810 struct dwo_unit *dwo_unit;
6811
6812 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6813 if (attr == NULL)
6814 {
6815 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6816 " its dwo_id [in module %s]"),
6817 offset.sect_off, dwo_file->dwo_name);
6818 return;
6819 }
6820
6821 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6822 dwo_unit->dwo_file = dwo_file;
6823 dwo_unit->signature = DW_UNSND (attr);
6824 dwo_unit->info_or_types_section = section;
6825 dwo_unit->offset = offset;
6826 dwo_unit->length = cu->per_cu->length;
6827
6828 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6829 gdb_assert (slot != NULL);
6830 if (*slot != NULL)
6831 {
6832 const struct dwo_unit *dup_dwo_unit = *slot;
6833
6834 complaint (&symfile_complaints,
6835 _("debug entry at offset 0x%x is duplicate to the entry at"
6836 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6837 offset.sect_off, dup_dwo_unit->offset.sect_off,
6838 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6839 dwo_file->dwo_name);
6840 }
6841 else
6842 *slot = dwo_unit;
6843
6844 if (dwarf2_die_debug)
6845 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6846 offset.sect_off,
6847 phex (dwo_unit->signature,
6848 sizeof (dwo_unit->signature)));
6849 }
6850
6851 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6852
6853 static htab_t
6854 create_debug_info_hash_table (struct dwo_file *dwo_file)
6855 {
6856 struct objfile *objfile = dwarf2_per_objfile->objfile;
6857 struct dwarf2_section_info *section = &dwo_file->sections.info;
6858 bfd *abfd;
6859 htab_t cu_htab;
6860 gdb_byte *info_ptr, *end_ptr;
6861 struct create_dwo_info_table_data create_dwo_info_table_data;
6862
6863 dwarf2_read_section (objfile, section);
6864 info_ptr = section->buffer;
6865
6866 if (info_ptr == NULL)
6867 return NULL;
6868
6869 /* We can't set abfd until now because the section may be empty or
6870 not present, in which case section->asection will be NULL. */
6871 abfd = section->asection->owner;
6872
6873 if (dwarf2_die_debug)
6874 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6875 bfd_get_filename (abfd));
6876
6877 cu_htab = allocate_dwo_unit_table (objfile);
6878
6879 create_dwo_info_table_data.dwo_file = dwo_file;
6880 create_dwo_info_table_data.cu_htab = cu_htab;
6881
6882 end_ptr = info_ptr + section->size;
6883 while (info_ptr < end_ptr)
6884 {
6885 struct dwarf2_per_cu_data per_cu;
6886
6887 memset (&per_cu, 0, sizeof (per_cu));
6888 per_cu.objfile = objfile;
6889 per_cu.is_debug_types = 0;
6890 per_cu.offset.sect_off = info_ptr - section->buffer;
6891 per_cu.info_or_types_section = section;
6892
6893 init_cutu_and_read_dies_no_follow (&per_cu,
6894 &dwo_file->sections.abbrev,
6895 dwo_file,
6896 create_debug_info_hash_table_reader,
6897 &create_dwo_info_table_data);
6898
6899 info_ptr += per_cu.length;
6900 }
6901
6902 return cu_htab;
6903 }
6904
6905 /* Subroutine of open_dwo_file to simplify it.
6906 Open the file specified by FILE_NAME and hand it off to BFD for
6907 preliminary analysis. Return a newly initialized bfd *, which
6908 includes a canonicalized copy of FILE_NAME.
6909 In case of trouble, return NULL.
6910 NOTE: This function is derived from symfile_bfd_open. */
6911
6912 static bfd *
6913 try_open_dwo_file (const char *file_name)
6914 {
6915 bfd *sym_bfd;
6916 int desc;
6917 char *absolute_name;
6918 char *name;
6919
6920 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6921 O_RDONLY | O_BINARY, &absolute_name);
6922 if (desc < 0)
6923 return NULL;
6924
6925 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6926 if (!sym_bfd)
6927 {
6928 close (desc);
6929 xfree (absolute_name);
6930 return NULL;
6931 }
6932 bfd_set_cacheable (sym_bfd, 1);
6933
6934 if (!bfd_check_format (sym_bfd, bfd_object))
6935 {
6936 bfd_close (sym_bfd); /* This also closes desc. */
6937 xfree (absolute_name);
6938 return NULL;
6939 }
6940
6941 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6942 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6943
6944 return sym_bfd;
6945 }
6946
6947 /* Try to open DWO file DWO_NAME.
6948 COMP_DIR is the DW_AT_comp_dir attribute.
6949 The result is the bfd handle of the file.
6950 If there is a problem finding or opening the file, return NULL.
6951 Upon success, the canonicalized path of the file is stored in the bfd,
6952 same as symfile_bfd_open. */
6953
6954 static bfd *
6955 open_dwo_file (const char *dwo_name, const char *comp_dir)
6956 {
6957 bfd *abfd;
6958 char *path_to_try, *debug_dir;
6959
6960 if (IS_ABSOLUTE_PATH (dwo_name))
6961 return try_open_dwo_file (dwo_name);
6962
6963 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6964
6965 if (comp_dir != NULL)
6966 {
6967 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6968
6969 /* NOTE: If comp_dir is a relative path, this will also try the
6970 search path, which seems useful. */
6971 abfd = try_open_dwo_file (path_to_try);
6972 xfree (path_to_try);
6973 if (abfd != NULL)
6974 return abfd;
6975 }
6976
6977 /* That didn't work, try debug-file-directory, which, despite its name,
6978 is a list of paths. */
6979
6980 if (*debug_file_directory == '\0')
6981 return NULL;
6982
6983 return try_open_dwo_file (dwo_name);
6984 }
6985
6986 /* Initialize the use of the DWO file specified by DWO_NAME. */
6987
6988 static struct dwo_file *
6989 init_dwo_file (const char *dwo_name, const char *comp_dir)
6990 {
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6992 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6993 struct dwo_file);
6994 bfd *abfd;
6995 struct cleanup *cleanups;
6996
6997 if (dwarf2_die_debug)
6998 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
6999
7000 abfd = open_dwo_file (dwo_name, comp_dir);
7001 if (abfd == NULL)
7002 return NULL;
7003 dwo_file->dwo_name = dwo_name;
7004 dwo_file->dwo_bfd = abfd;
7005
7006 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7007
7008 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7009
7010 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7011
7012 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7013 dwo_file->sections.types);
7014
7015 discard_cleanups (cleanups);
7016
7017 return dwo_file;
7018 }
7019
7020 /* Lookup DWO file DWO_NAME. */
7021
7022 static struct dwo_file *
7023 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7024 {
7025 struct dwo_file *dwo_file;
7026 struct dwo_file find_entry;
7027 void **slot;
7028
7029 if (dwarf2_per_objfile->dwo_files == NULL)
7030 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7031
7032 /* Have we already seen this DWO file? */
7033 find_entry.dwo_name = dwo_name;
7034 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7035
7036 /* If not, read it in and build a table of the DWOs it contains. */
7037 if (*slot == NULL)
7038 *slot = init_dwo_file (dwo_name, comp_dir);
7039
7040 /* NOTE: This will be NULL if unable to open the file. */
7041 dwo_file = *slot;
7042
7043 return dwo_file;
7044 }
7045
7046 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7047 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7048 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7049 nomenclature as TUs).
7050 The result is the DWO CU or NULL if we didn't find it
7051 (dwo_id mismatch or couldn't find the DWO file). */
7052
7053 static struct dwo_unit *
7054 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7055 char *dwo_name, const char *comp_dir,
7056 ULONGEST signature)
7057 {
7058 struct objfile *objfile = dwarf2_per_objfile->objfile;
7059 struct dwo_file *dwo_file;
7060
7061 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7062 if (dwo_file == NULL)
7063 return NULL;
7064
7065 /* Look up the DWO using its signature(dwo_id). */
7066
7067 if (dwo_file->cus != NULL)
7068 {
7069 struct dwo_unit find_dwo_cu, *dwo_cu;
7070
7071 find_dwo_cu.signature = signature;
7072 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7073
7074 if (dwo_cu != NULL)
7075 return dwo_cu;
7076 }
7077
7078 /* We didn't find it. This must mean a dwo_id mismatch. */
7079
7080 complaint (&symfile_complaints,
7081 _("Could not find DWO CU referenced by CU at offset 0x%x"
7082 " [in module %s]"),
7083 this_cu->offset.sect_off, objfile->name);
7084 return NULL;
7085 }
7086
7087 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7088 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7089 The result is the DWO CU or NULL if we didn't find it
7090 (dwo_id mismatch or couldn't find the DWO file). */
7091
7092 static struct dwo_unit *
7093 lookup_dwo_type_unit (struct signatured_type *this_tu,
7094 char *dwo_name, const char *comp_dir)
7095 {
7096 struct objfile *objfile = dwarf2_per_objfile->objfile;
7097 struct dwo_file *dwo_file;
7098 struct dwo_unit find_dwo_tu, *dwo_tu;
7099
7100 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7101 if (dwo_file == NULL)
7102 return NULL;
7103
7104 /* Look up the DWO using its signature(dwo_id). */
7105
7106 if (dwo_file->tus != NULL)
7107 {
7108 struct dwo_unit find_dwo_tu, *dwo_tu;
7109
7110 find_dwo_tu.signature = this_tu->signature;
7111 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7112
7113 if (dwo_tu != NULL)
7114 return dwo_tu;
7115 }
7116
7117 /* We didn't find it. This must mean a dwo_id mismatch. */
7118
7119 complaint (&symfile_complaints,
7120 _("Could not find DWO TU referenced by TU at offset 0x%x"
7121 " [in module %s]"),
7122 this_tu->per_cu.offset.sect_off, objfile->name);
7123 return NULL;
7124 }
7125
7126 /* Free all resources associated with DWO_FILE.
7127 Close the DWO file and munmap the sections.
7128 All memory should be on the objfile obstack. */
7129
7130 static void
7131 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7132 {
7133 int ix;
7134 struct dwarf2_section_info *section;
7135
7136 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7137 bfd_close (dwo_file->dwo_bfd);
7138
7139 munmap_section_buffer (&dwo_file->sections.abbrev);
7140 munmap_section_buffer (&dwo_file->sections.info);
7141 munmap_section_buffer (&dwo_file->sections.line);
7142 munmap_section_buffer (&dwo_file->sections.loc);
7143 munmap_section_buffer (&dwo_file->sections.str);
7144 munmap_section_buffer (&dwo_file->sections.str_offsets);
7145
7146 for (ix = 0;
7147 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7148 ix, section);
7149 ++ix)
7150 munmap_section_buffer (section);
7151
7152 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7153 }
7154
7155 /* Wrapper for free_dwo_file for use in cleanups. */
7156
7157 static void
7158 free_dwo_file_cleanup (void *arg)
7159 {
7160 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7161 struct objfile *objfile = dwarf2_per_objfile->objfile;
7162
7163 free_dwo_file (dwo_file, objfile);
7164 }
7165
7166 /* Traversal function for free_dwo_files. */
7167
7168 static int
7169 free_dwo_file_from_slot (void **slot, void *info)
7170 {
7171 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7172 struct objfile *objfile = (struct objfile *) info;
7173
7174 free_dwo_file (dwo_file, objfile);
7175
7176 return 1;
7177 }
7178
7179 /* Free all resources associated with DWO_FILES. */
7180
7181 static void
7182 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7183 {
7184 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7185 }
7186 \f
7187 /* Read in various DIEs. */
7188
7189 /* qsort helper for inherit_abstract_dies. */
7190
7191 static int
7192 unsigned_int_compar (const void *ap, const void *bp)
7193 {
7194 unsigned int a = *(unsigned int *) ap;
7195 unsigned int b = *(unsigned int *) bp;
7196
7197 return (a > b) - (b > a);
7198 }
7199
7200 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7201 Inherit only the children of the DW_AT_abstract_origin DIE not being
7202 already referenced by DW_AT_abstract_origin from the children of the
7203 current DIE. */
7204
7205 static void
7206 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7207 {
7208 struct die_info *child_die;
7209 unsigned die_children_count;
7210 /* CU offsets which were referenced by children of the current DIE. */
7211 sect_offset *offsets;
7212 sect_offset *offsets_end, *offsetp;
7213 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7214 struct die_info *origin_die;
7215 /* Iterator of the ORIGIN_DIE children. */
7216 struct die_info *origin_child_die;
7217 struct cleanup *cleanups;
7218 struct attribute *attr;
7219 struct dwarf2_cu *origin_cu;
7220 struct pending **origin_previous_list_in_scope;
7221
7222 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7223 if (!attr)
7224 return;
7225
7226 /* Note that following die references may follow to a die in a
7227 different cu. */
7228
7229 origin_cu = cu;
7230 origin_die = follow_die_ref (die, attr, &origin_cu);
7231
7232 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7233 symbols in. */
7234 origin_previous_list_in_scope = origin_cu->list_in_scope;
7235 origin_cu->list_in_scope = cu->list_in_scope;
7236
7237 if (die->tag != origin_die->tag
7238 && !(die->tag == DW_TAG_inlined_subroutine
7239 && origin_die->tag == DW_TAG_subprogram))
7240 complaint (&symfile_complaints,
7241 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7242 die->offset.sect_off, origin_die->offset.sect_off);
7243
7244 child_die = die->child;
7245 die_children_count = 0;
7246 while (child_die && child_die->tag)
7247 {
7248 child_die = sibling_die (child_die);
7249 die_children_count++;
7250 }
7251 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7252 cleanups = make_cleanup (xfree, offsets);
7253
7254 offsets_end = offsets;
7255 child_die = die->child;
7256 while (child_die && child_die->tag)
7257 {
7258 /* For each CHILD_DIE, find the corresponding child of
7259 ORIGIN_DIE. If there is more than one layer of
7260 DW_AT_abstract_origin, follow them all; there shouldn't be,
7261 but GCC versions at least through 4.4 generate this (GCC PR
7262 40573). */
7263 struct die_info *child_origin_die = child_die;
7264 struct dwarf2_cu *child_origin_cu = cu;
7265
7266 while (1)
7267 {
7268 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7269 child_origin_cu);
7270 if (attr == NULL)
7271 break;
7272 child_origin_die = follow_die_ref (child_origin_die, attr,
7273 &child_origin_cu);
7274 }
7275
7276 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7277 counterpart may exist. */
7278 if (child_origin_die != child_die)
7279 {
7280 if (child_die->tag != child_origin_die->tag
7281 && !(child_die->tag == DW_TAG_inlined_subroutine
7282 && child_origin_die->tag == DW_TAG_subprogram))
7283 complaint (&symfile_complaints,
7284 _("Child DIE 0x%x and its abstract origin 0x%x have "
7285 "different tags"), child_die->offset.sect_off,
7286 child_origin_die->offset.sect_off);
7287 if (child_origin_die->parent != origin_die)
7288 complaint (&symfile_complaints,
7289 _("Child DIE 0x%x and its abstract origin 0x%x have "
7290 "different parents"), child_die->offset.sect_off,
7291 child_origin_die->offset.sect_off);
7292 else
7293 *offsets_end++ = child_origin_die->offset;
7294 }
7295 child_die = sibling_die (child_die);
7296 }
7297 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7298 unsigned_int_compar);
7299 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7300 if (offsetp[-1].sect_off == offsetp->sect_off)
7301 complaint (&symfile_complaints,
7302 _("Multiple children of DIE 0x%x refer "
7303 "to DIE 0x%x as their abstract origin"),
7304 die->offset.sect_off, offsetp->sect_off);
7305
7306 offsetp = offsets;
7307 origin_child_die = origin_die->child;
7308 while (origin_child_die && origin_child_die->tag)
7309 {
7310 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7311 while (offsetp < offsets_end
7312 && offsetp->sect_off < origin_child_die->offset.sect_off)
7313 offsetp++;
7314 if (offsetp >= offsets_end
7315 || offsetp->sect_off > origin_child_die->offset.sect_off)
7316 {
7317 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7318 process_die (origin_child_die, origin_cu);
7319 }
7320 origin_child_die = sibling_die (origin_child_die);
7321 }
7322 origin_cu->list_in_scope = origin_previous_list_in_scope;
7323
7324 do_cleanups (cleanups);
7325 }
7326
7327 static void
7328 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7329 {
7330 struct objfile *objfile = cu->objfile;
7331 struct context_stack *new;
7332 CORE_ADDR lowpc;
7333 CORE_ADDR highpc;
7334 struct die_info *child_die;
7335 struct attribute *attr, *call_line, *call_file;
7336 char *name;
7337 CORE_ADDR baseaddr;
7338 struct block *block;
7339 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7340 VEC (symbolp) *template_args = NULL;
7341 struct template_symbol *templ_func = NULL;
7342
7343 if (inlined_func)
7344 {
7345 /* If we do not have call site information, we can't show the
7346 caller of this inlined function. That's too confusing, so
7347 only use the scope for local variables. */
7348 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7349 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7350 if (call_line == NULL || call_file == NULL)
7351 {
7352 read_lexical_block_scope (die, cu);
7353 return;
7354 }
7355 }
7356
7357 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7358
7359 name = dwarf2_name (die, cu);
7360
7361 /* Ignore functions with missing or empty names. These are actually
7362 illegal according to the DWARF standard. */
7363 if (name == NULL)
7364 {
7365 complaint (&symfile_complaints,
7366 _("missing name for subprogram DIE at %d"),
7367 die->offset.sect_off);
7368 return;
7369 }
7370
7371 /* Ignore functions with missing or invalid low and high pc attributes. */
7372 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7373 {
7374 attr = dwarf2_attr (die, DW_AT_external, cu);
7375 if (!attr || !DW_UNSND (attr))
7376 complaint (&symfile_complaints,
7377 _("cannot get low and high bounds "
7378 "for subprogram DIE at %d"),
7379 die->offset.sect_off);
7380 return;
7381 }
7382
7383 lowpc += baseaddr;
7384 highpc += baseaddr;
7385
7386 /* If we have any template arguments, then we must allocate a
7387 different sort of symbol. */
7388 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7389 {
7390 if (child_die->tag == DW_TAG_template_type_param
7391 || child_die->tag == DW_TAG_template_value_param)
7392 {
7393 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7394 struct template_symbol);
7395 templ_func->base.is_cplus_template_function = 1;
7396 break;
7397 }
7398 }
7399
7400 new = push_context (0, lowpc);
7401 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7402 (struct symbol *) templ_func);
7403
7404 /* If there is a location expression for DW_AT_frame_base, record
7405 it. */
7406 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7407 if (attr)
7408 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7409 expression is being recorded directly in the function's symbol
7410 and not in a separate frame-base object. I guess this hack is
7411 to avoid adding some sort of frame-base adjunct/annex to the
7412 function's symbol :-(. The problem with doing this is that it
7413 results in a function symbol with a location expression that
7414 has nothing to do with the location of the function, ouch! The
7415 relationship should be: a function's symbol has-a frame base; a
7416 frame-base has-a location expression. */
7417 dwarf2_symbol_mark_computed (attr, new->name, cu);
7418
7419 cu->list_in_scope = &local_symbols;
7420
7421 if (die->child != NULL)
7422 {
7423 child_die = die->child;
7424 while (child_die && child_die->tag)
7425 {
7426 if (child_die->tag == DW_TAG_template_type_param
7427 || child_die->tag == DW_TAG_template_value_param)
7428 {
7429 struct symbol *arg = new_symbol (child_die, NULL, cu);
7430
7431 if (arg != NULL)
7432 VEC_safe_push (symbolp, template_args, arg);
7433 }
7434 else
7435 process_die (child_die, cu);
7436 child_die = sibling_die (child_die);
7437 }
7438 }
7439
7440 inherit_abstract_dies (die, cu);
7441
7442 /* If we have a DW_AT_specification, we might need to import using
7443 directives from the context of the specification DIE. See the
7444 comment in determine_prefix. */
7445 if (cu->language == language_cplus
7446 && dwarf2_attr (die, DW_AT_specification, cu))
7447 {
7448 struct dwarf2_cu *spec_cu = cu;
7449 struct die_info *spec_die = die_specification (die, &spec_cu);
7450
7451 while (spec_die)
7452 {
7453 child_die = spec_die->child;
7454 while (child_die && child_die->tag)
7455 {
7456 if (child_die->tag == DW_TAG_imported_module)
7457 process_die (child_die, spec_cu);
7458 child_die = sibling_die (child_die);
7459 }
7460
7461 /* In some cases, GCC generates specification DIEs that
7462 themselves contain DW_AT_specification attributes. */
7463 spec_die = die_specification (spec_die, &spec_cu);
7464 }
7465 }
7466
7467 new = pop_context ();
7468 /* Make a block for the local symbols within. */
7469 block = finish_block (new->name, &local_symbols, new->old_blocks,
7470 lowpc, highpc, objfile);
7471
7472 /* For C++, set the block's scope. */
7473 if (cu->language == language_cplus || cu->language == language_fortran)
7474 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7475 determine_prefix (die, cu),
7476 processing_has_namespace_info);
7477
7478 /* If we have address ranges, record them. */
7479 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7480
7481 /* Attach template arguments to function. */
7482 if (! VEC_empty (symbolp, template_args))
7483 {
7484 gdb_assert (templ_func != NULL);
7485
7486 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7487 templ_func->template_arguments
7488 = obstack_alloc (&objfile->objfile_obstack,
7489 (templ_func->n_template_arguments
7490 * sizeof (struct symbol *)));
7491 memcpy (templ_func->template_arguments,
7492 VEC_address (symbolp, template_args),
7493 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7494 VEC_free (symbolp, template_args);
7495 }
7496
7497 /* In C++, we can have functions nested inside functions (e.g., when
7498 a function declares a class that has methods). This means that
7499 when we finish processing a function scope, we may need to go
7500 back to building a containing block's symbol lists. */
7501 local_symbols = new->locals;
7502 param_symbols = new->params;
7503 using_directives = new->using_directives;
7504
7505 /* If we've finished processing a top-level function, subsequent
7506 symbols go in the file symbol list. */
7507 if (outermost_context_p ())
7508 cu->list_in_scope = &file_symbols;
7509 }
7510
7511 /* Process all the DIES contained within a lexical block scope. Start
7512 a new scope, process the dies, and then close the scope. */
7513
7514 static void
7515 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7516 {
7517 struct objfile *objfile = cu->objfile;
7518 struct context_stack *new;
7519 CORE_ADDR lowpc, highpc;
7520 struct die_info *child_die;
7521 CORE_ADDR baseaddr;
7522
7523 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7524
7525 /* Ignore blocks with missing or invalid low and high pc attributes. */
7526 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7527 as multiple lexical blocks? Handling children in a sane way would
7528 be nasty. Might be easier to properly extend generic blocks to
7529 describe ranges. */
7530 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7531 return;
7532 lowpc += baseaddr;
7533 highpc += baseaddr;
7534
7535 push_context (0, lowpc);
7536 if (die->child != NULL)
7537 {
7538 child_die = die->child;
7539 while (child_die && child_die->tag)
7540 {
7541 process_die (child_die, cu);
7542 child_die = sibling_die (child_die);
7543 }
7544 }
7545 new = pop_context ();
7546
7547 if (local_symbols != NULL || using_directives != NULL)
7548 {
7549 struct block *block
7550 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7551 highpc, objfile);
7552
7553 /* Note that recording ranges after traversing children, as we
7554 do here, means that recording a parent's ranges entails
7555 walking across all its children's ranges as they appear in
7556 the address map, which is quadratic behavior.
7557
7558 It would be nicer to record the parent's ranges before
7559 traversing its children, simply overriding whatever you find
7560 there. But since we don't even decide whether to create a
7561 block until after we've traversed its children, that's hard
7562 to do. */
7563 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7564 }
7565 local_symbols = new->locals;
7566 using_directives = new->using_directives;
7567 }
7568
7569 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7570
7571 static void
7572 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7573 {
7574 struct objfile *objfile = cu->objfile;
7575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7576 CORE_ADDR pc, baseaddr;
7577 struct attribute *attr;
7578 struct call_site *call_site, call_site_local;
7579 void **slot;
7580 int nparams;
7581 struct die_info *child_die;
7582
7583 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7584
7585 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7586 if (!attr)
7587 {
7588 complaint (&symfile_complaints,
7589 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7590 "DIE 0x%x [in module %s]"),
7591 die->offset.sect_off, objfile->name);
7592 return;
7593 }
7594 pc = DW_ADDR (attr) + baseaddr;
7595
7596 if (cu->call_site_htab == NULL)
7597 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7598 NULL, &objfile->objfile_obstack,
7599 hashtab_obstack_allocate, NULL);
7600 call_site_local.pc = pc;
7601 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7602 if (*slot != NULL)
7603 {
7604 complaint (&symfile_complaints,
7605 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7606 "DIE 0x%x [in module %s]"),
7607 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7608 return;
7609 }
7610
7611 /* Count parameters at the caller. */
7612
7613 nparams = 0;
7614 for (child_die = die->child; child_die && child_die->tag;
7615 child_die = sibling_die (child_die))
7616 {
7617 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7618 {
7619 complaint (&symfile_complaints,
7620 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7621 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7622 child_die->tag, child_die->offset.sect_off, objfile->name);
7623 continue;
7624 }
7625
7626 nparams++;
7627 }
7628
7629 call_site = obstack_alloc (&objfile->objfile_obstack,
7630 (sizeof (*call_site)
7631 + (sizeof (*call_site->parameter)
7632 * (nparams - 1))));
7633 *slot = call_site;
7634 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7635 call_site->pc = pc;
7636
7637 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7638 {
7639 struct die_info *func_die;
7640
7641 /* Skip also over DW_TAG_inlined_subroutine. */
7642 for (func_die = die->parent;
7643 func_die && func_die->tag != DW_TAG_subprogram
7644 && func_die->tag != DW_TAG_subroutine_type;
7645 func_die = func_die->parent);
7646
7647 /* DW_AT_GNU_all_call_sites is a superset
7648 of DW_AT_GNU_all_tail_call_sites. */
7649 if (func_die
7650 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7651 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7652 {
7653 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7654 not complete. But keep CALL_SITE for look ups via call_site_htab,
7655 both the initial caller containing the real return address PC and
7656 the final callee containing the current PC of a chain of tail
7657 calls do not need to have the tail call list complete. But any
7658 function candidate for a virtual tail call frame searched via
7659 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7660 determined unambiguously. */
7661 }
7662 else
7663 {
7664 struct type *func_type = NULL;
7665
7666 if (func_die)
7667 func_type = get_die_type (func_die, cu);
7668 if (func_type != NULL)
7669 {
7670 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7671
7672 /* Enlist this call site to the function. */
7673 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7674 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7675 }
7676 else
7677 complaint (&symfile_complaints,
7678 _("Cannot find function owning DW_TAG_GNU_call_site "
7679 "DIE 0x%x [in module %s]"),
7680 die->offset.sect_off, objfile->name);
7681 }
7682 }
7683
7684 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7685 if (attr == NULL)
7686 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7687 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7688 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7689 /* Keep NULL DWARF_BLOCK. */;
7690 else if (attr_form_is_block (attr))
7691 {
7692 struct dwarf2_locexpr_baton *dlbaton;
7693
7694 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7695 dlbaton->data = DW_BLOCK (attr)->data;
7696 dlbaton->size = DW_BLOCK (attr)->size;
7697 dlbaton->per_cu = cu->per_cu;
7698
7699 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7700 }
7701 else if (is_ref_attr (attr))
7702 {
7703 struct dwarf2_cu *target_cu = cu;
7704 struct die_info *target_die;
7705
7706 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7707 gdb_assert (target_cu->objfile == objfile);
7708 if (die_is_declaration (target_die, target_cu))
7709 {
7710 const char *target_physname;
7711
7712 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7713 if (target_physname == NULL)
7714 complaint (&symfile_complaints,
7715 _("DW_AT_GNU_call_site_target target DIE has invalid "
7716 "physname, for referencing DIE 0x%x [in module %s]"),
7717 die->offset.sect_off, objfile->name);
7718 else
7719 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7720 }
7721 else
7722 {
7723 CORE_ADDR lowpc;
7724
7725 /* DW_AT_entry_pc should be preferred. */
7726 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7727 complaint (&symfile_complaints,
7728 _("DW_AT_GNU_call_site_target target DIE has invalid "
7729 "low pc, for referencing DIE 0x%x [in module %s]"),
7730 die->offset.sect_off, objfile->name);
7731 else
7732 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7733 }
7734 }
7735 else
7736 complaint (&symfile_complaints,
7737 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7738 "block nor reference, for DIE 0x%x [in module %s]"),
7739 die->offset.sect_off, objfile->name);
7740
7741 call_site->per_cu = cu->per_cu;
7742
7743 for (child_die = die->child;
7744 child_die && child_die->tag;
7745 child_die = sibling_die (child_die))
7746 {
7747 struct dwarf2_locexpr_baton *dlbaton;
7748 struct call_site_parameter *parameter;
7749
7750 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7751 {
7752 /* Already printed the complaint above. */
7753 continue;
7754 }
7755
7756 gdb_assert (call_site->parameter_count < nparams);
7757 parameter = &call_site->parameter[call_site->parameter_count];
7758
7759 /* DW_AT_location specifies the register number. Value of the data
7760 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7761
7762 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7763 if (!attr || !attr_form_is_block (attr))
7764 {
7765 complaint (&symfile_complaints,
7766 _("No DW_FORM_block* DW_AT_location for "
7767 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7768 child_die->offset.sect_off, objfile->name);
7769 continue;
7770 }
7771 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7772 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7773 if (parameter->dwarf_reg == -1
7774 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7775 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7776 &parameter->fb_offset))
7777 {
7778 complaint (&symfile_complaints,
7779 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7780 "for DW_FORM_block* DW_AT_location for "
7781 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7782 child_die->offset.sect_off, objfile->name);
7783 continue;
7784 }
7785
7786 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7787 if (!attr_form_is_block (attr))
7788 {
7789 complaint (&symfile_complaints,
7790 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7791 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7792 child_die->offset.sect_off, objfile->name);
7793 continue;
7794 }
7795 parameter->value = DW_BLOCK (attr)->data;
7796 parameter->value_size = DW_BLOCK (attr)->size;
7797
7798 /* Parameters are not pre-cleared by memset above. */
7799 parameter->data_value = NULL;
7800 parameter->data_value_size = 0;
7801 call_site->parameter_count++;
7802
7803 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7804 if (attr)
7805 {
7806 if (!attr_form_is_block (attr))
7807 complaint (&symfile_complaints,
7808 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7810 child_die->offset.sect_off, objfile->name);
7811 else
7812 {
7813 parameter->data_value = DW_BLOCK (attr)->data;
7814 parameter->data_value_size = DW_BLOCK (attr)->size;
7815 }
7816 }
7817 }
7818 }
7819
7820 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7821 Return 1 if the attributes are present and valid, otherwise, return 0.
7822 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7823
7824 static int
7825 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7826 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7827 struct partial_symtab *ranges_pst)
7828 {
7829 struct objfile *objfile = cu->objfile;
7830 struct comp_unit_head *cu_header = &cu->header;
7831 bfd *obfd = objfile->obfd;
7832 unsigned int addr_size = cu_header->addr_size;
7833 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7834 /* Base address selection entry. */
7835 CORE_ADDR base;
7836 int found_base;
7837 unsigned int dummy;
7838 gdb_byte *buffer;
7839 CORE_ADDR marker;
7840 int low_set;
7841 CORE_ADDR low = 0;
7842 CORE_ADDR high = 0;
7843 CORE_ADDR baseaddr;
7844
7845 found_base = cu->base_known;
7846 base = cu->base_address;
7847
7848 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7849 if (offset >= dwarf2_per_objfile->ranges.size)
7850 {
7851 complaint (&symfile_complaints,
7852 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7853 offset);
7854 return 0;
7855 }
7856 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7857
7858 /* Read in the largest possible address. */
7859 marker = read_address (obfd, buffer, cu, &dummy);
7860 if ((marker & mask) == mask)
7861 {
7862 /* If we found the largest possible address, then
7863 read the base address. */
7864 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7865 buffer += 2 * addr_size;
7866 offset += 2 * addr_size;
7867 found_base = 1;
7868 }
7869
7870 low_set = 0;
7871
7872 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7873
7874 while (1)
7875 {
7876 CORE_ADDR range_beginning, range_end;
7877
7878 range_beginning = read_address (obfd, buffer, cu, &dummy);
7879 buffer += addr_size;
7880 range_end = read_address (obfd, buffer, cu, &dummy);
7881 buffer += addr_size;
7882 offset += 2 * addr_size;
7883
7884 /* An end of list marker is a pair of zero addresses. */
7885 if (range_beginning == 0 && range_end == 0)
7886 /* Found the end of list entry. */
7887 break;
7888
7889 /* Each base address selection entry is a pair of 2 values.
7890 The first is the largest possible address, the second is
7891 the base address. Check for a base address here. */
7892 if ((range_beginning & mask) == mask)
7893 {
7894 /* If we found the largest possible address, then
7895 read the base address. */
7896 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7897 found_base = 1;
7898 continue;
7899 }
7900
7901 if (!found_base)
7902 {
7903 /* We have no valid base address for the ranges
7904 data. */
7905 complaint (&symfile_complaints,
7906 _("Invalid .debug_ranges data (no base address)"));
7907 return 0;
7908 }
7909
7910 if (range_beginning > range_end)
7911 {
7912 /* Inverted range entries are invalid. */
7913 complaint (&symfile_complaints,
7914 _("Invalid .debug_ranges data (inverted range)"));
7915 return 0;
7916 }
7917
7918 /* Empty range entries have no effect. */
7919 if (range_beginning == range_end)
7920 continue;
7921
7922 range_beginning += base;
7923 range_end += base;
7924
7925 if (ranges_pst != NULL)
7926 addrmap_set_empty (objfile->psymtabs_addrmap,
7927 range_beginning + baseaddr,
7928 range_end - 1 + baseaddr,
7929 ranges_pst);
7930
7931 /* FIXME: This is recording everything as a low-high
7932 segment of consecutive addresses. We should have a
7933 data structure for discontiguous block ranges
7934 instead. */
7935 if (! low_set)
7936 {
7937 low = range_beginning;
7938 high = range_end;
7939 low_set = 1;
7940 }
7941 else
7942 {
7943 if (range_beginning < low)
7944 low = range_beginning;
7945 if (range_end > high)
7946 high = range_end;
7947 }
7948 }
7949
7950 if (! low_set)
7951 /* If the first entry is an end-of-list marker, the range
7952 describes an empty scope, i.e. no instructions. */
7953 return 0;
7954
7955 if (low_return)
7956 *low_return = low;
7957 if (high_return)
7958 *high_return = high;
7959 return 1;
7960 }
7961
7962 /* Get low and high pc attributes from a die. Return 1 if the attributes
7963 are present and valid, otherwise, return 0. Return -1 if the range is
7964 discontinuous, i.e. derived from DW_AT_ranges information. */
7965
7966 static int
7967 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7968 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7969 struct partial_symtab *pst)
7970 {
7971 struct attribute *attr;
7972 struct attribute *attr_high;
7973 CORE_ADDR low = 0;
7974 CORE_ADDR high = 0;
7975 int ret = 0;
7976
7977 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7978 if (attr_high)
7979 {
7980 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7981 if (attr)
7982 {
7983 low = DW_ADDR (attr);
7984 if (attr_high->form == DW_FORM_addr
7985 || attr_high->form == DW_FORM_GNU_addr_index)
7986 high = DW_ADDR (attr_high);
7987 else
7988 high = low + DW_UNSND (attr_high);
7989 }
7990 else
7991 /* Found high w/o low attribute. */
7992 return 0;
7993
7994 /* Found consecutive range of addresses. */
7995 ret = 1;
7996 }
7997 else
7998 {
7999 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8000 if (attr != NULL)
8001 {
8002 /* Value of the DW_AT_ranges attribute is the offset in the
8003 .debug_ranges section. */
8004 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8005 return 0;
8006 /* Found discontinuous range of addresses. */
8007 ret = -1;
8008 }
8009 }
8010
8011 /* read_partial_die has also the strict LOW < HIGH requirement. */
8012 if (high <= low)
8013 return 0;
8014
8015 /* When using the GNU linker, .gnu.linkonce. sections are used to
8016 eliminate duplicate copies of functions and vtables and such.
8017 The linker will arbitrarily choose one and discard the others.
8018 The AT_*_pc values for such functions refer to local labels in
8019 these sections. If the section from that file was discarded, the
8020 labels are not in the output, so the relocs get a value of 0.
8021 If this is a discarded function, mark the pc bounds as invalid,
8022 so that GDB will ignore it. */
8023 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8024 return 0;
8025
8026 *lowpc = low;
8027 if (highpc)
8028 *highpc = high;
8029 return ret;
8030 }
8031
8032 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8033 its low and high PC addresses. Do nothing if these addresses could not
8034 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8035 and HIGHPC to the high address if greater than HIGHPC. */
8036
8037 static void
8038 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8039 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8040 struct dwarf2_cu *cu)
8041 {
8042 CORE_ADDR low, high;
8043 struct die_info *child = die->child;
8044
8045 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8046 {
8047 *lowpc = min (*lowpc, low);
8048 *highpc = max (*highpc, high);
8049 }
8050
8051 /* If the language does not allow nested subprograms (either inside
8052 subprograms or lexical blocks), we're done. */
8053 if (cu->language != language_ada)
8054 return;
8055
8056 /* Check all the children of the given DIE. If it contains nested
8057 subprograms, then check their pc bounds. Likewise, we need to
8058 check lexical blocks as well, as they may also contain subprogram
8059 definitions. */
8060 while (child && child->tag)
8061 {
8062 if (child->tag == DW_TAG_subprogram
8063 || child->tag == DW_TAG_lexical_block)
8064 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8065 child = sibling_die (child);
8066 }
8067 }
8068
8069 /* Get the low and high pc's represented by the scope DIE, and store
8070 them in *LOWPC and *HIGHPC. If the correct values can't be
8071 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8072
8073 static void
8074 get_scope_pc_bounds (struct die_info *die,
8075 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8076 struct dwarf2_cu *cu)
8077 {
8078 CORE_ADDR best_low = (CORE_ADDR) -1;
8079 CORE_ADDR best_high = (CORE_ADDR) 0;
8080 CORE_ADDR current_low, current_high;
8081
8082 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8083 {
8084 best_low = current_low;
8085 best_high = current_high;
8086 }
8087 else
8088 {
8089 struct die_info *child = die->child;
8090
8091 while (child && child->tag)
8092 {
8093 switch (child->tag) {
8094 case DW_TAG_subprogram:
8095 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8096 break;
8097 case DW_TAG_namespace:
8098 case DW_TAG_module:
8099 /* FIXME: carlton/2004-01-16: Should we do this for
8100 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8101 that current GCC's always emit the DIEs corresponding
8102 to definitions of methods of classes as children of a
8103 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8104 the DIEs giving the declarations, which could be
8105 anywhere). But I don't see any reason why the
8106 standards says that they have to be there. */
8107 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8108
8109 if (current_low != ((CORE_ADDR) -1))
8110 {
8111 best_low = min (best_low, current_low);
8112 best_high = max (best_high, current_high);
8113 }
8114 break;
8115 default:
8116 /* Ignore. */
8117 break;
8118 }
8119
8120 child = sibling_die (child);
8121 }
8122 }
8123
8124 *lowpc = best_low;
8125 *highpc = best_high;
8126 }
8127
8128 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8129 in DIE. */
8130
8131 static void
8132 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8133 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8134 {
8135 struct objfile *objfile = cu->objfile;
8136 struct attribute *attr;
8137 struct attribute *attr_high;
8138
8139 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8140 if (attr_high)
8141 {
8142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8143 if (attr)
8144 {
8145 CORE_ADDR low = DW_ADDR (attr);
8146 CORE_ADDR high;
8147 if (attr_high->form == DW_FORM_addr
8148 || attr_high->form == DW_FORM_GNU_addr_index)
8149 high = DW_ADDR (attr_high);
8150 else
8151 high = low + DW_UNSND (attr_high);
8152
8153 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8154 }
8155 }
8156
8157 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8158 if (attr)
8159 {
8160 bfd *obfd = objfile->obfd;
8161
8162 /* The value of the DW_AT_ranges attribute is the offset of the
8163 address range list in the .debug_ranges section. */
8164 unsigned long offset = DW_UNSND (attr);
8165 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8166
8167 /* For some target architectures, but not others, the
8168 read_address function sign-extends the addresses it returns.
8169 To recognize base address selection entries, we need a
8170 mask. */
8171 unsigned int addr_size = cu->header.addr_size;
8172 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8173
8174 /* The base address, to which the next pair is relative. Note
8175 that this 'base' is a DWARF concept: most entries in a range
8176 list are relative, to reduce the number of relocs against the
8177 debugging information. This is separate from this function's
8178 'baseaddr' argument, which GDB uses to relocate debugging
8179 information from a shared library based on the address at
8180 which the library was loaded. */
8181 CORE_ADDR base = cu->base_address;
8182 int base_known = cu->base_known;
8183
8184 gdb_assert (dwarf2_per_objfile->ranges.readin);
8185 if (offset >= dwarf2_per_objfile->ranges.size)
8186 {
8187 complaint (&symfile_complaints,
8188 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8189 offset);
8190 return;
8191 }
8192
8193 for (;;)
8194 {
8195 unsigned int bytes_read;
8196 CORE_ADDR start, end;
8197
8198 start = read_address (obfd, buffer, cu, &bytes_read);
8199 buffer += bytes_read;
8200 end = read_address (obfd, buffer, cu, &bytes_read);
8201 buffer += bytes_read;
8202
8203 /* Did we find the end of the range list? */
8204 if (start == 0 && end == 0)
8205 break;
8206
8207 /* Did we find a base address selection entry? */
8208 else if ((start & base_select_mask) == base_select_mask)
8209 {
8210 base = end;
8211 base_known = 1;
8212 }
8213
8214 /* We found an ordinary address range. */
8215 else
8216 {
8217 if (!base_known)
8218 {
8219 complaint (&symfile_complaints,
8220 _("Invalid .debug_ranges data "
8221 "(no base address)"));
8222 return;
8223 }
8224
8225 if (start > end)
8226 {
8227 /* Inverted range entries are invalid. */
8228 complaint (&symfile_complaints,
8229 _("Invalid .debug_ranges data "
8230 "(inverted range)"));
8231 return;
8232 }
8233
8234 /* Empty range entries have no effect. */
8235 if (start == end)
8236 continue;
8237
8238 record_block_range (block,
8239 baseaddr + base + start,
8240 baseaddr + base + end - 1);
8241 }
8242 }
8243 }
8244 }
8245
8246 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8247 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8248 during 4.6.0 experimental. */
8249
8250 static int
8251 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8252 {
8253 const char *cs;
8254 int major, minor, release;
8255 int result = 0;
8256
8257 if (cu->producer == NULL)
8258 {
8259 /* For unknown compilers expect their behavior is DWARF version
8260 compliant.
8261
8262 GCC started to support .debug_types sections by -gdwarf-4 since
8263 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8264 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8265 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8266 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8267
8268 return 0;
8269 }
8270
8271 if (cu->checked_producer)
8272 return cu->producer_is_gxx_lt_4_6;
8273
8274 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8275
8276 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8277 {
8278 /* For non-GCC compilers expect their behavior is DWARF version
8279 compliant. */
8280 }
8281 else
8282 {
8283 cs = &cu->producer[strlen ("GNU ")];
8284 while (*cs && !isdigit (*cs))
8285 cs++;
8286 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8287 {
8288 /* Not recognized as GCC. */
8289 }
8290 else
8291 result = major < 4 || (major == 4 && minor < 6);
8292 }
8293
8294 cu->checked_producer = 1;
8295 cu->producer_is_gxx_lt_4_6 = result;
8296
8297 return result;
8298 }
8299
8300 /* Return the default accessibility type if it is not overriden by
8301 DW_AT_accessibility. */
8302
8303 static enum dwarf_access_attribute
8304 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8305 {
8306 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8307 {
8308 /* The default DWARF 2 accessibility for members is public, the default
8309 accessibility for inheritance is private. */
8310
8311 if (die->tag != DW_TAG_inheritance)
8312 return DW_ACCESS_public;
8313 else
8314 return DW_ACCESS_private;
8315 }
8316 else
8317 {
8318 /* DWARF 3+ defines the default accessibility a different way. The same
8319 rules apply now for DW_TAG_inheritance as for the members and it only
8320 depends on the container kind. */
8321
8322 if (die->parent->tag == DW_TAG_class_type)
8323 return DW_ACCESS_private;
8324 else
8325 return DW_ACCESS_public;
8326 }
8327 }
8328
8329 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8330 offset. If the attribute was not found return 0, otherwise return
8331 1. If it was found but could not properly be handled, set *OFFSET
8332 to 0. */
8333
8334 static int
8335 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8336 LONGEST *offset)
8337 {
8338 struct attribute *attr;
8339
8340 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8341 if (attr != NULL)
8342 {
8343 *offset = 0;
8344
8345 /* Note that we do not check for a section offset first here.
8346 This is because DW_AT_data_member_location is new in DWARF 4,
8347 so if we see it, we can assume that a constant form is really
8348 a constant and not a section offset. */
8349 if (attr_form_is_constant (attr))
8350 *offset = dwarf2_get_attr_constant_value (attr, 0);
8351 else if (attr_form_is_section_offset (attr))
8352 dwarf2_complex_location_expr_complaint ();
8353 else if (attr_form_is_block (attr))
8354 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8355 else
8356 dwarf2_complex_location_expr_complaint ();
8357
8358 return 1;
8359 }
8360
8361 return 0;
8362 }
8363
8364 /* Add an aggregate field to the field list. */
8365
8366 static void
8367 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8368 struct dwarf2_cu *cu)
8369 {
8370 struct objfile *objfile = cu->objfile;
8371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8372 struct nextfield *new_field;
8373 struct attribute *attr;
8374 struct field *fp;
8375 char *fieldname = "";
8376
8377 /* Allocate a new field list entry and link it in. */
8378 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8379 make_cleanup (xfree, new_field);
8380 memset (new_field, 0, sizeof (struct nextfield));
8381
8382 if (die->tag == DW_TAG_inheritance)
8383 {
8384 new_field->next = fip->baseclasses;
8385 fip->baseclasses = new_field;
8386 }
8387 else
8388 {
8389 new_field->next = fip->fields;
8390 fip->fields = new_field;
8391 }
8392 fip->nfields++;
8393
8394 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8395 if (attr)
8396 new_field->accessibility = DW_UNSND (attr);
8397 else
8398 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8399 if (new_field->accessibility != DW_ACCESS_public)
8400 fip->non_public_fields = 1;
8401
8402 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8403 if (attr)
8404 new_field->virtuality = DW_UNSND (attr);
8405 else
8406 new_field->virtuality = DW_VIRTUALITY_none;
8407
8408 fp = &new_field->field;
8409
8410 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8411 {
8412 LONGEST offset;
8413
8414 /* Data member other than a C++ static data member. */
8415
8416 /* Get type of field. */
8417 fp->type = die_type (die, cu);
8418
8419 SET_FIELD_BITPOS (*fp, 0);
8420
8421 /* Get bit size of field (zero if none). */
8422 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8423 if (attr)
8424 {
8425 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8426 }
8427 else
8428 {
8429 FIELD_BITSIZE (*fp) = 0;
8430 }
8431
8432 /* Get bit offset of field. */
8433 if (handle_data_member_location (die, cu, &offset))
8434 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8435 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8436 if (attr)
8437 {
8438 if (gdbarch_bits_big_endian (gdbarch))
8439 {
8440 /* For big endian bits, the DW_AT_bit_offset gives the
8441 additional bit offset from the MSB of the containing
8442 anonymous object to the MSB of the field. We don't
8443 have to do anything special since we don't need to
8444 know the size of the anonymous object. */
8445 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8446 }
8447 else
8448 {
8449 /* For little endian bits, compute the bit offset to the
8450 MSB of the anonymous object, subtract off the number of
8451 bits from the MSB of the field to the MSB of the
8452 object, and then subtract off the number of bits of
8453 the field itself. The result is the bit offset of
8454 the LSB of the field. */
8455 int anonymous_size;
8456 int bit_offset = DW_UNSND (attr);
8457
8458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8459 if (attr)
8460 {
8461 /* The size of the anonymous object containing
8462 the bit field is explicit, so use the
8463 indicated size (in bytes). */
8464 anonymous_size = DW_UNSND (attr);
8465 }
8466 else
8467 {
8468 /* The size of the anonymous object containing
8469 the bit field must be inferred from the type
8470 attribute of the data member containing the
8471 bit field. */
8472 anonymous_size = TYPE_LENGTH (fp->type);
8473 }
8474 SET_FIELD_BITPOS (*fp,
8475 (FIELD_BITPOS (*fp)
8476 + anonymous_size * bits_per_byte
8477 - bit_offset - FIELD_BITSIZE (*fp)));
8478 }
8479 }
8480
8481 /* Get name of field. */
8482 fieldname = dwarf2_name (die, cu);
8483 if (fieldname == NULL)
8484 fieldname = "";
8485
8486 /* The name is already allocated along with this objfile, so we don't
8487 need to duplicate it for the type. */
8488 fp->name = fieldname;
8489
8490 /* Change accessibility for artificial fields (e.g. virtual table
8491 pointer or virtual base class pointer) to private. */
8492 if (dwarf2_attr (die, DW_AT_artificial, cu))
8493 {
8494 FIELD_ARTIFICIAL (*fp) = 1;
8495 new_field->accessibility = DW_ACCESS_private;
8496 fip->non_public_fields = 1;
8497 }
8498 }
8499 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8500 {
8501 /* C++ static member. */
8502
8503 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8504 is a declaration, but all versions of G++ as of this writing
8505 (so through at least 3.2.1) incorrectly generate
8506 DW_TAG_variable tags. */
8507
8508 const char *physname;
8509
8510 /* Get name of field. */
8511 fieldname = dwarf2_name (die, cu);
8512 if (fieldname == NULL)
8513 return;
8514
8515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8516 if (attr
8517 /* Only create a symbol if this is an external value.
8518 new_symbol checks this and puts the value in the global symbol
8519 table, which we want. If it is not external, new_symbol
8520 will try to put the value in cu->list_in_scope which is wrong. */
8521 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8522 {
8523 /* A static const member, not much different than an enum as far as
8524 we're concerned, except that we can support more types. */
8525 new_symbol (die, NULL, cu);
8526 }
8527
8528 /* Get physical name. */
8529 physname = dwarf2_physname (fieldname, die, cu);
8530
8531 /* The name is already allocated along with this objfile, so we don't
8532 need to duplicate it for the type. */
8533 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8534 FIELD_TYPE (*fp) = die_type (die, cu);
8535 FIELD_NAME (*fp) = fieldname;
8536 }
8537 else if (die->tag == DW_TAG_inheritance)
8538 {
8539 LONGEST offset;
8540
8541 /* C++ base class field. */
8542 if (handle_data_member_location (die, cu, &offset))
8543 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8544 FIELD_BITSIZE (*fp) = 0;
8545 FIELD_TYPE (*fp) = die_type (die, cu);
8546 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8547 fip->nbaseclasses++;
8548 }
8549 }
8550
8551 /* Add a typedef defined in the scope of the FIP's class. */
8552
8553 static void
8554 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8555 struct dwarf2_cu *cu)
8556 {
8557 struct objfile *objfile = cu->objfile;
8558 struct typedef_field_list *new_field;
8559 struct attribute *attr;
8560 struct typedef_field *fp;
8561 char *fieldname = "";
8562
8563 /* Allocate a new field list entry and link it in. */
8564 new_field = xzalloc (sizeof (*new_field));
8565 make_cleanup (xfree, new_field);
8566
8567 gdb_assert (die->tag == DW_TAG_typedef);
8568
8569 fp = &new_field->field;
8570
8571 /* Get name of field. */
8572 fp->name = dwarf2_name (die, cu);
8573 if (fp->name == NULL)
8574 return;
8575
8576 fp->type = read_type_die (die, cu);
8577
8578 new_field->next = fip->typedef_field_list;
8579 fip->typedef_field_list = new_field;
8580 fip->typedef_field_list_count++;
8581 }
8582
8583 /* Create the vector of fields, and attach it to the type. */
8584
8585 static void
8586 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8587 struct dwarf2_cu *cu)
8588 {
8589 int nfields = fip->nfields;
8590
8591 /* Record the field count, allocate space for the array of fields,
8592 and create blank accessibility bitfields if necessary. */
8593 TYPE_NFIELDS (type) = nfields;
8594 TYPE_FIELDS (type) = (struct field *)
8595 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8596 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8597
8598 if (fip->non_public_fields && cu->language != language_ada)
8599 {
8600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8601
8602 TYPE_FIELD_PRIVATE_BITS (type) =
8603 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8604 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8605
8606 TYPE_FIELD_PROTECTED_BITS (type) =
8607 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8608 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8609
8610 TYPE_FIELD_IGNORE_BITS (type) =
8611 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8612 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8613 }
8614
8615 /* If the type has baseclasses, allocate and clear a bit vector for
8616 TYPE_FIELD_VIRTUAL_BITS. */
8617 if (fip->nbaseclasses && cu->language != language_ada)
8618 {
8619 int num_bytes = B_BYTES (fip->nbaseclasses);
8620 unsigned char *pointer;
8621
8622 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8623 pointer = TYPE_ALLOC (type, num_bytes);
8624 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8625 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8626 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8627 }
8628
8629 /* Copy the saved-up fields into the field vector. Start from the head of
8630 the list, adding to the tail of the field array, so that they end up in
8631 the same order in the array in which they were added to the list. */
8632 while (nfields-- > 0)
8633 {
8634 struct nextfield *fieldp;
8635
8636 if (fip->fields)
8637 {
8638 fieldp = fip->fields;
8639 fip->fields = fieldp->next;
8640 }
8641 else
8642 {
8643 fieldp = fip->baseclasses;
8644 fip->baseclasses = fieldp->next;
8645 }
8646
8647 TYPE_FIELD (type, nfields) = fieldp->field;
8648 switch (fieldp->accessibility)
8649 {
8650 case DW_ACCESS_private:
8651 if (cu->language != language_ada)
8652 SET_TYPE_FIELD_PRIVATE (type, nfields);
8653 break;
8654
8655 case DW_ACCESS_protected:
8656 if (cu->language != language_ada)
8657 SET_TYPE_FIELD_PROTECTED (type, nfields);
8658 break;
8659
8660 case DW_ACCESS_public:
8661 break;
8662
8663 default:
8664 /* Unknown accessibility. Complain and treat it as public. */
8665 {
8666 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8667 fieldp->accessibility);
8668 }
8669 break;
8670 }
8671 if (nfields < fip->nbaseclasses)
8672 {
8673 switch (fieldp->virtuality)
8674 {
8675 case DW_VIRTUALITY_virtual:
8676 case DW_VIRTUALITY_pure_virtual:
8677 if (cu->language == language_ada)
8678 error (_("unexpected virtuality in component of Ada type"));
8679 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8680 break;
8681 }
8682 }
8683 }
8684 }
8685
8686 /* Add a member function to the proper fieldlist. */
8687
8688 static void
8689 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8690 struct type *type, struct dwarf2_cu *cu)
8691 {
8692 struct objfile *objfile = cu->objfile;
8693 struct attribute *attr;
8694 struct fnfieldlist *flp;
8695 int i;
8696 struct fn_field *fnp;
8697 char *fieldname;
8698 struct nextfnfield *new_fnfield;
8699 struct type *this_type;
8700 enum dwarf_access_attribute accessibility;
8701
8702 if (cu->language == language_ada)
8703 error (_("unexpected member function in Ada type"));
8704
8705 /* Get name of member function. */
8706 fieldname = dwarf2_name (die, cu);
8707 if (fieldname == NULL)
8708 return;
8709
8710 /* Look up member function name in fieldlist. */
8711 for (i = 0; i < fip->nfnfields; i++)
8712 {
8713 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8714 break;
8715 }
8716
8717 /* Create new list element if necessary. */
8718 if (i < fip->nfnfields)
8719 flp = &fip->fnfieldlists[i];
8720 else
8721 {
8722 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8723 {
8724 fip->fnfieldlists = (struct fnfieldlist *)
8725 xrealloc (fip->fnfieldlists,
8726 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8727 * sizeof (struct fnfieldlist));
8728 if (fip->nfnfields == 0)
8729 make_cleanup (free_current_contents, &fip->fnfieldlists);
8730 }
8731 flp = &fip->fnfieldlists[fip->nfnfields];
8732 flp->name = fieldname;
8733 flp->length = 0;
8734 flp->head = NULL;
8735 i = fip->nfnfields++;
8736 }
8737
8738 /* Create a new member function field and chain it to the field list
8739 entry. */
8740 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8741 make_cleanup (xfree, new_fnfield);
8742 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8743 new_fnfield->next = flp->head;
8744 flp->head = new_fnfield;
8745 flp->length++;
8746
8747 /* Fill in the member function field info. */
8748 fnp = &new_fnfield->fnfield;
8749
8750 /* Delay processing of the physname until later. */
8751 if (cu->language == language_cplus || cu->language == language_java)
8752 {
8753 add_to_method_list (type, i, flp->length - 1, fieldname,
8754 die, cu);
8755 }
8756 else
8757 {
8758 const char *physname = dwarf2_physname (fieldname, die, cu);
8759 fnp->physname = physname ? physname : "";
8760 }
8761
8762 fnp->type = alloc_type (objfile);
8763 this_type = read_type_die (die, cu);
8764 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8765 {
8766 int nparams = TYPE_NFIELDS (this_type);
8767
8768 /* TYPE is the domain of this method, and THIS_TYPE is the type
8769 of the method itself (TYPE_CODE_METHOD). */
8770 smash_to_method_type (fnp->type, type,
8771 TYPE_TARGET_TYPE (this_type),
8772 TYPE_FIELDS (this_type),
8773 TYPE_NFIELDS (this_type),
8774 TYPE_VARARGS (this_type));
8775
8776 /* Handle static member functions.
8777 Dwarf2 has no clean way to discern C++ static and non-static
8778 member functions. G++ helps GDB by marking the first
8779 parameter for non-static member functions (which is the this
8780 pointer) as artificial. We obtain this information from
8781 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8782 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8783 fnp->voffset = VOFFSET_STATIC;
8784 }
8785 else
8786 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8787 dwarf2_full_name (fieldname, die, cu));
8788
8789 /* Get fcontext from DW_AT_containing_type if present. */
8790 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8791 fnp->fcontext = die_containing_type (die, cu);
8792
8793 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8794 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8795
8796 /* Get accessibility. */
8797 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8798 if (attr)
8799 accessibility = DW_UNSND (attr);
8800 else
8801 accessibility = dwarf2_default_access_attribute (die, cu);
8802 switch (accessibility)
8803 {
8804 case DW_ACCESS_private:
8805 fnp->is_private = 1;
8806 break;
8807 case DW_ACCESS_protected:
8808 fnp->is_protected = 1;
8809 break;
8810 }
8811
8812 /* Check for artificial methods. */
8813 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8814 if (attr && DW_UNSND (attr) != 0)
8815 fnp->is_artificial = 1;
8816
8817 /* Get index in virtual function table if it is a virtual member
8818 function. For older versions of GCC, this is an offset in the
8819 appropriate virtual table, as specified by DW_AT_containing_type.
8820 For everyone else, it is an expression to be evaluated relative
8821 to the object address. */
8822
8823 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8824 if (attr)
8825 {
8826 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8827 {
8828 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8829 {
8830 /* Old-style GCC. */
8831 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8832 }
8833 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8834 || (DW_BLOCK (attr)->size > 1
8835 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8836 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8837 {
8838 struct dwarf_block blk;
8839 int offset;
8840
8841 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8842 ? 1 : 2);
8843 blk.size = DW_BLOCK (attr)->size - offset;
8844 blk.data = DW_BLOCK (attr)->data + offset;
8845 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8846 if ((fnp->voffset % cu->header.addr_size) != 0)
8847 dwarf2_complex_location_expr_complaint ();
8848 else
8849 fnp->voffset /= cu->header.addr_size;
8850 fnp->voffset += 2;
8851 }
8852 else
8853 dwarf2_complex_location_expr_complaint ();
8854
8855 if (!fnp->fcontext)
8856 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8857 }
8858 else if (attr_form_is_section_offset (attr))
8859 {
8860 dwarf2_complex_location_expr_complaint ();
8861 }
8862 else
8863 {
8864 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8865 fieldname);
8866 }
8867 }
8868 else
8869 {
8870 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8871 if (attr && DW_UNSND (attr))
8872 {
8873 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8874 complaint (&symfile_complaints,
8875 _("Member function \"%s\" (offset %d) is virtual "
8876 "but the vtable offset is not specified"),
8877 fieldname, die->offset.sect_off);
8878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8879 TYPE_CPLUS_DYNAMIC (type) = 1;
8880 }
8881 }
8882 }
8883
8884 /* Create the vector of member function fields, and attach it to the type. */
8885
8886 static void
8887 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8888 struct dwarf2_cu *cu)
8889 {
8890 struct fnfieldlist *flp;
8891 int i;
8892
8893 if (cu->language == language_ada)
8894 error (_("unexpected member functions in Ada type"));
8895
8896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8897 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8898 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8899
8900 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8901 {
8902 struct nextfnfield *nfp = flp->head;
8903 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8904 int k;
8905
8906 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8907 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8908 fn_flp->fn_fields = (struct fn_field *)
8909 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8910 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8911 fn_flp->fn_fields[k] = nfp->fnfield;
8912 }
8913
8914 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8915 }
8916
8917 /* Returns non-zero if NAME is the name of a vtable member in CU's
8918 language, zero otherwise. */
8919 static int
8920 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8921 {
8922 static const char vptr[] = "_vptr";
8923 static const char vtable[] = "vtable";
8924
8925 /* Look for the C++ and Java forms of the vtable. */
8926 if ((cu->language == language_java
8927 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8928 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8929 && is_cplus_marker (name[sizeof (vptr) - 1])))
8930 return 1;
8931
8932 return 0;
8933 }
8934
8935 /* GCC outputs unnamed structures that are really pointers to member
8936 functions, with the ABI-specified layout. If TYPE describes
8937 such a structure, smash it into a member function type.
8938
8939 GCC shouldn't do this; it should just output pointer to member DIEs.
8940 This is GCC PR debug/28767. */
8941
8942 static void
8943 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8944 {
8945 struct type *pfn_type, *domain_type, *new_type;
8946
8947 /* Check for a structure with no name and two children. */
8948 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8949 return;
8950
8951 /* Check for __pfn and __delta members. */
8952 if (TYPE_FIELD_NAME (type, 0) == NULL
8953 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8954 || TYPE_FIELD_NAME (type, 1) == NULL
8955 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8956 return;
8957
8958 /* Find the type of the method. */
8959 pfn_type = TYPE_FIELD_TYPE (type, 0);
8960 if (pfn_type == NULL
8961 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8962 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8963 return;
8964
8965 /* Look for the "this" argument. */
8966 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8967 if (TYPE_NFIELDS (pfn_type) == 0
8968 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8969 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8970 return;
8971
8972 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
8973 new_type = alloc_type (objfile);
8974 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
8975 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8976 TYPE_VARARGS (pfn_type));
8977 smash_to_methodptr_type (type, new_type);
8978 }
8979
8980 /* Called when we find the DIE that starts a structure or union scope
8981 (definition) to create a type for the structure or union. Fill in
8982 the type's name and general properties; the members will not be
8983 processed until process_structure_type.
8984
8985 NOTE: we need to call these functions regardless of whether or not the
8986 DIE has a DW_AT_name attribute, since it might be an anonymous
8987 structure or union. This gets the type entered into our set of
8988 user defined types.
8989
8990 However, if the structure is incomplete (an opaque struct/union)
8991 then suppress creating a symbol table entry for it since gdb only
8992 wants to find the one with the complete definition. Note that if
8993 it is complete, we just call new_symbol, which does it's own
8994 checking about whether the struct/union is anonymous or not (and
8995 suppresses creating a symbol table entry itself). */
8996
8997 static struct type *
8998 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
8999 {
9000 struct objfile *objfile = cu->objfile;
9001 struct type *type;
9002 struct attribute *attr;
9003 char *name;
9004
9005 /* If the definition of this type lives in .debug_types, read that type.
9006 Don't follow DW_AT_specification though, that will take us back up
9007 the chain and we want to go down. */
9008 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9009 if (attr)
9010 {
9011 struct dwarf2_cu *type_cu = cu;
9012 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9013
9014 /* We could just recurse on read_structure_type, but we need to call
9015 get_die_type to ensure only one type for this DIE is created.
9016 This is important, for example, because for c++ classes we need
9017 TYPE_NAME set which is only done by new_symbol. Blech. */
9018 type = read_type_die (type_die, type_cu);
9019
9020 /* TYPE_CU may not be the same as CU.
9021 Ensure TYPE is recorded in CU's type_hash table. */
9022 return set_die_type (die, type, cu);
9023 }
9024
9025 type = alloc_type (objfile);
9026 INIT_CPLUS_SPECIFIC (type);
9027
9028 name = dwarf2_name (die, cu);
9029 if (name != NULL)
9030 {
9031 if (cu->language == language_cplus
9032 || cu->language == language_java)
9033 {
9034 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9035
9036 /* dwarf2_full_name might have already finished building the DIE's
9037 type. If so, there is no need to continue. */
9038 if (get_die_type (die, cu) != NULL)
9039 return get_die_type (die, cu);
9040
9041 TYPE_TAG_NAME (type) = full_name;
9042 if (die->tag == DW_TAG_structure_type
9043 || die->tag == DW_TAG_class_type)
9044 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9045 }
9046 else
9047 {
9048 /* The name is already allocated along with this objfile, so
9049 we don't need to duplicate it for the type. */
9050 TYPE_TAG_NAME (type) = (char *) name;
9051 if (die->tag == DW_TAG_class_type)
9052 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9053 }
9054 }
9055
9056 if (die->tag == DW_TAG_structure_type)
9057 {
9058 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9059 }
9060 else if (die->tag == DW_TAG_union_type)
9061 {
9062 TYPE_CODE (type) = TYPE_CODE_UNION;
9063 }
9064 else
9065 {
9066 TYPE_CODE (type) = TYPE_CODE_CLASS;
9067 }
9068
9069 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9070 TYPE_DECLARED_CLASS (type) = 1;
9071
9072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9073 if (attr)
9074 {
9075 TYPE_LENGTH (type) = DW_UNSND (attr);
9076 }
9077 else
9078 {
9079 TYPE_LENGTH (type) = 0;
9080 }
9081
9082 TYPE_STUB_SUPPORTED (type) = 1;
9083 if (die_is_declaration (die, cu))
9084 TYPE_STUB (type) = 1;
9085 else if (attr == NULL && die->child == NULL
9086 && producer_is_realview (cu->producer))
9087 /* RealView does not output the required DW_AT_declaration
9088 on incomplete types. */
9089 TYPE_STUB (type) = 1;
9090
9091 /* We need to add the type field to the die immediately so we don't
9092 infinitely recurse when dealing with pointers to the structure
9093 type within the structure itself. */
9094 set_die_type (die, type, cu);
9095
9096 /* set_die_type should be already done. */
9097 set_descriptive_type (type, die, cu);
9098
9099 return type;
9100 }
9101
9102 /* Finish creating a structure or union type, including filling in
9103 its members and creating a symbol for it. */
9104
9105 static void
9106 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9107 {
9108 struct objfile *objfile = cu->objfile;
9109 struct die_info *child_die = die->child;
9110 struct type *type;
9111
9112 type = get_die_type (die, cu);
9113 if (type == NULL)
9114 type = read_structure_type (die, cu);
9115
9116 if (die->child != NULL && ! die_is_declaration (die, cu))
9117 {
9118 struct field_info fi;
9119 struct die_info *child_die;
9120 VEC (symbolp) *template_args = NULL;
9121 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9122
9123 memset (&fi, 0, sizeof (struct field_info));
9124
9125 child_die = die->child;
9126
9127 while (child_die && child_die->tag)
9128 {
9129 if (child_die->tag == DW_TAG_member
9130 || child_die->tag == DW_TAG_variable)
9131 {
9132 /* NOTE: carlton/2002-11-05: A C++ static data member
9133 should be a DW_TAG_member that is a declaration, but
9134 all versions of G++ as of this writing (so through at
9135 least 3.2.1) incorrectly generate DW_TAG_variable
9136 tags for them instead. */
9137 dwarf2_add_field (&fi, child_die, cu);
9138 }
9139 else if (child_die->tag == DW_TAG_subprogram)
9140 {
9141 /* C++ member function. */
9142 dwarf2_add_member_fn (&fi, child_die, type, cu);
9143 }
9144 else if (child_die->tag == DW_TAG_inheritance)
9145 {
9146 /* C++ base class field. */
9147 dwarf2_add_field (&fi, child_die, cu);
9148 }
9149 else if (child_die->tag == DW_TAG_typedef)
9150 dwarf2_add_typedef (&fi, child_die, cu);
9151 else if (child_die->tag == DW_TAG_template_type_param
9152 || child_die->tag == DW_TAG_template_value_param)
9153 {
9154 struct symbol *arg = new_symbol (child_die, NULL, cu);
9155
9156 if (arg != NULL)
9157 VEC_safe_push (symbolp, template_args, arg);
9158 }
9159
9160 child_die = sibling_die (child_die);
9161 }
9162
9163 /* Attach template arguments to type. */
9164 if (! VEC_empty (symbolp, template_args))
9165 {
9166 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9167 TYPE_N_TEMPLATE_ARGUMENTS (type)
9168 = VEC_length (symbolp, template_args);
9169 TYPE_TEMPLATE_ARGUMENTS (type)
9170 = obstack_alloc (&objfile->objfile_obstack,
9171 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9172 * sizeof (struct symbol *)));
9173 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9174 VEC_address (symbolp, template_args),
9175 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9176 * sizeof (struct symbol *)));
9177 VEC_free (symbolp, template_args);
9178 }
9179
9180 /* Attach fields and member functions to the type. */
9181 if (fi.nfields)
9182 dwarf2_attach_fields_to_type (&fi, type, cu);
9183 if (fi.nfnfields)
9184 {
9185 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9186
9187 /* Get the type which refers to the base class (possibly this
9188 class itself) which contains the vtable pointer for the current
9189 class from the DW_AT_containing_type attribute. This use of
9190 DW_AT_containing_type is a GNU extension. */
9191
9192 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9193 {
9194 struct type *t = die_containing_type (die, cu);
9195
9196 TYPE_VPTR_BASETYPE (type) = t;
9197 if (type == t)
9198 {
9199 int i;
9200
9201 /* Our own class provides vtbl ptr. */
9202 for (i = TYPE_NFIELDS (t) - 1;
9203 i >= TYPE_N_BASECLASSES (t);
9204 --i)
9205 {
9206 const char *fieldname = TYPE_FIELD_NAME (t, i);
9207
9208 if (is_vtable_name (fieldname, cu))
9209 {
9210 TYPE_VPTR_FIELDNO (type) = i;
9211 break;
9212 }
9213 }
9214
9215 /* Complain if virtual function table field not found. */
9216 if (i < TYPE_N_BASECLASSES (t))
9217 complaint (&symfile_complaints,
9218 _("virtual function table pointer "
9219 "not found when defining class '%s'"),
9220 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9221 "");
9222 }
9223 else
9224 {
9225 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9226 }
9227 }
9228 else if (cu->producer
9229 && strncmp (cu->producer,
9230 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9231 {
9232 /* The IBM XLC compiler does not provide direct indication
9233 of the containing type, but the vtable pointer is
9234 always named __vfp. */
9235
9236 int i;
9237
9238 for (i = TYPE_NFIELDS (type) - 1;
9239 i >= TYPE_N_BASECLASSES (type);
9240 --i)
9241 {
9242 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9243 {
9244 TYPE_VPTR_FIELDNO (type) = i;
9245 TYPE_VPTR_BASETYPE (type) = type;
9246 break;
9247 }
9248 }
9249 }
9250 }
9251
9252 /* Copy fi.typedef_field_list linked list elements content into the
9253 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9254 if (fi.typedef_field_list)
9255 {
9256 int i = fi.typedef_field_list_count;
9257
9258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9259 TYPE_TYPEDEF_FIELD_ARRAY (type)
9260 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9261 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9262
9263 /* Reverse the list order to keep the debug info elements order. */
9264 while (--i >= 0)
9265 {
9266 struct typedef_field *dest, *src;
9267
9268 dest = &TYPE_TYPEDEF_FIELD (type, i);
9269 src = &fi.typedef_field_list->field;
9270 fi.typedef_field_list = fi.typedef_field_list->next;
9271 *dest = *src;
9272 }
9273 }
9274
9275 do_cleanups (back_to);
9276
9277 if (HAVE_CPLUS_STRUCT (type))
9278 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9279 }
9280
9281 quirk_gcc_member_function_pointer (type, objfile);
9282
9283 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9284 snapshots) has been known to create a die giving a declaration
9285 for a class that has, as a child, a die giving a definition for a
9286 nested class. So we have to process our children even if the
9287 current die is a declaration. Normally, of course, a declaration
9288 won't have any children at all. */
9289
9290 while (child_die != NULL && child_die->tag)
9291 {
9292 if (child_die->tag == DW_TAG_member
9293 || child_die->tag == DW_TAG_variable
9294 || child_die->tag == DW_TAG_inheritance
9295 || child_die->tag == DW_TAG_template_value_param
9296 || child_die->tag == DW_TAG_template_type_param)
9297 {
9298 /* Do nothing. */
9299 }
9300 else
9301 process_die (child_die, cu);
9302
9303 child_die = sibling_die (child_die);
9304 }
9305
9306 /* Do not consider external references. According to the DWARF standard,
9307 these DIEs are identified by the fact that they have no byte_size
9308 attribute, and a declaration attribute. */
9309 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9310 || !die_is_declaration (die, cu))
9311 new_symbol (die, type, cu);
9312 }
9313
9314 /* Given a DW_AT_enumeration_type die, set its type. We do not
9315 complete the type's fields yet, or create any symbols. */
9316
9317 static struct type *
9318 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9319 {
9320 struct objfile *objfile = cu->objfile;
9321 struct type *type;
9322 struct attribute *attr;
9323 const char *name;
9324
9325 /* If the definition of this type lives in .debug_types, read that type.
9326 Don't follow DW_AT_specification though, that will take us back up
9327 the chain and we want to go down. */
9328 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9329 if (attr)
9330 {
9331 struct dwarf2_cu *type_cu = cu;
9332 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9333
9334 type = read_type_die (type_die, type_cu);
9335
9336 /* TYPE_CU may not be the same as CU.
9337 Ensure TYPE is recorded in CU's type_hash table. */
9338 return set_die_type (die, type, cu);
9339 }
9340
9341 type = alloc_type (objfile);
9342
9343 TYPE_CODE (type) = TYPE_CODE_ENUM;
9344 name = dwarf2_full_name (NULL, die, cu);
9345 if (name != NULL)
9346 TYPE_TAG_NAME (type) = (char *) name;
9347
9348 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9349 if (attr)
9350 {
9351 TYPE_LENGTH (type) = DW_UNSND (attr);
9352 }
9353 else
9354 {
9355 TYPE_LENGTH (type) = 0;
9356 }
9357
9358 /* The enumeration DIE can be incomplete. In Ada, any type can be
9359 declared as private in the package spec, and then defined only
9360 inside the package body. Such types are known as Taft Amendment
9361 Types. When another package uses such a type, an incomplete DIE
9362 may be generated by the compiler. */
9363 if (die_is_declaration (die, cu))
9364 TYPE_STUB (type) = 1;
9365
9366 return set_die_type (die, type, cu);
9367 }
9368
9369 /* Given a pointer to a die which begins an enumeration, process all
9370 the dies that define the members of the enumeration, and create the
9371 symbol for the enumeration type.
9372
9373 NOTE: We reverse the order of the element list. */
9374
9375 static void
9376 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9377 {
9378 struct type *this_type;
9379
9380 this_type = get_die_type (die, cu);
9381 if (this_type == NULL)
9382 this_type = read_enumeration_type (die, cu);
9383
9384 if (die->child != NULL)
9385 {
9386 struct die_info *child_die;
9387 struct symbol *sym;
9388 struct field *fields = NULL;
9389 int num_fields = 0;
9390 int unsigned_enum = 1;
9391 char *name;
9392 int flag_enum = 1;
9393 ULONGEST mask = 0;
9394
9395 child_die = die->child;
9396 while (child_die && child_die->tag)
9397 {
9398 if (child_die->tag != DW_TAG_enumerator)
9399 {
9400 process_die (child_die, cu);
9401 }
9402 else
9403 {
9404 name = dwarf2_name (child_die, cu);
9405 if (name)
9406 {
9407 sym = new_symbol (child_die, this_type, cu);
9408 if (SYMBOL_VALUE (sym) < 0)
9409 {
9410 unsigned_enum = 0;
9411 flag_enum = 0;
9412 }
9413 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9414 flag_enum = 0;
9415 else
9416 mask |= SYMBOL_VALUE (sym);
9417
9418 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9419 {
9420 fields = (struct field *)
9421 xrealloc (fields,
9422 (num_fields + DW_FIELD_ALLOC_CHUNK)
9423 * sizeof (struct field));
9424 }
9425
9426 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9427 FIELD_TYPE (fields[num_fields]) = NULL;
9428 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9429 FIELD_BITSIZE (fields[num_fields]) = 0;
9430
9431 num_fields++;
9432 }
9433 }
9434
9435 child_die = sibling_die (child_die);
9436 }
9437
9438 if (num_fields)
9439 {
9440 TYPE_NFIELDS (this_type) = num_fields;
9441 TYPE_FIELDS (this_type) = (struct field *)
9442 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9443 memcpy (TYPE_FIELDS (this_type), fields,
9444 sizeof (struct field) * num_fields);
9445 xfree (fields);
9446 }
9447 if (unsigned_enum)
9448 TYPE_UNSIGNED (this_type) = 1;
9449 if (flag_enum)
9450 TYPE_FLAG_ENUM (this_type) = 1;
9451 }
9452
9453 /* If we are reading an enum from a .debug_types unit, and the enum
9454 is a declaration, and the enum is not the signatured type in the
9455 unit, then we do not want to add a symbol for it. Adding a
9456 symbol would in some cases obscure the true definition of the
9457 enum, giving users an incomplete type when the definition is
9458 actually available. Note that we do not want to do this for all
9459 enums which are just declarations, because C++0x allows forward
9460 enum declarations. */
9461 if (cu->per_cu->is_debug_types
9462 && die_is_declaration (die, cu))
9463 {
9464 struct signatured_type *sig_type;
9465
9466 sig_type
9467 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9468 cu->per_cu->info_or_types_section,
9469 cu->per_cu->offset);
9470 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9471 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9472 return;
9473 }
9474
9475 new_symbol (die, this_type, cu);
9476 }
9477
9478 /* Extract all information from a DW_TAG_array_type DIE and put it in
9479 the DIE's type field. For now, this only handles one dimensional
9480 arrays. */
9481
9482 static struct type *
9483 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9484 {
9485 struct objfile *objfile = cu->objfile;
9486 struct die_info *child_die;
9487 struct type *type;
9488 struct type *element_type, *range_type, *index_type;
9489 struct type **range_types = NULL;
9490 struct attribute *attr;
9491 int ndim = 0;
9492 struct cleanup *back_to;
9493 char *name;
9494
9495 element_type = die_type (die, cu);
9496
9497 /* The die_type call above may have already set the type for this DIE. */
9498 type = get_die_type (die, cu);
9499 if (type)
9500 return type;
9501
9502 /* Irix 6.2 native cc creates array types without children for
9503 arrays with unspecified length. */
9504 if (die->child == NULL)
9505 {
9506 index_type = objfile_type (objfile)->builtin_int;
9507 range_type = create_range_type (NULL, index_type, 0, -1);
9508 type = create_array_type (NULL, element_type, range_type);
9509 return set_die_type (die, type, cu);
9510 }
9511
9512 back_to = make_cleanup (null_cleanup, NULL);
9513 child_die = die->child;
9514 while (child_die && child_die->tag)
9515 {
9516 if (child_die->tag == DW_TAG_subrange_type)
9517 {
9518 struct type *child_type = read_type_die (child_die, cu);
9519
9520 if (child_type != NULL)
9521 {
9522 /* The range type was succesfully read. Save it for the
9523 array type creation. */
9524 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9525 {
9526 range_types = (struct type **)
9527 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9528 * sizeof (struct type *));
9529 if (ndim == 0)
9530 make_cleanup (free_current_contents, &range_types);
9531 }
9532 range_types[ndim++] = child_type;
9533 }
9534 }
9535 child_die = sibling_die (child_die);
9536 }
9537
9538 /* Dwarf2 dimensions are output from left to right, create the
9539 necessary array types in backwards order. */
9540
9541 type = element_type;
9542
9543 if (read_array_order (die, cu) == DW_ORD_col_major)
9544 {
9545 int i = 0;
9546
9547 while (i < ndim)
9548 type = create_array_type (NULL, type, range_types[i++]);
9549 }
9550 else
9551 {
9552 while (ndim-- > 0)
9553 type = create_array_type (NULL, type, range_types[ndim]);
9554 }
9555
9556 /* Understand Dwarf2 support for vector types (like they occur on
9557 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9558 array type. This is not part of the Dwarf2/3 standard yet, but a
9559 custom vendor extension. The main difference between a regular
9560 array and the vector variant is that vectors are passed by value
9561 to functions. */
9562 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9563 if (attr)
9564 make_vector_type (type);
9565
9566 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9567 implementation may choose to implement triple vectors using this
9568 attribute. */
9569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9570 if (attr)
9571 {
9572 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9573 TYPE_LENGTH (type) = DW_UNSND (attr);
9574 else
9575 complaint (&symfile_complaints,
9576 _("DW_AT_byte_size for array type smaller "
9577 "than the total size of elements"));
9578 }
9579
9580 name = dwarf2_name (die, cu);
9581 if (name)
9582 TYPE_NAME (type) = name;
9583
9584 /* Install the type in the die. */
9585 set_die_type (die, type, cu);
9586
9587 /* set_die_type should be already done. */
9588 set_descriptive_type (type, die, cu);
9589
9590 do_cleanups (back_to);
9591
9592 return type;
9593 }
9594
9595 static enum dwarf_array_dim_ordering
9596 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9597 {
9598 struct attribute *attr;
9599
9600 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9601
9602 if (attr) return DW_SND (attr);
9603
9604 /* GNU F77 is a special case, as at 08/2004 array type info is the
9605 opposite order to the dwarf2 specification, but data is still
9606 laid out as per normal fortran.
9607
9608 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9609 version checking. */
9610
9611 if (cu->language == language_fortran
9612 && cu->producer && strstr (cu->producer, "GNU F77"))
9613 {
9614 return DW_ORD_row_major;
9615 }
9616
9617 switch (cu->language_defn->la_array_ordering)
9618 {
9619 case array_column_major:
9620 return DW_ORD_col_major;
9621 case array_row_major:
9622 default:
9623 return DW_ORD_row_major;
9624 };
9625 }
9626
9627 /* Extract all information from a DW_TAG_set_type DIE and put it in
9628 the DIE's type field. */
9629
9630 static struct type *
9631 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9632 {
9633 struct type *domain_type, *set_type;
9634 struct attribute *attr;
9635
9636 domain_type = die_type (die, cu);
9637
9638 /* The die_type call above may have already set the type for this DIE. */
9639 set_type = get_die_type (die, cu);
9640 if (set_type)
9641 return set_type;
9642
9643 set_type = create_set_type (NULL, domain_type);
9644
9645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9646 if (attr)
9647 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9648
9649 return set_die_type (die, set_type, cu);
9650 }
9651
9652 /* First cut: install each common block member as a global variable. */
9653
9654 static void
9655 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9656 {
9657 struct die_info *child_die;
9658 struct attribute *attr;
9659 struct symbol *sym;
9660 CORE_ADDR base = (CORE_ADDR) 0;
9661
9662 attr = dwarf2_attr (die, DW_AT_location, cu);
9663 if (attr)
9664 {
9665 /* Support the .debug_loc offsets. */
9666 if (attr_form_is_block (attr))
9667 {
9668 base = decode_locdesc (DW_BLOCK (attr), cu);
9669 }
9670 else if (attr_form_is_section_offset (attr))
9671 {
9672 dwarf2_complex_location_expr_complaint ();
9673 }
9674 else
9675 {
9676 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9677 "common block member");
9678 }
9679 }
9680 if (die->child != NULL)
9681 {
9682 child_die = die->child;
9683 while (child_die && child_die->tag)
9684 {
9685 LONGEST offset;
9686
9687 sym = new_symbol (child_die, NULL, cu);
9688 if (sym != NULL
9689 && handle_data_member_location (child_die, cu, &offset))
9690 {
9691 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9692 add_symbol_to_list (sym, &global_symbols);
9693 }
9694 child_die = sibling_die (child_die);
9695 }
9696 }
9697 }
9698
9699 /* Create a type for a C++ namespace. */
9700
9701 static struct type *
9702 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9703 {
9704 struct objfile *objfile = cu->objfile;
9705 const char *previous_prefix, *name;
9706 int is_anonymous;
9707 struct type *type;
9708
9709 /* For extensions, reuse the type of the original namespace. */
9710 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9711 {
9712 struct die_info *ext_die;
9713 struct dwarf2_cu *ext_cu = cu;
9714
9715 ext_die = dwarf2_extension (die, &ext_cu);
9716 type = read_type_die (ext_die, ext_cu);
9717
9718 /* EXT_CU may not be the same as CU.
9719 Ensure TYPE is recorded in CU's type_hash table. */
9720 return set_die_type (die, type, cu);
9721 }
9722
9723 name = namespace_name (die, &is_anonymous, cu);
9724
9725 /* Now build the name of the current namespace. */
9726
9727 previous_prefix = determine_prefix (die, cu);
9728 if (previous_prefix[0] != '\0')
9729 name = typename_concat (&objfile->objfile_obstack,
9730 previous_prefix, name, 0, cu);
9731
9732 /* Create the type. */
9733 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9734 objfile);
9735 TYPE_NAME (type) = (char *) name;
9736 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9737
9738 return set_die_type (die, type, cu);
9739 }
9740
9741 /* Read a C++ namespace. */
9742
9743 static void
9744 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9745 {
9746 struct objfile *objfile = cu->objfile;
9747 int is_anonymous;
9748
9749 /* Add a symbol associated to this if we haven't seen the namespace
9750 before. Also, add a using directive if it's an anonymous
9751 namespace. */
9752
9753 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9754 {
9755 struct type *type;
9756
9757 type = read_type_die (die, cu);
9758 new_symbol (die, type, cu);
9759
9760 namespace_name (die, &is_anonymous, cu);
9761 if (is_anonymous)
9762 {
9763 const char *previous_prefix = determine_prefix (die, cu);
9764
9765 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9766 NULL, NULL, &objfile->objfile_obstack);
9767 }
9768 }
9769
9770 if (die->child != NULL)
9771 {
9772 struct die_info *child_die = die->child;
9773
9774 while (child_die && child_die->tag)
9775 {
9776 process_die (child_die, cu);
9777 child_die = sibling_die (child_die);
9778 }
9779 }
9780 }
9781
9782 /* Read a Fortran module as type. This DIE can be only a declaration used for
9783 imported module. Still we need that type as local Fortran "use ... only"
9784 declaration imports depend on the created type in determine_prefix. */
9785
9786 static struct type *
9787 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9788 {
9789 struct objfile *objfile = cu->objfile;
9790 char *module_name;
9791 struct type *type;
9792
9793 module_name = dwarf2_name (die, cu);
9794 if (!module_name)
9795 complaint (&symfile_complaints,
9796 _("DW_TAG_module has no name, offset 0x%x"),
9797 die->offset.sect_off);
9798 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9799
9800 /* determine_prefix uses TYPE_TAG_NAME. */
9801 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9802
9803 return set_die_type (die, type, cu);
9804 }
9805
9806 /* Read a Fortran module. */
9807
9808 static void
9809 read_module (struct die_info *die, struct dwarf2_cu *cu)
9810 {
9811 struct die_info *child_die = die->child;
9812
9813 while (child_die && child_die->tag)
9814 {
9815 process_die (child_die, cu);
9816 child_die = sibling_die (child_die);
9817 }
9818 }
9819
9820 /* Return the name of the namespace represented by DIE. Set
9821 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9822 namespace. */
9823
9824 static const char *
9825 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9826 {
9827 struct die_info *current_die;
9828 const char *name = NULL;
9829
9830 /* Loop through the extensions until we find a name. */
9831
9832 for (current_die = die;
9833 current_die != NULL;
9834 current_die = dwarf2_extension (die, &cu))
9835 {
9836 name = dwarf2_name (current_die, cu);
9837 if (name != NULL)
9838 break;
9839 }
9840
9841 /* Is it an anonymous namespace? */
9842
9843 *is_anonymous = (name == NULL);
9844 if (*is_anonymous)
9845 name = CP_ANONYMOUS_NAMESPACE_STR;
9846
9847 return name;
9848 }
9849
9850 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9851 the user defined type vector. */
9852
9853 static struct type *
9854 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9855 {
9856 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9857 struct comp_unit_head *cu_header = &cu->header;
9858 struct type *type;
9859 struct attribute *attr_byte_size;
9860 struct attribute *attr_address_class;
9861 int byte_size, addr_class;
9862 struct type *target_type;
9863
9864 target_type = die_type (die, cu);
9865
9866 /* The die_type call above may have already set the type for this DIE. */
9867 type = get_die_type (die, cu);
9868 if (type)
9869 return type;
9870
9871 type = lookup_pointer_type (target_type);
9872
9873 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9874 if (attr_byte_size)
9875 byte_size = DW_UNSND (attr_byte_size);
9876 else
9877 byte_size = cu_header->addr_size;
9878
9879 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9880 if (attr_address_class)
9881 addr_class = DW_UNSND (attr_address_class);
9882 else
9883 addr_class = DW_ADDR_none;
9884
9885 /* If the pointer size or address class is different than the
9886 default, create a type variant marked as such and set the
9887 length accordingly. */
9888 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9889 {
9890 if (gdbarch_address_class_type_flags_p (gdbarch))
9891 {
9892 int type_flags;
9893
9894 type_flags = gdbarch_address_class_type_flags
9895 (gdbarch, byte_size, addr_class);
9896 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9897 == 0);
9898 type = make_type_with_address_space (type, type_flags);
9899 }
9900 else if (TYPE_LENGTH (type) != byte_size)
9901 {
9902 complaint (&symfile_complaints,
9903 _("invalid pointer size %d"), byte_size);
9904 }
9905 else
9906 {
9907 /* Should we also complain about unhandled address classes? */
9908 }
9909 }
9910
9911 TYPE_LENGTH (type) = byte_size;
9912 return set_die_type (die, type, cu);
9913 }
9914
9915 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9916 the user defined type vector. */
9917
9918 static struct type *
9919 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9920 {
9921 struct type *type;
9922 struct type *to_type;
9923 struct type *domain;
9924
9925 to_type = die_type (die, cu);
9926 domain = die_containing_type (die, cu);
9927
9928 /* The calls above may have already set the type for this DIE. */
9929 type = get_die_type (die, cu);
9930 if (type)
9931 return type;
9932
9933 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9934 type = lookup_methodptr_type (to_type);
9935 else
9936 type = lookup_memberptr_type (to_type, domain);
9937
9938 return set_die_type (die, type, cu);
9939 }
9940
9941 /* Extract all information from a DW_TAG_reference_type DIE and add to
9942 the user defined type vector. */
9943
9944 static struct type *
9945 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9946 {
9947 struct comp_unit_head *cu_header = &cu->header;
9948 struct type *type, *target_type;
9949 struct attribute *attr;
9950
9951 target_type = die_type (die, cu);
9952
9953 /* The die_type call above may have already set the type for this DIE. */
9954 type = get_die_type (die, cu);
9955 if (type)
9956 return type;
9957
9958 type = lookup_reference_type (target_type);
9959 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9960 if (attr)
9961 {
9962 TYPE_LENGTH (type) = DW_UNSND (attr);
9963 }
9964 else
9965 {
9966 TYPE_LENGTH (type) = cu_header->addr_size;
9967 }
9968 return set_die_type (die, type, cu);
9969 }
9970
9971 static struct type *
9972 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
9973 {
9974 struct type *base_type, *cv_type;
9975
9976 base_type = die_type (die, cu);
9977
9978 /* The die_type call above may have already set the type for this DIE. */
9979 cv_type = get_die_type (die, cu);
9980 if (cv_type)
9981 return cv_type;
9982
9983 /* In case the const qualifier is applied to an array type, the element type
9984 is so qualified, not the array type (section 6.7.3 of C99). */
9985 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9986 {
9987 struct type *el_type, *inner_array;
9988
9989 base_type = copy_type (base_type);
9990 inner_array = base_type;
9991
9992 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9993 {
9994 TYPE_TARGET_TYPE (inner_array) =
9995 copy_type (TYPE_TARGET_TYPE (inner_array));
9996 inner_array = TYPE_TARGET_TYPE (inner_array);
9997 }
9998
9999 el_type = TYPE_TARGET_TYPE (inner_array);
10000 TYPE_TARGET_TYPE (inner_array) =
10001 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10002
10003 return set_die_type (die, base_type, cu);
10004 }
10005
10006 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10007 return set_die_type (die, cv_type, cu);
10008 }
10009
10010 static struct type *
10011 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10012 {
10013 struct type *base_type, *cv_type;
10014
10015 base_type = die_type (die, cu);
10016
10017 /* The die_type call above may have already set the type for this DIE. */
10018 cv_type = get_die_type (die, cu);
10019 if (cv_type)
10020 return cv_type;
10021
10022 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10023 return set_die_type (die, cv_type, cu);
10024 }
10025
10026 /* Extract all information from a DW_TAG_string_type DIE and add to
10027 the user defined type vector. It isn't really a user defined type,
10028 but it behaves like one, with other DIE's using an AT_user_def_type
10029 attribute to reference it. */
10030
10031 static struct type *
10032 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10033 {
10034 struct objfile *objfile = cu->objfile;
10035 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10036 struct type *type, *range_type, *index_type, *char_type;
10037 struct attribute *attr;
10038 unsigned int length;
10039
10040 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10041 if (attr)
10042 {
10043 length = DW_UNSND (attr);
10044 }
10045 else
10046 {
10047 /* Check for the DW_AT_byte_size attribute. */
10048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10049 if (attr)
10050 {
10051 length = DW_UNSND (attr);
10052 }
10053 else
10054 {
10055 length = 1;
10056 }
10057 }
10058
10059 index_type = objfile_type (objfile)->builtin_int;
10060 range_type = create_range_type (NULL, index_type, 1, length);
10061 char_type = language_string_char_type (cu->language_defn, gdbarch);
10062 type = create_string_type (NULL, char_type, range_type);
10063
10064 return set_die_type (die, type, cu);
10065 }
10066
10067 /* Handle DIES due to C code like:
10068
10069 struct foo
10070 {
10071 int (*funcp)(int a, long l);
10072 int b;
10073 };
10074
10075 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10076
10077 static struct type *
10078 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10079 {
10080 struct objfile *objfile = cu->objfile;
10081 struct type *type; /* Type that this function returns. */
10082 struct type *ftype; /* Function that returns above type. */
10083 struct attribute *attr;
10084
10085 type = die_type (die, cu);
10086
10087 /* The die_type call above may have already set the type for this DIE. */
10088 ftype = get_die_type (die, cu);
10089 if (ftype)
10090 return ftype;
10091
10092 ftype = lookup_function_type (type);
10093
10094 /* All functions in C++, Pascal and Java have prototypes. */
10095 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10096 if ((attr && (DW_UNSND (attr) != 0))
10097 || cu->language == language_cplus
10098 || cu->language == language_java
10099 || cu->language == language_pascal)
10100 TYPE_PROTOTYPED (ftype) = 1;
10101 else if (producer_is_realview (cu->producer))
10102 /* RealView does not emit DW_AT_prototyped. We can not
10103 distinguish prototyped and unprototyped functions; default to
10104 prototyped, since that is more common in modern code (and
10105 RealView warns about unprototyped functions). */
10106 TYPE_PROTOTYPED (ftype) = 1;
10107
10108 /* Store the calling convention in the type if it's available in
10109 the subroutine die. Otherwise set the calling convention to
10110 the default value DW_CC_normal. */
10111 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10112 if (attr)
10113 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10114 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10115 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10116 else
10117 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10118
10119 /* We need to add the subroutine type to the die immediately so
10120 we don't infinitely recurse when dealing with parameters
10121 declared as the same subroutine type. */
10122 set_die_type (die, ftype, cu);
10123
10124 if (die->child != NULL)
10125 {
10126 struct type *void_type = objfile_type (objfile)->builtin_void;
10127 struct die_info *child_die;
10128 int nparams, iparams;
10129
10130 /* Count the number of parameters.
10131 FIXME: GDB currently ignores vararg functions, but knows about
10132 vararg member functions. */
10133 nparams = 0;
10134 child_die = die->child;
10135 while (child_die && child_die->tag)
10136 {
10137 if (child_die->tag == DW_TAG_formal_parameter)
10138 nparams++;
10139 else if (child_die->tag == DW_TAG_unspecified_parameters)
10140 TYPE_VARARGS (ftype) = 1;
10141 child_die = sibling_die (child_die);
10142 }
10143
10144 /* Allocate storage for parameters and fill them in. */
10145 TYPE_NFIELDS (ftype) = nparams;
10146 TYPE_FIELDS (ftype) = (struct field *)
10147 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10148
10149 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10150 even if we error out during the parameters reading below. */
10151 for (iparams = 0; iparams < nparams; iparams++)
10152 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10153
10154 iparams = 0;
10155 child_die = die->child;
10156 while (child_die && child_die->tag)
10157 {
10158 if (child_die->tag == DW_TAG_formal_parameter)
10159 {
10160 struct type *arg_type;
10161
10162 /* DWARF version 2 has no clean way to discern C++
10163 static and non-static member functions. G++ helps
10164 GDB by marking the first parameter for non-static
10165 member functions (which is the this pointer) as
10166 artificial. We pass this information to
10167 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10168
10169 DWARF version 3 added DW_AT_object_pointer, which GCC
10170 4.5 does not yet generate. */
10171 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10172 if (attr)
10173 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10174 else
10175 {
10176 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10177
10178 /* GCC/43521: In java, the formal parameter
10179 "this" is sometimes not marked with DW_AT_artificial. */
10180 if (cu->language == language_java)
10181 {
10182 const char *name = dwarf2_name (child_die, cu);
10183
10184 if (name && !strcmp (name, "this"))
10185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10186 }
10187 }
10188 arg_type = die_type (child_die, cu);
10189
10190 /* RealView does not mark THIS as const, which the testsuite
10191 expects. GCC marks THIS as const in method definitions,
10192 but not in the class specifications (GCC PR 43053). */
10193 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10194 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10195 {
10196 int is_this = 0;
10197 struct dwarf2_cu *arg_cu = cu;
10198 const char *name = dwarf2_name (child_die, cu);
10199
10200 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10201 if (attr)
10202 {
10203 /* If the compiler emits this, use it. */
10204 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10205 is_this = 1;
10206 }
10207 else if (name && strcmp (name, "this") == 0)
10208 /* Function definitions will have the argument names. */
10209 is_this = 1;
10210 else if (name == NULL && iparams == 0)
10211 /* Declarations may not have the names, so like
10212 elsewhere in GDB, assume an artificial first
10213 argument is "this". */
10214 is_this = 1;
10215
10216 if (is_this)
10217 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10218 arg_type, 0);
10219 }
10220
10221 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10222 iparams++;
10223 }
10224 child_die = sibling_die (child_die);
10225 }
10226 }
10227
10228 return ftype;
10229 }
10230
10231 static struct type *
10232 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10233 {
10234 struct objfile *objfile = cu->objfile;
10235 const char *name = NULL;
10236 struct type *this_type, *target_type;
10237
10238 name = dwarf2_full_name (NULL, die, cu);
10239 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10240 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10241 TYPE_NAME (this_type) = (char *) name;
10242 set_die_type (die, this_type, cu);
10243 target_type = die_type (die, cu);
10244 if (target_type != this_type)
10245 TYPE_TARGET_TYPE (this_type) = target_type;
10246 else
10247 {
10248 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10249 spec and cause infinite loops in GDB. */
10250 complaint (&symfile_complaints,
10251 _("Self-referential DW_TAG_typedef "
10252 "- DIE at 0x%x [in module %s]"),
10253 die->offset.sect_off, objfile->name);
10254 TYPE_TARGET_TYPE (this_type) = NULL;
10255 }
10256 return this_type;
10257 }
10258
10259 /* Find a representation of a given base type and install
10260 it in the TYPE field of the die. */
10261
10262 static struct type *
10263 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10264 {
10265 struct objfile *objfile = cu->objfile;
10266 struct type *type;
10267 struct attribute *attr;
10268 int encoding = 0, size = 0;
10269 char *name;
10270 enum type_code code = TYPE_CODE_INT;
10271 int type_flags = 0;
10272 struct type *target_type = NULL;
10273
10274 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10275 if (attr)
10276 {
10277 encoding = DW_UNSND (attr);
10278 }
10279 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10280 if (attr)
10281 {
10282 size = DW_UNSND (attr);
10283 }
10284 name = dwarf2_name (die, cu);
10285 if (!name)
10286 {
10287 complaint (&symfile_complaints,
10288 _("DW_AT_name missing from DW_TAG_base_type"));
10289 }
10290
10291 switch (encoding)
10292 {
10293 case DW_ATE_address:
10294 /* Turn DW_ATE_address into a void * pointer. */
10295 code = TYPE_CODE_PTR;
10296 type_flags |= TYPE_FLAG_UNSIGNED;
10297 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10298 break;
10299 case DW_ATE_boolean:
10300 code = TYPE_CODE_BOOL;
10301 type_flags |= TYPE_FLAG_UNSIGNED;
10302 break;
10303 case DW_ATE_complex_float:
10304 code = TYPE_CODE_COMPLEX;
10305 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10306 break;
10307 case DW_ATE_decimal_float:
10308 code = TYPE_CODE_DECFLOAT;
10309 break;
10310 case DW_ATE_float:
10311 code = TYPE_CODE_FLT;
10312 break;
10313 case DW_ATE_signed:
10314 break;
10315 case DW_ATE_unsigned:
10316 type_flags |= TYPE_FLAG_UNSIGNED;
10317 if (cu->language == language_fortran
10318 && name
10319 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10320 code = TYPE_CODE_CHAR;
10321 break;
10322 case DW_ATE_signed_char:
10323 if (cu->language == language_ada || cu->language == language_m2
10324 || cu->language == language_pascal
10325 || cu->language == language_fortran)
10326 code = TYPE_CODE_CHAR;
10327 break;
10328 case DW_ATE_unsigned_char:
10329 if (cu->language == language_ada || cu->language == language_m2
10330 || cu->language == language_pascal
10331 || cu->language == language_fortran)
10332 code = TYPE_CODE_CHAR;
10333 type_flags |= TYPE_FLAG_UNSIGNED;
10334 break;
10335 case DW_ATE_UTF:
10336 /* We just treat this as an integer and then recognize the
10337 type by name elsewhere. */
10338 break;
10339
10340 default:
10341 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10342 dwarf_type_encoding_name (encoding));
10343 break;
10344 }
10345
10346 type = init_type (code, size, type_flags, NULL, objfile);
10347 TYPE_NAME (type) = name;
10348 TYPE_TARGET_TYPE (type) = target_type;
10349
10350 if (name && strcmp (name, "char") == 0)
10351 TYPE_NOSIGN (type) = 1;
10352
10353 return set_die_type (die, type, cu);
10354 }
10355
10356 /* Read the given DW_AT_subrange DIE. */
10357
10358 static struct type *
10359 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10360 {
10361 struct type *base_type;
10362 struct type *range_type;
10363 struct attribute *attr;
10364 LONGEST low, high;
10365 int low_default_is_valid;
10366 char *name;
10367 LONGEST negative_mask;
10368
10369 base_type = die_type (die, cu);
10370 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10371 check_typedef (base_type);
10372
10373 /* The die_type call above may have already set the type for this DIE. */
10374 range_type = get_die_type (die, cu);
10375 if (range_type)
10376 return range_type;
10377
10378 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10379 omitting DW_AT_lower_bound. */
10380 switch (cu->language)
10381 {
10382 case language_c:
10383 case language_cplus:
10384 low = 0;
10385 low_default_is_valid = 1;
10386 break;
10387 case language_fortran:
10388 low = 1;
10389 low_default_is_valid = 1;
10390 break;
10391 case language_d:
10392 case language_java:
10393 case language_objc:
10394 low = 0;
10395 low_default_is_valid = (cu->header.version >= 4);
10396 break;
10397 case language_ada:
10398 case language_m2:
10399 case language_pascal:
10400 low = 1;
10401 low_default_is_valid = (cu->header.version >= 4);
10402 break;
10403 default:
10404 low = 0;
10405 low_default_is_valid = 0;
10406 break;
10407 }
10408
10409 /* FIXME: For variable sized arrays either of these could be
10410 a variable rather than a constant value. We'll allow it,
10411 but we don't know how to handle it. */
10412 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10413 if (attr)
10414 low = dwarf2_get_attr_constant_value (attr, low);
10415 else if (!low_default_is_valid)
10416 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10417 "- DIE at 0x%x [in module %s]"),
10418 die->offset.sect_off, cu->objfile->name);
10419
10420 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10421 if (attr)
10422 {
10423 if (attr_form_is_block (attr) || is_ref_attr (attr))
10424 {
10425 /* GCC encodes arrays with unspecified or dynamic length
10426 with a DW_FORM_block1 attribute or a reference attribute.
10427 FIXME: GDB does not yet know how to handle dynamic
10428 arrays properly, treat them as arrays with unspecified
10429 length for now.
10430
10431 FIXME: jimb/2003-09-22: GDB does not really know
10432 how to handle arrays of unspecified length
10433 either; we just represent them as zero-length
10434 arrays. Choose an appropriate upper bound given
10435 the lower bound we've computed above. */
10436 high = low - 1;
10437 }
10438 else
10439 high = dwarf2_get_attr_constant_value (attr, 1);
10440 }
10441 else
10442 {
10443 attr = dwarf2_attr (die, DW_AT_count, cu);
10444 if (attr)
10445 {
10446 int count = dwarf2_get_attr_constant_value (attr, 1);
10447 high = low + count - 1;
10448 }
10449 else
10450 {
10451 /* Unspecified array length. */
10452 high = low - 1;
10453 }
10454 }
10455
10456 /* Dwarf-2 specifications explicitly allows to create subrange types
10457 without specifying a base type.
10458 In that case, the base type must be set to the type of
10459 the lower bound, upper bound or count, in that order, if any of these
10460 three attributes references an object that has a type.
10461 If no base type is found, the Dwarf-2 specifications say that
10462 a signed integer type of size equal to the size of an address should
10463 be used.
10464 For the following C code: `extern char gdb_int [];'
10465 GCC produces an empty range DIE.
10466 FIXME: muller/2010-05-28: Possible references to object for low bound,
10467 high bound or count are not yet handled by this code. */
10468 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10469 {
10470 struct objfile *objfile = cu->objfile;
10471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10472 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10473 struct type *int_type = objfile_type (objfile)->builtin_int;
10474
10475 /* Test "int", "long int", and "long long int" objfile types,
10476 and select the first one having a size above or equal to the
10477 architecture address size. */
10478 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10479 base_type = int_type;
10480 else
10481 {
10482 int_type = objfile_type (objfile)->builtin_long;
10483 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10484 base_type = int_type;
10485 else
10486 {
10487 int_type = objfile_type (objfile)->builtin_long_long;
10488 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10489 base_type = int_type;
10490 }
10491 }
10492 }
10493
10494 negative_mask =
10495 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10496 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10497 low |= negative_mask;
10498 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10499 high |= negative_mask;
10500
10501 range_type = create_range_type (NULL, base_type, low, high);
10502
10503 /* Mark arrays with dynamic length at least as an array of unspecified
10504 length. GDB could check the boundary but before it gets implemented at
10505 least allow accessing the array elements. */
10506 if (attr && attr_form_is_block (attr))
10507 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10508
10509 /* Ada expects an empty array on no boundary attributes. */
10510 if (attr == NULL && cu->language != language_ada)
10511 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10512
10513 name = dwarf2_name (die, cu);
10514 if (name)
10515 TYPE_NAME (range_type) = name;
10516
10517 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10518 if (attr)
10519 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10520
10521 set_die_type (die, range_type, cu);
10522
10523 /* set_die_type should be already done. */
10524 set_descriptive_type (range_type, die, cu);
10525
10526 return range_type;
10527 }
10528
10529 static struct type *
10530 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10531 {
10532 struct type *type;
10533
10534 /* For now, we only support the C meaning of an unspecified type: void. */
10535
10536 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10537 TYPE_NAME (type) = dwarf2_name (die, cu);
10538
10539 return set_die_type (die, type, cu);
10540 }
10541
10542 /* Read a single die and all its descendents. Set the die's sibling
10543 field to NULL; set other fields in the die correctly, and set all
10544 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10545 location of the info_ptr after reading all of those dies. PARENT
10546 is the parent of the die in question. */
10547
10548 static struct die_info *
10549 read_die_and_children (const struct die_reader_specs *reader,
10550 gdb_byte *info_ptr,
10551 gdb_byte **new_info_ptr,
10552 struct die_info *parent)
10553 {
10554 struct die_info *die;
10555 gdb_byte *cur_ptr;
10556 int has_children;
10557
10558 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10559 if (die == NULL)
10560 {
10561 *new_info_ptr = cur_ptr;
10562 return NULL;
10563 }
10564 store_in_ref_table (die, reader->cu);
10565
10566 if (has_children)
10567 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10568 else
10569 {
10570 die->child = NULL;
10571 *new_info_ptr = cur_ptr;
10572 }
10573
10574 die->sibling = NULL;
10575 die->parent = parent;
10576 return die;
10577 }
10578
10579 /* Read a die, all of its descendents, and all of its siblings; set
10580 all of the fields of all of the dies correctly. Arguments are as
10581 in read_die_and_children. */
10582
10583 static struct die_info *
10584 read_die_and_siblings (const struct die_reader_specs *reader,
10585 gdb_byte *info_ptr,
10586 gdb_byte **new_info_ptr,
10587 struct die_info *parent)
10588 {
10589 struct die_info *first_die, *last_sibling;
10590 gdb_byte *cur_ptr;
10591
10592 cur_ptr = info_ptr;
10593 first_die = last_sibling = NULL;
10594
10595 while (1)
10596 {
10597 struct die_info *die
10598 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10599
10600 if (die == NULL)
10601 {
10602 *new_info_ptr = cur_ptr;
10603 return first_die;
10604 }
10605
10606 if (!first_die)
10607 first_die = die;
10608 else
10609 last_sibling->sibling = die;
10610
10611 last_sibling = die;
10612 }
10613 }
10614
10615 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10616 attributes.
10617 The caller is responsible for filling in the extra attributes
10618 and updating (*DIEP)->num_attrs.
10619 Set DIEP to point to a newly allocated die with its information,
10620 except for its child, sibling, and parent fields.
10621 Set HAS_CHILDREN to tell whether the die has children or not. */
10622
10623 static gdb_byte *
10624 read_full_die_1 (const struct die_reader_specs *reader,
10625 struct die_info **diep, gdb_byte *info_ptr,
10626 int *has_children, int num_extra_attrs)
10627 {
10628 unsigned int abbrev_number, bytes_read, i;
10629 sect_offset offset;
10630 struct abbrev_info *abbrev;
10631 struct die_info *die;
10632 struct dwarf2_cu *cu = reader->cu;
10633 bfd *abfd = reader->abfd;
10634
10635 offset.sect_off = info_ptr - reader->buffer;
10636 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10637 info_ptr += bytes_read;
10638 if (!abbrev_number)
10639 {
10640 *diep = NULL;
10641 *has_children = 0;
10642 return info_ptr;
10643 }
10644
10645 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10646 if (!abbrev)
10647 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10648 abbrev_number,
10649 bfd_get_filename (abfd));
10650
10651 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10652 die->offset = offset;
10653 die->tag = abbrev->tag;
10654 die->abbrev = abbrev_number;
10655
10656 /* Make the result usable.
10657 The caller needs to update num_attrs after adding the extra
10658 attributes. */
10659 die->num_attrs = abbrev->num_attrs;
10660
10661 for (i = 0; i < abbrev->num_attrs; ++i)
10662 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10663 info_ptr);
10664
10665 *diep = die;
10666 *has_children = abbrev->has_children;
10667 return info_ptr;
10668 }
10669
10670 /* Read a die and all its attributes.
10671 Set DIEP to point to a newly allocated die with its information,
10672 except for its child, sibling, and parent fields.
10673 Set HAS_CHILDREN to tell whether the die has children or not. */
10674
10675 static gdb_byte *
10676 read_full_die (const struct die_reader_specs *reader,
10677 struct die_info **diep, gdb_byte *info_ptr,
10678 int *has_children)
10679 {
10680 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10681 }
10682
10683 /* In DWARF version 2, the description of the debugging information is
10684 stored in a separate .debug_abbrev section. Before we read any
10685 dies from a section we read in all abbreviations and install them
10686 in a hash table. This function also sets flags in CU describing
10687 the data found in the abbrev table. */
10688
10689 static void
10690 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10691 struct dwarf2_section_info *abbrev_section)
10692
10693 {
10694 bfd *abfd = abbrev_section->asection->owner;
10695 struct comp_unit_head *cu_header = &cu->header;
10696 gdb_byte *abbrev_ptr;
10697 struct abbrev_info *cur_abbrev;
10698 unsigned int abbrev_number, bytes_read, abbrev_name;
10699 unsigned int abbrev_form, hash_number;
10700 struct attr_abbrev *cur_attrs;
10701 unsigned int allocated_attrs;
10702
10703 /* Initialize dwarf2 abbrevs. */
10704 obstack_init (&cu->abbrev_obstack);
10705 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10706 (ABBREV_HASH_SIZE
10707 * sizeof (struct abbrev_info *)));
10708 memset (cu->dwarf2_abbrevs, 0,
10709 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10710
10711 dwarf2_read_section (cu->objfile, abbrev_section);
10712 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10713 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10714 abbrev_ptr += bytes_read;
10715
10716 allocated_attrs = ATTR_ALLOC_CHUNK;
10717 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10718
10719 /* Loop until we reach an abbrev number of 0. */
10720 while (abbrev_number)
10721 {
10722 cur_abbrev = dwarf_alloc_abbrev (cu);
10723
10724 /* read in abbrev header */
10725 cur_abbrev->number = abbrev_number;
10726 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10727 abbrev_ptr += bytes_read;
10728 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10729 abbrev_ptr += 1;
10730
10731 /* now read in declarations */
10732 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10733 abbrev_ptr += bytes_read;
10734 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10735 abbrev_ptr += bytes_read;
10736 while (abbrev_name)
10737 {
10738 if (cur_abbrev->num_attrs == allocated_attrs)
10739 {
10740 allocated_attrs += ATTR_ALLOC_CHUNK;
10741 cur_attrs
10742 = xrealloc (cur_attrs, (allocated_attrs
10743 * sizeof (struct attr_abbrev)));
10744 }
10745
10746 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10747 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10748 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10749 abbrev_ptr += bytes_read;
10750 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10751 abbrev_ptr += bytes_read;
10752 }
10753
10754 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10755 (cur_abbrev->num_attrs
10756 * sizeof (struct attr_abbrev)));
10757 memcpy (cur_abbrev->attrs, cur_attrs,
10758 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10759
10760 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10761 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10762 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10763
10764 /* Get next abbreviation.
10765 Under Irix6 the abbreviations for a compilation unit are not
10766 always properly terminated with an abbrev number of 0.
10767 Exit loop if we encounter an abbreviation which we have
10768 already read (which means we are about to read the abbreviations
10769 for the next compile unit) or if the end of the abbreviation
10770 table is reached. */
10771 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10772 >= abbrev_section->size)
10773 break;
10774 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10775 abbrev_ptr += bytes_read;
10776 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10777 break;
10778 }
10779
10780 xfree (cur_attrs);
10781 }
10782
10783 /* Release the memory used by the abbrev table for a compilation unit. */
10784
10785 static void
10786 dwarf2_free_abbrev_table (void *ptr_to_cu)
10787 {
10788 struct dwarf2_cu *cu = ptr_to_cu;
10789
10790 obstack_free (&cu->abbrev_obstack, NULL);
10791 cu->dwarf2_abbrevs = NULL;
10792 }
10793
10794 /* Lookup an abbrev_info structure in the abbrev hash table. */
10795
10796 static struct abbrev_info *
10797 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10798 {
10799 unsigned int hash_number;
10800 struct abbrev_info *abbrev;
10801
10802 hash_number = number % ABBREV_HASH_SIZE;
10803 abbrev = cu->dwarf2_abbrevs[hash_number];
10804
10805 while (abbrev)
10806 {
10807 if (abbrev->number == number)
10808 return abbrev;
10809 else
10810 abbrev = abbrev->next;
10811 }
10812 return NULL;
10813 }
10814
10815 /* Returns nonzero if TAG represents a type that we might generate a partial
10816 symbol for. */
10817
10818 static int
10819 is_type_tag_for_partial (int tag)
10820 {
10821 switch (tag)
10822 {
10823 #if 0
10824 /* Some types that would be reasonable to generate partial symbols for,
10825 that we don't at present. */
10826 case DW_TAG_array_type:
10827 case DW_TAG_file_type:
10828 case DW_TAG_ptr_to_member_type:
10829 case DW_TAG_set_type:
10830 case DW_TAG_string_type:
10831 case DW_TAG_subroutine_type:
10832 #endif
10833 case DW_TAG_base_type:
10834 case DW_TAG_class_type:
10835 case DW_TAG_interface_type:
10836 case DW_TAG_enumeration_type:
10837 case DW_TAG_structure_type:
10838 case DW_TAG_subrange_type:
10839 case DW_TAG_typedef:
10840 case DW_TAG_union_type:
10841 return 1;
10842 default:
10843 return 0;
10844 }
10845 }
10846
10847 /* Load all DIEs that are interesting for partial symbols into memory. */
10848
10849 static struct partial_die_info *
10850 load_partial_dies (const struct die_reader_specs *reader,
10851 gdb_byte *info_ptr, int building_psymtab)
10852 {
10853 struct dwarf2_cu *cu = reader->cu;
10854 struct objfile *objfile = cu->objfile;
10855 struct partial_die_info *part_die;
10856 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10857 struct abbrev_info *abbrev;
10858 unsigned int bytes_read;
10859 unsigned int load_all = 0;
10860 int nesting_level = 1;
10861
10862 parent_die = NULL;
10863 last_die = NULL;
10864
10865 gdb_assert (cu->per_cu != NULL);
10866 if (cu->per_cu->load_all_dies)
10867 load_all = 1;
10868
10869 cu->partial_dies
10870 = htab_create_alloc_ex (cu->header.length / 12,
10871 partial_die_hash,
10872 partial_die_eq,
10873 NULL,
10874 &cu->comp_unit_obstack,
10875 hashtab_obstack_allocate,
10876 dummy_obstack_deallocate);
10877
10878 part_die = obstack_alloc (&cu->comp_unit_obstack,
10879 sizeof (struct partial_die_info));
10880
10881 while (1)
10882 {
10883 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10884
10885 /* A NULL abbrev means the end of a series of children. */
10886 if (abbrev == NULL)
10887 {
10888 if (--nesting_level == 0)
10889 {
10890 /* PART_DIE was probably the last thing allocated on the
10891 comp_unit_obstack, so we could call obstack_free
10892 here. We don't do that because the waste is small,
10893 and will be cleaned up when we're done with this
10894 compilation unit. This way, we're also more robust
10895 against other users of the comp_unit_obstack. */
10896 return first_die;
10897 }
10898 info_ptr += bytes_read;
10899 last_die = parent_die;
10900 parent_die = parent_die->die_parent;
10901 continue;
10902 }
10903
10904 /* Check for template arguments. We never save these; if
10905 they're seen, we just mark the parent, and go on our way. */
10906 if (parent_die != NULL
10907 && cu->language == language_cplus
10908 && (abbrev->tag == DW_TAG_template_type_param
10909 || abbrev->tag == DW_TAG_template_value_param))
10910 {
10911 parent_die->has_template_arguments = 1;
10912
10913 if (!load_all)
10914 {
10915 /* We don't need a partial DIE for the template argument. */
10916 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10917 continue;
10918 }
10919 }
10920
10921 /* We only recurse into c++ subprograms looking for template arguments.
10922 Skip their other children. */
10923 if (!load_all
10924 && cu->language == language_cplus
10925 && parent_die != NULL
10926 && parent_die->tag == DW_TAG_subprogram)
10927 {
10928 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10929 continue;
10930 }
10931
10932 /* Check whether this DIE is interesting enough to save. Normally
10933 we would not be interested in members here, but there may be
10934 later variables referencing them via DW_AT_specification (for
10935 static members). */
10936 if (!load_all
10937 && !is_type_tag_for_partial (abbrev->tag)
10938 && abbrev->tag != DW_TAG_constant
10939 && abbrev->tag != DW_TAG_enumerator
10940 && abbrev->tag != DW_TAG_subprogram
10941 && abbrev->tag != DW_TAG_lexical_block
10942 && abbrev->tag != DW_TAG_variable
10943 && abbrev->tag != DW_TAG_namespace
10944 && abbrev->tag != DW_TAG_module
10945 && abbrev->tag != DW_TAG_member
10946 && abbrev->tag != DW_TAG_imported_unit)
10947 {
10948 /* Otherwise we skip to the next sibling, if any. */
10949 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10950 continue;
10951 }
10952
10953 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10954 info_ptr);
10955
10956 /* This two-pass algorithm for processing partial symbols has a
10957 high cost in cache pressure. Thus, handle some simple cases
10958 here which cover the majority of C partial symbols. DIEs
10959 which neither have specification tags in them, nor could have
10960 specification tags elsewhere pointing at them, can simply be
10961 processed and discarded.
10962
10963 This segment is also optional; scan_partial_symbols and
10964 add_partial_symbol will handle these DIEs if we chain
10965 them in normally. When compilers which do not emit large
10966 quantities of duplicate debug information are more common,
10967 this code can probably be removed. */
10968
10969 /* Any complete simple types at the top level (pretty much all
10970 of them, for a language without namespaces), can be processed
10971 directly. */
10972 if (parent_die == NULL
10973 && part_die->has_specification == 0
10974 && part_die->is_declaration == 0
10975 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
10976 || part_die->tag == DW_TAG_base_type
10977 || part_die->tag == DW_TAG_subrange_type))
10978 {
10979 if (building_psymtab && part_die->name != NULL)
10980 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10981 VAR_DOMAIN, LOC_TYPEDEF,
10982 &objfile->static_psymbols,
10983 0, (CORE_ADDR) 0, cu->language, objfile);
10984 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10985 continue;
10986 }
10987
10988 /* The exception for DW_TAG_typedef with has_children above is
10989 a workaround of GCC PR debug/47510. In the case of this complaint
10990 type_name_no_tag_or_error will error on such types later.
10991
10992 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10993 it could not find the child DIEs referenced later, this is checked
10994 above. In correct DWARF DW_TAG_typedef should have no children. */
10995
10996 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10997 complaint (&symfile_complaints,
10998 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10999 "- DIE at 0x%x [in module %s]"),
11000 part_die->offset.sect_off, objfile->name);
11001
11002 /* If we're at the second level, and we're an enumerator, and
11003 our parent has no specification (meaning possibly lives in a
11004 namespace elsewhere), then we can add the partial symbol now
11005 instead of queueing it. */
11006 if (part_die->tag == DW_TAG_enumerator
11007 && parent_die != NULL
11008 && parent_die->die_parent == NULL
11009 && parent_die->tag == DW_TAG_enumeration_type
11010 && parent_die->has_specification == 0)
11011 {
11012 if (part_die->name == NULL)
11013 complaint (&symfile_complaints,
11014 _("malformed enumerator DIE ignored"));
11015 else if (building_psymtab)
11016 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11017 VAR_DOMAIN, LOC_CONST,
11018 (cu->language == language_cplus
11019 || cu->language == language_java)
11020 ? &objfile->global_psymbols
11021 : &objfile->static_psymbols,
11022 0, (CORE_ADDR) 0, cu->language, objfile);
11023
11024 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11025 continue;
11026 }
11027
11028 /* We'll save this DIE so link it in. */
11029 part_die->die_parent = parent_die;
11030 part_die->die_sibling = NULL;
11031 part_die->die_child = NULL;
11032
11033 if (last_die && last_die == parent_die)
11034 last_die->die_child = part_die;
11035 else if (last_die)
11036 last_die->die_sibling = part_die;
11037
11038 last_die = part_die;
11039
11040 if (first_die == NULL)
11041 first_die = part_die;
11042
11043 /* Maybe add the DIE to the hash table. Not all DIEs that we
11044 find interesting need to be in the hash table, because we
11045 also have the parent/sibling/child chains; only those that we
11046 might refer to by offset later during partial symbol reading.
11047
11048 For now this means things that might have be the target of a
11049 DW_AT_specification, DW_AT_abstract_origin, or
11050 DW_AT_extension. DW_AT_extension will refer only to
11051 namespaces; DW_AT_abstract_origin refers to functions (and
11052 many things under the function DIE, but we do not recurse
11053 into function DIEs during partial symbol reading) and
11054 possibly variables as well; DW_AT_specification refers to
11055 declarations. Declarations ought to have the DW_AT_declaration
11056 flag. It happens that GCC forgets to put it in sometimes, but
11057 only for functions, not for types.
11058
11059 Adding more things than necessary to the hash table is harmless
11060 except for the performance cost. Adding too few will result in
11061 wasted time in find_partial_die, when we reread the compilation
11062 unit with load_all_dies set. */
11063
11064 if (load_all
11065 || abbrev->tag == DW_TAG_constant
11066 || abbrev->tag == DW_TAG_subprogram
11067 || abbrev->tag == DW_TAG_variable
11068 || abbrev->tag == DW_TAG_namespace
11069 || part_die->is_declaration)
11070 {
11071 void **slot;
11072
11073 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11074 part_die->offset.sect_off, INSERT);
11075 *slot = part_die;
11076 }
11077
11078 part_die = obstack_alloc (&cu->comp_unit_obstack,
11079 sizeof (struct partial_die_info));
11080
11081 /* For some DIEs we want to follow their children (if any). For C
11082 we have no reason to follow the children of structures; for other
11083 languages we have to, so that we can get at method physnames
11084 to infer fully qualified class names, for DW_AT_specification,
11085 and for C++ template arguments. For C++, we also look one level
11086 inside functions to find template arguments (if the name of the
11087 function does not already contain the template arguments).
11088
11089 For Ada, we need to scan the children of subprograms and lexical
11090 blocks as well because Ada allows the definition of nested
11091 entities that could be interesting for the debugger, such as
11092 nested subprograms for instance. */
11093 if (last_die->has_children
11094 && (load_all
11095 || last_die->tag == DW_TAG_namespace
11096 || last_die->tag == DW_TAG_module
11097 || last_die->tag == DW_TAG_enumeration_type
11098 || (cu->language == language_cplus
11099 && last_die->tag == DW_TAG_subprogram
11100 && (last_die->name == NULL
11101 || strchr (last_die->name, '<') == NULL))
11102 || (cu->language != language_c
11103 && (last_die->tag == DW_TAG_class_type
11104 || last_die->tag == DW_TAG_interface_type
11105 || last_die->tag == DW_TAG_structure_type
11106 || last_die->tag == DW_TAG_union_type))
11107 || (cu->language == language_ada
11108 && (last_die->tag == DW_TAG_subprogram
11109 || last_die->tag == DW_TAG_lexical_block))))
11110 {
11111 nesting_level++;
11112 parent_die = last_die;
11113 continue;
11114 }
11115
11116 /* Otherwise we skip to the next sibling, if any. */
11117 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11118
11119 /* Back to the top, do it again. */
11120 }
11121 }
11122
11123 /* Read a minimal amount of information into the minimal die structure. */
11124
11125 static gdb_byte *
11126 read_partial_die (const struct die_reader_specs *reader,
11127 struct partial_die_info *part_die,
11128 struct abbrev_info *abbrev, unsigned int abbrev_len,
11129 gdb_byte *info_ptr)
11130 {
11131 struct dwarf2_cu *cu = reader->cu;
11132 struct objfile *objfile = cu->objfile;
11133 gdb_byte *buffer = reader->buffer;
11134 unsigned int i;
11135 struct attribute attr;
11136 int has_low_pc_attr = 0;
11137 int has_high_pc_attr = 0;
11138 int high_pc_relative = 0;
11139
11140 memset (part_die, 0, sizeof (struct partial_die_info));
11141
11142 part_die->offset.sect_off = info_ptr - buffer;
11143
11144 info_ptr += abbrev_len;
11145
11146 if (abbrev == NULL)
11147 return info_ptr;
11148
11149 part_die->tag = abbrev->tag;
11150 part_die->has_children = abbrev->has_children;
11151
11152 for (i = 0; i < abbrev->num_attrs; ++i)
11153 {
11154 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11155
11156 /* Store the data if it is of an attribute we want to keep in a
11157 partial symbol table. */
11158 switch (attr.name)
11159 {
11160 case DW_AT_name:
11161 switch (part_die->tag)
11162 {
11163 case DW_TAG_compile_unit:
11164 case DW_TAG_partial_unit:
11165 case DW_TAG_type_unit:
11166 /* Compilation units have a DW_AT_name that is a filename, not
11167 a source language identifier. */
11168 case DW_TAG_enumeration_type:
11169 case DW_TAG_enumerator:
11170 /* These tags always have simple identifiers already; no need
11171 to canonicalize them. */
11172 part_die->name = DW_STRING (&attr);
11173 break;
11174 default:
11175 part_die->name
11176 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11177 &objfile->objfile_obstack);
11178 break;
11179 }
11180 break;
11181 case DW_AT_linkage_name:
11182 case DW_AT_MIPS_linkage_name:
11183 /* Note that both forms of linkage name might appear. We
11184 assume they will be the same, and we only store the last
11185 one we see. */
11186 if (cu->language == language_ada)
11187 part_die->name = DW_STRING (&attr);
11188 part_die->linkage_name = DW_STRING (&attr);
11189 break;
11190 case DW_AT_low_pc:
11191 has_low_pc_attr = 1;
11192 part_die->lowpc = DW_ADDR (&attr);
11193 break;
11194 case DW_AT_high_pc:
11195 has_high_pc_attr = 1;
11196 if (attr.form == DW_FORM_addr
11197 || attr.form == DW_FORM_GNU_addr_index)
11198 part_die->highpc = DW_ADDR (&attr);
11199 else
11200 {
11201 high_pc_relative = 1;
11202 part_die->highpc = DW_UNSND (&attr);
11203 }
11204 break;
11205 case DW_AT_location:
11206 /* Support the .debug_loc offsets. */
11207 if (attr_form_is_block (&attr))
11208 {
11209 part_die->d.locdesc = DW_BLOCK (&attr);
11210 }
11211 else if (attr_form_is_section_offset (&attr))
11212 {
11213 dwarf2_complex_location_expr_complaint ();
11214 }
11215 else
11216 {
11217 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11218 "partial symbol information");
11219 }
11220 break;
11221 case DW_AT_external:
11222 part_die->is_external = DW_UNSND (&attr);
11223 break;
11224 case DW_AT_declaration:
11225 part_die->is_declaration = DW_UNSND (&attr);
11226 break;
11227 case DW_AT_type:
11228 part_die->has_type = 1;
11229 break;
11230 case DW_AT_abstract_origin:
11231 case DW_AT_specification:
11232 case DW_AT_extension:
11233 part_die->has_specification = 1;
11234 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11235 break;
11236 case DW_AT_sibling:
11237 /* Ignore absolute siblings, they might point outside of
11238 the current compile unit. */
11239 if (attr.form == DW_FORM_ref_addr)
11240 complaint (&symfile_complaints,
11241 _("ignoring absolute DW_AT_sibling"));
11242 else
11243 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11244 break;
11245 case DW_AT_byte_size:
11246 part_die->has_byte_size = 1;
11247 break;
11248 case DW_AT_calling_convention:
11249 /* DWARF doesn't provide a way to identify a program's source-level
11250 entry point. DW_AT_calling_convention attributes are only meant
11251 to describe functions' calling conventions.
11252
11253 However, because it's a necessary piece of information in
11254 Fortran, and because DW_CC_program is the only piece of debugging
11255 information whose definition refers to a 'main program' at all,
11256 several compilers have begun marking Fortran main programs with
11257 DW_CC_program --- even when those functions use the standard
11258 calling conventions.
11259
11260 So until DWARF specifies a way to provide this information and
11261 compilers pick up the new representation, we'll support this
11262 practice. */
11263 if (DW_UNSND (&attr) == DW_CC_program
11264 && cu->language == language_fortran)
11265 {
11266 set_main_name (part_die->name);
11267
11268 /* As this DIE has a static linkage the name would be difficult
11269 to look up later. */
11270 language_of_main = language_fortran;
11271 }
11272 break;
11273 case DW_AT_inline:
11274 if (DW_UNSND (&attr) == DW_INL_inlined
11275 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11276 part_die->may_be_inlined = 1;
11277 break;
11278
11279 case DW_AT_import:
11280 if (part_die->tag == DW_TAG_imported_unit)
11281 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11282 break;
11283
11284 default:
11285 break;
11286 }
11287 }
11288
11289 if (high_pc_relative)
11290 part_die->highpc += part_die->lowpc;
11291
11292 if (has_low_pc_attr && has_high_pc_attr)
11293 {
11294 /* When using the GNU linker, .gnu.linkonce. sections are used to
11295 eliminate duplicate copies of functions and vtables and such.
11296 The linker will arbitrarily choose one and discard the others.
11297 The AT_*_pc values for such functions refer to local labels in
11298 these sections. If the section from that file was discarded, the
11299 labels are not in the output, so the relocs get a value of 0.
11300 If this is a discarded function, mark the pc bounds as invalid,
11301 so that GDB will ignore it. */
11302 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11303 {
11304 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11305
11306 complaint (&symfile_complaints,
11307 _("DW_AT_low_pc %s is zero "
11308 "for DIE at 0x%x [in module %s]"),
11309 paddress (gdbarch, part_die->lowpc),
11310 part_die->offset.sect_off, objfile->name);
11311 }
11312 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11313 else if (part_die->lowpc >= part_die->highpc)
11314 {
11315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11316
11317 complaint (&symfile_complaints,
11318 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11319 "for DIE at 0x%x [in module %s]"),
11320 paddress (gdbarch, part_die->lowpc),
11321 paddress (gdbarch, part_die->highpc),
11322 part_die->offset.sect_off, objfile->name);
11323 }
11324 else
11325 part_die->has_pc_info = 1;
11326 }
11327
11328 return info_ptr;
11329 }
11330
11331 /* Find a cached partial DIE at OFFSET in CU. */
11332
11333 static struct partial_die_info *
11334 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11335 {
11336 struct partial_die_info *lookup_die = NULL;
11337 struct partial_die_info part_die;
11338
11339 part_die.offset = offset;
11340 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11341 offset.sect_off);
11342
11343 return lookup_die;
11344 }
11345
11346 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11347 except in the case of .debug_types DIEs which do not reference
11348 outside their CU (they do however referencing other types via
11349 DW_FORM_ref_sig8). */
11350
11351 static struct partial_die_info *
11352 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11353 {
11354 struct objfile *objfile = cu->objfile;
11355 struct dwarf2_per_cu_data *per_cu = NULL;
11356 struct partial_die_info *pd = NULL;
11357
11358 if (offset_in_cu_p (&cu->header, offset))
11359 {
11360 pd = find_partial_die_in_comp_unit (offset, cu);
11361 if (pd != NULL)
11362 return pd;
11363 /* We missed recording what we needed.
11364 Load all dies and try again. */
11365 per_cu = cu->per_cu;
11366 }
11367 else
11368 {
11369 /* TUs don't reference other CUs/TUs (except via type signatures). */
11370 if (cu->per_cu->is_debug_types)
11371 {
11372 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11373 " external reference to offset 0x%lx [in module %s].\n"),
11374 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11375 bfd_get_filename (objfile->obfd));
11376 }
11377 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11378
11379 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11380 load_partial_comp_unit (per_cu);
11381
11382 per_cu->cu->last_used = 0;
11383 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11384 }
11385
11386 /* If we didn't find it, and not all dies have been loaded,
11387 load them all and try again. */
11388
11389 if (pd == NULL && per_cu->load_all_dies == 0)
11390 {
11391 per_cu->load_all_dies = 1;
11392
11393 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11394 THIS_CU->cu may already be in use. So we can't just free it and
11395 replace its DIEs with the ones we read in. Instead, we leave those
11396 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11397 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11398 set. */
11399 load_partial_comp_unit (per_cu);
11400
11401 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11402 }
11403
11404 if (pd == NULL)
11405 internal_error (__FILE__, __LINE__,
11406 _("could not find partial DIE 0x%x "
11407 "in cache [from module %s]\n"),
11408 offset.sect_off, bfd_get_filename (objfile->obfd));
11409 return pd;
11410 }
11411
11412 /* See if we can figure out if the class lives in a namespace. We do
11413 this by looking for a member function; its demangled name will
11414 contain namespace info, if there is any. */
11415
11416 static void
11417 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11418 struct dwarf2_cu *cu)
11419 {
11420 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11421 what template types look like, because the demangler
11422 frequently doesn't give the same name as the debug info. We
11423 could fix this by only using the demangled name to get the
11424 prefix (but see comment in read_structure_type). */
11425
11426 struct partial_die_info *real_pdi;
11427 struct partial_die_info *child_pdi;
11428
11429 /* If this DIE (this DIE's specification, if any) has a parent, then
11430 we should not do this. We'll prepend the parent's fully qualified
11431 name when we create the partial symbol. */
11432
11433 real_pdi = struct_pdi;
11434 while (real_pdi->has_specification)
11435 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11436
11437 if (real_pdi->die_parent != NULL)
11438 return;
11439
11440 for (child_pdi = struct_pdi->die_child;
11441 child_pdi != NULL;
11442 child_pdi = child_pdi->die_sibling)
11443 {
11444 if (child_pdi->tag == DW_TAG_subprogram
11445 && child_pdi->linkage_name != NULL)
11446 {
11447 char *actual_class_name
11448 = language_class_name_from_physname (cu->language_defn,
11449 child_pdi->linkage_name);
11450 if (actual_class_name != NULL)
11451 {
11452 struct_pdi->name
11453 = obsavestring (actual_class_name,
11454 strlen (actual_class_name),
11455 &cu->objfile->objfile_obstack);
11456 xfree (actual_class_name);
11457 }
11458 break;
11459 }
11460 }
11461 }
11462
11463 /* Adjust PART_DIE before generating a symbol for it. This function
11464 may set the is_external flag or change the DIE's name. */
11465
11466 static void
11467 fixup_partial_die (struct partial_die_info *part_die,
11468 struct dwarf2_cu *cu)
11469 {
11470 /* Once we've fixed up a die, there's no point in doing so again.
11471 This also avoids a memory leak if we were to call
11472 guess_partial_die_structure_name multiple times. */
11473 if (part_die->fixup_called)
11474 return;
11475
11476 /* If we found a reference attribute and the DIE has no name, try
11477 to find a name in the referred to DIE. */
11478
11479 if (part_die->name == NULL && part_die->has_specification)
11480 {
11481 struct partial_die_info *spec_die;
11482
11483 spec_die = find_partial_die (part_die->spec_offset, cu);
11484
11485 fixup_partial_die (spec_die, cu);
11486
11487 if (spec_die->name)
11488 {
11489 part_die->name = spec_die->name;
11490
11491 /* Copy DW_AT_external attribute if it is set. */
11492 if (spec_die->is_external)
11493 part_die->is_external = spec_die->is_external;
11494 }
11495 }
11496
11497 /* Set default names for some unnamed DIEs. */
11498
11499 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11500 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11501
11502 /* If there is no parent die to provide a namespace, and there are
11503 children, see if we can determine the namespace from their linkage
11504 name. */
11505 if (cu->language == language_cplus
11506 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11507 && part_die->die_parent == NULL
11508 && part_die->has_children
11509 && (part_die->tag == DW_TAG_class_type
11510 || part_die->tag == DW_TAG_structure_type
11511 || part_die->tag == DW_TAG_union_type))
11512 guess_partial_die_structure_name (part_die, cu);
11513
11514 /* GCC might emit a nameless struct or union that has a linkage
11515 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11516 if (part_die->name == NULL
11517 && (part_die->tag == DW_TAG_class_type
11518 || part_die->tag == DW_TAG_interface_type
11519 || part_die->tag == DW_TAG_structure_type
11520 || part_die->tag == DW_TAG_union_type)
11521 && part_die->linkage_name != NULL)
11522 {
11523 char *demangled;
11524
11525 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11526 if (demangled)
11527 {
11528 const char *base;
11529
11530 /* Strip any leading namespaces/classes, keep only the base name.
11531 DW_AT_name for named DIEs does not contain the prefixes. */
11532 base = strrchr (demangled, ':');
11533 if (base && base > demangled && base[-1] == ':')
11534 base++;
11535 else
11536 base = demangled;
11537
11538 part_die->name = obsavestring (base, strlen (base),
11539 &cu->objfile->objfile_obstack);
11540 xfree (demangled);
11541 }
11542 }
11543
11544 part_die->fixup_called = 1;
11545 }
11546
11547 /* Read an attribute value described by an attribute form. */
11548
11549 static gdb_byte *
11550 read_attribute_value (const struct die_reader_specs *reader,
11551 struct attribute *attr, unsigned form,
11552 gdb_byte *info_ptr)
11553 {
11554 struct dwarf2_cu *cu = reader->cu;
11555 bfd *abfd = reader->abfd;
11556 struct comp_unit_head *cu_header = &cu->header;
11557 unsigned int bytes_read;
11558 struct dwarf_block *blk;
11559
11560 attr->form = form;
11561 switch (form)
11562 {
11563 case DW_FORM_ref_addr:
11564 if (cu->header.version == 2)
11565 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11566 else
11567 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11568 &cu->header, &bytes_read);
11569 info_ptr += bytes_read;
11570 break;
11571 case DW_FORM_addr:
11572 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11573 info_ptr += bytes_read;
11574 break;
11575 case DW_FORM_block2:
11576 blk = dwarf_alloc_block (cu);
11577 blk->size = read_2_bytes (abfd, info_ptr);
11578 info_ptr += 2;
11579 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11580 info_ptr += blk->size;
11581 DW_BLOCK (attr) = blk;
11582 break;
11583 case DW_FORM_block4:
11584 blk = dwarf_alloc_block (cu);
11585 blk->size = read_4_bytes (abfd, info_ptr);
11586 info_ptr += 4;
11587 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11588 info_ptr += blk->size;
11589 DW_BLOCK (attr) = blk;
11590 break;
11591 case DW_FORM_data2:
11592 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11593 info_ptr += 2;
11594 break;
11595 case DW_FORM_data4:
11596 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11597 info_ptr += 4;
11598 break;
11599 case DW_FORM_data8:
11600 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11601 info_ptr += 8;
11602 break;
11603 case DW_FORM_sec_offset:
11604 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11605 info_ptr += bytes_read;
11606 break;
11607 case DW_FORM_string:
11608 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11609 DW_STRING_IS_CANONICAL (attr) = 0;
11610 info_ptr += bytes_read;
11611 break;
11612 case DW_FORM_strp:
11613 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11614 &bytes_read);
11615 DW_STRING_IS_CANONICAL (attr) = 0;
11616 info_ptr += bytes_read;
11617 break;
11618 case DW_FORM_exprloc:
11619 case DW_FORM_block:
11620 blk = dwarf_alloc_block (cu);
11621 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11622 info_ptr += bytes_read;
11623 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11624 info_ptr += blk->size;
11625 DW_BLOCK (attr) = blk;
11626 break;
11627 case DW_FORM_block1:
11628 blk = dwarf_alloc_block (cu);
11629 blk->size = read_1_byte (abfd, info_ptr);
11630 info_ptr += 1;
11631 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11632 info_ptr += blk->size;
11633 DW_BLOCK (attr) = blk;
11634 break;
11635 case DW_FORM_data1:
11636 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11637 info_ptr += 1;
11638 break;
11639 case DW_FORM_flag:
11640 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11641 info_ptr += 1;
11642 break;
11643 case DW_FORM_flag_present:
11644 DW_UNSND (attr) = 1;
11645 break;
11646 case DW_FORM_sdata:
11647 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11648 info_ptr += bytes_read;
11649 break;
11650 case DW_FORM_udata:
11651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11652 info_ptr += bytes_read;
11653 break;
11654 case DW_FORM_ref1:
11655 DW_UNSND (attr) = (cu->header.offset.sect_off
11656 + read_1_byte (abfd, info_ptr));
11657 info_ptr += 1;
11658 break;
11659 case DW_FORM_ref2:
11660 DW_UNSND (attr) = (cu->header.offset.sect_off
11661 + read_2_bytes (abfd, info_ptr));
11662 info_ptr += 2;
11663 break;
11664 case DW_FORM_ref4:
11665 DW_UNSND (attr) = (cu->header.offset.sect_off
11666 + read_4_bytes (abfd, info_ptr));
11667 info_ptr += 4;
11668 break;
11669 case DW_FORM_ref8:
11670 DW_UNSND (attr) = (cu->header.offset.sect_off
11671 + read_8_bytes (abfd, info_ptr));
11672 info_ptr += 8;
11673 break;
11674 case DW_FORM_ref_sig8:
11675 /* Convert the signature to something we can record in DW_UNSND
11676 for later lookup.
11677 NOTE: This is NULL if the type wasn't found. */
11678 DW_SIGNATURED_TYPE (attr) =
11679 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11680 info_ptr += 8;
11681 break;
11682 case DW_FORM_ref_udata:
11683 DW_UNSND (attr) = (cu->header.offset.sect_off
11684 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11685 info_ptr += bytes_read;
11686 break;
11687 case DW_FORM_indirect:
11688 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11689 info_ptr += bytes_read;
11690 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11691 break;
11692 case DW_FORM_GNU_addr_index:
11693 if (reader->dwo_file == NULL)
11694 {
11695 /* For now flag a hard error.
11696 Later we can turn this into a complaint. */
11697 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11698 dwarf_form_name (form),
11699 bfd_get_filename (abfd));
11700 }
11701 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11702 info_ptr += bytes_read;
11703 break;
11704 case DW_FORM_GNU_str_index:
11705 if (reader->dwo_file == NULL)
11706 {
11707 /* For now flag a hard error.
11708 Later we can turn this into a complaint if warranted. */
11709 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11710 dwarf_form_name (form),
11711 bfd_get_filename (abfd));
11712 }
11713 {
11714 ULONGEST str_index =
11715 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11716
11717 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11718 DW_STRING_IS_CANONICAL (attr) = 0;
11719 info_ptr += bytes_read;
11720 }
11721 break;
11722 default:
11723 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11724 dwarf_form_name (form),
11725 bfd_get_filename (abfd));
11726 }
11727
11728 /* We have seen instances where the compiler tried to emit a byte
11729 size attribute of -1 which ended up being encoded as an unsigned
11730 0xffffffff. Although 0xffffffff is technically a valid size value,
11731 an object of this size seems pretty unlikely so we can relatively
11732 safely treat these cases as if the size attribute was invalid and
11733 treat them as zero by default. */
11734 if (attr->name == DW_AT_byte_size
11735 && form == DW_FORM_data4
11736 && DW_UNSND (attr) >= 0xffffffff)
11737 {
11738 complaint
11739 (&symfile_complaints,
11740 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11741 hex_string (DW_UNSND (attr)));
11742 DW_UNSND (attr) = 0;
11743 }
11744
11745 return info_ptr;
11746 }
11747
11748 /* Read an attribute described by an abbreviated attribute. */
11749
11750 static gdb_byte *
11751 read_attribute (const struct die_reader_specs *reader,
11752 struct attribute *attr, struct attr_abbrev *abbrev,
11753 gdb_byte *info_ptr)
11754 {
11755 attr->name = abbrev->name;
11756 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11757 }
11758
11759 /* Read dwarf information from a buffer. */
11760
11761 static unsigned int
11762 read_1_byte (bfd *abfd, gdb_byte *buf)
11763 {
11764 return bfd_get_8 (abfd, buf);
11765 }
11766
11767 static int
11768 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11769 {
11770 return bfd_get_signed_8 (abfd, buf);
11771 }
11772
11773 static unsigned int
11774 read_2_bytes (bfd *abfd, gdb_byte *buf)
11775 {
11776 return bfd_get_16 (abfd, buf);
11777 }
11778
11779 static int
11780 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11781 {
11782 return bfd_get_signed_16 (abfd, buf);
11783 }
11784
11785 static unsigned int
11786 read_4_bytes (bfd *abfd, gdb_byte *buf)
11787 {
11788 return bfd_get_32 (abfd, buf);
11789 }
11790
11791 static int
11792 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11793 {
11794 return bfd_get_signed_32 (abfd, buf);
11795 }
11796
11797 static ULONGEST
11798 read_8_bytes (bfd *abfd, gdb_byte *buf)
11799 {
11800 return bfd_get_64 (abfd, buf);
11801 }
11802
11803 static CORE_ADDR
11804 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11805 unsigned int *bytes_read)
11806 {
11807 struct comp_unit_head *cu_header = &cu->header;
11808 CORE_ADDR retval = 0;
11809
11810 if (cu_header->signed_addr_p)
11811 {
11812 switch (cu_header->addr_size)
11813 {
11814 case 2:
11815 retval = bfd_get_signed_16 (abfd, buf);
11816 break;
11817 case 4:
11818 retval = bfd_get_signed_32 (abfd, buf);
11819 break;
11820 case 8:
11821 retval = bfd_get_signed_64 (abfd, buf);
11822 break;
11823 default:
11824 internal_error (__FILE__, __LINE__,
11825 _("read_address: bad switch, signed [in module %s]"),
11826 bfd_get_filename (abfd));
11827 }
11828 }
11829 else
11830 {
11831 switch (cu_header->addr_size)
11832 {
11833 case 2:
11834 retval = bfd_get_16 (abfd, buf);
11835 break;
11836 case 4:
11837 retval = bfd_get_32 (abfd, buf);
11838 break;
11839 case 8:
11840 retval = bfd_get_64 (abfd, buf);
11841 break;
11842 default:
11843 internal_error (__FILE__, __LINE__,
11844 _("read_address: bad switch, "
11845 "unsigned [in module %s]"),
11846 bfd_get_filename (abfd));
11847 }
11848 }
11849
11850 *bytes_read = cu_header->addr_size;
11851 return retval;
11852 }
11853
11854 /* Read the initial length from a section. The (draft) DWARF 3
11855 specification allows the initial length to take up either 4 bytes
11856 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11857 bytes describe the length and all offsets will be 8 bytes in length
11858 instead of 4.
11859
11860 An older, non-standard 64-bit format is also handled by this
11861 function. The older format in question stores the initial length
11862 as an 8-byte quantity without an escape value. Lengths greater
11863 than 2^32 aren't very common which means that the initial 4 bytes
11864 is almost always zero. Since a length value of zero doesn't make
11865 sense for the 32-bit format, this initial zero can be considered to
11866 be an escape value which indicates the presence of the older 64-bit
11867 format. As written, the code can't detect (old format) lengths
11868 greater than 4GB. If it becomes necessary to handle lengths
11869 somewhat larger than 4GB, we could allow other small values (such
11870 as the non-sensical values of 1, 2, and 3) to also be used as
11871 escape values indicating the presence of the old format.
11872
11873 The value returned via bytes_read should be used to increment the
11874 relevant pointer after calling read_initial_length().
11875
11876 [ Note: read_initial_length() and read_offset() are based on the
11877 document entitled "DWARF Debugging Information Format", revision
11878 3, draft 8, dated November 19, 2001. This document was obtained
11879 from:
11880
11881 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11882
11883 This document is only a draft and is subject to change. (So beware.)
11884
11885 Details regarding the older, non-standard 64-bit format were
11886 determined empirically by examining 64-bit ELF files produced by
11887 the SGI toolchain on an IRIX 6.5 machine.
11888
11889 - Kevin, July 16, 2002
11890 ] */
11891
11892 static LONGEST
11893 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11894 {
11895 LONGEST length = bfd_get_32 (abfd, buf);
11896
11897 if (length == 0xffffffff)
11898 {
11899 length = bfd_get_64 (abfd, buf + 4);
11900 *bytes_read = 12;
11901 }
11902 else if (length == 0)
11903 {
11904 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11905 length = bfd_get_64 (abfd, buf);
11906 *bytes_read = 8;
11907 }
11908 else
11909 {
11910 *bytes_read = 4;
11911 }
11912
11913 return length;
11914 }
11915
11916 /* Cover function for read_initial_length.
11917 Returns the length of the object at BUF, and stores the size of the
11918 initial length in *BYTES_READ and stores the size that offsets will be in
11919 *OFFSET_SIZE.
11920 If the initial length size is not equivalent to that specified in
11921 CU_HEADER then issue a complaint.
11922 This is useful when reading non-comp-unit headers. */
11923
11924 static LONGEST
11925 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11926 const struct comp_unit_head *cu_header,
11927 unsigned int *bytes_read,
11928 unsigned int *offset_size)
11929 {
11930 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11931
11932 gdb_assert (cu_header->initial_length_size == 4
11933 || cu_header->initial_length_size == 8
11934 || cu_header->initial_length_size == 12);
11935
11936 if (cu_header->initial_length_size != *bytes_read)
11937 complaint (&symfile_complaints,
11938 _("intermixed 32-bit and 64-bit DWARF sections"));
11939
11940 *offset_size = (*bytes_read == 4) ? 4 : 8;
11941 return length;
11942 }
11943
11944 /* Read an offset from the data stream. The size of the offset is
11945 given by cu_header->offset_size. */
11946
11947 static LONGEST
11948 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11949 unsigned int *bytes_read)
11950 {
11951 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11952
11953 *bytes_read = cu_header->offset_size;
11954 return offset;
11955 }
11956
11957 /* Read an offset from the data stream. */
11958
11959 static LONGEST
11960 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11961 {
11962 LONGEST retval = 0;
11963
11964 switch (offset_size)
11965 {
11966 case 4:
11967 retval = bfd_get_32 (abfd, buf);
11968 break;
11969 case 8:
11970 retval = bfd_get_64 (abfd, buf);
11971 break;
11972 default:
11973 internal_error (__FILE__, __LINE__,
11974 _("read_offset_1: bad switch [in module %s]"),
11975 bfd_get_filename (abfd));
11976 }
11977
11978 return retval;
11979 }
11980
11981 static gdb_byte *
11982 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
11983 {
11984 /* If the size of a host char is 8 bits, we can return a pointer
11985 to the buffer, otherwise we have to copy the data to a buffer
11986 allocated on the temporary obstack. */
11987 gdb_assert (HOST_CHAR_BIT == 8);
11988 return buf;
11989 }
11990
11991 static char *
11992 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11993 {
11994 /* If the size of a host char is 8 bits, we can return a pointer
11995 to the string, otherwise we have to copy the string to a buffer
11996 allocated on the temporary obstack. */
11997 gdb_assert (HOST_CHAR_BIT == 8);
11998 if (*buf == '\0')
11999 {
12000 *bytes_read_ptr = 1;
12001 return NULL;
12002 }
12003 *bytes_read_ptr = strlen ((char *) buf) + 1;
12004 return (char *) buf;
12005 }
12006
12007 static char *
12008 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12009 {
12010 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12011 if (dwarf2_per_objfile->str.buffer == NULL)
12012 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12013 bfd_get_filename (abfd));
12014 if (str_offset >= dwarf2_per_objfile->str.size)
12015 error (_("DW_FORM_strp pointing outside of "
12016 ".debug_str section [in module %s]"),
12017 bfd_get_filename (abfd));
12018 gdb_assert (HOST_CHAR_BIT == 8);
12019 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12020 return NULL;
12021 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12022 }
12023
12024 static char *
12025 read_indirect_string (bfd *abfd, gdb_byte *buf,
12026 const struct comp_unit_head *cu_header,
12027 unsigned int *bytes_read_ptr)
12028 {
12029 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12030
12031 return read_indirect_string_at_offset (abfd, str_offset);
12032 }
12033
12034 static ULONGEST
12035 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12036 {
12037 ULONGEST result;
12038 unsigned int num_read;
12039 int i, shift;
12040 unsigned char byte;
12041
12042 result = 0;
12043 shift = 0;
12044 num_read = 0;
12045 i = 0;
12046 while (1)
12047 {
12048 byte = bfd_get_8 (abfd, buf);
12049 buf++;
12050 num_read++;
12051 result |= ((ULONGEST) (byte & 127) << shift);
12052 if ((byte & 128) == 0)
12053 {
12054 break;
12055 }
12056 shift += 7;
12057 }
12058 *bytes_read_ptr = num_read;
12059 return result;
12060 }
12061
12062 static LONGEST
12063 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12064 {
12065 LONGEST result;
12066 int i, shift, num_read;
12067 unsigned char byte;
12068
12069 result = 0;
12070 shift = 0;
12071 num_read = 0;
12072 i = 0;
12073 while (1)
12074 {
12075 byte = bfd_get_8 (abfd, buf);
12076 buf++;
12077 num_read++;
12078 result |= ((LONGEST) (byte & 127) << shift);
12079 shift += 7;
12080 if ((byte & 128) == 0)
12081 {
12082 break;
12083 }
12084 }
12085 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12086 result |= -(((LONGEST) 1) << shift);
12087 *bytes_read_ptr = num_read;
12088 return result;
12089 }
12090
12091 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12092 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12093 ADDR_SIZE is the size of addresses from the CU header. */
12094
12095 static CORE_ADDR
12096 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12097 {
12098 struct objfile *objfile = dwarf2_per_objfile->objfile;
12099 bfd *abfd = objfile->obfd;
12100 const gdb_byte *info_ptr;
12101
12102 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12103 if (dwarf2_per_objfile->addr.buffer == NULL)
12104 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12105 objfile->name);
12106 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12107 error (_("DW_FORM_addr_index pointing outside of "
12108 ".debug_addr section [in module %s]"),
12109 objfile->name);
12110 info_ptr = (dwarf2_per_objfile->addr.buffer
12111 + addr_base + addr_index * addr_size);
12112 if (addr_size == 4)
12113 return bfd_get_32 (abfd, info_ptr);
12114 else
12115 return bfd_get_64 (abfd, info_ptr);
12116 }
12117
12118 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12119
12120 static CORE_ADDR
12121 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12122 {
12123 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12124 }
12125
12126 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12127
12128 static CORE_ADDR
12129 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12130 unsigned int *bytes_read)
12131 {
12132 bfd *abfd = cu->objfile->obfd;
12133 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12134
12135 return read_addr_index (cu, addr_index);
12136 }
12137
12138 /* Data structure to pass results from dwarf2_read_addr_index_reader
12139 back to dwarf2_read_addr_index. */
12140
12141 struct dwarf2_read_addr_index_data
12142 {
12143 ULONGEST addr_base;
12144 int addr_size;
12145 };
12146
12147 /* die_reader_func for dwarf2_read_addr_index. */
12148
12149 static void
12150 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12151 gdb_byte *info_ptr,
12152 struct die_info *comp_unit_die,
12153 int has_children,
12154 void *data)
12155 {
12156 struct dwarf2_cu *cu = reader->cu;
12157 struct dwarf2_read_addr_index_data *aidata =
12158 (struct dwarf2_read_addr_index_data *) data;
12159
12160 aidata->addr_base = cu->addr_base;
12161 aidata->addr_size = cu->header.addr_size;
12162 }
12163
12164 /* Given an index in .debug_addr, fetch the value.
12165 NOTE: This can be called during dwarf expression evaluation,
12166 long after the debug information has been read, and thus per_cu->cu
12167 may no longer exist. */
12168
12169 CORE_ADDR
12170 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12171 unsigned int addr_index)
12172 {
12173 struct objfile *objfile = per_cu->objfile;
12174 struct dwarf2_cu *cu = per_cu->cu;
12175 ULONGEST addr_base;
12176 int addr_size;
12177
12178 /* This is intended to be called from outside this file. */
12179 dw2_setup (objfile);
12180
12181 /* We need addr_base and addr_size.
12182 If we don't have PER_CU->cu, we have to get it.
12183 Nasty, but the alternative is storing the needed info in PER_CU,
12184 which at this point doesn't seem justified: it's not clear how frequently
12185 it would get used and it would increase the size of every PER_CU.
12186 Entry points like dwarf2_per_cu_addr_size do a similar thing
12187 so we're not in uncharted territory here.
12188 Alas we need to be a bit more complicated as addr_base is contained
12189 in the DIE.
12190
12191 We don't need to read the entire CU(/TU).
12192 We just need the header and top level die.
12193 IWBN to use the aging mechanism to let us lazily later discard the CU.
12194 See however init_cutu_and_read_dies_simple. */
12195
12196 if (cu != NULL)
12197 {
12198 addr_base = cu->addr_base;
12199 addr_size = cu->header.addr_size;
12200 }
12201 else
12202 {
12203 struct dwarf2_read_addr_index_data aidata;
12204
12205 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12206 &aidata);
12207 addr_base = aidata.addr_base;
12208 addr_size = aidata.addr_size;
12209 }
12210
12211 return read_addr_index_1 (addr_index, addr_base, addr_size);
12212 }
12213
12214 /* Given a DW_AT_str_index, fetch the string. */
12215
12216 static char *
12217 read_str_index (const struct die_reader_specs *reader,
12218 struct dwarf2_cu *cu, ULONGEST str_index)
12219 {
12220 struct objfile *objfile = dwarf2_per_objfile->objfile;
12221 const char *dwo_name = objfile->name;
12222 bfd *abfd = objfile->obfd;
12223 struct dwo_sections *sections = &reader->dwo_file->sections;
12224 gdb_byte *info_ptr;
12225 ULONGEST str_offset;
12226
12227 dwarf2_read_section (objfile, &sections->str);
12228 dwarf2_read_section (objfile, &sections->str_offsets);
12229 if (sections->str.buffer == NULL)
12230 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12231 " in CU at offset 0x%lx [in module %s]"),
12232 (long) cu->header.offset.sect_off, dwo_name);
12233 if (sections->str_offsets.buffer == NULL)
12234 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12235 " in CU at offset 0x%lx [in module %s]"),
12236 (long) cu->header.offset.sect_off, dwo_name);
12237 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12238 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12239 " section in CU at offset 0x%lx [in module %s]"),
12240 (long) cu->header.offset.sect_off, dwo_name);
12241 info_ptr = (sections->str_offsets.buffer
12242 + str_index * cu->header.offset_size);
12243 if (cu->header.offset_size == 4)
12244 str_offset = bfd_get_32 (abfd, info_ptr);
12245 else
12246 str_offset = bfd_get_64 (abfd, info_ptr);
12247 if (str_offset >= sections->str.size)
12248 error (_("Offset from DW_FORM_str_index pointing outside of"
12249 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12250 (long) cu->header.offset.sect_off, dwo_name);
12251 return (char *) (sections->str.buffer + str_offset);
12252 }
12253
12254 /* Return a pointer to just past the end of an LEB128 number in BUF. */
12255
12256 static gdb_byte *
12257 skip_leb128 (bfd *abfd, gdb_byte *buf)
12258 {
12259 int byte;
12260
12261 while (1)
12262 {
12263 byte = bfd_get_8 (abfd, buf);
12264 buf++;
12265 if ((byte & 128) == 0)
12266 return buf;
12267 }
12268 }
12269
12270 /* Return the length of an LEB128 number in BUF. */
12271
12272 static int
12273 leb128_size (const gdb_byte *buf)
12274 {
12275 const gdb_byte *begin = buf;
12276 gdb_byte byte;
12277
12278 while (1)
12279 {
12280 byte = *buf++;
12281 if ((byte & 128) == 0)
12282 return buf - begin;
12283 }
12284 }
12285
12286 static void
12287 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12288 {
12289 switch (lang)
12290 {
12291 case DW_LANG_C89:
12292 case DW_LANG_C99:
12293 case DW_LANG_C:
12294 cu->language = language_c;
12295 break;
12296 case DW_LANG_C_plus_plus:
12297 cu->language = language_cplus;
12298 break;
12299 case DW_LANG_D:
12300 cu->language = language_d;
12301 break;
12302 case DW_LANG_Fortran77:
12303 case DW_LANG_Fortran90:
12304 case DW_LANG_Fortran95:
12305 cu->language = language_fortran;
12306 break;
12307 case DW_LANG_Go:
12308 cu->language = language_go;
12309 break;
12310 case DW_LANG_Mips_Assembler:
12311 cu->language = language_asm;
12312 break;
12313 case DW_LANG_Java:
12314 cu->language = language_java;
12315 break;
12316 case DW_LANG_Ada83:
12317 case DW_LANG_Ada95:
12318 cu->language = language_ada;
12319 break;
12320 case DW_LANG_Modula2:
12321 cu->language = language_m2;
12322 break;
12323 case DW_LANG_Pascal83:
12324 cu->language = language_pascal;
12325 break;
12326 case DW_LANG_ObjC:
12327 cu->language = language_objc;
12328 break;
12329 case DW_LANG_Cobol74:
12330 case DW_LANG_Cobol85:
12331 default:
12332 cu->language = language_minimal;
12333 break;
12334 }
12335 cu->language_defn = language_def (cu->language);
12336 }
12337
12338 /* Return the named attribute or NULL if not there. */
12339
12340 static struct attribute *
12341 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12342 {
12343 for (;;)
12344 {
12345 unsigned int i;
12346 struct attribute *spec = NULL;
12347
12348 for (i = 0; i < die->num_attrs; ++i)
12349 {
12350 if (die->attrs[i].name == name)
12351 return &die->attrs[i];
12352 if (die->attrs[i].name == DW_AT_specification
12353 || die->attrs[i].name == DW_AT_abstract_origin)
12354 spec = &die->attrs[i];
12355 }
12356
12357 if (!spec)
12358 break;
12359
12360 die = follow_die_ref (die, spec, &cu);
12361 }
12362
12363 return NULL;
12364 }
12365
12366 /* Return the named attribute or NULL if not there,
12367 but do not follow DW_AT_specification, etc.
12368 This is for use in contexts where we're reading .debug_types dies.
12369 Following DW_AT_specification, DW_AT_abstract_origin will take us
12370 back up the chain, and we want to go down. */
12371
12372 static struct attribute *
12373 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12374 struct dwarf2_cu *cu)
12375 {
12376 unsigned int i;
12377
12378 for (i = 0; i < die->num_attrs; ++i)
12379 if (die->attrs[i].name == name)
12380 return &die->attrs[i];
12381
12382 return NULL;
12383 }
12384
12385 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12386 and holds a non-zero value. This function should only be used for
12387 DW_FORM_flag or DW_FORM_flag_present attributes. */
12388
12389 static int
12390 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12391 {
12392 struct attribute *attr = dwarf2_attr (die, name, cu);
12393
12394 return (attr && DW_UNSND (attr));
12395 }
12396
12397 static int
12398 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12399 {
12400 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12401 which value is non-zero. However, we have to be careful with
12402 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12403 (via dwarf2_flag_true_p) follows this attribute. So we may
12404 end up accidently finding a declaration attribute that belongs
12405 to a different DIE referenced by the specification attribute,
12406 even though the given DIE does not have a declaration attribute. */
12407 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12408 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12409 }
12410
12411 /* Return the die giving the specification for DIE, if there is
12412 one. *SPEC_CU is the CU containing DIE on input, and the CU
12413 containing the return value on output. If there is no
12414 specification, but there is an abstract origin, that is
12415 returned. */
12416
12417 static struct die_info *
12418 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12419 {
12420 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12421 *spec_cu);
12422
12423 if (spec_attr == NULL)
12424 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12425
12426 if (spec_attr == NULL)
12427 return NULL;
12428 else
12429 return follow_die_ref (die, spec_attr, spec_cu);
12430 }
12431
12432 /* Free the line_header structure *LH, and any arrays and strings it
12433 refers to.
12434 NOTE: This is also used as a "cleanup" function. */
12435
12436 static void
12437 free_line_header (struct line_header *lh)
12438 {
12439 if (lh->standard_opcode_lengths)
12440 xfree (lh->standard_opcode_lengths);
12441
12442 /* Remember that all the lh->file_names[i].name pointers are
12443 pointers into debug_line_buffer, and don't need to be freed. */
12444 if (lh->file_names)
12445 xfree (lh->file_names);
12446
12447 /* Similarly for the include directory names. */
12448 if (lh->include_dirs)
12449 xfree (lh->include_dirs);
12450
12451 xfree (lh);
12452 }
12453
12454 /* Add an entry to LH's include directory table. */
12455
12456 static void
12457 add_include_dir (struct line_header *lh, char *include_dir)
12458 {
12459 /* Grow the array if necessary. */
12460 if (lh->include_dirs_size == 0)
12461 {
12462 lh->include_dirs_size = 1; /* for testing */
12463 lh->include_dirs = xmalloc (lh->include_dirs_size
12464 * sizeof (*lh->include_dirs));
12465 }
12466 else if (lh->num_include_dirs >= lh->include_dirs_size)
12467 {
12468 lh->include_dirs_size *= 2;
12469 lh->include_dirs = xrealloc (lh->include_dirs,
12470 (lh->include_dirs_size
12471 * sizeof (*lh->include_dirs)));
12472 }
12473
12474 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12475 }
12476
12477 /* Add an entry to LH's file name table. */
12478
12479 static void
12480 add_file_name (struct line_header *lh,
12481 char *name,
12482 unsigned int dir_index,
12483 unsigned int mod_time,
12484 unsigned int length)
12485 {
12486 struct file_entry *fe;
12487
12488 /* Grow the array if necessary. */
12489 if (lh->file_names_size == 0)
12490 {
12491 lh->file_names_size = 1; /* for testing */
12492 lh->file_names = xmalloc (lh->file_names_size
12493 * sizeof (*lh->file_names));
12494 }
12495 else if (lh->num_file_names >= lh->file_names_size)
12496 {
12497 lh->file_names_size *= 2;
12498 lh->file_names = xrealloc (lh->file_names,
12499 (lh->file_names_size
12500 * sizeof (*lh->file_names)));
12501 }
12502
12503 fe = &lh->file_names[lh->num_file_names++];
12504 fe->name = name;
12505 fe->dir_index = dir_index;
12506 fe->mod_time = mod_time;
12507 fe->length = length;
12508 fe->included_p = 0;
12509 fe->symtab = NULL;
12510 }
12511
12512 /* Read the statement program header starting at OFFSET in
12513 .debug_line, or .debug_line.dwo. Return a pointer
12514 to a struct line_header, allocated using xmalloc.
12515
12516 NOTE: the strings in the include directory and file name tables of
12517 the returned object point into the dwarf line section buffer,
12518 and must not be freed. */
12519
12520 static struct line_header *
12521 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12522 {
12523 struct cleanup *back_to;
12524 struct line_header *lh;
12525 gdb_byte *line_ptr;
12526 unsigned int bytes_read, offset_size;
12527 int i;
12528 char *cur_dir, *cur_file;
12529 struct dwarf2_section_info *section;
12530 bfd *abfd;
12531
12532 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12533 DWO file. */
12534 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12535 section = &cu->dwo_unit->dwo_file->sections.line;
12536 else
12537 section = &dwarf2_per_objfile->line;
12538
12539 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12540 if (section->buffer == NULL)
12541 {
12542 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12543 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12544 else
12545 complaint (&symfile_complaints, _("missing .debug_line section"));
12546 return 0;
12547 }
12548
12549 /* We can't do this until we know the section is non-empty.
12550 Only then do we know we have such a section. */
12551 abfd = section->asection->owner;
12552
12553 /* Make sure that at least there's room for the total_length field.
12554 That could be 12 bytes long, but we're just going to fudge that. */
12555 if (offset + 4 >= section->size)
12556 {
12557 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12558 return 0;
12559 }
12560
12561 lh = xmalloc (sizeof (*lh));
12562 memset (lh, 0, sizeof (*lh));
12563 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12564 (void *) lh);
12565
12566 line_ptr = section->buffer + offset;
12567
12568 /* Read in the header. */
12569 lh->total_length =
12570 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12571 &bytes_read, &offset_size);
12572 line_ptr += bytes_read;
12573 if (line_ptr + lh->total_length > (section->buffer + section->size))
12574 {
12575 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12576 return 0;
12577 }
12578 lh->statement_program_end = line_ptr + lh->total_length;
12579 lh->version = read_2_bytes (abfd, line_ptr);
12580 line_ptr += 2;
12581 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12582 line_ptr += offset_size;
12583 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12584 line_ptr += 1;
12585 if (lh->version >= 4)
12586 {
12587 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12588 line_ptr += 1;
12589 }
12590 else
12591 lh->maximum_ops_per_instruction = 1;
12592
12593 if (lh->maximum_ops_per_instruction == 0)
12594 {
12595 lh->maximum_ops_per_instruction = 1;
12596 complaint (&symfile_complaints,
12597 _("invalid maximum_ops_per_instruction "
12598 "in `.debug_line' section"));
12599 }
12600
12601 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12602 line_ptr += 1;
12603 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12604 line_ptr += 1;
12605 lh->line_range = read_1_byte (abfd, line_ptr);
12606 line_ptr += 1;
12607 lh->opcode_base = read_1_byte (abfd, line_ptr);
12608 line_ptr += 1;
12609 lh->standard_opcode_lengths
12610 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12611
12612 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12613 for (i = 1; i < lh->opcode_base; ++i)
12614 {
12615 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12616 line_ptr += 1;
12617 }
12618
12619 /* Read directory table. */
12620 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12621 {
12622 line_ptr += bytes_read;
12623 add_include_dir (lh, cur_dir);
12624 }
12625 line_ptr += bytes_read;
12626
12627 /* Read file name table. */
12628 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12629 {
12630 unsigned int dir_index, mod_time, length;
12631
12632 line_ptr += bytes_read;
12633 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12634 line_ptr += bytes_read;
12635 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12636 line_ptr += bytes_read;
12637 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12638 line_ptr += bytes_read;
12639
12640 add_file_name (lh, cur_file, dir_index, mod_time, length);
12641 }
12642 line_ptr += bytes_read;
12643 lh->statement_program_start = line_ptr;
12644
12645 if (line_ptr > (section->buffer + section->size))
12646 complaint (&symfile_complaints,
12647 _("line number info header doesn't "
12648 "fit in `.debug_line' section"));
12649
12650 discard_cleanups (back_to);
12651 return lh;
12652 }
12653
12654 /* Subroutine of dwarf_decode_lines to simplify it.
12655 Return the file name of the psymtab for included file FILE_INDEX
12656 in line header LH of PST.
12657 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12658 If space for the result is malloc'd, it will be freed by a cleanup.
12659 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12660
12661 static char *
12662 psymtab_include_file_name (const struct line_header *lh, int file_index,
12663 const struct partial_symtab *pst,
12664 const char *comp_dir)
12665 {
12666 const struct file_entry fe = lh->file_names [file_index];
12667 char *include_name = fe.name;
12668 char *include_name_to_compare = include_name;
12669 char *dir_name = NULL;
12670 const char *pst_filename;
12671 char *copied_name = NULL;
12672 int file_is_pst;
12673
12674 if (fe.dir_index)
12675 dir_name = lh->include_dirs[fe.dir_index - 1];
12676
12677 if (!IS_ABSOLUTE_PATH (include_name)
12678 && (dir_name != NULL || comp_dir != NULL))
12679 {
12680 /* Avoid creating a duplicate psymtab for PST.
12681 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12682 Before we do the comparison, however, we need to account
12683 for DIR_NAME and COMP_DIR.
12684 First prepend dir_name (if non-NULL). If we still don't
12685 have an absolute path prepend comp_dir (if non-NULL).
12686 However, the directory we record in the include-file's
12687 psymtab does not contain COMP_DIR (to match the
12688 corresponding symtab(s)).
12689
12690 Example:
12691
12692 bash$ cd /tmp
12693 bash$ gcc -g ./hello.c
12694 include_name = "hello.c"
12695 dir_name = "."
12696 DW_AT_comp_dir = comp_dir = "/tmp"
12697 DW_AT_name = "./hello.c" */
12698
12699 if (dir_name != NULL)
12700 {
12701 include_name = concat (dir_name, SLASH_STRING,
12702 include_name, (char *)NULL);
12703 include_name_to_compare = include_name;
12704 make_cleanup (xfree, include_name);
12705 }
12706 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12707 {
12708 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12709 include_name, (char *)NULL);
12710 }
12711 }
12712
12713 pst_filename = pst->filename;
12714 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12715 {
12716 copied_name = concat (pst->dirname, SLASH_STRING,
12717 pst_filename, (char *)NULL);
12718 pst_filename = copied_name;
12719 }
12720
12721 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12722
12723 if (include_name_to_compare != include_name)
12724 xfree (include_name_to_compare);
12725 if (copied_name != NULL)
12726 xfree (copied_name);
12727
12728 if (file_is_pst)
12729 return NULL;
12730 return include_name;
12731 }
12732
12733 /* Ignore this record_line request. */
12734
12735 static void
12736 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12737 {
12738 return;
12739 }
12740
12741 /* Subroutine of dwarf_decode_lines to simplify it.
12742 Process the line number information in LH. */
12743
12744 static void
12745 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12746 struct dwarf2_cu *cu, struct partial_symtab *pst)
12747 {
12748 gdb_byte *line_ptr, *extended_end;
12749 gdb_byte *line_end;
12750 unsigned int bytes_read, extended_len;
12751 unsigned char op_code, extended_op, adj_opcode;
12752 CORE_ADDR baseaddr;
12753 struct objfile *objfile = cu->objfile;
12754 bfd *abfd = objfile->obfd;
12755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12756 const int decode_for_pst_p = (pst != NULL);
12757 struct subfile *last_subfile = NULL;
12758 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12759 = record_line;
12760
12761 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12762
12763 line_ptr = lh->statement_program_start;
12764 line_end = lh->statement_program_end;
12765
12766 /* Read the statement sequences until there's nothing left. */
12767 while (line_ptr < line_end)
12768 {
12769 /* state machine registers */
12770 CORE_ADDR address = 0;
12771 unsigned int file = 1;
12772 unsigned int line = 1;
12773 unsigned int column = 0;
12774 int is_stmt = lh->default_is_stmt;
12775 int basic_block = 0;
12776 int end_sequence = 0;
12777 CORE_ADDR addr;
12778 unsigned char op_index = 0;
12779
12780 if (!decode_for_pst_p && lh->num_file_names >= file)
12781 {
12782 /* Start a subfile for the current file of the state machine. */
12783 /* lh->include_dirs and lh->file_names are 0-based, but the
12784 directory and file name numbers in the statement program
12785 are 1-based. */
12786 struct file_entry *fe = &lh->file_names[file - 1];
12787 char *dir = NULL;
12788
12789 if (fe->dir_index)
12790 dir = lh->include_dirs[fe->dir_index - 1];
12791
12792 dwarf2_start_subfile (fe->name, dir, comp_dir);
12793 }
12794
12795 /* Decode the table. */
12796 while (!end_sequence)
12797 {
12798 op_code = read_1_byte (abfd, line_ptr);
12799 line_ptr += 1;
12800 if (line_ptr > line_end)
12801 {
12802 dwarf2_debug_line_missing_end_sequence_complaint ();
12803 break;
12804 }
12805
12806 if (op_code >= lh->opcode_base)
12807 {
12808 /* Special operand. */
12809 adj_opcode = op_code - lh->opcode_base;
12810 address += (((op_index + (adj_opcode / lh->line_range))
12811 / lh->maximum_ops_per_instruction)
12812 * lh->minimum_instruction_length);
12813 op_index = ((op_index + (adj_opcode / lh->line_range))
12814 % lh->maximum_ops_per_instruction);
12815 line += lh->line_base + (adj_opcode % lh->line_range);
12816 if (lh->num_file_names < file || file == 0)
12817 dwarf2_debug_line_missing_file_complaint ();
12818 /* For now we ignore lines not starting on an
12819 instruction boundary. */
12820 else if (op_index == 0)
12821 {
12822 lh->file_names[file - 1].included_p = 1;
12823 if (!decode_for_pst_p && is_stmt)
12824 {
12825 if (last_subfile != current_subfile)
12826 {
12827 addr = gdbarch_addr_bits_remove (gdbarch, address);
12828 if (last_subfile)
12829 (*p_record_line) (last_subfile, 0, addr);
12830 last_subfile = current_subfile;
12831 }
12832 /* Append row to matrix using current values. */
12833 addr = gdbarch_addr_bits_remove (gdbarch, address);
12834 (*p_record_line) (current_subfile, line, addr);
12835 }
12836 }
12837 basic_block = 0;
12838 }
12839 else switch (op_code)
12840 {
12841 case DW_LNS_extended_op:
12842 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12843 &bytes_read);
12844 line_ptr += bytes_read;
12845 extended_end = line_ptr + extended_len;
12846 extended_op = read_1_byte (abfd, line_ptr);
12847 line_ptr += 1;
12848 switch (extended_op)
12849 {
12850 case DW_LNE_end_sequence:
12851 p_record_line = record_line;
12852 end_sequence = 1;
12853 break;
12854 case DW_LNE_set_address:
12855 address = read_address (abfd, line_ptr, cu, &bytes_read);
12856
12857 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12858 {
12859 /* This line table is for a function which has been
12860 GCd by the linker. Ignore it. PR gdb/12528 */
12861
12862 long line_offset
12863 = line_ptr - dwarf2_per_objfile->line.buffer;
12864
12865 complaint (&symfile_complaints,
12866 _(".debug_line address at offset 0x%lx is 0 "
12867 "[in module %s]"),
12868 line_offset, objfile->name);
12869 p_record_line = noop_record_line;
12870 }
12871
12872 op_index = 0;
12873 line_ptr += bytes_read;
12874 address += baseaddr;
12875 break;
12876 case DW_LNE_define_file:
12877 {
12878 char *cur_file;
12879 unsigned int dir_index, mod_time, length;
12880
12881 cur_file = read_direct_string (abfd, line_ptr,
12882 &bytes_read);
12883 line_ptr += bytes_read;
12884 dir_index =
12885 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12886 line_ptr += bytes_read;
12887 mod_time =
12888 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12889 line_ptr += bytes_read;
12890 length =
12891 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12892 line_ptr += bytes_read;
12893 add_file_name (lh, cur_file, dir_index, mod_time, length);
12894 }
12895 break;
12896 case DW_LNE_set_discriminator:
12897 /* The discriminator is not interesting to the debugger;
12898 just ignore it. */
12899 line_ptr = extended_end;
12900 break;
12901 default:
12902 complaint (&symfile_complaints,
12903 _("mangled .debug_line section"));
12904 return;
12905 }
12906 /* Make sure that we parsed the extended op correctly. If e.g.
12907 we expected a different address size than the producer used,
12908 we may have read the wrong number of bytes. */
12909 if (line_ptr != extended_end)
12910 {
12911 complaint (&symfile_complaints,
12912 _("mangled .debug_line section"));
12913 return;
12914 }
12915 break;
12916 case DW_LNS_copy:
12917 if (lh->num_file_names < file || file == 0)
12918 dwarf2_debug_line_missing_file_complaint ();
12919 else
12920 {
12921 lh->file_names[file - 1].included_p = 1;
12922 if (!decode_for_pst_p && is_stmt)
12923 {
12924 if (last_subfile != current_subfile)
12925 {
12926 addr = gdbarch_addr_bits_remove (gdbarch, address);
12927 if (last_subfile)
12928 (*p_record_line) (last_subfile, 0, addr);
12929 last_subfile = current_subfile;
12930 }
12931 addr = gdbarch_addr_bits_remove (gdbarch, address);
12932 (*p_record_line) (current_subfile, line, addr);
12933 }
12934 }
12935 basic_block = 0;
12936 break;
12937 case DW_LNS_advance_pc:
12938 {
12939 CORE_ADDR adjust
12940 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12941
12942 address += (((op_index + adjust)
12943 / lh->maximum_ops_per_instruction)
12944 * lh->minimum_instruction_length);
12945 op_index = ((op_index + adjust)
12946 % lh->maximum_ops_per_instruction);
12947 line_ptr += bytes_read;
12948 }
12949 break;
12950 case DW_LNS_advance_line:
12951 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12952 line_ptr += bytes_read;
12953 break;
12954 case DW_LNS_set_file:
12955 {
12956 /* The arrays lh->include_dirs and lh->file_names are
12957 0-based, but the directory and file name numbers in
12958 the statement program are 1-based. */
12959 struct file_entry *fe;
12960 char *dir = NULL;
12961
12962 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12963 line_ptr += bytes_read;
12964 if (lh->num_file_names < file || file == 0)
12965 dwarf2_debug_line_missing_file_complaint ();
12966 else
12967 {
12968 fe = &lh->file_names[file - 1];
12969 if (fe->dir_index)
12970 dir = lh->include_dirs[fe->dir_index - 1];
12971 if (!decode_for_pst_p)
12972 {
12973 last_subfile = current_subfile;
12974 dwarf2_start_subfile (fe->name, dir, comp_dir);
12975 }
12976 }
12977 }
12978 break;
12979 case DW_LNS_set_column:
12980 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12981 line_ptr += bytes_read;
12982 break;
12983 case DW_LNS_negate_stmt:
12984 is_stmt = (!is_stmt);
12985 break;
12986 case DW_LNS_set_basic_block:
12987 basic_block = 1;
12988 break;
12989 /* Add to the address register of the state machine the
12990 address increment value corresponding to special opcode
12991 255. I.e., this value is scaled by the minimum
12992 instruction length since special opcode 255 would have
12993 scaled the increment. */
12994 case DW_LNS_const_add_pc:
12995 {
12996 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12997
12998 address += (((op_index + adjust)
12999 / lh->maximum_ops_per_instruction)
13000 * lh->minimum_instruction_length);
13001 op_index = ((op_index + adjust)
13002 % lh->maximum_ops_per_instruction);
13003 }
13004 break;
13005 case DW_LNS_fixed_advance_pc:
13006 address += read_2_bytes (abfd, line_ptr);
13007 op_index = 0;
13008 line_ptr += 2;
13009 break;
13010 default:
13011 {
13012 /* Unknown standard opcode, ignore it. */
13013 int i;
13014
13015 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13016 {
13017 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13018 line_ptr += bytes_read;
13019 }
13020 }
13021 }
13022 }
13023 if (lh->num_file_names < file || file == 0)
13024 dwarf2_debug_line_missing_file_complaint ();
13025 else
13026 {
13027 lh->file_names[file - 1].included_p = 1;
13028 if (!decode_for_pst_p)
13029 {
13030 addr = gdbarch_addr_bits_remove (gdbarch, address);
13031 (*p_record_line) (current_subfile, 0, addr);
13032 }
13033 }
13034 }
13035 }
13036
13037 /* Decode the Line Number Program (LNP) for the given line_header
13038 structure and CU. The actual information extracted and the type
13039 of structures created from the LNP depends on the value of PST.
13040
13041 1. If PST is NULL, then this procedure uses the data from the program
13042 to create all necessary symbol tables, and their linetables.
13043
13044 2. If PST is not NULL, this procedure reads the program to determine
13045 the list of files included by the unit represented by PST, and
13046 builds all the associated partial symbol tables.
13047
13048 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13049 It is used for relative paths in the line table.
13050 NOTE: When processing partial symtabs (pst != NULL),
13051 comp_dir == pst->dirname.
13052
13053 NOTE: It is important that psymtabs have the same file name (via strcmp)
13054 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13055 symtab we don't use it in the name of the psymtabs we create.
13056 E.g. expand_line_sal requires this when finding psymtabs to expand.
13057 A good testcase for this is mb-inline.exp. */
13058
13059 static void
13060 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13061 struct dwarf2_cu *cu, struct partial_symtab *pst,
13062 int want_line_info)
13063 {
13064 struct objfile *objfile = cu->objfile;
13065 const int decode_for_pst_p = (pst != NULL);
13066 struct subfile *first_subfile = current_subfile;
13067
13068 if (want_line_info)
13069 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13070
13071 if (decode_for_pst_p)
13072 {
13073 int file_index;
13074
13075 /* Now that we're done scanning the Line Header Program, we can
13076 create the psymtab of each included file. */
13077 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13078 if (lh->file_names[file_index].included_p == 1)
13079 {
13080 char *include_name =
13081 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13082 if (include_name != NULL)
13083 dwarf2_create_include_psymtab (include_name, pst, objfile);
13084 }
13085 }
13086 else
13087 {
13088 /* Make sure a symtab is created for every file, even files
13089 which contain only variables (i.e. no code with associated
13090 line numbers). */
13091 int i;
13092
13093 for (i = 0; i < lh->num_file_names; i++)
13094 {
13095 char *dir = NULL;
13096 struct file_entry *fe;
13097
13098 fe = &lh->file_names[i];
13099 if (fe->dir_index)
13100 dir = lh->include_dirs[fe->dir_index - 1];
13101 dwarf2_start_subfile (fe->name, dir, comp_dir);
13102
13103 /* Skip the main file; we don't need it, and it must be
13104 allocated last, so that it will show up before the
13105 non-primary symtabs in the objfile's symtab list. */
13106 if (current_subfile == first_subfile)
13107 continue;
13108
13109 if (current_subfile->symtab == NULL)
13110 current_subfile->symtab = allocate_symtab (current_subfile->name,
13111 objfile);
13112 fe->symtab = current_subfile->symtab;
13113 }
13114 }
13115 }
13116
13117 /* Start a subfile for DWARF. FILENAME is the name of the file and
13118 DIRNAME the name of the source directory which contains FILENAME
13119 or NULL if not known. COMP_DIR is the compilation directory for the
13120 linetable's compilation unit or NULL if not known.
13121 This routine tries to keep line numbers from identical absolute and
13122 relative file names in a common subfile.
13123
13124 Using the `list' example from the GDB testsuite, which resides in
13125 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13126 of /srcdir/list0.c yields the following debugging information for list0.c:
13127
13128 DW_AT_name: /srcdir/list0.c
13129 DW_AT_comp_dir: /compdir
13130 files.files[0].name: list0.h
13131 files.files[0].dir: /srcdir
13132 files.files[1].name: list0.c
13133 files.files[1].dir: /srcdir
13134
13135 The line number information for list0.c has to end up in a single
13136 subfile, so that `break /srcdir/list0.c:1' works as expected.
13137 start_subfile will ensure that this happens provided that we pass the
13138 concatenation of files.files[1].dir and files.files[1].name as the
13139 subfile's name. */
13140
13141 static void
13142 dwarf2_start_subfile (char *filename, const char *dirname,
13143 const char *comp_dir)
13144 {
13145 char *fullname;
13146
13147 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13148 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13149 second argument to start_subfile. To be consistent, we do the
13150 same here. In order not to lose the line information directory,
13151 we concatenate it to the filename when it makes sense.
13152 Note that the Dwarf3 standard says (speaking of filenames in line
13153 information): ``The directory index is ignored for file names
13154 that represent full path names''. Thus ignoring dirname in the
13155 `else' branch below isn't an issue. */
13156
13157 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13158 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13159 else
13160 fullname = filename;
13161
13162 start_subfile (fullname, comp_dir);
13163
13164 if (fullname != filename)
13165 xfree (fullname);
13166 }
13167
13168 static void
13169 var_decode_location (struct attribute *attr, struct symbol *sym,
13170 struct dwarf2_cu *cu)
13171 {
13172 struct objfile *objfile = cu->objfile;
13173 struct comp_unit_head *cu_header = &cu->header;
13174
13175 /* NOTE drow/2003-01-30: There used to be a comment and some special
13176 code here to turn a symbol with DW_AT_external and a
13177 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13178 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13179 with some versions of binutils) where shared libraries could have
13180 relocations against symbols in their debug information - the
13181 minimal symbol would have the right address, but the debug info
13182 would not. It's no longer necessary, because we will explicitly
13183 apply relocations when we read in the debug information now. */
13184
13185 /* A DW_AT_location attribute with no contents indicates that a
13186 variable has been optimized away. */
13187 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13188 {
13189 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13190 return;
13191 }
13192
13193 /* Handle one degenerate form of location expression specially, to
13194 preserve GDB's previous behavior when section offsets are
13195 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13196 then mark this symbol as LOC_STATIC. */
13197
13198 if (attr_form_is_block (attr)
13199 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13200 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13201 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13202 && (DW_BLOCK (attr)->size
13203 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13204 {
13205 unsigned int dummy;
13206
13207 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13208 SYMBOL_VALUE_ADDRESS (sym) =
13209 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13210 else
13211 SYMBOL_VALUE_ADDRESS (sym) =
13212 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13213 SYMBOL_CLASS (sym) = LOC_STATIC;
13214 fixup_symbol_section (sym, objfile);
13215 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13216 SYMBOL_SECTION (sym));
13217 return;
13218 }
13219
13220 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13221 expression evaluator, and use LOC_COMPUTED only when necessary
13222 (i.e. when the value of a register or memory location is
13223 referenced, or a thread-local block, etc.). Then again, it might
13224 not be worthwhile. I'm assuming that it isn't unless performance
13225 or memory numbers show me otherwise. */
13226
13227 dwarf2_symbol_mark_computed (attr, sym, cu);
13228 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13229
13230 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13231 cu->has_loclist = 1;
13232 }
13233
13234 /* Given a pointer to a DWARF information entry, figure out if we need
13235 to make a symbol table entry for it, and if so, create a new entry
13236 and return a pointer to it.
13237 If TYPE is NULL, determine symbol type from the die, otherwise
13238 used the passed type.
13239 If SPACE is not NULL, use it to hold the new symbol. If it is
13240 NULL, allocate a new symbol on the objfile's obstack. */
13241
13242 static struct symbol *
13243 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13244 struct symbol *space)
13245 {
13246 struct objfile *objfile = cu->objfile;
13247 struct symbol *sym = NULL;
13248 char *name;
13249 struct attribute *attr = NULL;
13250 struct attribute *attr2 = NULL;
13251 CORE_ADDR baseaddr;
13252 struct pending **list_to_add = NULL;
13253
13254 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13255
13256 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13257
13258 name = dwarf2_name (die, cu);
13259 if (name)
13260 {
13261 const char *linkagename;
13262 int suppress_add = 0;
13263
13264 if (space)
13265 sym = space;
13266 else
13267 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13268 OBJSTAT (objfile, n_syms++);
13269
13270 /* Cache this symbol's name and the name's demangled form (if any). */
13271 SYMBOL_SET_LANGUAGE (sym, cu->language);
13272 linkagename = dwarf2_physname (name, die, cu);
13273 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13274
13275 /* Fortran does not have mangling standard and the mangling does differ
13276 between gfortran, iFort etc. */
13277 if (cu->language == language_fortran
13278 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13279 symbol_set_demangled_name (&(sym->ginfo),
13280 (char *) dwarf2_full_name (name, die, cu),
13281 NULL);
13282
13283 /* Default assumptions.
13284 Use the passed type or decode it from the die. */
13285 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13286 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13287 if (type != NULL)
13288 SYMBOL_TYPE (sym) = type;
13289 else
13290 SYMBOL_TYPE (sym) = die_type (die, cu);
13291 attr = dwarf2_attr (die,
13292 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13293 cu);
13294 if (attr)
13295 {
13296 SYMBOL_LINE (sym) = DW_UNSND (attr);
13297 }
13298
13299 attr = dwarf2_attr (die,
13300 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13301 cu);
13302 if (attr)
13303 {
13304 int file_index = DW_UNSND (attr);
13305
13306 if (cu->line_header == NULL
13307 || file_index > cu->line_header->num_file_names)
13308 complaint (&symfile_complaints,
13309 _("file index out of range"));
13310 else if (file_index > 0)
13311 {
13312 struct file_entry *fe;
13313
13314 fe = &cu->line_header->file_names[file_index - 1];
13315 SYMBOL_SYMTAB (sym) = fe->symtab;
13316 }
13317 }
13318
13319 switch (die->tag)
13320 {
13321 case DW_TAG_label:
13322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13323 if (attr)
13324 {
13325 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13326 }
13327 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13328 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13329 SYMBOL_CLASS (sym) = LOC_LABEL;
13330 add_symbol_to_list (sym, cu->list_in_scope);
13331 break;
13332 case DW_TAG_subprogram:
13333 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13334 finish_block. */
13335 SYMBOL_CLASS (sym) = LOC_BLOCK;
13336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13337 if ((attr2 && (DW_UNSND (attr2) != 0))
13338 || cu->language == language_ada)
13339 {
13340 /* Subprograms marked external are stored as a global symbol.
13341 Ada subprograms, whether marked external or not, are always
13342 stored as a global symbol, because we want to be able to
13343 access them globally. For instance, we want to be able
13344 to break on a nested subprogram without having to
13345 specify the context. */
13346 list_to_add = &global_symbols;
13347 }
13348 else
13349 {
13350 list_to_add = cu->list_in_scope;
13351 }
13352 break;
13353 case DW_TAG_inlined_subroutine:
13354 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13355 finish_block. */
13356 SYMBOL_CLASS (sym) = LOC_BLOCK;
13357 SYMBOL_INLINED (sym) = 1;
13358 list_to_add = cu->list_in_scope;
13359 break;
13360 case DW_TAG_template_value_param:
13361 suppress_add = 1;
13362 /* Fall through. */
13363 case DW_TAG_constant:
13364 case DW_TAG_variable:
13365 case DW_TAG_member:
13366 /* Compilation with minimal debug info may result in
13367 variables with missing type entries. Change the
13368 misleading `void' type to something sensible. */
13369 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13370 SYMBOL_TYPE (sym)
13371 = objfile_type (objfile)->nodebug_data_symbol;
13372
13373 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13374 /* In the case of DW_TAG_member, we should only be called for
13375 static const members. */
13376 if (die->tag == DW_TAG_member)
13377 {
13378 /* dwarf2_add_field uses die_is_declaration,
13379 so we do the same. */
13380 gdb_assert (die_is_declaration (die, cu));
13381 gdb_assert (attr);
13382 }
13383 if (attr)
13384 {
13385 dwarf2_const_value (attr, sym, cu);
13386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13387 if (!suppress_add)
13388 {
13389 if (attr2 && (DW_UNSND (attr2) != 0))
13390 list_to_add = &global_symbols;
13391 else
13392 list_to_add = cu->list_in_scope;
13393 }
13394 break;
13395 }
13396 attr = dwarf2_attr (die, DW_AT_location, cu);
13397 if (attr)
13398 {
13399 var_decode_location (attr, sym, cu);
13400 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13401 if (SYMBOL_CLASS (sym) == LOC_STATIC
13402 && SYMBOL_VALUE_ADDRESS (sym) == 0
13403 && !dwarf2_per_objfile->has_section_at_zero)
13404 {
13405 /* When a static variable is eliminated by the linker,
13406 the corresponding debug information is not stripped
13407 out, but the variable address is set to null;
13408 do not add such variables into symbol table. */
13409 }
13410 else if (attr2 && (DW_UNSND (attr2) != 0))
13411 {
13412 /* Workaround gfortran PR debug/40040 - it uses
13413 DW_AT_location for variables in -fPIC libraries which may
13414 get overriden by other libraries/executable and get
13415 a different address. Resolve it by the minimal symbol
13416 which may come from inferior's executable using copy
13417 relocation. Make this workaround only for gfortran as for
13418 other compilers GDB cannot guess the minimal symbol
13419 Fortran mangling kind. */
13420 if (cu->language == language_fortran && die->parent
13421 && die->parent->tag == DW_TAG_module
13422 && cu->producer
13423 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13424 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13425
13426 /* A variable with DW_AT_external is never static,
13427 but it may be block-scoped. */
13428 list_to_add = (cu->list_in_scope == &file_symbols
13429 ? &global_symbols : cu->list_in_scope);
13430 }
13431 else
13432 list_to_add = cu->list_in_scope;
13433 }
13434 else
13435 {
13436 /* We do not know the address of this symbol.
13437 If it is an external symbol and we have type information
13438 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13439 The address of the variable will then be determined from
13440 the minimal symbol table whenever the variable is
13441 referenced. */
13442 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13443 if (attr2 && (DW_UNSND (attr2) != 0)
13444 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13445 {
13446 /* A variable with DW_AT_external is never static, but it
13447 may be block-scoped. */
13448 list_to_add = (cu->list_in_scope == &file_symbols
13449 ? &global_symbols : cu->list_in_scope);
13450
13451 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13452 }
13453 else if (!die_is_declaration (die, cu))
13454 {
13455 /* Use the default LOC_OPTIMIZED_OUT class. */
13456 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13457 if (!suppress_add)
13458 list_to_add = cu->list_in_scope;
13459 }
13460 }
13461 break;
13462 case DW_TAG_formal_parameter:
13463 /* If we are inside a function, mark this as an argument. If
13464 not, we might be looking at an argument to an inlined function
13465 when we do not have enough information to show inlined frames;
13466 pretend it's a local variable in that case so that the user can
13467 still see it. */
13468 if (context_stack_depth > 0
13469 && context_stack[context_stack_depth - 1].name != NULL)
13470 SYMBOL_IS_ARGUMENT (sym) = 1;
13471 attr = dwarf2_attr (die, DW_AT_location, cu);
13472 if (attr)
13473 {
13474 var_decode_location (attr, sym, cu);
13475 }
13476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13477 if (attr)
13478 {
13479 dwarf2_const_value (attr, sym, cu);
13480 }
13481
13482 list_to_add = cu->list_in_scope;
13483 break;
13484 case DW_TAG_unspecified_parameters:
13485 /* From varargs functions; gdb doesn't seem to have any
13486 interest in this information, so just ignore it for now.
13487 (FIXME?) */
13488 break;
13489 case DW_TAG_template_type_param:
13490 suppress_add = 1;
13491 /* Fall through. */
13492 case DW_TAG_class_type:
13493 case DW_TAG_interface_type:
13494 case DW_TAG_structure_type:
13495 case DW_TAG_union_type:
13496 case DW_TAG_set_type:
13497 case DW_TAG_enumeration_type:
13498 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13499 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13500
13501 {
13502 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13503 really ever be static objects: otherwise, if you try
13504 to, say, break of a class's method and you're in a file
13505 which doesn't mention that class, it won't work unless
13506 the check for all static symbols in lookup_symbol_aux
13507 saves you. See the OtherFileClass tests in
13508 gdb.c++/namespace.exp. */
13509
13510 if (!suppress_add)
13511 {
13512 list_to_add = (cu->list_in_scope == &file_symbols
13513 && (cu->language == language_cplus
13514 || cu->language == language_java)
13515 ? &global_symbols : cu->list_in_scope);
13516
13517 /* The semantics of C++ state that "struct foo {
13518 ... }" also defines a typedef for "foo". A Java
13519 class declaration also defines a typedef for the
13520 class. */
13521 if (cu->language == language_cplus
13522 || cu->language == language_java
13523 || cu->language == language_ada)
13524 {
13525 /* The symbol's name is already allocated along
13526 with this objfile, so we don't need to
13527 duplicate it for the type. */
13528 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13529 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13530 }
13531 }
13532 }
13533 break;
13534 case DW_TAG_typedef:
13535 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13536 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13537 list_to_add = cu->list_in_scope;
13538 break;
13539 case DW_TAG_base_type:
13540 case DW_TAG_subrange_type:
13541 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13542 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13543 list_to_add = cu->list_in_scope;
13544 break;
13545 case DW_TAG_enumerator:
13546 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13547 if (attr)
13548 {
13549 dwarf2_const_value (attr, sym, cu);
13550 }
13551 {
13552 /* NOTE: carlton/2003-11-10: See comment above in the
13553 DW_TAG_class_type, etc. block. */
13554
13555 list_to_add = (cu->list_in_scope == &file_symbols
13556 && (cu->language == language_cplus
13557 || cu->language == language_java)
13558 ? &global_symbols : cu->list_in_scope);
13559 }
13560 break;
13561 case DW_TAG_namespace:
13562 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13563 list_to_add = &global_symbols;
13564 break;
13565 default:
13566 /* Not a tag we recognize. Hopefully we aren't processing
13567 trash data, but since we must specifically ignore things
13568 we don't recognize, there is nothing else we should do at
13569 this point. */
13570 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13571 dwarf_tag_name (die->tag));
13572 break;
13573 }
13574
13575 if (suppress_add)
13576 {
13577 sym->hash_next = objfile->template_symbols;
13578 objfile->template_symbols = sym;
13579 list_to_add = NULL;
13580 }
13581
13582 if (list_to_add != NULL)
13583 add_symbol_to_list (sym, list_to_add);
13584
13585 /* For the benefit of old versions of GCC, check for anonymous
13586 namespaces based on the demangled name. */
13587 if (!processing_has_namespace_info
13588 && cu->language == language_cplus)
13589 cp_scan_for_anonymous_namespaces (sym, objfile);
13590 }
13591 return (sym);
13592 }
13593
13594 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13595
13596 static struct symbol *
13597 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13598 {
13599 return new_symbol_full (die, type, cu, NULL);
13600 }
13601
13602 /* Given an attr with a DW_FORM_dataN value in host byte order,
13603 zero-extend it as appropriate for the symbol's type. The DWARF
13604 standard (v4) is not entirely clear about the meaning of using
13605 DW_FORM_dataN for a constant with a signed type, where the type is
13606 wider than the data. The conclusion of a discussion on the DWARF
13607 list was that this is unspecified. We choose to always zero-extend
13608 because that is the interpretation long in use by GCC. */
13609
13610 static gdb_byte *
13611 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13612 const char *name, struct obstack *obstack,
13613 struct dwarf2_cu *cu, LONGEST *value, int bits)
13614 {
13615 struct objfile *objfile = cu->objfile;
13616 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13617 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13618 LONGEST l = DW_UNSND (attr);
13619
13620 if (bits < sizeof (*value) * 8)
13621 {
13622 l &= ((LONGEST) 1 << bits) - 1;
13623 *value = l;
13624 }
13625 else if (bits == sizeof (*value) * 8)
13626 *value = l;
13627 else
13628 {
13629 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13630 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13631 return bytes;
13632 }
13633
13634 return NULL;
13635 }
13636
13637 /* Read a constant value from an attribute. Either set *VALUE, or if
13638 the value does not fit in *VALUE, set *BYTES - either already
13639 allocated on the objfile obstack, or newly allocated on OBSTACK,
13640 or, set *BATON, if we translated the constant to a location
13641 expression. */
13642
13643 static void
13644 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13645 const char *name, struct obstack *obstack,
13646 struct dwarf2_cu *cu,
13647 LONGEST *value, gdb_byte **bytes,
13648 struct dwarf2_locexpr_baton **baton)
13649 {
13650 struct objfile *objfile = cu->objfile;
13651 struct comp_unit_head *cu_header = &cu->header;
13652 struct dwarf_block *blk;
13653 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13654 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13655
13656 *value = 0;
13657 *bytes = NULL;
13658 *baton = NULL;
13659
13660 switch (attr->form)
13661 {
13662 case DW_FORM_addr:
13663 case DW_FORM_GNU_addr_index:
13664 {
13665 gdb_byte *data;
13666
13667 if (TYPE_LENGTH (type) != cu_header->addr_size)
13668 dwarf2_const_value_length_mismatch_complaint (name,
13669 cu_header->addr_size,
13670 TYPE_LENGTH (type));
13671 /* Symbols of this form are reasonably rare, so we just
13672 piggyback on the existing location code rather than writing
13673 a new implementation of symbol_computed_ops. */
13674 *baton = obstack_alloc (&objfile->objfile_obstack,
13675 sizeof (struct dwarf2_locexpr_baton));
13676 (*baton)->per_cu = cu->per_cu;
13677 gdb_assert ((*baton)->per_cu);
13678
13679 (*baton)->size = 2 + cu_header->addr_size;
13680 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13681 (*baton)->data = data;
13682
13683 data[0] = DW_OP_addr;
13684 store_unsigned_integer (&data[1], cu_header->addr_size,
13685 byte_order, DW_ADDR (attr));
13686 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13687 }
13688 break;
13689 case DW_FORM_string:
13690 case DW_FORM_strp:
13691 case DW_FORM_GNU_str_index:
13692 /* DW_STRING is already allocated on the objfile obstack, point
13693 directly to it. */
13694 *bytes = (gdb_byte *) DW_STRING (attr);
13695 break;
13696 case DW_FORM_block1:
13697 case DW_FORM_block2:
13698 case DW_FORM_block4:
13699 case DW_FORM_block:
13700 case DW_FORM_exprloc:
13701 blk = DW_BLOCK (attr);
13702 if (TYPE_LENGTH (type) != blk->size)
13703 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13704 TYPE_LENGTH (type));
13705 *bytes = blk->data;
13706 break;
13707
13708 /* The DW_AT_const_value attributes are supposed to carry the
13709 symbol's value "represented as it would be on the target
13710 architecture." By the time we get here, it's already been
13711 converted to host endianness, so we just need to sign- or
13712 zero-extend it as appropriate. */
13713 case DW_FORM_data1:
13714 *bytes = dwarf2_const_value_data (attr, type, name,
13715 obstack, cu, value, 8);
13716 break;
13717 case DW_FORM_data2:
13718 *bytes = dwarf2_const_value_data (attr, type, name,
13719 obstack, cu, value, 16);
13720 break;
13721 case DW_FORM_data4:
13722 *bytes = dwarf2_const_value_data (attr, type, name,
13723 obstack, cu, value, 32);
13724 break;
13725 case DW_FORM_data8:
13726 *bytes = dwarf2_const_value_data (attr, type, name,
13727 obstack, cu, value, 64);
13728 break;
13729
13730 case DW_FORM_sdata:
13731 *value = DW_SND (attr);
13732 break;
13733
13734 case DW_FORM_udata:
13735 *value = DW_UNSND (attr);
13736 break;
13737
13738 default:
13739 complaint (&symfile_complaints,
13740 _("unsupported const value attribute form: '%s'"),
13741 dwarf_form_name (attr->form));
13742 *value = 0;
13743 break;
13744 }
13745 }
13746
13747
13748 /* Copy constant value from an attribute to a symbol. */
13749
13750 static void
13751 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13752 struct dwarf2_cu *cu)
13753 {
13754 struct objfile *objfile = cu->objfile;
13755 struct comp_unit_head *cu_header = &cu->header;
13756 LONGEST value;
13757 gdb_byte *bytes;
13758 struct dwarf2_locexpr_baton *baton;
13759
13760 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13761 SYMBOL_PRINT_NAME (sym),
13762 &objfile->objfile_obstack, cu,
13763 &value, &bytes, &baton);
13764
13765 if (baton != NULL)
13766 {
13767 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13768 SYMBOL_LOCATION_BATON (sym) = baton;
13769 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13770 }
13771 else if (bytes != NULL)
13772 {
13773 SYMBOL_VALUE_BYTES (sym) = bytes;
13774 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13775 }
13776 else
13777 {
13778 SYMBOL_VALUE (sym) = value;
13779 SYMBOL_CLASS (sym) = LOC_CONST;
13780 }
13781 }
13782
13783 /* Return the type of the die in question using its DW_AT_type attribute. */
13784
13785 static struct type *
13786 die_type (struct die_info *die, struct dwarf2_cu *cu)
13787 {
13788 struct attribute *type_attr;
13789
13790 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13791 if (!type_attr)
13792 {
13793 /* A missing DW_AT_type represents a void type. */
13794 return objfile_type (cu->objfile)->builtin_void;
13795 }
13796
13797 return lookup_die_type (die, type_attr, cu);
13798 }
13799
13800 /* True iff CU's producer generates GNAT Ada auxiliary information
13801 that allows to find parallel types through that information instead
13802 of having to do expensive parallel lookups by type name. */
13803
13804 static int
13805 need_gnat_info (struct dwarf2_cu *cu)
13806 {
13807 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13808 of GNAT produces this auxiliary information, without any indication
13809 that it is produced. Part of enhancing the FSF version of GNAT
13810 to produce that information will be to put in place an indicator
13811 that we can use in order to determine whether the descriptive type
13812 info is available or not. One suggestion that has been made is
13813 to use a new attribute, attached to the CU die. For now, assume
13814 that the descriptive type info is not available. */
13815 return 0;
13816 }
13817
13818 /* Return the auxiliary type of the die in question using its
13819 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13820 attribute is not present. */
13821
13822 static struct type *
13823 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13824 {
13825 struct attribute *type_attr;
13826
13827 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13828 if (!type_attr)
13829 return NULL;
13830
13831 return lookup_die_type (die, type_attr, cu);
13832 }
13833
13834 /* If DIE has a descriptive_type attribute, then set the TYPE's
13835 descriptive type accordingly. */
13836
13837 static void
13838 set_descriptive_type (struct type *type, struct die_info *die,
13839 struct dwarf2_cu *cu)
13840 {
13841 struct type *descriptive_type = die_descriptive_type (die, cu);
13842
13843 if (descriptive_type)
13844 {
13845 ALLOCATE_GNAT_AUX_TYPE (type);
13846 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13847 }
13848 }
13849
13850 /* Return the containing type of the die in question using its
13851 DW_AT_containing_type attribute. */
13852
13853 static struct type *
13854 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13855 {
13856 struct attribute *type_attr;
13857
13858 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13859 if (!type_attr)
13860 error (_("Dwarf Error: Problem turning containing type into gdb type "
13861 "[in module %s]"), cu->objfile->name);
13862
13863 return lookup_die_type (die, type_attr, cu);
13864 }
13865
13866 /* Look up the type of DIE in CU using its type attribute ATTR.
13867 If there is no type substitute an error marker. */
13868
13869 static struct type *
13870 lookup_die_type (struct die_info *die, struct attribute *attr,
13871 struct dwarf2_cu *cu)
13872 {
13873 struct objfile *objfile = cu->objfile;
13874 struct type *this_type;
13875
13876 /* First see if we have it cached. */
13877
13878 if (is_ref_attr (attr))
13879 {
13880 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13881
13882 this_type = get_die_type_at_offset (offset, cu->per_cu);
13883 }
13884 else if (attr->form == DW_FORM_ref_sig8)
13885 {
13886 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13887
13888 /* sig_type will be NULL if the signatured type is missing from
13889 the debug info. */
13890 if (sig_type == NULL)
13891 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13892 "at 0x%x [in module %s]"),
13893 die->offset.sect_off, objfile->name);
13894
13895 gdb_assert (sig_type->per_cu.is_debug_types);
13896 /* If we haven't filled in type_offset_in_section yet, then we
13897 haven't read the type in yet. */
13898 this_type = NULL;
13899 if (sig_type->type_offset_in_section.sect_off != 0)
13900 {
13901 this_type =
13902 get_die_type_at_offset (sig_type->type_offset_in_section,
13903 &sig_type->per_cu);
13904 }
13905 }
13906 else
13907 {
13908 dump_die_for_error (die);
13909 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13910 dwarf_attr_name (attr->name), objfile->name);
13911 }
13912
13913 /* If not cached we need to read it in. */
13914
13915 if (this_type == NULL)
13916 {
13917 struct die_info *type_die;
13918 struct dwarf2_cu *type_cu = cu;
13919
13920 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13921 /* If we found the type now, it's probably because the type came
13922 from an inter-CU reference and the type's CU got expanded before
13923 ours. */
13924 this_type = get_die_type (type_die, type_cu);
13925 if (this_type == NULL)
13926 this_type = read_type_die_1 (type_die, type_cu);
13927 }
13928
13929 /* If we still don't have a type use an error marker. */
13930
13931 if (this_type == NULL)
13932 {
13933 char *message, *saved;
13934
13935 /* read_type_die already issued a complaint. */
13936 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13937 objfile->name,
13938 cu->header.offset.sect_off,
13939 die->offset.sect_off);
13940 saved = obstack_copy0 (&objfile->objfile_obstack,
13941 message, strlen (message));
13942 xfree (message);
13943
13944 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13945 }
13946
13947 return this_type;
13948 }
13949
13950 /* Return the type in DIE, CU.
13951 Returns NULL for invalid types.
13952
13953 This first does a lookup in the appropriate type_hash table,
13954 and only reads the die in if necessary.
13955
13956 NOTE: This can be called when reading in partial or full symbols. */
13957
13958 static struct type *
13959 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13960 {
13961 struct type *this_type;
13962
13963 this_type = get_die_type (die, cu);
13964 if (this_type)
13965 return this_type;
13966
13967 return read_type_die_1 (die, cu);
13968 }
13969
13970 /* Read the type in DIE, CU.
13971 Returns NULL for invalid types. */
13972
13973 static struct type *
13974 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13975 {
13976 struct type *this_type = NULL;
13977
13978 switch (die->tag)
13979 {
13980 case DW_TAG_class_type:
13981 case DW_TAG_interface_type:
13982 case DW_TAG_structure_type:
13983 case DW_TAG_union_type:
13984 this_type = read_structure_type (die, cu);
13985 break;
13986 case DW_TAG_enumeration_type:
13987 this_type = read_enumeration_type (die, cu);
13988 break;
13989 case DW_TAG_subprogram:
13990 case DW_TAG_subroutine_type:
13991 case DW_TAG_inlined_subroutine:
13992 this_type = read_subroutine_type (die, cu);
13993 break;
13994 case DW_TAG_array_type:
13995 this_type = read_array_type (die, cu);
13996 break;
13997 case DW_TAG_set_type:
13998 this_type = read_set_type (die, cu);
13999 break;
14000 case DW_TAG_pointer_type:
14001 this_type = read_tag_pointer_type (die, cu);
14002 break;
14003 case DW_TAG_ptr_to_member_type:
14004 this_type = read_tag_ptr_to_member_type (die, cu);
14005 break;
14006 case DW_TAG_reference_type:
14007 this_type = read_tag_reference_type (die, cu);
14008 break;
14009 case DW_TAG_const_type:
14010 this_type = read_tag_const_type (die, cu);
14011 break;
14012 case DW_TAG_volatile_type:
14013 this_type = read_tag_volatile_type (die, cu);
14014 break;
14015 case DW_TAG_string_type:
14016 this_type = read_tag_string_type (die, cu);
14017 break;
14018 case DW_TAG_typedef:
14019 this_type = read_typedef (die, cu);
14020 break;
14021 case DW_TAG_subrange_type:
14022 this_type = read_subrange_type (die, cu);
14023 break;
14024 case DW_TAG_base_type:
14025 this_type = read_base_type (die, cu);
14026 break;
14027 case DW_TAG_unspecified_type:
14028 this_type = read_unspecified_type (die, cu);
14029 break;
14030 case DW_TAG_namespace:
14031 this_type = read_namespace_type (die, cu);
14032 break;
14033 case DW_TAG_module:
14034 this_type = read_module_type (die, cu);
14035 break;
14036 default:
14037 complaint (&symfile_complaints,
14038 _("unexpected tag in read_type_die: '%s'"),
14039 dwarf_tag_name (die->tag));
14040 break;
14041 }
14042
14043 return this_type;
14044 }
14045
14046 /* See if we can figure out if the class lives in a namespace. We do
14047 this by looking for a member function; its demangled name will
14048 contain namespace info, if there is any.
14049 Return the computed name or NULL.
14050 Space for the result is allocated on the objfile's obstack.
14051 This is the full-die version of guess_partial_die_structure_name.
14052 In this case we know DIE has no useful parent. */
14053
14054 static char *
14055 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14056 {
14057 struct die_info *spec_die;
14058 struct dwarf2_cu *spec_cu;
14059 struct die_info *child;
14060
14061 spec_cu = cu;
14062 spec_die = die_specification (die, &spec_cu);
14063 if (spec_die != NULL)
14064 {
14065 die = spec_die;
14066 cu = spec_cu;
14067 }
14068
14069 for (child = die->child;
14070 child != NULL;
14071 child = child->sibling)
14072 {
14073 if (child->tag == DW_TAG_subprogram)
14074 {
14075 struct attribute *attr;
14076
14077 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14078 if (attr == NULL)
14079 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14080 if (attr != NULL)
14081 {
14082 char *actual_name
14083 = language_class_name_from_physname (cu->language_defn,
14084 DW_STRING (attr));
14085 char *name = NULL;
14086
14087 if (actual_name != NULL)
14088 {
14089 char *die_name = dwarf2_name (die, cu);
14090
14091 if (die_name != NULL
14092 && strcmp (die_name, actual_name) != 0)
14093 {
14094 /* Strip off the class name from the full name.
14095 We want the prefix. */
14096 int die_name_len = strlen (die_name);
14097 int actual_name_len = strlen (actual_name);
14098
14099 /* Test for '::' as a sanity check. */
14100 if (actual_name_len > die_name_len + 2
14101 && actual_name[actual_name_len
14102 - die_name_len - 1] == ':')
14103 name =
14104 obsavestring (actual_name,
14105 actual_name_len - die_name_len - 2,
14106 &cu->objfile->objfile_obstack);
14107 }
14108 }
14109 xfree (actual_name);
14110 return name;
14111 }
14112 }
14113 }
14114
14115 return NULL;
14116 }
14117
14118 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14119 prefix part in such case. See
14120 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14121
14122 static char *
14123 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14124 {
14125 struct attribute *attr;
14126 char *base;
14127
14128 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14129 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14130 return NULL;
14131
14132 attr = dwarf2_attr (die, DW_AT_name, cu);
14133 if (attr != NULL && DW_STRING (attr) != NULL)
14134 return NULL;
14135
14136 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14137 if (attr == NULL)
14138 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14139 if (attr == NULL || DW_STRING (attr) == NULL)
14140 return NULL;
14141
14142 /* dwarf2_name had to be already called. */
14143 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14144
14145 /* Strip the base name, keep any leading namespaces/classes. */
14146 base = strrchr (DW_STRING (attr), ':');
14147 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14148 return "";
14149
14150 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14151 &cu->objfile->objfile_obstack);
14152 }
14153
14154 /* Return the name of the namespace/class that DIE is defined within,
14155 or "" if we can't tell. The caller should not xfree the result.
14156
14157 For example, if we're within the method foo() in the following
14158 code:
14159
14160 namespace N {
14161 class C {
14162 void foo () {
14163 }
14164 };
14165 }
14166
14167 then determine_prefix on foo's die will return "N::C". */
14168
14169 static const char *
14170 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14171 {
14172 struct die_info *parent, *spec_die;
14173 struct dwarf2_cu *spec_cu;
14174 struct type *parent_type;
14175 char *retval;
14176
14177 if (cu->language != language_cplus && cu->language != language_java
14178 && cu->language != language_fortran)
14179 return "";
14180
14181 retval = anonymous_struct_prefix (die, cu);
14182 if (retval)
14183 return retval;
14184
14185 /* We have to be careful in the presence of DW_AT_specification.
14186 For example, with GCC 3.4, given the code
14187
14188 namespace N {
14189 void foo() {
14190 // Definition of N::foo.
14191 }
14192 }
14193
14194 then we'll have a tree of DIEs like this:
14195
14196 1: DW_TAG_compile_unit
14197 2: DW_TAG_namespace // N
14198 3: DW_TAG_subprogram // declaration of N::foo
14199 4: DW_TAG_subprogram // definition of N::foo
14200 DW_AT_specification // refers to die #3
14201
14202 Thus, when processing die #4, we have to pretend that we're in
14203 the context of its DW_AT_specification, namely the contex of die
14204 #3. */
14205 spec_cu = cu;
14206 spec_die = die_specification (die, &spec_cu);
14207 if (spec_die == NULL)
14208 parent = die->parent;
14209 else
14210 {
14211 parent = spec_die->parent;
14212 cu = spec_cu;
14213 }
14214
14215 if (parent == NULL)
14216 return "";
14217 else if (parent->building_fullname)
14218 {
14219 const char *name;
14220 const char *parent_name;
14221
14222 /* It has been seen on RealView 2.2 built binaries,
14223 DW_TAG_template_type_param types actually _defined_ as
14224 children of the parent class:
14225
14226 enum E {};
14227 template class <class Enum> Class{};
14228 Class<enum E> class_e;
14229
14230 1: DW_TAG_class_type (Class)
14231 2: DW_TAG_enumeration_type (E)
14232 3: DW_TAG_enumerator (enum1:0)
14233 3: DW_TAG_enumerator (enum2:1)
14234 ...
14235 2: DW_TAG_template_type_param
14236 DW_AT_type DW_FORM_ref_udata (E)
14237
14238 Besides being broken debug info, it can put GDB into an
14239 infinite loop. Consider:
14240
14241 When we're building the full name for Class<E>, we'll start
14242 at Class, and go look over its template type parameters,
14243 finding E. We'll then try to build the full name of E, and
14244 reach here. We're now trying to build the full name of E,
14245 and look over the parent DIE for containing scope. In the
14246 broken case, if we followed the parent DIE of E, we'd again
14247 find Class, and once again go look at its template type
14248 arguments, etc., etc. Simply don't consider such parent die
14249 as source-level parent of this die (it can't be, the language
14250 doesn't allow it), and break the loop here. */
14251 name = dwarf2_name (die, cu);
14252 parent_name = dwarf2_name (parent, cu);
14253 complaint (&symfile_complaints,
14254 _("template param type '%s' defined within parent '%s'"),
14255 name ? name : "<unknown>",
14256 parent_name ? parent_name : "<unknown>");
14257 return "";
14258 }
14259 else
14260 switch (parent->tag)
14261 {
14262 case DW_TAG_namespace:
14263 parent_type = read_type_die (parent, cu);
14264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14266 Work around this problem here. */
14267 if (cu->language == language_cplus
14268 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14269 return "";
14270 /* We give a name to even anonymous namespaces. */
14271 return TYPE_TAG_NAME (parent_type);
14272 case DW_TAG_class_type:
14273 case DW_TAG_interface_type:
14274 case DW_TAG_structure_type:
14275 case DW_TAG_union_type:
14276 case DW_TAG_module:
14277 parent_type = read_type_die (parent, cu);
14278 if (TYPE_TAG_NAME (parent_type) != NULL)
14279 return TYPE_TAG_NAME (parent_type);
14280 else
14281 /* An anonymous structure is only allowed non-static data
14282 members; no typedefs, no member functions, et cetera.
14283 So it does not need a prefix. */
14284 return "";
14285 case DW_TAG_compile_unit:
14286 case DW_TAG_partial_unit:
14287 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14288 if (cu->language == language_cplus
14289 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14290 && die->child != NULL
14291 && (die->tag == DW_TAG_class_type
14292 || die->tag == DW_TAG_structure_type
14293 || die->tag == DW_TAG_union_type))
14294 {
14295 char *name = guess_full_die_structure_name (die, cu);
14296 if (name != NULL)
14297 return name;
14298 }
14299 return "";
14300 default:
14301 return determine_prefix (parent, cu);
14302 }
14303 }
14304
14305 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14306 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14307 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14308 an obconcat, otherwise allocate storage for the result. The CU argument is
14309 used to determine the language and hence, the appropriate separator. */
14310
14311 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14312
14313 static char *
14314 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14315 int physname, struct dwarf2_cu *cu)
14316 {
14317 const char *lead = "";
14318 const char *sep;
14319
14320 if (suffix == NULL || suffix[0] == '\0'
14321 || prefix == NULL || prefix[0] == '\0')
14322 sep = "";
14323 else if (cu->language == language_java)
14324 sep = ".";
14325 else if (cu->language == language_fortran && physname)
14326 {
14327 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14328 DW_AT_MIPS_linkage_name is preferred and used instead. */
14329
14330 lead = "__";
14331 sep = "_MOD_";
14332 }
14333 else
14334 sep = "::";
14335
14336 if (prefix == NULL)
14337 prefix = "";
14338 if (suffix == NULL)
14339 suffix = "";
14340
14341 if (obs == NULL)
14342 {
14343 char *retval
14344 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14345
14346 strcpy (retval, lead);
14347 strcat (retval, prefix);
14348 strcat (retval, sep);
14349 strcat (retval, suffix);
14350 return retval;
14351 }
14352 else
14353 {
14354 /* We have an obstack. */
14355 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14356 }
14357 }
14358
14359 /* Return sibling of die, NULL if no sibling. */
14360
14361 static struct die_info *
14362 sibling_die (struct die_info *die)
14363 {
14364 return die->sibling;
14365 }
14366
14367 /* Get name of a die, return NULL if not found. */
14368
14369 static char *
14370 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14371 struct obstack *obstack)
14372 {
14373 if (name && cu->language == language_cplus)
14374 {
14375 char *canon_name = cp_canonicalize_string (name);
14376
14377 if (canon_name != NULL)
14378 {
14379 if (strcmp (canon_name, name) != 0)
14380 name = obsavestring (canon_name, strlen (canon_name),
14381 obstack);
14382 xfree (canon_name);
14383 }
14384 }
14385
14386 return name;
14387 }
14388
14389 /* Get name of a die, return NULL if not found. */
14390
14391 static char *
14392 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14393 {
14394 struct attribute *attr;
14395
14396 attr = dwarf2_attr (die, DW_AT_name, cu);
14397 if ((!attr || !DW_STRING (attr))
14398 && die->tag != DW_TAG_class_type
14399 && die->tag != DW_TAG_interface_type
14400 && die->tag != DW_TAG_structure_type
14401 && die->tag != DW_TAG_union_type)
14402 return NULL;
14403
14404 switch (die->tag)
14405 {
14406 case DW_TAG_compile_unit:
14407 case DW_TAG_partial_unit:
14408 /* Compilation units have a DW_AT_name that is a filename, not
14409 a source language identifier. */
14410 case DW_TAG_enumeration_type:
14411 case DW_TAG_enumerator:
14412 /* These tags always have simple identifiers already; no need
14413 to canonicalize them. */
14414 return DW_STRING (attr);
14415
14416 case DW_TAG_subprogram:
14417 /* Java constructors will all be named "<init>", so return
14418 the class name when we see this special case. */
14419 if (cu->language == language_java
14420 && DW_STRING (attr) != NULL
14421 && strcmp (DW_STRING (attr), "<init>") == 0)
14422 {
14423 struct dwarf2_cu *spec_cu = cu;
14424 struct die_info *spec_die;
14425
14426 /* GCJ will output '<init>' for Java constructor names.
14427 For this special case, return the name of the parent class. */
14428
14429 /* GCJ may output suprogram DIEs with AT_specification set.
14430 If so, use the name of the specified DIE. */
14431 spec_die = die_specification (die, &spec_cu);
14432 if (spec_die != NULL)
14433 return dwarf2_name (spec_die, spec_cu);
14434
14435 do
14436 {
14437 die = die->parent;
14438 if (die->tag == DW_TAG_class_type)
14439 return dwarf2_name (die, cu);
14440 }
14441 while (die->tag != DW_TAG_compile_unit
14442 && die->tag != DW_TAG_partial_unit);
14443 }
14444 break;
14445
14446 case DW_TAG_class_type:
14447 case DW_TAG_interface_type:
14448 case DW_TAG_structure_type:
14449 case DW_TAG_union_type:
14450 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14451 structures or unions. These were of the form "._%d" in GCC 4.1,
14452 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14453 and GCC 4.4. We work around this problem by ignoring these. */
14454 if (attr && DW_STRING (attr)
14455 && (strncmp (DW_STRING (attr), "._", 2) == 0
14456 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14457 return NULL;
14458
14459 /* GCC might emit a nameless typedef that has a linkage name. See
14460 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14461 if (!attr || DW_STRING (attr) == NULL)
14462 {
14463 char *demangled = NULL;
14464
14465 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14466 if (attr == NULL)
14467 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14468
14469 if (attr == NULL || DW_STRING (attr) == NULL)
14470 return NULL;
14471
14472 /* Avoid demangling DW_STRING (attr) the second time on a second
14473 call for the same DIE. */
14474 if (!DW_STRING_IS_CANONICAL (attr))
14475 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14476
14477 if (demangled)
14478 {
14479 char *base;
14480
14481 /* FIXME: we already did this for the partial symbol... */
14482 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14483 &cu->objfile->objfile_obstack);
14484 DW_STRING_IS_CANONICAL (attr) = 1;
14485 xfree (demangled);
14486
14487 /* Strip any leading namespaces/classes, keep only the base name.
14488 DW_AT_name for named DIEs does not contain the prefixes. */
14489 base = strrchr (DW_STRING (attr), ':');
14490 if (base && base > DW_STRING (attr) && base[-1] == ':')
14491 return &base[1];
14492 else
14493 return DW_STRING (attr);
14494 }
14495 }
14496 break;
14497
14498 default:
14499 break;
14500 }
14501
14502 if (!DW_STRING_IS_CANONICAL (attr))
14503 {
14504 DW_STRING (attr)
14505 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14506 &cu->objfile->objfile_obstack);
14507 DW_STRING_IS_CANONICAL (attr) = 1;
14508 }
14509 return DW_STRING (attr);
14510 }
14511
14512 /* Return the die that this die in an extension of, or NULL if there
14513 is none. *EXT_CU is the CU containing DIE on input, and the CU
14514 containing the return value on output. */
14515
14516 static struct die_info *
14517 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14518 {
14519 struct attribute *attr;
14520
14521 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14522 if (attr == NULL)
14523 return NULL;
14524
14525 return follow_die_ref (die, attr, ext_cu);
14526 }
14527
14528 /* Convert a DIE tag into its string name. */
14529
14530 static const char *
14531 dwarf_tag_name (unsigned tag)
14532 {
14533 const char *name = get_DW_TAG_name (tag);
14534
14535 if (name == NULL)
14536 return "DW_TAG_<unknown>";
14537
14538 return name;
14539 }
14540
14541 /* Convert a DWARF attribute code into its string name. */
14542
14543 static const char *
14544 dwarf_attr_name (unsigned attr)
14545 {
14546 const char *name;
14547
14548 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14549 if (attr == DW_AT_MIPS_fde)
14550 return "DW_AT_MIPS_fde";
14551 #else
14552 if (attr == DW_AT_HP_block_index)
14553 return "DW_AT_HP_block_index";
14554 #endif
14555
14556 name = get_DW_AT_name (attr);
14557
14558 if (name == NULL)
14559 return "DW_AT_<unknown>";
14560
14561 return name;
14562 }
14563
14564 /* Convert a DWARF value form code into its string name. */
14565
14566 static const char *
14567 dwarf_form_name (unsigned form)
14568 {
14569 const char *name = get_DW_FORM_name (form);
14570
14571 if (name == NULL)
14572 return "DW_FORM_<unknown>";
14573
14574 return name;
14575 }
14576
14577 static char *
14578 dwarf_bool_name (unsigned mybool)
14579 {
14580 if (mybool)
14581 return "TRUE";
14582 else
14583 return "FALSE";
14584 }
14585
14586 /* Convert a DWARF type code into its string name. */
14587
14588 static const char *
14589 dwarf_type_encoding_name (unsigned enc)
14590 {
14591 const char *name = get_DW_ATE_name (enc);
14592
14593 if (name == NULL)
14594 return "DW_ATE_<unknown>";
14595
14596 return name;
14597 }
14598
14599 static void
14600 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14601 {
14602 unsigned int i;
14603
14604 print_spaces (indent, f);
14605 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14606 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14607
14608 if (die->parent != NULL)
14609 {
14610 print_spaces (indent, f);
14611 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14612 die->parent->offset.sect_off);
14613 }
14614
14615 print_spaces (indent, f);
14616 fprintf_unfiltered (f, " has children: %s\n",
14617 dwarf_bool_name (die->child != NULL));
14618
14619 print_spaces (indent, f);
14620 fprintf_unfiltered (f, " attributes:\n");
14621
14622 for (i = 0; i < die->num_attrs; ++i)
14623 {
14624 print_spaces (indent, f);
14625 fprintf_unfiltered (f, " %s (%s) ",
14626 dwarf_attr_name (die->attrs[i].name),
14627 dwarf_form_name (die->attrs[i].form));
14628
14629 switch (die->attrs[i].form)
14630 {
14631 case DW_FORM_addr:
14632 case DW_FORM_GNU_addr_index:
14633 fprintf_unfiltered (f, "address: ");
14634 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14635 break;
14636 case DW_FORM_block2:
14637 case DW_FORM_block4:
14638 case DW_FORM_block:
14639 case DW_FORM_block1:
14640 fprintf_unfiltered (f, "block: size %d",
14641 DW_BLOCK (&die->attrs[i])->size);
14642 break;
14643 case DW_FORM_exprloc:
14644 fprintf_unfiltered (f, "expression: size %u",
14645 DW_BLOCK (&die->attrs[i])->size);
14646 break;
14647 case DW_FORM_ref_addr:
14648 fprintf_unfiltered (f, "ref address: ");
14649 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14650 break;
14651 case DW_FORM_ref1:
14652 case DW_FORM_ref2:
14653 case DW_FORM_ref4:
14654 case DW_FORM_ref8:
14655 case DW_FORM_ref_udata:
14656 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14657 (long) (DW_UNSND (&die->attrs[i])));
14658 break;
14659 case DW_FORM_data1:
14660 case DW_FORM_data2:
14661 case DW_FORM_data4:
14662 case DW_FORM_data8:
14663 case DW_FORM_udata:
14664 case DW_FORM_sdata:
14665 fprintf_unfiltered (f, "constant: %s",
14666 pulongest (DW_UNSND (&die->attrs[i])));
14667 break;
14668 case DW_FORM_sec_offset:
14669 fprintf_unfiltered (f, "section offset: %s",
14670 pulongest (DW_UNSND (&die->attrs[i])));
14671 break;
14672 case DW_FORM_ref_sig8:
14673 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14674 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14675 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14676 else
14677 fprintf_unfiltered (f, "signatured type, offset: unknown");
14678 break;
14679 case DW_FORM_string:
14680 case DW_FORM_strp:
14681 case DW_FORM_GNU_str_index:
14682 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14683 DW_STRING (&die->attrs[i])
14684 ? DW_STRING (&die->attrs[i]) : "",
14685 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14686 break;
14687 case DW_FORM_flag:
14688 if (DW_UNSND (&die->attrs[i]))
14689 fprintf_unfiltered (f, "flag: TRUE");
14690 else
14691 fprintf_unfiltered (f, "flag: FALSE");
14692 break;
14693 case DW_FORM_flag_present:
14694 fprintf_unfiltered (f, "flag: TRUE");
14695 break;
14696 case DW_FORM_indirect:
14697 /* The reader will have reduced the indirect form to
14698 the "base form" so this form should not occur. */
14699 fprintf_unfiltered (f,
14700 "unexpected attribute form: DW_FORM_indirect");
14701 break;
14702 default:
14703 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14704 die->attrs[i].form);
14705 break;
14706 }
14707 fprintf_unfiltered (f, "\n");
14708 }
14709 }
14710
14711 static void
14712 dump_die_for_error (struct die_info *die)
14713 {
14714 dump_die_shallow (gdb_stderr, 0, die);
14715 }
14716
14717 static void
14718 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14719 {
14720 int indent = level * 4;
14721
14722 gdb_assert (die != NULL);
14723
14724 if (level >= max_level)
14725 return;
14726
14727 dump_die_shallow (f, indent, die);
14728
14729 if (die->child != NULL)
14730 {
14731 print_spaces (indent, f);
14732 fprintf_unfiltered (f, " Children:");
14733 if (level + 1 < max_level)
14734 {
14735 fprintf_unfiltered (f, "\n");
14736 dump_die_1 (f, level + 1, max_level, die->child);
14737 }
14738 else
14739 {
14740 fprintf_unfiltered (f,
14741 " [not printed, max nesting level reached]\n");
14742 }
14743 }
14744
14745 if (die->sibling != NULL && level > 0)
14746 {
14747 dump_die_1 (f, level, max_level, die->sibling);
14748 }
14749 }
14750
14751 /* This is called from the pdie macro in gdbinit.in.
14752 It's not static so gcc will keep a copy callable from gdb. */
14753
14754 void
14755 dump_die (struct die_info *die, int max_level)
14756 {
14757 dump_die_1 (gdb_stdlog, 0, max_level, die);
14758 }
14759
14760 static void
14761 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14762 {
14763 void **slot;
14764
14765 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14766 INSERT);
14767
14768 *slot = die;
14769 }
14770
14771 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14772 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14773
14774 static int
14775 is_ref_attr (struct attribute *attr)
14776 {
14777 switch (attr->form)
14778 {
14779 case DW_FORM_ref_addr:
14780 case DW_FORM_ref1:
14781 case DW_FORM_ref2:
14782 case DW_FORM_ref4:
14783 case DW_FORM_ref8:
14784 case DW_FORM_ref_udata:
14785 return 1;
14786 default:
14787 return 0;
14788 }
14789 }
14790
14791 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14792 required kind. */
14793
14794 static sect_offset
14795 dwarf2_get_ref_die_offset (struct attribute *attr)
14796 {
14797 sect_offset retval = { DW_UNSND (attr) };
14798
14799 if (is_ref_attr (attr))
14800 return retval;
14801
14802 retval.sect_off = 0;
14803 complaint (&symfile_complaints,
14804 _("unsupported die ref attribute form: '%s'"),
14805 dwarf_form_name (attr->form));
14806 return retval;
14807 }
14808
14809 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14810 * the value held by the attribute is not constant. */
14811
14812 static LONGEST
14813 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14814 {
14815 if (attr->form == DW_FORM_sdata)
14816 return DW_SND (attr);
14817 else if (attr->form == DW_FORM_udata
14818 || attr->form == DW_FORM_data1
14819 || attr->form == DW_FORM_data2
14820 || attr->form == DW_FORM_data4
14821 || attr->form == DW_FORM_data8)
14822 return DW_UNSND (attr);
14823 else
14824 {
14825 complaint (&symfile_complaints,
14826 _("Attribute value is not a constant (%s)"),
14827 dwarf_form_name (attr->form));
14828 return default_value;
14829 }
14830 }
14831
14832 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14833 unit and add it to our queue.
14834 The result is non-zero if PER_CU was queued, otherwise the result is zero
14835 meaning either PER_CU is already queued or it is already loaded. */
14836
14837 static int
14838 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14839 struct dwarf2_per_cu_data *per_cu,
14840 enum language pretend_language)
14841 {
14842 /* We may arrive here during partial symbol reading, if we need full
14843 DIEs to process an unusual case (e.g. template arguments). Do
14844 not queue PER_CU, just tell our caller to load its DIEs. */
14845 if (dwarf2_per_objfile->reading_partial_symbols)
14846 {
14847 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14848 return 1;
14849 return 0;
14850 }
14851
14852 /* Mark the dependence relation so that we don't flush PER_CU
14853 too early. */
14854 dwarf2_add_dependence (this_cu, per_cu);
14855
14856 /* If it's already on the queue, we have nothing to do. */
14857 if (per_cu->queued)
14858 return 0;
14859
14860 /* If the compilation unit is already loaded, just mark it as
14861 used. */
14862 if (per_cu->cu != NULL)
14863 {
14864 per_cu->cu->last_used = 0;
14865 return 0;
14866 }
14867
14868 /* Add it to the queue. */
14869 queue_comp_unit (per_cu, pretend_language);
14870
14871 return 1;
14872 }
14873
14874 /* Follow reference or signature attribute ATTR of SRC_DIE.
14875 On entry *REF_CU is the CU of SRC_DIE.
14876 On exit *REF_CU is the CU of the result. */
14877
14878 static struct die_info *
14879 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14880 struct dwarf2_cu **ref_cu)
14881 {
14882 struct die_info *die;
14883
14884 if (is_ref_attr (attr))
14885 die = follow_die_ref (src_die, attr, ref_cu);
14886 else if (attr->form == DW_FORM_ref_sig8)
14887 die = follow_die_sig (src_die, attr, ref_cu);
14888 else
14889 {
14890 dump_die_for_error (src_die);
14891 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14892 (*ref_cu)->objfile->name);
14893 }
14894
14895 return die;
14896 }
14897
14898 /* Follow reference OFFSET.
14899 On entry *REF_CU is the CU of the source die referencing OFFSET.
14900 On exit *REF_CU is the CU of the result.
14901 Returns NULL if OFFSET is invalid. */
14902
14903 static struct die_info *
14904 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14905 {
14906 struct die_info temp_die;
14907 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14908
14909 gdb_assert (cu->per_cu != NULL);
14910
14911 target_cu = cu;
14912
14913 if (cu->per_cu->is_debug_types)
14914 {
14915 /* .debug_types CUs cannot reference anything outside their CU.
14916 If they need to, they have to reference a signatured type via
14917 DW_FORM_ref_sig8. */
14918 if (! offset_in_cu_p (&cu->header, offset))
14919 return NULL;
14920 }
14921 else if (! offset_in_cu_p (&cu->header, offset))
14922 {
14923 struct dwarf2_per_cu_data *per_cu;
14924
14925 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14926
14927 /* If necessary, add it to the queue and load its DIEs. */
14928 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14929 load_full_comp_unit (per_cu, cu->language);
14930
14931 target_cu = per_cu->cu;
14932 }
14933 else if (cu->dies == NULL)
14934 {
14935 /* We're loading full DIEs during partial symbol reading. */
14936 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14937 load_full_comp_unit (cu->per_cu, language_minimal);
14938 }
14939
14940 *ref_cu = target_cu;
14941 temp_die.offset = offset;
14942 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14943 }
14944
14945 /* Follow reference attribute ATTR of SRC_DIE.
14946 On entry *REF_CU is the CU of SRC_DIE.
14947 On exit *REF_CU is the CU of the result. */
14948
14949 static struct die_info *
14950 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14951 struct dwarf2_cu **ref_cu)
14952 {
14953 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14954 struct dwarf2_cu *cu = *ref_cu;
14955 struct die_info *die;
14956
14957 die = follow_die_offset (offset, ref_cu);
14958 if (!die)
14959 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14960 "at 0x%x [in module %s]"),
14961 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14962
14963 return die;
14964 }
14965
14966 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14967 Returned value is intended for DW_OP_call*. Returned
14968 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14969
14970 struct dwarf2_locexpr_baton
14971 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14972 struct dwarf2_per_cu_data *per_cu,
14973 CORE_ADDR (*get_frame_pc) (void *baton),
14974 void *baton)
14975 {
14976 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14977 struct dwarf2_cu *cu;
14978 struct die_info *die;
14979 struct attribute *attr;
14980 struct dwarf2_locexpr_baton retval;
14981
14982 dw2_setup (per_cu->objfile);
14983
14984 if (per_cu->cu == NULL)
14985 load_cu (per_cu);
14986 cu = per_cu->cu;
14987
14988 die = follow_die_offset (offset, &cu);
14989 if (!die)
14990 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14991 offset.sect_off, per_cu->objfile->name);
14992
14993 attr = dwarf2_attr (die, DW_AT_location, cu);
14994 if (!attr)
14995 {
14996 /* DWARF: "If there is no such attribute, then there is no effect.".
14997 DATA is ignored if SIZE is 0. */
14998
14999 retval.data = NULL;
15000 retval.size = 0;
15001 }
15002 else if (attr_form_is_section_offset (attr))
15003 {
15004 struct dwarf2_loclist_baton loclist_baton;
15005 CORE_ADDR pc = (*get_frame_pc) (baton);
15006 size_t size;
15007
15008 fill_in_loclist_baton (cu, &loclist_baton, attr);
15009
15010 retval.data = dwarf2_find_location_expression (&loclist_baton,
15011 &size, pc);
15012 retval.size = size;
15013 }
15014 else
15015 {
15016 if (!attr_form_is_block (attr))
15017 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15018 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15019 offset.sect_off, per_cu->objfile->name);
15020
15021 retval.data = DW_BLOCK (attr)->data;
15022 retval.size = DW_BLOCK (attr)->size;
15023 }
15024 retval.per_cu = cu->per_cu;
15025
15026 age_cached_comp_units ();
15027
15028 return retval;
15029 }
15030
15031 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15032 PER_CU. */
15033
15034 struct type *
15035 dwarf2_get_die_type (cu_offset die_offset,
15036 struct dwarf2_per_cu_data *per_cu)
15037 {
15038 sect_offset die_offset_sect;
15039
15040 dw2_setup (per_cu->objfile);
15041
15042 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15043 return get_die_type_at_offset (die_offset_sect, per_cu);
15044 }
15045
15046 /* Follow the signature attribute ATTR in SRC_DIE.
15047 On entry *REF_CU is the CU of SRC_DIE.
15048 On exit *REF_CU is the CU of the result. */
15049
15050 static struct die_info *
15051 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15052 struct dwarf2_cu **ref_cu)
15053 {
15054 struct objfile *objfile = (*ref_cu)->objfile;
15055 struct die_info temp_die;
15056 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15057 struct dwarf2_cu *sig_cu;
15058 struct die_info *die;
15059
15060 /* sig_type will be NULL if the signatured type is missing from
15061 the debug info. */
15062 if (sig_type == NULL)
15063 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15064 "at 0x%x [in module %s]"),
15065 src_die->offset.sect_off, objfile->name);
15066
15067 /* If necessary, add it to the queue and load its DIEs. */
15068
15069 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15070 read_signatured_type (sig_type);
15071
15072 gdb_assert (sig_type->per_cu.cu != NULL);
15073
15074 sig_cu = sig_type->per_cu.cu;
15075 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15076 temp_die.offset = sig_type->type_offset_in_section;
15077 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15078 temp_die.offset.sect_off);
15079 if (die)
15080 {
15081 *ref_cu = sig_cu;
15082 return die;
15083 }
15084
15085 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15086 "from DIE at 0x%x [in module %s]"),
15087 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15088 }
15089
15090 /* Given an offset of a signatured type, return its signatured_type. */
15091
15092 static struct signatured_type *
15093 lookup_signatured_type_at_offset (struct objfile *objfile,
15094 struct dwarf2_section_info *section,
15095 sect_offset offset)
15096 {
15097 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15098 unsigned int length, initial_length_size;
15099 unsigned int sig_offset;
15100 struct signatured_type find_entry, *sig_type;
15101
15102 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15103 sig_offset = (initial_length_size
15104 + 2 /*version*/
15105 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15106 + 1 /*address_size*/);
15107 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15108 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15109
15110 /* This is only used to lookup previously recorded types.
15111 If we didn't find it, it's our bug. */
15112 gdb_assert (sig_type != NULL);
15113 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15114
15115 return sig_type;
15116 }
15117
15118 /* Load the DIEs associated with type unit PER_CU into memory. */
15119
15120 static void
15121 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15122 {
15123 struct objfile *objfile = per_cu->objfile;
15124 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15125 sect_offset offset = per_cu->offset;
15126 struct signatured_type *sig_type;
15127
15128 dwarf2_read_section (objfile, sect);
15129
15130 /* We have the section offset, but we need the signature to do the
15131 hash table lookup. */
15132 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15133 the signature to assert we found the right one.
15134 Ok, but it's a lot of work. We should simplify things so any needed
15135 assert doesn't require all this clumsiness. */
15136 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15137
15138 gdb_assert (&sig_type->per_cu == per_cu);
15139 gdb_assert (sig_type->per_cu.cu == NULL);
15140
15141 read_signatured_type (sig_type);
15142
15143 gdb_assert (sig_type->per_cu.cu != NULL);
15144 }
15145
15146 /* die_reader_func for read_signatured_type.
15147 This is identical to load_full_comp_unit_reader,
15148 but is kept separate for now. */
15149
15150 static void
15151 read_signatured_type_reader (const struct die_reader_specs *reader,
15152 gdb_byte *info_ptr,
15153 struct die_info *comp_unit_die,
15154 int has_children,
15155 void *data)
15156 {
15157 struct dwarf2_cu *cu = reader->cu;
15158 struct attribute *attr;
15159
15160 gdb_assert (cu->die_hash == NULL);
15161 cu->die_hash =
15162 htab_create_alloc_ex (cu->header.length / 12,
15163 die_hash,
15164 die_eq,
15165 NULL,
15166 &cu->comp_unit_obstack,
15167 hashtab_obstack_allocate,
15168 dummy_obstack_deallocate);
15169
15170 if (has_children)
15171 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15172 &info_ptr, comp_unit_die);
15173 cu->dies = comp_unit_die;
15174 /* comp_unit_die is not stored in die_hash, no need. */
15175
15176 /* We try not to read any attributes in this function, because not
15177 all CUs needed for references have been loaded yet, and symbol
15178 table processing isn't initialized. But we have to set the CU language,
15179 or we won't be able to build types correctly.
15180 Similarly, if we do not read the producer, we can not apply
15181 producer-specific interpretation. */
15182 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15183 }
15184
15185 /* Read in a signatured type and build its CU and DIEs.
15186 If the type is a stub for the real type in a DWO file,
15187 read in the real type from the DWO file as well. */
15188
15189 static void
15190 read_signatured_type (struct signatured_type *sig_type)
15191 {
15192 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15193
15194 gdb_assert (per_cu->is_debug_types);
15195 gdb_assert (per_cu->cu == NULL);
15196
15197 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15198 }
15199
15200 /* Decode simple location descriptions.
15201 Given a pointer to a dwarf block that defines a location, compute
15202 the location and return the value.
15203
15204 NOTE drow/2003-11-18: This function is called in two situations
15205 now: for the address of static or global variables (partial symbols
15206 only) and for offsets into structures which are expected to be
15207 (more or less) constant. The partial symbol case should go away,
15208 and only the constant case should remain. That will let this
15209 function complain more accurately. A few special modes are allowed
15210 without complaint for global variables (for instance, global
15211 register values and thread-local values).
15212
15213 A location description containing no operations indicates that the
15214 object is optimized out. The return value is 0 for that case.
15215 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15216 callers will only want a very basic result and this can become a
15217 complaint.
15218
15219 Note that stack[0] is unused except as a default error return. */
15220
15221 static CORE_ADDR
15222 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15223 {
15224 struct objfile *objfile = cu->objfile;
15225 int i;
15226 int size = blk->size;
15227 gdb_byte *data = blk->data;
15228 CORE_ADDR stack[64];
15229 int stacki;
15230 unsigned int bytes_read, unsnd;
15231 gdb_byte op;
15232
15233 i = 0;
15234 stacki = 0;
15235 stack[stacki] = 0;
15236 stack[++stacki] = 0;
15237
15238 while (i < size)
15239 {
15240 op = data[i++];
15241 switch (op)
15242 {
15243 case DW_OP_lit0:
15244 case DW_OP_lit1:
15245 case DW_OP_lit2:
15246 case DW_OP_lit3:
15247 case DW_OP_lit4:
15248 case DW_OP_lit5:
15249 case DW_OP_lit6:
15250 case DW_OP_lit7:
15251 case DW_OP_lit8:
15252 case DW_OP_lit9:
15253 case DW_OP_lit10:
15254 case DW_OP_lit11:
15255 case DW_OP_lit12:
15256 case DW_OP_lit13:
15257 case DW_OP_lit14:
15258 case DW_OP_lit15:
15259 case DW_OP_lit16:
15260 case DW_OP_lit17:
15261 case DW_OP_lit18:
15262 case DW_OP_lit19:
15263 case DW_OP_lit20:
15264 case DW_OP_lit21:
15265 case DW_OP_lit22:
15266 case DW_OP_lit23:
15267 case DW_OP_lit24:
15268 case DW_OP_lit25:
15269 case DW_OP_lit26:
15270 case DW_OP_lit27:
15271 case DW_OP_lit28:
15272 case DW_OP_lit29:
15273 case DW_OP_lit30:
15274 case DW_OP_lit31:
15275 stack[++stacki] = op - DW_OP_lit0;
15276 break;
15277
15278 case DW_OP_reg0:
15279 case DW_OP_reg1:
15280 case DW_OP_reg2:
15281 case DW_OP_reg3:
15282 case DW_OP_reg4:
15283 case DW_OP_reg5:
15284 case DW_OP_reg6:
15285 case DW_OP_reg7:
15286 case DW_OP_reg8:
15287 case DW_OP_reg9:
15288 case DW_OP_reg10:
15289 case DW_OP_reg11:
15290 case DW_OP_reg12:
15291 case DW_OP_reg13:
15292 case DW_OP_reg14:
15293 case DW_OP_reg15:
15294 case DW_OP_reg16:
15295 case DW_OP_reg17:
15296 case DW_OP_reg18:
15297 case DW_OP_reg19:
15298 case DW_OP_reg20:
15299 case DW_OP_reg21:
15300 case DW_OP_reg22:
15301 case DW_OP_reg23:
15302 case DW_OP_reg24:
15303 case DW_OP_reg25:
15304 case DW_OP_reg26:
15305 case DW_OP_reg27:
15306 case DW_OP_reg28:
15307 case DW_OP_reg29:
15308 case DW_OP_reg30:
15309 case DW_OP_reg31:
15310 stack[++stacki] = op - DW_OP_reg0;
15311 if (i < size)
15312 dwarf2_complex_location_expr_complaint ();
15313 break;
15314
15315 case DW_OP_regx:
15316 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15317 i += bytes_read;
15318 stack[++stacki] = unsnd;
15319 if (i < size)
15320 dwarf2_complex_location_expr_complaint ();
15321 break;
15322
15323 case DW_OP_addr:
15324 stack[++stacki] = read_address (objfile->obfd, &data[i],
15325 cu, &bytes_read);
15326 i += bytes_read;
15327 break;
15328
15329 case DW_OP_const1u:
15330 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15331 i += 1;
15332 break;
15333
15334 case DW_OP_const1s:
15335 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15336 i += 1;
15337 break;
15338
15339 case DW_OP_const2u:
15340 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15341 i += 2;
15342 break;
15343
15344 case DW_OP_const2s:
15345 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15346 i += 2;
15347 break;
15348
15349 case DW_OP_const4u:
15350 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15351 i += 4;
15352 break;
15353
15354 case DW_OP_const4s:
15355 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15356 i += 4;
15357 break;
15358
15359 case DW_OP_const8u:
15360 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15361 i += 8;
15362 break;
15363
15364 case DW_OP_constu:
15365 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15366 &bytes_read);
15367 i += bytes_read;
15368 break;
15369
15370 case DW_OP_consts:
15371 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15372 i += bytes_read;
15373 break;
15374
15375 case DW_OP_dup:
15376 stack[stacki + 1] = stack[stacki];
15377 stacki++;
15378 break;
15379
15380 case DW_OP_plus:
15381 stack[stacki - 1] += stack[stacki];
15382 stacki--;
15383 break;
15384
15385 case DW_OP_plus_uconst:
15386 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15387 &bytes_read);
15388 i += bytes_read;
15389 break;
15390
15391 case DW_OP_minus:
15392 stack[stacki - 1] -= stack[stacki];
15393 stacki--;
15394 break;
15395
15396 case DW_OP_deref:
15397 /* If we're not the last op, then we definitely can't encode
15398 this using GDB's address_class enum. This is valid for partial
15399 global symbols, although the variable's address will be bogus
15400 in the psymtab. */
15401 if (i < size)
15402 dwarf2_complex_location_expr_complaint ();
15403 break;
15404
15405 case DW_OP_GNU_push_tls_address:
15406 /* The top of the stack has the offset from the beginning
15407 of the thread control block at which the variable is located. */
15408 /* Nothing should follow this operator, so the top of stack would
15409 be returned. */
15410 /* This is valid for partial global symbols, but the variable's
15411 address will be bogus in the psymtab. Make it always at least
15412 non-zero to not look as a variable garbage collected by linker
15413 which have DW_OP_addr 0. */
15414 if (i < size)
15415 dwarf2_complex_location_expr_complaint ();
15416 stack[stacki]++;
15417 break;
15418
15419 case DW_OP_GNU_uninit:
15420 break;
15421
15422 case DW_OP_GNU_addr_index:
15423 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15424 &bytes_read);
15425 i += bytes_read;
15426 break;
15427
15428 default:
15429 {
15430 const char *name = get_DW_OP_name (op);
15431
15432 if (name)
15433 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15434 name);
15435 else
15436 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15437 op);
15438 }
15439
15440 return (stack[stacki]);
15441 }
15442
15443 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15444 outside of the allocated space. Also enforce minimum>0. */
15445 if (stacki >= ARRAY_SIZE (stack) - 1)
15446 {
15447 complaint (&symfile_complaints,
15448 _("location description stack overflow"));
15449 return 0;
15450 }
15451
15452 if (stacki <= 0)
15453 {
15454 complaint (&symfile_complaints,
15455 _("location description stack underflow"));
15456 return 0;
15457 }
15458 }
15459 return (stack[stacki]);
15460 }
15461
15462 /* memory allocation interface */
15463
15464 static struct dwarf_block *
15465 dwarf_alloc_block (struct dwarf2_cu *cu)
15466 {
15467 struct dwarf_block *blk;
15468
15469 blk = (struct dwarf_block *)
15470 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15471 return (blk);
15472 }
15473
15474 static struct abbrev_info *
15475 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15476 {
15477 struct abbrev_info *abbrev;
15478
15479 abbrev = (struct abbrev_info *)
15480 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15481 memset (abbrev, 0, sizeof (struct abbrev_info));
15482 return (abbrev);
15483 }
15484
15485 static struct die_info *
15486 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15487 {
15488 struct die_info *die;
15489 size_t size = sizeof (struct die_info);
15490
15491 if (num_attrs > 1)
15492 size += (num_attrs - 1) * sizeof (struct attribute);
15493
15494 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15495 memset (die, 0, sizeof (struct die_info));
15496 return (die);
15497 }
15498
15499 \f
15500 /* Macro support. */
15501
15502 /* Return the full name of file number I in *LH's file name table.
15503 Use COMP_DIR as the name of the current directory of the
15504 compilation. The result is allocated using xmalloc; the caller is
15505 responsible for freeing it. */
15506 static char *
15507 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15508 {
15509 /* Is the file number a valid index into the line header's file name
15510 table? Remember that file numbers start with one, not zero. */
15511 if (1 <= file && file <= lh->num_file_names)
15512 {
15513 struct file_entry *fe = &lh->file_names[file - 1];
15514
15515 if (IS_ABSOLUTE_PATH (fe->name))
15516 return xstrdup (fe->name);
15517 else
15518 {
15519 const char *dir;
15520 int dir_len;
15521 char *full_name;
15522
15523 if (fe->dir_index)
15524 dir = lh->include_dirs[fe->dir_index - 1];
15525 else
15526 dir = comp_dir;
15527
15528 if (dir)
15529 {
15530 dir_len = strlen (dir);
15531 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15532 strcpy (full_name, dir);
15533 full_name[dir_len] = '/';
15534 strcpy (full_name + dir_len + 1, fe->name);
15535 return full_name;
15536 }
15537 else
15538 return xstrdup (fe->name);
15539 }
15540 }
15541 else
15542 {
15543 /* The compiler produced a bogus file number. We can at least
15544 record the macro definitions made in the file, even if we
15545 won't be able to find the file by name. */
15546 char fake_name[80];
15547
15548 sprintf (fake_name, "<bad macro file number %d>", file);
15549
15550 complaint (&symfile_complaints,
15551 _("bad file number in macro information (%d)"),
15552 file);
15553
15554 return xstrdup (fake_name);
15555 }
15556 }
15557
15558
15559 static struct macro_source_file *
15560 macro_start_file (int file, int line,
15561 struct macro_source_file *current_file,
15562 const char *comp_dir,
15563 struct line_header *lh, struct objfile *objfile)
15564 {
15565 /* The full name of this source file. */
15566 char *full_name = file_full_name (file, lh, comp_dir);
15567
15568 /* We don't create a macro table for this compilation unit
15569 at all until we actually get a filename. */
15570 if (! pending_macros)
15571 pending_macros = new_macro_table (&objfile->objfile_obstack,
15572 objfile->macro_cache);
15573
15574 if (! current_file)
15575 {
15576 /* If we have no current file, then this must be the start_file
15577 directive for the compilation unit's main source file. */
15578 current_file = macro_set_main (pending_macros, full_name);
15579 macro_define_special (pending_macros);
15580 }
15581 else
15582 current_file = macro_include (current_file, line, full_name);
15583
15584 xfree (full_name);
15585
15586 return current_file;
15587 }
15588
15589
15590 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15591 followed by a null byte. */
15592 static char *
15593 copy_string (const char *buf, int len)
15594 {
15595 char *s = xmalloc (len + 1);
15596
15597 memcpy (s, buf, len);
15598 s[len] = '\0';
15599 return s;
15600 }
15601
15602
15603 static const char *
15604 consume_improper_spaces (const char *p, const char *body)
15605 {
15606 if (*p == ' ')
15607 {
15608 complaint (&symfile_complaints,
15609 _("macro definition contains spaces "
15610 "in formal argument list:\n`%s'"),
15611 body);
15612
15613 while (*p == ' ')
15614 p++;
15615 }
15616
15617 return p;
15618 }
15619
15620
15621 static void
15622 parse_macro_definition (struct macro_source_file *file, int line,
15623 const char *body)
15624 {
15625 const char *p;
15626
15627 /* The body string takes one of two forms. For object-like macro
15628 definitions, it should be:
15629
15630 <macro name> " " <definition>
15631
15632 For function-like macro definitions, it should be:
15633
15634 <macro name> "() " <definition>
15635 or
15636 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15637
15638 Spaces may appear only where explicitly indicated, and in the
15639 <definition>.
15640
15641 The Dwarf 2 spec says that an object-like macro's name is always
15642 followed by a space, but versions of GCC around March 2002 omit
15643 the space when the macro's definition is the empty string.
15644
15645 The Dwarf 2 spec says that there should be no spaces between the
15646 formal arguments in a function-like macro's formal argument list,
15647 but versions of GCC around March 2002 include spaces after the
15648 commas. */
15649
15650
15651 /* Find the extent of the macro name. The macro name is terminated
15652 by either a space or null character (for an object-like macro) or
15653 an opening paren (for a function-like macro). */
15654 for (p = body; *p; p++)
15655 if (*p == ' ' || *p == '(')
15656 break;
15657
15658 if (*p == ' ' || *p == '\0')
15659 {
15660 /* It's an object-like macro. */
15661 int name_len = p - body;
15662 char *name = copy_string (body, name_len);
15663 const char *replacement;
15664
15665 if (*p == ' ')
15666 replacement = body + name_len + 1;
15667 else
15668 {
15669 dwarf2_macro_malformed_definition_complaint (body);
15670 replacement = body + name_len;
15671 }
15672
15673 macro_define_object (file, line, name, replacement);
15674
15675 xfree (name);
15676 }
15677 else if (*p == '(')
15678 {
15679 /* It's a function-like macro. */
15680 char *name = copy_string (body, p - body);
15681 int argc = 0;
15682 int argv_size = 1;
15683 char **argv = xmalloc (argv_size * sizeof (*argv));
15684
15685 p++;
15686
15687 p = consume_improper_spaces (p, body);
15688
15689 /* Parse the formal argument list. */
15690 while (*p && *p != ')')
15691 {
15692 /* Find the extent of the current argument name. */
15693 const char *arg_start = p;
15694
15695 while (*p && *p != ',' && *p != ')' && *p != ' ')
15696 p++;
15697
15698 if (! *p || p == arg_start)
15699 dwarf2_macro_malformed_definition_complaint (body);
15700 else
15701 {
15702 /* Make sure argv has room for the new argument. */
15703 if (argc >= argv_size)
15704 {
15705 argv_size *= 2;
15706 argv = xrealloc (argv, argv_size * sizeof (*argv));
15707 }
15708
15709 argv[argc++] = copy_string (arg_start, p - arg_start);
15710 }
15711
15712 p = consume_improper_spaces (p, body);
15713
15714 /* Consume the comma, if present. */
15715 if (*p == ',')
15716 {
15717 p++;
15718
15719 p = consume_improper_spaces (p, body);
15720 }
15721 }
15722
15723 if (*p == ')')
15724 {
15725 p++;
15726
15727 if (*p == ' ')
15728 /* Perfectly formed definition, no complaints. */
15729 macro_define_function (file, line, name,
15730 argc, (const char **) argv,
15731 p + 1);
15732 else if (*p == '\0')
15733 {
15734 /* Complain, but do define it. */
15735 dwarf2_macro_malformed_definition_complaint (body);
15736 macro_define_function (file, line, name,
15737 argc, (const char **) argv,
15738 p);
15739 }
15740 else
15741 /* Just complain. */
15742 dwarf2_macro_malformed_definition_complaint (body);
15743 }
15744 else
15745 /* Just complain. */
15746 dwarf2_macro_malformed_definition_complaint (body);
15747
15748 xfree (name);
15749 {
15750 int i;
15751
15752 for (i = 0; i < argc; i++)
15753 xfree (argv[i]);
15754 }
15755 xfree (argv);
15756 }
15757 else
15758 dwarf2_macro_malformed_definition_complaint (body);
15759 }
15760
15761 /* Skip some bytes from BYTES according to the form given in FORM.
15762 Returns the new pointer. */
15763
15764 static gdb_byte *
15765 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15766 enum dwarf_form form,
15767 unsigned int offset_size,
15768 struct dwarf2_section_info *section)
15769 {
15770 unsigned int bytes_read;
15771
15772 switch (form)
15773 {
15774 case DW_FORM_data1:
15775 case DW_FORM_flag:
15776 ++bytes;
15777 break;
15778
15779 case DW_FORM_data2:
15780 bytes += 2;
15781 break;
15782
15783 case DW_FORM_data4:
15784 bytes += 4;
15785 break;
15786
15787 case DW_FORM_data8:
15788 bytes += 8;
15789 break;
15790
15791 case DW_FORM_string:
15792 read_direct_string (abfd, bytes, &bytes_read);
15793 bytes += bytes_read;
15794 break;
15795
15796 case DW_FORM_sec_offset:
15797 case DW_FORM_strp:
15798 bytes += offset_size;
15799 break;
15800
15801 case DW_FORM_block:
15802 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15803 bytes += bytes_read;
15804 break;
15805
15806 case DW_FORM_block1:
15807 bytes += 1 + read_1_byte (abfd, bytes);
15808 break;
15809 case DW_FORM_block2:
15810 bytes += 2 + read_2_bytes (abfd, bytes);
15811 break;
15812 case DW_FORM_block4:
15813 bytes += 4 + read_4_bytes (abfd, bytes);
15814 break;
15815
15816 case DW_FORM_sdata:
15817 case DW_FORM_udata:
15818 case DW_FORM_GNU_addr_index:
15819 case DW_FORM_GNU_str_index:
15820 bytes = skip_leb128 (abfd, bytes);
15821 break;
15822
15823 default:
15824 {
15825 complain:
15826 complaint (&symfile_complaints,
15827 _("invalid form 0x%x in `%s'"),
15828 form,
15829 section->asection->name);
15830 return NULL;
15831 }
15832 }
15833
15834 return bytes;
15835 }
15836
15837 /* A helper for dwarf_decode_macros that handles skipping an unknown
15838 opcode. Returns an updated pointer to the macro data buffer; or,
15839 on error, issues a complaint and returns NULL. */
15840
15841 static gdb_byte *
15842 skip_unknown_opcode (unsigned int opcode,
15843 gdb_byte **opcode_definitions,
15844 gdb_byte *mac_ptr,
15845 bfd *abfd,
15846 unsigned int offset_size,
15847 struct dwarf2_section_info *section)
15848 {
15849 unsigned int bytes_read, i;
15850 unsigned long arg;
15851 gdb_byte *defn;
15852
15853 if (opcode_definitions[opcode] == NULL)
15854 {
15855 complaint (&symfile_complaints,
15856 _("unrecognized DW_MACFINO opcode 0x%x"),
15857 opcode);
15858 return NULL;
15859 }
15860
15861 defn = opcode_definitions[opcode];
15862 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15863 defn += bytes_read;
15864
15865 for (i = 0; i < arg; ++i)
15866 {
15867 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15868 if (mac_ptr == NULL)
15869 {
15870 /* skip_form_bytes already issued the complaint. */
15871 return NULL;
15872 }
15873 }
15874
15875 return mac_ptr;
15876 }
15877
15878 /* A helper function which parses the header of a macro section.
15879 If the macro section is the extended (for now called "GNU") type,
15880 then this updates *OFFSET_SIZE. Returns a pointer to just after
15881 the header, or issues a complaint and returns NULL on error. */
15882
15883 static gdb_byte *
15884 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15885 bfd *abfd,
15886 gdb_byte *mac_ptr,
15887 unsigned int *offset_size,
15888 int section_is_gnu)
15889 {
15890 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15891
15892 if (section_is_gnu)
15893 {
15894 unsigned int version, flags;
15895
15896 version = read_2_bytes (abfd, mac_ptr);
15897 if (version != 4)
15898 {
15899 complaint (&symfile_complaints,
15900 _("unrecognized version `%d' in .debug_macro section"),
15901 version);
15902 return NULL;
15903 }
15904 mac_ptr += 2;
15905
15906 flags = read_1_byte (abfd, mac_ptr);
15907 ++mac_ptr;
15908 *offset_size = (flags & 1) ? 8 : 4;
15909
15910 if ((flags & 2) != 0)
15911 /* We don't need the line table offset. */
15912 mac_ptr += *offset_size;
15913
15914 /* Vendor opcode descriptions. */
15915 if ((flags & 4) != 0)
15916 {
15917 unsigned int i, count;
15918
15919 count = read_1_byte (abfd, mac_ptr);
15920 ++mac_ptr;
15921 for (i = 0; i < count; ++i)
15922 {
15923 unsigned int opcode, bytes_read;
15924 unsigned long arg;
15925
15926 opcode = read_1_byte (abfd, mac_ptr);
15927 ++mac_ptr;
15928 opcode_definitions[opcode] = mac_ptr;
15929 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15930 mac_ptr += bytes_read;
15931 mac_ptr += arg;
15932 }
15933 }
15934 }
15935
15936 return mac_ptr;
15937 }
15938
15939 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15940 including DW_MACRO_GNU_transparent_include. */
15941
15942 static void
15943 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15944 struct macro_source_file *current_file,
15945 struct line_header *lh, char *comp_dir,
15946 struct dwarf2_section_info *section,
15947 int section_is_gnu,
15948 unsigned int offset_size,
15949 struct objfile *objfile,
15950 htab_t include_hash)
15951 {
15952 enum dwarf_macro_record_type macinfo_type;
15953 int at_commandline;
15954 gdb_byte *opcode_definitions[256];
15955
15956 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15957 &offset_size, section_is_gnu);
15958 if (mac_ptr == NULL)
15959 {
15960 /* We already issued a complaint. */
15961 return;
15962 }
15963
15964 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15965 GDB is still reading the definitions from command line. First
15966 DW_MACINFO_start_file will need to be ignored as it was already executed
15967 to create CURRENT_FILE for the main source holding also the command line
15968 definitions. On first met DW_MACINFO_start_file this flag is reset to
15969 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15970
15971 at_commandline = 1;
15972
15973 do
15974 {
15975 /* Do we at least have room for a macinfo type byte? */
15976 if (mac_ptr >= mac_end)
15977 {
15978 dwarf2_macros_too_long_complaint (section);
15979 break;
15980 }
15981
15982 macinfo_type = read_1_byte (abfd, mac_ptr);
15983 mac_ptr++;
15984
15985 /* Note that we rely on the fact that the corresponding GNU and
15986 DWARF constants are the same. */
15987 switch (macinfo_type)
15988 {
15989 /* A zero macinfo type indicates the end of the macro
15990 information. */
15991 case 0:
15992 break;
15993
15994 case DW_MACRO_GNU_define:
15995 case DW_MACRO_GNU_undef:
15996 case DW_MACRO_GNU_define_indirect:
15997 case DW_MACRO_GNU_undef_indirect:
15998 {
15999 unsigned int bytes_read;
16000 int line;
16001 char *body;
16002 int is_define;
16003
16004 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16005 mac_ptr += bytes_read;
16006
16007 if (macinfo_type == DW_MACRO_GNU_define
16008 || macinfo_type == DW_MACRO_GNU_undef)
16009 {
16010 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16011 mac_ptr += bytes_read;
16012 }
16013 else
16014 {
16015 LONGEST str_offset;
16016
16017 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16018 mac_ptr += offset_size;
16019
16020 body = read_indirect_string_at_offset (abfd, str_offset);
16021 }
16022
16023 is_define = (macinfo_type == DW_MACRO_GNU_define
16024 || macinfo_type == DW_MACRO_GNU_define_indirect);
16025 if (! current_file)
16026 {
16027 /* DWARF violation as no main source is present. */
16028 complaint (&symfile_complaints,
16029 _("debug info with no main source gives macro %s "
16030 "on line %d: %s"),
16031 is_define ? _("definition") : _("undefinition"),
16032 line, body);
16033 break;
16034 }
16035 if ((line == 0 && !at_commandline)
16036 || (line != 0 && at_commandline))
16037 complaint (&symfile_complaints,
16038 _("debug info gives %s macro %s with %s line %d: %s"),
16039 at_commandline ? _("command-line") : _("in-file"),
16040 is_define ? _("definition") : _("undefinition"),
16041 line == 0 ? _("zero") : _("non-zero"), line, body);
16042
16043 if (is_define)
16044 parse_macro_definition (current_file, line, body);
16045 else
16046 {
16047 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16048 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16049 macro_undef (current_file, line, body);
16050 }
16051 }
16052 break;
16053
16054 case DW_MACRO_GNU_start_file:
16055 {
16056 unsigned int bytes_read;
16057 int line, file;
16058
16059 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16060 mac_ptr += bytes_read;
16061 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16062 mac_ptr += bytes_read;
16063
16064 if ((line == 0 && !at_commandline)
16065 || (line != 0 && at_commandline))
16066 complaint (&symfile_complaints,
16067 _("debug info gives source %d included "
16068 "from %s at %s line %d"),
16069 file, at_commandline ? _("command-line") : _("file"),
16070 line == 0 ? _("zero") : _("non-zero"), line);
16071
16072 if (at_commandline)
16073 {
16074 /* This DW_MACRO_GNU_start_file was executed in the
16075 pass one. */
16076 at_commandline = 0;
16077 }
16078 else
16079 current_file = macro_start_file (file, line,
16080 current_file, comp_dir,
16081 lh, objfile);
16082 }
16083 break;
16084
16085 case DW_MACRO_GNU_end_file:
16086 if (! current_file)
16087 complaint (&symfile_complaints,
16088 _("macro debug info has an unmatched "
16089 "`close_file' directive"));
16090 else
16091 {
16092 current_file = current_file->included_by;
16093 if (! current_file)
16094 {
16095 enum dwarf_macro_record_type next_type;
16096
16097 /* GCC circa March 2002 doesn't produce the zero
16098 type byte marking the end of the compilation
16099 unit. Complain if it's not there, but exit no
16100 matter what. */
16101
16102 /* Do we at least have room for a macinfo type byte? */
16103 if (mac_ptr >= mac_end)
16104 {
16105 dwarf2_macros_too_long_complaint (section);
16106 return;
16107 }
16108
16109 /* We don't increment mac_ptr here, so this is just
16110 a look-ahead. */
16111 next_type = read_1_byte (abfd, mac_ptr);
16112 if (next_type != 0)
16113 complaint (&symfile_complaints,
16114 _("no terminating 0-type entry for "
16115 "macros in `.debug_macinfo' section"));
16116
16117 return;
16118 }
16119 }
16120 break;
16121
16122 case DW_MACRO_GNU_transparent_include:
16123 {
16124 LONGEST offset;
16125 void **slot;
16126
16127 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16128 mac_ptr += offset_size;
16129
16130 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16131 if (*slot != NULL)
16132 {
16133 /* This has actually happened; see
16134 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16135 complaint (&symfile_complaints,
16136 _("recursive DW_MACRO_GNU_transparent_include in "
16137 ".debug_macro section"));
16138 }
16139 else
16140 {
16141 *slot = mac_ptr;
16142
16143 dwarf_decode_macro_bytes (abfd,
16144 section->buffer + offset,
16145 mac_end, current_file,
16146 lh, comp_dir,
16147 section, section_is_gnu,
16148 offset_size, objfile, include_hash);
16149
16150 htab_remove_elt (include_hash, mac_ptr);
16151 }
16152 }
16153 break;
16154
16155 case DW_MACINFO_vendor_ext:
16156 if (!section_is_gnu)
16157 {
16158 unsigned int bytes_read;
16159 int constant;
16160
16161 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16162 mac_ptr += bytes_read;
16163 read_direct_string (abfd, mac_ptr, &bytes_read);
16164 mac_ptr += bytes_read;
16165
16166 /* We don't recognize any vendor extensions. */
16167 break;
16168 }
16169 /* FALLTHROUGH */
16170
16171 default:
16172 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16173 mac_ptr, abfd, offset_size,
16174 section);
16175 if (mac_ptr == NULL)
16176 return;
16177 break;
16178 }
16179 } while (macinfo_type != 0);
16180 }
16181
16182 static void
16183 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16184 char *comp_dir, bfd *abfd,
16185 struct dwarf2_cu *cu,
16186 struct dwarf2_section_info *section,
16187 int section_is_gnu, const char *section_name)
16188 {
16189 struct objfile *objfile = dwarf2_per_objfile->objfile;
16190 gdb_byte *mac_ptr, *mac_end;
16191 struct macro_source_file *current_file = 0;
16192 enum dwarf_macro_record_type macinfo_type;
16193 unsigned int offset_size = cu->header.offset_size;
16194 gdb_byte *opcode_definitions[256];
16195 struct cleanup *cleanup;
16196 htab_t include_hash;
16197 void **slot;
16198
16199 dwarf2_read_section (objfile, section);
16200 if (section->buffer == NULL)
16201 {
16202 complaint (&symfile_complaints, _("missing %s section"), section_name);
16203 return;
16204 }
16205
16206 /* First pass: Find the name of the base filename.
16207 This filename is needed in order to process all macros whose definition
16208 (or undefinition) comes from the command line. These macros are defined
16209 before the first DW_MACINFO_start_file entry, and yet still need to be
16210 associated to the base file.
16211
16212 To determine the base file name, we scan the macro definitions until we
16213 reach the first DW_MACINFO_start_file entry. We then initialize
16214 CURRENT_FILE accordingly so that any macro definition found before the
16215 first DW_MACINFO_start_file can still be associated to the base file. */
16216
16217 mac_ptr = section->buffer + offset;
16218 mac_end = section->buffer + section->size;
16219
16220 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16221 &offset_size, section_is_gnu);
16222 if (mac_ptr == NULL)
16223 {
16224 /* We already issued a complaint. */
16225 return;
16226 }
16227
16228 do
16229 {
16230 /* Do we at least have room for a macinfo type byte? */
16231 if (mac_ptr >= mac_end)
16232 {
16233 /* Complaint is printed during the second pass as GDB will probably
16234 stop the first pass earlier upon finding
16235 DW_MACINFO_start_file. */
16236 break;
16237 }
16238
16239 macinfo_type = read_1_byte (abfd, mac_ptr);
16240 mac_ptr++;
16241
16242 /* Note that we rely on the fact that the corresponding GNU and
16243 DWARF constants are the same. */
16244 switch (macinfo_type)
16245 {
16246 /* A zero macinfo type indicates the end of the macro
16247 information. */
16248 case 0:
16249 break;
16250
16251 case DW_MACRO_GNU_define:
16252 case DW_MACRO_GNU_undef:
16253 /* Only skip the data by MAC_PTR. */
16254 {
16255 unsigned int bytes_read;
16256
16257 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16258 mac_ptr += bytes_read;
16259 read_direct_string (abfd, mac_ptr, &bytes_read);
16260 mac_ptr += bytes_read;
16261 }
16262 break;
16263
16264 case DW_MACRO_GNU_start_file:
16265 {
16266 unsigned int bytes_read;
16267 int line, file;
16268
16269 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16270 mac_ptr += bytes_read;
16271 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16272 mac_ptr += bytes_read;
16273
16274 current_file = macro_start_file (file, line, current_file,
16275 comp_dir, lh, objfile);
16276 }
16277 break;
16278
16279 case DW_MACRO_GNU_end_file:
16280 /* No data to skip by MAC_PTR. */
16281 break;
16282
16283 case DW_MACRO_GNU_define_indirect:
16284 case DW_MACRO_GNU_undef_indirect:
16285 {
16286 unsigned int bytes_read;
16287
16288 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16289 mac_ptr += bytes_read;
16290 mac_ptr += offset_size;
16291 }
16292 break;
16293
16294 case DW_MACRO_GNU_transparent_include:
16295 /* Note that, according to the spec, a transparent include
16296 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16297 skip this opcode. */
16298 mac_ptr += offset_size;
16299 break;
16300
16301 case DW_MACINFO_vendor_ext:
16302 /* Only skip the data by MAC_PTR. */
16303 if (!section_is_gnu)
16304 {
16305 unsigned int bytes_read;
16306
16307 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16308 mac_ptr += bytes_read;
16309 read_direct_string (abfd, mac_ptr, &bytes_read);
16310 mac_ptr += bytes_read;
16311 }
16312 /* FALLTHROUGH */
16313
16314 default:
16315 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16316 mac_ptr, abfd, offset_size,
16317 section);
16318 if (mac_ptr == NULL)
16319 return;
16320 break;
16321 }
16322 } while (macinfo_type != 0 && current_file == NULL);
16323
16324 /* Second pass: Process all entries.
16325
16326 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16327 command-line macro definitions/undefinitions. This flag is unset when we
16328 reach the first DW_MACINFO_start_file entry. */
16329
16330 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16331 NULL, xcalloc, xfree);
16332 cleanup = make_cleanup_htab_delete (include_hash);
16333 mac_ptr = section->buffer + offset;
16334 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16335 *slot = mac_ptr;
16336 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16337 current_file, lh, comp_dir, section, section_is_gnu,
16338 offset_size, objfile, include_hash);
16339 do_cleanups (cleanup);
16340 }
16341
16342 /* Check if the attribute's form is a DW_FORM_block*
16343 if so return true else false. */
16344
16345 static int
16346 attr_form_is_block (struct attribute *attr)
16347 {
16348 return (attr == NULL ? 0 :
16349 attr->form == DW_FORM_block1
16350 || attr->form == DW_FORM_block2
16351 || attr->form == DW_FORM_block4
16352 || attr->form == DW_FORM_block
16353 || attr->form == DW_FORM_exprloc);
16354 }
16355
16356 /* Return non-zero if ATTR's value is a section offset --- classes
16357 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16358 You may use DW_UNSND (attr) to retrieve such offsets.
16359
16360 Section 7.5.4, "Attribute Encodings", explains that no attribute
16361 may have a value that belongs to more than one of these classes; it
16362 would be ambiguous if we did, because we use the same forms for all
16363 of them. */
16364
16365 static int
16366 attr_form_is_section_offset (struct attribute *attr)
16367 {
16368 return (attr->form == DW_FORM_data4
16369 || attr->form == DW_FORM_data8
16370 || attr->form == DW_FORM_sec_offset);
16371 }
16372
16373 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16374 zero otherwise. When this function returns true, you can apply
16375 dwarf2_get_attr_constant_value to it.
16376
16377 However, note that for some attributes you must check
16378 attr_form_is_section_offset before using this test. DW_FORM_data4
16379 and DW_FORM_data8 are members of both the constant class, and of
16380 the classes that contain offsets into other debug sections
16381 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16382 that, if an attribute's can be either a constant or one of the
16383 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16384 taken as section offsets, not constants. */
16385
16386 static int
16387 attr_form_is_constant (struct attribute *attr)
16388 {
16389 switch (attr->form)
16390 {
16391 case DW_FORM_sdata:
16392 case DW_FORM_udata:
16393 case DW_FORM_data1:
16394 case DW_FORM_data2:
16395 case DW_FORM_data4:
16396 case DW_FORM_data8:
16397 return 1;
16398 default:
16399 return 0;
16400 }
16401 }
16402
16403 /* Return the .debug_loc section to use for CU.
16404 For DWO files use .debug_loc.dwo. */
16405
16406 static struct dwarf2_section_info *
16407 cu_debug_loc_section (struct dwarf2_cu *cu)
16408 {
16409 if (cu->dwo_unit)
16410 return &cu->dwo_unit->dwo_file->sections.loc;
16411 return &dwarf2_per_objfile->loc;
16412 }
16413
16414 /* A helper function that fills in a dwarf2_loclist_baton. */
16415
16416 static void
16417 fill_in_loclist_baton (struct dwarf2_cu *cu,
16418 struct dwarf2_loclist_baton *baton,
16419 struct attribute *attr)
16420 {
16421 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16422
16423 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16424
16425 baton->per_cu = cu->per_cu;
16426 gdb_assert (baton->per_cu);
16427 /* We don't know how long the location list is, but make sure we
16428 don't run off the edge of the section. */
16429 baton->size = section->size - DW_UNSND (attr);
16430 baton->data = section->buffer + DW_UNSND (attr);
16431 baton->base_address = cu->base_address;
16432 }
16433
16434 static void
16435 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16436 struct dwarf2_cu *cu)
16437 {
16438 struct objfile *objfile = dwarf2_per_objfile->objfile;
16439 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16440
16441 if (attr_form_is_section_offset (attr)
16442 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16443 the section. If so, fall through to the complaint in the
16444 other branch. */
16445 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16446 {
16447 struct dwarf2_loclist_baton *baton;
16448
16449 baton = obstack_alloc (&objfile->objfile_obstack,
16450 sizeof (struct dwarf2_loclist_baton));
16451
16452 fill_in_loclist_baton (cu, baton, attr);
16453
16454 if (cu->base_known == 0)
16455 complaint (&symfile_complaints,
16456 _("Location list used without "
16457 "specifying the CU base address."));
16458
16459 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16460 SYMBOL_LOCATION_BATON (sym) = baton;
16461 }
16462 else
16463 {
16464 struct dwarf2_locexpr_baton *baton;
16465
16466 baton = obstack_alloc (&objfile->objfile_obstack,
16467 sizeof (struct dwarf2_locexpr_baton));
16468 baton->per_cu = cu->per_cu;
16469 gdb_assert (baton->per_cu);
16470
16471 if (attr_form_is_block (attr))
16472 {
16473 /* Note that we're just copying the block's data pointer
16474 here, not the actual data. We're still pointing into the
16475 info_buffer for SYM's objfile; right now we never release
16476 that buffer, but when we do clean up properly this may
16477 need to change. */
16478 baton->size = DW_BLOCK (attr)->size;
16479 baton->data = DW_BLOCK (attr)->data;
16480 }
16481 else
16482 {
16483 dwarf2_invalid_attrib_class_complaint ("location description",
16484 SYMBOL_NATURAL_NAME (sym));
16485 baton->size = 0;
16486 }
16487
16488 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16489 SYMBOL_LOCATION_BATON (sym) = baton;
16490 }
16491 }
16492
16493 /* Return the OBJFILE associated with the compilation unit CU. If CU
16494 came from a separate debuginfo file, then the master objfile is
16495 returned. */
16496
16497 struct objfile *
16498 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16499 {
16500 struct objfile *objfile = per_cu->objfile;
16501
16502 /* Return the master objfile, so that we can report and look up the
16503 correct file containing this variable. */
16504 if (objfile->separate_debug_objfile_backlink)
16505 objfile = objfile->separate_debug_objfile_backlink;
16506
16507 return objfile;
16508 }
16509
16510 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16511 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16512 CU_HEADERP first. */
16513
16514 static const struct comp_unit_head *
16515 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16516 struct dwarf2_per_cu_data *per_cu)
16517 {
16518 struct objfile *objfile;
16519 struct dwarf2_per_objfile *per_objfile;
16520 gdb_byte *info_ptr;
16521
16522 if (per_cu->cu)
16523 return &per_cu->cu->header;
16524
16525 objfile = per_cu->objfile;
16526 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16527 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16528
16529 memset (cu_headerp, 0, sizeof (*cu_headerp));
16530 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16531
16532 return cu_headerp;
16533 }
16534
16535 /* Return the address size given in the compilation unit header for CU. */
16536
16537 int
16538 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16539 {
16540 struct comp_unit_head cu_header_local;
16541 const struct comp_unit_head *cu_headerp;
16542
16543 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16544
16545 return cu_headerp->addr_size;
16546 }
16547
16548 /* Return the offset size given in the compilation unit header for CU. */
16549
16550 int
16551 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16552 {
16553 struct comp_unit_head cu_header_local;
16554 const struct comp_unit_head *cu_headerp;
16555
16556 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16557
16558 return cu_headerp->offset_size;
16559 }
16560
16561 /* See its dwarf2loc.h declaration. */
16562
16563 int
16564 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16565 {
16566 struct comp_unit_head cu_header_local;
16567 const struct comp_unit_head *cu_headerp;
16568
16569 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16570
16571 if (cu_headerp->version == 2)
16572 return cu_headerp->addr_size;
16573 else
16574 return cu_headerp->offset_size;
16575 }
16576
16577 /* Return the text offset of the CU. The returned offset comes from
16578 this CU's objfile. If this objfile came from a separate debuginfo
16579 file, then the offset may be different from the corresponding
16580 offset in the parent objfile. */
16581
16582 CORE_ADDR
16583 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16584 {
16585 struct objfile *objfile = per_cu->objfile;
16586
16587 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16588 }
16589
16590 /* Locate the .debug_info compilation unit from CU's objfile which contains
16591 the DIE at OFFSET. Raises an error on failure. */
16592
16593 static struct dwarf2_per_cu_data *
16594 dwarf2_find_containing_comp_unit (sect_offset offset,
16595 struct objfile *objfile)
16596 {
16597 struct dwarf2_per_cu_data *this_cu;
16598 int low, high;
16599
16600 low = 0;
16601 high = dwarf2_per_objfile->n_comp_units - 1;
16602 while (high > low)
16603 {
16604 int mid = low + (high - low) / 2;
16605
16606 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16607 >= offset.sect_off)
16608 high = mid;
16609 else
16610 low = mid + 1;
16611 }
16612 gdb_assert (low == high);
16613 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16614 > offset.sect_off)
16615 {
16616 if (low == 0)
16617 error (_("Dwarf Error: could not find partial DIE containing "
16618 "offset 0x%lx [in module %s]"),
16619 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16620
16621 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16622 <= offset.sect_off);
16623 return dwarf2_per_objfile->all_comp_units[low-1];
16624 }
16625 else
16626 {
16627 this_cu = dwarf2_per_objfile->all_comp_units[low];
16628 if (low == dwarf2_per_objfile->n_comp_units - 1
16629 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16630 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16631 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16632 return this_cu;
16633 }
16634 }
16635
16636 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16637
16638 static void
16639 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16640 {
16641 memset (cu, 0, sizeof (*cu));
16642 per_cu->cu = cu;
16643 cu->per_cu = per_cu;
16644 cu->objfile = per_cu->objfile;
16645 obstack_init (&cu->comp_unit_obstack);
16646 }
16647
16648 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16649
16650 static void
16651 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16652 enum language pretend_language)
16653 {
16654 struct attribute *attr;
16655
16656 /* Set the language we're debugging. */
16657 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16658 if (attr)
16659 set_cu_language (DW_UNSND (attr), cu);
16660 else
16661 {
16662 cu->language = pretend_language;
16663 cu->language_defn = language_def (cu->language);
16664 }
16665
16666 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16667 if (attr)
16668 cu->producer = DW_STRING (attr);
16669 }
16670
16671 /* Release one cached compilation unit, CU. We unlink it from the tree
16672 of compilation units, but we don't remove it from the read_in_chain;
16673 the caller is responsible for that.
16674 NOTE: DATA is a void * because this function is also used as a
16675 cleanup routine. */
16676
16677 static void
16678 free_heap_comp_unit (void *data)
16679 {
16680 struct dwarf2_cu *cu = data;
16681
16682 gdb_assert (cu->per_cu != NULL);
16683 cu->per_cu->cu = NULL;
16684 cu->per_cu = NULL;
16685
16686 obstack_free (&cu->comp_unit_obstack, NULL);
16687
16688 xfree (cu);
16689 }
16690
16691 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16692 when we're finished with it. We can't free the pointer itself, but be
16693 sure to unlink it from the cache. Also release any associated storage. */
16694
16695 static void
16696 free_stack_comp_unit (void *data)
16697 {
16698 struct dwarf2_cu *cu = data;
16699
16700 gdb_assert (cu->per_cu != NULL);
16701 cu->per_cu->cu = NULL;
16702 cu->per_cu = NULL;
16703
16704 obstack_free (&cu->comp_unit_obstack, NULL);
16705 cu->partial_dies = NULL;
16706 }
16707
16708 /* Free all cached compilation units. */
16709
16710 static void
16711 free_cached_comp_units (void *data)
16712 {
16713 struct dwarf2_per_cu_data *per_cu, **last_chain;
16714
16715 per_cu = dwarf2_per_objfile->read_in_chain;
16716 last_chain = &dwarf2_per_objfile->read_in_chain;
16717 while (per_cu != NULL)
16718 {
16719 struct dwarf2_per_cu_data *next_cu;
16720
16721 next_cu = per_cu->cu->read_in_chain;
16722
16723 free_heap_comp_unit (per_cu->cu);
16724 *last_chain = next_cu;
16725
16726 per_cu = next_cu;
16727 }
16728 }
16729
16730 /* Increase the age counter on each cached compilation unit, and free
16731 any that are too old. */
16732
16733 static void
16734 age_cached_comp_units (void)
16735 {
16736 struct dwarf2_per_cu_data *per_cu, **last_chain;
16737
16738 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16739 per_cu = dwarf2_per_objfile->read_in_chain;
16740 while (per_cu != NULL)
16741 {
16742 per_cu->cu->last_used ++;
16743 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16744 dwarf2_mark (per_cu->cu);
16745 per_cu = per_cu->cu->read_in_chain;
16746 }
16747
16748 per_cu = dwarf2_per_objfile->read_in_chain;
16749 last_chain = &dwarf2_per_objfile->read_in_chain;
16750 while (per_cu != NULL)
16751 {
16752 struct dwarf2_per_cu_data *next_cu;
16753
16754 next_cu = per_cu->cu->read_in_chain;
16755
16756 if (!per_cu->cu->mark)
16757 {
16758 free_heap_comp_unit (per_cu->cu);
16759 *last_chain = next_cu;
16760 }
16761 else
16762 last_chain = &per_cu->cu->read_in_chain;
16763
16764 per_cu = next_cu;
16765 }
16766 }
16767
16768 /* Remove a single compilation unit from the cache. */
16769
16770 static void
16771 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16772 {
16773 struct dwarf2_per_cu_data *per_cu, **last_chain;
16774
16775 per_cu = dwarf2_per_objfile->read_in_chain;
16776 last_chain = &dwarf2_per_objfile->read_in_chain;
16777 while (per_cu != NULL)
16778 {
16779 struct dwarf2_per_cu_data *next_cu;
16780
16781 next_cu = per_cu->cu->read_in_chain;
16782
16783 if (per_cu == target_per_cu)
16784 {
16785 free_heap_comp_unit (per_cu->cu);
16786 per_cu->cu = NULL;
16787 *last_chain = next_cu;
16788 break;
16789 }
16790 else
16791 last_chain = &per_cu->cu->read_in_chain;
16792
16793 per_cu = next_cu;
16794 }
16795 }
16796
16797 /* Release all extra memory associated with OBJFILE. */
16798
16799 void
16800 dwarf2_free_objfile (struct objfile *objfile)
16801 {
16802 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16803
16804 if (dwarf2_per_objfile == NULL)
16805 return;
16806
16807 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16808 free_cached_comp_units (NULL);
16809
16810 if (dwarf2_per_objfile->quick_file_names_table)
16811 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16812
16813 /* Everything else should be on the objfile obstack. */
16814 }
16815
16816 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16817 We store these in a hash table separate from the DIEs, and preserve them
16818 when the DIEs are flushed out of cache.
16819
16820 The CU "per_cu" pointer is needed because offset alone is not enough to
16821 uniquely identify the type. A file may have multiple .debug_types sections,
16822 or the type may come from a DWO file. We have to use something in
16823 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16824 routine, get_die_type_at_offset, from outside this file, and thus won't
16825 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16826 of the objfile. */
16827
16828 struct dwarf2_per_cu_offset_and_type
16829 {
16830 const struct dwarf2_per_cu_data *per_cu;
16831 sect_offset offset;
16832 struct type *type;
16833 };
16834
16835 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16836
16837 static hashval_t
16838 per_cu_offset_and_type_hash (const void *item)
16839 {
16840 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16841
16842 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16843 }
16844
16845 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16846
16847 static int
16848 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16849 {
16850 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16851 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16852
16853 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16854 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16855 }
16856
16857 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16858 table if necessary. For convenience, return TYPE.
16859
16860 The DIEs reading must have careful ordering to:
16861 * Not cause infite loops trying to read in DIEs as a prerequisite for
16862 reading current DIE.
16863 * Not trying to dereference contents of still incompletely read in types
16864 while reading in other DIEs.
16865 * Enable referencing still incompletely read in types just by a pointer to
16866 the type without accessing its fields.
16867
16868 Therefore caller should follow these rules:
16869 * Try to fetch any prerequisite types we may need to build this DIE type
16870 before building the type and calling set_die_type.
16871 * After building type call set_die_type for current DIE as soon as
16872 possible before fetching more types to complete the current type.
16873 * Make the type as complete as possible before fetching more types. */
16874
16875 static struct type *
16876 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16877 {
16878 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16879 struct objfile *objfile = cu->objfile;
16880
16881 /* For Ada types, make sure that the gnat-specific data is always
16882 initialized (if not already set). There are a few types where
16883 we should not be doing so, because the type-specific area is
16884 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16885 where the type-specific area is used to store the floatformat).
16886 But this is not a problem, because the gnat-specific information
16887 is actually not needed for these types. */
16888 if (need_gnat_info (cu)
16889 && TYPE_CODE (type) != TYPE_CODE_FUNC
16890 && TYPE_CODE (type) != TYPE_CODE_FLT
16891 && !HAVE_GNAT_AUX_INFO (type))
16892 INIT_GNAT_SPECIFIC (type);
16893
16894 if (dwarf2_per_objfile->die_type_hash == NULL)
16895 {
16896 dwarf2_per_objfile->die_type_hash =
16897 htab_create_alloc_ex (127,
16898 per_cu_offset_and_type_hash,
16899 per_cu_offset_and_type_eq,
16900 NULL,
16901 &objfile->objfile_obstack,
16902 hashtab_obstack_allocate,
16903 dummy_obstack_deallocate);
16904 }
16905
16906 ofs.per_cu = cu->per_cu;
16907 ofs.offset = die->offset;
16908 ofs.type = type;
16909 slot = (struct dwarf2_per_cu_offset_and_type **)
16910 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16911 if (*slot)
16912 complaint (&symfile_complaints,
16913 _("A problem internal to GDB: DIE 0x%x has type already set"),
16914 die->offset.sect_off);
16915 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16916 **slot = ofs;
16917 return type;
16918 }
16919
16920 /* Look up the type for the die at OFFSET in the appropriate type_hash
16921 table, or return NULL if the die does not have a saved type. */
16922
16923 static struct type *
16924 get_die_type_at_offset (sect_offset offset,
16925 struct dwarf2_per_cu_data *per_cu)
16926 {
16927 struct dwarf2_per_cu_offset_and_type *slot, ofs;
16928
16929 if (dwarf2_per_objfile->die_type_hash == NULL)
16930 return NULL;
16931
16932 ofs.per_cu = per_cu;
16933 ofs.offset = offset;
16934 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
16935 if (slot)
16936 return slot->type;
16937 else
16938 return NULL;
16939 }
16940
16941 /* Look up the type for DIE in the appropriate type_hash table,
16942 or return NULL if DIE does not have a saved type. */
16943
16944 static struct type *
16945 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16946 {
16947 return get_die_type_at_offset (die->offset, cu->per_cu);
16948 }
16949
16950 /* Add a dependence relationship from CU to REF_PER_CU. */
16951
16952 static void
16953 dwarf2_add_dependence (struct dwarf2_cu *cu,
16954 struct dwarf2_per_cu_data *ref_per_cu)
16955 {
16956 void **slot;
16957
16958 if (cu->dependencies == NULL)
16959 cu->dependencies
16960 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16961 NULL, &cu->comp_unit_obstack,
16962 hashtab_obstack_allocate,
16963 dummy_obstack_deallocate);
16964
16965 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16966 if (*slot == NULL)
16967 *slot = ref_per_cu;
16968 }
16969
16970 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16971 Set the mark field in every compilation unit in the
16972 cache that we must keep because we are keeping CU. */
16973
16974 static int
16975 dwarf2_mark_helper (void **slot, void *data)
16976 {
16977 struct dwarf2_per_cu_data *per_cu;
16978
16979 per_cu = (struct dwarf2_per_cu_data *) *slot;
16980
16981 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16982 reading of the chain. As such dependencies remain valid it is not much
16983 useful to track and undo them during QUIT cleanups. */
16984 if (per_cu->cu == NULL)
16985 return 1;
16986
16987 if (per_cu->cu->mark)
16988 return 1;
16989 per_cu->cu->mark = 1;
16990
16991 if (per_cu->cu->dependencies != NULL)
16992 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16993
16994 return 1;
16995 }
16996
16997 /* Set the mark field in CU and in every other compilation unit in the
16998 cache that we must keep because we are keeping CU. */
16999
17000 static void
17001 dwarf2_mark (struct dwarf2_cu *cu)
17002 {
17003 if (cu->mark)
17004 return;
17005 cu->mark = 1;
17006 if (cu->dependencies != NULL)
17007 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17008 }
17009
17010 static void
17011 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17012 {
17013 while (per_cu)
17014 {
17015 per_cu->cu->mark = 0;
17016 per_cu = per_cu->cu->read_in_chain;
17017 }
17018 }
17019
17020 /* Trivial hash function for partial_die_info: the hash value of a DIE
17021 is its offset in .debug_info for this objfile. */
17022
17023 static hashval_t
17024 partial_die_hash (const void *item)
17025 {
17026 const struct partial_die_info *part_die = item;
17027
17028 return part_die->offset.sect_off;
17029 }
17030
17031 /* Trivial comparison function for partial_die_info structures: two DIEs
17032 are equal if they have the same offset. */
17033
17034 static int
17035 partial_die_eq (const void *item_lhs, const void *item_rhs)
17036 {
17037 const struct partial_die_info *part_die_lhs = item_lhs;
17038 const struct partial_die_info *part_die_rhs = item_rhs;
17039
17040 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17041 }
17042
17043 static struct cmd_list_element *set_dwarf2_cmdlist;
17044 static struct cmd_list_element *show_dwarf2_cmdlist;
17045
17046 static void
17047 set_dwarf2_cmd (char *args, int from_tty)
17048 {
17049 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17050 }
17051
17052 static void
17053 show_dwarf2_cmd (char *args, int from_tty)
17054 {
17055 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17056 }
17057
17058 /* If section described by INFO was mmapped, munmap it now. */
17059
17060 static void
17061 munmap_section_buffer (struct dwarf2_section_info *info)
17062 {
17063 if (info->map_addr != NULL)
17064 {
17065 #ifdef HAVE_MMAP
17066 int res;
17067
17068 res = munmap (info->map_addr, info->map_len);
17069 gdb_assert (res == 0);
17070 #else
17071 /* Without HAVE_MMAP, we should never be here to begin with. */
17072 gdb_assert_not_reached ("no mmap support");
17073 #endif
17074 }
17075 }
17076
17077 /* munmap debug sections for OBJFILE, if necessary. */
17078
17079 static void
17080 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17081 {
17082 struct dwarf2_per_objfile *data = d;
17083 int ix;
17084 struct dwarf2_section_info *section;
17085
17086 /* This is sorted according to the order they're defined in to make it easier
17087 to keep in sync. */
17088 munmap_section_buffer (&data->info);
17089 munmap_section_buffer (&data->abbrev);
17090 munmap_section_buffer (&data->line);
17091 munmap_section_buffer (&data->loc);
17092 munmap_section_buffer (&data->macinfo);
17093 munmap_section_buffer (&data->macro);
17094 munmap_section_buffer (&data->str);
17095 munmap_section_buffer (&data->ranges);
17096 munmap_section_buffer (&data->addr);
17097 munmap_section_buffer (&data->frame);
17098 munmap_section_buffer (&data->eh_frame);
17099 munmap_section_buffer (&data->gdb_index);
17100
17101 for (ix = 0;
17102 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17103 ++ix)
17104 munmap_section_buffer (section);
17105
17106 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17107 VEC_free (dwarf2_per_cu_ptr,
17108 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17109
17110 VEC_free (dwarf2_section_info_def, data->types);
17111
17112 if (data->dwo_files)
17113 free_dwo_files (data->dwo_files, objfile);
17114 }
17115
17116 \f
17117 /* The "save gdb-index" command. */
17118
17119 /* The contents of the hash table we create when building the string
17120 table. */
17121 struct strtab_entry
17122 {
17123 offset_type offset;
17124 const char *str;
17125 };
17126
17127 /* Hash function for a strtab_entry.
17128
17129 Function is used only during write_hash_table so no index format backward
17130 compatibility is needed. */
17131
17132 static hashval_t
17133 hash_strtab_entry (const void *e)
17134 {
17135 const struct strtab_entry *entry = e;
17136 return mapped_index_string_hash (INT_MAX, entry->str);
17137 }
17138
17139 /* Equality function for a strtab_entry. */
17140
17141 static int
17142 eq_strtab_entry (const void *a, const void *b)
17143 {
17144 const struct strtab_entry *ea = a;
17145 const struct strtab_entry *eb = b;
17146 return !strcmp (ea->str, eb->str);
17147 }
17148
17149 /* Create a strtab_entry hash table. */
17150
17151 static htab_t
17152 create_strtab (void)
17153 {
17154 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17155 xfree, xcalloc, xfree);
17156 }
17157
17158 /* Add a string to the constant pool. Return the string's offset in
17159 host order. */
17160
17161 static offset_type
17162 add_string (htab_t table, struct obstack *cpool, const char *str)
17163 {
17164 void **slot;
17165 struct strtab_entry entry;
17166 struct strtab_entry *result;
17167
17168 entry.str = str;
17169 slot = htab_find_slot (table, &entry, INSERT);
17170 if (*slot)
17171 result = *slot;
17172 else
17173 {
17174 result = XNEW (struct strtab_entry);
17175 result->offset = obstack_object_size (cpool);
17176 result->str = str;
17177 obstack_grow_str0 (cpool, str);
17178 *slot = result;
17179 }
17180 return result->offset;
17181 }
17182
17183 /* An entry in the symbol table. */
17184 struct symtab_index_entry
17185 {
17186 /* The name of the symbol. */
17187 const char *name;
17188 /* The offset of the name in the constant pool. */
17189 offset_type index_offset;
17190 /* A sorted vector of the indices of all the CUs that hold an object
17191 of this name. */
17192 VEC (offset_type) *cu_indices;
17193 };
17194
17195 /* The symbol table. This is a power-of-2-sized hash table. */
17196 struct mapped_symtab
17197 {
17198 offset_type n_elements;
17199 offset_type size;
17200 struct symtab_index_entry **data;
17201 };
17202
17203 /* Hash function for a symtab_index_entry. */
17204
17205 static hashval_t
17206 hash_symtab_entry (const void *e)
17207 {
17208 const struct symtab_index_entry *entry = e;
17209 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17210 sizeof (offset_type) * VEC_length (offset_type,
17211 entry->cu_indices),
17212 0);
17213 }
17214
17215 /* Equality function for a symtab_index_entry. */
17216
17217 static int
17218 eq_symtab_entry (const void *a, const void *b)
17219 {
17220 const struct symtab_index_entry *ea = a;
17221 const struct symtab_index_entry *eb = b;
17222 int len = VEC_length (offset_type, ea->cu_indices);
17223 if (len != VEC_length (offset_type, eb->cu_indices))
17224 return 0;
17225 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17226 VEC_address (offset_type, eb->cu_indices),
17227 sizeof (offset_type) * len);
17228 }
17229
17230 /* Destroy a symtab_index_entry. */
17231
17232 static void
17233 delete_symtab_entry (void *p)
17234 {
17235 struct symtab_index_entry *entry = p;
17236 VEC_free (offset_type, entry->cu_indices);
17237 xfree (entry);
17238 }
17239
17240 /* Create a hash table holding symtab_index_entry objects. */
17241
17242 static htab_t
17243 create_symbol_hash_table (void)
17244 {
17245 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17246 delete_symtab_entry, xcalloc, xfree);
17247 }
17248
17249 /* Create a new mapped symtab object. */
17250
17251 static struct mapped_symtab *
17252 create_mapped_symtab (void)
17253 {
17254 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17255 symtab->n_elements = 0;
17256 symtab->size = 1024;
17257 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17258 return symtab;
17259 }
17260
17261 /* Destroy a mapped_symtab. */
17262
17263 static void
17264 cleanup_mapped_symtab (void *p)
17265 {
17266 struct mapped_symtab *symtab = p;
17267 /* The contents of the array are freed when the other hash table is
17268 destroyed. */
17269 xfree (symtab->data);
17270 xfree (symtab);
17271 }
17272
17273 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17274 the slot.
17275
17276 Function is used only during write_hash_table so no index format backward
17277 compatibility is needed. */
17278
17279 static struct symtab_index_entry **
17280 find_slot (struct mapped_symtab *symtab, const char *name)
17281 {
17282 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17283
17284 index = hash & (symtab->size - 1);
17285 step = ((hash * 17) & (symtab->size - 1)) | 1;
17286
17287 for (;;)
17288 {
17289 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17290 return &symtab->data[index];
17291 index = (index + step) & (symtab->size - 1);
17292 }
17293 }
17294
17295 /* Expand SYMTAB's hash table. */
17296
17297 static void
17298 hash_expand (struct mapped_symtab *symtab)
17299 {
17300 offset_type old_size = symtab->size;
17301 offset_type i;
17302 struct symtab_index_entry **old_entries = symtab->data;
17303
17304 symtab->size *= 2;
17305 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17306
17307 for (i = 0; i < old_size; ++i)
17308 {
17309 if (old_entries[i])
17310 {
17311 struct symtab_index_entry **slot = find_slot (symtab,
17312 old_entries[i]->name);
17313 *slot = old_entries[i];
17314 }
17315 }
17316
17317 xfree (old_entries);
17318 }
17319
17320 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17321 is the index of the CU in which the symbol appears. */
17322
17323 static void
17324 add_index_entry (struct mapped_symtab *symtab, const char *name,
17325 offset_type cu_index)
17326 {
17327 struct symtab_index_entry **slot;
17328
17329 ++symtab->n_elements;
17330 if (4 * symtab->n_elements / 3 >= symtab->size)
17331 hash_expand (symtab);
17332
17333 slot = find_slot (symtab, name);
17334 if (!*slot)
17335 {
17336 *slot = XNEW (struct symtab_index_entry);
17337 (*slot)->name = name;
17338 (*slot)->cu_indices = NULL;
17339 }
17340 /* Don't push an index twice. Due to how we add entries we only
17341 have to check the last one. */
17342 if (VEC_empty (offset_type, (*slot)->cu_indices)
17343 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17344 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17345 }
17346
17347 /* Add a vector of indices to the constant pool. */
17348
17349 static offset_type
17350 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17351 struct symtab_index_entry *entry)
17352 {
17353 void **slot;
17354
17355 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17356 if (!*slot)
17357 {
17358 offset_type len = VEC_length (offset_type, entry->cu_indices);
17359 offset_type val = MAYBE_SWAP (len);
17360 offset_type iter;
17361 int i;
17362
17363 *slot = entry;
17364 entry->index_offset = obstack_object_size (cpool);
17365
17366 obstack_grow (cpool, &val, sizeof (val));
17367 for (i = 0;
17368 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17369 ++i)
17370 {
17371 val = MAYBE_SWAP (iter);
17372 obstack_grow (cpool, &val, sizeof (val));
17373 }
17374 }
17375 else
17376 {
17377 struct symtab_index_entry *old_entry = *slot;
17378 entry->index_offset = old_entry->index_offset;
17379 entry = old_entry;
17380 }
17381 return entry->index_offset;
17382 }
17383
17384 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17385 constant pool entries going into the obstack CPOOL. */
17386
17387 static void
17388 write_hash_table (struct mapped_symtab *symtab,
17389 struct obstack *output, struct obstack *cpool)
17390 {
17391 offset_type i;
17392 htab_t symbol_hash_table;
17393 htab_t str_table;
17394
17395 symbol_hash_table = create_symbol_hash_table ();
17396 str_table = create_strtab ();
17397
17398 /* We add all the index vectors to the constant pool first, to
17399 ensure alignment is ok. */
17400 for (i = 0; i < symtab->size; ++i)
17401 {
17402 if (symtab->data[i])
17403 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17404 }
17405
17406 /* Now write out the hash table. */
17407 for (i = 0; i < symtab->size; ++i)
17408 {
17409 offset_type str_off, vec_off;
17410
17411 if (symtab->data[i])
17412 {
17413 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17414 vec_off = symtab->data[i]->index_offset;
17415 }
17416 else
17417 {
17418 /* While 0 is a valid constant pool index, it is not valid
17419 to have 0 for both offsets. */
17420 str_off = 0;
17421 vec_off = 0;
17422 }
17423
17424 str_off = MAYBE_SWAP (str_off);
17425 vec_off = MAYBE_SWAP (vec_off);
17426
17427 obstack_grow (output, &str_off, sizeof (str_off));
17428 obstack_grow (output, &vec_off, sizeof (vec_off));
17429 }
17430
17431 htab_delete (str_table);
17432 htab_delete (symbol_hash_table);
17433 }
17434
17435 /* Struct to map psymtab to CU index in the index file. */
17436 struct psymtab_cu_index_map
17437 {
17438 struct partial_symtab *psymtab;
17439 unsigned int cu_index;
17440 };
17441
17442 static hashval_t
17443 hash_psymtab_cu_index (const void *item)
17444 {
17445 const struct psymtab_cu_index_map *map = item;
17446
17447 return htab_hash_pointer (map->psymtab);
17448 }
17449
17450 static int
17451 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17452 {
17453 const struct psymtab_cu_index_map *lhs = item_lhs;
17454 const struct psymtab_cu_index_map *rhs = item_rhs;
17455
17456 return lhs->psymtab == rhs->psymtab;
17457 }
17458
17459 /* Helper struct for building the address table. */
17460 struct addrmap_index_data
17461 {
17462 struct objfile *objfile;
17463 struct obstack *addr_obstack;
17464 htab_t cu_index_htab;
17465
17466 /* Non-zero if the previous_* fields are valid.
17467 We can't write an entry until we see the next entry (since it is only then
17468 that we know the end of the entry). */
17469 int previous_valid;
17470 /* Index of the CU in the table of all CUs in the index file. */
17471 unsigned int previous_cu_index;
17472 /* Start address of the CU. */
17473 CORE_ADDR previous_cu_start;
17474 };
17475
17476 /* Write an address entry to OBSTACK. */
17477
17478 static void
17479 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17480 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17481 {
17482 offset_type cu_index_to_write;
17483 char addr[8];
17484 CORE_ADDR baseaddr;
17485
17486 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17487
17488 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17489 obstack_grow (obstack, addr, 8);
17490 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17491 obstack_grow (obstack, addr, 8);
17492 cu_index_to_write = MAYBE_SWAP (cu_index);
17493 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17494 }
17495
17496 /* Worker function for traversing an addrmap to build the address table. */
17497
17498 static int
17499 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17500 {
17501 struct addrmap_index_data *data = datap;
17502 struct partial_symtab *pst = obj;
17503 offset_type cu_index;
17504 void **slot;
17505
17506 if (data->previous_valid)
17507 add_address_entry (data->objfile, data->addr_obstack,
17508 data->previous_cu_start, start_addr,
17509 data->previous_cu_index);
17510
17511 data->previous_cu_start = start_addr;
17512 if (pst != NULL)
17513 {
17514 struct psymtab_cu_index_map find_map, *map;
17515 find_map.psymtab = pst;
17516 map = htab_find (data->cu_index_htab, &find_map);
17517 gdb_assert (map != NULL);
17518 data->previous_cu_index = map->cu_index;
17519 data->previous_valid = 1;
17520 }
17521 else
17522 data->previous_valid = 0;
17523
17524 return 0;
17525 }
17526
17527 /* Write OBJFILE's address map to OBSTACK.
17528 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17529 in the index file. */
17530
17531 static void
17532 write_address_map (struct objfile *objfile, struct obstack *obstack,
17533 htab_t cu_index_htab)
17534 {
17535 struct addrmap_index_data addrmap_index_data;
17536
17537 /* When writing the address table, we have to cope with the fact that
17538 the addrmap iterator only provides the start of a region; we have to
17539 wait until the next invocation to get the start of the next region. */
17540
17541 addrmap_index_data.objfile = objfile;
17542 addrmap_index_data.addr_obstack = obstack;
17543 addrmap_index_data.cu_index_htab = cu_index_htab;
17544 addrmap_index_data.previous_valid = 0;
17545
17546 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17547 &addrmap_index_data);
17548
17549 /* It's highly unlikely the last entry (end address = 0xff...ff)
17550 is valid, but we should still handle it.
17551 The end address is recorded as the start of the next region, but that
17552 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17553 anyway. */
17554 if (addrmap_index_data.previous_valid)
17555 add_address_entry (objfile, obstack,
17556 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17557 addrmap_index_data.previous_cu_index);
17558 }
17559
17560 /* Add a list of partial symbols to SYMTAB. */
17561
17562 static void
17563 write_psymbols (struct mapped_symtab *symtab,
17564 htab_t psyms_seen,
17565 struct partial_symbol **psymp,
17566 int count,
17567 offset_type cu_index,
17568 int is_static)
17569 {
17570 for (; count-- > 0; ++psymp)
17571 {
17572 void **slot, *lookup;
17573
17574 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17575 error (_("Ada is not currently supported by the index"));
17576
17577 /* We only want to add a given psymbol once. However, we also
17578 want to account for whether it is global or static. So, we
17579 may add it twice, using slightly different values. */
17580 if (is_static)
17581 {
17582 uintptr_t val = 1 | (uintptr_t) *psymp;
17583
17584 lookup = (void *) val;
17585 }
17586 else
17587 lookup = *psymp;
17588
17589 /* Only add a given psymbol once. */
17590 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17591 if (!*slot)
17592 {
17593 *slot = lookup;
17594 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17595 }
17596 }
17597 }
17598
17599 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17600 exception if there is an error. */
17601
17602 static void
17603 write_obstack (FILE *file, struct obstack *obstack)
17604 {
17605 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17606 file)
17607 != obstack_object_size (obstack))
17608 error (_("couldn't data write to file"));
17609 }
17610
17611 /* Unlink a file if the argument is not NULL. */
17612
17613 static void
17614 unlink_if_set (void *p)
17615 {
17616 char **filename = p;
17617 if (*filename)
17618 unlink (*filename);
17619 }
17620
17621 /* A helper struct used when iterating over debug_types. */
17622 struct signatured_type_index_data
17623 {
17624 struct objfile *objfile;
17625 struct mapped_symtab *symtab;
17626 struct obstack *types_list;
17627 htab_t psyms_seen;
17628 int cu_index;
17629 };
17630
17631 /* A helper function that writes a single signatured_type to an
17632 obstack. */
17633
17634 static int
17635 write_one_signatured_type (void **slot, void *d)
17636 {
17637 struct signatured_type_index_data *info = d;
17638 struct signatured_type *entry = (struct signatured_type *) *slot;
17639 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17640 struct partial_symtab *psymtab = per_cu->v.psymtab;
17641 gdb_byte val[8];
17642
17643 write_psymbols (info->symtab,
17644 info->psyms_seen,
17645 info->objfile->global_psymbols.list
17646 + psymtab->globals_offset,
17647 psymtab->n_global_syms, info->cu_index,
17648 0);
17649 write_psymbols (info->symtab,
17650 info->psyms_seen,
17651 info->objfile->static_psymbols.list
17652 + psymtab->statics_offset,
17653 psymtab->n_static_syms, info->cu_index,
17654 1);
17655
17656 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17657 entry->per_cu.offset.sect_off);
17658 obstack_grow (info->types_list, val, 8);
17659 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17660 entry->type_offset_in_tu.cu_off);
17661 obstack_grow (info->types_list, val, 8);
17662 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17663 obstack_grow (info->types_list, val, 8);
17664
17665 ++info->cu_index;
17666
17667 return 1;
17668 }
17669
17670 /* Recurse into all "included" dependencies and write their symbols as
17671 if they appeared in this psymtab. */
17672
17673 static void
17674 recursively_write_psymbols (struct objfile *objfile,
17675 struct partial_symtab *psymtab,
17676 struct mapped_symtab *symtab,
17677 htab_t psyms_seen,
17678 offset_type cu_index)
17679 {
17680 int i;
17681
17682 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17683 if (psymtab->dependencies[i]->user != NULL)
17684 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17685 symtab, psyms_seen, cu_index);
17686
17687 write_psymbols (symtab,
17688 psyms_seen,
17689 objfile->global_psymbols.list + psymtab->globals_offset,
17690 psymtab->n_global_syms, cu_index,
17691 0);
17692 write_psymbols (symtab,
17693 psyms_seen,
17694 objfile->static_psymbols.list + psymtab->statics_offset,
17695 psymtab->n_static_syms, cu_index,
17696 1);
17697 }
17698
17699 /* Create an index file for OBJFILE in the directory DIR. */
17700
17701 static void
17702 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17703 {
17704 struct cleanup *cleanup;
17705 char *filename, *cleanup_filename;
17706 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17707 struct obstack cu_list, types_cu_list;
17708 int i;
17709 FILE *out_file;
17710 struct mapped_symtab *symtab;
17711 offset_type val, size_of_contents, total_len;
17712 struct stat st;
17713 char buf[8];
17714 htab_t psyms_seen;
17715 htab_t cu_index_htab;
17716 struct psymtab_cu_index_map *psymtab_cu_index_map;
17717
17718 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17719 return;
17720
17721 if (dwarf2_per_objfile->using_index)
17722 error (_("Cannot use an index to create the index"));
17723
17724 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17725 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17726
17727 if (stat (objfile->name, &st) < 0)
17728 perror_with_name (objfile->name);
17729
17730 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17731 INDEX_SUFFIX, (char *) NULL);
17732 cleanup = make_cleanup (xfree, filename);
17733
17734 out_file = fopen (filename, "wb");
17735 if (!out_file)
17736 error (_("Can't open `%s' for writing"), filename);
17737
17738 cleanup_filename = filename;
17739 make_cleanup (unlink_if_set, &cleanup_filename);
17740
17741 symtab = create_mapped_symtab ();
17742 make_cleanup (cleanup_mapped_symtab, symtab);
17743
17744 obstack_init (&addr_obstack);
17745 make_cleanup_obstack_free (&addr_obstack);
17746
17747 obstack_init (&cu_list);
17748 make_cleanup_obstack_free (&cu_list);
17749
17750 obstack_init (&types_cu_list);
17751 make_cleanup_obstack_free (&types_cu_list);
17752
17753 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17754 NULL, xcalloc, xfree);
17755 make_cleanup_htab_delete (psyms_seen);
17756
17757 /* While we're scanning CU's create a table that maps a psymtab pointer
17758 (which is what addrmap records) to its index (which is what is recorded
17759 in the index file). This will later be needed to write the address
17760 table. */
17761 cu_index_htab = htab_create_alloc (100,
17762 hash_psymtab_cu_index,
17763 eq_psymtab_cu_index,
17764 NULL, xcalloc, xfree);
17765 make_cleanup_htab_delete (cu_index_htab);
17766 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17767 xmalloc (sizeof (struct psymtab_cu_index_map)
17768 * dwarf2_per_objfile->n_comp_units);
17769 make_cleanup (xfree, psymtab_cu_index_map);
17770
17771 /* The CU list is already sorted, so we don't need to do additional
17772 work here. Also, the debug_types entries do not appear in
17773 all_comp_units, but only in their own hash table. */
17774 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17775 {
17776 struct dwarf2_per_cu_data *per_cu
17777 = dwarf2_per_objfile->all_comp_units[i];
17778 struct partial_symtab *psymtab = per_cu->v.psymtab;
17779 gdb_byte val[8];
17780 struct psymtab_cu_index_map *map;
17781 void **slot;
17782
17783 if (psymtab->user == NULL)
17784 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
17785
17786 map = &psymtab_cu_index_map[i];
17787 map->psymtab = psymtab;
17788 map->cu_index = i;
17789 slot = htab_find_slot (cu_index_htab, map, INSERT);
17790 gdb_assert (slot != NULL);
17791 gdb_assert (*slot == NULL);
17792 *slot = map;
17793
17794 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17795 per_cu->offset.sect_off);
17796 obstack_grow (&cu_list, val, 8);
17797 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17798 obstack_grow (&cu_list, val, 8);
17799 }
17800
17801 /* Dump the address map. */
17802 write_address_map (objfile, &addr_obstack, cu_index_htab);
17803
17804 /* Write out the .debug_type entries, if any. */
17805 if (dwarf2_per_objfile->signatured_types)
17806 {
17807 struct signatured_type_index_data sig_data;
17808
17809 sig_data.objfile = objfile;
17810 sig_data.symtab = symtab;
17811 sig_data.types_list = &types_cu_list;
17812 sig_data.psyms_seen = psyms_seen;
17813 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17814 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17815 write_one_signatured_type, &sig_data);
17816 }
17817
17818 obstack_init (&constant_pool);
17819 make_cleanup_obstack_free (&constant_pool);
17820 obstack_init (&symtab_obstack);
17821 make_cleanup_obstack_free (&symtab_obstack);
17822 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17823
17824 obstack_init (&contents);
17825 make_cleanup_obstack_free (&contents);
17826 size_of_contents = 6 * sizeof (offset_type);
17827 total_len = size_of_contents;
17828
17829 /* The version number. */
17830 val = MAYBE_SWAP (6);
17831 obstack_grow (&contents, &val, sizeof (val));
17832
17833 /* The offset of the CU list from the start of the file. */
17834 val = MAYBE_SWAP (total_len);
17835 obstack_grow (&contents, &val, sizeof (val));
17836 total_len += obstack_object_size (&cu_list);
17837
17838 /* The offset of the types CU list from the start of the file. */
17839 val = MAYBE_SWAP (total_len);
17840 obstack_grow (&contents, &val, sizeof (val));
17841 total_len += obstack_object_size (&types_cu_list);
17842
17843 /* The offset of the address table from the start of the file. */
17844 val = MAYBE_SWAP (total_len);
17845 obstack_grow (&contents, &val, sizeof (val));
17846 total_len += obstack_object_size (&addr_obstack);
17847
17848 /* The offset of the symbol table from the start of the file. */
17849 val = MAYBE_SWAP (total_len);
17850 obstack_grow (&contents, &val, sizeof (val));
17851 total_len += obstack_object_size (&symtab_obstack);
17852
17853 /* The offset of the constant pool from the start of the file. */
17854 val = MAYBE_SWAP (total_len);
17855 obstack_grow (&contents, &val, sizeof (val));
17856 total_len += obstack_object_size (&constant_pool);
17857
17858 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17859
17860 write_obstack (out_file, &contents);
17861 write_obstack (out_file, &cu_list);
17862 write_obstack (out_file, &types_cu_list);
17863 write_obstack (out_file, &addr_obstack);
17864 write_obstack (out_file, &symtab_obstack);
17865 write_obstack (out_file, &constant_pool);
17866
17867 fclose (out_file);
17868
17869 /* We want to keep the file, so we set cleanup_filename to NULL
17870 here. See unlink_if_set. */
17871 cleanup_filename = NULL;
17872
17873 do_cleanups (cleanup);
17874 }
17875
17876 /* Implementation of the `save gdb-index' command.
17877
17878 Note that the file format used by this command is documented in the
17879 GDB manual. Any changes here must be documented there. */
17880
17881 static void
17882 save_gdb_index_command (char *arg, int from_tty)
17883 {
17884 struct objfile *objfile;
17885
17886 if (!arg || !*arg)
17887 error (_("usage: save gdb-index DIRECTORY"));
17888
17889 ALL_OBJFILES (objfile)
17890 {
17891 struct stat st;
17892
17893 /* If the objfile does not correspond to an actual file, skip it. */
17894 if (stat (objfile->name, &st) < 0)
17895 continue;
17896
17897 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17898 if (dwarf2_per_objfile)
17899 {
17900 volatile struct gdb_exception except;
17901
17902 TRY_CATCH (except, RETURN_MASK_ERROR)
17903 {
17904 write_psymtabs_to_index (objfile, arg);
17905 }
17906 if (except.reason < 0)
17907 exception_fprintf (gdb_stderr, except,
17908 _("Error while writing index for `%s': "),
17909 objfile->name);
17910 }
17911 }
17912 }
17913
17914 \f
17915
17916 int dwarf2_always_disassemble;
17917
17918 static void
17919 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17920 struct cmd_list_element *c, const char *value)
17921 {
17922 fprintf_filtered (file,
17923 _("Whether to always disassemble "
17924 "DWARF expressions is %s.\n"),
17925 value);
17926 }
17927
17928 static void
17929 show_check_physname (struct ui_file *file, int from_tty,
17930 struct cmd_list_element *c, const char *value)
17931 {
17932 fprintf_filtered (file,
17933 _("Whether to check \"physname\" is %s.\n"),
17934 value);
17935 }
17936
17937 void _initialize_dwarf2_read (void);
17938
17939 void
17940 _initialize_dwarf2_read (void)
17941 {
17942 struct cmd_list_element *c;
17943
17944 dwarf2_objfile_data_key
17945 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17946
17947 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17948 Set DWARF 2 specific variables.\n\
17949 Configure DWARF 2 variables such as the cache size"),
17950 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17951 0/*allow-unknown*/, &maintenance_set_cmdlist);
17952
17953 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17954 Show DWARF 2 specific variables\n\
17955 Show DWARF 2 variables such as the cache size"),
17956 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17957 0/*allow-unknown*/, &maintenance_show_cmdlist);
17958
17959 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17960 &dwarf2_max_cache_age, _("\
17961 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17962 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17963 A higher limit means that cached compilation units will be stored\n\
17964 in memory longer, and more total memory will be used. Zero disables\n\
17965 caching, which can slow down startup."),
17966 NULL,
17967 show_dwarf2_max_cache_age,
17968 &set_dwarf2_cmdlist,
17969 &show_dwarf2_cmdlist);
17970
17971 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17972 &dwarf2_always_disassemble, _("\
17973 Set whether `info address' always disassembles DWARF expressions."), _("\
17974 Show whether `info address' always disassembles DWARF expressions."), _("\
17975 When enabled, DWARF expressions are always printed in an assembly-like\n\
17976 syntax. When disabled, expressions will be printed in a more\n\
17977 conversational style, when possible."),
17978 NULL,
17979 show_dwarf2_always_disassemble,
17980 &set_dwarf2_cmdlist,
17981 &show_dwarf2_cmdlist);
17982
17983 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17984 Set debugging of the dwarf2 DIE reader."), _("\
17985 Show debugging of the dwarf2 DIE reader."), _("\
17986 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17987 The value is the maximum depth to print."),
17988 NULL,
17989 NULL,
17990 &setdebuglist, &showdebuglist);
17991
17992 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17993 Set cross-checking of \"physname\" code against demangler."), _("\
17994 Show cross-checking of \"physname\" code against demangler."), _("\
17995 When enabled, GDB's internal \"physname\" code is checked against\n\
17996 the demangler."),
17997 NULL, show_check_physname,
17998 &setdebuglist, &showdebuglist);
17999
18000 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18001 _("\
18002 Save a gdb-index file.\n\
18003 Usage: save gdb-index DIRECTORY"),
18004 &save_cmdlist);
18005 set_cmd_completer (c, filename_completer);
18006 }
This page took 0.52441 seconds and 4 git commands to generate.