* dwarf2read.c (dw2_find_symbol_file): Unconditionally use
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include <ctype.h>
66
67 #include <fcntl.h>
68 #include "gdb_string.h"
69 #include "gdb_assert.h"
70 #include <sys/types.h>
71 #ifdef HAVE_ZLIB_H
72 #include <zlib.h>
73 #endif
74 #ifdef HAVE_MMAP
75 #include <sys/mman.h>
76 #ifndef MAP_FAILED
77 #define MAP_FAILED ((void *) -1)
78 #endif
79 #endif
80
81 typedef struct symbol *symbolp;
82 DEF_VEC_P (symbolp);
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 int use_deprecated_index_sections = 0;
92
93 static int pagesize;
94
95 /* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99 static int processing_has_namespace_info;
100
101 static const struct objfile_data *dwarf2_objfile_data_key;
102
103 struct dwarf2_section_info
104 {
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
112 /* True if we have tried to read this section. */
113 int readin;
114 };
115
116 typedef struct dwarf2_section_info dwarf2_section_info_def;
117 DEF_VEC_O (dwarf2_section_info_def);
118
119 /* All offsets in the index are of this type. It must be
120 architecture-independent. */
121 typedef uint32_t offset_type;
122
123 DEF_VEC_I (offset_type);
124
125 /* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127 struct mapped_index
128 {
129 /* Index data format version. */
130 int version;
131
132 /* The total length of the buffer. */
133 off_t total_size;
134
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
137
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
140
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
143
144 /* Size in slots, each slot is 2 offset_types. */
145 offset_type symbol_table_slots;
146
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149 };
150
151 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152 DEF_VEC_P (dwarf2_per_cu_ptr);
153
154 /* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
157 struct dwarf2_per_objfile
158 {
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
164 struct dwarf2_section_info macro;
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
167 struct dwarf2_section_info addr;
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
170 struct dwarf2_section_info gdb_index;
171
172 VEC (dwarf2_section_info_def) *types;
173
174 /* Back link. */
175 struct objfile *objfile;
176
177 /* Table of all the compilation units. This is used to locate
178 the target compilation unit of a particular reference. */
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
184 /* The number of .debug_types-related CUs. */
185 int n_type_units;
186
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
189
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
193
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
205
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
208 unsigned char using_index;
209
210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
211 struct mapped_index *index_table;
212
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
222
223 /* Table mapping type DIEs to their struct type *.
224 This is NULL if not allocated yet.
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
230 };
231
232 static struct dwarf2_per_objfile *dwarf2_per_objfile;
233
234 /* Default names of the debugging sections. */
235
236 /* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
239 static const struct dwarf2_debug_sections dwarf2_elf_names =
240 {
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
246 { ".debug_macro", ".zdebug_macro" },
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
250 { ".debug_addr", ".zdebug_addr" },
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
253 { ".gdb_index", ".zgdb_index" },
254 23
255 };
256
257 /* List of DWO sections. */
258
259 static const struct dwo_section_names
260 {
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268 }
269 dwo_section_names =
270 {
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278 };
279
280 /* local data types */
281
282 /* We hold several abbreviation tables in memory at the same time. */
283 #ifndef ABBREV_HASH_SIZE
284 #define ABBREV_HASH_SIZE 121
285 #endif
286
287 /* The data in a compilation unit header, after target2host
288 translation, looks like this. */
289 struct comp_unit_head
290 {
291 unsigned int length;
292 short version;
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
295 sect_offset abbrev_offset;
296
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
299
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
302
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
305 sect_offset offset;
306
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
309 cu_offset first_die_offset;
310 };
311
312 /* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314 struct delayed_method_info
315 {
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330 };
331
332 typedef struct delayed_method_info delayed_method_info;
333 DEF_VEC_O (delayed_method_info);
334
335 /* Internal state when decoding a particular compilation unit. */
336 struct dwarf2_cu
337 {
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
341 /* The header of the compilation unit. */
342 struct comp_unit_head header;
343
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
354 const char *producer;
355
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
372
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
395 htab_t die_hash;
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
415 /* Non-NULL if this CU came from a DWO file.
416 There is an invariant here that is important to remember:
417 Except for attributes copied from the top level DIE in the "main"
418 (or "stub") file in preparation for reading the DWO file
419 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
420 Either there isn't a DWO file (in which case this is NULL and the point
421 is moot), or there is and either we're not going to read it (in which
422 case this is NULL) or there is and we are reading it (in which case this
423 is non-NULL). */
424 struct dwo_unit *dwo_unit;
425
426 /* The DW_AT_addr_base attribute if present, zero otherwise
427 (zero is a valid value though).
428 Note this value comes from the stub CU/TU's DIE. */
429 ULONGEST addr_base;
430
431 /* Mark used when releasing cached dies. */
432 unsigned int mark : 1;
433
434 /* This CU references .debug_loc. See the symtab->locations_valid field.
435 This test is imperfect as there may exist optimized debug code not using
436 any location list and still facing inlining issues if handled as
437 unoptimized code. For a future better test see GCC PR other/32998. */
438 unsigned int has_loclist : 1;
439
440 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
441 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
442 are valid. This information is cached because profiling CU expansion
443 showed excessive time spent in producer_is_gxx_lt_4_6. */
444 unsigned int checked_producer : 1;
445 unsigned int producer_is_gxx_lt_4_6 : 1;
446 unsigned int producer_is_icc : 1;
447
448 /* Non-zero if DW_AT_addr_base was found.
449 Used when processing DWO files. */
450 unsigned int have_addr_base : 1;
451 };
452
453 /* Persistent data held for a compilation unit, even when not
454 processing it. We put a pointer to this structure in the
455 read_symtab_private field of the psymtab. */
456
457 struct dwarf2_per_cu_data
458 {
459 /* The start offset and length of this compilation unit. 2**29-1
460 bytes should suffice to store the length of any compilation unit
461 - if it doesn't, GDB will fall over anyway.
462 NOTE: Unlike comp_unit_head.length, this length includes
463 initial_length_size.
464 If the DIE refers to a DWO file, this is always of the original die,
465 not the DWO file. */
466 sect_offset offset;
467 unsigned int length : 29;
468
469 /* Flag indicating this compilation unit will be read in before
470 any of the current compilation units are processed. */
471 unsigned int queued : 1;
472
473 /* This flag will be set when reading partial DIEs if we need to load
474 absolutely all DIEs for this compilation unit, instead of just the ones
475 we think are interesting. It gets set if we look for a DIE in the
476 hash table and don't find it. */
477 unsigned int load_all_dies : 1;
478
479 /* Non-zero if this CU is from .debug_types. */
480 unsigned int is_debug_types : 1;
481
482 /* The section this CU/TU lives in.
483 If the DIE refers to a DWO file, this is always the original die,
484 not the DWO file. */
485 struct dwarf2_section_info *info_or_types_section;
486
487 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
488 of the CU cache it gets reset to NULL again. */
489 struct dwarf2_cu *cu;
490
491 /* The corresponding objfile.
492 Normally we can get the objfile from dwarf2_per_objfile.
493 However we can enter this file with just a "per_cu" handle. */
494 struct objfile *objfile;
495
496 /* When using partial symbol tables, the 'psymtab' field is active.
497 Otherwise the 'quick' field is active. */
498 union
499 {
500 /* The partial symbol table associated with this compilation unit,
501 or NULL for unread partial units. */
502 struct partial_symtab *psymtab;
503
504 /* Data needed by the "quick" functions. */
505 struct dwarf2_per_cu_quick_data *quick;
506 } v;
507
508 /* The CUs we import using DW_TAG_imported_unit. This is filled in
509 while reading psymtabs, used to compute the psymtab dependencies,
510 and then cleared. Then it is filled in again while reading full
511 symbols, and only deleted when the objfile is destroyed. */
512 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
513 };
514
515 /* Entry in the signatured_types hash table. */
516
517 struct signatured_type
518 {
519 /* The type's signature. */
520 ULONGEST signature;
521
522 /* Offset in the TU of the type's DIE, as read from the TU header.
523 If the definition lives in a DWO file, this value is unusable. */
524 cu_offset type_offset_in_tu;
525
526 /* Offset in the section of the type's DIE.
527 If the definition lives in a DWO file, this is the offset in the
528 .debug_types.dwo section.
529 The value is zero until the actual value is known.
530 Zero is otherwise not a valid section offset. */
531 sect_offset type_offset_in_section;
532
533 /* The CU(/TU) of this type. */
534 struct dwarf2_per_cu_data per_cu;
535 };
536
537 /* These sections are what may appear in a "dwo" file. */
538
539 struct dwo_sections
540 {
541 struct dwarf2_section_info abbrev;
542 struct dwarf2_section_info info;
543 struct dwarf2_section_info line;
544 struct dwarf2_section_info loc;
545 struct dwarf2_section_info str;
546 struct dwarf2_section_info str_offsets;
547 VEC (dwarf2_section_info_def) *types;
548 };
549
550 /* Common bits of DWO CUs/TUs. */
551
552 struct dwo_unit
553 {
554 /* Backlink to the containing struct dwo_file. */
555 struct dwo_file *dwo_file;
556
557 /* The "id" that distinguishes this CU/TU.
558 .debug_info calls this "dwo_id", .debug_types calls this "signature".
559 Since signatures came first, we stick with it for consistency. */
560 ULONGEST signature;
561
562 /* The section this CU/TU lives in, in the DWO file. */
563 struct dwarf2_section_info *info_or_types_section;
564
565 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
566 sect_offset offset;
567 unsigned int length;
568
569 /* For types, offset in the type's DIE of the type defined by this TU. */
570 cu_offset type_offset_in_tu;
571 };
572
573 /* Data for one DWO file. */
574
575 struct dwo_file
576 {
577 /* The DW_AT_GNU_dwo_name attribute.
578 We don't manage space for this, it's an attribute. */
579 const char *dwo_name;
580
581 /* The bfd, when the file is open. Otherwise this is NULL. */
582 bfd *dwo_bfd;
583
584 /* Section info for this file. */
585 struct dwo_sections sections;
586
587 /* Table of CUs in the file.
588 Each element is a struct dwo_unit. */
589 htab_t cus;
590
591 /* Table of TUs in the file.
592 Each element is a struct dwo_unit. */
593 htab_t tus;
594 };
595
596 /* Struct used to pass misc. parameters to read_die_and_children, et
597 al. which are used for both .debug_info and .debug_types dies.
598 All parameters here are unchanging for the life of the call. This
599 struct exists to abstract away the constant parameters of die reading. */
600
601 struct die_reader_specs
602 {
603 /* die_section->asection->owner. */
604 bfd* abfd;
605
606 /* The CU of the DIE we are parsing. */
607 struct dwarf2_cu *cu;
608
609 /* Non-NULL if reading a DWO file. */
610 struct dwo_file *dwo_file;
611
612 /* The section the die comes from.
613 This is either .debug_info or .debug_types, or the .dwo variants. */
614 struct dwarf2_section_info *die_section;
615
616 /* die_section->buffer. */
617 gdb_byte *buffer;
618
619 /* The end of the buffer. */
620 const gdb_byte *buffer_end;
621 };
622
623 /* Type of function passed to init_cutu_and_read_dies, et.al. */
624 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
625 gdb_byte *info_ptr,
626 struct die_info *comp_unit_die,
627 int has_children,
628 void *data);
629
630 /* The line number information for a compilation unit (found in the
631 .debug_line section) begins with a "statement program header",
632 which contains the following information. */
633 struct line_header
634 {
635 unsigned int total_length;
636 unsigned short version;
637 unsigned int header_length;
638 unsigned char minimum_instruction_length;
639 unsigned char maximum_ops_per_instruction;
640 unsigned char default_is_stmt;
641 int line_base;
642 unsigned char line_range;
643 unsigned char opcode_base;
644
645 /* standard_opcode_lengths[i] is the number of operands for the
646 standard opcode whose value is i. This means that
647 standard_opcode_lengths[0] is unused, and the last meaningful
648 element is standard_opcode_lengths[opcode_base - 1]. */
649 unsigned char *standard_opcode_lengths;
650
651 /* The include_directories table. NOTE! These strings are not
652 allocated with xmalloc; instead, they are pointers into
653 debug_line_buffer. If you try to free them, `free' will get
654 indigestion. */
655 unsigned int num_include_dirs, include_dirs_size;
656 char **include_dirs;
657
658 /* The file_names table. NOTE! These strings are not allocated
659 with xmalloc; instead, they are pointers into debug_line_buffer.
660 Don't try to free them directly. */
661 unsigned int num_file_names, file_names_size;
662 struct file_entry
663 {
664 char *name;
665 unsigned int dir_index;
666 unsigned int mod_time;
667 unsigned int length;
668 int included_p; /* Non-zero if referenced by the Line Number Program. */
669 struct symtab *symtab; /* The associated symbol table, if any. */
670 } *file_names;
671
672 /* The start and end of the statement program following this
673 header. These point into dwarf2_per_objfile->line_buffer. */
674 gdb_byte *statement_program_start, *statement_program_end;
675 };
676
677 /* When we construct a partial symbol table entry we only
678 need this much information. */
679 struct partial_die_info
680 {
681 /* Offset of this DIE. */
682 sect_offset offset;
683
684 /* DWARF-2 tag for this DIE. */
685 ENUM_BITFIELD(dwarf_tag) tag : 16;
686
687 /* Assorted flags describing the data found in this DIE. */
688 unsigned int has_children : 1;
689 unsigned int is_external : 1;
690 unsigned int is_declaration : 1;
691 unsigned int has_type : 1;
692 unsigned int has_specification : 1;
693 unsigned int has_pc_info : 1;
694 unsigned int may_be_inlined : 1;
695
696 /* Flag set if the SCOPE field of this structure has been
697 computed. */
698 unsigned int scope_set : 1;
699
700 /* Flag set if the DIE has a byte_size attribute. */
701 unsigned int has_byte_size : 1;
702
703 /* Flag set if any of the DIE's children are template arguments. */
704 unsigned int has_template_arguments : 1;
705
706 /* Flag set if fixup_partial_die has been called on this die. */
707 unsigned int fixup_called : 1;
708
709 /* The name of this DIE. Normally the value of DW_AT_name, but
710 sometimes a default name for unnamed DIEs. */
711 char *name;
712
713 /* The linkage name, if present. */
714 const char *linkage_name;
715
716 /* The scope to prepend to our children. This is generally
717 allocated on the comp_unit_obstack, so will disappear
718 when this compilation unit leaves the cache. */
719 char *scope;
720
721 /* Some data associated with the partial DIE. The tag determines
722 which field is live. */
723 union
724 {
725 /* The location description associated with this DIE, if any. */
726 struct dwarf_block *locdesc;
727 /* The offset of an import, for DW_TAG_imported_unit. */
728 sect_offset offset;
729 } d;
730
731 /* If HAS_PC_INFO, the PC range associated with this DIE. */
732 CORE_ADDR lowpc;
733 CORE_ADDR highpc;
734
735 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
736 DW_AT_sibling, if any. */
737 /* NOTE: This member isn't strictly necessary, read_partial_die could
738 return DW_AT_sibling values to its caller load_partial_dies. */
739 gdb_byte *sibling;
740
741 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
742 DW_AT_specification (or DW_AT_abstract_origin or
743 DW_AT_extension). */
744 sect_offset spec_offset;
745
746 /* Pointers to this DIE's parent, first child, and next sibling,
747 if any. */
748 struct partial_die_info *die_parent, *die_child, *die_sibling;
749 };
750
751 /* This data structure holds the information of an abbrev. */
752 struct abbrev_info
753 {
754 unsigned int number; /* number identifying abbrev */
755 enum dwarf_tag tag; /* dwarf tag */
756 unsigned short has_children; /* boolean */
757 unsigned short num_attrs; /* number of attributes */
758 struct attr_abbrev *attrs; /* an array of attribute descriptions */
759 struct abbrev_info *next; /* next in chain */
760 };
761
762 struct attr_abbrev
763 {
764 ENUM_BITFIELD(dwarf_attribute) name : 16;
765 ENUM_BITFIELD(dwarf_form) form : 16;
766 };
767
768 /* Attributes have a name and a value. */
769 struct attribute
770 {
771 ENUM_BITFIELD(dwarf_attribute) name : 16;
772 ENUM_BITFIELD(dwarf_form) form : 15;
773
774 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
775 field should be in u.str (existing only for DW_STRING) but it is kept
776 here for better struct attribute alignment. */
777 unsigned int string_is_canonical : 1;
778
779 union
780 {
781 char *str;
782 struct dwarf_block *blk;
783 ULONGEST unsnd;
784 LONGEST snd;
785 CORE_ADDR addr;
786 struct signatured_type *signatured_type;
787 }
788 u;
789 };
790
791 /* This data structure holds a complete die structure. */
792 struct die_info
793 {
794 /* DWARF-2 tag for this DIE. */
795 ENUM_BITFIELD(dwarf_tag) tag : 16;
796
797 /* Number of attributes */
798 unsigned char num_attrs;
799
800 /* True if we're presently building the full type name for the
801 type derived from this DIE. */
802 unsigned char building_fullname : 1;
803
804 /* Abbrev number */
805 unsigned int abbrev;
806
807 /* Offset in .debug_info or .debug_types section. */
808 sect_offset offset;
809
810 /* The dies in a compilation unit form an n-ary tree. PARENT
811 points to this die's parent; CHILD points to the first child of
812 this node; and all the children of a given node are chained
813 together via their SIBLING fields. */
814 struct die_info *child; /* Its first child, if any. */
815 struct die_info *sibling; /* Its next sibling, if any. */
816 struct die_info *parent; /* Its parent, if any. */
817
818 /* An array of attributes, with NUM_ATTRS elements. There may be
819 zero, but it's not common and zero-sized arrays are not
820 sufficiently portable C. */
821 struct attribute attrs[1];
822 };
823
824 /* Get at parts of an attribute structure. */
825
826 #define DW_STRING(attr) ((attr)->u.str)
827 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
828 #define DW_UNSND(attr) ((attr)->u.unsnd)
829 #define DW_BLOCK(attr) ((attr)->u.blk)
830 #define DW_SND(attr) ((attr)->u.snd)
831 #define DW_ADDR(attr) ((attr)->u.addr)
832 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
833
834 /* Blocks are a bunch of untyped bytes. */
835 struct dwarf_block
836 {
837 unsigned int size;
838
839 /* Valid only if SIZE is not zero. */
840 gdb_byte *data;
841 };
842
843 #ifndef ATTR_ALLOC_CHUNK
844 #define ATTR_ALLOC_CHUNK 4
845 #endif
846
847 /* Allocate fields for structs, unions and enums in this size. */
848 #ifndef DW_FIELD_ALLOC_CHUNK
849 #define DW_FIELD_ALLOC_CHUNK 4
850 #endif
851
852 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
853 but this would require a corresponding change in unpack_field_as_long
854 and friends. */
855 static int bits_per_byte = 8;
856
857 /* The routines that read and process dies for a C struct or C++ class
858 pass lists of data member fields and lists of member function fields
859 in an instance of a field_info structure, as defined below. */
860 struct field_info
861 {
862 /* List of data member and baseclasses fields. */
863 struct nextfield
864 {
865 struct nextfield *next;
866 int accessibility;
867 int virtuality;
868 struct field field;
869 }
870 *fields, *baseclasses;
871
872 /* Number of fields (including baseclasses). */
873 int nfields;
874
875 /* Number of baseclasses. */
876 int nbaseclasses;
877
878 /* Set if the accesibility of one of the fields is not public. */
879 int non_public_fields;
880
881 /* Member function fields array, entries are allocated in the order they
882 are encountered in the object file. */
883 struct nextfnfield
884 {
885 struct nextfnfield *next;
886 struct fn_field fnfield;
887 }
888 *fnfields;
889
890 /* Member function fieldlist array, contains name of possibly overloaded
891 member function, number of overloaded member functions and a pointer
892 to the head of the member function field chain. */
893 struct fnfieldlist
894 {
895 char *name;
896 int length;
897 struct nextfnfield *head;
898 }
899 *fnfieldlists;
900
901 /* Number of entries in the fnfieldlists array. */
902 int nfnfields;
903
904 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
905 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
906 struct typedef_field_list
907 {
908 struct typedef_field field;
909 struct typedef_field_list *next;
910 }
911 *typedef_field_list;
912 unsigned typedef_field_list_count;
913 };
914
915 /* One item on the queue of compilation units to read in full symbols
916 for. */
917 struct dwarf2_queue_item
918 {
919 struct dwarf2_per_cu_data *per_cu;
920 enum language pretend_language;
921 struct dwarf2_queue_item *next;
922 };
923
924 /* The current queue. */
925 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
926
927 /* Loaded secondary compilation units are kept in memory until they
928 have not been referenced for the processing of this many
929 compilation units. Set this to zero to disable caching. Cache
930 sizes of up to at least twenty will improve startup time for
931 typical inter-CU-reference binaries, at an obvious memory cost. */
932 static int dwarf2_max_cache_age = 5;
933 static void
934 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
935 struct cmd_list_element *c, const char *value)
936 {
937 fprintf_filtered (file, _("The upper bound on the age of cached "
938 "dwarf2 compilation units is %s.\n"),
939 value);
940 }
941
942
943 /* Various complaints about symbol reading that don't abort the process. */
944
945 static void
946 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
947 {
948 complaint (&symfile_complaints,
949 _("statement list doesn't fit in .debug_line section"));
950 }
951
952 static void
953 dwarf2_debug_line_missing_file_complaint (void)
954 {
955 complaint (&symfile_complaints,
956 _(".debug_line section has line data without a file"));
957 }
958
959 static void
960 dwarf2_debug_line_missing_end_sequence_complaint (void)
961 {
962 complaint (&symfile_complaints,
963 _(".debug_line section has line "
964 "program sequence without an end"));
965 }
966
967 static void
968 dwarf2_complex_location_expr_complaint (void)
969 {
970 complaint (&symfile_complaints, _("location expression too complex"));
971 }
972
973 static void
974 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
975 int arg3)
976 {
977 complaint (&symfile_complaints,
978 _("const value length mismatch for '%s', got %d, expected %d"),
979 arg1, arg2, arg3);
980 }
981
982 static void
983 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
984 {
985 complaint (&symfile_complaints,
986 _("debug info runs off end of %s section"
987 " [in module %s]"),
988 section->asection->name,
989 bfd_get_filename (section->asection->owner));
990 }
991
992 static void
993 dwarf2_macro_malformed_definition_complaint (const char *arg1)
994 {
995 complaint (&symfile_complaints,
996 _("macro debug info contains a "
997 "malformed macro definition:\n`%s'"),
998 arg1);
999 }
1000
1001 static void
1002 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1003 {
1004 complaint (&symfile_complaints,
1005 _("invalid attribute class or form for '%s' in '%s'"),
1006 arg1, arg2);
1007 }
1008
1009 /* local function prototypes */
1010
1011 static void dwarf2_locate_sections (bfd *, asection *, void *);
1012
1013 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1014 struct objfile *);
1015
1016 static void dwarf2_find_base_address (struct die_info *die,
1017 struct dwarf2_cu *cu);
1018
1019 static void dwarf2_build_psymtabs_hard (struct objfile *);
1020
1021 static void scan_partial_symbols (struct partial_die_info *,
1022 CORE_ADDR *, CORE_ADDR *,
1023 int, struct dwarf2_cu *);
1024
1025 static void add_partial_symbol (struct partial_die_info *,
1026 struct dwarf2_cu *);
1027
1028 static void add_partial_namespace (struct partial_die_info *pdi,
1029 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1030 int need_pc, struct dwarf2_cu *cu);
1031
1032 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1033 CORE_ADDR *highpc, int need_pc,
1034 struct dwarf2_cu *cu);
1035
1036 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1037 struct dwarf2_cu *cu);
1038
1039 static void add_partial_subprogram (struct partial_die_info *pdi,
1040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1041 int need_pc, struct dwarf2_cu *cu);
1042
1043 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1044
1045 static void psymtab_to_symtab_1 (struct partial_symtab *);
1046
1047 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1048 struct dwarf2_section_info *);
1049
1050 static void dwarf2_free_abbrev_table (void *);
1051
1052 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1053
1054 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1055 struct dwarf2_cu *);
1056
1057 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1058 struct dwarf2_cu *);
1059
1060 static struct partial_die_info *load_partial_dies
1061 (const struct die_reader_specs *, gdb_byte *, int);
1062
1063 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1064 struct partial_die_info *,
1065 struct abbrev_info *,
1066 unsigned int,
1067 gdb_byte *);
1068
1069 static struct partial_die_info *find_partial_die (sect_offset,
1070 struct dwarf2_cu *);
1071
1072 static void fixup_partial_die (struct partial_die_info *,
1073 struct dwarf2_cu *);
1074
1075 static gdb_byte *read_attribute (const struct die_reader_specs *,
1076 struct attribute *, struct attr_abbrev *,
1077 gdb_byte *);
1078
1079 static unsigned int read_1_byte (bfd *, gdb_byte *);
1080
1081 static int read_1_signed_byte (bfd *, gdb_byte *);
1082
1083 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1084
1085 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1086
1087 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1088
1089 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1090 unsigned int *);
1091
1092 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1093
1094 static LONGEST read_checked_initial_length_and_offset
1095 (bfd *, gdb_byte *, const struct comp_unit_head *,
1096 unsigned int *, unsigned int *);
1097
1098 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1099 unsigned int *);
1100
1101 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1102
1103 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1104
1105 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1106
1107 static char *read_indirect_string (bfd *, gdb_byte *,
1108 const struct comp_unit_head *,
1109 unsigned int *);
1110
1111 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1112
1113 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1114
1115 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1116 unsigned int *);
1117
1118 static char *read_str_index (const struct die_reader_specs *reader,
1119 struct dwarf2_cu *cu, ULONGEST str_index);
1120
1121 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1122
1123 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1124 struct dwarf2_cu *);
1125
1126 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1127 unsigned int,
1128 struct dwarf2_cu *);
1129
1130 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1131 struct dwarf2_cu *cu);
1132
1133 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1134
1135 static struct die_info *die_specification (struct die_info *die,
1136 struct dwarf2_cu **);
1137
1138 static void free_line_header (struct line_header *lh);
1139
1140 static void add_file_name (struct line_header *, char *, unsigned int,
1141 unsigned int, unsigned int);
1142
1143 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1144 struct dwarf2_cu *cu);
1145
1146 static void dwarf_decode_lines (struct line_header *, const char *,
1147 struct dwarf2_cu *, struct partial_symtab *,
1148 int);
1149
1150 static void dwarf2_start_subfile (char *, const char *, const char *);
1151
1152 static struct symbol *new_symbol (struct die_info *, struct type *,
1153 struct dwarf2_cu *);
1154
1155 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1156 struct dwarf2_cu *, struct symbol *);
1157
1158 static void dwarf2_const_value (struct attribute *, struct symbol *,
1159 struct dwarf2_cu *);
1160
1161 static void dwarf2_const_value_attr (struct attribute *attr,
1162 struct type *type,
1163 const char *name,
1164 struct obstack *obstack,
1165 struct dwarf2_cu *cu, LONGEST *value,
1166 gdb_byte **bytes,
1167 struct dwarf2_locexpr_baton **baton);
1168
1169 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1170
1171 static int need_gnat_info (struct dwarf2_cu *);
1172
1173 static struct type *die_descriptive_type (struct die_info *,
1174 struct dwarf2_cu *);
1175
1176 static void set_descriptive_type (struct type *, struct die_info *,
1177 struct dwarf2_cu *);
1178
1179 static struct type *die_containing_type (struct die_info *,
1180 struct dwarf2_cu *);
1181
1182 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1183 struct dwarf2_cu *);
1184
1185 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1186
1187 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1188
1189 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1190
1191 static char *typename_concat (struct obstack *obs, const char *prefix,
1192 const char *suffix, int physname,
1193 struct dwarf2_cu *cu);
1194
1195 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1196
1197 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1198
1199 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1200
1201 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1202
1203 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1204
1205 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1206 struct dwarf2_cu *, struct partial_symtab *);
1207
1208 static int dwarf2_get_pc_bounds (struct die_info *,
1209 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1210 struct partial_symtab *);
1211
1212 static void get_scope_pc_bounds (struct die_info *,
1213 CORE_ADDR *, CORE_ADDR *,
1214 struct dwarf2_cu *);
1215
1216 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1217 CORE_ADDR, struct dwarf2_cu *);
1218
1219 static void dwarf2_add_field (struct field_info *, struct die_info *,
1220 struct dwarf2_cu *);
1221
1222 static void dwarf2_attach_fields_to_type (struct field_info *,
1223 struct type *, struct dwarf2_cu *);
1224
1225 static void dwarf2_add_member_fn (struct field_info *,
1226 struct die_info *, struct type *,
1227 struct dwarf2_cu *);
1228
1229 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1230 struct type *,
1231 struct dwarf2_cu *);
1232
1233 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1234
1235 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1236
1237 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1238
1239 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1240
1241 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1242
1243 static struct type *read_module_type (struct die_info *die,
1244 struct dwarf2_cu *cu);
1245
1246 static const char *namespace_name (struct die_info *die,
1247 int *is_anonymous, struct dwarf2_cu *);
1248
1249 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1250
1251 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1252
1253 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1254 struct dwarf2_cu *);
1255
1256 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1257 gdb_byte *info_ptr,
1258 gdb_byte **new_info_ptr,
1259 struct die_info *parent);
1260
1261 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1262 gdb_byte *info_ptr,
1263 gdb_byte **new_info_ptr,
1264 struct die_info *parent);
1265
1266 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1267 struct die_info **, gdb_byte *, int *, int);
1268
1269 static gdb_byte *read_full_die (const struct die_reader_specs *,
1270 struct die_info **, gdb_byte *, int *);
1271
1272 static void process_die (struct die_info *, struct dwarf2_cu *);
1273
1274 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1275 struct obstack *);
1276
1277 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1278
1279 static const char *dwarf2_full_name (char *name,
1280 struct die_info *die,
1281 struct dwarf2_cu *cu);
1282
1283 static struct die_info *dwarf2_extension (struct die_info *die,
1284 struct dwarf2_cu **);
1285
1286 static const char *dwarf_tag_name (unsigned int);
1287
1288 static const char *dwarf_attr_name (unsigned int);
1289
1290 static const char *dwarf_form_name (unsigned int);
1291
1292 static char *dwarf_bool_name (unsigned int);
1293
1294 static const char *dwarf_type_encoding_name (unsigned int);
1295
1296 static struct die_info *sibling_die (struct die_info *);
1297
1298 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1299
1300 static void dump_die_for_error (struct die_info *);
1301
1302 static void dump_die_1 (struct ui_file *, int level, int max_level,
1303 struct die_info *);
1304
1305 /*static*/ void dump_die (struct die_info *, int max_level);
1306
1307 static void store_in_ref_table (struct die_info *,
1308 struct dwarf2_cu *);
1309
1310 static int is_ref_attr (struct attribute *);
1311
1312 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1313
1314 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1315
1316 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1317 struct attribute *,
1318 struct dwarf2_cu **);
1319
1320 static struct die_info *follow_die_ref (struct die_info *,
1321 struct attribute *,
1322 struct dwarf2_cu **);
1323
1324 static struct die_info *follow_die_sig (struct die_info *,
1325 struct attribute *,
1326 struct dwarf2_cu **);
1327
1328 static struct signatured_type *lookup_signatured_type_at_offset
1329 (struct objfile *objfile,
1330 struct dwarf2_section_info *section, sect_offset offset);
1331
1332 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1333
1334 static void read_signatured_type (struct signatured_type *);
1335
1336 /* memory allocation interface */
1337
1338 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1339
1340 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1341
1342 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1343
1344 static void dwarf_decode_macros (struct line_header *, unsigned int,
1345 char *, bfd *, struct dwarf2_cu *,
1346 struct dwarf2_section_info *,
1347 int, const char *);
1348
1349 static int attr_form_is_block (struct attribute *);
1350
1351 static int attr_form_is_section_offset (struct attribute *);
1352
1353 static int attr_form_is_constant (struct attribute *);
1354
1355 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1356 struct dwarf2_loclist_baton *baton,
1357 struct attribute *attr);
1358
1359 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1360 struct symbol *sym,
1361 struct dwarf2_cu *cu);
1362
1363 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1364 gdb_byte *info_ptr,
1365 struct abbrev_info *abbrev);
1366
1367 static void free_stack_comp_unit (void *);
1368
1369 static hashval_t partial_die_hash (const void *item);
1370
1371 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1372
1373 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1374 (sect_offset offset, struct objfile *objfile);
1375
1376 static void init_one_comp_unit (struct dwarf2_cu *cu,
1377 struct dwarf2_per_cu_data *per_cu);
1378
1379 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1380 struct die_info *comp_unit_die,
1381 enum language pretend_language);
1382
1383 static void free_heap_comp_unit (void *);
1384
1385 static void free_cached_comp_units (void *);
1386
1387 static void age_cached_comp_units (void);
1388
1389 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1390
1391 static struct type *set_die_type (struct die_info *, struct type *,
1392 struct dwarf2_cu *);
1393
1394 static void create_all_comp_units (struct objfile *);
1395
1396 static int create_all_type_units (struct objfile *);
1397
1398 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1399 enum language);
1400
1401 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1402 enum language);
1403
1404 static void dwarf2_add_dependence (struct dwarf2_cu *,
1405 struct dwarf2_per_cu_data *);
1406
1407 static void dwarf2_mark (struct dwarf2_cu *);
1408
1409 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1410
1411 static struct type *get_die_type_at_offset (sect_offset,
1412 struct dwarf2_per_cu_data *per_cu);
1413
1414 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1415
1416 static void dwarf2_release_queue (void *dummy);
1417
1418 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1419 enum language pretend_language);
1420
1421 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1422 struct dwarf2_per_cu_data *per_cu,
1423 enum language pretend_language);
1424
1425 static void process_queue (void);
1426
1427 static void find_file_and_directory (struct die_info *die,
1428 struct dwarf2_cu *cu,
1429 char **name, char **comp_dir);
1430
1431 static char *file_full_name (int file, struct line_header *lh,
1432 const char *comp_dir);
1433
1434 static gdb_byte *read_and_check_comp_unit_head
1435 (struct comp_unit_head *header,
1436 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1437 int is_debug_types_section);
1438
1439 static void init_cutu_and_read_dies
1440 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1441 die_reader_func_ftype *die_reader_func, void *data);
1442
1443 static void init_cutu_and_read_dies_simple
1444 (struct dwarf2_per_cu_data *this_cu,
1445 die_reader_func_ftype *die_reader_func, void *data);
1446
1447 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1448
1449 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1450
1451 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1452
1453 static struct dwo_unit *lookup_dwo_comp_unit
1454 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1455
1456 static struct dwo_unit *lookup_dwo_type_unit
1457 (struct signatured_type *, char *, const char *);
1458
1459 static void free_dwo_file_cleanup (void *);
1460
1461 static void munmap_section_buffer (struct dwarf2_section_info *);
1462
1463 static void process_cu_includes (void);
1464
1465 #if WORDS_BIGENDIAN
1466
1467 /* Convert VALUE between big- and little-endian. */
1468 static offset_type
1469 byte_swap (offset_type value)
1470 {
1471 offset_type result;
1472
1473 result = (value & 0xff) << 24;
1474 result |= (value & 0xff00) << 8;
1475 result |= (value & 0xff0000) >> 8;
1476 result |= (value & 0xff000000) >> 24;
1477 return result;
1478 }
1479
1480 #define MAYBE_SWAP(V) byte_swap (V)
1481
1482 #else
1483 #define MAYBE_SWAP(V) (V)
1484 #endif /* WORDS_BIGENDIAN */
1485
1486 /* The suffix for an index file. */
1487 #define INDEX_SUFFIX ".gdb-index"
1488
1489 static const char *dwarf2_physname (char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
1492 /* Try to locate the sections we need for DWARF 2 debugging
1493 information and return true if we have enough to do something.
1494 NAMES points to the dwarf2 section names, or is NULL if the standard
1495 ELF names are used. */
1496
1497 int
1498 dwarf2_has_info (struct objfile *objfile,
1499 const struct dwarf2_debug_sections *names)
1500 {
1501 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1502 if (!dwarf2_per_objfile)
1503 {
1504 /* Initialize per-objfile state. */
1505 struct dwarf2_per_objfile *data
1506 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1507
1508 memset (data, 0, sizeof (*data));
1509 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1510 dwarf2_per_objfile = data;
1511
1512 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1513 (void *) names);
1514 dwarf2_per_objfile->objfile = objfile;
1515 }
1516 return (dwarf2_per_objfile->info.asection != NULL
1517 && dwarf2_per_objfile->abbrev.asection != NULL);
1518 }
1519
1520 /* When loading sections, we look either for uncompressed section or for
1521 compressed section names. */
1522
1523 static int
1524 section_is_p (const char *section_name,
1525 const struct dwarf2_section_names *names)
1526 {
1527 if (names->normal != NULL
1528 && strcmp (section_name, names->normal) == 0)
1529 return 1;
1530 if (names->compressed != NULL
1531 && strcmp (section_name, names->compressed) == 0)
1532 return 1;
1533 return 0;
1534 }
1535
1536 /* This function is mapped across the sections and remembers the
1537 offset and size of each of the debugging sections we are interested
1538 in. */
1539
1540 static void
1541 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1542 {
1543 const struct dwarf2_debug_sections *names;
1544
1545 if (vnames == NULL)
1546 names = &dwarf2_elf_names;
1547 else
1548 names = (const struct dwarf2_debug_sections *) vnames;
1549
1550 if (section_is_p (sectp->name, &names->info))
1551 {
1552 dwarf2_per_objfile->info.asection = sectp;
1553 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1554 }
1555 else if (section_is_p (sectp->name, &names->abbrev))
1556 {
1557 dwarf2_per_objfile->abbrev.asection = sectp;
1558 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1559 }
1560 else if (section_is_p (sectp->name, &names->line))
1561 {
1562 dwarf2_per_objfile->line.asection = sectp;
1563 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1564 }
1565 else if (section_is_p (sectp->name, &names->loc))
1566 {
1567 dwarf2_per_objfile->loc.asection = sectp;
1568 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1569 }
1570 else if (section_is_p (sectp->name, &names->macinfo))
1571 {
1572 dwarf2_per_objfile->macinfo.asection = sectp;
1573 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1574 }
1575 else if (section_is_p (sectp->name, &names->macro))
1576 {
1577 dwarf2_per_objfile->macro.asection = sectp;
1578 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1579 }
1580 else if (section_is_p (sectp->name, &names->str))
1581 {
1582 dwarf2_per_objfile->str.asection = sectp;
1583 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1584 }
1585 else if (section_is_p (sectp->name, &names->addr))
1586 {
1587 dwarf2_per_objfile->addr.asection = sectp;
1588 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1589 }
1590 else if (section_is_p (sectp->name, &names->frame))
1591 {
1592 dwarf2_per_objfile->frame.asection = sectp;
1593 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1594 }
1595 else if (section_is_p (sectp->name, &names->eh_frame))
1596 {
1597 flagword aflag = bfd_get_section_flags (abfd, sectp);
1598
1599 if (aflag & SEC_HAS_CONTENTS)
1600 {
1601 dwarf2_per_objfile->eh_frame.asection = sectp;
1602 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1603 }
1604 }
1605 else if (section_is_p (sectp->name, &names->ranges))
1606 {
1607 dwarf2_per_objfile->ranges.asection = sectp;
1608 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1609 }
1610 else if (section_is_p (sectp->name, &names->types))
1611 {
1612 struct dwarf2_section_info type_section;
1613
1614 memset (&type_section, 0, sizeof (type_section));
1615 type_section.asection = sectp;
1616 type_section.size = bfd_get_section_size (sectp);
1617
1618 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1619 &type_section);
1620 }
1621 else if (section_is_p (sectp->name, &names->gdb_index))
1622 {
1623 dwarf2_per_objfile->gdb_index.asection = sectp;
1624 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1625 }
1626
1627 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1628 && bfd_section_vma (abfd, sectp) == 0)
1629 dwarf2_per_objfile->has_section_at_zero = 1;
1630 }
1631
1632 /* Decompress a section that was compressed using zlib. Store the
1633 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1634
1635 static void
1636 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1637 gdb_byte **outbuf, bfd_size_type *outsize)
1638 {
1639 bfd *abfd = sectp->owner;
1640 #ifndef HAVE_ZLIB_H
1641 error (_("Support for zlib-compressed DWARF data (from '%s') "
1642 "is disabled in this copy of GDB"),
1643 bfd_get_filename (abfd));
1644 #else
1645 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1646 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1647 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1648 bfd_size_type uncompressed_size;
1649 gdb_byte *uncompressed_buffer;
1650 z_stream strm;
1651 int rc;
1652 int header_size = 12;
1653
1654 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1655 || bfd_bread (compressed_buffer,
1656 compressed_size, abfd) != compressed_size)
1657 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1658 bfd_get_filename (abfd));
1659
1660 /* Read the zlib header. In this case, it should be "ZLIB" followed
1661 by the uncompressed section size, 8 bytes in big-endian order. */
1662 if (compressed_size < header_size
1663 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1664 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1665 bfd_get_filename (abfd));
1666 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1667 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1668 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1669 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1670 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1671 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1672 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1673 uncompressed_size += compressed_buffer[11];
1674
1675 /* It is possible the section consists of several compressed
1676 buffers concatenated together, so we uncompress in a loop. */
1677 strm.zalloc = NULL;
1678 strm.zfree = NULL;
1679 strm.opaque = NULL;
1680 strm.avail_in = compressed_size - header_size;
1681 strm.next_in = (Bytef*) compressed_buffer + header_size;
1682 strm.avail_out = uncompressed_size;
1683 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1684 uncompressed_size);
1685 rc = inflateInit (&strm);
1686 while (strm.avail_in > 0)
1687 {
1688 if (rc != Z_OK)
1689 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1690 bfd_get_filename (abfd), rc);
1691 strm.next_out = ((Bytef*) uncompressed_buffer
1692 + (uncompressed_size - strm.avail_out));
1693 rc = inflate (&strm, Z_FINISH);
1694 if (rc != Z_STREAM_END)
1695 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1696 bfd_get_filename (abfd), rc);
1697 rc = inflateReset (&strm);
1698 }
1699 rc = inflateEnd (&strm);
1700 if (rc != Z_OK
1701 || strm.avail_out != 0)
1702 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1703 bfd_get_filename (abfd), rc);
1704
1705 do_cleanups (cleanup);
1706 *outbuf = uncompressed_buffer;
1707 *outsize = uncompressed_size;
1708 #endif
1709 }
1710
1711 /* A helper function that decides whether a section is empty,
1712 or not present. */
1713
1714 static int
1715 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1716 {
1717 return info->asection == NULL || info->size == 0;
1718 }
1719
1720 /* Read the contents of the section INFO.
1721 OBJFILE is the main object file, but not necessarily the file where
1722 the section comes from. E.g., for DWO files INFO->asection->owner
1723 is the bfd of the DWO file.
1724 If the section is compressed, uncompress it before returning. */
1725
1726 static void
1727 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1728 {
1729 asection *sectp = info->asection;
1730 bfd *abfd;
1731 gdb_byte *buf, *retbuf;
1732 unsigned char header[4];
1733
1734 if (info->readin)
1735 return;
1736 info->buffer = NULL;
1737 info->map_addr = NULL;
1738 info->readin = 1;
1739
1740 if (dwarf2_section_empty_p (info))
1741 return;
1742
1743 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1744 abfd = sectp->owner;
1745
1746 /* Check if the file has a 4-byte header indicating compression. */
1747 if (info->size > sizeof (header)
1748 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1749 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1750 {
1751 /* Upon decompression, update the buffer and its size. */
1752 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1753 {
1754 zlib_decompress_section (objfile, sectp, &info->buffer,
1755 &info->size);
1756 return;
1757 }
1758 }
1759
1760 #ifdef HAVE_MMAP
1761 if (pagesize == 0)
1762 pagesize = getpagesize ();
1763
1764 /* Only try to mmap sections which are large enough: we don't want to
1765 waste space due to fragmentation. Also, only try mmap for sections
1766 without relocations. */
1767
1768 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1769 {
1770 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1771 MAP_PRIVATE, sectp->filepos,
1772 &info->map_addr, &info->map_len);
1773
1774 if ((caddr_t)info->buffer != MAP_FAILED)
1775 {
1776 #if HAVE_POSIX_MADVISE
1777 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1778 #endif
1779 return;
1780 }
1781 }
1782 #endif
1783
1784 /* If we get here, we are a normal, not-compressed section. */
1785 info->buffer = buf
1786 = obstack_alloc (&objfile->objfile_obstack, info->size);
1787
1788 /* When debugging .o files, we may need to apply relocations; see
1789 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1790 We never compress sections in .o files, so we only need to
1791 try this when the section is not compressed. */
1792 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1793 if (retbuf != NULL)
1794 {
1795 info->buffer = retbuf;
1796 return;
1797 }
1798
1799 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1800 || bfd_bread (buf, info->size, abfd) != info->size)
1801 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1802 bfd_get_filename (abfd));
1803 }
1804
1805 /* A helper function that returns the size of a section in a safe way.
1806 If you are positive that the section has been read before using the
1807 size, then it is safe to refer to the dwarf2_section_info object's
1808 "size" field directly. In other cases, you must call this
1809 function, because for compressed sections the size field is not set
1810 correctly until the section has been read. */
1811
1812 static bfd_size_type
1813 dwarf2_section_size (struct objfile *objfile,
1814 struct dwarf2_section_info *info)
1815 {
1816 if (!info->readin)
1817 dwarf2_read_section (objfile, info);
1818 return info->size;
1819 }
1820
1821 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1822 SECTION_NAME. */
1823
1824 void
1825 dwarf2_get_section_info (struct objfile *objfile,
1826 enum dwarf2_section_enum sect,
1827 asection **sectp, gdb_byte **bufp,
1828 bfd_size_type *sizep)
1829 {
1830 struct dwarf2_per_objfile *data
1831 = objfile_data (objfile, dwarf2_objfile_data_key);
1832 struct dwarf2_section_info *info;
1833
1834 /* We may see an objfile without any DWARF, in which case we just
1835 return nothing. */
1836 if (data == NULL)
1837 {
1838 *sectp = NULL;
1839 *bufp = NULL;
1840 *sizep = 0;
1841 return;
1842 }
1843 switch (sect)
1844 {
1845 case DWARF2_DEBUG_FRAME:
1846 info = &data->frame;
1847 break;
1848 case DWARF2_EH_FRAME:
1849 info = &data->eh_frame;
1850 break;
1851 default:
1852 gdb_assert_not_reached ("unexpected section");
1853 }
1854
1855 dwarf2_read_section (objfile, info);
1856
1857 *sectp = info->asection;
1858 *bufp = info->buffer;
1859 *sizep = info->size;
1860 }
1861
1862 \f
1863 /* DWARF quick_symbols_functions support. */
1864
1865 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1866 unique line tables, so we maintain a separate table of all .debug_line
1867 derived entries to support the sharing.
1868 All the quick functions need is the list of file names. We discard the
1869 line_header when we're done and don't need to record it here. */
1870 struct quick_file_names
1871 {
1872 /* The offset in .debug_line of the line table. We hash on this. */
1873 unsigned int offset;
1874
1875 /* The number of entries in file_names, real_names. */
1876 unsigned int num_file_names;
1877
1878 /* The file names from the line table, after being run through
1879 file_full_name. */
1880 const char **file_names;
1881
1882 /* The file names from the line table after being run through
1883 gdb_realpath. These are computed lazily. */
1884 const char **real_names;
1885 };
1886
1887 /* When using the index (and thus not using psymtabs), each CU has an
1888 object of this type. This is used to hold information needed by
1889 the various "quick" methods. */
1890 struct dwarf2_per_cu_quick_data
1891 {
1892 /* The file table. This can be NULL if there was no file table
1893 or it's currently not read in.
1894 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1895 struct quick_file_names *file_names;
1896
1897 /* The corresponding symbol table. This is NULL if symbols for this
1898 CU have not yet been read. */
1899 struct symtab *symtab;
1900
1901 /* A temporary mark bit used when iterating over all CUs in
1902 expand_symtabs_matching. */
1903 unsigned int mark : 1;
1904
1905 /* True if we've tried to read the file table and found there isn't one.
1906 There will be no point in trying to read it again next time. */
1907 unsigned int no_file_data : 1;
1908 };
1909
1910 /* Hash function for a quick_file_names. */
1911
1912 static hashval_t
1913 hash_file_name_entry (const void *e)
1914 {
1915 const struct quick_file_names *file_data = e;
1916
1917 return file_data->offset;
1918 }
1919
1920 /* Equality function for a quick_file_names. */
1921
1922 static int
1923 eq_file_name_entry (const void *a, const void *b)
1924 {
1925 const struct quick_file_names *ea = a;
1926 const struct quick_file_names *eb = b;
1927
1928 return ea->offset == eb->offset;
1929 }
1930
1931 /* Delete function for a quick_file_names. */
1932
1933 static void
1934 delete_file_name_entry (void *e)
1935 {
1936 struct quick_file_names *file_data = e;
1937 int i;
1938
1939 for (i = 0; i < file_data->num_file_names; ++i)
1940 {
1941 xfree ((void*) file_data->file_names[i]);
1942 if (file_data->real_names)
1943 xfree ((void*) file_data->real_names[i]);
1944 }
1945
1946 /* The space for the struct itself lives on objfile_obstack,
1947 so we don't free it here. */
1948 }
1949
1950 /* Create a quick_file_names hash table. */
1951
1952 static htab_t
1953 create_quick_file_names_table (unsigned int nr_initial_entries)
1954 {
1955 return htab_create_alloc (nr_initial_entries,
1956 hash_file_name_entry, eq_file_name_entry,
1957 delete_file_name_entry, xcalloc, xfree);
1958 }
1959
1960 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1961 have to be created afterwards. You should call age_cached_comp_units after
1962 processing PER_CU->CU. dw2_setup must have been already called. */
1963
1964 static void
1965 load_cu (struct dwarf2_per_cu_data *per_cu)
1966 {
1967 if (per_cu->is_debug_types)
1968 load_full_type_unit (per_cu);
1969 else
1970 load_full_comp_unit (per_cu, language_minimal);
1971
1972 gdb_assert (per_cu->cu != NULL);
1973
1974 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1975 }
1976
1977 /* Read in the symbols for PER_CU. */
1978
1979 static void
1980 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1981 {
1982 struct cleanup *back_to;
1983
1984 back_to = make_cleanup (dwarf2_release_queue, NULL);
1985
1986 if (dwarf2_per_objfile->using_index
1987 ? per_cu->v.quick->symtab == NULL
1988 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1989 {
1990 queue_comp_unit (per_cu, language_minimal);
1991 load_cu (per_cu);
1992 }
1993
1994 process_queue ();
1995
1996 /* Age the cache, releasing compilation units that have not
1997 been used recently. */
1998 age_cached_comp_units ();
1999
2000 do_cleanups (back_to);
2001 }
2002
2003 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2004 the objfile from which this CU came. Returns the resulting symbol
2005 table. */
2006
2007 static struct symtab *
2008 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2009 {
2010 gdb_assert (dwarf2_per_objfile->using_index);
2011 if (!per_cu->v.quick->symtab)
2012 {
2013 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2014 increment_reading_symtab ();
2015 dw2_do_instantiate_symtab (per_cu);
2016 process_cu_includes ();
2017 do_cleanups (back_to);
2018 }
2019 return per_cu->v.quick->symtab;
2020 }
2021
2022 /* Return the CU given its index. */
2023
2024 static struct dwarf2_per_cu_data *
2025 dw2_get_cu (int index)
2026 {
2027 if (index >= dwarf2_per_objfile->n_comp_units)
2028 {
2029 index -= dwarf2_per_objfile->n_comp_units;
2030 return dwarf2_per_objfile->all_type_units[index];
2031 }
2032 return dwarf2_per_objfile->all_comp_units[index];
2033 }
2034
2035 /* A helper function that knows how to read a 64-bit value in a way
2036 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2037 otherwise. */
2038
2039 static int
2040 extract_cu_value (const char *bytes, ULONGEST *result)
2041 {
2042 if (sizeof (ULONGEST) < 8)
2043 {
2044 int i;
2045
2046 /* Ignore the upper 4 bytes if they are all zero. */
2047 for (i = 0; i < 4; ++i)
2048 if (bytes[i + 4] != 0)
2049 return 0;
2050
2051 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2052 }
2053 else
2054 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2055 return 1;
2056 }
2057
2058 /* Read the CU list from the mapped index, and use it to create all
2059 the CU objects for this objfile. Return 0 if something went wrong,
2060 1 if everything went ok. */
2061
2062 static int
2063 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2064 offset_type cu_list_elements)
2065 {
2066 offset_type i;
2067
2068 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2069 dwarf2_per_objfile->all_comp_units
2070 = obstack_alloc (&objfile->objfile_obstack,
2071 dwarf2_per_objfile->n_comp_units
2072 * sizeof (struct dwarf2_per_cu_data *));
2073
2074 for (i = 0; i < cu_list_elements; i += 2)
2075 {
2076 struct dwarf2_per_cu_data *the_cu;
2077 ULONGEST offset, length;
2078
2079 if (!extract_cu_value (cu_list, &offset)
2080 || !extract_cu_value (cu_list + 8, &length))
2081 return 0;
2082 cu_list += 2 * 8;
2083
2084 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2085 struct dwarf2_per_cu_data);
2086 the_cu->offset.sect_off = offset;
2087 the_cu->length = length;
2088 the_cu->objfile = objfile;
2089 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2090 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2091 struct dwarf2_per_cu_quick_data);
2092 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2093 }
2094
2095 return 1;
2096 }
2097
2098 /* Create the signatured type hash table from the index. */
2099
2100 static int
2101 create_signatured_type_table_from_index (struct objfile *objfile,
2102 struct dwarf2_section_info *section,
2103 const gdb_byte *bytes,
2104 offset_type elements)
2105 {
2106 offset_type i;
2107 htab_t sig_types_hash;
2108
2109 dwarf2_per_objfile->n_type_units = elements / 3;
2110 dwarf2_per_objfile->all_type_units
2111 = obstack_alloc (&objfile->objfile_obstack,
2112 dwarf2_per_objfile->n_type_units
2113 * sizeof (struct dwarf2_per_cu_data *));
2114
2115 sig_types_hash = allocate_signatured_type_table (objfile);
2116
2117 for (i = 0; i < elements; i += 3)
2118 {
2119 struct signatured_type *sig_type;
2120 ULONGEST offset, type_offset_in_tu, signature;
2121 void **slot;
2122
2123 if (!extract_cu_value (bytes, &offset)
2124 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2125 return 0;
2126 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2127 bytes += 3 * 8;
2128
2129 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2130 struct signatured_type);
2131 sig_type->signature = signature;
2132 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2133 sig_type->per_cu.is_debug_types = 1;
2134 sig_type->per_cu.info_or_types_section = section;
2135 sig_type->per_cu.offset.sect_off = offset;
2136 sig_type->per_cu.objfile = objfile;
2137 sig_type->per_cu.v.quick
2138 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2139 struct dwarf2_per_cu_quick_data);
2140
2141 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2142 *slot = sig_type;
2143
2144 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2145 }
2146
2147 dwarf2_per_objfile->signatured_types = sig_types_hash;
2148
2149 return 1;
2150 }
2151
2152 /* Read the address map data from the mapped index, and use it to
2153 populate the objfile's psymtabs_addrmap. */
2154
2155 static void
2156 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2157 {
2158 const gdb_byte *iter, *end;
2159 struct obstack temp_obstack;
2160 struct addrmap *mutable_map;
2161 struct cleanup *cleanup;
2162 CORE_ADDR baseaddr;
2163
2164 obstack_init (&temp_obstack);
2165 cleanup = make_cleanup_obstack_free (&temp_obstack);
2166 mutable_map = addrmap_create_mutable (&temp_obstack);
2167
2168 iter = index->address_table;
2169 end = iter + index->address_table_size;
2170
2171 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2172
2173 while (iter < end)
2174 {
2175 ULONGEST hi, lo, cu_index;
2176 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2177 iter += 8;
2178 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2179 iter += 8;
2180 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2181 iter += 4;
2182
2183 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2184 dw2_get_cu (cu_index));
2185 }
2186
2187 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2188 &objfile->objfile_obstack);
2189 do_cleanups (cleanup);
2190 }
2191
2192 /* The hash function for strings in the mapped index. This is the same as
2193 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2194 implementation. This is necessary because the hash function is tied to the
2195 format of the mapped index file. The hash values do not have to match with
2196 SYMBOL_HASH_NEXT.
2197
2198 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2199
2200 static hashval_t
2201 mapped_index_string_hash (int index_version, const void *p)
2202 {
2203 const unsigned char *str = (const unsigned char *) p;
2204 hashval_t r = 0;
2205 unsigned char c;
2206
2207 while ((c = *str++) != 0)
2208 {
2209 if (index_version >= 5)
2210 c = tolower (c);
2211 r = r * 67 + c - 113;
2212 }
2213
2214 return r;
2215 }
2216
2217 /* Find a slot in the mapped index INDEX for the object named NAME.
2218 If NAME is found, set *VEC_OUT to point to the CU vector in the
2219 constant pool and return 1. If NAME cannot be found, return 0. */
2220
2221 static int
2222 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2223 offset_type **vec_out)
2224 {
2225 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2226 offset_type hash;
2227 offset_type slot, step;
2228 int (*cmp) (const char *, const char *);
2229
2230 if (current_language->la_language == language_cplus
2231 || current_language->la_language == language_java
2232 || current_language->la_language == language_fortran)
2233 {
2234 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2235 not contain any. */
2236 const char *paren = strchr (name, '(');
2237
2238 if (paren)
2239 {
2240 char *dup;
2241
2242 dup = xmalloc (paren - name + 1);
2243 memcpy (dup, name, paren - name);
2244 dup[paren - name] = 0;
2245
2246 make_cleanup (xfree, dup);
2247 name = dup;
2248 }
2249 }
2250
2251 /* Index version 4 did not support case insensitive searches. But the
2252 indices for case insensitive languages are built in lowercase, therefore
2253 simulate our NAME being searched is also lowercased. */
2254 hash = mapped_index_string_hash ((index->version == 4
2255 && case_sensitivity == case_sensitive_off
2256 ? 5 : index->version),
2257 name);
2258
2259 slot = hash & (index->symbol_table_slots - 1);
2260 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2261 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2262
2263 for (;;)
2264 {
2265 /* Convert a slot number to an offset into the table. */
2266 offset_type i = 2 * slot;
2267 const char *str;
2268 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2269 {
2270 do_cleanups (back_to);
2271 return 0;
2272 }
2273
2274 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2275 if (!cmp (name, str))
2276 {
2277 *vec_out = (offset_type *) (index->constant_pool
2278 + MAYBE_SWAP (index->symbol_table[i + 1]));
2279 do_cleanups (back_to);
2280 return 1;
2281 }
2282
2283 slot = (slot + step) & (index->symbol_table_slots - 1);
2284 }
2285 }
2286
2287 /* Read the index file. If everything went ok, initialize the "quick"
2288 elements of all the CUs and return 1. Otherwise, return 0. */
2289
2290 static int
2291 dwarf2_read_index (struct objfile *objfile)
2292 {
2293 char *addr;
2294 struct mapped_index *map;
2295 offset_type *metadata;
2296 const gdb_byte *cu_list;
2297 const gdb_byte *types_list = NULL;
2298 offset_type version, cu_list_elements;
2299 offset_type types_list_elements = 0;
2300 int i;
2301
2302 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2303 return 0;
2304
2305 /* Older elfutils strip versions could keep the section in the main
2306 executable while splitting it for the separate debug info file. */
2307 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2308 & SEC_HAS_CONTENTS) == 0)
2309 return 0;
2310
2311 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2312
2313 addr = dwarf2_per_objfile->gdb_index.buffer;
2314 /* Version check. */
2315 version = MAYBE_SWAP (*(offset_type *) addr);
2316 /* Versions earlier than 3 emitted every copy of a psymbol. This
2317 causes the index to behave very poorly for certain requests. Version 3
2318 contained incomplete addrmap. So, it seems better to just ignore such
2319 indices. */
2320 if (version < 4)
2321 {
2322 static int warning_printed = 0;
2323 if (!warning_printed)
2324 {
2325 warning (_("Skipping obsolete .gdb_index section in %s."),
2326 objfile->name);
2327 warning_printed = 1;
2328 }
2329 return 0;
2330 }
2331 /* Index version 4 uses a different hash function than index version
2332 5 and later.
2333
2334 Versions earlier than 6 did not emit psymbols for inlined
2335 functions. Using these files will cause GDB not to be able to
2336 set breakpoints on inlined functions by name, so we ignore these
2337 indices unless the --use-deprecated-index-sections command line
2338 option was supplied. */
2339 if (version < 6 && !use_deprecated_index_sections)
2340 {
2341 static int warning_printed = 0;
2342 if (!warning_printed)
2343 {
2344 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2345 "--use-deprecated-index-sections to use them anyway"),
2346 objfile->name);
2347 warning_printed = 1;
2348 }
2349 return 0;
2350 }
2351 /* Indexes with higher version than the one supported by GDB may be no
2352 longer backward compatible. */
2353 if (version > 6)
2354 return 0;
2355
2356 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2357 map->version = version;
2358 map->total_size = dwarf2_per_objfile->gdb_index.size;
2359
2360 metadata = (offset_type *) (addr + sizeof (offset_type));
2361
2362 i = 0;
2363 cu_list = addr + MAYBE_SWAP (metadata[i]);
2364 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2365 / 8);
2366 ++i;
2367
2368 types_list = addr + MAYBE_SWAP (metadata[i]);
2369 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2370 - MAYBE_SWAP (metadata[i]))
2371 / 8);
2372 ++i;
2373
2374 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2375 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2376 - MAYBE_SWAP (metadata[i]));
2377 ++i;
2378
2379 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2380 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2381 - MAYBE_SWAP (metadata[i]))
2382 / (2 * sizeof (offset_type)));
2383 ++i;
2384
2385 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2386
2387 /* Don't use the index if it's empty. */
2388 if (map->symbol_table_slots == 0)
2389 return 0;
2390
2391 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2392 return 0;
2393
2394 if (types_list_elements)
2395 {
2396 struct dwarf2_section_info *section;
2397
2398 /* We can only handle a single .debug_types when we have an
2399 index. */
2400 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2401 return 0;
2402
2403 section = VEC_index (dwarf2_section_info_def,
2404 dwarf2_per_objfile->types, 0);
2405
2406 if (!create_signatured_type_table_from_index (objfile, section,
2407 types_list,
2408 types_list_elements))
2409 return 0;
2410 }
2411
2412 create_addrmap_from_index (objfile, map);
2413
2414 dwarf2_per_objfile->index_table = map;
2415 dwarf2_per_objfile->using_index = 1;
2416 dwarf2_per_objfile->quick_file_names_table =
2417 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2418
2419 return 1;
2420 }
2421
2422 /* A helper for the "quick" functions which sets the global
2423 dwarf2_per_objfile according to OBJFILE. */
2424
2425 static void
2426 dw2_setup (struct objfile *objfile)
2427 {
2428 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2429 gdb_assert (dwarf2_per_objfile);
2430 }
2431
2432 /* die_reader_func for dw2_get_file_names. */
2433
2434 static void
2435 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2436 gdb_byte *info_ptr,
2437 struct die_info *comp_unit_die,
2438 int has_children,
2439 void *data)
2440 {
2441 struct dwarf2_cu *cu = reader->cu;
2442 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2443 struct objfile *objfile = dwarf2_per_objfile->objfile;
2444 struct line_header *lh;
2445 struct attribute *attr;
2446 int i;
2447 char *name, *comp_dir;
2448 void **slot;
2449 struct quick_file_names *qfn;
2450 unsigned int line_offset;
2451
2452 /* Our callers never want to match partial units -- instead they
2453 will match the enclosing full CU. */
2454 if (comp_unit_die->tag == DW_TAG_partial_unit)
2455 {
2456 this_cu->v.quick->no_file_data = 1;
2457 return;
2458 }
2459
2460 lh = NULL;
2461 slot = NULL;
2462 line_offset = 0;
2463
2464 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2465 if (attr)
2466 {
2467 struct quick_file_names find_entry;
2468
2469 line_offset = DW_UNSND (attr);
2470
2471 /* We may have already read in this line header (TU line header sharing).
2472 If we have we're done. */
2473 find_entry.offset = line_offset;
2474 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2475 &find_entry, INSERT);
2476 if (*slot != NULL)
2477 {
2478 this_cu->v.quick->file_names = *slot;
2479 return;
2480 }
2481
2482 lh = dwarf_decode_line_header (line_offset, cu);
2483 }
2484 if (lh == NULL)
2485 {
2486 this_cu->v.quick->no_file_data = 1;
2487 return;
2488 }
2489
2490 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2491 qfn->offset = line_offset;
2492 gdb_assert (slot != NULL);
2493 *slot = qfn;
2494
2495 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2496
2497 qfn->num_file_names = lh->num_file_names;
2498 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2499 lh->num_file_names * sizeof (char *));
2500 for (i = 0; i < lh->num_file_names; ++i)
2501 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2502 qfn->real_names = NULL;
2503
2504 free_line_header (lh);
2505
2506 this_cu->v.quick->file_names = qfn;
2507 }
2508
2509 /* A helper for the "quick" functions which attempts to read the line
2510 table for THIS_CU. */
2511
2512 static struct quick_file_names *
2513 dw2_get_file_names (struct objfile *objfile,
2514 struct dwarf2_per_cu_data *this_cu)
2515 {
2516 if (this_cu->v.quick->file_names != NULL)
2517 return this_cu->v.quick->file_names;
2518 /* If we know there is no line data, no point in looking again. */
2519 if (this_cu->v.quick->no_file_data)
2520 return NULL;
2521
2522 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2523 in the stub for CUs, there's is no need to lookup the DWO file.
2524 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2525 DWO file. */
2526 if (this_cu->is_debug_types)
2527 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2528 else
2529 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2530
2531 if (this_cu->v.quick->no_file_data)
2532 return NULL;
2533 return this_cu->v.quick->file_names;
2534 }
2535
2536 /* A helper for the "quick" functions which computes and caches the
2537 real path for a given file name from the line table. */
2538
2539 static const char *
2540 dw2_get_real_path (struct objfile *objfile,
2541 struct quick_file_names *qfn, int index)
2542 {
2543 if (qfn->real_names == NULL)
2544 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2545 qfn->num_file_names, sizeof (char *));
2546
2547 if (qfn->real_names[index] == NULL)
2548 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2549
2550 return qfn->real_names[index];
2551 }
2552
2553 static struct symtab *
2554 dw2_find_last_source_symtab (struct objfile *objfile)
2555 {
2556 int index;
2557
2558 dw2_setup (objfile);
2559 index = dwarf2_per_objfile->n_comp_units - 1;
2560 return dw2_instantiate_symtab (dw2_get_cu (index));
2561 }
2562
2563 /* Traversal function for dw2_forget_cached_source_info. */
2564
2565 static int
2566 dw2_free_cached_file_names (void **slot, void *info)
2567 {
2568 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2569
2570 if (file_data->real_names)
2571 {
2572 int i;
2573
2574 for (i = 0; i < file_data->num_file_names; ++i)
2575 {
2576 xfree ((void*) file_data->real_names[i]);
2577 file_data->real_names[i] = NULL;
2578 }
2579 }
2580
2581 return 1;
2582 }
2583
2584 static void
2585 dw2_forget_cached_source_info (struct objfile *objfile)
2586 {
2587 dw2_setup (objfile);
2588
2589 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2590 dw2_free_cached_file_names, NULL);
2591 }
2592
2593 /* Helper function for dw2_map_symtabs_matching_filename that expands
2594 the symtabs and calls the iterator. */
2595
2596 static int
2597 dw2_map_expand_apply (struct objfile *objfile,
2598 struct dwarf2_per_cu_data *per_cu,
2599 const char *name,
2600 const char *full_path, const char *real_path,
2601 int (*callback) (struct symtab *, void *),
2602 void *data)
2603 {
2604 struct symtab *last_made = objfile->symtabs;
2605
2606 /* Don't visit already-expanded CUs. */
2607 if (per_cu->v.quick->symtab)
2608 return 0;
2609
2610 /* This may expand more than one symtab, and we want to iterate over
2611 all of them. */
2612 dw2_instantiate_symtab (per_cu);
2613
2614 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2615 objfile->symtabs, last_made);
2616 }
2617
2618 /* Implementation of the map_symtabs_matching_filename method. */
2619
2620 static int
2621 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2622 const char *full_path, const char *real_path,
2623 int (*callback) (struct symtab *, void *),
2624 void *data)
2625 {
2626 int i;
2627 const char *name_basename = lbasename (name);
2628 int name_len = strlen (name);
2629 int is_abs = IS_ABSOLUTE_PATH (name);
2630
2631 dw2_setup (objfile);
2632
2633 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2634 + dwarf2_per_objfile->n_type_units); ++i)
2635 {
2636 int j;
2637 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2638 struct quick_file_names *file_data;
2639
2640 /* We only need to look at symtabs not already expanded. */
2641 if (per_cu->v.quick->symtab)
2642 continue;
2643
2644 file_data = dw2_get_file_names (objfile, per_cu);
2645 if (file_data == NULL)
2646 continue;
2647
2648 for (j = 0; j < file_data->num_file_names; ++j)
2649 {
2650 const char *this_name = file_data->file_names[j];
2651
2652 if (FILENAME_CMP (name, this_name) == 0
2653 || (!is_abs && compare_filenames_for_search (this_name,
2654 name, name_len)))
2655 {
2656 if (dw2_map_expand_apply (objfile, per_cu,
2657 name, full_path, real_path,
2658 callback, data))
2659 return 1;
2660 }
2661
2662 /* Before we invoke realpath, which can get expensive when many
2663 files are involved, do a quick comparison of the basenames. */
2664 if (! basenames_may_differ
2665 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2666 continue;
2667
2668 if (full_path != NULL)
2669 {
2670 const char *this_real_name = dw2_get_real_path (objfile,
2671 file_data, j);
2672
2673 if (this_real_name != NULL
2674 && (FILENAME_CMP (full_path, this_real_name) == 0
2675 || (!is_abs
2676 && compare_filenames_for_search (this_real_name,
2677 name, name_len))))
2678 {
2679 if (dw2_map_expand_apply (objfile, per_cu,
2680 name, full_path, real_path,
2681 callback, data))
2682 return 1;
2683 }
2684 }
2685
2686 if (real_path != NULL)
2687 {
2688 const char *this_real_name = dw2_get_real_path (objfile,
2689 file_data, j);
2690
2691 if (this_real_name != NULL
2692 && (FILENAME_CMP (real_path, this_real_name) == 0
2693 || (!is_abs
2694 && compare_filenames_for_search (this_real_name,
2695 name, name_len))))
2696 {
2697 if (dw2_map_expand_apply (objfile, per_cu,
2698 name, full_path, real_path,
2699 callback, data))
2700 return 1;
2701 }
2702 }
2703 }
2704 }
2705
2706 return 0;
2707 }
2708
2709 static struct symtab *
2710 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2711 const char *name, domain_enum domain)
2712 {
2713 /* We do all the work in the pre_expand_symtabs_matching hook
2714 instead. */
2715 return NULL;
2716 }
2717
2718 /* A helper function that expands all symtabs that hold an object
2719 named NAME. */
2720
2721 static void
2722 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2723 {
2724 dw2_setup (objfile);
2725
2726 /* index_table is NULL if OBJF_READNOW. */
2727 if (dwarf2_per_objfile->index_table)
2728 {
2729 offset_type *vec;
2730
2731 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2732 name, &vec))
2733 {
2734 offset_type i, len = MAYBE_SWAP (*vec);
2735 for (i = 0; i < len; ++i)
2736 {
2737 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2738 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2739
2740 dw2_instantiate_symtab (per_cu);
2741 }
2742 }
2743 }
2744 }
2745
2746 static void
2747 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2748 enum block_enum block_kind, const char *name,
2749 domain_enum domain)
2750 {
2751 dw2_do_expand_symtabs_matching (objfile, name);
2752 }
2753
2754 static void
2755 dw2_print_stats (struct objfile *objfile)
2756 {
2757 int i, count;
2758
2759 dw2_setup (objfile);
2760 count = 0;
2761 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2762 + dwarf2_per_objfile->n_type_units); ++i)
2763 {
2764 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2765
2766 if (!per_cu->v.quick->symtab)
2767 ++count;
2768 }
2769 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2770 }
2771
2772 static void
2773 dw2_dump (struct objfile *objfile)
2774 {
2775 /* Nothing worth printing. */
2776 }
2777
2778 static void
2779 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2780 struct section_offsets *delta)
2781 {
2782 /* There's nothing to relocate here. */
2783 }
2784
2785 static void
2786 dw2_expand_symtabs_for_function (struct objfile *objfile,
2787 const char *func_name)
2788 {
2789 dw2_do_expand_symtabs_matching (objfile, func_name);
2790 }
2791
2792 static void
2793 dw2_expand_all_symtabs (struct objfile *objfile)
2794 {
2795 int i;
2796
2797 dw2_setup (objfile);
2798
2799 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2800 + dwarf2_per_objfile->n_type_units); ++i)
2801 {
2802 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2803
2804 dw2_instantiate_symtab (per_cu);
2805 }
2806 }
2807
2808 static void
2809 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2810 const char *filename)
2811 {
2812 int i;
2813
2814 dw2_setup (objfile);
2815
2816 /* We don't need to consider type units here.
2817 This is only called for examining code, e.g. expand_line_sal.
2818 There can be an order of magnitude (or more) more type units
2819 than comp units, and we avoid them if we can. */
2820
2821 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2822 {
2823 int j;
2824 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2825 struct quick_file_names *file_data;
2826
2827 /* We only need to look at symtabs not already expanded. */
2828 if (per_cu->v.quick->symtab)
2829 continue;
2830
2831 file_data = dw2_get_file_names (objfile, per_cu);
2832 if (file_data == NULL)
2833 continue;
2834
2835 for (j = 0; j < file_data->num_file_names; ++j)
2836 {
2837 const char *this_name = file_data->file_names[j];
2838 if (FILENAME_CMP (this_name, filename) == 0)
2839 {
2840 dw2_instantiate_symtab (per_cu);
2841 break;
2842 }
2843 }
2844 }
2845 }
2846
2847 /* A helper function for dw2_find_symbol_file that finds the primary
2848 file name for a given CU. This is a die_reader_func. */
2849
2850 static void
2851 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2852 gdb_byte *info_ptr,
2853 struct die_info *comp_unit_die,
2854 int has_children,
2855 void *data)
2856 {
2857 const char **result_ptr = data;
2858 struct dwarf2_cu *cu = reader->cu;
2859 struct attribute *attr;
2860
2861 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2862 if (attr == NULL)
2863 *result_ptr = NULL;
2864 else
2865 *result_ptr = DW_STRING (attr);
2866 }
2867
2868 static const char *
2869 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2870 {
2871 struct dwarf2_per_cu_data *per_cu;
2872 offset_type *vec;
2873 struct quick_file_names *file_data;
2874 const char *filename;
2875
2876 dw2_setup (objfile);
2877
2878 /* index_table is NULL if OBJF_READNOW. */
2879 if (!dwarf2_per_objfile->index_table)
2880 {
2881 struct symtab *s;
2882
2883 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2884 {
2885 struct blockvector *bv = BLOCKVECTOR (s);
2886 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2887 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2888
2889 if (sym)
2890 return sym->symtab->filename;
2891 }
2892 return NULL;
2893 }
2894
2895 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2896 name, &vec))
2897 return NULL;
2898
2899 /* Note that this just looks at the very first one named NAME -- but
2900 actually we are looking for a function. find_main_filename
2901 should be rewritten so that it doesn't require a custom hook. It
2902 could just use the ordinary symbol tables. */
2903 /* vec[0] is the length, which must always be >0. */
2904 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2905
2906 if (per_cu->v.quick->symtab != NULL)
2907 return per_cu->v.quick->symtab->filename;
2908
2909 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
2910 &filename);
2911
2912 return filename;
2913 }
2914
2915 static void
2916 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2917 struct objfile *objfile, int global,
2918 int (*callback) (struct block *,
2919 struct symbol *, void *),
2920 void *data, symbol_compare_ftype *match,
2921 symbol_compare_ftype *ordered_compare)
2922 {
2923 /* Currently unimplemented; used for Ada. The function can be called if the
2924 current language is Ada for a non-Ada objfile using GNU index. As Ada
2925 does not look for non-Ada symbols this function should just return. */
2926 }
2927
2928 static void
2929 dw2_expand_symtabs_matching
2930 (struct objfile *objfile,
2931 int (*file_matcher) (const char *, void *),
2932 int (*name_matcher) (const char *, void *),
2933 enum search_domain kind,
2934 void *data)
2935 {
2936 int i;
2937 offset_type iter;
2938 struct mapped_index *index;
2939
2940 dw2_setup (objfile);
2941
2942 /* index_table is NULL if OBJF_READNOW. */
2943 if (!dwarf2_per_objfile->index_table)
2944 return;
2945 index = dwarf2_per_objfile->index_table;
2946
2947 if (file_matcher != NULL)
2948 {
2949 struct cleanup *cleanup;
2950 htab_t visited_found, visited_not_found;
2951
2952 visited_found = htab_create_alloc (10,
2953 htab_hash_pointer, htab_eq_pointer,
2954 NULL, xcalloc, xfree);
2955 cleanup = make_cleanup_htab_delete (visited_found);
2956 visited_not_found = htab_create_alloc (10,
2957 htab_hash_pointer, htab_eq_pointer,
2958 NULL, xcalloc, xfree);
2959 make_cleanup_htab_delete (visited_not_found);
2960
2961 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2962 + dwarf2_per_objfile->n_type_units); ++i)
2963 {
2964 int j;
2965 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2966 struct quick_file_names *file_data;
2967 void **slot;
2968
2969 per_cu->v.quick->mark = 0;
2970
2971 /* We only need to look at symtabs not already expanded. */
2972 if (per_cu->v.quick->symtab)
2973 continue;
2974
2975 file_data = dw2_get_file_names (objfile, per_cu);
2976 if (file_data == NULL)
2977 continue;
2978
2979 if (htab_find (visited_not_found, file_data) != NULL)
2980 continue;
2981 else if (htab_find (visited_found, file_data) != NULL)
2982 {
2983 per_cu->v.quick->mark = 1;
2984 continue;
2985 }
2986
2987 for (j = 0; j < file_data->num_file_names; ++j)
2988 {
2989 if (file_matcher (file_data->file_names[j], data))
2990 {
2991 per_cu->v.quick->mark = 1;
2992 break;
2993 }
2994 }
2995
2996 slot = htab_find_slot (per_cu->v.quick->mark
2997 ? visited_found
2998 : visited_not_found,
2999 file_data, INSERT);
3000 *slot = file_data;
3001 }
3002
3003 do_cleanups (cleanup);
3004 }
3005
3006 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3007 {
3008 offset_type idx = 2 * iter;
3009 const char *name;
3010 offset_type *vec, vec_len, vec_idx;
3011
3012 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3013 continue;
3014
3015 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3016
3017 if (! (*name_matcher) (name, data))
3018 continue;
3019
3020 /* The name was matched, now expand corresponding CUs that were
3021 marked. */
3022 vec = (offset_type *) (index->constant_pool
3023 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3024 vec_len = MAYBE_SWAP (vec[0]);
3025 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3026 {
3027 struct dwarf2_per_cu_data *per_cu;
3028
3029 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
3030 if (file_matcher == NULL || per_cu->v.quick->mark)
3031 dw2_instantiate_symtab (per_cu);
3032 }
3033 }
3034 }
3035
3036 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3037 symtab. */
3038
3039 static struct symtab *
3040 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3041 {
3042 int i;
3043
3044 if (BLOCKVECTOR (symtab) != NULL
3045 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3046 return symtab;
3047
3048 if (symtab->includes == NULL)
3049 return NULL;
3050
3051 for (i = 0; symtab->includes[i]; ++i)
3052 {
3053 struct symtab *s = symtab->includes[i];
3054
3055 s = recursively_find_pc_sect_symtab (s, pc);
3056 if (s != NULL)
3057 return s;
3058 }
3059
3060 return NULL;
3061 }
3062
3063 static struct symtab *
3064 dw2_find_pc_sect_symtab (struct objfile *objfile,
3065 struct minimal_symbol *msymbol,
3066 CORE_ADDR pc,
3067 struct obj_section *section,
3068 int warn_if_readin)
3069 {
3070 struct dwarf2_per_cu_data *data;
3071 struct symtab *result;
3072
3073 dw2_setup (objfile);
3074
3075 if (!objfile->psymtabs_addrmap)
3076 return NULL;
3077
3078 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3079 if (!data)
3080 return NULL;
3081
3082 if (warn_if_readin && data->v.quick->symtab)
3083 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3084 paddress (get_objfile_arch (objfile), pc));
3085
3086 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3087 gdb_assert (result != NULL);
3088 return result;
3089 }
3090
3091 static void
3092 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3093 void *data, int need_fullname)
3094 {
3095 int i;
3096 struct cleanup *cleanup;
3097 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3098 NULL, xcalloc, xfree);
3099
3100 cleanup = make_cleanup_htab_delete (visited);
3101 dw2_setup (objfile);
3102
3103 /* We can ignore file names coming from already-expanded CUs. */
3104 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3105 + dwarf2_per_objfile->n_type_units); ++i)
3106 {
3107 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3108
3109 if (per_cu->v.quick->symtab)
3110 {
3111 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3112 INSERT);
3113
3114 *slot = per_cu->v.quick->file_names;
3115 }
3116 }
3117
3118 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3119 + dwarf2_per_objfile->n_type_units); ++i)
3120 {
3121 int j;
3122 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3123 struct quick_file_names *file_data;
3124 void **slot;
3125
3126 /* We only need to look at symtabs not already expanded. */
3127 if (per_cu->v.quick->symtab)
3128 continue;
3129
3130 file_data = dw2_get_file_names (objfile, per_cu);
3131 if (file_data == NULL)
3132 continue;
3133
3134 slot = htab_find_slot (visited, file_data, INSERT);
3135 if (*slot)
3136 {
3137 /* Already visited. */
3138 continue;
3139 }
3140 *slot = file_data;
3141
3142 for (j = 0; j < file_data->num_file_names; ++j)
3143 {
3144 const char *this_real_name;
3145
3146 if (need_fullname)
3147 this_real_name = dw2_get_real_path (objfile, file_data, j);
3148 else
3149 this_real_name = NULL;
3150 (*fun) (file_data->file_names[j], this_real_name, data);
3151 }
3152 }
3153
3154 do_cleanups (cleanup);
3155 }
3156
3157 static int
3158 dw2_has_symbols (struct objfile *objfile)
3159 {
3160 return 1;
3161 }
3162
3163 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3164 {
3165 dw2_has_symbols,
3166 dw2_find_last_source_symtab,
3167 dw2_forget_cached_source_info,
3168 dw2_map_symtabs_matching_filename,
3169 dw2_lookup_symbol,
3170 dw2_pre_expand_symtabs_matching,
3171 dw2_print_stats,
3172 dw2_dump,
3173 dw2_relocate,
3174 dw2_expand_symtabs_for_function,
3175 dw2_expand_all_symtabs,
3176 dw2_expand_symtabs_with_filename,
3177 dw2_find_symbol_file,
3178 dw2_map_matching_symbols,
3179 dw2_expand_symtabs_matching,
3180 dw2_find_pc_sect_symtab,
3181 dw2_map_symbol_filenames
3182 };
3183
3184 /* Initialize for reading DWARF for this objfile. Return 0 if this
3185 file will use psymtabs, or 1 if using the GNU index. */
3186
3187 int
3188 dwarf2_initialize_objfile (struct objfile *objfile)
3189 {
3190 /* If we're about to read full symbols, don't bother with the
3191 indices. In this case we also don't care if some other debug
3192 format is making psymtabs, because they are all about to be
3193 expanded anyway. */
3194 if ((objfile->flags & OBJF_READNOW))
3195 {
3196 int i;
3197
3198 dwarf2_per_objfile->using_index = 1;
3199 create_all_comp_units (objfile);
3200 create_all_type_units (objfile);
3201 dwarf2_per_objfile->quick_file_names_table =
3202 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3203
3204 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3205 + dwarf2_per_objfile->n_type_units); ++i)
3206 {
3207 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3208
3209 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3210 struct dwarf2_per_cu_quick_data);
3211 }
3212
3213 /* Return 1 so that gdb sees the "quick" functions. However,
3214 these functions will be no-ops because we will have expanded
3215 all symtabs. */
3216 return 1;
3217 }
3218
3219 if (dwarf2_read_index (objfile))
3220 return 1;
3221
3222 return 0;
3223 }
3224
3225 \f
3226
3227 /* Build a partial symbol table. */
3228
3229 void
3230 dwarf2_build_psymtabs (struct objfile *objfile)
3231 {
3232 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3233 {
3234 init_psymbol_list (objfile, 1024);
3235 }
3236
3237 dwarf2_build_psymtabs_hard (objfile);
3238 }
3239
3240 /* Return TRUE if OFFSET is within CU_HEADER. */
3241
3242 static inline int
3243 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3244 {
3245 sect_offset bottom = { cu_header->offset.sect_off };
3246 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3247 + cu_header->initial_length_size) };
3248
3249 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3250 }
3251
3252 /* Read in the comp unit header information from the debug_info at info_ptr.
3253 NOTE: This leaves members offset, first_die_offset to be filled in
3254 by the caller. */
3255
3256 static gdb_byte *
3257 read_comp_unit_head (struct comp_unit_head *cu_header,
3258 gdb_byte *info_ptr, bfd *abfd)
3259 {
3260 int signed_addr;
3261 unsigned int bytes_read;
3262
3263 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3264 cu_header->initial_length_size = bytes_read;
3265 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3266 info_ptr += bytes_read;
3267 cu_header->version = read_2_bytes (abfd, info_ptr);
3268 info_ptr += 2;
3269 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3270 &bytes_read);
3271 info_ptr += bytes_read;
3272 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3273 info_ptr += 1;
3274 signed_addr = bfd_get_sign_extend_vma (abfd);
3275 if (signed_addr < 0)
3276 internal_error (__FILE__, __LINE__,
3277 _("read_comp_unit_head: dwarf from non elf file"));
3278 cu_header->signed_addr_p = signed_addr;
3279
3280 return info_ptr;
3281 }
3282
3283 /* Subroutine of read_and_check_comp_unit_head and
3284 read_and_check_type_unit_head to simplify them.
3285 Perform various error checking on the header. */
3286
3287 static void
3288 error_check_comp_unit_head (struct comp_unit_head *header,
3289 struct dwarf2_section_info *section)
3290 {
3291 bfd *abfd = section->asection->owner;
3292 const char *filename = bfd_get_filename (abfd);
3293
3294 if (header->version != 2 && header->version != 3 && header->version != 4)
3295 error (_("Dwarf Error: wrong version in compilation unit header "
3296 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3297 filename);
3298
3299 if (header->abbrev_offset.sect_off
3300 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3301 &dwarf2_per_objfile->abbrev))
3302 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3303 "(offset 0x%lx + 6) [in module %s]"),
3304 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3305 filename);
3306
3307 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3308 avoid potential 32-bit overflow. */
3309 if (((unsigned long) header->offset.sect_off
3310 + header->length + header->initial_length_size)
3311 > section->size)
3312 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3313 "(offset 0x%lx + 0) [in module %s]"),
3314 (long) header->length, (long) header->offset.sect_off,
3315 filename);
3316 }
3317
3318 /* Read in a CU/TU header and perform some basic error checking.
3319 The contents of the header are stored in HEADER.
3320 The result is a pointer to the start of the first DIE. */
3321
3322 static gdb_byte *
3323 read_and_check_comp_unit_head (struct comp_unit_head *header,
3324 struct dwarf2_section_info *section,
3325 gdb_byte *info_ptr,
3326 int is_debug_types_section)
3327 {
3328 gdb_byte *beg_of_comp_unit = info_ptr;
3329 bfd *abfd = section->asection->owner;
3330
3331 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3332
3333 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3334
3335 /* If we're reading a type unit, skip over the signature and
3336 type_offset fields. */
3337 if (is_debug_types_section)
3338 info_ptr += 8 /*signature*/ + header->offset_size;
3339
3340 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3341
3342 error_check_comp_unit_head (header, section);
3343
3344 return info_ptr;
3345 }
3346
3347 /* Read in the types comp unit header information from .debug_types entry at
3348 types_ptr. The result is a pointer to one past the end of the header. */
3349
3350 static gdb_byte *
3351 read_and_check_type_unit_head (struct comp_unit_head *header,
3352 struct dwarf2_section_info *section,
3353 gdb_byte *info_ptr,
3354 ULONGEST *signature,
3355 cu_offset *type_offset_in_tu)
3356 {
3357 gdb_byte *beg_of_comp_unit = info_ptr;
3358 bfd *abfd = section->asection->owner;
3359
3360 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3361
3362 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3363
3364 /* If we're reading a type unit, skip over the signature and
3365 type_offset fields. */
3366 if (signature != NULL)
3367 *signature = read_8_bytes (abfd, info_ptr);
3368 info_ptr += 8;
3369 if (type_offset_in_tu != NULL)
3370 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3371 header->offset_size);
3372 info_ptr += header->offset_size;
3373
3374 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3375
3376 error_check_comp_unit_head (header, section);
3377
3378 return info_ptr;
3379 }
3380
3381 /* Allocate a new partial symtab for file named NAME and mark this new
3382 partial symtab as being an include of PST. */
3383
3384 static void
3385 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3386 struct objfile *objfile)
3387 {
3388 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3389
3390 subpst->section_offsets = pst->section_offsets;
3391 subpst->textlow = 0;
3392 subpst->texthigh = 0;
3393
3394 subpst->dependencies = (struct partial_symtab **)
3395 obstack_alloc (&objfile->objfile_obstack,
3396 sizeof (struct partial_symtab *));
3397 subpst->dependencies[0] = pst;
3398 subpst->number_of_dependencies = 1;
3399
3400 subpst->globals_offset = 0;
3401 subpst->n_global_syms = 0;
3402 subpst->statics_offset = 0;
3403 subpst->n_static_syms = 0;
3404 subpst->symtab = NULL;
3405 subpst->read_symtab = pst->read_symtab;
3406 subpst->readin = 0;
3407
3408 /* No private part is necessary for include psymtabs. This property
3409 can be used to differentiate between such include psymtabs and
3410 the regular ones. */
3411 subpst->read_symtab_private = NULL;
3412 }
3413
3414 /* Read the Line Number Program data and extract the list of files
3415 included by the source file represented by PST. Build an include
3416 partial symtab for each of these included files. */
3417
3418 static void
3419 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3420 struct die_info *die,
3421 struct partial_symtab *pst)
3422 {
3423 struct line_header *lh = NULL;
3424 struct attribute *attr;
3425
3426 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3427 if (attr)
3428 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3429 if (lh == NULL)
3430 return; /* No linetable, so no includes. */
3431
3432 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3433 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3434
3435 free_line_header (lh);
3436 }
3437
3438 static hashval_t
3439 hash_signatured_type (const void *item)
3440 {
3441 const struct signatured_type *sig_type = item;
3442
3443 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3444 return sig_type->signature;
3445 }
3446
3447 static int
3448 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3449 {
3450 const struct signatured_type *lhs = item_lhs;
3451 const struct signatured_type *rhs = item_rhs;
3452
3453 return lhs->signature == rhs->signature;
3454 }
3455
3456 /* Allocate a hash table for signatured types. */
3457
3458 static htab_t
3459 allocate_signatured_type_table (struct objfile *objfile)
3460 {
3461 return htab_create_alloc_ex (41,
3462 hash_signatured_type,
3463 eq_signatured_type,
3464 NULL,
3465 &objfile->objfile_obstack,
3466 hashtab_obstack_allocate,
3467 dummy_obstack_deallocate);
3468 }
3469
3470 /* A helper function to add a signatured type CU to a table. */
3471
3472 static int
3473 add_signatured_type_cu_to_table (void **slot, void *datum)
3474 {
3475 struct signatured_type *sigt = *slot;
3476 struct dwarf2_per_cu_data ***datap = datum;
3477
3478 **datap = &sigt->per_cu;
3479 ++*datap;
3480
3481 return 1;
3482 }
3483
3484 /* Create the hash table of all entries in the .debug_types section.
3485 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3486 The result is a pointer to the hash table or NULL if there are
3487 no types. */
3488
3489 static htab_t
3490 create_debug_types_hash_table (struct dwo_file *dwo_file,
3491 VEC (dwarf2_section_info_def) *types)
3492 {
3493 struct objfile *objfile = dwarf2_per_objfile->objfile;
3494 htab_t types_htab = NULL;
3495 int ix;
3496 struct dwarf2_section_info *section;
3497
3498 if (VEC_empty (dwarf2_section_info_def, types))
3499 return NULL;
3500
3501 for (ix = 0;
3502 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3503 ++ix)
3504 {
3505 bfd *abfd;
3506 gdb_byte *info_ptr, *end_ptr;
3507
3508 dwarf2_read_section (objfile, section);
3509 info_ptr = section->buffer;
3510
3511 if (info_ptr == NULL)
3512 continue;
3513
3514 /* We can't set abfd until now because the section may be empty or
3515 not present, in which case section->asection will be NULL. */
3516 abfd = section->asection->owner;
3517
3518 if (types_htab == NULL)
3519 {
3520 if (dwo_file)
3521 types_htab = allocate_dwo_unit_table (objfile);
3522 else
3523 types_htab = allocate_signatured_type_table (objfile);
3524 }
3525
3526 if (dwarf2_die_debug)
3527 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3528 bfd_get_filename (abfd));
3529
3530 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3531 because we don't need to read any dies: the signature is in the
3532 header. */
3533
3534 end_ptr = info_ptr + section->size;
3535 while (info_ptr < end_ptr)
3536 {
3537 sect_offset offset;
3538 cu_offset type_offset_in_tu;
3539 ULONGEST signature;
3540 struct signatured_type *sig_type;
3541 struct dwo_unit *dwo_tu;
3542 void **slot;
3543 gdb_byte *ptr = info_ptr;
3544 struct comp_unit_head header;
3545 unsigned int length;
3546
3547 offset.sect_off = ptr - section->buffer;
3548
3549 /* We need to read the type's signature in order to build the hash
3550 table, but we don't need anything else just yet. */
3551
3552 ptr = read_and_check_type_unit_head (&header, section, ptr,
3553 &signature, &type_offset_in_tu);
3554
3555 length = header.initial_length_size + header.length;
3556
3557 /* Skip dummy type units. */
3558 if (ptr >= info_ptr + length
3559 || peek_abbrev_code (abfd, ptr) == 0)
3560 {
3561 info_ptr += header.initial_length_size + header.length;
3562 continue;
3563 }
3564
3565 if (dwo_file)
3566 {
3567 sig_type = NULL;
3568 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3569 struct dwo_unit);
3570 dwo_tu->dwo_file = dwo_file;
3571 dwo_tu->signature = signature;
3572 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3573 dwo_tu->info_or_types_section = section;
3574 dwo_tu->offset = offset;
3575 dwo_tu->length = length;
3576 }
3577 else
3578 {
3579 /* N.B.: type_offset is not usable if this type uses a DWO file.
3580 The real type_offset is in the DWO file. */
3581 dwo_tu = NULL;
3582 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3583 struct signatured_type);
3584 sig_type->signature = signature;
3585 sig_type->type_offset_in_tu = type_offset_in_tu;
3586 sig_type->per_cu.objfile = objfile;
3587 sig_type->per_cu.is_debug_types = 1;
3588 sig_type->per_cu.info_or_types_section = section;
3589 sig_type->per_cu.offset = offset;
3590 sig_type->per_cu.length = length;
3591 }
3592
3593 slot = htab_find_slot (types_htab,
3594 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3595 INSERT);
3596 gdb_assert (slot != NULL);
3597 if (*slot != NULL)
3598 {
3599 sect_offset dup_offset;
3600
3601 if (dwo_file)
3602 {
3603 const struct dwo_unit *dup_tu = *slot;
3604
3605 dup_offset = dup_tu->offset;
3606 }
3607 else
3608 {
3609 const struct signatured_type *dup_tu = *slot;
3610
3611 dup_offset = dup_tu->per_cu.offset;
3612 }
3613
3614 complaint (&symfile_complaints,
3615 _("debug type entry at offset 0x%x is duplicate to the "
3616 "entry at offset 0x%x, signature 0x%s"),
3617 offset.sect_off, dup_offset.sect_off,
3618 phex (signature, sizeof (signature)));
3619 }
3620 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3621
3622 if (dwarf2_die_debug)
3623 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3624 offset.sect_off,
3625 phex (signature, sizeof (signature)));
3626
3627 info_ptr += length;
3628 }
3629 }
3630
3631 return types_htab;
3632 }
3633
3634 /* Create the hash table of all entries in the .debug_types section,
3635 and initialize all_type_units.
3636 The result is zero if there is an error (e.g. missing .debug_types section),
3637 otherwise non-zero. */
3638
3639 static int
3640 create_all_type_units (struct objfile *objfile)
3641 {
3642 htab_t types_htab;
3643 struct dwarf2_per_cu_data **iter;
3644
3645 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3646 if (types_htab == NULL)
3647 {
3648 dwarf2_per_objfile->signatured_types = NULL;
3649 return 0;
3650 }
3651
3652 dwarf2_per_objfile->signatured_types = types_htab;
3653
3654 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3655 dwarf2_per_objfile->all_type_units
3656 = obstack_alloc (&objfile->objfile_obstack,
3657 dwarf2_per_objfile->n_type_units
3658 * sizeof (struct dwarf2_per_cu_data *));
3659 iter = &dwarf2_per_objfile->all_type_units[0];
3660 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3661 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3662 == dwarf2_per_objfile->n_type_units);
3663
3664 return 1;
3665 }
3666
3667 /* Lookup a signature based type for DW_FORM_ref_sig8.
3668 Returns NULL if signature SIG is not present in the table. */
3669
3670 static struct signatured_type *
3671 lookup_signatured_type (ULONGEST sig)
3672 {
3673 struct signatured_type find_entry, *entry;
3674
3675 if (dwarf2_per_objfile->signatured_types == NULL)
3676 {
3677 complaint (&symfile_complaints,
3678 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3679 return NULL;
3680 }
3681
3682 find_entry.signature = sig;
3683 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3684 return entry;
3685 }
3686
3687 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3688
3689 static void
3690 init_cu_die_reader (struct die_reader_specs *reader,
3691 struct dwarf2_cu *cu,
3692 struct dwarf2_section_info *section,
3693 struct dwo_file *dwo_file)
3694 {
3695 gdb_assert (section->readin && section->buffer != NULL);
3696 reader->abfd = section->asection->owner;
3697 reader->cu = cu;
3698 reader->dwo_file = dwo_file;
3699 reader->die_section = section;
3700 reader->buffer = section->buffer;
3701 reader->buffer_end = section->buffer + section->size;
3702 }
3703
3704 /* Find the base address of the compilation unit for range lists and
3705 location lists. It will normally be specified by DW_AT_low_pc.
3706 In DWARF-3 draft 4, the base address could be overridden by
3707 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3708 compilation units with discontinuous ranges. */
3709
3710 static void
3711 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3712 {
3713 struct attribute *attr;
3714
3715 cu->base_known = 0;
3716 cu->base_address = 0;
3717
3718 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3719 if (attr)
3720 {
3721 cu->base_address = DW_ADDR (attr);
3722 cu->base_known = 1;
3723 }
3724 else
3725 {
3726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3727 if (attr)
3728 {
3729 cu->base_address = DW_ADDR (attr);
3730 cu->base_known = 1;
3731 }
3732 }
3733 }
3734
3735 /* Initialize a CU (or TU) and read its DIEs.
3736 If the CU defers to a DWO file, read the DWO file as well.
3737
3738 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3739 Otherwise, a new CU is allocated with xmalloc.
3740
3741 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3742 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3743
3744 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3745 linker) then DIE_READER_FUNC will not get called. */
3746
3747 static void
3748 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3749 int use_existing_cu, int keep,
3750 die_reader_func_ftype *die_reader_func,
3751 void *data)
3752 {
3753 struct objfile *objfile = dwarf2_per_objfile->objfile;
3754 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3755 bfd *abfd = section->asection->owner;
3756 struct dwarf2_cu *cu;
3757 gdb_byte *begin_info_ptr, *info_ptr;
3758 struct die_reader_specs reader;
3759 struct die_info *comp_unit_die;
3760 int has_children;
3761 struct attribute *attr;
3762 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3763 struct signatured_type *sig_type = NULL;
3764
3765 if (use_existing_cu)
3766 gdb_assert (keep);
3767
3768 cleanups = make_cleanup (null_cleanup, NULL);
3769
3770 /* This is cheap if the section is already read in. */
3771 dwarf2_read_section (objfile, section);
3772
3773 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3774
3775 if (use_existing_cu && this_cu->cu != NULL)
3776 {
3777 cu = this_cu->cu;
3778 info_ptr += cu->header.first_die_offset.cu_off;
3779 }
3780 else
3781 {
3782 /* If !use_existing_cu, this_cu->cu must be NULL. */
3783 gdb_assert (this_cu->cu == NULL);
3784
3785 cu = xmalloc (sizeof (*cu));
3786 init_one_comp_unit (cu, this_cu);
3787
3788 /* If an error occurs while loading, release our storage. */
3789 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3790
3791 if (this_cu->is_debug_types)
3792 {
3793 ULONGEST signature;
3794
3795 info_ptr = read_and_check_type_unit_head (&cu->header,
3796 section, info_ptr,
3797 &signature, NULL);
3798
3799 /* There's no way to get from PER_CU to its containing
3800 struct signatured_type.
3801 But we have the signature so we can use that. */
3802 sig_type = lookup_signatured_type (signature);
3803 /* We've already scanned all the signatured types,
3804 this must succeed. */
3805 gdb_assert (sig_type != NULL);
3806 gdb_assert (&sig_type->per_cu == this_cu);
3807 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3808
3809 /* LENGTH has not been set yet for type units. */
3810 this_cu->length = cu->header.length + cu->header.initial_length_size;
3811
3812 /* Establish the type offset that can be used to lookup the type. */
3813 sig_type->type_offset_in_section.sect_off =
3814 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3815 }
3816 else
3817 {
3818 info_ptr = read_and_check_comp_unit_head (&cu->header,
3819 section, info_ptr, 0);
3820
3821 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3822 gdb_assert (this_cu->length
3823 == cu->header.length + cu->header.initial_length_size);
3824 }
3825 }
3826
3827 /* Skip dummy compilation units. */
3828 if (info_ptr >= begin_info_ptr + this_cu->length
3829 || peek_abbrev_code (abfd, info_ptr) == 0)
3830 {
3831 do_cleanups (cleanups);
3832 return;
3833 }
3834
3835 /* Read the abbrevs for this compilation unit into a table. */
3836 if (cu->dwarf2_abbrevs == NULL)
3837 {
3838 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3839 make_cleanup (dwarf2_free_abbrev_table, cu);
3840 }
3841
3842 /* Read the top level CU/TU die. */
3843 init_cu_die_reader (&reader, cu, section, NULL);
3844 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3845
3846 /* If we have a DWO stub, process it and then read in the DWO file.
3847 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3848 a DWO CU, that this test will fail. */
3849 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3850 if (attr)
3851 {
3852 char *dwo_name = DW_STRING (attr);
3853 const char *comp_dir;
3854 struct dwo_unit *dwo_unit;
3855 ULONGEST signature; /* Or dwo_id. */
3856 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3857 int i,num_extra_attrs;
3858
3859 if (has_children)
3860 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3861 " has children (offset 0x%x) [in module %s]"),
3862 this_cu->offset.sect_off, bfd_get_filename (abfd));
3863
3864 /* These attributes aren't processed until later:
3865 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3866 However, the attribute is found in the stub which we won't have later.
3867 In order to not impose this complication on the rest of the code,
3868 we read them here and copy them to the DWO CU/TU die. */
3869 stmt_list = low_pc = high_pc = ranges = NULL;
3870
3871 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3872 DWO file. */
3873 if (! this_cu->is_debug_types)
3874 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3875 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3876 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3877 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3878
3879 /* There should be a DW_AT_addr_base attribute here (if needed).
3880 We need the value before we can process DW_FORM_GNU_addr_index. */
3881 cu->addr_base = 0;
3882 cu->have_addr_base = 0;
3883 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3884 if (attr)
3885 {
3886 cu->addr_base = DW_UNSND (attr);
3887 cu->have_addr_base = 1;
3888 }
3889
3890 if (this_cu->is_debug_types)
3891 {
3892 gdb_assert (sig_type != NULL);
3893 signature = sig_type->signature;
3894 }
3895 else
3896 {
3897 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3898 if (! attr)
3899 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3900 dwo_name);
3901 signature = DW_UNSND (attr);
3902 }
3903
3904 /* We may need the comp_dir in order to find the DWO file. */
3905 comp_dir = NULL;
3906 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3907 if (attr)
3908 comp_dir = DW_STRING (attr);
3909
3910 if (this_cu->is_debug_types)
3911 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3912 else
3913 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3914 signature);
3915
3916 if (dwo_unit == NULL)
3917 {
3918 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3919 " with ID %s [in module %s]"),
3920 this_cu->offset.sect_off,
3921 phex (signature, sizeof (signature)),
3922 objfile->name);
3923 }
3924
3925 /* Set up for reading the DWO CU/TU. */
3926 cu->dwo_unit = dwo_unit;
3927 section = dwo_unit->info_or_types_section;
3928 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3929 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3930
3931 if (this_cu->is_debug_types)
3932 {
3933 ULONGEST signature;
3934
3935 info_ptr = read_and_check_type_unit_head (&cu->header,
3936 section, info_ptr,
3937 &signature, NULL);
3938 gdb_assert (sig_type->signature == signature);
3939 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3940 gdb_assert (dwo_unit->length
3941 == cu->header.length + cu->header.initial_length_size);
3942
3943 /* Establish the type offset that can be used to lookup the type.
3944 For DWO files, we don't know it until now. */
3945 sig_type->type_offset_in_section.sect_off =
3946 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3947 }
3948 else
3949 {
3950 info_ptr = read_and_check_comp_unit_head (&cu->header,
3951 section, info_ptr, 0);
3952 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3953 gdb_assert (dwo_unit->length
3954 == cu->header.length + cu->header.initial_length_size);
3955 }
3956
3957 /* Discard the original CU's abbrev table, and read the DWO's. */
3958 dwarf2_free_abbrev_table (cu);
3959 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3960
3961 /* Read in the die, but leave space to copy over the attributes
3962 from the stub. This has the benefit of simplifying the rest of
3963 the code - all the real work is done here. */
3964 num_extra_attrs = ((stmt_list != NULL)
3965 + (low_pc != NULL)
3966 + (high_pc != NULL)
3967 + (ranges != NULL));
3968 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3969 &has_children, num_extra_attrs);
3970
3971 /* Copy over the attributes from the stub to the DWO die. */
3972 i = comp_unit_die->num_attrs;
3973 if (stmt_list != NULL)
3974 comp_unit_die->attrs[i++] = *stmt_list;
3975 if (low_pc != NULL)
3976 comp_unit_die->attrs[i++] = *low_pc;
3977 if (high_pc != NULL)
3978 comp_unit_die->attrs[i++] = *high_pc;
3979 if (ranges != NULL)
3980 comp_unit_die->attrs[i++] = *ranges;
3981 comp_unit_die->num_attrs += num_extra_attrs;
3982
3983 /* Skip dummy compilation units. */
3984 if (info_ptr >= begin_info_ptr + dwo_unit->length
3985 || peek_abbrev_code (abfd, info_ptr) == 0)
3986 {
3987 do_cleanups (cleanups);
3988 return;
3989 }
3990 }
3991
3992 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3993
3994 if (free_cu_cleanup != NULL)
3995 {
3996 if (keep)
3997 {
3998 /* We've successfully allocated this compilation unit. Let our
3999 caller clean it up when finished with it. */
4000 discard_cleanups (free_cu_cleanup);
4001
4002 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4003 So we have to manually free the abbrev table. */
4004 dwarf2_free_abbrev_table (cu);
4005
4006 /* Link this CU into read_in_chain. */
4007 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4008 dwarf2_per_objfile->read_in_chain = this_cu;
4009 }
4010 else
4011 do_cleanups (free_cu_cleanup);
4012 }
4013
4014 do_cleanups (cleanups);
4015 }
4016
4017 /* Read CU/TU THIS_CU in section SECTION,
4018 but do not follow DW_AT_GNU_dwo_name if present.
4019 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4020 have already done the lookup to find the DWO file).
4021
4022 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4023 THIS_CU->is_debug_types, but nothing else.
4024
4025 We fill in THIS_CU->length.
4026
4027 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4028 linker) then DIE_READER_FUNC will not get called.
4029
4030 THIS_CU->cu is always freed when done.
4031 This is done in order to not leave THIS_CU->cu in a state where we have
4032 to care whether it refers to the "main" CU or the DWO CU. */
4033
4034 static void
4035 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4036 struct dwarf2_section_info *abbrev_section,
4037 struct dwo_file *dwo_file,
4038 die_reader_func_ftype *die_reader_func,
4039 void *data)
4040 {
4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
4042 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4043 bfd *abfd = section->asection->owner;
4044 struct dwarf2_cu cu;
4045 gdb_byte *begin_info_ptr, *info_ptr;
4046 struct die_reader_specs reader;
4047 struct cleanup *cleanups;
4048 struct die_info *comp_unit_die;
4049 int has_children;
4050
4051 gdb_assert (this_cu->cu == NULL);
4052
4053 /* This is cheap if the section is already read in. */
4054 dwarf2_read_section (objfile, section);
4055
4056 init_one_comp_unit (&cu, this_cu);
4057
4058 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4059
4060 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4061 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
4062 this_cu->is_debug_types);
4063
4064 this_cu->length = cu.header.length + cu.header.initial_length_size;
4065
4066 /* Skip dummy compilation units. */
4067 if (info_ptr >= begin_info_ptr + this_cu->length
4068 || peek_abbrev_code (abfd, info_ptr) == 0)
4069 {
4070 do_cleanups (cleanups);
4071 return;
4072 }
4073
4074 dwarf2_read_abbrevs (&cu, abbrev_section);
4075 make_cleanup (dwarf2_free_abbrev_table, &cu);
4076
4077 init_cu_die_reader (&reader, &cu, section, dwo_file);
4078 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4079
4080 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4081
4082 do_cleanups (cleanups);
4083 }
4084
4085 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4086 does not lookup the specified DWO file.
4087 This cannot be used to read DWO files.
4088
4089 THIS_CU->cu is always freed when done.
4090 This is done in order to not leave THIS_CU->cu in a state where we have
4091 to care whether it refers to the "main" CU or the DWO CU.
4092 We can revisit this if the data shows there's a performance issue. */
4093
4094 static void
4095 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4096 die_reader_func_ftype *die_reader_func,
4097 void *data)
4098 {
4099 init_cutu_and_read_dies_no_follow (this_cu,
4100 &dwarf2_per_objfile->abbrev,
4101 NULL,
4102 die_reader_func, data);
4103 }
4104
4105 /* die_reader_func for process_psymtab_comp_unit. */
4106
4107 static void
4108 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4109 gdb_byte *info_ptr,
4110 struct die_info *comp_unit_die,
4111 int has_children,
4112 void *data)
4113 {
4114 struct dwarf2_cu *cu = reader->cu;
4115 struct objfile *objfile = cu->objfile;
4116 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4117 struct attribute *attr;
4118 CORE_ADDR baseaddr;
4119 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4120 struct partial_symtab *pst;
4121 int has_pc_info;
4122 const char *filename;
4123 int *want_partial_unit_ptr = data;
4124
4125 if (comp_unit_die->tag == DW_TAG_partial_unit
4126 && (want_partial_unit_ptr == NULL
4127 || !*want_partial_unit_ptr))
4128 return;
4129
4130 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4131
4132 cu->list_in_scope = &file_symbols;
4133
4134 /* Allocate a new partial symbol table structure. */
4135 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4136 if (attr == NULL || !DW_STRING (attr))
4137 filename = "";
4138 else
4139 filename = DW_STRING (attr);
4140 pst = start_psymtab_common (objfile, objfile->section_offsets,
4141 filename,
4142 /* TEXTLOW and TEXTHIGH are set below. */
4143 0,
4144 objfile->global_psymbols.next,
4145 objfile->static_psymbols.next);
4146 pst->psymtabs_addrmap_supported = 1;
4147
4148 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4149 if (attr != NULL)
4150 pst->dirname = DW_STRING (attr);
4151
4152 pst->read_symtab_private = per_cu;
4153
4154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4155
4156 /* Store the function that reads in the rest of the symbol table. */
4157 pst->read_symtab = dwarf2_psymtab_to_symtab;
4158
4159 per_cu->v.psymtab = pst;
4160
4161 dwarf2_find_base_address (comp_unit_die, cu);
4162
4163 /* Possibly set the default values of LOWPC and HIGHPC from
4164 `DW_AT_ranges'. */
4165 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4166 &best_highpc, cu, pst);
4167 if (has_pc_info == 1 && best_lowpc < best_highpc)
4168 /* Store the contiguous range if it is not empty; it can be empty for
4169 CUs with no code. */
4170 addrmap_set_empty (objfile->psymtabs_addrmap,
4171 best_lowpc + baseaddr,
4172 best_highpc + baseaddr - 1, pst);
4173
4174 /* Check if comp unit has_children.
4175 If so, read the rest of the partial symbols from this comp unit.
4176 If not, there's no more debug_info for this comp unit. */
4177 if (has_children)
4178 {
4179 struct partial_die_info *first_die;
4180 CORE_ADDR lowpc, highpc;
4181
4182 lowpc = ((CORE_ADDR) -1);
4183 highpc = ((CORE_ADDR) 0);
4184
4185 first_die = load_partial_dies (reader, info_ptr, 1);
4186
4187 scan_partial_symbols (first_die, &lowpc, &highpc,
4188 ! has_pc_info, cu);
4189
4190 /* If we didn't find a lowpc, set it to highpc to avoid
4191 complaints from `maint check'. */
4192 if (lowpc == ((CORE_ADDR) -1))
4193 lowpc = highpc;
4194
4195 /* If the compilation unit didn't have an explicit address range,
4196 then use the information extracted from its child dies. */
4197 if (! has_pc_info)
4198 {
4199 best_lowpc = lowpc;
4200 best_highpc = highpc;
4201 }
4202 }
4203 pst->textlow = best_lowpc + baseaddr;
4204 pst->texthigh = best_highpc + baseaddr;
4205
4206 pst->n_global_syms = objfile->global_psymbols.next -
4207 (objfile->global_psymbols.list + pst->globals_offset);
4208 pst->n_static_syms = objfile->static_psymbols.next -
4209 (objfile->static_psymbols.list + pst->statics_offset);
4210 sort_pst_symbols (pst);
4211
4212 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4213 {
4214 int i;
4215 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4216 struct dwarf2_per_cu_data *iter;
4217
4218 /* Fill in 'dependencies' here; we fill in 'users' in a
4219 post-pass. */
4220 pst->number_of_dependencies = len;
4221 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4222 len * sizeof (struct symtab *));
4223 for (i = 0;
4224 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4225 i, iter);
4226 ++i)
4227 pst->dependencies[i] = iter->v.psymtab;
4228
4229 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4230 }
4231
4232 if (per_cu->is_debug_types)
4233 {
4234 /* It's not clear we want to do anything with stmt lists here.
4235 Waiting to see what gcc ultimately does. */
4236 }
4237 else
4238 {
4239 /* Get the list of files included in the current compilation unit,
4240 and build a psymtab for each of them. */
4241 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4242 }
4243 }
4244
4245 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4246 Process compilation unit THIS_CU for a psymtab. */
4247
4248 static void
4249 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4250 int want_partial_unit)
4251 {
4252 /* If this compilation unit was already read in, free the
4253 cached copy in order to read it in again. This is
4254 necessary because we skipped some symbols when we first
4255 read in the compilation unit (see load_partial_dies).
4256 This problem could be avoided, but the benefit is unclear. */
4257 if (this_cu->cu != NULL)
4258 free_one_cached_comp_unit (this_cu);
4259
4260 gdb_assert (! this_cu->is_debug_types);
4261 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4262 &want_partial_unit);
4263
4264 /* Age out any secondary CUs. */
4265 age_cached_comp_units ();
4266 }
4267
4268 /* Traversal function for htab_traverse_noresize.
4269 Process one .debug_types comp-unit. */
4270
4271 static int
4272 process_psymtab_type_unit (void **slot, void *info)
4273 {
4274 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4275 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4276
4277 gdb_assert (per_cu->is_debug_types);
4278 gdb_assert (info == NULL);
4279
4280 /* If this compilation unit was already read in, free the
4281 cached copy in order to read it in again. This is
4282 necessary because we skipped some symbols when we first
4283 read in the compilation unit (see load_partial_dies).
4284 This problem could be avoided, but the benefit is unclear. */
4285 if (per_cu->cu != NULL)
4286 free_one_cached_comp_unit (per_cu);
4287
4288 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4289 NULL);
4290
4291 /* Age out any secondary CUs. */
4292 age_cached_comp_units ();
4293
4294 return 1;
4295 }
4296
4297 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4298 Build partial symbol tables for the .debug_types comp-units. */
4299
4300 static void
4301 build_type_psymtabs (struct objfile *objfile)
4302 {
4303 if (! create_all_type_units (objfile))
4304 return;
4305
4306 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4307 process_psymtab_type_unit, NULL);
4308 }
4309
4310 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4311
4312 static void
4313 psymtabs_addrmap_cleanup (void *o)
4314 {
4315 struct objfile *objfile = o;
4316
4317 objfile->psymtabs_addrmap = NULL;
4318 }
4319
4320 /* Compute the 'user' field for each psymtab in OBJFILE. */
4321
4322 static void
4323 set_partial_user (struct objfile *objfile)
4324 {
4325 int i;
4326
4327 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4328 {
4329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4330 struct partial_symtab *pst = per_cu->v.psymtab;
4331 int j;
4332
4333 for (j = 0; j < pst->number_of_dependencies; ++j)
4334 {
4335 /* Set the 'user' field only if it is not already set. */
4336 if (pst->dependencies[j]->user == NULL)
4337 pst->dependencies[j]->user = pst;
4338 }
4339 }
4340 }
4341
4342 /* Build the partial symbol table by doing a quick pass through the
4343 .debug_info and .debug_abbrev sections. */
4344
4345 static void
4346 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4347 {
4348 struct cleanup *back_to, *addrmap_cleanup;
4349 struct obstack temp_obstack;
4350 int i;
4351
4352 dwarf2_per_objfile->reading_partial_symbols = 1;
4353
4354 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4355
4356 /* Any cached compilation units will be linked by the per-objfile
4357 read_in_chain. Make sure to free them when we're done. */
4358 back_to = make_cleanup (free_cached_comp_units, NULL);
4359
4360 build_type_psymtabs (objfile);
4361
4362 create_all_comp_units (objfile);
4363
4364 /* Create a temporary address map on a temporary obstack. We later
4365 copy this to the final obstack. */
4366 obstack_init (&temp_obstack);
4367 make_cleanup_obstack_free (&temp_obstack);
4368 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4369 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4370
4371 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4372 {
4373 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4374
4375 process_psymtab_comp_unit (per_cu, 0);
4376 }
4377
4378 set_partial_user (objfile);
4379
4380 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4381 &objfile->objfile_obstack);
4382 discard_cleanups (addrmap_cleanup);
4383
4384 do_cleanups (back_to);
4385 }
4386
4387 /* die_reader_func for load_partial_comp_unit. */
4388
4389 static void
4390 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4391 gdb_byte *info_ptr,
4392 struct die_info *comp_unit_die,
4393 int has_children,
4394 void *data)
4395 {
4396 struct dwarf2_cu *cu = reader->cu;
4397
4398 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4399
4400 /* Check if comp unit has_children.
4401 If so, read the rest of the partial symbols from this comp unit.
4402 If not, there's no more debug_info for this comp unit. */
4403 if (has_children)
4404 load_partial_dies (reader, info_ptr, 0);
4405 }
4406
4407 /* Load the partial DIEs for a secondary CU into memory.
4408 This is also used when rereading a primary CU with load_all_dies. */
4409
4410 static void
4411 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4412 {
4413 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4414 }
4415
4416 /* Create a list of all compilation units in OBJFILE.
4417 This is only done for -readnow and building partial symtabs. */
4418
4419 static void
4420 create_all_comp_units (struct objfile *objfile)
4421 {
4422 int n_allocated;
4423 int n_comp_units;
4424 struct dwarf2_per_cu_data **all_comp_units;
4425 gdb_byte *info_ptr;
4426
4427 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4428 info_ptr = dwarf2_per_objfile->info.buffer;
4429
4430 n_comp_units = 0;
4431 n_allocated = 10;
4432 all_comp_units = xmalloc (n_allocated
4433 * sizeof (struct dwarf2_per_cu_data *));
4434
4435 while (info_ptr < dwarf2_per_objfile->info.buffer
4436 + dwarf2_per_objfile->info.size)
4437 {
4438 unsigned int length, initial_length_size;
4439 struct dwarf2_per_cu_data *this_cu;
4440 sect_offset offset;
4441
4442 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4443
4444 /* Read just enough information to find out where the next
4445 compilation unit is. */
4446 length = read_initial_length (objfile->obfd, info_ptr,
4447 &initial_length_size);
4448
4449 /* Save the compilation unit for later lookup. */
4450 this_cu = obstack_alloc (&objfile->objfile_obstack,
4451 sizeof (struct dwarf2_per_cu_data));
4452 memset (this_cu, 0, sizeof (*this_cu));
4453 this_cu->offset = offset;
4454 this_cu->length = length + initial_length_size;
4455 this_cu->objfile = objfile;
4456 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4457
4458 if (n_comp_units == n_allocated)
4459 {
4460 n_allocated *= 2;
4461 all_comp_units = xrealloc (all_comp_units,
4462 n_allocated
4463 * sizeof (struct dwarf2_per_cu_data *));
4464 }
4465 all_comp_units[n_comp_units++] = this_cu;
4466
4467 info_ptr = info_ptr + this_cu->length;
4468 }
4469
4470 dwarf2_per_objfile->all_comp_units
4471 = obstack_alloc (&objfile->objfile_obstack,
4472 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4473 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4474 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4475 xfree (all_comp_units);
4476 dwarf2_per_objfile->n_comp_units = n_comp_units;
4477 }
4478
4479 /* Process all loaded DIEs for compilation unit CU, starting at
4480 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4481 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4482 DW_AT_ranges). If NEED_PC is set, then this function will set
4483 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4484 and record the covered ranges in the addrmap. */
4485
4486 static void
4487 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4488 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4489 {
4490 struct partial_die_info *pdi;
4491
4492 /* Now, march along the PDI's, descending into ones which have
4493 interesting children but skipping the children of the other ones,
4494 until we reach the end of the compilation unit. */
4495
4496 pdi = first_die;
4497
4498 while (pdi != NULL)
4499 {
4500 fixup_partial_die (pdi, cu);
4501
4502 /* Anonymous namespaces or modules have no name but have interesting
4503 children, so we need to look at them. Ditto for anonymous
4504 enums. */
4505
4506 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4507 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4508 || pdi->tag == DW_TAG_imported_unit)
4509 {
4510 switch (pdi->tag)
4511 {
4512 case DW_TAG_subprogram:
4513 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4514 break;
4515 case DW_TAG_constant:
4516 case DW_TAG_variable:
4517 case DW_TAG_typedef:
4518 case DW_TAG_union_type:
4519 if (!pdi->is_declaration)
4520 {
4521 add_partial_symbol (pdi, cu);
4522 }
4523 break;
4524 case DW_TAG_class_type:
4525 case DW_TAG_interface_type:
4526 case DW_TAG_structure_type:
4527 if (!pdi->is_declaration)
4528 {
4529 add_partial_symbol (pdi, cu);
4530 }
4531 break;
4532 case DW_TAG_enumeration_type:
4533 if (!pdi->is_declaration)
4534 add_partial_enumeration (pdi, cu);
4535 break;
4536 case DW_TAG_base_type:
4537 case DW_TAG_subrange_type:
4538 /* File scope base type definitions are added to the partial
4539 symbol table. */
4540 add_partial_symbol (pdi, cu);
4541 break;
4542 case DW_TAG_namespace:
4543 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4544 break;
4545 case DW_TAG_module:
4546 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4547 break;
4548 case DW_TAG_imported_unit:
4549 {
4550 struct dwarf2_per_cu_data *per_cu;
4551
4552 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4553 cu->objfile);
4554
4555 /* Go read the partial unit, if needed. */
4556 if (per_cu->v.psymtab == NULL)
4557 process_psymtab_comp_unit (per_cu, 1);
4558
4559 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4560 per_cu);
4561 }
4562 break;
4563 default:
4564 break;
4565 }
4566 }
4567
4568 /* If the die has a sibling, skip to the sibling. */
4569
4570 pdi = pdi->die_sibling;
4571 }
4572 }
4573
4574 /* Functions used to compute the fully scoped name of a partial DIE.
4575
4576 Normally, this is simple. For C++, the parent DIE's fully scoped
4577 name is concatenated with "::" and the partial DIE's name. For
4578 Java, the same thing occurs except that "." is used instead of "::".
4579 Enumerators are an exception; they use the scope of their parent
4580 enumeration type, i.e. the name of the enumeration type is not
4581 prepended to the enumerator.
4582
4583 There are two complexities. One is DW_AT_specification; in this
4584 case "parent" means the parent of the target of the specification,
4585 instead of the direct parent of the DIE. The other is compilers
4586 which do not emit DW_TAG_namespace; in this case we try to guess
4587 the fully qualified name of structure types from their members'
4588 linkage names. This must be done using the DIE's children rather
4589 than the children of any DW_AT_specification target. We only need
4590 to do this for structures at the top level, i.e. if the target of
4591 any DW_AT_specification (if any; otherwise the DIE itself) does not
4592 have a parent. */
4593
4594 /* Compute the scope prefix associated with PDI's parent, in
4595 compilation unit CU. The result will be allocated on CU's
4596 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4597 field. NULL is returned if no prefix is necessary. */
4598 static char *
4599 partial_die_parent_scope (struct partial_die_info *pdi,
4600 struct dwarf2_cu *cu)
4601 {
4602 char *grandparent_scope;
4603 struct partial_die_info *parent, *real_pdi;
4604
4605 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4606 then this means the parent of the specification DIE. */
4607
4608 real_pdi = pdi;
4609 while (real_pdi->has_specification)
4610 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4611
4612 parent = real_pdi->die_parent;
4613 if (parent == NULL)
4614 return NULL;
4615
4616 if (parent->scope_set)
4617 return parent->scope;
4618
4619 fixup_partial_die (parent, cu);
4620
4621 grandparent_scope = partial_die_parent_scope (parent, cu);
4622
4623 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4624 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4625 Work around this problem here. */
4626 if (cu->language == language_cplus
4627 && parent->tag == DW_TAG_namespace
4628 && strcmp (parent->name, "::") == 0
4629 && grandparent_scope == NULL)
4630 {
4631 parent->scope = NULL;
4632 parent->scope_set = 1;
4633 return NULL;
4634 }
4635
4636 if (pdi->tag == DW_TAG_enumerator)
4637 /* Enumerators should not get the name of the enumeration as a prefix. */
4638 parent->scope = grandparent_scope;
4639 else if (parent->tag == DW_TAG_namespace
4640 || parent->tag == DW_TAG_module
4641 || parent->tag == DW_TAG_structure_type
4642 || parent->tag == DW_TAG_class_type
4643 || parent->tag == DW_TAG_interface_type
4644 || parent->tag == DW_TAG_union_type
4645 || parent->tag == DW_TAG_enumeration_type)
4646 {
4647 if (grandparent_scope == NULL)
4648 parent->scope = parent->name;
4649 else
4650 parent->scope = typename_concat (&cu->comp_unit_obstack,
4651 grandparent_scope,
4652 parent->name, 0, cu);
4653 }
4654 else
4655 {
4656 /* FIXME drow/2004-04-01: What should we be doing with
4657 function-local names? For partial symbols, we should probably be
4658 ignoring them. */
4659 complaint (&symfile_complaints,
4660 _("unhandled containing DIE tag %d for DIE at %d"),
4661 parent->tag, pdi->offset.sect_off);
4662 parent->scope = grandparent_scope;
4663 }
4664
4665 parent->scope_set = 1;
4666 return parent->scope;
4667 }
4668
4669 /* Return the fully scoped name associated with PDI, from compilation unit
4670 CU. The result will be allocated with malloc. */
4671
4672 static char *
4673 partial_die_full_name (struct partial_die_info *pdi,
4674 struct dwarf2_cu *cu)
4675 {
4676 char *parent_scope;
4677
4678 /* If this is a template instantiation, we can not work out the
4679 template arguments from partial DIEs. So, unfortunately, we have
4680 to go through the full DIEs. At least any work we do building
4681 types here will be reused if full symbols are loaded later. */
4682 if (pdi->has_template_arguments)
4683 {
4684 fixup_partial_die (pdi, cu);
4685
4686 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4687 {
4688 struct die_info *die;
4689 struct attribute attr;
4690 struct dwarf2_cu *ref_cu = cu;
4691
4692 /* DW_FORM_ref_addr is using section offset. */
4693 attr.name = 0;
4694 attr.form = DW_FORM_ref_addr;
4695 attr.u.unsnd = pdi->offset.sect_off;
4696 die = follow_die_ref (NULL, &attr, &ref_cu);
4697
4698 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4699 }
4700 }
4701
4702 parent_scope = partial_die_parent_scope (pdi, cu);
4703 if (parent_scope == NULL)
4704 return NULL;
4705 else
4706 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4707 }
4708
4709 static void
4710 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4711 {
4712 struct objfile *objfile = cu->objfile;
4713 CORE_ADDR addr = 0;
4714 char *actual_name = NULL;
4715 CORE_ADDR baseaddr;
4716 int built_actual_name = 0;
4717
4718 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4719
4720 actual_name = partial_die_full_name (pdi, cu);
4721 if (actual_name)
4722 built_actual_name = 1;
4723
4724 if (actual_name == NULL)
4725 actual_name = pdi->name;
4726
4727 switch (pdi->tag)
4728 {
4729 case DW_TAG_subprogram:
4730 if (pdi->is_external || cu->language == language_ada)
4731 {
4732 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4733 of the global scope. But in Ada, we want to be able to access
4734 nested procedures globally. So all Ada subprograms are stored
4735 in the global scope. */
4736 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4737 mst_text, objfile); */
4738 add_psymbol_to_list (actual_name, strlen (actual_name),
4739 built_actual_name,
4740 VAR_DOMAIN, LOC_BLOCK,
4741 &objfile->global_psymbols,
4742 0, pdi->lowpc + baseaddr,
4743 cu->language, objfile);
4744 }
4745 else
4746 {
4747 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4748 mst_file_text, objfile); */
4749 add_psymbol_to_list (actual_name, strlen (actual_name),
4750 built_actual_name,
4751 VAR_DOMAIN, LOC_BLOCK,
4752 &objfile->static_psymbols,
4753 0, pdi->lowpc + baseaddr,
4754 cu->language, objfile);
4755 }
4756 break;
4757 case DW_TAG_constant:
4758 {
4759 struct psymbol_allocation_list *list;
4760
4761 if (pdi->is_external)
4762 list = &objfile->global_psymbols;
4763 else
4764 list = &objfile->static_psymbols;
4765 add_psymbol_to_list (actual_name, strlen (actual_name),
4766 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4767 list, 0, 0, cu->language, objfile);
4768 }
4769 break;
4770 case DW_TAG_variable:
4771 if (pdi->d.locdesc)
4772 addr = decode_locdesc (pdi->d.locdesc, cu);
4773
4774 if (pdi->d.locdesc
4775 && addr == 0
4776 && !dwarf2_per_objfile->has_section_at_zero)
4777 {
4778 /* A global or static variable may also have been stripped
4779 out by the linker if unused, in which case its address
4780 will be nullified; do not add such variables into partial
4781 symbol table then. */
4782 }
4783 else if (pdi->is_external)
4784 {
4785 /* Global Variable.
4786 Don't enter into the minimal symbol tables as there is
4787 a minimal symbol table entry from the ELF symbols already.
4788 Enter into partial symbol table if it has a location
4789 descriptor or a type.
4790 If the location descriptor is missing, new_symbol will create
4791 a LOC_UNRESOLVED symbol, the address of the variable will then
4792 be determined from the minimal symbol table whenever the variable
4793 is referenced.
4794 The address for the partial symbol table entry is not
4795 used by GDB, but it comes in handy for debugging partial symbol
4796 table building. */
4797
4798 if (pdi->d.locdesc || pdi->has_type)
4799 add_psymbol_to_list (actual_name, strlen (actual_name),
4800 built_actual_name,
4801 VAR_DOMAIN, LOC_STATIC,
4802 &objfile->global_psymbols,
4803 0, addr + baseaddr,
4804 cu->language, objfile);
4805 }
4806 else
4807 {
4808 /* Static Variable. Skip symbols without location descriptors. */
4809 if (pdi->d.locdesc == NULL)
4810 {
4811 if (built_actual_name)
4812 xfree (actual_name);
4813 return;
4814 }
4815 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4816 mst_file_data, objfile); */
4817 add_psymbol_to_list (actual_name, strlen (actual_name),
4818 built_actual_name,
4819 VAR_DOMAIN, LOC_STATIC,
4820 &objfile->static_psymbols,
4821 0, addr + baseaddr,
4822 cu->language, objfile);
4823 }
4824 break;
4825 case DW_TAG_typedef:
4826 case DW_TAG_base_type:
4827 case DW_TAG_subrange_type:
4828 add_psymbol_to_list (actual_name, strlen (actual_name),
4829 built_actual_name,
4830 VAR_DOMAIN, LOC_TYPEDEF,
4831 &objfile->static_psymbols,
4832 0, (CORE_ADDR) 0, cu->language, objfile);
4833 break;
4834 case DW_TAG_namespace:
4835 add_psymbol_to_list (actual_name, strlen (actual_name),
4836 built_actual_name,
4837 VAR_DOMAIN, LOC_TYPEDEF,
4838 &objfile->global_psymbols,
4839 0, (CORE_ADDR) 0, cu->language, objfile);
4840 break;
4841 case DW_TAG_class_type:
4842 case DW_TAG_interface_type:
4843 case DW_TAG_structure_type:
4844 case DW_TAG_union_type:
4845 case DW_TAG_enumeration_type:
4846 /* Skip external references. The DWARF standard says in the section
4847 about "Structure, Union, and Class Type Entries": "An incomplete
4848 structure, union or class type is represented by a structure,
4849 union or class entry that does not have a byte size attribute
4850 and that has a DW_AT_declaration attribute." */
4851 if (!pdi->has_byte_size && pdi->is_declaration)
4852 {
4853 if (built_actual_name)
4854 xfree (actual_name);
4855 return;
4856 }
4857
4858 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4859 static vs. global. */
4860 add_psymbol_to_list (actual_name, strlen (actual_name),
4861 built_actual_name,
4862 STRUCT_DOMAIN, LOC_TYPEDEF,
4863 (cu->language == language_cplus
4864 || cu->language == language_java)
4865 ? &objfile->global_psymbols
4866 : &objfile->static_psymbols,
4867 0, (CORE_ADDR) 0, cu->language, objfile);
4868
4869 break;
4870 case DW_TAG_enumerator:
4871 add_psymbol_to_list (actual_name, strlen (actual_name),
4872 built_actual_name,
4873 VAR_DOMAIN, LOC_CONST,
4874 (cu->language == language_cplus
4875 || cu->language == language_java)
4876 ? &objfile->global_psymbols
4877 : &objfile->static_psymbols,
4878 0, (CORE_ADDR) 0, cu->language, objfile);
4879 break;
4880 default:
4881 break;
4882 }
4883
4884 if (built_actual_name)
4885 xfree (actual_name);
4886 }
4887
4888 /* Read a partial die corresponding to a namespace; also, add a symbol
4889 corresponding to that namespace to the symbol table. NAMESPACE is
4890 the name of the enclosing namespace. */
4891
4892 static void
4893 add_partial_namespace (struct partial_die_info *pdi,
4894 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4895 int need_pc, struct dwarf2_cu *cu)
4896 {
4897 /* Add a symbol for the namespace. */
4898
4899 add_partial_symbol (pdi, cu);
4900
4901 /* Now scan partial symbols in that namespace. */
4902
4903 if (pdi->has_children)
4904 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4905 }
4906
4907 /* Read a partial die corresponding to a Fortran module. */
4908
4909 static void
4910 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4911 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4912 {
4913 /* Now scan partial symbols in that module. */
4914
4915 if (pdi->has_children)
4916 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4917 }
4918
4919 /* Read a partial die corresponding to a subprogram and create a partial
4920 symbol for that subprogram. When the CU language allows it, this
4921 routine also defines a partial symbol for each nested subprogram
4922 that this subprogram contains.
4923
4924 DIE my also be a lexical block, in which case we simply search
4925 recursively for suprograms defined inside that lexical block.
4926 Again, this is only performed when the CU language allows this
4927 type of definitions. */
4928
4929 static void
4930 add_partial_subprogram (struct partial_die_info *pdi,
4931 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4932 int need_pc, struct dwarf2_cu *cu)
4933 {
4934 if (pdi->tag == DW_TAG_subprogram)
4935 {
4936 if (pdi->has_pc_info)
4937 {
4938 if (pdi->lowpc < *lowpc)
4939 *lowpc = pdi->lowpc;
4940 if (pdi->highpc > *highpc)
4941 *highpc = pdi->highpc;
4942 if (need_pc)
4943 {
4944 CORE_ADDR baseaddr;
4945 struct objfile *objfile = cu->objfile;
4946
4947 baseaddr = ANOFFSET (objfile->section_offsets,
4948 SECT_OFF_TEXT (objfile));
4949 addrmap_set_empty (objfile->psymtabs_addrmap,
4950 pdi->lowpc + baseaddr,
4951 pdi->highpc - 1 + baseaddr,
4952 cu->per_cu->v.psymtab);
4953 }
4954 }
4955
4956 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4957 {
4958 if (!pdi->is_declaration)
4959 /* Ignore subprogram DIEs that do not have a name, they are
4960 illegal. Do not emit a complaint at this point, we will
4961 do so when we convert this psymtab into a symtab. */
4962 if (pdi->name)
4963 add_partial_symbol (pdi, cu);
4964 }
4965 }
4966
4967 if (! pdi->has_children)
4968 return;
4969
4970 if (cu->language == language_ada)
4971 {
4972 pdi = pdi->die_child;
4973 while (pdi != NULL)
4974 {
4975 fixup_partial_die (pdi, cu);
4976 if (pdi->tag == DW_TAG_subprogram
4977 || pdi->tag == DW_TAG_lexical_block)
4978 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4979 pdi = pdi->die_sibling;
4980 }
4981 }
4982 }
4983
4984 /* Read a partial die corresponding to an enumeration type. */
4985
4986 static void
4987 add_partial_enumeration (struct partial_die_info *enum_pdi,
4988 struct dwarf2_cu *cu)
4989 {
4990 struct partial_die_info *pdi;
4991
4992 if (enum_pdi->name != NULL)
4993 add_partial_symbol (enum_pdi, cu);
4994
4995 pdi = enum_pdi->die_child;
4996 while (pdi)
4997 {
4998 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4999 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5000 else
5001 add_partial_symbol (pdi, cu);
5002 pdi = pdi->die_sibling;
5003 }
5004 }
5005
5006 /* Return the initial uleb128 in the die at INFO_PTR. */
5007
5008 static unsigned int
5009 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5010 {
5011 unsigned int bytes_read;
5012
5013 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5014 }
5015
5016 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5017 Return the corresponding abbrev, or NULL if the number is zero (indicating
5018 an empty DIE). In either case *BYTES_READ will be set to the length of
5019 the initial number. */
5020
5021 static struct abbrev_info *
5022 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5023 struct dwarf2_cu *cu)
5024 {
5025 bfd *abfd = cu->objfile->obfd;
5026 unsigned int abbrev_number;
5027 struct abbrev_info *abbrev;
5028
5029 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5030
5031 if (abbrev_number == 0)
5032 return NULL;
5033
5034 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5035 if (!abbrev)
5036 {
5037 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5038 abbrev_number, bfd_get_filename (abfd));
5039 }
5040
5041 return abbrev;
5042 }
5043
5044 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5045 Returns a pointer to the end of a series of DIEs, terminated by an empty
5046 DIE. Any children of the skipped DIEs will also be skipped. */
5047
5048 static gdb_byte *
5049 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5050 {
5051 struct dwarf2_cu *cu = reader->cu;
5052 struct abbrev_info *abbrev;
5053 unsigned int bytes_read;
5054
5055 while (1)
5056 {
5057 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5058 if (abbrev == NULL)
5059 return info_ptr + bytes_read;
5060 else
5061 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5062 }
5063 }
5064
5065 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5066 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5067 abbrev corresponding to that skipped uleb128 should be passed in
5068 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5069 children. */
5070
5071 static gdb_byte *
5072 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5073 struct abbrev_info *abbrev)
5074 {
5075 unsigned int bytes_read;
5076 struct attribute attr;
5077 bfd *abfd = reader->abfd;
5078 struct dwarf2_cu *cu = reader->cu;
5079 gdb_byte *buffer = reader->buffer;
5080 const gdb_byte *buffer_end = reader->buffer_end;
5081 gdb_byte *start_info_ptr = info_ptr;
5082 unsigned int form, i;
5083
5084 for (i = 0; i < abbrev->num_attrs; i++)
5085 {
5086 /* The only abbrev we care about is DW_AT_sibling. */
5087 if (abbrev->attrs[i].name == DW_AT_sibling)
5088 {
5089 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5090 if (attr.form == DW_FORM_ref_addr)
5091 complaint (&symfile_complaints,
5092 _("ignoring absolute DW_AT_sibling"));
5093 else
5094 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5095 }
5096
5097 /* If it isn't DW_AT_sibling, skip this attribute. */
5098 form = abbrev->attrs[i].form;
5099 skip_attribute:
5100 switch (form)
5101 {
5102 case DW_FORM_ref_addr:
5103 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5104 and later it is offset sized. */
5105 if (cu->header.version == 2)
5106 info_ptr += cu->header.addr_size;
5107 else
5108 info_ptr += cu->header.offset_size;
5109 break;
5110 case DW_FORM_addr:
5111 info_ptr += cu->header.addr_size;
5112 break;
5113 case DW_FORM_data1:
5114 case DW_FORM_ref1:
5115 case DW_FORM_flag:
5116 info_ptr += 1;
5117 break;
5118 case DW_FORM_flag_present:
5119 break;
5120 case DW_FORM_data2:
5121 case DW_FORM_ref2:
5122 info_ptr += 2;
5123 break;
5124 case DW_FORM_data4:
5125 case DW_FORM_ref4:
5126 info_ptr += 4;
5127 break;
5128 case DW_FORM_data8:
5129 case DW_FORM_ref8:
5130 case DW_FORM_ref_sig8:
5131 info_ptr += 8;
5132 break;
5133 case DW_FORM_string:
5134 read_direct_string (abfd, info_ptr, &bytes_read);
5135 info_ptr += bytes_read;
5136 break;
5137 case DW_FORM_sec_offset:
5138 case DW_FORM_strp:
5139 info_ptr += cu->header.offset_size;
5140 break;
5141 case DW_FORM_exprloc:
5142 case DW_FORM_block:
5143 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5144 info_ptr += bytes_read;
5145 break;
5146 case DW_FORM_block1:
5147 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5148 break;
5149 case DW_FORM_block2:
5150 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5151 break;
5152 case DW_FORM_block4:
5153 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5154 break;
5155 case DW_FORM_sdata:
5156 case DW_FORM_udata:
5157 case DW_FORM_ref_udata:
5158 case DW_FORM_GNU_addr_index:
5159 case DW_FORM_GNU_str_index:
5160 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5161 break;
5162 case DW_FORM_indirect:
5163 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5164 info_ptr += bytes_read;
5165 /* We need to continue parsing from here, so just go back to
5166 the top. */
5167 goto skip_attribute;
5168
5169 default:
5170 error (_("Dwarf Error: Cannot handle %s "
5171 "in DWARF reader [in module %s]"),
5172 dwarf_form_name (form),
5173 bfd_get_filename (abfd));
5174 }
5175 }
5176
5177 if (abbrev->has_children)
5178 return skip_children (reader, info_ptr);
5179 else
5180 return info_ptr;
5181 }
5182
5183 /* Locate ORIG_PDI's sibling.
5184 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5185
5186 static gdb_byte *
5187 locate_pdi_sibling (const struct die_reader_specs *reader,
5188 struct partial_die_info *orig_pdi,
5189 gdb_byte *info_ptr)
5190 {
5191 /* Do we know the sibling already? */
5192
5193 if (orig_pdi->sibling)
5194 return orig_pdi->sibling;
5195
5196 /* Are there any children to deal with? */
5197
5198 if (!orig_pdi->has_children)
5199 return info_ptr;
5200
5201 /* Skip the children the long way. */
5202
5203 return skip_children (reader, info_ptr);
5204 }
5205
5206 /* Expand this partial symbol table into a full symbol table. */
5207
5208 static void
5209 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5210 {
5211 if (pst != NULL)
5212 {
5213 if (pst->readin)
5214 {
5215 warning (_("bug: psymtab for %s is already read in."),
5216 pst->filename);
5217 }
5218 else
5219 {
5220 if (info_verbose)
5221 {
5222 printf_filtered (_("Reading in symbols for %s..."),
5223 pst->filename);
5224 gdb_flush (gdb_stdout);
5225 }
5226
5227 /* Restore our global data. */
5228 dwarf2_per_objfile = objfile_data (pst->objfile,
5229 dwarf2_objfile_data_key);
5230
5231 /* If this psymtab is constructed from a debug-only objfile, the
5232 has_section_at_zero flag will not necessarily be correct. We
5233 can get the correct value for this flag by looking at the data
5234 associated with the (presumably stripped) associated objfile. */
5235 if (pst->objfile->separate_debug_objfile_backlink)
5236 {
5237 struct dwarf2_per_objfile *dpo_backlink
5238 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5239 dwarf2_objfile_data_key);
5240
5241 dwarf2_per_objfile->has_section_at_zero
5242 = dpo_backlink->has_section_at_zero;
5243 }
5244
5245 dwarf2_per_objfile->reading_partial_symbols = 0;
5246
5247 psymtab_to_symtab_1 (pst);
5248
5249 /* Finish up the debug error message. */
5250 if (info_verbose)
5251 printf_filtered (_("done.\n"));
5252 }
5253 }
5254
5255 process_cu_includes ();
5256 }
5257 \f
5258 /* Reading in full CUs. */
5259
5260 /* Add PER_CU to the queue. */
5261
5262 static void
5263 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5264 enum language pretend_language)
5265 {
5266 struct dwarf2_queue_item *item;
5267
5268 per_cu->queued = 1;
5269 item = xmalloc (sizeof (*item));
5270 item->per_cu = per_cu;
5271 item->pretend_language = pretend_language;
5272 item->next = NULL;
5273
5274 if (dwarf2_queue == NULL)
5275 dwarf2_queue = item;
5276 else
5277 dwarf2_queue_tail->next = item;
5278
5279 dwarf2_queue_tail = item;
5280 }
5281
5282 /* Process the queue. */
5283
5284 static void
5285 process_queue (void)
5286 {
5287 struct dwarf2_queue_item *item, *next_item;
5288
5289 /* The queue starts out with one item, but following a DIE reference
5290 may load a new CU, adding it to the end of the queue. */
5291 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5292 {
5293 if (dwarf2_per_objfile->using_index
5294 ? !item->per_cu->v.quick->symtab
5295 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5296 process_full_comp_unit (item->per_cu, item->pretend_language);
5297
5298 item->per_cu->queued = 0;
5299 next_item = item->next;
5300 xfree (item);
5301 }
5302
5303 dwarf2_queue_tail = NULL;
5304 }
5305
5306 /* Free all allocated queue entries. This function only releases anything if
5307 an error was thrown; if the queue was processed then it would have been
5308 freed as we went along. */
5309
5310 static void
5311 dwarf2_release_queue (void *dummy)
5312 {
5313 struct dwarf2_queue_item *item, *last;
5314
5315 item = dwarf2_queue;
5316 while (item)
5317 {
5318 /* Anything still marked queued is likely to be in an
5319 inconsistent state, so discard it. */
5320 if (item->per_cu->queued)
5321 {
5322 if (item->per_cu->cu != NULL)
5323 free_one_cached_comp_unit (item->per_cu);
5324 item->per_cu->queued = 0;
5325 }
5326
5327 last = item;
5328 item = item->next;
5329 xfree (last);
5330 }
5331
5332 dwarf2_queue = dwarf2_queue_tail = NULL;
5333 }
5334
5335 /* Read in full symbols for PST, and anything it depends on. */
5336
5337 static void
5338 psymtab_to_symtab_1 (struct partial_symtab *pst)
5339 {
5340 struct dwarf2_per_cu_data *per_cu;
5341 int i;
5342
5343 if (pst->readin)
5344 return;
5345
5346 for (i = 0; i < pst->number_of_dependencies; i++)
5347 if (!pst->dependencies[i]->readin
5348 && pst->dependencies[i]->user == NULL)
5349 {
5350 /* Inform about additional files that need to be read in. */
5351 if (info_verbose)
5352 {
5353 /* FIXME: i18n: Need to make this a single string. */
5354 fputs_filtered (" ", gdb_stdout);
5355 wrap_here ("");
5356 fputs_filtered ("and ", gdb_stdout);
5357 wrap_here ("");
5358 printf_filtered ("%s...", pst->dependencies[i]->filename);
5359 wrap_here (""); /* Flush output. */
5360 gdb_flush (gdb_stdout);
5361 }
5362 psymtab_to_symtab_1 (pst->dependencies[i]);
5363 }
5364
5365 per_cu = pst->read_symtab_private;
5366
5367 if (per_cu == NULL)
5368 {
5369 /* It's an include file, no symbols to read for it.
5370 Everything is in the parent symtab. */
5371 pst->readin = 1;
5372 return;
5373 }
5374
5375 dw2_do_instantiate_symtab (per_cu);
5376 }
5377
5378 /* Trivial hash function for die_info: the hash value of a DIE
5379 is its offset in .debug_info for this objfile. */
5380
5381 static hashval_t
5382 die_hash (const void *item)
5383 {
5384 const struct die_info *die = item;
5385
5386 return die->offset.sect_off;
5387 }
5388
5389 /* Trivial comparison function for die_info structures: two DIEs
5390 are equal if they have the same offset. */
5391
5392 static int
5393 die_eq (const void *item_lhs, const void *item_rhs)
5394 {
5395 const struct die_info *die_lhs = item_lhs;
5396 const struct die_info *die_rhs = item_rhs;
5397
5398 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5399 }
5400
5401 /* die_reader_func for load_full_comp_unit.
5402 This is identical to read_signatured_type_reader,
5403 but is kept separate for now. */
5404
5405 static void
5406 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5407 gdb_byte *info_ptr,
5408 struct die_info *comp_unit_die,
5409 int has_children,
5410 void *data)
5411 {
5412 struct dwarf2_cu *cu = reader->cu;
5413 enum language *language_ptr = data;
5414
5415 gdb_assert (cu->die_hash == NULL);
5416 cu->die_hash =
5417 htab_create_alloc_ex (cu->header.length / 12,
5418 die_hash,
5419 die_eq,
5420 NULL,
5421 &cu->comp_unit_obstack,
5422 hashtab_obstack_allocate,
5423 dummy_obstack_deallocate);
5424
5425 if (has_children)
5426 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5427 &info_ptr, comp_unit_die);
5428 cu->dies = comp_unit_die;
5429 /* comp_unit_die is not stored in die_hash, no need. */
5430
5431 /* We try not to read any attributes in this function, because not
5432 all CUs needed for references have been loaded yet, and symbol
5433 table processing isn't initialized. But we have to set the CU language,
5434 or we won't be able to build types correctly.
5435 Similarly, if we do not read the producer, we can not apply
5436 producer-specific interpretation. */
5437 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5438 }
5439
5440 /* Load the DIEs associated with PER_CU into memory. */
5441
5442 static void
5443 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5444 enum language pretend_language)
5445 {
5446 gdb_assert (! this_cu->is_debug_types);
5447
5448 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5449 &pretend_language);
5450 }
5451
5452 /* Add a DIE to the delayed physname list. */
5453
5454 static void
5455 add_to_method_list (struct type *type, int fnfield_index, int index,
5456 const char *name, struct die_info *die,
5457 struct dwarf2_cu *cu)
5458 {
5459 struct delayed_method_info mi;
5460 mi.type = type;
5461 mi.fnfield_index = fnfield_index;
5462 mi.index = index;
5463 mi.name = name;
5464 mi.die = die;
5465 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5466 }
5467
5468 /* A cleanup for freeing the delayed method list. */
5469
5470 static void
5471 free_delayed_list (void *ptr)
5472 {
5473 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5474 if (cu->method_list != NULL)
5475 {
5476 VEC_free (delayed_method_info, cu->method_list);
5477 cu->method_list = NULL;
5478 }
5479 }
5480
5481 /* Compute the physnames of any methods on the CU's method list.
5482
5483 The computation of method physnames is delayed in order to avoid the
5484 (bad) condition that one of the method's formal parameters is of an as yet
5485 incomplete type. */
5486
5487 static void
5488 compute_delayed_physnames (struct dwarf2_cu *cu)
5489 {
5490 int i;
5491 struct delayed_method_info *mi;
5492 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5493 {
5494 const char *physname;
5495 struct fn_fieldlist *fn_flp
5496 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5497 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5498 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5499 }
5500 }
5501
5502 /* Go objects should be embedded in a DW_TAG_module DIE,
5503 and it's not clear if/how imported objects will appear.
5504 To keep Go support simple until that's worked out,
5505 go back through what we've read and create something usable.
5506 We could do this while processing each DIE, and feels kinda cleaner,
5507 but that way is more invasive.
5508 This is to, for example, allow the user to type "p var" or "b main"
5509 without having to specify the package name, and allow lookups
5510 of module.object to work in contexts that use the expression
5511 parser. */
5512
5513 static void
5514 fixup_go_packaging (struct dwarf2_cu *cu)
5515 {
5516 char *package_name = NULL;
5517 struct pending *list;
5518 int i;
5519
5520 for (list = global_symbols; list != NULL; list = list->next)
5521 {
5522 for (i = 0; i < list->nsyms; ++i)
5523 {
5524 struct symbol *sym = list->symbol[i];
5525
5526 if (SYMBOL_LANGUAGE (sym) == language_go
5527 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5528 {
5529 char *this_package_name = go_symbol_package_name (sym);
5530
5531 if (this_package_name == NULL)
5532 continue;
5533 if (package_name == NULL)
5534 package_name = this_package_name;
5535 else
5536 {
5537 if (strcmp (package_name, this_package_name) != 0)
5538 complaint (&symfile_complaints,
5539 _("Symtab %s has objects from two different Go packages: %s and %s"),
5540 (sym->symtab && sym->symtab->filename
5541 ? sym->symtab->filename
5542 : cu->objfile->name),
5543 this_package_name, package_name);
5544 xfree (this_package_name);
5545 }
5546 }
5547 }
5548 }
5549
5550 if (package_name != NULL)
5551 {
5552 struct objfile *objfile = cu->objfile;
5553 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5554 package_name, objfile);
5555 struct symbol *sym;
5556
5557 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5558
5559 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5560 SYMBOL_SET_LANGUAGE (sym, language_go);
5561 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5562 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5563 e.g., "main" finds the "main" module and not C's main(). */
5564 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5565 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5566 SYMBOL_TYPE (sym) = type;
5567
5568 add_symbol_to_list (sym, &global_symbols);
5569
5570 xfree (package_name);
5571 }
5572 }
5573
5574 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5575
5576 /* Return the symtab for PER_CU. This works properly regardless of
5577 whether we're using the index or psymtabs. */
5578
5579 static struct symtab *
5580 get_symtab (struct dwarf2_per_cu_data *per_cu)
5581 {
5582 return (dwarf2_per_objfile->using_index
5583 ? per_cu->v.quick->symtab
5584 : per_cu->v.psymtab->symtab);
5585 }
5586
5587 /* A helper function for computing the list of all symbol tables
5588 included by PER_CU. */
5589
5590 static void
5591 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5592 htab_t all_children,
5593 struct dwarf2_per_cu_data *per_cu)
5594 {
5595 void **slot;
5596 int ix;
5597 struct dwarf2_per_cu_data *iter;
5598
5599 slot = htab_find_slot (all_children, per_cu, INSERT);
5600 if (*slot != NULL)
5601 {
5602 /* This inclusion and its children have been processed. */
5603 return;
5604 }
5605
5606 *slot = per_cu;
5607 /* Only add a CU if it has a symbol table. */
5608 if (get_symtab (per_cu) != NULL)
5609 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5610
5611 for (ix = 0;
5612 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5613 ++ix)
5614 recursively_compute_inclusions (result, all_children, iter);
5615 }
5616
5617 /* Compute the symtab 'includes' fields for the symtab related to
5618 PER_CU. */
5619
5620 static void
5621 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5622 {
5623 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5624 {
5625 int ix, len;
5626 struct dwarf2_per_cu_data *iter;
5627 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5628 htab_t all_children;
5629 struct symtab *symtab = get_symtab (per_cu);
5630
5631 /* If we don't have a symtab, we can just skip this case. */
5632 if (symtab == NULL)
5633 return;
5634
5635 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5636 NULL, xcalloc, xfree);
5637
5638 for (ix = 0;
5639 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5640 ix, iter);
5641 ++ix)
5642 recursively_compute_inclusions (&result_children, all_children, iter);
5643
5644 /* Now we have a transitive closure of all the included CUs, so
5645 we can convert it to a list of symtabs. */
5646 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5647 symtab->includes
5648 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5649 (len + 1) * sizeof (struct symtab *));
5650 for (ix = 0;
5651 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5652 ++ix)
5653 symtab->includes[ix] = get_symtab (iter);
5654 symtab->includes[len] = NULL;
5655
5656 VEC_free (dwarf2_per_cu_ptr, result_children);
5657 htab_delete (all_children);
5658 }
5659 }
5660
5661 /* Compute the 'includes' field for the symtabs of all the CUs we just
5662 read. */
5663
5664 static void
5665 process_cu_includes (void)
5666 {
5667 int ix;
5668 struct dwarf2_per_cu_data *iter;
5669
5670 for (ix = 0;
5671 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5672 ix, iter);
5673 ++ix)
5674 compute_symtab_includes (iter);
5675
5676 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5677 }
5678
5679 /* Generate full symbol information for PER_CU, whose DIEs have
5680 already been loaded into memory. */
5681
5682 static void
5683 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5684 enum language pretend_language)
5685 {
5686 struct dwarf2_cu *cu = per_cu->cu;
5687 struct objfile *objfile = per_cu->objfile;
5688 CORE_ADDR lowpc, highpc;
5689 struct symtab *symtab;
5690 struct cleanup *back_to, *delayed_list_cleanup;
5691 CORE_ADDR baseaddr;
5692
5693 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5694
5695 buildsym_init ();
5696 back_to = make_cleanup (really_free_pendings, NULL);
5697 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5698
5699 cu->list_in_scope = &file_symbols;
5700
5701 cu->language = pretend_language;
5702 cu->language_defn = language_def (cu->language);
5703
5704 /* Do line number decoding in read_file_scope () */
5705 process_die (cu->dies, cu);
5706
5707 /* For now fudge the Go package. */
5708 if (cu->language == language_go)
5709 fixup_go_packaging (cu);
5710
5711 /* Now that we have processed all the DIEs in the CU, all the types
5712 should be complete, and it should now be safe to compute all of the
5713 physnames. */
5714 compute_delayed_physnames (cu);
5715 do_cleanups (delayed_list_cleanup);
5716
5717 /* Some compilers don't define a DW_AT_high_pc attribute for the
5718 compilation unit. If the DW_AT_high_pc is missing, synthesize
5719 it, by scanning the DIE's below the compilation unit. */
5720 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5721
5722 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5723
5724 if (symtab != NULL)
5725 {
5726 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5727
5728 /* Set symtab language to language from DW_AT_language. If the
5729 compilation is from a C file generated by language preprocessors, do
5730 not set the language if it was already deduced by start_subfile. */
5731 if (!(cu->language == language_c && symtab->language != language_c))
5732 symtab->language = cu->language;
5733
5734 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5735 produce DW_AT_location with location lists but it can be possibly
5736 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5737 there were bugs in prologue debug info, fixed later in GCC-4.5
5738 by "unwind info for epilogues" patch (which is not directly related).
5739
5740 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5741 needed, it would be wrong due to missing DW_AT_producer there.
5742
5743 Still one can confuse GDB by using non-standard GCC compilation
5744 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5745 */
5746 if (cu->has_loclist && gcc_4_minor >= 5)
5747 symtab->locations_valid = 1;
5748
5749 if (gcc_4_minor >= 5)
5750 symtab->epilogue_unwind_valid = 1;
5751
5752 symtab->call_site_htab = cu->call_site_htab;
5753 }
5754
5755 if (dwarf2_per_objfile->using_index)
5756 per_cu->v.quick->symtab = symtab;
5757 else
5758 {
5759 struct partial_symtab *pst = per_cu->v.psymtab;
5760 pst->symtab = symtab;
5761 pst->readin = 1;
5762 }
5763
5764 /* Push it for inclusion processing later. */
5765 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5766
5767 do_cleanups (back_to);
5768 }
5769
5770 /* Process an imported unit DIE. */
5771
5772 static void
5773 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5774 {
5775 struct attribute *attr;
5776
5777 attr = dwarf2_attr (die, DW_AT_import, cu);
5778 if (attr != NULL)
5779 {
5780 struct dwarf2_per_cu_data *per_cu;
5781 struct symtab *imported_symtab;
5782 sect_offset offset;
5783
5784 offset = dwarf2_get_ref_die_offset (attr);
5785 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5786
5787 /* Queue the unit, if needed. */
5788 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5789 load_full_comp_unit (per_cu, cu->language);
5790
5791 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5792 per_cu);
5793 }
5794 }
5795
5796 /* Process a die and its children. */
5797
5798 static void
5799 process_die (struct die_info *die, struct dwarf2_cu *cu)
5800 {
5801 switch (die->tag)
5802 {
5803 case DW_TAG_padding:
5804 break;
5805 case DW_TAG_compile_unit:
5806 case DW_TAG_partial_unit:
5807 read_file_scope (die, cu);
5808 break;
5809 case DW_TAG_type_unit:
5810 read_type_unit_scope (die, cu);
5811 break;
5812 case DW_TAG_subprogram:
5813 case DW_TAG_inlined_subroutine:
5814 read_func_scope (die, cu);
5815 break;
5816 case DW_TAG_lexical_block:
5817 case DW_TAG_try_block:
5818 case DW_TAG_catch_block:
5819 read_lexical_block_scope (die, cu);
5820 break;
5821 case DW_TAG_GNU_call_site:
5822 read_call_site_scope (die, cu);
5823 break;
5824 case DW_TAG_class_type:
5825 case DW_TAG_interface_type:
5826 case DW_TAG_structure_type:
5827 case DW_TAG_union_type:
5828 process_structure_scope (die, cu);
5829 break;
5830 case DW_TAG_enumeration_type:
5831 process_enumeration_scope (die, cu);
5832 break;
5833
5834 /* These dies have a type, but processing them does not create
5835 a symbol or recurse to process the children. Therefore we can
5836 read them on-demand through read_type_die. */
5837 case DW_TAG_subroutine_type:
5838 case DW_TAG_set_type:
5839 case DW_TAG_array_type:
5840 case DW_TAG_pointer_type:
5841 case DW_TAG_ptr_to_member_type:
5842 case DW_TAG_reference_type:
5843 case DW_TAG_string_type:
5844 break;
5845
5846 case DW_TAG_base_type:
5847 case DW_TAG_subrange_type:
5848 case DW_TAG_typedef:
5849 /* Add a typedef symbol for the type definition, if it has a
5850 DW_AT_name. */
5851 new_symbol (die, read_type_die (die, cu), cu);
5852 break;
5853 case DW_TAG_common_block:
5854 read_common_block (die, cu);
5855 break;
5856 case DW_TAG_common_inclusion:
5857 break;
5858 case DW_TAG_namespace:
5859 processing_has_namespace_info = 1;
5860 read_namespace (die, cu);
5861 break;
5862 case DW_TAG_module:
5863 processing_has_namespace_info = 1;
5864 read_module (die, cu);
5865 break;
5866 case DW_TAG_imported_declaration:
5867 case DW_TAG_imported_module:
5868 processing_has_namespace_info = 1;
5869 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5870 || cu->language != language_fortran))
5871 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5872 dwarf_tag_name (die->tag));
5873 read_import_statement (die, cu);
5874 break;
5875
5876 case DW_TAG_imported_unit:
5877 process_imported_unit_die (die, cu);
5878 break;
5879
5880 default:
5881 new_symbol (die, NULL, cu);
5882 break;
5883 }
5884 }
5885
5886 /* A helper function for dwarf2_compute_name which determines whether DIE
5887 needs to have the name of the scope prepended to the name listed in the
5888 die. */
5889
5890 static int
5891 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5892 {
5893 struct attribute *attr;
5894
5895 switch (die->tag)
5896 {
5897 case DW_TAG_namespace:
5898 case DW_TAG_typedef:
5899 case DW_TAG_class_type:
5900 case DW_TAG_interface_type:
5901 case DW_TAG_structure_type:
5902 case DW_TAG_union_type:
5903 case DW_TAG_enumeration_type:
5904 case DW_TAG_enumerator:
5905 case DW_TAG_subprogram:
5906 case DW_TAG_member:
5907 return 1;
5908
5909 case DW_TAG_variable:
5910 case DW_TAG_constant:
5911 /* We only need to prefix "globally" visible variables. These include
5912 any variable marked with DW_AT_external or any variable that
5913 lives in a namespace. [Variables in anonymous namespaces
5914 require prefixing, but they are not DW_AT_external.] */
5915
5916 if (dwarf2_attr (die, DW_AT_specification, cu))
5917 {
5918 struct dwarf2_cu *spec_cu = cu;
5919
5920 return die_needs_namespace (die_specification (die, &spec_cu),
5921 spec_cu);
5922 }
5923
5924 attr = dwarf2_attr (die, DW_AT_external, cu);
5925 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5926 && die->parent->tag != DW_TAG_module)
5927 return 0;
5928 /* A variable in a lexical block of some kind does not need a
5929 namespace, even though in C++ such variables may be external
5930 and have a mangled name. */
5931 if (die->parent->tag == DW_TAG_lexical_block
5932 || die->parent->tag == DW_TAG_try_block
5933 || die->parent->tag == DW_TAG_catch_block
5934 || die->parent->tag == DW_TAG_subprogram)
5935 return 0;
5936 return 1;
5937
5938 default:
5939 return 0;
5940 }
5941 }
5942
5943 /* Retrieve the last character from a mem_file. */
5944
5945 static void
5946 do_ui_file_peek_last (void *object, const char *buffer, long length)
5947 {
5948 char *last_char_p = (char *) object;
5949
5950 if (length > 0)
5951 *last_char_p = buffer[length - 1];
5952 }
5953
5954 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5955 compute the physname for the object, which include a method's:
5956 - formal parameters (C++/Java),
5957 - receiver type (Go),
5958 - return type (Java).
5959
5960 The term "physname" is a bit confusing.
5961 For C++, for example, it is the demangled name.
5962 For Go, for example, it's the mangled name.
5963
5964 For Ada, return the DIE's linkage name rather than the fully qualified
5965 name. PHYSNAME is ignored..
5966
5967 The result is allocated on the objfile_obstack and canonicalized. */
5968
5969 static const char *
5970 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5971 int physname)
5972 {
5973 struct objfile *objfile = cu->objfile;
5974
5975 if (name == NULL)
5976 name = dwarf2_name (die, cu);
5977
5978 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5979 compute it by typename_concat inside GDB. */
5980 if (cu->language == language_ada
5981 || (cu->language == language_fortran && physname))
5982 {
5983 /* For Ada unit, we prefer the linkage name over the name, as
5984 the former contains the exported name, which the user expects
5985 to be able to reference. Ideally, we want the user to be able
5986 to reference this entity using either natural or linkage name,
5987 but we haven't started looking at this enhancement yet. */
5988 struct attribute *attr;
5989
5990 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5991 if (attr == NULL)
5992 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5993 if (attr && DW_STRING (attr))
5994 return DW_STRING (attr);
5995 }
5996
5997 /* These are the only languages we know how to qualify names in. */
5998 if (name != NULL
5999 && (cu->language == language_cplus || cu->language == language_java
6000 || cu->language == language_fortran))
6001 {
6002 if (die_needs_namespace (die, cu))
6003 {
6004 long length;
6005 const char *prefix;
6006 struct ui_file *buf;
6007
6008 prefix = determine_prefix (die, cu);
6009 buf = mem_fileopen ();
6010 if (*prefix != '\0')
6011 {
6012 char *prefixed_name = typename_concat (NULL, prefix, name,
6013 physname, cu);
6014
6015 fputs_unfiltered (prefixed_name, buf);
6016 xfree (prefixed_name);
6017 }
6018 else
6019 fputs_unfiltered (name, buf);
6020
6021 /* Template parameters may be specified in the DIE's DW_AT_name, or
6022 as children with DW_TAG_template_type_param or
6023 DW_TAG_value_type_param. If the latter, add them to the name
6024 here. If the name already has template parameters, then
6025 skip this step; some versions of GCC emit both, and
6026 it is more efficient to use the pre-computed name.
6027
6028 Something to keep in mind about this process: it is very
6029 unlikely, or in some cases downright impossible, to produce
6030 something that will match the mangled name of a function.
6031 If the definition of the function has the same debug info,
6032 we should be able to match up with it anyway. But fallbacks
6033 using the minimal symbol, for instance to find a method
6034 implemented in a stripped copy of libstdc++, will not work.
6035 If we do not have debug info for the definition, we will have to
6036 match them up some other way.
6037
6038 When we do name matching there is a related problem with function
6039 templates; two instantiated function templates are allowed to
6040 differ only by their return types, which we do not add here. */
6041
6042 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6043 {
6044 struct attribute *attr;
6045 struct die_info *child;
6046 int first = 1;
6047
6048 die->building_fullname = 1;
6049
6050 for (child = die->child; child != NULL; child = child->sibling)
6051 {
6052 struct type *type;
6053 LONGEST value;
6054 gdb_byte *bytes;
6055 struct dwarf2_locexpr_baton *baton;
6056 struct value *v;
6057
6058 if (child->tag != DW_TAG_template_type_param
6059 && child->tag != DW_TAG_template_value_param)
6060 continue;
6061
6062 if (first)
6063 {
6064 fputs_unfiltered ("<", buf);
6065 first = 0;
6066 }
6067 else
6068 fputs_unfiltered (", ", buf);
6069
6070 attr = dwarf2_attr (child, DW_AT_type, cu);
6071 if (attr == NULL)
6072 {
6073 complaint (&symfile_complaints,
6074 _("template parameter missing DW_AT_type"));
6075 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6076 continue;
6077 }
6078 type = die_type (child, cu);
6079
6080 if (child->tag == DW_TAG_template_type_param)
6081 {
6082 c_print_type (type, "", buf, -1, 0);
6083 continue;
6084 }
6085
6086 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6087 if (attr == NULL)
6088 {
6089 complaint (&symfile_complaints,
6090 _("template parameter missing "
6091 "DW_AT_const_value"));
6092 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6093 continue;
6094 }
6095
6096 dwarf2_const_value_attr (attr, type, name,
6097 &cu->comp_unit_obstack, cu,
6098 &value, &bytes, &baton);
6099
6100 if (TYPE_NOSIGN (type))
6101 /* GDB prints characters as NUMBER 'CHAR'. If that's
6102 changed, this can use value_print instead. */
6103 c_printchar (value, type, buf);
6104 else
6105 {
6106 struct value_print_options opts;
6107
6108 if (baton != NULL)
6109 v = dwarf2_evaluate_loc_desc (type, NULL,
6110 baton->data,
6111 baton->size,
6112 baton->per_cu);
6113 else if (bytes != NULL)
6114 {
6115 v = allocate_value (type);
6116 memcpy (value_contents_writeable (v), bytes,
6117 TYPE_LENGTH (type));
6118 }
6119 else
6120 v = value_from_longest (type, value);
6121
6122 /* Specify decimal so that we do not depend on
6123 the radix. */
6124 get_formatted_print_options (&opts, 'd');
6125 opts.raw = 1;
6126 value_print (v, buf, &opts);
6127 release_value (v);
6128 value_free (v);
6129 }
6130 }
6131
6132 die->building_fullname = 0;
6133
6134 if (!first)
6135 {
6136 /* Close the argument list, with a space if necessary
6137 (nested templates). */
6138 char last_char = '\0';
6139 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6140 if (last_char == '>')
6141 fputs_unfiltered (" >", buf);
6142 else
6143 fputs_unfiltered (">", buf);
6144 }
6145 }
6146
6147 /* For Java and C++ methods, append formal parameter type
6148 information, if PHYSNAME. */
6149
6150 if (physname && die->tag == DW_TAG_subprogram
6151 && (cu->language == language_cplus
6152 || cu->language == language_java))
6153 {
6154 struct type *type = read_type_die (die, cu);
6155
6156 c_type_print_args (type, buf, 1, cu->language);
6157
6158 if (cu->language == language_java)
6159 {
6160 /* For java, we must append the return type to method
6161 names. */
6162 if (die->tag == DW_TAG_subprogram)
6163 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6164 0, 0);
6165 }
6166 else if (cu->language == language_cplus)
6167 {
6168 /* Assume that an artificial first parameter is
6169 "this", but do not crash if it is not. RealView
6170 marks unnamed (and thus unused) parameters as
6171 artificial; there is no way to differentiate
6172 the two cases. */
6173 if (TYPE_NFIELDS (type) > 0
6174 && TYPE_FIELD_ARTIFICIAL (type, 0)
6175 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6176 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6177 0))))
6178 fputs_unfiltered (" const", buf);
6179 }
6180 }
6181
6182 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6183 &length);
6184 ui_file_delete (buf);
6185
6186 if (cu->language == language_cplus)
6187 {
6188 char *cname
6189 = dwarf2_canonicalize_name (name, cu,
6190 &objfile->objfile_obstack);
6191
6192 if (cname != NULL)
6193 name = cname;
6194 }
6195 }
6196 }
6197
6198 return name;
6199 }
6200
6201 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6202 If scope qualifiers are appropriate they will be added. The result
6203 will be allocated on the objfile_obstack, or NULL if the DIE does
6204 not have a name. NAME may either be from a previous call to
6205 dwarf2_name or NULL.
6206
6207 The output string will be canonicalized (if C++/Java). */
6208
6209 static const char *
6210 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6211 {
6212 return dwarf2_compute_name (name, die, cu, 0);
6213 }
6214
6215 /* Construct a physname for the given DIE in CU. NAME may either be
6216 from a previous call to dwarf2_name or NULL. The result will be
6217 allocated on the objfile_objstack or NULL if the DIE does not have a
6218 name.
6219
6220 The output string will be canonicalized (if C++/Java). */
6221
6222 static const char *
6223 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6224 {
6225 struct objfile *objfile = cu->objfile;
6226 struct attribute *attr;
6227 const char *retval, *mangled = NULL, *canon = NULL;
6228 struct cleanup *back_to;
6229 int need_copy = 1;
6230
6231 /* In this case dwarf2_compute_name is just a shortcut not building anything
6232 on its own. */
6233 if (!die_needs_namespace (die, cu))
6234 return dwarf2_compute_name (name, die, cu, 1);
6235
6236 back_to = make_cleanup (null_cleanup, NULL);
6237
6238 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6239 if (!attr)
6240 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6241
6242 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6243 has computed. */
6244 if (attr && DW_STRING (attr))
6245 {
6246 char *demangled;
6247
6248 mangled = DW_STRING (attr);
6249
6250 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6251 type. It is easier for GDB users to search for such functions as
6252 `name(params)' than `long name(params)'. In such case the minimal
6253 symbol names do not match the full symbol names but for template
6254 functions there is never a need to look up their definition from their
6255 declaration so the only disadvantage remains the minimal symbol
6256 variant `long name(params)' does not have the proper inferior type.
6257 */
6258
6259 if (cu->language == language_go)
6260 {
6261 /* This is a lie, but we already lie to the caller new_symbol_full.
6262 new_symbol_full assumes we return the mangled name.
6263 This just undoes that lie until things are cleaned up. */
6264 demangled = NULL;
6265 }
6266 else
6267 {
6268 demangled = cplus_demangle (mangled,
6269 (DMGL_PARAMS | DMGL_ANSI
6270 | (cu->language == language_java
6271 ? DMGL_JAVA | DMGL_RET_POSTFIX
6272 : DMGL_RET_DROP)));
6273 }
6274 if (demangled)
6275 {
6276 make_cleanup (xfree, demangled);
6277 canon = demangled;
6278 }
6279 else
6280 {
6281 canon = mangled;
6282 need_copy = 0;
6283 }
6284 }
6285
6286 if (canon == NULL || check_physname)
6287 {
6288 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6289
6290 if (canon != NULL && strcmp (physname, canon) != 0)
6291 {
6292 /* It may not mean a bug in GDB. The compiler could also
6293 compute DW_AT_linkage_name incorrectly. But in such case
6294 GDB would need to be bug-to-bug compatible. */
6295
6296 complaint (&symfile_complaints,
6297 _("Computed physname <%s> does not match demangled <%s> "
6298 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6299 physname, canon, mangled, die->offset.sect_off, objfile->name);
6300
6301 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6302 is available here - over computed PHYSNAME. It is safer
6303 against both buggy GDB and buggy compilers. */
6304
6305 retval = canon;
6306 }
6307 else
6308 {
6309 retval = physname;
6310 need_copy = 0;
6311 }
6312 }
6313 else
6314 retval = canon;
6315
6316 if (need_copy)
6317 retval = obsavestring (retval, strlen (retval),
6318 &objfile->objfile_obstack);
6319
6320 do_cleanups (back_to);
6321 return retval;
6322 }
6323
6324 /* Read the import statement specified by the given die and record it. */
6325
6326 static void
6327 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6328 {
6329 struct objfile *objfile = cu->objfile;
6330 struct attribute *import_attr;
6331 struct die_info *imported_die, *child_die;
6332 struct dwarf2_cu *imported_cu;
6333 const char *imported_name;
6334 const char *imported_name_prefix;
6335 const char *canonical_name;
6336 const char *import_alias;
6337 const char *imported_declaration = NULL;
6338 const char *import_prefix;
6339 VEC (const_char_ptr) *excludes = NULL;
6340 struct cleanup *cleanups;
6341
6342 char *temp;
6343
6344 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6345 if (import_attr == NULL)
6346 {
6347 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6348 dwarf_tag_name (die->tag));
6349 return;
6350 }
6351
6352 imported_cu = cu;
6353 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6354 imported_name = dwarf2_name (imported_die, imported_cu);
6355 if (imported_name == NULL)
6356 {
6357 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6358
6359 The import in the following code:
6360 namespace A
6361 {
6362 typedef int B;
6363 }
6364
6365 int main ()
6366 {
6367 using A::B;
6368 B b;
6369 return b;
6370 }
6371
6372 ...
6373 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6374 <52> DW_AT_decl_file : 1
6375 <53> DW_AT_decl_line : 6
6376 <54> DW_AT_import : <0x75>
6377 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6378 <59> DW_AT_name : B
6379 <5b> DW_AT_decl_file : 1
6380 <5c> DW_AT_decl_line : 2
6381 <5d> DW_AT_type : <0x6e>
6382 ...
6383 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6384 <76> DW_AT_byte_size : 4
6385 <77> DW_AT_encoding : 5 (signed)
6386
6387 imports the wrong die ( 0x75 instead of 0x58 ).
6388 This case will be ignored until the gcc bug is fixed. */
6389 return;
6390 }
6391
6392 /* Figure out the local name after import. */
6393 import_alias = dwarf2_name (die, cu);
6394
6395 /* Figure out where the statement is being imported to. */
6396 import_prefix = determine_prefix (die, cu);
6397
6398 /* Figure out what the scope of the imported die is and prepend it
6399 to the name of the imported die. */
6400 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6401
6402 if (imported_die->tag != DW_TAG_namespace
6403 && imported_die->tag != DW_TAG_module)
6404 {
6405 imported_declaration = imported_name;
6406 canonical_name = imported_name_prefix;
6407 }
6408 else if (strlen (imported_name_prefix) > 0)
6409 {
6410 temp = alloca (strlen (imported_name_prefix)
6411 + 2 + strlen (imported_name) + 1);
6412 strcpy (temp, imported_name_prefix);
6413 strcat (temp, "::");
6414 strcat (temp, imported_name);
6415 canonical_name = temp;
6416 }
6417 else
6418 canonical_name = imported_name;
6419
6420 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6421
6422 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6423 for (child_die = die->child; child_die && child_die->tag;
6424 child_die = sibling_die (child_die))
6425 {
6426 /* DWARF-4: A Fortran use statement with a “rename list” may be
6427 represented by an imported module entry with an import attribute
6428 referring to the module and owned entries corresponding to those
6429 entities that are renamed as part of being imported. */
6430
6431 if (child_die->tag != DW_TAG_imported_declaration)
6432 {
6433 complaint (&symfile_complaints,
6434 _("child DW_TAG_imported_declaration expected "
6435 "- DIE at 0x%x [in module %s]"),
6436 child_die->offset.sect_off, objfile->name);
6437 continue;
6438 }
6439
6440 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6441 if (import_attr == NULL)
6442 {
6443 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6444 dwarf_tag_name (child_die->tag));
6445 continue;
6446 }
6447
6448 imported_cu = cu;
6449 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6450 &imported_cu);
6451 imported_name = dwarf2_name (imported_die, imported_cu);
6452 if (imported_name == NULL)
6453 {
6454 complaint (&symfile_complaints,
6455 _("child DW_TAG_imported_declaration has unknown "
6456 "imported name - DIE at 0x%x [in module %s]"),
6457 child_die->offset.sect_off, objfile->name);
6458 continue;
6459 }
6460
6461 VEC_safe_push (const_char_ptr, excludes, imported_name);
6462
6463 process_die (child_die, cu);
6464 }
6465
6466 cp_add_using_directive (import_prefix,
6467 canonical_name,
6468 import_alias,
6469 imported_declaration,
6470 excludes,
6471 &objfile->objfile_obstack);
6472
6473 do_cleanups (cleanups);
6474 }
6475
6476 /* Cleanup function for read_file_scope. */
6477
6478 static void
6479 free_cu_line_header (void *arg)
6480 {
6481 struct dwarf2_cu *cu = arg;
6482
6483 free_line_header (cu->line_header);
6484 cu->line_header = NULL;
6485 }
6486
6487 static void
6488 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6489 char **name, char **comp_dir)
6490 {
6491 struct attribute *attr;
6492
6493 *name = NULL;
6494 *comp_dir = NULL;
6495
6496 /* Find the filename. Do not use dwarf2_name here, since the filename
6497 is not a source language identifier. */
6498 attr = dwarf2_attr (die, DW_AT_name, cu);
6499 if (attr)
6500 {
6501 *name = DW_STRING (attr);
6502 }
6503
6504 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6505 if (attr)
6506 *comp_dir = DW_STRING (attr);
6507 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6508 {
6509 *comp_dir = ldirname (*name);
6510 if (*comp_dir != NULL)
6511 make_cleanup (xfree, *comp_dir);
6512 }
6513 if (*comp_dir != NULL)
6514 {
6515 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6516 directory, get rid of it. */
6517 char *cp = strchr (*comp_dir, ':');
6518
6519 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6520 *comp_dir = cp + 1;
6521 }
6522
6523 if (*name == NULL)
6524 *name = "<unknown>";
6525 }
6526
6527 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6528 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6529 COMP_DIR is the compilation directory.
6530 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6531
6532 static void
6533 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6534 const char *comp_dir, int want_line_info)
6535 {
6536 struct attribute *attr;
6537
6538 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6539 if (attr)
6540 {
6541 unsigned int line_offset = DW_UNSND (attr);
6542 struct line_header *line_header
6543 = dwarf_decode_line_header (line_offset, cu);
6544
6545 if (line_header)
6546 {
6547 cu->line_header = line_header;
6548 make_cleanup (free_cu_line_header, cu);
6549 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6550 }
6551 }
6552 }
6553
6554 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6555
6556 static void
6557 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6558 {
6559 struct objfile *objfile = dwarf2_per_objfile->objfile;
6560 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6561 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6562 CORE_ADDR highpc = ((CORE_ADDR) 0);
6563 struct attribute *attr;
6564 char *name = NULL;
6565 char *comp_dir = NULL;
6566 struct die_info *child_die;
6567 bfd *abfd = objfile->obfd;
6568 CORE_ADDR baseaddr;
6569
6570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6571
6572 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6573
6574 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6575 from finish_block. */
6576 if (lowpc == ((CORE_ADDR) -1))
6577 lowpc = highpc;
6578 lowpc += baseaddr;
6579 highpc += baseaddr;
6580
6581 find_file_and_directory (die, cu, &name, &comp_dir);
6582
6583 prepare_one_comp_unit (cu, die, cu->language);
6584
6585 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6586 standardised yet. As a workaround for the language detection we fall
6587 back to the DW_AT_producer string. */
6588 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6589 cu->language = language_opencl;
6590
6591 /* Similar hack for Go. */
6592 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6593 set_cu_language (DW_LANG_Go, cu);
6594
6595 /* We assume that we're processing GCC output. */
6596 processing_gcc_compilation = 2;
6597
6598 processing_has_namespace_info = 0;
6599
6600 start_symtab (name, comp_dir, lowpc);
6601 record_debugformat ("DWARF 2");
6602 record_producer (cu->producer);
6603
6604 /* Decode line number information if present. We do this before
6605 processing child DIEs, so that the line header table is available
6606 for DW_AT_decl_file. */
6607 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6608
6609 /* Process all dies in compilation unit. */
6610 if (die->child != NULL)
6611 {
6612 child_die = die->child;
6613 while (child_die && child_die->tag)
6614 {
6615 process_die (child_die, cu);
6616 child_die = sibling_die (child_die);
6617 }
6618 }
6619
6620 /* Decode macro information, if present. Dwarf 2 macro information
6621 refers to information in the line number info statement program
6622 header, so we can only read it if we've read the header
6623 successfully. */
6624 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6625 if (attr && cu->line_header)
6626 {
6627 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6628 complaint (&symfile_complaints,
6629 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6630
6631 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6632 comp_dir, abfd, cu,
6633 &dwarf2_per_objfile->macro, 1,
6634 ".debug_macro");
6635 }
6636 else
6637 {
6638 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6639 if (attr && cu->line_header)
6640 {
6641 unsigned int macro_offset = DW_UNSND (attr);
6642
6643 dwarf_decode_macros (cu->line_header, macro_offset,
6644 comp_dir, abfd, cu,
6645 &dwarf2_per_objfile->macinfo, 0,
6646 ".debug_macinfo");
6647 }
6648 }
6649
6650 do_cleanups (back_to);
6651 }
6652
6653 /* Process DW_TAG_type_unit.
6654 For TUs we want to skip the first top level sibling if it's not the
6655 actual type being defined by this TU. In this case the first top
6656 level sibling is there to provide context only. */
6657
6658 static void
6659 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6660 {
6661 struct objfile *objfile = cu->objfile;
6662 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6663 CORE_ADDR lowpc;
6664 struct attribute *attr;
6665 char *name = NULL;
6666 char *comp_dir = NULL;
6667 struct die_info *child_die;
6668 bfd *abfd = objfile->obfd;
6669
6670 /* start_symtab needs a low pc, but we don't really have one.
6671 Do what read_file_scope would do in the absence of such info. */
6672 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6673
6674 /* Find the filename. Do not use dwarf2_name here, since the filename
6675 is not a source language identifier. */
6676 attr = dwarf2_attr (die, DW_AT_name, cu);
6677 if (attr)
6678 name = DW_STRING (attr);
6679
6680 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6681 if (attr)
6682 comp_dir = DW_STRING (attr);
6683 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6684 {
6685 comp_dir = ldirname (name);
6686 if (comp_dir != NULL)
6687 make_cleanup (xfree, comp_dir);
6688 }
6689
6690 if (name == NULL)
6691 name = "<unknown>";
6692
6693 prepare_one_comp_unit (cu, die, language_minimal);
6694
6695 /* We assume that we're processing GCC output. */
6696 processing_gcc_compilation = 2;
6697
6698 processing_has_namespace_info = 0;
6699
6700 start_symtab (name, comp_dir, lowpc);
6701 record_debugformat ("DWARF 2");
6702 record_producer (cu->producer);
6703
6704 /* Decode line number information if present. We do this before
6705 processing child DIEs, so that the line header table is available
6706 for DW_AT_decl_file.
6707 We don't need the pc/line-number mapping for type units. */
6708 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6709
6710 /* Process the dies in the type unit. */
6711 if (die->child == NULL)
6712 {
6713 dump_die_for_error (die);
6714 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6715 bfd_get_filename (abfd));
6716 }
6717
6718 child_die = die->child;
6719
6720 while (child_die && child_die->tag)
6721 {
6722 process_die (child_die, cu);
6723
6724 child_die = sibling_die (child_die);
6725 }
6726
6727 do_cleanups (back_to);
6728 }
6729 \f
6730 /* DWO files. */
6731
6732 static hashval_t
6733 hash_dwo_file (const void *item)
6734 {
6735 const struct dwo_file *dwo_file = item;
6736
6737 return htab_hash_string (dwo_file->dwo_name);
6738 }
6739
6740 static int
6741 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6742 {
6743 const struct dwo_file *lhs = item_lhs;
6744 const struct dwo_file *rhs = item_rhs;
6745
6746 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6747 }
6748
6749 /* Allocate a hash table for DWO files. */
6750
6751 static htab_t
6752 allocate_dwo_file_hash_table (void)
6753 {
6754 struct objfile *objfile = dwarf2_per_objfile->objfile;
6755
6756 return htab_create_alloc_ex (41,
6757 hash_dwo_file,
6758 eq_dwo_file,
6759 NULL,
6760 &objfile->objfile_obstack,
6761 hashtab_obstack_allocate,
6762 dummy_obstack_deallocate);
6763 }
6764
6765 static hashval_t
6766 hash_dwo_unit (const void *item)
6767 {
6768 const struct dwo_unit *dwo_unit = item;
6769
6770 /* This drops the top 32 bits of the id, but is ok for a hash. */
6771 return dwo_unit->signature;
6772 }
6773
6774 static int
6775 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6776 {
6777 const struct dwo_unit *lhs = item_lhs;
6778 const struct dwo_unit *rhs = item_rhs;
6779
6780 /* The signature is assumed to be unique within the DWO file.
6781 So while object file CU dwo_id's always have the value zero,
6782 that's OK, assuming each object file DWO file has only one CU,
6783 and that's the rule for now. */
6784 return lhs->signature == rhs->signature;
6785 }
6786
6787 /* Allocate a hash table for DWO CUs,TUs.
6788 There is one of these tables for each of CUs,TUs for each DWO file. */
6789
6790 static htab_t
6791 allocate_dwo_unit_table (struct objfile *objfile)
6792 {
6793 /* Start out with a pretty small number.
6794 Generally DWO files contain only one CU and maybe some TUs. */
6795 return htab_create_alloc_ex (3,
6796 hash_dwo_unit,
6797 eq_dwo_unit,
6798 NULL,
6799 &objfile->objfile_obstack,
6800 hashtab_obstack_allocate,
6801 dummy_obstack_deallocate);
6802 }
6803
6804 /* This function is mapped across the sections and remembers the offset and
6805 size of each of the DWO debugging sections we are interested in. */
6806
6807 static void
6808 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6809 {
6810 struct dwo_file *dwo_file = dwo_file_ptr;
6811 const struct dwo_section_names *names = &dwo_section_names;
6812
6813 if (section_is_p (sectp->name, &names->abbrev_dwo))
6814 {
6815 dwo_file->sections.abbrev.asection = sectp;
6816 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6817 }
6818 else if (section_is_p (sectp->name, &names->info_dwo))
6819 {
6820 dwo_file->sections.info.asection = sectp;
6821 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6822 }
6823 else if (section_is_p (sectp->name, &names->line_dwo))
6824 {
6825 dwo_file->sections.line.asection = sectp;
6826 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6827 }
6828 else if (section_is_p (sectp->name, &names->loc_dwo))
6829 {
6830 dwo_file->sections.loc.asection = sectp;
6831 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6832 }
6833 else if (section_is_p (sectp->name, &names->str_dwo))
6834 {
6835 dwo_file->sections.str.asection = sectp;
6836 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6837 }
6838 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6839 {
6840 dwo_file->sections.str_offsets.asection = sectp;
6841 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6842 }
6843 else if (section_is_p (sectp->name, &names->types_dwo))
6844 {
6845 struct dwarf2_section_info type_section;
6846
6847 memset (&type_section, 0, sizeof (type_section));
6848 type_section.asection = sectp;
6849 type_section.size = bfd_get_section_size (sectp);
6850 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6851 &type_section);
6852 }
6853 }
6854
6855 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6856
6857 struct create_dwo_info_table_data
6858 {
6859 struct dwo_file *dwo_file;
6860 htab_t cu_htab;
6861 };
6862
6863 /* die_reader_func for create_debug_info_hash_table. */
6864
6865 static void
6866 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6867 gdb_byte *info_ptr,
6868 struct die_info *comp_unit_die,
6869 int has_children,
6870 void *datap)
6871 {
6872 struct dwarf2_cu *cu = reader->cu;
6873 struct objfile *objfile = dwarf2_per_objfile->objfile;
6874 sect_offset offset = cu->per_cu->offset;
6875 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6876 struct create_dwo_info_table_data *data = datap;
6877 struct dwo_file *dwo_file = data->dwo_file;
6878 htab_t cu_htab = data->cu_htab;
6879 void **slot;
6880 struct attribute *attr;
6881 struct dwo_unit *dwo_unit;
6882
6883 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6884 if (attr == NULL)
6885 {
6886 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6887 " its dwo_id [in module %s]"),
6888 offset.sect_off, dwo_file->dwo_name);
6889 return;
6890 }
6891
6892 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6893 dwo_unit->dwo_file = dwo_file;
6894 dwo_unit->signature = DW_UNSND (attr);
6895 dwo_unit->info_or_types_section = section;
6896 dwo_unit->offset = offset;
6897 dwo_unit->length = cu->per_cu->length;
6898
6899 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6900 gdb_assert (slot != NULL);
6901 if (*slot != NULL)
6902 {
6903 const struct dwo_unit *dup_dwo_unit = *slot;
6904
6905 complaint (&symfile_complaints,
6906 _("debug entry at offset 0x%x is duplicate to the entry at"
6907 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6908 offset.sect_off, dup_dwo_unit->offset.sect_off,
6909 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6910 dwo_file->dwo_name);
6911 }
6912 else
6913 *slot = dwo_unit;
6914
6915 if (dwarf2_die_debug)
6916 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6917 offset.sect_off,
6918 phex (dwo_unit->signature,
6919 sizeof (dwo_unit->signature)));
6920 }
6921
6922 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6923
6924 static htab_t
6925 create_debug_info_hash_table (struct dwo_file *dwo_file)
6926 {
6927 struct objfile *objfile = dwarf2_per_objfile->objfile;
6928 struct dwarf2_section_info *section = &dwo_file->sections.info;
6929 bfd *abfd;
6930 htab_t cu_htab;
6931 gdb_byte *info_ptr, *end_ptr;
6932 struct create_dwo_info_table_data create_dwo_info_table_data;
6933
6934 dwarf2_read_section (objfile, section);
6935 info_ptr = section->buffer;
6936
6937 if (info_ptr == NULL)
6938 return NULL;
6939
6940 /* We can't set abfd until now because the section may be empty or
6941 not present, in which case section->asection will be NULL. */
6942 abfd = section->asection->owner;
6943
6944 if (dwarf2_die_debug)
6945 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6946 bfd_get_filename (abfd));
6947
6948 cu_htab = allocate_dwo_unit_table (objfile);
6949
6950 create_dwo_info_table_data.dwo_file = dwo_file;
6951 create_dwo_info_table_data.cu_htab = cu_htab;
6952
6953 end_ptr = info_ptr + section->size;
6954 while (info_ptr < end_ptr)
6955 {
6956 struct dwarf2_per_cu_data per_cu;
6957
6958 memset (&per_cu, 0, sizeof (per_cu));
6959 per_cu.objfile = objfile;
6960 per_cu.is_debug_types = 0;
6961 per_cu.offset.sect_off = info_ptr - section->buffer;
6962 per_cu.info_or_types_section = section;
6963
6964 init_cutu_and_read_dies_no_follow (&per_cu,
6965 &dwo_file->sections.abbrev,
6966 dwo_file,
6967 create_debug_info_hash_table_reader,
6968 &create_dwo_info_table_data);
6969
6970 info_ptr += per_cu.length;
6971 }
6972
6973 return cu_htab;
6974 }
6975
6976 /* Subroutine of open_dwo_file to simplify it.
6977 Open the file specified by FILE_NAME and hand it off to BFD for
6978 preliminary analysis. Return a newly initialized bfd *, which
6979 includes a canonicalized copy of FILE_NAME.
6980 In case of trouble, return NULL.
6981 NOTE: This function is derived from symfile_bfd_open. */
6982
6983 static bfd *
6984 try_open_dwo_file (const char *file_name)
6985 {
6986 bfd *sym_bfd;
6987 int desc;
6988 char *absolute_name;
6989
6990 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6991 O_RDONLY | O_BINARY, &absolute_name);
6992 if (desc < 0)
6993 return NULL;
6994
6995 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6996 if (!sym_bfd)
6997 {
6998 xfree (absolute_name);
6999 return NULL;
7000 }
7001 bfd_set_cacheable (sym_bfd, 1);
7002
7003 if (!bfd_check_format (sym_bfd, bfd_object))
7004 {
7005 bfd_close (sym_bfd); /* This also closes desc. */
7006 xfree (absolute_name);
7007 return NULL;
7008 }
7009
7010 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7011 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7012
7013 return sym_bfd;
7014 }
7015
7016 /* Try to open DWO file DWO_NAME.
7017 COMP_DIR is the DW_AT_comp_dir attribute.
7018 The result is the bfd handle of the file.
7019 If there is a problem finding or opening the file, return NULL.
7020 Upon success, the canonicalized path of the file is stored in the bfd,
7021 same as symfile_bfd_open. */
7022
7023 static bfd *
7024 open_dwo_file (const char *dwo_name, const char *comp_dir)
7025 {
7026 bfd *abfd;
7027
7028 if (IS_ABSOLUTE_PATH (dwo_name))
7029 return try_open_dwo_file (dwo_name);
7030
7031 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7032
7033 if (comp_dir != NULL)
7034 {
7035 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7036
7037 /* NOTE: If comp_dir is a relative path, this will also try the
7038 search path, which seems useful. */
7039 abfd = try_open_dwo_file (path_to_try);
7040 xfree (path_to_try);
7041 if (abfd != NULL)
7042 return abfd;
7043 }
7044
7045 /* That didn't work, try debug-file-directory, which, despite its name,
7046 is a list of paths. */
7047
7048 if (*debug_file_directory == '\0')
7049 return NULL;
7050
7051 return try_open_dwo_file (dwo_name);
7052 }
7053
7054 /* Initialize the use of the DWO file specified by DWO_NAME. */
7055
7056 static struct dwo_file *
7057 init_dwo_file (const char *dwo_name, const char *comp_dir)
7058 {
7059 struct objfile *objfile = dwarf2_per_objfile->objfile;
7060 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7061 struct dwo_file);
7062 bfd *abfd;
7063 struct cleanup *cleanups;
7064
7065 if (dwarf2_die_debug)
7066 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7067
7068 abfd = open_dwo_file (dwo_name, comp_dir);
7069 if (abfd == NULL)
7070 return NULL;
7071 dwo_file->dwo_name = dwo_name;
7072 dwo_file->dwo_bfd = abfd;
7073
7074 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7075
7076 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7077
7078 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7079
7080 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7081 dwo_file->sections.types);
7082
7083 discard_cleanups (cleanups);
7084
7085 return dwo_file;
7086 }
7087
7088 /* Lookup DWO file DWO_NAME. */
7089
7090 static struct dwo_file *
7091 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7092 {
7093 struct dwo_file *dwo_file;
7094 struct dwo_file find_entry;
7095 void **slot;
7096
7097 if (dwarf2_per_objfile->dwo_files == NULL)
7098 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7099
7100 /* Have we already seen this DWO file? */
7101 find_entry.dwo_name = dwo_name;
7102 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7103
7104 /* If not, read it in and build a table of the DWOs it contains. */
7105 if (*slot == NULL)
7106 *slot = init_dwo_file (dwo_name, comp_dir);
7107
7108 /* NOTE: This will be NULL if unable to open the file. */
7109 dwo_file = *slot;
7110
7111 return dwo_file;
7112 }
7113
7114 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7115 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7116 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7117 nomenclature as TUs).
7118 The result is the DWO CU or NULL if we didn't find it
7119 (dwo_id mismatch or couldn't find the DWO file). */
7120
7121 static struct dwo_unit *
7122 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7123 char *dwo_name, const char *comp_dir,
7124 ULONGEST signature)
7125 {
7126 struct objfile *objfile = dwarf2_per_objfile->objfile;
7127 struct dwo_file *dwo_file;
7128
7129 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7130 if (dwo_file == NULL)
7131 return NULL;
7132
7133 /* Look up the DWO using its signature(dwo_id). */
7134
7135 if (dwo_file->cus != NULL)
7136 {
7137 struct dwo_unit find_dwo_cu, *dwo_cu;
7138
7139 find_dwo_cu.signature = signature;
7140 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7141
7142 if (dwo_cu != NULL)
7143 return dwo_cu;
7144 }
7145
7146 /* We didn't find it. This must mean a dwo_id mismatch. */
7147
7148 complaint (&symfile_complaints,
7149 _("Could not find DWO CU referenced by CU at offset 0x%x"
7150 " [in module %s]"),
7151 this_cu->offset.sect_off, objfile->name);
7152 return NULL;
7153 }
7154
7155 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7156 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7157 The result is the DWO CU or NULL if we didn't find it
7158 (dwo_id mismatch or couldn't find the DWO file). */
7159
7160 static struct dwo_unit *
7161 lookup_dwo_type_unit (struct signatured_type *this_tu,
7162 char *dwo_name, const char *comp_dir)
7163 {
7164 struct objfile *objfile = dwarf2_per_objfile->objfile;
7165 struct dwo_file *dwo_file;
7166
7167 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7168 if (dwo_file == NULL)
7169 return NULL;
7170
7171 /* Look up the DWO using its signature(dwo_id). */
7172
7173 if (dwo_file->tus != NULL)
7174 {
7175 struct dwo_unit find_dwo_tu, *dwo_tu;
7176
7177 find_dwo_tu.signature = this_tu->signature;
7178 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7179
7180 if (dwo_tu != NULL)
7181 return dwo_tu;
7182 }
7183
7184 /* We didn't find it. This must mean a dwo_id mismatch. */
7185
7186 complaint (&symfile_complaints,
7187 _("Could not find DWO TU referenced by TU at offset 0x%x"
7188 " [in module %s]"),
7189 this_tu->per_cu.offset.sect_off, objfile->name);
7190 return NULL;
7191 }
7192
7193 /* Free all resources associated with DWO_FILE.
7194 Close the DWO file and munmap the sections.
7195 All memory should be on the objfile obstack. */
7196
7197 static void
7198 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7199 {
7200 int ix;
7201 struct dwarf2_section_info *section;
7202
7203 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7204 bfd_close (dwo_file->dwo_bfd);
7205
7206 munmap_section_buffer (&dwo_file->sections.abbrev);
7207 munmap_section_buffer (&dwo_file->sections.info);
7208 munmap_section_buffer (&dwo_file->sections.line);
7209 munmap_section_buffer (&dwo_file->sections.loc);
7210 munmap_section_buffer (&dwo_file->sections.str);
7211 munmap_section_buffer (&dwo_file->sections.str_offsets);
7212
7213 for (ix = 0;
7214 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7215 ix, section);
7216 ++ix)
7217 munmap_section_buffer (section);
7218
7219 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7220 }
7221
7222 /* Wrapper for free_dwo_file for use in cleanups. */
7223
7224 static void
7225 free_dwo_file_cleanup (void *arg)
7226 {
7227 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7228 struct objfile *objfile = dwarf2_per_objfile->objfile;
7229
7230 free_dwo_file (dwo_file, objfile);
7231 }
7232
7233 /* Traversal function for free_dwo_files. */
7234
7235 static int
7236 free_dwo_file_from_slot (void **slot, void *info)
7237 {
7238 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7239 struct objfile *objfile = (struct objfile *) info;
7240
7241 free_dwo_file (dwo_file, objfile);
7242
7243 return 1;
7244 }
7245
7246 /* Free all resources associated with DWO_FILES. */
7247
7248 static void
7249 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7250 {
7251 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7252 }
7253 \f
7254 /* Read in various DIEs. */
7255
7256 /* qsort helper for inherit_abstract_dies. */
7257
7258 static int
7259 unsigned_int_compar (const void *ap, const void *bp)
7260 {
7261 unsigned int a = *(unsigned int *) ap;
7262 unsigned int b = *(unsigned int *) bp;
7263
7264 return (a > b) - (b > a);
7265 }
7266
7267 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7268 Inherit only the children of the DW_AT_abstract_origin DIE not being
7269 already referenced by DW_AT_abstract_origin from the children of the
7270 current DIE. */
7271
7272 static void
7273 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7274 {
7275 struct die_info *child_die;
7276 unsigned die_children_count;
7277 /* CU offsets which were referenced by children of the current DIE. */
7278 sect_offset *offsets;
7279 sect_offset *offsets_end, *offsetp;
7280 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7281 struct die_info *origin_die;
7282 /* Iterator of the ORIGIN_DIE children. */
7283 struct die_info *origin_child_die;
7284 struct cleanup *cleanups;
7285 struct attribute *attr;
7286 struct dwarf2_cu *origin_cu;
7287 struct pending **origin_previous_list_in_scope;
7288
7289 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7290 if (!attr)
7291 return;
7292
7293 /* Note that following die references may follow to a die in a
7294 different cu. */
7295
7296 origin_cu = cu;
7297 origin_die = follow_die_ref (die, attr, &origin_cu);
7298
7299 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7300 symbols in. */
7301 origin_previous_list_in_scope = origin_cu->list_in_scope;
7302 origin_cu->list_in_scope = cu->list_in_scope;
7303
7304 if (die->tag != origin_die->tag
7305 && !(die->tag == DW_TAG_inlined_subroutine
7306 && origin_die->tag == DW_TAG_subprogram))
7307 complaint (&symfile_complaints,
7308 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7309 die->offset.sect_off, origin_die->offset.sect_off);
7310
7311 child_die = die->child;
7312 die_children_count = 0;
7313 while (child_die && child_die->tag)
7314 {
7315 child_die = sibling_die (child_die);
7316 die_children_count++;
7317 }
7318 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7319 cleanups = make_cleanup (xfree, offsets);
7320
7321 offsets_end = offsets;
7322 child_die = die->child;
7323 while (child_die && child_die->tag)
7324 {
7325 /* For each CHILD_DIE, find the corresponding child of
7326 ORIGIN_DIE. If there is more than one layer of
7327 DW_AT_abstract_origin, follow them all; there shouldn't be,
7328 but GCC versions at least through 4.4 generate this (GCC PR
7329 40573). */
7330 struct die_info *child_origin_die = child_die;
7331 struct dwarf2_cu *child_origin_cu = cu;
7332
7333 while (1)
7334 {
7335 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7336 child_origin_cu);
7337 if (attr == NULL)
7338 break;
7339 child_origin_die = follow_die_ref (child_origin_die, attr,
7340 &child_origin_cu);
7341 }
7342
7343 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7344 counterpart may exist. */
7345 if (child_origin_die != child_die)
7346 {
7347 if (child_die->tag != child_origin_die->tag
7348 && !(child_die->tag == DW_TAG_inlined_subroutine
7349 && child_origin_die->tag == DW_TAG_subprogram))
7350 complaint (&symfile_complaints,
7351 _("Child DIE 0x%x and its abstract origin 0x%x have "
7352 "different tags"), child_die->offset.sect_off,
7353 child_origin_die->offset.sect_off);
7354 if (child_origin_die->parent != origin_die)
7355 complaint (&symfile_complaints,
7356 _("Child DIE 0x%x and its abstract origin 0x%x have "
7357 "different parents"), child_die->offset.sect_off,
7358 child_origin_die->offset.sect_off);
7359 else
7360 *offsets_end++ = child_origin_die->offset;
7361 }
7362 child_die = sibling_die (child_die);
7363 }
7364 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7365 unsigned_int_compar);
7366 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7367 if (offsetp[-1].sect_off == offsetp->sect_off)
7368 complaint (&symfile_complaints,
7369 _("Multiple children of DIE 0x%x refer "
7370 "to DIE 0x%x as their abstract origin"),
7371 die->offset.sect_off, offsetp->sect_off);
7372
7373 offsetp = offsets;
7374 origin_child_die = origin_die->child;
7375 while (origin_child_die && origin_child_die->tag)
7376 {
7377 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7378 while (offsetp < offsets_end
7379 && offsetp->sect_off < origin_child_die->offset.sect_off)
7380 offsetp++;
7381 if (offsetp >= offsets_end
7382 || offsetp->sect_off > origin_child_die->offset.sect_off)
7383 {
7384 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7385 process_die (origin_child_die, origin_cu);
7386 }
7387 origin_child_die = sibling_die (origin_child_die);
7388 }
7389 origin_cu->list_in_scope = origin_previous_list_in_scope;
7390
7391 do_cleanups (cleanups);
7392 }
7393
7394 static void
7395 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7396 {
7397 struct objfile *objfile = cu->objfile;
7398 struct context_stack *new;
7399 CORE_ADDR lowpc;
7400 CORE_ADDR highpc;
7401 struct die_info *child_die;
7402 struct attribute *attr, *call_line, *call_file;
7403 char *name;
7404 CORE_ADDR baseaddr;
7405 struct block *block;
7406 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7407 VEC (symbolp) *template_args = NULL;
7408 struct template_symbol *templ_func = NULL;
7409
7410 if (inlined_func)
7411 {
7412 /* If we do not have call site information, we can't show the
7413 caller of this inlined function. That's too confusing, so
7414 only use the scope for local variables. */
7415 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7416 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7417 if (call_line == NULL || call_file == NULL)
7418 {
7419 read_lexical_block_scope (die, cu);
7420 return;
7421 }
7422 }
7423
7424 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7425
7426 name = dwarf2_name (die, cu);
7427
7428 /* Ignore functions with missing or empty names. These are actually
7429 illegal according to the DWARF standard. */
7430 if (name == NULL)
7431 {
7432 complaint (&symfile_complaints,
7433 _("missing name for subprogram DIE at %d"),
7434 die->offset.sect_off);
7435 return;
7436 }
7437
7438 /* Ignore functions with missing or invalid low and high pc attributes. */
7439 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7440 {
7441 attr = dwarf2_attr (die, DW_AT_external, cu);
7442 if (!attr || !DW_UNSND (attr))
7443 complaint (&symfile_complaints,
7444 _("cannot get low and high bounds "
7445 "for subprogram DIE at %d"),
7446 die->offset.sect_off);
7447 return;
7448 }
7449
7450 lowpc += baseaddr;
7451 highpc += baseaddr;
7452
7453 /* If we have any template arguments, then we must allocate a
7454 different sort of symbol. */
7455 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7456 {
7457 if (child_die->tag == DW_TAG_template_type_param
7458 || child_die->tag == DW_TAG_template_value_param)
7459 {
7460 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7461 struct template_symbol);
7462 templ_func->base.is_cplus_template_function = 1;
7463 break;
7464 }
7465 }
7466
7467 new = push_context (0, lowpc);
7468 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7469 (struct symbol *) templ_func);
7470
7471 /* If there is a location expression for DW_AT_frame_base, record
7472 it. */
7473 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7474 if (attr)
7475 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7476 expression is being recorded directly in the function's symbol
7477 and not in a separate frame-base object. I guess this hack is
7478 to avoid adding some sort of frame-base adjunct/annex to the
7479 function's symbol :-(. The problem with doing this is that it
7480 results in a function symbol with a location expression that
7481 has nothing to do with the location of the function, ouch! The
7482 relationship should be: a function's symbol has-a frame base; a
7483 frame-base has-a location expression. */
7484 dwarf2_symbol_mark_computed (attr, new->name, cu);
7485
7486 cu->list_in_scope = &local_symbols;
7487
7488 if (die->child != NULL)
7489 {
7490 child_die = die->child;
7491 while (child_die && child_die->tag)
7492 {
7493 if (child_die->tag == DW_TAG_template_type_param
7494 || child_die->tag == DW_TAG_template_value_param)
7495 {
7496 struct symbol *arg = new_symbol (child_die, NULL, cu);
7497
7498 if (arg != NULL)
7499 VEC_safe_push (symbolp, template_args, arg);
7500 }
7501 else
7502 process_die (child_die, cu);
7503 child_die = sibling_die (child_die);
7504 }
7505 }
7506
7507 inherit_abstract_dies (die, cu);
7508
7509 /* If we have a DW_AT_specification, we might need to import using
7510 directives from the context of the specification DIE. See the
7511 comment in determine_prefix. */
7512 if (cu->language == language_cplus
7513 && dwarf2_attr (die, DW_AT_specification, cu))
7514 {
7515 struct dwarf2_cu *spec_cu = cu;
7516 struct die_info *spec_die = die_specification (die, &spec_cu);
7517
7518 while (spec_die)
7519 {
7520 child_die = spec_die->child;
7521 while (child_die && child_die->tag)
7522 {
7523 if (child_die->tag == DW_TAG_imported_module)
7524 process_die (child_die, spec_cu);
7525 child_die = sibling_die (child_die);
7526 }
7527
7528 /* In some cases, GCC generates specification DIEs that
7529 themselves contain DW_AT_specification attributes. */
7530 spec_die = die_specification (spec_die, &spec_cu);
7531 }
7532 }
7533
7534 new = pop_context ();
7535 /* Make a block for the local symbols within. */
7536 block = finish_block (new->name, &local_symbols, new->old_blocks,
7537 lowpc, highpc, objfile);
7538
7539 /* For C++, set the block's scope. */
7540 if (cu->language == language_cplus || cu->language == language_fortran)
7541 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7542 determine_prefix (die, cu),
7543 processing_has_namespace_info);
7544
7545 /* If we have address ranges, record them. */
7546 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7547
7548 /* Attach template arguments to function. */
7549 if (! VEC_empty (symbolp, template_args))
7550 {
7551 gdb_assert (templ_func != NULL);
7552
7553 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7554 templ_func->template_arguments
7555 = obstack_alloc (&objfile->objfile_obstack,
7556 (templ_func->n_template_arguments
7557 * sizeof (struct symbol *)));
7558 memcpy (templ_func->template_arguments,
7559 VEC_address (symbolp, template_args),
7560 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7561 VEC_free (symbolp, template_args);
7562 }
7563
7564 /* In C++, we can have functions nested inside functions (e.g., when
7565 a function declares a class that has methods). This means that
7566 when we finish processing a function scope, we may need to go
7567 back to building a containing block's symbol lists. */
7568 local_symbols = new->locals;
7569 param_symbols = new->params;
7570 using_directives = new->using_directives;
7571
7572 /* If we've finished processing a top-level function, subsequent
7573 symbols go in the file symbol list. */
7574 if (outermost_context_p ())
7575 cu->list_in_scope = &file_symbols;
7576 }
7577
7578 /* Process all the DIES contained within a lexical block scope. Start
7579 a new scope, process the dies, and then close the scope. */
7580
7581 static void
7582 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7583 {
7584 struct objfile *objfile = cu->objfile;
7585 struct context_stack *new;
7586 CORE_ADDR lowpc, highpc;
7587 struct die_info *child_die;
7588 CORE_ADDR baseaddr;
7589
7590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7591
7592 /* Ignore blocks with missing or invalid low and high pc attributes. */
7593 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7594 as multiple lexical blocks? Handling children in a sane way would
7595 be nasty. Might be easier to properly extend generic blocks to
7596 describe ranges. */
7597 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7598 return;
7599 lowpc += baseaddr;
7600 highpc += baseaddr;
7601
7602 push_context (0, lowpc);
7603 if (die->child != NULL)
7604 {
7605 child_die = die->child;
7606 while (child_die && child_die->tag)
7607 {
7608 process_die (child_die, cu);
7609 child_die = sibling_die (child_die);
7610 }
7611 }
7612 new = pop_context ();
7613
7614 if (local_symbols != NULL || using_directives != NULL)
7615 {
7616 struct block *block
7617 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7618 highpc, objfile);
7619
7620 /* Note that recording ranges after traversing children, as we
7621 do here, means that recording a parent's ranges entails
7622 walking across all its children's ranges as they appear in
7623 the address map, which is quadratic behavior.
7624
7625 It would be nicer to record the parent's ranges before
7626 traversing its children, simply overriding whatever you find
7627 there. But since we don't even decide whether to create a
7628 block until after we've traversed its children, that's hard
7629 to do. */
7630 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7631 }
7632 local_symbols = new->locals;
7633 using_directives = new->using_directives;
7634 }
7635
7636 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7637
7638 static void
7639 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7640 {
7641 struct objfile *objfile = cu->objfile;
7642 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7643 CORE_ADDR pc, baseaddr;
7644 struct attribute *attr;
7645 struct call_site *call_site, call_site_local;
7646 void **slot;
7647 int nparams;
7648 struct die_info *child_die;
7649
7650 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7651
7652 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7653 if (!attr)
7654 {
7655 complaint (&symfile_complaints,
7656 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7657 "DIE 0x%x [in module %s]"),
7658 die->offset.sect_off, objfile->name);
7659 return;
7660 }
7661 pc = DW_ADDR (attr) + baseaddr;
7662
7663 if (cu->call_site_htab == NULL)
7664 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7665 NULL, &objfile->objfile_obstack,
7666 hashtab_obstack_allocate, NULL);
7667 call_site_local.pc = pc;
7668 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7669 if (*slot != NULL)
7670 {
7671 complaint (&symfile_complaints,
7672 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7673 "DIE 0x%x [in module %s]"),
7674 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7675 return;
7676 }
7677
7678 /* Count parameters at the caller. */
7679
7680 nparams = 0;
7681 for (child_die = die->child; child_die && child_die->tag;
7682 child_die = sibling_die (child_die))
7683 {
7684 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7685 {
7686 complaint (&symfile_complaints,
7687 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7688 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7689 child_die->tag, child_die->offset.sect_off, objfile->name);
7690 continue;
7691 }
7692
7693 nparams++;
7694 }
7695
7696 call_site = obstack_alloc (&objfile->objfile_obstack,
7697 (sizeof (*call_site)
7698 + (sizeof (*call_site->parameter)
7699 * (nparams - 1))));
7700 *slot = call_site;
7701 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7702 call_site->pc = pc;
7703
7704 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7705 {
7706 struct die_info *func_die;
7707
7708 /* Skip also over DW_TAG_inlined_subroutine. */
7709 for (func_die = die->parent;
7710 func_die && func_die->tag != DW_TAG_subprogram
7711 && func_die->tag != DW_TAG_subroutine_type;
7712 func_die = func_die->parent);
7713
7714 /* DW_AT_GNU_all_call_sites is a superset
7715 of DW_AT_GNU_all_tail_call_sites. */
7716 if (func_die
7717 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7718 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7719 {
7720 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7721 not complete. But keep CALL_SITE for look ups via call_site_htab,
7722 both the initial caller containing the real return address PC and
7723 the final callee containing the current PC of a chain of tail
7724 calls do not need to have the tail call list complete. But any
7725 function candidate for a virtual tail call frame searched via
7726 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7727 determined unambiguously. */
7728 }
7729 else
7730 {
7731 struct type *func_type = NULL;
7732
7733 if (func_die)
7734 func_type = get_die_type (func_die, cu);
7735 if (func_type != NULL)
7736 {
7737 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7738
7739 /* Enlist this call site to the function. */
7740 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7741 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7742 }
7743 else
7744 complaint (&symfile_complaints,
7745 _("Cannot find function owning DW_TAG_GNU_call_site "
7746 "DIE 0x%x [in module %s]"),
7747 die->offset.sect_off, objfile->name);
7748 }
7749 }
7750
7751 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7752 if (attr == NULL)
7753 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7754 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7755 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7756 /* Keep NULL DWARF_BLOCK. */;
7757 else if (attr_form_is_block (attr))
7758 {
7759 struct dwarf2_locexpr_baton *dlbaton;
7760
7761 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7762 dlbaton->data = DW_BLOCK (attr)->data;
7763 dlbaton->size = DW_BLOCK (attr)->size;
7764 dlbaton->per_cu = cu->per_cu;
7765
7766 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7767 }
7768 else if (is_ref_attr (attr))
7769 {
7770 struct dwarf2_cu *target_cu = cu;
7771 struct die_info *target_die;
7772
7773 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7774 gdb_assert (target_cu->objfile == objfile);
7775 if (die_is_declaration (target_die, target_cu))
7776 {
7777 const char *target_physname;
7778
7779 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7780 if (target_physname == NULL)
7781 complaint (&symfile_complaints,
7782 _("DW_AT_GNU_call_site_target target DIE has invalid "
7783 "physname, for referencing DIE 0x%x [in module %s]"),
7784 die->offset.sect_off, objfile->name);
7785 else
7786 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7787 }
7788 else
7789 {
7790 CORE_ADDR lowpc;
7791
7792 /* DW_AT_entry_pc should be preferred. */
7793 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7794 complaint (&symfile_complaints,
7795 _("DW_AT_GNU_call_site_target target DIE has invalid "
7796 "low pc, for referencing DIE 0x%x [in module %s]"),
7797 die->offset.sect_off, objfile->name);
7798 else
7799 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7800 }
7801 }
7802 else
7803 complaint (&symfile_complaints,
7804 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7805 "block nor reference, for DIE 0x%x [in module %s]"),
7806 die->offset.sect_off, objfile->name);
7807
7808 call_site->per_cu = cu->per_cu;
7809
7810 for (child_die = die->child;
7811 child_die && child_die->tag;
7812 child_die = sibling_die (child_die))
7813 {
7814 struct call_site_parameter *parameter;
7815
7816 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7817 {
7818 /* Already printed the complaint above. */
7819 continue;
7820 }
7821
7822 gdb_assert (call_site->parameter_count < nparams);
7823 parameter = &call_site->parameter[call_site->parameter_count];
7824
7825 /* DW_AT_location specifies the register number. Value of the data
7826 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7827
7828 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7829 if (!attr || !attr_form_is_block (attr))
7830 {
7831 complaint (&symfile_complaints,
7832 _("No DW_FORM_block* DW_AT_location for "
7833 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7834 child_die->offset.sect_off, objfile->name);
7835 continue;
7836 }
7837 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7838 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7839 if (parameter->dwarf_reg == -1
7840 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7841 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7842 &parameter->fb_offset))
7843 {
7844 complaint (&symfile_complaints,
7845 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7846 "for DW_FORM_block* DW_AT_location for "
7847 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7848 child_die->offset.sect_off, objfile->name);
7849 continue;
7850 }
7851
7852 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7853 if (!attr_form_is_block (attr))
7854 {
7855 complaint (&symfile_complaints,
7856 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7857 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7858 child_die->offset.sect_off, objfile->name);
7859 continue;
7860 }
7861 parameter->value = DW_BLOCK (attr)->data;
7862 parameter->value_size = DW_BLOCK (attr)->size;
7863
7864 /* Parameters are not pre-cleared by memset above. */
7865 parameter->data_value = NULL;
7866 parameter->data_value_size = 0;
7867 call_site->parameter_count++;
7868
7869 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7870 if (attr)
7871 {
7872 if (!attr_form_is_block (attr))
7873 complaint (&symfile_complaints,
7874 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7875 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7876 child_die->offset.sect_off, objfile->name);
7877 else
7878 {
7879 parameter->data_value = DW_BLOCK (attr)->data;
7880 parameter->data_value_size = DW_BLOCK (attr)->size;
7881 }
7882 }
7883 }
7884 }
7885
7886 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7887 Return 1 if the attributes are present and valid, otherwise, return 0.
7888 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7889
7890 static int
7891 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7892 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7893 struct partial_symtab *ranges_pst)
7894 {
7895 struct objfile *objfile = cu->objfile;
7896 struct comp_unit_head *cu_header = &cu->header;
7897 bfd *obfd = objfile->obfd;
7898 unsigned int addr_size = cu_header->addr_size;
7899 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7900 /* Base address selection entry. */
7901 CORE_ADDR base;
7902 int found_base;
7903 unsigned int dummy;
7904 gdb_byte *buffer;
7905 CORE_ADDR marker;
7906 int low_set;
7907 CORE_ADDR low = 0;
7908 CORE_ADDR high = 0;
7909 CORE_ADDR baseaddr;
7910
7911 found_base = cu->base_known;
7912 base = cu->base_address;
7913
7914 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7915 if (offset >= dwarf2_per_objfile->ranges.size)
7916 {
7917 complaint (&symfile_complaints,
7918 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7919 offset);
7920 return 0;
7921 }
7922 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7923
7924 /* Read in the largest possible address. */
7925 marker = read_address (obfd, buffer, cu, &dummy);
7926 if ((marker & mask) == mask)
7927 {
7928 /* If we found the largest possible address, then
7929 read the base address. */
7930 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7931 buffer += 2 * addr_size;
7932 offset += 2 * addr_size;
7933 found_base = 1;
7934 }
7935
7936 low_set = 0;
7937
7938 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7939
7940 while (1)
7941 {
7942 CORE_ADDR range_beginning, range_end;
7943
7944 range_beginning = read_address (obfd, buffer, cu, &dummy);
7945 buffer += addr_size;
7946 range_end = read_address (obfd, buffer, cu, &dummy);
7947 buffer += addr_size;
7948 offset += 2 * addr_size;
7949
7950 /* An end of list marker is a pair of zero addresses. */
7951 if (range_beginning == 0 && range_end == 0)
7952 /* Found the end of list entry. */
7953 break;
7954
7955 /* Each base address selection entry is a pair of 2 values.
7956 The first is the largest possible address, the second is
7957 the base address. Check for a base address here. */
7958 if ((range_beginning & mask) == mask)
7959 {
7960 /* If we found the largest possible address, then
7961 read the base address. */
7962 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7963 found_base = 1;
7964 continue;
7965 }
7966
7967 if (!found_base)
7968 {
7969 /* We have no valid base address for the ranges
7970 data. */
7971 complaint (&symfile_complaints,
7972 _("Invalid .debug_ranges data (no base address)"));
7973 return 0;
7974 }
7975
7976 if (range_beginning > range_end)
7977 {
7978 /* Inverted range entries are invalid. */
7979 complaint (&symfile_complaints,
7980 _("Invalid .debug_ranges data (inverted range)"));
7981 return 0;
7982 }
7983
7984 /* Empty range entries have no effect. */
7985 if (range_beginning == range_end)
7986 continue;
7987
7988 range_beginning += base;
7989 range_end += base;
7990
7991 if (ranges_pst != NULL)
7992 addrmap_set_empty (objfile->psymtabs_addrmap,
7993 range_beginning + baseaddr,
7994 range_end - 1 + baseaddr,
7995 ranges_pst);
7996
7997 /* FIXME: This is recording everything as a low-high
7998 segment of consecutive addresses. We should have a
7999 data structure for discontiguous block ranges
8000 instead. */
8001 if (! low_set)
8002 {
8003 low = range_beginning;
8004 high = range_end;
8005 low_set = 1;
8006 }
8007 else
8008 {
8009 if (range_beginning < low)
8010 low = range_beginning;
8011 if (range_end > high)
8012 high = range_end;
8013 }
8014 }
8015
8016 if (! low_set)
8017 /* If the first entry is an end-of-list marker, the range
8018 describes an empty scope, i.e. no instructions. */
8019 return 0;
8020
8021 if (low_return)
8022 *low_return = low;
8023 if (high_return)
8024 *high_return = high;
8025 return 1;
8026 }
8027
8028 /* Get low and high pc attributes from a die. Return 1 if the attributes
8029 are present and valid, otherwise, return 0. Return -1 if the range is
8030 discontinuous, i.e. derived from DW_AT_ranges information. */
8031
8032 static int
8033 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
8034 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8035 struct partial_symtab *pst)
8036 {
8037 struct attribute *attr;
8038 struct attribute *attr_high;
8039 CORE_ADDR low = 0;
8040 CORE_ADDR high = 0;
8041 int ret = 0;
8042
8043 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8044 if (attr_high)
8045 {
8046 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8047 if (attr)
8048 {
8049 low = DW_ADDR (attr);
8050 if (attr_high->form == DW_FORM_addr
8051 || attr_high->form == DW_FORM_GNU_addr_index)
8052 high = DW_ADDR (attr_high);
8053 else
8054 high = low + DW_UNSND (attr_high);
8055 }
8056 else
8057 /* Found high w/o low attribute. */
8058 return 0;
8059
8060 /* Found consecutive range of addresses. */
8061 ret = 1;
8062 }
8063 else
8064 {
8065 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8066 if (attr != NULL)
8067 {
8068 /* Value of the DW_AT_ranges attribute is the offset in the
8069 .debug_ranges section. */
8070 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8071 return 0;
8072 /* Found discontinuous range of addresses. */
8073 ret = -1;
8074 }
8075 }
8076
8077 /* read_partial_die has also the strict LOW < HIGH requirement. */
8078 if (high <= low)
8079 return 0;
8080
8081 /* When using the GNU linker, .gnu.linkonce. sections are used to
8082 eliminate duplicate copies of functions and vtables and such.
8083 The linker will arbitrarily choose one and discard the others.
8084 The AT_*_pc values for such functions refer to local labels in
8085 these sections. If the section from that file was discarded, the
8086 labels are not in the output, so the relocs get a value of 0.
8087 If this is a discarded function, mark the pc bounds as invalid,
8088 so that GDB will ignore it. */
8089 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8090 return 0;
8091
8092 *lowpc = low;
8093 if (highpc)
8094 *highpc = high;
8095 return ret;
8096 }
8097
8098 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8099 its low and high PC addresses. Do nothing if these addresses could not
8100 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8101 and HIGHPC to the high address if greater than HIGHPC. */
8102
8103 static void
8104 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8105 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8106 struct dwarf2_cu *cu)
8107 {
8108 CORE_ADDR low, high;
8109 struct die_info *child = die->child;
8110
8111 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8112 {
8113 *lowpc = min (*lowpc, low);
8114 *highpc = max (*highpc, high);
8115 }
8116
8117 /* If the language does not allow nested subprograms (either inside
8118 subprograms or lexical blocks), we're done. */
8119 if (cu->language != language_ada)
8120 return;
8121
8122 /* Check all the children of the given DIE. If it contains nested
8123 subprograms, then check their pc bounds. Likewise, we need to
8124 check lexical blocks as well, as they may also contain subprogram
8125 definitions. */
8126 while (child && child->tag)
8127 {
8128 if (child->tag == DW_TAG_subprogram
8129 || child->tag == DW_TAG_lexical_block)
8130 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8131 child = sibling_die (child);
8132 }
8133 }
8134
8135 /* Get the low and high pc's represented by the scope DIE, and store
8136 them in *LOWPC and *HIGHPC. If the correct values can't be
8137 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8138
8139 static void
8140 get_scope_pc_bounds (struct die_info *die,
8141 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8142 struct dwarf2_cu *cu)
8143 {
8144 CORE_ADDR best_low = (CORE_ADDR) -1;
8145 CORE_ADDR best_high = (CORE_ADDR) 0;
8146 CORE_ADDR current_low, current_high;
8147
8148 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8149 {
8150 best_low = current_low;
8151 best_high = current_high;
8152 }
8153 else
8154 {
8155 struct die_info *child = die->child;
8156
8157 while (child && child->tag)
8158 {
8159 switch (child->tag) {
8160 case DW_TAG_subprogram:
8161 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8162 break;
8163 case DW_TAG_namespace:
8164 case DW_TAG_module:
8165 /* FIXME: carlton/2004-01-16: Should we do this for
8166 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8167 that current GCC's always emit the DIEs corresponding
8168 to definitions of methods of classes as children of a
8169 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8170 the DIEs giving the declarations, which could be
8171 anywhere). But I don't see any reason why the
8172 standards says that they have to be there. */
8173 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8174
8175 if (current_low != ((CORE_ADDR) -1))
8176 {
8177 best_low = min (best_low, current_low);
8178 best_high = max (best_high, current_high);
8179 }
8180 break;
8181 default:
8182 /* Ignore. */
8183 break;
8184 }
8185
8186 child = sibling_die (child);
8187 }
8188 }
8189
8190 *lowpc = best_low;
8191 *highpc = best_high;
8192 }
8193
8194 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8195 in DIE. */
8196
8197 static void
8198 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8199 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8200 {
8201 struct objfile *objfile = cu->objfile;
8202 struct attribute *attr;
8203 struct attribute *attr_high;
8204
8205 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8206 if (attr_high)
8207 {
8208 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8209 if (attr)
8210 {
8211 CORE_ADDR low = DW_ADDR (attr);
8212 CORE_ADDR high;
8213 if (attr_high->form == DW_FORM_addr
8214 || attr_high->form == DW_FORM_GNU_addr_index)
8215 high = DW_ADDR (attr_high);
8216 else
8217 high = low + DW_UNSND (attr_high);
8218
8219 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8220 }
8221 }
8222
8223 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8224 if (attr)
8225 {
8226 bfd *obfd = objfile->obfd;
8227
8228 /* The value of the DW_AT_ranges attribute is the offset of the
8229 address range list in the .debug_ranges section. */
8230 unsigned long offset = DW_UNSND (attr);
8231 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8232
8233 /* For some target architectures, but not others, the
8234 read_address function sign-extends the addresses it returns.
8235 To recognize base address selection entries, we need a
8236 mask. */
8237 unsigned int addr_size = cu->header.addr_size;
8238 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8239
8240 /* The base address, to which the next pair is relative. Note
8241 that this 'base' is a DWARF concept: most entries in a range
8242 list are relative, to reduce the number of relocs against the
8243 debugging information. This is separate from this function's
8244 'baseaddr' argument, which GDB uses to relocate debugging
8245 information from a shared library based on the address at
8246 which the library was loaded. */
8247 CORE_ADDR base = cu->base_address;
8248 int base_known = cu->base_known;
8249
8250 gdb_assert (dwarf2_per_objfile->ranges.readin);
8251 if (offset >= dwarf2_per_objfile->ranges.size)
8252 {
8253 complaint (&symfile_complaints,
8254 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8255 offset);
8256 return;
8257 }
8258
8259 for (;;)
8260 {
8261 unsigned int bytes_read;
8262 CORE_ADDR start, end;
8263
8264 start = read_address (obfd, buffer, cu, &bytes_read);
8265 buffer += bytes_read;
8266 end = read_address (obfd, buffer, cu, &bytes_read);
8267 buffer += bytes_read;
8268
8269 /* Did we find the end of the range list? */
8270 if (start == 0 && end == 0)
8271 break;
8272
8273 /* Did we find a base address selection entry? */
8274 else if ((start & base_select_mask) == base_select_mask)
8275 {
8276 base = end;
8277 base_known = 1;
8278 }
8279
8280 /* We found an ordinary address range. */
8281 else
8282 {
8283 if (!base_known)
8284 {
8285 complaint (&symfile_complaints,
8286 _("Invalid .debug_ranges data "
8287 "(no base address)"));
8288 return;
8289 }
8290
8291 if (start > end)
8292 {
8293 /* Inverted range entries are invalid. */
8294 complaint (&symfile_complaints,
8295 _("Invalid .debug_ranges data "
8296 "(inverted range)"));
8297 return;
8298 }
8299
8300 /* Empty range entries have no effect. */
8301 if (start == end)
8302 continue;
8303
8304 record_block_range (block,
8305 baseaddr + base + start,
8306 baseaddr + base + end - 1);
8307 }
8308 }
8309 }
8310 }
8311
8312 /* Check whether the producer field indicates either of GCC < 4.6, or the
8313 Intel C/C++ compiler, and cache the result in CU. */
8314
8315 static void
8316 check_producer (struct dwarf2_cu *cu)
8317 {
8318 const char *cs;
8319 int major, minor, release;
8320
8321 if (cu->producer == NULL)
8322 {
8323 /* For unknown compilers expect their behavior is DWARF version
8324 compliant.
8325
8326 GCC started to support .debug_types sections by -gdwarf-4 since
8327 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8328 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8329 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8330 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8331 }
8332 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
8333 {
8334 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8335
8336 cs = &cu->producer[strlen ("GNU ")];
8337 while (*cs && !isdigit (*cs))
8338 cs++;
8339 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8340 {
8341 /* Not recognized as GCC. */
8342 }
8343 else
8344 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8345 }
8346 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8347 cu->producer_is_icc = 1;
8348 else
8349 {
8350 /* For other non-GCC compilers, expect their behavior is DWARF version
8351 compliant. */
8352 }
8353
8354 cu->checked_producer = 1;
8355 }
8356
8357 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8358 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8359 during 4.6.0 experimental. */
8360
8361 static int
8362 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8363 {
8364 if (!cu->checked_producer)
8365 check_producer (cu);
8366
8367 return cu->producer_is_gxx_lt_4_6;
8368 }
8369
8370 /* Return the default accessibility type if it is not overriden by
8371 DW_AT_accessibility. */
8372
8373 static enum dwarf_access_attribute
8374 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8375 {
8376 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8377 {
8378 /* The default DWARF 2 accessibility for members is public, the default
8379 accessibility for inheritance is private. */
8380
8381 if (die->tag != DW_TAG_inheritance)
8382 return DW_ACCESS_public;
8383 else
8384 return DW_ACCESS_private;
8385 }
8386 else
8387 {
8388 /* DWARF 3+ defines the default accessibility a different way. The same
8389 rules apply now for DW_TAG_inheritance as for the members and it only
8390 depends on the container kind. */
8391
8392 if (die->parent->tag == DW_TAG_class_type)
8393 return DW_ACCESS_private;
8394 else
8395 return DW_ACCESS_public;
8396 }
8397 }
8398
8399 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8400 offset. If the attribute was not found return 0, otherwise return
8401 1. If it was found but could not properly be handled, set *OFFSET
8402 to 0. */
8403
8404 static int
8405 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8406 LONGEST *offset)
8407 {
8408 struct attribute *attr;
8409
8410 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8411 if (attr != NULL)
8412 {
8413 *offset = 0;
8414
8415 /* Note that we do not check for a section offset first here.
8416 This is because DW_AT_data_member_location is new in DWARF 4,
8417 so if we see it, we can assume that a constant form is really
8418 a constant and not a section offset. */
8419 if (attr_form_is_constant (attr))
8420 *offset = dwarf2_get_attr_constant_value (attr, 0);
8421 else if (attr_form_is_section_offset (attr))
8422 dwarf2_complex_location_expr_complaint ();
8423 else if (attr_form_is_block (attr))
8424 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8425 else
8426 dwarf2_complex_location_expr_complaint ();
8427
8428 return 1;
8429 }
8430
8431 return 0;
8432 }
8433
8434 /* Add an aggregate field to the field list. */
8435
8436 static void
8437 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8438 struct dwarf2_cu *cu)
8439 {
8440 struct objfile *objfile = cu->objfile;
8441 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8442 struct nextfield *new_field;
8443 struct attribute *attr;
8444 struct field *fp;
8445 char *fieldname = "";
8446
8447 /* Allocate a new field list entry and link it in. */
8448 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8449 make_cleanup (xfree, new_field);
8450 memset (new_field, 0, sizeof (struct nextfield));
8451
8452 if (die->tag == DW_TAG_inheritance)
8453 {
8454 new_field->next = fip->baseclasses;
8455 fip->baseclasses = new_field;
8456 }
8457 else
8458 {
8459 new_field->next = fip->fields;
8460 fip->fields = new_field;
8461 }
8462 fip->nfields++;
8463
8464 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8465 if (attr)
8466 new_field->accessibility = DW_UNSND (attr);
8467 else
8468 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8469 if (new_field->accessibility != DW_ACCESS_public)
8470 fip->non_public_fields = 1;
8471
8472 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8473 if (attr)
8474 new_field->virtuality = DW_UNSND (attr);
8475 else
8476 new_field->virtuality = DW_VIRTUALITY_none;
8477
8478 fp = &new_field->field;
8479
8480 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8481 {
8482 LONGEST offset;
8483
8484 /* Data member other than a C++ static data member. */
8485
8486 /* Get type of field. */
8487 fp->type = die_type (die, cu);
8488
8489 SET_FIELD_BITPOS (*fp, 0);
8490
8491 /* Get bit size of field (zero if none). */
8492 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8493 if (attr)
8494 {
8495 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8496 }
8497 else
8498 {
8499 FIELD_BITSIZE (*fp) = 0;
8500 }
8501
8502 /* Get bit offset of field. */
8503 if (handle_data_member_location (die, cu, &offset))
8504 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8505 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8506 if (attr)
8507 {
8508 if (gdbarch_bits_big_endian (gdbarch))
8509 {
8510 /* For big endian bits, the DW_AT_bit_offset gives the
8511 additional bit offset from the MSB of the containing
8512 anonymous object to the MSB of the field. We don't
8513 have to do anything special since we don't need to
8514 know the size of the anonymous object. */
8515 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8516 }
8517 else
8518 {
8519 /* For little endian bits, compute the bit offset to the
8520 MSB of the anonymous object, subtract off the number of
8521 bits from the MSB of the field to the MSB of the
8522 object, and then subtract off the number of bits of
8523 the field itself. The result is the bit offset of
8524 the LSB of the field. */
8525 int anonymous_size;
8526 int bit_offset = DW_UNSND (attr);
8527
8528 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8529 if (attr)
8530 {
8531 /* The size of the anonymous object containing
8532 the bit field is explicit, so use the
8533 indicated size (in bytes). */
8534 anonymous_size = DW_UNSND (attr);
8535 }
8536 else
8537 {
8538 /* The size of the anonymous object containing
8539 the bit field must be inferred from the type
8540 attribute of the data member containing the
8541 bit field. */
8542 anonymous_size = TYPE_LENGTH (fp->type);
8543 }
8544 SET_FIELD_BITPOS (*fp,
8545 (FIELD_BITPOS (*fp)
8546 + anonymous_size * bits_per_byte
8547 - bit_offset - FIELD_BITSIZE (*fp)));
8548 }
8549 }
8550
8551 /* Get name of field. */
8552 fieldname = dwarf2_name (die, cu);
8553 if (fieldname == NULL)
8554 fieldname = "";
8555
8556 /* The name is already allocated along with this objfile, so we don't
8557 need to duplicate it for the type. */
8558 fp->name = fieldname;
8559
8560 /* Change accessibility for artificial fields (e.g. virtual table
8561 pointer or virtual base class pointer) to private. */
8562 if (dwarf2_attr (die, DW_AT_artificial, cu))
8563 {
8564 FIELD_ARTIFICIAL (*fp) = 1;
8565 new_field->accessibility = DW_ACCESS_private;
8566 fip->non_public_fields = 1;
8567 }
8568 }
8569 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8570 {
8571 /* C++ static member. */
8572
8573 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8574 is a declaration, but all versions of G++ as of this writing
8575 (so through at least 3.2.1) incorrectly generate
8576 DW_TAG_variable tags. */
8577
8578 const char *physname;
8579
8580 /* Get name of field. */
8581 fieldname = dwarf2_name (die, cu);
8582 if (fieldname == NULL)
8583 return;
8584
8585 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8586 if (attr
8587 /* Only create a symbol if this is an external value.
8588 new_symbol checks this and puts the value in the global symbol
8589 table, which we want. If it is not external, new_symbol
8590 will try to put the value in cu->list_in_scope which is wrong. */
8591 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8592 {
8593 /* A static const member, not much different than an enum as far as
8594 we're concerned, except that we can support more types. */
8595 new_symbol (die, NULL, cu);
8596 }
8597
8598 /* Get physical name. */
8599 physname = dwarf2_physname (fieldname, die, cu);
8600
8601 /* The name is already allocated along with this objfile, so we don't
8602 need to duplicate it for the type. */
8603 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8604 FIELD_TYPE (*fp) = die_type (die, cu);
8605 FIELD_NAME (*fp) = fieldname;
8606 }
8607 else if (die->tag == DW_TAG_inheritance)
8608 {
8609 LONGEST offset;
8610
8611 /* C++ base class field. */
8612 if (handle_data_member_location (die, cu, &offset))
8613 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8614 FIELD_BITSIZE (*fp) = 0;
8615 FIELD_TYPE (*fp) = die_type (die, cu);
8616 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8617 fip->nbaseclasses++;
8618 }
8619 }
8620
8621 /* Add a typedef defined in the scope of the FIP's class. */
8622
8623 static void
8624 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8625 struct dwarf2_cu *cu)
8626 {
8627 struct objfile *objfile = cu->objfile;
8628 struct typedef_field_list *new_field;
8629 struct attribute *attr;
8630 struct typedef_field *fp;
8631 char *fieldname = "";
8632
8633 /* Allocate a new field list entry and link it in. */
8634 new_field = xzalloc (sizeof (*new_field));
8635 make_cleanup (xfree, new_field);
8636
8637 gdb_assert (die->tag == DW_TAG_typedef);
8638
8639 fp = &new_field->field;
8640
8641 /* Get name of field. */
8642 fp->name = dwarf2_name (die, cu);
8643 if (fp->name == NULL)
8644 return;
8645
8646 fp->type = read_type_die (die, cu);
8647
8648 new_field->next = fip->typedef_field_list;
8649 fip->typedef_field_list = new_field;
8650 fip->typedef_field_list_count++;
8651 }
8652
8653 /* Create the vector of fields, and attach it to the type. */
8654
8655 static void
8656 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8657 struct dwarf2_cu *cu)
8658 {
8659 int nfields = fip->nfields;
8660
8661 /* Record the field count, allocate space for the array of fields,
8662 and create blank accessibility bitfields if necessary. */
8663 TYPE_NFIELDS (type) = nfields;
8664 TYPE_FIELDS (type) = (struct field *)
8665 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8666 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8667
8668 if (fip->non_public_fields && cu->language != language_ada)
8669 {
8670 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8671
8672 TYPE_FIELD_PRIVATE_BITS (type) =
8673 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8674 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8675
8676 TYPE_FIELD_PROTECTED_BITS (type) =
8677 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8678 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8679
8680 TYPE_FIELD_IGNORE_BITS (type) =
8681 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8682 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8683 }
8684
8685 /* If the type has baseclasses, allocate and clear a bit vector for
8686 TYPE_FIELD_VIRTUAL_BITS. */
8687 if (fip->nbaseclasses && cu->language != language_ada)
8688 {
8689 int num_bytes = B_BYTES (fip->nbaseclasses);
8690 unsigned char *pointer;
8691
8692 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8693 pointer = TYPE_ALLOC (type, num_bytes);
8694 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8695 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8696 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8697 }
8698
8699 /* Copy the saved-up fields into the field vector. Start from the head of
8700 the list, adding to the tail of the field array, so that they end up in
8701 the same order in the array in which they were added to the list. */
8702 while (nfields-- > 0)
8703 {
8704 struct nextfield *fieldp;
8705
8706 if (fip->fields)
8707 {
8708 fieldp = fip->fields;
8709 fip->fields = fieldp->next;
8710 }
8711 else
8712 {
8713 fieldp = fip->baseclasses;
8714 fip->baseclasses = fieldp->next;
8715 }
8716
8717 TYPE_FIELD (type, nfields) = fieldp->field;
8718 switch (fieldp->accessibility)
8719 {
8720 case DW_ACCESS_private:
8721 if (cu->language != language_ada)
8722 SET_TYPE_FIELD_PRIVATE (type, nfields);
8723 break;
8724
8725 case DW_ACCESS_protected:
8726 if (cu->language != language_ada)
8727 SET_TYPE_FIELD_PROTECTED (type, nfields);
8728 break;
8729
8730 case DW_ACCESS_public:
8731 break;
8732
8733 default:
8734 /* Unknown accessibility. Complain and treat it as public. */
8735 {
8736 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8737 fieldp->accessibility);
8738 }
8739 break;
8740 }
8741 if (nfields < fip->nbaseclasses)
8742 {
8743 switch (fieldp->virtuality)
8744 {
8745 case DW_VIRTUALITY_virtual:
8746 case DW_VIRTUALITY_pure_virtual:
8747 if (cu->language == language_ada)
8748 error (_("unexpected virtuality in component of Ada type"));
8749 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8750 break;
8751 }
8752 }
8753 }
8754 }
8755
8756 /* Add a member function to the proper fieldlist. */
8757
8758 static void
8759 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8760 struct type *type, struct dwarf2_cu *cu)
8761 {
8762 struct objfile *objfile = cu->objfile;
8763 struct attribute *attr;
8764 struct fnfieldlist *flp;
8765 int i;
8766 struct fn_field *fnp;
8767 char *fieldname;
8768 struct nextfnfield *new_fnfield;
8769 struct type *this_type;
8770 enum dwarf_access_attribute accessibility;
8771
8772 if (cu->language == language_ada)
8773 error (_("unexpected member function in Ada type"));
8774
8775 /* Get name of member function. */
8776 fieldname = dwarf2_name (die, cu);
8777 if (fieldname == NULL)
8778 return;
8779
8780 /* Look up member function name in fieldlist. */
8781 for (i = 0; i < fip->nfnfields; i++)
8782 {
8783 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8784 break;
8785 }
8786
8787 /* Create new list element if necessary. */
8788 if (i < fip->nfnfields)
8789 flp = &fip->fnfieldlists[i];
8790 else
8791 {
8792 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8793 {
8794 fip->fnfieldlists = (struct fnfieldlist *)
8795 xrealloc (fip->fnfieldlists,
8796 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8797 * sizeof (struct fnfieldlist));
8798 if (fip->nfnfields == 0)
8799 make_cleanup (free_current_contents, &fip->fnfieldlists);
8800 }
8801 flp = &fip->fnfieldlists[fip->nfnfields];
8802 flp->name = fieldname;
8803 flp->length = 0;
8804 flp->head = NULL;
8805 i = fip->nfnfields++;
8806 }
8807
8808 /* Create a new member function field and chain it to the field list
8809 entry. */
8810 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8811 make_cleanup (xfree, new_fnfield);
8812 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8813 new_fnfield->next = flp->head;
8814 flp->head = new_fnfield;
8815 flp->length++;
8816
8817 /* Fill in the member function field info. */
8818 fnp = &new_fnfield->fnfield;
8819
8820 /* Delay processing of the physname until later. */
8821 if (cu->language == language_cplus || cu->language == language_java)
8822 {
8823 add_to_method_list (type, i, flp->length - 1, fieldname,
8824 die, cu);
8825 }
8826 else
8827 {
8828 const char *physname = dwarf2_physname (fieldname, die, cu);
8829 fnp->physname = physname ? physname : "";
8830 }
8831
8832 fnp->type = alloc_type (objfile);
8833 this_type = read_type_die (die, cu);
8834 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8835 {
8836 int nparams = TYPE_NFIELDS (this_type);
8837
8838 /* TYPE is the domain of this method, and THIS_TYPE is the type
8839 of the method itself (TYPE_CODE_METHOD). */
8840 smash_to_method_type (fnp->type, type,
8841 TYPE_TARGET_TYPE (this_type),
8842 TYPE_FIELDS (this_type),
8843 TYPE_NFIELDS (this_type),
8844 TYPE_VARARGS (this_type));
8845
8846 /* Handle static member functions.
8847 Dwarf2 has no clean way to discern C++ static and non-static
8848 member functions. G++ helps GDB by marking the first
8849 parameter for non-static member functions (which is the this
8850 pointer) as artificial. We obtain this information from
8851 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8852 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8853 fnp->voffset = VOFFSET_STATIC;
8854 }
8855 else
8856 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8857 dwarf2_full_name (fieldname, die, cu));
8858
8859 /* Get fcontext from DW_AT_containing_type if present. */
8860 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8861 fnp->fcontext = die_containing_type (die, cu);
8862
8863 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8864 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8865
8866 /* Get accessibility. */
8867 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8868 if (attr)
8869 accessibility = DW_UNSND (attr);
8870 else
8871 accessibility = dwarf2_default_access_attribute (die, cu);
8872 switch (accessibility)
8873 {
8874 case DW_ACCESS_private:
8875 fnp->is_private = 1;
8876 break;
8877 case DW_ACCESS_protected:
8878 fnp->is_protected = 1;
8879 break;
8880 }
8881
8882 /* Check for artificial methods. */
8883 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8884 if (attr && DW_UNSND (attr) != 0)
8885 fnp->is_artificial = 1;
8886
8887 /* Get index in virtual function table if it is a virtual member
8888 function. For older versions of GCC, this is an offset in the
8889 appropriate virtual table, as specified by DW_AT_containing_type.
8890 For everyone else, it is an expression to be evaluated relative
8891 to the object address. */
8892
8893 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8894 if (attr)
8895 {
8896 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8897 {
8898 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8899 {
8900 /* Old-style GCC. */
8901 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8902 }
8903 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8904 || (DW_BLOCK (attr)->size > 1
8905 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8906 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8907 {
8908 struct dwarf_block blk;
8909 int offset;
8910
8911 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8912 ? 1 : 2);
8913 blk.size = DW_BLOCK (attr)->size - offset;
8914 blk.data = DW_BLOCK (attr)->data + offset;
8915 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8916 if ((fnp->voffset % cu->header.addr_size) != 0)
8917 dwarf2_complex_location_expr_complaint ();
8918 else
8919 fnp->voffset /= cu->header.addr_size;
8920 fnp->voffset += 2;
8921 }
8922 else
8923 dwarf2_complex_location_expr_complaint ();
8924
8925 if (!fnp->fcontext)
8926 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8927 }
8928 else if (attr_form_is_section_offset (attr))
8929 {
8930 dwarf2_complex_location_expr_complaint ();
8931 }
8932 else
8933 {
8934 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8935 fieldname);
8936 }
8937 }
8938 else
8939 {
8940 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8941 if (attr && DW_UNSND (attr))
8942 {
8943 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8944 complaint (&symfile_complaints,
8945 _("Member function \"%s\" (offset %d) is virtual "
8946 "but the vtable offset is not specified"),
8947 fieldname, die->offset.sect_off);
8948 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8949 TYPE_CPLUS_DYNAMIC (type) = 1;
8950 }
8951 }
8952 }
8953
8954 /* Create the vector of member function fields, and attach it to the type. */
8955
8956 static void
8957 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8958 struct dwarf2_cu *cu)
8959 {
8960 struct fnfieldlist *flp;
8961 int i;
8962
8963 if (cu->language == language_ada)
8964 error (_("unexpected member functions in Ada type"));
8965
8966 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8967 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8968 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8969
8970 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8971 {
8972 struct nextfnfield *nfp = flp->head;
8973 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8974 int k;
8975
8976 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8977 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8978 fn_flp->fn_fields = (struct fn_field *)
8979 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8980 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8981 fn_flp->fn_fields[k] = nfp->fnfield;
8982 }
8983
8984 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8985 }
8986
8987 /* Returns non-zero if NAME is the name of a vtable member in CU's
8988 language, zero otherwise. */
8989 static int
8990 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8991 {
8992 static const char vptr[] = "_vptr";
8993 static const char vtable[] = "vtable";
8994
8995 /* Look for the C++ and Java forms of the vtable. */
8996 if ((cu->language == language_java
8997 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8998 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8999 && is_cplus_marker (name[sizeof (vptr) - 1])))
9000 return 1;
9001
9002 return 0;
9003 }
9004
9005 /* GCC outputs unnamed structures that are really pointers to member
9006 functions, with the ABI-specified layout. If TYPE describes
9007 such a structure, smash it into a member function type.
9008
9009 GCC shouldn't do this; it should just output pointer to member DIEs.
9010 This is GCC PR debug/28767. */
9011
9012 static void
9013 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
9014 {
9015 struct type *pfn_type, *domain_type, *new_type;
9016
9017 /* Check for a structure with no name and two children. */
9018 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9019 return;
9020
9021 /* Check for __pfn and __delta members. */
9022 if (TYPE_FIELD_NAME (type, 0) == NULL
9023 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9024 || TYPE_FIELD_NAME (type, 1) == NULL
9025 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9026 return;
9027
9028 /* Find the type of the method. */
9029 pfn_type = TYPE_FIELD_TYPE (type, 0);
9030 if (pfn_type == NULL
9031 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9032 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
9033 return;
9034
9035 /* Look for the "this" argument. */
9036 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9037 if (TYPE_NFIELDS (pfn_type) == 0
9038 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
9039 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
9040 return;
9041
9042 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9043 new_type = alloc_type (objfile);
9044 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9045 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9046 TYPE_VARARGS (pfn_type));
9047 smash_to_methodptr_type (type, new_type);
9048 }
9049
9050 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9051 (icc). */
9052
9053 static int
9054 producer_is_icc (struct dwarf2_cu *cu)
9055 {
9056 if (!cu->checked_producer)
9057 check_producer (cu);
9058
9059 return cu->producer_is_icc;
9060 }
9061
9062 /* Called when we find the DIE that starts a structure or union scope
9063 (definition) to create a type for the structure or union. Fill in
9064 the type's name and general properties; the members will not be
9065 processed until process_structure_type.
9066
9067 NOTE: we need to call these functions regardless of whether or not the
9068 DIE has a DW_AT_name attribute, since it might be an anonymous
9069 structure or union. This gets the type entered into our set of
9070 user defined types.
9071
9072 However, if the structure is incomplete (an opaque struct/union)
9073 then suppress creating a symbol table entry for it since gdb only
9074 wants to find the one with the complete definition. Note that if
9075 it is complete, we just call new_symbol, which does it's own
9076 checking about whether the struct/union is anonymous or not (and
9077 suppresses creating a symbol table entry itself). */
9078
9079 static struct type *
9080 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9081 {
9082 struct objfile *objfile = cu->objfile;
9083 struct type *type;
9084 struct attribute *attr;
9085 char *name;
9086
9087 /* If the definition of this type lives in .debug_types, read that type.
9088 Don't follow DW_AT_specification though, that will take us back up
9089 the chain and we want to go down. */
9090 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9091 if (attr)
9092 {
9093 struct dwarf2_cu *type_cu = cu;
9094 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9095
9096 /* We could just recurse on read_structure_type, but we need to call
9097 get_die_type to ensure only one type for this DIE is created.
9098 This is important, for example, because for c++ classes we need
9099 TYPE_NAME set which is only done by new_symbol. Blech. */
9100 type = read_type_die (type_die, type_cu);
9101
9102 /* TYPE_CU may not be the same as CU.
9103 Ensure TYPE is recorded in CU's type_hash table. */
9104 return set_die_type (die, type, cu);
9105 }
9106
9107 type = alloc_type (objfile);
9108 INIT_CPLUS_SPECIFIC (type);
9109
9110 name = dwarf2_name (die, cu);
9111 if (name != NULL)
9112 {
9113 if (cu->language == language_cplus
9114 || cu->language == language_java)
9115 {
9116 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9117
9118 /* dwarf2_full_name might have already finished building the DIE's
9119 type. If so, there is no need to continue. */
9120 if (get_die_type (die, cu) != NULL)
9121 return get_die_type (die, cu);
9122
9123 TYPE_TAG_NAME (type) = full_name;
9124 if (die->tag == DW_TAG_structure_type
9125 || die->tag == DW_TAG_class_type)
9126 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9127 }
9128 else
9129 {
9130 /* The name is already allocated along with this objfile, so
9131 we don't need to duplicate it for the type. */
9132 TYPE_TAG_NAME (type) = (char *) name;
9133 if (die->tag == DW_TAG_class_type)
9134 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9135 }
9136 }
9137
9138 if (die->tag == DW_TAG_structure_type)
9139 {
9140 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9141 }
9142 else if (die->tag == DW_TAG_union_type)
9143 {
9144 TYPE_CODE (type) = TYPE_CODE_UNION;
9145 }
9146 else
9147 {
9148 TYPE_CODE (type) = TYPE_CODE_CLASS;
9149 }
9150
9151 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9152 TYPE_DECLARED_CLASS (type) = 1;
9153
9154 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9155 if (attr)
9156 {
9157 TYPE_LENGTH (type) = DW_UNSND (attr);
9158 }
9159 else
9160 {
9161 TYPE_LENGTH (type) = 0;
9162 }
9163
9164 if (producer_is_icc (cu))
9165 {
9166 /* ICC does not output the required DW_AT_declaration
9167 on incomplete types, but gives them a size of zero. */
9168 }
9169 else
9170 TYPE_STUB_SUPPORTED (type) = 1;
9171
9172 if (die_is_declaration (die, cu))
9173 TYPE_STUB (type) = 1;
9174 else if (attr == NULL && die->child == NULL
9175 && producer_is_realview (cu->producer))
9176 /* RealView does not output the required DW_AT_declaration
9177 on incomplete types. */
9178 TYPE_STUB (type) = 1;
9179
9180 /* We need to add the type field to the die immediately so we don't
9181 infinitely recurse when dealing with pointers to the structure
9182 type within the structure itself. */
9183 set_die_type (die, type, cu);
9184
9185 /* set_die_type should be already done. */
9186 set_descriptive_type (type, die, cu);
9187
9188 return type;
9189 }
9190
9191 /* Finish creating a structure or union type, including filling in
9192 its members and creating a symbol for it. */
9193
9194 static void
9195 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9196 {
9197 struct objfile *objfile = cu->objfile;
9198 struct die_info *child_die = die->child;
9199 struct type *type;
9200
9201 type = get_die_type (die, cu);
9202 if (type == NULL)
9203 type = read_structure_type (die, cu);
9204
9205 if (die->child != NULL && ! die_is_declaration (die, cu))
9206 {
9207 struct field_info fi;
9208 struct die_info *child_die;
9209 VEC (symbolp) *template_args = NULL;
9210 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9211
9212 memset (&fi, 0, sizeof (struct field_info));
9213
9214 child_die = die->child;
9215
9216 while (child_die && child_die->tag)
9217 {
9218 if (child_die->tag == DW_TAG_member
9219 || child_die->tag == DW_TAG_variable)
9220 {
9221 /* NOTE: carlton/2002-11-05: A C++ static data member
9222 should be a DW_TAG_member that is a declaration, but
9223 all versions of G++ as of this writing (so through at
9224 least 3.2.1) incorrectly generate DW_TAG_variable
9225 tags for them instead. */
9226 dwarf2_add_field (&fi, child_die, cu);
9227 }
9228 else if (child_die->tag == DW_TAG_subprogram)
9229 {
9230 /* C++ member function. */
9231 dwarf2_add_member_fn (&fi, child_die, type, cu);
9232 }
9233 else if (child_die->tag == DW_TAG_inheritance)
9234 {
9235 /* C++ base class field. */
9236 dwarf2_add_field (&fi, child_die, cu);
9237 }
9238 else if (child_die->tag == DW_TAG_typedef)
9239 dwarf2_add_typedef (&fi, child_die, cu);
9240 else if (child_die->tag == DW_TAG_template_type_param
9241 || child_die->tag == DW_TAG_template_value_param)
9242 {
9243 struct symbol *arg = new_symbol (child_die, NULL, cu);
9244
9245 if (arg != NULL)
9246 VEC_safe_push (symbolp, template_args, arg);
9247 }
9248
9249 child_die = sibling_die (child_die);
9250 }
9251
9252 /* Attach template arguments to type. */
9253 if (! VEC_empty (symbolp, template_args))
9254 {
9255 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9256 TYPE_N_TEMPLATE_ARGUMENTS (type)
9257 = VEC_length (symbolp, template_args);
9258 TYPE_TEMPLATE_ARGUMENTS (type)
9259 = obstack_alloc (&objfile->objfile_obstack,
9260 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9261 * sizeof (struct symbol *)));
9262 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9263 VEC_address (symbolp, template_args),
9264 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9265 * sizeof (struct symbol *)));
9266 VEC_free (symbolp, template_args);
9267 }
9268
9269 /* Attach fields and member functions to the type. */
9270 if (fi.nfields)
9271 dwarf2_attach_fields_to_type (&fi, type, cu);
9272 if (fi.nfnfields)
9273 {
9274 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9275
9276 /* Get the type which refers to the base class (possibly this
9277 class itself) which contains the vtable pointer for the current
9278 class from the DW_AT_containing_type attribute. This use of
9279 DW_AT_containing_type is a GNU extension. */
9280
9281 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9282 {
9283 struct type *t = die_containing_type (die, cu);
9284
9285 TYPE_VPTR_BASETYPE (type) = t;
9286 if (type == t)
9287 {
9288 int i;
9289
9290 /* Our own class provides vtbl ptr. */
9291 for (i = TYPE_NFIELDS (t) - 1;
9292 i >= TYPE_N_BASECLASSES (t);
9293 --i)
9294 {
9295 const char *fieldname = TYPE_FIELD_NAME (t, i);
9296
9297 if (is_vtable_name (fieldname, cu))
9298 {
9299 TYPE_VPTR_FIELDNO (type) = i;
9300 break;
9301 }
9302 }
9303
9304 /* Complain if virtual function table field not found. */
9305 if (i < TYPE_N_BASECLASSES (t))
9306 complaint (&symfile_complaints,
9307 _("virtual function table pointer "
9308 "not found when defining class '%s'"),
9309 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9310 "");
9311 }
9312 else
9313 {
9314 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9315 }
9316 }
9317 else if (cu->producer
9318 && strncmp (cu->producer,
9319 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9320 {
9321 /* The IBM XLC compiler does not provide direct indication
9322 of the containing type, but the vtable pointer is
9323 always named __vfp. */
9324
9325 int i;
9326
9327 for (i = TYPE_NFIELDS (type) - 1;
9328 i >= TYPE_N_BASECLASSES (type);
9329 --i)
9330 {
9331 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9332 {
9333 TYPE_VPTR_FIELDNO (type) = i;
9334 TYPE_VPTR_BASETYPE (type) = type;
9335 break;
9336 }
9337 }
9338 }
9339 }
9340
9341 /* Copy fi.typedef_field_list linked list elements content into the
9342 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9343 if (fi.typedef_field_list)
9344 {
9345 int i = fi.typedef_field_list_count;
9346
9347 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9348 TYPE_TYPEDEF_FIELD_ARRAY (type)
9349 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9350 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9351
9352 /* Reverse the list order to keep the debug info elements order. */
9353 while (--i >= 0)
9354 {
9355 struct typedef_field *dest, *src;
9356
9357 dest = &TYPE_TYPEDEF_FIELD (type, i);
9358 src = &fi.typedef_field_list->field;
9359 fi.typedef_field_list = fi.typedef_field_list->next;
9360 *dest = *src;
9361 }
9362 }
9363
9364 do_cleanups (back_to);
9365
9366 if (HAVE_CPLUS_STRUCT (type))
9367 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9368 }
9369
9370 quirk_gcc_member_function_pointer (type, objfile);
9371
9372 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9373 snapshots) has been known to create a die giving a declaration
9374 for a class that has, as a child, a die giving a definition for a
9375 nested class. So we have to process our children even if the
9376 current die is a declaration. Normally, of course, a declaration
9377 won't have any children at all. */
9378
9379 while (child_die != NULL && child_die->tag)
9380 {
9381 if (child_die->tag == DW_TAG_member
9382 || child_die->tag == DW_TAG_variable
9383 || child_die->tag == DW_TAG_inheritance
9384 || child_die->tag == DW_TAG_template_value_param
9385 || child_die->tag == DW_TAG_template_type_param)
9386 {
9387 /* Do nothing. */
9388 }
9389 else
9390 process_die (child_die, cu);
9391
9392 child_die = sibling_die (child_die);
9393 }
9394
9395 /* Do not consider external references. According to the DWARF standard,
9396 these DIEs are identified by the fact that they have no byte_size
9397 attribute, and a declaration attribute. */
9398 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9399 || !die_is_declaration (die, cu))
9400 new_symbol (die, type, cu);
9401 }
9402
9403 /* Given a DW_AT_enumeration_type die, set its type. We do not
9404 complete the type's fields yet, or create any symbols. */
9405
9406 static struct type *
9407 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9408 {
9409 struct objfile *objfile = cu->objfile;
9410 struct type *type;
9411 struct attribute *attr;
9412 const char *name;
9413
9414 /* If the definition of this type lives in .debug_types, read that type.
9415 Don't follow DW_AT_specification though, that will take us back up
9416 the chain and we want to go down. */
9417 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9418 if (attr)
9419 {
9420 struct dwarf2_cu *type_cu = cu;
9421 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9422
9423 type = read_type_die (type_die, type_cu);
9424
9425 /* TYPE_CU may not be the same as CU.
9426 Ensure TYPE is recorded in CU's type_hash table. */
9427 return set_die_type (die, type, cu);
9428 }
9429
9430 type = alloc_type (objfile);
9431
9432 TYPE_CODE (type) = TYPE_CODE_ENUM;
9433 name = dwarf2_full_name (NULL, die, cu);
9434 if (name != NULL)
9435 TYPE_TAG_NAME (type) = (char *) name;
9436
9437 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9438 if (attr)
9439 {
9440 TYPE_LENGTH (type) = DW_UNSND (attr);
9441 }
9442 else
9443 {
9444 TYPE_LENGTH (type) = 0;
9445 }
9446
9447 /* The enumeration DIE can be incomplete. In Ada, any type can be
9448 declared as private in the package spec, and then defined only
9449 inside the package body. Such types are known as Taft Amendment
9450 Types. When another package uses such a type, an incomplete DIE
9451 may be generated by the compiler. */
9452 if (die_is_declaration (die, cu))
9453 TYPE_STUB (type) = 1;
9454
9455 return set_die_type (die, type, cu);
9456 }
9457
9458 /* Given a pointer to a die which begins an enumeration, process all
9459 the dies that define the members of the enumeration, and create the
9460 symbol for the enumeration type.
9461
9462 NOTE: We reverse the order of the element list. */
9463
9464 static void
9465 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9466 {
9467 struct type *this_type;
9468
9469 this_type = get_die_type (die, cu);
9470 if (this_type == NULL)
9471 this_type = read_enumeration_type (die, cu);
9472
9473 if (die->child != NULL)
9474 {
9475 struct die_info *child_die;
9476 struct symbol *sym;
9477 struct field *fields = NULL;
9478 int num_fields = 0;
9479 int unsigned_enum = 1;
9480 char *name;
9481 int flag_enum = 1;
9482 ULONGEST mask = 0;
9483
9484 child_die = die->child;
9485 while (child_die && child_die->tag)
9486 {
9487 if (child_die->tag != DW_TAG_enumerator)
9488 {
9489 process_die (child_die, cu);
9490 }
9491 else
9492 {
9493 name = dwarf2_name (child_die, cu);
9494 if (name)
9495 {
9496 sym = new_symbol (child_die, this_type, cu);
9497 if (SYMBOL_VALUE (sym) < 0)
9498 {
9499 unsigned_enum = 0;
9500 flag_enum = 0;
9501 }
9502 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9503 flag_enum = 0;
9504 else
9505 mask |= SYMBOL_VALUE (sym);
9506
9507 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9508 {
9509 fields = (struct field *)
9510 xrealloc (fields,
9511 (num_fields + DW_FIELD_ALLOC_CHUNK)
9512 * sizeof (struct field));
9513 }
9514
9515 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9516 FIELD_TYPE (fields[num_fields]) = NULL;
9517 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9518 FIELD_BITSIZE (fields[num_fields]) = 0;
9519
9520 num_fields++;
9521 }
9522 }
9523
9524 child_die = sibling_die (child_die);
9525 }
9526
9527 if (num_fields)
9528 {
9529 TYPE_NFIELDS (this_type) = num_fields;
9530 TYPE_FIELDS (this_type) = (struct field *)
9531 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9532 memcpy (TYPE_FIELDS (this_type), fields,
9533 sizeof (struct field) * num_fields);
9534 xfree (fields);
9535 }
9536 if (unsigned_enum)
9537 TYPE_UNSIGNED (this_type) = 1;
9538 if (flag_enum)
9539 TYPE_FLAG_ENUM (this_type) = 1;
9540 }
9541
9542 /* If we are reading an enum from a .debug_types unit, and the enum
9543 is a declaration, and the enum is not the signatured type in the
9544 unit, then we do not want to add a symbol for it. Adding a
9545 symbol would in some cases obscure the true definition of the
9546 enum, giving users an incomplete type when the definition is
9547 actually available. Note that we do not want to do this for all
9548 enums which are just declarations, because C++0x allows forward
9549 enum declarations. */
9550 if (cu->per_cu->is_debug_types
9551 && die_is_declaration (die, cu))
9552 {
9553 struct signatured_type *sig_type;
9554
9555 sig_type
9556 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9557 cu->per_cu->info_or_types_section,
9558 cu->per_cu->offset);
9559 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9560 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9561 return;
9562 }
9563
9564 new_symbol (die, this_type, cu);
9565 }
9566
9567 /* Extract all information from a DW_TAG_array_type DIE and put it in
9568 the DIE's type field. For now, this only handles one dimensional
9569 arrays. */
9570
9571 static struct type *
9572 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9573 {
9574 struct objfile *objfile = cu->objfile;
9575 struct die_info *child_die;
9576 struct type *type;
9577 struct type *element_type, *range_type, *index_type;
9578 struct type **range_types = NULL;
9579 struct attribute *attr;
9580 int ndim = 0;
9581 struct cleanup *back_to;
9582 char *name;
9583
9584 element_type = die_type (die, cu);
9585
9586 /* The die_type call above may have already set the type for this DIE. */
9587 type = get_die_type (die, cu);
9588 if (type)
9589 return type;
9590
9591 /* Irix 6.2 native cc creates array types without children for
9592 arrays with unspecified length. */
9593 if (die->child == NULL)
9594 {
9595 index_type = objfile_type (objfile)->builtin_int;
9596 range_type = create_range_type (NULL, index_type, 0, -1);
9597 type = create_array_type (NULL, element_type, range_type);
9598 return set_die_type (die, type, cu);
9599 }
9600
9601 back_to = make_cleanup (null_cleanup, NULL);
9602 child_die = die->child;
9603 while (child_die && child_die->tag)
9604 {
9605 if (child_die->tag == DW_TAG_subrange_type)
9606 {
9607 struct type *child_type = read_type_die (child_die, cu);
9608
9609 if (child_type != NULL)
9610 {
9611 /* The range type was succesfully read. Save it for the
9612 array type creation. */
9613 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9614 {
9615 range_types = (struct type **)
9616 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9617 * sizeof (struct type *));
9618 if (ndim == 0)
9619 make_cleanup (free_current_contents, &range_types);
9620 }
9621 range_types[ndim++] = child_type;
9622 }
9623 }
9624 child_die = sibling_die (child_die);
9625 }
9626
9627 /* Dwarf2 dimensions are output from left to right, create the
9628 necessary array types in backwards order. */
9629
9630 type = element_type;
9631
9632 if (read_array_order (die, cu) == DW_ORD_col_major)
9633 {
9634 int i = 0;
9635
9636 while (i < ndim)
9637 type = create_array_type (NULL, type, range_types[i++]);
9638 }
9639 else
9640 {
9641 while (ndim-- > 0)
9642 type = create_array_type (NULL, type, range_types[ndim]);
9643 }
9644
9645 /* Understand Dwarf2 support for vector types (like they occur on
9646 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9647 array type. This is not part of the Dwarf2/3 standard yet, but a
9648 custom vendor extension. The main difference between a regular
9649 array and the vector variant is that vectors are passed by value
9650 to functions. */
9651 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9652 if (attr)
9653 make_vector_type (type);
9654
9655 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9656 implementation may choose to implement triple vectors using this
9657 attribute. */
9658 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9659 if (attr)
9660 {
9661 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9662 TYPE_LENGTH (type) = DW_UNSND (attr);
9663 else
9664 complaint (&symfile_complaints,
9665 _("DW_AT_byte_size for array type smaller "
9666 "than the total size of elements"));
9667 }
9668
9669 name = dwarf2_name (die, cu);
9670 if (name)
9671 TYPE_NAME (type) = name;
9672
9673 /* Install the type in the die. */
9674 set_die_type (die, type, cu);
9675
9676 /* set_die_type should be already done. */
9677 set_descriptive_type (type, die, cu);
9678
9679 do_cleanups (back_to);
9680
9681 return type;
9682 }
9683
9684 static enum dwarf_array_dim_ordering
9685 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9686 {
9687 struct attribute *attr;
9688
9689 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9690
9691 if (attr) return DW_SND (attr);
9692
9693 /* GNU F77 is a special case, as at 08/2004 array type info is the
9694 opposite order to the dwarf2 specification, but data is still
9695 laid out as per normal fortran.
9696
9697 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9698 version checking. */
9699
9700 if (cu->language == language_fortran
9701 && cu->producer && strstr (cu->producer, "GNU F77"))
9702 {
9703 return DW_ORD_row_major;
9704 }
9705
9706 switch (cu->language_defn->la_array_ordering)
9707 {
9708 case array_column_major:
9709 return DW_ORD_col_major;
9710 case array_row_major:
9711 default:
9712 return DW_ORD_row_major;
9713 };
9714 }
9715
9716 /* Extract all information from a DW_TAG_set_type DIE and put it in
9717 the DIE's type field. */
9718
9719 static struct type *
9720 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9721 {
9722 struct type *domain_type, *set_type;
9723 struct attribute *attr;
9724
9725 domain_type = die_type (die, cu);
9726
9727 /* The die_type call above may have already set the type for this DIE. */
9728 set_type = get_die_type (die, cu);
9729 if (set_type)
9730 return set_type;
9731
9732 set_type = create_set_type (NULL, domain_type);
9733
9734 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9735 if (attr)
9736 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9737
9738 return set_die_type (die, set_type, cu);
9739 }
9740
9741 /* First cut: install each common block member as a global variable. */
9742
9743 static void
9744 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9745 {
9746 struct die_info *child_die;
9747 struct attribute *attr;
9748 struct symbol *sym;
9749 CORE_ADDR base = (CORE_ADDR) 0;
9750
9751 attr = dwarf2_attr (die, DW_AT_location, cu);
9752 if (attr)
9753 {
9754 /* Support the .debug_loc offsets. */
9755 if (attr_form_is_block (attr))
9756 {
9757 base = decode_locdesc (DW_BLOCK (attr), cu);
9758 }
9759 else if (attr_form_is_section_offset (attr))
9760 {
9761 dwarf2_complex_location_expr_complaint ();
9762 }
9763 else
9764 {
9765 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9766 "common block member");
9767 }
9768 }
9769 if (die->child != NULL)
9770 {
9771 child_die = die->child;
9772 while (child_die && child_die->tag)
9773 {
9774 LONGEST offset;
9775
9776 sym = new_symbol (child_die, NULL, cu);
9777 if (sym != NULL
9778 && handle_data_member_location (child_die, cu, &offset))
9779 {
9780 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9781 add_symbol_to_list (sym, &global_symbols);
9782 }
9783 child_die = sibling_die (child_die);
9784 }
9785 }
9786 }
9787
9788 /* Create a type for a C++ namespace. */
9789
9790 static struct type *
9791 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9792 {
9793 struct objfile *objfile = cu->objfile;
9794 const char *previous_prefix, *name;
9795 int is_anonymous;
9796 struct type *type;
9797
9798 /* For extensions, reuse the type of the original namespace. */
9799 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9800 {
9801 struct die_info *ext_die;
9802 struct dwarf2_cu *ext_cu = cu;
9803
9804 ext_die = dwarf2_extension (die, &ext_cu);
9805 type = read_type_die (ext_die, ext_cu);
9806
9807 /* EXT_CU may not be the same as CU.
9808 Ensure TYPE is recorded in CU's type_hash table. */
9809 return set_die_type (die, type, cu);
9810 }
9811
9812 name = namespace_name (die, &is_anonymous, cu);
9813
9814 /* Now build the name of the current namespace. */
9815
9816 previous_prefix = determine_prefix (die, cu);
9817 if (previous_prefix[0] != '\0')
9818 name = typename_concat (&objfile->objfile_obstack,
9819 previous_prefix, name, 0, cu);
9820
9821 /* Create the type. */
9822 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9823 objfile);
9824 TYPE_NAME (type) = (char *) name;
9825 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9826
9827 return set_die_type (die, type, cu);
9828 }
9829
9830 /* Read a C++ namespace. */
9831
9832 static void
9833 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9834 {
9835 struct objfile *objfile = cu->objfile;
9836 int is_anonymous;
9837
9838 /* Add a symbol associated to this if we haven't seen the namespace
9839 before. Also, add a using directive if it's an anonymous
9840 namespace. */
9841
9842 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9843 {
9844 struct type *type;
9845
9846 type = read_type_die (die, cu);
9847 new_symbol (die, type, cu);
9848
9849 namespace_name (die, &is_anonymous, cu);
9850 if (is_anonymous)
9851 {
9852 const char *previous_prefix = determine_prefix (die, cu);
9853
9854 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9855 NULL, NULL, &objfile->objfile_obstack);
9856 }
9857 }
9858
9859 if (die->child != NULL)
9860 {
9861 struct die_info *child_die = die->child;
9862
9863 while (child_die && child_die->tag)
9864 {
9865 process_die (child_die, cu);
9866 child_die = sibling_die (child_die);
9867 }
9868 }
9869 }
9870
9871 /* Read a Fortran module as type. This DIE can be only a declaration used for
9872 imported module. Still we need that type as local Fortran "use ... only"
9873 declaration imports depend on the created type in determine_prefix. */
9874
9875 static struct type *
9876 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9877 {
9878 struct objfile *objfile = cu->objfile;
9879 char *module_name;
9880 struct type *type;
9881
9882 module_name = dwarf2_name (die, cu);
9883 if (!module_name)
9884 complaint (&symfile_complaints,
9885 _("DW_TAG_module has no name, offset 0x%x"),
9886 die->offset.sect_off);
9887 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9888
9889 /* determine_prefix uses TYPE_TAG_NAME. */
9890 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9891
9892 return set_die_type (die, type, cu);
9893 }
9894
9895 /* Read a Fortran module. */
9896
9897 static void
9898 read_module (struct die_info *die, struct dwarf2_cu *cu)
9899 {
9900 struct die_info *child_die = die->child;
9901
9902 while (child_die && child_die->tag)
9903 {
9904 process_die (child_die, cu);
9905 child_die = sibling_die (child_die);
9906 }
9907 }
9908
9909 /* Return the name of the namespace represented by DIE. Set
9910 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9911 namespace. */
9912
9913 static const char *
9914 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9915 {
9916 struct die_info *current_die;
9917 const char *name = NULL;
9918
9919 /* Loop through the extensions until we find a name. */
9920
9921 for (current_die = die;
9922 current_die != NULL;
9923 current_die = dwarf2_extension (die, &cu))
9924 {
9925 name = dwarf2_name (current_die, cu);
9926 if (name != NULL)
9927 break;
9928 }
9929
9930 /* Is it an anonymous namespace? */
9931
9932 *is_anonymous = (name == NULL);
9933 if (*is_anonymous)
9934 name = CP_ANONYMOUS_NAMESPACE_STR;
9935
9936 return name;
9937 }
9938
9939 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9940 the user defined type vector. */
9941
9942 static struct type *
9943 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9944 {
9945 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9946 struct comp_unit_head *cu_header = &cu->header;
9947 struct type *type;
9948 struct attribute *attr_byte_size;
9949 struct attribute *attr_address_class;
9950 int byte_size, addr_class;
9951 struct type *target_type;
9952
9953 target_type = die_type (die, cu);
9954
9955 /* The die_type call above may have already set the type for this DIE. */
9956 type = get_die_type (die, cu);
9957 if (type)
9958 return type;
9959
9960 type = lookup_pointer_type (target_type);
9961
9962 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9963 if (attr_byte_size)
9964 byte_size = DW_UNSND (attr_byte_size);
9965 else
9966 byte_size = cu_header->addr_size;
9967
9968 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9969 if (attr_address_class)
9970 addr_class = DW_UNSND (attr_address_class);
9971 else
9972 addr_class = DW_ADDR_none;
9973
9974 /* If the pointer size or address class is different than the
9975 default, create a type variant marked as such and set the
9976 length accordingly. */
9977 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9978 {
9979 if (gdbarch_address_class_type_flags_p (gdbarch))
9980 {
9981 int type_flags;
9982
9983 type_flags = gdbarch_address_class_type_flags
9984 (gdbarch, byte_size, addr_class);
9985 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9986 == 0);
9987 type = make_type_with_address_space (type, type_flags);
9988 }
9989 else if (TYPE_LENGTH (type) != byte_size)
9990 {
9991 complaint (&symfile_complaints,
9992 _("invalid pointer size %d"), byte_size);
9993 }
9994 else
9995 {
9996 /* Should we also complain about unhandled address classes? */
9997 }
9998 }
9999
10000 TYPE_LENGTH (type) = byte_size;
10001 return set_die_type (die, type, cu);
10002 }
10003
10004 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10005 the user defined type vector. */
10006
10007 static struct type *
10008 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
10009 {
10010 struct type *type;
10011 struct type *to_type;
10012 struct type *domain;
10013
10014 to_type = die_type (die, cu);
10015 domain = die_containing_type (die, cu);
10016
10017 /* The calls above may have already set the type for this DIE. */
10018 type = get_die_type (die, cu);
10019 if (type)
10020 return type;
10021
10022 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10023 type = lookup_methodptr_type (to_type);
10024 else
10025 type = lookup_memberptr_type (to_type, domain);
10026
10027 return set_die_type (die, type, cu);
10028 }
10029
10030 /* Extract all information from a DW_TAG_reference_type DIE and add to
10031 the user defined type vector. */
10032
10033 static struct type *
10034 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
10035 {
10036 struct comp_unit_head *cu_header = &cu->header;
10037 struct type *type, *target_type;
10038 struct attribute *attr;
10039
10040 target_type = die_type (die, cu);
10041
10042 /* The die_type call above may have already set the type for this DIE. */
10043 type = get_die_type (die, cu);
10044 if (type)
10045 return type;
10046
10047 type = lookup_reference_type (target_type);
10048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10049 if (attr)
10050 {
10051 TYPE_LENGTH (type) = DW_UNSND (attr);
10052 }
10053 else
10054 {
10055 TYPE_LENGTH (type) = cu_header->addr_size;
10056 }
10057 return set_die_type (die, type, cu);
10058 }
10059
10060 static struct type *
10061 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10062 {
10063 struct type *base_type, *cv_type;
10064
10065 base_type = die_type (die, cu);
10066
10067 /* The die_type call above may have already set the type for this DIE. */
10068 cv_type = get_die_type (die, cu);
10069 if (cv_type)
10070 return cv_type;
10071
10072 /* In case the const qualifier is applied to an array type, the element type
10073 is so qualified, not the array type (section 6.7.3 of C99). */
10074 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10075 {
10076 struct type *el_type, *inner_array;
10077
10078 base_type = copy_type (base_type);
10079 inner_array = base_type;
10080
10081 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10082 {
10083 TYPE_TARGET_TYPE (inner_array) =
10084 copy_type (TYPE_TARGET_TYPE (inner_array));
10085 inner_array = TYPE_TARGET_TYPE (inner_array);
10086 }
10087
10088 el_type = TYPE_TARGET_TYPE (inner_array);
10089 TYPE_TARGET_TYPE (inner_array) =
10090 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10091
10092 return set_die_type (die, base_type, cu);
10093 }
10094
10095 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10096 return set_die_type (die, cv_type, cu);
10097 }
10098
10099 static struct type *
10100 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10101 {
10102 struct type *base_type, *cv_type;
10103
10104 base_type = die_type (die, cu);
10105
10106 /* The die_type call above may have already set the type for this DIE. */
10107 cv_type = get_die_type (die, cu);
10108 if (cv_type)
10109 return cv_type;
10110
10111 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10112 return set_die_type (die, cv_type, cu);
10113 }
10114
10115 /* Extract all information from a DW_TAG_string_type DIE and add to
10116 the user defined type vector. It isn't really a user defined type,
10117 but it behaves like one, with other DIE's using an AT_user_def_type
10118 attribute to reference it. */
10119
10120 static struct type *
10121 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10122 {
10123 struct objfile *objfile = cu->objfile;
10124 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10125 struct type *type, *range_type, *index_type, *char_type;
10126 struct attribute *attr;
10127 unsigned int length;
10128
10129 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10130 if (attr)
10131 {
10132 length = DW_UNSND (attr);
10133 }
10134 else
10135 {
10136 /* Check for the DW_AT_byte_size attribute. */
10137 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10138 if (attr)
10139 {
10140 length = DW_UNSND (attr);
10141 }
10142 else
10143 {
10144 length = 1;
10145 }
10146 }
10147
10148 index_type = objfile_type (objfile)->builtin_int;
10149 range_type = create_range_type (NULL, index_type, 1, length);
10150 char_type = language_string_char_type (cu->language_defn, gdbarch);
10151 type = create_string_type (NULL, char_type, range_type);
10152
10153 return set_die_type (die, type, cu);
10154 }
10155
10156 /* Handle DIES due to C code like:
10157
10158 struct foo
10159 {
10160 int (*funcp)(int a, long l);
10161 int b;
10162 };
10163
10164 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10165
10166 static struct type *
10167 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10168 {
10169 struct objfile *objfile = cu->objfile;
10170 struct type *type; /* Type that this function returns. */
10171 struct type *ftype; /* Function that returns above type. */
10172 struct attribute *attr;
10173
10174 type = die_type (die, cu);
10175
10176 /* The die_type call above may have already set the type for this DIE. */
10177 ftype = get_die_type (die, cu);
10178 if (ftype)
10179 return ftype;
10180
10181 ftype = lookup_function_type (type);
10182
10183 /* All functions in C++, Pascal and Java have prototypes. */
10184 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10185 if ((attr && (DW_UNSND (attr) != 0))
10186 || cu->language == language_cplus
10187 || cu->language == language_java
10188 || cu->language == language_pascal)
10189 TYPE_PROTOTYPED (ftype) = 1;
10190 else if (producer_is_realview (cu->producer))
10191 /* RealView does not emit DW_AT_prototyped. We can not
10192 distinguish prototyped and unprototyped functions; default to
10193 prototyped, since that is more common in modern code (and
10194 RealView warns about unprototyped functions). */
10195 TYPE_PROTOTYPED (ftype) = 1;
10196
10197 /* Store the calling convention in the type if it's available in
10198 the subroutine die. Otherwise set the calling convention to
10199 the default value DW_CC_normal. */
10200 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10201 if (attr)
10202 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10203 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10204 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10205 else
10206 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10207
10208 /* We need to add the subroutine type to the die immediately so
10209 we don't infinitely recurse when dealing with parameters
10210 declared as the same subroutine type. */
10211 set_die_type (die, ftype, cu);
10212
10213 if (die->child != NULL)
10214 {
10215 struct type *void_type = objfile_type (objfile)->builtin_void;
10216 struct die_info *child_die;
10217 int nparams, iparams;
10218
10219 /* Count the number of parameters.
10220 FIXME: GDB currently ignores vararg functions, but knows about
10221 vararg member functions. */
10222 nparams = 0;
10223 child_die = die->child;
10224 while (child_die && child_die->tag)
10225 {
10226 if (child_die->tag == DW_TAG_formal_parameter)
10227 nparams++;
10228 else if (child_die->tag == DW_TAG_unspecified_parameters)
10229 TYPE_VARARGS (ftype) = 1;
10230 child_die = sibling_die (child_die);
10231 }
10232
10233 /* Allocate storage for parameters and fill them in. */
10234 TYPE_NFIELDS (ftype) = nparams;
10235 TYPE_FIELDS (ftype) = (struct field *)
10236 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10237
10238 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10239 even if we error out during the parameters reading below. */
10240 for (iparams = 0; iparams < nparams; iparams++)
10241 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10242
10243 iparams = 0;
10244 child_die = die->child;
10245 while (child_die && child_die->tag)
10246 {
10247 if (child_die->tag == DW_TAG_formal_parameter)
10248 {
10249 struct type *arg_type;
10250
10251 /* DWARF version 2 has no clean way to discern C++
10252 static and non-static member functions. G++ helps
10253 GDB by marking the first parameter for non-static
10254 member functions (which is the this pointer) as
10255 artificial. We pass this information to
10256 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10257
10258 DWARF version 3 added DW_AT_object_pointer, which GCC
10259 4.5 does not yet generate. */
10260 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10261 if (attr)
10262 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10263 else
10264 {
10265 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10266
10267 /* GCC/43521: In java, the formal parameter
10268 "this" is sometimes not marked with DW_AT_artificial. */
10269 if (cu->language == language_java)
10270 {
10271 const char *name = dwarf2_name (child_die, cu);
10272
10273 if (name && !strcmp (name, "this"))
10274 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10275 }
10276 }
10277 arg_type = die_type (child_die, cu);
10278
10279 /* RealView does not mark THIS as const, which the testsuite
10280 expects. GCC marks THIS as const in method definitions,
10281 but not in the class specifications (GCC PR 43053). */
10282 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10283 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10284 {
10285 int is_this = 0;
10286 struct dwarf2_cu *arg_cu = cu;
10287 const char *name = dwarf2_name (child_die, cu);
10288
10289 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10290 if (attr)
10291 {
10292 /* If the compiler emits this, use it. */
10293 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10294 is_this = 1;
10295 }
10296 else if (name && strcmp (name, "this") == 0)
10297 /* Function definitions will have the argument names. */
10298 is_this = 1;
10299 else if (name == NULL && iparams == 0)
10300 /* Declarations may not have the names, so like
10301 elsewhere in GDB, assume an artificial first
10302 argument is "this". */
10303 is_this = 1;
10304
10305 if (is_this)
10306 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10307 arg_type, 0);
10308 }
10309
10310 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10311 iparams++;
10312 }
10313 child_die = sibling_die (child_die);
10314 }
10315 }
10316
10317 return ftype;
10318 }
10319
10320 static struct type *
10321 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10322 {
10323 struct objfile *objfile = cu->objfile;
10324 const char *name = NULL;
10325 struct type *this_type, *target_type;
10326
10327 name = dwarf2_full_name (NULL, die, cu);
10328 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10329 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10330 TYPE_NAME (this_type) = (char *) name;
10331 set_die_type (die, this_type, cu);
10332 target_type = die_type (die, cu);
10333 if (target_type != this_type)
10334 TYPE_TARGET_TYPE (this_type) = target_type;
10335 else
10336 {
10337 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10338 spec and cause infinite loops in GDB. */
10339 complaint (&symfile_complaints,
10340 _("Self-referential DW_TAG_typedef "
10341 "- DIE at 0x%x [in module %s]"),
10342 die->offset.sect_off, objfile->name);
10343 TYPE_TARGET_TYPE (this_type) = NULL;
10344 }
10345 return this_type;
10346 }
10347
10348 /* Find a representation of a given base type and install
10349 it in the TYPE field of the die. */
10350
10351 static struct type *
10352 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10353 {
10354 struct objfile *objfile = cu->objfile;
10355 struct type *type;
10356 struct attribute *attr;
10357 int encoding = 0, size = 0;
10358 char *name;
10359 enum type_code code = TYPE_CODE_INT;
10360 int type_flags = 0;
10361 struct type *target_type = NULL;
10362
10363 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10364 if (attr)
10365 {
10366 encoding = DW_UNSND (attr);
10367 }
10368 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10369 if (attr)
10370 {
10371 size = DW_UNSND (attr);
10372 }
10373 name = dwarf2_name (die, cu);
10374 if (!name)
10375 {
10376 complaint (&symfile_complaints,
10377 _("DW_AT_name missing from DW_TAG_base_type"));
10378 }
10379
10380 switch (encoding)
10381 {
10382 case DW_ATE_address:
10383 /* Turn DW_ATE_address into a void * pointer. */
10384 code = TYPE_CODE_PTR;
10385 type_flags |= TYPE_FLAG_UNSIGNED;
10386 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10387 break;
10388 case DW_ATE_boolean:
10389 code = TYPE_CODE_BOOL;
10390 type_flags |= TYPE_FLAG_UNSIGNED;
10391 break;
10392 case DW_ATE_complex_float:
10393 code = TYPE_CODE_COMPLEX;
10394 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10395 break;
10396 case DW_ATE_decimal_float:
10397 code = TYPE_CODE_DECFLOAT;
10398 break;
10399 case DW_ATE_float:
10400 code = TYPE_CODE_FLT;
10401 break;
10402 case DW_ATE_signed:
10403 break;
10404 case DW_ATE_unsigned:
10405 type_flags |= TYPE_FLAG_UNSIGNED;
10406 if (cu->language == language_fortran
10407 && name
10408 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10409 code = TYPE_CODE_CHAR;
10410 break;
10411 case DW_ATE_signed_char:
10412 if (cu->language == language_ada || cu->language == language_m2
10413 || cu->language == language_pascal
10414 || cu->language == language_fortran)
10415 code = TYPE_CODE_CHAR;
10416 break;
10417 case DW_ATE_unsigned_char:
10418 if (cu->language == language_ada || cu->language == language_m2
10419 || cu->language == language_pascal
10420 || cu->language == language_fortran)
10421 code = TYPE_CODE_CHAR;
10422 type_flags |= TYPE_FLAG_UNSIGNED;
10423 break;
10424 case DW_ATE_UTF:
10425 /* We just treat this as an integer and then recognize the
10426 type by name elsewhere. */
10427 break;
10428
10429 default:
10430 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10431 dwarf_type_encoding_name (encoding));
10432 break;
10433 }
10434
10435 type = init_type (code, size, type_flags, NULL, objfile);
10436 TYPE_NAME (type) = name;
10437 TYPE_TARGET_TYPE (type) = target_type;
10438
10439 if (name && strcmp (name, "char") == 0)
10440 TYPE_NOSIGN (type) = 1;
10441
10442 return set_die_type (die, type, cu);
10443 }
10444
10445 /* Read the given DW_AT_subrange DIE. */
10446
10447 static struct type *
10448 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10449 {
10450 struct type *base_type;
10451 struct type *range_type;
10452 struct attribute *attr;
10453 LONGEST low, high;
10454 int low_default_is_valid;
10455 char *name;
10456 LONGEST negative_mask;
10457
10458 base_type = die_type (die, cu);
10459 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10460 check_typedef (base_type);
10461
10462 /* The die_type call above may have already set the type for this DIE. */
10463 range_type = get_die_type (die, cu);
10464 if (range_type)
10465 return range_type;
10466
10467 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10468 omitting DW_AT_lower_bound. */
10469 switch (cu->language)
10470 {
10471 case language_c:
10472 case language_cplus:
10473 low = 0;
10474 low_default_is_valid = 1;
10475 break;
10476 case language_fortran:
10477 low = 1;
10478 low_default_is_valid = 1;
10479 break;
10480 case language_d:
10481 case language_java:
10482 case language_objc:
10483 low = 0;
10484 low_default_is_valid = (cu->header.version >= 4);
10485 break;
10486 case language_ada:
10487 case language_m2:
10488 case language_pascal:
10489 low = 1;
10490 low_default_is_valid = (cu->header.version >= 4);
10491 break;
10492 default:
10493 low = 0;
10494 low_default_is_valid = 0;
10495 break;
10496 }
10497
10498 /* FIXME: For variable sized arrays either of these could be
10499 a variable rather than a constant value. We'll allow it,
10500 but we don't know how to handle it. */
10501 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10502 if (attr)
10503 low = dwarf2_get_attr_constant_value (attr, low);
10504 else if (!low_default_is_valid)
10505 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10506 "- DIE at 0x%x [in module %s]"),
10507 die->offset.sect_off, cu->objfile->name);
10508
10509 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10510 if (attr)
10511 {
10512 if (attr_form_is_block (attr) || is_ref_attr (attr))
10513 {
10514 /* GCC encodes arrays with unspecified or dynamic length
10515 with a DW_FORM_block1 attribute or a reference attribute.
10516 FIXME: GDB does not yet know how to handle dynamic
10517 arrays properly, treat them as arrays with unspecified
10518 length for now.
10519
10520 FIXME: jimb/2003-09-22: GDB does not really know
10521 how to handle arrays of unspecified length
10522 either; we just represent them as zero-length
10523 arrays. Choose an appropriate upper bound given
10524 the lower bound we've computed above. */
10525 high = low - 1;
10526 }
10527 else
10528 high = dwarf2_get_attr_constant_value (attr, 1);
10529 }
10530 else
10531 {
10532 attr = dwarf2_attr (die, DW_AT_count, cu);
10533 if (attr)
10534 {
10535 int count = dwarf2_get_attr_constant_value (attr, 1);
10536 high = low + count - 1;
10537 }
10538 else
10539 {
10540 /* Unspecified array length. */
10541 high = low - 1;
10542 }
10543 }
10544
10545 /* Dwarf-2 specifications explicitly allows to create subrange types
10546 without specifying a base type.
10547 In that case, the base type must be set to the type of
10548 the lower bound, upper bound or count, in that order, if any of these
10549 three attributes references an object that has a type.
10550 If no base type is found, the Dwarf-2 specifications say that
10551 a signed integer type of size equal to the size of an address should
10552 be used.
10553 For the following C code: `extern char gdb_int [];'
10554 GCC produces an empty range DIE.
10555 FIXME: muller/2010-05-28: Possible references to object for low bound,
10556 high bound or count are not yet handled by this code. */
10557 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10558 {
10559 struct objfile *objfile = cu->objfile;
10560 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10561 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10562 struct type *int_type = objfile_type (objfile)->builtin_int;
10563
10564 /* Test "int", "long int", and "long long int" objfile types,
10565 and select the first one having a size above or equal to the
10566 architecture address size. */
10567 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10568 base_type = int_type;
10569 else
10570 {
10571 int_type = objfile_type (objfile)->builtin_long;
10572 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10573 base_type = int_type;
10574 else
10575 {
10576 int_type = objfile_type (objfile)->builtin_long_long;
10577 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10578 base_type = int_type;
10579 }
10580 }
10581 }
10582
10583 negative_mask =
10584 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10585 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10586 low |= negative_mask;
10587 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10588 high |= negative_mask;
10589
10590 range_type = create_range_type (NULL, base_type, low, high);
10591
10592 /* Mark arrays with dynamic length at least as an array of unspecified
10593 length. GDB could check the boundary but before it gets implemented at
10594 least allow accessing the array elements. */
10595 if (attr && attr_form_is_block (attr))
10596 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10597
10598 /* Ada expects an empty array on no boundary attributes. */
10599 if (attr == NULL && cu->language != language_ada)
10600 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10601
10602 name = dwarf2_name (die, cu);
10603 if (name)
10604 TYPE_NAME (range_type) = name;
10605
10606 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10607 if (attr)
10608 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10609
10610 set_die_type (die, range_type, cu);
10611
10612 /* set_die_type should be already done. */
10613 set_descriptive_type (range_type, die, cu);
10614
10615 return range_type;
10616 }
10617
10618 static struct type *
10619 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 struct type *type;
10622
10623 /* For now, we only support the C meaning of an unspecified type: void. */
10624
10625 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10626 TYPE_NAME (type) = dwarf2_name (die, cu);
10627
10628 return set_die_type (die, type, cu);
10629 }
10630
10631 /* Read a single die and all its descendents. Set the die's sibling
10632 field to NULL; set other fields in the die correctly, and set all
10633 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10634 location of the info_ptr after reading all of those dies. PARENT
10635 is the parent of the die in question. */
10636
10637 static struct die_info *
10638 read_die_and_children (const struct die_reader_specs *reader,
10639 gdb_byte *info_ptr,
10640 gdb_byte **new_info_ptr,
10641 struct die_info *parent)
10642 {
10643 struct die_info *die;
10644 gdb_byte *cur_ptr;
10645 int has_children;
10646
10647 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10648 if (die == NULL)
10649 {
10650 *new_info_ptr = cur_ptr;
10651 return NULL;
10652 }
10653 store_in_ref_table (die, reader->cu);
10654
10655 if (has_children)
10656 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10657 else
10658 {
10659 die->child = NULL;
10660 *new_info_ptr = cur_ptr;
10661 }
10662
10663 die->sibling = NULL;
10664 die->parent = parent;
10665 return die;
10666 }
10667
10668 /* Read a die, all of its descendents, and all of its siblings; set
10669 all of the fields of all of the dies correctly. Arguments are as
10670 in read_die_and_children. */
10671
10672 static struct die_info *
10673 read_die_and_siblings (const struct die_reader_specs *reader,
10674 gdb_byte *info_ptr,
10675 gdb_byte **new_info_ptr,
10676 struct die_info *parent)
10677 {
10678 struct die_info *first_die, *last_sibling;
10679 gdb_byte *cur_ptr;
10680
10681 cur_ptr = info_ptr;
10682 first_die = last_sibling = NULL;
10683
10684 while (1)
10685 {
10686 struct die_info *die
10687 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10688
10689 if (die == NULL)
10690 {
10691 *new_info_ptr = cur_ptr;
10692 return first_die;
10693 }
10694
10695 if (!first_die)
10696 first_die = die;
10697 else
10698 last_sibling->sibling = die;
10699
10700 last_sibling = die;
10701 }
10702 }
10703
10704 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10705 attributes.
10706 The caller is responsible for filling in the extra attributes
10707 and updating (*DIEP)->num_attrs.
10708 Set DIEP to point to a newly allocated die with its information,
10709 except for its child, sibling, and parent fields.
10710 Set HAS_CHILDREN to tell whether the die has children or not. */
10711
10712 static gdb_byte *
10713 read_full_die_1 (const struct die_reader_specs *reader,
10714 struct die_info **diep, gdb_byte *info_ptr,
10715 int *has_children, int num_extra_attrs)
10716 {
10717 unsigned int abbrev_number, bytes_read, i;
10718 sect_offset offset;
10719 struct abbrev_info *abbrev;
10720 struct die_info *die;
10721 struct dwarf2_cu *cu = reader->cu;
10722 bfd *abfd = reader->abfd;
10723
10724 offset.sect_off = info_ptr - reader->buffer;
10725 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10726 info_ptr += bytes_read;
10727 if (!abbrev_number)
10728 {
10729 *diep = NULL;
10730 *has_children = 0;
10731 return info_ptr;
10732 }
10733
10734 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10735 if (!abbrev)
10736 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10737 abbrev_number,
10738 bfd_get_filename (abfd));
10739
10740 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10741 die->offset = offset;
10742 die->tag = abbrev->tag;
10743 die->abbrev = abbrev_number;
10744
10745 /* Make the result usable.
10746 The caller needs to update num_attrs after adding the extra
10747 attributes. */
10748 die->num_attrs = abbrev->num_attrs;
10749
10750 for (i = 0; i < abbrev->num_attrs; ++i)
10751 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10752 info_ptr);
10753
10754 *diep = die;
10755 *has_children = abbrev->has_children;
10756 return info_ptr;
10757 }
10758
10759 /* Read a die and all its attributes.
10760 Set DIEP to point to a newly allocated die with its information,
10761 except for its child, sibling, and parent fields.
10762 Set HAS_CHILDREN to tell whether the die has children or not. */
10763
10764 static gdb_byte *
10765 read_full_die (const struct die_reader_specs *reader,
10766 struct die_info **diep, gdb_byte *info_ptr,
10767 int *has_children)
10768 {
10769 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10770 }
10771
10772 /* In DWARF version 2, the description of the debugging information is
10773 stored in a separate .debug_abbrev section. Before we read any
10774 dies from a section we read in all abbreviations and install them
10775 in a hash table. This function also sets flags in CU describing
10776 the data found in the abbrev table. */
10777
10778 static void
10779 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10780 struct dwarf2_section_info *abbrev_section)
10781
10782 {
10783 bfd *abfd = abbrev_section->asection->owner;
10784 struct comp_unit_head *cu_header = &cu->header;
10785 gdb_byte *abbrev_ptr;
10786 struct abbrev_info *cur_abbrev;
10787 unsigned int abbrev_number, bytes_read, abbrev_name;
10788 unsigned int abbrev_form, hash_number;
10789 struct attr_abbrev *cur_attrs;
10790 unsigned int allocated_attrs;
10791
10792 /* Initialize dwarf2 abbrevs. */
10793 obstack_init (&cu->abbrev_obstack);
10794 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10795 (ABBREV_HASH_SIZE
10796 * sizeof (struct abbrev_info *)));
10797 memset (cu->dwarf2_abbrevs, 0,
10798 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10799
10800 dwarf2_read_section (cu->objfile, abbrev_section);
10801 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10802 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10803 abbrev_ptr += bytes_read;
10804
10805 allocated_attrs = ATTR_ALLOC_CHUNK;
10806 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10807
10808 /* Loop until we reach an abbrev number of 0. */
10809 while (abbrev_number)
10810 {
10811 cur_abbrev = dwarf_alloc_abbrev (cu);
10812
10813 /* read in abbrev header */
10814 cur_abbrev->number = abbrev_number;
10815 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10816 abbrev_ptr += bytes_read;
10817 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10818 abbrev_ptr += 1;
10819
10820 /* now read in declarations */
10821 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10822 abbrev_ptr += bytes_read;
10823 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10824 abbrev_ptr += bytes_read;
10825 while (abbrev_name)
10826 {
10827 if (cur_abbrev->num_attrs == allocated_attrs)
10828 {
10829 allocated_attrs += ATTR_ALLOC_CHUNK;
10830 cur_attrs
10831 = xrealloc (cur_attrs, (allocated_attrs
10832 * sizeof (struct attr_abbrev)));
10833 }
10834
10835 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10836 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10837 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10838 abbrev_ptr += bytes_read;
10839 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10840 abbrev_ptr += bytes_read;
10841 }
10842
10843 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10844 (cur_abbrev->num_attrs
10845 * sizeof (struct attr_abbrev)));
10846 memcpy (cur_abbrev->attrs, cur_attrs,
10847 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10848
10849 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10850 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10851 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10852
10853 /* Get next abbreviation.
10854 Under Irix6 the abbreviations for a compilation unit are not
10855 always properly terminated with an abbrev number of 0.
10856 Exit loop if we encounter an abbreviation which we have
10857 already read (which means we are about to read the abbreviations
10858 for the next compile unit) or if the end of the abbreviation
10859 table is reached. */
10860 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10861 >= abbrev_section->size)
10862 break;
10863 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10864 abbrev_ptr += bytes_read;
10865 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10866 break;
10867 }
10868
10869 xfree (cur_attrs);
10870 }
10871
10872 /* Release the memory used by the abbrev table for a compilation unit. */
10873
10874 static void
10875 dwarf2_free_abbrev_table (void *ptr_to_cu)
10876 {
10877 struct dwarf2_cu *cu = ptr_to_cu;
10878
10879 obstack_free (&cu->abbrev_obstack, NULL);
10880 cu->dwarf2_abbrevs = NULL;
10881 }
10882
10883 /* Lookup an abbrev_info structure in the abbrev hash table. */
10884
10885 static struct abbrev_info *
10886 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10887 {
10888 unsigned int hash_number;
10889 struct abbrev_info *abbrev;
10890
10891 hash_number = number % ABBREV_HASH_SIZE;
10892 abbrev = cu->dwarf2_abbrevs[hash_number];
10893
10894 while (abbrev)
10895 {
10896 if (abbrev->number == number)
10897 return abbrev;
10898 else
10899 abbrev = abbrev->next;
10900 }
10901 return NULL;
10902 }
10903
10904 /* Returns nonzero if TAG represents a type that we might generate a partial
10905 symbol for. */
10906
10907 static int
10908 is_type_tag_for_partial (int tag)
10909 {
10910 switch (tag)
10911 {
10912 #if 0
10913 /* Some types that would be reasonable to generate partial symbols for,
10914 that we don't at present. */
10915 case DW_TAG_array_type:
10916 case DW_TAG_file_type:
10917 case DW_TAG_ptr_to_member_type:
10918 case DW_TAG_set_type:
10919 case DW_TAG_string_type:
10920 case DW_TAG_subroutine_type:
10921 #endif
10922 case DW_TAG_base_type:
10923 case DW_TAG_class_type:
10924 case DW_TAG_interface_type:
10925 case DW_TAG_enumeration_type:
10926 case DW_TAG_structure_type:
10927 case DW_TAG_subrange_type:
10928 case DW_TAG_typedef:
10929 case DW_TAG_union_type:
10930 return 1;
10931 default:
10932 return 0;
10933 }
10934 }
10935
10936 /* Load all DIEs that are interesting for partial symbols into memory. */
10937
10938 static struct partial_die_info *
10939 load_partial_dies (const struct die_reader_specs *reader,
10940 gdb_byte *info_ptr, int building_psymtab)
10941 {
10942 struct dwarf2_cu *cu = reader->cu;
10943 struct objfile *objfile = cu->objfile;
10944 struct partial_die_info *part_die;
10945 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10946 struct abbrev_info *abbrev;
10947 unsigned int bytes_read;
10948 unsigned int load_all = 0;
10949 int nesting_level = 1;
10950
10951 parent_die = NULL;
10952 last_die = NULL;
10953
10954 gdb_assert (cu->per_cu != NULL);
10955 if (cu->per_cu->load_all_dies)
10956 load_all = 1;
10957
10958 cu->partial_dies
10959 = htab_create_alloc_ex (cu->header.length / 12,
10960 partial_die_hash,
10961 partial_die_eq,
10962 NULL,
10963 &cu->comp_unit_obstack,
10964 hashtab_obstack_allocate,
10965 dummy_obstack_deallocate);
10966
10967 part_die = obstack_alloc (&cu->comp_unit_obstack,
10968 sizeof (struct partial_die_info));
10969
10970 while (1)
10971 {
10972 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10973
10974 /* A NULL abbrev means the end of a series of children. */
10975 if (abbrev == NULL)
10976 {
10977 if (--nesting_level == 0)
10978 {
10979 /* PART_DIE was probably the last thing allocated on the
10980 comp_unit_obstack, so we could call obstack_free
10981 here. We don't do that because the waste is small,
10982 and will be cleaned up when we're done with this
10983 compilation unit. This way, we're also more robust
10984 against other users of the comp_unit_obstack. */
10985 return first_die;
10986 }
10987 info_ptr += bytes_read;
10988 last_die = parent_die;
10989 parent_die = parent_die->die_parent;
10990 continue;
10991 }
10992
10993 /* Check for template arguments. We never save these; if
10994 they're seen, we just mark the parent, and go on our way. */
10995 if (parent_die != NULL
10996 && cu->language == language_cplus
10997 && (abbrev->tag == DW_TAG_template_type_param
10998 || abbrev->tag == DW_TAG_template_value_param))
10999 {
11000 parent_die->has_template_arguments = 1;
11001
11002 if (!load_all)
11003 {
11004 /* We don't need a partial DIE for the template argument. */
11005 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11006 continue;
11007 }
11008 }
11009
11010 /* We only recurse into c++ subprograms looking for template arguments.
11011 Skip their other children. */
11012 if (!load_all
11013 && cu->language == language_cplus
11014 && parent_die != NULL
11015 && parent_die->tag == DW_TAG_subprogram)
11016 {
11017 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11018 continue;
11019 }
11020
11021 /* Check whether this DIE is interesting enough to save. Normally
11022 we would not be interested in members here, but there may be
11023 later variables referencing them via DW_AT_specification (for
11024 static members). */
11025 if (!load_all
11026 && !is_type_tag_for_partial (abbrev->tag)
11027 && abbrev->tag != DW_TAG_constant
11028 && abbrev->tag != DW_TAG_enumerator
11029 && abbrev->tag != DW_TAG_subprogram
11030 && abbrev->tag != DW_TAG_lexical_block
11031 && abbrev->tag != DW_TAG_variable
11032 && abbrev->tag != DW_TAG_namespace
11033 && abbrev->tag != DW_TAG_module
11034 && abbrev->tag != DW_TAG_member
11035 && abbrev->tag != DW_TAG_imported_unit)
11036 {
11037 /* Otherwise we skip to the next sibling, if any. */
11038 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11039 continue;
11040 }
11041
11042 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11043 info_ptr);
11044
11045 /* This two-pass algorithm for processing partial symbols has a
11046 high cost in cache pressure. Thus, handle some simple cases
11047 here which cover the majority of C partial symbols. DIEs
11048 which neither have specification tags in them, nor could have
11049 specification tags elsewhere pointing at them, can simply be
11050 processed and discarded.
11051
11052 This segment is also optional; scan_partial_symbols and
11053 add_partial_symbol will handle these DIEs if we chain
11054 them in normally. When compilers which do not emit large
11055 quantities of duplicate debug information are more common,
11056 this code can probably be removed. */
11057
11058 /* Any complete simple types at the top level (pretty much all
11059 of them, for a language without namespaces), can be processed
11060 directly. */
11061 if (parent_die == NULL
11062 && part_die->has_specification == 0
11063 && part_die->is_declaration == 0
11064 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11065 || part_die->tag == DW_TAG_base_type
11066 || part_die->tag == DW_TAG_subrange_type))
11067 {
11068 if (building_psymtab && part_die->name != NULL)
11069 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11070 VAR_DOMAIN, LOC_TYPEDEF,
11071 &objfile->static_psymbols,
11072 0, (CORE_ADDR) 0, cu->language, objfile);
11073 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11074 continue;
11075 }
11076
11077 /* The exception for DW_TAG_typedef with has_children above is
11078 a workaround of GCC PR debug/47510. In the case of this complaint
11079 type_name_no_tag_or_error will error on such types later.
11080
11081 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11082 it could not find the child DIEs referenced later, this is checked
11083 above. In correct DWARF DW_TAG_typedef should have no children. */
11084
11085 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11086 complaint (&symfile_complaints,
11087 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11088 "- DIE at 0x%x [in module %s]"),
11089 part_die->offset.sect_off, objfile->name);
11090
11091 /* If we're at the second level, and we're an enumerator, and
11092 our parent has no specification (meaning possibly lives in a
11093 namespace elsewhere), then we can add the partial symbol now
11094 instead of queueing it. */
11095 if (part_die->tag == DW_TAG_enumerator
11096 && parent_die != NULL
11097 && parent_die->die_parent == NULL
11098 && parent_die->tag == DW_TAG_enumeration_type
11099 && parent_die->has_specification == 0)
11100 {
11101 if (part_die->name == NULL)
11102 complaint (&symfile_complaints,
11103 _("malformed enumerator DIE ignored"));
11104 else if (building_psymtab)
11105 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11106 VAR_DOMAIN, LOC_CONST,
11107 (cu->language == language_cplus
11108 || cu->language == language_java)
11109 ? &objfile->global_psymbols
11110 : &objfile->static_psymbols,
11111 0, (CORE_ADDR) 0, cu->language, objfile);
11112
11113 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11114 continue;
11115 }
11116
11117 /* We'll save this DIE so link it in. */
11118 part_die->die_parent = parent_die;
11119 part_die->die_sibling = NULL;
11120 part_die->die_child = NULL;
11121
11122 if (last_die && last_die == parent_die)
11123 last_die->die_child = part_die;
11124 else if (last_die)
11125 last_die->die_sibling = part_die;
11126
11127 last_die = part_die;
11128
11129 if (first_die == NULL)
11130 first_die = part_die;
11131
11132 /* Maybe add the DIE to the hash table. Not all DIEs that we
11133 find interesting need to be in the hash table, because we
11134 also have the parent/sibling/child chains; only those that we
11135 might refer to by offset later during partial symbol reading.
11136
11137 For now this means things that might have be the target of a
11138 DW_AT_specification, DW_AT_abstract_origin, or
11139 DW_AT_extension. DW_AT_extension will refer only to
11140 namespaces; DW_AT_abstract_origin refers to functions (and
11141 many things under the function DIE, but we do not recurse
11142 into function DIEs during partial symbol reading) and
11143 possibly variables as well; DW_AT_specification refers to
11144 declarations. Declarations ought to have the DW_AT_declaration
11145 flag. It happens that GCC forgets to put it in sometimes, but
11146 only for functions, not for types.
11147
11148 Adding more things than necessary to the hash table is harmless
11149 except for the performance cost. Adding too few will result in
11150 wasted time in find_partial_die, when we reread the compilation
11151 unit with load_all_dies set. */
11152
11153 if (load_all
11154 || abbrev->tag == DW_TAG_constant
11155 || abbrev->tag == DW_TAG_subprogram
11156 || abbrev->tag == DW_TAG_variable
11157 || abbrev->tag == DW_TAG_namespace
11158 || part_die->is_declaration)
11159 {
11160 void **slot;
11161
11162 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11163 part_die->offset.sect_off, INSERT);
11164 *slot = part_die;
11165 }
11166
11167 part_die = obstack_alloc (&cu->comp_unit_obstack,
11168 sizeof (struct partial_die_info));
11169
11170 /* For some DIEs we want to follow their children (if any). For C
11171 we have no reason to follow the children of structures; for other
11172 languages we have to, so that we can get at method physnames
11173 to infer fully qualified class names, for DW_AT_specification,
11174 and for C++ template arguments. For C++, we also look one level
11175 inside functions to find template arguments (if the name of the
11176 function does not already contain the template arguments).
11177
11178 For Ada, we need to scan the children of subprograms and lexical
11179 blocks as well because Ada allows the definition of nested
11180 entities that could be interesting for the debugger, such as
11181 nested subprograms for instance. */
11182 if (last_die->has_children
11183 && (load_all
11184 || last_die->tag == DW_TAG_namespace
11185 || last_die->tag == DW_TAG_module
11186 || last_die->tag == DW_TAG_enumeration_type
11187 || (cu->language == language_cplus
11188 && last_die->tag == DW_TAG_subprogram
11189 && (last_die->name == NULL
11190 || strchr (last_die->name, '<') == NULL))
11191 || (cu->language != language_c
11192 && (last_die->tag == DW_TAG_class_type
11193 || last_die->tag == DW_TAG_interface_type
11194 || last_die->tag == DW_TAG_structure_type
11195 || last_die->tag == DW_TAG_union_type))
11196 || (cu->language == language_ada
11197 && (last_die->tag == DW_TAG_subprogram
11198 || last_die->tag == DW_TAG_lexical_block))))
11199 {
11200 nesting_level++;
11201 parent_die = last_die;
11202 continue;
11203 }
11204
11205 /* Otherwise we skip to the next sibling, if any. */
11206 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11207
11208 /* Back to the top, do it again. */
11209 }
11210 }
11211
11212 /* Read a minimal amount of information into the minimal die structure. */
11213
11214 static gdb_byte *
11215 read_partial_die (const struct die_reader_specs *reader,
11216 struct partial_die_info *part_die,
11217 struct abbrev_info *abbrev, unsigned int abbrev_len,
11218 gdb_byte *info_ptr)
11219 {
11220 struct dwarf2_cu *cu = reader->cu;
11221 struct objfile *objfile = cu->objfile;
11222 gdb_byte *buffer = reader->buffer;
11223 unsigned int i;
11224 struct attribute attr;
11225 int has_low_pc_attr = 0;
11226 int has_high_pc_attr = 0;
11227 int high_pc_relative = 0;
11228
11229 memset (part_die, 0, sizeof (struct partial_die_info));
11230
11231 part_die->offset.sect_off = info_ptr - buffer;
11232
11233 info_ptr += abbrev_len;
11234
11235 if (abbrev == NULL)
11236 return info_ptr;
11237
11238 part_die->tag = abbrev->tag;
11239 part_die->has_children = abbrev->has_children;
11240
11241 for (i = 0; i < abbrev->num_attrs; ++i)
11242 {
11243 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11244
11245 /* Store the data if it is of an attribute we want to keep in a
11246 partial symbol table. */
11247 switch (attr.name)
11248 {
11249 case DW_AT_name:
11250 switch (part_die->tag)
11251 {
11252 case DW_TAG_compile_unit:
11253 case DW_TAG_partial_unit:
11254 case DW_TAG_type_unit:
11255 /* Compilation units have a DW_AT_name that is a filename, not
11256 a source language identifier. */
11257 case DW_TAG_enumeration_type:
11258 case DW_TAG_enumerator:
11259 /* These tags always have simple identifiers already; no need
11260 to canonicalize them. */
11261 part_die->name = DW_STRING (&attr);
11262 break;
11263 default:
11264 part_die->name
11265 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11266 &objfile->objfile_obstack);
11267 break;
11268 }
11269 break;
11270 case DW_AT_linkage_name:
11271 case DW_AT_MIPS_linkage_name:
11272 /* Note that both forms of linkage name might appear. We
11273 assume they will be the same, and we only store the last
11274 one we see. */
11275 if (cu->language == language_ada)
11276 part_die->name = DW_STRING (&attr);
11277 part_die->linkage_name = DW_STRING (&attr);
11278 break;
11279 case DW_AT_low_pc:
11280 has_low_pc_attr = 1;
11281 part_die->lowpc = DW_ADDR (&attr);
11282 break;
11283 case DW_AT_high_pc:
11284 has_high_pc_attr = 1;
11285 if (attr.form == DW_FORM_addr
11286 || attr.form == DW_FORM_GNU_addr_index)
11287 part_die->highpc = DW_ADDR (&attr);
11288 else
11289 {
11290 high_pc_relative = 1;
11291 part_die->highpc = DW_UNSND (&attr);
11292 }
11293 break;
11294 case DW_AT_location:
11295 /* Support the .debug_loc offsets. */
11296 if (attr_form_is_block (&attr))
11297 {
11298 part_die->d.locdesc = DW_BLOCK (&attr);
11299 }
11300 else if (attr_form_is_section_offset (&attr))
11301 {
11302 dwarf2_complex_location_expr_complaint ();
11303 }
11304 else
11305 {
11306 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11307 "partial symbol information");
11308 }
11309 break;
11310 case DW_AT_external:
11311 part_die->is_external = DW_UNSND (&attr);
11312 break;
11313 case DW_AT_declaration:
11314 part_die->is_declaration = DW_UNSND (&attr);
11315 break;
11316 case DW_AT_type:
11317 part_die->has_type = 1;
11318 break;
11319 case DW_AT_abstract_origin:
11320 case DW_AT_specification:
11321 case DW_AT_extension:
11322 part_die->has_specification = 1;
11323 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11324 break;
11325 case DW_AT_sibling:
11326 /* Ignore absolute siblings, they might point outside of
11327 the current compile unit. */
11328 if (attr.form == DW_FORM_ref_addr)
11329 complaint (&symfile_complaints,
11330 _("ignoring absolute DW_AT_sibling"));
11331 else
11332 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11333 break;
11334 case DW_AT_byte_size:
11335 part_die->has_byte_size = 1;
11336 break;
11337 case DW_AT_calling_convention:
11338 /* DWARF doesn't provide a way to identify a program's source-level
11339 entry point. DW_AT_calling_convention attributes are only meant
11340 to describe functions' calling conventions.
11341
11342 However, because it's a necessary piece of information in
11343 Fortran, and because DW_CC_program is the only piece of debugging
11344 information whose definition refers to a 'main program' at all,
11345 several compilers have begun marking Fortran main programs with
11346 DW_CC_program --- even when those functions use the standard
11347 calling conventions.
11348
11349 So until DWARF specifies a way to provide this information and
11350 compilers pick up the new representation, we'll support this
11351 practice. */
11352 if (DW_UNSND (&attr) == DW_CC_program
11353 && cu->language == language_fortran)
11354 {
11355 set_main_name (part_die->name);
11356
11357 /* As this DIE has a static linkage the name would be difficult
11358 to look up later. */
11359 language_of_main = language_fortran;
11360 }
11361 break;
11362 case DW_AT_inline:
11363 if (DW_UNSND (&attr) == DW_INL_inlined
11364 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11365 part_die->may_be_inlined = 1;
11366 break;
11367
11368 case DW_AT_import:
11369 if (part_die->tag == DW_TAG_imported_unit)
11370 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11371 break;
11372
11373 default:
11374 break;
11375 }
11376 }
11377
11378 if (high_pc_relative)
11379 part_die->highpc += part_die->lowpc;
11380
11381 if (has_low_pc_attr && has_high_pc_attr)
11382 {
11383 /* When using the GNU linker, .gnu.linkonce. sections are used to
11384 eliminate duplicate copies of functions and vtables and such.
11385 The linker will arbitrarily choose one and discard the others.
11386 The AT_*_pc values for such functions refer to local labels in
11387 these sections. If the section from that file was discarded, the
11388 labels are not in the output, so the relocs get a value of 0.
11389 If this is a discarded function, mark the pc bounds as invalid,
11390 so that GDB will ignore it. */
11391 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11392 {
11393 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11394
11395 complaint (&symfile_complaints,
11396 _("DW_AT_low_pc %s is zero "
11397 "for DIE at 0x%x [in module %s]"),
11398 paddress (gdbarch, part_die->lowpc),
11399 part_die->offset.sect_off, objfile->name);
11400 }
11401 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11402 else if (part_die->lowpc >= part_die->highpc)
11403 {
11404 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11405
11406 complaint (&symfile_complaints,
11407 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11408 "for DIE at 0x%x [in module %s]"),
11409 paddress (gdbarch, part_die->lowpc),
11410 paddress (gdbarch, part_die->highpc),
11411 part_die->offset.sect_off, objfile->name);
11412 }
11413 else
11414 part_die->has_pc_info = 1;
11415 }
11416
11417 return info_ptr;
11418 }
11419
11420 /* Find a cached partial DIE at OFFSET in CU. */
11421
11422 static struct partial_die_info *
11423 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11424 {
11425 struct partial_die_info *lookup_die = NULL;
11426 struct partial_die_info part_die;
11427
11428 part_die.offset = offset;
11429 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11430 offset.sect_off);
11431
11432 return lookup_die;
11433 }
11434
11435 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11436 except in the case of .debug_types DIEs which do not reference
11437 outside their CU (they do however referencing other types via
11438 DW_FORM_ref_sig8). */
11439
11440 static struct partial_die_info *
11441 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11442 {
11443 struct objfile *objfile = cu->objfile;
11444 struct dwarf2_per_cu_data *per_cu = NULL;
11445 struct partial_die_info *pd = NULL;
11446
11447 if (offset_in_cu_p (&cu->header, offset))
11448 {
11449 pd = find_partial_die_in_comp_unit (offset, cu);
11450 if (pd != NULL)
11451 return pd;
11452 /* We missed recording what we needed.
11453 Load all dies and try again. */
11454 per_cu = cu->per_cu;
11455 }
11456 else
11457 {
11458 /* TUs don't reference other CUs/TUs (except via type signatures). */
11459 if (cu->per_cu->is_debug_types)
11460 {
11461 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11462 " external reference to offset 0x%lx [in module %s].\n"),
11463 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11464 bfd_get_filename (objfile->obfd));
11465 }
11466 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11467
11468 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11469 load_partial_comp_unit (per_cu);
11470
11471 per_cu->cu->last_used = 0;
11472 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11473 }
11474
11475 /* If we didn't find it, and not all dies have been loaded,
11476 load them all and try again. */
11477
11478 if (pd == NULL && per_cu->load_all_dies == 0)
11479 {
11480 per_cu->load_all_dies = 1;
11481
11482 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11483 THIS_CU->cu may already be in use. So we can't just free it and
11484 replace its DIEs with the ones we read in. Instead, we leave those
11485 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11486 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11487 set. */
11488 load_partial_comp_unit (per_cu);
11489
11490 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11491 }
11492
11493 if (pd == NULL)
11494 internal_error (__FILE__, __LINE__,
11495 _("could not find partial DIE 0x%x "
11496 "in cache [from module %s]\n"),
11497 offset.sect_off, bfd_get_filename (objfile->obfd));
11498 return pd;
11499 }
11500
11501 /* See if we can figure out if the class lives in a namespace. We do
11502 this by looking for a member function; its demangled name will
11503 contain namespace info, if there is any. */
11504
11505 static void
11506 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11507 struct dwarf2_cu *cu)
11508 {
11509 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11510 what template types look like, because the demangler
11511 frequently doesn't give the same name as the debug info. We
11512 could fix this by only using the demangled name to get the
11513 prefix (but see comment in read_structure_type). */
11514
11515 struct partial_die_info *real_pdi;
11516 struct partial_die_info *child_pdi;
11517
11518 /* If this DIE (this DIE's specification, if any) has a parent, then
11519 we should not do this. We'll prepend the parent's fully qualified
11520 name when we create the partial symbol. */
11521
11522 real_pdi = struct_pdi;
11523 while (real_pdi->has_specification)
11524 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11525
11526 if (real_pdi->die_parent != NULL)
11527 return;
11528
11529 for (child_pdi = struct_pdi->die_child;
11530 child_pdi != NULL;
11531 child_pdi = child_pdi->die_sibling)
11532 {
11533 if (child_pdi->tag == DW_TAG_subprogram
11534 && child_pdi->linkage_name != NULL)
11535 {
11536 char *actual_class_name
11537 = language_class_name_from_physname (cu->language_defn,
11538 child_pdi->linkage_name);
11539 if (actual_class_name != NULL)
11540 {
11541 struct_pdi->name
11542 = obsavestring (actual_class_name,
11543 strlen (actual_class_name),
11544 &cu->objfile->objfile_obstack);
11545 xfree (actual_class_name);
11546 }
11547 break;
11548 }
11549 }
11550 }
11551
11552 /* Adjust PART_DIE before generating a symbol for it. This function
11553 may set the is_external flag or change the DIE's name. */
11554
11555 static void
11556 fixup_partial_die (struct partial_die_info *part_die,
11557 struct dwarf2_cu *cu)
11558 {
11559 /* Once we've fixed up a die, there's no point in doing so again.
11560 This also avoids a memory leak if we were to call
11561 guess_partial_die_structure_name multiple times. */
11562 if (part_die->fixup_called)
11563 return;
11564
11565 /* If we found a reference attribute and the DIE has no name, try
11566 to find a name in the referred to DIE. */
11567
11568 if (part_die->name == NULL && part_die->has_specification)
11569 {
11570 struct partial_die_info *spec_die;
11571
11572 spec_die = find_partial_die (part_die->spec_offset, cu);
11573
11574 fixup_partial_die (spec_die, cu);
11575
11576 if (spec_die->name)
11577 {
11578 part_die->name = spec_die->name;
11579
11580 /* Copy DW_AT_external attribute if it is set. */
11581 if (spec_die->is_external)
11582 part_die->is_external = spec_die->is_external;
11583 }
11584 }
11585
11586 /* Set default names for some unnamed DIEs. */
11587
11588 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11589 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11590
11591 /* If there is no parent die to provide a namespace, and there are
11592 children, see if we can determine the namespace from their linkage
11593 name. */
11594 if (cu->language == language_cplus
11595 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11596 && part_die->die_parent == NULL
11597 && part_die->has_children
11598 && (part_die->tag == DW_TAG_class_type
11599 || part_die->tag == DW_TAG_structure_type
11600 || part_die->tag == DW_TAG_union_type))
11601 guess_partial_die_structure_name (part_die, cu);
11602
11603 /* GCC might emit a nameless struct or union that has a linkage
11604 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11605 if (part_die->name == NULL
11606 && (part_die->tag == DW_TAG_class_type
11607 || part_die->tag == DW_TAG_interface_type
11608 || part_die->tag == DW_TAG_structure_type
11609 || part_die->tag == DW_TAG_union_type)
11610 && part_die->linkage_name != NULL)
11611 {
11612 char *demangled;
11613
11614 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11615 if (demangled)
11616 {
11617 const char *base;
11618
11619 /* Strip any leading namespaces/classes, keep only the base name.
11620 DW_AT_name for named DIEs does not contain the prefixes. */
11621 base = strrchr (demangled, ':');
11622 if (base && base > demangled && base[-1] == ':')
11623 base++;
11624 else
11625 base = demangled;
11626
11627 part_die->name = obsavestring (base, strlen (base),
11628 &cu->objfile->objfile_obstack);
11629 xfree (demangled);
11630 }
11631 }
11632
11633 part_die->fixup_called = 1;
11634 }
11635
11636 /* Read an attribute value described by an attribute form. */
11637
11638 static gdb_byte *
11639 read_attribute_value (const struct die_reader_specs *reader,
11640 struct attribute *attr, unsigned form,
11641 gdb_byte *info_ptr)
11642 {
11643 struct dwarf2_cu *cu = reader->cu;
11644 bfd *abfd = reader->abfd;
11645 struct comp_unit_head *cu_header = &cu->header;
11646 unsigned int bytes_read;
11647 struct dwarf_block *blk;
11648
11649 attr->form = form;
11650 switch (form)
11651 {
11652 case DW_FORM_ref_addr:
11653 if (cu->header.version == 2)
11654 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11655 else
11656 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11657 &cu->header, &bytes_read);
11658 info_ptr += bytes_read;
11659 break;
11660 case DW_FORM_addr:
11661 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11662 info_ptr += bytes_read;
11663 break;
11664 case DW_FORM_block2:
11665 blk = dwarf_alloc_block (cu);
11666 blk->size = read_2_bytes (abfd, info_ptr);
11667 info_ptr += 2;
11668 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11669 info_ptr += blk->size;
11670 DW_BLOCK (attr) = blk;
11671 break;
11672 case DW_FORM_block4:
11673 blk = dwarf_alloc_block (cu);
11674 blk->size = read_4_bytes (abfd, info_ptr);
11675 info_ptr += 4;
11676 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11677 info_ptr += blk->size;
11678 DW_BLOCK (attr) = blk;
11679 break;
11680 case DW_FORM_data2:
11681 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11682 info_ptr += 2;
11683 break;
11684 case DW_FORM_data4:
11685 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11686 info_ptr += 4;
11687 break;
11688 case DW_FORM_data8:
11689 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11690 info_ptr += 8;
11691 break;
11692 case DW_FORM_sec_offset:
11693 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11694 info_ptr += bytes_read;
11695 break;
11696 case DW_FORM_string:
11697 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11698 DW_STRING_IS_CANONICAL (attr) = 0;
11699 info_ptr += bytes_read;
11700 break;
11701 case DW_FORM_strp:
11702 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11703 &bytes_read);
11704 DW_STRING_IS_CANONICAL (attr) = 0;
11705 info_ptr += bytes_read;
11706 break;
11707 case DW_FORM_exprloc:
11708 case DW_FORM_block:
11709 blk = dwarf_alloc_block (cu);
11710 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11711 info_ptr += bytes_read;
11712 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11713 info_ptr += blk->size;
11714 DW_BLOCK (attr) = blk;
11715 break;
11716 case DW_FORM_block1:
11717 blk = dwarf_alloc_block (cu);
11718 blk->size = read_1_byte (abfd, info_ptr);
11719 info_ptr += 1;
11720 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11721 info_ptr += blk->size;
11722 DW_BLOCK (attr) = blk;
11723 break;
11724 case DW_FORM_data1:
11725 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11726 info_ptr += 1;
11727 break;
11728 case DW_FORM_flag:
11729 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11730 info_ptr += 1;
11731 break;
11732 case DW_FORM_flag_present:
11733 DW_UNSND (attr) = 1;
11734 break;
11735 case DW_FORM_sdata:
11736 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11737 info_ptr += bytes_read;
11738 break;
11739 case DW_FORM_udata:
11740 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11741 info_ptr += bytes_read;
11742 break;
11743 case DW_FORM_ref1:
11744 DW_UNSND (attr) = (cu->header.offset.sect_off
11745 + read_1_byte (abfd, info_ptr));
11746 info_ptr += 1;
11747 break;
11748 case DW_FORM_ref2:
11749 DW_UNSND (attr) = (cu->header.offset.sect_off
11750 + read_2_bytes (abfd, info_ptr));
11751 info_ptr += 2;
11752 break;
11753 case DW_FORM_ref4:
11754 DW_UNSND (attr) = (cu->header.offset.sect_off
11755 + read_4_bytes (abfd, info_ptr));
11756 info_ptr += 4;
11757 break;
11758 case DW_FORM_ref8:
11759 DW_UNSND (attr) = (cu->header.offset.sect_off
11760 + read_8_bytes (abfd, info_ptr));
11761 info_ptr += 8;
11762 break;
11763 case DW_FORM_ref_sig8:
11764 /* Convert the signature to something we can record in DW_UNSND
11765 for later lookup.
11766 NOTE: This is NULL if the type wasn't found. */
11767 DW_SIGNATURED_TYPE (attr) =
11768 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11769 info_ptr += 8;
11770 break;
11771 case DW_FORM_ref_udata:
11772 DW_UNSND (attr) = (cu->header.offset.sect_off
11773 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11774 info_ptr += bytes_read;
11775 break;
11776 case DW_FORM_indirect:
11777 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11778 info_ptr += bytes_read;
11779 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11780 break;
11781 case DW_FORM_GNU_addr_index:
11782 if (reader->dwo_file == NULL)
11783 {
11784 /* For now flag a hard error.
11785 Later we can turn this into a complaint. */
11786 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11787 dwarf_form_name (form),
11788 bfd_get_filename (abfd));
11789 }
11790 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11791 info_ptr += bytes_read;
11792 break;
11793 case DW_FORM_GNU_str_index:
11794 if (reader->dwo_file == NULL)
11795 {
11796 /* For now flag a hard error.
11797 Later we can turn this into a complaint if warranted. */
11798 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11799 dwarf_form_name (form),
11800 bfd_get_filename (abfd));
11801 }
11802 {
11803 ULONGEST str_index =
11804 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11805
11806 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11807 DW_STRING_IS_CANONICAL (attr) = 0;
11808 info_ptr += bytes_read;
11809 }
11810 break;
11811 default:
11812 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11813 dwarf_form_name (form),
11814 bfd_get_filename (abfd));
11815 }
11816
11817 /* We have seen instances where the compiler tried to emit a byte
11818 size attribute of -1 which ended up being encoded as an unsigned
11819 0xffffffff. Although 0xffffffff is technically a valid size value,
11820 an object of this size seems pretty unlikely so we can relatively
11821 safely treat these cases as if the size attribute was invalid and
11822 treat them as zero by default. */
11823 if (attr->name == DW_AT_byte_size
11824 && form == DW_FORM_data4
11825 && DW_UNSND (attr) >= 0xffffffff)
11826 {
11827 complaint
11828 (&symfile_complaints,
11829 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11830 hex_string (DW_UNSND (attr)));
11831 DW_UNSND (attr) = 0;
11832 }
11833
11834 return info_ptr;
11835 }
11836
11837 /* Read an attribute described by an abbreviated attribute. */
11838
11839 static gdb_byte *
11840 read_attribute (const struct die_reader_specs *reader,
11841 struct attribute *attr, struct attr_abbrev *abbrev,
11842 gdb_byte *info_ptr)
11843 {
11844 attr->name = abbrev->name;
11845 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11846 }
11847
11848 /* Read dwarf information from a buffer. */
11849
11850 static unsigned int
11851 read_1_byte (bfd *abfd, gdb_byte *buf)
11852 {
11853 return bfd_get_8 (abfd, buf);
11854 }
11855
11856 static int
11857 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11858 {
11859 return bfd_get_signed_8 (abfd, buf);
11860 }
11861
11862 static unsigned int
11863 read_2_bytes (bfd *abfd, gdb_byte *buf)
11864 {
11865 return bfd_get_16 (abfd, buf);
11866 }
11867
11868 static int
11869 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11870 {
11871 return bfd_get_signed_16 (abfd, buf);
11872 }
11873
11874 static unsigned int
11875 read_4_bytes (bfd *abfd, gdb_byte *buf)
11876 {
11877 return bfd_get_32 (abfd, buf);
11878 }
11879
11880 static int
11881 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11882 {
11883 return bfd_get_signed_32 (abfd, buf);
11884 }
11885
11886 static ULONGEST
11887 read_8_bytes (bfd *abfd, gdb_byte *buf)
11888 {
11889 return bfd_get_64 (abfd, buf);
11890 }
11891
11892 static CORE_ADDR
11893 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11894 unsigned int *bytes_read)
11895 {
11896 struct comp_unit_head *cu_header = &cu->header;
11897 CORE_ADDR retval = 0;
11898
11899 if (cu_header->signed_addr_p)
11900 {
11901 switch (cu_header->addr_size)
11902 {
11903 case 2:
11904 retval = bfd_get_signed_16 (abfd, buf);
11905 break;
11906 case 4:
11907 retval = bfd_get_signed_32 (abfd, buf);
11908 break;
11909 case 8:
11910 retval = bfd_get_signed_64 (abfd, buf);
11911 break;
11912 default:
11913 internal_error (__FILE__, __LINE__,
11914 _("read_address: bad switch, signed [in module %s]"),
11915 bfd_get_filename (abfd));
11916 }
11917 }
11918 else
11919 {
11920 switch (cu_header->addr_size)
11921 {
11922 case 2:
11923 retval = bfd_get_16 (abfd, buf);
11924 break;
11925 case 4:
11926 retval = bfd_get_32 (abfd, buf);
11927 break;
11928 case 8:
11929 retval = bfd_get_64 (abfd, buf);
11930 break;
11931 default:
11932 internal_error (__FILE__, __LINE__,
11933 _("read_address: bad switch, "
11934 "unsigned [in module %s]"),
11935 bfd_get_filename (abfd));
11936 }
11937 }
11938
11939 *bytes_read = cu_header->addr_size;
11940 return retval;
11941 }
11942
11943 /* Read the initial length from a section. The (draft) DWARF 3
11944 specification allows the initial length to take up either 4 bytes
11945 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11946 bytes describe the length and all offsets will be 8 bytes in length
11947 instead of 4.
11948
11949 An older, non-standard 64-bit format is also handled by this
11950 function. The older format in question stores the initial length
11951 as an 8-byte quantity without an escape value. Lengths greater
11952 than 2^32 aren't very common which means that the initial 4 bytes
11953 is almost always zero. Since a length value of zero doesn't make
11954 sense for the 32-bit format, this initial zero can be considered to
11955 be an escape value which indicates the presence of the older 64-bit
11956 format. As written, the code can't detect (old format) lengths
11957 greater than 4GB. If it becomes necessary to handle lengths
11958 somewhat larger than 4GB, we could allow other small values (such
11959 as the non-sensical values of 1, 2, and 3) to also be used as
11960 escape values indicating the presence of the old format.
11961
11962 The value returned via bytes_read should be used to increment the
11963 relevant pointer after calling read_initial_length().
11964
11965 [ Note: read_initial_length() and read_offset() are based on the
11966 document entitled "DWARF Debugging Information Format", revision
11967 3, draft 8, dated November 19, 2001. This document was obtained
11968 from:
11969
11970 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11971
11972 This document is only a draft and is subject to change. (So beware.)
11973
11974 Details regarding the older, non-standard 64-bit format were
11975 determined empirically by examining 64-bit ELF files produced by
11976 the SGI toolchain on an IRIX 6.5 machine.
11977
11978 - Kevin, July 16, 2002
11979 ] */
11980
11981 static LONGEST
11982 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11983 {
11984 LONGEST length = bfd_get_32 (abfd, buf);
11985
11986 if (length == 0xffffffff)
11987 {
11988 length = bfd_get_64 (abfd, buf + 4);
11989 *bytes_read = 12;
11990 }
11991 else if (length == 0)
11992 {
11993 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11994 length = bfd_get_64 (abfd, buf);
11995 *bytes_read = 8;
11996 }
11997 else
11998 {
11999 *bytes_read = 4;
12000 }
12001
12002 return length;
12003 }
12004
12005 /* Cover function for read_initial_length.
12006 Returns the length of the object at BUF, and stores the size of the
12007 initial length in *BYTES_READ and stores the size that offsets will be in
12008 *OFFSET_SIZE.
12009 If the initial length size is not equivalent to that specified in
12010 CU_HEADER then issue a complaint.
12011 This is useful when reading non-comp-unit headers. */
12012
12013 static LONGEST
12014 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12015 const struct comp_unit_head *cu_header,
12016 unsigned int *bytes_read,
12017 unsigned int *offset_size)
12018 {
12019 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12020
12021 gdb_assert (cu_header->initial_length_size == 4
12022 || cu_header->initial_length_size == 8
12023 || cu_header->initial_length_size == 12);
12024
12025 if (cu_header->initial_length_size != *bytes_read)
12026 complaint (&symfile_complaints,
12027 _("intermixed 32-bit and 64-bit DWARF sections"));
12028
12029 *offset_size = (*bytes_read == 4) ? 4 : 8;
12030 return length;
12031 }
12032
12033 /* Read an offset from the data stream. The size of the offset is
12034 given by cu_header->offset_size. */
12035
12036 static LONGEST
12037 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12038 unsigned int *bytes_read)
12039 {
12040 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12041
12042 *bytes_read = cu_header->offset_size;
12043 return offset;
12044 }
12045
12046 /* Read an offset from the data stream. */
12047
12048 static LONGEST
12049 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12050 {
12051 LONGEST retval = 0;
12052
12053 switch (offset_size)
12054 {
12055 case 4:
12056 retval = bfd_get_32 (abfd, buf);
12057 break;
12058 case 8:
12059 retval = bfd_get_64 (abfd, buf);
12060 break;
12061 default:
12062 internal_error (__FILE__, __LINE__,
12063 _("read_offset_1: bad switch [in module %s]"),
12064 bfd_get_filename (abfd));
12065 }
12066
12067 return retval;
12068 }
12069
12070 static gdb_byte *
12071 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12072 {
12073 /* If the size of a host char is 8 bits, we can return a pointer
12074 to the buffer, otherwise we have to copy the data to a buffer
12075 allocated on the temporary obstack. */
12076 gdb_assert (HOST_CHAR_BIT == 8);
12077 return buf;
12078 }
12079
12080 static char *
12081 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12082 {
12083 /* If the size of a host char is 8 bits, we can return a pointer
12084 to the string, otherwise we have to copy the string to a buffer
12085 allocated on the temporary obstack. */
12086 gdb_assert (HOST_CHAR_BIT == 8);
12087 if (*buf == '\0')
12088 {
12089 *bytes_read_ptr = 1;
12090 return NULL;
12091 }
12092 *bytes_read_ptr = strlen ((char *) buf) + 1;
12093 return (char *) buf;
12094 }
12095
12096 static char *
12097 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12098 {
12099 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12100 if (dwarf2_per_objfile->str.buffer == NULL)
12101 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12102 bfd_get_filename (abfd));
12103 if (str_offset >= dwarf2_per_objfile->str.size)
12104 error (_("DW_FORM_strp pointing outside of "
12105 ".debug_str section [in module %s]"),
12106 bfd_get_filename (abfd));
12107 gdb_assert (HOST_CHAR_BIT == 8);
12108 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12109 return NULL;
12110 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12111 }
12112
12113 static char *
12114 read_indirect_string (bfd *abfd, gdb_byte *buf,
12115 const struct comp_unit_head *cu_header,
12116 unsigned int *bytes_read_ptr)
12117 {
12118 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12119
12120 return read_indirect_string_at_offset (abfd, str_offset);
12121 }
12122
12123 static ULONGEST
12124 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12125 {
12126 ULONGEST result;
12127 unsigned int num_read;
12128 int i, shift;
12129 unsigned char byte;
12130
12131 result = 0;
12132 shift = 0;
12133 num_read = 0;
12134 i = 0;
12135 while (1)
12136 {
12137 byte = bfd_get_8 (abfd, buf);
12138 buf++;
12139 num_read++;
12140 result |= ((ULONGEST) (byte & 127) << shift);
12141 if ((byte & 128) == 0)
12142 {
12143 break;
12144 }
12145 shift += 7;
12146 }
12147 *bytes_read_ptr = num_read;
12148 return result;
12149 }
12150
12151 static LONGEST
12152 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12153 {
12154 LONGEST result;
12155 int i, shift, num_read;
12156 unsigned char byte;
12157
12158 result = 0;
12159 shift = 0;
12160 num_read = 0;
12161 i = 0;
12162 while (1)
12163 {
12164 byte = bfd_get_8 (abfd, buf);
12165 buf++;
12166 num_read++;
12167 result |= ((LONGEST) (byte & 127) << shift);
12168 shift += 7;
12169 if ((byte & 128) == 0)
12170 {
12171 break;
12172 }
12173 }
12174 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12175 result |= -(((LONGEST) 1) << shift);
12176 *bytes_read_ptr = num_read;
12177 return result;
12178 }
12179
12180 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12181 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12182 ADDR_SIZE is the size of addresses from the CU header. */
12183
12184 static CORE_ADDR
12185 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12186 {
12187 struct objfile *objfile = dwarf2_per_objfile->objfile;
12188 bfd *abfd = objfile->obfd;
12189 const gdb_byte *info_ptr;
12190
12191 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12192 if (dwarf2_per_objfile->addr.buffer == NULL)
12193 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12194 objfile->name);
12195 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12196 error (_("DW_FORM_addr_index pointing outside of "
12197 ".debug_addr section [in module %s]"),
12198 objfile->name);
12199 info_ptr = (dwarf2_per_objfile->addr.buffer
12200 + addr_base + addr_index * addr_size);
12201 if (addr_size == 4)
12202 return bfd_get_32 (abfd, info_ptr);
12203 else
12204 return bfd_get_64 (abfd, info_ptr);
12205 }
12206
12207 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12208
12209 static CORE_ADDR
12210 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12211 {
12212 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12213 }
12214
12215 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12216
12217 static CORE_ADDR
12218 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12219 unsigned int *bytes_read)
12220 {
12221 bfd *abfd = cu->objfile->obfd;
12222 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12223
12224 return read_addr_index (cu, addr_index);
12225 }
12226
12227 /* Data structure to pass results from dwarf2_read_addr_index_reader
12228 back to dwarf2_read_addr_index. */
12229
12230 struct dwarf2_read_addr_index_data
12231 {
12232 ULONGEST addr_base;
12233 int addr_size;
12234 };
12235
12236 /* die_reader_func for dwarf2_read_addr_index. */
12237
12238 static void
12239 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12240 gdb_byte *info_ptr,
12241 struct die_info *comp_unit_die,
12242 int has_children,
12243 void *data)
12244 {
12245 struct dwarf2_cu *cu = reader->cu;
12246 struct dwarf2_read_addr_index_data *aidata =
12247 (struct dwarf2_read_addr_index_data *) data;
12248
12249 aidata->addr_base = cu->addr_base;
12250 aidata->addr_size = cu->header.addr_size;
12251 }
12252
12253 /* Given an index in .debug_addr, fetch the value.
12254 NOTE: This can be called during dwarf expression evaluation,
12255 long after the debug information has been read, and thus per_cu->cu
12256 may no longer exist. */
12257
12258 CORE_ADDR
12259 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12260 unsigned int addr_index)
12261 {
12262 struct objfile *objfile = per_cu->objfile;
12263 struct dwarf2_cu *cu = per_cu->cu;
12264 ULONGEST addr_base;
12265 int addr_size;
12266
12267 /* This is intended to be called from outside this file. */
12268 dw2_setup (objfile);
12269
12270 /* We need addr_base and addr_size.
12271 If we don't have PER_CU->cu, we have to get it.
12272 Nasty, but the alternative is storing the needed info in PER_CU,
12273 which at this point doesn't seem justified: it's not clear how frequently
12274 it would get used and it would increase the size of every PER_CU.
12275 Entry points like dwarf2_per_cu_addr_size do a similar thing
12276 so we're not in uncharted territory here.
12277 Alas we need to be a bit more complicated as addr_base is contained
12278 in the DIE.
12279
12280 We don't need to read the entire CU(/TU).
12281 We just need the header and top level die.
12282 IWBN to use the aging mechanism to let us lazily later discard the CU.
12283 See however init_cutu_and_read_dies_simple. */
12284
12285 if (cu != NULL)
12286 {
12287 addr_base = cu->addr_base;
12288 addr_size = cu->header.addr_size;
12289 }
12290 else
12291 {
12292 struct dwarf2_read_addr_index_data aidata;
12293
12294 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12295 &aidata);
12296 addr_base = aidata.addr_base;
12297 addr_size = aidata.addr_size;
12298 }
12299
12300 return read_addr_index_1 (addr_index, addr_base, addr_size);
12301 }
12302
12303 /* Given a DW_AT_str_index, fetch the string. */
12304
12305 static char *
12306 read_str_index (const struct die_reader_specs *reader,
12307 struct dwarf2_cu *cu, ULONGEST str_index)
12308 {
12309 struct objfile *objfile = dwarf2_per_objfile->objfile;
12310 const char *dwo_name = objfile->name;
12311 bfd *abfd = objfile->obfd;
12312 struct dwo_sections *sections = &reader->dwo_file->sections;
12313 gdb_byte *info_ptr;
12314 ULONGEST str_offset;
12315
12316 dwarf2_read_section (objfile, &sections->str);
12317 dwarf2_read_section (objfile, &sections->str_offsets);
12318 if (sections->str.buffer == NULL)
12319 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12320 " in CU at offset 0x%lx [in module %s]"),
12321 (long) cu->header.offset.sect_off, dwo_name);
12322 if (sections->str_offsets.buffer == NULL)
12323 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12324 " in CU at offset 0x%lx [in module %s]"),
12325 (long) cu->header.offset.sect_off, dwo_name);
12326 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12327 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12328 " section in CU at offset 0x%lx [in module %s]"),
12329 (long) cu->header.offset.sect_off, dwo_name);
12330 info_ptr = (sections->str_offsets.buffer
12331 + str_index * cu->header.offset_size);
12332 if (cu->header.offset_size == 4)
12333 str_offset = bfd_get_32 (abfd, info_ptr);
12334 else
12335 str_offset = bfd_get_64 (abfd, info_ptr);
12336 if (str_offset >= sections->str.size)
12337 error (_("Offset from DW_FORM_str_index pointing outside of"
12338 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12339 (long) cu->header.offset.sect_off, dwo_name);
12340 return (char *) (sections->str.buffer + str_offset);
12341 }
12342
12343 /* Return the length of an LEB128 number in BUF. */
12344
12345 static int
12346 leb128_size (const gdb_byte *buf)
12347 {
12348 const gdb_byte *begin = buf;
12349 gdb_byte byte;
12350
12351 while (1)
12352 {
12353 byte = *buf++;
12354 if ((byte & 128) == 0)
12355 return buf - begin;
12356 }
12357 }
12358
12359 static void
12360 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12361 {
12362 switch (lang)
12363 {
12364 case DW_LANG_C89:
12365 case DW_LANG_C99:
12366 case DW_LANG_C:
12367 cu->language = language_c;
12368 break;
12369 case DW_LANG_C_plus_plus:
12370 cu->language = language_cplus;
12371 break;
12372 case DW_LANG_D:
12373 cu->language = language_d;
12374 break;
12375 case DW_LANG_Fortran77:
12376 case DW_LANG_Fortran90:
12377 case DW_LANG_Fortran95:
12378 cu->language = language_fortran;
12379 break;
12380 case DW_LANG_Go:
12381 cu->language = language_go;
12382 break;
12383 case DW_LANG_Mips_Assembler:
12384 cu->language = language_asm;
12385 break;
12386 case DW_LANG_Java:
12387 cu->language = language_java;
12388 break;
12389 case DW_LANG_Ada83:
12390 case DW_LANG_Ada95:
12391 cu->language = language_ada;
12392 break;
12393 case DW_LANG_Modula2:
12394 cu->language = language_m2;
12395 break;
12396 case DW_LANG_Pascal83:
12397 cu->language = language_pascal;
12398 break;
12399 case DW_LANG_ObjC:
12400 cu->language = language_objc;
12401 break;
12402 case DW_LANG_Cobol74:
12403 case DW_LANG_Cobol85:
12404 default:
12405 cu->language = language_minimal;
12406 break;
12407 }
12408 cu->language_defn = language_def (cu->language);
12409 }
12410
12411 /* Return the named attribute or NULL if not there. */
12412
12413 static struct attribute *
12414 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12415 {
12416 for (;;)
12417 {
12418 unsigned int i;
12419 struct attribute *spec = NULL;
12420
12421 for (i = 0; i < die->num_attrs; ++i)
12422 {
12423 if (die->attrs[i].name == name)
12424 return &die->attrs[i];
12425 if (die->attrs[i].name == DW_AT_specification
12426 || die->attrs[i].name == DW_AT_abstract_origin)
12427 spec = &die->attrs[i];
12428 }
12429
12430 if (!spec)
12431 break;
12432
12433 die = follow_die_ref (die, spec, &cu);
12434 }
12435
12436 return NULL;
12437 }
12438
12439 /* Return the named attribute or NULL if not there,
12440 but do not follow DW_AT_specification, etc.
12441 This is for use in contexts where we're reading .debug_types dies.
12442 Following DW_AT_specification, DW_AT_abstract_origin will take us
12443 back up the chain, and we want to go down. */
12444
12445 static struct attribute *
12446 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12447 struct dwarf2_cu *cu)
12448 {
12449 unsigned int i;
12450
12451 for (i = 0; i < die->num_attrs; ++i)
12452 if (die->attrs[i].name == name)
12453 return &die->attrs[i];
12454
12455 return NULL;
12456 }
12457
12458 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12459 and holds a non-zero value. This function should only be used for
12460 DW_FORM_flag or DW_FORM_flag_present attributes. */
12461
12462 static int
12463 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12464 {
12465 struct attribute *attr = dwarf2_attr (die, name, cu);
12466
12467 return (attr && DW_UNSND (attr));
12468 }
12469
12470 static int
12471 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12472 {
12473 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12474 which value is non-zero. However, we have to be careful with
12475 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12476 (via dwarf2_flag_true_p) follows this attribute. So we may
12477 end up accidently finding a declaration attribute that belongs
12478 to a different DIE referenced by the specification attribute,
12479 even though the given DIE does not have a declaration attribute. */
12480 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12481 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12482 }
12483
12484 /* Return the die giving the specification for DIE, if there is
12485 one. *SPEC_CU is the CU containing DIE on input, and the CU
12486 containing the return value on output. If there is no
12487 specification, but there is an abstract origin, that is
12488 returned. */
12489
12490 static struct die_info *
12491 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12492 {
12493 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12494 *spec_cu);
12495
12496 if (spec_attr == NULL)
12497 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12498
12499 if (spec_attr == NULL)
12500 return NULL;
12501 else
12502 return follow_die_ref (die, spec_attr, spec_cu);
12503 }
12504
12505 /* Free the line_header structure *LH, and any arrays and strings it
12506 refers to.
12507 NOTE: This is also used as a "cleanup" function. */
12508
12509 static void
12510 free_line_header (struct line_header *lh)
12511 {
12512 if (lh->standard_opcode_lengths)
12513 xfree (lh->standard_opcode_lengths);
12514
12515 /* Remember that all the lh->file_names[i].name pointers are
12516 pointers into debug_line_buffer, and don't need to be freed. */
12517 if (lh->file_names)
12518 xfree (lh->file_names);
12519
12520 /* Similarly for the include directory names. */
12521 if (lh->include_dirs)
12522 xfree (lh->include_dirs);
12523
12524 xfree (lh);
12525 }
12526
12527 /* Add an entry to LH's include directory table. */
12528
12529 static void
12530 add_include_dir (struct line_header *lh, char *include_dir)
12531 {
12532 /* Grow the array if necessary. */
12533 if (lh->include_dirs_size == 0)
12534 {
12535 lh->include_dirs_size = 1; /* for testing */
12536 lh->include_dirs = xmalloc (lh->include_dirs_size
12537 * sizeof (*lh->include_dirs));
12538 }
12539 else if (lh->num_include_dirs >= lh->include_dirs_size)
12540 {
12541 lh->include_dirs_size *= 2;
12542 lh->include_dirs = xrealloc (lh->include_dirs,
12543 (lh->include_dirs_size
12544 * sizeof (*lh->include_dirs)));
12545 }
12546
12547 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12548 }
12549
12550 /* Add an entry to LH's file name table. */
12551
12552 static void
12553 add_file_name (struct line_header *lh,
12554 char *name,
12555 unsigned int dir_index,
12556 unsigned int mod_time,
12557 unsigned int length)
12558 {
12559 struct file_entry *fe;
12560
12561 /* Grow the array if necessary. */
12562 if (lh->file_names_size == 0)
12563 {
12564 lh->file_names_size = 1; /* for testing */
12565 lh->file_names = xmalloc (lh->file_names_size
12566 * sizeof (*lh->file_names));
12567 }
12568 else if (lh->num_file_names >= lh->file_names_size)
12569 {
12570 lh->file_names_size *= 2;
12571 lh->file_names = xrealloc (lh->file_names,
12572 (lh->file_names_size
12573 * sizeof (*lh->file_names)));
12574 }
12575
12576 fe = &lh->file_names[lh->num_file_names++];
12577 fe->name = name;
12578 fe->dir_index = dir_index;
12579 fe->mod_time = mod_time;
12580 fe->length = length;
12581 fe->included_p = 0;
12582 fe->symtab = NULL;
12583 }
12584
12585 /* Read the statement program header starting at OFFSET in
12586 .debug_line, or .debug_line.dwo. Return a pointer
12587 to a struct line_header, allocated using xmalloc.
12588
12589 NOTE: the strings in the include directory and file name tables of
12590 the returned object point into the dwarf line section buffer,
12591 and must not be freed. */
12592
12593 static struct line_header *
12594 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12595 {
12596 struct cleanup *back_to;
12597 struct line_header *lh;
12598 gdb_byte *line_ptr;
12599 unsigned int bytes_read, offset_size;
12600 int i;
12601 char *cur_dir, *cur_file;
12602 struct dwarf2_section_info *section;
12603 bfd *abfd;
12604
12605 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12606 DWO file. */
12607 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12608 section = &cu->dwo_unit->dwo_file->sections.line;
12609 else
12610 section = &dwarf2_per_objfile->line;
12611
12612 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12613 if (section->buffer == NULL)
12614 {
12615 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12616 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12617 else
12618 complaint (&symfile_complaints, _("missing .debug_line section"));
12619 return 0;
12620 }
12621
12622 /* We can't do this until we know the section is non-empty.
12623 Only then do we know we have such a section. */
12624 abfd = section->asection->owner;
12625
12626 /* Make sure that at least there's room for the total_length field.
12627 That could be 12 bytes long, but we're just going to fudge that. */
12628 if (offset + 4 >= section->size)
12629 {
12630 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12631 return 0;
12632 }
12633
12634 lh = xmalloc (sizeof (*lh));
12635 memset (lh, 0, sizeof (*lh));
12636 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12637 (void *) lh);
12638
12639 line_ptr = section->buffer + offset;
12640
12641 /* Read in the header. */
12642 lh->total_length =
12643 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12644 &bytes_read, &offset_size);
12645 line_ptr += bytes_read;
12646 if (line_ptr + lh->total_length > (section->buffer + section->size))
12647 {
12648 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12649 return 0;
12650 }
12651 lh->statement_program_end = line_ptr + lh->total_length;
12652 lh->version = read_2_bytes (abfd, line_ptr);
12653 line_ptr += 2;
12654 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12655 line_ptr += offset_size;
12656 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12657 line_ptr += 1;
12658 if (lh->version >= 4)
12659 {
12660 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12661 line_ptr += 1;
12662 }
12663 else
12664 lh->maximum_ops_per_instruction = 1;
12665
12666 if (lh->maximum_ops_per_instruction == 0)
12667 {
12668 lh->maximum_ops_per_instruction = 1;
12669 complaint (&symfile_complaints,
12670 _("invalid maximum_ops_per_instruction "
12671 "in `.debug_line' section"));
12672 }
12673
12674 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12675 line_ptr += 1;
12676 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12677 line_ptr += 1;
12678 lh->line_range = read_1_byte (abfd, line_ptr);
12679 line_ptr += 1;
12680 lh->opcode_base = read_1_byte (abfd, line_ptr);
12681 line_ptr += 1;
12682 lh->standard_opcode_lengths
12683 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12684
12685 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12686 for (i = 1; i < lh->opcode_base; ++i)
12687 {
12688 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12689 line_ptr += 1;
12690 }
12691
12692 /* Read directory table. */
12693 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12694 {
12695 line_ptr += bytes_read;
12696 add_include_dir (lh, cur_dir);
12697 }
12698 line_ptr += bytes_read;
12699
12700 /* Read file name table. */
12701 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12702 {
12703 unsigned int dir_index, mod_time, length;
12704
12705 line_ptr += bytes_read;
12706 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12707 line_ptr += bytes_read;
12708 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12709 line_ptr += bytes_read;
12710 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12711 line_ptr += bytes_read;
12712
12713 add_file_name (lh, cur_file, dir_index, mod_time, length);
12714 }
12715 line_ptr += bytes_read;
12716 lh->statement_program_start = line_ptr;
12717
12718 if (line_ptr > (section->buffer + section->size))
12719 complaint (&symfile_complaints,
12720 _("line number info header doesn't "
12721 "fit in `.debug_line' section"));
12722
12723 discard_cleanups (back_to);
12724 return lh;
12725 }
12726
12727 /* Subroutine of dwarf_decode_lines to simplify it.
12728 Return the file name of the psymtab for included file FILE_INDEX
12729 in line header LH of PST.
12730 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12731 If space for the result is malloc'd, it will be freed by a cleanup.
12732 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12733
12734 static char *
12735 psymtab_include_file_name (const struct line_header *lh, int file_index,
12736 const struct partial_symtab *pst,
12737 const char *comp_dir)
12738 {
12739 const struct file_entry fe = lh->file_names [file_index];
12740 char *include_name = fe.name;
12741 char *include_name_to_compare = include_name;
12742 char *dir_name = NULL;
12743 const char *pst_filename;
12744 char *copied_name = NULL;
12745 int file_is_pst;
12746
12747 if (fe.dir_index)
12748 dir_name = lh->include_dirs[fe.dir_index - 1];
12749
12750 if (!IS_ABSOLUTE_PATH (include_name)
12751 && (dir_name != NULL || comp_dir != NULL))
12752 {
12753 /* Avoid creating a duplicate psymtab for PST.
12754 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12755 Before we do the comparison, however, we need to account
12756 for DIR_NAME and COMP_DIR.
12757 First prepend dir_name (if non-NULL). If we still don't
12758 have an absolute path prepend comp_dir (if non-NULL).
12759 However, the directory we record in the include-file's
12760 psymtab does not contain COMP_DIR (to match the
12761 corresponding symtab(s)).
12762
12763 Example:
12764
12765 bash$ cd /tmp
12766 bash$ gcc -g ./hello.c
12767 include_name = "hello.c"
12768 dir_name = "."
12769 DW_AT_comp_dir = comp_dir = "/tmp"
12770 DW_AT_name = "./hello.c" */
12771
12772 if (dir_name != NULL)
12773 {
12774 include_name = concat (dir_name, SLASH_STRING,
12775 include_name, (char *)NULL);
12776 include_name_to_compare = include_name;
12777 make_cleanup (xfree, include_name);
12778 }
12779 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12780 {
12781 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12782 include_name, (char *)NULL);
12783 }
12784 }
12785
12786 pst_filename = pst->filename;
12787 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12788 {
12789 copied_name = concat (pst->dirname, SLASH_STRING,
12790 pst_filename, (char *)NULL);
12791 pst_filename = copied_name;
12792 }
12793
12794 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12795
12796 if (include_name_to_compare != include_name)
12797 xfree (include_name_to_compare);
12798 if (copied_name != NULL)
12799 xfree (copied_name);
12800
12801 if (file_is_pst)
12802 return NULL;
12803 return include_name;
12804 }
12805
12806 /* Ignore this record_line request. */
12807
12808 static void
12809 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12810 {
12811 return;
12812 }
12813
12814 /* Subroutine of dwarf_decode_lines to simplify it.
12815 Process the line number information in LH. */
12816
12817 static void
12818 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12819 struct dwarf2_cu *cu, struct partial_symtab *pst)
12820 {
12821 gdb_byte *line_ptr, *extended_end;
12822 gdb_byte *line_end;
12823 unsigned int bytes_read, extended_len;
12824 unsigned char op_code, extended_op, adj_opcode;
12825 CORE_ADDR baseaddr;
12826 struct objfile *objfile = cu->objfile;
12827 bfd *abfd = objfile->obfd;
12828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12829 const int decode_for_pst_p = (pst != NULL);
12830 struct subfile *last_subfile = NULL;
12831 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12832 = record_line;
12833
12834 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12835
12836 line_ptr = lh->statement_program_start;
12837 line_end = lh->statement_program_end;
12838
12839 /* Read the statement sequences until there's nothing left. */
12840 while (line_ptr < line_end)
12841 {
12842 /* state machine registers */
12843 CORE_ADDR address = 0;
12844 unsigned int file = 1;
12845 unsigned int line = 1;
12846 unsigned int column = 0;
12847 int is_stmt = lh->default_is_stmt;
12848 int basic_block = 0;
12849 int end_sequence = 0;
12850 CORE_ADDR addr;
12851 unsigned char op_index = 0;
12852
12853 if (!decode_for_pst_p && lh->num_file_names >= file)
12854 {
12855 /* Start a subfile for the current file of the state machine. */
12856 /* lh->include_dirs and lh->file_names are 0-based, but the
12857 directory and file name numbers in the statement program
12858 are 1-based. */
12859 struct file_entry *fe = &lh->file_names[file - 1];
12860 char *dir = NULL;
12861
12862 if (fe->dir_index)
12863 dir = lh->include_dirs[fe->dir_index - 1];
12864
12865 dwarf2_start_subfile (fe->name, dir, comp_dir);
12866 }
12867
12868 /* Decode the table. */
12869 while (!end_sequence)
12870 {
12871 op_code = read_1_byte (abfd, line_ptr);
12872 line_ptr += 1;
12873 if (line_ptr > line_end)
12874 {
12875 dwarf2_debug_line_missing_end_sequence_complaint ();
12876 break;
12877 }
12878
12879 if (op_code >= lh->opcode_base)
12880 {
12881 /* Special operand. */
12882 adj_opcode = op_code - lh->opcode_base;
12883 address += (((op_index + (adj_opcode / lh->line_range))
12884 / lh->maximum_ops_per_instruction)
12885 * lh->minimum_instruction_length);
12886 op_index = ((op_index + (adj_opcode / lh->line_range))
12887 % lh->maximum_ops_per_instruction);
12888 line += lh->line_base + (adj_opcode % lh->line_range);
12889 if (lh->num_file_names < file || file == 0)
12890 dwarf2_debug_line_missing_file_complaint ();
12891 /* For now we ignore lines not starting on an
12892 instruction boundary. */
12893 else if (op_index == 0)
12894 {
12895 lh->file_names[file - 1].included_p = 1;
12896 if (!decode_for_pst_p && is_stmt)
12897 {
12898 if (last_subfile != current_subfile)
12899 {
12900 addr = gdbarch_addr_bits_remove (gdbarch, address);
12901 if (last_subfile)
12902 (*p_record_line) (last_subfile, 0, addr);
12903 last_subfile = current_subfile;
12904 }
12905 /* Append row to matrix using current values. */
12906 addr = gdbarch_addr_bits_remove (gdbarch, address);
12907 (*p_record_line) (current_subfile, line, addr);
12908 }
12909 }
12910 basic_block = 0;
12911 }
12912 else switch (op_code)
12913 {
12914 case DW_LNS_extended_op:
12915 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12916 &bytes_read);
12917 line_ptr += bytes_read;
12918 extended_end = line_ptr + extended_len;
12919 extended_op = read_1_byte (abfd, line_ptr);
12920 line_ptr += 1;
12921 switch (extended_op)
12922 {
12923 case DW_LNE_end_sequence:
12924 p_record_line = record_line;
12925 end_sequence = 1;
12926 break;
12927 case DW_LNE_set_address:
12928 address = read_address (abfd, line_ptr, cu, &bytes_read);
12929
12930 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12931 {
12932 /* This line table is for a function which has been
12933 GCd by the linker. Ignore it. PR gdb/12528 */
12934
12935 long line_offset
12936 = line_ptr - dwarf2_per_objfile->line.buffer;
12937
12938 complaint (&symfile_complaints,
12939 _(".debug_line address at offset 0x%lx is 0 "
12940 "[in module %s]"),
12941 line_offset, objfile->name);
12942 p_record_line = noop_record_line;
12943 }
12944
12945 op_index = 0;
12946 line_ptr += bytes_read;
12947 address += baseaddr;
12948 break;
12949 case DW_LNE_define_file:
12950 {
12951 char *cur_file;
12952 unsigned int dir_index, mod_time, length;
12953
12954 cur_file = read_direct_string (abfd, line_ptr,
12955 &bytes_read);
12956 line_ptr += bytes_read;
12957 dir_index =
12958 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12959 line_ptr += bytes_read;
12960 mod_time =
12961 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12962 line_ptr += bytes_read;
12963 length =
12964 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12965 line_ptr += bytes_read;
12966 add_file_name (lh, cur_file, dir_index, mod_time, length);
12967 }
12968 break;
12969 case DW_LNE_set_discriminator:
12970 /* The discriminator is not interesting to the debugger;
12971 just ignore it. */
12972 line_ptr = extended_end;
12973 break;
12974 default:
12975 complaint (&symfile_complaints,
12976 _("mangled .debug_line section"));
12977 return;
12978 }
12979 /* Make sure that we parsed the extended op correctly. If e.g.
12980 we expected a different address size than the producer used,
12981 we may have read the wrong number of bytes. */
12982 if (line_ptr != extended_end)
12983 {
12984 complaint (&symfile_complaints,
12985 _("mangled .debug_line section"));
12986 return;
12987 }
12988 break;
12989 case DW_LNS_copy:
12990 if (lh->num_file_names < file || file == 0)
12991 dwarf2_debug_line_missing_file_complaint ();
12992 else
12993 {
12994 lh->file_names[file - 1].included_p = 1;
12995 if (!decode_for_pst_p && is_stmt)
12996 {
12997 if (last_subfile != current_subfile)
12998 {
12999 addr = gdbarch_addr_bits_remove (gdbarch, address);
13000 if (last_subfile)
13001 (*p_record_line) (last_subfile, 0, addr);
13002 last_subfile = current_subfile;
13003 }
13004 addr = gdbarch_addr_bits_remove (gdbarch, address);
13005 (*p_record_line) (current_subfile, line, addr);
13006 }
13007 }
13008 basic_block = 0;
13009 break;
13010 case DW_LNS_advance_pc:
13011 {
13012 CORE_ADDR adjust
13013 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13014
13015 address += (((op_index + adjust)
13016 / lh->maximum_ops_per_instruction)
13017 * lh->minimum_instruction_length);
13018 op_index = ((op_index + adjust)
13019 % lh->maximum_ops_per_instruction);
13020 line_ptr += bytes_read;
13021 }
13022 break;
13023 case DW_LNS_advance_line:
13024 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13025 line_ptr += bytes_read;
13026 break;
13027 case DW_LNS_set_file:
13028 {
13029 /* The arrays lh->include_dirs and lh->file_names are
13030 0-based, but the directory and file name numbers in
13031 the statement program are 1-based. */
13032 struct file_entry *fe;
13033 char *dir = NULL;
13034
13035 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13036 line_ptr += bytes_read;
13037 if (lh->num_file_names < file || file == 0)
13038 dwarf2_debug_line_missing_file_complaint ();
13039 else
13040 {
13041 fe = &lh->file_names[file - 1];
13042 if (fe->dir_index)
13043 dir = lh->include_dirs[fe->dir_index - 1];
13044 if (!decode_for_pst_p)
13045 {
13046 last_subfile = current_subfile;
13047 dwarf2_start_subfile (fe->name, dir, comp_dir);
13048 }
13049 }
13050 }
13051 break;
13052 case DW_LNS_set_column:
13053 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13054 line_ptr += bytes_read;
13055 break;
13056 case DW_LNS_negate_stmt:
13057 is_stmt = (!is_stmt);
13058 break;
13059 case DW_LNS_set_basic_block:
13060 basic_block = 1;
13061 break;
13062 /* Add to the address register of the state machine the
13063 address increment value corresponding to special opcode
13064 255. I.e., this value is scaled by the minimum
13065 instruction length since special opcode 255 would have
13066 scaled the increment. */
13067 case DW_LNS_const_add_pc:
13068 {
13069 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13070
13071 address += (((op_index + adjust)
13072 / lh->maximum_ops_per_instruction)
13073 * lh->minimum_instruction_length);
13074 op_index = ((op_index + adjust)
13075 % lh->maximum_ops_per_instruction);
13076 }
13077 break;
13078 case DW_LNS_fixed_advance_pc:
13079 address += read_2_bytes (abfd, line_ptr);
13080 op_index = 0;
13081 line_ptr += 2;
13082 break;
13083 default:
13084 {
13085 /* Unknown standard opcode, ignore it. */
13086 int i;
13087
13088 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13089 {
13090 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13091 line_ptr += bytes_read;
13092 }
13093 }
13094 }
13095 }
13096 if (lh->num_file_names < file || file == 0)
13097 dwarf2_debug_line_missing_file_complaint ();
13098 else
13099 {
13100 lh->file_names[file - 1].included_p = 1;
13101 if (!decode_for_pst_p)
13102 {
13103 addr = gdbarch_addr_bits_remove (gdbarch, address);
13104 (*p_record_line) (current_subfile, 0, addr);
13105 }
13106 }
13107 }
13108 }
13109
13110 /* Decode the Line Number Program (LNP) for the given line_header
13111 structure and CU. The actual information extracted and the type
13112 of structures created from the LNP depends on the value of PST.
13113
13114 1. If PST is NULL, then this procedure uses the data from the program
13115 to create all necessary symbol tables, and their linetables.
13116
13117 2. If PST is not NULL, this procedure reads the program to determine
13118 the list of files included by the unit represented by PST, and
13119 builds all the associated partial symbol tables.
13120
13121 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13122 It is used for relative paths in the line table.
13123 NOTE: When processing partial symtabs (pst != NULL),
13124 comp_dir == pst->dirname.
13125
13126 NOTE: It is important that psymtabs have the same file name (via strcmp)
13127 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13128 symtab we don't use it in the name of the psymtabs we create.
13129 E.g. expand_line_sal requires this when finding psymtabs to expand.
13130 A good testcase for this is mb-inline.exp. */
13131
13132 static void
13133 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13134 struct dwarf2_cu *cu, struct partial_symtab *pst,
13135 int want_line_info)
13136 {
13137 struct objfile *objfile = cu->objfile;
13138 const int decode_for_pst_p = (pst != NULL);
13139 struct subfile *first_subfile = current_subfile;
13140
13141 if (want_line_info)
13142 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13143
13144 if (decode_for_pst_p)
13145 {
13146 int file_index;
13147
13148 /* Now that we're done scanning the Line Header Program, we can
13149 create the psymtab of each included file. */
13150 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13151 if (lh->file_names[file_index].included_p == 1)
13152 {
13153 char *include_name =
13154 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13155 if (include_name != NULL)
13156 dwarf2_create_include_psymtab (include_name, pst, objfile);
13157 }
13158 }
13159 else
13160 {
13161 /* Make sure a symtab is created for every file, even files
13162 which contain only variables (i.e. no code with associated
13163 line numbers). */
13164 int i;
13165
13166 for (i = 0; i < lh->num_file_names; i++)
13167 {
13168 char *dir = NULL;
13169 struct file_entry *fe;
13170
13171 fe = &lh->file_names[i];
13172 if (fe->dir_index)
13173 dir = lh->include_dirs[fe->dir_index - 1];
13174 dwarf2_start_subfile (fe->name, dir, comp_dir);
13175
13176 /* Skip the main file; we don't need it, and it must be
13177 allocated last, so that it will show up before the
13178 non-primary symtabs in the objfile's symtab list. */
13179 if (current_subfile == first_subfile)
13180 continue;
13181
13182 if (current_subfile->symtab == NULL)
13183 current_subfile->symtab = allocate_symtab (current_subfile->name,
13184 objfile);
13185 fe->symtab = current_subfile->symtab;
13186 }
13187 }
13188 }
13189
13190 /* Start a subfile for DWARF. FILENAME is the name of the file and
13191 DIRNAME the name of the source directory which contains FILENAME
13192 or NULL if not known. COMP_DIR is the compilation directory for the
13193 linetable's compilation unit or NULL if not known.
13194 This routine tries to keep line numbers from identical absolute and
13195 relative file names in a common subfile.
13196
13197 Using the `list' example from the GDB testsuite, which resides in
13198 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13199 of /srcdir/list0.c yields the following debugging information for list0.c:
13200
13201 DW_AT_name: /srcdir/list0.c
13202 DW_AT_comp_dir: /compdir
13203 files.files[0].name: list0.h
13204 files.files[0].dir: /srcdir
13205 files.files[1].name: list0.c
13206 files.files[1].dir: /srcdir
13207
13208 The line number information for list0.c has to end up in a single
13209 subfile, so that `break /srcdir/list0.c:1' works as expected.
13210 start_subfile will ensure that this happens provided that we pass the
13211 concatenation of files.files[1].dir and files.files[1].name as the
13212 subfile's name. */
13213
13214 static void
13215 dwarf2_start_subfile (char *filename, const char *dirname,
13216 const char *comp_dir)
13217 {
13218 char *fullname;
13219
13220 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13221 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13222 second argument to start_subfile. To be consistent, we do the
13223 same here. In order not to lose the line information directory,
13224 we concatenate it to the filename when it makes sense.
13225 Note that the Dwarf3 standard says (speaking of filenames in line
13226 information): ``The directory index is ignored for file names
13227 that represent full path names''. Thus ignoring dirname in the
13228 `else' branch below isn't an issue. */
13229
13230 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13231 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13232 else
13233 fullname = filename;
13234
13235 start_subfile (fullname, comp_dir);
13236
13237 if (fullname != filename)
13238 xfree (fullname);
13239 }
13240
13241 static void
13242 var_decode_location (struct attribute *attr, struct symbol *sym,
13243 struct dwarf2_cu *cu)
13244 {
13245 struct objfile *objfile = cu->objfile;
13246 struct comp_unit_head *cu_header = &cu->header;
13247
13248 /* NOTE drow/2003-01-30: There used to be a comment and some special
13249 code here to turn a symbol with DW_AT_external and a
13250 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13251 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13252 with some versions of binutils) where shared libraries could have
13253 relocations against symbols in their debug information - the
13254 minimal symbol would have the right address, but the debug info
13255 would not. It's no longer necessary, because we will explicitly
13256 apply relocations when we read in the debug information now. */
13257
13258 /* A DW_AT_location attribute with no contents indicates that a
13259 variable has been optimized away. */
13260 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13261 {
13262 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13263 return;
13264 }
13265
13266 /* Handle one degenerate form of location expression specially, to
13267 preserve GDB's previous behavior when section offsets are
13268 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13269 then mark this symbol as LOC_STATIC. */
13270
13271 if (attr_form_is_block (attr)
13272 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13273 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13274 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13275 && (DW_BLOCK (attr)->size
13276 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13277 {
13278 unsigned int dummy;
13279
13280 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13281 SYMBOL_VALUE_ADDRESS (sym) =
13282 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13283 else
13284 SYMBOL_VALUE_ADDRESS (sym) =
13285 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13286 SYMBOL_CLASS (sym) = LOC_STATIC;
13287 fixup_symbol_section (sym, objfile);
13288 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13289 SYMBOL_SECTION (sym));
13290 return;
13291 }
13292
13293 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13294 expression evaluator, and use LOC_COMPUTED only when necessary
13295 (i.e. when the value of a register or memory location is
13296 referenced, or a thread-local block, etc.). Then again, it might
13297 not be worthwhile. I'm assuming that it isn't unless performance
13298 or memory numbers show me otherwise. */
13299
13300 dwarf2_symbol_mark_computed (attr, sym, cu);
13301 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13302
13303 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13304 cu->has_loclist = 1;
13305 }
13306
13307 /* Given a pointer to a DWARF information entry, figure out if we need
13308 to make a symbol table entry for it, and if so, create a new entry
13309 and return a pointer to it.
13310 If TYPE is NULL, determine symbol type from the die, otherwise
13311 used the passed type.
13312 If SPACE is not NULL, use it to hold the new symbol. If it is
13313 NULL, allocate a new symbol on the objfile's obstack. */
13314
13315 static struct symbol *
13316 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13317 struct symbol *space)
13318 {
13319 struct objfile *objfile = cu->objfile;
13320 struct symbol *sym = NULL;
13321 char *name;
13322 struct attribute *attr = NULL;
13323 struct attribute *attr2 = NULL;
13324 CORE_ADDR baseaddr;
13325 struct pending **list_to_add = NULL;
13326
13327 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13328
13329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13330
13331 name = dwarf2_name (die, cu);
13332 if (name)
13333 {
13334 const char *linkagename;
13335 int suppress_add = 0;
13336
13337 if (space)
13338 sym = space;
13339 else
13340 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13341 OBJSTAT (objfile, n_syms++);
13342
13343 /* Cache this symbol's name and the name's demangled form (if any). */
13344 SYMBOL_SET_LANGUAGE (sym, cu->language);
13345 linkagename = dwarf2_physname (name, die, cu);
13346 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13347
13348 /* Fortran does not have mangling standard and the mangling does differ
13349 between gfortran, iFort etc. */
13350 if (cu->language == language_fortran
13351 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13352 symbol_set_demangled_name (&(sym->ginfo),
13353 (char *) dwarf2_full_name (name, die, cu),
13354 NULL);
13355
13356 /* Default assumptions.
13357 Use the passed type or decode it from the die. */
13358 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13359 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13360 if (type != NULL)
13361 SYMBOL_TYPE (sym) = type;
13362 else
13363 SYMBOL_TYPE (sym) = die_type (die, cu);
13364 attr = dwarf2_attr (die,
13365 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13366 cu);
13367 if (attr)
13368 {
13369 SYMBOL_LINE (sym) = DW_UNSND (attr);
13370 }
13371
13372 attr = dwarf2_attr (die,
13373 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13374 cu);
13375 if (attr)
13376 {
13377 int file_index = DW_UNSND (attr);
13378
13379 if (cu->line_header == NULL
13380 || file_index > cu->line_header->num_file_names)
13381 complaint (&symfile_complaints,
13382 _("file index out of range"));
13383 else if (file_index > 0)
13384 {
13385 struct file_entry *fe;
13386
13387 fe = &cu->line_header->file_names[file_index - 1];
13388 SYMBOL_SYMTAB (sym) = fe->symtab;
13389 }
13390 }
13391
13392 switch (die->tag)
13393 {
13394 case DW_TAG_label:
13395 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13396 if (attr)
13397 {
13398 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13399 }
13400 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13401 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13402 SYMBOL_CLASS (sym) = LOC_LABEL;
13403 add_symbol_to_list (sym, cu->list_in_scope);
13404 break;
13405 case DW_TAG_subprogram:
13406 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13407 finish_block. */
13408 SYMBOL_CLASS (sym) = LOC_BLOCK;
13409 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13410 if ((attr2 && (DW_UNSND (attr2) != 0))
13411 || cu->language == language_ada)
13412 {
13413 /* Subprograms marked external are stored as a global symbol.
13414 Ada subprograms, whether marked external or not, are always
13415 stored as a global symbol, because we want to be able to
13416 access them globally. For instance, we want to be able
13417 to break on a nested subprogram without having to
13418 specify the context. */
13419 list_to_add = &global_symbols;
13420 }
13421 else
13422 {
13423 list_to_add = cu->list_in_scope;
13424 }
13425 break;
13426 case DW_TAG_inlined_subroutine:
13427 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13428 finish_block. */
13429 SYMBOL_CLASS (sym) = LOC_BLOCK;
13430 SYMBOL_INLINED (sym) = 1;
13431 list_to_add = cu->list_in_scope;
13432 break;
13433 case DW_TAG_template_value_param:
13434 suppress_add = 1;
13435 /* Fall through. */
13436 case DW_TAG_constant:
13437 case DW_TAG_variable:
13438 case DW_TAG_member:
13439 /* Compilation with minimal debug info may result in
13440 variables with missing type entries. Change the
13441 misleading `void' type to something sensible. */
13442 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13443 SYMBOL_TYPE (sym)
13444 = objfile_type (objfile)->nodebug_data_symbol;
13445
13446 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13447 /* In the case of DW_TAG_member, we should only be called for
13448 static const members. */
13449 if (die->tag == DW_TAG_member)
13450 {
13451 /* dwarf2_add_field uses die_is_declaration,
13452 so we do the same. */
13453 gdb_assert (die_is_declaration (die, cu));
13454 gdb_assert (attr);
13455 }
13456 if (attr)
13457 {
13458 dwarf2_const_value (attr, sym, cu);
13459 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13460 if (!suppress_add)
13461 {
13462 if (attr2 && (DW_UNSND (attr2) != 0))
13463 list_to_add = &global_symbols;
13464 else
13465 list_to_add = cu->list_in_scope;
13466 }
13467 break;
13468 }
13469 attr = dwarf2_attr (die, DW_AT_location, cu);
13470 if (attr)
13471 {
13472 var_decode_location (attr, sym, cu);
13473 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13474 if (SYMBOL_CLASS (sym) == LOC_STATIC
13475 && SYMBOL_VALUE_ADDRESS (sym) == 0
13476 && !dwarf2_per_objfile->has_section_at_zero)
13477 {
13478 /* When a static variable is eliminated by the linker,
13479 the corresponding debug information is not stripped
13480 out, but the variable address is set to null;
13481 do not add such variables into symbol table. */
13482 }
13483 else if (attr2 && (DW_UNSND (attr2) != 0))
13484 {
13485 /* Workaround gfortran PR debug/40040 - it uses
13486 DW_AT_location for variables in -fPIC libraries which may
13487 get overriden by other libraries/executable and get
13488 a different address. Resolve it by the minimal symbol
13489 which may come from inferior's executable using copy
13490 relocation. Make this workaround only for gfortran as for
13491 other compilers GDB cannot guess the minimal symbol
13492 Fortran mangling kind. */
13493 if (cu->language == language_fortran && die->parent
13494 && die->parent->tag == DW_TAG_module
13495 && cu->producer
13496 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13497 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13498
13499 /* A variable with DW_AT_external is never static,
13500 but it may be block-scoped. */
13501 list_to_add = (cu->list_in_scope == &file_symbols
13502 ? &global_symbols : cu->list_in_scope);
13503 }
13504 else
13505 list_to_add = cu->list_in_scope;
13506 }
13507 else
13508 {
13509 /* We do not know the address of this symbol.
13510 If it is an external symbol and we have type information
13511 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13512 The address of the variable will then be determined from
13513 the minimal symbol table whenever the variable is
13514 referenced. */
13515 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13516 if (attr2 && (DW_UNSND (attr2) != 0)
13517 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13518 {
13519 /* A variable with DW_AT_external is never static, but it
13520 may be block-scoped. */
13521 list_to_add = (cu->list_in_scope == &file_symbols
13522 ? &global_symbols : cu->list_in_scope);
13523
13524 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13525 }
13526 else if (!die_is_declaration (die, cu))
13527 {
13528 /* Use the default LOC_OPTIMIZED_OUT class. */
13529 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13530 if (!suppress_add)
13531 list_to_add = cu->list_in_scope;
13532 }
13533 }
13534 break;
13535 case DW_TAG_formal_parameter:
13536 /* If we are inside a function, mark this as an argument. If
13537 not, we might be looking at an argument to an inlined function
13538 when we do not have enough information to show inlined frames;
13539 pretend it's a local variable in that case so that the user can
13540 still see it. */
13541 if (context_stack_depth > 0
13542 && context_stack[context_stack_depth - 1].name != NULL)
13543 SYMBOL_IS_ARGUMENT (sym) = 1;
13544 attr = dwarf2_attr (die, DW_AT_location, cu);
13545 if (attr)
13546 {
13547 var_decode_location (attr, sym, cu);
13548 }
13549 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13550 if (attr)
13551 {
13552 dwarf2_const_value (attr, sym, cu);
13553 }
13554
13555 list_to_add = cu->list_in_scope;
13556 break;
13557 case DW_TAG_unspecified_parameters:
13558 /* From varargs functions; gdb doesn't seem to have any
13559 interest in this information, so just ignore it for now.
13560 (FIXME?) */
13561 break;
13562 case DW_TAG_template_type_param:
13563 suppress_add = 1;
13564 /* Fall through. */
13565 case DW_TAG_class_type:
13566 case DW_TAG_interface_type:
13567 case DW_TAG_structure_type:
13568 case DW_TAG_union_type:
13569 case DW_TAG_set_type:
13570 case DW_TAG_enumeration_type:
13571 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13572 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13573
13574 {
13575 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13576 really ever be static objects: otherwise, if you try
13577 to, say, break of a class's method and you're in a file
13578 which doesn't mention that class, it won't work unless
13579 the check for all static symbols in lookup_symbol_aux
13580 saves you. See the OtherFileClass tests in
13581 gdb.c++/namespace.exp. */
13582
13583 if (!suppress_add)
13584 {
13585 list_to_add = (cu->list_in_scope == &file_symbols
13586 && (cu->language == language_cplus
13587 || cu->language == language_java)
13588 ? &global_symbols : cu->list_in_scope);
13589
13590 /* The semantics of C++ state that "struct foo {
13591 ... }" also defines a typedef for "foo". A Java
13592 class declaration also defines a typedef for the
13593 class. */
13594 if (cu->language == language_cplus
13595 || cu->language == language_java
13596 || cu->language == language_ada)
13597 {
13598 /* The symbol's name is already allocated along
13599 with this objfile, so we don't need to
13600 duplicate it for the type. */
13601 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13602 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13603 }
13604 }
13605 }
13606 break;
13607 case DW_TAG_typedef:
13608 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13609 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13610 list_to_add = cu->list_in_scope;
13611 break;
13612 case DW_TAG_base_type:
13613 case DW_TAG_subrange_type:
13614 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13615 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13616 list_to_add = cu->list_in_scope;
13617 break;
13618 case DW_TAG_enumerator:
13619 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13620 if (attr)
13621 {
13622 dwarf2_const_value (attr, sym, cu);
13623 }
13624 {
13625 /* NOTE: carlton/2003-11-10: See comment above in the
13626 DW_TAG_class_type, etc. block. */
13627
13628 list_to_add = (cu->list_in_scope == &file_symbols
13629 && (cu->language == language_cplus
13630 || cu->language == language_java)
13631 ? &global_symbols : cu->list_in_scope);
13632 }
13633 break;
13634 case DW_TAG_namespace:
13635 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13636 list_to_add = &global_symbols;
13637 break;
13638 default:
13639 /* Not a tag we recognize. Hopefully we aren't processing
13640 trash data, but since we must specifically ignore things
13641 we don't recognize, there is nothing else we should do at
13642 this point. */
13643 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13644 dwarf_tag_name (die->tag));
13645 break;
13646 }
13647
13648 if (suppress_add)
13649 {
13650 sym->hash_next = objfile->template_symbols;
13651 objfile->template_symbols = sym;
13652 list_to_add = NULL;
13653 }
13654
13655 if (list_to_add != NULL)
13656 add_symbol_to_list (sym, list_to_add);
13657
13658 /* For the benefit of old versions of GCC, check for anonymous
13659 namespaces based on the demangled name. */
13660 if (!processing_has_namespace_info
13661 && cu->language == language_cplus)
13662 cp_scan_for_anonymous_namespaces (sym, objfile);
13663 }
13664 return (sym);
13665 }
13666
13667 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13668
13669 static struct symbol *
13670 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13671 {
13672 return new_symbol_full (die, type, cu, NULL);
13673 }
13674
13675 /* Given an attr with a DW_FORM_dataN value in host byte order,
13676 zero-extend it as appropriate for the symbol's type. The DWARF
13677 standard (v4) is not entirely clear about the meaning of using
13678 DW_FORM_dataN for a constant with a signed type, where the type is
13679 wider than the data. The conclusion of a discussion on the DWARF
13680 list was that this is unspecified. We choose to always zero-extend
13681 because that is the interpretation long in use by GCC. */
13682
13683 static gdb_byte *
13684 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13685 const char *name, struct obstack *obstack,
13686 struct dwarf2_cu *cu, LONGEST *value, int bits)
13687 {
13688 struct objfile *objfile = cu->objfile;
13689 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13690 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13691 LONGEST l = DW_UNSND (attr);
13692
13693 if (bits < sizeof (*value) * 8)
13694 {
13695 l &= ((LONGEST) 1 << bits) - 1;
13696 *value = l;
13697 }
13698 else if (bits == sizeof (*value) * 8)
13699 *value = l;
13700 else
13701 {
13702 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13703 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13704 return bytes;
13705 }
13706
13707 return NULL;
13708 }
13709
13710 /* Read a constant value from an attribute. Either set *VALUE, or if
13711 the value does not fit in *VALUE, set *BYTES - either already
13712 allocated on the objfile obstack, or newly allocated on OBSTACK,
13713 or, set *BATON, if we translated the constant to a location
13714 expression. */
13715
13716 static void
13717 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13718 const char *name, struct obstack *obstack,
13719 struct dwarf2_cu *cu,
13720 LONGEST *value, gdb_byte **bytes,
13721 struct dwarf2_locexpr_baton **baton)
13722 {
13723 struct objfile *objfile = cu->objfile;
13724 struct comp_unit_head *cu_header = &cu->header;
13725 struct dwarf_block *blk;
13726 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13727 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13728
13729 *value = 0;
13730 *bytes = NULL;
13731 *baton = NULL;
13732
13733 switch (attr->form)
13734 {
13735 case DW_FORM_addr:
13736 case DW_FORM_GNU_addr_index:
13737 {
13738 gdb_byte *data;
13739
13740 if (TYPE_LENGTH (type) != cu_header->addr_size)
13741 dwarf2_const_value_length_mismatch_complaint (name,
13742 cu_header->addr_size,
13743 TYPE_LENGTH (type));
13744 /* Symbols of this form are reasonably rare, so we just
13745 piggyback on the existing location code rather than writing
13746 a new implementation of symbol_computed_ops. */
13747 *baton = obstack_alloc (&objfile->objfile_obstack,
13748 sizeof (struct dwarf2_locexpr_baton));
13749 (*baton)->per_cu = cu->per_cu;
13750 gdb_assert ((*baton)->per_cu);
13751
13752 (*baton)->size = 2 + cu_header->addr_size;
13753 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13754 (*baton)->data = data;
13755
13756 data[0] = DW_OP_addr;
13757 store_unsigned_integer (&data[1], cu_header->addr_size,
13758 byte_order, DW_ADDR (attr));
13759 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13760 }
13761 break;
13762 case DW_FORM_string:
13763 case DW_FORM_strp:
13764 case DW_FORM_GNU_str_index:
13765 /* DW_STRING is already allocated on the objfile obstack, point
13766 directly to it. */
13767 *bytes = (gdb_byte *) DW_STRING (attr);
13768 break;
13769 case DW_FORM_block1:
13770 case DW_FORM_block2:
13771 case DW_FORM_block4:
13772 case DW_FORM_block:
13773 case DW_FORM_exprloc:
13774 blk = DW_BLOCK (attr);
13775 if (TYPE_LENGTH (type) != blk->size)
13776 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13777 TYPE_LENGTH (type));
13778 *bytes = blk->data;
13779 break;
13780
13781 /* The DW_AT_const_value attributes are supposed to carry the
13782 symbol's value "represented as it would be on the target
13783 architecture." By the time we get here, it's already been
13784 converted to host endianness, so we just need to sign- or
13785 zero-extend it as appropriate. */
13786 case DW_FORM_data1:
13787 *bytes = dwarf2_const_value_data (attr, type, name,
13788 obstack, cu, value, 8);
13789 break;
13790 case DW_FORM_data2:
13791 *bytes = dwarf2_const_value_data (attr, type, name,
13792 obstack, cu, value, 16);
13793 break;
13794 case DW_FORM_data4:
13795 *bytes = dwarf2_const_value_data (attr, type, name,
13796 obstack, cu, value, 32);
13797 break;
13798 case DW_FORM_data8:
13799 *bytes = dwarf2_const_value_data (attr, type, name,
13800 obstack, cu, value, 64);
13801 break;
13802
13803 case DW_FORM_sdata:
13804 *value = DW_SND (attr);
13805 break;
13806
13807 case DW_FORM_udata:
13808 *value = DW_UNSND (attr);
13809 break;
13810
13811 default:
13812 complaint (&symfile_complaints,
13813 _("unsupported const value attribute form: '%s'"),
13814 dwarf_form_name (attr->form));
13815 *value = 0;
13816 break;
13817 }
13818 }
13819
13820
13821 /* Copy constant value from an attribute to a symbol. */
13822
13823 static void
13824 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13825 struct dwarf2_cu *cu)
13826 {
13827 struct objfile *objfile = cu->objfile;
13828 struct comp_unit_head *cu_header = &cu->header;
13829 LONGEST value;
13830 gdb_byte *bytes;
13831 struct dwarf2_locexpr_baton *baton;
13832
13833 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13834 SYMBOL_PRINT_NAME (sym),
13835 &objfile->objfile_obstack, cu,
13836 &value, &bytes, &baton);
13837
13838 if (baton != NULL)
13839 {
13840 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13841 SYMBOL_LOCATION_BATON (sym) = baton;
13842 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13843 }
13844 else if (bytes != NULL)
13845 {
13846 SYMBOL_VALUE_BYTES (sym) = bytes;
13847 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13848 }
13849 else
13850 {
13851 SYMBOL_VALUE (sym) = value;
13852 SYMBOL_CLASS (sym) = LOC_CONST;
13853 }
13854 }
13855
13856 /* Return the type of the die in question using its DW_AT_type attribute. */
13857
13858 static struct type *
13859 die_type (struct die_info *die, struct dwarf2_cu *cu)
13860 {
13861 struct attribute *type_attr;
13862
13863 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13864 if (!type_attr)
13865 {
13866 /* A missing DW_AT_type represents a void type. */
13867 return objfile_type (cu->objfile)->builtin_void;
13868 }
13869
13870 return lookup_die_type (die, type_attr, cu);
13871 }
13872
13873 /* True iff CU's producer generates GNAT Ada auxiliary information
13874 that allows to find parallel types through that information instead
13875 of having to do expensive parallel lookups by type name. */
13876
13877 static int
13878 need_gnat_info (struct dwarf2_cu *cu)
13879 {
13880 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13881 of GNAT produces this auxiliary information, without any indication
13882 that it is produced. Part of enhancing the FSF version of GNAT
13883 to produce that information will be to put in place an indicator
13884 that we can use in order to determine whether the descriptive type
13885 info is available or not. One suggestion that has been made is
13886 to use a new attribute, attached to the CU die. For now, assume
13887 that the descriptive type info is not available. */
13888 return 0;
13889 }
13890
13891 /* Return the auxiliary type of the die in question using its
13892 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13893 attribute is not present. */
13894
13895 static struct type *
13896 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13897 {
13898 struct attribute *type_attr;
13899
13900 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13901 if (!type_attr)
13902 return NULL;
13903
13904 return lookup_die_type (die, type_attr, cu);
13905 }
13906
13907 /* If DIE has a descriptive_type attribute, then set the TYPE's
13908 descriptive type accordingly. */
13909
13910 static void
13911 set_descriptive_type (struct type *type, struct die_info *die,
13912 struct dwarf2_cu *cu)
13913 {
13914 struct type *descriptive_type = die_descriptive_type (die, cu);
13915
13916 if (descriptive_type)
13917 {
13918 ALLOCATE_GNAT_AUX_TYPE (type);
13919 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13920 }
13921 }
13922
13923 /* Return the containing type of the die in question using its
13924 DW_AT_containing_type attribute. */
13925
13926 static struct type *
13927 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13928 {
13929 struct attribute *type_attr;
13930
13931 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13932 if (!type_attr)
13933 error (_("Dwarf Error: Problem turning containing type into gdb type "
13934 "[in module %s]"), cu->objfile->name);
13935
13936 return lookup_die_type (die, type_attr, cu);
13937 }
13938
13939 /* Look up the type of DIE in CU using its type attribute ATTR.
13940 If there is no type substitute an error marker. */
13941
13942 static struct type *
13943 lookup_die_type (struct die_info *die, struct attribute *attr,
13944 struct dwarf2_cu *cu)
13945 {
13946 struct objfile *objfile = cu->objfile;
13947 struct type *this_type;
13948
13949 /* First see if we have it cached. */
13950
13951 if (is_ref_attr (attr))
13952 {
13953 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13954
13955 this_type = get_die_type_at_offset (offset, cu->per_cu);
13956 }
13957 else if (attr->form == DW_FORM_ref_sig8)
13958 {
13959 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13960
13961 /* sig_type will be NULL if the signatured type is missing from
13962 the debug info. */
13963 if (sig_type == NULL)
13964 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13965 "at 0x%x [in module %s]"),
13966 die->offset.sect_off, objfile->name);
13967
13968 gdb_assert (sig_type->per_cu.is_debug_types);
13969 /* If we haven't filled in type_offset_in_section yet, then we
13970 haven't read the type in yet. */
13971 this_type = NULL;
13972 if (sig_type->type_offset_in_section.sect_off != 0)
13973 {
13974 this_type =
13975 get_die_type_at_offset (sig_type->type_offset_in_section,
13976 &sig_type->per_cu);
13977 }
13978 }
13979 else
13980 {
13981 dump_die_for_error (die);
13982 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13983 dwarf_attr_name (attr->name), objfile->name);
13984 }
13985
13986 /* If not cached we need to read it in. */
13987
13988 if (this_type == NULL)
13989 {
13990 struct die_info *type_die;
13991 struct dwarf2_cu *type_cu = cu;
13992
13993 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13994 /* If we found the type now, it's probably because the type came
13995 from an inter-CU reference and the type's CU got expanded before
13996 ours. */
13997 this_type = get_die_type (type_die, type_cu);
13998 if (this_type == NULL)
13999 this_type = read_type_die_1 (type_die, type_cu);
14000 }
14001
14002 /* If we still don't have a type use an error marker. */
14003
14004 if (this_type == NULL)
14005 {
14006 char *message, *saved;
14007
14008 /* read_type_die already issued a complaint. */
14009 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
14010 objfile->name,
14011 cu->header.offset.sect_off,
14012 die->offset.sect_off);
14013 saved = obstack_copy0 (&objfile->objfile_obstack,
14014 message, strlen (message));
14015 xfree (message);
14016
14017 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
14018 }
14019
14020 return this_type;
14021 }
14022
14023 /* Return the type in DIE, CU.
14024 Returns NULL for invalid types.
14025
14026 This first does a lookup in the appropriate type_hash table,
14027 and only reads the die in if necessary.
14028
14029 NOTE: This can be called when reading in partial or full symbols. */
14030
14031 static struct type *
14032 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
14033 {
14034 struct type *this_type;
14035
14036 this_type = get_die_type (die, cu);
14037 if (this_type)
14038 return this_type;
14039
14040 return read_type_die_1 (die, cu);
14041 }
14042
14043 /* Read the type in DIE, CU.
14044 Returns NULL for invalid types. */
14045
14046 static struct type *
14047 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14048 {
14049 struct type *this_type = NULL;
14050
14051 switch (die->tag)
14052 {
14053 case DW_TAG_class_type:
14054 case DW_TAG_interface_type:
14055 case DW_TAG_structure_type:
14056 case DW_TAG_union_type:
14057 this_type = read_structure_type (die, cu);
14058 break;
14059 case DW_TAG_enumeration_type:
14060 this_type = read_enumeration_type (die, cu);
14061 break;
14062 case DW_TAG_subprogram:
14063 case DW_TAG_subroutine_type:
14064 case DW_TAG_inlined_subroutine:
14065 this_type = read_subroutine_type (die, cu);
14066 break;
14067 case DW_TAG_array_type:
14068 this_type = read_array_type (die, cu);
14069 break;
14070 case DW_TAG_set_type:
14071 this_type = read_set_type (die, cu);
14072 break;
14073 case DW_TAG_pointer_type:
14074 this_type = read_tag_pointer_type (die, cu);
14075 break;
14076 case DW_TAG_ptr_to_member_type:
14077 this_type = read_tag_ptr_to_member_type (die, cu);
14078 break;
14079 case DW_TAG_reference_type:
14080 this_type = read_tag_reference_type (die, cu);
14081 break;
14082 case DW_TAG_const_type:
14083 this_type = read_tag_const_type (die, cu);
14084 break;
14085 case DW_TAG_volatile_type:
14086 this_type = read_tag_volatile_type (die, cu);
14087 break;
14088 case DW_TAG_string_type:
14089 this_type = read_tag_string_type (die, cu);
14090 break;
14091 case DW_TAG_typedef:
14092 this_type = read_typedef (die, cu);
14093 break;
14094 case DW_TAG_subrange_type:
14095 this_type = read_subrange_type (die, cu);
14096 break;
14097 case DW_TAG_base_type:
14098 this_type = read_base_type (die, cu);
14099 break;
14100 case DW_TAG_unspecified_type:
14101 this_type = read_unspecified_type (die, cu);
14102 break;
14103 case DW_TAG_namespace:
14104 this_type = read_namespace_type (die, cu);
14105 break;
14106 case DW_TAG_module:
14107 this_type = read_module_type (die, cu);
14108 break;
14109 default:
14110 complaint (&symfile_complaints,
14111 _("unexpected tag in read_type_die: '%s'"),
14112 dwarf_tag_name (die->tag));
14113 break;
14114 }
14115
14116 return this_type;
14117 }
14118
14119 /* See if we can figure out if the class lives in a namespace. We do
14120 this by looking for a member function; its demangled name will
14121 contain namespace info, if there is any.
14122 Return the computed name or NULL.
14123 Space for the result is allocated on the objfile's obstack.
14124 This is the full-die version of guess_partial_die_structure_name.
14125 In this case we know DIE has no useful parent. */
14126
14127 static char *
14128 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14129 {
14130 struct die_info *spec_die;
14131 struct dwarf2_cu *spec_cu;
14132 struct die_info *child;
14133
14134 spec_cu = cu;
14135 spec_die = die_specification (die, &spec_cu);
14136 if (spec_die != NULL)
14137 {
14138 die = spec_die;
14139 cu = spec_cu;
14140 }
14141
14142 for (child = die->child;
14143 child != NULL;
14144 child = child->sibling)
14145 {
14146 if (child->tag == DW_TAG_subprogram)
14147 {
14148 struct attribute *attr;
14149
14150 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14151 if (attr == NULL)
14152 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14153 if (attr != NULL)
14154 {
14155 char *actual_name
14156 = language_class_name_from_physname (cu->language_defn,
14157 DW_STRING (attr));
14158 char *name = NULL;
14159
14160 if (actual_name != NULL)
14161 {
14162 char *die_name = dwarf2_name (die, cu);
14163
14164 if (die_name != NULL
14165 && strcmp (die_name, actual_name) != 0)
14166 {
14167 /* Strip off the class name from the full name.
14168 We want the prefix. */
14169 int die_name_len = strlen (die_name);
14170 int actual_name_len = strlen (actual_name);
14171
14172 /* Test for '::' as a sanity check. */
14173 if (actual_name_len > die_name_len + 2
14174 && actual_name[actual_name_len
14175 - die_name_len - 1] == ':')
14176 name =
14177 obsavestring (actual_name,
14178 actual_name_len - die_name_len - 2,
14179 &cu->objfile->objfile_obstack);
14180 }
14181 }
14182 xfree (actual_name);
14183 return name;
14184 }
14185 }
14186 }
14187
14188 return NULL;
14189 }
14190
14191 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14192 prefix part in such case. See
14193 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14194
14195 static char *
14196 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14197 {
14198 struct attribute *attr;
14199 char *base;
14200
14201 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14202 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14203 return NULL;
14204
14205 attr = dwarf2_attr (die, DW_AT_name, cu);
14206 if (attr != NULL && DW_STRING (attr) != NULL)
14207 return NULL;
14208
14209 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14210 if (attr == NULL)
14211 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14212 if (attr == NULL || DW_STRING (attr) == NULL)
14213 return NULL;
14214
14215 /* dwarf2_name had to be already called. */
14216 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14217
14218 /* Strip the base name, keep any leading namespaces/classes. */
14219 base = strrchr (DW_STRING (attr), ':');
14220 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14221 return "";
14222
14223 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14224 &cu->objfile->objfile_obstack);
14225 }
14226
14227 /* Return the name of the namespace/class that DIE is defined within,
14228 or "" if we can't tell. The caller should not xfree the result.
14229
14230 For example, if we're within the method foo() in the following
14231 code:
14232
14233 namespace N {
14234 class C {
14235 void foo () {
14236 }
14237 };
14238 }
14239
14240 then determine_prefix on foo's die will return "N::C". */
14241
14242 static const char *
14243 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14244 {
14245 struct die_info *parent, *spec_die;
14246 struct dwarf2_cu *spec_cu;
14247 struct type *parent_type;
14248 char *retval;
14249
14250 if (cu->language != language_cplus && cu->language != language_java
14251 && cu->language != language_fortran)
14252 return "";
14253
14254 retval = anonymous_struct_prefix (die, cu);
14255 if (retval)
14256 return retval;
14257
14258 /* We have to be careful in the presence of DW_AT_specification.
14259 For example, with GCC 3.4, given the code
14260
14261 namespace N {
14262 void foo() {
14263 // Definition of N::foo.
14264 }
14265 }
14266
14267 then we'll have a tree of DIEs like this:
14268
14269 1: DW_TAG_compile_unit
14270 2: DW_TAG_namespace // N
14271 3: DW_TAG_subprogram // declaration of N::foo
14272 4: DW_TAG_subprogram // definition of N::foo
14273 DW_AT_specification // refers to die #3
14274
14275 Thus, when processing die #4, we have to pretend that we're in
14276 the context of its DW_AT_specification, namely the contex of die
14277 #3. */
14278 spec_cu = cu;
14279 spec_die = die_specification (die, &spec_cu);
14280 if (spec_die == NULL)
14281 parent = die->parent;
14282 else
14283 {
14284 parent = spec_die->parent;
14285 cu = spec_cu;
14286 }
14287
14288 if (parent == NULL)
14289 return "";
14290 else if (parent->building_fullname)
14291 {
14292 const char *name;
14293 const char *parent_name;
14294
14295 /* It has been seen on RealView 2.2 built binaries,
14296 DW_TAG_template_type_param types actually _defined_ as
14297 children of the parent class:
14298
14299 enum E {};
14300 template class <class Enum> Class{};
14301 Class<enum E> class_e;
14302
14303 1: DW_TAG_class_type (Class)
14304 2: DW_TAG_enumeration_type (E)
14305 3: DW_TAG_enumerator (enum1:0)
14306 3: DW_TAG_enumerator (enum2:1)
14307 ...
14308 2: DW_TAG_template_type_param
14309 DW_AT_type DW_FORM_ref_udata (E)
14310
14311 Besides being broken debug info, it can put GDB into an
14312 infinite loop. Consider:
14313
14314 When we're building the full name for Class<E>, we'll start
14315 at Class, and go look over its template type parameters,
14316 finding E. We'll then try to build the full name of E, and
14317 reach here. We're now trying to build the full name of E,
14318 and look over the parent DIE for containing scope. In the
14319 broken case, if we followed the parent DIE of E, we'd again
14320 find Class, and once again go look at its template type
14321 arguments, etc., etc. Simply don't consider such parent die
14322 as source-level parent of this die (it can't be, the language
14323 doesn't allow it), and break the loop here. */
14324 name = dwarf2_name (die, cu);
14325 parent_name = dwarf2_name (parent, cu);
14326 complaint (&symfile_complaints,
14327 _("template param type '%s' defined within parent '%s'"),
14328 name ? name : "<unknown>",
14329 parent_name ? parent_name : "<unknown>");
14330 return "";
14331 }
14332 else
14333 switch (parent->tag)
14334 {
14335 case DW_TAG_namespace:
14336 parent_type = read_type_die (parent, cu);
14337 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14338 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14339 Work around this problem here. */
14340 if (cu->language == language_cplus
14341 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14342 return "";
14343 /* We give a name to even anonymous namespaces. */
14344 return TYPE_TAG_NAME (parent_type);
14345 case DW_TAG_class_type:
14346 case DW_TAG_interface_type:
14347 case DW_TAG_structure_type:
14348 case DW_TAG_union_type:
14349 case DW_TAG_module:
14350 parent_type = read_type_die (parent, cu);
14351 if (TYPE_TAG_NAME (parent_type) != NULL)
14352 return TYPE_TAG_NAME (parent_type);
14353 else
14354 /* An anonymous structure is only allowed non-static data
14355 members; no typedefs, no member functions, et cetera.
14356 So it does not need a prefix. */
14357 return "";
14358 case DW_TAG_compile_unit:
14359 case DW_TAG_partial_unit:
14360 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14361 if (cu->language == language_cplus
14362 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14363 && die->child != NULL
14364 && (die->tag == DW_TAG_class_type
14365 || die->tag == DW_TAG_structure_type
14366 || die->tag == DW_TAG_union_type))
14367 {
14368 char *name = guess_full_die_structure_name (die, cu);
14369 if (name != NULL)
14370 return name;
14371 }
14372 return "";
14373 default:
14374 return determine_prefix (parent, cu);
14375 }
14376 }
14377
14378 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14379 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14380 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14381 an obconcat, otherwise allocate storage for the result. The CU argument is
14382 used to determine the language and hence, the appropriate separator. */
14383
14384 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14385
14386 static char *
14387 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14388 int physname, struct dwarf2_cu *cu)
14389 {
14390 const char *lead = "";
14391 const char *sep;
14392
14393 if (suffix == NULL || suffix[0] == '\0'
14394 || prefix == NULL || prefix[0] == '\0')
14395 sep = "";
14396 else if (cu->language == language_java)
14397 sep = ".";
14398 else if (cu->language == language_fortran && physname)
14399 {
14400 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14401 DW_AT_MIPS_linkage_name is preferred and used instead. */
14402
14403 lead = "__";
14404 sep = "_MOD_";
14405 }
14406 else
14407 sep = "::";
14408
14409 if (prefix == NULL)
14410 prefix = "";
14411 if (suffix == NULL)
14412 suffix = "";
14413
14414 if (obs == NULL)
14415 {
14416 char *retval
14417 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14418
14419 strcpy (retval, lead);
14420 strcat (retval, prefix);
14421 strcat (retval, sep);
14422 strcat (retval, suffix);
14423 return retval;
14424 }
14425 else
14426 {
14427 /* We have an obstack. */
14428 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14429 }
14430 }
14431
14432 /* Return sibling of die, NULL if no sibling. */
14433
14434 static struct die_info *
14435 sibling_die (struct die_info *die)
14436 {
14437 return die->sibling;
14438 }
14439
14440 /* Get name of a die, return NULL if not found. */
14441
14442 static char *
14443 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14444 struct obstack *obstack)
14445 {
14446 if (name && cu->language == language_cplus)
14447 {
14448 char *canon_name = cp_canonicalize_string (name);
14449
14450 if (canon_name != NULL)
14451 {
14452 if (strcmp (canon_name, name) != 0)
14453 name = obsavestring (canon_name, strlen (canon_name),
14454 obstack);
14455 xfree (canon_name);
14456 }
14457 }
14458
14459 return name;
14460 }
14461
14462 /* Get name of a die, return NULL if not found. */
14463
14464 static char *
14465 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14466 {
14467 struct attribute *attr;
14468
14469 attr = dwarf2_attr (die, DW_AT_name, cu);
14470 if ((!attr || !DW_STRING (attr))
14471 && die->tag != DW_TAG_class_type
14472 && die->tag != DW_TAG_interface_type
14473 && die->tag != DW_TAG_structure_type
14474 && die->tag != DW_TAG_union_type)
14475 return NULL;
14476
14477 switch (die->tag)
14478 {
14479 case DW_TAG_compile_unit:
14480 case DW_TAG_partial_unit:
14481 /* Compilation units have a DW_AT_name that is a filename, not
14482 a source language identifier. */
14483 case DW_TAG_enumeration_type:
14484 case DW_TAG_enumerator:
14485 /* These tags always have simple identifiers already; no need
14486 to canonicalize them. */
14487 return DW_STRING (attr);
14488
14489 case DW_TAG_subprogram:
14490 /* Java constructors will all be named "<init>", so return
14491 the class name when we see this special case. */
14492 if (cu->language == language_java
14493 && DW_STRING (attr) != NULL
14494 && strcmp (DW_STRING (attr), "<init>") == 0)
14495 {
14496 struct dwarf2_cu *spec_cu = cu;
14497 struct die_info *spec_die;
14498
14499 /* GCJ will output '<init>' for Java constructor names.
14500 For this special case, return the name of the parent class. */
14501
14502 /* GCJ may output suprogram DIEs with AT_specification set.
14503 If so, use the name of the specified DIE. */
14504 spec_die = die_specification (die, &spec_cu);
14505 if (spec_die != NULL)
14506 return dwarf2_name (spec_die, spec_cu);
14507
14508 do
14509 {
14510 die = die->parent;
14511 if (die->tag == DW_TAG_class_type)
14512 return dwarf2_name (die, cu);
14513 }
14514 while (die->tag != DW_TAG_compile_unit
14515 && die->tag != DW_TAG_partial_unit);
14516 }
14517 break;
14518
14519 case DW_TAG_class_type:
14520 case DW_TAG_interface_type:
14521 case DW_TAG_structure_type:
14522 case DW_TAG_union_type:
14523 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14524 structures or unions. These were of the form "._%d" in GCC 4.1,
14525 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14526 and GCC 4.4. We work around this problem by ignoring these. */
14527 if (attr && DW_STRING (attr)
14528 && (strncmp (DW_STRING (attr), "._", 2) == 0
14529 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14530 return NULL;
14531
14532 /* GCC might emit a nameless typedef that has a linkage name. See
14533 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14534 if (!attr || DW_STRING (attr) == NULL)
14535 {
14536 char *demangled = NULL;
14537
14538 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14539 if (attr == NULL)
14540 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14541
14542 if (attr == NULL || DW_STRING (attr) == NULL)
14543 return NULL;
14544
14545 /* Avoid demangling DW_STRING (attr) the second time on a second
14546 call for the same DIE. */
14547 if (!DW_STRING_IS_CANONICAL (attr))
14548 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14549
14550 if (demangled)
14551 {
14552 char *base;
14553
14554 /* FIXME: we already did this for the partial symbol... */
14555 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14556 &cu->objfile->objfile_obstack);
14557 DW_STRING_IS_CANONICAL (attr) = 1;
14558 xfree (demangled);
14559
14560 /* Strip any leading namespaces/classes, keep only the base name.
14561 DW_AT_name for named DIEs does not contain the prefixes. */
14562 base = strrchr (DW_STRING (attr), ':');
14563 if (base && base > DW_STRING (attr) && base[-1] == ':')
14564 return &base[1];
14565 else
14566 return DW_STRING (attr);
14567 }
14568 }
14569 break;
14570
14571 default:
14572 break;
14573 }
14574
14575 if (!DW_STRING_IS_CANONICAL (attr))
14576 {
14577 DW_STRING (attr)
14578 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14579 &cu->objfile->objfile_obstack);
14580 DW_STRING_IS_CANONICAL (attr) = 1;
14581 }
14582 return DW_STRING (attr);
14583 }
14584
14585 /* Return the die that this die in an extension of, or NULL if there
14586 is none. *EXT_CU is the CU containing DIE on input, and the CU
14587 containing the return value on output. */
14588
14589 static struct die_info *
14590 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14591 {
14592 struct attribute *attr;
14593
14594 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14595 if (attr == NULL)
14596 return NULL;
14597
14598 return follow_die_ref (die, attr, ext_cu);
14599 }
14600
14601 /* Convert a DIE tag into its string name. */
14602
14603 static const char *
14604 dwarf_tag_name (unsigned tag)
14605 {
14606 const char *name = get_DW_TAG_name (tag);
14607
14608 if (name == NULL)
14609 return "DW_TAG_<unknown>";
14610
14611 return name;
14612 }
14613
14614 /* Convert a DWARF attribute code into its string name. */
14615
14616 static const char *
14617 dwarf_attr_name (unsigned attr)
14618 {
14619 const char *name;
14620
14621 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14622 if (attr == DW_AT_MIPS_fde)
14623 return "DW_AT_MIPS_fde";
14624 #else
14625 if (attr == DW_AT_HP_block_index)
14626 return "DW_AT_HP_block_index";
14627 #endif
14628
14629 name = get_DW_AT_name (attr);
14630
14631 if (name == NULL)
14632 return "DW_AT_<unknown>";
14633
14634 return name;
14635 }
14636
14637 /* Convert a DWARF value form code into its string name. */
14638
14639 static const char *
14640 dwarf_form_name (unsigned form)
14641 {
14642 const char *name = get_DW_FORM_name (form);
14643
14644 if (name == NULL)
14645 return "DW_FORM_<unknown>";
14646
14647 return name;
14648 }
14649
14650 static char *
14651 dwarf_bool_name (unsigned mybool)
14652 {
14653 if (mybool)
14654 return "TRUE";
14655 else
14656 return "FALSE";
14657 }
14658
14659 /* Convert a DWARF type code into its string name. */
14660
14661 static const char *
14662 dwarf_type_encoding_name (unsigned enc)
14663 {
14664 const char *name = get_DW_ATE_name (enc);
14665
14666 if (name == NULL)
14667 return "DW_ATE_<unknown>";
14668
14669 return name;
14670 }
14671
14672 static void
14673 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14674 {
14675 unsigned int i;
14676
14677 print_spaces (indent, f);
14678 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14679 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14680
14681 if (die->parent != NULL)
14682 {
14683 print_spaces (indent, f);
14684 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14685 die->parent->offset.sect_off);
14686 }
14687
14688 print_spaces (indent, f);
14689 fprintf_unfiltered (f, " has children: %s\n",
14690 dwarf_bool_name (die->child != NULL));
14691
14692 print_spaces (indent, f);
14693 fprintf_unfiltered (f, " attributes:\n");
14694
14695 for (i = 0; i < die->num_attrs; ++i)
14696 {
14697 print_spaces (indent, f);
14698 fprintf_unfiltered (f, " %s (%s) ",
14699 dwarf_attr_name (die->attrs[i].name),
14700 dwarf_form_name (die->attrs[i].form));
14701
14702 switch (die->attrs[i].form)
14703 {
14704 case DW_FORM_addr:
14705 case DW_FORM_GNU_addr_index:
14706 fprintf_unfiltered (f, "address: ");
14707 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14708 break;
14709 case DW_FORM_block2:
14710 case DW_FORM_block4:
14711 case DW_FORM_block:
14712 case DW_FORM_block1:
14713 fprintf_unfiltered (f, "block: size %d",
14714 DW_BLOCK (&die->attrs[i])->size);
14715 break;
14716 case DW_FORM_exprloc:
14717 fprintf_unfiltered (f, "expression: size %u",
14718 DW_BLOCK (&die->attrs[i])->size);
14719 break;
14720 case DW_FORM_ref_addr:
14721 fprintf_unfiltered (f, "ref address: ");
14722 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14723 break;
14724 case DW_FORM_ref1:
14725 case DW_FORM_ref2:
14726 case DW_FORM_ref4:
14727 case DW_FORM_ref8:
14728 case DW_FORM_ref_udata:
14729 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14730 (long) (DW_UNSND (&die->attrs[i])));
14731 break;
14732 case DW_FORM_data1:
14733 case DW_FORM_data2:
14734 case DW_FORM_data4:
14735 case DW_FORM_data8:
14736 case DW_FORM_udata:
14737 case DW_FORM_sdata:
14738 fprintf_unfiltered (f, "constant: %s",
14739 pulongest (DW_UNSND (&die->attrs[i])));
14740 break;
14741 case DW_FORM_sec_offset:
14742 fprintf_unfiltered (f, "section offset: %s",
14743 pulongest (DW_UNSND (&die->attrs[i])));
14744 break;
14745 case DW_FORM_ref_sig8:
14746 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14747 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14748 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14749 else
14750 fprintf_unfiltered (f, "signatured type, offset: unknown");
14751 break;
14752 case DW_FORM_string:
14753 case DW_FORM_strp:
14754 case DW_FORM_GNU_str_index:
14755 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14756 DW_STRING (&die->attrs[i])
14757 ? DW_STRING (&die->attrs[i]) : "",
14758 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14759 break;
14760 case DW_FORM_flag:
14761 if (DW_UNSND (&die->attrs[i]))
14762 fprintf_unfiltered (f, "flag: TRUE");
14763 else
14764 fprintf_unfiltered (f, "flag: FALSE");
14765 break;
14766 case DW_FORM_flag_present:
14767 fprintf_unfiltered (f, "flag: TRUE");
14768 break;
14769 case DW_FORM_indirect:
14770 /* The reader will have reduced the indirect form to
14771 the "base form" so this form should not occur. */
14772 fprintf_unfiltered (f,
14773 "unexpected attribute form: DW_FORM_indirect");
14774 break;
14775 default:
14776 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14777 die->attrs[i].form);
14778 break;
14779 }
14780 fprintf_unfiltered (f, "\n");
14781 }
14782 }
14783
14784 static void
14785 dump_die_for_error (struct die_info *die)
14786 {
14787 dump_die_shallow (gdb_stderr, 0, die);
14788 }
14789
14790 static void
14791 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14792 {
14793 int indent = level * 4;
14794
14795 gdb_assert (die != NULL);
14796
14797 if (level >= max_level)
14798 return;
14799
14800 dump_die_shallow (f, indent, die);
14801
14802 if (die->child != NULL)
14803 {
14804 print_spaces (indent, f);
14805 fprintf_unfiltered (f, " Children:");
14806 if (level + 1 < max_level)
14807 {
14808 fprintf_unfiltered (f, "\n");
14809 dump_die_1 (f, level + 1, max_level, die->child);
14810 }
14811 else
14812 {
14813 fprintf_unfiltered (f,
14814 " [not printed, max nesting level reached]\n");
14815 }
14816 }
14817
14818 if (die->sibling != NULL && level > 0)
14819 {
14820 dump_die_1 (f, level, max_level, die->sibling);
14821 }
14822 }
14823
14824 /* This is called from the pdie macro in gdbinit.in.
14825 It's not static so gcc will keep a copy callable from gdb. */
14826
14827 void
14828 dump_die (struct die_info *die, int max_level)
14829 {
14830 dump_die_1 (gdb_stdlog, 0, max_level, die);
14831 }
14832
14833 static void
14834 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14835 {
14836 void **slot;
14837
14838 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14839 INSERT);
14840
14841 *slot = die;
14842 }
14843
14844 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14845 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14846
14847 static int
14848 is_ref_attr (struct attribute *attr)
14849 {
14850 switch (attr->form)
14851 {
14852 case DW_FORM_ref_addr:
14853 case DW_FORM_ref1:
14854 case DW_FORM_ref2:
14855 case DW_FORM_ref4:
14856 case DW_FORM_ref8:
14857 case DW_FORM_ref_udata:
14858 return 1;
14859 default:
14860 return 0;
14861 }
14862 }
14863
14864 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14865 required kind. */
14866
14867 static sect_offset
14868 dwarf2_get_ref_die_offset (struct attribute *attr)
14869 {
14870 sect_offset retval = { DW_UNSND (attr) };
14871
14872 if (is_ref_attr (attr))
14873 return retval;
14874
14875 retval.sect_off = 0;
14876 complaint (&symfile_complaints,
14877 _("unsupported die ref attribute form: '%s'"),
14878 dwarf_form_name (attr->form));
14879 return retval;
14880 }
14881
14882 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14883 * the value held by the attribute is not constant. */
14884
14885 static LONGEST
14886 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14887 {
14888 if (attr->form == DW_FORM_sdata)
14889 return DW_SND (attr);
14890 else if (attr->form == DW_FORM_udata
14891 || attr->form == DW_FORM_data1
14892 || attr->form == DW_FORM_data2
14893 || attr->form == DW_FORM_data4
14894 || attr->form == DW_FORM_data8)
14895 return DW_UNSND (attr);
14896 else
14897 {
14898 complaint (&symfile_complaints,
14899 _("Attribute value is not a constant (%s)"),
14900 dwarf_form_name (attr->form));
14901 return default_value;
14902 }
14903 }
14904
14905 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14906 unit and add it to our queue.
14907 The result is non-zero if PER_CU was queued, otherwise the result is zero
14908 meaning either PER_CU is already queued or it is already loaded. */
14909
14910 static int
14911 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14912 struct dwarf2_per_cu_data *per_cu,
14913 enum language pretend_language)
14914 {
14915 /* We may arrive here during partial symbol reading, if we need full
14916 DIEs to process an unusual case (e.g. template arguments). Do
14917 not queue PER_CU, just tell our caller to load its DIEs. */
14918 if (dwarf2_per_objfile->reading_partial_symbols)
14919 {
14920 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14921 return 1;
14922 return 0;
14923 }
14924
14925 /* Mark the dependence relation so that we don't flush PER_CU
14926 too early. */
14927 dwarf2_add_dependence (this_cu, per_cu);
14928
14929 /* If it's already on the queue, we have nothing to do. */
14930 if (per_cu->queued)
14931 return 0;
14932
14933 /* If the compilation unit is already loaded, just mark it as
14934 used. */
14935 if (per_cu->cu != NULL)
14936 {
14937 per_cu->cu->last_used = 0;
14938 return 0;
14939 }
14940
14941 /* Add it to the queue. */
14942 queue_comp_unit (per_cu, pretend_language);
14943
14944 return 1;
14945 }
14946
14947 /* Follow reference or signature attribute ATTR of SRC_DIE.
14948 On entry *REF_CU is the CU of SRC_DIE.
14949 On exit *REF_CU is the CU of the result. */
14950
14951 static struct die_info *
14952 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14953 struct dwarf2_cu **ref_cu)
14954 {
14955 struct die_info *die;
14956
14957 if (is_ref_attr (attr))
14958 die = follow_die_ref (src_die, attr, ref_cu);
14959 else if (attr->form == DW_FORM_ref_sig8)
14960 die = follow_die_sig (src_die, attr, ref_cu);
14961 else
14962 {
14963 dump_die_for_error (src_die);
14964 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14965 (*ref_cu)->objfile->name);
14966 }
14967
14968 return die;
14969 }
14970
14971 /* Follow reference OFFSET.
14972 On entry *REF_CU is the CU of the source die referencing OFFSET.
14973 On exit *REF_CU is the CU of the result.
14974 Returns NULL if OFFSET is invalid. */
14975
14976 static struct die_info *
14977 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14978 {
14979 struct die_info temp_die;
14980 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14981
14982 gdb_assert (cu->per_cu != NULL);
14983
14984 target_cu = cu;
14985
14986 if (cu->per_cu->is_debug_types)
14987 {
14988 /* .debug_types CUs cannot reference anything outside their CU.
14989 If they need to, they have to reference a signatured type via
14990 DW_FORM_ref_sig8. */
14991 if (! offset_in_cu_p (&cu->header, offset))
14992 return NULL;
14993 }
14994 else if (! offset_in_cu_p (&cu->header, offset))
14995 {
14996 struct dwarf2_per_cu_data *per_cu;
14997
14998 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14999
15000 /* If necessary, add it to the queue and load its DIEs. */
15001 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15002 load_full_comp_unit (per_cu, cu->language);
15003
15004 target_cu = per_cu->cu;
15005 }
15006 else if (cu->dies == NULL)
15007 {
15008 /* We're loading full DIEs during partial symbol reading. */
15009 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15010 load_full_comp_unit (cu->per_cu, language_minimal);
15011 }
15012
15013 *ref_cu = target_cu;
15014 temp_die.offset = offset;
15015 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15016 }
15017
15018 /* Follow reference attribute ATTR of SRC_DIE.
15019 On entry *REF_CU is the CU of SRC_DIE.
15020 On exit *REF_CU is the CU of the result. */
15021
15022 static struct die_info *
15023 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15024 struct dwarf2_cu **ref_cu)
15025 {
15026 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15027 struct dwarf2_cu *cu = *ref_cu;
15028 struct die_info *die;
15029
15030 die = follow_die_offset (offset, ref_cu);
15031 if (!die)
15032 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15033 "at 0x%x [in module %s]"),
15034 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15035
15036 return die;
15037 }
15038
15039 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15040 Returned value is intended for DW_OP_call*. Returned
15041 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15042
15043 struct dwarf2_locexpr_baton
15044 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15045 struct dwarf2_per_cu_data *per_cu,
15046 CORE_ADDR (*get_frame_pc) (void *baton),
15047 void *baton)
15048 {
15049 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15050 struct dwarf2_cu *cu;
15051 struct die_info *die;
15052 struct attribute *attr;
15053 struct dwarf2_locexpr_baton retval;
15054
15055 dw2_setup (per_cu->objfile);
15056
15057 if (per_cu->cu == NULL)
15058 load_cu (per_cu);
15059 cu = per_cu->cu;
15060
15061 die = follow_die_offset (offset, &cu);
15062 if (!die)
15063 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15064 offset.sect_off, per_cu->objfile->name);
15065
15066 attr = dwarf2_attr (die, DW_AT_location, cu);
15067 if (!attr)
15068 {
15069 /* DWARF: "If there is no such attribute, then there is no effect.".
15070 DATA is ignored if SIZE is 0. */
15071
15072 retval.data = NULL;
15073 retval.size = 0;
15074 }
15075 else if (attr_form_is_section_offset (attr))
15076 {
15077 struct dwarf2_loclist_baton loclist_baton;
15078 CORE_ADDR pc = (*get_frame_pc) (baton);
15079 size_t size;
15080
15081 fill_in_loclist_baton (cu, &loclist_baton, attr);
15082
15083 retval.data = dwarf2_find_location_expression (&loclist_baton,
15084 &size, pc);
15085 retval.size = size;
15086 }
15087 else
15088 {
15089 if (!attr_form_is_block (attr))
15090 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15091 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15092 offset.sect_off, per_cu->objfile->name);
15093
15094 retval.data = DW_BLOCK (attr)->data;
15095 retval.size = DW_BLOCK (attr)->size;
15096 }
15097 retval.per_cu = cu->per_cu;
15098
15099 age_cached_comp_units ();
15100
15101 return retval;
15102 }
15103
15104 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15105 PER_CU. */
15106
15107 struct type *
15108 dwarf2_get_die_type (cu_offset die_offset,
15109 struct dwarf2_per_cu_data *per_cu)
15110 {
15111 sect_offset die_offset_sect;
15112
15113 dw2_setup (per_cu->objfile);
15114
15115 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15116 return get_die_type_at_offset (die_offset_sect, per_cu);
15117 }
15118
15119 /* Follow the signature attribute ATTR in SRC_DIE.
15120 On entry *REF_CU is the CU of SRC_DIE.
15121 On exit *REF_CU is the CU of the result. */
15122
15123 static struct die_info *
15124 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15125 struct dwarf2_cu **ref_cu)
15126 {
15127 struct objfile *objfile = (*ref_cu)->objfile;
15128 struct die_info temp_die;
15129 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15130 struct dwarf2_cu *sig_cu;
15131 struct die_info *die;
15132
15133 /* sig_type will be NULL if the signatured type is missing from
15134 the debug info. */
15135 if (sig_type == NULL)
15136 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15137 "at 0x%x [in module %s]"),
15138 src_die->offset.sect_off, objfile->name);
15139
15140 /* If necessary, add it to the queue and load its DIEs. */
15141
15142 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15143 read_signatured_type (sig_type);
15144
15145 gdb_assert (sig_type->per_cu.cu != NULL);
15146
15147 sig_cu = sig_type->per_cu.cu;
15148 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15149 temp_die.offset = sig_type->type_offset_in_section;
15150 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15151 temp_die.offset.sect_off);
15152 if (die)
15153 {
15154 *ref_cu = sig_cu;
15155 return die;
15156 }
15157
15158 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15159 "from DIE at 0x%x [in module %s]"),
15160 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15161 }
15162
15163 /* Given an offset of a signatured type, return its signatured_type. */
15164
15165 static struct signatured_type *
15166 lookup_signatured_type_at_offset (struct objfile *objfile,
15167 struct dwarf2_section_info *section,
15168 sect_offset offset)
15169 {
15170 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15171 unsigned int length, initial_length_size;
15172 unsigned int sig_offset;
15173 struct signatured_type find_entry, *sig_type;
15174
15175 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15176 sig_offset = (initial_length_size
15177 + 2 /*version*/
15178 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15179 + 1 /*address_size*/);
15180 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15181 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15182
15183 /* This is only used to lookup previously recorded types.
15184 If we didn't find it, it's our bug. */
15185 gdb_assert (sig_type != NULL);
15186 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15187
15188 return sig_type;
15189 }
15190
15191 /* Load the DIEs associated with type unit PER_CU into memory. */
15192
15193 static void
15194 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15195 {
15196 struct objfile *objfile = per_cu->objfile;
15197 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15198 sect_offset offset = per_cu->offset;
15199 struct signatured_type *sig_type;
15200
15201 dwarf2_read_section (objfile, sect);
15202
15203 /* We have the section offset, but we need the signature to do the
15204 hash table lookup. */
15205 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15206 the signature to assert we found the right one.
15207 Ok, but it's a lot of work. We should simplify things so any needed
15208 assert doesn't require all this clumsiness. */
15209 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15210
15211 gdb_assert (&sig_type->per_cu == per_cu);
15212 gdb_assert (sig_type->per_cu.cu == NULL);
15213
15214 read_signatured_type (sig_type);
15215
15216 gdb_assert (sig_type->per_cu.cu != NULL);
15217 }
15218
15219 /* die_reader_func for read_signatured_type.
15220 This is identical to load_full_comp_unit_reader,
15221 but is kept separate for now. */
15222
15223 static void
15224 read_signatured_type_reader (const struct die_reader_specs *reader,
15225 gdb_byte *info_ptr,
15226 struct die_info *comp_unit_die,
15227 int has_children,
15228 void *data)
15229 {
15230 struct dwarf2_cu *cu = reader->cu;
15231
15232 gdb_assert (cu->die_hash == NULL);
15233 cu->die_hash =
15234 htab_create_alloc_ex (cu->header.length / 12,
15235 die_hash,
15236 die_eq,
15237 NULL,
15238 &cu->comp_unit_obstack,
15239 hashtab_obstack_allocate,
15240 dummy_obstack_deallocate);
15241
15242 if (has_children)
15243 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15244 &info_ptr, comp_unit_die);
15245 cu->dies = comp_unit_die;
15246 /* comp_unit_die is not stored in die_hash, no need. */
15247
15248 /* We try not to read any attributes in this function, because not
15249 all CUs needed for references have been loaded yet, and symbol
15250 table processing isn't initialized. But we have to set the CU language,
15251 or we won't be able to build types correctly.
15252 Similarly, if we do not read the producer, we can not apply
15253 producer-specific interpretation. */
15254 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15255 }
15256
15257 /* Read in a signatured type and build its CU and DIEs.
15258 If the type is a stub for the real type in a DWO file,
15259 read in the real type from the DWO file as well. */
15260
15261 static void
15262 read_signatured_type (struct signatured_type *sig_type)
15263 {
15264 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15265
15266 gdb_assert (per_cu->is_debug_types);
15267 gdb_assert (per_cu->cu == NULL);
15268
15269 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15270 }
15271
15272 /* Decode simple location descriptions.
15273 Given a pointer to a dwarf block that defines a location, compute
15274 the location and return the value.
15275
15276 NOTE drow/2003-11-18: This function is called in two situations
15277 now: for the address of static or global variables (partial symbols
15278 only) and for offsets into structures which are expected to be
15279 (more or less) constant. The partial symbol case should go away,
15280 and only the constant case should remain. That will let this
15281 function complain more accurately. A few special modes are allowed
15282 without complaint for global variables (for instance, global
15283 register values and thread-local values).
15284
15285 A location description containing no operations indicates that the
15286 object is optimized out. The return value is 0 for that case.
15287 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15288 callers will only want a very basic result and this can become a
15289 complaint.
15290
15291 Note that stack[0] is unused except as a default error return. */
15292
15293 static CORE_ADDR
15294 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15295 {
15296 struct objfile *objfile = cu->objfile;
15297 int i;
15298 int size = blk->size;
15299 gdb_byte *data = blk->data;
15300 CORE_ADDR stack[64];
15301 int stacki;
15302 unsigned int bytes_read, unsnd;
15303 gdb_byte op;
15304
15305 i = 0;
15306 stacki = 0;
15307 stack[stacki] = 0;
15308 stack[++stacki] = 0;
15309
15310 while (i < size)
15311 {
15312 op = data[i++];
15313 switch (op)
15314 {
15315 case DW_OP_lit0:
15316 case DW_OP_lit1:
15317 case DW_OP_lit2:
15318 case DW_OP_lit3:
15319 case DW_OP_lit4:
15320 case DW_OP_lit5:
15321 case DW_OP_lit6:
15322 case DW_OP_lit7:
15323 case DW_OP_lit8:
15324 case DW_OP_lit9:
15325 case DW_OP_lit10:
15326 case DW_OP_lit11:
15327 case DW_OP_lit12:
15328 case DW_OP_lit13:
15329 case DW_OP_lit14:
15330 case DW_OP_lit15:
15331 case DW_OP_lit16:
15332 case DW_OP_lit17:
15333 case DW_OP_lit18:
15334 case DW_OP_lit19:
15335 case DW_OP_lit20:
15336 case DW_OP_lit21:
15337 case DW_OP_lit22:
15338 case DW_OP_lit23:
15339 case DW_OP_lit24:
15340 case DW_OP_lit25:
15341 case DW_OP_lit26:
15342 case DW_OP_lit27:
15343 case DW_OP_lit28:
15344 case DW_OP_lit29:
15345 case DW_OP_lit30:
15346 case DW_OP_lit31:
15347 stack[++stacki] = op - DW_OP_lit0;
15348 break;
15349
15350 case DW_OP_reg0:
15351 case DW_OP_reg1:
15352 case DW_OP_reg2:
15353 case DW_OP_reg3:
15354 case DW_OP_reg4:
15355 case DW_OP_reg5:
15356 case DW_OP_reg6:
15357 case DW_OP_reg7:
15358 case DW_OP_reg8:
15359 case DW_OP_reg9:
15360 case DW_OP_reg10:
15361 case DW_OP_reg11:
15362 case DW_OP_reg12:
15363 case DW_OP_reg13:
15364 case DW_OP_reg14:
15365 case DW_OP_reg15:
15366 case DW_OP_reg16:
15367 case DW_OP_reg17:
15368 case DW_OP_reg18:
15369 case DW_OP_reg19:
15370 case DW_OP_reg20:
15371 case DW_OP_reg21:
15372 case DW_OP_reg22:
15373 case DW_OP_reg23:
15374 case DW_OP_reg24:
15375 case DW_OP_reg25:
15376 case DW_OP_reg26:
15377 case DW_OP_reg27:
15378 case DW_OP_reg28:
15379 case DW_OP_reg29:
15380 case DW_OP_reg30:
15381 case DW_OP_reg31:
15382 stack[++stacki] = op - DW_OP_reg0;
15383 if (i < size)
15384 dwarf2_complex_location_expr_complaint ();
15385 break;
15386
15387 case DW_OP_regx:
15388 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15389 i += bytes_read;
15390 stack[++stacki] = unsnd;
15391 if (i < size)
15392 dwarf2_complex_location_expr_complaint ();
15393 break;
15394
15395 case DW_OP_addr:
15396 stack[++stacki] = read_address (objfile->obfd, &data[i],
15397 cu, &bytes_read);
15398 i += bytes_read;
15399 break;
15400
15401 case DW_OP_const1u:
15402 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15403 i += 1;
15404 break;
15405
15406 case DW_OP_const1s:
15407 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15408 i += 1;
15409 break;
15410
15411 case DW_OP_const2u:
15412 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15413 i += 2;
15414 break;
15415
15416 case DW_OP_const2s:
15417 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15418 i += 2;
15419 break;
15420
15421 case DW_OP_const4u:
15422 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15423 i += 4;
15424 break;
15425
15426 case DW_OP_const4s:
15427 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15428 i += 4;
15429 break;
15430
15431 case DW_OP_const8u:
15432 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15433 i += 8;
15434 break;
15435
15436 case DW_OP_constu:
15437 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15438 &bytes_read);
15439 i += bytes_read;
15440 break;
15441
15442 case DW_OP_consts:
15443 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15444 i += bytes_read;
15445 break;
15446
15447 case DW_OP_dup:
15448 stack[stacki + 1] = stack[stacki];
15449 stacki++;
15450 break;
15451
15452 case DW_OP_plus:
15453 stack[stacki - 1] += stack[stacki];
15454 stacki--;
15455 break;
15456
15457 case DW_OP_plus_uconst:
15458 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15459 &bytes_read);
15460 i += bytes_read;
15461 break;
15462
15463 case DW_OP_minus:
15464 stack[stacki - 1] -= stack[stacki];
15465 stacki--;
15466 break;
15467
15468 case DW_OP_deref:
15469 /* If we're not the last op, then we definitely can't encode
15470 this using GDB's address_class enum. This is valid for partial
15471 global symbols, although the variable's address will be bogus
15472 in the psymtab. */
15473 if (i < size)
15474 dwarf2_complex_location_expr_complaint ();
15475 break;
15476
15477 case DW_OP_GNU_push_tls_address:
15478 /* The top of the stack has the offset from the beginning
15479 of the thread control block at which the variable is located. */
15480 /* Nothing should follow this operator, so the top of stack would
15481 be returned. */
15482 /* This is valid for partial global symbols, but the variable's
15483 address will be bogus in the psymtab. Make it always at least
15484 non-zero to not look as a variable garbage collected by linker
15485 which have DW_OP_addr 0. */
15486 if (i < size)
15487 dwarf2_complex_location_expr_complaint ();
15488 stack[stacki]++;
15489 break;
15490
15491 case DW_OP_GNU_uninit:
15492 break;
15493
15494 case DW_OP_GNU_addr_index:
15495 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15496 &bytes_read);
15497 i += bytes_read;
15498 break;
15499
15500 default:
15501 {
15502 const char *name = get_DW_OP_name (op);
15503
15504 if (name)
15505 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15506 name);
15507 else
15508 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15509 op);
15510 }
15511
15512 return (stack[stacki]);
15513 }
15514
15515 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15516 outside of the allocated space. Also enforce minimum>0. */
15517 if (stacki >= ARRAY_SIZE (stack) - 1)
15518 {
15519 complaint (&symfile_complaints,
15520 _("location description stack overflow"));
15521 return 0;
15522 }
15523
15524 if (stacki <= 0)
15525 {
15526 complaint (&symfile_complaints,
15527 _("location description stack underflow"));
15528 return 0;
15529 }
15530 }
15531 return (stack[stacki]);
15532 }
15533
15534 /* memory allocation interface */
15535
15536 static struct dwarf_block *
15537 dwarf_alloc_block (struct dwarf2_cu *cu)
15538 {
15539 struct dwarf_block *blk;
15540
15541 blk = (struct dwarf_block *)
15542 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15543 return (blk);
15544 }
15545
15546 static struct abbrev_info *
15547 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15548 {
15549 struct abbrev_info *abbrev;
15550
15551 abbrev = (struct abbrev_info *)
15552 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15553 memset (abbrev, 0, sizeof (struct abbrev_info));
15554 return (abbrev);
15555 }
15556
15557 static struct die_info *
15558 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15559 {
15560 struct die_info *die;
15561 size_t size = sizeof (struct die_info);
15562
15563 if (num_attrs > 1)
15564 size += (num_attrs - 1) * sizeof (struct attribute);
15565
15566 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15567 memset (die, 0, sizeof (struct die_info));
15568 return (die);
15569 }
15570
15571 \f
15572 /* Macro support. */
15573
15574 /* Return the full name of file number I in *LH's file name table.
15575 Use COMP_DIR as the name of the current directory of the
15576 compilation. The result is allocated using xmalloc; the caller is
15577 responsible for freeing it. */
15578 static char *
15579 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15580 {
15581 /* Is the file number a valid index into the line header's file name
15582 table? Remember that file numbers start with one, not zero. */
15583 if (1 <= file && file <= lh->num_file_names)
15584 {
15585 struct file_entry *fe = &lh->file_names[file - 1];
15586
15587 if (IS_ABSOLUTE_PATH (fe->name))
15588 return xstrdup (fe->name);
15589 else
15590 {
15591 const char *dir;
15592 int dir_len;
15593 char *full_name;
15594
15595 if (fe->dir_index)
15596 dir = lh->include_dirs[fe->dir_index - 1];
15597 else
15598 dir = comp_dir;
15599
15600 if (dir)
15601 {
15602 dir_len = strlen (dir);
15603 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15604 strcpy (full_name, dir);
15605 full_name[dir_len] = '/';
15606 strcpy (full_name + dir_len + 1, fe->name);
15607 return full_name;
15608 }
15609 else
15610 return xstrdup (fe->name);
15611 }
15612 }
15613 else
15614 {
15615 /* The compiler produced a bogus file number. We can at least
15616 record the macro definitions made in the file, even if we
15617 won't be able to find the file by name. */
15618 char fake_name[80];
15619
15620 sprintf (fake_name, "<bad macro file number %d>", file);
15621
15622 complaint (&symfile_complaints,
15623 _("bad file number in macro information (%d)"),
15624 file);
15625
15626 return xstrdup (fake_name);
15627 }
15628 }
15629
15630
15631 static struct macro_source_file *
15632 macro_start_file (int file, int line,
15633 struct macro_source_file *current_file,
15634 const char *comp_dir,
15635 struct line_header *lh, struct objfile *objfile)
15636 {
15637 /* The full name of this source file. */
15638 char *full_name = file_full_name (file, lh, comp_dir);
15639
15640 /* We don't create a macro table for this compilation unit
15641 at all until we actually get a filename. */
15642 if (! pending_macros)
15643 pending_macros = new_macro_table (&objfile->objfile_obstack,
15644 objfile->macro_cache);
15645
15646 if (! current_file)
15647 {
15648 /* If we have no current file, then this must be the start_file
15649 directive for the compilation unit's main source file. */
15650 current_file = macro_set_main (pending_macros, full_name);
15651 macro_define_special (pending_macros);
15652 }
15653 else
15654 current_file = macro_include (current_file, line, full_name);
15655
15656 xfree (full_name);
15657
15658 return current_file;
15659 }
15660
15661
15662 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15663 followed by a null byte. */
15664 static char *
15665 copy_string (const char *buf, int len)
15666 {
15667 char *s = xmalloc (len + 1);
15668
15669 memcpy (s, buf, len);
15670 s[len] = '\0';
15671 return s;
15672 }
15673
15674
15675 static const char *
15676 consume_improper_spaces (const char *p, const char *body)
15677 {
15678 if (*p == ' ')
15679 {
15680 complaint (&symfile_complaints,
15681 _("macro definition contains spaces "
15682 "in formal argument list:\n`%s'"),
15683 body);
15684
15685 while (*p == ' ')
15686 p++;
15687 }
15688
15689 return p;
15690 }
15691
15692
15693 static void
15694 parse_macro_definition (struct macro_source_file *file, int line,
15695 const char *body)
15696 {
15697 const char *p;
15698
15699 /* The body string takes one of two forms. For object-like macro
15700 definitions, it should be:
15701
15702 <macro name> " " <definition>
15703
15704 For function-like macro definitions, it should be:
15705
15706 <macro name> "() " <definition>
15707 or
15708 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15709
15710 Spaces may appear only where explicitly indicated, and in the
15711 <definition>.
15712
15713 The Dwarf 2 spec says that an object-like macro's name is always
15714 followed by a space, but versions of GCC around March 2002 omit
15715 the space when the macro's definition is the empty string.
15716
15717 The Dwarf 2 spec says that there should be no spaces between the
15718 formal arguments in a function-like macro's formal argument list,
15719 but versions of GCC around March 2002 include spaces after the
15720 commas. */
15721
15722
15723 /* Find the extent of the macro name. The macro name is terminated
15724 by either a space or null character (for an object-like macro) or
15725 an opening paren (for a function-like macro). */
15726 for (p = body; *p; p++)
15727 if (*p == ' ' || *p == '(')
15728 break;
15729
15730 if (*p == ' ' || *p == '\0')
15731 {
15732 /* It's an object-like macro. */
15733 int name_len = p - body;
15734 char *name = copy_string (body, name_len);
15735 const char *replacement;
15736
15737 if (*p == ' ')
15738 replacement = body + name_len + 1;
15739 else
15740 {
15741 dwarf2_macro_malformed_definition_complaint (body);
15742 replacement = body + name_len;
15743 }
15744
15745 macro_define_object (file, line, name, replacement);
15746
15747 xfree (name);
15748 }
15749 else if (*p == '(')
15750 {
15751 /* It's a function-like macro. */
15752 char *name = copy_string (body, p - body);
15753 int argc = 0;
15754 int argv_size = 1;
15755 char **argv = xmalloc (argv_size * sizeof (*argv));
15756
15757 p++;
15758
15759 p = consume_improper_spaces (p, body);
15760
15761 /* Parse the formal argument list. */
15762 while (*p && *p != ')')
15763 {
15764 /* Find the extent of the current argument name. */
15765 const char *arg_start = p;
15766
15767 while (*p && *p != ',' && *p != ')' && *p != ' ')
15768 p++;
15769
15770 if (! *p || p == arg_start)
15771 dwarf2_macro_malformed_definition_complaint (body);
15772 else
15773 {
15774 /* Make sure argv has room for the new argument. */
15775 if (argc >= argv_size)
15776 {
15777 argv_size *= 2;
15778 argv = xrealloc (argv, argv_size * sizeof (*argv));
15779 }
15780
15781 argv[argc++] = copy_string (arg_start, p - arg_start);
15782 }
15783
15784 p = consume_improper_spaces (p, body);
15785
15786 /* Consume the comma, if present. */
15787 if (*p == ',')
15788 {
15789 p++;
15790
15791 p = consume_improper_spaces (p, body);
15792 }
15793 }
15794
15795 if (*p == ')')
15796 {
15797 p++;
15798
15799 if (*p == ' ')
15800 /* Perfectly formed definition, no complaints. */
15801 macro_define_function (file, line, name,
15802 argc, (const char **) argv,
15803 p + 1);
15804 else if (*p == '\0')
15805 {
15806 /* Complain, but do define it. */
15807 dwarf2_macro_malformed_definition_complaint (body);
15808 macro_define_function (file, line, name,
15809 argc, (const char **) argv,
15810 p);
15811 }
15812 else
15813 /* Just complain. */
15814 dwarf2_macro_malformed_definition_complaint (body);
15815 }
15816 else
15817 /* Just complain. */
15818 dwarf2_macro_malformed_definition_complaint (body);
15819
15820 xfree (name);
15821 {
15822 int i;
15823
15824 for (i = 0; i < argc; i++)
15825 xfree (argv[i]);
15826 }
15827 xfree (argv);
15828 }
15829 else
15830 dwarf2_macro_malformed_definition_complaint (body);
15831 }
15832
15833 /* Skip some bytes from BYTES according to the form given in FORM.
15834 Returns the new pointer. */
15835
15836 static gdb_byte *
15837 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
15838 enum dwarf_form form,
15839 unsigned int offset_size,
15840 struct dwarf2_section_info *section)
15841 {
15842 unsigned int bytes_read;
15843
15844 switch (form)
15845 {
15846 case DW_FORM_data1:
15847 case DW_FORM_flag:
15848 ++bytes;
15849 break;
15850
15851 case DW_FORM_data2:
15852 bytes += 2;
15853 break;
15854
15855 case DW_FORM_data4:
15856 bytes += 4;
15857 break;
15858
15859 case DW_FORM_data8:
15860 bytes += 8;
15861 break;
15862
15863 case DW_FORM_string:
15864 read_direct_string (abfd, bytes, &bytes_read);
15865 bytes += bytes_read;
15866 break;
15867
15868 case DW_FORM_sec_offset:
15869 case DW_FORM_strp:
15870 bytes += offset_size;
15871 break;
15872
15873 case DW_FORM_block:
15874 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15875 bytes += bytes_read;
15876 break;
15877
15878 case DW_FORM_block1:
15879 bytes += 1 + read_1_byte (abfd, bytes);
15880 break;
15881 case DW_FORM_block2:
15882 bytes += 2 + read_2_bytes (abfd, bytes);
15883 break;
15884 case DW_FORM_block4:
15885 bytes += 4 + read_4_bytes (abfd, bytes);
15886 break;
15887
15888 case DW_FORM_sdata:
15889 case DW_FORM_udata:
15890 case DW_FORM_GNU_addr_index:
15891 case DW_FORM_GNU_str_index:
15892 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15893 if (bytes == NULL)
15894 {
15895 dwarf2_section_buffer_overflow_complaint (section);
15896 return NULL;
15897 }
15898 break;
15899
15900 default:
15901 {
15902 complain:
15903 complaint (&symfile_complaints,
15904 _("invalid form 0x%x in `%s'"),
15905 form,
15906 section->asection->name);
15907 return NULL;
15908 }
15909 }
15910
15911 return bytes;
15912 }
15913
15914 /* A helper for dwarf_decode_macros that handles skipping an unknown
15915 opcode. Returns an updated pointer to the macro data buffer; or,
15916 on error, issues a complaint and returns NULL. */
15917
15918 static gdb_byte *
15919 skip_unknown_opcode (unsigned int opcode,
15920 gdb_byte **opcode_definitions,
15921 gdb_byte *mac_ptr, gdb_byte *mac_end,
15922 bfd *abfd,
15923 unsigned int offset_size,
15924 struct dwarf2_section_info *section)
15925 {
15926 unsigned int bytes_read, i;
15927 unsigned long arg;
15928 gdb_byte *defn;
15929
15930 if (opcode_definitions[opcode] == NULL)
15931 {
15932 complaint (&symfile_complaints,
15933 _("unrecognized DW_MACFINO opcode 0x%x"),
15934 opcode);
15935 return NULL;
15936 }
15937
15938 defn = opcode_definitions[opcode];
15939 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15940 defn += bytes_read;
15941
15942 for (i = 0; i < arg; ++i)
15943 {
15944 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15945 section);
15946 if (mac_ptr == NULL)
15947 {
15948 /* skip_form_bytes already issued the complaint. */
15949 return NULL;
15950 }
15951 }
15952
15953 return mac_ptr;
15954 }
15955
15956 /* A helper function which parses the header of a macro section.
15957 If the macro section is the extended (for now called "GNU") type,
15958 then this updates *OFFSET_SIZE. Returns a pointer to just after
15959 the header, or issues a complaint and returns NULL on error. */
15960
15961 static gdb_byte *
15962 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15963 bfd *abfd,
15964 gdb_byte *mac_ptr,
15965 unsigned int *offset_size,
15966 int section_is_gnu)
15967 {
15968 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15969
15970 if (section_is_gnu)
15971 {
15972 unsigned int version, flags;
15973
15974 version = read_2_bytes (abfd, mac_ptr);
15975 if (version != 4)
15976 {
15977 complaint (&symfile_complaints,
15978 _("unrecognized version `%d' in .debug_macro section"),
15979 version);
15980 return NULL;
15981 }
15982 mac_ptr += 2;
15983
15984 flags = read_1_byte (abfd, mac_ptr);
15985 ++mac_ptr;
15986 *offset_size = (flags & 1) ? 8 : 4;
15987
15988 if ((flags & 2) != 0)
15989 /* We don't need the line table offset. */
15990 mac_ptr += *offset_size;
15991
15992 /* Vendor opcode descriptions. */
15993 if ((flags & 4) != 0)
15994 {
15995 unsigned int i, count;
15996
15997 count = read_1_byte (abfd, mac_ptr);
15998 ++mac_ptr;
15999 for (i = 0; i < count; ++i)
16000 {
16001 unsigned int opcode, bytes_read;
16002 unsigned long arg;
16003
16004 opcode = read_1_byte (abfd, mac_ptr);
16005 ++mac_ptr;
16006 opcode_definitions[opcode] = mac_ptr;
16007 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16008 mac_ptr += bytes_read;
16009 mac_ptr += arg;
16010 }
16011 }
16012 }
16013
16014 return mac_ptr;
16015 }
16016
16017 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16018 including DW_MACRO_GNU_transparent_include. */
16019
16020 static void
16021 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16022 struct macro_source_file *current_file,
16023 struct line_header *lh, char *comp_dir,
16024 struct dwarf2_section_info *section,
16025 int section_is_gnu,
16026 unsigned int offset_size,
16027 struct objfile *objfile,
16028 htab_t include_hash)
16029 {
16030 enum dwarf_macro_record_type macinfo_type;
16031 int at_commandline;
16032 gdb_byte *opcode_definitions[256];
16033
16034 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16035 &offset_size, section_is_gnu);
16036 if (mac_ptr == NULL)
16037 {
16038 /* We already issued a complaint. */
16039 return;
16040 }
16041
16042 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16043 GDB is still reading the definitions from command line. First
16044 DW_MACINFO_start_file will need to be ignored as it was already executed
16045 to create CURRENT_FILE for the main source holding also the command line
16046 definitions. On first met DW_MACINFO_start_file this flag is reset to
16047 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16048
16049 at_commandline = 1;
16050
16051 do
16052 {
16053 /* Do we at least have room for a macinfo type byte? */
16054 if (mac_ptr >= mac_end)
16055 {
16056 dwarf2_section_buffer_overflow_complaint (section);
16057 break;
16058 }
16059
16060 macinfo_type = read_1_byte (abfd, mac_ptr);
16061 mac_ptr++;
16062
16063 /* Note that we rely on the fact that the corresponding GNU and
16064 DWARF constants are the same. */
16065 switch (macinfo_type)
16066 {
16067 /* A zero macinfo type indicates the end of the macro
16068 information. */
16069 case 0:
16070 break;
16071
16072 case DW_MACRO_GNU_define:
16073 case DW_MACRO_GNU_undef:
16074 case DW_MACRO_GNU_define_indirect:
16075 case DW_MACRO_GNU_undef_indirect:
16076 {
16077 unsigned int bytes_read;
16078 int line;
16079 char *body;
16080 int is_define;
16081
16082 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16083 mac_ptr += bytes_read;
16084
16085 if (macinfo_type == DW_MACRO_GNU_define
16086 || macinfo_type == DW_MACRO_GNU_undef)
16087 {
16088 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16089 mac_ptr += bytes_read;
16090 }
16091 else
16092 {
16093 LONGEST str_offset;
16094
16095 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16096 mac_ptr += offset_size;
16097
16098 body = read_indirect_string_at_offset (abfd, str_offset);
16099 }
16100
16101 is_define = (macinfo_type == DW_MACRO_GNU_define
16102 || macinfo_type == DW_MACRO_GNU_define_indirect);
16103 if (! current_file)
16104 {
16105 /* DWARF violation as no main source is present. */
16106 complaint (&symfile_complaints,
16107 _("debug info with no main source gives macro %s "
16108 "on line %d: %s"),
16109 is_define ? _("definition") : _("undefinition"),
16110 line, body);
16111 break;
16112 }
16113 if ((line == 0 && !at_commandline)
16114 || (line != 0 && at_commandline))
16115 complaint (&symfile_complaints,
16116 _("debug info gives %s macro %s with %s line %d: %s"),
16117 at_commandline ? _("command-line") : _("in-file"),
16118 is_define ? _("definition") : _("undefinition"),
16119 line == 0 ? _("zero") : _("non-zero"), line, body);
16120
16121 if (is_define)
16122 parse_macro_definition (current_file, line, body);
16123 else
16124 {
16125 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16126 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16127 macro_undef (current_file, line, body);
16128 }
16129 }
16130 break;
16131
16132 case DW_MACRO_GNU_start_file:
16133 {
16134 unsigned int bytes_read;
16135 int line, file;
16136
16137 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16138 mac_ptr += bytes_read;
16139 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16140 mac_ptr += bytes_read;
16141
16142 if ((line == 0 && !at_commandline)
16143 || (line != 0 && at_commandline))
16144 complaint (&symfile_complaints,
16145 _("debug info gives source %d included "
16146 "from %s at %s line %d"),
16147 file, at_commandline ? _("command-line") : _("file"),
16148 line == 0 ? _("zero") : _("non-zero"), line);
16149
16150 if (at_commandline)
16151 {
16152 /* This DW_MACRO_GNU_start_file was executed in the
16153 pass one. */
16154 at_commandline = 0;
16155 }
16156 else
16157 current_file = macro_start_file (file, line,
16158 current_file, comp_dir,
16159 lh, objfile);
16160 }
16161 break;
16162
16163 case DW_MACRO_GNU_end_file:
16164 if (! current_file)
16165 complaint (&symfile_complaints,
16166 _("macro debug info has an unmatched "
16167 "`close_file' directive"));
16168 else
16169 {
16170 current_file = current_file->included_by;
16171 if (! current_file)
16172 {
16173 enum dwarf_macro_record_type next_type;
16174
16175 /* GCC circa March 2002 doesn't produce the zero
16176 type byte marking the end of the compilation
16177 unit. Complain if it's not there, but exit no
16178 matter what. */
16179
16180 /* Do we at least have room for a macinfo type byte? */
16181 if (mac_ptr >= mac_end)
16182 {
16183 dwarf2_section_buffer_overflow_complaint (section);
16184 return;
16185 }
16186
16187 /* We don't increment mac_ptr here, so this is just
16188 a look-ahead. */
16189 next_type = read_1_byte (abfd, mac_ptr);
16190 if (next_type != 0)
16191 complaint (&symfile_complaints,
16192 _("no terminating 0-type entry for "
16193 "macros in `.debug_macinfo' section"));
16194
16195 return;
16196 }
16197 }
16198 break;
16199
16200 case DW_MACRO_GNU_transparent_include:
16201 {
16202 LONGEST offset;
16203 void **slot;
16204
16205 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16206 mac_ptr += offset_size;
16207
16208 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16209 if (*slot != NULL)
16210 {
16211 /* This has actually happened; see
16212 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16213 complaint (&symfile_complaints,
16214 _("recursive DW_MACRO_GNU_transparent_include in "
16215 ".debug_macro section"));
16216 }
16217 else
16218 {
16219 *slot = mac_ptr;
16220
16221 dwarf_decode_macro_bytes (abfd,
16222 section->buffer + offset,
16223 mac_end, current_file,
16224 lh, comp_dir,
16225 section, section_is_gnu,
16226 offset_size, objfile, include_hash);
16227
16228 htab_remove_elt (include_hash, mac_ptr);
16229 }
16230 }
16231 break;
16232
16233 case DW_MACINFO_vendor_ext:
16234 if (!section_is_gnu)
16235 {
16236 unsigned int bytes_read;
16237 int constant;
16238
16239 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16240 mac_ptr += bytes_read;
16241 read_direct_string (abfd, mac_ptr, &bytes_read);
16242 mac_ptr += bytes_read;
16243
16244 /* We don't recognize any vendor extensions. */
16245 break;
16246 }
16247 /* FALLTHROUGH */
16248
16249 default:
16250 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16251 mac_ptr, mac_end, abfd, offset_size,
16252 section);
16253 if (mac_ptr == NULL)
16254 return;
16255 break;
16256 }
16257 } while (macinfo_type != 0);
16258 }
16259
16260 static void
16261 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16262 char *comp_dir, bfd *abfd,
16263 struct dwarf2_cu *cu,
16264 struct dwarf2_section_info *section,
16265 int section_is_gnu, const char *section_name)
16266 {
16267 struct objfile *objfile = dwarf2_per_objfile->objfile;
16268 gdb_byte *mac_ptr, *mac_end;
16269 struct macro_source_file *current_file = 0;
16270 enum dwarf_macro_record_type macinfo_type;
16271 unsigned int offset_size = cu->header.offset_size;
16272 gdb_byte *opcode_definitions[256];
16273 struct cleanup *cleanup;
16274 htab_t include_hash;
16275 void **slot;
16276
16277 dwarf2_read_section (objfile, section);
16278 if (section->buffer == NULL)
16279 {
16280 complaint (&symfile_complaints, _("missing %s section"), section_name);
16281 return;
16282 }
16283
16284 /* First pass: Find the name of the base filename.
16285 This filename is needed in order to process all macros whose definition
16286 (or undefinition) comes from the command line. These macros are defined
16287 before the first DW_MACINFO_start_file entry, and yet still need to be
16288 associated to the base file.
16289
16290 To determine the base file name, we scan the macro definitions until we
16291 reach the first DW_MACINFO_start_file entry. We then initialize
16292 CURRENT_FILE accordingly so that any macro definition found before the
16293 first DW_MACINFO_start_file can still be associated to the base file. */
16294
16295 mac_ptr = section->buffer + offset;
16296 mac_end = section->buffer + section->size;
16297
16298 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16299 &offset_size, section_is_gnu);
16300 if (mac_ptr == NULL)
16301 {
16302 /* We already issued a complaint. */
16303 return;
16304 }
16305
16306 do
16307 {
16308 /* Do we at least have room for a macinfo type byte? */
16309 if (mac_ptr >= mac_end)
16310 {
16311 /* Complaint is printed during the second pass as GDB will probably
16312 stop the first pass earlier upon finding
16313 DW_MACINFO_start_file. */
16314 break;
16315 }
16316
16317 macinfo_type = read_1_byte (abfd, mac_ptr);
16318 mac_ptr++;
16319
16320 /* Note that we rely on the fact that the corresponding GNU and
16321 DWARF constants are the same. */
16322 switch (macinfo_type)
16323 {
16324 /* A zero macinfo type indicates the end of the macro
16325 information. */
16326 case 0:
16327 break;
16328
16329 case DW_MACRO_GNU_define:
16330 case DW_MACRO_GNU_undef:
16331 /* Only skip the data by MAC_PTR. */
16332 {
16333 unsigned int bytes_read;
16334
16335 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16336 mac_ptr += bytes_read;
16337 read_direct_string (abfd, mac_ptr, &bytes_read);
16338 mac_ptr += bytes_read;
16339 }
16340 break;
16341
16342 case DW_MACRO_GNU_start_file:
16343 {
16344 unsigned int bytes_read;
16345 int line, file;
16346
16347 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16348 mac_ptr += bytes_read;
16349 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16350 mac_ptr += bytes_read;
16351
16352 current_file = macro_start_file (file, line, current_file,
16353 comp_dir, lh, objfile);
16354 }
16355 break;
16356
16357 case DW_MACRO_GNU_end_file:
16358 /* No data to skip by MAC_PTR. */
16359 break;
16360
16361 case DW_MACRO_GNU_define_indirect:
16362 case DW_MACRO_GNU_undef_indirect:
16363 {
16364 unsigned int bytes_read;
16365
16366 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16367 mac_ptr += bytes_read;
16368 mac_ptr += offset_size;
16369 }
16370 break;
16371
16372 case DW_MACRO_GNU_transparent_include:
16373 /* Note that, according to the spec, a transparent include
16374 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16375 skip this opcode. */
16376 mac_ptr += offset_size;
16377 break;
16378
16379 case DW_MACINFO_vendor_ext:
16380 /* Only skip the data by MAC_PTR. */
16381 if (!section_is_gnu)
16382 {
16383 unsigned int bytes_read;
16384
16385 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16386 mac_ptr += bytes_read;
16387 read_direct_string (abfd, mac_ptr, &bytes_read);
16388 mac_ptr += bytes_read;
16389 }
16390 /* FALLTHROUGH */
16391
16392 default:
16393 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16394 mac_ptr, mac_end, abfd, offset_size,
16395 section);
16396 if (mac_ptr == NULL)
16397 return;
16398 break;
16399 }
16400 } while (macinfo_type != 0 && current_file == NULL);
16401
16402 /* Second pass: Process all entries.
16403
16404 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16405 command-line macro definitions/undefinitions. This flag is unset when we
16406 reach the first DW_MACINFO_start_file entry. */
16407
16408 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16409 NULL, xcalloc, xfree);
16410 cleanup = make_cleanup_htab_delete (include_hash);
16411 mac_ptr = section->buffer + offset;
16412 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16413 *slot = mac_ptr;
16414 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16415 current_file, lh, comp_dir, section, section_is_gnu,
16416 offset_size, objfile, include_hash);
16417 do_cleanups (cleanup);
16418 }
16419
16420 /* Check if the attribute's form is a DW_FORM_block*
16421 if so return true else false. */
16422
16423 static int
16424 attr_form_is_block (struct attribute *attr)
16425 {
16426 return (attr == NULL ? 0 :
16427 attr->form == DW_FORM_block1
16428 || attr->form == DW_FORM_block2
16429 || attr->form == DW_FORM_block4
16430 || attr->form == DW_FORM_block
16431 || attr->form == DW_FORM_exprloc);
16432 }
16433
16434 /* Return non-zero if ATTR's value is a section offset --- classes
16435 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16436 You may use DW_UNSND (attr) to retrieve such offsets.
16437
16438 Section 7.5.4, "Attribute Encodings", explains that no attribute
16439 may have a value that belongs to more than one of these classes; it
16440 would be ambiguous if we did, because we use the same forms for all
16441 of them. */
16442
16443 static int
16444 attr_form_is_section_offset (struct attribute *attr)
16445 {
16446 return (attr->form == DW_FORM_data4
16447 || attr->form == DW_FORM_data8
16448 || attr->form == DW_FORM_sec_offset);
16449 }
16450
16451 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16452 zero otherwise. When this function returns true, you can apply
16453 dwarf2_get_attr_constant_value to it.
16454
16455 However, note that for some attributes you must check
16456 attr_form_is_section_offset before using this test. DW_FORM_data4
16457 and DW_FORM_data8 are members of both the constant class, and of
16458 the classes that contain offsets into other debug sections
16459 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16460 that, if an attribute's can be either a constant or one of the
16461 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16462 taken as section offsets, not constants. */
16463
16464 static int
16465 attr_form_is_constant (struct attribute *attr)
16466 {
16467 switch (attr->form)
16468 {
16469 case DW_FORM_sdata:
16470 case DW_FORM_udata:
16471 case DW_FORM_data1:
16472 case DW_FORM_data2:
16473 case DW_FORM_data4:
16474 case DW_FORM_data8:
16475 return 1;
16476 default:
16477 return 0;
16478 }
16479 }
16480
16481 /* Return the .debug_loc section to use for CU.
16482 For DWO files use .debug_loc.dwo. */
16483
16484 static struct dwarf2_section_info *
16485 cu_debug_loc_section (struct dwarf2_cu *cu)
16486 {
16487 if (cu->dwo_unit)
16488 return &cu->dwo_unit->dwo_file->sections.loc;
16489 return &dwarf2_per_objfile->loc;
16490 }
16491
16492 /* A helper function that fills in a dwarf2_loclist_baton. */
16493
16494 static void
16495 fill_in_loclist_baton (struct dwarf2_cu *cu,
16496 struct dwarf2_loclist_baton *baton,
16497 struct attribute *attr)
16498 {
16499 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16500
16501 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16502
16503 baton->per_cu = cu->per_cu;
16504 gdb_assert (baton->per_cu);
16505 /* We don't know how long the location list is, but make sure we
16506 don't run off the edge of the section. */
16507 baton->size = section->size - DW_UNSND (attr);
16508 baton->data = section->buffer + DW_UNSND (attr);
16509 baton->base_address = cu->base_address;
16510 baton->from_dwo = cu->dwo_unit != NULL;
16511 }
16512
16513 static void
16514 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16515 struct dwarf2_cu *cu)
16516 {
16517 struct objfile *objfile = dwarf2_per_objfile->objfile;
16518 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16519
16520 if (attr_form_is_section_offset (attr)
16521 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16522 the section. If so, fall through to the complaint in the
16523 other branch. */
16524 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16525 {
16526 struct dwarf2_loclist_baton *baton;
16527
16528 baton = obstack_alloc (&objfile->objfile_obstack,
16529 sizeof (struct dwarf2_loclist_baton));
16530
16531 fill_in_loclist_baton (cu, baton, attr);
16532
16533 if (cu->base_known == 0)
16534 complaint (&symfile_complaints,
16535 _("Location list used without "
16536 "specifying the CU base address."));
16537
16538 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16539 SYMBOL_LOCATION_BATON (sym) = baton;
16540 }
16541 else
16542 {
16543 struct dwarf2_locexpr_baton *baton;
16544
16545 baton = obstack_alloc (&objfile->objfile_obstack,
16546 sizeof (struct dwarf2_locexpr_baton));
16547 baton->per_cu = cu->per_cu;
16548 gdb_assert (baton->per_cu);
16549
16550 if (attr_form_is_block (attr))
16551 {
16552 /* Note that we're just copying the block's data pointer
16553 here, not the actual data. We're still pointing into the
16554 info_buffer for SYM's objfile; right now we never release
16555 that buffer, but when we do clean up properly this may
16556 need to change. */
16557 baton->size = DW_BLOCK (attr)->size;
16558 baton->data = DW_BLOCK (attr)->data;
16559 }
16560 else
16561 {
16562 dwarf2_invalid_attrib_class_complaint ("location description",
16563 SYMBOL_NATURAL_NAME (sym));
16564 baton->size = 0;
16565 }
16566
16567 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16568 SYMBOL_LOCATION_BATON (sym) = baton;
16569 }
16570 }
16571
16572 /* Return the OBJFILE associated with the compilation unit CU. If CU
16573 came from a separate debuginfo file, then the master objfile is
16574 returned. */
16575
16576 struct objfile *
16577 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16578 {
16579 struct objfile *objfile = per_cu->objfile;
16580
16581 /* Return the master objfile, so that we can report and look up the
16582 correct file containing this variable. */
16583 if (objfile->separate_debug_objfile_backlink)
16584 objfile = objfile->separate_debug_objfile_backlink;
16585
16586 return objfile;
16587 }
16588
16589 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16590 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16591 CU_HEADERP first. */
16592
16593 static const struct comp_unit_head *
16594 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16595 struct dwarf2_per_cu_data *per_cu)
16596 {
16597 struct objfile *objfile;
16598 struct dwarf2_per_objfile *per_objfile;
16599 gdb_byte *info_ptr;
16600
16601 if (per_cu->cu)
16602 return &per_cu->cu->header;
16603
16604 objfile = per_cu->objfile;
16605 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16606 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16607
16608 memset (cu_headerp, 0, sizeof (*cu_headerp));
16609 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16610
16611 return cu_headerp;
16612 }
16613
16614 /* Return the address size given in the compilation unit header for CU. */
16615
16616 int
16617 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16618 {
16619 struct comp_unit_head cu_header_local;
16620 const struct comp_unit_head *cu_headerp;
16621
16622 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16623
16624 return cu_headerp->addr_size;
16625 }
16626
16627 /* Return the offset size given in the compilation unit header for CU. */
16628
16629 int
16630 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16631 {
16632 struct comp_unit_head cu_header_local;
16633 const struct comp_unit_head *cu_headerp;
16634
16635 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16636
16637 return cu_headerp->offset_size;
16638 }
16639
16640 /* See its dwarf2loc.h declaration. */
16641
16642 int
16643 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16644 {
16645 struct comp_unit_head cu_header_local;
16646 const struct comp_unit_head *cu_headerp;
16647
16648 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16649
16650 if (cu_headerp->version == 2)
16651 return cu_headerp->addr_size;
16652 else
16653 return cu_headerp->offset_size;
16654 }
16655
16656 /* Return the text offset of the CU. The returned offset comes from
16657 this CU's objfile. If this objfile came from a separate debuginfo
16658 file, then the offset may be different from the corresponding
16659 offset in the parent objfile. */
16660
16661 CORE_ADDR
16662 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16663 {
16664 struct objfile *objfile = per_cu->objfile;
16665
16666 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16667 }
16668
16669 /* Locate the .debug_info compilation unit from CU's objfile which contains
16670 the DIE at OFFSET. Raises an error on failure. */
16671
16672 static struct dwarf2_per_cu_data *
16673 dwarf2_find_containing_comp_unit (sect_offset offset,
16674 struct objfile *objfile)
16675 {
16676 struct dwarf2_per_cu_data *this_cu;
16677 int low, high;
16678
16679 low = 0;
16680 high = dwarf2_per_objfile->n_comp_units - 1;
16681 while (high > low)
16682 {
16683 int mid = low + (high - low) / 2;
16684
16685 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16686 >= offset.sect_off)
16687 high = mid;
16688 else
16689 low = mid + 1;
16690 }
16691 gdb_assert (low == high);
16692 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16693 > offset.sect_off)
16694 {
16695 if (low == 0)
16696 error (_("Dwarf Error: could not find partial DIE containing "
16697 "offset 0x%lx [in module %s]"),
16698 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16699
16700 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16701 <= offset.sect_off);
16702 return dwarf2_per_objfile->all_comp_units[low-1];
16703 }
16704 else
16705 {
16706 this_cu = dwarf2_per_objfile->all_comp_units[low];
16707 if (low == dwarf2_per_objfile->n_comp_units - 1
16708 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16709 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16710 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16711 return this_cu;
16712 }
16713 }
16714
16715 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16716
16717 static void
16718 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16719 {
16720 memset (cu, 0, sizeof (*cu));
16721 per_cu->cu = cu;
16722 cu->per_cu = per_cu;
16723 cu->objfile = per_cu->objfile;
16724 obstack_init (&cu->comp_unit_obstack);
16725 }
16726
16727 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16728
16729 static void
16730 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16731 enum language pretend_language)
16732 {
16733 struct attribute *attr;
16734
16735 /* Set the language we're debugging. */
16736 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16737 if (attr)
16738 set_cu_language (DW_UNSND (attr), cu);
16739 else
16740 {
16741 cu->language = pretend_language;
16742 cu->language_defn = language_def (cu->language);
16743 }
16744
16745 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16746 if (attr)
16747 cu->producer = DW_STRING (attr);
16748 }
16749
16750 /* Release one cached compilation unit, CU. We unlink it from the tree
16751 of compilation units, but we don't remove it from the read_in_chain;
16752 the caller is responsible for that.
16753 NOTE: DATA is a void * because this function is also used as a
16754 cleanup routine. */
16755
16756 static void
16757 free_heap_comp_unit (void *data)
16758 {
16759 struct dwarf2_cu *cu = data;
16760
16761 gdb_assert (cu->per_cu != NULL);
16762 cu->per_cu->cu = NULL;
16763 cu->per_cu = NULL;
16764
16765 obstack_free (&cu->comp_unit_obstack, NULL);
16766
16767 xfree (cu);
16768 }
16769
16770 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16771 when we're finished with it. We can't free the pointer itself, but be
16772 sure to unlink it from the cache. Also release any associated storage. */
16773
16774 static void
16775 free_stack_comp_unit (void *data)
16776 {
16777 struct dwarf2_cu *cu = data;
16778
16779 gdb_assert (cu->per_cu != NULL);
16780 cu->per_cu->cu = NULL;
16781 cu->per_cu = NULL;
16782
16783 obstack_free (&cu->comp_unit_obstack, NULL);
16784 cu->partial_dies = NULL;
16785 }
16786
16787 /* Free all cached compilation units. */
16788
16789 static void
16790 free_cached_comp_units (void *data)
16791 {
16792 struct dwarf2_per_cu_data *per_cu, **last_chain;
16793
16794 per_cu = dwarf2_per_objfile->read_in_chain;
16795 last_chain = &dwarf2_per_objfile->read_in_chain;
16796 while (per_cu != NULL)
16797 {
16798 struct dwarf2_per_cu_data *next_cu;
16799
16800 next_cu = per_cu->cu->read_in_chain;
16801
16802 free_heap_comp_unit (per_cu->cu);
16803 *last_chain = next_cu;
16804
16805 per_cu = next_cu;
16806 }
16807 }
16808
16809 /* Increase the age counter on each cached compilation unit, and free
16810 any that are too old. */
16811
16812 static void
16813 age_cached_comp_units (void)
16814 {
16815 struct dwarf2_per_cu_data *per_cu, **last_chain;
16816
16817 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16818 per_cu = dwarf2_per_objfile->read_in_chain;
16819 while (per_cu != NULL)
16820 {
16821 per_cu->cu->last_used ++;
16822 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16823 dwarf2_mark (per_cu->cu);
16824 per_cu = per_cu->cu->read_in_chain;
16825 }
16826
16827 per_cu = dwarf2_per_objfile->read_in_chain;
16828 last_chain = &dwarf2_per_objfile->read_in_chain;
16829 while (per_cu != NULL)
16830 {
16831 struct dwarf2_per_cu_data *next_cu;
16832
16833 next_cu = per_cu->cu->read_in_chain;
16834
16835 if (!per_cu->cu->mark)
16836 {
16837 free_heap_comp_unit (per_cu->cu);
16838 *last_chain = next_cu;
16839 }
16840 else
16841 last_chain = &per_cu->cu->read_in_chain;
16842
16843 per_cu = next_cu;
16844 }
16845 }
16846
16847 /* Remove a single compilation unit from the cache. */
16848
16849 static void
16850 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16851 {
16852 struct dwarf2_per_cu_data *per_cu, **last_chain;
16853
16854 per_cu = dwarf2_per_objfile->read_in_chain;
16855 last_chain = &dwarf2_per_objfile->read_in_chain;
16856 while (per_cu != NULL)
16857 {
16858 struct dwarf2_per_cu_data *next_cu;
16859
16860 next_cu = per_cu->cu->read_in_chain;
16861
16862 if (per_cu == target_per_cu)
16863 {
16864 free_heap_comp_unit (per_cu->cu);
16865 per_cu->cu = NULL;
16866 *last_chain = next_cu;
16867 break;
16868 }
16869 else
16870 last_chain = &per_cu->cu->read_in_chain;
16871
16872 per_cu = next_cu;
16873 }
16874 }
16875
16876 /* Release all extra memory associated with OBJFILE. */
16877
16878 void
16879 dwarf2_free_objfile (struct objfile *objfile)
16880 {
16881 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16882
16883 if (dwarf2_per_objfile == NULL)
16884 return;
16885
16886 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16887 free_cached_comp_units (NULL);
16888
16889 if (dwarf2_per_objfile->quick_file_names_table)
16890 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16891
16892 /* Everything else should be on the objfile obstack. */
16893 }
16894
16895 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16896 We store these in a hash table separate from the DIEs, and preserve them
16897 when the DIEs are flushed out of cache.
16898
16899 The CU "per_cu" pointer is needed because offset alone is not enough to
16900 uniquely identify the type. A file may have multiple .debug_types sections,
16901 or the type may come from a DWO file. We have to use something in
16902 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16903 routine, get_die_type_at_offset, from outside this file, and thus won't
16904 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16905 of the objfile. */
16906
16907 struct dwarf2_per_cu_offset_and_type
16908 {
16909 const struct dwarf2_per_cu_data *per_cu;
16910 sect_offset offset;
16911 struct type *type;
16912 };
16913
16914 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16915
16916 static hashval_t
16917 per_cu_offset_and_type_hash (const void *item)
16918 {
16919 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16920
16921 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16922 }
16923
16924 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16925
16926 static int
16927 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16928 {
16929 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16930 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16931
16932 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16933 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16934 }
16935
16936 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16937 table if necessary. For convenience, return TYPE.
16938
16939 The DIEs reading must have careful ordering to:
16940 * Not cause infite loops trying to read in DIEs as a prerequisite for
16941 reading current DIE.
16942 * Not trying to dereference contents of still incompletely read in types
16943 while reading in other DIEs.
16944 * Enable referencing still incompletely read in types just by a pointer to
16945 the type without accessing its fields.
16946
16947 Therefore caller should follow these rules:
16948 * Try to fetch any prerequisite types we may need to build this DIE type
16949 before building the type and calling set_die_type.
16950 * After building type call set_die_type for current DIE as soon as
16951 possible before fetching more types to complete the current type.
16952 * Make the type as complete as possible before fetching more types. */
16953
16954 static struct type *
16955 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16956 {
16957 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16958 struct objfile *objfile = cu->objfile;
16959
16960 /* For Ada types, make sure that the gnat-specific data is always
16961 initialized (if not already set). There are a few types where
16962 we should not be doing so, because the type-specific area is
16963 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16964 where the type-specific area is used to store the floatformat).
16965 But this is not a problem, because the gnat-specific information
16966 is actually not needed for these types. */
16967 if (need_gnat_info (cu)
16968 && TYPE_CODE (type) != TYPE_CODE_FUNC
16969 && TYPE_CODE (type) != TYPE_CODE_FLT
16970 && !HAVE_GNAT_AUX_INFO (type))
16971 INIT_GNAT_SPECIFIC (type);
16972
16973 if (dwarf2_per_objfile->die_type_hash == NULL)
16974 {
16975 dwarf2_per_objfile->die_type_hash =
16976 htab_create_alloc_ex (127,
16977 per_cu_offset_and_type_hash,
16978 per_cu_offset_and_type_eq,
16979 NULL,
16980 &objfile->objfile_obstack,
16981 hashtab_obstack_allocate,
16982 dummy_obstack_deallocate);
16983 }
16984
16985 ofs.per_cu = cu->per_cu;
16986 ofs.offset = die->offset;
16987 ofs.type = type;
16988 slot = (struct dwarf2_per_cu_offset_and_type **)
16989 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16990 if (*slot)
16991 complaint (&symfile_complaints,
16992 _("A problem internal to GDB: DIE 0x%x has type already set"),
16993 die->offset.sect_off);
16994 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16995 **slot = ofs;
16996 return type;
16997 }
16998
16999 /* Look up the type for the die at OFFSET in the appropriate type_hash
17000 table, or return NULL if the die does not have a saved type. */
17001
17002 static struct type *
17003 get_die_type_at_offset (sect_offset offset,
17004 struct dwarf2_per_cu_data *per_cu)
17005 {
17006 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17007
17008 if (dwarf2_per_objfile->die_type_hash == NULL)
17009 return NULL;
17010
17011 ofs.per_cu = per_cu;
17012 ofs.offset = offset;
17013 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17014 if (slot)
17015 return slot->type;
17016 else
17017 return NULL;
17018 }
17019
17020 /* Look up the type for DIE in the appropriate type_hash table,
17021 or return NULL if DIE does not have a saved type. */
17022
17023 static struct type *
17024 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17025 {
17026 return get_die_type_at_offset (die->offset, cu->per_cu);
17027 }
17028
17029 /* Add a dependence relationship from CU to REF_PER_CU. */
17030
17031 static void
17032 dwarf2_add_dependence (struct dwarf2_cu *cu,
17033 struct dwarf2_per_cu_data *ref_per_cu)
17034 {
17035 void **slot;
17036
17037 if (cu->dependencies == NULL)
17038 cu->dependencies
17039 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17040 NULL, &cu->comp_unit_obstack,
17041 hashtab_obstack_allocate,
17042 dummy_obstack_deallocate);
17043
17044 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17045 if (*slot == NULL)
17046 *slot = ref_per_cu;
17047 }
17048
17049 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17050 Set the mark field in every compilation unit in the
17051 cache that we must keep because we are keeping CU. */
17052
17053 static int
17054 dwarf2_mark_helper (void **slot, void *data)
17055 {
17056 struct dwarf2_per_cu_data *per_cu;
17057
17058 per_cu = (struct dwarf2_per_cu_data *) *slot;
17059
17060 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17061 reading of the chain. As such dependencies remain valid it is not much
17062 useful to track and undo them during QUIT cleanups. */
17063 if (per_cu->cu == NULL)
17064 return 1;
17065
17066 if (per_cu->cu->mark)
17067 return 1;
17068 per_cu->cu->mark = 1;
17069
17070 if (per_cu->cu->dependencies != NULL)
17071 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17072
17073 return 1;
17074 }
17075
17076 /* Set the mark field in CU and in every other compilation unit in the
17077 cache that we must keep because we are keeping CU. */
17078
17079 static void
17080 dwarf2_mark (struct dwarf2_cu *cu)
17081 {
17082 if (cu->mark)
17083 return;
17084 cu->mark = 1;
17085 if (cu->dependencies != NULL)
17086 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17087 }
17088
17089 static void
17090 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17091 {
17092 while (per_cu)
17093 {
17094 per_cu->cu->mark = 0;
17095 per_cu = per_cu->cu->read_in_chain;
17096 }
17097 }
17098
17099 /* Trivial hash function for partial_die_info: the hash value of a DIE
17100 is its offset in .debug_info for this objfile. */
17101
17102 static hashval_t
17103 partial_die_hash (const void *item)
17104 {
17105 const struct partial_die_info *part_die = item;
17106
17107 return part_die->offset.sect_off;
17108 }
17109
17110 /* Trivial comparison function for partial_die_info structures: two DIEs
17111 are equal if they have the same offset. */
17112
17113 static int
17114 partial_die_eq (const void *item_lhs, const void *item_rhs)
17115 {
17116 const struct partial_die_info *part_die_lhs = item_lhs;
17117 const struct partial_die_info *part_die_rhs = item_rhs;
17118
17119 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17120 }
17121
17122 static struct cmd_list_element *set_dwarf2_cmdlist;
17123 static struct cmd_list_element *show_dwarf2_cmdlist;
17124
17125 static void
17126 set_dwarf2_cmd (char *args, int from_tty)
17127 {
17128 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17129 }
17130
17131 static void
17132 show_dwarf2_cmd (char *args, int from_tty)
17133 {
17134 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17135 }
17136
17137 /* If section described by INFO was mmapped, munmap it now. */
17138
17139 static void
17140 munmap_section_buffer (struct dwarf2_section_info *info)
17141 {
17142 if (info->map_addr != NULL)
17143 {
17144 #ifdef HAVE_MMAP
17145 int res;
17146
17147 res = munmap (info->map_addr, info->map_len);
17148 gdb_assert (res == 0);
17149 #else
17150 /* Without HAVE_MMAP, we should never be here to begin with. */
17151 gdb_assert_not_reached ("no mmap support");
17152 #endif
17153 }
17154 }
17155
17156 /* munmap debug sections for OBJFILE, if necessary. */
17157
17158 static void
17159 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17160 {
17161 struct dwarf2_per_objfile *data = d;
17162 int ix;
17163 struct dwarf2_section_info *section;
17164
17165 /* This is sorted according to the order they're defined in to make it easier
17166 to keep in sync. */
17167 munmap_section_buffer (&data->info);
17168 munmap_section_buffer (&data->abbrev);
17169 munmap_section_buffer (&data->line);
17170 munmap_section_buffer (&data->loc);
17171 munmap_section_buffer (&data->macinfo);
17172 munmap_section_buffer (&data->macro);
17173 munmap_section_buffer (&data->str);
17174 munmap_section_buffer (&data->ranges);
17175 munmap_section_buffer (&data->addr);
17176 munmap_section_buffer (&data->frame);
17177 munmap_section_buffer (&data->eh_frame);
17178 munmap_section_buffer (&data->gdb_index);
17179
17180 for (ix = 0;
17181 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17182 ++ix)
17183 munmap_section_buffer (section);
17184
17185 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17186 VEC_free (dwarf2_per_cu_ptr,
17187 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17188
17189 VEC_free (dwarf2_section_info_def, data->types);
17190
17191 if (data->dwo_files)
17192 free_dwo_files (data->dwo_files, objfile);
17193 }
17194
17195 \f
17196 /* The "save gdb-index" command. */
17197
17198 /* The contents of the hash table we create when building the string
17199 table. */
17200 struct strtab_entry
17201 {
17202 offset_type offset;
17203 const char *str;
17204 };
17205
17206 /* Hash function for a strtab_entry.
17207
17208 Function is used only during write_hash_table so no index format backward
17209 compatibility is needed. */
17210
17211 static hashval_t
17212 hash_strtab_entry (const void *e)
17213 {
17214 const struct strtab_entry *entry = e;
17215 return mapped_index_string_hash (INT_MAX, entry->str);
17216 }
17217
17218 /* Equality function for a strtab_entry. */
17219
17220 static int
17221 eq_strtab_entry (const void *a, const void *b)
17222 {
17223 const struct strtab_entry *ea = a;
17224 const struct strtab_entry *eb = b;
17225 return !strcmp (ea->str, eb->str);
17226 }
17227
17228 /* Create a strtab_entry hash table. */
17229
17230 static htab_t
17231 create_strtab (void)
17232 {
17233 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17234 xfree, xcalloc, xfree);
17235 }
17236
17237 /* Add a string to the constant pool. Return the string's offset in
17238 host order. */
17239
17240 static offset_type
17241 add_string (htab_t table, struct obstack *cpool, const char *str)
17242 {
17243 void **slot;
17244 struct strtab_entry entry;
17245 struct strtab_entry *result;
17246
17247 entry.str = str;
17248 slot = htab_find_slot (table, &entry, INSERT);
17249 if (*slot)
17250 result = *slot;
17251 else
17252 {
17253 result = XNEW (struct strtab_entry);
17254 result->offset = obstack_object_size (cpool);
17255 result->str = str;
17256 obstack_grow_str0 (cpool, str);
17257 *slot = result;
17258 }
17259 return result->offset;
17260 }
17261
17262 /* An entry in the symbol table. */
17263 struct symtab_index_entry
17264 {
17265 /* The name of the symbol. */
17266 const char *name;
17267 /* The offset of the name in the constant pool. */
17268 offset_type index_offset;
17269 /* A sorted vector of the indices of all the CUs that hold an object
17270 of this name. */
17271 VEC (offset_type) *cu_indices;
17272 };
17273
17274 /* The symbol table. This is a power-of-2-sized hash table. */
17275 struct mapped_symtab
17276 {
17277 offset_type n_elements;
17278 offset_type size;
17279 struct symtab_index_entry **data;
17280 };
17281
17282 /* Hash function for a symtab_index_entry. */
17283
17284 static hashval_t
17285 hash_symtab_entry (const void *e)
17286 {
17287 const struct symtab_index_entry *entry = e;
17288 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17289 sizeof (offset_type) * VEC_length (offset_type,
17290 entry->cu_indices),
17291 0);
17292 }
17293
17294 /* Equality function for a symtab_index_entry. */
17295
17296 static int
17297 eq_symtab_entry (const void *a, const void *b)
17298 {
17299 const struct symtab_index_entry *ea = a;
17300 const struct symtab_index_entry *eb = b;
17301 int len = VEC_length (offset_type, ea->cu_indices);
17302 if (len != VEC_length (offset_type, eb->cu_indices))
17303 return 0;
17304 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17305 VEC_address (offset_type, eb->cu_indices),
17306 sizeof (offset_type) * len);
17307 }
17308
17309 /* Destroy a symtab_index_entry. */
17310
17311 static void
17312 delete_symtab_entry (void *p)
17313 {
17314 struct symtab_index_entry *entry = p;
17315 VEC_free (offset_type, entry->cu_indices);
17316 xfree (entry);
17317 }
17318
17319 /* Create a hash table holding symtab_index_entry objects. */
17320
17321 static htab_t
17322 create_symbol_hash_table (void)
17323 {
17324 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17325 delete_symtab_entry, xcalloc, xfree);
17326 }
17327
17328 /* Create a new mapped symtab object. */
17329
17330 static struct mapped_symtab *
17331 create_mapped_symtab (void)
17332 {
17333 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17334 symtab->n_elements = 0;
17335 symtab->size = 1024;
17336 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17337 return symtab;
17338 }
17339
17340 /* Destroy a mapped_symtab. */
17341
17342 static void
17343 cleanup_mapped_symtab (void *p)
17344 {
17345 struct mapped_symtab *symtab = p;
17346 /* The contents of the array are freed when the other hash table is
17347 destroyed. */
17348 xfree (symtab->data);
17349 xfree (symtab);
17350 }
17351
17352 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17353 the slot.
17354
17355 Function is used only during write_hash_table so no index format backward
17356 compatibility is needed. */
17357
17358 static struct symtab_index_entry **
17359 find_slot (struct mapped_symtab *symtab, const char *name)
17360 {
17361 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17362
17363 index = hash & (symtab->size - 1);
17364 step = ((hash * 17) & (symtab->size - 1)) | 1;
17365
17366 for (;;)
17367 {
17368 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17369 return &symtab->data[index];
17370 index = (index + step) & (symtab->size - 1);
17371 }
17372 }
17373
17374 /* Expand SYMTAB's hash table. */
17375
17376 static void
17377 hash_expand (struct mapped_symtab *symtab)
17378 {
17379 offset_type old_size = symtab->size;
17380 offset_type i;
17381 struct symtab_index_entry **old_entries = symtab->data;
17382
17383 symtab->size *= 2;
17384 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17385
17386 for (i = 0; i < old_size; ++i)
17387 {
17388 if (old_entries[i])
17389 {
17390 struct symtab_index_entry **slot = find_slot (symtab,
17391 old_entries[i]->name);
17392 *slot = old_entries[i];
17393 }
17394 }
17395
17396 xfree (old_entries);
17397 }
17398
17399 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17400 is the index of the CU in which the symbol appears. */
17401
17402 static void
17403 add_index_entry (struct mapped_symtab *symtab, const char *name,
17404 offset_type cu_index)
17405 {
17406 struct symtab_index_entry **slot;
17407
17408 ++symtab->n_elements;
17409 if (4 * symtab->n_elements / 3 >= symtab->size)
17410 hash_expand (symtab);
17411
17412 slot = find_slot (symtab, name);
17413 if (!*slot)
17414 {
17415 *slot = XNEW (struct symtab_index_entry);
17416 (*slot)->name = name;
17417 (*slot)->cu_indices = NULL;
17418 }
17419 /* Don't push an index twice. Due to how we add entries we only
17420 have to check the last one. */
17421 if (VEC_empty (offset_type, (*slot)->cu_indices)
17422 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17423 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17424 }
17425
17426 /* Add a vector of indices to the constant pool. */
17427
17428 static offset_type
17429 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17430 struct symtab_index_entry *entry)
17431 {
17432 void **slot;
17433
17434 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17435 if (!*slot)
17436 {
17437 offset_type len = VEC_length (offset_type, entry->cu_indices);
17438 offset_type val = MAYBE_SWAP (len);
17439 offset_type iter;
17440 int i;
17441
17442 *slot = entry;
17443 entry->index_offset = obstack_object_size (cpool);
17444
17445 obstack_grow (cpool, &val, sizeof (val));
17446 for (i = 0;
17447 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17448 ++i)
17449 {
17450 val = MAYBE_SWAP (iter);
17451 obstack_grow (cpool, &val, sizeof (val));
17452 }
17453 }
17454 else
17455 {
17456 struct symtab_index_entry *old_entry = *slot;
17457 entry->index_offset = old_entry->index_offset;
17458 entry = old_entry;
17459 }
17460 return entry->index_offset;
17461 }
17462
17463 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17464 constant pool entries going into the obstack CPOOL. */
17465
17466 static void
17467 write_hash_table (struct mapped_symtab *symtab,
17468 struct obstack *output, struct obstack *cpool)
17469 {
17470 offset_type i;
17471 htab_t symbol_hash_table;
17472 htab_t str_table;
17473
17474 symbol_hash_table = create_symbol_hash_table ();
17475 str_table = create_strtab ();
17476
17477 /* We add all the index vectors to the constant pool first, to
17478 ensure alignment is ok. */
17479 for (i = 0; i < symtab->size; ++i)
17480 {
17481 if (symtab->data[i])
17482 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17483 }
17484
17485 /* Now write out the hash table. */
17486 for (i = 0; i < symtab->size; ++i)
17487 {
17488 offset_type str_off, vec_off;
17489
17490 if (symtab->data[i])
17491 {
17492 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17493 vec_off = symtab->data[i]->index_offset;
17494 }
17495 else
17496 {
17497 /* While 0 is a valid constant pool index, it is not valid
17498 to have 0 for both offsets. */
17499 str_off = 0;
17500 vec_off = 0;
17501 }
17502
17503 str_off = MAYBE_SWAP (str_off);
17504 vec_off = MAYBE_SWAP (vec_off);
17505
17506 obstack_grow (output, &str_off, sizeof (str_off));
17507 obstack_grow (output, &vec_off, sizeof (vec_off));
17508 }
17509
17510 htab_delete (str_table);
17511 htab_delete (symbol_hash_table);
17512 }
17513
17514 /* Struct to map psymtab to CU index in the index file. */
17515 struct psymtab_cu_index_map
17516 {
17517 struct partial_symtab *psymtab;
17518 unsigned int cu_index;
17519 };
17520
17521 static hashval_t
17522 hash_psymtab_cu_index (const void *item)
17523 {
17524 const struct psymtab_cu_index_map *map = item;
17525
17526 return htab_hash_pointer (map->psymtab);
17527 }
17528
17529 static int
17530 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17531 {
17532 const struct psymtab_cu_index_map *lhs = item_lhs;
17533 const struct psymtab_cu_index_map *rhs = item_rhs;
17534
17535 return lhs->psymtab == rhs->psymtab;
17536 }
17537
17538 /* Helper struct for building the address table. */
17539 struct addrmap_index_data
17540 {
17541 struct objfile *objfile;
17542 struct obstack *addr_obstack;
17543 htab_t cu_index_htab;
17544
17545 /* Non-zero if the previous_* fields are valid.
17546 We can't write an entry until we see the next entry (since it is only then
17547 that we know the end of the entry). */
17548 int previous_valid;
17549 /* Index of the CU in the table of all CUs in the index file. */
17550 unsigned int previous_cu_index;
17551 /* Start address of the CU. */
17552 CORE_ADDR previous_cu_start;
17553 };
17554
17555 /* Write an address entry to OBSTACK. */
17556
17557 static void
17558 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17559 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17560 {
17561 offset_type cu_index_to_write;
17562 char addr[8];
17563 CORE_ADDR baseaddr;
17564
17565 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17566
17567 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17568 obstack_grow (obstack, addr, 8);
17569 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17570 obstack_grow (obstack, addr, 8);
17571 cu_index_to_write = MAYBE_SWAP (cu_index);
17572 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17573 }
17574
17575 /* Worker function for traversing an addrmap to build the address table. */
17576
17577 static int
17578 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17579 {
17580 struct addrmap_index_data *data = datap;
17581 struct partial_symtab *pst = obj;
17582
17583 if (data->previous_valid)
17584 add_address_entry (data->objfile, data->addr_obstack,
17585 data->previous_cu_start, start_addr,
17586 data->previous_cu_index);
17587
17588 data->previous_cu_start = start_addr;
17589 if (pst != NULL)
17590 {
17591 struct psymtab_cu_index_map find_map, *map;
17592 find_map.psymtab = pst;
17593 map = htab_find (data->cu_index_htab, &find_map);
17594 gdb_assert (map != NULL);
17595 data->previous_cu_index = map->cu_index;
17596 data->previous_valid = 1;
17597 }
17598 else
17599 data->previous_valid = 0;
17600
17601 return 0;
17602 }
17603
17604 /* Write OBJFILE's address map to OBSTACK.
17605 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17606 in the index file. */
17607
17608 static void
17609 write_address_map (struct objfile *objfile, struct obstack *obstack,
17610 htab_t cu_index_htab)
17611 {
17612 struct addrmap_index_data addrmap_index_data;
17613
17614 /* When writing the address table, we have to cope with the fact that
17615 the addrmap iterator only provides the start of a region; we have to
17616 wait until the next invocation to get the start of the next region. */
17617
17618 addrmap_index_data.objfile = objfile;
17619 addrmap_index_data.addr_obstack = obstack;
17620 addrmap_index_data.cu_index_htab = cu_index_htab;
17621 addrmap_index_data.previous_valid = 0;
17622
17623 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17624 &addrmap_index_data);
17625
17626 /* It's highly unlikely the last entry (end address = 0xff...ff)
17627 is valid, but we should still handle it.
17628 The end address is recorded as the start of the next region, but that
17629 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17630 anyway. */
17631 if (addrmap_index_data.previous_valid)
17632 add_address_entry (objfile, obstack,
17633 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17634 addrmap_index_data.previous_cu_index);
17635 }
17636
17637 /* Add a list of partial symbols to SYMTAB. */
17638
17639 static void
17640 write_psymbols (struct mapped_symtab *symtab,
17641 htab_t psyms_seen,
17642 struct partial_symbol **psymp,
17643 int count,
17644 offset_type cu_index,
17645 int is_static)
17646 {
17647 for (; count-- > 0; ++psymp)
17648 {
17649 void **slot, *lookup;
17650
17651 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17652 error (_("Ada is not currently supported by the index"));
17653
17654 /* We only want to add a given psymbol once. However, we also
17655 want to account for whether it is global or static. So, we
17656 may add it twice, using slightly different values. */
17657 if (is_static)
17658 {
17659 uintptr_t val = 1 | (uintptr_t) *psymp;
17660
17661 lookup = (void *) val;
17662 }
17663 else
17664 lookup = *psymp;
17665
17666 /* Only add a given psymbol once. */
17667 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17668 if (!*slot)
17669 {
17670 *slot = lookup;
17671 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17672 }
17673 }
17674 }
17675
17676 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17677 exception if there is an error. */
17678
17679 static void
17680 write_obstack (FILE *file, struct obstack *obstack)
17681 {
17682 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17683 file)
17684 != obstack_object_size (obstack))
17685 error (_("couldn't data write to file"));
17686 }
17687
17688 /* Unlink a file if the argument is not NULL. */
17689
17690 static void
17691 unlink_if_set (void *p)
17692 {
17693 char **filename = p;
17694 if (*filename)
17695 unlink (*filename);
17696 }
17697
17698 /* A helper struct used when iterating over debug_types. */
17699 struct signatured_type_index_data
17700 {
17701 struct objfile *objfile;
17702 struct mapped_symtab *symtab;
17703 struct obstack *types_list;
17704 htab_t psyms_seen;
17705 int cu_index;
17706 };
17707
17708 /* A helper function that writes a single signatured_type to an
17709 obstack. */
17710
17711 static int
17712 write_one_signatured_type (void **slot, void *d)
17713 {
17714 struct signatured_type_index_data *info = d;
17715 struct signatured_type *entry = (struct signatured_type *) *slot;
17716 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17717 struct partial_symtab *psymtab = per_cu->v.psymtab;
17718 gdb_byte val[8];
17719
17720 write_psymbols (info->symtab,
17721 info->psyms_seen,
17722 info->objfile->global_psymbols.list
17723 + psymtab->globals_offset,
17724 psymtab->n_global_syms, info->cu_index,
17725 0);
17726 write_psymbols (info->symtab,
17727 info->psyms_seen,
17728 info->objfile->static_psymbols.list
17729 + psymtab->statics_offset,
17730 psymtab->n_static_syms, info->cu_index,
17731 1);
17732
17733 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17734 entry->per_cu.offset.sect_off);
17735 obstack_grow (info->types_list, val, 8);
17736 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17737 entry->type_offset_in_tu.cu_off);
17738 obstack_grow (info->types_list, val, 8);
17739 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17740 obstack_grow (info->types_list, val, 8);
17741
17742 ++info->cu_index;
17743
17744 return 1;
17745 }
17746
17747 /* Recurse into all "included" dependencies and write their symbols as
17748 if they appeared in this psymtab. */
17749
17750 static void
17751 recursively_write_psymbols (struct objfile *objfile,
17752 struct partial_symtab *psymtab,
17753 struct mapped_symtab *symtab,
17754 htab_t psyms_seen,
17755 offset_type cu_index)
17756 {
17757 int i;
17758
17759 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17760 if (psymtab->dependencies[i]->user != NULL)
17761 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17762 symtab, psyms_seen, cu_index);
17763
17764 write_psymbols (symtab,
17765 psyms_seen,
17766 objfile->global_psymbols.list + psymtab->globals_offset,
17767 psymtab->n_global_syms, cu_index,
17768 0);
17769 write_psymbols (symtab,
17770 psyms_seen,
17771 objfile->static_psymbols.list + psymtab->statics_offset,
17772 psymtab->n_static_syms, cu_index,
17773 1);
17774 }
17775
17776 /* Create an index file for OBJFILE in the directory DIR. */
17777
17778 static void
17779 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17780 {
17781 struct cleanup *cleanup;
17782 char *filename, *cleanup_filename;
17783 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17784 struct obstack cu_list, types_cu_list;
17785 int i;
17786 FILE *out_file;
17787 struct mapped_symtab *symtab;
17788 offset_type val, size_of_contents, total_len;
17789 struct stat st;
17790 htab_t psyms_seen;
17791 htab_t cu_index_htab;
17792 struct psymtab_cu_index_map *psymtab_cu_index_map;
17793
17794 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17795 return;
17796
17797 if (dwarf2_per_objfile->using_index)
17798 error (_("Cannot use an index to create the index"));
17799
17800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17801 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17802
17803 if (stat (objfile->name, &st) < 0)
17804 perror_with_name (objfile->name);
17805
17806 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17807 INDEX_SUFFIX, (char *) NULL);
17808 cleanup = make_cleanup (xfree, filename);
17809
17810 out_file = fopen (filename, "wb");
17811 if (!out_file)
17812 error (_("Can't open `%s' for writing"), filename);
17813
17814 cleanup_filename = filename;
17815 make_cleanup (unlink_if_set, &cleanup_filename);
17816
17817 symtab = create_mapped_symtab ();
17818 make_cleanup (cleanup_mapped_symtab, symtab);
17819
17820 obstack_init (&addr_obstack);
17821 make_cleanup_obstack_free (&addr_obstack);
17822
17823 obstack_init (&cu_list);
17824 make_cleanup_obstack_free (&cu_list);
17825
17826 obstack_init (&types_cu_list);
17827 make_cleanup_obstack_free (&types_cu_list);
17828
17829 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17830 NULL, xcalloc, xfree);
17831 make_cleanup_htab_delete (psyms_seen);
17832
17833 /* While we're scanning CU's create a table that maps a psymtab pointer
17834 (which is what addrmap records) to its index (which is what is recorded
17835 in the index file). This will later be needed to write the address
17836 table. */
17837 cu_index_htab = htab_create_alloc (100,
17838 hash_psymtab_cu_index,
17839 eq_psymtab_cu_index,
17840 NULL, xcalloc, xfree);
17841 make_cleanup_htab_delete (cu_index_htab);
17842 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17843 xmalloc (sizeof (struct psymtab_cu_index_map)
17844 * dwarf2_per_objfile->n_comp_units);
17845 make_cleanup (xfree, psymtab_cu_index_map);
17846
17847 /* The CU list is already sorted, so we don't need to do additional
17848 work here. Also, the debug_types entries do not appear in
17849 all_comp_units, but only in their own hash table. */
17850 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17851 {
17852 struct dwarf2_per_cu_data *per_cu
17853 = dwarf2_per_objfile->all_comp_units[i];
17854 struct partial_symtab *psymtab = per_cu->v.psymtab;
17855 gdb_byte val[8];
17856 struct psymtab_cu_index_map *map;
17857 void **slot;
17858
17859 if (psymtab->user == NULL)
17860 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
17861
17862 map = &psymtab_cu_index_map[i];
17863 map->psymtab = psymtab;
17864 map->cu_index = i;
17865 slot = htab_find_slot (cu_index_htab, map, INSERT);
17866 gdb_assert (slot != NULL);
17867 gdb_assert (*slot == NULL);
17868 *slot = map;
17869
17870 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17871 per_cu->offset.sect_off);
17872 obstack_grow (&cu_list, val, 8);
17873 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17874 obstack_grow (&cu_list, val, 8);
17875 }
17876
17877 /* Dump the address map. */
17878 write_address_map (objfile, &addr_obstack, cu_index_htab);
17879
17880 /* Write out the .debug_type entries, if any. */
17881 if (dwarf2_per_objfile->signatured_types)
17882 {
17883 struct signatured_type_index_data sig_data;
17884
17885 sig_data.objfile = objfile;
17886 sig_data.symtab = symtab;
17887 sig_data.types_list = &types_cu_list;
17888 sig_data.psyms_seen = psyms_seen;
17889 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17890 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17891 write_one_signatured_type, &sig_data);
17892 }
17893
17894 obstack_init (&constant_pool);
17895 make_cleanup_obstack_free (&constant_pool);
17896 obstack_init (&symtab_obstack);
17897 make_cleanup_obstack_free (&symtab_obstack);
17898 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17899
17900 obstack_init (&contents);
17901 make_cleanup_obstack_free (&contents);
17902 size_of_contents = 6 * sizeof (offset_type);
17903 total_len = size_of_contents;
17904
17905 /* The version number. */
17906 val = MAYBE_SWAP (6);
17907 obstack_grow (&contents, &val, sizeof (val));
17908
17909 /* The offset of the CU list from the start of the file. */
17910 val = MAYBE_SWAP (total_len);
17911 obstack_grow (&contents, &val, sizeof (val));
17912 total_len += obstack_object_size (&cu_list);
17913
17914 /* The offset of the types CU list from the start of the file. */
17915 val = MAYBE_SWAP (total_len);
17916 obstack_grow (&contents, &val, sizeof (val));
17917 total_len += obstack_object_size (&types_cu_list);
17918
17919 /* The offset of the address table from the start of the file. */
17920 val = MAYBE_SWAP (total_len);
17921 obstack_grow (&contents, &val, sizeof (val));
17922 total_len += obstack_object_size (&addr_obstack);
17923
17924 /* The offset of the symbol table from the start of the file. */
17925 val = MAYBE_SWAP (total_len);
17926 obstack_grow (&contents, &val, sizeof (val));
17927 total_len += obstack_object_size (&symtab_obstack);
17928
17929 /* The offset of the constant pool from the start of the file. */
17930 val = MAYBE_SWAP (total_len);
17931 obstack_grow (&contents, &val, sizeof (val));
17932 total_len += obstack_object_size (&constant_pool);
17933
17934 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17935
17936 write_obstack (out_file, &contents);
17937 write_obstack (out_file, &cu_list);
17938 write_obstack (out_file, &types_cu_list);
17939 write_obstack (out_file, &addr_obstack);
17940 write_obstack (out_file, &symtab_obstack);
17941 write_obstack (out_file, &constant_pool);
17942
17943 fclose (out_file);
17944
17945 /* We want to keep the file, so we set cleanup_filename to NULL
17946 here. See unlink_if_set. */
17947 cleanup_filename = NULL;
17948
17949 do_cleanups (cleanup);
17950 }
17951
17952 /* Implementation of the `save gdb-index' command.
17953
17954 Note that the file format used by this command is documented in the
17955 GDB manual. Any changes here must be documented there. */
17956
17957 static void
17958 save_gdb_index_command (char *arg, int from_tty)
17959 {
17960 struct objfile *objfile;
17961
17962 if (!arg || !*arg)
17963 error (_("usage: save gdb-index DIRECTORY"));
17964
17965 ALL_OBJFILES (objfile)
17966 {
17967 struct stat st;
17968
17969 /* If the objfile does not correspond to an actual file, skip it. */
17970 if (stat (objfile->name, &st) < 0)
17971 continue;
17972
17973 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17974 if (dwarf2_per_objfile)
17975 {
17976 volatile struct gdb_exception except;
17977
17978 TRY_CATCH (except, RETURN_MASK_ERROR)
17979 {
17980 write_psymtabs_to_index (objfile, arg);
17981 }
17982 if (except.reason < 0)
17983 exception_fprintf (gdb_stderr, except,
17984 _("Error while writing index for `%s': "),
17985 objfile->name);
17986 }
17987 }
17988 }
17989
17990 \f
17991
17992 int dwarf2_always_disassemble;
17993
17994 static void
17995 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17996 struct cmd_list_element *c, const char *value)
17997 {
17998 fprintf_filtered (file,
17999 _("Whether to always disassemble "
18000 "DWARF expressions is %s.\n"),
18001 value);
18002 }
18003
18004 static void
18005 show_check_physname (struct ui_file *file, int from_tty,
18006 struct cmd_list_element *c, const char *value)
18007 {
18008 fprintf_filtered (file,
18009 _("Whether to check \"physname\" is %s.\n"),
18010 value);
18011 }
18012
18013 void _initialize_dwarf2_read (void);
18014
18015 void
18016 _initialize_dwarf2_read (void)
18017 {
18018 struct cmd_list_element *c;
18019
18020 dwarf2_objfile_data_key
18021 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18022
18023 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18024 Set DWARF 2 specific variables.\n\
18025 Configure DWARF 2 variables such as the cache size"),
18026 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18027 0/*allow-unknown*/, &maintenance_set_cmdlist);
18028
18029 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18030 Show DWARF 2 specific variables\n\
18031 Show DWARF 2 variables such as the cache size"),
18032 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18033 0/*allow-unknown*/, &maintenance_show_cmdlist);
18034
18035 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18036 &dwarf2_max_cache_age, _("\
18037 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18038 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18039 A higher limit means that cached compilation units will be stored\n\
18040 in memory longer, and more total memory will be used. Zero disables\n\
18041 caching, which can slow down startup."),
18042 NULL,
18043 show_dwarf2_max_cache_age,
18044 &set_dwarf2_cmdlist,
18045 &show_dwarf2_cmdlist);
18046
18047 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18048 &dwarf2_always_disassemble, _("\
18049 Set whether `info address' always disassembles DWARF expressions."), _("\
18050 Show whether `info address' always disassembles DWARF expressions."), _("\
18051 When enabled, DWARF expressions are always printed in an assembly-like\n\
18052 syntax. When disabled, expressions will be printed in a more\n\
18053 conversational style, when possible."),
18054 NULL,
18055 show_dwarf2_always_disassemble,
18056 &set_dwarf2_cmdlist,
18057 &show_dwarf2_cmdlist);
18058
18059 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18060 Set debugging of the dwarf2 DIE reader."), _("\
18061 Show debugging of the dwarf2 DIE reader."), _("\
18062 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18063 The value is the maximum depth to print."),
18064 NULL,
18065 NULL,
18066 &setdebuglist, &showdebuglist);
18067
18068 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18069 Set cross-checking of \"physname\" code against demangler."), _("\
18070 Show cross-checking of \"physname\" code against demangler."), _("\
18071 When enabled, GDB's internal \"physname\" code is checked against\n\
18072 the demangler."),
18073 NULL, show_check_physname,
18074 &setdebuglist, &showdebuglist);
18075
18076 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18077 _("\
18078 Save a gdb-index file.\n\
18079 Usage: save gdb-index DIRECTORY"),
18080 &save_cmdlist);
18081 set_cmd_completer (c, filename_completer);
18082 }
This page took 0.400845 seconds and 5 git commands to generate.