* dwarf2read.c (dwarf_tag_name): Return const char *. Use
[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 /* Collection of data recorded per objfile.
152 This hangs off of dwarf2_objfile_data_key. */
153
154 struct dwarf2_per_objfile
155 {
156 struct dwarf2_section_info info;
157 struct dwarf2_section_info abbrev;
158 struct dwarf2_section_info line;
159 struct dwarf2_section_info loc;
160 struct dwarf2_section_info macinfo;
161 struct dwarf2_section_info macro;
162 struct dwarf2_section_info str;
163 struct dwarf2_section_info ranges;
164 struct dwarf2_section_info addr;
165 struct dwarf2_section_info frame;
166 struct dwarf2_section_info eh_frame;
167 struct dwarf2_section_info gdb_index;
168
169 VEC (dwarf2_section_info_def) *types;
170
171 /* Back link. */
172 struct objfile *objfile;
173
174 /* Table of all the compilation units. This is used to locate
175 the target compilation unit of a particular reference. */
176 struct dwarf2_per_cu_data **all_comp_units;
177
178 /* The number of compilation units in ALL_COMP_UNITS. */
179 int n_comp_units;
180
181 /* The number of .debug_types-related CUs. */
182 int n_type_units;
183
184 /* The .debug_types-related CUs (TUs). */
185 struct dwarf2_per_cu_data **all_type_units;
186
187 /* A chain of compilation units that are currently read in, so that
188 they can be freed later. */
189 struct dwarf2_per_cu_data *read_in_chain;
190
191 /* A table mapping .debug_types signatures to its signatured_type entry.
192 This is NULL if the .debug_types section hasn't been read in yet. */
193 htab_t signatured_types;
194
195 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
196 This is NULL if the table hasn't been allocated yet. */
197 htab_t dwo_files;
198
199 /* A flag indicating wether this objfile has a section loaded at a
200 VMA of 0. */
201 int has_section_at_zero;
202
203 /* True if we are using the mapped index,
204 or we are faking it for OBJF_READNOW's sake. */
205 unsigned char using_index;
206
207 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
208 struct mapped_index *index_table;
209
210 /* When using index_table, this keeps track of all quick_file_names entries.
211 TUs can share line table entries with CUs or other TUs, and there can be
212 a lot more TUs than unique line tables, so we maintain a separate table
213 of all line table entries to support the sharing. */
214 htab_t quick_file_names_table;
215
216 /* Set during partial symbol reading, to prevent queueing of full
217 symbols. */
218 int reading_partial_symbols;
219
220 /* Table mapping type DIEs to their struct type *.
221 This is NULL if not allocated yet.
222 The mapping is done via (CU/TU signature + DIE offset) -> type. */
223 htab_t die_type_hash;
224 };
225
226 static struct dwarf2_per_objfile *dwarf2_per_objfile;
227
228 /* Default names of the debugging sections. */
229
230 /* Note that if the debugging section has been compressed, it might
231 have a name like .zdebug_info. */
232
233 static const struct dwarf2_debug_sections dwarf2_elf_names =
234 {
235 { ".debug_info", ".zdebug_info" },
236 { ".debug_abbrev", ".zdebug_abbrev" },
237 { ".debug_line", ".zdebug_line" },
238 { ".debug_loc", ".zdebug_loc" },
239 { ".debug_macinfo", ".zdebug_macinfo" },
240 { ".debug_macro", ".zdebug_macro" },
241 { ".debug_str", ".zdebug_str" },
242 { ".debug_ranges", ".zdebug_ranges" },
243 { ".debug_types", ".zdebug_types" },
244 { ".debug_addr", ".zdebug_addr" },
245 { ".debug_frame", ".zdebug_frame" },
246 { ".eh_frame", NULL },
247 { ".gdb_index", ".zgdb_index" },
248 23
249 };
250
251 /* List of DWO sections. */
252
253 static const struct dwo_section_names
254 {
255 struct dwarf2_section_names abbrev_dwo;
256 struct dwarf2_section_names info_dwo;
257 struct dwarf2_section_names line_dwo;
258 struct dwarf2_section_names loc_dwo;
259 struct dwarf2_section_names str_dwo;
260 struct dwarf2_section_names str_offsets_dwo;
261 struct dwarf2_section_names types_dwo;
262 }
263 dwo_section_names =
264 {
265 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
266 { ".debug_info.dwo", ".zdebug_info.dwo" },
267 { ".debug_line.dwo", ".zdebug_line.dwo" },
268 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
269 { ".debug_str.dwo", ".zdebug_str.dwo" },
270 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
271 { ".debug_types.dwo", ".zdebug_types.dwo" },
272 };
273
274 /* local data types */
275
276 /* We hold several abbreviation tables in memory at the same time. */
277 #ifndef ABBREV_HASH_SIZE
278 #define ABBREV_HASH_SIZE 121
279 #endif
280
281 /* The data in a compilation unit header, after target2host
282 translation, looks like this. */
283 struct comp_unit_head
284 {
285 unsigned int length;
286 short version;
287 unsigned char addr_size;
288 unsigned char signed_addr_p;
289 sect_offset abbrev_offset;
290
291 /* Size of file offsets; either 4 or 8. */
292 unsigned int offset_size;
293
294 /* Size of the length field; either 4 or 12. */
295 unsigned int initial_length_size;
296
297 /* Offset to the first byte of this compilation unit header in the
298 .debug_info section, for resolving relative reference dies. */
299 sect_offset offset;
300
301 /* Offset to first die in this cu from the start of the cu.
302 This will be the first byte following the compilation unit header. */
303 cu_offset first_die_offset;
304 };
305
306 /* Type used for delaying computation of method physnames.
307 See comments for compute_delayed_physnames. */
308 struct delayed_method_info
309 {
310 /* The type to which the method is attached, i.e., its parent class. */
311 struct type *type;
312
313 /* The index of the method in the type's function fieldlists. */
314 int fnfield_index;
315
316 /* The index of the method in the fieldlist. */
317 int index;
318
319 /* The name of the DIE. */
320 const char *name;
321
322 /* The DIE associated with this method. */
323 struct die_info *die;
324 };
325
326 typedef struct delayed_method_info delayed_method_info;
327 DEF_VEC_O (delayed_method_info);
328
329 /* Internal state when decoding a particular compilation unit. */
330 struct dwarf2_cu
331 {
332 /* The objfile containing this compilation unit. */
333 struct objfile *objfile;
334
335 /* The header of the compilation unit. */
336 struct comp_unit_head header;
337
338 /* Base address of this compilation unit. */
339 CORE_ADDR base_address;
340
341 /* Non-zero if base_address has been set. */
342 int base_known;
343
344 /* The language we are debugging. */
345 enum language language;
346 const struct language_defn *language_defn;
347
348 const char *producer;
349
350 /* The generic symbol table building routines have separate lists for
351 file scope symbols and all all other scopes (local scopes). So
352 we need to select the right one to pass to add_symbol_to_list().
353 We do it by keeping a pointer to the correct list in list_in_scope.
354
355 FIXME: The original dwarf code just treated the file scope as the
356 first local scope, and all other local scopes as nested local
357 scopes, and worked fine. Check to see if we really need to
358 distinguish these in buildsym.c. */
359 struct pending **list_in_scope;
360
361 /* DWARF abbreviation table associated with this compilation unit. */
362 struct abbrev_info **dwarf2_abbrevs;
363
364 /* Storage for the abbrev table. */
365 struct obstack abbrev_obstack;
366
367 /* Hash table holding all the loaded partial DIEs
368 with partial_die->offset.SECT_OFF as hash. */
369 htab_t partial_dies;
370
371 /* Storage for things with the same lifetime as this read-in compilation
372 unit, including partial DIEs. */
373 struct obstack comp_unit_obstack;
374
375 /* When multiple dwarf2_cu structures are living in memory, this field
376 chains them all together, so that they can be released efficiently.
377 We will probably also want a generation counter so that most-recently-used
378 compilation units are cached... */
379 struct dwarf2_per_cu_data *read_in_chain;
380
381 /* Backchain to our per_cu entry if the tree has been built. */
382 struct dwarf2_per_cu_data *per_cu;
383
384 /* How many compilation units ago was this CU last referenced? */
385 int last_used;
386
387 /* A hash table of DIE cu_offset for following references with
388 die_info->offset.sect_off as hash. */
389 htab_t die_hash;
390
391 /* Full DIEs if read in. */
392 struct die_info *dies;
393
394 /* A set of pointers to dwarf2_per_cu_data objects for compilation
395 units referenced by this one. Only set during full symbol processing;
396 partial symbol tables do not have dependencies. */
397 htab_t dependencies;
398
399 /* Header data from the line table, during full symbol processing. */
400 struct line_header *line_header;
401
402 /* A list of methods which need to have physnames computed
403 after all type information has been read. */
404 VEC (delayed_method_info) *method_list;
405
406 /* To be copied to symtab->call_site_htab. */
407 htab_t call_site_htab;
408
409 /* Non-NULL if this CU came from a DWO file. */
410 struct dwo_unit *dwo_unit;
411
412 /* The DW_AT_addr_base attribute if present, zero otherwise
413 (zero is a valid value though).
414 Note this value comes from the stub CU/TU's DIE. */
415 ULONGEST addr_base;
416
417 /* Mark used when releasing cached dies. */
418 unsigned int mark : 1;
419
420 /* This CU references .debug_loc. See the symtab->locations_valid field.
421 This test is imperfect as there may exist optimized debug code not using
422 any location list and still facing inlining issues if handled as
423 unoptimized code. For a future better test see GCC PR other/32998. */
424 unsigned int has_loclist : 1;
425
426 /* These cache the results of producer_is_gxx_lt_4_6.
427 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
428 information is cached because profiling CU expansion showed
429 excessive time spent in producer_is_gxx_lt_4_6. */
430 unsigned int checked_producer : 1;
431 unsigned int producer_is_gxx_lt_4_6 : 1;
432
433 /* Non-zero if DW_AT_addr_base was found.
434 Used when processing DWO files. */
435 unsigned int have_addr_base : 1;
436 };
437
438 /* Persistent data held for a compilation unit, even when not
439 processing it. We put a pointer to this structure in the
440 read_symtab_private field of the psymtab. */
441
442 struct dwarf2_per_cu_data
443 {
444 /* The start offset and length of this compilation unit. 2**29-1
445 bytes should suffice to store the length of any compilation unit
446 - if it doesn't, GDB will fall over anyway.
447 NOTE: Unlike comp_unit_head.length, this length includes
448 initial_length_size.
449 If the DIE refers to a DWO file, this is always of the original die,
450 not the DWO file. */
451 sect_offset offset;
452 unsigned int length : 29;
453
454 /* Flag indicating this compilation unit will be read in before
455 any of the current compilation units are processed. */
456 unsigned int queued : 1;
457
458 /* This flag will be set when reading partial DIEs if we need to load
459 absolutely all DIEs for this compilation unit, instead of just the ones
460 we think are interesting. It gets set if we look for a DIE in the
461 hash table and don't find it. */
462 unsigned int load_all_dies : 1;
463
464 /* Non-zero if this CU is from .debug_types. */
465 unsigned int is_debug_types : 1;
466
467 /* The section this CU/TU lives in.
468 If the DIE refers to a DWO file, this is always the original die,
469 not the DWO file. */
470 struct dwarf2_section_info *info_or_types_section;
471
472 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
473 of the CU cache it gets reset to NULL again. */
474 struct dwarf2_cu *cu;
475
476 /* The corresponding objfile.
477 Normally we can get the objfile from dwarf2_per_objfile.
478 However we can enter this file with just a "per_cu" handle. */
479 struct objfile *objfile;
480
481 /* When using partial symbol tables, the 'psymtab' field is active.
482 Otherwise the 'quick' field is active. */
483 union
484 {
485 /* The partial symbol table associated with this compilation unit,
486 or NULL for partial units (which do not have an associated
487 symtab). */
488 struct partial_symtab *psymtab;
489
490 /* Data needed by the "quick" functions. */
491 struct dwarf2_per_cu_quick_data *quick;
492 } v;
493 };
494
495 /* Entry in the signatured_types hash table. */
496
497 struct signatured_type
498 {
499 /* The type's signature. */
500 ULONGEST signature;
501
502 /* Offset in the TU of the type's DIE, as read from the TU header.
503 If the definition lives in a DWO file, this value is unusable. */
504 cu_offset type_offset_in_tu;
505
506 /* Offset in the section of the type's DIE.
507 If the definition lives in a DWO file, this is the offset in the
508 .debug_types.dwo section.
509 The value is zero until the actual value is known.
510 Zero is otherwise not a valid section offset. */
511 sect_offset type_offset_in_section;
512
513 /* The CU(/TU) of this type. */
514 struct dwarf2_per_cu_data per_cu;
515 };
516
517 /* These sections are what may appear in a "dwo" file. */
518
519 struct dwo_sections
520 {
521 struct dwarf2_section_info abbrev;
522 struct dwarf2_section_info info;
523 struct dwarf2_section_info line;
524 struct dwarf2_section_info loc;
525 struct dwarf2_section_info str;
526 struct dwarf2_section_info str_offsets;
527 VEC (dwarf2_section_info_def) *types;
528 };
529
530 /* Common bits of DWO CUs/TUs. */
531
532 struct dwo_unit
533 {
534 /* Backlink to the containing struct dwo_file. */
535 struct dwo_file *dwo_file;
536
537 /* The "id" that distinguishes this CU/TU.
538 .debug_info calls this "dwo_id", .debug_types calls this "signature".
539 Since signatures came first, we stick with it for consistency. */
540 ULONGEST signature;
541
542 /* The section this CU/TU lives in, in the DWO file. */
543 struct dwarf2_section_info *info_or_types_section;
544
545 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
546 sect_offset offset;
547 unsigned int length;
548
549 /* For types, offset in the type's DIE of the type defined by this TU. */
550 cu_offset type_offset_in_tu;
551 };
552
553 /* Data for one DWO file. */
554
555 struct dwo_file
556 {
557 /* The DW_AT_GNU_dwo_name attribute.
558 We don't manage space for this, it's an attribute. */
559 const char *dwo_name;
560
561 /* The bfd, when the file is open. Otherwise this is NULL. */
562 bfd *dwo_bfd;
563
564 /* Section info for this file. */
565 struct dwo_sections sections;
566
567 /* Table of CUs in the file.
568 Each element is a struct dwo_unit. */
569 htab_t cus;
570
571 /* Table of TUs in the file.
572 Each element is a struct dwo_unit. */
573 htab_t tus;
574 };
575
576 /* Struct used to pass misc. parameters to read_die_and_children, et
577 al. which are used for both .debug_info and .debug_types dies.
578 All parameters here are unchanging for the life of the call. This
579 struct exists to abstract away the constant parameters of die reading. */
580
581 struct die_reader_specs
582 {
583 /* die_section->asection->owner. */
584 bfd* abfd;
585
586 /* The CU of the DIE we are parsing. */
587 struct dwarf2_cu *cu;
588
589 /* Non-NULL if reading a DWO file. */
590 struct dwo_file *dwo_file;
591
592 /* The section the die comes from.
593 This is either .debug_info or .debug_types, or the .dwo variants. */
594 struct dwarf2_section_info *die_section;
595
596 /* die_section->buffer. */
597 gdb_byte *buffer;
598 };
599
600 /* Type of function passed to init_cutu_and_read_dies, et.al. */
601 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
602 gdb_byte *info_ptr,
603 struct die_info *comp_unit_die,
604 int has_children,
605 void *data);
606
607 /* The line number information for a compilation unit (found in the
608 .debug_line section) begins with a "statement program header",
609 which contains the following information. */
610 struct line_header
611 {
612 unsigned int total_length;
613 unsigned short version;
614 unsigned int header_length;
615 unsigned char minimum_instruction_length;
616 unsigned char maximum_ops_per_instruction;
617 unsigned char default_is_stmt;
618 int line_base;
619 unsigned char line_range;
620 unsigned char opcode_base;
621
622 /* standard_opcode_lengths[i] is the number of operands for the
623 standard opcode whose value is i. This means that
624 standard_opcode_lengths[0] is unused, and the last meaningful
625 element is standard_opcode_lengths[opcode_base - 1]. */
626 unsigned char *standard_opcode_lengths;
627
628 /* The include_directories table. NOTE! These strings are not
629 allocated with xmalloc; instead, they are pointers into
630 debug_line_buffer. If you try to free them, `free' will get
631 indigestion. */
632 unsigned int num_include_dirs, include_dirs_size;
633 char **include_dirs;
634
635 /* The file_names table. NOTE! These strings are not allocated
636 with xmalloc; instead, they are pointers into debug_line_buffer.
637 Don't try to free them directly. */
638 unsigned int num_file_names, file_names_size;
639 struct file_entry
640 {
641 char *name;
642 unsigned int dir_index;
643 unsigned int mod_time;
644 unsigned int length;
645 int included_p; /* Non-zero if referenced by the Line Number Program. */
646 struct symtab *symtab; /* The associated symbol table, if any. */
647 } *file_names;
648
649 /* The start and end of the statement program following this
650 header. These point into dwarf2_per_objfile->line_buffer. */
651 gdb_byte *statement_program_start, *statement_program_end;
652 };
653
654 /* When we construct a partial symbol table entry we only
655 need this much information. */
656 struct partial_die_info
657 {
658 /* Offset of this DIE. */
659 sect_offset offset;
660
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Assorted flags describing the data found in this DIE. */
665 unsigned int has_children : 1;
666 unsigned int is_external : 1;
667 unsigned int is_declaration : 1;
668 unsigned int has_type : 1;
669 unsigned int has_specification : 1;
670 unsigned int has_pc_info : 1;
671 unsigned int may_be_inlined : 1;
672
673 /* Flag set if the SCOPE field of this structure has been
674 computed. */
675 unsigned int scope_set : 1;
676
677 /* Flag set if the DIE has a byte_size attribute. */
678 unsigned int has_byte_size : 1;
679
680 /* Flag set if any of the DIE's children are template arguments. */
681 unsigned int has_template_arguments : 1;
682
683 /* Flag set if fixup_partial_die has been called on this die. */
684 unsigned int fixup_called : 1;
685
686 /* The name of this DIE. Normally the value of DW_AT_name, but
687 sometimes a default name for unnamed DIEs. */
688 char *name;
689
690 /* The linkage name, if present. */
691 const char *linkage_name;
692
693 /* The scope to prepend to our children. This is generally
694 allocated on the comp_unit_obstack, so will disappear
695 when this compilation unit leaves the cache. */
696 char *scope;
697
698 /* The location description associated with this DIE, if any. */
699 struct dwarf_block *locdesc;
700
701 /* If HAS_PC_INFO, the PC range associated with this DIE. */
702 CORE_ADDR lowpc;
703 CORE_ADDR highpc;
704
705 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
706 DW_AT_sibling, if any. */
707 /* NOTE: This member isn't strictly necessary, read_partial_die could
708 return DW_AT_sibling values to its caller load_partial_dies. */
709 gdb_byte *sibling;
710
711 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
712 DW_AT_specification (or DW_AT_abstract_origin or
713 DW_AT_extension). */
714 sect_offset spec_offset;
715
716 /* Pointers to this DIE's parent, first child, and next sibling,
717 if any. */
718 struct partial_die_info *die_parent, *die_child, *die_sibling;
719 };
720
721 /* This data structure holds the information of an abbrev. */
722 struct abbrev_info
723 {
724 unsigned int number; /* number identifying abbrev */
725 enum dwarf_tag tag; /* dwarf tag */
726 unsigned short has_children; /* boolean */
727 unsigned short num_attrs; /* number of attributes */
728 struct attr_abbrev *attrs; /* an array of attribute descriptions */
729 struct abbrev_info *next; /* next in chain */
730 };
731
732 struct attr_abbrev
733 {
734 ENUM_BITFIELD(dwarf_attribute) name : 16;
735 ENUM_BITFIELD(dwarf_form) form : 16;
736 };
737
738 /* Attributes have a name and a value. */
739 struct attribute
740 {
741 ENUM_BITFIELD(dwarf_attribute) name : 16;
742 ENUM_BITFIELD(dwarf_form) form : 15;
743
744 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
745 field should be in u.str (existing only for DW_STRING) but it is kept
746 here for better struct attribute alignment. */
747 unsigned int string_is_canonical : 1;
748
749 union
750 {
751 char *str;
752 struct dwarf_block *blk;
753 ULONGEST unsnd;
754 LONGEST snd;
755 CORE_ADDR addr;
756 struct signatured_type *signatured_type;
757 }
758 u;
759 };
760
761 /* This data structure holds a complete die structure. */
762 struct die_info
763 {
764 /* DWARF-2 tag for this DIE. */
765 ENUM_BITFIELD(dwarf_tag) tag : 16;
766
767 /* Number of attributes */
768 unsigned char num_attrs;
769
770 /* True if we're presently building the full type name for the
771 type derived from this DIE. */
772 unsigned char building_fullname : 1;
773
774 /* Abbrev number */
775 unsigned int abbrev;
776
777 /* Offset in .debug_info or .debug_types section. */
778 sect_offset offset;
779
780 /* The dies in a compilation unit form an n-ary tree. PARENT
781 points to this die's parent; CHILD points to the first child of
782 this node; and all the children of a given node are chained
783 together via their SIBLING fields. */
784 struct die_info *child; /* Its first child, if any. */
785 struct die_info *sibling; /* Its next sibling, if any. */
786 struct die_info *parent; /* Its parent, if any. */
787
788 /* An array of attributes, with NUM_ATTRS elements. There may be
789 zero, but it's not common and zero-sized arrays are not
790 sufficiently portable C. */
791 struct attribute attrs[1];
792 };
793
794 /* Get at parts of an attribute structure. */
795
796 #define DW_STRING(attr) ((attr)->u.str)
797 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
798 #define DW_UNSND(attr) ((attr)->u.unsnd)
799 #define DW_BLOCK(attr) ((attr)->u.blk)
800 #define DW_SND(attr) ((attr)->u.snd)
801 #define DW_ADDR(attr) ((attr)->u.addr)
802 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
803
804 /* Blocks are a bunch of untyped bytes. */
805 struct dwarf_block
806 {
807 unsigned int size;
808
809 /* Valid only if SIZE is not zero. */
810 gdb_byte *data;
811 };
812
813 #ifndef ATTR_ALLOC_CHUNK
814 #define ATTR_ALLOC_CHUNK 4
815 #endif
816
817 /* Allocate fields for structs, unions and enums in this size. */
818 #ifndef DW_FIELD_ALLOC_CHUNK
819 #define DW_FIELD_ALLOC_CHUNK 4
820 #endif
821
822 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
823 but this would require a corresponding change in unpack_field_as_long
824 and friends. */
825 static int bits_per_byte = 8;
826
827 /* The routines that read and process dies for a C struct or C++ class
828 pass lists of data member fields and lists of member function fields
829 in an instance of a field_info structure, as defined below. */
830 struct field_info
831 {
832 /* List of data member and baseclasses fields. */
833 struct nextfield
834 {
835 struct nextfield *next;
836 int accessibility;
837 int virtuality;
838 struct field field;
839 }
840 *fields, *baseclasses;
841
842 /* Number of fields (including baseclasses). */
843 int nfields;
844
845 /* Number of baseclasses. */
846 int nbaseclasses;
847
848 /* Set if the accesibility of one of the fields is not public. */
849 int non_public_fields;
850
851 /* Member function fields array, entries are allocated in the order they
852 are encountered in the object file. */
853 struct nextfnfield
854 {
855 struct nextfnfield *next;
856 struct fn_field fnfield;
857 }
858 *fnfields;
859
860 /* Member function fieldlist array, contains name of possibly overloaded
861 member function, number of overloaded member functions and a pointer
862 to the head of the member function field chain. */
863 struct fnfieldlist
864 {
865 char *name;
866 int length;
867 struct nextfnfield *head;
868 }
869 *fnfieldlists;
870
871 /* Number of entries in the fnfieldlists array. */
872 int nfnfields;
873
874 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
875 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
876 struct typedef_field_list
877 {
878 struct typedef_field field;
879 struct typedef_field_list *next;
880 }
881 *typedef_field_list;
882 unsigned typedef_field_list_count;
883 };
884
885 /* One item on the queue of compilation units to read in full symbols
886 for. */
887 struct dwarf2_queue_item
888 {
889 struct dwarf2_per_cu_data *per_cu;
890 struct dwarf2_queue_item *next;
891 };
892
893 /* The current queue. */
894 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
895
896 /* Loaded secondary compilation units are kept in memory until they
897 have not been referenced for the processing of this many
898 compilation units. Set this to zero to disable caching. Cache
899 sizes of up to at least twenty will improve startup time for
900 typical inter-CU-reference binaries, at an obvious memory cost. */
901 static int dwarf2_max_cache_age = 5;
902 static void
903 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
904 struct cmd_list_element *c, const char *value)
905 {
906 fprintf_filtered (file, _("The upper bound on the age of cached "
907 "dwarf2 compilation units is %s.\n"),
908 value);
909 }
910
911
912 /* Various complaints about symbol reading that don't abort the process. */
913
914 static void
915 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
916 {
917 complaint (&symfile_complaints,
918 _("statement list doesn't fit in .debug_line section"));
919 }
920
921 static void
922 dwarf2_debug_line_missing_file_complaint (void)
923 {
924 complaint (&symfile_complaints,
925 _(".debug_line section has line data without a file"));
926 }
927
928 static void
929 dwarf2_debug_line_missing_end_sequence_complaint (void)
930 {
931 complaint (&symfile_complaints,
932 _(".debug_line section has line "
933 "program sequence without an end"));
934 }
935
936 static void
937 dwarf2_complex_location_expr_complaint (void)
938 {
939 complaint (&symfile_complaints, _("location expression too complex"));
940 }
941
942 static void
943 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
944 int arg3)
945 {
946 complaint (&symfile_complaints,
947 _("const value length mismatch for '%s', got %d, expected %d"),
948 arg1, arg2, arg3);
949 }
950
951 static void
952 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
953 {
954 complaint (&symfile_complaints,
955 _("macro info runs off end of `%s' section"),
956 section->asection->name);
957 }
958
959 static void
960 dwarf2_macro_malformed_definition_complaint (const char *arg1)
961 {
962 complaint (&symfile_complaints,
963 _("macro debug info contains a "
964 "malformed macro definition:\n`%s'"),
965 arg1);
966 }
967
968 static void
969 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
970 {
971 complaint (&symfile_complaints,
972 _("invalid attribute class or form for '%s' in '%s'"),
973 arg1, arg2);
974 }
975
976 /* local function prototypes */
977
978 static void dwarf2_locate_sections (bfd *, asection *, void *);
979
980 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
981 struct objfile *);
982
983 static void dwarf2_find_base_address (struct die_info *die,
984 struct dwarf2_cu *cu);
985
986 static void dwarf2_build_psymtabs_hard (struct objfile *);
987
988 static void scan_partial_symbols (struct partial_die_info *,
989 CORE_ADDR *, CORE_ADDR *,
990 int, struct dwarf2_cu *);
991
992 static void add_partial_symbol (struct partial_die_info *,
993 struct dwarf2_cu *);
994
995 static void add_partial_namespace (struct partial_die_info *pdi,
996 CORE_ADDR *lowpc, CORE_ADDR *highpc,
997 int need_pc, struct dwarf2_cu *cu);
998
999 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1000 CORE_ADDR *highpc, int need_pc,
1001 struct dwarf2_cu *cu);
1002
1003 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1004 struct dwarf2_cu *cu);
1005
1006 static void add_partial_subprogram (struct partial_die_info *pdi,
1007 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1008 int need_pc, struct dwarf2_cu *cu);
1009
1010 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1011
1012 static void psymtab_to_symtab_1 (struct partial_symtab *);
1013
1014 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1015 struct dwarf2_section_info *);
1016
1017 static void dwarf2_free_abbrev_table (void *);
1018
1019 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1020
1021 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1022 struct dwarf2_cu *);
1023
1024 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1025 struct dwarf2_cu *);
1026
1027 static struct partial_die_info *load_partial_dies
1028 (const struct die_reader_specs *, gdb_byte *, int);
1029
1030 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1031 struct partial_die_info *,
1032 struct abbrev_info *,
1033 unsigned int,
1034 gdb_byte *);
1035
1036 static struct partial_die_info *find_partial_die (sect_offset,
1037 struct dwarf2_cu *);
1038
1039 static void fixup_partial_die (struct partial_die_info *,
1040 struct dwarf2_cu *);
1041
1042 static gdb_byte *read_attribute (const struct die_reader_specs *,
1043 struct attribute *, struct attr_abbrev *,
1044 gdb_byte *);
1045
1046 static unsigned int read_1_byte (bfd *, gdb_byte *);
1047
1048 static int read_1_signed_byte (bfd *, gdb_byte *);
1049
1050 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1051
1052 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1053
1054 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1055
1056 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1057 unsigned int *);
1058
1059 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1060
1061 static LONGEST read_checked_initial_length_and_offset
1062 (bfd *, gdb_byte *, const struct comp_unit_head *,
1063 unsigned int *, unsigned int *);
1064
1065 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1066 unsigned int *);
1067
1068 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1069
1070 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1071
1072 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1073
1074 static char *read_indirect_string (bfd *, gdb_byte *,
1075 const struct comp_unit_head *,
1076 unsigned int *);
1077
1078 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1079
1080 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1081
1082 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1083 unsigned int *);
1084
1085 static char *read_str_index (const struct die_reader_specs *reader,
1086 struct dwarf2_cu *cu, ULONGEST str_index);
1087
1088 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1089
1090 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1091
1092 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1093 struct dwarf2_cu *);
1094
1095 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1096 unsigned int,
1097 struct dwarf2_cu *);
1098
1099 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1100 struct dwarf2_cu *cu);
1101
1102 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1103
1104 static struct die_info *die_specification (struct die_info *die,
1105 struct dwarf2_cu **);
1106
1107 static void free_line_header (struct line_header *lh);
1108
1109 static void add_file_name (struct line_header *, char *, unsigned int,
1110 unsigned int, unsigned int);
1111
1112 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1113 struct dwarf2_cu *cu);
1114
1115 static void dwarf_decode_lines (struct line_header *, const char *,
1116 struct dwarf2_cu *, struct partial_symtab *,
1117 int);
1118
1119 static void dwarf2_start_subfile (char *, const char *, const char *);
1120
1121 static struct symbol *new_symbol (struct die_info *, struct type *,
1122 struct dwarf2_cu *);
1123
1124 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1125 struct dwarf2_cu *, struct symbol *);
1126
1127 static void dwarf2_const_value (struct attribute *, struct symbol *,
1128 struct dwarf2_cu *);
1129
1130 static void dwarf2_const_value_attr (struct attribute *attr,
1131 struct type *type,
1132 const char *name,
1133 struct obstack *obstack,
1134 struct dwarf2_cu *cu, LONGEST *value,
1135 gdb_byte **bytes,
1136 struct dwarf2_locexpr_baton **baton);
1137
1138 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1139
1140 static int need_gnat_info (struct dwarf2_cu *);
1141
1142 static struct type *die_descriptive_type (struct die_info *,
1143 struct dwarf2_cu *);
1144
1145 static void set_descriptive_type (struct type *, struct die_info *,
1146 struct dwarf2_cu *);
1147
1148 static struct type *die_containing_type (struct die_info *,
1149 struct dwarf2_cu *);
1150
1151 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1152 struct dwarf2_cu *);
1153
1154 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1155
1156 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1157
1158 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1159
1160 static char *typename_concat (struct obstack *obs, const char *prefix,
1161 const char *suffix, int physname,
1162 struct dwarf2_cu *cu);
1163
1164 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1165
1166 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1167
1168 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1169
1170 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1171
1172 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1173
1174 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1175 struct dwarf2_cu *, struct partial_symtab *);
1176
1177 static int dwarf2_get_pc_bounds (struct die_info *,
1178 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1179 struct partial_symtab *);
1180
1181 static void get_scope_pc_bounds (struct die_info *,
1182 CORE_ADDR *, CORE_ADDR *,
1183 struct dwarf2_cu *);
1184
1185 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1186 CORE_ADDR, struct dwarf2_cu *);
1187
1188 static void dwarf2_add_field (struct field_info *, struct die_info *,
1189 struct dwarf2_cu *);
1190
1191 static void dwarf2_attach_fields_to_type (struct field_info *,
1192 struct type *, struct dwarf2_cu *);
1193
1194 static void dwarf2_add_member_fn (struct field_info *,
1195 struct die_info *, struct type *,
1196 struct dwarf2_cu *);
1197
1198 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1199 struct type *,
1200 struct dwarf2_cu *);
1201
1202 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1203
1204 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1205
1206 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1207
1208 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1209
1210 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1211
1212 static struct type *read_module_type (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
1215 static const char *namespace_name (struct die_info *die,
1216 int *is_anonymous, struct dwarf2_cu *);
1217
1218 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1219
1220 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1221
1222 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1223 struct dwarf2_cu *);
1224
1225 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1226 gdb_byte *info_ptr,
1227 gdb_byte **new_info_ptr,
1228 struct die_info *parent);
1229
1230 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1231 gdb_byte *info_ptr,
1232 gdb_byte **new_info_ptr,
1233 struct die_info *parent);
1234
1235 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1236 struct die_info **, gdb_byte *, int *, int);
1237
1238 static gdb_byte *read_full_die (const struct die_reader_specs *,
1239 struct die_info **, gdb_byte *, int *);
1240
1241 static void process_die (struct die_info *, struct dwarf2_cu *);
1242
1243 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1244 struct obstack *);
1245
1246 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1247
1248 static const char *dwarf2_full_name (char *name,
1249 struct die_info *die,
1250 struct dwarf2_cu *cu);
1251
1252 static struct die_info *dwarf2_extension (struct die_info *die,
1253 struct dwarf2_cu **);
1254
1255 static const char *dwarf_tag_name (unsigned int);
1256
1257 static const char *dwarf_attr_name (unsigned int);
1258
1259 static const char *dwarf_form_name (unsigned int);
1260
1261 static char *dwarf_bool_name (unsigned int);
1262
1263 static const char *dwarf_type_encoding_name (unsigned int);
1264
1265 static struct die_info *sibling_die (struct die_info *);
1266
1267 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1268
1269 static void dump_die_for_error (struct die_info *);
1270
1271 static void dump_die_1 (struct ui_file *, int level, int max_level,
1272 struct die_info *);
1273
1274 /*static*/ void dump_die (struct die_info *, int max_level);
1275
1276 static void store_in_ref_table (struct die_info *,
1277 struct dwarf2_cu *);
1278
1279 static int is_ref_attr (struct attribute *);
1280
1281 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1282
1283 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1284
1285 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1286 struct attribute *,
1287 struct dwarf2_cu **);
1288
1289 static struct die_info *follow_die_ref (struct die_info *,
1290 struct attribute *,
1291 struct dwarf2_cu **);
1292
1293 static struct die_info *follow_die_sig (struct die_info *,
1294 struct attribute *,
1295 struct dwarf2_cu **);
1296
1297 static struct signatured_type *lookup_signatured_type_at_offset
1298 (struct objfile *objfile,
1299 struct dwarf2_section_info *section, sect_offset offset);
1300
1301 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1302
1303 static void read_signatured_type (struct signatured_type *);
1304
1305 /* memory allocation interface */
1306
1307 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1308
1309 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1310
1311 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1312
1313 static void dwarf_decode_macros (struct line_header *, unsigned int,
1314 char *, bfd *, struct dwarf2_cu *,
1315 struct dwarf2_section_info *,
1316 int, const char *);
1317
1318 static int attr_form_is_block (struct attribute *);
1319
1320 static int attr_form_is_section_offset (struct attribute *);
1321
1322 static int attr_form_is_constant (struct attribute *);
1323
1324 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1325 struct dwarf2_loclist_baton *baton,
1326 struct attribute *attr);
1327
1328 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1329 struct symbol *sym,
1330 struct dwarf2_cu *cu);
1331
1332 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1333 gdb_byte *info_ptr,
1334 struct abbrev_info *abbrev);
1335
1336 static void free_stack_comp_unit (void *);
1337
1338 static hashval_t partial_die_hash (const void *item);
1339
1340 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1341
1342 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1343 (sect_offset offset, struct objfile *objfile);
1344
1345 static void init_one_comp_unit (struct dwarf2_cu *cu,
1346 struct dwarf2_per_cu_data *per_cu);
1347
1348 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1349 struct die_info *comp_unit_die);
1350
1351 static void free_heap_comp_unit (void *);
1352
1353 static void free_cached_comp_units (void *);
1354
1355 static void age_cached_comp_units (void);
1356
1357 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1358
1359 static struct type *set_die_type (struct die_info *, struct type *,
1360 struct dwarf2_cu *);
1361
1362 static void create_all_comp_units (struct objfile *);
1363
1364 static int create_all_type_units (struct objfile *);
1365
1366 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1367
1368 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1369
1370 static void dwarf2_add_dependence (struct dwarf2_cu *,
1371 struct dwarf2_per_cu_data *);
1372
1373 static void dwarf2_mark (struct dwarf2_cu *);
1374
1375 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1376
1377 static struct type *get_die_type_at_offset (sect_offset,
1378 struct dwarf2_per_cu_data *per_cu);
1379
1380 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1381
1382 static void dwarf2_release_queue (void *dummy);
1383
1384 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1385
1386 static void process_queue (void);
1387
1388 static void find_file_and_directory (struct die_info *die,
1389 struct dwarf2_cu *cu,
1390 char **name, char **comp_dir);
1391
1392 static char *file_full_name (int file, struct line_header *lh,
1393 const char *comp_dir);
1394
1395 static gdb_byte *read_and_check_comp_unit_head
1396 (struct comp_unit_head *header,
1397 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1398 int is_debug_types_section);
1399
1400 static void init_cutu_and_read_dies
1401 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1402 die_reader_func_ftype *die_reader_func, void *data);
1403
1404 static void init_cutu_and_read_dies_simple
1405 (struct dwarf2_per_cu_data *this_cu,
1406 die_reader_func_ftype *die_reader_func, void *data);
1407
1408 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1409
1410 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *);
1411
1412 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1413
1414 static struct dwo_unit *lookup_dwo_comp_unit
1415 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1416
1417 static struct dwo_unit *lookup_dwo_type_unit
1418 (struct signatured_type *, char *, const char *);
1419
1420 static void free_dwo_file_cleanup (void *);
1421
1422 static void munmap_section_buffer (struct dwarf2_section_info *);
1423
1424 #if WORDS_BIGENDIAN
1425
1426 /* Convert VALUE between big- and little-endian. */
1427 static offset_type
1428 byte_swap (offset_type value)
1429 {
1430 offset_type result;
1431
1432 result = (value & 0xff) << 24;
1433 result |= (value & 0xff00) << 8;
1434 result |= (value & 0xff0000) >> 8;
1435 result |= (value & 0xff000000) >> 24;
1436 return result;
1437 }
1438
1439 #define MAYBE_SWAP(V) byte_swap (V)
1440
1441 #else
1442 #define MAYBE_SWAP(V) (V)
1443 #endif /* WORDS_BIGENDIAN */
1444
1445 /* The suffix for an index file. */
1446 #define INDEX_SUFFIX ".gdb-index"
1447
1448 static const char *dwarf2_physname (char *name, struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
1451 /* Try to locate the sections we need for DWARF 2 debugging
1452 information and return true if we have enough to do something.
1453 NAMES points to the dwarf2 section names, or is NULL if the standard
1454 ELF names are used. */
1455
1456 int
1457 dwarf2_has_info (struct objfile *objfile,
1458 const struct dwarf2_debug_sections *names)
1459 {
1460 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1461 if (!dwarf2_per_objfile)
1462 {
1463 /* Initialize per-objfile state. */
1464 struct dwarf2_per_objfile *data
1465 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1466
1467 memset (data, 0, sizeof (*data));
1468 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1469 dwarf2_per_objfile = data;
1470
1471 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1472 (void *) names);
1473 dwarf2_per_objfile->objfile = objfile;
1474 }
1475 return (dwarf2_per_objfile->info.asection != NULL
1476 && dwarf2_per_objfile->abbrev.asection != NULL);
1477 }
1478
1479 /* When loading sections, we look either for uncompressed section or for
1480 compressed section names. */
1481
1482 static int
1483 section_is_p (const char *section_name,
1484 const struct dwarf2_section_names *names)
1485 {
1486 if (names->normal != NULL
1487 && strcmp (section_name, names->normal) == 0)
1488 return 1;
1489 if (names->compressed != NULL
1490 && strcmp (section_name, names->compressed) == 0)
1491 return 1;
1492 return 0;
1493 }
1494
1495 /* This function is mapped across the sections and remembers the
1496 offset and size of each of the debugging sections we are interested
1497 in. */
1498
1499 static void
1500 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1501 {
1502 const struct dwarf2_debug_sections *names;
1503
1504 if (vnames == NULL)
1505 names = &dwarf2_elf_names;
1506 else
1507 names = (const struct dwarf2_debug_sections *) vnames;
1508
1509 if (section_is_p (sectp->name, &names->info))
1510 {
1511 dwarf2_per_objfile->info.asection = sectp;
1512 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1513 }
1514 else if (section_is_p (sectp->name, &names->abbrev))
1515 {
1516 dwarf2_per_objfile->abbrev.asection = sectp;
1517 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1518 }
1519 else if (section_is_p (sectp->name, &names->line))
1520 {
1521 dwarf2_per_objfile->line.asection = sectp;
1522 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1523 }
1524 else if (section_is_p (sectp->name, &names->loc))
1525 {
1526 dwarf2_per_objfile->loc.asection = sectp;
1527 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1528 }
1529 else if (section_is_p (sectp->name, &names->macinfo))
1530 {
1531 dwarf2_per_objfile->macinfo.asection = sectp;
1532 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1533 }
1534 else if (section_is_p (sectp->name, &names->macro))
1535 {
1536 dwarf2_per_objfile->macro.asection = sectp;
1537 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1538 }
1539 else if (section_is_p (sectp->name, &names->str))
1540 {
1541 dwarf2_per_objfile->str.asection = sectp;
1542 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1543 }
1544 else if (section_is_p (sectp->name, &names->addr))
1545 {
1546 dwarf2_per_objfile->addr.asection = sectp;
1547 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1548 }
1549 else if (section_is_p (sectp->name, &names->frame))
1550 {
1551 dwarf2_per_objfile->frame.asection = sectp;
1552 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1553 }
1554 else if (section_is_p (sectp->name, &names->eh_frame))
1555 {
1556 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1557
1558 if (aflag & SEC_HAS_CONTENTS)
1559 {
1560 dwarf2_per_objfile->eh_frame.asection = sectp;
1561 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1562 }
1563 }
1564 else if (section_is_p (sectp->name, &names->ranges))
1565 {
1566 dwarf2_per_objfile->ranges.asection = sectp;
1567 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1568 }
1569 else if (section_is_p (sectp->name, &names->types))
1570 {
1571 struct dwarf2_section_info type_section;
1572
1573 memset (&type_section, 0, sizeof (type_section));
1574 type_section.asection = sectp;
1575 type_section.size = bfd_get_section_size (sectp);
1576
1577 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1578 &type_section);
1579 }
1580 else if (section_is_p (sectp->name, &names->gdb_index))
1581 {
1582 dwarf2_per_objfile->gdb_index.asection = sectp;
1583 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1584 }
1585
1586 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1587 && bfd_section_vma (abfd, sectp) == 0)
1588 dwarf2_per_objfile->has_section_at_zero = 1;
1589 }
1590
1591 /* Decompress a section that was compressed using zlib. Store the
1592 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1593
1594 static void
1595 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1596 gdb_byte **outbuf, bfd_size_type *outsize)
1597 {
1598 bfd *abfd = sectp->owner;
1599 #ifndef HAVE_ZLIB_H
1600 error (_("Support for zlib-compressed DWARF data (from '%s') "
1601 "is disabled in this copy of GDB"),
1602 bfd_get_filename (abfd));
1603 #else
1604 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1605 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1606 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1607 bfd_size_type uncompressed_size;
1608 gdb_byte *uncompressed_buffer;
1609 z_stream strm;
1610 int rc;
1611 int header_size = 12;
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614 || bfd_bread (compressed_buffer,
1615 compressed_size, abfd) != compressed_size)
1616 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1617 bfd_get_filename (abfd));
1618
1619 /* Read the zlib header. In this case, it should be "ZLIB" followed
1620 by the uncompressed section size, 8 bytes in big-endian order. */
1621 if (compressed_size < header_size
1622 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1623 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1624 bfd_get_filename (abfd));
1625 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1626 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1627 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1628 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1629 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1630 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1631 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1632 uncompressed_size += compressed_buffer[11];
1633
1634 /* It is possible the section consists of several compressed
1635 buffers concatenated together, so we uncompress in a loop. */
1636 strm.zalloc = NULL;
1637 strm.zfree = NULL;
1638 strm.opaque = NULL;
1639 strm.avail_in = compressed_size - header_size;
1640 strm.next_in = (Bytef*) compressed_buffer + header_size;
1641 strm.avail_out = uncompressed_size;
1642 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1643 uncompressed_size);
1644 rc = inflateInit (&strm);
1645 while (strm.avail_in > 0)
1646 {
1647 if (rc != Z_OK)
1648 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1649 bfd_get_filename (abfd), rc);
1650 strm.next_out = ((Bytef*) uncompressed_buffer
1651 + (uncompressed_size - strm.avail_out));
1652 rc = inflate (&strm, Z_FINISH);
1653 if (rc != Z_STREAM_END)
1654 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1655 bfd_get_filename (abfd), rc);
1656 rc = inflateReset (&strm);
1657 }
1658 rc = inflateEnd (&strm);
1659 if (rc != Z_OK
1660 || strm.avail_out != 0)
1661 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1662 bfd_get_filename (abfd), rc);
1663
1664 do_cleanups (cleanup);
1665 *outbuf = uncompressed_buffer;
1666 *outsize = uncompressed_size;
1667 #endif
1668 }
1669
1670 /* A helper function that decides whether a section is empty,
1671 or not present. */
1672
1673 static int
1674 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1675 {
1676 return info->asection == NULL || info->size == 0;
1677 }
1678
1679 /* Read the contents of the section INFO.
1680 OBJFILE is the main object file, but not necessarily the file where
1681 the section comes from. E.g., for DWO files INFO->asection->owner
1682 is the bfd of the DWO file.
1683 If the section is compressed, uncompress it before returning. */
1684
1685 static void
1686 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1687 {
1688 asection *sectp = info->asection;
1689 bfd *abfd;
1690 gdb_byte *buf, *retbuf;
1691 unsigned char header[4];
1692
1693 if (info->readin)
1694 return;
1695 info->buffer = NULL;
1696 info->map_addr = NULL;
1697 info->readin = 1;
1698
1699 if (dwarf2_section_empty_p (info))
1700 return;
1701
1702 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1703 abfd = sectp->owner;
1704
1705 /* Check if the file has a 4-byte header indicating compression. */
1706 if (info->size > sizeof (header)
1707 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1708 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1709 {
1710 /* Upon decompression, update the buffer and its size. */
1711 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1712 {
1713 zlib_decompress_section (objfile, sectp, &info->buffer,
1714 &info->size);
1715 return;
1716 }
1717 }
1718
1719 #ifdef HAVE_MMAP
1720 if (pagesize == 0)
1721 pagesize = getpagesize ();
1722
1723 /* Only try to mmap sections which are large enough: we don't want to
1724 waste space due to fragmentation. Also, only try mmap for sections
1725 without relocations. */
1726
1727 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1728 {
1729 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1730 MAP_PRIVATE, sectp->filepos,
1731 &info->map_addr, &info->map_len);
1732
1733 if ((caddr_t)info->buffer != MAP_FAILED)
1734 {
1735 #if HAVE_POSIX_MADVISE
1736 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1737 #endif
1738 return;
1739 }
1740 }
1741 #endif
1742
1743 /* If we get here, we are a normal, not-compressed section. */
1744 info->buffer = buf
1745 = obstack_alloc (&objfile->objfile_obstack, info->size);
1746
1747 /* When debugging .o files, we may need to apply relocations; see
1748 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1749 We never compress sections in .o files, so we only need to
1750 try this when the section is not compressed. */
1751 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1752 if (retbuf != NULL)
1753 {
1754 info->buffer = retbuf;
1755 return;
1756 }
1757
1758 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1759 || bfd_bread (buf, info->size, abfd) != info->size)
1760 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1761 bfd_get_filename (abfd));
1762 }
1763
1764 /* A helper function that returns the size of a section in a safe way.
1765 If you are positive that the section has been read before using the
1766 size, then it is safe to refer to the dwarf2_section_info object's
1767 "size" field directly. In other cases, you must call this
1768 function, because for compressed sections the size field is not set
1769 correctly until the section has been read. */
1770
1771 static bfd_size_type
1772 dwarf2_section_size (struct objfile *objfile,
1773 struct dwarf2_section_info *info)
1774 {
1775 if (!info->readin)
1776 dwarf2_read_section (objfile, info);
1777 return info->size;
1778 }
1779
1780 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1781 SECTION_NAME. */
1782
1783 void
1784 dwarf2_get_section_info (struct objfile *objfile,
1785 enum dwarf2_section_enum sect,
1786 asection **sectp, gdb_byte **bufp,
1787 bfd_size_type *sizep)
1788 {
1789 struct dwarf2_per_objfile *data
1790 = objfile_data (objfile, dwarf2_objfile_data_key);
1791 struct dwarf2_section_info *info;
1792
1793 /* We may see an objfile without any DWARF, in which case we just
1794 return nothing. */
1795 if (data == NULL)
1796 {
1797 *sectp = NULL;
1798 *bufp = NULL;
1799 *sizep = 0;
1800 return;
1801 }
1802 switch (sect)
1803 {
1804 case DWARF2_DEBUG_FRAME:
1805 info = &data->frame;
1806 break;
1807 case DWARF2_EH_FRAME:
1808 info = &data->eh_frame;
1809 break;
1810 default:
1811 gdb_assert_not_reached ("unexpected section");
1812 }
1813
1814 dwarf2_read_section (objfile, info);
1815
1816 *sectp = info->asection;
1817 *bufp = info->buffer;
1818 *sizep = info->size;
1819 }
1820
1821 \f
1822 /* DWARF quick_symbols_functions support. */
1823
1824 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1825 unique line tables, so we maintain a separate table of all .debug_line
1826 derived entries to support the sharing.
1827 All the quick functions need is the list of file names. We discard the
1828 line_header when we're done and don't need to record it here. */
1829 struct quick_file_names
1830 {
1831 /* The offset in .debug_line of the line table. We hash on this. */
1832 unsigned int offset;
1833
1834 /* The number of entries in file_names, real_names. */
1835 unsigned int num_file_names;
1836
1837 /* The file names from the line table, after being run through
1838 file_full_name. */
1839 const char **file_names;
1840
1841 /* The file names from the line table after being run through
1842 gdb_realpath. These are computed lazily. */
1843 const char **real_names;
1844 };
1845
1846 /* When using the index (and thus not using psymtabs), each CU has an
1847 object of this type. This is used to hold information needed by
1848 the various "quick" methods. */
1849 struct dwarf2_per_cu_quick_data
1850 {
1851 /* The file table. This can be NULL if there was no file table
1852 or it's currently not read in.
1853 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1854 struct quick_file_names *file_names;
1855
1856 /* The corresponding symbol table. This is NULL if symbols for this
1857 CU have not yet been read. */
1858 struct symtab *symtab;
1859
1860 /* A temporary mark bit used when iterating over all CUs in
1861 expand_symtabs_matching. */
1862 unsigned int mark : 1;
1863
1864 /* True if we've tried to read the file table and found there isn't one.
1865 There will be no point in trying to read it again next time. */
1866 unsigned int no_file_data : 1;
1867 };
1868
1869 /* Hash function for a quick_file_names. */
1870
1871 static hashval_t
1872 hash_file_name_entry (const void *e)
1873 {
1874 const struct quick_file_names *file_data = e;
1875
1876 return file_data->offset;
1877 }
1878
1879 /* Equality function for a quick_file_names. */
1880
1881 static int
1882 eq_file_name_entry (const void *a, const void *b)
1883 {
1884 const struct quick_file_names *ea = a;
1885 const struct quick_file_names *eb = b;
1886
1887 return ea->offset == eb->offset;
1888 }
1889
1890 /* Delete function for a quick_file_names. */
1891
1892 static void
1893 delete_file_name_entry (void *e)
1894 {
1895 struct quick_file_names *file_data = e;
1896 int i;
1897
1898 for (i = 0; i < file_data->num_file_names; ++i)
1899 {
1900 xfree ((void*) file_data->file_names[i]);
1901 if (file_data->real_names)
1902 xfree ((void*) file_data->real_names[i]);
1903 }
1904
1905 /* The space for the struct itself lives on objfile_obstack,
1906 so we don't free it here. */
1907 }
1908
1909 /* Create a quick_file_names hash table. */
1910
1911 static htab_t
1912 create_quick_file_names_table (unsigned int nr_initial_entries)
1913 {
1914 return htab_create_alloc (nr_initial_entries,
1915 hash_file_name_entry, eq_file_name_entry,
1916 delete_file_name_entry, xcalloc, xfree);
1917 }
1918
1919 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1920 have to be created afterwards. You should call age_cached_comp_units after
1921 processing PER_CU->CU. dw2_setup must have been already called. */
1922
1923 static void
1924 load_cu (struct dwarf2_per_cu_data *per_cu)
1925 {
1926 if (per_cu->is_debug_types)
1927 load_full_type_unit (per_cu);
1928 else
1929 load_full_comp_unit (per_cu);
1930
1931 gdb_assert (per_cu->cu != NULL);
1932
1933 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1934 }
1935
1936 /* Read in the symbols for PER_CU. */
1937
1938 static void
1939 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1940 {
1941 struct cleanup *back_to;
1942
1943 back_to = make_cleanup (dwarf2_release_queue, NULL);
1944
1945 queue_comp_unit (per_cu);
1946
1947 load_cu (per_cu);
1948
1949 process_queue ();
1950
1951 /* Age the cache, releasing compilation units that have not
1952 been used recently. */
1953 age_cached_comp_units ();
1954
1955 do_cleanups (back_to);
1956 }
1957
1958 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1959 the objfile from which this CU came. Returns the resulting symbol
1960 table. */
1961
1962 static struct symtab *
1963 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1964 {
1965 if (!per_cu->v.quick->symtab)
1966 {
1967 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1968 increment_reading_symtab ();
1969 dw2_do_instantiate_symtab (per_cu);
1970 do_cleanups (back_to);
1971 }
1972 return per_cu->v.quick->symtab;
1973 }
1974
1975 /* Return the CU given its index. */
1976
1977 static struct dwarf2_per_cu_data *
1978 dw2_get_cu (int index)
1979 {
1980 if (index >= dwarf2_per_objfile->n_comp_units)
1981 {
1982 index -= dwarf2_per_objfile->n_comp_units;
1983 return dwarf2_per_objfile->all_type_units[index];
1984 }
1985 return dwarf2_per_objfile->all_comp_units[index];
1986 }
1987
1988 /* A helper function that knows how to read a 64-bit value in a way
1989 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1990 otherwise. */
1991
1992 static int
1993 extract_cu_value (const char *bytes, ULONGEST *result)
1994 {
1995 if (sizeof (ULONGEST) < 8)
1996 {
1997 int i;
1998
1999 /* Ignore the upper 4 bytes if they are all zero. */
2000 for (i = 0; i < 4; ++i)
2001 if (bytes[i + 4] != 0)
2002 return 0;
2003
2004 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2005 }
2006 else
2007 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2008 return 1;
2009 }
2010
2011 /* Read the CU list from the mapped index, and use it to create all
2012 the CU objects for this objfile. Return 0 if something went wrong,
2013 1 if everything went ok. */
2014
2015 static int
2016 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2017 offset_type cu_list_elements)
2018 {
2019 offset_type i;
2020
2021 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2022 dwarf2_per_objfile->all_comp_units
2023 = obstack_alloc (&objfile->objfile_obstack,
2024 dwarf2_per_objfile->n_comp_units
2025 * sizeof (struct dwarf2_per_cu_data *));
2026
2027 for (i = 0; i < cu_list_elements; i += 2)
2028 {
2029 struct dwarf2_per_cu_data *the_cu;
2030 ULONGEST offset, length;
2031
2032 if (!extract_cu_value (cu_list, &offset)
2033 || !extract_cu_value (cu_list + 8, &length))
2034 return 0;
2035 cu_list += 2 * 8;
2036
2037 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2038 struct dwarf2_per_cu_data);
2039 the_cu->offset.sect_off = offset;
2040 the_cu->length = length;
2041 the_cu->objfile = objfile;
2042 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2043 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2044 struct dwarf2_per_cu_quick_data);
2045 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2046 }
2047
2048 return 1;
2049 }
2050
2051 /* Create the signatured type hash table from the index. */
2052
2053 static int
2054 create_signatured_type_table_from_index (struct objfile *objfile,
2055 struct dwarf2_section_info *section,
2056 const gdb_byte *bytes,
2057 offset_type elements)
2058 {
2059 offset_type i;
2060 htab_t sig_types_hash;
2061
2062 dwarf2_per_objfile->n_type_units = elements / 3;
2063 dwarf2_per_objfile->all_type_units
2064 = obstack_alloc (&objfile->objfile_obstack,
2065 dwarf2_per_objfile->n_type_units
2066 * sizeof (struct dwarf2_per_cu_data *));
2067
2068 sig_types_hash = allocate_signatured_type_table (objfile);
2069
2070 for (i = 0; i < elements; i += 3)
2071 {
2072 struct signatured_type *sig_type;
2073 ULONGEST offset, type_offset_in_tu, signature;
2074 void **slot;
2075
2076 if (!extract_cu_value (bytes, &offset)
2077 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2078 return 0;
2079 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2080 bytes += 3 * 8;
2081
2082 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2083 struct signatured_type);
2084 sig_type->signature = signature;
2085 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2086 sig_type->per_cu.is_debug_types = 1;
2087 sig_type->per_cu.info_or_types_section = section;
2088 sig_type->per_cu.offset.sect_off = offset;
2089 sig_type->per_cu.objfile = objfile;
2090 sig_type->per_cu.v.quick
2091 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2092 struct dwarf2_per_cu_quick_data);
2093
2094 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2095 *slot = sig_type;
2096
2097 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2098 }
2099
2100 dwarf2_per_objfile->signatured_types = sig_types_hash;
2101
2102 return 1;
2103 }
2104
2105 /* Read the address map data from the mapped index, and use it to
2106 populate the objfile's psymtabs_addrmap. */
2107
2108 static void
2109 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2110 {
2111 const gdb_byte *iter, *end;
2112 struct obstack temp_obstack;
2113 struct addrmap *mutable_map;
2114 struct cleanup *cleanup;
2115 CORE_ADDR baseaddr;
2116
2117 obstack_init (&temp_obstack);
2118 cleanup = make_cleanup_obstack_free (&temp_obstack);
2119 mutable_map = addrmap_create_mutable (&temp_obstack);
2120
2121 iter = index->address_table;
2122 end = iter + index->address_table_size;
2123
2124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2125
2126 while (iter < end)
2127 {
2128 ULONGEST hi, lo, cu_index;
2129 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2130 iter += 8;
2131 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2132 iter += 8;
2133 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2134 iter += 4;
2135
2136 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2137 dw2_get_cu (cu_index));
2138 }
2139
2140 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2141 &objfile->objfile_obstack);
2142 do_cleanups (cleanup);
2143 }
2144
2145 /* The hash function for strings in the mapped index. This is the same as
2146 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2147 implementation. This is necessary because the hash function is tied to the
2148 format of the mapped index file. The hash values do not have to match with
2149 SYMBOL_HASH_NEXT.
2150
2151 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2152
2153 static hashval_t
2154 mapped_index_string_hash (int index_version, const void *p)
2155 {
2156 const unsigned char *str = (const unsigned char *) p;
2157 hashval_t r = 0;
2158 unsigned char c;
2159
2160 while ((c = *str++) != 0)
2161 {
2162 if (index_version >= 5)
2163 c = tolower (c);
2164 r = r * 67 + c - 113;
2165 }
2166
2167 return r;
2168 }
2169
2170 /* Find a slot in the mapped index INDEX for the object named NAME.
2171 If NAME is found, set *VEC_OUT to point to the CU vector in the
2172 constant pool and return 1. If NAME cannot be found, return 0. */
2173
2174 static int
2175 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2176 offset_type **vec_out)
2177 {
2178 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2179 offset_type hash;
2180 offset_type slot, step;
2181 int (*cmp) (const char *, const char *);
2182
2183 if (current_language->la_language == language_cplus
2184 || current_language->la_language == language_java
2185 || current_language->la_language == language_fortran)
2186 {
2187 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2188 not contain any. */
2189 const char *paren = strchr (name, '(');
2190
2191 if (paren)
2192 {
2193 char *dup;
2194
2195 dup = xmalloc (paren - name + 1);
2196 memcpy (dup, name, paren - name);
2197 dup[paren - name] = 0;
2198
2199 make_cleanup (xfree, dup);
2200 name = dup;
2201 }
2202 }
2203
2204 /* Index version 4 did not support case insensitive searches. But the
2205 indices for case insensitive languages are built in lowercase, therefore
2206 simulate our NAME being searched is also lowercased. */
2207 hash = mapped_index_string_hash ((index->version == 4
2208 && case_sensitivity == case_sensitive_off
2209 ? 5 : index->version),
2210 name);
2211
2212 slot = hash & (index->symbol_table_slots - 1);
2213 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2214 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2215
2216 for (;;)
2217 {
2218 /* Convert a slot number to an offset into the table. */
2219 offset_type i = 2 * slot;
2220 const char *str;
2221 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2222 {
2223 do_cleanups (back_to);
2224 return 0;
2225 }
2226
2227 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2228 if (!cmp (name, str))
2229 {
2230 *vec_out = (offset_type *) (index->constant_pool
2231 + MAYBE_SWAP (index->symbol_table[i + 1]));
2232 do_cleanups (back_to);
2233 return 1;
2234 }
2235
2236 slot = (slot + step) & (index->symbol_table_slots - 1);
2237 }
2238 }
2239
2240 /* Read the index file. If everything went ok, initialize the "quick"
2241 elements of all the CUs and return 1. Otherwise, return 0. */
2242
2243 static int
2244 dwarf2_read_index (struct objfile *objfile)
2245 {
2246 char *addr;
2247 struct mapped_index *map;
2248 offset_type *metadata;
2249 const gdb_byte *cu_list;
2250 const gdb_byte *types_list = NULL;
2251 offset_type version, cu_list_elements;
2252 offset_type types_list_elements = 0;
2253 int i;
2254
2255 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2256 return 0;
2257
2258 /* Older elfutils strip versions could keep the section in the main
2259 executable while splitting it for the separate debug info file. */
2260 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2261 & SEC_HAS_CONTENTS) == 0)
2262 return 0;
2263
2264 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2265
2266 addr = dwarf2_per_objfile->gdb_index.buffer;
2267 /* Version check. */
2268 version = MAYBE_SWAP (*(offset_type *) addr);
2269 /* Versions earlier than 3 emitted every copy of a psymbol. This
2270 causes the index to behave very poorly for certain requests. Version 3
2271 contained incomplete addrmap. So, it seems better to just ignore such
2272 indices. */
2273 if (version < 4)
2274 {
2275 static int warning_printed = 0;
2276 if (!warning_printed)
2277 {
2278 warning (_("Skipping obsolete .gdb_index section in %s."),
2279 objfile->name);
2280 warning_printed = 1;
2281 }
2282 return 0;
2283 }
2284 /* Index version 4 uses a different hash function than index version
2285 5 and later.
2286
2287 Versions earlier than 6 did not emit psymbols for inlined
2288 functions. Using these files will cause GDB not to be able to
2289 set breakpoints on inlined functions by name, so we ignore these
2290 indices unless the --use-deprecated-index-sections command line
2291 option was supplied. */
2292 if (version < 6 && !use_deprecated_index_sections)
2293 {
2294 static int warning_printed = 0;
2295 if (!warning_printed)
2296 {
2297 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2298 "--use-deprecated-index-sections to use them anyway"),
2299 objfile->name);
2300 warning_printed = 1;
2301 }
2302 return 0;
2303 }
2304 /* Indexes with higher version than the one supported by GDB may be no
2305 longer backward compatible. */
2306 if (version > 6)
2307 return 0;
2308
2309 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2310 map->version = version;
2311 map->total_size = dwarf2_per_objfile->gdb_index.size;
2312
2313 metadata = (offset_type *) (addr + sizeof (offset_type));
2314
2315 i = 0;
2316 cu_list = addr + MAYBE_SWAP (metadata[i]);
2317 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2318 / 8);
2319 ++i;
2320
2321 types_list = addr + MAYBE_SWAP (metadata[i]);
2322 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2323 - MAYBE_SWAP (metadata[i]))
2324 / 8);
2325 ++i;
2326
2327 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2328 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2329 - MAYBE_SWAP (metadata[i]));
2330 ++i;
2331
2332 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2333 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2334 - MAYBE_SWAP (metadata[i]))
2335 / (2 * sizeof (offset_type)));
2336 ++i;
2337
2338 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2339
2340 /* Don't use the index if it's empty. */
2341 if (map->symbol_table_slots == 0)
2342 return 0;
2343
2344 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2345 return 0;
2346
2347 if (types_list_elements)
2348 {
2349 struct dwarf2_section_info *section;
2350
2351 /* We can only handle a single .debug_types when we have an
2352 index. */
2353 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2354 return 0;
2355
2356 section = VEC_index (dwarf2_section_info_def,
2357 dwarf2_per_objfile->types, 0);
2358
2359 if (!create_signatured_type_table_from_index (objfile, section,
2360 types_list,
2361 types_list_elements))
2362 return 0;
2363 }
2364
2365 create_addrmap_from_index (objfile, map);
2366
2367 dwarf2_per_objfile->index_table = map;
2368 dwarf2_per_objfile->using_index = 1;
2369 dwarf2_per_objfile->quick_file_names_table =
2370 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2371
2372 return 1;
2373 }
2374
2375 /* A helper for the "quick" functions which sets the global
2376 dwarf2_per_objfile according to OBJFILE. */
2377
2378 static void
2379 dw2_setup (struct objfile *objfile)
2380 {
2381 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2382 gdb_assert (dwarf2_per_objfile);
2383 }
2384
2385 /* die_reader_func for dw2_get_file_names. */
2386
2387 static void
2388 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2389 gdb_byte *info_ptr,
2390 struct die_info *comp_unit_die,
2391 int has_children,
2392 void *data)
2393 {
2394 struct dwarf2_cu *cu = reader->cu;
2395 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2396 struct objfile *objfile = dwarf2_per_objfile->objfile;
2397 struct line_header *lh;
2398 struct attribute *attr;
2399 int i;
2400 unsigned int bytes_read;
2401 char *name, *comp_dir;
2402 void **slot;
2403 struct quick_file_names *qfn;
2404 unsigned int line_offset;
2405
2406 lh = NULL;
2407 slot = NULL;
2408 line_offset = 0;
2409
2410 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2411 if (attr)
2412 {
2413 struct quick_file_names find_entry;
2414
2415 line_offset = DW_UNSND (attr);
2416
2417 /* We may have already read in this line header (TU line header sharing).
2418 If we have we're done. */
2419 find_entry.offset = line_offset;
2420 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2421 &find_entry, INSERT);
2422 if (*slot != NULL)
2423 {
2424 this_cu->v.quick->file_names = *slot;
2425 return;
2426 }
2427
2428 lh = dwarf_decode_line_header (line_offset, cu);
2429 }
2430 if (lh == NULL)
2431 {
2432 this_cu->v.quick->no_file_data = 1;
2433 return;
2434 }
2435
2436 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2437 qfn->offset = line_offset;
2438 gdb_assert (slot != NULL);
2439 *slot = qfn;
2440
2441 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2442
2443 qfn->num_file_names = lh->num_file_names;
2444 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2445 lh->num_file_names * sizeof (char *));
2446 for (i = 0; i < lh->num_file_names; ++i)
2447 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2448 qfn->real_names = NULL;
2449
2450 free_line_header (lh);
2451
2452 this_cu->v.quick->file_names = qfn;
2453 }
2454
2455 /* A helper for the "quick" functions which attempts to read the line
2456 table for THIS_CU. */
2457
2458 static struct quick_file_names *
2459 dw2_get_file_names (struct objfile *objfile,
2460 struct dwarf2_per_cu_data *this_cu)
2461 {
2462 if (this_cu->v.quick->file_names != NULL)
2463 return this_cu->v.quick->file_names;
2464 /* If we know there is no line data, no point in looking again. */
2465 if (this_cu->v.quick->no_file_data)
2466 return NULL;
2467
2468 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2469 in the stub for CUs, there's is no need to lookup the DWO file.
2470 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2471 DWO file. */
2472 if (this_cu->is_debug_types)
2473 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2474 else
2475 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2476
2477 if (this_cu->v.quick->no_file_data)
2478 return NULL;
2479 return this_cu->v.quick->file_names;
2480 }
2481
2482 /* A helper for the "quick" functions which computes and caches the
2483 real path for a given file name from the line table. */
2484
2485 static const char *
2486 dw2_get_real_path (struct objfile *objfile,
2487 struct quick_file_names *qfn, int index)
2488 {
2489 if (qfn->real_names == NULL)
2490 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2491 qfn->num_file_names, sizeof (char *));
2492
2493 if (qfn->real_names[index] == NULL)
2494 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2495
2496 return qfn->real_names[index];
2497 }
2498
2499 static struct symtab *
2500 dw2_find_last_source_symtab (struct objfile *objfile)
2501 {
2502 int index;
2503
2504 dw2_setup (objfile);
2505 index = dwarf2_per_objfile->n_comp_units - 1;
2506 return dw2_instantiate_symtab (dw2_get_cu (index));
2507 }
2508
2509 /* Traversal function for dw2_forget_cached_source_info. */
2510
2511 static int
2512 dw2_free_cached_file_names (void **slot, void *info)
2513 {
2514 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2515
2516 if (file_data->real_names)
2517 {
2518 int i;
2519
2520 for (i = 0; i < file_data->num_file_names; ++i)
2521 {
2522 xfree ((void*) file_data->real_names[i]);
2523 file_data->real_names[i] = NULL;
2524 }
2525 }
2526
2527 return 1;
2528 }
2529
2530 static void
2531 dw2_forget_cached_source_info (struct objfile *objfile)
2532 {
2533 dw2_setup (objfile);
2534
2535 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2536 dw2_free_cached_file_names, NULL);
2537 }
2538
2539 /* Helper function for dw2_map_symtabs_matching_filename that expands
2540 the symtabs and calls the iterator. */
2541
2542 static int
2543 dw2_map_expand_apply (struct objfile *objfile,
2544 struct dwarf2_per_cu_data *per_cu,
2545 const char *name,
2546 const char *full_path, const char *real_path,
2547 int (*callback) (struct symtab *, void *),
2548 void *data)
2549 {
2550 struct symtab *last_made = objfile->symtabs;
2551
2552 /* Don't visit already-expanded CUs. */
2553 if (per_cu->v.quick->symtab)
2554 return 0;
2555
2556 /* This may expand more than one symtab, and we want to iterate over
2557 all of them. */
2558 dw2_instantiate_symtab (per_cu);
2559
2560 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2561 objfile->symtabs, last_made);
2562 }
2563
2564 /* Implementation of the map_symtabs_matching_filename method. */
2565
2566 static int
2567 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2568 const char *full_path, const char *real_path,
2569 int (*callback) (struct symtab *, void *),
2570 void *data)
2571 {
2572 int i;
2573 const char *name_basename = lbasename (name);
2574 int name_len = strlen (name);
2575 int is_abs = IS_ABSOLUTE_PATH (name);
2576
2577 dw2_setup (objfile);
2578
2579 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2580 + dwarf2_per_objfile->n_type_units); ++i)
2581 {
2582 int j;
2583 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2584 struct quick_file_names *file_data;
2585
2586 /* We only need to look at symtabs not already expanded. */
2587 if (per_cu->v.quick->symtab)
2588 continue;
2589
2590 file_data = dw2_get_file_names (objfile, per_cu);
2591 if (file_data == NULL)
2592 continue;
2593
2594 for (j = 0; j < file_data->num_file_names; ++j)
2595 {
2596 const char *this_name = file_data->file_names[j];
2597
2598 if (FILENAME_CMP (name, this_name) == 0
2599 || (!is_abs && compare_filenames_for_search (this_name,
2600 name, name_len)))
2601 {
2602 if (dw2_map_expand_apply (objfile, per_cu,
2603 name, full_path, real_path,
2604 callback, data))
2605 return 1;
2606 }
2607
2608 /* Before we invoke realpath, which can get expensive when many
2609 files are involved, do a quick comparison of the basenames. */
2610 if (! basenames_may_differ
2611 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2612 continue;
2613
2614 if (full_path != NULL)
2615 {
2616 const char *this_real_name = dw2_get_real_path (objfile,
2617 file_data, j);
2618
2619 if (this_real_name != NULL
2620 && (FILENAME_CMP (full_path, this_real_name) == 0
2621 || (!is_abs
2622 && compare_filenames_for_search (this_real_name,
2623 name, name_len))))
2624 {
2625 if (dw2_map_expand_apply (objfile, per_cu,
2626 name, full_path, real_path,
2627 callback, data))
2628 return 1;
2629 }
2630 }
2631
2632 if (real_path != NULL)
2633 {
2634 const char *this_real_name = dw2_get_real_path (objfile,
2635 file_data, j);
2636
2637 if (this_real_name != NULL
2638 && (FILENAME_CMP (real_path, this_real_name) == 0
2639 || (!is_abs
2640 && compare_filenames_for_search (this_real_name,
2641 name, name_len))))
2642 {
2643 if (dw2_map_expand_apply (objfile, per_cu,
2644 name, full_path, real_path,
2645 callback, data))
2646 return 1;
2647 }
2648 }
2649 }
2650 }
2651
2652 return 0;
2653 }
2654
2655 static struct symtab *
2656 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2657 const char *name, domain_enum domain)
2658 {
2659 /* We do all the work in the pre_expand_symtabs_matching hook
2660 instead. */
2661 return NULL;
2662 }
2663
2664 /* A helper function that expands all symtabs that hold an object
2665 named NAME. */
2666
2667 static void
2668 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2669 {
2670 dw2_setup (objfile);
2671
2672 /* index_table is NULL if OBJF_READNOW. */
2673 if (dwarf2_per_objfile->index_table)
2674 {
2675 offset_type *vec;
2676
2677 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2678 name, &vec))
2679 {
2680 offset_type i, len = MAYBE_SWAP (*vec);
2681 for (i = 0; i < len; ++i)
2682 {
2683 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2684 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2685
2686 dw2_instantiate_symtab (per_cu);
2687 }
2688 }
2689 }
2690 }
2691
2692 static void
2693 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2694 enum block_enum block_kind, const char *name,
2695 domain_enum domain)
2696 {
2697 dw2_do_expand_symtabs_matching (objfile, name);
2698 }
2699
2700 static void
2701 dw2_print_stats (struct objfile *objfile)
2702 {
2703 int i, count;
2704
2705 dw2_setup (objfile);
2706 count = 0;
2707 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2708 + dwarf2_per_objfile->n_type_units); ++i)
2709 {
2710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2711
2712 if (!per_cu->v.quick->symtab)
2713 ++count;
2714 }
2715 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2716 }
2717
2718 static void
2719 dw2_dump (struct objfile *objfile)
2720 {
2721 /* Nothing worth printing. */
2722 }
2723
2724 static void
2725 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2726 struct section_offsets *delta)
2727 {
2728 /* There's nothing to relocate here. */
2729 }
2730
2731 static void
2732 dw2_expand_symtabs_for_function (struct objfile *objfile,
2733 const char *func_name)
2734 {
2735 dw2_do_expand_symtabs_matching (objfile, func_name);
2736 }
2737
2738 static void
2739 dw2_expand_all_symtabs (struct objfile *objfile)
2740 {
2741 int i;
2742
2743 dw2_setup (objfile);
2744
2745 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2746 + dwarf2_per_objfile->n_type_units); ++i)
2747 {
2748 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2749
2750 dw2_instantiate_symtab (per_cu);
2751 }
2752 }
2753
2754 static void
2755 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2756 const char *filename)
2757 {
2758 int i;
2759
2760 dw2_setup (objfile);
2761
2762 /* We don't need to consider type units here.
2763 This is only called for examining code, e.g. expand_line_sal.
2764 There can be an order of magnitude (or more) more type units
2765 than comp units, and we avoid them if we can. */
2766
2767 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2768 {
2769 int j;
2770 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2771 struct quick_file_names *file_data;
2772
2773 /* We only need to look at symtabs not already expanded. */
2774 if (per_cu->v.quick->symtab)
2775 continue;
2776
2777 file_data = dw2_get_file_names (objfile, per_cu);
2778 if (file_data == NULL)
2779 continue;
2780
2781 for (j = 0; j < file_data->num_file_names; ++j)
2782 {
2783 const char *this_name = file_data->file_names[j];
2784 if (FILENAME_CMP (this_name, filename) == 0)
2785 {
2786 dw2_instantiate_symtab (per_cu);
2787 break;
2788 }
2789 }
2790 }
2791 }
2792
2793 static const char *
2794 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2795 {
2796 struct dwarf2_per_cu_data *per_cu;
2797 offset_type *vec;
2798 struct quick_file_names *file_data;
2799
2800 dw2_setup (objfile);
2801
2802 /* index_table is NULL if OBJF_READNOW. */
2803 if (!dwarf2_per_objfile->index_table)
2804 {
2805 struct symtab *s;
2806
2807 ALL_OBJFILE_SYMTABS (objfile, s)
2808 if (s->primary)
2809 {
2810 struct blockvector *bv = BLOCKVECTOR (s);
2811 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2812 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2813
2814 if (sym)
2815 return sym->symtab->filename;
2816 }
2817 return NULL;
2818 }
2819
2820 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2821 name, &vec))
2822 return NULL;
2823
2824 /* Note that this just looks at the very first one named NAME -- but
2825 actually we are looking for a function. find_main_filename
2826 should be rewritten so that it doesn't require a custom hook. It
2827 could just use the ordinary symbol tables. */
2828 /* vec[0] is the length, which must always be >0. */
2829 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2830
2831 file_data = dw2_get_file_names (objfile, per_cu);
2832 if (file_data == NULL
2833 || file_data->num_file_names == 0)
2834 return NULL;
2835
2836 return file_data->file_names[file_data->num_file_names - 1];
2837 }
2838
2839 static void
2840 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2841 struct objfile *objfile, int global,
2842 int (*callback) (struct block *,
2843 struct symbol *, void *),
2844 void *data, symbol_compare_ftype *match,
2845 symbol_compare_ftype *ordered_compare)
2846 {
2847 /* Currently unimplemented; used for Ada. The function can be called if the
2848 current language is Ada for a non-Ada objfile using GNU index. As Ada
2849 does not look for non-Ada symbols this function should just return. */
2850 }
2851
2852 static void
2853 dw2_expand_symtabs_matching
2854 (struct objfile *objfile,
2855 int (*file_matcher) (const char *, void *),
2856 int (*name_matcher) (const char *, void *),
2857 enum search_domain kind,
2858 void *data)
2859 {
2860 int i;
2861 offset_type iter;
2862 struct mapped_index *index;
2863
2864 dw2_setup (objfile);
2865
2866 /* index_table is NULL if OBJF_READNOW. */
2867 if (!dwarf2_per_objfile->index_table)
2868 return;
2869 index = dwarf2_per_objfile->index_table;
2870
2871 if (file_matcher != NULL)
2872 {
2873 struct cleanup *cleanup;
2874 htab_t visited_found, visited_not_found;
2875
2876 visited_found = htab_create_alloc (10,
2877 htab_hash_pointer, htab_eq_pointer,
2878 NULL, xcalloc, xfree);
2879 cleanup = make_cleanup_htab_delete (visited_found);
2880 visited_not_found = htab_create_alloc (10,
2881 htab_hash_pointer, htab_eq_pointer,
2882 NULL, xcalloc, xfree);
2883 make_cleanup_htab_delete (visited_not_found);
2884
2885 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2886 + dwarf2_per_objfile->n_type_units); ++i)
2887 {
2888 int j;
2889 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2890 struct quick_file_names *file_data;
2891 void **slot;
2892
2893 per_cu->v.quick->mark = 0;
2894
2895 /* We only need to look at symtabs not already expanded. */
2896 if (per_cu->v.quick->symtab)
2897 continue;
2898
2899 file_data = dw2_get_file_names (objfile, per_cu);
2900 if (file_data == NULL)
2901 continue;
2902
2903 if (htab_find (visited_not_found, file_data) != NULL)
2904 continue;
2905 else if (htab_find (visited_found, file_data) != NULL)
2906 {
2907 per_cu->v.quick->mark = 1;
2908 continue;
2909 }
2910
2911 for (j = 0; j < file_data->num_file_names; ++j)
2912 {
2913 if (file_matcher (file_data->file_names[j], data))
2914 {
2915 per_cu->v.quick->mark = 1;
2916 break;
2917 }
2918 }
2919
2920 slot = htab_find_slot (per_cu->v.quick->mark
2921 ? visited_found
2922 : visited_not_found,
2923 file_data, INSERT);
2924 *slot = file_data;
2925 }
2926
2927 do_cleanups (cleanup);
2928 }
2929
2930 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2931 {
2932 offset_type idx = 2 * iter;
2933 const char *name;
2934 offset_type *vec, vec_len, vec_idx;
2935
2936 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2937 continue;
2938
2939 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2940
2941 if (! (*name_matcher) (name, data))
2942 continue;
2943
2944 /* The name was matched, now expand corresponding CUs that were
2945 marked. */
2946 vec = (offset_type *) (index->constant_pool
2947 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2948 vec_len = MAYBE_SWAP (vec[0]);
2949 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2950 {
2951 struct dwarf2_per_cu_data *per_cu;
2952
2953 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2954 if (file_matcher == NULL || per_cu->v.quick->mark)
2955 dw2_instantiate_symtab (per_cu);
2956 }
2957 }
2958 }
2959
2960 static struct symtab *
2961 dw2_find_pc_sect_symtab (struct objfile *objfile,
2962 struct minimal_symbol *msymbol,
2963 CORE_ADDR pc,
2964 struct obj_section *section,
2965 int warn_if_readin)
2966 {
2967 struct dwarf2_per_cu_data *data;
2968
2969 dw2_setup (objfile);
2970
2971 if (!objfile->psymtabs_addrmap)
2972 return NULL;
2973
2974 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2975 if (!data)
2976 return NULL;
2977
2978 if (warn_if_readin && data->v.quick->symtab)
2979 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2980 paddress (get_objfile_arch (objfile), pc));
2981
2982 return dw2_instantiate_symtab (data);
2983 }
2984
2985 static void
2986 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2987 void *data, int need_fullname)
2988 {
2989 int i;
2990 struct cleanup *cleanup;
2991 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2992 NULL, xcalloc, xfree);
2993
2994 cleanup = make_cleanup_htab_delete (visited);
2995 dw2_setup (objfile);
2996
2997 /* We can ignore file names coming from already-expanded CUs. */
2998 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2999 + dwarf2_per_objfile->n_type_units); ++i)
3000 {
3001 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3002
3003 if (per_cu->v.quick->symtab)
3004 {
3005 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3006 INSERT);
3007
3008 *slot = per_cu->v.quick->file_names;
3009 }
3010 }
3011
3012 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3013 + dwarf2_per_objfile->n_type_units); ++i)
3014 {
3015 int j;
3016 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3017 struct quick_file_names *file_data;
3018 void **slot;
3019
3020 /* We only need to look at symtabs not already expanded. */
3021 if (per_cu->v.quick->symtab)
3022 continue;
3023
3024 file_data = dw2_get_file_names (objfile, per_cu);
3025 if (file_data == NULL)
3026 continue;
3027
3028 slot = htab_find_slot (visited, file_data, INSERT);
3029 if (*slot)
3030 {
3031 /* Already visited. */
3032 continue;
3033 }
3034 *slot = file_data;
3035
3036 for (j = 0; j < file_data->num_file_names; ++j)
3037 {
3038 const char *this_real_name;
3039
3040 if (need_fullname)
3041 this_real_name = dw2_get_real_path (objfile, file_data, j);
3042 else
3043 this_real_name = NULL;
3044 (*fun) (file_data->file_names[j], this_real_name, data);
3045 }
3046 }
3047
3048 do_cleanups (cleanup);
3049 }
3050
3051 static int
3052 dw2_has_symbols (struct objfile *objfile)
3053 {
3054 return 1;
3055 }
3056
3057 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3058 {
3059 dw2_has_symbols,
3060 dw2_find_last_source_symtab,
3061 dw2_forget_cached_source_info,
3062 dw2_map_symtabs_matching_filename,
3063 dw2_lookup_symbol,
3064 dw2_pre_expand_symtabs_matching,
3065 dw2_print_stats,
3066 dw2_dump,
3067 dw2_relocate,
3068 dw2_expand_symtabs_for_function,
3069 dw2_expand_all_symtabs,
3070 dw2_expand_symtabs_with_filename,
3071 dw2_find_symbol_file,
3072 dw2_map_matching_symbols,
3073 dw2_expand_symtabs_matching,
3074 dw2_find_pc_sect_symtab,
3075 dw2_map_symbol_filenames
3076 };
3077
3078 /* Initialize for reading DWARF for this objfile. Return 0 if this
3079 file will use psymtabs, or 1 if using the GNU index. */
3080
3081 int
3082 dwarf2_initialize_objfile (struct objfile *objfile)
3083 {
3084 /* If we're about to read full symbols, don't bother with the
3085 indices. In this case we also don't care if some other debug
3086 format is making psymtabs, because they are all about to be
3087 expanded anyway. */
3088 if ((objfile->flags & OBJF_READNOW))
3089 {
3090 int i;
3091
3092 dwarf2_per_objfile->using_index = 1;
3093 create_all_comp_units (objfile);
3094 create_all_type_units (objfile);
3095 dwarf2_per_objfile->quick_file_names_table =
3096 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3097
3098 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3099 + dwarf2_per_objfile->n_type_units); ++i)
3100 {
3101 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3102
3103 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3104 struct dwarf2_per_cu_quick_data);
3105 }
3106
3107 /* Return 1 so that gdb sees the "quick" functions. However,
3108 these functions will be no-ops because we will have expanded
3109 all symtabs. */
3110 return 1;
3111 }
3112
3113 if (dwarf2_read_index (objfile))
3114 return 1;
3115
3116 return 0;
3117 }
3118
3119 \f
3120
3121 /* Build a partial symbol table. */
3122
3123 void
3124 dwarf2_build_psymtabs (struct objfile *objfile)
3125 {
3126 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3127 {
3128 init_psymbol_list (objfile, 1024);
3129 }
3130
3131 dwarf2_build_psymtabs_hard (objfile);
3132 }
3133
3134 /* Return TRUE if OFFSET is within CU_HEADER. */
3135
3136 static inline int
3137 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3138 {
3139 sect_offset bottom = { cu_header->offset.sect_off };
3140 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3141 + cu_header->initial_length_size) };
3142
3143 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3144 }
3145
3146 /* Read in the comp unit header information from the debug_info at info_ptr.
3147 NOTE: This leaves members offset, first_die_offset to be filled in
3148 by the caller. */
3149
3150 static gdb_byte *
3151 read_comp_unit_head (struct comp_unit_head *cu_header,
3152 gdb_byte *info_ptr, bfd *abfd)
3153 {
3154 int signed_addr;
3155 unsigned int bytes_read;
3156
3157 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3158 cu_header->initial_length_size = bytes_read;
3159 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3160 info_ptr += bytes_read;
3161 cu_header->version = read_2_bytes (abfd, info_ptr);
3162 info_ptr += 2;
3163 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3164 &bytes_read);
3165 info_ptr += bytes_read;
3166 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3167 info_ptr += 1;
3168 signed_addr = bfd_get_sign_extend_vma (abfd);
3169 if (signed_addr < 0)
3170 internal_error (__FILE__, __LINE__,
3171 _("read_comp_unit_head: dwarf from non elf file"));
3172 cu_header->signed_addr_p = signed_addr;
3173
3174 return info_ptr;
3175 }
3176
3177 /* Subroutine of read_and_check_comp_unit_head and
3178 read_and_check_type_unit_head to simplify them.
3179 Perform various error checking on the header. */
3180
3181 static void
3182 error_check_comp_unit_head (struct comp_unit_head *header,
3183 struct dwarf2_section_info *section)
3184 {
3185 bfd *abfd = section->asection->owner;
3186 const char *filename = bfd_get_filename (abfd);
3187
3188 if (header->version != 2 && header->version != 3 && header->version != 4)
3189 error (_("Dwarf Error: wrong version in compilation unit header "
3190 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3191 filename);
3192
3193 if (header->abbrev_offset.sect_off
3194 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3195 &dwarf2_per_objfile->abbrev))
3196 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3197 "(offset 0x%lx + 6) [in module %s]"),
3198 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3199 filename);
3200
3201 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3202 avoid potential 32-bit overflow. */
3203 if (((unsigned long) header->offset.sect_off
3204 + header->length + header->initial_length_size)
3205 > section->size)
3206 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3207 "(offset 0x%lx + 0) [in module %s]"),
3208 (long) header->length, (long) header->offset.sect_off,
3209 filename);
3210 }
3211
3212 /* Read in a CU/TU header and perform some basic error checking.
3213 The contents of the header are stored in HEADER.
3214 The result is a pointer to the start of the first DIE. */
3215
3216 static gdb_byte *
3217 read_and_check_comp_unit_head (struct comp_unit_head *header,
3218 struct dwarf2_section_info *section,
3219 gdb_byte *info_ptr,
3220 int is_debug_types_section)
3221 {
3222 gdb_byte *beg_of_comp_unit = info_ptr;
3223 bfd *abfd = section->asection->owner;
3224
3225 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3226
3227 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3228
3229 /* If we're reading a type unit, skip over the signature and
3230 type_offset fields. */
3231 if (is_debug_types_section)
3232 info_ptr += 8 /*signature*/ + header->offset_size;
3233
3234 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3235
3236 error_check_comp_unit_head (header, section);
3237
3238 return info_ptr;
3239 }
3240
3241 /* Read in the types comp unit header information from .debug_types entry at
3242 types_ptr. The result is a pointer to one past the end of the header. */
3243
3244 static gdb_byte *
3245 read_and_check_type_unit_head (struct comp_unit_head *header,
3246 struct dwarf2_section_info *section,
3247 gdb_byte *info_ptr,
3248 ULONGEST *signature,
3249 cu_offset *type_offset_in_tu)
3250 {
3251 gdb_byte *beg_of_comp_unit = info_ptr;
3252 bfd *abfd = section->asection->owner;
3253
3254 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3255
3256 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3257
3258 /* If we're reading a type unit, skip over the signature and
3259 type_offset fields. */
3260 if (signature != NULL)
3261 *signature = read_8_bytes (abfd, info_ptr);
3262 info_ptr += 8;
3263 if (type_offset_in_tu != NULL)
3264 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3265 header->offset_size);
3266 info_ptr += header->offset_size;
3267
3268 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3269
3270 error_check_comp_unit_head (header, section);
3271
3272 return info_ptr;
3273 }
3274
3275 /* Allocate a new partial symtab for file named NAME and mark this new
3276 partial symtab as being an include of PST. */
3277
3278 static void
3279 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3280 struct objfile *objfile)
3281 {
3282 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3283
3284 subpst->section_offsets = pst->section_offsets;
3285 subpst->textlow = 0;
3286 subpst->texthigh = 0;
3287
3288 subpst->dependencies = (struct partial_symtab **)
3289 obstack_alloc (&objfile->objfile_obstack,
3290 sizeof (struct partial_symtab *));
3291 subpst->dependencies[0] = pst;
3292 subpst->number_of_dependencies = 1;
3293
3294 subpst->globals_offset = 0;
3295 subpst->n_global_syms = 0;
3296 subpst->statics_offset = 0;
3297 subpst->n_static_syms = 0;
3298 subpst->symtab = NULL;
3299 subpst->read_symtab = pst->read_symtab;
3300 subpst->readin = 0;
3301
3302 /* No private part is necessary for include psymtabs. This property
3303 can be used to differentiate between such include psymtabs and
3304 the regular ones. */
3305 subpst->read_symtab_private = NULL;
3306 }
3307
3308 /* Read the Line Number Program data and extract the list of files
3309 included by the source file represented by PST. Build an include
3310 partial symtab for each of these included files. */
3311
3312 static void
3313 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3314 struct die_info *die,
3315 struct partial_symtab *pst)
3316 {
3317 struct line_header *lh = NULL;
3318 struct attribute *attr;
3319
3320 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3321 if (attr)
3322 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3323 if (lh == NULL)
3324 return; /* No linetable, so no includes. */
3325
3326 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3327 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3328
3329 free_line_header (lh);
3330 }
3331
3332 static hashval_t
3333 hash_signatured_type (const void *item)
3334 {
3335 const struct signatured_type *sig_type = item;
3336
3337 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3338 return sig_type->signature;
3339 }
3340
3341 static int
3342 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3343 {
3344 const struct signatured_type *lhs = item_lhs;
3345 const struct signatured_type *rhs = item_rhs;
3346
3347 return lhs->signature == rhs->signature;
3348 }
3349
3350 /* Allocate a hash table for signatured types. */
3351
3352 static htab_t
3353 allocate_signatured_type_table (struct objfile *objfile)
3354 {
3355 return htab_create_alloc_ex (41,
3356 hash_signatured_type,
3357 eq_signatured_type,
3358 NULL,
3359 &objfile->objfile_obstack,
3360 hashtab_obstack_allocate,
3361 dummy_obstack_deallocate);
3362 }
3363
3364 /* A helper function to add a signatured type CU to a table. */
3365
3366 static int
3367 add_signatured_type_cu_to_table (void **slot, void *datum)
3368 {
3369 struct signatured_type *sigt = *slot;
3370 struct dwarf2_per_cu_data ***datap = datum;
3371
3372 **datap = &sigt->per_cu;
3373 ++*datap;
3374
3375 return 1;
3376 }
3377
3378 /* Create the hash table of all entries in the .debug_types section.
3379 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3380 The result is a pointer to the hash table or NULL if there are
3381 no types. */
3382
3383 static htab_t
3384 create_debug_types_hash_table (struct dwo_file *dwo_file,
3385 VEC (dwarf2_section_info_def) *types)
3386 {
3387 struct objfile *objfile = dwarf2_per_objfile->objfile;
3388 htab_t types_htab = NULL;
3389 int ix;
3390 struct dwarf2_section_info *section;
3391
3392 if (VEC_empty (dwarf2_section_info_def, types))
3393 return NULL;
3394
3395 for (ix = 0;
3396 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3397 ++ix)
3398 {
3399 bfd *abfd;
3400 gdb_byte *info_ptr, *end_ptr;
3401
3402 dwarf2_read_section (objfile, section);
3403 info_ptr = section->buffer;
3404
3405 if (info_ptr == NULL)
3406 continue;
3407
3408 /* We can't set abfd until now because the section may be empty or
3409 not present, in which case section->asection will be NULL. */
3410 abfd = section->asection->owner;
3411
3412 if (types_htab == NULL)
3413 {
3414 if (dwo_file)
3415 types_htab = allocate_dwo_unit_table (objfile);
3416 else
3417 types_htab = allocate_signatured_type_table (objfile);
3418 }
3419
3420 if (dwarf2_die_debug)
3421 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3422 bfd_get_filename (abfd));
3423
3424 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3425 because we don't need to read any dies: the signature is in the
3426 header. */
3427
3428 end_ptr = info_ptr + section->size;
3429 while (info_ptr < end_ptr)
3430 {
3431 sect_offset offset;
3432 cu_offset type_offset_in_tu;
3433 ULONGEST signature;
3434 struct signatured_type *sig_type;
3435 struct dwo_unit *dwo_tu;
3436 void **slot;
3437 gdb_byte *ptr = info_ptr;
3438 struct comp_unit_head header;
3439 unsigned int length;
3440
3441 offset.sect_off = ptr - section->buffer;
3442
3443 /* We need to read the type's signature in order to build the hash
3444 table, but we don't need anything else just yet. */
3445
3446 ptr = read_and_check_type_unit_head (&header, section, ptr,
3447 &signature, &type_offset_in_tu);
3448
3449 length = header.initial_length_size + header.length;
3450
3451 /* Skip dummy type units. */
3452 if (ptr >= info_ptr + length
3453 || peek_abbrev_code (abfd, ptr) == 0)
3454 {
3455 info_ptr += header.initial_length_size + header.length;
3456 continue;
3457 }
3458
3459 if (dwo_file)
3460 {
3461 sig_type = NULL;
3462 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3463 struct dwo_unit);
3464 dwo_tu->dwo_file = dwo_file;
3465 dwo_tu->signature = signature;
3466 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3467 dwo_tu->info_or_types_section = section;
3468 dwo_tu->offset = offset;
3469 dwo_tu->length = length;
3470 }
3471 else
3472 {
3473 /* N.B.: type_offset is not usable if this type uses a DWO file.
3474 The real type_offset is in the DWO file. */
3475 dwo_tu = NULL;
3476 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3477 struct signatured_type);
3478 sig_type->signature = signature;
3479 sig_type->type_offset_in_tu = type_offset_in_tu;
3480 sig_type->per_cu.objfile = objfile;
3481 sig_type->per_cu.is_debug_types = 1;
3482 sig_type->per_cu.info_or_types_section = section;
3483 sig_type->per_cu.offset = offset;
3484 sig_type->per_cu.length = length;
3485 }
3486
3487 slot = htab_find_slot (types_htab,
3488 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3489 INSERT);
3490 gdb_assert (slot != NULL);
3491 if (*slot != NULL)
3492 {
3493 sect_offset dup_offset;
3494
3495 if (dwo_file)
3496 {
3497 const struct dwo_unit *dup_tu = *slot;
3498
3499 dup_offset = dup_tu->offset;
3500 }
3501 else
3502 {
3503 const struct signatured_type *dup_tu = *slot;
3504
3505 dup_offset = dup_tu->per_cu.offset;
3506 }
3507
3508 complaint (&symfile_complaints,
3509 _("debug type entry at offset 0x%x is duplicate to the "
3510 "entry at offset 0x%x, signature 0x%s"),
3511 offset.sect_off, dup_offset.sect_off,
3512 phex (signature, sizeof (signature)));
3513 }
3514 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3515
3516 if (dwarf2_die_debug)
3517 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3518 offset.sect_off,
3519 phex (signature, sizeof (signature)));
3520
3521 info_ptr += length;
3522 }
3523 }
3524
3525 return types_htab;
3526 }
3527
3528 /* Create the hash table of all entries in the .debug_types section,
3529 and initialize all_type_units.
3530 The result is zero if there is an error (e.g. missing .debug_types section),
3531 otherwise non-zero. */
3532
3533 static int
3534 create_all_type_units (struct objfile *objfile)
3535 {
3536 htab_t types_htab;
3537 struct dwarf2_per_cu_data **iter;
3538
3539 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3540 if (types_htab == NULL)
3541 {
3542 dwarf2_per_objfile->signatured_types = NULL;
3543 return 0;
3544 }
3545
3546 dwarf2_per_objfile->signatured_types = types_htab;
3547
3548 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3549 dwarf2_per_objfile->all_type_units
3550 = obstack_alloc (&objfile->objfile_obstack,
3551 dwarf2_per_objfile->n_type_units
3552 * sizeof (struct dwarf2_per_cu_data *));
3553 iter = &dwarf2_per_objfile->all_type_units[0];
3554 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3555 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3556 == dwarf2_per_objfile->n_type_units);
3557
3558 return 1;
3559 }
3560
3561 /* Lookup a signature based type for DW_FORM_ref_sig8.
3562 Returns NULL if signature SIG is not present in the table. */
3563
3564 static struct signatured_type *
3565 lookup_signatured_type (ULONGEST sig)
3566 {
3567 struct signatured_type find_entry, *entry;
3568
3569 if (dwarf2_per_objfile->signatured_types == NULL)
3570 {
3571 complaint (&symfile_complaints,
3572 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3573 return NULL;
3574 }
3575
3576 find_entry.signature = sig;
3577 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3578 return entry;
3579 }
3580
3581 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3582
3583 static void
3584 init_cu_die_reader (struct die_reader_specs *reader,
3585 struct dwarf2_cu *cu,
3586 struct dwarf2_section_info *section,
3587 struct dwo_file *dwo_file)
3588 {
3589 gdb_assert (section->readin && section->buffer != NULL);
3590 reader->abfd = section->asection->owner;
3591 reader->cu = cu;
3592 reader->dwo_file = dwo_file;
3593 reader->die_section = section;
3594 reader->buffer = section->buffer;
3595 }
3596
3597 /* Find the base address of the compilation unit for range lists and
3598 location lists. It will normally be specified by DW_AT_low_pc.
3599 In DWARF-3 draft 4, the base address could be overridden by
3600 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3601 compilation units with discontinuous ranges. */
3602
3603 static void
3604 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3605 {
3606 struct attribute *attr;
3607
3608 cu->base_known = 0;
3609 cu->base_address = 0;
3610
3611 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3612 if (attr)
3613 {
3614 cu->base_address = DW_ADDR (attr);
3615 cu->base_known = 1;
3616 }
3617 else
3618 {
3619 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3620 if (attr)
3621 {
3622 cu->base_address = DW_ADDR (attr);
3623 cu->base_known = 1;
3624 }
3625 }
3626 }
3627
3628 /* Initialize a CU (or TU) and read its DIEs.
3629 If the CU defers to a DWO file, read the DWO file as well.
3630
3631 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3632 Otherwise, a new CU is allocated with xmalloc.
3633
3634 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3635 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3636
3637 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3638 linker) then DIE_READER_FUNC will not get called. */
3639
3640 static void
3641 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3642 int use_existing_cu, int keep,
3643 die_reader_func_ftype *die_reader_func,
3644 void *data)
3645 {
3646 struct objfile *objfile = dwarf2_per_objfile->objfile;
3647 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3648 bfd *abfd = section->asection->owner;
3649 struct dwarf2_cu *cu;
3650 gdb_byte *begin_info_ptr, *info_ptr;
3651 struct die_reader_specs reader;
3652 struct die_info *comp_unit_die;
3653 int has_children;
3654 struct attribute *attr;
3655 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3656 struct signatured_type *sig_type = NULL;
3657
3658 if (use_existing_cu)
3659 gdb_assert (keep);
3660
3661 cleanups = make_cleanup (null_cleanup, NULL);
3662
3663 /* This is cheap if the section is already read in. */
3664 dwarf2_read_section (objfile, section);
3665
3666 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3667
3668 if (use_existing_cu && this_cu->cu != NULL)
3669 {
3670 cu = this_cu->cu;
3671 info_ptr += cu->header.first_die_offset.cu_off;
3672 }
3673 else
3674 {
3675 /* If !use_existing_cu, this_cu->cu must be NULL. */
3676 gdb_assert (this_cu->cu == NULL);
3677
3678 cu = xmalloc (sizeof (*cu));
3679 init_one_comp_unit (cu, this_cu);
3680
3681 /* If an error occurs while loading, release our storage. */
3682 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3683
3684 if (this_cu->is_debug_types)
3685 {
3686 ULONGEST signature;
3687
3688 info_ptr = read_and_check_type_unit_head (&cu->header,
3689 section, info_ptr,
3690 &signature, NULL);
3691
3692 /* There's no way to get from PER_CU to its containing
3693 struct signatured_type.
3694 But we have the signature so we can use that. */
3695 sig_type = lookup_signatured_type (signature);
3696 /* We've already scanned all the signatured types,
3697 this must succeed. */
3698 gdb_assert (sig_type != NULL);
3699 gdb_assert (&sig_type->per_cu == this_cu);
3700 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3701
3702 /* LENGTH has not been set yet for type units. */
3703 this_cu->length = cu->header.length + cu->header.initial_length_size;
3704
3705 /* Establish the type offset that can be used to lookup the type. */
3706 sig_type->type_offset_in_section.sect_off =
3707 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3708 }
3709 else
3710 {
3711 info_ptr = read_and_check_comp_unit_head (&cu->header,
3712 section, info_ptr, 0);
3713
3714 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3715 gdb_assert (this_cu->length
3716 == cu->header.length + cu->header.initial_length_size);
3717 }
3718 }
3719
3720 /* Skip dummy compilation units. */
3721 if (info_ptr >= begin_info_ptr + this_cu->length
3722 || peek_abbrev_code (abfd, info_ptr) == 0)
3723 {
3724 do_cleanups (cleanups);
3725 return;
3726 }
3727
3728 /* Read the abbrevs for this compilation unit into a table. */
3729 if (cu->dwarf2_abbrevs == NULL)
3730 {
3731 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3732 make_cleanup (dwarf2_free_abbrev_table, cu);
3733 }
3734
3735 /* Read the top level CU/TU die. */
3736 init_cu_die_reader (&reader, cu, section, NULL);
3737 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3738
3739 /* If we have a DWO stub, process it and then read in the DWO file.
3740 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3741 a DWO CU, that this test will fail. */
3742 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3743 if (attr)
3744 {
3745 char *dwo_name = DW_STRING (attr);
3746 const char *comp_dir;
3747 struct dwo_unit *dwo_unit;
3748 ULONGEST signature; /* Or dwo_id. */
3749 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3750 int i,num_extra_attrs;
3751
3752 if (has_children)
3753 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3754 " has children (offset 0x%x) [in module %s]"),
3755 this_cu->offset.sect_off, bfd_get_filename (abfd));
3756
3757 /* These attributes aren't processed until later:
3758 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3759 However, the attribute is found in the stub which we won't have later.
3760 In order to not impose this complication on the rest of the code,
3761 we read them here and copy them to the DWO CU/TU die. */
3762 stmt_list = low_pc = high_pc = ranges = NULL;
3763
3764 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3765 DWO file. */
3766 if (! this_cu->is_debug_types)
3767 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3768 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3769 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3770 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3771
3772 /* There should be a DW_AT_addr_base attribute here (if needed).
3773 We need the value before we can process DW_FORM_GNU_addr_index. */
3774 cu->addr_base = 0;
3775 cu->have_addr_base = 0;
3776 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3777 if (attr)
3778 {
3779 cu->addr_base = DW_UNSND (attr);
3780 cu->have_addr_base = 1;
3781 }
3782
3783 if (this_cu->is_debug_types)
3784 {
3785 gdb_assert (sig_type != NULL);
3786 signature = sig_type->signature;
3787 }
3788 else
3789 {
3790 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3791 if (! attr)
3792 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3793 dwo_name);
3794 signature = DW_UNSND (attr);
3795 }
3796
3797 /* We may need the comp_dir in order to find the DWO file. */
3798 comp_dir = NULL;
3799 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3800 if (attr)
3801 comp_dir = DW_STRING (attr);
3802
3803 if (this_cu->is_debug_types)
3804 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3805 else
3806 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3807 signature);
3808
3809 if (dwo_unit == NULL)
3810 {
3811 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3812 " with ID %s [in module %s]"),
3813 this_cu->offset.sect_off,
3814 phex (signature, sizeof (signature)),
3815 objfile->name);
3816 }
3817
3818 /* Set up for reading the DWO CU/TU. */
3819 cu->dwo_unit = dwo_unit;
3820 section = dwo_unit->info_or_types_section;
3821 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3822 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3823
3824 if (this_cu->is_debug_types)
3825 {
3826 ULONGEST signature;
3827
3828 info_ptr = read_and_check_type_unit_head (&cu->header,
3829 section, info_ptr,
3830 &signature, NULL);
3831 gdb_assert (sig_type->signature == signature);
3832 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3833 gdb_assert (dwo_unit->length
3834 == cu->header.length + cu->header.initial_length_size);
3835
3836 /* Establish the type offset that can be used to lookup the type.
3837 For DWO files, we don't know it until now. */
3838 sig_type->type_offset_in_section.sect_off =
3839 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3840 }
3841 else
3842 {
3843 info_ptr = read_and_check_comp_unit_head (&cu->header,
3844 section, info_ptr, 0);
3845 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3846 gdb_assert (dwo_unit->length
3847 == cu->header.length + cu->header.initial_length_size);
3848 }
3849
3850 /* Discard the original CU's abbrev table, and read the DWO's. */
3851 dwarf2_free_abbrev_table (cu);
3852 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3853
3854 /* Read in the die, but leave space to copy over the attributes
3855 from the stub. This has the benefit of simplifying the rest of
3856 the code - all the real work is done here. */
3857 num_extra_attrs = ((stmt_list != NULL)
3858 + (low_pc != NULL)
3859 + (high_pc != NULL)
3860 + (ranges != NULL));
3861 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3862 &has_children, num_extra_attrs);
3863
3864 /* Copy over the attributes from the stub to the DWO die. */
3865 i = comp_unit_die->num_attrs;
3866 if (stmt_list != NULL)
3867 comp_unit_die->attrs[i++] = *stmt_list;
3868 if (low_pc != NULL)
3869 comp_unit_die->attrs[i++] = *low_pc;
3870 if (high_pc != NULL)
3871 comp_unit_die->attrs[i++] = *high_pc;
3872 if (ranges != NULL)
3873 comp_unit_die->attrs[i++] = *ranges;
3874 comp_unit_die->num_attrs += num_extra_attrs;
3875
3876 /* Skip dummy compilation units. */
3877 if (info_ptr >= begin_info_ptr + dwo_unit->length
3878 || peek_abbrev_code (abfd, info_ptr) == 0)
3879 {
3880 do_cleanups (cleanups);
3881 return;
3882 }
3883 }
3884
3885 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3886
3887 if (free_cu_cleanup != NULL)
3888 {
3889 if (keep)
3890 {
3891 /* We've successfully allocated this compilation unit. Let our
3892 caller clean it up when finished with it. */
3893 discard_cleanups (free_cu_cleanup);
3894
3895 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3896 So we have to manually free the abbrev table. */
3897 dwarf2_free_abbrev_table (cu);
3898
3899 /* Link this CU into read_in_chain. */
3900 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3901 dwarf2_per_objfile->read_in_chain = this_cu;
3902 }
3903 else
3904 do_cleanups (free_cu_cleanup);
3905 }
3906
3907 do_cleanups (cleanups);
3908 }
3909
3910 /* Read CU/TU THIS_CU in section SECTION,
3911 but do not follow DW_AT_GNU_dwo_name if present.
3912 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3913 have already done the lookup to find the DWO file).
3914
3915 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3916 THIS_CU->is_debug_types, but nothing else.
3917
3918 We fill in THIS_CU->length.
3919
3920 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3921 linker) then DIE_READER_FUNC will not get called.
3922
3923 THIS_CU->cu is always freed when done.
3924 This is done in order to not leave THIS_CU->cu in a state where we have
3925 to care whether it refers to the "main" CU or the DWO CU. */
3926
3927 static void
3928 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3929 struct dwarf2_section_info *abbrev_section,
3930 struct dwo_file *dwo_file,
3931 die_reader_func_ftype *die_reader_func,
3932 void *data)
3933 {
3934 struct objfile *objfile = dwarf2_per_objfile->objfile;
3935 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3936 bfd *abfd = section->asection->owner;
3937 struct dwarf2_cu cu;
3938 gdb_byte *begin_info_ptr, *info_ptr;
3939 struct die_reader_specs reader;
3940 struct cleanup *cleanups;
3941 struct die_info *comp_unit_die;
3942 int has_children;
3943
3944 gdb_assert (this_cu->cu == NULL);
3945
3946 /* This is cheap if the section is already read in. */
3947 dwarf2_read_section (objfile, section);
3948
3949 init_one_comp_unit (&cu, this_cu);
3950
3951 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3952
3953 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3954 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3955 this_cu->is_debug_types);
3956
3957 this_cu->length = cu.header.length + cu.header.initial_length_size;
3958
3959 /* Skip dummy compilation units. */
3960 if (info_ptr >= begin_info_ptr + this_cu->length
3961 || peek_abbrev_code (abfd, info_ptr) == 0)
3962 {
3963 do_cleanups (cleanups);
3964 return;
3965 }
3966
3967 dwarf2_read_abbrevs (&cu, abbrev_section);
3968 make_cleanup (dwarf2_free_abbrev_table, &cu);
3969
3970 init_cu_die_reader (&reader, &cu, section, dwo_file);
3971 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3972
3973 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3974
3975 do_cleanups (cleanups);
3976 }
3977
3978 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
3979 does not lookup the specified DWO file.
3980 This cannot be used to read DWO files.
3981
3982 THIS_CU->cu is always freed when done.
3983 This is done in order to not leave THIS_CU->cu in a state where we have
3984 to care whether it refers to the "main" CU or the DWO CU.
3985 We can revisit this if the data shows there's a performance issue. */
3986
3987 static void
3988 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
3989 die_reader_func_ftype *die_reader_func,
3990 void *data)
3991 {
3992 init_cutu_and_read_dies_no_follow (this_cu,
3993 &dwarf2_per_objfile->abbrev,
3994 NULL,
3995 die_reader_func, data);
3996 }
3997
3998 /* die_reader_func for process_psymtab_comp_unit. */
3999
4000 static void
4001 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4002 gdb_byte *info_ptr,
4003 struct die_info *comp_unit_die,
4004 int has_children,
4005 void *data)
4006 {
4007 struct dwarf2_cu *cu = reader->cu;
4008 struct objfile *objfile = cu->objfile;
4009 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4010 bfd *abfd = objfile->obfd;
4011 struct attribute *attr;
4012 CORE_ADDR baseaddr;
4013 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4014 struct partial_symtab *pst;
4015 int has_pc_info;
4016 const char *filename;
4017
4018 if (comp_unit_die->tag == DW_TAG_partial_unit)
4019 return;
4020
4021 prepare_one_comp_unit (cu, comp_unit_die);
4022
4023 cu->list_in_scope = &file_symbols;
4024
4025 /* Allocate a new partial symbol table structure. */
4026 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4027 if (attr == NULL || !DW_STRING (attr))
4028 filename = "";
4029 else
4030 filename = DW_STRING (attr);
4031 pst = start_psymtab_common (objfile, objfile->section_offsets,
4032 filename,
4033 /* TEXTLOW and TEXTHIGH are set below. */
4034 0,
4035 objfile->global_psymbols.next,
4036 objfile->static_psymbols.next);
4037 pst->psymtabs_addrmap_supported = 1;
4038
4039 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4040 if (attr != NULL)
4041 pst->dirname = DW_STRING (attr);
4042
4043 pst->read_symtab_private = per_cu;
4044
4045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4046
4047 /* Store the function that reads in the rest of the symbol table. */
4048 pst->read_symtab = dwarf2_psymtab_to_symtab;
4049
4050 per_cu->v.psymtab = pst;
4051
4052 dwarf2_find_base_address (comp_unit_die, cu);
4053
4054 /* Possibly set the default values of LOWPC and HIGHPC from
4055 `DW_AT_ranges'. */
4056 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4057 &best_highpc, cu, pst);
4058 if (has_pc_info == 1 && best_lowpc < best_highpc)
4059 /* Store the contiguous range if it is not empty; it can be empty for
4060 CUs with no code. */
4061 addrmap_set_empty (objfile->psymtabs_addrmap,
4062 best_lowpc + baseaddr,
4063 best_highpc + baseaddr - 1, pst);
4064
4065 /* Check if comp unit has_children.
4066 If so, read the rest of the partial symbols from this comp unit.
4067 If not, there's no more debug_info for this comp unit. */
4068 if (has_children)
4069 {
4070 struct partial_die_info *first_die;
4071 CORE_ADDR lowpc, highpc;
4072
4073 lowpc = ((CORE_ADDR) -1);
4074 highpc = ((CORE_ADDR) 0);
4075
4076 first_die = load_partial_dies (reader, info_ptr, 1);
4077
4078 scan_partial_symbols (first_die, &lowpc, &highpc,
4079 ! has_pc_info, cu);
4080
4081 /* If we didn't find a lowpc, set it to highpc to avoid
4082 complaints from `maint check'. */
4083 if (lowpc == ((CORE_ADDR) -1))
4084 lowpc = highpc;
4085
4086 /* If the compilation unit didn't have an explicit address range,
4087 then use the information extracted from its child dies. */
4088 if (! has_pc_info)
4089 {
4090 best_lowpc = lowpc;
4091 best_highpc = highpc;
4092 }
4093 }
4094 pst->textlow = best_lowpc + baseaddr;
4095 pst->texthigh = best_highpc + baseaddr;
4096
4097 pst->n_global_syms = objfile->global_psymbols.next -
4098 (objfile->global_psymbols.list + pst->globals_offset);
4099 pst->n_static_syms = objfile->static_psymbols.next -
4100 (objfile->static_psymbols.list + pst->statics_offset);
4101 sort_pst_symbols (pst);
4102
4103 if (per_cu->is_debug_types)
4104 {
4105 /* It's not clear we want to do anything with stmt lists here.
4106 Waiting to see what gcc ultimately does. */
4107 }
4108 else
4109 {
4110 /* Get the list of files included in the current compilation unit,
4111 and build a psymtab for each of them. */
4112 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4113 }
4114 }
4115
4116 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4117 Process compilation unit THIS_CU for a psymtab. */
4118
4119 static void
4120 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu)
4121 {
4122 /* If this compilation unit was already read in, free the
4123 cached copy in order to read it in again. This is
4124 necessary because we skipped some symbols when we first
4125 read in the compilation unit (see load_partial_dies).
4126 This problem could be avoided, but the benefit is unclear. */
4127 if (this_cu->cu != NULL)
4128 free_one_cached_comp_unit (this_cu);
4129
4130 gdb_assert (! this_cu->is_debug_types);
4131 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4132 NULL);
4133
4134 /* Age out any secondary CUs. */
4135 age_cached_comp_units ();
4136 }
4137
4138 /* Traversal function for htab_traverse_noresize.
4139 Process one .debug_types comp-unit. */
4140
4141 static int
4142 process_psymtab_type_unit (void **slot, void *info)
4143 {
4144 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4145 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4146
4147 gdb_assert (per_cu->is_debug_types);
4148 gdb_assert (info == NULL);
4149
4150 /* If this compilation unit was already read in, free the
4151 cached copy in order to read it in again. This is
4152 necessary because we skipped some symbols when we first
4153 read in the compilation unit (see load_partial_dies).
4154 This problem could be avoided, but the benefit is unclear. */
4155 if (per_cu->cu != NULL)
4156 free_one_cached_comp_unit (per_cu);
4157
4158 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4159 NULL);
4160
4161 /* Age out any secondary CUs. */
4162 age_cached_comp_units ();
4163
4164 return 1;
4165 }
4166
4167 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4168 Build partial symbol tables for the .debug_types comp-units. */
4169
4170 static void
4171 build_type_psymtabs (struct objfile *objfile)
4172 {
4173 if (! create_all_type_units (objfile))
4174 return;
4175
4176 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4177 process_psymtab_type_unit, NULL);
4178 }
4179
4180 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4181
4182 static void
4183 psymtabs_addrmap_cleanup (void *o)
4184 {
4185 struct objfile *objfile = o;
4186
4187 objfile->psymtabs_addrmap = NULL;
4188 }
4189
4190 /* Build the partial symbol table by doing a quick pass through the
4191 .debug_info and .debug_abbrev sections. */
4192
4193 static void
4194 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4195 {
4196 struct cleanup *back_to, *addrmap_cleanup;
4197 struct obstack temp_obstack;
4198 int i;
4199
4200 dwarf2_per_objfile->reading_partial_symbols = 1;
4201
4202 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4203
4204 /* Any cached compilation units will be linked by the per-objfile
4205 read_in_chain. Make sure to free them when we're done. */
4206 back_to = make_cleanup (free_cached_comp_units, NULL);
4207
4208 build_type_psymtabs (objfile);
4209
4210 create_all_comp_units (objfile);
4211
4212 /* Create a temporary address map on a temporary obstack. We later
4213 copy this to the final obstack. */
4214 obstack_init (&temp_obstack);
4215 make_cleanup_obstack_free (&temp_obstack);
4216 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4217 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4218
4219 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4220 {
4221 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4222
4223 process_psymtab_comp_unit (per_cu);
4224 }
4225
4226 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4227 &objfile->objfile_obstack);
4228 discard_cleanups (addrmap_cleanup);
4229
4230 do_cleanups (back_to);
4231 }
4232
4233 /* die_reader_func for load_partial_comp_unit. */
4234
4235 static void
4236 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4237 gdb_byte *info_ptr,
4238 struct die_info *comp_unit_die,
4239 int has_children,
4240 void *data)
4241 {
4242 struct dwarf2_cu *cu = reader->cu;
4243
4244 prepare_one_comp_unit (cu, comp_unit_die);
4245
4246 /* Check if comp unit has_children.
4247 If so, read the rest of the partial symbols from this comp unit.
4248 If not, there's no more debug_info for this comp unit. */
4249 if (has_children)
4250 load_partial_dies (reader, info_ptr, 0);
4251 }
4252
4253 /* Load the partial DIEs for a secondary CU into memory.
4254 This is also used when rereading a primary CU with load_all_dies. */
4255
4256 static void
4257 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4258 {
4259 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4260 }
4261
4262 /* Create a list of all compilation units in OBJFILE.
4263 This is only done for -readnow and building partial symtabs. */
4264
4265 static void
4266 create_all_comp_units (struct objfile *objfile)
4267 {
4268 int n_allocated;
4269 int n_comp_units;
4270 struct dwarf2_per_cu_data **all_comp_units;
4271 gdb_byte *info_ptr;
4272
4273 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4274 info_ptr = dwarf2_per_objfile->info.buffer;
4275
4276 n_comp_units = 0;
4277 n_allocated = 10;
4278 all_comp_units = xmalloc (n_allocated
4279 * sizeof (struct dwarf2_per_cu_data *));
4280
4281 while (info_ptr < dwarf2_per_objfile->info.buffer
4282 + dwarf2_per_objfile->info.size)
4283 {
4284 unsigned int length, initial_length_size;
4285 struct dwarf2_per_cu_data *this_cu;
4286 sect_offset offset;
4287
4288 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4289
4290 /* Read just enough information to find out where the next
4291 compilation unit is. */
4292 length = read_initial_length (objfile->obfd, info_ptr,
4293 &initial_length_size);
4294
4295 /* Save the compilation unit for later lookup. */
4296 this_cu = obstack_alloc (&objfile->objfile_obstack,
4297 sizeof (struct dwarf2_per_cu_data));
4298 memset (this_cu, 0, sizeof (*this_cu));
4299 this_cu->offset = offset;
4300 this_cu->length = length + initial_length_size;
4301 this_cu->objfile = objfile;
4302 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4303
4304 if (n_comp_units == n_allocated)
4305 {
4306 n_allocated *= 2;
4307 all_comp_units = xrealloc (all_comp_units,
4308 n_allocated
4309 * sizeof (struct dwarf2_per_cu_data *));
4310 }
4311 all_comp_units[n_comp_units++] = this_cu;
4312
4313 info_ptr = info_ptr + this_cu->length;
4314 }
4315
4316 dwarf2_per_objfile->all_comp_units
4317 = obstack_alloc (&objfile->objfile_obstack,
4318 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4319 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4320 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4321 xfree (all_comp_units);
4322 dwarf2_per_objfile->n_comp_units = n_comp_units;
4323 }
4324
4325 /* Process all loaded DIEs for compilation unit CU, starting at
4326 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4327 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4328 DW_AT_ranges). If NEED_PC is set, then this function will set
4329 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4330 and record the covered ranges in the addrmap. */
4331
4332 static void
4333 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4334 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4335 {
4336 struct partial_die_info *pdi;
4337
4338 /* Now, march along the PDI's, descending into ones which have
4339 interesting children but skipping the children of the other ones,
4340 until we reach the end of the compilation unit. */
4341
4342 pdi = first_die;
4343
4344 while (pdi != NULL)
4345 {
4346 fixup_partial_die (pdi, cu);
4347
4348 /* Anonymous namespaces or modules have no name but have interesting
4349 children, so we need to look at them. Ditto for anonymous
4350 enums. */
4351
4352 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4353 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
4354 {
4355 switch (pdi->tag)
4356 {
4357 case DW_TAG_subprogram:
4358 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4359 break;
4360 case DW_TAG_constant:
4361 case DW_TAG_variable:
4362 case DW_TAG_typedef:
4363 case DW_TAG_union_type:
4364 if (!pdi->is_declaration)
4365 {
4366 add_partial_symbol (pdi, cu);
4367 }
4368 break;
4369 case DW_TAG_class_type:
4370 case DW_TAG_interface_type:
4371 case DW_TAG_structure_type:
4372 if (!pdi->is_declaration)
4373 {
4374 add_partial_symbol (pdi, cu);
4375 }
4376 break;
4377 case DW_TAG_enumeration_type:
4378 if (!pdi->is_declaration)
4379 add_partial_enumeration (pdi, cu);
4380 break;
4381 case DW_TAG_base_type:
4382 case DW_TAG_subrange_type:
4383 /* File scope base type definitions are added to the partial
4384 symbol table. */
4385 add_partial_symbol (pdi, cu);
4386 break;
4387 case DW_TAG_namespace:
4388 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4389 break;
4390 case DW_TAG_module:
4391 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4392 break;
4393 default:
4394 break;
4395 }
4396 }
4397
4398 /* If the die has a sibling, skip to the sibling. */
4399
4400 pdi = pdi->die_sibling;
4401 }
4402 }
4403
4404 /* Functions used to compute the fully scoped name of a partial DIE.
4405
4406 Normally, this is simple. For C++, the parent DIE's fully scoped
4407 name is concatenated with "::" and the partial DIE's name. For
4408 Java, the same thing occurs except that "." is used instead of "::".
4409 Enumerators are an exception; they use the scope of their parent
4410 enumeration type, i.e. the name of the enumeration type is not
4411 prepended to the enumerator.
4412
4413 There are two complexities. One is DW_AT_specification; in this
4414 case "parent" means the parent of the target of the specification,
4415 instead of the direct parent of the DIE. The other is compilers
4416 which do not emit DW_TAG_namespace; in this case we try to guess
4417 the fully qualified name of structure types from their members'
4418 linkage names. This must be done using the DIE's children rather
4419 than the children of any DW_AT_specification target. We only need
4420 to do this for structures at the top level, i.e. if the target of
4421 any DW_AT_specification (if any; otherwise the DIE itself) does not
4422 have a parent. */
4423
4424 /* Compute the scope prefix associated with PDI's parent, in
4425 compilation unit CU. The result will be allocated on CU's
4426 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4427 field. NULL is returned if no prefix is necessary. */
4428 static char *
4429 partial_die_parent_scope (struct partial_die_info *pdi,
4430 struct dwarf2_cu *cu)
4431 {
4432 char *grandparent_scope;
4433 struct partial_die_info *parent, *real_pdi;
4434
4435 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4436 then this means the parent of the specification DIE. */
4437
4438 real_pdi = pdi;
4439 while (real_pdi->has_specification)
4440 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4441
4442 parent = real_pdi->die_parent;
4443 if (parent == NULL)
4444 return NULL;
4445
4446 if (parent->scope_set)
4447 return parent->scope;
4448
4449 fixup_partial_die (parent, cu);
4450
4451 grandparent_scope = partial_die_parent_scope (parent, cu);
4452
4453 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4454 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4455 Work around this problem here. */
4456 if (cu->language == language_cplus
4457 && parent->tag == DW_TAG_namespace
4458 && strcmp (parent->name, "::") == 0
4459 && grandparent_scope == NULL)
4460 {
4461 parent->scope = NULL;
4462 parent->scope_set = 1;
4463 return NULL;
4464 }
4465
4466 if (pdi->tag == DW_TAG_enumerator)
4467 /* Enumerators should not get the name of the enumeration as a prefix. */
4468 parent->scope = grandparent_scope;
4469 else if (parent->tag == DW_TAG_namespace
4470 || parent->tag == DW_TAG_module
4471 || parent->tag == DW_TAG_structure_type
4472 || parent->tag == DW_TAG_class_type
4473 || parent->tag == DW_TAG_interface_type
4474 || parent->tag == DW_TAG_union_type
4475 || parent->tag == DW_TAG_enumeration_type)
4476 {
4477 if (grandparent_scope == NULL)
4478 parent->scope = parent->name;
4479 else
4480 parent->scope = typename_concat (&cu->comp_unit_obstack,
4481 grandparent_scope,
4482 parent->name, 0, cu);
4483 }
4484 else
4485 {
4486 /* FIXME drow/2004-04-01: What should we be doing with
4487 function-local names? For partial symbols, we should probably be
4488 ignoring them. */
4489 complaint (&symfile_complaints,
4490 _("unhandled containing DIE tag %d for DIE at %d"),
4491 parent->tag, pdi->offset.sect_off);
4492 parent->scope = grandparent_scope;
4493 }
4494
4495 parent->scope_set = 1;
4496 return parent->scope;
4497 }
4498
4499 /* Return the fully scoped name associated with PDI, from compilation unit
4500 CU. The result will be allocated with malloc. */
4501
4502 static char *
4503 partial_die_full_name (struct partial_die_info *pdi,
4504 struct dwarf2_cu *cu)
4505 {
4506 char *parent_scope;
4507
4508 /* If this is a template instantiation, we can not work out the
4509 template arguments from partial DIEs. So, unfortunately, we have
4510 to go through the full DIEs. At least any work we do building
4511 types here will be reused if full symbols are loaded later. */
4512 if (pdi->has_template_arguments)
4513 {
4514 fixup_partial_die (pdi, cu);
4515
4516 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4517 {
4518 struct die_info *die;
4519 struct attribute attr;
4520 struct dwarf2_cu *ref_cu = cu;
4521
4522 /* DW_FORM_ref_addr is using section offset. */
4523 attr.name = 0;
4524 attr.form = DW_FORM_ref_addr;
4525 attr.u.unsnd = pdi->offset.sect_off;
4526 die = follow_die_ref (NULL, &attr, &ref_cu);
4527
4528 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4529 }
4530 }
4531
4532 parent_scope = partial_die_parent_scope (pdi, cu);
4533 if (parent_scope == NULL)
4534 return NULL;
4535 else
4536 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4537 }
4538
4539 static void
4540 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4541 {
4542 struct objfile *objfile = cu->objfile;
4543 CORE_ADDR addr = 0;
4544 char *actual_name = NULL;
4545 CORE_ADDR baseaddr;
4546 int built_actual_name = 0;
4547
4548 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4549
4550 actual_name = partial_die_full_name (pdi, cu);
4551 if (actual_name)
4552 built_actual_name = 1;
4553
4554 if (actual_name == NULL)
4555 actual_name = pdi->name;
4556
4557 switch (pdi->tag)
4558 {
4559 case DW_TAG_subprogram:
4560 if (pdi->is_external || cu->language == language_ada)
4561 {
4562 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4563 of the global scope. But in Ada, we want to be able to access
4564 nested procedures globally. So all Ada subprograms are stored
4565 in the global scope. */
4566 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4567 mst_text, objfile); */
4568 add_psymbol_to_list (actual_name, strlen (actual_name),
4569 built_actual_name,
4570 VAR_DOMAIN, LOC_BLOCK,
4571 &objfile->global_psymbols,
4572 0, pdi->lowpc + baseaddr,
4573 cu->language, objfile);
4574 }
4575 else
4576 {
4577 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4578 mst_file_text, objfile); */
4579 add_psymbol_to_list (actual_name, strlen (actual_name),
4580 built_actual_name,
4581 VAR_DOMAIN, LOC_BLOCK,
4582 &objfile->static_psymbols,
4583 0, pdi->lowpc + baseaddr,
4584 cu->language, objfile);
4585 }
4586 break;
4587 case DW_TAG_constant:
4588 {
4589 struct psymbol_allocation_list *list;
4590
4591 if (pdi->is_external)
4592 list = &objfile->global_psymbols;
4593 else
4594 list = &objfile->static_psymbols;
4595 add_psymbol_to_list (actual_name, strlen (actual_name),
4596 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4597 list, 0, 0, cu->language, objfile);
4598 }
4599 break;
4600 case DW_TAG_variable:
4601 if (pdi->locdesc)
4602 addr = decode_locdesc (pdi->locdesc, cu);
4603
4604 if (pdi->locdesc
4605 && addr == 0
4606 && !dwarf2_per_objfile->has_section_at_zero)
4607 {
4608 /* A global or static variable may also have been stripped
4609 out by the linker if unused, in which case its address
4610 will be nullified; do not add such variables into partial
4611 symbol table then. */
4612 }
4613 else if (pdi->is_external)
4614 {
4615 /* Global Variable.
4616 Don't enter into the minimal symbol tables as there is
4617 a minimal symbol table entry from the ELF symbols already.
4618 Enter into partial symbol table if it has a location
4619 descriptor or a type.
4620 If the location descriptor is missing, new_symbol will create
4621 a LOC_UNRESOLVED symbol, the address of the variable will then
4622 be determined from the minimal symbol table whenever the variable
4623 is referenced.
4624 The address for the partial symbol table entry is not
4625 used by GDB, but it comes in handy for debugging partial symbol
4626 table building. */
4627
4628 if (pdi->locdesc || pdi->has_type)
4629 add_psymbol_to_list (actual_name, strlen (actual_name),
4630 built_actual_name,
4631 VAR_DOMAIN, LOC_STATIC,
4632 &objfile->global_psymbols,
4633 0, addr + baseaddr,
4634 cu->language, objfile);
4635 }
4636 else
4637 {
4638 /* Static Variable. Skip symbols without location descriptors. */
4639 if (pdi->locdesc == NULL)
4640 {
4641 if (built_actual_name)
4642 xfree (actual_name);
4643 return;
4644 }
4645 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4646 mst_file_data, objfile); */
4647 add_psymbol_to_list (actual_name, strlen (actual_name),
4648 built_actual_name,
4649 VAR_DOMAIN, LOC_STATIC,
4650 &objfile->static_psymbols,
4651 0, addr + baseaddr,
4652 cu->language, objfile);
4653 }
4654 break;
4655 case DW_TAG_typedef:
4656 case DW_TAG_base_type:
4657 case DW_TAG_subrange_type:
4658 add_psymbol_to_list (actual_name, strlen (actual_name),
4659 built_actual_name,
4660 VAR_DOMAIN, LOC_TYPEDEF,
4661 &objfile->static_psymbols,
4662 0, (CORE_ADDR) 0, cu->language, objfile);
4663 break;
4664 case DW_TAG_namespace:
4665 add_psymbol_to_list (actual_name, strlen (actual_name),
4666 built_actual_name,
4667 VAR_DOMAIN, LOC_TYPEDEF,
4668 &objfile->global_psymbols,
4669 0, (CORE_ADDR) 0, cu->language, objfile);
4670 break;
4671 case DW_TAG_class_type:
4672 case DW_TAG_interface_type:
4673 case DW_TAG_structure_type:
4674 case DW_TAG_union_type:
4675 case DW_TAG_enumeration_type:
4676 /* Skip external references. The DWARF standard says in the section
4677 about "Structure, Union, and Class Type Entries": "An incomplete
4678 structure, union or class type is represented by a structure,
4679 union or class entry that does not have a byte size attribute
4680 and that has a DW_AT_declaration attribute." */
4681 if (!pdi->has_byte_size && pdi->is_declaration)
4682 {
4683 if (built_actual_name)
4684 xfree (actual_name);
4685 return;
4686 }
4687
4688 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4689 static vs. global. */
4690 add_psymbol_to_list (actual_name, strlen (actual_name),
4691 built_actual_name,
4692 STRUCT_DOMAIN, LOC_TYPEDEF,
4693 (cu->language == language_cplus
4694 || cu->language == language_java)
4695 ? &objfile->global_psymbols
4696 : &objfile->static_psymbols,
4697 0, (CORE_ADDR) 0, cu->language, objfile);
4698
4699 break;
4700 case DW_TAG_enumerator:
4701 add_psymbol_to_list (actual_name, strlen (actual_name),
4702 built_actual_name,
4703 VAR_DOMAIN, LOC_CONST,
4704 (cu->language == language_cplus
4705 || cu->language == language_java)
4706 ? &objfile->global_psymbols
4707 : &objfile->static_psymbols,
4708 0, (CORE_ADDR) 0, cu->language, objfile);
4709 break;
4710 default:
4711 break;
4712 }
4713
4714 if (built_actual_name)
4715 xfree (actual_name);
4716 }
4717
4718 /* Read a partial die corresponding to a namespace; also, add a symbol
4719 corresponding to that namespace to the symbol table. NAMESPACE is
4720 the name of the enclosing namespace. */
4721
4722 static void
4723 add_partial_namespace (struct partial_die_info *pdi,
4724 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4725 int need_pc, struct dwarf2_cu *cu)
4726 {
4727 /* Add a symbol for the namespace. */
4728
4729 add_partial_symbol (pdi, cu);
4730
4731 /* Now scan partial symbols in that namespace. */
4732
4733 if (pdi->has_children)
4734 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4735 }
4736
4737 /* Read a partial die corresponding to a Fortran module. */
4738
4739 static void
4740 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4741 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4742 {
4743 /* Now scan partial symbols in that module. */
4744
4745 if (pdi->has_children)
4746 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4747 }
4748
4749 /* Read a partial die corresponding to a subprogram and create a partial
4750 symbol for that subprogram. When the CU language allows it, this
4751 routine also defines a partial symbol for each nested subprogram
4752 that this subprogram contains.
4753
4754 DIE my also be a lexical block, in which case we simply search
4755 recursively for suprograms defined inside that lexical block.
4756 Again, this is only performed when the CU language allows this
4757 type of definitions. */
4758
4759 static void
4760 add_partial_subprogram (struct partial_die_info *pdi,
4761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4762 int need_pc, struct dwarf2_cu *cu)
4763 {
4764 if (pdi->tag == DW_TAG_subprogram)
4765 {
4766 if (pdi->has_pc_info)
4767 {
4768 if (pdi->lowpc < *lowpc)
4769 *lowpc = pdi->lowpc;
4770 if (pdi->highpc > *highpc)
4771 *highpc = pdi->highpc;
4772 if (need_pc)
4773 {
4774 CORE_ADDR baseaddr;
4775 struct objfile *objfile = cu->objfile;
4776
4777 baseaddr = ANOFFSET (objfile->section_offsets,
4778 SECT_OFF_TEXT (objfile));
4779 addrmap_set_empty (objfile->psymtabs_addrmap,
4780 pdi->lowpc + baseaddr,
4781 pdi->highpc - 1 + baseaddr,
4782 cu->per_cu->v.psymtab);
4783 }
4784 }
4785
4786 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4787 {
4788 if (!pdi->is_declaration)
4789 /* Ignore subprogram DIEs that do not have a name, they are
4790 illegal. Do not emit a complaint at this point, we will
4791 do so when we convert this psymtab into a symtab. */
4792 if (pdi->name)
4793 add_partial_symbol (pdi, cu);
4794 }
4795 }
4796
4797 if (! pdi->has_children)
4798 return;
4799
4800 if (cu->language == language_ada)
4801 {
4802 pdi = pdi->die_child;
4803 while (pdi != NULL)
4804 {
4805 fixup_partial_die (pdi, cu);
4806 if (pdi->tag == DW_TAG_subprogram
4807 || pdi->tag == DW_TAG_lexical_block)
4808 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4809 pdi = pdi->die_sibling;
4810 }
4811 }
4812 }
4813
4814 /* Read a partial die corresponding to an enumeration type. */
4815
4816 static void
4817 add_partial_enumeration (struct partial_die_info *enum_pdi,
4818 struct dwarf2_cu *cu)
4819 {
4820 struct partial_die_info *pdi;
4821
4822 if (enum_pdi->name != NULL)
4823 add_partial_symbol (enum_pdi, cu);
4824
4825 pdi = enum_pdi->die_child;
4826 while (pdi)
4827 {
4828 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4829 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4830 else
4831 add_partial_symbol (pdi, cu);
4832 pdi = pdi->die_sibling;
4833 }
4834 }
4835
4836 /* Return the initial uleb128 in the die at INFO_PTR. */
4837
4838 static unsigned int
4839 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4840 {
4841 unsigned int bytes_read;
4842
4843 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4844 }
4845
4846 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4847 Return the corresponding abbrev, or NULL if the number is zero (indicating
4848 an empty DIE). In either case *BYTES_READ will be set to the length of
4849 the initial number. */
4850
4851 static struct abbrev_info *
4852 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4853 struct dwarf2_cu *cu)
4854 {
4855 bfd *abfd = cu->objfile->obfd;
4856 unsigned int abbrev_number;
4857 struct abbrev_info *abbrev;
4858
4859 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4860
4861 if (abbrev_number == 0)
4862 return NULL;
4863
4864 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4865 if (!abbrev)
4866 {
4867 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4868 abbrev_number, bfd_get_filename (abfd));
4869 }
4870
4871 return abbrev;
4872 }
4873
4874 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4875 Returns a pointer to the end of a series of DIEs, terminated by an empty
4876 DIE. Any children of the skipped DIEs will also be skipped. */
4877
4878 static gdb_byte *
4879 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4880 {
4881 struct dwarf2_cu *cu = reader->cu;
4882 struct abbrev_info *abbrev;
4883 unsigned int bytes_read;
4884
4885 while (1)
4886 {
4887 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4888 if (abbrev == NULL)
4889 return info_ptr + bytes_read;
4890 else
4891 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4892 }
4893 }
4894
4895 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4896 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4897 abbrev corresponding to that skipped uleb128 should be passed in
4898 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4899 children. */
4900
4901 static gdb_byte *
4902 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
4903 struct abbrev_info *abbrev)
4904 {
4905 unsigned int bytes_read;
4906 struct attribute attr;
4907 bfd *abfd = reader->abfd;
4908 struct dwarf2_cu *cu = reader->cu;
4909 gdb_byte *buffer = reader->buffer;
4910 unsigned int form, i;
4911
4912 for (i = 0; i < abbrev->num_attrs; i++)
4913 {
4914 /* The only abbrev we care about is DW_AT_sibling. */
4915 if (abbrev->attrs[i].name == DW_AT_sibling)
4916 {
4917 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4918 if (attr.form == DW_FORM_ref_addr)
4919 complaint (&symfile_complaints,
4920 _("ignoring absolute DW_AT_sibling"));
4921 else
4922 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4923 }
4924
4925 /* If it isn't DW_AT_sibling, skip this attribute. */
4926 form = abbrev->attrs[i].form;
4927 skip_attribute:
4928 switch (form)
4929 {
4930 case DW_FORM_ref_addr:
4931 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4932 and later it is offset sized. */
4933 if (cu->header.version == 2)
4934 info_ptr += cu->header.addr_size;
4935 else
4936 info_ptr += cu->header.offset_size;
4937 break;
4938 case DW_FORM_addr:
4939 info_ptr += cu->header.addr_size;
4940 break;
4941 case DW_FORM_data1:
4942 case DW_FORM_ref1:
4943 case DW_FORM_flag:
4944 info_ptr += 1;
4945 break;
4946 case DW_FORM_flag_present:
4947 break;
4948 case DW_FORM_data2:
4949 case DW_FORM_ref2:
4950 info_ptr += 2;
4951 break;
4952 case DW_FORM_data4:
4953 case DW_FORM_ref4:
4954 info_ptr += 4;
4955 break;
4956 case DW_FORM_data8:
4957 case DW_FORM_ref8:
4958 case DW_FORM_ref_sig8:
4959 info_ptr += 8;
4960 break;
4961 case DW_FORM_string:
4962 read_direct_string (abfd, info_ptr, &bytes_read);
4963 info_ptr += bytes_read;
4964 break;
4965 case DW_FORM_sec_offset:
4966 case DW_FORM_strp:
4967 info_ptr += cu->header.offset_size;
4968 break;
4969 case DW_FORM_exprloc:
4970 case DW_FORM_block:
4971 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4972 info_ptr += bytes_read;
4973 break;
4974 case DW_FORM_block1:
4975 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4976 break;
4977 case DW_FORM_block2:
4978 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4979 break;
4980 case DW_FORM_block4:
4981 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4982 break;
4983 case DW_FORM_sdata:
4984 case DW_FORM_udata:
4985 case DW_FORM_ref_udata:
4986 case DW_FORM_GNU_addr_index:
4987 case DW_FORM_GNU_str_index:
4988 info_ptr = skip_leb128 (abfd, info_ptr);
4989 break;
4990 case DW_FORM_indirect:
4991 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4992 info_ptr += bytes_read;
4993 /* We need to continue parsing from here, so just go back to
4994 the top. */
4995 goto skip_attribute;
4996
4997 default:
4998 error (_("Dwarf Error: Cannot handle %s "
4999 "in DWARF reader [in module %s]"),
5000 dwarf_form_name (form),
5001 bfd_get_filename (abfd));
5002 }
5003 }
5004
5005 if (abbrev->has_children)
5006 return skip_children (reader, info_ptr);
5007 else
5008 return info_ptr;
5009 }
5010
5011 /* Locate ORIG_PDI's sibling.
5012 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5013
5014 static gdb_byte *
5015 locate_pdi_sibling (const struct die_reader_specs *reader,
5016 struct partial_die_info *orig_pdi,
5017 gdb_byte *info_ptr)
5018 {
5019 /* Do we know the sibling already? */
5020
5021 if (orig_pdi->sibling)
5022 return orig_pdi->sibling;
5023
5024 /* Are there any children to deal with? */
5025
5026 if (!orig_pdi->has_children)
5027 return info_ptr;
5028
5029 /* Skip the children the long way. */
5030
5031 return skip_children (reader, info_ptr);
5032 }
5033
5034 /* Expand this partial symbol table into a full symbol table. */
5035
5036 static void
5037 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5038 {
5039 if (pst != NULL)
5040 {
5041 if (pst->readin)
5042 {
5043 warning (_("bug: psymtab for %s is already read in."),
5044 pst->filename);
5045 }
5046 else
5047 {
5048 if (info_verbose)
5049 {
5050 printf_filtered (_("Reading in symbols for %s..."),
5051 pst->filename);
5052 gdb_flush (gdb_stdout);
5053 }
5054
5055 /* Restore our global data. */
5056 dwarf2_per_objfile = objfile_data (pst->objfile,
5057 dwarf2_objfile_data_key);
5058
5059 /* If this psymtab is constructed from a debug-only objfile, the
5060 has_section_at_zero flag will not necessarily be correct. We
5061 can get the correct value for this flag by looking at the data
5062 associated with the (presumably stripped) associated objfile. */
5063 if (pst->objfile->separate_debug_objfile_backlink)
5064 {
5065 struct dwarf2_per_objfile *dpo_backlink
5066 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5067 dwarf2_objfile_data_key);
5068
5069 dwarf2_per_objfile->has_section_at_zero
5070 = dpo_backlink->has_section_at_zero;
5071 }
5072
5073 dwarf2_per_objfile->reading_partial_symbols = 0;
5074
5075 psymtab_to_symtab_1 (pst);
5076
5077 /* Finish up the debug error message. */
5078 if (info_verbose)
5079 printf_filtered (_("done.\n"));
5080 }
5081 }
5082 }
5083 \f
5084 /* Reading in full CUs. */
5085
5086 /* Add PER_CU to the queue. */
5087
5088 static void
5089 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
5090 {
5091 struct dwarf2_queue_item *item;
5092
5093 per_cu->queued = 1;
5094 item = xmalloc (sizeof (*item));
5095 item->per_cu = per_cu;
5096 item->next = NULL;
5097
5098 if (dwarf2_queue == NULL)
5099 dwarf2_queue = item;
5100 else
5101 dwarf2_queue_tail->next = item;
5102
5103 dwarf2_queue_tail = item;
5104 }
5105
5106 /* Process the queue. */
5107
5108 static void
5109 process_queue (void)
5110 {
5111 struct dwarf2_queue_item *item, *next_item;
5112
5113 /* The queue starts out with one item, but following a DIE reference
5114 may load a new CU, adding it to the end of the queue. */
5115 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5116 {
5117 if (dwarf2_per_objfile->using_index
5118 ? !item->per_cu->v.quick->symtab
5119 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5120 process_full_comp_unit (item->per_cu);
5121
5122 item->per_cu->queued = 0;
5123 next_item = item->next;
5124 xfree (item);
5125 }
5126
5127 dwarf2_queue_tail = NULL;
5128 }
5129
5130 /* Free all allocated queue entries. This function only releases anything if
5131 an error was thrown; if the queue was processed then it would have been
5132 freed as we went along. */
5133
5134 static void
5135 dwarf2_release_queue (void *dummy)
5136 {
5137 struct dwarf2_queue_item *item, *last;
5138
5139 item = dwarf2_queue;
5140 while (item)
5141 {
5142 /* Anything still marked queued is likely to be in an
5143 inconsistent state, so discard it. */
5144 if (item->per_cu->queued)
5145 {
5146 if (item->per_cu->cu != NULL)
5147 free_one_cached_comp_unit (item->per_cu);
5148 item->per_cu->queued = 0;
5149 }
5150
5151 last = item;
5152 item = item->next;
5153 xfree (last);
5154 }
5155
5156 dwarf2_queue = dwarf2_queue_tail = NULL;
5157 }
5158
5159 /* Read in full symbols for PST, and anything it depends on. */
5160
5161 static void
5162 psymtab_to_symtab_1 (struct partial_symtab *pst)
5163 {
5164 struct dwarf2_per_cu_data *per_cu;
5165 struct cleanup *back_to;
5166 int i;
5167
5168 for (i = 0; i < pst->number_of_dependencies; i++)
5169 if (!pst->dependencies[i]->readin)
5170 {
5171 /* Inform about additional files that need to be read in. */
5172 if (info_verbose)
5173 {
5174 /* FIXME: i18n: Need to make this a single string. */
5175 fputs_filtered (" ", gdb_stdout);
5176 wrap_here ("");
5177 fputs_filtered ("and ", gdb_stdout);
5178 wrap_here ("");
5179 printf_filtered ("%s...", pst->dependencies[i]->filename);
5180 wrap_here (""); /* Flush output. */
5181 gdb_flush (gdb_stdout);
5182 }
5183 psymtab_to_symtab_1 (pst->dependencies[i]);
5184 }
5185
5186 per_cu = pst->read_symtab_private;
5187
5188 if (per_cu == NULL)
5189 {
5190 /* It's an include file, no symbols to read for it.
5191 Everything is in the parent symtab. */
5192 pst->readin = 1;
5193 return;
5194 }
5195
5196 dw2_do_instantiate_symtab (per_cu);
5197 }
5198
5199 /* Trivial hash function for die_info: the hash value of a DIE
5200 is its offset in .debug_info for this objfile. */
5201
5202 static hashval_t
5203 die_hash (const void *item)
5204 {
5205 const struct die_info *die = item;
5206
5207 return die->offset.sect_off;
5208 }
5209
5210 /* Trivial comparison function for die_info structures: two DIEs
5211 are equal if they have the same offset. */
5212
5213 static int
5214 die_eq (const void *item_lhs, const void *item_rhs)
5215 {
5216 const struct die_info *die_lhs = item_lhs;
5217 const struct die_info *die_rhs = item_rhs;
5218
5219 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5220 }
5221
5222 /* die_reader_func for load_full_comp_unit.
5223 This is identical to read_signatured_type_reader,
5224 but is kept separate for now. */
5225
5226 static void
5227 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5228 gdb_byte *info_ptr,
5229 struct die_info *comp_unit_die,
5230 int has_children,
5231 void *data)
5232 {
5233 struct dwarf2_cu *cu = reader->cu;
5234 struct attribute *attr;
5235
5236 gdb_assert (cu->die_hash == NULL);
5237 cu->die_hash =
5238 htab_create_alloc_ex (cu->header.length / 12,
5239 die_hash,
5240 die_eq,
5241 NULL,
5242 &cu->comp_unit_obstack,
5243 hashtab_obstack_allocate,
5244 dummy_obstack_deallocate);
5245
5246 if (has_children)
5247 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5248 &info_ptr, comp_unit_die);
5249 cu->dies = comp_unit_die;
5250 /* comp_unit_die is not stored in die_hash, no need. */
5251
5252 /* We try not to read any attributes in this function, because not
5253 all CUs needed for references have been loaded yet, and symbol
5254 table processing isn't initialized. But we have to set the CU language,
5255 or we won't be able to build types correctly.
5256 Similarly, if we do not read the producer, we can not apply
5257 producer-specific interpretation. */
5258 prepare_one_comp_unit (cu, cu->dies);
5259 }
5260
5261 /* Load the DIEs associated with PER_CU into memory. */
5262
5263 static void
5264 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu)
5265 {
5266 gdb_assert (! this_cu->is_debug_types);
5267
5268 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader, NULL);
5269 }
5270
5271 /* Add a DIE to the delayed physname list. */
5272
5273 static void
5274 add_to_method_list (struct type *type, int fnfield_index, int index,
5275 const char *name, struct die_info *die,
5276 struct dwarf2_cu *cu)
5277 {
5278 struct delayed_method_info mi;
5279 mi.type = type;
5280 mi.fnfield_index = fnfield_index;
5281 mi.index = index;
5282 mi.name = name;
5283 mi.die = die;
5284 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5285 }
5286
5287 /* A cleanup for freeing the delayed method list. */
5288
5289 static void
5290 free_delayed_list (void *ptr)
5291 {
5292 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5293 if (cu->method_list != NULL)
5294 {
5295 VEC_free (delayed_method_info, cu->method_list);
5296 cu->method_list = NULL;
5297 }
5298 }
5299
5300 /* Compute the physnames of any methods on the CU's method list.
5301
5302 The computation of method physnames is delayed in order to avoid the
5303 (bad) condition that one of the method's formal parameters is of an as yet
5304 incomplete type. */
5305
5306 static void
5307 compute_delayed_physnames (struct dwarf2_cu *cu)
5308 {
5309 int i;
5310 struct delayed_method_info *mi;
5311 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5312 {
5313 const char *physname;
5314 struct fn_fieldlist *fn_flp
5315 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5316 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5317 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5318 }
5319 }
5320
5321 /* Go objects should be embedded in a DW_TAG_module DIE,
5322 and it's not clear if/how imported objects will appear.
5323 To keep Go support simple until that's worked out,
5324 go back through what we've read and create something usable.
5325 We could do this while processing each DIE, and feels kinda cleaner,
5326 but that way is more invasive.
5327 This is to, for example, allow the user to type "p var" or "b main"
5328 without having to specify the package name, and allow lookups
5329 of module.object to work in contexts that use the expression
5330 parser. */
5331
5332 static void
5333 fixup_go_packaging (struct dwarf2_cu *cu)
5334 {
5335 char *package_name = NULL;
5336 struct pending *list;
5337 int i;
5338
5339 for (list = global_symbols; list != NULL; list = list->next)
5340 {
5341 for (i = 0; i < list->nsyms; ++i)
5342 {
5343 struct symbol *sym = list->symbol[i];
5344
5345 if (SYMBOL_LANGUAGE (sym) == language_go
5346 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5347 {
5348 char *this_package_name = go_symbol_package_name (sym);
5349
5350 if (this_package_name == NULL)
5351 continue;
5352 if (package_name == NULL)
5353 package_name = this_package_name;
5354 else
5355 {
5356 if (strcmp (package_name, this_package_name) != 0)
5357 complaint (&symfile_complaints,
5358 _("Symtab %s has objects from two different Go packages: %s and %s"),
5359 (sym->symtab && sym->symtab->filename
5360 ? sym->symtab->filename
5361 : cu->objfile->name),
5362 this_package_name, package_name);
5363 xfree (this_package_name);
5364 }
5365 }
5366 }
5367 }
5368
5369 if (package_name != NULL)
5370 {
5371 struct objfile *objfile = cu->objfile;
5372 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5373 package_name, objfile);
5374 struct symbol *sym;
5375
5376 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5377
5378 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5379 SYMBOL_SET_LANGUAGE (sym, language_go);
5380 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5381 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5382 e.g., "main" finds the "main" module and not C's main(). */
5383 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5384 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5385 SYMBOL_TYPE (sym) = type;
5386
5387 add_symbol_to_list (sym, &global_symbols);
5388
5389 xfree (package_name);
5390 }
5391 }
5392
5393 /* Generate full symbol information for PER_CU, whose DIEs have
5394 already been loaded into memory. */
5395
5396 static void
5397 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
5398 {
5399 struct dwarf2_cu *cu = per_cu->cu;
5400 struct objfile *objfile = per_cu->objfile;
5401 CORE_ADDR lowpc, highpc;
5402 struct symtab *symtab;
5403 struct cleanup *back_to, *delayed_list_cleanup;
5404 CORE_ADDR baseaddr;
5405
5406 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5407
5408 buildsym_init ();
5409 back_to = make_cleanup (really_free_pendings, NULL);
5410 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5411
5412 cu->list_in_scope = &file_symbols;
5413
5414 /* Do line number decoding in read_file_scope () */
5415 process_die (cu->dies, cu);
5416
5417 /* For now fudge the Go package. */
5418 if (cu->language == language_go)
5419 fixup_go_packaging (cu);
5420
5421 /* Now that we have processed all the DIEs in the CU, all the types
5422 should be complete, and it should now be safe to compute all of the
5423 physnames. */
5424 compute_delayed_physnames (cu);
5425 do_cleanups (delayed_list_cleanup);
5426
5427 /* Some compilers don't define a DW_AT_high_pc attribute for the
5428 compilation unit. If the DW_AT_high_pc is missing, synthesize
5429 it, by scanning the DIE's below the compilation unit. */
5430 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5431
5432 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5433
5434 if (symtab != NULL)
5435 {
5436 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5437
5438 /* Set symtab language to language from DW_AT_language. If the
5439 compilation is from a C file generated by language preprocessors, do
5440 not set the language if it was already deduced by start_subfile. */
5441 if (!(cu->language == language_c && symtab->language != language_c))
5442 symtab->language = cu->language;
5443
5444 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5445 produce DW_AT_location with location lists but it can be possibly
5446 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5447 there were bugs in prologue debug info, fixed later in GCC-4.5
5448 by "unwind info for epilogues" patch (which is not directly related).
5449
5450 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5451 needed, it would be wrong due to missing DW_AT_producer there.
5452
5453 Still one can confuse GDB by using non-standard GCC compilation
5454 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5455 */
5456 if (cu->has_loclist && gcc_4_minor >= 5)
5457 symtab->locations_valid = 1;
5458
5459 if (gcc_4_minor >= 5)
5460 symtab->epilogue_unwind_valid = 1;
5461
5462 symtab->call_site_htab = cu->call_site_htab;
5463 }
5464
5465 if (dwarf2_per_objfile->using_index)
5466 per_cu->v.quick->symtab = symtab;
5467 else
5468 {
5469 struct partial_symtab *pst = per_cu->v.psymtab;
5470 pst->symtab = symtab;
5471 pst->readin = 1;
5472 }
5473
5474 do_cleanups (back_to);
5475 }
5476
5477 /* Process a die and its children. */
5478
5479 static void
5480 process_die (struct die_info *die, struct dwarf2_cu *cu)
5481 {
5482 switch (die->tag)
5483 {
5484 case DW_TAG_padding:
5485 break;
5486 case DW_TAG_compile_unit:
5487 read_file_scope (die, cu);
5488 break;
5489 case DW_TAG_type_unit:
5490 read_type_unit_scope (die, cu);
5491 break;
5492 case DW_TAG_subprogram:
5493 case DW_TAG_inlined_subroutine:
5494 read_func_scope (die, cu);
5495 break;
5496 case DW_TAG_lexical_block:
5497 case DW_TAG_try_block:
5498 case DW_TAG_catch_block:
5499 read_lexical_block_scope (die, cu);
5500 break;
5501 case DW_TAG_GNU_call_site:
5502 read_call_site_scope (die, cu);
5503 break;
5504 case DW_TAG_class_type:
5505 case DW_TAG_interface_type:
5506 case DW_TAG_structure_type:
5507 case DW_TAG_union_type:
5508 process_structure_scope (die, cu);
5509 break;
5510 case DW_TAG_enumeration_type:
5511 process_enumeration_scope (die, cu);
5512 break;
5513
5514 /* These dies have a type, but processing them does not create
5515 a symbol or recurse to process the children. Therefore we can
5516 read them on-demand through read_type_die. */
5517 case DW_TAG_subroutine_type:
5518 case DW_TAG_set_type:
5519 case DW_TAG_array_type:
5520 case DW_TAG_pointer_type:
5521 case DW_TAG_ptr_to_member_type:
5522 case DW_TAG_reference_type:
5523 case DW_TAG_string_type:
5524 break;
5525
5526 case DW_TAG_base_type:
5527 case DW_TAG_subrange_type:
5528 case DW_TAG_typedef:
5529 /* Add a typedef symbol for the type definition, if it has a
5530 DW_AT_name. */
5531 new_symbol (die, read_type_die (die, cu), cu);
5532 break;
5533 case DW_TAG_common_block:
5534 read_common_block (die, cu);
5535 break;
5536 case DW_TAG_common_inclusion:
5537 break;
5538 case DW_TAG_namespace:
5539 processing_has_namespace_info = 1;
5540 read_namespace (die, cu);
5541 break;
5542 case DW_TAG_module:
5543 processing_has_namespace_info = 1;
5544 read_module (die, cu);
5545 break;
5546 case DW_TAG_imported_declaration:
5547 case DW_TAG_imported_module:
5548 processing_has_namespace_info = 1;
5549 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5550 || cu->language != language_fortran))
5551 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5552 dwarf_tag_name (die->tag));
5553 read_import_statement (die, cu);
5554 break;
5555 default:
5556 new_symbol (die, NULL, cu);
5557 break;
5558 }
5559 }
5560
5561 /* A helper function for dwarf2_compute_name which determines whether DIE
5562 needs to have the name of the scope prepended to the name listed in the
5563 die. */
5564
5565 static int
5566 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5567 {
5568 struct attribute *attr;
5569
5570 switch (die->tag)
5571 {
5572 case DW_TAG_namespace:
5573 case DW_TAG_typedef:
5574 case DW_TAG_class_type:
5575 case DW_TAG_interface_type:
5576 case DW_TAG_structure_type:
5577 case DW_TAG_union_type:
5578 case DW_TAG_enumeration_type:
5579 case DW_TAG_enumerator:
5580 case DW_TAG_subprogram:
5581 case DW_TAG_member:
5582 return 1;
5583
5584 case DW_TAG_variable:
5585 case DW_TAG_constant:
5586 /* We only need to prefix "globally" visible variables. These include
5587 any variable marked with DW_AT_external or any variable that
5588 lives in a namespace. [Variables in anonymous namespaces
5589 require prefixing, but they are not DW_AT_external.] */
5590
5591 if (dwarf2_attr (die, DW_AT_specification, cu))
5592 {
5593 struct dwarf2_cu *spec_cu = cu;
5594
5595 return die_needs_namespace (die_specification (die, &spec_cu),
5596 spec_cu);
5597 }
5598
5599 attr = dwarf2_attr (die, DW_AT_external, cu);
5600 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5601 && die->parent->tag != DW_TAG_module)
5602 return 0;
5603 /* A variable in a lexical block of some kind does not need a
5604 namespace, even though in C++ such variables may be external
5605 and have a mangled name. */
5606 if (die->parent->tag == DW_TAG_lexical_block
5607 || die->parent->tag == DW_TAG_try_block
5608 || die->parent->tag == DW_TAG_catch_block
5609 || die->parent->tag == DW_TAG_subprogram)
5610 return 0;
5611 return 1;
5612
5613 default:
5614 return 0;
5615 }
5616 }
5617
5618 /* Retrieve the last character from a mem_file. */
5619
5620 static void
5621 do_ui_file_peek_last (void *object, const char *buffer, long length)
5622 {
5623 char *last_char_p = (char *) object;
5624
5625 if (length > 0)
5626 *last_char_p = buffer[length - 1];
5627 }
5628
5629 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5630 compute the physname for the object, which include a method's:
5631 - formal parameters (C++/Java),
5632 - receiver type (Go),
5633 - return type (Java).
5634
5635 The term "physname" is a bit confusing.
5636 For C++, for example, it is the demangled name.
5637 For Go, for example, it's the mangled name.
5638
5639 For Ada, return the DIE's linkage name rather than the fully qualified
5640 name. PHYSNAME is ignored..
5641
5642 The result is allocated on the objfile_obstack and canonicalized. */
5643
5644 static const char *
5645 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5646 int physname)
5647 {
5648 struct objfile *objfile = cu->objfile;
5649
5650 if (name == NULL)
5651 name = dwarf2_name (die, cu);
5652
5653 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5654 compute it by typename_concat inside GDB. */
5655 if (cu->language == language_ada
5656 || (cu->language == language_fortran && physname))
5657 {
5658 /* For Ada unit, we prefer the linkage name over the name, as
5659 the former contains the exported name, which the user expects
5660 to be able to reference. Ideally, we want the user to be able
5661 to reference this entity using either natural or linkage name,
5662 but we haven't started looking at this enhancement yet. */
5663 struct attribute *attr;
5664
5665 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5666 if (attr == NULL)
5667 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5668 if (attr && DW_STRING (attr))
5669 return DW_STRING (attr);
5670 }
5671
5672 /* These are the only languages we know how to qualify names in. */
5673 if (name != NULL
5674 && (cu->language == language_cplus || cu->language == language_java
5675 || cu->language == language_fortran))
5676 {
5677 if (die_needs_namespace (die, cu))
5678 {
5679 long length;
5680 const char *prefix;
5681 struct ui_file *buf;
5682
5683 prefix = determine_prefix (die, cu);
5684 buf = mem_fileopen ();
5685 if (*prefix != '\0')
5686 {
5687 char *prefixed_name = typename_concat (NULL, prefix, name,
5688 physname, cu);
5689
5690 fputs_unfiltered (prefixed_name, buf);
5691 xfree (prefixed_name);
5692 }
5693 else
5694 fputs_unfiltered (name, buf);
5695
5696 /* Template parameters may be specified in the DIE's DW_AT_name, or
5697 as children with DW_TAG_template_type_param or
5698 DW_TAG_value_type_param. If the latter, add them to the name
5699 here. If the name already has template parameters, then
5700 skip this step; some versions of GCC emit both, and
5701 it is more efficient to use the pre-computed name.
5702
5703 Something to keep in mind about this process: it is very
5704 unlikely, or in some cases downright impossible, to produce
5705 something that will match the mangled name of a function.
5706 If the definition of the function has the same debug info,
5707 we should be able to match up with it anyway. But fallbacks
5708 using the minimal symbol, for instance to find a method
5709 implemented in a stripped copy of libstdc++, will not work.
5710 If we do not have debug info for the definition, we will have to
5711 match them up some other way.
5712
5713 When we do name matching there is a related problem with function
5714 templates; two instantiated function templates are allowed to
5715 differ only by their return types, which we do not add here. */
5716
5717 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5718 {
5719 struct attribute *attr;
5720 struct die_info *child;
5721 int first = 1;
5722
5723 die->building_fullname = 1;
5724
5725 for (child = die->child; child != NULL; child = child->sibling)
5726 {
5727 struct type *type;
5728 LONGEST value;
5729 gdb_byte *bytes;
5730 struct dwarf2_locexpr_baton *baton;
5731 struct value *v;
5732
5733 if (child->tag != DW_TAG_template_type_param
5734 && child->tag != DW_TAG_template_value_param)
5735 continue;
5736
5737 if (first)
5738 {
5739 fputs_unfiltered ("<", buf);
5740 first = 0;
5741 }
5742 else
5743 fputs_unfiltered (", ", buf);
5744
5745 attr = dwarf2_attr (child, DW_AT_type, cu);
5746 if (attr == NULL)
5747 {
5748 complaint (&symfile_complaints,
5749 _("template parameter missing DW_AT_type"));
5750 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5751 continue;
5752 }
5753 type = die_type (child, cu);
5754
5755 if (child->tag == DW_TAG_template_type_param)
5756 {
5757 c_print_type (type, "", buf, -1, 0);
5758 continue;
5759 }
5760
5761 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5762 if (attr == NULL)
5763 {
5764 complaint (&symfile_complaints,
5765 _("template parameter missing "
5766 "DW_AT_const_value"));
5767 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5768 continue;
5769 }
5770
5771 dwarf2_const_value_attr (attr, type, name,
5772 &cu->comp_unit_obstack, cu,
5773 &value, &bytes, &baton);
5774
5775 if (TYPE_NOSIGN (type))
5776 /* GDB prints characters as NUMBER 'CHAR'. If that's
5777 changed, this can use value_print instead. */
5778 c_printchar (value, type, buf);
5779 else
5780 {
5781 struct value_print_options opts;
5782
5783 if (baton != NULL)
5784 v = dwarf2_evaluate_loc_desc (type, NULL,
5785 baton->data,
5786 baton->size,
5787 baton->per_cu);
5788 else if (bytes != NULL)
5789 {
5790 v = allocate_value (type);
5791 memcpy (value_contents_writeable (v), bytes,
5792 TYPE_LENGTH (type));
5793 }
5794 else
5795 v = value_from_longest (type, value);
5796
5797 /* Specify decimal so that we do not depend on
5798 the radix. */
5799 get_formatted_print_options (&opts, 'd');
5800 opts.raw = 1;
5801 value_print (v, buf, &opts);
5802 release_value (v);
5803 value_free (v);
5804 }
5805 }
5806
5807 die->building_fullname = 0;
5808
5809 if (!first)
5810 {
5811 /* Close the argument list, with a space if necessary
5812 (nested templates). */
5813 char last_char = '\0';
5814 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5815 if (last_char == '>')
5816 fputs_unfiltered (" >", buf);
5817 else
5818 fputs_unfiltered (">", buf);
5819 }
5820 }
5821
5822 /* For Java and C++ methods, append formal parameter type
5823 information, if PHYSNAME. */
5824
5825 if (physname && die->tag == DW_TAG_subprogram
5826 && (cu->language == language_cplus
5827 || cu->language == language_java))
5828 {
5829 struct type *type = read_type_die (die, cu);
5830
5831 c_type_print_args (type, buf, 1, cu->language);
5832
5833 if (cu->language == language_java)
5834 {
5835 /* For java, we must append the return type to method
5836 names. */
5837 if (die->tag == DW_TAG_subprogram)
5838 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5839 0, 0);
5840 }
5841 else if (cu->language == language_cplus)
5842 {
5843 /* Assume that an artificial first parameter is
5844 "this", but do not crash if it is not. RealView
5845 marks unnamed (and thus unused) parameters as
5846 artificial; there is no way to differentiate
5847 the two cases. */
5848 if (TYPE_NFIELDS (type) > 0
5849 && TYPE_FIELD_ARTIFICIAL (type, 0)
5850 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5851 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5852 0))))
5853 fputs_unfiltered (" const", buf);
5854 }
5855 }
5856
5857 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5858 &length);
5859 ui_file_delete (buf);
5860
5861 if (cu->language == language_cplus)
5862 {
5863 char *cname
5864 = dwarf2_canonicalize_name (name, cu,
5865 &objfile->objfile_obstack);
5866
5867 if (cname != NULL)
5868 name = cname;
5869 }
5870 }
5871 }
5872
5873 return name;
5874 }
5875
5876 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5877 If scope qualifiers are appropriate they will be added. The result
5878 will be allocated on the objfile_obstack, or NULL if the DIE does
5879 not have a name. NAME may either be from a previous call to
5880 dwarf2_name or NULL.
5881
5882 The output string will be canonicalized (if C++/Java). */
5883
5884 static const char *
5885 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5886 {
5887 return dwarf2_compute_name (name, die, cu, 0);
5888 }
5889
5890 /* Construct a physname for the given DIE in CU. NAME may either be
5891 from a previous call to dwarf2_name or NULL. The result will be
5892 allocated on the objfile_objstack or NULL if the DIE does not have a
5893 name.
5894
5895 The output string will be canonicalized (if C++/Java). */
5896
5897 static const char *
5898 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5899 {
5900 struct objfile *objfile = cu->objfile;
5901 struct attribute *attr;
5902 const char *retval, *mangled = NULL, *canon = NULL;
5903 struct cleanup *back_to;
5904 int need_copy = 1;
5905
5906 /* In this case dwarf2_compute_name is just a shortcut not building anything
5907 on its own. */
5908 if (!die_needs_namespace (die, cu))
5909 return dwarf2_compute_name (name, die, cu, 1);
5910
5911 back_to = make_cleanup (null_cleanup, NULL);
5912
5913 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5914 if (!attr)
5915 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5916
5917 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5918 has computed. */
5919 if (attr && DW_STRING (attr))
5920 {
5921 char *demangled;
5922
5923 mangled = DW_STRING (attr);
5924
5925 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5926 type. It is easier for GDB users to search for such functions as
5927 `name(params)' than `long name(params)'. In such case the minimal
5928 symbol names do not match the full symbol names but for template
5929 functions there is never a need to look up their definition from their
5930 declaration so the only disadvantage remains the minimal symbol
5931 variant `long name(params)' does not have the proper inferior type.
5932 */
5933
5934 if (cu->language == language_go)
5935 {
5936 /* This is a lie, but we already lie to the caller new_symbol_full.
5937 new_symbol_full assumes we return the mangled name.
5938 This just undoes that lie until things are cleaned up. */
5939 demangled = NULL;
5940 }
5941 else
5942 {
5943 demangled = cplus_demangle (mangled,
5944 (DMGL_PARAMS | DMGL_ANSI
5945 | (cu->language == language_java
5946 ? DMGL_JAVA | DMGL_RET_POSTFIX
5947 : DMGL_RET_DROP)));
5948 }
5949 if (demangled)
5950 {
5951 make_cleanup (xfree, demangled);
5952 canon = demangled;
5953 }
5954 else
5955 {
5956 canon = mangled;
5957 need_copy = 0;
5958 }
5959 }
5960
5961 if (canon == NULL || check_physname)
5962 {
5963 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5964
5965 if (canon != NULL && strcmp (physname, canon) != 0)
5966 {
5967 /* It may not mean a bug in GDB. The compiler could also
5968 compute DW_AT_linkage_name incorrectly. But in such case
5969 GDB would need to be bug-to-bug compatible. */
5970
5971 complaint (&symfile_complaints,
5972 _("Computed physname <%s> does not match demangled <%s> "
5973 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5974 physname, canon, mangled, die->offset.sect_off, objfile->name);
5975
5976 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5977 is available here - over computed PHYSNAME. It is safer
5978 against both buggy GDB and buggy compilers. */
5979
5980 retval = canon;
5981 }
5982 else
5983 {
5984 retval = physname;
5985 need_copy = 0;
5986 }
5987 }
5988 else
5989 retval = canon;
5990
5991 if (need_copy)
5992 retval = obsavestring (retval, strlen (retval),
5993 &objfile->objfile_obstack);
5994
5995 do_cleanups (back_to);
5996 return retval;
5997 }
5998
5999 /* Read the import statement specified by the given die and record it. */
6000
6001 static void
6002 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6003 {
6004 struct objfile *objfile = cu->objfile;
6005 struct attribute *import_attr;
6006 struct die_info *imported_die, *child_die;
6007 struct dwarf2_cu *imported_cu;
6008 const char *imported_name;
6009 const char *imported_name_prefix;
6010 const char *canonical_name;
6011 const char *import_alias;
6012 const char *imported_declaration = NULL;
6013 const char *import_prefix;
6014 VEC (const_char_ptr) *excludes = NULL;
6015 struct cleanup *cleanups;
6016
6017 char *temp;
6018
6019 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6020 if (import_attr == NULL)
6021 {
6022 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6023 dwarf_tag_name (die->tag));
6024 return;
6025 }
6026
6027 imported_cu = cu;
6028 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6029 imported_name = dwarf2_name (imported_die, imported_cu);
6030 if (imported_name == NULL)
6031 {
6032 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6033
6034 The import in the following code:
6035 namespace A
6036 {
6037 typedef int B;
6038 }
6039
6040 int main ()
6041 {
6042 using A::B;
6043 B b;
6044 return b;
6045 }
6046
6047 ...
6048 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6049 <52> DW_AT_decl_file : 1
6050 <53> DW_AT_decl_line : 6
6051 <54> DW_AT_import : <0x75>
6052 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6053 <59> DW_AT_name : B
6054 <5b> DW_AT_decl_file : 1
6055 <5c> DW_AT_decl_line : 2
6056 <5d> DW_AT_type : <0x6e>
6057 ...
6058 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6059 <76> DW_AT_byte_size : 4
6060 <77> DW_AT_encoding : 5 (signed)
6061
6062 imports the wrong die ( 0x75 instead of 0x58 ).
6063 This case will be ignored until the gcc bug is fixed. */
6064 return;
6065 }
6066
6067 /* Figure out the local name after import. */
6068 import_alias = dwarf2_name (die, cu);
6069
6070 /* Figure out where the statement is being imported to. */
6071 import_prefix = determine_prefix (die, cu);
6072
6073 /* Figure out what the scope of the imported die is and prepend it
6074 to the name of the imported die. */
6075 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6076
6077 if (imported_die->tag != DW_TAG_namespace
6078 && imported_die->tag != DW_TAG_module)
6079 {
6080 imported_declaration = imported_name;
6081 canonical_name = imported_name_prefix;
6082 }
6083 else if (strlen (imported_name_prefix) > 0)
6084 {
6085 temp = alloca (strlen (imported_name_prefix)
6086 + 2 + strlen (imported_name) + 1);
6087 strcpy (temp, imported_name_prefix);
6088 strcat (temp, "::");
6089 strcat (temp, imported_name);
6090 canonical_name = temp;
6091 }
6092 else
6093 canonical_name = imported_name;
6094
6095 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6096
6097 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6098 for (child_die = die->child; child_die && child_die->tag;
6099 child_die = sibling_die (child_die))
6100 {
6101 /* DWARF-4: A Fortran use statement with a “rename list” may be
6102 represented by an imported module entry with an import attribute
6103 referring to the module and owned entries corresponding to those
6104 entities that are renamed as part of being imported. */
6105
6106 if (child_die->tag != DW_TAG_imported_declaration)
6107 {
6108 complaint (&symfile_complaints,
6109 _("child DW_TAG_imported_declaration expected "
6110 "- DIE at 0x%x [in module %s]"),
6111 child_die->offset.sect_off, objfile->name);
6112 continue;
6113 }
6114
6115 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6116 if (import_attr == NULL)
6117 {
6118 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6119 dwarf_tag_name (child_die->tag));
6120 continue;
6121 }
6122
6123 imported_cu = cu;
6124 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6125 &imported_cu);
6126 imported_name = dwarf2_name (imported_die, imported_cu);
6127 if (imported_name == NULL)
6128 {
6129 complaint (&symfile_complaints,
6130 _("child DW_TAG_imported_declaration has unknown "
6131 "imported name - DIE at 0x%x [in module %s]"),
6132 child_die->offset.sect_off, objfile->name);
6133 continue;
6134 }
6135
6136 VEC_safe_push (const_char_ptr, excludes, imported_name);
6137
6138 process_die (child_die, cu);
6139 }
6140
6141 cp_add_using_directive (import_prefix,
6142 canonical_name,
6143 import_alias,
6144 imported_declaration,
6145 excludes,
6146 &objfile->objfile_obstack);
6147
6148 do_cleanups (cleanups);
6149 }
6150
6151 /* Cleanup function for read_file_scope. */
6152
6153 static void
6154 free_cu_line_header (void *arg)
6155 {
6156 struct dwarf2_cu *cu = arg;
6157
6158 free_line_header (cu->line_header);
6159 cu->line_header = NULL;
6160 }
6161
6162 static void
6163 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6164 char **name, char **comp_dir)
6165 {
6166 struct attribute *attr;
6167
6168 *name = NULL;
6169 *comp_dir = NULL;
6170
6171 /* Find the filename. Do not use dwarf2_name here, since the filename
6172 is not a source language identifier. */
6173 attr = dwarf2_attr (die, DW_AT_name, cu);
6174 if (attr)
6175 {
6176 *name = DW_STRING (attr);
6177 }
6178
6179 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6180 if (attr)
6181 *comp_dir = DW_STRING (attr);
6182 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6183 {
6184 *comp_dir = ldirname (*name);
6185 if (*comp_dir != NULL)
6186 make_cleanup (xfree, *comp_dir);
6187 }
6188 if (*comp_dir != NULL)
6189 {
6190 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6191 directory, get rid of it. */
6192 char *cp = strchr (*comp_dir, ':');
6193
6194 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6195 *comp_dir = cp + 1;
6196 }
6197
6198 if (*name == NULL)
6199 *name = "<unknown>";
6200 }
6201
6202 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6203 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6204 COMP_DIR is the compilation directory.
6205 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6206
6207 static void
6208 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6209 const char *comp_dir, int want_line_info)
6210 {
6211 struct attribute *attr;
6212
6213 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6214 if (attr)
6215 {
6216 unsigned int line_offset = DW_UNSND (attr);
6217 struct line_header *line_header
6218 = dwarf_decode_line_header (line_offset, cu);
6219
6220 if (line_header)
6221 {
6222 cu->line_header = line_header;
6223 make_cleanup (free_cu_line_header, cu);
6224 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6225 }
6226 }
6227 }
6228
6229 /* Process DW_TAG_compile_unit. */
6230
6231 static void
6232 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6233 {
6234 struct objfile *objfile = dwarf2_per_objfile->objfile;
6235 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6236 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6237 CORE_ADDR highpc = ((CORE_ADDR) 0);
6238 struct attribute *attr;
6239 char *name = NULL;
6240 char *comp_dir = NULL;
6241 struct die_info *child_die;
6242 bfd *abfd = objfile->obfd;
6243 CORE_ADDR baseaddr;
6244
6245 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6246
6247 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6248
6249 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6250 from finish_block. */
6251 if (lowpc == ((CORE_ADDR) -1))
6252 lowpc = highpc;
6253 lowpc += baseaddr;
6254 highpc += baseaddr;
6255
6256 find_file_and_directory (die, cu, &name, &comp_dir);
6257
6258 prepare_one_comp_unit (cu, die);
6259
6260 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6261 standardised yet. As a workaround for the language detection we fall
6262 back to the DW_AT_producer string. */
6263 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6264 cu->language = language_opencl;
6265
6266 /* Similar hack for Go. */
6267 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6268 set_cu_language (DW_LANG_Go, cu);
6269
6270 /* We assume that we're processing GCC output. */
6271 processing_gcc_compilation = 2;
6272
6273 processing_has_namespace_info = 0;
6274
6275 start_symtab (name, comp_dir, lowpc);
6276 record_debugformat ("DWARF 2");
6277 record_producer (cu->producer);
6278
6279 /* Decode line number information if present. We do this before
6280 processing child DIEs, so that the line header table is available
6281 for DW_AT_decl_file. */
6282 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6283
6284 /* Process all dies in compilation unit. */
6285 if (die->child != NULL)
6286 {
6287 child_die = die->child;
6288 while (child_die && child_die->tag)
6289 {
6290 process_die (child_die, cu);
6291 child_die = sibling_die (child_die);
6292 }
6293 }
6294
6295 /* Decode macro information, if present. Dwarf 2 macro information
6296 refers to information in the line number info statement program
6297 header, so we can only read it if we've read the header
6298 successfully. */
6299 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6300 if (attr && cu->line_header)
6301 {
6302 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6303 complaint (&symfile_complaints,
6304 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6305
6306 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6307 comp_dir, abfd, cu,
6308 &dwarf2_per_objfile->macro, 1,
6309 ".debug_macro");
6310 }
6311 else
6312 {
6313 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6314 if (attr && cu->line_header)
6315 {
6316 unsigned int macro_offset = DW_UNSND (attr);
6317
6318 dwarf_decode_macros (cu->line_header, macro_offset,
6319 comp_dir, abfd, cu,
6320 &dwarf2_per_objfile->macinfo, 0,
6321 ".debug_macinfo");
6322 }
6323 }
6324
6325 do_cleanups (back_to);
6326 }
6327
6328 /* Process DW_TAG_type_unit.
6329 For TUs we want to skip the first top level sibling if it's not the
6330 actual type being defined by this TU. In this case the first top
6331 level sibling is there to provide context only. */
6332
6333 static void
6334 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6335 {
6336 struct objfile *objfile = cu->objfile;
6337 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6338 CORE_ADDR lowpc;
6339 struct attribute *attr;
6340 char *name = NULL;
6341 char *comp_dir = NULL;
6342 struct die_info *child_die;
6343 bfd *abfd = objfile->obfd;
6344
6345 /* start_symtab needs a low pc, but we don't really have one.
6346 Do what read_file_scope would do in the absence of such info. */
6347 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6348
6349 /* Find the filename. Do not use dwarf2_name here, since the filename
6350 is not a source language identifier. */
6351 attr = dwarf2_attr (die, DW_AT_name, cu);
6352 if (attr)
6353 name = DW_STRING (attr);
6354
6355 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6356 if (attr)
6357 comp_dir = DW_STRING (attr);
6358 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6359 {
6360 comp_dir = ldirname (name);
6361 if (comp_dir != NULL)
6362 make_cleanup (xfree, comp_dir);
6363 }
6364
6365 if (name == NULL)
6366 name = "<unknown>";
6367
6368 prepare_one_comp_unit (cu, die);
6369
6370 /* We assume that we're processing GCC output. */
6371 processing_gcc_compilation = 2;
6372
6373 processing_has_namespace_info = 0;
6374
6375 start_symtab (name, comp_dir, lowpc);
6376 record_debugformat ("DWARF 2");
6377 record_producer (cu->producer);
6378
6379 /* Decode line number information if present. We do this before
6380 processing child DIEs, so that the line header table is available
6381 for DW_AT_decl_file.
6382 We don't need the pc/line-number mapping for type units. */
6383 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6384
6385 /* Process the dies in the type unit. */
6386 if (die->child == NULL)
6387 {
6388 dump_die_for_error (die);
6389 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6390 bfd_get_filename (abfd));
6391 }
6392
6393 child_die = die->child;
6394
6395 while (child_die && child_die->tag)
6396 {
6397 process_die (child_die, cu);
6398
6399 child_die = sibling_die (child_die);
6400 }
6401
6402 do_cleanups (back_to);
6403 }
6404 \f
6405 /* DWO files. */
6406
6407 static hashval_t
6408 hash_dwo_file (const void *item)
6409 {
6410 const struct dwo_file *dwo_file = item;
6411
6412 return htab_hash_string (dwo_file->dwo_name);
6413 }
6414
6415 static int
6416 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6417 {
6418 const struct dwo_file *lhs = item_lhs;
6419 const struct dwo_file *rhs = item_rhs;
6420
6421 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6422 }
6423
6424 /* Allocate a hash table for DWO files. */
6425
6426 static htab_t
6427 allocate_dwo_file_hash_table (void)
6428 {
6429 struct objfile *objfile = dwarf2_per_objfile->objfile;
6430
6431 return htab_create_alloc_ex (41,
6432 hash_dwo_file,
6433 eq_dwo_file,
6434 NULL,
6435 &objfile->objfile_obstack,
6436 hashtab_obstack_allocate,
6437 dummy_obstack_deallocate);
6438 }
6439
6440 static hashval_t
6441 hash_dwo_unit (const void *item)
6442 {
6443 const struct dwo_unit *dwo_unit = item;
6444
6445 /* This drops the top 32 bits of the id, but is ok for a hash. */
6446 return dwo_unit->signature;
6447 }
6448
6449 static int
6450 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6451 {
6452 const struct dwo_unit *lhs = item_lhs;
6453 const struct dwo_unit *rhs = item_rhs;
6454
6455 /* The signature is assumed to be unique within the DWO file.
6456 So while object file CU dwo_id's always have the value zero,
6457 that's OK, assuming each object file DWO file has only one CU,
6458 and that's the rule for now. */
6459 return lhs->signature == rhs->signature;
6460 }
6461
6462 /* Allocate a hash table for DWO CUs,TUs.
6463 There is one of these tables for each of CUs,TUs for each DWO file. */
6464
6465 static htab_t
6466 allocate_dwo_unit_table (struct objfile *objfile)
6467 {
6468 /* Start out with a pretty small number.
6469 Generally DWO files contain only one CU and maybe some TUs. */
6470 return htab_create_alloc_ex (3,
6471 hash_dwo_unit,
6472 eq_dwo_unit,
6473 NULL,
6474 &objfile->objfile_obstack,
6475 hashtab_obstack_allocate,
6476 dummy_obstack_deallocate);
6477 }
6478
6479 /* This function is mapped across the sections and remembers the offset and
6480 size of each of the DWO debugging sections we are interested in. */
6481
6482 static void
6483 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6484 {
6485 struct dwo_file *dwo_file = dwo_file_ptr;
6486 const struct dwo_section_names *names = &dwo_section_names;
6487
6488 if (section_is_p (sectp->name, &names->abbrev_dwo))
6489 {
6490 dwo_file->sections.abbrev.asection = sectp;
6491 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6492 }
6493 else if (section_is_p (sectp->name, &names->info_dwo))
6494 {
6495 dwo_file->sections.info.asection = sectp;
6496 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6497 }
6498 else if (section_is_p (sectp->name, &names->line_dwo))
6499 {
6500 dwo_file->sections.line.asection = sectp;
6501 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6502 }
6503 else if (section_is_p (sectp->name, &names->loc_dwo))
6504 {
6505 dwo_file->sections.loc.asection = sectp;
6506 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6507 }
6508 else if (section_is_p (sectp->name, &names->str_dwo))
6509 {
6510 dwo_file->sections.str.asection = sectp;
6511 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6512 }
6513 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6514 {
6515 dwo_file->sections.str_offsets.asection = sectp;
6516 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6517 }
6518 else if (section_is_p (sectp->name, &names->types_dwo))
6519 {
6520 struct dwarf2_section_info type_section;
6521
6522 memset (&type_section, 0, sizeof (type_section));
6523 type_section.asection = sectp;
6524 type_section.size = bfd_get_section_size (sectp);
6525 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6526 &type_section);
6527 }
6528 }
6529
6530 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6531
6532 struct create_dwo_info_table_data
6533 {
6534 struct dwo_file *dwo_file;
6535 htab_t cu_htab;
6536 };
6537
6538 /* die_reader_func for create_debug_info_hash_table. */
6539
6540 static void
6541 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6542 gdb_byte *info_ptr,
6543 struct die_info *comp_unit_die,
6544 int has_children,
6545 void *datap)
6546 {
6547 struct dwarf2_cu *cu = reader->cu;
6548 struct objfile *objfile = dwarf2_per_objfile->objfile;
6549 sect_offset offset = cu->per_cu->offset;
6550 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6551 struct create_dwo_info_table_data *data = datap;
6552 struct dwo_file *dwo_file = data->dwo_file;
6553 htab_t cu_htab = data->cu_htab;
6554 void **slot;
6555 struct attribute *attr;
6556 struct dwo_unit *dwo_unit;
6557
6558 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6559 if (attr == NULL)
6560 {
6561 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6562 " its dwo_id [in module %s]"),
6563 offset.sect_off, dwo_file->dwo_name);
6564 return;
6565 }
6566
6567 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6568 dwo_unit->dwo_file = dwo_file;
6569 dwo_unit->signature = DW_UNSND (attr);
6570 dwo_unit->info_or_types_section = section;
6571 dwo_unit->offset = offset;
6572 dwo_unit->length = cu->per_cu->length;
6573
6574 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6575 gdb_assert (slot != NULL);
6576 if (*slot != NULL)
6577 {
6578 const struct dwo_unit *dup_dwo_unit = *slot;
6579
6580 complaint (&symfile_complaints,
6581 _("debug entry at offset 0x%x is duplicate to the entry at"
6582 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6583 offset.sect_off, dup_dwo_unit->offset.sect_off,
6584 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6585 dwo_file->dwo_name);
6586 }
6587 else
6588 *slot = dwo_unit;
6589
6590 if (dwarf2_die_debug)
6591 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6592 offset.sect_off,
6593 phex (dwo_unit->signature,
6594 sizeof (dwo_unit->signature)));
6595 }
6596
6597 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6598
6599 static htab_t
6600 create_debug_info_hash_table (struct dwo_file *dwo_file)
6601 {
6602 struct objfile *objfile = dwarf2_per_objfile->objfile;
6603 struct dwarf2_section_info *section = &dwo_file->sections.info;
6604 bfd *abfd;
6605 htab_t cu_htab;
6606 gdb_byte *info_ptr, *end_ptr;
6607 struct create_dwo_info_table_data create_dwo_info_table_data;
6608
6609 dwarf2_read_section (objfile, section);
6610 info_ptr = section->buffer;
6611
6612 if (info_ptr == NULL)
6613 return NULL;
6614
6615 /* We can't set abfd until now because the section may be empty or
6616 not present, in which case section->asection will be NULL. */
6617 abfd = section->asection->owner;
6618
6619 if (dwarf2_die_debug)
6620 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6621 bfd_get_filename (abfd));
6622
6623 cu_htab = allocate_dwo_unit_table (objfile);
6624
6625 create_dwo_info_table_data.dwo_file = dwo_file;
6626 create_dwo_info_table_data.cu_htab = cu_htab;
6627
6628 end_ptr = info_ptr + section->size;
6629 while (info_ptr < end_ptr)
6630 {
6631 struct dwarf2_per_cu_data per_cu;
6632
6633 memset (&per_cu, 0, sizeof (per_cu));
6634 per_cu.objfile = objfile;
6635 per_cu.is_debug_types = 0;
6636 per_cu.offset.sect_off = info_ptr - section->buffer;
6637 per_cu.info_or_types_section = section;
6638
6639 init_cutu_and_read_dies_no_follow (&per_cu,
6640 &dwo_file->sections.abbrev,
6641 dwo_file,
6642 create_debug_info_hash_table_reader,
6643 &create_dwo_info_table_data);
6644
6645 info_ptr += per_cu.length;
6646 }
6647
6648 return cu_htab;
6649 }
6650
6651 /* Subroutine of open_dwo_file to simplify it.
6652 Open the file specified by FILE_NAME and hand it off to BFD for
6653 preliminary analysis. Return a newly initialized bfd *, which
6654 includes a canonicalized copy of FILE_NAME.
6655 In case of trouble, return NULL.
6656 NOTE: This function is derived from symfile_bfd_open. */
6657
6658 static bfd *
6659 try_open_dwo_file (const char *file_name)
6660 {
6661 bfd *sym_bfd;
6662 int desc;
6663 char *absolute_name;
6664 char *name;
6665
6666 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6667 O_RDONLY | O_BINARY, &absolute_name);
6668 if (desc < 0)
6669 return NULL;
6670
6671 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6672 if (!sym_bfd)
6673 {
6674 close (desc);
6675 xfree (absolute_name);
6676 return NULL;
6677 }
6678 bfd_set_cacheable (sym_bfd, 1);
6679
6680 if (!bfd_check_format (sym_bfd, bfd_object))
6681 {
6682 bfd_close (sym_bfd); /* This also closes desc. */
6683 xfree (absolute_name);
6684 return NULL;
6685 }
6686
6687 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6688 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6689
6690 return sym_bfd;
6691 }
6692
6693 /* Try to open DWO file DWO_NAME.
6694 COMP_DIR is the DW_AT_comp_dir attribute.
6695 The result is the bfd handle of the file.
6696 If there is a problem finding or opening the file, return NULL.
6697 Upon success, the canonicalized path of the file is stored in the bfd,
6698 same as symfile_bfd_open. */
6699
6700 static bfd *
6701 open_dwo_file (const char *dwo_name, const char *comp_dir)
6702 {
6703 bfd *abfd;
6704 char *path_to_try, *debug_dir;
6705
6706 if (IS_ABSOLUTE_PATH (dwo_name))
6707 return try_open_dwo_file (dwo_name);
6708
6709 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6710
6711 if (comp_dir != NULL)
6712 {
6713 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6714
6715 /* NOTE: If comp_dir is a relative path, this will also try the
6716 search path, which seems useful. */
6717 abfd = try_open_dwo_file (path_to_try);
6718 xfree (path_to_try);
6719 if (abfd != NULL)
6720 return abfd;
6721 }
6722
6723 /* That didn't work, try debug-file-directory, which, despite its name,
6724 is a list of paths. */
6725
6726 if (*debug_file_directory == '\0')
6727 return NULL;
6728
6729 return try_open_dwo_file (dwo_name);
6730 }
6731
6732 /* Initialize the use of the DWO file specified by DWO_NAME. */
6733
6734 static struct dwo_file *
6735 init_dwo_file (const char *dwo_name, const char *comp_dir)
6736 {
6737 struct objfile *objfile = dwarf2_per_objfile->objfile;
6738 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6739 struct dwo_file);
6740 bfd *abfd;
6741 struct cleanup *cleanups;
6742
6743 if (dwarf2_die_debug)
6744 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
6745
6746 abfd = open_dwo_file (dwo_name, comp_dir);
6747 if (abfd == NULL)
6748 return NULL;
6749 dwo_file->dwo_name = dwo_name;
6750 dwo_file->dwo_bfd = abfd;
6751
6752 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
6753
6754 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
6755
6756 dwo_file->cus = create_debug_info_hash_table (dwo_file);
6757
6758 dwo_file->tus = create_debug_types_hash_table (dwo_file,
6759 dwo_file->sections.types);
6760
6761 discard_cleanups (cleanups);
6762
6763 return dwo_file;
6764 }
6765
6766 /* Lookup DWO file DWO_NAME. */
6767
6768 static struct dwo_file *
6769 lookup_dwo_file (char *dwo_name, const char *comp_dir)
6770 {
6771 struct dwo_file *dwo_file;
6772 struct dwo_file find_entry;
6773 void **slot;
6774
6775 if (dwarf2_per_objfile->dwo_files == NULL)
6776 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
6777
6778 /* Have we already seen this DWO file? */
6779 find_entry.dwo_name = dwo_name;
6780 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
6781
6782 /* If not, read it in and build a table of the DWOs it contains. */
6783 if (*slot == NULL)
6784 *slot = init_dwo_file (dwo_name, comp_dir);
6785
6786 /* NOTE: This will be NULL if unable to open the file. */
6787 dwo_file = *slot;
6788
6789 return dwo_file;
6790 }
6791
6792 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
6793 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6794 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
6795 nomenclature as TUs).
6796 The result is the DWO CU or NULL if we didn't find it
6797 (dwo_id mismatch or couldn't find the DWO file). */
6798
6799 static struct dwo_unit *
6800 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
6801 char *dwo_name, const char *comp_dir,
6802 ULONGEST signature)
6803 {
6804 struct objfile *objfile = dwarf2_per_objfile->objfile;
6805 struct dwo_file *dwo_file;
6806
6807 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6808 if (dwo_file == NULL)
6809 return NULL;
6810
6811 /* Look up the DWO using its signature(dwo_id). */
6812
6813 if (dwo_file->cus != NULL)
6814 {
6815 struct dwo_unit find_dwo_cu, *dwo_cu;
6816
6817 find_dwo_cu.signature = signature;
6818 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
6819
6820 if (dwo_cu != NULL)
6821 return dwo_cu;
6822 }
6823
6824 /* We didn't find it. This must mean a dwo_id mismatch. */
6825
6826 complaint (&symfile_complaints,
6827 _("Could not find DWO CU referenced by CU at offset 0x%x"
6828 " [in module %s]"),
6829 this_cu->offset.sect_off, objfile->name);
6830 return NULL;
6831 }
6832
6833 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
6834 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6835 The result is the DWO CU or NULL if we didn't find it
6836 (dwo_id mismatch or couldn't find the DWO file). */
6837
6838 static struct dwo_unit *
6839 lookup_dwo_type_unit (struct signatured_type *this_tu,
6840 char *dwo_name, const char *comp_dir)
6841 {
6842 struct objfile *objfile = dwarf2_per_objfile->objfile;
6843 struct dwo_file *dwo_file;
6844 struct dwo_unit find_dwo_tu, *dwo_tu;
6845
6846 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6847 if (dwo_file == NULL)
6848 return NULL;
6849
6850 /* Look up the DWO using its signature(dwo_id). */
6851
6852 if (dwo_file->tus != NULL)
6853 {
6854 struct dwo_unit find_dwo_tu, *dwo_tu;
6855
6856 find_dwo_tu.signature = this_tu->signature;
6857 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
6858
6859 if (dwo_tu != NULL)
6860 return dwo_tu;
6861 }
6862
6863 /* We didn't find it. This must mean a dwo_id mismatch. */
6864
6865 complaint (&symfile_complaints,
6866 _("Could not find DWO TU referenced by TU at offset 0x%x"
6867 " [in module %s]"),
6868 this_tu->per_cu.offset.sect_off, objfile->name);
6869 return NULL;
6870 }
6871
6872 /* Free all resources associated with DWO_FILE.
6873 Close the DWO file and munmap the sections.
6874 All memory should be on the objfile obstack. */
6875
6876 static void
6877 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
6878 {
6879 int ix;
6880 struct dwarf2_section_info *section;
6881
6882 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
6883 bfd_close (dwo_file->dwo_bfd);
6884
6885 munmap_section_buffer (&dwo_file->sections.abbrev);
6886 munmap_section_buffer (&dwo_file->sections.info);
6887 munmap_section_buffer (&dwo_file->sections.line);
6888 munmap_section_buffer (&dwo_file->sections.loc);
6889 munmap_section_buffer (&dwo_file->sections.str);
6890 munmap_section_buffer (&dwo_file->sections.str_offsets);
6891
6892 for (ix = 0;
6893 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
6894 ix, section);
6895 ++ix)
6896 munmap_section_buffer (section);
6897
6898 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
6899 }
6900
6901 /* Wrapper for free_dwo_file for use in cleanups. */
6902
6903 static void
6904 free_dwo_file_cleanup (void *arg)
6905 {
6906 struct dwo_file *dwo_file = (struct dwo_file *) arg;
6907 struct objfile *objfile = dwarf2_per_objfile->objfile;
6908
6909 free_dwo_file (dwo_file, objfile);
6910 }
6911
6912 /* Traversal function for free_dwo_files. */
6913
6914 static int
6915 free_dwo_file_from_slot (void **slot, void *info)
6916 {
6917 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6918 struct objfile *objfile = (struct objfile *) info;
6919
6920 free_dwo_file (dwo_file, objfile);
6921
6922 return 1;
6923 }
6924
6925 /* Free all resources associated with DWO_FILES. */
6926
6927 static void
6928 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
6929 {
6930 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
6931 }
6932 \f
6933 /* Read in various DIEs. */
6934
6935 /* qsort helper for inherit_abstract_dies. */
6936
6937 static int
6938 unsigned_int_compar (const void *ap, const void *bp)
6939 {
6940 unsigned int a = *(unsigned int *) ap;
6941 unsigned int b = *(unsigned int *) bp;
6942
6943 return (a > b) - (b > a);
6944 }
6945
6946 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
6947 Inherit only the children of the DW_AT_abstract_origin DIE not being
6948 already referenced by DW_AT_abstract_origin from the children of the
6949 current DIE. */
6950
6951 static void
6952 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
6953 {
6954 struct die_info *child_die;
6955 unsigned die_children_count;
6956 /* CU offsets which were referenced by children of the current DIE. */
6957 sect_offset *offsets;
6958 sect_offset *offsets_end, *offsetp;
6959 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
6960 struct die_info *origin_die;
6961 /* Iterator of the ORIGIN_DIE children. */
6962 struct die_info *origin_child_die;
6963 struct cleanup *cleanups;
6964 struct attribute *attr;
6965 struct dwarf2_cu *origin_cu;
6966 struct pending **origin_previous_list_in_scope;
6967
6968 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6969 if (!attr)
6970 return;
6971
6972 /* Note that following die references may follow to a die in a
6973 different cu. */
6974
6975 origin_cu = cu;
6976 origin_die = follow_die_ref (die, attr, &origin_cu);
6977
6978 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
6979 symbols in. */
6980 origin_previous_list_in_scope = origin_cu->list_in_scope;
6981 origin_cu->list_in_scope = cu->list_in_scope;
6982
6983 if (die->tag != origin_die->tag
6984 && !(die->tag == DW_TAG_inlined_subroutine
6985 && origin_die->tag == DW_TAG_subprogram))
6986 complaint (&symfile_complaints,
6987 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
6988 die->offset.sect_off, origin_die->offset.sect_off);
6989
6990 child_die = die->child;
6991 die_children_count = 0;
6992 while (child_die && child_die->tag)
6993 {
6994 child_die = sibling_die (child_die);
6995 die_children_count++;
6996 }
6997 offsets = xmalloc (sizeof (*offsets) * die_children_count);
6998 cleanups = make_cleanup (xfree, offsets);
6999
7000 offsets_end = offsets;
7001 child_die = die->child;
7002 while (child_die && child_die->tag)
7003 {
7004 /* For each CHILD_DIE, find the corresponding child of
7005 ORIGIN_DIE. If there is more than one layer of
7006 DW_AT_abstract_origin, follow them all; there shouldn't be,
7007 but GCC versions at least through 4.4 generate this (GCC PR
7008 40573). */
7009 struct die_info *child_origin_die = child_die;
7010 struct dwarf2_cu *child_origin_cu = cu;
7011
7012 while (1)
7013 {
7014 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7015 child_origin_cu);
7016 if (attr == NULL)
7017 break;
7018 child_origin_die = follow_die_ref (child_origin_die, attr,
7019 &child_origin_cu);
7020 }
7021
7022 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7023 counterpart may exist. */
7024 if (child_origin_die != child_die)
7025 {
7026 if (child_die->tag != child_origin_die->tag
7027 && !(child_die->tag == DW_TAG_inlined_subroutine
7028 && child_origin_die->tag == DW_TAG_subprogram))
7029 complaint (&symfile_complaints,
7030 _("Child DIE 0x%x and its abstract origin 0x%x have "
7031 "different tags"), child_die->offset.sect_off,
7032 child_origin_die->offset.sect_off);
7033 if (child_origin_die->parent != origin_die)
7034 complaint (&symfile_complaints,
7035 _("Child DIE 0x%x and its abstract origin 0x%x have "
7036 "different parents"), child_die->offset.sect_off,
7037 child_origin_die->offset.sect_off);
7038 else
7039 *offsets_end++ = child_origin_die->offset;
7040 }
7041 child_die = sibling_die (child_die);
7042 }
7043 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7044 unsigned_int_compar);
7045 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7046 if (offsetp[-1].sect_off == offsetp->sect_off)
7047 complaint (&symfile_complaints,
7048 _("Multiple children of DIE 0x%x refer "
7049 "to DIE 0x%x as their abstract origin"),
7050 die->offset.sect_off, offsetp->sect_off);
7051
7052 offsetp = offsets;
7053 origin_child_die = origin_die->child;
7054 while (origin_child_die && origin_child_die->tag)
7055 {
7056 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7057 while (offsetp < offsets_end
7058 && offsetp->sect_off < origin_child_die->offset.sect_off)
7059 offsetp++;
7060 if (offsetp >= offsets_end
7061 || offsetp->sect_off > origin_child_die->offset.sect_off)
7062 {
7063 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7064 process_die (origin_child_die, origin_cu);
7065 }
7066 origin_child_die = sibling_die (origin_child_die);
7067 }
7068 origin_cu->list_in_scope = origin_previous_list_in_scope;
7069
7070 do_cleanups (cleanups);
7071 }
7072
7073 static void
7074 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7075 {
7076 struct objfile *objfile = cu->objfile;
7077 struct context_stack *new;
7078 CORE_ADDR lowpc;
7079 CORE_ADDR highpc;
7080 struct die_info *child_die;
7081 struct attribute *attr, *call_line, *call_file;
7082 char *name;
7083 CORE_ADDR baseaddr;
7084 struct block *block;
7085 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7086 VEC (symbolp) *template_args = NULL;
7087 struct template_symbol *templ_func = NULL;
7088
7089 if (inlined_func)
7090 {
7091 /* If we do not have call site information, we can't show the
7092 caller of this inlined function. That's too confusing, so
7093 only use the scope for local variables. */
7094 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7095 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7096 if (call_line == NULL || call_file == NULL)
7097 {
7098 read_lexical_block_scope (die, cu);
7099 return;
7100 }
7101 }
7102
7103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7104
7105 name = dwarf2_name (die, cu);
7106
7107 /* Ignore functions with missing or empty names. These are actually
7108 illegal according to the DWARF standard. */
7109 if (name == NULL)
7110 {
7111 complaint (&symfile_complaints,
7112 _("missing name for subprogram DIE at %d"),
7113 die->offset.sect_off);
7114 return;
7115 }
7116
7117 /* Ignore functions with missing or invalid low and high pc attributes. */
7118 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7119 {
7120 attr = dwarf2_attr (die, DW_AT_external, cu);
7121 if (!attr || !DW_UNSND (attr))
7122 complaint (&symfile_complaints,
7123 _("cannot get low and high bounds "
7124 "for subprogram DIE at %d"),
7125 die->offset.sect_off);
7126 return;
7127 }
7128
7129 lowpc += baseaddr;
7130 highpc += baseaddr;
7131
7132 /* If we have any template arguments, then we must allocate a
7133 different sort of symbol. */
7134 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7135 {
7136 if (child_die->tag == DW_TAG_template_type_param
7137 || child_die->tag == DW_TAG_template_value_param)
7138 {
7139 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7140 struct template_symbol);
7141 templ_func->base.is_cplus_template_function = 1;
7142 break;
7143 }
7144 }
7145
7146 new = push_context (0, lowpc);
7147 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7148 (struct symbol *) templ_func);
7149
7150 /* If there is a location expression for DW_AT_frame_base, record
7151 it. */
7152 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7153 if (attr)
7154 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7155 expression is being recorded directly in the function's symbol
7156 and not in a separate frame-base object. I guess this hack is
7157 to avoid adding some sort of frame-base adjunct/annex to the
7158 function's symbol :-(. The problem with doing this is that it
7159 results in a function symbol with a location expression that
7160 has nothing to do with the location of the function, ouch! The
7161 relationship should be: a function's symbol has-a frame base; a
7162 frame-base has-a location expression. */
7163 dwarf2_symbol_mark_computed (attr, new->name, cu);
7164
7165 cu->list_in_scope = &local_symbols;
7166
7167 if (die->child != NULL)
7168 {
7169 child_die = die->child;
7170 while (child_die && child_die->tag)
7171 {
7172 if (child_die->tag == DW_TAG_template_type_param
7173 || child_die->tag == DW_TAG_template_value_param)
7174 {
7175 struct symbol *arg = new_symbol (child_die, NULL, cu);
7176
7177 if (arg != NULL)
7178 VEC_safe_push (symbolp, template_args, arg);
7179 }
7180 else
7181 process_die (child_die, cu);
7182 child_die = sibling_die (child_die);
7183 }
7184 }
7185
7186 inherit_abstract_dies (die, cu);
7187
7188 /* If we have a DW_AT_specification, we might need to import using
7189 directives from the context of the specification DIE. See the
7190 comment in determine_prefix. */
7191 if (cu->language == language_cplus
7192 && dwarf2_attr (die, DW_AT_specification, cu))
7193 {
7194 struct dwarf2_cu *spec_cu = cu;
7195 struct die_info *spec_die = die_specification (die, &spec_cu);
7196
7197 while (spec_die)
7198 {
7199 child_die = spec_die->child;
7200 while (child_die && child_die->tag)
7201 {
7202 if (child_die->tag == DW_TAG_imported_module)
7203 process_die (child_die, spec_cu);
7204 child_die = sibling_die (child_die);
7205 }
7206
7207 /* In some cases, GCC generates specification DIEs that
7208 themselves contain DW_AT_specification attributes. */
7209 spec_die = die_specification (spec_die, &spec_cu);
7210 }
7211 }
7212
7213 new = pop_context ();
7214 /* Make a block for the local symbols within. */
7215 block = finish_block (new->name, &local_symbols, new->old_blocks,
7216 lowpc, highpc, objfile);
7217
7218 /* For C++, set the block's scope. */
7219 if (cu->language == language_cplus || cu->language == language_fortran)
7220 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7221 determine_prefix (die, cu),
7222 processing_has_namespace_info);
7223
7224 /* If we have address ranges, record them. */
7225 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7226
7227 /* Attach template arguments to function. */
7228 if (! VEC_empty (symbolp, template_args))
7229 {
7230 gdb_assert (templ_func != NULL);
7231
7232 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7233 templ_func->template_arguments
7234 = obstack_alloc (&objfile->objfile_obstack,
7235 (templ_func->n_template_arguments
7236 * sizeof (struct symbol *)));
7237 memcpy (templ_func->template_arguments,
7238 VEC_address (symbolp, template_args),
7239 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7240 VEC_free (symbolp, template_args);
7241 }
7242
7243 /* In C++, we can have functions nested inside functions (e.g., when
7244 a function declares a class that has methods). This means that
7245 when we finish processing a function scope, we may need to go
7246 back to building a containing block's symbol lists. */
7247 local_symbols = new->locals;
7248 param_symbols = new->params;
7249 using_directives = new->using_directives;
7250
7251 /* If we've finished processing a top-level function, subsequent
7252 symbols go in the file symbol list. */
7253 if (outermost_context_p ())
7254 cu->list_in_scope = &file_symbols;
7255 }
7256
7257 /* Process all the DIES contained within a lexical block scope. Start
7258 a new scope, process the dies, and then close the scope. */
7259
7260 static void
7261 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7262 {
7263 struct objfile *objfile = cu->objfile;
7264 struct context_stack *new;
7265 CORE_ADDR lowpc, highpc;
7266 struct die_info *child_die;
7267 CORE_ADDR baseaddr;
7268
7269 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7270
7271 /* Ignore blocks with missing or invalid low and high pc attributes. */
7272 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7273 as multiple lexical blocks? Handling children in a sane way would
7274 be nasty. Might be easier to properly extend generic blocks to
7275 describe ranges. */
7276 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7277 return;
7278 lowpc += baseaddr;
7279 highpc += baseaddr;
7280
7281 push_context (0, lowpc);
7282 if (die->child != NULL)
7283 {
7284 child_die = die->child;
7285 while (child_die && child_die->tag)
7286 {
7287 process_die (child_die, cu);
7288 child_die = sibling_die (child_die);
7289 }
7290 }
7291 new = pop_context ();
7292
7293 if (local_symbols != NULL || using_directives != NULL)
7294 {
7295 struct block *block
7296 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7297 highpc, objfile);
7298
7299 /* Note that recording ranges after traversing children, as we
7300 do here, means that recording a parent's ranges entails
7301 walking across all its children's ranges as they appear in
7302 the address map, which is quadratic behavior.
7303
7304 It would be nicer to record the parent's ranges before
7305 traversing its children, simply overriding whatever you find
7306 there. But since we don't even decide whether to create a
7307 block until after we've traversed its children, that's hard
7308 to do. */
7309 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7310 }
7311 local_symbols = new->locals;
7312 using_directives = new->using_directives;
7313 }
7314
7315 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7316
7317 static void
7318 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7319 {
7320 struct objfile *objfile = cu->objfile;
7321 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7322 CORE_ADDR pc, baseaddr;
7323 struct attribute *attr;
7324 struct call_site *call_site, call_site_local;
7325 void **slot;
7326 int nparams;
7327 struct die_info *child_die;
7328
7329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7330
7331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7332 if (!attr)
7333 {
7334 complaint (&symfile_complaints,
7335 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7336 "DIE 0x%x [in module %s]"),
7337 die->offset.sect_off, objfile->name);
7338 return;
7339 }
7340 pc = DW_ADDR (attr) + baseaddr;
7341
7342 if (cu->call_site_htab == NULL)
7343 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7344 NULL, &objfile->objfile_obstack,
7345 hashtab_obstack_allocate, NULL);
7346 call_site_local.pc = pc;
7347 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7348 if (*slot != NULL)
7349 {
7350 complaint (&symfile_complaints,
7351 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7352 "DIE 0x%x [in module %s]"),
7353 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7354 return;
7355 }
7356
7357 /* Count parameters at the caller. */
7358
7359 nparams = 0;
7360 for (child_die = die->child; child_die && child_die->tag;
7361 child_die = sibling_die (child_die))
7362 {
7363 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7364 {
7365 complaint (&symfile_complaints,
7366 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7367 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7368 child_die->tag, child_die->offset.sect_off, objfile->name);
7369 continue;
7370 }
7371
7372 nparams++;
7373 }
7374
7375 call_site = obstack_alloc (&objfile->objfile_obstack,
7376 (sizeof (*call_site)
7377 + (sizeof (*call_site->parameter)
7378 * (nparams - 1))));
7379 *slot = call_site;
7380 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7381 call_site->pc = pc;
7382
7383 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7384 {
7385 struct die_info *func_die;
7386
7387 /* Skip also over DW_TAG_inlined_subroutine. */
7388 for (func_die = die->parent;
7389 func_die && func_die->tag != DW_TAG_subprogram
7390 && func_die->tag != DW_TAG_subroutine_type;
7391 func_die = func_die->parent);
7392
7393 /* DW_AT_GNU_all_call_sites is a superset
7394 of DW_AT_GNU_all_tail_call_sites. */
7395 if (func_die
7396 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7397 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7398 {
7399 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7400 not complete. But keep CALL_SITE for look ups via call_site_htab,
7401 both the initial caller containing the real return address PC and
7402 the final callee containing the current PC of a chain of tail
7403 calls do not need to have the tail call list complete. But any
7404 function candidate for a virtual tail call frame searched via
7405 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7406 determined unambiguously. */
7407 }
7408 else
7409 {
7410 struct type *func_type = NULL;
7411
7412 if (func_die)
7413 func_type = get_die_type (func_die, cu);
7414 if (func_type != NULL)
7415 {
7416 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7417
7418 /* Enlist this call site to the function. */
7419 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7420 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7421 }
7422 else
7423 complaint (&symfile_complaints,
7424 _("Cannot find function owning DW_TAG_GNU_call_site "
7425 "DIE 0x%x [in module %s]"),
7426 die->offset.sect_off, objfile->name);
7427 }
7428 }
7429
7430 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7431 if (attr == NULL)
7432 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7433 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7434 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7435 /* Keep NULL DWARF_BLOCK. */;
7436 else if (attr_form_is_block (attr))
7437 {
7438 struct dwarf2_locexpr_baton *dlbaton;
7439
7440 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7441 dlbaton->data = DW_BLOCK (attr)->data;
7442 dlbaton->size = DW_BLOCK (attr)->size;
7443 dlbaton->per_cu = cu->per_cu;
7444
7445 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7446 }
7447 else if (is_ref_attr (attr))
7448 {
7449 struct dwarf2_cu *target_cu = cu;
7450 struct die_info *target_die;
7451
7452 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7453 gdb_assert (target_cu->objfile == objfile);
7454 if (die_is_declaration (target_die, target_cu))
7455 {
7456 const char *target_physname;
7457
7458 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7459 if (target_physname == NULL)
7460 complaint (&symfile_complaints,
7461 _("DW_AT_GNU_call_site_target target DIE has invalid "
7462 "physname, for referencing DIE 0x%x [in module %s]"),
7463 die->offset.sect_off, objfile->name);
7464 else
7465 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7466 }
7467 else
7468 {
7469 CORE_ADDR lowpc;
7470
7471 /* DW_AT_entry_pc should be preferred. */
7472 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7473 complaint (&symfile_complaints,
7474 _("DW_AT_GNU_call_site_target target DIE has invalid "
7475 "low pc, for referencing DIE 0x%x [in module %s]"),
7476 die->offset.sect_off, objfile->name);
7477 else
7478 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7479 }
7480 }
7481 else
7482 complaint (&symfile_complaints,
7483 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7484 "block nor reference, for DIE 0x%x [in module %s]"),
7485 die->offset.sect_off, objfile->name);
7486
7487 call_site->per_cu = cu->per_cu;
7488
7489 for (child_die = die->child;
7490 child_die && child_die->tag;
7491 child_die = sibling_die (child_die))
7492 {
7493 struct dwarf2_locexpr_baton *dlbaton;
7494 struct call_site_parameter *parameter;
7495
7496 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7497 {
7498 /* Already printed the complaint above. */
7499 continue;
7500 }
7501
7502 gdb_assert (call_site->parameter_count < nparams);
7503 parameter = &call_site->parameter[call_site->parameter_count];
7504
7505 /* DW_AT_location specifies the register number. Value of the data
7506 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7507
7508 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7509 if (!attr || !attr_form_is_block (attr))
7510 {
7511 complaint (&symfile_complaints,
7512 _("No DW_FORM_block* DW_AT_location for "
7513 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7514 child_die->offset.sect_off, objfile->name);
7515 continue;
7516 }
7517 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7518 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7519 if (parameter->dwarf_reg == -1
7520 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7521 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7522 &parameter->fb_offset))
7523 {
7524 complaint (&symfile_complaints,
7525 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7526 "for DW_FORM_block* DW_AT_location for "
7527 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7528 child_die->offset.sect_off, objfile->name);
7529 continue;
7530 }
7531
7532 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7533 if (!attr_form_is_block (attr))
7534 {
7535 complaint (&symfile_complaints,
7536 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7537 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7538 child_die->offset.sect_off, objfile->name);
7539 continue;
7540 }
7541 parameter->value = DW_BLOCK (attr)->data;
7542 parameter->value_size = DW_BLOCK (attr)->size;
7543
7544 /* Parameters are not pre-cleared by memset above. */
7545 parameter->data_value = NULL;
7546 parameter->data_value_size = 0;
7547 call_site->parameter_count++;
7548
7549 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7550 if (attr)
7551 {
7552 if (!attr_form_is_block (attr))
7553 complaint (&symfile_complaints,
7554 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7555 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7556 child_die->offset.sect_off, objfile->name);
7557 else
7558 {
7559 parameter->data_value = DW_BLOCK (attr)->data;
7560 parameter->data_value_size = DW_BLOCK (attr)->size;
7561 }
7562 }
7563 }
7564 }
7565
7566 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7567 Return 1 if the attributes are present and valid, otherwise, return 0.
7568 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7569
7570 static int
7571 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7572 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7573 struct partial_symtab *ranges_pst)
7574 {
7575 struct objfile *objfile = cu->objfile;
7576 struct comp_unit_head *cu_header = &cu->header;
7577 bfd *obfd = objfile->obfd;
7578 unsigned int addr_size = cu_header->addr_size;
7579 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7580 /* Base address selection entry. */
7581 CORE_ADDR base;
7582 int found_base;
7583 unsigned int dummy;
7584 gdb_byte *buffer;
7585 CORE_ADDR marker;
7586 int low_set;
7587 CORE_ADDR low = 0;
7588 CORE_ADDR high = 0;
7589 CORE_ADDR baseaddr;
7590
7591 found_base = cu->base_known;
7592 base = cu->base_address;
7593
7594 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7595 if (offset >= dwarf2_per_objfile->ranges.size)
7596 {
7597 complaint (&symfile_complaints,
7598 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7599 offset);
7600 return 0;
7601 }
7602 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7603
7604 /* Read in the largest possible address. */
7605 marker = read_address (obfd, buffer, cu, &dummy);
7606 if ((marker & mask) == mask)
7607 {
7608 /* If we found the largest possible address, then
7609 read the base address. */
7610 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7611 buffer += 2 * addr_size;
7612 offset += 2 * addr_size;
7613 found_base = 1;
7614 }
7615
7616 low_set = 0;
7617
7618 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7619
7620 while (1)
7621 {
7622 CORE_ADDR range_beginning, range_end;
7623
7624 range_beginning = read_address (obfd, buffer, cu, &dummy);
7625 buffer += addr_size;
7626 range_end = read_address (obfd, buffer, cu, &dummy);
7627 buffer += addr_size;
7628 offset += 2 * addr_size;
7629
7630 /* An end of list marker is a pair of zero addresses. */
7631 if (range_beginning == 0 && range_end == 0)
7632 /* Found the end of list entry. */
7633 break;
7634
7635 /* Each base address selection entry is a pair of 2 values.
7636 The first is the largest possible address, the second is
7637 the base address. Check for a base address here. */
7638 if ((range_beginning & mask) == mask)
7639 {
7640 /* If we found the largest possible address, then
7641 read the base address. */
7642 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7643 found_base = 1;
7644 continue;
7645 }
7646
7647 if (!found_base)
7648 {
7649 /* We have no valid base address for the ranges
7650 data. */
7651 complaint (&symfile_complaints,
7652 _("Invalid .debug_ranges data (no base address)"));
7653 return 0;
7654 }
7655
7656 if (range_beginning > range_end)
7657 {
7658 /* Inverted range entries are invalid. */
7659 complaint (&symfile_complaints,
7660 _("Invalid .debug_ranges data (inverted range)"));
7661 return 0;
7662 }
7663
7664 /* Empty range entries have no effect. */
7665 if (range_beginning == range_end)
7666 continue;
7667
7668 range_beginning += base;
7669 range_end += base;
7670
7671 if (ranges_pst != NULL)
7672 addrmap_set_empty (objfile->psymtabs_addrmap,
7673 range_beginning + baseaddr,
7674 range_end - 1 + baseaddr,
7675 ranges_pst);
7676
7677 /* FIXME: This is recording everything as a low-high
7678 segment of consecutive addresses. We should have a
7679 data structure for discontiguous block ranges
7680 instead. */
7681 if (! low_set)
7682 {
7683 low = range_beginning;
7684 high = range_end;
7685 low_set = 1;
7686 }
7687 else
7688 {
7689 if (range_beginning < low)
7690 low = range_beginning;
7691 if (range_end > high)
7692 high = range_end;
7693 }
7694 }
7695
7696 if (! low_set)
7697 /* If the first entry is an end-of-list marker, the range
7698 describes an empty scope, i.e. no instructions. */
7699 return 0;
7700
7701 if (low_return)
7702 *low_return = low;
7703 if (high_return)
7704 *high_return = high;
7705 return 1;
7706 }
7707
7708 /* Get low and high pc attributes from a die. Return 1 if the attributes
7709 are present and valid, otherwise, return 0. Return -1 if the range is
7710 discontinuous, i.e. derived from DW_AT_ranges information. */
7711
7712 static int
7713 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7714 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7715 struct partial_symtab *pst)
7716 {
7717 struct attribute *attr;
7718 struct attribute *attr_high;
7719 CORE_ADDR low = 0;
7720 CORE_ADDR high = 0;
7721 int ret = 0;
7722
7723 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7724 if (attr_high)
7725 {
7726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7727 if (attr)
7728 {
7729 low = DW_ADDR (attr);
7730 if (attr_high->form == DW_FORM_addr
7731 || attr_high->form == DW_FORM_GNU_addr_index)
7732 high = DW_ADDR (attr_high);
7733 else
7734 high = low + DW_UNSND (attr_high);
7735 }
7736 else
7737 /* Found high w/o low attribute. */
7738 return 0;
7739
7740 /* Found consecutive range of addresses. */
7741 ret = 1;
7742 }
7743 else
7744 {
7745 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7746 if (attr != NULL)
7747 {
7748 /* Value of the DW_AT_ranges attribute is the offset in the
7749 .debug_ranges section. */
7750 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
7751 return 0;
7752 /* Found discontinuous range of addresses. */
7753 ret = -1;
7754 }
7755 }
7756
7757 /* read_partial_die has also the strict LOW < HIGH requirement. */
7758 if (high <= low)
7759 return 0;
7760
7761 /* When using the GNU linker, .gnu.linkonce. sections are used to
7762 eliminate duplicate copies of functions and vtables and such.
7763 The linker will arbitrarily choose one and discard the others.
7764 The AT_*_pc values for such functions refer to local labels in
7765 these sections. If the section from that file was discarded, the
7766 labels are not in the output, so the relocs get a value of 0.
7767 If this is a discarded function, mark the pc bounds as invalid,
7768 so that GDB will ignore it. */
7769 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
7770 return 0;
7771
7772 *lowpc = low;
7773 if (highpc)
7774 *highpc = high;
7775 return ret;
7776 }
7777
7778 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
7779 its low and high PC addresses. Do nothing if these addresses could not
7780 be determined. Otherwise, set LOWPC to the low address if it is smaller,
7781 and HIGHPC to the high address if greater than HIGHPC. */
7782
7783 static void
7784 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
7785 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7786 struct dwarf2_cu *cu)
7787 {
7788 CORE_ADDR low, high;
7789 struct die_info *child = die->child;
7790
7791 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
7792 {
7793 *lowpc = min (*lowpc, low);
7794 *highpc = max (*highpc, high);
7795 }
7796
7797 /* If the language does not allow nested subprograms (either inside
7798 subprograms or lexical blocks), we're done. */
7799 if (cu->language != language_ada)
7800 return;
7801
7802 /* Check all the children of the given DIE. If it contains nested
7803 subprograms, then check their pc bounds. Likewise, we need to
7804 check lexical blocks as well, as they may also contain subprogram
7805 definitions. */
7806 while (child && child->tag)
7807 {
7808 if (child->tag == DW_TAG_subprogram
7809 || child->tag == DW_TAG_lexical_block)
7810 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
7811 child = sibling_die (child);
7812 }
7813 }
7814
7815 /* Get the low and high pc's represented by the scope DIE, and store
7816 them in *LOWPC and *HIGHPC. If the correct values can't be
7817 determined, set *LOWPC to -1 and *HIGHPC to 0. */
7818
7819 static void
7820 get_scope_pc_bounds (struct die_info *die,
7821 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7822 struct dwarf2_cu *cu)
7823 {
7824 CORE_ADDR best_low = (CORE_ADDR) -1;
7825 CORE_ADDR best_high = (CORE_ADDR) 0;
7826 CORE_ADDR current_low, current_high;
7827
7828 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
7829 {
7830 best_low = current_low;
7831 best_high = current_high;
7832 }
7833 else
7834 {
7835 struct die_info *child = die->child;
7836
7837 while (child && child->tag)
7838 {
7839 switch (child->tag) {
7840 case DW_TAG_subprogram:
7841 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
7842 break;
7843 case DW_TAG_namespace:
7844 case DW_TAG_module:
7845 /* FIXME: carlton/2004-01-16: Should we do this for
7846 DW_TAG_class_type/DW_TAG_structure_type, too? I think
7847 that current GCC's always emit the DIEs corresponding
7848 to definitions of methods of classes as children of a
7849 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
7850 the DIEs giving the declarations, which could be
7851 anywhere). But I don't see any reason why the
7852 standards says that they have to be there. */
7853 get_scope_pc_bounds (child, &current_low, &current_high, cu);
7854
7855 if (current_low != ((CORE_ADDR) -1))
7856 {
7857 best_low = min (best_low, current_low);
7858 best_high = max (best_high, current_high);
7859 }
7860 break;
7861 default:
7862 /* Ignore. */
7863 break;
7864 }
7865
7866 child = sibling_die (child);
7867 }
7868 }
7869
7870 *lowpc = best_low;
7871 *highpc = best_high;
7872 }
7873
7874 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
7875 in DIE. */
7876
7877 static void
7878 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
7879 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
7880 {
7881 struct objfile *objfile = cu->objfile;
7882 struct attribute *attr;
7883 struct attribute *attr_high;
7884
7885 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7886 if (attr_high)
7887 {
7888 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7889 if (attr)
7890 {
7891 CORE_ADDR low = DW_ADDR (attr);
7892 CORE_ADDR high;
7893 if (attr_high->form == DW_FORM_addr
7894 || attr_high->form == DW_FORM_GNU_addr_index)
7895 high = DW_ADDR (attr_high);
7896 else
7897 high = low + DW_UNSND (attr_high);
7898
7899 record_block_range (block, baseaddr + low, baseaddr + high - 1);
7900 }
7901 }
7902
7903 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7904 if (attr)
7905 {
7906 bfd *obfd = objfile->obfd;
7907
7908 /* The value of the DW_AT_ranges attribute is the offset of the
7909 address range list in the .debug_ranges section. */
7910 unsigned long offset = DW_UNSND (attr);
7911 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
7912
7913 /* For some target architectures, but not others, the
7914 read_address function sign-extends the addresses it returns.
7915 To recognize base address selection entries, we need a
7916 mask. */
7917 unsigned int addr_size = cu->header.addr_size;
7918 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7919
7920 /* The base address, to which the next pair is relative. Note
7921 that this 'base' is a DWARF concept: most entries in a range
7922 list are relative, to reduce the number of relocs against the
7923 debugging information. This is separate from this function's
7924 'baseaddr' argument, which GDB uses to relocate debugging
7925 information from a shared library based on the address at
7926 which the library was loaded. */
7927 CORE_ADDR base = cu->base_address;
7928 int base_known = cu->base_known;
7929
7930 gdb_assert (dwarf2_per_objfile->ranges.readin);
7931 if (offset >= dwarf2_per_objfile->ranges.size)
7932 {
7933 complaint (&symfile_complaints,
7934 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
7935 offset);
7936 return;
7937 }
7938
7939 for (;;)
7940 {
7941 unsigned int bytes_read;
7942 CORE_ADDR start, end;
7943
7944 start = read_address (obfd, buffer, cu, &bytes_read);
7945 buffer += bytes_read;
7946 end = read_address (obfd, buffer, cu, &bytes_read);
7947 buffer += bytes_read;
7948
7949 /* Did we find the end of the range list? */
7950 if (start == 0 && end == 0)
7951 break;
7952
7953 /* Did we find a base address selection entry? */
7954 else if ((start & base_select_mask) == base_select_mask)
7955 {
7956 base = end;
7957 base_known = 1;
7958 }
7959
7960 /* We found an ordinary address range. */
7961 else
7962 {
7963 if (!base_known)
7964 {
7965 complaint (&symfile_complaints,
7966 _("Invalid .debug_ranges data "
7967 "(no base address)"));
7968 return;
7969 }
7970
7971 if (start > end)
7972 {
7973 /* Inverted range entries are invalid. */
7974 complaint (&symfile_complaints,
7975 _("Invalid .debug_ranges data "
7976 "(inverted range)"));
7977 return;
7978 }
7979
7980 /* Empty range entries have no effect. */
7981 if (start == end)
7982 continue;
7983
7984 record_block_range (block,
7985 baseaddr + base + start,
7986 baseaddr + base + end - 1);
7987 }
7988 }
7989 }
7990 }
7991
7992 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
7993 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
7994 during 4.6.0 experimental. */
7995
7996 static int
7997 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
7998 {
7999 const char *cs;
8000 int major, minor, release;
8001 int result = 0;
8002
8003 if (cu->producer == NULL)
8004 {
8005 /* For unknown compilers expect their behavior is DWARF version
8006 compliant.
8007
8008 GCC started to support .debug_types sections by -gdwarf-4 since
8009 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8010 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8011 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8012 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8013
8014 return 0;
8015 }
8016
8017 if (cu->checked_producer)
8018 return cu->producer_is_gxx_lt_4_6;
8019
8020 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8021
8022 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8023 {
8024 /* For non-GCC compilers expect their behavior is DWARF version
8025 compliant. */
8026 }
8027 else
8028 {
8029 cs = &cu->producer[strlen ("GNU ")];
8030 while (*cs && !isdigit (*cs))
8031 cs++;
8032 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8033 {
8034 /* Not recognized as GCC. */
8035 }
8036 else
8037 result = major < 4 || (major == 4 && minor < 6);
8038 }
8039
8040 cu->checked_producer = 1;
8041 cu->producer_is_gxx_lt_4_6 = result;
8042
8043 return result;
8044 }
8045
8046 /* Return the default accessibility type if it is not overriden by
8047 DW_AT_accessibility. */
8048
8049 static enum dwarf_access_attribute
8050 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8051 {
8052 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8053 {
8054 /* The default DWARF 2 accessibility for members is public, the default
8055 accessibility for inheritance is private. */
8056
8057 if (die->tag != DW_TAG_inheritance)
8058 return DW_ACCESS_public;
8059 else
8060 return DW_ACCESS_private;
8061 }
8062 else
8063 {
8064 /* DWARF 3+ defines the default accessibility a different way. The same
8065 rules apply now for DW_TAG_inheritance as for the members and it only
8066 depends on the container kind. */
8067
8068 if (die->parent->tag == DW_TAG_class_type)
8069 return DW_ACCESS_private;
8070 else
8071 return DW_ACCESS_public;
8072 }
8073 }
8074
8075 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8076 offset. If the attribute was not found return 0, otherwise return
8077 1. If it was found but could not properly be handled, set *OFFSET
8078 to 0. */
8079
8080 static int
8081 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8082 LONGEST *offset)
8083 {
8084 struct attribute *attr;
8085
8086 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8087 if (attr != NULL)
8088 {
8089 *offset = 0;
8090
8091 /* Note that we do not check for a section offset first here.
8092 This is because DW_AT_data_member_location is new in DWARF 4,
8093 so if we see it, we can assume that a constant form is really
8094 a constant and not a section offset. */
8095 if (attr_form_is_constant (attr))
8096 *offset = dwarf2_get_attr_constant_value (attr, 0);
8097 else if (attr_form_is_section_offset (attr))
8098 dwarf2_complex_location_expr_complaint ();
8099 else if (attr_form_is_block (attr))
8100 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8101 else
8102 dwarf2_complex_location_expr_complaint ();
8103
8104 return 1;
8105 }
8106
8107 return 0;
8108 }
8109
8110 /* Add an aggregate field to the field list. */
8111
8112 static void
8113 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8114 struct dwarf2_cu *cu)
8115 {
8116 struct objfile *objfile = cu->objfile;
8117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8118 struct nextfield *new_field;
8119 struct attribute *attr;
8120 struct field *fp;
8121 char *fieldname = "";
8122
8123 /* Allocate a new field list entry and link it in. */
8124 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8125 make_cleanup (xfree, new_field);
8126 memset (new_field, 0, sizeof (struct nextfield));
8127
8128 if (die->tag == DW_TAG_inheritance)
8129 {
8130 new_field->next = fip->baseclasses;
8131 fip->baseclasses = new_field;
8132 }
8133 else
8134 {
8135 new_field->next = fip->fields;
8136 fip->fields = new_field;
8137 }
8138 fip->nfields++;
8139
8140 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8141 if (attr)
8142 new_field->accessibility = DW_UNSND (attr);
8143 else
8144 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8145 if (new_field->accessibility != DW_ACCESS_public)
8146 fip->non_public_fields = 1;
8147
8148 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8149 if (attr)
8150 new_field->virtuality = DW_UNSND (attr);
8151 else
8152 new_field->virtuality = DW_VIRTUALITY_none;
8153
8154 fp = &new_field->field;
8155
8156 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8157 {
8158 LONGEST offset;
8159
8160 /* Data member other than a C++ static data member. */
8161
8162 /* Get type of field. */
8163 fp->type = die_type (die, cu);
8164
8165 SET_FIELD_BITPOS (*fp, 0);
8166
8167 /* Get bit size of field (zero if none). */
8168 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8169 if (attr)
8170 {
8171 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8172 }
8173 else
8174 {
8175 FIELD_BITSIZE (*fp) = 0;
8176 }
8177
8178 /* Get bit offset of field. */
8179 if (handle_data_member_location (die, cu, &offset))
8180 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8181 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8182 if (attr)
8183 {
8184 if (gdbarch_bits_big_endian (gdbarch))
8185 {
8186 /* For big endian bits, the DW_AT_bit_offset gives the
8187 additional bit offset from the MSB of the containing
8188 anonymous object to the MSB of the field. We don't
8189 have to do anything special since we don't need to
8190 know the size of the anonymous object. */
8191 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8192 }
8193 else
8194 {
8195 /* For little endian bits, compute the bit offset to the
8196 MSB of the anonymous object, subtract off the number of
8197 bits from the MSB of the field to the MSB of the
8198 object, and then subtract off the number of bits of
8199 the field itself. The result is the bit offset of
8200 the LSB of the field. */
8201 int anonymous_size;
8202 int bit_offset = DW_UNSND (attr);
8203
8204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8205 if (attr)
8206 {
8207 /* The size of the anonymous object containing
8208 the bit field is explicit, so use the
8209 indicated size (in bytes). */
8210 anonymous_size = DW_UNSND (attr);
8211 }
8212 else
8213 {
8214 /* The size of the anonymous object containing
8215 the bit field must be inferred from the type
8216 attribute of the data member containing the
8217 bit field. */
8218 anonymous_size = TYPE_LENGTH (fp->type);
8219 }
8220 SET_FIELD_BITPOS (*fp,
8221 (FIELD_BITPOS (*fp)
8222 + anonymous_size * bits_per_byte
8223 - bit_offset - FIELD_BITSIZE (*fp)));
8224 }
8225 }
8226
8227 /* Get name of field. */
8228 fieldname = dwarf2_name (die, cu);
8229 if (fieldname == NULL)
8230 fieldname = "";
8231
8232 /* The name is already allocated along with this objfile, so we don't
8233 need to duplicate it for the type. */
8234 fp->name = fieldname;
8235
8236 /* Change accessibility for artificial fields (e.g. virtual table
8237 pointer or virtual base class pointer) to private. */
8238 if (dwarf2_attr (die, DW_AT_artificial, cu))
8239 {
8240 FIELD_ARTIFICIAL (*fp) = 1;
8241 new_field->accessibility = DW_ACCESS_private;
8242 fip->non_public_fields = 1;
8243 }
8244 }
8245 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8246 {
8247 /* C++ static member. */
8248
8249 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8250 is a declaration, but all versions of G++ as of this writing
8251 (so through at least 3.2.1) incorrectly generate
8252 DW_TAG_variable tags. */
8253
8254 const char *physname;
8255
8256 /* Get name of field. */
8257 fieldname = dwarf2_name (die, cu);
8258 if (fieldname == NULL)
8259 return;
8260
8261 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8262 if (attr
8263 /* Only create a symbol if this is an external value.
8264 new_symbol checks this and puts the value in the global symbol
8265 table, which we want. If it is not external, new_symbol
8266 will try to put the value in cu->list_in_scope which is wrong. */
8267 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8268 {
8269 /* A static const member, not much different than an enum as far as
8270 we're concerned, except that we can support more types. */
8271 new_symbol (die, NULL, cu);
8272 }
8273
8274 /* Get physical name. */
8275 physname = dwarf2_physname (fieldname, die, cu);
8276
8277 /* The name is already allocated along with this objfile, so we don't
8278 need to duplicate it for the type. */
8279 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8280 FIELD_TYPE (*fp) = die_type (die, cu);
8281 FIELD_NAME (*fp) = fieldname;
8282 }
8283 else if (die->tag == DW_TAG_inheritance)
8284 {
8285 LONGEST offset;
8286
8287 /* C++ base class field. */
8288 if (handle_data_member_location (die, cu, &offset))
8289 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8290 FIELD_BITSIZE (*fp) = 0;
8291 FIELD_TYPE (*fp) = die_type (die, cu);
8292 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8293 fip->nbaseclasses++;
8294 }
8295 }
8296
8297 /* Add a typedef defined in the scope of the FIP's class. */
8298
8299 static void
8300 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8301 struct dwarf2_cu *cu)
8302 {
8303 struct objfile *objfile = cu->objfile;
8304 struct typedef_field_list *new_field;
8305 struct attribute *attr;
8306 struct typedef_field *fp;
8307 char *fieldname = "";
8308
8309 /* Allocate a new field list entry and link it in. */
8310 new_field = xzalloc (sizeof (*new_field));
8311 make_cleanup (xfree, new_field);
8312
8313 gdb_assert (die->tag == DW_TAG_typedef);
8314
8315 fp = &new_field->field;
8316
8317 /* Get name of field. */
8318 fp->name = dwarf2_name (die, cu);
8319 if (fp->name == NULL)
8320 return;
8321
8322 fp->type = read_type_die (die, cu);
8323
8324 new_field->next = fip->typedef_field_list;
8325 fip->typedef_field_list = new_field;
8326 fip->typedef_field_list_count++;
8327 }
8328
8329 /* Create the vector of fields, and attach it to the type. */
8330
8331 static void
8332 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8333 struct dwarf2_cu *cu)
8334 {
8335 int nfields = fip->nfields;
8336
8337 /* Record the field count, allocate space for the array of fields,
8338 and create blank accessibility bitfields if necessary. */
8339 TYPE_NFIELDS (type) = nfields;
8340 TYPE_FIELDS (type) = (struct field *)
8341 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8342 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8343
8344 if (fip->non_public_fields && cu->language != language_ada)
8345 {
8346 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8347
8348 TYPE_FIELD_PRIVATE_BITS (type) =
8349 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8350 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8351
8352 TYPE_FIELD_PROTECTED_BITS (type) =
8353 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8354 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8355
8356 TYPE_FIELD_IGNORE_BITS (type) =
8357 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8358 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8359 }
8360
8361 /* If the type has baseclasses, allocate and clear a bit vector for
8362 TYPE_FIELD_VIRTUAL_BITS. */
8363 if (fip->nbaseclasses && cu->language != language_ada)
8364 {
8365 int num_bytes = B_BYTES (fip->nbaseclasses);
8366 unsigned char *pointer;
8367
8368 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8369 pointer = TYPE_ALLOC (type, num_bytes);
8370 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8371 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8372 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8373 }
8374
8375 /* Copy the saved-up fields into the field vector. Start from the head of
8376 the list, adding to the tail of the field array, so that they end up in
8377 the same order in the array in which they were added to the list. */
8378 while (nfields-- > 0)
8379 {
8380 struct nextfield *fieldp;
8381
8382 if (fip->fields)
8383 {
8384 fieldp = fip->fields;
8385 fip->fields = fieldp->next;
8386 }
8387 else
8388 {
8389 fieldp = fip->baseclasses;
8390 fip->baseclasses = fieldp->next;
8391 }
8392
8393 TYPE_FIELD (type, nfields) = fieldp->field;
8394 switch (fieldp->accessibility)
8395 {
8396 case DW_ACCESS_private:
8397 if (cu->language != language_ada)
8398 SET_TYPE_FIELD_PRIVATE (type, nfields);
8399 break;
8400
8401 case DW_ACCESS_protected:
8402 if (cu->language != language_ada)
8403 SET_TYPE_FIELD_PROTECTED (type, nfields);
8404 break;
8405
8406 case DW_ACCESS_public:
8407 break;
8408
8409 default:
8410 /* Unknown accessibility. Complain and treat it as public. */
8411 {
8412 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8413 fieldp->accessibility);
8414 }
8415 break;
8416 }
8417 if (nfields < fip->nbaseclasses)
8418 {
8419 switch (fieldp->virtuality)
8420 {
8421 case DW_VIRTUALITY_virtual:
8422 case DW_VIRTUALITY_pure_virtual:
8423 if (cu->language == language_ada)
8424 error (_("unexpected virtuality in component of Ada type"));
8425 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8426 break;
8427 }
8428 }
8429 }
8430 }
8431
8432 /* Add a member function to the proper fieldlist. */
8433
8434 static void
8435 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8436 struct type *type, struct dwarf2_cu *cu)
8437 {
8438 struct objfile *objfile = cu->objfile;
8439 struct attribute *attr;
8440 struct fnfieldlist *flp;
8441 int i;
8442 struct fn_field *fnp;
8443 char *fieldname;
8444 struct nextfnfield *new_fnfield;
8445 struct type *this_type;
8446 enum dwarf_access_attribute accessibility;
8447
8448 if (cu->language == language_ada)
8449 error (_("unexpected member function in Ada type"));
8450
8451 /* Get name of member function. */
8452 fieldname = dwarf2_name (die, cu);
8453 if (fieldname == NULL)
8454 return;
8455
8456 /* Look up member function name in fieldlist. */
8457 for (i = 0; i < fip->nfnfields; i++)
8458 {
8459 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8460 break;
8461 }
8462
8463 /* Create new list element if necessary. */
8464 if (i < fip->nfnfields)
8465 flp = &fip->fnfieldlists[i];
8466 else
8467 {
8468 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8469 {
8470 fip->fnfieldlists = (struct fnfieldlist *)
8471 xrealloc (fip->fnfieldlists,
8472 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8473 * sizeof (struct fnfieldlist));
8474 if (fip->nfnfields == 0)
8475 make_cleanup (free_current_contents, &fip->fnfieldlists);
8476 }
8477 flp = &fip->fnfieldlists[fip->nfnfields];
8478 flp->name = fieldname;
8479 flp->length = 0;
8480 flp->head = NULL;
8481 i = fip->nfnfields++;
8482 }
8483
8484 /* Create a new member function field and chain it to the field list
8485 entry. */
8486 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8487 make_cleanup (xfree, new_fnfield);
8488 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8489 new_fnfield->next = flp->head;
8490 flp->head = new_fnfield;
8491 flp->length++;
8492
8493 /* Fill in the member function field info. */
8494 fnp = &new_fnfield->fnfield;
8495
8496 /* Delay processing of the physname until later. */
8497 if (cu->language == language_cplus || cu->language == language_java)
8498 {
8499 add_to_method_list (type, i, flp->length - 1, fieldname,
8500 die, cu);
8501 }
8502 else
8503 {
8504 const char *physname = dwarf2_physname (fieldname, die, cu);
8505 fnp->physname = physname ? physname : "";
8506 }
8507
8508 fnp->type = alloc_type (objfile);
8509 this_type = read_type_die (die, cu);
8510 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8511 {
8512 int nparams = TYPE_NFIELDS (this_type);
8513
8514 /* TYPE is the domain of this method, and THIS_TYPE is the type
8515 of the method itself (TYPE_CODE_METHOD). */
8516 smash_to_method_type (fnp->type, type,
8517 TYPE_TARGET_TYPE (this_type),
8518 TYPE_FIELDS (this_type),
8519 TYPE_NFIELDS (this_type),
8520 TYPE_VARARGS (this_type));
8521
8522 /* Handle static member functions.
8523 Dwarf2 has no clean way to discern C++ static and non-static
8524 member functions. G++ helps GDB by marking the first
8525 parameter for non-static member functions (which is the this
8526 pointer) as artificial. We obtain this information from
8527 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8528 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8529 fnp->voffset = VOFFSET_STATIC;
8530 }
8531 else
8532 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8533 dwarf2_full_name (fieldname, die, cu));
8534
8535 /* Get fcontext from DW_AT_containing_type if present. */
8536 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8537 fnp->fcontext = die_containing_type (die, cu);
8538
8539 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8540 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8541
8542 /* Get accessibility. */
8543 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8544 if (attr)
8545 accessibility = DW_UNSND (attr);
8546 else
8547 accessibility = dwarf2_default_access_attribute (die, cu);
8548 switch (accessibility)
8549 {
8550 case DW_ACCESS_private:
8551 fnp->is_private = 1;
8552 break;
8553 case DW_ACCESS_protected:
8554 fnp->is_protected = 1;
8555 break;
8556 }
8557
8558 /* Check for artificial methods. */
8559 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8560 if (attr && DW_UNSND (attr) != 0)
8561 fnp->is_artificial = 1;
8562
8563 /* Get index in virtual function table if it is a virtual member
8564 function. For older versions of GCC, this is an offset in the
8565 appropriate virtual table, as specified by DW_AT_containing_type.
8566 For everyone else, it is an expression to be evaluated relative
8567 to the object address. */
8568
8569 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8570 if (attr)
8571 {
8572 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8573 {
8574 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8575 {
8576 /* Old-style GCC. */
8577 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8578 }
8579 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8580 || (DW_BLOCK (attr)->size > 1
8581 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8582 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8583 {
8584 struct dwarf_block blk;
8585 int offset;
8586
8587 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8588 ? 1 : 2);
8589 blk.size = DW_BLOCK (attr)->size - offset;
8590 blk.data = DW_BLOCK (attr)->data + offset;
8591 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8592 if ((fnp->voffset % cu->header.addr_size) != 0)
8593 dwarf2_complex_location_expr_complaint ();
8594 else
8595 fnp->voffset /= cu->header.addr_size;
8596 fnp->voffset += 2;
8597 }
8598 else
8599 dwarf2_complex_location_expr_complaint ();
8600
8601 if (!fnp->fcontext)
8602 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8603 }
8604 else if (attr_form_is_section_offset (attr))
8605 {
8606 dwarf2_complex_location_expr_complaint ();
8607 }
8608 else
8609 {
8610 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8611 fieldname);
8612 }
8613 }
8614 else
8615 {
8616 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8617 if (attr && DW_UNSND (attr))
8618 {
8619 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8620 complaint (&symfile_complaints,
8621 _("Member function \"%s\" (offset %d) is virtual "
8622 "but the vtable offset is not specified"),
8623 fieldname, die->offset.sect_off);
8624 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8625 TYPE_CPLUS_DYNAMIC (type) = 1;
8626 }
8627 }
8628 }
8629
8630 /* Create the vector of member function fields, and attach it to the type. */
8631
8632 static void
8633 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8634 struct dwarf2_cu *cu)
8635 {
8636 struct fnfieldlist *flp;
8637 int i;
8638
8639 if (cu->language == language_ada)
8640 error (_("unexpected member functions in Ada type"));
8641
8642 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8643 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8644 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8645
8646 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8647 {
8648 struct nextfnfield *nfp = flp->head;
8649 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8650 int k;
8651
8652 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8653 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8654 fn_flp->fn_fields = (struct fn_field *)
8655 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8656 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8657 fn_flp->fn_fields[k] = nfp->fnfield;
8658 }
8659
8660 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8661 }
8662
8663 /* Returns non-zero if NAME is the name of a vtable member in CU's
8664 language, zero otherwise. */
8665 static int
8666 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8667 {
8668 static const char vptr[] = "_vptr";
8669 static const char vtable[] = "vtable";
8670
8671 /* Look for the C++ and Java forms of the vtable. */
8672 if ((cu->language == language_java
8673 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8674 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8675 && is_cplus_marker (name[sizeof (vptr) - 1])))
8676 return 1;
8677
8678 return 0;
8679 }
8680
8681 /* GCC outputs unnamed structures that are really pointers to member
8682 functions, with the ABI-specified layout. If TYPE describes
8683 such a structure, smash it into a member function type.
8684
8685 GCC shouldn't do this; it should just output pointer to member DIEs.
8686 This is GCC PR debug/28767. */
8687
8688 static void
8689 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8690 {
8691 struct type *pfn_type, *domain_type, *new_type;
8692
8693 /* Check for a structure with no name and two children. */
8694 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8695 return;
8696
8697 /* Check for __pfn and __delta members. */
8698 if (TYPE_FIELD_NAME (type, 0) == NULL
8699 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8700 || TYPE_FIELD_NAME (type, 1) == NULL
8701 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8702 return;
8703
8704 /* Find the type of the method. */
8705 pfn_type = TYPE_FIELD_TYPE (type, 0);
8706 if (pfn_type == NULL
8707 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8708 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8709 return;
8710
8711 /* Look for the "this" argument. */
8712 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8713 if (TYPE_NFIELDS (pfn_type) == 0
8714 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8715 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8716 return;
8717
8718 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
8719 new_type = alloc_type (objfile);
8720 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
8721 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8722 TYPE_VARARGS (pfn_type));
8723 smash_to_methodptr_type (type, new_type);
8724 }
8725
8726 /* Called when we find the DIE that starts a structure or union scope
8727 (definition) to create a type for the structure or union. Fill in
8728 the type's name and general properties; the members will not be
8729 processed until process_structure_type.
8730
8731 NOTE: we need to call these functions regardless of whether or not the
8732 DIE has a DW_AT_name attribute, since it might be an anonymous
8733 structure or union. This gets the type entered into our set of
8734 user defined types.
8735
8736 However, if the structure is incomplete (an opaque struct/union)
8737 then suppress creating a symbol table entry for it since gdb only
8738 wants to find the one with the complete definition. Note that if
8739 it is complete, we just call new_symbol, which does it's own
8740 checking about whether the struct/union is anonymous or not (and
8741 suppresses creating a symbol table entry itself). */
8742
8743 static struct type *
8744 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
8745 {
8746 struct objfile *objfile = cu->objfile;
8747 struct type *type;
8748 struct attribute *attr;
8749 char *name;
8750
8751 /* If the definition of this type lives in .debug_types, read that type.
8752 Don't follow DW_AT_specification though, that will take us back up
8753 the chain and we want to go down. */
8754 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8755 if (attr)
8756 {
8757 struct dwarf2_cu *type_cu = cu;
8758 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
8759
8760 /* We could just recurse on read_structure_type, but we need to call
8761 get_die_type to ensure only one type for this DIE is created.
8762 This is important, for example, because for c++ classes we need
8763 TYPE_NAME set which is only done by new_symbol. Blech. */
8764 type = read_type_die (type_die, type_cu);
8765
8766 /* TYPE_CU may not be the same as CU.
8767 Ensure TYPE is recorded in CU's type_hash table. */
8768 return set_die_type (die, type, cu);
8769 }
8770
8771 type = alloc_type (objfile);
8772 INIT_CPLUS_SPECIFIC (type);
8773
8774 name = dwarf2_name (die, cu);
8775 if (name != NULL)
8776 {
8777 if (cu->language == language_cplus
8778 || cu->language == language_java)
8779 {
8780 char *full_name = (char *) dwarf2_full_name (name, die, cu);
8781
8782 /* dwarf2_full_name might have already finished building the DIE's
8783 type. If so, there is no need to continue. */
8784 if (get_die_type (die, cu) != NULL)
8785 return get_die_type (die, cu);
8786
8787 TYPE_TAG_NAME (type) = full_name;
8788 if (die->tag == DW_TAG_structure_type
8789 || die->tag == DW_TAG_class_type)
8790 TYPE_NAME (type) = TYPE_TAG_NAME (type);
8791 }
8792 else
8793 {
8794 /* The name is already allocated along with this objfile, so
8795 we don't need to duplicate it for the type. */
8796 TYPE_TAG_NAME (type) = (char *) name;
8797 if (die->tag == DW_TAG_class_type)
8798 TYPE_NAME (type) = TYPE_TAG_NAME (type);
8799 }
8800 }
8801
8802 if (die->tag == DW_TAG_structure_type)
8803 {
8804 TYPE_CODE (type) = TYPE_CODE_STRUCT;
8805 }
8806 else if (die->tag == DW_TAG_union_type)
8807 {
8808 TYPE_CODE (type) = TYPE_CODE_UNION;
8809 }
8810 else
8811 {
8812 TYPE_CODE (type) = TYPE_CODE_CLASS;
8813 }
8814
8815 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
8816 TYPE_DECLARED_CLASS (type) = 1;
8817
8818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8819 if (attr)
8820 {
8821 TYPE_LENGTH (type) = DW_UNSND (attr);
8822 }
8823 else
8824 {
8825 TYPE_LENGTH (type) = 0;
8826 }
8827
8828 TYPE_STUB_SUPPORTED (type) = 1;
8829 if (die_is_declaration (die, cu))
8830 TYPE_STUB (type) = 1;
8831 else if (attr == NULL && die->child == NULL
8832 && producer_is_realview (cu->producer))
8833 /* RealView does not output the required DW_AT_declaration
8834 on incomplete types. */
8835 TYPE_STUB (type) = 1;
8836
8837 /* We need to add the type field to the die immediately so we don't
8838 infinitely recurse when dealing with pointers to the structure
8839 type within the structure itself. */
8840 set_die_type (die, type, cu);
8841
8842 /* set_die_type should be already done. */
8843 set_descriptive_type (type, die, cu);
8844
8845 return type;
8846 }
8847
8848 /* Finish creating a structure or union type, including filling in
8849 its members and creating a symbol for it. */
8850
8851 static void
8852 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
8853 {
8854 struct objfile *objfile = cu->objfile;
8855 struct die_info *child_die = die->child;
8856 struct type *type;
8857
8858 type = get_die_type (die, cu);
8859 if (type == NULL)
8860 type = read_structure_type (die, cu);
8861
8862 if (die->child != NULL && ! die_is_declaration (die, cu))
8863 {
8864 struct field_info fi;
8865 struct die_info *child_die;
8866 VEC (symbolp) *template_args = NULL;
8867 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8868
8869 memset (&fi, 0, sizeof (struct field_info));
8870
8871 child_die = die->child;
8872
8873 while (child_die && child_die->tag)
8874 {
8875 if (child_die->tag == DW_TAG_member
8876 || child_die->tag == DW_TAG_variable)
8877 {
8878 /* NOTE: carlton/2002-11-05: A C++ static data member
8879 should be a DW_TAG_member that is a declaration, but
8880 all versions of G++ as of this writing (so through at
8881 least 3.2.1) incorrectly generate DW_TAG_variable
8882 tags for them instead. */
8883 dwarf2_add_field (&fi, child_die, cu);
8884 }
8885 else if (child_die->tag == DW_TAG_subprogram)
8886 {
8887 /* C++ member function. */
8888 dwarf2_add_member_fn (&fi, child_die, type, cu);
8889 }
8890 else if (child_die->tag == DW_TAG_inheritance)
8891 {
8892 /* C++ base class field. */
8893 dwarf2_add_field (&fi, child_die, cu);
8894 }
8895 else if (child_die->tag == DW_TAG_typedef)
8896 dwarf2_add_typedef (&fi, child_die, cu);
8897 else if (child_die->tag == DW_TAG_template_type_param
8898 || child_die->tag == DW_TAG_template_value_param)
8899 {
8900 struct symbol *arg = new_symbol (child_die, NULL, cu);
8901
8902 if (arg != NULL)
8903 VEC_safe_push (symbolp, template_args, arg);
8904 }
8905
8906 child_die = sibling_die (child_die);
8907 }
8908
8909 /* Attach template arguments to type. */
8910 if (! VEC_empty (symbolp, template_args))
8911 {
8912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8913 TYPE_N_TEMPLATE_ARGUMENTS (type)
8914 = VEC_length (symbolp, template_args);
8915 TYPE_TEMPLATE_ARGUMENTS (type)
8916 = obstack_alloc (&objfile->objfile_obstack,
8917 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8918 * sizeof (struct symbol *)));
8919 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
8920 VEC_address (symbolp, template_args),
8921 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8922 * sizeof (struct symbol *)));
8923 VEC_free (symbolp, template_args);
8924 }
8925
8926 /* Attach fields and member functions to the type. */
8927 if (fi.nfields)
8928 dwarf2_attach_fields_to_type (&fi, type, cu);
8929 if (fi.nfnfields)
8930 {
8931 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
8932
8933 /* Get the type which refers to the base class (possibly this
8934 class itself) which contains the vtable pointer for the current
8935 class from the DW_AT_containing_type attribute. This use of
8936 DW_AT_containing_type is a GNU extension. */
8937
8938 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8939 {
8940 struct type *t = die_containing_type (die, cu);
8941
8942 TYPE_VPTR_BASETYPE (type) = t;
8943 if (type == t)
8944 {
8945 int i;
8946
8947 /* Our own class provides vtbl ptr. */
8948 for (i = TYPE_NFIELDS (t) - 1;
8949 i >= TYPE_N_BASECLASSES (t);
8950 --i)
8951 {
8952 const char *fieldname = TYPE_FIELD_NAME (t, i);
8953
8954 if (is_vtable_name (fieldname, cu))
8955 {
8956 TYPE_VPTR_FIELDNO (type) = i;
8957 break;
8958 }
8959 }
8960
8961 /* Complain if virtual function table field not found. */
8962 if (i < TYPE_N_BASECLASSES (t))
8963 complaint (&symfile_complaints,
8964 _("virtual function table pointer "
8965 "not found when defining class '%s'"),
8966 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
8967 "");
8968 }
8969 else
8970 {
8971 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
8972 }
8973 }
8974 else if (cu->producer
8975 && strncmp (cu->producer,
8976 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
8977 {
8978 /* The IBM XLC compiler does not provide direct indication
8979 of the containing type, but the vtable pointer is
8980 always named __vfp. */
8981
8982 int i;
8983
8984 for (i = TYPE_NFIELDS (type) - 1;
8985 i >= TYPE_N_BASECLASSES (type);
8986 --i)
8987 {
8988 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
8989 {
8990 TYPE_VPTR_FIELDNO (type) = i;
8991 TYPE_VPTR_BASETYPE (type) = type;
8992 break;
8993 }
8994 }
8995 }
8996 }
8997
8998 /* Copy fi.typedef_field_list linked list elements content into the
8999 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9000 if (fi.typedef_field_list)
9001 {
9002 int i = fi.typedef_field_list_count;
9003
9004 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9005 TYPE_TYPEDEF_FIELD_ARRAY (type)
9006 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9007 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9008
9009 /* Reverse the list order to keep the debug info elements order. */
9010 while (--i >= 0)
9011 {
9012 struct typedef_field *dest, *src;
9013
9014 dest = &TYPE_TYPEDEF_FIELD (type, i);
9015 src = &fi.typedef_field_list->field;
9016 fi.typedef_field_list = fi.typedef_field_list->next;
9017 *dest = *src;
9018 }
9019 }
9020
9021 do_cleanups (back_to);
9022
9023 if (HAVE_CPLUS_STRUCT (type))
9024 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9025 }
9026
9027 quirk_gcc_member_function_pointer (type, objfile);
9028
9029 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9030 snapshots) has been known to create a die giving a declaration
9031 for a class that has, as a child, a die giving a definition for a
9032 nested class. So we have to process our children even if the
9033 current die is a declaration. Normally, of course, a declaration
9034 won't have any children at all. */
9035
9036 while (child_die != NULL && child_die->tag)
9037 {
9038 if (child_die->tag == DW_TAG_member
9039 || child_die->tag == DW_TAG_variable
9040 || child_die->tag == DW_TAG_inheritance
9041 || child_die->tag == DW_TAG_template_value_param
9042 || child_die->tag == DW_TAG_template_type_param)
9043 {
9044 /* Do nothing. */
9045 }
9046 else
9047 process_die (child_die, cu);
9048
9049 child_die = sibling_die (child_die);
9050 }
9051
9052 /* Do not consider external references. According to the DWARF standard,
9053 these DIEs are identified by the fact that they have no byte_size
9054 attribute, and a declaration attribute. */
9055 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9056 || !die_is_declaration (die, cu))
9057 new_symbol (die, type, cu);
9058 }
9059
9060 /* Given a DW_AT_enumeration_type die, set its type. We do not
9061 complete the type's fields yet, or create any symbols. */
9062
9063 static struct type *
9064 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9065 {
9066 struct objfile *objfile = cu->objfile;
9067 struct type *type;
9068 struct attribute *attr;
9069 const char *name;
9070
9071 /* If the definition of this type lives in .debug_types, read that type.
9072 Don't follow DW_AT_specification though, that will take us back up
9073 the chain and we want to go down. */
9074 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9075 if (attr)
9076 {
9077 struct dwarf2_cu *type_cu = cu;
9078 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9079
9080 type = read_type_die (type_die, type_cu);
9081
9082 /* TYPE_CU may not be the same as CU.
9083 Ensure TYPE is recorded in CU's type_hash table. */
9084 return set_die_type (die, type, cu);
9085 }
9086
9087 type = alloc_type (objfile);
9088
9089 TYPE_CODE (type) = TYPE_CODE_ENUM;
9090 name = dwarf2_full_name (NULL, die, cu);
9091 if (name != NULL)
9092 TYPE_TAG_NAME (type) = (char *) name;
9093
9094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9095 if (attr)
9096 {
9097 TYPE_LENGTH (type) = DW_UNSND (attr);
9098 }
9099 else
9100 {
9101 TYPE_LENGTH (type) = 0;
9102 }
9103
9104 /* The enumeration DIE can be incomplete. In Ada, any type can be
9105 declared as private in the package spec, and then defined only
9106 inside the package body. Such types are known as Taft Amendment
9107 Types. When another package uses such a type, an incomplete DIE
9108 may be generated by the compiler. */
9109 if (die_is_declaration (die, cu))
9110 TYPE_STUB (type) = 1;
9111
9112 return set_die_type (die, type, cu);
9113 }
9114
9115 /* Given a pointer to a die which begins an enumeration, process all
9116 the dies that define the members of the enumeration, and create the
9117 symbol for the enumeration type.
9118
9119 NOTE: We reverse the order of the element list. */
9120
9121 static void
9122 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9123 {
9124 struct type *this_type;
9125
9126 this_type = get_die_type (die, cu);
9127 if (this_type == NULL)
9128 this_type = read_enumeration_type (die, cu);
9129
9130 if (die->child != NULL)
9131 {
9132 struct die_info *child_die;
9133 struct symbol *sym;
9134 struct field *fields = NULL;
9135 int num_fields = 0;
9136 int unsigned_enum = 1;
9137 char *name;
9138 int flag_enum = 1;
9139 ULONGEST mask = 0;
9140
9141 child_die = die->child;
9142 while (child_die && child_die->tag)
9143 {
9144 if (child_die->tag != DW_TAG_enumerator)
9145 {
9146 process_die (child_die, cu);
9147 }
9148 else
9149 {
9150 name = dwarf2_name (child_die, cu);
9151 if (name)
9152 {
9153 sym = new_symbol (child_die, this_type, cu);
9154 if (SYMBOL_VALUE (sym) < 0)
9155 {
9156 unsigned_enum = 0;
9157 flag_enum = 0;
9158 }
9159 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9160 flag_enum = 0;
9161 else
9162 mask |= SYMBOL_VALUE (sym);
9163
9164 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9165 {
9166 fields = (struct field *)
9167 xrealloc (fields,
9168 (num_fields + DW_FIELD_ALLOC_CHUNK)
9169 * sizeof (struct field));
9170 }
9171
9172 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9173 FIELD_TYPE (fields[num_fields]) = NULL;
9174 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9175 FIELD_BITSIZE (fields[num_fields]) = 0;
9176
9177 num_fields++;
9178 }
9179 }
9180
9181 child_die = sibling_die (child_die);
9182 }
9183
9184 if (num_fields)
9185 {
9186 TYPE_NFIELDS (this_type) = num_fields;
9187 TYPE_FIELDS (this_type) = (struct field *)
9188 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9189 memcpy (TYPE_FIELDS (this_type), fields,
9190 sizeof (struct field) * num_fields);
9191 xfree (fields);
9192 }
9193 if (unsigned_enum)
9194 TYPE_UNSIGNED (this_type) = 1;
9195 if (flag_enum)
9196 TYPE_FLAG_ENUM (this_type) = 1;
9197 }
9198
9199 /* If we are reading an enum from a .debug_types unit, and the enum
9200 is a declaration, and the enum is not the signatured type in the
9201 unit, then we do not want to add a symbol for it. Adding a
9202 symbol would in some cases obscure the true definition of the
9203 enum, giving users an incomplete type when the definition is
9204 actually available. Note that we do not want to do this for all
9205 enums which are just declarations, because C++0x allows forward
9206 enum declarations. */
9207 if (cu->per_cu->is_debug_types
9208 && die_is_declaration (die, cu))
9209 {
9210 struct signatured_type *sig_type;
9211
9212 sig_type
9213 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9214 cu->per_cu->info_or_types_section,
9215 cu->per_cu->offset);
9216 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9217 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9218 return;
9219 }
9220
9221 new_symbol (die, this_type, cu);
9222 }
9223
9224 /* Extract all information from a DW_TAG_array_type DIE and put it in
9225 the DIE's type field. For now, this only handles one dimensional
9226 arrays. */
9227
9228 static struct type *
9229 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9230 {
9231 struct objfile *objfile = cu->objfile;
9232 struct die_info *child_die;
9233 struct type *type;
9234 struct type *element_type, *range_type, *index_type;
9235 struct type **range_types = NULL;
9236 struct attribute *attr;
9237 int ndim = 0;
9238 struct cleanup *back_to;
9239 char *name;
9240
9241 element_type = die_type (die, cu);
9242
9243 /* The die_type call above may have already set the type for this DIE. */
9244 type = get_die_type (die, cu);
9245 if (type)
9246 return type;
9247
9248 /* Irix 6.2 native cc creates array types without children for
9249 arrays with unspecified length. */
9250 if (die->child == NULL)
9251 {
9252 index_type = objfile_type (objfile)->builtin_int;
9253 range_type = create_range_type (NULL, index_type, 0, -1);
9254 type = create_array_type (NULL, element_type, range_type);
9255 return set_die_type (die, type, cu);
9256 }
9257
9258 back_to = make_cleanup (null_cleanup, NULL);
9259 child_die = die->child;
9260 while (child_die && child_die->tag)
9261 {
9262 if (child_die->tag == DW_TAG_subrange_type)
9263 {
9264 struct type *child_type = read_type_die (child_die, cu);
9265
9266 if (child_type != NULL)
9267 {
9268 /* The range type was succesfully read. Save it for the
9269 array type creation. */
9270 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9271 {
9272 range_types = (struct type **)
9273 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9274 * sizeof (struct type *));
9275 if (ndim == 0)
9276 make_cleanup (free_current_contents, &range_types);
9277 }
9278 range_types[ndim++] = child_type;
9279 }
9280 }
9281 child_die = sibling_die (child_die);
9282 }
9283
9284 /* Dwarf2 dimensions are output from left to right, create the
9285 necessary array types in backwards order. */
9286
9287 type = element_type;
9288
9289 if (read_array_order (die, cu) == DW_ORD_col_major)
9290 {
9291 int i = 0;
9292
9293 while (i < ndim)
9294 type = create_array_type (NULL, type, range_types[i++]);
9295 }
9296 else
9297 {
9298 while (ndim-- > 0)
9299 type = create_array_type (NULL, type, range_types[ndim]);
9300 }
9301
9302 /* Understand Dwarf2 support for vector types (like they occur on
9303 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9304 array type. This is not part of the Dwarf2/3 standard yet, but a
9305 custom vendor extension. The main difference between a regular
9306 array and the vector variant is that vectors are passed by value
9307 to functions. */
9308 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9309 if (attr)
9310 make_vector_type (type);
9311
9312 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9313 implementation may choose to implement triple vectors using this
9314 attribute. */
9315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9316 if (attr)
9317 {
9318 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9319 TYPE_LENGTH (type) = DW_UNSND (attr);
9320 else
9321 complaint (&symfile_complaints,
9322 _("DW_AT_byte_size for array type smaller "
9323 "than the total size of elements"));
9324 }
9325
9326 name = dwarf2_name (die, cu);
9327 if (name)
9328 TYPE_NAME (type) = name;
9329
9330 /* Install the type in the die. */
9331 set_die_type (die, type, cu);
9332
9333 /* set_die_type should be already done. */
9334 set_descriptive_type (type, die, cu);
9335
9336 do_cleanups (back_to);
9337
9338 return type;
9339 }
9340
9341 static enum dwarf_array_dim_ordering
9342 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9343 {
9344 struct attribute *attr;
9345
9346 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9347
9348 if (attr) return DW_SND (attr);
9349
9350 /* GNU F77 is a special case, as at 08/2004 array type info is the
9351 opposite order to the dwarf2 specification, but data is still
9352 laid out as per normal fortran.
9353
9354 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9355 version checking. */
9356
9357 if (cu->language == language_fortran
9358 && cu->producer && strstr (cu->producer, "GNU F77"))
9359 {
9360 return DW_ORD_row_major;
9361 }
9362
9363 switch (cu->language_defn->la_array_ordering)
9364 {
9365 case array_column_major:
9366 return DW_ORD_col_major;
9367 case array_row_major:
9368 default:
9369 return DW_ORD_row_major;
9370 };
9371 }
9372
9373 /* Extract all information from a DW_TAG_set_type DIE and put it in
9374 the DIE's type field. */
9375
9376 static struct type *
9377 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9378 {
9379 struct type *domain_type, *set_type;
9380 struct attribute *attr;
9381
9382 domain_type = die_type (die, cu);
9383
9384 /* The die_type call above may have already set the type for this DIE. */
9385 set_type = get_die_type (die, cu);
9386 if (set_type)
9387 return set_type;
9388
9389 set_type = create_set_type (NULL, domain_type);
9390
9391 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9392 if (attr)
9393 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9394
9395 return set_die_type (die, set_type, cu);
9396 }
9397
9398 /* First cut: install each common block member as a global variable. */
9399
9400 static void
9401 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9402 {
9403 struct die_info *child_die;
9404 struct attribute *attr;
9405 struct symbol *sym;
9406 CORE_ADDR base = (CORE_ADDR) 0;
9407
9408 attr = dwarf2_attr (die, DW_AT_location, cu);
9409 if (attr)
9410 {
9411 /* Support the .debug_loc offsets. */
9412 if (attr_form_is_block (attr))
9413 {
9414 base = decode_locdesc (DW_BLOCK (attr), cu);
9415 }
9416 else if (attr_form_is_section_offset (attr))
9417 {
9418 dwarf2_complex_location_expr_complaint ();
9419 }
9420 else
9421 {
9422 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9423 "common block member");
9424 }
9425 }
9426 if (die->child != NULL)
9427 {
9428 child_die = die->child;
9429 while (child_die && child_die->tag)
9430 {
9431 LONGEST offset;
9432
9433 sym = new_symbol (child_die, NULL, cu);
9434 if (sym != NULL
9435 && handle_data_member_location (child_die, cu, &offset))
9436 {
9437 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9438 add_symbol_to_list (sym, &global_symbols);
9439 }
9440 child_die = sibling_die (child_die);
9441 }
9442 }
9443 }
9444
9445 /* Create a type for a C++ namespace. */
9446
9447 static struct type *
9448 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9449 {
9450 struct objfile *objfile = cu->objfile;
9451 const char *previous_prefix, *name;
9452 int is_anonymous;
9453 struct type *type;
9454
9455 /* For extensions, reuse the type of the original namespace. */
9456 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9457 {
9458 struct die_info *ext_die;
9459 struct dwarf2_cu *ext_cu = cu;
9460
9461 ext_die = dwarf2_extension (die, &ext_cu);
9462 type = read_type_die (ext_die, ext_cu);
9463
9464 /* EXT_CU may not be the same as CU.
9465 Ensure TYPE is recorded in CU's type_hash table. */
9466 return set_die_type (die, type, cu);
9467 }
9468
9469 name = namespace_name (die, &is_anonymous, cu);
9470
9471 /* Now build the name of the current namespace. */
9472
9473 previous_prefix = determine_prefix (die, cu);
9474 if (previous_prefix[0] != '\0')
9475 name = typename_concat (&objfile->objfile_obstack,
9476 previous_prefix, name, 0, cu);
9477
9478 /* Create the type. */
9479 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9480 objfile);
9481 TYPE_NAME (type) = (char *) name;
9482 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9483
9484 return set_die_type (die, type, cu);
9485 }
9486
9487 /* Read a C++ namespace. */
9488
9489 static void
9490 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9491 {
9492 struct objfile *objfile = cu->objfile;
9493 int is_anonymous;
9494
9495 /* Add a symbol associated to this if we haven't seen the namespace
9496 before. Also, add a using directive if it's an anonymous
9497 namespace. */
9498
9499 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9500 {
9501 struct type *type;
9502
9503 type = read_type_die (die, cu);
9504 new_symbol (die, type, cu);
9505
9506 namespace_name (die, &is_anonymous, cu);
9507 if (is_anonymous)
9508 {
9509 const char *previous_prefix = determine_prefix (die, cu);
9510
9511 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9512 NULL, NULL, &objfile->objfile_obstack);
9513 }
9514 }
9515
9516 if (die->child != NULL)
9517 {
9518 struct die_info *child_die = die->child;
9519
9520 while (child_die && child_die->tag)
9521 {
9522 process_die (child_die, cu);
9523 child_die = sibling_die (child_die);
9524 }
9525 }
9526 }
9527
9528 /* Read a Fortran module as type. This DIE can be only a declaration used for
9529 imported module. Still we need that type as local Fortran "use ... only"
9530 declaration imports depend on the created type in determine_prefix. */
9531
9532 static struct type *
9533 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9534 {
9535 struct objfile *objfile = cu->objfile;
9536 char *module_name;
9537 struct type *type;
9538
9539 module_name = dwarf2_name (die, cu);
9540 if (!module_name)
9541 complaint (&symfile_complaints,
9542 _("DW_TAG_module has no name, offset 0x%x"),
9543 die->offset.sect_off);
9544 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9545
9546 /* determine_prefix uses TYPE_TAG_NAME. */
9547 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9548
9549 return set_die_type (die, type, cu);
9550 }
9551
9552 /* Read a Fortran module. */
9553
9554 static void
9555 read_module (struct die_info *die, struct dwarf2_cu *cu)
9556 {
9557 struct die_info *child_die = die->child;
9558
9559 while (child_die && child_die->tag)
9560 {
9561 process_die (child_die, cu);
9562 child_die = sibling_die (child_die);
9563 }
9564 }
9565
9566 /* Return the name of the namespace represented by DIE. Set
9567 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9568 namespace. */
9569
9570 static const char *
9571 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9572 {
9573 struct die_info *current_die;
9574 const char *name = NULL;
9575
9576 /* Loop through the extensions until we find a name. */
9577
9578 for (current_die = die;
9579 current_die != NULL;
9580 current_die = dwarf2_extension (die, &cu))
9581 {
9582 name = dwarf2_name (current_die, cu);
9583 if (name != NULL)
9584 break;
9585 }
9586
9587 /* Is it an anonymous namespace? */
9588
9589 *is_anonymous = (name == NULL);
9590 if (*is_anonymous)
9591 name = CP_ANONYMOUS_NAMESPACE_STR;
9592
9593 return name;
9594 }
9595
9596 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9597 the user defined type vector. */
9598
9599 static struct type *
9600 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9601 {
9602 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9603 struct comp_unit_head *cu_header = &cu->header;
9604 struct type *type;
9605 struct attribute *attr_byte_size;
9606 struct attribute *attr_address_class;
9607 int byte_size, addr_class;
9608 struct type *target_type;
9609
9610 target_type = die_type (die, cu);
9611
9612 /* The die_type call above may have already set the type for this DIE. */
9613 type = get_die_type (die, cu);
9614 if (type)
9615 return type;
9616
9617 type = lookup_pointer_type (target_type);
9618
9619 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9620 if (attr_byte_size)
9621 byte_size = DW_UNSND (attr_byte_size);
9622 else
9623 byte_size = cu_header->addr_size;
9624
9625 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9626 if (attr_address_class)
9627 addr_class = DW_UNSND (attr_address_class);
9628 else
9629 addr_class = DW_ADDR_none;
9630
9631 /* If the pointer size or address class is different than the
9632 default, create a type variant marked as such and set the
9633 length accordingly. */
9634 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9635 {
9636 if (gdbarch_address_class_type_flags_p (gdbarch))
9637 {
9638 int type_flags;
9639
9640 type_flags = gdbarch_address_class_type_flags
9641 (gdbarch, byte_size, addr_class);
9642 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9643 == 0);
9644 type = make_type_with_address_space (type, type_flags);
9645 }
9646 else if (TYPE_LENGTH (type) != byte_size)
9647 {
9648 complaint (&symfile_complaints,
9649 _("invalid pointer size %d"), byte_size);
9650 }
9651 else
9652 {
9653 /* Should we also complain about unhandled address classes? */
9654 }
9655 }
9656
9657 TYPE_LENGTH (type) = byte_size;
9658 return set_die_type (die, type, cu);
9659 }
9660
9661 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9662 the user defined type vector. */
9663
9664 static struct type *
9665 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9666 {
9667 struct type *type;
9668 struct type *to_type;
9669 struct type *domain;
9670
9671 to_type = die_type (die, cu);
9672 domain = die_containing_type (die, cu);
9673
9674 /* The calls above may have already set the type for this DIE. */
9675 type = get_die_type (die, cu);
9676 if (type)
9677 return type;
9678
9679 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9680 type = lookup_methodptr_type (to_type);
9681 else
9682 type = lookup_memberptr_type (to_type, domain);
9683
9684 return set_die_type (die, type, cu);
9685 }
9686
9687 /* Extract all information from a DW_TAG_reference_type DIE and add to
9688 the user defined type vector. */
9689
9690 static struct type *
9691 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9692 {
9693 struct comp_unit_head *cu_header = &cu->header;
9694 struct type *type, *target_type;
9695 struct attribute *attr;
9696
9697 target_type = die_type (die, cu);
9698
9699 /* The die_type call above may have already set the type for this DIE. */
9700 type = get_die_type (die, cu);
9701 if (type)
9702 return type;
9703
9704 type = lookup_reference_type (target_type);
9705 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9706 if (attr)
9707 {
9708 TYPE_LENGTH (type) = DW_UNSND (attr);
9709 }
9710 else
9711 {
9712 TYPE_LENGTH (type) = cu_header->addr_size;
9713 }
9714 return set_die_type (die, type, cu);
9715 }
9716
9717 static struct type *
9718 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
9719 {
9720 struct type *base_type, *cv_type;
9721
9722 base_type = die_type (die, cu);
9723
9724 /* The die_type call above may have already set the type for this DIE. */
9725 cv_type = get_die_type (die, cu);
9726 if (cv_type)
9727 return cv_type;
9728
9729 /* In case the const qualifier is applied to an array type, the element type
9730 is so qualified, not the array type (section 6.7.3 of C99). */
9731 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9732 {
9733 struct type *el_type, *inner_array;
9734
9735 base_type = copy_type (base_type);
9736 inner_array = base_type;
9737
9738 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9739 {
9740 TYPE_TARGET_TYPE (inner_array) =
9741 copy_type (TYPE_TARGET_TYPE (inner_array));
9742 inner_array = TYPE_TARGET_TYPE (inner_array);
9743 }
9744
9745 el_type = TYPE_TARGET_TYPE (inner_array);
9746 TYPE_TARGET_TYPE (inner_array) =
9747 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
9748
9749 return set_die_type (die, base_type, cu);
9750 }
9751
9752 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
9753 return set_die_type (die, cv_type, cu);
9754 }
9755
9756 static struct type *
9757 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
9758 {
9759 struct type *base_type, *cv_type;
9760
9761 base_type = die_type (die, cu);
9762
9763 /* The die_type call above may have already set the type for this DIE. */
9764 cv_type = get_die_type (die, cu);
9765 if (cv_type)
9766 return cv_type;
9767
9768 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
9769 return set_die_type (die, cv_type, cu);
9770 }
9771
9772 /* Extract all information from a DW_TAG_string_type DIE and add to
9773 the user defined type vector. It isn't really a user defined type,
9774 but it behaves like one, with other DIE's using an AT_user_def_type
9775 attribute to reference it. */
9776
9777 static struct type *
9778 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
9779 {
9780 struct objfile *objfile = cu->objfile;
9781 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9782 struct type *type, *range_type, *index_type, *char_type;
9783 struct attribute *attr;
9784 unsigned int length;
9785
9786 attr = dwarf2_attr (die, DW_AT_string_length, cu);
9787 if (attr)
9788 {
9789 length = DW_UNSND (attr);
9790 }
9791 else
9792 {
9793 /* Check for the DW_AT_byte_size attribute. */
9794 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9795 if (attr)
9796 {
9797 length = DW_UNSND (attr);
9798 }
9799 else
9800 {
9801 length = 1;
9802 }
9803 }
9804
9805 index_type = objfile_type (objfile)->builtin_int;
9806 range_type = create_range_type (NULL, index_type, 1, length);
9807 char_type = language_string_char_type (cu->language_defn, gdbarch);
9808 type = create_string_type (NULL, char_type, range_type);
9809
9810 return set_die_type (die, type, cu);
9811 }
9812
9813 /* Handle DIES due to C code like:
9814
9815 struct foo
9816 {
9817 int (*funcp)(int a, long l);
9818 int b;
9819 };
9820
9821 ('funcp' generates a DW_TAG_subroutine_type DIE). */
9822
9823 static struct type *
9824 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
9825 {
9826 struct objfile *objfile = cu->objfile;
9827 struct type *type; /* Type that this function returns. */
9828 struct type *ftype; /* Function that returns above type. */
9829 struct attribute *attr;
9830
9831 type = die_type (die, cu);
9832
9833 /* The die_type call above may have already set the type for this DIE. */
9834 ftype = get_die_type (die, cu);
9835 if (ftype)
9836 return ftype;
9837
9838 ftype = lookup_function_type (type);
9839
9840 /* All functions in C++, Pascal and Java have prototypes. */
9841 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
9842 if ((attr && (DW_UNSND (attr) != 0))
9843 || cu->language == language_cplus
9844 || cu->language == language_java
9845 || cu->language == language_pascal)
9846 TYPE_PROTOTYPED (ftype) = 1;
9847 else if (producer_is_realview (cu->producer))
9848 /* RealView does not emit DW_AT_prototyped. We can not
9849 distinguish prototyped and unprototyped functions; default to
9850 prototyped, since that is more common in modern code (and
9851 RealView warns about unprototyped functions). */
9852 TYPE_PROTOTYPED (ftype) = 1;
9853
9854 /* Store the calling convention in the type if it's available in
9855 the subroutine die. Otherwise set the calling convention to
9856 the default value DW_CC_normal. */
9857 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
9858 if (attr)
9859 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
9860 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
9861 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
9862 else
9863 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
9864
9865 /* We need to add the subroutine type to the die immediately so
9866 we don't infinitely recurse when dealing with parameters
9867 declared as the same subroutine type. */
9868 set_die_type (die, ftype, cu);
9869
9870 if (die->child != NULL)
9871 {
9872 struct type *void_type = objfile_type (objfile)->builtin_void;
9873 struct die_info *child_die;
9874 int nparams, iparams;
9875
9876 /* Count the number of parameters.
9877 FIXME: GDB currently ignores vararg functions, but knows about
9878 vararg member functions. */
9879 nparams = 0;
9880 child_die = die->child;
9881 while (child_die && child_die->tag)
9882 {
9883 if (child_die->tag == DW_TAG_formal_parameter)
9884 nparams++;
9885 else if (child_die->tag == DW_TAG_unspecified_parameters)
9886 TYPE_VARARGS (ftype) = 1;
9887 child_die = sibling_die (child_die);
9888 }
9889
9890 /* Allocate storage for parameters and fill them in. */
9891 TYPE_NFIELDS (ftype) = nparams;
9892 TYPE_FIELDS (ftype) = (struct field *)
9893 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
9894
9895 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
9896 even if we error out during the parameters reading below. */
9897 for (iparams = 0; iparams < nparams; iparams++)
9898 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
9899
9900 iparams = 0;
9901 child_die = die->child;
9902 while (child_die && child_die->tag)
9903 {
9904 if (child_die->tag == DW_TAG_formal_parameter)
9905 {
9906 struct type *arg_type;
9907
9908 /* DWARF version 2 has no clean way to discern C++
9909 static and non-static member functions. G++ helps
9910 GDB by marking the first parameter for non-static
9911 member functions (which is the this pointer) as
9912 artificial. We pass this information to
9913 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
9914
9915 DWARF version 3 added DW_AT_object_pointer, which GCC
9916 4.5 does not yet generate. */
9917 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
9918 if (attr)
9919 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
9920 else
9921 {
9922 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
9923
9924 /* GCC/43521: In java, the formal parameter
9925 "this" is sometimes not marked with DW_AT_artificial. */
9926 if (cu->language == language_java)
9927 {
9928 const char *name = dwarf2_name (child_die, cu);
9929
9930 if (name && !strcmp (name, "this"))
9931 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
9932 }
9933 }
9934 arg_type = die_type (child_die, cu);
9935
9936 /* RealView does not mark THIS as const, which the testsuite
9937 expects. GCC marks THIS as const in method definitions,
9938 but not in the class specifications (GCC PR 43053). */
9939 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
9940 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
9941 {
9942 int is_this = 0;
9943 struct dwarf2_cu *arg_cu = cu;
9944 const char *name = dwarf2_name (child_die, cu);
9945
9946 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
9947 if (attr)
9948 {
9949 /* If the compiler emits this, use it. */
9950 if (follow_die_ref (die, attr, &arg_cu) == child_die)
9951 is_this = 1;
9952 }
9953 else if (name && strcmp (name, "this") == 0)
9954 /* Function definitions will have the argument names. */
9955 is_this = 1;
9956 else if (name == NULL && iparams == 0)
9957 /* Declarations may not have the names, so like
9958 elsewhere in GDB, assume an artificial first
9959 argument is "this". */
9960 is_this = 1;
9961
9962 if (is_this)
9963 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
9964 arg_type, 0);
9965 }
9966
9967 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
9968 iparams++;
9969 }
9970 child_die = sibling_die (child_die);
9971 }
9972 }
9973
9974 return ftype;
9975 }
9976
9977 static struct type *
9978 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
9979 {
9980 struct objfile *objfile = cu->objfile;
9981 const char *name = NULL;
9982 struct type *this_type, *target_type;
9983
9984 name = dwarf2_full_name (NULL, die, cu);
9985 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
9986 TYPE_FLAG_TARGET_STUB, NULL, objfile);
9987 TYPE_NAME (this_type) = (char *) name;
9988 set_die_type (die, this_type, cu);
9989 target_type = die_type (die, cu);
9990 if (target_type != this_type)
9991 TYPE_TARGET_TYPE (this_type) = target_type;
9992 else
9993 {
9994 /* Self-referential typedefs are, it seems, not allowed by the DWARF
9995 spec and cause infinite loops in GDB. */
9996 complaint (&symfile_complaints,
9997 _("Self-referential DW_TAG_typedef "
9998 "- DIE at 0x%x [in module %s]"),
9999 die->offset.sect_off, objfile->name);
10000 TYPE_TARGET_TYPE (this_type) = NULL;
10001 }
10002 return this_type;
10003 }
10004
10005 /* Find a representation of a given base type and install
10006 it in the TYPE field of the die. */
10007
10008 static struct type *
10009 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10010 {
10011 struct objfile *objfile = cu->objfile;
10012 struct type *type;
10013 struct attribute *attr;
10014 int encoding = 0, size = 0;
10015 char *name;
10016 enum type_code code = TYPE_CODE_INT;
10017 int type_flags = 0;
10018 struct type *target_type = NULL;
10019
10020 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10021 if (attr)
10022 {
10023 encoding = DW_UNSND (attr);
10024 }
10025 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10026 if (attr)
10027 {
10028 size = DW_UNSND (attr);
10029 }
10030 name = dwarf2_name (die, cu);
10031 if (!name)
10032 {
10033 complaint (&symfile_complaints,
10034 _("DW_AT_name missing from DW_TAG_base_type"));
10035 }
10036
10037 switch (encoding)
10038 {
10039 case DW_ATE_address:
10040 /* Turn DW_ATE_address into a void * pointer. */
10041 code = TYPE_CODE_PTR;
10042 type_flags |= TYPE_FLAG_UNSIGNED;
10043 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10044 break;
10045 case DW_ATE_boolean:
10046 code = TYPE_CODE_BOOL;
10047 type_flags |= TYPE_FLAG_UNSIGNED;
10048 break;
10049 case DW_ATE_complex_float:
10050 code = TYPE_CODE_COMPLEX;
10051 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10052 break;
10053 case DW_ATE_decimal_float:
10054 code = TYPE_CODE_DECFLOAT;
10055 break;
10056 case DW_ATE_float:
10057 code = TYPE_CODE_FLT;
10058 break;
10059 case DW_ATE_signed:
10060 break;
10061 case DW_ATE_unsigned:
10062 type_flags |= TYPE_FLAG_UNSIGNED;
10063 if (cu->language == language_fortran
10064 && name
10065 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10066 code = TYPE_CODE_CHAR;
10067 break;
10068 case DW_ATE_signed_char:
10069 if (cu->language == language_ada || cu->language == language_m2
10070 || cu->language == language_pascal
10071 || cu->language == language_fortran)
10072 code = TYPE_CODE_CHAR;
10073 break;
10074 case DW_ATE_unsigned_char:
10075 if (cu->language == language_ada || cu->language == language_m2
10076 || cu->language == language_pascal
10077 || cu->language == language_fortran)
10078 code = TYPE_CODE_CHAR;
10079 type_flags |= TYPE_FLAG_UNSIGNED;
10080 break;
10081 case DW_ATE_UTF:
10082 /* We just treat this as an integer and then recognize the
10083 type by name elsewhere. */
10084 break;
10085
10086 default:
10087 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10088 dwarf_type_encoding_name (encoding));
10089 break;
10090 }
10091
10092 type = init_type (code, size, type_flags, NULL, objfile);
10093 TYPE_NAME (type) = name;
10094 TYPE_TARGET_TYPE (type) = target_type;
10095
10096 if (name && strcmp (name, "char") == 0)
10097 TYPE_NOSIGN (type) = 1;
10098
10099 return set_die_type (die, type, cu);
10100 }
10101
10102 /* Read the given DW_AT_subrange DIE. */
10103
10104 static struct type *
10105 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10106 {
10107 struct type *base_type;
10108 struct type *range_type;
10109 struct attribute *attr;
10110 LONGEST low, high;
10111 int low_default_is_valid;
10112 char *name;
10113 LONGEST negative_mask;
10114
10115 base_type = die_type (die, cu);
10116 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10117 check_typedef (base_type);
10118
10119 /* The die_type call above may have already set the type for this DIE. */
10120 range_type = get_die_type (die, cu);
10121 if (range_type)
10122 return range_type;
10123
10124 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10125 omitting DW_AT_lower_bound. */
10126 switch (cu->language)
10127 {
10128 case language_c:
10129 case language_cplus:
10130 low = 0;
10131 low_default_is_valid = 1;
10132 break;
10133 case language_fortran:
10134 low = 1;
10135 low_default_is_valid = 1;
10136 break;
10137 case language_d:
10138 case language_java:
10139 case language_objc:
10140 low = 0;
10141 low_default_is_valid = (cu->header.version >= 4);
10142 break;
10143 case language_ada:
10144 case language_m2:
10145 case language_pascal:
10146 low = 1;
10147 low_default_is_valid = (cu->header.version >= 4);
10148 break;
10149 default:
10150 low = 0;
10151 low_default_is_valid = 0;
10152 break;
10153 }
10154
10155 /* FIXME: For variable sized arrays either of these could be
10156 a variable rather than a constant value. We'll allow it,
10157 but we don't know how to handle it. */
10158 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10159 if (attr)
10160 low = dwarf2_get_attr_constant_value (attr, low);
10161 else if (!low_default_is_valid)
10162 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10163 "- DIE at 0x%x [in module %s]"),
10164 die->offset.sect_off, cu->objfile->name);
10165
10166 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10167 if (attr)
10168 {
10169 if (attr_form_is_block (attr) || is_ref_attr (attr))
10170 {
10171 /* GCC encodes arrays with unspecified or dynamic length
10172 with a DW_FORM_block1 attribute or a reference attribute.
10173 FIXME: GDB does not yet know how to handle dynamic
10174 arrays properly, treat them as arrays with unspecified
10175 length for now.
10176
10177 FIXME: jimb/2003-09-22: GDB does not really know
10178 how to handle arrays of unspecified length
10179 either; we just represent them as zero-length
10180 arrays. Choose an appropriate upper bound given
10181 the lower bound we've computed above. */
10182 high = low - 1;
10183 }
10184 else
10185 high = dwarf2_get_attr_constant_value (attr, 1);
10186 }
10187 else
10188 {
10189 attr = dwarf2_attr (die, DW_AT_count, cu);
10190 if (attr)
10191 {
10192 int count = dwarf2_get_attr_constant_value (attr, 1);
10193 high = low + count - 1;
10194 }
10195 else
10196 {
10197 /* Unspecified array length. */
10198 high = low - 1;
10199 }
10200 }
10201
10202 /* Dwarf-2 specifications explicitly allows to create subrange types
10203 without specifying a base type.
10204 In that case, the base type must be set to the type of
10205 the lower bound, upper bound or count, in that order, if any of these
10206 three attributes references an object that has a type.
10207 If no base type is found, the Dwarf-2 specifications say that
10208 a signed integer type of size equal to the size of an address should
10209 be used.
10210 For the following C code: `extern char gdb_int [];'
10211 GCC produces an empty range DIE.
10212 FIXME: muller/2010-05-28: Possible references to object for low bound,
10213 high bound or count are not yet handled by this code. */
10214 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10215 {
10216 struct objfile *objfile = cu->objfile;
10217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10218 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10219 struct type *int_type = objfile_type (objfile)->builtin_int;
10220
10221 /* Test "int", "long int", and "long long int" objfile types,
10222 and select the first one having a size above or equal to the
10223 architecture address size. */
10224 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10225 base_type = int_type;
10226 else
10227 {
10228 int_type = objfile_type (objfile)->builtin_long;
10229 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10230 base_type = int_type;
10231 else
10232 {
10233 int_type = objfile_type (objfile)->builtin_long_long;
10234 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10235 base_type = int_type;
10236 }
10237 }
10238 }
10239
10240 negative_mask =
10241 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10242 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10243 low |= negative_mask;
10244 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10245 high |= negative_mask;
10246
10247 range_type = create_range_type (NULL, base_type, low, high);
10248
10249 /* Mark arrays with dynamic length at least as an array of unspecified
10250 length. GDB could check the boundary but before it gets implemented at
10251 least allow accessing the array elements. */
10252 if (attr && attr_form_is_block (attr))
10253 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10254
10255 /* Ada expects an empty array on no boundary attributes. */
10256 if (attr == NULL && cu->language != language_ada)
10257 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10258
10259 name = dwarf2_name (die, cu);
10260 if (name)
10261 TYPE_NAME (range_type) = name;
10262
10263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10264 if (attr)
10265 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10266
10267 set_die_type (die, range_type, cu);
10268
10269 /* set_die_type should be already done. */
10270 set_descriptive_type (range_type, die, cu);
10271
10272 return range_type;
10273 }
10274
10275 static struct type *
10276 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10277 {
10278 struct type *type;
10279
10280 /* For now, we only support the C meaning of an unspecified type: void. */
10281
10282 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10283 TYPE_NAME (type) = dwarf2_name (die, cu);
10284
10285 return set_die_type (die, type, cu);
10286 }
10287
10288 /* Read a single die and all its descendents. Set the die's sibling
10289 field to NULL; set other fields in the die correctly, and set all
10290 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10291 location of the info_ptr after reading all of those dies. PARENT
10292 is the parent of the die in question. */
10293
10294 static struct die_info *
10295 read_die_and_children (const struct die_reader_specs *reader,
10296 gdb_byte *info_ptr,
10297 gdb_byte **new_info_ptr,
10298 struct die_info *parent)
10299 {
10300 struct die_info *die;
10301 gdb_byte *cur_ptr;
10302 int has_children;
10303
10304 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10305 if (die == NULL)
10306 {
10307 *new_info_ptr = cur_ptr;
10308 return NULL;
10309 }
10310 store_in_ref_table (die, reader->cu);
10311
10312 if (has_children)
10313 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10314 else
10315 {
10316 die->child = NULL;
10317 *new_info_ptr = cur_ptr;
10318 }
10319
10320 die->sibling = NULL;
10321 die->parent = parent;
10322 return die;
10323 }
10324
10325 /* Read a die, all of its descendents, and all of its siblings; set
10326 all of the fields of all of the dies correctly. Arguments are as
10327 in read_die_and_children. */
10328
10329 static struct die_info *
10330 read_die_and_siblings (const struct die_reader_specs *reader,
10331 gdb_byte *info_ptr,
10332 gdb_byte **new_info_ptr,
10333 struct die_info *parent)
10334 {
10335 struct die_info *first_die, *last_sibling;
10336 gdb_byte *cur_ptr;
10337
10338 cur_ptr = info_ptr;
10339 first_die = last_sibling = NULL;
10340
10341 while (1)
10342 {
10343 struct die_info *die
10344 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10345
10346 if (die == NULL)
10347 {
10348 *new_info_ptr = cur_ptr;
10349 return first_die;
10350 }
10351
10352 if (!first_die)
10353 first_die = die;
10354 else
10355 last_sibling->sibling = die;
10356
10357 last_sibling = die;
10358 }
10359 }
10360
10361 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10362 attributes.
10363 The caller is responsible for filling in the extra attributes
10364 and updating (*DIEP)->num_attrs.
10365 Set DIEP to point to a newly allocated die with its information,
10366 except for its child, sibling, and parent fields.
10367 Set HAS_CHILDREN to tell whether the die has children or not. */
10368
10369 static gdb_byte *
10370 read_full_die_1 (const struct die_reader_specs *reader,
10371 struct die_info **diep, gdb_byte *info_ptr,
10372 int *has_children, int num_extra_attrs)
10373 {
10374 unsigned int abbrev_number, bytes_read, i;
10375 sect_offset offset;
10376 struct abbrev_info *abbrev;
10377 struct die_info *die;
10378 struct dwarf2_cu *cu = reader->cu;
10379 bfd *abfd = reader->abfd;
10380
10381 offset.sect_off = info_ptr - reader->buffer;
10382 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10383 info_ptr += bytes_read;
10384 if (!abbrev_number)
10385 {
10386 *diep = NULL;
10387 *has_children = 0;
10388 return info_ptr;
10389 }
10390
10391 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10392 if (!abbrev)
10393 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10394 abbrev_number,
10395 bfd_get_filename (abfd));
10396
10397 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10398 die->offset = offset;
10399 die->tag = abbrev->tag;
10400 die->abbrev = abbrev_number;
10401
10402 /* Make the result usable.
10403 The caller needs to update num_attrs after adding the extra
10404 attributes. */
10405 die->num_attrs = abbrev->num_attrs;
10406
10407 for (i = 0; i < abbrev->num_attrs; ++i)
10408 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10409 info_ptr);
10410
10411 *diep = die;
10412 *has_children = abbrev->has_children;
10413 return info_ptr;
10414 }
10415
10416 /* Read a die and all its attributes.
10417 Set DIEP to point to a newly allocated die with its information,
10418 except for its child, sibling, and parent fields.
10419 Set HAS_CHILDREN to tell whether the die has children or not. */
10420
10421 static gdb_byte *
10422 read_full_die (const struct die_reader_specs *reader,
10423 struct die_info **diep, gdb_byte *info_ptr,
10424 int *has_children)
10425 {
10426 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10427 }
10428
10429 /* In DWARF version 2, the description of the debugging information is
10430 stored in a separate .debug_abbrev section. Before we read any
10431 dies from a section we read in all abbreviations and install them
10432 in a hash table. This function also sets flags in CU describing
10433 the data found in the abbrev table. */
10434
10435 static void
10436 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10437 struct dwarf2_section_info *abbrev_section)
10438
10439 {
10440 bfd *abfd = abbrev_section->asection->owner;
10441 struct comp_unit_head *cu_header = &cu->header;
10442 gdb_byte *abbrev_ptr;
10443 struct abbrev_info *cur_abbrev;
10444 unsigned int abbrev_number, bytes_read, abbrev_name;
10445 unsigned int abbrev_form, hash_number;
10446 struct attr_abbrev *cur_attrs;
10447 unsigned int allocated_attrs;
10448
10449 /* Initialize dwarf2 abbrevs. */
10450 obstack_init (&cu->abbrev_obstack);
10451 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10452 (ABBREV_HASH_SIZE
10453 * sizeof (struct abbrev_info *)));
10454 memset (cu->dwarf2_abbrevs, 0,
10455 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10456
10457 dwarf2_read_section (cu->objfile, abbrev_section);
10458 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10459 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10460 abbrev_ptr += bytes_read;
10461
10462 allocated_attrs = ATTR_ALLOC_CHUNK;
10463 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10464
10465 /* Loop until we reach an abbrev number of 0. */
10466 while (abbrev_number)
10467 {
10468 cur_abbrev = dwarf_alloc_abbrev (cu);
10469
10470 /* read in abbrev header */
10471 cur_abbrev->number = abbrev_number;
10472 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10473 abbrev_ptr += bytes_read;
10474 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10475 abbrev_ptr += 1;
10476
10477 /* now read in declarations */
10478 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10479 abbrev_ptr += bytes_read;
10480 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10481 abbrev_ptr += bytes_read;
10482 while (abbrev_name)
10483 {
10484 if (cur_abbrev->num_attrs == allocated_attrs)
10485 {
10486 allocated_attrs += ATTR_ALLOC_CHUNK;
10487 cur_attrs
10488 = xrealloc (cur_attrs, (allocated_attrs
10489 * sizeof (struct attr_abbrev)));
10490 }
10491
10492 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10493 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10494 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10495 abbrev_ptr += bytes_read;
10496 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10497 abbrev_ptr += bytes_read;
10498 }
10499
10500 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10501 (cur_abbrev->num_attrs
10502 * sizeof (struct attr_abbrev)));
10503 memcpy (cur_abbrev->attrs, cur_attrs,
10504 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10505
10506 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10507 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10508 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10509
10510 /* Get next abbreviation.
10511 Under Irix6 the abbreviations for a compilation unit are not
10512 always properly terminated with an abbrev number of 0.
10513 Exit loop if we encounter an abbreviation which we have
10514 already read (which means we are about to read the abbreviations
10515 for the next compile unit) or if the end of the abbreviation
10516 table is reached. */
10517 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10518 >= abbrev_section->size)
10519 break;
10520 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10521 abbrev_ptr += bytes_read;
10522 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10523 break;
10524 }
10525
10526 xfree (cur_attrs);
10527 }
10528
10529 /* Release the memory used by the abbrev table for a compilation unit. */
10530
10531 static void
10532 dwarf2_free_abbrev_table (void *ptr_to_cu)
10533 {
10534 struct dwarf2_cu *cu = ptr_to_cu;
10535
10536 obstack_free (&cu->abbrev_obstack, NULL);
10537 cu->dwarf2_abbrevs = NULL;
10538 }
10539
10540 /* Lookup an abbrev_info structure in the abbrev hash table. */
10541
10542 static struct abbrev_info *
10543 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10544 {
10545 unsigned int hash_number;
10546 struct abbrev_info *abbrev;
10547
10548 hash_number = number % ABBREV_HASH_SIZE;
10549 abbrev = cu->dwarf2_abbrevs[hash_number];
10550
10551 while (abbrev)
10552 {
10553 if (abbrev->number == number)
10554 return abbrev;
10555 else
10556 abbrev = abbrev->next;
10557 }
10558 return NULL;
10559 }
10560
10561 /* Returns nonzero if TAG represents a type that we might generate a partial
10562 symbol for. */
10563
10564 static int
10565 is_type_tag_for_partial (int tag)
10566 {
10567 switch (tag)
10568 {
10569 #if 0
10570 /* Some types that would be reasonable to generate partial symbols for,
10571 that we don't at present. */
10572 case DW_TAG_array_type:
10573 case DW_TAG_file_type:
10574 case DW_TAG_ptr_to_member_type:
10575 case DW_TAG_set_type:
10576 case DW_TAG_string_type:
10577 case DW_TAG_subroutine_type:
10578 #endif
10579 case DW_TAG_base_type:
10580 case DW_TAG_class_type:
10581 case DW_TAG_interface_type:
10582 case DW_TAG_enumeration_type:
10583 case DW_TAG_structure_type:
10584 case DW_TAG_subrange_type:
10585 case DW_TAG_typedef:
10586 case DW_TAG_union_type:
10587 return 1;
10588 default:
10589 return 0;
10590 }
10591 }
10592
10593 /* Load all DIEs that are interesting for partial symbols into memory. */
10594
10595 static struct partial_die_info *
10596 load_partial_dies (const struct die_reader_specs *reader,
10597 gdb_byte *info_ptr, int building_psymtab)
10598 {
10599 struct dwarf2_cu *cu = reader->cu;
10600 struct objfile *objfile = cu->objfile;
10601 struct partial_die_info *part_die;
10602 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10603 struct abbrev_info *abbrev;
10604 unsigned int bytes_read;
10605 unsigned int load_all = 0;
10606 int nesting_level = 1;
10607
10608 parent_die = NULL;
10609 last_die = NULL;
10610
10611 gdb_assert (cu->per_cu != NULL);
10612 if (cu->per_cu->load_all_dies)
10613 load_all = 1;
10614
10615 cu->partial_dies
10616 = htab_create_alloc_ex (cu->header.length / 12,
10617 partial_die_hash,
10618 partial_die_eq,
10619 NULL,
10620 &cu->comp_unit_obstack,
10621 hashtab_obstack_allocate,
10622 dummy_obstack_deallocate);
10623
10624 part_die = obstack_alloc (&cu->comp_unit_obstack,
10625 sizeof (struct partial_die_info));
10626
10627 while (1)
10628 {
10629 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10630
10631 /* A NULL abbrev means the end of a series of children. */
10632 if (abbrev == NULL)
10633 {
10634 if (--nesting_level == 0)
10635 {
10636 /* PART_DIE was probably the last thing allocated on the
10637 comp_unit_obstack, so we could call obstack_free
10638 here. We don't do that because the waste is small,
10639 and will be cleaned up when we're done with this
10640 compilation unit. This way, we're also more robust
10641 against other users of the comp_unit_obstack. */
10642 return first_die;
10643 }
10644 info_ptr += bytes_read;
10645 last_die = parent_die;
10646 parent_die = parent_die->die_parent;
10647 continue;
10648 }
10649
10650 /* Check for template arguments. We never save these; if
10651 they're seen, we just mark the parent, and go on our way. */
10652 if (parent_die != NULL
10653 && cu->language == language_cplus
10654 && (abbrev->tag == DW_TAG_template_type_param
10655 || abbrev->tag == DW_TAG_template_value_param))
10656 {
10657 parent_die->has_template_arguments = 1;
10658
10659 if (!load_all)
10660 {
10661 /* We don't need a partial DIE for the template argument. */
10662 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10663 continue;
10664 }
10665 }
10666
10667 /* We only recurse into c++ subprograms looking for template arguments.
10668 Skip their other children. */
10669 if (!load_all
10670 && cu->language == language_cplus
10671 && parent_die != NULL
10672 && parent_die->tag == DW_TAG_subprogram)
10673 {
10674 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10675 continue;
10676 }
10677
10678 /* Check whether this DIE is interesting enough to save. Normally
10679 we would not be interested in members here, but there may be
10680 later variables referencing them via DW_AT_specification (for
10681 static members). */
10682 if (!load_all
10683 && !is_type_tag_for_partial (abbrev->tag)
10684 && abbrev->tag != DW_TAG_constant
10685 && abbrev->tag != DW_TAG_enumerator
10686 && abbrev->tag != DW_TAG_subprogram
10687 && abbrev->tag != DW_TAG_lexical_block
10688 && abbrev->tag != DW_TAG_variable
10689 && abbrev->tag != DW_TAG_namespace
10690 && abbrev->tag != DW_TAG_module
10691 && abbrev->tag != DW_TAG_member)
10692 {
10693 /* Otherwise we skip to the next sibling, if any. */
10694 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10695 continue;
10696 }
10697
10698 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10699 info_ptr);
10700
10701 /* This two-pass algorithm for processing partial symbols has a
10702 high cost in cache pressure. Thus, handle some simple cases
10703 here which cover the majority of C partial symbols. DIEs
10704 which neither have specification tags in them, nor could have
10705 specification tags elsewhere pointing at them, can simply be
10706 processed and discarded.
10707
10708 This segment is also optional; scan_partial_symbols and
10709 add_partial_symbol will handle these DIEs if we chain
10710 them in normally. When compilers which do not emit large
10711 quantities of duplicate debug information are more common,
10712 this code can probably be removed. */
10713
10714 /* Any complete simple types at the top level (pretty much all
10715 of them, for a language without namespaces), can be processed
10716 directly. */
10717 if (parent_die == NULL
10718 && part_die->has_specification == 0
10719 && part_die->is_declaration == 0
10720 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
10721 || part_die->tag == DW_TAG_base_type
10722 || part_die->tag == DW_TAG_subrange_type))
10723 {
10724 if (building_psymtab && part_die->name != NULL)
10725 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10726 VAR_DOMAIN, LOC_TYPEDEF,
10727 &objfile->static_psymbols,
10728 0, (CORE_ADDR) 0, cu->language, objfile);
10729 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10730 continue;
10731 }
10732
10733 /* The exception for DW_TAG_typedef with has_children above is
10734 a workaround of GCC PR debug/47510. In the case of this complaint
10735 type_name_no_tag_or_error will error on such types later.
10736
10737 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10738 it could not find the child DIEs referenced later, this is checked
10739 above. In correct DWARF DW_TAG_typedef should have no children. */
10740
10741 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10742 complaint (&symfile_complaints,
10743 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10744 "- DIE at 0x%x [in module %s]"),
10745 part_die->offset.sect_off, objfile->name);
10746
10747 /* If we're at the second level, and we're an enumerator, and
10748 our parent has no specification (meaning possibly lives in a
10749 namespace elsewhere), then we can add the partial symbol now
10750 instead of queueing it. */
10751 if (part_die->tag == DW_TAG_enumerator
10752 && parent_die != NULL
10753 && parent_die->die_parent == NULL
10754 && parent_die->tag == DW_TAG_enumeration_type
10755 && parent_die->has_specification == 0)
10756 {
10757 if (part_die->name == NULL)
10758 complaint (&symfile_complaints,
10759 _("malformed enumerator DIE ignored"));
10760 else if (building_psymtab)
10761 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10762 VAR_DOMAIN, LOC_CONST,
10763 (cu->language == language_cplus
10764 || cu->language == language_java)
10765 ? &objfile->global_psymbols
10766 : &objfile->static_psymbols,
10767 0, (CORE_ADDR) 0, cu->language, objfile);
10768
10769 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10770 continue;
10771 }
10772
10773 /* We'll save this DIE so link it in. */
10774 part_die->die_parent = parent_die;
10775 part_die->die_sibling = NULL;
10776 part_die->die_child = NULL;
10777
10778 if (last_die && last_die == parent_die)
10779 last_die->die_child = part_die;
10780 else if (last_die)
10781 last_die->die_sibling = part_die;
10782
10783 last_die = part_die;
10784
10785 if (first_die == NULL)
10786 first_die = part_die;
10787
10788 /* Maybe add the DIE to the hash table. Not all DIEs that we
10789 find interesting need to be in the hash table, because we
10790 also have the parent/sibling/child chains; only those that we
10791 might refer to by offset later during partial symbol reading.
10792
10793 For now this means things that might have be the target of a
10794 DW_AT_specification, DW_AT_abstract_origin, or
10795 DW_AT_extension. DW_AT_extension will refer only to
10796 namespaces; DW_AT_abstract_origin refers to functions (and
10797 many things under the function DIE, but we do not recurse
10798 into function DIEs during partial symbol reading) and
10799 possibly variables as well; DW_AT_specification refers to
10800 declarations. Declarations ought to have the DW_AT_declaration
10801 flag. It happens that GCC forgets to put it in sometimes, but
10802 only for functions, not for types.
10803
10804 Adding more things than necessary to the hash table is harmless
10805 except for the performance cost. Adding too few will result in
10806 wasted time in find_partial_die, when we reread the compilation
10807 unit with load_all_dies set. */
10808
10809 if (load_all
10810 || abbrev->tag == DW_TAG_constant
10811 || abbrev->tag == DW_TAG_subprogram
10812 || abbrev->tag == DW_TAG_variable
10813 || abbrev->tag == DW_TAG_namespace
10814 || part_die->is_declaration)
10815 {
10816 void **slot;
10817
10818 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
10819 part_die->offset.sect_off, INSERT);
10820 *slot = part_die;
10821 }
10822
10823 part_die = obstack_alloc (&cu->comp_unit_obstack,
10824 sizeof (struct partial_die_info));
10825
10826 /* For some DIEs we want to follow their children (if any). For C
10827 we have no reason to follow the children of structures; for other
10828 languages we have to, so that we can get at method physnames
10829 to infer fully qualified class names, for DW_AT_specification,
10830 and for C++ template arguments. For C++, we also look one level
10831 inside functions to find template arguments (if the name of the
10832 function does not already contain the template arguments).
10833
10834 For Ada, we need to scan the children of subprograms and lexical
10835 blocks as well because Ada allows the definition of nested
10836 entities that could be interesting for the debugger, such as
10837 nested subprograms for instance. */
10838 if (last_die->has_children
10839 && (load_all
10840 || last_die->tag == DW_TAG_namespace
10841 || last_die->tag == DW_TAG_module
10842 || last_die->tag == DW_TAG_enumeration_type
10843 || (cu->language == language_cplus
10844 && last_die->tag == DW_TAG_subprogram
10845 && (last_die->name == NULL
10846 || strchr (last_die->name, '<') == NULL))
10847 || (cu->language != language_c
10848 && (last_die->tag == DW_TAG_class_type
10849 || last_die->tag == DW_TAG_interface_type
10850 || last_die->tag == DW_TAG_structure_type
10851 || last_die->tag == DW_TAG_union_type))
10852 || (cu->language == language_ada
10853 && (last_die->tag == DW_TAG_subprogram
10854 || last_die->tag == DW_TAG_lexical_block))))
10855 {
10856 nesting_level++;
10857 parent_die = last_die;
10858 continue;
10859 }
10860
10861 /* Otherwise we skip to the next sibling, if any. */
10862 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
10863
10864 /* Back to the top, do it again. */
10865 }
10866 }
10867
10868 /* Read a minimal amount of information into the minimal die structure. */
10869
10870 static gdb_byte *
10871 read_partial_die (const struct die_reader_specs *reader,
10872 struct partial_die_info *part_die,
10873 struct abbrev_info *abbrev, unsigned int abbrev_len,
10874 gdb_byte *info_ptr)
10875 {
10876 struct dwarf2_cu *cu = reader->cu;
10877 struct objfile *objfile = cu->objfile;
10878 gdb_byte *buffer = reader->buffer;
10879 unsigned int i;
10880 struct attribute attr;
10881 int has_low_pc_attr = 0;
10882 int has_high_pc_attr = 0;
10883 int high_pc_relative = 0;
10884
10885 memset (part_die, 0, sizeof (struct partial_die_info));
10886
10887 part_die->offset.sect_off = info_ptr - buffer;
10888
10889 info_ptr += abbrev_len;
10890
10891 if (abbrev == NULL)
10892 return info_ptr;
10893
10894 part_die->tag = abbrev->tag;
10895 part_die->has_children = abbrev->has_children;
10896
10897 for (i = 0; i < abbrev->num_attrs; ++i)
10898 {
10899 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
10900
10901 /* Store the data if it is of an attribute we want to keep in a
10902 partial symbol table. */
10903 switch (attr.name)
10904 {
10905 case DW_AT_name:
10906 switch (part_die->tag)
10907 {
10908 case DW_TAG_compile_unit:
10909 case DW_TAG_type_unit:
10910 /* Compilation units have a DW_AT_name that is a filename, not
10911 a source language identifier. */
10912 case DW_TAG_enumeration_type:
10913 case DW_TAG_enumerator:
10914 /* These tags always have simple identifiers already; no need
10915 to canonicalize them. */
10916 part_die->name = DW_STRING (&attr);
10917 break;
10918 default:
10919 part_die->name
10920 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
10921 &objfile->objfile_obstack);
10922 break;
10923 }
10924 break;
10925 case DW_AT_linkage_name:
10926 case DW_AT_MIPS_linkage_name:
10927 /* Note that both forms of linkage name might appear. We
10928 assume they will be the same, and we only store the last
10929 one we see. */
10930 if (cu->language == language_ada)
10931 part_die->name = DW_STRING (&attr);
10932 part_die->linkage_name = DW_STRING (&attr);
10933 break;
10934 case DW_AT_low_pc:
10935 has_low_pc_attr = 1;
10936 part_die->lowpc = DW_ADDR (&attr);
10937 break;
10938 case DW_AT_high_pc:
10939 has_high_pc_attr = 1;
10940 if (attr.form == DW_FORM_addr
10941 || attr.form == DW_FORM_GNU_addr_index)
10942 part_die->highpc = DW_ADDR (&attr);
10943 else
10944 {
10945 high_pc_relative = 1;
10946 part_die->highpc = DW_UNSND (&attr);
10947 }
10948 break;
10949 case DW_AT_location:
10950 /* Support the .debug_loc offsets. */
10951 if (attr_form_is_block (&attr))
10952 {
10953 part_die->locdesc = DW_BLOCK (&attr);
10954 }
10955 else if (attr_form_is_section_offset (&attr))
10956 {
10957 dwarf2_complex_location_expr_complaint ();
10958 }
10959 else
10960 {
10961 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10962 "partial symbol information");
10963 }
10964 break;
10965 case DW_AT_external:
10966 part_die->is_external = DW_UNSND (&attr);
10967 break;
10968 case DW_AT_declaration:
10969 part_die->is_declaration = DW_UNSND (&attr);
10970 break;
10971 case DW_AT_type:
10972 part_die->has_type = 1;
10973 break;
10974 case DW_AT_abstract_origin:
10975 case DW_AT_specification:
10976 case DW_AT_extension:
10977 part_die->has_specification = 1;
10978 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
10979 break;
10980 case DW_AT_sibling:
10981 /* Ignore absolute siblings, they might point outside of
10982 the current compile unit. */
10983 if (attr.form == DW_FORM_ref_addr)
10984 complaint (&symfile_complaints,
10985 _("ignoring absolute DW_AT_sibling"));
10986 else
10987 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
10988 break;
10989 case DW_AT_byte_size:
10990 part_die->has_byte_size = 1;
10991 break;
10992 case DW_AT_calling_convention:
10993 /* DWARF doesn't provide a way to identify a program's source-level
10994 entry point. DW_AT_calling_convention attributes are only meant
10995 to describe functions' calling conventions.
10996
10997 However, because it's a necessary piece of information in
10998 Fortran, and because DW_CC_program is the only piece of debugging
10999 information whose definition refers to a 'main program' at all,
11000 several compilers have begun marking Fortran main programs with
11001 DW_CC_program --- even when those functions use the standard
11002 calling conventions.
11003
11004 So until DWARF specifies a way to provide this information and
11005 compilers pick up the new representation, we'll support this
11006 practice. */
11007 if (DW_UNSND (&attr) == DW_CC_program
11008 && cu->language == language_fortran)
11009 {
11010 set_main_name (part_die->name);
11011
11012 /* As this DIE has a static linkage the name would be difficult
11013 to look up later. */
11014 language_of_main = language_fortran;
11015 }
11016 break;
11017 case DW_AT_inline:
11018 if (DW_UNSND (&attr) == DW_INL_inlined
11019 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11020 part_die->may_be_inlined = 1;
11021 break;
11022 default:
11023 break;
11024 }
11025 }
11026
11027 if (high_pc_relative)
11028 part_die->highpc += part_die->lowpc;
11029
11030 if (has_low_pc_attr && has_high_pc_attr)
11031 {
11032 /* When using the GNU linker, .gnu.linkonce. sections are used to
11033 eliminate duplicate copies of functions and vtables and such.
11034 The linker will arbitrarily choose one and discard the others.
11035 The AT_*_pc values for such functions refer to local labels in
11036 these sections. If the section from that file was discarded, the
11037 labels are not in the output, so the relocs get a value of 0.
11038 If this is a discarded function, mark the pc bounds as invalid,
11039 so that GDB will ignore it. */
11040 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11041 {
11042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11043
11044 complaint (&symfile_complaints,
11045 _("DW_AT_low_pc %s is zero "
11046 "for DIE at 0x%x [in module %s]"),
11047 paddress (gdbarch, part_die->lowpc),
11048 part_die->offset.sect_off, objfile->name);
11049 }
11050 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11051 else if (part_die->lowpc >= part_die->highpc)
11052 {
11053 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11054
11055 complaint (&symfile_complaints,
11056 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11057 "for DIE at 0x%x [in module %s]"),
11058 paddress (gdbarch, part_die->lowpc),
11059 paddress (gdbarch, part_die->highpc),
11060 part_die->offset.sect_off, objfile->name);
11061 }
11062 else
11063 part_die->has_pc_info = 1;
11064 }
11065
11066 return info_ptr;
11067 }
11068
11069 /* Find a cached partial DIE at OFFSET in CU. */
11070
11071 static struct partial_die_info *
11072 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11073 {
11074 struct partial_die_info *lookup_die = NULL;
11075 struct partial_die_info part_die;
11076
11077 part_die.offset = offset;
11078 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11079 offset.sect_off);
11080
11081 return lookup_die;
11082 }
11083
11084 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11085 except in the case of .debug_types DIEs which do not reference
11086 outside their CU (they do however referencing other types via
11087 DW_FORM_ref_sig8). */
11088
11089 static struct partial_die_info *
11090 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11091 {
11092 struct objfile *objfile = cu->objfile;
11093 struct dwarf2_per_cu_data *per_cu = NULL;
11094 struct partial_die_info *pd = NULL;
11095
11096 if (offset_in_cu_p (&cu->header, offset))
11097 {
11098 pd = find_partial_die_in_comp_unit (offset, cu);
11099 if (pd != NULL)
11100 return pd;
11101 /* We missed recording what we needed.
11102 Load all dies and try again. */
11103 per_cu = cu->per_cu;
11104 }
11105 else
11106 {
11107 /* TUs don't reference other CUs/TUs (except via type signatures). */
11108 if (cu->per_cu->is_debug_types)
11109 {
11110 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11111 " external reference to offset 0x%lx [in module %s].\n"),
11112 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11113 bfd_get_filename (objfile->obfd));
11114 }
11115 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11116
11117 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11118 load_partial_comp_unit (per_cu);
11119
11120 per_cu->cu->last_used = 0;
11121 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11122 }
11123
11124 /* If we didn't find it, and not all dies have been loaded,
11125 load them all and try again. */
11126
11127 if (pd == NULL && per_cu->load_all_dies == 0)
11128 {
11129 per_cu->load_all_dies = 1;
11130
11131 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11132 THIS_CU->cu may already be in use. So we can't just free it and
11133 replace its DIEs with the ones we read in. Instead, we leave those
11134 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11135 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11136 set. */
11137 load_partial_comp_unit (per_cu);
11138
11139 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11140 }
11141
11142 if (pd == NULL)
11143 internal_error (__FILE__, __LINE__,
11144 _("could not find partial DIE 0x%x "
11145 "in cache [from module %s]\n"),
11146 offset.sect_off, bfd_get_filename (objfile->obfd));
11147 return pd;
11148 }
11149
11150 /* See if we can figure out if the class lives in a namespace. We do
11151 this by looking for a member function; its demangled name will
11152 contain namespace info, if there is any. */
11153
11154 static void
11155 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11156 struct dwarf2_cu *cu)
11157 {
11158 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11159 what template types look like, because the demangler
11160 frequently doesn't give the same name as the debug info. We
11161 could fix this by only using the demangled name to get the
11162 prefix (but see comment in read_structure_type). */
11163
11164 struct partial_die_info *real_pdi;
11165 struct partial_die_info *child_pdi;
11166
11167 /* If this DIE (this DIE's specification, if any) has a parent, then
11168 we should not do this. We'll prepend the parent's fully qualified
11169 name when we create the partial symbol. */
11170
11171 real_pdi = struct_pdi;
11172 while (real_pdi->has_specification)
11173 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11174
11175 if (real_pdi->die_parent != NULL)
11176 return;
11177
11178 for (child_pdi = struct_pdi->die_child;
11179 child_pdi != NULL;
11180 child_pdi = child_pdi->die_sibling)
11181 {
11182 if (child_pdi->tag == DW_TAG_subprogram
11183 && child_pdi->linkage_name != NULL)
11184 {
11185 char *actual_class_name
11186 = language_class_name_from_physname (cu->language_defn,
11187 child_pdi->linkage_name);
11188 if (actual_class_name != NULL)
11189 {
11190 struct_pdi->name
11191 = obsavestring (actual_class_name,
11192 strlen (actual_class_name),
11193 &cu->objfile->objfile_obstack);
11194 xfree (actual_class_name);
11195 }
11196 break;
11197 }
11198 }
11199 }
11200
11201 /* Adjust PART_DIE before generating a symbol for it. This function
11202 may set the is_external flag or change the DIE's name. */
11203
11204 static void
11205 fixup_partial_die (struct partial_die_info *part_die,
11206 struct dwarf2_cu *cu)
11207 {
11208 /* Once we've fixed up a die, there's no point in doing so again.
11209 This also avoids a memory leak if we were to call
11210 guess_partial_die_structure_name multiple times. */
11211 if (part_die->fixup_called)
11212 return;
11213
11214 /* If we found a reference attribute and the DIE has no name, try
11215 to find a name in the referred to DIE. */
11216
11217 if (part_die->name == NULL && part_die->has_specification)
11218 {
11219 struct partial_die_info *spec_die;
11220
11221 spec_die = find_partial_die (part_die->spec_offset, cu);
11222
11223 fixup_partial_die (spec_die, cu);
11224
11225 if (spec_die->name)
11226 {
11227 part_die->name = spec_die->name;
11228
11229 /* Copy DW_AT_external attribute if it is set. */
11230 if (spec_die->is_external)
11231 part_die->is_external = spec_die->is_external;
11232 }
11233 }
11234
11235 /* Set default names for some unnamed DIEs. */
11236
11237 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11238 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11239
11240 /* If there is no parent die to provide a namespace, and there are
11241 children, see if we can determine the namespace from their linkage
11242 name. */
11243 if (cu->language == language_cplus
11244 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11245 && part_die->die_parent == NULL
11246 && part_die->has_children
11247 && (part_die->tag == DW_TAG_class_type
11248 || part_die->tag == DW_TAG_structure_type
11249 || part_die->tag == DW_TAG_union_type))
11250 guess_partial_die_structure_name (part_die, cu);
11251
11252 /* GCC might emit a nameless struct or union that has a linkage
11253 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11254 if (part_die->name == NULL
11255 && (part_die->tag == DW_TAG_class_type
11256 || part_die->tag == DW_TAG_interface_type
11257 || part_die->tag == DW_TAG_structure_type
11258 || part_die->tag == DW_TAG_union_type)
11259 && part_die->linkage_name != NULL)
11260 {
11261 char *demangled;
11262
11263 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11264 if (demangled)
11265 {
11266 const char *base;
11267
11268 /* Strip any leading namespaces/classes, keep only the base name.
11269 DW_AT_name for named DIEs does not contain the prefixes. */
11270 base = strrchr (demangled, ':');
11271 if (base && base > demangled && base[-1] == ':')
11272 base++;
11273 else
11274 base = demangled;
11275
11276 part_die->name = obsavestring (base, strlen (base),
11277 &cu->objfile->objfile_obstack);
11278 xfree (demangled);
11279 }
11280 }
11281
11282 part_die->fixup_called = 1;
11283 }
11284
11285 /* Read an attribute value described by an attribute form. */
11286
11287 static gdb_byte *
11288 read_attribute_value (const struct die_reader_specs *reader,
11289 struct attribute *attr, unsigned form,
11290 gdb_byte *info_ptr)
11291 {
11292 struct dwarf2_cu *cu = reader->cu;
11293 bfd *abfd = reader->abfd;
11294 struct comp_unit_head *cu_header = &cu->header;
11295 unsigned int bytes_read;
11296 struct dwarf_block *blk;
11297
11298 attr->form = form;
11299 switch (form)
11300 {
11301 case DW_FORM_ref_addr:
11302 if (cu->header.version == 2)
11303 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11304 else
11305 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11306 &cu->header, &bytes_read);
11307 info_ptr += bytes_read;
11308 break;
11309 case DW_FORM_addr:
11310 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11311 info_ptr += bytes_read;
11312 break;
11313 case DW_FORM_block2:
11314 blk = dwarf_alloc_block (cu);
11315 blk->size = read_2_bytes (abfd, info_ptr);
11316 info_ptr += 2;
11317 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11318 info_ptr += blk->size;
11319 DW_BLOCK (attr) = blk;
11320 break;
11321 case DW_FORM_block4:
11322 blk = dwarf_alloc_block (cu);
11323 blk->size = read_4_bytes (abfd, info_ptr);
11324 info_ptr += 4;
11325 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11326 info_ptr += blk->size;
11327 DW_BLOCK (attr) = blk;
11328 break;
11329 case DW_FORM_data2:
11330 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11331 info_ptr += 2;
11332 break;
11333 case DW_FORM_data4:
11334 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11335 info_ptr += 4;
11336 break;
11337 case DW_FORM_data8:
11338 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11339 info_ptr += 8;
11340 break;
11341 case DW_FORM_sec_offset:
11342 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11343 info_ptr += bytes_read;
11344 break;
11345 case DW_FORM_string:
11346 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11347 DW_STRING_IS_CANONICAL (attr) = 0;
11348 info_ptr += bytes_read;
11349 break;
11350 case DW_FORM_strp:
11351 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11352 &bytes_read);
11353 DW_STRING_IS_CANONICAL (attr) = 0;
11354 info_ptr += bytes_read;
11355 break;
11356 case DW_FORM_exprloc:
11357 case DW_FORM_block:
11358 blk = dwarf_alloc_block (cu);
11359 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11360 info_ptr += bytes_read;
11361 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11362 info_ptr += blk->size;
11363 DW_BLOCK (attr) = blk;
11364 break;
11365 case DW_FORM_block1:
11366 blk = dwarf_alloc_block (cu);
11367 blk->size = read_1_byte (abfd, info_ptr);
11368 info_ptr += 1;
11369 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11370 info_ptr += blk->size;
11371 DW_BLOCK (attr) = blk;
11372 break;
11373 case DW_FORM_data1:
11374 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11375 info_ptr += 1;
11376 break;
11377 case DW_FORM_flag:
11378 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11379 info_ptr += 1;
11380 break;
11381 case DW_FORM_flag_present:
11382 DW_UNSND (attr) = 1;
11383 break;
11384 case DW_FORM_sdata:
11385 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11386 info_ptr += bytes_read;
11387 break;
11388 case DW_FORM_udata:
11389 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11390 info_ptr += bytes_read;
11391 break;
11392 case DW_FORM_ref1:
11393 DW_UNSND (attr) = (cu->header.offset.sect_off
11394 + read_1_byte (abfd, info_ptr));
11395 info_ptr += 1;
11396 break;
11397 case DW_FORM_ref2:
11398 DW_UNSND (attr) = (cu->header.offset.sect_off
11399 + read_2_bytes (abfd, info_ptr));
11400 info_ptr += 2;
11401 break;
11402 case DW_FORM_ref4:
11403 DW_UNSND (attr) = (cu->header.offset.sect_off
11404 + read_4_bytes (abfd, info_ptr));
11405 info_ptr += 4;
11406 break;
11407 case DW_FORM_ref8:
11408 DW_UNSND (attr) = (cu->header.offset.sect_off
11409 + read_8_bytes (abfd, info_ptr));
11410 info_ptr += 8;
11411 break;
11412 case DW_FORM_ref_sig8:
11413 /* Convert the signature to something we can record in DW_UNSND
11414 for later lookup.
11415 NOTE: This is NULL if the type wasn't found. */
11416 DW_SIGNATURED_TYPE (attr) =
11417 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11418 info_ptr += 8;
11419 break;
11420 case DW_FORM_ref_udata:
11421 DW_UNSND (attr) = (cu->header.offset.sect_off
11422 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11423 info_ptr += bytes_read;
11424 break;
11425 case DW_FORM_indirect:
11426 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11427 info_ptr += bytes_read;
11428 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11429 break;
11430 case DW_FORM_GNU_addr_index:
11431 if (reader->dwo_file == NULL)
11432 {
11433 /* For now flag a hard error.
11434 Later we can turn this into a complaint. */
11435 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11436 dwarf_form_name (form),
11437 bfd_get_filename (abfd));
11438 }
11439 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11440 info_ptr += bytes_read;
11441 break;
11442 case DW_FORM_GNU_str_index:
11443 if (reader->dwo_file == NULL)
11444 {
11445 /* For now flag a hard error.
11446 Later we can turn this into a complaint if warranted. */
11447 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11448 dwarf_form_name (form),
11449 bfd_get_filename (abfd));
11450 }
11451 {
11452 ULONGEST str_index =
11453 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11454
11455 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11456 DW_STRING_IS_CANONICAL (attr) = 0;
11457 info_ptr += bytes_read;
11458 }
11459 break;
11460 default:
11461 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11462 dwarf_form_name (form),
11463 bfd_get_filename (abfd));
11464 }
11465
11466 /* We have seen instances where the compiler tried to emit a byte
11467 size attribute of -1 which ended up being encoded as an unsigned
11468 0xffffffff. Although 0xffffffff is technically a valid size value,
11469 an object of this size seems pretty unlikely so we can relatively
11470 safely treat these cases as if the size attribute was invalid and
11471 treat them as zero by default. */
11472 if (attr->name == DW_AT_byte_size
11473 && form == DW_FORM_data4
11474 && DW_UNSND (attr) >= 0xffffffff)
11475 {
11476 complaint
11477 (&symfile_complaints,
11478 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11479 hex_string (DW_UNSND (attr)));
11480 DW_UNSND (attr) = 0;
11481 }
11482
11483 return info_ptr;
11484 }
11485
11486 /* Read an attribute described by an abbreviated attribute. */
11487
11488 static gdb_byte *
11489 read_attribute (const struct die_reader_specs *reader,
11490 struct attribute *attr, struct attr_abbrev *abbrev,
11491 gdb_byte *info_ptr)
11492 {
11493 attr->name = abbrev->name;
11494 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11495 }
11496
11497 /* Read dwarf information from a buffer. */
11498
11499 static unsigned int
11500 read_1_byte (bfd *abfd, gdb_byte *buf)
11501 {
11502 return bfd_get_8 (abfd, buf);
11503 }
11504
11505 static int
11506 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11507 {
11508 return bfd_get_signed_8 (abfd, buf);
11509 }
11510
11511 static unsigned int
11512 read_2_bytes (bfd *abfd, gdb_byte *buf)
11513 {
11514 return bfd_get_16 (abfd, buf);
11515 }
11516
11517 static int
11518 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11519 {
11520 return bfd_get_signed_16 (abfd, buf);
11521 }
11522
11523 static unsigned int
11524 read_4_bytes (bfd *abfd, gdb_byte *buf)
11525 {
11526 return bfd_get_32 (abfd, buf);
11527 }
11528
11529 static int
11530 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11531 {
11532 return bfd_get_signed_32 (abfd, buf);
11533 }
11534
11535 static ULONGEST
11536 read_8_bytes (bfd *abfd, gdb_byte *buf)
11537 {
11538 return bfd_get_64 (abfd, buf);
11539 }
11540
11541 static CORE_ADDR
11542 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11543 unsigned int *bytes_read)
11544 {
11545 struct comp_unit_head *cu_header = &cu->header;
11546 CORE_ADDR retval = 0;
11547
11548 if (cu_header->signed_addr_p)
11549 {
11550 switch (cu_header->addr_size)
11551 {
11552 case 2:
11553 retval = bfd_get_signed_16 (abfd, buf);
11554 break;
11555 case 4:
11556 retval = bfd_get_signed_32 (abfd, buf);
11557 break;
11558 case 8:
11559 retval = bfd_get_signed_64 (abfd, buf);
11560 break;
11561 default:
11562 internal_error (__FILE__, __LINE__,
11563 _("read_address: bad switch, signed [in module %s]"),
11564 bfd_get_filename (abfd));
11565 }
11566 }
11567 else
11568 {
11569 switch (cu_header->addr_size)
11570 {
11571 case 2:
11572 retval = bfd_get_16 (abfd, buf);
11573 break;
11574 case 4:
11575 retval = bfd_get_32 (abfd, buf);
11576 break;
11577 case 8:
11578 retval = bfd_get_64 (abfd, buf);
11579 break;
11580 default:
11581 internal_error (__FILE__, __LINE__,
11582 _("read_address: bad switch, "
11583 "unsigned [in module %s]"),
11584 bfd_get_filename (abfd));
11585 }
11586 }
11587
11588 *bytes_read = cu_header->addr_size;
11589 return retval;
11590 }
11591
11592 /* Read the initial length from a section. The (draft) DWARF 3
11593 specification allows the initial length to take up either 4 bytes
11594 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11595 bytes describe the length and all offsets will be 8 bytes in length
11596 instead of 4.
11597
11598 An older, non-standard 64-bit format is also handled by this
11599 function. The older format in question stores the initial length
11600 as an 8-byte quantity without an escape value. Lengths greater
11601 than 2^32 aren't very common which means that the initial 4 bytes
11602 is almost always zero. Since a length value of zero doesn't make
11603 sense for the 32-bit format, this initial zero can be considered to
11604 be an escape value which indicates the presence of the older 64-bit
11605 format. As written, the code can't detect (old format) lengths
11606 greater than 4GB. If it becomes necessary to handle lengths
11607 somewhat larger than 4GB, we could allow other small values (such
11608 as the non-sensical values of 1, 2, and 3) to also be used as
11609 escape values indicating the presence of the old format.
11610
11611 The value returned via bytes_read should be used to increment the
11612 relevant pointer after calling read_initial_length().
11613
11614 [ Note: read_initial_length() and read_offset() are based on the
11615 document entitled "DWARF Debugging Information Format", revision
11616 3, draft 8, dated November 19, 2001. This document was obtained
11617 from:
11618
11619 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11620
11621 This document is only a draft and is subject to change. (So beware.)
11622
11623 Details regarding the older, non-standard 64-bit format were
11624 determined empirically by examining 64-bit ELF files produced by
11625 the SGI toolchain on an IRIX 6.5 machine.
11626
11627 - Kevin, July 16, 2002
11628 ] */
11629
11630 static LONGEST
11631 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11632 {
11633 LONGEST length = bfd_get_32 (abfd, buf);
11634
11635 if (length == 0xffffffff)
11636 {
11637 length = bfd_get_64 (abfd, buf + 4);
11638 *bytes_read = 12;
11639 }
11640 else if (length == 0)
11641 {
11642 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11643 length = bfd_get_64 (abfd, buf);
11644 *bytes_read = 8;
11645 }
11646 else
11647 {
11648 *bytes_read = 4;
11649 }
11650
11651 return length;
11652 }
11653
11654 /* Cover function for read_initial_length.
11655 Returns the length of the object at BUF, and stores the size of the
11656 initial length in *BYTES_READ and stores the size that offsets will be in
11657 *OFFSET_SIZE.
11658 If the initial length size is not equivalent to that specified in
11659 CU_HEADER then issue a complaint.
11660 This is useful when reading non-comp-unit headers. */
11661
11662 static LONGEST
11663 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11664 const struct comp_unit_head *cu_header,
11665 unsigned int *bytes_read,
11666 unsigned int *offset_size)
11667 {
11668 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11669
11670 gdb_assert (cu_header->initial_length_size == 4
11671 || cu_header->initial_length_size == 8
11672 || cu_header->initial_length_size == 12);
11673
11674 if (cu_header->initial_length_size != *bytes_read)
11675 complaint (&symfile_complaints,
11676 _("intermixed 32-bit and 64-bit DWARF sections"));
11677
11678 *offset_size = (*bytes_read == 4) ? 4 : 8;
11679 return length;
11680 }
11681
11682 /* Read an offset from the data stream. The size of the offset is
11683 given by cu_header->offset_size. */
11684
11685 static LONGEST
11686 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11687 unsigned int *bytes_read)
11688 {
11689 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11690
11691 *bytes_read = cu_header->offset_size;
11692 return offset;
11693 }
11694
11695 /* Read an offset from the data stream. */
11696
11697 static LONGEST
11698 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11699 {
11700 LONGEST retval = 0;
11701
11702 switch (offset_size)
11703 {
11704 case 4:
11705 retval = bfd_get_32 (abfd, buf);
11706 break;
11707 case 8:
11708 retval = bfd_get_64 (abfd, buf);
11709 break;
11710 default:
11711 internal_error (__FILE__, __LINE__,
11712 _("read_offset_1: bad switch [in module %s]"),
11713 bfd_get_filename (abfd));
11714 }
11715
11716 return retval;
11717 }
11718
11719 static gdb_byte *
11720 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
11721 {
11722 /* If the size of a host char is 8 bits, we can return a pointer
11723 to the buffer, otherwise we have to copy the data to a buffer
11724 allocated on the temporary obstack. */
11725 gdb_assert (HOST_CHAR_BIT == 8);
11726 return buf;
11727 }
11728
11729 static char *
11730 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11731 {
11732 /* If the size of a host char is 8 bits, we can return a pointer
11733 to the string, otherwise we have to copy the string to a buffer
11734 allocated on the temporary obstack. */
11735 gdb_assert (HOST_CHAR_BIT == 8);
11736 if (*buf == '\0')
11737 {
11738 *bytes_read_ptr = 1;
11739 return NULL;
11740 }
11741 *bytes_read_ptr = strlen ((char *) buf) + 1;
11742 return (char *) buf;
11743 }
11744
11745 static char *
11746 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
11747 {
11748 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
11749 if (dwarf2_per_objfile->str.buffer == NULL)
11750 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
11751 bfd_get_filename (abfd));
11752 if (str_offset >= dwarf2_per_objfile->str.size)
11753 error (_("DW_FORM_strp pointing outside of "
11754 ".debug_str section [in module %s]"),
11755 bfd_get_filename (abfd));
11756 gdb_assert (HOST_CHAR_BIT == 8);
11757 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
11758 return NULL;
11759 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
11760 }
11761
11762 static char *
11763 read_indirect_string (bfd *abfd, gdb_byte *buf,
11764 const struct comp_unit_head *cu_header,
11765 unsigned int *bytes_read_ptr)
11766 {
11767 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
11768
11769 return read_indirect_string_at_offset (abfd, str_offset);
11770 }
11771
11772 static ULONGEST
11773 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11774 {
11775 ULONGEST result;
11776 unsigned int num_read;
11777 int i, shift;
11778 unsigned char byte;
11779
11780 result = 0;
11781 shift = 0;
11782 num_read = 0;
11783 i = 0;
11784 while (1)
11785 {
11786 byte = bfd_get_8 (abfd, buf);
11787 buf++;
11788 num_read++;
11789 result |= ((ULONGEST) (byte & 127) << shift);
11790 if ((byte & 128) == 0)
11791 {
11792 break;
11793 }
11794 shift += 7;
11795 }
11796 *bytes_read_ptr = num_read;
11797 return result;
11798 }
11799
11800 static LONGEST
11801 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11802 {
11803 LONGEST result;
11804 int i, shift, num_read;
11805 unsigned char byte;
11806
11807 result = 0;
11808 shift = 0;
11809 num_read = 0;
11810 i = 0;
11811 while (1)
11812 {
11813 byte = bfd_get_8 (abfd, buf);
11814 buf++;
11815 num_read++;
11816 result |= ((LONGEST) (byte & 127) << shift);
11817 shift += 7;
11818 if ((byte & 128) == 0)
11819 {
11820 break;
11821 }
11822 }
11823 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
11824 result |= -(((LONGEST) 1) << shift);
11825 *bytes_read_ptr = num_read;
11826 return result;
11827 }
11828
11829 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
11830 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
11831 ADDR_SIZE is the size of addresses from the CU header. */
11832
11833 static CORE_ADDR
11834 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
11835 {
11836 struct objfile *objfile = dwarf2_per_objfile->objfile;
11837 bfd *abfd = objfile->obfd;
11838 const gdb_byte *info_ptr;
11839
11840 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
11841 if (dwarf2_per_objfile->addr.buffer == NULL)
11842 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
11843 objfile->name);
11844 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
11845 error (_("DW_FORM_addr_index pointing outside of "
11846 ".debug_addr section [in module %s]"),
11847 objfile->name);
11848 info_ptr = (dwarf2_per_objfile->addr.buffer
11849 + addr_base + addr_index * addr_size);
11850 if (addr_size == 4)
11851 return bfd_get_32 (abfd, info_ptr);
11852 else
11853 return bfd_get_64 (abfd, info_ptr);
11854 }
11855
11856 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
11857
11858 static CORE_ADDR
11859 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
11860 {
11861 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
11862 }
11863
11864 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
11865
11866 static CORE_ADDR
11867 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
11868 unsigned int *bytes_read)
11869 {
11870 bfd *abfd = cu->objfile->obfd;
11871 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
11872
11873 return read_addr_index (cu, addr_index);
11874 }
11875
11876 /* Data structure to pass results from dwarf2_read_addr_index_reader
11877 back to dwarf2_read_addr_index. */
11878
11879 struct dwarf2_read_addr_index_data
11880 {
11881 ULONGEST addr_base;
11882 int addr_size;
11883 };
11884
11885 /* die_reader_func for dwarf2_read_addr_index. */
11886
11887 static void
11888 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
11889 gdb_byte *info_ptr,
11890 struct die_info *comp_unit_die,
11891 int has_children,
11892 void *data)
11893 {
11894 struct dwarf2_cu *cu = reader->cu;
11895 struct dwarf2_read_addr_index_data *aidata =
11896 (struct dwarf2_read_addr_index_data *) data;
11897
11898 aidata->addr_base = cu->addr_base;
11899 aidata->addr_size = cu->header.addr_size;
11900 }
11901
11902 /* Given an index in .debug_addr, fetch the value.
11903 NOTE: This can be called during dwarf expression evaluation,
11904 long after the debug information has been read, and thus per_cu->cu
11905 may no longer exist. */
11906
11907 CORE_ADDR
11908 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
11909 unsigned int addr_index)
11910 {
11911 struct objfile *objfile = per_cu->objfile;
11912 struct dwarf2_cu *cu = per_cu->cu;
11913 ULONGEST addr_base;
11914 int addr_size;
11915
11916 /* This is intended to be called from outside this file. */
11917 dw2_setup (objfile);
11918
11919 /* We need addr_base and addr_size.
11920 If we don't have PER_CU->cu, we have to get it.
11921 Nasty, but the alternative is storing the needed info in PER_CU,
11922 which at this point doesn't seem justified: it's not clear how frequently
11923 it would get used and it would increase the size of every PER_CU.
11924 Entry points like dwarf2_per_cu_addr_size do a similar thing
11925 so we're not in uncharted territory here.
11926 Alas we need to be a bit more complicated as addr_base is contained
11927 in the DIE.
11928
11929 We don't need to read the entire CU(/TU).
11930 We just need the header and top level die.
11931 IWBN to use the aging mechanism to let us lazily later discard the CU.
11932 See however init_cutu_and_read_dies_simple. */
11933
11934 if (cu != NULL)
11935 {
11936 addr_base = cu->addr_base;
11937 addr_size = cu->header.addr_size;
11938 }
11939 else
11940 {
11941 struct dwarf2_read_addr_index_data aidata;
11942
11943 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
11944 &aidata);
11945 addr_base = aidata.addr_base;
11946 addr_size = aidata.addr_size;
11947 }
11948
11949 return read_addr_index_1 (addr_index, addr_base, addr_size);
11950 }
11951
11952 /* Given a DW_AT_str_index, fetch the string. */
11953
11954 static char *
11955 read_str_index (const struct die_reader_specs *reader,
11956 struct dwarf2_cu *cu, ULONGEST str_index)
11957 {
11958 struct objfile *objfile = dwarf2_per_objfile->objfile;
11959 const char *dwo_name = objfile->name;
11960 bfd *abfd = objfile->obfd;
11961 struct dwo_sections *sections = &reader->dwo_file->sections;
11962 gdb_byte *info_ptr;
11963 ULONGEST str_offset;
11964
11965 dwarf2_read_section (objfile, &sections->str);
11966 dwarf2_read_section (objfile, &sections->str_offsets);
11967 if (sections->str.buffer == NULL)
11968 error (_("DW_FORM_str_index used without .debug_str.dwo section"
11969 " in CU at offset 0x%lx [in module %s]"),
11970 (long) cu->header.offset.sect_off, dwo_name);
11971 if (sections->str_offsets.buffer == NULL)
11972 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
11973 " in CU at offset 0x%lx [in module %s]"),
11974 (long) cu->header.offset.sect_off, dwo_name);
11975 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
11976 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
11977 " section in CU at offset 0x%lx [in module %s]"),
11978 (long) cu->header.offset.sect_off, dwo_name);
11979 info_ptr = (sections->str_offsets.buffer
11980 + str_index * cu->header.offset_size);
11981 if (cu->header.offset_size == 4)
11982 str_offset = bfd_get_32 (abfd, info_ptr);
11983 else
11984 str_offset = bfd_get_64 (abfd, info_ptr);
11985 if (str_offset >= sections->str.size)
11986 error (_("Offset from DW_FORM_str_index pointing outside of"
11987 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
11988 (long) cu->header.offset.sect_off, dwo_name);
11989 return (char *) (sections->str.buffer + str_offset);
11990 }
11991
11992 /* Return a pointer to just past the end of an LEB128 number in BUF. */
11993
11994 static gdb_byte *
11995 skip_leb128 (bfd *abfd, gdb_byte *buf)
11996 {
11997 int byte;
11998
11999 while (1)
12000 {
12001 byte = bfd_get_8 (abfd, buf);
12002 buf++;
12003 if ((byte & 128) == 0)
12004 return buf;
12005 }
12006 }
12007
12008 /* Return the length of an LEB128 number in BUF. */
12009
12010 static int
12011 leb128_size (const gdb_byte *buf)
12012 {
12013 const gdb_byte *begin = buf;
12014 gdb_byte byte;
12015
12016 while (1)
12017 {
12018 byte = *buf++;
12019 if ((byte & 128) == 0)
12020 return buf - begin;
12021 }
12022 }
12023
12024 static void
12025 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12026 {
12027 switch (lang)
12028 {
12029 case DW_LANG_C89:
12030 case DW_LANG_C99:
12031 case DW_LANG_C:
12032 cu->language = language_c;
12033 break;
12034 case DW_LANG_C_plus_plus:
12035 cu->language = language_cplus;
12036 break;
12037 case DW_LANG_D:
12038 cu->language = language_d;
12039 break;
12040 case DW_LANG_Fortran77:
12041 case DW_LANG_Fortran90:
12042 case DW_LANG_Fortran95:
12043 cu->language = language_fortran;
12044 break;
12045 case DW_LANG_Go:
12046 cu->language = language_go;
12047 break;
12048 case DW_LANG_Mips_Assembler:
12049 cu->language = language_asm;
12050 break;
12051 case DW_LANG_Java:
12052 cu->language = language_java;
12053 break;
12054 case DW_LANG_Ada83:
12055 case DW_LANG_Ada95:
12056 cu->language = language_ada;
12057 break;
12058 case DW_LANG_Modula2:
12059 cu->language = language_m2;
12060 break;
12061 case DW_LANG_Pascal83:
12062 cu->language = language_pascal;
12063 break;
12064 case DW_LANG_ObjC:
12065 cu->language = language_objc;
12066 break;
12067 case DW_LANG_Cobol74:
12068 case DW_LANG_Cobol85:
12069 default:
12070 cu->language = language_minimal;
12071 break;
12072 }
12073 cu->language_defn = language_def (cu->language);
12074 }
12075
12076 /* Return the named attribute or NULL if not there. */
12077
12078 static struct attribute *
12079 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12080 {
12081 for (;;)
12082 {
12083 unsigned int i;
12084 struct attribute *spec = NULL;
12085
12086 for (i = 0; i < die->num_attrs; ++i)
12087 {
12088 if (die->attrs[i].name == name)
12089 return &die->attrs[i];
12090 if (die->attrs[i].name == DW_AT_specification
12091 || die->attrs[i].name == DW_AT_abstract_origin)
12092 spec = &die->attrs[i];
12093 }
12094
12095 if (!spec)
12096 break;
12097
12098 die = follow_die_ref (die, spec, &cu);
12099 }
12100
12101 return NULL;
12102 }
12103
12104 /* Return the named attribute or NULL if not there,
12105 but do not follow DW_AT_specification, etc.
12106 This is for use in contexts where we're reading .debug_types dies.
12107 Following DW_AT_specification, DW_AT_abstract_origin will take us
12108 back up the chain, and we want to go down. */
12109
12110 static struct attribute *
12111 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12112 struct dwarf2_cu *cu)
12113 {
12114 unsigned int i;
12115
12116 for (i = 0; i < die->num_attrs; ++i)
12117 if (die->attrs[i].name == name)
12118 return &die->attrs[i];
12119
12120 return NULL;
12121 }
12122
12123 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12124 and holds a non-zero value. This function should only be used for
12125 DW_FORM_flag or DW_FORM_flag_present attributes. */
12126
12127 static int
12128 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12129 {
12130 struct attribute *attr = dwarf2_attr (die, name, cu);
12131
12132 return (attr && DW_UNSND (attr));
12133 }
12134
12135 static int
12136 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12137 {
12138 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12139 which value is non-zero. However, we have to be careful with
12140 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12141 (via dwarf2_flag_true_p) follows this attribute. So we may
12142 end up accidently finding a declaration attribute that belongs
12143 to a different DIE referenced by the specification attribute,
12144 even though the given DIE does not have a declaration attribute. */
12145 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12146 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12147 }
12148
12149 /* Return the die giving the specification for DIE, if there is
12150 one. *SPEC_CU is the CU containing DIE on input, and the CU
12151 containing the return value on output. If there is no
12152 specification, but there is an abstract origin, that is
12153 returned. */
12154
12155 static struct die_info *
12156 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12157 {
12158 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12159 *spec_cu);
12160
12161 if (spec_attr == NULL)
12162 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12163
12164 if (spec_attr == NULL)
12165 return NULL;
12166 else
12167 return follow_die_ref (die, spec_attr, spec_cu);
12168 }
12169
12170 /* Free the line_header structure *LH, and any arrays and strings it
12171 refers to.
12172 NOTE: This is also used as a "cleanup" function. */
12173
12174 static void
12175 free_line_header (struct line_header *lh)
12176 {
12177 if (lh->standard_opcode_lengths)
12178 xfree (lh->standard_opcode_lengths);
12179
12180 /* Remember that all the lh->file_names[i].name pointers are
12181 pointers into debug_line_buffer, and don't need to be freed. */
12182 if (lh->file_names)
12183 xfree (lh->file_names);
12184
12185 /* Similarly for the include directory names. */
12186 if (lh->include_dirs)
12187 xfree (lh->include_dirs);
12188
12189 xfree (lh);
12190 }
12191
12192 /* Add an entry to LH's include directory table. */
12193
12194 static void
12195 add_include_dir (struct line_header *lh, char *include_dir)
12196 {
12197 /* Grow the array if necessary. */
12198 if (lh->include_dirs_size == 0)
12199 {
12200 lh->include_dirs_size = 1; /* for testing */
12201 lh->include_dirs = xmalloc (lh->include_dirs_size
12202 * sizeof (*lh->include_dirs));
12203 }
12204 else if (lh->num_include_dirs >= lh->include_dirs_size)
12205 {
12206 lh->include_dirs_size *= 2;
12207 lh->include_dirs = xrealloc (lh->include_dirs,
12208 (lh->include_dirs_size
12209 * sizeof (*lh->include_dirs)));
12210 }
12211
12212 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12213 }
12214
12215 /* Add an entry to LH's file name table. */
12216
12217 static void
12218 add_file_name (struct line_header *lh,
12219 char *name,
12220 unsigned int dir_index,
12221 unsigned int mod_time,
12222 unsigned int length)
12223 {
12224 struct file_entry *fe;
12225
12226 /* Grow the array if necessary. */
12227 if (lh->file_names_size == 0)
12228 {
12229 lh->file_names_size = 1; /* for testing */
12230 lh->file_names = xmalloc (lh->file_names_size
12231 * sizeof (*lh->file_names));
12232 }
12233 else if (lh->num_file_names >= lh->file_names_size)
12234 {
12235 lh->file_names_size *= 2;
12236 lh->file_names = xrealloc (lh->file_names,
12237 (lh->file_names_size
12238 * sizeof (*lh->file_names)));
12239 }
12240
12241 fe = &lh->file_names[lh->num_file_names++];
12242 fe->name = name;
12243 fe->dir_index = dir_index;
12244 fe->mod_time = mod_time;
12245 fe->length = length;
12246 fe->included_p = 0;
12247 fe->symtab = NULL;
12248 }
12249
12250 /* Read the statement program header starting at OFFSET in
12251 .debug_line, or .debug_line.dwo. Return a pointer
12252 to a struct line_header, allocated using xmalloc.
12253
12254 NOTE: the strings in the include directory and file name tables of
12255 the returned object point into the dwarf line section buffer,
12256 and must not be freed. */
12257
12258 static struct line_header *
12259 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12260 {
12261 struct cleanup *back_to;
12262 struct line_header *lh;
12263 gdb_byte *line_ptr;
12264 unsigned int bytes_read, offset_size;
12265 int i;
12266 char *cur_dir, *cur_file;
12267 struct dwarf2_section_info *section;
12268 bfd *abfd;
12269
12270 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12271 DWO file. */
12272 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12273 section = &cu->dwo_unit->dwo_file->sections.line;
12274 else
12275 section = &dwarf2_per_objfile->line;
12276
12277 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12278 if (section->buffer == NULL)
12279 {
12280 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12281 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12282 else
12283 complaint (&symfile_complaints, _("missing .debug_line section"));
12284 return 0;
12285 }
12286
12287 /* We can't do this until we know the section is non-empty.
12288 Only then do we know we have such a section. */
12289 abfd = section->asection->owner;
12290
12291 /* Make sure that at least there's room for the total_length field.
12292 That could be 12 bytes long, but we're just going to fudge that. */
12293 if (offset + 4 >= section->size)
12294 {
12295 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12296 return 0;
12297 }
12298
12299 lh = xmalloc (sizeof (*lh));
12300 memset (lh, 0, sizeof (*lh));
12301 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12302 (void *) lh);
12303
12304 line_ptr = section->buffer + offset;
12305
12306 /* Read in the header. */
12307 lh->total_length =
12308 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12309 &bytes_read, &offset_size);
12310 line_ptr += bytes_read;
12311 if (line_ptr + lh->total_length > (section->buffer + section->size))
12312 {
12313 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12314 return 0;
12315 }
12316 lh->statement_program_end = line_ptr + lh->total_length;
12317 lh->version = read_2_bytes (abfd, line_ptr);
12318 line_ptr += 2;
12319 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12320 line_ptr += offset_size;
12321 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12322 line_ptr += 1;
12323 if (lh->version >= 4)
12324 {
12325 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12326 line_ptr += 1;
12327 }
12328 else
12329 lh->maximum_ops_per_instruction = 1;
12330
12331 if (lh->maximum_ops_per_instruction == 0)
12332 {
12333 lh->maximum_ops_per_instruction = 1;
12334 complaint (&symfile_complaints,
12335 _("invalid maximum_ops_per_instruction "
12336 "in `.debug_line' section"));
12337 }
12338
12339 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12340 line_ptr += 1;
12341 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12342 line_ptr += 1;
12343 lh->line_range = read_1_byte (abfd, line_ptr);
12344 line_ptr += 1;
12345 lh->opcode_base = read_1_byte (abfd, line_ptr);
12346 line_ptr += 1;
12347 lh->standard_opcode_lengths
12348 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12349
12350 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12351 for (i = 1; i < lh->opcode_base; ++i)
12352 {
12353 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12354 line_ptr += 1;
12355 }
12356
12357 /* Read directory table. */
12358 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12359 {
12360 line_ptr += bytes_read;
12361 add_include_dir (lh, cur_dir);
12362 }
12363 line_ptr += bytes_read;
12364
12365 /* Read file name table. */
12366 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12367 {
12368 unsigned int dir_index, mod_time, length;
12369
12370 line_ptr += bytes_read;
12371 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12372 line_ptr += bytes_read;
12373 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12374 line_ptr += bytes_read;
12375 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12376 line_ptr += bytes_read;
12377
12378 add_file_name (lh, cur_file, dir_index, mod_time, length);
12379 }
12380 line_ptr += bytes_read;
12381 lh->statement_program_start = line_ptr;
12382
12383 if (line_ptr > (section->buffer + section->size))
12384 complaint (&symfile_complaints,
12385 _("line number info header doesn't "
12386 "fit in `.debug_line' section"));
12387
12388 discard_cleanups (back_to);
12389 return lh;
12390 }
12391
12392 /* Subroutine of dwarf_decode_lines to simplify it.
12393 Return the file name of the psymtab for included file FILE_INDEX
12394 in line header LH of PST.
12395 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12396 If space for the result is malloc'd, it will be freed by a cleanup.
12397 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12398
12399 static char *
12400 psymtab_include_file_name (const struct line_header *lh, int file_index,
12401 const struct partial_symtab *pst,
12402 const char *comp_dir)
12403 {
12404 const struct file_entry fe = lh->file_names [file_index];
12405 char *include_name = fe.name;
12406 char *include_name_to_compare = include_name;
12407 char *dir_name = NULL;
12408 const char *pst_filename;
12409 char *copied_name = NULL;
12410 int file_is_pst;
12411
12412 if (fe.dir_index)
12413 dir_name = lh->include_dirs[fe.dir_index - 1];
12414
12415 if (!IS_ABSOLUTE_PATH (include_name)
12416 && (dir_name != NULL || comp_dir != NULL))
12417 {
12418 /* Avoid creating a duplicate psymtab for PST.
12419 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12420 Before we do the comparison, however, we need to account
12421 for DIR_NAME and COMP_DIR.
12422 First prepend dir_name (if non-NULL). If we still don't
12423 have an absolute path prepend comp_dir (if non-NULL).
12424 However, the directory we record in the include-file's
12425 psymtab does not contain COMP_DIR (to match the
12426 corresponding symtab(s)).
12427
12428 Example:
12429
12430 bash$ cd /tmp
12431 bash$ gcc -g ./hello.c
12432 include_name = "hello.c"
12433 dir_name = "."
12434 DW_AT_comp_dir = comp_dir = "/tmp"
12435 DW_AT_name = "./hello.c" */
12436
12437 if (dir_name != NULL)
12438 {
12439 include_name = concat (dir_name, SLASH_STRING,
12440 include_name, (char *)NULL);
12441 include_name_to_compare = include_name;
12442 make_cleanup (xfree, include_name);
12443 }
12444 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12445 {
12446 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12447 include_name, (char *)NULL);
12448 }
12449 }
12450
12451 pst_filename = pst->filename;
12452 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12453 {
12454 copied_name = concat (pst->dirname, SLASH_STRING,
12455 pst_filename, (char *)NULL);
12456 pst_filename = copied_name;
12457 }
12458
12459 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12460
12461 if (include_name_to_compare != include_name)
12462 xfree (include_name_to_compare);
12463 if (copied_name != NULL)
12464 xfree (copied_name);
12465
12466 if (file_is_pst)
12467 return NULL;
12468 return include_name;
12469 }
12470
12471 /* Ignore this record_line request. */
12472
12473 static void
12474 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12475 {
12476 return;
12477 }
12478
12479 /* Subroutine of dwarf_decode_lines to simplify it.
12480 Process the line number information in LH. */
12481
12482 static void
12483 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12484 struct dwarf2_cu *cu, struct partial_symtab *pst)
12485 {
12486 gdb_byte *line_ptr, *extended_end;
12487 gdb_byte *line_end;
12488 unsigned int bytes_read, extended_len;
12489 unsigned char op_code, extended_op, adj_opcode;
12490 CORE_ADDR baseaddr;
12491 struct objfile *objfile = cu->objfile;
12492 bfd *abfd = objfile->obfd;
12493 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12494 const int decode_for_pst_p = (pst != NULL);
12495 struct subfile *last_subfile = NULL;
12496 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12497 = record_line;
12498
12499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12500
12501 line_ptr = lh->statement_program_start;
12502 line_end = lh->statement_program_end;
12503
12504 /* Read the statement sequences until there's nothing left. */
12505 while (line_ptr < line_end)
12506 {
12507 /* state machine registers */
12508 CORE_ADDR address = 0;
12509 unsigned int file = 1;
12510 unsigned int line = 1;
12511 unsigned int column = 0;
12512 int is_stmt = lh->default_is_stmt;
12513 int basic_block = 0;
12514 int end_sequence = 0;
12515 CORE_ADDR addr;
12516 unsigned char op_index = 0;
12517
12518 if (!decode_for_pst_p && lh->num_file_names >= file)
12519 {
12520 /* Start a subfile for the current file of the state machine. */
12521 /* lh->include_dirs and lh->file_names are 0-based, but the
12522 directory and file name numbers in the statement program
12523 are 1-based. */
12524 struct file_entry *fe = &lh->file_names[file - 1];
12525 char *dir = NULL;
12526
12527 if (fe->dir_index)
12528 dir = lh->include_dirs[fe->dir_index - 1];
12529
12530 dwarf2_start_subfile (fe->name, dir, comp_dir);
12531 }
12532
12533 /* Decode the table. */
12534 while (!end_sequence)
12535 {
12536 op_code = read_1_byte (abfd, line_ptr);
12537 line_ptr += 1;
12538 if (line_ptr > line_end)
12539 {
12540 dwarf2_debug_line_missing_end_sequence_complaint ();
12541 break;
12542 }
12543
12544 if (op_code >= lh->opcode_base)
12545 {
12546 /* Special operand. */
12547 adj_opcode = op_code - lh->opcode_base;
12548 address += (((op_index + (adj_opcode / lh->line_range))
12549 / lh->maximum_ops_per_instruction)
12550 * lh->minimum_instruction_length);
12551 op_index = ((op_index + (adj_opcode / lh->line_range))
12552 % lh->maximum_ops_per_instruction);
12553 line += lh->line_base + (adj_opcode % lh->line_range);
12554 if (lh->num_file_names < file || file == 0)
12555 dwarf2_debug_line_missing_file_complaint ();
12556 /* For now we ignore lines not starting on an
12557 instruction boundary. */
12558 else if (op_index == 0)
12559 {
12560 lh->file_names[file - 1].included_p = 1;
12561 if (!decode_for_pst_p && is_stmt)
12562 {
12563 if (last_subfile != current_subfile)
12564 {
12565 addr = gdbarch_addr_bits_remove (gdbarch, address);
12566 if (last_subfile)
12567 (*p_record_line) (last_subfile, 0, addr);
12568 last_subfile = current_subfile;
12569 }
12570 /* Append row to matrix using current values. */
12571 addr = gdbarch_addr_bits_remove (gdbarch, address);
12572 (*p_record_line) (current_subfile, line, addr);
12573 }
12574 }
12575 basic_block = 0;
12576 }
12577 else switch (op_code)
12578 {
12579 case DW_LNS_extended_op:
12580 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12581 &bytes_read);
12582 line_ptr += bytes_read;
12583 extended_end = line_ptr + extended_len;
12584 extended_op = read_1_byte (abfd, line_ptr);
12585 line_ptr += 1;
12586 switch (extended_op)
12587 {
12588 case DW_LNE_end_sequence:
12589 p_record_line = record_line;
12590 end_sequence = 1;
12591 break;
12592 case DW_LNE_set_address:
12593 address = read_address (abfd, line_ptr, cu, &bytes_read);
12594
12595 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12596 {
12597 /* This line table is for a function which has been
12598 GCd by the linker. Ignore it. PR gdb/12528 */
12599
12600 long line_offset
12601 = line_ptr - dwarf2_per_objfile->line.buffer;
12602
12603 complaint (&symfile_complaints,
12604 _(".debug_line address at offset 0x%lx is 0 "
12605 "[in module %s]"),
12606 line_offset, objfile->name);
12607 p_record_line = noop_record_line;
12608 }
12609
12610 op_index = 0;
12611 line_ptr += bytes_read;
12612 address += baseaddr;
12613 break;
12614 case DW_LNE_define_file:
12615 {
12616 char *cur_file;
12617 unsigned int dir_index, mod_time, length;
12618
12619 cur_file = read_direct_string (abfd, line_ptr,
12620 &bytes_read);
12621 line_ptr += bytes_read;
12622 dir_index =
12623 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12624 line_ptr += bytes_read;
12625 mod_time =
12626 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12627 line_ptr += bytes_read;
12628 length =
12629 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12630 line_ptr += bytes_read;
12631 add_file_name (lh, cur_file, dir_index, mod_time, length);
12632 }
12633 break;
12634 case DW_LNE_set_discriminator:
12635 /* The discriminator is not interesting to the debugger;
12636 just ignore it. */
12637 line_ptr = extended_end;
12638 break;
12639 default:
12640 complaint (&symfile_complaints,
12641 _("mangled .debug_line section"));
12642 return;
12643 }
12644 /* Make sure that we parsed the extended op correctly. If e.g.
12645 we expected a different address size than the producer used,
12646 we may have read the wrong number of bytes. */
12647 if (line_ptr != extended_end)
12648 {
12649 complaint (&symfile_complaints,
12650 _("mangled .debug_line section"));
12651 return;
12652 }
12653 break;
12654 case DW_LNS_copy:
12655 if (lh->num_file_names < file || file == 0)
12656 dwarf2_debug_line_missing_file_complaint ();
12657 else
12658 {
12659 lh->file_names[file - 1].included_p = 1;
12660 if (!decode_for_pst_p && is_stmt)
12661 {
12662 if (last_subfile != current_subfile)
12663 {
12664 addr = gdbarch_addr_bits_remove (gdbarch, address);
12665 if (last_subfile)
12666 (*p_record_line) (last_subfile, 0, addr);
12667 last_subfile = current_subfile;
12668 }
12669 addr = gdbarch_addr_bits_remove (gdbarch, address);
12670 (*p_record_line) (current_subfile, line, addr);
12671 }
12672 }
12673 basic_block = 0;
12674 break;
12675 case DW_LNS_advance_pc:
12676 {
12677 CORE_ADDR adjust
12678 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12679
12680 address += (((op_index + adjust)
12681 / lh->maximum_ops_per_instruction)
12682 * lh->minimum_instruction_length);
12683 op_index = ((op_index + adjust)
12684 % lh->maximum_ops_per_instruction);
12685 line_ptr += bytes_read;
12686 }
12687 break;
12688 case DW_LNS_advance_line:
12689 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12690 line_ptr += bytes_read;
12691 break;
12692 case DW_LNS_set_file:
12693 {
12694 /* The arrays lh->include_dirs and lh->file_names are
12695 0-based, but the directory and file name numbers in
12696 the statement program are 1-based. */
12697 struct file_entry *fe;
12698 char *dir = NULL;
12699
12700 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12701 line_ptr += bytes_read;
12702 if (lh->num_file_names < file || file == 0)
12703 dwarf2_debug_line_missing_file_complaint ();
12704 else
12705 {
12706 fe = &lh->file_names[file - 1];
12707 if (fe->dir_index)
12708 dir = lh->include_dirs[fe->dir_index - 1];
12709 if (!decode_for_pst_p)
12710 {
12711 last_subfile = current_subfile;
12712 dwarf2_start_subfile (fe->name, dir, comp_dir);
12713 }
12714 }
12715 }
12716 break;
12717 case DW_LNS_set_column:
12718 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12719 line_ptr += bytes_read;
12720 break;
12721 case DW_LNS_negate_stmt:
12722 is_stmt = (!is_stmt);
12723 break;
12724 case DW_LNS_set_basic_block:
12725 basic_block = 1;
12726 break;
12727 /* Add to the address register of the state machine the
12728 address increment value corresponding to special opcode
12729 255. I.e., this value is scaled by the minimum
12730 instruction length since special opcode 255 would have
12731 scaled the increment. */
12732 case DW_LNS_const_add_pc:
12733 {
12734 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12735
12736 address += (((op_index + adjust)
12737 / lh->maximum_ops_per_instruction)
12738 * lh->minimum_instruction_length);
12739 op_index = ((op_index + adjust)
12740 % lh->maximum_ops_per_instruction);
12741 }
12742 break;
12743 case DW_LNS_fixed_advance_pc:
12744 address += read_2_bytes (abfd, line_ptr);
12745 op_index = 0;
12746 line_ptr += 2;
12747 break;
12748 default:
12749 {
12750 /* Unknown standard opcode, ignore it. */
12751 int i;
12752
12753 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
12754 {
12755 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12756 line_ptr += bytes_read;
12757 }
12758 }
12759 }
12760 }
12761 if (lh->num_file_names < file || file == 0)
12762 dwarf2_debug_line_missing_file_complaint ();
12763 else
12764 {
12765 lh->file_names[file - 1].included_p = 1;
12766 if (!decode_for_pst_p)
12767 {
12768 addr = gdbarch_addr_bits_remove (gdbarch, address);
12769 (*p_record_line) (current_subfile, 0, addr);
12770 }
12771 }
12772 }
12773 }
12774
12775 /* Decode the Line Number Program (LNP) for the given line_header
12776 structure and CU. The actual information extracted and the type
12777 of structures created from the LNP depends on the value of PST.
12778
12779 1. If PST is NULL, then this procedure uses the data from the program
12780 to create all necessary symbol tables, and their linetables.
12781
12782 2. If PST is not NULL, this procedure reads the program to determine
12783 the list of files included by the unit represented by PST, and
12784 builds all the associated partial symbol tables.
12785
12786 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12787 It is used for relative paths in the line table.
12788 NOTE: When processing partial symtabs (pst != NULL),
12789 comp_dir == pst->dirname.
12790
12791 NOTE: It is important that psymtabs have the same file name (via strcmp)
12792 as the corresponding symtab. Since COMP_DIR is not used in the name of the
12793 symtab we don't use it in the name of the psymtabs we create.
12794 E.g. expand_line_sal requires this when finding psymtabs to expand.
12795 A good testcase for this is mb-inline.exp. */
12796
12797 static void
12798 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
12799 struct dwarf2_cu *cu, struct partial_symtab *pst,
12800 int want_line_info)
12801 {
12802 struct objfile *objfile = cu->objfile;
12803 const int decode_for_pst_p = (pst != NULL);
12804 struct subfile *first_subfile = current_subfile;
12805
12806 if (want_line_info)
12807 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
12808
12809 if (decode_for_pst_p)
12810 {
12811 int file_index;
12812
12813 /* Now that we're done scanning the Line Header Program, we can
12814 create the psymtab of each included file. */
12815 for (file_index = 0; file_index < lh->num_file_names; file_index++)
12816 if (lh->file_names[file_index].included_p == 1)
12817 {
12818 char *include_name =
12819 psymtab_include_file_name (lh, file_index, pst, comp_dir);
12820 if (include_name != NULL)
12821 dwarf2_create_include_psymtab (include_name, pst, objfile);
12822 }
12823 }
12824 else
12825 {
12826 /* Make sure a symtab is created for every file, even files
12827 which contain only variables (i.e. no code with associated
12828 line numbers). */
12829 int i;
12830
12831 for (i = 0; i < lh->num_file_names; i++)
12832 {
12833 char *dir = NULL;
12834 struct file_entry *fe;
12835
12836 fe = &lh->file_names[i];
12837 if (fe->dir_index)
12838 dir = lh->include_dirs[fe->dir_index - 1];
12839 dwarf2_start_subfile (fe->name, dir, comp_dir);
12840
12841 /* Skip the main file; we don't need it, and it must be
12842 allocated last, so that it will show up before the
12843 non-primary symtabs in the objfile's symtab list. */
12844 if (current_subfile == first_subfile)
12845 continue;
12846
12847 if (current_subfile->symtab == NULL)
12848 current_subfile->symtab = allocate_symtab (current_subfile->name,
12849 objfile);
12850 fe->symtab = current_subfile->symtab;
12851 }
12852 }
12853 }
12854
12855 /* Start a subfile for DWARF. FILENAME is the name of the file and
12856 DIRNAME the name of the source directory which contains FILENAME
12857 or NULL if not known. COMP_DIR is the compilation directory for the
12858 linetable's compilation unit or NULL if not known.
12859 This routine tries to keep line numbers from identical absolute and
12860 relative file names in a common subfile.
12861
12862 Using the `list' example from the GDB testsuite, which resides in
12863 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
12864 of /srcdir/list0.c yields the following debugging information for list0.c:
12865
12866 DW_AT_name: /srcdir/list0.c
12867 DW_AT_comp_dir: /compdir
12868 files.files[0].name: list0.h
12869 files.files[0].dir: /srcdir
12870 files.files[1].name: list0.c
12871 files.files[1].dir: /srcdir
12872
12873 The line number information for list0.c has to end up in a single
12874 subfile, so that `break /srcdir/list0.c:1' works as expected.
12875 start_subfile will ensure that this happens provided that we pass the
12876 concatenation of files.files[1].dir and files.files[1].name as the
12877 subfile's name. */
12878
12879 static void
12880 dwarf2_start_subfile (char *filename, const char *dirname,
12881 const char *comp_dir)
12882 {
12883 char *fullname;
12884
12885 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
12886 `start_symtab' will always pass the contents of DW_AT_comp_dir as
12887 second argument to start_subfile. To be consistent, we do the
12888 same here. In order not to lose the line information directory,
12889 we concatenate it to the filename when it makes sense.
12890 Note that the Dwarf3 standard says (speaking of filenames in line
12891 information): ``The directory index is ignored for file names
12892 that represent full path names''. Thus ignoring dirname in the
12893 `else' branch below isn't an issue. */
12894
12895 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
12896 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
12897 else
12898 fullname = filename;
12899
12900 start_subfile (fullname, comp_dir);
12901
12902 if (fullname != filename)
12903 xfree (fullname);
12904 }
12905
12906 static void
12907 var_decode_location (struct attribute *attr, struct symbol *sym,
12908 struct dwarf2_cu *cu)
12909 {
12910 struct objfile *objfile = cu->objfile;
12911 struct comp_unit_head *cu_header = &cu->header;
12912
12913 /* NOTE drow/2003-01-30: There used to be a comment and some special
12914 code here to turn a symbol with DW_AT_external and a
12915 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
12916 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
12917 with some versions of binutils) where shared libraries could have
12918 relocations against symbols in their debug information - the
12919 minimal symbol would have the right address, but the debug info
12920 would not. It's no longer necessary, because we will explicitly
12921 apply relocations when we read in the debug information now. */
12922
12923 /* A DW_AT_location attribute with no contents indicates that a
12924 variable has been optimized away. */
12925 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
12926 {
12927 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
12928 return;
12929 }
12930
12931 /* Handle one degenerate form of location expression specially, to
12932 preserve GDB's previous behavior when section offsets are
12933 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
12934 then mark this symbol as LOC_STATIC. */
12935
12936 if (attr_form_is_block (attr)
12937 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
12938 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
12939 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
12940 && (DW_BLOCK (attr)->size
12941 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
12942 {
12943 unsigned int dummy;
12944
12945 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
12946 SYMBOL_VALUE_ADDRESS (sym) =
12947 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
12948 else
12949 SYMBOL_VALUE_ADDRESS (sym) =
12950 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
12951 SYMBOL_CLASS (sym) = LOC_STATIC;
12952 fixup_symbol_section (sym, objfile);
12953 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
12954 SYMBOL_SECTION (sym));
12955 return;
12956 }
12957
12958 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
12959 expression evaluator, and use LOC_COMPUTED only when necessary
12960 (i.e. when the value of a register or memory location is
12961 referenced, or a thread-local block, etc.). Then again, it might
12962 not be worthwhile. I'm assuming that it isn't unless performance
12963 or memory numbers show me otherwise. */
12964
12965 dwarf2_symbol_mark_computed (attr, sym, cu);
12966 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12967
12968 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
12969 cu->has_loclist = 1;
12970 }
12971
12972 /* Given a pointer to a DWARF information entry, figure out if we need
12973 to make a symbol table entry for it, and if so, create a new entry
12974 and return a pointer to it.
12975 If TYPE is NULL, determine symbol type from the die, otherwise
12976 used the passed type.
12977 If SPACE is not NULL, use it to hold the new symbol. If it is
12978 NULL, allocate a new symbol on the objfile's obstack. */
12979
12980 static struct symbol *
12981 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
12982 struct symbol *space)
12983 {
12984 struct objfile *objfile = cu->objfile;
12985 struct symbol *sym = NULL;
12986 char *name;
12987 struct attribute *attr = NULL;
12988 struct attribute *attr2 = NULL;
12989 CORE_ADDR baseaddr;
12990 struct pending **list_to_add = NULL;
12991
12992 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12993
12994 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12995
12996 name = dwarf2_name (die, cu);
12997 if (name)
12998 {
12999 const char *linkagename;
13000 int suppress_add = 0;
13001
13002 if (space)
13003 sym = space;
13004 else
13005 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13006 OBJSTAT (objfile, n_syms++);
13007
13008 /* Cache this symbol's name and the name's demangled form (if any). */
13009 SYMBOL_SET_LANGUAGE (sym, cu->language);
13010 linkagename = dwarf2_physname (name, die, cu);
13011 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13012
13013 /* Fortran does not have mangling standard and the mangling does differ
13014 between gfortran, iFort etc. */
13015 if (cu->language == language_fortran
13016 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13017 symbol_set_demangled_name (&(sym->ginfo),
13018 (char *) dwarf2_full_name (name, die, cu),
13019 NULL);
13020
13021 /* Default assumptions.
13022 Use the passed type or decode it from the die. */
13023 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13024 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13025 if (type != NULL)
13026 SYMBOL_TYPE (sym) = type;
13027 else
13028 SYMBOL_TYPE (sym) = die_type (die, cu);
13029 attr = dwarf2_attr (die,
13030 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13031 cu);
13032 if (attr)
13033 {
13034 SYMBOL_LINE (sym) = DW_UNSND (attr);
13035 }
13036
13037 attr = dwarf2_attr (die,
13038 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13039 cu);
13040 if (attr)
13041 {
13042 int file_index = DW_UNSND (attr);
13043
13044 if (cu->line_header == NULL
13045 || file_index > cu->line_header->num_file_names)
13046 complaint (&symfile_complaints,
13047 _("file index out of range"));
13048 else if (file_index > 0)
13049 {
13050 struct file_entry *fe;
13051
13052 fe = &cu->line_header->file_names[file_index - 1];
13053 SYMBOL_SYMTAB (sym) = fe->symtab;
13054 }
13055 }
13056
13057 switch (die->tag)
13058 {
13059 case DW_TAG_label:
13060 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13061 if (attr)
13062 {
13063 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13064 }
13065 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13066 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13067 SYMBOL_CLASS (sym) = LOC_LABEL;
13068 add_symbol_to_list (sym, cu->list_in_scope);
13069 break;
13070 case DW_TAG_subprogram:
13071 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13072 finish_block. */
13073 SYMBOL_CLASS (sym) = LOC_BLOCK;
13074 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13075 if ((attr2 && (DW_UNSND (attr2) != 0))
13076 || cu->language == language_ada)
13077 {
13078 /* Subprograms marked external are stored as a global symbol.
13079 Ada subprograms, whether marked external or not, are always
13080 stored as a global symbol, because we want to be able to
13081 access them globally. For instance, we want to be able
13082 to break on a nested subprogram without having to
13083 specify the context. */
13084 list_to_add = &global_symbols;
13085 }
13086 else
13087 {
13088 list_to_add = cu->list_in_scope;
13089 }
13090 break;
13091 case DW_TAG_inlined_subroutine:
13092 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13093 finish_block. */
13094 SYMBOL_CLASS (sym) = LOC_BLOCK;
13095 SYMBOL_INLINED (sym) = 1;
13096 list_to_add = cu->list_in_scope;
13097 break;
13098 case DW_TAG_template_value_param:
13099 suppress_add = 1;
13100 /* Fall through. */
13101 case DW_TAG_constant:
13102 case DW_TAG_variable:
13103 case DW_TAG_member:
13104 /* Compilation with minimal debug info may result in
13105 variables with missing type entries. Change the
13106 misleading `void' type to something sensible. */
13107 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13108 SYMBOL_TYPE (sym)
13109 = objfile_type (objfile)->nodebug_data_symbol;
13110
13111 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13112 /* In the case of DW_TAG_member, we should only be called for
13113 static const members. */
13114 if (die->tag == DW_TAG_member)
13115 {
13116 /* dwarf2_add_field uses die_is_declaration,
13117 so we do the same. */
13118 gdb_assert (die_is_declaration (die, cu));
13119 gdb_assert (attr);
13120 }
13121 if (attr)
13122 {
13123 dwarf2_const_value (attr, sym, cu);
13124 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13125 if (!suppress_add)
13126 {
13127 if (attr2 && (DW_UNSND (attr2) != 0))
13128 list_to_add = &global_symbols;
13129 else
13130 list_to_add = cu->list_in_scope;
13131 }
13132 break;
13133 }
13134 attr = dwarf2_attr (die, DW_AT_location, cu);
13135 if (attr)
13136 {
13137 var_decode_location (attr, sym, cu);
13138 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13139 if (SYMBOL_CLASS (sym) == LOC_STATIC
13140 && SYMBOL_VALUE_ADDRESS (sym) == 0
13141 && !dwarf2_per_objfile->has_section_at_zero)
13142 {
13143 /* When a static variable is eliminated by the linker,
13144 the corresponding debug information is not stripped
13145 out, but the variable address is set to null;
13146 do not add such variables into symbol table. */
13147 }
13148 else if (attr2 && (DW_UNSND (attr2) != 0))
13149 {
13150 /* Workaround gfortran PR debug/40040 - it uses
13151 DW_AT_location for variables in -fPIC libraries which may
13152 get overriden by other libraries/executable and get
13153 a different address. Resolve it by the minimal symbol
13154 which may come from inferior's executable using copy
13155 relocation. Make this workaround only for gfortran as for
13156 other compilers GDB cannot guess the minimal symbol
13157 Fortran mangling kind. */
13158 if (cu->language == language_fortran && die->parent
13159 && die->parent->tag == DW_TAG_module
13160 && cu->producer
13161 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13162 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13163
13164 /* A variable with DW_AT_external is never static,
13165 but it may be block-scoped. */
13166 list_to_add = (cu->list_in_scope == &file_symbols
13167 ? &global_symbols : cu->list_in_scope);
13168 }
13169 else
13170 list_to_add = cu->list_in_scope;
13171 }
13172 else
13173 {
13174 /* We do not know the address of this symbol.
13175 If it is an external symbol and we have type information
13176 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13177 The address of the variable will then be determined from
13178 the minimal symbol table whenever the variable is
13179 referenced. */
13180 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13181 if (attr2 && (DW_UNSND (attr2) != 0)
13182 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13183 {
13184 /* A variable with DW_AT_external is never static, but it
13185 may be block-scoped. */
13186 list_to_add = (cu->list_in_scope == &file_symbols
13187 ? &global_symbols : cu->list_in_scope);
13188
13189 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13190 }
13191 else if (!die_is_declaration (die, cu))
13192 {
13193 /* Use the default LOC_OPTIMIZED_OUT class. */
13194 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13195 if (!suppress_add)
13196 list_to_add = cu->list_in_scope;
13197 }
13198 }
13199 break;
13200 case DW_TAG_formal_parameter:
13201 /* If we are inside a function, mark this as an argument. If
13202 not, we might be looking at an argument to an inlined function
13203 when we do not have enough information to show inlined frames;
13204 pretend it's a local variable in that case so that the user can
13205 still see it. */
13206 if (context_stack_depth > 0
13207 && context_stack[context_stack_depth - 1].name != NULL)
13208 SYMBOL_IS_ARGUMENT (sym) = 1;
13209 attr = dwarf2_attr (die, DW_AT_location, cu);
13210 if (attr)
13211 {
13212 var_decode_location (attr, sym, cu);
13213 }
13214 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13215 if (attr)
13216 {
13217 dwarf2_const_value (attr, sym, cu);
13218 }
13219
13220 list_to_add = cu->list_in_scope;
13221 break;
13222 case DW_TAG_unspecified_parameters:
13223 /* From varargs functions; gdb doesn't seem to have any
13224 interest in this information, so just ignore it for now.
13225 (FIXME?) */
13226 break;
13227 case DW_TAG_template_type_param:
13228 suppress_add = 1;
13229 /* Fall through. */
13230 case DW_TAG_class_type:
13231 case DW_TAG_interface_type:
13232 case DW_TAG_structure_type:
13233 case DW_TAG_union_type:
13234 case DW_TAG_set_type:
13235 case DW_TAG_enumeration_type:
13236 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13237 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13238
13239 {
13240 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13241 really ever be static objects: otherwise, if you try
13242 to, say, break of a class's method and you're in a file
13243 which doesn't mention that class, it won't work unless
13244 the check for all static symbols in lookup_symbol_aux
13245 saves you. See the OtherFileClass tests in
13246 gdb.c++/namespace.exp. */
13247
13248 if (!suppress_add)
13249 {
13250 list_to_add = (cu->list_in_scope == &file_symbols
13251 && (cu->language == language_cplus
13252 || cu->language == language_java)
13253 ? &global_symbols : cu->list_in_scope);
13254
13255 /* The semantics of C++ state that "struct foo {
13256 ... }" also defines a typedef for "foo". A Java
13257 class declaration also defines a typedef for the
13258 class. */
13259 if (cu->language == language_cplus
13260 || cu->language == language_java
13261 || cu->language == language_ada)
13262 {
13263 /* The symbol's name is already allocated along
13264 with this objfile, so we don't need to
13265 duplicate it for the type. */
13266 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13267 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13268 }
13269 }
13270 }
13271 break;
13272 case DW_TAG_typedef:
13273 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13274 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13275 list_to_add = cu->list_in_scope;
13276 break;
13277 case DW_TAG_base_type:
13278 case DW_TAG_subrange_type:
13279 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13280 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13281 list_to_add = cu->list_in_scope;
13282 break;
13283 case DW_TAG_enumerator:
13284 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13285 if (attr)
13286 {
13287 dwarf2_const_value (attr, sym, cu);
13288 }
13289 {
13290 /* NOTE: carlton/2003-11-10: See comment above in the
13291 DW_TAG_class_type, etc. block. */
13292
13293 list_to_add = (cu->list_in_scope == &file_symbols
13294 && (cu->language == language_cplus
13295 || cu->language == language_java)
13296 ? &global_symbols : cu->list_in_scope);
13297 }
13298 break;
13299 case DW_TAG_namespace:
13300 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13301 list_to_add = &global_symbols;
13302 break;
13303 default:
13304 /* Not a tag we recognize. Hopefully we aren't processing
13305 trash data, but since we must specifically ignore things
13306 we don't recognize, there is nothing else we should do at
13307 this point. */
13308 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13309 dwarf_tag_name (die->tag));
13310 break;
13311 }
13312
13313 if (suppress_add)
13314 {
13315 sym->hash_next = objfile->template_symbols;
13316 objfile->template_symbols = sym;
13317 list_to_add = NULL;
13318 }
13319
13320 if (list_to_add != NULL)
13321 add_symbol_to_list (sym, list_to_add);
13322
13323 /* For the benefit of old versions of GCC, check for anonymous
13324 namespaces based on the demangled name. */
13325 if (!processing_has_namespace_info
13326 && cu->language == language_cplus)
13327 cp_scan_for_anonymous_namespaces (sym, objfile);
13328 }
13329 return (sym);
13330 }
13331
13332 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13333
13334 static struct symbol *
13335 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13336 {
13337 return new_symbol_full (die, type, cu, NULL);
13338 }
13339
13340 /* Given an attr with a DW_FORM_dataN value in host byte order,
13341 zero-extend it as appropriate for the symbol's type. The DWARF
13342 standard (v4) is not entirely clear about the meaning of using
13343 DW_FORM_dataN for a constant with a signed type, where the type is
13344 wider than the data. The conclusion of a discussion on the DWARF
13345 list was that this is unspecified. We choose to always zero-extend
13346 because that is the interpretation long in use by GCC. */
13347
13348 static gdb_byte *
13349 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13350 const char *name, struct obstack *obstack,
13351 struct dwarf2_cu *cu, LONGEST *value, int bits)
13352 {
13353 struct objfile *objfile = cu->objfile;
13354 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13355 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13356 LONGEST l = DW_UNSND (attr);
13357
13358 if (bits < sizeof (*value) * 8)
13359 {
13360 l &= ((LONGEST) 1 << bits) - 1;
13361 *value = l;
13362 }
13363 else if (bits == sizeof (*value) * 8)
13364 *value = l;
13365 else
13366 {
13367 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13368 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13369 return bytes;
13370 }
13371
13372 return NULL;
13373 }
13374
13375 /* Read a constant value from an attribute. Either set *VALUE, or if
13376 the value does not fit in *VALUE, set *BYTES - either already
13377 allocated on the objfile obstack, or newly allocated on OBSTACK,
13378 or, set *BATON, if we translated the constant to a location
13379 expression. */
13380
13381 static void
13382 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13383 const char *name, struct obstack *obstack,
13384 struct dwarf2_cu *cu,
13385 LONGEST *value, gdb_byte **bytes,
13386 struct dwarf2_locexpr_baton **baton)
13387 {
13388 struct objfile *objfile = cu->objfile;
13389 struct comp_unit_head *cu_header = &cu->header;
13390 struct dwarf_block *blk;
13391 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13392 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13393
13394 *value = 0;
13395 *bytes = NULL;
13396 *baton = NULL;
13397
13398 switch (attr->form)
13399 {
13400 case DW_FORM_addr:
13401 case DW_FORM_GNU_addr_index:
13402 {
13403 gdb_byte *data;
13404
13405 if (TYPE_LENGTH (type) != cu_header->addr_size)
13406 dwarf2_const_value_length_mismatch_complaint (name,
13407 cu_header->addr_size,
13408 TYPE_LENGTH (type));
13409 /* Symbols of this form are reasonably rare, so we just
13410 piggyback on the existing location code rather than writing
13411 a new implementation of symbol_computed_ops. */
13412 *baton = obstack_alloc (&objfile->objfile_obstack,
13413 sizeof (struct dwarf2_locexpr_baton));
13414 (*baton)->per_cu = cu->per_cu;
13415 gdb_assert ((*baton)->per_cu);
13416
13417 (*baton)->size = 2 + cu_header->addr_size;
13418 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13419 (*baton)->data = data;
13420
13421 data[0] = DW_OP_addr;
13422 store_unsigned_integer (&data[1], cu_header->addr_size,
13423 byte_order, DW_ADDR (attr));
13424 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13425 }
13426 break;
13427 case DW_FORM_string:
13428 case DW_FORM_strp:
13429 case DW_FORM_GNU_str_index:
13430 /* DW_STRING is already allocated on the objfile obstack, point
13431 directly to it. */
13432 *bytes = (gdb_byte *) DW_STRING (attr);
13433 break;
13434 case DW_FORM_block1:
13435 case DW_FORM_block2:
13436 case DW_FORM_block4:
13437 case DW_FORM_block:
13438 case DW_FORM_exprloc:
13439 blk = DW_BLOCK (attr);
13440 if (TYPE_LENGTH (type) != blk->size)
13441 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13442 TYPE_LENGTH (type));
13443 *bytes = blk->data;
13444 break;
13445
13446 /* The DW_AT_const_value attributes are supposed to carry the
13447 symbol's value "represented as it would be on the target
13448 architecture." By the time we get here, it's already been
13449 converted to host endianness, so we just need to sign- or
13450 zero-extend it as appropriate. */
13451 case DW_FORM_data1:
13452 *bytes = dwarf2_const_value_data (attr, type, name,
13453 obstack, cu, value, 8);
13454 break;
13455 case DW_FORM_data2:
13456 *bytes = dwarf2_const_value_data (attr, type, name,
13457 obstack, cu, value, 16);
13458 break;
13459 case DW_FORM_data4:
13460 *bytes = dwarf2_const_value_data (attr, type, name,
13461 obstack, cu, value, 32);
13462 break;
13463 case DW_FORM_data8:
13464 *bytes = dwarf2_const_value_data (attr, type, name,
13465 obstack, cu, value, 64);
13466 break;
13467
13468 case DW_FORM_sdata:
13469 *value = DW_SND (attr);
13470 break;
13471
13472 case DW_FORM_udata:
13473 *value = DW_UNSND (attr);
13474 break;
13475
13476 default:
13477 complaint (&symfile_complaints,
13478 _("unsupported const value attribute form: '%s'"),
13479 dwarf_form_name (attr->form));
13480 *value = 0;
13481 break;
13482 }
13483 }
13484
13485
13486 /* Copy constant value from an attribute to a symbol. */
13487
13488 static void
13489 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13490 struct dwarf2_cu *cu)
13491 {
13492 struct objfile *objfile = cu->objfile;
13493 struct comp_unit_head *cu_header = &cu->header;
13494 LONGEST value;
13495 gdb_byte *bytes;
13496 struct dwarf2_locexpr_baton *baton;
13497
13498 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13499 SYMBOL_PRINT_NAME (sym),
13500 &objfile->objfile_obstack, cu,
13501 &value, &bytes, &baton);
13502
13503 if (baton != NULL)
13504 {
13505 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13506 SYMBOL_LOCATION_BATON (sym) = baton;
13507 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13508 }
13509 else if (bytes != NULL)
13510 {
13511 SYMBOL_VALUE_BYTES (sym) = bytes;
13512 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13513 }
13514 else
13515 {
13516 SYMBOL_VALUE (sym) = value;
13517 SYMBOL_CLASS (sym) = LOC_CONST;
13518 }
13519 }
13520
13521 /* Return the type of the die in question using its DW_AT_type attribute. */
13522
13523 static struct type *
13524 die_type (struct die_info *die, struct dwarf2_cu *cu)
13525 {
13526 struct attribute *type_attr;
13527
13528 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13529 if (!type_attr)
13530 {
13531 /* A missing DW_AT_type represents a void type. */
13532 return objfile_type (cu->objfile)->builtin_void;
13533 }
13534
13535 return lookup_die_type (die, type_attr, cu);
13536 }
13537
13538 /* True iff CU's producer generates GNAT Ada auxiliary information
13539 that allows to find parallel types through that information instead
13540 of having to do expensive parallel lookups by type name. */
13541
13542 static int
13543 need_gnat_info (struct dwarf2_cu *cu)
13544 {
13545 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13546 of GNAT produces this auxiliary information, without any indication
13547 that it is produced. Part of enhancing the FSF version of GNAT
13548 to produce that information will be to put in place an indicator
13549 that we can use in order to determine whether the descriptive type
13550 info is available or not. One suggestion that has been made is
13551 to use a new attribute, attached to the CU die. For now, assume
13552 that the descriptive type info is not available. */
13553 return 0;
13554 }
13555
13556 /* Return the auxiliary type of the die in question using its
13557 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13558 attribute is not present. */
13559
13560 static struct type *
13561 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13562 {
13563 struct attribute *type_attr;
13564
13565 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13566 if (!type_attr)
13567 return NULL;
13568
13569 return lookup_die_type (die, type_attr, cu);
13570 }
13571
13572 /* If DIE has a descriptive_type attribute, then set the TYPE's
13573 descriptive type accordingly. */
13574
13575 static void
13576 set_descriptive_type (struct type *type, struct die_info *die,
13577 struct dwarf2_cu *cu)
13578 {
13579 struct type *descriptive_type = die_descriptive_type (die, cu);
13580
13581 if (descriptive_type)
13582 {
13583 ALLOCATE_GNAT_AUX_TYPE (type);
13584 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13585 }
13586 }
13587
13588 /* Return the containing type of the die in question using its
13589 DW_AT_containing_type attribute. */
13590
13591 static struct type *
13592 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13593 {
13594 struct attribute *type_attr;
13595
13596 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13597 if (!type_attr)
13598 error (_("Dwarf Error: Problem turning containing type into gdb type "
13599 "[in module %s]"), cu->objfile->name);
13600
13601 return lookup_die_type (die, type_attr, cu);
13602 }
13603
13604 /* Look up the type of DIE in CU using its type attribute ATTR.
13605 If there is no type substitute an error marker. */
13606
13607 static struct type *
13608 lookup_die_type (struct die_info *die, struct attribute *attr,
13609 struct dwarf2_cu *cu)
13610 {
13611 struct objfile *objfile = cu->objfile;
13612 struct type *this_type;
13613
13614 /* First see if we have it cached. */
13615
13616 if (is_ref_attr (attr))
13617 {
13618 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13619
13620 this_type = get_die_type_at_offset (offset, cu->per_cu);
13621 }
13622 else if (attr->form == DW_FORM_ref_sig8)
13623 {
13624 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13625
13626 /* sig_type will be NULL if the signatured type is missing from
13627 the debug info. */
13628 if (sig_type == NULL)
13629 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13630 "at 0x%x [in module %s]"),
13631 die->offset.sect_off, objfile->name);
13632
13633 gdb_assert (sig_type->per_cu.is_debug_types);
13634 /* If we haven't filled in type_offset_in_section yet, then we
13635 haven't read the type in yet. */
13636 this_type = NULL;
13637 if (sig_type->type_offset_in_section.sect_off != 0)
13638 {
13639 this_type =
13640 get_die_type_at_offset (sig_type->type_offset_in_section,
13641 &sig_type->per_cu);
13642 }
13643 }
13644 else
13645 {
13646 dump_die_for_error (die);
13647 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13648 dwarf_attr_name (attr->name), objfile->name);
13649 }
13650
13651 /* If not cached we need to read it in. */
13652
13653 if (this_type == NULL)
13654 {
13655 struct die_info *type_die;
13656 struct dwarf2_cu *type_cu = cu;
13657
13658 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13659 /* If we found the type now, it's probably because the type came
13660 from an inter-CU reference and the type's CU got expanded before
13661 ours. */
13662 this_type = get_die_type (type_die, type_cu);
13663 if (this_type == NULL)
13664 this_type = read_type_die_1 (type_die, type_cu);
13665 }
13666
13667 /* If we still don't have a type use an error marker. */
13668
13669 if (this_type == NULL)
13670 {
13671 char *message, *saved;
13672
13673 /* read_type_die already issued a complaint. */
13674 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13675 objfile->name,
13676 cu->header.offset.sect_off,
13677 die->offset.sect_off);
13678 saved = obstack_copy0 (&objfile->objfile_obstack,
13679 message, strlen (message));
13680 xfree (message);
13681
13682 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13683 }
13684
13685 return this_type;
13686 }
13687
13688 /* Return the type in DIE, CU.
13689 Returns NULL for invalid types.
13690
13691 This first does a lookup in the appropriate type_hash table,
13692 and only reads the die in if necessary.
13693
13694 NOTE: This can be called when reading in partial or full symbols. */
13695
13696 static struct type *
13697 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13698 {
13699 struct type *this_type;
13700
13701 this_type = get_die_type (die, cu);
13702 if (this_type)
13703 return this_type;
13704
13705 return read_type_die_1 (die, cu);
13706 }
13707
13708 /* Read the type in DIE, CU.
13709 Returns NULL for invalid types. */
13710
13711 static struct type *
13712 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13713 {
13714 struct type *this_type = NULL;
13715
13716 switch (die->tag)
13717 {
13718 case DW_TAG_class_type:
13719 case DW_TAG_interface_type:
13720 case DW_TAG_structure_type:
13721 case DW_TAG_union_type:
13722 this_type = read_structure_type (die, cu);
13723 break;
13724 case DW_TAG_enumeration_type:
13725 this_type = read_enumeration_type (die, cu);
13726 break;
13727 case DW_TAG_subprogram:
13728 case DW_TAG_subroutine_type:
13729 case DW_TAG_inlined_subroutine:
13730 this_type = read_subroutine_type (die, cu);
13731 break;
13732 case DW_TAG_array_type:
13733 this_type = read_array_type (die, cu);
13734 break;
13735 case DW_TAG_set_type:
13736 this_type = read_set_type (die, cu);
13737 break;
13738 case DW_TAG_pointer_type:
13739 this_type = read_tag_pointer_type (die, cu);
13740 break;
13741 case DW_TAG_ptr_to_member_type:
13742 this_type = read_tag_ptr_to_member_type (die, cu);
13743 break;
13744 case DW_TAG_reference_type:
13745 this_type = read_tag_reference_type (die, cu);
13746 break;
13747 case DW_TAG_const_type:
13748 this_type = read_tag_const_type (die, cu);
13749 break;
13750 case DW_TAG_volatile_type:
13751 this_type = read_tag_volatile_type (die, cu);
13752 break;
13753 case DW_TAG_string_type:
13754 this_type = read_tag_string_type (die, cu);
13755 break;
13756 case DW_TAG_typedef:
13757 this_type = read_typedef (die, cu);
13758 break;
13759 case DW_TAG_subrange_type:
13760 this_type = read_subrange_type (die, cu);
13761 break;
13762 case DW_TAG_base_type:
13763 this_type = read_base_type (die, cu);
13764 break;
13765 case DW_TAG_unspecified_type:
13766 this_type = read_unspecified_type (die, cu);
13767 break;
13768 case DW_TAG_namespace:
13769 this_type = read_namespace_type (die, cu);
13770 break;
13771 case DW_TAG_module:
13772 this_type = read_module_type (die, cu);
13773 break;
13774 default:
13775 complaint (&symfile_complaints,
13776 _("unexpected tag in read_type_die: '%s'"),
13777 dwarf_tag_name (die->tag));
13778 break;
13779 }
13780
13781 return this_type;
13782 }
13783
13784 /* See if we can figure out if the class lives in a namespace. We do
13785 this by looking for a member function; its demangled name will
13786 contain namespace info, if there is any.
13787 Return the computed name or NULL.
13788 Space for the result is allocated on the objfile's obstack.
13789 This is the full-die version of guess_partial_die_structure_name.
13790 In this case we know DIE has no useful parent. */
13791
13792 static char *
13793 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
13794 {
13795 struct die_info *spec_die;
13796 struct dwarf2_cu *spec_cu;
13797 struct die_info *child;
13798
13799 spec_cu = cu;
13800 spec_die = die_specification (die, &spec_cu);
13801 if (spec_die != NULL)
13802 {
13803 die = spec_die;
13804 cu = spec_cu;
13805 }
13806
13807 for (child = die->child;
13808 child != NULL;
13809 child = child->sibling)
13810 {
13811 if (child->tag == DW_TAG_subprogram)
13812 {
13813 struct attribute *attr;
13814
13815 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
13816 if (attr == NULL)
13817 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
13818 if (attr != NULL)
13819 {
13820 char *actual_name
13821 = language_class_name_from_physname (cu->language_defn,
13822 DW_STRING (attr));
13823 char *name = NULL;
13824
13825 if (actual_name != NULL)
13826 {
13827 char *die_name = dwarf2_name (die, cu);
13828
13829 if (die_name != NULL
13830 && strcmp (die_name, actual_name) != 0)
13831 {
13832 /* Strip off the class name from the full name.
13833 We want the prefix. */
13834 int die_name_len = strlen (die_name);
13835 int actual_name_len = strlen (actual_name);
13836
13837 /* Test for '::' as a sanity check. */
13838 if (actual_name_len > die_name_len + 2
13839 && actual_name[actual_name_len
13840 - die_name_len - 1] == ':')
13841 name =
13842 obsavestring (actual_name,
13843 actual_name_len - die_name_len - 2,
13844 &cu->objfile->objfile_obstack);
13845 }
13846 }
13847 xfree (actual_name);
13848 return name;
13849 }
13850 }
13851 }
13852
13853 return NULL;
13854 }
13855
13856 /* GCC might emit a nameless typedef that has a linkage name. Determine the
13857 prefix part in such case. See
13858 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13859
13860 static char *
13861 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
13862 {
13863 struct attribute *attr;
13864 char *base;
13865
13866 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
13867 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
13868 return NULL;
13869
13870 attr = dwarf2_attr (die, DW_AT_name, cu);
13871 if (attr != NULL && DW_STRING (attr) != NULL)
13872 return NULL;
13873
13874 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
13875 if (attr == NULL)
13876 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
13877 if (attr == NULL || DW_STRING (attr) == NULL)
13878 return NULL;
13879
13880 /* dwarf2_name had to be already called. */
13881 gdb_assert (DW_STRING_IS_CANONICAL (attr));
13882
13883 /* Strip the base name, keep any leading namespaces/classes. */
13884 base = strrchr (DW_STRING (attr), ':');
13885 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
13886 return "";
13887
13888 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
13889 &cu->objfile->objfile_obstack);
13890 }
13891
13892 /* Return the name of the namespace/class that DIE is defined within,
13893 or "" if we can't tell. The caller should not xfree the result.
13894
13895 For example, if we're within the method foo() in the following
13896 code:
13897
13898 namespace N {
13899 class C {
13900 void foo () {
13901 }
13902 };
13903 }
13904
13905 then determine_prefix on foo's die will return "N::C". */
13906
13907 static const char *
13908 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
13909 {
13910 struct die_info *parent, *spec_die;
13911 struct dwarf2_cu *spec_cu;
13912 struct type *parent_type;
13913 char *retval;
13914
13915 if (cu->language != language_cplus && cu->language != language_java
13916 && cu->language != language_fortran)
13917 return "";
13918
13919 retval = anonymous_struct_prefix (die, cu);
13920 if (retval)
13921 return retval;
13922
13923 /* We have to be careful in the presence of DW_AT_specification.
13924 For example, with GCC 3.4, given the code
13925
13926 namespace N {
13927 void foo() {
13928 // Definition of N::foo.
13929 }
13930 }
13931
13932 then we'll have a tree of DIEs like this:
13933
13934 1: DW_TAG_compile_unit
13935 2: DW_TAG_namespace // N
13936 3: DW_TAG_subprogram // declaration of N::foo
13937 4: DW_TAG_subprogram // definition of N::foo
13938 DW_AT_specification // refers to die #3
13939
13940 Thus, when processing die #4, we have to pretend that we're in
13941 the context of its DW_AT_specification, namely the contex of die
13942 #3. */
13943 spec_cu = cu;
13944 spec_die = die_specification (die, &spec_cu);
13945 if (spec_die == NULL)
13946 parent = die->parent;
13947 else
13948 {
13949 parent = spec_die->parent;
13950 cu = spec_cu;
13951 }
13952
13953 if (parent == NULL)
13954 return "";
13955 else if (parent->building_fullname)
13956 {
13957 const char *name;
13958 const char *parent_name;
13959
13960 /* It has been seen on RealView 2.2 built binaries,
13961 DW_TAG_template_type_param types actually _defined_ as
13962 children of the parent class:
13963
13964 enum E {};
13965 template class <class Enum> Class{};
13966 Class<enum E> class_e;
13967
13968 1: DW_TAG_class_type (Class)
13969 2: DW_TAG_enumeration_type (E)
13970 3: DW_TAG_enumerator (enum1:0)
13971 3: DW_TAG_enumerator (enum2:1)
13972 ...
13973 2: DW_TAG_template_type_param
13974 DW_AT_type DW_FORM_ref_udata (E)
13975
13976 Besides being broken debug info, it can put GDB into an
13977 infinite loop. Consider:
13978
13979 When we're building the full name for Class<E>, we'll start
13980 at Class, and go look over its template type parameters,
13981 finding E. We'll then try to build the full name of E, and
13982 reach here. We're now trying to build the full name of E,
13983 and look over the parent DIE for containing scope. In the
13984 broken case, if we followed the parent DIE of E, we'd again
13985 find Class, and once again go look at its template type
13986 arguments, etc., etc. Simply don't consider such parent die
13987 as source-level parent of this die (it can't be, the language
13988 doesn't allow it), and break the loop here. */
13989 name = dwarf2_name (die, cu);
13990 parent_name = dwarf2_name (parent, cu);
13991 complaint (&symfile_complaints,
13992 _("template param type '%s' defined within parent '%s'"),
13993 name ? name : "<unknown>",
13994 parent_name ? parent_name : "<unknown>");
13995 return "";
13996 }
13997 else
13998 switch (parent->tag)
13999 {
14000 case DW_TAG_namespace:
14001 parent_type = read_type_die (parent, cu);
14002 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14003 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14004 Work around this problem here. */
14005 if (cu->language == language_cplus
14006 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14007 return "";
14008 /* We give a name to even anonymous namespaces. */
14009 return TYPE_TAG_NAME (parent_type);
14010 case DW_TAG_class_type:
14011 case DW_TAG_interface_type:
14012 case DW_TAG_structure_type:
14013 case DW_TAG_union_type:
14014 case DW_TAG_module:
14015 parent_type = read_type_die (parent, cu);
14016 if (TYPE_TAG_NAME (parent_type) != NULL)
14017 return TYPE_TAG_NAME (parent_type);
14018 else
14019 /* An anonymous structure is only allowed non-static data
14020 members; no typedefs, no member functions, et cetera.
14021 So it does not need a prefix. */
14022 return "";
14023 case DW_TAG_compile_unit:
14024 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14025 if (cu->language == language_cplus
14026 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14027 && die->child != NULL
14028 && (die->tag == DW_TAG_class_type
14029 || die->tag == DW_TAG_structure_type
14030 || die->tag == DW_TAG_union_type))
14031 {
14032 char *name = guess_full_die_structure_name (die, cu);
14033 if (name != NULL)
14034 return name;
14035 }
14036 return "";
14037 default:
14038 return determine_prefix (parent, cu);
14039 }
14040 }
14041
14042 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14043 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14044 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14045 an obconcat, otherwise allocate storage for the result. The CU argument is
14046 used to determine the language and hence, the appropriate separator. */
14047
14048 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14049
14050 static char *
14051 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14052 int physname, struct dwarf2_cu *cu)
14053 {
14054 const char *lead = "";
14055 const char *sep;
14056
14057 if (suffix == NULL || suffix[0] == '\0'
14058 || prefix == NULL || prefix[0] == '\0')
14059 sep = "";
14060 else if (cu->language == language_java)
14061 sep = ".";
14062 else if (cu->language == language_fortran && physname)
14063 {
14064 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14065 DW_AT_MIPS_linkage_name is preferred and used instead. */
14066
14067 lead = "__";
14068 sep = "_MOD_";
14069 }
14070 else
14071 sep = "::";
14072
14073 if (prefix == NULL)
14074 prefix = "";
14075 if (suffix == NULL)
14076 suffix = "";
14077
14078 if (obs == NULL)
14079 {
14080 char *retval
14081 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14082
14083 strcpy (retval, lead);
14084 strcat (retval, prefix);
14085 strcat (retval, sep);
14086 strcat (retval, suffix);
14087 return retval;
14088 }
14089 else
14090 {
14091 /* We have an obstack. */
14092 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14093 }
14094 }
14095
14096 /* Return sibling of die, NULL if no sibling. */
14097
14098 static struct die_info *
14099 sibling_die (struct die_info *die)
14100 {
14101 return die->sibling;
14102 }
14103
14104 /* Get name of a die, return NULL if not found. */
14105
14106 static char *
14107 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14108 struct obstack *obstack)
14109 {
14110 if (name && cu->language == language_cplus)
14111 {
14112 char *canon_name = cp_canonicalize_string (name);
14113
14114 if (canon_name != NULL)
14115 {
14116 if (strcmp (canon_name, name) != 0)
14117 name = obsavestring (canon_name, strlen (canon_name),
14118 obstack);
14119 xfree (canon_name);
14120 }
14121 }
14122
14123 return name;
14124 }
14125
14126 /* Get name of a die, return NULL if not found. */
14127
14128 static char *
14129 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14130 {
14131 struct attribute *attr;
14132
14133 attr = dwarf2_attr (die, DW_AT_name, cu);
14134 if ((!attr || !DW_STRING (attr))
14135 && die->tag != DW_TAG_class_type
14136 && die->tag != DW_TAG_interface_type
14137 && die->tag != DW_TAG_structure_type
14138 && die->tag != DW_TAG_union_type)
14139 return NULL;
14140
14141 switch (die->tag)
14142 {
14143 case DW_TAG_compile_unit:
14144 /* Compilation units have a DW_AT_name that is a filename, not
14145 a source language identifier. */
14146 case DW_TAG_enumeration_type:
14147 case DW_TAG_enumerator:
14148 /* These tags always have simple identifiers already; no need
14149 to canonicalize them. */
14150 return DW_STRING (attr);
14151
14152 case DW_TAG_subprogram:
14153 /* Java constructors will all be named "<init>", so return
14154 the class name when we see this special case. */
14155 if (cu->language == language_java
14156 && DW_STRING (attr) != NULL
14157 && strcmp (DW_STRING (attr), "<init>") == 0)
14158 {
14159 struct dwarf2_cu *spec_cu = cu;
14160 struct die_info *spec_die;
14161
14162 /* GCJ will output '<init>' for Java constructor names.
14163 For this special case, return the name of the parent class. */
14164
14165 /* GCJ may output suprogram DIEs with AT_specification set.
14166 If so, use the name of the specified DIE. */
14167 spec_die = die_specification (die, &spec_cu);
14168 if (spec_die != NULL)
14169 return dwarf2_name (spec_die, spec_cu);
14170
14171 do
14172 {
14173 die = die->parent;
14174 if (die->tag == DW_TAG_class_type)
14175 return dwarf2_name (die, cu);
14176 }
14177 while (die->tag != DW_TAG_compile_unit);
14178 }
14179 break;
14180
14181 case DW_TAG_class_type:
14182 case DW_TAG_interface_type:
14183 case DW_TAG_structure_type:
14184 case DW_TAG_union_type:
14185 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14186 structures or unions. These were of the form "._%d" in GCC 4.1,
14187 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14188 and GCC 4.4. We work around this problem by ignoring these. */
14189 if (attr && DW_STRING (attr)
14190 && (strncmp (DW_STRING (attr), "._", 2) == 0
14191 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14192 return NULL;
14193
14194 /* GCC might emit a nameless typedef that has a linkage name. See
14195 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14196 if (!attr || DW_STRING (attr) == NULL)
14197 {
14198 char *demangled = NULL;
14199
14200 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14201 if (attr == NULL)
14202 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14203
14204 if (attr == NULL || DW_STRING (attr) == NULL)
14205 return NULL;
14206
14207 /* Avoid demangling DW_STRING (attr) the second time on a second
14208 call for the same DIE. */
14209 if (!DW_STRING_IS_CANONICAL (attr))
14210 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14211
14212 if (demangled)
14213 {
14214 char *base;
14215
14216 /* FIXME: we already did this for the partial symbol... */
14217 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14218 &cu->objfile->objfile_obstack);
14219 DW_STRING_IS_CANONICAL (attr) = 1;
14220 xfree (demangled);
14221
14222 /* Strip any leading namespaces/classes, keep only the base name.
14223 DW_AT_name for named DIEs does not contain the prefixes. */
14224 base = strrchr (DW_STRING (attr), ':');
14225 if (base && base > DW_STRING (attr) && base[-1] == ':')
14226 return &base[1];
14227 else
14228 return DW_STRING (attr);
14229 }
14230 }
14231 break;
14232
14233 default:
14234 break;
14235 }
14236
14237 if (!DW_STRING_IS_CANONICAL (attr))
14238 {
14239 DW_STRING (attr)
14240 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14241 &cu->objfile->objfile_obstack);
14242 DW_STRING_IS_CANONICAL (attr) = 1;
14243 }
14244 return DW_STRING (attr);
14245 }
14246
14247 /* Return the die that this die in an extension of, or NULL if there
14248 is none. *EXT_CU is the CU containing DIE on input, and the CU
14249 containing the return value on output. */
14250
14251 static struct die_info *
14252 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14253 {
14254 struct attribute *attr;
14255
14256 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14257 if (attr == NULL)
14258 return NULL;
14259
14260 return follow_die_ref (die, attr, ext_cu);
14261 }
14262
14263 /* Convert a DIE tag into its string name. */
14264
14265 static const char *
14266 dwarf_tag_name (unsigned tag)
14267 {
14268 const char *name = get_DW_TAG_name (tag);
14269
14270 if (name == NULL)
14271 return "DW_TAG_<unknown>";
14272
14273 return name;
14274 }
14275
14276 /* Convert a DWARF attribute code into its string name. */
14277
14278 static const char *
14279 dwarf_attr_name (unsigned attr)
14280 {
14281 const char *name;
14282
14283 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14284 if (attr == DW_AT_MIPS_fde)
14285 return "DW_AT_MIPS_fde";
14286 #else
14287 if (attr == DW_AT_HP_block_index)
14288 return "DW_AT_HP_block_index";
14289 #endif
14290
14291 name = get_DW_AT_name (attr);
14292
14293 if (name == NULL)
14294 return "DW_AT_<unknown>";
14295
14296 return name;
14297 }
14298
14299 /* Convert a DWARF value form code into its string name. */
14300
14301 static const char *
14302 dwarf_form_name (unsigned form)
14303 {
14304 const char *name = get_DW_FORM_name (form);
14305
14306 if (name == NULL)
14307 return "DW_FORM_<unknown>";
14308
14309 return name;
14310 }
14311
14312 static char *
14313 dwarf_bool_name (unsigned mybool)
14314 {
14315 if (mybool)
14316 return "TRUE";
14317 else
14318 return "FALSE";
14319 }
14320
14321 /* Convert a DWARF type code into its string name. */
14322
14323 static const char *
14324 dwarf_type_encoding_name (unsigned enc)
14325 {
14326 const char *name = get_DW_ATE_name (enc);
14327
14328 if (name == NULL)
14329 return "DW_ATE_<unknown>";
14330
14331 return name;
14332 }
14333
14334 static void
14335 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14336 {
14337 unsigned int i;
14338
14339 print_spaces (indent, f);
14340 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14341 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14342
14343 if (die->parent != NULL)
14344 {
14345 print_spaces (indent, f);
14346 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14347 die->parent->offset.sect_off);
14348 }
14349
14350 print_spaces (indent, f);
14351 fprintf_unfiltered (f, " has children: %s\n",
14352 dwarf_bool_name (die->child != NULL));
14353
14354 print_spaces (indent, f);
14355 fprintf_unfiltered (f, " attributes:\n");
14356
14357 for (i = 0; i < die->num_attrs; ++i)
14358 {
14359 print_spaces (indent, f);
14360 fprintf_unfiltered (f, " %s (%s) ",
14361 dwarf_attr_name (die->attrs[i].name),
14362 dwarf_form_name (die->attrs[i].form));
14363
14364 switch (die->attrs[i].form)
14365 {
14366 case DW_FORM_addr:
14367 case DW_FORM_GNU_addr_index:
14368 fprintf_unfiltered (f, "address: ");
14369 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14370 break;
14371 case DW_FORM_block2:
14372 case DW_FORM_block4:
14373 case DW_FORM_block:
14374 case DW_FORM_block1:
14375 fprintf_unfiltered (f, "block: size %d",
14376 DW_BLOCK (&die->attrs[i])->size);
14377 break;
14378 case DW_FORM_exprloc:
14379 fprintf_unfiltered (f, "expression: size %u",
14380 DW_BLOCK (&die->attrs[i])->size);
14381 break;
14382 case DW_FORM_ref_addr:
14383 fprintf_unfiltered (f, "ref address: ");
14384 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14385 break;
14386 case DW_FORM_ref1:
14387 case DW_FORM_ref2:
14388 case DW_FORM_ref4:
14389 case DW_FORM_ref8:
14390 case DW_FORM_ref_udata:
14391 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14392 (long) (DW_UNSND (&die->attrs[i])));
14393 break;
14394 case DW_FORM_data1:
14395 case DW_FORM_data2:
14396 case DW_FORM_data4:
14397 case DW_FORM_data8:
14398 case DW_FORM_udata:
14399 case DW_FORM_sdata:
14400 fprintf_unfiltered (f, "constant: %s",
14401 pulongest (DW_UNSND (&die->attrs[i])));
14402 break;
14403 case DW_FORM_sec_offset:
14404 fprintf_unfiltered (f, "section offset: %s",
14405 pulongest (DW_UNSND (&die->attrs[i])));
14406 break;
14407 case DW_FORM_ref_sig8:
14408 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14409 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14410 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14411 else
14412 fprintf_unfiltered (f, "signatured type, offset: unknown");
14413 break;
14414 case DW_FORM_string:
14415 case DW_FORM_strp:
14416 case DW_FORM_GNU_str_index:
14417 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14418 DW_STRING (&die->attrs[i])
14419 ? DW_STRING (&die->attrs[i]) : "",
14420 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14421 break;
14422 case DW_FORM_flag:
14423 if (DW_UNSND (&die->attrs[i]))
14424 fprintf_unfiltered (f, "flag: TRUE");
14425 else
14426 fprintf_unfiltered (f, "flag: FALSE");
14427 break;
14428 case DW_FORM_flag_present:
14429 fprintf_unfiltered (f, "flag: TRUE");
14430 break;
14431 case DW_FORM_indirect:
14432 /* The reader will have reduced the indirect form to
14433 the "base form" so this form should not occur. */
14434 fprintf_unfiltered (f,
14435 "unexpected attribute form: DW_FORM_indirect");
14436 break;
14437 default:
14438 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14439 die->attrs[i].form);
14440 break;
14441 }
14442 fprintf_unfiltered (f, "\n");
14443 }
14444 }
14445
14446 static void
14447 dump_die_for_error (struct die_info *die)
14448 {
14449 dump_die_shallow (gdb_stderr, 0, die);
14450 }
14451
14452 static void
14453 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14454 {
14455 int indent = level * 4;
14456
14457 gdb_assert (die != NULL);
14458
14459 if (level >= max_level)
14460 return;
14461
14462 dump_die_shallow (f, indent, die);
14463
14464 if (die->child != NULL)
14465 {
14466 print_spaces (indent, f);
14467 fprintf_unfiltered (f, " Children:");
14468 if (level + 1 < max_level)
14469 {
14470 fprintf_unfiltered (f, "\n");
14471 dump_die_1 (f, level + 1, max_level, die->child);
14472 }
14473 else
14474 {
14475 fprintf_unfiltered (f,
14476 " [not printed, max nesting level reached]\n");
14477 }
14478 }
14479
14480 if (die->sibling != NULL && level > 0)
14481 {
14482 dump_die_1 (f, level, max_level, die->sibling);
14483 }
14484 }
14485
14486 /* This is called from the pdie macro in gdbinit.in.
14487 It's not static so gcc will keep a copy callable from gdb. */
14488
14489 void
14490 dump_die (struct die_info *die, int max_level)
14491 {
14492 dump_die_1 (gdb_stdlog, 0, max_level, die);
14493 }
14494
14495 static void
14496 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14497 {
14498 void **slot;
14499
14500 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14501 INSERT);
14502
14503 *slot = die;
14504 }
14505
14506 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14507 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14508
14509 static int
14510 is_ref_attr (struct attribute *attr)
14511 {
14512 switch (attr->form)
14513 {
14514 case DW_FORM_ref_addr:
14515 case DW_FORM_ref1:
14516 case DW_FORM_ref2:
14517 case DW_FORM_ref4:
14518 case DW_FORM_ref8:
14519 case DW_FORM_ref_udata:
14520 return 1;
14521 default:
14522 return 0;
14523 }
14524 }
14525
14526 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14527 required kind. */
14528
14529 static sect_offset
14530 dwarf2_get_ref_die_offset (struct attribute *attr)
14531 {
14532 sect_offset retval = { DW_UNSND (attr) };
14533
14534 if (is_ref_attr (attr))
14535 return retval;
14536
14537 retval.sect_off = 0;
14538 complaint (&symfile_complaints,
14539 _("unsupported die ref attribute form: '%s'"),
14540 dwarf_form_name (attr->form));
14541 return retval;
14542 }
14543
14544 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14545 * the value held by the attribute is not constant. */
14546
14547 static LONGEST
14548 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14549 {
14550 if (attr->form == DW_FORM_sdata)
14551 return DW_SND (attr);
14552 else if (attr->form == DW_FORM_udata
14553 || attr->form == DW_FORM_data1
14554 || attr->form == DW_FORM_data2
14555 || attr->form == DW_FORM_data4
14556 || attr->form == DW_FORM_data8)
14557 return DW_UNSND (attr);
14558 else
14559 {
14560 complaint (&symfile_complaints,
14561 _("Attribute value is not a constant (%s)"),
14562 dwarf_form_name (attr->form));
14563 return default_value;
14564 }
14565 }
14566
14567 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14568 unit and add it to our queue.
14569 The result is non-zero if PER_CU was queued, otherwise the result is zero
14570 meaning either PER_CU is already queued or it is already loaded. */
14571
14572 static int
14573 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14574 struct dwarf2_per_cu_data *per_cu)
14575 {
14576 /* We may arrive here during partial symbol reading, if we need full
14577 DIEs to process an unusual case (e.g. template arguments). Do
14578 not queue PER_CU, just tell our caller to load its DIEs. */
14579 if (dwarf2_per_objfile->reading_partial_symbols)
14580 {
14581 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14582 return 1;
14583 return 0;
14584 }
14585
14586 /* Mark the dependence relation so that we don't flush PER_CU
14587 too early. */
14588 dwarf2_add_dependence (this_cu, per_cu);
14589
14590 /* If it's already on the queue, we have nothing to do. */
14591 if (per_cu->queued)
14592 return 0;
14593
14594 /* If the compilation unit is already loaded, just mark it as
14595 used. */
14596 if (per_cu->cu != NULL)
14597 {
14598 per_cu->cu->last_used = 0;
14599 return 0;
14600 }
14601
14602 /* Add it to the queue. */
14603 queue_comp_unit (per_cu);
14604
14605 return 1;
14606 }
14607
14608 /* Follow reference or signature attribute ATTR of SRC_DIE.
14609 On entry *REF_CU is the CU of SRC_DIE.
14610 On exit *REF_CU is the CU of the result. */
14611
14612 static struct die_info *
14613 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14614 struct dwarf2_cu **ref_cu)
14615 {
14616 struct die_info *die;
14617
14618 if (is_ref_attr (attr))
14619 die = follow_die_ref (src_die, attr, ref_cu);
14620 else if (attr->form == DW_FORM_ref_sig8)
14621 die = follow_die_sig (src_die, attr, ref_cu);
14622 else
14623 {
14624 dump_die_for_error (src_die);
14625 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14626 (*ref_cu)->objfile->name);
14627 }
14628
14629 return die;
14630 }
14631
14632 /* Follow reference OFFSET.
14633 On entry *REF_CU is the CU of the source die referencing OFFSET.
14634 On exit *REF_CU is the CU of the result.
14635 Returns NULL if OFFSET is invalid. */
14636
14637 static struct die_info *
14638 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14639 {
14640 struct die_info temp_die;
14641 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14642
14643 gdb_assert (cu->per_cu != NULL);
14644
14645 target_cu = cu;
14646
14647 if (cu->per_cu->is_debug_types)
14648 {
14649 /* .debug_types CUs cannot reference anything outside their CU.
14650 If they need to, they have to reference a signatured type via
14651 DW_FORM_ref_sig8. */
14652 if (! offset_in_cu_p (&cu->header, offset))
14653 return NULL;
14654 }
14655 else if (! offset_in_cu_p (&cu->header, offset))
14656 {
14657 struct dwarf2_per_cu_data *per_cu;
14658
14659 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14660
14661 /* If necessary, add it to the queue and load its DIEs. */
14662 if (maybe_queue_comp_unit (cu, per_cu))
14663 load_full_comp_unit (per_cu);
14664
14665 target_cu = per_cu->cu;
14666 }
14667 else if (cu->dies == NULL)
14668 {
14669 /* We're loading full DIEs during partial symbol reading. */
14670 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14671 load_full_comp_unit (cu->per_cu);
14672 }
14673
14674 *ref_cu = target_cu;
14675 temp_die.offset = offset;
14676 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14677 }
14678
14679 /* Follow reference attribute ATTR of SRC_DIE.
14680 On entry *REF_CU is the CU of SRC_DIE.
14681 On exit *REF_CU is the CU of the result. */
14682
14683 static struct die_info *
14684 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14685 struct dwarf2_cu **ref_cu)
14686 {
14687 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14688 struct dwarf2_cu *cu = *ref_cu;
14689 struct die_info *die;
14690
14691 die = follow_die_offset (offset, ref_cu);
14692 if (!die)
14693 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14694 "at 0x%x [in module %s]"),
14695 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14696
14697 return die;
14698 }
14699
14700 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14701 Returned value is intended for DW_OP_call*. Returned
14702 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14703
14704 struct dwarf2_locexpr_baton
14705 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14706 struct dwarf2_per_cu_data *per_cu,
14707 CORE_ADDR (*get_frame_pc) (void *baton),
14708 void *baton)
14709 {
14710 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14711 struct dwarf2_cu *cu;
14712 struct die_info *die;
14713 struct attribute *attr;
14714 struct dwarf2_locexpr_baton retval;
14715
14716 dw2_setup (per_cu->objfile);
14717
14718 if (per_cu->cu == NULL)
14719 load_cu (per_cu);
14720 cu = per_cu->cu;
14721
14722 die = follow_die_offset (offset, &cu);
14723 if (!die)
14724 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14725 offset.sect_off, per_cu->objfile->name);
14726
14727 attr = dwarf2_attr (die, DW_AT_location, cu);
14728 if (!attr)
14729 {
14730 /* DWARF: "If there is no such attribute, then there is no effect.".
14731 DATA is ignored if SIZE is 0. */
14732
14733 retval.data = NULL;
14734 retval.size = 0;
14735 }
14736 else if (attr_form_is_section_offset (attr))
14737 {
14738 struct dwarf2_loclist_baton loclist_baton;
14739 CORE_ADDR pc = (*get_frame_pc) (baton);
14740 size_t size;
14741
14742 fill_in_loclist_baton (cu, &loclist_baton, attr);
14743
14744 retval.data = dwarf2_find_location_expression (&loclist_baton,
14745 &size, pc);
14746 retval.size = size;
14747 }
14748 else
14749 {
14750 if (!attr_form_is_block (attr))
14751 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14752 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14753 offset.sect_off, per_cu->objfile->name);
14754
14755 retval.data = DW_BLOCK (attr)->data;
14756 retval.size = DW_BLOCK (attr)->size;
14757 }
14758 retval.per_cu = cu->per_cu;
14759
14760 age_cached_comp_units ();
14761
14762 return retval;
14763 }
14764
14765 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14766 PER_CU. */
14767
14768 struct type *
14769 dwarf2_get_die_type (cu_offset die_offset,
14770 struct dwarf2_per_cu_data *per_cu)
14771 {
14772 sect_offset die_offset_sect;
14773
14774 dw2_setup (per_cu->objfile);
14775
14776 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14777 return get_die_type_at_offset (die_offset_sect, per_cu);
14778 }
14779
14780 /* Follow the signature attribute ATTR in SRC_DIE.
14781 On entry *REF_CU is the CU of SRC_DIE.
14782 On exit *REF_CU is the CU of the result. */
14783
14784 static struct die_info *
14785 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14786 struct dwarf2_cu **ref_cu)
14787 {
14788 struct objfile *objfile = (*ref_cu)->objfile;
14789 struct die_info temp_die;
14790 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14791 struct dwarf2_cu *sig_cu;
14792 struct die_info *die;
14793
14794 /* sig_type will be NULL if the signatured type is missing from
14795 the debug info. */
14796 if (sig_type == NULL)
14797 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14798 "at 0x%x [in module %s]"),
14799 src_die->offset.sect_off, objfile->name);
14800
14801 /* If necessary, add it to the queue and load its DIEs. */
14802
14803 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14804 read_signatured_type (sig_type);
14805
14806 gdb_assert (sig_type->per_cu.cu != NULL);
14807
14808 sig_cu = sig_type->per_cu.cu;
14809 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
14810 temp_die.offset = sig_type->type_offset_in_section;
14811 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14812 temp_die.offset.sect_off);
14813 if (die)
14814 {
14815 *ref_cu = sig_cu;
14816 return die;
14817 }
14818
14819 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14820 "from DIE at 0x%x [in module %s]"),
14821 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
14822 }
14823
14824 /* Given an offset of a signatured type, return its signatured_type. */
14825
14826 static struct signatured_type *
14827 lookup_signatured_type_at_offset (struct objfile *objfile,
14828 struct dwarf2_section_info *section,
14829 sect_offset offset)
14830 {
14831 gdb_byte *info_ptr = section->buffer + offset.sect_off;
14832 unsigned int length, initial_length_size;
14833 unsigned int sig_offset;
14834 struct signatured_type find_entry, *sig_type;
14835
14836 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14837 sig_offset = (initial_length_size
14838 + 2 /*version*/
14839 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14840 + 1 /*address_size*/);
14841 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14842 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14843
14844 /* This is only used to lookup previously recorded types.
14845 If we didn't find it, it's our bug. */
14846 gdb_assert (sig_type != NULL);
14847 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
14848
14849 return sig_type;
14850 }
14851
14852 /* Load the DIEs associated with type unit PER_CU into memory. */
14853
14854 static void
14855 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14856 {
14857 struct objfile *objfile = per_cu->objfile;
14858 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
14859 sect_offset offset = per_cu->offset;
14860 struct signatured_type *sig_type;
14861
14862 dwarf2_read_section (objfile, sect);
14863
14864 /* We have the section offset, but we need the signature to do the
14865 hash table lookup. */
14866 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14867 the signature to assert we found the right one.
14868 Ok, but it's a lot of work. We should simplify things so any needed
14869 assert doesn't require all this clumsiness. */
14870 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
14871
14872 gdb_assert (&sig_type->per_cu == per_cu);
14873 gdb_assert (sig_type->per_cu.cu == NULL);
14874
14875 read_signatured_type (sig_type);
14876
14877 gdb_assert (sig_type->per_cu.cu != NULL);
14878 }
14879
14880 /* die_reader_func for read_signatured_type.
14881 This is identical to load_full_comp_unit_reader,
14882 but is kept separate for now. */
14883
14884 static void
14885 read_signatured_type_reader (const struct die_reader_specs *reader,
14886 gdb_byte *info_ptr,
14887 struct die_info *comp_unit_die,
14888 int has_children,
14889 void *data)
14890 {
14891 struct dwarf2_cu *cu = reader->cu;
14892 struct attribute *attr;
14893
14894 gdb_assert (cu->die_hash == NULL);
14895 cu->die_hash =
14896 htab_create_alloc_ex (cu->header.length / 12,
14897 die_hash,
14898 die_eq,
14899 NULL,
14900 &cu->comp_unit_obstack,
14901 hashtab_obstack_allocate,
14902 dummy_obstack_deallocate);
14903
14904 if (has_children)
14905 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
14906 &info_ptr, comp_unit_die);
14907 cu->dies = comp_unit_die;
14908 /* comp_unit_die is not stored in die_hash, no need. */
14909
14910 /* We try not to read any attributes in this function, because not
14911 all CUs needed for references have been loaded yet, and symbol
14912 table processing isn't initialized. But we have to set the CU language,
14913 or we won't be able to build types correctly.
14914 Similarly, if we do not read the producer, we can not apply
14915 producer-specific interpretation. */
14916 prepare_one_comp_unit (cu, cu->dies);
14917 }
14918
14919 /* Read in a signatured type and build its CU and DIEs.
14920 If the type is a stub for the real type in a DWO file,
14921 read in the real type from the DWO file as well. */
14922
14923 static void
14924 read_signatured_type (struct signatured_type *sig_type)
14925 {
14926 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
14927
14928 gdb_assert (per_cu->is_debug_types);
14929 gdb_assert (per_cu->cu == NULL);
14930
14931 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
14932 }
14933
14934 /* Decode simple location descriptions.
14935 Given a pointer to a dwarf block that defines a location, compute
14936 the location and return the value.
14937
14938 NOTE drow/2003-11-18: This function is called in two situations
14939 now: for the address of static or global variables (partial symbols
14940 only) and for offsets into structures which are expected to be
14941 (more or less) constant. The partial symbol case should go away,
14942 and only the constant case should remain. That will let this
14943 function complain more accurately. A few special modes are allowed
14944 without complaint for global variables (for instance, global
14945 register values and thread-local values).
14946
14947 A location description containing no operations indicates that the
14948 object is optimized out. The return value is 0 for that case.
14949 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14950 callers will only want a very basic result and this can become a
14951 complaint.
14952
14953 Note that stack[0] is unused except as a default error return. */
14954
14955 static CORE_ADDR
14956 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14957 {
14958 struct objfile *objfile = cu->objfile;
14959 int i;
14960 int size = blk->size;
14961 gdb_byte *data = blk->data;
14962 CORE_ADDR stack[64];
14963 int stacki;
14964 unsigned int bytes_read, unsnd;
14965 gdb_byte op;
14966
14967 i = 0;
14968 stacki = 0;
14969 stack[stacki] = 0;
14970 stack[++stacki] = 0;
14971
14972 while (i < size)
14973 {
14974 op = data[i++];
14975 switch (op)
14976 {
14977 case DW_OP_lit0:
14978 case DW_OP_lit1:
14979 case DW_OP_lit2:
14980 case DW_OP_lit3:
14981 case DW_OP_lit4:
14982 case DW_OP_lit5:
14983 case DW_OP_lit6:
14984 case DW_OP_lit7:
14985 case DW_OP_lit8:
14986 case DW_OP_lit9:
14987 case DW_OP_lit10:
14988 case DW_OP_lit11:
14989 case DW_OP_lit12:
14990 case DW_OP_lit13:
14991 case DW_OP_lit14:
14992 case DW_OP_lit15:
14993 case DW_OP_lit16:
14994 case DW_OP_lit17:
14995 case DW_OP_lit18:
14996 case DW_OP_lit19:
14997 case DW_OP_lit20:
14998 case DW_OP_lit21:
14999 case DW_OP_lit22:
15000 case DW_OP_lit23:
15001 case DW_OP_lit24:
15002 case DW_OP_lit25:
15003 case DW_OP_lit26:
15004 case DW_OP_lit27:
15005 case DW_OP_lit28:
15006 case DW_OP_lit29:
15007 case DW_OP_lit30:
15008 case DW_OP_lit31:
15009 stack[++stacki] = op - DW_OP_lit0;
15010 break;
15011
15012 case DW_OP_reg0:
15013 case DW_OP_reg1:
15014 case DW_OP_reg2:
15015 case DW_OP_reg3:
15016 case DW_OP_reg4:
15017 case DW_OP_reg5:
15018 case DW_OP_reg6:
15019 case DW_OP_reg7:
15020 case DW_OP_reg8:
15021 case DW_OP_reg9:
15022 case DW_OP_reg10:
15023 case DW_OP_reg11:
15024 case DW_OP_reg12:
15025 case DW_OP_reg13:
15026 case DW_OP_reg14:
15027 case DW_OP_reg15:
15028 case DW_OP_reg16:
15029 case DW_OP_reg17:
15030 case DW_OP_reg18:
15031 case DW_OP_reg19:
15032 case DW_OP_reg20:
15033 case DW_OP_reg21:
15034 case DW_OP_reg22:
15035 case DW_OP_reg23:
15036 case DW_OP_reg24:
15037 case DW_OP_reg25:
15038 case DW_OP_reg26:
15039 case DW_OP_reg27:
15040 case DW_OP_reg28:
15041 case DW_OP_reg29:
15042 case DW_OP_reg30:
15043 case DW_OP_reg31:
15044 stack[++stacki] = op - DW_OP_reg0;
15045 if (i < size)
15046 dwarf2_complex_location_expr_complaint ();
15047 break;
15048
15049 case DW_OP_regx:
15050 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15051 i += bytes_read;
15052 stack[++stacki] = unsnd;
15053 if (i < size)
15054 dwarf2_complex_location_expr_complaint ();
15055 break;
15056
15057 case DW_OP_addr:
15058 stack[++stacki] = read_address (objfile->obfd, &data[i],
15059 cu, &bytes_read);
15060 i += bytes_read;
15061 break;
15062
15063 case DW_OP_const1u:
15064 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15065 i += 1;
15066 break;
15067
15068 case DW_OP_const1s:
15069 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15070 i += 1;
15071 break;
15072
15073 case DW_OP_const2u:
15074 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15075 i += 2;
15076 break;
15077
15078 case DW_OP_const2s:
15079 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15080 i += 2;
15081 break;
15082
15083 case DW_OP_const4u:
15084 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15085 i += 4;
15086 break;
15087
15088 case DW_OP_const4s:
15089 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15090 i += 4;
15091 break;
15092
15093 case DW_OP_const8u:
15094 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15095 i += 8;
15096 break;
15097
15098 case DW_OP_constu:
15099 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15100 &bytes_read);
15101 i += bytes_read;
15102 break;
15103
15104 case DW_OP_consts:
15105 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15106 i += bytes_read;
15107 break;
15108
15109 case DW_OP_dup:
15110 stack[stacki + 1] = stack[stacki];
15111 stacki++;
15112 break;
15113
15114 case DW_OP_plus:
15115 stack[stacki - 1] += stack[stacki];
15116 stacki--;
15117 break;
15118
15119 case DW_OP_plus_uconst:
15120 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15121 &bytes_read);
15122 i += bytes_read;
15123 break;
15124
15125 case DW_OP_minus:
15126 stack[stacki - 1] -= stack[stacki];
15127 stacki--;
15128 break;
15129
15130 case DW_OP_deref:
15131 /* If we're not the last op, then we definitely can't encode
15132 this using GDB's address_class enum. This is valid for partial
15133 global symbols, although the variable's address will be bogus
15134 in the psymtab. */
15135 if (i < size)
15136 dwarf2_complex_location_expr_complaint ();
15137 break;
15138
15139 case DW_OP_GNU_push_tls_address:
15140 /* The top of the stack has the offset from the beginning
15141 of the thread control block at which the variable is located. */
15142 /* Nothing should follow this operator, so the top of stack would
15143 be returned. */
15144 /* This is valid for partial global symbols, but the variable's
15145 address will be bogus in the psymtab. Make it always at least
15146 non-zero to not look as a variable garbage collected by linker
15147 which have DW_OP_addr 0. */
15148 if (i < size)
15149 dwarf2_complex_location_expr_complaint ();
15150 stack[stacki]++;
15151 break;
15152
15153 case DW_OP_GNU_uninit:
15154 break;
15155
15156 case DW_OP_GNU_addr_index:
15157 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15158 &bytes_read);
15159 i += bytes_read;
15160 break;
15161
15162 default:
15163 {
15164 const char *name = get_DW_OP_name (op);
15165
15166 if (name)
15167 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15168 name);
15169 else
15170 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15171 op);
15172 }
15173
15174 return (stack[stacki]);
15175 }
15176
15177 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15178 outside of the allocated space. Also enforce minimum>0. */
15179 if (stacki >= ARRAY_SIZE (stack) - 1)
15180 {
15181 complaint (&symfile_complaints,
15182 _("location description stack overflow"));
15183 return 0;
15184 }
15185
15186 if (stacki <= 0)
15187 {
15188 complaint (&symfile_complaints,
15189 _("location description stack underflow"));
15190 return 0;
15191 }
15192 }
15193 return (stack[stacki]);
15194 }
15195
15196 /* memory allocation interface */
15197
15198 static struct dwarf_block *
15199 dwarf_alloc_block (struct dwarf2_cu *cu)
15200 {
15201 struct dwarf_block *blk;
15202
15203 blk = (struct dwarf_block *)
15204 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15205 return (blk);
15206 }
15207
15208 static struct abbrev_info *
15209 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15210 {
15211 struct abbrev_info *abbrev;
15212
15213 abbrev = (struct abbrev_info *)
15214 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15215 memset (abbrev, 0, sizeof (struct abbrev_info));
15216 return (abbrev);
15217 }
15218
15219 static struct die_info *
15220 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15221 {
15222 struct die_info *die;
15223 size_t size = sizeof (struct die_info);
15224
15225 if (num_attrs > 1)
15226 size += (num_attrs - 1) * sizeof (struct attribute);
15227
15228 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15229 memset (die, 0, sizeof (struct die_info));
15230 return (die);
15231 }
15232
15233 \f
15234 /* Macro support. */
15235
15236 /* Return the full name of file number I in *LH's file name table.
15237 Use COMP_DIR as the name of the current directory of the
15238 compilation. The result is allocated using xmalloc; the caller is
15239 responsible for freeing it. */
15240 static char *
15241 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15242 {
15243 /* Is the file number a valid index into the line header's file name
15244 table? Remember that file numbers start with one, not zero. */
15245 if (1 <= file && file <= lh->num_file_names)
15246 {
15247 struct file_entry *fe = &lh->file_names[file - 1];
15248
15249 if (IS_ABSOLUTE_PATH (fe->name))
15250 return xstrdup (fe->name);
15251 else
15252 {
15253 const char *dir;
15254 int dir_len;
15255 char *full_name;
15256
15257 if (fe->dir_index)
15258 dir = lh->include_dirs[fe->dir_index - 1];
15259 else
15260 dir = comp_dir;
15261
15262 if (dir)
15263 {
15264 dir_len = strlen (dir);
15265 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15266 strcpy (full_name, dir);
15267 full_name[dir_len] = '/';
15268 strcpy (full_name + dir_len + 1, fe->name);
15269 return full_name;
15270 }
15271 else
15272 return xstrdup (fe->name);
15273 }
15274 }
15275 else
15276 {
15277 /* The compiler produced a bogus file number. We can at least
15278 record the macro definitions made in the file, even if we
15279 won't be able to find the file by name. */
15280 char fake_name[80];
15281
15282 sprintf (fake_name, "<bad macro file number %d>", file);
15283
15284 complaint (&symfile_complaints,
15285 _("bad file number in macro information (%d)"),
15286 file);
15287
15288 return xstrdup (fake_name);
15289 }
15290 }
15291
15292
15293 static struct macro_source_file *
15294 macro_start_file (int file, int line,
15295 struct macro_source_file *current_file,
15296 const char *comp_dir,
15297 struct line_header *lh, struct objfile *objfile)
15298 {
15299 /* The full name of this source file. */
15300 char *full_name = file_full_name (file, lh, comp_dir);
15301
15302 /* We don't create a macro table for this compilation unit
15303 at all until we actually get a filename. */
15304 if (! pending_macros)
15305 pending_macros = new_macro_table (&objfile->objfile_obstack,
15306 objfile->macro_cache);
15307
15308 if (! current_file)
15309 /* If we have no current file, then this must be the start_file
15310 directive for the compilation unit's main source file. */
15311 current_file = macro_set_main (pending_macros, full_name);
15312 else
15313 current_file = macro_include (current_file, line, full_name);
15314
15315 xfree (full_name);
15316
15317 return current_file;
15318 }
15319
15320
15321 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15322 followed by a null byte. */
15323 static char *
15324 copy_string (const char *buf, int len)
15325 {
15326 char *s = xmalloc (len + 1);
15327
15328 memcpy (s, buf, len);
15329 s[len] = '\0';
15330 return s;
15331 }
15332
15333
15334 static const char *
15335 consume_improper_spaces (const char *p, const char *body)
15336 {
15337 if (*p == ' ')
15338 {
15339 complaint (&symfile_complaints,
15340 _("macro definition contains spaces "
15341 "in formal argument list:\n`%s'"),
15342 body);
15343
15344 while (*p == ' ')
15345 p++;
15346 }
15347
15348 return p;
15349 }
15350
15351
15352 static void
15353 parse_macro_definition (struct macro_source_file *file, int line,
15354 const char *body)
15355 {
15356 const char *p;
15357
15358 /* The body string takes one of two forms. For object-like macro
15359 definitions, it should be:
15360
15361 <macro name> " " <definition>
15362
15363 For function-like macro definitions, it should be:
15364
15365 <macro name> "() " <definition>
15366 or
15367 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15368
15369 Spaces may appear only where explicitly indicated, and in the
15370 <definition>.
15371
15372 The Dwarf 2 spec says that an object-like macro's name is always
15373 followed by a space, but versions of GCC around March 2002 omit
15374 the space when the macro's definition is the empty string.
15375
15376 The Dwarf 2 spec says that there should be no spaces between the
15377 formal arguments in a function-like macro's formal argument list,
15378 but versions of GCC around March 2002 include spaces after the
15379 commas. */
15380
15381
15382 /* Find the extent of the macro name. The macro name is terminated
15383 by either a space or null character (for an object-like macro) or
15384 an opening paren (for a function-like macro). */
15385 for (p = body; *p; p++)
15386 if (*p == ' ' || *p == '(')
15387 break;
15388
15389 if (*p == ' ' || *p == '\0')
15390 {
15391 /* It's an object-like macro. */
15392 int name_len = p - body;
15393 char *name = copy_string (body, name_len);
15394 const char *replacement;
15395
15396 if (*p == ' ')
15397 replacement = body + name_len + 1;
15398 else
15399 {
15400 dwarf2_macro_malformed_definition_complaint (body);
15401 replacement = body + name_len;
15402 }
15403
15404 macro_define_object (file, line, name, replacement);
15405
15406 xfree (name);
15407 }
15408 else if (*p == '(')
15409 {
15410 /* It's a function-like macro. */
15411 char *name = copy_string (body, p - body);
15412 int argc = 0;
15413 int argv_size = 1;
15414 char **argv = xmalloc (argv_size * sizeof (*argv));
15415
15416 p++;
15417
15418 p = consume_improper_spaces (p, body);
15419
15420 /* Parse the formal argument list. */
15421 while (*p && *p != ')')
15422 {
15423 /* Find the extent of the current argument name. */
15424 const char *arg_start = p;
15425
15426 while (*p && *p != ',' && *p != ')' && *p != ' ')
15427 p++;
15428
15429 if (! *p || p == arg_start)
15430 dwarf2_macro_malformed_definition_complaint (body);
15431 else
15432 {
15433 /* Make sure argv has room for the new argument. */
15434 if (argc >= argv_size)
15435 {
15436 argv_size *= 2;
15437 argv = xrealloc (argv, argv_size * sizeof (*argv));
15438 }
15439
15440 argv[argc++] = copy_string (arg_start, p - arg_start);
15441 }
15442
15443 p = consume_improper_spaces (p, body);
15444
15445 /* Consume the comma, if present. */
15446 if (*p == ',')
15447 {
15448 p++;
15449
15450 p = consume_improper_spaces (p, body);
15451 }
15452 }
15453
15454 if (*p == ')')
15455 {
15456 p++;
15457
15458 if (*p == ' ')
15459 /* Perfectly formed definition, no complaints. */
15460 macro_define_function (file, line, name,
15461 argc, (const char **) argv,
15462 p + 1);
15463 else if (*p == '\0')
15464 {
15465 /* Complain, but do define it. */
15466 dwarf2_macro_malformed_definition_complaint (body);
15467 macro_define_function (file, line, name,
15468 argc, (const char **) argv,
15469 p);
15470 }
15471 else
15472 /* Just complain. */
15473 dwarf2_macro_malformed_definition_complaint (body);
15474 }
15475 else
15476 /* Just complain. */
15477 dwarf2_macro_malformed_definition_complaint (body);
15478
15479 xfree (name);
15480 {
15481 int i;
15482
15483 for (i = 0; i < argc; i++)
15484 xfree (argv[i]);
15485 }
15486 xfree (argv);
15487 }
15488 else
15489 dwarf2_macro_malformed_definition_complaint (body);
15490 }
15491
15492 /* Skip some bytes from BYTES according to the form given in FORM.
15493 Returns the new pointer. */
15494
15495 static gdb_byte *
15496 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15497 enum dwarf_form form,
15498 unsigned int offset_size,
15499 struct dwarf2_section_info *section)
15500 {
15501 unsigned int bytes_read;
15502
15503 switch (form)
15504 {
15505 case DW_FORM_data1:
15506 case DW_FORM_flag:
15507 ++bytes;
15508 break;
15509
15510 case DW_FORM_data2:
15511 bytes += 2;
15512 break;
15513
15514 case DW_FORM_data4:
15515 bytes += 4;
15516 break;
15517
15518 case DW_FORM_data8:
15519 bytes += 8;
15520 break;
15521
15522 case DW_FORM_string:
15523 read_direct_string (abfd, bytes, &bytes_read);
15524 bytes += bytes_read;
15525 break;
15526
15527 case DW_FORM_sec_offset:
15528 case DW_FORM_strp:
15529 bytes += offset_size;
15530 break;
15531
15532 case DW_FORM_block:
15533 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15534 bytes += bytes_read;
15535 break;
15536
15537 case DW_FORM_block1:
15538 bytes += 1 + read_1_byte (abfd, bytes);
15539 break;
15540 case DW_FORM_block2:
15541 bytes += 2 + read_2_bytes (abfd, bytes);
15542 break;
15543 case DW_FORM_block4:
15544 bytes += 4 + read_4_bytes (abfd, bytes);
15545 break;
15546
15547 case DW_FORM_sdata:
15548 case DW_FORM_udata:
15549 case DW_FORM_GNU_addr_index:
15550 case DW_FORM_GNU_str_index:
15551 bytes = skip_leb128 (abfd, bytes);
15552 break;
15553
15554 default:
15555 {
15556 complain:
15557 complaint (&symfile_complaints,
15558 _("invalid form 0x%x in `%s'"),
15559 form,
15560 section->asection->name);
15561 return NULL;
15562 }
15563 }
15564
15565 return bytes;
15566 }
15567
15568 /* A helper for dwarf_decode_macros that handles skipping an unknown
15569 opcode. Returns an updated pointer to the macro data buffer; or,
15570 on error, issues a complaint and returns NULL. */
15571
15572 static gdb_byte *
15573 skip_unknown_opcode (unsigned int opcode,
15574 gdb_byte **opcode_definitions,
15575 gdb_byte *mac_ptr,
15576 bfd *abfd,
15577 unsigned int offset_size,
15578 struct dwarf2_section_info *section)
15579 {
15580 unsigned int bytes_read, i;
15581 unsigned long arg;
15582 gdb_byte *defn;
15583
15584 if (opcode_definitions[opcode] == NULL)
15585 {
15586 complaint (&symfile_complaints,
15587 _("unrecognized DW_MACFINO opcode 0x%x"),
15588 opcode);
15589 return NULL;
15590 }
15591
15592 defn = opcode_definitions[opcode];
15593 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15594 defn += bytes_read;
15595
15596 for (i = 0; i < arg; ++i)
15597 {
15598 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15599 if (mac_ptr == NULL)
15600 {
15601 /* skip_form_bytes already issued the complaint. */
15602 return NULL;
15603 }
15604 }
15605
15606 return mac_ptr;
15607 }
15608
15609 /* A helper function which parses the header of a macro section.
15610 If the macro section is the extended (for now called "GNU") type,
15611 then this updates *OFFSET_SIZE. Returns a pointer to just after
15612 the header, or issues a complaint and returns NULL on error. */
15613
15614 static gdb_byte *
15615 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15616 bfd *abfd,
15617 gdb_byte *mac_ptr,
15618 unsigned int *offset_size,
15619 int section_is_gnu)
15620 {
15621 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15622
15623 if (section_is_gnu)
15624 {
15625 unsigned int version, flags;
15626
15627 version = read_2_bytes (abfd, mac_ptr);
15628 if (version != 4)
15629 {
15630 complaint (&symfile_complaints,
15631 _("unrecognized version `%d' in .debug_macro section"),
15632 version);
15633 return NULL;
15634 }
15635 mac_ptr += 2;
15636
15637 flags = read_1_byte (abfd, mac_ptr);
15638 ++mac_ptr;
15639 *offset_size = (flags & 1) ? 8 : 4;
15640
15641 if ((flags & 2) != 0)
15642 /* We don't need the line table offset. */
15643 mac_ptr += *offset_size;
15644
15645 /* Vendor opcode descriptions. */
15646 if ((flags & 4) != 0)
15647 {
15648 unsigned int i, count;
15649
15650 count = read_1_byte (abfd, mac_ptr);
15651 ++mac_ptr;
15652 for (i = 0; i < count; ++i)
15653 {
15654 unsigned int opcode, bytes_read;
15655 unsigned long arg;
15656
15657 opcode = read_1_byte (abfd, mac_ptr);
15658 ++mac_ptr;
15659 opcode_definitions[opcode] = mac_ptr;
15660 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15661 mac_ptr += bytes_read;
15662 mac_ptr += arg;
15663 }
15664 }
15665 }
15666
15667 return mac_ptr;
15668 }
15669
15670 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15671 including DW_MACRO_GNU_transparent_include. */
15672
15673 static void
15674 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15675 struct macro_source_file *current_file,
15676 struct line_header *lh, char *comp_dir,
15677 struct dwarf2_section_info *section,
15678 int section_is_gnu,
15679 unsigned int offset_size,
15680 struct objfile *objfile,
15681 htab_t include_hash)
15682 {
15683 enum dwarf_macro_record_type macinfo_type;
15684 int at_commandline;
15685 gdb_byte *opcode_definitions[256];
15686
15687 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15688 &offset_size, section_is_gnu);
15689 if (mac_ptr == NULL)
15690 {
15691 /* We already issued a complaint. */
15692 return;
15693 }
15694
15695 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15696 GDB is still reading the definitions from command line. First
15697 DW_MACINFO_start_file will need to be ignored as it was already executed
15698 to create CURRENT_FILE for the main source holding also the command line
15699 definitions. On first met DW_MACINFO_start_file this flag is reset to
15700 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15701
15702 at_commandline = 1;
15703
15704 do
15705 {
15706 /* Do we at least have room for a macinfo type byte? */
15707 if (mac_ptr >= mac_end)
15708 {
15709 dwarf2_macros_too_long_complaint (section);
15710 break;
15711 }
15712
15713 macinfo_type = read_1_byte (abfd, mac_ptr);
15714 mac_ptr++;
15715
15716 /* Note that we rely on the fact that the corresponding GNU and
15717 DWARF constants are the same. */
15718 switch (macinfo_type)
15719 {
15720 /* A zero macinfo type indicates the end of the macro
15721 information. */
15722 case 0:
15723 break;
15724
15725 case DW_MACRO_GNU_define:
15726 case DW_MACRO_GNU_undef:
15727 case DW_MACRO_GNU_define_indirect:
15728 case DW_MACRO_GNU_undef_indirect:
15729 {
15730 unsigned int bytes_read;
15731 int line;
15732 char *body;
15733 int is_define;
15734
15735 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15736 mac_ptr += bytes_read;
15737
15738 if (macinfo_type == DW_MACRO_GNU_define
15739 || macinfo_type == DW_MACRO_GNU_undef)
15740 {
15741 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15742 mac_ptr += bytes_read;
15743 }
15744 else
15745 {
15746 LONGEST str_offset;
15747
15748 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15749 mac_ptr += offset_size;
15750
15751 body = read_indirect_string_at_offset (abfd, str_offset);
15752 }
15753
15754 is_define = (macinfo_type == DW_MACRO_GNU_define
15755 || macinfo_type == DW_MACRO_GNU_define_indirect);
15756 if (! current_file)
15757 {
15758 /* DWARF violation as no main source is present. */
15759 complaint (&symfile_complaints,
15760 _("debug info with no main source gives macro %s "
15761 "on line %d: %s"),
15762 is_define ? _("definition") : _("undefinition"),
15763 line, body);
15764 break;
15765 }
15766 if ((line == 0 && !at_commandline)
15767 || (line != 0 && at_commandline))
15768 complaint (&symfile_complaints,
15769 _("debug info gives %s macro %s with %s line %d: %s"),
15770 at_commandline ? _("command-line") : _("in-file"),
15771 is_define ? _("definition") : _("undefinition"),
15772 line == 0 ? _("zero") : _("non-zero"), line, body);
15773
15774 if (is_define)
15775 parse_macro_definition (current_file, line, body);
15776 else
15777 {
15778 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15779 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15780 macro_undef (current_file, line, body);
15781 }
15782 }
15783 break;
15784
15785 case DW_MACRO_GNU_start_file:
15786 {
15787 unsigned int bytes_read;
15788 int line, file;
15789
15790 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15791 mac_ptr += bytes_read;
15792 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15793 mac_ptr += bytes_read;
15794
15795 if ((line == 0 && !at_commandline)
15796 || (line != 0 && at_commandline))
15797 complaint (&symfile_complaints,
15798 _("debug info gives source %d included "
15799 "from %s at %s line %d"),
15800 file, at_commandline ? _("command-line") : _("file"),
15801 line == 0 ? _("zero") : _("non-zero"), line);
15802
15803 if (at_commandline)
15804 {
15805 /* This DW_MACRO_GNU_start_file was executed in the
15806 pass one. */
15807 at_commandline = 0;
15808 }
15809 else
15810 current_file = macro_start_file (file, line,
15811 current_file, comp_dir,
15812 lh, objfile);
15813 }
15814 break;
15815
15816 case DW_MACRO_GNU_end_file:
15817 if (! current_file)
15818 complaint (&symfile_complaints,
15819 _("macro debug info has an unmatched "
15820 "`close_file' directive"));
15821 else
15822 {
15823 current_file = current_file->included_by;
15824 if (! current_file)
15825 {
15826 enum dwarf_macro_record_type next_type;
15827
15828 /* GCC circa March 2002 doesn't produce the zero
15829 type byte marking the end of the compilation
15830 unit. Complain if it's not there, but exit no
15831 matter what. */
15832
15833 /* Do we at least have room for a macinfo type byte? */
15834 if (mac_ptr >= mac_end)
15835 {
15836 dwarf2_macros_too_long_complaint (section);
15837 return;
15838 }
15839
15840 /* We don't increment mac_ptr here, so this is just
15841 a look-ahead. */
15842 next_type = read_1_byte (abfd, mac_ptr);
15843 if (next_type != 0)
15844 complaint (&symfile_complaints,
15845 _("no terminating 0-type entry for "
15846 "macros in `.debug_macinfo' section"));
15847
15848 return;
15849 }
15850 }
15851 break;
15852
15853 case DW_MACRO_GNU_transparent_include:
15854 {
15855 LONGEST offset;
15856 void **slot;
15857
15858 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15859 mac_ptr += offset_size;
15860
15861 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15862 if (*slot != NULL)
15863 {
15864 /* This has actually happened; see
15865 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15866 complaint (&symfile_complaints,
15867 _("recursive DW_MACRO_GNU_transparent_include in "
15868 ".debug_macro section"));
15869 }
15870 else
15871 {
15872 *slot = mac_ptr;
15873
15874 dwarf_decode_macro_bytes (abfd,
15875 section->buffer + offset,
15876 mac_end, current_file,
15877 lh, comp_dir,
15878 section, section_is_gnu,
15879 offset_size, objfile, include_hash);
15880
15881 htab_remove_elt (include_hash, mac_ptr);
15882 }
15883 }
15884 break;
15885
15886 case DW_MACINFO_vendor_ext:
15887 if (!section_is_gnu)
15888 {
15889 unsigned int bytes_read;
15890 int constant;
15891
15892 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15893 mac_ptr += bytes_read;
15894 read_direct_string (abfd, mac_ptr, &bytes_read);
15895 mac_ptr += bytes_read;
15896
15897 /* We don't recognize any vendor extensions. */
15898 break;
15899 }
15900 /* FALLTHROUGH */
15901
15902 default:
15903 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15904 mac_ptr, abfd, offset_size,
15905 section);
15906 if (mac_ptr == NULL)
15907 return;
15908 break;
15909 }
15910 } while (macinfo_type != 0);
15911 }
15912
15913 static void
15914 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15915 char *comp_dir, bfd *abfd,
15916 struct dwarf2_cu *cu,
15917 struct dwarf2_section_info *section,
15918 int section_is_gnu, const char *section_name)
15919 {
15920 struct objfile *objfile = dwarf2_per_objfile->objfile;
15921 gdb_byte *mac_ptr, *mac_end;
15922 struct macro_source_file *current_file = 0;
15923 enum dwarf_macro_record_type macinfo_type;
15924 unsigned int offset_size = cu->header.offset_size;
15925 gdb_byte *opcode_definitions[256];
15926 struct cleanup *cleanup;
15927 htab_t include_hash;
15928 void **slot;
15929
15930 dwarf2_read_section (objfile, section);
15931 if (section->buffer == NULL)
15932 {
15933 complaint (&symfile_complaints, _("missing %s section"), section_name);
15934 return;
15935 }
15936
15937 /* First pass: Find the name of the base filename.
15938 This filename is needed in order to process all macros whose definition
15939 (or undefinition) comes from the command line. These macros are defined
15940 before the first DW_MACINFO_start_file entry, and yet still need to be
15941 associated to the base file.
15942
15943 To determine the base file name, we scan the macro definitions until we
15944 reach the first DW_MACINFO_start_file entry. We then initialize
15945 CURRENT_FILE accordingly so that any macro definition found before the
15946 first DW_MACINFO_start_file can still be associated to the base file. */
15947
15948 mac_ptr = section->buffer + offset;
15949 mac_end = section->buffer + section->size;
15950
15951 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15952 &offset_size, section_is_gnu);
15953 if (mac_ptr == NULL)
15954 {
15955 /* We already issued a complaint. */
15956 return;
15957 }
15958
15959 do
15960 {
15961 /* Do we at least have room for a macinfo type byte? */
15962 if (mac_ptr >= mac_end)
15963 {
15964 /* Complaint is printed during the second pass as GDB will probably
15965 stop the first pass earlier upon finding
15966 DW_MACINFO_start_file. */
15967 break;
15968 }
15969
15970 macinfo_type = read_1_byte (abfd, mac_ptr);
15971 mac_ptr++;
15972
15973 /* Note that we rely on the fact that the corresponding GNU and
15974 DWARF constants are the same. */
15975 switch (macinfo_type)
15976 {
15977 /* A zero macinfo type indicates the end of the macro
15978 information. */
15979 case 0:
15980 break;
15981
15982 case DW_MACRO_GNU_define:
15983 case DW_MACRO_GNU_undef:
15984 /* Only skip the data by MAC_PTR. */
15985 {
15986 unsigned int bytes_read;
15987
15988 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15989 mac_ptr += bytes_read;
15990 read_direct_string (abfd, mac_ptr, &bytes_read);
15991 mac_ptr += bytes_read;
15992 }
15993 break;
15994
15995 case DW_MACRO_GNU_start_file:
15996 {
15997 unsigned int bytes_read;
15998 int line, file;
15999
16000 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16001 mac_ptr += bytes_read;
16002 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16003 mac_ptr += bytes_read;
16004
16005 current_file = macro_start_file (file, line, current_file,
16006 comp_dir, lh, objfile);
16007 }
16008 break;
16009
16010 case DW_MACRO_GNU_end_file:
16011 /* No data to skip by MAC_PTR. */
16012 break;
16013
16014 case DW_MACRO_GNU_define_indirect:
16015 case DW_MACRO_GNU_undef_indirect:
16016 {
16017 unsigned int bytes_read;
16018
16019 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16020 mac_ptr += bytes_read;
16021 mac_ptr += offset_size;
16022 }
16023 break;
16024
16025 case DW_MACRO_GNU_transparent_include:
16026 /* Note that, according to the spec, a transparent include
16027 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16028 skip this opcode. */
16029 mac_ptr += offset_size;
16030 break;
16031
16032 case DW_MACINFO_vendor_ext:
16033 /* Only skip the data by MAC_PTR. */
16034 if (!section_is_gnu)
16035 {
16036 unsigned int bytes_read;
16037
16038 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16039 mac_ptr += bytes_read;
16040 read_direct_string (abfd, mac_ptr, &bytes_read);
16041 mac_ptr += bytes_read;
16042 }
16043 /* FALLTHROUGH */
16044
16045 default:
16046 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16047 mac_ptr, abfd, offset_size,
16048 section);
16049 if (mac_ptr == NULL)
16050 return;
16051 break;
16052 }
16053 } while (macinfo_type != 0 && current_file == NULL);
16054
16055 /* Second pass: Process all entries.
16056
16057 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16058 command-line macro definitions/undefinitions. This flag is unset when we
16059 reach the first DW_MACINFO_start_file entry. */
16060
16061 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16062 NULL, xcalloc, xfree);
16063 cleanup = make_cleanup_htab_delete (include_hash);
16064 mac_ptr = section->buffer + offset;
16065 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16066 *slot = mac_ptr;
16067 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16068 current_file, lh, comp_dir, section, section_is_gnu,
16069 offset_size, objfile, include_hash);
16070 do_cleanups (cleanup);
16071 }
16072
16073 /* Check if the attribute's form is a DW_FORM_block*
16074 if so return true else false. */
16075
16076 static int
16077 attr_form_is_block (struct attribute *attr)
16078 {
16079 return (attr == NULL ? 0 :
16080 attr->form == DW_FORM_block1
16081 || attr->form == DW_FORM_block2
16082 || attr->form == DW_FORM_block4
16083 || attr->form == DW_FORM_block
16084 || attr->form == DW_FORM_exprloc);
16085 }
16086
16087 /* Return non-zero if ATTR's value is a section offset --- classes
16088 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16089 You may use DW_UNSND (attr) to retrieve such offsets.
16090
16091 Section 7.5.4, "Attribute Encodings", explains that no attribute
16092 may have a value that belongs to more than one of these classes; it
16093 would be ambiguous if we did, because we use the same forms for all
16094 of them. */
16095
16096 static int
16097 attr_form_is_section_offset (struct attribute *attr)
16098 {
16099 return (attr->form == DW_FORM_data4
16100 || attr->form == DW_FORM_data8
16101 || attr->form == DW_FORM_sec_offset);
16102 }
16103
16104 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16105 zero otherwise. When this function returns true, you can apply
16106 dwarf2_get_attr_constant_value to it.
16107
16108 However, note that for some attributes you must check
16109 attr_form_is_section_offset before using this test. DW_FORM_data4
16110 and DW_FORM_data8 are members of both the constant class, and of
16111 the classes that contain offsets into other debug sections
16112 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16113 that, if an attribute's can be either a constant or one of the
16114 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16115 taken as section offsets, not constants. */
16116
16117 static int
16118 attr_form_is_constant (struct attribute *attr)
16119 {
16120 switch (attr->form)
16121 {
16122 case DW_FORM_sdata:
16123 case DW_FORM_udata:
16124 case DW_FORM_data1:
16125 case DW_FORM_data2:
16126 case DW_FORM_data4:
16127 case DW_FORM_data8:
16128 return 1;
16129 default:
16130 return 0;
16131 }
16132 }
16133
16134 /* Return the .debug_loc section to use for CU.
16135 For DWO files use .debug_loc.dwo. */
16136
16137 static struct dwarf2_section_info *
16138 cu_debug_loc_section (struct dwarf2_cu *cu)
16139 {
16140 if (cu->dwo_unit)
16141 return &cu->dwo_unit->dwo_file->sections.loc;
16142 return &dwarf2_per_objfile->loc;
16143 }
16144
16145 /* A helper function that fills in a dwarf2_loclist_baton. */
16146
16147 static void
16148 fill_in_loclist_baton (struct dwarf2_cu *cu,
16149 struct dwarf2_loclist_baton *baton,
16150 struct attribute *attr)
16151 {
16152 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16153
16154 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16155
16156 baton->per_cu = cu->per_cu;
16157 gdb_assert (baton->per_cu);
16158 /* We don't know how long the location list is, but make sure we
16159 don't run off the edge of the section. */
16160 baton->size = section->size - DW_UNSND (attr);
16161 baton->data = section->buffer + DW_UNSND (attr);
16162 baton->base_address = cu->base_address;
16163 }
16164
16165 static void
16166 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16167 struct dwarf2_cu *cu)
16168 {
16169 struct objfile *objfile = dwarf2_per_objfile->objfile;
16170 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16171
16172 if (attr_form_is_section_offset (attr)
16173 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16174 the section. If so, fall through to the complaint in the
16175 other branch. */
16176 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16177 {
16178 struct dwarf2_loclist_baton *baton;
16179
16180 baton = obstack_alloc (&objfile->objfile_obstack,
16181 sizeof (struct dwarf2_loclist_baton));
16182
16183 fill_in_loclist_baton (cu, baton, attr);
16184
16185 if (cu->base_known == 0)
16186 complaint (&symfile_complaints,
16187 _("Location list used without "
16188 "specifying the CU base address."));
16189
16190 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16191 SYMBOL_LOCATION_BATON (sym) = baton;
16192 }
16193 else
16194 {
16195 struct dwarf2_locexpr_baton *baton;
16196
16197 baton = obstack_alloc (&objfile->objfile_obstack,
16198 sizeof (struct dwarf2_locexpr_baton));
16199 baton->per_cu = cu->per_cu;
16200 gdb_assert (baton->per_cu);
16201
16202 if (attr_form_is_block (attr))
16203 {
16204 /* Note that we're just copying the block's data pointer
16205 here, not the actual data. We're still pointing into the
16206 info_buffer for SYM's objfile; right now we never release
16207 that buffer, but when we do clean up properly this may
16208 need to change. */
16209 baton->size = DW_BLOCK (attr)->size;
16210 baton->data = DW_BLOCK (attr)->data;
16211 }
16212 else
16213 {
16214 dwarf2_invalid_attrib_class_complaint ("location description",
16215 SYMBOL_NATURAL_NAME (sym));
16216 baton->size = 0;
16217 }
16218
16219 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16220 SYMBOL_LOCATION_BATON (sym) = baton;
16221 }
16222 }
16223
16224 /* Return the OBJFILE associated with the compilation unit CU. If CU
16225 came from a separate debuginfo file, then the master objfile is
16226 returned. */
16227
16228 struct objfile *
16229 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16230 {
16231 struct objfile *objfile = per_cu->objfile;
16232
16233 /* Return the master objfile, so that we can report and look up the
16234 correct file containing this variable. */
16235 if (objfile->separate_debug_objfile_backlink)
16236 objfile = objfile->separate_debug_objfile_backlink;
16237
16238 return objfile;
16239 }
16240
16241 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16242 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16243 CU_HEADERP first. */
16244
16245 static const struct comp_unit_head *
16246 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16247 struct dwarf2_per_cu_data *per_cu)
16248 {
16249 struct objfile *objfile;
16250 struct dwarf2_per_objfile *per_objfile;
16251 gdb_byte *info_ptr;
16252
16253 if (per_cu->cu)
16254 return &per_cu->cu->header;
16255
16256 objfile = per_cu->objfile;
16257 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16258 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16259
16260 memset (cu_headerp, 0, sizeof (*cu_headerp));
16261 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16262
16263 return cu_headerp;
16264 }
16265
16266 /* Return the address size given in the compilation unit header for CU. */
16267
16268 int
16269 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16270 {
16271 struct comp_unit_head cu_header_local;
16272 const struct comp_unit_head *cu_headerp;
16273
16274 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16275
16276 return cu_headerp->addr_size;
16277 }
16278
16279 /* Return the offset size given in the compilation unit header for CU. */
16280
16281 int
16282 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16283 {
16284 struct comp_unit_head cu_header_local;
16285 const struct comp_unit_head *cu_headerp;
16286
16287 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16288
16289 return cu_headerp->offset_size;
16290 }
16291
16292 /* See its dwarf2loc.h declaration. */
16293
16294 int
16295 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16296 {
16297 struct comp_unit_head cu_header_local;
16298 const struct comp_unit_head *cu_headerp;
16299
16300 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16301
16302 if (cu_headerp->version == 2)
16303 return cu_headerp->addr_size;
16304 else
16305 return cu_headerp->offset_size;
16306 }
16307
16308 /* Return the text offset of the CU. The returned offset comes from
16309 this CU's objfile. If this objfile came from a separate debuginfo
16310 file, then the offset may be different from the corresponding
16311 offset in the parent objfile. */
16312
16313 CORE_ADDR
16314 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16315 {
16316 struct objfile *objfile = per_cu->objfile;
16317
16318 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16319 }
16320
16321 /* Locate the .debug_info compilation unit from CU's objfile which contains
16322 the DIE at OFFSET. Raises an error on failure. */
16323
16324 static struct dwarf2_per_cu_data *
16325 dwarf2_find_containing_comp_unit (sect_offset offset,
16326 struct objfile *objfile)
16327 {
16328 struct dwarf2_per_cu_data *this_cu;
16329 int low, high;
16330
16331 low = 0;
16332 high = dwarf2_per_objfile->n_comp_units - 1;
16333 while (high > low)
16334 {
16335 int mid = low + (high - low) / 2;
16336
16337 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16338 >= offset.sect_off)
16339 high = mid;
16340 else
16341 low = mid + 1;
16342 }
16343 gdb_assert (low == high);
16344 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16345 > offset.sect_off)
16346 {
16347 if (low == 0)
16348 error (_("Dwarf Error: could not find partial DIE containing "
16349 "offset 0x%lx [in module %s]"),
16350 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16351
16352 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16353 <= offset.sect_off);
16354 return dwarf2_per_objfile->all_comp_units[low-1];
16355 }
16356 else
16357 {
16358 this_cu = dwarf2_per_objfile->all_comp_units[low];
16359 if (low == dwarf2_per_objfile->n_comp_units - 1
16360 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16361 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16362 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16363 return this_cu;
16364 }
16365 }
16366
16367 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16368
16369 static void
16370 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16371 {
16372 memset (cu, 0, sizeof (*cu));
16373 per_cu->cu = cu;
16374 cu->per_cu = per_cu;
16375 cu->objfile = per_cu->objfile;
16376 obstack_init (&cu->comp_unit_obstack);
16377 }
16378
16379 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16380
16381 static void
16382 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
16383 {
16384 struct attribute *attr;
16385
16386 /* Set the language we're debugging. */
16387 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16388 if (attr)
16389 set_cu_language (DW_UNSND (attr), cu);
16390 else
16391 {
16392 cu->language = language_minimal;
16393 cu->language_defn = language_def (cu->language);
16394 }
16395
16396 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16397 if (attr)
16398 cu->producer = DW_STRING (attr);
16399 }
16400
16401 /* Release one cached compilation unit, CU. We unlink it from the tree
16402 of compilation units, but we don't remove it from the read_in_chain;
16403 the caller is responsible for that.
16404 NOTE: DATA is a void * because this function is also used as a
16405 cleanup routine. */
16406
16407 static void
16408 free_heap_comp_unit (void *data)
16409 {
16410 struct dwarf2_cu *cu = data;
16411
16412 gdb_assert (cu->per_cu != NULL);
16413 cu->per_cu->cu = NULL;
16414 cu->per_cu = NULL;
16415
16416 obstack_free (&cu->comp_unit_obstack, NULL);
16417
16418 xfree (cu);
16419 }
16420
16421 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16422 when we're finished with it. We can't free the pointer itself, but be
16423 sure to unlink it from the cache. Also release any associated storage. */
16424
16425 static void
16426 free_stack_comp_unit (void *data)
16427 {
16428 struct dwarf2_cu *cu = data;
16429
16430 gdb_assert (cu->per_cu != NULL);
16431 cu->per_cu->cu = NULL;
16432 cu->per_cu = NULL;
16433
16434 obstack_free (&cu->comp_unit_obstack, NULL);
16435 cu->partial_dies = NULL;
16436 }
16437
16438 /* Free all cached compilation units. */
16439
16440 static void
16441 free_cached_comp_units (void *data)
16442 {
16443 struct dwarf2_per_cu_data *per_cu, **last_chain;
16444
16445 per_cu = dwarf2_per_objfile->read_in_chain;
16446 last_chain = &dwarf2_per_objfile->read_in_chain;
16447 while (per_cu != NULL)
16448 {
16449 struct dwarf2_per_cu_data *next_cu;
16450
16451 next_cu = per_cu->cu->read_in_chain;
16452
16453 free_heap_comp_unit (per_cu->cu);
16454 *last_chain = next_cu;
16455
16456 per_cu = next_cu;
16457 }
16458 }
16459
16460 /* Increase the age counter on each cached compilation unit, and free
16461 any that are too old. */
16462
16463 static void
16464 age_cached_comp_units (void)
16465 {
16466 struct dwarf2_per_cu_data *per_cu, **last_chain;
16467
16468 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16469 per_cu = dwarf2_per_objfile->read_in_chain;
16470 while (per_cu != NULL)
16471 {
16472 per_cu->cu->last_used ++;
16473 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16474 dwarf2_mark (per_cu->cu);
16475 per_cu = per_cu->cu->read_in_chain;
16476 }
16477
16478 per_cu = dwarf2_per_objfile->read_in_chain;
16479 last_chain = &dwarf2_per_objfile->read_in_chain;
16480 while (per_cu != NULL)
16481 {
16482 struct dwarf2_per_cu_data *next_cu;
16483
16484 next_cu = per_cu->cu->read_in_chain;
16485
16486 if (!per_cu->cu->mark)
16487 {
16488 free_heap_comp_unit (per_cu->cu);
16489 *last_chain = next_cu;
16490 }
16491 else
16492 last_chain = &per_cu->cu->read_in_chain;
16493
16494 per_cu = next_cu;
16495 }
16496 }
16497
16498 /* Remove a single compilation unit from the cache. */
16499
16500 static void
16501 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16502 {
16503 struct dwarf2_per_cu_data *per_cu, **last_chain;
16504
16505 per_cu = dwarf2_per_objfile->read_in_chain;
16506 last_chain = &dwarf2_per_objfile->read_in_chain;
16507 while (per_cu != NULL)
16508 {
16509 struct dwarf2_per_cu_data *next_cu;
16510
16511 next_cu = per_cu->cu->read_in_chain;
16512
16513 if (per_cu == target_per_cu)
16514 {
16515 free_heap_comp_unit (per_cu->cu);
16516 per_cu->cu = NULL;
16517 *last_chain = next_cu;
16518 break;
16519 }
16520 else
16521 last_chain = &per_cu->cu->read_in_chain;
16522
16523 per_cu = next_cu;
16524 }
16525 }
16526
16527 /* Release all extra memory associated with OBJFILE. */
16528
16529 void
16530 dwarf2_free_objfile (struct objfile *objfile)
16531 {
16532 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16533
16534 if (dwarf2_per_objfile == NULL)
16535 return;
16536
16537 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16538 free_cached_comp_units (NULL);
16539
16540 if (dwarf2_per_objfile->quick_file_names_table)
16541 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16542
16543 /* Everything else should be on the objfile obstack. */
16544 }
16545
16546 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16547 We store these in a hash table separate from the DIEs, and preserve them
16548 when the DIEs are flushed out of cache.
16549
16550 The CU "per_cu" pointer is needed because offset alone is not enough to
16551 uniquely identify the type. A file may have multiple .debug_types sections,
16552 or the type may come from a DWO file. We have to use something in
16553 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16554 routine, get_die_type_at_offset, from outside this file, and thus won't
16555 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16556 of the objfile. */
16557
16558 struct dwarf2_per_cu_offset_and_type
16559 {
16560 const struct dwarf2_per_cu_data *per_cu;
16561 sect_offset offset;
16562 struct type *type;
16563 };
16564
16565 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16566
16567 static hashval_t
16568 per_cu_offset_and_type_hash (const void *item)
16569 {
16570 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16571
16572 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16573 }
16574
16575 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16576
16577 static int
16578 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16579 {
16580 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16581 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16582
16583 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16584 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16585 }
16586
16587 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16588 table if necessary. For convenience, return TYPE.
16589
16590 The DIEs reading must have careful ordering to:
16591 * Not cause infite loops trying to read in DIEs as a prerequisite for
16592 reading current DIE.
16593 * Not trying to dereference contents of still incompletely read in types
16594 while reading in other DIEs.
16595 * Enable referencing still incompletely read in types just by a pointer to
16596 the type without accessing its fields.
16597
16598 Therefore caller should follow these rules:
16599 * Try to fetch any prerequisite types we may need to build this DIE type
16600 before building the type and calling set_die_type.
16601 * After building type call set_die_type for current DIE as soon as
16602 possible before fetching more types to complete the current type.
16603 * Make the type as complete as possible before fetching more types. */
16604
16605 static struct type *
16606 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16607 {
16608 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16609 struct objfile *objfile = cu->objfile;
16610
16611 /* For Ada types, make sure that the gnat-specific data is always
16612 initialized (if not already set). There are a few types where
16613 we should not be doing so, because the type-specific area is
16614 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16615 where the type-specific area is used to store the floatformat).
16616 But this is not a problem, because the gnat-specific information
16617 is actually not needed for these types. */
16618 if (need_gnat_info (cu)
16619 && TYPE_CODE (type) != TYPE_CODE_FUNC
16620 && TYPE_CODE (type) != TYPE_CODE_FLT
16621 && !HAVE_GNAT_AUX_INFO (type))
16622 INIT_GNAT_SPECIFIC (type);
16623
16624 if (dwarf2_per_objfile->die_type_hash == NULL)
16625 {
16626 dwarf2_per_objfile->die_type_hash =
16627 htab_create_alloc_ex (127,
16628 per_cu_offset_and_type_hash,
16629 per_cu_offset_and_type_eq,
16630 NULL,
16631 &objfile->objfile_obstack,
16632 hashtab_obstack_allocate,
16633 dummy_obstack_deallocate);
16634 }
16635
16636 ofs.per_cu = cu->per_cu;
16637 ofs.offset = die->offset;
16638 ofs.type = type;
16639 slot = (struct dwarf2_per_cu_offset_and_type **)
16640 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16641 if (*slot)
16642 complaint (&symfile_complaints,
16643 _("A problem internal to GDB: DIE 0x%x has type already set"),
16644 die->offset.sect_off);
16645 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16646 **slot = ofs;
16647 return type;
16648 }
16649
16650 /* Look up the type for the die at OFFSET in the appropriate type_hash
16651 table, or return NULL if the die does not have a saved type. */
16652
16653 static struct type *
16654 get_die_type_at_offset (sect_offset offset,
16655 struct dwarf2_per_cu_data *per_cu)
16656 {
16657 struct dwarf2_per_cu_offset_and_type *slot, ofs;
16658
16659 if (dwarf2_per_objfile->die_type_hash == NULL)
16660 return NULL;
16661
16662 ofs.per_cu = per_cu;
16663 ofs.offset = offset;
16664 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
16665 if (slot)
16666 return slot->type;
16667 else
16668 return NULL;
16669 }
16670
16671 /* Look up the type for DIE in the appropriate type_hash table,
16672 or return NULL if DIE does not have a saved type. */
16673
16674 static struct type *
16675 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16676 {
16677 return get_die_type_at_offset (die->offset, cu->per_cu);
16678 }
16679
16680 /* Add a dependence relationship from CU to REF_PER_CU. */
16681
16682 static void
16683 dwarf2_add_dependence (struct dwarf2_cu *cu,
16684 struct dwarf2_per_cu_data *ref_per_cu)
16685 {
16686 void **slot;
16687
16688 if (cu->dependencies == NULL)
16689 cu->dependencies
16690 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16691 NULL, &cu->comp_unit_obstack,
16692 hashtab_obstack_allocate,
16693 dummy_obstack_deallocate);
16694
16695 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16696 if (*slot == NULL)
16697 *slot = ref_per_cu;
16698 }
16699
16700 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16701 Set the mark field in every compilation unit in the
16702 cache that we must keep because we are keeping CU. */
16703
16704 static int
16705 dwarf2_mark_helper (void **slot, void *data)
16706 {
16707 struct dwarf2_per_cu_data *per_cu;
16708
16709 per_cu = (struct dwarf2_per_cu_data *) *slot;
16710
16711 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16712 reading of the chain. As such dependencies remain valid it is not much
16713 useful to track and undo them during QUIT cleanups. */
16714 if (per_cu->cu == NULL)
16715 return 1;
16716
16717 if (per_cu->cu->mark)
16718 return 1;
16719 per_cu->cu->mark = 1;
16720
16721 if (per_cu->cu->dependencies != NULL)
16722 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16723
16724 return 1;
16725 }
16726
16727 /* Set the mark field in CU and in every other compilation unit in the
16728 cache that we must keep because we are keeping CU. */
16729
16730 static void
16731 dwarf2_mark (struct dwarf2_cu *cu)
16732 {
16733 if (cu->mark)
16734 return;
16735 cu->mark = 1;
16736 if (cu->dependencies != NULL)
16737 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16738 }
16739
16740 static void
16741 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16742 {
16743 while (per_cu)
16744 {
16745 per_cu->cu->mark = 0;
16746 per_cu = per_cu->cu->read_in_chain;
16747 }
16748 }
16749
16750 /* Trivial hash function for partial_die_info: the hash value of a DIE
16751 is its offset in .debug_info for this objfile. */
16752
16753 static hashval_t
16754 partial_die_hash (const void *item)
16755 {
16756 const struct partial_die_info *part_die = item;
16757
16758 return part_die->offset.sect_off;
16759 }
16760
16761 /* Trivial comparison function for partial_die_info structures: two DIEs
16762 are equal if they have the same offset. */
16763
16764 static int
16765 partial_die_eq (const void *item_lhs, const void *item_rhs)
16766 {
16767 const struct partial_die_info *part_die_lhs = item_lhs;
16768 const struct partial_die_info *part_die_rhs = item_rhs;
16769
16770 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
16771 }
16772
16773 static struct cmd_list_element *set_dwarf2_cmdlist;
16774 static struct cmd_list_element *show_dwarf2_cmdlist;
16775
16776 static void
16777 set_dwarf2_cmd (char *args, int from_tty)
16778 {
16779 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16780 }
16781
16782 static void
16783 show_dwarf2_cmd (char *args, int from_tty)
16784 {
16785 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16786 }
16787
16788 /* If section described by INFO was mmapped, munmap it now. */
16789
16790 static void
16791 munmap_section_buffer (struct dwarf2_section_info *info)
16792 {
16793 if (info->map_addr != NULL)
16794 {
16795 #ifdef HAVE_MMAP
16796 int res;
16797
16798 res = munmap (info->map_addr, info->map_len);
16799 gdb_assert (res == 0);
16800 #else
16801 /* Without HAVE_MMAP, we should never be here to begin with. */
16802 gdb_assert_not_reached ("no mmap support");
16803 #endif
16804 }
16805 }
16806
16807 /* munmap debug sections for OBJFILE, if necessary. */
16808
16809 static void
16810 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16811 {
16812 struct dwarf2_per_objfile *data = d;
16813 int ix;
16814 struct dwarf2_section_info *section;
16815
16816 /* This is sorted according to the order they're defined in to make it easier
16817 to keep in sync. */
16818 munmap_section_buffer (&data->info);
16819 munmap_section_buffer (&data->abbrev);
16820 munmap_section_buffer (&data->line);
16821 munmap_section_buffer (&data->loc);
16822 munmap_section_buffer (&data->macinfo);
16823 munmap_section_buffer (&data->macro);
16824 munmap_section_buffer (&data->str);
16825 munmap_section_buffer (&data->ranges);
16826 munmap_section_buffer (&data->addr);
16827 munmap_section_buffer (&data->frame);
16828 munmap_section_buffer (&data->eh_frame);
16829 munmap_section_buffer (&data->gdb_index);
16830
16831 for (ix = 0;
16832 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16833 ++ix)
16834 munmap_section_buffer (section);
16835
16836 VEC_free (dwarf2_section_info_def, data->types);
16837
16838 if (data->dwo_files)
16839 free_dwo_files (data->dwo_files, objfile);
16840 }
16841
16842 \f
16843 /* The "save gdb-index" command. */
16844
16845 /* The contents of the hash table we create when building the string
16846 table. */
16847 struct strtab_entry
16848 {
16849 offset_type offset;
16850 const char *str;
16851 };
16852
16853 /* Hash function for a strtab_entry.
16854
16855 Function is used only during write_hash_table so no index format backward
16856 compatibility is needed. */
16857
16858 static hashval_t
16859 hash_strtab_entry (const void *e)
16860 {
16861 const struct strtab_entry *entry = e;
16862 return mapped_index_string_hash (INT_MAX, entry->str);
16863 }
16864
16865 /* Equality function for a strtab_entry. */
16866
16867 static int
16868 eq_strtab_entry (const void *a, const void *b)
16869 {
16870 const struct strtab_entry *ea = a;
16871 const struct strtab_entry *eb = b;
16872 return !strcmp (ea->str, eb->str);
16873 }
16874
16875 /* Create a strtab_entry hash table. */
16876
16877 static htab_t
16878 create_strtab (void)
16879 {
16880 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16881 xfree, xcalloc, xfree);
16882 }
16883
16884 /* Add a string to the constant pool. Return the string's offset in
16885 host order. */
16886
16887 static offset_type
16888 add_string (htab_t table, struct obstack *cpool, const char *str)
16889 {
16890 void **slot;
16891 struct strtab_entry entry;
16892 struct strtab_entry *result;
16893
16894 entry.str = str;
16895 slot = htab_find_slot (table, &entry, INSERT);
16896 if (*slot)
16897 result = *slot;
16898 else
16899 {
16900 result = XNEW (struct strtab_entry);
16901 result->offset = obstack_object_size (cpool);
16902 result->str = str;
16903 obstack_grow_str0 (cpool, str);
16904 *slot = result;
16905 }
16906 return result->offset;
16907 }
16908
16909 /* An entry in the symbol table. */
16910 struct symtab_index_entry
16911 {
16912 /* The name of the symbol. */
16913 const char *name;
16914 /* The offset of the name in the constant pool. */
16915 offset_type index_offset;
16916 /* A sorted vector of the indices of all the CUs that hold an object
16917 of this name. */
16918 VEC (offset_type) *cu_indices;
16919 };
16920
16921 /* The symbol table. This is a power-of-2-sized hash table. */
16922 struct mapped_symtab
16923 {
16924 offset_type n_elements;
16925 offset_type size;
16926 struct symtab_index_entry **data;
16927 };
16928
16929 /* Hash function for a symtab_index_entry. */
16930
16931 static hashval_t
16932 hash_symtab_entry (const void *e)
16933 {
16934 const struct symtab_index_entry *entry = e;
16935 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16936 sizeof (offset_type) * VEC_length (offset_type,
16937 entry->cu_indices),
16938 0);
16939 }
16940
16941 /* Equality function for a symtab_index_entry. */
16942
16943 static int
16944 eq_symtab_entry (const void *a, const void *b)
16945 {
16946 const struct symtab_index_entry *ea = a;
16947 const struct symtab_index_entry *eb = b;
16948 int len = VEC_length (offset_type, ea->cu_indices);
16949 if (len != VEC_length (offset_type, eb->cu_indices))
16950 return 0;
16951 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16952 VEC_address (offset_type, eb->cu_indices),
16953 sizeof (offset_type) * len);
16954 }
16955
16956 /* Destroy a symtab_index_entry. */
16957
16958 static void
16959 delete_symtab_entry (void *p)
16960 {
16961 struct symtab_index_entry *entry = p;
16962 VEC_free (offset_type, entry->cu_indices);
16963 xfree (entry);
16964 }
16965
16966 /* Create a hash table holding symtab_index_entry objects. */
16967
16968 static htab_t
16969 create_symbol_hash_table (void)
16970 {
16971 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16972 delete_symtab_entry, xcalloc, xfree);
16973 }
16974
16975 /* Create a new mapped symtab object. */
16976
16977 static struct mapped_symtab *
16978 create_mapped_symtab (void)
16979 {
16980 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16981 symtab->n_elements = 0;
16982 symtab->size = 1024;
16983 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16984 return symtab;
16985 }
16986
16987 /* Destroy a mapped_symtab. */
16988
16989 static void
16990 cleanup_mapped_symtab (void *p)
16991 {
16992 struct mapped_symtab *symtab = p;
16993 /* The contents of the array are freed when the other hash table is
16994 destroyed. */
16995 xfree (symtab->data);
16996 xfree (symtab);
16997 }
16998
16999 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17000 the slot.
17001
17002 Function is used only during write_hash_table so no index format backward
17003 compatibility is needed. */
17004
17005 static struct symtab_index_entry **
17006 find_slot (struct mapped_symtab *symtab, const char *name)
17007 {
17008 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17009
17010 index = hash & (symtab->size - 1);
17011 step = ((hash * 17) & (symtab->size - 1)) | 1;
17012
17013 for (;;)
17014 {
17015 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17016 return &symtab->data[index];
17017 index = (index + step) & (symtab->size - 1);
17018 }
17019 }
17020
17021 /* Expand SYMTAB's hash table. */
17022
17023 static void
17024 hash_expand (struct mapped_symtab *symtab)
17025 {
17026 offset_type old_size = symtab->size;
17027 offset_type i;
17028 struct symtab_index_entry **old_entries = symtab->data;
17029
17030 symtab->size *= 2;
17031 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17032
17033 for (i = 0; i < old_size; ++i)
17034 {
17035 if (old_entries[i])
17036 {
17037 struct symtab_index_entry **slot = find_slot (symtab,
17038 old_entries[i]->name);
17039 *slot = old_entries[i];
17040 }
17041 }
17042
17043 xfree (old_entries);
17044 }
17045
17046 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17047 is the index of the CU in which the symbol appears. */
17048
17049 static void
17050 add_index_entry (struct mapped_symtab *symtab, const char *name,
17051 offset_type cu_index)
17052 {
17053 struct symtab_index_entry **slot;
17054
17055 ++symtab->n_elements;
17056 if (4 * symtab->n_elements / 3 >= symtab->size)
17057 hash_expand (symtab);
17058
17059 slot = find_slot (symtab, name);
17060 if (!*slot)
17061 {
17062 *slot = XNEW (struct symtab_index_entry);
17063 (*slot)->name = name;
17064 (*slot)->cu_indices = NULL;
17065 }
17066 /* Don't push an index twice. Due to how we add entries we only
17067 have to check the last one. */
17068 if (VEC_empty (offset_type, (*slot)->cu_indices)
17069 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17070 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17071 }
17072
17073 /* Add a vector of indices to the constant pool. */
17074
17075 static offset_type
17076 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17077 struct symtab_index_entry *entry)
17078 {
17079 void **slot;
17080
17081 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17082 if (!*slot)
17083 {
17084 offset_type len = VEC_length (offset_type, entry->cu_indices);
17085 offset_type val = MAYBE_SWAP (len);
17086 offset_type iter;
17087 int i;
17088
17089 *slot = entry;
17090 entry->index_offset = obstack_object_size (cpool);
17091
17092 obstack_grow (cpool, &val, sizeof (val));
17093 for (i = 0;
17094 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17095 ++i)
17096 {
17097 val = MAYBE_SWAP (iter);
17098 obstack_grow (cpool, &val, sizeof (val));
17099 }
17100 }
17101 else
17102 {
17103 struct symtab_index_entry *old_entry = *slot;
17104 entry->index_offset = old_entry->index_offset;
17105 entry = old_entry;
17106 }
17107 return entry->index_offset;
17108 }
17109
17110 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17111 constant pool entries going into the obstack CPOOL. */
17112
17113 static void
17114 write_hash_table (struct mapped_symtab *symtab,
17115 struct obstack *output, struct obstack *cpool)
17116 {
17117 offset_type i;
17118 htab_t symbol_hash_table;
17119 htab_t str_table;
17120
17121 symbol_hash_table = create_symbol_hash_table ();
17122 str_table = create_strtab ();
17123
17124 /* We add all the index vectors to the constant pool first, to
17125 ensure alignment is ok. */
17126 for (i = 0; i < symtab->size; ++i)
17127 {
17128 if (symtab->data[i])
17129 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17130 }
17131
17132 /* Now write out the hash table. */
17133 for (i = 0; i < symtab->size; ++i)
17134 {
17135 offset_type str_off, vec_off;
17136
17137 if (symtab->data[i])
17138 {
17139 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17140 vec_off = symtab->data[i]->index_offset;
17141 }
17142 else
17143 {
17144 /* While 0 is a valid constant pool index, it is not valid
17145 to have 0 for both offsets. */
17146 str_off = 0;
17147 vec_off = 0;
17148 }
17149
17150 str_off = MAYBE_SWAP (str_off);
17151 vec_off = MAYBE_SWAP (vec_off);
17152
17153 obstack_grow (output, &str_off, sizeof (str_off));
17154 obstack_grow (output, &vec_off, sizeof (vec_off));
17155 }
17156
17157 htab_delete (str_table);
17158 htab_delete (symbol_hash_table);
17159 }
17160
17161 /* Struct to map psymtab to CU index in the index file. */
17162 struct psymtab_cu_index_map
17163 {
17164 struct partial_symtab *psymtab;
17165 unsigned int cu_index;
17166 };
17167
17168 static hashval_t
17169 hash_psymtab_cu_index (const void *item)
17170 {
17171 const struct psymtab_cu_index_map *map = item;
17172
17173 return htab_hash_pointer (map->psymtab);
17174 }
17175
17176 static int
17177 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17178 {
17179 const struct psymtab_cu_index_map *lhs = item_lhs;
17180 const struct psymtab_cu_index_map *rhs = item_rhs;
17181
17182 return lhs->psymtab == rhs->psymtab;
17183 }
17184
17185 /* Helper struct for building the address table. */
17186 struct addrmap_index_data
17187 {
17188 struct objfile *objfile;
17189 struct obstack *addr_obstack;
17190 htab_t cu_index_htab;
17191
17192 /* Non-zero if the previous_* fields are valid.
17193 We can't write an entry until we see the next entry (since it is only then
17194 that we know the end of the entry). */
17195 int previous_valid;
17196 /* Index of the CU in the table of all CUs in the index file. */
17197 unsigned int previous_cu_index;
17198 /* Start address of the CU. */
17199 CORE_ADDR previous_cu_start;
17200 };
17201
17202 /* Write an address entry to OBSTACK. */
17203
17204 static void
17205 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17206 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17207 {
17208 offset_type cu_index_to_write;
17209 char addr[8];
17210 CORE_ADDR baseaddr;
17211
17212 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17213
17214 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17215 obstack_grow (obstack, addr, 8);
17216 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17217 obstack_grow (obstack, addr, 8);
17218 cu_index_to_write = MAYBE_SWAP (cu_index);
17219 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17220 }
17221
17222 /* Worker function for traversing an addrmap to build the address table. */
17223
17224 static int
17225 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17226 {
17227 struct addrmap_index_data *data = datap;
17228 struct partial_symtab *pst = obj;
17229 offset_type cu_index;
17230 void **slot;
17231
17232 if (data->previous_valid)
17233 add_address_entry (data->objfile, data->addr_obstack,
17234 data->previous_cu_start, start_addr,
17235 data->previous_cu_index);
17236
17237 data->previous_cu_start = start_addr;
17238 if (pst != NULL)
17239 {
17240 struct psymtab_cu_index_map find_map, *map;
17241 find_map.psymtab = pst;
17242 map = htab_find (data->cu_index_htab, &find_map);
17243 gdb_assert (map != NULL);
17244 data->previous_cu_index = map->cu_index;
17245 data->previous_valid = 1;
17246 }
17247 else
17248 data->previous_valid = 0;
17249
17250 return 0;
17251 }
17252
17253 /* Write OBJFILE's address map to OBSTACK.
17254 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17255 in the index file. */
17256
17257 static void
17258 write_address_map (struct objfile *objfile, struct obstack *obstack,
17259 htab_t cu_index_htab)
17260 {
17261 struct addrmap_index_data addrmap_index_data;
17262
17263 /* When writing the address table, we have to cope with the fact that
17264 the addrmap iterator only provides the start of a region; we have to
17265 wait until the next invocation to get the start of the next region. */
17266
17267 addrmap_index_data.objfile = objfile;
17268 addrmap_index_data.addr_obstack = obstack;
17269 addrmap_index_data.cu_index_htab = cu_index_htab;
17270 addrmap_index_data.previous_valid = 0;
17271
17272 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17273 &addrmap_index_data);
17274
17275 /* It's highly unlikely the last entry (end address = 0xff...ff)
17276 is valid, but we should still handle it.
17277 The end address is recorded as the start of the next region, but that
17278 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17279 anyway. */
17280 if (addrmap_index_data.previous_valid)
17281 add_address_entry (objfile, obstack,
17282 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17283 addrmap_index_data.previous_cu_index);
17284 }
17285
17286 /* Add a list of partial symbols to SYMTAB. */
17287
17288 static void
17289 write_psymbols (struct mapped_symtab *symtab,
17290 htab_t psyms_seen,
17291 struct partial_symbol **psymp,
17292 int count,
17293 offset_type cu_index,
17294 int is_static)
17295 {
17296 for (; count-- > 0; ++psymp)
17297 {
17298 void **slot, *lookup;
17299
17300 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17301 error (_("Ada is not currently supported by the index"));
17302
17303 /* We only want to add a given psymbol once. However, we also
17304 want to account for whether it is global or static. So, we
17305 may add it twice, using slightly different values. */
17306 if (is_static)
17307 {
17308 uintptr_t val = 1 | (uintptr_t) *psymp;
17309
17310 lookup = (void *) val;
17311 }
17312 else
17313 lookup = *psymp;
17314
17315 /* Only add a given psymbol once. */
17316 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17317 if (!*slot)
17318 {
17319 *slot = lookup;
17320 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17321 }
17322 }
17323 }
17324
17325 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17326 exception if there is an error. */
17327
17328 static void
17329 write_obstack (FILE *file, struct obstack *obstack)
17330 {
17331 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17332 file)
17333 != obstack_object_size (obstack))
17334 error (_("couldn't data write to file"));
17335 }
17336
17337 /* Unlink a file if the argument is not NULL. */
17338
17339 static void
17340 unlink_if_set (void *p)
17341 {
17342 char **filename = p;
17343 if (*filename)
17344 unlink (*filename);
17345 }
17346
17347 /* A helper struct used when iterating over debug_types. */
17348 struct signatured_type_index_data
17349 {
17350 struct objfile *objfile;
17351 struct mapped_symtab *symtab;
17352 struct obstack *types_list;
17353 htab_t psyms_seen;
17354 int cu_index;
17355 };
17356
17357 /* A helper function that writes a single signatured_type to an
17358 obstack. */
17359
17360 static int
17361 write_one_signatured_type (void **slot, void *d)
17362 {
17363 struct signatured_type_index_data *info = d;
17364 struct signatured_type *entry = (struct signatured_type *) *slot;
17365 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17366 struct partial_symtab *psymtab = per_cu->v.psymtab;
17367 gdb_byte val[8];
17368
17369 write_psymbols (info->symtab,
17370 info->psyms_seen,
17371 info->objfile->global_psymbols.list
17372 + psymtab->globals_offset,
17373 psymtab->n_global_syms, info->cu_index,
17374 0);
17375 write_psymbols (info->symtab,
17376 info->psyms_seen,
17377 info->objfile->static_psymbols.list
17378 + psymtab->statics_offset,
17379 psymtab->n_static_syms, info->cu_index,
17380 1);
17381
17382 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17383 entry->per_cu.offset.sect_off);
17384 obstack_grow (info->types_list, val, 8);
17385 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17386 entry->type_offset_in_tu.cu_off);
17387 obstack_grow (info->types_list, val, 8);
17388 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17389 obstack_grow (info->types_list, val, 8);
17390
17391 ++info->cu_index;
17392
17393 return 1;
17394 }
17395
17396 /* Create an index file for OBJFILE in the directory DIR. */
17397
17398 static void
17399 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17400 {
17401 struct cleanup *cleanup;
17402 char *filename, *cleanup_filename;
17403 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17404 struct obstack cu_list, types_cu_list;
17405 int i;
17406 FILE *out_file;
17407 struct mapped_symtab *symtab;
17408 offset_type val, size_of_contents, total_len;
17409 struct stat st;
17410 char buf[8];
17411 htab_t psyms_seen;
17412 htab_t cu_index_htab;
17413 struct psymtab_cu_index_map *psymtab_cu_index_map;
17414
17415 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17416 return;
17417
17418 if (dwarf2_per_objfile->using_index)
17419 error (_("Cannot use an index to create the index"));
17420
17421 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17422 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17423
17424 if (stat (objfile->name, &st) < 0)
17425 perror_with_name (objfile->name);
17426
17427 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17428 INDEX_SUFFIX, (char *) NULL);
17429 cleanup = make_cleanup (xfree, filename);
17430
17431 out_file = fopen (filename, "wb");
17432 if (!out_file)
17433 error (_("Can't open `%s' for writing"), filename);
17434
17435 cleanup_filename = filename;
17436 make_cleanup (unlink_if_set, &cleanup_filename);
17437
17438 symtab = create_mapped_symtab ();
17439 make_cleanup (cleanup_mapped_symtab, symtab);
17440
17441 obstack_init (&addr_obstack);
17442 make_cleanup_obstack_free (&addr_obstack);
17443
17444 obstack_init (&cu_list);
17445 make_cleanup_obstack_free (&cu_list);
17446
17447 obstack_init (&types_cu_list);
17448 make_cleanup_obstack_free (&types_cu_list);
17449
17450 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17451 NULL, xcalloc, xfree);
17452 make_cleanup_htab_delete (psyms_seen);
17453
17454 /* While we're scanning CU's create a table that maps a psymtab pointer
17455 (which is what addrmap records) to its index (which is what is recorded
17456 in the index file). This will later be needed to write the address
17457 table. */
17458 cu_index_htab = htab_create_alloc (100,
17459 hash_psymtab_cu_index,
17460 eq_psymtab_cu_index,
17461 NULL, xcalloc, xfree);
17462 make_cleanup_htab_delete (cu_index_htab);
17463 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17464 xmalloc (sizeof (struct psymtab_cu_index_map)
17465 * dwarf2_per_objfile->n_comp_units);
17466 make_cleanup (xfree, psymtab_cu_index_map);
17467
17468 /* The CU list is already sorted, so we don't need to do additional
17469 work here. Also, the debug_types entries do not appear in
17470 all_comp_units, but only in their own hash table. */
17471 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17472 {
17473 struct dwarf2_per_cu_data *per_cu
17474 = dwarf2_per_objfile->all_comp_units[i];
17475 struct partial_symtab *psymtab = per_cu->v.psymtab;
17476 gdb_byte val[8];
17477 struct psymtab_cu_index_map *map;
17478 void **slot;
17479
17480 write_psymbols (symtab,
17481 psyms_seen,
17482 objfile->global_psymbols.list + psymtab->globals_offset,
17483 psymtab->n_global_syms, i,
17484 0);
17485 write_psymbols (symtab,
17486 psyms_seen,
17487 objfile->static_psymbols.list + psymtab->statics_offset,
17488 psymtab->n_static_syms, i,
17489 1);
17490
17491 map = &psymtab_cu_index_map[i];
17492 map->psymtab = psymtab;
17493 map->cu_index = i;
17494 slot = htab_find_slot (cu_index_htab, map, INSERT);
17495 gdb_assert (slot != NULL);
17496 gdb_assert (*slot == NULL);
17497 *slot = map;
17498
17499 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17500 per_cu->offset.sect_off);
17501 obstack_grow (&cu_list, val, 8);
17502 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17503 obstack_grow (&cu_list, val, 8);
17504 }
17505
17506 /* Dump the address map. */
17507 write_address_map (objfile, &addr_obstack, cu_index_htab);
17508
17509 /* Write out the .debug_type entries, if any. */
17510 if (dwarf2_per_objfile->signatured_types)
17511 {
17512 struct signatured_type_index_data sig_data;
17513
17514 sig_data.objfile = objfile;
17515 sig_data.symtab = symtab;
17516 sig_data.types_list = &types_cu_list;
17517 sig_data.psyms_seen = psyms_seen;
17518 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17519 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17520 write_one_signatured_type, &sig_data);
17521 }
17522
17523 obstack_init (&constant_pool);
17524 make_cleanup_obstack_free (&constant_pool);
17525 obstack_init (&symtab_obstack);
17526 make_cleanup_obstack_free (&symtab_obstack);
17527 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17528
17529 obstack_init (&contents);
17530 make_cleanup_obstack_free (&contents);
17531 size_of_contents = 6 * sizeof (offset_type);
17532 total_len = size_of_contents;
17533
17534 /* The version number. */
17535 val = MAYBE_SWAP (6);
17536 obstack_grow (&contents, &val, sizeof (val));
17537
17538 /* The offset of the CU list from the start of the file. */
17539 val = MAYBE_SWAP (total_len);
17540 obstack_grow (&contents, &val, sizeof (val));
17541 total_len += obstack_object_size (&cu_list);
17542
17543 /* The offset of the types CU list from the start of the file. */
17544 val = MAYBE_SWAP (total_len);
17545 obstack_grow (&contents, &val, sizeof (val));
17546 total_len += obstack_object_size (&types_cu_list);
17547
17548 /* The offset of the address table from the start of the file. */
17549 val = MAYBE_SWAP (total_len);
17550 obstack_grow (&contents, &val, sizeof (val));
17551 total_len += obstack_object_size (&addr_obstack);
17552
17553 /* The offset of the symbol table from the start of the file. */
17554 val = MAYBE_SWAP (total_len);
17555 obstack_grow (&contents, &val, sizeof (val));
17556 total_len += obstack_object_size (&symtab_obstack);
17557
17558 /* The offset of the constant pool from the start of the file. */
17559 val = MAYBE_SWAP (total_len);
17560 obstack_grow (&contents, &val, sizeof (val));
17561 total_len += obstack_object_size (&constant_pool);
17562
17563 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17564
17565 write_obstack (out_file, &contents);
17566 write_obstack (out_file, &cu_list);
17567 write_obstack (out_file, &types_cu_list);
17568 write_obstack (out_file, &addr_obstack);
17569 write_obstack (out_file, &symtab_obstack);
17570 write_obstack (out_file, &constant_pool);
17571
17572 fclose (out_file);
17573
17574 /* We want to keep the file, so we set cleanup_filename to NULL
17575 here. See unlink_if_set. */
17576 cleanup_filename = NULL;
17577
17578 do_cleanups (cleanup);
17579 }
17580
17581 /* Implementation of the `save gdb-index' command.
17582
17583 Note that the file format used by this command is documented in the
17584 GDB manual. Any changes here must be documented there. */
17585
17586 static void
17587 save_gdb_index_command (char *arg, int from_tty)
17588 {
17589 struct objfile *objfile;
17590
17591 if (!arg || !*arg)
17592 error (_("usage: save gdb-index DIRECTORY"));
17593
17594 ALL_OBJFILES (objfile)
17595 {
17596 struct stat st;
17597
17598 /* If the objfile does not correspond to an actual file, skip it. */
17599 if (stat (objfile->name, &st) < 0)
17600 continue;
17601
17602 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17603 if (dwarf2_per_objfile)
17604 {
17605 volatile struct gdb_exception except;
17606
17607 TRY_CATCH (except, RETURN_MASK_ERROR)
17608 {
17609 write_psymtabs_to_index (objfile, arg);
17610 }
17611 if (except.reason < 0)
17612 exception_fprintf (gdb_stderr, except,
17613 _("Error while writing index for `%s': "),
17614 objfile->name);
17615 }
17616 }
17617 }
17618
17619 \f
17620
17621 int dwarf2_always_disassemble;
17622
17623 static void
17624 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17625 struct cmd_list_element *c, const char *value)
17626 {
17627 fprintf_filtered (file,
17628 _("Whether to always disassemble "
17629 "DWARF expressions is %s.\n"),
17630 value);
17631 }
17632
17633 static void
17634 show_check_physname (struct ui_file *file, int from_tty,
17635 struct cmd_list_element *c, const char *value)
17636 {
17637 fprintf_filtered (file,
17638 _("Whether to check \"physname\" is %s.\n"),
17639 value);
17640 }
17641
17642 void _initialize_dwarf2_read (void);
17643
17644 void
17645 _initialize_dwarf2_read (void)
17646 {
17647 struct cmd_list_element *c;
17648
17649 dwarf2_objfile_data_key
17650 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17651
17652 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17653 Set DWARF 2 specific variables.\n\
17654 Configure DWARF 2 variables such as the cache size"),
17655 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17656 0/*allow-unknown*/, &maintenance_set_cmdlist);
17657
17658 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17659 Show DWARF 2 specific variables\n\
17660 Show DWARF 2 variables such as the cache size"),
17661 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17662 0/*allow-unknown*/, &maintenance_show_cmdlist);
17663
17664 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17665 &dwarf2_max_cache_age, _("\
17666 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17667 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17668 A higher limit means that cached compilation units will be stored\n\
17669 in memory longer, and more total memory will be used. Zero disables\n\
17670 caching, which can slow down startup."),
17671 NULL,
17672 show_dwarf2_max_cache_age,
17673 &set_dwarf2_cmdlist,
17674 &show_dwarf2_cmdlist);
17675
17676 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17677 &dwarf2_always_disassemble, _("\
17678 Set whether `info address' always disassembles DWARF expressions."), _("\
17679 Show whether `info address' always disassembles DWARF expressions."), _("\
17680 When enabled, DWARF expressions are always printed in an assembly-like\n\
17681 syntax. When disabled, expressions will be printed in a more\n\
17682 conversational style, when possible."),
17683 NULL,
17684 show_dwarf2_always_disassemble,
17685 &set_dwarf2_cmdlist,
17686 &show_dwarf2_cmdlist);
17687
17688 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17689 Set debugging of the dwarf2 DIE reader."), _("\
17690 Show debugging of the dwarf2 DIE reader."), _("\
17691 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17692 The value is the maximum depth to print."),
17693 NULL,
17694 NULL,
17695 &setdebuglist, &showdebuglist);
17696
17697 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17698 Set cross-checking of \"physname\" code against demangler."), _("\
17699 Show cross-checking of \"physname\" code against demangler."), _("\
17700 When enabled, GDB's internal \"physname\" code is checked against\n\
17701 the demangler."),
17702 NULL, show_check_physname,
17703 &setdebuglist, &showdebuglist);
17704
17705 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17706 _("\
17707 Save a gdb-index file.\n\
17708 Usage: save gdb-index DIRECTORY"),
17709 &save_cmdlist);
17710 set_cmd_completer (c, filename_completer);
17711 }