*** empty log message ***
[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 (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 char *name, *comp_dir;
2436 void **slot;
2437 struct quick_file_names *qfn;
2438 unsigned int line_offset;
2439
2440 lh = NULL;
2441 slot = NULL;
2442 line_offset = 0;
2443
2444 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2445 if (attr)
2446 {
2447 struct quick_file_names find_entry;
2448
2449 line_offset = DW_UNSND (attr);
2450
2451 /* We may have already read in this line header (TU line header sharing).
2452 If we have we're done. */
2453 find_entry.offset = line_offset;
2454 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2455 &find_entry, INSERT);
2456 if (*slot != NULL)
2457 {
2458 this_cu->v.quick->file_names = *slot;
2459 return;
2460 }
2461
2462 lh = dwarf_decode_line_header (line_offset, cu);
2463 }
2464 if (lh == NULL)
2465 {
2466 this_cu->v.quick->no_file_data = 1;
2467 return;
2468 }
2469
2470 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2471 qfn->offset = line_offset;
2472 gdb_assert (slot != NULL);
2473 *slot = qfn;
2474
2475 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2476
2477 qfn->num_file_names = lh->num_file_names;
2478 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2479 lh->num_file_names * sizeof (char *));
2480 for (i = 0; i < lh->num_file_names; ++i)
2481 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2482 qfn->real_names = NULL;
2483
2484 free_line_header (lh);
2485
2486 this_cu->v.quick->file_names = qfn;
2487 }
2488
2489 /* A helper for the "quick" functions which attempts to read the line
2490 table for THIS_CU. */
2491
2492 static struct quick_file_names *
2493 dw2_get_file_names (struct objfile *objfile,
2494 struct dwarf2_per_cu_data *this_cu)
2495 {
2496 if (this_cu->v.quick->file_names != NULL)
2497 return this_cu->v.quick->file_names;
2498 /* If we know there is no line data, no point in looking again. */
2499 if (this_cu->v.quick->no_file_data)
2500 return NULL;
2501
2502 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2503 in the stub for CUs, there's is no need to lookup the DWO file.
2504 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2505 DWO file. */
2506 if (this_cu->is_debug_types)
2507 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2508 else
2509 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2510
2511 if (this_cu->v.quick->no_file_data)
2512 return NULL;
2513 return this_cu->v.quick->file_names;
2514 }
2515
2516 /* A helper for the "quick" functions which computes and caches the
2517 real path for a given file name from the line table. */
2518
2519 static const char *
2520 dw2_get_real_path (struct objfile *objfile,
2521 struct quick_file_names *qfn, int index)
2522 {
2523 if (qfn->real_names == NULL)
2524 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2525 qfn->num_file_names, sizeof (char *));
2526
2527 if (qfn->real_names[index] == NULL)
2528 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2529
2530 return qfn->real_names[index];
2531 }
2532
2533 static struct symtab *
2534 dw2_find_last_source_symtab (struct objfile *objfile)
2535 {
2536 int index;
2537
2538 dw2_setup (objfile);
2539 index = dwarf2_per_objfile->n_comp_units - 1;
2540 return dw2_instantiate_symtab (dw2_get_cu (index));
2541 }
2542
2543 /* Traversal function for dw2_forget_cached_source_info. */
2544
2545 static int
2546 dw2_free_cached_file_names (void **slot, void *info)
2547 {
2548 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2549
2550 if (file_data->real_names)
2551 {
2552 int i;
2553
2554 for (i = 0; i < file_data->num_file_names; ++i)
2555 {
2556 xfree ((void*) file_data->real_names[i]);
2557 file_data->real_names[i] = NULL;
2558 }
2559 }
2560
2561 return 1;
2562 }
2563
2564 static void
2565 dw2_forget_cached_source_info (struct objfile *objfile)
2566 {
2567 dw2_setup (objfile);
2568
2569 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2570 dw2_free_cached_file_names, NULL);
2571 }
2572
2573 /* Helper function for dw2_map_symtabs_matching_filename that expands
2574 the symtabs and calls the iterator. */
2575
2576 static int
2577 dw2_map_expand_apply (struct objfile *objfile,
2578 struct dwarf2_per_cu_data *per_cu,
2579 const char *name,
2580 const char *full_path, const char *real_path,
2581 int (*callback) (struct symtab *, void *),
2582 void *data)
2583 {
2584 struct symtab *last_made = objfile->symtabs;
2585
2586 /* Don't visit already-expanded CUs. */
2587 if (per_cu->v.quick->symtab)
2588 return 0;
2589
2590 /* This may expand more than one symtab, and we want to iterate over
2591 all of them. */
2592 dw2_instantiate_symtab (per_cu);
2593
2594 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2595 objfile->symtabs, last_made);
2596 }
2597
2598 /* Implementation of the map_symtabs_matching_filename method. */
2599
2600 static int
2601 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2602 const char *full_path, const char *real_path,
2603 int (*callback) (struct symtab *, void *),
2604 void *data)
2605 {
2606 int i;
2607 const char *name_basename = lbasename (name);
2608 int name_len = strlen (name);
2609 int is_abs = IS_ABSOLUTE_PATH (name);
2610
2611 dw2_setup (objfile);
2612
2613 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2614 + dwarf2_per_objfile->n_type_units); ++i)
2615 {
2616 int j;
2617 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2618 struct quick_file_names *file_data;
2619
2620 /* We only need to look at symtabs not already expanded. */
2621 if (per_cu->v.quick->symtab)
2622 continue;
2623
2624 file_data = dw2_get_file_names (objfile, per_cu);
2625 if (file_data == NULL)
2626 continue;
2627
2628 for (j = 0; j < file_data->num_file_names; ++j)
2629 {
2630 const char *this_name = file_data->file_names[j];
2631
2632 if (FILENAME_CMP (name, this_name) == 0
2633 || (!is_abs && compare_filenames_for_search (this_name,
2634 name, name_len)))
2635 {
2636 if (dw2_map_expand_apply (objfile, per_cu,
2637 name, full_path, real_path,
2638 callback, data))
2639 return 1;
2640 }
2641
2642 /* Before we invoke realpath, which can get expensive when many
2643 files are involved, do a quick comparison of the basenames. */
2644 if (! basenames_may_differ
2645 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2646 continue;
2647
2648 if (full_path != NULL)
2649 {
2650 const char *this_real_name = dw2_get_real_path (objfile,
2651 file_data, j);
2652
2653 if (this_real_name != NULL
2654 && (FILENAME_CMP (full_path, this_real_name) == 0
2655 || (!is_abs
2656 && compare_filenames_for_search (this_real_name,
2657 name, name_len))))
2658 {
2659 if (dw2_map_expand_apply (objfile, per_cu,
2660 name, full_path, real_path,
2661 callback, data))
2662 return 1;
2663 }
2664 }
2665
2666 if (real_path != NULL)
2667 {
2668 const char *this_real_name = dw2_get_real_path (objfile,
2669 file_data, j);
2670
2671 if (this_real_name != NULL
2672 && (FILENAME_CMP (real_path, this_real_name) == 0
2673 || (!is_abs
2674 && compare_filenames_for_search (this_real_name,
2675 name, name_len))))
2676 {
2677 if (dw2_map_expand_apply (objfile, per_cu,
2678 name, full_path, real_path,
2679 callback, data))
2680 return 1;
2681 }
2682 }
2683 }
2684 }
2685
2686 return 0;
2687 }
2688
2689 static struct symtab *
2690 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2691 const char *name, domain_enum domain)
2692 {
2693 /* We do all the work in the pre_expand_symtabs_matching hook
2694 instead. */
2695 return NULL;
2696 }
2697
2698 /* A helper function that expands all symtabs that hold an object
2699 named NAME. */
2700
2701 static void
2702 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2703 {
2704 dw2_setup (objfile);
2705
2706 /* index_table is NULL if OBJF_READNOW. */
2707 if (dwarf2_per_objfile->index_table)
2708 {
2709 offset_type *vec;
2710
2711 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2712 name, &vec))
2713 {
2714 offset_type i, len = MAYBE_SWAP (*vec);
2715 for (i = 0; i < len; ++i)
2716 {
2717 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2718 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2719
2720 dw2_instantiate_symtab (per_cu);
2721 }
2722 }
2723 }
2724 }
2725
2726 static void
2727 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2728 enum block_enum block_kind, const char *name,
2729 domain_enum domain)
2730 {
2731 dw2_do_expand_symtabs_matching (objfile, name);
2732 }
2733
2734 static void
2735 dw2_print_stats (struct objfile *objfile)
2736 {
2737 int i, count;
2738
2739 dw2_setup (objfile);
2740 count = 0;
2741 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2742 + dwarf2_per_objfile->n_type_units); ++i)
2743 {
2744 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2745
2746 if (!per_cu->v.quick->symtab)
2747 ++count;
2748 }
2749 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2750 }
2751
2752 static void
2753 dw2_dump (struct objfile *objfile)
2754 {
2755 /* Nothing worth printing. */
2756 }
2757
2758 static void
2759 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2760 struct section_offsets *delta)
2761 {
2762 /* There's nothing to relocate here. */
2763 }
2764
2765 static void
2766 dw2_expand_symtabs_for_function (struct objfile *objfile,
2767 const char *func_name)
2768 {
2769 dw2_do_expand_symtabs_matching (objfile, func_name);
2770 }
2771
2772 static void
2773 dw2_expand_all_symtabs (struct objfile *objfile)
2774 {
2775 int i;
2776
2777 dw2_setup (objfile);
2778
2779 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2780 + dwarf2_per_objfile->n_type_units); ++i)
2781 {
2782 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2783
2784 dw2_instantiate_symtab (per_cu);
2785 }
2786 }
2787
2788 static void
2789 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2790 const char *filename)
2791 {
2792 int i;
2793
2794 dw2_setup (objfile);
2795
2796 /* We don't need to consider type units here.
2797 This is only called for examining code, e.g. expand_line_sal.
2798 There can be an order of magnitude (or more) more type units
2799 than comp units, and we avoid them if we can. */
2800
2801 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2802 {
2803 int j;
2804 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2805 struct quick_file_names *file_data;
2806
2807 /* We only need to look at symtabs not already expanded. */
2808 if (per_cu->v.quick->symtab)
2809 continue;
2810
2811 file_data = dw2_get_file_names (objfile, per_cu);
2812 if (file_data == NULL)
2813 continue;
2814
2815 for (j = 0; j < file_data->num_file_names; ++j)
2816 {
2817 const char *this_name = file_data->file_names[j];
2818 if (FILENAME_CMP (this_name, filename) == 0)
2819 {
2820 dw2_instantiate_symtab (per_cu);
2821 break;
2822 }
2823 }
2824 }
2825 }
2826
2827 static const char *
2828 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2829 {
2830 struct dwarf2_per_cu_data *per_cu;
2831 offset_type *vec;
2832 struct quick_file_names *file_data;
2833
2834 dw2_setup (objfile);
2835
2836 /* index_table is NULL if OBJF_READNOW. */
2837 if (!dwarf2_per_objfile->index_table)
2838 {
2839 struct symtab *s;
2840
2841 ALL_OBJFILE_SYMTABS (objfile, s)
2842 if (s->primary)
2843 {
2844 struct blockvector *bv = BLOCKVECTOR (s);
2845 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2846 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2847
2848 if (sym)
2849 return sym->symtab->filename;
2850 }
2851 return NULL;
2852 }
2853
2854 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2855 name, &vec))
2856 return NULL;
2857
2858 /* Note that this just looks at the very first one named NAME -- but
2859 actually we are looking for a function. find_main_filename
2860 should be rewritten so that it doesn't require a custom hook. It
2861 could just use the ordinary symbol tables. */
2862 /* vec[0] is the length, which must always be >0. */
2863 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2864
2865 file_data = dw2_get_file_names (objfile, per_cu);
2866 if (file_data == NULL
2867 || file_data->num_file_names == 0)
2868 return NULL;
2869
2870 return file_data->file_names[file_data->num_file_names - 1];
2871 }
2872
2873 static void
2874 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2875 struct objfile *objfile, int global,
2876 int (*callback) (struct block *,
2877 struct symbol *, void *),
2878 void *data, symbol_compare_ftype *match,
2879 symbol_compare_ftype *ordered_compare)
2880 {
2881 /* Currently unimplemented; used for Ada. The function can be called if the
2882 current language is Ada for a non-Ada objfile using GNU index. As Ada
2883 does not look for non-Ada symbols this function should just return. */
2884 }
2885
2886 static void
2887 dw2_expand_symtabs_matching
2888 (struct objfile *objfile,
2889 int (*file_matcher) (const char *, void *),
2890 int (*name_matcher) (const char *, void *),
2891 enum search_domain kind,
2892 void *data)
2893 {
2894 int i;
2895 offset_type iter;
2896 struct mapped_index *index;
2897
2898 dw2_setup (objfile);
2899
2900 /* index_table is NULL if OBJF_READNOW. */
2901 if (!dwarf2_per_objfile->index_table)
2902 return;
2903 index = dwarf2_per_objfile->index_table;
2904
2905 if (file_matcher != NULL)
2906 {
2907 struct cleanup *cleanup;
2908 htab_t visited_found, visited_not_found;
2909
2910 visited_found = htab_create_alloc (10,
2911 htab_hash_pointer, htab_eq_pointer,
2912 NULL, xcalloc, xfree);
2913 cleanup = make_cleanup_htab_delete (visited_found);
2914 visited_not_found = htab_create_alloc (10,
2915 htab_hash_pointer, htab_eq_pointer,
2916 NULL, xcalloc, xfree);
2917 make_cleanup_htab_delete (visited_not_found);
2918
2919 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2920 + dwarf2_per_objfile->n_type_units); ++i)
2921 {
2922 int j;
2923 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2924 struct quick_file_names *file_data;
2925 void **slot;
2926
2927 per_cu->v.quick->mark = 0;
2928
2929 /* We only need to look at symtabs not already expanded. */
2930 if (per_cu->v.quick->symtab)
2931 continue;
2932
2933 file_data = dw2_get_file_names (objfile, per_cu);
2934 if (file_data == NULL)
2935 continue;
2936
2937 if (htab_find (visited_not_found, file_data) != NULL)
2938 continue;
2939 else if (htab_find (visited_found, file_data) != NULL)
2940 {
2941 per_cu->v.quick->mark = 1;
2942 continue;
2943 }
2944
2945 for (j = 0; j < file_data->num_file_names; ++j)
2946 {
2947 if (file_matcher (file_data->file_names[j], data))
2948 {
2949 per_cu->v.quick->mark = 1;
2950 break;
2951 }
2952 }
2953
2954 slot = htab_find_slot (per_cu->v.quick->mark
2955 ? visited_found
2956 : visited_not_found,
2957 file_data, INSERT);
2958 *slot = file_data;
2959 }
2960
2961 do_cleanups (cleanup);
2962 }
2963
2964 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2965 {
2966 offset_type idx = 2 * iter;
2967 const char *name;
2968 offset_type *vec, vec_len, vec_idx;
2969
2970 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2971 continue;
2972
2973 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2974
2975 if (! (*name_matcher) (name, data))
2976 continue;
2977
2978 /* The name was matched, now expand corresponding CUs that were
2979 marked. */
2980 vec = (offset_type *) (index->constant_pool
2981 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2982 vec_len = MAYBE_SWAP (vec[0]);
2983 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2984 {
2985 struct dwarf2_per_cu_data *per_cu;
2986
2987 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2988 if (file_matcher == NULL || per_cu->v.quick->mark)
2989 dw2_instantiate_symtab (per_cu);
2990 }
2991 }
2992 }
2993
2994 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
2995 symtab. */
2996
2997 static struct symtab *
2998 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
2999 {
3000 int i;
3001
3002 if (BLOCKVECTOR (symtab) != NULL
3003 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3004 return symtab;
3005
3006 for (i = 0; symtab->includes[i]; ++i)
3007 {
3008 struct symtab *s;
3009
3010 s = recursively_find_pc_sect_symtab (s, pc);
3011 if (s != NULL)
3012 return s;
3013 }
3014
3015 return NULL;
3016 }
3017
3018 static struct symtab *
3019 dw2_find_pc_sect_symtab (struct objfile *objfile,
3020 struct minimal_symbol *msymbol,
3021 CORE_ADDR pc,
3022 struct obj_section *section,
3023 int warn_if_readin)
3024 {
3025 struct dwarf2_per_cu_data *data;
3026 struct symtab *result;
3027
3028 dw2_setup (objfile);
3029
3030 if (!objfile->psymtabs_addrmap)
3031 return NULL;
3032
3033 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3034 if (!data)
3035 return NULL;
3036
3037 if (warn_if_readin && data->v.quick->symtab)
3038 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3039 paddress (get_objfile_arch (objfile), pc));
3040
3041 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3042 gdb_assert (result != NULL);
3043 return result;
3044 }
3045
3046 static void
3047 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3048 void *data, int need_fullname)
3049 {
3050 int i;
3051 struct cleanup *cleanup;
3052 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3053 NULL, xcalloc, xfree);
3054
3055 cleanup = make_cleanup_htab_delete (visited);
3056 dw2_setup (objfile);
3057
3058 /* We can ignore file names coming from already-expanded CUs. */
3059 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3060 + dwarf2_per_objfile->n_type_units); ++i)
3061 {
3062 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3063
3064 if (per_cu->v.quick->symtab)
3065 {
3066 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3067 INSERT);
3068
3069 *slot = per_cu->v.quick->file_names;
3070 }
3071 }
3072
3073 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3074 + dwarf2_per_objfile->n_type_units); ++i)
3075 {
3076 int j;
3077 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3078 struct quick_file_names *file_data;
3079 void **slot;
3080
3081 /* We only need to look at symtabs not already expanded. */
3082 if (per_cu->v.quick->symtab)
3083 continue;
3084
3085 file_data = dw2_get_file_names (objfile, per_cu);
3086 if (file_data == NULL)
3087 continue;
3088
3089 slot = htab_find_slot (visited, file_data, INSERT);
3090 if (*slot)
3091 {
3092 /* Already visited. */
3093 continue;
3094 }
3095 *slot = file_data;
3096
3097 for (j = 0; j < file_data->num_file_names; ++j)
3098 {
3099 const char *this_real_name;
3100
3101 if (need_fullname)
3102 this_real_name = dw2_get_real_path (objfile, file_data, j);
3103 else
3104 this_real_name = NULL;
3105 (*fun) (file_data->file_names[j], this_real_name, data);
3106 }
3107 }
3108
3109 do_cleanups (cleanup);
3110 }
3111
3112 static int
3113 dw2_has_symbols (struct objfile *objfile)
3114 {
3115 return 1;
3116 }
3117
3118 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3119 {
3120 dw2_has_symbols,
3121 dw2_find_last_source_symtab,
3122 dw2_forget_cached_source_info,
3123 dw2_map_symtabs_matching_filename,
3124 dw2_lookup_symbol,
3125 dw2_pre_expand_symtabs_matching,
3126 dw2_print_stats,
3127 dw2_dump,
3128 dw2_relocate,
3129 dw2_expand_symtabs_for_function,
3130 dw2_expand_all_symtabs,
3131 dw2_expand_symtabs_with_filename,
3132 dw2_find_symbol_file,
3133 dw2_map_matching_symbols,
3134 dw2_expand_symtabs_matching,
3135 dw2_find_pc_sect_symtab,
3136 dw2_map_symbol_filenames
3137 };
3138
3139 /* Initialize for reading DWARF for this objfile. Return 0 if this
3140 file will use psymtabs, or 1 if using the GNU index. */
3141
3142 int
3143 dwarf2_initialize_objfile (struct objfile *objfile)
3144 {
3145 /* If we're about to read full symbols, don't bother with the
3146 indices. In this case we also don't care if some other debug
3147 format is making psymtabs, because they are all about to be
3148 expanded anyway. */
3149 if ((objfile->flags & OBJF_READNOW))
3150 {
3151 int i;
3152
3153 dwarf2_per_objfile->using_index = 1;
3154 create_all_comp_units (objfile);
3155 create_all_type_units (objfile);
3156 dwarf2_per_objfile->quick_file_names_table =
3157 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3158
3159 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3160 + dwarf2_per_objfile->n_type_units); ++i)
3161 {
3162 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3163
3164 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3165 struct dwarf2_per_cu_quick_data);
3166 }
3167
3168 /* Return 1 so that gdb sees the "quick" functions. However,
3169 these functions will be no-ops because we will have expanded
3170 all symtabs. */
3171 return 1;
3172 }
3173
3174 if (dwarf2_read_index (objfile))
3175 return 1;
3176
3177 return 0;
3178 }
3179
3180 \f
3181
3182 /* Build a partial symbol table. */
3183
3184 void
3185 dwarf2_build_psymtabs (struct objfile *objfile)
3186 {
3187 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3188 {
3189 init_psymbol_list (objfile, 1024);
3190 }
3191
3192 dwarf2_build_psymtabs_hard (objfile);
3193 }
3194
3195 /* Return TRUE if OFFSET is within CU_HEADER. */
3196
3197 static inline int
3198 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3199 {
3200 sect_offset bottom = { cu_header->offset.sect_off };
3201 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3202 + cu_header->initial_length_size) };
3203
3204 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3205 }
3206
3207 /* Read in the comp unit header information from the debug_info at info_ptr.
3208 NOTE: This leaves members offset, first_die_offset to be filled in
3209 by the caller. */
3210
3211 static gdb_byte *
3212 read_comp_unit_head (struct comp_unit_head *cu_header,
3213 gdb_byte *info_ptr, bfd *abfd)
3214 {
3215 int signed_addr;
3216 unsigned int bytes_read;
3217
3218 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3219 cu_header->initial_length_size = bytes_read;
3220 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3221 info_ptr += bytes_read;
3222 cu_header->version = read_2_bytes (abfd, info_ptr);
3223 info_ptr += 2;
3224 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3225 &bytes_read);
3226 info_ptr += bytes_read;
3227 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3228 info_ptr += 1;
3229 signed_addr = bfd_get_sign_extend_vma (abfd);
3230 if (signed_addr < 0)
3231 internal_error (__FILE__, __LINE__,
3232 _("read_comp_unit_head: dwarf from non elf file"));
3233 cu_header->signed_addr_p = signed_addr;
3234
3235 return info_ptr;
3236 }
3237
3238 /* Subroutine of read_and_check_comp_unit_head and
3239 read_and_check_type_unit_head to simplify them.
3240 Perform various error checking on the header. */
3241
3242 static void
3243 error_check_comp_unit_head (struct comp_unit_head *header,
3244 struct dwarf2_section_info *section)
3245 {
3246 bfd *abfd = section->asection->owner;
3247 const char *filename = bfd_get_filename (abfd);
3248
3249 if (header->version != 2 && header->version != 3 && header->version != 4)
3250 error (_("Dwarf Error: wrong version in compilation unit header "
3251 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3252 filename);
3253
3254 if (header->abbrev_offset.sect_off
3255 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3256 &dwarf2_per_objfile->abbrev))
3257 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3258 "(offset 0x%lx + 6) [in module %s]"),
3259 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3260 filename);
3261
3262 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3263 avoid potential 32-bit overflow. */
3264 if (((unsigned long) header->offset.sect_off
3265 + header->length + header->initial_length_size)
3266 > section->size)
3267 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3268 "(offset 0x%lx + 0) [in module %s]"),
3269 (long) header->length, (long) header->offset.sect_off,
3270 filename);
3271 }
3272
3273 /* Read in a CU/TU header and perform some basic error checking.
3274 The contents of the header are stored in HEADER.
3275 The result is a pointer to the start of the first DIE. */
3276
3277 static gdb_byte *
3278 read_and_check_comp_unit_head (struct comp_unit_head *header,
3279 struct dwarf2_section_info *section,
3280 gdb_byte *info_ptr,
3281 int is_debug_types_section)
3282 {
3283 gdb_byte *beg_of_comp_unit = info_ptr;
3284 bfd *abfd = section->asection->owner;
3285
3286 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3287
3288 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3289
3290 /* If we're reading a type unit, skip over the signature and
3291 type_offset fields. */
3292 if (is_debug_types_section)
3293 info_ptr += 8 /*signature*/ + header->offset_size;
3294
3295 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3296
3297 error_check_comp_unit_head (header, section);
3298
3299 return info_ptr;
3300 }
3301
3302 /* Read in the types comp unit header information from .debug_types entry at
3303 types_ptr. The result is a pointer to one past the end of the header. */
3304
3305 static gdb_byte *
3306 read_and_check_type_unit_head (struct comp_unit_head *header,
3307 struct dwarf2_section_info *section,
3308 gdb_byte *info_ptr,
3309 ULONGEST *signature,
3310 cu_offset *type_offset_in_tu)
3311 {
3312 gdb_byte *beg_of_comp_unit = info_ptr;
3313 bfd *abfd = section->asection->owner;
3314
3315 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3316
3317 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3318
3319 /* If we're reading a type unit, skip over the signature and
3320 type_offset fields. */
3321 if (signature != NULL)
3322 *signature = read_8_bytes (abfd, info_ptr);
3323 info_ptr += 8;
3324 if (type_offset_in_tu != NULL)
3325 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3326 header->offset_size);
3327 info_ptr += header->offset_size;
3328
3329 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3330
3331 error_check_comp_unit_head (header, section);
3332
3333 return info_ptr;
3334 }
3335
3336 /* Allocate a new partial symtab for file named NAME and mark this new
3337 partial symtab as being an include of PST. */
3338
3339 static void
3340 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3341 struct objfile *objfile)
3342 {
3343 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3344
3345 subpst->section_offsets = pst->section_offsets;
3346 subpst->textlow = 0;
3347 subpst->texthigh = 0;
3348
3349 subpst->dependencies = (struct partial_symtab **)
3350 obstack_alloc (&objfile->objfile_obstack,
3351 sizeof (struct partial_symtab *));
3352 subpst->dependencies[0] = pst;
3353 subpst->number_of_dependencies = 1;
3354
3355 subpst->globals_offset = 0;
3356 subpst->n_global_syms = 0;
3357 subpst->statics_offset = 0;
3358 subpst->n_static_syms = 0;
3359 subpst->symtab = NULL;
3360 subpst->read_symtab = pst->read_symtab;
3361 subpst->readin = 0;
3362
3363 /* No private part is necessary for include psymtabs. This property
3364 can be used to differentiate between such include psymtabs and
3365 the regular ones. */
3366 subpst->read_symtab_private = NULL;
3367 }
3368
3369 /* Read the Line Number Program data and extract the list of files
3370 included by the source file represented by PST. Build an include
3371 partial symtab for each of these included files. */
3372
3373 static void
3374 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3375 struct die_info *die,
3376 struct partial_symtab *pst)
3377 {
3378 struct line_header *lh = NULL;
3379 struct attribute *attr;
3380
3381 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3382 if (attr)
3383 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3384 if (lh == NULL)
3385 return; /* No linetable, so no includes. */
3386
3387 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3388 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3389
3390 free_line_header (lh);
3391 }
3392
3393 static hashval_t
3394 hash_signatured_type (const void *item)
3395 {
3396 const struct signatured_type *sig_type = item;
3397
3398 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3399 return sig_type->signature;
3400 }
3401
3402 static int
3403 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3404 {
3405 const struct signatured_type *lhs = item_lhs;
3406 const struct signatured_type *rhs = item_rhs;
3407
3408 return lhs->signature == rhs->signature;
3409 }
3410
3411 /* Allocate a hash table for signatured types. */
3412
3413 static htab_t
3414 allocate_signatured_type_table (struct objfile *objfile)
3415 {
3416 return htab_create_alloc_ex (41,
3417 hash_signatured_type,
3418 eq_signatured_type,
3419 NULL,
3420 &objfile->objfile_obstack,
3421 hashtab_obstack_allocate,
3422 dummy_obstack_deallocate);
3423 }
3424
3425 /* A helper function to add a signatured type CU to a table. */
3426
3427 static int
3428 add_signatured_type_cu_to_table (void **slot, void *datum)
3429 {
3430 struct signatured_type *sigt = *slot;
3431 struct dwarf2_per_cu_data ***datap = datum;
3432
3433 **datap = &sigt->per_cu;
3434 ++*datap;
3435
3436 return 1;
3437 }
3438
3439 /* Create the hash table of all entries in the .debug_types section.
3440 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3441 The result is a pointer to the hash table or NULL if there are
3442 no types. */
3443
3444 static htab_t
3445 create_debug_types_hash_table (struct dwo_file *dwo_file,
3446 VEC (dwarf2_section_info_def) *types)
3447 {
3448 struct objfile *objfile = dwarf2_per_objfile->objfile;
3449 htab_t types_htab = NULL;
3450 int ix;
3451 struct dwarf2_section_info *section;
3452
3453 if (VEC_empty (dwarf2_section_info_def, types))
3454 return NULL;
3455
3456 for (ix = 0;
3457 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3458 ++ix)
3459 {
3460 bfd *abfd;
3461 gdb_byte *info_ptr, *end_ptr;
3462
3463 dwarf2_read_section (objfile, section);
3464 info_ptr = section->buffer;
3465
3466 if (info_ptr == NULL)
3467 continue;
3468
3469 /* We can't set abfd until now because the section may be empty or
3470 not present, in which case section->asection will be NULL. */
3471 abfd = section->asection->owner;
3472
3473 if (types_htab == NULL)
3474 {
3475 if (dwo_file)
3476 types_htab = allocate_dwo_unit_table (objfile);
3477 else
3478 types_htab = allocate_signatured_type_table (objfile);
3479 }
3480
3481 if (dwarf2_die_debug)
3482 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3483 bfd_get_filename (abfd));
3484
3485 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3486 because we don't need to read any dies: the signature is in the
3487 header. */
3488
3489 end_ptr = info_ptr + section->size;
3490 while (info_ptr < end_ptr)
3491 {
3492 sect_offset offset;
3493 cu_offset type_offset_in_tu;
3494 ULONGEST signature;
3495 struct signatured_type *sig_type;
3496 struct dwo_unit *dwo_tu;
3497 void **slot;
3498 gdb_byte *ptr = info_ptr;
3499 struct comp_unit_head header;
3500 unsigned int length;
3501
3502 offset.sect_off = ptr - section->buffer;
3503
3504 /* We need to read the type's signature in order to build the hash
3505 table, but we don't need anything else just yet. */
3506
3507 ptr = read_and_check_type_unit_head (&header, section, ptr,
3508 &signature, &type_offset_in_tu);
3509
3510 length = header.initial_length_size + header.length;
3511
3512 /* Skip dummy type units. */
3513 if (ptr >= info_ptr + length
3514 || peek_abbrev_code (abfd, ptr) == 0)
3515 {
3516 info_ptr += header.initial_length_size + header.length;
3517 continue;
3518 }
3519
3520 if (dwo_file)
3521 {
3522 sig_type = NULL;
3523 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3524 struct dwo_unit);
3525 dwo_tu->dwo_file = dwo_file;
3526 dwo_tu->signature = signature;
3527 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3528 dwo_tu->info_or_types_section = section;
3529 dwo_tu->offset = offset;
3530 dwo_tu->length = length;
3531 }
3532 else
3533 {
3534 /* N.B.: type_offset is not usable if this type uses a DWO file.
3535 The real type_offset is in the DWO file. */
3536 dwo_tu = NULL;
3537 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3538 struct signatured_type);
3539 sig_type->signature = signature;
3540 sig_type->type_offset_in_tu = type_offset_in_tu;
3541 sig_type->per_cu.objfile = objfile;
3542 sig_type->per_cu.is_debug_types = 1;
3543 sig_type->per_cu.info_or_types_section = section;
3544 sig_type->per_cu.offset = offset;
3545 sig_type->per_cu.length = length;
3546 }
3547
3548 slot = htab_find_slot (types_htab,
3549 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3550 INSERT);
3551 gdb_assert (slot != NULL);
3552 if (*slot != NULL)
3553 {
3554 sect_offset dup_offset;
3555
3556 if (dwo_file)
3557 {
3558 const struct dwo_unit *dup_tu = *slot;
3559
3560 dup_offset = dup_tu->offset;
3561 }
3562 else
3563 {
3564 const struct signatured_type *dup_tu = *slot;
3565
3566 dup_offset = dup_tu->per_cu.offset;
3567 }
3568
3569 complaint (&symfile_complaints,
3570 _("debug type entry at offset 0x%x is duplicate to the "
3571 "entry at offset 0x%x, signature 0x%s"),
3572 offset.sect_off, dup_offset.sect_off,
3573 phex (signature, sizeof (signature)));
3574 }
3575 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3576
3577 if (dwarf2_die_debug)
3578 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3579 offset.sect_off,
3580 phex (signature, sizeof (signature)));
3581
3582 info_ptr += length;
3583 }
3584 }
3585
3586 return types_htab;
3587 }
3588
3589 /* Create the hash table of all entries in the .debug_types section,
3590 and initialize all_type_units.
3591 The result is zero if there is an error (e.g. missing .debug_types section),
3592 otherwise non-zero. */
3593
3594 static int
3595 create_all_type_units (struct objfile *objfile)
3596 {
3597 htab_t types_htab;
3598 struct dwarf2_per_cu_data **iter;
3599
3600 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3601 if (types_htab == NULL)
3602 {
3603 dwarf2_per_objfile->signatured_types = NULL;
3604 return 0;
3605 }
3606
3607 dwarf2_per_objfile->signatured_types = types_htab;
3608
3609 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3610 dwarf2_per_objfile->all_type_units
3611 = obstack_alloc (&objfile->objfile_obstack,
3612 dwarf2_per_objfile->n_type_units
3613 * sizeof (struct dwarf2_per_cu_data *));
3614 iter = &dwarf2_per_objfile->all_type_units[0];
3615 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3616 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3617 == dwarf2_per_objfile->n_type_units);
3618
3619 return 1;
3620 }
3621
3622 /* Lookup a signature based type for DW_FORM_ref_sig8.
3623 Returns NULL if signature SIG is not present in the table. */
3624
3625 static struct signatured_type *
3626 lookup_signatured_type (ULONGEST sig)
3627 {
3628 struct signatured_type find_entry, *entry;
3629
3630 if (dwarf2_per_objfile->signatured_types == NULL)
3631 {
3632 complaint (&symfile_complaints,
3633 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3634 return NULL;
3635 }
3636
3637 find_entry.signature = sig;
3638 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3639 return entry;
3640 }
3641
3642 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3643
3644 static void
3645 init_cu_die_reader (struct die_reader_specs *reader,
3646 struct dwarf2_cu *cu,
3647 struct dwarf2_section_info *section,
3648 struct dwo_file *dwo_file)
3649 {
3650 gdb_assert (section->readin && section->buffer != NULL);
3651 reader->abfd = section->asection->owner;
3652 reader->cu = cu;
3653 reader->dwo_file = dwo_file;
3654 reader->die_section = section;
3655 reader->buffer = section->buffer;
3656 }
3657
3658 /* Find the base address of the compilation unit for range lists and
3659 location lists. It will normally be specified by DW_AT_low_pc.
3660 In DWARF-3 draft 4, the base address could be overridden by
3661 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3662 compilation units with discontinuous ranges. */
3663
3664 static void
3665 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3666 {
3667 struct attribute *attr;
3668
3669 cu->base_known = 0;
3670 cu->base_address = 0;
3671
3672 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3673 if (attr)
3674 {
3675 cu->base_address = DW_ADDR (attr);
3676 cu->base_known = 1;
3677 }
3678 else
3679 {
3680 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3681 if (attr)
3682 {
3683 cu->base_address = DW_ADDR (attr);
3684 cu->base_known = 1;
3685 }
3686 }
3687 }
3688
3689 /* Initialize a CU (or TU) and read its DIEs.
3690 If the CU defers to a DWO file, read the DWO file as well.
3691
3692 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3693 Otherwise, a new CU is allocated with xmalloc.
3694
3695 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3696 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3697
3698 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3699 linker) then DIE_READER_FUNC will not get called. */
3700
3701 static void
3702 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3703 int use_existing_cu, int keep,
3704 die_reader_func_ftype *die_reader_func,
3705 void *data)
3706 {
3707 struct objfile *objfile = dwarf2_per_objfile->objfile;
3708 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3709 bfd *abfd = section->asection->owner;
3710 struct dwarf2_cu *cu;
3711 gdb_byte *begin_info_ptr, *info_ptr;
3712 struct die_reader_specs reader;
3713 struct die_info *comp_unit_die;
3714 int has_children;
3715 struct attribute *attr;
3716 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3717 struct signatured_type *sig_type = NULL;
3718
3719 if (use_existing_cu)
3720 gdb_assert (keep);
3721
3722 cleanups = make_cleanup (null_cleanup, NULL);
3723
3724 /* This is cheap if the section is already read in. */
3725 dwarf2_read_section (objfile, section);
3726
3727 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3728
3729 if (use_existing_cu && this_cu->cu != NULL)
3730 {
3731 cu = this_cu->cu;
3732 info_ptr += cu->header.first_die_offset.cu_off;
3733 }
3734 else
3735 {
3736 /* If !use_existing_cu, this_cu->cu must be NULL. */
3737 gdb_assert (this_cu->cu == NULL);
3738
3739 cu = xmalloc (sizeof (*cu));
3740 init_one_comp_unit (cu, this_cu);
3741
3742 /* If an error occurs while loading, release our storage. */
3743 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3744
3745 if (this_cu->is_debug_types)
3746 {
3747 ULONGEST signature;
3748
3749 info_ptr = read_and_check_type_unit_head (&cu->header,
3750 section, info_ptr,
3751 &signature, NULL);
3752
3753 /* There's no way to get from PER_CU to its containing
3754 struct signatured_type.
3755 But we have the signature so we can use that. */
3756 sig_type = lookup_signatured_type (signature);
3757 /* We've already scanned all the signatured types,
3758 this must succeed. */
3759 gdb_assert (sig_type != NULL);
3760 gdb_assert (&sig_type->per_cu == this_cu);
3761 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3762
3763 /* LENGTH has not been set yet for type units. */
3764 this_cu->length = cu->header.length + cu->header.initial_length_size;
3765
3766 /* Establish the type offset that can be used to lookup the type. */
3767 sig_type->type_offset_in_section.sect_off =
3768 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3769 }
3770 else
3771 {
3772 info_ptr = read_and_check_comp_unit_head (&cu->header,
3773 section, info_ptr, 0);
3774
3775 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3776 gdb_assert (this_cu->length
3777 == cu->header.length + cu->header.initial_length_size);
3778 }
3779 }
3780
3781 /* Skip dummy compilation units. */
3782 if (info_ptr >= begin_info_ptr + this_cu->length
3783 || peek_abbrev_code (abfd, info_ptr) == 0)
3784 {
3785 do_cleanups (cleanups);
3786 return;
3787 }
3788
3789 /* Read the abbrevs for this compilation unit into a table. */
3790 if (cu->dwarf2_abbrevs == NULL)
3791 {
3792 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3793 make_cleanup (dwarf2_free_abbrev_table, cu);
3794 }
3795
3796 /* Read the top level CU/TU die. */
3797 init_cu_die_reader (&reader, cu, section, NULL);
3798 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3799
3800 /* If we have a DWO stub, process it and then read in the DWO file.
3801 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3802 a DWO CU, that this test will fail. */
3803 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3804 if (attr)
3805 {
3806 char *dwo_name = DW_STRING (attr);
3807 const char *comp_dir;
3808 struct dwo_unit *dwo_unit;
3809 ULONGEST signature; /* Or dwo_id. */
3810 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3811 int i,num_extra_attrs;
3812
3813 if (has_children)
3814 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3815 " has children (offset 0x%x) [in module %s]"),
3816 this_cu->offset.sect_off, bfd_get_filename (abfd));
3817
3818 /* These attributes aren't processed until later:
3819 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3820 However, the attribute is found in the stub which we won't have later.
3821 In order to not impose this complication on the rest of the code,
3822 we read them here and copy them to the DWO CU/TU die. */
3823 stmt_list = low_pc = high_pc = ranges = NULL;
3824
3825 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3826 DWO file. */
3827 if (! this_cu->is_debug_types)
3828 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3829 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3830 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3831 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3832
3833 /* There should be a DW_AT_addr_base attribute here (if needed).
3834 We need the value before we can process DW_FORM_GNU_addr_index. */
3835 cu->addr_base = 0;
3836 cu->have_addr_base = 0;
3837 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3838 if (attr)
3839 {
3840 cu->addr_base = DW_UNSND (attr);
3841 cu->have_addr_base = 1;
3842 }
3843
3844 if (this_cu->is_debug_types)
3845 {
3846 gdb_assert (sig_type != NULL);
3847 signature = sig_type->signature;
3848 }
3849 else
3850 {
3851 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3852 if (! attr)
3853 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3854 dwo_name);
3855 signature = DW_UNSND (attr);
3856 }
3857
3858 /* We may need the comp_dir in order to find the DWO file. */
3859 comp_dir = NULL;
3860 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3861 if (attr)
3862 comp_dir = DW_STRING (attr);
3863
3864 if (this_cu->is_debug_types)
3865 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3866 else
3867 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3868 signature);
3869
3870 if (dwo_unit == NULL)
3871 {
3872 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3873 " with ID %s [in module %s]"),
3874 this_cu->offset.sect_off,
3875 phex (signature, sizeof (signature)),
3876 objfile->name);
3877 }
3878
3879 /* Set up for reading the DWO CU/TU. */
3880 cu->dwo_unit = dwo_unit;
3881 section = dwo_unit->info_or_types_section;
3882 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3883 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3884
3885 if (this_cu->is_debug_types)
3886 {
3887 ULONGEST signature;
3888
3889 info_ptr = read_and_check_type_unit_head (&cu->header,
3890 section, info_ptr,
3891 &signature, NULL);
3892 gdb_assert (sig_type->signature == signature);
3893 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3894 gdb_assert (dwo_unit->length
3895 == cu->header.length + cu->header.initial_length_size);
3896
3897 /* Establish the type offset that can be used to lookup the type.
3898 For DWO files, we don't know it until now. */
3899 sig_type->type_offset_in_section.sect_off =
3900 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3901 }
3902 else
3903 {
3904 info_ptr = read_and_check_comp_unit_head (&cu->header,
3905 section, info_ptr, 0);
3906 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3907 gdb_assert (dwo_unit->length
3908 == cu->header.length + cu->header.initial_length_size);
3909 }
3910
3911 /* Discard the original CU's abbrev table, and read the DWO's. */
3912 dwarf2_free_abbrev_table (cu);
3913 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3914
3915 /* Read in the die, but leave space to copy over the attributes
3916 from the stub. This has the benefit of simplifying the rest of
3917 the code - all the real work is done here. */
3918 num_extra_attrs = ((stmt_list != NULL)
3919 + (low_pc != NULL)
3920 + (high_pc != NULL)
3921 + (ranges != NULL));
3922 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3923 &has_children, num_extra_attrs);
3924
3925 /* Copy over the attributes from the stub to the DWO die. */
3926 i = comp_unit_die->num_attrs;
3927 if (stmt_list != NULL)
3928 comp_unit_die->attrs[i++] = *stmt_list;
3929 if (low_pc != NULL)
3930 comp_unit_die->attrs[i++] = *low_pc;
3931 if (high_pc != NULL)
3932 comp_unit_die->attrs[i++] = *high_pc;
3933 if (ranges != NULL)
3934 comp_unit_die->attrs[i++] = *ranges;
3935 comp_unit_die->num_attrs += num_extra_attrs;
3936
3937 /* Skip dummy compilation units. */
3938 if (info_ptr >= begin_info_ptr + dwo_unit->length
3939 || peek_abbrev_code (abfd, info_ptr) == 0)
3940 {
3941 do_cleanups (cleanups);
3942 return;
3943 }
3944 }
3945
3946 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3947
3948 if (free_cu_cleanup != NULL)
3949 {
3950 if (keep)
3951 {
3952 /* We've successfully allocated this compilation unit. Let our
3953 caller clean it up when finished with it. */
3954 discard_cleanups (free_cu_cleanup);
3955
3956 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3957 So we have to manually free the abbrev table. */
3958 dwarf2_free_abbrev_table (cu);
3959
3960 /* Link this CU into read_in_chain. */
3961 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3962 dwarf2_per_objfile->read_in_chain = this_cu;
3963 }
3964 else
3965 do_cleanups (free_cu_cleanup);
3966 }
3967
3968 do_cleanups (cleanups);
3969 }
3970
3971 /* Read CU/TU THIS_CU in section SECTION,
3972 but do not follow DW_AT_GNU_dwo_name if present.
3973 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3974 have already done the lookup to find the DWO file).
3975
3976 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3977 THIS_CU->is_debug_types, but nothing else.
3978
3979 We fill in THIS_CU->length.
3980
3981 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3982 linker) then DIE_READER_FUNC will not get called.
3983
3984 THIS_CU->cu is always freed when done.
3985 This is done in order to not leave THIS_CU->cu in a state where we have
3986 to care whether it refers to the "main" CU or the DWO CU. */
3987
3988 static void
3989 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3990 struct dwarf2_section_info *abbrev_section,
3991 struct dwo_file *dwo_file,
3992 die_reader_func_ftype *die_reader_func,
3993 void *data)
3994 {
3995 struct objfile *objfile = dwarf2_per_objfile->objfile;
3996 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3997 bfd *abfd = section->asection->owner;
3998 struct dwarf2_cu cu;
3999 gdb_byte *begin_info_ptr, *info_ptr;
4000 struct die_reader_specs reader;
4001 struct cleanup *cleanups;
4002 struct die_info *comp_unit_die;
4003 int has_children;
4004
4005 gdb_assert (this_cu->cu == NULL);
4006
4007 /* This is cheap if the section is already read in. */
4008 dwarf2_read_section (objfile, section);
4009
4010 init_one_comp_unit (&cu, this_cu);
4011
4012 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4013
4014 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4015 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
4016 this_cu->is_debug_types);
4017
4018 this_cu->length = cu.header.length + cu.header.initial_length_size;
4019
4020 /* Skip dummy compilation units. */
4021 if (info_ptr >= begin_info_ptr + this_cu->length
4022 || peek_abbrev_code (abfd, info_ptr) == 0)
4023 {
4024 do_cleanups (cleanups);
4025 return;
4026 }
4027
4028 dwarf2_read_abbrevs (&cu, abbrev_section);
4029 make_cleanup (dwarf2_free_abbrev_table, &cu);
4030
4031 init_cu_die_reader (&reader, &cu, section, dwo_file);
4032 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4033
4034 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4035
4036 do_cleanups (cleanups);
4037 }
4038
4039 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4040 does not lookup the specified DWO file.
4041 This cannot be used to read DWO files.
4042
4043 THIS_CU->cu is always freed when done.
4044 This is done in order to not leave THIS_CU->cu in a state where we have
4045 to care whether it refers to the "main" CU or the DWO CU.
4046 We can revisit this if the data shows there's a performance issue. */
4047
4048 static void
4049 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4050 die_reader_func_ftype *die_reader_func,
4051 void *data)
4052 {
4053 init_cutu_and_read_dies_no_follow (this_cu,
4054 &dwarf2_per_objfile->abbrev,
4055 NULL,
4056 die_reader_func, data);
4057 }
4058
4059 /* die_reader_func for process_psymtab_comp_unit. */
4060
4061 static void
4062 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4063 gdb_byte *info_ptr,
4064 struct die_info *comp_unit_die,
4065 int has_children,
4066 void *data)
4067 {
4068 struct dwarf2_cu *cu = reader->cu;
4069 struct objfile *objfile = cu->objfile;
4070 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4071 struct attribute *attr;
4072 CORE_ADDR baseaddr;
4073 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4074 struct partial_symtab *pst;
4075 int has_pc_info;
4076 const char *filename;
4077 int *want_partial_unit_ptr = data;
4078
4079 if (comp_unit_die->tag == DW_TAG_partial_unit
4080 && (want_partial_unit_ptr == NULL
4081 || !*want_partial_unit_ptr))
4082 return;
4083
4084 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4085
4086 cu->list_in_scope = &file_symbols;
4087
4088 /* Allocate a new partial symbol table structure. */
4089 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4090 if (attr == NULL || !DW_STRING (attr))
4091 filename = "";
4092 else
4093 filename = DW_STRING (attr);
4094 pst = start_psymtab_common (objfile, objfile->section_offsets,
4095 filename,
4096 /* TEXTLOW and TEXTHIGH are set below. */
4097 0,
4098 objfile->global_psymbols.next,
4099 objfile->static_psymbols.next);
4100 pst->psymtabs_addrmap_supported = 1;
4101
4102 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4103 if (attr != NULL)
4104 pst->dirname = DW_STRING (attr);
4105
4106 pst->read_symtab_private = per_cu;
4107
4108 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4109
4110 /* Store the function that reads in the rest of the symbol table. */
4111 pst->read_symtab = dwarf2_psymtab_to_symtab;
4112
4113 per_cu->v.psymtab = pst;
4114
4115 dwarf2_find_base_address (comp_unit_die, cu);
4116
4117 /* Possibly set the default values of LOWPC and HIGHPC from
4118 `DW_AT_ranges'. */
4119 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4120 &best_highpc, cu, pst);
4121 if (has_pc_info == 1 && best_lowpc < best_highpc)
4122 /* Store the contiguous range if it is not empty; it can be empty for
4123 CUs with no code. */
4124 addrmap_set_empty (objfile->psymtabs_addrmap,
4125 best_lowpc + baseaddr,
4126 best_highpc + baseaddr - 1, pst);
4127
4128 /* Check if comp unit has_children.
4129 If so, read the rest of the partial symbols from this comp unit.
4130 If not, there's no more debug_info for this comp unit. */
4131 if (has_children)
4132 {
4133 struct partial_die_info *first_die;
4134 CORE_ADDR lowpc, highpc;
4135
4136 lowpc = ((CORE_ADDR) -1);
4137 highpc = ((CORE_ADDR) 0);
4138
4139 first_die = load_partial_dies (reader, info_ptr, 1);
4140
4141 scan_partial_symbols (first_die, &lowpc, &highpc,
4142 ! has_pc_info, cu);
4143
4144 /* If we didn't find a lowpc, set it to highpc to avoid
4145 complaints from `maint check'. */
4146 if (lowpc == ((CORE_ADDR) -1))
4147 lowpc = highpc;
4148
4149 /* If the compilation unit didn't have an explicit address range,
4150 then use the information extracted from its child dies. */
4151 if (! has_pc_info)
4152 {
4153 best_lowpc = lowpc;
4154 best_highpc = highpc;
4155 }
4156 }
4157 pst->textlow = best_lowpc + baseaddr;
4158 pst->texthigh = best_highpc + baseaddr;
4159
4160 pst->n_global_syms = objfile->global_psymbols.next -
4161 (objfile->global_psymbols.list + pst->globals_offset);
4162 pst->n_static_syms = objfile->static_psymbols.next -
4163 (objfile->static_psymbols.list + pst->statics_offset);
4164 sort_pst_symbols (pst);
4165
4166 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4167 {
4168 int i;
4169 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4170 struct dwarf2_per_cu_data *iter;
4171
4172 /* Fill in 'dependencies' here; we fill in 'users' in a
4173 post-pass. */
4174 pst->number_of_dependencies = len;
4175 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4176 len * sizeof (struct symtab *));
4177 for (i = 0;
4178 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4179 i, iter);
4180 ++i)
4181 pst->dependencies[i] = iter->v.psymtab;
4182
4183 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4184 }
4185
4186 if (per_cu->is_debug_types)
4187 {
4188 /* It's not clear we want to do anything with stmt lists here.
4189 Waiting to see what gcc ultimately does. */
4190 }
4191 else
4192 {
4193 /* Get the list of files included in the current compilation unit,
4194 and build a psymtab for each of them. */
4195 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4196 }
4197 }
4198
4199 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4200 Process compilation unit THIS_CU for a psymtab. */
4201
4202 static void
4203 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4204 int want_partial_unit)
4205 {
4206 /* If this compilation unit was already read in, free the
4207 cached copy in order to read it in again. This is
4208 necessary because we skipped some symbols when we first
4209 read in the compilation unit (see load_partial_dies).
4210 This problem could be avoided, but the benefit is unclear. */
4211 if (this_cu->cu != NULL)
4212 free_one_cached_comp_unit (this_cu);
4213
4214 gdb_assert (! this_cu->is_debug_types);
4215 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4216 &want_partial_unit);
4217
4218 /* Age out any secondary CUs. */
4219 age_cached_comp_units ();
4220 }
4221
4222 /* Traversal function for htab_traverse_noresize.
4223 Process one .debug_types comp-unit. */
4224
4225 static int
4226 process_psymtab_type_unit (void **slot, void *info)
4227 {
4228 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4229 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4230
4231 gdb_assert (per_cu->is_debug_types);
4232 gdb_assert (info == NULL);
4233
4234 /* If this compilation unit was already read in, free the
4235 cached copy in order to read it in again. This is
4236 necessary because we skipped some symbols when we first
4237 read in the compilation unit (see load_partial_dies).
4238 This problem could be avoided, but the benefit is unclear. */
4239 if (per_cu->cu != NULL)
4240 free_one_cached_comp_unit (per_cu);
4241
4242 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4243 NULL);
4244
4245 /* Age out any secondary CUs. */
4246 age_cached_comp_units ();
4247
4248 return 1;
4249 }
4250
4251 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4252 Build partial symbol tables for the .debug_types comp-units. */
4253
4254 static void
4255 build_type_psymtabs (struct objfile *objfile)
4256 {
4257 if (! create_all_type_units (objfile))
4258 return;
4259
4260 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4261 process_psymtab_type_unit, NULL);
4262 }
4263
4264 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4265
4266 static void
4267 psymtabs_addrmap_cleanup (void *o)
4268 {
4269 struct objfile *objfile = o;
4270
4271 objfile->psymtabs_addrmap = NULL;
4272 }
4273
4274 /* Compute the 'user' field for each psymtab in OBJFILE. */
4275
4276 static void
4277 set_partial_user (struct objfile *objfile)
4278 {
4279 int i;
4280
4281 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4282 {
4283 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4284 struct partial_symtab *pst = per_cu->v.psymtab;
4285 int j;
4286
4287 for (j = 0; j < pst->number_of_dependencies; ++j)
4288 {
4289 /* Set the 'user' field only if it is not already set. */
4290 if (pst->dependencies[j]->user == NULL)
4291 pst->dependencies[j]->user = pst;
4292 }
4293 }
4294 }
4295
4296 /* Build the partial symbol table by doing a quick pass through the
4297 .debug_info and .debug_abbrev sections. */
4298
4299 static void
4300 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4301 {
4302 struct cleanup *back_to, *addrmap_cleanup;
4303 struct obstack temp_obstack;
4304 int i;
4305
4306 dwarf2_per_objfile->reading_partial_symbols = 1;
4307
4308 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4309
4310 /* Any cached compilation units will be linked by the per-objfile
4311 read_in_chain. Make sure to free them when we're done. */
4312 back_to = make_cleanup (free_cached_comp_units, NULL);
4313
4314 build_type_psymtabs (objfile);
4315
4316 create_all_comp_units (objfile);
4317
4318 /* Create a temporary address map on a temporary obstack. We later
4319 copy this to the final obstack. */
4320 obstack_init (&temp_obstack);
4321 make_cleanup_obstack_free (&temp_obstack);
4322 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4323 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4324
4325 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4326 {
4327 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4328
4329 process_psymtab_comp_unit (per_cu, 0);
4330 }
4331
4332 set_partial_user (objfile);
4333
4334 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4335 &objfile->objfile_obstack);
4336 discard_cleanups (addrmap_cleanup);
4337
4338 do_cleanups (back_to);
4339 }
4340
4341 /* die_reader_func for load_partial_comp_unit. */
4342
4343 static void
4344 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4345 gdb_byte *info_ptr,
4346 struct die_info *comp_unit_die,
4347 int has_children,
4348 void *data)
4349 {
4350 struct dwarf2_cu *cu = reader->cu;
4351
4352 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4353
4354 /* Check if comp unit has_children.
4355 If so, read the rest of the partial symbols from this comp unit.
4356 If not, there's no more debug_info for this comp unit. */
4357 if (has_children)
4358 load_partial_dies (reader, info_ptr, 0);
4359 }
4360
4361 /* Load the partial DIEs for a secondary CU into memory.
4362 This is also used when rereading a primary CU with load_all_dies. */
4363
4364 static void
4365 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4366 {
4367 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4368 }
4369
4370 /* Create a list of all compilation units in OBJFILE.
4371 This is only done for -readnow and building partial symtabs. */
4372
4373 static void
4374 create_all_comp_units (struct objfile *objfile)
4375 {
4376 int n_allocated;
4377 int n_comp_units;
4378 struct dwarf2_per_cu_data **all_comp_units;
4379 gdb_byte *info_ptr;
4380
4381 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4382 info_ptr = dwarf2_per_objfile->info.buffer;
4383
4384 n_comp_units = 0;
4385 n_allocated = 10;
4386 all_comp_units = xmalloc (n_allocated
4387 * sizeof (struct dwarf2_per_cu_data *));
4388
4389 while (info_ptr < dwarf2_per_objfile->info.buffer
4390 + dwarf2_per_objfile->info.size)
4391 {
4392 unsigned int length, initial_length_size;
4393 struct dwarf2_per_cu_data *this_cu;
4394 sect_offset offset;
4395
4396 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4397
4398 /* Read just enough information to find out where the next
4399 compilation unit is. */
4400 length = read_initial_length (objfile->obfd, info_ptr,
4401 &initial_length_size);
4402
4403 /* Save the compilation unit for later lookup. */
4404 this_cu = obstack_alloc (&objfile->objfile_obstack,
4405 sizeof (struct dwarf2_per_cu_data));
4406 memset (this_cu, 0, sizeof (*this_cu));
4407 this_cu->offset = offset;
4408 this_cu->length = length + initial_length_size;
4409 this_cu->objfile = objfile;
4410 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4411
4412 if (n_comp_units == n_allocated)
4413 {
4414 n_allocated *= 2;
4415 all_comp_units = xrealloc (all_comp_units,
4416 n_allocated
4417 * sizeof (struct dwarf2_per_cu_data *));
4418 }
4419 all_comp_units[n_comp_units++] = this_cu;
4420
4421 info_ptr = info_ptr + this_cu->length;
4422 }
4423
4424 dwarf2_per_objfile->all_comp_units
4425 = obstack_alloc (&objfile->objfile_obstack,
4426 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4427 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4428 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4429 xfree (all_comp_units);
4430 dwarf2_per_objfile->n_comp_units = n_comp_units;
4431 }
4432
4433 /* Process all loaded DIEs for compilation unit CU, starting at
4434 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4435 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4436 DW_AT_ranges). If NEED_PC is set, then this function will set
4437 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4438 and record the covered ranges in the addrmap. */
4439
4440 static void
4441 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4442 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4443 {
4444 struct partial_die_info *pdi;
4445
4446 /* Now, march along the PDI's, descending into ones which have
4447 interesting children but skipping the children of the other ones,
4448 until we reach the end of the compilation unit. */
4449
4450 pdi = first_die;
4451
4452 while (pdi != NULL)
4453 {
4454 fixup_partial_die (pdi, cu);
4455
4456 /* Anonymous namespaces or modules have no name but have interesting
4457 children, so we need to look at them. Ditto for anonymous
4458 enums. */
4459
4460 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4461 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4462 || pdi->tag == DW_TAG_imported_unit)
4463 {
4464 switch (pdi->tag)
4465 {
4466 case DW_TAG_subprogram:
4467 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4468 break;
4469 case DW_TAG_constant:
4470 case DW_TAG_variable:
4471 case DW_TAG_typedef:
4472 case DW_TAG_union_type:
4473 if (!pdi->is_declaration)
4474 {
4475 add_partial_symbol (pdi, cu);
4476 }
4477 break;
4478 case DW_TAG_class_type:
4479 case DW_TAG_interface_type:
4480 case DW_TAG_structure_type:
4481 if (!pdi->is_declaration)
4482 {
4483 add_partial_symbol (pdi, cu);
4484 }
4485 break;
4486 case DW_TAG_enumeration_type:
4487 if (!pdi->is_declaration)
4488 add_partial_enumeration (pdi, cu);
4489 break;
4490 case DW_TAG_base_type:
4491 case DW_TAG_subrange_type:
4492 /* File scope base type definitions are added to the partial
4493 symbol table. */
4494 add_partial_symbol (pdi, cu);
4495 break;
4496 case DW_TAG_namespace:
4497 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4498 break;
4499 case DW_TAG_module:
4500 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4501 break;
4502 case DW_TAG_imported_unit:
4503 {
4504 struct dwarf2_per_cu_data *per_cu;
4505
4506 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4507 cu->objfile);
4508
4509 /* Go read the partial unit, if needed. */
4510 if (per_cu->v.psymtab == NULL)
4511 process_psymtab_comp_unit (per_cu, 1);
4512
4513 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4514 per_cu);
4515 }
4516 break;
4517 default:
4518 break;
4519 }
4520 }
4521
4522 /* If the die has a sibling, skip to the sibling. */
4523
4524 pdi = pdi->die_sibling;
4525 }
4526 }
4527
4528 /* Functions used to compute the fully scoped name of a partial DIE.
4529
4530 Normally, this is simple. For C++, the parent DIE's fully scoped
4531 name is concatenated with "::" and the partial DIE's name. For
4532 Java, the same thing occurs except that "." is used instead of "::".
4533 Enumerators are an exception; they use the scope of their parent
4534 enumeration type, i.e. the name of the enumeration type is not
4535 prepended to the enumerator.
4536
4537 There are two complexities. One is DW_AT_specification; in this
4538 case "parent" means the parent of the target of the specification,
4539 instead of the direct parent of the DIE. The other is compilers
4540 which do not emit DW_TAG_namespace; in this case we try to guess
4541 the fully qualified name of structure types from their members'
4542 linkage names. This must be done using the DIE's children rather
4543 than the children of any DW_AT_specification target. We only need
4544 to do this for structures at the top level, i.e. if the target of
4545 any DW_AT_specification (if any; otherwise the DIE itself) does not
4546 have a parent. */
4547
4548 /* Compute the scope prefix associated with PDI's parent, in
4549 compilation unit CU. The result will be allocated on CU's
4550 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4551 field. NULL is returned if no prefix is necessary. */
4552 static char *
4553 partial_die_parent_scope (struct partial_die_info *pdi,
4554 struct dwarf2_cu *cu)
4555 {
4556 char *grandparent_scope;
4557 struct partial_die_info *parent, *real_pdi;
4558
4559 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4560 then this means the parent of the specification DIE. */
4561
4562 real_pdi = pdi;
4563 while (real_pdi->has_specification)
4564 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4565
4566 parent = real_pdi->die_parent;
4567 if (parent == NULL)
4568 return NULL;
4569
4570 if (parent->scope_set)
4571 return parent->scope;
4572
4573 fixup_partial_die (parent, cu);
4574
4575 grandparent_scope = partial_die_parent_scope (parent, cu);
4576
4577 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4578 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4579 Work around this problem here. */
4580 if (cu->language == language_cplus
4581 && parent->tag == DW_TAG_namespace
4582 && strcmp (parent->name, "::") == 0
4583 && grandparent_scope == NULL)
4584 {
4585 parent->scope = NULL;
4586 parent->scope_set = 1;
4587 return NULL;
4588 }
4589
4590 if (pdi->tag == DW_TAG_enumerator)
4591 /* Enumerators should not get the name of the enumeration as a prefix. */
4592 parent->scope = grandparent_scope;
4593 else if (parent->tag == DW_TAG_namespace
4594 || parent->tag == DW_TAG_module
4595 || parent->tag == DW_TAG_structure_type
4596 || parent->tag == DW_TAG_class_type
4597 || parent->tag == DW_TAG_interface_type
4598 || parent->tag == DW_TAG_union_type
4599 || parent->tag == DW_TAG_enumeration_type)
4600 {
4601 if (grandparent_scope == NULL)
4602 parent->scope = parent->name;
4603 else
4604 parent->scope = typename_concat (&cu->comp_unit_obstack,
4605 grandparent_scope,
4606 parent->name, 0, cu);
4607 }
4608 else
4609 {
4610 /* FIXME drow/2004-04-01: What should we be doing with
4611 function-local names? For partial symbols, we should probably be
4612 ignoring them. */
4613 complaint (&symfile_complaints,
4614 _("unhandled containing DIE tag %d for DIE at %d"),
4615 parent->tag, pdi->offset.sect_off);
4616 parent->scope = grandparent_scope;
4617 }
4618
4619 parent->scope_set = 1;
4620 return parent->scope;
4621 }
4622
4623 /* Return the fully scoped name associated with PDI, from compilation unit
4624 CU. The result will be allocated with malloc. */
4625
4626 static char *
4627 partial_die_full_name (struct partial_die_info *pdi,
4628 struct dwarf2_cu *cu)
4629 {
4630 char *parent_scope;
4631
4632 /* If this is a template instantiation, we can not work out the
4633 template arguments from partial DIEs. So, unfortunately, we have
4634 to go through the full DIEs. At least any work we do building
4635 types here will be reused if full symbols are loaded later. */
4636 if (pdi->has_template_arguments)
4637 {
4638 fixup_partial_die (pdi, cu);
4639
4640 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4641 {
4642 struct die_info *die;
4643 struct attribute attr;
4644 struct dwarf2_cu *ref_cu = cu;
4645
4646 /* DW_FORM_ref_addr is using section offset. */
4647 attr.name = 0;
4648 attr.form = DW_FORM_ref_addr;
4649 attr.u.unsnd = pdi->offset.sect_off;
4650 die = follow_die_ref (NULL, &attr, &ref_cu);
4651
4652 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4653 }
4654 }
4655
4656 parent_scope = partial_die_parent_scope (pdi, cu);
4657 if (parent_scope == NULL)
4658 return NULL;
4659 else
4660 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4661 }
4662
4663 static void
4664 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4665 {
4666 struct objfile *objfile = cu->objfile;
4667 CORE_ADDR addr = 0;
4668 char *actual_name = NULL;
4669 CORE_ADDR baseaddr;
4670 int built_actual_name = 0;
4671
4672 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4673
4674 actual_name = partial_die_full_name (pdi, cu);
4675 if (actual_name)
4676 built_actual_name = 1;
4677
4678 if (actual_name == NULL)
4679 actual_name = pdi->name;
4680
4681 switch (pdi->tag)
4682 {
4683 case DW_TAG_subprogram:
4684 if (pdi->is_external || cu->language == language_ada)
4685 {
4686 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4687 of the global scope. But in Ada, we want to be able to access
4688 nested procedures globally. So all Ada subprograms are stored
4689 in the global scope. */
4690 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4691 mst_text, objfile); */
4692 add_psymbol_to_list (actual_name, strlen (actual_name),
4693 built_actual_name,
4694 VAR_DOMAIN, LOC_BLOCK,
4695 &objfile->global_psymbols,
4696 0, pdi->lowpc + baseaddr,
4697 cu->language, objfile);
4698 }
4699 else
4700 {
4701 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4702 mst_file_text, objfile); */
4703 add_psymbol_to_list (actual_name, strlen (actual_name),
4704 built_actual_name,
4705 VAR_DOMAIN, LOC_BLOCK,
4706 &objfile->static_psymbols,
4707 0, pdi->lowpc + baseaddr,
4708 cu->language, objfile);
4709 }
4710 break;
4711 case DW_TAG_constant:
4712 {
4713 struct psymbol_allocation_list *list;
4714
4715 if (pdi->is_external)
4716 list = &objfile->global_psymbols;
4717 else
4718 list = &objfile->static_psymbols;
4719 add_psymbol_to_list (actual_name, strlen (actual_name),
4720 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4721 list, 0, 0, cu->language, objfile);
4722 }
4723 break;
4724 case DW_TAG_variable:
4725 if (pdi->d.locdesc)
4726 addr = decode_locdesc (pdi->d.locdesc, cu);
4727
4728 if (pdi->d.locdesc
4729 && addr == 0
4730 && !dwarf2_per_objfile->has_section_at_zero)
4731 {
4732 /* A global or static variable may also have been stripped
4733 out by the linker if unused, in which case its address
4734 will be nullified; do not add such variables into partial
4735 symbol table then. */
4736 }
4737 else if (pdi->is_external)
4738 {
4739 /* Global Variable.
4740 Don't enter into the minimal symbol tables as there is
4741 a minimal symbol table entry from the ELF symbols already.
4742 Enter into partial symbol table if it has a location
4743 descriptor or a type.
4744 If the location descriptor is missing, new_symbol will create
4745 a LOC_UNRESOLVED symbol, the address of the variable will then
4746 be determined from the minimal symbol table whenever the variable
4747 is referenced.
4748 The address for the partial symbol table entry is not
4749 used by GDB, but it comes in handy for debugging partial symbol
4750 table building. */
4751
4752 if (pdi->d.locdesc || pdi->has_type)
4753 add_psymbol_to_list (actual_name, strlen (actual_name),
4754 built_actual_name,
4755 VAR_DOMAIN, LOC_STATIC,
4756 &objfile->global_psymbols,
4757 0, addr + baseaddr,
4758 cu->language, objfile);
4759 }
4760 else
4761 {
4762 /* Static Variable. Skip symbols without location descriptors. */
4763 if (pdi->d.locdesc == NULL)
4764 {
4765 if (built_actual_name)
4766 xfree (actual_name);
4767 return;
4768 }
4769 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4770 mst_file_data, objfile); */
4771 add_psymbol_to_list (actual_name, strlen (actual_name),
4772 built_actual_name,
4773 VAR_DOMAIN, LOC_STATIC,
4774 &objfile->static_psymbols,
4775 0, addr + baseaddr,
4776 cu->language, objfile);
4777 }
4778 break;
4779 case DW_TAG_typedef:
4780 case DW_TAG_base_type:
4781 case DW_TAG_subrange_type:
4782 add_psymbol_to_list (actual_name, strlen (actual_name),
4783 built_actual_name,
4784 VAR_DOMAIN, LOC_TYPEDEF,
4785 &objfile->static_psymbols,
4786 0, (CORE_ADDR) 0, cu->language, objfile);
4787 break;
4788 case DW_TAG_namespace:
4789 add_psymbol_to_list (actual_name, strlen (actual_name),
4790 built_actual_name,
4791 VAR_DOMAIN, LOC_TYPEDEF,
4792 &objfile->global_psymbols,
4793 0, (CORE_ADDR) 0, cu->language, objfile);
4794 break;
4795 case DW_TAG_class_type:
4796 case DW_TAG_interface_type:
4797 case DW_TAG_structure_type:
4798 case DW_TAG_union_type:
4799 case DW_TAG_enumeration_type:
4800 /* Skip external references. The DWARF standard says in the section
4801 about "Structure, Union, and Class Type Entries": "An incomplete
4802 structure, union or class type is represented by a structure,
4803 union or class entry that does not have a byte size attribute
4804 and that has a DW_AT_declaration attribute." */
4805 if (!pdi->has_byte_size && pdi->is_declaration)
4806 {
4807 if (built_actual_name)
4808 xfree (actual_name);
4809 return;
4810 }
4811
4812 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4813 static vs. global. */
4814 add_psymbol_to_list (actual_name, strlen (actual_name),
4815 built_actual_name,
4816 STRUCT_DOMAIN, LOC_TYPEDEF,
4817 (cu->language == language_cplus
4818 || cu->language == language_java)
4819 ? &objfile->global_psymbols
4820 : &objfile->static_psymbols,
4821 0, (CORE_ADDR) 0, cu->language, objfile);
4822
4823 break;
4824 case DW_TAG_enumerator:
4825 add_psymbol_to_list (actual_name, strlen (actual_name),
4826 built_actual_name,
4827 VAR_DOMAIN, LOC_CONST,
4828 (cu->language == language_cplus
4829 || cu->language == language_java)
4830 ? &objfile->global_psymbols
4831 : &objfile->static_psymbols,
4832 0, (CORE_ADDR) 0, cu->language, objfile);
4833 break;
4834 default:
4835 break;
4836 }
4837
4838 if (built_actual_name)
4839 xfree (actual_name);
4840 }
4841
4842 /* Read a partial die corresponding to a namespace; also, add a symbol
4843 corresponding to that namespace to the symbol table. NAMESPACE is
4844 the name of the enclosing namespace. */
4845
4846 static void
4847 add_partial_namespace (struct partial_die_info *pdi,
4848 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4849 int need_pc, struct dwarf2_cu *cu)
4850 {
4851 /* Add a symbol for the namespace. */
4852
4853 add_partial_symbol (pdi, cu);
4854
4855 /* Now scan partial symbols in that namespace. */
4856
4857 if (pdi->has_children)
4858 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4859 }
4860
4861 /* Read a partial die corresponding to a Fortran module. */
4862
4863 static void
4864 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4865 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4866 {
4867 /* Now scan partial symbols in that module. */
4868
4869 if (pdi->has_children)
4870 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4871 }
4872
4873 /* Read a partial die corresponding to a subprogram and create a partial
4874 symbol for that subprogram. When the CU language allows it, this
4875 routine also defines a partial symbol for each nested subprogram
4876 that this subprogram contains.
4877
4878 DIE my also be a lexical block, in which case we simply search
4879 recursively for suprograms defined inside that lexical block.
4880 Again, this is only performed when the CU language allows this
4881 type of definitions. */
4882
4883 static void
4884 add_partial_subprogram (struct partial_die_info *pdi,
4885 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4886 int need_pc, struct dwarf2_cu *cu)
4887 {
4888 if (pdi->tag == DW_TAG_subprogram)
4889 {
4890 if (pdi->has_pc_info)
4891 {
4892 if (pdi->lowpc < *lowpc)
4893 *lowpc = pdi->lowpc;
4894 if (pdi->highpc > *highpc)
4895 *highpc = pdi->highpc;
4896 if (need_pc)
4897 {
4898 CORE_ADDR baseaddr;
4899 struct objfile *objfile = cu->objfile;
4900
4901 baseaddr = ANOFFSET (objfile->section_offsets,
4902 SECT_OFF_TEXT (objfile));
4903 addrmap_set_empty (objfile->psymtabs_addrmap,
4904 pdi->lowpc + baseaddr,
4905 pdi->highpc - 1 + baseaddr,
4906 cu->per_cu->v.psymtab);
4907 }
4908 }
4909
4910 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4911 {
4912 if (!pdi->is_declaration)
4913 /* Ignore subprogram DIEs that do not have a name, they are
4914 illegal. Do not emit a complaint at this point, we will
4915 do so when we convert this psymtab into a symtab. */
4916 if (pdi->name)
4917 add_partial_symbol (pdi, cu);
4918 }
4919 }
4920
4921 if (! pdi->has_children)
4922 return;
4923
4924 if (cu->language == language_ada)
4925 {
4926 pdi = pdi->die_child;
4927 while (pdi != NULL)
4928 {
4929 fixup_partial_die (pdi, cu);
4930 if (pdi->tag == DW_TAG_subprogram
4931 || pdi->tag == DW_TAG_lexical_block)
4932 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4933 pdi = pdi->die_sibling;
4934 }
4935 }
4936 }
4937
4938 /* Read a partial die corresponding to an enumeration type. */
4939
4940 static void
4941 add_partial_enumeration (struct partial_die_info *enum_pdi,
4942 struct dwarf2_cu *cu)
4943 {
4944 struct partial_die_info *pdi;
4945
4946 if (enum_pdi->name != NULL)
4947 add_partial_symbol (enum_pdi, cu);
4948
4949 pdi = enum_pdi->die_child;
4950 while (pdi)
4951 {
4952 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4953 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4954 else
4955 add_partial_symbol (pdi, cu);
4956 pdi = pdi->die_sibling;
4957 }
4958 }
4959
4960 /* Return the initial uleb128 in the die at INFO_PTR. */
4961
4962 static unsigned int
4963 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4964 {
4965 unsigned int bytes_read;
4966
4967 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4968 }
4969
4970 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4971 Return the corresponding abbrev, or NULL if the number is zero (indicating
4972 an empty DIE). In either case *BYTES_READ will be set to the length of
4973 the initial number. */
4974
4975 static struct abbrev_info *
4976 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4977 struct dwarf2_cu *cu)
4978 {
4979 bfd *abfd = cu->objfile->obfd;
4980 unsigned int abbrev_number;
4981 struct abbrev_info *abbrev;
4982
4983 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4984
4985 if (abbrev_number == 0)
4986 return NULL;
4987
4988 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4989 if (!abbrev)
4990 {
4991 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4992 abbrev_number, bfd_get_filename (abfd));
4993 }
4994
4995 return abbrev;
4996 }
4997
4998 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4999 Returns a pointer to the end of a series of DIEs, terminated by an empty
5000 DIE. Any children of the skipped DIEs will also be skipped. */
5001
5002 static gdb_byte *
5003 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5004 {
5005 struct dwarf2_cu *cu = reader->cu;
5006 struct abbrev_info *abbrev;
5007 unsigned int bytes_read;
5008
5009 while (1)
5010 {
5011 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5012 if (abbrev == NULL)
5013 return info_ptr + bytes_read;
5014 else
5015 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5016 }
5017 }
5018
5019 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5020 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5021 abbrev corresponding to that skipped uleb128 should be passed in
5022 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5023 children. */
5024
5025 static gdb_byte *
5026 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5027 struct abbrev_info *abbrev)
5028 {
5029 unsigned int bytes_read;
5030 struct attribute attr;
5031 bfd *abfd = reader->abfd;
5032 struct dwarf2_cu *cu = reader->cu;
5033 gdb_byte *buffer = reader->buffer;
5034 unsigned int form, i;
5035
5036 for (i = 0; i < abbrev->num_attrs; i++)
5037 {
5038 /* The only abbrev we care about is DW_AT_sibling. */
5039 if (abbrev->attrs[i].name == DW_AT_sibling)
5040 {
5041 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5042 if (attr.form == DW_FORM_ref_addr)
5043 complaint (&symfile_complaints,
5044 _("ignoring absolute DW_AT_sibling"));
5045 else
5046 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5047 }
5048
5049 /* If it isn't DW_AT_sibling, skip this attribute. */
5050 form = abbrev->attrs[i].form;
5051 skip_attribute:
5052 switch (form)
5053 {
5054 case DW_FORM_ref_addr:
5055 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5056 and later it is offset sized. */
5057 if (cu->header.version == 2)
5058 info_ptr += cu->header.addr_size;
5059 else
5060 info_ptr += cu->header.offset_size;
5061 break;
5062 case DW_FORM_addr:
5063 info_ptr += cu->header.addr_size;
5064 break;
5065 case DW_FORM_data1:
5066 case DW_FORM_ref1:
5067 case DW_FORM_flag:
5068 info_ptr += 1;
5069 break;
5070 case DW_FORM_flag_present:
5071 break;
5072 case DW_FORM_data2:
5073 case DW_FORM_ref2:
5074 info_ptr += 2;
5075 break;
5076 case DW_FORM_data4:
5077 case DW_FORM_ref4:
5078 info_ptr += 4;
5079 break;
5080 case DW_FORM_data8:
5081 case DW_FORM_ref8:
5082 case DW_FORM_ref_sig8:
5083 info_ptr += 8;
5084 break;
5085 case DW_FORM_string:
5086 read_direct_string (abfd, info_ptr, &bytes_read);
5087 info_ptr += bytes_read;
5088 break;
5089 case DW_FORM_sec_offset:
5090 case DW_FORM_strp:
5091 info_ptr += cu->header.offset_size;
5092 break;
5093 case DW_FORM_exprloc:
5094 case DW_FORM_block:
5095 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5096 info_ptr += bytes_read;
5097 break;
5098 case DW_FORM_block1:
5099 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5100 break;
5101 case DW_FORM_block2:
5102 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5103 break;
5104 case DW_FORM_block4:
5105 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5106 break;
5107 case DW_FORM_sdata:
5108 case DW_FORM_udata:
5109 case DW_FORM_ref_udata:
5110 case DW_FORM_GNU_addr_index:
5111 case DW_FORM_GNU_str_index:
5112 info_ptr = skip_leb128 (abfd, info_ptr);
5113 break;
5114 case DW_FORM_indirect:
5115 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5116 info_ptr += bytes_read;
5117 /* We need to continue parsing from here, so just go back to
5118 the top. */
5119 goto skip_attribute;
5120
5121 default:
5122 error (_("Dwarf Error: Cannot handle %s "
5123 "in DWARF reader [in module %s]"),
5124 dwarf_form_name (form),
5125 bfd_get_filename (abfd));
5126 }
5127 }
5128
5129 if (abbrev->has_children)
5130 return skip_children (reader, info_ptr);
5131 else
5132 return info_ptr;
5133 }
5134
5135 /* Locate ORIG_PDI's sibling.
5136 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5137
5138 static gdb_byte *
5139 locate_pdi_sibling (const struct die_reader_specs *reader,
5140 struct partial_die_info *orig_pdi,
5141 gdb_byte *info_ptr)
5142 {
5143 /* Do we know the sibling already? */
5144
5145 if (orig_pdi->sibling)
5146 return orig_pdi->sibling;
5147
5148 /* Are there any children to deal with? */
5149
5150 if (!orig_pdi->has_children)
5151 return info_ptr;
5152
5153 /* Skip the children the long way. */
5154
5155 return skip_children (reader, info_ptr);
5156 }
5157
5158 /* Expand this partial symbol table into a full symbol table. */
5159
5160 static void
5161 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5162 {
5163 if (pst != NULL)
5164 {
5165 if (pst->readin)
5166 {
5167 warning (_("bug: psymtab for %s is already read in."),
5168 pst->filename);
5169 }
5170 else
5171 {
5172 if (info_verbose)
5173 {
5174 printf_filtered (_("Reading in symbols for %s..."),
5175 pst->filename);
5176 gdb_flush (gdb_stdout);
5177 }
5178
5179 /* Restore our global data. */
5180 dwarf2_per_objfile = objfile_data (pst->objfile,
5181 dwarf2_objfile_data_key);
5182
5183 /* If this psymtab is constructed from a debug-only objfile, the
5184 has_section_at_zero flag will not necessarily be correct. We
5185 can get the correct value for this flag by looking at the data
5186 associated with the (presumably stripped) associated objfile. */
5187 if (pst->objfile->separate_debug_objfile_backlink)
5188 {
5189 struct dwarf2_per_objfile *dpo_backlink
5190 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5191 dwarf2_objfile_data_key);
5192
5193 dwarf2_per_objfile->has_section_at_zero
5194 = dpo_backlink->has_section_at_zero;
5195 }
5196
5197 dwarf2_per_objfile->reading_partial_symbols = 0;
5198
5199 psymtab_to_symtab_1 (pst);
5200
5201 /* Finish up the debug error message. */
5202 if (info_verbose)
5203 printf_filtered (_("done.\n"));
5204 }
5205 }
5206
5207 process_cu_includes ();
5208 }
5209 \f
5210 /* Reading in full CUs. */
5211
5212 /* Add PER_CU to the queue. */
5213
5214 static void
5215 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5216 enum language pretend_language)
5217 {
5218 struct dwarf2_queue_item *item;
5219
5220 per_cu->queued = 1;
5221 item = xmalloc (sizeof (*item));
5222 item->per_cu = per_cu;
5223 item->pretend_language = pretend_language;
5224 item->next = NULL;
5225
5226 if (dwarf2_queue == NULL)
5227 dwarf2_queue = item;
5228 else
5229 dwarf2_queue_tail->next = item;
5230
5231 dwarf2_queue_tail = item;
5232 }
5233
5234 /* Process the queue. */
5235
5236 static void
5237 process_queue (void)
5238 {
5239 struct dwarf2_queue_item *item, *next_item;
5240
5241 /* The queue starts out with one item, but following a DIE reference
5242 may load a new CU, adding it to the end of the queue. */
5243 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5244 {
5245 if (dwarf2_per_objfile->using_index
5246 ? !item->per_cu->v.quick->symtab
5247 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5248 process_full_comp_unit (item->per_cu, item->pretend_language);
5249
5250 item->per_cu->queued = 0;
5251 next_item = item->next;
5252 xfree (item);
5253 }
5254
5255 dwarf2_queue_tail = NULL;
5256 }
5257
5258 /* Free all allocated queue entries. This function only releases anything if
5259 an error was thrown; if the queue was processed then it would have been
5260 freed as we went along. */
5261
5262 static void
5263 dwarf2_release_queue (void *dummy)
5264 {
5265 struct dwarf2_queue_item *item, *last;
5266
5267 item = dwarf2_queue;
5268 while (item)
5269 {
5270 /* Anything still marked queued is likely to be in an
5271 inconsistent state, so discard it. */
5272 if (item->per_cu->queued)
5273 {
5274 if (item->per_cu->cu != NULL)
5275 free_one_cached_comp_unit (item->per_cu);
5276 item->per_cu->queued = 0;
5277 }
5278
5279 last = item;
5280 item = item->next;
5281 xfree (last);
5282 }
5283
5284 dwarf2_queue = dwarf2_queue_tail = NULL;
5285 }
5286
5287 /* Read in full symbols for PST, and anything it depends on. */
5288
5289 static void
5290 psymtab_to_symtab_1 (struct partial_symtab *pst)
5291 {
5292 struct dwarf2_per_cu_data *per_cu;
5293 int i;
5294
5295 if (pst->readin)
5296 return;
5297
5298 for (i = 0; i < pst->number_of_dependencies; i++)
5299 if (!pst->dependencies[i]->readin
5300 && pst->dependencies[i]->user == NULL)
5301 {
5302 /* Inform about additional files that need to be read in. */
5303 if (info_verbose)
5304 {
5305 /* FIXME: i18n: Need to make this a single string. */
5306 fputs_filtered (" ", gdb_stdout);
5307 wrap_here ("");
5308 fputs_filtered ("and ", gdb_stdout);
5309 wrap_here ("");
5310 printf_filtered ("%s...", pst->dependencies[i]->filename);
5311 wrap_here (""); /* Flush output. */
5312 gdb_flush (gdb_stdout);
5313 }
5314 psymtab_to_symtab_1 (pst->dependencies[i]);
5315 }
5316
5317 per_cu = pst->read_symtab_private;
5318
5319 if (per_cu == NULL)
5320 {
5321 /* It's an include file, no symbols to read for it.
5322 Everything is in the parent symtab. */
5323 pst->readin = 1;
5324 return;
5325 }
5326
5327 dw2_do_instantiate_symtab (per_cu);
5328 }
5329
5330 /* Trivial hash function for die_info: the hash value of a DIE
5331 is its offset in .debug_info for this objfile. */
5332
5333 static hashval_t
5334 die_hash (const void *item)
5335 {
5336 const struct die_info *die = item;
5337
5338 return die->offset.sect_off;
5339 }
5340
5341 /* Trivial comparison function for die_info structures: two DIEs
5342 are equal if they have the same offset. */
5343
5344 static int
5345 die_eq (const void *item_lhs, const void *item_rhs)
5346 {
5347 const struct die_info *die_lhs = item_lhs;
5348 const struct die_info *die_rhs = item_rhs;
5349
5350 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5351 }
5352
5353 /* die_reader_func for load_full_comp_unit.
5354 This is identical to read_signatured_type_reader,
5355 but is kept separate for now. */
5356
5357 static void
5358 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5359 gdb_byte *info_ptr,
5360 struct die_info *comp_unit_die,
5361 int has_children,
5362 void *data)
5363 {
5364 struct dwarf2_cu *cu = reader->cu;
5365 enum language *language_ptr = data;
5366
5367 gdb_assert (cu->die_hash == NULL);
5368 cu->die_hash =
5369 htab_create_alloc_ex (cu->header.length / 12,
5370 die_hash,
5371 die_eq,
5372 NULL,
5373 &cu->comp_unit_obstack,
5374 hashtab_obstack_allocate,
5375 dummy_obstack_deallocate);
5376
5377 if (has_children)
5378 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5379 &info_ptr, comp_unit_die);
5380 cu->dies = comp_unit_die;
5381 /* comp_unit_die is not stored in die_hash, no need. */
5382
5383 /* We try not to read any attributes in this function, because not
5384 all CUs needed for references have been loaded yet, and symbol
5385 table processing isn't initialized. But we have to set the CU language,
5386 or we won't be able to build types correctly.
5387 Similarly, if we do not read the producer, we can not apply
5388 producer-specific interpretation. */
5389 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5390 }
5391
5392 /* Load the DIEs associated with PER_CU into memory. */
5393
5394 static void
5395 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5396 enum language pretend_language)
5397 {
5398 gdb_assert (! this_cu->is_debug_types);
5399
5400 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5401 &pretend_language);
5402 }
5403
5404 /* Add a DIE to the delayed physname list. */
5405
5406 static void
5407 add_to_method_list (struct type *type, int fnfield_index, int index,
5408 const char *name, struct die_info *die,
5409 struct dwarf2_cu *cu)
5410 {
5411 struct delayed_method_info mi;
5412 mi.type = type;
5413 mi.fnfield_index = fnfield_index;
5414 mi.index = index;
5415 mi.name = name;
5416 mi.die = die;
5417 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5418 }
5419
5420 /* A cleanup for freeing the delayed method list. */
5421
5422 static void
5423 free_delayed_list (void *ptr)
5424 {
5425 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5426 if (cu->method_list != NULL)
5427 {
5428 VEC_free (delayed_method_info, cu->method_list);
5429 cu->method_list = NULL;
5430 }
5431 }
5432
5433 /* Compute the physnames of any methods on the CU's method list.
5434
5435 The computation of method physnames is delayed in order to avoid the
5436 (bad) condition that one of the method's formal parameters is of an as yet
5437 incomplete type. */
5438
5439 static void
5440 compute_delayed_physnames (struct dwarf2_cu *cu)
5441 {
5442 int i;
5443 struct delayed_method_info *mi;
5444 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5445 {
5446 const char *physname;
5447 struct fn_fieldlist *fn_flp
5448 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5449 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5450 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5451 }
5452 }
5453
5454 /* Go objects should be embedded in a DW_TAG_module DIE,
5455 and it's not clear if/how imported objects will appear.
5456 To keep Go support simple until that's worked out,
5457 go back through what we've read and create something usable.
5458 We could do this while processing each DIE, and feels kinda cleaner,
5459 but that way is more invasive.
5460 This is to, for example, allow the user to type "p var" or "b main"
5461 without having to specify the package name, and allow lookups
5462 of module.object to work in contexts that use the expression
5463 parser. */
5464
5465 static void
5466 fixup_go_packaging (struct dwarf2_cu *cu)
5467 {
5468 char *package_name = NULL;
5469 struct pending *list;
5470 int i;
5471
5472 for (list = global_symbols; list != NULL; list = list->next)
5473 {
5474 for (i = 0; i < list->nsyms; ++i)
5475 {
5476 struct symbol *sym = list->symbol[i];
5477
5478 if (SYMBOL_LANGUAGE (sym) == language_go
5479 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5480 {
5481 char *this_package_name = go_symbol_package_name (sym);
5482
5483 if (this_package_name == NULL)
5484 continue;
5485 if (package_name == NULL)
5486 package_name = this_package_name;
5487 else
5488 {
5489 if (strcmp (package_name, this_package_name) != 0)
5490 complaint (&symfile_complaints,
5491 _("Symtab %s has objects from two different Go packages: %s and %s"),
5492 (sym->symtab && sym->symtab->filename
5493 ? sym->symtab->filename
5494 : cu->objfile->name),
5495 this_package_name, package_name);
5496 xfree (this_package_name);
5497 }
5498 }
5499 }
5500 }
5501
5502 if (package_name != NULL)
5503 {
5504 struct objfile *objfile = cu->objfile;
5505 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5506 package_name, objfile);
5507 struct symbol *sym;
5508
5509 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5510
5511 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5512 SYMBOL_SET_LANGUAGE (sym, language_go);
5513 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5514 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5515 e.g., "main" finds the "main" module and not C's main(). */
5516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5517 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5518 SYMBOL_TYPE (sym) = type;
5519
5520 add_symbol_to_list (sym, &global_symbols);
5521
5522 xfree (package_name);
5523 }
5524 }
5525
5526 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5527
5528 /* Return the symtab for PER_CU. This works properly regardless of
5529 whether we're using the index or psymtabs. */
5530
5531 static struct symtab *
5532 get_symtab (struct dwarf2_per_cu_data *per_cu)
5533 {
5534 return (dwarf2_per_objfile->using_index
5535 ? per_cu->v.quick->symtab
5536 : per_cu->v.psymtab->symtab);
5537 }
5538
5539 /* A helper function for computing the list of all symbol tables
5540 included by PER_CU. */
5541
5542 static void
5543 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5544 htab_t all_children,
5545 struct dwarf2_per_cu_data *per_cu)
5546 {
5547 void **slot;
5548 int ix;
5549 struct dwarf2_per_cu_data *iter;
5550
5551 slot = htab_find_slot (all_children, per_cu, INSERT);
5552 if (*slot != NULL)
5553 {
5554 /* This inclusion and its children have been processed. */
5555 return;
5556 }
5557
5558 *slot = per_cu;
5559 /* Only add a CU if it has a symbol table. */
5560 if (get_symtab (per_cu) != NULL)
5561 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5562
5563 for (ix = 0;
5564 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5565 ++ix)
5566 recursively_compute_inclusions (result, all_children, iter);
5567 }
5568
5569 /* Compute the symtab 'includes' fields for the symtab related to
5570 PER_CU. */
5571
5572 static void
5573 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5574 {
5575 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5576 {
5577 int ix, len;
5578 struct dwarf2_per_cu_data *iter;
5579 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5580 htab_t all_children;
5581 struct symtab *symtab = get_symtab (per_cu);
5582
5583 /* If we don't have a symtab, we can just skip this case. */
5584 if (symtab == NULL)
5585 return;
5586
5587 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5588 NULL, xcalloc, xfree);
5589
5590 for (ix = 0;
5591 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5592 ix, iter);
5593 ++ix)
5594 recursively_compute_inclusions (&result_children, all_children, iter);
5595
5596 /* Now we have a transitive closure of all the included CUs, so
5597 we can convert it to a list of symtabs. */
5598 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5599 symtab->includes
5600 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5601 (len + 1) * sizeof (struct symtab *));
5602 for (ix = 0;
5603 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5604 ++ix)
5605 symtab->includes[ix] = get_symtab (iter);
5606 symtab->includes[len] = NULL;
5607
5608 VEC_free (dwarf2_per_cu_ptr, result_children);
5609 htab_delete (all_children);
5610 }
5611 }
5612
5613 /* Compute the 'includes' field for the symtabs of all the CUs we just
5614 read. */
5615
5616 static void
5617 process_cu_includes (void)
5618 {
5619 int ix;
5620 struct dwarf2_per_cu_data *iter;
5621
5622 for (ix = 0;
5623 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5624 ix, iter);
5625 ++ix)
5626 compute_symtab_includes (iter);
5627
5628 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5629 }
5630
5631 /* Generate full symbol information for PER_CU, whose DIEs have
5632 already been loaded into memory. */
5633
5634 static void
5635 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5636 enum language pretend_language)
5637 {
5638 struct dwarf2_cu *cu = per_cu->cu;
5639 struct objfile *objfile = per_cu->objfile;
5640 CORE_ADDR lowpc, highpc;
5641 struct symtab *symtab;
5642 struct cleanup *back_to, *delayed_list_cleanup;
5643 CORE_ADDR baseaddr;
5644
5645 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5646
5647 buildsym_init ();
5648 back_to = make_cleanup (really_free_pendings, NULL);
5649 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5650
5651 cu->list_in_scope = &file_symbols;
5652
5653 cu->language = pretend_language;
5654 cu->language_defn = language_def (cu->language);
5655
5656 /* Do line number decoding in read_file_scope () */
5657 process_die (cu->dies, cu);
5658
5659 /* For now fudge the Go package. */
5660 if (cu->language == language_go)
5661 fixup_go_packaging (cu);
5662
5663 /* Now that we have processed all the DIEs in the CU, all the types
5664 should be complete, and it should now be safe to compute all of the
5665 physnames. */
5666 compute_delayed_physnames (cu);
5667 do_cleanups (delayed_list_cleanup);
5668
5669 /* Some compilers don't define a DW_AT_high_pc attribute for the
5670 compilation unit. If the DW_AT_high_pc is missing, synthesize
5671 it, by scanning the DIE's below the compilation unit. */
5672 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5673
5674 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5675
5676 if (symtab != NULL)
5677 {
5678 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5679
5680 /* Set symtab language to language from DW_AT_language. If the
5681 compilation is from a C file generated by language preprocessors, do
5682 not set the language if it was already deduced by start_subfile. */
5683 if (!(cu->language == language_c && symtab->language != language_c))
5684 symtab->language = cu->language;
5685
5686 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5687 produce DW_AT_location with location lists but it can be possibly
5688 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5689 there were bugs in prologue debug info, fixed later in GCC-4.5
5690 by "unwind info for epilogues" patch (which is not directly related).
5691
5692 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5693 needed, it would be wrong due to missing DW_AT_producer there.
5694
5695 Still one can confuse GDB by using non-standard GCC compilation
5696 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5697 */
5698 if (cu->has_loclist && gcc_4_minor >= 5)
5699 symtab->locations_valid = 1;
5700
5701 if (gcc_4_minor >= 5)
5702 symtab->epilogue_unwind_valid = 1;
5703
5704 symtab->call_site_htab = cu->call_site_htab;
5705 }
5706
5707 if (dwarf2_per_objfile->using_index)
5708 per_cu->v.quick->symtab = symtab;
5709 else
5710 {
5711 struct partial_symtab *pst = per_cu->v.psymtab;
5712 pst->symtab = symtab;
5713 pst->readin = 1;
5714 }
5715
5716 /* Push it for inclusion processing later. */
5717 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5718
5719 do_cleanups (back_to);
5720 }
5721
5722 /* Process an imported unit DIE. */
5723
5724 static void
5725 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5726 {
5727 struct attribute *attr;
5728
5729 attr = dwarf2_attr (die, DW_AT_import, cu);
5730 if (attr != NULL)
5731 {
5732 struct dwarf2_per_cu_data *per_cu;
5733 struct symtab *imported_symtab;
5734 sect_offset offset;
5735
5736 offset = dwarf2_get_ref_die_offset (attr);
5737 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5738
5739 /* Queue the unit, if needed. */
5740 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5741 load_full_comp_unit (per_cu, cu->language);
5742
5743 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5744 per_cu);
5745 }
5746 }
5747
5748 /* Process a die and its children. */
5749
5750 static void
5751 process_die (struct die_info *die, struct dwarf2_cu *cu)
5752 {
5753 switch (die->tag)
5754 {
5755 case DW_TAG_padding:
5756 break;
5757 case DW_TAG_compile_unit:
5758 case DW_TAG_partial_unit:
5759 read_file_scope (die, cu);
5760 break;
5761 case DW_TAG_type_unit:
5762 read_type_unit_scope (die, cu);
5763 break;
5764 case DW_TAG_subprogram:
5765 case DW_TAG_inlined_subroutine:
5766 read_func_scope (die, cu);
5767 break;
5768 case DW_TAG_lexical_block:
5769 case DW_TAG_try_block:
5770 case DW_TAG_catch_block:
5771 read_lexical_block_scope (die, cu);
5772 break;
5773 case DW_TAG_GNU_call_site:
5774 read_call_site_scope (die, cu);
5775 break;
5776 case DW_TAG_class_type:
5777 case DW_TAG_interface_type:
5778 case DW_TAG_structure_type:
5779 case DW_TAG_union_type:
5780 process_structure_scope (die, cu);
5781 break;
5782 case DW_TAG_enumeration_type:
5783 process_enumeration_scope (die, cu);
5784 break;
5785
5786 /* These dies have a type, but processing them does not create
5787 a symbol or recurse to process the children. Therefore we can
5788 read them on-demand through read_type_die. */
5789 case DW_TAG_subroutine_type:
5790 case DW_TAG_set_type:
5791 case DW_TAG_array_type:
5792 case DW_TAG_pointer_type:
5793 case DW_TAG_ptr_to_member_type:
5794 case DW_TAG_reference_type:
5795 case DW_TAG_string_type:
5796 break;
5797
5798 case DW_TAG_base_type:
5799 case DW_TAG_subrange_type:
5800 case DW_TAG_typedef:
5801 /* Add a typedef symbol for the type definition, if it has a
5802 DW_AT_name. */
5803 new_symbol (die, read_type_die (die, cu), cu);
5804 break;
5805 case DW_TAG_common_block:
5806 read_common_block (die, cu);
5807 break;
5808 case DW_TAG_common_inclusion:
5809 break;
5810 case DW_TAG_namespace:
5811 processing_has_namespace_info = 1;
5812 read_namespace (die, cu);
5813 break;
5814 case DW_TAG_module:
5815 processing_has_namespace_info = 1;
5816 read_module (die, cu);
5817 break;
5818 case DW_TAG_imported_declaration:
5819 case DW_TAG_imported_module:
5820 processing_has_namespace_info = 1;
5821 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5822 || cu->language != language_fortran))
5823 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5824 dwarf_tag_name (die->tag));
5825 read_import_statement (die, cu);
5826 break;
5827
5828 case DW_TAG_imported_unit:
5829 process_imported_unit_die (die, cu);
5830 break;
5831
5832 default:
5833 new_symbol (die, NULL, cu);
5834 break;
5835 }
5836 }
5837
5838 /* A helper function for dwarf2_compute_name which determines whether DIE
5839 needs to have the name of the scope prepended to the name listed in the
5840 die. */
5841
5842 static int
5843 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5844 {
5845 struct attribute *attr;
5846
5847 switch (die->tag)
5848 {
5849 case DW_TAG_namespace:
5850 case DW_TAG_typedef:
5851 case DW_TAG_class_type:
5852 case DW_TAG_interface_type:
5853 case DW_TAG_structure_type:
5854 case DW_TAG_union_type:
5855 case DW_TAG_enumeration_type:
5856 case DW_TAG_enumerator:
5857 case DW_TAG_subprogram:
5858 case DW_TAG_member:
5859 return 1;
5860
5861 case DW_TAG_variable:
5862 case DW_TAG_constant:
5863 /* We only need to prefix "globally" visible variables. These include
5864 any variable marked with DW_AT_external or any variable that
5865 lives in a namespace. [Variables in anonymous namespaces
5866 require prefixing, but they are not DW_AT_external.] */
5867
5868 if (dwarf2_attr (die, DW_AT_specification, cu))
5869 {
5870 struct dwarf2_cu *spec_cu = cu;
5871
5872 return die_needs_namespace (die_specification (die, &spec_cu),
5873 spec_cu);
5874 }
5875
5876 attr = dwarf2_attr (die, DW_AT_external, cu);
5877 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5878 && die->parent->tag != DW_TAG_module)
5879 return 0;
5880 /* A variable in a lexical block of some kind does not need a
5881 namespace, even though in C++ such variables may be external
5882 and have a mangled name. */
5883 if (die->parent->tag == DW_TAG_lexical_block
5884 || die->parent->tag == DW_TAG_try_block
5885 || die->parent->tag == DW_TAG_catch_block
5886 || die->parent->tag == DW_TAG_subprogram)
5887 return 0;
5888 return 1;
5889
5890 default:
5891 return 0;
5892 }
5893 }
5894
5895 /* Retrieve the last character from a mem_file. */
5896
5897 static void
5898 do_ui_file_peek_last (void *object, const char *buffer, long length)
5899 {
5900 char *last_char_p = (char *) object;
5901
5902 if (length > 0)
5903 *last_char_p = buffer[length - 1];
5904 }
5905
5906 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5907 compute the physname for the object, which include a method's:
5908 - formal parameters (C++/Java),
5909 - receiver type (Go),
5910 - return type (Java).
5911
5912 The term "physname" is a bit confusing.
5913 For C++, for example, it is the demangled name.
5914 For Go, for example, it's the mangled name.
5915
5916 For Ada, return the DIE's linkage name rather than the fully qualified
5917 name. PHYSNAME is ignored..
5918
5919 The result is allocated on the objfile_obstack and canonicalized. */
5920
5921 static const char *
5922 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5923 int physname)
5924 {
5925 struct objfile *objfile = cu->objfile;
5926
5927 if (name == NULL)
5928 name = dwarf2_name (die, cu);
5929
5930 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5931 compute it by typename_concat inside GDB. */
5932 if (cu->language == language_ada
5933 || (cu->language == language_fortran && physname))
5934 {
5935 /* For Ada unit, we prefer the linkage name over the name, as
5936 the former contains the exported name, which the user expects
5937 to be able to reference. Ideally, we want the user to be able
5938 to reference this entity using either natural or linkage name,
5939 but we haven't started looking at this enhancement yet. */
5940 struct attribute *attr;
5941
5942 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5943 if (attr == NULL)
5944 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5945 if (attr && DW_STRING (attr))
5946 return DW_STRING (attr);
5947 }
5948
5949 /* These are the only languages we know how to qualify names in. */
5950 if (name != NULL
5951 && (cu->language == language_cplus || cu->language == language_java
5952 || cu->language == language_fortran))
5953 {
5954 if (die_needs_namespace (die, cu))
5955 {
5956 long length;
5957 const char *prefix;
5958 struct ui_file *buf;
5959
5960 prefix = determine_prefix (die, cu);
5961 buf = mem_fileopen ();
5962 if (*prefix != '\0')
5963 {
5964 char *prefixed_name = typename_concat (NULL, prefix, name,
5965 physname, cu);
5966
5967 fputs_unfiltered (prefixed_name, buf);
5968 xfree (prefixed_name);
5969 }
5970 else
5971 fputs_unfiltered (name, buf);
5972
5973 /* Template parameters may be specified in the DIE's DW_AT_name, or
5974 as children with DW_TAG_template_type_param or
5975 DW_TAG_value_type_param. If the latter, add them to the name
5976 here. If the name already has template parameters, then
5977 skip this step; some versions of GCC emit both, and
5978 it is more efficient to use the pre-computed name.
5979
5980 Something to keep in mind about this process: it is very
5981 unlikely, or in some cases downright impossible, to produce
5982 something that will match the mangled name of a function.
5983 If the definition of the function has the same debug info,
5984 we should be able to match up with it anyway. But fallbacks
5985 using the minimal symbol, for instance to find a method
5986 implemented in a stripped copy of libstdc++, will not work.
5987 If we do not have debug info for the definition, we will have to
5988 match them up some other way.
5989
5990 When we do name matching there is a related problem with function
5991 templates; two instantiated function templates are allowed to
5992 differ only by their return types, which we do not add here. */
5993
5994 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5995 {
5996 struct attribute *attr;
5997 struct die_info *child;
5998 int first = 1;
5999
6000 die->building_fullname = 1;
6001
6002 for (child = die->child; child != NULL; child = child->sibling)
6003 {
6004 struct type *type;
6005 LONGEST value;
6006 gdb_byte *bytes;
6007 struct dwarf2_locexpr_baton *baton;
6008 struct value *v;
6009
6010 if (child->tag != DW_TAG_template_type_param
6011 && child->tag != DW_TAG_template_value_param)
6012 continue;
6013
6014 if (first)
6015 {
6016 fputs_unfiltered ("<", buf);
6017 first = 0;
6018 }
6019 else
6020 fputs_unfiltered (", ", buf);
6021
6022 attr = dwarf2_attr (child, DW_AT_type, cu);
6023 if (attr == NULL)
6024 {
6025 complaint (&symfile_complaints,
6026 _("template parameter missing DW_AT_type"));
6027 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6028 continue;
6029 }
6030 type = die_type (child, cu);
6031
6032 if (child->tag == DW_TAG_template_type_param)
6033 {
6034 c_print_type (type, "", buf, -1, 0);
6035 continue;
6036 }
6037
6038 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6039 if (attr == NULL)
6040 {
6041 complaint (&symfile_complaints,
6042 _("template parameter missing "
6043 "DW_AT_const_value"));
6044 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6045 continue;
6046 }
6047
6048 dwarf2_const_value_attr (attr, type, name,
6049 &cu->comp_unit_obstack, cu,
6050 &value, &bytes, &baton);
6051
6052 if (TYPE_NOSIGN (type))
6053 /* GDB prints characters as NUMBER 'CHAR'. If that's
6054 changed, this can use value_print instead. */
6055 c_printchar (value, type, buf);
6056 else
6057 {
6058 struct value_print_options opts;
6059
6060 if (baton != NULL)
6061 v = dwarf2_evaluate_loc_desc (type, NULL,
6062 baton->data,
6063 baton->size,
6064 baton->per_cu);
6065 else if (bytes != NULL)
6066 {
6067 v = allocate_value (type);
6068 memcpy (value_contents_writeable (v), bytes,
6069 TYPE_LENGTH (type));
6070 }
6071 else
6072 v = value_from_longest (type, value);
6073
6074 /* Specify decimal so that we do not depend on
6075 the radix. */
6076 get_formatted_print_options (&opts, 'd');
6077 opts.raw = 1;
6078 value_print (v, buf, &opts);
6079 release_value (v);
6080 value_free (v);
6081 }
6082 }
6083
6084 die->building_fullname = 0;
6085
6086 if (!first)
6087 {
6088 /* Close the argument list, with a space if necessary
6089 (nested templates). */
6090 char last_char = '\0';
6091 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6092 if (last_char == '>')
6093 fputs_unfiltered (" >", buf);
6094 else
6095 fputs_unfiltered (">", buf);
6096 }
6097 }
6098
6099 /* For Java and C++ methods, append formal parameter type
6100 information, if PHYSNAME. */
6101
6102 if (physname && die->tag == DW_TAG_subprogram
6103 && (cu->language == language_cplus
6104 || cu->language == language_java))
6105 {
6106 struct type *type = read_type_die (die, cu);
6107
6108 c_type_print_args (type, buf, 1, cu->language);
6109
6110 if (cu->language == language_java)
6111 {
6112 /* For java, we must append the return type to method
6113 names. */
6114 if (die->tag == DW_TAG_subprogram)
6115 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6116 0, 0);
6117 }
6118 else if (cu->language == language_cplus)
6119 {
6120 /* Assume that an artificial first parameter is
6121 "this", but do not crash if it is not. RealView
6122 marks unnamed (and thus unused) parameters as
6123 artificial; there is no way to differentiate
6124 the two cases. */
6125 if (TYPE_NFIELDS (type) > 0
6126 && TYPE_FIELD_ARTIFICIAL (type, 0)
6127 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6128 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6129 0))))
6130 fputs_unfiltered (" const", buf);
6131 }
6132 }
6133
6134 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6135 &length);
6136 ui_file_delete (buf);
6137
6138 if (cu->language == language_cplus)
6139 {
6140 char *cname
6141 = dwarf2_canonicalize_name (name, cu,
6142 &objfile->objfile_obstack);
6143
6144 if (cname != NULL)
6145 name = cname;
6146 }
6147 }
6148 }
6149
6150 return name;
6151 }
6152
6153 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6154 If scope qualifiers are appropriate they will be added. The result
6155 will be allocated on the objfile_obstack, or NULL if the DIE does
6156 not have a name. NAME may either be from a previous call to
6157 dwarf2_name or NULL.
6158
6159 The output string will be canonicalized (if C++/Java). */
6160
6161 static const char *
6162 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6163 {
6164 return dwarf2_compute_name (name, die, cu, 0);
6165 }
6166
6167 /* Construct a physname for the given DIE in CU. NAME may either be
6168 from a previous call to dwarf2_name or NULL. The result will be
6169 allocated on the objfile_objstack or NULL if the DIE does not have a
6170 name.
6171
6172 The output string will be canonicalized (if C++/Java). */
6173
6174 static const char *
6175 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6176 {
6177 struct objfile *objfile = cu->objfile;
6178 struct attribute *attr;
6179 const char *retval, *mangled = NULL, *canon = NULL;
6180 struct cleanup *back_to;
6181 int need_copy = 1;
6182
6183 /* In this case dwarf2_compute_name is just a shortcut not building anything
6184 on its own. */
6185 if (!die_needs_namespace (die, cu))
6186 return dwarf2_compute_name (name, die, cu, 1);
6187
6188 back_to = make_cleanup (null_cleanup, NULL);
6189
6190 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6191 if (!attr)
6192 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6193
6194 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6195 has computed. */
6196 if (attr && DW_STRING (attr))
6197 {
6198 char *demangled;
6199
6200 mangled = DW_STRING (attr);
6201
6202 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6203 type. It is easier for GDB users to search for such functions as
6204 `name(params)' than `long name(params)'. In such case the minimal
6205 symbol names do not match the full symbol names but for template
6206 functions there is never a need to look up their definition from their
6207 declaration so the only disadvantage remains the minimal symbol
6208 variant `long name(params)' does not have the proper inferior type.
6209 */
6210
6211 if (cu->language == language_go)
6212 {
6213 /* This is a lie, but we already lie to the caller new_symbol_full.
6214 new_symbol_full assumes we return the mangled name.
6215 This just undoes that lie until things are cleaned up. */
6216 demangled = NULL;
6217 }
6218 else
6219 {
6220 demangled = cplus_demangle (mangled,
6221 (DMGL_PARAMS | DMGL_ANSI
6222 | (cu->language == language_java
6223 ? DMGL_JAVA | DMGL_RET_POSTFIX
6224 : DMGL_RET_DROP)));
6225 }
6226 if (demangled)
6227 {
6228 make_cleanup (xfree, demangled);
6229 canon = demangled;
6230 }
6231 else
6232 {
6233 canon = mangled;
6234 need_copy = 0;
6235 }
6236 }
6237
6238 if (canon == NULL || check_physname)
6239 {
6240 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6241
6242 if (canon != NULL && strcmp (physname, canon) != 0)
6243 {
6244 /* It may not mean a bug in GDB. The compiler could also
6245 compute DW_AT_linkage_name incorrectly. But in such case
6246 GDB would need to be bug-to-bug compatible. */
6247
6248 complaint (&symfile_complaints,
6249 _("Computed physname <%s> does not match demangled <%s> "
6250 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6251 physname, canon, mangled, die->offset.sect_off, objfile->name);
6252
6253 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6254 is available here - over computed PHYSNAME. It is safer
6255 against both buggy GDB and buggy compilers. */
6256
6257 retval = canon;
6258 }
6259 else
6260 {
6261 retval = physname;
6262 need_copy = 0;
6263 }
6264 }
6265 else
6266 retval = canon;
6267
6268 if (need_copy)
6269 retval = obsavestring (retval, strlen (retval),
6270 &objfile->objfile_obstack);
6271
6272 do_cleanups (back_to);
6273 return retval;
6274 }
6275
6276 /* Read the import statement specified by the given die and record it. */
6277
6278 static void
6279 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6280 {
6281 struct objfile *objfile = cu->objfile;
6282 struct attribute *import_attr;
6283 struct die_info *imported_die, *child_die;
6284 struct dwarf2_cu *imported_cu;
6285 const char *imported_name;
6286 const char *imported_name_prefix;
6287 const char *canonical_name;
6288 const char *import_alias;
6289 const char *imported_declaration = NULL;
6290 const char *import_prefix;
6291 VEC (const_char_ptr) *excludes = NULL;
6292 struct cleanup *cleanups;
6293
6294 char *temp;
6295
6296 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6297 if (import_attr == NULL)
6298 {
6299 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6300 dwarf_tag_name (die->tag));
6301 return;
6302 }
6303
6304 imported_cu = cu;
6305 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6306 imported_name = dwarf2_name (imported_die, imported_cu);
6307 if (imported_name == NULL)
6308 {
6309 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6310
6311 The import in the following code:
6312 namespace A
6313 {
6314 typedef int B;
6315 }
6316
6317 int main ()
6318 {
6319 using A::B;
6320 B b;
6321 return b;
6322 }
6323
6324 ...
6325 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6326 <52> DW_AT_decl_file : 1
6327 <53> DW_AT_decl_line : 6
6328 <54> DW_AT_import : <0x75>
6329 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6330 <59> DW_AT_name : B
6331 <5b> DW_AT_decl_file : 1
6332 <5c> DW_AT_decl_line : 2
6333 <5d> DW_AT_type : <0x6e>
6334 ...
6335 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6336 <76> DW_AT_byte_size : 4
6337 <77> DW_AT_encoding : 5 (signed)
6338
6339 imports the wrong die ( 0x75 instead of 0x58 ).
6340 This case will be ignored until the gcc bug is fixed. */
6341 return;
6342 }
6343
6344 /* Figure out the local name after import. */
6345 import_alias = dwarf2_name (die, cu);
6346
6347 /* Figure out where the statement is being imported to. */
6348 import_prefix = determine_prefix (die, cu);
6349
6350 /* Figure out what the scope of the imported die is and prepend it
6351 to the name of the imported die. */
6352 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6353
6354 if (imported_die->tag != DW_TAG_namespace
6355 && imported_die->tag != DW_TAG_module)
6356 {
6357 imported_declaration = imported_name;
6358 canonical_name = imported_name_prefix;
6359 }
6360 else if (strlen (imported_name_prefix) > 0)
6361 {
6362 temp = alloca (strlen (imported_name_prefix)
6363 + 2 + strlen (imported_name) + 1);
6364 strcpy (temp, imported_name_prefix);
6365 strcat (temp, "::");
6366 strcat (temp, imported_name);
6367 canonical_name = temp;
6368 }
6369 else
6370 canonical_name = imported_name;
6371
6372 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6373
6374 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6375 for (child_die = die->child; child_die && child_die->tag;
6376 child_die = sibling_die (child_die))
6377 {
6378 /* DWARF-4: A Fortran use statement with a “rename list” may be
6379 represented by an imported module entry with an import attribute
6380 referring to the module and owned entries corresponding to those
6381 entities that are renamed as part of being imported. */
6382
6383 if (child_die->tag != DW_TAG_imported_declaration)
6384 {
6385 complaint (&symfile_complaints,
6386 _("child DW_TAG_imported_declaration expected "
6387 "- DIE at 0x%x [in module %s]"),
6388 child_die->offset.sect_off, objfile->name);
6389 continue;
6390 }
6391
6392 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6393 if (import_attr == NULL)
6394 {
6395 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6396 dwarf_tag_name (child_die->tag));
6397 continue;
6398 }
6399
6400 imported_cu = cu;
6401 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6402 &imported_cu);
6403 imported_name = dwarf2_name (imported_die, imported_cu);
6404 if (imported_name == NULL)
6405 {
6406 complaint (&symfile_complaints,
6407 _("child DW_TAG_imported_declaration has unknown "
6408 "imported name - DIE at 0x%x [in module %s]"),
6409 child_die->offset.sect_off, objfile->name);
6410 continue;
6411 }
6412
6413 VEC_safe_push (const_char_ptr, excludes, imported_name);
6414
6415 process_die (child_die, cu);
6416 }
6417
6418 cp_add_using_directive (import_prefix,
6419 canonical_name,
6420 import_alias,
6421 imported_declaration,
6422 excludes,
6423 &objfile->objfile_obstack);
6424
6425 do_cleanups (cleanups);
6426 }
6427
6428 /* Cleanup function for read_file_scope. */
6429
6430 static void
6431 free_cu_line_header (void *arg)
6432 {
6433 struct dwarf2_cu *cu = arg;
6434
6435 free_line_header (cu->line_header);
6436 cu->line_header = NULL;
6437 }
6438
6439 static void
6440 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6441 char **name, char **comp_dir)
6442 {
6443 struct attribute *attr;
6444
6445 *name = NULL;
6446 *comp_dir = NULL;
6447
6448 /* Find the filename. Do not use dwarf2_name here, since the filename
6449 is not a source language identifier. */
6450 attr = dwarf2_attr (die, DW_AT_name, cu);
6451 if (attr)
6452 {
6453 *name = DW_STRING (attr);
6454 }
6455
6456 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6457 if (attr)
6458 *comp_dir = DW_STRING (attr);
6459 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6460 {
6461 *comp_dir = ldirname (*name);
6462 if (*comp_dir != NULL)
6463 make_cleanup (xfree, *comp_dir);
6464 }
6465 if (*comp_dir != NULL)
6466 {
6467 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6468 directory, get rid of it. */
6469 char *cp = strchr (*comp_dir, ':');
6470
6471 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6472 *comp_dir = cp + 1;
6473 }
6474
6475 if (*name == NULL)
6476 *name = "<unknown>";
6477 }
6478
6479 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6480 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6481 COMP_DIR is the compilation directory.
6482 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6483
6484 static void
6485 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6486 const char *comp_dir, int want_line_info)
6487 {
6488 struct attribute *attr;
6489
6490 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6491 if (attr)
6492 {
6493 unsigned int line_offset = DW_UNSND (attr);
6494 struct line_header *line_header
6495 = dwarf_decode_line_header (line_offset, cu);
6496
6497 if (line_header)
6498 {
6499 cu->line_header = line_header;
6500 make_cleanup (free_cu_line_header, cu);
6501 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6502 }
6503 }
6504 }
6505
6506 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6507
6508 static void
6509 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6510 {
6511 struct objfile *objfile = dwarf2_per_objfile->objfile;
6512 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6513 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6514 CORE_ADDR highpc = ((CORE_ADDR) 0);
6515 struct attribute *attr;
6516 char *name = NULL;
6517 char *comp_dir = NULL;
6518 struct die_info *child_die;
6519 bfd *abfd = objfile->obfd;
6520 CORE_ADDR baseaddr;
6521
6522 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6523
6524 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6525
6526 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6527 from finish_block. */
6528 if (lowpc == ((CORE_ADDR) -1))
6529 lowpc = highpc;
6530 lowpc += baseaddr;
6531 highpc += baseaddr;
6532
6533 find_file_and_directory (die, cu, &name, &comp_dir);
6534
6535 prepare_one_comp_unit (cu, die, cu->language);
6536
6537 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6538 standardised yet. As a workaround for the language detection we fall
6539 back to the DW_AT_producer string. */
6540 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6541 cu->language = language_opencl;
6542
6543 /* Similar hack for Go. */
6544 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6545 set_cu_language (DW_LANG_Go, cu);
6546
6547 /* We assume that we're processing GCC output. */
6548 processing_gcc_compilation = 2;
6549
6550 processing_has_namespace_info = 0;
6551
6552 start_symtab (name, comp_dir, lowpc);
6553 record_debugformat ("DWARF 2");
6554 record_producer (cu->producer);
6555
6556 /* Decode line number information if present. We do this before
6557 processing child DIEs, so that the line header table is available
6558 for DW_AT_decl_file. */
6559 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6560
6561 /* Process all dies in compilation unit. */
6562 if (die->child != NULL)
6563 {
6564 child_die = die->child;
6565 while (child_die && child_die->tag)
6566 {
6567 process_die (child_die, cu);
6568 child_die = sibling_die (child_die);
6569 }
6570 }
6571
6572 /* Decode macro information, if present. Dwarf 2 macro information
6573 refers to information in the line number info statement program
6574 header, so we can only read it if we've read the header
6575 successfully. */
6576 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6577 if (attr && cu->line_header)
6578 {
6579 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6580 complaint (&symfile_complaints,
6581 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6582
6583 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6584 comp_dir, abfd, cu,
6585 &dwarf2_per_objfile->macro, 1,
6586 ".debug_macro");
6587 }
6588 else
6589 {
6590 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6591 if (attr && cu->line_header)
6592 {
6593 unsigned int macro_offset = DW_UNSND (attr);
6594
6595 dwarf_decode_macros (cu->line_header, macro_offset,
6596 comp_dir, abfd, cu,
6597 &dwarf2_per_objfile->macinfo, 0,
6598 ".debug_macinfo");
6599 }
6600 }
6601
6602 do_cleanups (back_to);
6603 }
6604
6605 /* Process DW_TAG_type_unit.
6606 For TUs we want to skip the first top level sibling if it's not the
6607 actual type being defined by this TU. In this case the first top
6608 level sibling is there to provide context only. */
6609
6610 static void
6611 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6612 {
6613 struct objfile *objfile = cu->objfile;
6614 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6615 CORE_ADDR lowpc;
6616 struct attribute *attr;
6617 char *name = NULL;
6618 char *comp_dir = NULL;
6619 struct die_info *child_die;
6620 bfd *abfd = objfile->obfd;
6621
6622 /* start_symtab needs a low pc, but we don't really have one.
6623 Do what read_file_scope would do in the absence of such info. */
6624 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6625
6626 /* Find the filename. Do not use dwarf2_name here, since the filename
6627 is not a source language identifier. */
6628 attr = dwarf2_attr (die, DW_AT_name, cu);
6629 if (attr)
6630 name = DW_STRING (attr);
6631
6632 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6633 if (attr)
6634 comp_dir = DW_STRING (attr);
6635 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6636 {
6637 comp_dir = ldirname (name);
6638 if (comp_dir != NULL)
6639 make_cleanup (xfree, comp_dir);
6640 }
6641
6642 if (name == NULL)
6643 name = "<unknown>";
6644
6645 prepare_one_comp_unit (cu, die, language_minimal);
6646
6647 /* We assume that we're processing GCC output. */
6648 processing_gcc_compilation = 2;
6649
6650 processing_has_namespace_info = 0;
6651
6652 start_symtab (name, comp_dir, lowpc);
6653 record_debugformat ("DWARF 2");
6654 record_producer (cu->producer);
6655
6656 /* Decode line number information if present. We do this before
6657 processing child DIEs, so that the line header table is available
6658 for DW_AT_decl_file.
6659 We don't need the pc/line-number mapping for type units. */
6660 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6661
6662 /* Process the dies in the type unit. */
6663 if (die->child == NULL)
6664 {
6665 dump_die_for_error (die);
6666 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6667 bfd_get_filename (abfd));
6668 }
6669
6670 child_die = die->child;
6671
6672 while (child_die && child_die->tag)
6673 {
6674 process_die (child_die, cu);
6675
6676 child_die = sibling_die (child_die);
6677 }
6678
6679 do_cleanups (back_to);
6680 }
6681 \f
6682 /* DWO files. */
6683
6684 static hashval_t
6685 hash_dwo_file (const void *item)
6686 {
6687 const struct dwo_file *dwo_file = item;
6688
6689 return htab_hash_string (dwo_file->dwo_name);
6690 }
6691
6692 static int
6693 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6694 {
6695 const struct dwo_file *lhs = item_lhs;
6696 const struct dwo_file *rhs = item_rhs;
6697
6698 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6699 }
6700
6701 /* Allocate a hash table for DWO files. */
6702
6703 static htab_t
6704 allocate_dwo_file_hash_table (void)
6705 {
6706 struct objfile *objfile = dwarf2_per_objfile->objfile;
6707
6708 return htab_create_alloc_ex (41,
6709 hash_dwo_file,
6710 eq_dwo_file,
6711 NULL,
6712 &objfile->objfile_obstack,
6713 hashtab_obstack_allocate,
6714 dummy_obstack_deallocate);
6715 }
6716
6717 static hashval_t
6718 hash_dwo_unit (const void *item)
6719 {
6720 const struct dwo_unit *dwo_unit = item;
6721
6722 /* This drops the top 32 bits of the id, but is ok for a hash. */
6723 return dwo_unit->signature;
6724 }
6725
6726 static int
6727 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6728 {
6729 const struct dwo_unit *lhs = item_lhs;
6730 const struct dwo_unit *rhs = item_rhs;
6731
6732 /* The signature is assumed to be unique within the DWO file.
6733 So while object file CU dwo_id's always have the value zero,
6734 that's OK, assuming each object file DWO file has only one CU,
6735 and that's the rule for now. */
6736 return lhs->signature == rhs->signature;
6737 }
6738
6739 /* Allocate a hash table for DWO CUs,TUs.
6740 There is one of these tables for each of CUs,TUs for each DWO file. */
6741
6742 static htab_t
6743 allocate_dwo_unit_table (struct objfile *objfile)
6744 {
6745 /* Start out with a pretty small number.
6746 Generally DWO files contain only one CU and maybe some TUs. */
6747 return htab_create_alloc_ex (3,
6748 hash_dwo_unit,
6749 eq_dwo_unit,
6750 NULL,
6751 &objfile->objfile_obstack,
6752 hashtab_obstack_allocate,
6753 dummy_obstack_deallocate);
6754 }
6755
6756 /* This function is mapped across the sections and remembers the offset and
6757 size of each of the DWO debugging sections we are interested in. */
6758
6759 static void
6760 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6761 {
6762 struct dwo_file *dwo_file = dwo_file_ptr;
6763 const struct dwo_section_names *names = &dwo_section_names;
6764
6765 if (section_is_p (sectp->name, &names->abbrev_dwo))
6766 {
6767 dwo_file->sections.abbrev.asection = sectp;
6768 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6769 }
6770 else if (section_is_p (sectp->name, &names->info_dwo))
6771 {
6772 dwo_file->sections.info.asection = sectp;
6773 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6774 }
6775 else if (section_is_p (sectp->name, &names->line_dwo))
6776 {
6777 dwo_file->sections.line.asection = sectp;
6778 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6779 }
6780 else if (section_is_p (sectp->name, &names->loc_dwo))
6781 {
6782 dwo_file->sections.loc.asection = sectp;
6783 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6784 }
6785 else if (section_is_p (sectp->name, &names->str_dwo))
6786 {
6787 dwo_file->sections.str.asection = sectp;
6788 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6789 }
6790 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6791 {
6792 dwo_file->sections.str_offsets.asection = sectp;
6793 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6794 }
6795 else if (section_is_p (sectp->name, &names->types_dwo))
6796 {
6797 struct dwarf2_section_info type_section;
6798
6799 memset (&type_section, 0, sizeof (type_section));
6800 type_section.asection = sectp;
6801 type_section.size = bfd_get_section_size (sectp);
6802 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6803 &type_section);
6804 }
6805 }
6806
6807 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6808
6809 struct create_dwo_info_table_data
6810 {
6811 struct dwo_file *dwo_file;
6812 htab_t cu_htab;
6813 };
6814
6815 /* die_reader_func for create_debug_info_hash_table. */
6816
6817 static void
6818 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6819 gdb_byte *info_ptr,
6820 struct die_info *comp_unit_die,
6821 int has_children,
6822 void *datap)
6823 {
6824 struct dwarf2_cu *cu = reader->cu;
6825 struct objfile *objfile = dwarf2_per_objfile->objfile;
6826 sect_offset offset = cu->per_cu->offset;
6827 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6828 struct create_dwo_info_table_data *data = datap;
6829 struct dwo_file *dwo_file = data->dwo_file;
6830 htab_t cu_htab = data->cu_htab;
6831 void **slot;
6832 struct attribute *attr;
6833 struct dwo_unit *dwo_unit;
6834
6835 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6836 if (attr == NULL)
6837 {
6838 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6839 " its dwo_id [in module %s]"),
6840 offset.sect_off, dwo_file->dwo_name);
6841 return;
6842 }
6843
6844 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6845 dwo_unit->dwo_file = dwo_file;
6846 dwo_unit->signature = DW_UNSND (attr);
6847 dwo_unit->info_or_types_section = section;
6848 dwo_unit->offset = offset;
6849 dwo_unit->length = cu->per_cu->length;
6850
6851 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6852 gdb_assert (slot != NULL);
6853 if (*slot != NULL)
6854 {
6855 const struct dwo_unit *dup_dwo_unit = *slot;
6856
6857 complaint (&symfile_complaints,
6858 _("debug entry at offset 0x%x is duplicate to the entry at"
6859 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6860 offset.sect_off, dup_dwo_unit->offset.sect_off,
6861 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6862 dwo_file->dwo_name);
6863 }
6864 else
6865 *slot = dwo_unit;
6866
6867 if (dwarf2_die_debug)
6868 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6869 offset.sect_off,
6870 phex (dwo_unit->signature,
6871 sizeof (dwo_unit->signature)));
6872 }
6873
6874 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6875
6876 static htab_t
6877 create_debug_info_hash_table (struct dwo_file *dwo_file)
6878 {
6879 struct objfile *objfile = dwarf2_per_objfile->objfile;
6880 struct dwarf2_section_info *section = &dwo_file->sections.info;
6881 bfd *abfd;
6882 htab_t cu_htab;
6883 gdb_byte *info_ptr, *end_ptr;
6884 struct create_dwo_info_table_data create_dwo_info_table_data;
6885
6886 dwarf2_read_section (objfile, section);
6887 info_ptr = section->buffer;
6888
6889 if (info_ptr == NULL)
6890 return NULL;
6891
6892 /* We can't set abfd until now because the section may be empty or
6893 not present, in which case section->asection will be NULL. */
6894 abfd = section->asection->owner;
6895
6896 if (dwarf2_die_debug)
6897 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6898 bfd_get_filename (abfd));
6899
6900 cu_htab = allocate_dwo_unit_table (objfile);
6901
6902 create_dwo_info_table_data.dwo_file = dwo_file;
6903 create_dwo_info_table_data.cu_htab = cu_htab;
6904
6905 end_ptr = info_ptr + section->size;
6906 while (info_ptr < end_ptr)
6907 {
6908 struct dwarf2_per_cu_data per_cu;
6909
6910 memset (&per_cu, 0, sizeof (per_cu));
6911 per_cu.objfile = objfile;
6912 per_cu.is_debug_types = 0;
6913 per_cu.offset.sect_off = info_ptr - section->buffer;
6914 per_cu.info_or_types_section = section;
6915
6916 init_cutu_and_read_dies_no_follow (&per_cu,
6917 &dwo_file->sections.abbrev,
6918 dwo_file,
6919 create_debug_info_hash_table_reader,
6920 &create_dwo_info_table_data);
6921
6922 info_ptr += per_cu.length;
6923 }
6924
6925 return cu_htab;
6926 }
6927
6928 /* Subroutine of open_dwo_file to simplify it.
6929 Open the file specified by FILE_NAME and hand it off to BFD for
6930 preliminary analysis. Return a newly initialized bfd *, which
6931 includes a canonicalized copy of FILE_NAME.
6932 In case of trouble, return NULL.
6933 NOTE: This function is derived from symfile_bfd_open. */
6934
6935 static bfd *
6936 try_open_dwo_file (const char *file_name)
6937 {
6938 bfd *sym_bfd;
6939 int desc;
6940 char *absolute_name;
6941
6942 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6943 O_RDONLY | O_BINARY, &absolute_name);
6944 if (desc < 0)
6945 return NULL;
6946
6947 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6948 if (!sym_bfd)
6949 {
6950 close (desc);
6951 xfree (absolute_name);
6952 return NULL;
6953 }
6954 bfd_set_cacheable (sym_bfd, 1);
6955
6956 if (!bfd_check_format (sym_bfd, bfd_object))
6957 {
6958 bfd_close (sym_bfd); /* This also closes desc. */
6959 xfree (absolute_name);
6960 return NULL;
6961 }
6962
6963 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6964 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6965
6966 return sym_bfd;
6967 }
6968
6969 /* Try to open DWO file DWO_NAME.
6970 COMP_DIR is the DW_AT_comp_dir attribute.
6971 The result is the bfd handle of the file.
6972 If there is a problem finding or opening the file, return NULL.
6973 Upon success, the canonicalized path of the file is stored in the bfd,
6974 same as symfile_bfd_open. */
6975
6976 static bfd *
6977 open_dwo_file (const char *dwo_name, const char *comp_dir)
6978 {
6979 bfd *abfd;
6980
6981 if (IS_ABSOLUTE_PATH (dwo_name))
6982 return try_open_dwo_file (dwo_name);
6983
6984 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6985
6986 if (comp_dir != NULL)
6987 {
6988 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6989
6990 /* NOTE: If comp_dir is a relative path, this will also try the
6991 search path, which seems useful. */
6992 abfd = try_open_dwo_file (path_to_try);
6993 xfree (path_to_try);
6994 if (abfd != NULL)
6995 return abfd;
6996 }
6997
6998 /* That didn't work, try debug-file-directory, which, despite its name,
6999 is a list of paths. */
7000
7001 if (*debug_file_directory == '\0')
7002 return NULL;
7003
7004 return try_open_dwo_file (dwo_name);
7005 }
7006
7007 /* Initialize the use of the DWO file specified by DWO_NAME. */
7008
7009 static struct dwo_file *
7010 init_dwo_file (const char *dwo_name, const char *comp_dir)
7011 {
7012 struct objfile *objfile = dwarf2_per_objfile->objfile;
7013 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7014 struct dwo_file);
7015 bfd *abfd;
7016 struct cleanup *cleanups;
7017
7018 if (dwarf2_die_debug)
7019 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7020
7021 abfd = open_dwo_file (dwo_name, comp_dir);
7022 if (abfd == NULL)
7023 return NULL;
7024 dwo_file->dwo_name = dwo_name;
7025 dwo_file->dwo_bfd = abfd;
7026
7027 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7028
7029 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7030
7031 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7032
7033 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7034 dwo_file->sections.types);
7035
7036 discard_cleanups (cleanups);
7037
7038 return dwo_file;
7039 }
7040
7041 /* Lookup DWO file DWO_NAME. */
7042
7043 static struct dwo_file *
7044 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7045 {
7046 struct dwo_file *dwo_file;
7047 struct dwo_file find_entry;
7048 void **slot;
7049
7050 if (dwarf2_per_objfile->dwo_files == NULL)
7051 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7052
7053 /* Have we already seen this DWO file? */
7054 find_entry.dwo_name = dwo_name;
7055 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7056
7057 /* If not, read it in and build a table of the DWOs it contains. */
7058 if (*slot == NULL)
7059 *slot = init_dwo_file (dwo_name, comp_dir);
7060
7061 /* NOTE: This will be NULL if unable to open the file. */
7062 dwo_file = *slot;
7063
7064 return dwo_file;
7065 }
7066
7067 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7068 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7069 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7070 nomenclature as TUs).
7071 The result is the DWO CU or NULL if we didn't find it
7072 (dwo_id mismatch or couldn't find the DWO file). */
7073
7074 static struct dwo_unit *
7075 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7076 char *dwo_name, const char *comp_dir,
7077 ULONGEST signature)
7078 {
7079 struct objfile *objfile = dwarf2_per_objfile->objfile;
7080 struct dwo_file *dwo_file;
7081
7082 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7083 if (dwo_file == NULL)
7084 return NULL;
7085
7086 /* Look up the DWO using its signature(dwo_id). */
7087
7088 if (dwo_file->cus != NULL)
7089 {
7090 struct dwo_unit find_dwo_cu, *dwo_cu;
7091
7092 find_dwo_cu.signature = signature;
7093 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7094
7095 if (dwo_cu != NULL)
7096 return dwo_cu;
7097 }
7098
7099 /* We didn't find it. This must mean a dwo_id mismatch. */
7100
7101 complaint (&symfile_complaints,
7102 _("Could not find DWO CU referenced by CU at offset 0x%x"
7103 " [in module %s]"),
7104 this_cu->offset.sect_off, objfile->name);
7105 return NULL;
7106 }
7107
7108 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7109 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7110 The result is the DWO CU or NULL if we didn't find it
7111 (dwo_id mismatch or couldn't find the DWO file). */
7112
7113 static struct dwo_unit *
7114 lookup_dwo_type_unit (struct signatured_type *this_tu,
7115 char *dwo_name, const char *comp_dir)
7116 {
7117 struct objfile *objfile = dwarf2_per_objfile->objfile;
7118 struct dwo_file *dwo_file;
7119
7120 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7121 if (dwo_file == NULL)
7122 return NULL;
7123
7124 /* Look up the DWO using its signature(dwo_id). */
7125
7126 if (dwo_file->tus != NULL)
7127 {
7128 struct dwo_unit find_dwo_tu, *dwo_tu;
7129
7130 find_dwo_tu.signature = this_tu->signature;
7131 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7132
7133 if (dwo_tu != NULL)
7134 return dwo_tu;
7135 }
7136
7137 /* We didn't find it. This must mean a dwo_id mismatch. */
7138
7139 complaint (&symfile_complaints,
7140 _("Could not find DWO TU referenced by TU at offset 0x%x"
7141 " [in module %s]"),
7142 this_tu->per_cu.offset.sect_off, objfile->name);
7143 return NULL;
7144 }
7145
7146 /* Free all resources associated with DWO_FILE.
7147 Close the DWO file and munmap the sections.
7148 All memory should be on the objfile obstack. */
7149
7150 static void
7151 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7152 {
7153 int ix;
7154 struct dwarf2_section_info *section;
7155
7156 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7157 bfd_close (dwo_file->dwo_bfd);
7158
7159 munmap_section_buffer (&dwo_file->sections.abbrev);
7160 munmap_section_buffer (&dwo_file->sections.info);
7161 munmap_section_buffer (&dwo_file->sections.line);
7162 munmap_section_buffer (&dwo_file->sections.loc);
7163 munmap_section_buffer (&dwo_file->sections.str);
7164 munmap_section_buffer (&dwo_file->sections.str_offsets);
7165
7166 for (ix = 0;
7167 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7168 ix, section);
7169 ++ix)
7170 munmap_section_buffer (section);
7171
7172 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7173 }
7174
7175 /* Wrapper for free_dwo_file for use in cleanups. */
7176
7177 static void
7178 free_dwo_file_cleanup (void *arg)
7179 {
7180 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7181 struct objfile *objfile = dwarf2_per_objfile->objfile;
7182
7183 free_dwo_file (dwo_file, objfile);
7184 }
7185
7186 /* Traversal function for free_dwo_files. */
7187
7188 static int
7189 free_dwo_file_from_slot (void **slot, void *info)
7190 {
7191 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7192 struct objfile *objfile = (struct objfile *) info;
7193
7194 free_dwo_file (dwo_file, objfile);
7195
7196 return 1;
7197 }
7198
7199 /* Free all resources associated with DWO_FILES. */
7200
7201 static void
7202 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7203 {
7204 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7205 }
7206 \f
7207 /* Read in various DIEs. */
7208
7209 /* qsort helper for inherit_abstract_dies. */
7210
7211 static int
7212 unsigned_int_compar (const void *ap, const void *bp)
7213 {
7214 unsigned int a = *(unsigned int *) ap;
7215 unsigned int b = *(unsigned int *) bp;
7216
7217 return (a > b) - (b > a);
7218 }
7219
7220 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7221 Inherit only the children of the DW_AT_abstract_origin DIE not being
7222 already referenced by DW_AT_abstract_origin from the children of the
7223 current DIE. */
7224
7225 static void
7226 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7227 {
7228 struct die_info *child_die;
7229 unsigned die_children_count;
7230 /* CU offsets which were referenced by children of the current DIE. */
7231 sect_offset *offsets;
7232 sect_offset *offsets_end, *offsetp;
7233 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7234 struct die_info *origin_die;
7235 /* Iterator of the ORIGIN_DIE children. */
7236 struct die_info *origin_child_die;
7237 struct cleanup *cleanups;
7238 struct attribute *attr;
7239 struct dwarf2_cu *origin_cu;
7240 struct pending **origin_previous_list_in_scope;
7241
7242 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7243 if (!attr)
7244 return;
7245
7246 /* Note that following die references may follow to a die in a
7247 different cu. */
7248
7249 origin_cu = cu;
7250 origin_die = follow_die_ref (die, attr, &origin_cu);
7251
7252 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7253 symbols in. */
7254 origin_previous_list_in_scope = origin_cu->list_in_scope;
7255 origin_cu->list_in_scope = cu->list_in_scope;
7256
7257 if (die->tag != origin_die->tag
7258 && !(die->tag == DW_TAG_inlined_subroutine
7259 && origin_die->tag == DW_TAG_subprogram))
7260 complaint (&symfile_complaints,
7261 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7262 die->offset.sect_off, origin_die->offset.sect_off);
7263
7264 child_die = die->child;
7265 die_children_count = 0;
7266 while (child_die && child_die->tag)
7267 {
7268 child_die = sibling_die (child_die);
7269 die_children_count++;
7270 }
7271 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7272 cleanups = make_cleanup (xfree, offsets);
7273
7274 offsets_end = offsets;
7275 child_die = die->child;
7276 while (child_die && child_die->tag)
7277 {
7278 /* For each CHILD_DIE, find the corresponding child of
7279 ORIGIN_DIE. If there is more than one layer of
7280 DW_AT_abstract_origin, follow them all; there shouldn't be,
7281 but GCC versions at least through 4.4 generate this (GCC PR
7282 40573). */
7283 struct die_info *child_origin_die = child_die;
7284 struct dwarf2_cu *child_origin_cu = cu;
7285
7286 while (1)
7287 {
7288 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7289 child_origin_cu);
7290 if (attr == NULL)
7291 break;
7292 child_origin_die = follow_die_ref (child_origin_die, attr,
7293 &child_origin_cu);
7294 }
7295
7296 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7297 counterpart may exist. */
7298 if (child_origin_die != child_die)
7299 {
7300 if (child_die->tag != child_origin_die->tag
7301 && !(child_die->tag == DW_TAG_inlined_subroutine
7302 && child_origin_die->tag == DW_TAG_subprogram))
7303 complaint (&symfile_complaints,
7304 _("Child DIE 0x%x and its abstract origin 0x%x have "
7305 "different tags"), child_die->offset.sect_off,
7306 child_origin_die->offset.sect_off);
7307 if (child_origin_die->parent != origin_die)
7308 complaint (&symfile_complaints,
7309 _("Child DIE 0x%x and its abstract origin 0x%x have "
7310 "different parents"), child_die->offset.sect_off,
7311 child_origin_die->offset.sect_off);
7312 else
7313 *offsets_end++ = child_origin_die->offset;
7314 }
7315 child_die = sibling_die (child_die);
7316 }
7317 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7318 unsigned_int_compar);
7319 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7320 if (offsetp[-1].sect_off == offsetp->sect_off)
7321 complaint (&symfile_complaints,
7322 _("Multiple children of DIE 0x%x refer "
7323 "to DIE 0x%x as their abstract origin"),
7324 die->offset.sect_off, offsetp->sect_off);
7325
7326 offsetp = offsets;
7327 origin_child_die = origin_die->child;
7328 while (origin_child_die && origin_child_die->tag)
7329 {
7330 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7331 while (offsetp < offsets_end
7332 && offsetp->sect_off < origin_child_die->offset.sect_off)
7333 offsetp++;
7334 if (offsetp >= offsets_end
7335 || offsetp->sect_off > origin_child_die->offset.sect_off)
7336 {
7337 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7338 process_die (origin_child_die, origin_cu);
7339 }
7340 origin_child_die = sibling_die (origin_child_die);
7341 }
7342 origin_cu->list_in_scope = origin_previous_list_in_scope;
7343
7344 do_cleanups (cleanups);
7345 }
7346
7347 static void
7348 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7349 {
7350 struct objfile *objfile = cu->objfile;
7351 struct context_stack *new;
7352 CORE_ADDR lowpc;
7353 CORE_ADDR highpc;
7354 struct die_info *child_die;
7355 struct attribute *attr, *call_line, *call_file;
7356 char *name;
7357 CORE_ADDR baseaddr;
7358 struct block *block;
7359 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7360 VEC (symbolp) *template_args = NULL;
7361 struct template_symbol *templ_func = NULL;
7362
7363 if (inlined_func)
7364 {
7365 /* If we do not have call site information, we can't show the
7366 caller of this inlined function. That's too confusing, so
7367 only use the scope for local variables. */
7368 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7369 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7370 if (call_line == NULL || call_file == NULL)
7371 {
7372 read_lexical_block_scope (die, cu);
7373 return;
7374 }
7375 }
7376
7377 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7378
7379 name = dwarf2_name (die, cu);
7380
7381 /* Ignore functions with missing or empty names. These are actually
7382 illegal according to the DWARF standard. */
7383 if (name == NULL)
7384 {
7385 complaint (&symfile_complaints,
7386 _("missing name for subprogram DIE at %d"),
7387 die->offset.sect_off);
7388 return;
7389 }
7390
7391 /* Ignore functions with missing or invalid low and high pc attributes. */
7392 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7393 {
7394 attr = dwarf2_attr (die, DW_AT_external, cu);
7395 if (!attr || !DW_UNSND (attr))
7396 complaint (&symfile_complaints,
7397 _("cannot get low and high bounds "
7398 "for subprogram DIE at %d"),
7399 die->offset.sect_off);
7400 return;
7401 }
7402
7403 lowpc += baseaddr;
7404 highpc += baseaddr;
7405
7406 /* If we have any template arguments, then we must allocate a
7407 different sort of symbol. */
7408 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7409 {
7410 if (child_die->tag == DW_TAG_template_type_param
7411 || child_die->tag == DW_TAG_template_value_param)
7412 {
7413 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7414 struct template_symbol);
7415 templ_func->base.is_cplus_template_function = 1;
7416 break;
7417 }
7418 }
7419
7420 new = push_context (0, lowpc);
7421 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7422 (struct symbol *) templ_func);
7423
7424 /* If there is a location expression for DW_AT_frame_base, record
7425 it. */
7426 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7427 if (attr)
7428 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7429 expression is being recorded directly in the function's symbol
7430 and not in a separate frame-base object. I guess this hack is
7431 to avoid adding some sort of frame-base adjunct/annex to the
7432 function's symbol :-(. The problem with doing this is that it
7433 results in a function symbol with a location expression that
7434 has nothing to do with the location of the function, ouch! The
7435 relationship should be: a function's symbol has-a frame base; a
7436 frame-base has-a location expression. */
7437 dwarf2_symbol_mark_computed (attr, new->name, cu);
7438
7439 cu->list_in_scope = &local_symbols;
7440
7441 if (die->child != NULL)
7442 {
7443 child_die = die->child;
7444 while (child_die && child_die->tag)
7445 {
7446 if (child_die->tag == DW_TAG_template_type_param
7447 || child_die->tag == DW_TAG_template_value_param)
7448 {
7449 struct symbol *arg = new_symbol (child_die, NULL, cu);
7450
7451 if (arg != NULL)
7452 VEC_safe_push (symbolp, template_args, arg);
7453 }
7454 else
7455 process_die (child_die, cu);
7456 child_die = sibling_die (child_die);
7457 }
7458 }
7459
7460 inherit_abstract_dies (die, cu);
7461
7462 /* If we have a DW_AT_specification, we might need to import using
7463 directives from the context of the specification DIE. See the
7464 comment in determine_prefix. */
7465 if (cu->language == language_cplus
7466 && dwarf2_attr (die, DW_AT_specification, cu))
7467 {
7468 struct dwarf2_cu *spec_cu = cu;
7469 struct die_info *spec_die = die_specification (die, &spec_cu);
7470
7471 while (spec_die)
7472 {
7473 child_die = spec_die->child;
7474 while (child_die && child_die->tag)
7475 {
7476 if (child_die->tag == DW_TAG_imported_module)
7477 process_die (child_die, spec_cu);
7478 child_die = sibling_die (child_die);
7479 }
7480
7481 /* In some cases, GCC generates specification DIEs that
7482 themselves contain DW_AT_specification attributes. */
7483 spec_die = die_specification (spec_die, &spec_cu);
7484 }
7485 }
7486
7487 new = pop_context ();
7488 /* Make a block for the local symbols within. */
7489 block = finish_block (new->name, &local_symbols, new->old_blocks,
7490 lowpc, highpc, objfile);
7491
7492 /* For C++, set the block's scope. */
7493 if (cu->language == language_cplus || cu->language == language_fortran)
7494 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7495 determine_prefix (die, cu),
7496 processing_has_namespace_info);
7497
7498 /* If we have address ranges, record them. */
7499 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7500
7501 /* Attach template arguments to function. */
7502 if (! VEC_empty (symbolp, template_args))
7503 {
7504 gdb_assert (templ_func != NULL);
7505
7506 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7507 templ_func->template_arguments
7508 = obstack_alloc (&objfile->objfile_obstack,
7509 (templ_func->n_template_arguments
7510 * sizeof (struct symbol *)));
7511 memcpy (templ_func->template_arguments,
7512 VEC_address (symbolp, template_args),
7513 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7514 VEC_free (symbolp, template_args);
7515 }
7516
7517 /* In C++, we can have functions nested inside functions (e.g., when
7518 a function declares a class that has methods). This means that
7519 when we finish processing a function scope, we may need to go
7520 back to building a containing block's symbol lists. */
7521 local_symbols = new->locals;
7522 param_symbols = new->params;
7523 using_directives = new->using_directives;
7524
7525 /* If we've finished processing a top-level function, subsequent
7526 symbols go in the file symbol list. */
7527 if (outermost_context_p ())
7528 cu->list_in_scope = &file_symbols;
7529 }
7530
7531 /* Process all the DIES contained within a lexical block scope. Start
7532 a new scope, process the dies, and then close the scope. */
7533
7534 static void
7535 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7536 {
7537 struct objfile *objfile = cu->objfile;
7538 struct context_stack *new;
7539 CORE_ADDR lowpc, highpc;
7540 struct die_info *child_die;
7541 CORE_ADDR baseaddr;
7542
7543 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7544
7545 /* Ignore blocks with missing or invalid low and high pc attributes. */
7546 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7547 as multiple lexical blocks? Handling children in a sane way would
7548 be nasty. Might be easier to properly extend generic blocks to
7549 describe ranges. */
7550 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7551 return;
7552 lowpc += baseaddr;
7553 highpc += baseaddr;
7554
7555 push_context (0, lowpc);
7556 if (die->child != NULL)
7557 {
7558 child_die = die->child;
7559 while (child_die && child_die->tag)
7560 {
7561 process_die (child_die, cu);
7562 child_die = sibling_die (child_die);
7563 }
7564 }
7565 new = pop_context ();
7566
7567 if (local_symbols != NULL || using_directives != NULL)
7568 {
7569 struct block *block
7570 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7571 highpc, objfile);
7572
7573 /* Note that recording ranges after traversing children, as we
7574 do here, means that recording a parent's ranges entails
7575 walking across all its children's ranges as they appear in
7576 the address map, which is quadratic behavior.
7577
7578 It would be nicer to record the parent's ranges before
7579 traversing its children, simply overriding whatever you find
7580 there. But since we don't even decide whether to create a
7581 block until after we've traversed its children, that's hard
7582 to do. */
7583 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7584 }
7585 local_symbols = new->locals;
7586 using_directives = new->using_directives;
7587 }
7588
7589 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7590
7591 static void
7592 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7593 {
7594 struct objfile *objfile = cu->objfile;
7595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7596 CORE_ADDR pc, baseaddr;
7597 struct attribute *attr;
7598 struct call_site *call_site, call_site_local;
7599 void **slot;
7600 int nparams;
7601 struct die_info *child_die;
7602
7603 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7604
7605 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7606 if (!attr)
7607 {
7608 complaint (&symfile_complaints,
7609 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7610 "DIE 0x%x [in module %s]"),
7611 die->offset.sect_off, objfile->name);
7612 return;
7613 }
7614 pc = DW_ADDR (attr) + baseaddr;
7615
7616 if (cu->call_site_htab == NULL)
7617 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7618 NULL, &objfile->objfile_obstack,
7619 hashtab_obstack_allocate, NULL);
7620 call_site_local.pc = pc;
7621 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7622 if (*slot != NULL)
7623 {
7624 complaint (&symfile_complaints,
7625 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7626 "DIE 0x%x [in module %s]"),
7627 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7628 return;
7629 }
7630
7631 /* Count parameters at the caller. */
7632
7633 nparams = 0;
7634 for (child_die = die->child; child_die && child_die->tag;
7635 child_die = sibling_die (child_die))
7636 {
7637 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7638 {
7639 complaint (&symfile_complaints,
7640 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7641 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7642 child_die->tag, child_die->offset.sect_off, objfile->name);
7643 continue;
7644 }
7645
7646 nparams++;
7647 }
7648
7649 call_site = obstack_alloc (&objfile->objfile_obstack,
7650 (sizeof (*call_site)
7651 + (sizeof (*call_site->parameter)
7652 * (nparams - 1))));
7653 *slot = call_site;
7654 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7655 call_site->pc = pc;
7656
7657 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7658 {
7659 struct die_info *func_die;
7660
7661 /* Skip also over DW_TAG_inlined_subroutine. */
7662 for (func_die = die->parent;
7663 func_die && func_die->tag != DW_TAG_subprogram
7664 && func_die->tag != DW_TAG_subroutine_type;
7665 func_die = func_die->parent);
7666
7667 /* DW_AT_GNU_all_call_sites is a superset
7668 of DW_AT_GNU_all_tail_call_sites. */
7669 if (func_die
7670 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7671 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7672 {
7673 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7674 not complete. But keep CALL_SITE for look ups via call_site_htab,
7675 both the initial caller containing the real return address PC and
7676 the final callee containing the current PC of a chain of tail
7677 calls do not need to have the tail call list complete. But any
7678 function candidate for a virtual tail call frame searched via
7679 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7680 determined unambiguously. */
7681 }
7682 else
7683 {
7684 struct type *func_type = NULL;
7685
7686 if (func_die)
7687 func_type = get_die_type (func_die, cu);
7688 if (func_type != NULL)
7689 {
7690 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7691
7692 /* Enlist this call site to the function. */
7693 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7694 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7695 }
7696 else
7697 complaint (&symfile_complaints,
7698 _("Cannot find function owning DW_TAG_GNU_call_site "
7699 "DIE 0x%x [in module %s]"),
7700 die->offset.sect_off, objfile->name);
7701 }
7702 }
7703
7704 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7705 if (attr == NULL)
7706 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7707 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7708 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7709 /* Keep NULL DWARF_BLOCK. */;
7710 else if (attr_form_is_block (attr))
7711 {
7712 struct dwarf2_locexpr_baton *dlbaton;
7713
7714 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7715 dlbaton->data = DW_BLOCK (attr)->data;
7716 dlbaton->size = DW_BLOCK (attr)->size;
7717 dlbaton->per_cu = cu->per_cu;
7718
7719 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7720 }
7721 else if (is_ref_attr (attr))
7722 {
7723 struct dwarf2_cu *target_cu = cu;
7724 struct die_info *target_die;
7725
7726 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7727 gdb_assert (target_cu->objfile == objfile);
7728 if (die_is_declaration (target_die, target_cu))
7729 {
7730 const char *target_physname;
7731
7732 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7733 if (target_physname == NULL)
7734 complaint (&symfile_complaints,
7735 _("DW_AT_GNU_call_site_target target DIE has invalid "
7736 "physname, for referencing DIE 0x%x [in module %s]"),
7737 die->offset.sect_off, objfile->name);
7738 else
7739 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7740 }
7741 else
7742 {
7743 CORE_ADDR lowpc;
7744
7745 /* DW_AT_entry_pc should be preferred. */
7746 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7747 complaint (&symfile_complaints,
7748 _("DW_AT_GNU_call_site_target target DIE has invalid "
7749 "low pc, for referencing DIE 0x%x [in module %s]"),
7750 die->offset.sect_off, objfile->name);
7751 else
7752 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7753 }
7754 }
7755 else
7756 complaint (&symfile_complaints,
7757 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7758 "block nor reference, for DIE 0x%x [in module %s]"),
7759 die->offset.sect_off, objfile->name);
7760
7761 call_site->per_cu = cu->per_cu;
7762
7763 for (child_die = die->child;
7764 child_die && child_die->tag;
7765 child_die = sibling_die (child_die))
7766 {
7767 struct call_site_parameter *parameter;
7768
7769 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7770 {
7771 /* Already printed the complaint above. */
7772 continue;
7773 }
7774
7775 gdb_assert (call_site->parameter_count < nparams);
7776 parameter = &call_site->parameter[call_site->parameter_count];
7777
7778 /* DW_AT_location specifies the register number. Value of the data
7779 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7780
7781 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7782 if (!attr || !attr_form_is_block (attr))
7783 {
7784 complaint (&symfile_complaints,
7785 _("No DW_FORM_block* DW_AT_location for "
7786 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7787 child_die->offset.sect_off, objfile->name);
7788 continue;
7789 }
7790 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7791 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7792 if (parameter->dwarf_reg == -1
7793 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7794 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7795 &parameter->fb_offset))
7796 {
7797 complaint (&symfile_complaints,
7798 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7799 "for DW_FORM_block* DW_AT_location for "
7800 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7801 child_die->offset.sect_off, objfile->name);
7802 continue;
7803 }
7804
7805 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7806 if (!attr_form_is_block (attr))
7807 {
7808 complaint (&symfile_complaints,
7809 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7810 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7811 child_die->offset.sect_off, objfile->name);
7812 continue;
7813 }
7814 parameter->value = DW_BLOCK (attr)->data;
7815 parameter->value_size = DW_BLOCK (attr)->size;
7816
7817 /* Parameters are not pre-cleared by memset above. */
7818 parameter->data_value = NULL;
7819 parameter->data_value_size = 0;
7820 call_site->parameter_count++;
7821
7822 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7823 if (attr)
7824 {
7825 if (!attr_form_is_block (attr))
7826 complaint (&symfile_complaints,
7827 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7828 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7829 child_die->offset.sect_off, objfile->name);
7830 else
7831 {
7832 parameter->data_value = DW_BLOCK (attr)->data;
7833 parameter->data_value_size = DW_BLOCK (attr)->size;
7834 }
7835 }
7836 }
7837 }
7838
7839 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7840 Return 1 if the attributes are present and valid, otherwise, return 0.
7841 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7842
7843 static int
7844 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7845 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7846 struct partial_symtab *ranges_pst)
7847 {
7848 struct objfile *objfile = cu->objfile;
7849 struct comp_unit_head *cu_header = &cu->header;
7850 bfd *obfd = objfile->obfd;
7851 unsigned int addr_size = cu_header->addr_size;
7852 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7853 /* Base address selection entry. */
7854 CORE_ADDR base;
7855 int found_base;
7856 unsigned int dummy;
7857 gdb_byte *buffer;
7858 CORE_ADDR marker;
7859 int low_set;
7860 CORE_ADDR low = 0;
7861 CORE_ADDR high = 0;
7862 CORE_ADDR baseaddr;
7863
7864 found_base = cu->base_known;
7865 base = cu->base_address;
7866
7867 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7868 if (offset >= dwarf2_per_objfile->ranges.size)
7869 {
7870 complaint (&symfile_complaints,
7871 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7872 offset);
7873 return 0;
7874 }
7875 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7876
7877 /* Read in the largest possible address. */
7878 marker = read_address (obfd, buffer, cu, &dummy);
7879 if ((marker & mask) == mask)
7880 {
7881 /* If we found the largest possible address, then
7882 read the base address. */
7883 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7884 buffer += 2 * addr_size;
7885 offset += 2 * addr_size;
7886 found_base = 1;
7887 }
7888
7889 low_set = 0;
7890
7891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7892
7893 while (1)
7894 {
7895 CORE_ADDR range_beginning, range_end;
7896
7897 range_beginning = read_address (obfd, buffer, cu, &dummy);
7898 buffer += addr_size;
7899 range_end = read_address (obfd, buffer, cu, &dummy);
7900 buffer += addr_size;
7901 offset += 2 * addr_size;
7902
7903 /* An end of list marker is a pair of zero addresses. */
7904 if (range_beginning == 0 && range_end == 0)
7905 /* Found the end of list entry. */
7906 break;
7907
7908 /* Each base address selection entry is a pair of 2 values.
7909 The first is the largest possible address, the second is
7910 the base address. Check for a base address here. */
7911 if ((range_beginning & mask) == mask)
7912 {
7913 /* If we found the largest possible address, then
7914 read the base address. */
7915 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7916 found_base = 1;
7917 continue;
7918 }
7919
7920 if (!found_base)
7921 {
7922 /* We have no valid base address for the ranges
7923 data. */
7924 complaint (&symfile_complaints,
7925 _("Invalid .debug_ranges data (no base address)"));
7926 return 0;
7927 }
7928
7929 if (range_beginning > range_end)
7930 {
7931 /* Inverted range entries are invalid. */
7932 complaint (&symfile_complaints,
7933 _("Invalid .debug_ranges data (inverted range)"));
7934 return 0;
7935 }
7936
7937 /* Empty range entries have no effect. */
7938 if (range_beginning == range_end)
7939 continue;
7940
7941 range_beginning += base;
7942 range_end += base;
7943
7944 if (ranges_pst != NULL)
7945 addrmap_set_empty (objfile->psymtabs_addrmap,
7946 range_beginning + baseaddr,
7947 range_end - 1 + baseaddr,
7948 ranges_pst);
7949
7950 /* FIXME: This is recording everything as a low-high
7951 segment of consecutive addresses. We should have a
7952 data structure for discontiguous block ranges
7953 instead. */
7954 if (! low_set)
7955 {
7956 low = range_beginning;
7957 high = range_end;
7958 low_set = 1;
7959 }
7960 else
7961 {
7962 if (range_beginning < low)
7963 low = range_beginning;
7964 if (range_end > high)
7965 high = range_end;
7966 }
7967 }
7968
7969 if (! low_set)
7970 /* If the first entry is an end-of-list marker, the range
7971 describes an empty scope, i.e. no instructions. */
7972 return 0;
7973
7974 if (low_return)
7975 *low_return = low;
7976 if (high_return)
7977 *high_return = high;
7978 return 1;
7979 }
7980
7981 /* Get low and high pc attributes from a die. Return 1 if the attributes
7982 are present and valid, otherwise, return 0. Return -1 if the range is
7983 discontinuous, i.e. derived from DW_AT_ranges information. */
7984
7985 static int
7986 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7987 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7988 struct partial_symtab *pst)
7989 {
7990 struct attribute *attr;
7991 struct attribute *attr_high;
7992 CORE_ADDR low = 0;
7993 CORE_ADDR high = 0;
7994 int ret = 0;
7995
7996 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7997 if (attr_high)
7998 {
7999 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8000 if (attr)
8001 {
8002 low = DW_ADDR (attr);
8003 if (attr_high->form == DW_FORM_addr
8004 || attr_high->form == DW_FORM_GNU_addr_index)
8005 high = DW_ADDR (attr_high);
8006 else
8007 high = low + DW_UNSND (attr_high);
8008 }
8009 else
8010 /* Found high w/o low attribute. */
8011 return 0;
8012
8013 /* Found consecutive range of addresses. */
8014 ret = 1;
8015 }
8016 else
8017 {
8018 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8019 if (attr != NULL)
8020 {
8021 /* Value of the DW_AT_ranges attribute is the offset in the
8022 .debug_ranges section. */
8023 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8024 return 0;
8025 /* Found discontinuous range of addresses. */
8026 ret = -1;
8027 }
8028 }
8029
8030 /* read_partial_die has also the strict LOW < HIGH requirement. */
8031 if (high <= low)
8032 return 0;
8033
8034 /* When using the GNU linker, .gnu.linkonce. sections are used to
8035 eliminate duplicate copies of functions and vtables and such.
8036 The linker will arbitrarily choose one and discard the others.
8037 The AT_*_pc values for such functions refer to local labels in
8038 these sections. If the section from that file was discarded, the
8039 labels are not in the output, so the relocs get a value of 0.
8040 If this is a discarded function, mark the pc bounds as invalid,
8041 so that GDB will ignore it. */
8042 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8043 return 0;
8044
8045 *lowpc = low;
8046 if (highpc)
8047 *highpc = high;
8048 return ret;
8049 }
8050
8051 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8052 its low and high PC addresses. Do nothing if these addresses could not
8053 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8054 and HIGHPC to the high address if greater than HIGHPC. */
8055
8056 static void
8057 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8058 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8059 struct dwarf2_cu *cu)
8060 {
8061 CORE_ADDR low, high;
8062 struct die_info *child = die->child;
8063
8064 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8065 {
8066 *lowpc = min (*lowpc, low);
8067 *highpc = max (*highpc, high);
8068 }
8069
8070 /* If the language does not allow nested subprograms (either inside
8071 subprograms or lexical blocks), we're done. */
8072 if (cu->language != language_ada)
8073 return;
8074
8075 /* Check all the children of the given DIE. If it contains nested
8076 subprograms, then check their pc bounds. Likewise, we need to
8077 check lexical blocks as well, as they may also contain subprogram
8078 definitions. */
8079 while (child && child->tag)
8080 {
8081 if (child->tag == DW_TAG_subprogram
8082 || child->tag == DW_TAG_lexical_block)
8083 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8084 child = sibling_die (child);
8085 }
8086 }
8087
8088 /* Get the low and high pc's represented by the scope DIE, and store
8089 them in *LOWPC and *HIGHPC. If the correct values can't be
8090 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8091
8092 static void
8093 get_scope_pc_bounds (struct die_info *die,
8094 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8095 struct dwarf2_cu *cu)
8096 {
8097 CORE_ADDR best_low = (CORE_ADDR) -1;
8098 CORE_ADDR best_high = (CORE_ADDR) 0;
8099 CORE_ADDR current_low, current_high;
8100
8101 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8102 {
8103 best_low = current_low;
8104 best_high = current_high;
8105 }
8106 else
8107 {
8108 struct die_info *child = die->child;
8109
8110 while (child && child->tag)
8111 {
8112 switch (child->tag) {
8113 case DW_TAG_subprogram:
8114 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8115 break;
8116 case DW_TAG_namespace:
8117 case DW_TAG_module:
8118 /* FIXME: carlton/2004-01-16: Should we do this for
8119 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8120 that current GCC's always emit the DIEs corresponding
8121 to definitions of methods of classes as children of a
8122 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8123 the DIEs giving the declarations, which could be
8124 anywhere). But I don't see any reason why the
8125 standards says that they have to be there. */
8126 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8127
8128 if (current_low != ((CORE_ADDR) -1))
8129 {
8130 best_low = min (best_low, current_low);
8131 best_high = max (best_high, current_high);
8132 }
8133 break;
8134 default:
8135 /* Ignore. */
8136 break;
8137 }
8138
8139 child = sibling_die (child);
8140 }
8141 }
8142
8143 *lowpc = best_low;
8144 *highpc = best_high;
8145 }
8146
8147 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8148 in DIE. */
8149
8150 static void
8151 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8152 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8153 {
8154 struct objfile *objfile = cu->objfile;
8155 struct attribute *attr;
8156 struct attribute *attr_high;
8157
8158 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8159 if (attr_high)
8160 {
8161 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8162 if (attr)
8163 {
8164 CORE_ADDR low = DW_ADDR (attr);
8165 CORE_ADDR high;
8166 if (attr_high->form == DW_FORM_addr
8167 || attr_high->form == DW_FORM_GNU_addr_index)
8168 high = DW_ADDR (attr_high);
8169 else
8170 high = low + DW_UNSND (attr_high);
8171
8172 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8173 }
8174 }
8175
8176 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8177 if (attr)
8178 {
8179 bfd *obfd = objfile->obfd;
8180
8181 /* The value of the DW_AT_ranges attribute is the offset of the
8182 address range list in the .debug_ranges section. */
8183 unsigned long offset = DW_UNSND (attr);
8184 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8185
8186 /* For some target architectures, but not others, the
8187 read_address function sign-extends the addresses it returns.
8188 To recognize base address selection entries, we need a
8189 mask. */
8190 unsigned int addr_size = cu->header.addr_size;
8191 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8192
8193 /* The base address, to which the next pair is relative. Note
8194 that this 'base' is a DWARF concept: most entries in a range
8195 list are relative, to reduce the number of relocs against the
8196 debugging information. This is separate from this function's
8197 'baseaddr' argument, which GDB uses to relocate debugging
8198 information from a shared library based on the address at
8199 which the library was loaded. */
8200 CORE_ADDR base = cu->base_address;
8201 int base_known = cu->base_known;
8202
8203 gdb_assert (dwarf2_per_objfile->ranges.readin);
8204 if (offset >= dwarf2_per_objfile->ranges.size)
8205 {
8206 complaint (&symfile_complaints,
8207 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8208 offset);
8209 return;
8210 }
8211
8212 for (;;)
8213 {
8214 unsigned int bytes_read;
8215 CORE_ADDR start, end;
8216
8217 start = read_address (obfd, buffer, cu, &bytes_read);
8218 buffer += bytes_read;
8219 end = read_address (obfd, buffer, cu, &bytes_read);
8220 buffer += bytes_read;
8221
8222 /* Did we find the end of the range list? */
8223 if (start == 0 && end == 0)
8224 break;
8225
8226 /* Did we find a base address selection entry? */
8227 else if ((start & base_select_mask) == base_select_mask)
8228 {
8229 base = end;
8230 base_known = 1;
8231 }
8232
8233 /* We found an ordinary address range. */
8234 else
8235 {
8236 if (!base_known)
8237 {
8238 complaint (&symfile_complaints,
8239 _("Invalid .debug_ranges data "
8240 "(no base address)"));
8241 return;
8242 }
8243
8244 if (start > end)
8245 {
8246 /* Inverted range entries are invalid. */
8247 complaint (&symfile_complaints,
8248 _("Invalid .debug_ranges data "
8249 "(inverted range)"));
8250 return;
8251 }
8252
8253 /* Empty range entries have no effect. */
8254 if (start == end)
8255 continue;
8256
8257 record_block_range (block,
8258 baseaddr + base + start,
8259 baseaddr + base + end - 1);
8260 }
8261 }
8262 }
8263 }
8264
8265 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8266 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8267 during 4.6.0 experimental. */
8268
8269 static int
8270 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8271 {
8272 const char *cs;
8273 int major, minor, release;
8274 int result = 0;
8275
8276 if (cu->producer == NULL)
8277 {
8278 /* For unknown compilers expect their behavior is DWARF version
8279 compliant.
8280
8281 GCC started to support .debug_types sections by -gdwarf-4 since
8282 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8283 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8284 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8285 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8286
8287 return 0;
8288 }
8289
8290 if (cu->checked_producer)
8291 return cu->producer_is_gxx_lt_4_6;
8292
8293 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8294
8295 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8296 {
8297 /* For non-GCC compilers expect their behavior is DWARF version
8298 compliant. */
8299 }
8300 else
8301 {
8302 cs = &cu->producer[strlen ("GNU ")];
8303 while (*cs && !isdigit (*cs))
8304 cs++;
8305 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8306 {
8307 /* Not recognized as GCC. */
8308 }
8309 else
8310 result = major < 4 || (major == 4 && minor < 6);
8311 }
8312
8313 cu->checked_producer = 1;
8314 cu->producer_is_gxx_lt_4_6 = result;
8315
8316 return result;
8317 }
8318
8319 /* Return the default accessibility type if it is not overriden by
8320 DW_AT_accessibility. */
8321
8322 static enum dwarf_access_attribute
8323 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8324 {
8325 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8326 {
8327 /* The default DWARF 2 accessibility for members is public, the default
8328 accessibility for inheritance is private. */
8329
8330 if (die->tag != DW_TAG_inheritance)
8331 return DW_ACCESS_public;
8332 else
8333 return DW_ACCESS_private;
8334 }
8335 else
8336 {
8337 /* DWARF 3+ defines the default accessibility a different way. The same
8338 rules apply now for DW_TAG_inheritance as for the members and it only
8339 depends on the container kind. */
8340
8341 if (die->parent->tag == DW_TAG_class_type)
8342 return DW_ACCESS_private;
8343 else
8344 return DW_ACCESS_public;
8345 }
8346 }
8347
8348 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8349 offset. If the attribute was not found return 0, otherwise return
8350 1. If it was found but could not properly be handled, set *OFFSET
8351 to 0. */
8352
8353 static int
8354 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8355 LONGEST *offset)
8356 {
8357 struct attribute *attr;
8358
8359 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8360 if (attr != NULL)
8361 {
8362 *offset = 0;
8363
8364 /* Note that we do not check for a section offset first here.
8365 This is because DW_AT_data_member_location is new in DWARF 4,
8366 so if we see it, we can assume that a constant form is really
8367 a constant and not a section offset. */
8368 if (attr_form_is_constant (attr))
8369 *offset = dwarf2_get_attr_constant_value (attr, 0);
8370 else if (attr_form_is_section_offset (attr))
8371 dwarf2_complex_location_expr_complaint ();
8372 else if (attr_form_is_block (attr))
8373 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8374 else
8375 dwarf2_complex_location_expr_complaint ();
8376
8377 return 1;
8378 }
8379
8380 return 0;
8381 }
8382
8383 /* Add an aggregate field to the field list. */
8384
8385 static void
8386 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8387 struct dwarf2_cu *cu)
8388 {
8389 struct objfile *objfile = cu->objfile;
8390 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8391 struct nextfield *new_field;
8392 struct attribute *attr;
8393 struct field *fp;
8394 char *fieldname = "";
8395
8396 /* Allocate a new field list entry and link it in. */
8397 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8398 make_cleanup (xfree, new_field);
8399 memset (new_field, 0, sizeof (struct nextfield));
8400
8401 if (die->tag == DW_TAG_inheritance)
8402 {
8403 new_field->next = fip->baseclasses;
8404 fip->baseclasses = new_field;
8405 }
8406 else
8407 {
8408 new_field->next = fip->fields;
8409 fip->fields = new_field;
8410 }
8411 fip->nfields++;
8412
8413 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8414 if (attr)
8415 new_field->accessibility = DW_UNSND (attr);
8416 else
8417 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8418 if (new_field->accessibility != DW_ACCESS_public)
8419 fip->non_public_fields = 1;
8420
8421 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8422 if (attr)
8423 new_field->virtuality = DW_UNSND (attr);
8424 else
8425 new_field->virtuality = DW_VIRTUALITY_none;
8426
8427 fp = &new_field->field;
8428
8429 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8430 {
8431 LONGEST offset;
8432
8433 /* Data member other than a C++ static data member. */
8434
8435 /* Get type of field. */
8436 fp->type = die_type (die, cu);
8437
8438 SET_FIELD_BITPOS (*fp, 0);
8439
8440 /* Get bit size of field (zero if none). */
8441 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8442 if (attr)
8443 {
8444 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8445 }
8446 else
8447 {
8448 FIELD_BITSIZE (*fp) = 0;
8449 }
8450
8451 /* Get bit offset of field. */
8452 if (handle_data_member_location (die, cu, &offset))
8453 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8454 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8455 if (attr)
8456 {
8457 if (gdbarch_bits_big_endian (gdbarch))
8458 {
8459 /* For big endian bits, the DW_AT_bit_offset gives the
8460 additional bit offset from the MSB of the containing
8461 anonymous object to the MSB of the field. We don't
8462 have to do anything special since we don't need to
8463 know the size of the anonymous object. */
8464 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8465 }
8466 else
8467 {
8468 /* For little endian bits, compute the bit offset to the
8469 MSB of the anonymous object, subtract off the number of
8470 bits from the MSB of the field to the MSB of the
8471 object, and then subtract off the number of bits of
8472 the field itself. The result is the bit offset of
8473 the LSB of the field. */
8474 int anonymous_size;
8475 int bit_offset = DW_UNSND (attr);
8476
8477 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8478 if (attr)
8479 {
8480 /* The size of the anonymous object containing
8481 the bit field is explicit, so use the
8482 indicated size (in bytes). */
8483 anonymous_size = DW_UNSND (attr);
8484 }
8485 else
8486 {
8487 /* The size of the anonymous object containing
8488 the bit field must be inferred from the type
8489 attribute of the data member containing the
8490 bit field. */
8491 anonymous_size = TYPE_LENGTH (fp->type);
8492 }
8493 SET_FIELD_BITPOS (*fp,
8494 (FIELD_BITPOS (*fp)
8495 + anonymous_size * bits_per_byte
8496 - bit_offset - FIELD_BITSIZE (*fp)));
8497 }
8498 }
8499
8500 /* Get name of field. */
8501 fieldname = dwarf2_name (die, cu);
8502 if (fieldname == NULL)
8503 fieldname = "";
8504
8505 /* The name is already allocated along with this objfile, so we don't
8506 need to duplicate it for the type. */
8507 fp->name = fieldname;
8508
8509 /* Change accessibility for artificial fields (e.g. virtual table
8510 pointer or virtual base class pointer) to private. */
8511 if (dwarf2_attr (die, DW_AT_artificial, cu))
8512 {
8513 FIELD_ARTIFICIAL (*fp) = 1;
8514 new_field->accessibility = DW_ACCESS_private;
8515 fip->non_public_fields = 1;
8516 }
8517 }
8518 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8519 {
8520 /* C++ static member. */
8521
8522 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8523 is a declaration, but all versions of G++ as of this writing
8524 (so through at least 3.2.1) incorrectly generate
8525 DW_TAG_variable tags. */
8526
8527 const char *physname;
8528
8529 /* Get name of field. */
8530 fieldname = dwarf2_name (die, cu);
8531 if (fieldname == NULL)
8532 return;
8533
8534 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8535 if (attr
8536 /* Only create a symbol if this is an external value.
8537 new_symbol checks this and puts the value in the global symbol
8538 table, which we want. If it is not external, new_symbol
8539 will try to put the value in cu->list_in_scope which is wrong. */
8540 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8541 {
8542 /* A static const member, not much different than an enum as far as
8543 we're concerned, except that we can support more types. */
8544 new_symbol (die, NULL, cu);
8545 }
8546
8547 /* Get physical name. */
8548 physname = dwarf2_physname (fieldname, die, cu);
8549
8550 /* The name is already allocated along with this objfile, so we don't
8551 need to duplicate it for the type. */
8552 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8553 FIELD_TYPE (*fp) = die_type (die, cu);
8554 FIELD_NAME (*fp) = fieldname;
8555 }
8556 else if (die->tag == DW_TAG_inheritance)
8557 {
8558 LONGEST offset;
8559
8560 /* C++ base class field. */
8561 if (handle_data_member_location (die, cu, &offset))
8562 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8563 FIELD_BITSIZE (*fp) = 0;
8564 FIELD_TYPE (*fp) = die_type (die, cu);
8565 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8566 fip->nbaseclasses++;
8567 }
8568 }
8569
8570 /* Add a typedef defined in the scope of the FIP's class. */
8571
8572 static void
8573 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8574 struct dwarf2_cu *cu)
8575 {
8576 struct objfile *objfile = cu->objfile;
8577 struct typedef_field_list *new_field;
8578 struct attribute *attr;
8579 struct typedef_field *fp;
8580 char *fieldname = "";
8581
8582 /* Allocate a new field list entry and link it in. */
8583 new_field = xzalloc (sizeof (*new_field));
8584 make_cleanup (xfree, new_field);
8585
8586 gdb_assert (die->tag == DW_TAG_typedef);
8587
8588 fp = &new_field->field;
8589
8590 /* Get name of field. */
8591 fp->name = dwarf2_name (die, cu);
8592 if (fp->name == NULL)
8593 return;
8594
8595 fp->type = read_type_die (die, cu);
8596
8597 new_field->next = fip->typedef_field_list;
8598 fip->typedef_field_list = new_field;
8599 fip->typedef_field_list_count++;
8600 }
8601
8602 /* Create the vector of fields, and attach it to the type. */
8603
8604 static void
8605 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8606 struct dwarf2_cu *cu)
8607 {
8608 int nfields = fip->nfields;
8609
8610 /* Record the field count, allocate space for the array of fields,
8611 and create blank accessibility bitfields if necessary. */
8612 TYPE_NFIELDS (type) = nfields;
8613 TYPE_FIELDS (type) = (struct field *)
8614 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8615 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8616
8617 if (fip->non_public_fields && cu->language != language_ada)
8618 {
8619 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8620
8621 TYPE_FIELD_PRIVATE_BITS (type) =
8622 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8623 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8624
8625 TYPE_FIELD_PROTECTED_BITS (type) =
8626 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8627 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8628
8629 TYPE_FIELD_IGNORE_BITS (type) =
8630 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8631 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8632 }
8633
8634 /* If the type has baseclasses, allocate and clear a bit vector for
8635 TYPE_FIELD_VIRTUAL_BITS. */
8636 if (fip->nbaseclasses && cu->language != language_ada)
8637 {
8638 int num_bytes = B_BYTES (fip->nbaseclasses);
8639 unsigned char *pointer;
8640
8641 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8642 pointer = TYPE_ALLOC (type, num_bytes);
8643 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8644 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8645 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8646 }
8647
8648 /* Copy the saved-up fields into the field vector. Start from the head of
8649 the list, adding to the tail of the field array, so that they end up in
8650 the same order in the array in which they were added to the list. */
8651 while (nfields-- > 0)
8652 {
8653 struct nextfield *fieldp;
8654
8655 if (fip->fields)
8656 {
8657 fieldp = fip->fields;
8658 fip->fields = fieldp->next;
8659 }
8660 else
8661 {
8662 fieldp = fip->baseclasses;
8663 fip->baseclasses = fieldp->next;
8664 }
8665
8666 TYPE_FIELD (type, nfields) = fieldp->field;
8667 switch (fieldp->accessibility)
8668 {
8669 case DW_ACCESS_private:
8670 if (cu->language != language_ada)
8671 SET_TYPE_FIELD_PRIVATE (type, nfields);
8672 break;
8673
8674 case DW_ACCESS_protected:
8675 if (cu->language != language_ada)
8676 SET_TYPE_FIELD_PROTECTED (type, nfields);
8677 break;
8678
8679 case DW_ACCESS_public:
8680 break;
8681
8682 default:
8683 /* Unknown accessibility. Complain and treat it as public. */
8684 {
8685 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8686 fieldp->accessibility);
8687 }
8688 break;
8689 }
8690 if (nfields < fip->nbaseclasses)
8691 {
8692 switch (fieldp->virtuality)
8693 {
8694 case DW_VIRTUALITY_virtual:
8695 case DW_VIRTUALITY_pure_virtual:
8696 if (cu->language == language_ada)
8697 error (_("unexpected virtuality in component of Ada type"));
8698 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8699 break;
8700 }
8701 }
8702 }
8703 }
8704
8705 /* Add a member function to the proper fieldlist. */
8706
8707 static void
8708 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8709 struct type *type, struct dwarf2_cu *cu)
8710 {
8711 struct objfile *objfile = cu->objfile;
8712 struct attribute *attr;
8713 struct fnfieldlist *flp;
8714 int i;
8715 struct fn_field *fnp;
8716 char *fieldname;
8717 struct nextfnfield *new_fnfield;
8718 struct type *this_type;
8719 enum dwarf_access_attribute accessibility;
8720
8721 if (cu->language == language_ada)
8722 error (_("unexpected member function in Ada type"));
8723
8724 /* Get name of member function. */
8725 fieldname = dwarf2_name (die, cu);
8726 if (fieldname == NULL)
8727 return;
8728
8729 /* Look up member function name in fieldlist. */
8730 for (i = 0; i < fip->nfnfields; i++)
8731 {
8732 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8733 break;
8734 }
8735
8736 /* Create new list element if necessary. */
8737 if (i < fip->nfnfields)
8738 flp = &fip->fnfieldlists[i];
8739 else
8740 {
8741 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8742 {
8743 fip->fnfieldlists = (struct fnfieldlist *)
8744 xrealloc (fip->fnfieldlists,
8745 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8746 * sizeof (struct fnfieldlist));
8747 if (fip->nfnfields == 0)
8748 make_cleanup (free_current_contents, &fip->fnfieldlists);
8749 }
8750 flp = &fip->fnfieldlists[fip->nfnfields];
8751 flp->name = fieldname;
8752 flp->length = 0;
8753 flp->head = NULL;
8754 i = fip->nfnfields++;
8755 }
8756
8757 /* Create a new member function field and chain it to the field list
8758 entry. */
8759 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8760 make_cleanup (xfree, new_fnfield);
8761 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8762 new_fnfield->next = flp->head;
8763 flp->head = new_fnfield;
8764 flp->length++;
8765
8766 /* Fill in the member function field info. */
8767 fnp = &new_fnfield->fnfield;
8768
8769 /* Delay processing of the physname until later. */
8770 if (cu->language == language_cplus || cu->language == language_java)
8771 {
8772 add_to_method_list (type, i, flp->length - 1, fieldname,
8773 die, cu);
8774 }
8775 else
8776 {
8777 const char *physname = dwarf2_physname (fieldname, die, cu);
8778 fnp->physname = physname ? physname : "";
8779 }
8780
8781 fnp->type = alloc_type (objfile);
8782 this_type = read_type_die (die, cu);
8783 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8784 {
8785 int nparams = TYPE_NFIELDS (this_type);
8786
8787 /* TYPE is the domain of this method, and THIS_TYPE is the type
8788 of the method itself (TYPE_CODE_METHOD). */
8789 smash_to_method_type (fnp->type, type,
8790 TYPE_TARGET_TYPE (this_type),
8791 TYPE_FIELDS (this_type),
8792 TYPE_NFIELDS (this_type),
8793 TYPE_VARARGS (this_type));
8794
8795 /* Handle static member functions.
8796 Dwarf2 has no clean way to discern C++ static and non-static
8797 member functions. G++ helps GDB by marking the first
8798 parameter for non-static member functions (which is the this
8799 pointer) as artificial. We obtain this information from
8800 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8801 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8802 fnp->voffset = VOFFSET_STATIC;
8803 }
8804 else
8805 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8806 dwarf2_full_name (fieldname, die, cu));
8807
8808 /* Get fcontext from DW_AT_containing_type if present. */
8809 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8810 fnp->fcontext = die_containing_type (die, cu);
8811
8812 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8813 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8814
8815 /* Get accessibility. */
8816 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8817 if (attr)
8818 accessibility = DW_UNSND (attr);
8819 else
8820 accessibility = dwarf2_default_access_attribute (die, cu);
8821 switch (accessibility)
8822 {
8823 case DW_ACCESS_private:
8824 fnp->is_private = 1;
8825 break;
8826 case DW_ACCESS_protected:
8827 fnp->is_protected = 1;
8828 break;
8829 }
8830
8831 /* Check for artificial methods. */
8832 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8833 if (attr && DW_UNSND (attr) != 0)
8834 fnp->is_artificial = 1;
8835
8836 /* Get index in virtual function table if it is a virtual member
8837 function. For older versions of GCC, this is an offset in the
8838 appropriate virtual table, as specified by DW_AT_containing_type.
8839 For everyone else, it is an expression to be evaluated relative
8840 to the object address. */
8841
8842 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8843 if (attr)
8844 {
8845 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8846 {
8847 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8848 {
8849 /* Old-style GCC. */
8850 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8851 }
8852 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8853 || (DW_BLOCK (attr)->size > 1
8854 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8855 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8856 {
8857 struct dwarf_block blk;
8858 int offset;
8859
8860 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8861 ? 1 : 2);
8862 blk.size = DW_BLOCK (attr)->size - offset;
8863 blk.data = DW_BLOCK (attr)->data + offset;
8864 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8865 if ((fnp->voffset % cu->header.addr_size) != 0)
8866 dwarf2_complex_location_expr_complaint ();
8867 else
8868 fnp->voffset /= cu->header.addr_size;
8869 fnp->voffset += 2;
8870 }
8871 else
8872 dwarf2_complex_location_expr_complaint ();
8873
8874 if (!fnp->fcontext)
8875 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8876 }
8877 else if (attr_form_is_section_offset (attr))
8878 {
8879 dwarf2_complex_location_expr_complaint ();
8880 }
8881 else
8882 {
8883 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8884 fieldname);
8885 }
8886 }
8887 else
8888 {
8889 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8890 if (attr && DW_UNSND (attr))
8891 {
8892 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8893 complaint (&symfile_complaints,
8894 _("Member function \"%s\" (offset %d) is virtual "
8895 "but the vtable offset is not specified"),
8896 fieldname, die->offset.sect_off);
8897 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8898 TYPE_CPLUS_DYNAMIC (type) = 1;
8899 }
8900 }
8901 }
8902
8903 /* Create the vector of member function fields, and attach it to the type. */
8904
8905 static void
8906 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8907 struct dwarf2_cu *cu)
8908 {
8909 struct fnfieldlist *flp;
8910 int i;
8911
8912 if (cu->language == language_ada)
8913 error (_("unexpected member functions in Ada type"));
8914
8915 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8916 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8917 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8918
8919 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8920 {
8921 struct nextfnfield *nfp = flp->head;
8922 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8923 int k;
8924
8925 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8926 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8927 fn_flp->fn_fields = (struct fn_field *)
8928 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8929 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8930 fn_flp->fn_fields[k] = nfp->fnfield;
8931 }
8932
8933 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8934 }
8935
8936 /* Returns non-zero if NAME is the name of a vtable member in CU's
8937 language, zero otherwise. */
8938 static int
8939 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8940 {
8941 static const char vptr[] = "_vptr";
8942 static const char vtable[] = "vtable";
8943
8944 /* Look for the C++ and Java forms of the vtable. */
8945 if ((cu->language == language_java
8946 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8947 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8948 && is_cplus_marker (name[sizeof (vptr) - 1])))
8949 return 1;
8950
8951 return 0;
8952 }
8953
8954 /* GCC outputs unnamed structures that are really pointers to member
8955 functions, with the ABI-specified layout. If TYPE describes
8956 such a structure, smash it into a member function type.
8957
8958 GCC shouldn't do this; it should just output pointer to member DIEs.
8959 This is GCC PR debug/28767. */
8960
8961 static void
8962 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8963 {
8964 struct type *pfn_type, *domain_type, *new_type;
8965
8966 /* Check for a structure with no name and two children. */
8967 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8968 return;
8969
8970 /* Check for __pfn and __delta members. */
8971 if (TYPE_FIELD_NAME (type, 0) == NULL
8972 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8973 || TYPE_FIELD_NAME (type, 1) == NULL
8974 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8975 return;
8976
8977 /* Find the type of the method. */
8978 pfn_type = TYPE_FIELD_TYPE (type, 0);
8979 if (pfn_type == NULL
8980 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8981 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8982 return;
8983
8984 /* Look for the "this" argument. */
8985 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8986 if (TYPE_NFIELDS (pfn_type) == 0
8987 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8988 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8989 return;
8990
8991 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
8992 new_type = alloc_type (objfile);
8993 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
8994 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8995 TYPE_VARARGS (pfn_type));
8996 smash_to_methodptr_type (type, new_type);
8997 }
8998
8999 /* Called when we find the DIE that starts a structure or union scope
9000 (definition) to create a type for the structure or union. Fill in
9001 the type's name and general properties; the members will not be
9002 processed until process_structure_type.
9003
9004 NOTE: we need to call these functions regardless of whether or not the
9005 DIE has a DW_AT_name attribute, since it might be an anonymous
9006 structure or union. This gets the type entered into our set of
9007 user defined types.
9008
9009 However, if the structure is incomplete (an opaque struct/union)
9010 then suppress creating a symbol table entry for it since gdb only
9011 wants to find the one with the complete definition. Note that if
9012 it is complete, we just call new_symbol, which does it's own
9013 checking about whether the struct/union is anonymous or not (and
9014 suppresses creating a symbol table entry itself). */
9015
9016 static struct type *
9017 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9018 {
9019 struct objfile *objfile = cu->objfile;
9020 struct type *type;
9021 struct attribute *attr;
9022 char *name;
9023
9024 /* If the definition of this type lives in .debug_types, read that type.
9025 Don't follow DW_AT_specification though, that will take us back up
9026 the chain and we want to go down. */
9027 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9028 if (attr)
9029 {
9030 struct dwarf2_cu *type_cu = cu;
9031 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9032
9033 /* We could just recurse on read_structure_type, but we need to call
9034 get_die_type to ensure only one type for this DIE is created.
9035 This is important, for example, because for c++ classes we need
9036 TYPE_NAME set which is only done by new_symbol. Blech. */
9037 type = read_type_die (type_die, type_cu);
9038
9039 /* TYPE_CU may not be the same as CU.
9040 Ensure TYPE is recorded in CU's type_hash table. */
9041 return set_die_type (die, type, cu);
9042 }
9043
9044 type = alloc_type (objfile);
9045 INIT_CPLUS_SPECIFIC (type);
9046
9047 name = dwarf2_name (die, cu);
9048 if (name != NULL)
9049 {
9050 if (cu->language == language_cplus
9051 || cu->language == language_java)
9052 {
9053 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9054
9055 /* dwarf2_full_name might have already finished building the DIE's
9056 type. If so, there is no need to continue. */
9057 if (get_die_type (die, cu) != NULL)
9058 return get_die_type (die, cu);
9059
9060 TYPE_TAG_NAME (type) = full_name;
9061 if (die->tag == DW_TAG_structure_type
9062 || die->tag == DW_TAG_class_type)
9063 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9064 }
9065 else
9066 {
9067 /* The name is already allocated along with this objfile, so
9068 we don't need to duplicate it for the type. */
9069 TYPE_TAG_NAME (type) = (char *) name;
9070 if (die->tag == DW_TAG_class_type)
9071 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9072 }
9073 }
9074
9075 if (die->tag == DW_TAG_structure_type)
9076 {
9077 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9078 }
9079 else if (die->tag == DW_TAG_union_type)
9080 {
9081 TYPE_CODE (type) = TYPE_CODE_UNION;
9082 }
9083 else
9084 {
9085 TYPE_CODE (type) = TYPE_CODE_CLASS;
9086 }
9087
9088 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9089 TYPE_DECLARED_CLASS (type) = 1;
9090
9091 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9092 if (attr)
9093 {
9094 TYPE_LENGTH (type) = DW_UNSND (attr);
9095 }
9096 else
9097 {
9098 TYPE_LENGTH (type) = 0;
9099 }
9100
9101 TYPE_STUB_SUPPORTED (type) = 1;
9102 if (die_is_declaration (die, cu))
9103 TYPE_STUB (type) = 1;
9104 else if (attr == NULL && die->child == NULL
9105 && producer_is_realview (cu->producer))
9106 /* RealView does not output the required DW_AT_declaration
9107 on incomplete types. */
9108 TYPE_STUB (type) = 1;
9109
9110 /* We need to add the type field to the die immediately so we don't
9111 infinitely recurse when dealing with pointers to the structure
9112 type within the structure itself. */
9113 set_die_type (die, type, cu);
9114
9115 /* set_die_type should be already done. */
9116 set_descriptive_type (type, die, cu);
9117
9118 return type;
9119 }
9120
9121 /* Finish creating a structure or union type, including filling in
9122 its members and creating a symbol for it. */
9123
9124 static void
9125 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9126 {
9127 struct objfile *objfile = cu->objfile;
9128 struct die_info *child_die = die->child;
9129 struct type *type;
9130
9131 type = get_die_type (die, cu);
9132 if (type == NULL)
9133 type = read_structure_type (die, cu);
9134
9135 if (die->child != NULL && ! die_is_declaration (die, cu))
9136 {
9137 struct field_info fi;
9138 struct die_info *child_die;
9139 VEC (symbolp) *template_args = NULL;
9140 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9141
9142 memset (&fi, 0, sizeof (struct field_info));
9143
9144 child_die = die->child;
9145
9146 while (child_die && child_die->tag)
9147 {
9148 if (child_die->tag == DW_TAG_member
9149 || child_die->tag == DW_TAG_variable)
9150 {
9151 /* NOTE: carlton/2002-11-05: A C++ static data member
9152 should be a DW_TAG_member that is a declaration, but
9153 all versions of G++ as of this writing (so through at
9154 least 3.2.1) incorrectly generate DW_TAG_variable
9155 tags for them instead. */
9156 dwarf2_add_field (&fi, child_die, cu);
9157 }
9158 else if (child_die->tag == DW_TAG_subprogram)
9159 {
9160 /* C++ member function. */
9161 dwarf2_add_member_fn (&fi, child_die, type, cu);
9162 }
9163 else if (child_die->tag == DW_TAG_inheritance)
9164 {
9165 /* C++ base class field. */
9166 dwarf2_add_field (&fi, child_die, cu);
9167 }
9168 else if (child_die->tag == DW_TAG_typedef)
9169 dwarf2_add_typedef (&fi, child_die, cu);
9170 else if (child_die->tag == DW_TAG_template_type_param
9171 || child_die->tag == DW_TAG_template_value_param)
9172 {
9173 struct symbol *arg = new_symbol (child_die, NULL, cu);
9174
9175 if (arg != NULL)
9176 VEC_safe_push (symbolp, template_args, arg);
9177 }
9178
9179 child_die = sibling_die (child_die);
9180 }
9181
9182 /* Attach template arguments to type. */
9183 if (! VEC_empty (symbolp, template_args))
9184 {
9185 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9186 TYPE_N_TEMPLATE_ARGUMENTS (type)
9187 = VEC_length (symbolp, template_args);
9188 TYPE_TEMPLATE_ARGUMENTS (type)
9189 = obstack_alloc (&objfile->objfile_obstack,
9190 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9191 * sizeof (struct symbol *)));
9192 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9193 VEC_address (symbolp, template_args),
9194 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9195 * sizeof (struct symbol *)));
9196 VEC_free (symbolp, template_args);
9197 }
9198
9199 /* Attach fields and member functions to the type. */
9200 if (fi.nfields)
9201 dwarf2_attach_fields_to_type (&fi, type, cu);
9202 if (fi.nfnfields)
9203 {
9204 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9205
9206 /* Get the type which refers to the base class (possibly this
9207 class itself) which contains the vtable pointer for the current
9208 class from the DW_AT_containing_type attribute. This use of
9209 DW_AT_containing_type is a GNU extension. */
9210
9211 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9212 {
9213 struct type *t = die_containing_type (die, cu);
9214
9215 TYPE_VPTR_BASETYPE (type) = t;
9216 if (type == t)
9217 {
9218 int i;
9219
9220 /* Our own class provides vtbl ptr. */
9221 for (i = TYPE_NFIELDS (t) - 1;
9222 i >= TYPE_N_BASECLASSES (t);
9223 --i)
9224 {
9225 const char *fieldname = TYPE_FIELD_NAME (t, i);
9226
9227 if (is_vtable_name (fieldname, cu))
9228 {
9229 TYPE_VPTR_FIELDNO (type) = i;
9230 break;
9231 }
9232 }
9233
9234 /* Complain if virtual function table field not found. */
9235 if (i < TYPE_N_BASECLASSES (t))
9236 complaint (&symfile_complaints,
9237 _("virtual function table pointer "
9238 "not found when defining class '%s'"),
9239 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9240 "");
9241 }
9242 else
9243 {
9244 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9245 }
9246 }
9247 else if (cu->producer
9248 && strncmp (cu->producer,
9249 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9250 {
9251 /* The IBM XLC compiler does not provide direct indication
9252 of the containing type, but the vtable pointer is
9253 always named __vfp. */
9254
9255 int i;
9256
9257 for (i = TYPE_NFIELDS (type) - 1;
9258 i >= TYPE_N_BASECLASSES (type);
9259 --i)
9260 {
9261 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9262 {
9263 TYPE_VPTR_FIELDNO (type) = i;
9264 TYPE_VPTR_BASETYPE (type) = type;
9265 break;
9266 }
9267 }
9268 }
9269 }
9270
9271 /* Copy fi.typedef_field_list linked list elements content into the
9272 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9273 if (fi.typedef_field_list)
9274 {
9275 int i = fi.typedef_field_list_count;
9276
9277 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9278 TYPE_TYPEDEF_FIELD_ARRAY (type)
9279 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9280 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9281
9282 /* Reverse the list order to keep the debug info elements order. */
9283 while (--i >= 0)
9284 {
9285 struct typedef_field *dest, *src;
9286
9287 dest = &TYPE_TYPEDEF_FIELD (type, i);
9288 src = &fi.typedef_field_list->field;
9289 fi.typedef_field_list = fi.typedef_field_list->next;
9290 *dest = *src;
9291 }
9292 }
9293
9294 do_cleanups (back_to);
9295
9296 if (HAVE_CPLUS_STRUCT (type))
9297 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9298 }
9299
9300 quirk_gcc_member_function_pointer (type, objfile);
9301
9302 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9303 snapshots) has been known to create a die giving a declaration
9304 for a class that has, as a child, a die giving a definition for a
9305 nested class. So we have to process our children even if the
9306 current die is a declaration. Normally, of course, a declaration
9307 won't have any children at all. */
9308
9309 while (child_die != NULL && child_die->tag)
9310 {
9311 if (child_die->tag == DW_TAG_member
9312 || child_die->tag == DW_TAG_variable
9313 || child_die->tag == DW_TAG_inheritance
9314 || child_die->tag == DW_TAG_template_value_param
9315 || child_die->tag == DW_TAG_template_type_param)
9316 {
9317 /* Do nothing. */
9318 }
9319 else
9320 process_die (child_die, cu);
9321
9322 child_die = sibling_die (child_die);
9323 }
9324
9325 /* Do not consider external references. According to the DWARF standard,
9326 these DIEs are identified by the fact that they have no byte_size
9327 attribute, and a declaration attribute. */
9328 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9329 || !die_is_declaration (die, cu))
9330 new_symbol (die, type, cu);
9331 }
9332
9333 /* Given a DW_AT_enumeration_type die, set its type. We do not
9334 complete the type's fields yet, or create any symbols. */
9335
9336 static struct type *
9337 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9338 {
9339 struct objfile *objfile = cu->objfile;
9340 struct type *type;
9341 struct attribute *attr;
9342 const char *name;
9343
9344 /* If the definition of this type lives in .debug_types, read that type.
9345 Don't follow DW_AT_specification though, that will take us back up
9346 the chain and we want to go down. */
9347 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9348 if (attr)
9349 {
9350 struct dwarf2_cu *type_cu = cu;
9351 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9352
9353 type = read_type_die (type_die, type_cu);
9354
9355 /* TYPE_CU may not be the same as CU.
9356 Ensure TYPE is recorded in CU's type_hash table. */
9357 return set_die_type (die, type, cu);
9358 }
9359
9360 type = alloc_type (objfile);
9361
9362 TYPE_CODE (type) = TYPE_CODE_ENUM;
9363 name = dwarf2_full_name (NULL, die, cu);
9364 if (name != NULL)
9365 TYPE_TAG_NAME (type) = (char *) name;
9366
9367 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9368 if (attr)
9369 {
9370 TYPE_LENGTH (type) = DW_UNSND (attr);
9371 }
9372 else
9373 {
9374 TYPE_LENGTH (type) = 0;
9375 }
9376
9377 /* The enumeration DIE can be incomplete. In Ada, any type can be
9378 declared as private in the package spec, and then defined only
9379 inside the package body. Such types are known as Taft Amendment
9380 Types. When another package uses such a type, an incomplete DIE
9381 may be generated by the compiler. */
9382 if (die_is_declaration (die, cu))
9383 TYPE_STUB (type) = 1;
9384
9385 return set_die_type (die, type, cu);
9386 }
9387
9388 /* Given a pointer to a die which begins an enumeration, process all
9389 the dies that define the members of the enumeration, and create the
9390 symbol for the enumeration type.
9391
9392 NOTE: We reverse the order of the element list. */
9393
9394 static void
9395 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9396 {
9397 struct type *this_type;
9398
9399 this_type = get_die_type (die, cu);
9400 if (this_type == NULL)
9401 this_type = read_enumeration_type (die, cu);
9402
9403 if (die->child != NULL)
9404 {
9405 struct die_info *child_die;
9406 struct symbol *sym;
9407 struct field *fields = NULL;
9408 int num_fields = 0;
9409 int unsigned_enum = 1;
9410 char *name;
9411 int flag_enum = 1;
9412 ULONGEST mask = 0;
9413
9414 child_die = die->child;
9415 while (child_die && child_die->tag)
9416 {
9417 if (child_die->tag != DW_TAG_enumerator)
9418 {
9419 process_die (child_die, cu);
9420 }
9421 else
9422 {
9423 name = dwarf2_name (child_die, cu);
9424 if (name)
9425 {
9426 sym = new_symbol (child_die, this_type, cu);
9427 if (SYMBOL_VALUE (sym) < 0)
9428 {
9429 unsigned_enum = 0;
9430 flag_enum = 0;
9431 }
9432 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9433 flag_enum = 0;
9434 else
9435 mask |= SYMBOL_VALUE (sym);
9436
9437 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9438 {
9439 fields = (struct field *)
9440 xrealloc (fields,
9441 (num_fields + DW_FIELD_ALLOC_CHUNK)
9442 * sizeof (struct field));
9443 }
9444
9445 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9446 FIELD_TYPE (fields[num_fields]) = NULL;
9447 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9448 FIELD_BITSIZE (fields[num_fields]) = 0;
9449
9450 num_fields++;
9451 }
9452 }
9453
9454 child_die = sibling_die (child_die);
9455 }
9456
9457 if (num_fields)
9458 {
9459 TYPE_NFIELDS (this_type) = num_fields;
9460 TYPE_FIELDS (this_type) = (struct field *)
9461 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9462 memcpy (TYPE_FIELDS (this_type), fields,
9463 sizeof (struct field) * num_fields);
9464 xfree (fields);
9465 }
9466 if (unsigned_enum)
9467 TYPE_UNSIGNED (this_type) = 1;
9468 if (flag_enum)
9469 TYPE_FLAG_ENUM (this_type) = 1;
9470 }
9471
9472 /* If we are reading an enum from a .debug_types unit, and the enum
9473 is a declaration, and the enum is not the signatured type in the
9474 unit, then we do not want to add a symbol for it. Adding a
9475 symbol would in some cases obscure the true definition of the
9476 enum, giving users an incomplete type when the definition is
9477 actually available. Note that we do not want to do this for all
9478 enums which are just declarations, because C++0x allows forward
9479 enum declarations. */
9480 if (cu->per_cu->is_debug_types
9481 && die_is_declaration (die, cu))
9482 {
9483 struct signatured_type *sig_type;
9484
9485 sig_type
9486 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9487 cu->per_cu->info_or_types_section,
9488 cu->per_cu->offset);
9489 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9490 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9491 return;
9492 }
9493
9494 new_symbol (die, this_type, cu);
9495 }
9496
9497 /* Extract all information from a DW_TAG_array_type DIE and put it in
9498 the DIE's type field. For now, this only handles one dimensional
9499 arrays. */
9500
9501 static struct type *
9502 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9503 {
9504 struct objfile *objfile = cu->objfile;
9505 struct die_info *child_die;
9506 struct type *type;
9507 struct type *element_type, *range_type, *index_type;
9508 struct type **range_types = NULL;
9509 struct attribute *attr;
9510 int ndim = 0;
9511 struct cleanup *back_to;
9512 char *name;
9513
9514 element_type = die_type (die, cu);
9515
9516 /* The die_type call above may have already set the type for this DIE. */
9517 type = get_die_type (die, cu);
9518 if (type)
9519 return type;
9520
9521 /* Irix 6.2 native cc creates array types without children for
9522 arrays with unspecified length. */
9523 if (die->child == NULL)
9524 {
9525 index_type = objfile_type (objfile)->builtin_int;
9526 range_type = create_range_type (NULL, index_type, 0, -1);
9527 type = create_array_type (NULL, element_type, range_type);
9528 return set_die_type (die, type, cu);
9529 }
9530
9531 back_to = make_cleanup (null_cleanup, NULL);
9532 child_die = die->child;
9533 while (child_die && child_die->tag)
9534 {
9535 if (child_die->tag == DW_TAG_subrange_type)
9536 {
9537 struct type *child_type = read_type_die (child_die, cu);
9538
9539 if (child_type != NULL)
9540 {
9541 /* The range type was succesfully read. Save it for the
9542 array type creation. */
9543 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9544 {
9545 range_types = (struct type **)
9546 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9547 * sizeof (struct type *));
9548 if (ndim == 0)
9549 make_cleanup (free_current_contents, &range_types);
9550 }
9551 range_types[ndim++] = child_type;
9552 }
9553 }
9554 child_die = sibling_die (child_die);
9555 }
9556
9557 /* Dwarf2 dimensions are output from left to right, create the
9558 necessary array types in backwards order. */
9559
9560 type = element_type;
9561
9562 if (read_array_order (die, cu) == DW_ORD_col_major)
9563 {
9564 int i = 0;
9565
9566 while (i < ndim)
9567 type = create_array_type (NULL, type, range_types[i++]);
9568 }
9569 else
9570 {
9571 while (ndim-- > 0)
9572 type = create_array_type (NULL, type, range_types[ndim]);
9573 }
9574
9575 /* Understand Dwarf2 support for vector types (like they occur on
9576 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9577 array type. This is not part of the Dwarf2/3 standard yet, but a
9578 custom vendor extension. The main difference between a regular
9579 array and the vector variant is that vectors are passed by value
9580 to functions. */
9581 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9582 if (attr)
9583 make_vector_type (type);
9584
9585 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9586 implementation may choose to implement triple vectors using this
9587 attribute. */
9588 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9589 if (attr)
9590 {
9591 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9592 TYPE_LENGTH (type) = DW_UNSND (attr);
9593 else
9594 complaint (&symfile_complaints,
9595 _("DW_AT_byte_size for array type smaller "
9596 "than the total size of elements"));
9597 }
9598
9599 name = dwarf2_name (die, cu);
9600 if (name)
9601 TYPE_NAME (type) = name;
9602
9603 /* Install the type in the die. */
9604 set_die_type (die, type, cu);
9605
9606 /* set_die_type should be already done. */
9607 set_descriptive_type (type, die, cu);
9608
9609 do_cleanups (back_to);
9610
9611 return type;
9612 }
9613
9614 static enum dwarf_array_dim_ordering
9615 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9616 {
9617 struct attribute *attr;
9618
9619 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9620
9621 if (attr) return DW_SND (attr);
9622
9623 /* GNU F77 is a special case, as at 08/2004 array type info is the
9624 opposite order to the dwarf2 specification, but data is still
9625 laid out as per normal fortran.
9626
9627 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9628 version checking. */
9629
9630 if (cu->language == language_fortran
9631 && cu->producer && strstr (cu->producer, "GNU F77"))
9632 {
9633 return DW_ORD_row_major;
9634 }
9635
9636 switch (cu->language_defn->la_array_ordering)
9637 {
9638 case array_column_major:
9639 return DW_ORD_col_major;
9640 case array_row_major:
9641 default:
9642 return DW_ORD_row_major;
9643 };
9644 }
9645
9646 /* Extract all information from a DW_TAG_set_type DIE and put it in
9647 the DIE's type field. */
9648
9649 static struct type *
9650 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9651 {
9652 struct type *domain_type, *set_type;
9653 struct attribute *attr;
9654
9655 domain_type = die_type (die, cu);
9656
9657 /* The die_type call above may have already set the type for this DIE. */
9658 set_type = get_die_type (die, cu);
9659 if (set_type)
9660 return set_type;
9661
9662 set_type = create_set_type (NULL, domain_type);
9663
9664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9665 if (attr)
9666 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9667
9668 return set_die_type (die, set_type, cu);
9669 }
9670
9671 /* First cut: install each common block member as a global variable. */
9672
9673 static void
9674 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9675 {
9676 struct die_info *child_die;
9677 struct attribute *attr;
9678 struct symbol *sym;
9679 CORE_ADDR base = (CORE_ADDR) 0;
9680
9681 attr = dwarf2_attr (die, DW_AT_location, cu);
9682 if (attr)
9683 {
9684 /* Support the .debug_loc offsets. */
9685 if (attr_form_is_block (attr))
9686 {
9687 base = decode_locdesc (DW_BLOCK (attr), cu);
9688 }
9689 else if (attr_form_is_section_offset (attr))
9690 {
9691 dwarf2_complex_location_expr_complaint ();
9692 }
9693 else
9694 {
9695 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9696 "common block member");
9697 }
9698 }
9699 if (die->child != NULL)
9700 {
9701 child_die = die->child;
9702 while (child_die && child_die->tag)
9703 {
9704 LONGEST offset;
9705
9706 sym = new_symbol (child_die, NULL, cu);
9707 if (sym != NULL
9708 && handle_data_member_location (child_die, cu, &offset))
9709 {
9710 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9711 add_symbol_to_list (sym, &global_symbols);
9712 }
9713 child_die = sibling_die (child_die);
9714 }
9715 }
9716 }
9717
9718 /* Create a type for a C++ namespace. */
9719
9720 static struct type *
9721 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9722 {
9723 struct objfile *objfile = cu->objfile;
9724 const char *previous_prefix, *name;
9725 int is_anonymous;
9726 struct type *type;
9727
9728 /* For extensions, reuse the type of the original namespace. */
9729 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9730 {
9731 struct die_info *ext_die;
9732 struct dwarf2_cu *ext_cu = cu;
9733
9734 ext_die = dwarf2_extension (die, &ext_cu);
9735 type = read_type_die (ext_die, ext_cu);
9736
9737 /* EXT_CU may not be the same as CU.
9738 Ensure TYPE is recorded in CU's type_hash table. */
9739 return set_die_type (die, type, cu);
9740 }
9741
9742 name = namespace_name (die, &is_anonymous, cu);
9743
9744 /* Now build the name of the current namespace. */
9745
9746 previous_prefix = determine_prefix (die, cu);
9747 if (previous_prefix[0] != '\0')
9748 name = typename_concat (&objfile->objfile_obstack,
9749 previous_prefix, name, 0, cu);
9750
9751 /* Create the type. */
9752 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9753 objfile);
9754 TYPE_NAME (type) = (char *) name;
9755 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9756
9757 return set_die_type (die, type, cu);
9758 }
9759
9760 /* Read a C++ namespace. */
9761
9762 static void
9763 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9764 {
9765 struct objfile *objfile = cu->objfile;
9766 int is_anonymous;
9767
9768 /* Add a symbol associated to this if we haven't seen the namespace
9769 before. Also, add a using directive if it's an anonymous
9770 namespace. */
9771
9772 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9773 {
9774 struct type *type;
9775
9776 type = read_type_die (die, cu);
9777 new_symbol (die, type, cu);
9778
9779 namespace_name (die, &is_anonymous, cu);
9780 if (is_anonymous)
9781 {
9782 const char *previous_prefix = determine_prefix (die, cu);
9783
9784 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9785 NULL, NULL, &objfile->objfile_obstack);
9786 }
9787 }
9788
9789 if (die->child != NULL)
9790 {
9791 struct die_info *child_die = die->child;
9792
9793 while (child_die && child_die->tag)
9794 {
9795 process_die (child_die, cu);
9796 child_die = sibling_die (child_die);
9797 }
9798 }
9799 }
9800
9801 /* Read a Fortran module as type. This DIE can be only a declaration used for
9802 imported module. Still we need that type as local Fortran "use ... only"
9803 declaration imports depend on the created type in determine_prefix. */
9804
9805 static struct type *
9806 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9807 {
9808 struct objfile *objfile = cu->objfile;
9809 char *module_name;
9810 struct type *type;
9811
9812 module_name = dwarf2_name (die, cu);
9813 if (!module_name)
9814 complaint (&symfile_complaints,
9815 _("DW_TAG_module has no name, offset 0x%x"),
9816 die->offset.sect_off);
9817 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9818
9819 /* determine_prefix uses TYPE_TAG_NAME. */
9820 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9821
9822 return set_die_type (die, type, cu);
9823 }
9824
9825 /* Read a Fortran module. */
9826
9827 static void
9828 read_module (struct die_info *die, struct dwarf2_cu *cu)
9829 {
9830 struct die_info *child_die = die->child;
9831
9832 while (child_die && child_die->tag)
9833 {
9834 process_die (child_die, cu);
9835 child_die = sibling_die (child_die);
9836 }
9837 }
9838
9839 /* Return the name of the namespace represented by DIE. Set
9840 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9841 namespace. */
9842
9843 static const char *
9844 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9845 {
9846 struct die_info *current_die;
9847 const char *name = NULL;
9848
9849 /* Loop through the extensions until we find a name. */
9850
9851 for (current_die = die;
9852 current_die != NULL;
9853 current_die = dwarf2_extension (die, &cu))
9854 {
9855 name = dwarf2_name (current_die, cu);
9856 if (name != NULL)
9857 break;
9858 }
9859
9860 /* Is it an anonymous namespace? */
9861
9862 *is_anonymous = (name == NULL);
9863 if (*is_anonymous)
9864 name = CP_ANONYMOUS_NAMESPACE_STR;
9865
9866 return name;
9867 }
9868
9869 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9870 the user defined type vector. */
9871
9872 static struct type *
9873 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9874 {
9875 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9876 struct comp_unit_head *cu_header = &cu->header;
9877 struct type *type;
9878 struct attribute *attr_byte_size;
9879 struct attribute *attr_address_class;
9880 int byte_size, addr_class;
9881 struct type *target_type;
9882
9883 target_type = die_type (die, cu);
9884
9885 /* The die_type call above may have already set the type for this DIE. */
9886 type = get_die_type (die, cu);
9887 if (type)
9888 return type;
9889
9890 type = lookup_pointer_type (target_type);
9891
9892 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9893 if (attr_byte_size)
9894 byte_size = DW_UNSND (attr_byte_size);
9895 else
9896 byte_size = cu_header->addr_size;
9897
9898 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9899 if (attr_address_class)
9900 addr_class = DW_UNSND (attr_address_class);
9901 else
9902 addr_class = DW_ADDR_none;
9903
9904 /* If the pointer size or address class is different than the
9905 default, create a type variant marked as such and set the
9906 length accordingly. */
9907 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9908 {
9909 if (gdbarch_address_class_type_flags_p (gdbarch))
9910 {
9911 int type_flags;
9912
9913 type_flags = gdbarch_address_class_type_flags
9914 (gdbarch, byte_size, addr_class);
9915 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9916 == 0);
9917 type = make_type_with_address_space (type, type_flags);
9918 }
9919 else if (TYPE_LENGTH (type) != byte_size)
9920 {
9921 complaint (&symfile_complaints,
9922 _("invalid pointer size %d"), byte_size);
9923 }
9924 else
9925 {
9926 /* Should we also complain about unhandled address classes? */
9927 }
9928 }
9929
9930 TYPE_LENGTH (type) = byte_size;
9931 return set_die_type (die, type, cu);
9932 }
9933
9934 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9935 the user defined type vector. */
9936
9937 static struct type *
9938 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9939 {
9940 struct type *type;
9941 struct type *to_type;
9942 struct type *domain;
9943
9944 to_type = die_type (die, cu);
9945 domain = die_containing_type (die, cu);
9946
9947 /* The calls above may have already set the type for this DIE. */
9948 type = get_die_type (die, cu);
9949 if (type)
9950 return type;
9951
9952 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9953 type = lookup_methodptr_type (to_type);
9954 else
9955 type = lookup_memberptr_type (to_type, domain);
9956
9957 return set_die_type (die, type, cu);
9958 }
9959
9960 /* Extract all information from a DW_TAG_reference_type DIE and add to
9961 the user defined type vector. */
9962
9963 static struct type *
9964 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9965 {
9966 struct comp_unit_head *cu_header = &cu->header;
9967 struct type *type, *target_type;
9968 struct attribute *attr;
9969
9970 target_type = die_type (die, cu);
9971
9972 /* The die_type call above may have already set the type for this DIE. */
9973 type = get_die_type (die, cu);
9974 if (type)
9975 return type;
9976
9977 type = lookup_reference_type (target_type);
9978 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9979 if (attr)
9980 {
9981 TYPE_LENGTH (type) = DW_UNSND (attr);
9982 }
9983 else
9984 {
9985 TYPE_LENGTH (type) = cu_header->addr_size;
9986 }
9987 return set_die_type (die, type, cu);
9988 }
9989
9990 static struct type *
9991 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
9992 {
9993 struct type *base_type, *cv_type;
9994
9995 base_type = die_type (die, cu);
9996
9997 /* The die_type call above may have already set the type for this DIE. */
9998 cv_type = get_die_type (die, cu);
9999 if (cv_type)
10000 return cv_type;
10001
10002 /* In case the const qualifier is applied to an array type, the element type
10003 is so qualified, not the array type (section 6.7.3 of C99). */
10004 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10005 {
10006 struct type *el_type, *inner_array;
10007
10008 base_type = copy_type (base_type);
10009 inner_array = base_type;
10010
10011 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10012 {
10013 TYPE_TARGET_TYPE (inner_array) =
10014 copy_type (TYPE_TARGET_TYPE (inner_array));
10015 inner_array = TYPE_TARGET_TYPE (inner_array);
10016 }
10017
10018 el_type = TYPE_TARGET_TYPE (inner_array);
10019 TYPE_TARGET_TYPE (inner_array) =
10020 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10021
10022 return set_die_type (die, base_type, cu);
10023 }
10024
10025 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10026 return set_die_type (die, cv_type, cu);
10027 }
10028
10029 static struct type *
10030 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10031 {
10032 struct type *base_type, *cv_type;
10033
10034 base_type = die_type (die, cu);
10035
10036 /* The die_type call above may have already set the type for this DIE. */
10037 cv_type = get_die_type (die, cu);
10038 if (cv_type)
10039 return cv_type;
10040
10041 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10042 return set_die_type (die, cv_type, cu);
10043 }
10044
10045 /* Extract all information from a DW_TAG_string_type DIE and add to
10046 the user defined type vector. It isn't really a user defined type,
10047 but it behaves like one, with other DIE's using an AT_user_def_type
10048 attribute to reference it. */
10049
10050 static struct type *
10051 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10052 {
10053 struct objfile *objfile = cu->objfile;
10054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10055 struct type *type, *range_type, *index_type, *char_type;
10056 struct attribute *attr;
10057 unsigned int length;
10058
10059 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10060 if (attr)
10061 {
10062 length = DW_UNSND (attr);
10063 }
10064 else
10065 {
10066 /* Check for the DW_AT_byte_size attribute. */
10067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10068 if (attr)
10069 {
10070 length = DW_UNSND (attr);
10071 }
10072 else
10073 {
10074 length = 1;
10075 }
10076 }
10077
10078 index_type = objfile_type (objfile)->builtin_int;
10079 range_type = create_range_type (NULL, index_type, 1, length);
10080 char_type = language_string_char_type (cu->language_defn, gdbarch);
10081 type = create_string_type (NULL, char_type, range_type);
10082
10083 return set_die_type (die, type, cu);
10084 }
10085
10086 /* Handle DIES due to C code like:
10087
10088 struct foo
10089 {
10090 int (*funcp)(int a, long l);
10091 int b;
10092 };
10093
10094 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10095
10096 static struct type *
10097 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10098 {
10099 struct objfile *objfile = cu->objfile;
10100 struct type *type; /* Type that this function returns. */
10101 struct type *ftype; /* Function that returns above type. */
10102 struct attribute *attr;
10103
10104 type = die_type (die, cu);
10105
10106 /* The die_type call above may have already set the type for this DIE. */
10107 ftype = get_die_type (die, cu);
10108 if (ftype)
10109 return ftype;
10110
10111 ftype = lookup_function_type (type);
10112
10113 /* All functions in C++, Pascal and Java have prototypes. */
10114 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10115 if ((attr && (DW_UNSND (attr) != 0))
10116 || cu->language == language_cplus
10117 || cu->language == language_java
10118 || cu->language == language_pascal)
10119 TYPE_PROTOTYPED (ftype) = 1;
10120 else if (producer_is_realview (cu->producer))
10121 /* RealView does not emit DW_AT_prototyped. We can not
10122 distinguish prototyped and unprototyped functions; default to
10123 prototyped, since that is more common in modern code (and
10124 RealView warns about unprototyped functions). */
10125 TYPE_PROTOTYPED (ftype) = 1;
10126
10127 /* Store the calling convention in the type if it's available in
10128 the subroutine die. Otherwise set the calling convention to
10129 the default value DW_CC_normal. */
10130 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10131 if (attr)
10132 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10133 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10134 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10135 else
10136 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10137
10138 /* We need to add the subroutine type to the die immediately so
10139 we don't infinitely recurse when dealing with parameters
10140 declared as the same subroutine type. */
10141 set_die_type (die, ftype, cu);
10142
10143 if (die->child != NULL)
10144 {
10145 struct type *void_type = objfile_type (objfile)->builtin_void;
10146 struct die_info *child_die;
10147 int nparams, iparams;
10148
10149 /* Count the number of parameters.
10150 FIXME: GDB currently ignores vararg functions, but knows about
10151 vararg member functions. */
10152 nparams = 0;
10153 child_die = die->child;
10154 while (child_die && child_die->tag)
10155 {
10156 if (child_die->tag == DW_TAG_formal_parameter)
10157 nparams++;
10158 else if (child_die->tag == DW_TAG_unspecified_parameters)
10159 TYPE_VARARGS (ftype) = 1;
10160 child_die = sibling_die (child_die);
10161 }
10162
10163 /* Allocate storage for parameters and fill them in. */
10164 TYPE_NFIELDS (ftype) = nparams;
10165 TYPE_FIELDS (ftype) = (struct field *)
10166 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10167
10168 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10169 even if we error out during the parameters reading below. */
10170 for (iparams = 0; iparams < nparams; iparams++)
10171 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10172
10173 iparams = 0;
10174 child_die = die->child;
10175 while (child_die && child_die->tag)
10176 {
10177 if (child_die->tag == DW_TAG_formal_parameter)
10178 {
10179 struct type *arg_type;
10180
10181 /* DWARF version 2 has no clean way to discern C++
10182 static and non-static member functions. G++ helps
10183 GDB by marking the first parameter for non-static
10184 member functions (which is the this pointer) as
10185 artificial. We pass this information to
10186 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10187
10188 DWARF version 3 added DW_AT_object_pointer, which GCC
10189 4.5 does not yet generate. */
10190 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10191 if (attr)
10192 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10193 else
10194 {
10195 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10196
10197 /* GCC/43521: In java, the formal parameter
10198 "this" is sometimes not marked with DW_AT_artificial. */
10199 if (cu->language == language_java)
10200 {
10201 const char *name = dwarf2_name (child_die, cu);
10202
10203 if (name && !strcmp (name, "this"))
10204 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10205 }
10206 }
10207 arg_type = die_type (child_die, cu);
10208
10209 /* RealView does not mark THIS as const, which the testsuite
10210 expects. GCC marks THIS as const in method definitions,
10211 but not in the class specifications (GCC PR 43053). */
10212 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10213 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10214 {
10215 int is_this = 0;
10216 struct dwarf2_cu *arg_cu = cu;
10217 const char *name = dwarf2_name (child_die, cu);
10218
10219 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10220 if (attr)
10221 {
10222 /* If the compiler emits this, use it. */
10223 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10224 is_this = 1;
10225 }
10226 else if (name && strcmp (name, "this") == 0)
10227 /* Function definitions will have the argument names. */
10228 is_this = 1;
10229 else if (name == NULL && iparams == 0)
10230 /* Declarations may not have the names, so like
10231 elsewhere in GDB, assume an artificial first
10232 argument is "this". */
10233 is_this = 1;
10234
10235 if (is_this)
10236 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10237 arg_type, 0);
10238 }
10239
10240 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10241 iparams++;
10242 }
10243 child_die = sibling_die (child_die);
10244 }
10245 }
10246
10247 return ftype;
10248 }
10249
10250 static struct type *
10251 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10252 {
10253 struct objfile *objfile = cu->objfile;
10254 const char *name = NULL;
10255 struct type *this_type, *target_type;
10256
10257 name = dwarf2_full_name (NULL, die, cu);
10258 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10259 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10260 TYPE_NAME (this_type) = (char *) name;
10261 set_die_type (die, this_type, cu);
10262 target_type = die_type (die, cu);
10263 if (target_type != this_type)
10264 TYPE_TARGET_TYPE (this_type) = target_type;
10265 else
10266 {
10267 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10268 spec and cause infinite loops in GDB. */
10269 complaint (&symfile_complaints,
10270 _("Self-referential DW_TAG_typedef "
10271 "- DIE at 0x%x [in module %s]"),
10272 die->offset.sect_off, objfile->name);
10273 TYPE_TARGET_TYPE (this_type) = NULL;
10274 }
10275 return this_type;
10276 }
10277
10278 /* Find a representation of a given base type and install
10279 it in the TYPE field of the die. */
10280
10281 static struct type *
10282 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10283 {
10284 struct objfile *objfile = cu->objfile;
10285 struct type *type;
10286 struct attribute *attr;
10287 int encoding = 0, size = 0;
10288 char *name;
10289 enum type_code code = TYPE_CODE_INT;
10290 int type_flags = 0;
10291 struct type *target_type = NULL;
10292
10293 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10294 if (attr)
10295 {
10296 encoding = DW_UNSND (attr);
10297 }
10298 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10299 if (attr)
10300 {
10301 size = DW_UNSND (attr);
10302 }
10303 name = dwarf2_name (die, cu);
10304 if (!name)
10305 {
10306 complaint (&symfile_complaints,
10307 _("DW_AT_name missing from DW_TAG_base_type"));
10308 }
10309
10310 switch (encoding)
10311 {
10312 case DW_ATE_address:
10313 /* Turn DW_ATE_address into a void * pointer. */
10314 code = TYPE_CODE_PTR;
10315 type_flags |= TYPE_FLAG_UNSIGNED;
10316 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10317 break;
10318 case DW_ATE_boolean:
10319 code = TYPE_CODE_BOOL;
10320 type_flags |= TYPE_FLAG_UNSIGNED;
10321 break;
10322 case DW_ATE_complex_float:
10323 code = TYPE_CODE_COMPLEX;
10324 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10325 break;
10326 case DW_ATE_decimal_float:
10327 code = TYPE_CODE_DECFLOAT;
10328 break;
10329 case DW_ATE_float:
10330 code = TYPE_CODE_FLT;
10331 break;
10332 case DW_ATE_signed:
10333 break;
10334 case DW_ATE_unsigned:
10335 type_flags |= TYPE_FLAG_UNSIGNED;
10336 if (cu->language == language_fortran
10337 && name
10338 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10339 code = TYPE_CODE_CHAR;
10340 break;
10341 case DW_ATE_signed_char:
10342 if (cu->language == language_ada || cu->language == language_m2
10343 || cu->language == language_pascal
10344 || cu->language == language_fortran)
10345 code = TYPE_CODE_CHAR;
10346 break;
10347 case DW_ATE_unsigned_char:
10348 if (cu->language == language_ada || cu->language == language_m2
10349 || cu->language == language_pascal
10350 || cu->language == language_fortran)
10351 code = TYPE_CODE_CHAR;
10352 type_flags |= TYPE_FLAG_UNSIGNED;
10353 break;
10354 case DW_ATE_UTF:
10355 /* We just treat this as an integer and then recognize the
10356 type by name elsewhere. */
10357 break;
10358
10359 default:
10360 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10361 dwarf_type_encoding_name (encoding));
10362 break;
10363 }
10364
10365 type = init_type (code, size, type_flags, NULL, objfile);
10366 TYPE_NAME (type) = name;
10367 TYPE_TARGET_TYPE (type) = target_type;
10368
10369 if (name && strcmp (name, "char") == 0)
10370 TYPE_NOSIGN (type) = 1;
10371
10372 return set_die_type (die, type, cu);
10373 }
10374
10375 /* Read the given DW_AT_subrange DIE. */
10376
10377 static struct type *
10378 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10379 {
10380 struct type *base_type;
10381 struct type *range_type;
10382 struct attribute *attr;
10383 LONGEST low, high;
10384 int low_default_is_valid;
10385 char *name;
10386 LONGEST negative_mask;
10387
10388 base_type = die_type (die, cu);
10389 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10390 check_typedef (base_type);
10391
10392 /* The die_type call above may have already set the type for this DIE. */
10393 range_type = get_die_type (die, cu);
10394 if (range_type)
10395 return range_type;
10396
10397 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10398 omitting DW_AT_lower_bound. */
10399 switch (cu->language)
10400 {
10401 case language_c:
10402 case language_cplus:
10403 low = 0;
10404 low_default_is_valid = 1;
10405 break;
10406 case language_fortran:
10407 low = 1;
10408 low_default_is_valid = 1;
10409 break;
10410 case language_d:
10411 case language_java:
10412 case language_objc:
10413 low = 0;
10414 low_default_is_valid = (cu->header.version >= 4);
10415 break;
10416 case language_ada:
10417 case language_m2:
10418 case language_pascal:
10419 low = 1;
10420 low_default_is_valid = (cu->header.version >= 4);
10421 break;
10422 default:
10423 low = 0;
10424 low_default_is_valid = 0;
10425 break;
10426 }
10427
10428 /* FIXME: For variable sized arrays either of these could be
10429 a variable rather than a constant value. We'll allow it,
10430 but we don't know how to handle it. */
10431 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10432 if (attr)
10433 low = dwarf2_get_attr_constant_value (attr, low);
10434 else if (!low_default_is_valid)
10435 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10436 "- DIE at 0x%x [in module %s]"),
10437 die->offset.sect_off, cu->objfile->name);
10438
10439 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10440 if (attr)
10441 {
10442 if (attr_form_is_block (attr) || is_ref_attr (attr))
10443 {
10444 /* GCC encodes arrays with unspecified or dynamic length
10445 with a DW_FORM_block1 attribute or a reference attribute.
10446 FIXME: GDB does not yet know how to handle dynamic
10447 arrays properly, treat them as arrays with unspecified
10448 length for now.
10449
10450 FIXME: jimb/2003-09-22: GDB does not really know
10451 how to handle arrays of unspecified length
10452 either; we just represent them as zero-length
10453 arrays. Choose an appropriate upper bound given
10454 the lower bound we've computed above. */
10455 high = low - 1;
10456 }
10457 else
10458 high = dwarf2_get_attr_constant_value (attr, 1);
10459 }
10460 else
10461 {
10462 attr = dwarf2_attr (die, DW_AT_count, cu);
10463 if (attr)
10464 {
10465 int count = dwarf2_get_attr_constant_value (attr, 1);
10466 high = low + count - 1;
10467 }
10468 else
10469 {
10470 /* Unspecified array length. */
10471 high = low - 1;
10472 }
10473 }
10474
10475 /* Dwarf-2 specifications explicitly allows to create subrange types
10476 without specifying a base type.
10477 In that case, the base type must be set to the type of
10478 the lower bound, upper bound or count, in that order, if any of these
10479 three attributes references an object that has a type.
10480 If no base type is found, the Dwarf-2 specifications say that
10481 a signed integer type of size equal to the size of an address should
10482 be used.
10483 For the following C code: `extern char gdb_int [];'
10484 GCC produces an empty range DIE.
10485 FIXME: muller/2010-05-28: Possible references to object for low bound,
10486 high bound or count are not yet handled by this code. */
10487 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10488 {
10489 struct objfile *objfile = cu->objfile;
10490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10491 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10492 struct type *int_type = objfile_type (objfile)->builtin_int;
10493
10494 /* Test "int", "long int", and "long long int" objfile types,
10495 and select the first one having a size above or equal to the
10496 architecture address size. */
10497 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10498 base_type = int_type;
10499 else
10500 {
10501 int_type = objfile_type (objfile)->builtin_long;
10502 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10503 base_type = int_type;
10504 else
10505 {
10506 int_type = objfile_type (objfile)->builtin_long_long;
10507 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10508 base_type = int_type;
10509 }
10510 }
10511 }
10512
10513 negative_mask =
10514 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10515 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10516 low |= negative_mask;
10517 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10518 high |= negative_mask;
10519
10520 range_type = create_range_type (NULL, base_type, low, high);
10521
10522 /* Mark arrays with dynamic length at least as an array of unspecified
10523 length. GDB could check the boundary but before it gets implemented at
10524 least allow accessing the array elements. */
10525 if (attr && attr_form_is_block (attr))
10526 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10527
10528 /* Ada expects an empty array on no boundary attributes. */
10529 if (attr == NULL && cu->language != language_ada)
10530 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10531
10532 name = dwarf2_name (die, cu);
10533 if (name)
10534 TYPE_NAME (range_type) = name;
10535
10536 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10537 if (attr)
10538 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10539
10540 set_die_type (die, range_type, cu);
10541
10542 /* set_die_type should be already done. */
10543 set_descriptive_type (range_type, die, cu);
10544
10545 return range_type;
10546 }
10547
10548 static struct type *
10549 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10550 {
10551 struct type *type;
10552
10553 /* For now, we only support the C meaning of an unspecified type: void. */
10554
10555 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10556 TYPE_NAME (type) = dwarf2_name (die, cu);
10557
10558 return set_die_type (die, type, cu);
10559 }
10560
10561 /* Read a single die and all its descendents. Set the die's sibling
10562 field to NULL; set other fields in the die correctly, and set all
10563 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10564 location of the info_ptr after reading all of those dies. PARENT
10565 is the parent of the die in question. */
10566
10567 static struct die_info *
10568 read_die_and_children (const struct die_reader_specs *reader,
10569 gdb_byte *info_ptr,
10570 gdb_byte **new_info_ptr,
10571 struct die_info *parent)
10572 {
10573 struct die_info *die;
10574 gdb_byte *cur_ptr;
10575 int has_children;
10576
10577 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10578 if (die == NULL)
10579 {
10580 *new_info_ptr = cur_ptr;
10581 return NULL;
10582 }
10583 store_in_ref_table (die, reader->cu);
10584
10585 if (has_children)
10586 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10587 else
10588 {
10589 die->child = NULL;
10590 *new_info_ptr = cur_ptr;
10591 }
10592
10593 die->sibling = NULL;
10594 die->parent = parent;
10595 return die;
10596 }
10597
10598 /* Read a die, all of its descendents, and all of its siblings; set
10599 all of the fields of all of the dies correctly. Arguments are as
10600 in read_die_and_children. */
10601
10602 static struct die_info *
10603 read_die_and_siblings (const struct die_reader_specs *reader,
10604 gdb_byte *info_ptr,
10605 gdb_byte **new_info_ptr,
10606 struct die_info *parent)
10607 {
10608 struct die_info *first_die, *last_sibling;
10609 gdb_byte *cur_ptr;
10610
10611 cur_ptr = info_ptr;
10612 first_die = last_sibling = NULL;
10613
10614 while (1)
10615 {
10616 struct die_info *die
10617 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10618
10619 if (die == NULL)
10620 {
10621 *new_info_ptr = cur_ptr;
10622 return first_die;
10623 }
10624
10625 if (!first_die)
10626 first_die = die;
10627 else
10628 last_sibling->sibling = die;
10629
10630 last_sibling = die;
10631 }
10632 }
10633
10634 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10635 attributes.
10636 The caller is responsible for filling in the extra attributes
10637 and updating (*DIEP)->num_attrs.
10638 Set DIEP to point to a newly allocated die with its information,
10639 except for its child, sibling, and parent fields.
10640 Set HAS_CHILDREN to tell whether the die has children or not. */
10641
10642 static gdb_byte *
10643 read_full_die_1 (const struct die_reader_specs *reader,
10644 struct die_info **diep, gdb_byte *info_ptr,
10645 int *has_children, int num_extra_attrs)
10646 {
10647 unsigned int abbrev_number, bytes_read, i;
10648 sect_offset offset;
10649 struct abbrev_info *abbrev;
10650 struct die_info *die;
10651 struct dwarf2_cu *cu = reader->cu;
10652 bfd *abfd = reader->abfd;
10653
10654 offset.sect_off = info_ptr - reader->buffer;
10655 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10656 info_ptr += bytes_read;
10657 if (!abbrev_number)
10658 {
10659 *diep = NULL;
10660 *has_children = 0;
10661 return info_ptr;
10662 }
10663
10664 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10665 if (!abbrev)
10666 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10667 abbrev_number,
10668 bfd_get_filename (abfd));
10669
10670 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10671 die->offset = offset;
10672 die->tag = abbrev->tag;
10673 die->abbrev = abbrev_number;
10674
10675 /* Make the result usable.
10676 The caller needs to update num_attrs after adding the extra
10677 attributes. */
10678 die->num_attrs = abbrev->num_attrs;
10679
10680 for (i = 0; i < abbrev->num_attrs; ++i)
10681 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10682 info_ptr);
10683
10684 *diep = die;
10685 *has_children = abbrev->has_children;
10686 return info_ptr;
10687 }
10688
10689 /* Read a die and all its attributes.
10690 Set DIEP to point to a newly allocated die with its information,
10691 except for its child, sibling, and parent fields.
10692 Set HAS_CHILDREN to tell whether the die has children or not. */
10693
10694 static gdb_byte *
10695 read_full_die (const struct die_reader_specs *reader,
10696 struct die_info **diep, gdb_byte *info_ptr,
10697 int *has_children)
10698 {
10699 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10700 }
10701
10702 /* In DWARF version 2, the description of the debugging information is
10703 stored in a separate .debug_abbrev section. Before we read any
10704 dies from a section we read in all abbreviations and install them
10705 in a hash table. This function also sets flags in CU describing
10706 the data found in the abbrev table. */
10707
10708 static void
10709 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10710 struct dwarf2_section_info *abbrev_section)
10711
10712 {
10713 bfd *abfd = abbrev_section->asection->owner;
10714 struct comp_unit_head *cu_header = &cu->header;
10715 gdb_byte *abbrev_ptr;
10716 struct abbrev_info *cur_abbrev;
10717 unsigned int abbrev_number, bytes_read, abbrev_name;
10718 unsigned int abbrev_form, hash_number;
10719 struct attr_abbrev *cur_attrs;
10720 unsigned int allocated_attrs;
10721
10722 /* Initialize dwarf2 abbrevs. */
10723 obstack_init (&cu->abbrev_obstack);
10724 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10725 (ABBREV_HASH_SIZE
10726 * sizeof (struct abbrev_info *)));
10727 memset (cu->dwarf2_abbrevs, 0,
10728 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10729
10730 dwarf2_read_section (cu->objfile, abbrev_section);
10731 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10732 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10733 abbrev_ptr += bytes_read;
10734
10735 allocated_attrs = ATTR_ALLOC_CHUNK;
10736 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10737
10738 /* Loop until we reach an abbrev number of 0. */
10739 while (abbrev_number)
10740 {
10741 cur_abbrev = dwarf_alloc_abbrev (cu);
10742
10743 /* read in abbrev header */
10744 cur_abbrev->number = abbrev_number;
10745 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10746 abbrev_ptr += bytes_read;
10747 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10748 abbrev_ptr += 1;
10749
10750 /* now read in declarations */
10751 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10752 abbrev_ptr += bytes_read;
10753 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10754 abbrev_ptr += bytes_read;
10755 while (abbrev_name)
10756 {
10757 if (cur_abbrev->num_attrs == allocated_attrs)
10758 {
10759 allocated_attrs += ATTR_ALLOC_CHUNK;
10760 cur_attrs
10761 = xrealloc (cur_attrs, (allocated_attrs
10762 * sizeof (struct attr_abbrev)));
10763 }
10764
10765 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10766 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10767 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10768 abbrev_ptr += bytes_read;
10769 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10770 abbrev_ptr += bytes_read;
10771 }
10772
10773 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10774 (cur_abbrev->num_attrs
10775 * sizeof (struct attr_abbrev)));
10776 memcpy (cur_abbrev->attrs, cur_attrs,
10777 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10778
10779 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10780 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10781 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10782
10783 /* Get next abbreviation.
10784 Under Irix6 the abbreviations for a compilation unit are not
10785 always properly terminated with an abbrev number of 0.
10786 Exit loop if we encounter an abbreviation which we have
10787 already read (which means we are about to read the abbreviations
10788 for the next compile unit) or if the end of the abbreviation
10789 table is reached. */
10790 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10791 >= abbrev_section->size)
10792 break;
10793 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10794 abbrev_ptr += bytes_read;
10795 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10796 break;
10797 }
10798
10799 xfree (cur_attrs);
10800 }
10801
10802 /* Release the memory used by the abbrev table for a compilation unit. */
10803
10804 static void
10805 dwarf2_free_abbrev_table (void *ptr_to_cu)
10806 {
10807 struct dwarf2_cu *cu = ptr_to_cu;
10808
10809 obstack_free (&cu->abbrev_obstack, NULL);
10810 cu->dwarf2_abbrevs = NULL;
10811 }
10812
10813 /* Lookup an abbrev_info structure in the abbrev hash table. */
10814
10815 static struct abbrev_info *
10816 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10817 {
10818 unsigned int hash_number;
10819 struct abbrev_info *abbrev;
10820
10821 hash_number = number % ABBREV_HASH_SIZE;
10822 abbrev = cu->dwarf2_abbrevs[hash_number];
10823
10824 while (abbrev)
10825 {
10826 if (abbrev->number == number)
10827 return abbrev;
10828 else
10829 abbrev = abbrev->next;
10830 }
10831 return NULL;
10832 }
10833
10834 /* Returns nonzero if TAG represents a type that we might generate a partial
10835 symbol for. */
10836
10837 static int
10838 is_type_tag_for_partial (int tag)
10839 {
10840 switch (tag)
10841 {
10842 #if 0
10843 /* Some types that would be reasonable to generate partial symbols for,
10844 that we don't at present. */
10845 case DW_TAG_array_type:
10846 case DW_TAG_file_type:
10847 case DW_TAG_ptr_to_member_type:
10848 case DW_TAG_set_type:
10849 case DW_TAG_string_type:
10850 case DW_TAG_subroutine_type:
10851 #endif
10852 case DW_TAG_base_type:
10853 case DW_TAG_class_type:
10854 case DW_TAG_interface_type:
10855 case DW_TAG_enumeration_type:
10856 case DW_TAG_structure_type:
10857 case DW_TAG_subrange_type:
10858 case DW_TAG_typedef:
10859 case DW_TAG_union_type:
10860 return 1;
10861 default:
10862 return 0;
10863 }
10864 }
10865
10866 /* Load all DIEs that are interesting for partial symbols into memory. */
10867
10868 static struct partial_die_info *
10869 load_partial_dies (const struct die_reader_specs *reader,
10870 gdb_byte *info_ptr, int building_psymtab)
10871 {
10872 struct dwarf2_cu *cu = reader->cu;
10873 struct objfile *objfile = cu->objfile;
10874 struct partial_die_info *part_die;
10875 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10876 struct abbrev_info *abbrev;
10877 unsigned int bytes_read;
10878 unsigned int load_all = 0;
10879 int nesting_level = 1;
10880
10881 parent_die = NULL;
10882 last_die = NULL;
10883
10884 gdb_assert (cu->per_cu != NULL);
10885 if (cu->per_cu->load_all_dies)
10886 load_all = 1;
10887
10888 cu->partial_dies
10889 = htab_create_alloc_ex (cu->header.length / 12,
10890 partial_die_hash,
10891 partial_die_eq,
10892 NULL,
10893 &cu->comp_unit_obstack,
10894 hashtab_obstack_allocate,
10895 dummy_obstack_deallocate);
10896
10897 part_die = obstack_alloc (&cu->comp_unit_obstack,
10898 sizeof (struct partial_die_info));
10899
10900 while (1)
10901 {
10902 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10903
10904 /* A NULL abbrev means the end of a series of children. */
10905 if (abbrev == NULL)
10906 {
10907 if (--nesting_level == 0)
10908 {
10909 /* PART_DIE was probably the last thing allocated on the
10910 comp_unit_obstack, so we could call obstack_free
10911 here. We don't do that because the waste is small,
10912 and will be cleaned up when we're done with this
10913 compilation unit. This way, we're also more robust
10914 against other users of the comp_unit_obstack. */
10915 return first_die;
10916 }
10917 info_ptr += bytes_read;
10918 last_die = parent_die;
10919 parent_die = parent_die->die_parent;
10920 continue;
10921 }
10922
10923 /* Check for template arguments. We never save these; if
10924 they're seen, we just mark the parent, and go on our way. */
10925 if (parent_die != NULL
10926 && cu->language == language_cplus
10927 && (abbrev->tag == DW_TAG_template_type_param
10928 || abbrev->tag == DW_TAG_template_value_param))
10929 {
10930 parent_die->has_template_arguments = 1;
10931
10932 if (!load_all)
10933 {
10934 /* We don't need a partial DIE for the template argument. */
10935 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10936 continue;
10937 }
10938 }
10939
10940 /* We only recurse into c++ subprograms looking for template arguments.
10941 Skip their other children. */
10942 if (!load_all
10943 && cu->language == language_cplus
10944 && parent_die != NULL
10945 && parent_die->tag == DW_TAG_subprogram)
10946 {
10947 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10948 continue;
10949 }
10950
10951 /* Check whether this DIE is interesting enough to save. Normally
10952 we would not be interested in members here, but there may be
10953 later variables referencing them via DW_AT_specification (for
10954 static members). */
10955 if (!load_all
10956 && !is_type_tag_for_partial (abbrev->tag)
10957 && abbrev->tag != DW_TAG_constant
10958 && abbrev->tag != DW_TAG_enumerator
10959 && abbrev->tag != DW_TAG_subprogram
10960 && abbrev->tag != DW_TAG_lexical_block
10961 && abbrev->tag != DW_TAG_variable
10962 && abbrev->tag != DW_TAG_namespace
10963 && abbrev->tag != DW_TAG_module
10964 && abbrev->tag != DW_TAG_member
10965 && abbrev->tag != DW_TAG_imported_unit)
10966 {
10967 /* Otherwise we skip to the next sibling, if any. */
10968 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10969 continue;
10970 }
10971
10972 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10973 info_ptr);
10974
10975 /* This two-pass algorithm for processing partial symbols has a
10976 high cost in cache pressure. Thus, handle some simple cases
10977 here which cover the majority of C partial symbols. DIEs
10978 which neither have specification tags in them, nor could have
10979 specification tags elsewhere pointing at them, can simply be
10980 processed and discarded.
10981
10982 This segment is also optional; scan_partial_symbols and
10983 add_partial_symbol will handle these DIEs if we chain
10984 them in normally. When compilers which do not emit large
10985 quantities of duplicate debug information are more common,
10986 this code can probably be removed. */
10987
10988 /* Any complete simple types at the top level (pretty much all
10989 of them, for a language without namespaces), can be processed
10990 directly. */
10991 if (parent_die == NULL
10992 && part_die->has_specification == 0
10993 && part_die->is_declaration == 0
10994 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
10995 || part_die->tag == DW_TAG_base_type
10996 || part_die->tag == DW_TAG_subrange_type))
10997 {
10998 if (building_psymtab && part_die->name != NULL)
10999 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11000 VAR_DOMAIN, LOC_TYPEDEF,
11001 &objfile->static_psymbols,
11002 0, (CORE_ADDR) 0, cu->language, objfile);
11003 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11004 continue;
11005 }
11006
11007 /* The exception for DW_TAG_typedef with has_children above is
11008 a workaround of GCC PR debug/47510. In the case of this complaint
11009 type_name_no_tag_or_error will error on such types later.
11010
11011 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11012 it could not find the child DIEs referenced later, this is checked
11013 above. In correct DWARF DW_TAG_typedef should have no children. */
11014
11015 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11016 complaint (&symfile_complaints,
11017 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11018 "- DIE at 0x%x [in module %s]"),
11019 part_die->offset.sect_off, objfile->name);
11020
11021 /* If we're at the second level, and we're an enumerator, and
11022 our parent has no specification (meaning possibly lives in a
11023 namespace elsewhere), then we can add the partial symbol now
11024 instead of queueing it. */
11025 if (part_die->tag == DW_TAG_enumerator
11026 && parent_die != NULL
11027 && parent_die->die_parent == NULL
11028 && parent_die->tag == DW_TAG_enumeration_type
11029 && parent_die->has_specification == 0)
11030 {
11031 if (part_die->name == NULL)
11032 complaint (&symfile_complaints,
11033 _("malformed enumerator DIE ignored"));
11034 else if (building_psymtab)
11035 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11036 VAR_DOMAIN, LOC_CONST,
11037 (cu->language == language_cplus
11038 || cu->language == language_java)
11039 ? &objfile->global_psymbols
11040 : &objfile->static_psymbols,
11041 0, (CORE_ADDR) 0, cu->language, objfile);
11042
11043 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11044 continue;
11045 }
11046
11047 /* We'll save this DIE so link it in. */
11048 part_die->die_parent = parent_die;
11049 part_die->die_sibling = NULL;
11050 part_die->die_child = NULL;
11051
11052 if (last_die && last_die == parent_die)
11053 last_die->die_child = part_die;
11054 else if (last_die)
11055 last_die->die_sibling = part_die;
11056
11057 last_die = part_die;
11058
11059 if (first_die == NULL)
11060 first_die = part_die;
11061
11062 /* Maybe add the DIE to the hash table. Not all DIEs that we
11063 find interesting need to be in the hash table, because we
11064 also have the parent/sibling/child chains; only those that we
11065 might refer to by offset later during partial symbol reading.
11066
11067 For now this means things that might have be the target of a
11068 DW_AT_specification, DW_AT_abstract_origin, or
11069 DW_AT_extension. DW_AT_extension will refer only to
11070 namespaces; DW_AT_abstract_origin refers to functions (and
11071 many things under the function DIE, but we do not recurse
11072 into function DIEs during partial symbol reading) and
11073 possibly variables as well; DW_AT_specification refers to
11074 declarations. Declarations ought to have the DW_AT_declaration
11075 flag. It happens that GCC forgets to put it in sometimes, but
11076 only for functions, not for types.
11077
11078 Adding more things than necessary to the hash table is harmless
11079 except for the performance cost. Adding too few will result in
11080 wasted time in find_partial_die, when we reread the compilation
11081 unit with load_all_dies set. */
11082
11083 if (load_all
11084 || abbrev->tag == DW_TAG_constant
11085 || abbrev->tag == DW_TAG_subprogram
11086 || abbrev->tag == DW_TAG_variable
11087 || abbrev->tag == DW_TAG_namespace
11088 || part_die->is_declaration)
11089 {
11090 void **slot;
11091
11092 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11093 part_die->offset.sect_off, INSERT);
11094 *slot = part_die;
11095 }
11096
11097 part_die = obstack_alloc (&cu->comp_unit_obstack,
11098 sizeof (struct partial_die_info));
11099
11100 /* For some DIEs we want to follow their children (if any). For C
11101 we have no reason to follow the children of structures; for other
11102 languages we have to, so that we can get at method physnames
11103 to infer fully qualified class names, for DW_AT_specification,
11104 and for C++ template arguments. For C++, we also look one level
11105 inside functions to find template arguments (if the name of the
11106 function does not already contain the template arguments).
11107
11108 For Ada, we need to scan the children of subprograms and lexical
11109 blocks as well because Ada allows the definition of nested
11110 entities that could be interesting for the debugger, such as
11111 nested subprograms for instance. */
11112 if (last_die->has_children
11113 && (load_all
11114 || last_die->tag == DW_TAG_namespace
11115 || last_die->tag == DW_TAG_module
11116 || last_die->tag == DW_TAG_enumeration_type
11117 || (cu->language == language_cplus
11118 && last_die->tag == DW_TAG_subprogram
11119 && (last_die->name == NULL
11120 || strchr (last_die->name, '<') == NULL))
11121 || (cu->language != language_c
11122 && (last_die->tag == DW_TAG_class_type
11123 || last_die->tag == DW_TAG_interface_type
11124 || last_die->tag == DW_TAG_structure_type
11125 || last_die->tag == DW_TAG_union_type))
11126 || (cu->language == language_ada
11127 && (last_die->tag == DW_TAG_subprogram
11128 || last_die->tag == DW_TAG_lexical_block))))
11129 {
11130 nesting_level++;
11131 parent_die = last_die;
11132 continue;
11133 }
11134
11135 /* Otherwise we skip to the next sibling, if any. */
11136 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11137
11138 /* Back to the top, do it again. */
11139 }
11140 }
11141
11142 /* Read a minimal amount of information into the minimal die structure. */
11143
11144 static gdb_byte *
11145 read_partial_die (const struct die_reader_specs *reader,
11146 struct partial_die_info *part_die,
11147 struct abbrev_info *abbrev, unsigned int abbrev_len,
11148 gdb_byte *info_ptr)
11149 {
11150 struct dwarf2_cu *cu = reader->cu;
11151 struct objfile *objfile = cu->objfile;
11152 gdb_byte *buffer = reader->buffer;
11153 unsigned int i;
11154 struct attribute attr;
11155 int has_low_pc_attr = 0;
11156 int has_high_pc_attr = 0;
11157 int high_pc_relative = 0;
11158
11159 memset (part_die, 0, sizeof (struct partial_die_info));
11160
11161 part_die->offset.sect_off = info_ptr - buffer;
11162
11163 info_ptr += abbrev_len;
11164
11165 if (abbrev == NULL)
11166 return info_ptr;
11167
11168 part_die->tag = abbrev->tag;
11169 part_die->has_children = abbrev->has_children;
11170
11171 for (i = 0; i < abbrev->num_attrs; ++i)
11172 {
11173 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11174
11175 /* Store the data if it is of an attribute we want to keep in a
11176 partial symbol table. */
11177 switch (attr.name)
11178 {
11179 case DW_AT_name:
11180 switch (part_die->tag)
11181 {
11182 case DW_TAG_compile_unit:
11183 case DW_TAG_partial_unit:
11184 case DW_TAG_type_unit:
11185 /* Compilation units have a DW_AT_name that is a filename, not
11186 a source language identifier. */
11187 case DW_TAG_enumeration_type:
11188 case DW_TAG_enumerator:
11189 /* These tags always have simple identifiers already; no need
11190 to canonicalize them. */
11191 part_die->name = DW_STRING (&attr);
11192 break;
11193 default:
11194 part_die->name
11195 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11196 &objfile->objfile_obstack);
11197 break;
11198 }
11199 break;
11200 case DW_AT_linkage_name:
11201 case DW_AT_MIPS_linkage_name:
11202 /* Note that both forms of linkage name might appear. We
11203 assume they will be the same, and we only store the last
11204 one we see. */
11205 if (cu->language == language_ada)
11206 part_die->name = DW_STRING (&attr);
11207 part_die->linkage_name = DW_STRING (&attr);
11208 break;
11209 case DW_AT_low_pc:
11210 has_low_pc_attr = 1;
11211 part_die->lowpc = DW_ADDR (&attr);
11212 break;
11213 case DW_AT_high_pc:
11214 has_high_pc_attr = 1;
11215 if (attr.form == DW_FORM_addr
11216 || attr.form == DW_FORM_GNU_addr_index)
11217 part_die->highpc = DW_ADDR (&attr);
11218 else
11219 {
11220 high_pc_relative = 1;
11221 part_die->highpc = DW_UNSND (&attr);
11222 }
11223 break;
11224 case DW_AT_location:
11225 /* Support the .debug_loc offsets. */
11226 if (attr_form_is_block (&attr))
11227 {
11228 part_die->d.locdesc = DW_BLOCK (&attr);
11229 }
11230 else if (attr_form_is_section_offset (&attr))
11231 {
11232 dwarf2_complex_location_expr_complaint ();
11233 }
11234 else
11235 {
11236 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11237 "partial symbol information");
11238 }
11239 break;
11240 case DW_AT_external:
11241 part_die->is_external = DW_UNSND (&attr);
11242 break;
11243 case DW_AT_declaration:
11244 part_die->is_declaration = DW_UNSND (&attr);
11245 break;
11246 case DW_AT_type:
11247 part_die->has_type = 1;
11248 break;
11249 case DW_AT_abstract_origin:
11250 case DW_AT_specification:
11251 case DW_AT_extension:
11252 part_die->has_specification = 1;
11253 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11254 break;
11255 case DW_AT_sibling:
11256 /* Ignore absolute siblings, they might point outside of
11257 the current compile unit. */
11258 if (attr.form == DW_FORM_ref_addr)
11259 complaint (&symfile_complaints,
11260 _("ignoring absolute DW_AT_sibling"));
11261 else
11262 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11263 break;
11264 case DW_AT_byte_size:
11265 part_die->has_byte_size = 1;
11266 break;
11267 case DW_AT_calling_convention:
11268 /* DWARF doesn't provide a way to identify a program's source-level
11269 entry point. DW_AT_calling_convention attributes are only meant
11270 to describe functions' calling conventions.
11271
11272 However, because it's a necessary piece of information in
11273 Fortran, and because DW_CC_program is the only piece of debugging
11274 information whose definition refers to a 'main program' at all,
11275 several compilers have begun marking Fortran main programs with
11276 DW_CC_program --- even when those functions use the standard
11277 calling conventions.
11278
11279 So until DWARF specifies a way to provide this information and
11280 compilers pick up the new representation, we'll support this
11281 practice. */
11282 if (DW_UNSND (&attr) == DW_CC_program
11283 && cu->language == language_fortran)
11284 {
11285 set_main_name (part_die->name);
11286
11287 /* As this DIE has a static linkage the name would be difficult
11288 to look up later. */
11289 language_of_main = language_fortran;
11290 }
11291 break;
11292 case DW_AT_inline:
11293 if (DW_UNSND (&attr) == DW_INL_inlined
11294 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11295 part_die->may_be_inlined = 1;
11296 break;
11297
11298 case DW_AT_import:
11299 if (part_die->tag == DW_TAG_imported_unit)
11300 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11301 break;
11302
11303 default:
11304 break;
11305 }
11306 }
11307
11308 if (high_pc_relative)
11309 part_die->highpc += part_die->lowpc;
11310
11311 if (has_low_pc_attr && has_high_pc_attr)
11312 {
11313 /* When using the GNU linker, .gnu.linkonce. sections are used to
11314 eliminate duplicate copies of functions and vtables and such.
11315 The linker will arbitrarily choose one and discard the others.
11316 The AT_*_pc values for such functions refer to local labels in
11317 these sections. If the section from that file was discarded, the
11318 labels are not in the output, so the relocs get a value of 0.
11319 If this is a discarded function, mark the pc bounds as invalid,
11320 so that GDB will ignore it. */
11321 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11322 {
11323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11324
11325 complaint (&symfile_complaints,
11326 _("DW_AT_low_pc %s is zero "
11327 "for DIE at 0x%x [in module %s]"),
11328 paddress (gdbarch, part_die->lowpc),
11329 part_die->offset.sect_off, objfile->name);
11330 }
11331 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11332 else if (part_die->lowpc >= part_die->highpc)
11333 {
11334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11335
11336 complaint (&symfile_complaints,
11337 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11338 "for DIE at 0x%x [in module %s]"),
11339 paddress (gdbarch, part_die->lowpc),
11340 paddress (gdbarch, part_die->highpc),
11341 part_die->offset.sect_off, objfile->name);
11342 }
11343 else
11344 part_die->has_pc_info = 1;
11345 }
11346
11347 return info_ptr;
11348 }
11349
11350 /* Find a cached partial DIE at OFFSET in CU. */
11351
11352 static struct partial_die_info *
11353 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11354 {
11355 struct partial_die_info *lookup_die = NULL;
11356 struct partial_die_info part_die;
11357
11358 part_die.offset = offset;
11359 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11360 offset.sect_off);
11361
11362 return lookup_die;
11363 }
11364
11365 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11366 except in the case of .debug_types DIEs which do not reference
11367 outside their CU (they do however referencing other types via
11368 DW_FORM_ref_sig8). */
11369
11370 static struct partial_die_info *
11371 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11372 {
11373 struct objfile *objfile = cu->objfile;
11374 struct dwarf2_per_cu_data *per_cu = NULL;
11375 struct partial_die_info *pd = NULL;
11376
11377 if (offset_in_cu_p (&cu->header, offset))
11378 {
11379 pd = find_partial_die_in_comp_unit (offset, cu);
11380 if (pd != NULL)
11381 return pd;
11382 /* We missed recording what we needed.
11383 Load all dies and try again. */
11384 per_cu = cu->per_cu;
11385 }
11386 else
11387 {
11388 /* TUs don't reference other CUs/TUs (except via type signatures). */
11389 if (cu->per_cu->is_debug_types)
11390 {
11391 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11392 " external reference to offset 0x%lx [in module %s].\n"),
11393 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11394 bfd_get_filename (objfile->obfd));
11395 }
11396 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11397
11398 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11399 load_partial_comp_unit (per_cu);
11400
11401 per_cu->cu->last_used = 0;
11402 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11403 }
11404
11405 /* If we didn't find it, and not all dies have been loaded,
11406 load them all and try again. */
11407
11408 if (pd == NULL && per_cu->load_all_dies == 0)
11409 {
11410 per_cu->load_all_dies = 1;
11411
11412 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11413 THIS_CU->cu may already be in use. So we can't just free it and
11414 replace its DIEs with the ones we read in. Instead, we leave those
11415 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11416 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11417 set. */
11418 load_partial_comp_unit (per_cu);
11419
11420 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11421 }
11422
11423 if (pd == NULL)
11424 internal_error (__FILE__, __LINE__,
11425 _("could not find partial DIE 0x%x "
11426 "in cache [from module %s]\n"),
11427 offset.sect_off, bfd_get_filename (objfile->obfd));
11428 return pd;
11429 }
11430
11431 /* See if we can figure out if the class lives in a namespace. We do
11432 this by looking for a member function; its demangled name will
11433 contain namespace info, if there is any. */
11434
11435 static void
11436 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11437 struct dwarf2_cu *cu)
11438 {
11439 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11440 what template types look like, because the demangler
11441 frequently doesn't give the same name as the debug info. We
11442 could fix this by only using the demangled name to get the
11443 prefix (but see comment in read_structure_type). */
11444
11445 struct partial_die_info *real_pdi;
11446 struct partial_die_info *child_pdi;
11447
11448 /* If this DIE (this DIE's specification, if any) has a parent, then
11449 we should not do this. We'll prepend the parent's fully qualified
11450 name when we create the partial symbol. */
11451
11452 real_pdi = struct_pdi;
11453 while (real_pdi->has_specification)
11454 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11455
11456 if (real_pdi->die_parent != NULL)
11457 return;
11458
11459 for (child_pdi = struct_pdi->die_child;
11460 child_pdi != NULL;
11461 child_pdi = child_pdi->die_sibling)
11462 {
11463 if (child_pdi->tag == DW_TAG_subprogram
11464 && child_pdi->linkage_name != NULL)
11465 {
11466 char *actual_class_name
11467 = language_class_name_from_physname (cu->language_defn,
11468 child_pdi->linkage_name);
11469 if (actual_class_name != NULL)
11470 {
11471 struct_pdi->name
11472 = obsavestring (actual_class_name,
11473 strlen (actual_class_name),
11474 &cu->objfile->objfile_obstack);
11475 xfree (actual_class_name);
11476 }
11477 break;
11478 }
11479 }
11480 }
11481
11482 /* Adjust PART_DIE before generating a symbol for it. This function
11483 may set the is_external flag or change the DIE's name. */
11484
11485 static void
11486 fixup_partial_die (struct partial_die_info *part_die,
11487 struct dwarf2_cu *cu)
11488 {
11489 /* Once we've fixed up a die, there's no point in doing so again.
11490 This also avoids a memory leak if we were to call
11491 guess_partial_die_structure_name multiple times. */
11492 if (part_die->fixup_called)
11493 return;
11494
11495 /* If we found a reference attribute and the DIE has no name, try
11496 to find a name in the referred to DIE. */
11497
11498 if (part_die->name == NULL && part_die->has_specification)
11499 {
11500 struct partial_die_info *spec_die;
11501
11502 spec_die = find_partial_die (part_die->spec_offset, cu);
11503
11504 fixup_partial_die (spec_die, cu);
11505
11506 if (spec_die->name)
11507 {
11508 part_die->name = spec_die->name;
11509
11510 /* Copy DW_AT_external attribute if it is set. */
11511 if (spec_die->is_external)
11512 part_die->is_external = spec_die->is_external;
11513 }
11514 }
11515
11516 /* Set default names for some unnamed DIEs. */
11517
11518 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11519 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11520
11521 /* If there is no parent die to provide a namespace, and there are
11522 children, see if we can determine the namespace from their linkage
11523 name. */
11524 if (cu->language == language_cplus
11525 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11526 && part_die->die_parent == NULL
11527 && part_die->has_children
11528 && (part_die->tag == DW_TAG_class_type
11529 || part_die->tag == DW_TAG_structure_type
11530 || part_die->tag == DW_TAG_union_type))
11531 guess_partial_die_structure_name (part_die, cu);
11532
11533 /* GCC might emit a nameless struct or union that has a linkage
11534 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11535 if (part_die->name == NULL
11536 && (part_die->tag == DW_TAG_class_type
11537 || part_die->tag == DW_TAG_interface_type
11538 || part_die->tag == DW_TAG_structure_type
11539 || part_die->tag == DW_TAG_union_type)
11540 && part_die->linkage_name != NULL)
11541 {
11542 char *demangled;
11543
11544 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11545 if (demangled)
11546 {
11547 const char *base;
11548
11549 /* Strip any leading namespaces/classes, keep only the base name.
11550 DW_AT_name for named DIEs does not contain the prefixes. */
11551 base = strrchr (demangled, ':');
11552 if (base && base > demangled && base[-1] == ':')
11553 base++;
11554 else
11555 base = demangled;
11556
11557 part_die->name = obsavestring (base, strlen (base),
11558 &cu->objfile->objfile_obstack);
11559 xfree (demangled);
11560 }
11561 }
11562
11563 part_die->fixup_called = 1;
11564 }
11565
11566 /* Read an attribute value described by an attribute form. */
11567
11568 static gdb_byte *
11569 read_attribute_value (const struct die_reader_specs *reader,
11570 struct attribute *attr, unsigned form,
11571 gdb_byte *info_ptr)
11572 {
11573 struct dwarf2_cu *cu = reader->cu;
11574 bfd *abfd = reader->abfd;
11575 struct comp_unit_head *cu_header = &cu->header;
11576 unsigned int bytes_read;
11577 struct dwarf_block *blk;
11578
11579 attr->form = form;
11580 switch (form)
11581 {
11582 case DW_FORM_ref_addr:
11583 if (cu->header.version == 2)
11584 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11585 else
11586 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11587 &cu->header, &bytes_read);
11588 info_ptr += bytes_read;
11589 break;
11590 case DW_FORM_addr:
11591 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11592 info_ptr += bytes_read;
11593 break;
11594 case DW_FORM_block2:
11595 blk = dwarf_alloc_block (cu);
11596 blk->size = read_2_bytes (abfd, info_ptr);
11597 info_ptr += 2;
11598 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11599 info_ptr += blk->size;
11600 DW_BLOCK (attr) = blk;
11601 break;
11602 case DW_FORM_block4:
11603 blk = dwarf_alloc_block (cu);
11604 blk->size = read_4_bytes (abfd, info_ptr);
11605 info_ptr += 4;
11606 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11607 info_ptr += blk->size;
11608 DW_BLOCK (attr) = blk;
11609 break;
11610 case DW_FORM_data2:
11611 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11612 info_ptr += 2;
11613 break;
11614 case DW_FORM_data4:
11615 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11616 info_ptr += 4;
11617 break;
11618 case DW_FORM_data8:
11619 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11620 info_ptr += 8;
11621 break;
11622 case DW_FORM_sec_offset:
11623 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11624 info_ptr += bytes_read;
11625 break;
11626 case DW_FORM_string:
11627 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11628 DW_STRING_IS_CANONICAL (attr) = 0;
11629 info_ptr += bytes_read;
11630 break;
11631 case DW_FORM_strp:
11632 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11633 &bytes_read);
11634 DW_STRING_IS_CANONICAL (attr) = 0;
11635 info_ptr += bytes_read;
11636 break;
11637 case DW_FORM_exprloc:
11638 case DW_FORM_block:
11639 blk = dwarf_alloc_block (cu);
11640 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11641 info_ptr += bytes_read;
11642 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11643 info_ptr += blk->size;
11644 DW_BLOCK (attr) = blk;
11645 break;
11646 case DW_FORM_block1:
11647 blk = dwarf_alloc_block (cu);
11648 blk->size = read_1_byte (abfd, info_ptr);
11649 info_ptr += 1;
11650 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11651 info_ptr += blk->size;
11652 DW_BLOCK (attr) = blk;
11653 break;
11654 case DW_FORM_data1:
11655 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11656 info_ptr += 1;
11657 break;
11658 case DW_FORM_flag:
11659 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11660 info_ptr += 1;
11661 break;
11662 case DW_FORM_flag_present:
11663 DW_UNSND (attr) = 1;
11664 break;
11665 case DW_FORM_sdata:
11666 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11667 info_ptr += bytes_read;
11668 break;
11669 case DW_FORM_udata:
11670 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11671 info_ptr += bytes_read;
11672 break;
11673 case DW_FORM_ref1:
11674 DW_UNSND (attr) = (cu->header.offset.sect_off
11675 + read_1_byte (abfd, info_ptr));
11676 info_ptr += 1;
11677 break;
11678 case DW_FORM_ref2:
11679 DW_UNSND (attr) = (cu->header.offset.sect_off
11680 + read_2_bytes (abfd, info_ptr));
11681 info_ptr += 2;
11682 break;
11683 case DW_FORM_ref4:
11684 DW_UNSND (attr) = (cu->header.offset.sect_off
11685 + read_4_bytes (abfd, info_ptr));
11686 info_ptr += 4;
11687 break;
11688 case DW_FORM_ref8:
11689 DW_UNSND (attr) = (cu->header.offset.sect_off
11690 + read_8_bytes (abfd, info_ptr));
11691 info_ptr += 8;
11692 break;
11693 case DW_FORM_ref_sig8:
11694 /* Convert the signature to something we can record in DW_UNSND
11695 for later lookup.
11696 NOTE: This is NULL if the type wasn't found. */
11697 DW_SIGNATURED_TYPE (attr) =
11698 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11699 info_ptr += 8;
11700 break;
11701 case DW_FORM_ref_udata:
11702 DW_UNSND (attr) = (cu->header.offset.sect_off
11703 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11704 info_ptr += bytes_read;
11705 break;
11706 case DW_FORM_indirect:
11707 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11708 info_ptr += bytes_read;
11709 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11710 break;
11711 case DW_FORM_GNU_addr_index:
11712 if (reader->dwo_file == NULL)
11713 {
11714 /* For now flag a hard error.
11715 Later we can turn this into a complaint. */
11716 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11717 dwarf_form_name (form),
11718 bfd_get_filename (abfd));
11719 }
11720 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11721 info_ptr += bytes_read;
11722 break;
11723 case DW_FORM_GNU_str_index:
11724 if (reader->dwo_file == NULL)
11725 {
11726 /* For now flag a hard error.
11727 Later we can turn this into a complaint if warranted. */
11728 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11729 dwarf_form_name (form),
11730 bfd_get_filename (abfd));
11731 }
11732 {
11733 ULONGEST str_index =
11734 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11735
11736 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11737 DW_STRING_IS_CANONICAL (attr) = 0;
11738 info_ptr += bytes_read;
11739 }
11740 break;
11741 default:
11742 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11743 dwarf_form_name (form),
11744 bfd_get_filename (abfd));
11745 }
11746
11747 /* We have seen instances where the compiler tried to emit a byte
11748 size attribute of -1 which ended up being encoded as an unsigned
11749 0xffffffff. Although 0xffffffff is technically a valid size value,
11750 an object of this size seems pretty unlikely so we can relatively
11751 safely treat these cases as if the size attribute was invalid and
11752 treat them as zero by default. */
11753 if (attr->name == DW_AT_byte_size
11754 && form == DW_FORM_data4
11755 && DW_UNSND (attr) >= 0xffffffff)
11756 {
11757 complaint
11758 (&symfile_complaints,
11759 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11760 hex_string (DW_UNSND (attr)));
11761 DW_UNSND (attr) = 0;
11762 }
11763
11764 return info_ptr;
11765 }
11766
11767 /* Read an attribute described by an abbreviated attribute. */
11768
11769 static gdb_byte *
11770 read_attribute (const struct die_reader_specs *reader,
11771 struct attribute *attr, struct attr_abbrev *abbrev,
11772 gdb_byte *info_ptr)
11773 {
11774 attr->name = abbrev->name;
11775 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11776 }
11777
11778 /* Read dwarf information from a buffer. */
11779
11780 static unsigned int
11781 read_1_byte (bfd *abfd, gdb_byte *buf)
11782 {
11783 return bfd_get_8 (abfd, buf);
11784 }
11785
11786 static int
11787 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11788 {
11789 return bfd_get_signed_8 (abfd, buf);
11790 }
11791
11792 static unsigned int
11793 read_2_bytes (bfd *abfd, gdb_byte *buf)
11794 {
11795 return bfd_get_16 (abfd, buf);
11796 }
11797
11798 static int
11799 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11800 {
11801 return bfd_get_signed_16 (abfd, buf);
11802 }
11803
11804 static unsigned int
11805 read_4_bytes (bfd *abfd, gdb_byte *buf)
11806 {
11807 return bfd_get_32 (abfd, buf);
11808 }
11809
11810 static int
11811 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11812 {
11813 return bfd_get_signed_32 (abfd, buf);
11814 }
11815
11816 static ULONGEST
11817 read_8_bytes (bfd *abfd, gdb_byte *buf)
11818 {
11819 return bfd_get_64 (abfd, buf);
11820 }
11821
11822 static CORE_ADDR
11823 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11824 unsigned int *bytes_read)
11825 {
11826 struct comp_unit_head *cu_header = &cu->header;
11827 CORE_ADDR retval = 0;
11828
11829 if (cu_header->signed_addr_p)
11830 {
11831 switch (cu_header->addr_size)
11832 {
11833 case 2:
11834 retval = bfd_get_signed_16 (abfd, buf);
11835 break;
11836 case 4:
11837 retval = bfd_get_signed_32 (abfd, buf);
11838 break;
11839 case 8:
11840 retval = bfd_get_signed_64 (abfd, buf);
11841 break;
11842 default:
11843 internal_error (__FILE__, __LINE__,
11844 _("read_address: bad switch, signed [in module %s]"),
11845 bfd_get_filename (abfd));
11846 }
11847 }
11848 else
11849 {
11850 switch (cu_header->addr_size)
11851 {
11852 case 2:
11853 retval = bfd_get_16 (abfd, buf);
11854 break;
11855 case 4:
11856 retval = bfd_get_32 (abfd, buf);
11857 break;
11858 case 8:
11859 retval = bfd_get_64 (abfd, buf);
11860 break;
11861 default:
11862 internal_error (__FILE__, __LINE__,
11863 _("read_address: bad switch, "
11864 "unsigned [in module %s]"),
11865 bfd_get_filename (abfd));
11866 }
11867 }
11868
11869 *bytes_read = cu_header->addr_size;
11870 return retval;
11871 }
11872
11873 /* Read the initial length from a section. The (draft) DWARF 3
11874 specification allows the initial length to take up either 4 bytes
11875 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11876 bytes describe the length and all offsets will be 8 bytes in length
11877 instead of 4.
11878
11879 An older, non-standard 64-bit format is also handled by this
11880 function. The older format in question stores the initial length
11881 as an 8-byte quantity without an escape value. Lengths greater
11882 than 2^32 aren't very common which means that the initial 4 bytes
11883 is almost always zero. Since a length value of zero doesn't make
11884 sense for the 32-bit format, this initial zero can be considered to
11885 be an escape value which indicates the presence of the older 64-bit
11886 format. As written, the code can't detect (old format) lengths
11887 greater than 4GB. If it becomes necessary to handle lengths
11888 somewhat larger than 4GB, we could allow other small values (such
11889 as the non-sensical values of 1, 2, and 3) to also be used as
11890 escape values indicating the presence of the old format.
11891
11892 The value returned via bytes_read should be used to increment the
11893 relevant pointer after calling read_initial_length().
11894
11895 [ Note: read_initial_length() and read_offset() are based on the
11896 document entitled "DWARF Debugging Information Format", revision
11897 3, draft 8, dated November 19, 2001. This document was obtained
11898 from:
11899
11900 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11901
11902 This document is only a draft and is subject to change. (So beware.)
11903
11904 Details regarding the older, non-standard 64-bit format were
11905 determined empirically by examining 64-bit ELF files produced by
11906 the SGI toolchain on an IRIX 6.5 machine.
11907
11908 - Kevin, July 16, 2002
11909 ] */
11910
11911 static LONGEST
11912 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11913 {
11914 LONGEST length = bfd_get_32 (abfd, buf);
11915
11916 if (length == 0xffffffff)
11917 {
11918 length = bfd_get_64 (abfd, buf + 4);
11919 *bytes_read = 12;
11920 }
11921 else if (length == 0)
11922 {
11923 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11924 length = bfd_get_64 (abfd, buf);
11925 *bytes_read = 8;
11926 }
11927 else
11928 {
11929 *bytes_read = 4;
11930 }
11931
11932 return length;
11933 }
11934
11935 /* Cover function for read_initial_length.
11936 Returns the length of the object at BUF, and stores the size of the
11937 initial length in *BYTES_READ and stores the size that offsets will be in
11938 *OFFSET_SIZE.
11939 If the initial length size is not equivalent to that specified in
11940 CU_HEADER then issue a complaint.
11941 This is useful when reading non-comp-unit headers. */
11942
11943 static LONGEST
11944 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11945 const struct comp_unit_head *cu_header,
11946 unsigned int *bytes_read,
11947 unsigned int *offset_size)
11948 {
11949 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11950
11951 gdb_assert (cu_header->initial_length_size == 4
11952 || cu_header->initial_length_size == 8
11953 || cu_header->initial_length_size == 12);
11954
11955 if (cu_header->initial_length_size != *bytes_read)
11956 complaint (&symfile_complaints,
11957 _("intermixed 32-bit and 64-bit DWARF sections"));
11958
11959 *offset_size = (*bytes_read == 4) ? 4 : 8;
11960 return length;
11961 }
11962
11963 /* Read an offset from the data stream. The size of the offset is
11964 given by cu_header->offset_size. */
11965
11966 static LONGEST
11967 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11968 unsigned int *bytes_read)
11969 {
11970 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11971
11972 *bytes_read = cu_header->offset_size;
11973 return offset;
11974 }
11975
11976 /* Read an offset from the data stream. */
11977
11978 static LONGEST
11979 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11980 {
11981 LONGEST retval = 0;
11982
11983 switch (offset_size)
11984 {
11985 case 4:
11986 retval = bfd_get_32 (abfd, buf);
11987 break;
11988 case 8:
11989 retval = bfd_get_64 (abfd, buf);
11990 break;
11991 default:
11992 internal_error (__FILE__, __LINE__,
11993 _("read_offset_1: bad switch [in module %s]"),
11994 bfd_get_filename (abfd));
11995 }
11996
11997 return retval;
11998 }
11999
12000 static gdb_byte *
12001 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12002 {
12003 /* If the size of a host char is 8 bits, we can return a pointer
12004 to the buffer, otherwise we have to copy the data to a buffer
12005 allocated on the temporary obstack. */
12006 gdb_assert (HOST_CHAR_BIT == 8);
12007 return buf;
12008 }
12009
12010 static char *
12011 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12012 {
12013 /* If the size of a host char is 8 bits, we can return a pointer
12014 to the string, otherwise we have to copy the string to a buffer
12015 allocated on the temporary obstack. */
12016 gdb_assert (HOST_CHAR_BIT == 8);
12017 if (*buf == '\0')
12018 {
12019 *bytes_read_ptr = 1;
12020 return NULL;
12021 }
12022 *bytes_read_ptr = strlen ((char *) buf) + 1;
12023 return (char *) buf;
12024 }
12025
12026 static char *
12027 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12028 {
12029 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12030 if (dwarf2_per_objfile->str.buffer == NULL)
12031 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12032 bfd_get_filename (abfd));
12033 if (str_offset >= dwarf2_per_objfile->str.size)
12034 error (_("DW_FORM_strp pointing outside of "
12035 ".debug_str section [in module %s]"),
12036 bfd_get_filename (abfd));
12037 gdb_assert (HOST_CHAR_BIT == 8);
12038 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12039 return NULL;
12040 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12041 }
12042
12043 static char *
12044 read_indirect_string (bfd *abfd, gdb_byte *buf,
12045 const struct comp_unit_head *cu_header,
12046 unsigned int *bytes_read_ptr)
12047 {
12048 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12049
12050 return read_indirect_string_at_offset (abfd, str_offset);
12051 }
12052
12053 static ULONGEST
12054 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12055 {
12056 ULONGEST result;
12057 unsigned int num_read;
12058 int i, shift;
12059 unsigned char byte;
12060
12061 result = 0;
12062 shift = 0;
12063 num_read = 0;
12064 i = 0;
12065 while (1)
12066 {
12067 byte = bfd_get_8 (abfd, buf);
12068 buf++;
12069 num_read++;
12070 result |= ((ULONGEST) (byte & 127) << shift);
12071 if ((byte & 128) == 0)
12072 {
12073 break;
12074 }
12075 shift += 7;
12076 }
12077 *bytes_read_ptr = num_read;
12078 return result;
12079 }
12080
12081 static LONGEST
12082 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12083 {
12084 LONGEST result;
12085 int i, shift, num_read;
12086 unsigned char byte;
12087
12088 result = 0;
12089 shift = 0;
12090 num_read = 0;
12091 i = 0;
12092 while (1)
12093 {
12094 byte = bfd_get_8 (abfd, buf);
12095 buf++;
12096 num_read++;
12097 result |= ((LONGEST) (byte & 127) << shift);
12098 shift += 7;
12099 if ((byte & 128) == 0)
12100 {
12101 break;
12102 }
12103 }
12104 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12105 result |= -(((LONGEST) 1) << shift);
12106 *bytes_read_ptr = num_read;
12107 return result;
12108 }
12109
12110 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12111 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12112 ADDR_SIZE is the size of addresses from the CU header. */
12113
12114 static CORE_ADDR
12115 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12116 {
12117 struct objfile *objfile = dwarf2_per_objfile->objfile;
12118 bfd *abfd = objfile->obfd;
12119 const gdb_byte *info_ptr;
12120
12121 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12122 if (dwarf2_per_objfile->addr.buffer == NULL)
12123 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12124 objfile->name);
12125 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12126 error (_("DW_FORM_addr_index pointing outside of "
12127 ".debug_addr section [in module %s]"),
12128 objfile->name);
12129 info_ptr = (dwarf2_per_objfile->addr.buffer
12130 + addr_base + addr_index * addr_size);
12131 if (addr_size == 4)
12132 return bfd_get_32 (abfd, info_ptr);
12133 else
12134 return bfd_get_64 (abfd, info_ptr);
12135 }
12136
12137 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12138
12139 static CORE_ADDR
12140 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12141 {
12142 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12143 }
12144
12145 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12146
12147 static CORE_ADDR
12148 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12149 unsigned int *bytes_read)
12150 {
12151 bfd *abfd = cu->objfile->obfd;
12152 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12153
12154 return read_addr_index (cu, addr_index);
12155 }
12156
12157 /* Data structure to pass results from dwarf2_read_addr_index_reader
12158 back to dwarf2_read_addr_index. */
12159
12160 struct dwarf2_read_addr_index_data
12161 {
12162 ULONGEST addr_base;
12163 int addr_size;
12164 };
12165
12166 /* die_reader_func for dwarf2_read_addr_index. */
12167
12168 static void
12169 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12170 gdb_byte *info_ptr,
12171 struct die_info *comp_unit_die,
12172 int has_children,
12173 void *data)
12174 {
12175 struct dwarf2_cu *cu = reader->cu;
12176 struct dwarf2_read_addr_index_data *aidata =
12177 (struct dwarf2_read_addr_index_data *) data;
12178
12179 aidata->addr_base = cu->addr_base;
12180 aidata->addr_size = cu->header.addr_size;
12181 }
12182
12183 /* Given an index in .debug_addr, fetch the value.
12184 NOTE: This can be called during dwarf expression evaluation,
12185 long after the debug information has been read, and thus per_cu->cu
12186 may no longer exist. */
12187
12188 CORE_ADDR
12189 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12190 unsigned int addr_index)
12191 {
12192 struct objfile *objfile = per_cu->objfile;
12193 struct dwarf2_cu *cu = per_cu->cu;
12194 ULONGEST addr_base;
12195 int addr_size;
12196
12197 /* This is intended to be called from outside this file. */
12198 dw2_setup (objfile);
12199
12200 /* We need addr_base and addr_size.
12201 If we don't have PER_CU->cu, we have to get it.
12202 Nasty, but the alternative is storing the needed info in PER_CU,
12203 which at this point doesn't seem justified: it's not clear how frequently
12204 it would get used and it would increase the size of every PER_CU.
12205 Entry points like dwarf2_per_cu_addr_size do a similar thing
12206 so we're not in uncharted territory here.
12207 Alas we need to be a bit more complicated as addr_base is contained
12208 in the DIE.
12209
12210 We don't need to read the entire CU(/TU).
12211 We just need the header and top level die.
12212 IWBN to use the aging mechanism to let us lazily later discard the CU.
12213 See however init_cutu_and_read_dies_simple. */
12214
12215 if (cu != NULL)
12216 {
12217 addr_base = cu->addr_base;
12218 addr_size = cu->header.addr_size;
12219 }
12220 else
12221 {
12222 struct dwarf2_read_addr_index_data aidata;
12223
12224 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12225 &aidata);
12226 addr_base = aidata.addr_base;
12227 addr_size = aidata.addr_size;
12228 }
12229
12230 return read_addr_index_1 (addr_index, addr_base, addr_size);
12231 }
12232
12233 /* Given a DW_AT_str_index, fetch the string. */
12234
12235 static char *
12236 read_str_index (const struct die_reader_specs *reader,
12237 struct dwarf2_cu *cu, ULONGEST str_index)
12238 {
12239 struct objfile *objfile = dwarf2_per_objfile->objfile;
12240 const char *dwo_name = objfile->name;
12241 bfd *abfd = objfile->obfd;
12242 struct dwo_sections *sections = &reader->dwo_file->sections;
12243 gdb_byte *info_ptr;
12244 ULONGEST str_offset;
12245
12246 dwarf2_read_section (objfile, &sections->str);
12247 dwarf2_read_section (objfile, &sections->str_offsets);
12248 if (sections->str.buffer == NULL)
12249 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12250 " in CU at offset 0x%lx [in module %s]"),
12251 (long) cu->header.offset.sect_off, dwo_name);
12252 if (sections->str_offsets.buffer == NULL)
12253 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12254 " in CU at offset 0x%lx [in module %s]"),
12255 (long) cu->header.offset.sect_off, dwo_name);
12256 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12257 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12258 " section in CU at offset 0x%lx [in module %s]"),
12259 (long) cu->header.offset.sect_off, dwo_name);
12260 info_ptr = (sections->str_offsets.buffer
12261 + str_index * cu->header.offset_size);
12262 if (cu->header.offset_size == 4)
12263 str_offset = bfd_get_32 (abfd, info_ptr);
12264 else
12265 str_offset = bfd_get_64 (abfd, info_ptr);
12266 if (str_offset >= sections->str.size)
12267 error (_("Offset from DW_FORM_str_index pointing outside of"
12268 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12269 (long) cu->header.offset.sect_off, dwo_name);
12270 return (char *) (sections->str.buffer + str_offset);
12271 }
12272
12273 /* Return a pointer to just past the end of an LEB128 number in BUF. */
12274
12275 static gdb_byte *
12276 skip_leb128 (bfd *abfd, gdb_byte *buf)
12277 {
12278 int byte;
12279
12280 while (1)
12281 {
12282 byte = bfd_get_8 (abfd, buf);
12283 buf++;
12284 if ((byte & 128) == 0)
12285 return buf;
12286 }
12287 }
12288
12289 /* Return the length of an LEB128 number in BUF. */
12290
12291 static int
12292 leb128_size (const gdb_byte *buf)
12293 {
12294 const gdb_byte *begin = buf;
12295 gdb_byte byte;
12296
12297 while (1)
12298 {
12299 byte = *buf++;
12300 if ((byte & 128) == 0)
12301 return buf - begin;
12302 }
12303 }
12304
12305 static void
12306 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12307 {
12308 switch (lang)
12309 {
12310 case DW_LANG_C89:
12311 case DW_LANG_C99:
12312 case DW_LANG_C:
12313 cu->language = language_c;
12314 break;
12315 case DW_LANG_C_plus_plus:
12316 cu->language = language_cplus;
12317 break;
12318 case DW_LANG_D:
12319 cu->language = language_d;
12320 break;
12321 case DW_LANG_Fortran77:
12322 case DW_LANG_Fortran90:
12323 case DW_LANG_Fortran95:
12324 cu->language = language_fortran;
12325 break;
12326 case DW_LANG_Go:
12327 cu->language = language_go;
12328 break;
12329 case DW_LANG_Mips_Assembler:
12330 cu->language = language_asm;
12331 break;
12332 case DW_LANG_Java:
12333 cu->language = language_java;
12334 break;
12335 case DW_LANG_Ada83:
12336 case DW_LANG_Ada95:
12337 cu->language = language_ada;
12338 break;
12339 case DW_LANG_Modula2:
12340 cu->language = language_m2;
12341 break;
12342 case DW_LANG_Pascal83:
12343 cu->language = language_pascal;
12344 break;
12345 case DW_LANG_ObjC:
12346 cu->language = language_objc;
12347 break;
12348 case DW_LANG_Cobol74:
12349 case DW_LANG_Cobol85:
12350 default:
12351 cu->language = language_minimal;
12352 break;
12353 }
12354 cu->language_defn = language_def (cu->language);
12355 }
12356
12357 /* Return the named attribute or NULL if not there. */
12358
12359 static struct attribute *
12360 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12361 {
12362 for (;;)
12363 {
12364 unsigned int i;
12365 struct attribute *spec = NULL;
12366
12367 for (i = 0; i < die->num_attrs; ++i)
12368 {
12369 if (die->attrs[i].name == name)
12370 return &die->attrs[i];
12371 if (die->attrs[i].name == DW_AT_specification
12372 || die->attrs[i].name == DW_AT_abstract_origin)
12373 spec = &die->attrs[i];
12374 }
12375
12376 if (!spec)
12377 break;
12378
12379 die = follow_die_ref (die, spec, &cu);
12380 }
12381
12382 return NULL;
12383 }
12384
12385 /* Return the named attribute or NULL if not there,
12386 but do not follow DW_AT_specification, etc.
12387 This is for use in contexts where we're reading .debug_types dies.
12388 Following DW_AT_specification, DW_AT_abstract_origin will take us
12389 back up the chain, and we want to go down. */
12390
12391 static struct attribute *
12392 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12393 struct dwarf2_cu *cu)
12394 {
12395 unsigned int i;
12396
12397 for (i = 0; i < die->num_attrs; ++i)
12398 if (die->attrs[i].name == name)
12399 return &die->attrs[i];
12400
12401 return NULL;
12402 }
12403
12404 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12405 and holds a non-zero value. This function should only be used for
12406 DW_FORM_flag or DW_FORM_flag_present attributes. */
12407
12408 static int
12409 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12410 {
12411 struct attribute *attr = dwarf2_attr (die, name, cu);
12412
12413 return (attr && DW_UNSND (attr));
12414 }
12415
12416 static int
12417 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12418 {
12419 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12420 which value is non-zero. However, we have to be careful with
12421 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12422 (via dwarf2_flag_true_p) follows this attribute. So we may
12423 end up accidently finding a declaration attribute that belongs
12424 to a different DIE referenced by the specification attribute,
12425 even though the given DIE does not have a declaration attribute. */
12426 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12427 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12428 }
12429
12430 /* Return the die giving the specification for DIE, if there is
12431 one. *SPEC_CU is the CU containing DIE on input, and the CU
12432 containing the return value on output. If there is no
12433 specification, but there is an abstract origin, that is
12434 returned. */
12435
12436 static struct die_info *
12437 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12438 {
12439 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12440 *spec_cu);
12441
12442 if (spec_attr == NULL)
12443 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12444
12445 if (spec_attr == NULL)
12446 return NULL;
12447 else
12448 return follow_die_ref (die, spec_attr, spec_cu);
12449 }
12450
12451 /* Free the line_header structure *LH, and any arrays and strings it
12452 refers to.
12453 NOTE: This is also used as a "cleanup" function. */
12454
12455 static void
12456 free_line_header (struct line_header *lh)
12457 {
12458 if (lh->standard_opcode_lengths)
12459 xfree (lh->standard_opcode_lengths);
12460
12461 /* Remember that all the lh->file_names[i].name pointers are
12462 pointers into debug_line_buffer, and don't need to be freed. */
12463 if (lh->file_names)
12464 xfree (lh->file_names);
12465
12466 /* Similarly for the include directory names. */
12467 if (lh->include_dirs)
12468 xfree (lh->include_dirs);
12469
12470 xfree (lh);
12471 }
12472
12473 /* Add an entry to LH's include directory table. */
12474
12475 static void
12476 add_include_dir (struct line_header *lh, char *include_dir)
12477 {
12478 /* Grow the array if necessary. */
12479 if (lh->include_dirs_size == 0)
12480 {
12481 lh->include_dirs_size = 1; /* for testing */
12482 lh->include_dirs = xmalloc (lh->include_dirs_size
12483 * sizeof (*lh->include_dirs));
12484 }
12485 else if (lh->num_include_dirs >= lh->include_dirs_size)
12486 {
12487 lh->include_dirs_size *= 2;
12488 lh->include_dirs = xrealloc (lh->include_dirs,
12489 (lh->include_dirs_size
12490 * sizeof (*lh->include_dirs)));
12491 }
12492
12493 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12494 }
12495
12496 /* Add an entry to LH's file name table. */
12497
12498 static void
12499 add_file_name (struct line_header *lh,
12500 char *name,
12501 unsigned int dir_index,
12502 unsigned int mod_time,
12503 unsigned int length)
12504 {
12505 struct file_entry *fe;
12506
12507 /* Grow the array if necessary. */
12508 if (lh->file_names_size == 0)
12509 {
12510 lh->file_names_size = 1; /* for testing */
12511 lh->file_names = xmalloc (lh->file_names_size
12512 * sizeof (*lh->file_names));
12513 }
12514 else if (lh->num_file_names >= lh->file_names_size)
12515 {
12516 lh->file_names_size *= 2;
12517 lh->file_names = xrealloc (lh->file_names,
12518 (lh->file_names_size
12519 * sizeof (*lh->file_names)));
12520 }
12521
12522 fe = &lh->file_names[lh->num_file_names++];
12523 fe->name = name;
12524 fe->dir_index = dir_index;
12525 fe->mod_time = mod_time;
12526 fe->length = length;
12527 fe->included_p = 0;
12528 fe->symtab = NULL;
12529 }
12530
12531 /* Read the statement program header starting at OFFSET in
12532 .debug_line, or .debug_line.dwo. Return a pointer
12533 to a struct line_header, allocated using xmalloc.
12534
12535 NOTE: the strings in the include directory and file name tables of
12536 the returned object point into the dwarf line section buffer,
12537 and must not be freed. */
12538
12539 static struct line_header *
12540 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12541 {
12542 struct cleanup *back_to;
12543 struct line_header *lh;
12544 gdb_byte *line_ptr;
12545 unsigned int bytes_read, offset_size;
12546 int i;
12547 char *cur_dir, *cur_file;
12548 struct dwarf2_section_info *section;
12549 bfd *abfd;
12550
12551 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12552 DWO file. */
12553 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12554 section = &cu->dwo_unit->dwo_file->sections.line;
12555 else
12556 section = &dwarf2_per_objfile->line;
12557
12558 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12559 if (section->buffer == NULL)
12560 {
12561 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12562 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12563 else
12564 complaint (&symfile_complaints, _("missing .debug_line section"));
12565 return 0;
12566 }
12567
12568 /* We can't do this until we know the section is non-empty.
12569 Only then do we know we have such a section. */
12570 abfd = section->asection->owner;
12571
12572 /* Make sure that at least there's room for the total_length field.
12573 That could be 12 bytes long, but we're just going to fudge that. */
12574 if (offset + 4 >= section->size)
12575 {
12576 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12577 return 0;
12578 }
12579
12580 lh = xmalloc (sizeof (*lh));
12581 memset (lh, 0, sizeof (*lh));
12582 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12583 (void *) lh);
12584
12585 line_ptr = section->buffer + offset;
12586
12587 /* Read in the header. */
12588 lh->total_length =
12589 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12590 &bytes_read, &offset_size);
12591 line_ptr += bytes_read;
12592 if (line_ptr + lh->total_length > (section->buffer + section->size))
12593 {
12594 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12595 return 0;
12596 }
12597 lh->statement_program_end = line_ptr + lh->total_length;
12598 lh->version = read_2_bytes (abfd, line_ptr);
12599 line_ptr += 2;
12600 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12601 line_ptr += offset_size;
12602 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12603 line_ptr += 1;
12604 if (lh->version >= 4)
12605 {
12606 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12607 line_ptr += 1;
12608 }
12609 else
12610 lh->maximum_ops_per_instruction = 1;
12611
12612 if (lh->maximum_ops_per_instruction == 0)
12613 {
12614 lh->maximum_ops_per_instruction = 1;
12615 complaint (&symfile_complaints,
12616 _("invalid maximum_ops_per_instruction "
12617 "in `.debug_line' section"));
12618 }
12619
12620 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12621 line_ptr += 1;
12622 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12623 line_ptr += 1;
12624 lh->line_range = read_1_byte (abfd, line_ptr);
12625 line_ptr += 1;
12626 lh->opcode_base = read_1_byte (abfd, line_ptr);
12627 line_ptr += 1;
12628 lh->standard_opcode_lengths
12629 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12630
12631 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12632 for (i = 1; i < lh->opcode_base; ++i)
12633 {
12634 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12635 line_ptr += 1;
12636 }
12637
12638 /* Read directory table. */
12639 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12640 {
12641 line_ptr += bytes_read;
12642 add_include_dir (lh, cur_dir);
12643 }
12644 line_ptr += bytes_read;
12645
12646 /* Read file name table. */
12647 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12648 {
12649 unsigned int dir_index, mod_time, length;
12650
12651 line_ptr += bytes_read;
12652 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12653 line_ptr += bytes_read;
12654 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12655 line_ptr += bytes_read;
12656 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12657 line_ptr += bytes_read;
12658
12659 add_file_name (lh, cur_file, dir_index, mod_time, length);
12660 }
12661 line_ptr += bytes_read;
12662 lh->statement_program_start = line_ptr;
12663
12664 if (line_ptr > (section->buffer + section->size))
12665 complaint (&symfile_complaints,
12666 _("line number info header doesn't "
12667 "fit in `.debug_line' section"));
12668
12669 discard_cleanups (back_to);
12670 return lh;
12671 }
12672
12673 /* Subroutine of dwarf_decode_lines to simplify it.
12674 Return the file name of the psymtab for included file FILE_INDEX
12675 in line header LH of PST.
12676 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12677 If space for the result is malloc'd, it will be freed by a cleanup.
12678 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12679
12680 static char *
12681 psymtab_include_file_name (const struct line_header *lh, int file_index,
12682 const struct partial_symtab *pst,
12683 const char *comp_dir)
12684 {
12685 const struct file_entry fe = lh->file_names [file_index];
12686 char *include_name = fe.name;
12687 char *include_name_to_compare = include_name;
12688 char *dir_name = NULL;
12689 const char *pst_filename;
12690 char *copied_name = NULL;
12691 int file_is_pst;
12692
12693 if (fe.dir_index)
12694 dir_name = lh->include_dirs[fe.dir_index - 1];
12695
12696 if (!IS_ABSOLUTE_PATH (include_name)
12697 && (dir_name != NULL || comp_dir != NULL))
12698 {
12699 /* Avoid creating a duplicate psymtab for PST.
12700 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12701 Before we do the comparison, however, we need to account
12702 for DIR_NAME and COMP_DIR.
12703 First prepend dir_name (if non-NULL). If we still don't
12704 have an absolute path prepend comp_dir (if non-NULL).
12705 However, the directory we record in the include-file's
12706 psymtab does not contain COMP_DIR (to match the
12707 corresponding symtab(s)).
12708
12709 Example:
12710
12711 bash$ cd /tmp
12712 bash$ gcc -g ./hello.c
12713 include_name = "hello.c"
12714 dir_name = "."
12715 DW_AT_comp_dir = comp_dir = "/tmp"
12716 DW_AT_name = "./hello.c" */
12717
12718 if (dir_name != NULL)
12719 {
12720 include_name = concat (dir_name, SLASH_STRING,
12721 include_name, (char *)NULL);
12722 include_name_to_compare = include_name;
12723 make_cleanup (xfree, include_name);
12724 }
12725 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12726 {
12727 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12728 include_name, (char *)NULL);
12729 }
12730 }
12731
12732 pst_filename = pst->filename;
12733 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12734 {
12735 copied_name = concat (pst->dirname, SLASH_STRING,
12736 pst_filename, (char *)NULL);
12737 pst_filename = copied_name;
12738 }
12739
12740 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12741
12742 if (include_name_to_compare != include_name)
12743 xfree (include_name_to_compare);
12744 if (copied_name != NULL)
12745 xfree (copied_name);
12746
12747 if (file_is_pst)
12748 return NULL;
12749 return include_name;
12750 }
12751
12752 /* Ignore this record_line request. */
12753
12754 static void
12755 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12756 {
12757 return;
12758 }
12759
12760 /* Subroutine of dwarf_decode_lines to simplify it.
12761 Process the line number information in LH. */
12762
12763 static void
12764 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12765 struct dwarf2_cu *cu, struct partial_symtab *pst)
12766 {
12767 gdb_byte *line_ptr, *extended_end;
12768 gdb_byte *line_end;
12769 unsigned int bytes_read, extended_len;
12770 unsigned char op_code, extended_op, adj_opcode;
12771 CORE_ADDR baseaddr;
12772 struct objfile *objfile = cu->objfile;
12773 bfd *abfd = objfile->obfd;
12774 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12775 const int decode_for_pst_p = (pst != NULL);
12776 struct subfile *last_subfile = NULL;
12777 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12778 = record_line;
12779
12780 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12781
12782 line_ptr = lh->statement_program_start;
12783 line_end = lh->statement_program_end;
12784
12785 /* Read the statement sequences until there's nothing left. */
12786 while (line_ptr < line_end)
12787 {
12788 /* state machine registers */
12789 CORE_ADDR address = 0;
12790 unsigned int file = 1;
12791 unsigned int line = 1;
12792 unsigned int column = 0;
12793 int is_stmt = lh->default_is_stmt;
12794 int basic_block = 0;
12795 int end_sequence = 0;
12796 CORE_ADDR addr;
12797 unsigned char op_index = 0;
12798
12799 if (!decode_for_pst_p && lh->num_file_names >= file)
12800 {
12801 /* Start a subfile for the current file of the state machine. */
12802 /* lh->include_dirs and lh->file_names are 0-based, but the
12803 directory and file name numbers in the statement program
12804 are 1-based. */
12805 struct file_entry *fe = &lh->file_names[file - 1];
12806 char *dir = NULL;
12807
12808 if (fe->dir_index)
12809 dir = lh->include_dirs[fe->dir_index - 1];
12810
12811 dwarf2_start_subfile (fe->name, dir, comp_dir);
12812 }
12813
12814 /* Decode the table. */
12815 while (!end_sequence)
12816 {
12817 op_code = read_1_byte (abfd, line_ptr);
12818 line_ptr += 1;
12819 if (line_ptr > line_end)
12820 {
12821 dwarf2_debug_line_missing_end_sequence_complaint ();
12822 break;
12823 }
12824
12825 if (op_code >= lh->opcode_base)
12826 {
12827 /* Special operand. */
12828 adj_opcode = op_code - lh->opcode_base;
12829 address += (((op_index + (adj_opcode / lh->line_range))
12830 / lh->maximum_ops_per_instruction)
12831 * lh->minimum_instruction_length);
12832 op_index = ((op_index + (adj_opcode / lh->line_range))
12833 % lh->maximum_ops_per_instruction);
12834 line += lh->line_base + (adj_opcode % lh->line_range);
12835 if (lh->num_file_names < file || file == 0)
12836 dwarf2_debug_line_missing_file_complaint ();
12837 /* For now we ignore lines not starting on an
12838 instruction boundary. */
12839 else if (op_index == 0)
12840 {
12841 lh->file_names[file - 1].included_p = 1;
12842 if (!decode_for_pst_p && is_stmt)
12843 {
12844 if (last_subfile != current_subfile)
12845 {
12846 addr = gdbarch_addr_bits_remove (gdbarch, address);
12847 if (last_subfile)
12848 (*p_record_line) (last_subfile, 0, addr);
12849 last_subfile = current_subfile;
12850 }
12851 /* Append row to matrix using current values. */
12852 addr = gdbarch_addr_bits_remove (gdbarch, address);
12853 (*p_record_line) (current_subfile, line, addr);
12854 }
12855 }
12856 basic_block = 0;
12857 }
12858 else switch (op_code)
12859 {
12860 case DW_LNS_extended_op:
12861 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12862 &bytes_read);
12863 line_ptr += bytes_read;
12864 extended_end = line_ptr + extended_len;
12865 extended_op = read_1_byte (abfd, line_ptr);
12866 line_ptr += 1;
12867 switch (extended_op)
12868 {
12869 case DW_LNE_end_sequence:
12870 p_record_line = record_line;
12871 end_sequence = 1;
12872 break;
12873 case DW_LNE_set_address:
12874 address = read_address (abfd, line_ptr, cu, &bytes_read);
12875
12876 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12877 {
12878 /* This line table is for a function which has been
12879 GCd by the linker. Ignore it. PR gdb/12528 */
12880
12881 long line_offset
12882 = line_ptr - dwarf2_per_objfile->line.buffer;
12883
12884 complaint (&symfile_complaints,
12885 _(".debug_line address at offset 0x%lx is 0 "
12886 "[in module %s]"),
12887 line_offset, objfile->name);
12888 p_record_line = noop_record_line;
12889 }
12890
12891 op_index = 0;
12892 line_ptr += bytes_read;
12893 address += baseaddr;
12894 break;
12895 case DW_LNE_define_file:
12896 {
12897 char *cur_file;
12898 unsigned int dir_index, mod_time, length;
12899
12900 cur_file = read_direct_string (abfd, line_ptr,
12901 &bytes_read);
12902 line_ptr += bytes_read;
12903 dir_index =
12904 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12905 line_ptr += bytes_read;
12906 mod_time =
12907 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12908 line_ptr += bytes_read;
12909 length =
12910 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12911 line_ptr += bytes_read;
12912 add_file_name (lh, cur_file, dir_index, mod_time, length);
12913 }
12914 break;
12915 case DW_LNE_set_discriminator:
12916 /* The discriminator is not interesting to the debugger;
12917 just ignore it. */
12918 line_ptr = extended_end;
12919 break;
12920 default:
12921 complaint (&symfile_complaints,
12922 _("mangled .debug_line section"));
12923 return;
12924 }
12925 /* Make sure that we parsed the extended op correctly. If e.g.
12926 we expected a different address size than the producer used,
12927 we may have read the wrong number of bytes. */
12928 if (line_ptr != extended_end)
12929 {
12930 complaint (&symfile_complaints,
12931 _("mangled .debug_line section"));
12932 return;
12933 }
12934 break;
12935 case DW_LNS_copy:
12936 if (lh->num_file_names < file || file == 0)
12937 dwarf2_debug_line_missing_file_complaint ();
12938 else
12939 {
12940 lh->file_names[file - 1].included_p = 1;
12941 if (!decode_for_pst_p && is_stmt)
12942 {
12943 if (last_subfile != current_subfile)
12944 {
12945 addr = gdbarch_addr_bits_remove (gdbarch, address);
12946 if (last_subfile)
12947 (*p_record_line) (last_subfile, 0, addr);
12948 last_subfile = current_subfile;
12949 }
12950 addr = gdbarch_addr_bits_remove (gdbarch, address);
12951 (*p_record_line) (current_subfile, line, addr);
12952 }
12953 }
12954 basic_block = 0;
12955 break;
12956 case DW_LNS_advance_pc:
12957 {
12958 CORE_ADDR adjust
12959 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12960
12961 address += (((op_index + adjust)
12962 / lh->maximum_ops_per_instruction)
12963 * lh->minimum_instruction_length);
12964 op_index = ((op_index + adjust)
12965 % lh->maximum_ops_per_instruction);
12966 line_ptr += bytes_read;
12967 }
12968 break;
12969 case DW_LNS_advance_line:
12970 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12971 line_ptr += bytes_read;
12972 break;
12973 case DW_LNS_set_file:
12974 {
12975 /* The arrays lh->include_dirs and lh->file_names are
12976 0-based, but the directory and file name numbers in
12977 the statement program are 1-based. */
12978 struct file_entry *fe;
12979 char *dir = NULL;
12980
12981 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12982 line_ptr += bytes_read;
12983 if (lh->num_file_names < file || file == 0)
12984 dwarf2_debug_line_missing_file_complaint ();
12985 else
12986 {
12987 fe = &lh->file_names[file - 1];
12988 if (fe->dir_index)
12989 dir = lh->include_dirs[fe->dir_index - 1];
12990 if (!decode_for_pst_p)
12991 {
12992 last_subfile = current_subfile;
12993 dwarf2_start_subfile (fe->name, dir, comp_dir);
12994 }
12995 }
12996 }
12997 break;
12998 case DW_LNS_set_column:
12999 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13000 line_ptr += bytes_read;
13001 break;
13002 case DW_LNS_negate_stmt:
13003 is_stmt = (!is_stmt);
13004 break;
13005 case DW_LNS_set_basic_block:
13006 basic_block = 1;
13007 break;
13008 /* Add to the address register of the state machine the
13009 address increment value corresponding to special opcode
13010 255. I.e., this value is scaled by the minimum
13011 instruction length since special opcode 255 would have
13012 scaled the increment. */
13013 case DW_LNS_const_add_pc:
13014 {
13015 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13016
13017 address += (((op_index + adjust)
13018 / lh->maximum_ops_per_instruction)
13019 * lh->minimum_instruction_length);
13020 op_index = ((op_index + adjust)
13021 % lh->maximum_ops_per_instruction);
13022 }
13023 break;
13024 case DW_LNS_fixed_advance_pc:
13025 address += read_2_bytes (abfd, line_ptr);
13026 op_index = 0;
13027 line_ptr += 2;
13028 break;
13029 default:
13030 {
13031 /* Unknown standard opcode, ignore it. */
13032 int i;
13033
13034 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13035 {
13036 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13037 line_ptr += bytes_read;
13038 }
13039 }
13040 }
13041 }
13042 if (lh->num_file_names < file || file == 0)
13043 dwarf2_debug_line_missing_file_complaint ();
13044 else
13045 {
13046 lh->file_names[file - 1].included_p = 1;
13047 if (!decode_for_pst_p)
13048 {
13049 addr = gdbarch_addr_bits_remove (gdbarch, address);
13050 (*p_record_line) (current_subfile, 0, addr);
13051 }
13052 }
13053 }
13054 }
13055
13056 /* Decode the Line Number Program (LNP) for the given line_header
13057 structure and CU. The actual information extracted and the type
13058 of structures created from the LNP depends on the value of PST.
13059
13060 1. If PST is NULL, then this procedure uses the data from the program
13061 to create all necessary symbol tables, and their linetables.
13062
13063 2. If PST is not NULL, this procedure reads the program to determine
13064 the list of files included by the unit represented by PST, and
13065 builds all the associated partial symbol tables.
13066
13067 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13068 It is used for relative paths in the line table.
13069 NOTE: When processing partial symtabs (pst != NULL),
13070 comp_dir == pst->dirname.
13071
13072 NOTE: It is important that psymtabs have the same file name (via strcmp)
13073 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13074 symtab we don't use it in the name of the psymtabs we create.
13075 E.g. expand_line_sal requires this when finding psymtabs to expand.
13076 A good testcase for this is mb-inline.exp. */
13077
13078 static void
13079 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13080 struct dwarf2_cu *cu, struct partial_symtab *pst,
13081 int want_line_info)
13082 {
13083 struct objfile *objfile = cu->objfile;
13084 const int decode_for_pst_p = (pst != NULL);
13085 struct subfile *first_subfile = current_subfile;
13086
13087 if (want_line_info)
13088 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13089
13090 if (decode_for_pst_p)
13091 {
13092 int file_index;
13093
13094 /* Now that we're done scanning the Line Header Program, we can
13095 create the psymtab of each included file. */
13096 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13097 if (lh->file_names[file_index].included_p == 1)
13098 {
13099 char *include_name =
13100 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13101 if (include_name != NULL)
13102 dwarf2_create_include_psymtab (include_name, pst, objfile);
13103 }
13104 }
13105 else
13106 {
13107 /* Make sure a symtab is created for every file, even files
13108 which contain only variables (i.e. no code with associated
13109 line numbers). */
13110 int i;
13111
13112 for (i = 0; i < lh->num_file_names; i++)
13113 {
13114 char *dir = NULL;
13115 struct file_entry *fe;
13116
13117 fe = &lh->file_names[i];
13118 if (fe->dir_index)
13119 dir = lh->include_dirs[fe->dir_index - 1];
13120 dwarf2_start_subfile (fe->name, dir, comp_dir);
13121
13122 /* Skip the main file; we don't need it, and it must be
13123 allocated last, so that it will show up before the
13124 non-primary symtabs in the objfile's symtab list. */
13125 if (current_subfile == first_subfile)
13126 continue;
13127
13128 if (current_subfile->symtab == NULL)
13129 current_subfile->symtab = allocate_symtab (current_subfile->name,
13130 objfile);
13131 fe->symtab = current_subfile->symtab;
13132 }
13133 }
13134 }
13135
13136 /* Start a subfile for DWARF. FILENAME is the name of the file and
13137 DIRNAME the name of the source directory which contains FILENAME
13138 or NULL if not known. COMP_DIR is the compilation directory for the
13139 linetable's compilation unit or NULL if not known.
13140 This routine tries to keep line numbers from identical absolute and
13141 relative file names in a common subfile.
13142
13143 Using the `list' example from the GDB testsuite, which resides in
13144 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13145 of /srcdir/list0.c yields the following debugging information for list0.c:
13146
13147 DW_AT_name: /srcdir/list0.c
13148 DW_AT_comp_dir: /compdir
13149 files.files[0].name: list0.h
13150 files.files[0].dir: /srcdir
13151 files.files[1].name: list0.c
13152 files.files[1].dir: /srcdir
13153
13154 The line number information for list0.c has to end up in a single
13155 subfile, so that `break /srcdir/list0.c:1' works as expected.
13156 start_subfile will ensure that this happens provided that we pass the
13157 concatenation of files.files[1].dir and files.files[1].name as the
13158 subfile's name. */
13159
13160 static void
13161 dwarf2_start_subfile (char *filename, const char *dirname,
13162 const char *comp_dir)
13163 {
13164 char *fullname;
13165
13166 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13167 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13168 second argument to start_subfile. To be consistent, we do the
13169 same here. In order not to lose the line information directory,
13170 we concatenate it to the filename when it makes sense.
13171 Note that the Dwarf3 standard says (speaking of filenames in line
13172 information): ``The directory index is ignored for file names
13173 that represent full path names''. Thus ignoring dirname in the
13174 `else' branch below isn't an issue. */
13175
13176 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13177 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13178 else
13179 fullname = filename;
13180
13181 start_subfile (fullname, comp_dir);
13182
13183 if (fullname != filename)
13184 xfree (fullname);
13185 }
13186
13187 static void
13188 var_decode_location (struct attribute *attr, struct symbol *sym,
13189 struct dwarf2_cu *cu)
13190 {
13191 struct objfile *objfile = cu->objfile;
13192 struct comp_unit_head *cu_header = &cu->header;
13193
13194 /* NOTE drow/2003-01-30: There used to be a comment and some special
13195 code here to turn a symbol with DW_AT_external and a
13196 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13197 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13198 with some versions of binutils) where shared libraries could have
13199 relocations against symbols in their debug information - the
13200 minimal symbol would have the right address, but the debug info
13201 would not. It's no longer necessary, because we will explicitly
13202 apply relocations when we read in the debug information now. */
13203
13204 /* A DW_AT_location attribute with no contents indicates that a
13205 variable has been optimized away. */
13206 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13207 {
13208 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13209 return;
13210 }
13211
13212 /* Handle one degenerate form of location expression specially, to
13213 preserve GDB's previous behavior when section offsets are
13214 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13215 then mark this symbol as LOC_STATIC. */
13216
13217 if (attr_form_is_block (attr)
13218 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13219 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13220 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13221 && (DW_BLOCK (attr)->size
13222 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13223 {
13224 unsigned int dummy;
13225
13226 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13227 SYMBOL_VALUE_ADDRESS (sym) =
13228 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13229 else
13230 SYMBOL_VALUE_ADDRESS (sym) =
13231 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13232 SYMBOL_CLASS (sym) = LOC_STATIC;
13233 fixup_symbol_section (sym, objfile);
13234 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13235 SYMBOL_SECTION (sym));
13236 return;
13237 }
13238
13239 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13240 expression evaluator, and use LOC_COMPUTED only when necessary
13241 (i.e. when the value of a register or memory location is
13242 referenced, or a thread-local block, etc.). Then again, it might
13243 not be worthwhile. I'm assuming that it isn't unless performance
13244 or memory numbers show me otherwise. */
13245
13246 dwarf2_symbol_mark_computed (attr, sym, cu);
13247 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13248
13249 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13250 cu->has_loclist = 1;
13251 }
13252
13253 /* Given a pointer to a DWARF information entry, figure out if we need
13254 to make a symbol table entry for it, and if so, create a new entry
13255 and return a pointer to it.
13256 If TYPE is NULL, determine symbol type from the die, otherwise
13257 used the passed type.
13258 If SPACE is not NULL, use it to hold the new symbol. If it is
13259 NULL, allocate a new symbol on the objfile's obstack. */
13260
13261 static struct symbol *
13262 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13263 struct symbol *space)
13264 {
13265 struct objfile *objfile = cu->objfile;
13266 struct symbol *sym = NULL;
13267 char *name;
13268 struct attribute *attr = NULL;
13269 struct attribute *attr2 = NULL;
13270 CORE_ADDR baseaddr;
13271 struct pending **list_to_add = NULL;
13272
13273 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13274
13275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13276
13277 name = dwarf2_name (die, cu);
13278 if (name)
13279 {
13280 const char *linkagename;
13281 int suppress_add = 0;
13282
13283 if (space)
13284 sym = space;
13285 else
13286 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13287 OBJSTAT (objfile, n_syms++);
13288
13289 /* Cache this symbol's name and the name's demangled form (if any). */
13290 SYMBOL_SET_LANGUAGE (sym, cu->language);
13291 linkagename = dwarf2_physname (name, die, cu);
13292 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13293
13294 /* Fortran does not have mangling standard and the mangling does differ
13295 between gfortran, iFort etc. */
13296 if (cu->language == language_fortran
13297 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13298 symbol_set_demangled_name (&(sym->ginfo),
13299 (char *) dwarf2_full_name (name, die, cu),
13300 NULL);
13301
13302 /* Default assumptions.
13303 Use the passed type or decode it from the die. */
13304 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13305 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13306 if (type != NULL)
13307 SYMBOL_TYPE (sym) = type;
13308 else
13309 SYMBOL_TYPE (sym) = die_type (die, cu);
13310 attr = dwarf2_attr (die,
13311 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13312 cu);
13313 if (attr)
13314 {
13315 SYMBOL_LINE (sym) = DW_UNSND (attr);
13316 }
13317
13318 attr = dwarf2_attr (die,
13319 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13320 cu);
13321 if (attr)
13322 {
13323 int file_index = DW_UNSND (attr);
13324
13325 if (cu->line_header == NULL
13326 || file_index > cu->line_header->num_file_names)
13327 complaint (&symfile_complaints,
13328 _("file index out of range"));
13329 else if (file_index > 0)
13330 {
13331 struct file_entry *fe;
13332
13333 fe = &cu->line_header->file_names[file_index - 1];
13334 SYMBOL_SYMTAB (sym) = fe->symtab;
13335 }
13336 }
13337
13338 switch (die->tag)
13339 {
13340 case DW_TAG_label:
13341 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13342 if (attr)
13343 {
13344 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13345 }
13346 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13347 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13348 SYMBOL_CLASS (sym) = LOC_LABEL;
13349 add_symbol_to_list (sym, cu->list_in_scope);
13350 break;
13351 case DW_TAG_subprogram:
13352 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13353 finish_block. */
13354 SYMBOL_CLASS (sym) = LOC_BLOCK;
13355 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13356 if ((attr2 && (DW_UNSND (attr2) != 0))
13357 || cu->language == language_ada)
13358 {
13359 /* Subprograms marked external are stored as a global symbol.
13360 Ada subprograms, whether marked external or not, are always
13361 stored as a global symbol, because we want to be able to
13362 access them globally. For instance, we want to be able
13363 to break on a nested subprogram without having to
13364 specify the context. */
13365 list_to_add = &global_symbols;
13366 }
13367 else
13368 {
13369 list_to_add = cu->list_in_scope;
13370 }
13371 break;
13372 case DW_TAG_inlined_subroutine:
13373 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13374 finish_block. */
13375 SYMBOL_CLASS (sym) = LOC_BLOCK;
13376 SYMBOL_INLINED (sym) = 1;
13377 list_to_add = cu->list_in_scope;
13378 break;
13379 case DW_TAG_template_value_param:
13380 suppress_add = 1;
13381 /* Fall through. */
13382 case DW_TAG_constant:
13383 case DW_TAG_variable:
13384 case DW_TAG_member:
13385 /* Compilation with minimal debug info may result in
13386 variables with missing type entries. Change the
13387 misleading `void' type to something sensible. */
13388 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13389 SYMBOL_TYPE (sym)
13390 = objfile_type (objfile)->nodebug_data_symbol;
13391
13392 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13393 /* In the case of DW_TAG_member, we should only be called for
13394 static const members. */
13395 if (die->tag == DW_TAG_member)
13396 {
13397 /* dwarf2_add_field uses die_is_declaration,
13398 so we do the same. */
13399 gdb_assert (die_is_declaration (die, cu));
13400 gdb_assert (attr);
13401 }
13402 if (attr)
13403 {
13404 dwarf2_const_value (attr, sym, cu);
13405 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13406 if (!suppress_add)
13407 {
13408 if (attr2 && (DW_UNSND (attr2) != 0))
13409 list_to_add = &global_symbols;
13410 else
13411 list_to_add = cu->list_in_scope;
13412 }
13413 break;
13414 }
13415 attr = dwarf2_attr (die, DW_AT_location, cu);
13416 if (attr)
13417 {
13418 var_decode_location (attr, sym, cu);
13419 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13420 if (SYMBOL_CLASS (sym) == LOC_STATIC
13421 && SYMBOL_VALUE_ADDRESS (sym) == 0
13422 && !dwarf2_per_objfile->has_section_at_zero)
13423 {
13424 /* When a static variable is eliminated by the linker,
13425 the corresponding debug information is not stripped
13426 out, but the variable address is set to null;
13427 do not add such variables into symbol table. */
13428 }
13429 else if (attr2 && (DW_UNSND (attr2) != 0))
13430 {
13431 /* Workaround gfortran PR debug/40040 - it uses
13432 DW_AT_location for variables in -fPIC libraries which may
13433 get overriden by other libraries/executable and get
13434 a different address. Resolve it by the minimal symbol
13435 which may come from inferior's executable using copy
13436 relocation. Make this workaround only for gfortran as for
13437 other compilers GDB cannot guess the minimal symbol
13438 Fortran mangling kind. */
13439 if (cu->language == language_fortran && die->parent
13440 && die->parent->tag == DW_TAG_module
13441 && cu->producer
13442 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13443 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13444
13445 /* A variable with DW_AT_external is never static,
13446 but it may be block-scoped. */
13447 list_to_add = (cu->list_in_scope == &file_symbols
13448 ? &global_symbols : cu->list_in_scope);
13449 }
13450 else
13451 list_to_add = cu->list_in_scope;
13452 }
13453 else
13454 {
13455 /* We do not know the address of this symbol.
13456 If it is an external symbol and we have type information
13457 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13458 The address of the variable will then be determined from
13459 the minimal symbol table whenever the variable is
13460 referenced. */
13461 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13462 if (attr2 && (DW_UNSND (attr2) != 0)
13463 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13464 {
13465 /* A variable with DW_AT_external is never static, but it
13466 may be block-scoped. */
13467 list_to_add = (cu->list_in_scope == &file_symbols
13468 ? &global_symbols : cu->list_in_scope);
13469
13470 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13471 }
13472 else if (!die_is_declaration (die, cu))
13473 {
13474 /* Use the default LOC_OPTIMIZED_OUT class. */
13475 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13476 if (!suppress_add)
13477 list_to_add = cu->list_in_scope;
13478 }
13479 }
13480 break;
13481 case DW_TAG_formal_parameter:
13482 /* If we are inside a function, mark this as an argument. If
13483 not, we might be looking at an argument to an inlined function
13484 when we do not have enough information to show inlined frames;
13485 pretend it's a local variable in that case so that the user can
13486 still see it. */
13487 if (context_stack_depth > 0
13488 && context_stack[context_stack_depth - 1].name != NULL)
13489 SYMBOL_IS_ARGUMENT (sym) = 1;
13490 attr = dwarf2_attr (die, DW_AT_location, cu);
13491 if (attr)
13492 {
13493 var_decode_location (attr, sym, cu);
13494 }
13495 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13496 if (attr)
13497 {
13498 dwarf2_const_value (attr, sym, cu);
13499 }
13500
13501 list_to_add = cu->list_in_scope;
13502 break;
13503 case DW_TAG_unspecified_parameters:
13504 /* From varargs functions; gdb doesn't seem to have any
13505 interest in this information, so just ignore it for now.
13506 (FIXME?) */
13507 break;
13508 case DW_TAG_template_type_param:
13509 suppress_add = 1;
13510 /* Fall through. */
13511 case DW_TAG_class_type:
13512 case DW_TAG_interface_type:
13513 case DW_TAG_structure_type:
13514 case DW_TAG_union_type:
13515 case DW_TAG_set_type:
13516 case DW_TAG_enumeration_type:
13517 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13518 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13519
13520 {
13521 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13522 really ever be static objects: otherwise, if you try
13523 to, say, break of a class's method and you're in a file
13524 which doesn't mention that class, it won't work unless
13525 the check for all static symbols in lookup_symbol_aux
13526 saves you. See the OtherFileClass tests in
13527 gdb.c++/namespace.exp. */
13528
13529 if (!suppress_add)
13530 {
13531 list_to_add = (cu->list_in_scope == &file_symbols
13532 && (cu->language == language_cplus
13533 || cu->language == language_java)
13534 ? &global_symbols : cu->list_in_scope);
13535
13536 /* The semantics of C++ state that "struct foo {
13537 ... }" also defines a typedef for "foo". A Java
13538 class declaration also defines a typedef for the
13539 class. */
13540 if (cu->language == language_cplus
13541 || cu->language == language_java
13542 || cu->language == language_ada)
13543 {
13544 /* The symbol's name is already allocated along
13545 with this objfile, so we don't need to
13546 duplicate it for the type. */
13547 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13548 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13549 }
13550 }
13551 }
13552 break;
13553 case DW_TAG_typedef:
13554 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13555 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13556 list_to_add = cu->list_in_scope;
13557 break;
13558 case DW_TAG_base_type:
13559 case DW_TAG_subrange_type:
13560 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13561 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13562 list_to_add = cu->list_in_scope;
13563 break;
13564 case DW_TAG_enumerator:
13565 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13566 if (attr)
13567 {
13568 dwarf2_const_value (attr, sym, cu);
13569 }
13570 {
13571 /* NOTE: carlton/2003-11-10: See comment above in the
13572 DW_TAG_class_type, etc. block. */
13573
13574 list_to_add = (cu->list_in_scope == &file_symbols
13575 && (cu->language == language_cplus
13576 || cu->language == language_java)
13577 ? &global_symbols : cu->list_in_scope);
13578 }
13579 break;
13580 case DW_TAG_namespace:
13581 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13582 list_to_add = &global_symbols;
13583 break;
13584 default:
13585 /* Not a tag we recognize. Hopefully we aren't processing
13586 trash data, but since we must specifically ignore things
13587 we don't recognize, there is nothing else we should do at
13588 this point. */
13589 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13590 dwarf_tag_name (die->tag));
13591 break;
13592 }
13593
13594 if (suppress_add)
13595 {
13596 sym->hash_next = objfile->template_symbols;
13597 objfile->template_symbols = sym;
13598 list_to_add = NULL;
13599 }
13600
13601 if (list_to_add != NULL)
13602 add_symbol_to_list (sym, list_to_add);
13603
13604 /* For the benefit of old versions of GCC, check for anonymous
13605 namespaces based on the demangled name. */
13606 if (!processing_has_namespace_info
13607 && cu->language == language_cplus)
13608 cp_scan_for_anonymous_namespaces (sym, objfile);
13609 }
13610 return (sym);
13611 }
13612
13613 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13614
13615 static struct symbol *
13616 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13617 {
13618 return new_symbol_full (die, type, cu, NULL);
13619 }
13620
13621 /* Given an attr with a DW_FORM_dataN value in host byte order,
13622 zero-extend it as appropriate for the symbol's type. The DWARF
13623 standard (v4) is not entirely clear about the meaning of using
13624 DW_FORM_dataN for a constant with a signed type, where the type is
13625 wider than the data. The conclusion of a discussion on the DWARF
13626 list was that this is unspecified. We choose to always zero-extend
13627 because that is the interpretation long in use by GCC. */
13628
13629 static gdb_byte *
13630 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13631 const char *name, struct obstack *obstack,
13632 struct dwarf2_cu *cu, LONGEST *value, int bits)
13633 {
13634 struct objfile *objfile = cu->objfile;
13635 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13636 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13637 LONGEST l = DW_UNSND (attr);
13638
13639 if (bits < sizeof (*value) * 8)
13640 {
13641 l &= ((LONGEST) 1 << bits) - 1;
13642 *value = l;
13643 }
13644 else if (bits == sizeof (*value) * 8)
13645 *value = l;
13646 else
13647 {
13648 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13649 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13650 return bytes;
13651 }
13652
13653 return NULL;
13654 }
13655
13656 /* Read a constant value from an attribute. Either set *VALUE, or if
13657 the value does not fit in *VALUE, set *BYTES - either already
13658 allocated on the objfile obstack, or newly allocated on OBSTACK,
13659 or, set *BATON, if we translated the constant to a location
13660 expression. */
13661
13662 static void
13663 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13664 const char *name, struct obstack *obstack,
13665 struct dwarf2_cu *cu,
13666 LONGEST *value, gdb_byte **bytes,
13667 struct dwarf2_locexpr_baton **baton)
13668 {
13669 struct objfile *objfile = cu->objfile;
13670 struct comp_unit_head *cu_header = &cu->header;
13671 struct dwarf_block *blk;
13672 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13673 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13674
13675 *value = 0;
13676 *bytes = NULL;
13677 *baton = NULL;
13678
13679 switch (attr->form)
13680 {
13681 case DW_FORM_addr:
13682 case DW_FORM_GNU_addr_index:
13683 {
13684 gdb_byte *data;
13685
13686 if (TYPE_LENGTH (type) != cu_header->addr_size)
13687 dwarf2_const_value_length_mismatch_complaint (name,
13688 cu_header->addr_size,
13689 TYPE_LENGTH (type));
13690 /* Symbols of this form are reasonably rare, so we just
13691 piggyback on the existing location code rather than writing
13692 a new implementation of symbol_computed_ops. */
13693 *baton = obstack_alloc (&objfile->objfile_obstack,
13694 sizeof (struct dwarf2_locexpr_baton));
13695 (*baton)->per_cu = cu->per_cu;
13696 gdb_assert ((*baton)->per_cu);
13697
13698 (*baton)->size = 2 + cu_header->addr_size;
13699 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13700 (*baton)->data = data;
13701
13702 data[0] = DW_OP_addr;
13703 store_unsigned_integer (&data[1], cu_header->addr_size,
13704 byte_order, DW_ADDR (attr));
13705 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13706 }
13707 break;
13708 case DW_FORM_string:
13709 case DW_FORM_strp:
13710 case DW_FORM_GNU_str_index:
13711 /* DW_STRING is already allocated on the objfile obstack, point
13712 directly to it. */
13713 *bytes = (gdb_byte *) DW_STRING (attr);
13714 break;
13715 case DW_FORM_block1:
13716 case DW_FORM_block2:
13717 case DW_FORM_block4:
13718 case DW_FORM_block:
13719 case DW_FORM_exprloc:
13720 blk = DW_BLOCK (attr);
13721 if (TYPE_LENGTH (type) != blk->size)
13722 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13723 TYPE_LENGTH (type));
13724 *bytes = blk->data;
13725 break;
13726
13727 /* The DW_AT_const_value attributes are supposed to carry the
13728 symbol's value "represented as it would be on the target
13729 architecture." By the time we get here, it's already been
13730 converted to host endianness, so we just need to sign- or
13731 zero-extend it as appropriate. */
13732 case DW_FORM_data1:
13733 *bytes = dwarf2_const_value_data (attr, type, name,
13734 obstack, cu, value, 8);
13735 break;
13736 case DW_FORM_data2:
13737 *bytes = dwarf2_const_value_data (attr, type, name,
13738 obstack, cu, value, 16);
13739 break;
13740 case DW_FORM_data4:
13741 *bytes = dwarf2_const_value_data (attr, type, name,
13742 obstack, cu, value, 32);
13743 break;
13744 case DW_FORM_data8:
13745 *bytes = dwarf2_const_value_data (attr, type, name,
13746 obstack, cu, value, 64);
13747 break;
13748
13749 case DW_FORM_sdata:
13750 *value = DW_SND (attr);
13751 break;
13752
13753 case DW_FORM_udata:
13754 *value = DW_UNSND (attr);
13755 break;
13756
13757 default:
13758 complaint (&symfile_complaints,
13759 _("unsupported const value attribute form: '%s'"),
13760 dwarf_form_name (attr->form));
13761 *value = 0;
13762 break;
13763 }
13764 }
13765
13766
13767 /* Copy constant value from an attribute to a symbol. */
13768
13769 static void
13770 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13771 struct dwarf2_cu *cu)
13772 {
13773 struct objfile *objfile = cu->objfile;
13774 struct comp_unit_head *cu_header = &cu->header;
13775 LONGEST value;
13776 gdb_byte *bytes;
13777 struct dwarf2_locexpr_baton *baton;
13778
13779 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13780 SYMBOL_PRINT_NAME (sym),
13781 &objfile->objfile_obstack, cu,
13782 &value, &bytes, &baton);
13783
13784 if (baton != NULL)
13785 {
13786 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13787 SYMBOL_LOCATION_BATON (sym) = baton;
13788 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13789 }
13790 else if (bytes != NULL)
13791 {
13792 SYMBOL_VALUE_BYTES (sym) = bytes;
13793 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13794 }
13795 else
13796 {
13797 SYMBOL_VALUE (sym) = value;
13798 SYMBOL_CLASS (sym) = LOC_CONST;
13799 }
13800 }
13801
13802 /* Return the type of the die in question using its DW_AT_type attribute. */
13803
13804 static struct type *
13805 die_type (struct die_info *die, struct dwarf2_cu *cu)
13806 {
13807 struct attribute *type_attr;
13808
13809 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13810 if (!type_attr)
13811 {
13812 /* A missing DW_AT_type represents a void type. */
13813 return objfile_type (cu->objfile)->builtin_void;
13814 }
13815
13816 return lookup_die_type (die, type_attr, cu);
13817 }
13818
13819 /* True iff CU's producer generates GNAT Ada auxiliary information
13820 that allows to find parallel types through that information instead
13821 of having to do expensive parallel lookups by type name. */
13822
13823 static int
13824 need_gnat_info (struct dwarf2_cu *cu)
13825 {
13826 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13827 of GNAT produces this auxiliary information, without any indication
13828 that it is produced. Part of enhancing the FSF version of GNAT
13829 to produce that information will be to put in place an indicator
13830 that we can use in order to determine whether the descriptive type
13831 info is available or not. One suggestion that has been made is
13832 to use a new attribute, attached to the CU die. For now, assume
13833 that the descriptive type info is not available. */
13834 return 0;
13835 }
13836
13837 /* Return the auxiliary type of the die in question using its
13838 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13839 attribute is not present. */
13840
13841 static struct type *
13842 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13843 {
13844 struct attribute *type_attr;
13845
13846 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13847 if (!type_attr)
13848 return NULL;
13849
13850 return lookup_die_type (die, type_attr, cu);
13851 }
13852
13853 /* If DIE has a descriptive_type attribute, then set the TYPE's
13854 descriptive type accordingly. */
13855
13856 static void
13857 set_descriptive_type (struct type *type, struct die_info *die,
13858 struct dwarf2_cu *cu)
13859 {
13860 struct type *descriptive_type = die_descriptive_type (die, cu);
13861
13862 if (descriptive_type)
13863 {
13864 ALLOCATE_GNAT_AUX_TYPE (type);
13865 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13866 }
13867 }
13868
13869 /* Return the containing type of the die in question using its
13870 DW_AT_containing_type attribute. */
13871
13872 static struct type *
13873 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13874 {
13875 struct attribute *type_attr;
13876
13877 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13878 if (!type_attr)
13879 error (_("Dwarf Error: Problem turning containing type into gdb type "
13880 "[in module %s]"), cu->objfile->name);
13881
13882 return lookup_die_type (die, type_attr, cu);
13883 }
13884
13885 /* Look up the type of DIE in CU using its type attribute ATTR.
13886 If there is no type substitute an error marker. */
13887
13888 static struct type *
13889 lookup_die_type (struct die_info *die, struct attribute *attr,
13890 struct dwarf2_cu *cu)
13891 {
13892 struct objfile *objfile = cu->objfile;
13893 struct type *this_type;
13894
13895 /* First see if we have it cached. */
13896
13897 if (is_ref_attr (attr))
13898 {
13899 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13900
13901 this_type = get_die_type_at_offset (offset, cu->per_cu);
13902 }
13903 else if (attr->form == DW_FORM_ref_sig8)
13904 {
13905 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13906
13907 /* sig_type will be NULL if the signatured type is missing from
13908 the debug info. */
13909 if (sig_type == NULL)
13910 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13911 "at 0x%x [in module %s]"),
13912 die->offset.sect_off, objfile->name);
13913
13914 gdb_assert (sig_type->per_cu.is_debug_types);
13915 /* If we haven't filled in type_offset_in_section yet, then we
13916 haven't read the type in yet. */
13917 this_type = NULL;
13918 if (sig_type->type_offset_in_section.sect_off != 0)
13919 {
13920 this_type =
13921 get_die_type_at_offset (sig_type->type_offset_in_section,
13922 &sig_type->per_cu);
13923 }
13924 }
13925 else
13926 {
13927 dump_die_for_error (die);
13928 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13929 dwarf_attr_name (attr->name), objfile->name);
13930 }
13931
13932 /* If not cached we need to read it in. */
13933
13934 if (this_type == NULL)
13935 {
13936 struct die_info *type_die;
13937 struct dwarf2_cu *type_cu = cu;
13938
13939 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13940 /* If we found the type now, it's probably because the type came
13941 from an inter-CU reference and the type's CU got expanded before
13942 ours. */
13943 this_type = get_die_type (type_die, type_cu);
13944 if (this_type == NULL)
13945 this_type = read_type_die_1 (type_die, type_cu);
13946 }
13947
13948 /* If we still don't have a type use an error marker. */
13949
13950 if (this_type == NULL)
13951 {
13952 char *message, *saved;
13953
13954 /* read_type_die already issued a complaint. */
13955 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13956 objfile->name,
13957 cu->header.offset.sect_off,
13958 die->offset.sect_off);
13959 saved = obstack_copy0 (&objfile->objfile_obstack,
13960 message, strlen (message));
13961 xfree (message);
13962
13963 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13964 }
13965
13966 return this_type;
13967 }
13968
13969 /* Return the type in DIE, CU.
13970 Returns NULL for invalid types.
13971
13972 This first does a lookup in the appropriate type_hash table,
13973 and only reads the die in if necessary.
13974
13975 NOTE: This can be called when reading in partial or full symbols. */
13976
13977 static struct type *
13978 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13979 {
13980 struct type *this_type;
13981
13982 this_type = get_die_type (die, cu);
13983 if (this_type)
13984 return this_type;
13985
13986 return read_type_die_1 (die, cu);
13987 }
13988
13989 /* Read the type in DIE, CU.
13990 Returns NULL for invalid types. */
13991
13992 static struct type *
13993 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13994 {
13995 struct type *this_type = NULL;
13996
13997 switch (die->tag)
13998 {
13999 case DW_TAG_class_type:
14000 case DW_TAG_interface_type:
14001 case DW_TAG_structure_type:
14002 case DW_TAG_union_type:
14003 this_type = read_structure_type (die, cu);
14004 break;
14005 case DW_TAG_enumeration_type:
14006 this_type = read_enumeration_type (die, cu);
14007 break;
14008 case DW_TAG_subprogram:
14009 case DW_TAG_subroutine_type:
14010 case DW_TAG_inlined_subroutine:
14011 this_type = read_subroutine_type (die, cu);
14012 break;
14013 case DW_TAG_array_type:
14014 this_type = read_array_type (die, cu);
14015 break;
14016 case DW_TAG_set_type:
14017 this_type = read_set_type (die, cu);
14018 break;
14019 case DW_TAG_pointer_type:
14020 this_type = read_tag_pointer_type (die, cu);
14021 break;
14022 case DW_TAG_ptr_to_member_type:
14023 this_type = read_tag_ptr_to_member_type (die, cu);
14024 break;
14025 case DW_TAG_reference_type:
14026 this_type = read_tag_reference_type (die, cu);
14027 break;
14028 case DW_TAG_const_type:
14029 this_type = read_tag_const_type (die, cu);
14030 break;
14031 case DW_TAG_volatile_type:
14032 this_type = read_tag_volatile_type (die, cu);
14033 break;
14034 case DW_TAG_string_type:
14035 this_type = read_tag_string_type (die, cu);
14036 break;
14037 case DW_TAG_typedef:
14038 this_type = read_typedef (die, cu);
14039 break;
14040 case DW_TAG_subrange_type:
14041 this_type = read_subrange_type (die, cu);
14042 break;
14043 case DW_TAG_base_type:
14044 this_type = read_base_type (die, cu);
14045 break;
14046 case DW_TAG_unspecified_type:
14047 this_type = read_unspecified_type (die, cu);
14048 break;
14049 case DW_TAG_namespace:
14050 this_type = read_namespace_type (die, cu);
14051 break;
14052 case DW_TAG_module:
14053 this_type = read_module_type (die, cu);
14054 break;
14055 default:
14056 complaint (&symfile_complaints,
14057 _("unexpected tag in read_type_die: '%s'"),
14058 dwarf_tag_name (die->tag));
14059 break;
14060 }
14061
14062 return this_type;
14063 }
14064
14065 /* See if we can figure out if the class lives in a namespace. We do
14066 this by looking for a member function; its demangled name will
14067 contain namespace info, if there is any.
14068 Return the computed name or NULL.
14069 Space for the result is allocated on the objfile's obstack.
14070 This is the full-die version of guess_partial_die_structure_name.
14071 In this case we know DIE has no useful parent. */
14072
14073 static char *
14074 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14075 {
14076 struct die_info *spec_die;
14077 struct dwarf2_cu *spec_cu;
14078 struct die_info *child;
14079
14080 spec_cu = cu;
14081 spec_die = die_specification (die, &spec_cu);
14082 if (spec_die != NULL)
14083 {
14084 die = spec_die;
14085 cu = spec_cu;
14086 }
14087
14088 for (child = die->child;
14089 child != NULL;
14090 child = child->sibling)
14091 {
14092 if (child->tag == DW_TAG_subprogram)
14093 {
14094 struct attribute *attr;
14095
14096 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14097 if (attr == NULL)
14098 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14099 if (attr != NULL)
14100 {
14101 char *actual_name
14102 = language_class_name_from_physname (cu->language_defn,
14103 DW_STRING (attr));
14104 char *name = NULL;
14105
14106 if (actual_name != NULL)
14107 {
14108 char *die_name = dwarf2_name (die, cu);
14109
14110 if (die_name != NULL
14111 && strcmp (die_name, actual_name) != 0)
14112 {
14113 /* Strip off the class name from the full name.
14114 We want the prefix. */
14115 int die_name_len = strlen (die_name);
14116 int actual_name_len = strlen (actual_name);
14117
14118 /* Test for '::' as a sanity check. */
14119 if (actual_name_len > die_name_len + 2
14120 && actual_name[actual_name_len
14121 - die_name_len - 1] == ':')
14122 name =
14123 obsavestring (actual_name,
14124 actual_name_len - die_name_len - 2,
14125 &cu->objfile->objfile_obstack);
14126 }
14127 }
14128 xfree (actual_name);
14129 return name;
14130 }
14131 }
14132 }
14133
14134 return NULL;
14135 }
14136
14137 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14138 prefix part in such case. See
14139 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14140
14141 static char *
14142 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14143 {
14144 struct attribute *attr;
14145 char *base;
14146
14147 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14148 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14149 return NULL;
14150
14151 attr = dwarf2_attr (die, DW_AT_name, cu);
14152 if (attr != NULL && DW_STRING (attr) != NULL)
14153 return NULL;
14154
14155 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14156 if (attr == NULL)
14157 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14158 if (attr == NULL || DW_STRING (attr) == NULL)
14159 return NULL;
14160
14161 /* dwarf2_name had to be already called. */
14162 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14163
14164 /* Strip the base name, keep any leading namespaces/classes. */
14165 base = strrchr (DW_STRING (attr), ':');
14166 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14167 return "";
14168
14169 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14170 &cu->objfile->objfile_obstack);
14171 }
14172
14173 /* Return the name of the namespace/class that DIE is defined within,
14174 or "" if we can't tell. The caller should not xfree the result.
14175
14176 For example, if we're within the method foo() in the following
14177 code:
14178
14179 namespace N {
14180 class C {
14181 void foo () {
14182 }
14183 };
14184 }
14185
14186 then determine_prefix on foo's die will return "N::C". */
14187
14188 static const char *
14189 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14190 {
14191 struct die_info *parent, *spec_die;
14192 struct dwarf2_cu *spec_cu;
14193 struct type *parent_type;
14194 char *retval;
14195
14196 if (cu->language != language_cplus && cu->language != language_java
14197 && cu->language != language_fortran)
14198 return "";
14199
14200 retval = anonymous_struct_prefix (die, cu);
14201 if (retval)
14202 return retval;
14203
14204 /* We have to be careful in the presence of DW_AT_specification.
14205 For example, with GCC 3.4, given the code
14206
14207 namespace N {
14208 void foo() {
14209 // Definition of N::foo.
14210 }
14211 }
14212
14213 then we'll have a tree of DIEs like this:
14214
14215 1: DW_TAG_compile_unit
14216 2: DW_TAG_namespace // N
14217 3: DW_TAG_subprogram // declaration of N::foo
14218 4: DW_TAG_subprogram // definition of N::foo
14219 DW_AT_specification // refers to die #3
14220
14221 Thus, when processing die #4, we have to pretend that we're in
14222 the context of its DW_AT_specification, namely the contex of die
14223 #3. */
14224 spec_cu = cu;
14225 spec_die = die_specification (die, &spec_cu);
14226 if (spec_die == NULL)
14227 parent = die->parent;
14228 else
14229 {
14230 parent = spec_die->parent;
14231 cu = spec_cu;
14232 }
14233
14234 if (parent == NULL)
14235 return "";
14236 else if (parent->building_fullname)
14237 {
14238 const char *name;
14239 const char *parent_name;
14240
14241 /* It has been seen on RealView 2.2 built binaries,
14242 DW_TAG_template_type_param types actually _defined_ as
14243 children of the parent class:
14244
14245 enum E {};
14246 template class <class Enum> Class{};
14247 Class<enum E> class_e;
14248
14249 1: DW_TAG_class_type (Class)
14250 2: DW_TAG_enumeration_type (E)
14251 3: DW_TAG_enumerator (enum1:0)
14252 3: DW_TAG_enumerator (enum2:1)
14253 ...
14254 2: DW_TAG_template_type_param
14255 DW_AT_type DW_FORM_ref_udata (E)
14256
14257 Besides being broken debug info, it can put GDB into an
14258 infinite loop. Consider:
14259
14260 When we're building the full name for Class<E>, we'll start
14261 at Class, and go look over its template type parameters,
14262 finding E. We'll then try to build the full name of E, and
14263 reach here. We're now trying to build the full name of E,
14264 and look over the parent DIE for containing scope. In the
14265 broken case, if we followed the parent DIE of E, we'd again
14266 find Class, and once again go look at its template type
14267 arguments, etc., etc. Simply don't consider such parent die
14268 as source-level parent of this die (it can't be, the language
14269 doesn't allow it), and break the loop here. */
14270 name = dwarf2_name (die, cu);
14271 parent_name = dwarf2_name (parent, cu);
14272 complaint (&symfile_complaints,
14273 _("template param type '%s' defined within parent '%s'"),
14274 name ? name : "<unknown>",
14275 parent_name ? parent_name : "<unknown>");
14276 return "";
14277 }
14278 else
14279 switch (parent->tag)
14280 {
14281 case DW_TAG_namespace:
14282 parent_type = read_type_die (parent, cu);
14283 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14284 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14285 Work around this problem here. */
14286 if (cu->language == language_cplus
14287 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14288 return "";
14289 /* We give a name to even anonymous namespaces. */
14290 return TYPE_TAG_NAME (parent_type);
14291 case DW_TAG_class_type:
14292 case DW_TAG_interface_type:
14293 case DW_TAG_structure_type:
14294 case DW_TAG_union_type:
14295 case DW_TAG_module:
14296 parent_type = read_type_die (parent, cu);
14297 if (TYPE_TAG_NAME (parent_type) != NULL)
14298 return TYPE_TAG_NAME (parent_type);
14299 else
14300 /* An anonymous structure is only allowed non-static data
14301 members; no typedefs, no member functions, et cetera.
14302 So it does not need a prefix. */
14303 return "";
14304 case DW_TAG_compile_unit:
14305 case DW_TAG_partial_unit:
14306 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14307 if (cu->language == language_cplus
14308 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14309 && die->child != NULL
14310 && (die->tag == DW_TAG_class_type
14311 || die->tag == DW_TAG_structure_type
14312 || die->tag == DW_TAG_union_type))
14313 {
14314 char *name = guess_full_die_structure_name (die, cu);
14315 if (name != NULL)
14316 return name;
14317 }
14318 return "";
14319 default:
14320 return determine_prefix (parent, cu);
14321 }
14322 }
14323
14324 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14325 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14326 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14327 an obconcat, otherwise allocate storage for the result. The CU argument is
14328 used to determine the language and hence, the appropriate separator. */
14329
14330 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14331
14332 static char *
14333 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14334 int physname, struct dwarf2_cu *cu)
14335 {
14336 const char *lead = "";
14337 const char *sep;
14338
14339 if (suffix == NULL || suffix[0] == '\0'
14340 || prefix == NULL || prefix[0] == '\0')
14341 sep = "";
14342 else if (cu->language == language_java)
14343 sep = ".";
14344 else if (cu->language == language_fortran && physname)
14345 {
14346 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14347 DW_AT_MIPS_linkage_name is preferred and used instead. */
14348
14349 lead = "__";
14350 sep = "_MOD_";
14351 }
14352 else
14353 sep = "::";
14354
14355 if (prefix == NULL)
14356 prefix = "";
14357 if (suffix == NULL)
14358 suffix = "";
14359
14360 if (obs == NULL)
14361 {
14362 char *retval
14363 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14364
14365 strcpy (retval, lead);
14366 strcat (retval, prefix);
14367 strcat (retval, sep);
14368 strcat (retval, suffix);
14369 return retval;
14370 }
14371 else
14372 {
14373 /* We have an obstack. */
14374 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14375 }
14376 }
14377
14378 /* Return sibling of die, NULL if no sibling. */
14379
14380 static struct die_info *
14381 sibling_die (struct die_info *die)
14382 {
14383 return die->sibling;
14384 }
14385
14386 /* Get name of a die, return NULL if not found. */
14387
14388 static char *
14389 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14390 struct obstack *obstack)
14391 {
14392 if (name && cu->language == language_cplus)
14393 {
14394 char *canon_name = cp_canonicalize_string (name);
14395
14396 if (canon_name != NULL)
14397 {
14398 if (strcmp (canon_name, name) != 0)
14399 name = obsavestring (canon_name, strlen (canon_name),
14400 obstack);
14401 xfree (canon_name);
14402 }
14403 }
14404
14405 return name;
14406 }
14407
14408 /* Get name of a die, return NULL if not found. */
14409
14410 static char *
14411 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14412 {
14413 struct attribute *attr;
14414
14415 attr = dwarf2_attr (die, DW_AT_name, cu);
14416 if ((!attr || !DW_STRING (attr))
14417 && die->tag != DW_TAG_class_type
14418 && die->tag != DW_TAG_interface_type
14419 && die->tag != DW_TAG_structure_type
14420 && die->tag != DW_TAG_union_type)
14421 return NULL;
14422
14423 switch (die->tag)
14424 {
14425 case DW_TAG_compile_unit:
14426 case DW_TAG_partial_unit:
14427 /* Compilation units have a DW_AT_name that is a filename, not
14428 a source language identifier. */
14429 case DW_TAG_enumeration_type:
14430 case DW_TAG_enumerator:
14431 /* These tags always have simple identifiers already; no need
14432 to canonicalize them. */
14433 return DW_STRING (attr);
14434
14435 case DW_TAG_subprogram:
14436 /* Java constructors will all be named "<init>", so return
14437 the class name when we see this special case. */
14438 if (cu->language == language_java
14439 && DW_STRING (attr) != NULL
14440 && strcmp (DW_STRING (attr), "<init>") == 0)
14441 {
14442 struct dwarf2_cu *spec_cu = cu;
14443 struct die_info *spec_die;
14444
14445 /* GCJ will output '<init>' for Java constructor names.
14446 For this special case, return the name of the parent class. */
14447
14448 /* GCJ may output suprogram DIEs with AT_specification set.
14449 If so, use the name of the specified DIE. */
14450 spec_die = die_specification (die, &spec_cu);
14451 if (spec_die != NULL)
14452 return dwarf2_name (spec_die, spec_cu);
14453
14454 do
14455 {
14456 die = die->parent;
14457 if (die->tag == DW_TAG_class_type)
14458 return dwarf2_name (die, cu);
14459 }
14460 while (die->tag != DW_TAG_compile_unit
14461 && die->tag != DW_TAG_partial_unit);
14462 }
14463 break;
14464
14465 case DW_TAG_class_type:
14466 case DW_TAG_interface_type:
14467 case DW_TAG_structure_type:
14468 case DW_TAG_union_type:
14469 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14470 structures or unions. These were of the form "._%d" in GCC 4.1,
14471 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14472 and GCC 4.4. We work around this problem by ignoring these. */
14473 if (attr && DW_STRING (attr)
14474 && (strncmp (DW_STRING (attr), "._", 2) == 0
14475 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14476 return NULL;
14477
14478 /* GCC might emit a nameless typedef that has a linkage name. See
14479 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14480 if (!attr || DW_STRING (attr) == NULL)
14481 {
14482 char *demangled = NULL;
14483
14484 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14485 if (attr == NULL)
14486 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14487
14488 if (attr == NULL || DW_STRING (attr) == NULL)
14489 return NULL;
14490
14491 /* Avoid demangling DW_STRING (attr) the second time on a second
14492 call for the same DIE. */
14493 if (!DW_STRING_IS_CANONICAL (attr))
14494 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14495
14496 if (demangled)
14497 {
14498 char *base;
14499
14500 /* FIXME: we already did this for the partial symbol... */
14501 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14502 &cu->objfile->objfile_obstack);
14503 DW_STRING_IS_CANONICAL (attr) = 1;
14504 xfree (demangled);
14505
14506 /* Strip any leading namespaces/classes, keep only the base name.
14507 DW_AT_name for named DIEs does not contain the prefixes. */
14508 base = strrchr (DW_STRING (attr), ':');
14509 if (base && base > DW_STRING (attr) && base[-1] == ':')
14510 return &base[1];
14511 else
14512 return DW_STRING (attr);
14513 }
14514 }
14515 break;
14516
14517 default:
14518 break;
14519 }
14520
14521 if (!DW_STRING_IS_CANONICAL (attr))
14522 {
14523 DW_STRING (attr)
14524 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14525 &cu->objfile->objfile_obstack);
14526 DW_STRING_IS_CANONICAL (attr) = 1;
14527 }
14528 return DW_STRING (attr);
14529 }
14530
14531 /* Return the die that this die in an extension of, or NULL if there
14532 is none. *EXT_CU is the CU containing DIE on input, and the CU
14533 containing the return value on output. */
14534
14535 static struct die_info *
14536 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14537 {
14538 struct attribute *attr;
14539
14540 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14541 if (attr == NULL)
14542 return NULL;
14543
14544 return follow_die_ref (die, attr, ext_cu);
14545 }
14546
14547 /* Convert a DIE tag into its string name. */
14548
14549 static const char *
14550 dwarf_tag_name (unsigned tag)
14551 {
14552 const char *name = get_DW_TAG_name (tag);
14553
14554 if (name == NULL)
14555 return "DW_TAG_<unknown>";
14556
14557 return name;
14558 }
14559
14560 /* Convert a DWARF attribute code into its string name. */
14561
14562 static const char *
14563 dwarf_attr_name (unsigned attr)
14564 {
14565 const char *name;
14566
14567 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14568 if (attr == DW_AT_MIPS_fde)
14569 return "DW_AT_MIPS_fde";
14570 #else
14571 if (attr == DW_AT_HP_block_index)
14572 return "DW_AT_HP_block_index";
14573 #endif
14574
14575 name = get_DW_AT_name (attr);
14576
14577 if (name == NULL)
14578 return "DW_AT_<unknown>";
14579
14580 return name;
14581 }
14582
14583 /* Convert a DWARF value form code into its string name. */
14584
14585 static const char *
14586 dwarf_form_name (unsigned form)
14587 {
14588 const char *name = get_DW_FORM_name (form);
14589
14590 if (name == NULL)
14591 return "DW_FORM_<unknown>";
14592
14593 return name;
14594 }
14595
14596 static char *
14597 dwarf_bool_name (unsigned mybool)
14598 {
14599 if (mybool)
14600 return "TRUE";
14601 else
14602 return "FALSE";
14603 }
14604
14605 /* Convert a DWARF type code into its string name. */
14606
14607 static const char *
14608 dwarf_type_encoding_name (unsigned enc)
14609 {
14610 const char *name = get_DW_ATE_name (enc);
14611
14612 if (name == NULL)
14613 return "DW_ATE_<unknown>";
14614
14615 return name;
14616 }
14617
14618 static void
14619 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14620 {
14621 unsigned int i;
14622
14623 print_spaces (indent, f);
14624 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14625 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14626
14627 if (die->parent != NULL)
14628 {
14629 print_spaces (indent, f);
14630 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14631 die->parent->offset.sect_off);
14632 }
14633
14634 print_spaces (indent, f);
14635 fprintf_unfiltered (f, " has children: %s\n",
14636 dwarf_bool_name (die->child != NULL));
14637
14638 print_spaces (indent, f);
14639 fprintf_unfiltered (f, " attributes:\n");
14640
14641 for (i = 0; i < die->num_attrs; ++i)
14642 {
14643 print_spaces (indent, f);
14644 fprintf_unfiltered (f, " %s (%s) ",
14645 dwarf_attr_name (die->attrs[i].name),
14646 dwarf_form_name (die->attrs[i].form));
14647
14648 switch (die->attrs[i].form)
14649 {
14650 case DW_FORM_addr:
14651 case DW_FORM_GNU_addr_index:
14652 fprintf_unfiltered (f, "address: ");
14653 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14654 break;
14655 case DW_FORM_block2:
14656 case DW_FORM_block4:
14657 case DW_FORM_block:
14658 case DW_FORM_block1:
14659 fprintf_unfiltered (f, "block: size %d",
14660 DW_BLOCK (&die->attrs[i])->size);
14661 break;
14662 case DW_FORM_exprloc:
14663 fprintf_unfiltered (f, "expression: size %u",
14664 DW_BLOCK (&die->attrs[i])->size);
14665 break;
14666 case DW_FORM_ref_addr:
14667 fprintf_unfiltered (f, "ref address: ");
14668 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14669 break;
14670 case DW_FORM_ref1:
14671 case DW_FORM_ref2:
14672 case DW_FORM_ref4:
14673 case DW_FORM_ref8:
14674 case DW_FORM_ref_udata:
14675 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14676 (long) (DW_UNSND (&die->attrs[i])));
14677 break;
14678 case DW_FORM_data1:
14679 case DW_FORM_data2:
14680 case DW_FORM_data4:
14681 case DW_FORM_data8:
14682 case DW_FORM_udata:
14683 case DW_FORM_sdata:
14684 fprintf_unfiltered (f, "constant: %s",
14685 pulongest (DW_UNSND (&die->attrs[i])));
14686 break;
14687 case DW_FORM_sec_offset:
14688 fprintf_unfiltered (f, "section offset: %s",
14689 pulongest (DW_UNSND (&die->attrs[i])));
14690 break;
14691 case DW_FORM_ref_sig8:
14692 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14693 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14694 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14695 else
14696 fprintf_unfiltered (f, "signatured type, offset: unknown");
14697 break;
14698 case DW_FORM_string:
14699 case DW_FORM_strp:
14700 case DW_FORM_GNU_str_index:
14701 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14702 DW_STRING (&die->attrs[i])
14703 ? DW_STRING (&die->attrs[i]) : "",
14704 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14705 break;
14706 case DW_FORM_flag:
14707 if (DW_UNSND (&die->attrs[i]))
14708 fprintf_unfiltered (f, "flag: TRUE");
14709 else
14710 fprintf_unfiltered (f, "flag: FALSE");
14711 break;
14712 case DW_FORM_flag_present:
14713 fprintf_unfiltered (f, "flag: TRUE");
14714 break;
14715 case DW_FORM_indirect:
14716 /* The reader will have reduced the indirect form to
14717 the "base form" so this form should not occur. */
14718 fprintf_unfiltered (f,
14719 "unexpected attribute form: DW_FORM_indirect");
14720 break;
14721 default:
14722 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14723 die->attrs[i].form);
14724 break;
14725 }
14726 fprintf_unfiltered (f, "\n");
14727 }
14728 }
14729
14730 static void
14731 dump_die_for_error (struct die_info *die)
14732 {
14733 dump_die_shallow (gdb_stderr, 0, die);
14734 }
14735
14736 static void
14737 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14738 {
14739 int indent = level * 4;
14740
14741 gdb_assert (die != NULL);
14742
14743 if (level >= max_level)
14744 return;
14745
14746 dump_die_shallow (f, indent, die);
14747
14748 if (die->child != NULL)
14749 {
14750 print_spaces (indent, f);
14751 fprintf_unfiltered (f, " Children:");
14752 if (level + 1 < max_level)
14753 {
14754 fprintf_unfiltered (f, "\n");
14755 dump_die_1 (f, level + 1, max_level, die->child);
14756 }
14757 else
14758 {
14759 fprintf_unfiltered (f,
14760 " [not printed, max nesting level reached]\n");
14761 }
14762 }
14763
14764 if (die->sibling != NULL && level > 0)
14765 {
14766 dump_die_1 (f, level, max_level, die->sibling);
14767 }
14768 }
14769
14770 /* This is called from the pdie macro in gdbinit.in.
14771 It's not static so gcc will keep a copy callable from gdb. */
14772
14773 void
14774 dump_die (struct die_info *die, int max_level)
14775 {
14776 dump_die_1 (gdb_stdlog, 0, max_level, die);
14777 }
14778
14779 static void
14780 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14781 {
14782 void **slot;
14783
14784 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14785 INSERT);
14786
14787 *slot = die;
14788 }
14789
14790 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14791 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14792
14793 static int
14794 is_ref_attr (struct attribute *attr)
14795 {
14796 switch (attr->form)
14797 {
14798 case DW_FORM_ref_addr:
14799 case DW_FORM_ref1:
14800 case DW_FORM_ref2:
14801 case DW_FORM_ref4:
14802 case DW_FORM_ref8:
14803 case DW_FORM_ref_udata:
14804 return 1;
14805 default:
14806 return 0;
14807 }
14808 }
14809
14810 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14811 required kind. */
14812
14813 static sect_offset
14814 dwarf2_get_ref_die_offset (struct attribute *attr)
14815 {
14816 sect_offset retval = { DW_UNSND (attr) };
14817
14818 if (is_ref_attr (attr))
14819 return retval;
14820
14821 retval.sect_off = 0;
14822 complaint (&symfile_complaints,
14823 _("unsupported die ref attribute form: '%s'"),
14824 dwarf_form_name (attr->form));
14825 return retval;
14826 }
14827
14828 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14829 * the value held by the attribute is not constant. */
14830
14831 static LONGEST
14832 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14833 {
14834 if (attr->form == DW_FORM_sdata)
14835 return DW_SND (attr);
14836 else if (attr->form == DW_FORM_udata
14837 || attr->form == DW_FORM_data1
14838 || attr->form == DW_FORM_data2
14839 || attr->form == DW_FORM_data4
14840 || attr->form == DW_FORM_data8)
14841 return DW_UNSND (attr);
14842 else
14843 {
14844 complaint (&symfile_complaints,
14845 _("Attribute value is not a constant (%s)"),
14846 dwarf_form_name (attr->form));
14847 return default_value;
14848 }
14849 }
14850
14851 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14852 unit and add it to our queue.
14853 The result is non-zero if PER_CU was queued, otherwise the result is zero
14854 meaning either PER_CU is already queued or it is already loaded. */
14855
14856 static int
14857 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14858 struct dwarf2_per_cu_data *per_cu,
14859 enum language pretend_language)
14860 {
14861 /* We may arrive here during partial symbol reading, if we need full
14862 DIEs to process an unusual case (e.g. template arguments). Do
14863 not queue PER_CU, just tell our caller to load its DIEs. */
14864 if (dwarf2_per_objfile->reading_partial_symbols)
14865 {
14866 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14867 return 1;
14868 return 0;
14869 }
14870
14871 /* Mark the dependence relation so that we don't flush PER_CU
14872 too early. */
14873 dwarf2_add_dependence (this_cu, per_cu);
14874
14875 /* If it's already on the queue, we have nothing to do. */
14876 if (per_cu->queued)
14877 return 0;
14878
14879 /* If the compilation unit is already loaded, just mark it as
14880 used. */
14881 if (per_cu->cu != NULL)
14882 {
14883 per_cu->cu->last_used = 0;
14884 return 0;
14885 }
14886
14887 /* Add it to the queue. */
14888 queue_comp_unit (per_cu, pretend_language);
14889
14890 return 1;
14891 }
14892
14893 /* Follow reference or signature attribute ATTR of SRC_DIE.
14894 On entry *REF_CU is the CU of SRC_DIE.
14895 On exit *REF_CU is the CU of the result. */
14896
14897 static struct die_info *
14898 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14899 struct dwarf2_cu **ref_cu)
14900 {
14901 struct die_info *die;
14902
14903 if (is_ref_attr (attr))
14904 die = follow_die_ref (src_die, attr, ref_cu);
14905 else if (attr->form == DW_FORM_ref_sig8)
14906 die = follow_die_sig (src_die, attr, ref_cu);
14907 else
14908 {
14909 dump_die_for_error (src_die);
14910 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14911 (*ref_cu)->objfile->name);
14912 }
14913
14914 return die;
14915 }
14916
14917 /* Follow reference OFFSET.
14918 On entry *REF_CU is the CU of the source die referencing OFFSET.
14919 On exit *REF_CU is the CU of the result.
14920 Returns NULL if OFFSET is invalid. */
14921
14922 static struct die_info *
14923 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14924 {
14925 struct die_info temp_die;
14926 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14927
14928 gdb_assert (cu->per_cu != NULL);
14929
14930 target_cu = cu;
14931
14932 if (cu->per_cu->is_debug_types)
14933 {
14934 /* .debug_types CUs cannot reference anything outside their CU.
14935 If they need to, they have to reference a signatured type via
14936 DW_FORM_ref_sig8. */
14937 if (! offset_in_cu_p (&cu->header, offset))
14938 return NULL;
14939 }
14940 else if (! offset_in_cu_p (&cu->header, offset))
14941 {
14942 struct dwarf2_per_cu_data *per_cu;
14943
14944 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14945
14946 /* If necessary, add it to the queue and load its DIEs. */
14947 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14948 load_full_comp_unit (per_cu, cu->language);
14949
14950 target_cu = per_cu->cu;
14951 }
14952 else if (cu->dies == NULL)
14953 {
14954 /* We're loading full DIEs during partial symbol reading. */
14955 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14956 load_full_comp_unit (cu->per_cu, language_minimal);
14957 }
14958
14959 *ref_cu = target_cu;
14960 temp_die.offset = offset;
14961 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14962 }
14963
14964 /* Follow reference attribute ATTR of SRC_DIE.
14965 On entry *REF_CU is the CU of SRC_DIE.
14966 On exit *REF_CU is the CU of the result. */
14967
14968 static struct die_info *
14969 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14970 struct dwarf2_cu **ref_cu)
14971 {
14972 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14973 struct dwarf2_cu *cu = *ref_cu;
14974 struct die_info *die;
14975
14976 die = follow_die_offset (offset, ref_cu);
14977 if (!die)
14978 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14979 "at 0x%x [in module %s]"),
14980 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14981
14982 return die;
14983 }
14984
14985 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14986 Returned value is intended for DW_OP_call*. Returned
14987 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14988
14989 struct dwarf2_locexpr_baton
14990 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14991 struct dwarf2_per_cu_data *per_cu,
14992 CORE_ADDR (*get_frame_pc) (void *baton),
14993 void *baton)
14994 {
14995 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14996 struct dwarf2_cu *cu;
14997 struct die_info *die;
14998 struct attribute *attr;
14999 struct dwarf2_locexpr_baton retval;
15000
15001 dw2_setup (per_cu->objfile);
15002
15003 if (per_cu->cu == NULL)
15004 load_cu (per_cu);
15005 cu = per_cu->cu;
15006
15007 die = follow_die_offset (offset, &cu);
15008 if (!die)
15009 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15010 offset.sect_off, per_cu->objfile->name);
15011
15012 attr = dwarf2_attr (die, DW_AT_location, cu);
15013 if (!attr)
15014 {
15015 /* DWARF: "If there is no such attribute, then there is no effect.".
15016 DATA is ignored if SIZE is 0. */
15017
15018 retval.data = NULL;
15019 retval.size = 0;
15020 }
15021 else if (attr_form_is_section_offset (attr))
15022 {
15023 struct dwarf2_loclist_baton loclist_baton;
15024 CORE_ADDR pc = (*get_frame_pc) (baton);
15025 size_t size;
15026
15027 fill_in_loclist_baton (cu, &loclist_baton, attr);
15028
15029 retval.data = dwarf2_find_location_expression (&loclist_baton,
15030 &size, pc);
15031 retval.size = size;
15032 }
15033 else
15034 {
15035 if (!attr_form_is_block (attr))
15036 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15037 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15038 offset.sect_off, per_cu->objfile->name);
15039
15040 retval.data = DW_BLOCK (attr)->data;
15041 retval.size = DW_BLOCK (attr)->size;
15042 }
15043 retval.per_cu = cu->per_cu;
15044
15045 age_cached_comp_units ();
15046
15047 return retval;
15048 }
15049
15050 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15051 PER_CU. */
15052
15053 struct type *
15054 dwarf2_get_die_type (cu_offset die_offset,
15055 struct dwarf2_per_cu_data *per_cu)
15056 {
15057 sect_offset die_offset_sect;
15058
15059 dw2_setup (per_cu->objfile);
15060
15061 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15062 return get_die_type_at_offset (die_offset_sect, per_cu);
15063 }
15064
15065 /* Follow the signature attribute ATTR in SRC_DIE.
15066 On entry *REF_CU is the CU of SRC_DIE.
15067 On exit *REF_CU is the CU of the result. */
15068
15069 static struct die_info *
15070 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15071 struct dwarf2_cu **ref_cu)
15072 {
15073 struct objfile *objfile = (*ref_cu)->objfile;
15074 struct die_info temp_die;
15075 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15076 struct dwarf2_cu *sig_cu;
15077 struct die_info *die;
15078
15079 /* sig_type will be NULL if the signatured type is missing from
15080 the debug info. */
15081 if (sig_type == NULL)
15082 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15083 "at 0x%x [in module %s]"),
15084 src_die->offset.sect_off, objfile->name);
15085
15086 /* If necessary, add it to the queue and load its DIEs. */
15087
15088 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15089 read_signatured_type (sig_type);
15090
15091 gdb_assert (sig_type->per_cu.cu != NULL);
15092
15093 sig_cu = sig_type->per_cu.cu;
15094 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15095 temp_die.offset = sig_type->type_offset_in_section;
15096 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15097 temp_die.offset.sect_off);
15098 if (die)
15099 {
15100 *ref_cu = sig_cu;
15101 return die;
15102 }
15103
15104 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15105 "from DIE at 0x%x [in module %s]"),
15106 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15107 }
15108
15109 /* Given an offset of a signatured type, return its signatured_type. */
15110
15111 static struct signatured_type *
15112 lookup_signatured_type_at_offset (struct objfile *objfile,
15113 struct dwarf2_section_info *section,
15114 sect_offset offset)
15115 {
15116 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15117 unsigned int length, initial_length_size;
15118 unsigned int sig_offset;
15119 struct signatured_type find_entry, *sig_type;
15120
15121 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15122 sig_offset = (initial_length_size
15123 + 2 /*version*/
15124 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15125 + 1 /*address_size*/);
15126 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15127 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15128
15129 /* This is only used to lookup previously recorded types.
15130 If we didn't find it, it's our bug. */
15131 gdb_assert (sig_type != NULL);
15132 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15133
15134 return sig_type;
15135 }
15136
15137 /* Load the DIEs associated with type unit PER_CU into memory. */
15138
15139 static void
15140 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15141 {
15142 struct objfile *objfile = per_cu->objfile;
15143 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15144 sect_offset offset = per_cu->offset;
15145 struct signatured_type *sig_type;
15146
15147 dwarf2_read_section (objfile, sect);
15148
15149 /* We have the section offset, but we need the signature to do the
15150 hash table lookup. */
15151 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15152 the signature to assert we found the right one.
15153 Ok, but it's a lot of work. We should simplify things so any needed
15154 assert doesn't require all this clumsiness. */
15155 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15156
15157 gdb_assert (&sig_type->per_cu == per_cu);
15158 gdb_assert (sig_type->per_cu.cu == NULL);
15159
15160 read_signatured_type (sig_type);
15161
15162 gdb_assert (sig_type->per_cu.cu != NULL);
15163 }
15164
15165 /* die_reader_func for read_signatured_type.
15166 This is identical to load_full_comp_unit_reader,
15167 but is kept separate for now. */
15168
15169 static void
15170 read_signatured_type_reader (const struct die_reader_specs *reader,
15171 gdb_byte *info_ptr,
15172 struct die_info *comp_unit_die,
15173 int has_children,
15174 void *data)
15175 {
15176 struct dwarf2_cu *cu = reader->cu;
15177
15178 gdb_assert (cu->die_hash == NULL);
15179 cu->die_hash =
15180 htab_create_alloc_ex (cu->header.length / 12,
15181 die_hash,
15182 die_eq,
15183 NULL,
15184 &cu->comp_unit_obstack,
15185 hashtab_obstack_allocate,
15186 dummy_obstack_deallocate);
15187
15188 if (has_children)
15189 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15190 &info_ptr, comp_unit_die);
15191 cu->dies = comp_unit_die;
15192 /* comp_unit_die is not stored in die_hash, no need. */
15193
15194 /* We try not to read any attributes in this function, because not
15195 all CUs needed for references have been loaded yet, and symbol
15196 table processing isn't initialized. But we have to set the CU language,
15197 or we won't be able to build types correctly.
15198 Similarly, if we do not read the producer, we can not apply
15199 producer-specific interpretation. */
15200 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15201 }
15202
15203 /* Read in a signatured type and build its CU and DIEs.
15204 If the type is a stub for the real type in a DWO file,
15205 read in the real type from the DWO file as well. */
15206
15207 static void
15208 read_signatured_type (struct signatured_type *sig_type)
15209 {
15210 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15211
15212 gdb_assert (per_cu->is_debug_types);
15213 gdb_assert (per_cu->cu == NULL);
15214
15215 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15216 }
15217
15218 /* Decode simple location descriptions.
15219 Given a pointer to a dwarf block that defines a location, compute
15220 the location and return the value.
15221
15222 NOTE drow/2003-11-18: This function is called in two situations
15223 now: for the address of static or global variables (partial symbols
15224 only) and for offsets into structures which are expected to be
15225 (more or less) constant. The partial symbol case should go away,
15226 and only the constant case should remain. That will let this
15227 function complain more accurately. A few special modes are allowed
15228 without complaint for global variables (for instance, global
15229 register values and thread-local values).
15230
15231 A location description containing no operations indicates that the
15232 object is optimized out. The return value is 0 for that case.
15233 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15234 callers will only want a very basic result and this can become a
15235 complaint.
15236
15237 Note that stack[0] is unused except as a default error return. */
15238
15239 static CORE_ADDR
15240 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15241 {
15242 struct objfile *objfile = cu->objfile;
15243 int i;
15244 int size = blk->size;
15245 gdb_byte *data = blk->data;
15246 CORE_ADDR stack[64];
15247 int stacki;
15248 unsigned int bytes_read, unsnd;
15249 gdb_byte op;
15250
15251 i = 0;
15252 stacki = 0;
15253 stack[stacki] = 0;
15254 stack[++stacki] = 0;
15255
15256 while (i < size)
15257 {
15258 op = data[i++];
15259 switch (op)
15260 {
15261 case DW_OP_lit0:
15262 case DW_OP_lit1:
15263 case DW_OP_lit2:
15264 case DW_OP_lit3:
15265 case DW_OP_lit4:
15266 case DW_OP_lit5:
15267 case DW_OP_lit6:
15268 case DW_OP_lit7:
15269 case DW_OP_lit8:
15270 case DW_OP_lit9:
15271 case DW_OP_lit10:
15272 case DW_OP_lit11:
15273 case DW_OP_lit12:
15274 case DW_OP_lit13:
15275 case DW_OP_lit14:
15276 case DW_OP_lit15:
15277 case DW_OP_lit16:
15278 case DW_OP_lit17:
15279 case DW_OP_lit18:
15280 case DW_OP_lit19:
15281 case DW_OP_lit20:
15282 case DW_OP_lit21:
15283 case DW_OP_lit22:
15284 case DW_OP_lit23:
15285 case DW_OP_lit24:
15286 case DW_OP_lit25:
15287 case DW_OP_lit26:
15288 case DW_OP_lit27:
15289 case DW_OP_lit28:
15290 case DW_OP_lit29:
15291 case DW_OP_lit30:
15292 case DW_OP_lit31:
15293 stack[++stacki] = op - DW_OP_lit0;
15294 break;
15295
15296 case DW_OP_reg0:
15297 case DW_OP_reg1:
15298 case DW_OP_reg2:
15299 case DW_OP_reg3:
15300 case DW_OP_reg4:
15301 case DW_OP_reg5:
15302 case DW_OP_reg6:
15303 case DW_OP_reg7:
15304 case DW_OP_reg8:
15305 case DW_OP_reg9:
15306 case DW_OP_reg10:
15307 case DW_OP_reg11:
15308 case DW_OP_reg12:
15309 case DW_OP_reg13:
15310 case DW_OP_reg14:
15311 case DW_OP_reg15:
15312 case DW_OP_reg16:
15313 case DW_OP_reg17:
15314 case DW_OP_reg18:
15315 case DW_OP_reg19:
15316 case DW_OP_reg20:
15317 case DW_OP_reg21:
15318 case DW_OP_reg22:
15319 case DW_OP_reg23:
15320 case DW_OP_reg24:
15321 case DW_OP_reg25:
15322 case DW_OP_reg26:
15323 case DW_OP_reg27:
15324 case DW_OP_reg28:
15325 case DW_OP_reg29:
15326 case DW_OP_reg30:
15327 case DW_OP_reg31:
15328 stack[++stacki] = op - DW_OP_reg0;
15329 if (i < size)
15330 dwarf2_complex_location_expr_complaint ();
15331 break;
15332
15333 case DW_OP_regx:
15334 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15335 i += bytes_read;
15336 stack[++stacki] = unsnd;
15337 if (i < size)
15338 dwarf2_complex_location_expr_complaint ();
15339 break;
15340
15341 case DW_OP_addr:
15342 stack[++stacki] = read_address (objfile->obfd, &data[i],
15343 cu, &bytes_read);
15344 i += bytes_read;
15345 break;
15346
15347 case DW_OP_const1u:
15348 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15349 i += 1;
15350 break;
15351
15352 case DW_OP_const1s:
15353 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15354 i += 1;
15355 break;
15356
15357 case DW_OP_const2u:
15358 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15359 i += 2;
15360 break;
15361
15362 case DW_OP_const2s:
15363 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15364 i += 2;
15365 break;
15366
15367 case DW_OP_const4u:
15368 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15369 i += 4;
15370 break;
15371
15372 case DW_OP_const4s:
15373 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15374 i += 4;
15375 break;
15376
15377 case DW_OP_const8u:
15378 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15379 i += 8;
15380 break;
15381
15382 case DW_OP_constu:
15383 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15384 &bytes_read);
15385 i += bytes_read;
15386 break;
15387
15388 case DW_OP_consts:
15389 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15390 i += bytes_read;
15391 break;
15392
15393 case DW_OP_dup:
15394 stack[stacki + 1] = stack[stacki];
15395 stacki++;
15396 break;
15397
15398 case DW_OP_plus:
15399 stack[stacki - 1] += stack[stacki];
15400 stacki--;
15401 break;
15402
15403 case DW_OP_plus_uconst:
15404 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15405 &bytes_read);
15406 i += bytes_read;
15407 break;
15408
15409 case DW_OP_minus:
15410 stack[stacki - 1] -= stack[stacki];
15411 stacki--;
15412 break;
15413
15414 case DW_OP_deref:
15415 /* If we're not the last op, then we definitely can't encode
15416 this using GDB's address_class enum. This is valid for partial
15417 global symbols, although the variable's address will be bogus
15418 in the psymtab. */
15419 if (i < size)
15420 dwarf2_complex_location_expr_complaint ();
15421 break;
15422
15423 case DW_OP_GNU_push_tls_address:
15424 /* The top of the stack has the offset from the beginning
15425 of the thread control block at which the variable is located. */
15426 /* Nothing should follow this operator, so the top of stack would
15427 be returned. */
15428 /* This is valid for partial global symbols, but the variable's
15429 address will be bogus in the psymtab. Make it always at least
15430 non-zero to not look as a variable garbage collected by linker
15431 which have DW_OP_addr 0. */
15432 if (i < size)
15433 dwarf2_complex_location_expr_complaint ();
15434 stack[stacki]++;
15435 break;
15436
15437 case DW_OP_GNU_uninit:
15438 break;
15439
15440 case DW_OP_GNU_addr_index:
15441 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15442 &bytes_read);
15443 i += bytes_read;
15444 break;
15445
15446 default:
15447 {
15448 const char *name = get_DW_OP_name (op);
15449
15450 if (name)
15451 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15452 name);
15453 else
15454 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15455 op);
15456 }
15457
15458 return (stack[stacki]);
15459 }
15460
15461 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15462 outside of the allocated space. Also enforce minimum>0. */
15463 if (stacki >= ARRAY_SIZE (stack) - 1)
15464 {
15465 complaint (&symfile_complaints,
15466 _("location description stack overflow"));
15467 return 0;
15468 }
15469
15470 if (stacki <= 0)
15471 {
15472 complaint (&symfile_complaints,
15473 _("location description stack underflow"));
15474 return 0;
15475 }
15476 }
15477 return (stack[stacki]);
15478 }
15479
15480 /* memory allocation interface */
15481
15482 static struct dwarf_block *
15483 dwarf_alloc_block (struct dwarf2_cu *cu)
15484 {
15485 struct dwarf_block *blk;
15486
15487 blk = (struct dwarf_block *)
15488 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15489 return (blk);
15490 }
15491
15492 static struct abbrev_info *
15493 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15494 {
15495 struct abbrev_info *abbrev;
15496
15497 abbrev = (struct abbrev_info *)
15498 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15499 memset (abbrev, 0, sizeof (struct abbrev_info));
15500 return (abbrev);
15501 }
15502
15503 static struct die_info *
15504 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15505 {
15506 struct die_info *die;
15507 size_t size = sizeof (struct die_info);
15508
15509 if (num_attrs > 1)
15510 size += (num_attrs - 1) * sizeof (struct attribute);
15511
15512 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15513 memset (die, 0, sizeof (struct die_info));
15514 return (die);
15515 }
15516
15517 \f
15518 /* Macro support. */
15519
15520 /* Return the full name of file number I in *LH's file name table.
15521 Use COMP_DIR as the name of the current directory of the
15522 compilation. The result is allocated using xmalloc; the caller is
15523 responsible for freeing it. */
15524 static char *
15525 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15526 {
15527 /* Is the file number a valid index into the line header's file name
15528 table? Remember that file numbers start with one, not zero. */
15529 if (1 <= file && file <= lh->num_file_names)
15530 {
15531 struct file_entry *fe = &lh->file_names[file - 1];
15532
15533 if (IS_ABSOLUTE_PATH (fe->name))
15534 return xstrdup (fe->name);
15535 else
15536 {
15537 const char *dir;
15538 int dir_len;
15539 char *full_name;
15540
15541 if (fe->dir_index)
15542 dir = lh->include_dirs[fe->dir_index - 1];
15543 else
15544 dir = comp_dir;
15545
15546 if (dir)
15547 {
15548 dir_len = strlen (dir);
15549 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15550 strcpy (full_name, dir);
15551 full_name[dir_len] = '/';
15552 strcpy (full_name + dir_len + 1, fe->name);
15553 return full_name;
15554 }
15555 else
15556 return xstrdup (fe->name);
15557 }
15558 }
15559 else
15560 {
15561 /* The compiler produced a bogus file number. We can at least
15562 record the macro definitions made in the file, even if we
15563 won't be able to find the file by name. */
15564 char fake_name[80];
15565
15566 sprintf (fake_name, "<bad macro file number %d>", file);
15567
15568 complaint (&symfile_complaints,
15569 _("bad file number in macro information (%d)"),
15570 file);
15571
15572 return xstrdup (fake_name);
15573 }
15574 }
15575
15576
15577 static struct macro_source_file *
15578 macro_start_file (int file, int line,
15579 struct macro_source_file *current_file,
15580 const char *comp_dir,
15581 struct line_header *lh, struct objfile *objfile)
15582 {
15583 /* The full name of this source file. */
15584 char *full_name = file_full_name (file, lh, comp_dir);
15585
15586 /* We don't create a macro table for this compilation unit
15587 at all until we actually get a filename. */
15588 if (! pending_macros)
15589 pending_macros = new_macro_table (&objfile->objfile_obstack,
15590 objfile->macro_cache);
15591
15592 if (! current_file)
15593 {
15594 /* If we have no current file, then this must be the start_file
15595 directive for the compilation unit's main source file. */
15596 current_file = macro_set_main (pending_macros, full_name);
15597 macro_define_special (pending_macros);
15598 }
15599 else
15600 current_file = macro_include (current_file, line, full_name);
15601
15602 xfree (full_name);
15603
15604 return current_file;
15605 }
15606
15607
15608 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15609 followed by a null byte. */
15610 static char *
15611 copy_string (const char *buf, int len)
15612 {
15613 char *s = xmalloc (len + 1);
15614
15615 memcpy (s, buf, len);
15616 s[len] = '\0';
15617 return s;
15618 }
15619
15620
15621 static const char *
15622 consume_improper_spaces (const char *p, const char *body)
15623 {
15624 if (*p == ' ')
15625 {
15626 complaint (&symfile_complaints,
15627 _("macro definition contains spaces "
15628 "in formal argument list:\n`%s'"),
15629 body);
15630
15631 while (*p == ' ')
15632 p++;
15633 }
15634
15635 return p;
15636 }
15637
15638
15639 static void
15640 parse_macro_definition (struct macro_source_file *file, int line,
15641 const char *body)
15642 {
15643 const char *p;
15644
15645 /* The body string takes one of two forms. For object-like macro
15646 definitions, it should be:
15647
15648 <macro name> " " <definition>
15649
15650 For function-like macro definitions, it should be:
15651
15652 <macro name> "() " <definition>
15653 or
15654 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15655
15656 Spaces may appear only where explicitly indicated, and in the
15657 <definition>.
15658
15659 The Dwarf 2 spec says that an object-like macro's name is always
15660 followed by a space, but versions of GCC around March 2002 omit
15661 the space when the macro's definition is the empty string.
15662
15663 The Dwarf 2 spec says that there should be no spaces between the
15664 formal arguments in a function-like macro's formal argument list,
15665 but versions of GCC around March 2002 include spaces after the
15666 commas. */
15667
15668
15669 /* Find the extent of the macro name. The macro name is terminated
15670 by either a space or null character (for an object-like macro) or
15671 an opening paren (for a function-like macro). */
15672 for (p = body; *p; p++)
15673 if (*p == ' ' || *p == '(')
15674 break;
15675
15676 if (*p == ' ' || *p == '\0')
15677 {
15678 /* It's an object-like macro. */
15679 int name_len = p - body;
15680 char *name = copy_string (body, name_len);
15681 const char *replacement;
15682
15683 if (*p == ' ')
15684 replacement = body + name_len + 1;
15685 else
15686 {
15687 dwarf2_macro_malformed_definition_complaint (body);
15688 replacement = body + name_len;
15689 }
15690
15691 macro_define_object (file, line, name, replacement);
15692
15693 xfree (name);
15694 }
15695 else if (*p == '(')
15696 {
15697 /* It's a function-like macro. */
15698 char *name = copy_string (body, p - body);
15699 int argc = 0;
15700 int argv_size = 1;
15701 char **argv = xmalloc (argv_size * sizeof (*argv));
15702
15703 p++;
15704
15705 p = consume_improper_spaces (p, body);
15706
15707 /* Parse the formal argument list. */
15708 while (*p && *p != ')')
15709 {
15710 /* Find the extent of the current argument name. */
15711 const char *arg_start = p;
15712
15713 while (*p && *p != ',' && *p != ')' && *p != ' ')
15714 p++;
15715
15716 if (! *p || p == arg_start)
15717 dwarf2_macro_malformed_definition_complaint (body);
15718 else
15719 {
15720 /* Make sure argv has room for the new argument. */
15721 if (argc >= argv_size)
15722 {
15723 argv_size *= 2;
15724 argv = xrealloc (argv, argv_size * sizeof (*argv));
15725 }
15726
15727 argv[argc++] = copy_string (arg_start, p - arg_start);
15728 }
15729
15730 p = consume_improper_spaces (p, body);
15731
15732 /* Consume the comma, if present. */
15733 if (*p == ',')
15734 {
15735 p++;
15736
15737 p = consume_improper_spaces (p, body);
15738 }
15739 }
15740
15741 if (*p == ')')
15742 {
15743 p++;
15744
15745 if (*p == ' ')
15746 /* Perfectly formed definition, no complaints. */
15747 macro_define_function (file, line, name,
15748 argc, (const char **) argv,
15749 p + 1);
15750 else if (*p == '\0')
15751 {
15752 /* Complain, but do define it. */
15753 dwarf2_macro_malformed_definition_complaint (body);
15754 macro_define_function (file, line, name,
15755 argc, (const char **) argv,
15756 p);
15757 }
15758 else
15759 /* Just complain. */
15760 dwarf2_macro_malformed_definition_complaint (body);
15761 }
15762 else
15763 /* Just complain. */
15764 dwarf2_macro_malformed_definition_complaint (body);
15765
15766 xfree (name);
15767 {
15768 int i;
15769
15770 for (i = 0; i < argc; i++)
15771 xfree (argv[i]);
15772 }
15773 xfree (argv);
15774 }
15775 else
15776 dwarf2_macro_malformed_definition_complaint (body);
15777 }
15778
15779 /* Skip some bytes from BYTES according to the form given in FORM.
15780 Returns the new pointer. */
15781
15782 static gdb_byte *
15783 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15784 enum dwarf_form form,
15785 unsigned int offset_size,
15786 struct dwarf2_section_info *section)
15787 {
15788 unsigned int bytes_read;
15789
15790 switch (form)
15791 {
15792 case DW_FORM_data1:
15793 case DW_FORM_flag:
15794 ++bytes;
15795 break;
15796
15797 case DW_FORM_data2:
15798 bytes += 2;
15799 break;
15800
15801 case DW_FORM_data4:
15802 bytes += 4;
15803 break;
15804
15805 case DW_FORM_data8:
15806 bytes += 8;
15807 break;
15808
15809 case DW_FORM_string:
15810 read_direct_string (abfd, bytes, &bytes_read);
15811 bytes += bytes_read;
15812 break;
15813
15814 case DW_FORM_sec_offset:
15815 case DW_FORM_strp:
15816 bytes += offset_size;
15817 break;
15818
15819 case DW_FORM_block:
15820 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15821 bytes += bytes_read;
15822 break;
15823
15824 case DW_FORM_block1:
15825 bytes += 1 + read_1_byte (abfd, bytes);
15826 break;
15827 case DW_FORM_block2:
15828 bytes += 2 + read_2_bytes (abfd, bytes);
15829 break;
15830 case DW_FORM_block4:
15831 bytes += 4 + read_4_bytes (abfd, bytes);
15832 break;
15833
15834 case DW_FORM_sdata:
15835 case DW_FORM_udata:
15836 case DW_FORM_GNU_addr_index:
15837 case DW_FORM_GNU_str_index:
15838 bytes = skip_leb128 (abfd, bytes);
15839 break;
15840
15841 default:
15842 {
15843 complain:
15844 complaint (&symfile_complaints,
15845 _("invalid form 0x%x in `%s'"),
15846 form,
15847 section->asection->name);
15848 return NULL;
15849 }
15850 }
15851
15852 return bytes;
15853 }
15854
15855 /* A helper for dwarf_decode_macros that handles skipping an unknown
15856 opcode. Returns an updated pointer to the macro data buffer; or,
15857 on error, issues a complaint and returns NULL. */
15858
15859 static gdb_byte *
15860 skip_unknown_opcode (unsigned int opcode,
15861 gdb_byte **opcode_definitions,
15862 gdb_byte *mac_ptr,
15863 bfd *abfd,
15864 unsigned int offset_size,
15865 struct dwarf2_section_info *section)
15866 {
15867 unsigned int bytes_read, i;
15868 unsigned long arg;
15869 gdb_byte *defn;
15870
15871 if (opcode_definitions[opcode] == NULL)
15872 {
15873 complaint (&symfile_complaints,
15874 _("unrecognized DW_MACFINO opcode 0x%x"),
15875 opcode);
15876 return NULL;
15877 }
15878
15879 defn = opcode_definitions[opcode];
15880 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15881 defn += bytes_read;
15882
15883 for (i = 0; i < arg; ++i)
15884 {
15885 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15886 if (mac_ptr == NULL)
15887 {
15888 /* skip_form_bytes already issued the complaint. */
15889 return NULL;
15890 }
15891 }
15892
15893 return mac_ptr;
15894 }
15895
15896 /* A helper function which parses the header of a macro section.
15897 If the macro section is the extended (for now called "GNU") type,
15898 then this updates *OFFSET_SIZE. Returns a pointer to just after
15899 the header, or issues a complaint and returns NULL on error. */
15900
15901 static gdb_byte *
15902 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15903 bfd *abfd,
15904 gdb_byte *mac_ptr,
15905 unsigned int *offset_size,
15906 int section_is_gnu)
15907 {
15908 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15909
15910 if (section_is_gnu)
15911 {
15912 unsigned int version, flags;
15913
15914 version = read_2_bytes (abfd, mac_ptr);
15915 if (version != 4)
15916 {
15917 complaint (&symfile_complaints,
15918 _("unrecognized version `%d' in .debug_macro section"),
15919 version);
15920 return NULL;
15921 }
15922 mac_ptr += 2;
15923
15924 flags = read_1_byte (abfd, mac_ptr);
15925 ++mac_ptr;
15926 *offset_size = (flags & 1) ? 8 : 4;
15927
15928 if ((flags & 2) != 0)
15929 /* We don't need the line table offset. */
15930 mac_ptr += *offset_size;
15931
15932 /* Vendor opcode descriptions. */
15933 if ((flags & 4) != 0)
15934 {
15935 unsigned int i, count;
15936
15937 count = read_1_byte (abfd, mac_ptr);
15938 ++mac_ptr;
15939 for (i = 0; i < count; ++i)
15940 {
15941 unsigned int opcode, bytes_read;
15942 unsigned long arg;
15943
15944 opcode = read_1_byte (abfd, mac_ptr);
15945 ++mac_ptr;
15946 opcode_definitions[opcode] = mac_ptr;
15947 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15948 mac_ptr += bytes_read;
15949 mac_ptr += arg;
15950 }
15951 }
15952 }
15953
15954 return mac_ptr;
15955 }
15956
15957 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15958 including DW_MACRO_GNU_transparent_include. */
15959
15960 static void
15961 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15962 struct macro_source_file *current_file,
15963 struct line_header *lh, char *comp_dir,
15964 struct dwarf2_section_info *section,
15965 int section_is_gnu,
15966 unsigned int offset_size,
15967 struct objfile *objfile,
15968 htab_t include_hash)
15969 {
15970 enum dwarf_macro_record_type macinfo_type;
15971 int at_commandline;
15972 gdb_byte *opcode_definitions[256];
15973
15974 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15975 &offset_size, section_is_gnu);
15976 if (mac_ptr == NULL)
15977 {
15978 /* We already issued a complaint. */
15979 return;
15980 }
15981
15982 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15983 GDB is still reading the definitions from command line. First
15984 DW_MACINFO_start_file will need to be ignored as it was already executed
15985 to create CURRENT_FILE for the main source holding also the command line
15986 definitions. On first met DW_MACINFO_start_file this flag is reset to
15987 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15988
15989 at_commandline = 1;
15990
15991 do
15992 {
15993 /* Do we at least have room for a macinfo type byte? */
15994 if (mac_ptr >= mac_end)
15995 {
15996 dwarf2_macros_too_long_complaint (section);
15997 break;
15998 }
15999
16000 macinfo_type = read_1_byte (abfd, mac_ptr);
16001 mac_ptr++;
16002
16003 /* Note that we rely on the fact that the corresponding GNU and
16004 DWARF constants are the same. */
16005 switch (macinfo_type)
16006 {
16007 /* A zero macinfo type indicates the end of the macro
16008 information. */
16009 case 0:
16010 break;
16011
16012 case DW_MACRO_GNU_define:
16013 case DW_MACRO_GNU_undef:
16014 case DW_MACRO_GNU_define_indirect:
16015 case DW_MACRO_GNU_undef_indirect:
16016 {
16017 unsigned int bytes_read;
16018 int line;
16019 char *body;
16020 int is_define;
16021
16022 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16023 mac_ptr += bytes_read;
16024
16025 if (macinfo_type == DW_MACRO_GNU_define
16026 || macinfo_type == DW_MACRO_GNU_undef)
16027 {
16028 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16029 mac_ptr += bytes_read;
16030 }
16031 else
16032 {
16033 LONGEST str_offset;
16034
16035 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16036 mac_ptr += offset_size;
16037
16038 body = read_indirect_string_at_offset (abfd, str_offset);
16039 }
16040
16041 is_define = (macinfo_type == DW_MACRO_GNU_define
16042 || macinfo_type == DW_MACRO_GNU_define_indirect);
16043 if (! current_file)
16044 {
16045 /* DWARF violation as no main source is present. */
16046 complaint (&symfile_complaints,
16047 _("debug info with no main source gives macro %s "
16048 "on line %d: %s"),
16049 is_define ? _("definition") : _("undefinition"),
16050 line, body);
16051 break;
16052 }
16053 if ((line == 0 && !at_commandline)
16054 || (line != 0 && at_commandline))
16055 complaint (&symfile_complaints,
16056 _("debug info gives %s macro %s with %s line %d: %s"),
16057 at_commandline ? _("command-line") : _("in-file"),
16058 is_define ? _("definition") : _("undefinition"),
16059 line == 0 ? _("zero") : _("non-zero"), line, body);
16060
16061 if (is_define)
16062 parse_macro_definition (current_file, line, body);
16063 else
16064 {
16065 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16066 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16067 macro_undef (current_file, line, body);
16068 }
16069 }
16070 break;
16071
16072 case DW_MACRO_GNU_start_file:
16073 {
16074 unsigned int bytes_read;
16075 int line, file;
16076
16077 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16078 mac_ptr += bytes_read;
16079 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16080 mac_ptr += bytes_read;
16081
16082 if ((line == 0 && !at_commandline)
16083 || (line != 0 && at_commandline))
16084 complaint (&symfile_complaints,
16085 _("debug info gives source %d included "
16086 "from %s at %s line %d"),
16087 file, at_commandline ? _("command-line") : _("file"),
16088 line == 0 ? _("zero") : _("non-zero"), line);
16089
16090 if (at_commandline)
16091 {
16092 /* This DW_MACRO_GNU_start_file was executed in the
16093 pass one. */
16094 at_commandline = 0;
16095 }
16096 else
16097 current_file = macro_start_file (file, line,
16098 current_file, comp_dir,
16099 lh, objfile);
16100 }
16101 break;
16102
16103 case DW_MACRO_GNU_end_file:
16104 if (! current_file)
16105 complaint (&symfile_complaints,
16106 _("macro debug info has an unmatched "
16107 "`close_file' directive"));
16108 else
16109 {
16110 current_file = current_file->included_by;
16111 if (! current_file)
16112 {
16113 enum dwarf_macro_record_type next_type;
16114
16115 /* GCC circa March 2002 doesn't produce the zero
16116 type byte marking the end of the compilation
16117 unit. Complain if it's not there, but exit no
16118 matter what. */
16119
16120 /* Do we at least have room for a macinfo type byte? */
16121 if (mac_ptr >= mac_end)
16122 {
16123 dwarf2_macros_too_long_complaint (section);
16124 return;
16125 }
16126
16127 /* We don't increment mac_ptr here, so this is just
16128 a look-ahead. */
16129 next_type = read_1_byte (abfd, mac_ptr);
16130 if (next_type != 0)
16131 complaint (&symfile_complaints,
16132 _("no terminating 0-type entry for "
16133 "macros in `.debug_macinfo' section"));
16134
16135 return;
16136 }
16137 }
16138 break;
16139
16140 case DW_MACRO_GNU_transparent_include:
16141 {
16142 LONGEST offset;
16143 void **slot;
16144
16145 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16146 mac_ptr += offset_size;
16147
16148 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16149 if (*slot != NULL)
16150 {
16151 /* This has actually happened; see
16152 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16153 complaint (&symfile_complaints,
16154 _("recursive DW_MACRO_GNU_transparent_include in "
16155 ".debug_macro section"));
16156 }
16157 else
16158 {
16159 *slot = mac_ptr;
16160
16161 dwarf_decode_macro_bytes (abfd,
16162 section->buffer + offset,
16163 mac_end, current_file,
16164 lh, comp_dir,
16165 section, section_is_gnu,
16166 offset_size, objfile, include_hash);
16167
16168 htab_remove_elt (include_hash, mac_ptr);
16169 }
16170 }
16171 break;
16172
16173 case DW_MACINFO_vendor_ext:
16174 if (!section_is_gnu)
16175 {
16176 unsigned int bytes_read;
16177 int constant;
16178
16179 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16180 mac_ptr += bytes_read;
16181 read_direct_string (abfd, mac_ptr, &bytes_read);
16182 mac_ptr += bytes_read;
16183
16184 /* We don't recognize any vendor extensions. */
16185 break;
16186 }
16187 /* FALLTHROUGH */
16188
16189 default:
16190 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16191 mac_ptr, abfd, offset_size,
16192 section);
16193 if (mac_ptr == NULL)
16194 return;
16195 break;
16196 }
16197 } while (macinfo_type != 0);
16198 }
16199
16200 static void
16201 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16202 char *comp_dir, bfd *abfd,
16203 struct dwarf2_cu *cu,
16204 struct dwarf2_section_info *section,
16205 int section_is_gnu, const char *section_name)
16206 {
16207 struct objfile *objfile = dwarf2_per_objfile->objfile;
16208 gdb_byte *mac_ptr, *mac_end;
16209 struct macro_source_file *current_file = 0;
16210 enum dwarf_macro_record_type macinfo_type;
16211 unsigned int offset_size = cu->header.offset_size;
16212 gdb_byte *opcode_definitions[256];
16213 struct cleanup *cleanup;
16214 htab_t include_hash;
16215 void **slot;
16216
16217 dwarf2_read_section (objfile, section);
16218 if (section->buffer == NULL)
16219 {
16220 complaint (&symfile_complaints, _("missing %s section"), section_name);
16221 return;
16222 }
16223
16224 /* First pass: Find the name of the base filename.
16225 This filename is needed in order to process all macros whose definition
16226 (or undefinition) comes from the command line. These macros are defined
16227 before the first DW_MACINFO_start_file entry, and yet still need to be
16228 associated to the base file.
16229
16230 To determine the base file name, we scan the macro definitions until we
16231 reach the first DW_MACINFO_start_file entry. We then initialize
16232 CURRENT_FILE accordingly so that any macro definition found before the
16233 first DW_MACINFO_start_file can still be associated to the base file. */
16234
16235 mac_ptr = section->buffer + offset;
16236 mac_end = section->buffer + section->size;
16237
16238 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16239 &offset_size, section_is_gnu);
16240 if (mac_ptr == NULL)
16241 {
16242 /* We already issued a complaint. */
16243 return;
16244 }
16245
16246 do
16247 {
16248 /* Do we at least have room for a macinfo type byte? */
16249 if (mac_ptr >= mac_end)
16250 {
16251 /* Complaint is printed during the second pass as GDB will probably
16252 stop the first pass earlier upon finding
16253 DW_MACINFO_start_file. */
16254 break;
16255 }
16256
16257 macinfo_type = read_1_byte (abfd, mac_ptr);
16258 mac_ptr++;
16259
16260 /* Note that we rely on the fact that the corresponding GNU and
16261 DWARF constants are the same. */
16262 switch (macinfo_type)
16263 {
16264 /* A zero macinfo type indicates the end of the macro
16265 information. */
16266 case 0:
16267 break;
16268
16269 case DW_MACRO_GNU_define:
16270 case DW_MACRO_GNU_undef:
16271 /* Only skip the data by MAC_PTR. */
16272 {
16273 unsigned int bytes_read;
16274
16275 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16276 mac_ptr += bytes_read;
16277 read_direct_string (abfd, mac_ptr, &bytes_read);
16278 mac_ptr += bytes_read;
16279 }
16280 break;
16281
16282 case DW_MACRO_GNU_start_file:
16283 {
16284 unsigned int bytes_read;
16285 int line, file;
16286
16287 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16288 mac_ptr += bytes_read;
16289 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16290 mac_ptr += bytes_read;
16291
16292 current_file = macro_start_file (file, line, current_file,
16293 comp_dir, lh, objfile);
16294 }
16295 break;
16296
16297 case DW_MACRO_GNU_end_file:
16298 /* No data to skip by MAC_PTR. */
16299 break;
16300
16301 case DW_MACRO_GNU_define_indirect:
16302 case DW_MACRO_GNU_undef_indirect:
16303 {
16304 unsigned int bytes_read;
16305
16306 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16307 mac_ptr += bytes_read;
16308 mac_ptr += offset_size;
16309 }
16310 break;
16311
16312 case DW_MACRO_GNU_transparent_include:
16313 /* Note that, according to the spec, a transparent include
16314 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16315 skip this opcode. */
16316 mac_ptr += offset_size;
16317 break;
16318
16319 case DW_MACINFO_vendor_ext:
16320 /* Only skip the data by MAC_PTR. */
16321 if (!section_is_gnu)
16322 {
16323 unsigned int bytes_read;
16324
16325 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16326 mac_ptr += bytes_read;
16327 read_direct_string (abfd, mac_ptr, &bytes_read);
16328 mac_ptr += bytes_read;
16329 }
16330 /* FALLTHROUGH */
16331
16332 default:
16333 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16334 mac_ptr, abfd, offset_size,
16335 section);
16336 if (mac_ptr == NULL)
16337 return;
16338 break;
16339 }
16340 } while (macinfo_type != 0 && current_file == NULL);
16341
16342 /* Second pass: Process all entries.
16343
16344 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16345 command-line macro definitions/undefinitions. This flag is unset when we
16346 reach the first DW_MACINFO_start_file entry. */
16347
16348 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16349 NULL, xcalloc, xfree);
16350 cleanup = make_cleanup_htab_delete (include_hash);
16351 mac_ptr = section->buffer + offset;
16352 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16353 *slot = mac_ptr;
16354 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16355 current_file, lh, comp_dir, section, section_is_gnu,
16356 offset_size, objfile, include_hash);
16357 do_cleanups (cleanup);
16358 }
16359
16360 /* Check if the attribute's form is a DW_FORM_block*
16361 if so return true else false. */
16362
16363 static int
16364 attr_form_is_block (struct attribute *attr)
16365 {
16366 return (attr == NULL ? 0 :
16367 attr->form == DW_FORM_block1
16368 || attr->form == DW_FORM_block2
16369 || attr->form == DW_FORM_block4
16370 || attr->form == DW_FORM_block
16371 || attr->form == DW_FORM_exprloc);
16372 }
16373
16374 /* Return non-zero if ATTR's value is a section offset --- classes
16375 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16376 You may use DW_UNSND (attr) to retrieve such offsets.
16377
16378 Section 7.5.4, "Attribute Encodings", explains that no attribute
16379 may have a value that belongs to more than one of these classes; it
16380 would be ambiguous if we did, because we use the same forms for all
16381 of them. */
16382
16383 static int
16384 attr_form_is_section_offset (struct attribute *attr)
16385 {
16386 return (attr->form == DW_FORM_data4
16387 || attr->form == DW_FORM_data8
16388 || attr->form == DW_FORM_sec_offset);
16389 }
16390
16391 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16392 zero otherwise. When this function returns true, you can apply
16393 dwarf2_get_attr_constant_value to it.
16394
16395 However, note that for some attributes you must check
16396 attr_form_is_section_offset before using this test. DW_FORM_data4
16397 and DW_FORM_data8 are members of both the constant class, and of
16398 the classes that contain offsets into other debug sections
16399 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16400 that, if an attribute's can be either a constant or one of the
16401 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16402 taken as section offsets, not constants. */
16403
16404 static int
16405 attr_form_is_constant (struct attribute *attr)
16406 {
16407 switch (attr->form)
16408 {
16409 case DW_FORM_sdata:
16410 case DW_FORM_udata:
16411 case DW_FORM_data1:
16412 case DW_FORM_data2:
16413 case DW_FORM_data4:
16414 case DW_FORM_data8:
16415 return 1;
16416 default:
16417 return 0;
16418 }
16419 }
16420
16421 /* Return the .debug_loc section to use for CU.
16422 For DWO files use .debug_loc.dwo. */
16423
16424 static struct dwarf2_section_info *
16425 cu_debug_loc_section (struct dwarf2_cu *cu)
16426 {
16427 if (cu->dwo_unit)
16428 return &cu->dwo_unit->dwo_file->sections.loc;
16429 return &dwarf2_per_objfile->loc;
16430 }
16431
16432 /* A helper function that fills in a dwarf2_loclist_baton. */
16433
16434 static void
16435 fill_in_loclist_baton (struct dwarf2_cu *cu,
16436 struct dwarf2_loclist_baton *baton,
16437 struct attribute *attr)
16438 {
16439 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16440
16441 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16442
16443 baton->per_cu = cu->per_cu;
16444 gdb_assert (baton->per_cu);
16445 /* We don't know how long the location list is, but make sure we
16446 don't run off the edge of the section. */
16447 baton->size = section->size - DW_UNSND (attr);
16448 baton->data = section->buffer + DW_UNSND (attr);
16449 baton->base_address = cu->base_address;
16450 }
16451
16452 static void
16453 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16454 struct dwarf2_cu *cu)
16455 {
16456 struct objfile *objfile = dwarf2_per_objfile->objfile;
16457 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16458
16459 if (attr_form_is_section_offset (attr)
16460 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16461 the section. If so, fall through to the complaint in the
16462 other branch. */
16463 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16464 {
16465 struct dwarf2_loclist_baton *baton;
16466
16467 baton = obstack_alloc (&objfile->objfile_obstack,
16468 sizeof (struct dwarf2_loclist_baton));
16469
16470 fill_in_loclist_baton (cu, baton, attr);
16471
16472 if (cu->base_known == 0)
16473 complaint (&symfile_complaints,
16474 _("Location list used without "
16475 "specifying the CU base address."));
16476
16477 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16478 SYMBOL_LOCATION_BATON (sym) = baton;
16479 }
16480 else
16481 {
16482 struct dwarf2_locexpr_baton *baton;
16483
16484 baton = obstack_alloc (&objfile->objfile_obstack,
16485 sizeof (struct dwarf2_locexpr_baton));
16486 baton->per_cu = cu->per_cu;
16487 gdb_assert (baton->per_cu);
16488
16489 if (attr_form_is_block (attr))
16490 {
16491 /* Note that we're just copying the block's data pointer
16492 here, not the actual data. We're still pointing into the
16493 info_buffer for SYM's objfile; right now we never release
16494 that buffer, but when we do clean up properly this may
16495 need to change. */
16496 baton->size = DW_BLOCK (attr)->size;
16497 baton->data = DW_BLOCK (attr)->data;
16498 }
16499 else
16500 {
16501 dwarf2_invalid_attrib_class_complaint ("location description",
16502 SYMBOL_NATURAL_NAME (sym));
16503 baton->size = 0;
16504 }
16505
16506 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16507 SYMBOL_LOCATION_BATON (sym) = baton;
16508 }
16509 }
16510
16511 /* Return the OBJFILE associated with the compilation unit CU. If CU
16512 came from a separate debuginfo file, then the master objfile is
16513 returned. */
16514
16515 struct objfile *
16516 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16517 {
16518 struct objfile *objfile = per_cu->objfile;
16519
16520 /* Return the master objfile, so that we can report and look up the
16521 correct file containing this variable. */
16522 if (objfile->separate_debug_objfile_backlink)
16523 objfile = objfile->separate_debug_objfile_backlink;
16524
16525 return objfile;
16526 }
16527
16528 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16529 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16530 CU_HEADERP first. */
16531
16532 static const struct comp_unit_head *
16533 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16534 struct dwarf2_per_cu_data *per_cu)
16535 {
16536 struct objfile *objfile;
16537 struct dwarf2_per_objfile *per_objfile;
16538 gdb_byte *info_ptr;
16539
16540 if (per_cu->cu)
16541 return &per_cu->cu->header;
16542
16543 objfile = per_cu->objfile;
16544 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16545 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16546
16547 memset (cu_headerp, 0, sizeof (*cu_headerp));
16548 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16549
16550 return cu_headerp;
16551 }
16552
16553 /* Return the address size given in the compilation unit header for CU. */
16554
16555 int
16556 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16557 {
16558 struct comp_unit_head cu_header_local;
16559 const struct comp_unit_head *cu_headerp;
16560
16561 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16562
16563 return cu_headerp->addr_size;
16564 }
16565
16566 /* Return the offset size given in the compilation unit header for CU. */
16567
16568 int
16569 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16570 {
16571 struct comp_unit_head cu_header_local;
16572 const struct comp_unit_head *cu_headerp;
16573
16574 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16575
16576 return cu_headerp->offset_size;
16577 }
16578
16579 /* See its dwarf2loc.h declaration. */
16580
16581 int
16582 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16583 {
16584 struct comp_unit_head cu_header_local;
16585 const struct comp_unit_head *cu_headerp;
16586
16587 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16588
16589 if (cu_headerp->version == 2)
16590 return cu_headerp->addr_size;
16591 else
16592 return cu_headerp->offset_size;
16593 }
16594
16595 /* Return the text offset of the CU. The returned offset comes from
16596 this CU's objfile. If this objfile came from a separate debuginfo
16597 file, then the offset may be different from the corresponding
16598 offset in the parent objfile. */
16599
16600 CORE_ADDR
16601 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16602 {
16603 struct objfile *objfile = per_cu->objfile;
16604
16605 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16606 }
16607
16608 /* Locate the .debug_info compilation unit from CU's objfile which contains
16609 the DIE at OFFSET. Raises an error on failure. */
16610
16611 static struct dwarf2_per_cu_data *
16612 dwarf2_find_containing_comp_unit (sect_offset offset,
16613 struct objfile *objfile)
16614 {
16615 struct dwarf2_per_cu_data *this_cu;
16616 int low, high;
16617
16618 low = 0;
16619 high = dwarf2_per_objfile->n_comp_units - 1;
16620 while (high > low)
16621 {
16622 int mid = low + (high - low) / 2;
16623
16624 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16625 >= offset.sect_off)
16626 high = mid;
16627 else
16628 low = mid + 1;
16629 }
16630 gdb_assert (low == high);
16631 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16632 > offset.sect_off)
16633 {
16634 if (low == 0)
16635 error (_("Dwarf Error: could not find partial DIE containing "
16636 "offset 0x%lx [in module %s]"),
16637 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16638
16639 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16640 <= offset.sect_off);
16641 return dwarf2_per_objfile->all_comp_units[low-1];
16642 }
16643 else
16644 {
16645 this_cu = dwarf2_per_objfile->all_comp_units[low];
16646 if (low == dwarf2_per_objfile->n_comp_units - 1
16647 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16648 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16649 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16650 return this_cu;
16651 }
16652 }
16653
16654 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16655
16656 static void
16657 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16658 {
16659 memset (cu, 0, sizeof (*cu));
16660 per_cu->cu = cu;
16661 cu->per_cu = per_cu;
16662 cu->objfile = per_cu->objfile;
16663 obstack_init (&cu->comp_unit_obstack);
16664 }
16665
16666 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16667
16668 static void
16669 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16670 enum language pretend_language)
16671 {
16672 struct attribute *attr;
16673
16674 /* Set the language we're debugging. */
16675 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16676 if (attr)
16677 set_cu_language (DW_UNSND (attr), cu);
16678 else
16679 {
16680 cu->language = pretend_language;
16681 cu->language_defn = language_def (cu->language);
16682 }
16683
16684 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16685 if (attr)
16686 cu->producer = DW_STRING (attr);
16687 }
16688
16689 /* Release one cached compilation unit, CU. We unlink it from the tree
16690 of compilation units, but we don't remove it from the read_in_chain;
16691 the caller is responsible for that.
16692 NOTE: DATA is a void * because this function is also used as a
16693 cleanup routine. */
16694
16695 static void
16696 free_heap_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
16706 xfree (cu);
16707 }
16708
16709 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16710 when we're finished with it. We can't free the pointer itself, but be
16711 sure to unlink it from the cache. Also release any associated storage. */
16712
16713 static void
16714 free_stack_comp_unit (void *data)
16715 {
16716 struct dwarf2_cu *cu = data;
16717
16718 gdb_assert (cu->per_cu != NULL);
16719 cu->per_cu->cu = NULL;
16720 cu->per_cu = NULL;
16721
16722 obstack_free (&cu->comp_unit_obstack, NULL);
16723 cu->partial_dies = NULL;
16724 }
16725
16726 /* Free all cached compilation units. */
16727
16728 static void
16729 free_cached_comp_units (void *data)
16730 {
16731 struct dwarf2_per_cu_data *per_cu, **last_chain;
16732
16733 per_cu = dwarf2_per_objfile->read_in_chain;
16734 last_chain = &dwarf2_per_objfile->read_in_chain;
16735 while (per_cu != NULL)
16736 {
16737 struct dwarf2_per_cu_data *next_cu;
16738
16739 next_cu = per_cu->cu->read_in_chain;
16740
16741 free_heap_comp_unit (per_cu->cu);
16742 *last_chain = next_cu;
16743
16744 per_cu = next_cu;
16745 }
16746 }
16747
16748 /* Increase the age counter on each cached compilation unit, and free
16749 any that are too old. */
16750
16751 static void
16752 age_cached_comp_units (void)
16753 {
16754 struct dwarf2_per_cu_data *per_cu, **last_chain;
16755
16756 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16757 per_cu = dwarf2_per_objfile->read_in_chain;
16758 while (per_cu != NULL)
16759 {
16760 per_cu->cu->last_used ++;
16761 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16762 dwarf2_mark (per_cu->cu);
16763 per_cu = per_cu->cu->read_in_chain;
16764 }
16765
16766 per_cu = dwarf2_per_objfile->read_in_chain;
16767 last_chain = &dwarf2_per_objfile->read_in_chain;
16768 while (per_cu != NULL)
16769 {
16770 struct dwarf2_per_cu_data *next_cu;
16771
16772 next_cu = per_cu->cu->read_in_chain;
16773
16774 if (!per_cu->cu->mark)
16775 {
16776 free_heap_comp_unit (per_cu->cu);
16777 *last_chain = next_cu;
16778 }
16779 else
16780 last_chain = &per_cu->cu->read_in_chain;
16781
16782 per_cu = next_cu;
16783 }
16784 }
16785
16786 /* Remove a single compilation unit from the cache. */
16787
16788 static void
16789 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16790 {
16791 struct dwarf2_per_cu_data *per_cu, **last_chain;
16792
16793 per_cu = dwarf2_per_objfile->read_in_chain;
16794 last_chain = &dwarf2_per_objfile->read_in_chain;
16795 while (per_cu != NULL)
16796 {
16797 struct dwarf2_per_cu_data *next_cu;
16798
16799 next_cu = per_cu->cu->read_in_chain;
16800
16801 if (per_cu == target_per_cu)
16802 {
16803 free_heap_comp_unit (per_cu->cu);
16804 per_cu->cu = NULL;
16805 *last_chain = next_cu;
16806 break;
16807 }
16808 else
16809 last_chain = &per_cu->cu->read_in_chain;
16810
16811 per_cu = next_cu;
16812 }
16813 }
16814
16815 /* Release all extra memory associated with OBJFILE. */
16816
16817 void
16818 dwarf2_free_objfile (struct objfile *objfile)
16819 {
16820 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16821
16822 if (dwarf2_per_objfile == NULL)
16823 return;
16824
16825 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16826 free_cached_comp_units (NULL);
16827
16828 if (dwarf2_per_objfile->quick_file_names_table)
16829 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16830
16831 /* Everything else should be on the objfile obstack. */
16832 }
16833
16834 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16835 We store these in a hash table separate from the DIEs, and preserve them
16836 when the DIEs are flushed out of cache.
16837
16838 The CU "per_cu" pointer is needed because offset alone is not enough to
16839 uniquely identify the type. A file may have multiple .debug_types sections,
16840 or the type may come from a DWO file. We have to use something in
16841 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16842 routine, get_die_type_at_offset, from outside this file, and thus won't
16843 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16844 of the objfile. */
16845
16846 struct dwarf2_per_cu_offset_and_type
16847 {
16848 const struct dwarf2_per_cu_data *per_cu;
16849 sect_offset offset;
16850 struct type *type;
16851 };
16852
16853 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16854
16855 static hashval_t
16856 per_cu_offset_and_type_hash (const void *item)
16857 {
16858 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16859
16860 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16861 }
16862
16863 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16864
16865 static int
16866 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16867 {
16868 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16869 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16870
16871 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16872 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16873 }
16874
16875 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16876 table if necessary. For convenience, return TYPE.
16877
16878 The DIEs reading must have careful ordering to:
16879 * Not cause infite loops trying to read in DIEs as a prerequisite for
16880 reading current DIE.
16881 * Not trying to dereference contents of still incompletely read in types
16882 while reading in other DIEs.
16883 * Enable referencing still incompletely read in types just by a pointer to
16884 the type without accessing its fields.
16885
16886 Therefore caller should follow these rules:
16887 * Try to fetch any prerequisite types we may need to build this DIE type
16888 before building the type and calling set_die_type.
16889 * After building type call set_die_type for current DIE as soon as
16890 possible before fetching more types to complete the current type.
16891 * Make the type as complete as possible before fetching more types. */
16892
16893 static struct type *
16894 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16895 {
16896 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16897 struct objfile *objfile = cu->objfile;
16898
16899 /* For Ada types, make sure that the gnat-specific data is always
16900 initialized (if not already set). There are a few types where
16901 we should not be doing so, because the type-specific area is
16902 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16903 where the type-specific area is used to store the floatformat).
16904 But this is not a problem, because the gnat-specific information
16905 is actually not needed for these types. */
16906 if (need_gnat_info (cu)
16907 && TYPE_CODE (type) != TYPE_CODE_FUNC
16908 && TYPE_CODE (type) != TYPE_CODE_FLT
16909 && !HAVE_GNAT_AUX_INFO (type))
16910 INIT_GNAT_SPECIFIC (type);
16911
16912 if (dwarf2_per_objfile->die_type_hash == NULL)
16913 {
16914 dwarf2_per_objfile->die_type_hash =
16915 htab_create_alloc_ex (127,
16916 per_cu_offset_and_type_hash,
16917 per_cu_offset_and_type_eq,
16918 NULL,
16919 &objfile->objfile_obstack,
16920 hashtab_obstack_allocate,
16921 dummy_obstack_deallocate);
16922 }
16923
16924 ofs.per_cu = cu->per_cu;
16925 ofs.offset = die->offset;
16926 ofs.type = type;
16927 slot = (struct dwarf2_per_cu_offset_and_type **)
16928 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16929 if (*slot)
16930 complaint (&symfile_complaints,
16931 _("A problem internal to GDB: DIE 0x%x has type already set"),
16932 die->offset.sect_off);
16933 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16934 **slot = ofs;
16935 return type;
16936 }
16937
16938 /* Look up the type for the die at OFFSET in the appropriate type_hash
16939 table, or return NULL if the die does not have a saved type. */
16940
16941 static struct type *
16942 get_die_type_at_offset (sect_offset offset,
16943 struct dwarf2_per_cu_data *per_cu)
16944 {
16945 struct dwarf2_per_cu_offset_and_type *slot, ofs;
16946
16947 if (dwarf2_per_objfile->die_type_hash == NULL)
16948 return NULL;
16949
16950 ofs.per_cu = per_cu;
16951 ofs.offset = offset;
16952 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
16953 if (slot)
16954 return slot->type;
16955 else
16956 return NULL;
16957 }
16958
16959 /* Look up the type for DIE in the appropriate type_hash table,
16960 or return NULL if DIE does not have a saved type. */
16961
16962 static struct type *
16963 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16964 {
16965 return get_die_type_at_offset (die->offset, cu->per_cu);
16966 }
16967
16968 /* Add a dependence relationship from CU to REF_PER_CU. */
16969
16970 static void
16971 dwarf2_add_dependence (struct dwarf2_cu *cu,
16972 struct dwarf2_per_cu_data *ref_per_cu)
16973 {
16974 void **slot;
16975
16976 if (cu->dependencies == NULL)
16977 cu->dependencies
16978 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16979 NULL, &cu->comp_unit_obstack,
16980 hashtab_obstack_allocate,
16981 dummy_obstack_deallocate);
16982
16983 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16984 if (*slot == NULL)
16985 *slot = ref_per_cu;
16986 }
16987
16988 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16989 Set the mark field in every compilation unit in the
16990 cache that we must keep because we are keeping CU. */
16991
16992 static int
16993 dwarf2_mark_helper (void **slot, void *data)
16994 {
16995 struct dwarf2_per_cu_data *per_cu;
16996
16997 per_cu = (struct dwarf2_per_cu_data *) *slot;
16998
16999 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17000 reading of the chain. As such dependencies remain valid it is not much
17001 useful to track and undo them during QUIT cleanups. */
17002 if (per_cu->cu == NULL)
17003 return 1;
17004
17005 if (per_cu->cu->mark)
17006 return 1;
17007 per_cu->cu->mark = 1;
17008
17009 if (per_cu->cu->dependencies != NULL)
17010 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17011
17012 return 1;
17013 }
17014
17015 /* Set the mark field in CU and in every other compilation unit in the
17016 cache that we must keep because we are keeping CU. */
17017
17018 static void
17019 dwarf2_mark (struct dwarf2_cu *cu)
17020 {
17021 if (cu->mark)
17022 return;
17023 cu->mark = 1;
17024 if (cu->dependencies != NULL)
17025 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17026 }
17027
17028 static void
17029 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17030 {
17031 while (per_cu)
17032 {
17033 per_cu->cu->mark = 0;
17034 per_cu = per_cu->cu->read_in_chain;
17035 }
17036 }
17037
17038 /* Trivial hash function for partial_die_info: the hash value of a DIE
17039 is its offset in .debug_info for this objfile. */
17040
17041 static hashval_t
17042 partial_die_hash (const void *item)
17043 {
17044 const struct partial_die_info *part_die = item;
17045
17046 return part_die->offset.sect_off;
17047 }
17048
17049 /* Trivial comparison function for partial_die_info structures: two DIEs
17050 are equal if they have the same offset. */
17051
17052 static int
17053 partial_die_eq (const void *item_lhs, const void *item_rhs)
17054 {
17055 const struct partial_die_info *part_die_lhs = item_lhs;
17056 const struct partial_die_info *part_die_rhs = item_rhs;
17057
17058 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17059 }
17060
17061 static struct cmd_list_element *set_dwarf2_cmdlist;
17062 static struct cmd_list_element *show_dwarf2_cmdlist;
17063
17064 static void
17065 set_dwarf2_cmd (char *args, int from_tty)
17066 {
17067 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17068 }
17069
17070 static void
17071 show_dwarf2_cmd (char *args, int from_tty)
17072 {
17073 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17074 }
17075
17076 /* If section described by INFO was mmapped, munmap it now. */
17077
17078 static void
17079 munmap_section_buffer (struct dwarf2_section_info *info)
17080 {
17081 if (info->map_addr != NULL)
17082 {
17083 #ifdef HAVE_MMAP
17084 int res;
17085
17086 res = munmap (info->map_addr, info->map_len);
17087 gdb_assert (res == 0);
17088 #else
17089 /* Without HAVE_MMAP, we should never be here to begin with. */
17090 gdb_assert_not_reached ("no mmap support");
17091 #endif
17092 }
17093 }
17094
17095 /* munmap debug sections for OBJFILE, if necessary. */
17096
17097 static void
17098 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17099 {
17100 struct dwarf2_per_objfile *data = d;
17101 int ix;
17102 struct dwarf2_section_info *section;
17103
17104 /* This is sorted according to the order they're defined in to make it easier
17105 to keep in sync. */
17106 munmap_section_buffer (&data->info);
17107 munmap_section_buffer (&data->abbrev);
17108 munmap_section_buffer (&data->line);
17109 munmap_section_buffer (&data->loc);
17110 munmap_section_buffer (&data->macinfo);
17111 munmap_section_buffer (&data->macro);
17112 munmap_section_buffer (&data->str);
17113 munmap_section_buffer (&data->ranges);
17114 munmap_section_buffer (&data->addr);
17115 munmap_section_buffer (&data->frame);
17116 munmap_section_buffer (&data->eh_frame);
17117 munmap_section_buffer (&data->gdb_index);
17118
17119 for (ix = 0;
17120 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17121 ++ix)
17122 munmap_section_buffer (section);
17123
17124 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17125 VEC_free (dwarf2_per_cu_ptr,
17126 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17127
17128 VEC_free (dwarf2_section_info_def, data->types);
17129
17130 if (data->dwo_files)
17131 free_dwo_files (data->dwo_files, objfile);
17132 }
17133
17134 \f
17135 /* The "save gdb-index" command. */
17136
17137 /* The contents of the hash table we create when building the string
17138 table. */
17139 struct strtab_entry
17140 {
17141 offset_type offset;
17142 const char *str;
17143 };
17144
17145 /* Hash function for a strtab_entry.
17146
17147 Function is used only during write_hash_table so no index format backward
17148 compatibility is needed. */
17149
17150 static hashval_t
17151 hash_strtab_entry (const void *e)
17152 {
17153 const struct strtab_entry *entry = e;
17154 return mapped_index_string_hash (INT_MAX, entry->str);
17155 }
17156
17157 /* Equality function for a strtab_entry. */
17158
17159 static int
17160 eq_strtab_entry (const void *a, const void *b)
17161 {
17162 const struct strtab_entry *ea = a;
17163 const struct strtab_entry *eb = b;
17164 return !strcmp (ea->str, eb->str);
17165 }
17166
17167 /* Create a strtab_entry hash table. */
17168
17169 static htab_t
17170 create_strtab (void)
17171 {
17172 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17173 xfree, xcalloc, xfree);
17174 }
17175
17176 /* Add a string to the constant pool. Return the string's offset in
17177 host order. */
17178
17179 static offset_type
17180 add_string (htab_t table, struct obstack *cpool, const char *str)
17181 {
17182 void **slot;
17183 struct strtab_entry entry;
17184 struct strtab_entry *result;
17185
17186 entry.str = str;
17187 slot = htab_find_slot (table, &entry, INSERT);
17188 if (*slot)
17189 result = *slot;
17190 else
17191 {
17192 result = XNEW (struct strtab_entry);
17193 result->offset = obstack_object_size (cpool);
17194 result->str = str;
17195 obstack_grow_str0 (cpool, str);
17196 *slot = result;
17197 }
17198 return result->offset;
17199 }
17200
17201 /* An entry in the symbol table. */
17202 struct symtab_index_entry
17203 {
17204 /* The name of the symbol. */
17205 const char *name;
17206 /* The offset of the name in the constant pool. */
17207 offset_type index_offset;
17208 /* A sorted vector of the indices of all the CUs that hold an object
17209 of this name. */
17210 VEC (offset_type) *cu_indices;
17211 };
17212
17213 /* The symbol table. This is a power-of-2-sized hash table. */
17214 struct mapped_symtab
17215 {
17216 offset_type n_elements;
17217 offset_type size;
17218 struct symtab_index_entry **data;
17219 };
17220
17221 /* Hash function for a symtab_index_entry. */
17222
17223 static hashval_t
17224 hash_symtab_entry (const void *e)
17225 {
17226 const struct symtab_index_entry *entry = e;
17227 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17228 sizeof (offset_type) * VEC_length (offset_type,
17229 entry->cu_indices),
17230 0);
17231 }
17232
17233 /* Equality function for a symtab_index_entry. */
17234
17235 static int
17236 eq_symtab_entry (const void *a, const void *b)
17237 {
17238 const struct symtab_index_entry *ea = a;
17239 const struct symtab_index_entry *eb = b;
17240 int len = VEC_length (offset_type, ea->cu_indices);
17241 if (len != VEC_length (offset_type, eb->cu_indices))
17242 return 0;
17243 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17244 VEC_address (offset_type, eb->cu_indices),
17245 sizeof (offset_type) * len);
17246 }
17247
17248 /* Destroy a symtab_index_entry. */
17249
17250 static void
17251 delete_symtab_entry (void *p)
17252 {
17253 struct symtab_index_entry *entry = p;
17254 VEC_free (offset_type, entry->cu_indices);
17255 xfree (entry);
17256 }
17257
17258 /* Create a hash table holding symtab_index_entry objects. */
17259
17260 static htab_t
17261 create_symbol_hash_table (void)
17262 {
17263 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17264 delete_symtab_entry, xcalloc, xfree);
17265 }
17266
17267 /* Create a new mapped symtab object. */
17268
17269 static struct mapped_symtab *
17270 create_mapped_symtab (void)
17271 {
17272 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17273 symtab->n_elements = 0;
17274 symtab->size = 1024;
17275 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17276 return symtab;
17277 }
17278
17279 /* Destroy a mapped_symtab. */
17280
17281 static void
17282 cleanup_mapped_symtab (void *p)
17283 {
17284 struct mapped_symtab *symtab = p;
17285 /* The contents of the array are freed when the other hash table is
17286 destroyed. */
17287 xfree (symtab->data);
17288 xfree (symtab);
17289 }
17290
17291 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17292 the slot.
17293
17294 Function is used only during write_hash_table so no index format backward
17295 compatibility is needed. */
17296
17297 static struct symtab_index_entry **
17298 find_slot (struct mapped_symtab *symtab, const char *name)
17299 {
17300 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17301
17302 index = hash & (symtab->size - 1);
17303 step = ((hash * 17) & (symtab->size - 1)) | 1;
17304
17305 for (;;)
17306 {
17307 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17308 return &symtab->data[index];
17309 index = (index + step) & (symtab->size - 1);
17310 }
17311 }
17312
17313 /* Expand SYMTAB's hash table. */
17314
17315 static void
17316 hash_expand (struct mapped_symtab *symtab)
17317 {
17318 offset_type old_size = symtab->size;
17319 offset_type i;
17320 struct symtab_index_entry **old_entries = symtab->data;
17321
17322 symtab->size *= 2;
17323 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17324
17325 for (i = 0; i < old_size; ++i)
17326 {
17327 if (old_entries[i])
17328 {
17329 struct symtab_index_entry **slot = find_slot (symtab,
17330 old_entries[i]->name);
17331 *slot = old_entries[i];
17332 }
17333 }
17334
17335 xfree (old_entries);
17336 }
17337
17338 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17339 is the index of the CU in which the symbol appears. */
17340
17341 static void
17342 add_index_entry (struct mapped_symtab *symtab, const char *name,
17343 offset_type cu_index)
17344 {
17345 struct symtab_index_entry **slot;
17346
17347 ++symtab->n_elements;
17348 if (4 * symtab->n_elements / 3 >= symtab->size)
17349 hash_expand (symtab);
17350
17351 slot = find_slot (symtab, name);
17352 if (!*slot)
17353 {
17354 *slot = XNEW (struct symtab_index_entry);
17355 (*slot)->name = name;
17356 (*slot)->cu_indices = NULL;
17357 }
17358 /* Don't push an index twice. Due to how we add entries we only
17359 have to check the last one. */
17360 if (VEC_empty (offset_type, (*slot)->cu_indices)
17361 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17362 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17363 }
17364
17365 /* Add a vector of indices to the constant pool. */
17366
17367 static offset_type
17368 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17369 struct symtab_index_entry *entry)
17370 {
17371 void **slot;
17372
17373 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17374 if (!*slot)
17375 {
17376 offset_type len = VEC_length (offset_type, entry->cu_indices);
17377 offset_type val = MAYBE_SWAP (len);
17378 offset_type iter;
17379 int i;
17380
17381 *slot = entry;
17382 entry->index_offset = obstack_object_size (cpool);
17383
17384 obstack_grow (cpool, &val, sizeof (val));
17385 for (i = 0;
17386 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17387 ++i)
17388 {
17389 val = MAYBE_SWAP (iter);
17390 obstack_grow (cpool, &val, sizeof (val));
17391 }
17392 }
17393 else
17394 {
17395 struct symtab_index_entry *old_entry = *slot;
17396 entry->index_offset = old_entry->index_offset;
17397 entry = old_entry;
17398 }
17399 return entry->index_offset;
17400 }
17401
17402 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17403 constant pool entries going into the obstack CPOOL. */
17404
17405 static void
17406 write_hash_table (struct mapped_symtab *symtab,
17407 struct obstack *output, struct obstack *cpool)
17408 {
17409 offset_type i;
17410 htab_t symbol_hash_table;
17411 htab_t str_table;
17412
17413 symbol_hash_table = create_symbol_hash_table ();
17414 str_table = create_strtab ();
17415
17416 /* We add all the index vectors to the constant pool first, to
17417 ensure alignment is ok. */
17418 for (i = 0; i < symtab->size; ++i)
17419 {
17420 if (symtab->data[i])
17421 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17422 }
17423
17424 /* Now write out the hash table. */
17425 for (i = 0; i < symtab->size; ++i)
17426 {
17427 offset_type str_off, vec_off;
17428
17429 if (symtab->data[i])
17430 {
17431 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17432 vec_off = symtab->data[i]->index_offset;
17433 }
17434 else
17435 {
17436 /* While 0 is a valid constant pool index, it is not valid
17437 to have 0 for both offsets. */
17438 str_off = 0;
17439 vec_off = 0;
17440 }
17441
17442 str_off = MAYBE_SWAP (str_off);
17443 vec_off = MAYBE_SWAP (vec_off);
17444
17445 obstack_grow (output, &str_off, sizeof (str_off));
17446 obstack_grow (output, &vec_off, sizeof (vec_off));
17447 }
17448
17449 htab_delete (str_table);
17450 htab_delete (symbol_hash_table);
17451 }
17452
17453 /* Struct to map psymtab to CU index in the index file. */
17454 struct psymtab_cu_index_map
17455 {
17456 struct partial_symtab *psymtab;
17457 unsigned int cu_index;
17458 };
17459
17460 static hashval_t
17461 hash_psymtab_cu_index (const void *item)
17462 {
17463 const struct psymtab_cu_index_map *map = item;
17464
17465 return htab_hash_pointer (map->psymtab);
17466 }
17467
17468 static int
17469 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17470 {
17471 const struct psymtab_cu_index_map *lhs = item_lhs;
17472 const struct psymtab_cu_index_map *rhs = item_rhs;
17473
17474 return lhs->psymtab == rhs->psymtab;
17475 }
17476
17477 /* Helper struct for building the address table. */
17478 struct addrmap_index_data
17479 {
17480 struct objfile *objfile;
17481 struct obstack *addr_obstack;
17482 htab_t cu_index_htab;
17483
17484 /* Non-zero if the previous_* fields are valid.
17485 We can't write an entry until we see the next entry (since it is only then
17486 that we know the end of the entry). */
17487 int previous_valid;
17488 /* Index of the CU in the table of all CUs in the index file. */
17489 unsigned int previous_cu_index;
17490 /* Start address of the CU. */
17491 CORE_ADDR previous_cu_start;
17492 };
17493
17494 /* Write an address entry to OBSTACK. */
17495
17496 static void
17497 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17498 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17499 {
17500 offset_type cu_index_to_write;
17501 char addr[8];
17502 CORE_ADDR baseaddr;
17503
17504 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17505
17506 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17507 obstack_grow (obstack, addr, 8);
17508 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17509 obstack_grow (obstack, addr, 8);
17510 cu_index_to_write = MAYBE_SWAP (cu_index);
17511 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17512 }
17513
17514 /* Worker function for traversing an addrmap to build the address table. */
17515
17516 static int
17517 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17518 {
17519 struct addrmap_index_data *data = datap;
17520 struct partial_symtab *pst = obj;
17521
17522 if (data->previous_valid)
17523 add_address_entry (data->objfile, data->addr_obstack,
17524 data->previous_cu_start, start_addr,
17525 data->previous_cu_index);
17526
17527 data->previous_cu_start = start_addr;
17528 if (pst != NULL)
17529 {
17530 struct psymtab_cu_index_map find_map, *map;
17531 find_map.psymtab = pst;
17532 map = htab_find (data->cu_index_htab, &find_map);
17533 gdb_assert (map != NULL);
17534 data->previous_cu_index = map->cu_index;
17535 data->previous_valid = 1;
17536 }
17537 else
17538 data->previous_valid = 0;
17539
17540 return 0;
17541 }
17542
17543 /* Write OBJFILE's address map to OBSTACK.
17544 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17545 in the index file. */
17546
17547 static void
17548 write_address_map (struct objfile *objfile, struct obstack *obstack,
17549 htab_t cu_index_htab)
17550 {
17551 struct addrmap_index_data addrmap_index_data;
17552
17553 /* When writing the address table, we have to cope with the fact that
17554 the addrmap iterator only provides the start of a region; we have to
17555 wait until the next invocation to get the start of the next region. */
17556
17557 addrmap_index_data.objfile = objfile;
17558 addrmap_index_data.addr_obstack = obstack;
17559 addrmap_index_data.cu_index_htab = cu_index_htab;
17560 addrmap_index_data.previous_valid = 0;
17561
17562 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17563 &addrmap_index_data);
17564
17565 /* It's highly unlikely the last entry (end address = 0xff...ff)
17566 is valid, but we should still handle it.
17567 The end address is recorded as the start of the next region, but that
17568 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17569 anyway. */
17570 if (addrmap_index_data.previous_valid)
17571 add_address_entry (objfile, obstack,
17572 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17573 addrmap_index_data.previous_cu_index);
17574 }
17575
17576 /* Add a list of partial symbols to SYMTAB. */
17577
17578 static void
17579 write_psymbols (struct mapped_symtab *symtab,
17580 htab_t psyms_seen,
17581 struct partial_symbol **psymp,
17582 int count,
17583 offset_type cu_index,
17584 int is_static)
17585 {
17586 for (; count-- > 0; ++psymp)
17587 {
17588 void **slot, *lookup;
17589
17590 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17591 error (_("Ada is not currently supported by the index"));
17592
17593 /* We only want to add a given psymbol once. However, we also
17594 want to account for whether it is global or static. So, we
17595 may add it twice, using slightly different values. */
17596 if (is_static)
17597 {
17598 uintptr_t val = 1 | (uintptr_t) *psymp;
17599
17600 lookup = (void *) val;
17601 }
17602 else
17603 lookup = *psymp;
17604
17605 /* Only add a given psymbol once. */
17606 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17607 if (!*slot)
17608 {
17609 *slot = lookup;
17610 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17611 }
17612 }
17613 }
17614
17615 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17616 exception if there is an error. */
17617
17618 static void
17619 write_obstack (FILE *file, struct obstack *obstack)
17620 {
17621 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17622 file)
17623 != obstack_object_size (obstack))
17624 error (_("couldn't data write to file"));
17625 }
17626
17627 /* Unlink a file if the argument is not NULL. */
17628
17629 static void
17630 unlink_if_set (void *p)
17631 {
17632 char **filename = p;
17633 if (*filename)
17634 unlink (*filename);
17635 }
17636
17637 /* A helper struct used when iterating over debug_types. */
17638 struct signatured_type_index_data
17639 {
17640 struct objfile *objfile;
17641 struct mapped_symtab *symtab;
17642 struct obstack *types_list;
17643 htab_t psyms_seen;
17644 int cu_index;
17645 };
17646
17647 /* A helper function that writes a single signatured_type to an
17648 obstack. */
17649
17650 static int
17651 write_one_signatured_type (void **slot, void *d)
17652 {
17653 struct signatured_type_index_data *info = d;
17654 struct signatured_type *entry = (struct signatured_type *) *slot;
17655 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17656 struct partial_symtab *psymtab = per_cu->v.psymtab;
17657 gdb_byte val[8];
17658
17659 write_psymbols (info->symtab,
17660 info->psyms_seen,
17661 info->objfile->global_psymbols.list
17662 + psymtab->globals_offset,
17663 psymtab->n_global_syms, info->cu_index,
17664 0);
17665 write_psymbols (info->symtab,
17666 info->psyms_seen,
17667 info->objfile->static_psymbols.list
17668 + psymtab->statics_offset,
17669 psymtab->n_static_syms, info->cu_index,
17670 1);
17671
17672 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17673 entry->per_cu.offset.sect_off);
17674 obstack_grow (info->types_list, val, 8);
17675 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17676 entry->type_offset_in_tu.cu_off);
17677 obstack_grow (info->types_list, val, 8);
17678 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17679 obstack_grow (info->types_list, val, 8);
17680
17681 ++info->cu_index;
17682
17683 return 1;
17684 }
17685
17686 /* Recurse into all "included" dependencies and write their symbols as
17687 if they appeared in this psymtab. */
17688
17689 static void
17690 recursively_write_psymbols (struct objfile *objfile,
17691 struct partial_symtab *psymtab,
17692 struct mapped_symtab *symtab,
17693 htab_t psyms_seen,
17694 offset_type cu_index)
17695 {
17696 int i;
17697
17698 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17699 if (psymtab->dependencies[i]->user != NULL)
17700 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17701 symtab, psyms_seen, cu_index);
17702
17703 write_psymbols (symtab,
17704 psyms_seen,
17705 objfile->global_psymbols.list + psymtab->globals_offset,
17706 psymtab->n_global_syms, cu_index,
17707 0);
17708 write_psymbols (symtab,
17709 psyms_seen,
17710 objfile->static_psymbols.list + psymtab->statics_offset,
17711 psymtab->n_static_syms, cu_index,
17712 1);
17713 }
17714
17715 /* Create an index file for OBJFILE in the directory DIR. */
17716
17717 static void
17718 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17719 {
17720 struct cleanup *cleanup;
17721 char *filename, *cleanup_filename;
17722 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17723 struct obstack cu_list, types_cu_list;
17724 int i;
17725 FILE *out_file;
17726 struct mapped_symtab *symtab;
17727 offset_type val, size_of_contents, total_len;
17728 struct stat st;
17729 htab_t psyms_seen;
17730 htab_t cu_index_htab;
17731 struct psymtab_cu_index_map *psymtab_cu_index_map;
17732
17733 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17734 return;
17735
17736 if (dwarf2_per_objfile->using_index)
17737 error (_("Cannot use an index to create the index"));
17738
17739 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17740 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17741
17742 if (stat (objfile->name, &st) < 0)
17743 perror_with_name (objfile->name);
17744
17745 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17746 INDEX_SUFFIX, (char *) NULL);
17747 cleanup = make_cleanup (xfree, filename);
17748
17749 out_file = fopen (filename, "wb");
17750 if (!out_file)
17751 error (_("Can't open `%s' for writing"), filename);
17752
17753 cleanup_filename = filename;
17754 make_cleanup (unlink_if_set, &cleanup_filename);
17755
17756 symtab = create_mapped_symtab ();
17757 make_cleanup (cleanup_mapped_symtab, symtab);
17758
17759 obstack_init (&addr_obstack);
17760 make_cleanup_obstack_free (&addr_obstack);
17761
17762 obstack_init (&cu_list);
17763 make_cleanup_obstack_free (&cu_list);
17764
17765 obstack_init (&types_cu_list);
17766 make_cleanup_obstack_free (&types_cu_list);
17767
17768 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17769 NULL, xcalloc, xfree);
17770 make_cleanup_htab_delete (psyms_seen);
17771
17772 /* While we're scanning CU's create a table that maps a psymtab pointer
17773 (which is what addrmap records) to its index (which is what is recorded
17774 in the index file). This will later be needed to write the address
17775 table. */
17776 cu_index_htab = htab_create_alloc (100,
17777 hash_psymtab_cu_index,
17778 eq_psymtab_cu_index,
17779 NULL, xcalloc, xfree);
17780 make_cleanup_htab_delete (cu_index_htab);
17781 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17782 xmalloc (sizeof (struct psymtab_cu_index_map)
17783 * dwarf2_per_objfile->n_comp_units);
17784 make_cleanup (xfree, psymtab_cu_index_map);
17785
17786 /* The CU list is already sorted, so we don't need to do additional
17787 work here. Also, the debug_types entries do not appear in
17788 all_comp_units, but only in their own hash table. */
17789 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17790 {
17791 struct dwarf2_per_cu_data *per_cu
17792 = dwarf2_per_objfile->all_comp_units[i];
17793 struct partial_symtab *psymtab = per_cu->v.psymtab;
17794 gdb_byte val[8];
17795 struct psymtab_cu_index_map *map;
17796 void **slot;
17797
17798 if (psymtab->user == NULL)
17799 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
17800
17801 map = &psymtab_cu_index_map[i];
17802 map->psymtab = psymtab;
17803 map->cu_index = i;
17804 slot = htab_find_slot (cu_index_htab, map, INSERT);
17805 gdb_assert (slot != NULL);
17806 gdb_assert (*slot == NULL);
17807 *slot = map;
17808
17809 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17810 per_cu->offset.sect_off);
17811 obstack_grow (&cu_list, val, 8);
17812 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17813 obstack_grow (&cu_list, val, 8);
17814 }
17815
17816 /* Dump the address map. */
17817 write_address_map (objfile, &addr_obstack, cu_index_htab);
17818
17819 /* Write out the .debug_type entries, if any. */
17820 if (dwarf2_per_objfile->signatured_types)
17821 {
17822 struct signatured_type_index_data sig_data;
17823
17824 sig_data.objfile = objfile;
17825 sig_data.symtab = symtab;
17826 sig_data.types_list = &types_cu_list;
17827 sig_data.psyms_seen = psyms_seen;
17828 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17829 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17830 write_one_signatured_type, &sig_data);
17831 }
17832
17833 obstack_init (&constant_pool);
17834 make_cleanup_obstack_free (&constant_pool);
17835 obstack_init (&symtab_obstack);
17836 make_cleanup_obstack_free (&symtab_obstack);
17837 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17838
17839 obstack_init (&contents);
17840 make_cleanup_obstack_free (&contents);
17841 size_of_contents = 6 * sizeof (offset_type);
17842 total_len = size_of_contents;
17843
17844 /* The version number. */
17845 val = MAYBE_SWAP (6);
17846 obstack_grow (&contents, &val, sizeof (val));
17847
17848 /* The offset of the CU list 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 (&cu_list);
17852
17853 /* The offset of the types CU list 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 (&types_cu_list);
17857
17858 /* The offset of the address table from the start of the file. */
17859 val = MAYBE_SWAP (total_len);
17860 obstack_grow (&contents, &val, sizeof (val));
17861 total_len += obstack_object_size (&addr_obstack);
17862
17863 /* The offset of the symbol table from the start of the file. */
17864 val = MAYBE_SWAP (total_len);
17865 obstack_grow (&contents, &val, sizeof (val));
17866 total_len += obstack_object_size (&symtab_obstack);
17867
17868 /* The offset of the constant pool from the start of the file. */
17869 val = MAYBE_SWAP (total_len);
17870 obstack_grow (&contents, &val, sizeof (val));
17871 total_len += obstack_object_size (&constant_pool);
17872
17873 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17874
17875 write_obstack (out_file, &contents);
17876 write_obstack (out_file, &cu_list);
17877 write_obstack (out_file, &types_cu_list);
17878 write_obstack (out_file, &addr_obstack);
17879 write_obstack (out_file, &symtab_obstack);
17880 write_obstack (out_file, &constant_pool);
17881
17882 fclose (out_file);
17883
17884 /* We want to keep the file, so we set cleanup_filename to NULL
17885 here. See unlink_if_set. */
17886 cleanup_filename = NULL;
17887
17888 do_cleanups (cleanup);
17889 }
17890
17891 /* Implementation of the `save gdb-index' command.
17892
17893 Note that the file format used by this command is documented in the
17894 GDB manual. Any changes here must be documented there. */
17895
17896 static void
17897 save_gdb_index_command (char *arg, int from_tty)
17898 {
17899 struct objfile *objfile;
17900
17901 if (!arg || !*arg)
17902 error (_("usage: save gdb-index DIRECTORY"));
17903
17904 ALL_OBJFILES (objfile)
17905 {
17906 struct stat st;
17907
17908 /* If the objfile does not correspond to an actual file, skip it. */
17909 if (stat (objfile->name, &st) < 0)
17910 continue;
17911
17912 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17913 if (dwarf2_per_objfile)
17914 {
17915 volatile struct gdb_exception except;
17916
17917 TRY_CATCH (except, RETURN_MASK_ERROR)
17918 {
17919 write_psymtabs_to_index (objfile, arg);
17920 }
17921 if (except.reason < 0)
17922 exception_fprintf (gdb_stderr, except,
17923 _("Error while writing index for `%s': "),
17924 objfile->name);
17925 }
17926 }
17927 }
17928
17929 \f
17930
17931 int dwarf2_always_disassemble;
17932
17933 static void
17934 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17935 struct cmd_list_element *c, const char *value)
17936 {
17937 fprintf_filtered (file,
17938 _("Whether to always disassemble "
17939 "DWARF expressions is %s.\n"),
17940 value);
17941 }
17942
17943 static void
17944 show_check_physname (struct ui_file *file, int from_tty,
17945 struct cmd_list_element *c, const char *value)
17946 {
17947 fprintf_filtered (file,
17948 _("Whether to check \"physname\" is %s.\n"),
17949 value);
17950 }
17951
17952 void _initialize_dwarf2_read (void);
17953
17954 void
17955 _initialize_dwarf2_read (void)
17956 {
17957 struct cmd_list_element *c;
17958
17959 dwarf2_objfile_data_key
17960 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17961
17962 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17963 Set DWARF 2 specific variables.\n\
17964 Configure DWARF 2 variables such as the cache size"),
17965 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17966 0/*allow-unknown*/, &maintenance_set_cmdlist);
17967
17968 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17969 Show DWARF 2 specific variables\n\
17970 Show DWARF 2 variables such as the cache size"),
17971 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17972 0/*allow-unknown*/, &maintenance_show_cmdlist);
17973
17974 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17975 &dwarf2_max_cache_age, _("\
17976 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17977 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17978 A higher limit means that cached compilation units will be stored\n\
17979 in memory longer, and more total memory will be used. Zero disables\n\
17980 caching, which can slow down startup."),
17981 NULL,
17982 show_dwarf2_max_cache_age,
17983 &set_dwarf2_cmdlist,
17984 &show_dwarf2_cmdlist);
17985
17986 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17987 &dwarf2_always_disassemble, _("\
17988 Set whether `info address' always disassembles DWARF expressions."), _("\
17989 Show whether `info address' always disassembles DWARF expressions."), _("\
17990 When enabled, DWARF expressions are always printed in an assembly-like\n\
17991 syntax. When disabled, expressions will be printed in a more\n\
17992 conversational style, when possible."),
17993 NULL,
17994 show_dwarf2_always_disassemble,
17995 &set_dwarf2_cmdlist,
17996 &show_dwarf2_cmdlist);
17997
17998 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17999 Set debugging of the dwarf2 DIE reader."), _("\
18000 Show debugging of the dwarf2 DIE reader."), _("\
18001 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18002 The value is the maximum depth to print."),
18003 NULL,
18004 NULL,
18005 &setdebuglist, &showdebuglist);
18006
18007 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18008 Set cross-checking of \"physname\" code against demangler."), _("\
18009 Show cross-checking of \"physname\" code against demangler."), _("\
18010 When enabled, GDB's internal \"physname\" code is checked against\n\
18011 the demangler."),
18012 NULL, show_check_physname,
18013 &setdebuglist, &showdebuglist);
18014
18015 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18016 _("\
18017 Save a gdb-index file.\n\
18018 Usage: save gdb-index DIRECTORY"),
18019 &save_cmdlist);
18020 set_cmd_completer (c, filename_completer);
18021 }
This page took 0.412663 seconds and 4 git commands to generate.