PR 14125
[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 "gdb/gdb-index.h"
66 #include <ctype.h>
67
68 #include <fcntl.h>
69 #include "gdb_string.h"
70 #include "gdb_assert.h"
71 #include <sys/types.h>
72 #ifdef HAVE_ZLIB_H
73 #include <zlib.h>
74 #endif
75 #ifdef HAVE_MMAP
76 #include <sys/mman.h>
77 #ifndef MAP_FAILED
78 #define MAP_FAILED ((void *) -1)
79 #endif
80 #endif
81
82 typedef struct symbol *symbolp;
83 DEF_VEC_P (symbolp);
84
85 /* When non-zero, dump DIEs after they are read in. */
86 static int dwarf2_die_debug = 0;
87
88 /* When non-zero, cross-check physname against demangler. */
89 static int check_physname = 0;
90
91 /* When non-zero, do not reject deprecated .gdb_index sections. */
92 int use_deprecated_index_sections = 0;
93
94 static int pagesize;
95
96 /* When set, the file that we're processing is known to have debugging
97 info for C++ namespaces. GCC 3.3.x did not produce this information,
98 but later versions do. */
99
100 static int processing_has_namespace_info;
101
102 static const struct objfile_data *dwarf2_objfile_data_key;
103
104 struct dwarf2_section_info
105 {
106 asection *asection;
107 gdb_byte *buffer;
108 bfd_size_type size;
109 /* Not NULL if the section was actually mmapped. */
110 void *map_addr;
111 /* Page aligned size of mmapped area. */
112 bfd_size_type map_len;
113 /* True if we have tried to read this section. */
114 int readin;
115 };
116
117 typedef struct dwarf2_section_info dwarf2_section_info_def;
118 DEF_VEC_O (dwarf2_section_info_def);
119
120 /* All offsets in the index are of this type. It must be
121 architecture-independent. */
122 typedef uint32_t offset_type;
123
124 DEF_VEC_I (offset_type);
125
126 /* Ensure only legit values are used. */
127 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
128 do { \
129 gdb_assert ((unsigned int) (value) <= 1); \
130 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133 /* Ensure only legit values are used. */
134 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
137 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
138 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
139 } while (0)
140
141 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
142 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
143 do { \
144 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
145 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
146 } while (0)
147
148 /* A description of the mapped index. The file format is described in
149 a comment by the code that writes the index. */
150 struct mapped_index
151 {
152 /* Index data format version. */
153 int version;
154
155 /* The total length of the buffer. */
156 off_t total_size;
157
158 /* A pointer to the address table data. */
159 const gdb_byte *address_table;
160
161 /* Size of the address table data in bytes. */
162 offset_type address_table_size;
163
164 /* The symbol table, implemented as a hash table. */
165 const offset_type *symbol_table;
166
167 /* Size in slots, each slot is 2 offset_types. */
168 offset_type symbol_table_slots;
169
170 /* A pointer to the constant pool. */
171 const char *constant_pool;
172 };
173
174 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
175 DEF_VEC_P (dwarf2_per_cu_ptr);
176
177 /* Collection of data recorded per objfile.
178 This hangs off of dwarf2_objfile_data_key. */
179
180 struct dwarf2_per_objfile
181 {
182 struct dwarf2_section_info info;
183 struct dwarf2_section_info abbrev;
184 struct dwarf2_section_info line;
185 struct dwarf2_section_info loc;
186 struct dwarf2_section_info macinfo;
187 struct dwarf2_section_info macro;
188 struct dwarf2_section_info str;
189 struct dwarf2_section_info ranges;
190 struct dwarf2_section_info addr;
191 struct dwarf2_section_info frame;
192 struct dwarf2_section_info eh_frame;
193 struct dwarf2_section_info gdb_index;
194
195 VEC (dwarf2_section_info_def) *types;
196
197 /* Back link. */
198 struct objfile *objfile;
199
200 /* Table of all the compilation units. This is used to locate
201 the target compilation unit of a particular reference. */
202 struct dwarf2_per_cu_data **all_comp_units;
203
204 /* The number of compilation units in ALL_COMP_UNITS. */
205 int n_comp_units;
206
207 /* The number of .debug_types-related CUs. */
208 int n_type_units;
209
210 /* The .debug_types-related CUs (TUs). */
211 struct dwarf2_per_cu_data **all_type_units;
212
213 /* A chain of compilation units that are currently read in, so that
214 they can be freed later. */
215 struct dwarf2_per_cu_data *read_in_chain;
216
217 /* A table mapping .debug_types signatures to its signatured_type entry.
218 This is NULL if the .debug_types section hasn't been read in yet. */
219 htab_t signatured_types;
220
221 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
222 This is NULL if the table hasn't been allocated yet. */
223 htab_t dwo_files;
224
225 /* A flag indicating wether this objfile has a section loaded at a
226 VMA of 0. */
227 int has_section_at_zero;
228
229 /* True if we are using the mapped index,
230 or we are faking it for OBJF_READNOW's sake. */
231 unsigned char using_index;
232
233 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
234 struct mapped_index *index_table;
235
236 /* When using index_table, this keeps track of all quick_file_names entries.
237 TUs can share line table entries with CUs or other TUs, and there can be
238 a lot more TUs than unique line tables, so we maintain a separate table
239 of all line table entries to support the sharing. */
240 htab_t quick_file_names_table;
241
242 /* Set during partial symbol reading, to prevent queueing of full
243 symbols. */
244 int reading_partial_symbols;
245
246 /* Table mapping type DIEs to their struct type *.
247 This is NULL if not allocated yet.
248 The mapping is done via (CU/TU signature + DIE offset) -> type. */
249 htab_t die_type_hash;
250
251 /* The CUs we recently read. */
252 VEC (dwarf2_per_cu_ptr) *just_read_cus;
253 };
254
255 static struct dwarf2_per_objfile *dwarf2_per_objfile;
256
257 /* Default names of the debugging sections. */
258
259 /* Note that if the debugging section has been compressed, it might
260 have a name like .zdebug_info. */
261
262 static const struct dwarf2_debug_sections dwarf2_elf_names =
263 {
264 { ".debug_info", ".zdebug_info" },
265 { ".debug_abbrev", ".zdebug_abbrev" },
266 { ".debug_line", ".zdebug_line" },
267 { ".debug_loc", ".zdebug_loc" },
268 { ".debug_macinfo", ".zdebug_macinfo" },
269 { ".debug_macro", ".zdebug_macro" },
270 { ".debug_str", ".zdebug_str" },
271 { ".debug_ranges", ".zdebug_ranges" },
272 { ".debug_types", ".zdebug_types" },
273 { ".debug_addr", ".zdebug_addr" },
274 { ".debug_frame", ".zdebug_frame" },
275 { ".eh_frame", NULL },
276 { ".gdb_index", ".zgdb_index" },
277 23
278 };
279
280 /* List of DWO sections. */
281
282 static const struct dwo_section_names
283 {
284 struct dwarf2_section_names abbrev_dwo;
285 struct dwarf2_section_names info_dwo;
286 struct dwarf2_section_names line_dwo;
287 struct dwarf2_section_names loc_dwo;
288 struct dwarf2_section_names str_dwo;
289 struct dwarf2_section_names str_offsets_dwo;
290 struct dwarf2_section_names types_dwo;
291 }
292 dwo_section_names =
293 {
294 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
295 { ".debug_info.dwo", ".zdebug_info.dwo" },
296 { ".debug_line.dwo", ".zdebug_line.dwo" },
297 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
298 { ".debug_str.dwo", ".zdebug_str.dwo" },
299 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
300 { ".debug_types.dwo", ".zdebug_types.dwo" },
301 };
302
303 /* local data types */
304
305 /* We hold several abbreviation tables in memory at the same time. */
306 #ifndef ABBREV_HASH_SIZE
307 #define ABBREV_HASH_SIZE 121
308 #endif
309
310 /* The data in a compilation unit header, after target2host
311 translation, looks like this. */
312 struct comp_unit_head
313 {
314 unsigned int length;
315 short version;
316 unsigned char addr_size;
317 unsigned char signed_addr_p;
318 sect_offset abbrev_offset;
319
320 /* Size of file offsets; either 4 or 8. */
321 unsigned int offset_size;
322
323 /* Size of the length field; either 4 or 12. */
324 unsigned int initial_length_size;
325
326 /* Offset to the first byte of this compilation unit header in the
327 .debug_info section, for resolving relative reference dies. */
328 sect_offset offset;
329
330 /* Offset to first die in this cu from the start of the cu.
331 This will be the first byte following the compilation unit header. */
332 cu_offset first_die_offset;
333 };
334
335 /* Type used for delaying computation of method physnames.
336 See comments for compute_delayed_physnames. */
337 struct delayed_method_info
338 {
339 /* The type to which the method is attached, i.e., its parent class. */
340 struct type *type;
341
342 /* The index of the method in the type's function fieldlists. */
343 int fnfield_index;
344
345 /* The index of the method in the fieldlist. */
346 int index;
347
348 /* The name of the DIE. */
349 const char *name;
350
351 /* The DIE associated with this method. */
352 struct die_info *die;
353 };
354
355 typedef struct delayed_method_info delayed_method_info;
356 DEF_VEC_O (delayed_method_info);
357
358 /* Internal state when decoding a particular compilation unit. */
359 struct dwarf2_cu
360 {
361 /* The objfile containing this compilation unit. */
362 struct objfile *objfile;
363
364 /* The header of the compilation unit. */
365 struct comp_unit_head header;
366
367 /* Base address of this compilation unit. */
368 CORE_ADDR base_address;
369
370 /* Non-zero if base_address has been set. */
371 int base_known;
372
373 /* The language we are debugging. */
374 enum language language;
375 const struct language_defn *language_defn;
376
377 const char *producer;
378
379 /* The generic symbol table building routines have separate lists for
380 file scope symbols and all all other scopes (local scopes). So
381 we need to select the right one to pass to add_symbol_to_list().
382 We do it by keeping a pointer to the correct list in list_in_scope.
383
384 FIXME: The original dwarf code just treated the file scope as the
385 first local scope, and all other local scopes as nested local
386 scopes, and worked fine. Check to see if we really need to
387 distinguish these in buildsym.c. */
388 struct pending **list_in_scope;
389
390 /* DWARF abbreviation table associated with this compilation unit. */
391 struct abbrev_info **dwarf2_abbrevs;
392
393 /* Storage for the abbrev table. */
394 struct obstack abbrev_obstack;
395
396 /* Hash table holding all the loaded partial DIEs
397 with partial_die->offset.SECT_OFF as hash. */
398 htab_t partial_dies;
399
400 /* Storage for things with the same lifetime as this read-in compilation
401 unit, including partial DIEs. */
402 struct obstack comp_unit_obstack;
403
404 /* When multiple dwarf2_cu structures are living in memory, this field
405 chains them all together, so that they can be released efficiently.
406 We will probably also want a generation counter so that most-recently-used
407 compilation units are cached... */
408 struct dwarf2_per_cu_data *read_in_chain;
409
410 /* Backchain to our per_cu entry if the tree has been built. */
411 struct dwarf2_per_cu_data *per_cu;
412
413 /* How many compilation units ago was this CU last referenced? */
414 int last_used;
415
416 /* A hash table of DIE cu_offset for following references with
417 die_info->offset.sect_off as hash. */
418 htab_t die_hash;
419
420 /* Full DIEs if read in. */
421 struct die_info *dies;
422
423 /* A set of pointers to dwarf2_per_cu_data objects for compilation
424 units referenced by this one. Only set during full symbol processing;
425 partial symbol tables do not have dependencies. */
426 htab_t dependencies;
427
428 /* Header data from the line table, during full symbol processing. */
429 struct line_header *line_header;
430
431 /* A list of methods which need to have physnames computed
432 after all type information has been read. */
433 VEC (delayed_method_info) *method_list;
434
435 /* To be copied to symtab->call_site_htab. */
436 htab_t call_site_htab;
437
438 /* Non-NULL if this CU came from a DWO file.
439 There is an invariant here that is important to remember:
440 Except for attributes copied from the top level DIE in the "main"
441 (or "stub") file in preparation for reading the DWO file
442 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
443 Either there isn't a DWO file (in which case this is NULL and the point
444 is moot), or there is and either we're not going to read it (in which
445 case this is NULL) or there is and we are reading it (in which case this
446 is non-NULL). */
447 struct dwo_unit *dwo_unit;
448
449 /* The DW_AT_addr_base attribute if present, zero otherwise
450 (zero is a valid value though).
451 Note this value comes from the stub CU/TU's DIE. */
452 ULONGEST addr_base;
453
454 /* Mark used when releasing cached dies. */
455 unsigned int mark : 1;
456
457 /* This CU references .debug_loc. See the symtab->locations_valid field.
458 This test is imperfect as there may exist optimized debug code not using
459 any location list and still facing inlining issues if handled as
460 unoptimized code. For a future better test see GCC PR other/32998. */
461 unsigned int has_loclist : 1;
462
463 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
464 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
465 are valid. This information is cached because profiling CU expansion
466 showed excessive time spent in producer_is_gxx_lt_4_6. */
467 unsigned int checked_producer : 1;
468 unsigned int producer_is_gxx_lt_4_6 : 1;
469 unsigned int producer_is_icc : 1;
470
471 /* Non-zero if DW_AT_addr_base was found.
472 Used when processing DWO files. */
473 unsigned int have_addr_base : 1;
474 };
475
476 /* Persistent data held for a compilation unit, even when not
477 processing it. We put a pointer to this structure in the
478 read_symtab_private field of the psymtab. */
479
480 struct dwarf2_per_cu_data
481 {
482 /* The start offset and length of this compilation unit. 2**29-1
483 bytes should suffice to store the length of any compilation unit
484 - if it doesn't, GDB will fall over anyway.
485 NOTE: Unlike comp_unit_head.length, this length includes
486 initial_length_size.
487 If the DIE refers to a DWO file, this is always of the original die,
488 not the DWO file. */
489 sect_offset offset;
490 unsigned int length : 29;
491
492 /* Flag indicating this compilation unit will be read in before
493 any of the current compilation units are processed. */
494 unsigned int queued : 1;
495
496 /* This flag will be set when reading partial DIEs if we need to load
497 absolutely all DIEs for this compilation unit, instead of just the ones
498 we think are interesting. It gets set if we look for a DIE in the
499 hash table and don't find it. */
500 unsigned int load_all_dies : 1;
501
502 /* Non-zero if this CU is from .debug_types. */
503 unsigned int is_debug_types : 1;
504
505 /* The section this CU/TU lives in.
506 If the DIE refers to a DWO file, this is always the original die,
507 not the DWO file. */
508 struct dwarf2_section_info *info_or_types_section;
509
510 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
511 of the CU cache it gets reset to NULL again. */
512 struct dwarf2_cu *cu;
513
514 /* The corresponding objfile.
515 Normally we can get the objfile from dwarf2_per_objfile.
516 However we can enter this file with just a "per_cu" handle. */
517 struct objfile *objfile;
518
519 /* When using partial symbol tables, the 'psymtab' field is active.
520 Otherwise the 'quick' field is active. */
521 union
522 {
523 /* The partial symbol table associated with this compilation unit,
524 or NULL for unread partial units. */
525 struct partial_symtab *psymtab;
526
527 /* Data needed by the "quick" functions. */
528 struct dwarf2_per_cu_quick_data *quick;
529 } v;
530
531 /* The CUs we import using DW_TAG_imported_unit. This is filled in
532 while reading psymtabs, used to compute the psymtab dependencies,
533 and then cleared. Then it is filled in again while reading full
534 symbols, and only deleted when the objfile is destroyed. */
535 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
536 };
537
538 /* Entry in the signatured_types hash table. */
539
540 struct signatured_type
541 {
542 /* The type's signature. */
543 ULONGEST signature;
544
545 /* Offset in the TU of the type's DIE, as read from the TU header.
546 If the definition lives in a DWO file, this value is unusable. */
547 cu_offset type_offset_in_tu;
548
549 /* Offset in the section of the type's DIE.
550 If the definition lives in a DWO file, this is the offset in the
551 .debug_types.dwo section.
552 The value is zero until the actual value is known.
553 Zero is otherwise not a valid section offset. */
554 sect_offset type_offset_in_section;
555
556 /* The CU(/TU) of this type. */
557 struct dwarf2_per_cu_data per_cu;
558 };
559
560 /* These sections are what may appear in a "dwo" file. */
561
562 struct dwo_sections
563 {
564 struct dwarf2_section_info abbrev;
565 struct dwarf2_section_info info;
566 struct dwarf2_section_info line;
567 struct dwarf2_section_info loc;
568 struct dwarf2_section_info str;
569 struct dwarf2_section_info str_offsets;
570 VEC (dwarf2_section_info_def) *types;
571 };
572
573 /* Common bits of DWO CUs/TUs. */
574
575 struct dwo_unit
576 {
577 /* Backlink to the containing struct dwo_file. */
578 struct dwo_file *dwo_file;
579
580 /* The "id" that distinguishes this CU/TU.
581 .debug_info calls this "dwo_id", .debug_types calls this "signature".
582 Since signatures came first, we stick with it for consistency. */
583 ULONGEST signature;
584
585 /* The section this CU/TU lives in, in the DWO file. */
586 struct dwarf2_section_info *info_or_types_section;
587
588 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
589 sect_offset offset;
590 unsigned int length;
591
592 /* For types, offset in the type's DIE of the type defined by this TU. */
593 cu_offset type_offset_in_tu;
594 };
595
596 /* Data for one DWO file. */
597
598 struct dwo_file
599 {
600 /* The DW_AT_GNU_dwo_name attribute.
601 We don't manage space for this, it's an attribute. */
602 const char *dwo_name;
603
604 /* The bfd, when the file is open. Otherwise this is NULL. */
605 bfd *dwo_bfd;
606
607 /* Section info for this file. */
608 struct dwo_sections sections;
609
610 /* Table of CUs in the file.
611 Each element is a struct dwo_unit. */
612 htab_t cus;
613
614 /* Table of TUs in the file.
615 Each element is a struct dwo_unit. */
616 htab_t tus;
617 };
618
619 /* Struct used to pass misc. parameters to read_die_and_children, et
620 al. which are used for both .debug_info and .debug_types dies.
621 All parameters here are unchanging for the life of the call. This
622 struct exists to abstract away the constant parameters of die reading. */
623
624 struct die_reader_specs
625 {
626 /* die_section->asection->owner. */
627 bfd* abfd;
628
629 /* The CU of the DIE we are parsing. */
630 struct dwarf2_cu *cu;
631
632 /* Non-NULL if reading a DWO file. */
633 struct dwo_file *dwo_file;
634
635 /* The section the die comes from.
636 This is either .debug_info or .debug_types, or the .dwo variants. */
637 struct dwarf2_section_info *die_section;
638
639 /* die_section->buffer. */
640 gdb_byte *buffer;
641
642 /* The end of the buffer. */
643 const gdb_byte *buffer_end;
644 };
645
646 /* Type of function passed to init_cutu_and_read_dies, et.al. */
647 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
648 gdb_byte *info_ptr,
649 struct die_info *comp_unit_die,
650 int has_children,
651 void *data);
652
653 /* The line number information for a compilation unit (found in the
654 .debug_line section) begins with a "statement program header",
655 which contains the following information. */
656 struct line_header
657 {
658 unsigned int total_length;
659 unsigned short version;
660 unsigned int header_length;
661 unsigned char minimum_instruction_length;
662 unsigned char maximum_ops_per_instruction;
663 unsigned char default_is_stmt;
664 int line_base;
665 unsigned char line_range;
666 unsigned char opcode_base;
667
668 /* standard_opcode_lengths[i] is the number of operands for the
669 standard opcode whose value is i. This means that
670 standard_opcode_lengths[0] is unused, and the last meaningful
671 element is standard_opcode_lengths[opcode_base - 1]. */
672 unsigned char *standard_opcode_lengths;
673
674 /* The include_directories table. NOTE! These strings are not
675 allocated with xmalloc; instead, they are pointers into
676 debug_line_buffer. If you try to free them, `free' will get
677 indigestion. */
678 unsigned int num_include_dirs, include_dirs_size;
679 char **include_dirs;
680
681 /* The file_names table. NOTE! These strings are not allocated
682 with xmalloc; instead, they are pointers into debug_line_buffer.
683 Don't try to free them directly. */
684 unsigned int num_file_names, file_names_size;
685 struct file_entry
686 {
687 char *name;
688 unsigned int dir_index;
689 unsigned int mod_time;
690 unsigned int length;
691 int included_p; /* Non-zero if referenced by the Line Number Program. */
692 struct symtab *symtab; /* The associated symbol table, if any. */
693 } *file_names;
694
695 /* The start and end of the statement program following this
696 header. These point into dwarf2_per_objfile->line_buffer. */
697 gdb_byte *statement_program_start, *statement_program_end;
698 };
699
700 /* When we construct a partial symbol table entry we only
701 need this much information. */
702 struct partial_die_info
703 {
704 /* Offset of this DIE. */
705 sect_offset offset;
706
707 /* DWARF-2 tag for this DIE. */
708 ENUM_BITFIELD(dwarf_tag) tag : 16;
709
710 /* Assorted flags describing the data found in this DIE. */
711 unsigned int has_children : 1;
712 unsigned int is_external : 1;
713 unsigned int is_declaration : 1;
714 unsigned int has_type : 1;
715 unsigned int has_specification : 1;
716 unsigned int has_pc_info : 1;
717 unsigned int may_be_inlined : 1;
718
719 /* Flag set if the SCOPE field of this structure has been
720 computed. */
721 unsigned int scope_set : 1;
722
723 /* Flag set if the DIE has a byte_size attribute. */
724 unsigned int has_byte_size : 1;
725
726 /* Flag set if any of the DIE's children are template arguments. */
727 unsigned int has_template_arguments : 1;
728
729 /* Flag set if fixup_partial_die has been called on this die. */
730 unsigned int fixup_called : 1;
731
732 /* The name of this DIE. Normally the value of DW_AT_name, but
733 sometimes a default name for unnamed DIEs. */
734 char *name;
735
736 /* The linkage name, if present. */
737 const char *linkage_name;
738
739 /* The scope to prepend to our children. This is generally
740 allocated on the comp_unit_obstack, so will disappear
741 when this compilation unit leaves the cache. */
742 char *scope;
743
744 /* Some data associated with the partial DIE. The tag determines
745 which field is live. */
746 union
747 {
748 /* The location description associated with this DIE, if any. */
749 struct dwarf_block *locdesc;
750 /* The offset of an import, for DW_TAG_imported_unit. */
751 sect_offset offset;
752 } d;
753
754 /* If HAS_PC_INFO, the PC range associated with this DIE. */
755 CORE_ADDR lowpc;
756 CORE_ADDR highpc;
757
758 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
759 DW_AT_sibling, if any. */
760 /* NOTE: This member isn't strictly necessary, read_partial_die could
761 return DW_AT_sibling values to its caller load_partial_dies. */
762 gdb_byte *sibling;
763
764 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
765 DW_AT_specification (or DW_AT_abstract_origin or
766 DW_AT_extension). */
767 sect_offset spec_offset;
768
769 /* Pointers to this DIE's parent, first child, and next sibling,
770 if any. */
771 struct partial_die_info *die_parent, *die_child, *die_sibling;
772 };
773
774 /* This data structure holds the information of an abbrev. */
775 struct abbrev_info
776 {
777 unsigned int number; /* number identifying abbrev */
778 enum dwarf_tag tag; /* dwarf tag */
779 unsigned short has_children; /* boolean */
780 unsigned short num_attrs; /* number of attributes */
781 struct attr_abbrev *attrs; /* an array of attribute descriptions */
782 struct abbrev_info *next; /* next in chain */
783 };
784
785 struct attr_abbrev
786 {
787 ENUM_BITFIELD(dwarf_attribute) name : 16;
788 ENUM_BITFIELD(dwarf_form) form : 16;
789 };
790
791 /* Attributes have a name and a value. */
792 struct attribute
793 {
794 ENUM_BITFIELD(dwarf_attribute) name : 16;
795 ENUM_BITFIELD(dwarf_form) form : 15;
796
797 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
798 field should be in u.str (existing only for DW_STRING) but it is kept
799 here for better struct attribute alignment. */
800 unsigned int string_is_canonical : 1;
801
802 union
803 {
804 char *str;
805 struct dwarf_block *blk;
806 ULONGEST unsnd;
807 LONGEST snd;
808 CORE_ADDR addr;
809 struct signatured_type *signatured_type;
810 }
811 u;
812 };
813
814 /* This data structure holds a complete die structure. */
815 struct die_info
816 {
817 /* DWARF-2 tag for this DIE. */
818 ENUM_BITFIELD(dwarf_tag) tag : 16;
819
820 /* Number of attributes */
821 unsigned char num_attrs;
822
823 /* True if we're presently building the full type name for the
824 type derived from this DIE. */
825 unsigned char building_fullname : 1;
826
827 /* Abbrev number */
828 unsigned int abbrev;
829
830 /* Offset in .debug_info or .debug_types section. */
831 sect_offset offset;
832
833 /* The dies in a compilation unit form an n-ary tree. PARENT
834 points to this die's parent; CHILD points to the first child of
835 this node; and all the children of a given node are chained
836 together via their SIBLING fields. */
837 struct die_info *child; /* Its first child, if any. */
838 struct die_info *sibling; /* Its next sibling, if any. */
839 struct die_info *parent; /* Its parent, if any. */
840
841 /* An array of attributes, with NUM_ATTRS elements. There may be
842 zero, but it's not common and zero-sized arrays are not
843 sufficiently portable C. */
844 struct attribute attrs[1];
845 };
846
847 /* Get at parts of an attribute structure. */
848
849 #define DW_STRING(attr) ((attr)->u.str)
850 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
851 #define DW_UNSND(attr) ((attr)->u.unsnd)
852 #define DW_BLOCK(attr) ((attr)->u.blk)
853 #define DW_SND(attr) ((attr)->u.snd)
854 #define DW_ADDR(attr) ((attr)->u.addr)
855 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
856
857 /* Blocks are a bunch of untyped bytes. */
858 struct dwarf_block
859 {
860 unsigned int size;
861
862 /* Valid only if SIZE is not zero. */
863 gdb_byte *data;
864 };
865
866 #ifndef ATTR_ALLOC_CHUNK
867 #define ATTR_ALLOC_CHUNK 4
868 #endif
869
870 /* Allocate fields for structs, unions and enums in this size. */
871 #ifndef DW_FIELD_ALLOC_CHUNK
872 #define DW_FIELD_ALLOC_CHUNK 4
873 #endif
874
875 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
876 but this would require a corresponding change in unpack_field_as_long
877 and friends. */
878 static int bits_per_byte = 8;
879
880 /* The routines that read and process dies for a C struct or C++ class
881 pass lists of data member fields and lists of member function fields
882 in an instance of a field_info structure, as defined below. */
883 struct field_info
884 {
885 /* List of data member and baseclasses fields. */
886 struct nextfield
887 {
888 struct nextfield *next;
889 int accessibility;
890 int virtuality;
891 struct field field;
892 }
893 *fields, *baseclasses;
894
895 /* Number of fields (including baseclasses). */
896 int nfields;
897
898 /* Number of baseclasses. */
899 int nbaseclasses;
900
901 /* Set if the accesibility of one of the fields is not public. */
902 int non_public_fields;
903
904 /* Member function fields array, entries are allocated in the order they
905 are encountered in the object file. */
906 struct nextfnfield
907 {
908 struct nextfnfield *next;
909 struct fn_field fnfield;
910 }
911 *fnfields;
912
913 /* Member function fieldlist array, contains name of possibly overloaded
914 member function, number of overloaded member functions and a pointer
915 to the head of the member function field chain. */
916 struct fnfieldlist
917 {
918 char *name;
919 int length;
920 struct nextfnfield *head;
921 }
922 *fnfieldlists;
923
924 /* Number of entries in the fnfieldlists array. */
925 int nfnfields;
926
927 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
928 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
929 struct typedef_field_list
930 {
931 struct typedef_field field;
932 struct typedef_field_list *next;
933 }
934 *typedef_field_list;
935 unsigned typedef_field_list_count;
936 };
937
938 /* One item on the queue of compilation units to read in full symbols
939 for. */
940 struct dwarf2_queue_item
941 {
942 struct dwarf2_per_cu_data *per_cu;
943 enum language pretend_language;
944 struct dwarf2_queue_item *next;
945 };
946
947 /* The current queue. */
948 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
949
950 /* Loaded secondary compilation units are kept in memory until they
951 have not been referenced for the processing of this many
952 compilation units. Set this to zero to disable caching. Cache
953 sizes of up to at least twenty will improve startup time for
954 typical inter-CU-reference binaries, at an obvious memory cost. */
955 static int dwarf2_max_cache_age = 5;
956 static void
957 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
958 struct cmd_list_element *c, const char *value)
959 {
960 fprintf_filtered (file, _("The upper bound on the age of cached "
961 "dwarf2 compilation units is %s.\n"),
962 value);
963 }
964
965
966 /* Various complaints about symbol reading that don't abort the process. */
967
968 static void
969 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
970 {
971 complaint (&symfile_complaints,
972 _("statement list doesn't fit in .debug_line section"));
973 }
974
975 static void
976 dwarf2_debug_line_missing_file_complaint (void)
977 {
978 complaint (&symfile_complaints,
979 _(".debug_line section has line data without a file"));
980 }
981
982 static void
983 dwarf2_debug_line_missing_end_sequence_complaint (void)
984 {
985 complaint (&symfile_complaints,
986 _(".debug_line section has line "
987 "program sequence without an end"));
988 }
989
990 static void
991 dwarf2_complex_location_expr_complaint (void)
992 {
993 complaint (&symfile_complaints, _("location expression too complex"));
994 }
995
996 static void
997 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
998 int arg3)
999 {
1000 complaint (&symfile_complaints,
1001 _("const value length mismatch for '%s', got %d, expected %d"),
1002 arg1, arg2, arg3);
1003 }
1004
1005 static void
1006 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1007 {
1008 complaint (&symfile_complaints,
1009 _("debug info runs off end of %s section"
1010 " [in module %s]"),
1011 section->asection->name,
1012 bfd_get_filename (section->asection->owner));
1013 }
1014
1015 static void
1016 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1017 {
1018 complaint (&symfile_complaints,
1019 _("macro debug info contains a "
1020 "malformed macro definition:\n`%s'"),
1021 arg1);
1022 }
1023
1024 static void
1025 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1026 {
1027 complaint (&symfile_complaints,
1028 _("invalid attribute class or form for '%s' in '%s'"),
1029 arg1, arg2);
1030 }
1031
1032 /* local function prototypes */
1033
1034 static void dwarf2_locate_sections (bfd *, asection *, void *);
1035
1036 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1037 struct objfile *);
1038
1039 static void dwarf2_find_base_address (struct die_info *die,
1040 struct dwarf2_cu *cu);
1041
1042 static void dwarf2_build_psymtabs_hard (struct objfile *);
1043
1044 static void scan_partial_symbols (struct partial_die_info *,
1045 CORE_ADDR *, CORE_ADDR *,
1046 int, struct dwarf2_cu *);
1047
1048 static void add_partial_symbol (struct partial_die_info *,
1049 struct dwarf2_cu *);
1050
1051 static void add_partial_namespace (struct partial_die_info *pdi,
1052 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1053 int need_pc, struct dwarf2_cu *cu);
1054
1055 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1056 CORE_ADDR *highpc, int need_pc,
1057 struct dwarf2_cu *cu);
1058
1059 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1060 struct dwarf2_cu *cu);
1061
1062 static void add_partial_subprogram (struct partial_die_info *pdi,
1063 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1064 int need_pc, struct dwarf2_cu *cu);
1065
1066 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1067
1068 static void psymtab_to_symtab_1 (struct partial_symtab *);
1069
1070 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1071 struct dwarf2_section_info *);
1072
1073 static void dwarf2_free_abbrev_table (void *);
1074
1075 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1076
1077 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1078 struct dwarf2_cu *);
1079
1080 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1081 struct dwarf2_cu *);
1082
1083 static struct partial_die_info *load_partial_dies
1084 (const struct die_reader_specs *, gdb_byte *, int);
1085
1086 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1087 struct partial_die_info *,
1088 struct abbrev_info *,
1089 unsigned int,
1090 gdb_byte *);
1091
1092 static struct partial_die_info *find_partial_die (sect_offset,
1093 struct dwarf2_cu *);
1094
1095 static void fixup_partial_die (struct partial_die_info *,
1096 struct dwarf2_cu *);
1097
1098 static gdb_byte *read_attribute (const struct die_reader_specs *,
1099 struct attribute *, struct attr_abbrev *,
1100 gdb_byte *);
1101
1102 static unsigned int read_1_byte (bfd *, gdb_byte *);
1103
1104 static int read_1_signed_byte (bfd *, gdb_byte *);
1105
1106 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1107
1108 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1109
1110 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1111
1112 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1113 unsigned int *);
1114
1115 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1116
1117 static LONGEST read_checked_initial_length_and_offset
1118 (bfd *, gdb_byte *, const struct comp_unit_head *,
1119 unsigned int *, unsigned int *);
1120
1121 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1122 unsigned int *);
1123
1124 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1125
1126 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1127
1128 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1129
1130 static char *read_indirect_string (bfd *, gdb_byte *,
1131 const struct comp_unit_head *,
1132 unsigned int *);
1133
1134 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1135
1136 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1137
1138 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1139 unsigned int *);
1140
1141 static char *read_str_index (const struct die_reader_specs *reader,
1142 struct dwarf2_cu *cu, ULONGEST str_index);
1143
1144 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1145
1146 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1147 struct dwarf2_cu *);
1148
1149 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1150 unsigned int,
1151 struct dwarf2_cu *);
1152
1153 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1154 struct dwarf2_cu *cu);
1155
1156 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1157
1158 static struct die_info *die_specification (struct die_info *die,
1159 struct dwarf2_cu **);
1160
1161 static void free_line_header (struct line_header *lh);
1162
1163 static void add_file_name (struct line_header *, char *, unsigned int,
1164 unsigned int, unsigned int);
1165
1166 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1167 struct dwarf2_cu *cu);
1168
1169 static void dwarf_decode_lines (struct line_header *, const char *,
1170 struct dwarf2_cu *, struct partial_symtab *,
1171 int);
1172
1173 static void dwarf2_start_subfile (char *, const char *, const char *);
1174
1175 static struct symbol *new_symbol (struct die_info *, struct type *,
1176 struct dwarf2_cu *);
1177
1178 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1179 struct dwarf2_cu *, struct symbol *);
1180
1181 static void dwarf2_const_value (struct attribute *, struct symbol *,
1182 struct dwarf2_cu *);
1183
1184 static void dwarf2_const_value_attr (struct attribute *attr,
1185 struct type *type,
1186 const char *name,
1187 struct obstack *obstack,
1188 struct dwarf2_cu *cu, LONGEST *value,
1189 gdb_byte **bytes,
1190 struct dwarf2_locexpr_baton **baton);
1191
1192 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1193
1194 static int need_gnat_info (struct dwarf2_cu *);
1195
1196 static struct type *die_descriptive_type (struct die_info *,
1197 struct dwarf2_cu *);
1198
1199 static void set_descriptive_type (struct type *, struct die_info *,
1200 struct dwarf2_cu *);
1201
1202 static struct type *die_containing_type (struct die_info *,
1203 struct dwarf2_cu *);
1204
1205 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1206 struct dwarf2_cu *);
1207
1208 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1209
1210 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1211
1212 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1213
1214 static char *typename_concat (struct obstack *obs, const char *prefix,
1215 const char *suffix, int physname,
1216 struct dwarf2_cu *cu);
1217
1218 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1219
1220 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1221
1222 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1223
1224 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1225
1226 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1227
1228 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1229 struct dwarf2_cu *, struct partial_symtab *);
1230
1231 static int dwarf2_get_pc_bounds (struct die_info *,
1232 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1233 struct partial_symtab *);
1234
1235 static void get_scope_pc_bounds (struct die_info *,
1236 CORE_ADDR *, CORE_ADDR *,
1237 struct dwarf2_cu *);
1238
1239 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1240 CORE_ADDR, struct dwarf2_cu *);
1241
1242 static void dwarf2_add_field (struct field_info *, struct die_info *,
1243 struct dwarf2_cu *);
1244
1245 static void dwarf2_attach_fields_to_type (struct field_info *,
1246 struct type *, struct dwarf2_cu *);
1247
1248 static void dwarf2_add_member_fn (struct field_info *,
1249 struct die_info *, struct type *,
1250 struct dwarf2_cu *);
1251
1252 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1253 struct type *,
1254 struct dwarf2_cu *);
1255
1256 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1257
1258 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1259
1260 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1261
1262 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1263
1264 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1265
1266 static struct type *read_module_type (struct die_info *die,
1267 struct dwarf2_cu *cu);
1268
1269 static const char *namespace_name (struct die_info *die,
1270 int *is_anonymous, struct dwarf2_cu *);
1271
1272 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1273
1274 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1275
1276 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1277 struct dwarf2_cu *);
1278
1279 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1280 gdb_byte *info_ptr,
1281 gdb_byte **new_info_ptr,
1282 struct die_info *parent);
1283
1284 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1285 gdb_byte *info_ptr,
1286 gdb_byte **new_info_ptr,
1287 struct die_info *parent);
1288
1289 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1290 struct die_info **, gdb_byte *, int *, int);
1291
1292 static gdb_byte *read_full_die (const struct die_reader_specs *,
1293 struct die_info **, gdb_byte *, int *);
1294
1295 static void process_die (struct die_info *, struct dwarf2_cu *);
1296
1297 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1298 struct obstack *);
1299
1300 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1301
1302 static const char *dwarf2_full_name (char *name,
1303 struct die_info *die,
1304 struct dwarf2_cu *cu);
1305
1306 static struct die_info *dwarf2_extension (struct die_info *die,
1307 struct dwarf2_cu **);
1308
1309 static const char *dwarf_tag_name (unsigned int);
1310
1311 static const char *dwarf_attr_name (unsigned int);
1312
1313 static const char *dwarf_form_name (unsigned int);
1314
1315 static char *dwarf_bool_name (unsigned int);
1316
1317 static const char *dwarf_type_encoding_name (unsigned int);
1318
1319 static struct die_info *sibling_die (struct die_info *);
1320
1321 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1322
1323 static void dump_die_for_error (struct die_info *);
1324
1325 static void dump_die_1 (struct ui_file *, int level, int max_level,
1326 struct die_info *);
1327
1328 /*static*/ void dump_die (struct die_info *, int max_level);
1329
1330 static void store_in_ref_table (struct die_info *,
1331 struct dwarf2_cu *);
1332
1333 static int is_ref_attr (struct attribute *);
1334
1335 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1336
1337 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1338
1339 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1340 struct attribute *,
1341 struct dwarf2_cu **);
1342
1343 static struct die_info *follow_die_ref (struct die_info *,
1344 struct attribute *,
1345 struct dwarf2_cu **);
1346
1347 static struct die_info *follow_die_sig (struct die_info *,
1348 struct attribute *,
1349 struct dwarf2_cu **);
1350
1351 static struct signatured_type *lookup_signatured_type_at_offset
1352 (struct objfile *objfile,
1353 struct dwarf2_section_info *section, sect_offset offset);
1354
1355 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1356
1357 static void read_signatured_type (struct signatured_type *);
1358
1359 /* memory allocation interface */
1360
1361 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1362
1363 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1364
1365 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1366
1367 static void dwarf_decode_macros (struct line_header *, unsigned int,
1368 char *, bfd *, struct dwarf2_cu *,
1369 struct dwarf2_section_info *,
1370 int, const char *);
1371
1372 static int attr_form_is_block (struct attribute *);
1373
1374 static int attr_form_is_section_offset (struct attribute *);
1375
1376 static int attr_form_is_constant (struct attribute *);
1377
1378 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1379 struct dwarf2_loclist_baton *baton,
1380 struct attribute *attr);
1381
1382 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1383 struct symbol *sym,
1384 struct dwarf2_cu *cu);
1385
1386 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1387 gdb_byte *info_ptr,
1388 struct abbrev_info *abbrev);
1389
1390 static void free_stack_comp_unit (void *);
1391
1392 static hashval_t partial_die_hash (const void *item);
1393
1394 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1395
1396 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1397 (sect_offset offset, struct objfile *objfile);
1398
1399 static void init_one_comp_unit (struct dwarf2_cu *cu,
1400 struct dwarf2_per_cu_data *per_cu);
1401
1402 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1403 struct die_info *comp_unit_die,
1404 enum language pretend_language);
1405
1406 static void free_heap_comp_unit (void *);
1407
1408 static void free_cached_comp_units (void *);
1409
1410 static void age_cached_comp_units (void);
1411
1412 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1413
1414 static struct type *set_die_type (struct die_info *, struct type *,
1415 struct dwarf2_cu *);
1416
1417 static void create_all_comp_units (struct objfile *);
1418
1419 static int create_all_type_units (struct objfile *);
1420
1421 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1422 enum language);
1423
1424 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1425 enum language);
1426
1427 static void dwarf2_add_dependence (struct dwarf2_cu *,
1428 struct dwarf2_per_cu_data *);
1429
1430 static void dwarf2_mark (struct dwarf2_cu *);
1431
1432 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1433
1434 static struct type *get_die_type_at_offset (sect_offset,
1435 struct dwarf2_per_cu_data *per_cu);
1436
1437 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1438
1439 static void dwarf2_release_queue (void *dummy);
1440
1441 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1442 enum language pretend_language);
1443
1444 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1445 struct dwarf2_per_cu_data *per_cu,
1446 enum language pretend_language);
1447
1448 static void process_queue (void);
1449
1450 static void find_file_and_directory (struct die_info *die,
1451 struct dwarf2_cu *cu,
1452 char **name, char **comp_dir);
1453
1454 static char *file_full_name (int file, struct line_header *lh,
1455 const char *comp_dir);
1456
1457 static gdb_byte *read_and_check_comp_unit_head
1458 (struct comp_unit_head *header,
1459 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1460 int is_debug_types_section);
1461
1462 static void init_cutu_and_read_dies
1463 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1464 die_reader_func_ftype *die_reader_func, void *data);
1465
1466 static void init_cutu_and_read_dies_simple
1467 (struct dwarf2_per_cu_data *this_cu,
1468 die_reader_func_ftype *die_reader_func, void *data);
1469
1470 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1471
1472 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1473
1474 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1475
1476 static struct dwo_unit *lookup_dwo_comp_unit
1477 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1478
1479 static struct dwo_unit *lookup_dwo_type_unit
1480 (struct signatured_type *, char *, const char *);
1481
1482 static void free_dwo_file_cleanup (void *);
1483
1484 static void munmap_section_buffer (struct dwarf2_section_info *);
1485
1486 static void process_cu_includes (void);
1487
1488 #if WORDS_BIGENDIAN
1489
1490 /* Convert VALUE between big- and little-endian. */
1491 static offset_type
1492 byte_swap (offset_type value)
1493 {
1494 offset_type result;
1495
1496 result = (value & 0xff) << 24;
1497 result |= (value & 0xff00) << 8;
1498 result |= (value & 0xff0000) >> 8;
1499 result |= (value & 0xff000000) >> 24;
1500 return result;
1501 }
1502
1503 #define MAYBE_SWAP(V) byte_swap (V)
1504
1505 #else
1506 #define MAYBE_SWAP(V) (V)
1507 #endif /* WORDS_BIGENDIAN */
1508
1509 /* The suffix for an index file. */
1510 #define INDEX_SUFFIX ".gdb-index"
1511
1512 static const char *dwarf2_physname (char *name, struct die_info *die,
1513 struct dwarf2_cu *cu);
1514
1515 /* Try to locate the sections we need for DWARF 2 debugging
1516 information and return true if we have enough to do something.
1517 NAMES points to the dwarf2 section names, or is NULL if the standard
1518 ELF names are used. */
1519
1520 int
1521 dwarf2_has_info (struct objfile *objfile,
1522 const struct dwarf2_debug_sections *names)
1523 {
1524 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1525 if (!dwarf2_per_objfile)
1526 {
1527 /* Initialize per-objfile state. */
1528 struct dwarf2_per_objfile *data
1529 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1530
1531 memset (data, 0, sizeof (*data));
1532 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1533 dwarf2_per_objfile = data;
1534
1535 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1536 (void *) names);
1537 dwarf2_per_objfile->objfile = objfile;
1538 }
1539 return (dwarf2_per_objfile->info.asection != NULL
1540 && dwarf2_per_objfile->abbrev.asection != NULL);
1541 }
1542
1543 /* When loading sections, we look either for uncompressed section or for
1544 compressed section names. */
1545
1546 static int
1547 section_is_p (const char *section_name,
1548 const struct dwarf2_section_names *names)
1549 {
1550 if (names->normal != NULL
1551 && strcmp (section_name, names->normal) == 0)
1552 return 1;
1553 if (names->compressed != NULL
1554 && strcmp (section_name, names->compressed) == 0)
1555 return 1;
1556 return 0;
1557 }
1558
1559 /* This function is mapped across the sections and remembers the
1560 offset and size of each of the debugging sections we are interested
1561 in. */
1562
1563 static void
1564 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1565 {
1566 const struct dwarf2_debug_sections *names;
1567
1568 if (vnames == NULL)
1569 names = &dwarf2_elf_names;
1570 else
1571 names = (const struct dwarf2_debug_sections *) vnames;
1572
1573 if (section_is_p (sectp->name, &names->info))
1574 {
1575 dwarf2_per_objfile->info.asection = sectp;
1576 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1577 }
1578 else if (section_is_p (sectp->name, &names->abbrev))
1579 {
1580 dwarf2_per_objfile->abbrev.asection = sectp;
1581 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1582 }
1583 else if (section_is_p (sectp->name, &names->line))
1584 {
1585 dwarf2_per_objfile->line.asection = sectp;
1586 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1587 }
1588 else if (section_is_p (sectp->name, &names->loc))
1589 {
1590 dwarf2_per_objfile->loc.asection = sectp;
1591 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1592 }
1593 else if (section_is_p (sectp->name, &names->macinfo))
1594 {
1595 dwarf2_per_objfile->macinfo.asection = sectp;
1596 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1597 }
1598 else if (section_is_p (sectp->name, &names->macro))
1599 {
1600 dwarf2_per_objfile->macro.asection = sectp;
1601 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1602 }
1603 else if (section_is_p (sectp->name, &names->str))
1604 {
1605 dwarf2_per_objfile->str.asection = sectp;
1606 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1607 }
1608 else if (section_is_p (sectp->name, &names->addr))
1609 {
1610 dwarf2_per_objfile->addr.asection = sectp;
1611 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1612 }
1613 else if (section_is_p (sectp->name, &names->frame))
1614 {
1615 dwarf2_per_objfile->frame.asection = sectp;
1616 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1617 }
1618 else if (section_is_p (sectp->name, &names->eh_frame))
1619 {
1620 flagword aflag = bfd_get_section_flags (abfd, sectp);
1621
1622 if (aflag & SEC_HAS_CONTENTS)
1623 {
1624 dwarf2_per_objfile->eh_frame.asection = sectp;
1625 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1626 }
1627 }
1628 else if (section_is_p (sectp->name, &names->ranges))
1629 {
1630 dwarf2_per_objfile->ranges.asection = sectp;
1631 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1632 }
1633 else if (section_is_p (sectp->name, &names->types))
1634 {
1635 struct dwarf2_section_info type_section;
1636
1637 memset (&type_section, 0, sizeof (type_section));
1638 type_section.asection = sectp;
1639 type_section.size = bfd_get_section_size (sectp);
1640
1641 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1642 &type_section);
1643 }
1644 else if (section_is_p (sectp->name, &names->gdb_index))
1645 {
1646 dwarf2_per_objfile->gdb_index.asection = sectp;
1647 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1648 }
1649
1650 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1651 && bfd_section_vma (abfd, sectp) == 0)
1652 dwarf2_per_objfile->has_section_at_zero = 1;
1653 }
1654
1655 /* Decompress a section that was compressed using zlib. Store the
1656 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1657
1658 static void
1659 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1660 gdb_byte **outbuf, bfd_size_type *outsize)
1661 {
1662 bfd *abfd = sectp->owner;
1663 #ifndef HAVE_ZLIB_H
1664 error (_("Support for zlib-compressed DWARF data (from '%s') "
1665 "is disabled in this copy of GDB"),
1666 bfd_get_filename (abfd));
1667 #else
1668 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1669 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1670 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1671 bfd_size_type uncompressed_size;
1672 gdb_byte *uncompressed_buffer;
1673 z_stream strm;
1674 int rc;
1675 int header_size = 12;
1676
1677 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1678 || bfd_bread (compressed_buffer,
1679 compressed_size, abfd) != compressed_size)
1680 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1681 bfd_get_filename (abfd));
1682
1683 /* Read the zlib header. In this case, it should be "ZLIB" followed
1684 by the uncompressed section size, 8 bytes in big-endian order. */
1685 if (compressed_size < header_size
1686 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1687 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1688 bfd_get_filename (abfd));
1689 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1690 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1691 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1692 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1693 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1694 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1695 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1696 uncompressed_size += compressed_buffer[11];
1697
1698 /* It is possible the section consists of several compressed
1699 buffers concatenated together, so we uncompress in a loop. */
1700 strm.zalloc = NULL;
1701 strm.zfree = NULL;
1702 strm.opaque = NULL;
1703 strm.avail_in = compressed_size - header_size;
1704 strm.next_in = (Bytef*) compressed_buffer + header_size;
1705 strm.avail_out = uncompressed_size;
1706 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1707 uncompressed_size);
1708 rc = inflateInit (&strm);
1709 while (strm.avail_in > 0)
1710 {
1711 if (rc != Z_OK)
1712 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1713 bfd_get_filename (abfd), rc);
1714 strm.next_out = ((Bytef*) uncompressed_buffer
1715 + (uncompressed_size - strm.avail_out));
1716 rc = inflate (&strm, Z_FINISH);
1717 if (rc != Z_STREAM_END)
1718 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1719 bfd_get_filename (abfd), rc);
1720 rc = inflateReset (&strm);
1721 }
1722 rc = inflateEnd (&strm);
1723 if (rc != Z_OK
1724 || strm.avail_out != 0)
1725 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1726 bfd_get_filename (abfd), rc);
1727
1728 do_cleanups (cleanup);
1729 *outbuf = uncompressed_buffer;
1730 *outsize = uncompressed_size;
1731 #endif
1732 }
1733
1734 /* A helper function that decides whether a section is empty,
1735 or not present. */
1736
1737 static int
1738 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1739 {
1740 return info->asection == NULL || info->size == 0;
1741 }
1742
1743 /* Read the contents of the section INFO.
1744 OBJFILE is the main object file, but not necessarily the file where
1745 the section comes from. E.g., for DWO files INFO->asection->owner
1746 is the bfd of the DWO file.
1747 If the section is compressed, uncompress it before returning. */
1748
1749 static void
1750 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1751 {
1752 asection *sectp = info->asection;
1753 bfd *abfd;
1754 gdb_byte *buf, *retbuf;
1755 unsigned char header[4];
1756
1757 if (info->readin)
1758 return;
1759 info->buffer = NULL;
1760 info->map_addr = NULL;
1761 info->readin = 1;
1762
1763 if (dwarf2_section_empty_p (info))
1764 return;
1765
1766 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1767 abfd = sectp->owner;
1768
1769 /* Check if the file has a 4-byte header indicating compression. */
1770 if (info->size > sizeof (header)
1771 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1772 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1773 {
1774 /* Upon decompression, update the buffer and its size. */
1775 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1776 {
1777 zlib_decompress_section (objfile, sectp, &info->buffer,
1778 &info->size);
1779 return;
1780 }
1781 }
1782
1783 #ifdef HAVE_MMAP
1784 if (pagesize == 0)
1785 pagesize = getpagesize ();
1786
1787 /* Only try to mmap sections which are large enough: we don't want to
1788 waste space due to fragmentation. Also, only try mmap for sections
1789 without relocations. */
1790
1791 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1792 {
1793 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1794 MAP_PRIVATE, sectp->filepos,
1795 &info->map_addr, &info->map_len);
1796
1797 if ((caddr_t)info->buffer != MAP_FAILED)
1798 {
1799 #if HAVE_POSIX_MADVISE
1800 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1801 #endif
1802 return;
1803 }
1804 }
1805 #endif
1806
1807 /* If we get here, we are a normal, not-compressed section. */
1808 info->buffer = buf
1809 = obstack_alloc (&objfile->objfile_obstack, info->size);
1810
1811 /* When debugging .o files, we may need to apply relocations; see
1812 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1813 We never compress sections in .o files, so we only need to
1814 try this when the section is not compressed. */
1815 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1816 if (retbuf != NULL)
1817 {
1818 info->buffer = retbuf;
1819 return;
1820 }
1821
1822 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1823 || bfd_bread (buf, info->size, abfd) != info->size)
1824 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1825 bfd_get_filename (abfd));
1826 }
1827
1828 /* A helper function that returns the size of a section in a safe way.
1829 If you are positive that the section has been read before using the
1830 size, then it is safe to refer to the dwarf2_section_info object's
1831 "size" field directly. In other cases, you must call this
1832 function, because for compressed sections the size field is not set
1833 correctly until the section has been read. */
1834
1835 static bfd_size_type
1836 dwarf2_section_size (struct objfile *objfile,
1837 struct dwarf2_section_info *info)
1838 {
1839 if (!info->readin)
1840 dwarf2_read_section (objfile, info);
1841 return info->size;
1842 }
1843
1844 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1845 SECTION_NAME. */
1846
1847 void
1848 dwarf2_get_section_info (struct objfile *objfile,
1849 enum dwarf2_section_enum sect,
1850 asection **sectp, gdb_byte **bufp,
1851 bfd_size_type *sizep)
1852 {
1853 struct dwarf2_per_objfile *data
1854 = objfile_data (objfile, dwarf2_objfile_data_key);
1855 struct dwarf2_section_info *info;
1856
1857 /* We may see an objfile without any DWARF, in which case we just
1858 return nothing. */
1859 if (data == NULL)
1860 {
1861 *sectp = NULL;
1862 *bufp = NULL;
1863 *sizep = 0;
1864 return;
1865 }
1866 switch (sect)
1867 {
1868 case DWARF2_DEBUG_FRAME:
1869 info = &data->frame;
1870 break;
1871 case DWARF2_EH_FRAME:
1872 info = &data->eh_frame;
1873 break;
1874 default:
1875 gdb_assert_not_reached ("unexpected section");
1876 }
1877
1878 dwarf2_read_section (objfile, info);
1879
1880 *sectp = info->asection;
1881 *bufp = info->buffer;
1882 *sizep = info->size;
1883 }
1884
1885 \f
1886 /* DWARF quick_symbols_functions support. */
1887
1888 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1889 unique line tables, so we maintain a separate table of all .debug_line
1890 derived entries to support the sharing.
1891 All the quick functions need is the list of file names. We discard the
1892 line_header when we're done and don't need to record it here. */
1893 struct quick_file_names
1894 {
1895 /* The offset in .debug_line of the line table. We hash on this. */
1896 unsigned int offset;
1897
1898 /* The number of entries in file_names, real_names. */
1899 unsigned int num_file_names;
1900
1901 /* The file names from the line table, after being run through
1902 file_full_name. */
1903 const char **file_names;
1904
1905 /* The file names from the line table after being run through
1906 gdb_realpath. These are computed lazily. */
1907 const char **real_names;
1908 };
1909
1910 /* When using the index (and thus not using psymtabs), each CU has an
1911 object of this type. This is used to hold information needed by
1912 the various "quick" methods. */
1913 struct dwarf2_per_cu_quick_data
1914 {
1915 /* The file table. This can be NULL if there was no file table
1916 or it's currently not read in.
1917 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1918 struct quick_file_names *file_names;
1919
1920 /* The corresponding symbol table. This is NULL if symbols for this
1921 CU have not yet been read. */
1922 struct symtab *symtab;
1923
1924 /* A temporary mark bit used when iterating over all CUs in
1925 expand_symtabs_matching. */
1926 unsigned int mark : 1;
1927
1928 /* True if we've tried to read the file table and found there isn't one.
1929 There will be no point in trying to read it again next time. */
1930 unsigned int no_file_data : 1;
1931 };
1932
1933 /* Hash function for a quick_file_names. */
1934
1935 static hashval_t
1936 hash_file_name_entry (const void *e)
1937 {
1938 const struct quick_file_names *file_data = e;
1939
1940 return file_data->offset;
1941 }
1942
1943 /* Equality function for a quick_file_names. */
1944
1945 static int
1946 eq_file_name_entry (const void *a, const void *b)
1947 {
1948 const struct quick_file_names *ea = a;
1949 const struct quick_file_names *eb = b;
1950
1951 return ea->offset == eb->offset;
1952 }
1953
1954 /* Delete function for a quick_file_names. */
1955
1956 static void
1957 delete_file_name_entry (void *e)
1958 {
1959 struct quick_file_names *file_data = e;
1960 int i;
1961
1962 for (i = 0; i < file_data->num_file_names; ++i)
1963 {
1964 xfree ((void*) file_data->file_names[i]);
1965 if (file_data->real_names)
1966 xfree ((void*) file_data->real_names[i]);
1967 }
1968
1969 /* The space for the struct itself lives on objfile_obstack,
1970 so we don't free it here. */
1971 }
1972
1973 /* Create a quick_file_names hash table. */
1974
1975 static htab_t
1976 create_quick_file_names_table (unsigned int nr_initial_entries)
1977 {
1978 return htab_create_alloc (nr_initial_entries,
1979 hash_file_name_entry, eq_file_name_entry,
1980 delete_file_name_entry, xcalloc, xfree);
1981 }
1982
1983 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1984 have to be created afterwards. You should call age_cached_comp_units after
1985 processing PER_CU->CU. dw2_setup must have been already called. */
1986
1987 static void
1988 load_cu (struct dwarf2_per_cu_data *per_cu)
1989 {
1990 if (per_cu->is_debug_types)
1991 load_full_type_unit (per_cu);
1992 else
1993 load_full_comp_unit (per_cu, language_minimal);
1994
1995 gdb_assert (per_cu->cu != NULL);
1996
1997 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1998 }
1999
2000 /* Read in the symbols for PER_CU. */
2001
2002 static void
2003 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2004 {
2005 struct cleanup *back_to;
2006
2007 back_to = make_cleanup (dwarf2_release_queue, NULL);
2008
2009 if (dwarf2_per_objfile->using_index
2010 ? per_cu->v.quick->symtab == NULL
2011 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2012 {
2013 queue_comp_unit (per_cu, language_minimal);
2014 load_cu (per_cu);
2015 }
2016
2017 process_queue ();
2018
2019 /* Age the cache, releasing compilation units that have not
2020 been used recently. */
2021 age_cached_comp_units ();
2022
2023 do_cleanups (back_to);
2024 }
2025
2026 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2027 the objfile from which this CU came. Returns the resulting symbol
2028 table. */
2029
2030 static struct symtab *
2031 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2032 {
2033 gdb_assert (dwarf2_per_objfile->using_index);
2034 if (!per_cu->v.quick->symtab)
2035 {
2036 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2037 increment_reading_symtab ();
2038 dw2_do_instantiate_symtab (per_cu);
2039 process_cu_includes ();
2040 do_cleanups (back_to);
2041 }
2042 return per_cu->v.quick->symtab;
2043 }
2044
2045 /* Return the CU given its index. */
2046
2047 static struct dwarf2_per_cu_data *
2048 dw2_get_cu (int index)
2049 {
2050 if (index >= dwarf2_per_objfile->n_comp_units)
2051 {
2052 index -= dwarf2_per_objfile->n_comp_units;
2053 return dwarf2_per_objfile->all_type_units[index];
2054 }
2055 return dwarf2_per_objfile->all_comp_units[index];
2056 }
2057
2058 /* A helper function that knows how to read a 64-bit value in a way
2059 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2060 otherwise. */
2061
2062 static int
2063 extract_cu_value (const char *bytes, ULONGEST *result)
2064 {
2065 if (sizeof (ULONGEST) < 8)
2066 {
2067 int i;
2068
2069 /* Ignore the upper 4 bytes if they are all zero. */
2070 for (i = 0; i < 4; ++i)
2071 if (bytes[i + 4] != 0)
2072 return 0;
2073
2074 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2075 }
2076 else
2077 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2078 return 1;
2079 }
2080
2081 /* Read the CU list from the mapped index, and use it to create all
2082 the CU objects for this objfile. Return 0 if something went wrong,
2083 1 if everything went ok. */
2084
2085 static int
2086 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2087 offset_type cu_list_elements)
2088 {
2089 offset_type i;
2090
2091 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2092 dwarf2_per_objfile->all_comp_units
2093 = obstack_alloc (&objfile->objfile_obstack,
2094 dwarf2_per_objfile->n_comp_units
2095 * sizeof (struct dwarf2_per_cu_data *));
2096
2097 for (i = 0; i < cu_list_elements; i += 2)
2098 {
2099 struct dwarf2_per_cu_data *the_cu;
2100 ULONGEST offset, length;
2101
2102 if (!extract_cu_value (cu_list, &offset)
2103 || !extract_cu_value (cu_list + 8, &length))
2104 return 0;
2105 cu_list += 2 * 8;
2106
2107 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2108 struct dwarf2_per_cu_data);
2109 the_cu->offset.sect_off = offset;
2110 the_cu->length = length;
2111 the_cu->objfile = objfile;
2112 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2113 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2114 struct dwarf2_per_cu_quick_data);
2115 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2116 }
2117
2118 return 1;
2119 }
2120
2121 /* Create the signatured type hash table from the index. */
2122
2123 static int
2124 create_signatured_type_table_from_index (struct objfile *objfile,
2125 struct dwarf2_section_info *section,
2126 const gdb_byte *bytes,
2127 offset_type elements)
2128 {
2129 offset_type i;
2130 htab_t sig_types_hash;
2131
2132 dwarf2_per_objfile->n_type_units = elements / 3;
2133 dwarf2_per_objfile->all_type_units
2134 = obstack_alloc (&objfile->objfile_obstack,
2135 dwarf2_per_objfile->n_type_units
2136 * sizeof (struct dwarf2_per_cu_data *));
2137
2138 sig_types_hash = allocate_signatured_type_table (objfile);
2139
2140 for (i = 0; i < elements; i += 3)
2141 {
2142 struct signatured_type *sig_type;
2143 ULONGEST offset, type_offset_in_tu, signature;
2144 void **slot;
2145
2146 if (!extract_cu_value (bytes, &offset)
2147 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2148 return 0;
2149 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2150 bytes += 3 * 8;
2151
2152 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2153 struct signatured_type);
2154 sig_type->signature = signature;
2155 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2156 sig_type->per_cu.is_debug_types = 1;
2157 sig_type->per_cu.info_or_types_section = section;
2158 sig_type->per_cu.offset.sect_off = offset;
2159 sig_type->per_cu.objfile = objfile;
2160 sig_type->per_cu.v.quick
2161 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2162 struct dwarf2_per_cu_quick_data);
2163
2164 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2165 *slot = sig_type;
2166
2167 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2168 }
2169
2170 dwarf2_per_objfile->signatured_types = sig_types_hash;
2171
2172 return 1;
2173 }
2174
2175 /* Read the address map data from the mapped index, and use it to
2176 populate the objfile's psymtabs_addrmap. */
2177
2178 static void
2179 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2180 {
2181 const gdb_byte *iter, *end;
2182 struct obstack temp_obstack;
2183 struct addrmap *mutable_map;
2184 struct cleanup *cleanup;
2185 CORE_ADDR baseaddr;
2186
2187 obstack_init (&temp_obstack);
2188 cleanup = make_cleanup_obstack_free (&temp_obstack);
2189 mutable_map = addrmap_create_mutable (&temp_obstack);
2190
2191 iter = index->address_table;
2192 end = iter + index->address_table_size;
2193
2194 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2195
2196 while (iter < end)
2197 {
2198 ULONGEST hi, lo, cu_index;
2199 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2200 iter += 8;
2201 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2202 iter += 8;
2203 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2204 iter += 4;
2205
2206 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2207 dw2_get_cu (cu_index));
2208 }
2209
2210 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2211 &objfile->objfile_obstack);
2212 do_cleanups (cleanup);
2213 }
2214
2215 /* The hash function for strings in the mapped index. This is the same as
2216 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2217 implementation. This is necessary because the hash function is tied to the
2218 format of the mapped index file. The hash values do not have to match with
2219 SYMBOL_HASH_NEXT.
2220
2221 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2222
2223 static hashval_t
2224 mapped_index_string_hash (int index_version, const void *p)
2225 {
2226 const unsigned char *str = (const unsigned char *) p;
2227 hashval_t r = 0;
2228 unsigned char c;
2229
2230 while ((c = *str++) != 0)
2231 {
2232 if (index_version >= 5)
2233 c = tolower (c);
2234 r = r * 67 + c - 113;
2235 }
2236
2237 return r;
2238 }
2239
2240 /* Find a slot in the mapped index INDEX for the object named NAME.
2241 If NAME is found, set *VEC_OUT to point to the CU vector in the
2242 constant pool and return 1. If NAME cannot be found, return 0. */
2243
2244 static int
2245 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2246 offset_type **vec_out)
2247 {
2248 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2249 offset_type hash;
2250 offset_type slot, step;
2251 int (*cmp) (const char *, const char *);
2252
2253 if (current_language->la_language == language_cplus
2254 || current_language->la_language == language_java
2255 || current_language->la_language == language_fortran)
2256 {
2257 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2258 not contain any. */
2259 const char *paren = strchr (name, '(');
2260
2261 if (paren)
2262 {
2263 char *dup;
2264
2265 dup = xmalloc (paren - name + 1);
2266 memcpy (dup, name, paren - name);
2267 dup[paren - name] = 0;
2268
2269 make_cleanup (xfree, dup);
2270 name = dup;
2271 }
2272 }
2273
2274 /* Index version 4 did not support case insensitive searches. But the
2275 indices for case insensitive languages are built in lowercase, therefore
2276 simulate our NAME being searched is also lowercased. */
2277 hash = mapped_index_string_hash ((index->version == 4
2278 && case_sensitivity == case_sensitive_off
2279 ? 5 : index->version),
2280 name);
2281
2282 slot = hash & (index->symbol_table_slots - 1);
2283 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2284 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2285
2286 for (;;)
2287 {
2288 /* Convert a slot number to an offset into the table. */
2289 offset_type i = 2 * slot;
2290 const char *str;
2291 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2292 {
2293 do_cleanups (back_to);
2294 return 0;
2295 }
2296
2297 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2298 if (!cmp (name, str))
2299 {
2300 *vec_out = (offset_type *) (index->constant_pool
2301 + MAYBE_SWAP (index->symbol_table[i + 1]));
2302 do_cleanups (back_to);
2303 return 1;
2304 }
2305
2306 slot = (slot + step) & (index->symbol_table_slots - 1);
2307 }
2308 }
2309
2310 /* Read the index file. If everything went ok, initialize the "quick"
2311 elements of all the CUs and return 1. Otherwise, return 0. */
2312
2313 static int
2314 dwarf2_read_index (struct objfile *objfile)
2315 {
2316 char *addr;
2317 struct mapped_index *map;
2318 offset_type *metadata;
2319 const gdb_byte *cu_list;
2320 const gdb_byte *types_list = NULL;
2321 offset_type version, cu_list_elements;
2322 offset_type types_list_elements = 0;
2323 int i;
2324
2325 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2326 return 0;
2327
2328 /* Older elfutils strip versions could keep the section in the main
2329 executable while splitting it for the separate debug info file. */
2330 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2331 & SEC_HAS_CONTENTS) == 0)
2332 return 0;
2333
2334 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2335
2336 addr = dwarf2_per_objfile->gdb_index.buffer;
2337 /* Version check. */
2338 version = MAYBE_SWAP (*(offset_type *) addr);
2339 /* Versions earlier than 3 emitted every copy of a psymbol. This
2340 causes the index to behave very poorly for certain requests. Version 3
2341 contained incomplete addrmap. So, it seems better to just ignore such
2342 indices. */
2343 if (version < 4)
2344 {
2345 static int warning_printed = 0;
2346 if (!warning_printed)
2347 {
2348 warning (_("Skipping obsolete .gdb_index section in %s."),
2349 objfile->name);
2350 warning_printed = 1;
2351 }
2352 return 0;
2353 }
2354 /* Index version 4 uses a different hash function than index version
2355 5 and later.
2356
2357 Versions earlier than 6 did not emit psymbols for inlined
2358 functions. Using these files will cause GDB not to be able to
2359 set breakpoints on inlined functions by name, so we ignore these
2360 indices unless the --use-deprecated-index-sections command line
2361 option was supplied. */
2362 if (version < 6 && !use_deprecated_index_sections)
2363 {
2364 static int warning_printed = 0;
2365 if (!warning_printed)
2366 {
2367 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2368 "--use-deprecated-index-sections to use them anyway"),
2369 objfile->name);
2370 warning_printed = 1;
2371 }
2372 return 0;
2373 }
2374 /* Indexes with higher version than the one supported by GDB may be no
2375 longer backward compatible. */
2376 if (version > 7)
2377 return 0;
2378
2379 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2380 map->version = version;
2381 map->total_size = dwarf2_per_objfile->gdb_index.size;
2382
2383 metadata = (offset_type *) (addr + sizeof (offset_type));
2384
2385 i = 0;
2386 cu_list = addr + MAYBE_SWAP (metadata[i]);
2387 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2388 / 8);
2389 ++i;
2390
2391 types_list = addr + MAYBE_SWAP (metadata[i]);
2392 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2393 - MAYBE_SWAP (metadata[i]))
2394 / 8);
2395 ++i;
2396
2397 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2398 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2399 - MAYBE_SWAP (metadata[i]));
2400 ++i;
2401
2402 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2403 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2404 - MAYBE_SWAP (metadata[i]))
2405 / (2 * sizeof (offset_type)));
2406 ++i;
2407
2408 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2409
2410 /* Don't use the index if it's empty. */
2411 if (map->symbol_table_slots == 0)
2412 return 0;
2413
2414 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2415 return 0;
2416
2417 if (types_list_elements)
2418 {
2419 struct dwarf2_section_info *section;
2420
2421 /* We can only handle a single .debug_types when we have an
2422 index. */
2423 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2424 return 0;
2425
2426 section = VEC_index (dwarf2_section_info_def,
2427 dwarf2_per_objfile->types, 0);
2428
2429 if (!create_signatured_type_table_from_index (objfile, section,
2430 types_list,
2431 types_list_elements))
2432 return 0;
2433 }
2434
2435 create_addrmap_from_index (objfile, map);
2436
2437 dwarf2_per_objfile->index_table = map;
2438 dwarf2_per_objfile->using_index = 1;
2439 dwarf2_per_objfile->quick_file_names_table =
2440 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2441
2442 return 1;
2443 }
2444
2445 /* A helper for the "quick" functions which sets the global
2446 dwarf2_per_objfile according to OBJFILE. */
2447
2448 static void
2449 dw2_setup (struct objfile *objfile)
2450 {
2451 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2452 gdb_assert (dwarf2_per_objfile);
2453 }
2454
2455 /* die_reader_func for dw2_get_file_names. */
2456
2457 static void
2458 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2459 gdb_byte *info_ptr,
2460 struct die_info *comp_unit_die,
2461 int has_children,
2462 void *data)
2463 {
2464 struct dwarf2_cu *cu = reader->cu;
2465 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2466 struct objfile *objfile = dwarf2_per_objfile->objfile;
2467 struct line_header *lh;
2468 struct attribute *attr;
2469 int i;
2470 char *name, *comp_dir;
2471 void **slot;
2472 struct quick_file_names *qfn;
2473 unsigned int line_offset;
2474
2475 /* Our callers never want to match partial units -- instead they
2476 will match the enclosing full CU. */
2477 if (comp_unit_die->tag == DW_TAG_partial_unit)
2478 {
2479 this_cu->v.quick->no_file_data = 1;
2480 return;
2481 }
2482
2483 lh = NULL;
2484 slot = NULL;
2485 line_offset = 0;
2486
2487 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2488 if (attr)
2489 {
2490 struct quick_file_names find_entry;
2491
2492 line_offset = DW_UNSND (attr);
2493
2494 /* We may have already read in this line header (TU line header sharing).
2495 If we have we're done. */
2496 find_entry.offset = line_offset;
2497 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2498 &find_entry, INSERT);
2499 if (*slot != NULL)
2500 {
2501 this_cu->v.quick->file_names = *slot;
2502 return;
2503 }
2504
2505 lh = dwarf_decode_line_header (line_offset, cu);
2506 }
2507 if (lh == NULL)
2508 {
2509 this_cu->v.quick->no_file_data = 1;
2510 return;
2511 }
2512
2513 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2514 qfn->offset = line_offset;
2515 gdb_assert (slot != NULL);
2516 *slot = qfn;
2517
2518 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2519
2520 qfn->num_file_names = lh->num_file_names;
2521 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2522 lh->num_file_names * sizeof (char *));
2523 for (i = 0; i < lh->num_file_names; ++i)
2524 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2525 qfn->real_names = NULL;
2526
2527 free_line_header (lh);
2528
2529 this_cu->v.quick->file_names = qfn;
2530 }
2531
2532 /* A helper for the "quick" functions which attempts to read the line
2533 table for THIS_CU. */
2534
2535 static struct quick_file_names *
2536 dw2_get_file_names (struct objfile *objfile,
2537 struct dwarf2_per_cu_data *this_cu)
2538 {
2539 if (this_cu->v.quick->file_names != NULL)
2540 return this_cu->v.quick->file_names;
2541 /* If we know there is no line data, no point in looking again. */
2542 if (this_cu->v.quick->no_file_data)
2543 return NULL;
2544
2545 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2546 in the stub for CUs, there's is no need to lookup the DWO file.
2547 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2548 DWO file. */
2549 if (this_cu->is_debug_types)
2550 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2551 else
2552 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2553
2554 if (this_cu->v.quick->no_file_data)
2555 return NULL;
2556 return this_cu->v.quick->file_names;
2557 }
2558
2559 /* A helper for the "quick" functions which computes and caches the
2560 real path for a given file name from the line table. */
2561
2562 static const char *
2563 dw2_get_real_path (struct objfile *objfile,
2564 struct quick_file_names *qfn, int index)
2565 {
2566 if (qfn->real_names == NULL)
2567 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2568 qfn->num_file_names, sizeof (char *));
2569
2570 if (qfn->real_names[index] == NULL)
2571 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2572
2573 return qfn->real_names[index];
2574 }
2575
2576 static struct symtab *
2577 dw2_find_last_source_symtab (struct objfile *objfile)
2578 {
2579 int index;
2580
2581 dw2_setup (objfile);
2582 index = dwarf2_per_objfile->n_comp_units - 1;
2583 return dw2_instantiate_symtab (dw2_get_cu (index));
2584 }
2585
2586 /* Traversal function for dw2_forget_cached_source_info. */
2587
2588 static int
2589 dw2_free_cached_file_names (void **slot, void *info)
2590 {
2591 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2592
2593 if (file_data->real_names)
2594 {
2595 int i;
2596
2597 for (i = 0; i < file_data->num_file_names; ++i)
2598 {
2599 xfree ((void*) file_data->real_names[i]);
2600 file_data->real_names[i] = NULL;
2601 }
2602 }
2603
2604 return 1;
2605 }
2606
2607 static void
2608 dw2_forget_cached_source_info (struct objfile *objfile)
2609 {
2610 dw2_setup (objfile);
2611
2612 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2613 dw2_free_cached_file_names, NULL);
2614 }
2615
2616 /* Helper function for dw2_map_symtabs_matching_filename that expands
2617 the symtabs and calls the iterator. */
2618
2619 static int
2620 dw2_map_expand_apply (struct objfile *objfile,
2621 struct dwarf2_per_cu_data *per_cu,
2622 const char *name,
2623 const char *full_path, const char *real_path,
2624 int (*callback) (struct symtab *, void *),
2625 void *data)
2626 {
2627 struct symtab *last_made = objfile->symtabs;
2628
2629 /* Don't visit already-expanded CUs. */
2630 if (per_cu->v.quick->symtab)
2631 return 0;
2632
2633 /* This may expand more than one symtab, and we want to iterate over
2634 all of them. */
2635 dw2_instantiate_symtab (per_cu);
2636
2637 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2638 objfile->symtabs, last_made);
2639 }
2640
2641 /* Implementation of the map_symtabs_matching_filename method. */
2642
2643 static int
2644 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2645 const char *full_path, const char *real_path,
2646 int (*callback) (struct symtab *, void *),
2647 void *data)
2648 {
2649 int i;
2650 const char *name_basename = lbasename (name);
2651 int name_len = strlen (name);
2652 int is_abs = IS_ABSOLUTE_PATH (name);
2653
2654 dw2_setup (objfile);
2655
2656 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2657 + dwarf2_per_objfile->n_type_units); ++i)
2658 {
2659 int j;
2660 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2661 struct quick_file_names *file_data;
2662
2663 /* We only need to look at symtabs not already expanded. */
2664 if (per_cu->v.quick->symtab)
2665 continue;
2666
2667 file_data = dw2_get_file_names (objfile, per_cu);
2668 if (file_data == NULL)
2669 continue;
2670
2671 for (j = 0; j < file_data->num_file_names; ++j)
2672 {
2673 const char *this_name = file_data->file_names[j];
2674
2675 if (FILENAME_CMP (name, this_name) == 0
2676 || (!is_abs && compare_filenames_for_search (this_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 /* Before we invoke realpath, which can get expensive when many
2686 files are involved, do a quick comparison of the basenames. */
2687 if (! basenames_may_differ
2688 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2689 continue;
2690
2691 if (full_path != NULL)
2692 {
2693 const char *this_real_name = dw2_get_real_path (objfile,
2694 file_data, j);
2695
2696 if (this_real_name != NULL
2697 && (FILENAME_CMP (full_path, this_real_name) == 0
2698 || (!is_abs
2699 && compare_filenames_for_search (this_real_name,
2700 name, name_len))))
2701 {
2702 if (dw2_map_expand_apply (objfile, per_cu,
2703 name, full_path, real_path,
2704 callback, data))
2705 return 1;
2706 }
2707 }
2708
2709 if (real_path != NULL)
2710 {
2711 const char *this_real_name = dw2_get_real_path (objfile,
2712 file_data, j);
2713
2714 if (this_real_name != NULL
2715 && (FILENAME_CMP (real_path, this_real_name) == 0
2716 || (!is_abs
2717 && compare_filenames_for_search (this_real_name,
2718 name, name_len))))
2719 {
2720 if (dw2_map_expand_apply (objfile, per_cu,
2721 name, full_path, real_path,
2722 callback, data))
2723 return 1;
2724 }
2725 }
2726 }
2727 }
2728
2729 return 0;
2730 }
2731
2732 static struct symtab *
2733 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2734 const char *name, domain_enum domain)
2735 {
2736 /* We do all the work in the pre_expand_symtabs_matching hook
2737 instead. */
2738 return NULL;
2739 }
2740
2741 /* A helper function that expands all symtabs that hold an object
2742 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2743 symbols in block BLOCK_KIND. */
2744
2745 static void
2746 dw2_do_expand_symtabs_matching (struct objfile *objfile,
2747 int want_specific_block,
2748 enum block_enum block_kind,
2749 const char *name, domain_enum domain)
2750 {
2751 struct mapped_index *index;
2752
2753 dw2_setup (objfile);
2754
2755 index = dwarf2_per_objfile->index_table;
2756
2757 /* index_table is NULL if OBJF_READNOW. */
2758 if (index)
2759 {
2760 offset_type *vec;
2761
2762 if (find_slot_in_mapped_hash (index, name, &vec))
2763 {
2764 offset_type i, len = MAYBE_SWAP (*vec);
2765 for (i = 0; i < len; ++i)
2766 {
2767 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2768 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2769 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2770 int want_static = block_kind != GLOBAL_BLOCK;
2771 /* This value is only valid for index versions >= 7. */
2772 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2773 gdb_index_symbol_kind symbol_kind =
2774 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2775
2776 if (want_specific_block
2777 && index->version >= 7
2778 && want_static != is_static)
2779 continue;
2780
2781 /* Only check the symbol's kind if it has one.
2782 Indices prior to version 7 don't record it. */
2783 if (index->version >= 7)
2784 {
2785 switch (domain)
2786 {
2787 case VAR_DOMAIN:
2788 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2789 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2790 /* Some types are also in VAR_DOMAIN. */
2791 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2792 continue;
2793 break;
2794 case STRUCT_DOMAIN:
2795 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2796 continue;
2797 break;
2798 case LABEL_DOMAIN:
2799 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2800 continue;
2801 break;
2802 default:
2803 break;
2804 }
2805 }
2806
2807 dw2_instantiate_symtab (per_cu);
2808 }
2809 }
2810 }
2811 }
2812
2813 static void
2814 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2815 enum block_enum block_kind, const char *name,
2816 domain_enum domain)
2817 {
2818 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
2819 }
2820
2821 static void
2822 dw2_print_stats (struct objfile *objfile)
2823 {
2824 int i, count;
2825
2826 dw2_setup (objfile);
2827 count = 0;
2828 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2829 + dwarf2_per_objfile->n_type_units); ++i)
2830 {
2831 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2832
2833 if (!per_cu->v.quick->symtab)
2834 ++count;
2835 }
2836 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2837 }
2838
2839 static void
2840 dw2_dump (struct objfile *objfile)
2841 {
2842 /* Nothing worth printing. */
2843 }
2844
2845 static void
2846 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2847 struct section_offsets *delta)
2848 {
2849 /* There's nothing to relocate here. */
2850 }
2851
2852 static void
2853 dw2_expand_symtabs_for_function (struct objfile *objfile,
2854 const char *func_name)
2855 {
2856 /* Note: It doesn't matter what we pass for block_kind here. */
2857 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2858 VAR_DOMAIN);
2859 }
2860
2861 static void
2862 dw2_expand_all_symtabs (struct objfile *objfile)
2863 {
2864 int i;
2865
2866 dw2_setup (objfile);
2867
2868 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2869 + dwarf2_per_objfile->n_type_units); ++i)
2870 {
2871 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2872
2873 dw2_instantiate_symtab (per_cu);
2874 }
2875 }
2876
2877 static void
2878 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2879 const char *filename)
2880 {
2881 int i;
2882
2883 dw2_setup (objfile);
2884
2885 /* We don't need to consider type units here.
2886 This is only called for examining code, e.g. expand_line_sal.
2887 There can be an order of magnitude (or more) more type units
2888 than comp units, and we avoid them if we can. */
2889
2890 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2891 {
2892 int j;
2893 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2894 struct quick_file_names *file_data;
2895
2896 /* We only need to look at symtabs not already expanded. */
2897 if (per_cu->v.quick->symtab)
2898 continue;
2899
2900 file_data = dw2_get_file_names (objfile, per_cu);
2901 if (file_data == NULL)
2902 continue;
2903
2904 for (j = 0; j < file_data->num_file_names; ++j)
2905 {
2906 const char *this_name = file_data->file_names[j];
2907 if (FILENAME_CMP (this_name, filename) == 0)
2908 {
2909 dw2_instantiate_symtab (per_cu);
2910 break;
2911 }
2912 }
2913 }
2914 }
2915
2916 /* A helper function for dw2_find_symbol_file that finds the primary
2917 file name for a given CU. This is a die_reader_func. */
2918
2919 static void
2920 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2921 gdb_byte *info_ptr,
2922 struct die_info *comp_unit_die,
2923 int has_children,
2924 void *data)
2925 {
2926 const char **result_ptr = data;
2927 struct dwarf2_cu *cu = reader->cu;
2928 struct attribute *attr;
2929
2930 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2931 if (attr == NULL)
2932 *result_ptr = NULL;
2933 else
2934 *result_ptr = DW_STRING (attr);
2935 }
2936
2937 static const char *
2938 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2939 {
2940 struct dwarf2_per_cu_data *per_cu;
2941 offset_type *vec;
2942 struct quick_file_names *file_data;
2943 const char *filename;
2944
2945 dw2_setup (objfile);
2946
2947 /* index_table is NULL if OBJF_READNOW. */
2948 if (!dwarf2_per_objfile->index_table)
2949 {
2950 struct symtab *s;
2951
2952 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2953 {
2954 struct blockvector *bv = BLOCKVECTOR (s);
2955 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2956 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2957
2958 if (sym)
2959 return sym->symtab->filename;
2960 }
2961 return NULL;
2962 }
2963
2964 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2965 name, &vec))
2966 return NULL;
2967
2968 /* Note that this just looks at the very first one named NAME -- but
2969 actually we are looking for a function. find_main_filename
2970 should be rewritten so that it doesn't require a custom hook. It
2971 could just use the ordinary symbol tables. */
2972 /* vec[0] is the length, which must always be >0. */
2973 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
2974
2975 if (per_cu->v.quick->symtab != NULL)
2976 return per_cu->v.quick->symtab->filename;
2977
2978 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
2979 &filename);
2980
2981 return filename;
2982 }
2983
2984 static void
2985 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2986 struct objfile *objfile, int global,
2987 int (*callback) (struct block *,
2988 struct symbol *, void *),
2989 void *data, symbol_compare_ftype *match,
2990 symbol_compare_ftype *ordered_compare)
2991 {
2992 /* Currently unimplemented; used for Ada. The function can be called if the
2993 current language is Ada for a non-Ada objfile using GNU index. As Ada
2994 does not look for non-Ada symbols this function should just return. */
2995 }
2996
2997 static void
2998 dw2_expand_symtabs_matching
2999 (struct objfile *objfile,
3000 int (*file_matcher) (const char *, void *),
3001 int (*name_matcher) (const char *, void *),
3002 enum search_domain kind,
3003 void *data)
3004 {
3005 int i;
3006 offset_type iter;
3007 struct mapped_index *index;
3008
3009 dw2_setup (objfile);
3010
3011 /* index_table is NULL if OBJF_READNOW. */
3012 if (!dwarf2_per_objfile->index_table)
3013 return;
3014 index = dwarf2_per_objfile->index_table;
3015
3016 if (file_matcher != NULL)
3017 {
3018 struct cleanup *cleanup;
3019 htab_t visited_found, visited_not_found;
3020
3021 visited_found = htab_create_alloc (10,
3022 htab_hash_pointer, htab_eq_pointer,
3023 NULL, xcalloc, xfree);
3024 cleanup = make_cleanup_htab_delete (visited_found);
3025 visited_not_found = htab_create_alloc (10,
3026 htab_hash_pointer, htab_eq_pointer,
3027 NULL, xcalloc, xfree);
3028 make_cleanup_htab_delete (visited_not_found);
3029
3030 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3031 + dwarf2_per_objfile->n_type_units); ++i)
3032 {
3033 int j;
3034 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3035 struct quick_file_names *file_data;
3036 void **slot;
3037
3038 per_cu->v.quick->mark = 0;
3039
3040 /* We only need to look at symtabs not already expanded. */
3041 if (per_cu->v.quick->symtab)
3042 continue;
3043
3044 file_data = dw2_get_file_names (objfile, per_cu);
3045 if (file_data == NULL)
3046 continue;
3047
3048 if (htab_find (visited_not_found, file_data) != NULL)
3049 continue;
3050 else if (htab_find (visited_found, file_data) != NULL)
3051 {
3052 per_cu->v.quick->mark = 1;
3053 continue;
3054 }
3055
3056 for (j = 0; j < file_data->num_file_names; ++j)
3057 {
3058 if (file_matcher (file_data->file_names[j], data))
3059 {
3060 per_cu->v.quick->mark = 1;
3061 break;
3062 }
3063 }
3064
3065 slot = htab_find_slot (per_cu->v.quick->mark
3066 ? visited_found
3067 : visited_not_found,
3068 file_data, INSERT);
3069 *slot = file_data;
3070 }
3071
3072 do_cleanups (cleanup);
3073 }
3074
3075 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3076 {
3077 offset_type idx = 2 * iter;
3078 const char *name;
3079 offset_type *vec, vec_len, vec_idx;
3080
3081 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3082 continue;
3083
3084 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3085
3086 if (! (*name_matcher) (name, data))
3087 continue;
3088
3089 /* The name was matched, now expand corresponding CUs that were
3090 marked. */
3091 vec = (offset_type *) (index->constant_pool
3092 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3093 vec_len = MAYBE_SWAP (vec[0]);
3094 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3095 {
3096 struct dwarf2_per_cu_data *per_cu;
3097 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3098 gdb_index_symbol_kind symbol_kind =
3099 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3100 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3101
3102 /* Don't crash on bad data. */
3103 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3104 + dwarf2_per_objfile->n_comp_units))
3105 continue;
3106
3107 /* Only check the symbol's kind if it has one.
3108 Indices prior to version 7 don't record it. */
3109 if (index->version >= 7)
3110 {
3111 switch (kind)
3112 {
3113 case VARIABLES_DOMAIN:
3114 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3115 continue;
3116 break;
3117 case FUNCTIONS_DOMAIN:
3118 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3119 continue;
3120 break;
3121 case TYPES_DOMAIN:
3122 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3123 continue;
3124 break;
3125 default:
3126 break;
3127 }
3128 }
3129
3130 per_cu = dw2_get_cu (cu_index);
3131 if (file_matcher == NULL || per_cu->v.quick->mark)
3132 dw2_instantiate_symtab (per_cu);
3133 }
3134 }
3135 }
3136
3137 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3138 symtab. */
3139
3140 static struct symtab *
3141 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3142 {
3143 int i;
3144
3145 if (BLOCKVECTOR (symtab) != NULL
3146 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3147 return symtab;
3148
3149 if (symtab->includes == NULL)
3150 return NULL;
3151
3152 for (i = 0; symtab->includes[i]; ++i)
3153 {
3154 struct symtab *s = symtab->includes[i];
3155
3156 s = recursively_find_pc_sect_symtab (s, pc);
3157 if (s != NULL)
3158 return s;
3159 }
3160
3161 return NULL;
3162 }
3163
3164 static struct symtab *
3165 dw2_find_pc_sect_symtab (struct objfile *objfile,
3166 struct minimal_symbol *msymbol,
3167 CORE_ADDR pc,
3168 struct obj_section *section,
3169 int warn_if_readin)
3170 {
3171 struct dwarf2_per_cu_data *data;
3172 struct symtab *result;
3173
3174 dw2_setup (objfile);
3175
3176 if (!objfile->psymtabs_addrmap)
3177 return NULL;
3178
3179 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3180 if (!data)
3181 return NULL;
3182
3183 if (warn_if_readin && data->v.quick->symtab)
3184 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3185 paddress (get_objfile_arch (objfile), pc));
3186
3187 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3188 gdb_assert (result != NULL);
3189 return result;
3190 }
3191
3192 static void
3193 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3194 void *data, int need_fullname)
3195 {
3196 int i;
3197 struct cleanup *cleanup;
3198 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3199 NULL, xcalloc, xfree);
3200
3201 cleanup = make_cleanup_htab_delete (visited);
3202 dw2_setup (objfile);
3203
3204 /* We can ignore file names coming from already-expanded CUs. */
3205 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3206 + dwarf2_per_objfile->n_type_units); ++i)
3207 {
3208 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3209
3210 if (per_cu->v.quick->symtab)
3211 {
3212 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3213 INSERT);
3214
3215 *slot = per_cu->v.quick->file_names;
3216 }
3217 }
3218
3219 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3220 + dwarf2_per_objfile->n_type_units); ++i)
3221 {
3222 int j;
3223 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3224 struct quick_file_names *file_data;
3225 void **slot;
3226
3227 /* We only need to look at symtabs not already expanded. */
3228 if (per_cu->v.quick->symtab)
3229 continue;
3230
3231 file_data = dw2_get_file_names (objfile, per_cu);
3232 if (file_data == NULL)
3233 continue;
3234
3235 slot = htab_find_slot (visited, file_data, INSERT);
3236 if (*slot)
3237 {
3238 /* Already visited. */
3239 continue;
3240 }
3241 *slot = file_data;
3242
3243 for (j = 0; j < file_data->num_file_names; ++j)
3244 {
3245 const char *this_real_name;
3246
3247 if (need_fullname)
3248 this_real_name = dw2_get_real_path (objfile, file_data, j);
3249 else
3250 this_real_name = NULL;
3251 (*fun) (file_data->file_names[j], this_real_name, data);
3252 }
3253 }
3254
3255 do_cleanups (cleanup);
3256 }
3257
3258 static int
3259 dw2_has_symbols (struct objfile *objfile)
3260 {
3261 return 1;
3262 }
3263
3264 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3265 {
3266 dw2_has_symbols,
3267 dw2_find_last_source_symtab,
3268 dw2_forget_cached_source_info,
3269 dw2_map_symtabs_matching_filename,
3270 dw2_lookup_symbol,
3271 dw2_pre_expand_symtabs_matching,
3272 dw2_print_stats,
3273 dw2_dump,
3274 dw2_relocate,
3275 dw2_expand_symtabs_for_function,
3276 dw2_expand_all_symtabs,
3277 dw2_expand_symtabs_with_filename,
3278 dw2_find_symbol_file,
3279 dw2_map_matching_symbols,
3280 dw2_expand_symtabs_matching,
3281 dw2_find_pc_sect_symtab,
3282 dw2_map_symbol_filenames
3283 };
3284
3285 /* Initialize for reading DWARF for this objfile. Return 0 if this
3286 file will use psymtabs, or 1 if using the GNU index. */
3287
3288 int
3289 dwarf2_initialize_objfile (struct objfile *objfile)
3290 {
3291 /* If we're about to read full symbols, don't bother with the
3292 indices. In this case we also don't care if some other debug
3293 format is making psymtabs, because they are all about to be
3294 expanded anyway. */
3295 if ((objfile->flags & OBJF_READNOW))
3296 {
3297 int i;
3298
3299 dwarf2_per_objfile->using_index = 1;
3300 create_all_comp_units (objfile);
3301 create_all_type_units (objfile);
3302 dwarf2_per_objfile->quick_file_names_table =
3303 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3304
3305 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3306 + dwarf2_per_objfile->n_type_units); ++i)
3307 {
3308 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3309
3310 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3311 struct dwarf2_per_cu_quick_data);
3312 }
3313
3314 /* Return 1 so that gdb sees the "quick" functions. However,
3315 these functions will be no-ops because we will have expanded
3316 all symtabs. */
3317 return 1;
3318 }
3319
3320 if (dwarf2_read_index (objfile))
3321 return 1;
3322
3323 return 0;
3324 }
3325
3326 \f
3327
3328 /* Build a partial symbol table. */
3329
3330 void
3331 dwarf2_build_psymtabs (struct objfile *objfile)
3332 {
3333 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3334 {
3335 init_psymbol_list (objfile, 1024);
3336 }
3337
3338 dwarf2_build_psymtabs_hard (objfile);
3339 }
3340
3341 /* Return TRUE if OFFSET is within CU_HEADER. */
3342
3343 static inline int
3344 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3345 {
3346 sect_offset bottom = { cu_header->offset.sect_off };
3347 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3348 + cu_header->initial_length_size) };
3349
3350 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3351 }
3352
3353 /* Read in the comp unit header information from the debug_info at info_ptr.
3354 NOTE: This leaves members offset, first_die_offset to be filled in
3355 by the caller. */
3356
3357 static gdb_byte *
3358 read_comp_unit_head (struct comp_unit_head *cu_header,
3359 gdb_byte *info_ptr, bfd *abfd)
3360 {
3361 int signed_addr;
3362 unsigned int bytes_read;
3363
3364 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3365 cu_header->initial_length_size = bytes_read;
3366 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3367 info_ptr += bytes_read;
3368 cu_header->version = read_2_bytes (abfd, info_ptr);
3369 info_ptr += 2;
3370 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3371 &bytes_read);
3372 info_ptr += bytes_read;
3373 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3374 info_ptr += 1;
3375 signed_addr = bfd_get_sign_extend_vma (abfd);
3376 if (signed_addr < 0)
3377 internal_error (__FILE__, __LINE__,
3378 _("read_comp_unit_head: dwarf from non elf file"));
3379 cu_header->signed_addr_p = signed_addr;
3380
3381 return info_ptr;
3382 }
3383
3384 /* Subroutine of read_and_check_comp_unit_head and
3385 read_and_check_type_unit_head to simplify them.
3386 Perform various error checking on the header. */
3387
3388 static void
3389 error_check_comp_unit_head (struct comp_unit_head *header,
3390 struct dwarf2_section_info *section)
3391 {
3392 bfd *abfd = section->asection->owner;
3393 const char *filename = bfd_get_filename (abfd);
3394
3395 if (header->version != 2 && header->version != 3 && header->version != 4)
3396 error (_("Dwarf Error: wrong version in compilation unit header "
3397 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3398 filename);
3399
3400 if (header->abbrev_offset.sect_off
3401 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3402 &dwarf2_per_objfile->abbrev))
3403 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3404 "(offset 0x%lx + 6) [in module %s]"),
3405 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3406 filename);
3407
3408 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3409 avoid potential 32-bit overflow. */
3410 if (((unsigned long) header->offset.sect_off
3411 + header->length + header->initial_length_size)
3412 > section->size)
3413 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3414 "(offset 0x%lx + 0) [in module %s]"),
3415 (long) header->length, (long) header->offset.sect_off,
3416 filename);
3417 }
3418
3419 /* Read in a CU/TU header and perform some basic error checking.
3420 The contents of the header are stored in HEADER.
3421 The result is a pointer to the start of the first DIE. */
3422
3423 static gdb_byte *
3424 read_and_check_comp_unit_head (struct comp_unit_head *header,
3425 struct dwarf2_section_info *section,
3426 gdb_byte *info_ptr,
3427 int is_debug_types_section)
3428 {
3429 gdb_byte *beg_of_comp_unit = info_ptr;
3430 bfd *abfd = section->asection->owner;
3431
3432 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3433
3434 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3435
3436 /* If we're reading a type unit, skip over the signature and
3437 type_offset fields. */
3438 if (is_debug_types_section)
3439 info_ptr += 8 /*signature*/ + header->offset_size;
3440
3441 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3442
3443 error_check_comp_unit_head (header, section);
3444
3445 return info_ptr;
3446 }
3447
3448 /* Read in the types comp unit header information from .debug_types entry at
3449 types_ptr. The result is a pointer to one past the end of the header. */
3450
3451 static gdb_byte *
3452 read_and_check_type_unit_head (struct comp_unit_head *header,
3453 struct dwarf2_section_info *section,
3454 gdb_byte *info_ptr,
3455 ULONGEST *signature,
3456 cu_offset *type_offset_in_tu)
3457 {
3458 gdb_byte *beg_of_comp_unit = info_ptr;
3459 bfd *abfd = section->asection->owner;
3460
3461 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3462
3463 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3464
3465 /* If we're reading a type unit, skip over the signature and
3466 type_offset fields. */
3467 if (signature != NULL)
3468 *signature = read_8_bytes (abfd, info_ptr);
3469 info_ptr += 8;
3470 if (type_offset_in_tu != NULL)
3471 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3472 header->offset_size);
3473 info_ptr += header->offset_size;
3474
3475 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3476
3477 error_check_comp_unit_head (header, section);
3478
3479 return info_ptr;
3480 }
3481
3482 /* Allocate a new partial symtab for file named NAME and mark this new
3483 partial symtab as being an include of PST. */
3484
3485 static void
3486 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3487 struct objfile *objfile)
3488 {
3489 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3490
3491 subpst->section_offsets = pst->section_offsets;
3492 subpst->textlow = 0;
3493 subpst->texthigh = 0;
3494
3495 subpst->dependencies = (struct partial_symtab **)
3496 obstack_alloc (&objfile->objfile_obstack,
3497 sizeof (struct partial_symtab *));
3498 subpst->dependencies[0] = pst;
3499 subpst->number_of_dependencies = 1;
3500
3501 subpst->globals_offset = 0;
3502 subpst->n_global_syms = 0;
3503 subpst->statics_offset = 0;
3504 subpst->n_static_syms = 0;
3505 subpst->symtab = NULL;
3506 subpst->read_symtab = pst->read_symtab;
3507 subpst->readin = 0;
3508
3509 /* No private part is necessary for include psymtabs. This property
3510 can be used to differentiate between such include psymtabs and
3511 the regular ones. */
3512 subpst->read_symtab_private = NULL;
3513 }
3514
3515 /* Read the Line Number Program data and extract the list of files
3516 included by the source file represented by PST. Build an include
3517 partial symtab for each of these included files. */
3518
3519 static void
3520 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3521 struct die_info *die,
3522 struct partial_symtab *pst)
3523 {
3524 struct line_header *lh = NULL;
3525 struct attribute *attr;
3526
3527 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3528 if (attr)
3529 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3530 if (lh == NULL)
3531 return; /* No linetable, so no includes. */
3532
3533 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3534 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3535
3536 free_line_header (lh);
3537 }
3538
3539 static hashval_t
3540 hash_signatured_type (const void *item)
3541 {
3542 const struct signatured_type *sig_type = item;
3543
3544 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3545 return sig_type->signature;
3546 }
3547
3548 static int
3549 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3550 {
3551 const struct signatured_type *lhs = item_lhs;
3552 const struct signatured_type *rhs = item_rhs;
3553
3554 return lhs->signature == rhs->signature;
3555 }
3556
3557 /* Allocate a hash table for signatured types. */
3558
3559 static htab_t
3560 allocate_signatured_type_table (struct objfile *objfile)
3561 {
3562 return htab_create_alloc_ex (41,
3563 hash_signatured_type,
3564 eq_signatured_type,
3565 NULL,
3566 &objfile->objfile_obstack,
3567 hashtab_obstack_allocate,
3568 dummy_obstack_deallocate);
3569 }
3570
3571 /* A helper function to add a signatured type CU to a table. */
3572
3573 static int
3574 add_signatured_type_cu_to_table (void **slot, void *datum)
3575 {
3576 struct signatured_type *sigt = *slot;
3577 struct dwarf2_per_cu_data ***datap = datum;
3578
3579 **datap = &sigt->per_cu;
3580 ++*datap;
3581
3582 return 1;
3583 }
3584
3585 /* Create the hash table of all entries in the .debug_types section.
3586 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3587 The result is a pointer to the hash table or NULL if there are
3588 no types. */
3589
3590 static htab_t
3591 create_debug_types_hash_table (struct dwo_file *dwo_file,
3592 VEC (dwarf2_section_info_def) *types)
3593 {
3594 struct objfile *objfile = dwarf2_per_objfile->objfile;
3595 htab_t types_htab = NULL;
3596 int ix;
3597 struct dwarf2_section_info *section;
3598
3599 if (VEC_empty (dwarf2_section_info_def, types))
3600 return NULL;
3601
3602 for (ix = 0;
3603 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3604 ++ix)
3605 {
3606 bfd *abfd;
3607 gdb_byte *info_ptr, *end_ptr;
3608
3609 dwarf2_read_section (objfile, section);
3610 info_ptr = section->buffer;
3611
3612 if (info_ptr == NULL)
3613 continue;
3614
3615 /* We can't set abfd until now because the section may be empty or
3616 not present, in which case section->asection will be NULL. */
3617 abfd = section->asection->owner;
3618
3619 if (types_htab == NULL)
3620 {
3621 if (dwo_file)
3622 types_htab = allocate_dwo_unit_table (objfile);
3623 else
3624 types_htab = allocate_signatured_type_table (objfile);
3625 }
3626
3627 if (dwarf2_die_debug)
3628 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3629 bfd_get_filename (abfd));
3630
3631 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3632 because we don't need to read any dies: the signature is in the
3633 header. */
3634
3635 end_ptr = info_ptr + section->size;
3636 while (info_ptr < end_ptr)
3637 {
3638 sect_offset offset;
3639 cu_offset type_offset_in_tu;
3640 ULONGEST signature;
3641 struct signatured_type *sig_type;
3642 struct dwo_unit *dwo_tu;
3643 void **slot;
3644 gdb_byte *ptr = info_ptr;
3645 struct comp_unit_head header;
3646 unsigned int length;
3647
3648 offset.sect_off = ptr - section->buffer;
3649
3650 /* We need to read the type's signature in order to build the hash
3651 table, but we don't need anything else just yet. */
3652
3653 ptr = read_and_check_type_unit_head (&header, section, ptr,
3654 &signature, &type_offset_in_tu);
3655
3656 length = header.initial_length_size + header.length;
3657
3658 /* Skip dummy type units. */
3659 if (ptr >= info_ptr + length
3660 || peek_abbrev_code (abfd, ptr) == 0)
3661 {
3662 info_ptr += header.initial_length_size + header.length;
3663 continue;
3664 }
3665
3666 if (dwo_file)
3667 {
3668 sig_type = NULL;
3669 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3670 struct dwo_unit);
3671 dwo_tu->dwo_file = dwo_file;
3672 dwo_tu->signature = signature;
3673 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3674 dwo_tu->info_or_types_section = section;
3675 dwo_tu->offset = offset;
3676 dwo_tu->length = length;
3677 }
3678 else
3679 {
3680 /* N.B.: type_offset is not usable if this type uses a DWO file.
3681 The real type_offset is in the DWO file. */
3682 dwo_tu = NULL;
3683 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3684 struct signatured_type);
3685 sig_type->signature = signature;
3686 sig_type->type_offset_in_tu = type_offset_in_tu;
3687 sig_type->per_cu.objfile = objfile;
3688 sig_type->per_cu.is_debug_types = 1;
3689 sig_type->per_cu.info_or_types_section = section;
3690 sig_type->per_cu.offset = offset;
3691 sig_type->per_cu.length = length;
3692 }
3693
3694 slot = htab_find_slot (types_htab,
3695 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3696 INSERT);
3697 gdb_assert (slot != NULL);
3698 if (*slot != NULL)
3699 {
3700 sect_offset dup_offset;
3701
3702 if (dwo_file)
3703 {
3704 const struct dwo_unit *dup_tu = *slot;
3705
3706 dup_offset = dup_tu->offset;
3707 }
3708 else
3709 {
3710 const struct signatured_type *dup_tu = *slot;
3711
3712 dup_offset = dup_tu->per_cu.offset;
3713 }
3714
3715 complaint (&symfile_complaints,
3716 _("debug type entry at offset 0x%x is duplicate to the "
3717 "entry at offset 0x%x, signature 0x%s"),
3718 offset.sect_off, dup_offset.sect_off,
3719 phex (signature, sizeof (signature)));
3720 }
3721 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3722
3723 if (dwarf2_die_debug)
3724 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3725 offset.sect_off,
3726 phex (signature, sizeof (signature)));
3727
3728 info_ptr += length;
3729 }
3730 }
3731
3732 return types_htab;
3733 }
3734
3735 /* Create the hash table of all entries in the .debug_types section,
3736 and initialize all_type_units.
3737 The result is zero if there is an error (e.g. missing .debug_types section),
3738 otherwise non-zero. */
3739
3740 static int
3741 create_all_type_units (struct objfile *objfile)
3742 {
3743 htab_t types_htab;
3744 struct dwarf2_per_cu_data **iter;
3745
3746 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3747 if (types_htab == NULL)
3748 {
3749 dwarf2_per_objfile->signatured_types = NULL;
3750 return 0;
3751 }
3752
3753 dwarf2_per_objfile->signatured_types = types_htab;
3754
3755 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3756 dwarf2_per_objfile->all_type_units
3757 = obstack_alloc (&objfile->objfile_obstack,
3758 dwarf2_per_objfile->n_type_units
3759 * sizeof (struct dwarf2_per_cu_data *));
3760 iter = &dwarf2_per_objfile->all_type_units[0];
3761 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3762 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3763 == dwarf2_per_objfile->n_type_units);
3764
3765 return 1;
3766 }
3767
3768 /* Lookup a signature based type for DW_FORM_ref_sig8.
3769 Returns NULL if signature SIG is not present in the table. */
3770
3771 static struct signatured_type *
3772 lookup_signatured_type (ULONGEST sig)
3773 {
3774 struct signatured_type find_entry, *entry;
3775
3776 if (dwarf2_per_objfile->signatured_types == NULL)
3777 {
3778 complaint (&symfile_complaints,
3779 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3780 return NULL;
3781 }
3782
3783 find_entry.signature = sig;
3784 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3785 return entry;
3786 }
3787
3788 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3789
3790 static void
3791 init_cu_die_reader (struct die_reader_specs *reader,
3792 struct dwarf2_cu *cu,
3793 struct dwarf2_section_info *section,
3794 struct dwo_file *dwo_file)
3795 {
3796 gdb_assert (section->readin && section->buffer != NULL);
3797 reader->abfd = section->asection->owner;
3798 reader->cu = cu;
3799 reader->dwo_file = dwo_file;
3800 reader->die_section = section;
3801 reader->buffer = section->buffer;
3802 reader->buffer_end = section->buffer + section->size;
3803 }
3804
3805 /* Find the base address of the compilation unit for range lists and
3806 location lists. It will normally be specified by DW_AT_low_pc.
3807 In DWARF-3 draft 4, the base address could be overridden by
3808 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3809 compilation units with discontinuous ranges. */
3810
3811 static void
3812 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3813 {
3814 struct attribute *attr;
3815
3816 cu->base_known = 0;
3817 cu->base_address = 0;
3818
3819 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3820 if (attr)
3821 {
3822 cu->base_address = DW_ADDR (attr);
3823 cu->base_known = 1;
3824 }
3825 else
3826 {
3827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3828 if (attr)
3829 {
3830 cu->base_address = DW_ADDR (attr);
3831 cu->base_known = 1;
3832 }
3833 }
3834 }
3835
3836 /* Initialize a CU (or TU) and read its DIEs.
3837 If the CU defers to a DWO file, read the DWO file as well.
3838
3839 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3840 Otherwise, a new CU is allocated with xmalloc.
3841
3842 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3843 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3844
3845 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3846 linker) then DIE_READER_FUNC will not get called. */
3847
3848 static void
3849 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3850 int use_existing_cu, int keep,
3851 die_reader_func_ftype *die_reader_func,
3852 void *data)
3853 {
3854 struct objfile *objfile = dwarf2_per_objfile->objfile;
3855 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3856 bfd *abfd = section->asection->owner;
3857 struct dwarf2_cu *cu;
3858 gdb_byte *begin_info_ptr, *info_ptr;
3859 struct die_reader_specs reader;
3860 struct die_info *comp_unit_die;
3861 int has_children;
3862 struct attribute *attr;
3863 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3864 struct signatured_type *sig_type = NULL;
3865
3866 if (use_existing_cu)
3867 gdb_assert (keep);
3868
3869 cleanups = make_cleanup (null_cleanup, NULL);
3870
3871 /* This is cheap if the section is already read in. */
3872 dwarf2_read_section (objfile, section);
3873
3874 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3875
3876 if (use_existing_cu && this_cu->cu != NULL)
3877 {
3878 cu = this_cu->cu;
3879 info_ptr += cu->header.first_die_offset.cu_off;
3880 }
3881 else
3882 {
3883 /* If !use_existing_cu, this_cu->cu must be NULL. */
3884 gdb_assert (this_cu->cu == NULL);
3885
3886 cu = xmalloc (sizeof (*cu));
3887 init_one_comp_unit (cu, this_cu);
3888
3889 /* If an error occurs while loading, release our storage. */
3890 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3891
3892 if (this_cu->is_debug_types)
3893 {
3894 ULONGEST signature;
3895
3896 info_ptr = read_and_check_type_unit_head (&cu->header,
3897 section, info_ptr,
3898 &signature, NULL);
3899
3900 /* There's no way to get from PER_CU to its containing
3901 struct signatured_type.
3902 But we have the signature so we can use that. */
3903 sig_type = lookup_signatured_type (signature);
3904 /* We've already scanned all the signatured types,
3905 this must succeed. */
3906 gdb_assert (sig_type != NULL);
3907 gdb_assert (&sig_type->per_cu == this_cu);
3908 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3909
3910 /* LENGTH has not been set yet for type units. */
3911 this_cu->length = cu->header.length + cu->header.initial_length_size;
3912
3913 /* Establish the type offset that can be used to lookup the type. */
3914 sig_type->type_offset_in_section.sect_off =
3915 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3916 }
3917 else
3918 {
3919 info_ptr = read_and_check_comp_unit_head (&cu->header,
3920 section, info_ptr, 0);
3921
3922 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3923 gdb_assert (this_cu->length
3924 == cu->header.length + cu->header.initial_length_size);
3925 }
3926 }
3927
3928 /* Skip dummy compilation units. */
3929 if (info_ptr >= begin_info_ptr + this_cu->length
3930 || peek_abbrev_code (abfd, info_ptr) == 0)
3931 {
3932 do_cleanups (cleanups);
3933 return;
3934 }
3935
3936 /* Read the abbrevs for this compilation unit into a table. */
3937 if (cu->dwarf2_abbrevs == NULL)
3938 {
3939 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3940 make_cleanup (dwarf2_free_abbrev_table, cu);
3941 }
3942
3943 /* Read the top level CU/TU die. */
3944 init_cu_die_reader (&reader, cu, section, NULL);
3945 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3946
3947 /* If we have a DWO stub, process it and then read in the DWO file.
3948 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3949 a DWO CU, that this test will fail. */
3950 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3951 if (attr)
3952 {
3953 char *dwo_name = DW_STRING (attr);
3954 const char *comp_dir;
3955 struct dwo_unit *dwo_unit;
3956 ULONGEST signature; /* Or dwo_id. */
3957 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3958 int i,num_extra_attrs;
3959
3960 if (has_children)
3961 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3962 " has children (offset 0x%x) [in module %s]"),
3963 this_cu->offset.sect_off, bfd_get_filename (abfd));
3964
3965 /* These attributes aren't processed until later:
3966 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3967 However, the attribute is found in the stub which we won't have later.
3968 In order to not impose this complication on the rest of the code,
3969 we read them here and copy them to the DWO CU/TU die. */
3970 stmt_list = low_pc = high_pc = ranges = NULL;
3971
3972 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3973 DWO file. */
3974 if (! this_cu->is_debug_types)
3975 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3976 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3977 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3978 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3979
3980 /* There should be a DW_AT_addr_base attribute here (if needed).
3981 We need the value before we can process DW_FORM_GNU_addr_index. */
3982 cu->addr_base = 0;
3983 cu->have_addr_base = 0;
3984 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3985 if (attr)
3986 {
3987 cu->addr_base = DW_UNSND (attr);
3988 cu->have_addr_base = 1;
3989 }
3990
3991 if (this_cu->is_debug_types)
3992 {
3993 gdb_assert (sig_type != NULL);
3994 signature = sig_type->signature;
3995 }
3996 else
3997 {
3998 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3999 if (! attr)
4000 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4001 dwo_name);
4002 signature = DW_UNSND (attr);
4003 }
4004
4005 /* We may need the comp_dir in order to find the DWO file. */
4006 comp_dir = NULL;
4007 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4008 if (attr)
4009 comp_dir = DW_STRING (attr);
4010
4011 if (this_cu->is_debug_types)
4012 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4013 else
4014 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4015 signature);
4016
4017 if (dwo_unit == NULL)
4018 {
4019 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4020 " with ID %s [in module %s]"),
4021 this_cu->offset.sect_off,
4022 phex (signature, sizeof (signature)),
4023 objfile->name);
4024 }
4025
4026 /* Set up for reading the DWO CU/TU. */
4027 cu->dwo_unit = dwo_unit;
4028 section = dwo_unit->info_or_types_section;
4029 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4030 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4031
4032 if (this_cu->is_debug_types)
4033 {
4034 ULONGEST signature;
4035
4036 info_ptr = read_and_check_type_unit_head (&cu->header,
4037 section, info_ptr,
4038 &signature, NULL);
4039 gdb_assert (sig_type->signature == signature);
4040 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4041 gdb_assert (dwo_unit->length
4042 == cu->header.length + cu->header.initial_length_size);
4043
4044 /* Establish the type offset that can be used to lookup the type.
4045 For DWO files, we don't know it until now. */
4046 sig_type->type_offset_in_section.sect_off =
4047 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4048 }
4049 else
4050 {
4051 info_ptr = read_and_check_comp_unit_head (&cu->header,
4052 section, info_ptr, 0);
4053 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4054 gdb_assert (dwo_unit->length
4055 == cu->header.length + cu->header.initial_length_size);
4056 }
4057
4058 /* Discard the original CU's abbrev table, and read the DWO's. */
4059 dwarf2_free_abbrev_table (cu);
4060 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
4061
4062 /* Read in the die, but leave space to copy over the attributes
4063 from the stub. This has the benefit of simplifying the rest of
4064 the code - all the real work is done here. */
4065 num_extra_attrs = ((stmt_list != NULL)
4066 + (low_pc != NULL)
4067 + (high_pc != NULL)
4068 + (ranges != NULL));
4069 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4070 &has_children, num_extra_attrs);
4071
4072 /* Copy over the attributes from the stub to the DWO die. */
4073 i = comp_unit_die->num_attrs;
4074 if (stmt_list != NULL)
4075 comp_unit_die->attrs[i++] = *stmt_list;
4076 if (low_pc != NULL)
4077 comp_unit_die->attrs[i++] = *low_pc;
4078 if (high_pc != NULL)
4079 comp_unit_die->attrs[i++] = *high_pc;
4080 if (ranges != NULL)
4081 comp_unit_die->attrs[i++] = *ranges;
4082 comp_unit_die->num_attrs += num_extra_attrs;
4083
4084 /* Skip dummy compilation units. */
4085 if (info_ptr >= begin_info_ptr + dwo_unit->length
4086 || peek_abbrev_code (abfd, info_ptr) == 0)
4087 {
4088 do_cleanups (cleanups);
4089 return;
4090 }
4091 }
4092
4093 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4094
4095 if (free_cu_cleanup != NULL)
4096 {
4097 if (keep)
4098 {
4099 /* We've successfully allocated this compilation unit. Let our
4100 caller clean it up when finished with it. */
4101 discard_cleanups (free_cu_cleanup);
4102
4103 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4104 So we have to manually free the abbrev table. */
4105 dwarf2_free_abbrev_table (cu);
4106
4107 /* Link this CU into read_in_chain. */
4108 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4109 dwarf2_per_objfile->read_in_chain = this_cu;
4110 }
4111 else
4112 do_cleanups (free_cu_cleanup);
4113 }
4114
4115 do_cleanups (cleanups);
4116 }
4117
4118 /* Read CU/TU THIS_CU in section SECTION,
4119 but do not follow DW_AT_GNU_dwo_name if present.
4120 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4121 have already done the lookup to find the DWO file).
4122
4123 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4124 THIS_CU->is_debug_types, but nothing else.
4125
4126 We fill in THIS_CU->length.
4127
4128 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4129 linker) then DIE_READER_FUNC will not get called.
4130
4131 THIS_CU->cu is always freed when done.
4132 This is done in order to not leave THIS_CU->cu in a state where we have
4133 to care whether it refers to the "main" CU or the DWO CU. */
4134
4135 static void
4136 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4137 struct dwarf2_section_info *abbrev_section,
4138 struct dwo_file *dwo_file,
4139 die_reader_func_ftype *die_reader_func,
4140 void *data)
4141 {
4142 struct objfile *objfile = dwarf2_per_objfile->objfile;
4143 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4144 bfd *abfd = section->asection->owner;
4145 struct dwarf2_cu cu;
4146 gdb_byte *begin_info_ptr, *info_ptr;
4147 struct die_reader_specs reader;
4148 struct cleanup *cleanups;
4149 struct die_info *comp_unit_die;
4150 int has_children;
4151
4152 gdb_assert (this_cu->cu == NULL);
4153
4154 /* This is cheap if the section is already read in. */
4155 dwarf2_read_section (objfile, section);
4156
4157 init_one_comp_unit (&cu, this_cu);
4158
4159 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4160
4161 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4162 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
4163 this_cu->is_debug_types);
4164
4165 this_cu->length = cu.header.length + cu.header.initial_length_size;
4166
4167 /* Skip dummy compilation units. */
4168 if (info_ptr >= begin_info_ptr + this_cu->length
4169 || peek_abbrev_code (abfd, info_ptr) == 0)
4170 {
4171 do_cleanups (cleanups);
4172 return;
4173 }
4174
4175 dwarf2_read_abbrevs (&cu, abbrev_section);
4176 make_cleanup (dwarf2_free_abbrev_table, &cu);
4177
4178 init_cu_die_reader (&reader, &cu, section, dwo_file);
4179 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4180
4181 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4182
4183 do_cleanups (cleanups);
4184 }
4185
4186 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4187 does not lookup the specified DWO file.
4188 This cannot be used to read DWO files.
4189
4190 THIS_CU->cu is always freed when done.
4191 This is done in order to not leave THIS_CU->cu in a state where we have
4192 to care whether it refers to the "main" CU or the DWO CU.
4193 We can revisit this if the data shows there's a performance issue. */
4194
4195 static void
4196 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4197 die_reader_func_ftype *die_reader_func,
4198 void *data)
4199 {
4200 init_cutu_and_read_dies_no_follow (this_cu,
4201 &dwarf2_per_objfile->abbrev,
4202 NULL,
4203 die_reader_func, data);
4204 }
4205
4206 /* die_reader_func for process_psymtab_comp_unit. */
4207
4208 static void
4209 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4210 gdb_byte *info_ptr,
4211 struct die_info *comp_unit_die,
4212 int has_children,
4213 void *data)
4214 {
4215 struct dwarf2_cu *cu = reader->cu;
4216 struct objfile *objfile = cu->objfile;
4217 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4218 struct attribute *attr;
4219 CORE_ADDR baseaddr;
4220 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4221 struct partial_symtab *pst;
4222 int has_pc_info;
4223 const char *filename;
4224 int *want_partial_unit_ptr = data;
4225
4226 if (comp_unit_die->tag == DW_TAG_partial_unit
4227 && (want_partial_unit_ptr == NULL
4228 || !*want_partial_unit_ptr))
4229 return;
4230
4231 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4232
4233 cu->list_in_scope = &file_symbols;
4234
4235 /* Allocate a new partial symbol table structure. */
4236 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4237 if (attr == NULL || !DW_STRING (attr))
4238 filename = "";
4239 else
4240 filename = DW_STRING (attr);
4241 pst = start_psymtab_common (objfile, objfile->section_offsets,
4242 filename,
4243 /* TEXTLOW and TEXTHIGH are set below. */
4244 0,
4245 objfile->global_psymbols.next,
4246 objfile->static_psymbols.next);
4247 pst->psymtabs_addrmap_supported = 1;
4248
4249 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4250 if (attr != NULL)
4251 pst->dirname = DW_STRING (attr);
4252
4253 pst->read_symtab_private = per_cu;
4254
4255 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4256
4257 /* Store the function that reads in the rest of the symbol table. */
4258 pst->read_symtab = dwarf2_psymtab_to_symtab;
4259
4260 per_cu->v.psymtab = pst;
4261
4262 dwarf2_find_base_address (comp_unit_die, cu);
4263
4264 /* Possibly set the default values of LOWPC and HIGHPC from
4265 `DW_AT_ranges'. */
4266 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4267 &best_highpc, cu, pst);
4268 if (has_pc_info == 1 && best_lowpc < best_highpc)
4269 /* Store the contiguous range if it is not empty; it can be empty for
4270 CUs with no code. */
4271 addrmap_set_empty (objfile->psymtabs_addrmap,
4272 best_lowpc + baseaddr,
4273 best_highpc + baseaddr - 1, pst);
4274
4275 /* Check if comp unit has_children.
4276 If so, read the rest of the partial symbols from this comp unit.
4277 If not, there's no more debug_info for this comp unit. */
4278 if (has_children)
4279 {
4280 struct partial_die_info *first_die;
4281 CORE_ADDR lowpc, highpc;
4282
4283 lowpc = ((CORE_ADDR) -1);
4284 highpc = ((CORE_ADDR) 0);
4285
4286 first_die = load_partial_dies (reader, info_ptr, 1);
4287
4288 scan_partial_symbols (first_die, &lowpc, &highpc,
4289 ! has_pc_info, cu);
4290
4291 /* If we didn't find a lowpc, set it to highpc to avoid
4292 complaints from `maint check'. */
4293 if (lowpc == ((CORE_ADDR) -1))
4294 lowpc = highpc;
4295
4296 /* If the compilation unit didn't have an explicit address range,
4297 then use the information extracted from its child dies. */
4298 if (! has_pc_info)
4299 {
4300 best_lowpc = lowpc;
4301 best_highpc = highpc;
4302 }
4303 }
4304 pst->textlow = best_lowpc + baseaddr;
4305 pst->texthigh = best_highpc + baseaddr;
4306
4307 pst->n_global_syms = objfile->global_psymbols.next -
4308 (objfile->global_psymbols.list + pst->globals_offset);
4309 pst->n_static_syms = objfile->static_psymbols.next -
4310 (objfile->static_psymbols.list + pst->statics_offset);
4311 sort_pst_symbols (pst);
4312
4313 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4314 {
4315 int i;
4316 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4317 struct dwarf2_per_cu_data *iter;
4318
4319 /* Fill in 'dependencies' here; we fill in 'users' in a
4320 post-pass. */
4321 pst->number_of_dependencies = len;
4322 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4323 len * sizeof (struct symtab *));
4324 for (i = 0;
4325 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4326 i, iter);
4327 ++i)
4328 pst->dependencies[i] = iter->v.psymtab;
4329
4330 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4331 }
4332
4333 if (per_cu->is_debug_types)
4334 {
4335 /* It's not clear we want to do anything with stmt lists here.
4336 Waiting to see what gcc ultimately does. */
4337 }
4338 else
4339 {
4340 /* Get the list of files included in the current compilation unit,
4341 and build a psymtab for each of them. */
4342 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4343 }
4344 }
4345
4346 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4347 Process compilation unit THIS_CU for a psymtab. */
4348
4349 static void
4350 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4351 int want_partial_unit)
4352 {
4353 /* If this compilation unit was already read in, free the
4354 cached copy in order to read it in again. This is
4355 necessary because we skipped some symbols when we first
4356 read in the compilation unit (see load_partial_dies).
4357 This problem could be avoided, but the benefit is unclear. */
4358 if (this_cu->cu != NULL)
4359 free_one_cached_comp_unit (this_cu);
4360
4361 gdb_assert (! this_cu->is_debug_types);
4362 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4363 &want_partial_unit);
4364
4365 /* Age out any secondary CUs. */
4366 age_cached_comp_units ();
4367 }
4368
4369 /* Traversal function for htab_traverse_noresize.
4370 Process one .debug_types comp-unit. */
4371
4372 static int
4373 process_psymtab_type_unit (void **slot, void *info)
4374 {
4375 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4376 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4377
4378 gdb_assert (per_cu->is_debug_types);
4379 gdb_assert (info == NULL);
4380
4381 /* If this compilation unit was already read in, free the
4382 cached copy in order to read it in again. This is
4383 necessary because we skipped some symbols when we first
4384 read in the compilation unit (see load_partial_dies).
4385 This problem could be avoided, but the benefit is unclear. */
4386 if (per_cu->cu != NULL)
4387 free_one_cached_comp_unit (per_cu);
4388
4389 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4390 NULL);
4391
4392 /* Age out any secondary CUs. */
4393 age_cached_comp_units ();
4394
4395 return 1;
4396 }
4397
4398 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4399 Build partial symbol tables for the .debug_types comp-units. */
4400
4401 static void
4402 build_type_psymtabs (struct objfile *objfile)
4403 {
4404 if (! create_all_type_units (objfile))
4405 return;
4406
4407 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4408 process_psymtab_type_unit, NULL);
4409 }
4410
4411 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4412
4413 static void
4414 psymtabs_addrmap_cleanup (void *o)
4415 {
4416 struct objfile *objfile = o;
4417
4418 objfile->psymtabs_addrmap = NULL;
4419 }
4420
4421 /* Compute the 'user' field for each psymtab in OBJFILE. */
4422
4423 static void
4424 set_partial_user (struct objfile *objfile)
4425 {
4426 int i;
4427
4428 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4429 {
4430 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4431 struct partial_symtab *pst = per_cu->v.psymtab;
4432 int j;
4433
4434 for (j = 0; j < pst->number_of_dependencies; ++j)
4435 {
4436 /* Set the 'user' field only if it is not already set. */
4437 if (pst->dependencies[j]->user == NULL)
4438 pst->dependencies[j]->user = pst;
4439 }
4440 }
4441 }
4442
4443 /* Build the partial symbol table by doing a quick pass through the
4444 .debug_info and .debug_abbrev sections. */
4445
4446 static void
4447 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4448 {
4449 struct cleanup *back_to, *addrmap_cleanup;
4450 struct obstack temp_obstack;
4451 int i;
4452
4453 dwarf2_per_objfile->reading_partial_symbols = 1;
4454
4455 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4456
4457 /* Any cached compilation units will be linked by the per-objfile
4458 read_in_chain. Make sure to free them when we're done. */
4459 back_to = make_cleanup (free_cached_comp_units, NULL);
4460
4461 build_type_psymtabs (objfile);
4462
4463 create_all_comp_units (objfile);
4464
4465 /* Create a temporary address map on a temporary obstack. We later
4466 copy this to the final obstack. */
4467 obstack_init (&temp_obstack);
4468 make_cleanup_obstack_free (&temp_obstack);
4469 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4470 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4471
4472 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4473 {
4474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4475
4476 process_psymtab_comp_unit (per_cu, 0);
4477 }
4478
4479 set_partial_user (objfile);
4480
4481 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4482 &objfile->objfile_obstack);
4483 discard_cleanups (addrmap_cleanup);
4484
4485 do_cleanups (back_to);
4486 }
4487
4488 /* die_reader_func for load_partial_comp_unit. */
4489
4490 static void
4491 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4492 gdb_byte *info_ptr,
4493 struct die_info *comp_unit_die,
4494 int has_children,
4495 void *data)
4496 {
4497 struct dwarf2_cu *cu = reader->cu;
4498
4499 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4500
4501 /* Check if comp unit has_children.
4502 If so, read the rest of the partial symbols from this comp unit.
4503 If not, there's no more debug_info for this comp unit. */
4504 if (has_children)
4505 load_partial_dies (reader, info_ptr, 0);
4506 }
4507
4508 /* Load the partial DIEs for a secondary CU into memory.
4509 This is also used when rereading a primary CU with load_all_dies. */
4510
4511 static void
4512 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4513 {
4514 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4515 }
4516
4517 /* Create a list of all compilation units in OBJFILE.
4518 This is only done for -readnow and building partial symtabs. */
4519
4520 static void
4521 create_all_comp_units (struct objfile *objfile)
4522 {
4523 int n_allocated;
4524 int n_comp_units;
4525 struct dwarf2_per_cu_data **all_comp_units;
4526 gdb_byte *info_ptr;
4527
4528 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4529 info_ptr = dwarf2_per_objfile->info.buffer;
4530
4531 n_comp_units = 0;
4532 n_allocated = 10;
4533 all_comp_units = xmalloc (n_allocated
4534 * sizeof (struct dwarf2_per_cu_data *));
4535
4536 while (info_ptr < dwarf2_per_objfile->info.buffer
4537 + dwarf2_per_objfile->info.size)
4538 {
4539 unsigned int length, initial_length_size;
4540 struct dwarf2_per_cu_data *this_cu;
4541 sect_offset offset;
4542
4543 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4544
4545 /* Read just enough information to find out where the next
4546 compilation unit is. */
4547 length = read_initial_length (objfile->obfd, info_ptr,
4548 &initial_length_size);
4549
4550 /* Save the compilation unit for later lookup. */
4551 this_cu = obstack_alloc (&objfile->objfile_obstack,
4552 sizeof (struct dwarf2_per_cu_data));
4553 memset (this_cu, 0, sizeof (*this_cu));
4554 this_cu->offset = offset;
4555 this_cu->length = length + initial_length_size;
4556 this_cu->objfile = objfile;
4557 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4558
4559 if (n_comp_units == n_allocated)
4560 {
4561 n_allocated *= 2;
4562 all_comp_units = xrealloc (all_comp_units,
4563 n_allocated
4564 * sizeof (struct dwarf2_per_cu_data *));
4565 }
4566 all_comp_units[n_comp_units++] = this_cu;
4567
4568 info_ptr = info_ptr + this_cu->length;
4569 }
4570
4571 dwarf2_per_objfile->all_comp_units
4572 = obstack_alloc (&objfile->objfile_obstack,
4573 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4574 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4575 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4576 xfree (all_comp_units);
4577 dwarf2_per_objfile->n_comp_units = n_comp_units;
4578 }
4579
4580 /* Process all loaded DIEs for compilation unit CU, starting at
4581 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4582 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4583 DW_AT_ranges). If NEED_PC is set, then this function will set
4584 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4585 and record the covered ranges in the addrmap. */
4586
4587 static void
4588 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4589 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4590 {
4591 struct partial_die_info *pdi;
4592
4593 /* Now, march along the PDI's, descending into ones which have
4594 interesting children but skipping the children of the other ones,
4595 until we reach the end of the compilation unit. */
4596
4597 pdi = first_die;
4598
4599 while (pdi != NULL)
4600 {
4601 fixup_partial_die (pdi, cu);
4602
4603 /* Anonymous namespaces or modules have no name but have interesting
4604 children, so we need to look at them. Ditto for anonymous
4605 enums. */
4606
4607 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4608 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4609 || pdi->tag == DW_TAG_imported_unit)
4610 {
4611 switch (pdi->tag)
4612 {
4613 case DW_TAG_subprogram:
4614 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4615 break;
4616 case DW_TAG_constant:
4617 case DW_TAG_variable:
4618 case DW_TAG_typedef:
4619 case DW_TAG_union_type:
4620 if (!pdi->is_declaration)
4621 {
4622 add_partial_symbol (pdi, cu);
4623 }
4624 break;
4625 case DW_TAG_class_type:
4626 case DW_TAG_interface_type:
4627 case DW_TAG_structure_type:
4628 if (!pdi->is_declaration)
4629 {
4630 add_partial_symbol (pdi, cu);
4631 }
4632 break;
4633 case DW_TAG_enumeration_type:
4634 if (!pdi->is_declaration)
4635 add_partial_enumeration (pdi, cu);
4636 break;
4637 case DW_TAG_base_type:
4638 case DW_TAG_subrange_type:
4639 /* File scope base type definitions are added to the partial
4640 symbol table. */
4641 add_partial_symbol (pdi, cu);
4642 break;
4643 case DW_TAG_namespace:
4644 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4645 break;
4646 case DW_TAG_module:
4647 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4648 break;
4649 case DW_TAG_imported_unit:
4650 {
4651 struct dwarf2_per_cu_data *per_cu;
4652
4653 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4654 cu->objfile);
4655
4656 /* Go read the partial unit, if needed. */
4657 if (per_cu->v.psymtab == NULL)
4658 process_psymtab_comp_unit (per_cu, 1);
4659
4660 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4661 per_cu);
4662 }
4663 break;
4664 default:
4665 break;
4666 }
4667 }
4668
4669 /* If the die has a sibling, skip to the sibling. */
4670
4671 pdi = pdi->die_sibling;
4672 }
4673 }
4674
4675 /* Functions used to compute the fully scoped name of a partial DIE.
4676
4677 Normally, this is simple. For C++, the parent DIE's fully scoped
4678 name is concatenated with "::" and the partial DIE's name. For
4679 Java, the same thing occurs except that "." is used instead of "::".
4680 Enumerators are an exception; they use the scope of their parent
4681 enumeration type, i.e. the name of the enumeration type is not
4682 prepended to the enumerator.
4683
4684 There are two complexities. One is DW_AT_specification; in this
4685 case "parent" means the parent of the target of the specification,
4686 instead of the direct parent of the DIE. The other is compilers
4687 which do not emit DW_TAG_namespace; in this case we try to guess
4688 the fully qualified name of structure types from their members'
4689 linkage names. This must be done using the DIE's children rather
4690 than the children of any DW_AT_specification target. We only need
4691 to do this for structures at the top level, i.e. if the target of
4692 any DW_AT_specification (if any; otherwise the DIE itself) does not
4693 have a parent. */
4694
4695 /* Compute the scope prefix associated with PDI's parent, in
4696 compilation unit CU. The result will be allocated on CU's
4697 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4698 field. NULL is returned if no prefix is necessary. */
4699 static char *
4700 partial_die_parent_scope (struct partial_die_info *pdi,
4701 struct dwarf2_cu *cu)
4702 {
4703 char *grandparent_scope;
4704 struct partial_die_info *parent, *real_pdi;
4705
4706 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4707 then this means the parent of the specification DIE. */
4708
4709 real_pdi = pdi;
4710 while (real_pdi->has_specification)
4711 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4712
4713 parent = real_pdi->die_parent;
4714 if (parent == NULL)
4715 return NULL;
4716
4717 if (parent->scope_set)
4718 return parent->scope;
4719
4720 fixup_partial_die (parent, cu);
4721
4722 grandparent_scope = partial_die_parent_scope (parent, cu);
4723
4724 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4725 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4726 Work around this problem here. */
4727 if (cu->language == language_cplus
4728 && parent->tag == DW_TAG_namespace
4729 && strcmp (parent->name, "::") == 0
4730 && grandparent_scope == NULL)
4731 {
4732 parent->scope = NULL;
4733 parent->scope_set = 1;
4734 return NULL;
4735 }
4736
4737 if (pdi->tag == DW_TAG_enumerator)
4738 /* Enumerators should not get the name of the enumeration as a prefix. */
4739 parent->scope = grandparent_scope;
4740 else if (parent->tag == DW_TAG_namespace
4741 || parent->tag == DW_TAG_module
4742 || parent->tag == DW_TAG_structure_type
4743 || parent->tag == DW_TAG_class_type
4744 || parent->tag == DW_TAG_interface_type
4745 || parent->tag == DW_TAG_union_type
4746 || parent->tag == DW_TAG_enumeration_type)
4747 {
4748 if (grandparent_scope == NULL)
4749 parent->scope = parent->name;
4750 else
4751 parent->scope = typename_concat (&cu->comp_unit_obstack,
4752 grandparent_scope,
4753 parent->name, 0, cu);
4754 }
4755 else
4756 {
4757 /* FIXME drow/2004-04-01: What should we be doing with
4758 function-local names? For partial symbols, we should probably be
4759 ignoring them. */
4760 complaint (&symfile_complaints,
4761 _("unhandled containing DIE tag %d for DIE at %d"),
4762 parent->tag, pdi->offset.sect_off);
4763 parent->scope = grandparent_scope;
4764 }
4765
4766 parent->scope_set = 1;
4767 return parent->scope;
4768 }
4769
4770 /* Return the fully scoped name associated with PDI, from compilation unit
4771 CU. The result will be allocated with malloc. */
4772
4773 static char *
4774 partial_die_full_name (struct partial_die_info *pdi,
4775 struct dwarf2_cu *cu)
4776 {
4777 char *parent_scope;
4778
4779 /* If this is a template instantiation, we can not work out the
4780 template arguments from partial DIEs. So, unfortunately, we have
4781 to go through the full DIEs. At least any work we do building
4782 types here will be reused if full symbols are loaded later. */
4783 if (pdi->has_template_arguments)
4784 {
4785 fixup_partial_die (pdi, cu);
4786
4787 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4788 {
4789 struct die_info *die;
4790 struct attribute attr;
4791 struct dwarf2_cu *ref_cu = cu;
4792
4793 /* DW_FORM_ref_addr is using section offset. */
4794 attr.name = 0;
4795 attr.form = DW_FORM_ref_addr;
4796 attr.u.unsnd = pdi->offset.sect_off;
4797 die = follow_die_ref (NULL, &attr, &ref_cu);
4798
4799 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4800 }
4801 }
4802
4803 parent_scope = partial_die_parent_scope (pdi, cu);
4804 if (parent_scope == NULL)
4805 return NULL;
4806 else
4807 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4808 }
4809
4810 static void
4811 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4812 {
4813 struct objfile *objfile = cu->objfile;
4814 CORE_ADDR addr = 0;
4815 char *actual_name = NULL;
4816 CORE_ADDR baseaddr;
4817 int built_actual_name = 0;
4818
4819 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4820
4821 actual_name = partial_die_full_name (pdi, cu);
4822 if (actual_name)
4823 built_actual_name = 1;
4824
4825 if (actual_name == NULL)
4826 actual_name = pdi->name;
4827
4828 switch (pdi->tag)
4829 {
4830 case DW_TAG_subprogram:
4831 if (pdi->is_external || cu->language == language_ada)
4832 {
4833 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4834 of the global scope. But in Ada, we want to be able to access
4835 nested procedures globally. So all Ada subprograms are stored
4836 in the global scope. */
4837 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4838 mst_text, objfile); */
4839 add_psymbol_to_list (actual_name, strlen (actual_name),
4840 built_actual_name,
4841 VAR_DOMAIN, LOC_BLOCK,
4842 &objfile->global_psymbols,
4843 0, pdi->lowpc + baseaddr,
4844 cu->language, objfile);
4845 }
4846 else
4847 {
4848 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4849 mst_file_text, objfile); */
4850 add_psymbol_to_list (actual_name, strlen (actual_name),
4851 built_actual_name,
4852 VAR_DOMAIN, LOC_BLOCK,
4853 &objfile->static_psymbols,
4854 0, pdi->lowpc + baseaddr,
4855 cu->language, objfile);
4856 }
4857 break;
4858 case DW_TAG_constant:
4859 {
4860 struct psymbol_allocation_list *list;
4861
4862 if (pdi->is_external)
4863 list = &objfile->global_psymbols;
4864 else
4865 list = &objfile->static_psymbols;
4866 add_psymbol_to_list (actual_name, strlen (actual_name),
4867 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4868 list, 0, 0, cu->language, objfile);
4869 }
4870 break;
4871 case DW_TAG_variable:
4872 if (pdi->d.locdesc)
4873 addr = decode_locdesc (pdi->d.locdesc, cu);
4874
4875 if (pdi->d.locdesc
4876 && addr == 0
4877 && !dwarf2_per_objfile->has_section_at_zero)
4878 {
4879 /* A global or static variable may also have been stripped
4880 out by the linker if unused, in which case its address
4881 will be nullified; do not add such variables into partial
4882 symbol table then. */
4883 }
4884 else if (pdi->is_external)
4885 {
4886 /* Global Variable.
4887 Don't enter into the minimal symbol tables as there is
4888 a minimal symbol table entry from the ELF symbols already.
4889 Enter into partial symbol table if it has a location
4890 descriptor or a type.
4891 If the location descriptor is missing, new_symbol will create
4892 a LOC_UNRESOLVED symbol, the address of the variable will then
4893 be determined from the minimal symbol table whenever the variable
4894 is referenced.
4895 The address for the partial symbol table entry is not
4896 used by GDB, but it comes in handy for debugging partial symbol
4897 table building. */
4898
4899 if (pdi->d.locdesc || pdi->has_type)
4900 add_psymbol_to_list (actual_name, strlen (actual_name),
4901 built_actual_name,
4902 VAR_DOMAIN, LOC_STATIC,
4903 &objfile->global_psymbols,
4904 0, addr + baseaddr,
4905 cu->language, objfile);
4906 }
4907 else
4908 {
4909 /* Static Variable. Skip symbols without location descriptors. */
4910 if (pdi->d.locdesc == NULL)
4911 {
4912 if (built_actual_name)
4913 xfree (actual_name);
4914 return;
4915 }
4916 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4917 mst_file_data, objfile); */
4918 add_psymbol_to_list (actual_name, strlen (actual_name),
4919 built_actual_name,
4920 VAR_DOMAIN, LOC_STATIC,
4921 &objfile->static_psymbols,
4922 0, addr + baseaddr,
4923 cu->language, objfile);
4924 }
4925 break;
4926 case DW_TAG_typedef:
4927 case DW_TAG_base_type:
4928 case DW_TAG_subrange_type:
4929 add_psymbol_to_list (actual_name, strlen (actual_name),
4930 built_actual_name,
4931 VAR_DOMAIN, LOC_TYPEDEF,
4932 &objfile->static_psymbols,
4933 0, (CORE_ADDR) 0, cu->language, objfile);
4934 break;
4935 case DW_TAG_namespace:
4936 add_psymbol_to_list (actual_name, strlen (actual_name),
4937 built_actual_name,
4938 VAR_DOMAIN, LOC_TYPEDEF,
4939 &objfile->global_psymbols,
4940 0, (CORE_ADDR) 0, cu->language, objfile);
4941 break;
4942 case DW_TAG_class_type:
4943 case DW_TAG_interface_type:
4944 case DW_TAG_structure_type:
4945 case DW_TAG_union_type:
4946 case DW_TAG_enumeration_type:
4947 /* Skip external references. The DWARF standard says in the section
4948 about "Structure, Union, and Class Type Entries": "An incomplete
4949 structure, union or class type is represented by a structure,
4950 union or class entry that does not have a byte size attribute
4951 and that has a DW_AT_declaration attribute." */
4952 if (!pdi->has_byte_size && pdi->is_declaration)
4953 {
4954 if (built_actual_name)
4955 xfree (actual_name);
4956 return;
4957 }
4958
4959 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4960 static vs. global. */
4961 add_psymbol_to_list (actual_name, strlen (actual_name),
4962 built_actual_name,
4963 STRUCT_DOMAIN, LOC_TYPEDEF,
4964 (cu->language == language_cplus
4965 || cu->language == language_java)
4966 ? &objfile->global_psymbols
4967 : &objfile->static_psymbols,
4968 0, (CORE_ADDR) 0, cu->language, objfile);
4969
4970 break;
4971 case DW_TAG_enumerator:
4972 add_psymbol_to_list (actual_name, strlen (actual_name),
4973 built_actual_name,
4974 VAR_DOMAIN, LOC_CONST,
4975 (cu->language == language_cplus
4976 || cu->language == language_java)
4977 ? &objfile->global_psymbols
4978 : &objfile->static_psymbols,
4979 0, (CORE_ADDR) 0, cu->language, objfile);
4980 break;
4981 default:
4982 break;
4983 }
4984
4985 if (built_actual_name)
4986 xfree (actual_name);
4987 }
4988
4989 /* Read a partial die corresponding to a namespace; also, add a symbol
4990 corresponding to that namespace to the symbol table. NAMESPACE is
4991 the name of the enclosing namespace. */
4992
4993 static void
4994 add_partial_namespace (struct partial_die_info *pdi,
4995 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4996 int need_pc, struct dwarf2_cu *cu)
4997 {
4998 /* Add a symbol for the namespace. */
4999
5000 add_partial_symbol (pdi, cu);
5001
5002 /* Now scan partial symbols in that namespace. */
5003
5004 if (pdi->has_children)
5005 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5006 }
5007
5008 /* Read a partial die corresponding to a Fortran module. */
5009
5010 static void
5011 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5012 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5013 {
5014 /* Now scan partial symbols in that module. */
5015
5016 if (pdi->has_children)
5017 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5018 }
5019
5020 /* Read a partial die corresponding to a subprogram and create a partial
5021 symbol for that subprogram. When the CU language allows it, this
5022 routine also defines a partial symbol for each nested subprogram
5023 that this subprogram contains.
5024
5025 DIE my also be a lexical block, in which case we simply search
5026 recursively for suprograms defined inside that lexical block.
5027 Again, this is only performed when the CU language allows this
5028 type of definitions. */
5029
5030 static void
5031 add_partial_subprogram (struct partial_die_info *pdi,
5032 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5033 int need_pc, struct dwarf2_cu *cu)
5034 {
5035 if (pdi->tag == DW_TAG_subprogram)
5036 {
5037 if (pdi->has_pc_info)
5038 {
5039 if (pdi->lowpc < *lowpc)
5040 *lowpc = pdi->lowpc;
5041 if (pdi->highpc > *highpc)
5042 *highpc = pdi->highpc;
5043 if (need_pc)
5044 {
5045 CORE_ADDR baseaddr;
5046 struct objfile *objfile = cu->objfile;
5047
5048 baseaddr = ANOFFSET (objfile->section_offsets,
5049 SECT_OFF_TEXT (objfile));
5050 addrmap_set_empty (objfile->psymtabs_addrmap,
5051 pdi->lowpc + baseaddr,
5052 pdi->highpc - 1 + baseaddr,
5053 cu->per_cu->v.psymtab);
5054 }
5055 }
5056
5057 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5058 {
5059 if (!pdi->is_declaration)
5060 /* Ignore subprogram DIEs that do not have a name, they are
5061 illegal. Do not emit a complaint at this point, we will
5062 do so when we convert this psymtab into a symtab. */
5063 if (pdi->name)
5064 add_partial_symbol (pdi, cu);
5065 }
5066 }
5067
5068 if (! pdi->has_children)
5069 return;
5070
5071 if (cu->language == language_ada)
5072 {
5073 pdi = pdi->die_child;
5074 while (pdi != NULL)
5075 {
5076 fixup_partial_die (pdi, cu);
5077 if (pdi->tag == DW_TAG_subprogram
5078 || pdi->tag == DW_TAG_lexical_block)
5079 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5080 pdi = pdi->die_sibling;
5081 }
5082 }
5083 }
5084
5085 /* Read a partial die corresponding to an enumeration type. */
5086
5087 static void
5088 add_partial_enumeration (struct partial_die_info *enum_pdi,
5089 struct dwarf2_cu *cu)
5090 {
5091 struct partial_die_info *pdi;
5092
5093 if (enum_pdi->name != NULL)
5094 add_partial_symbol (enum_pdi, cu);
5095
5096 pdi = enum_pdi->die_child;
5097 while (pdi)
5098 {
5099 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
5100 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5101 else
5102 add_partial_symbol (pdi, cu);
5103 pdi = pdi->die_sibling;
5104 }
5105 }
5106
5107 /* Return the initial uleb128 in the die at INFO_PTR. */
5108
5109 static unsigned int
5110 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5111 {
5112 unsigned int bytes_read;
5113
5114 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5115 }
5116
5117 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5118 Return the corresponding abbrev, or NULL if the number is zero (indicating
5119 an empty DIE). In either case *BYTES_READ will be set to the length of
5120 the initial number. */
5121
5122 static struct abbrev_info *
5123 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5124 struct dwarf2_cu *cu)
5125 {
5126 bfd *abfd = cu->objfile->obfd;
5127 unsigned int abbrev_number;
5128 struct abbrev_info *abbrev;
5129
5130 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5131
5132 if (abbrev_number == 0)
5133 return NULL;
5134
5135 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5136 if (!abbrev)
5137 {
5138 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5139 abbrev_number, bfd_get_filename (abfd));
5140 }
5141
5142 return abbrev;
5143 }
5144
5145 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5146 Returns a pointer to the end of a series of DIEs, terminated by an empty
5147 DIE. Any children of the skipped DIEs will also be skipped. */
5148
5149 static gdb_byte *
5150 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5151 {
5152 struct dwarf2_cu *cu = reader->cu;
5153 struct abbrev_info *abbrev;
5154 unsigned int bytes_read;
5155
5156 while (1)
5157 {
5158 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5159 if (abbrev == NULL)
5160 return info_ptr + bytes_read;
5161 else
5162 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5163 }
5164 }
5165
5166 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5167 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5168 abbrev corresponding to that skipped uleb128 should be passed in
5169 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5170 children. */
5171
5172 static gdb_byte *
5173 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5174 struct abbrev_info *abbrev)
5175 {
5176 unsigned int bytes_read;
5177 struct attribute attr;
5178 bfd *abfd = reader->abfd;
5179 struct dwarf2_cu *cu = reader->cu;
5180 gdb_byte *buffer = reader->buffer;
5181 const gdb_byte *buffer_end = reader->buffer_end;
5182 gdb_byte *start_info_ptr = info_ptr;
5183 unsigned int form, i;
5184
5185 for (i = 0; i < abbrev->num_attrs; i++)
5186 {
5187 /* The only abbrev we care about is DW_AT_sibling. */
5188 if (abbrev->attrs[i].name == DW_AT_sibling)
5189 {
5190 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5191 if (attr.form == DW_FORM_ref_addr)
5192 complaint (&symfile_complaints,
5193 _("ignoring absolute DW_AT_sibling"));
5194 else
5195 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5196 }
5197
5198 /* If it isn't DW_AT_sibling, skip this attribute. */
5199 form = abbrev->attrs[i].form;
5200 skip_attribute:
5201 switch (form)
5202 {
5203 case DW_FORM_ref_addr:
5204 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5205 and later it is offset sized. */
5206 if (cu->header.version == 2)
5207 info_ptr += cu->header.addr_size;
5208 else
5209 info_ptr += cu->header.offset_size;
5210 break;
5211 case DW_FORM_addr:
5212 info_ptr += cu->header.addr_size;
5213 break;
5214 case DW_FORM_data1:
5215 case DW_FORM_ref1:
5216 case DW_FORM_flag:
5217 info_ptr += 1;
5218 break;
5219 case DW_FORM_flag_present:
5220 break;
5221 case DW_FORM_data2:
5222 case DW_FORM_ref2:
5223 info_ptr += 2;
5224 break;
5225 case DW_FORM_data4:
5226 case DW_FORM_ref4:
5227 info_ptr += 4;
5228 break;
5229 case DW_FORM_data8:
5230 case DW_FORM_ref8:
5231 case DW_FORM_ref_sig8:
5232 info_ptr += 8;
5233 break;
5234 case DW_FORM_string:
5235 read_direct_string (abfd, info_ptr, &bytes_read);
5236 info_ptr += bytes_read;
5237 break;
5238 case DW_FORM_sec_offset:
5239 case DW_FORM_strp:
5240 info_ptr += cu->header.offset_size;
5241 break;
5242 case DW_FORM_exprloc:
5243 case DW_FORM_block:
5244 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5245 info_ptr += bytes_read;
5246 break;
5247 case DW_FORM_block1:
5248 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5249 break;
5250 case DW_FORM_block2:
5251 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5252 break;
5253 case DW_FORM_block4:
5254 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5255 break;
5256 case DW_FORM_sdata:
5257 case DW_FORM_udata:
5258 case DW_FORM_ref_udata:
5259 case DW_FORM_GNU_addr_index:
5260 case DW_FORM_GNU_str_index:
5261 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5262 break;
5263 case DW_FORM_indirect:
5264 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5265 info_ptr += bytes_read;
5266 /* We need to continue parsing from here, so just go back to
5267 the top. */
5268 goto skip_attribute;
5269
5270 default:
5271 error (_("Dwarf Error: Cannot handle %s "
5272 "in DWARF reader [in module %s]"),
5273 dwarf_form_name (form),
5274 bfd_get_filename (abfd));
5275 }
5276 }
5277
5278 if (abbrev->has_children)
5279 return skip_children (reader, info_ptr);
5280 else
5281 return info_ptr;
5282 }
5283
5284 /* Locate ORIG_PDI's sibling.
5285 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5286
5287 static gdb_byte *
5288 locate_pdi_sibling (const struct die_reader_specs *reader,
5289 struct partial_die_info *orig_pdi,
5290 gdb_byte *info_ptr)
5291 {
5292 /* Do we know the sibling already? */
5293
5294 if (orig_pdi->sibling)
5295 return orig_pdi->sibling;
5296
5297 /* Are there any children to deal with? */
5298
5299 if (!orig_pdi->has_children)
5300 return info_ptr;
5301
5302 /* Skip the children the long way. */
5303
5304 return skip_children (reader, info_ptr);
5305 }
5306
5307 /* Expand this partial symbol table into a full symbol table. */
5308
5309 static void
5310 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5311 {
5312 if (pst != NULL)
5313 {
5314 if (pst->readin)
5315 {
5316 warning (_("bug: psymtab for %s is already read in."),
5317 pst->filename);
5318 }
5319 else
5320 {
5321 if (info_verbose)
5322 {
5323 printf_filtered (_("Reading in symbols for %s..."),
5324 pst->filename);
5325 gdb_flush (gdb_stdout);
5326 }
5327
5328 /* Restore our global data. */
5329 dwarf2_per_objfile = objfile_data (pst->objfile,
5330 dwarf2_objfile_data_key);
5331
5332 /* If this psymtab is constructed from a debug-only objfile, the
5333 has_section_at_zero flag will not necessarily be correct. We
5334 can get the correct value for this flag by looking at the data
5335 associated with the (presumably stripped) associated objfile. */
5336 if (pst->objfile->separate_debug_objfile_backlink)
5337 {
5338 struct dwarf2_per_objfile *dpo_backlink
5339 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5340 dwarf2_objfile_data_key);
5341
5342 dwarf2_per_objfile->has_section_at_zero
5343 = dpo_backlink->has_section_at_zero;
5344 }
5345
5346 dwarf2_per_objfile->reading_partial_symbols = 0;
5347
5348 psymtab_to_symtab_1 (pst);
5349
5350 /* Finish up the debug error message. */
5351 if (info_verbose)
5352 printf_filtered (_("done.\n"));
5353 }
5354 }
5355
5356 process_cu_includes ();
5357 }
5358 \f
5359 /* Reading in full CUs. */
5360
5361 /* Add PER_CU to the queue. */
5362
5363 static void
5364 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5365 enum language pretend_language)
5366 {
5367 struct dwarf2_queue_item *item;
5368
5369 per_cu->queued = 1;
5370 item = xmalloc (sizeof (*item));
5371 item->per_cu = per_cu;
5372 item->pretend_language = pretend_language;
5373 item->next = NULL;
5374
5375 if (dwarf2_queue == NULL)
5376 dwarf2_queue = item;
5377 else
5378 dwarf2_queue_tail->next = item;
5379
5380 dwarf2_queue_tail = item;
5381 }
5382
5383 /* Process the queue. */
5384
5385 static void
5386 process_queue (void)
5387 {
5388 struct dwarf2_queue_item *item, *next_item;
5389
5390 /* The queue starts out with one item, but following a DIE reference
5391 may load a new CU, adding it to the end of the queue. */
5392 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5393 {
5394 if (dwarf2_per_objfile->using_index
5395 ? !item->per_cu->v.quick->symtab
5396 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5397 process_full_comp_unit (item->per_cu, item->pretend_language);
5398
5399 item->per_cu->queued = 0;
5400 next_item = item->next;
5401 xfree (item);
5402 }
5403
5404 dwarf2_queue_tail = NULL;
5405 }
5406
5407 /* Free all allocated queue entries. This function only releases anything if
5408 an error was thrown; if the queue was processed then it would have been
5409 freed as we went along. */
5410
5411 static void
5412 dwarf2_release_queue (void *dummy)
5413 {
5414 struct dwarf2_queue_item *item, *last;
5415
5416 item = dwarf2_queue;
5417 while (item)
5418 {
5419 /* Anything still marked queued is likely to be in an
5420 inconsistent state, so discard it. */
5421 if (item->per_cu->queued)
5422 {
5423 if (item->per_cu->cu != NULL)
5424 free_one_cached_comp_unit (item->per_cu);
5425 item->per_cu->queued = 0;
5426 }
5427
5428 last = item;
5429 item = item->next;
5430 xfree (last);
5431 }
5432
5433 dwarf2_queue = dwarf2_queue_tail = NULL;
5434 }
5435
5436 /* Read in full symbols for PST, and anything it depends on. */
5437
5438 static void
5439 psymtab_to_symtab_1 (struct partial_symtab *pst)
5440 {
5441 struct dwarf2_per_cu_data *per_cu;
5442 int i;
5443
5444 if (pst->readin)
5445 return;
5446
5447 for (i = 0; i < pst->number_of_dependencies; i++)
5448 if (!pst->dependencies[i]->readin
5449 && pst->dependencies[i]->user == NULL)
5450 {
5451 /* Inform about additional files that need to be read in. */
5452 if (info_verbose)
5453 {
5454 /* FIXME: i18n: Need to make this a single string. */
5455 fputs_filtered (" ", gdb_stdout);
5456 wrap_here ("");
5457 fputs_filtered ("and ", gdb_stdout);
5458 wrap_here ("");
5459 printf_filtered ("%s...", pst->dependencies[i]->filename);
5460 wrap_here (""); /* Flush output. */
5461 gdb_flush (gdb_stdout);
5462 }
5463 psymtab_to_symtab_1 (pst->dependencies[i]);
5464 }
5465
5466 per_cu = pst->read_symtab_private;
5467
5468 if (per_cu == NULL)
5469 {
5470 /* It's an include file, no symbols to read for it.
5471 Everything is in the parent symtab. */
5472 pst->readin = 1;
5473 return;
5474 }
5475
5476 dw2_do_instantiate_symtab (per_cu);
5477 }
5478
5479 /* Trivial hash function for die_info: the hash value of a DIE
5480 is its offset in .debug_info for this objfile. */
5481
5482 static hashval_t
5483 die_hash (const void *item)
5484 {
5485 const struct die_info *die = item;
5486
5487 return die->offset.sect_off;
5488 }
5489
5490 /* Trivial comparison function for die_info structures: two DIEs
5491 are equal if they have the same offset. */
5492
5493 static int
5494 die_eq (const void *item_lhs, const void *item_rhs)
5495 {
5496 const struct die_info *die_lhs = item_lhs;
5497 const struct die_info *die_rhs = item_rhs;
5498
5499 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5500 }
5501
5502 /* die_reader_func for load_full_comp_unit.
5503 This is identical to read_signatured_type_reader,
5504 but is kept separate for now. */
5505
5506 static void
5507 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5508 gdb_byte *info_ptr,
5509 struct die_info *comp_unit_die,
5510 int has_children,
5511 void *data)
5512 {
5513 struct dwarf2_cu *cu = reader->cu;
5514 enum language *language_ptr = data;
5515
5516 gdb_assert (cu->die_hash == NULL);
5517 cu->die_hash =
5518 htab_create_alloc_ex (cu->header.length / 12,
5519 die_hash,
5520 die_eq,
5521 NULL,
5522 &cu->comp_unit_obstack,
5523 hashtab_obstack_allocate,
5524 dummy_obstack_deallocate);
5525
5526 if (has_children)
5527 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5528 &info_ptr, comp_unit_die);
5529 cu->dies = comp_unit_die;
5530 /* comp_unit_die is not stored in die_hash, no need. */
5531
5532 /* We try not to read any attributes in this function, because not
5533 all CUs needed for references have been loaded yet, and symbol
5534 table processing isn't initialized. But we have to set the CU language,
5535 or we won't be able to build types correctly.
5536 Similarly, if we do not read the producer, we can not apply
5537 producer-specific interpretation. */
5538 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5539 }
5540
5541 /* Load the DIEs associated with PER_CU into memory. */
5542
5543 static void
5544 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5545 enum language pretend_language)
5546 {
5547 gdb_assert (! this_cu->is_debug_types);
5548
5549 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5550 &pretend_language);
5551 }
5552
5553 /* Add a DIE to the delayed physname list. */
5554
5555 static void
5556 add_to_method_list (struct type *type, int fnfield_index, int index,
5557 const char *name, struct die_info *die,
5558 struct dwarf2_cu *cu)
5559 {
5560 struct delayed_method_info mi;
5561 mi.type = type;
5562 mi.fnfield_index = fnfield_index;
5563 mi.index = index;
5564 mi.name = name;
5565 mi.die = die;
5566 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5567 }
5568
5569 /* A cleanup for freeing the delayed method list. */
5570
5571 static void
5572 free_delayed_list (void *ptr)
5573 {
5574 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5575 if (cu->method_list != NULL)
5576 {
5577 VEC_free (delayed_method_info, cu->method_list);
5578 cu->method_list = NULL;
5579 }
5580 }
5581
5582 /* Compute the physnames of any methods on the CU's method list.
5583
5584 The computation of method physnames is delayed in order to avoid the
5585 (bad) condition that one of the method's formal parameters is of an as yet
5586 incomplete type. */
5587
5588 static void
5589 compute_delayed_physnames (struct dwarf2_cu *cu)
5590 {
5591 int i;
5592 struct delayed_method_info *mi;
5593 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5594 {
5595 const char *physname;
5596 struct fn_fieldlist *fn_flp
5597 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5598 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5599 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5600 }
5601 }
5602
5603 /* Go objects should be embedded in a DW_TAG_module DIE,
5604 and it's not clear if/how imported objects will appear.
5605 To keep Go support simple until that's worked out,
5606 go back through what we've read and create something usable.
5607 We could do this while processing each DIE, and feels kinda cleaner,
5608 but that way is more invasive.
5609 This is to, for example, allow the user to type "p var" or "b main"
5610 without having to specify the package name, and allow lookups
5611 of module.object to work in contexts that use the expression
5612 parser. */
5613
5614 static void
5615 fixup_go_packaging (struct dwarf2_cu *cu)
5616 {
5617 char *package_name = NULL;
5618 struct pending *list;
5619 int i;
5620
5621 for (list = global_symbols; list != NULL; list = list->next)
5622 {
5623 for (i = 0; i < list->nsyms; ++i)
5624 {
5625 struct symbol *sym = list->symbol[i];
5626
5627 if (SYMBOL_LANGUAGE (sym) == language_go
5628 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5629 {
5630 char *this_package_name = go_symbol_package_name (sym);
5631
5632 if (this_package_name == NULL)
5633 continue;
5634 if (package_name == NULL)
5635 package_name = this_package_name;
5636 else
5637 {
5638 if (strcmp (package_name, this_package_name) != 0)
5639 complaint (&symfile_complaints,
5640 _("Symtab %s has objects from two different Go packages: %s and %s"),
5641 (sym->symtab && sym->symtab->filename
5642 ? sym->symtab->filename
5643 : cu->objfile->name),
5644 this_package_name, package_name);
5645 xfree (this_package_name);
5646 }
5647 }
5648 }
5649 }
5650
5651 if (package_name != NULL)
5652 {
5653 struct objfile *objfile = cu->objfile;
5654 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5655 package_name, objfile);
5656 struct symbol *sym;
5657
5658 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5659
5660 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5661 SYMBOL_SET_LANGUAGE (sym, language_go);
5662 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5663 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5664 e.g., "main" finds the "main" module and not C's main(). */
5665 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5666 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5667 SYMBOL_TYPE (sym) = type;
5668
5669 add_symbol_to_list (sym, &global_symbols);
5670
5671 xfree (package_name);
5672 }
5673 }
5674
5675 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5676
5677 /* Return the symtab for PER_CU. This works properly regardless of
5678 whether we're using the index or psymtabs. */
5679
5680 static struct symtab *
5681 get_symtab (struct dwarf2_per_cu_data *per_cu)
5682 {
5683 return (dwarf2_per_objfile->using_index
5684 ? per_cu->v.quick->symtab
5685 : per_cu->v.psymtab->symtab);
5686 }
5687
5688 /* A helper function for computing the list of all symbol tables
5689 included by PER_CU. */
5690
5691 static void
5692 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5693 htab_t all_children,
5694 struct dwarf2_per_cu_data *per_cu)
5695 {
5696 void **slot;
5697 int ix;
5698 struct dwarf2_per_cu_data *iter;
5699
5700 slot = htab_find_slot (all_children, per_cu, INSERT);
5701 if (*slot != NULL)
5702 {
5703 /* This inclusion and its children have been processed. */
5704 return;
5705 }
5706
5707 *slot = per_cu;
5708 /* Only add a CU if it has a symbol table. */
5709 if (get_symtab (per_cu) != NULL)
5710 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5711
5712 for (ix = 0;
5713 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5714 ++ix)
5715 recursively_compute_inclusions (result, all_children, iter);
5716 }
5717
5718 /* Compute the symtab 'includes' fields for the symtab related to
5719 PER_CU. */
5720
5721 static void
5722 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5723 {
5724 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5725 {
5726 int ix, len;
5727 struct dwarf2_per_cu_data *iter;
5728 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5729 htab_t all_children;
5730 struct symtab *symtab = get_symtab (per_cu);
5731
5732 /* If we don't have a symtab, we can just skip this case. */
5733 if (symtab == NULL)
5734 return;
5735
5736 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5737 NULL, xcalloc, xfree);
5738
5739 for (ix = 0;
5740 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5741 ix, iter);
5742 ++ix)
5743 recursively_compute_inclusions (&result_children, all_children, iter);
5744
5745 /* Now we have a transitive closure of all the included CUs, so
5746 we can convert it to a list of symtabs. */
5747 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5748 symtab->includes
5749 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5750 (len + 1) * sizeof (struct symtab *));
5751 for (ix = 0;
5752 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5753 ++ix)
5754 symtab->includes[ix] = get_symtab (iter);
5755 symtab->includes[len] = NULL;
5756
5757 VEC_free (dwarf2_per_cu_ptr, result_children);
5758 htab_delete (all_children);
5759 }
5760 }
5761
5762 /* Compute the 'includes' field for the symtabs of all the CUs we just
5763 read. */
5764
5765 static void
5766 process_cu_includes (void)
5767 {
5768 int ix;
5769 struct dwarf2_per_cu_data *iter;
5770
5771 for (ix = 0;
5772 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5773 ix, iter);
5774 ++ix)
5775 compute_symtab_includes (iter);
5776
5777 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5778 }
5779
5780 /* Generate full symbol information for PER_CU, whose DIEs have
5781 already been loaded into memory. */
5782
5783 static void
5784 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5785 enum language pretend_language)
5786 {
5787 struct dwarf2_cu *cu = per_cu->cu;
5788 struct objfile *objfile = per_cu->objfile;
5789 CORE_ADDR lowpc, highpc;
5790 struct symtab *symtab;
5791 struct cleanup *back_to, *delayed_list_cleanup;
5792 CORE_ADDR baseaddr;
5793
5794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5795
5796 buildsym_init ();
5797 back_to = make_cleanup (really_free_pendings, NULL);
5798 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5799
5800 cu->list_in_scope = &file_symbols;
5801
5802 cu->language = pretend_language;
5803 cu->language_defn = language_def (cu->language);
5804
5805 /* Do line number decoding in read_file_scope () */
5806 process_die (cu->dies, cu);
5807
5808 /* For now fudge the Go package. */
5809 if (cu->language == language_go)
5810 fixup_go_packaging (cu);
5811
5812 /* Now that we have processed all the DIEs in the CU, all the types
5813 should be complete, and it should now be safe to compute all of the
5814 physnames. */
5815 compute_delayed_physnames (cu);
5816 do_cleanups (delayed_list_cleanup);
5817
5818 /* Some compilers don't define a DW_AT_high_pc attribute for the
5819 compilation unit. If the DW_AT_high_pc is missing, synthesize
5820 it, by scanning the DIE's below the compilation unit. */
5821 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5822
5823 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5824
5825 if (symtab != NULL)
5826 {
5827 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5828
5829 /* Set symtab language to language from DW_AT_language. If the
5830 compilation is from a C file generated by language preprocessors, do
5831 not set the language if it was already deduced by start_subfile. */
5832 if (!(cu->language == language_c && symtab->language != language_c))
5833 symtab->language = cu->language;
5834
5835 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5836 produce DW_AT_location with location lists but it can be possibly
5837 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5838 there were bugs in prologue debug info, fixed later in GCC-4.5
5839 by "unwind info for epilogues" patch (which is not directly related).
5840
5841 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5842 needed, it would be wrong due to missing DW_AT_producer there.
5843
5844 Still one can confuse GDB by using non-standard GCC compilation
5845 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5846 */
5847 if (cu->has_loclist && gcc_4_minor >= 5)
5848 symtab->locations_valid = 1;
5849
5850 if (gcc_4_minor >= 5)
5851 symtab->epilogue_unwind_valid = 1;
5852
5853 symtab->call_site_htab = cu->call_site_htab;
5854 }
5855
5856 if (dwarf2_per_objfile->using_index)
5857 per_cu->v.quick->symtab = symtab;
5858 else
5859 {
5860 struct partial_symtab *pst = per_cu->v.psymtab;
5861 pst->symtab = symtab;
5862 pst->readin = 1;
5863 }
5864
5865 /* Push it for inclusion processing later. */
5866 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5867
5868 do_cleanups (back_to);
5869 }
5870
5871 /* Process an imported unit DIE. */
5872
5873 static void
5874 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5875 {
5876 struct attribute *attr;
5877
5878 attr = dwarf2_attr (die, DW_AT_import, cu);
5879 if (attr != NULL)
5880 {
5881 struct dwarf2_per_cu_data *per_cu;
5882 struct symtab *imported_symtab;
5883 sect_offset offset;
5884
5885 offset = dwarf2_get_ref_die_offset (attr);
5886 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5887
5888 /* Queue the unit, if needed. */
5889 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5890 load_full_comp_unit (per_cu, cu->language);
5891
5892 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5893 per_cu);
5894 }
5895 }
5896
5897 /* Process a die and its children. */
5898
5899 static void
5900 process_die (struct die_info *die, struct dwarf2_cu *cu)
5901 {
5902 switch (die->tag)
5903 {
5904 case DW_TAG_padding:
5905 break;
5906 case DW_TAG_compile_unit:
5907 case DW_TAG_partial_unit:
5908 read_file_scope (die, cu);
5909 break;
5910 case DW_TAG_type_unit:
5911 read_type_unit_scope (die, cu);
5912 break;
5913 case DW_TAG_subprogram:
5914 case DW_TAG_inlined_subroutine:
5915 read_func_scope (die, cu);
5916 break;
5917 case DW_TAG_lexical_block:
5918 case DW_TAG_try_block:
5919 case DW_TAG_catch_block:
5920 read_lexical_block_scope (die, cu);
5921 break;
5922 case DW_TAG_GNU_call_site:
5923 read_call_site_scope (die, cu);
5924 break;
5925 case DW_TAG_class_type:
5926 case DW_TAG_interface_type:
5927 case DW_TAG_structure_type:
5928 case DW_TAG_union_type:
5929 process_structure_scope (die, cu);
5930 break;
5931 case DW_TAG_enumeration_type:
5932 process_enumeration_scope (die, cu);
5933 break;
5934
5935 /* These dies have a type, but processing them does not create
5936 a symbol or recurse to process the children. Therefore we can
5937 read them on-demand through read_type_die. */
5938 case DW_TAG_subroutine_type:
5939 case DW_TAG_set_type:
5940 case DW_TAG_array_type:
5941 case DW_TAG_pointer_type:
5942 case DW_TAG_ptr_to_member_type:
5943 case DW_TAG_reference_type:
5944 case DW_TAG_string_type:
5945 break;
5946
5947 case DW_TAG_base_type:
5948 case DW_TAG_subrange_type:
5949 case DW_TAG_typedef:
5950 /* Add a typedef symbol for the type definition, if it has a
5951 DW_AT_name. */
5952 new_symbol (die, read_type_die (die, cu), cu);
5953 break;
5954 case DW_TAG_common_block:
5955 read_common_block (die, cu);
5956 break;
5957 case DW_TAG_common_inclusion:
5958 break;
5959 case DW_TAG_namespace:
5960 processing_has_namespace_info = 1;
5961 read_namespace (die, cu);
5962 break;
5963 case DW_TAG_module:
5964 processing_has_namespace_info = 1;
5965 read_module (die, cu);
5966 break;
5967 case DW_TAG_imported_declaration:
5968 case DW_TAG_imported_module:
5969 processing_has_namespace_info = 1;
5970 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5971 || cu->language != language_fortran))
5972 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5973 dwarf_tag_name (die->tag));
5974 read_import_statement (die, cu);
5975 break;
5976
5977 case DW_TAG_imported_unit:
5978 process_imported_unit_die (die, cu);
5979 break;
5980
5981 default:
5982 new_symbol (die, NULL, cu);
5983 break;
5984 }
5985 }
5986
5987 /* A helper function for dwarf2_compute_name which determines whether DIE
5988 needs to have the name of the scope prepended to the name listed in the
5989 die. */
5990
5991 static int
5992 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5993 {
5994 struct attribute *attr;
5995
5996 switch (die->tag)
5997 {
5998 case DW_TAG_namespace:
5999 case DW_TAG_typedef:
6000 case DW_TAG_class_type:
6001 case DW_TAG_interface_type:
6002 case DW_TAG_structure_type:
6003 case DW_TAG_union_type:
6004 case DW_TAG_enumeration_type:
6005 case DW_TAG_enumerator:
6006 case DW_TAG_subprogram:
6007 case DW_TAG_member:
6008 return 1;
6009
6010 case DW_TAG_variable:
6011 case DW_TAG_constant:
6012 /* We only need to prefix "globally" visible variables. These include
6013 any variable marked with DW_AT_external or any variable that
6014 lives in a namespace. [Variables in anonymous namespaces
6015 require prefixing, but they are not DW_AT_external.] */
6016
6017 if (dwarf2_attr (die, DW_AT_specification, cu))
6018 {
6019 struct dwarf2_cu *spec_cu = cu;
6020
6021 return die_needs_namespace (die_specification (die, &spec_cu),
6022 spec_cu);
6023 }
6024
6025 attr = dwarf2_attr (die, DW_AT_external, cu);
6026 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6027 && die->parent->tag != DW_TAG_module)
6028 return 0;
6029 /* A variable in a lexical block of some kind does not need a
6030 namespace, even though in C++ such variables may be external
6031 and have a mangled name. */
6032 if (die->parent->tag == DW_TAG_lexical_block
6033 || die->parent->tag == DW_TAG_try_block
6034 || die->parent->tag == DW_TAG_catch_block
6035 || die->parent->tag == DW_TAG_subprogram)
6036 return 0;
6037 return 1;
6038
6039 default:
6040 return 0;
6041 }
6042 }
6043
6044 /* Retrieve the last character from a mem_file. */
6045
6046 static void
6047 do_ui_file_peek_last (void *object, const char *buffer, long length)
6048 {
6049 char *last_char_p = (char *) object;
6050
6051 if (length > 0)
6052 *last_char_p = buffer[length - 1];
6053 }
6054
6055 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6056 compute the physname for the object, which include a method's:
6057 - formal parameters (C++/Java),
6058 - receiver type (Go),
6059 - return type (Java).
6060
6061 The term "physname" is a bit confusing.
6062 For C++, for example, it is the demangled name.
6063 For Go, for example, it's the mangled name.
6064
6065 For Ada, return the DIE's linkage name rather than the fully qualified
6066 name. PHYSNAME is ignored..
6067
6068 The result is allocated on the objfile_obstack and canonicalized. */
6069
6070 static const char *
6071 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6072 int physname)
6073 {
6074 struct objfile *objfile = cu->objfile;
6075
6076 if (name == NULL)
6077 name = dwarf2_name (die, cu);
6078
6079 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6080 compute it by typename_concat inside GDB. */
6081 if (cu->language == language_ada
6082 || (cu->language == language_fortran && physname))
6083 {
6084 /* For Ada unit, we prefer the linkage name over the name, as
6085 the former contains the exported name, which the user expects
6086 to be able to reference. Ideally, we want the user to be able
6087 to reference this entity using either natural or linkage name,
6088 but we haven't started looking at this enhancement yet. */
6089 struct attribute *attr;
6090
6091 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6092 if (attr == NULL)
6093 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6094 if (attr && DW_STRING (attr))
6095 return DW_STRING (attr);
6096 }
6097
6098 /* These are the only languages we know how to qualify names in. */
6099 if (name != NULL
6100 && (cu->language == language_cplus || cu->language == language_java
6101 || cu->language == language_fortran))
6102 {
6103 if (die_needs_namespace (die, cu))
6104 {
6105 long length;
6106 const char *prefix;
6107 struct ui_file *buf;
6108
6109 prefix = determine_prefix (die, cu);
6110 buf = mem_fileopen ();
6111 if (*prefix != '\0')
6112 {
6113 char *prefixed_name = typename_concat (NULL, prefix, name,
6114 physname, cu);
6115
6116 fputs_unfiltered (prefixed_name, buf);
6117 xfree (prefixed_name);
6118 }
6119 else
6120 fputs_unfiltered (name, buf);
6121
6122 /* Template parameters may be specified in the DIE's DW_AT_name, or
6123 as children with DW_TAG_template_type_param or
6124 DW_TAG_value_type_param. If the latter, add them to the name
6125 here. If the name already has template parameters, then
6126 skip this step; some versions of GCC emit both, and
6127 it is more efficient to use the pre-computed name.
6128
6129 Something to keep in mind about this process: it is very
6130 unlikely, or in some cases downright impossible, to produce
6131 something that will match the mangled name of a function.
6132 If the definition of the function has the same debug info,
6133 we should be able to match up with it anyway. But fallbacks
6134 using the minimal symbol, for instance to find a method
6135 implemented in a stripped copy of libstdc++, will not work.
6136 If we do not have debug info for the definition, we will have to
6137 match them up some other way.
6138
6139 When we do name matching there is a related problem with function
6140 templates; two instantiated function templates are allowed to
6141 differ only by their return types, which we do not add here. */
6142
6143 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6144 {
6145 struct attribute *attr;
6146 struct die_info *child;
6147 int first = 1;
6148
6149 die->building_fullname = 1;
6150
6151 for (child = die->child; child != NULL; child = child->sibling)
6152 {
6153 struct type *type;
6154 LONGEST value;
6155 gdb_byte *bytes;
6156 struct dwarf2_locexpr_baton *baton;
6157 struct value *v;
6158
6159 if (child->tag != DW_TAG_template_type_param
6160 && child->tag != DW_TAG_template_value_param)
6161 continue;
6162
6163 if (first)
6164 {
6165 fputs_unfiltered ("<", buf);
6166 first = 0;
6167 }
6168 else
6169 fputs_unfiltered (", ", buf);
6170
6171 attr = dwarf2_attr (child, DW_AT_type, cu);
6172 if (attr == NULL)
6173 {
6174 complaint (&symfile_complaints,
6175 _("template parameter missing DW_AT_type"));
6176 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6177 continue;
6178 }
6179 type = die_type (child, cu);
6180
6181 if (child->tag == DW_TAG_template_type_param)
6182 {
6183 c_print_type (type, "", buf, -1, 0);
6184 continue;
6185 }
6186
6187 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6188 if (attr == NULL)
6189 {
6190 complaint (&symfile_complaints,
6191 _("template parameter missing "
6192 "DW_AT_const_value"));
6193 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6194 continue;
6195 }
6196
6197 dwarf2_const_value_attr (attr, type, name,
6198 &cu->comp_unit_obstack, cu,
6199 &value, &bytes, &baton);
6200
6201 if (TYPE_NOSIGN (type))
6202 /* GDB prints characters as NUMBER 'CHAR'. If that's
6203 changed, this can use value_print instead. */
6204 c_printchar (value, type, buf);
6205 else
6206 {
6207 struct value_print_options opts;
6208
6209 if (baton != NULL)
6210 v = dwarf2_evaluate_loc_desc (type, NULL,
6211 baton->data,
6212 baton->size,
6213 baton->per_cu);
6214 else if (bytes != NULL)
6215 {
6216 v = allocate_value (type);
6217 memcpy (value_contents_writeable (v), bytes,
6218 TYPE_LENGTH (type));
6219 }
6220 else
6221 v = value_from_longest (type, value);
6222
6223 /* Specify decimal so that we do not depend on
6224 the radix. */
6225 get_formatted_print_options (&opts, 'd');
6226 opts.raw = 1;
6227 value_print (v, buf, &opts);
6228 release_value (v);
6229 value_free (v);
6230 }
6231 }
6232
6233 die->building_fullname = 0;
6234
6235 if (!first)
6236 {
6237 /* Close the argument list, with a space if necessary
6238 (nested templates). */
6239 char last_char = '\0';
6240 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6241 if (last_char == '>')
6242 fputs_unfiltered (" >", buf);
6243 else
6244 fputs_unfiltered (">", buf);
6245 }
6246 }
6247
6248 /* For Java and C++ methods, append formal parameter type
6249 information, if PHYSNAME. */
6250
6251 if (physname && die->tag == DW_TAG_subprogram
6252 && (cu->language == language_cplus
6253 || cu->language == language_java))
6254 {
6255 struct type *type = read_type_die (die, cu);
6256
6257 c_type_print_args (type, buf, 1, cu->language);
6258
6259 if (cu->language == language_java)
6260 {
6261 /* For java, we must append the return type to method
6262 names. */
6263 if (die->tag == DW_TAG_subprogram)
6264 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6265 0, 0);
6266 }
6267 else if (cu->language == language_cplus)
6268 {
6269 /* Assume that an artificial first parameter is
6270 "this", but do not crash if it is not. RealView
6271 marks unnamed (and thus unused) parameters as
6272 artificial; there is no way to differentiate
6273 the two cases. */
6274 if (TYPE_NFIELDS (type) > 0
6275 && TYPE_FIELD_ARTIFICIAL (type, 0)
6276 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6277 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6278 0))))
6279 fputs_unfiltered (" const", buf);
6280 }
6281 }
6282
6283 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6284 &length);
6285 ui_file_delete (buf);
6286
6287 if (cu->language == language_cplus)
6288 {
6289 char *cname
6290 = dwarf2_canonicalize_name (name, cu,
6291 &objfile->objfile_obstack);
6292
6293 if (cname != NULL)
6294 name = cname;
6295 }
6296 }
6297 }
6298
6299 return name;
6300 }
6301
6302 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6303 If scope qualifiers are appropriate they will be added. The result
6304 will be allocated on the objfile_obstack, or NULL if the DIE does
6305 not have a name. NAME may either be from a previous call to
6306 dwarf2_name or NULL.
6307
6308 The output string will be canonicalized (if C++/Java). */
6309
6310 static const char *
6311 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6312 {
6313 return dwarf2_compute_name (name, die, cu, 0);
6314 }
6315
6316 /* Construct a physname for the given DIE in CU. NAME may either be
6317 from a previous call to dwarf2_name or NULL. The result will be
6318 allocated on the objfile_objstack or NULL if the DIE does not have a
6319 name.
6320
6321 The output string will be canonicalized (if C++/Java). */
6322
6323 static const char *
6324 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6325 {
6326 struct objfile *objfile = cu->objfile;
6327 struct attribute *attr;
6328 const char *retval, *mangled = NULL, *canon = NULL;
6329 struct cleanup *back_to;
6330 int need_copy = 1;
6331
6332 /* In this case dwarf2_compute_name is just a shortcut not building anything
6333 on its own. */
6334 if (!die_needs_namespace (die, cu))
6335 return dwarf2_compute_name (name, die, cu, 1);
6336
6337 back_to = make_cleanup (null_cleanup, NULL);
6338
6339 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6340 if (!attr)
6341 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6342
6343 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6344 has computed. */
6345 if (attr && DW_STRING (attr))
6346 {
6347 char *demangled;
6348
6349 mangled = DW_STRING (attr);
6350
6351 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6352 type. It is easier for GDB users to search for such functions as
6353 `name(params)' than `long name(params)'. In such case the minimal
6354 symbol names do not match the full symbol names but for template
6355 functions there is never a need to look up their definition from their
6356 declaration so the only disadvantage remains the minimal symbol
6357 variant `long name(params)' does not have the proper inferior type.
6358 */
6359
6360 if (cu->language == language_go)
6361 {
6362 /* This is a lie, but we already lie to the caller new_symbol_full.
6363 new_symbol_full assumes we return the mangled name.
6364 This just undoes that lie until things are cleaned up. */
6365 demangled = NULL;
6366 }
6367 else
6368 {
6369 demangled = cplus_demangle (mangled,
6370 (DMGL_PARAMS | DMGL_ANSI
6371 | (cu->language == language_java
6372 ? DMGL_JAVA | DMGL_RET_POSTFIX
6373 : DMGL_RET_DROP)));
6374 }
6375 if (demangled)
6376 {
6377 make_cleanup (xfree, demangled);
6378 canon = demangled;
6379 }
6380 else
6381 {
6382 canon = mangled;
6383 need_copy = 0;
6384 }
6385 }
6386
6387 if (canon == NULL || check_physname)
6388 {
6389 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6390
6391 if (canon != NULL && strcmp (physname, canon) != 0)
6392 {
6393 /* It may not mean a bug in GDB. The compiler could also
6394 compute DW_AT_linkage_name incorrectly. But in such case
6395 GDB would need to be bug-to-bug compatible. */
6396
6397 complaint (&symfile_complaints,
6398 _("Computed physname <%s> does not match demangled <%s> "
6399 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6400 physname, canon, mangled, die->offset.sect_off, objfile->name);
6401
6402 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6403 is available here - over computed PHYSNAME. It is safer
6404 against both buggy GDB and buggy compilers. */
6405
6406 retval = canon;
6407 }
6408 else
6409 {
6410 retval = physname;
6411 need_copy = 0;
6412 }
6413 }
6414 else
6415 retval = canon;
6416
6417 if (need_copy)
6418 retval = obsavestring (retval, strlen (retval),
6419 &objfile->objfile_obstack);
6420
6421 do_cleanups (back_to);
6422 return retval;
6423 }
6424
6425 /* Read the import statement specified by the given die and record it. */
6426
6427 static void
6428 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6429 {
6430 struct objfile *objfile = cu->objfile;
6431 struct attribute *import_attr;
6432 struct die_info *imported_die, *child_die;
6433 struct dwarf2_cu *imported_cu;
6434 const char *imported_name;
6435 const char *imported_name_prefix;
6436 const char *canonical_name;
6437 const char *import_alias;
6438 const char *imported_declaration = NULL;
6439 const char *import_prefix;
6440 VEC (const_char_ptr) *excludes = NULL;
6441 struct cleanup *cleanups;
6442
6443 char *temp;
6444
6445 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6446 if (import_attr == NULL)
6447 {
6448 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6449 dwarf_tag_name (die->tag));
6450 return;
6451 }
6452
6453 imported_cu = cu;
6454 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6455 imported_name = dwarf2_name (imported_die, imported_cu);
6456 if (imported_name == NULL)
6457 {
6458 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6459
6460 The import in the following code:
6461 namespace A
6462 {
6463 typedef int B;
6464 }
6465
6466 int main ()
6467 {
6468 using A::B;
6469 B b;
6470 return b;
6471 }
6472
6473 ...
6474 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6475 <52> DW_AT_decl_file : 1
6476 <53> DW_AT_decl_line : 6
6477 <54> DW_AT_import : <0x75>
6478 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6479 <59> DW_AT_name : B
6480 <5b> DW_AT_decl_file : 1
6481 <5c> DW_AT_decl_line : 2
6482 <5d> DW_AT_type : <0x6e>
6483 ...
6484 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6485 <76> DW_AT_byte_size : 4
6486 <77> DW_AT_encoding : 5 (signed)
6487
6488 imports the wrong die ( 0x75 instead of 0x58 ).
6489 This case will be ignored until the gcc bug is fixed. */
6490 return;
6491 }
6492
6493 /* Figure out the local name after import. */
6494 import_alias = dwarf2_name (die, cu);
6495
6496 /* Figure out where the statement is being imported to. */
6497 import_prefix = determine_prefix (die, cu);
6498
6499 /* Figure out what the scope of the imported die is and prepend it
6500 to the name of the imported die. */
6501 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6502
6503 if (imported_die->tag != DW_TAG_namespace
6504 && imported_die->tag != DW_TAG_module)
6505 {
6506 imported_declaration = imported_name;
6507 canonical_name = imported_name_prefix;
6508 }
6509 else if (strlen (imported_name_prefix) > 0)
6510 {
6511 temp = alloca (strlen (imported_name_prefix)
6512 + 2 + strlen (imported_name) + 1);
6513 strcpy (temp, imported_name_prefix);
6514 strcat (temp, "::");
6515 strcat (temp, imported_name);
6516 canonical_name = temp;
6517 }
6518 else
6519 canonical_name = imported_name;
6520
6521 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6522
6523 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6524 for (child_die = die->child; child_die && child_die->tag;
6525 child_die = sibling_die (child_die))
6526 {
6527 /* DWARF-4: A Fortran use statement with a “rename list” may be
6528 represented by an imported module entry with an import attribute
6529 referring to the module and owned entries corresponding to those
6530 entities that are renamed as part of being imported. */
6531
6532 if (child_die->tag != DW_TAG_imported_declaration)
6533 {
6534 complaint (&symfile_complaints,
6535 _("child DW_TAG_imported_declaration expected "
6536 "- DIE at 0x%x [in module %s]"),
6537 child_die->offset.sect_off, objfile->name);
6538 continue;
6539 }
6540
6541 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6542 if (import_attr == NULL)
6543 {
6544 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6545 dwarf_tag_name (child_die->tag));
6546 continue;
6547 }
6548
6549 imported_cu = cu;
6550 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6551 &imported_cu);
6552 imported_name = dwarf2_name (imported_die, imported_cu);
6553 if (imported_name == NULL)
6554 {
6555 complaint (&symfile_complaints,
6556 _("child DW_TAG_imported_declaration has unknown "
6557 "imported name - DIE at 0x%x [in module %s]"),
6558 child_die->offset.sect_off, objfile->name);
6559 continue;
6560 }
6561
6562 VEC_safe_push (const_char_ptr, excludes, imported_name);
6563
6564 process_die (child_die, cu);
6565 }
6566
6567 cp_add_using_directive (import_prefix,
6568 canonical_name,
6569 import_alias,
6570 imported_declaration,
6571 excludes,
6572 &objfile->objfile_obstack);
6573
6574 do_cleanups (cleanups);
6575 }
6576
6577 /* Cleanup function for read_file_scope. */
6578
6579 static void
6580 free_cu_line_header (void *arg)
6581 {
6582 struct dwarf2_cu *cu = arg;
6583
6584 free_line_header (cu->line_header);
6585 cu->line_header = NULL;
6586 }
6587
6588 static void
6589 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6590 char **name, char **comp_dir)
6591 {
6592 struct attribute *attr;
6593
6594 *name = NULL;
6595 *comp_dir = NULL;
6596
6597 /* Find the filename. Do not use dwarf2_name here, since the filename
6598 is not a source language identifier. */
6599 attr = dwarf2_attr (die, DW_AT_name, cu);
6600 if (attr)
6601 {
6602 *name = DW_STRING (attr);
6603 }
6604
6605 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6606 if (attr)
6607 *comp_dir = DW_STRING (attr);
6608 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6609 {
6610 *comp_dir = ldirname (*name);
6611 if (*comp_dir != NULL)
6612 make_cleanup (xfree, *comp_dir);
6613 }
6614 if (*comp_dir != NULL)
6615 {
6616 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6617 directory, get rid of it. */
6618 char *cp = strchr (*comp_dir, ':');
6619
6620 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6621 *comp_dir = cp + 1;
6622 }
6623
6624 if (*name == NULL)
6625 *name = "<unknown>";
6626 }
6627
6628 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6629 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6630 COMP_DIR is the compilation directory.
6631 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6632
6633 static void
6634 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6635 const char *comp_dir, int want_line_info)
6636 {
6637 struct attribute *attr;
6638
6639 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6640 if (attr)
6641 {
6642 unsigned int line_offset = DW_UNSND (attr);
6643 struct line_header *line_header
6644 = dwarf_decode_line_header (line_offset, cu);
6645
6646 if (line_header)
6647 {
6648 cu->line_header = line_header;
6649 make_cleanup (free_cu_line_header, cu);
6650 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6651 }
6652 }
6653 }
6654
6655 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6656
6657 static void
6658 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6659 {
6660 struct objfile *objfile = dwarf2_per_objfile->objfile;
6661 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6662 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6663 CORE_ADDR highpc = ((CORE_ADDR) 0);
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 CORE_ADDR baseaddr;
6670
6671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6672
6673 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6674
6675 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6676 from finish_block. */
6677 if (lowpc == ((CORE_ADDR) -1))
6678 lowpc = highpc;
6679 lowpc += baseaddr;
6680 highpc += baseaddr;
6681
6682 find_file_and_directory (die, cu, &name, &comp_dir);
6683
6684 prepare_one_comp_unit (cu, die, cu->language);
6685
6686 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6687 standardised yet. As a workaround for the language detection we fall
6688 back to the DW_AT_producer string. */
6689 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6690 cu->language = language_opencl;
6691
6692 /* Similar hack for Go. */
6693 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6694 set_cu_language (DW_LANG_Go, cu);
6695
6696 /* We assume that we're processing GCC output. */
6697 processing_gcc_compilation = 2;
6698
6699 processing_has_namespace_info = 0;
6700
6701 start_symtab (name, comp_dir, lowpc);
6702 record_debugformat ("DWARF 2");
6703 record_producer (cu->producer);
6704
6705 /* Decode line number information if present. We do this before
6706 processing child DIEs, so that the line header table is available
6707 for DW_AT_decl_file. */
6708 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6709
6710 /* Process all dies in compilation unit. */
6711 if (die->child != NULL)
6712 {
6713 child_die = die->child;
6714 while (child_die && child_die->tag)
6715 {
6716 process_die (child_die, cu);
6717 child_die = sibling_die (child_die);
6718 }
6719 }
6720
6721 /* Decode macro information, if present. Dwarf 2 macro information
6722 refers to information in the line number info statement program
6723 header, so we can only read it if we've read the header
6724 successfully. */
6725 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6726 if (attr && cu->line_header)
6727 {
6728 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6729 complaint (&symfile_complaints,
6730 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6731
6732 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6733 comp_dir, abfd, cu,
6734 &dwarf2_per_objfile->macro, 1,
6735 ".debug_macro");
6736 }
6737 else
6738 {
6739 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6740 if (attr && cu->line_header)
6741 {
6742 unsigned int macro_offset = DW_UNSND (attr);
6743
6744 dwarf_decode_macros (cu->line_header, macro_offset,
6745 comp_dir, abfd, cu,
6746 &dwarf2_per_objfile->macinfo, 0,
6747 ".debug_macinfo");
6748 }
6749 }
6750
6751 do_cleanups (back_to);
6752 }
6753
6754 /* Process DW_TAG_type_unit.
6755 For TUs we want to skip the first top level sibling if it's not the
6756 actual type being defined by this TU. In this case the first top
6757 level sibling is there to provide context only. */
6758
6759 static void
6760 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6761 {
6762 struct objfile *objfile = cu->objfile;
6763 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6764 CORE_ADDR lowpc;
6765 struct attribute *attr;
6766 char *name = NULL;
6767 char *comp_dir = NULL;
6768 struct die_info *child_die;
6769 bfd *abfd = objfile->obfd;
6770
6771 /* start_symtab needs a low pc, but we don't really have one.
6772 Do what read_file_scope would do in the absence of such info. */
6773 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6774
6775 /* Find the filename. Do not use dwarf2_name here, since the filename
6776 is not a source language identifier. */
6777 attr = dwarf2_attr (die, DW_AT_name, cu);
6778 if (attr)
6779 name = DW_STRING (attr);
6780
6781 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6782 if (attr)
6783 comp_dir = DW_STRING (attr);
6784 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6785 {
6786 comp_dir = ldirname (name);
6787 if (comp_dir != NULL)
6788 make_cleanup (xfree, comp_dir);
6789 }
6790
6791 if (name == NULL)
6792 name = "<unknown>";
6793
6794 prepare_one_comp_unit (cu, die, language_minimal);
6795
6796 /* We assume that we're processing GCC output. */
6797 processing_gcc_compilation = 2;
6798
6799 processing_has_namespace_info = 0;
6800
6801 start_symtab (name, comp_dir, lowpc);
6802 record_debugformat ("DWARF 2");
6803 record_producer (cu->producer);
6804
6805 /* Decode line number information if present. We do this before
6806 processing child DIEs, so that the line header table is available
6807 for DW_AT_decl_file.
6808 We don't need the pc/line-number mapping for type units. */
6809 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6810
6811 /* Process the dies in the type unit. */
6812 if (die->child == NULL)
6813 {
6814 dump_die_for_error (die);
6815 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6816 bfd_get_filename (abfd));
6817 }
6818
6819 child_die = die->child;
6820
6821 while (child_die && child_die->tag)
6822 {
6823 process_die (child_die, cu);
6824
6825 child_die = sibling_die (child_die);
6826 }
6827
6828 do_cleanups (back_to);
6829 }
6830 \f
6831 /* DWO files. */
6832
6833 static hashval_t
6834 hash_dwo_file (const void *item)
6835 {
6836 const struct dwo_file *dwo_file = item;
6837
6838 return htab_hash_string (dwo_file->dwo_name);
6839 }
6840
6841 static int
6842 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6843 {
6844 const struct dwo_file *lhs = item_lhs;
6845 const struct dwo_file *rhs = item_rhs;
6846
6847 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6848 }
6849
6850 /* Allocate a hash table for DWO files. */
6851
6852 static htab_t
6853 allocate_dwo_file_hash_table (void)
6854 {
6855 struct objfile *objfile = dwarf2_per_objfile->objfile;
6856
6857 return htab_create_alloc_ex (41,
6858 hash_dwo_file,
6859 eq_dwo_file,
6860 NULL,
6861 &objfile->objfile_obstack,
6862 hashtab_obstack_allocate,
6863 dummy_obstack_deallocate);
6864 }
6865
6866 static hashval_t
6867 hash_dwo_unit (const void *item)
6868 {
6869 const struct dwo_unit *dwo_unit = item;
6870
6871 /* This drops the top 32 bits of the id, but is ok for a hash. */
6872 return dwo_unit->signature;
6873 }
6874
6875 static int
6876 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6877 {
6878 const struct dwo_unit *lhs = item_lhs;
6879 const struct dwo_unit *rhs = item_rhs;
6880
6881 /* The signature is assumed to be unique within the DWO file.
6882 So while object file CU dwo_id's always have the value zero,
6883 that's OK, assuming each object file DWO file has only one CU,
6884 and that's the rule for now. */
6885 return lhs->signature == rhs->signature;
6886 }
6887
6888 /* Allocate a hash table for DWO CUs,TUs.
6889 There is one of these tables for each of CUs,TUs for each DWO file. */
6890
6891 static htab_t
6892 allocate_dwo_unit_table (struct objfile *objfile)
6893 {
6894 /* Start out with a pretty small number.
6895 Generally DWO files contain only one CU and maybe some TUs. */
6896 return htab_create_alloc_ex (3,
6897 hash_dwo_unit,
6898 eq_dwo_unit,
6899 NULL,
6900 &objfile->objfile_obstack,
6901 hashtab_obstack_allocate,
6902 dummy_obstack_deallocate);
6903 }
6904
6905 /* This function is mapped across the sections and remembers the offset and
6906 size of each of the DWO debugging sections we are interested in. */
6907
6908 static void
6909 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6910 {
6911 struct dwo_file *dwo_file = dwo_file_ptr;
6912 const struct dwo_section_names *names = &dwo_section_names;
6913
6914 if (section_is_p (sectp->name, &names->abbrev_dwo))
6915 {
6916 dwo_file->sections.abbrev.asection = sectp;
6917 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6918 }
6919 else if (section_is_p (sectp->name, &names->info_dwo))
6920 {
6921 dwo_file->sections.info.asection = sectp;
6922 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6923 }
6924 else if (section_is_p (sectp->name, &names->line_dwo))
6925 {
6926 dwo_file->sections.line.asection = sectp;
6927 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6928 }
6929 else if (section_is_p (sectp->name, &names->loc_dwo))
6930 {
6931 dwo_file->sections.loc.asection = sectp;
6932 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6933 }
6934 else if (section_is_p (sectp->name, &names->str_dwo))
6935 {
6936 dwo_file->sections.str.asection = sectp;
6937 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6938 }
6939 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6940 {
6941 dwo_file->sections.str_offsets.asection = sectp;
6942 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6943 }
6944 else if (section_is_p (sectp->name, &names->types_dwo))
6945 {
6946 struct dwarf2_section_info type_section;
6947
6948 memset (&type_section, 0, sizeof (type_section));
6949 type_section.asection = sectp;
6950 type_section.size = bfd_get_section_size (sectp);
6951 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6952 &type_section);
6953 }
6954 }
6955
6956 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6957
6958 struct create_dwo_info_table_data
6959 {
6960 struct dwo_file *dwo_file;
6961 htab_t cu_htab;
6962 };
6963
6964 /* die_reader_func for create_debug_info_hash_table. */
6965
6966 static void
6967 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6968 gdb_byte *info_ptr,
6969 struct die_info *comp_unit_die,
6970 int has_children,
6971 void *datap)
6972 {
6973 struct dwarf2_cu *cu = reader->cu;
6974 struct objfile *objfile = dwarf2_per_objfile->objfile;
6975 sect_offset offset = cu->per_cu->offset;
6976 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6977 struct create_dwo_info_table_data *data = datap;
6978 struct dwo_file *dwo_file = data->dwo_file;
6979 htab_t cu_htab = data->cu_htab;
6980 void **slot;
6981 struct attribute *attr;
6982 struct dwo_unit *dwo_unit;
6983
6984 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6985 if (attr == NULL)
6986 {
6987 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6988 " its dwo_id [in module %s]"),
6989 offset.sect_off, dwo_file->dwo_name);
6990 return;
6991 }
6992
6993 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6994 dwo_unit->dwo_file = dwo_file;
6995 dwo_unit->signature = DW_UNSND (attr);
6996 dwo_unit->info_or_types_section = section;
6997 dwo_unit->offset = offset;
6998 dwo_unit->length = cu->per_cu->length;
6999
7000 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7001 gdb_assert (slot != NULL);
7002 if (*slot != NULL)
7003 {
7004 const struct dwo_unit *dup_dwo_unit = *slot;
7005
7006 complaint (&symfile_complaints,
7007 _("debug entry at offset 0x%x is duplicate to the entry at"
7008 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7009 offset.sect_off, dup_dwo_unit->offset.sect_off,
7010 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7011 dwo_file->dwo_name);
7012 }
7013 else
7014 *slot = dwo_unit;
7015
7016 if (dwarf2_die_debug)
7017 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7018 offset.sect_off,
7019 phex (dwo_unit->signature,
7020 sizeof (dwo_unit->signature)));
7021 }
7022
7023 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7024
7025 static htab_t
7026 create_debug_info_hash_table (struct dwo_file *dwo_file)
7027 {
7028 struct objfile *objfile = dwarf2_per_objfile->objfile;
7029 struct dwarf2_section_info *section = &dwo_file->sections.info;
7030 bfd *abfd;
7031 htab_t cu_htab;
7032 gdb_byte *info_ptr, *end_ptr;
7033 struct create_dwo_info_table_data create_dwo_info_table_data;
7034
7035 dwarf2_read_section (objfile, section);
7036 info_ptr = section->buffer;
7037
7038 if (info_ptr == NULL)
7039 return NULL;
7040
7041 /* We can't set abfd until now because the section may be empty or
7042 not present, in which case section->asection will be NULL. */
7043 abfd = section->asection->owner;
7044
7045 if (dwarf2_die_debug)
7046 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7047 bfd_get_filename (abfd));
7048
7049 cu_htab = allocate_dwo_unit_table (objfile);
7050
7051 create_dwo_info_table_data.dwo_file = dwo_file;
7052 create_dwo_info_table_data.cu_htab = cu_htab;
7053
7054 end_ptr = info_ptr + section->size;
7055 while (info_ptr < end_ptr)
7056 {
7057 struct dwarf2_per_cu_data per_cu;
7058
7059 memset (&per_cu, 0, sizeof (per_cu));
7060 per_cu.objfile = objfile;
7061 per_cu.is_debug_types = 0;
7062 per_cu.offset.sect_off = info_ptr - section->buffer;
7063 per_cu.info_or_types_section = section;
7064
7065 init_cutu_and_read_dies_no_follow (&per_cu,
7066 &dwo_file->sections.abbrev,
7067 dwo_file,
7068 create_debug_info_hash_table_reader,
7069 &create_dwo_info_table_data);
7070
7071 info_ptr += per_cu.length;
7072 }
7073
7074 return cu_htab;
7075 }
7076
7077 /* Subroutine of open_dwo_file to simplify it.
7078 Open the file specified by FILE_NAME and hand it off to BFD for
7079 preliminary analysis. Return a newly initialized bfd *, which
7080 includes a canonicalized copy of FILE_NAME.
7081 In case of trouble, return NULL.
7082 NOTE: This function is derived from symfile_bfd_open. */
7083
7084 static bfd *
7085 try_open_dwo_file (const char *file_name)
7086 {
7087 bfd *sym_bfd;
7088 int desc;
7089 char *absolute_name;
7090
7091 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7092 O_RDONLY | O_BINARY, &absolute_name);
7093 if (desc < 0)
7094 return NULL;
7095
7096 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7097 if (!sym_bfd)
7098 {
7099 xfree (absolute_name);
7100 return NULL;
7101 }
7102 bfd_set_cacheable (sym_bfd, 1);
7103
7104 if (!bfd_check_format (sym_bfd, bfd_object))
7105 {
7106 bfd_close (sym_bfd); /* This also closes desc. */
7107 xfree (absolute_name);
7108 return NULL;
7109 }
7110
7111 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7112 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7113
7114 return sym_bfd;
7115 }
7116
7117 /* Try to open DWO file DWO_NAME.
7118 COMP_DIR is the DW_AT_comp_dir attribute.
7119 The result is the bfd handle of the file.
7120 If there is a problem finding or opening the file, return NULL.
7121 Upon success, the canonicalized path of the file is stored in the bfd,
7122 same as symfile_bfd_open. */
7123
7124 static bfd *
7125 open_dwo_file (const char *dwo_name, const char *comp_dir)
7126 {
7127 bfd *abfd;
7128
7129 if (IS_ABSOLUTE_PATH (dwo_name))
7130 return try_open_dwo_file (dwo_name);
7131
7132 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7133
7134 if (comp_dir != NULL)
7135 {
7136 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7137
7138 /* NOTE: If comp_dir is a relative path, this will also try the
7139 search path, which seems useful. */
7140 abfd = try_open_dwo_file (path_to_try);
7141 xfree (path_to_try);
7142 if (abfd != NULL)
7143 return abfd;
7144 }
7145
7146 /* That didn't work, try debug-file-directory, which, despite its name,
7147 is a list of paths. */
7148
7149 if (*debug_file_directory == '\0')
7150 return NULL;
7151
7152 return try_open_dwo_file (dwo_name);
7153 }
7154
7155 /* Initialize the use of the DWO file specified by DWO_NAME. */
7156
7157 static struct dwo_file *
7158 init_dwo_file (const char *dwo_name, const char *comp_dir)
7159 {
7160 struct objfile *objfile = dwarf2_per_objfile->objfile;
7161 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7162 struct dwo_file);
7163 bfd *abfd;
7164 struct cleanup *cleanups;
7165
7166 if (dwarf2_die_debug)
7167 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7168
7169 abfd = open_dwo_file (dwo_name, comp_dir);
7170 if (abfd == NULL)
7171 return NULL;
7172 dwo_file->dwo_name = dwo_name;
7173 dwo_file->dwo_bfd = abfd;
7174
7175 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7176
7177 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7178
7179 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7180
7181 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7182 dwo_file->sections.types);
7183
7184 discard_cleanups (cleanups);
7185
7186 return dwo_file;
7187 }
7188
7189 /* Lookup DWO file DWO_NAME. */
7190
7191 static struct dwo_file *
7192 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7193 {
7194 struct dwo_file *dwo_file;
7195 struct dwo_file find_entry;
7196 void **slot;
7197
7198 if (dwarf2_per_objfile->dwo_files == NULL)
7199 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7200
7201 /* Have we already seen this DWO file? */
7202 find_entry.dwo_name = dwo_name;
7203 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7204
7205 /* If not, read it in and build a table of the DWOs it contains. */
7206 if (*slot == NULL)
7207 *slot = init_dwo_file (dwo_name, comp_dir);
7208
7209 /* NOTE: This will be NULL if unable to open the file. */
7210 dwo_file = *slot;
7211
7212 return dwo_file;
7213 }
7214
7215 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7216 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7217 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7218 nomenclature as TUs).
7219 The result is the DWO CU or NULL if we didn't find it
7220 (dwo_id mismatch or couldn't find the DWO file). */
7221
7222 static struct dwo_unit *
7223 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7224 char *dwo_name, const char *comp_dir,
7225 ULONGEST signature)
7226 {
7227 struct objfile *objfile = dwarf2_per_objfile->objfile;
7228 struct dwo_file *dwo_file;
7229
7230 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7231 if (dwo_file == NULL)
7232 return NULL;
7233
7234 /* Look up the DWO using its signature(dwo_id). */
7235
7236 if (dwo_file->cus != NULL)
7237 {
7238 struct dwo_unit find_dwo_cu, *dwo_cu;
7239
7240 find_dwo_cu.signature = signature;
7241 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7242
7243 if (dwo_cu != NULL)
7244 return dwo_cu;
7245 }
7246
7247 /* We didn't find it. This must mean a dwo_id mismatch. */
7248
7249 complaint (&symfile_complaints,
7250 _("Could not find DWO CU referenced by CU at offset 0x%x"
7251 " [in module %s]"),
7252 this_cu->offset.sect_off, objfile->name);
7253 return NULL;
7254 }
7255
7256 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7257 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7258 The result is the DWO CU or NULL if we didn't find it
7259 (dwo_id mismatch or couldn't find the DWO file). */
7260
7261 static struct dwo_unit *
7262 lookup_dwo_type_unit (struct signatured_type *this_tu,
7263 char *dwo_name, const char *comp_dir)
7264 {
7265 struct objfile *objfile = dwarf2_per_objfile->objfile;
7266 struct dwo_file *dwo_file;
7267
7268 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7269 if (dwo_file == NULL)
7270 return NULL;
7271
7272 /* Look up the DWO using its signature(dwo_id). */
7273
7274 if (dwo_file->tus != NULL)
7275 {
7276 struct dwo_unit find_dwo_tu, *dwo_tu;
7277
7278 find_dwo_tu.signature = this_tu->signature;
7279 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7280
7281 if (dwo_tu != NULL)
7282 return dwo_tu;
7283 }
7284
7285 /* We didn't find it. This must mean a dwo_id mismatch. */
7286
7287 complaint (&symfile_complaints,
7288 _("Could not find DWO TU referenced by TU at offset 0x%x"
7289 " [in module %s]"),
7290 this_tu->per_cu.offset.sect_off, objfile->name);
7291 return NULL;
7292 }
7293
7294 /* Free all resources associated with DWO_FILE.
7295 Close the DWO file and munmap the sections.
7296 All memory should be on the objfile obstack. */
7297
7298 static void
7299 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7300 {
7301 int ix;
7302 struct dwarf2_section_info *section;
7303
7304 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7305 bfd_close (dwo_file->dwo_bfd);
7306
7307 munmap_section_buffer (&dwo_file->sections.abbrev);
7308 munmap_section_buffer (&dwo_file->sections.info);
7309 munmap_section_buffer (&dwo_file->sections.line);
7310 munmap_section_buffer (&dwo_file->sections.loc);
7311 munmap_section_buffer (&dwo_file->sections.str);
7312 munmap_section_buffer (&dwo_file->sections.str_offsets);
7313
7314 for (ix = 0;
7315 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7316 ix, section);
7317 ++ix)
7318 munmap_section_buffer (section);
7319
7320 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7321 }
7322
7323 /* Wrapper for free_dwo_file for use in cleanups. */
7324
7325 static void
7326 free_dwo_file_cleanup (void *arg)
7327 {
7328 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7329 struct objfile *objfile = dwarf2_per_objfile->objfile;
7330
7331 free_dwo_file (dwo_file, objfile);
7332 }
7333
7334 /* Traversal function for free_dwo_files. */
7335
7336 static int
7337 free_dwo_file_from_slot (void **slot, void *info)
7338 {
7339 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7340 struct objfile *objfile = (struct objfile *) info;
7341
7342 free_dwo_file (dwo_file, objfile);
7343
7344 return 1;
7345 }
7346
7347 /* Free all resources associated with DWO_FILES. */
7348
7349 static void
7350 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7351 {
7352 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7353 }
7354 \f
7355 /* Read in various DIEs. */
7356
7357 /* qsort helper for inherit_abstract_dies. */
7358
7359 static int
7360 unsigned_int_compar (const void *ap, const void *bp)
7361 {
7362 unsigned int a = *(unsigned int *) ap;
7363 unsigned int b = *(unsigned int *) bp;
7364
7365 return (a > b) - (b > a);
7366 }
7367
7368 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7369 Inherit only the children of the DW_AT_abstract_origin DIE not being
7370 already referenced by DW_AT_abstract_origin from the children of the
7371 current DIE. */
7372
7373 static void
7374 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7375 {
7376 struct die_info *child_die;
7377 unsigned die_children_count;
7378 /* CU offsets which were referenced by children of the current DIE. */
7379 sect_offset *offsets;
7380 sect_offset *offsets_end, *offsetp;
7381 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7382 struct die_info *origin_die;
7383 /* Iterator of the ORIGIN_DIE children. */
7384 struct die_info *origin_child_die;
7385 struct cleanup *cleanups;
7386 struct attribute *attr;
7387 struct dwarf2_cu *origin_cu;
7388 struct pending **origin_previous_list_in_scope;
7389
7390 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7391 if (!attr)
7392 return;
7393
7394 /* Note that following die references may follow to a die in a
7395 different cu. */
7396
7397 origin_cu = cu;
7398 origin_die = follow_die_ref (die, attr, &origin_cu);
7399
7400 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7401 symbols in. */
7402 origin_previous_list_in_scope = origin_cu->list_in_scope;
7403 origin_cu->list_in_scope = cu->list_in_scope;
7404
7405 if (die->tag != origin_die->tag
7406 && !(die->tag == DW_TAG_inlined_subroutine
7407 && origin_die->tag == DW_TAG_subprogram))
7408 complaint (&symfile_complaints,
7409 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7410 die->offset.sect_off, origin_die->offset.sect_off);
7411
7412 child_die = die->child;
7413 die_children_count = 0;
7414 while (child_die && child_die->tag)
7415 {
7416 child_die = sibling_die (child_die);
7417 die_children_count++;
7418 }
7419 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7420 cleanups = make_cleanup (xfree, offsets);
7421
7422 offsets_end = offsets;
7423 child_die = die->child;
7424 while (child_die && child_die->tag)
7425 {
7426 /* For each CHILD_DIE, find the corresponding child of
7427 ORIGIN_DIE. If there is more than one layer of
7428 DW_AT_abstract_origin, follow them all; there shouldn't be,
7429 but GCC versions at least through 4.4 generate this (GCC PR
7430 40573). */
7431 struct die_info *child_origin_die = child_die;
7432 struct dwarf2_cu *child_origin_cu = cu;
7433
7434 while (1)
7435 {
7436 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7437 child_origin_cu);
7438 if (attr == NULL)
7439 break;
7440 child_origin_die = follow_die_ref (child_origin_die, attr,
7441 &child_origin_cu);
7442 }
7443
7444 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7445 counterpart may exist. */
7446 if (child_origin_die != child_die)
7447 {
7448 if (child_die->tag != child_origin_die->tag
7449 && !(child_die->tag == DW_TAG_inlined_subroutine
7450 && child_origin_die->tag == DW_TAG_subprogram))
7451 complaint (&symfile_complaints,
7452 _("Child DIE 0x%x and its abstract origin 0x%x have "
7453 "different tags"), child_die->offset.sect_off,
7454 child_origin_die->offset.sect_off);
7455 if (child_origin_die->parent != origin_die)
7456 complaint (&symfile_complaints,
7457 _("Child DIE 0x%x and its abstract origin 0x%x have "
7458 "different parents"), child_die->offset.sect_off,
7459 child_origin_die->offset.sect_off);
7460 else
7461 *offsets_end++ = child_origin_die->offset;
7462 }
7463 child_die = sibling_die (child_die);
7464 }
7465 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7466 unsigned_int_compar);
7467 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7468 if (offsetp[-1].sect_off == offsetp->sect_off)
7469 complaint (&symfile_complaints,
7470 _("Multiple children of DIE 0x%x refer "
7471 "to DIE 0x%x as their abstract origin"),
7472 die->offset.sect_off, offsetp->sect_off);
7473
7474 offsetp = offsets;
7475 origin_child_die = origin_die->child;
7476 while (origin_child_die && origin_child_die->tag)
7477 {
7478 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7479 while (offsetp < offsets_end
7480 && offsetp->sect_off < origin_child_die->offset.sect_off)
7481 offsetp++;
7482 if (offsetp >= offsets_end
7483 || offsetp->sect_off > origin_child_die->offset.sect_off)
7484 {
7485 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7486 process_die (origin_child_die, origin_cu);
7487 }
7488 origin_child_die = sibling_die (origin_child_die);
7489 }
7490 origin_cu->list_in_scope = origin_previous_list_in_scope;
7491
7492 do_cleanups (cleanups);
7493 }
7494
7495 static void
7496 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7497 {
7498 struct objfile *objfile = cu->objfile;
7499 struct context_stack *new;
7500 CORE_ADDR lowpc;
7501 CORE_ADDR highpc;
7502 struct die_info *child_die;
7503 struct attribute *attr, *call_line, *call_file;
7504 char *name;
7505 CORE_ADDR baseaddr;
7506 struct block *block;
7507 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7508 VEC (symbolp) *template_args = NULL;
7509 struct template_symbol *templ_func = NULL;
7510
7511 if (inlined_func)
7512 {
7513 /* If we do not have call site information, we can't show the
7514 caller of this inlined function. That's too confusing, so
7515 only use the scope for local variables. */
7516 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7517 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7518 if (call_line == NULL || call_file == NULL)
7519 {
7520 read_lexical_block_scope (die, cu);
7521 return;
7522 }
7523 }
7524
7525 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7526
7527 name = dwarf2_name (die, cu);
7528
7529 /* Ignore functions with missing or empty names. These are actually
7530 illegal according to the DWARF standard. */
7531 if (name == NULL)
7532 {
7533 complaint (&symfile_complaints,
7534 _("missing name for subprogram DIE at %d"),
7535 die->offset.sect_off);
7536 return;
7537 }
7538
7539 /* Ignore functions with missing or invalid low and high pc attributes. */
7540 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7541 {
7542 attr = dwarf2_attr (die, DW_AT_external, cu);
7543 if (!attr || !DW_UNSND (attr))
7544 complaint (&symfile_complaints,
7545 _("cannot get low and high bounds "
7546 "for subprogram DIE at %d"),
7547 die->offset.sect_off);
7548 return;
7549 }
7550
7551 lowpc += baseaddr;
7552 highpc += baseaddr;
7553
7554 /* If we have any template arguments, then we must allocate a
7555 different sort of symbol. */
7556 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7557 {
7558 if (child_die->tag == DW_TAG_template_type_param
7559 || child_die->tag == DW_TAG_template_value_param)
7560 {
7561 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7562 struct template_symbol);
7563 templ_func->base.is_cplus_template_function = 1;
7564 break;
7565 }
7566 }
7567
7568 new = push_context (0, lowpc);
7569 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7570 (struct symbol *) templ_func);
7571
7572 /* If there is a location expression for DW_AT_frame_base, record
7573 it. */
7574 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7575 if (attr)
7576 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7577 expression is being recorded directly in the function's symbol
7578 and not in a separate frame-base object. I guess this hack is
7579 to avoid adding some sort of frame-base adjunct/annex to the
7580 function's symbol :-(. The problem with doing this is that it
7581 results in a function symbol with a location expression that
7582 has nothing to do with the location of the function, ouch! The
7583 relationship should be: a function's symbol has-a frame base; a
7584 frame-base has-a location expression. */
7585 dwarf2_symbol_mark_computed (attr, new->name, cu);
7586
7587 cu->list_in_scope = &local_symbols;
7588
7589 if (die->child != NULL)
7590 {
7591 child_die = die->child;
7592 while (child_die && child_die->tag)
7593 {
7594 if (child_die->tag == DW_TAG_template_type_param
7595 || child_die->tag == DW_TAG_template_value_param)
7596 {
7597 struct symbol *arg = new_symbol (child_die, NULL, cu);
7598
7599 if (arg != NULL)
7600 VEC_safe_push (symbolp, template_args, arg);
7601 }
7602 else
7603 process_die (child_die, cu);
7604 child_die = sibling_die (child_die);
7605 }
7606 }
7607
7608 inherit_abstract_dies (die, cu);
7609
7610 /* If we have a DW_AT_specification, we might need to import using
7611 directives from the context of the specification DIE. See the
7612 comment in determine_prefix. */
7613 if (cu->language == language_cplus
7614 && dwarf2_attr (die, DW_AT_specification, cu))
7615 {
7616 struct dwarf2_cu *spec_cu = cu;
7617 struct die_info *spec_die = die_specification (die, &spec_cu);
7618
7619 while (spec_die)
7620 {
7621 child_die = spec_die->child;
7622 while (child_die && child_die->tag)
7623 {
7624 if (child_die->tag == DW_TAG_imported_module)
7625 process_die (child_die, spec_cu);
7626 child_die = sibling_die (child_die);
7627 }
7628
7629 /* In some cases, GCC generates specification DIEs that
7630 themselves contain DW_AT_specification attributes. */
7631 spec_die = die_specification (spec_die, &spec_cu);
7632 }
7633 }
7634
7635 new = pop_context ();
7636 /* Make a block for the local symbols within. */
7637 block = finish_block (new->name, &local_symbols, new->old_blocks,
7638 lowpc, highpc, objfile);
7639
7640 /* For C++, set the block's scope. */
7641 if (cu->language == language_cplus || cu->language == language_fortran)
7642 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7643 determine_prefix (die, cu),
7644 processing_has_namespace_info);
7645
7646 /* If we have address ranges, record them. */
7647 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7648
7649 /* Attach template arguments to function. */
7650 if (! VEC_empty (symbolp, template_args))
7651 {
7652 gdb_assert (templ_func != NULL);
7653
7654 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7655 templ_func->template_arguments
7656 = obstack_alloc (&objfile->objfile_obstack,
7657 (templ_func->n_template_arguments
7658 * sizeof (struct symbol *)));
7659 memcpy (templ_func->template_arguments,
7660 VEC_address (symbolp, template_args),
7661 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7662 VEC_free (symbolp, template_args);
7663 }
7664
7665 /* In C++, we can have functions nested inside functions (e.g., when
7666 a function declares a class that has methods). This means that
7667 when we finish processing a function scope, we may need to go
7668 back to building a containing block's symbol lists. */
7669 local_symbols = new->locals;
7670 param_symbols = new->params;
7671 using_directives = new->using_directives;
7672
7673 /* If we've finished processing a top-level function, subsequent
7674 symbols go in the file symbol list. */
7675 if (outermost_context_p ())
7676 cu->list_in_scope = &file_symbols;
7677 }
7678
7679 /* Process all the DIES contained within a lexical block scope. Start
7680 a new scope, process the dies, and then close the scope. */
7681
7682 static void
7683 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7684 {
7685 struct objfile *objfile = cu->objfile;
7686 struct context_stack *new;
7687 CORE_ADDR lowpc, highpc;
7688 struct die_info *child_die;
7689 CORE_ADDR baseaddr;
7690
7691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7692
7693 /* Ignore blocks with missing or invalid low and high pc attributes. */
7694 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7695 as multiple lexical blocks? Handling children in a sane way would
7696 be nasty. Might be easier to properly extend generic blocks to
7697 describe ranges. */
7698 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7699 return;
7700 lowpc += baseaddr;
7701 highpc += baseaddr;
7702
7703 push_context (0, lowpc);
7704 if (die->child != NULL)
7705 {
7706 child_die = die->child;
7707 while (child_die && child_die->tag)
7708 {
7709 process_die (child_die, cu);
7710 child_die = sibling_die (child_die);
7711 }
7712 }
7713 new = pop_context ();
7714
7715 if (local_symbols != NULL || using_directives != NULL)
7716 {
7717 struct block *block
7718 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7719 highpc, objfile);
7720
7721 /* Note that recording ranges after traversing children, as we
7722 do here, means that recording a parent's ranges entails
7723 walking across all its children's ranges as they appear in
7724 the address map, which is quadratic behavior.
7725
7726 It would be nicer to record the parent's ranges before
7727 traversing its children, simply overriding whatever you find
7728 there. But since we don't even decide whether to create a
7729 block until after we've traversed its children, that's hard
7730 to do. */
7731 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7732 }
7733 local_symbols = new->locals;
7734 using_directives = new->using_directives;
7735 }
7736
7737 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7738
7739 static void
7740 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7741 {
7742 struct objfile *objfile = cu->objfile;
7743 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7744 CORE_ADDR pc, baseaddr;
7745 struct attribute *attr;
7746 struct call_site *call_site, call_site_local;
7747 void **slot;
7748 int nparams;
7749 struct die_info *child_die;
7750
7751 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7752
7753 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7754 if (!attr)
7755 {
7756 complaint (&symfile_complaints,
7757 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7758 "DIE 0x%x [in module %s]"),
7759 die->offset.sect_off, objfile->name);
7760 return;
7761 }
7762 pc = DW_ADDR (attr) + baseaddr;
7763
7764 if (cu->call_site_htab == NULL)
7765 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7766 NULL, &objfile->objfile_obstack,
7767 hashtab_obstack_allocate, NULL);
7768 call_site_local.pc = pc;
7769 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7770 if (*slot != NULL)
7771 {
7772 complaint (&symfile_complaints,
7773 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7774 "DIE 0x%x [in module %s]"),
7775 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7776 return;
7777 }
7778
7779 /* Count parameters at the caller. */
7780
7781 nparams = 0;
7782 for (child_die = die->child; child_die && child_die->tag;
7783 child_die = sibling_die (child_die))
7784 {
7785 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7786 {
7787 complaint (&symfile_complaints,
7788 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7789 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7790 child_die->tag, child_die->offset.sect_off, objfile->name);
7791 continue;
7792 }
7793
7794 nparams++;
7795 }
7796
7797 call_site = obstack_alloc (&objfile->objfile_obstack,
7798 (sizeof (*call_site)
7799 + (sizeof (*call_site->parameter)
7800 * (nparams - 1))));
7801 *slot = call_site;
7802 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7803 call_site->pc = pc;
7804
7805 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7806 {
7807 struct die_info *func_die;
7808
7809 /* Skip also over DW_TAG_inlined_subroutine. */
7810 for (func_die = die->parent;
7811 func_die && func_die->tag != DW_TAG_subprogram
7812 && func_die->tag != DW_TAG_subroutine_type;
7813 func_die = func_die->parent);
7814
7815 /* DW_AT_GNU_all_call_sites is a superset
7816 of DW_AT_GNU_all_tail_call_sites. */
7817 if (func_die
7818 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7819 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7820 {
7821 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7822 not complete. But keep CALL_SITE for look ups via call_site_htab,
7823 both the initial caller containing the real return address PC and
7824 the final callee containing the current PC of a chain of tail
7825 calls do not need to have the tail call list complete. But any
7826 function candidate for a virtual tail call frame searched via
7827 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7828 determined unambiguously. */
7829 }
7830 else
7831 {
7832 struct type *func_type = NULL;
7833
7834 if (func_die)
7835 func_type = get_die_type (func_die, cu);
7836 if (func_type != NULL)
7837 {
7838 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7839
7840 /* Enlist this call site to the function. */
7841 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7842 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7843 }
7844 else
7845 complaint (&symfile_complaints,
7846 _("Cannot find function owning DW_TAG_GNU_call_site "
7847 "DIE 0x%x [in module %s]"),
7848 die->offset.sect_off, objfile->name);
7849 }
7850 }
7851
7852 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7853 if (attr == NULL)
7854 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7855 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7856 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7857 /* Keep NULL DWARF_BLOCK. */;
7858 else if (attr_form_is_block (attr))
7859 {
7860 struct dwarf2_locexpr_baton *dlbaton;
7861
7862 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7863 dlbaton->data = DW_BLOCK (attr)->data;
7864 dlbaton->size = DW_BLOCK (attr)->size;
7865 dlbaton->per_cu = cu->per_cu;
7866
7867 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7868 }
7869 else if (is_ref_attr (attr))
7870 {
7871 struct dwarf2_cu *target_cu = cu;
7872 struct die_info *target_die;
7873
7874 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7875 gdb_assert (target_cu->objfile == objfile);
7876 if (die_is_declaration (target_die, target_cu))
7877 {
7878 const char *target_physname;
7879
7880 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7881 if (target_physname == NULL)
7882 complaint (&symfile_complaints,
7883 _("DW_AT_GNU_call_site_target target DIE has invalid "
7884 "physname, for referencing DIE 0x%x [in module %s]"),
7885 die->offset.sect_off, objfile->name);
7886 else
7887 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7888 }
7889 else
7890 {
7891 CORE_ADDR lowpc;
7892
7893 /* DW_AT_entry_pc should be preferred. */
7894 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7895 complaint (&symfile_complaints,
7896 _("DW_AT_GNU_call_site_target target DIE has invalid "
7897 "low pc, for referencing DIE 0x%x [in module %s]"),
7898 die->offset.sect_off, objfile->name);
7899 else
7900 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7901 }
7902 }
7903 else
7904 complaint (&symfile_complaints,
7905 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7906 "block nor reference, for DIE 0x%x [in module %s]"),
7907 die->offset.sect_off, objfile->name);
7908
7909 call_site->per_cu = cu->per_cu;
7910
7911 for (child_die = die->child;
7912 child_die && child_die->tag;
7913 child_die = sibling_die (child_die))
7914 {
7915 struct call_site_parameter *parameter;
7916 struct attribute *loc, *origin;
7917
7918 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7919 {
7920 /* Already printed the complaint above. */
7921 continue;
7922 }
7923
7924 gdb_assert (call_site->parameter_count < nparams);
7925 parameter = &call_site->parameter[call_site->parameter_count];
7926
7927 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
7928 specifies DW_TAG_formal_parameter. Value of the data assumed for the
7929 register is contained in DW_AT_GNU_call_site_value. */
7930
7931 loc = dwarf2_attr (child_die, DW_AT_location, cu);
7932 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7933 if (loc == NULL && origin != NULL && is_ref_attr (origin))
7934 {
7935 sect_offset offset;
7936
7937 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
7938 offset = dwarf2_get_ref_die_offset (origin);
7939 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
7940 parameter->u.param_offset.cu_off = (offset.sect_off
7941 - cu->header.offset.sect_off);
7942 }
7943 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
7944 {
7945 complaint (&symfile_complaints,
7946 _("No DW_FORM_block* DW_AT_location for "
7947 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7948 child_die->offset.sect_off, objfile->name);
7949 continue;
7950 }
7951 else
7952 {
7953 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
7954 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
7955 if (parameter->u.dwarf_reg != -1)
7956 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
7957 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
7958 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
7959 &parameter->u.fb_offset))
7960 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
7961 else
7962 {
7963 complaint (&symfile_complaints,
7964 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7965 "for DW_FORM_block* DW_AT_location is supported for "
7966 "DW_TAG_GNU_call_site child DIE 0x%x "
7967 "[in module %s]"),
7968 child_die->offset.sect_off, objfile->name);
7969 continue;
7970 }
7971 }
7972
7973 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7974 if (!attr_form_is_block (attr))
7975 {
7976 complaint (&symfile_complaints,
7977 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7978 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7979 child_die->offset.sect_off, objfile->name);
7980 continue;
7981 }
7982 parameter->value = DW_BLOCK (attr)->data;
7983 parameter->value_size = DW_BLOCK (attr)->size;
7984
7985 /* Parameters are not pre-cleared by memset above. */
7986 parameter->data_value = NULL;
7987 parameter->data_value_size = 0;
7988 call_site->parameter_count++;
7989
7990 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7991 if (attr)
7992 {
7993 if (!attr_form_is_block (attr))
7994 complaint (&symfile_complaints,
7995 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7996 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7997 child_die->offset.sect_off, objfile->name);
7998 else
7999 {
8000 parameter->data_value = DW_BLOCK (attr)->data;
8001 parameter->data_value_size = DW_BLOCK (attr)->size;
8002 }
8003 }
8004 }
8005 }
8006
8007 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
8008 Return 1 if the attributes are present and valid, otherwise, return 0.
8009 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
8010
8011 static int
8012 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8013 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8014 struct partial_symtab *ranges_pst)
8015 {
8016 struct objfile *objfile = cu->objfile;
8017 struct comp_unit_head *cu_header = &cu->header;
8018 bfd *obfd = objfile->obfd;
8019 unsigned int addr_size = cu_header->addr_size;
8020 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8021 /* Base address selection entry. */
8022 CORE_ADDR base;
8023 int found_base;
8024 unsigned int dummy;
8025 gdb_byte *buffer;
8026 CORE_ADDR marker;
8027 int low_set;
8028 CORE_ADDR low = 0;
8029 CORE_ADDR high = 0;
8030 CORE_ADDR baseaddr;
8031
8032 found_base = cu->base_known;
8033 base = cu->base_address;
8034
8035 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
8036 if (offset >= dwarf2_per_objfile->ranges.size)
8037 {
8038 complaint (&symfile_complaints,
8039 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8040 offset);
8041 return 0;
8042 }
8043 buffer = dwarf2_per_objfile->ranges.buffer + offset;
8044
8045 /* Read in the largest possible address. */
8046 marker = read_address (obfd, buffer, cu, &dummy);
8047 if ((marker & mask) == mask)
8048 {
8049 /* If we found the largest possible address, then
8050 read the base address. */
8051 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8052 buffer += 2 * addr_size;
8053 offset += 2 * addr_size;
8054 found_base = 1;
8055 }
8056
8057 low_set = 0;
8058
8059 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8060
8061 while (1)
8062 {
8063 CORE_ADDR range_beginning, range_end;
8064
8065 range_beginning = read_address (obfd, buffer, cu, &dummy);
8066 buffer += addr_size;
8067 range_end = read_address (obfd, buffer, cu, &dummy);
8068 buffer += addr_size;
8069 offset += 2 * addr_size;
8070
8071 /* An end of list marker is a pair of zero addresses. */
8072 if (range_beginning == 0 && range_end == 0)
8073 /* Found the end of list entry. */
8074 break;
8075
8076 /* Each base address selection entry is a pair of 2 values.
8077 The first is the largest possible address, the second is
8078 the base address. Check for a base address here. */
8079 if ((range_beginning & mask) == mask)
8080 {
8081 /* If we found the largest possible address, then
8082 read the base address. */
8083 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8084 found_base = 1;
8085 continue;
8086 }
8087
8088 if (!found_base)
8089 {
8090 /* We have no valid base address for the ranges
8091 data. */
8092 complaint (&symfile_complaints,
8093 _("Invalid .debug_ranges data (no base address)"));
8094 return 0;
8095 }
8096
8097 if (range_beginning > range_end)
8098 {
8099 /* Inverted range entries are invalid. */
8100 complaint (&symfile_complaints,
8101 _("Invalid .debug_ranges data (inverted range)"));
8102 return 0;
8103 }
8104
8105 /* Empty range entries have no effect. */
8106 if (range_beginning == range_end)
8107 continue;
8108
8109 range_beginning += base;
8110 range_end += base;
8111
8112 if (ranges_pst != NULL)
8113 addrmap_set_empty (objfile->psymtabs_addrmap,
8114 range_beginning + baseaddr,
8115 range_end - 1 + baseaddr,
8116 ranges_pst);
8117
8118 /* FIXME: This is recording everything as a low-high
8119 segment of consecutive addresses. We should have a
8120 data structure for discontiguous block ranges
8121 instead. */
8122 if (! low_set)
8123 {
8124 low = range_beginning;
8125 high = range_end;
8126 low_set = 1;
8127 }
8128 else
8129 {
8130 if (range_beginning < low)
8131 low = range_beginning;
8132 if (range_end > high)
8133 high = range_end;
8134 }
8135 }
8136
8137 if (! low_set)
8138 /* If the first entry is an end-of-list marker, the range
8139 describes an empty scope, i.e. no instructions. */
8140 return 0;
8141
8142 if (low_return)
8143 *low_return = low;
8144 if (high_return)
8145 *high_return = high;
8146 return 1;
8147 }
8148
8149 /* Get low and high pc attributes from a die. Return 1 if the attributes
8150 are present and valid, otherwise, return 0. Return -1 if the range is
8151 discontinuous, i.e. derived from DW_AT_ranges information. */
8152
8153 static int
8154 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
8155 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8156 struct partial_symtab *pst)
8157 {
8158 struct attribute *attr;
8159 struct attribute *attr_high;
8160 CORE_ADDR low = 0;
8161 CORE_ADDR high = 0;
8162 int ret = 0;
8163
8164 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8165 if (attr_high)
8166 {
8167 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8168 if (attr)
8169 {
8170 low = DW_ADDR (attr);
8171 if (attr_high->form == DW_FORM_addr
8172 || attr_high->form == DW_FORM_GNU_addr_index)
8173 high = DW_ADDR (attr_high);
8174 else
8175 high = low + DW_UNSND (attr_high);
8176 }
8177 else
8178 /* Found high w/o low attribute. */
8179 return 0;
8180
8181 /* Found consecutive range of addresses. */
8182 ret = 1;
8183 }
8184 else
8185 {
8186 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8187 if (attr != NULL)
8188 {
8189 /* Value of the DW_AT_ranges attribute is the offset in the
8190 .debug_ranges section. */
8191 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8192 return 0;
8193 /* Found discontinuous range of addresses. */
8194 ret = -1;
8195 }
8196 }
8197
8198 /* read_partial_die has also the strict LOW < HIGH requirement. */
8199 if (high <= low)
8200 return 0;
8201
8202 /* When using the GNU linker, .gnu.linkonce. sections are used to
8203 eliminate duplicate copies of functions and vtables and such.
8204 The linker will arbitrarily choose one and discard the others.
8205 The AT_*_pc values for such functions refer to local labels in
8206 these sections. If the section from that file was discarded, the
8207 labels are not in the output, so the relocs get a value of 0.
8208 If this is a discarded function, mark the pc bounds as invalid,
8209 so that GDB will ignore it. */
8210 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8211 return 0;
8212
8213 *lowpc = low;
8214 if (highpc)
8215 *highpc = high;
8216 return ret;
8217 }
8218
8219 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8220 its low and high PC addresses. Do nothing if these addresses could not
8221 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8222 and HIGHPC to the high address if greater than HIGHPC. */
8223
8224 static void
8225 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8226 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8227 struct dwarf2_cu *cu)
8228 {
8229 CORE_ADDR low, high;
8230 struct die_info *child = die->child;
8231
8232 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8233 {
8234 *lowpc = min (*lowpc, low);
8235 *highpc = max (*highpc, high);
8236 }
8237
8238 /* If the language does not allow nested subprograms (either inside
8239 subprograms or lexical blocks), we're done. */
8240 if (cu->language != language_ada)
8241 return;
8242
8243 /* Check all the children of the given DIE. If it contains nested
8244 subprograms, then check their pc bounds. Likewise, we need to
8245 check lexical blocks as well, as they may also contain subprogram
8246 definitions. */
8247 while (child && child->tag)
8248 {
8249 if (child->tag == DW_TAG_subprogram
8250 || child->tag == DW_TAG_lexical_block)
8251 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8252 child = sibling_die (child);
8253 }
8254 }
8255
8256 /* Get the low and high pc's represented by the scope DIE, and store
8257 them in *LOWPC and *HIGHPC. If the correct values can't be
8258 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8259
8260 static void
8261 get_scope_pc_bounds (struct die_info *die,
8262 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8263 struct dwarf2_cu *cu)
8264 {
8265 CORE_ADDR best_low = (CORE_ADDR) -1;
8266 CORE_ADDR best_high = (CORE_ADDR) 0;
8267 CORE_ADDR current_low, current_high;
8268
8269 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8270 {
8271 best_low = current_low;
8272 best_high = current_high;
8273 }
8274 else
8275 {
8276 struct die_info *child = die->child;
8277
8278 while (child && child->tag)
8279 {
8280 switch (child->tag) {
8281 case DW_TAG_subprogram:
8282 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8283 break;
8284 case DW_TAG_namespace:
8285 case DW_TAG_module:
8286 /* FIXME: carlton/2004-01-16: Should we do this for
8287 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8288 that current GCC's always emit the DIEs corresponding
8289 to definitions of methods of classes as children of a
8290 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8291 the DIEs giving the declarations, which could be
8292 anywhere). But I don't see any reason why the
8293 standards says that they have to be there. */
8294 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8295
8296 if (current_low != ((CORE_ADDR) -1))
8297 {
8298 best_low = min (best_low, current_low);
8299 best_high = max (best_high, current_high);
8300 }
8301 break;
8302 default:
8303 /* Ignore. */
8304 break;
8305 }
8306
8307 child = sibling_die (child);
8308 }
8309 }
8310
8311 *lowpc = best_low;
8312 *highpc = best_high;
8313 }
8314
8315 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8316 in DIE. */
8317
8318 static void
8319 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8320 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8321 {
8322 struct objfile *objfile = cu->objfile;
8323 struct attribute *attr;
8324 struct attribute *attr_high;
8325
8326 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8327 if (attr_high)
8328 {
8329 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8330 if (attr)
8331 {
8332 CORE_ADDR low = DW_ADDR (attr);
8333 CORE_ADDR high;
8334 if (attr_high->form == DW_FORM_addr
8335 || attr_high->form == DW_FORM_GNU_addr_index)
8336 high = DW_ADDR (attr_high);
8337 else
8338 high = low + DW_UNSND (attr_high);
8339
8340 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8341 }
8342 }
8343
8344 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8345 if (attr)
8346 {
8347 bfd *obfd = objfile->obfd;
8348
8349 /* The value of the DW_AT_ranges attribute is the offset of the
8350 address range list in the .debug_ranges section. */
8351 unsigned long offset = DW_UNSND (attr);
8352 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8353
8354 /* For some target architectures, but not others, the
8355 read_address function sign-extends the addresses it returns.
8356 To recognize base address selection entries, we need a
8357 mask. */
8358 unsigned int addr_size = cu->header.addr_size;
8359 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8360
8361 /* The base address, to which the next pair is relative. Note
8362 that this 'base' is a DWARF concept: most entries in a range
8363 list are relative, to reduce the number of relocs against the
8364 debugging information. This is separate from this function's
8365 'baseaddr' argument, which GDB uses to relocate debugging
8366 information from a shared library based on the address at
8367 which the library was loaded. */
8368 CORE_ADDR base = cu->base_address;
8369 int base_known = cu->base_known;
8370
8371 gdb_assert (dwarf2_per_objfile->ranges.readin);
8372 if (offset >= dwarf2_per_objfile->ranges.size)
8373 {
8374 complaint (&symfile_complaints,
8375 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8376 offset);
8377 return;
8378 }
8379
8380 for (;;)
8381 {
8382 unsigned int bytes_read;
8383 CORE_ADDR start, end;
8384
8385 start = read_address (obfd, buffer, cu, &bytes_read);
8386 buffer += bytes_read;
8387 end = read_address (obfd, buffer, cu, &bytes_read);
8388 buffer += bytes_read;
8389
8390 /* Did we find the end of the range list? */
8391 if (start == 0 && end == 0)
8392 break;
8393
8394 /* Did we find a base address selection entry? */
8395 else if ((start & base_select_mask) == base_select_mask)
8396 {
8397 base = end;
8398 base_known = 1;
8399 }
8400
8401 /* We found an ordinary address range. */
8402 else
8403 {
8404 if (!base_known)
8405 {
8406 complaint (&symfile_complaints,
8407 _("Invalid .debug_ranges data "
8408 "(no base address)"));
8409 return;
8410 }
8411
8412 if (start > end)
8413 {
8414 /* Inverted range entries are invalid. */
8415 complaint (&symfile_complaints,
8416 _("Invalid .debug_ranges data "
8417 "(inverted range)"));
8418 return;
8419 }
8420
8421 /* Empty range entries have no effect. */
8422 if (start == end)
8423 continue;
8424
8425 record_block_range (block,
8426 baseaddr + base + start,
8427 baseaddr + base + end - 1);
8428 }
8429 }
8430 }
8431 }
8432
8433 /* Check whether the producer field indicates either of GCC < 4.6, or the
8434 Intel C/C++ compiler, and cache the result in CU. */
8435
8436 static void
8437 check_producer (struct dwarf2_cu *cu)
8438 {
8439 const char *cs;
8440 int major, minor, release;
8441
8442 if (cu->producer == NULL)
8443 {
8444 /* For unknown compilers expect their behavior is DWARF version
8445 compliant.
8446
8447 GCC started to support .debug_types sections by -gdwarf-4 since
8448 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8449 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8450 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8451 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8452 }
8453 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
8454 {
8455 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8456
8457 cs = &cu->producer[strlen ("GNU ")];
8458 while (*cs && !isdigit (*cs))
8459 cs++;
8460 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8461 {
8462 /* Not recognized as GCC. */
8463 }
8464 else
8465 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8466 }
8467 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8468 cu->producer_is_icc = 1;
8469 else
8470 {
8471 /* For other non-GCC compilers, expect their behavior is DWARF version
8472 compliant. */
8473 }
8474
8475 cu->checked_producer = 1;
8476 }
8477
8478 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8479 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8480 during 4.6.0 experimental. */
8481
8482 static int
8483 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8484 {
8485 if (!cu->checked_producer)
8486 check_producer (cu);
8487
8488 return cu->producer_is_gxx_lt_4_6;
8489 }
8490
8491 /* Return the default accessibility type if it is not overriden by
8492 DW_AT_accessibility. */
8493
8494 static enum dwarf_access_attribute
8495 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8496 {
8497 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8498 {
8499 /* The default DWARF 2 accessibility for members is public, the default
8500 accessibility for inheritance is private. */
8501
8502 if (die->tag != DW_TAG_inheritance)
8503 return DW_ACCESS_public;
8504 else
8505 return DW_ACCESS_private;
8506 }
8507 else
8508 {
8509 /* DWARF 3+ defines the default accessibility a different way. The same
8510 rules apply now for DW_TAG_inheritance as for the members and it only
8511 depends on the container kind. */
8512
8513 if (die->parent->tag == DW_TAG_class_type)
8514 return DW_ACCESS_private;
8515 else
8516 return DW_ACCESS_public;
8517 }
8518 }
8519
8520 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8521 offset. If the attribute was not found return 0, otherwise return
8522 1. If it was found but could not properly be handled, set *OFFSET
8523 to 0. */
8524
8525 static int
8526 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8527 LONGEST *offset)
8528 {
8529 struct attribute *attr;
8530
8531 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8532 if (attr != NULL)
8533 {
8534 *offset = 0;
8535
8536 /* Note that we do not check for a section offset first here.
8537 This is because DW_AT_data_member_location is new in DWARF 4,
8538 so if we see it, we can assume that a constant form is really
8539 a constant and not a section offset. */
8540 if (attr_form_is_constant (attr))
8541 *offset = dwarf2_get_attr_constant_value (attr, 0);
8542 else if (attr_form_is_section_offset (attr))
8543 dwarf2_complex_location_expr_complaint ();
8544 else if (attr_form_is_block (attr))
8545 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8546 else
8547 dwarf2_complex_location_expr_complaint ();
8548
8549 return 1;
8550 }
8551
8552 return 0;
8553 }
8554
8555 /* Add an aggregate field to the field list. */
8556
8557 static void
8558 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8559 struct dwarf2_cu *cu)
8560 {
8561 struct objfile *objfile = cu->objfile;
8562 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8563 struct nextfield *new_field;
8564 struct attribute *attr;
8565 struct field *fp;
8566 char *fieldname = "";
8567
8568 /* Allocate a new field list entry and link it in. */
8569 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8570 make_cleanup (xfree, new_field);
8571 memset (new_field, 0, sizeof (struct nextfield));
8572
8573 if (die->tag == DW_TAG_inheritance)
8574 {
8575 new_field->next = fip->baseclasses;
8576 fip->baseclasses = new_field;
8577 }
8578 else
8579 {
8580 new_field->next = fip->fields;
8581 fip->fields = new_field;
8582 }
8583 fip->nfields++;
8584
8585 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8586 if (attr)
8587 new_field->accessibility = DW_UNSND (attr);
8588 else
8589 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8590 if (new_field->accessibility != DW_ACCESS_public)
8591 fip->non_public_fields = 1;
8592
8593 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8594 if (attr)
8595 new_field->virtuality = DW_UNSND (attr);
8596 else
8597 new_field->virtuality = DW_VIRTUALITY_none;
8598
8599 fp = &new_field->field;
8600
8601 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8602 {
8603 LONGEST offset;
8604
8605 /* Data member other than a C++ static data member. */
8606
8607 /* Get type of field. */
8608 fp->type = die_type (die, cu);
8609
8610 SET_FIELD_BITPOS (*fp, 0);
8611
8612 /* Get bit size of field (zero if none). */
8613 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8614 if (attr)
8615 {
8616 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8617 }
8618 else
8619 {
8620 FIELD_BITSIZE (*fp) = 0;
8621 }
8622
8623 /* Get bit offset of field. */
8624 if (handle_data_member_location (die, cu, &offset))
8625 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8626 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8627 if (attr)
8628 {
8629 if (gdbarch_bits_big_endian (gdbarch))
8630 {
8631 /* For big endian bits, the DW_AT_bit_offset gives the
8632 additional bit offset from the MSB of the containing
8633 anonymous object to the MSB of the field. We don't
8634 have to do anything special since we don't need to
8635 know the size of the anonymous object. */
8636 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8637 }
8638 else
8639 {
8640 /* For little endian bits, compute the bit offset to the
8641 MSB of the anonymous object, subtract off the number of
8642 bits from the MSB of the field to the MSB of the
8643 object, and then subtract off the number of bits of
8644 the field itself. The result is the bit offset of
8645 the LSB of the field. */
8646 int anonymous_size;
8647 int bit_offset = DW_UNSND (attr);
8648
8649 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8650 if (attr)
8651 {
8652 /* The size of the anonymous object containing
8653 the bit field is explicit, so use the
8654 indicated size (in bytes). */
8655 anonymous_size = DW_UNSND (attr);
8656 }
8657 else
8658 {
8659 /* The size of the anonymous object containing
8660 the bit field must be inferred from the type
8661 attribute of the data member containing the
8662 bit field. */
8663 anonymous_size = TYPE_LENGTH (fp->type);
8664 }
8665 SET_FIELD_BITPOS (*fp,
8666 (FIELD_BITPOS (*fp)
8667 + anonymous_size * bits_per_byte
8668 - bit_offset - FIELD_BITSIZE (*fp)));
8669 }
8670 }
8671
8672 /* Get name of field. */
8673 fieldname = dwarf2_name (die, cu);
8674 if (fieldname == NULL)
8675 fieldname = "";
8676
8677 /* The name is already allocated along with this objfile, so we don't
8678 need to duplicate it for the type. */
8679 fp->name = fieldname;
8680
8681 /* Change accessibility for artificial fields (e.g. virtual table
8682 pointer or virtual base class pointer) to private. */
8683 if (dwarf2_attr (die, DW_AT_artificial, cu))
8684 {
8685 FIELD_ARTIFICIAL (*fp) = 1;
8686 new_field->accessibility = DW_ACCESS_private;
8687 fip->non_public_fields = 1;
8688 }
8689 }
8690 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8691 {
8692 /* C++ static member. */
8693
8694 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8695 is a declaration, but all versions of G++ as of this writing
8696 (so through at least 3.2.1) incorrectly generate
8697 DW_TAG_variable tags. */
8698
8699 const char *physname;
8700
8701 /* Get name of field. */
8702 fieldname = dwarf2_name (die, cu);
8703 if (fieldname == NULL)
8704 return;
8705
8706 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8707 if (attr
8708 /* Only create a symbol if this is an external value.
8709 new_symbol checks this and puts the value in the global symbol
8710 table, which we want. If it is not external, new_symbol
8711 will try to put the value in cu->list_in_scope which is wrong. */
8712 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8713 {
8714 /* A static const member, not much different than an enum as far as
8715 we're concerned, except that we can support more types. */
8716 new_symbol (die, NULL, cu);
8717 }
8718
8719 /* Get physical name. */
8720 physname = dwarf2_physname (fieldname, die, cu);
8721
8722 /* The name is already allocated along with this objfile, so we don't
8723 need to duplicate it for the type. */
8724 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8725 FIELD_TYPE (*fp) = die_type (die, cu);
8726 FIELD_NAME (*fp) = fieldname;
8727 }
8728 else if (die->tag == DW_TAG_inheritance)
8729 {
8730 LONGEST offset;
8731
8732 /* C++ base class field. */
8733 if (handle_data_member_location (die, cu, &offset))
8734 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8735 FIELD_BITSIZE (*fp) = 0;
8736 FIELD_TYPE (*fp) = die_type (die, cu);
8737 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8738 fip->nbaseclasses++;
8739 }
8740 }
8741
8742 /* Add a typedef defined in the scope of the FIP's class. */
8743
8744 static void
8745 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8746 struct dwarf2_cu *cu)
8747 {
8748 struct objfile *objfile = cu->objfile;
8749 struct typedef_field_list *new_field;
8750 struct attribute *attr;
8751 struct typedef_field *fp;
8752 char *fieldname = "";
8753
8754 /* Allocate a new field list entry and link it in. */
8755 new_field = xzalloc (sizeof (*new_field));
8756 make_cleanup (xfree, new_field);
8757
8758 gdb_assert (die->tag == DW_TAG_typedef);
8759
8760 fp = &new_field->field;
8761
8762 /* Get name of field. */
8763 fp->name = dwarf2_name (die, cu);
8764 if (fp->name == NULL)
8765 return;
8766
8767 fp->type = read_type_die (die, cu);
8768
8769 new_field->next = fip->typedef_field_list;
8770 fip->typedef_field_list = new_field;
8771 fip->typedef_field_list_count++;
8772 }
8773
8774 /* Create the vector of fields, and attach it to the type. */
8775
8776 static void
8777 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8778 struct dwarf2_cu *cu)
8779 {
8780 int nfields = fip->nfields;
8781
8782 /* Record the field count, allocate space for the array of fields,
8783 and create blank accessibility bitfields if necessary. */
8784 TYPE_NFIELDS (type) = nfields;
8785 TYPE_FIELDS (type) = (struct field *)
8786 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8787 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8788
8789 if (fip->non_public_fields && cu->language != language_ada)
8790 {
8791 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8792
8793 TYPE_FIELD_PRIVATE_BITS (type) =
8794 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8795 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8796
8797 TYPE_FIELD_PROTECTED_BITS (type) =
8798 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8799 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8800
8801 TYPE_FIELD_IGNORE_BITS (type) =
8802 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8803 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8804 }
8805
8806 /* If the type has baseclasses, allocate and clear a bit vector for
8807 TYPE_FIELD_VIRTUAL_BITS. */
8808 if (fip->nbaseclasses && cu->language != language_ada)
8809 {
8810 int num_bytes = B_BYTES (fip->nbaseclasses);
8811 unsigned char *pointer;
8812
8813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8814 pointer = TYPE_ALLOC (type, num_bytes);
8815 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8816 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8817 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8818 }
8819
8820 /* Copy the saved-up fields into the field vector. Start from the head of
8821 the list, adding to the tail of the field array, so that they end up in
8822 the same order in the array in which they were added to the list. */
8823 while (nfields-- > 0)
8824 {
8825 struct nextfield *fieldp;
8826
8827 if (fip->fields)
8828 {
8829 fieldp = fip->fields;
8830 fip->fields = fieldp->next;
8831 }
8832 else
8833 {
8834 fieldp = fip->baseclasses;
8835 fip->baseclasses = fieldp->next;
8836 }
8837
8838 TYPE_FIELD (type, nfields) = fieldp->field;
8839 switch (fieldp->accessibility)
8840 {
8841 case DW_ACCESS_private:
8842 if (cu->language != language_ada)
8843 SET_TYPE_FIELD_PRIVATE (type, nfields);
8844 break;
8845
8846 case DW_ACCESS_protected:
8847 if (cu->language != language_ada)
8848 SET_TYPE_FIELD_PROTECTED (type, nfields);
8849 break;
8850
8851 case DW_ACCESS_public:
8852 break;
8853
8854 default:
8855 /* Unknown accessibility. Complain and treat it as public. */
8856 {
8857 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8858 fieldp->accessibility);
8859 }
8860 break;
8861 }
8862 if (nfields < fip->nbaseclasses)
8863 {
8864 switch (fieldp->virtuality)
8865 {
8866 case DW_VIRTUALITY_virtual:
8867 case DW_VIRTUALITY_pure_virtual:
8868 if (cu->language == language_ada)
8869 error (_("unexpected virtuality in component of Ada type"));
8870 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8871 break;
8872 }
8873 }
8874 }
8875 }
8876
8877 /* Add a member function to the proper fieldlist. */
8878
8879 static void
8880 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8881 struct type *type, struct dwarf2_cu *cu)
8882 {
8883 struct objfile *objfile = cu->objfile;
8884 struct attribute *attr;
8885 struct fnfieldlist *flp;
8886 int i;
8887 struct fn_field *fnp;
8888 char *fieldname;
8889 struct nextfnfield *new_fnfield;
8890 struct type *this_type;
8891 enum dwarf_access_attribute accessibility;
8892
8893 if (cu->language == language_ada)
8894 error (_("unexpected member function in Ada type"));
8895
8896 /* Get name of member function. */
8897 fieldname = dwarf2_name (die, cu);
8898 if (fieldname == NULL)
8899 return;
8900
8901 /* Look up member function name in fieldlist. */
8902 for (i = 0; i < fip->nfnfields; i++)
8903 {
8904 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8905 break;
8906 }
8907
8908 /* Create new list element if necessary. */
8909 if (i < fip->nfnfields)
8910 flp = &fip->fnfieldlists[i];
8911 else
8912 {
8913 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8914 {
8915 fip->fnfieldlists = (struct fnfieldlist *)
8916 xrealloc (fip->fnfieldlists,
8917 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8918 * sizeof (struct fnfieldlist));
8919 if (fip->nfnfields == 0)
8920 make_cleanup (free_current_contents, &fip->fnfieldlists);
8921 }
8922 flp = &fip->fnfieldlists[fip->nfnfields];
8923 flp->name = fieldname;
8924 flp->length = 0;
8925 flp->head = NULL;
8926 i = fip->nfnfields++;
8927 }
8928
8929 /* Create a new member function field and chain it to the field list
8930 entry. */
8931 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8932 make_cleanup (xfree, new_fnfield);
8933 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8934 new_fnfield->next = flp->head;
8935 flp->head = new_fnfield;
8936 flp->length++;
8937
8938 /* Fill in the member function field info. */
8939 fnp = &new_fnfield->fnfield;
8940
8941 /* Delay processing of the physname until later. */
8942 if (cu->language == language_cplus || cu->language == language_java)
8943 {
8944 add_to_method_list (type, i, flp->length - 1, fieldname,
8945 die, cu);
8946 }
8947 else
8948 {
8949 const char *physname = dwarf2_physname (fieldname, die, cu);
8950 fnp->physname = physname ? physname : "";
8951 }
8952
8953 fnp->type = alloc_type (objfile);
8954 this_type = read_type_die (die, cu);
8955 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8956 {
8957 int nparams = TYPE_NFIELDS (this_type);
8958
8959 /* TYPE is the domain of this method, and THIS_TYPE is the type
8960 of the method itself (TYPE_CODE_METHOD). */
8961 smash_to_method_type (fnp->type, type,
8962 TYPE_TARGET_TYPE (this_type),
8963 TYPE_FIELDS (this_type),
8964 TYPE_NFIELDS (this_type),
8965 TYPE_VARARGS (this_type));
8966
8967 /* Handle static member functions.
8968 Dwarf2 has no clean way to discern C++ static and non-static
8969 member functions. G++ helps GDB by marking the first
8970 parameter for non-static member functions (which is the this
8971 pointer) as artificial. We obtain this information from
8972 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8973 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8974 fnp->voffset = VOFFSET_STATIC;
8975 }
8976 else
8977 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8978 dwarf2_full_name (fieldname, die, cu));
8979
8980 /* Get fcontext from DW_AT_containing_type if present. */
8981 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8982 fnp->fcontext = die_containing_type (die, cu);
8983
8984 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8985 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8986
8987 /* Get accessibility. */
8988 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8989 if (attr)
8990 accessibility = DW_UNSND (attr);
8991 else
8992 accessibility = dwarf2_default_access_attribute (die, cu);
8993 switch (accessibility)
8994 {
8995 case DW_ACCESS_private:
8996 fnp->is_private = 1;
8997 break;
8998 case DW_ACCESS_protected:
8999 fnp->is_protected = 1;
9000 break;
9001 }
9002
9003 /* Check for artificial methods. */
9004 attr = dwarf2_attr (die, DW_AT_artificial, cu);
9005 if (attr && DW_UNSND (attr) != 0)
9006 fnp->is_artificial = 1;
9007
9008 /* Get index in virtual function table if it is a virtual member
9009 function. For older versions of GCC, this is an offset in the
9010 appropriate virtual table, as specified by DW_AT_containing_type.
9011 For everyone else, it is an expression to be evaluated relative
9012 to the object address. */
9013
9014 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
9015 if (attr)
9016 {
9017 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
9018 {
9019 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9020 {
9021 /* Old-style GCC. */
9022 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9023 }
9024 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9025 || (DW_BLOCK (attr)->size > 1
9026 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9027 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9028 {
9029 struct dwarf_block blk;
9030 int offset;
9031
9032 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9033 ? 1 : 2);
9034 blk.size = DW_BLOCK (attr)->size - offset;
9035 blk.data = DW_BLOCK (attr)->data + offset;
9036 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9037 if ((fnp->voffset % cu->header.addr_size) != 0)
9038 dwarf2_complex_location_expr_complaint ();
9039 else
9040 fnp->voffset /= cu->header.addr_size;
9041 fnp->voffset += 2;
9042 }
9043 else
9044 dwarf2_complex_location_expr_complaint ();
9045
9046 if (!fnp->fcontext)
9047 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9048 }
9049 else if (attr_form_is_section_offset (attr))
9050 {
9051 dwarf2_complex_location_expr_complaint ();
9052 }
9053 else
9054 {
9055 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9056 fieldname);
9057 }
9058 }
9059 else
9060 {
9061 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9062 if (attr && DW_UNSND (attr))
9063 {
9064 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9065 complaint (&symfile_complaints,
9066 _("Member function \"%s\" (offset %d) is virtual "
9067 "but the vtable offset is not specified"),
9068 fieldname, die->offset.sect_off);
9069 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9070 TYPE_CPLUS_DYNAMIC (type) = 1;
9071 }
9072 }
9073 }
9074
9075 /* Create the vector of member function fields, and attach it to the type. */
9076
9077 static void
9078 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
9079 struct dwarf2_cu *cu)
9080 {
9081 struct fnfieldlist *flp;
9082 int i;
9083
9084 if (cu->language == language_ada)
9085 error (_("unexpected member functions in Ada type"));
9086
9087 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9088 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9089 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9090
9091 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9092 {
9093 struct nextfnfield *nfp = flp->head;
9094 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9095 int k;
9096
9097 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9098 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9099 fn_flp->fn_fields = (struct fn_field *)
9100 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9101 for (k = flp->length; (k--, nfp); nfp = nfp->next)
9102 fn_flp->fn_fields[k] = nfp->fnfield;
9103 }
9104
9105 TYPE_NFN_FIELDS (type) = fip->nfnfields;
9106 }
9107
9108 /* Returns non-zero if NAME is the name of a vtable member in CU's
9109 language, zero otherwise. */
9110 static int
9111 is_vtable_name (const char *name, struct dwarf2_cu *cu)
9112 {
9113 static const char vptr[] = "_vptr";
9114 static const char vtable[] = "vtable";
9115
9116 /* Look for the C++ and Java forms of the vtable. */
9117 if ((cu->language == language_java
9118 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9119 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9120 && is_cplus_marker (name[sizeof (vptr) - 1])))
9121 return 1;
9122
9123 return 0;
9124 }
9125
9126 /* GCC outputs unnamed structures that are really pointers to member
9127 functions, with the ABI-specified layout. If TYPE describes
9128 such a structure, smash it into a member function type.
9129
9130 GCC shouldn't do this; it should just output pointer to member DIEs.
9131 This is GCC PR debug/28767. */
9132
9133 static void
9134 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
9135 {
9136 struct type *pfn_type, *domain_type, *new_type;
9137
9138 /* Check for a structure with no name and two children. */
9139 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9140 return;
9141
9142 /* Check for __pfn and __delta members. */
9143 if (TYPE_FIELD_NAME (type, 0) == NULL
9144 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9145 || TYPE_FIELD_NAME (type, 1) == NULL
9146 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9147 return;
9148
9149 /* Find the type of the method. */
9150 pfn_type = TYPE_FIELD_TYPE (type, 0);
9151 if (pfn_type == NULL
9152 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9153 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
9154 return;
9155
9156 /* Look for the "this" argument. */
9157 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9158 if (TYPE_NFIELDS (pfn_type) == 0
9159 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
9160 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
9161 return;
9162
9163 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9164 new_type = alloc_type (objfile);
9165 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9166 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9167 TYPE_VARARGS (pfn_type));
9168 smash_to_methodptr_type (type, new_type);
9169 }
9170
9171 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9172 (icc). */
9173
9174 static int
9175 producer_is_icc (struct dwarf2_cu *cu)
9176 {
9177 if (!cu->checked_producer)
9178 check_producer (cu);
9179
9180 return cu->producer_is_icc;
9181 }
9182
9183 /* Called when we find the DIE that starts a structure or union scope
9184 (definition) to create a type for the structure or union. Fill in
9185 the type's name and general properties; the members will not be
9186 processed until process_structure_type.
9187
9188 NOTE: we need to call these functions regardless of whether or not the
9189 DIE has a DW_AT_name attribute, since it might be an anonymous
9190 structure or union. This gets the type entered into our set of
9191 user defined types.
9192
9193 However, if the structure is incomplete (an opaque struct/union)
9194 then suppress creating a symbol table entry for it since gdb only
9195 wants to find the one with the complete definition. Note that if
9196 it is complete, we just call new_symbol, which does it's own
9197 checking about whether the struct/union is anonymous or not (and
9198 suppresses creating a symbol table entry itself). */
9199
9200 static struct type *
9201 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9202 {
9203 struct objfile *objfile = cu->objfile;
9204 struct type *type;
9205 struct attribute *attr;
9206 char *name;
9207
9208 /* If the definition of this type lives in .debug_types, read that type.
9209 Don't follow DW_AT_specification though, that will take us back up
9210 the chain and we want to go down. */
9211 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9212 if (attr)
9213 {
9214 struct dwarf2_cu *type_cu = cu;
9215 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9216
9217 /* We could just recurse on read_structure_type, but we need to call
9218 get_die_type to ensure only one type for this DIE is created.
9219 This is important, for example, because for c++ classes we need
9220 TYPE_NAME set which is only done by new_symbol. Blech. */
9221 type = read_type_die (type_die, type_cu);
9222
9223 /* TYPE_CU may not be the same as CU.
9224 Ensure TYPE is recorded in CU's type_hash table. */
9225 return set_die_type (die, type, cu);
9226 }
9227
9228 type = alloc_type (objfile);
9229 INIT_CPLUS_SPECIFIC (type);
9230
9231 name = dwarf2_name (die, cu);
9232 if (name != NULL)
9233 {
9234 if (cu->language == language_cplus
9235 || cu->language == language_java)
9236 {
9237 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9238
9239 /* dwarf2_full_name might have already finished building the DIE's
9240 type. If so, there is no need to continue. */
9241 if (get_die_type (die, cu) != NULL)
9242 return get_die_type (die, cu);
9243
9244 TYPE_TAG_NAME (type) = full_name;
9245 if (die->tag == DW_TAG_structure_type
9246 || die->tag == DW_TAG_class_type)
9247 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9248 }
9249 else
9250 {
9251 /* The name is already allocated along with this objfile, so
9252 we don't need to duplicate it for the type. */
9253 TYPE_TAG_NAME (type) = (char *) name;
9254 if (die->tag == DW_TAG_class_type)
9255 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9256 }
9257 }
9258
9259 if (die->tag == DW_TAG_structure_type)
9260 {
9261 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9262 }
9263 else if (die->tag == DW_TAG_union_type)
9264 {
9265 TYPE_CODE (type) = TYPE_CODE_UNION;
9266 }
9267 else
9268 {
9269 TYPE_CODE (type) = TYPE_CODE_CLASS;
9270 }
9271
9272 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9273 TYPE_DECLARED_CLASS (type) = 1;
9274
9275 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9276 if (attr)
9277 {
9278 TYPE_LENGTH (type) = DW_UNSND (attr);
9279 }
9280 else
9281 {
9282 TYPE_LENGTH (type) = 0;
9283 }
9284
9285 if (producer_is_icc (cu))
9286 {
9287 /* ICC does not output the required DW_AT_declaration
9288 on incomplete types, but gives them a size of zero. */
9289 }
9290 else
9291 TYPE_STUB_SUPPORTED (type) = 1;
9292
9293 if (die_is_declaration (die, cu))
9294 TYPE_STUB (type) = 1;
9295 else if (attr == NULL && die->child == NULL
9296 && producer_is_realview (cu->producer))
9297 /* RealView does not output the required DW_AT_declaration
9298 on incomplete types. */
9299 TYPE_STUB (type) = 1;
9300
9301 /* We need to add the type field to the die immediately so we don't
9302 infinitely recurse when dealing with pointers to the structure
9303 type within the structure itself. */
9304 set_die_type (die, type, cu);
9305
9306 /* set_die_type should be already done. */
9307 set_descriptive_type (type, die, cu);
9308
9309 return type;
9310 }
9311
9312 /* Finish creating a structure or union type, including filling in
9313 its members and creating a symbol for it. */
9314
9315 static void
9316 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9317 {
9318 struct objfile *objfile = cu->objfile;
9319 struct die_info *child_die = die->child;
9320 struct type *type;
9321
9322 type = get_die_type (die, cu);
9323 if (type == NULL)
9324 type = read_structure_type (die, cu);
9325
9326 if (die->child != NULL && ! die_is_declaration (die, cu))
9327 {
9328 struct field_info fi;
9329 struct die_info *child_die;
9330 VEC (symbolp) *template_args = NULL;
9331 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9332
9333 memset (&fi, 0, sizeof (struct field_info));
9334
9335 child_die = die->child;
9336
9337 while (child_die && child_die->tag)
9338 {
9339 if (child_die->tag == DW_TAG_member
9340 || child_die->tag == DW_TAG_variable)
9341 {
9342 /* NOTE: carlton/2002-11-05: A C++ static data member
9343 should be a DW_TAG_member that is a declaration, but
9344 all versions of G++ as of this writing (so through at
9345 least 3.2.1) incorrectly generate DW_TAG_variable
9346 tags for them instead. */
9347 dwarf2_add_field (&fi, child_die, cu);
9348 }
9349 else if (child_die->tag == DW_TAG_subprogram)
9350 {
9351 /* C++ member function. */
9352 dwarf2_add_member_fn (&fi, child_die, type, cu);
9353 }
9354 else if (child_die->tag == DW_TAG_inheritance)
9355 {
9356 /* C++ base class field. */
9357 dwarf2_add_field (&fi, child_die, cu);
9358 }
9359 else if (child_die->tag == DW_TAG_typedef)
9360 dwarf2_add_typedef (&fi, child_die, cu);
9361 else if (child_die->tag == DW_TAG_template_type_param
9362 || child_die->tag == DW_TAG_template_value_param)
9363 {
9364 struct symbol *arg = new_symbol (child_die, NULL, cu);
9365
9366 if (arg != NULL)
9367 VEC_safe_push (symbolp, template_args, arg);
9368 }
9369
9370 child_die = sibling_die (child_die);
9371 }
9372
9373 /* Attach template arguments to type. */
9374 if (! VEC_empty (symbolp, template_args))
9375 {
9376 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9377 TYPE_N_TEMPLATE_ARGUMENTS (type)
9378 = VEC_length (symbolp, template_args);
9379 TYPE_TEMPLATE_ARGUMENTS (type)
9380 = obstack_alloc (&objfile->objfile_obstack,
9381 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9382 * sizeof (struct symbol *)));
9383 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9384 VEC_address (symbolp, template_args),
9385 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9386 * sizeof (struct symbol *)));
9387 VEC_free (symbolp, template_args);
9388 }
9389
9390 /* Attach fields and member functions to the type. */
9391 if (fi.nfields)
9392 dwarf2_attach_fields_to_type (&fi, type, cu);
9393 if (fi.nfnfields)
9394 {
9395 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9396
9397 /* Get the type which refers to the base class (possibly this
9398 class itself) which contains the vtable pointer for the current
9399 class from the DW_AT_containing_type attribute. This use of
9400 DW_AT_containing_type is a GNU extension. */
9401
9402 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9403 {
9404 struct type *t = die_containing_type (die, cu);
9405
9406 TYPE_VPTR_BASETYPE (type) = t;
9407 if (type == t)
9408 {
9409 int i;
9410
9411 /* Our own class provides vtbl ptr. */
9412 for (i = TYPE_NFIELDS (t) - 1;
9413 i >= TYPE_N_BASECLASSES (t);
9414 --i)
9415 {
9416 const char *fieldname = TYPE_FIELD_NAME (t, i);
9417
9418 if (is_vtable_name (fieldname, cu))
9419 {
9420 TYPE_VPTR_FIELDNO (type) = i;
9421 break;
9422 }
9423 }
9424
9425 /* Complain if virtual function table field not found. */
9426 if (i < TYPE_N_BASECLASSES (t))
9427 complaint (&symfile_complaints,
9428 _("virtual function table pointer "
9429 "not found when defining class '%s'"),
9430 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9431 "");
9432 }
9433 else
9434 {
9435 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9436 }
9437 }
9438 else if (cu->producer
9439 && strncmp (cu->producer,
9440 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9441 {
9442 /* The IBM XLC compiler does not provide direct indication
9443 of the containing type, but the vtable pointer is
9444 always named __vfp. */
9445
9446 int i;
9447
9448 for (i = TYPE_NFIELDS (type) - 1;
9449 i >= TYPE_N_BASECLASSES (type);
9450 --i)
9451 {
9452 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9453 {
9454 TYPE_VPTR_FIELDNO (type) = i;
9455 TYPE_VPTR_BASETYPE (type) = type;
9456 break;
9457 }
9458 }
9459 }
9460 }
9461
9462 /* Copy fi.typedef_field_list linked list elements content into the
9463 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9464 if (fi.typedef_field_list)
9465 {
9466 int i = fi.typedef_field_list_count;
9467
9468 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9469 TYPE_TYPEDEF_FIELD_ARRAY (type)
9470 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9471 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9472
9473 /* Reverse the list order to keep the debug info elements order. */
9474 while (--i >= 0)
9475 {
9476 struct typedef_field *dest, *src;
9477
9478 dest = &TYPE_TYPEDEF_FIELD (type, i);
9479 src = &fi.typedef_field_list->field;
9480 fi.typedef_field_list = fi.typedef_field_list->next;
9481 *dest = *src;
9482 }
9483 }
9484
9485 do_cleanups (back_to);
9486
9487 if (HAVE_CPLUS_STRUCT (type))
9488 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9489 }
9490
9491 quirk_gcc_member_function_pointer (type, objfile);
9492
9493 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9494 snapshots) has been known to create a die giving a declaration
9495 for a class that has, as a child, a die giving a definition for a
9496 nested class. So we have to process our children even if the
9497 current die is a declaration. Normally, of course, a declaration
9498 won't have any children at all. */
9499
9500 while (child_die != NULL && child_die->tag)
9501 {
9502 if (child_die->tag == DW_TAG_member
9503 || child_die->tag == DW_TAG_variable
9504 || child_die->tag == DW_TAG_inheritance
9505 || child_die->tag == DW_TAG_template_value_param
9506 || child_die->tag == DW_TAG_template_type_param)
9507 {
9508 /* Do nothing. */
9509 }
9510 else
9511 process_die (child_die, cu);
9512
9513 child_die = sibling_die (child_die);
9514 }
9515
9516 /* Do not consider external references. According to the DWARF standard,
9517 these DIEs are identified by the fact that they have no byte_size
9518 attribute, and a declaration attribute. */
9519 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9520 || !die_is_declaration (die, cu))
9521 new_symbol (die, type, cu);
9522 }
9523
9524 /* Given a DW_AT_enumeration_type die, set its type. We do not
9525 complete the type's fields yet, or create any symbols. */
9526
9527 static struct type *
9528 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9529 {
9530 struct objfile *objfile = cu->objfile;
9531 struct type *type;
9532 struct attribute *attr;
9533 const char *name;
9534
9535 /* If the definition of this type lives in .debug_types, read that type.
9536 Don't follow DW_AT_specification though, that will take us back up
9537 the chain and we want to go down. */
9538 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9539 if (attr)
9540 {
9541 struct dwarf2_cu *type_cu = cu;
9542 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9543
9544 type = read_type_die (type_die, type_cu);
9545
9546 /* TYPE_CU may not be the same as CU.
9547 Ensure TYPE is recorded in CU's type_hash table. */
9548 return set_die_type (die, type, cu);
9549 }
9550
9551 type = alloc_type (objfile);
9552
9553 TYPE_CODE (type) = TYPE_CODE_ENUM;
9554 name = dwarf2_full_name (NULL, die, cu);
9555 if (name != NULL)
9556 TYPE_TAG_NAME (type) = (char *) name;
9557
9558 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9559 if (attr)
9560 {
9561 TYPE_LENGTH (type) = DW_UNSND (attr);
9562 }
9563 else
9564 {
9565 TYPE_LENGTH (type) = 0;
9566 }
9567
9568 /* The enumeration DIE can be incomplete. In Ada, any type can be
9569 declared as private in the package spec, and then defined only
9570 inside the package body. Such types are known as Taft Amendment
9571 Types. When another package uses such a type, an incomplete DIE
9572 may be generated by the compiler. */
9573 if (die_is_declaration (die, cu))
9574 TYPE_STUB (type) = 1;
9575
9576 return set_die_type (die, type, cu);
9577 }
9578
9579 /* Given a pointer to a die which begins an enumeration, process all
9580 the dies that define the members of the enumeration, and create the
9581 symbol for the enumeration type.
9582
9583 NOTE: We reverse the order of the element list. */
9584
9585 static void
9586 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9587 {
9588 struct type *this_type;
9589
9590 this_type = get_die_type (die, cu);
9591 if (this_type == NULL)
9592 this_type = read_enumeration_type (die, cu);
9593
9594 if (die->child != NULL)
9595 {
9596 struct die_info *child_die;
9597 struct symbol *sym;
9598 struct field *fields = NULL;
9599 int num_fields = 0;
9600 int unsigned_enum = 1;
9601 char *name;
9602 int flag_enum = 1;
9603 ULONGEST mask = 0;
9604
9605 child_die = die->child;
9606 while (child_die && child_die->tag)
9607 {
9608 if (child_die->tag != DW_TAG_enumerator)
9609 {
9610 process_die (child_die, cu);
9611 }
9612 else
9613 {
9614 name = dwarf2_name (child_die, cu);
9615 if (name)
9616 {
9617 sym = new_symbol (child_die, this_type, cu);
9618 if (SYMBOL_VALUE (sym) < 0)
9619 {
9620 unsigned_enum = 0;
9621 flag_enum = 0;
9622 }
9623 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9624 flag_enum = 0;
9625 else
9626 mask |= SYMBOL_VALUE (sym);
9627
9628 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9629 {
9630 fields = (struct field *)
9631 xrealloc (fields,
9632 (num_fields + DW_FIELD_ALLOC_CHUNK)
9633 * sizeof (struct field));
9634 }
9635
9636 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9637 FIELD_TYPE (fields[num_fields]) = NULL;
9638 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9639 FIELD_BITSIZE (fields[num_fields]) = 0;
9640
9641 num_fields++;
9642 }
9643 }
9644
9645 child_die = sibling_die (child_die);
9646 }
9647
9648 if (num_fields)
9649 {
9650 TYPE_NFIELDS (this_type) = num_fields;
9651 TYPE_FIELDS (this_type) = (struct field *)
9652 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9653 memcpy (TYPE_FIELDS (this_type), fields,
9654 sizeof (struct field) * num_fields);
9655 xfree (fields);
9656 }
9657 if (unsigned_enum)
9658 TYPE_UNSIGNED (this_type) = 1;
9659 if (flag_enum)
9660 TYPE_FLAG_ENUM (this_type) = 1;
9661 }
9662
9663 /* If we are reading an enum from a .debug_types unit, and the enum
9664 is a declaration, and the enum is not the signatured type in the
9665 unit, then we do not want to add a symbol for it. Adding a
9666 symbol would in some cases obscure the true definition of the
9667 enum, giving users an incomplete type when the definition is
9668 actually available. Note that we do not want to do this for all
9669 enums which are just declarations, because C++0x allows forward
9670 enum declarations. */
9671 if (cu->per_cu->is_debug_types
9672 && die_is_declaration (die, cu))
9673 {
9674 struct signatured_type *sig_type;
9675
9676 sig_type
9677 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9678 cu->per_cu->info_or_types_section,
9679 cu->per_cu->offset);
9680 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9681 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9682 return;
9683 }
9684
9685 new_symbol (die, this_type, cu);
9686 }
9687
9688 /* Extract all information from a DW_TAG_array_type DIE and put it in
9689 the DIE's type field. For now, this only handles one dimensional
9690 arrays. */
9691
9692 static struct type *
9693 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9694 {
9695 struct objfile *objfile = cu->objfile;
9696 struct die_info *child_die;
9697 struct type *type;
9698 struct type *element_type, *range_type, *index_type;
9699 struct type **range_types = NULL;
9700 struct attribute *attr;
9701 int ndim = 0;
9702 struct cleanup *back_to;
9703 char *name;
9704
9705 element_type = die_type (die, cu);
9706
9707 /* The die_type call above may have already set the type for this DIE. */
9708 type = get_die_type (die, cu);
9709 if (type)
9710 return type;
9711
9712 /* Irix 6.2 native cc creates array types without children for
9713 arrays with unspecified length. */
9714 if (die->child == NULL)
9715 {
9716 index_type = objfile_type (objfile)->builtin_int;
9717 range_type = create_range_type (NULL, index_type, 0, -1);
9718 type = create_array_type (NULL, element_type, range_type);
9719 return set_die_type (die, type, cu);
9720 }
9721
9722 back_to = make_cleanup (null_cleanup, NULL);
9723 child_die = die->child;
9724 while (child_die && child_die->tag)
9725 {
9726 if (child_die->tag == DW_TAG_subrange_type)
9727 {
9728 struct type *child_type = read_type_die (child_die, cu);
9729
9730 if (child_type != NULL)
9731 {
9732 /* The range type was succesfully read. Save it for the
9733 array type creation. */
9734 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9735 {
9736 range_types = (struct type **)
9737 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9738 * sizeof (struct type *));
9739 if (ndim == 0)
9740 make_cleanup (free_current_contents, &range_types);
9741 }
9742 range_types[ndim++] = child_type;
9743 }
9744 }
9745 child_die = sibling_die (child_die);
9746 }
9747
9748 /* Dwarf2 dimensions are output from left to right, create the
9749 necessary array types in backwards order. */
9750
9751 type = element_type;
9752
9753 if (read_array_order (die, cu) == DW_ORD_col_major)
9754 {
9755 int i = 0;
9756
9757 while (i < ndim)
9758 type = create_array_type (NULL, type, range_types[i++]);
9759 }
9760 else
9761 {
9762 while (ndim-- > 0)
9763 type = create_array_type (NULL, type, range_types[ndim]);
9764 }
9765
9766 /* Understand Dwarf2 support for vector types (like they occur on
9767 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9768 array type. This is not part of the Dwarf2/3 standard yet, but a
9769 custom vendor extension. The main difference between a regular
9770 array and the vector variant is that vectors are passed by value
9771 to functions. */
9772 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9773 if (attr)
9774 make_vector_type (type);
9775
9776 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9777 implementation may choose to implement triple vectors using this
9778 attribute. */
9779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9780 if (attr)
9781 {
9782 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9783 TYPE_LENGTH (type) = DW_UNSND (attr);
9784 else
9785 complaint (&symfile_complaints,
9786 _("DW_AT_byte_size for array type smaller "
9787 "than the total size of elements"));
9788 }
9789
9790 name = dwarf2_name (die, cu);
9791 if (name)
9792 TYPE_NAME (type) = name;
9793
9794 /* Install the type in the die. */
9795 set_die_type (die, type, cu);
9796
9797 /* set_die_type should be already done. */
9798 set_descriptive_type (type, die, cu);
9799
9800 do_cleanups (back_to);
9801
9802 return type;
9803 }
9804
9805 static enum dwarf_array_dim_ordering
9806 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9807 {
9808 struct attribute *attr;
9809
9810 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9811
9812 if (attr) return DW_SND (attr);
9813
9814 /* GNU F77 is a special case, as at 08/2004 array type info is the
9815 opposite order to the dwarf2 specification, but data is still
9816 laid out as per normal fortran.
9817
9818 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9819 version checking. */
9820
9821 if (cu->language == language_fortran
9822 && cu->producer && strstr (cu->producer, "GNU F77"))
9823 {
9824 return DW_ORD_row_major;
9825 }
9826
9827 switch (cu->language_defn->la_array_ordering)
9828 {
9829 case array_column_major:
9830 return DW_ORD_col_major;
9831 case array_row_major:
9832 default:
9833 return DW_ORD_row_major;
9834 };
9835 }
9836
9837 /* Extract all information from a DW_TAG_set_type DIE and put it in
9838 the DIE's type field. */
9839
9840 static struct type *
9841 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9842 {
9843 struct type *domain_type, *set_type;
9844 struct attribute *attr;
9845
9846 domain_type = die_type (die, cu);
9847
9848 /* The die_type call above may have already set the type for this DIE. */
9849 set_type = get_die_type (die, cu);
9850 if (set_type)
9851 return set_type;
9852
9853 set_type = create_set_type (NULL, domain_type);
9854
9855 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9856 if (attr)
9857 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9858
9859 return set_die_type (die, set_type, cu);
9860 }
9861
9862 /* First cut: install each common block member as a global variable. */
9863
9864 static void
9865 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9866 {
9867 struct die_info *child_die;
9868 struct attribute *attr;
9869 struct symbol *sym;
9870 CORE_ADDR base = (CORE_ADDR) 0;
9871
9872 attr = dwarf2_attr (die, DW_AT_location, cu);
9873 if (attr)
9874 {
9875 /* Support the .debug_loc offsets. */
9876 if (attr_form_is_block (attr))
9877 {
9878 base = decode_locdesc (DW_BLOCK (attr), cu);
9879 }
9880 else if (attr_form_is_section_offset (attr))
9881 {
9882 dwarf2_complex_location_expr_complaint ();
9883 }
9884 else
9885 {
9886 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9887 "common block member");
9888 }
9889 }
9890 if (die->child != NULL)
9891 {
9892 child_die = die->child;
9893 while (child_die && child_die->tag)
9894 {
9895 LONGEST offset;
9896
9897 sym = new_symbol (child_die, NULL, cu);
9898 if (sym != NULL
9899 && handle_data_member_location (child_die, cu, &offset))
9900 {
9901 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9902 add_symbol_to_list (sym, &global_symbols);
9903 }
9904 child_die = sibling_die (child_die);
9905 }
9906 }
9907 }
9908
9909 /* Create a type for a C++ namespace. */
9910
9911 static struct type *
9912 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9913 {
9914 struct objfile *objfile = cu->objfile;
9915 const char *previous_prefix, *name;
9916 int is_anonymous;
9917 struct type *type;
9918
9919 /* For extensions, reuse the type of the original namespace. */
9920 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9921 {
9922 struct die_info *ext_die;
9923 struct dwarf2_cu *ext_cu = cu;
9924
9925 ext_die = dwarf2_extension (die, &ext_cu);
9926 type = read_type_die (ext_die, ext_cu);
9927
9928 /* EXT_CU may not be the same as CU.
9929 Ensure TYPE is recorded in CU's type_hash table. */
9930 return set_die_type (die, type, cu);
9931 }
9932
9933 name = namespace_name (die, &is_anonymous, cu);
9934
9935 /* Now build the name of the current namespace. */
9936
9937 previous_prefix = determine_prefix (die, cu);
9938 if (previous_prefix[0] != '\0')
9939 name = typename_concat (&objfile->objfile_obstack,
9940 previous_prefix, name, 0, cu);
9941
9942 /* Create the type. */
9943 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9944 objfile);
9945 TYPE_NAME (type) = (char *) name;
9946 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9947
9948 return set_die_type (die, type, cu);
9949 }
9950
9951 /* Read a C++ namespace. */
9952
9953 static void
9954 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9955 {
9956 struct objfile *objfile = cu->objfile;
9957 int is_anonymous;
9958
9959 /* Add a symbol associated to this if we haven't seen the namespace
9960 before. Also, add a using directive if it's an anonymous
9961 namespace. */
9962
9963 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9964 {
9965 struct type *type;
9966
9967 type = read_type_die (die, cu);
9968 new_symbol (die, type, cu);
9969
9970 namespace_name (die, &is_anonymous, cu);
9971 if (is_anonymous)
9972 {
9973 const char *previous_prefix = determine_prefix (die, cu);
9974
9975 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9976 NULL, NULL, &objfile->objfile_obstack);
9977 }
9978 }
9979
9980 if (die->child != NULL)
9981 {
9982 struct die_info *child_die = die->child;
9983
9984 while (child_die && child_die->tag)
9985 {
9986 process_die (child_die, cu);
9987 child_die = sibling_die (child_die);
9988 }
9989 }
9990 }
9991
9992 /* Read a Fortran module as type. This DIE can be only a declaration used for
9993 imported module. Still we need that type as local Fortran "use ... only"
9994 declaration imports depend on the created type in determine_prefix. */
9995
9996 static struct type *
9997 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9998 {
9999 struct objfile *objfile = cu->objfile;
10000 char *module_name;
10001 struct type *type;
10002
10003 module_name = dwarf2_name (die, cu);
10004 if (!module_name)
10005 complaint (&symfile_complaints,
10006 _("DW_TAG_module has no name, offset 0x%x"),
10007 die->offset.sect_off);
10008 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10009
10010 /* determine_prefix uses TYPE_TAG_NAME. */
10011 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10012
10013 return set_die_type (die, type, cu);
10014 }
10015
10016 /* Read a Fortran module. */
10017
10018 static void
10019 read_module (struct die_info *die, struct dwarf2_cu *cu)
10020 {
10021 struct die_info *child_die = die->child;
10022
10023 while (child_die && child_die->tag)
10024 {
10025 process_die (child_die, cu);
10026 child_die = sibling_die (child_die);
10027 }
10028 }
10029
10030 /* Return the name of the namespace represented by DIE. Set
10031 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10032 namespace. */
10033
10034 static const char *
10035 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
10036 {
10037 struct die_info *current_die;
10038 const char *name = NULL;
10039
10040 /* Loop through the extensions until we find a name. */
10041
10042 for (current_die = die;
10043 current_die != NULL;
10044 current_die = dwarf2_extension (die, &cu))
10045 {
10046 name = dwarf2_name (current_die, cu);
10047 if (name != NULL)
10048 break;
10049 }
10050
10051 /* Is it an anonymous namespace? */
10052
10053 *is_anonymous = (name == NULL);
10054 if (*is_anonymous)
10055 name = CP_ANONYMOUS_NAMESPACE_STR;
10056
10057 return name;
10058 }
10059
10060 /* Extract all information from a DW_TAG_pointer_type DIE and add to
10061 the user defined type vector. */
10062
10063 static struct type *
10064 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
10065 {
10066 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
10067 struct comp_unit_head *cu_header = &cu->header;
10068 struct type *type;
10069 struct attribute *attr_byte_size;
10070 struct attribute *attr_address_class;
10071 int byte_size, addr_class;
10072 struct type *target_type;
10073
10074 target_type = die_type (die, cu);
10075
10076 /* The die_type call above may have already set the type for this DIE. */
10077 type = get_die_type (die, cu);
10078 if (type)
10079 return type;
10080
10081 type = lookup_pointer_type (target_type);
10082
10083 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
10084 if (attr_byte_size)
10085 byte_size = DW_UNSND (attr_byte_size);
10086 else
10087 byte_size = cu_header->addr_size;
10088
10089 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
10090 if (attr_address_class)
10091 addr_class = DW_UNSND (attr_address_class);
10092 else
10093 addr_class = DW_ADDR_none;
10094
10095 /* If the pointer size or address class is different than the
10096 default, create a type variant marked as such and set the
10097 length accordingly. */
10098 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
10099 {
10100 if (gdbarch_address_class_type_flags_p (gdbarch))
10101 {
10102 int type_flags;
10103
10104 type_flags = gdbarch_address_class_type_flags
10105 (gdbarch, byte_size, addr_class);
10106 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10107 == 0);
10108 type = make_type_with_address_space (type, type_flags);
10109 }
10110 else if (TYPE_LENGTH (type) != byte_size)
10111 {
10112 complaint (&symfile_complaints,
10113 _("invalid pointer size %d"), byte_size);
10114 }
10115 else
10116 {
10117 /* Should we also complain about unhandled address classes? */
10118 }
10119 }
10120
10121 TYPE_LENGTH (type) = byte_size;
10122 return set_die_type (die, type, cu);
10123 }
10124
10125 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10126 the user defined type vector. */
10127
10128 static struct type *
10129 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
10130 {
10131 struct type *type;
10132 struct type *to_type;
10133 struct type *domain;
10134
10135 to_type = die_type (die, cu);
10136 domain = die_containing_type (die, cu);
10137
10138 /* The calls above may have already set the type for this DIE. */
10139 type = get_die_type (die, cu);
10140 if (type)
10141 return type;
10142
10143 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10144 type = lookup_methodptr_type (to_type);
10145 else
10146 type = lookup_memberptr_type (to_type, domain);
10147
10148 return set_die_type (die, type, cu);
10149 }
10150
10151 /* Extract all information from a DW_TAG_reference_type DIE and add to
10152 the user defined type vector. */
10153
10154 static struct type *
10155 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
10156 {
10157 struct comp_unit_head *cu_header = &cu->header;
10158 struct type *type, *target_type;
10159 struct attribute *attr;
10160
10161 target_type = die_type (die, cu);
10162
10163 /* The die_type call above may have already set the type for this DIE. */
10164 type = get_die_type (die, cu);
10165 if (type)
10166 return type;
10167
10168 type = lookup_reference_type (target_type);
10169 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10170 if (attr)
10171 {
10172 TYPE_LENGTH (type) = DW_UNSND (attr);
10173 }
10174 else
10175 {
10176 TYPE_LENGTH (type) = cu_header->addr_size;
10177 }
10178 return set_die_type (die, type, cu);
10179 }
10180
10181 static struct type *
10182 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10183 {
10184 struct type *base_type, *cv_type;
10185
10186 base_type = die_type (die, cu);
10187
10188 /* The die_type call above may have already set the type for this DIE. */
10189 cv_type = get_die_type (die, cu);
10190 if (cv_type)
10191 return cv_type;
10192
10193 /* In case the const qualifier is applied to an array type, the element type
10194 is so qualified, not the array type (section 6.7.3 of C99). */
10195 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10196 {
10197 struct type *el_type, *inner_array;
10198
10199 base_type = copy_type (base_type);
10200 inner_array = base_type;
10201
10202 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10203 {
10204 TYPE_TARGET_TYPE (inner_array) =
10205 copy_type (TYPE_TARGET_TYPE (inner_array));
10206 inner_array = TYPE_TARGET_TYPE (inner_array);
10207 }
10208
10209 el_type = TYPE_TARGET_TYPE (inner_array);
10210 TYPE_TARGET_TYPE (inner_array) =
10211 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10212
10213 return set_die_type (die, base_type, cu);
10214 }
10215
10216 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10217 return set_die_type (die, cv_type, cu);
10218 }
10219
10220 static struct type *
10221 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10222 {
10223 struct type *base_type, *cv_type;
10224
10225 base_type = die_type (die, cu);
10226
10227 /* The die_type call above may have already set the type for this DIE. */
10228 cv_type = get_die_type (die, cu);
10229 if (cv_type)
10230 return cv_type;
10231
10232 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10233 return set_die_type (die, cv_type, cu);
10234 }
10235
10236 /* Extract all information from a DW_TAG_string_type DIE and add to
10237 the user defined type vector. It isn't really a user defined type,
10238 but it behaves like one, with other DIE's using an AT_user_def_type
10239 attribute to reference it. */
10240
10241 static struct type *
10242 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10243 {
10244 struct objfile *objfile = cu->objfile;
10245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10246 struct type *type, *range_type, *index_type, *char_type;
10247 struct attribute *attr;
10248 unsigned int length;
10249
10250 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10251 if (attr)
10252 {
10253 length = DW_UNSND (attr);
10254 }
10255 else
10256 {
10257 /* Check for the DW_AT_byte_size attribute. */
10258 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10259 if (attr)
10260 {
10261 length = DW_UNSND (attr);
10262 }
10263 else
10264 {
10265 length = 1;
10266 }
10267 }
10268
10269 index_type = objfile_type (objfile)->builtin_int;
10270 range_type = create_range_type (NULL, index_type, 1, length);
10271 char_type = language_string_char_type (cu->language_defn, gdbarch);
10272 type = create_string_type (NULL, char_type, range_type);
10273
10274 return set_die_type (die, type, cu);
10275 }
10276
10277 /* Handle DIES due to C code like:
10278
10279 struct foo
10280 {
10281 int (*funcp)(int a, long l);
10282 int b;
10283 };
10284
10285 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10286
10287 static struct type *
10288 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10289 {
10290 struct objfile *objfile = cu->objfile;
10291 struct type *type; /* Type that this function returns. */
10292 struct type *ftype; /* Function that returns above type. */
10293 struct attribute *attr;
10294
10295 type = die_type (die, cu);
10296
10297 /* The die_type call above may have already set the type for this DIE. */
10298 ftype = get_die_type (die, cu);
10299 if (ftype)
10300 return ftype;
10301
10302 ftype = lookup_function_type (type);
10303
10304 /* All functions in C++, Pascal and Java have prototypes. */
10305 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10306 if ((attr && (DW_UNSND (attr) != 0))
10307 || cu->language == language_cplus
10308 || cu->language == language_java
10309 || cu->language == language_pascal)
10310 TYPE_PROTOTYPED (ftype) = 1;
10311 else if (producer_is_realview (cu->producer))
10312 /* RealView does not emit DW_AT_prototyped. We can not
10313 distinguish prototyped and unprototyped functions; default to
10314 prototyped, since that is more common in modern code (and
10315 RealView warns about unprototyped functions). */
10316 TYPE_PROTOTYPED (ftype) = 1;
10317
10318 /* Store the calling convention in the type if it's available in
10319 the subroutine die. Otherwise set the calling convention to
10320 the default value DW_CC_normal. */
10321 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10322 if (attr)
10323 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10324 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10325 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10326 else
10327 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10328
10329 /* We need to add the subroutine type to the die immediately so
10330 we don't infinitely recurse when dealing with parameters
10331 declared as the same subroutine type. */
10332 set_die_type (die, ftype, cu);
10333
10334 if (die->child != NULL)
10335 {
10336 struct type *void_type = objfile_type (objfile)->builtin_void;
10337 struct die_info *child_die;
10338 int nparams, iparams;
10339
10340 /* Count the number of parameters.
10341 FIXME: GDB currently ignores vararg functions, but knows about
10342 vararg member functions. */
10343 nparams = 0;
10344 child_die = die->child;
10345 while (child_die && child_die->tag)
10346 {
10347 if (child_die->tag == DW_TAG_formal_parameter)
10348 nparams++;
10349 else if (child_die->tag == DW_TAG_unspecified_parameters)
10350 TYPE_VARARGS (ftype) = 1;
10351 child_die = sibling_die (child_die);
10352 }
10353
10354 /* Allocate storage for parameters and fill them in. */
10355 TYPE_NFIELDS (ftype) = nparams;
10356 TYPE_FIELDS (ftype) = (struct field *)
10357 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10358
10359 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10360 even if we error out during the parameters reading below. */
10361 for (iparams = 0; iparams < nparams; iparams++)
10362 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10363
10364 iparams = 0;
10365 child_die = die->child;
10366 while (child_die && child_die->tag)
10367 {
10368 if (child_die->tag == DW_TAG_formal_parameter)
10369 {
10370 struct type *arg_type;
10371
10372 /* DWARF version 2 has no clean way to discern C++
10373 static and non-static member functions. G++ helps
10374 GDB by marking the first parameter for non-static
10375 member functions (which is the this pointer) as
10376 artificial. We pass this information to
10377 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10378
10379 DWARF version 3 added DW_AT_object_pointer, which GCC
10380 4.5 does not yet generate. */
10381 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10382 if (attr)
10383 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10384 else
10385 {
10386 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10387
10388 /* GCC/43521: In java, the formal parameter
10389 "this" is sometimes not marked with DW_AT_artificial. */
10390 if (cu->language == language_java)
10391 {
10392 const char *name = dwarf2_name (child_die, cu);
10393
10394 if (name && !strcmp (name, "this"))
10395 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10396 }
10397 }
10398 arg_type = die_type (child_die, cu);
10399
10400 /* RealView does not mark THIS as const, which the testsuite
10401 expects. GCC marks THIS as const in method definitions,
10402 but not in the class specifications (GCC PR 43053). */
10403 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10404 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10405 {
10406 int is_this = 0;
10407 struct dwarf2_cu *arg_cu = cu;
10408 const char *name = dwarf2_name (child_die, cu);
10409
10410 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10411 if (attr)
10412 {
10413 /* If the compiler emits this, use it. */
10414 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10415 is_this = 1;
10416 }
10417 else if (name && strcmp (name, "this") == 0)
10418 /* Function definitions will have the argument names. */
10419 is_this = 1;
10420 else if (name == NULL && iparams == 0)
10421 /* Declarations may not have the names, so like
10422 elsewhere in GDB, assume an artificial first
10423 argument is "this". */
10424 is_this = 1;
10425
10426 if (is_this)
10427 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10428 arg_type, 0);
10429 }
10430
10431 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10432 iparams++;
10433 }
10434 child_die = sibling_die (child_die);
10435 }
10436 }
10437
10438 return ftype;
10439 }
10440
10441 static struct type *
10442 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10443 {
10444 struct objfile *objfile = cu->objfile;
10445 const char *name = NULL;
10446 struct type *this_type, *target_type;
10447
10448 name = dwarf2_full_name (NULL, die, cu);
10449 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10450 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10451 TYPE_NAME (this_type) = (char *) name;
10452 set_die_type (die, this_type, cu);
10453 target_type = die_type (die, cu);
10454 if (target_type != this_type)
10455 TYPE_TARGET_TYPE (this_type) = target_type;
10456 else
10457 {
10458 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10459 spec and cause infinite loops in GDB. */
10460 complaint (&symfile_complaints,
10461 _("Self-referential DW_TAG_typedef "
10462 "- DIE at 0x%x [in module %s]"),
10463 die->offset.sect_off, objfile->name);
10464 TYPE_TARGET_TYPE (this_type) = NULL;
10465 }
10466 return this_type;
10467 }
10468
10469 /* Find a representation of a given base type and install
10470 it in the TYPE field of the die. */
10471
10472 static struct type *
10473 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10474 {
10475 struct objfile *objfile = cu->objfile;
10476 struct type *type;
10477 struct attribute *attr;
10478 int encoding = 0, size = 0;
10479 char *name;
10480 enum type_code code = TYPE_CODE_INT;
10481 int type_flags = 0;
10482 struct type *target_type = NULL;
10483
10484 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10485 if (attr)
10486 {
10487 encoding = DW_UNSND (attr);
10488 }
10489 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10490 if (attr)
10491 {
10492 size = DW_UNSND (attr);
10493 }
10494 name = dwarf2_name (die, cu);
10495 if (!name)
10496 {
10497 complaint (&symfile_complaints,
10498 _("DW_AT_name missing from DW_TAG_base_type"));
10499 }
10500
10501 switch (encoding)
10502 {
10503 case DW_ATE_address:
10504 /* Turn DW_ATE_address into a void * pointer. */
10505 code = TYPE_CODE_PTR;
10506 type_flags |= TYPE_FLAG_UNSIGNED;
10507 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10508 break;
10509 case DW_ATE_boolean:
10510 code = TYPE_CODE_BOOL;
10511 type_flags |= TYPE_FLAG_UNSIGNED;
10512 break;
10513 case DW_ATE_complex_float:
10514 code = TYPE_CODE_COMPLEX;
10515 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10516 break;
10517 case DW_ATE_decimal_float:
10518 code = TYPE_CODE_DECFLOAT;
10519 break;
10520 case DW_ATE_float:
10521 code = TYPE_CODE_FLT;
10522 break;
10523 case DW_ATE_signed:
10524 break;
10525 case DW_ATE_unsigned:
10526 type_flags |= TYPE_FLAG_UNSIGNED;
10527 if (cu->language == language_fortran
10528 && name
10529 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10530 code = TYPE_CODE_CHAR;
10531 break;
10532 case DW_ATE_signed_char:
10533 if (cu->language == language_ada || cu->language == language_m2
10534 || cu->language == language_pascal
10535 || cu->language == language_fortran)
10536 code = TYPE_CODE_CHAR;
10537 break;
10538 case DW_ATE_unsigned_char:
10539 if (cu->language == language_ada || cu->language == language_m2
10540 || cu->language == language_pascal
10541 || cu->language == language_fortran)
10542 code = TYPE_CODE_CHAR;
10543 type_flags |= TYPE_FLAG_UNSIGNED;
10544 break;
10545 case DW_ATE_UTF:
10546 /* We just treat this as an integer and then recognize the
10547 type by name elsewhere. */
10548 break;
10549
10550 default:
10551 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10552 dwarf_type_encoding_name (encoding));
10553 break;
10554 }
10555
10556 type = init_type (code, size, type_flags, NULL, objfile);
10557 TYPE_NAME (type) = name;
10558 TYPE_TARGET_TYPE (type) = target_type;
10559
10560 if (name && strcmp (name, "char") == 0)
10561 TYPE_NOSIGN (type) = 1;
10562
10563 return set_die_type (die, type, cu);
10564 }
10565
10566 /* Read the given DW_AT_subrange DIE. */
10567
10568 static struct type *
10569 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10570 {
10571 struct type *base_type;
10572 struct type *range_type;
10573 struct attribute *attr;
10574 LONGEST low, high;
10575 int low_default_is_valid;
10576 char *name;
10577 LONGEST negative_mask;
10578
10579 base_type = die_type (die, cu);
10580 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10581 check_typedef (base_type);
10582
10583 /* The die_type call above may have already set the type for this DIE. */
10584 range_type = get_die_type (die, cu);
10585 if (range_type)
10586 return range_type;
10587
10588 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10589 omitting DW_AT_lower_bound. */
10590 switch (cu->language)
10591 {
10592 case language_c:
10593 case language_cplus:
10594 low = 0;
10595 low_default_is_valid = 1;
10596 break;
10597 case language_fortran:
10598 low = 1;
10599 low_default_is_valid = 1;
10600 break;
10601 case language_d:
10602 case language_java:
10603 case language_objc:
10604 low = 0;
10605 low_default_is_valid = (cu->header.version >= 4);
10606 break;
10607 case language_ada:
10608 case language_m2:
10609 case language_pascal:
10610 low = 1;
10611 low_default_is_valid = (cu->header.version >= 4);
10612 break;
10613 default:
10614 low = 0;
10615 low_default_is_valid = 0;
10616 break;
10617 }
10618
10619 /* FIXME: For variable sized arrays either of these could be
10620 a variable rather than a constant value. We'll allow it,
10621 but we don't know how to handle it. */
10622 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10623 if (attr)
10624 low = dwarf2_get_attr_constant_value (attr, low);
10625 else if (!low_default_is_valid)
10626 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10627 "- DIE at 0x%x [in module %s]"),
10628 die->offset.sect_off, cu->objfile->name);
10629
10630 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10631 if (attr)
10632 {
10633 if (attr_form_is_block (attr) || is_ref_attr (attr))
10634 {
10635 /* GCC encodes arrays with unspecified or dynamic length
10636 with a DW_FORM_block1 attribute or a reference attribute.
10637 FIXME: GDB does not yet know how to handle dynamic
10638 arrays properly, treat them as arrays with unspecified
10639 length for now.
10640
10641 FIXME: jimb/2003-09-22: GDB does not really know
10642 how to handle arrays of unspecified length
10643 either; we just represent them as zero-length
10644 arrays. Choose an appropriate upper bound given
10645 the lower bound we've computed above. */
10646 high = low - 1;
10647 }
10648 else
10649 high = dwarf2_get_attr_constant_value (attr, 1);
10650 }
10651 else
10652 {
10653 attr = dwarf2_attr (die, DW_AT_count, cu);
10654 if (attr)
10655 {
10656 int count = dwarf2_get_attr_constant_value (attr, 1);
10657 high = low + count - 1;
10658 }
10659 else
10660 {
10661 /* Unspecified array length. */
10662 high = low - 1;
10663 }
10664 }
10665
10666 /* Dwarf-2 specifications explicitly allows to create subrange types
10667 without specifying a base type.
10668 In that case, the base type must be set to the type of
10669 the lower bound, upper bound or count, in that order, if any of these
10670 three attributes references an object that has a type.
10671 If no base type is found, the Dwarf-2 specifications say that
10672 a signed integer type of size equal to the size of an address should
10673 be used.
10674 For the following C code: `extern char gdb_int [];'
10675 GCC produces an empty range DIE.
10676 FIXME: muller/2010-05-28: Possible references to object for low bound,
10677 high bound or count are not yet handled by this code. */
10678 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10679 {
10680 struct objfile *objfile = cu->objfile;
10681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10682 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10683 struct type *int_type = objfile_type (objfile)->builtin_int;
10684
10685 /* Test "int", "long int", and "long long int" objfile types,
10686 and select the first one having a size above or equal to the
10687 architecture address size. */
10688 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10689 base_type = int_type;
10690 else
10691 {
10692 int_type = objfile_type (objfile)->builtin_long;
10693 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10694 base_type = int_type;
10695 else
10696 {
10697 int_type = objfile_type (objfile)->builtin_long_long;
10698 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10699 base_type = int_type;
10700 }
10701 }
10702 }
10703
10704 negative_mask =
10705 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10706 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10707 low |= negative_mask;
10708 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10709 high |= negative_mask;
10710
10711 range_type = create_range_type (NULL, base_type, low, high);
10712
10713 /* Mark arrays with dynamic length at least as an array of unspecified
10714 length. GDB could check the boundary but before it gets implemented at
10715 least allow accessing the array elements. */
10716 if (attr && attr_form_is_block (attr))
10717 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10718
10719 /* Ada expects an empty array on no boundary attributes. */
10720 if (attr == NULL && cu->language != language_ada)
10721 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10722
10723 name = dwarf2_name (die, cu);
10724 if (name)
10725 TYPE_NAME (range_type) = name;
10726
10727 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10728 if (attr)
10729 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10730
10731 set_die_type (die, range_type, cu);
10732
10733 /* set_die_type should be already done. */
10734 set_descriptive_type (range_type, die, cu);
10735
10736 return range_type;
10737 }
10738
10739 static struct type *
10740 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10741 {
10742 struct type *type;
10743
10744 /* For now, we only support the C meaning of an unspecified type: void. */
10745
10746 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10747 TYPE_NAME (type) = dwarf2_name (die, cu);
10748
10749 return set_die_type (die, type, cu);
10750 }
10751
10752 /* Read a single die and all its descendents. Set the die's sibling
10753 field to NULL; set other fields in the die correctly, and set all
10754 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10755 location of the info_ptr after reading all of those dies. PARENT
10756 is the parent of the die in question. */
10757
10758 static struct die_info *
10759 read_die_and_children (const struct die_reader_specs *reader,
10760 gdb_byte *info_ptr,
10761 gdb_byte **new_info_ptr,
10762 struct die_info *parent)
10763 {
10764 struct die_info *die;
10765 gdb_byte *cur_ptr;
10766 int has_children;
10767
10768 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10769 if (die == NULL)
10770 {
10771 *new_info_ptr = cur_ptr;
10772 return NULL;
10773 }
10774 store_in_ref_table (die, reader->cu);
10775
10776 if (has_children)
10777 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10778 else
10779 {
10780 die->child = NULL;
10781 *new_info_ptr = cur_ptr;
10782 }
10783
10784 die->sibling = NULL;
10785 die->parent = parent;
10786 return die;
10787 }
10788
10789 /* Read a die, all of its descendents, and all of its siblings; set
10790 all of the fields of all of the dies correctly. Arguments are as
10791 in read_die_and_children. */
10792
10793 static struct die_info *
10794 read_die_and_siblings (const struct die_reader_specs *reader,
10795 gdb_byte *info_ptr,
10796 gdb_byte **new_info_ptr,
10797 struct die_info *parent)
10798 {
10799 struct die_info *first_die, *last_sibling;
10800 gdb_byte *cur_ptr;
10801
10802 cur_ptr = info_ptr;
10803 first_die = last_sibling = NULL;
10804
10805 while (1)
10806 {
10807 struct die_info *die
10808 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10809
10810 if (die == NULL)
10811 {
10812 *new_info_ptr = cur_ptr;
10813 return first_die;
10814 }
10815
10816 if (!first_die)
10817 first_die = die;
10818 else
10819 last_sibling->sibling = die;
10820
10821 last_sibling = die;
10822 }
10823 }
10824
10825 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10826 attributes.
10827 The caller is responsible for filling in the extra attributes
10828 and updating (*DIEP)->num_attrs.
10829 Set DIEP to point to a newly allocated die with its information,
10830 except for its child, sibling, and parent fields.
10831 Set HAS_CHILDREN to tell whether the die has children or not. */
10832
10833 static gdb_byte *
10834 read_full_die_1 (const struct die_reader_specs *reader,
10835 struct die_info **diep, gdb_byte *info_ptr,
10836 int *has_children, int num_extra_attrs)
10837 {
10838 unsigned int abbrev_number, bytes_read, i;
10839 sect_offset offset;
10840 struct abbrev_info *abbrev;
10841 struct die_info *die;
10842 struct dwarf2_cu *cu = reader->cu;
10843 bfd *abfd = reader->abfd;
10844
10845 offset.sect_off = info_ptr - reader->buffer;
10846 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10847 info_ptr += bytes_read;
10848 if (!abbrev_number)
10849 {
10850 *diep = NULL;
10851 *has_children = 0;
10852 return info_ptr;
10853 }
10854
10855 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10856 if (!abbrev)
10857 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10858 abbrev_number,
10859 bfd_get_filename (abfd));
10860
10861 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10862 die->offset = offset;
10863 die->tag = abbrev->tag;
10864 die->abbrev = abbrev_number;
10865
10866 /* Make the result usable.
10867 The caller needs to update num_attrs after adding the extra
10868 attributes. */
10869 die->num_attrs = abbrev->num_attrs;
10870
10871 for (i = 0; i < abbrev->num_attrs; ++i)
10872 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10873 info_ptr);
10874
10875 *diep = die;
10876 *has_children = abbrev->has_children;
10877 return info_ptr;
10878 }
10879
10880 /* Read a die and all its attributes.
10881 Set DIEP to point to a newly allocated die with its information,
10882 except for its child, sibling, and parent fields.
10883 Set HAS_CHILDREN to tell whether the die has children or not. */
10884
10885 static gdb_byte *
10886 read_full_die (const struct die_reader_specs *reader,
10887 struct die_info **diep, gdb_byte *info_ptr,
10888 int *has_children)
10889 {
10890 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10891 }
10892
10893 /* In DWARF version 2, the description of the debugging information is
10894 stored in a separate .debug_abbrev section. Before we read any
10895 dies from a section we read in all abbreviations and install them
10896 in a hash table. This function also sets flags in CU describing
10897 the data found in the abbrev table. */
10898
10899 static void
10900 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10901 struct dwarf2_section_info *abbrev_section)
10902
10903 {
10904 bfd *abfd = abbrev_section->asection->owner;
10905 struct comp_unit_head *cu_header = &cu->header;
10906 gdb_byte *abbrev_ptr;
10907 struct abbrev_info *cur_abbrev;
10908 unsigned int abbrev_number, bytes_read, abbrev_name;
10909 unsigned int abbrev_form, hash_number;
10910 struct attr_abbrev *cur_attrs;
10911 unsigned int allocated_attrs;
10912
10913 /* Initialize dwarf2 abbrevs. */
10914 obstack_init (&cu->abbrev_obstack);
10915 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10916 (ABBREV_HASH_SIZE
10917 * sizeof (struct abbrev_info *)));
10918 memset (cu->dwarf2_abbrevs, 0,
10919 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10920
10921 dwarf2_read_section (cu->objfile, abbrev_section);
10922 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10923 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10924 abbrev_ptr += bytes_read;
10925
10926 allocated_attrs = ATTR_ALLOC_CHUNK;
10927 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10928
10929 /* Loop until we reach an abbrev number of 0. */
10930 while (abbrev_number)
10931 {
10932 cur_abbrev = dwarf_alloc_abbrev (cu);
10933
10934 /* read in abbrev header */
10935 cur_abbrev->number = abbrev_number;
10936 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10937 abbrev_ptr += bytes_read;
10938 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10939 abbrev_ptr += 1;
10940
10941 /* now read in declarations */
10942 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10943 abbrev_ptr += bytes_read;
10944 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10945 abbrev_ptr += bytes_read;
10946 while (abbrev_name)
10947 {
10948 if (cur_abbrev->num_attrs == allocated_attrs)
10949 {
10950 allocated_attrs += ATTR_ALLOC_CHUNK;
10951 cur_attrs
10952 = xrealloc (cur_attrs, (allocated_attrs
10953 * sizeof (struct attr_abbrev)));
10954 }
10955
10956 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10957 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10958 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10959 abbrev_ptr += bytes_read;
10960 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10961 abbrev_ptr += bytes_read;
10962 }
10963
10964 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10965 (cur_abbrev->num_attrs
10966 * sizeof (struct attr_abbrev)));
10967 memcpy (cur_abbrev->attrs, cur_attrs,
10968 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10969
10970 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10971 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10972 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10973
10974 /* Get next abbreviation.
10975 Under Irix6 the abbreviations for a compilation unit are not
10976 always properly terminated with an abbrev number of 0.
10977 Exit loop if we encounter an abbreviation which we have
10978 already read (which means we are about to read the abbreviations
10979 for the next compile unit) or if the end of the abbreviation
10980 table is reached. */
10981 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10982 >= abbrev_section->size)
10983 break;
10984 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10985 abbrev_ptr += bytes_read;
10986 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10987 break;
10988 }
10989
10990 xfree (cur_attrs);
10991 }
10992
10993 /* Release the memory used by the abbrev table for a compilation unit. */
10994
10995 static void
10996 dwarf2_free_abbrev_table (void *ptr_to_cu)
10997 {
10998 struct dwarf2_cu *cu = ptr_to_cu;
10999
11000 obstack_free (&cu->abbrev_obstack, NULL);
11001 cu->dwarf2_abbrevs = NULL;
11002 }
11003
11004 /* Lookup an abbrev_info structure in the abbrev hash table. */
11005
11006 static struct abbrev_info *
11007 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
11008 {
11009 unsigned int hash_number;
11010 struct abbrev_info *abbrev;
11011
11012 hash_number = number % ABBREV_HASH_SIZE;
11013 abbrev = cu->dwarf2_abbrevs[hash_number];
11014
11015 while (abbrev)
11016 {
11017 if (abbrev->number == number)
11018 return abbrev;
11019 else
11020 abbrev = abbrev->next;
11021 }
11022 return NULL;
11023 }
11024
11025 /* Returns nonzero if TAG represents a type that we might generate a partial
11026 symbol for. */
11027
11028 static int
11029 is_type_tag_for_partial (int tag)
11030 {
11031 switch (tag)
11032 {
11033 #if 0
11034 /* Some types that would be reasonable to generate partial symbols for,
11035 that we don't at present. */
11036 case DW_TAG_array_type:
11037 case DW_TAG_file_type:
11038 case DW_TAG_ptr_to_member_type:
11039 case DW_TAG_set_type:
11040 case DW_TAG_string_type:
11041 case DW_TAG_subroutine_type:
11042 #endif
11043 case DW_TAG_base_type:
11044 case DW_TAG_class_type:
11045 case DW_TAG_interface_type:
11046 case DW_TAG_enumeration_type:
11047 case DW_TAG_structure_type:
11048 case DW_TAG_subrange_type:
11049 case DW_TAG_typedef:
11050 case DW_TAG_union_type:
11051 return 1;
11052 default:
11053 return 0;
11054 }
11055 }
11056
11057 /* Load all DIEs that are interesting for partial symbols into memory. */
11058
11059 static struct partial_die_info *
11060 load_partial_dies (const struct die_reader_specs *reader,
11061 gdb_byte *info_ptr, int building_psymtab)
11062 {
11063 struct dwarf2_cu *cu = reader->cu;
11064 struct objfile *objfile = cu->objfile;
11065 struct partial_die_info *part_die;
11066 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11067 struct abbrev_info *abbrev;
11068 unsigned int bytes_read;
11069 unsigned int load_all = 0;
11070 int nesting_level = 1;
11071
11072 parent_die = NULL;
11073 last_die = NULL;
11074
11075 gdb_assert (cu->per_cu != NULL);
11076 if (cu->per_cu->load_all_dies)
11077 load_all = 1;
11078
11079 cu->partial_dies
11080 = htab_create_alloc_ex (cu->header.length / 12,
11081 partial_die_hash,
11082 partial_die_eq,
11083 NULL,
11084 &cu->comp_unit_obstack,
11085 hashtab_obstack_allocate,
11086 dummy_obstack_deallocate);
11087
11088 part_die = obstack_alloc (&cu->comp_unit_obstack,
11089 sizeof (struct partial_die_info));
11090
11091 while (1)
11092 {
11093 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11094
11095 /* A NULL abbrev means the end of a series of children. */
11096 if (abbrev == NULL)
11097 {
11098 if (--nesting_level == 0)
11099 {
11100 /* PART_DIE was probably the last thing allocated on the
11101 comp_unit_obstack, so we could call obstack_free
11102 here. We don't do that because the waste is small,
11103 and will be cleaned up when we're done with this
11104 compilation unit. This way, we're also more robust
11105 against other users of the comp_unit_obstack. */
11106 return first_die;
11107 }
11108 info_ptr += bytes_read;
11109 last_die = parent_die;
11110 parent_die = parent_die->die_parent;
11111 continue;
11112 }
11113
11114 /* Check for template arguments. We never save these; if
11115 they're seen, we just mark the parent, and go on our way. */
11116 if (parent_die != NULL
11117 && cu->language == language_cplus
11118 && (abbrev->tag == DW_TAG_template_type_param
11119 || abbrev->tag == DW_TAG_template_value_param))
11120 {
11121 parent_die->has_template_arguments = 1;
11122
11123 if (!load_all)
11124 {
11125 /* We don't need a partial DIE for the template argument. */
11126 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11127 continue;
11128 }
11129 }
11130
11131 /* We only recurse into c++ subprograms looking for template arguments.
11132 Skip their other children. */
11133 if (!load_all
11134 && cu->language == language_cplus
11135 && parent_die != NULL
11136 && parent_die->tag == DW_TAG_subprogram)
11137 {
11138 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11139 continue;
11140 }
11141
11142 /* Check whether this DIE is interesting enough to save. Normally
11143 we would not be interested in members here, but there may be
11144 later variables referencing them via DW_AT_specification (for
11145 static members). */
11146 if (!load_all
11147 && !is_type_tag_for_partial (abbrev->tag)
11148 && abbrev->tag != DW_TAG_constant
11149 && abbrev->tag != DW_TAG_enumerator
11150 && abbrev->tag != DW_TAG_subprogram
11151 && abbrev->tag != DW_TAG_lexical_block
11152 && abbrev->tag != DW_TAG_variable
11153 && abbrev->tag != DW_TAG_namespace
11154 && abbrev->tag != DW_TAG_module
11155 && abbrev->tag != DW_TAG_member
11156 && abbrev->tag != DW_TAG_imported_unit)
11157 {
11158 /* Otherwise we skip to the next sibling, if any. */
11159 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11160 continue;
11161 }
11162
11163 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11164 info_ptr);
11165
11166 /* This two-pass algorithm for processing partial symbols has a
11167 high cost in cache pressure. Thus, handle some simple cases
11168 here which cover the majority of C partial symbols. DIEs
11169 which neither have specification tags in them, nor could have
11170 specification tags elsewhere pointing at them, can simply be
11171 processed and discarded.
11172
11173 This segment is also optional; scan_partial_symbols and
11174 add_partial_symbol will handle these DIEs if we chain
11175 them in normally. When compilers which do not emit large
11176 quantities of duplicate debug information are more common,
11177 this code can probably be removed. */
11178
11179 /* Any complete simple types at the top level (pretty much all
11180 of them, for a language without namespaces), can be processed
11181 directly. */
11182 if (parent_die == NULL
11183 && part_die->has_specification == 0
11184 && part_die->is_declaration == 0
11185 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11186 || part_die->tag == DW_TAG_base_type
11187 || part_die->tag == DW_TAG_subrange_type))
11188 {
11189 if (building_psymtab && part_die->name != NULL)
11190 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11191 VAR_DOMAIN, LOC_TYPEDEF,
11192 &objfile->static_psymbols,
11193 0, (CORE_ADDR) 0, cu->language, objfile);
11194 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11195 continue;
11196 }
11197
11198 /* The exception for DW_TAG_typedef with has_children above is
11199 a workaround of GCC PR debug/47510. In the case of this complaint
11200 type_name_no_tag_or_error will error on such types later.
11201
11202 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11203 it could not find the child DIEs referenced later, this is checked
11204 above. In correct DWARF DW_TAG_typedef should have no children. */
11205
11206 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11207 complaint (&symfile_complaints,
11208 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11209 "- DIE at 0x%x [in module %s]"),
11210 part_die->offset.sect_off, objfile->name);
11211
11212 /* If we're at the second level, and we're an enumerator, and
11213 our parent has no specification (meaning possibly lives in a
11214 namespace elsewhere), then we can add the partial symbol now
11215 instead of queueing it. */
11216 if (part_die->tag == DW_TAG_enumerator
11217 && parent_die != NULL
11218 && parent_die->die_parent == NULL
11219 && parent_die->tag == DW_TAG_enumeration_type
11220 && parent_die->has_specification == 0)
11221 {
11222 if (part_die->name == NULL)
11223 complaint (&symfile_complaints,
11224 _("malformed enumerator DIE ignored"));
11225 else if (building_psymtab)
11226 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11227 VAR_DOMAIN, LOC_CONST,
11228 (cu->language == language_cplus
11229 || cu->language == language_java)
11230 ? &objfile->global_psymbols
11231 : &objfile->static_psymbols,
11232 0, (CORE_ADDR) 0, cu->language, objfile);
11233
11234 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11235 continue;
11236 }
11237
11238 /* We'll save this DIE so link it in. */
11239 part_die->die_parent = parent_die;
11240 part_die->die_sibling = NULL;
11241 part_die->die_child = NULL;
11242
11243 if (last_die && last_die == parent_die)
11244 last_die->die_child = part_die;
11245 else if (last_die)
11246 last_die->die_sibling = part_die;
11247
11248 last_die = part_die;
11249
11250 if (first_die == NULL)
11251 first_die = part_die;
11252
11253 /* Maybe add the DIE to the hash table. Not all DIEs that we
11254 find interesting need to be in the hash table, because we
11255 also have the parent/sibling/child chains; only those that we
11256 might refer to by offset later during partial symbol reading.
11257
11258 For now this means things that might have be the target of a
11259 DW_AT_specification, DW_AT_abstract_origin, or
11260 DW_AT_extension. DW_AT_extension will refer only to
11261 namespaces; DW_AT_abstract_origin refers to functions (and
11262 many things under the function DIE, but we do not recurse
11263 into function DIEs during partial symbol reading) and
11264 possibly variables as well; DW_AT_specification refers to
11265 declarations. Declarations ought to have the DW_AT_declaration
11266 flag. It happens that GCC forgets to put it in sometimes, but
11267 only for functions, not for types.
11268
11269 Adding more things than necessary to the hash table is harmless
11270 except for the performance cost. Adding too few will result in
11271 wasted time in find_partial_die, when we reread the compilation
11272 unit with load_all_dies set. */
11273
11274 if (load_all
11275 || abbrev->tag == DW_TAG_constant
11276 || abbrev->tag == DW_TAG_subprogram
11277 || abbrev->tag == DW_TAG_variable
11278 || abbrev->tag == DW_TAG_namespace
11279 || part_die->is_declaration)
11280 {
11281 void **slot;
11282
11283 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11284 part_die->offset.sect_off, INSERT);
11285 *slot = part_die;
11286 }
11287
11288 part_die = obstack_alloc (&cu->comp_unit_obstack,
11289 sizeof (struct partial_die_info));
11290
11291 /* For some DIEs we want to follow their children (if any). For C
11292 we have no reason to follow the children of structures; for other
11293 languages we have to, so that we can get at method physnames
11294 to infer fully qualified class names, for DW_AT_specification,
11295 and for C++ template arguments. For C++, we also look one level
11296 inside functions to find template arguments (if the name of the
11297 function does not already contain the template arguments).
11298
11299 For Ada, we need to scan the children of subprograms and lexical
11300 blocks as well because Ada allows the definition of nested
11301 entities that could be interesting for the debugger, such as
11302 nested subprograms for instance. */
11303 if (last_die->has_children
11304 && (load_all
11305 || last_die->tag == DW_TAG_namespace
11306 || last_die->tag == DW_TAG_module
11307 || last_die->tag == DW_TAG_enumeration_type
11308 || (cu->language == language_cplus
11309 && last_die->tag == DW_TAG_subprogram
11310 && (last_die->name == NULL
11311 || strchr (last_die->name, '<') == NULL))
11312 || (cu->language != language_c
11313 && (last_die->tag == DW_TAG_class_type
11314 || last_die->tag == DW_TAG_interface_type
11315 || last_die->tag == DW_TAG_structure_type
11316 || last_die->tag == DW_TAG_union_type))
11317 || (cu->language == language_ada
11318 && (last_die->tag == DW_TAG_subprogram
11319 || last_die->tag == DW_TAG_lexical_block))))
11320 {
11321 nesting_level++;
11322 parent_die = last_die;
11323 continue;
11324 }
11325
11326 /* Otherwise we skip to the next sibling, if any. */
11327 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11328
11329 /* Back to the top, do it again. */
11330 }
11331 }
11332
11333 /* Read a minimal amount of information into the minimal die structure. */
11334
11335 static gdb_byte *
11336 read_partial_die (const struct die_reader_specs *reader,
11337 struct partial_die_info *part_die,
11338 struct abbrev_info *abbrev, unsigned int abbrev_len,
11339 gdb_byte *info_ptr)
11340 {
11341 struct dwarf2_cu *cu = reader->cu;
11342 struct objfile *objfile = cu->objfile;
11343 gdb_byte *buffer = reader->buffer;
11344 unsigned int i;
11345 struct attribute attr;
11346 int has_low_pc_attr = 0;
11347 int has_high_pc_attr = 0;
11348 int high_pc_relative = 0;
11349
11350 memset (part_die, 0, sizeof (struct partial_die_info));
11351
11352 part_die->offset.sect_off = info_ptr - buffer;
11353
11354 info_ptr += abbrev_len;
11355
11356 if (abbrev == NULL)
11357 return info_ptr;
11358
11359 part_die->tag = abbrev->tag;
11360 part_die->has_children = abbrev->has_children;
11361
11362 for (i = 0; i < abbrev->num_attrs; ++i)
11363 {
11364 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11365
11366 /* Store the data if it is of an attribute we want to keep in a
11367 partial symbol table. */
11368 switch (attr.name)
11369 {
11370 case DW_AT_name:
11371 switch (part_die->tag)
11372 {
11373 case DW_TAG_compile_unit:
11374 case DW_TAG_partial_unit:
11375 case DW_TAG_type_unit:
11376 /* Compilation units have a DW_AT_name that is a filename, not
11377 a source language identifier. */
11378 case DW_TAG_enumeration_type:
11379 case DW_TAG_enumerator:
11380 /* These tags always have simple identifiers already; no need
11381 to canonicalize them. */
11382 part_die->name = DW_STRING (&attr);
11383 break;
11384 default:
11385 part_die->name
11386 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11387 &objfile->objfile_obstack);
11388 break;
11389 }
11390 break;
11391 case DW_AT_linkage_name:
11392 case DW_AT_MIPS_linkage_name:
11393 /* Note that both forms of linkage name might appear. We
11394 assume they will be the same, and we only store the last
11395 one we see. */
11396 if (cu->language == language_ada)
11397 part_die->name = DW_STRING (&attr);
11398 part_die->linkage_name = DW_STRING (&attr);
11399 break;
11400 case DW_AT_low_pc:
11401 has_low_pc_attr = 1;
11402 part_die->lowpc = DW_ADDR (&attr);
11403 break;
11404 case DW_AT_high_pc:
11405 has_high_pc_attr = 1;
11406 if (attr.form == DW_FORM_addr
11407 || attr.form == DW_FORM_GNU_addr_index)
11408 part_die->highpc = DW_ADDR (&attr);
11409 else
11410 {
11411 high_pc_relative = 1;
11412 part_die->highpc = DW_UNSND (&attr);
11413 }
11414 break;
11415 case DW_AT_location:
11416 /* Support the .debug_loc offsets. */
11417 if (attr_form_is_block (&attr))
11418 {
11419 part_die->d.locdesc = DW_BLOCK (&attr);
11420 }
11421 else if (attr_form_is_section_offset (&attr))
11422 {
11423 dwarf2_complex_location_expr_complaint ();
11424 }
11425 else
11426 {
11427 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11428 "partial symbol information");
11429 }
11430 break;
11431 case DW_AT_external:
11432 part_die->is_external = DW_UNSND (&attr);
11433 break;
11434 case DW_AT_declaration:
11435 part_die->is_declaration = DW_UNSND (&attr);
11436 break;
11437 case DW_AT_type:
11438 part_die->has_type = 1;
11439 break;
11440 case DW_AT_abstract_origin:
11441 case DW_AT_specification:
11442 case DW_AT_extension:
11443 part_die->has_specification = 1;
11444 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11445 break;
11446 case DW_AT_sibling:
11447 /* Ignore absolute siblings, they might point outside of
11448 the current compile unit. */
11449 if (attr.form == DW_FORM_ref_addr)
11450 complaint (&symfile_complaints,
11451 _("ignoring absolute DW_AT_sibling"));
11452 else
11453 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11454 break;
11455 case DW_AT_byte_size:
11456 part_die->has_byte_size = 1;
11457 break;
11458 case DW_AT_calling_convention:
11459 /* DWARF doesn't provide a way to identify a program's source-level
11460 entry point. DW_AT_calling_convention attributes are only meant
11461 to describe functions' calling conventions.
11462
11463 However, because it's a necessary piece of information in
11464 Fortran, and because DW_CC_program is the only piece of debugging
11465 information whose definition refers to a 'main program' at all,
11466 several compilers have begun marking Fortran main programs with
11467 DW_CC_program --- even when those functions use the standard
11468 calling conventions.
11469
11470 So until DWARF specifies a way to provide this information and
11471 compilers pick up the new representation, we'll support this
11472 practice. */
11473 if (DW_UNSND (&attr) == DW_CC_program
11474 && cu->language == language_fortran)
11475 {
11476 set_main_name (part_die->name);
11477
11478 /* As this DIE has a static linkage the name would be difficult
11479 to look up later. */
11480 language_of_main = language_fortran;
11481 }
11482 break;
11483 case DW_AT_inline:
11484 if (DW_UNSND (&attr) == DW_INL_inlined
11485 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11486 part_die->may_be_inlined = 1;
11487 break;
11488
11489 case DW_AT_import:
11490 if (part_die->tag == DW_TAG_imported_unit)
11491 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11492 break;
11493
11494 default:
11495 break;
11496 }
11497 }
11498
11499 if (high_pc_relative)
11500 part_die->highpc += part_die->lowpc;
11501
11502 if (has_low_pc_attr && has_high_pc_attr)
11503 {
11504 /* When using the GNU linker, .gnu.linkonce. sections are used to
11505 eliminate duplicate copies of functions and vtables and such.
11506 The linker will arbitrarily choose one and discard the others.
11507 The AT_*_pc values for such functions refer to local labels in
11508 these sections. If the section from that file was discarded, the
11509 labels are not in the output, so the relocs get a value of 0.
11510 If this is a discarded function, mark the pc bounds as invalid,
11511 so that GDB will ignore it. */
11512 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11513 {
11514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11515
11516 complaint (&symfile_complaints,
11517 _("DW_AT_low_pc %s is zero "
11518 "for DIE at 0x%x [in module %s]"),
11519 paddress (gdbarch, part_die->lowpc),
11520 part_die->offset.sect_off, objfile->name);
11521 }
11522 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11523 else if (part_die->lowpc >= part_die->highpc)
11524 {
11525 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11526
11527 complaint (&symfile_complaints,
11528 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11529 "for DIE at 0x%x [in module %s]"),
11530 paddress (gdbarch, part_die->lowpc),
11531 paddress (gdbarch, part_die->highpc),
11532 part_die->offset.sect_off, objfile->name);
11533 }
11534 else
11535 part_die->has_pc_info = 1;
11536 }
11537
11538 return info_ptr;
11539 }
11540
11541 /* Find a cached partial DIE at OFFSET in CU. */
11542
11543 static struct partial_die_info *
11544 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11545 {
11546 struct partial_die_info *lookup_die = NULL;
11547 struct partial_die_info part_die;
11548
11549 part_die.offset = offset;
11550 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11551 offset.sect_off);
11552
11553 return lookup_die;
11554 }
11555
11556 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11557 except in the case of .debug_types DIEs which do not reference
11558 outside their CU (they do however referencing other types via
11559 DW_FORM_ref_sig8). */
11560
11561 static struct partial_die_info *
11562 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11563 {
11564 struct objfile *objfile = cu->objfile;
11565 struct dwarf2_per_cu_data *per_cu = NULL;
11566 struct partial_die_info *pd = NULL;
11567
11568 if (offset_in_cu_p (&cu->header, offset))
11569 {
11570 pd = find_partial_die_in_comp_unit (offset, cu);
11571 if (pd != NULL)
11572 return pd;
11573 /* We missed recording what we needed.
11574 Load all dies and try again. */
11575 per_cu = cu->per_cu;
11576 }
11577 else
11578 {
11579 /* TUs don't reference other CUs/TUs (except via type signatures). */
11580 if (cu->per_cu->is_debug_types)
11581 {
11582 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11583 " external reference to offset 0x%lx [in module %s].\n"),
11584 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11585 bfd_get_filename (objfile->obfd));
11586 }
11587 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11588
11589 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11590 load_partial_comp_unit (per_cu);
11591
11592 per_cu->cu->last_used = 0;
11593 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11594 }
11595
11596 /* If we didn't find it, and not all dies have been loaded,
11597 load them all and try again. */
11598
11599 if (pd == NULL && per_cu->load_all_dies == 0)
11600 {
11601 per_cu->load_all_dies = 1;
11602
11603 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11604 THIS_CU->cu may already be in use. So we can't just free it and
11605 replace its DIEs with the ones we read in. Instead, we leave those
11606 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11607 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11608 set. */
11609 load_partial_comp_unit (per_cu);
11610
11611 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11612 }
11613
11614 if (pd == NULL)
11615 internal_error (__FILE__, __LINE__,
11616 _("could not find partial DIE 0x%x "
11617 "in cache [from module %s]\n"),
11618 offset.sect_off, bfd_get_filename (objfile->obfd));
11619 return pd;
11620 }
11621
11622 /* See if we can figure out if the class lives in a namespace. We do
11623 this by looking for a member function; its demangled name will
11624 contain namespace info, if there is any. */
11625
11626 static void
11627 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11628 struct dwarf2_cu *cu)
11629 {
11630 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11631 what template types look like, because the demangler
11632 frequently doesn't give the same name as the debug info. We
11633 could fix this by only using the demangled name to get the
11634 prefix (but see comment in read_structure_type). */
11635
11636 struct partial_die_info *real_pdi;
11637 struct partial_die_info *child_pdi;
11638
11639 /* If this DIE (this DIE's specification, if any) has a parent, then
11640 we should not do this. We'll prepend the parent's fully qualified
11641 name when we create the partial symbol. */
11642
11643 real_pdi = struct_pdi;
11644 while (real_pdi->has_specification)
11645 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11646
11647 if (real_pdi->die_parent != NULL)
11648 return;
11649
11650 for (child_pdi = struct_pdi->die_child;
11651 child_pdi != NULL;
11652 child_pdi = child_pdi->die_sibling)
11653 {
11654 if (child_pdi->tag == DW_TAG_subprogram
11655 && child_pdi->linkage_name != NULL)
11656 {
11657 char *actual_class_name
11658 = language_class_name_from_physname (cu->language_defn,
11659 child_pdi->linkage_name);
11660 if (actual_class_name != NULL)
11661 {
11662 struct_pdi->name
11663 = obsavestring (actual_class_name,
11664 strlen (actual_class_name),
11665 &cu->objfile->objfile_obstack);
11666 xfree (actual_class_name);
11667 }
11668 break;
11669 }
11670 }
11671 }
11672
11673 /* Adjust PART_DIE before generating a symbol for it. This function
11674 may set the is_external flag or change the DIE's name. */
11675
11676 static void
11677 fixup_partial_die (struct partial_die_info *part_die,
11678 struct dwarf2_cu *cu)
11679 {
11680 /* Once we've fixed up a die, there's no point in doing so again.
11681 This also avoids a memory leak if we were to call
11682 guess_partial_die_structure_name multiple times. */
11683 if (part_die->fixup_called)
11684 return;
11685
11686 /* If we found a reference attribute and the DIE has no name, try
11687 to find a name in the referred to DIE. */
11688
11689 if (part_die->name == NULL && part_die->has_specification)
11690 {
11691 struct partial_die_info *spec_die;
11692
11693 spec_die = find_partial_die (part_die->spec_offset, cu);
11694
11695 fixup_partial_die (spec_die, cu);
11696
11697 if (spec_die->name)
11698 {
11699 part_die->name = spec_die->name;
11700
11701 /* Copy DW_AT_external attribute if it is set. */
11702 if (spec_die->is_external)
11703 part_die->is_external = spec_die->is_external;
11704 }
11705 }
11706
11707 /* Set default names for some unnamed DIEs. */
11708
11709 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11710 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11711
11712 /* If there is no parent die to provide a namespace, and there are
11713 children, see if we can determine the namespace from their linkage
11714 name. */
11715 if (cu->language == language_cplus
11716 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11717 && part_die->die_parent == NULL
11718 && part_die->has_children
11719 && (part_die->tag == DW_TAG_class_type
11720 || part_die->tag == DW_TAG_structure_type
11721 || part_die->tag == DW_TAG_union_type))
11722 guess_partial_die_structure_name (part_die, cu);
11723
11724 /* GCC might emit a nameless struct or union that has a linkage
11725 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11726 if (part_die->name == NULL
11727 && (part_die->tag == DW_TAG_class_type
11728 || part_die->tag == DW_TAG_interface_type
11729 || part_die->tag == DW_TAG_structure_type
11730 || part_die->tag == DW_TAG_union_type)
11731 && part_die->linkage_name != NULL)
11732 {
11733 char *demangled;
11734
11735 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11736 if (demangled)
11737 {
11738 const char *base;
11739
11740 /* Strip any leading namespaces/classes, keep only the base name.
11741 DW_AT_name for named DIEs does not contain the prefixes. */
11742 base = strrchr (demangled, ':');
11743 if (base && base > demangled && base[-1] == ':')
11744 base++;
11745 else
11746 base = demangled;
11747
11748 part_die->name = obsavestring (base, strlen (base),
11749 &cu->objfile->objfile_obstack);
11750 xfree (demangled);
11751 }
11752 }
11753
11754 part_die->fixup_called = 1;
11755 }
11756
11757 /* Read an attribute value described by an attribute form. */
11758
11759 static gdb_byte *
11760 read_attribute_value (const struct die_reader_specs *reader,
11761 struct attribute *attr, unsigned form,
11762 gdb_byte *info_ptr)
11763 {
11764 struct dwarf2_cu *cu = reader->cu;
11765 bfd *abfd = reader->abfd;
11766 struct comp_unit_head *cu_header = &cu->header;
11767 unsigned int bytes_read;
11768 struct dwarf_block *blk;
11769
11770 attr->form = form;
11771 switch (form)
11772 {
11773 case DW_FORM_ref_addr:
11774 if (cu->header.version == 2)
11775 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11776 else
11777 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11778 &cu->header, &bytes_read);
11779 info_ptr += bytes_read;
11780 break;
11781 case DW_FORM_addr:
11782 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11783 info_ptr += bytes_read;
11784 break;
11785 case DW_FORM_block2:
11786 blk = dwarf_alloc_block (cu);
11787 blk->size = read_2_bytes (abfd, info_ptr);
11788 info_ptr += 2;
11789 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11790 info_ptr += blk->size;
11791 DW_BLOCK (attr) = blk;
11792 break;
11793 case DW_FORM_block4:
11794 blk = dwarf_alloc_block (cu);
11795 blk->size = read_4_bytes (abfd, info_ptr);
11796 info_ptr += 4;
11797 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11798 info_ptr += blk->size;
11799 DW_BLOCK (attr) = blk;
11800 break;
11801 case DW_FORM_data2:
11802 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11803 info_ptr += 2;
11804 break;
11805 case DW_FORM_data4:
11806 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11807 info_ptr += 4;
11808 break;
11809 case DW_FORM_data8:
11810 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11811 info_ptr += 8;
11812 break;
11813 case DW_FORM_sec_offset:
11814 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11815 info_ptr += bytes_read;
11816 break;
11817 case DW_FORM_string:
11818 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11819 DW_STRING_IS_CANONICAL (attr) = 0;
11820 info_ptr += bytes_read;
11821 break;
11822 case DW_FORM_strp:
11823 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11824 &bytes_read);
11825 DW_STRING_IS_CANONICAL (attr) = 0;
11826 info_ptr += bytes_read;
11827 break;
11828 case DW_FORM_exprloc:
11829 case DW_FORM_block:
11830 blk = dwarf_alloc_block (cu);
11831 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11832 info_ptr += bytes_read;
11833 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11834 info_ptr += blk->size;
11835 DW_BLOCK (attr) = blk;
11836 break;
11837 case DW_FORM_block1:
11838 blk = dwarf_alloc_block (cu);
11839 blk->size = read_1_byte (abfd, info_ptr);
11840 info_ptr += 1;
11841 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11842 info_ptr += blk->size;
11843 DW_BLOCK (attr) = blk;
11844 break;
11845 case DW_FORM_data1:
11846 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11847 info_ptr += 1;
11848 break;
11849 case DW_FORM_flag:
11850 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11851 info_ptr += 1;
11852 break;
11853 case DW_FORM_flag_present:
11854 DW_UNSND (attr) = 1;
11855 break;
11856 case DW_FORM_sdata:
11857 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11858 info_ptr += bytes_read;
11859 break;
11860 case DW_FORM_udata:
11861 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11862 info_ptr += bytes_read;
11863 break;
11864 case DW_FORM_ref1:
11865 DW_UNSND (attr) = (cu->header.offset.sect_off
11866 + read_1_byte (abfd, info_ptr));
11867 info_ptr += 1;
11868 break;
11869 case DW_FORM_ref2:
11870 DW_UNSND (attr) = (cu->header.offset.sect_off
11871 + read_2_bytes (abfd, info_ptr));
11872 info_ptr += 2;
11873 break;
11874 case DW_FORM_ref4:
11875 DW_UNSND (attr) = (cu->header.offset.sect_off
11876 + read_4_bytes (abfd, info_ptr));
11877 info_ptr += 4;
11878 break;
11879 case DW_FORM_ref8:
11880 DW_UNSND (attr) = (cu->header.offset.sect_off
11881 + read_8_bytes (abfd, info_ptr));
11882 info_ptr += 8;
11883 break;
11884 case DW_FORM_ref_sig8:
11885 /* Convert the signature to something we can record in DW_UNSND
11886 for later lookup.
11887 NOTE: This is NULL if the type wasn't found. */
11888 DW_SIGNATURED_TYPE (attr) =
11889 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11890 info_ptr += 8;
11891 break;
11892 case DW_FORM_ref_udata:
11893 DW_UNSND (attr) = (cu->header.offset.sect_off
11894 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11895 info_ptr += bytes_read;
11896 break;
11897 case DW_FORM_indirect:
11898 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11899 info_ptr += bytes_read;
11900 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11901 break;
11902 case DW_FORM_GNU_addr_index:
11903 if (reader->dwo_file == NULL)
11904 {
11905 /* For now flag a hard error.
11906 Later we can turn this into a complaint. */
11907 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11908 dwarf_form_name (form),
11909 bfd_get_filename (abfd));
11910 }
11911 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11912 info_ptr += bytes_read;
11913 break;
11914 case DW_FORM_GNU_str_index:
11915 if (reader->dwo_file == NULL)
11916 {
11917 /* For now flag a hard error.
11918 Later we can turn this into a complaint if warranted. */
11919 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11920 dwarf_form_name (form),
11921 bfd_get_filename (abfd));
11922 }
11923 {
11924 ULONGEST str_index =
11925 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11926
11927 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11928 DW_STRING_IS_CANONICAL (attr) = 0;
11929 info_ptr += bytes_read;
11930 }
11931 break;
11932 default:
11933 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11934 dwarf_form_name (form),
11935 bfd_get_filename (abfd));
11936 }
11937
11938 /* We have seen instances where the compiler tried to emit a byte
11939 size attribute of -1 which ended up being encoded as an unsigned
11940 0xffffffff. Although 0xffffffff is technically a valid size value,
11941 an object of this size seems pretty unlikely so we can relatively
11942 safely treat these cases as if the size attribute was invalid and
11943 treat them as zero by default. */
11944 if (attr->name == DW_AT_byte_size
11945 && form == DW_FORM_data4
11946 && DW_UNSND (attr) >= 0xffffffff)
11947 {
11948 complaint
11949 (&symfile_complaints,
11950 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11951 hex_string (DW_UNSND (attr)));
11952 DW_UNSND (attr) = 0;
11953 }
11954
11955 return info_ptr;
11956 }
11957
11958 /* Read an attribute described by an abbreviated attribute. */
11959
11960 static gdb_byte *
11961 read_attribute (const struct die_reader_specs *reader,
11962 struct attribute *attr, struct attr_abbrev *abbrev,
11963 gdb_byte *info_ptr)
11964 {
11965 attr->name = abbrev->name;
11966 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11967 }
11968
11969 /* Read dwarf information from a buffer. */
11970
11971 static unsigned int
11972 read_1_byte (bfd *abfd, gdb_byte *buf)
11973 {
11974 return bfd_get_8 (abfd, buf);
11975 }
11976
11977 static int
11978 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11979 {
11980 return bfd_get_signed_8 (abfd, buf);
11981 }
11982
11983 static unsigned int
11984 read_2_bytes (bfd *abfd, gdb_byte *buf)
11985 {
11986 return bfd_get_16 (abfd, buf);
11987 }
11988
11989 static int
11990 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11991 {
11992 return bfd_get_signed_16 (abfd, buf);
11993 }
11994
11995 static unsigned int
11996 read_4_bytes (bfd *abfd, gdb_byte *buf)
11997 {
11998 return bfd_get_32 (abfd, buf);
11999 }
12000
12001 static int
12002 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12003 {
12004 return bfd_get_signed_32 (abfd, buf);
12005 }
12006
12007 static ULONGEST
12008 read_8_bytes (bfd *abfd, gdb_byte *buf)
12009 {
12010 return bfd_get_64 (abfd, buf);
12011 }
12012
12013 static CORE_ADDR
12014 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
12015 unsigned int *bytes_read)
12016 {
12017 struct comp_unit_head *cu_header = &cu->header;
12018 CORE_ADDR retval = 0;
12019
12020 if (cu_header->signed_addr_p)
12021 {
12022 switch (cu_header->addr_size)
12023 {
12024 case 2:
12025 retval = bfd_get_signed_16 (abfd, buf);
12026 break;
12027 case 4:
12028 retval = bfd_get_signed_32 (abfd, buf);
12029 break;
12030 case 8:
12031 retval = bfd_get_signed_64 (abfd, buf);
12032 break;
12033 default:
12034 internal_error (__FILE__, __LINE__,
12035 _("read_address: bad switch, signed [in module %s]"),
12036 bfd_get_filename (abfd));
12037 }
12038 }
12039 else
12040 {
12041 switch (cu_header->addr_size)
12042 {
12043 case 2:
12044 retval = bfd_get_16 (abfd, buf);
12045 break;
12046 case 4:
12047 retval = bfd_get_32 (abfd, buf);
12048 break;
12049 case 8:
12050 retval = bfd_get_64 (abfd, buf);
12051 break;
12052 default:
12053 internal_error (__FILE__, __LINE__,
12054 _("read_address: bad switch, "
12055 "unsigned [in module %s]"),
12056 bfd_get_filename (abfd));
12057 }
12058 }
12059
12060 *bytes_read = cu_header->addr_size;
12061 return retval;
12062 }
12063
12064 /* Read the initial length from a section. The (draft) DWARF 3
12065 specification allows the initial length to take up either 4 bytes
12066 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12067 bytes describe the length and all offsets will be 8 bytes in length
12068 instead of 4.
12069
12070 An older, non-standard 64-bit format is also handled by this
12071 function. The older format in question stores the initial length
12072 as an 8-byte quantity without an escape value. Lengths greater
12073 than 2^32 aren't very common which means that the initial 4 bytes
12074 is almost always zero. Since a length value of zero doesn't make
12075 sense for the 32-bit format, this initial zero can be considered to
12076 be an escape value which indicates the presence of the older 64-bit
12077 format. As written, the code can't detect (old format) lengths
12078 greater than 4GB. If it becomes necessary to handle lengths
12079 somewhat larger than 4GB, we could allow other small values (such
12080 as the non-sensical values of 1, 2, and 3) to also be used as
12081 escape values indicating the presence of the old format.
12082
12083 The value returned via bytes_read should be used to increment the
12084 relevant pointer after calling read_initial_length().
12085
12086 [ Note: read_initial_length() and read_offset() are based on the
12087 document entitled "DWARF Debugging Information Format", revision
12088 3, draft 8, dated November 19, 2001. This document was obtained
12089 from:
12090
12091 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
12092
12093 This document is only a draft and is subject to change. (So beware.)
12094
12095 Details regarding the older, non-standard 64-bit format were
12096 determined empirically by examining 64-bit ELF files produced by
12097 the SGI toolchain on an IRIX 6.5 machine.
12098
12099 - Kevin, July 16, 2002
12100 ] */
12101
12102 static LONGEST
12103 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
12104 {
12105 LONGEST length = bfd_get_32 (abfd, buf);
12106
12107 if (length == 0xffffffff)
12108 {
12109 length = bfd_get_64 (abfd, buf + 4);
12110 *bytes_read = 12;
12111 }
12112 else if (length == 0)
12113 {
12114 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
12115 length = bfd_get_64 (abfd, buf);
12116 *bytes_read = 8;
12117 }
12118 else
12119 {
12120 *bytes_read = 4;
12121 }
12122
12123 return length;
12124 }
12125
12126 /* Cover function for read_initial_length.
12127 Returns the length of the object at BUF, and stores the size of the
12128 initial length in *BYTES_READ and stores the size that offsets will be in
12129 *OFFSET_SIZE.
12130 If the initial length size is not equivalent to that specified in
12131 CU_HEADER then issue a complaint.
12132 This is useful when reading non-comp-unit headers. */
12133
12134 static LONGEST
12135 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12136 const struct comp_unit_head *cu_header,
12137 unsigned int *bytes_read,
12138 unsigned int *offset_size)
12139 {
12140 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12141
12142 gdb_assert (cu_header->initial_length_size == 4
12143 || cu_header->initial_length_size == 8
12144 || cu_header->initial_length_size == 12);
12145
12146 if (cu_header->initial_length_size != *bytes_read)
12147 complaint (&symfile_complaints,
12148 _("intermixed 32-bit and 64-bit DWARF sections"));
12149
12150 *offset_size = (*bytes_read == 4) ? 4 : 8;
12151 return length;
12152 }
12153
12154 /* Read an offset from the data stream. The size of the offset is
12155 given by cu_header->offset_size. */
12156
12157 static LONGEST
12158 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12159 unsigned int *bytes_read)
12160 {
12161 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12162
12163 *bytes_read = cu_header->offset_size;
12164 return offset;
12165 }
12166
12167 /* Read an offset from the data stream. */
12168
12169 static LONGEST
12170 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12171 {
12172 LONGEST retval = 0;
12173
12174 switch (offset_size)
12175 {
12176 case 4:
12177 retval = bfd_get_32 (abfd, buf);
12178 break;
12179 case 8:
12180 retval = bfd_get_64 (abfd, buf);
12181 break;
12182 default:
12183 internal_error (__FILE__, __LINE__,
12184 _("read_offset_1: bad switch [in module %s]"),
12185 bfd_get_filename (abfd));
12186 }
12187
12188 return retval;
12189 }
12190
12191 static gdb_byte *
12192 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12193 {
12194 /* If the size of a host char is 8 bits, we can return a pointer
12195 to the buffer, otherwise we have to copy the data to a buffer
12196 allocated on the temporary obstack. */
12197 gdb_assert (HOST_CHAR_BIT == 8);
12198 return buf;
12199 }
12200
12201 static char *
12202 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12203 {
12204 /* If the size of a host char is 8 bits, we can return a pointer
12205 to the string, otherwise we have to copy the string to a buffer
12206 allocated on the temporary obstack. */
12207 gdb_assert (HOST_CHAR_BIT == 8);
12208 if (*buf == '\0')
12209 {
12210 *bytes_read_ptr = 1;
12211 return NULL;
12212 }
12213 *bytes_read_ptr = strlen ((char *) buf) + 1;
12214 return (char *) buf;
12215 }
12216
12217 static char *
12218 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12219 {
12220 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12221 if (dwarf2_per_objfile->str.buffer == NULL)
12222 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12223 bfd_get_filename (abfd));
12224 if (str_offset >= dwarf2_per_objfile->str.size)
12225 error (_("DW_FORM_strp pointing outside of "
12226 ".debug_str section [in module %s]"),
12227 bfd_get_filename (abfd));
12228 gdb_assert (HOST_CHAR_BIT == 8);
12229 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12230 return NULL;
12231 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12232 }
12233
12234 static char *
12235 read_indirect_string (bfd *abfd, gdb_byte *buf,
12236 const struct comp_unit_head *cu_header,
12237 unsigned int *bytes_read_ptr)
12238 {
12239 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12240
12241 return read_indirect_string_at_offset (abfd, str_offset);
12242 }
12243
12244 static ULONGEST
12245 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12246 {
12247 ULONGEST result;
12248 unsigned int num_read;
12249 int i, shift;
12250 unsigned char byte;
12251
12252 result = 0;
12253 shift = 0;
12254 num_read = 0;
12255 i = 0;
12256 while (1)
12257 {
12258 byte = bfd_get_8 (abfd, buf);
12259 buf++;
12260 num_read++;
12261 result |= ((ULONGEST) (byte & 127) << shift);
12262 if ((byte & 128) == 0)
12263 {
12264 break;
12265 }
12266 shift += 7;
12267 }
12268 *bytes_read_ptr = num_read;
12269 return result;
12270 }
12271
12272 static LONGEST
12273 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12274 {
12275 LONGEST result;
12276 int i, shift, num_read;
12277 unsigned char byte;
12278
12279 result = 0;
12280 shift = 0;
12281 num_read = 0;
12282 i = 0;
12283 while (1)
12284 {
12285 byte = bfd_get_8 (abfd, buf);
12286 buf++;
12287 num_read++;
12288 result |= ((LONGEST) (byte & 127) << shift);
12289 shift += 7;
12290 if ((byte & 128) == 0)
12291 {
12292 break;
12293 }
12294 }
12295 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12296 result |= -(((LONGEST) 1) << shift);
12297 *bytes_read_ptr = num_read;
12298 return result;
12299 }
12300
12301 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12302 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12303 ADDR_SIZE is the size of addresses from the CU header. */
12304
12305 static CORE_ADDR
12306 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12307 {
12308 struct objfile *objfile = dwarf2_per_objfile->objfile;
12309 bfd *abfd = objfile->obfd;
12310 const gdb_byte *info_ptr;
12311
12312 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12313 if (dwarf2_per_objfile->addr.buffer == NULL)
12314 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12315 objfile->name);
12316 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12317 error (_("DW_FORM_addr_index pointing outside of "
12318 ".debug_addr section [in module %s]"),
12319 objfile->name);
12320 info_ptr = (dwarf2_per_objfile->addr.buffer
12321 + addr_base + addr_index * addr_size);
12322 if (addr_size == 4)
12323 return bfd_get_32 (abfd, info_ptr);
12324 else
12325 return bfd_get_64 (abfd, info_ptr);
12326 }
12327
12328 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12329
12330 static CORE_ADDR
12331 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12332 {
12333 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12334 }
12335
12336 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12337
12338 static CORE_ADDR
12339 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12340 unsigned int *bytes_read)
12341 {
12342 bfd *abfd = cu->objfile->obfd;
12343 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12344
12345 return read_addr_index (cu, addr_index);
12346 }
12347
12348 /* Data structure to pass results from dwarf2_read_addr_index_reader
12349 back to dwarf2_read_addr_index. */
12350
12351 struct dwarf2_read_addr_index_data
12352 {
12353 ULONGEST addr_base;
12354 int addr_size;
12355 };
12356
12357 /* die_reader_func for dwarf2_read_addr_index. */
12358
12359 static void
12360 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12361 gdb_byte *info_ptr,
12362 struct die_info *comp_unit_die,
12363 int has_children,
12364 void *data)
12365 {
12366 struct dwarf2_cu *cu = reader->cu;
12367 struct dwarf2_read_addr_index_data *aidata =
12368 (struct dwarf2_read_addr_index_data *) data;
12369
12370 aidata->addr_base = cu->addr_base;
12371 aidata->addr_size = cu->header.addr_size;
12372 }
12373
12374 /* Given an index in .debug_addr, fetch the value.
12375 NOTE: This can be called during dwarf expression evaluation,
12376 long after the debug information has been read, and thus per_cu->cu
12377 may no longer exist. */
12378
12379 CORE_ADDR
12380 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12381 unsigned int addr_index)
12382 {
12383 struct objfile *objfile = per_cu->objfile;
12384 struct dwarf2_cu *cu = per_cu->cu;
12385 ULONGEST addr_base;
12386 int addr_size;
12387
12388 /* This is intended to be called from outside this file. */
12389 dw2_setup (objfile);
12390
12391 /* We need addr_base and addr_size.
12392 If we don't have PER_CU->cu, we have to get it.
12393 Nasty, but the alternative is storing the needed info in PER_CU,
12394 which at this point doesn't seem justified: it's not clear how frequently
12395 it would get used and it would increase the size of every PER_CU.
12396 Entry points like dwarf2_per_cu_addr_size do a similar thing
12397 so we're not in uncharted territory here.
12398 Alas we need to be a bit more complicated as addr_base is contained
12399 in the DIE.
12400
12401 We don't need to read the entire CU(/TU).
12402 We just need the header and top level die.
12403 IWBN to use the aging mechanism to let us lazily later discard the CU.
12404 See however init_cutu_and_read_dies_simple. */
12405
12406 if (cu != NULL)
12407 {
12408 addr_base = cu->addr_base;
12409 addr_size = cu->header.addr_size;
12410 }
12411 else
12412 {
12413 struct dwarf2_read_addr_index_data aidata;
12414
12415 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12416 &aidata);
12417 addr_base = aidata.addr_base;
12418 addr_size = aidata.addr_size;
12419 }
12420
12421 return read_addr_index_1 (addr_index, addr_base, addr_size);
12422 }
12423
12424 /* Given a DW_AT_str_index, fetch the string. */
12425
12426 static char *
12427 read_str_index (const struct die_reader_specs *reader,
12428 struct dwarf2_cu *cu, ULONGEST str_index)
12429 {
12430 struct objfile *objfile = dwarf2_per_objfile->objfile;
12431 const char *dwo_name = objfile->name;
12432 bfd *abfd = objfile->obfd;
12433 struct dwo_sections *sections = &reader->dwo_file->sections;
12434 gdb_byte *info_ptr;
12435 ULONGEST str_offset;
12436
12437 dwarf2_read_section (objfile, &sections->str);
12438 dwarf2_read_section (objfile, &sections->str_offsets);
12439 if (sections->str.buffer == NULL)
12440 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12441 " in CU at offset 0x%lx [in module %s]"),
12442 (long) cu->header.offset.sect_off, dwo_name);
12443 if (sections->str_offsets.buffer == NULL)
12444 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12445 " in CU at offset 0x%lx [in module %s]"),
12446 (long) cu->header.offset.sect_off, dwo_name);
12447 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12448 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12449 " section in CU at offset 0x%lx [in module %s]"),
12450 (long) cu->header.offset.sect_off, dwo_name);
12451 info_ptr = (sections->str_offsets.buffer
12452 + str_index * cu->header.offset_size);
12453 if (cu->header.offset_size == 4)
12454 str_offset = bfd_get_32 (abfd, info_ptr);
12455 else
12456 str_offset = bfd_get_64 (abfd, info_ptr);
12457 if (str_offset >= sections->str.size)
12458 error (_("Offset from DW_FORM_str_index pointing outside of"
12459 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12460 (long) cu->header.offset.sect_off, dwo_name);
12461 return (char *) (sections->str.buffer + str_offset);
12462 }
12463
12464 /* Return the length of an LEB128 number in BUF. */
12465
12466 static int
12467 leb128_size (const gdb_byte *buf)
12468 {
12469 const gdb_byte *begin = buf;
12470 gdb_byte byte;
12471
12472 while (1)
12473 {
12474 byte = *buf++;
12475 if ((byte & 128) == 0)
12476 return buf - begin;
12477 }
12478 }
12479
12480 static void
12481 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12482 {
12483 switch (lang)
12484 {
12485 case DW_LANG_C89:
12486 case DW_LANG_C99:
12487 case DW_LANG_C:
12488 cu->language = language_c;
12489 break;
12490 case DW_LANG_C_plus_plus:
12491 cu->language = language_cplus;
12492 break;
12493 case DW_LANG_D:
12494 cu->language = language_d;
12495 break;
12496 case DW_LANG_Fortran77:
12497 case DW_LANG_Fortran90:
12498 case DW_LANG_Fortran95:
12499 cu->language = language_fortran;
12500 break;
12501 case DW_LANG_Go:
12502 cu->language = language_go;
12503 break;
12504 case DW_LANG_Mips_Assembler:
12505 cu->language = language_asm;
12506 break;
12507 case DW_LANG_Java:
12508 cu->language = language_java;
12509 break;
12510 case DW_LANG_Ada83:
12511 case DW_LANG_Ada95:
12512 cu->language = language_ada;
12513 break;
12514 case DW_LANG_Modula2:
12515 cu->language = language_m2;
12516 break;
12517 case DW_LANG_Pascal83:
12518 cu->language = language_pascal;
12519 break;
12520 case DW_LANG_ObjC:
12521 cu->language = language_objc;
12522 break;
12523 case DW_LANG_Cobol74:
12524 case DW_LANG_Cobol85:
12525 default:
12526 cu->language = language_minimal;
12527 break;
12528 }
12529 cu->language_defn = language_def (cu->language);
12530 }
12531
12532 /* Return the named attribute or NULL if not there. */
12533
12534 static struct attribute *
12535 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12536 {
12537 for (;;)
12538 {
12539 unsigned int i;
12540 struct attribute *spec = NULL;
12541
12542 for (i = 0; i < die->num_attrs; ++i)
12543 {
12544 if (die->attrs[i].name == name)
12545 return &die->attrs[i];
12546 if (die->attrs[i].name == DW_AT_specification
12547 || die->attrs[i].name == DW_AT_abstract_origin)
12548 spec = &die->attrs[i];
12549 }
12550
12551 if (!spec)
12552 break;
12553
12554 die = follow_die_ref (die, spec, &cu);
12555 }
12556
12557 return NULL;
12558 }
12559
12560 /* Return the named attribute or NULL if not there,
12561 but do not follow DW_AT_specification, etc.
12562 This is for use in contexts where we're reading .debug_types dies.
12563 Following DW_AT_specification, DW_AT_abstract_origin will take us
12564 back up the chain, and we want to go down. */
12565
12566 static struct attribute *
12567 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12568 struct dwarf2_cu *cu)
12569 {
12570 unsigned int i;
12571
12572 for (i = 0; i < die->num_attrs; ++i)
12573 if (die->attrs[i].name == name)
12574 return &die->attrs[i];
12575
12576 return NULL;
12577 }
12578
12579 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12580 and holds a non-zero value. This function should only be used for
12581 DW_FORM_flag or DW_FORM_flag_present attributes. */
12582
12583 static int
12584 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12585 {
12586 struct attribute *attr = dwarf2_attr (die, name, cu);
12587
12588 return (attr && DW_UNSND (attr));
12589 }
12590
12591 static int
12592 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12593 {
12594 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12595 which value is non-zero. However, we have to be careful with
12596 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12597 (via dwarf2_flag_true_p) follows this attribute. So we may
12598 end up accidently finding a declaration attribute that belongs
12599 to a different DIE referenced by the specification attribute,
12600 even though the given DIE does not have a declaration attribute. */
12601 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12602 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12603 }
12604
12605 /* Return the die giving the specification for DIE, if there is
12606 one. *SPEC_CU is the CU containing DIE on input, and the CU
12607 containing the return value on output. If there is no
12608 specification, but there is an abstract origin, that is
12609 returned. */
12610
12611 static struct die_info *
12612 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12613 {
12614 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12615 *spec_cu);
12616
12617 if (spec_attr == NULL)
12618 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12619
12620 if (spec_attr == NULL)
12621 return NULL;
12622 else
12623 return follow_die_ref (die, spec_attr, spec_cu);
12624 }
12625
12626 /* Free the line_header structure *LH, and any arrays and strings it
12627 refers to.
12628 NOTE: This is also used as a "cleanup" function. */
12629
12630 static void
12631 free_line_header (struct line_header *lh)
12632 {
12633 if (lh->standard_opcode_lengths)
12634 xfree (lh->standard_opcode_lengths);
12635
12636 /* Remember that all the lh->file_names[i].name pointers are
12637 pointers into debug_line_buffer, and don't need to be freed. */
12638 if (lh->file_names)
12639 xfree (lh->file_names);
12640
12641 /* Similarly for the include directory names. */
12642 if (lh->include_dirs)
12643 xfree (lh->include_dirs);
12644
12645 xfree (lh);
12646 }
12647
12648 /* Add an entry to LH's include directory table. */
12649
12650 static void
12651 add_include_dir (struct line_header *lh, char *include_dir)
12652 {
12653 /* Grow the array if necessary. */
12654 if (lh->include_dirs_size == 0)
12655 {
12656 lh->include_dirs_size = 1; /* for testing */
12657 lh->include_dirs = xmalloc (lh->include_dirs_size
12658 * sizeof (*lh->include_dirs));
12659 }
12660 else if (lh->num_include_dirs >= lh->include_dirs_size)
12661 {
12662 lh->include_dirs_size *= 2;
12663 lh->include_dirs = xrealloc (lh->include_dirs,
12664 (lh->include_dirs_size
12665 * sizeof (*lh->include_dirs)));
12666 }
12667
12668 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12669 }
12670
12671 /* Add an entry to LH's file name table. */
12672
12673 static void
12674 add_file_name (struct line_header *lh,
12675 char *name,
12676 unsigned int dir_index,
12677 unsigned int mod_time,
12678 unsigned int length)
12679 {
12680 struct file_entry *fe;
12681
12682 /* Grow the array if necessary. */
12683 if (lh->file_names_size == 0)
12684 {
12685 lh->file_names_size = 1; /* for testing */
12686 lh->file_names = xmalloc (lh->file_names_size
12687 * sizeof (*lh->file_names));
12688 }
12689 else if (lh->num_file_names >= lh->file_names_size)
12690 {
12691 lh->file_names_size *= 2;
12692 lh->file_names = xrealloc (lh->file_names,
12693 (lh->file_names_size
12694 * sizeof (*lh->file_names)));
12695 }
12696
12697 fe = &lh->file_names[lh->num_file_names++];
12698 fe->name = name;
12699 fe->dir_index = dir_index;
12700 fe->mod_time = mod_time;
12701 fe->length = length;
12702 fe->included_p = 0;
12703 fe->symtab = NULL;
12704 }
12705
12706 /* Read the statement program header starting at OFFSET in
12707 .debug_line, or .debug_line.dwo. Return a pointer
12708 to a struct line_header, allocated using xmalloc.
12709
12710 NOTE: the strings in the include directory and file name tables of
12711 the returned object point into the dwarf line section buffer,
12712 and must not be freed. */
12713
12714 static struct line_header *
12715 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12716 {
12717 struct cleanup *back_to;
12718 struct line_header *lh;
12719 gdb_byte *line_ptr;
12720 unsigned int bytes_read, offset_size;
12721 int i;
12722 char *cur_dir, *cur_file;
12723 struct dwarf2_section_info *section;
12724 bfd *abfd;
12725
12726 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12727 DWO file. */
12728 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12729 section = &cu->dwo_unit->dwo_file->sections.line;
12730 else
12731 section = &dwarf2_per_objfile->line;
12732
12733 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12734 if (section->buffer == NULL)
12735 {
12736 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12737 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12738 else
12739 complaint (&symfile_complaints, _("missing .debug_line section"));
12740 return 0;
12741 }
12742
12743 /* We can't do this until we know the section is non-empty.
12744 Only then do we know we have such a section. */
12745 abfd = section->asection->owner;
12746
12747 /* Make sure that at least there's room for the total_length field.
12748 That could be 12 bytes long, but we're just going to fudge that. */
12749 if (offset + 4 >= section->size)
12750 {
12751 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12752 return 0;
12753 }
12754
12755 lh = xmalloc (sizeof (*lh));
12756 memset (lh, 0, sizeof (*lh));
12757 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12758 (void *) lh);
12759
12760 line_ptr = section->buffer + offset;
12761
12762 /* Read in the header. */
12763 lh->total_length =
12764 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12765 &bytes_read, &offset_size);
12766 line_ptr += bytes_read;
12767 if (line_ptr + lh->total_length > (section->buffer + section->size))
12768 {
12769 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12770 return 0;
12771 }
12772 lh->statement_program_end = line_ptr + lh->total_length;
12773 lh->version = read_2_bytes (abfd, line_ptr);
12774 line_ptr += 2;
12775 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12776 line_ptr += offset_size;
12777 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12778 line_ptr += 1;
12779 if (lh->version >= 4)
12780 {
12781 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12782 line_ptr += 1;
12783 }
12784 else
12785 lh->maximum_ops_per_instruction = 1;
12786
12787 if (lh->maximum_ops_per_instruction == 0)
12788 {
12789 lh->maximum_ops_per_instruction = 1;
12790 complaint (&symfile_complaints,
12791 _("invalid maximum_ops_per_instruction "
12792 "in `.debug_line' section"));
12793 }
12794
12795 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12796 line_ptr += 1;
12797 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12798 line_ptr += 1;
12799 lh->line_range = read_1_byte (abfd, line_ptr);
12800 line_ptr += 1;
12801 lh->opcode_base = read_1_byte (abfd, line_ptr);
12802 line_ptr += 1;
12803 lh->standard_opcode_lengths
12804 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12805
12806 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12807 for (i = 1; i < lh->opcode_base; ++i)
12808 {
12809 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12810 line_ptr += 1;
12811 }
12812
12813 /* Read directory table. */
12814 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12815 {
12816 line_ptr += bytes_read;
12817 add_include_dir (lh, cur_dir);
12818 }
12819 line_ptr += bytes_read;
12820
12821 /* Read file name table. */
12822 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12823 {
12824 unsigned int dir_index, mod_time, length;
12825
12826 line_ptr += bytes_read;
12827 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12828 line_ptr += bytes_read;
12829 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12830 line_ptr += bytes_read;
12831 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12832 line_ptr += bytes_read;
12833
12834 add_file_name (lh, cur_file, dir_index, mod_time, length);
12835 }
12836 line_ptr += bytes_read;
12837 lh->statement_program_start = line_ptr;
12838
12839 if (line_ptr > (section->buffer + section->size))
12840 complaint (&symfile_complaints,
12841 _("line number info header doesn't "
12842 "fit in `.debug_line' section"));
12843
12844 discard_cleanups (back_to);
12845 return lh;
12846 }
12847
12848 /* Subroutine of dwarf_decode_lines to simplify it.
12849 Return the file name of the psymtab for included file FILE_INDEX
12850 in line header LH of PST.
12851 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12852 If space for the result is malloc'd, it will be freed by a cleanup.
12853 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12854
12855 static char *
12856 psymtab_include_file_name (const struct line_header *lh, int file_index,
12857 const struct partial_symtab *pst,
12858 const char *comp_dir)
12859 {
12860 const struct file_entry fe = lh->file_names [file_index];
12861 char *include_name = fe.name;
12862 char *include_name_to_compare = include_name;
12863 char *dir_name = NULL;
12864 const char *pst_filename;
12865 char *copied_name = NULL;
12866 int file_is_pst;
12867
12868 if (fe.dir_index)
12869 dir_name = lh->include_dirs[fe.dir_index - 1];
12870
12871 if (!IS_ABSOLUTE_PATH (include_name)
12872 && (dir_name != NULL || comp_dir != NULL))
12873 {
12874 /* Avoid creating a duplicate psymtab for PST.
12875 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12876 Before we do the comparison, however, we need to account
12877 for DIR_NAME and COMP_DIR.
12878 First prepend dir_name (if non-NULL). If we still don't
12879 have an absolute path prepend comp_dir (if non-NULL).
12880 However, the directory we record in the include-file's
12881 psymtab does not contain COMP_DIR (to match the
12882 corresponding symtab(s)).
12883
12884 Example:
12885
12886 bash$ cd /tmp
12887 bash$ gcc -g ./hello.c
12888 include_name = "hello.c"
12889 dir_name = "."
12890 DW_AT_comp_dir = comp_dir = "/tmp"
12891 DW_AT_name = "./hello.c" */
12892
12893 if (dir_name != NULL)
12894 {
12895 include_name = concat (dir_name, SLASH_STRING,
12896 include_name, (char *)NULL);
12897 include_name_to_compare = include_name;
12898 make_cleanup (xfree, include_name);
12899 }
12900 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12901 {
12902 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12903 include_name, (char *)NULL);
12904 }
12905 }
12906
12907 pst_filename = pst->filename;
12908 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12909 {
12910 copied_name = concat (pst->dirname, SLASH_STRING,
12911 pst_filename, (char *)NULL);
12912 pst_filename = copied_name;
12913 }
12914
12915 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12916
12917 if (include_name_to_compare != include_name)
12918 xfree (include_name_to_compare);
12919 if (copied_name != NULL)
12920 xfree (copied_name);
12921
12922 if (file_is_pst)
12923 return NULL;
12924 return include_name;
12925 }
12926
12927 /* Ignore this record_line request. */
12928
12929 static void
12930 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12931 {
12932 return;
12933 }
12934
12935 /* Subroutine of dwarf_decode_lines to simplify it.
12936 Process the line number information in LH. */
12937
12938 static void
12939 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12940 struct dwarf2_cu *cu, struct partial_symtab *pst)
12941 {
12942 gdb_byte *line_ptr, *extended_end;
12943 gdb_byte *line_end;
12944 unsigned int bytes_read, extended_len;
12945 unsigned char op_code, extended_op, adj_opcode;
12946 CORE_ADDR baseaddr;
12947 struct objfile *objfile = cu->objfile;
12948 bfd *abfd = objfile->obfd;
12949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12950 const int decode_for_pst_p = (pst != NULL);
12951 struct subfile *last_subfile = NULL;
12952 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12953 = record_line;
12954
12955 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12956
12957 line_ptr = lh->statement_program_start;
12958 line_end = lh->statement_program_end;
12959
12960 /* Read the statement sequences until there's nothing left. */
12961 while (line_ptr < line_end)
12962 {
12963 /* state machine registers */
12964 CORE_ADDR address = 0;
12965 unsigned int file = 1;
12966 unsigned int line = 1;
12967 unsigned int column = 0;
12968 int is_stmt = lh->default_is_stmt;
12969 int basic_block = 0;
12970 int end_sequence = 0;
12971 CORE_ADDR addr;
12972 unsigned char op_index = 0;
12973
12974 if (!decode_for_pst_p && lh->num_file_names >= file)
12975 {
12976 /* Start a subfile for the current file of the state machine. */
12977 /* lh->include_dirs and lh->file_names are 0-based, but the
12978 directory and file name numbers in the statement program
12979 are 1-based. */
12980 struct file_entry *fe = &lh->file_names[file - 1];
12981 char *dir = NULL;
12982
12983 if (fe->dir_index)
12984 dir = lh->include_dirs[fe->dir_index - 1];
12985
12986 dwarf2_start_subfile (fe->name, dir, comp_dir);
12987 }
12988
12989 /* Decode the table. */
12990 while (!end_sequence)
12991 {
12992 op_code = read_1_byte (abfd, line_ptr);
12993 line_ptr += 1;
12994 if (line_ptr > line_end)
12995 {
12996 dwarf2_debug_line_missing_end_sequence_complaint ();
12997 break;
12998 }
12999
13000 if (op_code >= lh->opcode_base)
13001 {
13002 /* Special operand. */
13003 adj_opcode = op_code - lh->opcode_base;
13004 address += (((op_index + (adj_opcode / lh->line_range))
13005 / lh->maximum_ops_per_instruction)
13006 * lh->minimum_instruction_length);
13007 op_index = ((op_index + (adj_opcode / lh->line_range))
13008 % lh->maximum_ops_per_instruction);
13009 line += lh->line_base + (adj_opcode % lh->line_range);
13010 if (lh->num_file_names < file || file == 0)
13011 dwarf2_debug_line_missing_file_complaint ();
13012 /* For now we ignore lines not starting on an
13013 instruction boundary. */
13014 else if (op_index == 0)
13015 {
13016 lh->file_names[file - 1].included_p = 1;
13017 if (!decode_for_pst_p && is_stmt)
13018 {
13019 if (last_subfile != current_subfile)
13020 {
13021 addr = gdbarch_addr_bits_remove (gdbarch, address);
13022 if (last_subfile)
13023 (*p_record_line) (last_subfile, 0, addr);
13024 last_subfile = current_subfile;
13025 }
13026 /* Append row to matrix using current values. */
13027 addr = gdbarch_addr_bits_remove (gdbarch, address);
13028 (*p_record_line) (current_subfile, line, addr);
13029 }
13030 }
13031 basic_block = 0;
13032 }
13033 else switch (op_code)
13034 {
13035 case DW_LNS_extended_op:
13036 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13037 &bytes_read);
13038 line_ptr += bytes_read;
13039 extended_end = line_ptr + extended_len;
13040 extended_op = read_1_byte (abfd, line_ptr);
13041 line_ptr += 1;
13042 switch (extended_op)
13043 {
13044 case DW_LNE_end_sequence:
13045 p_record_line = record_line;
13046 end_sequence = 1;
13047 break;
13048 case DW_LNE_set_address:
13049 address = read_address (abfd, line_ptr, cu, &bytes_read);
13050
13051 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13052 {
13053 /* This line table is for a function which has been
13054 GCd by the linker. Ignore it. PR gdb/12528 */
13055
13056 long line_offset
13057 = line_ptr - dwarf2_per_objfile->line.buffer;
13058
13059 complaint (&symfile_complaints,
13060 _(".debug_line address at offset 0x%lx is 0 "
13061 "[in module %s]"),
13062 line_offset, objfile->name);
13063 p_record_line = noop_record_line;
13064 }
13065
13066 op_index = 0;
13067 line_ptr += bytes_read;
13068 address += baseaddr;
13069 break;
13070 case DW_LNE_define_file:
13071 {
13072 char *cur_file;
13073 unsigned int dir_index, mod_time, length;
13074
13075 cur_file = read_direct_string (abfd, line_ptr,
13076 &bytes_read);
13077 line_ptr += bytes_read;
13078 dir_index =
13079 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13080 line_ptr += bytes_read;
13081 mod_time =
13082 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13083 line_ptr += bytes_read;
13084 length =
13085 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13086 line_ptr += bytes_read;
13087 add_file_name (lh, cur_file, dir_index, mod_time, length);
13088 }
13089 break;
13090 case DW_LNE_set_discriminator:
13091 /* The discriminator is not interesting to the debugger;
13092 just ignore it. */
13093 line_ptr = extended_end;
13094 break;
13095 default:
13096 complaint (&symfile_complaints,
13097 _("mangled .debug_line section"));
13098 return;
13099 }
13100 /* Make sure that we parsed the extended op correctly. If e.g.
13101 we expected a different address size than the producer used,
13102 we may have read the wrong number of bytes. */
13103 if (line_ptr != extended_end)
13104 {
13105 complaint (&symfile_complaints,
13106 _("mangled .debug_line section"));
13107 return;
13108 }
13109 break;
13110 case DW_LNS_copy:
13111 if (lh->num_file_names < file || file == 0)
13112 dwarf2_debug_line_missing_file_complaint ();
13113 else
13114 {
13115 lh->file_names[file - 1].included_p = 1;
13116 if (!decode_for_pst_p && is_stmt)
13117 {
13118 if (last_subfile != current_subfile)
13119 {
13120 addr = gdbarch_addr_bits_remove (gdbarch, address);
13121 if (last_subfile)
13122 (*p_record_line) (last_subfile, 0, addr);
13123 last_subfile = current_subfile;
13124 }
13125 addr = gdbarch_addr_bits_remove (gdbarch, address);
13126 (*p_record_line) (current_subfile, line, addr);
13127 }
13128 }
13129 basic_block = 0;
13130 break;
13131 case DW_LNS_advance_pc:
13132 {
13133 CORE_ADDR adjust
13134 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13135
13136 address += (((op_index + adjust)
13137 / lh->maximum_ops_per_instruction)
13138 * lh->minimum_instruction_length);
13139 op_index = ((op_index + adjust)
13140 % lh->maximum_ops_per_instruction);
13141 line_ptr += bytes_read;
13142 }
13143 break;
13144 case DW_LNS_advance_line:
13145 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13146 line_ptr += bytes_read;
13147 break;
13148 case DW_LNS_set_file:
13149 {
13150 /* The arrays lh->include_dirs and lh->file_names are
13151 0-based, but the directory and file name numbers in
13152 the statement program are 1-based. */
13153 struct file_entry *fe;
13154 char *dir = NULL;
13155
13156 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13157 line_ptr += bytes_read;
13158 if (lh->num_file_names < file || file == 0)
13159 dwarf2_debug_line_missing_file_complaint ();
13160 else
13161 {
13162 fe = &lh->file_names[file - 1];
13163 if (fe->dir_index)
13164 dir = lh->include_dirs[fe->dir_index - 1];
13165 if (!decode_for_pst_p)
13166 {
13167 last_subfile = current_subfile;
13168 dwarf2_start_subfile (fe->name, dir, comp_dir);
13169 }
13170 }
13171 }
13172 break;
13173 case DW_LNS_set_column:
13174 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13175 line_ptr += bytes_read;
13176 break;
13177 case DW_LNS_negate_stmt:
13178 is_stmt = (!is_stmt);
13179 break;
13180 case DW_LNS_set_basic_block:
13181 basic_block = 1;
13182 break;
13183 /* Add to the address register of the state machine the
13184 address increment value corresponding to special opcode
13185 255. I.e., this value is scaled by the minimum
13186 instruction length since special opcode 255 would have
13187 scaled the increment. */
13188 case DW_LNS_const_add_pc:
13189 {
13190 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13191
13192 address += (((op_index + adjust)
13193 / lh->maximum_ops_per_instruction)
13194 * lh->minimum_instruction_length);
13195 op_index = ((op_index + adjust)
13196 % lh->maximum_ops_per_instruction);
13197 }
13198 break;
13199 case DW_LNS_fixed_advance_pc:
13200 address += read_2_bytes (abfd, line_ptr);
13201 op_index = 0;
13202 line_ptr += 2;
13203 break;
13204 default:
13205 {
13206 /* Unknown standard opcode, ignore it. */
13207 int i;
13208
13209 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13210 {
13211 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13212 line_ptr += bytes_read;
13213 }
13214 }
13215 }
13216 }
13217 if (lh->num_file_names < file || file == 0)
13218 dwarf2_debug_line_missing_file_complaint ();
13219 else
13220 {
13221 lh->file_names[file - 1].included_p = 1;
13222 if (!decode_for_pst_p)
13223 {
13224 addr = gdbarch_addr_bits_remove (gdbarch, address);
13225 (*p_record_line) (current_subfile, 0, addr);
13226 }
13227 }
13228 }
13229 }
13230
13231 /* Decode the Line Number Program (LNP) for the given line_header
13232 structure and CU. The actual information extracted and the type
13233 of structures created from the LNP depends on the value of PST.
13234
13235 1. If PST is NULL, then this procedure uses the data from the program
13236 to create all necessary symbol tables, and their linetables.
13237
13238 2. If PST is not NULL, this procedure reads the program to determine
13239 the list of files included by the unit represented by PST, and
13240 builds all the associated partial symbol tables.
13241
13242 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13243 It is used for relative paths in the line table.
13244 NOTE: When processing partial symtabs (pst != NULL),
13245 comp_dir == pst->dirname.
13246
13247 NOTE: It is important that psymtabs have the same file name (via strcmp)
13248 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13249 symtab we don't use it in the name of the psymtabs we create.
13250 E.g. expand_line_sal requires this when finding psymtabs to expand.
13251 A good testcase for this is mb-inline.exp. */
13252
13253 static void
13254 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13255 struct dwarf2_cu *cu, struct partial_symtab *pst,
13256 int want_line_info)
13257 {
13258 struct objfile *objfile = cu->objfile;
13259 const int decode_for_pst_p = (pst != NULL);
13260 struct subfile *first_subfile = current_subfile;
13261
13262 if (want_line_info)
13263 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13264
13265 if (decode_for_pst_p)
13266 {
13267 int file_index;
13268
13269 /* Now that we're done scanning the Line Header Program, we can
13270 create the psymtab of each included file. */
13271 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13272 if (lh->file_names[file_index].included_p == 1)
13273 {
13274 char *include_name =
13275 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13276 if (include_name != NULL)
13277 dwarf2_create_include_psymtab (include_name, pst, objfile);
13278 }
13279 }
13280 else
13281 {
13282 /* Make sure a symtab is created for every file, even files
13283 which contain only variables (i.e. no code with associated
13284 line numbers). */
13285 int i;
13286
13287 for (i = 0; i < lh->num_file_names; i++)
13288 {
13289 char *dir = NULL;
13290 struct file_entry *fe;
13291
13292 fe = &lh->file_names[i];
13293 if (fe->dir_index)
13294 dir = lh->include_dirs[fe->dir_index - 1];
13295 dwarf2_start_subfile (fe->name, dir, comp_dir);
13296
13297 /* Skip the main file; we don't need it, and it must be
13298 allocated last, so that it will show up before the
13299 non-primary symtabs in the objfile's symtab list. */
13300 if (current_subfile == first_subfile)
13301 continue;
13302
13303 if (current_subfile->symtab == NULL)
13304 current_subfile->symtab = allocate_symtab (current_subfile->name,
13305 objfile);
13306 fe->symtab = current_subfile->symtab;
13307 }
13308 }
13309 }
13310
13311 /* Start a subfile for DWARF. FILENAME is the name of the file and
13312 DIRNAME the name of the source directory which contains FILENAME
13313 or NULL if not known. COMP_DIR is the compilation directory for the
13314 linetable's compilation unit or NULL if not known.
13315 This routine tries to keep line numbers from identical absolute and
13316 relative file names in a common subfile.
13317
13318 Using the `list' example from the GDB testsuite, which resides in
13319 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13320 of /srcdir/list0.c yields the following debugging information for list0.c:
13321
13322 DW_AT_name: /srcdir/list0.c
13323 DW_AT_comp_dir: /compdir
13324 files.files[0].name: list0.h
13325 files.files[0].dir: /srcdir
13326 files.files[1].name: list0.c
13327 files.files[1].dir: /srcdir
13328
13329 The line number information for list0.c has to end up in a single
13330 subfile, so that `break /srcdir/list0.c:1' works as expected.
13331 start_subfile will ensure that this happens provided that we pass the
13332 concatenation of files.files[1].dir and files.files[1].name as the
13333 subfile's name. */
13334
13335 static void
13336 dwarf2_start_subfile (char *filename, const char *dirname,
13337 const char *comp_dir)
13338 {
13339 char *fullname;
13340
13341 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13342 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13343 second argument to start_subfile. To be consistent, we do the
13344 same here. In order not to lose the line information directory,
13345 we concatenate it to the filename when it makes sense.
13346 Note that the Dwarf3 standard says (speaking of filenames in line
13347 information): ``The directory index is ignored for file names
13348 that represent full path names''. Thus ignoring dirname in the
13349 `else' branch below isn't an issue. */
13350
13351 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13352 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13353 else
13354 fullname = filename;
13355
13356 start_subfile (fullname, comp_dir);
13357
13358 if (fullname != filename)
13359 xfree (fullname);
13360 }
13361
13362 static void
13363 var_decode_location (struct attribute *attr, struct symbol *sym,
13364 struct dwarf2_cu *cu)
13365 {
13366 struct objfile *objfile = cu->objfile;
13367 struct comp_unit_head *cu_header = &cu->header;
13368
13369 /* NOTE drow/2003-01-30: There used to be a comment and some special
13370 code here to turn a symbol with DW_AT_external and a
13371 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13372 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13373 with some versions of binutils) where shared libraries could have
13374 relocations against symbols in their debug information - the
13375 minimal symbol would have the right address, but the debug info
13376 would not. It's no longer necessary, because we will explicitly
13377 apply relocations when we read in the debug information now. */
13378
13379 /* A DW_AT_location attribute with no contents indicates that a
13380 variable has been optimized away. */
13381 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13382 {
13383 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13384 return;
13385 }
13386
13387 /* Handle one degenerate form of location expression specially, to
13388 preserve GDB's previous behavior when section offsets are
13389 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13390 then mark this symbol as LOC_STATIC. */
13391
13392 if (attr_form_is_block (attr)
13393 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13394 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13395 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13396 && (DW_BLOCK (attr)->size
13397 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13398 {
13399 unsigned int dummy;
13400
13401 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13402 SYMBOL_VALUE_ADDRESS (sym) =
13403 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13404 else
13405 SYMBOL_VALUE_ADDRESS (sym) =
13406 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13407 SYMBOL_CLASS (sym) = LOC_STATIC;
13408 fixup_symbol_section (sym, objfile);
13409 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13410 SYMBOL_SECTION (sym));
13411 return;
13412 }
13413
13414 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13415 expression evaluator, and use LOC_COMPUTED only when necessary
13416 (i.e. when the value of a register or memory location is
13417 referenced, or a thread-local block, etc.). Then again, it might
13418 not be worthwhile. I'm assuming that it isn't unless performance
13419 or memory numbers show me otherwise. */
13420
13421 dwarf2_symbol_mark_computed (attr, sym, cu);
13422 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13423
13424 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13425 cu->has_loclist = 1;
13426 }
13427
13428 /* Given a pointer to a DWARF information entry, figure out if we need
13429 to make a symbol table entry for it, and if so, create a new entry
13430 and return a pointer to it.
13431 If TYPE is NULL, determine symbol type from the die, otherwise
13432 used the passed type.
13433 If SPACE is not NULL, use it to hold the new symbol. If it is
13434 NULL, allocate a new symbol on the objfile's obstack. */
13435
13436 static struct symbol *
13437 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13438 struct symbol *space)
13439 {
13440 struct objfile *objfile = cu->objfile;
13441 struct symbol *sym = NULL;
13442 char *name;
13443 struct attribute *attr = NULL;
13444 struct attribute *attr2 = NULL;
13445 CORE_ADDR baseaddr;
13446 struct pending **list_to_add = NULL;
13447
13448 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13449
13450 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13451
13452 name = dwarf2_name (die, cu);
13453 if (name)
13454 {
13455 const char *linkagename;
13456 int suppress_add = 0;
13457
13458 if (space)
13459 sym = space;
13460 else
13461 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13462 OBJSTAT (objfile, n_syms++);
13463
13464 /* Cache this symbol's name and the name's demangled form (if any). */
13465 SYMBOL_SET_LANGUAGE (sym, cu->language);
13466 linkagename = dwarf2_physname (name, die, cu);
13467 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13468
13469 /* Fortran does not have mangling standard and the mangling does differ
13470 between gfortran, iFort etc. */
13471 if (cu->language == language_fortran
13472 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13473 symbol_set_demangled_name (&(sym->ginfo),
13474 (char *) dwarf2_full_name (name, die, cu),
13475 NULL);
13476
13477 /* Default assumptions.
13478 Use the passed type or decode it from the die. */
13479 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13480 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13481 if (type != NULL)
13482 SYMBOL_TYPE (sym) = type;
13483 else
13484 SYMBOL_TYPE (sym) = die_type (die, cu);
13485 attr = dwarf2_attr (die,
13486 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13487 cu);
13488 if (attr)
13489 {
13490 SYMBOL_LINE (sym) = DW_UNSND (attr);
13491 }
13492
13493 attr = dwarf2_attr (die,
13494 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13495 cu);
13496 if (attr)
13497 {
13498 int file_index = DW_UNSND (attr);
13499
13500 if (cu->line_header == NULL
13501 || file_index > cu->line_header->num_file_names)
13502 complaint (&symfile_complaints,
13503 _("file index out of range"));
13504 else if (file_index > 0)
13505 {
13506 struct file_entry *fe;
13507
13508 fe = &cu->line_header->file_names[file_index - 1];
13509 SYMBOL_SYMTAB (sym) = fe->symtab;
13510 }
13511 }
13512
13513 switch (die->tag)
13514 {
13515 case DW_TAG_label:
13516 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13517 if (attr)
13518 {
13519 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13520 }
13521 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13522 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13523 SYMBOL_CLASS (sym) = LOC_LABEL;
13524 add_symbol_to_list (sym, cu->list_in_scope);
13525 break;
13526 case DW_TAG_subprogram:
13527 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13528 finish_block. */
13529 SYMBOL_CLASS (sym) = LOC_BLOCK;
13530 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13531 if ((attr2 && (DW_UNSND (attr2) != 0))
13532 || cu->language == language_ada)
13533 {
13534 /* Subprograms marked external are stored as a global symbol.
13535 Ada subprograms, whether marked external or not, are always
13536 stored as a global symbol, because we want to be able to
13537 access them globally. For instance, we want to be able
13538 to break on a nested subprogram without having to
13539 specify the context. */
13540 list_to_add = &global_symbols;
13541 }
13542 else
13543 {
13544 list_to_add = cu->list_in_scope;
13545 }
13546 break;
13547 case DW_TAG_inlined_subroutine:
13548 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13549 finish_block. */
13550 SYMBOL_CLASS (sym) = LOC_BLOCK;
13551 SYMBOL_INLINED (sym) = 1;
13552 list_to_add = cu->list_in_scope;
13553 break;
13554 case DW_TAG_template_value_param:
13555 suppress_add = 1;
13556 /* Fall through. */
13557 case DW_TAG_constant:
13558 case DW_TAG_variable:
13559 case DW_TAG_member:
13560 /* Compilation with minimal debug info may result in
13561 variables with missing type entries. Change the
13562 misleading `void' type to something sensible. */
13563 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13564 SYMBOL_TYPE (sym)
13565 = objfile_type (objfile)->nodebug_data_symbol;
13566
13567 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13568 /* In the case of DW_TAG_member, we should only be called for
13569 static const members. */
13570 if (die->tag == DW_TAG_member)
13571 {
13572 /* dwarf2_add_field uses die_is_declaration,
13573 so we do the same. */
13574 gdb_assert (die_is_declaration (die, cu));
13575 gdb_assert (attr);
13576 }
13577 if (attr)
13578 {
13579 dwarf2_const_value (attr, sym, cu);
13580 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13581 if (!suppress_add)
13582 {
13583 if (attr2 && (DW_UNSND (attr2) != 0))
13584 list_to_add = &global_symbols;
13585 else
13586 list_to_add = cu->list_in_scope;
13587 }
13588 break;
13589 }
13590 attr = dwarf2_attr (die, DW_AT_location, cu);
13591 if (attr)
13592 {
13593 var_decode_location (attr, sym, cu);
13594 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13595 if (SYMBOL_CLASS (sym) == LOC_STATIC
13596 && SYMBOL_VALUE_ADDRESS (sym) == 0
13597 && !dwarf2_per_objfile->has_section_at_zero)
13598 {
13599 /* When a static variable is eliminated by the linker,
13600 the corresponding debug information is not stripped
13601 out, but the variable address is set to null;
13602 do not add such variables into symbol table. */
13603 }
13604 else if (attr2 && (DW_UNSND (attr2) != 0))
13605 {
13606 /* Workaround gfortran PR debug/40040 - it uses
13607 DW_AT_location for variables in -fPIC libraries which may
13608 get overriden by other libraries/executable and get
13609 a different address. Resolve it by the minimal symbol
13610 which may come from inferior's executable using copy
13611 relocation. Make this workaround only for gfortran as for
13612 other compilers GDB cannot guess the minimal symbol
13613 Fortran mangling kind. */
13614 if (cu->language == language_fortran && die->parent
13615 && die->parent->tag == DW_TAG_module
13616 && cu->producer
13617 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13618 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13619
13620 /* A variable with DW_AT_external is never static,
13621 but it may be block-scoped. */
13622 list_to_add = (cu->list_in_scope == &file_symbols
13623 ? &global_symbols : cu->list_in_scope);
13624 }
13625 else
13626 list_to_add = cu->list_in_scope;
13627 }
13628 else
13629 {
13630 /* We do not know the address of this symbol.
13631 If it is an external symbol and we have type information
13632 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13633 The address of the variable will then be determined from
13634 the minimal symbol table whenever the variable is
13635 referenced. */
13636 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13637 if (attr2 && (DW_UNSND (attr2) != 0)
13638 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13639 {
13640 /* A variable with DW_AT_external is never static, but it
13641 may be block-scoped. */
13642 list_to_add = (cu->list_in_scope == &file_symbols
13643 ? &global_symbols : cu->list_in_scope);
13644
13645 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13646 }
13647 else if (!die_is_declaration (die, cu))
13648 {
13649 /* Use the default LOC_OPTIMIZED_OUT class. */
13650 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13651 if (!suppress_add)
13652 list_to_add = cu->list_in_scope;
13653 }
13654 }
13655 break;
13656 case DW_TAG_formal_parameter:
13657 /* If we are inside a function, mark this as an argument. If
13658 not, we might be looking at an argument to an inlined function
13659 when we do not have enough information to show inlined frames;
13660 pretend it's a local variable in that case so that the user can
13661 still see it. */
13662 if (context_stack_depth > 0
13663 && context_stack[context_stack_depth - 1].name != NULL)
13664 SYMBOL_IS_ARGUMENT (sym) = 1;
13665 attr = dwarf2_attr (die, DW_AT_location, cu);
13666 if (attr)
13667 {
13668 var_decode_location (attr, sym, cu);
13669 }
13670 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13671 if (attr)
13672 {
13673 dwarf2_const_value (attr, sym, cu);
13674 }
13675
13676 list_to_add = cu->list_in_scope;
13677 break;
13678 case DW_TAG_unspecified_parameters:
13679 /* From varargs functions; gdb doesn't seem to have any
13680 interest in this information, so just ignore it for now.
13681 (FIXME?) */
13682 break;
13683 case DW_TAG_template_type_param:
13684 suppress_add = 1;
13685 /* Fall through. */
13686 case DW_TAG_class_type:
13687 case DW_TAG_interface_type:
13688 case DW_TAG_structure_type:
13689 case DW_TAG_union_type:
13690 case DW_TAG_set_type:
13691 case DW_TAG_enumeration_type:
13692 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13693 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13694
13695 {
13696 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13697 really ever be static objects: otherwise, if you try
13698 to, say, break of a class's method and you're in a file
13699 which doesn't mention that class, it won't work unless
13700 the check for all static symbols in lookup_symbol_aux
13701 saves you. See the OtherFileClass tests in
13702 gdb.c++/namespace.exp. */
13703
13704 if (!suppress_add)
13705 {
13706 list_to_add = (cu->list_in_scope == &file_symbols
13707 && (cu->language == language_cplus
13708 || cu->language == language_java)
13709 ? &global_symbols : cu->list_in_scope);
13710
13711 /* The semantics of C++ state that "struct foo {
13712 ... }" also defines a typedef for "foo". A Java
13713 class declaration also defines a typedef for the
13714 class. */
13715 if (cu->language == language_cplus
13716 || cu->language == language_java
13717 || cu->language == language_ada)
13718 {
13719 /* The symbol's name is already allocated along
13720 with this objfile, so we don't need to
13721 duplicate it for the type. */
13722 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13723 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13724 }
13725 }
13726 }
13727 break;
13728 case DW_TAG_typedef:
13729 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13730 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13731 list_to_add = cu->list_in_scope;
13732 break;
13733 case DW_TAG_base_type:
13734 case DW_TAG_subrange_type:
13735 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13736 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13737 list_to_add = cu->list_in_scope;
13738 break;
13739 case DW_TAG_enumerator:
13740 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13741 if (attr)
13742 {
13743 dwarf2_const_value (attr, sym, cu);
13744 }
13745 {
13746 /* NOTE: carlton/2003-11-10: See comment above in the
13747 DW_TAG_class_type, etc. block. */
13748
13749 list_to_add = (cu->list_in_scope == &file_symbols
13750 && (cu->language == language_cplus
13751 || cu->language == language_java)
13752 ? &global_symbols : cu->list_in_scope);
13753 }
13754 break;
13755 case DW_TAG_namespace:
13756 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13757 list_to_add = &global_symbols;
13758 break;
13759 default:
13760 /* Not a tag we recognize. Hopefully we aren't processing
13761 trash data, but since we must specifically ignore things
13762 we don't recognize, there is nothing else we should do at
13763 this point. */
13764 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13765 dwarf_tag_name (die->tag));
13766 break;
13767 }
13768
13769 if (suppress_add)
13770 {
13771 sym->hash_next = objfile->template_symbols;
13772 objfile->template_symbols = sym;
13773 list_to_add = NULL;
13774 }
13775
13776 if (list_to_add != NULL)
13777 add_symbol_to_list (sym, list_to_add);
13778
13779 /* For the benefit of old versions of GCC, check for anonymous
13780 namespaces based on the demangled name. */
13781 if (!processing_has_namespace_info
13782 && cu->language == language_cplus)
13783 cp_scan_for_anonymous_namespaces (sym, objfile);
13784 }
13785 return (sym);
13786 }
13787
13788 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13789
13790 static struct symbol *
13791 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13792 {
13793 return new_symbol_full (die, type, cu, NULL);
13794 }
13795
13796 /* Given an attr with a DW_FORM_dataN value in host byte order,
13797 zero-extend it as appropriate for the symbol's type. The DWARF
13798 standard (v4) is not entirely clear about the meaning of using
13799 DW_FORM_dataN for a constant with a signed type, where the type is
13800 wider than the data. The conclusion of a discussion on the DWARF
13801 list was that this is unspecified. We choose to always zero-extend
13802 because that is the interpretation long in use by GCC. */
13803
13804 static gdb_byte *
13805 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13806 const char *name, struct obstack *obstack,
13807 struct dwarf2_cu *cu, LONGEST *value, int bits)
13808 {
13809 struct objfile *objfile = cu->objfile;
13810 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13811 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13812 LONGEST l = DW_UNSND (attr);
13813
13814 if (bits < sizeof (*value) * 8)
13815 {
13816 l &= ((LONGEST) 1 << bits) - 1;
13817 *value = l;
13818 }
13819 else if (bits == sizeof (*value) * 8)
13820 *value = l;
13821 else
13822 {
13823 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13824 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13825 return bytes;
13826 }
13827
13828 return NULL;
13829 }
13830
13831 /* Read a constant value from an attribute. Either set *VALUE, or if
13832 the value does not fit in *VALUE, set *BYTES - either already
13833 allocated on the objfile obstack, or newly allocated on OBSTACK,
13834 or, set *BATON, if we translated the constant to a location
13835 expression. */
13836
13837 static void
13838 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13839 const char *name, struct obstack *obstack,
13840 struct dwarf2_cu *cu,
13841 LONGEST *value, gdb_byte **bytes,
13842 struct dwarf2_locexpr_baton **baton)
13843 {
13844 struct objfile *objfile = cu->objfile;
13845 struct comp_unit_head *cu_header = &cu->header;
13846 struct dwarf_block *blk;
13847 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13848 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13849
13850 *value = 0;
13851 *bytes = NULL;
13852 *baton = NULL;
13853
13854 switch (attr->form)
13855 {
13856 case DW_FORM_addr:
13857 case DW_FORM_GNU_addr_index:
13858 {
13859 gdb_byte *data;
13860
13861 if (TYPE_LENGTH (type) != cu_header->addr_size)
13862 dwarf2_const_value_length_mismatch_complaint (name,
13863 cu_header->addr_size,
13864 TYPE_LENGTH (type));
13865 /* Symbols of this form are reasonably rare, so we just
13866 piggyback on the existing location code rather than writing
13867 a new implementation of symbol_computed_ops. */
13868 *baton = obstack_alloc (&objfile->objfile_obstack,
13869 sizeof (struct dwarf2_locexpr_baton));
13870 (*baton)->per_cu = cu->per_cu;
13871 gdb_assert ((*baton)->per_cu);
13872
13873 (*baton)->size = 2 + cu_header->addr_size;
13874 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13875 (*baton)->data = data;
13876
13877 data[0] = DW_OP_addr;
13878 store_unsigned_integer (&data[1], cu_header->addr_size,
13879 byte_order, DW_ADDR (attr));
13880 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13881 }
13882 break;
13883 case DW_FORM_string:
13884 case DW_FORM_strp:
13885 case DW_FORM_GNU_str_index:
13886 /* DW_STRING is already allocated on the objfile obstack, point
13887 directly to it. */
13888 *bytes = (gdb_byte *) DW_STRING (attr);
13889 break;
13890 case DW_FORM_block1:
13891 case DW_FORM_block2:
13892 case DW_FORM_block4:
13893 case DW_FORM_block:
13894 case DW_FORM_exprloc:
13895 blk = DW_BLOCK (attr);
13896 if (TYPE_LENGTH (type) != blk->size)
13897 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13898 TYPE_LENGTH (type));
13899 *bytes = blk->data;
13900 break;
13901
13902 /* The DW_AT_const_value attributes are supposed to carry the
13903 symbol's value "represented as it would be on the target
13904 architecture." By the time we get here, it's already been
13905 converted to host endianness, so we just need to sign- or
13906 zero-extend it as appropriate. */
13907 case DW_FORM_data1:
13908 *bytes = dwarf2_const_value_data (attr, type, name,
13909 obstack, cu, value, 8);
13910 break;
13911 case DW_FORM_data2:
13912 *bytes = dwarf2_const_value_data (attr, type, name,
13913 obstack, cu, value, 16);
13914 break;
13915 case DW_FORM_data4:
13916 *bytes = dwarf2_const_value_data (attr, type, name,
13917 obstack, cu, value, 32);
13918 break;
13919 case DW_FORM_data8:
13920 *bytes = dwarf2_const_value_data (attr, type, name,
13921 obstack, cu, value, 64);
13922 break;
13923
13924 case DW_FORM_sdata:
13925 *value = DW_SND (attr);
13926 break;
13927
13928 case DW_FORM_udata:
13929 *value = DW_UNSND (attr);
13930 break;
13931
13932 default:
13933 complaint (&symfile_complaints,
13934 _("unsupported const value attribute form: '%s'"),
13935 dwarf_form_name (attr->form));
13936 *value = 0;
13937 break;
13938 }
13939 }
13940
13941
13942 /* Copy constant value from an attribute to a symbol. */
13943
13944 static void
13945 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13946 struct dwarf2_cu *cu)
13947 {
13948 struct objfile *objfile = cu->objfile;
13949 struct comp_unit_head *cu_header = &cu->header;
13950 LONGEST value;
13951 gdb_byte *bytes;
13952 struct dwarf2_locexpr_baton *baton;
13953
13954 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13955 SYMBOL_PRINT_NAME (sym),
13956 &objfile->objfile_obstack, cu,
13957 &value, &bytes, &baton);
13958
13959 if (baton != NULL)
13960 {
13961 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13962 SYMBOL_LOCATION_BATON (sym) = baton;
13963 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13964 }
13965 else if (bytes != NULL)
13966 {
13967 SYMBOL_VALUE_BYTES (sym) = bytes;
13968 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13969 }
13970 else
13971 {
13972 SYMBOL_VALUE (sym) = value;
13973 SYMBOL_CLASS (sym) = LOC_CONST;
13974 }
13975 }
13976
13977 /* Return the type of the die in question using its DW_AT_type attribute. */
13978
13979 static struct type *
13980 die_type (struct die_info *die, struct dwarf2_cu *cu)
13981 {
13982 struct attribute *type_attr;
13983
13984 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13985 if (!type_attr)
13986 {
13987 /* A missing DW_AT_type represents a void type. */
13988 return objfile_type (cu->objfile)->builtin_void;
13989 }
13990
13991 return lookup_die_type (die, type_attr, cu);
13992 }
13993
13994 /* True iff CU's producer generates GNAT Ada auxiliary information
13995 that allows to find parallel types through that information instead
13996 of having to do expensive parallel lookups by type name. */
13997
13998 static int
13999 need_gnat_info (struct dwarf2_cu *cu)
14000 {
14001 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14002 of GNAT produces this auxiliary information, without any indication
14003 that it is produced. Part of enhancing the FSF version of GNAT
14004 to produce that information will be to put in place an indicator
14005 that we can use in order to determine whether the descriptive type
14006 info is available or not. One suggestion that has been made is
14007 to use a new attribute, attached to the CU die. For now, assume
14008 that the descriptive type info is not available. */
14009 return 0;
14010 }
14011
14012 /* Return the auxiliary type of the die in question using its
14013 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14014 attribute is not present. */
14015
14016 static struct type *
14017 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14018 {
14019 struct attribute *type_attr;
14020
14021 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14022 if (!type_attr)
14023 return NULL;
14024
14025 return lookup_die_type (die, type_attr, cu);
14026 }
14027
14028 /* If DIE has a descriptive_type attribute, then set the TYPE's
14029 descriptive type accordingly. */
14030
14031 static void
14032 set_descriptive_type (struct type *type, struct die_info *die,
14033 struct dwarf2_cu *cu)
14034 {
14035 struct type *descriptive_type = die_descriptive_type (die, cu);
14036
14037 if (descriptive_type)
14038 {
14039 ALLOCATE_GNAT_AUX_TYPE (type);
14040 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14041 }
14042 }
14043
14044 /* Return the containing type of the die in question using its
14045 DW_AT_containing_type attribute. */
14046
14047 static struct type *
14048 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14049 {
14050 struct attribute *type_attr;
14051
14052 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
14053 if (!type_attr)
14054 error (_("Dwarf Error: Problem turning containing type into gdb type "
14055 "[in module %s]"), cu->objfile->name);
14056
14057 return lookup_die_type (die, type_attr, cu);
14058 }
14059
14060 /* Look up the type of DIE in CU using its type attribute ATTR.
14061 If there is no type substitute an error marker. */
14062
14063 static struct type *
14064 lookup_die_type (struct die_info *die, struct attribute *attr,
14065 struct dwarf2_cu *cu)
14066 {
14067 struct objfile *objfile = cu->objfile;
14068 struct type *this_type;
14069
14070 /* First see if we have it cached. */
14071
14072 if (is_ref_attr (attr))
14073 {
14074 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14075
14076 this_type = get_die_type_at_offset (offset, cu->per_cu);
14077 }
14078 else if (attr->form == DW_FORM_ref_sig8)
14079 {
14080 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14081
14082 /* sig_type will be NULL if the signatured type is missing from
14083 the debug info. */
14084 if (sig_type == NULL)
14085 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14086 "at 0x%x [in module %s]"),
14087 die->offset.sect_off, objfile->name);
14088
14089 gdb_assert (sig_type->per_cu.is_debug_types);
14090 /* If we haven't filled in type_offset_in_section yet, then we
14091 haven't read the type in yet. */
14092 this_type = NULL;
14093 if (sig_type->type_offset_in_section.sect_off != 0)
14094 {
14095 this_type =
14096 get_die_type_at_offset (sig_type->type_offset_in_section,
14097 &sig_type->per_cu);
14098 }
14099 }
14100 else
14101 {
14102 dump_die_for_error (die);
14103 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
14104 dwarf_attr_name (attr->name), objfile->name);
14105 }
14106
14107 /* If not cached we need to read it in. */
14108
14109 if (this_type == NULL)
14110 {
14111 struct die_info *type_die;
14112 struct dwarf2_cu *type_cu = cu;
14113
14114 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
14115 /* If we found the type now, it's probably because the type came
14116 from an inter-CU reference and the type's CU got expanded before
14117 ours. */
14118 this_type = get_die_type (type_die, type_cu);
14119 if (this_type == NULL)
14120 this_type = read_type_die_1 (type_die, type_cu);
14121 }
14122
14123 /* If we still don't have a type use an error marker. */
14124
14125 if (this_type == NULL)
14126 {
14127 char *message, *saved;
14128
14129 /* read_type_die already issued a complaint. */
14130 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
14131 objfile->name,
14132 cu->header.offset.sect_off,
14133 die->offset.sect_off);
14134 saved = obstack_copy0 (&objfile->objfile_obstack,
14135 message, strlen (message));
14136 xfree (message);
14137
14138 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
14139 }
14140
14141 return this_type;
14142 }
14143
14144 /* Return the type in DIE, CU.
14145 Returns NULL for invalid types.
14146
14147 This first does a lookup in the appropriate type_hash table,
14148 and only reads the die in if necessary.
14149
14150 NOTE: This can be called when reading in partial or full symbols. */
14151
14152 static struct type *
14153 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
14154 {
14155 struct type *this_type;
14156
14157 this_type = get_die_type (die, cu);
14158 if (this_type)
14159 return this_type;
14160
14161 return read_type_die_1 (die, cu);
14162 }
14163
14164 /* Read the type in DIE, CU.
14165 Returns NULL for invalid types. */
14166
14167 static struct type *
14168 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14169 {
14170 struct type *this_type = NULL;
14171
14172 switch (die->tag)
14173 {
14174 case DW_TAG_class_type:
14175 case DW_TAG_interface_type:
14176 case DW_TAG_structure_type:
14177 case DW_TAG_union_type:
14178 this_type = read_structure_type (die, cu);
14179 break;
14180 case DW_TAG_enumeration_type:
14181 this_type = read_enumeration_type (die, cu);
14182 break;
14183 case DW_TAG_subprogram:
14184 case DW_TAG_subroutine_type:
14185 case DW_TAG_inlined_subroutine:
14186 this_type = read_subroutine_type (die, cu);
14187 break;
14188 case DW_TAG_array_type:
14189 this_type = read_array_type (die, cu);
14190 break;
14191 case DW_TAG_set_type:
14192 this_type = read_set_type (die, cu);
14193 break;
14194 case DW_TAG_pointer_type:
14195 this_type = read_tag_pointer_type (die, cu);
14196 break;
14197 case DW_TAG_ptr_to_member_type:
14198 this_type = read_tag_ptr_to_member_type (die, cu);
14199 break;
14200 case DW_TAG_reference_type:
14201 this_type = read_tag_reference_type (die, cu);
14202 break;
14203 case DW_TAG_const_type:
14204 this_type = read_tag_const_type (die, cu);
14205 break;
14206 case DW_TAG_volatile_type:
14207 this_type = read_tag_volatile_type (die, cu);
14208 break;
14209 case DW_TAG_string_type:
14210 this_type = read_tag_string_type (die, cu);
14211 break;
14212 case DW_TAG_typedef:
14213 this_type = read_typedef (die, cu);
14214 break;
14215 case DW_TAG_subrange_type:
14216 this_type = read_subrange_type (die, cu);
14217 break;
14218 case DW_TAG_base_type:
14219 this_type = read_base_type (die, cu);
14220 break;
14221 case DW_TAG_unspecified_type:
14222 this_type = read_unspecified_type (die, cu);
14223 break;
14224 case DW_TAG_namespace:
14225 this_type = read_namespace_type (die, cu);
14226 break;
14227 case DW_TAG_module:
14228 this_type = read_module_type (die, cu);
14229 break;
14230 default:
14231 complaint (&symfile_complaints,
14232 _("unexpected tag in read_type_die: '%s'"),
14233 dwarf_tag_name (die->tag));
14234 break;
14235 }
14236
14237 return this_type;
14238 }
14239
14240 /* See if we can figure out if the class lives in a namespace. We do
14241 this by looking for a member function; its demangled name will
14242 contain namespace info, if there is any.
14243 Return the computed name or NULL.
14244 Space for the result is allocated on the objfile's obstack.
14245 This is the full-die version of guess_partial_die_structure_name.
14246 In this case we know DIE has no useful parent. */
14247
14248 static char *
14249 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14250 {
14251 struct die_info *spec_die;
14252 struct dwarf2_cu *spec_cu;
14253 struct die_info *child;
14254
14255 spec_cu = cu;
14256 spec_die = die_specification (die, &spec_cu);
14257 if (spec_die != NULL)
14258 {
14259 die = spec_die;
14260 cu = spec_cu;
14261 }
14262
14263 for (child = die->child;
14264 child != NULL;
14265 child = child->sibling)
14266 {
14267 if (child->tag == DW_TAG_subprogram)
14268 {
14269 struct attribute *attr;
14270
14271 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14272 if (attr == NULL)
14273 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14274 if (attr != NULL)
14275 {
14276 char *actual_name
14277 = language_class_name_from_physname (cu->language_defn,
14278 DW_STRING (attr));
14279 char *name = NULL;
14280
14281 if (actual_name != NULL)
14282 {
14283 char *die_name = dwarf2_name (die, cu);
14284
14285 if (die_name != NULL
14286 && strcmp (die_name, actual_name) != 0)
14287 {
14288 /* Strip off the class name from the full name.
14289 We want the prefix. */
14290 int die_name_len = strlen (die_name);
14291 int actual_name_len = strlen (actual_name);
14292
14293 /* Test for '::' as a sanity check. */
14294 if (actual_name_len > die_name_len + 2
14295 && actual_name[actual_name_len
14296 - die_name_len - 1] == ':')
14297 name =
14298 obsavestring (actual_name,
14299 actual_name_len - die_name_len - 2,
14300 &cu->objfile->objfile_obstack);
14301 }
14302 }
14303 xfree (actual_name);
14304 return name;
14305 }
14306 }
14307 }
14308
14309 return NULL;
14310 }
14311
14312 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14313 prefix part in such case. See
14314 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14315
14316 static char *
14317 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14318 {
14319 struct attribute *attr;
14320 char *base;
14321
14322 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14323 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14324 return NULL;
14325
14326 attr = dwarf2_attr (die, DW_AT_name, cu);
14327 if (attr != NULL && DW_STRING (attr) != NULL)
14328 return NULL;
14329
14330 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14331 if (attr == NULL)
14332 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14333 if (attr == NULL || DW_STRING (attr) == NULL)
14334 return NULL;
14335
14336 /* dwarf2_name had to be already called. */
14337 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14338
14339 /* Strip the base name, keep any leading namespaces/classes. */
14340 base = strrchr (DW_STRING (attr), ':');
14341 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14342 return "";
14343
14344 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14345 &cu->objfile->objfile_obstack);
14346 }
14347
14348 /* Return the name of the namespace/class that DIE is defined within,
14349 or "" if we can't tell. The caller should not xfree the result.
14350
14351 For example, if we're within the method foo() in the following
14352 code:
14353
14354 namespace N {
14355 class C {
14356 void foo () {
14357 }
14358 };
14359 }
14360
14361 then determine_prefix on foo's die will return "N::C". */
14362
14363 static const char *
14364 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14365 {
14366 struct die_info *parent, *spec_die;
14367 struct dwarf2_cu *spec_cu;
14368 struct type *parent_type;
14369 char *retval;
14370
14371 if (cu->language != language_cplus && cu->language != language_java
14372 && cu->language != language_fortran)
14373 return "";
14374
14375 retval = anonymous_struct_prefix (die, cu);
14376 if (retval)
14377 return retval;
14378
14379 /* We have to be careful in the presence of DW_AT_specification.
14380 For example, with GCC 3.4, given the code
14381
14382 namespace N {
14383 void foo() {
14384 // Definition of N::foo.
14385 }
14386 }
14387
14388 then we'll have a tree of DIEs like this:
14389
14390 1: DW_TAG_compile_unit
14391 2: DW_TAG_namespace // N
14392 3: DW_TAG_subprogram // declaration of N::foo
14393 4: DW_TAG_subprogram // definition of N::foo
14394 DW_AT_specification // refers to die #3
14395
14396 Thus, when processing die #4, we have to pretend that we're in
14397 the context of its DW_AT_specification, namely the contex of die
14398 #3. */
14399 spec_cu = cu;
14400 spec_die = die_specification (die, &spec_cu);
14401 if (spec_die == NULL)
14402 parent = die->parent;
14403 else
14404 {
14405 parent = spec_die->parent;
14406 cu = spec_cu;
14407 }
14408
14409 if (parent == NULL)
14410 return "";
14411 else if (parent->building_fullname)
14412 {
14413 const char *name;
14414 const char *parent_name;
14415
14416 /* It has been seen on RealView 2.2 built binaries,
14417 DW_TAG_template_type_param types actually _defined_ as
14418 children of the parent class:
14419
14420 enum E {};
14421 template class <class Enum> Class{};
14422 Class<enum E> class_e;
14423
14424 1: DW_TAG_class_type (Class)
14425 2: DW_TAG_enumeration_type (E)
14426 3: DW_TAG_enumerator (enum1:0)
14427 3: DW_TAG_enumerator (enum2:1)
14428 ...
14429 2: DW_TAG_template_type_param
14430 DW_AT_type DW_FORM_ref_udata (E)
14431
14432 Besides being broken debug info, it can put GDB into an
14433 infinite loop. Consider:
14434
14435 When we're building the full name for Class<E>, we'll start
14436 at Class, and go look over its template type parameters,
14437 finding E. We'll then try to build the full name of E, and
14438 reach here. We're now trying to build the full name of E,
14439 and look over the parent DIE for containing scope. In the
14440 broken case, if we followed the parent DIE of E, we'd again
14441 find Class, and once again go look at its template type
14442 arguments, etc., etc. Simply don't consider such parent die
14443 as source-level parent of this die (it can't be, the language
14444 doesn't allow it), and break the loop here. */
14445 name = dwarf2_name (die, cu);
14446 parent_name = dwarf2_name (parent, cu);
14447 complaint (&symfile_complaints,
14448 _("template param type '%s' defined within parent '%s'"),
14449 name ? name : "<unknown>",
14450 parent_name ? parent_name : "<unknown>");
14451 return "";
14452 }
14453 else
14454 switch (parent->tag)
14455 {
14456 case DW_TAG_namespace:
14457 parent_type = read_type_die (parent, cu);
14458 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14459 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14460 Work around this problem here. */
14461 if (cu->language == language_cplus
14462 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14463 return "";
14464 /* We give a name to even anonymous namespaces. */
14465 return TYPE_TAG_NAME (parent_type);
14466 case DW_TAG_class_type:
14467 case DW_TAG_interface_type:
14468 case DW_TAG_structure_type:
14469 case DW_TAG_union_type:
14470 case DW_TAG_module:
14471 parent_type = read_type_die (parent, cu);
14472 if (TYPE_TAG_NAME (parent_type) != NULL)
14473 return TYPE_TAG_NAME (parent_type);
14474 else
14475 /* An anonymous structure is only allowed non-static data
14476 members; no typedefs, no member functions, et cetera.
14477 So it does not need a prefix. */
14478 return "";
14479 case DW_TAG_compile_unit:
14480 case DW_TAG_partial_unit:
14481 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14482 if (cu->language == language_cplus
14483 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14484 && die->child != NULL
14485 && (die->tag == DW_TAG_class_type
14486 || die->tag == DW_TAG_structure_type
14487 || die->tag == DW_TAG_union_type))
14488 {
14489 char *name = guess_full_die_structure_name (die, cu);
14490 if (name != NULL)
14491 return name;
14492 }
14493 return "";
14494 default:
14495 return determine_prefix (parent, cu);
14496 }
14497 }
14498
14499 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14500 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14501 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14502 an obconcat, otherwise allocate storage for the result. The CU argument is
14503 used to determine the language and hence, the appropriate separator. */
14504
14505 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14506
14507 static char *
14508 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14509 int physname, struct dwarf2_cu *cu)
14510 {
14511 const char *lead = "";
14512 const char *sep;
14513
14514 if (suffix == NULL || suffix[0] == '\0'
14515 || prefix == NULL || prefix[0] == '\0')
14516 sep = "";
14517 else if (cu->language == language_java)
14518 sep = ".";
14519 else if (cu->language == language_fortran && physname)
14520 {
14521 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14522 DW_AT_MIPS_linkage_name is preferred and used instead. */
14523
14524 lead = "__";
14525 sep = "_MOD_";
14526 }
14527 else
14528 sep = "::";
14529
14530 if (prefix == NULL)
14531 prefix = "";
14532 if (suffix == NULL)
14533 suffix = "";
14534
14535 if (obs == NULL)
14536 {
14537 char *retval
14538 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14539
14540 strcpy (retval, lead);
14541 strcat (retval, prefix);
14542 strcat (retval, sep);
14543 strcat (retval, suffix);
14544 return retval;
14545 }
14546 else
14547 {
14548 /* We have an obstack. */
14549 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14550 }
14551 }
14552
14553 /* Return sibling of die, NULL if no sibling. */
14554
14555 static struct die_info *
14556 sibling_die (struct die_info *die)
14557 {
14558 return die->sibling;
14559 }
14560
14561 /* Get name of a die, return NULL if not found. */
14562
14563 static char *
14564 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14565 struct obstack *obstack)
14566 {
14567 if (name && cu->language == language_cplus)
14568 {
14569 char *canon_name = cp_canonicalize_string (name);
14570
14571 if (canon_name != NULL)
14572 {
14573 if (strcmp (canon_name, name) != 0)
14574 name = obsavestring (canon_name, strlen (canon_name),
14575 obstack);
14576 xfree (canon_name);
14577 }
14578 }
14579
14580 return name;
14581 }
14582
14583 /* Get name of a die, return NULL if not found. */
14584
14585 static char *
14586 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14587 {
14588 struct attribute *attr;
14589
14590 attr = dwarf2_attr (die, DW_AT_name, cu);
14591 if ((!attr || !DW_STRING (attr))
14592 && die->tag != DW_TAG_class_type
14593 && die->tag != DW_TAG_interface_type
14594 && die->tag != DW_TAG_structure_type
14595 && die->tag != DW_TAG_union_type)
14596 return NULL;
14597
14598 switch (die->tag)
14599 {
14600 case DW_TAG_compile_unit:
14601 case DW_TAG_partial_unit:
14602 /* Compilation units have a DW_AT_name that is a filename, not
14603 a source language identifier. */
14604 case DW_TAG_enumeration_type:
14605 case DW_TAG_enumerator:
14606 /* These tags always have simple identifiers already; no need
14607 to canonicalize them. */
14608 return DW_STRING (attr);
14609
14610 case DW_TAG_subprogram:
14611 /* Java constructors will all be named "<init>", so return
14612 the class name when we see this special case. */
14613 if (cu->language == language_java
14614 && DW_STRING (attr) != NULL
14615 && strcmp (DW_STRING (attr), "<init>") == 0)
14616 {
14617 struct dwarf2_cu *spec_cu = cu;
14618 struct die_info *spec_die;
14619
14620 /* GCJ will output '<init>' for Java constructor names.
14621 For this special case, return the name of the parent class. */
14622
14623 /* GCJ may output suprogram DIEs with AT_specification set.
14624 If so, use the name of the specified DIE. */
14625 spec_die = die_specification (die, &spec_cu);
14626 if (spec_die != NULL)
14627 return dwarf2_name (spec_die, spec_cu);
14628
14629 do
14630 {
14631 die = die->parent;
14632 if (die->tag == DW_TAG_class_type)
14633 return dwarf2_name (die, cu);
14634 }
14635 while (die->tag != DW_TAG_compile_unit
14636 && die->tag != DW_TAG_partial_unit);
14637 }
14638 break;
14639
14640 case DW_TAG_class_type:
14641 case DW_TAG_interface_type:
14642 case DW_TAG_structure_type:
14643 case DW_TAG_union_type:
14644 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14645 structures or unions. These were of the form "._%d" in GCC 4.1,
14646 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14647 and GCC 4.4. We work around this problem by ignoring these. */
14648 if (attr && DW_STRING (attr)
14649 && (strncmp (DW_STRING (attr), "._", 2) == 0
14650 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14651 return NULL;
14652
14653 /* GCC might emit a nameless typedef that has a linkage name. See
14654 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14655 if (!attr || DW_STRING (attr) == NULL)
14656 {
14657 char *demangled = NULL;
14658
14659 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14660 if (attr == NULL)
14661 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14662
14663 if (attr == NULL || DW_STRING (attr) == NULL)
14664 return NULL;
14665
14666 /* Avoid demangling DW_STRING (attr) the second time on a second
14667 call for the same DIE. */
14668 if (!DW_STRING_IS_CANONICAL (attr))
14669 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14670
14671 if (demangled)
14672 {
14673 char *base;
14674
14675 /* FIXME: we already did this for the partial symbol... */
14676 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14677 &cu->objfile->objfile_obstack);
14678 DW_STRING_IS_CANONICAL (attr) = 1;
14679 xfree (demangled);
14680
14681 /* Strip any leading namespaces/classes, keep only the base name.
14682 DW_AT_name for named DIEs does not contain the prefixes. */
14683 base = strrchr (DW_STRING (attr), ':');
14684 if (base && base > DW_STRING (attr) && base[-1] == ':')
14685 return &base[1];
14686 else
14687 return DW_STRING (attr);
14688 }
14689 }
14690 break;
14691
14692 default:
14693 break;
14694 }
14695
14696 if (!DW_STRING_IS_CANONICAL (attr))
14697 {
14698 DW_STRING (attr)
14699 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14700 &cu->objfile->objfile_obstack);
14701 DW_STRING_IS_CANONICAL (attr) = 1;
14702 }
14703 return DW_STRING (attr);
14704 }
14705
14706 /* Return the die that this die in an extension of, or NULL if there
14707 is none. *EXT_CU is the CU containing DIE on input, and the CU
14708 containing the return value on output. */
14709
14710 static struct die_info *
14711 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14712 {
14713 struct attribute *attr;
14714
14715 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14716 if (attr == NULL)
14717 return NULL;
14718
14719 return follow_die_ref (die, attr, ext_cu);
14720 }
14721
14722 /* Convert a DIE tag into its string name. */
14723
14724 static const char *
14725 dwarf_tag_name (unsigned tag)
14726 {
14727 const char *name = get_DW_TAG_name (tag);
14728
14729 if (name == NULL)
14730 return "DW_TAG_<unknown>";
14731
14732 return name;
14733 }
14734
14735 /* Convert a DWARF attribute code into its string name. */
14736
14737 static const char *
14738 dwarf_attr_name (unsigned attr)
14739 {
14740 const char *name;
14741
14742 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14743 if (attr == DW_AT_MIPS_fde)
14744 return "DW_AT_MIPS_fde";
14745 #else
14746 if (attr == DW_AT_HP_block_index)
14747 return "DW_AT_HP_block_index";
14748 #endif
14749
14750 name = get_DW_AT_name (attr);
14751
14752 if (name == NULL)
14753 return "DW_AT_<unknown>";
14754
14755 return name;
14756 }
14757
14758 /* Convert a DWARF value form code into its string name. */
14759
14760 static const char *
14761 dwarf_form_name (unsigned form)
14762 {
14763 const char *name = get_DW_FORM_name (form);
14764
14765 if (name == NULL)
14766 return "DW_FORM_<unknown>";
14767
14768 return name;
14769 }
14770
14771 static char *
14772 dwarf_bool_name (unsigned mybool)
14773 {
14774 if (mybool)
14775 return "TRUE";
14776 else
14777 return "FALSE";
14778 }
14779
14780 /* Convert a DWARF type code into its string name. */
14781
14782 static const char *
14783 dwarf_type_encoding_name (unsigned enc)
14784 {
14785 const char *name = get_DW_ATE_name (enc);
14786
14787 if (name == NULL)
14788 return "DW_ATE_<unknown>";
14789
14790 return name;
14791 }
14792
14793 static void
14794 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14795 {
14796 unsigned int i;
14797
14798 print_spaces (indent, f);
14799 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14800 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14801
14802 if (die->parent != NULL)
14803 {
14804 print_spaces (indent, f);
14805 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14806 die->parent->offset.sect_off);
14807 }
14808
14809 print_spaces (indent, f);
14810 fprintf_unfiltered (f, " has children: %s\n",
14811 dwarf_bool_name (die->child != NULL));
14812
14813 print_spaces (indent, f);
14814 fprintf_unfiltered (f, " attributes:\n");
14815
14816 for (i = 0; i < die->num_attrs; ++i)
14817 {
14818 print_spaces (indent, f);
14819 fprintf_unfiltered (f, " %s (%s) ",
14820 dwarf_attr_name (die->attrs[i].name),
14821 dwarf_form_name (die->attrs[i].form));
14822
14823 switch (die->attrs[i].form)
14824 {
14825 case DW_FORM_addr:
14826 case DW_FORM_GNU_addr_index:
14827 fprintf_unfiltered (f, "address: ");
14828 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14829 break;
14830 case DW_FORM_block2:
14831 case DW_FORM_block4:
14832 case DW_FORM_block:
14833 case DW_FORM_block1:
14834 fprintf_unfiltered (f, "block: size %d",
14835 DW_BLOCK (&die->attrs[i])->size);
14836 break;
14837 case DW_FORM_exprloc:
14838 fprintf_unfiltered (f, "expression: size %u",
14839 DW_BLOCK (&die->attrs[i])->size);
14840 break;
14841 case DW_FORM_ref_addr:
14842 fprintf_unfiltered (f, "ref address: ");
14843 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14844 break;
14845 case DW_FORM_ref1:
14846 case DW_FORM_ref2:
14847 case DW_FORM_ref4:
14848 case DW_FORM_ref8:
14849 case DW_FORM_ref_udata:
14850 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14851 (long) (DW_UNSND (&die->attrs[i])));
14852 break;
14853 case DW_FORM_data1:
14854 case DW_FORM_data2:
14855 case DW_FORM_data4:
14856 case DW_FORM_data8:
14857 case DW_FORM_udata:
14858 case DW_FORM_sdata:
14859 fprintf_unfiltered (f, "constant: %s",
14860 pulongest (DW_UNSND (&die->attrs[i])));
14861 break;
14862 case DW_FORM_sec_offset:
14863 fprintf_unfiltered (f, "section offset: %s",
14864 pulongest (DW_UNSND (&die->attrs[i])));
14865 break;
14866 case DW_FORM_ref_sig8:
14867 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14868 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14869 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14870 else
14871 fprintf_unfiltered (f, "signatured type, offset: unknown");
14872 break;
14873 case DW_FORM_string:
14874 case DW_FORM_strp:
14875 case DW_FORM_GNU_str_index:
14876 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14877 DW_STRING (&die->attrs[i])
14878 ? DW_STRING (&die->attrs[i]) : "",
14879 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14880 break;
14881 case DW_FORM_flag:
14882 if (DW_UNSND (&die->attrs[i]))
14883 fprintf_unfiltered (f, "flag: TRUE");
14884 else
14885 fprintf_unfiltered (f, "flag: FALSE");
14886 break;
14887 case DW_FORM_flag_present:
14888 fprintf_unfiltered (f, "flag: TRUE");
14889 break;
14890 case DW_FORM_indirect:
14891 /* The reader will have reduced the indirect form to
14892 the "base form" so this form should not occur. */
14893 fprintf_unfiltered (f,
14894 "unexpected attribute form: DW_FORM_indirect");
14895 break;
14896 default:
14897 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14898 die->attrs[i].form);
14899 break;
14900 }
14901 fprintf_unfiltered (f, "\n");
14902 }
14903 }
14904
14905 static void
14906 dump_die_for_error (struct die_info *die)
14907 {
14908 dump_die_shallow (gdb_stderr, 0, die);
14909 }
14910
14911 static void
14912 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14913 {
14914 int indent = level * 4;
14915
14916 gdb_assert (die != NULL);
14917
14918 if (level >= max_level)
14919 return;
14920
14921 dump_die_shallow (f, indent, die);
14922
14923 if (die->child != NULL)
14924 {
14925 print_spaces (indent, f);
14926 fprintf_unfiltered (f, " Children:");
14927 if (level + 1 < max_level)
14928 {
14929 fprintf_unfiltered (f, "\n");
14930 dump_die_1 (f, level + 1, max_level, die->child);
14931 }
14932 else
14933 {
14934 fprintf_unfiltered (f,
14935 " [not printed, max nesting level reached]\n");
14936 }
14937 }
14938
14939 if (die->sibling != NULL && level > 0)
14940 {
14941 dump_die_1 (f, level, max_level, die->sibling);
14942 }
14943 }
14944
14945 /* This is called from the pdie macro in gdbinit.in.
14946 It's not static so gcc will keep a copy callable from gdb. */
14947
14948 void
14949 dump_die (struct die_info *die, int max_level)
14950 {
14951 dump_die_1 (gdb_stdlog, 0, max_level, die);
14952 }
14953
14954 static void
14955 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14956 {
14957 void **slot;
14958
14959 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14960 INSERT);
14961
14962 *slot = die;
14963 }
14964
14965 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14966 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14967
14968 static int
14969 is_ref_attr (struct attribute *attr)
14970 {
14971 switch (attr->form)
14972 {
14973 case DW_FORM_ref_addr:
14974 case DW_FORM_ref1:
14975 case DW_FORM_ref2:
14976 case DW_FORM_ref4:
14977 case DW_FORM_ref8:
14978 case DW_FORM_ref_udata:
14979 return 1;
14980 default:
14981 return 0;
14982 }
14983 }
14984
14985 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14986 required kind. */
14987
14988 static sect_offset
14989 dwarf2_get_ref_die_offset (struct attribute *attr)
14990 {
14991 sect_offset retval = { DW_UNSND (attr) };
14992
14993 if (is_ref_attr (attr))
14994 return retval;
14995
14996 retval.sect_off = 0;
14997 complaint (&symfile_complaints,
14998 _("unsupported die ref attribute form: '%s'"),
14999 dwarf_form_name (attr->form));
15000 return retval;
15001 }
15002
15003 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15004 * the value held by the attribute is not constant. */
15005
15006 static LONGEST
15007 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15008 {
15009 if (attr->form == DW_FORM_sdata)
15010 return DW_SND (attr);
15011 else if (attr->form == DW_FORM_udata
15012 || attr->form == DW_FORM_data1
15013 || attr->form == DW_FORM_data2
15014 || attr->form == DW_FORM_data4
15015 || attr->form == DW_FORM_data8)
15016 return DW_UNSND (attr);
15017 else
15018 {
15019 complaint (&symfile_complaints,
15020 _("Attribute value is not a constant (%s)"),
15021 dwarf_form_name (attr->form));
15022 return default_value;
15023 }
15024 }
15025
15026 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15027 unit and add it to our queue.
15028 The result is non-zero if PER_CU was queued, otherwise the result is zero
15029 meaning either PER_CU is already queued or it is already loaded. */
15030
15031 static int
15032 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15033 struct dwarf2_per_cu_data *per_cu,
15034 enum language pretend_language)
15035 {
15036 /* We may arrive here during partial symbol reading, if we need full
15037 DIEs to process an unusual case (e.g. template arguments). Do
15038 not queue PER_CU, just tell our caller to load its DIEs. */
15039 if (dwarf2_per_objfile->reading_partial_symbols)
15040 {
15041 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15042 return 1;
15043 return 0;
15044 }
15045
15046 /* Mark the dependence relation so that we don't flush PER_CU
15047 too early. */
15048 dwarf2_add_dependence (this_cu, per_cu);
15049
15050 /* If it's already on the queue, we have nothing to do. */
15051 if (per_cu->queued)
15052 return 0;
15053
15054 /* If the compilation unit is already loaded, just mark it as
15055 used. */
15056 if (per_cu->cu != NULL)
15057 {
15058 per_cu->cu->last_used = 0;
15059 return 0;
15060 }
15061
15062 /* Add it to the queue. */
15063 queue_comp_unit (per_cu, pretend_language);
15064
15065 return 1;
15066 }
15067
15068 /* Follow reference or signature attribute ATTR of SRC_DIE.
15069 On entry *REF_CU is the CU of SRC_DIE.
15070 On exit *REF_CU is the CU of the result. */
15071
15072 static struct die_info *
15073 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15074 struct dwarf2_cu **ref_cu)
15075 {
15076 struct die_info *die;
15077
15078 if (is_ref_attr (attr))
15079 die = follow_die_ref (src_die, attr, ref_cu);
15080 else if (attr->form == DW_FORM_ref_sig8)
15081 die = follow_die_sig (src_die, attr, ref_cu);
15082 else
15083 {
15084 dump_die_for_error (src_die);
15085 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15086 (*ref_cu)->objfile->name);
15087 }
15088
15089 return die;
15090 }
15091
15092 /* Follow reference OFFSET.
15093 On entry *REF_CU is the CU of the source die referencing OFFSET.
15094 On exit *REF_CU is the CU of the result.
15095 Returns NULL if OFFSET is invalid. */
15096
15097 static struct die_info *
15098 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15099 {
15100 struct die_info temp_die;
15101 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15102
15103 gdb_assert (cu->per_cu != NULL);
15104
15105 target_cu = cu;
15106
15107 if (cu->per_cu->is_debug_types)
15108 {
15109 /* .debug_types CUs cannot reference anything outside their CU.
15110 If they need to, they have to reference a signatured type via
15111 DW_FORM_ref_sig8. */
15112 if (! offset_in_cu_p (&cu->header, offset))
15113 return NULL;
15114 }
15115 else if (! offset_in_cu_p (&cu->header, offset))
15116 {
15117 struct dwarf2_per_cu_data *per_cu;
15118
15119 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15120
15121 /* If necessary, add it to the queue and load its DIEs. */
15122 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15123 load_full_comp_unit (per_cu, cu->language);
15124
15125 target_cu = per_cu->cu;
15126 }
15127 else if (cu->dies == NULL)
15128 {
15129 /* We're loading full DIEs during partial symbol reading. */
15130 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15131 load_full_comp_unit (cu->per_cu, language_minimal);
15132 }
15133
15134 *ref_cu = target_cu;
15135 temp_die.offset = offset;
15136 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15137 }
15138
15139 /* Follow reference attribute ATTR of SRC_DIE.
15140 On entry *REF_CU is the CU of SRC_DIE.
15141 On exit *REF_CU is the CU of the result. */
15142
15143 static struct die_info *
15144 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15145 struct dwarf2_cu **ref_cu)
15146 {
15147 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15148 struct dwarf2_cu *cu = *ref_cu;
15149 struct die_info *die;
15150
15151 die = follow_die_offset (offset, ref_cu);
15152 if (!die)
15153 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15154 "at 0x%x [in module %s]"),
15155 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15156
15157 return die;
15158 }
15159
15160 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15161 Returned value is intended for DW_OP_call*. Returned
15162 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15163
15164 struct dwarf2_locexpr_baton
15165 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15166 struct dwarf2_per_cu_data *per_cu,
15167 CORE_ADDR (*get_frame_pc) (void *baton),
15168 void *baton)
15169 {
15170 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15171 struct dwarf2_cu *cu;
15172 struct die_info *die;
15173 struct attribute *attr;
15174 struct dwarf2_locexpr_baton retval;
15175
15176 dw2_setup (per_cu->objfile);
15177
15178 if (per_cu->cu == NULL)
15179 load_cu (per_cu);
15180 cu = per_cu->cu;
15181
15182 die = follow_die_offset (offset, &cu);
15183 if (!die)
15184 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15185 offset.sect_off, per_cu->objfile->name);
15186
15187 attr = dwarf2_attr (die, DW_AT_location, cu);
15188 if (!attr)
15189 {
15190 /* DWARF: "If there is no such attribute, then there is no effect.".
15191 DATA is ignored if SIZE is 0. */
15192
15193 retval.data = NULL;
15194 retval.size = 0;
15195 }
15196 else if (attr_form_is_section_offset (attr))
15197 {
15198 struct dwarf2_loclist_baton loclist_baton;
15199 CORE_ADDR pc = (*get_frame_pc) (baton);
15200 size_t size;
15201
15202 fill_in_loclist_baton (cu, &loclist_baton, attr);
15203
15204 retval.data = dwarf2_find_location_expression (&loclist_baton,
15205 &size, pc);
15206 retval.size = size;
15207 }
15208 else
15209 {
15210 if (!attr_form_is_block (attr))
15211 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15212 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15213 offset.sect_off, per_cu->objfile->name);
15214
15215 retval.data = DW_BLOCK (attr)->data;
15216 retval.size = DW_BLOCK (attr)->size;
15217 }
15218 retval.per_cu = cu->per_cu;
15219
15220 age_cached_comp_units ();
15221
15222 return retval;
15223 }
15224
15225 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15226 PER_CU. */
15227
15228 struct type *
15229 dwarf2_get_die_type (cu_offset die_offset,
15230 struct dwarf2_per_cu_data *per_cu)
15231 {
15232 sect_offset die_offset_sect;
15233
15234 dw2_setup (per_cu->objfile);
15235
15236 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15237 return get_die_type_at_offset (die_offset_sect, per_cu);
15238 }
15239
15240 /* Follow the signature attribute ATTR in SRC_DIE.
15241 On entry *REF_CU is the CU of SRC_DIE.
15242 On exit *REF_CU is the CU of the result. */
15243
15244 static struct die_info *
15245 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15246 struct dwarf2_cu **ref_cu)
15247 {
15248 struct objfile *objfile = (*ref_cu)->objfile;
15249 struct die_info temp_die;
15250 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15251 struct dwarf2_cu *sig_cu;
15252 struct die_info *die;
15253
15254 /* sig_type will be NULL if the signatured type is missing from
15255 the debug info. */
15256 if (sig_type == NULL)
15257 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15258 "at 0x%x [in module %s]"),
15259 src_die->offset.sect_off, objfile->name);
15260
15261 /* If necessary, add it to the queue and load its DIEs. */
15262
15263 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15264 read_signatured_type (sig_type);
15265
15266 gdb_assert (sig_type->per_cu.cu != NULL);
15267
15268 sig_cu = sig_type->per_cu.cu;
15269 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15270 temp_die.offset = sig_type->type_offset_in_section;
15271 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15272 temp_die.offset.sect_off);
15273 if (die)
15274 {
15275 *ref_cu = sig_cu;
15276 return die;
15277 }
15278
15279 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15280 "from DIE at 0x%x [in module %s]"),
15281 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15282 }
15283
15284 /* Given an offset of a signatured type, return its signatured_type. */
15285
15286 static struct signatured_type *
15287 lookup_signatured_type_at_offset (struct objfile *objfile,
15288 struct dwarf2_section_info *section,
15289 sect_offset offset)
15290 {
15291 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15292 unsigned int length, initial_length_size;
15293 unsigned int sig_offset;
15294 struct signatured_type find_entry, *sig_type;
15295
15296 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15297 sig_offset = (initial_length_size
15298 + 2 /*version*/
15299 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15300 + 1 /*address_size*/);
15301 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15302 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15303
15304 /* This is only used to lookup previously recorded types.
15305 If we didn't find it, it's our bug. */
15306 gdb_assert (sig_type != NULL);
15307 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15308
15309 return sig_type;
15310 }
15311
15312 /* Load the DIEs associated with type unit PER_CU into memory. */
15313
15314 static void
15315 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15316 {
15317 struct objfile *objfile = per_cu->objfile;
15318 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15319 sect_offset offset = per_cu->offset;
15320 struct signatured_type *sig_type;
15321
15322 dwarf2_read_section (objfile, sect);
15323
15324 /* We have the section offset, but we need the signature to do the
15325 hash table lookup. */
15326 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15327 the signature to assert we found the right one.
15328 Ok, but it's a lot of work. We should simplify things so any needed
15329 assert doesn't require all this clumsiness. */
15330 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15331
15332 gdb_assert (&sig_type->per_cu == per_cu);
15333 gdb_assert (sig_type->per_cu.cu == NULL);
15334
15335 read_signatured_type (sig_type);
15336
15337 gdb_assert (sig_type->per_cu.cu != NULL);
15338 }
15339
15340 /* die_reader_func for read_signatured_type.
15341 This is identical to load_full_comp_unit_reader,
15342 but is kept separate for now. */
15343
15344 static void
15345 read_signatured_type_reader (const struct die_reader_specs *reader,
15346 gdb_byte *info_ptr,
15347 struct die_info *comp_unit_die,
15348 int has_children,
15349 void *data)
15350 {
15351 struct dwarf2_cu *cu = reader->cu;
15352
15353 gdb_assert (cu->die_hash == NULL);
15354 cu->die_hash =
15355 htab_create_alloc_ex (cu->header.length / 12,
15356 die_hash,
15357 die_eq,
15358 NULL,
15359 &cu->comp_unit_obstack,
15360 hashtab_obstack_allocate,
15361 dummy_obstack_deallocate);
15362
15363 if (has_children)
15364 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15365 &info_ptr, comp_unit_die);
15366 cu->dies = comp_unit_die;
15367 /* comp_unit_die is not stored in die_hash, no need. */
15368
15369 /* We try not to read any attributes in this function, because not
15370 all CUs needed for references have been loaded yet, and symbol
15371 table processing isn't initialized. But we have to set the CU language,
15372 or we won't be able to build types correctly.
15373 Similarly, if we do not read the producer, we can not apply
15374 producer-specific interpretation. */
15375 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15376 }
15377
15378 /* Read in a signatured type and build its CU and DIEs.
15379 If the type is a stub for the real type in a DWO file,
15380 read in the real type from the DWO file as well. */
15381
15382 static void
15383 read_signatured_type (struct signatured_type *sig_type)
15384 {
15385 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15386
15387 gdb_assert (per_cu->is_debug_types);
15388 gdb_assert (per_cu->cu == NULL);
15389
15390 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15391 }
15392
15393 /* Decode simple location descriptions.
15394 Given a pointer to a dwarf block that defines a location, compute
15395 the location and return the value.
15396
15397 NOTE drow/2003-11-18: This function is called in two situations
15398 now: for the address of static or global variables (partial symbols
15399 only) and for offsets into structures which are expected to be
15400 (more or less) constant. The partial symbol case should go away,
15401 and only the constant case should remain. That will let this
15402 function complain more accurately. A few special modes are allowed
15403 without complaint for global variables (for instance, global
15404 register values and thread-local values).
15405
15406 A location description containing no operations indicates that the
15407 object is optimized out. The return value is 0 for that case.
15408 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15409 callers will only want a very basic result and this can become a
15410 complaint.
15411
15412 Note that stack[0] is unused except as a default error return. */
15413
15414 static CORE_ADDR
15415 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15416 {
15417 struct objfile *objfile = cu->objfile;
15418 int i;
15419 int size = blk->size;
15420 gdb_byte *data = blk->data;
15421 CORE_ADDR stack[64];
15422 int stacki;
15423 unsigned int bytes_read, unsnd;
15424 gdb_byte op;
15425
15426 i = 0;
15427 stacki = 0;
15428 stack[stacki] = 0;
15429 stack[++stacki] = 0;
15430
15431 while (i < size)
15432 {
15433 op = data[i++];
15434 switch (op)
15435 {
15436 case DW_OP_lit0:
15437 case DW_OP_lit1:
15438 case DW_OP_lit2:
15439 case DW_OP_lit3:
15440 case DW_OP_lit4:
15441 case DW_OP_lit5:
15442 case DW_OP_lit6:
15443 case DW_OP_lit7:
15444 case DW_OP_lit8:
15445 case DW_OP_lit9:
15446 case DW_OP_lit10:
15447 case DW_OP_lit11:
15448 case DW_OP_lit12:
15449 case DW_OP_lit13:
15450 case DW_OP_lit14:
15451 case DW_OP_lit15:
15452 case DW_OP_lit16:
15453 case DW_OP_lit17:
15454 case DW_OP_lit18:
15455 case DW_OP_lit19:
15456 case DW_OP_lit20:
15457 case DW_OP_lit21:
15458 case DW_OP_lit22:
15459 case DW_OP_lit23:
15460 case DW_OP_lit24:
15461 case DW_OP_lit25:
15462 case DW_OP_lit26:
15463 case DW_OP_lit27:
15464 case DW_OP_lit28:
15465 case DW_OP_lit29:
15466 case DW_OP_lit30:
15467 case DW_OP_lit31:
15468 stack[++stacki] = op - DW_OP_lit0;
15469 break;
15470
15471 case DW_OP_reg0:
15472 case DW_OP_reg1:
15473 case DW_OP_reg2:
15474 case DW_OP_reg3:
15475 case DW_OP_reg4:
15476 case DW_OP_reg5:
15477 case DW_OP_reg6:
15478 case DW_OP_reg7:
15479 case DW_OP_reg8:
15480 case DW_OP_reg9:
15481 case DW_OP_reg10:
15482 case DW_OP_reg11:
15483 case DW_OP_reg12:
15484 case DW_OP_reg13:
15485 case DW_OP_reg14:
15486 case DW_OP_reg15:
15487 case DW_OP_reg16:
15488 case DW_OP_reg17:
15489 case DW_OP_reg18:
15490 case DW_OP_reg19:
15491 case DW_OP_reg20:
15492 case DW_OP_reg21:
15493 case DW_OP_reg22:
15494 case DW_OP_reg23:
15495 case DW_OP_reg24:
15496 case DW_OP_reg25:
15497 case DW_OP_reg26:
15498 case DW_OP_reg27:
15499 case DW_OP_reg28:
15500 case DW_OP_reg29:
15501 case DW_OP_reg30:
15502 case DW_OP_reg31:
15503 stack[++stacki] = op - DW_OP_reg0;
15504 if (i < size)
15505 dwarf2_complex_location_expr_complaint ();
15506 break;
15507
15508 case DW_OP_regx:
15509 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15510 i += bytes_read;
15511 stack[++stacki] = unsnd;
15512 if (i < size)
15513 dwarf2_complex_location_expr_complaint ();
15514 break;
15515
15516 case DW_OP_addr:
15517 stack[++stacki] = read_address (objfile->obfd, &data[i],
15518 cu, &bytes_read);
15519 i += bytes_read;
15520 break;
15521
15522 case DW_OP_const1u:
15523 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15524 i += 1;
15525 break;
15526
15527 case DW_OP_const1s:
15528 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15529 i += 1;
15530 break;
15531
15532 case DW_OP_const2u:
15533 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15534 i += 2;
15535 break;
15536
15537 case DW_OP_const2s:
15538 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15539 i += 2;
15540 break;
15541
15542 case DW_OP_const4u:
15543 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15544 i += 4;
15545 break;
15546
15547 case DW_OP_const4s:
15548 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15549 i += 4;
15550 break;
15551
15552 case DW_OP_const8u:
15553 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15554 i += 8;
15555 break;
15556
15557 case DW_OP_constu:
15558 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15559 &bytes_read);
15560 i += bytes_read;
15561 break;
15562
15563 case DW_OP_consts:
15564 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15565 i += bytes_read;
15566 break;
15567
15568 case DW_OP_dup:
15569 stack[stacki + 1] = stack[stacki];
15570 stacki++;
15571 break;
15572
15573 case DW_OP_plus:
15574 stack[stacki - 1] += stack[stacki];
15575 stacki--;
15576 break;
15577
15578 case DW_OP_plus_uconst:
15579 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15580 &bytes_read);
15581 i += bytes_read;
15582 break;
15583
15584 case DW_OP_minus:
15585 stack[stacki - 1] -= stack[stacki];
15586 stacki--;
15587 break;
15588
15589 case DW_OP_deref:
15590 /* If we're not the last op, then we definitely can't encode
15591 this using GDB's address_class enum. This is valid for partial
15592 global symbols, although the variable's address will be bogus
15593 in the psymtab. */
15594 if (i < size)
15595 dwarf2_complex_location_expr_complaint ();
15596 break;
15597
15598 case DW_OP_GNU_push_tls_address:
15599 /* The top of the stack has the offset from the beginning
15600 of the thread control block at which the variable is located. */
15601 /* Nothing should follow this operator, so the top of stack would
15602 be returned. */
15603 /* This is valid for partial global symbols, but the variable's
15604 address will be bogus in the psymtab. Make it always at least
15605 non-zero to not look as a variable garbage collected by linker
15606 which have DW_OP_addr 0. */
15607 if (i < size)
15608 dwarf2_complex_location_expr_complaint ();
15609 stack[stacki]++;
15610 break;
15611
15612 case DW_OP_GNU_uninit:
15613 break;
15614
15615 case DW_OP_GNU_addr_index:
15616 case DW_OP_GNU_const_index:
15617 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15618 &bytes_read);
15619 i += bytes_read;
15620 break;
15621
15622 default:
15623 {
15624 const char *name = get_DW_OP_name (op);
15625
15626 if (name)
15627 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15628 name);
15629 else
15630 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15631 op);
15632 }
15633
15634 return (stack[stacki]);
15635 }
15636
15637 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15638 outside of the allocated space. Also enforce minimum>0. */
15639 if (stacki >= ARRAY_SIZE (stack) - 1)
15640 {
15641 complaint (&symfile_complaints,
15642 _("location description stack overflow"));
15643 return 0;
15644 }
15645
15646 if (stacki <= 0)
15647 {
15648 complaint (&symfile_complaints,
15649 _("location description stack underflow"));
15650 return 0;
15651 }
15652 }
15653 return (stack[stacki]);
15654 }
15655
15656 /* memory allocation interface */
15657
15658 static struct dwarf_block *
15659 dwarf_alloc_block (struct dwarf2_cu *cu)
15660 {
15661 struct dwarf_block *blk;
15662
15663 blk = (struct dwarf_block *)
15664 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15665 return (blk);
15666 }
15667
15668 static struct abbrev_info *
15669 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15670 {
15671 struct abbrev_info *abbrev;
15672
15673 abbrev = (struct abbrev_info *)
15674 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15675 memset (abbrev, 0, sizeof (struct abbrev_info));
15676 return (abbrev);
15677 }
15678
15679 static struct die_info *
15680 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15681 {
15682 struct die_info *die;
15683 size_t size = sizeof (struct die_info);
15684
15685 if (num_attrs > 1)
15686 size += (num_attrs - 1) * sizeof (struct attribute);
15687
15688 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15689 memset (die, 0, sizeof (struct die_info));
15690 return (die);
15691 }
15692
15693 \f
15694 /* Macro support. */
15695
15696 /* Return the full name of file number I in *LH's file name table.
15697 Use COMP_DIR as the name of the current directory of the
15698 compilation. The result is allocated using xmalloc; the caller is
15699 responsible for freeing it. */
15700 static char *
15701 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15702 {
15703 /* Is the file number a valid index into the line header's file name
15704 table? Remember that file numbers start with one, not zero. */
15705 if (1 <= file && file <= lh->num_file_names)
15706 {
15707 struct file_entry *fe = &lh->file_names[file - 1];
15708
15709 if (IS_ABSOLUTE_PATH (fe->name))
15710 return xstrdup (fe->name);
15711 else
15712 {
15713 const char *dir;
15714 int dir_len;
15715 char *full_name;
15716
15717 if (fe->dir_index)
15718 dir = lh->include_dirs[fe->dir_index - 1];
15719 else
15720 dir = comp_dir;
15721
15722 if (dir)
15723 {
15724 dir_len = strlen (dir);
15725 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15726 strcpy (full_name, dir);
15727 full_name[dir_len] = '/';
15728 strcpy (full_name + dir_len + 1, fe->name);
15729 return full_name;
15730 }
15731 else
15732 return xstrdup (fe->name);
15733 }
15734 }
15735 else
15736 {
15737 /* The compiler produced a bogus file number. We can at least
15738 record the macro definitions made in the file, even if we
15739 won't be able to find the file by name. */
15740 char fake_name[80];
15741
15742 sprintf (fake_name, "<bad macro file number %d>", file);
15743
15744 complaint (&symfile_complaints,
15745 _("bad file number in macro information (%d)"),
15746 file);
15747
15748 return xstrdup (fake_name);
15749 }
15750 }
15751
15752
15753 static struct macro_source_file *
15754 macro_start_file (int file, int line,
15755 struct macro_source_file *current_file,
15756 const char *comp_dir,
15757 struct line_header *lh, struct objfile *objfile)
15758 {
15759 /* The full name of this source file. */
15760 char *full_name = file_full_name (file, lh, comp_dir);
15761
15762 /* We don't create a macro table for this compilation unit
15763 at all until we actually get a filename. */
15764 if (! pending_macros)
15765 pending_macros = new_macro_table (&objfile->objfile_obstack,
15766 objfile->macro_cache);
15767
15768 if (! current_file)
15769 {
15770 /* If we have no current file, then this must be the start_file
15771 directive for the compilation unit's main source file. */
15772 current_file = macro_set_main (pending_macros, full_name);
15773 macro_define_special (pending_macros);
15774 }
15775 else
15776 current_file = macro_include (current_file, line, full_name);
15777
15778 xfree (full_name);
15779
15780 return current_file;
15781 }
15782
15783
15784 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15785 followed by a null byte. */
15786 static char *
15787 copy_string (const char *buf, int len)
15788 {
15789 char *s = xmalloc (len + 1);
15790
15791 memcpy (s, buf, len);
15792 s[len] = '\0';
15793 return s;
15794 }
15795
15796
15797 static const char *
15798 consume_improper_spaces (const char *p, const char *body)
15799 {
15800 if (*p == ' ')
15801 {
15802 complaint (&symfile_complaints,
15803 _("macro definition contains spaces "
15804 "in formal argument list:\n`%s'"),
15805 body);
15806
15807 while (*p == ' ')
15808 p++;
15809 }
15810
15811 return p;
15812 }
15813
15814
15815 static void
15816 parse_macro_definition (struct macro_source_file *file, int line,
15817 const char *body)
15818 {
15819 const char *p;
15820
15821 /* The body string takes one of two forms. For object-like macro
15822 definitions, it should be:
15823
15824 <macro name> " " <definition>
15825
15826 For function-like macro definitions, it should be:
15827
15828 <macro name> "() " <definition>
15829 or
15830 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15831
15832 Spaces may appear only where explicitly indicated, and in the
15833 <definition>.
15834
15835 The Dwarf 2 spec says that an object-like macro's name is always
15836 followed by a space, but versions of GCC around March 2002 omit
15837 the space when the macro's definition is the empty string.
15838
15839 The Dwarf 2 spec says that there should be no spaces between the
15840 formal arguments in a function-like macro's formal argument list,
15841 but versions of GCC around March 2002 include spaces after the
15842 commas. */
15843
15844
15845 /* Find the extent of the macro name. The macro name is terminated
15846 by either a space or null character (for an object-like macro) or
15847 an opening paren (for a function-like macro). */
15848 for (p = body; *p; p++)
15849 if (*p == ' ' || *p == '(')
15850 break;
15851
15852 if (*p == ' ' || *p == '\0')
15853 {
15854 /* It's an object-like macro. */
15855 int name_len = p - body;
15856 char *name = copy_string (body, name_len);
15857 const char *replacement;
15858
15859 if (*p == ' ')
15860 replacement = body + name_len + 1;
15861 else
15862 {
15863 dwarf2_macro_malformed_definition_complaint (body);
15864 replacement = body + name_len;
15865 }
15866
15867 macro_define_object (file, line, name, replacement);
15868
15869 xfree (name);
15870 }
15871 else if (*p == '(')
15872 {
15873 /* It's a function-like macro. */
15874 char *name = copy_string (body, p - body);
15875 int argc = 0;
15876 int argv_size = 1;
15877 char **argv = xmalloc (argv_size * sizeof (*argv));
15878
15879 p++;
15880
15881 p = consume_improper_spaces (p, body);
15882
15883 /* Parse the formal argument list. */
15884 while (*p && *p != ')')
15885 {
15886 /* Find the extent of the current argument name. */
15887 const char *arg_start = p;
15888
15889 while (*p && *p != ',' && *p != ')' && *p != ' ')
15890 p++;
15891
15892 if (! *p || p == arg_start)
15893 dwarf2_macro_malformed_definition_complaint (body);
15894 else
15895 {
15896 /* Make sure argv has room for the new argument. */
15897 if (argc >= argv_size)
15898 {
15899 argv_size *= 2;
15900 argv = xrealloc (argv, argv_size * sizeof (*argv));
15901 }
15902
15903 argv[argc++] = copy_string (arg_start, p - arg_start);
15904 }
15905
15906 p = consume_improper_spaces (p, body);
15907
15908 /* Consume the comma, if present. */
15909 if (*p == ',')
15910 {
15911 p++;
15912
15913 p = consume_improper_spaces (p, body);
15914 }
15915 }
15916
15917 if (*p == ')')
15918 {
15919 p++;
15920
15921 if (*p == ' ')
15922 /* Perfectly formed definition, no complaints. */
15923 macro_define_function (file, line, name,
15924 argc, (const char **) argv,
15925 p + 1);
15926 else if (*p == '\0')
15927 {
15928 /* Complain, but do define it. */
15929 dwarf2_macro_malformed_definition_complaint (body);
15930 macro_define_function (file, line, name,
15931 argc, (const char **) argv,
15932 p);
15933 }
15934 else
15935 /* Just complain. */
15936 dwarf2_macro_malformed_definition_complaint (body);
15937 }
15938 else
15939 /* Just complain. */
15940 dwarf2_macro_malformed_definition_complaint (body);
15941
15942 xfree (name);
15943 {
15944 int i;
15945
15946 for (i = 0; i < argc; i++)
15947 xfree (argv[i]);
15948 }
15949 xfree (argv);
15950 }
15951 else
15952 dwarf2_macro_malformed_definition_complaint (body);
15953 }
15954
15955 /* Skip some bytes from BYTES according to the form given in FORM.
15956 Returns the new pointer. */
15957
15958 static gdb_byte *
15959 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
15960 enum dwarf_form form,
15961 unsigned int offset_size,
15962 struct dwarf2_section_info *section)
15963 {
15964 unsigned int bytes_read;
15965
15966 switch (form)
15967 {
15968 case DW_FORM_data1:
15969 case DW_FORM_flag:
15970 ++bytes;
15971 break;
15972
15973 case DW_FORM_data2:
15974 bytes += 2;
15975 break;
15976
15977 case DW_FORM_data4:
15978 bytes += 4;
15979 break;
15980
15981 case DW_FORM_data8:
15982 bytes += 8;
15983 break;
15984
15985 case DW_FORM_string:
15986 read_direct_string (abfd, bytes, &bytes_read);
15987 bytes += bytes_read;
15988 break;
15989
15990 case DW_FORM_sec_offset:
15991 case DW_FORM_strp:
15992 bytes += offset_size;
15993 break;
15994
15995 case DW_FORM_block:
15996 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15997 bytes += bytes_read;
15998 break;
15999
16000 case DW_FORM_block1:
16001 bytes += 1 + read_1_byte (abfd, bytes);
16002 break;
16003 case DW_FORM_block2:
16004 bytes += 2 + read_2_bytes (abfd, bytes);
16005 break;
16006 case DW_FORM_block4:
16007 bytes += 4 + read_4_bytes (abfd, bytes);
16008 break;
16009
16010 case DW_FORM_sdata:
16011 case DW_FORM_udata:
16012 case DW_FORM_GNU_addr_index:
16013 case DW_FORM_GNU_str_index:
16014 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16015 if (bytes == NULL)
16016 {
16017 dwarf2_section_buffer_overflow_complaint (section);
16018 return NULL;
16019 }
16020 break;
16021
16022 default:
16023 {
16024 complain:
16025 complaint (&symfile_complaints,
16026 _("invalid form 0x%x in `%s'"),
16027 form,
16028 section->asection->name);
16029 return NULL;
16030 }
16031 }
16032
16033 return bytes;
16034 }
16035
16036 /* A helper for dwarf_decode_macros that handles skipping an unknown
16037 opcode. Returns an updated pointer to the macro data buffer; or,
16038 on error, issues a complaint and returns NULL. */
16039
16040 static gdb_byte *
16041 skip_unknown_opcode (unsigned int opcode,
16042 gdb_byte **opcode_definitions,
16043 gdb_byte *mac_ptr, gdb_byte *mac_end,
16044 bfd *abfd,
16045 unsigned int offset_size,
16046 struct dwarf2_section_info *section)
16047 {
16048 unsigned int bytes_read, i;
16049 unsigned long arg;
16050 gdb_byte *defn;
16051
16052 if (opcode_definitions[opcode] == NULL)
16053 {
16054 complaint (&symfile_complaints,
16055 _("unrecognized DW_MACFINO opcode 0x%x"),
16056 opcode);
16057 return NULL;
16058 }
16059
16060 defn = opcode_definitions[opcode];
16061 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16062 defn += bytes_read;
16063
16064 for (i = 0; i < arg; ++i)
16065 {
16066 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16067 section);
16068 if (mac_ptr == NULL)
16069 {
16070 /* skip_form_bytes already issued the complaint. */
16071 return NULL;
16072 }
16073 }
16074
16075 return mac_ptr;
16076 }
16077
16078 /* A helper function which parses the header of a macro section.
16079 If the macro section is the extended (for now called "GNU") type,
16080 then this updates *OFFSET_SIZE. Returns a pointer to just after
16081 the header, or issues a complaint and returns NULL on error. */
16082
16083 static gdb_byte *
16084 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16085 bfd *abfd,
16086 gdb_byte *mac_ptr,
16087 unsigned int *offset_size,
16088 int section_is_gnu)
16089 {
16090 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16091
16092 if (section_is_gnu)
16093 {
16094 unsigned int version, flags;
16095
16096 version = read_2_bytes (abfd, mac_ptr);
16097 if (version != 4)
16098 {
16099 complaint (&symfile_complaints,
16100 _("unrecognized version `%d' in .debug_macro section"),
16101 version);
16102 return NULL;
16103 }
16104 mac_ptr += 2;
16105
16106 flags = read_1_byte (abfd, mac_ptr);
16107 ++mac_ptr;
16108 *offset_size = (flags & 1) ? 8 : 4;
16109
16110 if ((flags & 2) != 0)
16111 /* We don't need the line table offset. */
16112 mac_ptr += *offset_size;
16113
16114 /* Vendor opcode descriptions. */
16115 if ((flags & 4) != 0)
16116 {
16117 unsigned int i, count;
16118
16119 count = read_1_byte (abfd, mac_ptr);
16120 ++mac_ptr;
16121 for (i = 0; i < count; ++i)
16122 {
16123 unsigned int opcode, bytes_read;
16124 unsigned long arg;
16125
16126 opcode = read_1_byte (abfd, mac_ptr);
16127 ++mac_ptr;
16128 opcode_definitions[opcode] = mac_ptr;
16129 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16130 mac_ptr += bytes_read;
16131 mac_ptr += arg;
16132 }
16133 }
16134 }
16135
16136 return mac_ptr;
16137 }
16138
16139 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16140 including DW_MACRO_GNU_transparent_include. */
16141
16142 static void
16143 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16144 struct macro_source_file *current_file,
16145 struct line_header *lh, char *comp_dir,
16146 struct dwarf2_section_info *section,
16147 int section_is_gnu,
16148 unsigned int offset_size,
16149 struct objfile *objfile,
16150 htab_t include_hash)
16151 {
16152 enum dwarf_macro_record_type macinfo_type;
16153 int at_commandline;
16154 gdb_byte *opcode_definitions[256];
16155
16156 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16157 &offset_size, section_is_gnu);
16158 if (mac_ptr == NULL)
16159 {
16160 /* We already issued a complaint. */
16161 return;
16162 }
16163
16164 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16165 GDB is still reading the definitions from command line. First
16166 DW_MACINFO_start_file will need to be ignored as it was already executed
16167 to create CURRENT_FILE for the main source holding also the command line
16168 definitions. On first met DW_MACINFO_start_file this flag is reset to
16169 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16170
16171 at_commandline = 1;
16172
16173 do
16174 {
16175 /* Do we at least have room for a macinfo type byte? */
16176 if (mac_ptr >= mac_end)
16177 {
16178 dwarf2_section_buffer_overflow_complaint (section);
16179 break;
16180 }
16181
16182 macinfo_type = read_1_byte (abfd, mac_ptr);
16183 mac_ptr++;
16184
16185 /* Note that we rely on the fact that the corresponding GNU and
16186 DWARF constants are the same. */
16187 switch (macinfo_type)
16188 {
16189 /* A zero macinfo type indicates the end of the macro
16190 information. */
16191 case 0:
16192 break;
16193
16194 case DW_MACRO_GNU_define:
16195 case DW_MACRO_GNU_undef:
16196 case DW_MACRO_GNU_define_indirect:
16197 case DW_MACRO_GNU_undef_indirect:
16198 {
16199 unsigned int bytes_read;
16200 int line;
16201 char *body;
16202 int is_define;
16203
16204 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16205 mac_ptr += bytes_read;
16206
16207 if (macinfo_type == DW_MACRO_GNU_define
16208 || macinfo_type == DW_MACRO_GNU_undef)
16209 {
16210 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16211 mac_ptr += bytes_read;
16212 }
16213 else
16214 {
16215 LONGEST str_offset;
16216
16217 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16218 mac_ptr += offset_size;
16219
16220 body = read_indirect_string_at_offset (abfd, str_offset);
16221 }
16222
16223 is_define = (macinfo_type == DW_MACRO_GNU_define
16224 || macinfo_type == DW_MACRO_GNU_define_indirect);
16225 if (! current_file)
16226 {
16227 /* DWARF violation as no main source is present. */
16228 complaint (&symfile_complaints,
16229 _("debug info with no main source gives macro %s "
16230 "on line %d: %s"),
16231 is_define ? _("definition") : _("undefinition"),
16232 line, body);
16233 break;
16234 }
16235 if ((line == 0 && !at_commandline)
16236 || (line != 0 && at_commandline))
16237 complaint (&symfile_complaints,
16238 _("debug info gives %s macro %s with %s line %d: %s"),
16239 at_commandline ? _("command-line") : _("in-file"),
16240 is_define ? _("definition") : _("undefinition"),
16241 line == 0 ? _("zero") : _("non-zero"), line, body);
16242
16243 if (is_define)
16244 parse_macro_definition (current_file, line, body);
16245 else
16246 {
16247 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16248 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16249 macro_undef (current_file, line, body);
16250 }
16251 }
16252 break;
16253
16254 case DW_MACRO_GNU_start_file:
16255 {
16256 unsigned int bytes_read;
16257 int line, file;
16258
16259 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16260 mac_ptr += bytes_read;
16261 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16262 mac_ptr += bytes_read;
16263
16264 if ((line == 0 && !at_commandline)
16265 || (line != 0 && at_commandline))
16266 complaint (&symfile_complaints,
16267 _("debug info gives source %d included "
16268 "from %s at %s line %d"),
16269 file, at_commandline ? _("command-line") : _("file"),
16270 line == 0 ? _("zero") : _("non-zero"), line);
16271
16272 if (at_commandline)
16273 {
16274 /* This DW_MACRO_GNU_start_file was executed in the
16275 pass one. */
16276 at_commandline = 0;
16277 }
16278 else
16279 current_file = macro_start_file (file, line,
16280 current_file, comp_dir,
16281 lh, objfile);
16282 }
16283 break;
16284
16285 case DW_MACRO_GNU_end_file:
16286 if (! current_file)
16287 complaint (&symfile_complaints,
16288 _("macro debug info has an unmatched "
16289 "`close_file' directive"));
16290 else
16291 {
16292 current_file = current_file->included_by;
16293 if (! current_file)
16294 {
16295 enum dwarf_macro_record_type next_type;
16296
16297 /* GCC circa March 2002 doesn't produce the zero
16298 type byte marking the end of the compilation
16299 unit. Complain if it's not there, but exit no
16300 matter what. */
16301
16302 /* Do we at least have room for a macinfo type byte? */
16303 if (mac_ptr >= mac_end)
16304 {
16305 dwarf2_section_buffer_overflow_complaint (section);
16306 return;
16307 }
16308
16309 /* We don't increment mac_ptr here, so this is just
16310 a look-ahead. */
16311 next_type = read_1_byte (abfd, mac_ptr);
16312 if (next_type != 0)
16313 complaint (&symfile_complaints,
16314 _("no terminating 0-type entry for "
16315 "macros in `.debug_macinfo' section"));
16316
16317 return;
16318 }
16319 }
16320 break;
16321
16322 case DW_MACRO_GNU_transparent_include:
16323 {
16324 LONGEST offset;
16325 void **slot;
16326
16327 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16328 mac_ptr += offset_size;
16329
16330 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16331 if (*slot != NULL)
16332 {
16333 /* This has actually happened; see
16334 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16335 complaint (&symfile_complaints,
16336 _("recursive DW_MACRO_GNU_transparent_include in "
16337 ".debug_macro section"));
16338 }
16339 else
16340 {
16341 *slot = mac_ptr;
16342
16343 dwarf_decode_macro_bytes (abfd,
16344 section->buffer + offset,
16345 mac_end, current_file,
16346 lh, comp_dir,
16347 section, section_is_gnu,
16348 offset_size, objfile, include_hash);
16349
16350 htab_remove_elt (include_hash, mac_ptr);
16351 }
16352 }
16353 break;
16354
16355 case DW_MACINFO_vendor_ext:
16356 if (!section_is_gnu)
16357 {
16358 unsigned int bytes_read;
16359 int constant;
16360
16361 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16362 mac_ptr += bytes_read;
16363 read_direct_string (abfd, mac_ptr, &bytes_read);
16364 mac_ptr += bytes_read;
16365
16366 /* We don't recognize any vendor extensions. */
16367 break;
16368 }
16369 /* FALLTHROUGH */
16370
16371 default:
16372 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16373 mac_ptr, mac_end, abfd, offset_size,
16374 section);
16375 if (mac_ptr == NULL)
16376 return;
16377 break;
16378 }
16379 } while (macinfo_type != 0);
16380 }
16381
16382 static void
16383 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16384 char *comp_dir, bfd *abfd,
16385 struct dwarf2_cu *cu,
16386 struct dwarf2_section_info *section,
16387 int section_is_gnu, const char *section_name)
16388 {
16389 struct objfile *objfile = dwarf2_per_objfile->objfile;
16390 gdb_byte *mac_ptr, *mac_end;
16391 struct macro_source_file *current_file = 0;
16392 enum dwarf_macro_record_type macinfo_type;
16393 unsigned int offset_size = cu->header.offset_size;
16394 gdb_byte *opcode_definitions[256];
16395 struct cleanup *cleanup;
16396 htab_t include_hash;
16397 void **slot;
16398
16399 dwarf2_read_section (objfile, section);
16400 if (section->buffer == NULL)
16401 {
16402 complaint (&symfile_complaints, _("missing %s section"), section_name);
16403 return;
16404 }
16405
16406 /* First pass: Find the name of the base filename.
16407 This filename is needed in order to process all macros whose definition
16408 (or undefinition) comes from the command line. These macros are defined
16409 before the first DW_MACINFO_start_file entry, and yet still need to be
16410 associated to the base file.
16411
16412 To determine the base file name, we scan the macro definitions until we
16413 reach the first DW_MACINFO_start_file entry. We then initialize
16414 CURRENT_FILE accordingly so that any macro definition found before the
16415 first DW_MACINFO_start_file can still be associated to the base file. */
16416
16417 mac_ptr = section->buffer + offset;
16418 mac_end = section->buffer + section->size;
16419
16420 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16421 &offset_size, section_is_gnu);
16422 if (mac_ptr == NULL)
16423 {
16424 /* We already issued a complaint. */
16425 return;
16426 }
16427
16428 do
16429 {
16430 /* Do we at least have room for a macinfo type byte? */
16431 if (mac_ptr >= mac_end)
16432 {
16433 /* Complaint is printed during the second pass as GDB will probably
16434 stop the first pass earlier upon finding
16435 DW_MACINFO_start_file. */
16436 break;
16437 }
16438
16439 macinfo_type = read_1_byte (abfd, mac_ptr);
16440 mac_ptr++;
16441
16442 /* Note that we rely on the fact that the corresponding GNU and
16443 DWARF constants are the same. */
16444 switch (macinfo_type)
16445 {
16446 /* A zero macinfo type indicates the end of the macro
16447 information. */
16448 case 0:
16449 break;
16450
16451 case DW_MACRO_GNU_define:
16452 case DW_MACRO_GNU_undef:
16453 /* Only skip the data by MAC_PTR. */
16454 {
16455 unsigned int bytes_read;
16456
16457 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16458 mac_ptr += bytes_read;
16459 read_direct_string (abfd, mac_ptr, &bytes_read);
16460 mac_ptr += bytes_read;
16461 }
16462 break;
16463
16464 case DW_MACRO_GNU_start_file:
16465 {
16466 unsigned int bytes_read;
16467 int line, file;
16468
16469 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16470 mac_ptr += bytes_read;
16471 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16472 mac_ptr += bytes_read;
16473
16474 current_file = macro_start_file (file, line, current_file,
16475 comp_dir, lh, objfile);
16476 }
16477 break;
16478
16479 case DW_MACRO_GNU_end_file:
16480 /* No data to skip by MAC_PTR. */
16481 break;
16482
16483 case DW_MACRO_GNU_define_indirect:
16484 case DW_MACRO_GNU_undef_indirect:
16485 {
16486 unsigned int bytes_read;
16487
16488 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16489 mac_ptr += bytes_read;
16490 mac_ptr += offset_size;
16491 }
16492 break;
16493
16494 case DW_MACRO_GNU_transparent_include:
16495 /* Note that, according to the spec, a transparent include
16496 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16497 skip this opcode. */
16498 mac_ptr += offset_size;
16499 break;
16500
16501 case DW_MACINFO_vendor_ext:
16502 /* Only skip the data by MAC_PTR. */
16503 if (!section_is_gnu)
16504 {
16505 unsigned int bytes_read;
16506
16507 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16508 mac_ptr += bytes_read;
16509 read_direct_string (abfd, mac_ptr, &bytes_read);
16510 mac_ptr += bytes_read;
16511 }
16512 /* FALLTHROUGH */
16513
16514 default:
16515 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16516 mac_ptr, mac_end, abfd, offset_size,
16517 section);
16518 if (mac_ptr == NULL)
16519 return;
16520 break;
16521 }
16522 } while (macinfo_type != 0 && current_file == NULL);
16523
16524 /* Second pass: Process all entries.
16525
16526 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16527 command-line macro definitions/undefinitions. This flag is unset when we
16528 reach the first DW_MACINFO_start_file entry. */
16529
16530 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16531 NULL, xcalloc, xfree);
16532 cleanup = make_cleanup_htab_delete (include_hash);
16533 mac_ptr = section->buffer + offset;
16534 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16535 *slot = mac_ptr;
16536 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16537 current_file, lh, comp_dir, section, section_is_gnu,
16538 offset_size, objfile, include_hash);
16539 do_cleanups (cleanup);
16540 }
16541
16542 /* Check if the attribute's form is a DW_FORM_block*
16543 if so return true else false. */
16544
16545 static int
16546 attr_form_is_block (struct attribute *attr)
16547 {
16548 return (attr == NULL ? 0 :
16549 attr->form == DW_FORM_block1
16550 || attr->form == DW_FORM_block2
16551 || attr->form == DW_FORM_block4
16552 || attr->form == DW_FORM_block
16553 || attr->form == DW_FORM_exprloc);
16554 }
16555
16556 /* Return non-zero if ATTR's value is a section offset --- classes
16557 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16558 You may use DW_UNSND (attr) to retrieve such offsets.
16559
16560 Section 7.5.4, "Attribute Encodings", explains that no attribute
16561 may have a value that belongs to more than one of these classes; it
16562 would be ambiguous if we did, because we use the same forms for all
16563 of them. */
16564
16565 static int
16566 attr_form_is_section_offset (struct attribute *attr)
16567 {
16568 return (attr->form == DW_FORM_data4
16569 || attr->form == DW_FORM_data8
16570 || attr->form == DW_FORM_sec_offset);
16571 }
16572
16573 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16574 zero otherwise. When this function returns true, you can apply
16575 dwarf2_get_attr_constant_value to it.
16576
16577 However, note that for some attributes you must check
16578 attr_form_is_section_offset before using this test. DW_FORM_data4
16579 and DW_FORM_data8 are members of both the constant class, and of
16580 the classes that contain offsets into other debug sections
16581 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16582 that, if an attribute's can be either a constant or one of the
16583 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16584 taken as section offsets, not constants. */
16585
16586 static int
16587 attr_form_is_constant (struct attribute *attr)
16588 {
16589 switch (attr->form)
16590 {
16591 case DW_FORM_sdata:
16592 case DW_FORM_udata:
16593 case DW_FORM_data1:
16594 case DW_FORM_data2:
16595 case DW_FORM_data4:
16596 case DW_FORM_data8:
16597 return 1;
16598 default:
16599 return 0;
16600 }
16601 }
16602
16603 /* Return the .debug_loc section to use for CU.
16604 For DWO files use .debug_loc.dwo. */
16605
16606 static struct dwarf2_section_info *
16607 cu_debug_loc_section (struct dwarf2_cu *cu)
16608 {
16609 if (cu->dwo_unit)
16610 return &cu->dwo_unit->dwo_file->sections.loc;
16611 return &dwarf2_per_objfile->loc;
16612 }
16613
16614 /* A helper function that fills in a dwarf2_loclist_baton. */
16615
16616 static void
16617 fill_in_loclist_baton (struct dwarf2_cu *cu,
16618 struct dwarf2_loclist_baton *baton,
16619 struct attribute *attr)
16620 {
16621 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16622
16623 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16624
16625 baton->per_cu = cu->per_cu;
16626 gdb_assert (baton->per_cu);
16627 /* We don't know how long the location list is, but make sure we
16628 don't run off the edge of the section. */
16629 baton->size = section->size - DW_UNSND (attr);
16630 baton->data = section->buffer + DW_UNSND (attr);
16631 baton->base_address = cu->base_address;
16632 baton->from_dwo = cu->dwo_unit != NULL;
16633 }
16634
16635 static void
16636 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16637 struct dwarf2_cu *cu)
16638 {
16639 struct objfile *objfile = dwarf2_per_objfile->objfile;
16640 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16641
16642 if (attr_form_is_section_offset (attr)
16643 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16644 the section. If so, fall through to the complaint in the
16645 other branch. */
16646 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16647 {
16648 struct dwarf2_loclist_baton *baton;
16649
16650 baton = obstack_alloc (&objfile->objfile_obstack,
16651 sizeof (struct dwarf2_loclist_baton));
16652
16653 fill_in_loclist_baton (cu, baton, attr);
16654
16655 if (cu->base_known == 0)
16656 complaint (&symfile_complaints,
16657 _("Location list used without "
16658 "specifying the CU base address."));
16659
16660 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16661 SYMBOL_LOCATION_BATON (sym) = baton;
16662 }
16663 else
16664 {
16665 struct dwarf2_locexpr_baton *baton;
16666
16667 baton = obstack_alloc (&objfile->objfile_obstack,
16668 sizeof (struct dwarf2_locexpr_baton));
16669 baton->per_cu = cu->per_cu;
16670 gdb_assert (baton->per_cu);
16671
16672 if (attr_form_is_block (attr))
16673 {
16674 /* Note that we're just copying the block's data pointer
16675 here, not the actual data. We're still pointing into the
16676 info_buffer for SYM's objfile; right now we never release
16677 that buffer, but when we do clean up properly this may
16678 need to change. */
16679 baton->size = DW_BLOCK (attr)->size;
16680 baton->data = DW_BLOCK (attr)->data;
16681 }
16682 else
16683 {
16684 dwarf2_invalid_attrib_class_complaint ("location description",
16685 SYMBOL_NATURAL_NAME (sym));
16686 baton->size = 0;
16687 }
16688
16689 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16690 SYMBOL_LOCATION_BATON (sym) = baton;
16691 }
16692 }
16693
16694 /* Return the OBJFILE associated with the compilation unit CU. If CU
16695 came from a separate debuginfo file, then the master objfile is
16696 returned. */
16697
16698 struct objfile *
16699 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16700 {
16701 struct objfile *objfile = per_cu->objfile;
16702
16703 /* Return the master objfile, so that we can report and look up the
16704 correct file containing this variable. */
16705 if (objfile->separate_debug_objfile_backlink)
16706 objfile = objfile->separate_debug_objfile_backlink;
16707
16708 return objfile;
16709 }
16710
16711 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16712 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16713 CU_HEADERP first. */
16714
16715 static const struct comp_unit_head *
16716 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16717 struct dwarf2_per_cu_data *per_cu)
16718 {
16719 struct objfile *objfile;
16720 struct dwarf2_per_objfile *per_objfile;
16721 gdb_byte *info_ptr;
16722
16723 if (per_cu->cu)
16724 return &per_cu->cu->header;
16725
16726 objfile = per_cu->objfile;
16727 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16728 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16729
16730 memset (cu_headerp, 0, sizeof (*cu_headerp));
16731 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16732
16733 return cu_headerp;
16734 }
16735
16736 /* Return the address size given in the compilation unit header for CU. */
16737
16738 int
16739 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16740 {
16741 struct comp_unit_head cu_header_local;
16742 const struct comp_unit_head *cu_headerp;
16743
16744 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16745
16746 return cu_headerp->addr_size;
16747 }
16748
16749 /* Return the offset size given in the compilation unit header for CU. */
16750
16751 int
16752 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16753 {
16754 struct comp_unit_head cu_header_local;
16755 const struct comp_unit_head *cu_headerp;
16756
16757 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16758
16759 return cu_headerp->offset_size;
16760 }
16761
16762 /* See its dwarf2loc.h declaration. */
16763
16764 int
16765 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16766 {
16767 struct comp_unit_head cu_header_local;
16768 const struct comp_unit_head *cu_headerp;
16769
16770 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16771
16772 if (cu_headerp->version == 2)
16773 return cu_headerp->addr_size;
16774 else
16775 return cu_headerp->offset_size;
16776 }
16777
16778 /* Return the text offset of the CU. The returned offset comes from
16779 this CU's objfile. If this objfile came from a separate debuginfo
16780 file, then the offset may be different from the corresponding
16781 offset in the parent objfile. */
16782
16783 CORE_ADDR
16784 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16785 {
16786 struct objfile *objfile = per_cu->objfile;
16787
16788 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16789 }
16790
16791 /* Locate the .debug_info compilation unit from CU's objfile which contains
16792 the DIE at OFFSET. Raises an error on failure. */
16793
16794 static struct dwarf2_per_cu_data *
16795 dwarf2_find_containing_comp_unit (sect_offset offset,
16796 struct objfile *objfile)
16797 {
16798 struct dwarf2_per_cu_data *this_cu;
16799 int low, high;
16800
16801 low = 0;
16802 high = dwarf2_per_objfile->n_comp_units - 1;
16803 while (high > low)
16804 {
16805 int mid = low + (high - low) / 2;
16806
16807 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16808 >= offset.sect_off)
16809 high = mid;
16810 else
16811 low = mid + 1;
16812 }
16813 gdb_assert (low == high);
16814 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16815 > offset.sect_off)
16816 {
16817 if (low == 0)
16818 error (_("Dwarf Error: could not find partial DIE containing "
16819 "offset 0x%lx [in module %s]"),
16820 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16821
16822 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16823 <= offset.sect_off);
16824 return dwarf2_per_objfile->all_comp_units[low-1];
16825 }
16826 else
16827 {
16828 this_cu = dwarf2_per_objfile->all_comp_units[low];
16829 if (low == dwarf2_per_objfile->n_comp_units - 1
16830 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16831 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16832 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16833 return this_cu;
16834 }
16835 }
16836
16837 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16838
16839 static void
16840 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16841 {
16842 memset (cu, 0, sizeof (*cu));
16843 per_cu->cu = cu;
16844 cu->per_cu = per_cu;
16845 cu->objfile = per_cu->objfile;
16846 obstack_init (&cu->comp_unit_obstack);
16847 }
16848
16849 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16850
16851 static void
16852 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16853 enum language pretend_language)
16854 {
16855 struct attribute *attr;
16856
16857 /* Set the language we're debugging. */
16858 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16859 if (attr)
16860 set_cu_language (DW_UNSND (attr), cu);
16861 else
16862 {
16863 cu->language = pretend_language;
16864 cu->language_defn = language_def (cu->language);
16865 }
16866
16867 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16868 if (attr)
16869 cu->producer = DW_STRING (attr);
16870 }
16871
16872 /* Release one cached compilation unit, CU. We unlink it from the tree
16873 of compilation units, but we don't remove it from the read_in_chain;
16874 the caller is responsible for that.
16875 NOTE: DATA is a void * because this function is also used as a
16876 cleanup routine. */
16877
16878 static void
16879 free_heap_comp_unit (void *data)
16880 {
16881 struct dwarf2_cu *cu = data;
16882
16883 gdb_assert (cu->per_cu != NULL);
16884 cu->per_cu->cu = NULL;
16885 cu->per_cu = NULL;
16886
16887 obstack_free (&cu->comp_unit_obstack, NULL);
16888
16889 xfree (cu);
16890 }
16891
16892 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16893 when we're finished with it. We can't free the pointer itself, but be
16894 sure to unlink it from the cache. Also release any associated storage. */
16895
16896 static void
16897 free_stack_comp_unit (void *data)
16898 {
16899 struct dwarf2_cu *cu = data;
16900
16901 gdb_assert (cu->per_cu != NULL);
16902 cu->per_cu->cu = NULL;
16903 cu->per_cu = NULL;
16904
16905 obstack_free (&cu->comp_unit_obstack, NULL);
16906 cu->partial_dies = NULL;
16907 }
16908
16909 /* Free all cached compilation units. */
16910
16911 static void
16912 free_cached_comp_units (void *data)
16913 {
16914 struct dwarf2_per_cu_data *per_cu, **last_chain;
16915
16916 per_cu = dwarf2_per_objfile->read_in_chain;
16917 last_chain = &dwarf2_per_objfile->read_in_chain;
16918 while (per_cu != NULL)
16919 {
16920 struct dwarf2_per_cu_data *next_cu;
16921
16922 next_cu = per_cu->cu->read_in_chain;
16923
16924 free_heap_comp_unit (per_cu->cu);
16925 *last_chain = next_cu;
16926
16927 per_cu = next_cu;
16928 }
16929 }
16930
16931 /* Increase the age counter on each cached compilation unit, and free
16932 any that are too old. */
16933
16934 static void
16935 age_cached_comp_units (void)
16936 {
16937 struct dwarf2_per_cu_data *per_cu, **last_chain;
16938
16939 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16940 per_cu = dwarf2_per_objfile->read_in_chain;
16941 while (per_cu != NULL)
16942 {
16943 per_cu->cu->last_used ++;
16944 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16945 dwarf2_mark (per_cu->cu);
16946 per_cu = per_cu->cu->read_in_chain;
16947 }
16948
16949 per_cu = dwarf2_per_objfile->read_in_chain;
16950 last_chain = &dwarf2_per_objfile->read_in_chain;
16951 while (per_cu != NULL)
16952 {
16953 struct dwarf2_per_cu_data *next_cu;
16954
16955 next_cu = per_cu->cu->read_in_chain;
16956
16957 if (!per_cu->cu->mark)
16958 {
16959 free_heap_comp_unit (per_cu->cu);
16960 *last_chain = next_cu;
16961 }
16962 else
16963 last_chain = &per_cu->cu->read_in_chain;
16964
16965 per_cu = next_cu;
16966 }
16967 }
16968
16969 /* Remove a single compilation unit from the cache. */
16970
16971 static void
16972 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16973 {
16974 struct dwarf2_per_cu_data *per_cu, **last_chain;
16975
16976 per_cu = dwarf2_per_objfile->read_in_chain;
16977 last_chain = &dwarf2_per_objfile->read_in_chain;
16978 while (per_cu != NULL)
16979 {
16980 struct dwarf2_per_cu_data *next_cu;
16981
16982 next_cu = per_cu->cu->read_in_chain;
16983
16984 if (per_cu == target_per_cu)
16985 {
16986 free_heap_comp_unit (per_cu->cu);
16987 per_cu->cu = NULL;
16988 *last_chain = next_cu;
16989 break;
16990 }
16991 else
16992 last_chain = &per_cu->cu->read_in_chain;
16993
16994 per_cu = next_cu;
16995 }
16996 }
16997
16998 /* Release all extra memory associated with OBJFILE. */
16999
17000 void
17001 dwarf2_free_objfile (struct objfile *objfile)
17002 {
17003 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17004
17005 if (dwarf2_per_objfile == NULL)
17006 return;
17007
17008 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17009 free_cached_comp_units (NULL);
17010
17011 if (dwarf2_per_objfile->quick_file_names_table)
17012 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17013
17014 /* Everything else should be on the objfile obstack. */
17015 }
17016
17017 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17018 We store these in a hash table separate from the DIEs, and preserve them
17019 when the DIEs are flushed out of cache.
17020
17021 The CU "per_cu" pointer is needed because offset alone is not enough to
17022 uniquely identify the type. A file may have multiple .debug_types sections,
17023 or the type may come from a DWO file. We have to use something in
17024 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17025 routine, get_die_type_at_offset, from outside this file, and thus won't
17026 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17027 of the objfile. */
17028
17029 struct dwarf2_per_cu_offset_and_type
17030 {
17031 const struct dwarf2_per_cu_data *per_cu;
17032 sect_offset offset;
17033 struct type *type;
17034 };
17035
17036 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17037
17038 static hashval_t
17039 per_cu_offset_and_type_hash (const void *item)
17040 {
17041 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17042
17043 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17044 }
17045
17046 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17047
17048 static int
17049 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17050 {
17051 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17052 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17053
17054 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17055 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17056 }
17057
17058 /* Set the type associated with DIE to TYPE. Save it in CU's hash
17059 table if necessary. For convenience, return TYPE.
17060
17061 The DIEs reading must have careful ordering to:
17062 * Not cause infite loops trying to read in DIEs as a prerequisite for
17063 reading current DIE.
17064 * Not trying to dereference contents of still incompletely read in types
17065 while reading in other DIEs.
17066 * Enable referencing still incompletely read in types just by a pointer to
17067 the type without accessing its fields.
17068
17069 Therefore caller should follow these rules:
17070 * Try to fetch any prerequisite types we may need to build this DIE type
17071 before building the type and calling set_die_type.
17072 * After building type call set_die_type for current DIE as soon as
17073 possible before fetching more types to complete the current type.
17074 * Make the type as complete as possible before fetching more types. */
17075
17076 static struct type *
17077 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17078 {
17079 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17080 struct objfile *objfile = cu->objfile;
17081
17082 /* For Ada types, make sure that the gnat-specific data is always
17083 initialized (if not already set). There are a few types where
17084 we should not be doing so, because the type-specific area is
17085 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17086 where the type-specific area is used to store the floatformat).
17087 But this is not a problem, because the gnat-specific information
17088 is actually not needed for these types. */
17089 if (need_gnat_info (cu)
17090 && TYPE_CODE (type) != TYPE_CODE_FUNC
17091 && TYPE_CODE (type) != TYPE_CODE_FLT
17092 && !HAVE_GNAT_AUX_INFO (type))
17093 INIT_GNAT_SPECIFIC (type);
17094
17095 if (dwarf2_per_objfile->die_type_hash == NULL)
17096 {
17097 dwarf2_per_objfile->die_type_hash =
17098 htab_create_alloc_ex (127,
17099 per_cu_offset_and_type_hash,
17100 per_cu_offset_and_type_eq,
17101 NULL,
17102 &objfile->objfile_obstack,
17103 hashtab_obstack_allocate,
17104 dummy_obstack_deallocate);
17105 }
17106
17107 ofs.per_cu = cu->per_cu;
17108 ofs.offset = die->offset;
17109 ofs.type = type;
17110 slot = (struct dwarf2_per_cu_offset_and_type **)
17111 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17112 if (*slot)
17113 complaint (&symfile_complaints,
17114 _("A problem internal to GDB: DIE 0x%x has type already set"),
17115 die->offset.sect_off);
17116 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17117 **slot = ofs;
17118 return type;
17119 }
17120
17121 /* Look up the type for the die at OFFSET in the appropriate type_hash
17122 table, or return NULL if the die does not have a saved type. */
17123
17124 static struct type *
17125 get_die_type_at_offset (sect_offset offset,
17126 struct dwarf2_per_cu_data *per_cu)
17127 {
17128 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17129
17130 if (dwarf2_per_objfile->die_type_hash == NULL)
17131 return NULL;
17132
17133 ofs.per_cu = per_cu;
17134 ofs.offset = offset;
17135 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17136 if (slot)
17137 return slot->type;
17138 else
17139 return NULL;
17140 }
17141
17142 /* Look up the type for DIE in the appropriate type_hash table,
17143 or return NULL if DIE does not have a saved type. */
17144
17145 static struct type *
17146 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17147 {
17148 return get_die_type_at_offset (die->offset, cu->per_cu);
17149 }
17150
17151 /* Add a dependence relationship from CU to REF_PER_CU. */
17152
17153 static void
17154 dwarf2_add_dependence (struct dwarf2_cu *cu,
17155 struct dwarf2_per_cu_data *ref_per_cu)
17156 {
17157 void **slot;
17158
17159 if (cu->dependencies == NULL)
17160 cu->dependencies
17161 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17162 NULL, &cu->comp_unit_obstack,
17163 hashtab_obstack_allocate,
17164 dummy_obstack_deallocate);
17165
17166 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17167 if (*slot == NULL)
17168 *slot = ref_per_cu;
17169 }
17170
17171 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17172 Set the mark field in every compilation unit in the
17173 cache that we must keep because we are keeping CU. */
17174
17175 static int
17176 dwarf2_mark_helper (void **slot, void *data)
17177 {
17178 struct dwarf2_per_cu_data *per_cu;
17179
17180 per_cu = (struct dwarf2_per_cu_data *) *slot;
17181
17182 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17183 reading of the chain. As such dependencies remain valid it is not much
17184 useful to track and undo them during QUIT cleanups. */
17185 if (per_cu->cu == NULL)
17186 return 1;
17187
17188 if (per_cu->cu->mark)
17189 return 1;
17190 per_cu->cu->mark = 1;
17191
17192 if (per_cu->cu->dependencies != NULL)
17193 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17194
17195 return 1;
17196 }
17197
17198 /* Set the mark field in CU and in every other compilation unit in the
17199 cache that we must keep because we are keeping CU. */
17200
17201 static void
17202 dwarf2_mark (struct dwarf2_cu *cu)
17203 {
17204 if (cu->mark)
17205 return;
17206 cu->mark = 1;
17207 if (cu->dependencies != NULL)
17208 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17209 }
17210
17211 static void
17212 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17213 {
17214 while (per_cu)
17215 {
17216 per_cu->cu->mark = 0;
17217 per_cu = per_cu->cu->read_in_chain;
17218 }
17219 }
17220
17221 /* Trivial hash function for partial_die_info: the hash value of a DIE
17222 is its offset in .debug_info for this objfile. */
17223
17224 static hashval_t
17225 partial_die_hash (const void *item)
17226 {
17227 const struct partial_die_info *part_die = item;
17228
17229 return part_die->offset.sect_off;
17230 }
17231
17232 /* Trivial comparison function for partial_die_info structures: two DIEs
17233 are equal if they have the same offset. */
17234
17235 static int
17236 partial_die_eq (const void *item_lhs, const void *item_rhs)
17237 {
17238 const struct partial_die_info *part_die_lhs = item_lhs;
17239 const struct partial_die_info *part_die_rhs = item_rhs;
17240
17241 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17242 }
17243
17244 static struct cmd_list_element *set_dwarf2_cmdlist;
17245 static struct cmd_list_element *show_dwarf2_cmdlist;
17246
17247 static void
17248 set_dwarf2_cmd (char *args, int from_tty)
17249 {
17250 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17251 }
17252
17253 static void
17254 show_dwarf2_cmd (char *args, int from_tty)
17255 {
17256 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17257 }
17258
17259 /* If section described by INFO was mmapped, munmap it now. */
17260
17261 static void
17262 munmap_section_buffer (struct dwarf2_section_info *info)
17263 {
17264 if (info->map_addr != NULL)
17265 {
17266 #ifdef HAVE_MMAP
17267 int res;
17268
17269 res = munmap (info->map_addr, info->map_len);
17270 gdb_assert (res == 0);
17271 #else
17272 /* Without HAVE_MMAP, we should never be here to begin with. */
17273 gdb_assert_not_reached ("no mmap support");
17274 #endif
17275 }
17276 }
17277
17278 /* munmap debug sections for OBJFILE, if necessary. */
17279
17280 static void
17281 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17282 {
17283 struct dwarf2_per_objfile *data = d;
17284 int ix;
17285 struct dwarf2_section_info *section;
17286
17287 /* This is sorted according to the order they're defined in to make it easier
17288 to keep in sync. */
17289 munmap_section_buffer (&data->info);
17290 munmap_section_buffer (&data->abbrev);
17291 munmap_section_buffer (&data->line);
17292 munmap_section_buffer (&data->loc);
17293 munmap_section_buffer (&data->macinfo);
17294 munmap_section_buffer (&data->macro);
17295 munmap_section_buffer (&data->str);
17296 munmap_section_buffer (&data->ranges);
17297 munmap_section_buffer (&data->addr);
17298 munmap_section_buffer (&data->frame);
17299 munmap_section_buffer (&data->eh_frame);
17300 munmap_section_buffer (&data->gdb_index);
17301
17302 for (ix = 0;
17303 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17304 ++ix)
17305 munmap_section_buffer (section);
17306
17307 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17308 VEC_free (dwarf2_per_cu_ptr,
17309 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17310
17311 VEC_free (dwarf2_section_info_def, data->types);
17312
17313 if (data->dwo_files)
17314 free_dwo_files (data->dwo_files, objfile);
17315 }
17316
17317 \f
17318 /* The "save gdb-index" command. */
17319
17320 /* The contents of the hash table we create when building the string
17321 table. */
17322 struct strtab_entry
17323 {
17324 offset_type offset;
17325 const char *str;
17326 };
17327
17328 /* Hash function for a strtab_entry.
17329
17330 Function is used only during write_hash_table so no index format backward
17331 compatibility is needed. */
17332
17333 static hashval_t
17334 hash_strtab_entry (const void *e)
17335 {
17336 const struct strtab_entry *entry = e;
17337 return mapped_index_string_hash (INT_MAX, entry->str);
17338 }
17339
17340 /* Equality function for a strtab_entry. */
17341
17342 static int
17343 eq_strtab_entry (const void *a, const void *b)
17344 {
17345 const struct strtab_entry *ea = a;
17346 const struct strtab_entry *eb = b;
17347 return !strcmp (ea->str, eb->str);
17348 }
17349
17350 /* Create a strtab_entry hash table. */
17351
17352 static htab_t
17353 create_strtab (void)
17354 {
17355 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17356 xfree, xcalloc, xfree);
17357 }
17358
17359 /* Add a string to the constant pool. Return the string's offset in
17360 host order. */
17361
17362 static offset_type
17363 add_string (htab_t table, struct obstack *cpool, const char *str)
17364 {
17365 void **slot;
17366 struct strtab_entry entry;
17367 struct strtab_entry *result;
17368
17369 entry.str = str;
17370 slot = htab_find_slot (table, &entry, INSERT);
17371 if (*slot)
17372 result = *slot;
17373 else
17374 {
17375 result = XNEW (struct strtab_entry);
17376 result->offset = obstack_object_size (cpool);
17377 result->str = str;
17378 obstack_grow_str0 (cpool, str);
17379 *slot = result;
17380 }
17381 return result->offset;
17382 }
17383
17384 /* An entry in the symbol table. */
17385 struct symtab_index_entry
17386 {
17387 /* The name of the symbol. */
17388 const char *name;
17389 /* The offset of the name in the constant pool. */
17390 offset_type index_offset;
17391 /* A sorted vector of the indices of all the CUs that hold an object
17392 of this name. */
17393 VEC (offset_type) *cu_indices;
17394 };
17395
17396 /* The symbol table. This is a power-of-2-sized hash table. */
17397 struct mapped_symtab
17398 {
17399 offset_type n_elements;
17400 offset_type size;
17401 struct symtab_index_entry **data;
17402 };
17403
17404 /* Hash function for a symtab_index_entry. */
17405
17406 static hashval_t
17407 hash_symtab_entry (const void *e)
17408 {
17409 const struct symtab_index_entry *entry = e;
17410 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17411 sizeof (offset_type) * VEC_length (offset_type,
17412 entry->cu_indices),
17413 0);
17414 }
17415
17416 /* Equality function for a symtab_index_entry. */
17417
17418 static int
17419 eq_symtab_entry (const void *a, const void *b)
17420 {
17421 const struct symtab_index_entry *ea = a;
17422 const struct symtab_index_entry *eb = b;
17423 int len = VEC_length (offset_type, ea->cu_indices);
17424 if (len != VEC_length (offset_type, eb->cu_indices))
17425 return 0;
17426 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17427 VEC_address (offset_type, eb->cu_indices),
17428 sizeof (offset_type) * len);
17429 }
17430
17431 /* Destroy a symtab_index_entry. */
17432
17433 static void
17434 delete_symtab_entry (void *p)
17435 {
17436 struct symtab_index_entry *entry = p;
17437 VEC_free (offset_type, entry->cu_indices);
17438 xfree (entry);
17439 }
17440
17441 /* Create a hash table holding symtab_index_entry objects. */
17442
17443 static htab_t
17444 create_symbol_hash_table (void)
17445 {
17446 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17447 delete_symtab_entry, xcalloc, xfree);
17448 }
17449
17450 /* Create a new mapped symtab object. */
17451
17452 static struct mapped_symtab *
17453 create_mapped_symtab (void)
17454 {
17455 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17456 symtab->n_elements = 0;
17457 symtab->size = 1024;
17458 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17459 return symtab;
17460 }
17461
17462 /* Destroy a mapped_symtab. */
17463
17464 static void
17465 cleanup_mapped_symtab (void *p)
17466 {
17467 struct mapped_symtab *symtab = p;
17468 /* The contents of the array are freed when the other hash table is
17469 destroyed. */
17470 xfree (symtab->data);
17471 xfree (symtab);
17472 }
17473
17474 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17475 the slot.
17476
17477 Function is used only during write_hash_table so no index format backward
17478 compatibility is needed. */
17479
17480 static struct symtab_index_entry **
17481 find_slot (struct mapped_symtab *symtab, const char *name)
17482 {
17483 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17484
17485 index = hash & (symtab->size - 1);
17486 step = ((hash * 17) & (symtab->size - 1)) | 1;
17487
17488 for (;;)
17489 {
17490 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17491 return &symtab->data[index];
17492 index = (index + step) & (symtab->size - 1);
17493 }
17494 }
17495
17496 /* Expand SYMTAB's hash table. */
17497
17498 static void
17499 hash_expand (struct mapped_symtab *symtab)
17500 {
17501 offset_type old_size = symtab->size;
17502 offset_type i;
17503 struct symtab_index_entry **old_entries = symtab->data;
17504
17505 symtab->size *= 2;
17506 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17507
17508 for (i = 0; i < old_size; ++i)
17509 {
17510 if (old_entries[i])
17511 {
17512 struct symtab_index_entry **slot = find_slot (symtab,
17513 old_entries[i]->name);
17514 *slot = old_entries[i];
17515 }
17516 }
17517
17518 xfree (old_entries);
17519 }
17520
17521 /* Add an entry to SYMTAB. NAME is the name of the symbol.
17522 CU_INDEX is the index of the CU in which the symbol appears.
17523 IS_STATIC is one if the symbol is static, otherwise zero (global). */
17524
17525 static void
17526 add_index_entry (struct mapped_symtab *symtab, const char *name,
17527 int is_static, gdb_index_symbol_kind kind,
17528 offset_type cu_index)
17529 {
17530 struct symtab_index_entry **slot;
17531 offset_type cu_index_and_attrs;
17532
17533 ++symtab->n_elements;
17534 if (4 * symtab->n_elements / 3 >= symtab->size)
17535 hash_expand (symtab);
17536
17537 slot = find_slot (symtab, name);
17538 if (!*slot)
17539 {
17540 *slot = XNEW (struct symtab_index_entry);
17541 (*slot)->name = name;
17542 /* index_offset is set later. */
17543 (*slot)->cu_indices = NULL;
17544 }
17545
17546 cu_index_and_attrs = 0;
17547 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17548 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17549 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17550
17551 /* We don't want to record an index value twice as we want to avoid the
17552 duplication.
17553 We process all global symbols and then all static symbols
17554 (which would allow us to avoid the duplication by only having to check
17555 the last entry pushed), but a symbol could have multiple kinds in one CU.
17556 To keep things simple we don't worry about the duplication here and
17557 sort and uniqufy the list after we've processed all symbols. */
17558 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17559 }
17560
17561 /* qsort helper routine for uniquify_cu_indices. */
17562
17563 static int
17564 offset_type_compare (const void *ap, const void *bp)
17565 {
17566 offset_type a = *(offset_type *) ap;
17567 offset_type b = *(offset_type *) bp;
17568
17569 return (a > b) - (b > a);
17570 }
17571
17572 /* Sort and remove duplicates of all symbols' cu_indices lists. */
17573
17574 static void
17575 uniquify_cu_indices (struct mapped_symtab *symtab)
17576 {
17577 int i;
17578
17579 for (i = 0; i < symtab->size; ++i)
17580 {
17581 struct symtab_index_entry *entry = symtab->data[i];
17582
17583 if (entry
17584 && entry->cu_indices != NULL)
17585 {
17586 unsigned int next_to_insert, next_to_check;
17587 offset_type last_value;
17588
17589 qsort (VEC_address (offset_type, entry->cu_indices),
17590 VEC_length (offset_type, entry->cu_indices),
17591 sizeof (offset_type), offset_type_compare);
17592
17593 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17594 next_to_insert = 1;
17595 for (next_to_check = 1;
17596 next_to_check < VEC_length (offset_type, entry->cu_indices);
17597 ++next_to_check)
17598 {
17599 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17600 != last_value)
17601 {
17602 last_value = VEC_index (offset_type, entry->cu_indices,
17603 next_to_check);
17604 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17605 last_value);
17606 ++next_to_insert;
17607 }
17608 }
17609 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17610 }
17611 }
17612 }
17613
17614 /* Add a vector of indices to the constant pool. */
17615
17616 static offset_type
17617 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17618 struct symtab_index_entry *entry)
17619 {
17620 void **slot;
17621
17622 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17623 if (!*slot)
17624 {
17625 offset_type len = VEC_length (offset_type, entry->cu_indices);
17626 offset_type val = MAYBE_SWAP (len);
17627 offset_type iter;
17628 int i;
17629
17630 *slot = entry;
17631 entry->index_offset = obstack_object_size (cpool);
17632
17633 obstack_grow (cpool, &val, sizeof (val));
17634 for (i = 0;
17635 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17636 ++i)
17637 {
17638 val = MAYBE_SWAP (iter);
17639 obstack_grow (cpool, &val, sizeof (val));
17640 }
17641 }
17642 else
17643 {
17644 struct symtab_index_entry *old_entry = *slot;
17645 entry->index_offset = old_entry->index_offset;
17646 entry = old_entry;
17647 }
17648 return entry->index_offset;
17649 }
17650
17651 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17652 constant pool entries going into the obstack CPOOL. */
17653
17654 static void
17655 write_hash_table (struct mapped_symtab *symtab,
17656 struct obstack *output, struct obstack *cpool)
17657 {
17658 offset_type i;
17659 htab_t symbol_hash_table;
17660 htab_t str_table;
17661
17662 symbol_hash_table = create_symbol_hash_table ();
17663 str_table = create_strtab ();
17664
17665 /* We add all the index vectors to the constant pool first, to
17666 ensure alignment is ok. */
17667 for (i = 0; i < symtab->size; ++i)
17668 {
17669 if (symtab->data[i])
17670 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17671 }
17672
17673 /* Now write out the hash table. */
17674 for (i = 0; i < symtab->size; ++i)
17675 {
17676 offset_type str_off, vec_off;
17677
17678 if (symtab->data[i])
17679 {
17680 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17681 vec_off = symtab->data[i]->index_offset;
17682 }
17683 else
17684 {
17685 /* While 0 is a valid constant pool index, it is not valid
17686 to have 0 for both offsets. */
17687 str_off = 0;
17688 vec_off = 0;
17689 }
17690
17691 str_off = MAYBE_SWAP (str_off);
17692 vec_off = MAYBE_SWAP (vec_off);
17693
17694 obstack_grow (output, &str_off, sizeof (str_off));
17695 obstack_grow (output, &vec_off, sizeof (vec_off));
17696 }
17697
17698 htab_delete (str_table);
17699 htab_delete (symbol_hash_table);
17700 }
17701
17702 /* Struct to map psymtab to CU index in the index file. */
17703 struct psymtab_cu_index_map
17704 {
17705 struct partial_symtab *psymtab;
17706 unsigned int cu_index;
17707 };
17708
17709 static hashval_t
17710 hash_psymtab_cu_index (const void *item)
17711 {
17712 const struct psymtab_cu_index_map *map = item;
17713
17714 return htab_hash_pointer (map->psymtab);
17715 }
17716
17717 static int
17718 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17719 {
17720 const struct psymtab_cu_index_map *lhs = item_lhs;
17721 const struct psymtab_cu_index_map *rhs = item_rhs;
17722
17723 return lhs->psymtab == rhs->psymtab;
17724 }
17725
17726 /* Helper struct for building the address table. */
17727 struct addrmap_index_data
17728 {
17729 struct objfile *objfile;
17730 struct obstack *addr_obstack;
17731 htab_t cu_index_htab;
17732
17733 /* Non-zero if the previous_* fields are valid.
17734 We can't write an entry until we see the next entry (since it is only then
17735 that we know the end of the entry). */
17736 int previous_valid;
17737 /* Index of the CU in the table of all CUs in the index file. */
17738 unsigned int previous_cu_index;
17739 /* Start address of the CU. */
17740 CORE_ADDR previous_cu_start;
17741 };
17742
17743 /* Write an address entry to OBSTACK. */
17744
17745 static void
17746 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17747 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17748 {
17749 offset_type cu_index_to_write;
17750 char addr[8];
17751 CORE_ADDR baseaddr;
17752
17753 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17754
17755 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17756 obstack_grow (obstack, addr, 8);
17757 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17758 obstack_grow (obstack, addr, 8);
17759 cu_index_to_write = MAYBE_SWAP (cu_index);
17760 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17761 }
17762
17763 /* Worker function for traversing an addrmap to build the address table. */
17764
17765 static int
17766 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17767 {
17768 struct addrmap_index_data *data = datap;
17769 struct partial_symtab *pst = obj;
17770
17771 if (data->previous_valid)
17772 add_address_entry (data->objfile, data->addr_obstack,
17773 data->previous_cu_start, start_addr,
17774 data->previous_cu_index);
17775
17776 data->previous_cu_start = start_addr;
17777 if (pst != NULL)
17778 {
17779 struct psymtab_cu_index_map find_map, *map;
17780 find_map.psymtab = pst;
17781 map = htab_find (data->cu_index_htab, &find_map);
17782 gdb_assert (map != NULL);
17783 data->previous_cu_index = map->cu_index;
17784 data->previous_valid = 1;
17785 }
17786 else
17787 data->previous_valid = 0;
17788
17789 return 0;
17790 }
17791
17792 /* Write OBJFILE's address map to OBSTACK.
17793 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17794 in the index file. */
17795
17796 static void
17797 write_address_map (struct objfile *objfile, struct obstack *obstack,
17798 htab_t cu_index_htab)
17799 {
17800 struct addrmap_index_data addrmap_index_data;
17801
17802 /* When writing the address table, we have to cope with the fact that
17803 the addrmap iterator only provides the start of a region; we have to
17804 wait until the next invocation to get the start of the next region. */
17805
17806 addrmap_index_data.objfile = objfile;
17807 addrmap_index_data.addr_obstack = obstack;
17808 addrmap_index_data.cu_index_htab = cu_index_htab;
17809 addrmap_index_data.previous_valid = 0;
17810
17811 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17812 &addrmap_index_data);
17813
17814 /* It's highly unlikely the last entry (end address = 0xff...ff)
17815 is valid, but we should still handle it.
17816 The end address is recorded as the start of the next region, but that
17817 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17818 anyway. */
17819 if (addrmap_index_data.previous_valid)
17820 add_address_entry (objfile, obstack,
17821 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17822 addrmap_index_data.previous_cu_index);
17823 }
17824
17825 /* Return the symbol kind of PSYM. */
17826
17827 static gdb_index_symbol_kind
17828 symbol_kind (struct partial_symbol *psym)
17829 {
17830 domain_enum domain = PSYMBOL_DOMAIN (psym);
17831 enum address_class aclass = PSYMBOL_CLASS (psym);
17832
17833 switch (domain)
17834 {
17835 case VAR_DOMAIN:
17836 switch (aclass)
17837 {
17838 case LOC_BLOCK:
17839 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
17840 case LOC_TYPEDEF:
17841 return GDB_INDEX_SYMBOL_KIND_TYPE;
17842 case LOC_COMPUTED:
17843 case LOC_CONST_BYTES:
17844 case LOC_OPTIMIZED_OUT:
17845 case LOC_STATIC:
17846 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17847 case LOC_CONST:
17848 /* Note: It's currently impossible to recognize psyms as enum values
17849 short of reading the type info. For now punt. */
17850 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17851 default:
17852 /* There are other LOC_FOO values that one might want to classify
17853 as variables, but dwarf2read.c doesn't currently use them. */
17854 return GDB_INDEX_SYMBOL_KIND_OTHER;
17855 }
17856 case STRUCT_DOMAIN:
17857 return GDB_INDEX_SYMBOL_KIND_TYPE;
17858 default:
17859 return GDB_INDEX_SYMBOL_KIND_OTHER;
17860 }
17861 }
17862
17863 /* Add a list of partial symbols to SYMTAB. */
17864
17865 static void
17866 write_psymbols (struct mapped_symtab *symtab,
17867 htab_t psyms_seen,
17868 struct partial_symbol **psymp,
17869 int count,
17870 offset_type cu_index,
17871 int is_static)
17872 {
17873 for (; count-- > 0; ++psymp)
17874 {
17875 struct partial_symbol *psym = *psymp;
17876 void **slot;
17877
17878 if (SYMBOL_LANGUAGE (psym) == language_ada)
17879 error (_("Ada is not currently supported by the index"));
17880
17881 /* Only add a given psymbol once. */
17882 slot = htab_find_slot (psyms_seen, psym, INSERT);
17883 if (!*slot)
17884 {
17885 gdb_index_symbol_kind kind = symbol_kind (psym);
17886
17887 *slot = psym;
17888 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
17889 is_static, kind, cu_index);
17890 }
17891 }
17892 }
17893
17894 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17895 exception if there is an error. */
17896
17897 static void
17898 write_obstack (FILE *file, struct obstack *obstack)
17899 {
17900 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17901 file)
17902 != obstack_object_size (obstack))
17903 error (_("couldn't data write to file"));
17904 }
17905
17906 /* Unlink a file if the argument is not NULL. */
17907
17908 static void
17909 unlink_if_set (void *p)
17910 {
17911 char **filename = p;
17912 if (*filename)
17913 unlink (*filename);
17914 }
17915
17916 /* A helper struct used when iterating over debug_types. */
17917 struct signatured_type_index_data
17918 {
17919 struct objfile *objfile;
17920 struct mapped_symtab *symtab;
17921 struct obstack *types_list;
17922 htab_t psyms_seen;
17923 int cu_index;
17924 };
17925
17926 /* A helper function that writes a single signatured_type to an
17927 obstack. */
17928
17929 static int
17930 write_one_signatured_type (void **slot, void *d)
17931 {
17932 struct signatured_type_index_data *info = d;
17933 struct signatured_type *entry = (struct signatured_type *) *slot;
17934 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17935 struct partial_symtab *psymtab = per_cu->v.psymtab;
17936 gdb_byte val[8];
17937
17938 write_psymbols (info->symtab,
17939 info->psyms_seen,
17940 info->objfile->global_psymbols.list
17941 + psymtab->globals_offset,
17942 psymtab->n_global_syms, info->cu_index,
17943 0);
17944 write_psymbols (info->symtab,
17945 info->psyms_seen,
17946 info->objfile->static_psymbols.list
17947 + psymtab->statics_offset,
17948 psymtab->n_static_syms, info->cu_index,
17949 1);
17950
17951 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17952 entry->per_cu.offset.sect_off);
17953 obstack_grow (info->types_list, val, 8);
17954 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17955 entry->type_offset_in_tu.cu_off);
17956 obstack_grow (info->types_list, val, 8);
17957 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17958 obstack_grow (info->types_list, val, 8);
17959
17960 ++info->cu_index;
17961
17962 return 1;
17963 }
17964
17965 /* Recurse into all "included" dependencies and write their symbols as
17966 if they appeared in this psymtab. */
17967
17968 static void
17969 recursively_write_psymbols (struct objfile *objfile,
17970 struct partial_symtab *psymtab,
17971 struct mapped_symtab *symtab,
17972 htab_t psyms_seen,
17973 offset_type cu_index)
17974 {
17975 int i;
17976
17977 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17978 if (psymtab->dependencies[i]->user != NULL)
17979 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17980 symtab, psyms_seen, cu_index);
17981
17982 write_psymbols (symtab,
17983 psyms_seen,
17984 objfile->global_psymbols.list + psymtab->globals_offset,
17985 psymtab->n_global_syms, cu_index,
17986 0);
17987 write_psymbols (symtab,
17988 psyms_seen,
17989 objfile->static_psymbols.list + psymtab->statics_offset,
17990 psymtab->n_static_syms, cu_index,
17991 1);
17992 }
17993
17994 /* Create an index file for OBJFILE in the directory DIR. */
17995
17996 static void
17997 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17998 {
17999 struct cleanup *cleanup;
18000 char *filename, *cleanup_filename;
18001 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18002 struct obstack cu_list, types_cu_list;
18003 int i;
18004 FILE *out_file;
18005 struct mapped_symtab *symtab;
18006 offset_type val, size_of_contents, total_len;
18007 struct stat st;
18008 htab_t psyms_seen;
18009 htab_t cu_index_htab;
18010 struct psymtab_cu_index_map *psymtab_cu_index_map;
18011
18012 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18013 return;
18014
18015 if (dwarf2_per_objfile->using_index)
18016 error (_("Cannot use an index to create the index"));
18017
18018 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18019 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18020
18021 if (stat (objfile->name, &st) < 0)
18022 perror_with_name (objfile->name);
18023
18024 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18025 INDEX_SUFFIX, (char *) NULL);
18026 cleanup = make_cleanup (xfree, filename);
18027
18028 out_file = fopen (filename, "wb");
18029 if (!out_file)
18030 error (_("Can't open `%s' for writing"), filename);
18031
18032 cleanup_filename = filename;
18033 make_cleanup (unlink_if_set, &cleanup_filename);
18034
18035 symtab = create_mapped_symtab ();
18036 make_cleanup (cleanup_mapped_symtab, symtab);
18037
18038 obstack_init (&addr_obstack);
18039 make_cleanup_obstack_free (&addr_obstack);
18040
18041 obstack_init (&cu_list);
18042 make_cleanup_obstack_free (&cu_list);
18043
18044 obstack_init (&types_cu_list);
18045 make_cleanup_obstack_free (&types_cu_list);
18046
18047 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18048 NULL, xcalloc, xfree);
18049 make_cleanup_htab_delete (psyms_seen);
18050
18051 /* While we're scanning CU's create a table that maps a psymtab pointer
18052 (which is what addrmap records) to its index (which is what is recorded
18053 in the index file). This will later be needed to write the address
18054 table. */
18055 cu_index_htab = htab_create_alloc (100,
18056 hash_psymtab_cu_index,
18057 eq_psymtab_cu_index,
18058 NULL, xcalloc, xfree);
18059 make_cleanup_htab_delete (cu_index_htab);
18060 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18061 xmalloc (sizeof (struct psymtab_cu_index_map)
18062 * dwarf2_per_objfile->n_comp_units);
18063 make_cleanup (xfree, psymtab_cu_index_map);
18064
18065 /* The CU list is already sorted, so we don't need to do additional
18066 work here. Also, the debug_types entries do not appear in
18067 all_comp_units, but only in their own hash table. */
18068 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18069 {
18070 struct dwarf2_per_cu_data *per_cu
18071 = dwarf2_per_objfile->all_comp_units[i];
18072 struct partial_symtab *psymtab = per_cu->v.psymtab;
18073 gdb_byte val[8];
18074 struct psymtab_cu_index_map *map;
18075 void **slot;
18076
18077 if (psymtab->user == NULL)
18078 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
18079
18080 map = &psymtab_cu_index_map[i];
18081 map->psymtab = psymtab;
18082 map->cu_index = i;
18083 slot = htab_find_slot (cu_index_htab, map, INSERT);
18084 gdb_assert (slot != NULL);
18085 gdb_assert (*slot == NULL);
18086 *slot = map;
18087
18088 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18089 per_cu->offset.sect_off);
18090 obstack_grow (&cu_list, val, 8);
18091 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18092 obstack_grow (&cu_list, val, 8);
18093 }
18094
18095 /* Dump the address map. */
18096 write_address_map (objfile, &addr_obstack, cu_index_htab);
18097
18098 /* Write out the .debug_type entries, if any. */
18099 if (dwarf2_per_objfile->signatured_types)
18100 {
18101 struct signatured_type_index_data sig_data;
18102
18103 sig_data.objfile = objfile;
18104 sig_data.symtab = symtab;
18105 sig_data.types_list = &types_cu_list;
18106 sig_data.psyms_seen = psyms_seen;
18107 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18108 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18109 write_one_signatured_type, &sig_data);
18110 }
18111
18112 /* Now that we've processed all symbols we can shrink their cu_indices
18113 lists. */
18114 uniquify_cu_indices (symtab);
18115
18116 obstack_init (&constant_pool);
18117 make_cleanup_obstack_free (&constant_pool);
18118 obstack_init (&symtab_obstack);
18119 make_cleanup_obstack_free (&symtab_obstack);
18120 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18121
18122 obstack_init (&contents);
18123 make_cleanup_obstack_free (&contents);
18124 size_of_contents = 6 * sizeof (offset_type);
18125 total_len = size_of_contents;
18126
18127 /* The version number. */
18128 val = MAYBE_SWAP (7);
18129 obstack_grow (&contents, &val, sizeof (val));
18130
18131 /* The offset of the CU list from the start of the file. */
18132 val = MAYBE_SWAP (total_len);
18133 obstack_grow (&contents, &val, sizeof (val));
18134 total_len += obstack_object_size (&cu_list);
18135
18136 /* The offset of the types CU list from the start of the file. */
18137 val = MAYBE_SWAP (total_len);
18138 obstack_grow (&contents, &val, sizeof (val));
18139 total_len += obstack_object_size (&types_cu_list);
18140
18141 /* The offset of the address table from the start of the file. */
18142 val = MAYBE_SWAP (total_len);
18143 obstack_grow (&contents, &val, sizeof (val));
18144 total_len += obstack_object_size (&addr_obstack);
18145
18146 /* The offset of the symbol table from the start of the file. */
18147 val = MAYBE_SWAP (total_len);
18148 obstack_grow (&contents, &val, sizeof (val));
18149 total_len += obstack_object_size (&symtab_obstack);
18150
18151 /* The offset of the constant pool from the start of the file. */
18152 val = MAYBE_SWAP (total_len);
18153 obstack_grow (&contents, &val, sizeof (val));
18154 total_len += obstack_object_size (&constant_pool);
18155
18156 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18157
18158 write_obstack (out_file, &contents);
18159 write_obstack (out_file, &cu_list);
18160 write_obstack (out_file, &types_cu_list);
18161 write_obstack (out_file, &addr_obstack);
18162 write_obstack (out_file, &symtab_obstack);
18163 write_obstack (out_file, &constant_pool);
18164
18165 fclose (out_file);
18166
18167 /* We want to keep the file, so we set cleanup_filename to NULL
18168 here. See unlink_if_set. */
18169 cleanup_filename = NULL;
18170
18171 do_cleanups (cleanup);
18172 }
18173
18174 /* Implementation of the `save gdb-index' command.
18175
18176 Note that the file format used by this command is documented in the
18177 GDB manual. Any changes here must be documented there. */
18178
18179 static void
18180 save_gdb_index_command (char *arg, int from_tty)
18181 {
18182 struct objfile *objfile;
18183
18184 if (!arg || !*arg)
18185 error (_("usage: save gdb-index DIRECTORY"));
18186
18187 ALL_OBJFILES (objfile)
18188 {
18189 struct stat st;
18190
18191 /* If the objfile does not correspond to an actual file, skip it. */
18192 if (stat (objfile->name, &st) < 0)
18193 continue;
18194
18195 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18196 if (dwarf2_per_objfile)
18197 {
18198 volatile struct gdb_exception except;
18199
18200 TRY_CATCH (except, RETURN_MASK_ERROR)
18201 {
18202 write_psymtabs_to_index (objfile, arg);
18203 }
18204 if (except.reason < 0)
18205 exception_fprintf (gdb_stderr, except,
18206 _("Error while writing index for `%s': "),
18207 objfile->name);
18208 }
18209 }
18210 }
18211
18212 \f
18213
18214 int dwarf2_always_disassemble;
18215
18216 static void
18217 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18218 struct cmd_list_element *c, const char *value)
18219 {
18220 fprintf_filtered (file,
18221 _("Whether to always disassemble "
18222 "DWARF expressions is %s.\n"),
18223 value);
18224 }
18225
18226 static void
18227 show_check_physname (struct ui_file *file, int from_tty,
18228 struct cmd_list_element *c, const char *value)
18229 {
18230 fprintf_filtered (file,
18231 _("Whether to check \"physname\" is %s.\n"),
18232 value);
18233 }
18234
18235 void _initialize_dwarf2_read (void);
18236
18237 void
18238 _initialize_dwarf2_read (void)
18239 {
18240 struct cmd_list_element *c;
18241
18242 dwarf2_objfile_data_key
18243 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18244
18245 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18246 Set DWARF 2 specific variables.\n\
18247 Configure DWARF 2 variables such as the cache size"),
18248 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18249 0/*allow-unknown*/, &maintenance_set_cmdlist);
18250
18251 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18252 Show DWARF 2 specific variables\n\
18253 Show DWARF 2 variables such as the cache size"),
18254 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18255 0/*allow-unknown*/, &maintenance_show_cmdlist);
18256
18257 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18258 &dwarf2_max_cache_age, _("\
18259 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18260 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18261 A higher limit means that cached compilation units will be stored\n\
18262 in memory longer, and more total memory will be used. Zero disables\n\
18263 caching, which can slow down startup."),
18264 NULL,
18265 show_dwarf2_max_cache_age,
18266 &set_dwarf2_cmdlist,
18267 &show_dwarf2_cmdlist);
18268
18269 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18270 &dwarf2_always_disassemble, _("\
18271 Set whether `info address' always disassembles DWARF expressions."), _("\
18272 Show whether `info address' always disassembles DWARF expressions."), _("\
18273 When enabled, DWARF expressions are always printed in an assembly-like\n\
18274 syntax. When disabled, expressions will be printed in a more\n\
18275 conversational style, when possible."),
18276 NULL,
18277 show_dwarf2_always_disassemble,
18278 &set_dwarf2_cmdlist,
18279 &show_dwarf2_cmdlist);
18280
18281 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18282 Set debugging of the dwarf2 DIE reader."), _("\
18283 Show debugging of the dwarf2 DIE reader."), _("\
18284 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18285 The value is the maximum depth to print."),
18286 NULL,
18287 NULL,
18288 &setdebuglist, &showdebuglist);
18289
18290 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18291 Set cross-checking of \"physname\" code against demangler."), _("\
18292 Show cross-checking of \"physname\" code against demangler."), _("\
18293 When enabled, GDB's internal \"physname\" code is checked against\n\
18294 the demangler."),
18295 NULL, show_check_physname,
18296 &setdebuglist, &showdebuglist);
18297
18298 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18299 _("\
18300 Save a gdb-index file.\n\
18301 Usage: save gdb-index DIRECTORY"),
18302 &save_cmdlist);
18303 set_cmd_completer (c, filename_completer);
18304 }