* psymtab.c (find_pc_sect_symtab_from_partial): Return the symtab
[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 unsigned int bytes_read;
2436 char *name, *comp_dir;
2437 void **slot;
2438 struct quick_file_names *qfn;
2439 unsigned int line_offset;
2440
2441 lh = NULL;
2442 slot = NULL;
2443 line_offset = 0;
2444
2445 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2446 if (attr)
2447 {
2448 struct quick_file_names find_entry;
2449
2450 line_offset = DW_UNSND (attr);
2451
2452 /* We may have already read in this line header (TU line header sharing).
2453 If we have we're done. */
2454 find_entry.offset = line_offset;
2455 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2456 &find_entry, INSERT);
2457 if (*slot != NULL)
2458 {
2459 this_cu->v.quick->file_names = *slot;
2460 return;
2461 }
2462
2463 lh = dwarf_decode_line_header (line_offset, cu);
2464 }
2465 if (lh == NULL)
2466 {
2467 this_cu->v.quick->no_file_data = 1;
2468 return;
2469 }
2470
2471 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2472 qfn->offset = line_offset;
2473 gdb_assert (slot != NULL);
2474 *slot = qfn;
2475
2476 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2477
2478 qfn->num_file_names = lh->num_file_names;
2479 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2480 lh->num_file_names * sizeof (char *));
2481 for (i = 0; i < lh->num_file_names; ++i)
2482 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2483 qfn->real_names = NULL;
2484
2485 free_line_header (lh);
2486
2487 this_cu->v.quick->file_names = qfn;
2488 }
2489
2490 /* A helper for the "quick" functions which attempts to read the line
2491 table for THIS_CU. */
2492
2493 static struct quick_file_names *
2494 dw2_get_file_names (struct objfile *objfile,
2495 struct dwarf2_per_cu_data *this_cu)
2496 {
2497 if (this_cu->v.quick->file_names != NULL)
2498 return this_cu->v.quick->file_names;
2499 /* If we know there is no line data, no point in looking again. */
2500 if (this_cu->v.quick->no_file_data)
2501 return NULL;
2502
2503 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2504 in the stub for CUs, there's is no need to lookup the DWO file.
2505 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2506 DWO file. */
2507 if (this_cu->is_debug_types)
2508 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2509 else
2510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2511
2512 if (this_cu->v.quick->no_file_data)
2513 return NULL;
2514 return this_cu->v.quick->file_names;
2515 }
2516
2517 /* A helper for the "quick" functions which computes and caches the
2518 real path for a given file name from the line table. */
2519
2520 static const char *
2521 dw2_get_real_path (struct objfile *objfile,
2522 struct quick_file_names *qfn, int index)
2523 {
2524 if (qfn->real_names == NULL)
2525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2526 qfn->num_file_names, sizeof (char *));
2527
2528 if (qfn->real_names[index] == NULL)
2529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2530
2531 return qfn->real_names[index];
2532 }
2533
2534 static struct symtab *
2535 dw2_find_last_source_symtab (struct objfile *objfile)
2536 {
2537 int index;
2538
2539 dw2_setup (objfile);
2540 index = dwarf2_per_objfile->n_comp_units - 1;
2541 return dw2_instantiate_symtab (dw2_get_cu (index));
2542 }
2543
2544 /* Traversal function for dw2_forget_cached_source_info. */
2545
2546 static int
2547 dw2_free_cached_file_names (void **slot, void *info)
2548 {
2549 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2550
2551 if (file_data->real_names)
2552 {
2553 int i;
2554
2555 for (i = 0; i < file_data->num_file_names; ++i)
2556 {
2557 xfree ((void*) file_data->real_names[i]);
2558 file_data->real_names[i] = NULL;
2559 }
2560 }
2561
2562 return 1;
2563 }
2564
2565 static void
2566 dw2_forget_cached_source_info (struct objfile *objfile)
2567 {
2568 dw2_setup (objfile);
2569
2570 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2571 dw2_free_cached_file_names, NULL);
2572 }
2573
2574 /* Helper function for dw2_map_symtabs_matching_filename that expands
2575 the symtabs and calls the iterator. */
2576
2577 static int
2578 dw2_map_expand_apply (struct objfile *objfile,
2579 struct dwarf2_per_cu_data *per_cu,
2580 const char *name,
2581 const char *full_path, const char *real_path,
2582 int (*callback) (struct symtab *, void *),
2583 void *data)
2584 {
2585 struct symtab *last_made = objfile->symtabs;
2586
2587 /* Don't visit already-expanded CUs. */
2588 if (per_cu->v.quick->symtab)
2589 return 0;
2590
2591 /* This may expand more than one symtab, and we want to iterate over
2592 all of them. */
2593 dw2_instantiate_symtab (per_cu);
2594
2595 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2596 objfile->symtabs, last_made);
2597 }
2598
2599 /* Implementation of the map_symtabs_matching_filename method. */
2600
2601 static int
2602 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2603 const char *full_path, const char *real_path,
2604 int (*callback) (struct symtab *, void *),
2605 void *data)
2606 {
2607 int i;
2608 const char *name_basename = lbasename (name);
2609 int name_len = strlen (name);
2610 int is_abs = IS_ABSOLUTE_PATH (name);
2611
2612 dw2_setup (objfile);
2613
2614 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2615 + dwarf2_per_objfile->n_type_units); ++i)
2616 {
2617 int j;
2618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2619 struct quick_file_names *file_data;
2620
2621 /* We only need to look at symtabs not already expanded. */
2622 if (per_cu->v.quick->symtab)
2623 continue;
2624
2625 file_data = dw2_get_file_names (objfile, per_cu);
2626 if (file_data == NULL)
2627 continue;
2628
2629 for (j = 0; j < file_data->num_file_names; ++j)
2630 {
2631 const char *this_name = file_data->file_names[j];
2632
2633 if (FILENAME_CMP (name, this_name) == 0
2634 || (!is_abs && compare_filenames_for_search (this_name,
2635 name, name_len)))
2636 {
2637 if (dw2_map_expand_apply (objfile, per_cu,
2638 name, full_path, real_path,
2639 callback, data))
2640 return 1;
2641 }
2642
2643 /* Before we invoke realpath, which can get expensive when many
2644 files are involved, do a quick comparison of the basenames. */
2645 if (! basenames_may_differ
2646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2647 continue;
2648
2649 if (full_path != NULL)
2650 {
2651 const char *this_real_name = dw2_get_real_path (objfile,
2652 file_data, j);
2653
2654 if (this_real_name != NULL
2655 && (FILENAME_CMP (full_path, this_real_name) == 0
2656 || (!is_abs
2657 && compare_filenames_for_search (this_real_name,
2658 name, name_len))))
2659 {
2660 if (dw2_map_expand_apply (objfile, per_cu,
2661 name, full_path, real_path,
2662 callback, data))
2663 return 1;
2664 }
2665 }
2666
2667 if (real_path != NULL)
2668 {
2669 const char *this_real_name = dw2_get_real_path (objfile,
2670 file_data, j);
2671
2672 if (this_real_name != NULL
2673 && (FILENAME_CMP (real_path, this_real_name) == 0
2674 || (!is_abs
2675 && compare_filenames_for_search (this_real_name,
2676 name, name_len))))
2677 {
2678 if (dw2_map_expand_apply (objfile, per_cu,
2679 name, full_path, real_path,
2680 callback, data))
2681 return 1;
2682 }
2683 }
2684 }
2685 }
2686
2687 return 0;
2688 }
2689
2690 static struct symtab *
2691 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2692 const char *name, domain_enum domain)
2693 {
2694 /* We do all the work in the pre_expand_symtabs_matching hook
2695 instead. */
2696 return NULL;
2697 }
2698
2699 /* A helper function that expands all symtabs that hold an object
2700 named NAME. */
2701
2702 static void
2703 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2704 {
2705 dw2_setup (objfile);
2706
2707 /* index_table is NULL if OBJF_READNOW. */
2708 if (dwarf2_per_objfile->index_table)
2709 {
2710 offset_type *vec;
2711
2712 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2713 name, &vec))
2714 {
2715 offset_type i, len = MAYBE_SWAP (*vec);
2716 for (i = 0; i < len; ++i)
2717 {
2718 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2719 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2720
2721 dw2_instantiate_symtab (per_cu);
2722 }
2723 }
2724 }
2725 }
2726
2727 static void
2728 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2729 enum block_enum block_kind, const char *name,
2730 domain_enum domain)
2731 {
2732 dw2_do_expand_symtabs_matching (objfile, name);
2733 }
2734
2735 static void
2736 dw2_print_stats (struct objfile *objfile)
2737 {
2738 int i, count;
2739
2740 dw2_setup (objfile);
2741 count = 0;
2742 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2743 + dwarf2_per_objfile->n_type_units); ++i)
2744 {
2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2746
2747 if (!per_cu->v.quick->symtab)
2748 ++count;
2749 }
2750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2751 }
2752
2753 static void
2754 dw2_dump (struct objfile *objfile)
2755 {
2756 /* Nothing worth printing. */
2757 }
2758
2759 static void
2760 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2761 struct section_offsets *delta)
2762 {
2763 /* There's nothing to relocate here. */
2764 }
2765
2766 static void
2767 dw2_expand_symtabs_for_function (struct objfile *objfile,
2768 const char *func_name)
2769 {
2770 dw2_do_expand_symtabs_matching (objfile, func_name);
2771 }
2772
2773 static void
2774 dw2_expand_all_symtabs (struct objfile *objfile)
2775 {
2776 int i;
2777
2778 dw2_setup (objfile);
2779
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2781 + dwarf2_per_objfile->n_type_units); ++i)
2782 {
2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2784
2785 dw2_instantiate_symtab (per_cu);
2786 }
2787 }
2788
2789 static void
2790 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2791 const char *filename)
2792 {
2793 int i;
2794
2795 dw2_setup (objfile);
2796
2797 /* We don't need to consider type units here.
2798 This is only called for examining code, e.g. expand_line_sal.
2799 There can be an order of magnitude (or more) more type units
2800 than comp units, and we avoid them if we can. */
2801
2802 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2803 {
2804 int j;
2805 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2806 struct quick_file_names *file_data;
2807
2808 /* We only need to look at symtabs not already expanded. */
2809 if (per_cu->v.quick->symtab)
2810 continue;
2811
2812 file_data = dw2_get_file_names (objfile, per_cu);
2813 if (file_data == NULL)
2814 continue;
2815
2816 for (j = 0; j < file_data->num_file_names; ++j)
2817 {
2818 const char *this_name = file_data->file_names[j];
2819 if (FILENAME_CMP (this_name, filename) == 0)
2820 {
2821 dw2_instantiate_symtab (per_cu);
2822 break;
2823 }
2824 }
2825 }
2826 }
2827
2828 static const char *
2829 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2830 {
2831 struct dwarf2_per_cu_data *per_cu;
2832 offset_type *vec;
2833 struct quick_file_names *file_data;
2834
2835 dw2_setup (objfile);
2836
2837 /* index_table is NULL if OBJF_READNOW. */
2838 if (!dwarf2_per_objfile->index_table)
2839 {
2840 struct symtab *s;
2841
2842 ALL_OBJFILE_SYMTABS (objfile, s)
2843 if (s->primary)
2844 {
2845 struct blockvector *bv = BLOCKVECTOR (s);
2846 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2847 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2848
2849 if (sym)
2850 return sym->symtab->filename;
2851 }
2852 return NULL;
2853 }
2854
2855 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2856 name, &vec))
2857 return NULL;
2858
2859 /* Note that this just looks at the very first one named NAME -- but
2860 actually we are looking for a function. find_main_filename
2861 should be rewritten so that it doesn't require a custom hook. It
2862 could just use the ordinary symbol tables. */
2863 /* vec[0] is the length, which must always be >0. */
2864 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2865
2866 file_data = dw2_get_file_names (objfile, per_cu);
2867 if (file_data == NULL
2868 || file_data->num_file_names == 0)
2869 return NULL;
2870
2871 return file_data->file_names[file_data->num_file_names - 1];
2872 }
2873
2874 static void
2875 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2876 struct objfile *objfile, int global,
2877 int (*callback) (struct block *,
2878 struct symbol *, void *),
2879 void *data, symbol_compare_ftype *match,
2880 symbol_compare_ftype *ordered_compare)
2881 {
2882 /* Currently unimplemented; used for Ada. The function can be called if the
2883 current language is Ada for a non-Ada objfile using GNU index. As Ada
2884 does not look for non-Ada symbols this function should just return. */
2885 }
2886
2887 static void
2888 dw2_expand_symtabs_matching
2889 (struct objfile *objfile,
2890 int (*file_matcher) (const char *, void *),
2891 int (*name_matcher) (const char *, void *),
2892 enum search_domain kind,
2893 void *data)
2894 {
2895 int i;
2896 offset_type iter;
2897 struct mapped_index *index;
2898
2899 dw2_setup (objfile);
2900
2901 /* index_table is NULL if OBJF_READNOW. */
2902 if (!dwarf2_per_objfile->index_table)
2903 return;
2904 index = dwarf2_per_objfile->index_table;
2905
2906 if (file_matcher != NULL)
2907 {
2908 struct cleanup *cleanup;
2909 htab_t visited_found, visited_not_found;
2910
2911 visited_found = htab_create_alloc (10,
2912 htab_hash_pointer, htab_eq_pointer,
2913 NULL, xcalloc, xfree);
2914 cleanup = make_cleanup_htab_delete (visited_found);
2915 visited_not_found = htab_create_alloc (10,
2916 htab_hash_pointer, htab_eq_pointer,
2917 NULL, xcalloc, xfree);
2918 make_cleanup_htab_delete (visited_not_found);
2919
2920 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2921 + dwarf2_per_objfile->n_type_units); ++i)
2922 {
2923 int j;
2924 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2925 struct quick_file_names *file_data;
2926 void **slot;
2927
2928 per_cu->v.quick->mark = 0;
2929
2930 /* We only need to look at symtabs not already expanded. */
2931 if (per_cu->v.quick->symtab)
2932 continue;
2933
2934 file_data = dw2_get_file_names (objfile, per_cu);
2935 if (file_data == NULL)
2936 continue;
2937
2938 if (htab_find (visited_not_found, file_data) != NULL)
2939 continue;
2940 else if (htab_find (visited_found, file_data) != NULL)
2941 {
2942 per_cu->v.quick->mark = 1;
2943 continue;
2944 }
2945
2946 for (j = 0; j < file_data->num_file_names; ++j)
2947 {
2948 if (file_matcher (file_data->file_names[j], data))
2949 {
2950 per_cu->v.quick->mark = 1;
2951 break;
2952 }
2953 }
2954
2955 slot = htab_find_slot (per_cu->v.quick->mark
2956 ? visited_found
2957 : visited_not_found,
2958 file_data, INSERT);
2959 *slot = file_data;
2960 }
2961
2962 do_cleanups (cleanup);
2963 }
2964
2965 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2966 {
2967 offset_type idx = 2 * iter;
2968 const char *name;
2969 offset_type *vec, vec_len, vec_idx;
2970
2971 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2972 continue;
2973
2974 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2975
2976 if (! (*name_matcher) (name, data))
2977 continue;
2978
2979 /* The name was matched, now expand corresponding CUs that were
2980 marked. */
2981 vec = (offset_type *) (index->constant_pool
2982 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2983 vec_len = MAYBE_SWAP (vec[0]);
2984 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2985 {
2986 struct dwarf2_per_cu_data *per_cu;
2987
2988 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2989 if (file_matcher == NULL || per_cu->v.quick->mark)
2990 dw2_instantiate_symtab (per_cu);
2991 }
2992 }
2993 }
2994
2995 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
2996 symtab. */
2997
2998 static struct symtab *
2999 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3000 {
3001 int i;
3002
3003 if (BLOCKVECTOR (symtab) != NULL
3004 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3005 return symtab;
3006
3007 for (i = 0; symtab->includes[i]; ++i)
3008 {
3009 struct symtab *s;
3010
3011 s = recursively_find_pc_sect_symtab (s, pc);
3012 if (s != NULL)
3013 return s;
3014 }
3015
3016 return NULL;
3017 }
3018
3019 static struct symtab *
3020 dw2_find_pc_sect_symtab (struct objfile *objfile,
3021 struct minimal_symbol *msymbol,
3022 CORE_ADDR pc,
3023 struct obj_section *section,
3024 int warn_if_readin)
3025 {
3026 struct dwarf2_per_cu_data *data;
3027 struct symtab *result;
3028
3029 dw2_setup (objfile);
3030
3031 if (!objfile->psymtabs_addrmap)
3032 return NULL;
3033
3034 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3035 if (!data)
3036 return NULL;
3037
3038 if (warn_if_readin && data->v.quick->symtab)
3039 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3040 paddress (get_objfile_arch (objfile), pc));
3041
3042 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3043 gdb_assert (result != NULL);
3044 return result;
3045 }
3046
3047 static void
3048 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3049 void *data, int need_fullname)
3050 {
3051 int i;
3052 struct cleanup *cleanup;
3053 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3054 NULL, xcalloc, xfree);
3055
3056 cleanup = make_cleanup_htab_delete (visited);
3057 dw2_setup (objfile);
3058
3059 /* We can ignore file names coming from already-expanded CUs. */
3060 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3061 + dwarf2_per_objfile->n_type_units); ++i)
3062 {
3063 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3064
3065 if (per_cu->v.quick->symtab)
3066 {
3067 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3068 INSERT);
3069
3070 *slot = per_cu->v.quick->file_names;
3071 }
3072 }
3073
3074 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3075 + dwarf2_per_objfile->n_type_units); ++i)
3076 {
3077 int j;
3078 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3079 struct quick_file_names *file_data;
3080 void **slot;
3081
3082 /* We only need to look at symtabs not already expanded. */
3083 if (per_cu->v.quick->symtab)
3084 continue;
3085
3086 file_data = dw2_get_file_names (objfile, per_cu);
3087 if (file_data == NULL)
3088 continue;
3089
3090 slot = htab_find_slot (visited, file_data, INSERT);
3091 if (*slot)
3092 {
3093 /* Already visited. */
3094 continue;
3095 }
3096 *slot = file_data;
3097
3098 for (j = 0; j < file_data->num_file_names; ++j)
3099 {
3100 const char *this_real_name;
3101
3102 if (need_fullname)
3103 this_real_name = dw2_get_real_path (objfile, file_data, j);
3104 else
3105 this_real_name = NULL;
3106 (*fun) (file_data->file_names[j], this_real_name, data);
3107 }
3108 }
3109
3110 do_cleanups (cleanup);
3111 }
3112
3113 static int
3114 dw2_has_symbols (struct objfile *objfile)
3115 {
3116 return 1;
3117 }
3118
3119 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3120 {
3121 dw2_has_symbols,
3122 dw2_find_last_source_symtab,
3123 dw2_forget_cached_source_info,
3124 dw2_map_symtabs_matching_filename,
3125 dw2_lookup_symbol,
3126 dw2_pre_expand_symtabs_matching,
3127 dw2_print_stats,
3128 dw2_dump,
3129 dw2_relocate,
3130 dw2_expand_symtabs_for_function,
3131 dw2_expand_all_symtabs,
3132 dw2_expand_symtabs_with_filename,
3133 dw2_find_symbol_file,
3134 dw2_map_matching_symbols,
3135 dw2_expand_symtabs_matching,
3136 dw2_find_pc_sect_symtab,
3137 dw2_map_symbol_filenames
3138 };
3139
3140 /* Initialize for reading DWARF for this objfile. Return 0 if this
3141 file will use psymtabs, or 1 if using the GNU index. */
3142
3143 int
3144 dwarf2_initialize_objfile (struct objfile *objfile)
3145 {
3146 /* If we're about to read full symbols, don't bother with the
3147 indices. In this case we also don't care if some other debug
3148 format is making psymtabs, because they are all about to be
3149 expanded anyway. */
3150 if ((objfile->flags & OBJF_READNOW))
3151 {
3152 int i;
3153
3154 dwarf2_per_objfile->using_index = 1;
3155 create_all_comp_units (objfile);
3156 create_all_type_units (objfile);
3157 dwarf2_per_objfile->quick_file_names_table =
3158 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3159
3160 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3161 + dwarf2_per_objfile->n_type_units); ++i)
3162 {
3163 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3164
3165 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3166 struct dwarf2_per_cu_quick_data);
3167 }
3168
3169 /* Return 1 so that gdb sees the "quick" functions. However,
3170 these functions will be no-ops because we will have expanded
3171 all symtabs. */
3172 return 1;
3173 }
3174
3175 if (dwarf2_read_index (objfile))
3176 return 1;
3177
3178 return 0;
3179 }
3180
3181 \f
3182
3183 /* Build a partial symbol table. */
3184
3185 void
3186 dwarf2_build_psymtabs (struct objfile *objfile)
3187 {
3188 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3189 {
3190 init_psymbol_list (objfile, 1024);
3191 }
3192
3193 dwarf2_build_psymtabs_hard (objfile);
3194 }
3195
3196 /* Return TRUE if OFFSET is within CU_HEADER. */
3197
3198 static inline int
3199 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3200 {
3201 sect_offset bottom = { cu_header->offset.sect_off };
3202 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3203 + cu_header->initial_length_size) };
3204
3205 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3206 }
3207
3208 /* Read in the comp unit header information from the debug_info at info_ptr.
3209 NOTE: This leaves members offset, first_die_offset to be filled in
3210 by the caller. */
3211
3212 static gdb_byte *
3213 read_comp_unit_head (struct comp_unit_head *cu_header,
3214 gdb_byte *info_ptr, bfd *abfd)
3215 {
3216 int signed_addr;
3217 unsigned int bytes_read;
3218
3219 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3220 cu_header->initial_length_size = bytes_read;
3221 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3222 info_ptr += bytes_read;
3223 cu_header->version = read_2_bytes (abfd, info_ptr);
3224 info_ptr += 2;
3225 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3226 &bytes_read);
3227 info_ptr += bytes_read;
3228 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3229 info_ptr += 1;
3230 signed_addr = bfd_get_sign_extend_vma (abfd);
3231 if (signed_addr < 0)
3232 internal_error (__FILE__, __LINE__,
3233 _("read_comp_unit_head: dwarf from non elf file"));
3234 cu_header->signed_addr_p = signed_addr;
3235
3236 return info_ptr;
3237 }
3238
3239 /* Subroutine of read_and_check_comp_unit_head and
3240 read_and_check_type_unit_head to simplify them.
3241 Perform various error checking on the header. */
3242
3243 static void
3244 error_check_comp_unit_head (struct comp_unit_head *header,
3245 struct dwarf2_section_info *section)
3246 {
3247 bfd *abfd = section->asection->owner;
3248 const char *filename = bfd_get_filename (abfd);
3249
3250 if (header->version != 2 && header->version != 3 && header->version != 4)
3251 error (_("Dwarf Error: wrong version in compilation unit header "
3252 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3253 filename);
3254
3255 if (header->abbrev_offset.sect_off
3256 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3257 &dwarf2_per_objfile->abbrev))
3258 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3259 "(offset 0x%lx + 6) [in module %s]"),
3260 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3261 filename);
3262
3263 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3264 avoid potential 32-bit overflow. */
3265 if (((unsigned long) header->offset.sect_off
3266 + header->length + header->initial_length_size)
3267 > section->size)
3268 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3269 "(offset 0x%lx + 0) [in module %s]"),
3270 (long) header->length, (long) header->offset.sect_off,
3271 filename);
3272 }
3273
3274 /* Read in a CU/TU header and perform some basic error checking.
3275 The contents of the header are stored in HEADER.
3276 The result is a pointer to the start of the first DIE. */
3277
3278 static gdb_byte *
3279 read_and_check_comp_unit_head (struct comp_unit_head *header,
3280 struct dwarf2_section_info *section,
3281 gdb_byte *info_ptr,
3282 int is_debug_types_section)
3283 {
3284 gdb_byte *beg_of_comp_unit = info_ptr;
3285 bfd *abfd = section->asection->owner;
3286
3287 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3288
3289 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3290
3291 /* If we're reading a type unit, skip over the signature and
3292 type_offset fields. */
3293 if (is_debug_types_section)
3294 info_ptr += 8 /*signature*/ + header->offset_size;
3295
3296 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3297
3298 error_check_comp_unit_head (header, section);
3299
3300 return info_ptr;
3301 }
3302
3303 /* Read in the types comp unit header information from .debug_types entry at
3304 types_ptr. The result is a pointer to one past the end of the header. */
3305
3306 static gdb_byte *
3307 read_and_check_type_unit_head (struct comp_unit_head *header,
3308 struct dwarf2_section_info *section,
3309 gdb_byte *info_ptr,
3310 ULONGEST *signature,
3311 cu_offset *type_offset_in_tu)
3312 {
3313 gdb_byte *beg_of_comp_unit = info_ptr;
3314 bfd *abfd = section->asection->owner;
3315
3316 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3317
3318 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3319
3320 /* If we're reading a type unit, skip over the signature and
3321 type_offset fields. */
3322 if (signature != NULL)
3323 *signature = read_8_bytes (abfd, info_ptr);
3324 info_ptr += 8;
3325 if (type_offset_in_tu != NULL)
3326 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3327 header->offset_size);
3328 info_ptr += header->offset_size;
3329
3330 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3331
3332 error_check_comp_unit_head (header, section);
3333
3334 return info_ptr;
3335 }
3336
3337 /* Allocate a new partial symtab for file named NAME and mark this new
3338 partial symtab as being an include of PST. */
3339
3340 static void
3341 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3342 struct objfile *objfile)
3343 {
3344 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3345
3346 subpst->section_offsets = pst->section_offsets;
3347 subpst->textlow = 0;
3348 subpst->texthigh = 0;
3349
3350 subpst->dependencies = (struct partial_symtab **)
3351 obstack_alloc (&objfile->objfile_obstack,
3352 sizeof (struct partial_symtab *));
3353 subpst->dependencies[0] = pst;
3354 subpst->number_of_dependencies = 1;
3355
3356 subpst->globals_offset = 0;
3357 subpst->n_global_syms = 0;
3358 subpst->statics_offset = 0;
3359 subpst->n_static_syms = 0;
3360 subpst->symtab = NULL;
3361 subpst->read_symtab = pst->read_symtab;
3362 subpst->readin = 0;
3363
3364 /* No private part is necessary for include psymtabs. This property
3365 can be used to differentiate between such include psymtabs and
3366 the regular ones. */
3367 subpst->read_symtab_private = NULL;
3368 }
3369
3370 /* Read the Line Number Program data and extract the list of files
3371 included by the source file represented by PST. Build an include
3372 partial symtab for each of these included files. */
3373
3374 static void
3375 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3376 struct die_info *die,
3377 struct partial_symtab *pst)
3378 {
3379 struct line_header *lh = NULL;
3380 struct attribute *attr;
3381
3382 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3383 if (attr)
3384 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3385 if (lh == NULL)
3386 return; /* No linetable, so no includes. */
3387
3388 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3389 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3390
3391 free_line_header (lh);
3392 }
3393
3394 static hashval_t
3395 hash_signatured_type (const void *item)
3396 {
3397 const struct signatured_type *sig_type = item;
3398
3399 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3400 return sig_type->signature;
3401 }
3402
3403 static int
3404 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3405 {
3406 const struct signatured_type *lhs = item_lhs;
3407 const struct signatured_type *rhs = item_rhs;
3408
3409 return lhs->signature == rhs->signature;
3410 }
3411
3412 /* Allocate a hash table for signatured types. */
3413
3414 static htab_t
3415 allocate_signatured_type_table (struct objfile *objfile)
3416 {
3417 return htab_create_alloc_ex (41,
3418 hash_signatured_type,
3419 eq_signatured_type,
3420 NULL,
3421 &objfile->objfile_obstack,
3422 hashtab_obstack_allocate,
3423 dummy_obstack_deallocate);
3424 }
3425
3426 /* A helper function to add a signatured type CU to a table. */
3427
3428 static int
3429 add_signatured_type_cu_to_table (void **slot, void *datum)
3430 {
3431 struct signatured_type *sigt = *slot;
3432 struct dwarf2_per_cu_data ***datap = datum;
3433
3434 **datap = &sigt->per_cu;
3435 ++*datap;
3436
3437 return 1;
3438 }
3439
3440 /* Create the hash table of all entries in the .debug_types section.
3441 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3442 The result is a pointer to the hash table or NULL if there are
3443 no types. */
3444
3445 static htab_t
3446 create_debug_types_hash_table (struct dwo_file *dwo_file,
3447 VEC (dwarf2_section_info_def) *types)
3448 {
3449 struct objfile *objfile = dwarf2_per_objfile->objfile;
3450 htab_t types_htab = NULL;
3451 int ix;
3452 struct dwarf2_section_info *section;
3453
3454 if (VEC_empty (dwarf2_section_info_def, types))
3455 return NULL;
3456
3457 for (ix = 0;
3458 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3459 ++ix)
3460 {
3461 bfd *abfd;
3462 gdb_byte *info_ptr, *end_ptr;
3463
3464 dwarf2_read_section (objfile, section);
3465 info_ptr = section->buffer;
3466
3467 if (info_ptr == NULL)
3468 continue;
3469
3470 /* We can't set abfd until now because the section may be empty or
3471 not present, in which case section->asection will be NULL. */
3472 abfd = section->asection->owner;
3473
3474 if (types_htab == NULL)
3475 {
3476 if (dwo_file)
3477 types_htab = allocate_dwo_unit_table (objfile);
3478 else
3479 types_htab = allocate_signatured_type_table (objfile);
3480 }
3481
3482 if (dwarf2_die_debug)
3483 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3484 bfd_get_filename (abfd));
3485
3486 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3487 because we don't need to read any dies: the signature is in the
3488 header. */
3489
3490 end_ptr = info_ptr + section->size;
3491 while (info_ptr < end_ptr)
3492 {
3493 sect_offset offset;
3494 cu_offset type_offset_in_tu;
3495 ULONGEST signature;
3496 struct signatured_type *sig_type;
3497 struct dwo_unit *dwo_tu;
3498 void **slot;
3499 gdb_byte *ptr = info_ptr;
3500 struct comp_unit_head header;
3501 unsigned int length;
3502
3503 offset.sect_off = ptr - section->buffer;
3504
3505 /* We need to read the type's signature in order to build the hash
3506 table, but we don't need anything else just yet. */
3507
3508 ptr = read_and_check_type_unit_head (&header, section, ptr,
3509 &signature, &type_offset_in_tu);
3510
3511 length = header.initial_length_size + header.length;
3512
3513 /* Skip dummy type units. */
3514 if (ptr >= info_ptr + length
3515 || peek_abbrev_code (abfd, ptr) == 0)
3516 {
3517 info_ptr += header.initial_length_size + header.length;
3518 continue;
3519 }
3520
3521 if (dwo_file)
3522 {
3523 sig_type = NULL;
3524 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3525 struct dwo_unit);
3526 dwo_tu->dwo_file = dwo_file;
3527 dwo_tu->signature = signature;
3528 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3529 dwo_tu->info_or_types_section = section;
3530 dwo_tu->offset = offset;
3531 dwo_tu->length = length;
3532 }
3533 else
3534 {
3535 /* N.B.: type_offset is not usable if this type uses a DWO file.
3536 The real type_offset is in the DWO file. */
3537 dwo_tu = NULL;
3538 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3539 struct signatured_type);
3540 sig_type->signature = signature;
3541 sig_type->type_offset_in_tu = type_offset_in_tu;
3542 sig_type->per_cu.objfile = objfile;
3543 sig_type->per_cu.is_debug_types = 1;
3544 sig_type->per_cu.info_or_types_section = section;
3545 sig_type->per_cu.offset = offset;
3546 sig_type->per_cu.length = length;
3547 }
3548
3549 slot = htab_find_slot (types_htab,
3550 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3551 INSERT);
3552 gdb_assert (slot != NULL);
3553 if (*slot != NULL)
3554 {
3555 sect_offset dup_offset;
3556
3557 if (dwo_file)
3558 {
3559 const struct dwo_unit *dup_tu = *slot;
3560
3561 dup_offset = dup_tu->offset;
3562 }
3563 else
3564 {
3565 const struct signatured_type *dup_tu = *slot;
3566
3567 dup_offset = dup_tu->per_cu.offset;
3568 }
3569
3570 complaint (&symfile_complaints,
3571 _("debug type entry at offset 0x%x is duplicate to the "
3572 "entry at offset 0x%x, signature 0x%s"),
3573 offset.sect_off, dup_offset.sect_off,
3574 phex (signature, sizeof (signature)));
3575 }
3576 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3577
3578 if (dwarf2_die_debug)
3579 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3580 offset.sect_off,
3581 phex (signature, sizeof (signature)));
3582
3583 info_ptr += length;
3584 }
3585 }
3586
3587 return types_htab;
3588 }
3589
3590 /* Create the hash table of all entries in the .debug_types section,
3591 and initialize all_type_units.
3592 The result is zero if there is an error (e.g. missing .debug_types section),
3593 otherwise non-zero. */
3594
3595 static int
3596 create_all_type_units (struct objfile *objfile)
3597 {
3598 htab_t types_htab;
3599 struct dwarf2_per_cu_data **iter;
3600
3601 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3602 if (types_htab == NULL)
3603 {
3604 dwarf2_per_objfile->signatured_types = NULL;
3605 return 0;
3606 }
3607
3608 dwarf2_per_objfile->signatured_types = types_htab;
3609
3610 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3611 dwarf2_per_objfile->all_type_units
3612 = obstack_alloc (&objfile->objfile_obstack,
3613 dwarf2_per_objfile->n_type_units
3614 * sizeof (struct dwarf2_per_cu_data *));
3615 iter = &dwarf2_per_objfile->all_type_units[0];
3616 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3617 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3618 == dwarf2_per_objfile->n_type_units);
3619
3620 return 1;
3621 }
3622
3623 /* Lookup a signature based type for DW_FORM_ref_sig8.
3624 Returns NULL if signature SIG is not present in the table. */
3625
3626 static struct signatured_type *
3627 lookup_signatured_type (ULONGEST sig)
3628 {
3629 struct signatured_type find_entry, *entry;
3630
3631 if (dwarf2_per_objfile->signatured_types == NULL)
3632 {
3633 complaint (&symfile_complaints,
3634 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3635 return NULL;
3636 }
3637
3638 find_entry.signature = sig;
3639 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3640 return entry;
3641 }
3642
3643 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3644
3645 static void
3646 init_cu_die_reader (struct die_reader_specs *reader,
3647 struct dwarf2_cu *cu,
3648 struct dwarf2_section_info *section,
3649 struct dwo_file *dwo_file)
3650 {
3651 gdb_assert (section->readin && section->buffer != NULL);
3652 reader->abfd = section->asection->owner;
3653 reader->cu = cu;
3654 reader->dwo_file = dwo_file;
3655 reader->die_section = section;
3656 reader->buffer = section->buffer;
3657 }
3658
3659 /* Find the base address of the compilation unit for range lists and
3660 location lists. It will normally be specified by DW_AT_low_pc.
3661 In DWARF-3 draft 4, the base address could be overridden by
3662 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3663 compilation units with discontinuous ranges. */
3664
3665 static void
3666 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3667 {
3668 struct attribute *attr;
3669
3670 cu->base_known = 0;
3671 cu->base_address = 0;
3672
3673 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3674 if (attr)
3675 {
3676 cu->base_address = DW_ADDR (attr);
3677 cu->base_known = 1;
3678 }
3679 else
3680 {
3681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3682 if (attr)
3683 {
3684 cu->base_address = DW_ADDR (attr);
3685 cu->base_known = 1;
3686 }
3687 }
3688 }
3689
3690 /* Initialize a CU (or TU) and read its DIEs.
3691 If the CU defers to a DWO file, read the DWO file as well.
3692
3693 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3694 Otherwise, a new CU is allocated with xmalloc.
3695
3696 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3697 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3698
3699 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3700 linker) then DIE_READER_FUNC will not get called. */
3701
3702 static void
3703 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3704 int use_existing_cu, int keep,
3705 die_reader_func_ftype *die_reader_func,
3706 void *data)
3707 {
3708 struct objfile *objfile = dwarf2_per_objfile->objfile;
3709 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3710 bfd *abfd = section->asection->owner;
3711 struct dwarf2_cu *cu;
3712 gdb_byte *begin_info_ptr, *info_ptr;
3713 struct die_reader_specs reader;
3714 struct die_info *comp_unit_die;
3715 int has_children;
3716 struct attribute *attr;
3717 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3718 struct signatured_type *sig_type = NULL;
3719
3720 if (use_existing_cu)
3721 gdb_assert (keep);
3722
3723 cleanups = make_cleanup (null_cleanup, NULL);
3724
3725 /* This is cheap if the section is already read in. */
3726 dwarf2_read_section (objfile, section);
3727
3728 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3729
3730 if (use_existing_cu && this_cu->cu != NULL)
3731 {
3732 cu = this_cu->cu;
3733 info_ptr += cu->header.first_die_offset.cu_off;
3734 }
3735 else
3736 {
3737 /* If !use_existing_cu, this_cu->cu must be NULL. */
3738 gdb_assert (this_cu->cu == NULL);
3739
3740 cu = xmalloc (sizeof (*cu));
3741 init_one_comp_unit (cu, this_cu);
3742
3743 /* If an error occurs while loading, release our storage. */
3744 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3745
3746 if (this_cu->is_debug_types)
3747 {
3748 ULONGEST signature;
3749
3750 info_ptr = read_and_check_type_unit_head (&cu->header,
3751 section, info_ptr,
3752 &signature, NULL);
3753
3754 /* There's no way to get from PER_CU to its containing
3755 struct signatured_type.
3756 But we have the signature so we can use that. */
3757 sig_type = lookup_signatured_type (signature);
3758 /* We've already scanned all the signatured types,
3759 this must succeed. */
3760 gdb_assert (sig_type != NULL);
3761 gdb_assert (&sig_type->per_cu == this_cu);
3762 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3763
3764 /* LENGTH has not been set yet for type units. */
3765 this_cu->length = cu->header.length + cu->header.initial_length_size;
3766
3767 /* Establish the type offset that can be used to lookup the type. */
3768 sig_type->type_offset_in_section.sect_off =
3769 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3770 }
3771 else
3772 {
3773 info_ptr = read_and_check_comp_unit_head (&cu->header,
3774 section, info_ptr, 0);
3775
3776 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3777 gdb_assert (this_cu->length
3778 == cu->header.length + cu->header.initial_length_size);
3779 }
3780 }
3781
3782 /* Skip dummy compilation units. */
3783 if (info_ptr >= begin_info_ptr + this_cu->length
3784 || peek_abbrev_code (abfd, info_ptr) == 0)
3785 {
3786 do_cleanups (cleanups);
3787 return;
3788 }
3789
3790 /* Read the abbrevs for this compilation unit into a table. */
3791 if (cu->dwarf2_abbrevs == NULL)
3792 {
3793 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3794 make_cleanup (dwarf2_free_abbrev_table, cu);
3795 }
3796
3797 /* Read the top level CU/TU die. */
3798 init_cu_die_reader (&reader, cu, section, NULL);
3799 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3800
3801 /* If we have a DWO stub, process it and then read in the DWO file.
3802 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3803 a DWO CU, that this test will fail. */
3804 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3805 if (attr)
3806 {
3807 char *dwo_name = DW_STRING (attr);
3808 const char *comp_dir;
3809 struct dwo_unit *dwo_unit;
3810 ULONGEST signature; /* Or dwo_id. */
3811 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3812 int i,num_extra_attrs;
3813
3814 if (has_children)
3815 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3816 " has children (offset 0x%x) [in module %s]"),
3817 this_cu->offset.sect_off, bfd_get_filename (abfd));
3818
3819 /* These attributes aren't processed until later:
3820 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3821 However, the attribute is found in the stub which we won't have later.
3822 In order to not impose this complication on the rest of the code,
3823 we read them here and copy them to the DWO CU/TU die. */
3824 stmt_list = low_pc = high_pc = ranges = NULL;
3825
3826 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3827 DWO file. */
3828 if (! this_cu->is_debug_types)
3829 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3830 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3831 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3832 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3833
3834 /* There should be a DW_AT_addr_base attribute here (if needed).
3835 We need the value before we can process DW_FORM_GNU_addr_index. */
3836 cu->addr_base = 0;
3837 cu->have_addr_base = 0;
3838 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3839 if (attr)
3840 {
3841 cu->addr_base = DW_UNSND (attr);
3842 cu->have_addr_base = 1;
3843 }
3844
3845 if (this_cu->is_debug_types)
3846 {
3847 gdb_assert (sig_type != NULL);
3848 signature = sig_type->signature;
3849 }
3850 else
3851 {
3852 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3853 if (! attr)
3854 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3855 dwo_name);
3856 signature = DW_UNSND (attr);
3857 }
3858
3859 /* We may need the comp_dir in order to find the DWO file. */
3860 comp_dir = NULL;
3861 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3862 if (attr)
3863 comp_dir = DW_STRING (attr);
3864
3865 if (this_cu->is_debug_types)
3866 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3867 else
3868 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3869 signature);
3870
3871 if (dwo_unit == NULL)
3872 {
3873 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3874 " with ID %s [in module %s]"),
3875 this_cu->offset.sect_off,
3876 phex (signature, sizeof (signature)),
3877 objfile->name);
3878 }
3879
3880 /* Set up for reading the DWO CU/TU. */
3881 cu->dwo_unit = dwo_unit;
3882 section = dwo_unit->info_or_types_section;
3883 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3884 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3885
3886 if (this_cu->is_debug_types)
3887 {
3888 ULONGEST signature;
3889
3890 info_ptr = read_and_check_type_unit_head (&cu->header,
3891 section, info_ptr,
3892 &signature, NULL);
3893 gdb_assert (sig_type->signature == signature);
3894 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3895 gdb_assert (dwo_unit->length
3896 == cu->header.length + cu->header.initial_length_size);
3897
3898 /* Establish the type offset that can be used to lookup the type.
3899 For DWO files, we don't know it until now. */
3900 sig_type->type_offset_in_section.sect_off =
3901 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3902 }
3903 else
3904 {
3905 info_ptr = read_and_check_comp_unit_head (&cu->header,
3906 section, info_ptr, 0);
3907 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3908 gdb_assert (dwo_unit->length
3909 == cu->header.length + cu->header.initial_length_size);
3910 }
3911
3912 /* Discard the original CU's abbrev table, and read the DWO's. */
3913 dwarf2_free_abbrev_table (cu);
3914 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3915
3916 /* Read in the die, but leave space to copy over the attributes
3917 from the stub. This has the benefit of simplifying the rest of
3918 the code - all the real work is done here. */
3919 num_extra_attrs = ((stmt_list != NULL)
3920 + (low_pc != NULL)
3921 + (high_pc != NULL)
3922 + (ranges != NULL));
3923 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3924 &has_children, num_extra_attrs);
3925
3926 /* Copy over the attributes from the stub to the DWO die. */
3927 i = comp_unit_die->num_attrs;
3928 if (stmt_list != NULL)
3929 comp_unit_die->attrs[i++] = *stmt_list;
3930 if (low_pc != NULL)
3931 comp_unit_die->attrs[i++] = *low_pc;
3932 if (high_pc != NULL)
3933 comp_unit_die->attrs[i++] = *high_pc;
3934 if (ranges != NULL)
3935 comp_unit_die->attrs[i++] = *ranges;
3936 comp_unit_die->num_attrs += num_extra_attrs;
3937
3938 /* Skip dummy compilation units. */
3939 if (info_ptr >= begin_info_ptr + dwo_unit->length
3940 || peek_abbrev_code (abfd, info_ptr) == 0)
3941 {
3942 do_cleanups (cleanups);
3943 return;
3944 }
3945 }
3946
3947 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3948
3949 if (free_cu_cleanup != NULL)
3950 {
3951 if (keep)
3952 {
3953 /* We've successfully allocated this compilation unit. Let our
3954 caller clean it up when finished with it. */
3955 discard_cleanups (free_cu_cleanup);
3956
3957 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3958 So we have to manually free the abbrev table. */
3959 dwarf2_free_abbrev_table (cu);
3960
3961 /* Link this CU into read_in_chain. */
3962 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3963 dwarf2_per_objfile->read_in_chain = this_cu;
3964 }
3965 else
3966 do_cleanups (free_cu_cleanup);
3967 }
3968
3969 do_cleanups (cleanups);
3970 }
3971
3972 /* Read CU/TU THIS_CU in section SECTION,
3973 but do not follow DW_AT_GNU_dwo_name if present.
3974 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3975 have already done the lookup to find the DWO file).
3976
3977 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3978 THIS_CU->is_debug_types, but nothing else.
3979
3980 We fill in THIS_CU->length.
3981
3982 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3983 linker) then DIE_READER_FUNC will not get called.
3984
3985 THIS_CU->cu is always freed when done.
3986 This is done in order to not leave THIS_CU->cu in a state where we have
3987 to care whether it refers to the "main" CU or the DWO CU. */
3988
3989 static void
3990 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3991 struct dwarf2_section_info *abbrev_section,
3992 struct dwo_file *dwo_file,
3993 die_reader_func_ftype *die_reader_func,
3994 void *data)
3995 {
3996 struct objfile *objfile = dwarf2_per_objfile->objfile;
3997 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3998 bfd *abfd = section->asection->owner;
3999 struct dwarf2_cu cu;
4000 gdb_byte *begin_info_ptr, *info_ptr;
4001 struct die_reader_specs reader;
4002 struct cleanup *cleanups;
4003 struct die_info *comp_unit_die;
4004 int has_children;
4005
4006 gdb_assert (this_cu->cu == NULL);
4007
4008 /* This is cheap if the section is already read in. */
4009 dwarf2_read_section (objfile, section);
4010
4011 init_one_comp_unit (&cu, this_cu);
4012
4013 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4014
4015 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4016 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
4017 this_cu->is_debug_types);
4018
4019 this_cu->length = cu.header.length + cu.header.initial_length_size;
4020
4021 /* Skip dummy compilation units. */
4022 if (info_ptr >= begin_info_ptr + this_cu->length
4023 || peek_abbrev_code (abfd, info_ptr) == 0)
4024 {
4025 do_cleanups (cleanups);
4026 return;
4027 }
4028
4029 dwarf2_read_abbrevs (&cu, abbrev_section);
4030 make_cleanup (dwarf2_free_abbrev_table, &cu);
4031
4032 init_cu_die_reader (&reader, &cu, section, dwo_file);
4033 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4034
4035 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4036
4037 do_cleanups (cleanups);
4038 }
4039
4040 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4041 does not lookup the specified DWO file.
4042 This cannot be used to read DWO files.
4043
4044 THIS_CU->cu is always freed when done.
4045 This is done in order to not leave THIS_CU->cu in a state where we have
4046 to care whether it refers to the "main" CU or the DWO CU.
4047 We can revisit this if the data shows there's a performance issue. */
4048
4049 static void
4050 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4051 die_reader_func_ftype *die_reader_func,
4052 void *data)
4053 {
4054 init_cutu_and_read_dies_no_follow (this_cu,
4055 &dwarf2_per_objfile->abbrev,
4056 NULL,
4057 die_reader_func, data);
4058 }
4059
4060 /* die_reader_func for process_psymtab_comp_unit. */
4061
4062 static void
4063 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4064 gdb_byte *info_ptr,
4065 struct die_info *comp_unit_die,
4066 int has_children,
4067 void *data)
4068 {
4069 struct dwarf2_cu *cu = reader->cu;
4070 struct objfile *objfile = cu->objfile;
4071 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4072 bfd *abfd = objfile->obfd;
4073 struct attribute *attr;
4074 CORE_ADDR baseaddr;
4075 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4076 struct partial_symtab *pst;
4077 int has_pc_info;
4078 const char *filename;
4079 int *want_partial_unit_ptr = data;
4080
4081 if (comp_unit_die->tag == DW_TAG_partial_unit
4082 && (want_partial_unit_ptr == NULL
4083 || !*want_partial_unit_ptr))
4084 return;
4085
4086 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4087
4088 cu->list_in_scope = &file_symbols;
4089
4090 /* Allocate a new partial symbol table structure. */
4091 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4092 if (attr == NULL || !DW_STRING (attr))
4093 filename = "";
4094 else
4095 filename = DW_STRING (attr);
4096 pst = start_psymtab_common (objfile, objfile->section_offsets,
4097 filename,
4098 /* TEXTLOW and TEXTHIGH are set below. */
4099 0,
4100 objfile->global_psymbols.next,
4101 objfile->static_psymbols.next);
4102 pst->psymtabs_addrmap_supported = 1;
4103
4104 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4105 if (attr != NULL)
4106 pst->dirname = DW_STRING (attr);
4107
4108 pst->read_symtab_private = per_cu;
4109
4110 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4111
4112 /* Store the function that reads in the rest of the symbol table. */
4113 pst->read_symtab = dwarf2_psymtab_to_symtab;
4114
4115 per_cu->v.psymtab = pst;
4116
4117 dwarf2_find_base_address (comp_unit_die, cu);
4118
4119 /* Possibly set the default values of LOWPC and HIGHPC from
4120 `DW_AT_ranges'. */
4121 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4122 &best_highpc, cu, pst);
4123 if (has_pc_info == 1 && best_lowpc < best_highpc)
4124 /* Store the contiguous range if it is not empty; it can be empty for
4125 CUs with no code. */
4126 addrmap_set_empty (objfile->psymtabs_addrmap,
4127 best_lowpc + baseaddr,
4128 best_highpc + baseaddr - 1, pst);
4129
4130 /* Check if comp unit has_children.
4131 If so, read the rest of the partial symbols from this comp unit.
4132 If not, there's no more debug_info for this comp unit. */
4133 if (has_children)
4134 {
4135 struct partial_die_info *first_die;
4136 CORE_ADDR lowpc, highpc;
4137
4138 lowpc = ((CORE_ADDR) -1);
4139 highpc = ((CORE_ADDR) 0);
4140
4141 first_die = load_partial_dies (reader, info_ptr, 1);
4142
4143 scan_partial_symbols (first_die, &lowpc, &highpc,
4144 ! has_pc_info, cu);
4145
4146 /* If we didn't find a lowpc, set it to highpc to avoid
4147 complaints from `maint check'. */
4148 if (lowpc == ((CORE_ADDR) -1))
4149 lowpc = highpc;
4150
4151 /* If the compilation unit didn't have an explicit address range,
4152 then use the information extracted from its child dies. */
4153 if (! has_pc_info)
4154 {
4155 best_lowpc = lowpc;
4156 best_highpc = highpc;
4157 }
4158 }
4159 pst->textlow = best_lowpc + baseaddr;
4160 pst->texthigh = best_highpc + baseaddr;
4161
4162 pst->n_global_syms = objfile->global_psymbols.next -
4163 (objfile->global_psymbols.list + pst->globals_offset);
4164 pst->n_static_syms = objfile->static_psymbols.next -
4165 (objfile->static_psymbols.list + pst->statics_offset);
4166 sort_pst_symbols (pst);
4167
4168 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4169 {
4170 int i;
4171 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4172 struct dwarf2_per_cu_data *iter;
4173
4174 /* Fill in 'dependencies' here; we fill in 'users' in a
4175 post-pass. */
4176 pst->number_of_dependencies = len;
4177 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4178 len * sizeof (struct symtab *));
4179 for (i = 0;
4180 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4181 i, iter);
4182 ++i)
4183 pst->dependencies[i] = iter->v.psymtab;
4184
4185 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4186 }
4187
4188 if (per_cu->is_debug_types)
4189 {
4190 /* It's not clear we want to do anything with stmt lists here.
4191 Waiting to see what gcc ultimately does. */
4192 }
4193 else
4194 {
4195 /* Get the list of files included in the current compilation unit,
4196 and build a psymtab for each of them. */
4197 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4198 }
4199 }
4200
4201 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4202 Process compilation unit THIS_CU for a psymtab. */
4203
4204 static void
4205 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4206 int want_partial_unit)
4207 {
4208 /* If this compilation unit was already read in, free the
4209 cached copy in order to read it in again. This is
4210 necessary because we skipped some symbols when we first
4211 read in the compilation unit (see load_partial_dies).
4212 This problem could be avoided, but the benefit is unclear. */
4213 if (this_cu->cu != NULL)
4214 free_one_cached_comp_unit (this_cu);
4215
4216 gdb_assert (! this_cu->is_debug_types);
4217 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4218 &want_partial_unit);
4219
4220 /* Age out any secondary CUs. */
4221 age_cached_comp_units ();
4222 }
4223
4224 /* Traversal function for htab_traverse_noresize.
4225 Process one .debug_types comp-unit. */
4226
4227 static int
4228 process_psymtab_type_unit (void **slot, void *info)
4229 {
4230 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4231 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4232
4233 gdb_assert (per_cu->is_debug_types);
4234 gdb_assert (info == NULL);
4235
4236 /* If this compilation unit was already read in, free the
4237 cached copy in order to read it in again. This is
4238 necessary because we skipped some symbols when we first
4239 read in the compilation unit (see load_partial_dies).
4240 This problem could be avoided, but the benefit is unclear. */
4241 if (per_cu->cu != NULL)
4242 free_one_cached_comp_unit (per_cu);
4243
4244 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4245 NULL);
4246
4247 /* Age out any secondary CUs. */
4248 age_cached_comp_units ();
4249
4250 return 1;
4251 }
4252
4253 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4254 Build partial symbol tables for the .debug_types comp-units. */
4255
4256 static void
4257 build_type_psymtabs (struct objfile *objfile)
4258 {
4259 if (! create_all_type_units (objfile))
4260 return;
4261
4262 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4263 process_psymtab_type_unit, NULL);
4264 }
4265
4266 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4267
4268 static void
4269 psymtabs_addrmap_cleanup (void *o)
4270 {
4271 struct objfile *objfile = o;
4272
4273 objfile->psymtabs_addrmap = NULL;
4274 }
4275
4276 /* Compute the 'user' field for each psymtab in OBJFILE. */
4277
4278 static void
4279 set_partial_user (struct objfile *objfile)
4280 {
4281 int i;
4282
4283 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4284 {
4285 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4286 struct partial_symtab *pst = per_cu->v.psymtab;
4287 int j;
4288
4289 for (j = 0; j < pst->number_of_dependencies; ++j)
4290 {
4291 /* Set the 'user' field only if it is not already set. */
4292 if (pst->dependencies[j]->user == NULL)
4293 pst->dependencies[j]->user = pst;
4294 }
4295 }
4296 }
4297
4298 /* Build the partial symbol table by doing a quick pass through the
4299 .debug_info and .debug_abbrev sections. */
4300
4301 static void
4302 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4303 {
4304 struct cleanup *back_to, *addrmap_cleanup;
4305 struct obstack temp_obstack;
4306 int i;
4307
4308 dwarf2_per_objfile->reading_partial_symbols = 1;
4309
4310 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4311
4312 /* Any cached compilation units will be linked by the per-objfile
4313 read_in_chain. Make sure to free them when we're done. */
4314 back_to = make_cleanup (free_cached_comp_units, NULL);
4315
4316 build_type_psymtabs (objfile);
4317
4318 create_all_comp_units (objfile);
4319
4320 /* Create a temporary address map on a temporary obstack. We later
4321 copy this to the final obstack. */
4322 obstack_init (&temp_obstack);
4323 make_cleanup_obstack_free (&temp_obstack);
4324 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4325 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4326
4327 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4328 {
4329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4330
4331 process_psymtab_comp_unit (per_cu, 0);
4332 }
4333
4334 set_partial_user (objfile);
4335
4336 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4337 &objfile->objfile_obstack);
4338 discard_cleanups (addrmap_cleanup);
4339
4340 do_cleanups (back_to);
4341 }
4342
4343 /* die_reader_func for load_partial_comp_unit. */
4344
4345 static void
4346 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4347 gdb_byte *info_ptr,
4348 struct die_info *comp_unit_die,
4349 int has_children,
4350 void *data)
4351 {
4352 struct dwarf2_cu *cu = reader->cu;
4353
4354 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4355
4356 /* Check if comp unit has_children.
4357 If so, read the rest of the partial symbols from this comp unit.
4358 If not, there's no more debug_info for this comp unit. */
4359 if (has_children)
4360 load_partial_dies (reader, info_ptr, 0);
4361 }
4362
4363 /* Load the partial DIEs for a secondary CU into memory.
4364 This is also used when rereading a primary CU with load_all_dies. */
4365
4366 static void
4367 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4368 {
4369 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4370 }
4371
4372 /* Create a list of all compilation units in OBJFILE.
4373 This is only done for -readnow and building partial symtabs. */
4374
4375 static void
4376 create_all_comp_units (struct objfile *objfile)
4377 {
4378 int n_allocated;
4379 int n_comp_units;
4380 struct dwarf2_per_cu_data **all_comp_units;
4381 gdb_byte *info_ptr;
4382
4383 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4384 info_ptr = dwarf2_per_objfile->info.buffer;
4385
4386 n_comp_units = 0;
4387 n_allocated = 10;
4388 all_comp_units = xmalloc (n_allocated
4389 * sizeof (struct dwarf2_per_cu_data *));
4390
4391 while (info_ptr < dwarf2_per_objfile->info.buffer
4392 + dwarf2_per_objfile->info.size)
4393 {
4394 unsigned int length, initial_length_size;
4395 struct dwarf2_per_cu_data *this_cu;
4396 sect_offset offset;
4397
4398 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4399
4400 /* Read just enough information to find out where the next
4401 compilation unit is. */
4402 length = read_initial_length (objfile->obfd, info_ptr,
4403 &initial_length_size);
4404
4405 /* Save the compilation unit for later lookup. */
4406 this_cu = obstack_alloc (&objfile->objfile_obstack,
4407 sizeof (struct dwarf2_per_cu_data));
4408 memset (this_cu, 0, sizeof (*this_cu));
4409 this_cu->offset = offset;
4410 this_cu->length = length + initial_length_size;
4411 this_cu->objfile = objfile;
4412 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4413
4414 if (n_comp_units == n_allocated)
4415 {
4416 n_allocated *= 2;
4417 all_comp_units = xrealloc (all_comp_units,
4418 n_allocated
4419 * sizeof (struct dwarf2_per_cu_data *));
4420 }
4421 all_comp_units[n_comp_units++] = this_cu;
4422
4423 info_ptr = info_ptr + this_cu->length;
4424 }
4425
4426 dwarf2_per_objfile->all_comp_units
4427 = obstack_alloc (&objfile->objfile_obstack,
4428 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4429 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4430 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4431 xfree (all_comp_units);
4432 dwarf2_per_objfile->n_comp_units = n_comp_units;
4433 }
4434
4435 /* Process all loaded DIEs for compilation unit CU, starting at
4436 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4437 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4438 DW_AT_ranges). If NEED_PC is set, then this function will set
4439 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4440 and record the covered ranges in the addrmap. */
4441
4442 static void
4443 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4444 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4445 {
4446 struct partial_die_info *pdi;
4447
4448 /* Now, march along the PDI's, descending into ones which have
4449 interesting children but skipping the children of the other ones,
4450 until we reach the end of the compilation unit. */
4451
4452 pdi = first_die;
4453
4454 while (pdi != NULL)
4455 {
4456 fixup_partial_die (pdi, cu);
4457
4458 /* Anonymous namespaces or modules have no name but have interesting
4459 children, so we need to look at them. Ditto for anonymous
4460 enums. */
4461
4462 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4463 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4464 || pdi->tag == DW_TAG_imported_unit)
4465 {
4466 switch (pdi->tag)
4467 {
4468 case DW_TAG_subprogram:
4469 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4470 break;
4471 case DW_TAG_constant:
4472 case DW_TAG_variable:
4473 case DW_TAG_typedef:
4474 case DW_TAG_union_type:
4475 if (!pdi->is_declaration)
4476 {
4477 add_partial_symbol (pdi, cu);
4478 }
4479 break;
4480 case DW_TAG_class_type:
4481 case DW_TAG_interface_type:
4482 case DW_TAG_structure_type:
4483 if (!pdi->is_declaration)
4484 {
4485 add_partial_symbol (pdi, cu);
4486 }
4487 break;
4488 case DW_TAG_enumeration_type:
4489 if (!pdi->is_declaration)
4490 add_partial_enumeration (pdi, cu);
4491 break;
4492 case DW_TAG_base_type:
4493 case DW_TAG_subrange_type:
4494 /* File scope base type definitions are added to the partial
4495 symbol table. */
4496 add_partial_symbol (pdi, cu);
4497 break;
4498 case DW_TAG_namespace:
4499 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4500 break;
4501 case DW_TAG_module:
4502 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4503 break;
4504 case DW_TAG_imported_unit:
4505 {
4506 struct dwarf2_per_cu_data *per_cu;
4507
4508 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4509 cu->objfile);
4510
4511 /* Go read the partial unit, if needed. */
4512 if (per_cu->v.psymtab == NULL)
4513 process_psymtab_comp_unit (per_cu, 1);
4514
4515 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4516 per_cu);
4517 }
4518 break;
4519 default:
4520 break;
4521 }
4522 }
4523
4524 /* If the die has a sibling, skip to the sibling. */
4525
4526 pdi = pdi->die_sibling;
4527 }
4528 }
4529
4530 /* Functions used to compute the fully scoped name of a partial DIE.
4531
4532 Normally, this is simple. For C++, the parent DIE's fully scoped
4533 name is concatenated with "::" and the partial DIE's name. For
4534 Java, the same thing occurs except that "." is used instead of "::".
4535 Enumerators are an exception; they use the scope of their parent
4536 enumeration type, i.e. the name of the enumeration type is not
4537 prepended to the enumerator.
4538
4539 There are two complexities. One is DW_AT_specification; in this
4540 case "parent" means the parent of the target of the specification,
4541 instead of the direct parent of the DIE. The other is compilers
4542 which do not emit DW_TAG_namespace; in this case we try to guess
4543 the fully qualified name of structure types from their members'
4544 linkage names. This must be done using the DIE's children rather
4545 than the children of any DW_AT_specification target. We only need
4546 to do this for structures at the top level, i.e. if the target of
4547 any DW_AT_specification (if any; otherwise the DIE itself) does not
4548 have a parent. */
4549
4550 /* Compute the scope prefix associated with PDI's parent, in
4551 compilation unit CU. The result will be allocated on CU's
4552 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4553 field. NULL is returned if no prefix is necessary. */
4554 static char *
4555 partial_die_parent_scope (struct partial_die_info *pdi,
4556 struct dwarf2_cu *cu)
4557 {
4558 char *grandparent_scope;
4559 struct partial_die_info *parent, *real_pdi;
4560
4561 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4562 then this means the parent of the specification DIE. */
4563
4564 real_pdi = pdi;
4565 while (real_pdi->has_specification)
4566 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4567
4568 parent = real_pdi->die_parent;
4569 if (parent == NULL)
4570 return NULL;
4571
4572 if (parent->scope_set)
4573 return parent->scope;
4574
4575 fixup_partial_die (parent, cu);
4576
4577 grandparent_scope = partial_die_parent_scope (parent, cu);
4578
4579 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4580 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4581 Work around this problem here. */
4582 if (cu->language == language_cplus
4583 && parent->tag == DW_TAG_namespace
4584 && strcmp (parent->name, "::") == 0
4585 && grandparent_scope == NULL)
4586 {
4587 parent->scope = NULL;
4588 parent->scope_set = 1;
4589 return NULL;
4590 }
4591
4592 if (pdi->tag == DW_TAG_enumerator)
4593 /* Enumerators should not get the name of the enumeration as a prefix. */
4594 parent->scope = grandparent_scope;
4595 else if (parent->tag == DW_TAG_namespace
4596 || parent->tag == DW_TAG_module
4597 || parent->tag == DW_TAG_structure_type
4598 || parent->tag == DW_TAG_class_type
4599 || parent->tag == DW_TAG_interface_type
4600 || parent->tag == DW_TAG_union_type
4601 || parent->tag == DW_TAG_enumeration_type)
4602 {
4603 if (grandparent_scope == NULL)
4604 parent->scope = parent->name;
4605 else
4606 parent->scope = typename_concat (&cu->comp_unit_obstack,
4607 grandparent_scope,
4608 parent->name, 0, cu);
4609 }
4610 else
4611 {
4612 /* FIXME drow/2004-04-01: What should we be doing with
4613 function-local names? For partial symbols, we should probably be
4614 ignoring them. */
4615 complaint (&symfile_complaints,
4616 _("unhandled containing DIE tag %d for DIE at %d"),
4617 parent->tag, pdi->offset.sect_off);
4618 parent->scope = grandparent_scope;
4619 }
4620
4621 parent->scope_set = 1;
4622 return parent->scope;
4623 }
4624
4625 /* Return the fully scoped name associated with PDI, from compilation unit
4626 CU. The result will be allocated with malloc. */
4627
4628 static char *
4629 partial_die_full_name (struct partial_die_info *pdi,
4630 struct dwarf2_cu *cu)
4631 {
4632 char *parent_scope;
4633
4634 /* If this is a template instantiation, we can not work out the
4635 template arguments from partial DIEs. So, unfortunately, we have
4636 to go through the full DIEs. At least any work we do building
4637 types here will be reused if full symbols are loaded later. */
4638 if (pdi->has_template_arguments)
4639 {
4640 fixup_partial_die (pdi, cu);
4641
4642 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4643 {
4644 struct die_info *die;
4645 struct attribute attr;
4646 struct dwarf2_cu *ref_cu = cu;
4647
4648 /* DW_FORM_ref_addr is using section offset. */
4649 attr.name = 0;
4650 attr.form = DW_FORM_ref_addr;
4651 attr.u.unsnd = pdi->offset.sect_off;
4652 die = follow_die_ref (NULL, &attr, &ref_cu);
4653
4654 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4655 }
4656 }
4657
4658 parent_scope = partial_die_parent_scope (pdi, cu);
4659 if (parent_scope == NULL)
4660 return NULL;
4661 else
4662 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4663 }
4664
4665 static void
4666 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4667 {
4668 struct objfile *objfile = cu->objfile;
4669 CORE_ADDR addr = 0;
4670 char *actual_name = NULL;
4671 CORE_ADDR baseaddr;
4672 int built_actual_name = 0;
4673
4674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4675
4676 actual_name = partial_die_full_name (pdi, cu);
4677 if (actual_name)
4678 built_actual_name = 1;
4679
4680 if (actual_name == NULL)
4681 actual_name = pdi->name;
4682
4683 switch (pdi->tag)
4684 {
4685 case DW_TAG_subprogram:
4686 if (pdi->is_external || cu->language == language_ada)
4687 {
4688 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4689 of the global scope. But in Ada, we want to be able to access
4690 nested procedures globally. So all Ada subprograms are stored
4691 in the global scope. */
4692 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4693 mst_text, objfile); */
4694 add_psymbol_to_list (actual_name, strlen (actual_name),
4695 built_actual_name,
4696 VAR_DOMAIN, LOC_BLOCK,
4697 &objfile->global_psymbols,
4698 0, pdi->lowpc + baseaddr,
4699 cu->language, objfile);
4700 }
4701 else
4702 {
4703 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4704 mst_file_text, objfile); */
4705 add_psymbol_to_list (actual_name, strlen (actual_name),
4706 built_actual_name,
4707 VAR_DOMAIN, LOC_BLOCK,
4708 &objfile->static_psymbols,
4709 0, pdi->lowpc + baseaddr,
4710 cu->language, objfile);
4711 }
4712 break;
4713 case DW_TAG_constant:
4714 {
4715 struct psymbol_allocation_list *list;
4716
4717 if (pdi->is_external)
4718 list = &objfile->global_psymbols;
4719 else
4720 list = &objfile->static_psymbols;
4721 add_psymbol_to_list (actual_name, strlen (actual_name),
4722 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4723 list, 0, 0, cu->language, objfile);
4724 }
4725 break;
4726 case DW_TAG_variable:
4727 if (pdi->d.locdesc)
4728 addr = decode_locdesc (pdi->d.locdesc, cu);
4729
4730 if (pdi->d.locdesc
4731 && addr == 0
4732 && !dwarf2_per_objfile->has_section_at_zero)
4733 {
4734 /* A global or static variable may also have been stripped
4735 out by the linker if unused, in which case its address
4736 will be nullified; do not add such variables into partial
4737 symbol table then. */
4738 }
4739 else if (pdi->is_external)
4740 {
4741 /* Global Variable.
4742 Don't enter into the minimal symbol tables as there is
4743 a minimal symbol table entry from the ELF symbols already.
4744 Enter into partial symbol table if it has a location
4745 descriptor or a type.
4746 If the location descriptor is missing, new_symbol will create
4747 a LOC_UNRESOLVED symbol, the address of the variable will then
4748 be determined from the minimal symbol table whenever the variable
4749 is referenced.
4750 The address for the partial symbol table entry is not
4751 used by GDB, but it comes in handy for debugging partial symbol
4752 table building. */
4753
4754 if (pdi->d.locdesc || pdi->has_type)
4755 add_psymbol_to_list (actual_name, strlen (actual_name),
4756 built_actual_name,
4757 VAR_DOMAIN, LOC_STATIC,
4758 &objfile->global_psymbols,
4759 0, addr + baseaddr,
4760 cu->language, objfile);
4761 }
4762 else
4763 {
4764 /* Static Variable. Skip symbols without location descriptors. */
4765 if (pdi->d.locdesc == NULL)
4766 {
4767 if (built_actual_name)
4768 xfree (actual_name);
4769 return;
4770 }
4771 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4772 mst_file_data, objfile); */
4773 add_psymbol_to_list (actual_name, strlen (actual_name),
4774 built_actual_name,
4775 VAR_DOMAIN, LOC_STATIC,
4776 &objfile->static_psymbols,
4777 0, addr + baseaddr,
4778 cu->language, objfile);
4779 }
4780 break;
4781 case DW_TAG_typedef:
4782 case DW_TAG_base_type:
4783 case DW_TAG_subrange_type:
4784 add_psymbol_to_list (actual_name, strlen (actual_name),
4785 built_actual_name,
4786 VAR_DOMAIN, LOC_TYPEDEF,
4787 &objfile->static_psymbols,
4788 0, (CORE_ADDR) 0, cu->language, objfile);
4789 break;
4790 case DW_TAG_namespace:
4791 add_psymbol_to_list (actual_name, strlen (actual_name),
4792 built_actual_name,
4793 VAR_DOMAIN, LOC_TYPEDEF,
4794 &objfile->global_psymbols,
4795 0, (CORE_ADDR) 0, cu->language, objfile);
4796 break;
4797 case DW_TAG_class_type:
4798 case DW_TAG_interface_type:
4799 case DW_TAG_structure_type:
4800 case DW_TAG_union_type:
4801 case DW_TAG_enumeration_type:
4802 /* Skip external references. The DWARF standard says in the section
4803 about "Structure, Union, and Class Type Entries": "An incomplete
4804 structure, union or class type is represented by a structure,
4805 union or class entry that does not have a byte size attribute
4806 and that has a DW_AT_declaration attribute." */
4807 if (!pdi->has_byte_size && pdi->is_declaration)
4808 {
4809 if (built_actual_name)
4810 xfree (actual_name);
4811 return;
4812 }
4813
4814 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4815 static vs. global. */
4816 add_psymbol_to_list (actual_name, strlen (actual_name),
4817 built_actual_name,
4818 STRUCT_DOMAIN, LOC_TYPEDEF,
4819 (cu->language == language_cplus
4820 || cu->language == language_java)
4821 ? &objfile->global_psymbols
4822 : &objfile->static_psymbols,
4823 0, (CORE_ADDR) 0, cu->language, objfile);
4824
4825 break;
4826 case DW_TAG_enumerator:
4827 add_psymbol_to_list (actual_name, strlen (actual_name),
4828 built_actual_name,
4829 VAR_DOMAIN, LOC_CONST,
4830 (cu->language == language_cplus
4831 || cu->language == language_java)
4832 ? &objfile->global_psymbols
4833 : &objfile->static_psymbols,
4834 0, (CORE_ADDR) 0, cu->language, objfile);
4835 break;
4836 default:
4837 break;
4838 }
4839
4840 if (built_actual_name)
4841 xfree (actual_name);
4842 }
4843
4844 /* Read a partial die corresponding to a namespace; also, add a symbol
4845 corresponding to that namespace to the symbol table. NAMESPACE is
4846 the name of the enclosing namespace. */
4847
4848 static void
4849 add_partial_namespace (struct partial_die_info *pdi,
4850 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4851 int need_pc, struct dwarf2_cu *cu)
4852 {
4853 /* Add a symbol for the namespace. */
4854
4855 add_partial_symbol (pdi, cu);
4856
4857 /* Now scan partial symbols in that namespace. */
4858
4859 if (pdi->has_children)
4860 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4861 }
4862
4863 /* Read a partial die corresponding to a Fortran module. */
4864
4865 static void
4866 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4867 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4868 {
4869 /* Now scan partial symbols in that module. */
4870
4871 if (pdi->has_children)
4872 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4873 }
4874
4875 /* Read a partial die corresponding to a subprogram and create a partial
4876 symbol for that subprogram. When the CU language allows it, this
4877 routine also defines a partial symbol for each nested subprogram
4878 that this subprogram contains.
4879
4880 DIE my also be a lexical block, in which case we simply search
4881 recursively for suprograms defined inside that lexical block.
4882 Again, this is only performed when the CU language allows this
4883 type of definitions. */
4884
4885 static void
4886 add_partial_subprogram (struct partial_die_info *pdi,
4887 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4888 int need_pc, struct dwarf2_cu *cu)
4889 {
4890 if (pdi->tag == DW_TAG_subprogram)
4891 {
4892 if (pdi->has_pc_info)
4893 {
4894 if (pdi->lowpc < *lowpc)
4895 *lowpc = pdi->lowpc;
4896 if (pdi->highpc > *highpc)
4897 *highpc = pdi->highpc;
4898 if (need_pc)
4899 {
4900 CORE_ADDR baseaddr;
4901 struct objfile *objfile = cu->objfile;
4902
4903 baseaddr = ANOFFSET (objfile->section_offsets,
4904 SECT_OFF_TEXT (objfile));
4905 addrmap_set_empty (objfile->psymtabs_addrmap,
4906 pdi->lowpc + baseaddr,
4907 pdi->highpc - 1 + baseaddr,
4908 cu->per_cu->v.psymtab);
4909 }
4910 }
4911
4912 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4913 {
4914 if (!pdi->is_declaration)
4915 /* Ignore subprogram DIEs that do not have a name, they are
4916 illegal. Do not emit a complaint at this point, we will
4917 do so when we convert this psymtab into a symtab. */
4918 if (pdi->name)
4919 add_partial_symbol (pdi, cu);
4920 }
4921 }
4922
4923 if (! pdi->has_children)
4924 return;
4925
4926 if (cu->language == language_ada)
4927 {
4928 pdi = pdi->die_child;
4929 while (pdi != NULL)
4930 {
4931 fixup_partial_die (pdi, cu);
4932 if (pdi->tag == DW_TAG_subprogram
4933 || pdi->tag == DW_TAG_lexical_block)
4934 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4935 pdi = pdi->die_sibling;
4936 }
4937 }
4938 }
4939
4940 /* Read a partial die corresponding to an enumeration type. */
4941
4942 static void
4943 add_partial_enumeration (struct partial_die_info *enum_pdi,
4944 struct dwarf2_cu *cu)
4945 {
4946 struct partial_die_info *pdi;
4947
4948 if (enum_pdi->name != NULL)
4949 add_partial_symbol (enum_pdi, cu);
4950
4951 pdi = enum_pdi->die_child;
4952 while (pdi)
4953 {
4954 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4955 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4956 else
4957 add_partial_symbol (pdi, cu);
4958 pdi = pdi->die_sibling;
4959 }
4960 }
4961
4962 /* Return the initial uleb128 in the die at INFO_PTR. */
4963
4964 static unsigned int
4965 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4966 {
4967 unsigned int bytes_read;
4968
4969 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4970 }
4971
4972 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4973 Return the corresponding abbrev, or NULL if the number is zero (indicating
4974 an empty DIE). In either case *BYTES_READ will be set to the length of
4975 the initial number. */
4976
4977 static struct abbrev_info *
4978 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4979 struct dwarf2_cu *cu)
4980 {
4981 bfd *abfd = cu->objfile->obfd;
4982 unsigned int abbrev_number;
4983 struct abbrev_info *abbrev;
4984
4985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4986
4987 if (abbrev_number == 0)
4988 return NULL;
4989
4990 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4991 if (!abbrev)
4992 {
4993 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4994 abbrev_number, bfd_get_filename (abfd));
4995 }
4996
4997 return abbrev;
4998 }
4999
5000 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5001 Returns a pointer to the end of a series of DIEs, terminated by an empty
5002 DIE. Any children of the skipped DIEs will also be skipped. */
5003
5004 static gdb_byte *
5005 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5006 {
5007 struct dwarf2_cu *cu = reader->cu;
5008 struct abbrev_info *abbrev;
5009 unsigned int bytes_read;
5010
5011 while (1)
5012 {
5013 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5014 if (abbrev == NULL)
5015 return info_ptr + bytes_read;
5016 else
5017 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5018 }
5019 }
5020
5021 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5022 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5023 abbrev corresponding to that skipped uleb128 should be passed in
5024 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5025 children. */
5026
5027 static gdb_byte *
5028 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5029 struct abbrev_info *abbrev)
5030 {
5031 unsigned int bytes_read;
5032 struct attribute attr;
5033 bfd *abfd = reader->abfd;
5034 struct dwarf2_cu *cu = reader->cu;
5035 gdb_byte *buffer = reader->buffer;
5036 unsigned int form, i;
5037
5038 for (i = 0; i < abbrev->num_attrs; i++)
5039 {
5040 /* The only abbrev we care about is DW_AT_sibling. */
5041 if (abbrev->attrs[i].name == DW_AT_sibling)
5042 {
5043 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5044 if (attr.form == DW_FORM_ref_addr)
5045 complaint (&symfile_complaints,
5046 _("ignoring absolute DW_AT_sibling"));
5047 else
5048 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5049 }
5050
5051 /* If it isn't DW_AT_sibling, skip this attribute. */
5052 form = abbrev->attrs[i].form;
5053 skip_attribute:
5054 switch (form)
5055 {
5056 case DW_FORM_ref_addr:
5057 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5058 and later it is offset sized. */
5059 if (cu->header.version == 2)
5060 info_ptr += cu->header.addr_size;
5061 else
5062 info_ptr += cu->header.offset_size;
5063 break;
5064 case DW_FORM_addr:
5065 info_ptr += cu->header.addr_size;
5066 break;
5067 case DW_FORM_data1:
5068 case DW_FORM_ref1:
5069 case DW_FORM_flag:
5070 info_ptr += 1;
5071 break;
5072 case DW_FORM_flag_present:
5073 break;
5074 case DW_FORM_data2:
5075 case DW_FORM_ref2:
5076 info_ptr += 2;
5077 break;
5078 case DW_FORM_data4:
5079 case DW_FORM_ref4:
5080 info_ptr += 4;
5081 break;
5082 case DW_FORM_data8:
5083 case DW_FORM_ref8:
5084 case DW_FORM_ref_sig8:
5085 info_ptr += 8;
5086 break;
5087 case DW_FORM_string:
5088 read_direct_string (abfd, info_ptr, &bytes_read);
5089 info_ptr += bytes_read;
5090 break;
5091 case DW_FORM_sec_offset:
5092 case DW_FORM_strp:
5093 info_ptr += cu->header.offset_size;
5094 break;
5095 case DW_FORM_exprloc:
5096 case DW_FORM_block:
5097 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5098 info_ptr += bytes_read;
5099 break;
5100 case DW_FORM_block1:
5101 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5102 break;
5103 case DW_FORM_block2:
5104 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5105 break;
5106 case DW_FORM_block4:
5107 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5108 break;
5109 case DW_FORM_sdata:
5110 case DW_FORM_udata:
5111 case DW_FORM_ref_udata:
5112 case DW_FORM_GNU_addr_index:
5113 case DW_FORM_GNU_str_index:
5114 info_ptr = skip_leb128 (abfd, info_ptr);
5115 break;
5116 case DW_FORM_indirect:
5117 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5118 info_ptr += bytes_read;
5119 /* We need to continue parsing from here, so just go back to
5120 the top. */
5121 goto skip_attribute;
5122
5123 default:
5124 error (_("Dwarf Error: Cannot handle %s "
5125 "in DWARF reader [in module %s]"),
5126 dwarf_form_name (form),
5127 bfd_get_filename (abfd));
5128 }
5129 }
5130
5131 if (abbrev->has_children)
5132 return skip_children (reader, info_ptr);
5133 else
5134 return info_ptr;
5135 }
5136
5137 /* Locate ORIG_PDI's sibling.
5138 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5139
5140 static gdb_byte *
5141 locate_pdi_sibling (const struct die_reader_specs *reader,
5142 struct partial_die_info *orig_pdi,
5143 gdb_byte *info_ptr)
5144 {
5145 /* Do we know the sibling already? */
5146
5147 if (orig_pdi->sibling)
5148 return orig_pdi->sibling;
5149
5150 /* Are there any children to deal with? */
5151
5152 if (!orig_pdi->has_children)
5153 return info_ptr;
5154
5155 /* Skip the children the long way. */
5156
5157 return skip_children (reader, info_ptr);
5158 }
5159
5160 /* Expand this partial symbol table into a full symbol table. */
5161
5162 static void
5163 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5164 {
5165 if (pst != NULL)
5166 {
5167 if (pst->readin)
5168 {
5169 warning (_("bug: psymtab for %s is already read in."),
5170 pst->filename);
5171 }
5172 else
5173 {
5174 if (info_verbose)
5175 {
5176 printf_filtered (_("Reading in symbols for %s..."),
5177 pst->filename);
5178 gdb_flush (gdb_stdout);
5179 }
5180
5181 /* Restore our global data. */
5182 dwarf2_per_objfile = objfile_data (pst->objfile,
5183 dwarf2_objfile_data_key);
5184
5185 /* If this psymtab is constructed from a debug-only objfile, the
5186 has_section_at_zero flag will not necessarily be correct. We
5187 can get the correct value for this flag by looking at the data
5188 associated with the (presumably stripped) associated objfile. */
5189 if (pst->objfile->separate_debug_objfile_backlink)
5190 {
5191 struct dwarf2_per_objfile *dpo_backlink
5192 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5193 dwarf2_objfile_data_key);
5194
5195 dwarf2_per_objfile->has_section_at_zero
5196 = dpo_backlink->has_section_at_zero;
5197 }
5198
5199 dwarf2_per_objfile->reading_partial_symbols = 0;
5200
5201 psymtab_to_symtab_1 (pst);
5202
5203 /* Finish up the debug error message. */
5204 if (info_verbose)
5205 printf_filtered (_("done.\n"));
5206 }
5207 }
5208
5209 process_cu_includes ();
5210 }
5211 \f
5212 /* Reading in full CUs. */
5213
5214 /* Add PER_CU to the queue. */
5215
5216 static void
5217 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5218 enum language pretend_language)
5219 {
5220 struct dwarf2_queue_item *item;
5221
5222 per_cu->queued = 1;
5223 item = xmalloc (sizeof (*item));
5224 item->per_cu = per_cu;
5225 item->pretend_language = pretend_language;
5226 item->next = NULL;
5227
5228 if (dwarf2_queue == NULL)
5229 dwarf2_queue = item;
5230 else
5231 dwarf2_queue_tail->next = item;
5232
5233 dwarf2_queue_tail = item;
5234 }
5235
5236 /* Process the queue. */
5237
5238 static void
5239 process_queue (void)
5240 {
5241 struct dwarf2_queue_item *item, *next_item;
5242
5243 /* The queue starts out with one item, but following a DIE reference
5244 may load a new CU, adding it to the end of the queue. */
5245 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5246 {
5247 if (dwarf2_per_objfile->using_index
5248 ? !item->per_cu->v.quick->symtab
5249 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5250 process_full_comp_unit (item->per_cu, item->pretend_language);
5251
5252 item->per_cu->queued = 0;
5253 next_item = item->next;
5254 xfree (item);
5255 }
5256
5257 dwarf2_queue_tail = NULL;
5258 }
5259
5260 /* Free all allocated queue entries. This function only releases anything if
5261 an error was thrown; if the queue was processed then it would have been
5262 freed as we went along. */
5263
5264 static void
5265 dwarf2_release_queue (void *dummy)
5266 {
5267 struct dwarf2_queue_item *item, *last;
5268
5269 item = dwarf2_queue;
5270 while (item)
5271 {
5272 /* Anything still marked queued is likely to be in an
5273 inconsistent state, so discard it. */
5274 if (item->per_cu->queued)
5275 {
5276 if (item->per_cu->cu != NULL)
5277 free_one_cached_comp_unit (item->per_cu);
5278 item->per_cu->queued = 0;
5279 }
5280
5281 last = item;
5282 item = item->next;
5283 xfree (last);
5284 }
5285
5286 dwarf2_queue = dwarf2_queue_tail = NULL;
5287 }
5288
5289 /* Read in full symbols for PST, and anything it depends on. */
5290
5291 static void
5292 psymtab_to_symtab_1 (struct partial_symtab *pst)
5293 {
5294 struct dwarf2_per_cu_data *per_cu;
5295 struct cleanup *back_to;
5296 int i;
5297
5298 if (pst->readin)
5299 return;
5300
5301 for (i = 0; i < pst->number_of_dependencies; i++)
5302 if (!pst->dependencies[i]->readin
5303 && pst->dependencies[i]->user == NULL)
5304 {
5305 /* Inform about additional files that need to be read in. */
5306 if (info_verbose)
5307 {
5308 /* FIXME: i18n: Need to make this a single string. */
5309 fputs_filtered (" ", gdb_stdout);
5310 wrap_here ("");
5311 fputs_filtered ("and ", gdb_stdout);
5312 wrap_here ("");
5313 printf_filtered ("%s...", pst->dependencies[i]->filename);
5314 wrap_here (""); /* Flush output. */
5315 gdb_flush (gdb_stdout);
5316 }
5317 psymtab_to_symtab_1 (pst->dependencies[i]);
5318 }
5319
5320 per_cu = pst->read_symtab_private;
5321
5322 if (per_cu == NULL)
5323 {
5324 /* It's an include file, no symbols to read for it.
5325 Everything is in the parent symtab. */
5326 pst->readin = 1;
5327 return;
5328 }
5329
5330 dw2_do_instantiate_symtab (per_cu);
5331 }
5332
5333 /* Trivial hash function for die_info: the hash value of a DIE
5334 is its offset in .debug_info for this objfile. */
5335
5336 static hashval_t
5337 die_hash (const void *item)
5338 {
5339 const struct die_info *die = item;
5340
5341 return die->offset.sect_off;
5342 }
5343
5344 /* Trivial comparison function for die_info structures: two DIEs
5345 are equal if they have the same offset. */
5346
5347 static int
5348 die_eq (const void *item_lhs, const void *item_rhs)
5349 {
5350 const struct die_info *die_lhs = item_lhs;
5351 const struct die_info *die_rhs = item_rhs;
5352
5353 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5354 }
5355
5356 /* die_reader_func for load_full_comp_unit.
5357 This is identical to read_signatured_type_reader,
5358 but is kept separate for now. */
5359
5360 static void
5361 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5362 gdb_byte *info_ptr,
5363 struct die_info *comp_unit_die,
5364 int has_children,
5365 void *data)
5366 {
5367 struct dwarf2_cu *cu = reader->cu;
5368 struct attribute *attr;
5369 enum language *language_ptr = data;
5370
5371 gdb_assert (cu->die_hash == NULL);
5372 cu->die_hash =
5373 htab_create_alloc_ex (cu->header.length / 12,
5374 die_hash,
5375 die_eq,
5376 NULL,
5377 &cu->comp_unit_obstack,
5378 hashtab_obstack_allocate,
5379 dummy_obstack_deallocate);
5380
5381 if (has_children)
5382 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5383 &info_ptr, comp_unit_die);
5384 cu->dies = comp_unit_die;
5385 /* comp_unit_die is not stored in die_hash, no need. */
5386
5387 /* We try not to read any attributes in this function, because not
5388 all CUs needed for references have been loaded yet, and symbol
5389 table processing isn't initialized. But we have to set the CU language,
5390 or we won't be able to build types correctly.
5391 Similarly, if we do not read the producer, we can not apply
5392 producer-specific interpretation. */
5393 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5394 }
5395
5396 /* Load the DIEs associated with PER_CU into memory. */
5397
5398 static void
5399 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5400 enum language pretend_language)
5401 {
5402 gdb_assert (! this_cu->is_debug_types);
5403
5404 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5405 &pretend_language);
5406 }
5407
5408 /* Add a DIE to the delayed physname list. */
5409
5410 static void
5411 add_to_method_list (struct type *type, int fnfield_index, int index,
5412 const char *name, struct die_info *die,
5413 struct dwarf2_cu *cu)
5414 {
5415 struct delayed_method_info mi;
5416 mi.type = type;
5417 mi.fnfield_index = fnfield_index;
5418 mi.index = index;
5419 mi.name = name;
5420 mi.die = die;
5421 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5422 }
5423
5424 /* A cleanup for freeing the delayed method list. */
5425
5426 static void
5427 free_delayed_list (void *ptr)
5428 {
5429 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5430 if (cu->method_list != NULL)
5431 {
5432 VEC_free (delayed_method_info, cu->method_list);
5433 cu->method_list = NULL;
5434 }
5435 }
5436
5437 /* Compute the physnames of any methods on the CU's method list.
5438
5439 The computation of method physnames is delayed in order to avoid the
5440 (bad) condition that one of the method's formal parameters is of an as yet
5441 incomplete type. */
5442
5443 static void
5444 compute_delayed_physnames (struct dwarf2_cu *cu)
5445 {
5446 int i;
5447 struct delayed_method_info *mi;
5448 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5449 {
5450 const char *physname;
5451 struct fn_fieldlist *fn_flp
5452 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5453 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5454 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5455 }
5456 }
5457
5458 /* Go objects should be embedded in a DW_TAG_module DIE,
5459 and it's not clear if/how imported objects will appear.
5460 To keep Go support simple until that's worked out,
5461 go back through what we've read and create something usable.
5462 We could do this while processing each DIE, and feels kinda cleaner,
5463 but that way is more invasive.
5464 This is to, for example, allow the user to type "p var" or "b main"
5465 without having to specify the package name, and allow lookups
5466 of module.object to work in contexts that use the expression
5467 parser. */
5468
5469 static void
5470 fixup_go_packaging (struct dwarf2_cu *cu)
5471 {
5472 char *package_name = NULL;
5473 struct pending *list;
5474 int i;
5475
5476 for (list = global_symbols; list != NULL; list = list->next)
5477 {
5478 for (i = 0; i < list->nsyms; ++i)
5479 {
5480 struct symbol *sym = list->symbol[i];
5481
5482 if (SYMBOL_LANGUAGE (sym) == language_go
5483 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5484 {
5485 char *this_package_name = go_symbol_package_name (sym);
5486
5487 if (this_package_name == NULL)
5488 continue;
5489 if (package_name == NULL)
5490 package_name = this_package_name;
5491 else
5492 {
5493 if (strcmp (package_name, this_package_name) != 0)
5494 complaint (&symfile_complaints,
5495 _("Symtab %s has objects from two different Go packages: %s and %s"),
5496 (sym->symtab && sym->symtab->filename
5497 ? sym->symtab->filename
5498 : cu->objfile->name),
5499 this_package_name, package_name);
5500 xfree (this_package_name);
5501 }
5502 }
5503 }
5504 }
5505
5506 if (package_name != NULL)
5507 {
5508 struct objfile *objfile = cu->objfile;
5509 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5510 package_name, objfile);
5511 struct symbol *sym;
5512
5513 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5514
5515 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5516 SYMBOL_SET_LANGUAGE (sym, language_go);
5517 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5518 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5519 e.g., "main" finds the "main" module and not C's main(). */
5520 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5521 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5522 SYMBOL_TYPE (sym) = type;
5523
5524 add_symbol_to_list (sym, &global_symbols);
5525
5526 xfree (package_name);
5527 }
5528 }
5529
5530 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5531
5532 /* Return the symtab for PER_CU. This works properly regardless of
5533 whether we're using the index or psymtabs. */
5534
5535 static struct symtab *
5536 get_symtab (struct dwarf2_per_cu_data *per_cu)
5537 {
5538 return (dwarf2_per_objfile->using_index
5539 ? per_cu->v.quick->symtab
5540 : per_cu->v.psymtab->symtab);
5541 }
5542
5543 /* A helper function for computing the list of all symbol tables
5544 included by PER_CU. */
5545
5546 static void
5547 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5548 htab_t all_children,
5549 struct dwarf2_per_cu_data *per_cu)
5550 {
5551 void **slot;
5552 int ix;
5553 struct dwarf2_per_cu_data *iter;
5554
5555 slot = htab_find_slot (all_children, per_cu, INSERT);
5556 if (*slot != NULL)
5557 {
5558 /* This inclusion and its children have been processed. */
5559 return;
5560 }
5561
5562 *slot = per_cu;
5563 /* Only add a CU if it has a symbol table. */
5564 if (get_symtab (per_cu) != NULL)
5565 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5566
5567 for (ix = 0;
5568 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5569 ++ix)
5570 recursively_compute_inclusions (result, all_children, iter);
5571 }
5572
5573 /* Compute the symtab 'includes' fields for the symtab related to
5574 PER_CU. */
5575
5576 static void
5577 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5578 {
5579 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5580 {
5581 int ix, len;
5582 struct dwarf2_per_cu_data *iter;
5583 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5584 htab_t all_children;
5585 struct symtab *symtab = get_symtab (per_cu);
5586
5587 /* If we don't have a symtab, we can just skip this case. */
5588 if (symtab == NULL)
5589 return;
5590
5591 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5592 NULL, xcalloc, xfree);
5593
5594 for (ix = 0;
5595 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5596 ix, iter);
5597 ++ix)
5598 recursively_compute_inclusions (&result_children, all_children, iter);
5599
5600 /* Now we have a transitive closure of all the included CUs, so
5601 we can convert it to a list of symtabs. */
5602 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5603 symtab->includes
5604 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5605 (len + 1) * sizeof (struct symtab *));
5606 for (ix = 0;
5607 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5608 ++ix)
5609 symtab->includes[ix] = get_symtab (iter);
5610 symtab->includes[len] = NULL;
5611
5612 VEC_free (dwarf2_per_cu_ptr, result_children);
5613 htab_delete (all_children);
5614 }
5615 }
5616
5617 /* Compute the 'includes' field for the symtabs of all the CUs we just
5618 read. */
5619
5620 static void
5621 process_cu_includes (void)
5622 {
5623 int ix;
5624 struct dwarf2_per_cu_data *iter;
5625
5626 for (ix = 0;
5627 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5628 ix, iter);
5629 ++ix)
5630 compute_symtab_includes (iter);
5631
5632 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5633 }
5634
5635 /* Generate full symbol information for PER_CU, whose DIEs have
5636 already been loaded into memory. */
5637
5638 static void
5639 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5640 enum language pretend_language)
5641 {
5642 struct dwarf2_cu *cu = per_cu->cu;
5643 struct objfile *objfile = per_cu->objfile;
5644 CORE_ADDR lowpc, highpc;
5645 struct symtab *symtab;
5646 struct cleanup *back_to, *delayed_list_cleanup;
5647 CORE_ADDR baseaddr;
5648
5649 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5650
5651 buildsym_init ();
5652 back_to = make_cleanup (really_free_pendings, NULL);
5653 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5654
5655 cu->list_in_scope = &file_symbols;
5656
5657 cu->language = pretend_language;
5658 cu->language_defn = language_def (cu->language);
5659
5660 /* Do line number decoding in read_file_scope () */
5661 process_die (cu->dies, cu);
5662
5663 /* For now fudge the Go package. */
5664 if (cu->language == language_go)
5665 fixup_go_packaging (cu);
5666
5667 /* Now that we have processed all the DIEs in the CU, all the types
5668 should be complete, and it should now be safe to compute all of the
5669 physnames. */
5670 compute_delayed_physnames (cu);
5671 do_cleanups (delayed_list_cleanup);
5672
5673 /* Some compilers don't define a DW_AT_high_pc attribute for the
5674 compilation unit. If the DW_AT_high_pc is missing, synthesize
5675 it, by scanning the DIE's below the compilation unit. */
5676 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5677
5678 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5679
5680 if (symtab != NULL)
5681 {
5682 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5683
5684 /* Set symtab language to language from DW_AT_language. If the
5685 compilation is from a C file generated by language preprocessors, do
5686 not set the language if it was already deduced by start_subfile. */
5687 if (!(cu->language == language_c && symtab->language != language_c))
5688 symtab->language = cu->language;
5689
5690 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5691 produce DW_AT_location with location lists but it can be possibly
5692 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5693 there were bugs in prologue debug info, fixed later in GCC-4.5
5694 by "unwind info for epilogues" patch (which is not directly related).
5695
5696 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5697 needed, it would be wrong due to missing DW_AT_producer there.
5698
5699 Still one can confuse GDB by using non-standard GCC compilation
5700 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5701 */
5702 if (cu->has_loclist && gcc_4_minor >= 5)
5703 symtab->locations_valid = 1;
5704
5705 if (gcc_4_minor >= 5)
5706 symtab->epilogue_unwind_valid = 1;
5707
5708 symtab->call_site_htab = cu->call_site_htab;
5709 }
5710
5711 if (dwarf2_per_objfile->using_index)
5712 per_cu->v.quick->symtab = symtab;
5713 else
5714 {
5715 struct partial_symtab *pst = per_cu->v.psymtab;
5716 pst->symtab = symtab;
5717 pst->readin = 1;
5718 }
5719
5720 /* Push it for inclusion processing later. */
5721 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5722
5723 do_cleanups (back_to);
5724 }
5725
5726 /* Process an imported unit DIE. */
5727
5728 static void
5729 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5730 {
5731 struct attribute *attr;
5732
5733 attr = dwarf2_attr (die, DW_AT_import, cu);
5734 if (attr != NULL)
5735 {
5736 struct dwarf2_per_cu_data *per_cu;
5737 struct symtab *imported_symtab;
5738 sect_offset offset;
5739
5740 offset = dwarf2_get_ref_die_offset (attr);
5741 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5742
5743 /* Queue the unit, if needed. */
5744 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5745 load_full_comp_unit (per_cu, cu->language);
5746
5747 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5748 per_cu);
5749 }
5750 }
5751
5752 /* Process a die and its children. */
5753
5754 static void
5755 process_die (struct die_info *die, struct dwarf2_cu *cu)
5756 {
5757 switch (die->tag)
5758 {
5759 case DW_TAG_padding:
5760 break;
5761 case DW_TAG_compile_unit:
5762 case DW_TAG_partial_unit:
5763 read_file_scope (die, cu);
5764 break;
5765 case DW_TAG_type_unit:
5766 read_type_unit_scope (die, cu);
5767 break;
5768 case DW_TAG_subprogram:
5769 case DW_TAG_inlined_subroutine:
5770 read_func_scope (die, cu);
5771 break;
5772 case DW_TAG_lexical_block:
5773 case DW_TAG_try_block:
5774 case DW_TAG_catch_block:
5775 read_lexical_block_scope (die, cu);
5776 break;
5777 case DW_TAG_GNU_call_site:
5778 read_call_site_scope (die, cu);
5779 break;
5780 case DW_TAG_class_type:
5781 case DW_TAG_interface_type:
5782 case DW_TAG_structure_type:
5783 case DW_TAG_union_type:
5784 process_structure_scope (die, cu);
5785 break;
5786 case DW_TAG_enumeration_type:
5787 process_enumeration_scope (die, cu);
5788 break;
5789
5790 /* These dies have a type, but processing them does not create
5791 a symbol or recurse to process the children. Therefore we can
5792 read them on-demand through read_type_die. */
5793 case DW_TAG_subroutine_type:
5794 case DW_TAG_set_type:
5795 case DW_TAG_array_type:
5796 case DW_TAG_pointer_type:
5797 case DW_TAG_ptr_to_member_type:
5798 case DW_TAG_reference_type:
5799 case DW_TAG_string_type:
5800 break;
5801
5802 case DW_TAG_base_type:
5803 case DW_TAG_subrange_type:
5804 case DW_TAG_typedef:
5805 /* Add a typedef symbol for the type definition, if it has a
5806 DW_AT_name. */
5807 new_symbol (die, read_type_die (die, cu), cu);
5808 break;
5809 case DW_TAG_common_block:
5810 read_common_block (die, cu);
5811 break;
5812 case DW_TAG_common_inclusion:
5813 break;
5814 case DW_TAG_namespace:
5815 processing_has_namespace_info = 1;
5816 read_namespace (die, cu);
5817 break;
5818 case DW_TAG_module:
5819 processing_has_namespace_info = 1;
5820 read_module (die, cu);
5821 break;
5822 case DW_TAG_imported_declaration:
5823 case DW_TAG_imported_module:
5824 processing_has_namespace_info = 1;
5825 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5826 || cu->language != language_fortran))
5827 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5828 dwarf_tag_name (die->tag));
5829 read_import_statement (die, cu);
5830 break;
5831
5832 case DW_TAG_imported_unit:
5833 process_imported_unit_die (die, cu);
5834 break;
5835
5836 default:
5837 new_symbol (die, NULL, cu);
5838 break;
5839 }
5840 }
5841
5842 /* A helper function for dwarf2_compute_name which determines whether DIE
5843 needs to have the name of the scope prepended to the name listed in the
5844 die. */
5845
5846 static int
5847 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5848 {
5849 struct attribute *attr;
5850
5851 switch (die->tag)
5852 {
5853 case DW_TAG_namespace:
5854 case DW_TAG_typedef:
5855 case DW_TAG_class_type:
5856 case DW_TAG_interface_type:
5857 case DW_TAG_structure_type:
5858 case DW_TAG_union_type:
5859 case DW_TAG_enumeration_type:
5860 case DW_TAG_enumerator:
5861 case DW_TAG_subprogram:
5862 case DW_TAG_member:
5863 return 1;
5864
5865 case DW_TAG_variable:
5866 case DW_TAG_constant:
5867 /* We only need to prefix "globally" visible variables. These include
5868 any variable marked with DW_AT_external or any variable that
5869 lives in a namespace. [Variables in anonymous namespaces
5870 require prefixing, but they are not DW_AT_external.] */
5871
5872 if (dwarf2_attr (die, DW_AT_specification, cu))
5873 {
5874 struct dwarf2_cu *spec_cu = cu;
5875
5876 return die_needs_namespace (die_specification (die, &spec_cu),
5877 spec_cu);
5878 }
5879
5880 attr = dwarf2_attr (die, DW_AT_external, cu);
5881 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5882 && die->parent->tag != DW_TAG_module)
5883 return 0;
5884 /* A variable in a lexical block of some kind does not need a
5885 namespace, even though in C++ such variables may be external
5886 and have a mangled name. */
5887 if (die->parent->tag == DW_TAG_lexical_block
5888 || die->parent->tag == DW_TAG_try_block
5889 || die->parent->tag == DW_TAG_catch_block
5890 || die->parent->tag == DW_TAG_subprogram)
5891 return 0;
5892 return 1;
5893
5894 default:
5895 return 0;
5896 }
5897 }
5898
5899 /* Retrieve the last character from a mem_file. */
5900
5901 static void
5902 do_ui_file_peek_last (void *object, const char *buffer, long length)
5903 {
5904 char *last_char_p = (char *) object;
5905
5906 if (length > 0)
5907 *last_char_p = buffer[length - 1];
5908 }
5909
5910 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5911 compute the physname for the object, which include a method's:
5912 - formal parameters (C++/Java),
5913 - receiver type (Go),
5914 - return type (Java).
5915
5916 The term "physname" is a bit confusing.
5917 For C++, for example, it is the demangled name.
5918 For Go, for example, it's the mangled name.
5919
5920 For Ada, return the DIE's linkage name rather than the fully qualified
5921 name. PHYSNAME is ignored..
5922
5923 The result is allocated on the objfile_obstack and canonicalized. */
5924
5925 static const char *
5926 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5927 int physname)
5928 {
5929 struct objfile *objfile = cu->objfile;
5930
5931 if (name == NULL)
5932 name = dwarf2_name (die, cu);
5933
5934 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5935 compute it by typename_concat inside GDB. */
5936 if (cu->language == language_ada
5937 || (cu->language == language_fortran && physname))
5938 {
5939 /* For Ada unit, we prefer the linkage name over the name, as
5940 the former contains the exported name, which the user expects
5941 to be able to reference. Ideally, we want the user to be able
5942 to reference this entity using either natural or linkage name,
5943 but we haven't started looking at this enhancement yet. */
5944 struct attribute *attr;
5945
5946 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5947 if (attr == NULL)
5948 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5949 if (attr && DW_STRING (attr))
5950 return DW_STRING (attr);
5951 }
5952
5953 /* These are the only languages we know how to qualify names in. */
5954 if (name != NULL
5955 && (cu->language == language_cplus || cu->language == language_java
5956 || cu->language == language_fortran))
5957 {
5958 if (die_needs_namespace (die, cu))
5959 {
5960 long length;
5961 const char *prefix;
5962 struct ui_file *buf;
5963
5964 prefix = determine_prefix (die, cu);
5965 buf = mem_fileopen ();
5966 if (*prefix != '\0')
5967 {
5968 char *prefixed_name = typename_concat (NULL, prefix, name,
5969 physname, cu);
5970
5971 fputs_unfiltered (prefixed_name, buf);
5972 xfree (prefixed_name);
5973 }
5974 else
5975 fputs_unfiltered (name, buf);
5976
5977 /* Template parameters may be specified in the DIE's DW_AT_name, or
5978 as children with DW_TAG_template_type_param or
5979 DW_TAG_value_type_param. If the latter, add them to the name
5980 here. If the name already has template parameters, then
5981 skip this step; some versions of GCC emit both, and
5982 it is more efficient to use the pre-computed name.
5983
5984 Something to keep in mind about this process: it is very
5985 unlikely, or in some cases downright impossible, to produce
5986 something that will match the mangled name of a function.
5987 If the definition of the function has the same debug info,
5988 we should be able to match up with it anyway. But fallbacks
5989 using the minimal symbol, for instance to find a method
5990 implemented in a stripped copy of libstdc++, will not work.
5991 If we do not have debug info for the definition, we will have to
5992 match them up some other way.
5993
5994 When we do name matching there is a related problem with function
5995 templates; two instantiated function templates are allowed to
5996 differ only by their return types, which we do not add here. */
5997
5998 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5999 {
6000 struct attribute *attr;
6001 struct die_info *child;
6002 int first = 1;
6003
6004 die->building_fullname = 1;
6005
6006 for (child = die->child; child != NULL; child = child->sibling)
6007 {
6008 struct type *type;
6009 LONGEST value;
6010 gdb_byte *bytes;
6011 struct dwarf2_locexpr_baton *baton;
6012 struct value *v;
6013
6014 if (child->tag != DW_TAG_template_type_param
6015 && child->tag != DW_TAG_template_value_param)
6016 continue;
6017
6018 if (first)
6019 {
6020 fputs_unfiltered ("<", buf);
6021 first = 0;
6022 }
6023 else
6024 fputs_unfiltered (", ", buf);
6025
6026 attr = dwarf2_attr (child, DW_AT_type, cu);
6027 if (attr == NULL)
6028 {
6029 complaint (&symfile_complaints,
6030 _("template parameter missing DW_AT_type"));
6031 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6032 continue;
6033 }
6034 type = die_type (child, cu);
6035
6036 if (child->tag == DW_TAG_template_type_param)
6037 {
6038 c_print_type (type, "", buf, -1, 0);
6039 continue;
6040 }
6041
6042 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6043 if (attr == NULL)
6044 {
6045 complaint (&symfile_complaints,
6046 _("template parameter missing "
6047 "DW_AT_const_value"));
6048 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6049 continue;
6050 }
6051
6052 dwarf2_const_value_attr (attr, type, name,
6053 &cu->comp_unit_obstack, cu,
6054 &value, &bytes, &baton);
6055
6056 if (TYPE_NOSIGN (type))
6057 /* GDB prints characters as NUMBER 'CHAR'. If that's
6058 changed, this can use value_print instead. */
6059 c_printchar (value, type, buf);
6060 else
6061 {
6062 struct value_print_options opts;
6063
6064 if (baton != NULL)
6065 v = dwarf2_evaluate_loc_desc (type, NULL,
6066 baton->data,
6067 baton->size,
6068 baton->per_cu);
6069 else if (bytes != NULL)
6070 {
6071 v = allocate_value (type);
6072 memcpy (value_contents_writeable (v), bytes,
6073 TYPE_LENGTH (type));
6074 }
6075 else
6076 v = value_from_longest (type, value);
6077
6078 /* Specify decimal so that we do not depend on
6079 the radix. */
6080 get_formatted_print_options (&opts, 'd');
6081 opts.raw = 1;
6082 value_print (v, buf, &opts);
6083 release_value (v);
6084 value_free (v);
6085 }
6086 }
6087
6088 die->building_fullname = 0;
6089
6090 if (!first)
6091 {
6092 /* Close the argument list, with a space if necessary
6093 (nested templates). */
6094 char last_char = '\0';
6095 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6096 if (last_char == '>')
6097 fputs_unfiltered (" >", buf);
6098 else
6099 fputs_unfiltered (">", buf);
6100 }
6101 }
6102
6103 /* For Java and C++ methods, append formal parameter type
6104 information, if PHYSNAME. */
6105
6106 if (physname && die->tag == DW_TAG_subprogram
6107 && (cu->language == language_cplus
6108 || cu->language == language_java))
6109 {
6110 struct type *type = read_type_die (die, cu);
6111
6112 c_type_print_args (type, buf, 1, cu->language);
6113
6114 if (cu->language == language_java)
6115 {
6116 /* For java, we must append the return type to method
6117 names. */
6118 if (die->tag == DW_TAG_subprogram)
6119 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6120 0, 0);
6121 }
6122 else if (cu->language == language_cplus)
6123 {
6124 /* Assume that an artificial first parameter is
6125 "this", but do not crash if it is not. RealView
6126 marks unnamed (and thus unused) parameters as
6127 artificial; there is no way to differentiate
6128 the two cases. */
6129 if (TYPE_NFIELDS (type) > 0
6130 && TYPE_FIELD_ARTIFICIAL (type, 0)
6131 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6132 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6133 0))))
6134 fputs_unfiltered (" const", buf);
6135 }
6136 }
6137
6138 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6139 &length);
6140 ui_file_delete (buf);
6141
6142 if (cu->language == language_cplus)
6143 {
6144 char *cname
6145 = dwarf2_canonicalize_name (name, cu,
6146 &objfile->objfile_obstack);
6147
6148 if (cname != NULL)
6149 name = cname;
6150 }
6151 }
6152 }
6153
6154 return name;
6155 }
6156
6157 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6158 If scope qualifiers are appropriate they will be added. The result
6159 will be allocated on the objfile_obstack, or NULL if the DIE does
6160 not have a name. NAME may either be from a previous call to
6161 dwarf2_name or NULL.
6162
6163 The output string will be canonicalized (if C++/Java). */
6164
6165 static const char *
6166 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6167 {
6168 return dwarf2_compute_name (name, die, cu, 0);
6169 }
6170
6171 /* Construct a physname for the given DIE in CU. NAME may either be
6172 from a previous call to dwarf2_name or NULL. The result will be
6173 allocated on the objfile_objstack or NULL if the DIE does not have a
6174 name.
6175
6176 The output string will be canonicalized (if C++/Java). */
6177
6178 static const char *
6179 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6180 {
6181 struct objfile *objfile = cu->objfile;
6182 struct attribute *attr;
6183 const char *retval, *mangled = NULL, *canon = NULL;
6184 struct cleanup *back_to;
6185 int need_copy = 1;
6186
6187 /* In this case dwarf2_compute_name is just a shortcut not building anything
6188 on its own. */
6189 if (!die_needs_namespace (die, cu))
6190 return dwarf2_compute_name (name, die, cu, 1);
6191
6192 back_to = make_cleanup (null_cleanup, NULL);
6193
6194 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6195 if (!attr)
6196 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6197
6198 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6199 has computed. */
6200 if (attr && DW_STRING (attr))
6201 {
6202 char *demangled;
6203
6204 mangled = DW_STRING (attr);
6205
6206 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6207 type. It is easier for GDB users to search for such functions as
6208 `name(params)' than `long name(params)'. In such case the minimal
6209 symbol names do not match the full symbol names but for template
6210 functions there is never a need to look up their definition from their
6211 declaration so the only disadvantage remains the minimal symbol
6212 variant `long name(params)' does not have the proper inferior type.
6213 */
6214
6215 if (cu->language == language_go)
6216 {
6217 /* This is a lie, but we already lie to the caller new_symbol_full.
6218 new_symbol_full assumes we return the mangled name.
6219 This just undoes that lie until things are cleaned up. */
6220 demangled = NULL;
6221 }
6222 else
6223 {
6224 demangled = cplus_demangle (mangled,
6225 (DMGL_PARAMS | DMGL_ANSI
6226 | (cu->language == language_java
6227 ? DMGL_JAVA | DMGL_RET_POSTFIX
6228 : DMGL_RET_DROP)));
6229 }
6230 if (demangled)
6231 {
6232 make_cleanup (xfree, demangled);
6233 canon = demangled;
6234 }
6235 else
6236 {
6237 canon = mangled;
6238 need_copy = 0;
6239 }
6240 }
6241
6242 if (canon == NULL || check_physname)
6243 {
6244 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6245
6246 if (canon != NULL && strcmp (physname, canon) != 0)
6247 {
6248 /* It may not mean a bug in GDB. The compiler could also
6249 compute DW_AT_linkage_name incorrectly. But in such case
6250 GDB would need to be bug-to-bug compatible. */
6251
6252 complaint (&symfile_complaints,
6253 _("Computed physname <%s> does not match demangled <%s> "
6254 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6255 physname, canon, mangled, die->offset.sect_off, objfile->name);
6256
6257 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6258 is available here - over computed PHYSNAME. It is safer
6259 against both buggy GDB and buggy compilers. */
6260
6261 retval = canon;
6262 }
6263 else
6264 {
6265 retval = physname;
6266 need_copy = 0;
6267 }
6268 }
6269 else
6270 retval = canon;
6271
6272 if (need_copy)
6273 retval = obsavestring (retval, strlen (retval),
6274 &objfile->objfile_obstack);
6275
6276 do_cleanups (back_to);
6277 return retval;
6278 }
6279
6280 /* Read the import statement specified by the given die and record it. */
6281
6282 static void
6283 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6284 {
6285 struct objfile *objfile = cu->objfile;
6286 struct attribute *import_attr;
6287 struct die_info *imported_die, *child_die;
6288 struct dwarf2_cu *imported_cu;
6289 const char *imported_name;
6290 const char *imported_name_prefix;
6291 const char *canonical_name;
6292 const char *import_alias;
6293 const char *imported_declaration = NULL;
6294 const char *import_prefix;
6295 VEC (const_char_ptr) *excludes = NULL;
6296 struct cleanup *cleanups;
6297
6298 char *temp;
6299
6300 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6301 if (import_attr == NULL)
6302 {
6303 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6304 dwarf_tag_name (die->tag));
6305 return;
6306 }
6307
6308 imported_cu = cu;
6309 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6310 imported_name = dwarf2_name (imported_die, imported_cu);
6311 if (imported_name == NULL)
6312 {
6313 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6314
6315 The import in the following code:
6316 namespace A
6317 {
6318 typedef int B;
6319 }
6320
6321 int main ()
6322 {
6323 using A::B;
6324 B b;
6325 return b;
6326 }
6327
6328 ...
6329 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6330 <52> DW_AT_decl_file : 1
6331 <53> DW_AT_decl_line : 6
6332 <54> DW_AT_import : <0x75>
6333 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6334 <59> DW_AT_name : B
6335 <5b> DW_AT_decl_file : 1
6336 <5c> DW_AT_decl_line : 2
6337 <5d> DW_AT_type : <0x6e>
6338 ...
6339 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6340 <76> DW_AT_byte_size : 4
6341 <77> DW_AT_encoding : 5 (signed)
6342
6343 imports the wrong die ( 0x75 instead of 0x58 ).
6344 This case will be ignored until the gcc bug is fixed. */
6345 return;
6346 }
6347
6348 /* Figure out the local name after import. */
6349 import_alias = dwarf2_name (die, cu);
6350
6351 /* Figure out where the statement is being imported to. */
6352 import_prefix = determine_prefix (die, cu);
6353
6354 /* Figure out what the scope of the imported die is and prepend it
6355 to the name of the imported die. */
6356 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6357
6358 if (imported_die->tag != DW_TAG_namespace
6359 && imported_die->tag != DW_TAG_module)
6360 {
6361 imported_declaration = imported_name;
6362 canonical_name = imported_name_prefix;
6363 }
6364 else if (strlen (imported_name_prefix) > 0)
6365 {
6366 temp = alloca (strlen (imported_name_prefix)
6367 + 2 + strlen (imported_name) + 1);
6368 strcpy (temp, imported_name_prefix);
6369 strcat (temp, "::");
6370 strcat (temp, imported_name);
6371 canonical_name = temp;
6372 }
6373 else
6374 canonical_name = imported_name;
6375
6376 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6377
6378 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6379 for (child_die = die->child; child_die && child_die->tag;
6380 child_die = sibling_die (child_die))
6381 {
6382 /* DWARF-4: A Fortran use statement with a “rename list” may be
6383 represented by an imported module entry with an import attribute
6384 referring to the module and owned entries corresponding to those
6385 entities that are renamed as part of being imported. */
6386
6387 if (child_die->tag != DW_TAG_imported_declaration)
6388 {
6389 complaint (&symfile_complaints,
6390 _("child DW_TAG_imported_declaration expected "
6391 "- DIE at 0x%x [in module %s]"),
6392 child_die->offset.sect_off, objfile->name);
6393 continue;
6394 }
6395
6396 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6397 if (import_attr == NULL)
6398 {
6399 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6400 dwarf_tag_name (child_die->tag));
6401 continue;
6402 }
6403
6404 imported_cu = cu;
6405 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6406 &imported_cu);
6407 imported_name = dwarf2_name (imported_die, imported_cu);
6408 if (imported_name == NULL)
6409 {
6410 complaint (&symfile_complaints,
6411 _("child DW_TAG_imported_declaration has unknown "
6412 "imported name - DIE at 0x%x [in module %s]"),
6413 child_die->offset.sect_off, objfile->name);
6414 continue;
6415 }
6416
6417 VEC_safe_push (const_char_ptr, excludes, imported_name);
6418
6419 process_die (child_die, cu);
6420 }
6421
6422 cp_add_using_directive (import_prefix,
6423 canonical_name,
6424 import_alias,
6425 imported_declaration,
6426 excludes,
6427 &objfile->objfile_obstack);
6428
6429 do_cleanups (cleanups);
6430 }
6431
6432 /* Cleanup function for read_file_scope. */
6433
6434 static void
6435 free_cu_line_header (void *arg)
6436 {
6437 struct dwarf2_cu *cu = arg;
6438
6439 free_line_header (cu->line_header);
6440 cu->line_header = NULL;
6441 }
6442
6443 static void
6444 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6445 char **name, char **comp_dir)
6446 {
6447 struct attribute *attr;
6448
6449 *name = NULL;
6450 *comp_dir = NULL;
6451
6452 /* Find the filename. Do not use dwarf2_name here, since the filename
6453 is not a source language identifier. */
6454 attr = dwarf2_attr (die, DW_AT_name, cu);
6455 if (attr)
6456 {
6457 *name = DW_STRING (attr);
6458 }
6459
6460 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6461 if (attr)
6462 *comp_dir = DW_STRING (attr);
6463 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6464 {
6465 *comp_dir = ldirname (*name);
6466 if (*comp_dir != NULL)
6467 make_cleanup (xfree, *comp_dir);
6468 }
6469 if (*comp_dir != NULL)
6470 {
6471 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6472 directory, get rid of it. */
6473 char *cp = strchr (*comp_dir, ':');
6474
6475 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6476 *comp_dir = cp + 1;
6477 }
6478
6479 if (*name == NULL)
6480 *name = "<unknown>";
6481 }
6482
6483 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6484 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6485 COMP_DIR is the compilation directory.
6486 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6487
6488 static void
6489 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6490 const char *comp_dir, int want_line_info)
6491 {
6492 struct attribute *attr;
6493
6494 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6495 if (attr)
6496 {
6497 unsigned int line_offset = DW_UNSND (attr);
6498 struct line_header *line_header
6499 = dwarf_decode_line_header (line_offset, cu);
6500
6501 if (line_header)
6502 {
6503 cu->line_header = line_header;
6504 make_cleanup (free_cu_line_header, cu);
6505 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6506 }
6507 }
6508 }
6509
6510 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6511
6512 static void
6513 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6514 {
6515 struct objfile *objfile = dwarf2_per_objfile->objfile;
6516 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6517 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6518 CORE_ADDR highpc = ((CORE_ADDR) 0);
6519 struct attribute *attr;
6520 char *name = NULL;
6521 char *comp_dir = NULL;
6522 struct die_info *child_die;
6523 bfd *abfd = objfile->obfd;
6524 CORE_ADDR baseaddr;
6525
6526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6527
6528 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6529
6530 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6531 from finish_block. */
6532 if (lowpc == ((CORE_ADDR) -1))
6533 lowpc = highpc;
6534 lowpc += baseaddr;
6535 highpc += baseaddr;
6536
6537 find_file_and_directory (die, cu, &name, &comp_dir);
6538
6539 prepare_one_comp_unit (cu, die, cu->language);
6540
6541 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6542 standardised yet. As a workaround for the language detection we fall
6543 back to the DW_AT_producer string. */
6544 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6545 cu->language = language_opencl;
6546
6547 /* Similar hack for Go. */
6548 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6549 set_cu_language (DW_LANG_Go, cu);
6550
6551 /* We assume that we're processing GCC output. */
6552 processing_gcc_compilation = 2;
6553
6554 processing_has_namespace_info = 0;
6555
6556 start_symtab (name, comp_dir, lowpc);
6557 record_debugformat ("DWARF 2");
6558 record_producer (cu->producer);
6559
6560 /* Decode line number information if present. We do this before
6561 processing child DIEs, so that the line header table is available
6562 for DW_AT_decl_file. */
6563 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6564
6565 /* Process all dies in compilation unit. */
6566 if (die->child != NULL)
6567 {
6568 child_die = die->child;
6569 while (child_die && child_die->tag)
6570 {
6571 process_die (child_die, cu);
6572 child_die = sibling_die (child_die);
6573 }
6574 }
6575
6576 /* Decode macro information, if present. Dwarf 2 macro information
6577 refers to information in the line number info statement program
6578 header, so we can only read it if we've read the header
6579 successfully. */
6580 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6581 if (attr && cu->line_header)
6582 {
6583 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6584 complaint (&symfile_complaints,
6585 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6586
6587 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6588 comp_dir, abfd, cu,
6589 &dwarf2_per_objfile->macro, 1,
6590 ".debug_macro");
6591 }
6592 else
6593 {
6594 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6595 if (attr && cu->line_header)
6596 {
6597 unsigned int macro_offset = DW_UNSND (attr);
6598
6599 dwarf_decode_macros (cu->line_header, macro_offset,
6600 comp_dir, abfd, cu,
6601 &dwarf2_per_objfile->macinfo, 0,
6602 ".debug_macinfo");
6603 }
6604 }
6605
6606 do_cleanups (back_to);
6607 }
6608
6609 /* Process DW_TAG_type_unit.
6610 For TUs we want to skip the first top level sibling if it's not the
6611 actual type being defined by this TU. In this case the first top
6612 level sibling is there to provide context only. */
6613
6614 static void
6615 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6616 {
6617 struct objfile *objfile = cu->objfile;
6618 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6619 CORE_ADDR lowpc;
6620 struct attribute *attr;
6621 char *name = NULL;
6622 char *comp_dir = NULL;
6623 struct die_info *child_die;
6624 bfd *abfd = objfile->obfd;
6625
6626 /* start_symtab needs a low pc, but we don't really have one.
6627 Do what read_file_scope would do in the absence of such info. */
6628 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6629
6630 /* Find the filename. Do not use dwarf2_name here, since the filename
6631 is not a source language identifier. */
6632 attr = dwarf2_attr (die, DW_AT_name, cu);
6633 if (attr)
6634 name = DW_STRING (attr);
6635
6636 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6637 if (attr)
6638 comp_dir = DW_STRING (attr);
6639 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6640 {
6641 comp_dir = ldirname (name);
6642 if (comp_dir != NULL)
6643 make_cleanup (xfree, comp_dir);
6644 }
6645
6646 if (name == NULL)
6647 name = "<unknown>";
6648
6649 prepare_one_comp_unit (cu, die, language_minimal);
6650
6651 /* We assume that we're processing GCC output. */
6652 processing_gcc_compilation = 2;
6653
6654 processing_has_namespace_info = 0;
6655
6656 start_symtab (name, comp_dir, lowpc);
6657 record_debugformat ("DWARF 2");
6658 record_producer (cu->producer);
6659
6660 /* Decode line number information if present. We do this before
6661 processing child DIEs, so that the line header table is available
6662 for DW_AT_decl_file.
6663 We don't need the pc/line-number mapping for type units. */
6664 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6665
6666 /* Process the dies in the type unit. */
6667 if (die->child == NULL)
6668 {
6669 dump_die_for_error (die);
6670 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6671 bfd_get_filename (abfd));
6672 }
6673
6674 child_die = die->child;
6675
6676 while (child_die && child_die->tag)
6677 {
6678 process_die (child_die, cu);
6679
6680 child_die = sibling_die (child_die);
6681 }
6682
6683 do_cleanups (back_to);
6684 }
6685 \f
6686 /* DWO files. */
6687
6688 static hashval_t
6689 hash_dwo_file (const void *item)
6690 {
6691 const struct dwo_file *dwo_file = item;
6692
6693 return htab_hash_string (dwo_file->dwo_name);
6694 }
6695
6696 static int
6697 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6698 {
6699 const struct dwo_file *lhs = item_lhs;
6700 const struct dwo_file *rhs = item_rhs;
6701
6702 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6703 }
6704
6705 /* Allocate a hash table for DWO files. */
6706
6707 static htab_t
6708 allocate_dwo_file_hash_table (void)
6709 {
6710 struct objfile *objfile = dwarf2_per_objfile->objfile;
6711
6712 return htab_create_alloc_ex (41,
6713 hash_dwo_file,
6714 eq_dwo_file,
6715 NULL,
6716 &objfile->objfile_obstack,
6717 hashtab_obstack_allocate,
6718 dummy_obstack_deallocate);
6719 }
6720
6721 static hashval_t
6722 hash_dwo_unit (const void *item)
6723 {
6724 const struct dwo_unit *dwo_unit = item;
6725
6726 /* This drops the top 32 bits of the id, but is ok for a hash. */
6727 return dwo_unit->signature;
6728 }
6729
6730 static int
6731 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6732 {
6733 const struct dwo_unit *lhs = item_lhs;
6734 const struct dwo_unit *rhs = item_rhs;
6735
6736 /* The signature is assumed to be unique within the DWO file.
6737 So while object file CU dwo_id's always have the value zero,
6738 that's OK, assuming each object file DWO file has only one CU,
6739 and that's the rule for now. */
6740 return lhs->signature == rhs->signature;
6741 }
6742
6743 /* Allocate a hash table for DWO CUs,TUs.
6744 There is one of these tables for each of CUs,TUs for each DWO file. */
6745
6746 static htab_t
6747 allocate_dwo_unit_table (struct objfile *objfile)
6748 {
6749 /* Start out with a pretty small number.
6750 Generally DWO files contain only one CU and maybe some TUs. */
6751 return htab_create_alloc_ex (3,
6752 hash_dwo_unit,
6753 eq_dwo_unit,
6754 NULL,
6755 &objfile->objfile_obstack,
6756 hashtab_obstack_allocate,
6757 dummy_obstack_deallocate);
6758 }
6759
6760 /* This function is mapped across the sections and remembers the offset and
6761 size of each of the DWO debugging sections we are interested in. */
6762
6763 static void
6764 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6765 {
6766 struct dwo_file *dwo_file = dwo_file_ptr;
6767 const struct dwo_section_names *names = &dwo_section_names;
6768
6769 if (section_is_p (sectp->name, &names->abbrev_dwo))
6770 {
6771 dwo_file->sections.abbrev.asection = sectp;
6772 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6773 }
6774 else if (section_is_p (sectp->name, &names->info_dwo))
6775 {
6776 dwo_file->sections.info.asection = sectp;
6777 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6778 }
6779 else if (section_is_p (sectp->name, &names->line_dwo))
6780 {
6781 dwo_file->sections.line.asection = sectp;
6782 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6783 }
6784 else if (section_is_p (sectp->name, &names->loc_dwo))
6785 {
6786 dwo_file->sections.loc.asection = sectp;
6787 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6788 }
6789 else if (section_is_p (sectp->name, &names->str_dwo))
6790 {
6791 dwo_file->sections.str.asection = sectp;
6792 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6793 }
6794 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6795 {
6796 dwo_file->sections.str_offsets.asection = sectp;
6797 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6798 }
6799 else if (section_is_p (sectp->name, &names->types_dwo))
6800 {
6801 struct dwarf2_section_info type_section;
6802
6803 memset (&type_section, 0, sizeof (type_section));
6804 type_section.asection = sectp;
6805 type_section.size = bfd_get_section_size (sectp);
6806 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6807 &type_section);
6808 }
6809 }
6810
6811 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6812
6813 struct create_dwo_info_table_data
6814 {
6815 struct dwo_file *dwo_file;
6816 htab_t cu_htab;
6817 };
6818
6819 /* die_reader_func for create_debug_info_hash_table. */
6820
6821 static void
6822 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6823 gdb_byte *info_ptr,
6824 struct die_info *comp_unit_die,
6825 int has_children,
6826 void *datap)
6827 {
6828 struct dwarf2_cu *cu = reader->cu;
6829 struct objfile *objfile = dwarf2_per_objfile->objfile;
6830 sect_offset offset = cu->per_cu->offset;
6831 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6832 struct create_dwo_info_table_data *data = datap;
6833 struct dwo_file *dwo_file = data->dwo_file;
6834 htab_t cu_htab = data->cu_htab;
6835 void **slot;
6836 struct attribute *attr;
6837 struct dwo_unit *dwo_unit;
6838
6839 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6840 if (attr == NULL)
6841 {
6842 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6843 " its dwo_id [in module %s]"),
6844 offset.sect_off, dwo_file->dwo_name);
6845 return;
6846 }
6847
6848 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6849 dwo_unit->dwo_file = dwo_file;
6850 dwo_unit->signature = DW_UNSND (attr);
6851 dwo_unit->info_or_types_section = section;
6852 dwo_unit->offset = offset;
6853 dwo_unit->length = cu->per_cu->length;
6854
6855 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6856 gdb_assert (slot != NULL);
6857 if (*slot != NULL)
6858 {
6859 const struct dwo_unit *dup_dwo_unit = *slot;
6860
6861 complaint (&symfile_complaints,
6862 _("debug entry at offset 0x%x is duplicate to the entry at"
6863 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6864 offset.sect_off, dup_dwo_unit->offset.sect_off,
6865 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6866 dwo_file->dwo_name);
6867 }
6868 else
6869 *slot = dwo_unit;
6870
6871 if (dwarf2_die_debug)
6872 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6873 offset.sect_off,
6874 phex (dwo_unit->signature,
6875 sizeof (dwo_unit->signature)));
6876 }
6877
6878 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6879
6880 static htab_t
6881 create_debug_info_hash_table (struct dwo_file *dwo_file)
6882 {
6883 struct objfile *objfile = dwarf2_per_objfile->objfile;
6884 struct dwarf2_section_info *section = &dwo_file->sections.info;
6885 bfd *abfd;
6886 htab_t cu_htab;
6887 gdb_byte *info_ptr, *end_ptr;
6888 struct create_dwo_info_table_data create_dwo_info_table_data;
6889
6890 dwarf2_read_section (objfile, section);
6891 info_ptr = section->buffer;
6892
6893 if (info_ptr == NULL)
6894 return NULL;
6895
6896 /* We can't set abfd until now because the section may be empty or
6897 not present, in which case section->asection will be NULL. */
6898 abfd = section->asection->owner;
6899
6900 if (dwarf2_die_debug)
6901 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6902 bfd_get_filename (abfd));
6903
6904 cu_htab = allocate_dwo_unit_table (objfile);
6905
6906 create_dwo_info_table_data.dwo_file = dwo_file;
6907 create_dwo_info_table_data.cu_htab = cu_htab;
6908
6909 end_ptr = info_ptr + section->size;
6910 while (info_ptr < end_ptr)
6911 {
6912 struct dwarf2_per_cu_data per_cu;
6913
6914 memset (&per_cu, 0, sizeof (per_cu));
6915 per_cu.objfile = objfile;
6916 per_cu.is_debug_types = 0;
6917 per_cu.offset.sect_off = info_ptr - section->buffer;
6918 per_cu.info_or_types_section = section;
6919
6920 init_cutu_and_read_dies_no_follow (&per_cu,
6921 &dwo_file->sections.abbrev,
6922 dwo_file,
6923 create_debug_info_hash_table_reader,
6924 &create_dwo_info_table_data);
6925
6926 info_ptr += per_cu.length;
6927 }
6928
6929 return cu_htab;
6930 }
6931
6932 /* Subroutine of open_dwo_file to simplify it.
6933 Open the file specified by FILE_NAME and hand it off to BFD for
6934 preliminary analysis. Return a newly initialized bfd *, which
6935 includes a canonicalized copy of FILE_NAME.
6936 In case of trouble, return NULL.
6937 NOTE: This function is derived from symfile_bfd_open. */
6938
6939 static bfd *
6940 try_open_dwo_file (const char *file_name)
6941 {
6942 bfd *sym_bfd;
6943 int desc;
6944 char *absolute_name;
6945 char *name;
6946
6947 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6948 O_RDONLY | O_BINARY, &absolute_name);
6949 if (desc < 0)
6950 return NULL;
6951
6952 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6953 if (!sym_bfd)
6954 {
6955 close (desc);
6956 xfree (absolute_name);
6957 return NULL;
6958 }
6959 bfd_set_cacheable (sym_bfd, 1);
6960
6961 if (!bfd_check_format (sym_bfd, bfd_object))
6962 {
6963 bfd_close (sym_bfd); /* This also closes desc. */
6964 xfree (absolute_name);
6965 return NULL;
6966 }
6967
6968 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6969 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6970
6971 return sym_bfd;
6972 }
6973
6974 /* Try to open DWO file DWO_NAME.
6975 COMP_DIR is the DW_AT_comp_dir attribute.
6976 The result is the bfd handle of the file.
6977 If there is a problem finding or opening the file, return NULL.
6978 Upon success, the canonicalized path of the file is stored in the bfd,
6979 same as symfile_bfd_open. */
6980
6981 static bfd *
6982 open_dwo_file (const char *dwo_name, const char *comp_dir)
6983 {
6984 bfd *abfd;
6985 char *path_to_try, *debug_dir;
6986
6987 if (IS_ABSOLUTE_PATH (dwo_name))
6988 return try_open_dwo_file (dwo_name);
6989
6990 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6991
6992 if (comp_dir != NULL)
6993 {
6994 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6995
6996 /* NOTE: If comp_dir is a relative path, this will also try the
6997 search path, which seems useful. */
6998 abfd = try_open_dwo_file (path_to_try);
6999 xfree (path_to_try);
7000 if (abfd != NULL)
7001 return abfd;
7002 }
7003
7004 /* That didn't work, try debug-file-directory, which, despite its name,
7005 is a list of paths. */
7006
7007 if (*debug_file_directory == '\0')
7008 return NULL;
7009
7010 return try_open_dwo_file (dwo_name);
7011 }
7012
7013 /* Initialize the use of the DWO file specified by DWO_NAME. */
7014
7015 static struct dwo_file *
7016 init_dwo_file (const char *dwo_name, const char *comp_dir)
7017 {
7018 struct objfile *objfile = dwarf2_per_objfile->objfile;
7019 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7020 struct dwo_file);
7021 bfd *abfd;
7022 struct cleanup *cleanups;
7023
7024 if (dwarf2_die_debug)
7025 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7026
7027 abfd = open_dwo_file (dwo_name, comp_dir);
7028 if (abfd == NULL)
7029 return NULL;
7030 dwo_file->dwo_name = dwo_name;
7031 dwo_file->dwo_bfd = abfd;
7032
7033 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7034
7035 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7036
7037 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7038
7039 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7040 dwo_file->sections.types);
7041
7042 discard_cleanups (cleanups);
7043
7044 return dwo_file;
7045 }
7046
7047 /* Lookup DWO file DWO_NAME. */
7048
7049 static struct dwo_file *
7050 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7051 {
7052 struct dwo_file *dwo_file;
7053 struct dwo_file find_entry;
7054 void **slot;
7055
7056 if (dwarf2_per_objfile->dwo_files == NULL)
7057 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7058
7059 /* Have we already seen this DWO file? */
7060 find_entry.dwo_name = dwo_name;
7061 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7062
7063 /* If not, read it in and build a table of the DWOs it contains. */
7064 if (*slot == NULL)
7065 *slot = init_dwo_file (dwo_name, comp_dir);
7066
7067 /* NOTE: This will be NULL if unable to open the file. */
7068 dwo_file = *slot;
7069
7070 return dwo_file;
7071 }
7072
7073 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7074 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7075 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7076 nomenclature as TUs).
7077 The result is the DWO CU or NULL if we didn't find it
7078 (dwo_id mismatch or couldn't find the DWO file). */
7079
7080 static struct dwo_unit *
7081 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7082 char *dwo_name, const char *comp_dir,
7083 ULONGEST signature)
7084 {
7085 struct objfile *objfile = dwarf2_per_objfile->objfile;
7086 struct dwo_file *dwo_file;
7087
7088 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7089 if (dwo_file == NULL)
7090 return NULL;
7091
7092 /* Look up the DWO using its signature(dwo_id). */
7093
7094 if (dwo_file->cus != NULL)
7095 {
7096 struct dwo_unit find_dwo_cu, *dwo_cu;
7097
7098 find_dwo_cu.signature = signature;
7099 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7100
7101 if (dwo_cu != NULL)
7102 return dwo_cu;
7103 }
7104
7105 /* We didn't find it. This must mean a dwo_id mismatch. */
7106
7107 complaint (&symfile_complaints,
7108 _("Could not find DWO CU referenced by CU at offset 0x%x"
7109 " [in module %s]"),
7110 this_cu->offset.sect_off, objfile->name);
7111 return NULL;
7112 }
7113
7114 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7115 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7116 The result is the DWO CU or NULL if we didn't find it
7117 (dwo_id mismatch or couldn't find the DWO file). */
7118
7119 static struct dwo_unit *
7120 lookup_dwo_type_unit (struct signatured_type *this_tu,
7121 char *dwo_name, const char *comp_dir)
7122 {
7123 struct objfile *objfile = dwarf2_per_objfile->objfile;
7124 struct dwo_file *dwo_file;
7125 struct dwo_unit find_dwo_tu, *dwo_tu;
7126
7127 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7128 if (dwo_file == NULL)
7129 return NULL;
7130
7131 /* Look up the DWO using its signature(dwo_id). */
7132
7133 if (dwo_file->tus != NULL)
7134 {
7135 struct dwo_unit find_dwo_tu, *dwo_tu;
7136
7137 find_dwo_tu.signature = this_tu->signature;
7138 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7139
7140 if (dwo_tu != NULL)
7141 return dwo_tu;
7142 }
7143
7144 /* We didn't find it. This must mean a dwo_id mismatch. */
7145
7146 complaint (&symfile_complaints,
7147 _("Could not find DWO TU referenced by TU at offset 0x%x"
7148 " [in module %s]"),
7149 this_tu->per_cu.offset.sect_off, objfile->name);
7150 return NULL;
7151 }
7152
7153 /* Free all resources associated with DWO_FILE.
7154 Close the DWO file and munmap the sections.
7155 All memory should be on the objfile obstack. */
7156
7157 static void
7158 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7159 {
7160 int ix;
7161 struct dwarf2_section_info *section;
7162
7163 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7164 bfd_close (dwo_file->dwo_bfd);
7165
7166 munmap_section_buffer (&dwo_file->sections.abbrev);
7167 munmap_section_buffer (&dwo_file->sections.info);
7168 munmap_section_buffer (&dwo_file->sections.line);
7169 munmap_section_buffer (&dwo_file->sections.loc);
7170 munmap_section_buffer (&dwo_file->sections.str);
7171 munmap_section_buffer (&dwo_file->sections.str_offsets);
7172
7173 for (ix = 0;
7174 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7175 ix, section);
7176 ++ix)
7177 munmap_section_buffer (section);
7178
7179 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7180 }
7181
7182 /* Wrapper for free_dwo_file for use in cleanups. */
7183
7184 static void
7185 free_dwo_file_cleanup (void *arg)
7186 {
7187 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7188 struct objfile *objfile = dwarf2_per_objfile->objfile;
7189
7190 free_dwo_file (dwo_file, objfile);
7191 }
7192
7193 /* Traversal function for free_dwo_files. */
7194
7195 static int
7196 free_dwo_file_from_slot (void **slot, void *info)
7197 {
7198 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7199 struct objfile *objfile = (struct objfile *) info;
7200
7201 free_dwo_file (dwo_file, objfile);
7202
7203 return 1;
7204 }
7205
7206 /* Free all resources associated with DWO_FILES. */
7207
7208 static void
7209 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7210 {
7211 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7212 }
7213 \f
7214 /* Read in various DIEs. */
7215
7216 /* qsort helper for inherit_abstract_dies. */
7217
7218 static int
7219 unsigned_int_compar (const void *ap, const void *bp)
7220 {
7221 unsigned int a = *(unsigned int *) ap;
7222 unsigned int b = *(unsigned int *) bp;
7223
7224 return (a > b) - (b > a);
7225 }
7226
7227 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7228 Inherit only the children of the DW_AT_abstract_origin DIE not being
7229 already referenced by DW_AT_abstract_origin from the children of the
7230 current DIE. */
7231
7232 static void
7233 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7234 {
7235 struct die_info *child_die;
7236 unsigned die_children_count;
7237 /* CU offsets which were referenced by children of the current DIE. */
7238 sect_offset *offsets;
7239 sect_offset *offsets_end, *offsetp;
7240 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7241 struct die_info *origin_die;
7242 /* Iterator of the ORIGIN_DIE children. */
7243 struct die_info *origin_child_die;
7244 struct cleanup *cleanups;
7245 struct attribute *attr;
7246 struct dwarf2_cu *origin_cu;
7247 struct pending **origin_previous_list_in_scope;
7248
7249 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7250 if (!attr)
7251 return;
7252
7253 /* Note that following die references may follow to a die in a
7254 different cu. */
7255
7256 origin_cu = cu;
7257 origin_die = follow_die_ref (die, attr, &origin_cu);
7258
7259 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7260 symbols in. */
7261 origin_previous_list_in_scope = origin_cu->list_in_scope;
7262 origin_cu->list_in_scope = cu->list_in_scope;
7263
7264 if (die->tag != origin_die->tag
7265 && !(die->tag == DW_TAG_inlined_subroutine
7266 && origin_die->tag == DW_TAG_subprogram))
7267 complaint (&symfile_complaints,
7268 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7269 die->offset.sect_off, origin_die->offset.sect_off);
7270
7271 child_die = die->child;
7272 die_children_count = 0;
7273 while (child_die && child_die->tag)
7274 {
7275 child_die = sibling_die (child_die);
7276 die_children_count++;
7277 }
7278 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7279 cleanups = make_cleanup (xfree, offsets);
7280
7281 offsets_end = offsets;
7282 child_die = die->child;
7283 while (child_die && child_die->tag)
7284 {
7285 /* For each CHILD_DIE, find the corresponding child of
7286 ORIGIN_DIE. If there is more than one layer of
7287 DW_AT_abstract_origin, follow them all; there shouldn't be,
7288 but GCC versions at least through 4.4 generate this (GCC PR
7289 40573). */
7290 struct die_info *child_origin_die = child_die;
7291 struct dwarf2_cu *child_origin_cu = cu;
7292
7293 while (1)
7294 {
7295 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7296 child_origin_cu);
7297 if (attr == NULL)
7298 break;
7299 child_origin_die = follow_die_ref (child_origin_die, attr,
7300 &child_origin_cu);
7301 }
7302
7303 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7304 counterpart may exist. */
7305 if (child_origin_die != child_die)
7306 {
7307 if (child_die->tag != child_origin_die->tag
7308 && !(child_die->tag == DW_TAG_inlined_subroutine
7309 && child_origin_die->tag == DW_TAG_subprogram))
7310 complaint (&symfile_complaints,
7311 _("Child DIE 0x%x and its abstract origin 0x%x have "
7312 "different tags"), child_die->offset.sect_off,
7313 child_origin_die->offset.sect_off);
7314 if (child_origin_die->parent != origin_die)
7315 complaint (&symfile_complaints,
7316 _("Child DIE 0x%x and its abstract origin 0x%x have "
7317 "different parents"), child_die->offset.sect_off,
7318 child_origin_die->offset.sect_off);
7319 else
7320 *offsets_end++ = child_origin_die->offset;
7321 }
7322 child_die = sibling_die (child_die);
7323 }
7324 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7325 unsigned_int_compar);
7326 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7327 if (offsetp[-1].sect_off == offsetp->sect_off)
7328 complaint (&symfile_complaints,
7329 _("Multiple children of DIE 0x%x refer "
7330 "to DIE 0x%x as their abstract origin"),
7331 die->offset.sect_off, offsetp->sect_off);
7332
7333 offsetp = offsets;
7334 origin_child_die = origin_die->child;
7335 while (origin_child_die && origin_child_die->tag)
7336 {
7337 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7338 while (offsetp < offsets_end
7339 && offsetp->sect_off < origin_child_die->offset.sect_off)
7340 offsetp++;
7341 if (offsetp >= offsets_end
7342 || offsetp->sect_off > origin_child_die->offset.sect_off)
7343 {
7344 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7345 process_die (origin_child_die, origin_cu);
7346 }
7347 origin_child_die = sibling_die (origin_child_die);
7348 }
7349 origin_cu->list_in_scope = origin_previous_list_in_scope;
7350
7351 do_cleanups (cleanups);
7352 }
7353
7354 static void
7355 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7356 {
7357 struct objfile *objfile = cu->objfile;
7358 struct context_stack *new;
7359 CORE_ADDR lowpc;
7360 CORE_ADDR highpc;
7361 struct die_info *child_die;
7362 struct attribute *attr, *call_line, *call_file;
7363 char *name;
7364 CORE_ADDR baseaddr;
7365 struct block *block;
7366 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7367 VEC (symbolp) *template_args = NULL;
7368 struct template_symbol *templ_func = NULL;
7369
7370 if (inlined_func)
7371 {
7372 /* If we do not have call site information, we can't show the
7373 caller of this inlined function. That's too confusing, so
7374 only use the scope for local variables. */
7375 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7376 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7377 if (call_line == NULL || call_file == NULL)
7378 {
7379 read_lexical_block_scope (die, cu);
7380 return;
7381 }
7382 }
7383
7384 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7385
7386 name = dwarf2_name (die, cu);
7387
7388 /* Ignore functions with missing or empty names. These are actually
7389 illegal according to the DWARF standard. */
7390 if (name == NULL)
7391 {
7392 complaint (&symfile_complaints,
7393 _("missing name for subprogram DIE at %d"),
7394 die->offset.sect_off);
7395 return;
7396 }
7397
7398 /* Ignore functions with missing or invalid low and high pc attributes. */
7399 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7400 {
7401 attr = dwarf2_attr (die, DW_AT_external, cu);
7402 if (!attr || !DW_UNSND (attr))
7403 complaint (&symfile_complaints,
7404 _("cannot get low and high bounds "
7405 "for subprogram DIE at %d"),
7406 die->offset.sect_off);
7407 return;
7408 }
7409
7410 lowpc += baseaddr;
7411 highpc += baseaddr;
7412
7413 /* If we have any template arguments, then we must allocate a
7414 different sort of symbol. */
7415 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7416 {
7417 if (child_die->tag == DW_TAG_template_type_param
7418 || child_die->tag == DW_TAG_template_value_param)
7419 {
7420 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7421 struct template_symbol);
7422 templ_func->base.is_cplus_template_function = 1;
7423 break;
7424 }
7425 }
7426
7427 new = push_context (0, lowpc);
7428 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7429 (struct symbol *) templ_func);
7430
7431 /* If there is a location expression for DW_AT_frame_base, record
7432 it. */
7433 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7434 if (attr)
7435 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7436 expression is being recorded directly in the function's symbol
7437 and not in a separate frame-base object. I guess this hack is
7438 to avoid adding some sort of frame-base adjunct/annex to the
7439 function's symbol :-(. The problem with doing this is that it
7440 results in a function symbol with a location expression that
7441 has nothing to do with the location of the function, ouch! The
7442 relationship should be: a function's symbol has-a frame base; a
7443 frame-base has-a location expression. */
7444 dwarf2_symbol_mark_computed (attr, new->name, cu);
7445
7446 cu->list_in_scope = &local_symbols;
7447
7448 if (die->child != NULL)
7449 {
7450 child_die = die->child;
7451 while (child_die && child_die->tag)
7452 {
7453 if (child_die->tag == DW_TAG_template_type_param
7454 || child_die->tag == DW_TAG_template_value_param)
7455 {
7456 struct symbol *arg = new_symbol (child_die, NULL, cu);
7457
7458 if (arg != NULL)
7459 VEC_safe_push (symbolp, template_args, arg);
7460 }
7461 else
7462 process_die (child_die, cu);
7463 child_die = sibling_die (child_die);
7464 }
7465 }
7466
7467 inherit_abstract_dies (die, cu);
7468
7469 /* If we have a DW_AT_specification, we might need to import using
7470 directives from the context of the specification DIE. See the
7471 comment in determine_prefix. */
7472 if (cu->language == language_cplus
7473 && dwarf2_attr (die, DW_AT_specification, cu))
7474 {
7475 struct dwarf2_cu *spec_cu = cu;
7476 struct die_info *spec_die = die_specification (die, &spec_cu);
7477
7478 while (spec_die)
7479 {
7480 child_die = spec_die->child;
7481 while (child_die && child_die->tag)
7482 {
7483 if (child_die->tag == DW_TAG_imported_module)
7484 process_die (child_die, spec_cu);
7485 child_die = sibling_die (child_die);
7486 }
7487
7488 /* In some cases, GCC generates specification DIEs that
7489 themselves contain DW_AT_specification attributes. */
7490 spec_die = die_specification (spec_die, &spec_cu);
7491 }
7492 }
7493
7494 new = pop_context ();
7495 /* Make a block for the local symbols within. */
7496 block = finish_block (new->name, &local_symbols, new->old_blocks,
7497 lowpc, highpc, objfile);
7498
7499 /* For C++, set the block's scope. */
7500 if (cu->language == language_cplus || cu->language == language_fortran)
7501 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7502 determine_prefix (die, cu),
7503 processing_has_namespace_info);
7504
7505 /* If we have address ranges, record them. */
7506 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7507
7508 /* Attach template arguments to function. */
7509 if (! VEC_empty (symbolp, template_args))
7510 {
7511 gdb_assert (templ_func != NULL);
7512
7513 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7514 templ_func->template_arguments
7515 = obstack_alloc (&objfile->objfile_obstack,
7516 (templ_func->n_template_arguments
7517 * sizeof (struct symbol *)));
7518 memcpy (templ_func->template_arguments,
7519 VEC_address (symbolp, template_args),
7520 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7521 VEC_free (symbolp, template_args);
7522 }
7523
7524 /* In C++, we can have functions nested inside functions (e.g., when
7525 a function declares a class that has methods). This means that
7526 when we finish processing a function scope, we may need to go
7527 back to building a containing block's symbol lists. */
7528 local_symbols = new->locals;
7529 param_symbols = new->params;
7530 using_directives = new->using_directives;
7531
7532 /* If we've finished processing a top-level function, subsequent
7533 symbols go in the file symbol list. */
7534 if (outermost_context_p ())
7535 cu->list_in_scope = &file_symbols;
7536 }
7537
7538 /* Process all the DIES contained within a lexical block scope. Start
7539 a new scope, process the dies, and then close the scope. */
7540
7541 static void
7542 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7543 {
7544 struct objfile *objfile = cu->objfile;
7545 struct context_stack *new;
7546 CORE_ADDR lowpc, highpc;
7547 struct die_info *child_die;
7548 CORE_ADDR baseaddr;
7549
7550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7551
7552 /* Ignore blocks with missing or invalid low and high pc attributes. */
7553 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7554 as multiple lexical blocks? Handling children in a sane way would
7555 be nasty. Might be easier to properly extend generic blocks to
7556 describe ranges. */
7557 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7558 return;
7559 lowpc += baseaddr;
7560 highpc += baseaddr;
7561
7562 push_context (0, lowpc);
7563 if (die->child != NULL)
7564 {
7565 child_die = die->child;
7566 while (child_die && child_die->tag)
7567 {
7568 process_die (child_die, cu);
7569 child_die = sibling_die (child_die);
7570 }
7571 }
7572 new = pop_context ();
7573
7574 if (local_symbols != NULL || using_directives != NULL)
7575 {
7576 struct block *block
7577 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7578 highpc, objfile);
7579
7580 /* Note that recording ranges after traversing children, as we
7581 do here, means that recording a parent's ranges entails
7582 walking across all its children's ranges as they appear in
7583 the address map, which is quadratic behavior.
7584
7585 It would be nicer to record the parent's ranges before
7586 traversing its children, simply overriding whatever you find
7587 there. But since we don't even decide whether to create a
7588 block until after we've traversed its children, that's hard
7589 to do. */
7590 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7591 }
7592 local_symbols = new->locals;
7593 using_directives = new->using_directives;
7594 }
7595
7596 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7597
7598 static void
7599 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7600 {
7601 struct objfile *objfile = cu->objfile;
7602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7603 CORE_ADDR pc, baseaddr;
7604 struct attribute *attr;
7605 struct call_site *call_site, call_site_local;
7606 void **slot;
7607 int nparams;
7608 struct die_info *child_die;
7609
7610 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7611
7612 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7613 if (!attr)
7614 {
7615 complaint (&symfile_complaints,
7616 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7617 "DIE 0x%x [in module %s]"),
7618 die->offset.sect_off, objfile->name);
7619 return;
7620 }
7621 pc = DW_ADDR (attr) + baseaddr;
7622
7623 if (cu->call_site_htab == NULL)
7624 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7625 NULL, &objfile->objfile_obstack,
7626 hashtab_obstack_allocate, NULL);
7627 call_site_local.pc = pc;
7628 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7629 if (*slot != NULL)
7630 {
7631 complaint (&symfile_complaints,
7632 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7633 "DIE 0x%x [in module %s]"),
7634 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7635 return;
7636 }
7637
7638 /* Count parameters at the caller. */
7639
7640 nparams = 0;
7641 for (child_die = die->child; child_die && child_die->tag;
7642 child_die = sibling_die (child_die))
7643 {
7644 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7645 {
7646 complaint (&symfile_complaints,
7647 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7648 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7649 child_die->tag, child_die->offset.sect_off, objfile->name);
7650 continue;
7651 }
7652
7653 nparams++;
7654 }
7655
7656 call_site = obstack_alloc (&objfile->objfile_obstack,
7657 (sizeof (*call_site)
7658 + (sizeof (*call_site->parameter)
7659 * (nparams - 1))));
7660 *slot = call_site;
7661 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7662 call_site->pc = pc;
7663
7664 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7665 {
7666 struct die_info *func_die;
7667
7668 /* Skip also over DW_TAG_inlined_subroutine. */
7669 for (func_die = die->parent;
7670 func_die && func_die->tag != DW_TAG_subprogram
7671 && func_die->tag != DW_TAG_subroutine_type;
7672 func_die = func_die->parent);
7673
7674 /* DW_AT_GNU_all_call_sites is a superset
7675 of DW_AT_GNU_all_tail_call_sites. */
7676 if (func_die
7677 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7678 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7679 {
7680 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7681 not complete. But keep CALL_SITE for look ups via call_site_htab,
7682 both the initial caller containing the real return address PC and
7683 the final callee containing the current PC of a chain of tail
7684 calls do not need to have the tail call list complete. But any
7685 function candidate for a virtual tail call frame searched via
7686 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7687 determined unambiguously. */
7688 }
7689 else
7690 {
7691 struct type *func_type = NULL;
7692
7693 if (func_die)
7694 func_type = get_die_type (func_die, cu);
7695 if (func_type != NULL)
7696 {
7697 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7698
7699 /* Enlist this call site to the function. */
7700 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7701 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7702 }
7703 else
7704 complaint (&symfile_complaints,
7705 _("Cannot find function owning DW_TAG_GNU_call_site "
7706 "DIE 0x%x [in module %s]"),
7707 die->offset.sect_off, objfile->name);
7708 }
7709 }
7710
7711 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7712 if (attr == NULL)
7713 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7714 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7715 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7716 /* Keep NULL DWARF_BLOCK. */;
7717 else if (attr_form_is_block (attr))
7718 {
7719 struct dwarf2_locexpr_baton *dlbaton;
7720
7721 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7722 dlbaton->data = DW_BLOCK (attr)->data;
7723 dlbaton->size = DW_BLOCK (attr)->size;
7724 dlbaton->per_cu = cu->per_cu;
7725
7726 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7727 }
7728 else if (is_ref_attr (attr))
7729 {
7730 struct dwarf2_cu *target_cu = cu;
7731 struct die_info *target_die;
7732
7733 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7734 gdb_assert (target_cu->objfile == objfile);
7735 if (die_is_declaration (target_die, target_cu))
7736 {
7737 const char *target_physname;
7738
7739 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7740 if (target_physname == NULL)
7741 complaint (&symfile_complaints,
7742 _("DW_AT_GNU_call_site_target target DIE has invalid "
7743 "physname, for referencing DIE 0x%x [in module %s]"),
7744 die->offset.sect_off, objfile->name);
7745 else
7746 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7747 }
7748 else
7749 {
7750 CORE_ADDR lowpc;
7751
7752 /* DW_AT_entry_pc should be preferred. */
7753 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7754 complaint (&symfile_complaints,
7755 _("DW_AT_GNU_call_site_target target DIE has invalid "
7756 "low pc, for referencing DIE 0x%x [in module %s]"),
7757 die->offset.sect_off, objfile->name);
7758 else
7759 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7760 }
7761 }
7762 else
7763 complaint (&symfile_complaints,
7764 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7765 "block nor reference, for DIE 0x%x [in module %s]"),
7766 die->offset.sect_off, objfile->name);
7767
7768 call_site->per_cu = cu->per_cu;
7769
7770 for (child_die = die->child;
7771 child_die && child_die->tag;
7772 child_die = sibling_die (child_die))
7773 {
7774 struct dwarf2_locexpr_baton *dlbaton;
7775 struct call_site_parameter *parameter;
7776
7777 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7778 {
7779 /* Already printed the complaint above. */
7780 continue;
7781 }
7782
7783 gdb_assert (call_site->parameter_count < nparams);
7784 parameter = &call_site->parameter[call_site->parameter_count];
7785
7786 /* DW_AT_location specifies the register number. Value of the data
7787 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7788
7789 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7790 if (!attr || !attr_form_is_block (attr))
7791 {
7792 complaint (&symfile_complaints,
7793 _("No DW_FORM_block* DW_AT_location for "
7794 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7795 child_die->offset.sect_off, objfile->name);
7796 continue;
7797 }
7798 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7799 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7800 if (parameter->dwarf_reg == -1
7801 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7802 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7803 &parameter->fb_offset))
7804 {
7805 complaint (&symfile_complaints,
7806 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7807 "for DW_FORM_block* DW_AT_location for "
7808 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7809 child_die->offset.sect_off, objfile->name);
7810 continue;
7811 }
7812
7813 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7814 if (!attr_form_is_block (attr))
7815 {
7816 complaint (&symfile_complaints,
7817 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7818 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7819 child_die->offset.sect_off, objfile->name);
7820 continue;
7821 }
7822 parameter->value = DW_BLOCK (attr)->data;
7823 parameter->value_size = DW_BLOCK (attr)->size;
7824
7825 /* Parameters are not pre-cleared by memset above. */
7826 parameter->data_value = NULL;
7827 parameter->data_value_size = 0;
7828 call_site->parameter_count++;
7829
7830 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7831 if (attr)
7832 {
7833 if (!attr_form_is_block (attr))
7834 complaint (&symfile_complaints,
7835 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7836 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7837 child_die->offset.sect_off, objfile->name);
7838 else
7839 {
7840 parameter->data_value = DW_BLOCK (attr)->data;
7841 parameter->data_value_size = DW_BLOCK (attr)->size;
7842 }
7843 }
7844 }
7845 }
7846
7847 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7848 Return 1 if the attributes are present and valid, otherwise, return 0.
7849 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7850
7851 static int
7852 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7853 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7854 struct partial_symtab *ranges_pst)
7855 {
7856 struct objfile *objfile = cu->objfile;
7857 struct comp_unit_head *cu_header = &cu->header;
7858 bfd *obfd = objfile->obfd;
7859 unsigned int addr_size = cu_header->addr_size;
7860 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7861 /* Base address selection entry. */
7862 CORE_ADDR base;
7863 int found_base;
7864 unsigned int dummy;
7865 gdb_byte *buffer;
7866 CORE_ADDR marker;
7867 int low_set;
7868 CORE_ADDR low = 0;
7869 CORE_ADDR high = 0;
7870 CORE_ADDR baseaddr;
7871
7872 found_base = cu->base_known;
7873 base = cu->base_address;
7874
7875 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7876 if (offset >= dwarf2_per_objfile->ranges.size)
7877 {
7878 complaint (&symfile_complaints,
7879 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7880 offset);
7881 return 0;
7882 }
7883 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7884
7885 /* Read in the largest possible address. */
7886 marker = read_address (obfd, buffer, cu, &dummy);
7887 if ((marker & mask) == mask)
7888 {
7889 /* If we found the largest possible address, then
7890 read the base address. */
7891 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7892 buffer += 2 * addr_size;
7893 offset += 2 * addr_size;
7894 found_base = 1;
7895 }
7896
7897 low_set = 0;
7898
7899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7900
7901 while (1)
7902 {
7903 CORE_ADDR range_beginning, range_end;
7904
7905 range_beginning = read_address (obfd, buffer, cu, &dummy);
7906 buffer += addr_size;
7907 range_end = read_address (obfd, buffer, cu, &dummy);
7908 buffer += addr_size;
7909 offset += 2 * addr_size;
7910
7911 /* An end of list marker is a pair of zero addresses. */
7912 if (range_beginning == 0 && range_end == 0)
7913 /* Found the end of list entry. */
7914 break;
7915
7916 /* Each base address selection entry is a pair of 2 values.
7917 The first is the largest possible address, the second is
7918 the base address. Check for a base address here. */
7919 if ((range_beginning & mask) == mask)
7920 {
7921 /* If we found the largest possible address, then
7922 read the base address. */
7923 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7924 found_base = 1;
7925 continue;
7926 }
7927
7928 if (!found_base)
7929 {
7930 /* We have no valid base address for the ranges
7931 data. */
7932 complaint (&symfile_complaints,
7933 _("Invalid .debug_ranges data (no base address)"));
7934 return 0;
7935 }
7936
7937 if (range_beginning > range_end)
7938 {
7939 /* Inverted range entries are invalid. */
7940 complaint (&symfile_complaints,
7941 _("Invalid .debug_ranges data (inverted range)"));
7942 return 0;
7943 }
7944
7945 /* Empty range entries have no effect. */
7946 if (range_beginning == range_end)
7947 continue;
7948
7949 range_beginning += base;
7950 range_end += base;
7951
7952 if (ranges_pst != NULL)
7953 addrmap_set_empty (objfile->psymtabs_addrmap,
7954 range_beginning + baseaddr,
7955 range_end - 1 + baseaddr,
7956 ranges_pst);
7957
7958 /* FIXME: This is recording everything as a low-high
7959 segment of consecutive addresses. We should have a
7960 data structure for discontiguous block ranges
7961 instead. */
7962 if (! low_set)
7963 {
7964 low = range_beginning;
7965 high = range_end;
7966 low_set = 1;
7967 }
7968 else
7969 {
7970 if (range_beginning < low)
7971 low = range_beginning;
7972 if (range_end > high)
7973 high = range_end;
7974 }
7975 }
7976
7977 if (! low_set)
7978 /* If the first entry is an end-of-list marker, the range
7979 describes an empty scope, i.e. no instructions. */
7980 return 0;
7981
7982 if (low_return)
7983 *low_return = low;
7984 if (high_return)
7985 *high_return = high;
7986 return 1;
7987 }
7988
7989 /* Get low and high pc attributes from a die. Return 1 if the attributes
7990 are present and valid, otherwise, return 0. Return -1 if the range is
7991 discontinuous, i.e. derived from DW_AT_ranges information. */
7992
7993 static int
7994 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7995 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7996 struct partial_symtab *pst)
7997 {
7998 struct attribute *attr;
7999 struct attribute *attr_high;
8000 CORE_ADDR low = 0;
8001 CORE_ADDR high = 0;
8002 int ret = 0;
8003
8004 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8005 if (attr_high)
8006 {
8007 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8008 if (attr)
8009 {
8010 low = DW_ADDR (attr);
8011 if (attr_high->form == DW_FORM_addr
8012 || attr_high->form == DW_FORM_GNU_addr_index)
8013 high = DW_ADDR (attr_high);
8014 else
8015 high = low + DW_UNSND (attr_high);
8016 }
8017 else
8018 /* Found high w/o low attribute. */
8019 return 0;
8020
8021 /* Found consecutive range of addresses. */
8022 ret = 1;
8023 }
8024 else
8025 {
8026 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8027 if (attr != NULL)
8028 {
8029 /* Value of the DW_AT_ranges attribute is the offset in the
8030 .debug_ranges section. */
8031 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8032 return 0;
8033 /* Found discontinuous range of addresses. */
8034 ret = -1;
8035 }
8036 }
8037
8038 /* read_partial_die has also the strict LOW < HIGH requirement. */
8039 if (high <= low)
8040 return 0;
8041
8042 /* When using the GNU linker, .gnu.linkonce. sections are used to
8043 eliminate duplicate copies of functions and vtables and such.
8044 The linker will arbitrarily choose one and discard the others.
8045 The AT_*_pc values for such functions refer to local labels in
8046 these sections. If the section from that file was discarded, the
8047 labels are not in the output, so the relocs get a value of 0.
8048 If this is a discarded function, mark the pc bounds as invalid,
8049 so that GDB will ignore it. */
8050 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8051 return 0;
8052
8053 *lowpc = low;
8054 if (highpc)
8055 *highpc = high;
8056 return ret;
8057 }
8058
8059 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8060 its low and high PC addresses. Do nothing if these addresses could not
8061 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8062 and HIGHPC to the high address if greater than HIGHPC. */
8063
8064 static void
8065 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8067 struct dwarf2_cu *cu)
8068 {
8069 CORE_ADDR low, high;
8070 struct die_info *child = die->child;
8071
8072 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8073 {
8074 *lowpc = min (*lowpc, low);
8075 *highpc = max (*highpc, high);
8076 }
8077
8078 /* If the language does not allow nested subprograms (either inside
8079 subprograms or lexical blocks), we're done. */
8080 if (cu->language != language_ada)
8081 return;
8082
8083 /* Check all the children of the given DIE. If it contains nested
8084 subprograms, then check their pc bounds. Likewise, we need to
8085 check lexical blocks as well, as they may also contain subprogram
8086 definitions. */
8087 while (child && child->tag)
8088 {
8089 if (child->tag == DW_TAG_subprogram
8090 || child->tag == DW_TAG_lexical_block)
8091 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8092 child = sibling_die (child);
8093 }
8094 }
8095
8096 /* Get the low and high pc's represented by the scope DIE, and store
8097 them in *LOWPC and *HIGHPC. If the correct values can't be
8098 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8099
8100 static void
8101 get_scope_pc_bounds (struct die_info *die,
8102 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8103 struct dwarf2_cu *cu)
8104 {
8105 CORE_ADDR best_low = (CORE_ADDR) -1;
8106 CORE_ADDR best_high = (CORE_ADDR) 0;
8107 CORE_ADDR current_low, current_high;
8108
8109 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8110 {
8111 best_low = current_low;
8112 best_high = current_high;
8113 }
8114 else
8115 {
8116 struct die_info *child = die->child;
8117
8118 while (child && child->tag)
8119 {
8120 switch (child->tag) {
8121 case DW_TAG_subprogram:
8122 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8123 break;
8124 case DW_TAG_namespace:
8125 case DW_TAG_module:
8126 /* FIXME: carlton/2004-01-16: Should we do this for
8127 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8128 that current GCC's always emit the DIEs corresponding
8129 to definitions of methods of classes as children of a
8130 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8131 the DIEs giving the declarations, which could be
8132 anywhere). But I don't see any reason why the
8133 standards says that they have to be there. */
8134 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8135
8136 if (current_low != ((CORE_ADDR) -1))
8137 {
8138 best_low = min (best_low, current_low);
8139 best_high = max (best_high, current_high);
8140 }
8141 break;
8142 default:
8143 /* Ignore. */
8144 break;
8145 }
8146
8147 child = sibling_die (child);
8148 }
8149 }
8150
8151 *lowpc = best_low;
8152 *highpc = best_high;
8153 }
8154
8155 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8156 in DIE. */
8157
8158 static void
8159 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8160 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8161 {
8162 struct objfile *objfile = cu->objfile;
8163 struct attribute *attr;
8164 struct attribute *attr_high;
8165
8166 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8167 if (attr_high)
8168 {
8169 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8170 if (attr)
8171 {
8172 CORE_ADDR low = DW_ADDR (attr);
8173 CORE_ADDR high;
8174 if (attr_high->form == DW_FORM_addr
8175 || attr_high->form == DW_FORM_GNU_addr_index)
8176 high = DW_ADDR (attr_high);
8177 else
8178 high = low + DW_UNSND (attr_high);
8179
8180 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8181 }
8182 }
8183
8184 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8185 if (attr)
8186 {
8187 bfd *obfd = objfile->obfd;
8188
8189 /* The value of the DW_AT_ranges attribute is the offset of the
8190 address range list in the .debug_ranges section. */
8191 unsigned long offset = DW_UNSND (attr);
8192 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8193
8194 /* For some target architectures, but not others, the
8195 read_address function sign-extends the addresses it returns.
8196 To recognize base address selection entries, we need a
8197 mask. */
8198 unsigned int addr_size = cu->header.addr_size;
8199 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8200
8201 /* The base address, to which the next pair is relative. Note
8202 that this 'base' is a DWARF concept: most entries in a range
8203 list are relative, to reduce the number of relocs against the
8204 debugging information. This is separate from this function's
8205 'baseaddr' argument, which GDB uses to relocate debugging
8206 information from a shared library based on the address at
8207 which the library was loaded. */
8208 CORE_ADDR base = cu->base_address;
8209 int base_known = cu->base_known;
8210
8211 gdb_assert (dwarf2_per_objfile->ranges.readin);
8212 if (offset >= dwarf2_per_objfile->ranges.size)
8213 {
8214 complaint (&symfile_complaints,
8215 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8216 offset);
8217 return;
8218 }
8219
8220 for (;;)
8221 {
8222 unsigned int bytes_read;
8223 CORE_ADDR start, end;
8224
8225 start = read_address (obfd, buffer, cu, &bytes_read);
8226 buffer += bytes_read;
8227 end = read_address (obfd, buffer, cu, &bytes_read);
8228 buffer += bytes_read;
8229
8230 /* Did we find the end of the range list? */
8231 if (start == 0 && end == 0)
8232 break;
8233
8234 /* Did we find a base address selection entry? */
8235 else if ((start & base_select_mask) == base_select_mask)
8236 {
8237 base = end;
8238 base_known = 1;
8239 }
8240
8241 /* We found an ordinary address range. */
8242 else
8243 {
8244 if (!base_known)
8245 {
8246 complaint (&symfile_complaints,
8247 _("Invalid .debug_ranges data "
8248 "(no base address)"));
8249 return;
8250 }
8251
8252 if (start > end)
8253 {
8254 /* Inverted range entries are invalid. */
8255 complaint (&symfile_complaints,
8256 _("Invalid .debug_ranges data "
8257 "(inverted range)"));
8258 return;
8259 }
8260
8261 /* Empty range entries have no effect. */
8262 if (start == end)
8263 continue;
8264
8265 record_block_range (block,
8266 baseaddr + base + start,
8267 baseaddr + base + end - 1);
8268 }
8269 }
8270 }
8271 }
8272
8273 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8274 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8275 during 4.6.0 experimental. */
8276
8277 static int
8278 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8279 {
8280 const char *cs;
8281 int major, minor, release;
8282 int result = 0;
8283
8284 if (cu->producer == NULL)
8285 {
8286 /* For unknown compilers expect their behavior is DWARF version
8287 compliant.
8288
8289 GCC started to support .debug_types sections by -gdwarf-4 since
8290 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8291 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8292 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8293 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8294
8295 return 0;
8296 }
8297
8298 if (cu->checked_producer)
8299 return cu->producer_is_gxx_lt_4_6;
8300
8301 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8302
8303 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8304 {
8305 /* For non-GCC compilers expect their behavior is DWARF version
8306 compliant. */
8307 }
8308 else
8309 {
8310 cs = &cu->producer[strlen ("GNU ")];
8311 while (*cs && !isdigit (*cs))
8312 cs++;
8313 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8314 {
8315 /* Not recognized as GCC. */
8316 }
8317 else
8318 result = major < 4 || (major == 4 && minor < 6);
8319 }
8320
8321 cu->checked_producer = 1;
8322 cu->producer_is_gxx_lt_4_6 = result;
8323
8324 return result;
8325 }
8326
8327 /* Return the default accessibility type if it is not overriden by
8328 DW_AT_accessibility. */
8329
8330 static enum dwarf_access_attribute
8331 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8332 {
8333 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8334 {
8335 /* The default DWARF 2 accessibility for members is public, the default
8336 accessibility for inheritance is private. */
8337
8338 if (die->tag != DW_TAG_inheritance)
8339 return DW_ACCESS_public;
8340 else
8341 return DW_ACCESS_private;
8342 }
8343 else
8344 {
8345 /* DWARF 3+ defines the default accessibility a different way. The same
8346 rules apply now for DW_TAG_inheritance as for the members and it only
8347 depends on the container kind. */
8348
8349 if (die->parent->tag == DW_TAG_class_type)
8350 return DW_ACCESS_private;
8351 else
8352 return DW_ACCESS_public;
8353 }
8354 }
8355
8356 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8357 offset. If the attribute was not found return 0, otherwise return
8358 1. If it was found but could not properly be handled, set *OFFSET
8359 to 0. */
8360
8361 static int
8362 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8363 LONGEST *offset)
8364 {
8365 struct attribute *attr;
8366
8367 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8368 if (attr != NULL)
8369 {
8370 *offset = 0;
8371
8372 /* Note that we do not check for a section offset first here.
8373 This is because DW_AT_data_member_location is new in DWARF 4,
8374 so if we see it, we can assume that a constant form is really
8375 a constant and not a section offset. */
8376 if (attr_form_is_constant (attr))
8377 *offset = dwarf2_get_attr_constant_value (attr, 0);
8378 else if (attr_form_is_section_offset (attr))
8379 dwarf2_complex_location_expr_complaint ();
8380 else if (attr_form_is_block (attr))
8381 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8382 else
8383 dwarf2_complex_location_expr_complaint ();
8384
8385 return 1;
8386 }
8387
8388 return 0;
8389 }
8390
8391 /* Add an aggregate field to the field list. */
8392
8393 static void
8394 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8395 struct dwarf2_cu *cu)
8396 {
8397 struct objfile *objfile = cu->objfile;
8398 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8399 struct nextfield *new_field;
8400 struct attribute *attr;
8401 struct field *fp;
8402 char *fieldname = "";
8403
8404 /* Allocate a new field list entry and link it in. */
8405 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8406 make_cleanup (xfree, new_field);
8407 memset (new_field, 0, sizeof (struct nextfield));
8408
8409 if (die->tag == DW_TAG_inheritance)
8410 {
8411 new_field->next = fip->baseclasses;
8412 fip->baseclasses = new_field;
8413 }
8414 else
8415 {
8416 new_field->next = fip->fields;
8417 fip->fields = new_field;
8418 }
8419 fip->nfields++;
8420
8421 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8422 if (attr)
8423 new_field->accessibility = DW_UNSND (attr);
8424 else
8425 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8426 if (new_field->accessibility != DW_ACCESS_public)
8427 fip->non_public_fields = 1;
8428
8429 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8430 if (attr)
8431 new_field->virtuality = DW_UNSND (attr);
8432 else
8433 new_field->virtuality = DW_VIRTUALITY_none;
8434
8435 fp = &new_field->field;
8436
8437 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8438 {
8439 LONGEST offset;
8440
8441 /* Data member other than a C++ static data member. */
8442
8443 /* Get type of field. */
8444 fp->type = die_type (die, cu);
8445
8446 SET_FIELD_BITPOS (*fp, 0);
8447
8448 /* Get bit size of field (zero if none). */
8449 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8450 if (attr)
8451 {
8452 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8453 }
8454 else
8455 {
8456 FIELD_BITSIZE (*fp) = 0;
8457 }
8458
8459 /* Get bit offset of field. */
8460 if (handle_data_member_location (die, cu, &offset))
8461 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8462 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8463 if (attr)
8464 {
8465 if (gdbarch_bits_big_endian (gdbarch))
8466 {
8467 /* For big endian bits, the DW_AT_bit_offset gives the
8468 additional bit offset from the MSB of the containing
8469 anonymous object to the MSB of the field. We don't
8470 have to do anything special since we don't need to
8471 know the size of the anonymous object. */
8472 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8473 }
8474 else
8475 {
8476 /* For little endian bits, compute the bit offset to the
8477 MSB of the anonymous object, subtract off the number of
8478 bits from the MSB of the field to the MSB of the
8479 object, and then subtract off the number of bits of
8480 the field itself. The result is the bit offset of
8481 the LSB of the field. */
8482 int anonymous_size;
8483 int bit_offset = DW_UNSND (attr);
8484
8485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8486 if (attr)
8487 {
8488 /* The size of the anonymous object containing
8489 the bit field is explicit, so use the
8490 indicated size (in bytes). */
8491 anonymous_size = DW_UNSND (attr);
8492 }
8493 else
8494 {
8495 /* The size of the anonymous object containing
8496 the bit field must be inferred from the type
8497 attribute of the data member containing the
8498 bit field. */
8499 anonymous_size = TYPE_LENGTH (fp->type);
8500 }
8501 SET_FIELD_BITPOS (*fp,
8502 (FIELD_BITPOS (*fp)
8503 + anonymous_size * bits_per_byte
8504 - bit_offset - FIELD_BITSIZE (*fp)));
8505 }
8506 }
8507
8508 /* Get name of field. */
8509 fieldname = dwarf2_name (die, cu);
8510 if (fieldname == NULL)
8511 fieldname = "";
8512
8513 /* The name is already allocated along with this objfile, so we don't
8514 need to duplicate it for the type. */
8515 fp->name = fieldname;
8516
8517 /* Change accessibility for artificial fields (e.g. virtual table
8518 pointer or virtual base class pointer) to private. */
8519 if (dwarf2_attr (die, DW_AT_artificial, cu))
8520 {
8521 FIELD_ARTIFICIAL (*fp) = 1;
8522 new_field->accessibility = DW_ACCESS_private;
8523 fip->non_public_fields = 1;
8524 }
8525 }
8526 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8527 {
8528 /* C++ static member. */
8529
8530 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8531 is a declaration, but all versions of G++ as of this writing
8532 (so through at least 3.2.1) incorrectly generate
8533 DW_TAG_variable tags. */
8534
8535 const char *physname;
8536
8537 /* Get name of field. */
8538 fieldname = dwarf2_name (die, cu);
8539 if (fieldname == NULL)
8540 return;
8541
8542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8543 if (attr
8544 /* Only create a symbol if this is an external value.
8545 new_symbol checks this and puts the value in the global symbol
8546 table, which we want. If it is not external, new_symbol
8547 will try to put the value in cu->list_in_scope which is wrong. */
8548 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8549 {
8550 /* A static const member, not much different than an enum as far as
8551 we're concerned, except that we can support more types. */
8552 new_symbol (die, NULL, cu);
8553 }
8554
8555 /* Get physical name. */
8556 physname = dwarf2_physname (fieldname, die, cu);
8557
8558 /* The name is already allocated along with this objfile, so we don't
8559 need to duplicate it for the type. */
8560 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8561 FIELD_TYPE (*fp) = die_type (die, cu);
8562 FIELD_NAME (*fp) = fieldname;
8563 }
8564 else if (die->tag == DW_TAG_inheritance)
8565 {
8566 LONGEST offset;
8567
8568 /* C++ base class field. */
8569 if (handle_data_member_location (die, cu, &offset))
8570 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8571 FIELD_BITSIZE (*fp) = 0;
8572 FIELD_TYPE (*fp) = die_type (die, cu);
8573 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8574 fip->nbaseclasses++;
8575 }
8576 }
8577
8578 /* Add a typedef defined in the scope of the FIP's class. */
8579
8580 static void
8581 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8582 struct dwarf2_cu *cu)
8583 {
8584 struct objfile *objfile = cu->objfile;
8585 struct typedef_field_list *new_field;
8586 struct attribute *attr;
8587 struct typedef_field *fp;
8588 char *fieldname = "";
8589
8590 /* Allocate a new field list entry and link it in. */
8591 new_field = xzalloc (sizeof (*new_field));
8592 make_cleanup (xfree, new_field);
8593
8594 gdb_assert (die->tag == DW_TAG_typedef);
8595
8596 fp = &new_field->field;
8597
8598 /* Get name of field. */
8599 fp->name = dwarf2_name (die, cu);
8600 if (fp->name == NULL)
8601 return;
8602
8603 fp->type = read_type_die (die, cu);
8604
8605 new_field->next = fip->typedef_field_list;
8606 fip->typedef_field_list = new_field;
8607 fip->typedef_field_list_count++;
8608 }
8609
8610 /* Create the vector of fields, and attach it to the type. */
8611
8612 static void
8613 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8614 struct dwarf2_cu *cu)
8615 {
8616 int nfields = fip->nfields;
8617
8618 /* Record the field count, allocate space for the array of fields,
8619 and create blank accessibility bitfields if necessary. */
8620 TYPE_NFIELDS (type) = nfields;
8621 TYPE_FIELDS (type) = (struct field *)
8622 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8623 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8624
8625 if (fip->non_public_fields && cu->language != language_ada)
8626 {
8627 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8628
8629 TYPE_FIELD_PRIVATE_BITS (type) =
8630 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8631 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8632
8633 TYPE_FIELD_PROTECTED_BITS (type) =
8634 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8635 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8636
8637 TYPE_FIELD_IGNORE_BITS (type) =
8638 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8639 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8640 }
8641
8642 /* If the type has baseclasses, allocate and clear a bit vector for
8643 TYPE_FIELD_VIRTUAL_BITS. */
8644 if (fip->nbaseclasses && cu->language != language_ada)
8645 {
8646 int num_bytes = B_BYTES (fip->nbaseclasses);
8647 unsigned char *pointer;
8648
8649 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8650 pointer = TYPE_ALLOC (type, num_bytes);
8651 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8652 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8653 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8654 }
8655
8656 /* Copy the saved-up fields into the field vector. Start from the head of
8657 the list, adding to the tail of the field array, so that they end up in
8658 the same order in the array in which they were added to the list. */
8659 while (nfields-- > 0)
8660 {
8661 struct nextfield *fieldp;
8662
8663 if (fip->fields)
8664 {
8665 fieldp = fip->fields;
8666 fip->fields = fieldp->next;
8667 }
8668 else
8669 {
8670 fieldp = fip->baseclasses;
8671 fip->baseclasses = fieldp->next;
8672 }
8673
8674 TYPE_FIELD (type, nfields) = fieldp->field;
8675 switch (fieldp->accessibility)
8676 {
8677 case DW_ACCESS_private:
8678 if (cu->language != language_ada)
8679 SET_TYPE_FIELD_PRIVATE (type, nfields);
8680 break;
8681
8682 case DW_ACCESS_protected:
8683 if (cu->language != language_ada)
8684 SET_TYPE_FIELD_PROTECTED (type, nfields);
8685 break;
8686
8687 case DW_ACCESS_public:
8688 break;
8689
8690 default:
8691 /* Unknown accessibility. Complain and treat it as public. */
8692 {
8693 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8694 fieldp->accessibility);
8695 }
8696 break;
8697 }
8698 if (nfields < fip->nbaseclasses)
8699 {
8700 switch (fieldp->virtuality)
8701 {
8702 case DW_VIRTUALITY_virtual:
8703 case DW_VIRTUALITY_pure_virtual:
8704 if (cu->language == language_ada)
8705 error (_("unexpected virtuality in component of Ada type"));
8706 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8707 break;
8708 }
8709 }
8710 }
8711 }
8712
8713 /* Add a member function to the proper fieldlist. */
8714
8715 static void
8716 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8717 struct type *type, struct dwarf2_cu *cu)
8718 {
8719 struct objfile *objfile = cu->objfile;
8720 struct attribute *attr;
8721 struct fnfieldlist *flp;
8722 int i;
8723 struct fn_field *fnp;
8724 char *fieldname;
8725 struct nextfnfield *new_fnfield;
8726 struct type *this_type;
8727 enum dwarf_access_attribute accessibility;
8728
8729 if (cu->language == language_ada)
8730 error (_("unexpected member function in Ada type"));
8731
8732 /* Get name of member function. */
8733 fieldname = dwarf2_name (die, cu);
8734 if (fieldname == NULL)
8735 return;
8736
8737 /* Look up member function name in fieldlist. */
8738 for (i = 0; i < fip->nfnfields; i++)
8739 {
8740 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8741 break;
8742 }
8743
8744 /* Create new list element if necessary. */
8745 if (i < fip->nfnfields)
8746 flp = &fip->fnfieldlists[i];
8747 else
8748 {
8749 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8750 {
8751 fip->fnfieldlists = (struct fnfieldlist *)
8752 xrealloc (fip->fnfieldlists,
8753 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8754 * sizeof (struct fnfieldlist));
8755 if (fip->nfnfields == 0)
8756 make_cleanup (free_current_contents, &fip->fnfieldlists);
8757 }
8758 flp = &fip->fnfieldlists[fip->nfnfields];
8759 flp->name = fieldname;
8760 flp->length = 0;
8761 flp->head = NULL;
8762 i = fip->nfnfields++;
8763 }
8764
8765 /* Create a new member function field and chain it to the field list
8766 entry. */
8767 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8768 make_cleanup (xfree, new_fnfield);
8769 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8770 new_fnfield->next = flp->head;
8771 flp->head = new_fnfield;
8772 flp->length++;
8773
8774 /* Fill in the member function field info. */
8775 fnp = &new_fnfield->fnfield;
8776
8777 /* Delay processing of the physname until later. */
8778 if (cu->language == language_cplus || cu->language == language_java)
8779 {
8780 add_to_method_list (type, i, flp->length - 1, fieldname,
8781 die, cu);
8782 }
8783 else
8784 {
8785 const char *physname = dwarf2_physname (fieldname, die, cu);
8786 fnp->physname = physname ? physname : "";
8787 }
8788
8789 fnp->type = alloc_type (objfile);
8790 this_type = read_type_die (die, cu);
8791 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8792 {
8793 int nparams = TYPE_NFIELDS (this_type);
8794
8795 /* TYPE is the domain of this method, and THIS_TYPE is the type
8796 of the method itself (TYPE_CODE_METHOD). */
8797 smash_to_method_type (fnp->type, type,
8798 TYPE_TARGET_TYPE (this_type),
8799 TYPE_FIELDS (this_type),
8800 TYPE_NFIELDS (this_type),
8801 TYPE_VARARGS (this_type));
8802
8803 /* Handle static member functions.
8804 Dwarf2 has no clean way to discern C++ static and non-static
8805 member functions. G++ helps GDB by marking the first
8806 parameter for non-static member functions (which is the this
8807 pointer) as artificial. We obtain this information from
8808 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8809 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8810 fnp->voffset = VOFFSET_STATIC;
8811 }
8812 else
8813 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8814 dwarf2_full_name (fieldname, die, cu));
8815
8816 /* Get fcontext from DW_AT_containing_type if present. */
8817 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8818 fnp->fcontext = die_containing_type (die, cu);
8819
8820 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8821 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8822
8823 /* Get accessibility. */
8824 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8825 if (attr)
8826 accessibility = DW_UNSND (attr);
8827 else
8828 accessibility = dwarf2_default_access_attribute (die, cu);
8829 switch (accessibility)
8830 {
8831 case DW_ACCESS_private:
8832 fnp->is_private = 1;
8833 break;
8834 case DW_ACCESS_protected:
8835 fnp->is_protected = 1;
8836 break;
8837 }
8838
8839 /* Check for artificial methods. */
8840 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8841 if (attr && DW_UNSND (attr) != 0)
8842 fnp->is_artificial = 1;
8843
8844 /* Get index in virtual function table if it is a virtual member
8845 function. For older versions of GCC, this is an offset in the
8846 appropriate virtual table, as specified by DW_AT_containing_type.
8847 For everyone else, it is an expression to be evaluated relative
8848 to the object address. */
8849
8850 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8851 if (attr)
8852 {
8853 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8854 {
8855 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8856 {
8857 /* Old-style GCC. */
8858 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8859 }
8860 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8861 || (DW_BLOCK (attr)->size > 1
8862 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8863 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8864 {
8865 struct dwarf_block blk;
8866 int offset;
8867
8868 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8869 ? 1 : 2);
8870 blk.size = DW_BLOCK (attr)->size - offset;
8871 blk.data = DW_BLOCK (attr)->data + offset;
8872 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8873 if ((fnp->voffset % cu->header.addr_size) != 0)
8874 dwarf2_complex_location_expr_complaint ();
8875 else
8876 fnp->voffset /= cu->header.addr_size;
8877 fnp->voffset += 2;
8878 }
8879 else
8880 dwarf2_complex_location_expr_complaint ();
8881
8882 if (!fnp->fcontext)
8883 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8884 }
8885 else if (attr_form_is_section_offset (attr))
8886 {
8887 dwarf2_complex_location_expr_complaint ();
8888 }
8889 else
8890 {
8891 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8892 fieldname);
8893 }
8894 }
8895 else
8896 {
8897 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8898 if (attr && DW_UNSND (attr))
8899 {
8900 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8901 complaint (&symfile_complaints,
8902 _("Member function \"%s\" (offset %d) is virtual "
8903 "but the vtable offset is not specified"),
8904 fieldname, die->offset.sect_off);
8905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8906 TYPE_CPLUS_DYNAMIC (type) = 1;
8907 }
8908 }
8909 }
8910
8911 /* Create the vector of member function fields, and attach it to the type. */
8912
8913 static void
8914 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8915 struct dwarf2_cu *cu)
8916 {
8917 struct fnfieldlist *flp;
8918 int i;
8919
8920 if (cu->language == language_ada)
8921 error (_("unexpected member functions in Ada type"));
8922
8923 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8924 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8925 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8926
8927 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8928 {
8929 struct nextfnfield *nfp = flp->head;
8930 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8931 int k;
8932
8933 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8934 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8935 fn_flp->fn_fields = (struct fn_field *)
8936 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8937 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8938 fn_flp->fn_fields[k] = nfp->fnfield;
8939 }
8940
8941 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8942 }
8943
8944 /* Returns non-zero if NAME is the name of a vtable member in CU's
8945 language, zero otherwise. */
8946 static int
8947 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8948 {
8949 static const char vptr[] = "_vptr";
8950 static const char vtable[] = "vtable";
8951
8952 /* Look for the C++ and Java forms of the vtable. */
8953 if ((cu->language == language_java
8954 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8955 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8956 && is_cplus_marker (name[sizeof (vptr) - 1])))
8957 return 1;
8958
8959 return 0;
8960 }
8961
8962 /* GCC outputs unnamed structures that are really pointers to member
8963 functions, with the ABI-specified layout. If TYPE describes
8964 such a structure, smash it into a member function type.
8965
8966 GCC shouldn't do this; it should just output pointer to member DIEs.
8967 This is GCC PR debug/28767. */
8968
8969 static void
8970 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8971 {
8972 struct type *pfn_type, *domain_type, *new_type;
8973
8974 /* Check for a structure with no name and two children. */
8975 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8976 return;
8977
8978 /* Check for __pfn and __delta members. */
8979 if (TYPE_FIELD_NAME (type, 0) == NULL
8980 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8981 || TYPE_FIELD_NAME (type, 1) == NULL
8982 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8983 return;
8984
8985 /* Find the type of the method. */
8986 pfn_type = TYPE_FIELD_TYPE (type, 0);
8987 if (pfn_type == NULL
8988 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8989 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8990 return;
8991
8992 /* Look for the "this" argument. */
8993 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8994 if (TYPE_NFIELDS (pfn_type) == 0
8995 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8996 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8997 return;
8998
8999 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9000 new_type = alloc_type (objfile);
9001 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9002 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9003 TYPE_VARARGS (pfn_type));
9004 smash_to_methodptr_type (type, new_type);
9005 }
9006
9007 /* Called when we find the DIE that starts a structure or union scope
9008 (definition) to create a type for the structure or union. Fill in
9009 the type's name and general properties; the members will not be
9010 processed until process_structure_type.
9011
9012 NOTE: we need to call these functions regardless of whether or not the
9013 DIE has a DW_AT_name attribute, since it might be an anonymous
9014 structure or union. This gets the type entered into our set of
9015 user defined types.
9016
9017 However, if the structure is incomplete (an opaque struct/union)
9018 then suppress creating a symbol table entry for it since gdb only
9019 wants to find the one with the complete definition. Note that if
9020 it is complete, we just call new_symbol, which does it's own
9021 checking about whether the struct/union is anonymous or not (and
9022 suppresses creating a symbol table entry itself). */
9023
9024 static struct type *
9025 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9026 {
9027 struct objfile *objfile = cu->objfile;
9028 struct type *type;
9029 struct attribute *attr;
9030 char *name;
9031
9032 /* If the definition of this type lives in .debug_types, read that type.
9033 Don't follow DW_AT_specification though, that will take us back up
9034 the chain and we want to go down. */
9035 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9036 if (attr)
9037 {
9038 struct dwarf2_cu *type_cu = cu;
9039 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9040
9041 /* We could just recurse on read_structure_type, but we need to call
9042 get_die_type to ensure only one type for this DIE is created.
9043 This is important, for example, because for c++ classes we need
9044 TYPE_NAME set which is only done by new_symbol. Blech. */
9045 type = read_type_die (type_die, type_cu);
9046
9047 /* TYPE_CU may not be the same as CU.
9048 Ensure TYPE is recorded in CU's type_hash table. */
9049 return set_die_type (die, type, cu);
9050 }
9051
9052 type = alloc_type (objfile);
9053 INIT_CPLUS_SPECIFIC (type);
9054
9055 name = dwarf2_name (die, cu);
9056 if (name != NULL)
9057 {
9058 if (cu->language == language_cplus
9059 || cu->language == language_java)
9060 {
9061 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9062
9063 /* dwarf2_full_name might have already finished building the DIE's
9064 type. If so, there is no need to continue. */
9065 if (get_die_type (die, cu) != NULL)
9066 return get_die_type (die, cu);
9067
9068 TYPE_TAG_NAME (type) = full_name;
9069 if (die->tag == DW_TAG_structure_type
9070 || die->tag == DW_TAG_class_type)
9071 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9072 }
9073 else
9074 {
9075 /* The name is already allocated along with this objfile, so
9076 we don't need to duplicate it for the type. */
9077 TYPE_TAG_NAME (type) = (char *) name;
9078 if (die->tag == DW_TAG_class_type)
9079 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9080 }
9081 }
9082
9083 if (die->tag == DW_TAG_structure_type)
9084 {
9085 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9086 }
9087 else if (die->tag == DW_TAG_union_type)
9088 {
9089 TYPE_CODE (type) = TYPE_CODE_UNION;
9090 }
9091 else
9092 {
9093 TYPE_CODE (type) = TYPE_CODE_CLASS;
9094 }
9095
9096 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9097 TYPE_DECLARED_CLASS (type) = 1;
9098
9099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9100 if (attr)
9101 {
9102 TYPE_LENGTH (type) = DW_UNSND (attr);
9103 }
9104 else
9105 {
9106 TYPE_LENGTH (type) = 0;
9107 }
9108
9109 TYPE_STUB_SUPPORTED (type) = 1;
9110 if (die_is_declaration (die, cu))
9111 TYPE_STUB (type) = 1;
9112 else if (attr == NULL && die->child == NULL
9113 && producer_is_realview (cu->producer))
9114 /* RealView does not output the required DW_AT_declaration
9115 on incomplete types. */
9116 TYPE_STUB (type) = 1;
9117
9118 /* We need to add the type field to the die immediately so we don't
9119 infinitely recurse when dealing with pointers to the structure
9120 type within the structure itself. */
9121 set_die_type (die, type, cu);
9122
9123 /* set_die_type should be already done. */
9124 set_descriptive_type (type, die, cu);
9125
9126 return type;
9127 }
9128
9129 /* Finish creating a structure or union type, including filling in
9130 its members and creating a symbol for it. */
9131
9132 static void
9133 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9134 {
9135 struct objfile *objfile = cu->objfile;
9136 struct die_info *child_die = die->child;
9137 struct type *type;
9138
9139 type = get_die_type (die, cu);
9140 if (type == NULL)
9141 type = read_structure_type (die, cu);
9142
9143 if (die->child != NULL && ! die_is_declaration (die, cu))
9144 {
9145 struct field_info fi;
9146 struct die_info *child_die;
9147 VEC (symbolp) *template_args = NULL;
9148 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9149
9150 memset (&fi, 0, sizeof (struct field_info));
9151
9152 child_die = die->child;
9153
9154 while (child_die && child_die->tag)
9155 {
9156 if (child_die->tag == DW_TAG_member
9157 || child_die->tag == DW_TAG_variable)
9158 {
9159 /* NOTE: carlton/2002-11-05: A C++ static data member
9160 should be a DW_TAG_member that is a declaration, but
9161 all versions of G++ as of this writing (so through at
9162 least 3.2.1) incorrectly generate DW_TAG_variable
9163 tags for them instead. */
9164 dwarf2_add_field (&fi, child_die, cu);
9165 }
9166 else if (child_die->tag == DW_TAG_subprogram)
9167 {
9168 /* C++ member function. */
9169 dwarf2_add_member_fn (&fi, child_die, type, cu);
9170 }
9171 else if (child_die->tag == DW_TAG_inheritance)
9172 {
9173 /* C++ base class field. */
9174 dwarf2_add_field (&fi, child_die, cu);
9175 }
9176 else if (child_die->tag == DW_TAG_typedef)
9177 dwarf2_add_typedef (&fi, child_die, cu);
9178 else if (child_die->tag == DW_TAG_template_type_param
9179 || child_die->tag == DW_TAG_template_value_param)
9180 {
9181 struct symbol *arg = new_symbol (child_die, NULL, cu);
9182
9183 if (arg != NULL)
9184 VEC_safe_push (symbolp, template_args, arg);
9185 }
9186
9187 child_die = sibling_die (child_die);
9188 }
9189
9190 /* Attach template arguments to type. */
9191 if (! VEC_empty (symbolp, template_args))
9192 {
9193 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9194 TYPE_N_TEMPLATE_ARGUMENTS (type)
9195 = VEC_length (symbolp, template_args);
9196 TYPE_TEMPLATE_ARGUMENTS (type)
9197 = obstack_alloc (&objfile->objfile_obstack,
9198 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9199 * sizeof (struct symbol *)));
9200 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9201 VEC_address (symbolp, template_args),
9202 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9203 * sizeof (struct symbol *)));
9204 VEC_free (symbolp, template_args);
9205 }
9206
9207 /* Attach fields and member functions to the type. */
9208 if (fi.nfields)
9209 dwarf2_attach_fields_to_type (&fi, type, cu);
9210 if (fi.nfnfields)
9211 {
9212 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9213
9214 /* Get the type which refers to the base class (possibly this
9215 class itself) which contains the vtable pointer for the current
9216 class from the DW_AT_containing_type attribute. This use of
9217 DW_AT_containing_type is a GNU extension. */
9218
9219 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9220 {
9221 struct type *t = die_containing_type (die, cu);
9222
9223 TYPE_VPTR_BASETYPE (type) = t;
9224 if (type == t)
9225 {
9226 int i;
9227
9228 /* Our own class provides vtbl ptr. */
9229 for (i = TYPE_NFIELDS (t) - 1;
9230 i >= TYPE_N_BASECLASSES (t);
9231 --i)
9232 {
9233 const char *fieldname = TYPE_FIELD_NAME (t, i);
9234
9235 if (is_vtable_name (fieldname, cu))
9236 {
9237 TYPE_VPTR_FIELDNO (type) = i;
9238 break;
9239 }
9240 }
9241
9242 /* Complain if virtual function table field not found. */
9243 if (i < TYPE_N_BASECLASSES (t))
9244 complaint (&symfile_complaints,
9245 _("virtual function table pointer "
9246 "not found when defining class '%s'"),
9247 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9248 "");
9249 }
9250 else
9251 {
9252 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9253 }
9254 }
9255 else if (cu->producer
9256 && strncmp (cu->producer,
9257 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9258 {
9259 /* The IBM XLC compiler does not provide direct indication
9260 of the containing type, but the vtable pointer is
9261 always named __vfp. */
9262
9263 int i;
9264
9265 for (i = TYPE_NFIELDS (type) - 1;
9266 i >= TYPE_N_BASECLASSES (type);
9267 --i)
9268 {
9269 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9270 {
9271 TYPE_VPTR_FIELDNO (type) = i;
9272 TYPE_VPTR_BASETYPE (type) = type;
9273 break;
9274 }
9275 }
9276 }
9277 }
9278
9279 /* Copy fi.typedef_field_list linked list elements content into the
9280 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9281 if (fi.typedef_field_list)
9282 {
9283 int i = fi.typedef_field_list_count;
9284
9285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9286 TYPE_TYPEDEF_FIELD_ARRAY (type)
9287 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9288 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9289
9290 /* Reverse the list order to keep the debug info elements order. */
9291 while (--i >= 0)
9292 {
9293 struct typedef_field *dest, *src;
9294
9295 dest = &TYPE_TYPEDEF_FIELD (type, i);
9296 src = &fi.typedef_field_list->field;
9297 fi.typedef_field_list = fi.typedef_field_list->next;
9298 *dest = *src;
9299 }
9300 }
9301
9302 do_cleanups (back_to);
9303
9304 if (HAVE_CPLUS_STRUCT (type))
9305 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9306 }
9307
9308 quirk_gcc_member_function_pointer (type, objfile);
9309
9310 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9311 snapshots) has been known to create a die giving a declaration
9312 for a class that has, as a child, a die giving a definition for a
9313 nested class. So we have to process our children even if the
9314 current die is a declaration. Normally, of course, a declaration
9315 won't have any children at all. */
9316
9317 while (child_die != NULL && child_die->tag)
9318 {
9319 if (child_die->tag == DW_TAG_member
9320 || child_die->tag == DW_TAG_variable
9321 || child_die->tag == DW_TAG_inheritance
9322 || child_die->tag == DW_TAG_template_value_param
9323 || child_die->tag == DW_TAG_template_type_param)
9324 {
9325 /* Do nothing. */
9326 }
9327 else
9328 process_die (child_die, cu);
9329
9330 child_die = sibling_die (child_die);
9331 }
9332
9333 /* Do not consider external references. According to the DWARF standard,
9334 these DIEs are identified by the fact that they have no byte_size
9335 attribute, and a declaration attribute. */
9336 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9337 || !die_is_declaration (die, cu))
9338 new_symbol (die, type, cu);
9339 }
9340
9341 /* Given a DW_AT_enumeration_type die, set its type. We do not
9342 complete the type's fields yet, or create any symbols. */
9343
9344 static struct type *
9345 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9346 {
9347 struct objfile *objfile = cu->objfile;
9348 struct type *type;
9349 struct attribute *attr;
9350 const char *name;
9351
9352 /* If the definition of this type lives in .debug_types, read that type.
9353 Don't follow DW_AT_specification though, that will take us back up
9354 the chain and we want to go down. */
9355 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9356 if (attr)
9357 {
9358 struct dwarf2_cu *type_cu = cu;
9359 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9360
9361 type = read_type_die (type_die, type_cu);
9362
9363 /* TYPE_CU may not be the same as CU.
9364 Ensure TYPE is recorded in CU's type_hash table. */
9365 return set_die_type (die, type, cu);
9366 }
9367
9368 type = alloc_type (objfile);
9369
9370 TYPE_CODE (type) = TYPE_CODE_ENUM;
9371 name = dwarf2_full_name (NULL, die, cu);
9372 if (name != NULL)
9373 TYPE_TAG_NAME (type) = (char *) name;
9374
9375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9376 if (attr)
9377 {
9378 TYPE_LENGTH (type) = DW_UNSND (attr);
9379 }
9380 else
9381 {
9382 TYPE_LENGTH (type) = 0;
9383 }
9384
9385 /* The enumeration DIE can be incomplete. In Ada, any type can be
9386 declared as private in the package spec, and then defined only
9387 inside the package body. Such types are known as Taft Amendment
9388 Types. When another package uses such a type, an incomplete DIE
9389 may be generated by the compiler. */
9390 if (die_is_declaration (die, cu))
9391 TYPE_STUB (type) = 1;
9392
9393 return set_die_type (die, type, cu);
9394 }
9395
9396 /* Given a pointer to a die which begins an enumeration, process all
9397 the dies that define the members of the enumeration, and create the
9398 symbol for the enumeration type.
9399
9400 NOTE: We reverse the order of the element list. */
9401
9402 static void
9403 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9404 {
9405 struct type *this_type;
9406
9407 this_type = get_die_type (die, cu);
9408 if (this_type == NULL)
9409 this_type = read_enumeration_type (die, cu);
9410
9411 if (die->child != NULL)
9412 {
9413 struct die_info *child_die;
9414 struct symbol *sym;
9415 struct field *fields = NULL;
9416 int num_fields = 0;
9417 int unsigned_enum = 1;
9418 char *name;
9419 int flag_enum = 1;
9420 ULONGEST mask = 0;
9421
9422 child_die = die->child;
9423 while (child_die && child_die->tag)
9424 {
9425 if (child_die->tag != DW_TAG_enumerator)
9426 {
9427 process_die (child_die, cu);
9428 }
9429 else
9430 {
9431 name = dwarf2_name (child_die, cu);
9432 if (name)
9433 {
9434 sym = new_symbol (child_die, this_type, cu);
9435 if (SYMBOL_VALUE (sym) < 0)
9436 {
9437 unsigned_enum = 0;
9438 flag_enum = 0;
9439 }
9440 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9441 flag_enum = 0;
9442 else
9443 mask |= SYMBOL_VALUE (sym);
9444
9445 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9446 {
9447 fields = (struct field *)
9448 xrealloc (fields,
9449 (num_fields + DW_FIELD_ALLOC_CHUNK)
9450 * sizeof (struct field));
9451 }
9452
9453 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9454 FIELD_TYPE (fields[num_fields]) = NULL;
9455 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9456 FIELD_BITSIZE (fields[num_fields]) = 0;
9457
9458 num_fields++;
9459 }
9460 }
9461
9462 child_die = sibling_die (child_die);
9463 }
9464
9465 if (num_fields)
9466 {
9467 TYPE_NFIELDS (this_type) = num_fields;
9468 TYPE_FIELDS (this_type) = (struct field *)
9469 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9470 memcpy (TYPE_FIELDS (this_type), fields,
9471 sizeof (struct field) * num_fields);
9472 xfree (fields);
9473 }
9474 if (unsigned_enum)
9475 TYPE_UNSIGNED (this_type) = 1;
9476 if (flag_enum)
9477 TYPE_FLAG_ENUM (this_type) = 1;
9478 }
9479
9480 /* If we are reading an enum from a .debug_types unit, and the enum
9481 is a declaration, and the enum is not the signatured type in the
9482 unit, then we do not want to add a symbol for it. Adding a
9483 symbol would in some cases obscure the true definition of the
9484 enum, giving users an incomplete type when the definition is
9485 actually available. Note that we do not want to do this for all
9486 enums which are just declarations, because C++0x allows forward
9487 enum declarations. */
9488 if (cu->per_cu->is_debug_types
9489 && die_is_declaration (die, cu))
9490 {
9491 struct signatured_type *sig_type;
9492
9493 sig_type
9494 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9495 cu->per_cu->info_or_types_section,
9496 cu->per_cu->offset);
9497 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9498 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9499 return;
9500 }
9501
9502 new_symbol (die, this_type, cu);
9503 }
9504
9505 /* Extract all information from a DW_TAG_array_type DIE and put it in
9506 the DIE's type field. For now, this only handles one dimensional
9507 arrays. */
9508
9509 static struct type *
9510 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9511 {
9512 struct objfile *objfile = cu->objfile;
9513 struct die_info *child_die;
9514 struct type *type;
9515 struct type *element_type, *range_type, *index_type;
9516 struct type **range_types = NULL;
9517 struct attribute *attr;
9518 int ndim = 0;
9519 struct cleanup *back_to;
9520 char *name;
9521
9522 element_type = die_type (die, cu);
9523
9524 /* The die_type call above may have already set the type for this DIE. */
9525 type = get_die_type (die, cu);
9526 if (type)
9527 return type;
9528
9529 /* Irix 6.2 native cc creates array types without children for
9530 arrays with unspecified length. */
9531 if (die->child == NULL)
9532 {
9533 index_type = objfile_type (objfile)->builtin_int;
9534 range_type = create_range_type (NULL, index_type, 0, -1);
9535 type = create_array_type (NULL, element_type, range_type);
9536 return set_die_type (die, type, cu);
9537 }
9538
9539 back_to = make_cleanup (null_cleanup, NULL);
9540 child_die = die->child;
9541 while (child_die && child_die->tag)
9542 {
9543 if (child_die->tag == DW_TAG_subrange_type)
9544 {
9545 struct type *child_type = read_type_die (child_die, cu);
9546
9547 if (child_type != NULL)
9548 {
9549 /* The range type was succesfully read. Save it for the
9550 array type creation. */
9551 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9552 {
9553 range_types = (struct type **)
9554 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9555 * sizeof (struct type *));
9556 if (ndim == 0)
9557 make_cleanup (free_current_contents, &range_types);
9558 }
9559 range_types[ndim++] = child_type;
9560 }
9561 }
9562 child_die = sibling_die (child_die);
9563 }
9564
9565 /* Dwarf2 dimensions are output from left to right, create the
9566 necessary array types in backwards order. */
9567
9568 type = element_type;
9569
9570 if (read_array_order (die, cu) == DW_ORD_col_major)
9571 {
9572 int i = 0;
9573
9574 while (i < ndim)
9575 type = create_array_type (NULL, type, range_types[i++]);
9576 }
9577 else
9578 {
9579 while (ndim-- > 0)
9580 type = create_array_type (NULL, type, range_types[ndim]);
9581 }
9582
9583 /* Understand Dwarf2 support for vector types (like they occur on
9584 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9585 array type. This is not part of the Dwarf2/3 standard yet, but a
9586 custom vendor extension. The main difference between a regular
9587 array and the vector variant is that vectors are passed by value
9588 to functions. */
9589 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9590 if (attr)
9591 make_vector_type (type);
9592
9593 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9594 implementation may choose to implement triple vectors using this
9595 attribute. */
9596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9597 if (attr)
9598 {
9599 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9600 TYPE_LENGTH (type) = DW_UNSND (attr);
9601 else
9602 complaint (&symfile_complaints,
9603 _("DW_AT_byte_size for array type smaller "
9604 "than the total size of elements"));
9605 }
9606
9607 name = dwarf2_name (die, cu);
9608 if (name)
9609 TYPE_NAME (type) = name;
9610
9611 /* Install the type in the die. */
9612 set_die_type (die, type, cu);
9613
9614 /* set_die_type should be already done. */
9615 set_descriptive_type (type, die, cu);
9616
9617 do_cleanups (back_to);
9618
9619 return type;
9620 }
9621
9622 static enum dwarf_array_dim_ordering
9623 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9624 {
9625 struct attribute *attr;
9626
9627 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9628
9629 if (attr) return DW_SND (attr);
9630
9631 /* GNU F77 is a special case, as at 08/2004 array type info is the
9632 opposite order to the dwarf2 specification, but data is still
9633 laid out as per normal fortran.
9634
9635 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9636 version checking. */
9637
9638 if (cu->language == language_fortran
9639 && cu->producer && strstr (cu->producer, "GNU F77"))
9640 {
9641 return DW_ORD_row_major;
9642 }
9643
9644 switch (cu->language_defn->la_array_ordering)
9645 {
9646 case array_column_major:
9647 return DW_ORD_col_major;
9648 case array_row_major:
9649 default:
9650 return DW_ORD_row_major;
9651 };
9652 }
9653
9654 /* Extract all information from a DW_TAG_set_type DIE and put it in
9655 the DIE's type field. */
9656
9657 static struct type *
9658 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9659 {
9660 struct type *domain_type, *set_type;
9661 struct attribute *attr;
9662
9663 domain_type = die_type (die, cu);
9664
9665 /* The die_type call above may have already set the type for this DIE. */
9666 set_type = get_die_type (die, cu);
9667 if (set_type)
9668 return set_type;
9669
9670 set_type = create_set_type (NULL, domain_type);
9671
9672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9673 if (attr)
9674 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9675
9676 return set_die_type (die, set_type, cu);
9677 }
9678
9679 /* First cut: install each common block member as a global variable. */
9680
9681 static void
9682 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9683 {
9684 struct die_info *child_die;
9685 struct attribute *attr;
9686 struct symbol *sym;
9687 CORE_ADDR base = (CORE_ADDR) 0;
9688
9689 attr = dwarf2_attr (die, DW_AT_location, cu);
9690 if (attr)
9691 {
9692 /* Support the .debug_loc offsets. */
9693 if (attr_form_is_block (attr))
9694 {
9695 base = decode_locdesc (DW_BLOCK (attr), cu);
9696 }
9697 else if (attr_form_is_section_offset (attr))
9698 {
9699 dwarf2_complex_location_expr_complaint ();
9700 }
9701 else
9702 {
9703 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9704 "common block member");
9705 }
9706 }
9707 if (die->child != NULL)
9708 {
9709 child_die = die->child;
9710 while (child_die && child_die->tag)
9711 {
9712 LONGEST offset;
9713
9714 sym = new_symbol (child_die, NULL, cu);
9715 if (sym != NULL
9716 && handle_data_member_location (child_die, cu, &offset))
9717 {
9718 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9719 add_symbol_to_list (sym, &global_symbols);
9720 }
9721 child_die = sibling_die (child_die);
9722 }
9723 }
9724 }
9725
9726 /* Create a type for a C++ namespace. */
9727
9728 static struct type *
9729 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9730 {
9731 struct objfile *objfile = cu->objfile;
9732 const char *previous_prefix, *name;
9733 int is_anonymous;
9734 struct type *type;
9735
9736 /* For extensions, reuse the type of the original namespace. */
9737 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9738 {
9739 struct die_info *ext_die;
9740 struct dwarf2_cu *ext_cu = cu;
9741
9742 ext_die = dwarf2_extension (die, &ext_cu);
9743 type = read_type_die (ext_die, ext_cu);
9744
9745 /* EXT_CU may not be the same as CU.
9746 Ensure TYPE is recorded in CU's type_hash table. */
9747 return set_die_type (die, type, cu);
9748 }
9749
9750 name = namespace_name (die, &is_anonymous, cu);
9751
9752 /* Now build the name of the current namespace. */
9753
9754 previous_prefix = determine_prefix (die, cu);
9755 if (previous_prefix[0] != '\0')
9756 name = typename_concat (&objfile->objfile_obstack,
9757 previous_prefix, name, 0, cu);
9758
9759 /* Create the type. */
9760 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9761 objfile);
9762 TYPE_NAME (type) = (char *) name;
9763 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9764
9765 return set_die_type (die, type, cu);
9766 }
9767
9768 /* Read a C++ namespace. */
9769
9770 static void
9771 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9772 {
9773 struct objfile *objfile = cu->objfile;
9774 int is_anonymous;
9775
9776 /* Add a symbol associated to this if we haven't seen the namespace
9777 before. Also, add a using directive if it's an anonymous
9778 namespace. */
9779
9780 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9781 {
9782 struct type *type;
9783
9784 type = read_type_die (die, cu);
9785 new_symbol (die, type, cu);
9786
9787 namespace_name (die, &is_anonymous, cu);
9788 if (is_anonymous)
9789 {
9790 const char *previous_prefix = determine_prefix (die, cu);
9791
9792 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9793 NULL, NULL, &objfile->objfile_obstack);
9794 }
9795 }
9796
9797 if (die->child != NULL)
9798 {
9799 struct die_info *child_die = die->child;
9800
9801 while (child_die && child_die->tag)
9802 {
9803 process_die (child_die, cu);
9804 child_die = sibling_die (child_die);
9805 }
9806 }
9807 }
9808
9809 /* Read a Fortran module as type. This DIE can be only a declaration used for
9810 imported module. Still we need that type as local Fortran "use ... only"
9811 declaration imports depend on the created type in determine_prefix. */
9812
9813 static struct type *
9814 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9815 {
9816 struct objfile *objfile = cu->objfile;
9817 char *module_name;
9818 struct type *type;
9819
9820 module_name = dwarf2_name (die, cu);
9821 if (!module_name)
9822 complaint (&symfile_complaints,
9823 _("DW_TAG_module has no name, offset 0x%x"),
9824 die->offset.sect_off);
9825 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9826
9827 /* determine_prefix uses TYPE_TAG_NAME. */
9828 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9829
9830 return set_die_type (die, type, cu);
9831 }
9832
9833 /* Read a Fortran module. */
9834
9835 static void
9836 read_module (struct die_info *die, struct dwarf2_cu *cu)
9837 {
9838 struct die_info *child_die = die->child;
9839
9840 while (child_die && child_die->tag)
9841 {
9842 process_die (child_die, cu);
9843 child_die = sibling_die (child_die);
9844 }
9845 }
9846
9847 /* Return the name of the namespace represented by DIE. Set
9848 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9849 namespace. */
9850
9851 static const char *
9852 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9853 {
9854 struct die_info *current_die;
9855 const char *name = NULL;
9856
9857 /* Loop through the extensions until we find a name. */
9858
9859 for (current_die = die;
9860 current_die != NULL;
9861 current_die = dwarf2_extension (die, &cu))
9862 {
9863 name = dwarf2_name (current_die, cu);
9864 if (name != NULL)
9865 break;
9866 }
9867
9868 /* Is it an anonymous namespace? */
9869
9870 *is_anonymous = (name == NULL);
9871 if (*is_anonymous)
9872 name = CP_ANONYMOUS_NAMESPACE_STR;
9873
9874 return name;
9875 }
9876
9877 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9878 the user defined type vector. */
9879
9880 static struct type *
9881 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9882 {
9883 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9884 struct comp_unit_head *cu_header = &cu->header;
9885 struct type *type;
9886 struct attribute *attr_byte_size;
9887 struct attribute *attr_address_class;
9888 int byte_size, addr_class;
9889 struct type *target_type;
9890
9891 target_type = die_type (die, cu);
9892
9893 /* The die_type call above may have already set the type for this DIE. */
9894 type = get_die_type (die, cu);
9895 if (type)
9896 return type;
9897
9898 type = lookup_pointer_type (target_type);
9899
9900 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9901 if (attr_byte_size)
9902 byte_size = DW_UNSND (attr_byte_size);
9903 else
9904 byte_size = cu_header->addr_size;
9905
9906 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9907 if (attr_address_class)
9908 addr_class = DW_UNSND (attr_address_class);
9909 else
9910 addr_class = DW_ADDR_none;
9911
9912 /* If the pointer size or address class is different than the
9913 default, create a type variant marked as such and set the
9914 length accordingly. */
9915 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9916 {
9917 if (gdbarch_address_class_type_flags_p (gdbarch))
9918 {
9919 int type_flags;
9920
9921 type_flags = gdbarch_address_class_type_flags
9922 (gdbarch, byte_size, addr_class);
9923 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9924 == 0);
9925 type = make_type_with_address_space (type, type_flags);
9926 }
9927 else if (TYPE_LENGTH (type) != byte_size)
9928 {
9929 complaint (&symfile_complaints,
9930 _("invalid pointer size %d"), byte_size);
9931 }
9932 else
9933 {
9934 /* Should we also complain about unhandled address classes? */
9935 }
9936 }
9937
9938 TYPE_LENGTH (type) = byte_size;
9939 return set_die_type (die, type, cu);
9940 }
9941
9942 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9943 the user defined type vector. */
9944
9945 static struct type *
9946 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9947 {
9948 struct type *type;
9949 struct type *to_type;
9950 struct type *domain;
9951
9952 to_type = die_type (die, cu);
9953 domain = die_containing_type (die, cu);
9954
9955 /* The calls above may have already set the type for this DIE. */
9956 type = get_die_type (die, cu);
9957 if (type)
9958 return type;
9959
9960 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9961 type = lookup_methodptr_type (to_type);
9962 else
9963 type = lookup_memberptr_type (to_type, domain);
9964
9965 return set_die_type (die, type, cu);
9966 }
9967
9968 /* Extract all information from a DW_TAG_reference_type DIE and add to
9969 the user defined type vector. */
9970
9971 static struct type *
9972 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9973 {
9974 struct comp_unit_head *cu_header = &cu->header;
9975 struct type *type, *target_type;
9976 struct attribute *attr;
9977
9978 target_type = die_type (die, cu);
9979
9980 /* The die_type call above may have already set the type for this DIE. */
9981 type = get_die_type (die, cu);
9982 if (type)
9983 return type;
9984
9985 type = lookup_reference_type (target_type);
9986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9987 if (attr)
9988 {
9989 TYPE_LENGTH (type) = DW_UNSND (attr);
9990 }
9991 else
9992 {
9993 TYPE_LENGTH (type) = cu_header->addr_size;
9994 }
9995 return set_die_type (die, type, cu);
9996 }
9997
9998 static struct type *
9999 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10000 {
10001 struct type *base_type, *cv_type;
10002
10003 base_type = die_type (die, cu);
10004
10005 /* The die_type call above may have already set the type for this DIE. */
10006 cv_type = get_die_type (die, cu);
10007 if (cv_type)
10008 return cv_type;
10009
10010 /* In case the const qualifier is applied to an array type, the element type
10011 is so qualified, not the array type (section 6.7.3 of C99). */
10012 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10013 {
10014 struct type *el_type, *inner_array;
10015
10016 base_type = copy_type (base_type);
10017 inner_array = base_type;
10018
10019 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10020 {
10021 TYPE_TARGET_TYPE (inner_array) =
10022 copy_type (TYPE_TARGET_TYPE (inner_array));
10023 inner_array = TYPE_TARGET_TYPE (inner_array);
10024 }
10025
10026 el_type = TYPE_TARGET_TYPE (inner_array);
10027 TYPE_TARGET_TYPE (inner_array) =
10028 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10029
10030 return set_die_type (die, base_type, cu);
10031 }
10032
10033 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10034 return set_die_type (die, cv_type, cu);
10035 }
10036
10037 static struct type *
10038 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10039 {
10040 struct type *base_type, *cv_type;
10041
10042 base_type = die_type (die, cu);
10043
10044 /* The die_type call above may have already set the type for this DIE. */
10045 cv_type = get_die_type (die, cu);
10046 if (cv_type)
10047 return cv_type;
10048
10049 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10050 return set_die_type (die, cv_type, cu);
10051 }
10052
10053 /* Extract all information from a DW_TAG_string_type DIE and add to
10054 the user defined type vector. It isn't really a user defined type,
10055 but it behaves like one, with other DIE's using an AT_user_def_type
10056 attribute to reference it. */
10057
10058 static struct type *
10059 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10060 {
10061 struct objfile *objfile = cu->objfile;
10062 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10063 struct type *type, *range_type, *index_type, *char_type;
10064 struct attribute *attr;
10065 unsigned int length;
10066
10067 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10068 if (attr)
10069 {
10070 length = DW_UNSND (attr);
10071 }
10072 else
10073 {
10074 /* Check for the DW_AT_byte_size attribute. */
10075 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10076 if (attr)
10077 {
10078 length = DW_UNSND (attr);
10079 }
10080 else
10081 {
10082 length = 1;
10083 }
10084 }
10085
10086 index_type = objfile_type (objfile)->builtin_int;
10087 range_type = create_range_type (NULL, index_type, 1, length);
10088 char_type = language_string_char_type (cu->language_defn, gdbarch);
10089 type = create_string_type (NULL, char_type, range_type);
10090
10091 return set_die_type (die, type, cu);
10092 }
10093
10094 /* Handle DIES due to C code like:
10095
10096 struct foo
10097 {
10098 int (*funcp)(int a, long l);
10099 int b;
10100 };
10101
10102 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10103
10104 static struct type *
10105 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10106 {
10107 struct objfile *objfile = cu->objfile;
10108 struct type *type; /* Type that this function returns. */
10109 struct type *ftype; /* Function that returns above type. */
10110 struct attribute *attr;
10111
10112 type = die_type (die, cu);
10113
10114 /* The die_type call above may have already set the type for this DIE. */
10115 ftype = get_die_type (die, cu);
10116 if (ftype)
10117 return ftype;
10118
10119 ftype = lookup_function_type (type);
10120
10121 /* All functions in C++, Pascal and Java have prototypes. */
10122 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10123 if ((attr && (DW_UNSND (attr) != 0))
10124 || cu->language == language_cplus
10125 || cu->language == language_java
10126 || cu->language == language_pascal)
10127 TYPE_PROTOTYPED (ftype) = 1;
10128 else if (producer_is_realview (cu->producer))
10129 /* RealView does not emit DW_AT_prototyped. We can not
10130 distinguish prototyped and unprototyped functions; default to
10131 prototyped, since that is more common in modern code (and
10132 RealView warns about unprototyped functions). */
10133 TYPE_PROTOTYPED (ftype) = 1;
10134
10135 /* Store the calling convention in the type if it's available in
10136 the subroutine die. Otherwise set the calling convention to
10137 the default value DW_CC_normal. */
10138 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10139 if (attr)
10140 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10141 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10142 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10143 else
10144 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10145
10146 /* We need to add the subroutine type to the die immediately so
10147 we don't infinitely recurse when dealing with parameters
10148 declared as the same subroutine type. */
10149 set_die_type (die, ftype, cu);
10150
10151 if (die->child != NULL)
10152 {
10153 struct type *void_type = objfile_type (objfile)->builtin_void;
10154 struct die_info *child_die;
10155 int nparams, iparams;
10156
10157 /* Count the number of parameters.
10158 FIXME: GDB currently ignores vararg functions, but knows about
10159 vararg member functions. */
10160 nparams = 0;
10161 child_die = die->child;
10162 while (child_die && child_die->tag)
10163 {
10164 if (child_die->tag == DW_TAG_formal_parameter)
10165 nparams++;
10166 else if (child_die->tag == DW_TAG_unspecified_parameters)
10167 TYPE_VARARGS (ftype) = 1;
10168 child_die = sibling_die (child_die);
10169 }
10170
10171 /* Allocate storage for parameters and fill them in. */
10172 TYPE_NFIELDS (ftype) = nparams;
10173 TYPE_FIELDS (ftype) = (struct field *)
10174 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10175
10176 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10177 even if we error out during the parameters reading below. */
10178 for (iparams = 0; iparams < nparams; iparams++)
10179 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10180
10181 iparams = 0;
10182 child_die = die->child;
10183 while (child_die && child_die->tag)
10184 {
10185 if (child_die->tag == DW_TAG_formal_parameter)
10186 {
10187 struct type *arg_type;
10188
10189 /* DWARF version 2 has no clean way to discern C++
10190 static and non-static member functions. G++ helps
10191 GDB by marking the first parameter for non-static
10192 member functions (which is the this pointer) as
10193 artificial. We pass this information to
10194 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10195
10196 DWARF version 3 added DW_AT_object_pointer, which GCC
10197 4.5 does not yet generate. */
10198 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10199 if (attr)
10200 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10201 else
10202 {
10203 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10204
10205 /* GCC/43521: In java, the formal parameter
10206 "this" is sometimes not marked with DW_AT_artificial. */
10207 if (cu->language == language_java)
10208 {
10209 const char *name = dwarf2_name (child_die, cu);
10210
10211 if (name && !strcmp (name, "this"))
10212 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10213 }
10214 }
10215 arg_type = die_type (child_die, cu);
10216
10217 /* RealView does not mark THIS as const, which the testsuite
10218 expects. GCC marks THIS as const in method definitions,
10219 but not in the class specifications (GCC PR 43053). */
10220 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10221 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10222 {
10223 int is_this = 0;
10224 struct dwarf2_cu *arg_cu = cu;
10225 const char *name = dwarf2_name (child_die, cu);
10226
10227 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10228 if (attr)
10229 {
10230 /* If the compiler emits this, use it. */
10231 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10232 is_this = 1;
10233 }
10234 else if (name && strcmp (name, "this") == 0)
10235 /* Function definitions will have the argument names. */
10236 is_this = 1;
10237 else if (name == NULL && iparams == 0)
10238 /* Declarations may not have the names, so like
10239 elsewhere in GDB, assume an artificial first
10240 argument is "this". */
10241 is_this = 1;
10242
10243 if (is_this)
10244 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10245 arg_type, 0);
10246 }
10247
10248 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10249 iparams++;
10250 }
10251 child_die = sibling_die (child_die);
10252 }
10253 }
10254
10255 return ftype;
10256 }
10257
10258 static struct type *
10259 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10260 {
10261 struct objfile *objfile = cu->objfile;
10262 const char *name = NULL;
10263 struct type *this_type, *target_type;
10264
10265 name = dwarf2_full_name (NULL, die, cu);
10266 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10267 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10268 TYPE_NAME (this_type) = (char *) name;
10269 set_die_type (die, this_type, cu);
10270 target_type = die_type (die, cu);
10271 if (target_type != this_type)
10272 TYPE_TARGET_TYPE (this_type) = target_type;
10273 else
10274 {
10275 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10276 spec and cause infinite loops in GDB. */
10277 complaint (&symfile_complaints,
10278 _("Self-referential DW_TAG_typedef "
10279 "- DIE at 0x%x [in module %s]"),
10280 die->offset.sect_off, objfile->name);
10281 TYPE_TARGET_TYPE (this_type) = NULL;
10282 }
10283 return this_type;
10284 }
10285
10286 /* Find a representation of a given base type and install
10287 it in the TYPE field of the die. */
10288
10289 static struct type *
10290 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10291 {
10292 struct objfile *objfile = cu->objfile;
10293 struct type *type;
10294 struct attribute *attr;
10295 int encoding = 0, size = 0;
10296 char *name;
10297 enum type_code code = TYPE_CODE_INT;
10298 int type_flags = 0;
10299 struct type *target_type = NULL;
10300
10301 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10302 if (attr)
10303 {
10304 encoding = DW_UNSND (attr);
10305 }
10306 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10307 if (attr)
10308 {
10309 size = DW_UNSND (attr);
10310 }
10311 name = dwarf2_name (die, cu);
10312 if (!name)
10313 {
10314 complaint (&symfile_complaints,
10315 _("DW_AT_name missing from DW_TAG_base_type"));
10316 }
10317
10318 switch (encoding)
10319 {
10320 case DW_ATE_address:
10321 /* Turn DW_ATE_address into a void * pointer. */
10322 code = TYPE_CODE_PTR;
10323 type_flags |= TYPE_FLAG_UNSIGNED;
10324 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10325 break;
10326 case DW_ATE_boolean:
10327 code = TYPE_CODE_BOOL;
10328 type_flags |= TYPE_FLAG_UNSIGNED;
10329 break;
10330 case DW_ATE_complex_float:
10331 code = TYPE_CODE_COMPLEX;
10332 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10333 break;
10334 case DW_ATE_decimal_float:
10335 code = TYPE_CODE_DECFLOAT;
10336 break;
10337 case DW_ATE_float:
10338 code = TYPE_CODE_FLT;
10339 break;
10340 case DW_ATE_signed:
10341 break;
10342 case DW_ATE_unsigned:
10343 type_flags |= TYPE_FLAG_UNSIGNED;
10344 if (cu->language == language_fortran
10345 && name
10346 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10347 code = TYPE_CODE_CHAR;
10348 break;
10349 case DW_ATE_signed_char:
10350 if (cu->language == language_ada || cu->language == language_m2
10351 || cu->language == language_pascal
10352 || cu->language == language_fortran)
10353 code = TYPE_CODE_CHAR;
10354 break;
10355 case DW_ATE_unsigned_char:
10356 if (cu->language == language_ada || cu->language == language_m2
10357 || cu->language == language_pascal
10358 || cu->language == language_fortran)
10359 code = TYPE_CODE_CHAR;
10360 type_flags |= TYPE_FLAG_UNSIGNED;
10361 break;
10362 case DW_ATE_UTF:
10363 /* We just treat this as an integer and then recognize the
10364 type by name elsewhere. */
10365 break;
10366
10367 default:
10368 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10369 dwarf_type_encoding_name (encoding));
10370 break;
10371 }
10372
10373 type = init_type (code, size, type_flags, NULL, objfile);
10374 TYPE_NAME (type) = name;
10375 TYPE_TARGET_TYPE (type) = target_type;
10376
10377 if (name && strcmp (name, "char") == 0)
10378 TYPE_NOSIGN (type) = 1;
10379
10380 return set_die_type (die, type, cu);
10381 }
10382
10383 /* Read the given DW_AT_subrange DIE. */
10384
10385 static struct type *
10386 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10387 {
10388 struct type *base_type;
10389 struct type *range_type;
10390 struct attribute *attr;
10391 LONGEST low, high;
10392 int low_default_is_valid;
10393 char *name;
10394 LONGEST negative_mask;
10395
10396 base_type = die_type (die, cu);
10397 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10398 check_typedef (base_type);
10399
10400 /* The die_type call above may have already set the type for this DIE. */
10401 range_type = get_die_type (die, cu);
10402 if (range_type)
10403 return range_type;
10404
10405 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10406 omitting DW_AT_lower_bound. */
10407 switch (cu->language)
10408 {
10409 case language_c:
10410 case language_cplus:
10411 low = 0;
10412 low_default_is_valid = 1;
10413 break;
10414 case language_fortran:
10415 low = 1;
10416 low_default_is_valid = 1;
10417 break;
10418 case language_d:
10419 case language_java:
10420 case language_objc:
10421 low = 0;
10422 low_default_is_valid = (cu->header.version >= 4);
10423 break;
10424 case language_ada:
10425 case language_m2:
10426 case language_pascal:
10427 low = 1;
10428 low_default_is_valid = (cu->header.version >= 4);
10429 break;
10430 default:
10431 low = 0;
10432 low_default_is_valid = 0;
10433 break;
10434 }
10435
10436 /* FIXME: For variable sized arrays either of these could be
10437 a variable rather than a constant value. We'll allow it,
10438 but we don't know how to handle it. */
10439 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10440 if (attr)
10441 low = dwarf2_get_attr_constant_value (attr, low);
10442 else if (!low_default_is_valid)
10443 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10444 "- DIE at 0x%x [in module %s]"),
10445 die->offset.sect_off, cu->objfile->name);
10446
10447 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10448 if (attr)
10449 {
10450 if (attr_form_is_block (attr) || is_ref_attr (attr))
10451 {
10452 /* GCC encodes arrays with unspecified or dynamic length
10453 with a DW_FORM_block1 attribute or a reference attribute.
10454 FIXME: GDB does not yet know how to handle dynamic
10455 arrays properly, treat them as arrays with unspecified
10456 length for now.
10457
10458 FIXME: jimb/2003-09-22: GDB does not really know
10459 how to handle arrays of unspecified length
10460 either; we just represent them as zero-length
10461 arrays. Choose an appropriate upper bound given
10462 the lower bound we've computed above. */
10463 high = low - 1;
10464 }
10465 else
10466 high = dwarf2_get_attr_constant_value (attr, 1);
10467 }
10468 else
10469 {
10470 attr = dwarf2_attr (die, DW_AT_count, cu);
10471 if (attr)
10472 {
10473 int count = dwarf2_get_attr_constant_value (attr, 1);
10474 high = low + count - 1;
10475 }
10476 else
10477 {
10478 /* Unspecified array length. */
10479 high = low - 1;
10480 }
10481 }
10482
10483 /* Dwarf-2 specifications explicitly allows to create subrange types
10484 without specifying a base type.
10485 In that case, the base type must be set to the type of
10486 the lower bound, upper bound or count, in that order, if any of these
10487 three attributes references an object that has a type.
10488 If no base type is found, the Dwarf-2 specifications say that
10489 a signed integer type of size equal to the size of an address should
10490 be used.
10491 For the following C code: `extern char gdb_int [];'
10492 GCC produces an empty range DIE.
10493 FIXME: muller/2010-05-28: Possible references to object for low bound,
10494 high bound or count are not yet handled by this code. */
10495 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10496 {
10497 struct objfile *objfile = cu->objfile;
10498 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10499 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10500 struct type *int_type = objfile_type (objfile)->builtin_int;
10501
10502 /* Test "int", "long int", and "long long int" objfile types,
10503 and select the first one having a size above or equal to the
10504 architecture address size. */
10505 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10506 base_type = int_type;
10507 else
10508 {
10509 int_type = objfile_type (objfile)->builtin_long;
10510 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10511 base_type = int_type;
10512 else
10513 {
10514 int_type = objfile_type (objfile)->builtin_long_long;
10515 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10516 base_type = int_type;
10517 }
10518 }
10519 }
10520
10521 negative_mask =
10522 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10523 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10524 low |= negative_mask;
10525 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10526 high |= negative_mask;
10527
10528 range_type = create_range_type (NULL, base_type, low, high);
10529
10530 /* Mark arrays with dynamic length at least as an array of unspecified
10531 length. GDB could check the boundary but before it gets implemented at
10532 least allow accessing the array elements. */
10533 if (attr && attr_form_is_block (attr))
10534 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10535
10536 /* Ada expects an empty array on no boundary attributes. */
10537 if (attr == NULL && cu->language != language_ada)
10538 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10539
10540 name = dwarf2_name (die, cu);
10541 if (name)
10542 TYPE_NAME (range_type) = name;
10543
10544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10545 if (attr)
10546 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10547
10548 set_die_type (die, range_type, cu);
10549
10550 /* set_die_type should be already done. */
10551 set_descriptive_type (range_type, die, cu);
10552
10553 return range_type;
10554 }
10555
10556 static struct type *
10557 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10558 {
10559 struct type *type;
10560
10561 /* For now, we only support the C meaning of an unspecified type: void. */
10562
10563 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10564 TYPE_NAME (type) = dwarf2_name (die, cu);
10565
10566 return set_die_type (die, type, cu);
10567 }
10568
10569 /* Read a single die and all its descendents. Set the die's sibling
10570 field to NULL; set other fields in the die correctly, and set all
10571 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10572 location of the info_ptr after reading all of those dies. PARENT
10573 is the parent of the die in question. */
10574
10575 static struct die_info *
10576 read_die_and_children (const struct die_reader_specs *reader,
10577 gdb_byte *info_ptr,
10578 gdb_byte **new_info_ptr,
10579 struct die_info *parent)
10580 {
10581 struct die_info *die;
10582 gdb_byte *cur_ptr;
10583 int has_children;
10584
10585 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10586 if (die == NULL)
10587 {
10588 *new_info_ptr = cur_ptr;
10589 return NULL;
10590 }
10591 store_in_ref_table (die, reader->cu);
10592
10593 if (has_children)
10594 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10595 else
10596 {
10597 die->child = NULL;
10598 *new_info_ptr = cur_ptr;
10599 }
10600
10601 die->sibling = NULL;
10602 die->parent = parent;
10603 return die;
10604 }
10605
10606 /* Read a die, all of its descendents, and all of its siblings; set
10607 all of the fields of all of the dies correctly. Arguments are as
10608 in read_die_and_children. */
10609
10610 static struct die_info *
10611 read_die_and_siblings (const struct die_reader_specs *reader,
10612 gdb_byte *info_ptr,
10613 gdb_byte **new_info_ptr,
10614 struct die_info *parent)
10615 {
10616 struct die_info *first_die, *last_sibling;
10617 gdb_byte *cur_ptr;
10618
10619 cur_ptr = info_ptr;
10620 first_die = last_sibling = NULL;
10621
10622 while (1)
10623 {
10624 struct die_info *die
10625 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10626
10627 if (die == NULL)
10628 {
10629 *new_info_ptr = cur_ptr;
10630 return first_die;
10631 }
10632
10633 if (!first_die)
10634 first_die = die;
10635 else
10636 last_sibling->sibling = die;
10637
10638 last_sibling = die;
10639 }
10640 }
10641
10642 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10643 attributes.
10644 The caller is responsible for filling in the extra attributes
10645 and updating (*DIEP)->num_attrs.
10646 Set DIEP to point to a newly allocated die with its information,
10647 except for its child, sibling, and parent fields.
10648 Set HAS_CHILDREN to tell whether the die has children or not. */
10649
10650 static gdb_byte *
10651 read_full_die_1 (const struct die_reader_specs *reader,
10652 struct die_info **diep, gdb_byte *info_ptr,
10653 int *has_children, int num_extra_attrs)
10654 {
10655 unsigned int abbrev_number, bytes_read, i;
10656 sect_offset offset;
10657 struct abbrev_info *abbrev;
10658 struct die_info *die;
10659 struct dwarf2_cu *cu = reader->cu;
10660 bfd *abfd = reader->abfd;
10661
10662 offset.sect_off = info_ptr - reader->buffer;
10663 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10664 info_ptr += bytes_read;
10665 if (!abbrev_number)
10666 {
10667 *diep = NULL;
10668 *has_children = 0;
10669 return info_ptr;
10670 }
10671
10672 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10673 if (!abbrev)
10674 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10675 abbrev_number,
10676 bfd_get_filename (abfd));
10677
10678 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10679 die->offset = offset;
10680 die->tag = abbrev->tag;
10681 die->abbrev = abbrev_number;
10682
10683 /* Make the result usable.
10684 The caller needs to update num_attrs after adding the extra
10685 attributes. */
10686 die->num_attrs = abbrev->num_attrs;
10687
10688 for (i = 0; i < abbrev->num_attrs; ++i)
10689 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10690 info_ptr);
10691
10692 *diep = die;
10693 *has_children = abbrev->has_children;
10694 return info_ptr;
10695 }
10696
10697 /* Read a die and all its attributes.
10698 Set DIEP to point to a newly allocated die with its information,
10699 except for its child, sibling, and parent fields.
10700 Set HAS_CHILDREN to tell whether the die has children or not. */
10701
10702 static gdb_byte *
10703 read_full_die (const struct die_reader_specs *reader,
10704 struct die_info **diep, gdb_byte *info_ptr,
10705 int *has_children)
10706 {
10707 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10708 }
10709
10710 /* In DWARF version 2, the description of the debugging information is
10711 stored in a separate .debug_abbrev section. Before we read any
10712 dies from a section we read in all abbreviations and install them
10713 in a hash table. This function also sets flags in CU describing
10714 the data found in the abbrev table. */
10715
10716 static void
10717 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10718 struct dwarf2_section_info *abbrev_section)
10719
10720 {
10721 bfd *abfd = abbrev_section->asection->owner;
10722 struct comp_unit_head *cu_header = &cu->header;
10723 gdb_byte *abbrev_ptr;
10724 struct abbrev_info *cur_abbrev;
10725 unsigned int abbrev_number, bytes_read, abbrev_name;
10726 unsigned int abbrev_form, hash_number;
10727 struct attr_abbrev *cur_attrs;
10728 unsigned int allocated_attrs;
10729
10730 /* Initialize dwarf2 abbrevs. */
10731 obstack_init (&cu->abbrev_obstack);
10732 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10733 (ABBREV_HASH_SIZE
10734 * sizeof (struct abbrev_info *)));
10735 memset (cu->dwarf2_abbrevs, 0,
10736 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10737
10738 dwarf2_read_section (cu->objfile, abbrev_section);
10739 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10740 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10741 abbrev_ptr += bytes_read;
10742
10743 allocated_attrs = ATTR_ALLOC_CHUNK;
10744 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10745
10746 /* Loop until we reach an abbrev number of 0. */
10747 while (abbrev_number)
10748 {
10749 cur_abbrev = dwarf_alloc_abbrev (cu);
10750
10751 /* read in abbrev header */
10752 cur_abbrev->number = abbrev_number;
10753 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10754 abbrev_ptr += bytes_read;
10755 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10756 abbrev_ptr += 1;
10757
10758 /* now read in declarations */
10759 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10760 abbrev_ptr += bytes_read;
10761 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10762 abbrev_ptr += bytes_read;
10763 while (abbrev_name)
10764 {
10765 if (cur_abbrev->num_attrs == allocated_attrs)
10766 {
10767 allocated_attrs += ATTR_ALLOC_CHUNK;
10768 cur_attrs
10769 = xrealloc (cur_attrs, (allocated_attrs
10770 * sizeof (struct attr_abbrev)));
10771 }
10772
10773 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10774 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10775 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10776 abbrev_ptr += bytes_read;
10777 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10778 abbrev_ptr += bytes_read;
10779 }
10780
10781 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10782 (cur_abbrev->num_attrs
10783 * sizeof (struct attr_abbrev)));
10784 memcpy (cur_abbrev->attrs, cur_attrs,
10785 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10786
10787 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10788 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10789 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10790
10791 /* Get next abbreviation.
10792 Under Irix6 the abbreviations for a compilation unit are not
10793 always properly terminated with an abbrev number of 0.
10794 Exit loop if we encounter an abbreviation which we have
10795 already read (which means we are about to read the abbreviations
10796 for the next compile unit) or if the end of the abbreviation
10797 table is reached. */
10798 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10799 >= abbrev_section->size)
10800 break;
10801 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10802 abbrev_ptr += bytes_read;
10803 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10804 break;
10805 }
10806
10807 xfree (cur_attrs);
10808 }
10809
10810 /* Release the memory used by the abbrev table for a compilation unit. */
10811
10812 static void
10813 dwarf2_free_abbrev_table (void *ptr_to_cu)
10814 {
10815 struct dwarf2_cu *cu = ptr_to_cu;
10816
10817 obstack_free (&cu->abbrev_obstack, NULL);
10818 cu->dwarf2_abbrevs = NULL;
10819 }
10820
10821 /* Lookup an abbrev_info structure in the abbrev hash table. */
10822
10823 static struct abbrev_info *
10824 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10825 {
10826 unsigned int hash_number;
10827 struct abbrev_info *abbrev;
10828
10829 hash_number = number % ABBREV_HASH_SIZE;
10830 abbrev = cu->dwarf2_abbrevs[hash_number];
10831
10832 while (abbrev)
10833 {
10834 if (abbrev->number == number)
10835 return abbrev;
10836 else
10837 abbrev = abbrev->next;
10838 }
10839 return NULL;
10840 }
10841
10842 /* Returns nonzero if TAG represents a type that we might generate a partial
10843 symbol for. */
10844
10845 static int
10846 is_type_tag_for_partial (int tag)
10847 {
10848 switch (tag)
10849 {
10850 #if 0
10851 /* Some types that would be reasonable to generate partial symbols for,
10852 that we don't at present. */
10853 case DW_TAG_array_type:
10854 case DW_TAG_file_type:
10855 case DW_TAG_ptr_to_member_type:
10856 case DW_TAG_set_type:
10857 case DW_TAG_string_type:
10858 case DW_TAG_subroutine_type:
10859 #endif
10860 case DW_TAG_base_type:
10861 case DW_TAG_class_type:
10862 case DW_TAG_interface_type:
10863 case DW_TAG_enumeration_type:
10864 case DW_TAG_structure_type:
10865 case DW_TAG_subrange_type:
10866 case DW_TAG_typedef:
10867 case DW_TAG_union_type:
10868 return 1;
10869 default:
10870 return 0;
10871 }
10872 }
10873
10874 /* Load all DIEs that are interesting for partial symbols into memory. */
10875
10876 static struct partial_die_info *
10877 load_partial_dies (const struct die_reader_specs *reader,
10878 gdb_byte *info_ptr, int building_psymtab)
10879 {
10880 struct dwarf2_cu *cu = reader->cu;
10881 struct objfile *objfile = cu->objfile;
10882 struct partial_die_info *part_die;
10883 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10884 struct abbrev_info *abbrev;
10885 unsigned int bytes_read;
10886 unsigned int load_all = 0;
10887 int nesting_level = 1;
10888
10889 parent_die = NULL;
10890 last_die = NULL;
10891
10892 gdb_assert (cu->per_cu != NULL);
10893 if (cu->per_cu->load_all_dies)
10894 load_all = 1;
10895
10896 cu->partial_dies
10897 = htab_create_alloc_ex (cu->header.length / 12,
10898 partial_die_hash,
10899 partial_die_eq,
10900 NULL,
10901 &cu->comp_unit_obstack,
10902 hashtab_obstack_allocate,
10903 dummy_obstack_deallocate);
10904
10905 part_die = obstack_alloc (&cu->comp_unit_obstack,
10906 sizeof (struct partial_die_info));
10907
10908 while (1)
10909 {
10910 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10911
10912 /* A NULL abbrev means the end of a series of children. */
10913 if (abbrev == NULL)
10914 {
10915 if (--nesting_level == 0)
10916 {
10917 /* PART_DIE was probably the last thing allocated on the
10918 comp_unit_obstack, so we could call obstack_free
10919 here. We don't do that because the waste is small,
10920 and will be cleaned up when we're done with this
10921 compilation unit. This way, we're also more robust
10922 against other users of the comp_unit_obstack. */
10923 return first_die;
10924 }
10925 info_ptr += bytes_read;
10926 last_die = parent_die;
10927 parent_die = parent_die->die_parent;
10928 continue;
10929 }
10930
10931 /* Check for template arguments. We never save these; if
10932 they're seen, we just mark the parent, and go on our way. */
10933 if (parent_die != NULL
10934 && cu->language == language_cplus
10935 && (abbrev->tag == DW_TAG_template_type_param
10936 || abbrev->tag == DW_TAG_template_value_param))
10937 {
10938 parent_die->has_template_arguments = 1;
10939
10940 if (!load_all)
10941 {
10942 /* We don't need a partial DIE for the template argument. */
10943 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10944 continue;
10945 }
10946 }
10947
10948 /* We only recurse into c++ subprograms looking for template arguments.
10949 Skip their other children. */
10950 if (!load_all
10951 && cu->language == language_cplus
10952 && parent_die != NULL
10953 && parent_die->tag == DW_TAG_subprogram)
10954 {
10955 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10956 continue;
10957 }
10958
10959 /* Check whether this DIE is interesting enough to save. Normally
10960 we would not be interested in members here, but there may be
10961 later variables referencing them via DW_AT_specification (for
10962 static members). */
10963 if (!load_all
10964 && !is_type_tag_for_partial (abbrev->tag)
10965 && abbrev->tag != DW_TAG_constant
10966 && abbrev->tag != DW_TAG_enumerator
10967 && abbrev->tag != DW_TAG_subprogram
10968 && abbrev->tag != DW_TAG_lexical_block
10969 && abbrev->tag != DW_TAG_variable
10970 && abbrev->tag != DW_TAG_namespace
10971 && abbrev->tag != DW_TAG_module
10972 && abbrev->tag != DW_TAG_member
10973 && abbrev->tag != DW_TAG_imported_unit)
10974 {
10975 /* Otherwise we skip to the next sibling, if any. */
10976 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10977 continue;
10978 }
10979
10980 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10981 info_ptr);
10982
10983 /* This two-pass algorithm for processing partial symbols has a
10984 high cost in cache pressure. Thus, handle some simple cases
10985 here which cover the majority of C partial symbols. DIEs
10986 which neither have specification tags in them, nor could have
10987 specification tags elsewhere pointing at them, can simply be
10988 processed and discarded.
10989
10990 This segment is also optional; scan_partial_symbols and
10991 add_partial_symbol will handle these DIEs if we chain
10992 them in normally. When compilers which do not emit large
10993 quantities of duplicate debug information are more common,
10994 this code can probably be removed. */
10995
10996 /* Any complete simple types at the top level (pretty much all
10997 of them, for a language without namespaces), can be processed
10998 directly. */
10999 if (parent_die == NULL
11000 && part_die->has_specification == 0
11001 && part_die->is_declaration == 0
11002 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11003 || part_die->tag == DW_TAG_base_type
11004 || part_die->tag == DW_TAG_subrange_type))
11005 {
11006 if (building_psymtab && part_die->name != NULL)
11007 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11008 VAR_DOMAIN, LOC_TYPEDEF,
11009 &objfile->static_psymbols,
11010 0, (CORE_ADDR) 0, cu->language, objfile);
11011 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11012 continue;
11013 }
11014
11015 /* The exception for DW_TAG_typedef with has_children above is
11016 a workaround of GCC PR debug/47510. In the case of this complaint
11017 type_name_no_tag_or_error will error on such types later.
11018
11019 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11020 it could not find the child DIEs referenced later, this is checked
11021 above. In correct DWARF DW_TAG_typedef should have no children. */
11022
11023 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11024 complaint (&symfile_complaints,
11025 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11026 "- DIE at 0x%x [in module %s]"),
11027 part_die->offset.sect_off, objfile->name);
11028
11029 /* If we're at the second level, and we're an enumerator, and
11030 our parent has no specification (meaning possibly lives in a
11031 namespace elsewhere), then we can add the partial symbol now
11032 instead of queueing it. */
11033 if (part_die->tag == DW_TAG_enumerator
11034 && parent_die != NULL
11035 && parent_die->die_parent == NULL
11036 && parent_die->tag == DW_TAG_enumeration_type
11037 && parent_die->has_specification == 0)
11038 {
11039 if (part_die->name == NULL)
11040 complaint (&symfile_complaints,
11041 _("malformed enumerator DIE ignored"));
11042 else if (building_psymtab)
11043 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11044 VAR_DOMAIN, LOC_CONST,
11045 (cu->language == language_cplus
11046 || cu->language == language_java)
11047 ? &objfile->global_psymbols
11048 : &objfile->static_psymbols,
11049 0, (CORE_ADDR) 0, cu->language, objfile);
11050
11051 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11052 continue;
11053 }
11054
11055 /* We'll save this DIE so link it in. */
11056 part_die->die_parent = parent_die;
11057 part_die->die_sibling = NULL;
11058 part_die->die_child = NULL;
11059
11060 if (last_die && last_die == parent_die)
11061 last_die->die_child = part_die;
11062 else if (last_die)
11063 last_die->die_sibling = part_die;
11064
11065 last_die = part_die;
11066
11067 if (first_die == NULL)
11068 first_die = part_die;
11069
11070 /* Maybe add the DIE to the hash table. Not all DIEs that we
11071 find interesting need to be in the hash table, because we
11072 also have the parent/sibling/child chains; only those that we
11073 might refer to by offset later during partial symbol reading.
11074
11075 For now this means things that might have be the target of a
11076 DW_AT_specification, DW_AT_abstract_origin, or
11077 DW_AT_extension. DW_AT_extension will refer only to
11078 namespaces; DW_AT_abstract_origin refers to functions (and
11079 many things under the function DIE, but we do not recurse
11080 into function DIEs during partial symbol reading) and
11081 possibly variables as well; DW_AT_specification refers to
11082 declarations. Declarations ought to have the DW_AT_declaration
11083 flag. It happens that GCC forgets to put it in sometimes, but
11084 only for functions, not for types.
11085
11086 Adding more things than necessary to the hash table is harmless
11087 except for the performance cost. Adding too few will result in
11088 wasted time in find_partial_die, when we reread the compilation
11089 unit with load_all_dies set. */
11090
11091 if (load_all
11092 || abbrev->tag == DW_TAG_constant
11093 || abbrev->tag == DW_TAG_subprogram
11094 || abbrev->tag == DW_TAG_variable
11095 || abbrev->tag == DW_TAG_namespace
11096 || part_die->is_declaration)
11097 {
11098 void **slot;
11099
11100 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11101 part_die->offset.sect_off, INSERT);
11102 *slot = part_die;
11103 }
11104
11105 part_die = obstack_alloc (&cu->comp_unit_obstack,
11106 sizeof (struct partial_die_info));
11107
11108 /* For some DIEs we want to follow their children (if any). For C
11109 we have no reason to follow the children of structures; for other
11110 languages we have to, so that we can get at method physnames
11111 to infer fully qualified class names, for DW_AT_specification,
11112 and for C++ template arguments. For C++, we also look one level
11113 inside functions to find template arguments (if the name of the
11114 function does not already contain the template arguments).
11115
11116 For Ada, we need to scan the children of subprograms and lexical
11117 blocks as well because Ada allows the definition of nested
11118 entities that could be interesting for the debugger, such as
11119 nested subprograms for instance. */
11120 if (last_die->has_children
11121 && (load_all
11122 || last_die->tag == DW_TAG_namespace
11123 || last_die->tag == DW_TAG_module
11124 || last_die->tag == DW_TAG_enumeration_type
11125 || (cu->language == language_cplus
11126 && last_die->tag == DW_TAG_subprogram
11127 && (last_die->name == NULL
11128 || strchr (last_die->name, '<') == NULL))
11129 || (cu->language != language_c
11130 && (last_die->tag == DW_TAG_class_type
11131 || last_die->tag == DW_TAG_interface_type
11132 || last_die->tag == DW_TAG_structure_type
11133 || last_die->tag == DW_TAG_union_type))
11134 || (cu->language == language_ada
11135 && (last_die->tag == DW_TAG_subprogram
11136 || last_die->tag == DW_TAG_lexical_block))))
11137 {
11138 nesting_level++;
11139 parent_die = last_die;
11140 continue;
11141 }
11142
11143 /* Otherwise we skip to the next sibling, if any. */
11144 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11145
11146 /* Back to the top, do it again. */
11147 }
11148 }
11149
11150 /* Read a minimal amount of information into the minimal die structure. */
11151
11152 static gdb_byte *
11153 read_partial_die (const struct die_reader_specs *reader,
11154 struct partial_die_info *part_die,
11155 struct abbrev_info *abbrev, unsigned int abbrev_len,
11156 gdb_byte *info_ptr)
11157 {
11158 struct dwarf2_cu *cu = reader->cu;
11159 struct objfile *objfile = cu->objfile;
11160 gdb_byte *buffer = reader->buffer;
11161 unsigned int i;
11162 struct attribute attr;
11163 int has_low_pc_attr = 0;
11164 int has_high_pc_attr = 0;
11165 int high_pc_relative = 0;
11166
11167 memset (part_die, 0, sizeof (struct partial_die_info));
11168
11169 part_die->offset.sect_off = info_ptr - buffer;
11170
11171 info_ptr += abbrev_len;
11172
11173 if (abbrev == NULL)
11174 return info_ptr;
11175
11176 part_die->tag = abbrev->tag;
11177 part_die->has_children = abbrev->has_children;
11178
11179 for (i = 0; i < abbrev->num_attrs; ++i)
11180 {
11181 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11182
11183 /* Store the data if it is of an attribute we want to keep in a
11184 partial symbol table. */
11185 switch (attr.name)
11186 {
11187 case DW_AT_name:
11188 switch (part_die->tag)
11189 {
11190 case DW_TAG_compile_unit:
11191 case DW_TAG_partial_unit:
11192 case DW_TAG_type_unit:
11193 /* Compilation units have a DW_AT_name that is a filename, not
11194 a source language identifier. */
11195 case DW_TAG_enumeration_type:
11196 case DW_TAG_enumerator:
11197 /* These tags always have simple identifiers already; no need
11198 to canonicalize them. */
11199 part_die->name = DW_STRING (&attr);
11200 break;
11201 default:
11202 part_die->name
11203 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11204 &objfile->objfile_obstack);
11205 break;
11206 }
11207 break;
11208 case DW_AT_linkage_name:
11209 case DW_AT_MIPS_linkage_name:
11210 /* Note that both forms of linkage name might appear. We
11211 assume they will be the same, and we only store the last
11212 one we see. */
11213 if (cu->language == language_ada)
11214 part_die->name = DW_STRING (&attr);
11215 part_die->linkage_name = DW_STRING (&attr);
11216 break;
11217 case DW_AT_low_pc:
11218 has_low_pc_attr = 1;
11219 part_die->lowpc = DW_ADDR (&attr);
11220 break;
11221 case DW_AT_high_pc:
11222 has_high_pc_attr = 1;
11223 if (attr.form == DW_FORM_addr
11224 || attr.form == DW_FORM_GNU_addr_index)
11225 part_die->highpc = DW_ADDR (&attr);
11226 else
11227 {
11228 high_pc_relative = 1;
11229 part_die->highpc = DW_UNSND (&attr);
11230 }
11231 break;
11232 case DW_AT_location:
11233 /* Support the .debug_loc offsets. */
11234 if (attr_form_is_block (&attr))
11235 {
11236 part_die->d.locdesc = DW_BLOCK (&attr);
11237 }
11238 else if (attr_form_is_section_offset (&attr))
11239 {
11240 dwarf2_complex_location_expr_complaint ();
11241 }
11242 else
11243 {
11244 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11245 "partial symbol information");
11246 }
11247 break;
11248 case DW_AT_external:
11249 part_die->is_external = DW_UNSND (&attr);
11250 break;
11251 case DW_AT_declaration:
11252 part_die->is_declaration = DW_UNSND (&attr);
11253 break;
11254 case DW_AT_type:
11255 part_die->has_type = 1;
11256 break;
11257 case DW_AT_abstract_origin:
11258 case DW_AT_specification:
11259 case DW_AT_extension:
11260 part_die->has_specification = 1;
11261 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11262 break;
11263 case DW_AT_sibling:
11264 /* Ignore absolute siblings, they might point outside of
11265 the current compile unit. */
11266 if (attr.form == DW_FORM_ref_addr)
11267 complaint (&symfile_complaints,
11268 _("ignoring absolute DW_AT_sibling"));
11269 else
11270 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11271 break;
11272 case DW_AT_byte_size:
11273 part_die->has_byte_size = 1;
11274 break;
11275 case DW_AT_calling_convention:
11276 /* DWARF doesn't provide a way to identify a program's source-level
11277 entry point. DW_AT_calling_convention attributes are only meant
11278 to describe functions' calling conventions.
11279
11280 However, because it's a necessary piece of information in
11281 Fortran, and because DW_CC_program is the only piece of debugging
11282 information whose definition refers to a 'main program' at all,
11283 several compilers have begun marking Fortran main programs with
11284 DW_CC_program --- even when those functions use the standard
11285 calling conventions.
11286
11287 So until DWARF specifies a way to provide this information and
11288 compilers pick up the new representation, we'll support this
11289 practice. */
11290 if (DW_UNSND (&attr) == DW_CC_program
11291 && cu->language == language_fortran)
11292 {
11293 set_main_name (part_die->name);
11294
11295 /* As this DIE has a static linkage the name would be difficult
11296 to look up later. */
11297 language_of_main = language_fortran;
11298 }
11299 break;
11300 case DW_AT_inline:
11301 if (DW_UNSND (&attr) == DW_INL_inlined
11302 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11303 part_die->may_be_inlined = 1;
11304 break;
11305
11306 case DW_AT_import:
11307 if (part_die->tag == DW_TAG_imported_unit)
11308 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11309 break;
11310
11311 default:
11312 break;
11313 }
11314 }
11315
11316 if (high_pc_relative)
11317 part_die->highpc += part_die->lowpc;
11318
11319 if (has_low_pc_attr && has_high_pc_attr)
11320 {
11321 /* When using the GNU linker, .gnu.linkonce. sections are used to
11322 eliminate duplicate copies of functions and vtables and such.
11323 The linker will arbitrarily choose one and discard the others.
11324 The AT_*_pc values for such functions refer to local labels in
11325 these sections. If the section from that file was discarded, the
11326 labels are not in the output, so the relocs get a value of 0.
11327 If this is a discarded function, mark the pc bounds as invalid,
11328 so that GDB will ignore it. */
11329 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11330 {
11331 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11332
11333 complaint (&symfile_complaints,
11334 _("DW_AT_low_pc %s is zero "
11335 "for DIE at 0x%x [in module %s]"),
11336 paddress (gdbarch, part_die->lowpc),
11337 part_die->offset.sect_off, objfile->name);
11338 }
11339 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11340 else if (part_die->lowpc >= part_die->highpc)
11341 {
11342 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11343
11344 complaint (&symfile_complaints,
11345 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11346 "for DIE at 0x%x [in module %s]"),
11347 paddress (gdbarch, part_die->lowpc),
11348 paddress (gdbarch, part_die->highpc),
11349 part_die->offset.sect_off, objfile->name);
11350 }
11351 else
11352 part_die->has_pc_info = 1;
11353 }
11354
11355 return info_ptr;
11356 }
11357
11358 /* Find a cached partial DIE at OFFSET in CU. */
11359
11360 static struct partial_die_info *
11361 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11362 {
11363 struct partial_die_info *lookup_die = NULL;
11364 struct partial_die_info part_die;
11365
11366 part_die.offset = offset;
11367 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11368 offset.sect_off);
11369
11370 return lookup_die;
11371 }
11372
11373 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11374 except in the case of .debug_types DIEs which do not reference
11375 outside their CU (they do however referencing other types via
11376 DW_FORM_ref_sig8). */
11377
11378 static struct partial_die_info *
11379 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11380 {
11381 struct objfile *objfile = cu->objfile;
11382 struct dwarf2_per_cu_data *per_cu = NULL;
11383 struct partial_die_info *pd = NULL;
11384
11385 if (offset_in_cu_p (&cu->header, offset))
11386 {
11387 pd = find_partial_die_in_comp_unit (offset, cu);
11388 if (pd != NULL)
11389 return pd;
11390 /* We missed recording what we needed.
11391 Load all dies and try again. */
11392 per_cu = cu->per_cu;
11393 }
11394 else
11395 {
11396 /* TUs don't reference other CUs/TUs (except via type signatures). */
11397 if (cu->per_cu->is_debug_types)
11398 {
11399 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11400 " external reference to offset 0x%lx [in module %s].\n"),
11401 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11402 bfd_get_filename (objfile->obfd));
11403 }
11404 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11405
11406 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11407 load_partial_comp_unit (per_cu);
11408
11409 per_cu->cu->last_used = 0;
11410 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11411 }
11412
11413 /* If we didn't find it, and not all dies have been loaded,
11414 load them all and try again. */
11415
11416 if (pd == NULL && per_cu->load_all_dies == 0)
11417 {
11418 per_cu->load_all_dies = 1;
11419
11420 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11421 THIS_CU->cu may already be in use. So we can't just free it and
11422 replace its DIEs with the ones we read in. Instead, we leave those
11423 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11424 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11425 set. */
11426 load_partial_comp_unit (per_cu);
11427
11428 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11429 }
11430
11431 if (pd == NULL)
11432 internal_error (__FILE__, __LINE__,
11433 _("could not find partial DIE 0x%x "
11434 "in cache [from module %s]\n"),
11435 offset.sect_off, bfd_get_filename (objfile->obfd));
11436 return pd;
11437 }
11438
11439 /* See if we can figure out if the class lives in a namespace. We do
11440 this by looking for a member function; its demangled name will
11441 contain namespace info, if there is any. */
11442
11443 static void
11444 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11445 struct dwarf2_cu *cu)
11446 {
11447 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11448 what template types look like, because the demangler
11449 frequently doesn't give the same name as the debug info. We
11450 could fix this by only using the demangled name to get the
11451 prefix (but see comment in read_structure_type). */
11452
11453 struct partial_die_info *real_pdi;
11454 struct partial_die_info *child_pdi;
11455
11456 /* If this DIE (this DIE's specification, if any) has a parent, then
11457 we should not do this. We'll prepend the parent's fully qualified
11458 name when we create the partial symbol. */
11459
11460 real_pdi = struct_pdi;
11461 while (real_pdi->has_specification)
11462 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11463
11464 if (real_pdi->die_parent != NULL)
11465 return;
11466
11467 for (child_pdi = struct_pdi->die_child;
11468 child_pdi != NULL;
11469 child_pdi = child_pdi->die_sibling)
11470 {
11471 if (child_pdi->tag == DW_TAG_subprogram
11472 && child_pdi->linkage_name != NULL)
11473 {
11474 char *actual_class_name
11475 = language_class_name_from_physname (cu->language_defn,
11476 child_pdi->linkage_name);
11477 if (actual_class_name != NULL)
11478 {
11479 struct_pdi->name
11480 = obsavestring (actual_class_name,
11481 strlen (actual_class_name),
11482 &cu->objfile->objfile_obstack);
11483 xfree (actual_class_name);
11484 }
11485 break;
11486 }
11487 }
11488 }
11489
11490 /* Adjust PART_DIE before generating a symbol for it. This function
11491 may set the is_external flag or change the DIE's name. */
11492
11493 static void
11494 fixup_partial_die (struct partial_die_info *part_die,
11495 struct dwarf2_cu *cu)
11496 {
11497 /* Once we've fixed up a die, there's no point in doing so again.
11498 This also avoids a memory leak if we were to call
11499 guess_partial_die_structure_name multiple times. */
11500 if (part_die->fixup_called)
11501 return;
11502
11503 /* If we found a reference attribute and the DIE has no name, try
11504 to find a name in the referred to DIE. */
11505
11506 if (part_die->name == NULL && part_die->has_specification)
11507 {
11508 struct partial_die_info *spec_die;
11509
11510 spec_die = find_partial_die (part_die->spec_offset, cu);
11511
11512 fixup_partial_die (spec_die, cu);
11513
11514 if (spec_die->name)
11515 {
11516 part_die->name = spec_die->name;
11517
11518 /* Copy DW_AT_external attribute if it is set. */
11519 if (spec_die->is_external)
11520 part_die->is_external = spec_die->is_external;
11521 }
11522 }
11523
11524 /* Set default names for some unnamed DIEs. */
11525
11526 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11527 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11528
11529 /* If there is no parent die to provide a namespace, and there are
11530 children, see if we can determine the namespace from their linkage
11531 name. */
11532 if (cu->language == language_cplus
11533 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11534 && part_die->die_parent == NULL
11535 && part_die->has_children
11536 && (part_die->tag == DW_TAG_class_type
11537 || part_die->tag == DW_TAG_structure_type
11538 || part_die->tag == DW_TAG_union_type))
11539 guess_partial_die_structure_name (part_die, cu);
11540
11541 /* GCC might emit a nameless struct or union that has a linkage
11542 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11543 if (part_die->name == NULL
11544 && (part_die->tag == DW_TAG_class_type
11545 || part_die->tag == DW_TAG_interface_type
11546 || part_die->tag == DW_TAG_structure_type
11547 || part_die->tag == DW_TAG_union_type)
11548 && part_die->linkage_name != NULL)
11549 {
11550 char *demangled;
11551
11552 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11553 if (demangled)
11554 {
11555 const char *base;
11556
11557 /* Strip any leading namespaces/classes, keep only the base name.
11558 DW_AT_name for named DIEs does not contain the prefixes. */
11559 base = strrchr (demangled, ':');
11560 if (base && base > demangled && base[-1] == ':')
11561 base++;
11562 else
11563 base = demangled;
11564
11565 part_die->name = obsavestring (base, strlen (base),
11566 &cu->objfile->objfile_obstack);
11567 xfree (demangled);
11568 }
11569 }
11570
11571 part_die->fixup_called = 1;
11572 }
11573
11574 /* Read an attribute value described by an attribute form. */
11575
11576 static gdb_byte *
11577 read_attribute_value (const struct die_reader_specs *reader,
11578 struct attribute *attr, unsigned form,
11579 gdb_byte *info_ptr)
11580 {
11581 struct dwarf2_cu *cu = reader->cu;
11582 bfd *abfd = reader->abfd;
11583 struct comp_unit_head *cu_header = &cu->header;
11584 unsigned int bytes_read;
11585 struct dwarf_block *blk;
11586
11587 attr->form = form;
11588 switch (form)
11589 {
11590 case DW_FORM_ref_addr:
11591 if (cu->header.version == 2)
11592 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11593 else
11594 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11595 &cu->header, &bytes_read);
11596 info_ptr += bytes_read;
11597 break;
11598 case DW_FORM_addr:
11599 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11600 info_ptr += bytes_read;
11601 break;
11602 case DW_FORM_block2:
11603 blk = dwarf_alloc_block (cu);
11604 blk->size = read_2_bytes (abfd, info_ptr);
11605 info_ptr += 2;
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_block4:
11611 blk = dwarf_alloc_block (cu);
11612 blk->size = read_4_bytes (abfd, info_ptr);
11613 info_ptr += 4;
11614 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11615 info_ptr += blk->size;
11616 DW_BLOCK (attr) = blk;
11617 break;
11618 case DW_FORM_data2:
11619 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11620 info_ptr += 2;
11621 break;
11622 case DW_FORM_data4:
11623 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11624 info_ptr += 4;
11625 break;
11626 case DW_FORM_data8:
11627 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11628 info_ptr += 8;
11629 break;
11630 case DW_FORM_sec_offset:
11631 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11632 info_ptr += bytes_read;
11633 break;
11634 case DW_FORM_string:
11635 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11636 DW_STRING_IS_CANONICAL (attr) = 0;
11637 info_ptr += bytes_read;
11638 break;
11639 case DW_FORM_strp:
11640 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11641 &bytes_read);
11642 DW_STRING_IS_CANONICAL (attr) = 0;
11643 info_ptr += bytes_read;
11644 break;
11645 case DW_FORM_exprloc:
11646 case DW_FORM_block:
11647 blk = dwarf_alloc_block (cu);
11648 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11649 info_ptr += bytes_read;
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_block1:
11655 blk = dwarf_alloc_block (cu);
11656 blk->size = read_1_byte (abfd, info_ptr);
11657 info_ptr += 1;
11658 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11659 info_ptr += blk->size;
11660 DW_BLOCK (attr) = blk;
11661 break;
11662 case DW_FORM_data1:
11663 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11664 info_ptr += 1;
11665 break;
11666 case DW_FORM_flag:
11667 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11668 info_ptr += 1;
11669 break;
11670 case DW_FORM_flag_present:
11671 DW_UNSND (attr) = 1;
11672 break;
11673 case DW_FORM_sdata:
11674 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11675 info_ptr += bytes_read;
11676 break;
11677 case DW_FORM_udata:
11678 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11679 info_ptr += bytes_read;
11680 break;
11681 case DW_FORM_ref1:
11682 DW_UNSND (attr) = (cu->header.offset.sect_off
11683 + read_1_byte (abfd, info_ptr));
11684 info_ptr += 1;
11685 break;
11686 case DW_FORM_ref2:
11687 DW_UNSND (attr) = (cu->header.offset.sect_off
11688 + read_2_bytes (abfd, info_ptr));
11689 info_ptr += 2;
11690 break;
11691 case DW_FORM_ref4:
11692 DW_UNSND (attr) = (cu->header.offset.sect_off
11693 + read_4_bytes (abfd, info_ptr));
11694 info_ptr += 4;
11695 break;
11696 case DW_FORM_ref8:
11697 DW_UNSND (attr) = (cu->header.offset.sect_off
11698 + read_8_bytes (abfd, info_ptr));
11699 info_ptr += 8;
11700 break;
11701 case DW_FORM_ref_sig8:
11702 /* Convert the signature to something we can record in DW_UNSND
11703 for later lookup.
11704 NOTE: This is NULL if the type wasn't found. */
11705 DW_SIGNATURED_TYPE (attr) =
11706 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11707 info_ptr += 8;
11708 break;
11709 case DW_FORM_ref_udata:
11710 DW_UNSND (attr) = (cu->header.offset.sect_off
11711 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11712 info_ptr += bytes_read;
11713 break;
11714 case DW_FORM_indirect:
11715 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11716 info_ptr += bytes_read;
11717 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11718 break;
11719 case DW_FORM_GNU_addr_index:
11720 if (reader->dwo_file == NULL)
11721 {
11722 /* For now flag a hard error.
11723 Later we can turn this into a complaint. */
11724 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11725 dwarf_form_name (form),
11726 bfd_get_filename (abfd));
11727 }
11728 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11729 info_ptr += bytes_read;
11730 break;
11731 case DW_FORM_GNU_str_index:
11732 if (reader->dwo_file == NULL)
11733 {
11734 /* For now flag a hard error.
11735 Later we can turn this into a complaint if warranted. */
11736 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11737 dwarf_form_name (form),
11738 bfd_get_filename (abfd));
11739 }
11740 {
11741 ULONGEST str_index =
11742 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11743
11744 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11745 DW_STRING_IS_CANONICAL (attr) = 0;
11746 info_ptr += bytes_read;
11747 }
11748 break;
11749 default:
11750 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11751 dwarf_form_name (form),
11752 bfd_get_filename (abfd));
11753 }
11754
11755 /* We have seen instances where the compiler tried to emit a byte
11756 size attribute of -1 which ended up being encoded as an unsigned
11757 0xffffffff. Although 0xffffffff is technically a valid size value,
11758 an object of this size seems pretty unlikely so we can relatively
11759 safely treat these cases as if the size attribute was invalid and
11760 treat them as zero by default. */
11761 if (attr->name == DW_AT_byte_size
11762 && form == DW_FORM_data4
11763 && DW_UNSND (attr) >= 0xffffffff)
11764 {
11765 complaint
11766 (&symfile_complaints,
11767 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11768 hex_string (DW_UNSND (attr)));
11769 DW_UNSND (attr) = 0;
11770 }
11771
11772 return info_ptr;
11773 }
11774
11775 /* Read an attribute described by an abbreviated attribute. */
11776
11777 static gdb_byte *
11778 read_attribute (const struct die_reader_specs *reader,
11779 struct attribute *attr, struct attr_abbrev *abbrev,
11780 gdb_byte *info_ptr)
11781 {
11782 attr->name = abbrev->name;
11783 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11784 }
11785
11786 /* Read dwarf information from a buffer. */
11787
11788 static unsigned int
11789 read_1_byte (bfd *abfd, gdb_byte *buf)
11790 {
11791 return bfd_get_8 (abfd, buf);
11792 }
11793
11794 static int
11795 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11796 {
11797 return bfd_get_signed_8 (abfd, buf);
11798 }
11799
11800 static unsigned int
11801 read_2_bytes (bfd *abfd, gdb_byte *buf)
11802 {
11803 return bfd_get_16 (abfd, buf);
11804 }
11805
11806 static int
11807 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11808 {
11809 return bfd_get_signed_16 (abfd, buf);
11810 }
11811
11812 static unsigned int
11813 read_4_bytes (bfd *abfd, gdb_byte *buf)
11814 {
11815 return bfd_get_32 (abfd, buf);
11816 }
11817
11818 static int
11819 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11820 {
11821 return bfd_get_signed_32 (abfd, buf);
11822 }
11823
11824 static ULONGEST
11825 read_8_bytes (bfd *abfd, gdb_byte *buf)
11826 {
11827 return bfd_get_64 (abfd, buf);
11828 }
11829
11830 static CORE_ADDR
11831 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11832 unsigned int *bytes_read)
11833 {
11834 struct comp_unit_head *cu_header = &cu->header;
11835 CORE_ADDR retval = 0;
11836
11837 if (cu_header->signed_addr_p)
11838 {
11839 switch (cu_header->addr_size)
11840 {
11841 case 2:
11842 retval = bfd_get_signed_16 (abfd, buf);
11843 break;
11844 case 4:
11845 retval = bfd_get_signed_32 (abfd, buf);
11846 break;
11847 case 8:
11848 retval = bfd_get_signed_64 (abfd, buf);
11849 break;
11850 default:
11851 internal_error (__FILE__, __LINE__,
11852 _("read_address: bad switch, signed [in module %s]"),
11853 bfd_get_filename (abfd));
11854 }
11855 }
11856 else
11857 {
11858 switch (cu_header->addr_size)
11859 {
11860 case 2:
11861 retval = bfd_get_16 (abfd, buf);
11862 break;
11863 case 4:
11864 retval = bfd_get_32 (abfd, buf);
11865 break;
11866 case 8:
11867 retval = bfd_get_64 (abfd, buf);
11868 break;
11869 default:
11870 internal_error (__FILE__, __LINE__,
11871 _("read_address: bad switch, "
11872 "unsigned [in module %s]"),
11873 bfd_get_filename (abfd));
11874 }
11875 }
11876
11877 *bytes_read = cu_header->addr_size;
11878 return retval;
11879 }
11880
11881 /* Read the initial length from a section. The (draft) DWARF 3
11882 specification allows the initial length to take up either 4 bytes
11883 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11884 bytes describe the length and all offsets will be 8 bytes in length
11885 instead of 4.
11886
11887 An older, non-standard 64-bit format is also handled by this
11888 function. The older format in question stores the initial length
11889 as an 8-byte quantity without an escape value. Lengths greater
11890 than 2^32 aren't very common which means that the initial 4 bytes
11891 is almost always zero. Since a length value of zero doesn't make
11892 sense for the 32-bit format, this initial zero can be considered to
11893 be an escape value which indicates the presence of the older 64-bit
11894 format. As written, the code can't detect (old format) lengths
11895 greater than 4GB. If it becomes necessary to handle lengths
11896 somewhat larger than 4GB, we could allow other small values (such
11897 as the non-sensical values of 1, 2, and 3) to also be used as
11898 escape values indicating the presence of the old format.
11899
11900 The value returned via bytes_read should be used to increment the
11901 relevant pointer after calling read_initial_length().
11902
11903 [ Note: read_initial_length() and read_offset() are based on the
11904 document entitled "DWARF Debugging Information Format", revision
11905 3, draft 8, dated November 19, 2001. This document was obtained
11906 from:
11907
11908 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11909
11910 This document is only a draft and is subject to change. (So beware.)
11911
11912 Details regarding the older, non-standard 64-bit format were
11913 determined empirically by examining 64-bit ELF files produced by
11914 the SGI toolchain on an IRIX 6.5 machine.
11915
11916 - Kevin, July 16, 2002
11917 ] */
11918
11919 static LONGEST
11920 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11921 {
11922 LONGEST length = bfd_get_32 (abfd, buf);
11923
11924 if (length == 0xffffffff)
11925 {
11926 length = bfd_get_64 (abfd, buf + 4);
11927 *bytes_read = 12;
11928 }
11929 else if (length == 0)
11930 {
11931 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11932 length = bfd_get_64 (abfd, buf);
11933 *bytes_read = 8;
11934 }
11935 else
11936 {
11937 *bytes_read = 4;
11938 }
11939
11940 return length;
11941 }
11942
11943 /* Cover function for read_initial_length.
11944 Returns the length of the object at BUF, and stores the size of the
11945 initial length in *BYTES_READ and stores the size that offsets will be in
11946 *OFFSET_SIZE.
11947 If the initial length size is not equivalent to that specified in
11948 CU_HEADER then issue a complaint.
11949 This is useful when reading non-comp-unit headers. */
11950
11951 static LONGEST
11952 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11953 const struct comp_unit_head *cu_header,
11954 unsigned int *bytes_read,
11955 unsigned int *offset_size)
11956 {
11957 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11958
11959 gdb_assert (cu_header->initial_length_size == 4
11960 || cu_header->initial_length_size == 8
11961 || cu_header->initial_length_size == 12);
11962
11963 if (cu_header->initial_length_size != *bytes_read)
11964 complaint (&symfile_complaints,
11965 _("intermixed 32-bit and 64-bit DWARF sections"));
11966
11967 *offset_size = (*bytes_read == 4) ? 4 : 8;
11968 return length;
11969 }
11970
11971 /* Read an offset from the data stream. The size of the offset is
11972 given by cu_header->offset_size. */
11973
11974 static LONGEST
11975 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11976 unsigned int *bytes_read)
11977 {
11978 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11979
11980 *bytes_read = cu_header->offset_size;
11981 return offset;
11982 }
11983
11984 /* Read an offset from the data stream. */
11985
11986 static LONGEST
11987 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11988 {
11989 LONGEST retval = 0;
11990
11991 switch (offset_size)
11992 {
11993 case 4:
11994 retval = bfd_get_32 (abfd, buf);
11995 break;
11996 case 8:
11997 retval = bfd_get_64 (abfd, buf);
11998 break;
11999 default:
12000 internal_error (__FILE__, __LINE__,
12001 _("read_offset_1: bad switch [in module %s]"),
12002 bfd_get_filename (abfd));
12003 }
12004
12005 return retval;
12006 }
12007
12008 static gdb_byte *
12009 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12010 {
12011 /* If the size of a host char is 8 bits, we can return a pointer
12012 to the buffer, otherwise we have to copy the data to a buffer
12013 allocated on the temporary obstack. */
12014 gdb_assert (HOST_CHAR_BIT == 8);
12015 return buf;
12016 }
12017
12018 static char *
12019 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12020 {
12021 /* If the size of a host char is 8 bits, we can return a pointer
12022 to the string, otherwise we have to copy the string to a buffer
12023 allocated on the temporary obstack. */
12024 gdb_assert (HOST_CHAR_BIT == 8);
12025 if (*buf == '\0')
12026 {
12027 *bytes_read_ptr = 1;
12028 return NULL;
12029 }
12030 *bytes_read_ptr = strlen ((char *) buf) + 1;
12031 return (char *) buf;
12032 }
12033
12034 static char *
12035 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12036 {
12037 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12038 if (dwarf2_per_objfile->str.buffer == NULL)
12039 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12040 bfd_get_filename (abfd));
12041 if (str_offset >= dwarf2_per_objfile->str.size)
12042 error (_("DW_FORM_strp pointing outside of "
12043 ".debug_str section [in module %s]"),
12044 bfd_get_filename (abfd));
12045 gdb_assert (HOST_CHAR_BIT == 8);
12046 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12047 return NULL;
12048 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12049 }
12050
12051 static char *
12052 read_indirect_string (bfd *abfd, gdb_byte *buf,
12053 const struct comp_unit_head *cu_header,
12054 unsigned int *bytes_read_ptr)
12055 {
12056 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12057
12058 return read_indirect_string_at_offset (abfd, str_offset);
12059 }
12060
12061 static ULONGEST
12062 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12063 {
12064 ULONGEST result;
12065 unsigned int num_read;
12066 int i, shift;
12067 unsigned char byte;
12068
12069 result = 0;
12070 shift = 0;
12071 num_read = 0;
12072 i = 0;
12073 while (1)
12074 {
12075 byte = bfd_get_8 (abfd, buf);
12076 buf++;
12077 num_read++;
12078 result |= ((ULONGEST) (byte & 127) << shift);
12079 if ((byte & 128) == 0)
12080 {
12081 break;
12082 }
12083 shift += 7;
12084 }
12085 *bytes_read_ptr = num_read;
12086 return result;
12087 }
12088
12089 static LONGEST
12090 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12091 {
12092 LONGEST result;
12093 int i, shift, num_read;
12094 unsigned char byte;
12095
12096 result = 0;
12097 shift = 0;
12098 num_read = 0;
12099 i = 0;
12100 while (1)
12101 {
12102 byte = bfd_get_8 (abfd, buf);
12103 buf++;
12104 num_read++;
12105 result |= ((LONGEST) (byte & 127) << shift);
12106 shift += 7;
12107 if ((byte & 128) == 0)
12108 {
12109 break;
12110 }
12111 }
12112 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12113 result |= -(((LONGEST) 1) << shift);
12114 *bytes_read_ptr = num_read;
12115 return result;
12116 }
12117
12118 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12119 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12120 ADDR_SIZE is the size of addresses from the CU header. */
12121
12122 static CORE_ADDR
12123 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12124 {
12125 struct objfile *objfile = dwarf2_per_objfile->objfile;
12126 bfd *abfd = objfile->obfd;
12127 const gdb_byte *info_ptr;
12128
12129 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12130 if (dwarf2_per_objfile->addr.buffer == NULL)
12131 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12132 objfile->name);
12133 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12134 error (_("DW_FORM_addr_index pointing outside of "
12135 ".debug_addr section [in module %s]"),
12136 objfile->name);
12137 info_ptr = (dwarf2_per_objfile->addr.buffer
12138 + addr_base + addr_index * addr_size);
12139 if (addr_size == 4)
12140 return bfd_get_32 (abfd, info_ptr);
12141 else
12142 return bfd_get_64 (abfd, info_ptr);
12143 }
12144
12145 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12146
12147 static CORE_ADDR
12148 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12149 {
12150 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12151 }
12152
12153 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12154
12155 static CORE_ADDR
12156 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12157 unsigned int *bytes_read)
12158 {
12159 bfd *abfd = cu->objfile->obfd;
12160 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12161
12162 return read_addr_index (cu, addr_index);
12163 }
12164
12165 /* Data structure to pass results from dwarf2_read_addr_index_reader
12166 back to dwarf2_read_addr_index. */
12167
12168 struct dwarf2_read_addr_index_data
12169 {
12170 ULONGEST addr_base;
12171 int addr_size;
12172 };
12173
12174 /* die_reader_func for dwarf2_read_addr_index. */
12175
12176 static void
12177 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12178 gdb_byte *info_ptr,
12179 struct die_info *comp_unit_die,
12180 int has_children,
12181 void *data)
12182 {
12183 struct dwarf2_cu *cu = reader->cu;
12184 struct dwarf2_read_addr_index_data *aidata =
12185 (struct dwarf2_read_addr_index_data *) data;
12186
12187 aidata->addr_base = cu->addr_base;
12188 aidata->addr_size = cu->header.addr_size;
12189 }
12190
12191 /* Given an index in .debug_addr, fetch the value.
12192 NOTE: This can be called during dwarf expression evaluation,
12193 long after the debug information has been read, and thus per_cu->cu
12194 may no longer exist. */
12195
12196 CORE_ADDR
12197 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12198 unsigned int addr_index)
12199 {
12200 struct objfile *objfile = per_cu->objfile;
12201 struct dwarf2_cu *cu = per_cu->cu;
12202 ULONGEST addr_base;
12203 int addr_size;
12204
12205 /* This is intended to be called from outside this file. */
12206 dw2_setup (objfile);
12207
12208 /* We need addr_base and addr_size.
12209 If we don't have PER_CU->cu, we have to get it.
12210 Nasty, but the alternative is storing the needed info in PER_CU,
12211 which at this point doesn't seem justified: it's not clear how frequently
12212 it would get used and it would increase the size of every PER_CU.
12213 Entry points like dwarf2_per_cu_addr_size do a similar thing
12214 so we're not in uncharted territory here.
12215 Alas we need to be a bit more complicated as addr_base is contained
12216 in the DIE.
12217
12218 We don't need to read the entire CU(/TU).
12219 We just need the header and top level die.
12220 IWBN to use the aging mechanism to let us lazily later discard the CU.
12221 See however init_cutu_and_read_dies_simple. */
12222
12223 if (cu != NULL)
12224 {
12225 addr_base = cu->addr_base;
12226 addr_size = cu->header.addr_size;
12227 }
12228 else
12229 {
12230 struct dwarf2_read_addr_index_data aidata;
12231
12232 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12233 &aidata);
12234 addr_base = aidata.addr_base;
12235 addr_size = aidata.addr_size;
12236 }
12237
12238 return read_addr_index_1 (addr_index, addr_base, addr_size);
12239 }
12240
12241 /* Given a DW_AT_str_index, fetch the string. */
12242
12243 static char *
12244 read_str_index (const struct die_reader_specs *reader,
12245 struct dwarf2_cu *cu, ULONGEST str_index)
12246 {
12247 struct objfile *objfile = dwarf2_per_objfile->objfile;
12248 const char *dwo_name = objfile->name;
12249 bfd *abfd = objfile->obfd;
12250 struct dwo_sections *sections = &reader->dwo_file->sections;
12251 gdb_byte *info_ptr;
12252 ULONGEST str_offset;
12253
12254 dwarf2_read_section (objfile, &sections->str);
12255 dwarf2_read_section (objfile, &sections->str_offsets);
12256 if (sections->str.buffer == NULL)
12257 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12258 " in CU at offset 0x%lx [in module %s]"),
12259 (long) cu->header.offset.sect_off, dwo_name);
12260 if (sections->str_offsets.buffer == NULL)
12261 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12262 " in CU at offset 0x%lx [in module %s]"),
12263 (long) cu->header.offset.sect_off, dwo_name);
12264 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12265 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12266 " section in CU at offset 0x%lx [in module %s]"),
12267 (long) cu->header.offset.sect_off, dwo_name);
12268 info_ptr = (sections->str_offsets.buffer
12269 + str_index * cu->header.offset_size);
12270 if (cu->header.offset_size == 4)
12271 str_offset = bfd_get_32 (abfd, info_ptr);
12272 else
12273 str_offset = bfd_get_64 (abfd, info_ptr);
12274 if (str_offset >= sections->str.size)
12275 error (_("Offset from DW_FORM_str_index pointing outside of"
12276 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12277 (long) cu->header.offset.sect_off, dwo_name);
12278 return (char *) (sections->str.buffer + str_offset);
12279 }
12280
12281 /* Return a pointer to just past the end of an LEB128 number in BUF. */
12282
12283 static gdb_byte *
12284 skip_leb128 (bfd *abfd, gdb_byte *buf)
12285 {
12286 int byte;
12287
12288 while (1)
12289 {
12290 byte = bfd_get_8 (abfd, buf);
12291 buf++;
12292 if ((byte & 128) == 0)
12293 return buf;
12294 }
12295 }
12296
12297 /* Return the length of an LEB128 number in BUF. */
12298
12299 static int
12300 leb128_size (const gdb_byte *buf)
12301 {
12302 const gdb_byte *begin = buf;
12303 gdb_byte byte;
12304
12305 while (1)
12306 {
12307 byte = *buf++;
12308 if ((byte & 128) == 0)
12309 return buf - begin;
12310 }
12311 }
12312
12313 static void
12314 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12315 {
12316 switch (lang)
12317 {
12318 case DW_LANG_C89:
12319 case DW_LANG_C99:
12320 case DW_LANG_C:
12321 cu->language = language_c;
12322 break;
12323 case DW_LANG_C_plus_plus:
12324 cu->language = language_cplus;
12325 break;
12326 case DW_LANG_D:
12327 cu->language = language_d;
12328 break;
12329 case DW_LANG_Fortran77:
12330 case DW_LANG_Fortran90:
12331 case DW_LANG_Fortran95:
12332 cu->language = language_fortran;
12333 break;
12334 case DW_LANG_Go:
12335 cu->language = language_go;
12336 break;
12337 case DW_LANG_Mips_Assembler:
12338 cu->language = language_asm;
12339 break;
12340 case DW_LANG_Java:
12341 cu->language = language_java;
12342 break;
12343 case DW_LANG_Ada83:
12344 case DW_LANG_Ada95:
12345 cu->language = language_ada;
12346 break;
12347 case DW_LANG_Modula2:
12348 cu->language = language_m2;
12349 break;
12350 case DW_LANG_Pascal83:
12351 cu->language = language_pascal;
12352 break;
12353 case DW_LANG_ObjC:
12354 cu->language = language_objc;
12355 break;
12356 case DW_LANG_Cobol74:
12357 case DW_LANG_Cobol85:
12358 default:
12359 cu->language = language_minimal;
12360 break;
12361 }
12362 cu->language_defn = language_def (cu->language);
12363 }
12364
12365 /* Return the named attribute or NULL if not there. */
12366
12367 static struct attribute *
12368 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12369 {
12370 for (;;)
12371 {
12372 unsigned int i;
12373 struct attribute *spec = NULL;
12374
12375 for (i = 0; i < die->num_attrs; ++i)
12376 {
12377 if (die->attrs[i].name == name)
12378 return &die->attrs[i];
12379 if (die->attrs[i].name == DW_AT_specification
12380 || die->attrs[i].name == DW_AT_abstract_origin)
12381 spec = &die->attrs[i];
12382 }
12383
12384 if (!spec)
12385 break;
12386
12387 die = follow_die_ref (die, spec, &cu);
12388 }
12389
12390 return NULL;
12391 }
12392
12393 /* Return the named attribute or NULL if not there,
12394 but do not follow DW_AT_specification, etc.
12395 This is for use in contexts where we're reading .debug_types dies.
12396 Following DW_AT_specification, DW_AT_abstract_origin will take us
12397 back up the chain, and we want to go down. */
12398
12399 static struct attribute *
12400 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12401 struct dwarf2_cu *cu)
12402 {
12403 unsigned int i;
12404
12405 for (i = 0; i < die->num_attrs; ++i)
12406 if (die->attrs[i].name == name)
12407 return &die->attrs[i];
12408
12409 return NULL;
12410 }
12411
12412 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12413 and holds a non-zero value. This function should only be used for
12414 DW_FORM_flag or DW_FORM_flag_present attributes. */
12415
12416 static int
12417 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12418 {
12419 struct attribute *attr = dwarf2_attr (die, name, cu);
12420
12421 return (attr && DW_UNSND (attr));
12422 }
12423
12424 static int
12425 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12426 {
12427 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12428 which value is non-zero. However, we have to be careful with
12429 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12430 (via dwarf2_flag_true_p) follows this attribute. So we may
12431 end up accidently finding a declaration attribute that belongs
12432 to a different DIE referenced by the specification attribute,
12433 even though the given DIE does not have a declaration attribute. */
12434 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12435 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12436 }
12437
12438 /* Return the die giving the specification for DIE, if there is
12439 one. *SPEC_CU is the CU containing DIE on input, and the CU
12440 containing the return value on output. If there is no
12441 specification, but there is an abstract origin, that is
12442 returned. */
12443
12444 static struct die_info *
12445 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12446 {
12447 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12448 *spec_cu);
12449
12450 if (spec_attr == NULL)
12451 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12452
12453 if (spec_attr == NULL)
12454 return NULL;
12455 else
12456 return follow_die_ref (die, spec_attr, spec_cu);
12457 }
12458
12459 /* Free the line_header structure *LH, and any arrays and strings it
12460 refers to.
12461 NOTE: This is also used as a "cleanup" function. */
12462
12463 static void
12464 free_line_header (struct line_header *lh)
12465 {
12466 if (lh->standard_opcode_lengths)
12467 xfree (lh->standard_opcode_lengths);
12468
12469 /* Remember that all the lh->file_names[i].name pointers are
12470 pointers into debug_line_buffer, and don't need to be freed. */
12471 if (lh->file_names)
12472 xfree (lh->file_names);
12473
12474 /* Similarly for the include directory names. */
12475 if (lh->include_dirs)
12476 xfree (lh->include_dirs);
12477
12478 xfree (lh);
12479 }
12480
12481 /* Add an entry to LH's include directory table. */
12482
12483 static void
12484 add_include_dir (struct line_header *lh, char *include_dir)
12485 {
12486 /* Grow the array if necessary. */
12487 if (lh->include_dirs_size == 0)
12488 {
12489 lh->include_dirs_size = 1; /* for testing */
12490 lh->include_dirs = xmalloc (lh->include_dirs_size
12491 * sizeof (*lh->include_dirs));
12492 }
12493 else if (lh->num_include_dirs >= lh->include_dirs_size)
12494 {
12495 lh->include_dirs_size *= 2;
12496 lh->include_dirs = xrealloc (lh->include_dirs,
12497 (lh->include_dirs_size
12498 * sizeof (*lh->include_dirs)));
12499 }
12500
12501 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12502 }
12503
12504 /* Add an entry to LH's file name table. */
12505
12506 static void
12507 add_file_name (struct line_header *lh,
12508 char *name,
12509 unsigned int dir_index,
12510 unsigned int mod_time,
12511 unsigned int length)
12512 {
12513 struct file_entry *fe;
12514
12515 /* Grow the array if necessary. */
12516 if (lh->file_names_size == 0)
12517 {
12518 lh->file_names_size = 1; /* for testing */
12519 lh->file_names = xmalloc (lh->file_names_size
12520 * sizeof (*lh->file_names));
12521 }
12522 else if (lh->num_file_names >= lh->file_names_size)
12523 {
12524 lh->file_names_size *= 2;
12525 lh->file_names = xrealloc (lh->file_names,
12526 (lh->file_names_size
12527 * sizeof (*lh->file_names)));
12528 }
12529
12530 fe = &lh->file_names[lh->num_file_names++];
12531 fe->name = name;
12532 fe->dir_index = dir_index;
12533 fe->mod_time = mod_time;
12534 fe->length = length;
12535 fe->included_p = 0;
12536 fe->symtab = NULL;
12537 }
12538
12539 /* Read the statement program header starting at OFFSET in
12540 .debug_line, or .debug_line.dwo. Return a pointer
12541 to a struct line_header, allocated using xmalloc.
12542
12543 NOTE: the strings in the include directory and file name tables of
12544 the returned object point into the dwarf line section buffer,
12545 and must not be freed. */
12546
12547 static struct line_header *
12548 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12549 {
12550 struct cleanup *back_to;
12551 struct line_header *lh;
12552 gdb_byte *line_ptr;
12553 unsigned int bytes_read, offset_size;
12554 int i;
12555 char *cur_dir, *cur_file;
12556 struct dwarf2_section_info *section;
12557 bfd *abfd;
12558
12559 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12560 DWO file. */
12561 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12562 section = &cu->dwo_unit->dwo_file->sections.line;
12563 else
12564 section = &dwarf2_per_objfile->line;
12565
12566 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12567 if (section->buffer == NULL)
12568 {
12569 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12570 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12571 else
12572 complaint (&symfile_complaints, _("missing .debug_line section"));
12573 return 0;
12574 }
12575
12576 /* We can't do this until we know the section is non-empty.
12577 Only then do we know we have such a section. */
12578 abfd = section->asection->owner;
12579
12580 /* Make sure that at least there's room for the total_length field.
12581 That could be 12 bytes long, but we're just going to fudge that. */
12582 if (offset + 4 >= section->size)
12583 {
12584 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12585 return 0;
12586 }
12587
12588 lh = xmalloc (sizeof (*lh));
12589 memset (lh, 0, sizeof (*lh));
12590 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12591 (void *) lh);
12592
12593 line_ptr = section->buffer + offset;
12594
12595 /* Read in the header. */
12596 lh->total_length =
12597 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12598 &bytes_read, &offset_size);
12599 line_ptr += bytes_read;
12600 if (line_ptr + lh->total_length > (section->buffer + section->size))
12601 {
12602 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12603 return 0;
12604 }
12605 lh->statement_program_end = line_ptr + lh->total_length;
12606 lh->version = read_2_bytes (abfd, line_ptr);
12607 line_ptr += 2;
12608 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12609 line_ptr += offset_size;
12610 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12611 line_ptr += 1;
12612 if (lh->version >= 4)
12613 {
12614 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12615 line_ptr += 1;
12616 }
12617 else
12618 lh->maximum_ops_per_instruction = 1;
12619
12620 if (lh->maximum_ops_per_instruction == 0)
12621 {
12622 lh->maximum_ops_per_instruction = 1;
12623 complaint (&symfile_complaints,
12624 _("invalid maximum_ops_per_instruction "
12625 "in `.debug_line' section"));
12626 }
12627
12628 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12629 line_ptr += 1;
12630 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12631 line_ptr += 1;
12632 lh->line_range = read_1_byte (abfd, line_ptr);
12633 line_ptr += 1;
12634 lh->opcode_base = read_1_byte (abfd, line_ptr);
12635 line_ptr += 1;
12636 lh->standard_opcode_lengths
12637 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12638
12639 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12640 for (i = 1; i < lh->opcode_base; ++i)
12641 {
12642 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12643 line_ptr += 1;
12644 }
12645
12646 /* Read directory table. */
12647 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12648 {
12649 line_ptr += bytes_read;
12650 add_include_dir (lh, cur_dir);
12651 }
12652 line_ptr += bytes_read;
12653
12654 /* Read file name table. */
12655 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12656 {
12657 unsigned int dir_index, mod_time, length;
12658
12659 line_ptr += bytes_read;
12660 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12661 line_ptr += bytes_read;
12662 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12663 line_ptr += bytes_read;
12664 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12665 line_ptr += bytes_read;
12666
12667 add_file_name (lh, cur_file, dir_index, mod_time, length);
12668 }
12669 line_ptr += bytes_read;
12670 lh->statement_program_start = line_ptr;
12671
12672 if (line_ptr > (section->buffer + section->size))
12673 complaint (&symfile_complaints,
12674 _("line number info header doesn't "
12675 "fit in `.debug_line' section"));
12676
12677 discard_cleanups (back_to);
12678 return lh;
12679 }
12680
12681 /* Subroutine of dwarf_decode_lines to simplify it.
12682 Return the file name of the psymtab for included file FILE_INDEX
12683 in line header LH of PST.
12684 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12685 If space for the result is malloc'd, it will be freed by a cleanup.
12686 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12687
12688 static char *
12689 psymtab_include_file_name (const struct line_header *lh, int file_index,
12690 const struct partial_symtab *pst,
12691 const char *comp_dir)
12692 {
12693 const struct file_entry fe = lh->file_names [file_index];
12694 char *include_name = fe.name;
12695 char *include_name_to_compare = include_name;
12696 char *dir_name = NULL;
12697 const char *pst_filename;
12698 char *copied_name = NULL;
12699 int file_is_pst;
12700
12701 if (fe.dir_index)
12702 dir_name = lh->include_dirs[fe.dir_index - 1];
12703
12704 if (!IS_ABSOLUTE_PATH (include_name)
12705 && (dir_name != NULL || comp_dir != NULL))
12706 {
12707 /* Avoid creating a duplicate psymtab for PST.
12708 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12709 Before we do the comparison, however, we need to account
12710 for DIR_NAME and COMP_DIR.
12711 First prepend dir_name (if non-NULL). If we still don't
12712 have an absolute path prepend comp_dir (if non-NULL).
12713 However, the directory we record in the include-file's
12714 psymtab does not contain COMP_DIR (to match the
12715 corresponding symtab(s)).
12716
12717 Example:
12718
12719 bash$ cd /tmp
12720 bash$ gcc -g ./hello.c
12721 include_name = "hello.c"
12722 dir_name = "."
12723 DW_AT_comp_dir = comp_dir = "/tmp"
12724 DW_AT_name = "./hello.c" */
12725
12726 if (dir_name != NULL)
12727 {
12728 include_name = concat (dir_name, SLASH_STRING,
12729 include_name, (char *)NULL);
12730 include_name_to_compare = include_name;
12731 make_cleanup (xfree, include_name);
12732 }
12733 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12734 {
12735 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12736 include_name, (char *)NULL);
12737 }
12738 }
12739
12740 pst_filename = pst->filename;
12741 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12742 {
12743 copied_name = concat (pst->dirname, SLASH_STRING,
12744 pst_filename, (char *)NULL);
12745 pst_filename = copied_name;
12746 }
12747
12748 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12749
12750 if (include_name_to_compare != include_name)
12751 xfree (include_name_to_compare);
12752 if (copied_name != NULL)
12753 xfree (copied_name);
12754
12755 if (file_is_pst)
12756 return NULL;
12757 return include_name;
12758 }
12759
12760 /* Ignore this record_line request. */
12761
12762 static void
12763 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12764 {
12765 return;
12766 }
12767
12768 /* Subroutine of dwarf_decode_lines to simplify it.
12769 Process the line number information in LH. */
12770
12771 static void
12772 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12773 struct dwarf2_cu *cu, struct partial_symtab *pst)
12774 {
12775 gdb_byte *line_ptr, *extended_end;
12776 gdb_byte *line_end;
12777 unsigned int bytes_read, extended_len;
12778 unsigned char op_code, extended_op, adj_opcode;
12779 CORE_ADDR baseaddr;
12780 struct objfile *objfile = cu->objfile;
12781 bfd *abfd = objfile->obfd;
12782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12783 const int decode_for_pst_p = (pst != NULL);
12784 struct subfile *last_subfile = NULL;
12785 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12786 = record_line;
12787
12788 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12789
12790 line_ptr = lh->statement_program_start;
12791 line_end = lh->statement_program_end;
12792
12793 /* Read the statement sequences until there's nothing left. */
12794 while (line_ptr < line_end)
12795 {
12796 /* state machine registers */
12797 CORE_ADDR address = 0;
12798 unsigned int file = 1;
12799 unsigned int line = 1;
12800 unsigned int column = 0;
12801 int is_stmt = lh->default_is_stmt;
12802 int basic_block = 0;
12803 int end_sequence = 0;
12804 CORE_ADDR addr;
12805 unsigned char op_index = 0;
12806
12807 if (!decode_for_pst_p && lh->num_file_names >= file)
12808 {
12809 /* Start a subfile for the current file of the state machine. */
12810 /* lh->include_dirs and lh->file_names are 0-based, but the
12811 directory and file name numbers in the statement program
12812 are 1-based. */
12813 struct file_entry *fe = &lh->file_names[file - 1];
12814 char *dir = NULL;
12815
12816 if (fe->dir_index)
12817 dir = lh->include_dirs[fe->dir_index - 1];
12818
12819 dwarf2_start_subfile (fe->name, dir, comp_dir);
12820 }
12821
12822 /* Decode the table. */
12823 while (!end_sequence)
12824 {
12825 op_code = read_1_byte (abfd, line_ptr);
12826 line_ptr += 1;
12827 if (line_ptr > line_end)
12828 {
12829 dwarf2_debug_line_missing_end_sequence_complaint ();
12830 break;
12831 }
12832
12833 if (op_code >= lh->opcode_base)
12834 {
12835 /* Special operand. */
12836 adj_opcode = op_code - lh->opcode_base;
12837 address += (((op_index + (adj_opcode / lh->line_range))
12838 / lh->maximum_ops_per_instruction)
12839 * lh->minimum_instruction_length);
12840 op_index = ((op_index + (adj_opcode / lh->line_range))
12841 % lh->maximum_ops_per_instruction);
12842 line += lh->line_base + (adj_opcode % lh->line_range);
12843 if (lh->num_file_names < file || file == 0)
12844 dwarf2_debug_line_missing_file_complaint ();
12845 /* For now we ignore lines not starting on an
12846 instruction boundary. */
12847 else if (op_index == 0)
12848 {
12849 lh->file_names[file - 1].included_p = 1;
12850 if (!decode_for_pst_p && is_stmt)
12851 {
12852 if (last_subfile != current_subfile)
12853 {
12854 addr = gdbarch_addr_bits_remove (gdbarch, address);
12855 if (last_subfile)
12856 (*p_record_line) (last_subfile, 0, addr);
12857 last_subfile = current_subfile;
12858 }
12859 /* Append row to matrix using current values. */
12860 addr = gdbarch_addr_bits_remove (gdbarch, address);
12861 (*p_record_line) (current_subfile, line, addr);
12862 }
12863 }
12864 basic_block = 0;
12865 }
12866 else switch (op_code)
12867 {
12868 case DW_LNS_extended_op:
12869 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12870 &bytes_read);
12871 line_ptr += bytes_read;
12872 extended_end = line_ptr + extended_len;
12873 extended_op = read_1_byte (abfd, line_ptr);
12874 line_ptr += 1;
12875 switch (extended_op)
12876 {
12877 case DW_LNE_end_sequence:
12878 p_record_line = record_line;
12879 end_sequence = 1;
12880 break;
12881 case DW_LNE_set_address:
12882 address = read_address (abfd, line_ptr, cu, &bytes_read);
12883
12884 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12885 {
12886 /* This line table is for a function which has been
12887 GCd by the linker. Ignore it. PR gdb/12528 */
12888
12889 long line_offset
12890 = line_ptr - dwarf2_per_objfile->line.buffer;
12891
12892 complaint (&symfile_complaints,
12893 _(".debug_line address at offset 0x%lx is 0 "
12894 "[in module %s]"),
12895 line_offset, objfile->name);
12896 p_record_line = noop_record_line;
12897 }
12898
12899 op_index = 0;
12900 line_ptr += bytes_read;
12901 address += baseaddr;
12902 break;
12903 case DW_LNE_define_file:
12904 {
12905 char *cur_file;
12906 unsigned int dir_index, mod_time, length;
12907
12908 cur_file = read_direct_string (abfd, line_ptr,
12909 &bytes_read);
12910 line_ptr += bytes_read;
12911 dir_index =
12912 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12913 line_ptr += bytes_read;
12914 mod_time =
12915 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12916 line_ptr += bytes_read;
12917 length =
12918 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12919 line_ptr += bytes_read;
12920 add_file_name (lh, cur_file, dir_index, mod_time, length);
12921 }
12922 break;
12923 case DW_LNE_set_discriminator:
12924 /* The discriminator is not interesting to the debugger;
12925 just ignore it. */
12926 line_ptr = extended_end;
12927 break;
12928 default:
12929 complaint (&symfile_complaints,
12930 _("mangled .debug_line section"));
12931 return;
12932 }
12933 /* Make sure that we parsed the extended op correctly. If e.g.
12934 we expected a different address size than the producer used,
12935 we may have read the wrong number of bytes. */
12936 if (line_ptr != extended_end)
12937 {
12938 complaint (&symfile_complaints,
12939 _("mangled .debug_line section"));
12940 return;
12941 }
12942 break;
12943 case DW_LNS_copy:
12944 if (lh->num_file_names < file || file == 0)
12945 dwarf2_debug_line_missing_file_complaint ();
12946 else
12947 {
12948 lh->file_names[file - 1].included_p = 1;
12949 if (!decode_for_pst_p && is_stmt)
12950 {
12951 if (last_subfile != current_subfile)
12952 {
12953 addr = gdbarch_addr_bits_remove (gdbarch, address);
12954 if (last_subfile)
12955 (*p_record_line) (last_subfile, 0, addr);
12956 last_subfile = current_subfile;
12957 }
12958 addr = gdbarch_addr_bits_remove (gdbarch, address);
12959 (*p_record_line) (current_subfile, line, addr);
12960 }
12961 }
12962 basic_block = 0;
12963 break;
12964 case DW_LNS_advance_pc:
12965 {
12966 CORE_ADDR adjust
12967 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12968
12969 address += (((op_index + adjust)
12970 / lh->maximum_ops_per_instruction)
12971 * lh->minimum_instruction_length);
12972 op_index = ((op_index + adjust)
12973 % lh->maximum_ops_per_instruction);
12974 line_ptr += bytes_read;
12975 }
12976 break;
12977 case DW_LNS_advance_line:
12978 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12979 line_ptr += bytes_read;
12980 break;
12981 case DW_LNS_set_file:
12982 {
12983 /* The arrays lh->include_dirs and lh->file_names are
12984 0-based, but the directory and file name numbers in
12985 the statement program are 1-based. */
12986 struct file_entry *fe;
12987 char *dir = NULL;
12988
12989 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12990 line_ptr += bytes_read;
12991 if (lh->num_file_names < file || file == 0)
12992 dwarf2_debug_line_missing_file_complaint ();
12993 else
12994 {
12995 fe = &lh->file_names[file - 1];
12996 if (fe->dir_index)
12997 dir = lh->include_dirs[fe->dir_index - 1];
12998 if (!decode_for_pst_p)
12999 {
13000 last_subfile = current_subfile;
13001 dwarf2_start_subfile (fe->name, dir, comp_dir);
13002 }
13003 }
13004 }
13005 break;
13006 case DW_LNS_set_column:
13007 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13008 line_ptr += bytes_read;
13009 break;
13010 case DW_LNS_negate_stmt:
13011 is_stmt = (!is_stmt);
13012 break;
13013 case DW_LNS_set_basic_block:
13014 basic_block = 1;
13015 break;
13016 /* Add to the address register of the state machine the
13017 address increment value corresponding to special opcode
13018 255. I.e., this value is scaled by the minimum
13019 instruction length since special opcode 255 would have
13020 scaled the increment. */
13021 case DW_LNS_const_add_pc:
13022 {
13023 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13024
13025 address += (((op_index + adjust)
13026 / lh->maximum_ops_per_instruction)
13027 * lh->minimum_instruction_length);
13028 op_index = ((op_index + adjust)
13029 % lh->maximum_ops_per_instruction);
13030 }
13031 break;
13032 case DW_LNS_fixed_advance_pc:
13033 address += read_2_bytes (abfd, line_ptr);
13034 op_index = 0;
13035 line_ptr += 2;
13036 break;
13037 default:
13038 {
13039 /* Unknown standard opcode, ignore it. */
13040 int i;
13041
13042 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13043 {
13044 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13045 line_ptr += bytes_read;
13046 }
13047 }
13048 }
13049 }
13050 if (lh->num_file_names < file || file == 0)
13051 dwarf2_debug_line_missing_file_complaint ();
13052 else
13053 {
13054 lh->file_names[file - 1].included_p = 1;
13055 if (!decode_for_pst_p)
13056 {
13057 addr = gdbarch_addr_bits_remove (gdbarch, address);
13058 (*p_record_line) (current_subfile, 0, addr);
13059 }
13060 }
13061 }
13062 }
13063
13064 /* Decode the Line Number Program (LNP) for the given line_header
13065 structure and CU. The actual information extracted and the type
13066 of structures created from the LNP depends on the value of PST.
13067
13068 1. If PST is NULL, then this procedure uses the data from the program
13069 to create all necessary symbol tables, and their linetables.
13070
13071 2. If PST is not NULL, this procedure reads the program to determine
13072 the list of files included by the unit represented by PST, and
13073 builds all the associated partial symbol tables.
13074
13075 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13076 It is used for relative paths in the line table.
13077 NOTE: When processing partial symtabs (pst != NULL),
13078 comp_dir == pst->dirname.
13079
13080 NOTE: It is important that psymtabs have the same file name (via strcmp)
13081 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13082 symtab we don't use it in the name of the psymtabs we create.
13083 E.g. expand_line_sal requires this when finding psymtabs to expand.
13084 A good testcase for this is mb-inline.exp. */
13085
13086 static void
13087 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13088 struct dwarf2_cu *cu, struct partial_symtab *pst,
13089 int want_line_info)
13090 {
13091 struct objfile *objfile = cu->objfile;
13092 const int decode_for_pst_p = (pst != NULL);
13093 struct subfile *first_subfile = current_subfile;
13094
13095 if (want_line_info)
13096 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13097
13098 if (decode_for_pst_p)
13099 {
13100 int file_index;
13101
13102 /* Now that we're done scanning the Line Header Program, we can
13103 create the psymtab of each included file. */
13104 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13105 if (lh->file_names[file_index].included_p == 1)
13106 {
13107 char *include_name =
13108 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13109 if (include_name != NULL)
13110 dwarf2_create_include_psymtab (include_name, pst, objfile);
13111 }
13112 }
13113 else
13114 {
13115 /* Make sure a symtab is created for every file, even files
13116 which contain only variables (i.e. no code with associated
13117 line numbers). */
13118 int i;
13119
13120 for (i = 0; i < lh->num_file_names; i++)
13121 {
13122 char *dir = NULL;
13123 struct file_entry *fe;
13124
13125 fe = &lh->file_names[i];
13126 if (fe->dir_index)
13127 dir = lh->include_dirs[fe->dir_index - 1];
13128 dwarf2_start_subfile (fe->name, dir, comp_dir);
13129
13130 /* Skip the main file; we don't need it, and it must be
13131 allocated last, so that it will show up before the
13132 non-primary symtabs in the objfile's symtab list. */
13133 if (current_subfile == first_subfile)
13134 continue;
13135
13136 if (current_subfile->symtab == NULL)
13137 current_subfile->symtab = allocate_symtab (current_subfile->name,
13138 objfile);
13139 fe->symtab = current_subfile->symtab;
13140 }
13141 }
13142 }
13143
13144 /* Start a subfile for DWARF. FILENAME is the name of the file and
13145 DIRNAME the name of the source directory which contains FILENAME
13146 or NULL if not known. COMP_DIR is the compilation directory for the
13147 linetable's compilation unit or NULL if not known.
13148 This routine tries to keep line numbers from identical absolute and
13149 relative file names in a common subfile.
13150
13151 Using the `list' example from the GDB testsuite, which resides in
13152 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13153 of /srcdir/list0.c yields the following debugging information for list0.c:
13154
13155 DW_AT_name: /srcdir/list0.c
13156 DW_AT_comp_dir: /compdir
13157 files.files[0].name: list0.h
13158 files.files[0].dir: /srcdir
13159 files.files[1].name: list0.c
13160 files.files[1].dir: /srcdir
13161
13162 The line number information for list0.c has to end up in a single
13163 subfile, so that `break /srcdir/list0.c:1' works as expected.
13164 start_subfile will ensure that this happens provided that we pass the
13165 concatenation of files.files[1].dir and files.files[1].name as the
13166 subfile's name. */
13167
13168 static void
13169 dwarf2_start_subfile (char *filename, const char *dirname,
13170 const char *comp_dir)
13171 {
13172 char *fullname;
13173
13174 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13175 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13176 second argument to start_subfile. To be consistent, we do the
13177 same here. In order not to lose the line information directory,
13178 we concatenate it to the filename when it makes sense.
13179 Note that the Dwarf3 standard says (speaking of filenames in line
13180 information): ``The directory index is ignored for file names
13181 that represent full path names''. Thus ignoring dirname in the
13182 `else' branch below isn't an issue. */
13183
13184 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13185 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13186 else
13187 fullname = filename;
13188
13189 start_subfile (fullname, comp_dir);
13190
13191 if (fullname != filename)
13192 xfree (fullname);
13193 }
13194
13195 static void
13196 var_decode_location (struct attribute *attr, struct symbol *sym,
13197 struct dwarf2_cu *cu)
13198 {
13199 struct objfile *objfile = cu->objfile;
13200 struct comp_unit_head *cu_header = &cu->header;
13201
13202 /* NOTE drow/2003-01-30: There used to be a comment and some special
13203 code here to turn a symbol with DW_AT_external and a
13204 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13205 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13206 with some versions of binutils) where shared libraries could have
13207 relocations against symbols in their debug information - the
13208 minimal symbol would have the right address, but the debug info
13209 would not. It's no longer necessary, because we will explicitly
13210 apply relocations when we read in the debug information now. */
13211
13212 /* A DW_AT_location attribute with no contents indicates that a
13213 variable has been optimized away. */
13214 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13215 {
13216 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13217 return;
13218 }
13219
13220 /* Handle one degenerate form of location expression specially, to
13221 preserve GDB's previous behavior when section offsets are
13222 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13223 then mark this symbol as LOC_STATIC. */
13224
13225 if (attr_form_is_block (attr)
13226 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13227 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13228 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13229 && (DW_BLOCK (attr)->size
13230 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13231 {
13232 unsigned int dummy;
13233
13234 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13235 SYMBOL_VALUE_ADDRESS (sym) =
13236 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13237 else
13238 SYMBOL_VALUE_ADDRESS (sym) =
13239 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13240 SYMBOL_CLASS (sym) = LOC_STATIC;
13241 fixup_symbol_section (sym, objfile);
13242 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13243 SYMBOL_SECTION (sym));
13244 return;
13245 }
13246
13247 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13248 expression evaluator, and use LOC_COMPUTED only when necessary
13249 (i.e. when the value of a register or memory location is
13250 referenced, or a thread-local block, etc.). Then again, it might
13251 not be worthwhile. I'm assuming that it isn't unless performance
13252 or memory numbers show me otherwise. */
13253
13254 dwarf2_symbol_mark_computed (attr, sym, cu);
13255 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13256
13257 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13258 cu->has_loclist = 1;
13259 }
13260
13261 /* Given a pointer to a DWARF information entry, figure out if we need
13262 to make a symbol table entry for it, and if so, create a new entry
13263 and return a pointer to it.
13264 If TYPE is NULL, determine symbol type from the die, otherwise
13265 used the passed type.
13266 If SPACE is not NULL, use it to hold the new symbol. If it is
13267 NULL, allocate a new symbol on the objfile's obstack. */
13268
13269 static struct symbol *
13270 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13271 struct symbol *space)
13272 {
13273 struct objfile *objfile = cu->objfile;
13274 struct symbol *sym = NULL;
13275 char *name;
13276 struct attribute *attr = NULL;
13277 struct attribute *attr2 = NULL;
13278 CORE_ADDR baseaddr;
13279 struct pending **list_to_add = NULL;
13280
13281 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13282
13283 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13284
13285 name = dwarf2_name (die, cu);
13286 if (name)
13287 {
13288 const char *linkagename;
13289 int suppress_add = 0;
13290
13291 if (space)
13292 sym = space;
13293 else
13294 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13295 OBJSTAT (objfile, n_syms++);
13296
13297 /* Cache this symbol's name and the name's demangled form (if any). */
13298 SYMBOL_SET_LANGUAGE (sym, cu->language);
13299 linkagename = dwarf2_physname (name, die, cu);
13300 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13301
13302 /* Fortran does not have mangling standard and the mangling does differ
13303 between gfortran, iFort etc. */
13304 if (cu->language == language_fortran
13305 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13306 symbol_set_demangled_name (&(sym->ginfo),
13307 (char *) dwarf2_full_name (name, die, cu),
13308 NULL);
13309
13310 /* Default assumptions.
13311 Use the passed type or decode it from the die. */
13312 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13313 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13314 if (type != NULL)
13315 SYMBOL_TYPE (sym) = type;
13316 else
13317 SYMBOL_TYPE (sym) = die_type (die, cu);
13318 attr = dwarf2_attr (die,
13319 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13320 cu);
13321 if (attr)
13322 {
13323 SYMBOL_LINE (sym) = DW_UNSND (attr);
13324 }
13325
13326 attr = dwarf2_attr (die,
13327 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13328 cu);
13329 if (attr)
13330 {
13331 int file_index = DW_UNSND (attr);
13332
13333 if (cu->line_header == NULL
13334 || file_index > cu->line_header->num_file_names)
13335 complaint (&symfile_complaints,
13336 _("file index out of range"));
13337 else if (file_index > 0)
13338 {
13339 struct file_entry *fe;
13340
13341 fe = &cu->line_header->file_names[file_index - 1];
13342 SYMBOL_SYMTAB (sym) = fe->symtab;
13343 }
13344 }
13345
13346 switch (die->tag)
13347 {
13348 case DW_TAG_label:
13349 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13350 if (attr)
13351 {
13352 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13353 }
13354 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13355 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13356 SYMBOL_CLASS (sym) = LOC_LABEL;
13357 add_symbol_to_list (sym, cu->list_in_scope);
13358 break;
13359 case DW_TAG_subprogram:
13360 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13361 finish_block. */
13362 SYMBOL_CLASS (sym) = LOC_BLOCK;
13363 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13364 if ((attr2 && (DW_UNSND (attr2) != 0))
13365 || cu->language == language_ada)
13366 {
13367 /* Subprograms marked external are stored as a global symbol.
13368 Ada subprograms, whether marked external or not, are always
13369 stored as a global symbol, because we want to be able to
13370 access them globally. For instance, we want to be able
13371 to break on a nested subprogram without having to
13372 specify the context. */
13373 list_to_add = &global_symbols;
13374 }
13375 else
13376 {
13377 list_to_add = cu->list_in_scope;
13378 }
13379 break;
13380 case DW_TAG_inlined_subroutine:
13381 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13382 finish_block. */
13383 SYMBOL_CLASS (sym) = LOC_BLOCK;
13384 SYMBOL_INLINED (sym) = 1;
13385 list_to_add = cu->list_in_scope;
13386 break;
13387 case DW_TAG_template_value_param:
13388 suppress_add = 1;
13389 /* Fall through. */
13390 case DW_TAG_constant:
13391 case DW_TAG_variable:
13392 case DW_TAG_member:
13393 /* Compilation with minimal debug info may result in
13394 variables with missing type entries. Change the
13395 misleading `void' type to something sensible. */
13396 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13397 SYMBOL_TYPE (sym)
13398 = objfile_type (objfile)->nodebug_data_symbol;
13399
13400 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13401 /* In the case of DW_TAG_member, we should only be called for
13402 static const members. */
13403 if (die->tag == DW_TAG_member)
13404 {
13405 /* dwarf2_add_field uses die_is_declaration,
13406 so we do the same. */
13407 gdb_assert (die_is_declaration (die, cu));
13408 gdb_assert (attr);
13409 }
13410 if (attr)
13411 {
13412 dwarf2_const_value (attr, sym, cu);
13413 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13414 if (!suppress_add)
13415 {
13416 if (attr2 && (DW_UNSND (attr2) != 0))
13417 list_to_add = &global_symbols;
13418 else
13419 list_to_add = cu->list_in_scope;
13420 }
13421 break;
13422 }
13423 attr = dwarf2_attr (die, DW_AT_location, cu);
13424 if (attr)
13425 {
13426 var_decode_location (attr, sym, cu);
13427 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13428 if (SYMBOL_CLASS (sym) == LOC_STATIC
13429 && SYMBOL_VALUE_ADDRESS (sym) == 0
13430 && !dwarf2_per_objfile->has_section_at_zero)
13431 {
13432 /* When a static variable is eliminated by the linker,
13433 the corresponding debug information is not stripped
13434 out, but the variable address is set to null;
13435 do not add such variables into symbol table. */
13436 }
13437 else if (attr2 && (DW_UNSND (attr2) != 0))
13438 {
13439 /* Workaround gfortran PR debug/40040 - it uses
13440 DW_AT_location for variables in -fPIC libraries which may
13441 get overriden by other libraries/executable and get
13442 a different address. Resolve it by the minimal symbol
13443 which may come from inferior's executable using copy
13444 relocation. Make this workaround only for gfortran as for
13445 other compilers GDB cannot guess the minimal symbol
13446 Fortran mangling kind. */
13447 if (cu->language == language_fortran && die->parent
13448 && die->parent->tag == DW_TAG_module
13449 && cu->producer
13450 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13451 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13452
13453 /* A variable with DW_AT_external is never static,
13454 but it may be block-scoped. */
13455 list_to_add = (cu->list_in_scope == &file_symbols
13456 ? &global_symbols : cu->list_in_scope);
13457 }
13458 else
13459 list_to_add = cu->list_in_scope;
13460 }
13461 else
13462 {
13463 /* We do not know the address of this symbol.
13464 If it is an external symbol and we have type information
13465 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13466 The address of the variable will then be determined from
13467 the minimal symbol table whenever the variable is
13468 referenced. */
13469 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13470 if (attr2 && (DW_UNSND (attr2) != 0)
13471 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13472 {
13473 /* A variable with DW_AT_external is never static, but it
13474 may be block-scoped. */
13475 list_to_add = (cu->list_in_scope == &file_symbols
13476 ? &global_symbols : cu->list_in_scope);
13477
13478 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13479 }
13480 else if (!die_is_declaration (die, cu))
13481 {
13482 /* Use the default LOC_OPTIMIZED_OUT class. */
13483 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13484 if (!suppress_add)
13485 list_to_add = cu->list_in_scope;
13486 }
13487 }
13488 break;
13489 case DW_TAG_formal_parameter:
13490 /* If we are inside a function, mark this as an argument. If
13491 not, we might be looking at an argument to an inlined function
13492 when we do not have enough information to show inlined frames;
13493 pretend it's a local variable in that case so that the user can
13494 still see it. */
13495 if (context_stack_depth > 0
13496 && context_stack[context_stack_depth - 1].name != NULL)
13497 SYMBOL_IS_ARGUMENT (sym) = 1;
13498 attr = dwarf2_attr (die, DW_AT_location, cu);
13499 if (attr)
13500 {
13501 var_decode_location (attr, sym, cu);
13502 }
13503 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13504 if (attr)
13505 {
13506 dwarf2_const_value (attr, sym, cu);
13507 }
13508
13509 list_to_add = cu->list_in_scope;
13510 break;
13511 case DW_TAG_unspecified_parameters:
13512 /* From varargs functions; gdb doesn't seem to have any
13513 interest in this information, so just ignore it for now.
13514 (FIXME?) */
13515 break;
13516 case DW_TAG_template_type_param:
13517 suppress_add = 1;
13518 /* Fall through. */
13519 case DW_TAG_class_type:
13520 case DW_TAG_interface_type:
13521 case DW_TAG_structure_type:
13522 case DW_TAG_union_type:
13523 case DW_TAG_set_type:
13524 case DW_TAG_enumeration_type:
13525 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13526 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13527
13528 {
13529 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13530 really ever be static objects: otherwise, if you try
13531 to, say, break of a class's method and you're in a file
13532 which doesn't mention that class, it won't work unless
13533 the check for all static symbols in lookup_symbol_aux
13534 saves you. See the OtherFileClass tests in
13535 gdb.c++/namespace.exp. */
13536
13537 if (!suppress_add)
13538 {
13539 list_to_add = (cu->list_in_scope == &file_symbols
13540 && (cu->language == language_cplus
13541 || cu->language == language_java)
13542 ? &global_symbols : cu->list_in_scope);
13543
13544 /* The semantics of C++ state that "struct foo {
13545 ... }" also defines a typedef for "foo". A Java
13546 class declaration also defines a typedef for the
13547 class. */
13548 if (cu->language == language_cplus
13549 || cu->language == language_java
13550 || cu->language == language_ada)
13551 {
13552 /* The symbol's name is already allocated along
13553 with this objfile, so we don't need to
13554 duplicate it for the type. */
13555 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13556 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13557 }
13558 }
13559 }
13560 break;
13561 case DW_TAG_typedef:
13562 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13563 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13564 list_to_add = cu->list_in_scope;
13565 break;
13566 case DW_TAG_base_type:
13567 case DW_TAG_subrange_type:
13568 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13569 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13570 list_to_add = cu->list_in_scope;
13571 break;
13572 case DW_TAG_enumerator:
13573 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13574 if (attr)
13575 {
13576 dwarf2_const_value (attr, sym, cu);
13577 }
13578 {
13579 /* NOTE: carlton/2003-11-10: See comment above in the
13580 DW_TAG_class_type, etc. block. */
13581
13582 list_to_add = (cu->list_in_scope == &file_symbols
13583 && (cu->language == language_cplus
13584 || cu->language == language_java)
13585 ? &global_symbols : cu->list_in_scope);
13586 }
13587 break;
13588 case DW_TAG_namespace:
13589 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13590 list_to_add = &global_symbols;
13591 break;
13592 default:
13593 /* Not a tag we recognize. Hopefully we aren't processing
13594 trash data, but since we must specifically ignore things
13595 we don't recognize, there is nothing else we should do at
13596 this point. */
13597 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13598 dwarf_tag_name (die->tag));
13599 break;
13600 }
13601
13602 if (suppress_add)
13603 {
13604 sym->hash_next = objfile->template_symbols;
13605 objfile->template_symbols = sym;
13606 list_to_add = NULL;
13607 }
13608
13609 if (list_to_add != NULL)
13610 add_symbol_to_list (sym, list_to_add);
13611
13612 /* For the benefit of old versions of GCC, check for anonymous
13613 namespaces based on the demangled name. */
13614 if (!processing_has_namespace_info
13615 && cu->language == language_cplus)
13616 cp_scan_for_anonymous_namespaces (sym, objfile);
13617 }
13618 return (sym);
13619 }
13620
13621 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13622
13623 static struct symbol *
13624 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13625 {
13626 return new_symbol_full (die, type, cu, NULL);
13627 }
13628
13629 /* Given an attr with a DW_FORM_dataN value in host byte order,
13630 zero-extend it as appropriate for the symbol's type. The DWARF
13631 standard (v4) is not entirely clear about the meaning of using
13632 DW_FORM_dataN for a constant with a signed type, where the type is
13633 wider than the data. The conclusion of a discussion on the DWARF
13634 list was that this is unspecified. We choose to always zero-extend
13635 because that is the interpretation long in use by GCC. */
13636
13637 static gdb_byte *
13638 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13639 const char *name, struct obstack *obstack,
13640 struct dwarf2_cu *cu, LONGEST *value, int bits)
13641 {
13642 struct objfile *objfile = cu->objfile;
13643 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13644 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13645 LONGEST l = DW_UNSND (attr);
13646
13647 if (bits < sizeof (*value) * 8)
13648 {
13649 l &= ((LONGEST) 1 << bits) - 1;
13650 *value = l;
13651 }
13652 else if (bits == sizeof (*value) * 8)
13653 *value = l;
13654 else
13655 {
13656 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13657 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13658 return bytes;
13659 }
13660
13661 return NULL;
13662 }
13663
13664 /* Read a constant value from an attribute. Either set *VALUE, or if
13665 the value does not fit in *VALUE, set *BYTES - either already
13666 allocated on the objfile obstack, or newly allocated on OBSTACK,
13667 or, set *BATON, if we translated the constant to a location
13668 expression. */
13669
13670 static void
13671 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13672 const char *name, struct obstack *obstack,
13673 struct dwarf2_cu *cu,
13674 LONGEST *value, gdb_byte **bytes,
13675 struct dwarf2_locexpr_baton **baton)
13676 {
13677 struct objfile *objfile = cu->objfile;
13678 struct comp_unit_head *cu_header = &cu->header;
13679 struct dwarf_block *blk;
13680 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13681 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13682
13683 *value = 0;
13684 *bytes = NULL;
13685 *baton = NULL;
13686
13687 switch (attr->form)
13688 {
13689 case DW_FORM_addr:
13690 case DW_FORM_GNU_addr_index:
13691 {
13692 gdb_byte *data;
13693
13694 if (TYPE_LENGTH (type) != cu_header->addr_size)
13695 dwarf2_const_value_length_mismatch_complaint (name,
13696 cu_header->addr_size,
13697 TYPE_LENGTH (type));
13698 /* Symbols of this form are reasonably rare, so we just
13699 piggyback on the existing location code rather than writing
13700 a new implementation of symbol_computed_ops. */
13701 *baton = obstack_alloc (&objfile->objfile_obstack,
13702 sizeof (struct dwarf2_locexpr_baton));
13703 (*baton)->per_cu = cu->per_cu;
13704 gdb_assert ((*baton)->per_cu);
13705
13706 (*baton)->size = 2 + cu_header->addr_size;
13707 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13708 (*baton)->data = data;
13709
13710 data[0] = DW_OP_addr;
13711 store_unsigned_integer (&data[1], cu_header->addr_size,
13712 byte_order, DW_ADDR (attr));
13713 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13714 }
13715 break;
13716 case DW_FORM_string:
13717 case DW_FORM_strp:
13718 case DW_FORM_GNU_str_index:
13719 /* DW_STRING is already allocated on the objfile obstack, point
13720 directly to it. */
13721 *bytes = (gdb_byte *) DW_STRING (attr);
13722 break;
13723 case DW_FORM_block1:
13724 case DW_FORM_block2:
13725 case DW_FORM_block4:
13726 case DW_FORM_block:
13727 case DW_FORM_exprloc:
13728 blk = DW_BLOCK (attr);
13729 if (TYPE_LENGTH (type) != blk->size)
13730 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13731 TYPE_LENGTH (type));
13732 *bytes = blk->data;
13733 break;
13734
13735 /* The DW_AT_const_value attributes are supposed to carry the
13736 symbol's value "represented as it would be on the target
13737 architecture." By the time we get here, it's already been
13738 converted to host endianness, so we just need to sign- or
13739 zero-extend it as appropriate. */
13740 case DW_FORM_data1:
13741 *bytes = dwarf2_const_value_data (attr, type, name,
13742 obstack, cu, value, 8);
13743 break;
13744 case DW_FORM_data2:
13745 *bytes = dwarf2_const_value_data (attr, type, name,
13746 obstack, cu, value, 16);
13747 break;
13748 case DW_FORM_data4:
13749 *bytes = dwarf2_const_value_data (attr, type, name,
13750 obstack, cu, value, 32);
13751 break;
13752 case DW_FORM_data8:
13753 *bytes = dwarf2_const_value_data (attr, type, name,
13754 obstack, cu, value, 64);
13755 break;
13756
13757 case DW_FORM_sdata:
13758 *value = DW_SND (attr);
13759 break;
13760
13761 case DW_FORM_udata:
13762 *value = DW_UNSND (attr);
13763 break;
13764
13765 default:
13766 complaint (&symfile_complaints,
13767 _("unsupported const value attribute form: '%s'"),
13768 dwarf_form_name (attr->form));
13769 *value = 0;
13770 break;
13771 }
13772 }
13773
13774
13775 /* Copy constant value from an attribute to a symbol. */
13776
13777 static void
13778 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13779 struct dwarf2_cu *cu)
13780 {
13781 struct objfile *objfile = cu->objfile;
13782 struct comp_unit_head *cu_header = &cu->header;
13783 LONGEST value;
13784 gdb_byte *bytes;
13785 struct dwarf2_locexpr_baton *baton;
13786
13787 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13788 SYMBOL_PRINT_NAME (sym),
13789 &objfile->objfile_obstack, cu,
13790 &value, &bytes, &baton);
13791
13792 if (baton != NULL)
13793 {
13794 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13795 SYMBOL_LOCATION_BATON (sym) = baton;
13796 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13797 }
13798 else if (bytes != NULL)
13799 {
13800 SYMBOL_VALUE_BYTES (sym) = bytes;
13801 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13802 }
13803 else
13804 {
13805 SYMBOL_VALUE (sym) = value;
13806 SYMBOL_CLASS (sym) = LOC_CONST;
13807 }
13808 }
13809
13810 /* Return the type of the die in question using its DW_AT_type attribute. */
13811
13812 static struct type *
13813 die_type (struct die_info *die, struct dwarf2_cu *cu)
13814 {
13815 struct attribute *type_attr;
13816
13817 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13818 if (!type_attr)
13819 {
13820 /* A missing DW_AT_type represents a void type. */
13821 return objfile_type (cu->objfile)->builtin_void;
13822 }
13823
13824 return lookup_die_type (die, type_attr, cu);
13825 }
13826
13827 /* True iff CU's producer generates GNAT Ada auxiliary information
13828 that allows to find parallel types through that information instead
13829 of having to do expensive parallel lookups by type name. */
13830
13831 static int
13832 need_gnat_info (struct dwarf2_cu *cu)
13833 {
13834 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13835 of GNAT produces this auxiliary information, without any indication
13836 that it is produced. Part of enhancing the FSF version of GNAT
13837 to produce that information will be to put in place an indicator
13838 that we can use in order to determine whether the descriptive type
13839 info is available or not. One suggestion that has been made is
13840 to use a new attribute, attached to the CU die. For now, assume
13841 that the descriptive type info is not available. */
13842 return 0;
13843 }
13844
13845 /* Return the auxiliary type of the die in question using its
13846 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13847 attribute is not present. */
13848
13849 static struct type *
13850 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13851 {
13852 struct attribute *type_attr;
13853
13854 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13855 if (!type_attr)
13856 return NULL;
13857
13858 return lookup_die_type (die, type_attr, cu);
13859 }
13860
13861 /* If DIE has a descriptive_type attribute, then set the TYPE's
13862 descriptive type accordingly. */
13863
13864 static void
13865 set_descriptive_type (struct type *type, struct die_info *die,
13866 struct dwarf2_cu *cu)
13867 {
13868 struct type *descriptive_type = die_descriptive_type (die, cu);
13869
13870 if (descriptive_type)
13871 {
13872 ALLOCATE_GNAT_AUX_TYPE (type);
13873 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13874 }
13875 }
13876
13877 /* Return the containing type of the die in question using its
13878 DW_AT_containing_type attribute. */
13879
13880 static struct type *
13881 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13882 {
13883 struct attribute *type_attr;
13884
13885 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13886 if (!type_attr)
13887 error (_("Dwarf Error: Problem turning containing type into gdb type "
13888 "[in module %s]"), cu->objfile->name);
13889
13890 return lookup_die_type (die, type_attr, cu);
13891 }
13892
13893 /* Look up the type of DIE in CU using its type attribute ATTR.
13894 If there is no type substitute an error marker. */
13895
13896 static struct type *
13897 lookup_die_type (struct die_info *die, struct attribute *attr,
13898 struct dwarf2_cu *cu)
13899 {
13900 struct objfile *objfile = cu->objfile;
13901 struct type *this_type;
13902
13903 /* First see if we have it cached. */
13904
13905 if (is_ref_attr (attr))
13906 {
13907 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13908
13909 this_type = get_die_type_at_offset (offset, cu->per_cu);
13910 }
13911 else if (attr->form == DW_FORM_ref_sig8)
13912 {
13913 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13914
13915 /* sig_type will be NULL if the signatured type is missing from
13916 the debug info. */
13917 if (sig_type == NULL)
13918 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13919 "at 0x%x [in module %s]"),
13920 die->offset.sect_off, objfile->name);
13921
13922 gdb_assert (sig_type->per_cu.is_debug_types);
13923 /* If we haven't filled in type_offset_in_section yet, then we
13924 haven't read the type in yet. */
13925 this_type = NULL;
13926 if (sig_type->type_offset_in_section.sect_off != 0)
13927 {
13928 this_type =
13929 get_die_type_at_offset (sig_type->type_offset_in_section,
13930 &sig_type->per_cu);
13931 }
13932 }
13933 else
13934 {
13935 dump_die_for_error (die);
13936 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13937 dwarf_attr_name (attr->name), objfile->name);
13938 }
13939
13940 /* If not cached we need to read it in. */
13941
13942 if (this_type == NULL)
13943 {
13944 struct die_info *type_die;
13945 struct dwarf2_cu *type_cu = cu;
13946
13947 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13948 /* If we found the type now, it's probably because the type came
13949 from an inter-CU reference and the type's CU got expanded before
13950 ours. */
13951 this_type = get_die_type (type_die, type_cu);
13952 if (this_type == NULL)
13953 this_type = read_type_die_1 (type_die, type_cu);
13954 }
13955
13956 /* If we still don't have a type use an error marker. */
13957
13958 if (this_type == NULL)
13959 {
13960 char *message, *saved;
13961
13962 /* read_type_die already issued a complaint. */
13963 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13964 objfile->name,
13965 cu->header.offset.sect_off,
13966 die->offset.sect_off);
13967 saved = obstack_copy0 (&objfile->objfile_obstack,
13968 message, strlen (message));
13969 xfree (message);
13970
13971 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13972 }
13973
13974 return this_type;
13975 }
13976
13977 /* Return the type in DIE, CU.
13978 Returns NULL for invalid types.
13979
13980 This first does a lookup in the appropriate type_hash table,
13981 and only reads the die in if necessary.
13982
13983 NOTE: This can be called when reading in partial or full symbols. */
13984
13985 static struct type *
13986 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13987 {
13988 struct type *this_type;
13989
13990 this_type = get_die_type (die, cu);
13991 if (this_type)
13992 return this_type;
13993
13994 return read_type_die_1 (die, cu);
13995 }
13996
13997 /* Read the type in DIE, CU.
13998 Returns NULL for invalid types. */
13999
14000 static struct type *
14001 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14002 {
14003 struct type *this_type = NULL;
14004
14005 switch (die->tag)
14006 {
14007 case DW_TAG_class_type:
14008 case DW_TAG_interface_type:
14009 case DW_TAG_structure_type:
14010 case DW_TAG_union_type:
14011 this_type = read_structure_type (die, cu);
14012 break;
14013 case DW_TAG_enumeration_type:
14014 this_type = read_enumeration_type (die, cu);
14015 break;
14016 case DW_TAG_subprogram:
14017 case DW_TAG_subroutine_type:
14018 case DW_TAG_inlined_subroutine:
14019 this_type = read_subroutine_type (die, cu);
14020 break;
14021 case DW_TAG_array_type:
14022 this_type = read_array_type (die, cu);
14023 break;
14024 case DW_TAG_set_type:
14025 this_type = read_set_type (die, cu);
14026 break;
14027 case DW_TAG_pointer_type:
14028 this_type = read_tag_pointer_type (die, cu);
14029 break;
14030 case DW_TAG_ptr_to_member_type:
14031 this_type = read_tag_ptr_to_member_type (die, cu);
14032 break;
14033 case DW_TAG_reference_type:
14034 this_type = read_tag_reference_type (die, cu);
14035 break;
14036 case DW_TAG_const_type:
14037 this_type = read_tag_const_type (die, cu);
14038 break;
14039 case DW_TAG_volatile_type:
14040 this_type = read_tag_volatile_type (die, cu);
14041 break;
14042 case DW_TAG_string_type:
14043 this_type = read_tag_string_type (die, cu);
14044 break;
14045 case DW_TAG_typedef:
14046 this_type = read_typedef (die, cu);
14047 break;
14048 case DW_TAG_subrange_type:
14049 this_type = read_subrange_type (die, cu);
14050 break;
14051 case DW_TAG_base_type:
14052 this_type = read_base_type (die, cu);
14053 break;
14054 case DW_TAG_unspecified_type:
14055 this_type = read_unspecified_type (die, cu);
14056 break;
14057 case DW_TAG_namespace:
14058 this_type = read_namespace_type (die, cu);
14059 break;
14060 case DW_TAG_module:
14061 this_type = read_module_type (die, cu);
14062 break;
14063 default:
14064 complaint (&symfile_complaints,
14065 _("unexpected tag in read_type_die: '%s'"),
14066 dwarf_tag_name (die->tag));
14067 break;
14068 }
14069
14070 return this_type;
14071 }
14072
14073 /* See if we can figure out if the class lives in a namespace. We do
14074 this by looking for a member function; its demangled name will
14075 contain namespace info, if there is any.
14076 Return the computed name or NULL.
14077 Space for the result is allocated on the objfile's obstack.
14078 This is the full-die version of guess_partial_die_structure_name.
14079 In this case we know DIE has no useful parent. */
14080
14081 static char *
14082 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14083 {
14084 struct die_info *spec_die;
14085 struct dwarf2_cu *spec_cu;
14086 struct die_info *child;
14087
14088 spec_cu = cu;
14089 spec_die = die_specification (die, &spec_cu);
14090 if (spec_die != NULL)
14091 {
14092 die = spec_die;
14093 cu = spec_cu;
14094 }
14095
14096 for (child = die->child;
14097 child != NULL;
14098 child = child->sibling)
14099 {
14100 if (child->tag == DW_TAG_subprogram)
14101 {
14102 struct attribute *attr;
14103
14104 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14105 if (attr == NULL)
14106 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14107 if (attr != NULL)
14108 {
14109 char *actual_name
14110 = language_class_name_from_physname (cu->language_defn,
14111 DW_STRING (attr));
14112 char *name = NULL;
14113
14114 if (actual_name != NULL)
14115 {
14116 char *die_name = dwarf2_name (die, cu);
14117
14118 if (die_name != NULL
14119 && strcmp (die_name, actual_name) != 0)
14120 {
14121 /* Strip off the class name from the full name.
14122 We want the prefix. */
14123 int die_name_len = strlen (die_name);
14124 int actual_name_len = strlen (actual_name);
14125
14126 /* Test for '::' as a sanity check. */
14127 if (actual_name_len > die_name_len + 2
14128 && actual_name[actual_name_len
14129 - die_name_len - 1] == ':')
14130 name =
14131 obsavestring (actual_name,
14132 actual_name_len - die_name_len - 2,
14133 &cu->objfile->objfile_obstack);
14134 }
14135 }
14136 xfree (actual_name);
14137 return name;
14138 }
14139 }
14140 }
14141
14142 return NULL;
14143 }
14144
14145 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14146 prefix part in such case. See
14147 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14148
14149 static char *
14150 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14151 {
14152 struct attribute *attr;
14153 char *base;
14154
14155 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14156 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14157 return NULL;
14158
14159 attr = dwarf2_attr (die, DW_AT_name, cu);
14160 if (attr != NULL && DW_STRING (attr) != NULL)
14161 return NULL;
14162
14163 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14164 if (attr == NULL)
14165 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14166 if (attr == NULL || DW_STRING (attr) == NULL)
14167 return NULL;
14168
14169 /* dwarf2_name had to be already called. */
14170 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14171
14172 /* Strip the base name, keep any leading namespaces/classes. */
14173 base = strrchr (DW_STRING (attr), ':');
14174 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14175 return "";
14176
14177 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14178 &cu->objfile->objfile_obstack);
14179 }
14180
14181 /* Return the name of the namespace/class that DIE is defined within,
14182 or "" if we can't tell. The caller should not xfree the result.
14183
14184 For example, if we're within the method foo() in the following
14185 code:
14186
14187 namespace N {
14188 class C {
14189 void foo () {
14190 }
14191 };
14192 }
14193
14194 then determine_prefix on foo's die will return "N::C". */
14195
14196 static const char *
14197 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14198 {
14199 struct die_info *parent, *spec_die;
14200 struct dwarf2_cu *spec_cu;
14201 struct type *parent_type;
14202 char *retval;
14203
14204 if (cu->language != language_cplus && cu->language != language_java
14205 && cu->language != language_fortran)
14206 return "";
14207
14208 retval = anonymous_struct_prefix (die, cu);
14209 if (retval)
14210 return retval;
14211
14212 /* We have to be careful in the presence of DW_AT_specification.
14213 For example, with GCC 3.4, given the code
14214
14215 namespace N {
14216 void foo() {
14217 // Definition of N::foo.
14218 }
14219 }
14220
14221 then we'll have a tree of DIEs like this:
14222
14223 1: DW_TAG_compile_unit
14224 2: DW_TAG_namespace // N
14225 3: DW_TAG_subprogram // declaration of N::foo
14226 4: DW_TAG_subprogram // definition of N::foo
14227 DW_AT_specification // refers to die #3
14228
14229 Thus, when processing die #4, we have to pretend that we're in
14230 the context of its DW_AT_specification, namely the contex of die
14231 #3. */
14232 spec_cu = cu;
14233 spec_die = die_specification (die, &spec_cu);
14234 if (spec_die == NULL)
14235 parent = die->parent;
14236 else
14237 {
14238 parent = spec_die->parent;
14239 cu = spec_cu;
14240 }
14241
14242 if (parent == NULL)
14243 return "";
14244 else if (parent->building_fullname)
14245 {
14246 const char *name;
14247 const char *parent_name;
14248
14249 /* It has been seen on RealView 2.2 built binaries,
14250 DW_TAG_template_type_param types actually _defined_ as
14251 children of the parent class:
14252
14253 enum E {};
14254 template class <class Enum> Class{};
14255 Class<enum E> class_e;
14256
14257 1: DW_TAG_class_type (Class)
14258 2: DW_TAG_enumeration_type (E)
14259 3: DW_TAG_enumerator (enum1:0)
14260 3: DW_TAG_enumerator (enum2:1)
14261 ...
14262 2: DW_TAG_template_type_param
14263 DW_AT_type DW_FORM_ref_udata (E)
14264
14265 Besides being broken debug info, it can put GDB into an
14266 infinite loop. Consider:
14267
14268 When we're building the full name for Class<E>, we'll start
14269 at Class, and go look over its template type parameters,
14270 finding E. We'll then try to build the full name of E, and
14271 reach here. We're now trying to build the full name of E,
14272 and look over the parent DIE for containing scope. In the
14273 broken case, if we followed the parent DIE of E, we'd again
14274 find Class, and once again go look at its template type
14275 arguments, etc., etc. Simply don't consider such parent die
14276 as source-level parent of this die (it can't be, the language
14277 doesn't allow it), and break the loop here. */
14278 name = dwarf2_name (die, cu);
14279 parent_name = dwarf2_name (parent, cu);
14280 complaint (&symfile_complaints,
14281 _("template param type '%s' defined within parent '%s'"),
14282 name ? name : "<unknown>",
14283 parent_name ? parent_name : "<unknown>");
14284 return "";
14285 }
14286 else
14287 switch (parent->tag)
14288 {
14289 case DW_TAG_namespace:
14290 parent_type = read_type_die (parent, cu);
14291 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14292 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14293 Work around this problem here. */
14294 if (cu->language == language_cplus
14295 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14296 return "";
14297 /* We give a name to even anonymous namespaces. */
14298 return TYPE_TAG_NAME (parent_type);
14299 case DW_TAG_class_type:
14300 case DW_TAG_interface_type:
14301 case DW_TAG_structure_type:
14302 case DW_TAG_union_type:
14303 case DW_TAG_module:
14304 parent_type = read_type_die (parent, cu);
14305 if (TYPE_TAG_NAME (parent_type) != NULL)
14306 return TYPE_TAG_NAME (parent_type);
14307 else
14308 /* An anonymous structure is only allowed non-static data
14309 members; no typedefs, no member functions, et cetera.
14310 So it does not need a prefix. */
14311 return "";
14312 case DW_TAG_compile_unit:
14313 case DW_TAG_partial_unit:
14314 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14315 if (cu->language == language_cplus
14316 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14317 && die->child != NULL
14318 && (die->tag == DW_TAG_class_type
14319 || die->tag == DW_TAG_structure_type
14320 || die->tag == DW_TAG_union_type))
14321 {
14322 char *name = guess_full_die_structure_name (die, cu);
14323 if (name != NULL)
14324 return name;
14325 }
14326 return "";
14327 default:
14328 return determine_prefix (parent, cu);
14329 }
14330 }
14331
14332 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14333 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14334 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14335 an obconcat, otherwise allocate storage for the result. The CU argument is
14336 used to determine the language and hence, the appropriate separator. */
14337
14338 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14339
14340 static char *
14341 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14342 int physname, struct dwarf2_cu *cu)
14343 {
14344 const char *lead = "";
14345 const char *sep;
14346
14347 if (suffix == NULL || suffix[0] == '\0'
14348 || prefix == NULL || prefix[0] == '\0')
14349 sep = "";
14350 else if (cu->language == language_java)
14351 sep = ".";
14352 else if (cu->language == language_fortran && physname)
14353 {
14354 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14355 DW_AT_MIPS_linkage_name is preferred and used instead. */
14356
14357 lead = "__";
14358 sep = "_MOD_";
14359 }
14360 else
14361 sep = "::";
14362
14363 if (prefix == NULL)
14364 prefix = "";
14365 if (suffix == NULL)
14366 suffix = "";
14367
14368 if (obs == NULL)
14369 {
14370 char *retval
14371 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14372
14373 strcpy (retval, lead);
14374 strcat (retval, prefix);
14375 strcat (retval, sep);
14376 strcat (retval, suffix);
14377 return retval;
14378 }
14379 else
14380 {
14381 /* We have an obstack. */
14382 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14383 }
14384 }
14385
14386 /* Return sibling of die, NULL if no sibling. */
14387
14388 static struct die_info *
14389 sibling_die (struct die_info *die)
14390 {
14391 return die->sibling;
14392 }
14393
14394 /* Get name of a die, return NULL if not found. */
14395
14396 static char *
14397 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14398 struct obstack *obstack)
14399 {
14400 if (name && cu->language == language_cplus)
14401 {
14402 char *canon_name = cp_canonicalize_string (name);
14403
14404 if (canon_name != NULL)
14405 {
14406 if (strcmp (canon_name, name) != 0)
14407 name = obsavestring (canon_name, strlen (canon_name),
14408 obstack);
14409 xfree (canon_name);
14410 }
14411 }
14412
14413 return name;
14414 }
14415
14416 /* Get name of a die, return NULL if not found. */
14417
14418 static char *
14419 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14420 {
14421 struct attribute *attr;
14422
14423 attr = dwarf2_attr (die, DW_AT_name, cu);
14424 if ((!attr || !DW_STRING (attr))
14425 && die->tag != DW_TAG_class_type
14426 && die->tag != DW_TAG_interface_type
14427 && die->tag != DW_TAG_structure_type
14428 && die->tag != DW_TAG_union_type)
14429 return NULL;
14430
14431 switch (die->tag)
14432 {
14433 case DW_TAG_compile_unit:
14434 case DW_TAG_partial_unit:
14435 /* Compilation units have a DW_AT_name that is a filename, not
14436 a source language identifier. */
14437 case DW_TAG_enumeration_type:
14438 case DW_TAG_enumerator:
14439 /* These tags always have simple identifiers already; no need
14440 to canonicalize them. */
14441 return DW_STRING (attr);
14442
14443 case DW_TAG_subprogram:
14444 /* Java constructors will all be named "<init>", so return
14445 the class name when we see this special case. */
14446 if (cu->language == language_java
14447 && DW_STRING (attr) != NULL
14448 && strcmp (DW_STRING (attr), "<init>") == 0)
14449 {
14450 struct dwarf2_cu *spec_cu = cu;
14451 struct die_info *spec_die;
14452
14453 /* GCJ will output '<init>' for Java constructor names.
14454 For this special case, return the name of the parent class. */
14455
14456 /* GCJ may output suprogram DIEs with AT_specification set.
14457 If so, use the name of the specified DIE. */
14458 spec_die = die_specification (die, &spec_cu);
14459 if (spec_die != NULL)
14460 return dwarf2_name (spec_die, spec_cu);
14461
14462 do
14463 {
14464 die = die->parent;
14465 if (die->tag == DW_TAG_class_type)
14466 return dwarf2_name (die, cu);
14467 }
14468 while (die->tag != DW_TAG_compile_unit
14469 && die->tag != DW_TAG_partial_unit);
14470 }
14471 break;
14472
14473 case DW_TAG_class_type:
14474 case DW_TAG_interface_type:
14475 case DW_TAG_structure_type:
14476 case DW_TAG_union_type:
14477 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14478 structures or unions. These were of the form "._%d" in GCC 4.1,
14479 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14480 and GCC 4.4. We work around this problem by ignoring these. */
14481 if (attr && DW_STRING (attr)
14482 && (strncmp (DW_STRING (attr), "._", 2) == 0
14483 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14484 return NULL;
14485
14486 /* GCC might emit a nameless typedef that has a linkage name. See
14487 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14488 if (!attr || DW_STRING (attr) == NULL)
14489 {
14490 char *demangled = NULL;
14491
14492 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14493 if (attr == NULL)
14494 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14495
14496 if (attr == NULL || DW_STRING (attr) == NULL)
14497 return NULL;
14498
14499 /* Avoid demangling DW_STRING (attr) the second time on a second
14500 call for the same DIE. */
14501 if (!DW_STRING_IS_CANONICAL (attr))
14502 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14503
14504 if (demangled)
14505 {
14506 char *base;
14507
14508 /* FIXME: we already did this for the partial symbol... */
14509 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14510 &cu->objfile->objfile_obstack);
14511 DW_STRING_IS_CANONICAL (attr) = 1;
14512 xfree (demangled);
14513
14514 /* Strip any leading namespaces/classes, keep only the base name.
14515 DW_AT_name for named DIEs does not contain the prefixes. */
14516 base = strrchr (DW_STRING (attr), ':');
14517 if (base && base > DW_STRING (attr) && base[-1] == ':')
14518 return &base[1];
14519 else
14520 return DW_STRING (attr);
14521 }
14522 }
14523 break;
14524
14525 default:
14526 break;
14527 }
14528
14529 if (!DW_STRING_IS_CANONICAL (attr))
14530 {
14531 DW_STRING (attr)
14532 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14533 &cu->objfile->objfile_obstack);
14534 DW_STRING_IS_CANONICAL (attr) = 1;
14535 }
14536 return DW_STRING (attr);
14537 }
14538
14539 /* Return the die that this die in an extension of, or NULL if there
14540 is none. *EXT_CU is the CU containing DIE on input, and the CU
14541 containing the return value on output. */
14542
14543 static struct die_info *
14544 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14545 {
14546 struct attribute *attr;
14547
14548 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14549 if (attr == NULL)
14550 return NULL;
14551
14552 return follow_die_ref (die, attr, ext_cu);
14553 }
14554
14555 /* Convert a DIE tag into its string name. */
14556
14557 static const char *
14558 dwarf_tag_name (unsigned tag)
14559 {
14560 const char *name = get_DW_TAG_name (tag);
14561
14562 if (name == NULL)
14563 return "DW_TAG_<unknown>";
14564
14565 return name;
14566 }
14567
14568 /* Convert a DWARF attribute code into its string name. */
14569
14570 static const char *
14571 dwarf_attr_name (unsigned attr)
14572 {
14573 const char *name;
14574
14575 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14576 if (attr == DW_AT_MIPS_fde)
14577 return "DW_AT_MIPS_fde";
14578 #else
14579 if (attr == DW_AT_HP_block_index)
14580 return "DW_AT_HP_block_index";
14581 #endif
14582
14583 name = get_DW_AT_name (attr);
14584
14585 if (name == NULL)
14586 return "DW_AT_<unknown>";
14587
14588 return name;
14589 }
14590
14591 /* Convert a DWARF value form code into its string name. */
14592
14593 static const char *
14594 dwarf_form_name (unsigned form)
14595 {
14596 const char *name = get_DW_FORM_name (form);
14597
14598 if (name == NULL)
14599 return "DW_FORM_<unknown>";
14600
14601 return name;
14602 }
14603
14604 static char *
14605 dwarf_bool_name (unsigned mybool)
14606 {
14607 if (mybool)
14608 return "TRUE";
14609 else
14610 return "FALSE";
14611 }
14612
14613 /* Convert a DWARF type code into its string name. */
14614
14615 static const char *
14616 dwarf_type_encoding_name (unsigned enc)
14617 {
14618 const char *name = get_DW_ATE_name (enc);
14619
14620 if (name == NULL)
14621 return "DW_ATE_<unknown>";
14622
14623 return name;
14624 }
14625
14626 static void
14627 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14628 {
14629 unsigned int i;
14630
14631 print_spaces (indent, f);
14632 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14633 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14634
14635 if (die->parent != NULL)
14636 {
14637 print_spaces (indent, f);
14638 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14639 die->parent->offset.sect_off);
14640 }
14641
14642 print_spaces (indent, f);
14643 fprintf_unfiltered (f, " has children: %s\n",
14644 dwarf_bool_name (die->child != NULL));
14645
14646 print_spaces (indent, f);
14647 fprintf_unfiltered (f, " attributes:\n");
14648
14649 for (i = 0; i < die->num_attrs; ++i)
14650 {
14651 print_spaces (indent, f);
14652 fprintf_unfiltered (f, " %s (%s) ",
14653 dwarf_attr_name (die->attrs[i].name),
14654 dwarf_form_name (die->attrs[i].form));
14655
14656 switch (die->attrs[i].form)
14657 {
14658 case DW_FORM_addr:
14659 case DW_FORM_GNU_addr_index:
14660 fprintf_unfiltered (f, "address: ");
14661 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14662 break;
14663 case DW_FORM_block2:
14664 case DW_FORM_block4:
14665 case DW_FORM_block:
14666 case DW_FORM_block1:
14667 fprintf_unfiltered (f, "block: size %d",
14668 DW_BLOCK (&die->attrs[i])->size);
14669 break;
14670 case DW_FORM_exprloc:
14671 fprintf_unfiltered (f, "expression: size %u",
14672 DW_BLOCK (&die->attrs[i])->size);
14673 break;
14674 case DW_FORM_ref_addr:
14675 fprintf_unfiltered (f, "ref address: ");
14676 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14677 break;
14678 case DW_FORM_ref1:
14679 case DW_FORM_ref2:
14680 case DW_FORM_ref4:
14681 case DW_FORM_ref8:
14682 case DW_FORM_ref_udata:
14683 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14684 (long) (DW_UNSND (&die->attrs[i])));
14685 break;
14686 case DW_FORM_data1:
14687 case DW_FORM_data2:
14688 case DW_FORM_data4:
14689 case DW_FORM_data8:
14690 case DW_FORM_udata:
14691 case DW_FORM_sdata:
14692 fprintf_unfiltered (f, "constant: %s",
14693 pulongest (DW_UNSND (&die->attrs[i])));
14694 break;
14695 case DW_FORM_sec_offset:
14696 fprintf_unfiltered (f, "section offset: %s",
14697 pulongest (DW_UNSND (&die->attrs[i])));
14698 break;
14699 case DW_FORM_ref_sig8:
14700 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14701 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14702 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14703 else
14704 fprintf_unfiltered (f, "signatured type, offset: unknown");
14705 break;
14706 case DW_FORM_string:
14707 case DW_FORM_strp:
14708 case DW_FORM_GNU_str_index:
14709 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14710 DW_STRING (&die->attrs[i])
14711 ? DW_STRING (&die->attrs[i]) : "",
14712 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14713 break;
14714 case DW_FORM_flag:
14715 if (DW_UNSND (&die->attrs[i]))
14716 fprintf_unfiltered (f, "flag: TRUE");
14717 else
14718 fprintf_unfiltered (f, "flag: FALSE");
14719 break;
14720 case DW_FORM_flag_present:
14721 fprintf_unfiltered (f, "flag: TRUE");
14722 break;
14723 case DW_FORM_indirect:
14724 /* The reader will have reduced the indirect form to
14725 the "base form" so this form should not occur. */
14726 fprintf_unfiltered (f,
14727 "unexpected attribute form: DW_FORM_indirect");
14728 break;
14729 default:
14730 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14731 die->attrs[i].form);
14732 break;
14733 }
14734 fprintf_unfiltered (f, "\n");
14735 }
14736 }
14737
14738 static void
14739 dump_die_for_error (struct die_info *die)
14740 {
14741 dump_die_shallow (gdb_stderr, 0, die);
14742 }
14743
14744 static void
14745 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14746 {
14747 int indent = level * 4;
14748
14749 gdb_assert (die != NULL);
14750
14751 if (level >= max_level)
14752 return;
14753
14754 dump_die_shallow (f, indent, die);
14755
14756 if (die->child != NULL)
14757 {
14758 print_spaces (indent, f);
14759 fprintf_unfiltered (f, " Children:");
14760 if (level + 1 < max_level)
14761 {
14762 fprintf_unfiltered (f, "\n");
14763 dump_die_1 (f, level + 1, max_level, die->child);
14764 }
14765 else
14766 {
14767 fprintf_unfiltered (f,
14768 " [not printed, max nesting level reached]\n");
14769 }
14770 }
14771
14772 if (die->sibling != NULL && level > 0)
14773 {
14774 dump_die_1 (f, level, max_level, die->sibling);
14775 }
14776 }
14777
14778 /* This is called from the pdie macro in gdbinit.in.
14779 It's not static so gcc will keep a copy callable from gdb. */
14780
14781 void
14782 dump_die (struct die_info *die, int max_level)
14783 {
14784 dump_die_1 (gdb_stdlog, 0, max_level, die);
14785 }
14786
14787 static void
14788 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14789 {
14790 void **slot;
14791
14792 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14793 INSERT);
14794
14795 *slot = die;
14796 }
14797
14798 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14799 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14800
14801 static int
14802 is_ref_attr (struct attribute *attr)
14803 {
14804 switch (attr->form)
14805 {
14806 case DW_FORM_ref_addr:
14807 case DW_FORM_ref1:
14808 case DW_FORM_ref2:
14809 case DW_FORM_ref4:
14810 case DW_FORM_ref8:
14811 case DW_FORM_ref_udata:
14812 return 1;
14813 default:
14814 return 0;
14815 }
14816 }
14817
14818 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14819 required kind. */
14820
14821 static sect_offset
14822 dwarf2_get_ref_die_offset (struct attribute *attr)
14823 {
14824 sect_offset retval = { DW_UNSND (attr) };
14825
14826 if (is_ref_attr (attr))
14827 return retval;
14828
14829 retval.sect_off = 0;
14830 complaint (&symfile_complaints,
14831 _("unsupported die ref attribute form: '%s'"),
14832 dwarf_form_name (attr->form));
14833 return retval;
14834 }
14835
14836 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14837 * the value held by the attribute is not constant. */
14838
14839 static LONGEST
14840 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14841 {
14842 if (attr->form == DW_FORM_sdata)
14843 return DW_SND (attr);
14844 else if (attr->form == DW_FORM_udata
14845 || attr->form == DW_FORM_data1
14846 || attr->form == DW_FORM_data2
14847 || attr->form == DW_FORM_data4
14848 || attr->form == DW_FORM_data8)
14849 return DW_UNSND (attr);
14850 else
14851 {
14852 complaint (&symfile_complaints,
14853 _("Attribute value is not a constant (%s)"),
14854 dwarf_form_name (attr->form));
14855 return default_value;
14856 }
14857 }
14858
14859 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14860 unit and add it to our queue.
14861 The result is non-zero if PER_CU was queued, otherwise the result is zero
14862 meaning either PER_CU is already queued or it is already loaded. */
14863
14864 static int
14865 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14866 struct dwarf2_per_cu_data *per_cu,
14867 enum language pretend_language)
14868 {
14869 /* We may arrive here during partial symbol reading, if we need full
14870 DIEs to process an unusual case (e.g. template arguments). Do
14871 not queue PER_CU, just tell our caller to load its DIEs. */
14872 if (dwarf2_per_objfile->reading_partial_symbols)
14873 {
14874 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14875 return 1;
14876 return 0;
14877 }
14878
14879 /* Mark the dependence relation so that we don't flush PER_CU
14880 too early. */
14881 dwarf2_add_dependence (this_cu, per_cu);
14882
14883 /* If it's already on the queue, we have nothing to do. */
14884 if (per_cu->queued)
14885 return 0;
14886
14887 /* If the compilation unit is already loaded, just mark it as
14888 used. */
14889 if (per_cu->cu != NULL)
14890 {
14891 per_cu->cu->last_used = 0;
14892 return 0;
14893 }
14894
14895 /* Add it to the queue. */
14896 queue_comp_unit (per_cu, pretend_language);
14897
14898 return 1;
14899 }
14900
14901 /* Follow reference or signature attribute ATTR of SRC_DIE.
14902 On entry *REF_CU is the CU of SRC_DIE.
14903 On exit *REF_CU is the CU of the result. */
14904
14905 static struct die_info *
14906 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14907 struct dwarf2_cu **ref_cu)
14908 {
14909 struct die_info *die;
14910
14911 if (is_ref_attr (attr))
14912 die = follow_die_ref (src_die, attr, ref_cu);
14913 else if (attr->form == DW_FORM_ref_sig8)
14914 die = follow_die_sig (src_die, attr, ref_cu);
14915 else
14916 {
14917 dump_die_for_error (src_die);
14918 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14919 (*ref_cu)->objfile->name);
14920 }
14921
14922 return die;
14923 }
14924
14925 /* Follow reference OFFSET.
14926 On entry *REF_CU is the CU of the source die referencing OFFSET.
14927 On exit *REF_CU is the CU of the result.
14928 Returns NULL if OFFSET is invalid. */
14929
14930 static struct die_info *
14931 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14932 {
14933 struct die_info temp_die;
14934 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14935
14936 gdb_assert (cu->per_cu != NULL);
14937
14938 target_cu = cu;
14939
14940 if (cu->per_cu->is_debug_types)
14941 {
14942 /* .debug_types CUs cannot reference anything outside their CU.
14943 If they need to, they have to reference a signatured type via
14944 DW_FORM_ref_sig8. */
14945 if (! offset_in_cu_p (&cu->header, offset))
14946 return NULL;
14947 }
14948 else if (! offset_in_cu_p (&cu->header, offset))
14949 {
14950 struct dwarf2_per_cu_data *per_cu;
14951
14952 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14953
14954 /* If necessary, add it to the queue and load its DIEs. */
14955 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14956 load_full_comp_unit (per_cu, cu->language);
14957
14958 target_cu = per_cu->cu;
14959 }
14960 else if (cu->dies == NULL)
14961 {
14962 /* We're loading full DIEs during partial symbol reading. */
14963 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14964 load_full_comp_unit (cu->per_cu, language_minimal);
14965 }
14966
14967 *ref_cu = target_cu;
14968 temp_die.offset = offset;
14969 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14970 }
14971
14972 /* Follow reference attribute ATTR of SRC_DIE.
14973 On entry *REF_CU is the CU of SRC_DIE.
14974 On exit *REF_CU is the CU of the result. */
14975
14976 static struct die_info *
14977 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14978 struct dwarf2_cu **ref_cu)
14979 {
14980 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14981 struct dwarf2_cu *cu = *ref_cu;
14982 struct die_info *die;
14983
14984 die = follow_die_offset (offset, ref_cu);
14985 if (!die)
14986 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14987 "at 0x%x [in module %s]"),
14988 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14989
14990 return die;
14991 }
14992
14993 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14994 Returned value is intended for DW_OP_call*. Returned
14995 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14996
14997 struct dwarf2_locexpr_baton
14998 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14999 struct dwarf2_per_cu_data *per_cu,
15000 CORE_ADDR (*get_frame_pc) (void *baton),
15001 void *baton)
15002 {
15003 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15004 struct dwarf2_cu *cu;
15005 struct die_info *die;
15006 struct attribute *attr;
15007 struct dwarf2_locexpr_baton retval;
15008
15009 dw2_setup (per_cu->objfile);
15010
15011 if (per_cu->cu == NULL)
15012 load_cu (per_cu);
15013 cu = per_cu->cu;
15014
15015 die = follow_die_offset (offset, &cu);
15016 if (!die)
15017 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15018 offset.sect_off, per_cu->objfile->name);
15019
15020 attr = dwarf2_attr (die, DW_AT_location, cu);
15021 if (!attr)
15022 {
15023 /* DWARF: "If there is no such attribute, then there is no effect.".
15024 DATA is ignored if SIZE is 0. */
15025
15026 retval.data = NULL;
15027 retval.size = 0;
15028 }
15029 else if (attr_form_is_section_offset (attr))
15030 {
15031 struct dwarf2_loclist_baton loclist_baton;
15032 CORE_ADDR pc = (*get_frame_pc) (baton);
15033 size_t size;
15034
15035 fill_in_loclist_baton (cu, &loclist_baton, attr);
15036
15037 retval.data = dwarf2_find_location_expression (&loclist_baton,
15038 &size, pc);
15039 retval.size = size;
15040 }
15041 else
15042 {
15043 if (!attr_form_is_block (attr))
15044 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15045 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15046 offset.sect_off, per_cu->objfile->name);
15047
15048 retval.data = DW_BLOCK (attr)->data;
15049 retval.size = DW_BLOCK (attr)->size;
15050 }
15051 retval.per_cu = cu->per_cu;
15052
15053 age_cached_comp_units ();
15054
15055 return retval;
15056 }
15057
15058 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15059 PER_CU. */
15060
15061 struct type *
15062 dwarf2_get_die_type (cu_offset die_offset,
15063 struct dwarf2_per_cu_data *per_cu)
15064 {
15065 sect_offset die_offset_sect;
15066
15067 dw2_setup (per_cu->objfile);
15068
15069 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15070 return get_die_type_at_offset (die_offset_sect, per_cu);
15071 }
15072
15073 /* Follow the signature attribute ATTR in SRC_DIE.
15074 On entry *REF_CU is the CU of SRC_DIE.
15075 On exit *REF_CU is the CU of the result. */
15076
15077 static struct die_info *
15078 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15079 struct dwarf2_cu **ref_cu)
15080 {
15081 struct objfile *objfile = (*ref_cu)->objfile;
15082 struct die_info temp_die;
15083 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15084 struct dwarf2_cu *sig_cu;
15085 struct die_info *die;
15086
15087 /* sig_type will be NULL if the signatured type is missing from
15088 the debug info. */
15089 if (sig_type == NULL)
15090 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15091 "at 0x%x [in module %s]"),
15092 src_die->offset.sect_off, objfile->name);
15093
15094 /* If necessary, add it to the queue and load its DIEs. */
15095
15096 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15097 read_signatured_type (sig_type);
15098
15099 gdb_assert (sig_type->per_cu.cu != NULL);
15100
15101 sig_cu = sig_type->per_cu.cu;
15102 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15103 temp_die.offset = sig_type->type_offset_in_section;
15104 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15105 temp_die.offset.sect_off);
15106 if (die)
15107 {
15108 *ref_cu = sig_cu;
15109 return die;
15110 }
15111
15112 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15113 "from DIE at 0x%x [in module %s]"),
15114 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15115 }
15116
15117 /* Given an offset of a signatured type, return its signatured_type. */
15118
15119 static struct signatured_type *
15120 lookup_signatured_type_at_offset (struct objfile *objfile,
15121 struct dwarf2_section_info *section,
15122 sect_offset offset)
15123 {
15124 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15125 unsigned int length, initial_length_size;
15126 unsigned int sig_offset;
15127 struct signatured_type find_entry, *sig_type;
15128
15129 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15130 sig_offset = (initial_length_size
15131 + 2 /*version*/
15132 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15133 + 1 /*address_size*/);
15134 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15135 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15136
15137 /* This is only used to lookup previously recorded types.
15138 If we didn't find it, it's our bug. */
15139 gdb_assert (sig_type != NULL);
15140 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15141
15142 return sig_type;
15143 }
15144
15145 /* Load the DIEs associated with type unit PER_CU into memory. */
15146
15147 static void
15148 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15149 {
15150 struct objfile *objfile = per_cu->objfile;
15151 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15152 sect_offset offset = per_cu->offset;
15153 struct signatured_type *sig_type;
15154
15155 dwarf2_read_section (objfile, sect);
15156
15157 /* We have the section offset, but we need the signature to do the
15158 hash table lookup. */
15159 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15160 the signature to assert we found the right one.
15161 Ok, but it's a lot of work. We should simplify things so any needed
15162 assert doesn't require all this clumsiness. */
15163 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15164
15165 gdb_assert (&sig_type->per_cu == per_cu);
15166 gdb_assert (sig_type->per_cu.cu == NULL);
15167
15168 read_signatured_type (sig_type);
15169
15170 gdb_assert (sig_type->per_cu.cu != NULL);
15171 }
15172
15173 /* die_reader_func for read_signatured_type.
15174 This is identical to load_full_comp_unit_reader,
15175 but is kept separate for now. */
15176
15177 static void
15178 read_signatured_type_reader (const struct die_reader_specs *reader,
15179 gdb_byte *info_ptr,
15180 struct die_info *comp_unit_die,
15181 int has_children,
15182 void *data)
15183 {
15184 struct dwarf2_cu *cu = reader->cu;
15185 struct attribute *attr;
15186
15187 gdb_assert (cu->die_hash == NULL);
15188 cu->die_hash =
15189 htab_create_alloc_ex (cu->header.length / 12,
15190 die_hash,
15191 die_eq,
15192 NULL,
15193 &cu->comp_unit_obstack,
15194 hashtab_obstack_allocate,
15195 dummy_obstack_deallocate);
15196
15197 if (has_children)
15198 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15199 &info_ptr, comp_unit_die);
15200 cu->dies = comp_unit_die;
15201 /* comp_unit_die is not stored in die_hash, no need. */
15202
15203 /* We try not to read any attributes in this function, because not
15204 all CUs needed for references have been loaded yet, and symbol
15205 table processing isn't initialized. But we have to set the CU language,
15206 or we won't be able to build types correctly.
15207 Similarly, if we do not read the producer, we can not apply
15208 producer-specific interpretation. */
15209 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15210 }
15211
15212 /* Read in a signatured type and build its CU and DIEs.
15213 If the type is a stub for the real type in a DWO file,
15214 read in the real type from the DWO file as well. */
15215
15216 static void
15217 read_signatured_type (struct signatured_type *sig_type)
15218 {
15219 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15220
15221 gdb_assert (per_cu->is_debug_types);
15222 gdb_assert (per_cu->cu == NULL);
15223
15224 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15225 }
15226
15227 /* Decode simple location descriptions.
15228 Given a pointer to a dwarf block that defines a location, compute
15229 the location and return the value.
15230
15231 NOTE drow/2003-11-18: This function is called in two situations
15232 now: for the address of static or global variables (partial symbols
15233 only) and for offsets into structures which are expected to be
15234 (more or less) constant. The partial symbol case should go away,
15235 and only the constant case should remain. That will let this
15236 function complain more accurately. A few special modes are allowed
15237 without complaint for global variables (for instance, global
15238 register values and thread-local values).
15239
15240 A location description containing no operations indicates that the
15241 object is optimized out. The return value is 0 for that case.
15242 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15243 callers will only want a very basic result and this can become a
15244 complaint.
15245
15246 Note that stack[0] is unused except as a default error return. */
15247
15248 static CORE_ADDR
15249 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15250 {
15251 struct objfile *objfile = cu->objfile;
15252 int i;
15253 int size = blk->size;
15254 gdb_byte *data = blk->data;
15255 CORE_ADDR stack[64];
15256 int stacki;
15257 unsigned int bytes_read, unsnd;
15258 gdb_byte op;
15259
15260 i = 0;
15261 stacki = 0;
15262 stack[stacki] = 0;
15263 stack[++stacki] = 0;
15264
15265 while (i < size)
15266 {
15267 op = data[i++];
15268 switch (op)
15269 {
15270 case DW_OP_lit0:
15271 case DW_OP_lit1:
15272 case DW_OP_lit2:
15273 case DW_OP_lit3:
15274 case DW_OP_lit4:
15275 case DW_OP_lit5:
15276 case DW_OP_lit6:
15277 case DW_OP_lit7:
15278 case DW_OP_lit8:
15279 case DW_OP_lit9:
15280 case DW_OP_lit10:
15281 case DW_OP_lit11:
15282 case DW_OP_lit12:
15283 case DW_OP_lit13:
15284 case DW_OP_lit14:
15285 case DW_OP_lit15:
15286 case DW_OP_lit16:
15287 case DW_OP_lit17:
15288 case DW_OP_lit18:
15289 case DW_OP_lit19:
15290 case DW_OP_lit20:
15291 case DW_OP_lit21:
15292 case DW_OP_lit22:
15293 case DW_OP_lit23:
15294 case DW_OP_lit24:
15295 case DW_OP_lit25:
15296 case DW_OP_lit26:
15297 case DW_OP_lit27:
15298 case DW_OP_lit28:
15299 case DW_OP_lit29:
15300 case DW_OP_lit30:
15301 case DW_OP_lit31:
15302 stack[++stacki] = op - DW_OP_lit0;
15303 break;
15304
15305 case DW_OP_reg0:
15306 case DW_OP_reg1:
15307 case DW_OP_reg2:
15308 case DW_OP_reg3:
15309 case DW_OP_reg4:
15310 case DW_OP_reg5:
15311 case DW_OP_reg6:
15312 case DW_OP_reg7:
15313 case DW_OP_reg8:
15314 case DW_OP_reg9:
15315 case DW_OP_reg10:
15316 case DW_OP_reg11:
15317 case DW_OP_reg12:
15318 case DW_OP_reg13:
15319 case DW_OP_reg14:
15320 case DW_OP_reg15:
15321 case DW_OP_reg16:
15322 case DW_OP_reg17:
15323 case DW_OP_reg18:
15324 case DW_OP_reg19:
15325 case DW_OP_reg20:
15326 case DW_OP_reg21:
15327 case DW_OP_reg22:
15328 case DW_OP_reg23:
15329 case DW_OP_reg24:
15330 case DW_OP_reg25:
15331 case DW_OP_reg26:
15332 case DW_OP_reg27:
15333 case DW_OP_reg28:
15334 case DW_OP_reg29:
15335 case DW_OP_reg30:
15336 case DW_OP_reg31:
15337 stack[++stacki] = op - DW_OP_reg0;
15338 if (i < size)
15339 dwarf2_complex_location_expr_complaint ();
15340 break;
15341
15342 case DW_OP_regx:
15343 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15344 i += bytes_read;
15345 stack[++stacki] = unsnd;
15346 if (i < size)
15347 dwarf2_complex_location_expr_complaint ();
15348 break;
15349
15350 case DW_OP_addr:
15351 stack[++stacki] = read_address (objfile->obfd, &data[i],
15352 cu, &bytes_read);
15353 i += bytes_read;
15354 break;
15355
15356 case DW_OP_const1u:
15357 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15358 i += 1;
15359 break;
15360
15361 case DW_OP_const1s:
15362 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15363 i += 1;
15364 break;
15365
15366 case DW_OP_const2u:
15367 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15368 i += 2;
15369 break;
15370
15371 case DW_OP_const2s:
15372 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15373 i += 2;
15374 break;
15375
15376 case DW_OP_const4u:
15377 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15378 i += 4;
15379 break;
15380
15381 case DW_OP_const4s:
15382 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15383 i += 4;
15384 break;
15385
15386 case DW_OP_const8u:
15387 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15388 i += 8;
15389 break;
15390
15391 case DW_OP_constu:
15392 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15393 &bytes_read);
15394 i += bytes_read;
15395 break;
15396
15397 case DW_OP_consts:
15398 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15399 i += bytes_read;
15400 break;
15401
15402 case DW_OP_dup:
15403 stack[stacki + 1] = stack[stacki];
15404 stacki++;
15405 break;
15406
15407 case DW_OP_plus:
15408 stack[stacki - 1] += stack[stacki];
15409 stacki--;
15410 break;
15411
15412 case DW_OP_plus_uconst:
15413 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15414 &bytes_read);
15415 i += bytes_read;
15416 break;
15417
15418 case DW_OP_minus:
15419 stack[stacki - 1] -= stack[stacki];
15420 stacki--;
15421 break;
15422
15423 case DW_OP_deref:
15424 /* If we're not the last op, then we definitely can't encode
15425 this using GDB's address_class enum. This is valid for partial
15426 global symbols, although the variable's address will be bogus
15427 in the psymtab. */
15428 if (i < size)
15429 dwarf2_complex_location_expr_complaint ();
15430 break;
15431
15432 case DW_OP_GNU_push_tls_address:
15433 /* The top of the stack has the offset from the beginning
15434 of the thread control block at which the variable is located. */
15435 /* Nothing should follow this operator, so the top of stack would
15436 be returned. */
15437 /* This is valid for partial global symbols, but the variable's
15438 address will be bogus in the psymtab. Make it always at least
15439 non-zero to not look as a variable garbage collected by linker
15440 which have DW_OP_addr 0. */
15441 if (i < size)
15442 dwarf2_complex_location_expr_complaint ();
15443 stack[stacki]++;
15444 break;
15445
15446 case DW_OP_GNU_uninit:
15447 break;
15448
15449 case DW_OP_GNU_addr_index:
15450 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15451 &bytes_read);
15452 i += bytes_read;
15453 break;
15454
15455 default:
15456 {
15457 const char *name = get_DW_OP_name (op);
15458
15459 if (name)
15460 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15461 name);
15462 else
15463 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15464 op);
15465 }
15466
15467 return (stack[stacki]);
15468 }
15469
15470 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15471 outside of the allocated space. Also enforce minimum>0. */
15472 if (stacki >= ARRAY_SIZE (stack) - 1)
15473 {
15474 complaint (&symfile_complaints,
15475 _("location description stack overflow"));
15476 return 0;
15477 }
15478
15479 if (stacki <= 0)
15480 {
15481 complaint (&symfile_complaints,
15482 _("location description stack underflow"));
15483 return 0;
15484 }
15485 }
15486 return (stack[stacki]);
15487 }
15488
15489 /* memory allocation interface */
15490
15491 static struct dwarf_block *
15492 dwarf_alloc_block (struct dwarf2_cu *cu)
15493 {
15494 struct dwarf_block *blk;
15495
15496 blk = (struct dwarf_block *)
15497 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15498 return (blk);
15499 }
15500
15501 static struct abbrev_info *
15502 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15503 {
15504 struct abbrev_info *abbrev;
15505
15506 abbrev = (struct abbrev_info *)
15507 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15508 memset (abbrev, 0, sizeof (struct abbrev_info));
15509 return (abbrev);
15510 }
15511
15512 static struct die_info *
15513 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15514 {
15515 struct die_info *die;
15516 size_t size = sizeof (struct die_info);
15517
15518 if (num_attrs > 1)
15519 size += (num_attrs - 1) * sizeof (struct attribute);
15520
15521 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15522 memset (die, 0, sizeof (struct die_info));
15523 return (die);
15524 }
15525
15526 \f
15527 /* Macro support. */
15528
15529 /* Return the full name of file number I in *LH's file name table.
15530 Use COMP_DIR as the name of the current directory of the
15531 compilation. The result is allocated using xmalloc; the caller is
15532 responsible for freeing it. */
15533 static char *
15534 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15535 {
15536 /* Is the file number a valid index into the line header's file name
15537 table? Remember that file numbers start with one, not zero. */
15538 if (1 <= file && file <= lh->num_file_names)
15539 {
15540 struct file_entry *fe = &lh->file_names[file - 1];
15541
15542 if (IS_ABSOLUTE_PATH (fe->name))
15543 return xstrdup (fe->name);
15544 else
15545 {
15546 const char *dir;
15547 int dir_len;
15548 char *full_name;
15549
15550 if (fe->dir_index)
15551 dir = lh->include_dirs[fe->dir_index - 1];
15552 else
15553 dir = comp_dir;
15554
15555 if (dir)
15556 {
15557 dir_len = strlen (dir);
15558 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15559 strcpy (full_name, dir);
15560 full_name[dir_len] = '/';
15561 strcpy (full_name + dir_len + 1, fe->name);
15562 return full_name;
15563 }
15564 else
15565 return xstrdup (fe->name);
15566 }
15567 }
15568 else
15569 {
15570 /* The compiler produced a bogus file number. We can at least
15571 record the macro definitions made in the file, even if we
15572 won't be able to find the file by name. */
15573 char fake_name[80];
15574
15575 sprintf (fake_name, "<bad macro file number %d>", file);
15576
15577 complaint (&symfile_complaints,
15578 _("bad file number in macro information (%d)"),
15579 file);
15580
15581 return xstrdup (fake_name);
15582 }
15583 }
15584
15585
15586 static struct macro_source_file *
15587 macro_start_file (int file, int line,
15588 struct macro_source_file *current_file,
15589 const char *comp_dir,
15590 struct line_header *lh, struct objfile *objfile)
15591 {
15592 /* The full name of this source file. */
15593 char *full_name = file_full_name (file, lh, comp_dir);
15594
15595 /* We don't create a macro table for this compilation unit
15596 at all until we actually get a filename. */
15597 if (! pending_macros)
15598 pending_macros = new_macro_table (&objfile->objfile_obstack,
15599 objfile->macro_cache);
15600
15601 if (! current_file)
15602 {
15603 /* If we have no current file, then this must be the start_file
15604 directive for the compilation unit's main source file. */
15605 current_file = macro_set_main (pending_macros, full_name);
15606 macro_define_special (pending_macros);
15607 }
15608 else
15609 current_file = macro_include (current_file, line, full_name);
15610
15611 xfree (full_name);
15612
15613 return current_file;
15614 }
15615
15616
15617 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15618 followed by a null byte. */
15619 static char *
15620 copy_string (const char *buf, int len)
15621 {
15622 char *s = xmalloc (len + 1);
15623
15624 memcpy (s, buf, len);
15625 s[len] = '\0';
15626 return s;
15627 }
15628
15629
15630 static const char *
15631 consume_improper_spaces (const char *p, const char *body)
15632 {
15633 if (*p == ' ')
15634 {
15635 complaint (&symfile_complaints,
15636 _("macro definition contains spaces "
15637 "in formal argument list:\n`%s'"),
15638 body);
15639
15640 while (*p == ' ')
15641 p++;
15642 }
15643
15644 return p;
15645 }
15646
15647
15648 static void
15649 parse_macro_definition (struct macro_source_file *file, int line,
15650 const char *body)
15651 {
15652 const char *p;
15653
15654 /* The body string takes one of two forms. For object-like macro
15655 definitions, it should be:
15656
15657 <macro name> " " <definition>
15658
15659 For function-like macro definitions, it should be:
15660
15661 <macro name> "() " <definition>
15662 or
15663 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15664
15665 Spaces may appear only where explicitly indicated, and in the
15666 <definition>.
15667
15668 The Dwarf 2 spec says that an object-like macro's name is always
15669 followed by a space, but versions of GCC around March 2002 omit
15670 the space when the macro's definition is the empty string.
15671
15672 The Dwarf 2 spec says that there should be no spaces between the
15673 formal arguments in a function-like macro's formal argument list,
15674 but versions of GCC around March 2002 include spaces after the
15675 commas. */
15676
15677
15678 /* Find the extent of the macro name. The macro name is terminated
15679 by either a space or null character (for an object-like macro) or
15680 an opening paren (for a function-like macro). */
15681 for (p = body; *p; p++)
15682 if (*p == ' ' || *p == '(')
15683 break;
15684
15685 if (*p == ' ' || *p == '\0')
15686 {
15687 /* It's an object-like macro. */
15688 int name_len = p - body;
15689 char *name = copy_string (body, name_len);
15690 const char *replacement;
15691
15692 if (*p == ' ')
15693 replacement = body + name_len + 1;
15694 else
15695 {
15696 dwarf2_macro_malformed_definition_complaint (body);
15697 replacement = body + name_len;
15698 }
15699
15700 macro_define_object (file, line, name, replacement);
15701
15702 xfree (name);
15703 }
15704 else if (*p == '(')
15705 {
15706 /* It's a function-like macro. */
15707 char *name = copy_string (body, p - body);
15708 int argc = 0;
15709 int argv_size = 1;
15710 char **argv = xmalloc (argv_size * sizeof (*argv));
15711
15712 p++;
15713
15714 p = consume_improper_spaces (p, body);
15715
15716 /* Parse the formal argument list. */
15717 while (*p && *p != ')')
15718 {
15719 /* Find the extent of the current argument name. */
15720 const char *arg_start = p;
15721
15722 while (*p && *p != ',' && *p != ')' && *p != ' ')
15723 p++;
15724
15725 if (! *p || p == arg_start)
15726 dwarf2_macro_malformed_definition_complaint (body);
15727 else
15728 {
15729 /* Make sure argv has room for the new argument. */
15730 if (argc >= argv_size)
15731 {
15732 argv_size *= 2;
15733 argv = xrealloc (argv, argv_size * sizeof (*argv));
15734 }
15735
15736 argv[argc++] = copy_string (arg_start, p - arg_start);
15737 }
15738
15739 p = consume_improper_spaces (p, body);
15740
15741 /* Consume the comma, if present. */
15742 if (*p == ',')
15743 {
15744 p++;
15745
15746 p = consume_improper_spaces (p, body);
15747 }
15748 }
15749
15750 if (*p == ')')
15751 {
15752 p++;
15753
15754 if (*p == ' ')
15755 /* Perfectly formed definition, no complaints. */
15756 macro_define_function (file, line, name,
15757 argc, (const char **) argv,
15758 p + 1);
15759 else if (*p == '\0')
15760 {
15761 /* Complain, but do define it. */
15762 dwarf2_macro_malformed_definition_complaint (body);
15763 macro_define_function (file, line, name,
15764 argc, (const char **) argv,
15765 p);
15766 }
15767 else
15768 /* Just complain. */
15769 dwarf2_macro_malformed_definition_complaint (body);
15770 }
15771 else
15772 /* Just complain. */
15773 dwarf2_macro_malformed_definition_complaint (body);
15774
15775 xfree (name);
15776 {
15777 int i;
15778
15779 for (i = 0; i < argc; i++)
15780 xfree (argv[i]);
15781 }
15782 xfree (argv);
15783 }
15784 else
15785 dwarf2_macro_malformed_definition_complaint (body);
15786 }
15787
15788 /* Skip some bytes from BYTES according to the form given in FORM.
15789 Returns the new pointer. */
15790
15791 static gdb_byte *
15792 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15793 enum dwarf_form form,
15794 unsigned int offset_size,
15795 struct dwarf2_section_info *section)
15796 {
15797 unsigned int bytes_read;
15798
15799 switch (form)
15800 {
15801 case DW_FORM_data1:
15802 case DW_FORM_flag:
15803 ++bytes;
15804 break;
15805
15806 case DW_FORM_data2:
15807 bytes += 2;
15808 break;
15809
15810 case DW_FORM_data4:
15811 bytes += 4;
15812 break;
15813
15814 case DW_FORM_data8:
15815 bytes += 8;
15816 break;
15817
15818 case DW_FORM_string:
15819 read_direct_string (abfd, bytes, &bytes_read);
15820 bytes += bytes_read;
15821 break;
15822
15823 case DW_FORM_sec_offset:
15824 case DW_FORM_strp:
15825 bytes += offset_size;
15826 break;
15827
15828 case DW_FORM_block:
15829 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15830 bytes += bytes_read;
15831 break;
15832
15833 case DW_FORM_block1:
15834 bytes += 1 + read_1_byte (abfd, bytes);
15835 break;
15836 case DW_FORM_block2:
15837 bytes += 2 + read_2_bytes (abfd, bytes);
15838 break;
15839 case DW_FORM_block4:
15840 bytes += 4 + read_4_bytes (abfd, bytes);
15841 break;
15842
15843 case DW_FORM_sdata:
15844 case DW_FORM_udata:
15845 case DW_FORM_GNU_addr_index:
15846 case DW_FORM_GNU_str_index:
15847 bytes = skip_leb128 (abfd, bytes);
15848 break;
15849
15850 default:
15851 {
15852 complain:
15853 complaint (&symfile_complaints,
15854 _("invalid form 0x%x in `%s'"),
15855 form,
15856 section->asection->name);
15857 return NULL;
15858 }
15859 }
15860
15861 return bytes;
15862 }
15863
15864 /* A helper for dwarf_decode_macros that handles skipping an unknown
15865 opcode. Returns an updated pointer to the macro data buffer; or,
15866 on error, issues a complaint and returns NULL. */
15867
15868 static gdb_byte *
15869 skip_unknown_opcode (unsigned int opcode,
15870 gdb_byte **opcode_definitions,
15871 gdb_byte *mac_ptr,
15872 bfd *abfd,
15873 unsigned int offset_size,
15874 struct dwarf2_section_info *section)
15875 {
15876 unsigned int bytes_read, i;
15877 unsigned long arg;
15878 gdb_byte *defn;
15879
15880 if (opcode_definitions[opcode] == NULL)
15881 {
15882 complaint (&symfile_complaints,
15883 _("unrecognized DW_MACFINO opcode 0x%x"),
15884 opcode);
15885 return NULL;
15886 }
15887
15888 defn = opcode_definitions[opcode];
15889 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15890 defn += bytes_read;
15891
15892 for (i = 0; i < arg; ++i)
15893 {
15894 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15895 if (mac_ptr == NULL)
15896 {
15897 /* skip_form_bytes already issued the complaint. */
15898 return NULL;
15899 }
15900 }
15901
15902 return mac_ptr;
15903 }
15904
15905 /* A helper function which parses the header of a macro section.
15906 If the macro section is the extended (for now called "GNU") type,
15907 then this updates *OFFSET_SIZE. Returns a pointer to just after
15908 the header, or issues a complaint and returns NULL on error. */
15909
15910 static gdb_byte *
15911 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15912 bfd *abfd,
15913 gdb_byte *mac_ptr,
15914 unsigned int *offset_size,
15915 int section_is_gnu)
15916 {
15917 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15918
15919 if (section_is_gnu)
15920 {
15921 unsigned int version, flags;
15922
15923 version = read_2_bytes (abfd, mac_ptr);
15924 if (version != 4)
15925 {
15926 complaint (&symfile_complaints,
15927 _("unrecognized version `%d' in .debug_macro section"),
15928 version);
15929 return NULL;
15930 }
15931 mac_ptr += 2;
15932
15933 flags = read_1_byte (abfd, mac_ptr);
15934 ++mac_ptr;
15935 *offset_size = (flags & 1) ? 8 : 4;
15936
15937 if ((flags & 2) != 0)
15938 /* We don't need the line table offset. */
15939 mac_ptr += *offset_size;
15940
15941 /* Vendor opcode descriptions. */
15942 if ((flags & 4) != 0)
15943 {
15944 unsigned int i, count;
15945
15946 count = read_1_byte (abfd, mac_ptr);
15947 ++mac_ptr;
15948 for (i = 0; i < count; ++i)
15949 {
15950 unsigned int opcode, bytes_read;
15951 unsigned long arg;
15952
15953 opcode = read_1_byte (abfd, mac_ptr);
15954 ++mac_ptr;
15955 opcode_definitions[opcode] = mac_ptr;
15956 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15957 mac_ptr += bytes_read;
15958 mac_ptr += arg;
15959 }
15960 }
15961 }
15962
15963 return mac_ptr;
15964 }
15965
15966 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15967 including DW_MACRO_GNU_transparent_include. */
15968
15969 static void
15970 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15971 struct macro_source_file *current_file,
15972 struct line_header *lh, char *comp_dir,
15973 struct dwarf2_section_info *section,
15974 int section_is_gnu,
15975 unsigned int offset_size,
15976 struct objfile *objfile,
15977 htab_t include_hash)
15978 {
15979 enum dwarf_macro_record_type macinfo_type;
15980 int at_commandline;
15981 gdb_byte *opcode_definitions[256];
15982
15983 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15984 &offset_size, section_is_gnu);
15985 if (mac_ptr == NULL)
15986 {
15987 /* We already issued a complaint. */
15988 return;
15989 }
15990
15991 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15992 GDB is still reading the definitions from command line. First
15993 DW_MACINFO_start_file will need to be ignored as it was already executed
15994 to create CURRENT_FILE for the main source holding also the command line
15995 definitions. On first met DW_MACINFO_start_file this flag is reset to
15996 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15997
15998 at_commandline = 1;
15999
16000 do
16001 {
16002 /* Do we at least have room for a macinfo type byte? */
16003 if (mac_ptr >= mac_end)
16004 {
16005 dwarf2_macros_too_long_complaint (section);
16006 break;
16007 }
16008
16009 macinfo_type = read_1_byte (abfd, mac_ptr);
16010 mac_ptr++;
16011
16012 /* Note that we rely on the fact that the corresponding GNU and
16013 DWARF constants are the same. */
16014 switch (macinfo_type)
16015 {
16016 /* A zero macinfo type indicates the end of the macro
16017 information. */
16018 case 0:
16019 break;
16020
16021 case DW_MACRO_GNU_define:
16022 case DW_MACRO_GNU_undef:
16023 case DW_MACRO_GNU_define_indirect:
16024 case DW_MACRO_GNU_undef_indirect:
16025 {
16026 unsigned int bytes_read;
16027 int line;
16028 char *body;
16029 int is_define;
16030
16031 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16032 mac_ptr += bytes_read;
16033
16034 if (macinfo_type == DW_MACRO_GNU_define
16035 || macinfo_type == DW_MACRO_GNU_undef)
16036 {
16037 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16038 mac_ptr += bytes_read;
16039 }
16040 else
16041 {
16042 LONGEST str_offset;
16043
16044 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16045 mac_ptr += offset_size;
16046
16047 body = read_indirect_string_at_offset (abfd, str_offset);
16048 }
16049
16050 is_define = (macinfo_type == DW_MACRO_GNU_define
16051 || macinfo_type == DW_MACRO_GNU_define_indirect);
16052 if (! current_file)
16053 {
16054 /* DWARF violation as no main source is present. */
16055 complaint (&symfile_complaints,
16056 _("debug info with no main source gives macro %s "
16057 "on line %d: %s"),
16058 is_define ? _("definition") : _("undefinition"),
16059 line, body);
16060 break;
16061 }
16062 if ((line == 0 && !at_commandline)
16063 || (line != 0 && at_commandline))
16064 complaint (&symfile_complaints,
16065 _("debug info gives %s macro %s with %s line %d: %s"),
16066 at_commandline ? _("command-line") : _("in-file"),
16067 is_define ? _("definition") : _("undefinition"),
16068 line == 0 ? _("zero") : _("non-zero"), line, body);
16069
16070 if (is_define)
16071 parse_macro_definition (current_file, line, body);
16072 else
16073 {
16074 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16075 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16076 macro_undef (current_file, line, body);
16077 }
16078 }
16079 break;
16080
16081 case DW_MACRO_GNU_start_file:
16082 {
16083 unsigned int bytes_read;
16084 int line, file;
16085
16086 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16087 mac_ptr += bytes_read;
16088 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16089 mac_ptr += bytes_read;
16090
16091 if ((line == 0 && !at_commandline)
16092 || (line != 0 && at_commandline))
16093 complaint (&symfile_complaints,
16094 _("debug info gives source %d included "
16095 "from %s at %s line %d"),
16096 file, at_commandline ? _("command-line") : _("file"),
16097 line == 0 ? _("zero") : _("non-zero"), line);
16098
16099 if (at_commandline)
16100 {
16101 /* This DW_MACRO_GNU_start_file was executed in the
16102 pass one. */
16103 at_commandline = 0;
16104 }
16105 else
16106 current_file = macro_start_file (file, line,
16107 current_file, comp_dir,
16108 lh, objfile);
16109 }
16110 break;
16111
16112 case DW_MACRO_GNU_end_file:
16113 if (! current_file)
16114 complaint (&symfile_complaints,
16115 _("macro debug info has an unmatched "
16116 "`close_file' directive"));
16117 else
16118 {
16119 current_file = current_file->included_by;
16120 if (! current_file)
16121 {
16122 enum dwarf_macro_record_type next_type;
16123
16124 /* GCC circa March 2002 doesn't produce the zero
16125 type byte marking the end of the compilation
16126 unit. Complain if it's not there, but exit no
16127 matter what. */
16128
16129 /* Do we at least have room for a macinfo type byte? */
16130 if (mac_ptr >= mac_end)
16131 {
16132 dwarf2_macros_too_long_complaint (section);
16133 return;
16134 }
16135
16136 /* We don't increment mac_ptr here, so this is just
16137 a look-ahead. */
16138 next_type = read_1_byte (abfd, mac_ptr);
16139 if (next_type != 0)
16140 complaint (&symfile_complaints,
16141 _("no terminating 0-type entry for "
16142 "macros in `.debug_macinfo' section"));
16143
16144 return;
16145 }
16146 }
16147 break;
16148
16149 case DW_MACRO_GNU_transparent_include:
16150 {
16151 LONGEST offset;
16152 void **slot;
16153
16154 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16155 mac_ptr += offset_size;
16156
16157 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16158 if (*slot != NULL)
16159 {
16160 /* This has actually happened; see
16161 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16162 complaint (&symfile_complaints,
16163 _("recursive DW_MACRO_GNU_transparent_include in "
16164 ".debug_macro section"));
16165 }
16166 else
16167 {
16168 *slot = mac_ptr;
16169
16170 dwarf_decode_macro_bytes (abfd,
16171 section->buffer + offset,
16172 mac_end, current_file,
16173 lh, comp_dir,
16174 section, section_is_gnu,
16175 offset_size, objfile, include_hash);
16176
16177 htab_remove_elt (include_hash, mac_ptr);
16178 }
16179 }
16180 break;
16181
16182 case DW_MACINFO_vendor_ext:
16183 if (!section_is_gnu)
16184 {
16185 unsigned int bytes_read;
16186 int constant;
16187
16188 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16189 mac_ptr += bytes_read;
16190 read_direct_string (abfd, mac_ptr, &bytes_read);
16191 mac_ptr += bytes_read;
16192
16193 /* We don't recognize any vendor extensions. */
16194 break;
16195 }
16196 /* FALLTHROUGH */
16197
16198 default:
16199 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16200 mac_ptr, abfd, offset_size,
16201 section);
16202 if (mac_ptr == NULL)
16203 return;
16204 break;
16205 }
16206 } while (macinfo_type != 0);
16207 }
16208
16209 static void
16210 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16211 char *comp_dir, bfd *abfd,
16212 struct dwarf2_cu *cu,
16213 struct dwarf2_section_info *section,
16214 int section_is_gnu, const char *section_name)
16215 {
16216 struct objfile *objfile = dwarf2_per_objfile->objfile;
16217 gdb_byte *mac_ptr, *mac_end;
16218 struct macro_source_file *current_file = 0;
16219 enum dwarf_macro_record_type macinfo_type;
16220 unsigned int offset_size = cu->header.offset_size;
16221 gdb_byte *opcode_definitions[256];
16222 struct cleanup *cleanup;
16223 htab_t include_hash;
16224 void **slot;
16225
16226 dwarf2_read_section (objfile, section);
16227 if (section->buffer == NULL)
16228 {
16229 complaint (&symfile_complaints, _("missing %s section"), section_name);
16230 return;
16231 }
16232
16233 /* First pass: Find the name of the base filename.
16234 This filename is needed in order to process all macros whose definition
16235 (or undefinition) comes from the command line. These macros are defined
16236 before the first DW_MACINFO_start_file entry, and yet still need to be
16237 associated to the base file.
16238
16239 To determine the base file name, we scan the macro definitions until we
16240 reach the first DW_MACINFO_start_file entry. We then initialize
16241 CURRENT_FILE accordingly so that any macro definition found before the
16242 first DW_MACINFO_start_file can still be associated to the base file. */
16243
16244 mac_ptr = section->buffer + offset;
16245 mac_end = section->buffer + section->size;
16246
16247 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16248 &offset_size, section_is_gnu);
16249 if (mac_ptr == NULL)
16250 {
16251 /* We already issued a complaint. */
16252 return;
16253 }
16254
16255 do
16256 {
16257 /* Do we at least have room for a macinfo type byte? */
16258 if (mac_ptr >= mac_end)
16259 {
16260 /* Complaint is printed during the second pass as GDB will probably
16261 stop the first pass earlier upon finding
16262 DW_MACINFO_start_file. */
16263 break;
16264 }
16265
16266 macinfo_type = read_1_byte (abfd, mac_ptr);
16267 mac_ptr++;
16268
16269 /* Note that we rely on the fact that the corresponding GNU and
16270 DWARF constants are the same. */
16271 switch (macinfo_type)
16272 {
16273 /* A zero macinfo type indicates the end of the macro
16274 information. */
16275 case 0:
16276 break;
16277
16278 case DW_MACRO_GNU_define:
16279 case DW_MACRO_GNU_undef:
16280 /* Only skip the data by MAC_PTR. */
16281 {
16282 unsigned int bytes_read;
16283
16284 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16285 mac_ptr += bytes_read;
16286 read_direct_string (abfd, mac_ptr, &bytes_read);
16287 mac_ptr += bytes_read;
16288 }
16289 break;
16290
16291 case DW_MACRO_GNU_start_file:
16292 {
16293 unsigned int bytes_read;
16294 int line, file;
16295
16296 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16297 mac_ptr += bytes_read;
16298 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16299 mac_ptr += bytes_read;
16300
16301 current_file = macro_start_file (file, line, current_file,
16302 comp_dir, lh, objfile);
16303 }
16304 break;
16305
16306 case DW_MACRO_GNU_end_file:
16307 /* No data to skip by MAC_PTR. */
16308 break;
16309
16310 case DW_MACRO_GNU_define_indirect:
16311 case DW_MACRO_GNU_undef_indirect:
16312 {
16313 unsigned int bytes_read;
16314
16315 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16316 mac_ptr += bytes_read;
16317 mac_ptr += offset_size;
16318 }
16319 break;
16320
16321 case DW_MACRO_GNU_transparent_include:
16322 /* Note that, according to the spec, a transparent include
16323 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16324 skip this opcode. */
16325 mac_ptr += offset_size;
16326 break;
16327
16328 case DW_MACINFO_vendor_ext:
16329 /* Only skip the data by MAC_PTR. */
16330 if (!section_is_gnu)
16331 {
16332 unsigned int bytes_read;
16333
16334 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16335 mac_ptr += bytes_read;
16336 read_direct_string (abfd, mac_ptr, &bytes_read);
16337 mac_ptr += bytes_read;
16338 }
16339 /* FALLTHROUGH */
16340
16341 default:
16342 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16343 mac_ptr, abfd, offset_size,
16344 section);
16345 if (mac_ptr == NULL)
16346 return;
16347 break;
16348 }
16349 } while (macinfo_type != 0 && current_file == NULL);
16350
16351 /* Second pass: Process all entries.
16352
16353 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16354 command-line macro definitions/undefinitions. This flag is unset when we
16355 reach the first DW_MACINFO_start_file entry. */
16356
16357 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16358 NULL, xcalloc, xfree);
16359 cleanup = make_cleanup_htab_delete (include_hash);
16360 mac_ptr = section->buffer + offset;
16361 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16362 *slot = mac_ptr;
16363 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16364 current_file, lh, comp_dir, section, section_is_gnu,
16365 offset_size, objfile, include_hash);
16366 do_cleanups (cleanup);
16367 }
16368
16369 /* Check if the attribute's form is a DW_FORM_block*
16370 if so return true else false. */
16371
16372 static int
16373 attr_form_is_block (struct attribute *attr)
16374 {
16375 return (attr == NULL ? 0 :
16376 attr->form == DW_FORM_block1
16377 || attr->form == DW_FORM_block2
16378 || attr->form == DW_FORM_block4
16379 || attr->form == DW_FORM_block
16380 || attr->form == DW_FORM_exprloc);
16381 }
16382
16383 /* Return non-zero if ATTR's value is a section offset --- classes
16384 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16385 You may use DW_UNSND (attr) to retrieve such offsets.
16386
16387 Section 7.5.4, "Attribute Encodings", explains that no attribute
16388 may have a value that belongs to more than one of these classes; it
16389 would be ambiguous if we did, because we use the same forms for all
16390 of them. */
16391
16392 static int
16393 attr_form_is_section_offset (struct attribute *attr)
16394 {
16395 return (attr->form == DW_FORM_data4
16396 || attr->form == DW_FORM_data8
16397 || attr->form == DW_FORM_sec_offset);
16398 }
16399
16400 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16401 zero otherwise. When this function returns true, you can apply
16402 dwarf2_get_attr_constant_value to it.
16403
16404 However, note that for some attributes you must check
16405 attr_form_is_section_offset before using this test. DW_FORM_data4
16406 and DW_FORM_data8 are members of both the constant class, and of
16407 the classes that contain offsets into other debug sections
16408 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16409 that, if an attribute's can be either a constant or one of the
16410 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16411 taken as section offsets, not constants. */
16412
16413 static int
16414 attr_form_is_constant (struct attribute *attr)
16415 {
16416 switch (attr->form)
16417 {
16418 case DW_FORM_sdata:
16419 case DW_FORM_udata:
16420 case DW_FORM_data1:
16421 case DW_FORM_data2:
16422 case DW_FORM_data4:
16423 case DW_FORM_data8:
16424 return 1;
16425 default:
16426 return 0;
16427 }
16428 }
16429
16430 /* Return the .debug_loc section to use for CU.
16431 For DWO files use .debug_loc.dwo. */
16432
16433 static struct dwarf2_section_info *
16434 cu_debug_loc_section (struct dwarf2_cu *cu)
16435 {
16436 if (cu->dwo_unit)
16437 return &cu->dwo_unit->dwo_file->sections.loc;
16438 return &dwarf2_per_objfile->loc;
16439 }
16440
16441 /* A helper function that fills in a dwarf2_loclist_baton. */
16442
16443 static void
16444 fill_in_loclist_baton (struct dwarf2_cu *cu,
16445 struct dwarf2_loclist_baton *baton,
16446 struct attribute *attr)
16447 {
16448 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16449
16450 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16451
16452 baton->per_cu = cu->per_cu;
16453 gdb_assert (baton->per_cu);
16454 /* We don't know how long the location list is, but make sure we
16455 don't run off the edge of the section. */
16456 baton->size = section->size - DW_UNSND (attr);
16457 baton->data = section->buffer + DW_UNSND (attr);
16458 baton->base_address = cu->base_address;
16459 }
16460
16461 static void
16462 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16463 struct dwarf2_cu *cu)
16464 {
16465 struct objfile *objfile = dwarf2_per_objfile->objfile;
16466 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16467
16468 if (attr_form_is_section_offset (attr)
16469 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16470 the section. If so, fall through to the complaint in the
16471 other branch. */
16472 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16473 {
16474 struct dwarf2_loclist_baton *baton;
16475
16476 baton = obstack_alloc (&objfile->objfile_obstack,
16477 sizeof (struct dwarf2_loclist_baton));
16478
16479 fill_in_loclist_baton (cu, baton, attr);
16480
16481 if (cu->base_known == 0)
16482 complaint (&symfile_complaints,
16483 _("Location list used without "
16484 "specifying the CU base address."));
16485
16486 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16487 SYMBOL_LOCATION_BATON (sym) = baton;
16488 }
16489 else
16490 {
16491 struct dwarf2_locexpr_baton *baton;
16492
16493 baton = obstack_alloc (&objfile->objfile_obstack,
16494 sizeof (struct dwarf2_locexpr_baton));
16495 baton->per_cu = cu->per_cu;
16496 gdb_assert (baton->per_cu);
16497
16498 if (attr_form_is_block (attr))
16499 {
16500 /* Note that we're just copying the block's data pointer
16501 here, not the actual data. We're still pointing into the
16502 info_buffer for SYM's objfile; right now we never release
16503 that buffer, but when we do clean up properly this may
16504 need to change. */
16505 baton->size = DW_BLOCK (attr)->size;
16506 baton->data = DW_BLOCK (attr)->data;
16507 }
16508 else
16509 {
16510 dwarf2_invalid_attrib_class_complaint ("location description",
16511 SYMBOL_NATURAL_NAME (sym));
16512 baton->size = 0;
16513 }
16514
16515 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16516 SYMBOL_LOCATION_BATON (sym) = baton;
16517 }
16518 }
16519
16520 /* Return the OBJFILE associated with the compilation unit CU. If CU
16521 came from a separate debuginfo file, then the master objfile is
16522 returned. */
16523
16524 struct objfile *
16525 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16526 {
16527 struct objfile *objfile = per_cu->objfile;
16528
16529 /* Return the master objfile, so that we can report and look up the
16530 correct file containing this variable. */
16531 if (objfile->separate_debug_objfile_backlink)
16532 objfile = objfile->separate_debug_objfile_backlink;
16533
16534 return objfile;
16535 }
16536
16537 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16538 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16539 CU_HEADERP first. */
16540
16541 static const struct comp_unit_head *
16542 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16543 struct dwarf2_per_cu_data *per_cu)
16544 {
16545 struct objfile *objfile;
16546 struct dwarf2_per_objfile *per_objfile;
16547 gdb_byte *info_ptr;
16548
16549 if (per_cu->cu)
16550 return &per_cu->cu->header;
16551
16552 objfile = per_cu->objfile;
16553 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16554 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16555
16556 memset (cu_headerp, 0, sizeof (*cu_headerp));
16557 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16558
16559 return cu_headerp;
16560 }
16561
16562 /* Return the address size given in the compilation unit header for CU. */
16563
16564 int
16565 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16566 {
16567 struct comp_unit_head cu_header_local;
16568 const struct comp_unit_head *cu_headerp;
16569
16570 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16571
16572 return cu_headerp->addr_size;
16573 }
16574
16575 /* Return the offset size given in the compilation unit header for CU. */
16576
16577 int
16578 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16579 {
16580 struct comp_unit_head cu_header_local;
16581 const struct comp_unit_head *cu_headerp;
16582
16583 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16584
16585 return cu_headerp->offset_size;
16586 }
16587
16588 /* See its dwarf2loc.h declaration. */
16589
16590 int
16591 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16592 {
16593 struct comp_unit_head cu_header_local;
16594 const struct comp_unit_head *cu_headerp;
16595
16596 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16597
16598 if (cu_headerp->version == 2)
16599 return cu_headerp->addr_size;
16600 else
16601 return cu_headerp->offset_size;
16602 }
16603
16604 /* Return the text offset of the CU. The returned offset comes from
16605 this CU's objfile. If this objfile came from a separate debuginfo
16606 file, then the offset may be different from the corresponding
16607 offset in the parent objfile. */
16608
16609 CORE_ADDR
16610 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16611 {
16612 struct objfile *objfile = per_cu->objfile;
16613
16614 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16615 }
16616
16617 /* Locate the .debug_info compilation unit from CU's objfile which contains
16618 the DIE at OFFSET. Raises an error on failure. */
16619
16620 static struct dwarf2_per_cu_data *
16621 dwarf2_find_containing_comp_unit (sect_offset offset,
16622 struct objfile *objfile)
16623 {
16624 struct dwarf2_per_cu_data *this_cu;
16625 int low, high;
16626
16627 low = 0;
16628 high = dwarf2_per_objfile->n_comp_units - 1;
16629 while (high > low)
16630 {
16631 int mid = low + (high - low) / 2;
16632
16633 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16634 >= offset.sect_off)
16635 high = mid;
16636 else
16637 low = mid + 1;
16638 }
16639 gdb_assert (low == high);
16640 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16641 > offset.sect_off)
16642 {
16643 if (low == 0)
16644 error (_("Dwarf Error: could not find partial DIE containing "
16645 "offset 0x%lx [in module %s]"),
16646 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16647
16648 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16649 <= offset.sect_off);
16650 return dwarf2_per_objfile->all_comp_units[low-1];
16651 }
16652 else
16653 {
16654 this_cu = dwarf2_per_objfile->all_comp_units[low];
16655 if (low == dwarf2_per_objfile->n_comp_units - 1
16656 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16657 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16658 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16659 return this_cu;
16660 }
16661 }
16662
16663 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16664
16665 static void
16666 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16667 {
16668 memset (cu, 0, sizeof (*cu));
16669 per_cu->cu = cu;
16670 cu->per_cu = per_cu;
16671 cu->objfile = per_cu->objfile;
16672 obstack_init (&cu->comp_unit_obstack);
16673 }
16674
16675 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16676
16677 static void
16678 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16679 enum language pretend_language)
16680 {
16681 struct attribute *attr;
16682
16683 /* Set the language we're debugging. */
16684 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16685 if (attr)
16686 set_cu_language (DW_UNSND (attr), cu);
16687 else
16688 {
16689 cu->language = pretend_language;
16690 cu->language_defn = language_def (cu->language);
16691 }
16692
16693 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16694 if (attr)
16695 cu->producer = DW_STRING (attr);
16696 }
16697
16698 /* Release one cached compilation unit, CU. We unlink it from the tree
16699 of compilation units, but we don't remove it from the read_in_chain;
16700 the caller is responsible for that.
16701 NOTE: DATA is a void * because this function is also used as a
16702 cleanup routine. */
16703
16704 static void
16705 free_heap_comp_unit (void *data)
16706 {
16707 struct dwarf2_cu *cu = data;
16708
16709 gdb_assert (cu->per_cu != NULL);
16710 cu->per_cu->cu = NULL;
16711 cu->per_cu = NULL;
16712
16713 obstack_free (&cu->comp_unit_obstack, NULL);
16714
16715 xfree (cu);
16716 }
16717
16718 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16719 when we're finished with it. We can't free the pointer itself, but be
16720 sure to unlink it from the cache. Also release any associated storage. */
16721
16722 static void
16723 free_stack_comp_unit (void *data)
16724 {
16725 struct dwarf2_cu *cu = data;
16726
16727 gdb_assert (cu->per_cu != NULL);
16728 cu->per_cu->cu = NULL;
16729 cu->per_cu = NULL;
16730
16731 obstack_free (&cu->comp_unit_obstack, NULL);
16732 cu->partial_dies = NULL;
16733 }
16734
16735 /* Free all cached compilation units. */
16736
16737 static void
16738 free_cached_comp_units (void *data)
16739 {
16740 struct dwarf2_per_cu_data *per_cu, **last_chain;
16741
16742 per_cu = dwarf2_per_objfile->read_in_chain;
16743 last_chain = &dwarf2_per_objfile->read_in_chain;
16744 while (per_cu != NULL)
16745 {
16746 struct dwarf2_per_cu_data *next_cu;
16747
16748 next_cu = per_cu->cu->read_in_chain;
16749
16750 free_heap_comp_unit (per_cu->cu);
16751 *last_chain = next_cu;
16752
16753 per_cu = next_cu;
16754 }
16755 }
16756
16757 /* Increase the age counter on each cached compilation unit, and free
16758 any that are too old. */
16759
16760 static void
16761 age_cached_comp_units (void)
16762 {
16763 struct dwarf2_per_cu_data *per_cu, **last_chain;
16764
16765 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16766 per_cu = dwarf2_per_objfile->read_in_chain;
16767 while (per_cu != NULL)
16768 {
16769 per_cu->cu->last_used ++;
16770 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16771 dwarf2_mark (per_cu->cu);
16772 per_cu = per_cu->cu->read_in_chain;
16773 }
16774
16775 per_cu = dwarf2_per_objfile->read_in_chain;
16776 last_chain = &dwarf2_per_objfile->read_in_chain;
16777 while (per_cu != NULL)
16778 {
16779 struct dwarf2_per_cu_data *next_cu;
16780
16781 next_cu = per_cu->cu->read_in_chain;
16782
16783 if (!per_cu->cu->mark)
16784 {
16785 free_heap_comp_unit (per_cu->cu);
16786 *last_chain = next_cu;
16787 }
16788 else
16789 last_chain = &per_cu->cu->read_in_chain;
16790
16791 per_cu = next_cu;
16792 }
16793 }
16794
16795 /* Remove a single compilation unit from the cache. */
16796
16797 static void
16798 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16799 {
16800 struct dwarf2_per_cu_data *per_cu, **last_chain;
16801
16802 per_cu = dwarf2_per_objfile->read_in_chain;
16803 last_chain = &dwarf2_per_objfile->read_in_chain;
16804 while (per_cu != NULL)
16805 {
16806 struct dwarf2_per_cu_data *next_cu;
16807
16808 next_cu = per_cu->cu->read_in_chain;
16809
16810 if (per_cu == target_per_cu)
16811 {
16812 free_heap_comp_unit (per_cu->cu);
16813 per_cu->cu = NULL;
16814 *last_chain = next_cu;
16815 break;
16816 }
16817 else
16818 last_chain = &per_cu->cu->read_in_chain;
16819
16820 per_cu = next_cu;
16821 }
16822 }
16823
16824 /* Release all extra memory associated with OBJFILE. */
16825
16826 void
16827 dwarf2_free_objfile (struct objfile *objfile)
16828 {
16829 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16830
16831 if (dwarf2_per_objfile == NULL)
16832 return;
16833
16834 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16835 free_cached_comp_units (NULL);
16836
16837 if (dwarf2_per_objfile->quick_file_names_table)
16838 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16839
16840 /* Everything else should be on the objfile obstack. */
16841 }
16842
16843 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16844 We store these in a hash table separate from the DIEs, and preserve them
16845 when the DIEs are flushed out of cache.
16846
16847 The CU "per_cu" pointer is needed because offset alone is not enough to
16848 uniquely identify the type. A file may have multiple .debug_types sections,
16849 or the type may come from a DWO file. We have to use something in
16850 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16851 routine, get_die_type_at_offset, from outside this file, and thus won't
16852 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16853 of the objfile. */
16854
16855 struct dwarf2_per_cu_offset_and_type
16856 {
16857 const struct dwarf2_per_cu_data *per_cu;
16858 sect_offset offset;
16859 struct type *type;
16860 };
16861
16862 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16863
16864 static hashval_t
16865 per_cu_offset_and_type_hash (const void *item)
16866 {
16867 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16868
16869 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16870 }
16871
16872 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16873
16874 static int
16875 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16876 {
16877 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16878 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16879
16880 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16881 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16882 }
16883
16884 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16885 table if necessary. For convenience, return TYPE.
16886
16887 The DIEs reading must have careful ordering to:
16888 * Not cause infite loops trying to read in DIEs as a prerequisite for
16889 reading current DIE.
16890 * Not trying to dereference contents of still incompletely read in types
16891 while reading in other DIEs.
16892 * Enable referencing still incompletely read in types just by a pointer to
16893 the type without accessing its fields.
16894
16895 Therefore caller should follow these rules:
16896 * Try to fetch any prerequisite types we may need to build this DIE type
16897 before building the type and calling set_die_type.
16898 * After building type call set_die_type for current DIE as soon as
16899 possible before fetching more types to complete the current type.
16900 * Make the type as complete as possible before fetching more types. */
16901
16902 static struct type *
16903 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16904 {
16905 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16906 struct objfile *objfile = cu->objfile;
16907
16908 /* For Ada types, make sure that the gnat-specific data is always
16909 initialized (if not already set). There are a few types where
16910 we should not be doing so, because the type-specific area is
16911 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16912 where the type-specific area is used to store the floatformat).
16913 But this is not a problem, because the gnat-specific information
16914 is actually not needed for these types. */
16915 if (need_gnat_info (cu)
16916 && TYPE_CODE (type) != TYPE_CODE_FUNC
16917 && TYPE_CODE (type) != TYPE_CODE_FLT
16918 && !HAVE_GNAT_AUX_INFO (type))
16919 INIT_GNAT_SPECIFIC (type);
16920
16921 if (dwarf2_per_objfile->die_type_hash == NULL)
16922 {
16923 dwarf2_per_objfile->die_type_hash =
16924 htab_create_alloc_ex (127,
16925 per_cu_offset_and_type_hash,
16926 per_cu_offset_and_type_eq,
16927 NULL,
16928 &objfile->objfile_obstack,
16929 hashtab_obstack_allocate,
16930 dummy_obstack_deallocate);
16931 }
16932
16933 ofs.per_cu = cu->per_cu;
16934 ofs.offset = die->offset;
16935 ofs.type = type;
16936 slot = (struct dwarf2_per_cu_offset_and_type **)
16937 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16938 if (*slot)
16939 complaint (&symfile_complaints,
16940 _("A problem internal to GDB: DIE 0x%x has type already set"),
16941 die->offset.sect_off);
16942 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16943 **slot = ofs;
16944 return type;
16945 }
16946
16947 /* Look up the type for the die at OFFSET in the appropriate type_hash
16948 table, or return NULL if the die does not have a saved type. */
16949
16950 static struct type *
16951 get_die_type_at_offset (sect_offset offset,
16952 struct dwarf2_per_cu_data *per_cu)
16953 {
16954 struct dwarf2_per_cu_offset_and_type *slot, ofs;
16955
16956 if (dwarf2_per_objfile->die_type_hash == NULL)
16957 return NULL;
16958
16959 ofs.per_cu = per_cu;
16960 ofs.offset = offset;
16961 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
16962 if (slot)
16963 return slot->type;
16964 else
16965 return NULL;
16966 }
16967
16968 /* Look up the type for DIE in the appropriate type_hash table,
16969 or return NULL if DIE does not have a saved type. */
16970
16971 static struct type *
16972 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16973 {
16974 return get_die_type_at_offset (die->offset, cu->per_cu);
16975 }
16976
16977 /* Add a dependence relationship from CU to REF_PER_CU. */
16978
16979 static void
16980 dwarf2_add_dependence (struct dwarf2_cu *cu,
16981 struct dwarf2_per_cu_data *ref_per_cu)
16982 {
16983 void **slot;
16984
16985 if (cu->dependencies == NULL)
16986 cu->dependencies
16987 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16988 NULL, &cu->comp_unit_obstack,
16989 hashtab_obstack_allocate,
16990 dummy_obstack_deallocate);
16991
16992 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16993 if (*slot == NULL)
16994 *slot = ref_per_cu;
16995 }
16996
16997 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16998 Set the mark field in every compilation unit in the
16999 cache that we must keep because we are keeping CU. */
17000
17001 static int
17002 dwarf2_mark_helper (void **slot, void *data)
17003 {
17004 struct dwarf2_per_cu_data *per_cu;
17005
17006 per_cu = (struct dwarf2_per_cu_data *) *slot;
17007
17008 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17009 reading of the chain. As such dependencies remain valid it is not much
17010 useful to track and undo them during QUIT cleanups. */
17011 if (per_cu->cu == NULL)
17012 return 1;
17013
17014 if (per_cu->cu->mark)
17015 return 1;
17016 per_cu->cu->mark = 1;
17017
17018 if (per_cu->cu->dependencies != NULL)
17019 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17020
17021 return 1;
17022 }
17023
17024 /* Set the mark field in CU and in every other compilation unit in the
17025 cache that we must keep because we are keeping CU. */
17026
17027 static void
17028 dwarf2_mark (struct dwarf2_cu *cu)
17029 {
17030 if (cu->mark)
17031 return;
17032 cu->mark = 1;
17033 if (cu->dependencies != NULL)
17034 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17035 }
17036
17037 static void
17038 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17039 {
17040 while (per_cu)
17041 {
17042 per_cu->cu->mark = 0;
17043 per_cu = per_cu->cu->read_in_chain;
17044 }
17045 }
17046
17047 /* Trivial hash function for partial_die_info: the hash value of a DIE
17048 is its offset in .debug_info for this objfile. */
17049
17050 static hashval_t
17051 partial_die_hash (const void *item)
17052 {
17053 const struct partial_die_info *part_die = item;
17054
17055 return part_die->offset.sect_off;
17056 }
17057
17058 /* Trivial comparison function for partial_die_info structures: two DIEs
17059 are equal if they have the same offset. */
17060
17061 static int
17062 partial_die_eq (const void *item_lhs, const void *item_rhs)
17063 {
17064 const struct partial_die_info *part_die_lhs = item_lhs;
17065 const struct partial_die_info *part_die_rhs = item_rhs;
17066
17067 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17068 }
17069
17070 static struct cmd_list_element *set_dwarf2_cmdlist;
17071 static struct cmd_list_element *show_dwarf2_cmdlist;
17072
17073 static void
17074 set_dwarf2_cmd (char *args, int from_tty)
17075 {
17076 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17077 }
17078
17079 static void
17080 show_dwarf2_cmd (char *args, int from_tty)
17081 {
17082 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17083 }
17084
17085 /* If section described by INFO was mmapped, munmap it now. */
17086
17087 static void
17088 munmap_section_buffer (struct dwarf2_section_info *info)
17089 {
17090 if (info->map_addr != NULL)
17091 {
17092 #ifdef HAVE_MMAP
17093 int res;
17094
17095 res = munmap (info->map_addr, info->map_len);
17096 gdb_assert (res == 0);
17097 #else
17098 /* Without HAVE_MMAP, we should never be here to begin with. */
17099 gdb_assert_not_reached ("no mmap support");
17100 #endif
17101 }
17102 }
17103
17104 /* munmap debug sections for OBJFILE, if necessary. */
17105
17106 static void
17107 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17108 {
17109 struct dwarf2_per_objfile *data = d;
17110 int ix;
17111 struct dwarf2_section_info *section;
17112
17113 /* This is sorted according to the order they're defined in to make it easier
17114 to keep in sync. */
17115 munmap_section_buffer (&data->info);
17116 munmap_section_buffer (&data->abbrev);
17117 munmap_section_buffer (&data->line);
17118 munmap_section_buffer (&data->loc);
17119 munmap_section_buffer (&data->macinfo);
17120 munmap_section_buffer (&data->macro);
17121 munmap_section_buffer (&data->str);
17122 munmap_section_buffer (&data->ranges);
17123 munmap_section_buffer (&data->addr);
17124 munmap_section_buffer (&data->frame);
17125 munmap_section_buffer (&data->eh_frame);
17126 munmap_section_buffer (&data->gdb_index);
17127
17128 for (ix = 0;
17129 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17130 ++ix)
17131 munmap_section_buffer (section);
17132
17133 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17134 VEC_free (dwarf2_per_cu_ptr,
17135 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17136
17137 VEC_free (dwarf2_section_info_def, data->types);
17138
17139 if (data->dwo_files)
17140 free_dwo_files (data->dwo_files, objfile);
17141 }
17142
17143 \f
17144 /* The "save gdb-index" command. */
17145
17146 /* The contents of the hash table we create when building the string
17147 table. */
17148 struct strtab_entry
17149 {
17150 offset_type offset;
17151 const char *str;
17152 };
17153
17154 /* Hash function for a strtab_entry.
17155
17156 Function is used only during write_hash_table so no index format backward
17157 compatibility is needed. */
17158
17159 static hashval_t
17160 hash_strtab_entry (const void *e)
17161 {
17162 const struct strtab_entry *entry = e;
17163 return mapped_index_string_hash (INT_MAX, entry->str);
17164 }
17165
17166 /* Equality function for a strtab_entry. */
17167
17168 static int
17169 eq_strtab_entry (const void *a, const void *b)
17170 {
17171 const struct strtab_entry *ea = a;
17172 const struct strtab_entry *eb = b;
17173 return !strcmp (ea->str, eb->str);
17174 }
17175
17176 /* Create a strtab_entry hash table. */
17177
17178 static htab_t
17179 create_strtab (void)
17180 {
17181 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17182 xfree, xcalloc, xfree);
17183 }
17184
17185 /* Add a string to the constant pool. Return the string's offset in
17186 host order. */
17187
17188 static offset_type
17189 add_string (htab_t table, struct obstack *cpool, const char *str)
17190 {
17191 void **slot;
17192 struct strtab_entry entry;
17193 struct strtab_entry *result;
17194
17195 entry.str = str;
17196 slot = htab_find_slot (table, &entry, INSERT);
17197 if (*slot)
17198 result = *slot;
17199 else
17200 {
17201 result = XNEW (struct strtab_entry);
17202 result->offset = obstack_object_size (cpool);
17203 result->str = str;
17204 obstack_grow_str0 (cpool, str);
17205 *slot = result;
17206 }
17207 return result->offset;
17208 }
17209
17210 /* An entry in the symbol table. */
17211 struct symtab_index_entry
17212 {
17213 /* The name of the symbol. */
17214 const char *name;
17215 /* The offset of the name in the constant pool. */
17216 offset_type index_offset;
17217 /* A sorted vector of the indices of all the CUs that hold an object
17218 of this name. */
17219 VEC (offset_type) *cu_indices;
17220 };
17221
17222 /* The symbol table. This is a power-of-2-sized hash table. */
17223 struct mapped_symtab
17224 {
17225 offset_type n_elements;
17226 offset_type size;
17227 struct symtab_index_entry **data;
17228 };
17229
17230 /* Hash function for a symtab_index_entry. */
17231
17232 static hashval_t
17233 hash_symtab_entry (const void *e)
17234 {
17235 const struct symtab_index_entry *entry = e;
17236 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17237 sizeof (offset_type) * VEC_length (offset_type,
17238 entry->cu_indices),
17239 0);
17240 }
17241
17242 /* Equality function for a symtab_index_entry. */
17243
17244 static int
17245 eq_symtab_entry (const void *a, const void *b)
17246 {
17247 const struct symtab_index_entry *ea = a;
17248 const struct symtab_index_entry *eb = b;
17249 int len = VEC_length (offset_type, ea->cu_indices);
17250 if (len != VEC_length (offset_type, eb->cu_indices))
17251 return 0;
17252 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17253 VEC_address (offset_type, eb->cu_indices),
17254 sizeof (offset_type) * len);
17255 }
17256
17257 /* Destroy a symtab_index_entry. */
17258
17259 static void
17260 delete_symtab_entry (void *p)
17261 {
17262 struct symtab_index_entry *entry = p;
17263 VEC_free (offset_type, entry->cu_indices);
17264 xfree (entry);
17265 }
17266
17267 /* Create a hash table holding symtab_index_entry objects. */
17268
17269 static htab_t
17270 create_symbol_hash_table (void)
17271 {
17272 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17273 delete_symtab_entry, xcalloc, xfree);
17274 }
17275
17276 /* Create a new mapped symtab object. */
17277
17278 static struct mapped_symtab *
17279 create_mapped_symtab (void)
17280 {
17281 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17282 symtab->n_elements = 0;
17283 symtab->size = 1024;
17284 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17285 return symtab;
17286 }
17287
17288 /* Destroy a mapped_symtab. */
17289
17290 static void
17291 cleanup_mapped_symtab (void *p)
17292 {
17293 struct mapped_symtab *symtab = p;
17294 /* The contents of the array are freed when the other hash table is
17295 destroyed. */
17296 xfree (symtab->data);
17297 xfree (symtab);
17298 }
17299
17300 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17301 the slot.
17302
17303 Function is used only during write_hash_table so no index format backward
17304 compatibility is needed. */
17305
17306 static struct symtab_index_entry **
17307 find_slot (struct mapped_symtab *symtab, const char *name)
17308 {
17309 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17310
17311 index = hash & (symtab->size - 1);
17312 step = ((hash * 17) & (symtab->size - 1)) | 1;
17313
17314 for (;;)
17315 {
17316 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17317 return &symtab->data[index];
17318 index = (index + step) & (symtab->size - 1);
17319 }
17320 }
17321
17322 /* Expand SYMTAB's hash table. */
17323
17324 static void
17325 hash_expand (struct mapped_symtab *symtab)
17326 {
17327 offset_type old_size = symtab->size;
17328 offset_type i;
17329 struct symtab_index_entry **old_entries = symtab->data;
17330
17331 symtab->size *= 2;
17332 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17333
17334 for (i = 0; i < old_size; ++i)
17335 {
17336 if (old_entries[i])
17337 {
17338 struct symtab_index_entry **slot = find_slot (symtab,
17339 old_entries[i]->name);
17340 *slot = old_entries[i];
17341 }
17342 }
17343
17344 xfree (old_entries);
17345 }
17346
17347 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17348 is the index of the CU in which the symbol appears. */
17349
17350 static void
17351 add_index_entry (struct mapped_symtab *symtab, const char *name,
17352 offset_type cu_index)
17353 {
17354 struct symtab_index_entry **slot;
17355
17356 ++symtab->n_elements;
17357 if (4 * symtab->n_elements / 3 >= symtab->size)
17358 hash_expand (symtab);
17359
17360 slot = find_slot (symtab, name);
17361 if (!*slot)
17362 {
17363 *slot = XNEW (struct symtab_index_entry);
17364 (*slot)->name = name;
17365 (*slot)->cu_indices = NULL;
17366 }
17367 /* Don't push an index twice. Due to how we add entries we only
17368 have to check the last one. */
17369 if (VEC_empty (offset_type, (*slot)->cu_indices)
17370 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17371 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17372 }
17373
17374 /* Add a vector of indices to the constant pool. */
17375
17376 static offset_type
17377 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17378 struct symtab_index_entry *entry)
17379 {
17380 void **slot;
17381
17382 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17383 if (!*slot)
17384 {
17385 offset_type len = VEC_length (offset_type, entry->cu_indices);
17386 offset_type val = MAYBE_SWAP (len);
17387 offset_type iter;
17388 int i;
17389
17390 *slot = entry;
17391 entry->index_offset = obstack_object_size (cpool);
17392
17393 obstack_grow (cpool, &val, sizeof (val));
17394 for (i = 0;
17395 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17396 ++i)
17397 {
17398 val = MAYBE_SWAP (iter);
17399 obstack_grow (cpool, &val, sizeof (val));
17400 }
17401 }
17402 else
17403 {
17404 struct symtab_index_entry *old_entry = *slot;
17405 entry->index_offset = old_entry->index_offset;
17406 entry = old_entry;
17407 }
17408 return entry->index_offset;
17409 }
17410
17411 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17412 constant pool entries going into the obstack CPOOL. */
17413
17414 static void
17415 write_hash_table (struct mapped_symtab *symtab,
17416 struct obstack *output, struct obstack *cpool)
17417 {
17418 offset_type i;
17419 htab_t symbol_hash_table;
17420 htab_t str_table;
17421
17422 symbol_hash_table = create_symbol_hash_table ();
17423 str_table = create_strtab ();
17424
17425 /* We add all the index vectors to the constant pool first, to
17426 ensure alignment is ok. */
17427 for (i = 0; i < symtab->size; ++i)
17428 {
17429 if (symtab->data[i])
17430 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17431 }
17432
17433 /* Now write out the hash table. */
17434 for (i = 0; i < symtab->size; ++i)
17435 {
17436 offset_type str_off, vec_off;
17437
17438 if (symtab->data[i])
17439 {
17440 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17441 vec_off = symtab->data[i]->index_offset;
17442 }
17443 else
17444 {
17445 /* While 0 is a valid constant pool index, it is not valid
17446 to have 0 for both offsets. */
17447 str_off = 0;
17448 vec_off = 0;
17449 }
17450
17451 str_off = MAYBE_SWAP (str_off);
17452 vec_off = MAYBE_SWAP (vec_off);
17453
17454 obstack_grow (output, &str_off, sizeof (str_off));
17455 obstack_grow (output, &vec_off, sizeof (vec_off));
17456 }
17457
17458 htab_delete (str_table);
17459 htab_delete (symbol_hash_table);
17460 }
17461
17462 /* Struct to map psymtab to CU index in the index file. */
17463 struct psymtab_cu_index_map
17464 {
17465 struct partial_symtab *psymtab;
17466 unsigned int cu_index;
17467 };
17468
17469 static hashval_t
17470 hash_psymtab_cu_index (const void *item)
17471 {
17472 const struct psymtab_cu_index_map *map = item;
17473
17474 return htab_hash_pointer (map->psymtab);
17475 }
17476
17477 static int
17478 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17479 {
17480 const struct psymtab_cu_index_map *lhs = item_lhs;
17481 const struct psymtab_cu_index_map *rhs = item_rhs;
17482
17483 return lhs->psymtab == rhs->psymtab;
17484 }
17485
17486 /* Helper struct for building the address table. */
17487 struct addrmap_index_data
17488 {
17489 struct objfile *objfile;
17490 struct obstack *addr_obstack;
17491 htab_t cu_index_htab;
17492
17493 /* Non-zero if the previous_* fields are valid.
17494 We can't write an entry until we see the next entry (since it is only then
17495 that we know the end of the entry). */
17496 int previous_valid;
17497 /* Index of the CU in the table of all CUs in the index file. */
17498 unsigned int previous_cu_index;
17499 /* Start address of the CU. */
17500 CORE_ADDR previous_cu_start;
17501 };
17502
17503 /* Write an address entry to OBSTACK. */
17504
17505 static void
17506 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17507 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17508 {
17509 offset_type cu_index_to_write;
17510 char addr[8];
17511 CORE_ADDR baseaddr;
17512
17513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17514
17515 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17516 obstack_grow (obstack, addr, 8);
17517 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17518 obstack_grow (obstack, addr, 8);
17519 cu_index_to_write = MAYBE_SWAP (cu_index);
17520 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17521 }
17522
17523 /* Worker function for traversing an addrmap to build the address table. */
17524
17525 static int
17526 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17527 {
17528 struct addrmap_index_data *data = datap;
17529 struct partial_symtab *pst = obj;
17530 offset_type cu_index;
17531 void **slot;
17532
17533 if (data->previous_valid)
17534 add_address_entry (data->objfile, data->addr_obstack,
17535 data->previous_cu_start, start_addr,
17536 data->previous_cu_index);
17537
17538 data->previous_cu_start = start_addr;
17539 if (pst != NULL)
17540 {
17541 struct psymtab_cu_index_map find_map, *map;
17542 find_map.psymtab = pst;
17543 map = htab_find (data->cu_index_htab, &find_map);
17544 gdb_assert (map != NULL);
17545 data->previous_cu_index = map->cu_index;
17546 data->previous_valid = 1;
17547 }
17548 else
17549 data->previous_valid = 0;
17550
17551 return 0;
17552 }
17553
17554 /* Write OBJFILE's address map to OBSTACK.
17555 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17556 in the index file. */
17557
17558 static void
17559 write_address_map (struct objfile *objfile, struct obstack *obstack,
17560 htab_t cu_index_htab)
17561 {
17562 struct addrmap_index_data addrmap_index_data;
17563
17564 /* When writing the address table, we have to cope with the fact that
17565 the addrmap iterator only provides the start of a region; we have to
17566 wait until the next invocation to get the start of the next region. */
17567
17568 addrmap_index_data.objfile = objfile;
17569 addrmap_index_data.addr_obstack = obstack;
17570 addrmap_index_data.cu_index_htab = cu_index_htab;
17571 addrmap_index_data.previous_valid = 0;
17572
17573 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17574 &addrmap_index_data);
17575
17576 /* It's highly unlikely the last entry (end address = 0xff...ff)
17577 is valid, but we should still handle it.
17578 The end address is recorded as the start of the next region, but that
17579 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17580 anyway. */
17581 if (addrmap_index_data.previous_valid)
17582 add_address_entry (objfile, obstack,
17583 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17584 addrmap_index_data.previous_cu_index);
17585 }
17586
17587 /* Add a list of partial symbols to SYMTAB. */
17588
17589 static void
17590 write_psymbols (struct mapped_symtab *symtab,
17591 htab_t psyms_seen,
17592 struct partial_symbol **psymp,
17593 int count,
17594 offset_type cu_index,
17595 int is_static)
17596 {
17597 for (; count-- > 0; ++psymp)
17598 {
17599 void **slot, *lookup;
17600
17601 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17602 error (_("Ada is not currently supported by the index"));
17603
17604 /* We only want to add a given psymbol once. However, we also
17605 want to account for whether it is global or static. So, we
17606 may add it twice, using slightly different values. */
17607 if (is_static)
17608 {
17609 uintptr_t val = 1 | (uintptr_t) *psymp;
17610
17611 lookup = (void *) val;
17612 }
17613 else
17614 lookup = *psymp;
17615
17616 /* Only add a given psymbol once. */
17617 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17618 if (!*slot)
17619 {
17620 *slot = lookup;
17621 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17622 }
17623 }
17624 }
17625
17626 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17627 exception if there is an error. */
17628
17629 static void
17630 write_obstack (FILE *file, struct obstack *obstack)
17631 {
17632 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17633 file)
17634 != obstack_object_size (obstack))
17635 error (_("couldn't data write to file"));
17636 }
17637
17638 /* Unlink a file if the argument is not NULL. */
17639
17640 static void
17641 unlink_if_set (void *p)
17642 {
17643 char **filename = p;
17644 if (*filename)
17645 unlink (*filename);
17646 }
17647
17648 /* A helper struct used when iterating over debug_types. */
17649 struct signatured_type_index_data
17650 {
17651 struct objfile *objfile;
17652 struct mapped_symtab *symtab;
17653 struct obstack *types_list;
17654 htab_t psyms_seen;
17655 int cu_index;
17656 };
17657
17658 /* A helper function that writes a single signatured_type to an
17659 obstack. */
17660
17661 static int
17662 write_one_signatured_type (void **slot, void *d)
17663 {
17664 struct signatured_type_index_data *info = d;
17665 struct signatured_type *entry = (struct signatured_type *) *slot;
17666 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17667 struct partial_symtab *psymtab = per_cu->v.psymtab;
17668 gdb_byte val[8];
17669
17670 write_psymbols (info->symtab,
17671 info->psyms_seen,
17672 info->objfile->global_psymbols.list
17673 + psymtab->globals_offset,
17674 psymtab->n_global_syms, info->cu_index,
17675 0);
17676 write_psymbols (info->symtab,
17677 info->psyms_seen,
17678 info->objfile->static_psymbols.list
17679 + psymtab->statics_offset,
17680 psymtab->n_static_syms, info->cu_index,
17681 1);
17682
17683 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17684 entry->per_cu.offset.sect_off);
17685 obstack_grow (info->types_list, val, 8);
17686 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17687 entry->type_offset_in_tu.cu_off);
17688 obstack_grow (info->types_list, val, 8);
17689 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17690 obstack_grow (info->types_list, val, 8);
17691
17692 ++info->cu_index;
17693
17694 return 1;
17695 }
17696
17697 /* Recurse into all "included" dependencies and write their symbols as
17698 if they appeared in this psymtab. */
17699
17700 static void
17701 recursively_write_psymbols (struct objfile *objfile,
17702 struct partial_symtab *psymtab,
17703 struct mapped_symtab *symtab,
17704 htab_t psyms_seen,
17705 offset_type cu_index)
17706 {
17707 int i;
17708
17709 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17710 if (psymtab->dependencies[i]->user != NULL)
17711 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17712 symtab, psyms_seen, cu_index);
17713
17714 write_psymbols (symtab,
17715 psyms_seen,
17716 objfile->global_psymbols.list + psymtab->globals_offset,
17717 psymtab->n_global_syms, cu_index,
17718 0);
17719 write_psymbols (symtab,
17720 psyms_seen,
17721 objfile->static_psymbols.list + psymtab->statics_offset,
17722 psymtab->n_static_syms, cu_index,
17723 1);
17724 }
17725
17726 /* Create an index file for OBJFILE in the directory DIR. */
17727
17728 static void
17729 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17730 {
17731 struct cleanup *cleanup;
17732 char *filename, *cleanup_filename;
17733 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17734 struct obstack cu_list, types_cu_list;
17735 int i;
17736 FILE *out_file;
17737 struct mapped_symtab *symtab;
17738 offset_type val, size_of_contents, total_len;
17739 struct stat st;
17740 char buf[8];
17741 htab_t psyms_seen;
17742 htab_t cu_index_htab;
17743 struct psymtab_cu_index_map *psymtab_cu_index_map;
17744
17745 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17746 return;
17747
17748 if (dwarf2_per_objfile->using_index)
17749 error (_("Cannot use an index to create the index"));
17750
17751 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17752 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17753
17754 if (stat (objfile->name, &st) < 0)
17755 perror_with_name (objfile->name);
17756
17757 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17758 INDEX_SUFFIX, (char *) NULL);
17759 cleanup = make_cleanup (xfree, filename);
17760
17761 out_file = fopen (filename, "wb");
17762 if (!out_file)
17763 error (_("Can't open `%s' for writing"), filename);
17764
17765 cleanup_filename = filename;
17766 make_cleanup (unlink_if_set, &cleanup_filename);
17767
17768 symtab = create_mapped_symtab ();
17769 make_cleanup (cleanup_mapped_symtab, symtab);
17770
17771 obstack_init (&addr_obstack);
17772 make_cleanup_obstack_free (&addr_obstack);
17773
17774 obstack_init (&cu_list);
17775 make_cleanup_obstack_free (&cu_list);
17776
17777 obstack_init (&types_cu_list);
17778 make_cleanup_obstack_free (&types_cu_list);
17779
17780 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17781 NULL, xcalloc, xfree);
17782 make_cleanup_htab_delete (psyms_seen);
17783
17784 /* While we're scanning CU's create a table that maps a psymtab pointer
17785 (which is what addrmap records) to its index (which is what is recorded
17786 in the index file). This will later be needed to write the address
17787 table. */
17788 cu_index_htab = htab_create_alloc (100,
17789 hash_psymtab_cu_index,
17790 eq_psymtab_cu_index,
17791 NULL, xcalloc, xfree);
17792 make_cleanup_htab_delete (cu_index_htab);
17793 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17794 xmalloc (sizeof (struct psymtab_cu_index_map)
17795 * dwarf2_per_objfile->n_comp_units);
17796 make_cleanup (xfree, psymtab_cu_index_map);
17797
17798 /* The CU list is already sorted, so we don't need to do additional
17799 work here. Also, the debug_types entries do not appear in
17800 all_comp_units, but only in their own hash table. */
17801 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17802 {
17803 struct dwarf2_per_cu_data *per_cu
17804 = dwarf2_per_objfile->all_comp_units[i];
17805 struct partial_symtab *psymtab = per_cu->v.psymtab;
17806 gdb_byte val[8];
17807 struct psymtab_cu_index_map *map;
17808 void **slot;
17809
17810 if (psymtab->user == NULL)
17811 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
17812
17813 map = &psymtab_cu_index_map[i];
17814 map->psymtab = psymtab;
17815 map->cu_index = i;
17816 slot = htab_find_slot (cu_index_htab, map, INSERT);
17817 gdb_assert (slot != NULL);
17818 gdb_assert (*slot == NULL);
17819 *slot = map;
17820
17821 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17822 per_cu->offset.sect_off);
17823 obstack_grow (&cu_list, val, 8);
17824 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17825 obstack_grow (&cu_list, val, 8);
17826 }
17827
17828 /* Dump the address map. */
17829 write_address_map (objfile, &addr_obstack, cu_index_htab);
17830
17831 /* Write out the .debug_type entries, if any. */
17832 if (dwarf2_per_objfile->signatured_types)
17833 {
17834 struct signatured_type_index_data sig_data;
17835
17836 sig_data.objfile = objfile;
17837 sig_data.symtab = symtab;
17838 sig_data.types_list = &types_cu_list;
17839 sig_data.psyms_seen = psyms_seen;
17840 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17841 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17842 write_one_signatured_type, &sig_data);
17843 }
17844
17845 obstack_init (&constant_pool);
17846 make_cleanup_obstack_free (&constant_pool);
17847 obstack_init (&symtab_obstack);
17848 make_cleanup_obstack_free (&symtab_obstack);
17849 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17850
17851 obstack_init (&contents);
17852 make_cleanup_obstack_free (&contents);
17853 size_of_contents = 6 * sizeof (offset_type);
17854 total_len = size_of_contents;
17855
17856 /* The version number. */
17857 val = MAYBE_SWAP (6);
17858 obstack_grow (&contents, &val, sizeof (val));
17859
17860 /* The offset of the CU list from the start of the file. */
17861 val = MAYBE_SWAP (total_len);
17862 obstack_grow (&contents, &val, sizeof (val));
17863 total_len += obstack_object_size (&cu_list);
17864
17865 /* The offset of the types CU list from the start of the file. */
17866 val = MAYBE_SWAP (total_len);
17867 obstack_grow (&contents, &val, sizeof (val));
17868 total_len += obstack_object_size (&types_cu_list);
17869
17870 /* The offset of the address table from the start of the file. */
17871 val = MAYBE_SWAP (total_len);
17872 obstack_grow (&contents, &val, sizeof (val));
17873 total_len += obstack_object_size (&addr_obstack);
17874
17875 /* The offset of the symbol table from the start of the file. */
17876 val = MAYBE_SWAP (total_len);
17877 obstack_grow (&contents, &val, sizeof (val));
17878 total_len += obstack_object_size (&symtab_obstack);
17879
17880 /* The offset of the constant pool from the start of the file. */
17881 val = MAYBE_SWAP (total_len);
17882 obstack_grow (&contents, &val, sizeof (val));
17883 total_len += obstack_object_size (&constant_pool);
17884
17885 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17886
17887 write_obstack (out_file, &contents);
17888 write_obstack (out_file, &cu_list);
17889 write_obstack (out_file, &types_cu_list);
17890 write_obstack (out_file, &addr_obstack);
17891 write_obstack (out_file, &symtab_obstack);
17892 write_obstack (out_file, &constant_pool);
17893
17894 fclose (out_file);
17895
17896 /* We want to keep the file, so we set cleanup_filename to NULL
17897 here. See unlink_if_set. */
17898 cleanup_filename = NULL;
17899
17900 do_cleanups (cleanup);
17901 }
17902
17903 /* Implementation of the `save gdb-index' command.
17904
17905 Note that the file format used by this command is documented in the
17906 GDB manual. Any changes here must be documented there. */
17907
17908 static void
17909 save_gdb_index_command (char *arg, int from_tty)
17910 {
17911 struct objfile *objfile;
17912
17913 if (!arg || !*arg)
17914 error (_("usage: save gdb-index DIRECTORY"));
17915
17916 ALL_OBJFILES (objfile)
17917 {
17918 struct stat st;
17919
17920 /* If the objfile does not correspond to an actual file, skip it. */
17921 if (stat (objfile->name, &st) < 0)
17922 continue;
17923
17924 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17925 if (dwarf2_per_objfile)
17926 {
17927 volatile struct gdb_exception except;
17928
17929 TRY_CATCH (except, RETURN_MASK_ERROR)
17930 {
17931 write_psymtabs_to_index (objfile, arg);
17932 }
17933 if (except.reason < 0)
17934 exception_fprintf (gdb_stderr, except,
17935 _("Error while writing index for `%s': "),
17936 objfile->name);
17937 }
17938 }
17939 }
17940
17941 \f
17942
17943 int dwarf2_always_disassemble;
17944
17945 static void
17946 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17947 struct cmd_list_element *c, const char *value)
17948 {
17949 fprintf_filtered (file,
17950 _("Whether to always disassemble "
17951 "DWARF expressions is %s.\n"),
17952 value);
17953 }
17954
17955 static void
17956 show_check_physname (struct ui_file *file, int from_tty,
17957 struct cmd_list_element *c, const char *value)
17958 {
17959 fprintf_filtered (file,
17960 _("Whether to check \"physname\" is %s.\n"),
17961 value);
17962 }
17963
17964 void _initialize_dwarf2_read (void);
17965
17966 void
17967 _initialize_dwarf2_read (void)
17968 {
17969 struct cmd_list_element *c;
17970
17971 dwarf2_objfile_data_key
17972 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17973
17974 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17975 Set DWARF 2 specific variables.\n\
17976 Configure DWARF 2 variables such as the cache size"),
17977 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17978 0/*allow-unknown*/, &maintenance_set_cmdlist);
17979
17980 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17981 Show DWARF 2 specific variables\n\
17982 Show DWARF 2 variables such as the cache size"),
17983 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17984 0/*allow-unknown*/, &maintenance_show_cmdlist);
17985
17986 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17987 &dwarf2_max_cache_age, _("\
17988 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17989 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17990 A higher limit means that cached compilation units will be stored\n\
17991 in memory longer, and more total memory will be used. Zero disables\n\
17992 caching, which can slow down startup."),
17993 NULL,
17994 show_dwarf2_max_cache_age,
17995 &set_dwarf2_cmdlist,
17996 &show_dwarf2_cmdlist);
17997
17998 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17999 &dwarf2_always_disassemble, _("\
18000 Set whether `info address' always disassembles DWARF expressions."), _("\
18001 Show whether `info address' always disassembles DWARF expressions."), _("\
18002 When enabled, DWARF expressions are always printed in an assembly-like\n\
18003 syntax. When disabled, expressions will be printed in a more\n\
18004 conversational style, when possible."),
18005 NULL,
18006 show_dwarf2_always_disassemble,
18007 &set_dwarf2_cmdlist,
18008 &show_dwarf2_cmdlist);
18009
18010 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18011 Set debugging of the dwarf2 DIE reader."), _("\
18012 Show debugging of the dwarf2 DIE reader."), _("\
18013 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18014 The value is the maximum depth to print."),
18015 NULL,
18016 NULL,
18017 &setdebuglist, &showdebuglist);
18018
18019 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18020 Set cross-checking of \"physname\" code against demangler."), _("\
18021 Show cross-checking of \"physname\" code against demangler."), _("\
18022 When enabled, GDB's internal \"physname\" code is checked against\n\
18023 the demangler."),
18024 NULL, show_check_physname,
18025 &setdebuglist, &showdebuglist);
18026
18027 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18028 _("\
18029 Save a gdb-index file.\n\
18030 Usage: save gdb-index DIRECTORY"),
18031 &save_cmdlist);
18032 set_cmd_completer (c, filename_completer);
18033 }
This page took 0.458659 seconds and 4 git commands to generate.