* dwarf2read.c (read_and_check_comp_unit_head): Delete unnecessary
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "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, print basic high level tracing messages.
86 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
87 static int dwarf2_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static int dwarf2_die_debug = 0;
91
92 /* When non-zero, cross-check physname against demangler. */
93 static int check_physname = 0;
94
95 /* When non-zero, do not reject deprecated .gdb_index sections. */
96 int use_deprecated_index_sections = 0;
97
98 static int pagesize;
99
100 /* When set, the file that we're processing is known to have debugging
101 info for C++ namespaces. GCC 3.3.x did not produce this information,
102 but later versions do. */
103
104 static int processing_has_namespace_info;
105
106 static const struct objfile_data *dwarf2_objfile_data_key;
107
108 struct dwarf2_section_info
109 {
110 asection *asection;
111 gdb_byte *buffer;
112 bfd_size_type size;
113 /* Not NULL if the section was actually mmapped. */
114 void *map_addr;
115 /* Page aligned size of mmapped area. */
116 bfd_size_type map_len;
117 /* True if we have tried to read this section. */
118 int readin;
119 };
120
121 typedef struct dwarf2_section_info dwarf2_section_info_def;
122 DEF_VEC_O (dwarf2_section_info_def);
123
124 /* All offsets in the index are of this type. It must be
125 architecture-independent. */
126 typedef uint32_t offset_type;
127
128 DEF_VEC_I (offset_type);
129
130 /* Ensure only legit values are used. */
131 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
132 do { \
133 gdb_assert ((unsigned int) (value) <= 1); \
134 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
135 } while (0)
136
137 /* Ensure only legit values are used. */
138 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
139 do { \
140 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
141 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
142 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
143 } while (0)
144
145 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
146 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
147 do { \
148 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
149 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
150 } while (0)
151
152 /* A description of the mapped index. The file format is described in
153 a comment by the code that writes the index. */
154 struct mapped_index
155 {
156 /* Index data format version. */
157 int version;
158
159 /* The total length of the buffer. */
160 off_t total_size;
161
162 /* A pointer to the address table data. */
163 const gdb_byte *address_table;
164
165 /* Size of the address table data in bytes. */
166 offset_type address_table_size;
167
168 /* The symbol table, implemented as a hash table. */
169 const offset_type *symbol_table;
170
171 /* Size in slots, each slot is 2 offset_types. */
172 offset_type symbol_table_slots;
173
174 /* A pointer to the constant pool. */
175 const char *constant_pool;
176 };
177
178 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
179 DEF_VEC_P (dwarf2_per_cu_ptr);
180
181 /* Collection of data recorded per objfile.
182 This hangs off of dwarf2_objfile_data_key. */
183
184 struct dwarf2_per_objfile
185 {
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
191 struct dwarf2_section_info macro;
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
194 struct dwarf2_section_info addr;
195 struct dwarf2_section_info frame;
196 struct dwarf2_section_info eh_frame;
197 struct dwarf2_section_info gdb_index;
198
199 VEC (dwarf2_section_info_def) *types;
200
201 /* Back link. */
202 struct objfile *objfile;
203
204 /* Table of all the compilation units. This is used to locate
205 the target compilation unit of a particular reference. */
206 struct dwarf2_per_cu_data **all_comp_units;
207
208 /* The number of compilation units in ALL_COMP_UNITS. */
209 int n_comp_units;
210
211 /* The number of .debug_types-related CUs. */
212 int n_type_units;
213
214 /* The .debug_types-related CUs (TUs). */
215 struct dwarf2_per_cu_data **all_type_units;
216
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
220
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
225 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
226 This is NULL if the table hasn't been allocated yet. */
227 htab_t dwo_files;
228
229 /* A flag indicating wether this objfile has a section loaded at a
230 VMA of 0. */
231 int has_section_at_zero;
232
233 /* True if we are using the mapped index,
234 or we are faking it for OBJF_READNOW's sake. */
235 unsigned char using_index;
236
237 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
238 struct mapped_index *index_table;
239
240 /* When using index_table, this keeps track of all quick_file_names entries.
241 TUs can share line table entries with CUs or other TUs, and there can be
242 a lot more TUs than unique line tables, so we maintain a separate table
243 of all line table entries to support the sharing. */
244 htab_t quick_file_names_table;
245
246 /* Set during partial symbol reading, to prevent queueing of full
247 symbols. */
248 int reading_partial_symbols;
249
250 /* Table mapping type DIEs to their struct type *.
251 This is NULL if not allocated yet.
252 The mapping is done via (CU/TU signature + DIE offset) -> type. */
253 htab_t die_type_hash;
254
255 /* The CUs we recently read. */
256 VEC (dwarf2_per_cu_ptr) *just_read_cus;
257 };
258
259 static struct dwarf2_per_objfile *dwarf2_per_objfile;
260
261 /* Default names of the debugging sections. */
262
263 /* Note that if the debugging section has been compressed, it might
264 have a name like .zdebug_info. */
265
266 static const struct dwarf2_debug_sections dwarf2_elf_names =
267 {
268 { ".debug_info", ".zdebug_info" },
269 { ".debug_abbrev", ".zdebug_abbrev" },
270 { ".debug_line", ".zdebug_line" },
271 { ".debug_loc", ".zdebug_loc" },
272 { ".debug_macinfo", ".zdebug_macinfo" },
273 { ".debug_macro", ".zdebug_macro" },
274 { ".debug_str", ".zdebug_str" },
275 { ".debug_ranges", ".zdebug_ranges" },
276 { ".debug_types", ".zdebug_types" },
277 { ".debug_addr", ".zdebug_addr" },
278 { ".debug_frame", ".zdebug_frame" },
279 { ".eh_frame", NULL },
280 { ".gdb_index", ".zgdb_index" },
281 23
282 };
283
284 /* List of DWO sections. */
285
286 static const struct dwo_section_names
287 {
288 struct dwarf2_section_names abbrev_dwo;
289 struct dwarf2_section_names info_dwo;
290 struct dwarf2_section_names line_dwo;
291 struct dwarf2_section_names loc_dwo;
292 struct dwarf2_section_names macinfo_dwo;
293 struct dwarf2_section_names macro_dwo;
294 struct dwarf2_section_names str_dwo;
295 struct dwarf2_section_names str_offsets_dwo;
296 struct dwarf2_section_names types_dwo;
297 }
298 dwo_section_names =
299 {
300 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
301 { ".debug_info.dwo", ".zdebug_info.dwo" },
302 { ".debug_line.dwo", ".zdebug_line.dwo" },
303 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
304 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
305 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
306 { ".debug_str.dwo", ".zdebug_str.dwo" },
307 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
308 { ".debug_types.dwo", ".zdebug_types.dwo" },
309 };
310
311 /* local data types */
312
313 /* We hold several abbreviation tables in memory at the same time. */
314 #ifndef ABBREV_HASH_SIZE
315 #define ABBREV_HASH_SIZE 121
316 #endif
317
318 /* The data in a compilation unit header, after target2host
319 translation, looks like this. */
320 struct comp_unit_head
321 {
322 unsigned int length;
323 short version;
324 unsigned char addr_size;
325 unsigned char signed_addr_p;
326 sect_offset abbrev_offset;
327
328 /* Size of file offsets; either 4 or 8. */
329 unsigned int offset_size;
330
331 /* Size of the length field; either 4 or 12. */
332 unsigned int initial_length_size;
333
334 /* Offset to the first byte of this compilation unit header in the
335 .debug_info section, for resolving relative reference dies. */
336 sect_offset offset;
337
338 /* Offset to first die in this cu from the start of the cu.
339 This will be the first byte following the compilation unit header. */
340 cu_offset first_die_offset;
341 };
342
343 /* Type used for delaying computation of method physnames.
344 See comments for compute_delayed_physnames. */
345 struct delayed_method_info
346 {
347 /* The type to which the method is attached, i.e., its parent class. */
348 struct type *type;
349
350 /* The index of the method in the type's function fieldlists. */
351 int fnfield_index;
352
353 /* The index of the method in the fieldlist. */
354 int index;
355
356 /* The name of the DIE. */
357 const char *name;
358
359 /* The DIE associated with this method. */
360 struct die_info *die;
361 };
362
363 typedef struct delayed_method_info delayed_method_info;
364 DEF_VEC_O (delayed_method_info);
365
366 /* Internal state when decoding a particular compilation unit. */
367 struct dwarf2_cu
368 {
369 /* The objfile containing this compilation unit. */
370 struct objfile *objfile;
371
372 /* The header of the compilation unit. */
373 struct comp_unit_head header;
374
375 /* Base address of this compilation unit. */
376 CORE_ADDR base_address;
377
378 /* Non-zero if base_address has been set. */
379 int base_known;
380
381 /* The language we are debugging. */
382 enum language language;
383 const struct language_defn *language_defn;
384
385 const char *producer;
386
387 /* The generic symbol table building routines have separate lists for
388 file scope symbols and all all other scopes (local scopes). So
389 we need to select the right one to pass to add_symbol_to_list().
390 We do it by keeping a pointer to the correct list in list_in_scope.
391
392 FIXME: The original dwarf code just treated the file scope as the
393 first local scope, and all other local scopes as nested local
394 scopes, and worked fine. Check to see if we really need to
395 distinguish these in buildsym.c. */
396 struct pending **list_in_scope;
397
398 /* DWARF abbreviation table associated with this compilation unit. */
399 struct abbrev_info **dwarf2_abbrevs;
400
401 /* Storage for the abbrev table. */
402 struct obstack abbrev_obstack;
403
404 /* Hash table holding all the loaded partial DIEs
405 with partial_die->offset.SECT_OFF as hash. */
406 htab_t partial_dies;
407
408 /* Storage for things with the same lifetime as this read-in compilation
409 unit, including partial DIEs. */
410 struct obstack comp_unit_obstack;
411
412 /* When multiple dwarf2_cu structures are living in memory, this field
413 chains them all together, so that they can be released efficiently.
414 We will probably also want a generation counter so that most-recently-used
415 compilation units are cached... */
416 struct dwarf2_per_cu_data *read_in_chain;
417
418 /* Backchain to our per_cu entry if the tree has been built. */
419 struct dwarf2_per_cu_data *per_cu;
420
421 /* How many compilation units ago was this CU last referenced? */
422 int last_used;
423
424 /* A hash table of DIE cu_offset for following references with
425 die_info->offset.sect_off as hash. */
426 htab_t die_hash;
427
428 /* Full DIEs if read in. */
429 struct die_info *dies;
430
431 /* A set of pointers to dwarf2_per_cu_data objects for compilation
432 units referenced by this one. Only set during full symbol processing;
433 partial symbol tables do not have dependencies. */
434 htab_t dependencies;
435
436 /* Header data from the line table, during full symbol processing. */
437 struct line_header *line_header;
438
439 /* A list of methods which need to have physnames computed
440 after all type information has been read. */
441 VEC (delayed_method_info) *method_list;
442
443 /* To be copied to symtab->call_site_htab. */
444 htab_t call_site_htab;
445
446 /* Non-NULL if this CU came from a DWO file.
447 There is an invariant here that is important to remember:
448 Except for attributes copied from the top level DIE in the "main"
449 (or "stub") file in preparation for reading the DWO file
450 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
451 Either there isn't a DWO file (in which case this is NULL and the point
452 is moot), or there is and either we're not going to read it (in which
453 case this is NULL) or there is and we are reading it (in which case this
454 is non-NULL). */
455 struct dwo_unit *dwo_unit;
456
457 /* The DW_AT_addr_base attribute if present, zero otherwise
458 (zero is a valid value though).
459 Note this value comes from the stub CU/TU's DIE. */
460 ULONGEST addr_base;
461
462 /* Mark used when releasing cached dies. */
463 unsigned int mark : 1;
464
465 /* This CU references .debug_loc. See the symtab->locations_valid field.
466 This test is imperfect as there may exist optimized debug code not using
467 any location list and still facing inlining issues if handled as
468 unoptimized code. For a future better test see GCC PR other/32998. */
469 unsigned int has_loclist : 1;
470
471 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
472 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
473 are valid. This information is cached because profiling CU expansion
474 showed excessive time spent in producer_is_gxx_lt_4_6. */
475 unsigned int checked_producer : 1;
476 unsigned int producer_is_gxx_lt_4_6 : 1;
477 unsigned int producer_is_icc : 1;
478
479 /* Non-zero if DW_AT_addr_base was found.
480 Used when processing DWO files. */
481 unsigned int have_addr_base : 1;
482 };
483
484 /* Persistent data held for a compilation unit, even when not
485 processing it. We put a pointer to this structure in the
486 read_symtab_private field of the psymtab. */
487
488 struct dwarf2_per_cu_data
489 {
490 /* The start offset and length of this compilation unit. 2**29-1
491 bytes should suffice to store the length of any compilation unit
492 - if it doesn't, GDB will fall over anyway.
493 NOTE: Unlike comp_unit_head.length, this length includes
494 initial_length_size.
495 If the DIE refers to a DWO file, this is always of the original die,
496 not the DWO file. */
497 sect_offset offset;
498 unsigned int length : 29;
499
500 /* Flag indicating this compilation unit will be read in before
501 any of the current compilation units are processed. */
502 unsigned int queued : 1;
503
504 /* This flag will be set when reading partial DIEs if we need to load
505 absolutely all DIEs for this compilation unit, instead of just the ones
506 we think are interesting. It gets set if we look for a DIE in the
507 hash table and don't find it. */
508 unsigned int load_all_dies : 1;
509
510 /* Non-zero if this CU is from .debug_types. */
511 unsigned int is_debug_types : 1;
512
513 /* The section this CU/TU lives in.
514 If the DIE refers to a DWO file, this is always the original die,
515 not the DWO file. */
516 struct dwarf2_section_info *info_or_types_section;
517
518 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
519 of the CU cache it gets reset to NULL again. */
520 struct dwarf2_cu *cu;
521
522 /* The corresponding objfile.
523 Normally we can get the objfile from dwarf2_per_objfile.
524 However we can enter this file with just a "per_cu" handle. */
525 struct objfile *objfile;
526
527 /* When using partial symbol tables, the 'psymtab' field is active.
528 Otherwise the 'quick' field is active. */
529 union
530 {
531 /* The partial symbol table associated with this compilation unit,
532 or NULL for unread partial units. */
533 struct partial_symtab *psymtab;
534
535 /* Data needed by the "quick" functions. */
536 struct dwarf2_per_cu_quick_data *quick;
537 } v;
538
539 /* The CUs we import using DW_TAG_imported_unit. This is filled in
540 while reading psymtabs, used to compute the psymtab dependencies,
541 and then cleared. Then it is filled in again while reading full
542 symbols, and only deleted when the objfile is destroyed. */
543 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
544 };
545
546 /* Entry in the signatured_types hash table. */
547
548 struct signatured_type
549 {
550 /* The type's signature. */
551 ULONGEST signature;
552
553 /* Offset in the TU of the type's DIE, as read from the TU header.
554 If the definition lives in a DWO file, this value is unusable. */
555 cu_offset type_offset_in_tu;
556
557 /* Offset in the section of the type's DIE.
558 If the definition lives in a DWO file, this is the offset in the
559 .debug_types.dwo section.
560 The value is zero until the actual value is known.
561 Zero is otherwise not a valid section offset. */
562 sect_offset type_offset_in_section;
563
564 /* The CU(/TU) of this type. */
565 struct dwarf2_per_cu_data per_cu;
566 };
567
568 /* These sections are what may appear in a "dwo" file. */
569
570 struct dwo_sections
571 {
572 struct dwarf2_section_info abbrev;
573 struct dwarf2_section_info info;
574 struct dwarf2_section_info line;
575 struct dwarf2_section_info loc;
576 struct dwarf2_section_info macinfo;
577 struct dwarf2_section_info macro;
578 struct dwarf2_section_info str;
579 struct dwarf2_section_info str_offsets;
580 VEC (dwarf2_section_info_def) *types;
581 };
582
583 /* Common bits of DWO CUs/TUs. */
584
585 struct dwo_unit
586 {
587 /* Backlink to the containing struct dwo_file. */
588 struct dwo_file *dwo_file;
589
590 /* The "id" that distinguishes this CU/TU.
591 .debug_info calls this "dwo_id", .debug_types calls this "signature".
592 Since signatures came first, we stick with it for consistency. */
593 ULONGEST signature;
594
595 /* The section this CU/TU lives in, in the DWO file. */
596 struct dwarf2_section_info *info_or_types_section;
597
598 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
599 sect_offset offset;
600 unsigned int length;
601
602 /* For types, offset in the type's DIE of the type defined by this TU. */
603 cu_offset type_offset_in_tu;
604 };
605
606 /* Data for one DWO file. */
607
608 struct dwo_file
609 {
610 /* The DW_AT_GNU_dwo_name attribute.
611 We don't manage space for this, it's an attribute. */
612 const char *dwo_name;
613
614 /* The bfd, when the file is open. Otherwise this is NULL. */
615 bfd *dwo_bfd;
616
617 /* Section info for this file. */
618 struct dwo_sections sections;
619
620 /* Table of CUs in the file.
621 Each element is a struct dwo_unit. */
622 htab_t cus;
623
624 /* Table of TUs in the file.
625 Each element is a struct dwo_unit. */
626 htab_t tus;
627 };
628
629 /* Struct used to pass misc. parameters to read_die_and_children, et
630 al. which are used for both .debug_info and .debug_types dies.
631 All parameters here are unchanging for the life of the call. This
632 struct exists to abstract away the constant parameters of die reading. */
633
634 struct die_reader_specs
635 {
636 /* die_section->asection->owner. */
637 bfd* abfd;
638
639 /* The CU of the DIE we are parsing. */
640 struct dwarf2_cu *cu;
641
642 /* Non-NULL if reading a DWO file. */
643 struct dwo_file *dwo_file;
644
645 /* The section the die comes from.
646 This is either .debug_info or .debug_types, or the .dwo variants. */
647 struct dwarf2_section_info *die_section;
648
649 /* die_section->buffer. */
650 gdb_byte *buffer;
651
652 /* The end of the buffer. */
653 const gdb_byte *buffer_end;
654 };
655
656 /* Type of function passed to init_cutu_and_read_dies, et.al. */
657 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
658 gdb_byte *info_ptr,
659 struct die_info *comp_unit_die,
660 int has_children,
661 void *data);
662
663 /* The line number information for a compilation unit (found in the
664 .debug_line section) begins with a "statement program header",
665 which contains the following information. */
666 struct line_header
667 {
668 unsigned int total_length;
669 unsigned short version;
670 unsigned int header_length;
671 unsigned char minimum_instruction_length;
672 unsigned char maximum_ops_per_instruction;
673 unsigned char default_is_stmt;
674 int line_base;
675 unsigned char line_range;
676 unsigned char opcode_base;
677
678 /* standard_opcode_lengths[i] is the number of operands for the
679 standard opcode whose value is i. This means that
680 standard_opcode_lengths[0] is unused, and the last meaningful
681 element is standard_opcode_lengths[opcode_base - 1]. */
682 unsigned char *standard_opcode_lengths;
683
684 /* The include_directories table. NOTE! These strings are not
685 allocated with xmalloc; instead, they are pointers into
686 debug_line_buffer. If you try to free them, `free' will get
687 indigestion. */
688 unsigned int num_include_dirs, include_dirs_size;
689 char **include_dirs;
690
691 /* The file_names table. NOTE! These strings are not allocated
692 with xmalloc; instead, they are pointers into debug_line_buffer.
693 Don't try to free them directly. */
694 unsigned int num_file_names, file_names_size;
695 struct file_entry
696 {
697 char *name;
698 unsigned int dir_index;
699 unsigned int mod_time;
700 unsigned int length;
701 int included_p; /* Non-zero if referenced by the Line Number Program. */
702 struct symtab *symtab; /* The associated symbol table, if any. */
703 } *file_names;
704
705 /* The start and end of the statement program following this
706 header. These point into dwarf2_per_objfile->line_buffer. */
707 gdb_byte *statement_program_start, *statement_program_end;
708 };
709
710 /* When we construct a partial symbol table entry we only
711 need this much information. */
712 struct partial_die_info
713 {
714 /* Offset of this DIE. */
715 sect_offset offset;
716
717 /* DWARF-2 tag for this DIE. */
718 ENUM_BITFIELD(dwarf_tag) tag : 16;
719
720 /* Assorted flags describing the data found in this DIE. */
721 unsigned int has_children : 1;
722 unsigned int is_external : 1;
723 unsigned int is_declaration : 1;
724 unsigned int has_type : 1;
725 unsigned int has_specification : 1;
726 unsigned int has_pc_info : 1;
727 unsigned int may_be_inlined : 1;
728
729 /* Flag set if the SCOPE field of this structure has been
730 computed. */
731 unsigned int scope_set : 1;
732
733 /* Flag set if the DIE has a byte_size attribute. */
734 unsigned int has_byte_size : 1;
735
736 /* Flag set if any of the DIE's children are template arguments. */
737 unsigned int has_template_arguments : 1;
738
739 /* Flag set if fixup_partial_die has been called on this die. */
740 unsigned int fixup_called : 1;
741
742 /* The name of this DIE. Normally the value of DW_AT_name, but
743 sometimes a default name for unnamed DIEs. */
744 char *name;
745
746 /* The linkage name, if present. */
747 const char *linkage_name;
748
749 /* The scope to prepend to our children. This is generally
750 allocated on the comp_unit_obstack, so will disappear
751 when this compilation unit leaves the cache. */
752 char *scope;
753
754 /* Some data associated with the partial DIE. The tag determines
755 which field is live. */
756 union
757 {
758 /* The location description associated with this DIE, if any. */
759 struct dwarf_block *locdesc;
760 /* The offset of an import, for DW_TAG_imported_unit. */
761 sect_offset offset;
762 } d;
763
764 /* If HAS_PC_INFO, the PC range associated with this DIE. */
765 CORE_ADDR lowpc;
766 CORE_ADDR highpc;
767
768 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
769 DW_AT_sibling, if any. */
770 /* NOTE: This member isn't strictly necessary, read_partial_die could
771 return DW_AT_sibling values to its caller load_partial_dies. */
772 gdb_byte *sibling;
773
774 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
775 DW_AT_specification (or DW_AT_abstract_origin or
776 DW_AT_extension). */
777 sect_offset spec_offset;
778
779 /* Pointers to this DIE's parent, first child, and next sibling,
780 if any. */
781 struct partial_die_info *die_parent, *die_child, *die_sibling;
782 };
783
784 /* This data structure holds the information of an abbrev. */
785 struct abbrev_info
786 {
787 unsigned int number; /* number identifying abbrev */
788 enum dwarf_tag tag; /* dwarf tag */
789 unsigned short has_children; /* boolean */
790 unsigned short num_attrs; /* number of attributes */
791 struct attr_abbrev *attrs; /* an array of attribute descriptions */
792 struct abbrev_info *next; /* next in chain */
793 };
794
795 struct attr_abbrev
796 {
797 ENUM_BITFIELD(dwarf_attribute) name : 16;
798 ENUM_BITFIELD(dwarf_form) form : 16;
799 };
800
801 /* Attributes have a name and a value. */
802 struct attribute
803 {
804 ENUM_BITFIELD(dwarf_attribute) name : 16;
805 ENUM_BITFIELD(dwarf_form) form : 15;
806
807 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
808 field should be in u.str (existing only for DW_STRING) but it is kept
809 here for better struct attribute alignment. */
810 unsigned int string_is_canonical : 1;
811
812 union
813 {
814 char *str;
815 struct dwarf_block *blk;
816 ULONGEST unsnd;
817 LONGEST snd;
818 CORE_ADDR addr;
819 struct signatured_type *signatured_type;
820 }
821 u;
822 };
823
824 /* This data structure holds a complete die structure. */
825 struct die_info
826 {
827 /* DWARF-2 tag for this DIE. */
828 ENUM_BITFIELD(dwarf_tag) tag : 16;
829
830 /* Number of attributes */
831 unsigned char num_attrs;
832
833 /* True if we're presently building the full type name for the
834 type derived from this DIE. */
835 unsigned char building_fullname : 1;
836
837 /* Abbrev number */
838 unsigned int abbrev;
839
840 /* Offset in .debug_info or .debug_types section. */
841 sect_offset offset;
842
843 /* The dies in a compilation unit form an n-ary tree. PARENT
844 points to this die's parent; CHILD points to the first child of
845 this node; and all the children of a given node are chained
846 together via their SIBLING fields. */
847 struct die_info *child; /* Its first child, if any. */
848 struct die_info *sibling; /* Its next sibling, if any. */
849 struct die_info *parent; /* Its parent, if any. */
850
851 /* An array of attributes, with NUM_ATTRS elements. There may be
852 zero, but it's not common and zero-sized arrays are not
853 sufficiently portable C. */
854 struct attribute attrs[1];
855 };
856
857 /* Get at parts of an attribute structure. */
858
859 #define DW_STRING(attr) ((attr)->u.str)
860 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
861 #define DW_UNSND(attr) ((attr)->u.unsnd)
862 #define DW_BLOCK(attr) ((attr)->u.blk)
863 #define DW_SND(attr) ((attr)->u.snd)
864 #define DW_ADDR(attr) ((attr)->u.addr)
865 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
866
867 /* Blocks are a bunch of untyped bytes. */
868 struct dwarf_block
869 {
870 unsigned int size;
871
872 /* Valid only if SIZE is not zero. */
873 gdb_byte *data;
874 };
875
876 #ifndef ATTR_ALLOC_CHUNK
877 #define ATTR_ALLOC_CHUNK 4
878 #endif
879
880 /* Allocate fields for structs, unions and enums in this size. */
881 #ifndef DW_FIELD_ALLOC_CHUNK
882 #define DW_FIELD_ALLOC_CHUNK 4
883 #endif
884
885 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
886 but this would require a corresponding change in unpack_field_as_long
887 and friends. */
888 static int bits_per_byte = 8;
889
890 /* The routines that read and process dies for a C struct or C++ class
891 pass lists of data member fields and lists of member function fields
892 in an instance of a field_info structure, as defined below. */
893 struct field_info
894 {
895 /* List of data member and baseclasses fields. */
896 struct nextfield
897 {
898 struct nextfield *next;
899 int accessibility;
900 int virtuality;
901 struct field field;
902 }
903 *fields, *baseclasses;
904
905 /* Number of fields (including baseclasses). */
906 int nfields;
907
908 /* Number of baseclasses. */
909 int nbaseclasses;
910
911 /* Set if the accesibility of one of the fields is not public. */
912 int non_public_fields;
913
914 /* Member function fields array, entries are allocated in the order they
915 are encountered in the object file. */
916 struct nextfnfield
917 {
918 struct nextfnfield *next;
919 struct fn_field fnfield;
920 }
921 *fnfields;
922
923 /* Member function fieldlist array, contains name of possibly overloaded
924 member function, number of overloaded member functions and a pointer
925 to the head of the member function field chain. */
926 struct fnfieldlist
927 {
928 char *name;
929 int length;
930 struct nextfnfield *head;
931 }
932 *fnfieldlists;
933
934 /* Number of entries in the fnfieldlists array. */
935 int nfnfields;
936
937 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
938 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
939 struct typedef_field_list
940 {
941 struct typedef_field field;
942 struct typedef_field_list *next;
943 }
944 *typedef_field_list;
945 unsigned typedef_field_list_count;
946 };
947
948 /* One item on the queue of compilation units to read in full symbols
949 for. */
950 struct dwarf2_queue_item
951 {
952 struct dwarf2_per_cu_data *per_cu;
953 enum language pretend_language;
954 struct dwarf2_queue_item *next;
955 };
956
957 /* The current queue. */
958 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
959
960 /* Loaded secondary compilation units are kept in memory until they
961 have not been referenced for the processing of this many
962 compilation units. Set this to zero to disable caching. Cache
963 sizes of up to at least twenty will improve startup time for
964 typical inter-CU-reference binaries, at an obvious memory cost. */
965 static int dwarf2_max_cache_age = 5;
966 static void
967 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
968 struct cmd_list_element *c, const char *value)
969 {
970 fprintf_filtered (file, _("The upper bound on the age of cached "
971 "dwarf2 compilation units is %s.\n"),
972 value);
973 }
974
975
976 /* Various complaints about symbol reading that don't abort the process. */
977
978 static void
979 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
980 {
981 complaint (&symfile_complaints,
982 _("statement list doesn't fit in .debug_line section"));
983 }
984
985 static void
986 dwarf2_debug_line_missing_file_complaint (void)
987 {
988 complaint (&symfile_complaints,
989 _(".debug_line section has line data without a file"));
990 }
991
992 static void
993 dwarf2_debug_line_missing_end_sequence_complaint (void)
994 {
995 complaint (&symfile_complaints,
996 _(".debug_line section has line "
997 "program sequence without an end"));
998 }
999
1000 static void
1001 dwarf2_complex_location_expr_complaint (void)
1002 {
1003 complaint (&symfile_complaints, _("location expression too complex"));
1004 }
1005
1006 static void
1007 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1008 int arg3)
1009 {
1010 complaint (&symfile_complaints,
1011 _("const value length mismatch for '%s', got %d, expected %d"),
1012 arg1, arg2, arg3);
1013 }
1014
1015 static void
1016 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1017 {
1018 complaint (&symfile_complaints,
1019 _("debug info runs off end of %s section"
1020 " [in module %s]"),
1021 section->asection->name,
1022 bfd_get_filename (section->asection->owner));
1023 }
1024
1025 static void
1026 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1027 {
1028 complaint (&symfile_complaints,
1029 _("macro debug info contains a "
1030 "malformed macro definition:\n`%s'"),
1031 arg1);
1032 }
1033
1034 static void
1035 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1036 {
1037 complaint (&symfile_complaints,
1038 _("invalid attribute class or form for '%s' in '%s'"),
1039 arg1, arg2);
1040 }
1041
1042 /* local function prototypes */
1043
1044 static void dwarf2_locate_sections (bfd *, asection *, void *);
1045
1046 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1047 struct objfile *);
1048
1049 static void dwarf2_find_base_address (struct die_info *die,
1050 struct dwarf2_cu *cu);
1051
1052 static void dwarf2_build_psymtabs_hard (struct objfile *);
1053
1054 static void scan_partial_symbols (struct partial_die_info *,
1055 CORE_ADDR *, CORE_ADDR *,
1056 int, struct dwarf2_cu *);
1057
1058 static void add_partial_symbol (struct partial_die_info *,
1059 struct dwarf2_cu *);
1060
1061 static void add_partial_namespace (struct partial_die_info *pdi,
1062 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1063 int need_pc, struct dwarf2_cu *cu);
1064
1065 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1066 CORE_ADDR *highpc, int need_pc,
1067 struct dwarf2_cu *cu);
1068
1069 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1070 struct dwarf2_cu *cu);
1071
1072 static void add_partial_subprogram (struct partial_die_info *pdi,
1073 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1074 int need_pc, struct dwarf2_cu *cu);
1075
1076 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1077
1078 static void psymtab_to_symtab_1 (struct partial_symtab *);
1079
1080 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1081 struct dwarf2_section_info *);
1082
1083 static void dwarf2_free_abbrev_table (void *);
1084
1085 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1086
1087 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1088 struct dwarf2_cu *);
1089
1090 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1091 struct dwarf2_cu *);
1092
1093 static struct partial_die_info *load_partial_dies
1094 (const struct die_reader_specs *, gdb_byte *, int);
1095
1096 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1097 struct partial_die_info *,
1098 struct abbrev_info *,
1099 unsigned int,
1100 gdb_byte *);
1101
1102 static struct partial_die_info *find_partial_die (sect_offset,
1103 struct dwarf2_cu *);
1104
1105 static void fixup_partial_die (struct partial_die_info *,
1106 struct dwarf2_cu *);
1107
1108 static gdb_byte *read_attribute (const struct die_reader_specs *,
1109 struct attribute *, struct attr_abbrev *,
1110 gdb_byte *);
1111
1112 static unsigned int read_1_byte (bfd *, gdb_byte *);
1113
1114 static int read_1_signed_byte (bfd *, gdb_byte *);
1115
1116 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1117
1118 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1119
1120 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1121
1122 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1123 unsigned int *);
1124
1125 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1126
1127 static LONGEST read_checked_initial_length_and_offset
1128 (bfd *, gdb_byte *, const struct comp_unit_head *,
1129 unsigned int *, unsigned int *);
1130
1131 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1132 unsigned int *);
1133
1134 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1135
1136 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1137
1138 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1139
1140 static char *read_indirect_string (bfd *, gdb_byte *,
1141 const struct comp_unit_head *,
1142 unsigned int *);
1143
1144 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1145
1146 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1147
1148 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1149 unsigned int *);
1150
1151 static char *read_str_index (const struct die_reader_specs *reader,
1152 struct dwarf2_cu *cu, ULONGEST str_index);
1153
1154 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1155
1156 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1157 struct dwarf2_cu *);
1158
1159 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1160 unsigned int,
1161 struct dwarf2_cu *);
1162
1163 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1164 struct dwarf2_cu *cu);
1165
1166 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1167
1168 static struct die_info *die_specification (struct die_info *die,
1169 struct dwarf2_cu **);
1170
1171 static void free_line_header (struct line_header *lh);
1172
1173 static void add_file_name (struct line_header *, char *, unsigned int,
1174 unsigned int, unsigned int);
1175
1176 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1177 struct dwarf2_cu *cu);
1178
1179 static void dwarf_decode_lines (struct line_header *, const char *,
1180 struct dwarf2_cu *, struct partial_symtab *,
1181 int);
1182
1183 static void dwarf2_start_subfile (char *, const char *, const char *);
1184
1185 static struct symbol *new_symbol (struct die_info *, struct type *,
1186 struct dwarf2_cu *);
1187
1188 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1189 struct dwarf2_cu *, struct symbol *);
1190
1191 static void dwarf2_const_value (struct attribute *, struct symbol *,
1192 struct dwarf2_cu *);
1193
1194 static void dwarf2_const_value_attr (struct attribute *attr,
1195 struct type *type,
1196 const char *name,
1197 struct obstack *obstack,
1198 struct dwarf2_cu *cu, LONGEST *value,
1199 gdb_byte **bytes,
1200 struct dwarf2_locexpr_baton **baton);
1201
1202 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1203
1204 static int need_gnat_info (struct dwarf2_cu *);
1205
1206 static struct type *die_descriptive_type (struct die_info *,
1207 struct dwarf2_cu *);
1208
1209 static void set_descriptive_type (struct type *, struct die_info *,
1210 struct dwarf2_cu *);
1211
1212 static struct type *die_containing_type (struct die_info *,
1213 struct dwarf2_cu *);
1214
1215 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1216 struct dwarf2_cu *);
1217
1218 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1219
1220 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1221
1222 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1223
1224 static char *typename_concat (struct obstack *obs, const char *prefix,
1225 const char *suffix, int physname,
1226 struct dwarf2_cu *cu);
1227
1228 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1229
1230 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1231
1232 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1233
1234 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1235
1236 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1237
1238 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1239 struct dwarf2_cu *, struct partial_symtab *);
1240
1241 static int dwarf2_get_pc_bounds (struct die_info *,
1242 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1243 struct partial_symtab *);
1244
1245 static void get_scope_pc_bounds (struct die_info *,
1246 CORE_ADDR *, CORE_ADDR *,
1247 struct dwarf2_cu *);
1248
1249 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1250 CORE_ADDR, struct dwarf2_cu *);
1251
1252 static void dwarf2_add_field (struct field_info *, struct die_info *,
1253 struct dwarf2_cu *);
1254
1255 static void dwarf2_attach_fields_to_type (struct field_info *,
1256 struct type *, struct dwarf2_cu *);
1257
1258 static void dwarf2_add_member_fn (struct field_info *,
1259 struct die_info *, struct type *,
1260 struct dwarf2_cu *);
1261
1262 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1263 struct type *,
1264 struct dwarf2_cu *);
1265
1266 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1267
1268 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1269
1270 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1271
1272 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1273
1274 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1275
1276 static struct type *read_module_type (struct die_info *die,
1277 struct dwarf2_cu *cu);
1278
1279 static const char *namespace_name (struct die_info *die,
1280 int *is_anonymous, struct dwarf2_cu *);
1281
1282 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1283
1284 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1285
1286 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1287 struct dwarf2_cu *);
1288
1289 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1290 gdb_byte *info_ptr,
1291 gdb_byte **new_info_ptr,
1292 struct die_info *parent);
1293
1294 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1295 gdb_byte *info_ptr,
1296 gdb_byte **new_info_ptr,
1297 struct die_info *parent);
1298
1299 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1300 struct die_info **, gdb_byte *, int *, int);
1301
1302 static gdb_byte *read_full_die (const struct die_reader_specs *,
1303 struct die_info **, gdb_byte *, int *);
1304
1305 static void process_die (struct die_info *, struct dwarf2_cu *);
1306
1307 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1308 struct obstack *);
1309
1310 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1311
1312 static const char *dwarf2_full_name (char *name,
1313 struct die_info *die,
1314 struct dwarf2_cu *cu);
1315
1316 static struct die_info *dwarf2_extension (struct die_info *die,
1317 struct dwarf2_cu **);
1318
1319 static const char *dwarf_tag_name (unsigned int);
1320
1321 static const char *dwarf_attr_name (unsigned int);
1322
1323 static const char *dwarf_form_name (unsigned int);
1324
1325 static char *dwarf_bool_name (unsigned int);
1326
1327 static const char *dwarf_type_encoding_name (unsigned int);
1328
1329 static struct die_info *sibling_die (struct die_info *);
1330
1331 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1332
1333 static void dump_die_for_error (struct die_info *);
1334
1335 static void dump_die_1 (struct ui_file *, int level, int max_level,
1336 struct die_info *);
1337
1338 /*static*/ void dump_die (struct die_info *, int max_level);
1339
1340 static void store_in_ref_table (struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static int is_ref_attr (struct attribute *);
1344
1345 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1346
1347 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1348
1349 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1350 struct attribute *,
1351 struct dwarf2_cu **);
1352
1353 static struct die_info *follow_die_ref (struct die_info *,
1354 struct attribute *,
1355 struct dwarf2_cu **);
1356
1357 static struct die_info *follow_die_sig (struct die_info *,
1358 struct attribute *,
1359 struct dwarf2_cu **);
1360
1361 static struct signatured_type *lookup_signatured_type_at_offset
1362 (struct objfile *objfile,
1363 struct dwarf2_section_info *section, sect_offset offset);
1364
1365 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1366
1367 static void read_signatured_type (struct signatured_type *);
1368
1369 /* memory allocation interface */
1370
1371 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1372
1373 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1374
1375 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1376
1377 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1378 char *, int);
1379
1380 static int attr_form_is_block (struct attribute *);
1381
1382 static int attr_form_is_section_offset (struct attribute *);
1383
1384 static int attr_form_is_constant (struct attribute *);
1385
1386 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1387 struct dwarf2_loclist_baton *baton,
1388 struct attribute *attr);
1389
1390 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1391 struct symbol *sym,
1392 struct dwarf2_cu *cu);
1393
1394 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1395 gdb_byte *info_ptr,
1396 struct abbrev_info *abbrev);
1397
1398 static void free_stack_comp_unit (void *);
1399
1400 static hashval_t partial_die_hash (const void *item);
1401
1402 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1403
1404 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1405 (sect_offset offset, struct objfile *objfile);
1406
1407 static void init_one_comp_unit (struct dwarf2_cu *cu,
1408 struct dwarf2_per_cu_data *per_cu);
1409
1410 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1411 struct die_info *comp_unit_die,
1412 enum language pretend_language);
1413
1414 static void free_heap_comp_unit (void *);
1415
1416 static void free_cached_comp_units (void *);
1417
1418 static void age_cached_comp_units (void);
1419
1420 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1421
1422 static struct type *set_die_type (struct die_info *, struct type *,
1423 struct dwarf2_cu *);
1424
1425 static void create_all_comp_units (struct objfile *);
1426
1427 static int create_all_type_units (struct objfile *);
1428
1429 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1430 enum language);
1431
1432 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1433 enum language);
1434
1435 static void dwarf2_add_dependence (struct dwarf2_cu *,
1436 struct dwarf2_per_cu_data *);
1437
1438 static void dwarf2_mark (struct dwarf2_cu *);
1439
1440 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1441
1442 static struct type *get_die_type_at_offset (sect_offset,
1443 struct dwarf2_per_cu_data *per_cu);
1444
1445 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1446
1447 static void dwarf2_release_queue (void *dummy);
1448
1449 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1450 enum language pretend_language);
1451
1452 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1453 struct dwarf2_per_cu_data *per_cu,
1454 enum language pretend_language);
1455
1456 static void process_queue (void);
1457
1458 static void find_file_and_directory (struct die_info *die,
1459 struct dwarf2_cu *cu,
1460 char **name, char **comp_dir);
1461
1462 static char *file_full_name (int file, struct line_header *lh,
1463 const char *comp_dir);
1464
1465 static void init_cutu_and_read_dies
1466 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1467 die_reader_func_ftype *die_reader_func, void *data);
1468
1469 static void init_cutu_and_read_dies_simple
1470 (struct dwarf2_per_cu_data *this_cu,
1471 die_reader_func_ftype *die_reader_func, void *data);
1472
1473 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1474
1475 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1476
1477 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1478
1479 static struct dwo_unit *lookup_dwo_comp_unit
1480 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1481
1482 static struct dwo_unit *lookup_dwo_type_unit
1483 (struct signatured_type *, char *, const char *);
1484
1485 static void free_dwo_file_cleanup (void *);
1486
1487 static void munmap_section_buffer (struct dwarf2_section_info *);
1488
1489 static void process_cu_includes (void);
1490
1491 #if WORDS_BIGENDIAN
1492
1493 /* Convert VALUE between big- and little-endian. */
1494 static offset_type
1495 byte_swap (offset_type value)
1496 {
1497 offset_type result;
1498
1499 result = (value & 0xff) << 24;
1500 result |= (value & 0xff00) << 8;
1501 result |= (value & 0xff0000) >> 8;
1502 result |= (value & 0xff000000) >> 24;
1503 return result;
1504 }
1505
1506 #define MAYBE_SWAP(V) byte_swap (V)
1507
1508 #else
1509 #define MAYBE_SWAP(V) (V)
1510 #endif /* WORDS_BIGENDIAN */
1511
1512 /* The suffix for an index file. */
1513 #define INDEX_SUFFIX ".gdb-index"
1514
1515 static const char *dwarf2_physname (char *name, struct die_info *die,
1516 struct dwarf2_cu *cu);
1517
1518 /* Try to locate the sections we need for DWARF 2 debugging
1519 information and return true if we have enough to do something.
1520 NAMES points to the dwarf2 section names, or is NULL if the standard
1521 ELF names are used. */
1522
1523 int
1524 dwarf2_has_info (struct objfile *objfile,
1525 const struct dwarf2_debug_sections *names)
1526 {
1527 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1528 if (!dwarf2_per_objfile)
1529 {
1530 /* Initialize per-objfile state. */
1531 struct dwarf2_per_objfile *data
1532 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1533
1534 memset (data, 0, sizeof (*data));
1535 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1536 dwarf2_per_objfile = data;
1537
1538 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1539 (void *) names);
1540 dwarf2_per_objfile->objfile = objfile;
1541 }
1542 return (dwarf2_per_objfile->info.asection != NULL
1543 && dwarf2_per_objfile->abbrev.asection != NULL);
1544 }
1545
1546 /* When loading sections, we look either for uncompressed section or for
1547 compressed section names. */
1548
1549 static int
1550 section_is_p (const char *section_name,
1551 const struct dwarf2_section_names *names)
1552 {
1553 if (names->normal != NULL
1554 && strcmp (section_name, names->normal) == 0)
1555 return 1;
1556 if (names->compressed != NULL
1557 && strcmp (section_name, names->compressed) == 0)
1558 return 1;
1559 return 0;
1560 }
1561
1562 /* This function is mapped across the sections and remembers the
1563 offset and size of each of the debugging sections we are interested
1564 in. */
1565
1566 static void
1567 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1568 {
1569 const struct dwarf2_debug_sections *names;
1570
1571 if (vnames == NULL)
1572 names = &dwarf2_elf_names;
1573 else
1574 names = (const struct dwarf2_debug_sections *) vnames;
1575
1576 if (section_is_p (sectp->name, &names->info))
1577 {
1578 dwarf2_per_objfile->info.asection = sectp;
1579 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1580 }
1581 else if (section_is_p (sectp->name, &names->abbrev))
1582 {
1583 dwarf2_per_objfile->abbrev.asection = sectp;
1584 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1585 }
1586 else if (section_is_p (sectp->name, &names->line))
1587 {
1588 dwarf2_per_objfile->line.asection = sectp;
1589 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1590 }
1591 else if (section_is_p (sectp->name, &names->loc))
1592 {
1593 dwarf2_per_objfile->loc.asection = sectp;
1594 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1595 }
1596 else if (section_is_p (sectp->name, &names->macinfo))
1597 {
1598 dwarf2_per_objfile->macinfo.asection = sectp;
1599 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1600 }
1601 else if (section_is_p (sectp->name, &names->macro))
1602 {
1603 dwarf2_per_objfile->macro.asection = sectp;
1604 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1605 }
1606 else if (section_is_p (sectp->name, &names->str))
1607 {
1608 dwarf2_per_objfile->str.asection = sectp;
1609 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1610 }
1611 else if (section_is_p (sectp->name, &names->addr))
1612 {
1613 dwarf2_per_objfile->addr.asection = sectp;
1614 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1615 }
1616 else if (section_is_p (sectp->name, &names->frame))
1617 {
1618 dwarf2_per_objfile->frame.asection = sectp;
1619 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1620 }
1621 else if (section_is_p (sectp->name, &names->eh_frame))
1622 {
1623 flagword aflag = bfd_get_section_flags (abfd, sectp);
1624
1625 if (aflag & SEC_HAS_CONTENTS)
1626 {
1627 dwarf2_per_objfile->eh_frame.asection = sectp;
1628 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1629 }
1630 }
1631 else if (section_is_p (sectp->name, &names->ranges))
1632 {
1633 dwarf2_per_objfile->ranges.asection = sectp;
1634 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1635 }
1636 else if (section_is_p (sectp->name, &names->types))
1637 {
1638 struct dwarf2_section_info type_section;
1639
1640 memset (&type_section, 0, sizeof (type_section));
1641 type_section.asection = sectp;
1642 type_section.size = bfd_get_section_size (sectp);
1643
1644 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1645 &type_section);
1646 }
1647 else if (section_is_p (sectp->name, &names->gdb_index))
1648 {
1649 dwarf2_per_objfile->gdb_index.asection = sectp;
1650 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1651 }
1652
1653 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1654 && bfd_section_vma (abfd, sectp) == 0)
1655 dwarf2_per_objfile->has_section_at_zero = 1;
1656 }
1657
1658 /* Decompress a section that was compressed using zlib. Store the
1659 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1660
1661 static void
1662 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1663 gdb_byte **outbuf, bfd_size_type *outsize)
1664 {
1665 bfd *abfd = sectp->owner;
1666 #ifndef HAVE_ZLIB_H
1667 error (_("Support for zlib-compressed DWARF data (from '%s') "
1668 "is disabled in this copy of GDB"),
1669 bfd_get_filename (abfd));
1670 #else
1671 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1672 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1673 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1674 bfd_size_type uncompressed_size;
1675 gdb_byte *uncompressed_buffer;
1676 z_stream strm;
1677 int rc;
1678 int header_size = 12;
1679
1680 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1681 || bfd_bread (compressed_buffer,
1682 compressed_size, abfd) != compressed_size)
1683 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1684 bfd_get_filename (abfd));
1685
1686 /* Read the zlib header. In this case, it should be "ZLIB" followed
1687 by the uncompressed section size, 8 bytes in big-endian order. */
1688 if (compressed_size < header_size
1689 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1690 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1691 bfd_get_filename (abfd));
1692 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1693 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1694 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1695 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1696 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1697 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1698 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1699 uncompressed_size += compressed_buffer[11];
1700
1701 /* It is possible the section consists of several compressed
1702 buffers concatenated together, so we uncompress in a loop. */
1703 strm.zalloc = NULL;
1704 strm.zfree = NULL;
1705 strm.opaque = NULL;
1706 strm.avail_in = compressed_size - header_size;
1707 strm.next_in = (Bytef*) compressed_buffer + header_size;
1708 strm.avail_out = uncompressed_size;
1709 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1710 uncompressed_size);
1711 rc = inflateInit (&strm);
1712 while (strm.avail_in > 0)
1713 {
1714 if (rc != Z_OK)
1715 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1716 bfd_get_filename (abfd), rc);
1717 strm.next_out = ((Bytef*) uncompressed_buffer
1718 + (uncompressed_size - strm.avail_out));
1719 rc = inflate (&strm, Z_FINISH);
1720 if (rc != Z_STREAM_END)
1721 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1722 bfd_get_filename (abfd), rc);
1723 rc = inflateReset (&strm);
1724 }
1725 rc = inflateEnd (&strm);
1726 if (rc != Z_OK
1727 || strm.avail_out != 0)
1728 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1729 bfd_get_filename (abfd), rc);
1730
1731 do_cleanups (cleanup);
1732 *outbuf = uncompressed_buffer;
1733 *outsize = uncompressed_size;
1734 #endif
1735 }
1736
1737 /* A helper function that decides whether a section is empty,
1738 or not present. */
1739
1740 static int
1741 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1742 {
1743 return info->asection == NULL || info->size == 0;
1744 }
1745
1746 /* Read the contents of the section INFO.
1747 OBJFILE is the main object file, but not necessarily the file where
1748 the section comes from. E.g., for DWO files INFO->asection->owner
1749 is the bfd of the DWO file.
1750 If the section is compressed, uncompress it before returning. */
1751
1752 static void
1753 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1754 {
1755 asection *sectp = info->asection;
1756 bfd *abfd;
1757 gdb_byte *buf, *retbuf;
1758 unsigned char header[4];
1759
1760 if (info->readin)
1761 return;
1762 info->buffer = NULL;
1763 info->map_addr = NULL;
1764 info->readin = 1;
1765
1766 if (dwarf2_section_empty_p (info))
1767 return;
1768
1769 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1770 abfd = sectp->owner;
1771
1772 /* Check if the file has a 4-byte header indicating compression. */
1773 if (info->size > sizeof (header)
1774 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1775 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1776 {
1777 /* Upon decompression, update the buffer and its size. */
1778 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1779 {
1780 zlib_decompress_section (objfile, sectp, &info->buffer,
1781 &info->size);
1782 return;
1783 }
1784 }
1785
1786 #ifdef HAVE_MMAP
1787 if (pagesize == 0)
1788 pagesize = getpagesize ();
1789
1790 /* Only try to mmap sections which are large enough: we don't want to
1791 waste space due to fragmentation. Also, only try mmap for sections
1792 without relocations. */
1793
1794 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1795 {
1796 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1797 MAP_PRIVATE, sectp->filepos,
1798 &info->map_addr, &info->map_len);
1799
1800 if ((caddr_t)info->buffer != MAP_FAILED)
1801 {
1802 #if HAVE_POSIX_MADVISE
1803 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1804 #endif
1805 return;
1806 }
1807 }
1808 #endif
1809
1810 /* If we get here, we are a normal, not-compressed section. */
1811 info->buffer = buf
1812 = obstack_alloc (&objfile->objfile_obstack, info->size);
1813
1814 /* When debugging .o files, we may need to apply relocations; see
1815 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1816 We never compress sections in .o files, so we only need to
1817 try this when the section is not compressed. */
1818 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1819 if (retbuf != NULL)
1820 {
1821 info->buffer = retbuf;
1822 return;
1823 }
1824
1825 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1826 || bfd_bread (buf, info->size, abfd) != info->size)
1827 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1828 bfd_get_filename (abfd));
1829 }
1830
1831 /* A helper function that returns the size of a section in a safe way.
1832 If you are positive that the section has been read before using the
1833 size, then it is safe to refer to the dwarf2_section_info object's
1834 "size" field directly. In other cases, you must call this
1835 function, because for compressed sections the size field is not set
1836 correctly until the section has been read. */
1837
1838 static bfd_size_type
1839 dwarf2_section_size (struct objfile *objfile,
1840 struct dwarf2_section_info *info)
1841 {
1842 if (!info->readin)
1843 dwarf2_read_section (objfile, info);
1844 return info->size;
1845 }
1846
1847 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1848 SECTION_NAME. */
1849
1850 void
1851 dwarf2_get_section_info (struct objfile *objfile,
1852 enum dwarf2_section_enum sect,
1853 asection **sectp, gdb_byte **bufp,
1854 bfd_size_type *sizep)
1855 {
1856 struct dwarf2_per_objfile *data
1857 = objfile_data (objfile, dwarf2_objfile_data_key);
1858 struct dwarf2_section_info *info;
1859
1860 /* We may see an objfile without any DWARF, in which case we just
1861 return nothing. */
1862 if (data == NULL)
1863 {
1864 *sectp = NULL;
1865 *bufp = NULL;
1866 *sizep = 0;
1867 return;
1868 }
1869 switch (sect)
1870 {
1871 case DWARF2_DEBUG_FRAME:
1872 info = &data->frame;
1873 break;
1874 case DWARF2_EH_FRAME:
1875 info = &data->eh_frame;
1876 break;
1877 default:
1878 gdb_assert_not_reached ("unexpected section");
1879 }
1880
1881 dwarf2_read_section (objfile, info);
1882
1883 *sectp = info->asection;
1884 *bufp = info->buffer;
1885 *sizep = info->size;
1886 }
1887
1888 \f
1889 /* DWARF quick_symbols_functions support. */
1890
1891 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1892 unique line tables, so we maintain a separate table of all .debug_line
1893 derived entries to support the sharing.
1894 All the quick functions need is the list of file names. We discard the
1895 line_header when we're done and don't need to record it here. */
1896 struct quick_file_names
1897 {
1898 /* The offset in .debug_line of the line table. We hash on this. */
1899 unsigned int offset;
1900
1901 /* The number of entries in file_names, real_names. */
1902 unsigned int num_file_names;
1903
1904 /* The file names from the line table, after being run through
1905 file_full_name. */
1906 const char **file_names;
1907
1908 /* The file names from the line table after being run through
1909 gdb_realpath. These are computed lazily. */
1910 const char **real_names;
1911 };
1912
1913 /* When using the index (and thus not using psymtabs), each CU has an
1914 object of this type. This is used to hold information needed by
1915 the various "quick" methods. */
1916 struct dwarf2_per_cu_quick_data
1917 {
1918 /* The file table. This can be NULL if there was no file table
1919 or it's currently not read in.
1920 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1921 struct quick_file_names *file_names;
1922
1923 /* The corresponding symbol table. This is NULL if symbols for this
1924 CU have not yet been read. */
1925 struct symtab *symtab;
1926
1927 /* A temporary mark bit used when iterating over all CUs in
1928 expand_symtabs_matching. */
1929 unsigned int mark : 1;
1930
1931 /* True if we've tried to read the file table and found there isn't one.
1932 There will be no point in trying to read it again next time. */
1933 unsigned int no_file_data : 1;
1934 };
1935
1936 /* Hash function for a quick_file_names. */
1937
1938 static hashval_t
1939 hash_file_name_entry (const void *e)
1940 {
1941 const struct quick_file_names *file_data = e;
1942
1943 return file_data->offset;
1944 }
1945
1946 /* Equality function for a quick_file_names. */
1947
1948 static int
1949 eq_file_name_entry (const void *a, const void *b)
1950 {
1951 const struct quick_file_names *ea = a;
1952 const struct quick_file_names *eb = b;
1953
1954 return ea->offset == eb->offset;
1955 }
1956
1957 /* Delete function for a quick_file_names. */
1958
1959 static void
1960 delete_file_name_entry (void *e)
1961 {
1962 struct quick_file_names *file_data = e;
1963 int i;
1964
1965 for (i = 0; i < file_data->num_file_names; ++i)
1966 {
1967 xfree ((void*) file_data->file_names[i]);
1968 if (file_data->real_names)
1969 xfree ((void*) file_data->real_names[i]);
1970 }
1971
1972 /* The space for the struct itself lives on objfile_obstack,
1973 so we don't free it here. */
1974 }
1975
1976 /* Create a quick_file_names hash table. */
1977
1978 static htab_t
1979 create_quick_file_names_table (unsigned int nr_initial_entries)
1980 {
1981 return htab_create_alloc (nr_initial_entries,
1982 hash_file_name_entry, eq_file_name_entry,
1983 delete_file_name_entry, xcalloc, xfree);
1984 }
1985
1986 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1987 have to be created afterwards. You should call age_cached_comp_units after
1988 processing PER_CU->CU. dw2_setup must have been already called. */
1989
1990 static void
1991 load_cu (struct dwarf2_per_cu_data *per_cu)
1992 {
1993 if (per_cu->is_debug_types)
1994 load_full_type_unit (per_cu);
1995 else
1996 load_full_comp_unit (per_cu, language_minimal);
1997
1998 gdb_assert (per_cu->cu != NULL);
1999
2000 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2001 }
2002
2003 /* Read in the symbols for PER_CU. */
2004
2005 static void
2006 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2007 {
2008 struct cleanup *back_to;
2009
2010 back_to = make_cleanup (dwarf2_release_queue, NULL);
2011
2012 if (dwarf2_per_objfile->using_index
2013 ? per_cu->v.quick->symtab == NULL
2014 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2015 {
2016 queue_comp_unit (per_cu, language_minimal);
2017 load_cu (per_cu);
2018 }
2019
2020 process_queue ();
2021
2022 /* Age the cache, releasing compilation units that have not
2023 been used recently. */
2024 age_cached_comp_units ();
2025
2026 do_cleanups (back_to);
2027 }
2028
2029 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2030 the objfile from which this CU came. Returns the resulting symbol
2031 table. */
2032
2033 static struct symtab *
2034 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2035 {
2036 gdb_assert (dwarf2_per_objfile->using_index);
2037 if (!per_cu->v.quick->symtab)
2038 {
2039 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2040 increment_reading_symtab ();
2041 dw2_do_instantiate_symtab (per_cu);
2042 process_cu_includes ();
2043 do_cleanups (back_to);
2044 }
2045 return per_cu->v.quick->symtab;
2046 }
2047
2048 /* Return the CU given its index. */
2049
2050 static struct dwarf2_per_cu_data *
2051 dw2_get_cu (int index)
2052 {
2053 if (index >= dwarf2_per_objfile->n_comp_units)
2054 {
2055 index -= dwarf2_per_objfile->n_comp_units;
2056 return dwarf2_per_objfile->all_type_units[index];
2057 }
2058 return dwarf2_per_objfile->all_comp_units[index];
2059 }
2060
2061 /* A helper function that knows how to read a 64-bit value in a way
2062 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2063 otherwise. */
2064
2065 static int
2066 extract_cu_value (const char *bytes, ULONGEST *result)
2067 {
2068 if (sizeof (ULONGEST) < 8)
2069 {
2070 int i;
2071
2072 /* Ignore the upper 4 bytes if they are all zero. */
2073 for (i = 0; i < 4; ++i)
2074 if (bytes[i + 4] != 0)
2075 return 0;
2076
2077 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2078 }
2079 else
2080 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2081 return 1;
2082 }
2083
2084 /* Read the CU list from the mapped index, and use it to create all
2085 the CU objects for this objfile. Return 0 if something went wrong,
2086 1 if everything went ok. */
2087
2088 static int
2089 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2090 offset_type cu_list_elements)
2091 {
2092 offset_type i;
2093
2094 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2095 dwarf2_per_objfile->all_comp_units
2096 = obstack_alloc (&objfile->objfile_obstack,
2097 dwarf2_per_objfile->n_comp_units
2098 * sizeof (struct dwarf2_per_cu_data *));
2099
2100 for (i = 0; i < cu_list_elements; i += 2)
2101 {
2102 struct dwarf2_per_cu_data *the_cu;
2103 ULONGEST offset, length;
2104
2105 if (!extract_cu_value (cu_list, &offset)
2106 || !extract_cu_value (cu_list + 8, &length))
2107 return 0;
2108 cu_list += 2 * 8;
2109
2110 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2111 struct dwarf2_per_cu_data);
2112 the_cu->offset.sect_off = offset;
2113 the_cu->length = length;
2114 the_cu->objfile = objfile;
2115 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2116 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2117 struct dwarf2_per_cu_quick_data);
2118 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2119 }
2120
2121 return 1;
2122 }
2123
2124 /* Create the signatured type hash table from the index. */
2125
2126 static int
2127 create_signatured_type_table_from_index (struct objfile *objfile,
2128 struct dwarf2_section_info *section,
2129 const gdb_byte *bytes,
2130 offset_type elements)
2131 {
2132 offset_type i;
2133 htab_t sig_types_hash;
2134
2135 dwarf2_per_objfile->n_type_units = elements / 3;
2136 dwarf2_per_objfile->all_type_units
2137 = obstack_alloc (&objfile->objfile_obstack,
2138 dwarf2_per_objfile->n_type_units
2139 * sizeof (struct dwarf2_per_cu_data *));
2140
2141 sig_types_hash = allocate_signatured_type_table (objfile);
2142
2143 for (i = 0; i < elements; i += 3)
2144 {
2145 struct signatured_type *sig_type;
2146 ULONGEST offset, type_offset_in_tu, signature;
2147 void **slot;
2148
2149 if (!extract_cu_value (bytes, &offset)
2150 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2151 return 0;
2152 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2153 bytes += 3 * 8;
2154
2155 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2156 struct signatured_type);
2157 sig_type->signature = signature;
2158 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2159 sig_type->per_cu.is_debug_types = 1;
2160 sig_type->per_cu.info_or_types_section = section;
2161 sig_type->per_cu.offset.sect_off = offset;
2162 sig_type->per_cu.objfile = objfile;
2163 sig_type->per_cu.v.quick
2164 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2165 struct dwarf2_per_cu_quick_data);
2166
2167 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2168 *slot = sig_type;
2169
2170 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2171 }
2172
2173 dwarf2_per_objfile->signatured_types = sig_types_hash;
2174
2175 return 1;
2176 }
2177
2178 /* Read the address map data from the mapped index, and use it to
2179 populate the objfile's psymtabs_addrmap. */
2180
2181 static void
2182 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2183 {
2184 const gdb_byte *iter, *end;
2185 struct obstack temp_obstack;
2186 struct addrmap *mutable_map;
2187 struct cleanup *cleanup;
2188 CORE_ADDR baseaddr;
2189
2190 obstack_init (&temp_obstack);
2191 cleanup = make_cleanup_obstack_free (&temp_obstack);
2192 mutable_map = addrmap_create_mutable (&temp_obstack);
2193
2194 iter = index->address_table;
2195 end = iter + index->address_table_size;
2196
2197 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2198
2199 while (iter < end)
2200 {
2201 ULONGEST hi, lo, cu_index;
2202 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2203 iter += 8;
2204 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2205 iter += 8;
2206 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2207 iter += 4;
2208
2209 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2210 dw2_get_cu (cu_index));
2211 }
2212
2213 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2214 &objfile->objfile_obstack);
2215 do_cleanups (cleanup);
2216 }
2217
2218 /* The hash function for strings in the mapped index. This is the same as
2219 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2220 implementation. This is necessary because the hash function is tied to the
2221 format of the mapped index file. The hash values do not have to match with
2222 SYMBOL_HASH_NEXT.
2223
2224 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2225
2226 static hashval_t
2227 mapped_index_string_hash (int index_version, const void *p)
2228 {
2229 const unsigned char *str = (const unsigned char *) p;
2230 hashval_t r = 0;
2231 unsigned char c;
2232
2233 while ((c = *str++) != 0)
2234 {
2235 if (index_version >= 5)
2236 c = tolower (c);
2237 r = r * 67 + c - 113;
2238 }
2239
2240 return r;
2241 }
2242
2243 /* Find a slot in the mapped index INDEX for the object named NAME.
2244 If NAME is found, set *VEC_OUT to point to the CU vector in the
2245 constant pool and return 1. If NAME cannot be found, return 0. */
2246
2247 static int
2248 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2249 offset_type **vec_out)
2250 {
2251 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2252 offset_type hash;
2253 offset_type slot, step;
2254 int (*cmp) (const char *, const char *);
2255
2256 if (current_language->la_language == language_cplus
2257 || current_language->la_language == language_java
2258 || current_language->la_language == language_fortran)
2259 {
2260 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2261 not contain any. */
2262 const char *paren = strchr (name, '(');
2263
2264 if (paren)
2265 {
2266 char *dup;
2267
2268 dup = xmalloc (paren - name + 1);
2269 memcpy (dup, name, paren - name);
2270 dup[paren - name] = 0;
2271
2272 make_cleanup (xfree, dup);
2273 name = dup;
2274 }
2275 }
2276
2277 /* Index version 4 did not support case insensitive searches. But the
2278 indices for case insensitive languages are built in lowercase, therefore
2279 simulate our NAME being searched is also lowercased. */
2280 hash = mapped_index_string_hash ((index->version == 4
2281 && case_sensitivity == case_sensitive_off
2282 ? 5 : index->version),
2283 name);
2284
2285 slot = hash & (index->symbol_table_slots - 1);
2286 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2287 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2288
2289 for (;;)
2290 {
2291 /* Convert a slot number to an offset into the table. */
2292 offset_type i = 2 * slot;
2293 const char *str;
2294 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2295 {
2296 do_cleanups (back_to);
2297 return 0;
2298 }
2299
2300 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2301 if (!cmp (name, str))
2302 {
2303 *vec_out = (offset_type *) (index->constant_pool
2304 + MAYBE_SWAP (index->symbol_table[i + 1]));
2305 do_cleanups (back_to);
2306 return 1;
2307 }
2308
2309 slot = (slot + step) & (index->symbol_table_slots - 1);
2310 }
2311 }
2312
2313 /* Read the index file. If everything went ok, initialize the "quick"
2314 elements of all the CUs and return 1. Otherwise, return 0. */
2315
2316 static int
2317 dwarf2_read_index (struct objfile *objfile)
2318 {
2319 char *addr;
2320 struct mapped_index *map;
2321 offset_type *metadata;
2322 const gdb_byte *cu_list;
2323 const gdb_byte *types_list = NULL;
2324 offset_type version, cu_list_elements;
2325 offset_type types_list_elements = 0;
2326 int i;
2327
2328 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2329 return 0;
2330
2331 /* Older elfutils strip versions could keep the section in the main
2332 executable while splitting it for the separate debug info file. */
2333 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2334 & SEC_HAS_CONTENTS) == 0)
2335 return 0;
2336
2337 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2338
2339 addr = dwarf2_per_objfile->gdb_index.buffer;
2340 /* Version check. */
2341 version = MAYBE_SWAP (*(offset_type *) addr);
2342 /* Versions earlier than 3 emitted every copy of a psymbol. This
2343 causes the index to behave very poorly for certain requests. Version 3
2344 contained incomplete addrmap. So, it seems better to just ignore such
2345 indices. */
2346 if (version < 4)
2347 {
2348 static int warning_printed = 0;
2349 if (!warning_printed)
2350 {
2351 warning (_("Skipping obsolete .gdb_index section in %s."),
2352 objfile->name);
2353 warning_printed = 1;
2354 }
2355 return 0;
2356 }
2357 /* Index version 4 uses a different hash function than index version
2358 5 and later.
2359
2360 Versions earlier than 6 did not emit psymbols for inlined
2361 functions. Using these files will cause GDB not to be able to
2362 set breakpoints on inlined functions by name, so we ignore these
2363 indices unless the --use-deprecated-index-sections command line
2364 option was supplied. */
2365 if (version < 6 && !use_deprecated_index_sections)
2366 {
2367 static int warning_printed = 0;
2368 if (!warning_printed)
2369 {
2370 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2371 "--use-deprecated-index-sections to use them anyway"),
2372 objfile->name);
2373 warning_printed = 1;
2374 }
2375 return 0;
2376 }
2377 /* Indexes with higher version than the one supported by GDB may be no
2378 longer backward compatible. */
2379 if (version > 7)
2380 return 0;
2381
2382 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2383 map->version = version;
2384 map->total_size = dwarf2_per_objfile->gdb_index.size;
2385
2386 metadata = (offset_type *) (addr + sizeof (offset_type));
2387
2388 i = 0;
2389 cu_list = addr + MAYBE_SWAP (metadata[i]);
2390 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2391 / 8);
2392 ++i;
2393
2394 types_list = addr + MAYBE_SWAP (metadata[i]);
2395 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2396 - MAYBE_SWAP (metadata[i]))
2397 / 8);
2398 ++i;
2399
2400 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2401 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2402 - MAYBE_SWAP (metadata[i]));
2403 ++i;
2404
2405 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2406 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2407 - MAYBE_SWAP (metadata[i]))
2408 / (2 * sizeof (offset_type)));
2409 ++i;
2410
2411 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2412
2413 /* Don't use the index if it's empty. */
2414 if (map->symbol_table_slots == 0)
2415 return 0;
2416
2417 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2418 return 0;
2419
2420 if (types_list_elements)
2421 {
2422 struct dwarf2_section_info *section;
2423
2424 /* We can only handle a single .debug_types when we have an
2425 index. */
2426 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2427 return 0;
2428
2429 section = VEC_index (dwarf2_section_info_def,
2430 dwarf2_per_objfile->types, 0);
2431
2432 if (!create_signatured_type_table_from_index (objfile, section,
2433 types_list,
2434 types_list_elements))
2435 return 0;
2436 }
2437
2438 create_addrmap_from_index (objfile, map);
2439
2440 dwarf2_per_objfile->index_table = map;
2441 dwarf2_per_objfile->using_index = 1;
2442 dwarf2_per_objfile->quick_file_names_table =
2443 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2444
2445 return 1;
2446 }
2447
2448 /* A helper for the "quick" functions which sets the global
2449 dwarf2_per_objfile according to OBJFILE. */
2450
2451 static void
2452 dw2_setup (struct objfile *objfile)
2453 {
2454 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2455 gdb_assert (dwarf2_per_objfile);
2456 }
2457
2458 /* die_reader_func for dw2_get_file_names. */
2459
2460 static void
2461 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2462 gdb_byte *info_ptr,
2463 struct die_info *comp_unit_die,
2464 int has_children,
2465 void *data)
2466 {
2467 struct dwarf2_cu *cu = reader->cu;
2468 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2469 struct objfile *objfile = dwarf2_per_objfile->objfile;
2470 struct line_header *lh;
2471 struct attribute *attr;
2472 int i;
2473 char *name, *comp_dir;
2474 void **slot;
2475 struct quick_file_names *qfn;
2476 unsigned int line_offset;
2477
2478 /* Our callers never want to match partial units -- instead they
2479 will match the enclosing full CU. */
2480 if (comp_unit_die->tag == DW_TAG_partial_unit)
2481 {
2482 this_cu->v.quick->no_file_data = 1;
2483 return;
2484 }
2485
2486 lh = NULL;
2487 slot = NULL;
2488 line_offset = 0;
2489
2490 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2491 if (attr)
2492 {
2493 struct quick_file_names find_entry;
2494
2495 line_offset = DW_UNSND (attr);
2496
2497 /* We may have already read in this line header (TU line header sharing).
2498 If we have we're done. */
2499 find_entry.offset = line_offset;
2500 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2501 &find_entry, INSERT);
2502 if (*slot != NULL)
2503 {
2504 this_cu->v.quick->file_names = *slot;
2505 return;
2506 }
2507
2508 lh = dwarf_decode_line_header (line_offset, cu);
2509 }
2510 if (lh == NULL)
2511 {
2512 this_cu->v.quick->no_file_data = 1;
2513 return;
2514 }
2515
2516 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2517 qfn->offset = line_offset;
2518 gdb_assert (slot != NULL);
2519 *slot = qfn;
2520
2521 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2522
2523 qfn->num_file_names = lh->num_file_names;
2524 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2525 lh->num_file_names * sizeof (char *));
2526 for (i = 0; i < lh->num_file_names; ++i)
2527 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2528 qfn->real_names = NULL;
2529
2530 free_line_header (lh);
2531
2532 this_cu->v.quick->file_names = qfn;
2533 }
2534
2535 /* A helper for the "quick" functions which attempts to read the line
2536 table for THIS_CU. */
2537
2538 static struct quick_file_names *
2539 dw2_get_file_names (struct objfile *objfile,
2540 struct dwarf2_per_cu_data *this_cu)
2541 {
2542 if (this_cu->v.quick->file_names != NULL)
2543 return this_cu->v.quick->file_names;
2544 /* If we know there is no line data, no point in looking again. */
2545 if (this_cu->v.quick->no_file_data)
2546 return NULL;
2547
2548 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2549 in the stub for CUs, there's is no need to lookup the DWO file.
2550 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2551 DWO file. */
2552 if (this_cu->is_debug_types)
2553 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2554 else
2555 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2556
2557 if (this_cu->v.quick->no_file_data)
2558 return NULL;
2559 return this_cu->v.quick->file_names;
2560 }
2561
2562 /* A helper for the "quick" functions which computes and caches the
2563 real path for a given file name from the line table. */
2564
2565 static const char *
2566 dw2_get_real_path (struct objfile *objfile,
2567 struct quick_file_names *qfn, int index)
2568 {
2569 if (qfn->real_names == NULL)
2570 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2571 qfn->num_file_names, sizeof (char *));
2572
2573 if (qfn->real_names[index] == NULL)
2574 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2575
2576 return qfn->real_names[index];
2577 }
2578
2579 static struct symtab *
2580 dw2_find_last_source_symtab (struct objfile *objfile)
2581 {
2582 int index;
2583
2584 dw2_setup (objfile);
2585 index = dwarf2_per_objfile->n_comp_units - 1;
2586 return dw2_instantiate_symtab (dw2_get_cu (index));
2587 }
2588
2589 /* Traversal function for dw2_forget_cached_source_info. */
2590
2591 static int
2592 dw2_free_cached_file_names (void **slot, void *info)
2593 {
2594 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2595
2596 if (file_data->real_names)
2597 {
2598 int i;
2599
2600 for (i = 0; i < file_data->num_file_names; ++i)
2601 {
2602 xfree ((void*) file_data->real_names[i]);
2603 file_data->real_names[i] = NULL;
2604 }
2605 }
2606
2607 return 1;
2608 }
2609
2610 static void
2611 dw2_forget_cached_source_info (struct objfile *objfile)
2612 {
2613 dw2_setup (objfile);
2614
2615 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2616 dw2_free_cached_file_names, NULL);
2617 }
2618
2619 /* Helper function for dw2_map_symtabs_matching_filename that expands
2620 the symtabs and calls the iterator. */
2621
2622 static int
2623 dw2_map_expand_apply (struct objfile *objfile,
2624 struct dwarf2_per_cu_data *per_cu,
2625 const char *name,
2626 const char *full_path, const char *real_path,
2627 int (*callback) (struct symtab *, void *),
2628 void *data)
2629 {
2630 struct symtab *last_made = objfile->symtabs;
2631
2632 /* Don't visit already-expanded CUs. */
2633 if (per_cu->v.quick->symtab)
2634 return 0;
2635
2636 /* This may expand more than one symtab, and we want to iterate over
2637 all of them. */
2638 dw2_instantiate_symtab (per_cu);
2639
2640 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2641 objfile->symtabs, last_made);
2642 }
2643
2644 /* Implementation of the map_symtabs_matching_filename method. */
2645
2646 static int
2647 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2648 const char *full_path, const char *real_path,
2649 int (*callback) (struct symtab *, void *),
2650 void *data)
2651 {
2652 int i;
2653 const char *name_basename = lbasename (name);
2654 int name_len = strlen (name);
2655 int is_abs = IS_ABSOLUTE_PATH (name);
2656
2657 dw2_setup (objfile);
2658
2659 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2660 + dwarf2_per_objfile->n_type_units); ++i)
2661 {
2662 int j;
2663 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2664 struct quick_file_names *file_data;
2665
2666 /* We only need to look at symtabs not already expanded. */
2667 if (per_cu->v.quick->symtab)
2668 continue;
2669
2670 file_data = dw2_get_file_names (objfile, per_cu);
2671 if (file_data == NULL)
2672 continue;
2673
2674 for (j = 0; j < file_data->num_file_names; ++j)
2675 {
2676 const char *this_name = file_data->file_names[j];
2677
2678 if (FILENAME_CMP (name, this_name) == 0
2679 || (!is_abs && compare_filenames_for_search (this_name,
2680 name, name_len)))
2681 {
2682 if (dw2_map_expand_apply (objfile, per_cu,
2683 name, full_path, real_path,
2684 callback, data))
2685 return 1;
2686 }
2687
2688 /* Before we invoke realpath, which can get expensive when many
2689 files are involved, do a quick comparison of the basenames. */
2690 if (! basenames_may_differ
2691 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2692 continue;
2693
2694 if (full_path != NULL)
2695 {
2696 const char *this_real_name = dw2_get_real_path (objfile,
2697 file_data, j);
2698
2699 if (this_real_name != NULL
2700 && (FILENAME_CMP (full_path, this_real_name) == 0
2701 || (!is_abs
2702 && compare_filenames_for_search (this_real_name,
2703 name, name_len))))
2704 {
2705 if (dw2_map_expand_apply (objfile, per_cu,
2706 name, full_path, real_path,
2707 callback, data))
2708 return 1;
2709 }
2710 }
2711
2712 if (real_path != NULL)
2713 {
2714 const char *this_real_name = dw2_get_real_path (objfile,
2715 file_data, j);
2716
2717 if (this_real_name != NULL
2718 && (FILENAME_CMP (real_path, this_real_name) == 0
2719 || (!is_abs
2720 && compare_filenames_for_search (this_real_name,
2721 name, name_len))))
2722 {
2723 if (dw2_map_expand_apply (objfile, per_cu,
2724 name, full_path, real_path,
2725 callback, data))
2726 return 1;
2727 }
2728 }
2729 }
2730 }
2731
2732 return 0;
2733 }
2734
2735 static struct symtab *
2736 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2737 const char *name, domain_enum domain)
2738 {
2739 /* We do all the work in the pre_expand_symtabs_matching hook
2740 instead. */
2741 return NULL;
2742 }
2743
2744 /* A helper function that expands all symtabs that hold an object
2745 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2746 symbols in block BLOCK_KIND. */
2747
2748 static void
2749 dw2_do_expand_symtabs_matching (struct objfile *objfile,
2750 int want_specific_block,
2751 enum block_enum block_kind,
2752 const char *name, domain_enum domain)
2753 {
2754 struct mapped_index *index;
2755
2756 dw2_setup (objfile);
2757
2758 index = dwarf2_per_objfile->index_table;
2759
2760 /* index_table is NULL if OBJF_READNOW. */
2761 if (index)
2762 {
2763 offset_type *vec;
2764
2765 if (find_slot_in_mapped_hash (index, name, &vec))
2766 {
2767 offset_type i, len = MAYBE_SWAP (*vec);
2768 for (i = 0; i < len; ++i)
2769 {
2770 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2771 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2772 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2773 int want_static = block_kind != GLOBAL_BLOCK;
2774 /* This value is only valid for index versions >= 7. */
2775 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2776 gdb_index_symbol_kind symbol_kind =
2777 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2778
2779 if (want_specific_block
2780 && index->version >= 7
2781 && want_static != is_static)
2782 continue;
2783
2784 /* Only check the symbol's kind if it has one.
2785 Indices prior to version 7 don't record it. */
2786 if (index->version >= 7)
2787 {
2788 switch (domain)
2789 {
2790 case VAR_DOMAIN:
2791 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2792 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2793 /* Some types are also in VAR_DOMAIN. */
2794 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2795 continue;
2796 break;
2797 case STRUCT_DOMAIN:
2798 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2799 continue;
2800 break;
2801 case LABEL_DOMAIN:
2802 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2803 continue;
2804 break;
2805 default:
2806 break;
2807 }
2808 }
2809
2810 dw2_instantiate_symtab (per_cu);
2811 }
2812 }
2813 }
2814 }
2815
2816 static void
2817 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2818 enum block_enum block_kind, const char *name,
2819 domain_enum domain)
2820 {
2821 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
2822 }
2823
2824 static void
2825 dw2_print_stats (struct objfile *objfile)
2826 {
2827 int i, count;
2828
2829 dw2_setup (objfile);
2830 count = 0;
2831 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2832 + dwarf2_per_objfile->n_type_units); ++i)
2833 {
2834 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2835
2836 if (!per_cu->v.quick->symtab)
2837 ++count;
2838 }
2839 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2840 }
2841
2842 static void
2843 dw2_dump (struct objfile *objfile)
2844 {
2845 /* Nothing worth printing. */
2846 }
2847
2848 static void
2849 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2850 struct section_offsets *delta)
2851 {
2852 /* There's nothing to relocate here. */
2853 }
2854
2855 static void
2856 dw2_expand_symtabs_for_function (struct objfile *objfile,
2857 const char *func_name)
2858 {
2859 /* Note: It doesn't matter what we pass for block_kind here. */
2860 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2861 VAR_DOMAIN);
2862 }
2863
2864 static void
2865 dw2_expand_all_symtabs (struct objfile *objfile)
2866 {
2867 int i;
2868
2869 dw2_setup (objfile);
2870
2871 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2872 + dwarf2_per_objfile->n_type_units); ++i)
2873 {
2874 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2875
2876 dw2_instantiate_symtab (per_cu);
2877 }
2878 }
2879
2880 static void
2881 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2882 const char *filename)
2883 {
2884 int i;
2885
2886 dw2_setup (objfile);
2887
2888 /* We don't need to consider type units here.
2889 This is only called for examining code, e.g. expand_line_sal.
2890 There can be an order of magnitude (or more) more type units
2891 than comp units, and we avoid them if we can. */
2892
2893 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2894 {
2895 int j;
2896 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2897 struct quick_file_names *file_data;
2898
2899 /* We only need to look at symtabs not already expanded. */
2900 if (per_cu->v.quick->symtab)
2901 continue;
2902
2903 file_data = dw2_get_file_names (objfile, per_cu);
2904 if (file_data == NULL)
2905 continue;
2906
2907 for (j = 0; j < file_data->num_file_names; ++j)
2908 {
2909 const char *this_name = file_data->file_names[j];
2910 if (FILENAME_CMP (this_name, filename) == 0)
2911 {
2912 dw2_instantiate_symtab (per_cu);
2913 break;
2914 }
2915 }
2916 }
2917 }
2918
2919 /* A helper function for dw2_find_symbol_file that finds the primary
2920 file name for a given CU. This is a die_reader_func. */
2921
2922 static void
2923 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2924 gdb_byte *info_ptr,
2925 struct die_info *comp_unit_die,
2926 int has_children,
2927 void *data)
2928 {
2929 const char **result_ptr = data;
2930 struct dwarf2_cu *cu = reader->cu;
2931 struct attribute *attr;
2932
2933 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2934 if (attr == NULL)
2935 *result_ptr = NULL;
2936 else
2937 *result_ptr = DW_STRING (attr);
2938 }
2939
2940 static const char *
2941 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2942 {
2943 struct dwarf2_per_cu_data *per_cu;
2944 offset_type *vec;
2945 struct quick_file_names *file_data;
2946 const char *filename;
2947
2948 dw2_setup (objfile);
2949
2950 /* index_table is NULL if OBJF_READNOW. */
2951 if (!dwarf2_per_objfile->index_table)
2952 {
2953 struct symtab *s;
2954
2955 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2956 {
2957 struct blockvector *bv = BLOCKVECTOR (s);
2958 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2959 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2960
2961 if (sym)
2962 return sym->symtab->filename;
2963 }
2964 return NULL;
2965 }
2966
2967 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2968 name, &vec))
2969 return NULL;
2970
2971 /* Note that this just looks at the very first one named NAME -- but
2972 actually we are looking for a function. find_main_filename
2973 should be rewritten so that it doesn't require a custom hook. It
2974 could just use the ordinary symbol tables. */
2975 /* vec[0] is the length, which must always be >0. */
2976 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
2977
2978 if (per_cu->v.quick->symtab != NULL)
2979 return per_cu->v.quick->symtab->filename;
2980
2981 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
2982 &filename);
2983
2984 return filename;
2985 }
2986
2987 static void
2988 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2989 struct objfile *objfile, int global,
2990 int (*callback) (struct block *,
2991 struct symbol *, void *),
2992 void *data, symbol_compare_ftype *match,
2993 symbol_compare_ftype *ordered_compare)
2994 {
2995 /* Currently unimplemented; used for Ada. The function can be called if the
2996 current language is Ada for a non-Ada objfile using GNU index. As Ada
2997 does not look for non-Ada symbols this function should just return. */
2998 }
2999
3000 static void
3001 dw2_expand_symtabs_matching
3002 (struct objfile *objfile,
3003 int (*file_matcher) (const char *, void *),
3004 int (*name_matcher) (const char *, void *),
3005 enum search_domain kind,
3006 void *data)
3007 {
3008 int i;
3009 offset_type iter;
3010 struct mapped_index *index;
3011
3012 dw2_setup (objfile);
3013
3014 /* index_table is NULL if OBJF_READNOW. */
3015 if (!dwarf2_per_objfile->index_table)
3016 return;
3017 index = dwarf2_per_objfile->index_table;
3018
3019 if (file_matcher != NULL)
3020 {
3021 struct cleanup *cleanup;
3022 htab_t visited_found, visited_not_found;
3023
3024 visited_found = htab_create_alloc (10,
3025 htab_hash_pointer, htab_eq_pointer,
3026 NULL, xcalloc, xfree);
3027 cleanup = make_cleanup_htab_delete (visited_found);
3028 visited_not_found = htab_create_alloc (10,
3029 htab_hash_pointer, htab_eq_pointer,
3030 NULL, xcalloc, xfree);
3031 make_cleanup_htab_delete (visited_not_found);
3032
3033 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3034 + dwarf2_per_objfile->n_type_units); ++i)
3035 {
3036 int j;
3037 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3038 struct quick_file_names *file_data;
3039 void **slot;
3040
3041 per_cu->v.quick->mark = 0;
3042
3043 /* We only need to look at symtabs not already expanded. */
3044 if (per_cu->v.quick->symtab)
3045 continue;
3046
3047 file_data = dw2_get_file_names (objfile, per_cu);
3048 if (file_data == NULL)
3049 continue;
3050
3051 if (htab_find (visited_not_found, file_data) != NULL)
3052 continue;
3053 else if (htab_find (visited_found, file_data) != NULL)
3054 {
3055 per_cu->v.quick->mark = 1;
3056 continue;
3057 }
3058
3059 for (j = 0; j < file_data->num_file_names; ++j)
3060 {
3061 if (file_matcher (file_data->file_names[j], data))
3062 {
3063 per_cu->v.quick->mark = 1;
3064 break;
3065 }
3066 }
3067
3068 slot = htab_find_slot (per_cu->v.quick->mark
3069 ? visited_found
3070 : visited_not_found,
3071 file_data, INSERT);
3072 *slot = file_data;
3073 }
3074
3075 do_cleanups (cleanup);
3076 }
3077
3078 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3079 {
3080 offset_type idx = 2 * iter;
3081 const char *name;
3082 offset_type *vec, vec_len, vec_idx;
3083
3084 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3085 continue;
3086
3087 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3088
3089 if (! (*name_matcher) (name, data))
3090 continue;
3091
3092 /* The name was matched, now expand corresponding CUs that were
3093 marked. */
3094 vec = (offset_type *) (index->constant_pool
3095 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3096 vec_len = MAYBE_SWAP (vec[0]);
3097 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3098 {
3099 struct dwarf2_per_cu_data *per_cu;
3100 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3101 gdb_index_symbol_kind symbol_kind =
3102 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3103 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3104
3105 /* Don't crash on bad data. */
3106 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3107 + dwarf2_per_objfile->n_comp_units))
3108 continue;
3109
3110 /* Only check the symbol's kind if it has one.
3111 Indices prior to version 7 don't record it. */
3112 if (index->version >= 7)
3113 {
3114 switch (kind)
3115 {
3116 case VARIABLES_DOMAIN:
3117 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3118 continue;
3119 break;
3120 case FUNCTIONS_DOMAIN:
3121 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3122 continue;
3123 break;
3124 case TYPES_DOMAIN:
3125 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3126 continue;
3127 break;
3128 default:
3129 break;
3130 }
3131 }
3132
3133 per_cu = dw2_get_cu (cu_index);
3134 if (file_matcher == NULL || per_cu->v.quick->mark)
3135 dw2_instantiate_symtab (per_cu);
3136 }
3137 }
3138 }
3139
3140 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3141 symtab. */
3142
3143 static struct symtab *
3144 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3145 {
3146 int i;
3147
3148 if (BLOCKVECTOR (symtab) != NULL
3149 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3150 return symtab;
3151
3152 if (symtab->includes == NULL)
3153 return NULL;
3154
3155 for (i = 0; symtab->includes[i]; ++i)
3156 {
3157 struct symtab *s = symtab->includes[i];
3158
3159 s = recursively_find_pc_sect_symtab (s, pc);
3160 if (s != NULL)
3161 return s;
3162 }
3163
3164 return NULL;
3165 }
3166
3167 static struct symtab *
3168 dw2_find_pc_sect_symtab (struct objfile *objfile,
3169 struct minimal_symbol *msymbol,
3170 CORE_ADDR pc,
3171 struct obj_section *section,
3172 int warn_if_readin)
3173 {
3174 struct dwarf2_per_cu_data *data;
3175 struct symtab *result;
3176
3177 dw2_setup (objfile);
3178
3179 if (!objfile->psymtabs_addrmap)
3180 return NULL;
3181
3182 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3183 if (!data)
3184 return NULL;
3185
3186 if (warn_if_readin && data->v.quick->symtab)
3187 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3188 paddress (get_objfile_arch (objfile), pc));
3189
3190 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3191 gdb_assert (result != NULL);
3192 return result;
3193 }
3194
3195 static void
3196 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3197 void *data, int need_fullname)
3198 {
3199 int i;
3200 struct cleanup *cleanup;
3201 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3202 NULL, xcalloc, xfree);
3203
3204 cleanup = make_cleanup_htab_delete (visited);
3205 dw2_setup (objfile);
3206
3207 /* We can ignore file names coming from already-expanded CUs. */
3208 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3209 + dwarf2_per_objfile->n_type_units); ++i)
3210 {
3211 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3212
3213 if (per_cu->v.quick->symtab)
3214 {
3215 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3216 INSERT);
3217
3218 *slot = per_cu->v.quick->file_names;
3219 }
3220 }
3221
3222 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3223 + dwarf2_per_objfile->n_type_units); ++i)
3224 {
3225 int j;
3226 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3227 struct quick_file_names *file_data;
3228 void **slot;
3229
3230 /* We only need to look at symtabs not already expanded. */
3231 if (per_cu->v.quick->symtab)
3232 continue;
3233
3234 file_data = dw2_get_file_names (objfile, per_cu);
3235 if (file_data == NULL)
3236 continue;
3237
3238 slot = htab_find_slot (visited, file_data, INSERT);
3239 if (*slot)
3240 {
3241 /* Already visited. */
3242 continue;
3243 }
3244 *slot = file_data;
3245
3246 for (j = 0; j < file_data->num_file_names; ++j)
3247 {
3248 const char *this_real_name;
3249
3250 if (need_fullname)
3251 this_real_name = dw2_get_real_path (objfile, file_data, j);
3252 else
3253 this_real_name = NULL;
3254 (*fun) (file_data->file_names[j], this_real_name, data);
3255 }
3256 }
3257
3258 do_cleanups (cleanup);
3259 }
3260
3261 static int
3262 dw2_has_symbols (struct objfile *objfile)
3263 {
3264 return 1;
3265 }
3266
3267 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3268 {
3269 dw2_has_symbols,
3270 dw2_find_last_source_symtab,
3271 dw2_forget_cached_source_info,
3272 dw2_map_symtabs_matching_filename,
3273 dw2_lookup_symbol,
3274 dw2_pre_expand_symtabs_matching,
3275 dw2_print_stats,
3276 dw2_dump,
3277 dw2_relocate,
3278 dw2_expand_symtabs_for_function,
3279 dw2_expand_all_symtabs,
3280 dw2_expand_symtabs_with_filename,
3281 dw2_find_symbol_file,
3282 dw2_map_matching_symbols,
3283 dw2_expand_symtabs_matching,
3284 dw2_find_pc_sect_symtab,
3285 dw2_map_symbol_filenames
3286 };
3287
3288 /* Initialize for reading DWARF for this objfile. Return 0 if this
3289 file will use psymtabs, or 1 if using the GNU index. */
3290
3291 int
3292 dwarf2_initialize_objfile (struct objfile *objfile)
3293 {
3294 /* If we're about to read full symbols, don't bother with the
3295 indices. In this case we also don't care if some other debug
3296 format is making psymtabs, because they are all about to be
3297 expanded anyway. */
3298 if ((objfile->flags & OBJF_READNOW))
3299 {
3300 int i;
3301
3302 dwarf2_per_objfile->using_index = 1;
3303 create_all_comp_units (objfile);
3304 create_all_type_units (objfile);
3305 dwarf2_per_objfile->quick_file_names_table =
3306 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3307
3308 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3309 + dwarf2_per_objfile->n_type_units); ++i)
3310 {
3311 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3312
3313 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3314 struct dwarf2_per_cu_quick_data);
3315 }
3316
3317 /* Return 1 so that gdb sees the "quick" functions. However,
3318 these functions will be no-ops because we will have expanded
3319 all symtabs. */
3320 return 1;
3321 }
3322
3323 if (dwarf2_read_index (objfile))
3324 return 1;
3325
3326 return 0;
3327 }
3328
3329 \f
3330
3331 /* Build a partial symbol table. */
3332
3333 void
3334 dwarf2_build_psymtabs (struct objfile *objfile)
3335 {
3336 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3337 {
3338 init_psymbol_list (objfile, 1024);
3339 }
3340
3341 dwarf2_build_psymtabs_hard (objfile);
3342 }
3343
3344 /* Return TRUE if OFFSET is within CU_HEADER. */
3345
3346 static inline int
3347 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3348 {
3349 sect_offset bottom = { cu_header->offset.sect_off };
3350 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3351 + cu_header->initial_length_size) };
3352
3353 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3354 }
3355
3356 /* Read in the comp unit header information from the debug_info at info_ptr.
3357 NOTE: This leaves members offset, first_die_offset to be filled in
3358 by the caller. */
3359
3360 static gdb_byte *
3361 read_comp_unit_head (struct comp_unit_head *cu_header,
3362 gdb_byte *info_ptr, bfd *abfd)
3363 {
3364 int signed_addr;
3365 unsigned int bytes_read;
3366
3367 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3368 cu_header->initial_length_size = bytes_read;
3369 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3370 info_ptr += bytes_read;
3371 cu_header->version = read_2_bytes (abfd, info_ptr);
3372 info_ptr += 2;
3373 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3374 &bytes_read);
3375 info_ptr += bytes_read;
3376 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3377 info_ptr += 1;
3378 signed_addr = bfd_get_sign_extend_vma (abfd);
3379 if (signed_addr < 0)
3380 internal_error (__FILE__, __LINE__,
3381 _("read_comp_unit_head: dwarf from non elf file"));
3382 cu_header->signed_addr_p = signed_addr;
3383
3384 return info_ptr;
3385 }
3386
3387 /* Subroutine of read_and_check_comp_unit_head and
3388 read_and_check_type_unit_head to simplify them.
3389 Perform various error checking on the header. */
3390
3391 static void
3392 error_check_comp_unit_head (struct comp_unit_head *header,
3393 struct dwarf2_section_info *section,
3394 struct dwarf2_section_info *abbrev_section)
3395 {
3396 bfd *abfd = section->asection->owner;
3397 const char *filename = bfd_get_filename (abfd);
3398
3399 if (header->version != 2 && header->version != 3 && header->version != 4)
3400 error (_("Dwarf Error: wrong version in compilation unit header "
3401 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3402 filename);
3403
3404 if (header->abbrev_offset.sect_off
3405 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3406 &dwarf2_per_objfile->abbrev))
3407 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3408 "(offset 0x%lx + 6) [in module %s]"),
3409 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3410 filename);
3411
3412 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3413 avoid potential 32-bit overflow. */
3414 if (((unsigned long) header->offset.sect_off
3415 + header->length + header->initial_length_size)
3416 > section->size)
3417 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3418 "(offset 0x%lx + 0) [in module %s]"),
3419 (long) header->length, (long) header->offset.sect_off,
3420 filename);
3421 }
3422
3423 /* Read in a CU/TU header and perform some basic error checking.
3424 The contents of the header are stored in HEADER.
3425 The result is a pointer to the start of the first DIE. */
3426
3427 static gdb_byte *
3428 read_and_check_comp_unit_head (struct comp_unit_head *header,
3429 struct dwarf2_section_info *section,
3430 struct dwarf2_section_info *abbrev_section,
3431 gdb_byte *info_ptr,
3432 int is_debug_types_section)
3433 {
3434 gdb_byte *beg_of_comp_unit = info_ptr;
3435 bfd *abfd = section->asection->owner;
3436
3437 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3438
3439 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3440
3441 /* If we're reading a type unit, skip over the signature and
3442 type_offset fields. */
3443 if (is_debug_types_section)
3444 info_ptr += 8 /*signature*/ + header->offset_size;
3445
3446 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3447
3448 error_check_comp_unit_head (header, section, abbrev_section);
3449
3450 return info_ptr;
3451 }
3452
3453 /* Read in the types comp unit header information from .debug_types entry at
3454 types_ptr. The result is a pointer to one past the end of the header. */
3455
3456 static gdb_byte *
3457 read_and_check_type_unit_head (struct comp_unit_head *header,
3458 struct dwarf2_section_info *section,
3459 struct dwarf2_section_info *abbrev_section,
3460 gdb_byte *info_ptr,
3461 ULONGEST *signature,
3462 cu_offset *type_offset_in_tu)
3463 {
3464 gdb_byte *beg_of_comp_unit = info_ptr;
3465 bfd *abfd = section->asection->owner;
3466
3467 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3468
3469 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3470
3471 /* If we're reading a type unit, skip over the signature and
3472 type_offset fields. */
3473 if (signature != NULL)
3474 *signature = read_8_bytes (abfd, info_ptr);
3475 info_ptr += 8;
3476 if (type_offset_in_tu != NULL)
3477 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3478 header->offset_size);
3479 info_ptr += header->offset_size;
3480
3481 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3482
3483 error_check_comp_unit_head (header, section, abbrev_section);
3484
3485 return info_ptr;
3486 }
3487
3488 /* Allocate a new partial symtab for file named NAME and mark this new
3489 partial symtab as being an include of PST. */
3490
3491 static void
3492 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3493 struct objfile *objfile)
3494 {
3495 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3496
3497 subpst->section_offsets = pst->section_offsets;
3498 subpst->textlow = 0;
3499 subpst->texthigh = 0;
3500
3501 subpst->dependencies = (struct partial_symtab **)
3502 obstack_alloc (&objfile->objfile_obstack,
3503 sizeof (struct partial_symtab *));
3504 subpst->dependencies[0] = pst;
3505 subpst->number_of_dependencies = 1;
3506
3507 subpst->globals_offset = 0;
3508 subpst->n_global_syms = 0;
3509 subpst->statics_offset = 0;
3510 subpst->n_static_syms = 0;
3511 subpst->symtab = NULL;
3512 subpst->read_symtab = pst->read_symtab;
3513 subpst->readin = 0;
3514
3515 /* No private part is necessary for include psymtabs. This property
3516 can be used to differentiate between such include psymtabs and
3517 the regular ones. */
3518 subpst->read_symtab_private = NULL;
3519 }
3520
3521 /* Read the Line Number Program data and extract the list of files
3522 included by the source file represented by PST. Build an include
3523 partial symtab for each of these included files. */
3524
3525 static void
3526 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3527 struct die_info *die,
3528 struct partial_symtab *pst)
3529 {
3530 struct line_header *lh = NULL;
3531 struct attribute *attr;
3532
3533 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3534 if (attr)
3535 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3536 if (lh == NULL)
3537 return; /* No linetable, so no includes. */
3538
3539 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3540 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3541
3542 free_line_header (lh);
3543 }
3544
3545 static hashval_t
3546 hash_signatured_type (const void *item)
3547 {
3548 const struct signatured_type *sig_type = item;
3549
3550 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3551 return sig_type->signature;
3552 }
3553
3554 static int
3555 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3556 {
3557 const struct signatured_type *lhs = item_lhs;
3558 const struct signatured_type *rhs = item_rhs;
3559
3560 return lhs->signature == rhs->signature;
3561 }
3562
3563 /* Allocate a hash table for signatured types. */
3564
3565 static htab_t
3566 allocate_signatured_type_table (struct objfile *objfile)
3567 {
3568 return htab_create_alloc_ex (41,
3569 hash_signatured_type,
3570 eq_signatured_type,
3571 NULL,
3572 &objfile->objfile_obstack,
3573 hashtab_obstack_allocate,
3574 dummy_obstack_deallocate);
3575 }
3576
3577 /* A helper function to add a signatured type CU to a table. */
3578
3579 static int
3580 add_signatured_type_cu_to_table (void **slot, void *datum)
3581 {
3582 struct signatured_type *sigt = *slot;
3583 struct dwarf2_per_cu_data ***datap = datum;
3584
3585 **datap = &sigt->per_cu;
3586 ++*datap;
3587
3588 return 1;
3589 }
3590
3591 /* Create the hash table of all entries in the .debug_types section.
3592 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3593 The result is a pointer to the hash table or NULL if there are
3594 no types. */
3595
3596 static htab_t
3597 create_debug_types_hash_table (struct dwo_file *dwo_file,
3598 VEC (dwarf2_section_info_def) *types)
3599 {
3600 struct objfile *objfile = dwarf2_per_objfile->objfile;
3601 htab_t types_htab = NULL;
3602 int ix;
3603 struct dwarf2_section_info *section;
3604 struct dwarf2_section_info *abbrev_section;
3605
3606 if (VEC_empty (dwarf2_section_info_def, types))
3607 return NULL;
3608
3609 abbrev_section = (dwo_file != NULL
3610 ? &dwo_file->sections.abbrev
3611 : &dwarf2_per_objfile->abbrev);
3612
3613 for (ix = 0;
3614 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3615 ++ix)
3616 {
3617 bfd *abfd;
3618 gdb_byte *info_ptr, *end_ptr;
3619
3620 dwarf2_read_section (objfile, section);
3621 info_ptr = section->buffer;
3622
3623 if (info_ptr == NULL)
3624 continue;
3625
3626 /* We can't set abfd until now because the section may be empty or
3627 not present, in which case section->asection will be NULL. */
3628 abfd = section->asection->owner;
3629
3630 if (types_htab == NULL)
3631 {
3632 if (dwo_file)
3633 types_htab = allocate_dwo_unit_table (objfile);
3634 else
3635 types_htab = allocate_signatured_type_table (objfile);
3636 }
3637
3638 if (dwarf2_die_debug)
3639 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3640 bfd_get_filename (abfd));
3641
3642 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3643 because we don't need to read any dies: the signature is in the
3644 header. */
3645
3646 end_ptr = info_ptr + section->size;
3647 while (info_ptr < end_ptr)
3648 {
3649 sect_offset offset;
3650 cu_offset type_offset_in_tu;
3651 ULONGEST signature;
3652 struct signatured_type *sig_type;
3653 struct dwo_unit *dwo_tu;
3654 void **slot;
3655 gdb_byte *ptr = info_ptr;
3656 struct comp_unit_head header;
3657 unsigned int length;
3658
3659 offset.sect_off = ptr - section->buffer;
3660
3661 /* We need to read the type's signature in order to build the hash
3662 table, but we don't need anything else just yet. */
3663
3664 ptr = read_and_check_type_unit_head (&header, section,
3665 abbrev_section, ptr,
3666 &signature, &type_offset_in_tu);
3667
3668 length = header.initial_length_size + header.length;
3669
3670 /* Skip dummy type units. */
3671 if (ptr >= info_ptr + length
3672 || peek_abbrev_code (abfd, ptr) == 0)
3673 {
3674 info_ptr += header.initial_length_size + header.length;
3675 continue;
3676 }
3677
3678 if (dwo_file)
3679 {
3680 sig_type = NULL;
3681 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3682 struct dwo_unit);
3683 dwo_tu->dwo_file = dwo_file;
3684 dwo_tu->signature = signature;
3685 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3686 dwo_tu->info_or_types_section = section;
3687 dwo_tu->offset = offset;
3688 dwo_tu->length = length;
3689 }
3690 else
3691 {
3692 /* N.B.: type_offset is not usable if this type uses a DWO file.
3693 The real type_offset is in the DWO file. */
3694 dwo_tu = NULL;
3695 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3696 struct signatured_type);
3697 sig_type->signature = signature;
3698 sig_type->type_offset_in_tu = type_offset_in_tu;
3699 sig_type->per_cu.objfile = objfile;
3700 sig_type->per_cu.is_debug_types = 1;
3701 sig_type->per_cu.info_or_types_section = section;
3702 sig_type->per_cu.offset = offset;
3703 sig_type->per_cu.length = length;
3704 }
3705
3706 slot = htab_find_slot (types_htab,
3707 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3708 INSERT);
3709 gdb_assert (slot != NULL);
3710 if (*slot != NULL)
3711 {
3712 sect_offset dup_offset;
3713
3714 if (dwo_file)
3715 {
3716 const struct dwo_unit *dup_tu = *slot;
3717
3718 dup_offset = dup_tu->offset;
3719 }
3720 else
3721 {
3722 const struct signatured_type *dup_tu = *slot;
3723
3724 dup_offset = dup_tu->per_cu.offset;
3725 }
3726
3727 complaint (&symfile_complaints,
3728 _("debug type entry at offset 0x%x is duplicate to the "
3729 "entry at offset 0x%x, signature 0x%s"),
3730 offset.sect_off, dup_offset.sect_off,
3731 phex (signature, sizeof (signature)));
3732 }
3733 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3734
3735 if (dwarf2_die_debug)
3736 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3737 offset.sect_off,
3738 phex (signature, sizeof (signature)));
3739
3740 info_ptr += length;
3741 }
3742 }
3743
3744 return types_htab;
3745 }
3746
3747 /* Create the hash table of all entries in the .debug_types section,
3748 and initialize all_type_units.
3749 The result is zero if there is an error (e.g. missing .debug_types section),
3750 otherwise non-zero. */
3751
3752 static int
3753 create_all_type_units (struct objfile *objfile)
3754 {
3755 htab_t types_htab;
3756 struct dwarf2_per_cu_data **iter;
3757
3758 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3759 if (types_htab == NULL)
3760 {
3761 dwarf2_per_objfile->signatured_types = NULL;
3762 return 0;
3763 }
3764
3765 dwarf2_per_objfile->signatured_types = types_htab;
3766
3767 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3768 dwarf2_per_objfile->all_type_units
3769 = obstack_alloc (&objfile->objfile_obstack,
3770 dwarf2_per_objfile->n_type_units
3771 * sizeof (struct dwarf2_per_cu_data *));
3772 iter = &dwarf2_per_objfile->all_type_units[0];
3773 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3774 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3775 == dwarf2_per_objfile->n_type_units);
3776
3777 return 1;
3778 }
3779
3780 /* Lookup a signature based type for DW_FORM_ref_sig8.
3781 Returns NULL if signature SIG is not present in the table. */
3782
3783 static struct signatured_type *
3784 lookup_signatured_type (ULONGEST sig)
3785 {
3786 struct signatured_type find_entry, *entry;
3787
3788 if (dwarf2_per_objfile->signatured_types == NULL)
3789 {
3790 complaint (&symfile_complaints,
3791 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3792 return NULL;
3793 }
3794
3795 find_entry.signature = sig;
3796 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3797 return entry;
3798 }
3799
3800 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3801
3802 static void
3803 init_cu_die_reader (struct die_reader_specs *reader,
3804 struct dwarf2_cu *cu,
3805 struct dwarf2_section_info *section,
3806 struct dwo_file *dwo_file)
3807 {
3808 gdb_assert (section->readin && section->buffer != NULL);
3809 reader->abfd = section->asection->owner;
3810 reader->cu = cu;
3811 reader->dwo_file = dwo_file;
3812 reader->die_section = section;
3813 reader->buffer = section->buffer;
3814 reader->buffer_end = section->buffer + section->size;
3815 }
3816
3817 /* Find the base address of the compilation unit for range lists and
3818 location lists. It will normally be specified by DW_AT_low_pc.
3819 In DWARF-3 draft 4, the base address could be overridden by
3820 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3821 compilation units with discontinuous ranges. */
3822
3823 static void
3824 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3825 {
3826 struct attribute *attr;
3827
3828 cu->base_known = 0;
3829 cu->base_address = 0;
3830
3831 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3832 if (attr)
3833 {
3834 cu->base_address = DW_ADDR (attr);
3835 cu->base_known = 1;
3836 }
3837 else
3838 {
3839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3840 if (attr)
3841 {
3842 cu->base_address = DW_ADDR (attr);
3843 cu->base_known = 1;
3844 }
3845 }
3846 }
3847
3848 /* Initialize a CU (or TU) and read its DIEs.
3849 If the CU defers to a DWO file, read the DWO file as well.
3850
3851 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3852 Otherwise, a new CU is allocated with xmalloc.
3853
3854 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3855 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3856
3857 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3858 linker) then DIE_READER_FUNC will not get called. */
3859
3860 static void
3861 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3862 int use_existing_cu, int keep,
3863 die_reader_func_ftype *die_reader_func,
3864 void *data)
3865 {
3866 struct objfile *objfile = dwarf2_per_objfile->objfile;
3867 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3868 bfd *abfd = section->asection->owner;
3869 struct dwarf2_cu *cu;
3870 gdb_byte *begin_info_ptr, *info_ptr;
3871 struct die_reader_specs reader;
3872 struct die_info *comp_unit_die;
3873 int has_children;
3874 struct attribute *attr;
3875 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3876 struct signatured_type *sig_type = NULL;
3877 struct dwarf2_section_info *abbrev_section;
3878
3879 if (use_existing_cu)
3880 gdb_assert (keep);
3881
3882 cleanups = make_cleanup (null_cleanup, NULL);
3883
3884 /* This is cheap if the section is already read in. */
3885 dwarf2_read_section (objfile, section);
3886
3887 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3888 abbrev_section = &dwarf2_per_objfile->abbrev;
3889
3890 if (use_existing_cu && this_cu->cu != NULL)
3891 {
3892 cu = this_cu->cu;
3893 info_ptr += cu->header.first_die_offset.cu_off;
3894 }
3895 else
3896 {
3897 /* If !use_existing_cu, this_cu->cu must be NULL. */
3898 gdb_assert (this_cu->cu == NULL);
3899
3900 cu = xmalloc (sizeof (*cu));
3901 init_one_comp_unit (cu, this_cu);
3902
3903 /* If an error occurs while loading, release our storage. */
3904 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3905
3906 if (this_cu->is_debug_types)
3907 {
3908 ULONGEST signature;
3909
3910 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3911 abbrev_section, info_ptr,
3912 &signature, NULL);
3913
3914 /* There's no way to get from PER_CU to its containing
3915 struct signatured_type.
3916 But we have the signature so we can use that. */
3917 sig_type = lookup_signatured_type (signature);
3918 /* We've already scanned all the signatured types,
3919 this must succeed. */
3920 gdb_assert (sig_type != NULL);
3921 gdb_assert (&sig_type->per_cu == this_cu);
3922 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3923
3924 /* LENGTH has not been set yet for type units. */
3925 this_cu->length = cu->header.length + cu->header.initial_length_size;
3926
3927 /* Establish the type offset that can be used to lookup the type. */
3928 sig_type->type_offset_in_section.sect_off =
3929 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3930 }
3931 else
3932 {
3933 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3934 abbrev_section,
3935 info_ptr, 0);
3936
3937 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3938 gdb_assert (this_cu->length
3939 == cu->header.length + cu->header.initial_length_size);
3940 }
3941 }
3942
3943 /* Skip dummy compilation units. */
3944 if (info_ptr >= begin_info_ptr + this_cu->length
3945 || peek_abbrev_code (abfd, info_ptr) == 0)
3946 {
3947 do_cleanups (cleanups);
3948 return;
3949 }
3950
3951 /* Read the abbrevs for this compilation unit into a table. */
3952 if (cu->dwarf2_abbrevs == NULL)
3953 {
3954 dwarf2_read_abbrevs (cu, abbrev_section);
3955 make_cleanup (dwarf2_free_abbrev_table, cu);
3956 }
3957
3958 /* Read the top level CU/TU die. */
3959 init_cu_die_reader (&reader, cu, section, NULL);
3960 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3961
3962 /* If we have a DWO stub, process it and then read in the DWO file.
3963 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3964 a DWO CU, that this test will fail. */
3965 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3966 if (attr)
3967 {
3968 char *dwo_name = DW_STRING (attr);
3969 const char *comp_dir;
3970 struct dwo_unit *dwo_unit;
3971 ULONGEST signature; /* Or dwo_id. */
3972 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3973 int i,num_extra_attrs;
3974 struct dwarf2_section_info *dwo_abbrev_section;
3975
3976 if (has_children)
3977 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3978 " has children (offset 0x%x) [in module %s]"),
3979 this_cu->offset.sect_off, bfd_get_filename (abfd));
3980
3981 /* These attributes aren't processed until later:
3982 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3983 However, the attribute is found in the stub which we won't have later.
3984 In order to not impose this complication on the rest of the code,
3985 we read them here and copy them to the DWO CU/TU die. */
3986 stmt_list = low_pc = high_pc = ranges = NULL;
3987
3988 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3989 DWO file. */
3990 if (! this_cu->is_debug_types)
3991 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3992 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3993 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3994 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3995
3996 /* There should be a DW_AT_addr_base attribute here (if needed).
3997 We need the value before we can process DW_FORM_GNU_addr_index. */
3998 cu->addr_base = 0;
3999 cu->have_addr_base = 0;
4000 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4001 if (attr)
4002 {
4003 cu->addr_base = DW_UNSND (attr);
4004 cu->have_addr_base = 1;
4005 }
4006
4007 if (this_cu->is_debug_types)
4008 {
4009 gdb_assert (sig_type != NULL);
4010 signature = sig_type->signature;
4011 }
4012 else
4013 {
4014 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4015 if (! attr)
4016 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4017 dwo_name);
4018 signature = DW_UNSND (attr);
4019 }
4020
4021 /* We may need the comp_dir in order to find the DWO file. */
4022 comp_dir = NULL;
4023 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4024 if (attr)
4025 comp_dir = DW_STRING (attr);
4026
4027 if (this_cu->is_debug_types)
4028 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4029 else
4030 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4031 signature);
4032
4033 if (dwo_unit == NULL)
4034 {
4035 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4036 " with ID %s [in module %s]"),
4037 this_cu->offset.sect_off,
4038 phex (signature, sizeof (signature)),
4039 objfile->name);
4040 }
4041
4042 /* Set up for reading the DWO CU/TU. */
4043 cu->dwo_unit = dwo_unit;
4044 section = dwo_unit->info_or_types_section;
4045 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4046 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4047 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4048
4049 if (this_cu->is_debug_types)
4050 {
4051 ULONGEST signature;
4052
4053 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4054 dwo_abbrev_section,
4055 info_ptr,
4056 &signature, NULL);
4057 gdb_assert (sig_type->signature == signature);
4058 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4059 gdb_assert (dwo_unit->length
4060 == cu->header.length + cu->header.initial_length_size);
4061
4062 /* Establish the type offset that can be used to lookup the type.
4063 For DWO files, we don't know it until now. */
4064 sig_type->type_offset_in_section.sect_off =
4065 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4066 }
4067 else
4068 {
4069 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4070 dwo_abbrev_section,
4071 info_ptr, 0);
4072 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4073 gdb_assert (dwo_unit->length
4074 == cu->header.length + cu->header.initial_length_size);
4075 }
4076
4077 /* Discard the original CU's abbrev table, and read the DWO's. */
4078 dwarf2_free_abbrev_table (cu);
4079 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4080
4081 /* Read in the die, but leave space to copy over the attributes
4082 from the stub. This has the benefit of simplifying the rest of
4083 the code - all the real work is done here. */
4084 num_extra_attrs = ((stmt_list != NULL)
4085 + (low_pc != NULL)
4086 + (high_pc != NULL)
4087 + (ranges != NULL));
4088 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4089 &has_children, num_extra_attrs);
4090
4091 /* Copy over the attributes from the stub to the DWO die. */
4092 i = comp_unit_die->num_attrs;
4093 if (stmt_list != NULL)
4094 comp_unit_die->attrs[i++] = *stmt_list;
4095 if (low_pc != NULL)
4096 comp_unit_die->attrs[i++] = *low_pc;
4097 if (high_pc != NULL)
4098 comp_unit_die->attrs[i++] = *high_pc;
4099 if (ranges != NULL)
4100 comp_unit_die->attrs[i++] = *ranges;
4101 comp_unit_die->num_attrs += num_extra_attrs;
4102
4103 /* Skip dummy compilation units. */
4104 if (info_ptr >= begin_info_ptr + dwo_unit->length
4105 || peek_abbrev_code (abfd, info_ptr) == 0)
4106 {
4107 do_cleanups (cleanups);
4108 return;
4109 }
4110 }
4111
4112 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4113
4114 if (free_cu_cleanup != NULL)
4115 {
4116 if (keep)
4117 {
4118 /* We've successfully allocated this compilation unit. Let our
4119 caller clean it up when finished with it. */
4120 discard_cleanups (free_cu_cleanup);
4121
4122 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4123 So we have to manually free the abbrev table. */
4124 dwarf2_free_abbrev_table (cu);
4125
4126 /* Link this CU into read_in_chain. */
4127 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4128 dwarf2_per_objfile->read_in_chain = this_cu;
4129 }
4130 else
4131 do_cleanups (free_cu_cleanup);
4132 }
4133
4134 do_cleanups (cleanups);
4135 }
4136
4137 /* Read CU/TU THIS_CU in section SECTION,
4138 but do not follow DW_AT_GNU_dwo_name if present.
4139 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4140 have already done the lookup to find the DWO file).
4141
4142 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4143 THIS_CU->is_debug_types, but nothing else.
4144
4145 We fill in THIS_CU->length.
4146
4147 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4148 linker) then DIE_READER_FUNC will not get called.
4149
4150 THIS_CU->cu is always freed when done.
4151 This is done in order to not leave THIS_CU->cu in a state where we have
4152 to care whether it refers to the "main" CU or the DWO CU. */
4153
4154 static void
4155 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4156 struct dwarf2_section_info *abbrev_section,
4157 struct dwo_file *dwo_file,
4158 die_reader_func_ftype *die_reader_func,
4159 void *data)
4160 {
4161 struct objfile *objfile = dwarf2_per_objfile->objfile;
4162 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4163 bfd *abfd = section->asection->owner;
4164 struct dwarf2_cu cu;
4165 gdb_byte *begin_info_ptr, *info_ptr;
4166 struct die_reader_specs reader;
4167 struct cleanup *cleanups;
4168 struct die_info *comp_unit_die;
4169 int has_children;
4170
4171 gdb_assert (this_cu->cu == NULL);
4172
4173 /* This is cheap if the section is already read in. */
4174 dwarf2_read_section (objfile, section);
4175
4176 init_one_comp_unit (&cu, this_cu);
4177
4178 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4179
4180 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4181 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4182 abbrev_section, info_ptr,
4183 this_cu->is_debug_types);
4184
4185 this_cu->length = cu.header.length + cu.header.initial_length_size;
4186
4187 /* Skip dummy compilation units. */
4188 if (info_ptr >= begin_info_ptr + this_cu->length
4189 || peek_abbrev_code (abfd, info_ptr) == 0)
4190 {
4191 do_cleanups (cleanups);
4192 return;
4193 }
4194
4195 dwarf2_read_abbrevs (&cu, abbrev_section);
4196 make_cleanup (dwarf2_free_abbrev_table, &cu);
4197
4198 init_cu_die_reader (&reader, &cu, section, dwo_file);
4199 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4200
4201 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4202
4203 do_cleanups (cleanups);
4204 }
4205
4206 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4207 does not lookup the specified DWO file.
4208 This cannot be used to read DWO files.
4209
4210 THIS_CU->cu is always freed when done.
4211 This is done in order to not leave THIS_CU->cu in a state where we have
4212 to care whether it refers to the "main" CU or the DWO CU.
4213 We can revisit this if the data shows there's a performance issue. */
4214
4215 static void
4216 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4217 die_reader_func_ftype *die_reader_func,
4218 void *data)
4219 {
4220 init_cutu_and_read_dies_no_follow (this_cu,
4221 &dwarf2_per_objfile->abbrev,
4222 NULL,
4223 die_reader_func, data);
4224 }
4225
4226 /* die_reader_func for process_psymtab_comp_unit. */
4227
4228 static void
4229 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4230 gdb_byte *info_ptr,
4231 struct die_info *comp_unit_die,
4232 int has_children,
4233 void *data)
4234 {
4235 struct dwarf2_cu *cu = reader->cu;
4236 struct objfile *objfile = cu->objfile;
4237 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4238 struct attribute *attr;
4239 CORE_ADDR baseaddr;
4240 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4241 struct partial_symtab *pst;
4242 int has_pc_info;
4243 const char *filename;
4244 int *want_partial_unit_ptr = data;
4245
4246 if (comp_unit_die->tag == DW_TAG_partial_unit
4247 && (want_partial_unit_ptr == NULL
4248 || !*want_partial_unit_ptr))
4249 return;
4250
4251 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4252
4253 cu->list_in_scope = &file_symbols;
4254
4255 /* Allocate a new partial symbol table structure. */
4256 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4257 if (attr == NULL || !DW_STRING (attr))
4258 filename = "";
4259 else
4260 filename = DW_STRING (attr);
4261 pst = start_psymtab_common (objfile, objfile->section_offsets,
4262 filename,
4263 /* TEXTLOW and TEXTHIGH are set below. */
4264 0,
4265 objfile->global_psymbols.next,
4266 objfile->static_psymbols.next);
4267 pst->psymtabs_addrmap_supported = 1;
4268
4269 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4270 if (attr != NULL)
4271 pst->dirname = DW_STRING (attr);
4272
4273 pst->read_symtab_private = per_cu;
4274
4275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4276
4277 /* Store the function that reads in the rest of the symbol table. */
4278 pst->read_symtab = dwarf2_psymtab_to_symtab;
4279
4280 per_cu->v.psymtab = pst;
4281
4282 dwarf2_find_base_address (comp_unit_die, cu);
4283
4284 /* Possibly set the default values of LOWPC and HIGHPC from
4285 `DW_AT_ranges'. */
4286 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4287 &best_highpc, cu, pst);
4288 if (has_pc_info == 1 && best_lowpc < best_highpc)
4289 /* Store the contiguous range if it is not empty; it can be empty for
4290 CUs with no code. */
4291 addrmap_set_empty (objfile->psymtabs_addrmap,
4292 best_lowpc + baseaddr,
4293 best_highpc + baseaddr - 1, pst);
4294
4295 /* Check if comp unit has_children.
4296 If so, read the rest of the partial symbols from this comp unit.
4297 If not, there's no more debug_info for this comp unit. */
4298 if (has_children)
4299 {
4300 struct partial_die_info *first_die;
4301 CORE_ADDR lowpc, highpc;
4302
4303 lowpc = ((CORE_ADDR) -1);
4304 highpc = ((CORE_ADDR) 0);
4305
4306 first_die = load_partial_dies (reader, info_ptr, 1);
4307
4308 scan_partial_symbols (first_die, &lowpc, &highpc,
4309 ! has_pc_info, cu);
4310
4311 /* If we didn't find a lowpc, set it to highpc to avoid
4312 complaints from `maint check'. */
4313 if (lowpc == ((CORE_ADDR) -1))
4314 lowpc = highpc;
4315
4316 /* If the compilation unit didn't have an explicit address range,
4317 then use the information extracted from its child dies. */
4318 if (! has_pc_info)
4319 {
4320 best_lowpc = lowpc;
4321 best_highpc = highpc;
4322 }
4323 }
4324 pst->textlow = best_lowpc + baseaddr;
4325 pst->texthigh = best_highpc + baseaddr;
4326
4327 pst->n_global_syms = objfile->global_psymbols.next -
4328 (objfile->global_psymbols.list + pst->globals_offset);
4329 pst->n_static_syms = objfile->static_psymbols.next -
4330 (objfile->static_psymbols.list + pst->statics_offset);
4331 sort_pst_symbols (pst);
4332
4333 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4334 {
4335 int i;
4336 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4337 struct dwarf2_per_cu_data *iter;
4338
4339 /* Fill in 'dependencies' here; we fill in 'users' in a
4340 post-pass. */
4341 pst->number_of_dependencies = len;
4342 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4343 len * sizeof (struct symtab *));
4344 for (i = 0;
4345 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4346 i, iter);
4347 ++i)
4348 pst->dependencies[i] = iter->v.psymtab;
4349
4350 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4351 }
4352
4353 if (per_cu->is_debug_types)
4354 {
4355 /* It's not clear we want to do anything with stmt lists here.
4356 Waiting to see what gcc ultimately does. */
4357 }
4358 else
4359 {
4360 /* Get the list of files included in the current compilation unit,
4361 and build a psymtab for each of them. */
4362 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4363 }
4364 }
4365
4366 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4367 Process compilation unit THIS_CU for a psymtab. */
4368
4369 static void
4370 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4371 int want_partial_unit)
4372 {
4373 /* If this compilation unit was already read in, free the
4374 cached copy in order to read it in again. This is
4375 necessary because we skipped some symbols when we first
4376 read in the compilation unit (see load_partial_dies).
4377 This problem could be avoided, but the benefit is unclear. */
4378 if (this_cu->cu != NULL)
4379 free_one_cached_comp_unit (this_cu);
4380
4381 gdb_assert (! this_cu->is_debug_types);
4382 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4383 &want_partial_unit);
4384
4385 /* Age out any secondary CUs. */
4386 age_cached_comp_units ();
4387 }
4388
4389 /* Traversal function for htab_traverse_noresize.
4390 Process one .debug_types comp-unit. */
4391
4392 static int
4393 process_psymtab_type_unit (void **slot, void *info)
4394 {
4395 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4396 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4397
4398 gdb_assert (per_cu->is_debug_types);
4399 gdb_assert (info == NULL);
4400
4401 /* If this compilation unit was already read in, free the
4402 cached copy in order to read it in again. This is
4403 necessary because we skipped some symbols when we first
4404 read in the compilation unit (see load_partial_dies).
4405 This problem could be avoided, but the benefit is unclear. */
4406 if (per_cu->cu != NULL)
4407 free_one_cached_comp_unit (per_cu);
4408
4409 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4410 NULL);
4411
4412 /* Age out any secondary CUs. */
4413 age_cached_comp_units ();
4414
4415 return 1;
4416 }
4417
4418 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4419 Build partial symbol tables for the .debug_types comp-units. */
4420
4421 static void
4422 build_type_psymtabs (struct objfile *objfile)
4423 {
4424 if (! create_all_type_units (objfile))
4425 return;
4426
4427 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4428 process_psymtab_type_unit, NULL);
4429 }
4430
4431 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4432
4433 static void
4434 psymtabs_addrmap_cleanup (void *o)
4435 {
4436 struct objfile *objfile = o;
4437
4438 objfile->psymtabs_addrmap = NULL;
4439 }
4440
4441 /* Compute the 'user' field for each psymtab in OBJFILE. */
4442
4443 static void
4444 set_partial_user (struct objfile *objfile)
4445 {
4446 int i;
4447
4448 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4449 {
4450 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4451 struct partial_symtab *pst = per_cu->v.psymtab;
4452 int j;
4453
4454 for (j = 0; j < pst->number_of_dependencies; ++j)
4455 {
4456 /* Set the 'user' field only if it is not already set. */
4457 if (pst->dependencies[j]->user == NULL)
4458 pst->dependencies[j]->user = pst;
4459 }
4460 }
4461 }
4462
4463 /* Build the partial symbol table by doing a quick pass through the
4464 .debug_info and .debug_abbrev sections. */
4465
4466 static void
4467 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4468 {
4469 struct cleanup *back_to, *addrmap_cleanup;
4470 struct obstack temp_obstack;
4471 int i;
4472
4473 if (dwarf2_read_debug)
4474 {
4475 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4476 objfile->name);
4477 }
4478
4479 dwarf2_per_objfile->reading_partial_symbols = 1;
4480
4481 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4482
4483 /* Any cached compilation units will be linked by the per-objfile
4484 read_in_chain. Make sure to free them when we're done. */
4485 back_to = make_cleanup (free_cached_comp_units, NULL);
4486
4487 build_type_psymtabs (objfile);
4488
4489 create_all_comp_units (objfile);
4490
4491 /* Create a temporary address map on a temporary obstack. We later
4492 copy this to the final obstack. */
4493 obstack_init (&temp_obstack);
4494 make_cleanup_obstack_free (&temp_obstack);
4495 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4496 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4497
4498 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4499 {
4500 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4501
4502 process_psymtab_comp_unit (per_cu, 0);
4503 }
4504
4505 set_partial_user (objfile);
4506
4507 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4508 &objfile->objfile_obstack);
4509 discard_cleanups (addrmap_cleanup);
4510
4511 do_cleanups (back_to);
4512
4513 if (dwarf2_read_debug)
4514 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4515 objfile->name);
4516 }
4517
4518 /* die_reader_func for load_partial_comp_unit. */
4519
4520 static void
4521 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4522 gdb_byte *info_ptr,
4523 struct die_info *comp_unit_die,
4524 int has_children,
4525 void *data)
4526 {
4527 struct dwarf2_cu *cu = reader->cu;
4528
4529 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4530
4531 /* Check if comp unit has_children.
4532 If so, read the rest of the partial symbols from this comp unit.
4533 If not, there's no more debug_info for this comp unit. */
4534 if (has_children)
4535 load_partial_dies (reader, info_ptr, 0);
4536 }
4537
4538 /* Load the partial DIEs for a secondary CU into memory.
4539 This is also used when rereading a primary CU with load_all_dies. */
4540
4541 static void
4542 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4543 {
4544 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4545 }
4546
4547 /* Create a list of all compilation units in OBJFILE.
4548 This is only done for -readnow and building partial symtabs. */
4549
4550 static void
4551 create_all_comp_units (struct objfile *objfile)
4552 {
4553 int n_allocated;
4554 int n_comp_units;
4555 struct dwarf2_per_cu_data **all_comp_units;
4556 gdb_byte *info_ptr;
4557
4558 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4559 info_ptr = dwarf2_per_objfile->info.buffer;
4560
4561 n_comp_units = 0;
4562 n_allocated = 10;
4563 all_comp_units = xmalloc (n_allocated
4564 * sizeof (struct dwarf2_per_cu_data *));
4565
4566 while (info_ptr < dwarf2_per_objfile->info.buffer
4567 + dwarf2_per_objfile->info.size)
4568 {
4569 unsigned int length, initial_length_size;
4570 struct dwarf2_per_cu_data *this_cu;
4571 sect_offset offset;
4572
4573 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4574
4575 /* Read just enough information to find out where the next
4576 compilation unit is. */
4577 length = read_initial_length (objfile->obfd, info_ptr,
4578 &initial_length_size);
4579
4580 /* Save the compilation unit for later lookup. */
4581 this_cu = obstack_alloc (&objfile->objfile_obstack,
4582 sizeof (struct dwarf2_per_cu_data));
4583 memset (this_cu, 0, sizeof (*this_cu));
4584 this_cu->offset = offset;
4585 this_cu->length = length + initial_length_size;
4586 this_cu->objfile = objfile;
4587 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4588
4589 if (n_comp_units == n_allocated)
4590 {
4591 n_allocated *= 2;
4592 all_comp_units = xrealloc (all_comp_units,
4593 n_allocated
4594 * sizeof (struct dwarf2_per_cu_data *));
4595 }
4596 all_comp_units[n_comp_units++] = this_cu;
4597
4598 info_ptr = info_ptr + this_cu->length;
4599 }
4600
4601 dwarf2_per_objfile->all_comp_units
4602 = obstack_alloc (&objfile->objfile_obstack,
4603 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4604 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4605 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4606 xfree (all_comp_units);
4607 dwarf2_per_objfile->n_comp_units = n_comp_units;
4608 }
4609
4610 /* Process all loaded DIEs for compilation unit CU, starting at
4611 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4612 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4613 DW_AT_ranges). If NEED_PC is set, then this function will set
4614 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4615 and record the covered ranges in the addrmap. */
4616
4617 static void
4618 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4619 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4620 {
4621 struct partial_die_info *pdi;
4622
4623 /* Now, march along the PDI's, descending into ones which have
4624 interesting children but skipping the children of the other ones,
4625 until we reach the end of the compilation unit. */
4626
4627 pdi = first_die;
4628
4629 while (pdi != NULL)
4630 {
4631 fixup_partial_die (pdi, cu);
4632
4633 /* Anonymous namespaces or modules have no name but have interesting
4634 children, so we need to look at them. Ditto for anonymous
4635 enums. */
4636
4637 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4638 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4639 || pdi->tag == DW_TAG_imported_unit)
4640 {
4641 switch (pdi->tag)
4642 {
4643 case DW_TAG_subprogram:
4644 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4645 break;
4646 case DW_TAG_constant:
4647 case DW_TAG_variable:
4648 case DW_TAG_typedef:
4649 case DW_TAG_union_type:
4650 if (!pdi->is_declaration)
4651 {
4652 add_partial_symbol (pdi, cu);
4653 }
4654 break;
4655 case DW_TAG_class_type:
4656 case DW_TAG_interface_type:
4657 case DW_TAG_structure_type:
4658 if (!pdi->is_declaration)
4659 {
4660 add_partial_symbol (pdi, cu);
4661 }
4662 break;
4663 case DW_TAG_enumeration_type:
4664 if (!pdi->is_declaration)
4665 add_partial_enumeration (pdi, cu);
4666 break;
4667 case DW_TAG_base_type:
4668 case DW_TAG_subrange_type:
4669 /* File scope base type definitions are added to the partial
4670 symbol table. */
4671 add_partial_symbol (pdi, cu);
4672 break;
4673 case DW_TAG_namespace:
4674 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4675 break;
4676 case DW_TAG_module:
4677 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4678 break;
4679 case DW_TAG_imported_unit:
4680 {
4681 struct dwarf2_per_cu_data *per_cu;
4682
4683 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4684 cu->objfile);
4685
4686 /* Go read the partial unit, if needed. */
4687 if (per_cu->v.psymtab == NULL)
4688 process_psymtab_comp_unit (per_cu, 1);
4689
4690 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4691 per_cu);
4692 }
4693 break;
4694 default:
4695 break;
4696 }
4697 }
4698
4699 /* If the die has a sibling, skip to the sibling. */
4700
4701 pdi = pdi->die_sibling;
4702 }
4703 }
4704
4705 /* Functions used to compute the fully scoped name of a partial DIE.
4706
4707 Normally, this is simple. For C++, the parent DIE's fully scoped
4708 name is concatenated with "::" and the partial DIE's name. For
4709 Java, the same thing occurs except that "." is used instead of "::".
4710 Enumerators are an exception; they use the scope of their parent
4711 enumeration type, i.e. the name of the enumeration type is not
4712 prepended to the enumerator.
4713
4714 There are two complexities. One is DW_AT_specification; in this
4715 case "parent" means the parent of the target of the specification,
4716 instead of the direct parent of the DIE. The other is compilers
4717 which do not emit DW_TAG_namespace; in this case we try to guess
4718 the fully qualified name of structure types from their members'
4719 linkage names. This must be done using the DIE's children rather
4720 than the children of any DW_AT_specification target. We only need
4721 to do this for structures at the top level, i.e. if the target of
4722 any DW_AT_specification (if any; otherwise the DIE itself) does not
4723 have a parent. */
4724
4725 /* Compute the scope prefix associated with PDI's parent, in
4726 compilation unit CU. The result will be allocated on CU's
4727 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4728 field. NULL is returned if no prefix is necessary. */
4729 static char *
4730 partial_die_parent_scope (struct partial_die_info *pdi,
4731 struct dwarf2_cu *cu)
4732 {
4733 char *grandparent_scope;
4734 struct partial_die_info *parent, *real_pdi;
4735
4736 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4737 then this means the parent of the specification DIE. */
4738
4739 real_pdi = pdi;
4740 while (real_pdi->has_specification)
4741 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4742
4743 parent = real_pdi->die_parent;
4744 if (parent == NULL)
4745 return NULL;
4746
4747 if (parent->scope_set)
4748 return parent->scope;
4749
4750 fixup_partial_die (parent, cu);
4751
4752 grandparent_scope = partial_die_parent_scope (parent, cu);
4753
4754 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4755 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4756 Work around this problem here. */
4757 if (cu->language == language_cplus
4758 && parent->tag == DW_TAG_namespace
4759 && strcmp (parent->name, "::") == 0
4760 && grandparent_scope == NULL)
4761 {
4762 parent->scope = NULL;
4763 parent->scope_set = 1;
4764 return NULL;
4765 }
4766
4767 if (pdi->tag == DW_TAG_enumerator)
4768 /* Enumerators should not get the name of the enumeration as a prefix. */
4769 parent->scope = grandparent_scope;
4770 else if (parent->tag == DW_TAG_namespace
4771 || parent->tag == DW_TAG_module
4772 || parent->tag == DW_TAG_structure_type
4773 || parent->tag == DW_TAG_class_type
4774 || parent->tag == DW_TAG_interface_type
4775 || parent->tag == DW_TAG_union_type
4776 || parent->tag == DW_TAG_enumeration_type)
4777 {
4778 if (grandparent_scope == NULL)
4779 parent->scope = parent->name;
4780 else
4781 parent->scope = typename_concat (&cu->comp_unit_obstack,
4782 grandparent_scope,
4783 parent->name, 0, cu);
4784 }
4785 else
4786 {
4787 /* FIXME drow/2004-04-01: What should we be doing with
4788 function-local names? For partial symbols, we should probably be
4789 ignoring them. */
4790 complaint (&symfile_complaints,
4791 _("unhandled containing DIE tag %d for DIE at %d"),
4792 parent->tag, pdi->offset.sect_off);
4793 parent->scope = grandparent_scope;
4794 }
4795
4796 parent->scope_set = 1;
4797 return parent->scope;
4798 }
4799
4800 /* Return the fully scoped name associated with PDI, from compilation unit
4801 CU. The result will be allocated with malloc. */
4802
4803 static char *
4804 partial_die_full_name (struct partial_die_info *pdi,
4805 struct dwarf2_cu *cu)
4806 {
4807 char *parent_scope;
4808
4809 /* If this is a template instantiation, we can not work out the
4810 template arguments from partial DIEs. So, unfortunately, we have
4811 to go through the full DIEs. At least any work we do building
4812 types here will be reused if full symbols are loaded later. */
4813 if (pdi->has_template_arguments)
4814 {
4815 fixup_partial_die (pdi, cu);
4816
4817 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4818 {
4819 struct die_info *die;
4820 struct attribute attr;
4821 struct dwarf2_cu *ref_cu = cu;
4822
4823 /* DW_FORM_ref_addr is using section offset. */
4824 attr.name = 0;
4825 attr.form = DW_FORM_ref_addr;
4826 attr.u.unsnd = pdi->offset.sect_off;
4827 die = follow_die_ref (NULL, &attr, &ref_cu);
4828
4829 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4830 }
4831 }
4832
4833 parent_scope = partial_die_parent_scope (pdi, cu);
4834 if (parent_scope == NULL)
4835 return NULL;
4836 else
4837 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4838 }
4839
4840 static void
4841 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4842 {
4843 struct objfile *objfile = cu->objfile;
4844 CORE_ADDR addr = 0;
4845 char *actual_name = NULL;
4846 CORE_ADDR baseaddr;
4847 int built_actual_name = 0;
4848
4849 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4850
4851 actual_name = partial_die_full_name (pdi, cu);
4852 if (actual_name)
4853 built_actual_name = 1;
4854
4855 if (actual_name == NULL)
4856 actual_name = pdi->name;
4857
4858 switch (pdi->tag)
4859 {
4860 case DW_TAG_subprogram:
4861 if (pdi->is_external || cu->language == language_ada)
4862 {
4863 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4864 of the global scope. But in Ada, we want to be able to access
4865 nested procedures globally. So all Ada subprograms are stored
4866 in the global scope. */
4867 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4868 mst_text, objfile); */
4869 add_psymbol_to_list (actual_name, strlen (actual_name),
4870 built_actual_name,
4871 VAR_DOMAIN, LOC_BLOCK,
4872 &objfile->global_psymbols,
4873 0, pdi->lowpc + baseaddr,
4874 cu->language, objfile);
4875 }
4876 else
4877 {
4878 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4879 mst_file_text, objfile); */
4880 add_psymbol_to_list (actual_name, strlen (actual_name),
4881 built_actual_name,
4882 VAR_DOMAIN, LOC_BLOCK,
4883 &objfile->static_psymbols,
4884 0, pdi->lowpc + baseaddr,
4885 cu->language, objfile);
4886 }
4887 break;
4888 case DW_TAG_constant:
4889 {
4890 struct psymbol_allocation_list *list;
4891
4892 if (pdi->is_external)
4893 list = &objfile->global_psymbols;
4894 else
4895 list = &objfile->static_psymbols;
4896 add_psymbol_to_list (actual_name, strlen (actual_name),
4897 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4898 list, 0, 0, cu->language, objfile);
4899 }
4900 break;
4901 case DW_TAG_variable:
4902 if (pdi->d.locdesc)
4903 addr = decode_locdesc (pdi->d.locdesc, cu);
4904
4905 if (pdi->d.locdesc
4906 && addr == 0
4907 && !dwarf2_per_objfile->has_section_at_zero)
4908 {
4909 /* A global or static variable may also have been stripped
4910 out by the linker if unused, in which case its address
4911 will be nullified; do not add such variables into partial
4912 symbol table then. */
4913 }
4914 else if (pdi->is_external)
4915 {
4916 /* Global Variable.
4917 Don't enter into the minimal symbol tables as there is
4918 a minimal symbol table entry from the ELF symbols already.
4919 Enter into partial symbol table if it has a location
4920 descriptor or a type.
4921 If the location descriptor is missing, new_symbol will create
4922 a LOC_UNRESOLVED symbol, the address of the variable will then
4923 be determined from the minimal symbol table whenever the variable
4924 is referenced.
4925 The address for the partial symbol table entry is not
4926 used by GDB, but it comes in handy for debugging partial symbol
4927 table building. */
4928
4929 if (pdi->d.locdesc || pdi->has_type)
4930 add_psymbol_to_list (actual_name, strlen (actual_name),
4931 built_actual_name,
4932 VAR_DOMAIN, LOC_STATIC,
4933 &objfile->global_psymbols,
4934 0, addr + baseaddr,
4935 cu->language, objfile);
4936 }
4937 else
4938 {
4939 /* Static Variable. Skip symbols without location descriptors. */
4940 if (pdi->d.locdesc == NULL)
4941 {
4942 if (built_actual_name)
4943 xfree (actual_name);
4944 return;
4945 }
4946 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4947 mst_file_data, objfile); */
4948 add_psymbol_to_list (actual_name, strlen (actual_name),
4949 built_actual_name,
4950 VAR_DOMAIN, LOC_STATIC,
4951 &objfile->static_psymbols,
4952 0, addr + baseaddr,
4953 cu->language, objfile);
4954 }
4955 break;
4956 case DW_TAG_typedef:
4957 case DW_TAG_base_type:
4958 case DW_TAG_subrange_type:
4959 add_psymbol_to_list (actual_name, strlen (actual_name),
4960 built_actual_name,
4961 VAR_DOMAIN, LOC_TYPEDEF,
4962 &objfile->static_psymbols,
4963 0, (CORE_ADDR) 0, cu->language, objfile);
4964 break;
4965 case DW_TAG_namespace:
4966 add_psymbol_to_list (actual_name, strlen (actual_name),
4967 built_actual_name,
4968 VAR_DOMAIN, LOC_TYPEDEF,
4969 &objfile->global_psymbols,
4970 0, (CORE_ADDR) 0, cu->language, objfile);
4971 break;
4972 case DW_TAG_class_type:
4973 case DW_TAG_interface_type:
4974 case DW_TAG_structure_type:
4975 case DW_TAG_union_type:
4976 case DW_TAG_enumeration_type:
4977 /* Skip external references. The DWARF standard says in the section
4978 about "Structure, Union, and Class Type Entries": "An incomplete
4979 structure, union or class type is represented by a structure,
4980 union or class entry that does not have a byte size attribute
4981 and that has a DW_AT_declaration attribute." */
4982 if (!pdi->has_byte_size && pdi->is_declaration)
4983 {
4984 if (built_actual_name)
4985 xfree (actual_name);
4986 return;
4987 }
4988
4989 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4990 static vs. global. */
4991 add_psymbol_to_list (actual_name, strlen (actual_name),
4992 built_actual_name,
4993 STRUCT_DOMAIN, LOC_TYPEDEF,
4994 (cu->language == language_cplus
4995 || cu->language == language_java)
4996 ? &objfile->global_psymbols
4997 : &objfile->static_psymbols,
4998 0, (CORE_ADDR) 0, cu->language, objfile);
4999
5000 break;
5001 case DW_TAG_enumerator:
5002 add_psymbol_to_list (actual_name, strlen (actual_name),
5003 built_actual_name,
5004 VAR_DOMAIN, LOC_CONST,
5005 (cu->language == language_cplus
5006 || cu->language == language_java)
5007 ? &objfile->global_psymbols
5008 : &objfile->static_psymbols,
5009 0, (CORE_ADDR) 0, cu->language, objfile);
5010 break;
5011 default:
5012 break;
5013 }
5014
5015 if (built_actual_name)
5016 xfree (actual_name);
5017 }
5018
5019 /* Read a partial die corresponding to a namespace; also, add a symbol
5020 corresponding to that namespace to the symbol table. NAMESPACE is
5021 the name of the enclosing namespace. */
5022
5023 static void
5024 add_partial_namespace (struct partial_die_info *pdi,
5025 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5026 int need_pc, struct dwarf2_cu *cu)
5027 {
5028 /* Add a symbol for the namespace. */
5029
5030 add_partial_symbol (pdi, cu);
5031
5032 /* Now scan partial symbols in that namespace. */
5033
5034 if (pdi->has_children)
5035 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5036 }
5037
5038 /* Read a partial die corresponding to a Fortran module. */
5039
5040 static void
5041 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5042 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5043 {
5044 /* Now scan partial symbols in that module. */
5045
5046 if (pdi->has_children)
5047 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5048 }
5049
5050 /* Read a partial die corresponding to a subprogram and create a partial
5051 symbol for that subprogram. When the CU language allows it, this
5052 routine also defines a partial symbol for each nested subprogram
5053 that this subprogram contains.
5054
5055 DIE my also be a lexical block, in which case we simply search
5056 recursively for suprograms defined inside that lexical block.
5057 Again, this is only performed when the CU language allows this
5058 type of definitions. */
5059
5060 static void
5061 add_partial_subprogram (struct partial_die_info *pdi,
5062 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5063 int need_pc, struct dwarf2_cu *cu)
5064 {
5065 if (pdi->tag == DW_TAG_subprogram)
5066 {
5067 if (pdi->has_pc_info)
5068 {
5069 if (pdi->lowpc < *lowpc)
5070 *lowpc = pdi->lowpc;
5071 if (pdi->highpc > *highpc)
5072 *highpc = pdi->highpc;
5073 if (need_pc)
5074 {
5075 CORE_ADDR baseaddr;
5076 struct objfile *objfile = cu->objfile;
5077
5078 baseaddr = ANOFFSET (objfile->section_offsets,
5079 SECT_OFF_TEXT (objfile));
5080 addrmap_set_empty (objfile->psymtabs_addrmap,
5081 pdi->lowpc + baseaddr,
5082 pdi->highpc - 1 + baseaddr,
5083 cu->per_cu->v.psymtab);
5084 }
5085 }
5086
5087 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5088 {
5089 if (!pdi->is_declaration)
5090 /* Ignore subprogram DIEs that do not have a name, they are
5091 illegal. Do not emit a complaint at this point, we will
5092 do so when we convert this psymtab into a symtab. */
5093 if (pdi->name)
5094 add_partial_symbol (pdi, cu);
5095 }
5096 }
5097
5098 if (! pdi->has_children)
5099 return;
5100
5101 if (cu->language == language_ada)
5102 {
5103 pdi = pdi->die_child;
5104 while (pdi != NULL)
5105 {
5106 fixup_partial_die (pdi, cu);
5107 if (pdi->tag == DW_TAG_subprogram
5108 || pdi->tag == DW_TAG_lexical_block)
5109 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5110 pdi = pdi->die_sibling;
5111 }
5112 }
5113 }
5114
5115 /* Read a partial die corresponding to an enumeration type. */
5116
5117 static void
5118 add_partial_enumeration (struct partial_die_info *enum_pdi,
5119 struct dwarf2_cu *cu)
5120 {
5121 struct partial_die_info *pdi;
5122
5123 if (enum_pdi->name != NULL)
5124 add_partial_symbol (enum_pdi, cu);
5125
5126 pdi = enum_pdi->die_child;
5127 while (pdi)
5128 {
5129 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
5130 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5131 else
5132 add_partial_symbol (pdi, cu);
5133 pdi = pdi->die_sibling;
5134 }
5135 }
5136
5137 /* Return the initial uleb128 in the die at INFO_PTR. */
5138
5139 static unsigned int
5140 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5141 {
5142 unsigned int bytes_read;
5143
5144 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5145 }
5146
5147 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5148 Return the corresponding abbrev, or NULL if the number is zero (indicating
5149 an empty DIE). In either case *BYTES_READ will be set to the length of
5150 the initial number. */
5151
5152 static struct abbrev_info *
5153 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5154 struct dwarf2_cu *cu)
5155 {
5156 bfd *abfd = cu->objfile->obfd;
5157 unsigned int abbrev_number;
5158 struct abbrev_info *abbrev;
5159
5160 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5161
5162 if (abbrev_number == 0)
5163 return NULL;
5164
5165 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5166 if (!abbrev)
5167 {
5168 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5169 abbrev_number, bfd_get_filename (abfd));
5170 }
5171
5172 return abbrev;
5173 }
5174
5175 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5176 Returns a pointer to the end of a series of DIEs, terminated by an empty
5177 DIE. Any children of the skipped DIEs will also be skipped. */
5178
5179 static gdb_byte *
5180 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5181 {
5182 struct dwarf2_cu *cu = reader->cu;
5183 struct abbrev_info *abbrev;
5184 unsigned int bytes_read;
5185
5186 while (1)
5187 {
5188 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5189 if (abbrev == NULL)
5190 return info_ptr + bytes_read;
5191 else
5192 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5193 }
5194 }
5195
5196 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5197 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5198 abbrev corresponding to that skipped uleb128 should be passed in
5199 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5200 children. */
5201
5202 static gdb_byte *
5203 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5204 struct abbrev_info *abbrev)
5205 {
5206 unsigned int bytes_read;
5207 struct attribute attr;
5208 bfd *abfd = reader->abfd;
5209 struct dwarf2_cu *cu = reader->cu;
5210 gdb_byte *buffer = reader->buffer;
5211 const gdb_byte *buffer_end = reader->buffer_end;
5212 gdb_byte *start_info_ptr = info_ptr;
5213 unsigned int form, i;
5214
5215 for (i = 0; i < abbrev->num_attrs; i++)
5216 {
5217 /* The only abbrev we care about is DW_AT_sibling. */
5218 if (abbrev->attrs[i].name == DW_AT_sibling)
5219 {
5220 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5221 if (attr.form == DW_FORM_ref_addr)
5222 complaint (&symfile_complaints,
5223 _("ignoring absolute DW_AT_sibling"));
5224 else
5225 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5226 }
5227
5228 /* If it isn't DW_AT_sibling, skip this attribute. */
5229 form = abbrev->attrs[i].form;
5230 skip_attribute:
5231 switch (form)
5232 {
5233 case DW_FORM_ref_addr:
5234 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5235 and later it is offset sized. */
5236 if (cu->header.version == 2)
5237 info_ptr += cu->header.addr_size;
5238 else
5239 info_ptr += cu->header.offset_size;
5240 break;
5241 case DW_FORM_addr:
5242 info_ptr += cu->header.addr_size;
5243 break;
5244 case DW_FORM_data1:
5245 case DW_FORM_ref1:
5246 case DW_FORM_flag:
5247 info_ptr += 1;
5248 break;
5249 case DW_FORM_flag_present:
5250 break;
5251 case DW_FORM_data2:
5252 case DW_FORM_ref2:
5253 info_ptr += 2;
5254 break;
5255 case DW_FORM_data4:
5256 case DW_FORM_ref4:
5257 info_ptr += 4;
5258 break;
5259 case DW_FORM_data8:
5260 case DW_FORM_ref8:
5261 case DW_FORM_ref_sig8:
5262 info_ptr += 8;
5263 break;
5264 case DW_FORM_string:
5265 read_direct_string (abfd, info_ptr, &bytes_read);
5266 info_ptr += bytes_read;
5267 break;
5268 case DW_FORM_sec_offset:
5269 case DW_FORM_strp:
5270 info_ptr += cu->header.offset_size;
5271 break;
5272 case DW_FORM_exprloc:
5273 case DW_FORM_block:
5274 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5275 info_ptr += bytes_read;
5276 break;
5277 case DW_FORM_block1:
5278 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5279 break;
5280 case DW_FORM_block2:
5281 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5282 break;
5283 case DW_FORM_block4:
5284 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5285 break;
5286 case DW_FORM_sdata:
5287 case DW_FORM_udata:
5288 case DW_FORM_ref_udata:
5289 case DW_FORM_GNU_addr_index:
5290 case DW_FORM_GNU_str_index:
5291 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5292 break;
5293 case DW_FORM_indirect:
5294 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5295 info_ptr += bytes_read;
5296 /* We need to continue parsing from here, so just go back to
5297 the top. */
5298 goto skip_attribute;
5299
5300 default:
5301 error (_("Dwarf Error: Cannot handle %s "
5302 "in DWARF reader [in module %s]"),
5303 dwarf_form_name (form),
5304 bfd_get_filename (abfd));
5305 }
5306 }
5307
5308 if (abbrev->has_children)
5309 return skip_children (reader, info_ptr);
5310 else
5311 return info_ptr;
5312 }
5313
5314 /* Locate ORIG_PDI's sibling.
5315 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5316
5317 static gdb_byte *
5318 locate_pdi_sibling (const struct die_reader_specs *reader,
5319 struct partial_die_info *orig_pdi,
5320 gdb_byte *info_ptr)
5321 {
5322 /* Do we know the sibling already? */
5323
5324 if (orig_pdi->sibling)
5325 return orig_pdi->sibling;
5326
5327 /* Are there any children to deal with? */
5328
5329 if (!orig_pdi->has_children)
5330 return info_ptr;
5331
5332 /* Skip the children the long way. */
5333
5334 return skip_children (reader, info_ptr);
5335 }
5336
5337 /* Expand this partial symbol table into a full symbol table. */
5338
5339 static void
5340 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5341 {
5342 if (pst != NULL)
5343 {
5344 if (pst->readin)
5345 {
5346 warning (_("bug: psymtab for %s is already read in."),
5347 pst->filename);
5348 }
5349 else
5350 {
5351 if (info_verbose)
5352 {
5353 printf_filtered (_("Reading in symbols for %s..."),
5354 pst->filename);
5355 gdb_flush (gdb_stdout);
5356 }
5357
5358 /* Restore our global data. */
5359 dwarf2_per_objfile = objfile_data (pst->objfile,
5360 dwarf2_objfile_data_key);
5361
5362 /* If this psymtab is constructed from a debug-only objfile, the
5363 has_section_at_zero flag will not necessarily be correct. We
5364 can get the correct value for this flag by looking at the data
5365 associated with the (presumably stripped) associated objfile. */
5366 if (pst->objfile->separate_debug_objfile_backlink)
5367 {
5368 struct dwarf2_per_objfile *dpo_backlink
5369 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5370 dwarf2_objfile_data_key);
5371
5372 dwarf2_per_objfile->has_section_at_zero
5373 = dpo_backlink->has_section_at_zero;
5374 }
5375
5376 dwarf2_per_objfile->reading_partial_symbols = 0;
5377
5378 psymtab_to_symtab_1 (pst);
5379
5380 /* Finish up the debug error message. */
5381 if (info_verbose)
5382 printf_filtered (_("done.\n"));
5383 }
5384 }
5385
5386 process_cu_includes ();
5387 }
5388 \f
5389 /* Reading in full CUs. */
5390
5391 /* Add PER_CU to the queue. */
5392
5393 static void
5394 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5395 enum language pretend_language)
5396 {
5397 struct dwarf2_queue_item *item;
5398
5399 per_cu->queued = 1;
5400 item = xmalloc (sizeof (*item));
5401 item->per_cu = per_cu;
5402 item->pretend_language = pretend_language;
5403 item->next = NULL;
5404
5405 if (dwarf2_queue == NULL)
5406 dwarf2_queue = item;
5407 else
5408 dwarf2_queue_tail->next = item;
5409
5410 dwarf2_queue_tail = item;
5411 }
5412
5413 /* Process the queue. */
5414
5415 static void
5416 process_queue (void)
5417 {
5418 struct dwarf2_queue_item *item, *next_item;
5419
5420 if (dwarf2_read_debug)
5421 {
5422 fprintf_unfiltered (gdb_stdlog,
5423 "Expanding one or more symtabs of objfile %s ...\n",
5424 dwarf2_per_objfile->objfile->name);
5425 }
5426
5427 /* The queue starts out with one item, but following a DIE reference
5428 may load a new CU, adding it to the end of the queue. */
5429 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5430 {
5431 if (dwarf2_per_objfile->using_index
5432 ? !item->per_cu->v.quick->symtab
5433 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5434 process_full_comp_unit (item->per_cu, item->pretend_language);
5435
5436 item->per_cu->queued = 0;
5437 next_item = item->next;
5438 xfree (item);
5439 }
5440
5441 dwarf2_queue_tail = NULL;
5442
5443 if (dwarf2_read_debug)
5444 {
5445 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5446 dwarf2_per_objfile->objfile->name);
5447 }
5448 }
5449
5450 /* Free all allocated queue entries. This function only releases anything if
5451 an error was thrown; if the queue was processed then it would have been
5452 freed as we went along. */
5453
5454 static void
5455 dwarf2_release_queue (void *dummy)
5456 {
5457 struct dwarf2_queue_item *item, *last;
5458
5459 item = dwarf2_queue;
5460 while (item)
5461 {
5462 /* Anything still marked queued is likely to be in an
5463 inconsistent state, so discard it. */
5464 if (item->per_cu->queued)
5465 {
5466 if (item->per_cu->cu != NULL)
5467 free_one_cached_comp_unit (item->per_cu);
5468 item->per_cu->queued = 0;
5469 }
5470
5471 last = item;
5472 item = item->next;
5473 xfree (last);
5474 }
5475
5476 dwarf2_queue = dwarf2_queue_tail = NULL;
5477 }
5478
5479 /* Read in full symbols for PST, and anything it depends on. */
5480
5481 static void
5482 psymtab_to_symtab_1 (struct partial_symtab *pst)
5483 {
5484 struct dwarf2_per_cu_data *per_cu;
5485 int i;
5486
5487 if (pst->readin)
5488 return;
5489
5490 for (i = 0; i < pst->number_of_dependencies; i++)
5491 if (!pst->dependencies[i]->readin
5492 && pst->dependencies[i]->user == NULL)
5493 {
5494 /* Inform about additional files that need to be read in. */
5495 if (info_verbose)
5496 {
5497 /* FIXME: i18n: Need to make this a single string. */
5498 fputs_filtered (" ", gdb_stdout);
5499 wrap_here ("");
5500 fputs_filtered ("and ", gdb_stdout);
5501 wrap_here ("");
5502 printf_filtered ("%s...", pst->dependencies[i]->filename);
5503 wrap_here (""); /* Flush output. */
5504 gdb_flush (gdb_stdout);
5505 }
5506 psymtab_to_symtab_1 (pst->dependencies[i]);
5507 }
5508
5509 per_cu = pst->read_symtab_private;
5510
5511 if (per_cu == NULL)
5512 {
5513 /* It's an include file, no symbols to read for it.
5514 Everything is in the parent symtab. */
5515 pst->readin = 1;
5516 return;
5517 }
5518
5519 dw2_do_instantiate_symtab (per_cu);
5520 }
5521
5522 /* Trivial hash function for die_info: the hash value of a DIE
5523 is its offset in .debug_info for this objfile. */
5524
5525 static hashval_t
5526 die_hash (const void *item)
5527 {
5528 const struct die_info *die = item;
5529
5530 return die->offset.sect_off;
5531 }
5532
5533 /* Trivial comparison function for die_info structures: two DIEs
5534 are equal if they have the same offset. */
5535
5536 static int
5537 die_eq (const void *item_lhs, const void *item_rhs)
5538 {
5539 const struct die_info *die_lhs = item_lhs;
5540 const struct die_info *die_rhs = item_rhs;
5541
5542 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5543 }
5544
5545 /* die_reader_func for load_full_comp_unit.
5546 This is identical to read_signatured_type_reader,
5547 but is kept separate for now. */
5548
5549 static void
5550 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5551 gdb_byte *info_ptr,
5552 struct die_info *comp_unit_die,
5553 int has_children,
5554 void *data)
5555 {
5556 struct dwarf2_cu *cu = reader->cu;
5557 enum language *language_ptr = data;
5558
5559 gdb_assert (cu->die_hash == NULL);
5560 cu->die_hash =
5561 htab_create_alloc_ex (cu->header.length / 12,
5562 die_hash,
5563 die_eq,
5564 NULL,
5565 &cu->comp_unit_obstack,
5566 hashtab_obstack_allocate,
5567 dummy_obstack_deallocate);
5568
5569 if (has_children)
5570 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5571 &info_ptr, comp_unit_die);
5572 cu->dies = comp_unit_die;
5573 /* comp_unit_die is not stored in die_hash, no need. */
5574
5575 /* We try not to read any attributes in this function, because not
5576 all CUs needed for references have been loaded yet, and symbol
5577 table processing isn't initialized. But we have to set the CU language,
5578 or we won't be able to build types correctly.
5579 Similarly, if we do not read the producer, we can not apply
5580 producer-specific interpretation. */
5581 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5582 }
5583
5584 /* Load the DIEs associated with PER_CU into memory. */
5585
5586 static void
5587 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5588 enum language pretend_language)
5589 {
5590 gdb_assert (! this_cu->is_debug_types);
5591
5592 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5593 &pretend_language);
5594 }
5595
5596 /* Add a DIE to the delayed physname list. */
5597
5598 static void
5599 add_to_method_list (struct type *type, int fnfield_index, int index,
5600 const char *name, struct die_info *die,
5601 struct dwarf2_cu *cu)
5602 {
5603 struct delayed_method_info mi;
5604 mi.type = type;
5605 mi.fnfield_index = fnfield_index;
5606 mi.index = index;
5607 mi.name = name;
5608 mi.die = die;
5609 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5610 }
5611
5612 /* A cleanup for freeing the delayed method list. */
5613
5614 static void
5615 free_delayed_list (void *ptr)
5616 {
5617 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5618 if (cu->method_list != NULL)
5619 {
5620 VEC_free (delayed_method_info, cu->method_list);
5621 cu->method_list = NULL;
5622 }
5623 }
5624
5625 /* Compute the physnames of any methods on the CU's method list.
5626
5627 The computation of method physnames is delayed in order to avoid the
5628 (bad) condition that one of the method's formal parameters is of an as yet
5629 incomplete type. */
5630
5631 static void
5632 compute_delayed_physnames (struct dwarf2_cu *cu)
5633 {
5634 int i;
5635 struct delayed_method_info *mi;
5636 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5637 {
5638 const char *physname;
5639 struct fn_fieldlist *fn_flp
5640 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5641 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5642 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5643 }
5644 }
5645
5646 /* Go objects should be embedded in a DW_TAG_module DIE,
5647 and it's not clear if/how imported objects will appear.
5648 To keep Go support simple until that's worked out,
5649 go back through what we've read and create something usable.
5650 We could do this while processing each DIE, and feels kinda cleaner,
5651 but that way is more invasive.
5652 This is to, for example, allow the user to type "p var" or "b main"
5653 without having to specify the package name, and allow lookups
5654 of module.object to work in contexts that use the expression
5655 parser. */
5656
5657 static void
5658 fixup_go_packaging (struct dwarf2_cu *cu)
5659 {
5660 char *package_name = NULL;
5661 struct pending *list;
5662 int i;
5663
5664 for (list = global_symbols; list != NULL; list = list->next)
5665 {
5666 for (i = 0; i < list->nsyms; ++i)
5667 {
5668 struct symbol *sym = list->symbol[i];
5669
5670 if (SYMBOL_LANGUAGE (sym) == language_go
5671 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5672 {
5673 char *this_package_name = go_symbol_package_name (sym);
5674
5675 if (this_package_name == NULL)
5676 continue;
5677 if (package_name == NULL)
5678 package_name = this_package_name;
5679 else
5680 {
5681 if (strcmp (package_name, this_package_name) != 0)
5682 complaint (&symfile_complaints,
5683 _("Symtab %s has objects from two different Go packages: %s and %s"),
5684 (sym->symtab && sym->symtab->filename
5685 ? sym->symtab->filename
5686 : cu->objfile->name),
5687 this_package_name, package_name);
5688 xfree (this_package_name);
5689 }
5690 }
5691 }
5692 }
5693
5694 if (package_name != NULL)
5695 {
5696 struct objfile *objfile = cu->objfile;
5697 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5698 package_name, objfile);
5699 struct symbol *sym;
5700
5701 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5702
5703 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5704 SYMBOL_SET_LANGUAGE (sym, language_go);
5705 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5706 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5707 e.g., "main" finds the "main" module and not C's main(). */
5708 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5709 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5710 SYMBOL_TYPE (sym) = type;
5711
5712 add_symbol_to_list (sym, &global_symbols);
5713
5714 xfree (package_name);
5715 }
5716 }
5717
5718 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5719
5720 /* Return the symtab for PER_CU. This works properly regardless of
5721 whether we're using the index or psymtabs. */
5722
5723 static struct symtab *
5724 get_symtab (struct dwarf2_per_cu_data *per_cu)
5725 {
5726 return (dwarf2_per_objfile->using_index
5727 ? per_cu->v.quick->symtab
5728 : per_cu->v.psymtab->symtab);
5729 }
5730
5731 /* A helper function for computing the list of all symbol tables
5732 included by PER_CU. */
5733
5734 static void
5735 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5736 htab_t all_children,
5737 struct dwarf2_per_cu_data *per_cu)
5738 {
5739 void **slot;
5740 int ix;
5741 struct dwarf2_per_cu_data *iter;
5742
5743 slot = htab_find_slot (all_children, per_cu, INSERT);
5744 if (*slot != NULL)
5745 {
5746 /* This inclusion and its children have been processed. */
5747 return;
5748 }
5749
5750 *slot = per_cu;
5751 /* Only add a CU if it has a symbol table. */
5752 if (get_symtab (per_cu) != NULL)
5753 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5754
5755 for (ix = 0;
5756 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5757 ++ix)
5758 recursively_compute_inclusions (result, all_children, iter);
5759 }
5760
5761 /* Compute the symtab 'includes' fields for the symtab related to
5762 PER_CU. */
5763
5764 static void
5765 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5766 {
5767 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5768 {
5769 int ix, len;
5770 struct dwarf2_per_cu_data *iter;
5771 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5772 htab_t all_children;
5773 struct symtab *symtab = get_symtab (per_cu);
5774
5775 /* If we don't have a symtab, we can just skip this case. */
5776 if (symtab == NULL)
5777 return;
5778
5779 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5780 NULL, xcalloc, xfree);
5781
5782 for (ix = 0;
5783 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5784 ix, iter);
5785 ++ix)
5786 recursively_compute_inclusions (&result_children, all_children, iter);
5787
5788 /* Now we have a transitive closure of all the included CUs, so
5789 we can convert it to a list of symtabs. */
5790 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5791 symtab->includes
5792 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5793 (len + 1) * sizeof (struct symtab *));
5794 for (ix = 0;
5795 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5796 ++ix)
5797 symtab->includes[ix] = get_symtab (iter);
5798 symtab->includes[len] = NULL;
5799
5800 VEC_free (dwarf2_per_cu_ptr, result_children);
5801 htab_delete (all_children);
5802 }
5803 }
5804
5805 /* Compute the 'includes' field for the symtabs of all the CUs we just
5806 read. */
5807
5808 static void
5809 process_cu_includes (void)
5810 {
5811 int ix;
5812 struct dwarf2_per_cu_data *iter;
5813
5814 for (ix = 0;
5815 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5816 ix, iter);
5817 ++ix)
5818 compute_symtab_includes (iter);
5819
5820 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5821 }
5822
5823 /* Generate full symbol information for PER_CU, whose DIEs have
5824 already been loaded into memory. */
5825
5826 static void
5827 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5828 enum language pretend_language)
5829 {
5830 struct dwarf2_cu *cu = per_cu->cu;
5831 struct objfile *objfile = per_cu->objfile;
5832 CORE_ADDR lowpc, highpc;
5833 struct symtab *symtab;
5834 struct cleanup *back_to, *delayed_list_cleanup;
5835 CORE_ADDR baseaddr;
5836
5837 if (dwarf2_read_debug)
5838 {
5839 fprintf_unfiltered (gdb_stdlog,
5840 "Expanding symtab of %s at offset 0x%x\n",
5841 per_cu->is_debug_types ? "TU" : "CU",
5842 per_cu->offset.sect_off);
5843 }
5844
5845 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5846
5847 buildsym_init ();
5848 back_to = make_cleanup (really_free_pendings, NULL);
5849 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5850
5851 cu->list_in_scope = &file_symbols;
5852
5853 cu->language = pretend_language;
5854 cu->language_defn = language_def (cu->language);
5855
5856 /* Do line number decoding in read_file_scope () */
5857 process_die (cu->dies, cu);
5858
5859 /* For now fudge the Go package. */
5860 if (cu->language == language_go)
5861 fixup_go_packaging (cu);
5862
5863 /* Now that we have processed all the DIEs in the CU, all the types
5864 should be complete, and it should now be safe to compute all of the
5865 physnames. */
5866 compute_delayed_physnames (cu);
5867 do_cleanups (delayed_list_cleanup);
5868
5869 /* Some compilers don't define a DW_AT_high_pc attribute for the
5870 compilation unit. If the DW_AT_high_pc is missing, synthesize
5871 it, by scanning the DIE's below the compilation unit. */
5872 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5873
5874 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5875
5876 if (symtab != NULL)
5877 {
5878 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5879
5880 /* Set symtab language to language from DW_AT_language. If the
5881 compilation is from a C file generated by language preprocessors, do
5882 not set the language if it was already deduced by start_subfile. */
5883 if (!(cu->language == language_c && symtab->language != language_c))
5884 symtab->language = cu->language;
5885
5886 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5887 produce DW_AT_location with location lists but it can be possibly
5888 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5889 there were bugs in prologue debug info, fixed later in GCC-4.5
5890 by "unwind info for epilogues" patch (which is not directly related).
5891
5892 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5893 needed, it would be wrong due to missing DW_AT_producer there.
5894
5895 Still one can confuse GDB by using non-standard GCC compilation
5896 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5897 */
5898 if (cu->has_loclist && gcc_4_minor >= 5)
5899 symtab->locations_valid = 1;
5900
5901 if (gcc_4_minor >= 5)
5902 symtab->epilogue_unwind_valid = 1;
5903
5904 symtab->call_site_htab = cu->call_site_htab;
5905 }
5906
5907 if (dwarf2_per_objfile->using_index)
5908 per_cu->v.quick->symtab = symtab;
5909 else
5910 {
5911 struct partial_symtab *pst = per_cu->v.psymtab;
5912 pst->symtab = symtab;
5913 pst->readin = 1;
5914 }
5915
5916 /* Push it for inclusion processing later. */
5917 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5918
5919 do_cleanups (back_to);
5920
5921 if (dwarf2_read_debug)
5922 {
5923 fprintf_unfiltered (gdb_stdlog,
5924 "Done expanding symtab of %s at offset 0x%x\n",
5925 per_cu->is_debug_types ? "TU" : "CU",
5926 per_cu->offset.sect_off);
5927 }
5928 }
5929
5930 /* Process an imported unit DIE. */
5931
5932 static void
5933 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5934 {
5935 struct attribute *attr;
5936
5937 attr = dwarf2_attr (die, DW_AT_import, cu);
5938 if (attr != NULL)
5939 {
5940 struct dwarf2_per_cu_data *per_cu;
5941 struct symtab *imported_symtab;
5942 sect_offset offset;
5943
5944 offset = dwarf2_get_ref_die_offset (attr);
5945 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5946
5947 /* Queue the unit, if needed. */
5948 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5949 load_full_comp_unit (per_cu, cu->language);
5950
5951 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5952 per_cu);
5953 }
5954 }
5955
5956 /* Process a die and its children. */
5957
5958 static void
5959 process_die (struct die_info *die, struct dwarf2_cu *cu)
5960 {
5961 switch (die->tag)
5962 {
5963 case DW_TAG_padding:
5964 break;
5965 case DW_TAG_compile_unit:
5966 case DW_TAG_partial_unit:
5967 read_file_scope (die, cu);
5968 break;
5969 case DW_TAG_type_unit:
5970 read_type_unit_scope (die, cu);
5971 break;
5972 case DW_TAG_subprogram:
5973 case DW_TAG_inlined_subroutine:
5974 read_func_scope (die, cu);
5975 break;
5976 case DW_TAG_lexical_block:
5977 case DW_TAG_try_block:
5978 case DW_TAG_catch_block:
5979 read_lexical_block_scope (die, cu);
5980 break;
5981 case DW_TAG_GNU_call_site:
5982 read_call_site_scope (die, cu);
5983 break;
5984 case DW_TAG_class_type:
5985 case DW_TAG_interface_type:
5986 case DW_TAG_structure_type:
5987 case DW_TAG_union_type:
5988 process_structure_scope (die, cu);
5989 break;
5990 case DW_TAG_enumeration_type:
5991 process_enumeration_scope (die, cu);
5992 break;
5993
5994 /* These dies have a type, but processing them does not create
5995 a symbol or recurse to process the children. Therefore we can
5996 read them on-demand through read_type_die. */
5997 case DW_TAG_subroutine_type:
5998 case DW_TAG_set_type:
5999 case DW_TAG_array_type:
6000 case DW_TAG_pointer_type:
6001 case DW_TAG_ptr_to_member_type:
6002 case DW_TAG_reference_type:
6003 case DW_TAG_string_type:
6004 break;
6005
6006 case DW_TAG_base_type:
6007 case DW_TAG_subrange_type:
6008 case DW_TAG_typedef:
6009 /* Add a typedef symbol for the type definition, if it has a
6010 DW_AT_name. */
6011 new_symbol (die, read_type_die (die, cu), cu);
6012 break;
6013 case DW_TAG_common_block:
6014 read_common_block (die, cu);
6015 break;
6016 case DW_TAG_common_inclusion:
6017 break;
6018 case DW_TAG_namespace:
6019 processing_has_namespace_info = 1;
6020 read_namespace (die, cu);
6021 break;
6022 case DW_TAG_module:
6023 processing_has_namespace_info = 1;
6024 read_module (die, cu);
6025 break;
6026 case DW_TAG_imported_declaration:
6027 case DW_TAG_imported_module:
6028 processing_has_namespace_info = 1;
6029 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6030 || cu->language != language_fortran))
6031 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6032 dwarf_tag_name (die->tag));
6033 read_import_statement (die, cu);
6034 break;
6035
6036 case DW_TAG_imported_unit:
6037 process_imported_unit_die (die, cu);
6038 break;
6039
6040 default:
6041 new_symbol (die, NULL, cu);
6042 break;
6043 }
6044 }
6045
6046 /* A helper function for dwarf2_compute_name which determines whether DIE
6047 needs to have the name of the scope prepended to the name listed in the
6048 die. */
6049
6050 static int
6051 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6052 {
6053 struct attribute *attr;
6054
6055 switch (die->tag)
6056 {
6057 case DW_TAG_namespace:
6058 case DW_TAG_typedef:
6059 case DW_TAG_class_type:
6060 case DW_TAG_interface_type:
6061 case DW_TAG_structure_type:
6062 case DW_TAG_union_type:
6063 case DW_TAG_enumeration_type:
6064 case DW_TAG_enumerator:
6065 case DW_TAG_subprogram:
6066 case DW_TAG_member:
6067 return 1;
6068
6069 case DW_TAG_variable:
6070 case DW_TAG_constant:
6071 /* We only need to prefix "globally" visible variables. These include
6072 any variable marked with DW_AT_external or any variable that
6073 lives in a namespace. [Variables in anonymous namespaces
6074 require prefixing, but they are not DW_AT_external.] */
6075
6076 if (dwarf2_attr (die, DW_AT_specification, cu))
6077 {
6078 struct dwarf2_cu *spec_cu = cu;
6079
6080 return die_needs_namespace (die_specification (die, &spec_cu),
6081 spec_cu);
6082 }
6083
6084 attr = dwarf2_attr (die, DW_AT_external, cu);
6085 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6086 && die->parent->tag != DW_TAG_module)
6087 return 0;
6088 /* A variable in a lexical block of some kind does not need a
6089 namespace, even though in C++ such variables may be external
6090 and have a mangled name. */
6091 if (die->parent->tag == DW_TAG_lexical_block
6092 || die->parent->tag == DW_TAG_try_block
6093 || die->parent->tag == DW_TAG_catch_block
6094 || die->parent->tag == DW_TAG_subprogram)
6095 return 0;
6096 return 1;
6097
6098 default:
6099 return 0;
6100 }
6101 }
6102
6103 /* Retrieve the last character from a mem_file. */
6104
6105 static void
6106 do_ui_file_peek_last (void *object, const char *buffer, long length)
6107 {
6108 char *last_char_p = (char *) object;
6109
6110 if (length > 0)
6111 *last_char_p = buffer[length - 1];
6112 }
6113
6114 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6115 compute the physname for the object, which include a method's:
6116 - formal parameters (C++/Java),
6117 - receiver type (Go),
6118 - return type (Java).
6119
6120 The term "physname" is a bit confusing.
6121 For C++, for example, it is the demangled name.
6122 For Go, for example, it's the mangled name.
6123
6124 For Ada, return the DIE's linkage name rather than the fully qualified
6125 name. PHYSNAME is ignored..
6126
6127 The result is allocated on the objfile_obstack and canonicalized. */
6128
6129 static const char *
6130 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6131 int physname)
6132 {
6133 struct objfile *objfile = cu->objfile;
6134
6135 if (name == NULL)
6136 name = dwarf2_name (die, cu);
6137
6138 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6139 compute it by typename_concat inside GDB. */
6140 if (cu->language == language_ada
6141 || (cu->language == language_fortran && physname))
6142 {
6143 /* For Ada unit, we prefer the linkage name over the name, as
6144 the former contains the exported name, which the user expects
6145 to be able to reference. Ideally, we want the user to be able
6146 to reference this entity using either natural or linkage name,
6147 but we haven't started looking at this enhancement yet. */
6148 struct attribute *attr;
6149
6150 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6151 if (attr == NULL)
6152 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6153 if (attr && DW_STRING (attr))
6154 return DW_STRING (attr);
6155 }
6156
6157 /* These are the only languages we know how to qualify names in. */
6158 if (name != NULL
6159 && (cu->language == language_cplus || cu->language == language_java
6160 || cu->language == language_fortran))
6161 {
6162 if (die_needs_namespace (die, cu))
6163 {
6164 long length;
6165 const char *prefix;
6166 struct ui_file *buf;
6167
6168 prefix = determine_prefix (die, cu);
6169 buf = mem_fileopen ();
6170 if (*prefix != '\0')
6171 {
6172 char *prefixed_name = typename_concat (NULL, prefix, name,
6173 physname, cu);
6174
6175 fputs_unfiltered (prefixed_name, buf);
6176 xfree (prefixed_name);
6177 }
6178 else
6179 fputs_unfiltered (name, buf);
6180
6181 /* Template parameters may be specified in the DIE's DW_AT_name, or
6182 as children with DW_TAG_template_type_param or
6183 DW_TAG_value_type_param. If the latter, add them to the name
6184 here. If the name already has template parameters, then
6185 skip this step; some versions of GCC emit both, and
6186 it is more efficient to use the pre-computed name.
6187
6188 Something to keep in mind about this process: it is very
6189 unlikely, or in some cases downright impossible, to produce
6190 something that will match the mangled name of a function.
6191 If the definition of the function has the same debug info,
6192 we should be able to match up with it anyway. But fallbacks
6193 using the minimal symbol, for instance to find a method
6194 implemented in a stripped copy of libstdc++, will not work.
6195 If we do not have debug info for the definition, we will have to
6196 match them up some other way.
6197
6198 When we do name matching there is a related problem with function
6199 templates; two instantiated function templates are allowed to
6200 differ only by their return types, which we do not add here. */
6201
6202 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6203 {
6204 struct attribute *attr;
6205 struct die_info *child;
6206 int first = 1;
6207
6208 die->building_fullname = 1;
6209
6210 for (child = die->child; child != NULL; child = child->sibling)
6211 {
6212 struct type *type;
6213 LONGEST value;
6214 gdb_byte *bytes;
6215 struct dwarf2_locexpr_baton *baton;
6216 struct value *v;
6217
6218 if (child->tag != DW_TAG_template_type_param
6219 && child->tag != DW_TAG_template_value_param)
6220 continue;
6221
6222 if (first)
6223 {
6224 fputs_unfiltered ("<", buf);
6225 first = 0;
6226 }
6227 else
6228 fputs_unfiltered (", ", buf);
6229
6230 attr = dwarf2_attr (child, DW_AT_type, cu);
6231 if (attr == NULL)
6232 {
6233 complaint (&symfile_complaints,
6234 _("template parameter missing DW_AT_type"));
6235 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6236 continue;
6237 }
6238 type = die_type (child, cu);
6239
6240 if (child->tag == DW_TAG_template_type_param)
6241 {
6242 c_print_type (type, "", buf, -1, 0);
6243 continue;
6244 }
6245
6246 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6247 if (attr == NULL)
6248 {
6249 complaint (&symfile_complaints,
6250 _("template parameter missing "
6251 "DW_AT_const_value"));
6252 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6253 continue;
6254 }
6255
6256 dwarf2_const_value_attr (attr, type, name,
6257 &cu->comp_unit_obstack, cu,
6258 &value, &bytes, &baton);
6259
6260 if (TYPE_NOSIGN (type))
6261 /* GDB prints characters as NUMBER 'CHAR'. If that's
6262 changed, this can use value_print instead. */
6263 c_printchar (value, type, buf);
6264 else
6265 {
6266 struct value_print_options opts;
6267
6268 if (baton != NULL)
6269 v = dwarf2_evaluate_loc_desc (type, NULL,
6270 baton->data,
6271 baton->size,
6272 baton->per_cu);
6273 else if (bytes != NULL)
6274 {
6275 v = allocate_value (type);
6276 memcpy (value_contents_writeable (v), bytes,
6277 TYPE_LENGTH (type));
6278 }
6279 else
6280 v = value_from_longest (type, value);
6281
6282 /* Specify decimal so that we do not depend on
6283 the radix. */
6284 get_formatted_print_options (&opts, 'd');
6285 opts.raw = 1;
6286 value_print (v, buf, &opts);
6287 release_value (v);
6288 value_free (v);
6289 }
6290 }
6291
6292 die->building_fullname = 0;
6293
6294 if (!first)
6295 {
6296 /* Close the argument list, with a space if necessary
6297 (nested templates). */
6298 char last_char = '\0';
6299 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6300 if (last_char == '>')
6301 fputs_unfiltered (" >", buf);
6302 else
6303 fputs_unfiltered (">", buf);
6304 }
6305 }
6306
6307 /* For Java and C++ methods, append formal parameter type
6308 information, if PHYSNAME. */
6309
6310 if (physname && die->tag == DW_TAG_subprogram
6311 && (cu->language == language_cplus
6312 || cu->language == language_java))
6313 {
6314 struct type *type = read_type_die (die, cu);
6315
6316 c_type_print_args (type, buf, 1, cu->language);
6317
6318 if (cu->language == language_java)
6319 {
6320 /* For java, we must append the return type to method
6321 names. */
6322 if (die->tag == DW_TAG_subprogram)
6323 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6324 0, 0);
6325 }
6326 else if (cu->language == language_cplus)
6327 {
6328 /* Assume that an artificial first parameter is
6329 "this", but do not crash if it is not. RealView
6330 marks unnamed (and thus unused) parameters as
6331 artificial; there is no way to differentiate
6332 the two cases. */
6333 if (TYPE_NFIELDS (type) > 0
6334 && TYPE_FIELD_ARTIFICIAL (type, 0)
6335 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6336 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6337 0))))
6338 fputs_unfiltered (" const", buf);
6339 }
6340 }
6341
6342 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6343 &length);
6344 ui_file_delete (buf);
6345
6346 if (cu->language == language_cplus)
6347 {
6348 char *cname
6349 = dwarf2_canonicalize_name (name, cu,
6350 &objfile->objfile_obstack);
6351
6352 if (cname != NULL)
6353 name = cname;
6354 }
6355 }
6356 }
6357
6358 return name;
6359 }
6360
6361 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6362 If scope qualifiers are appropriate they will be added. The result
6363 will be allocated on the objfile_obstack, or NULL if the DIE does
6364 not have a name. NAME may either be from a previous call to
6365 dwarf2_name or NULL.
6366
6367 The output string will be canonicalized (if C++/Java). */
6368
6369 static const char *
6370 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6371 {
6372 return dwarf2_compute_name (name, die, cu, 0);
6373 }
6374
6375 /* Construct a physname for the given DIE in CU. NAME may either be
6376 from a previous call to dwarf2_name or NULL. The result will be
6377 allocated on the objfile_objstack or NULL if the DIE does not have a
6378 name.
6379
6380 The output string will be canonicalized (if C++/Java). */
6381
6382 static const char *
6383 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6384 {
6385 struct objfile *objfile = cu->objfile;
6386 struct attribute *attr;
6387 const char *retval, *mangled = NULL, *canon = NULL;
6388 struct cleanup *back_to;
6389 int need_copy = 1;
6390
6391 /* In this case dwarf2_compute_name is just a shortcut not building anything
6392 on its own. */
6393 if (!die_needs_namespace (die, cu))
6394 return dwarf2_compute_name (name, die, cu, 1);
6395
6396 back_to = make_cleanup (null_cleanup, NULL);
6397
6398 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6399 if (!attr)
6400 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6401
6402 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6403 has computed. */
6404 if (attr && DW_STRING (attr))
6405 {
6406 char *demangled;
6407
6408 mangled = DW_STRING (attr);
6409
6410 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6411 type. It is easier for GDB users to search for such functions as
6412 `name(params)' than `long name(params)'. In such case the minimal
6413 symbol names do not match the full symbol names but for template
6414 functions there is never a need to look up their definition from their
6415 declaration so the only disadvantage remains the minimal symbol
6416 variant `long name(params)' does not have the proper inferior type.
6417 */
6418
6419 if (cu->language == language_go)
6420 {
6421 /* This is a lie, but we already lie to the caller new_symbol_full.
6422 new_symbol_full assumes we return the mangled name.
6423 This just undoes that lie until things are cleaned up. */
6424 demangled = NULL;
6425 }
6426 else
6427 {
6428 demangled = cplus_demangle (mangled,
6429 (DMGL_PARAMS | DMGL_ANSI
6430 | (cu->language == language_java
6431 ? DMGL_JAVA | DMGL_RET_POSTFIX
6432 : DMGL_RET_DROP)));
6433 }
6434 if (demangled)
6435 {
6436 make_cleanup (xfree, demangled);
6437 canon = demangled;
6438 }
6439 else
6440 {
6441 canon = mangled;
6442 need_copy = 0;
6443 }
6444 }
6445
6446 if (canon == NULL || check_physname)
6447 {
6448 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6449
6450 if (canon != NULL && strcmp (physname, canon) != 0)
6451 {
6452 /* It may not mean a bug in GDB. The compiler could also
6453 compute DW_AT_linkage_name incorrectly. But in such case
6454 GDB would need to be bug-to-bug compatible. */
6455
6456 complaint (&symfile_complaints,
6457 _("Computed physname <%s> does not match demangled <%s> "
6458 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6459 physname, canon, mangled, die->offset.sect_off, objfile->name);
6460
6461 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6462 is available here - over computed PHYSNAME. It is safer
6463 against both buggy GDB and buggy compilers. */
6464
6465 retval = canon;
6466 }
6467 else
6468 {
6469 retval = physname;
6470 need_copy = 0;
6471 }
6472 }
6473 else
6474 retval = canon;
6475
6476 if (need_copy)
6477 retval = obsavestring (retval, strlen (retval),
6478 &objfile->objfile_obstack);
6479
6480 do_cleanups (back_to);
6481 return retval;
6482 }
6483
6484 /* Read the import statement specified by the given die and record it. */
6485
6486 static void
6487 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6488 {
6489 struct objfile *objfile = cu->objfile;
6490 struct attribute *import_attr;
6491 struct die_info *imported_die, *child_die;
6492 struct dwarf2_cu *imported_cu;
6493 const char *imported_name;
6494 const char *imported_name_prefix;
6495 const char *canonical_name;
6496 const char *import_alias;
6497 const char *imported_declaration = NULL;
6498 const char *import_prefix;
6499 VEC (const_char_ptr) *excludes = NULL;
6500 struct cleanup *cleanups;
6501
6502 char *temp;
6503
6504 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6505 if (import_attr == NULL)
6506 {
6507 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6508 dwarf_tag_name (die->tag));
6509 return;
6510 }
6511
6512 imported_cu = cu;
6513 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6514 imported_name = dwarf2_name (imported_die, imported_cu);
6515 if (imported_name == NULL)
6516 {
6517 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6518
6519 The import in the following code:
6520 namespace A
6521 {
6522 typedef int B;
6523 }
6524
6525 int main ()
6526 {
6527 using A::B;
6528 B b;
6529 return b;
6530 }
6531
6532 ...
6533 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6534 <52> DW_AT_decl_file : 1
6535 <53> DW_AT_decl_line : 6
6536 <54> DW_AT_import : <0x75>
6537 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6538 <59> DW_AT_name : B
6539 <5b> DW_AT_decl_file : 1
6540 <5c> DW_AT_decl_line : 2
6541 <5d> DW_AT_type : <0x6e>
6542 ...
6543 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6544 <76> DW_AT_byte_size : 4
6545 <77> DW_AT_encoding : 5 (signed)
6546
6547 imports the wrong die ( 0x75 instead of 0x58 ).
6548 This case will be ignored until the gcc bug is fixed. */
6549 return;
6550 }
6551
6552 /* Figure out the local name after import. */
6553 import_alias = dwarf2_name (die, cu);
6554
6555 /* Figure out where the statement is being imported to. */
6556 import_prefix = determine_prefix (die, cu);
6557
6558 /* Figure out what the scope of the imported die is and prepend it
6559 to the name of the imported die. */
6560 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6561
6562 if (imported_die->tag != DW_TAG_namespace
6563 && imported_die->tag != DW_TAG_module)
6564 {
6565 imported_declaration = imported_name;
6566 canonical_name = imported_name_prefix;
6567 }
6568 else if (strlen (imported_name_prefix) > 0)
6569 {
6570 temp = alloca (strlen (imported_name_prefix)
6571 + 2 + strlen (imported_name) + 1);
6572 strcpy (temp, imported_name_prefix);
6573 strcat (temp, "::");
6574 strcat (temp, imported_name);
6575 canonical_name = temp;
6576 }
6577 else
6578 canonical_name = imported_name;
6579
6580 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6581
6582 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6583 for (child_die = die->child; child_die && child_die->tag;
6584 child_die = sibling_die (child_die))
6585 {
6586 /* DWARF-4: A Fortran use statement with a “rename list” may be
6587 represented by an imported module entry with an import attribute
6588 referring to the module and owned entries corresponding to those
6589 entities that are renamed as part of being imported. */
6590
6591 if (child_die->tag != DW_TAG_imported_declaration)
6592 {
6593 complaint (&symfile_complaints,
6594 _("child DW_TAG_imported_declaration expected "
6595 "- DIE at 0x%x [in module %s]"),
6596 child_die->offset.sect_off, objfile->name);
6597 continue;
6598 }
6599
6600 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6601 if (import_attr == NULL)
6602 {
6603 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6604 dwarf_tag_name (child_die->tag));
6605 continue;
6606 }
6607
6608 imported_cu = cu;
6609 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6610 &imported_cu);
6611 imported_name = dwarf2_name (imported_die, imported_cu);
6612 if (imported_name == NULL)
6613 {
6614 complaint (&symfile_complaints,
6615 _("child DW_TAG_imported_declaration has unknown "
6616 "imported name - DIE at 0x%x [in module %s]"),
6617 child_die->offset.sect_off, objfile->name);
6618 continue;
6619 }
6620
6621 VEC_safe_push (const_char_ptr, excludes, imported_name);
6622
6623 process_die (child_die, cu);
6624 }
6625
6626 cp_add_using_directive (import_prefix,
6627 canonical_name,
6628 import_alias,
6629 imported_declaration,
6630 excludes,
6631 &objfile->objfile_obstack);
6632
6633 do_cleanups (cleanups);
6634 }
6635
6636 /* Cleanup function for read_file_scope. */
6637
6638 static void
6639 free_cu_line_header (void *arg)
6640 {
6641 struct dwarf2_cu *cu = arg;
6642
6643 free_line_header (cu->line_header);
6644 cu->line_header = NULL;
6645 }
6646
6647 static void
6648 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6649 char **name, char **comp_dir)
6650 {
6651 struct attribute *attr;
6652
6653 *name = NULL;
6654 *comp_dir = NULL;
6655
6656 /* Find the filename. Do not use dwarf2_name here, since the filename
6657 is not a source language identifier. */
6658 attr = dwarf2_attr (die, DW_AT_name, cu);
6659 if (attr)
6660 {
6661 *name = DW_STRING (attr);
6662 }
6663
6664 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6665 if (attr)
6666 *comp_dir = DW_STRING (attr);
6667 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6668 {
6669 *comp_dir = ldirname (*name);
6670 if (*comp_dir != NULL)
6671 make_cleanup (xfree, *comp_dir);
6672 }
6673 if (*comp_dir != NULL)
6674 {
6675 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6676 directory, get rid of it. */
6677 char *cp = strchr (*comp_dir, ':');
6678
6679 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6680 *comp_dir = cp + 1;
6681 }
6682
6683 if (*name == NULL)
6684 *name = "<unknown>";
6685 }
6686
6687 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6688 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6689 COMP_DIR is the compilation directory.
6690 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6691
6692 static void
6693 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6694 const char *comp_dir, int want_line_info)
6695 {
6696 struct attribute *attr;
6697
6698 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6699 if (attr)
6700 {
6701 unsigned int line_offset = DW_UNSND (attr);
6702 struct line_header *line_header
6703 = dwarf_decode_line_header (line_offset, cu);
6704
6705 if (line_header)
6706 {
6707 cu->line_header = line_header;
6708 make_cleanup (free_cu_line_header, cu);
6709 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6710 }
6711 }
6712 }
6713
6714 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6715
6716 static void
6717 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6718 {
6719 struct objfile *objfile = dwarf2_per_objfile->objfile;
6720 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6721 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6722 CORE_ADDR highpc = ((CORE_ADDR) 0);
6723 struct attribute *attr;
6724 char *name = NULL;
6725 char *comp_dir = NULL;
6726 struct die_info *child_die;
6727 bfd *abfd = objfile->obfd;
6728 CORE_ADDR baseaddr;
6729
6730 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6731
6732 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6733
6734 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6735 from finish_block. */
6736 if (lowpc == ((CORE_ADDR) -1))
6737 lowpc = highpc;
6738 lowpc += baseaddr;
6739 highpc += baseaddr;
6740
6741 find_file_and_directory (die, cu, &name, &comp_dir);
6742
6743 prepare_one_comp_unit (cu, die, cu->language);
6744
6745 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6746 standardised yet. As a workaround for the language detection we fall
6747 back to the DW_AT_producer string. */
6748 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6749 cu->language = language_opencl;
6750
6751 /* Similar hack for Go. */
6752 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6753 set_cu_language (DW_LANG_Go, cu);
6754
6755 /* We assume that we're processing GCC output. */
6756 processing_gcc_compilation = 2;
6757
6758 processing_has_namespace_info = 0;
6759
6760 start_symtab (name, comp_dir, lowpc);
6761 record_debugformat ("DWARF 2");
6762 record_producer (cu->producer);
6763
6764 /* Decode line number information if present. We do this before
6765 processing child DIEs, so that the line header table is available
6766 for DW_AT_decl_file. */
6767 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6768
6769 /* Process all dies in compilation unit. */
6770 if (die->child != NULL)
6771 {
6772 child_die = die->child;
6773 while (child_die && child_die->tag)
6774 {
6775 process_die (child_die, cu);
6776 child_die = sibling_die (child_die);
6777 }
6778 }
6779
6780 /* Decode macro information, if present. Dwarf 2 macro information
6781 refers to information in the line number info statement program
6782 header, so we can only read it if we've read the header
6783 successfully. */
6784 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6785 if (attr && cu->line_header)
6786 {
6787 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6788 complaint (&symfile_complaints,
6789 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6790
6791 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
6792 }
6793 else
6794 {
6795 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6796 if (attr && cu->line_header)
6797 {
6798 unsigned int macro_offset = DW_UNSND (attr);
6799
6800 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
6801 }
6802 }
6803
6804 do_cleanups (back_to);
6805 }
6806
6807 /* Process DW_TAG_type_unit.
6808 For TUs we want to skip the first top level sibling if it's not the
6809 actual type being defined by this TU. In this case the first top
6810 level sibling is there to provide context only. */
6811
6812 static void
6813 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6814 {
6815 struct objfile *objfile = cu->objfile;
6816 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6817 CORE_ADDR lowpc;
6818 struct attribute *attr;
6819 char *name = NULL;
6820 char *comp_dir = NULL;
6821 struct die_info *child_die;
6822 bfd *abfd = objfile->obfd;
6823
6824 /* start_symtab needs a low pc, but we don't really have one.
6825 Do what read_file_scope would do in the absence of such info. */
6826 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6827
6828 /* Find the filename. Do not use dwarf2_name here, since the filename
6829 is not a source language identifier. */
6830 attr = dwarf2_attr (die, DW_AT_name, cu);
6831 if (attr)
6832 name = DW_STRING (attr);
6833
6834 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6835 if (attr)
6836 comp_dir = DW_STRING (attr);
6837 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6838 {
6839 comp_dir = ldirname (name);
6840 if (comp_dir != NULL)
6841 make_cleanup (xfree, comp_dir);
6842 }
6843
6844 if (name == NULL)
6845 name = "<unknown>";
6846
6847 prepare_one_comp_unit (cu, die, language_minimal);
6848
6849 /* We assume that we're processing GCC output. */
6850 processing_gcc_compilation = 2;
6851
6852 processing_has_namespace_info = 0;
6853
6854 start_symtab (name, comp_dir, lowpc);
6855 record_debugformat ("DWARF 2");
6856 record_producer (cu->producer);
6857
6858 /* Decode line number information if present. We do this before
6859 processing child DIEs, so that the line header table is available
6860 for DW_AT_decl_file.
6861 We don't need the pc/line-number mapping for type units. */
6862 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6863
6864 /* Process the dies in the type unit. */
6865 if (die->child == NULL)
6866 {
6867 dump_die_for_error (die);
6868 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6869 bfd_get_filename (abfd));
6870 }
6871
6872 child_die = die->child;
6873
6874 while (child_die && child_die->tag)
6875 {
6876 process_die (child_die, cu);
6877
6878 child_die = sibling_die (child_die);
6879 }
6880
6881 do_cleanups (back_to);
6882 }
6883 \f
6884 /* DWO files. */
6885
6886 static hashval_t
6887 hash_dwo_file (const void *item)
6888 {
6889 const struct dwo_file *dwo_file = item;
6890
6891 return htab_hash_string (dwo_file->dwo_name);
6892 }
6893
6894 static int
6895 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6896 {
6897 const struct dwo_file *lhs = item_lhs;
6898 const struct dwo_file *rhs = item_rhs;
6899
6900 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6901 }
6902
6903 /* Allocate a hash table for DWO files. */
6904
6905 static htab_t
6906 allocate_dwo_file_hash_table (void)
6907 {
6908 struct objfile *objfile = dwarf2_per_objfile->objfile;
6909
6910 return htab_create_alloc_ex (41,
6911 hash_dwo_file,
6912 eq_dwo_file,
6913 NULL,
6914 &objfile->objfile_obstack,
6915 hashtab_obstack_allocate,
6916 dummy_obstack_deallocate);
6917 }
6918
6919 static hashval_t
6920 hash_dwo_unit (const void *item)
6921 {
6922 const struct dwo_unit *dwo_unit = item;
6923
6924 /* This drops the top 32 bits of the id, but is ok for a hash. */
6925 return dwo_unit->signature;
6926 }
6927
6928 static int
6929 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6930 {
6931 const struct dwo_unit *lhs = item_lhs;
6932 const struct dwo_unit *rhs = item_rhs;
6933
6934 /* The signature is assumed to be unique within the DWO file.
6935 So while object file CU dwo_id's always have the value zero,
6936 that's OK, assuming each object file DWO file has only one CU,
6937 and that's the rule for now. */
6938 return lhs->signature == rhs->signature;
6939 }
6940
6941 /* Allocate a hash table for DWO CUs,TUs.
6942 There is one of these tables for each of CUs,TUs for each DWO file. */
6943
6944 static htab_t
6945 allocate_dwo_unit_table (struct objfile *objfile)
6946 {
6947 /* Start out with a pretty small number.
6948 Generally DWO files contain only one CU and maybe some TUs. */
6949 return htab_create_alloc_ex (3,
6950 hash_dwo_unit,
6951 eq_dwo_unit,
6952 NULL,
6953 &objfile->objfile_obstack,
6954 hashtab_obstack_allocate,
6955 dummy_obstack_deallocate);
6956 }
6957
6958 /* This function is mapped across the sections and remembers the offset and
6959 size of each of the DWO debugging sections we are interested in. */
6960
6961 static void
6962 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6963 {
6964 struct dwo_file *dwo_file = dwo_file_ptr;
6965 const struct dwo_section_names *names = &dwo_section_names;
6966
6967 if (section_is_p (sectp->name, &names->abbrev_dwo))
6968 {
6969 dwo_file->sections.abbrev.asection = sectp;
6970 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6971 }
6972 else if (section_is_p (sectp->name, &names->info_dwo))
6973 {
6974 dwo_file->sections.info.asection = sectp;
6975 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6976 }
6977 else if (section_is_p (sectp->name, &names->line_dwo))
6978 {
6979 dwo_file->sections.line.asection = sectp;
6980 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6981 }
6982 else if (section_is_p (sectp->name, &names->loc_dwo))
6983 {
6984 dwo_file->sections.loc.asection = sectp;
6985 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6986 }
6987 else if (section_is_p (sectp->name, &names->macinfo_dwo))
6988 {
6989 dwo_file->sections.macinfo.asection = sectp;
6990 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
6991 }
6992 else if (section_is_p (sectp->name, &names->macro_dwo))
6993 {
6994 dwo_file->sections.macro.asection = sectp;
6995 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
6996 }
6997 else if (section_is_p (sectp->name, &names->str_dwo))
6998 {
6999 dwo_file->sections.str.asection = sectp;
7000 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7001 }
7002 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7003 {
7004 dwo_file->sections.str_offsets.asection = sectp;
7005 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7006 }
7007 else if (section_is_p (sectp->name, &names->types_dwo))
7008 {
7009 struct dwarf2_section_info type_section;
7010
7011 memset (&type_section, 0, sizeof (type_section));
7012 type_section.asection = sectp;
7013 type_section.size = bfd_get_section_size (sectp);
7014 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7015 &type_section);
7016 }
7017 }
7018
7019 /* Structure used to pass data to create_debug_info_hash_table_reader. */
7020
7021 struct create_dwo_info_table_data
7022 {
7023 struct dwo_file *dwo_file;
7024 htab_t cu_htab;
7025 };
7026
7027 /* die_reader_func for create_debug_info_hash_table. */
7028
7029 static void
7030 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7031 gdb_byte *info_ptr,
7032 struct die_info *comp_unit_die,
7033 int has_children,
7034 void *datap)
7035 {
7036 struct dwarf2_cu *cu = reader->cu;
7037 struct objfile *objfile = dwarf2_per_objfile->objfile;
7038 sect_offset offset = cu->per_cu->offset;
7039 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7040 struct create_dwo_info_table_data *data = datap;
7041 struct dwo_file *dwo_file = data->dwo_file;
7042 htab_t cu_htab = data->cu_htab;
7043 void **slot;
7044 struct attribute *attr;
7045 struct dwo_unit *dwo_unit;
7046
7047 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7048 if (attr == NULL)
7049 {
7050 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7051 " its dwo_id [in module %s]"),
7052 offset.sect_off, dwo_file->dwo_name);
7053 return;
7054 }
7055
7056 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7057 dwo_unit->dwo_file = dwo_file;
7058 dwo_unit->signature = DW_UNSND (attr);
7059 dwo_unit->info_or_types_section = section;
7060 dwo_unit->offset = offset;
7061 dwo_unit->length = cu->per_cu->length;
7062
7063 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7064 gdb_assert (slot != NULL);
7065 if (*slot != NULL)
7066 {
7067 const struct dwo_unit *dup_dwo_unit = *slot;
7068
7069 complaint (&symfile_complaints,
7070 _("debug entry at offset 0x%x is duplicate to the entry at"
7071 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7072 offset.sect_off, dup_dwo_unit->offset.sect_off,
7073 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7074 dwo_file->dwo_name);
7075 }
7076 else
7077 *slot = dwo_unit;
7078
7079 if (dwarf2_die_debug)
7080 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7081 offset.sect_off,
7082 phex (dwo_unit->signature,
7083 sizeof (dwo_unit->signature)));
7084 }
7085
7086 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7087
7088 static htab_t
7089 create_debug_info_hash_table (struct dwo_file *dwo_file)
7090 {
7091 struct objfile *objfile = dwarf2_per_objfile->objfile;
7092 struct dwarf2_section_info *section = &dwo_file->sections.info;
7093 bfd *abfd;
7094 htab_t cu_htab;
7095 gdb_byte *info_ptr, *end_ptr;
7096 struct create_dwo_info_table_data create_dwo_info_table_data;
7097
7098 dwarf2_read_section (objfile, section);
7099 info_ptr = section->buffer;
7100
7101 if (info_ptr == NULL)
7102 return NULL;
7103
7104 /* We can't set abfd until now because the section may be empty or
7105 not present, in which case section->asection will be NULL. */
7106 abfd = section->asection->owner;
7107
7108 if (dwarf2_die_debug)
7109 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7110 bfd_get_filename (abfd));
7111
7112 cu_htab = allocate_dwo_unit_table (objfile);
7113
7114 create_dwo_info_table_data.dwo_file = dwo_file;
7115 create_dwo_info_table_data.cu_htab = cu_htab;
7116
7117 end_ptr = info_ptr + section->size;
7118 while (info_ptr < end_ptr)
7119 {
7120 struct dwarf2_per_cu_data per_cu;
7121
7122 memset (&per_cu, 0, sizeof (per_cu));
7123 per_cu.objfile = objfile;
7124 per_cu.is_debug_types = 0;
7125 per_cu.offset.sect_off = info_ptr - section->buffer;
7126 per_cu.info_or_types_section = section;
7127
7128 init_cutu_and_read_dies_no_follow (&per_cu,
7129 &dwo_file->sections.abbrev,
7130 dwo_file,
7131 create_debug_info_hash_table_reader,
7132 &create_dwo_info_table_data);
7133
7134 info_ptr += per_cu.length;
7135 }
7136
7137 return cu_htab;
7138 }
7139
7140 /* Subroutine of open_dwo_file to simplify it.
7141 Open the file specified by FILE_NAME and hand it off to BFD for
7142 preliminary analysis. Return a newly initialized bfd *, which
7143 includes a canonicalized copy of FILE_NAME.
7144 In case of trouble, return NULL.
7145 NOTE: This function is derived from symfile_bfd_open. */
7146
7147 static bfd *
7148 try_open_dwo_file (const char *file_name)
7149 {
7150 bfd *sym_bfd;
7151 int desc;
7152 char *absolute_name;
7153
7154 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7155 O_RDONLY | O_BINARY, &absolute_name);
7156 if (desc < 0)
7157 return NULL;
7158
7159 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7160 if (!sym_bfd)
7161 {
7162 xfree (absolute_name);
7163 return NULL;
7164 }
7165 bfd_set_cacheable (sym_bfd, 1);
7166
7167 if (!bfd_check_format (sym_bfd, bfd_object))
7168 {
7169 bfd_close (sym_bfd); /* This also closes desc. */
7170 xfree (absolute_name);
7171 return NULL;
7172 }
7173
7174 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7175 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7176
7177 return sym_bfd;
7178 }
7179
7180 /* Try to open DWO file DWO_NAME.
7181 COMP_DIR is the DW_AT_comp_dir attribute.
7182 The result is the bfd handle of the file.
7183 If there is a problem finding or opening the file, return NULL.
7184 Upon success, the canonicalized path of the file is stored in the bfd,
7185 same as symfile_bfd_open. */
7186
7187 static bfd *
7188 open_dwo_file (const char *dwo_name, const char *comp_dir)
7189 {
7190 bfd *abfd;
7191
7192 if (IS_ABSOLUTE_PATH (dwo_name))
7193 return try_open_dwo_file (dwo_name);
7194
7195 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7196
7197 if (comp_dir != NULL)
7198 {
7199 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7200
7201 /* NOTE: If comp_dir is a relative path, this will also try the
7202 search path, which seems useful. */
7203 abfd = try_open_dwo_file (path_to_try);
7204 xfree (path_to_try);
7205 if (abfd != NULL)
7206 return abfd;
7207 }
7208
7209 /* That didn't work, try debug-file-directory, which, despite its name,
7210 is a list of paths. */
7211
7212 if (*debug_file_directory == '\0')
7213 return NULL;
7214
7215 return try_open_dwo_file (dwo_name);
7216 }
7217
7218 /* Initialize the use of the DWO file specified by DWO_NAME. */
7219
7220 static struct dwo_file *
7221 init_dwo_file (const char *dwo_name, const char *comp_dir)
7222 {
7223 struct objfile *objfile = dwarf2_per_objfile->objfile;
7224 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7225 struct dwo_file);
7226 bfd *abfd;
7227 struct cleanup *cleanups;
7228
7229 if (dwarf2_die_debug)
7230 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7231
7232 abfd = open_dwo_file (dwo_name, comp_dir);
7233 if (abfd == NULL)
7234 return NULL;
7235 dwo_file->dwo_name = dwo_name;
7236 dwo_file->dwo_bfd = abfd;
7237
7238 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7239
7240 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7241
7242 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7243
7244 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7245 dwo_file->sections.types);
7246
7247 discard_cleanups (cleanups);
7248
7249 return dwo_file;
7250 }
7251
7252 /* Lookup DWO file DWO_NAME. */
7253
7254 static struct dwo_file *
7255 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7256 {
7257 struct dwo_file *dwo_file;
7258 struct dwo_file find_entry;
7259 void **slot;
7260
7261 if (dwarf2_per_objfile->dwo_files == NULL)
7262 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7263
7264 /* Have we already seen this DWO file? */
7265 find_entry.dwo_name = dwo_name;
7266 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7267
7268 /* If not, read it in and build a table of the DWOs it contains. */
7269 if (*slot == NULL)
7270 *slot = init_dwo_file (dwo_name, comp_dir);
7271
7272 /* NOTE: This will be NULL if unable to open the file. */
7273 dwo_file = *slot;
7274
7275 return dwo_file;
7276 }
7277
7278 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7279 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7280 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7281 nomenclature as TUs).
7282 The result is a pointer to the dwo_unit object or NULL if we didn't find it
7283 (dwo_id mismatch or couldn't find the DWO file). */
7284
7285 static struct dwo_unit *
7286 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7287 char *dwo_name, const char *comp_dir,
7288 ULONGEST signature)
7289 {
7290 struct objfile *objfile = dwarf2_per_objfile->objfile;
7291 struct dwo_file *dwo_file;
7292
7293 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7294 if (dwo_file == NULL)
7295 return NULL;
7296
7297 /* Look up the DWO using its signature(dwo_id). */
7298
7299 if (dwo_file->cus != NULL)
7300 {
7301 struct dwo_unit find_dwo_cu, *dwo_cu;
7302
7303 find_dwo_cu.signature = signature;
7304 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7305
7306 if (dwo_cu != NULL)
7307 return dwo_cu;
7308 }
7309
7310 /* We didn't find it. This must mean a dwo_id mismatch. */
7311
7312 complaint (&symfile_complaints,
7313 _("Could not find DWO CU referenced by CU at offset 0x%x"
7314 " [in module %s]"),
7315 this_cu->offset.sect_off, objfile->name);
7316 return NULL;
7317 }
7318
7319 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7320 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7321 The result is a pointer to the dwo_unit object or NULL if we didn't find it
7322 (dwo_id mismatch or couldn't find the DWO file). */
7323
7324 static struct dwo_unit *
7325 lookup_dwo_type_unit (struct signatured_type *this_tu,
7326 char *dwo_name, const char *comp_dir)
7327 {
7328 struct objfile *objfile = dwarf2_per_objfile->objfile;
7329 struct dwo_file *dwo_file;
7330
7331 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7332 if (dwo_file == NULL)
7333 return NULL;
7334
7335 /* Look up the DWO using its signature(dwo_id). */
7336
7337 if (dwo_file->tus != NULL)
7338 {
7339 struct dwo_unit find_dwo_tu, *dwo_tu;
7340
7341 find_dwo_tu.signature = this_tu->signature;
7342 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7343
7344 if (dwo_tu != NULL)
7345 return dwo_tu;
7346 }
7347
7348 /* We didn't find it. This must mean a dwo_id mismatch. */
7349
7350 complaint (&symfile_complaints,
7351 _("Could not find DWO TU referenced by TU at offset 0x%x"
7352 " [in module %s]"),
7353 this_tu->per_cu.offset.sect_off, objfile->name);
7354 return NULL;
7355 }
7356
7357 /* Free all resources associated with DWO_FILE.
7358 Close the DWO file and munmap the sections.
7359 All memory should be on the objfile obstack. */
7360
7361 static void
7362 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7363 {
7364 int ix;
7365 struct dwarf2_section_info *section;
7366
7367 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7368 bfd_close (dwo_file->dwo_bfd);
7369
7370 munmap_section_buffer (&dwo_file->sections.abbrev);
7371 munmap_section_buffer (&dwo_file->sections.info);
7372 munmap_section_buffer (&dwo_file->sections.line);
7373 munmap_section_buffer (&dwo_file->sections.loc);
7374 munmap_section_buffer (&dwo_file->sections.str);
7375 munmap_section_buffer (&dwo_file->sections.str_offsets);
7376
7377 for (ix = 0;
7378 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7379 ix, section);
7380 ++ix)
7381 munmap_section_buffer (section);
7382
7383 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7384 }
7385
7386 /* Wrapper for free_dwo_file for use in cleanups. */
7387
7388 static void
7389 free_dwo_file_cleanup (void *arg)
7390 {
7391 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7392 struct objfile *objfile = dwarf2_per_objfile->objfile;
7393
7394 free_dwo_file (dwo_file, objfile);
7395 }
7396
7397 /* Traversal function for free_dwo_files. */
7398
7399 static int
7400 free_dwo_file_from_slot (void **slot, void *info)
7401 {
7402 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7403 struct objfile *objfile = (struct objfile *) info;
7404
7405 free_dwo_file (dwo_file, objfile);
7406
7407 return 1;
7408 }
7409
7410 /* Free all resources associated with DWO_FILES. */
7411
7412 static void
7413 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7414 {
7415 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7416 }
7417 \f
7418 /* Read in various DIEs. */
7419
7420 /* qsort helper for inherit_abstract_dies. */
7421
7422 static int
7423 unsigned_int_compar (const void *ap, const void *bp)
7424 {
7425 unsigned int a = *(unsigned int *) ap;
7426 unsigned int b = *(unsigned int *) bp;
7427
7428 return (a > b) - (b > a);
7429 }
7430
7431 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7432 Inherit only the children of the DW_AT_abstract_origin DIE not being
7433 already referenced by DW_AT_abstract_origin from the children of the
7434 current DIE. */
7435
7436 static void
7437 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7438 {
7439 struct die_info *child_die;
7440 unsigned die_children_count;
7441 /* CU offsets which were referenced by children of the current DIE. */
7442 sect_offset *offsets;
7443 sect_offset *offsets_end, *offsetp;
7444 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7445 struct die_info *origin_die;
7446 /* Iterator of the ORIGIN_DIE children. */
7447 struct die_info *origin_child_die;
7448 struct cleanup *cleanups;
7449 struct attribute *attr;
7450 struct dwarf2_cu *origin_cu;
7451 struct pending **origin_previous_list_in_scope;
7452
7453 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7454 if (!attr)
7455 return;
7456
7457 /* Note that following die references may follow to a die in a
7458 different cu. */
7459
7460 origin_cu = cu;
7461 origin_die = follow_die_ref (die, attr, &origin_cu);
7462
7463 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7464 symbols in. */
7465 origin_previous_list_in_scope = origin_cu->list_in_scope;
7466 origin_cu->list_in_scope = cu->list_in_scope;
7467
7468 if (die->tag != origin_die->tag
7469 && !(die->tag == DW_TAG_inlined_subroutine
7470 && origin_die->tag == DW_TAG_subprogram))
7471 complaint (&symfile_complaints,
7472 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7473 die->offset.sect_off, origin_die->offset.sect_off);
7474
7475 child_die = die->child;
7476 die_children_count = 0;
7477 while (child_die && child_die->tag)
7478 {
7479 child_die = sibling_die (child_die);
7480 die_children_count++;
7481 }
7482 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7483 cleanups = make_cleanup (xfree, offsets);
7484
7485 offsets_end = offsets;
7486 child_die = die->child;
7487 while (child_die && child_die->tag)
7488 {
7489 /* For each CHILD_DIE, find the corresponding child of
7490 ORIGIN_DIE. If there is more than one layer of
7491 DW_AT_abstract_origin, follow them all; there shouldn't be,
7492 but GCC versions at least through 4.4 generate this (GCC PR
7493 40573). */
7494 struct die_info *child_origin_die = child_die;
7495 struct dwarf2_cu *child_origin_cu = cu;
7496
7497 while (1)
7498 {
7499 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7500 child_origin_cu);
7501 if (attr == NULL)
7502 break;
7503 child_origin_die = follow_die_ref (child_origin_die, attr,
7504 &child_origin_cu);
7505 }
7506
7507 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7508 counterpart may exist. */
7509 if (child_origin_die != child_die)
7510 {
7511 if (child_die->tag != child_origin_die->tag
7512 && !(child_die->tag == DW_TAG_inlined_subroutine
7513 && child_origin_die->tag == DW_TAG_subprogram))
7514 complaint (&symfile_complaints,
7515 _("Child DIE 0x%x and its abstract origin 0x%x have "
7516 "different tags"), child_die->offset.sect_off,
7517 child_origin_die->offset.sect_off);
7518 if (child_origin_die->parent != origin_die)
7519 complaint (&symfile_complaints,
7520 _("Child DIE 0x%x and its abstract origin 0x%x have "
7521 "different parents"), child_die->offset.sect_off,
7522 child_origin_die->offset.sect_off);
7523 else
7524 *offsets_end++ = child_origin_die->offset;
7525 }
7526 child_die = sibling_die (child_die);
7527 }
7528 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7529 unsigned_int_compar);
7530 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7531 if (offsetp[-1].sect_off == offsetp->sect_off)
7532 complaint (&symfile_complaints,
7533 _("Multiple children of DIE 0x%x refer "
7534 "to DIE 0x%x as their abstract origin"),
7535 die->offset.sect_off, offsetp->sect_off);
7536
7537 offsetp = offsets;
7538 origin_child_die = origin_die->child;
7539 while (origin_child_die && origin_child_die->tag)
7540 {
7541 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7542 while (offsetp < offsets_end
7543 && offsetp->sect_off < origin_child_die->offset.sect_off)
7544 offsetp++;
7545 if (offsetp >= offsets_end
7546 || offsetp->sect_off > origin_child_die->offset.sect_off)
7547 {
7548 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7549 process_die (origin_child_die, origin_cu);
7550 }
7551 origin_child_die = sibling_die (origin_child_die);
7552 }
7553 origin_cu->list_in_scope = origin_previous_list_in_scope;
7554
7555 do_cleanups (cleanups);
7556 }
7557
7558 static void
7559 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7560 {
7561 struct objfile *objfile = cu->objfile;
7562 struct context_stack *new;
7563 CORE_ADDR lowpc;
7564 CORE_ADDR highpc;
7565 struct die_info *child_die;
7566 struct attribute *attr, *call_line, *call_file;
7567 char *name;
7568 CORE_ADDR baseaddr;
7569 struct block *block;
7570 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7571 VEC (symbolp) *template_args = NULL;
7572 struct template_symbol *templ_func = NULL;
7573
7574 if (inlined_func)
7575 {
7576 /* If we do not have call site information, we can't show the
7577 caller of this inlined function. That's too confusing, so
7578 only use the scope for local variables. */
7579 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7580 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7581 if (call_line == NULL || call_file == NULL)
7582 {
7583 read_lexical_block_scope (die, cu);
7584 return;
7585 }
7586 }
7587
7588 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7589
7590 name = dwarf2_name (die, cu);
7591
7592 /* Ignore functions with missing or empty names. These are actually
7593 illegal according to the DWARF standard. */
7594 if (name == NULL)
7595 {
7596 complaint (&symfile_complaints,
7597 _("missing name for subprogram DIE at %d"),
7598 die->offset.sect_off);
7599 return;
7600 }
7601
7602 /* Ignore functions with missing or invalid low and high pc attributes. */
7603 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7604 {
7605 attr = dwarf2_attr (die, DW_AT_external, cu);
7606 if (!attr || !DW_UNSND (attr))
7607 complaint (&symfile_complaints,
7608 _("cannot get low and high bounds "
7609 "for subprogram DIE at %d"),
7610 die->offset.sect_off);
7611 return;
7612 }
7613
7614 lowpc += baseaddr;
7615 highpc += baseaddr;
7616
7617 /* If we have any template arguments, then we must allocate a
7618 different sort of symbol. */
7619 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7620 {
7621 if (child_die->tag == DW_TAG_template_type_param
7622 || child_die->tag == DW_TAG_template_value_param)
7623 {
7624 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7625 struct template_symbol);
7626 templ_func->base.is_cplus_template_function = 1;
7627 break;
7628 }
7629 }
7630
7631 new = push_context (0, lowpc);
7632 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7633 (struct symbol *) templ_func);
7634
7635 /* If there is a location expression for DW_AT_frame_base, record
7636 it. */
7637 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7638 if (attr)
7639 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7640 expression is being recorded directly in the function's symbol
7641 and not in a separate frame-base object. I guess this hack is
7642 to avoid adding some sort of frame-base adjunct/annex to the
7643 function's symbol :-(. The problem with doing this is that it
7644 results in a function symbol with a location expression that
7645 has nothing to do with the location of the function, ouch! The
7646 relationship should be: a function's symbol has-a frame base; a
7647 frame-base has-a location expression. */
7648 dwarf2_symbol_mark_computed (attr, new->name, cu);
7649
7650 cu->list_in_scope = &local_symbols;
7651
7652 if (die->child != NULL)
7653 {
7654 child_die = die->child;
7655 while (child_die && child_die->tag)
7656 {
7657 if (child_die->tag == DW_TAG_template_type_param
7658 || child_die->tag == DW_TAG_template_value_param)
7659 {
7660 struct symbol *arg = new_symbol (child_die, NULL, cu);
7661
7662 if (arg != NULL)
7663 VEC_safe_push (symbolp, template_args, arg);
7664 }
7665 else
7666 process_die (child_die, cu);
7667 child_die = sibling_die (child_die);
7668 }
7669 }
7670
7671 inherit_abstract_dies (die, cu);
7672
7673 /* If we have a DW_AT_specification, we might need to import using
7674 directives from the context of the specification DIE. See the
7675 comment in determine_prefix. */
7676 if (cu->language == language_cplus
7677 && dwarf2_attr (die, DW_AT_specification, cu))
7678 {
7679 struct dwarf2_cu *spec_cu = cu;
7680 struct die_info *spec_die = die_specification (die, &spec_cu);
7681
7682 while (spec_die)
7683 {
7684 child_die = spec_die->child;
7685 while (child_die && child_die->tag)
7686 {
7687 if (child_die->tag == DW_TAG_imported_module)
7688 process_die (child_die, spec_cu);
7689 child_die = sibling_die (child_die);
7690 }
7691
7692 /* In some cases, GCC generates specification DIEs that
7693 themselves contain DW_AT_specification attributes. */
7694 spec_die = die_specification (spec_die, &spec_cu);
7695 }
7696 }
7697
7698 new = pop_context ();
7699 /* Make a block for the local symbols within. */
7700 block = finish_block (new->name, &local_symbols, new->old_blocks,
7701 lowpc, highpc, objfile);
7702
7703 /* For C++, set the block's scope. */
7704 if (cu->language == language_cplus || cu->language == language_fortran)
7705 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7706 determine_prefix (die, cu),
7707 processing_has_namespace_info);
7708
7709 /* If we have address ranges, record them. */
7710 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7711
7712 /* Attach template arguments to function. */
7713 if (! VEC_empty (symbolp, template_args))
7714 {
7715 gdb_assert (templ_func != NULL);
7716
7717 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7718 templ_func->template_arguments
7719 = obstack_alloc (&objfile->objfile_obstack,
7720 (templ_func->n_template_arguments
7721 * sizeof (struct symbol *)));
7722 memcpy (templ_func->template_arguments,
7723 VEC_address (symbolp, template_args),
7724 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7725 VEC_free (symbolp, template_args);
7726 }
7727
7728 /* In C++, we can have functions nested inside functions (e.g., when
7729 a function declares a class that has methods). This means that
7730 when we finish processing a function scope, we may need to go
7731 back to building a containing block's symbol lists. */
7732 local_symbols = new->locals;
7733 param_symbols = new->params;
7734 using_directives = new->using_directives;
7735
7736 /* If we've finished processing a top-level function, subsequent
7737 symbols go in the file symbol list. */
7738 if (outermost_context_p ())
7739 cu->list_in_scope = &file_symbols;
7740 }
7741
7742 /* Process all the DIES contained within a lexical block scope. Start
7743 a new scope, process the dies, and then close the scope. */
7744
7745 static void
7746 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7747 {
7748 struct objfile *objfile = cu->objfile;
7749 struct context_stack *new;
7750 CORE_ADDR lowpc, highpc;
7751 struct die_info *child_die;
7752 CORE_ADDR baseaddr;
7753
7754 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7755
7756 /* Ignore blocks with missing or invalid low and high pc attributes. */
7757 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7758 as multiple lexical blocks? Handling children in a sane way would
7759 be nasty. Might be easier to properly extend generic blocks to
7760 describe ranges. */
7761 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7762 return;
7763 lowpc += baseaddr;
7764 highpc += baseaddr;
7765
7766 push_context (0, lowpc);
7767 if (die->child != NULL)
7768 {
7769 child_die = die->child;
7770 while (child_die && child_die->tag)
7771 {
7772 process_die (child_die, cu);
7773 child_die = sibling_die (child_die);
7774 }
7775 }
7776 new = pop_context ();
7777
7778 if (local_symbols != NULL || using_directives != NULL)
7779 {
7780 struct block *block
7781 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7782 highpc, objfile);
7783
7784 /* Note that recording ranges after traversing children, as we
7785 do here, means that recording a parent's ranges entails
7786 walking across all its children's ranges as they appear in
7787 the address map, which is quadratic behavior.
7788
7789 It would be nicer to record the parent's ranges before
7790 traversing its children, simply overriding whatever you find
7791 there. But since we don't even decide whether to create a
7792 block until after we've traversed its children, that's hard
7793 to do. */
7794 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7795 }
7796 local_symbols = new->locals;
7797 using_directives = new->using_directives;
7798 }
7799
7800 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7801
7802 static void
7803 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7804 {
7805 struct objfile *objfile = cu->objfile;
7806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7807 CORE_ADDR pc, baseaddr;
7808 struct attribute *attr;
7809 struct call_site *call_site, call_site_local;
7810 void **slot;
7811 int nparams;
7812 struct die_info *child_die;
7813
7814 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7815
7816 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7817 if (!attr)
7818 {
7819 complaint (&symfile_complaints,
7820 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7821 "DIE 0x%x [in module %s]"),
7822 die->offset.sect_off, objfile->name);
7823 return;
7824 }
7825 pc = DW_ADDR (attr) + baseaddr;
7826
7827 if (cu->call_site_htab == NULL)
7828 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7829 NULL, &objfile->objfile_obstack,
7830 hashtab_obstack_allocate, NULL);
7831 call_site_local.pc = pc;
7832 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7833 if (*slot != NULL)
7834 {
7835 complaint (&symfile_complaints,
7836 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7837 "DIE 0x%x [in module %s]"),
7838 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7839 return;
7840 }
7841
7842 /* Count parameters at the caller. */
7843
7844 nparams = 0;
7845 for (child_die = die->child; child_die && child_die->tag;
7846 child_die = sibling_die (child_die))
7847 {
7848 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7849 {
7850 complaint (&symfile_complaints,
7851 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7852 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7853 child_die->tag, child_die->offset.sect_off, objfile->name);
7854 continue;
7855 }
7856
7857 nparams++;
7858 }
7859
7860 call_site = obstack_alloc (&objfile->objfile_obstack,
7861 (sizeof (*call_site)
7862 + (sizeof (*call_site->parameter)
7863 * (nparams - 1))));
7864 *slot = call_site;
7865 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7866 call_site->pc = pc;
7867
7868 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7869 {
7870 struct die_info *func_die;
7871
7872 /* Skip also over DW_TAG_inlined_subroutine. */
7873 for (func_die = die->parent;
7874 func_die && func_die->tag != DW_TAG_subprogram
7875 && func_die->tag != DW_TAG_subroutine_type;
7876 func_die = func_die->parent);
7877
7878 /* DW_AT_GNU_all_call_sites is a superset
7879 of DW_AT_GNU_all_tail_call_sites. */
7880 if (func_die
7881 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7882 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7883 {
7884 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7885 not complete. But keep CALL_SITE for look ups via call_site_htab,
7886 both the initial caller containing the real return address PC and
7887 the final callee containing the current PC of a chain of tail
7888 calls do not need to have the tail call list complete. But any
7889 function candidate for a virtual tail call frame searched via
7890 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7891 determined unambiguously. */
7892 }
7893 else
7894 {
7895 struct type *func_type = NULL;
7896
7897 if (func_die)
7898 func_type = get_die_type (func_die, cu);
7899 if (func_type != NULL)
7900 {
7901 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7902
7903 /* Enlist this call site to the function. */
7904 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7905 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7906 }
7907 else
7908 complaint (&symfile_complaints,
7909 _("Cannot find function owning DW_TAG_GNU_call_site "
7910 "DIE 0x%x [in module %s]"),
7911 die->offset.sect_off, objfile->name);
7912 }
7913 }
7914
7915 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7916 if (attr == NULL)
7917 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7918 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7919 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7920 /* Keep NULL DWARF_BLOCK. */;
7921 else if (attr_form_is_block (attr))
7922 {
7923 struct dwarf2_locexpr_baton *dlbaton;
7924
7925 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7926 dlbaton->data = DW_BLOCK (attr)->data;
7927 dlbaton->size = DW_BLOCK (attr)->size;
7928 dlbaton->per_cu = cu->per_cu;
7929
7930 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7931 }
7932 else if (is_ref_attr (attr))
7933 {
7934 struct dwarf2_cu *target_cu = cu;
7935 struct die_info *target_die;
7936
7937 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7938 gdb_assert (target_cu->objfile == objfile);
7939 if (die_is_declaration (target_die, target_cu))
7940 {
7941 const char *target_physname;
7942
7943 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7944 if (target_physname == NULL)
7945 complaint (&symfile_complaints,
7946 _("DW_AT_GNU_call_site_target target DIE has invalid "
7947 "physname, for referencing DIE 0x%x [in module %s]"),
7948 die->offset.sect_off, objfile->name);
7949 else
7950 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7951 }
7952 else
7953 {
7954 CORE_ADDR lowpc;
7955
7956 /* DW_AT_entry_pc should be preferred. */
7957 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7958 complaint (&symfile_complaints,
7959 _("DW_AT_GNU_call_site_target target DIE has invalid "
7960 "low pc, for referencing DIE 0x%x [in module %s]"),
7961 die->offset.sect_off, objfile->name);
7962 else
7963 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7964 }
7965 }
7966 else
7967 complaint (&symfile_complaints,
7968 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7969 "block nor reference, for DIE 0x%x [in module %s]"),
7970 die->offset.sect_off, objfile->name);
7971
7972 call_site->per_cu = cu->per_cu;
7973
7974 for (child_die = die->child;
7975 child_die && child_die->tag;
7976 child_die = sibling_die (child_die))
7977 {
7978 struct call_site_parameter *parameter;
7979 struct attribute *loc, *origin;
7980
7981 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7982 {
7983 /* Already printed the complaint above. */
7984 continue;
7985 }
7986
7987 gdb_assert (call_site->parameter_count < nparams);
7988 parameter = &call_site->parameter[call_site->parameter_count];
7989
7990 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
7991 specifies DW_TAG_formal_parameter. Value of the data assumed for the
7992 register is contained in DW_AT_GNU_call_site_value. */
7993
7994 loc = dwarf2_attr (child_die, DW_AT_location, cu);
7995 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7996 if (loc == NULL && origin != NULL && is_ref_attr (origin))
7997 {
7998 sect_offset offset;
7999
8000 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8001 offset = dwarf2_get_ref_die_offset (origin);
8002 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8003 parameter->u.param_offset.cu_off = (offset.sect_off
8004 - cu->header.offset.sect_off);
8005 }
8006 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
8007 {
8008 complaint (&symfile_complaints,
8009 _("No DW_FORM_block* DW_AT_location for "
8010 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8011 child_die->offset.sect_off, objfile->name);
8012 continue;
8013 }
8014 else
8015 {
8016 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8017 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8018 if (parameter->u.dwarf_reg != -1)
8019 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8020 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8021 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8022 &parameter->u.fb_offset))
8023 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8024 else
8025 {
8026 complaint (&symfile_complaints,
8027 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8028 "for DW_FORM_block* DW_AT_location is supported for "
8029 "DW_TAG_GNU_call_site child DIE 0x%x "
8030 "[in module %s]"),
8031 child_die->offset.sect_off, objfile->name);
8032 continue;
8033 }
8034 }
8035
8036 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8037 if (!attr_form_is_block (attr))
8038 {
8039 complaint (&symfile_complaints,
8040 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8041 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8042 child_die->offset.sect_off, objfile->name);
8043 continue;
8044 }
8045 parameter->value = DW_BLOCK (attr)->data;
8046 parameter->value_size = DW_BLOCK (attr)->size;
8047
8048 /* Parameters are not pre-cleared by memset above. */
8049 parameter->data_value = NULL;
8050 parameter->data_value_size = 0;
8051 call_site->parameter_count++;
8052
8053 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8054 if (attr)
8055 {
8056 if (!attr_form_is_block (attr))
8057 complaint (&symfile_complaints,
8058 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8059 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8060 child_die->offset.sect_off, objfile->name);
8061 else
8062 {
8063 parameter->data_value = DW_BLOCK (attr)->data;
8064 parameter->data_value_size = DW_BLOCK (attr)->size;
8065 }
8066 }
8067 }
8068 }
8069
8070 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
8071 Return 1 if the attributes are present and valid, otherwise, return 0.
8072 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
8073
8074 static int
8075 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8076 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8077 struct partial_symtab *ranges_pst)
8078 {
8079 struct objfile *objfile = cu->objfile;
8080 struct comp_unit_head *cu_header = &cu->header;
8081 bfd *obfd = objfile->obfd;
8082 unsigned int addr_size = cu_header->addr_size;
8083 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8084 /* Base address selection entry. */
8085 CORE_ADDR base;
8086 int found_base;
8087 unsigned int dummy;
8088 gdb_byte *buffer;
8089 CORE_ADDR marker;
8090 int low_set;
8091 CORE_ADDR low = 0;
8092 CORE_ADDR high = 0;
8093 CORE_ADDR baseaddr;
8094
8095 found_base = cu->base_known;
8096 base = cu->base_address;
8097
8098 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
8099 if (offset >= dwarf2_per_objfile->ranges.size)
8100 {
8101 complaint (&symfile_complaints,
8102 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8103 offset);
8104 return 0;
8105 }
8106 buffer = dwarf2_per_objfile->ranges.buffer + offset;
8107
8108 /* Read in the largest possible address. */
8109 marker = read_address (obfd, buffer, cu, &dummy);
8110 if ((marker & mask) == mask)
8111 {
8112 /* If we found the largest possible address, then
8113 read the base address. */
8114 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8115 buffer += 2 * addr_size;
8116 offset += 2 * addr_size;
8117 found_base = 1;
8118 }
8119
8120 low_set = 0;
8121
8122 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8123
8124 while (1)
8125 {
8126 CORE_ADDR range_beginning, range_end;
8127
8128 range_beginning = read_address (obfd, buffer, cu, &dummy);
8129 buffer += addr_size;
8130 range_end = read_address (obfd, buffer, cu, &dummy);
8131 buffer += addr_size;
8132 offset += 2 * addr_size;
8133
8134 /* An end of list marker is a pair of zero addresses. */
8135 if (range_beginning == 0 && range_end == 0)
8136 /* Found the end of list entry. */
8137 break;
8138
8139 /* Each base address selection entry is a pair of 2 values.
8140 The first is the largest possible address, the second is
8141 the base address. Check for a base address here. */
8142 if ((range_beginning & mask) == mask)
8143 {
8144 /* If we found the largest possible address, then
8145 read the base address. */
8146 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8147 found_base = 1;
8148 continue;
8149 }
8150
8151 if (!found_base)
8152 {
8153 /* We have no valid base address for the ranges
8154 data. */
8155 complaint (&symfile_complaints,
8156 _("Invalid .debug_ranges data (no base address)"));
8157 return 0;
8158 }
8159
8160 if (range_beginning > range_end)
8161 {
8162 /* Inverted range entries are invalid. */
8163 complaint (&symfile_complaints,
8164 _("Invalid .debug_ranges data (inverted range)"));
8165 return 0;
8166 }
8167
8168 /* Empty range entries have no effect. */
8169 if (range_beginning == range_end)
8170 continue;
8171
8172 range_beginning += base;
8173 range_end += base;
8174
8175 if (ranges_pst != NULL)
8176 addrmap_set_empty (objfile->psymtabs_addrmap,
8177 range_beginning + baseaddr,
8178 range_end - 1 + baseaddr,
8179 ranges_pst);
8180
8181 /* FIXME: This is recording everything as a low-high
8182 segment of consecutive addresses. We should have a
8183 data structure for discontiguous block ranges
8184 instead. */
8185 if (! low_set)
8186 {
8187 low = range_beginning;
8188 high = range_end;
8189 low_set = 1;
8190 }
8191 else
8192 {
8193 if (range_beginning < low)
8194 low = range_beginning;
8195 if (range_end > high)
8196 high = range_end;
8197 }
8198 }
8199
8200 if (! low_set)
8201 /* If the first entry is an end-of-list marker, the range
8202 describes an empty scope, i.e. no instructions. */
8203 return 0;
8204
8205 if (low_return)
8206 *low_return = low;
8207 if (high_return)
8208 *high_return = high;
8209 return 1;
8210 }
8211
8212 /* Get low and high pc attributes from a die. Return 1 if the attributes
8213 are present and valid, otherwise, return 0. Return -1 if the range is
8214 discontinuous, i.e. derived from DW_AT_ranges information. */
8215
8216 static int
8217 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
8218 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8219 struct partial_symtab *pst)
8220 {
8221 struct attribute *attr;
8222 struct attribute *attr_high;
8223 CORE_ADDR low = 0;
8224 CORE_ADDR high = 0;
8225 int ret = 0;
8226
8227 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8228 if (attr_high)
8229 {
8230 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8231 if (attr)
8232 {
8233 low = DW_ADDR (attr);
8234 if (attr_high->form == DW_FORM_addr
8235 || attr_high->form == DW_FORM_GNU_addr_index)
8236 high = DW_ADDR (attr_high);
8237 else
8238 high = low + DW_UNSND (attr_high);
8239 }
8240 else
8241 /* Found high w/o low attribute. */
8242 return 0;
8243
8244 /* Found consecutive range of addresses. */
8245 ret = 1;
8246 }
8247 else
8248 {
8249 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8250 if (attr != NULL)
8251 {
8252 /* Value of the DW_AT_ranges attribute is the offset in the
8253 .debug_ranges section. */
8254 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8255 return 0;
8256 /* Found discontinuous range of addresses. */
8257 ret = -1;
8258 }
8259 }
8260
8261 /* read_partial_die has also the strict LOW < HIGH requirement. */
8262 if (high <= low)
8263 return 0;
8264
8265 /* When using the GNU linker, .gnu.linkonce. sections are used to
8266 eliminate duplicate copies of functions and vtables and such.
8267 The linker will arbitrarily choose one and discard the others.
8268 The AT_*_pc values for such functions refer to local labels in
8269 these sections. If the section from that file was discarded, the
8270 labels are not in the output, so the relocs get a value of 0.
8271 If this is a discarded function, mark the pc bounds as invalid,
8272 so that GDB will ignore it. */
8273 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8274 return 0;
8275
8276 *lowpc = low;
8277 if (highpc)
8278 *highpc = high;
8279 return ret;
8280 }
8281
8282 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8283 its low and high PC addresses. Do nothing if these addresses could not
8284 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8285 and HIGHPC to the high address if greater than HIGHPC. */
8286
8287 static void
8288 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8289 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8290 struct dwarf2_cu *cu)
8291 {
8292 CORE_ADDR low, high;
8293 struct die_info *child = die->child;
8294
8295 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8296 {
8297 *lowpc = min (*lowpc, low);
8298 *highpc = max (*highpc, high);
8299 }
8300
8301 /* If the language does not allow nested subprograms (either inside
8302 subprograms or lexical blocks), we're done. */
8303 if (cu->language != language_ada)
8304 return;
8305
8306 /* Check all the children of the given DIE. If it contains nested
8307 subprograms, then check their pc bounds. Likewise, we need to
8308 check lexical blocks as well, as they may also contain subprogram
8309 definitions. */
8310 while (child && child->tag)
8311 {
8312 if (child->tag == DW_TAG_subprogram
8313 || child->tag == DW_TAG_lexical_block)
8314 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8315 child = sibling_die (child);
8316 }
8317 }
8318
8319 /* Get the low and high pc's represented by the scope DIE, and store
8320 them in *LOWPC and *HIGHPC. If the correct values can't be
8321 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8322
8323 static void
8324 get_scope_pc_bounds (struct die_info *die,
8325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8326 struct dwarf2_cu *cu)
8327 {
8328 CORE_ADDR best_low = (CORE_ADDR) -1;
8329 CORE_ADDR best_high = (CORE_ADDR) 0;
8330 CORE_ADDR current_low, current_high;
8331
8332 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8333 {
8334 best_low = current_low;
8335 best_high = current_high;
8336 }
8337 else
8338 {
8339 struct die_info *child = die->child;
8340
8341 while (child && child->tag)
8342 {
8343 switch (child->tag) {
8344 case DW_TAG_subprogram:
8345 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8346 break;
8347 case DW_TAG_namespace:
8348 case DW_TAG_module:
8349 /* FIXME: carlton/2004-01-16: Should we do this for
8350 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8351 that current GCC's always emit the DIEs corresponding
8352 to definitions of methods of classes as children of a
8353 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8354 the DIEs giving the declarations, which could be
8355 anywhere). But I don't see any reason why the
8356 standards says that they have to be there. */
8357 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8358
8359 if (current_low != ((CORE_ADDR) -1))
8360 {
8361 best_low = min (best_low, current_low);
8362 best_high = max (best_high, current_high);
8363 }
8364 break;
8365 default:
8366 /* Ignore. */
8367 break;
8368 }
8369
8370 child = sibling_die (child);
8371 }
8372 }
8373
8374 *lowpc = best_low;
8375 *highpc = best_high;
8376 }
8377
8378 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8379 in DIE. */
8380
8381 static void
8382 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8383 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8384 {
8385 struct objfile *objfile = cu->objfile;
8386 struct attribute *attr;
8387 struct attribute *attr_high;
8388
8389 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8390 if (attr_high)
8391 {
8392 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8393 if (attr)
8394 {
8395 CORE_ADDR low = DW_ADDR (attr);
8396 CORE_ADDR high;
8397 if (attr_high->form == DW_FORM_addr
8398 || attr_high->form == DW_FORM_GNU_addr_index)
8399 high = DW_ADDR (attr_high);
8400 else
8401 high = low + DW_UNSND (attr_high);
8402
8403 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8404 }
8405 }
8406
8407 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8408 if (attr)
8409 {
8410 bfd *obfd = objfile->obfd;
8411
8412 /* The value of the DW_AT_ranges attribute is the offset of the
8413 address range list in the .debug_ranges section. */
8414 unsigned long offset = DW_UNSND (attr);
8415 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8416
8417 /* For some target architectures, but not others, the
8418 read_address function sign-extends the addresses it returns.
8419 To recognize base address selection entries, we need a
8420 mask. */
8421 unsigned int addr_size = cu->header.addr_size;
8422 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8423
8424 /* The base address, to which the next pair is relative. Note
8425 that this 'base' is a DWARF concept: most entries in a range
8426 list are relative, to reduce the number of relocs against the
8427 debugging information. This is separate from this function's
8428 'baseaddr' argument, which GDB uses to relocate debugging
8429 information from a shared library based on the address at
8430 which the library was loaded. */
8431 CORE_ADDR base = cu->base_address;
8432 int base_known = cu->base_known;
8433
8434 gdb_assert (dwarf2_per_objfile->ranges.readin);
8435 if (offset >= dwarf2_per_objfile->ranges.size)
8436 {
8437 complaint (&symfile_complaints,
8438 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8439 offset);
8440 return;
8441 }
8442
8443 for (;;)
8444 {
8445 unsigned int bytes_read;
8446 CORE_ADDR start, end;
8447
8448 start = read_address (obfd, buffer, cu, &bytes_read);
8449 buffer += bytes_read;
8450 end = read_address (obfd, buffer, cu, &bytes_read);
8451 buffer += bytes_read;
8452
8453 /* Did we find the end of the range list? */
8454 if (start == 0 && end == 0)
8455 break;
8456
8457 /* Did we find a base address selection entry? */
8458 else if ((start & base_select_mask) == base_select_mask)
8459 {
8460 base = end;
8461 base_known = 1;
8462 }
8463
8464 /* We found an ordinary address range. */
8465 else
8466 {
8467 if (!base_known)
8468 {
8469 complaint (&symfile_complaints,
8470 _("Invalid .debug_ranges data "
8471 "(no base address)"));
8472 return;
8473 }
8474
8475 if (start > end)
8476 {
8477 /* Inverted range entries are invalid. */
8478 complaint (&symfile_complaints,
8479 _("Invalid .debug_ranges data "
8480 "(inverted range)"));
8481 return;
8482 }
8483
8484 /* Empty range entries have no effect. */
8485 if (start == end)
8486 continue;
8487
8488 record_block_range (block,
8489 baseaddr + base + start,
8490 baseaddr + base + end - 1);
8491 }
8492 }
8493 }
8494 }
8495
8496 /* Check whether the producer field indicates either of GCC < 4.6, or the
8497 Intel C/C++ compiler, and cache the result in CU. */
8498
8499 static void
8500 check_producer (struct dwarf2_cu *cu)
8501 {
8502 const char *cs;
8503 int major, minor, release;
8504
8505 if (cu->producer == NULL)
8506 {
8507 /* For unknown compilers expect their behavior is DWARF version
8508 compliant.
8509
8510 GCC started to support .debug_types sections by -gdwarf-4 since
8511 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8512 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8513 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8514 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8515 }
8516 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
8517 {
8518 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8519
8520 cs = &cu->producer[strlen ("GNU ")];
8521 while (*cs && !isdigit (*cs))
8522 cs++;
8523 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8524 {
8525 /* Not recognized as GCC. */
8526 }
8527 else
8528 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8529 }
8530 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8531 cu->producer_is_icc = 1;
8532 else
8533 {
8534 /* For other non-GCC compilers, expect their behavior is DWARF version
8535 compliant. */
8536 }
8537
8538 cu->checked_producer = 1;
8539 }
8540
8541 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8542 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8543 during 4.6.0 experimental. */
8544
8545 static int
8546 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8547 {
8548 if (!cu->checked_producer)
8549 check_producer (cu);
8550
8551 return cu->producer_is_gxx_lt_4_6;
8552 }
8553
8554 /* Return the default accessibility type if it is not overriden by
8555 DW_AT_accessibility. */
8556
8557 static enum dwarf_access_attribute
8558 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8559 {
8560 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8561 {
8562 /* The default DWARF 2 accessibility for members is public, the default
8563 accessibility for inheritance is private. */
8564
8565 if (die->tag != DW_TAG_inheritance)
8566 return DW_ACCESS_public;
8567 else
8568 return DW_ACCESS_private;
8569 }
8570 else
8571 {
8572 /* DWARF 3+ defines the default accessibility a different way. The same
8573 rules apply now for DW_TAG_inheritance as for the members and it only
8574 depends on the container kind. */
8575
8576 if (die->parent->tag == DW_TAG_class_type)
8577 return DW_ACCESS_private;
8578 else
8579 return DW_ACCESS_public;
8580 }
8581 }
8582
8583 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8584 offset. If the attribute was not found return 0, otherwise return
8585 1. If it was found but could not properly be handled, set *OFFSET
8586 to 0. */
8587
8588 static int
8589 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8590 LONGEST *offset)
8591 {
8592 struct attribute *attr;
8593
8594 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8595 if (attr != NULL)
8596 {
8597 *offset = 0;
8598
8599 /* Note that we do not check for a section offset first here.
8600 This is because DW_AT_data_member_location is new in DWARF 4,
8601 so if we see it, we can assume that a constant form is really
8602 a constant and not a section offset. */
8603 if (attr_form_is_constant (attr))
8604 *offset = dwarf2_get_attr_constant_value (attr, 0);
8605 else if (attr_form_is_section_offset (attr))
8606 dwarf2_complex_location_expr_complaint ();
8607 else if (attr_form_is_block (attr))
8608 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8609 else
8610 dwarf2_complex_location_expr_complaint ();
8611
8612 return 1;
8613 }
8614
8615 return 0;
8616 }
8617
8618 /* Add an aggregate field to the field list. */
8619
8620 static void
8621 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8622 struct dwarf2_cu *cu)
8623 {
8624 struct objfile *objfile = cu->objfile;
8625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8626 struct nextfield *new_field;
8627 struct attribute *attr;
8628 struct field *fp;
8629 char *fieldname = "";
8630
8631 /* Allocate a new field list entry and link it in. */
8632 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8633 make_cleanup (xfree, new_field);
8634 memset (new_field, 0, sizeof (struct nextfield));
8635
8636 if (die->tag == DW_TAG_inheritance)
8637 {
8638 new_field->next = fip->baseclasses;
8639 fip->baseclasses = new_field;
8640 }
8641 else
8642 {
8643 new_field->next = fip->fields;
8644 fip->fields = new_field;
8645 }
8646 fip->nfields++;
8647
8648 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8649 if (attr)
8650 new_field->accessibility = DW_UNSND (attr);
8651 else
8652 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8653 if (new_field->accessibility != DW_ACCESS_public)
8654 fip->non_public_fields = 1;
8655
8656 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8657 if (attr)
8658 new_field->virtuality = DW_UNSND (attr);
8659 else
8660 new_field->virtuality = DW_VIRTUALITY_none;
8661
8662 fp = &new_field->field;
8663
8664 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8665 {
8666 LONGEST offset;
8667
8668 /* Data member other than a C++ static data member. */
8669
8670 /* Get type of field. */
8671 fp->type = die_type (die, cu);
8672
8673 SET_FIELD_BITPOS (*fp, 0);
8674
8675 /* Get bit size of field (zero if none). */
8676 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8677 if (attr)
8678 {
8679 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8680 }
8681 else
8682 {
8683 FIELD_BITSIZE (*fp) = 0;
8684 }
8685
8686 /* Get bit offset of field. */
8687 if (handle_data_member_location (die, cu, &offset))
8688 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8689 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8690 if (attr)
8691 {
8692 if (gdbarch_bits_big_endian (gdbarch))
8693 {
8694 /* For big endian bits, the DW_AT_bit_offset gives the
8695 additional bit offset from the MSB of the containing
8696 anonymous object to the MSB of the field. We don't
8697 have to do anything special since we don't need to
8698 know the size of the anonymous object. */
8699 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8700 }
8701 else
8702 {
8703 /* For little endian bits, compute the bit offset to the
8704 MSB of the anonymous object, subtract off the number of
8705 bits from the MSB of the field to the MSB of the
8706 object, and then subtract off the number of bits of
8707 the field itself. The result is the bit offset of
8708 the LSB of the field. */
8709 int anonymous_size;
8710 int bit_offset = DW_UNSND (attr);
8711
8712 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8713 if (attr)
8714 {
8715 /* The size of the anonymous object containing
8716 the bit field is explicit, so use the
8717 indicated size (in bytes). */
8718 anonymous_size = DW_UNSND (attr);
8719 }
8720 else
8721 {
8722 /* The size of the anonymous object containing
8723 the bit field must be inferred from the type
8724 attribute of the data member containing the
8725 bit field. */
8726 anonymous_size = TYPE_LENGTH (fp->type);
8727 }
8728 SET_FIELD_BITPOS (*fp,
8729 (FIELD_BITPOS (*fp)
8730 + anonymous_size * bits_per_byte
8731 - bit_offset - FIELD_BITSIZE (*fp)));
8732 }
8733 }
8734
8735 /* Get name of field. */
8736 fieldname = dwarf2_name (die, cu);
8737 if (fieldname == NULL)
8738 fieldname = "";
8739
8740 /* The name is already allocated along with this objfile, so we don't
8741 need to duplicate it for the type. */
8742 fp->name = fieldname;
8743
8744 /* Change accessibility for artificial fields (e.g. virtual table
8745 pointer or virtual base class pointer) to private. */
8746 if (dwarf2_attr (die, DW_AT_artificial, cu))
8747 {
8748 FIELD_ARTIFICIAL (*fp) = 1;
8749 new_field->accessibility = DW_ACCESS_private;
8750 fip->non_public_fields = 1;
8751 }
8752 }
8753 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8754 {
8755 /* C++ static member. */
8756
8757 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8758 is a declaration, but all versions of G++ as of this writing
8759 (so through at least 3.2.1) incorrectly generate
8760 DW_TAG_variable tags. */
8761
8762 const char *physname;
8763
8764 /* Get name of field. */
8765 fieldname = dwarf2_name (die, cu);
8766 if (fieldname == NULL)
8767 return;
8768
8769 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8770 if (attr
8771 /* Only create a symbol if this is an external value.
8772 new_symbol checks this and puts the value in the global symbol
8773 table, which we want. If it is not external, new_symbol
8774 will try to put the value in cu->list_in_scope which is wrong. */
8775 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8776 {
8777 /* A static const member, not much different than an enum as far as
8778 we're concerned, except that we can support more types. */
8779 new_symbol (die, NULL, cu);
8780 }
8781
8782 /* Get physical name. */
8783 physname = dwarf2_physname (fieldname, die, cu);
8784
8785 /* The name is already allocated along with this objfile, so we don't
8786 need to duplicate it for the type. */
8787 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8788 FIELD_TYPE (*fp) = die_type (die, cu);
8789 FIELD_NAME (*fp) = fieldname;
8790 }
8791 else if (die->tag == DW_TAG_inheritance)
8792 {
8793 LONGEST offset;
8794
8795 /* C++ base class field. */
8796 if (handle_data_member_location (die, cu, &offset))
8797 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8798 FIELD_BITSIZE (*fp) = 0;
8799 FIELD_TYPE (*fp) = die_type (die, cu);
8800 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8801 fip->nbaseclasses++;
8802 }
8803 }
8804
8805 /* Add a typedef defined in the scope of the FIP's class. */
8806
8807 static void
8808 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8809 struct dwarf2_cu *cu)
8810 {
8811 struct objfile *objfile = cu->objfile;
8812 struct typedef_field_list *new_field;
8813 struct attribute *attr;
8814 struct typedef_field *fp;
8815 char *fieldname = "";
8816
8817 /* Allocate a new field list entry and link it in. */
8818 new_field = xzalloc (sizeof (*new_field));
8819 make_cleanup (xfree, new_field);
8820
8821 gdb_assert (die->tag == DW_TAG_typedef);
8822
8823 fp = &new_field->field;
8824
8825 /* Get name of field. */
8826 fp->name = dwarf2_name (die, cu);
8827 if (fp->name == NULL)
8828 return;
8829
8830 fp->type = read_type_die (die, cu);
8831
8832 new_field->next = fip->typedef_field_list;
8833 fip->typedef_field_list = new_field;
8834 fip->typedef_field_list_count++;
8835 }
8836
8837 /* Create the vector of fields, and attach it to the type. */
8838
8839 static void
8840 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8841 struct dwarf2_cu *cu)
8842 {
8843 int nfields = fip->nfields;
8844
8845 /* Record the field count, allocate space for the array of fields,
8846 and create blank accessibility bitfields if necessary. */
8847 TYPE_NFIELDS (type) = nfields;
8848 TYPE_FIELDS (type) = (struct field *)
8849 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8850 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8851
8852 if (fip->non_public_fields && cu->language != language_ada)
8853 {
8854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8855
8856 TYPE_FIELD_PRIVATE_BITS (type) =
8857 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8858 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8859
8860 TYPE_FIELD_PROTECTED_BITS (type) =
8861 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8862 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8863
8864 TYPE_FIELD_IGNORE_BITS (type) =
8865 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8866 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8867 }
8868
8869 /* If the type has baseclasses, allocate and clear a bit vector for
8870 TYPE_FIELD_VIRTUAL_BITS. */
8871 if (fip->nbaseclasses && cu->language != language_ada)
8872 {
8873 int num_bytes = B_BYTES (fip->nbaseclasses);
8874 unsigned char *pointer;
8875
8876 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8877 pointer = TYPE_ALLOC (type, num_bytes);
8878 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8879 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8880 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8881 }
8882
8883 /* Copy the saved-up fields into the field vector. Start from the head of
8884 the list, adding to the tail of the field array, so that they end up in
8885 the same order in the array in which they were added to the list. */
8886 while (nfields-- > 0)
8887 {
8888 struct nextfield *fieldp;
8889
8890 if (fip->fields)
8891 {
8892 fieldp = fip->fields;
8893 fip->fields = fieldp->next;
8894 }
8895 else
8896 {
8897 fieldp = fip->baseclasses;
8898 fip->baseclasses = fieldp->next;
8899 }
8900
8901 TYPE_FIELD (type, nfields) = fieldp->field;
8902 switch (fieldp->accessibility)
8903 {
8904 case DW_ACCESS_private:
8905 if (cu->language != language_ada)
8906 SET_TYPE_FIELD_PRIVATE (type, nfields);
8907 break;
8908
8909 case DW_ACCESS_protected:
8910 if (cu->language != language_ada)
8911 SET_TYPE_FIELD_PROTECTED (type, nfields);
8912 break;
8913
8914 case DW_ACCESS_public:
8915 break;
8916
8917 default:
8918 /* Unknown accessibility. Complain and treat it as public. */
8919 {
8920 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8921 fieldp->accessibility);
8922 }
8923 break;
8924 }
8925 if (nfields < fip->nbaseclasses)
8926 {
8927 switch (fieldp->virtuality)
8928 {
8929 case DW_VIRTUALITY_virtual:
8930 case DW_VIRTUALITY_pure_virtual:
8931 if (cu->language == language_ada)
8932 error (_("unexpected virtuality in component of Ada type"));
8933 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8934 break;
8935 }
8936 }
8937 }
8938 }
8939
8940 /* Add a member function to the proper fieldlist. */
8941
8942 static void
8943 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8944 struct type *type, struct dwarf2_cu *cu)
8945 {
8946 struct objfile *objfile = cu->objfile;
8947 struct attribute *attr;
8948 struct fnfieldlist *flp;
8949 int i;
8950 struct fn_field *fnp;
8951 char *fieldname;
8952 struct nextfnfield *new_fnfield;
8953 struct type *this_type;
8954 enum dwarf_access_attribute accessibility;
8955
8956 if (cu->language == language_ada)
8957 error (_("unexpected member function in Ada type"));
8958
8959 /* Get name of member function. */
8960 fieldname = dwarf2_name (die, cu);
8961 if (fieldname == NULL)
8962 return;
8963
8964 /* Look up member function name in fieldlist. */
8965 for (i = 0; i < fip->nfnfields; i++)
8966 {
8967 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8968 break;
8969 }
8970
8971 /* Create new list element if necessary. */
8972 if (i < fip->nfnfields)
8973 flp = &fip->fnfieldlists[i];
8974 else
8975 {
8976 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8977 {
8978 fip->fnfieldlists = (struct fnfieldlist *)
8979 xrealloc (fip->fnfieldlists,
8980 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8981 * sizeof (struct fnfieldlist));
8982 if (fip->nfnfields == 0)
8983 make_cleanup (free_current_contents, &fip->fnfieldlists);
8984 }
8985 flp = &fip->fnfieldlists[fip->nfnfields];
8986 flp->name = fieldname;
8987 flp->length = 0;
8988 flp->head = NULL;
8989 i = fip->nfnfields++;
8990 }
8991
8992 /* Create a new member function field and chain it to the field list
8993 entry. */
8994 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8995 make_cleanup (xfree, new_fnfield);
8996 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8997 new_fnfield->next = flp->head;
8998 flp->head = new_fnfield;
8999 flp->length++;
9000
9001 /* Fill in the member function field info. */
9002 fnp = &new_fnfield->fnfield;
9003
9004 /* Delay processing of the physname until later. */
9005 if (cu->language == language_cplus || cu->language == language_java)
9006 {
9007 add_to_method_list (type, i, flp->length - 1, fieldname,
9008 die, cu);
9009 }
9010 else
9011 {
9012 const char *physname = dwarf2_physname (fieldname, die, cu);
9013 fnp->physname = physname ? physname : "";
9014 }
9015
9016 fnp->type = alloc_type (objfile);
9017 this_type = read_type_die (die, cu);
9018 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
9019 {
9020 int nparams = TYPE_NFIELDS (this_type);
9021
9022 /* TYPE is the domain of this method, and THIS_TYPE is the type
9023 of the method itself (TYPE_CODE_METHOD). */
9024 smash_to_method_type (fnp->type, type,
9025 TYPE_TARGET_TYPE (this_type),
9026 TYPE_FIELDS (this_type),
9027 TYPE_NFIELDS (this_type),
9028 TYPE_VARARGS (this_type));
9029
9030 /* Handle static member functions.
9031 Dwarf2 has no clean way to discern C++ static and non-static
9032 member functions. G++ helps GDB by marking the first
9033 parameter for non-static member functions (which is the this
9034 pointer) as artificial. We obtain this information from
9035 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
9036 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
9037 fnp->voffset = VOFFSET_STATIC;
9038 }
9039 else
9040 complaint (&symfile_complaints, _("member function type missing for '%s'"),
9041 dwarf2_full_name (fieldname, die, cu));
9042
9043 /* Get fcontext from DW_AT_containing_type if present. */
9044 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9045 fnp->fcontext = die_containing_type (die, cu);
9046
9047 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9048 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
9049
9050 /* Get accessibility. */
9051 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9052 if (attr)
9053 accessibility = DW_UNSND (attr);
9054 else
9055 accessibility = dwarf2_default_access_attribute (die, cu);
9056 switch (accessibility)
9057 {
9058 case DW_ACCESS_private:
9059 fnp->is_private = 1;
9060 break;
9061 case DW_ACCESS_protected:
9062 fnp->is_protected = 1;
9063 break;
9064 }
9065
9066 /* Check for artificial methods. */
9067 attr = dwarf2_attr (die, DW_AT_artificial, cu);
9068 if (attr && DW_UNSND (attr) != 0)
9069 fnp->is_artificial = 1;
9070
9071 /* Get index in virtual function table if it is a virtual member
9072 function. For older versions of GCC, this is an offset in the
9073 appropriate virtual table, as specified by DW_AT_containing_type.
9074 For everyone else, it is an expression to be evaluated relative
9075 to the object address. */
9076
9077 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
9078 if (attr)
9079 {
9080 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
9081 {
9082 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9083 {
9084 /* Old-style GCC. */
9085 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9086 }
9087 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9088 || (DW_BLOCK (attr)->size > 1
9089 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9090 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9091 {
9092 struct dwarf_block blk;
9093 int offset;
9094
9095 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9096 ? 1 : 2);
9097 blk.size = DW_BLOCK (attr)->size - offset;
9098 blk.data = DW_BLOCK (attr)->data + offset;
9099 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9100 if ((fnp->voffset % cu->header.addr_size) != 0)
9101 dwarf2_complex_location_expr_complaint ();
9102 else
9103 fnp->voffset /= cu->header.addr_size;
9104 fnp->voffset += 2;
9105 }
9106 else
9107 dwarf2_complex_location_expr_complaint ();
9108
9109 if (!fnp->fcontext)
9110 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9111 }
9112 else if (attr_form_is_section_offset (attr))
9113 {
9114 dwarf2_complex_location_expr_complaint ();
9115 }
9116 else
9117 {
9118 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9119 fieldname);
9120 }
9121 }
9122 else
9123 {
9124 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9125 if (attr && DW_UNSND (attr))
9126 {
9127 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9128 complaint (&symfile_complaints,
9129 _("Member function \"%s\" (offset %d) is virtual "
9130 "but the vtable offset is not specified"),
9131 fieldname, die->offset.sect_off);
9132 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9133 TYPE_CPLUS_DYNAMIC (type) = 1;
9134 }
9135 }
9136 }
9137
9138 /* Create the vector of member function fields, and attach it to the type. */
9139
9140 static void
9141 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
9142 struct dwarf2_cu *cu)
9143 {
9144 struct fnfieldlist *flp;
9145 int i;
9146
9147 if (cu->language == language_ada)
9148 error (_("unexpected member functions in Ada type"));
9149
9150 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9151 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9152 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9153
9154 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9155 {
9156 struct nextfnfield *nfp = flp->head;
9157 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9158 int k;
9159
9160 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9161 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9162 fn_flp->fn_fields = (struct fn_field *)
9163 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9164 for (k = flp->length; (k--, nfp); nfp = nfp->next)
9165 fn_flp->fn_fields[k] = nfp->fnfield;
9166 }
9167
9168 TYPE_NFN_FIELDS (type) = fip->nfnfields;
9169 }
9170
9171 /* Returns non-zero if NAME is the name of a vtable member in CU's
9172 language, zero otherwise. */
9173 static int
9174 is_vtable_name (const char *name, struct dwarf2_cu *cu)
9175 {
9176 static const char vptr[] = "_vptr";
9177 static const char vtable[] = "vtable";
9178
9179 /* Look for the C++ and Java forms of the vtable. */
9180 if ((cu->language == language_java
9181 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9182 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9183 && is_cplus_marker (name[sizeof (vptr) - 1])))
9184 return 1;
9185
9186 return 0;
9187 }
9188
9189 /* GCC outputs unnamed structures that are really pointers to member
9190 functions, with the ABI-specified layout. If TYPE describes
9191 such a structure, smash it into a member function type.
9192
9193 GCC shouldn't do this; it should just output pointer to member DIEs.
9194 This is GCC PR debug/28767. */
9195
9196 static void
9197 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
9198 {
9199 struct type *pfn_type, *domain_type, *new_type;
9200
9201 /* Check for a structure with no name and two children. */
9202 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9203 return;
9204
9205 /* Check for __pfn and __delta members. */
9206 if (TYPE_FIELD_NAME (type, 0) == NULL
9207 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9208 || TYPE_FIELD_NAME (type, 1) == NULL
9209 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9210 return;
9211
9212 /* Find the type of the method. */
9213 pfn_type = TYPE_FIELD_TYPE (type, 0);
9214 if (pfn_type == NULL
9215 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9216 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
9217 return;
9218
9219 /* Look for the "this" argument. */
9220 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9221 if (TYPE_NFIELDS (pfn_type) == 0
9222 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
9223 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
9224 return;
9225
9226 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9227 new_type = alloc_type (objfile);
9228 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9229 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9230 TYPE_VARARGS (pfn_type));
9231 smash_to_methodptr_type (type, new_type);
9232 }
9233
9234 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9235 (icc). */
9236
9237 static int
9238 producer_is_icc (struct dwarf2_cu *cu)
9239 {
9240 if (!cu->checked_producer)
9241 check_producer (cu);
9242
9243 return cu->producer_is_icc;
9244 }
9245
9246 /* Called when we find the DIE that starts a structure or union scope
9247 (definition) to create a type for the structure or union. Fill in
9248 the type's name and general properties; the members will not be
9249 processed until process_structure_type.
9250
9251 NOTE: we need to call these functions regardless of whether or not the
9252 DIE has a DW_AT_name attribute, since it might be an anonymous
9253 structure or union. This gets the type entered into our set of
9254 user defined types.
9255
9256 However, if the structure is incomplete (an opaque struct/union)
9257 then suppress creating a symbol table entry for it since gdb only
9258 wants to find the one with the complete definition. Note that if
9259 it is complete, we just call new_symbol, which does it's own
9260 checking about whether the struct/union is anonymous or not (and
9261 suppresses creating a symbol table entry itself). */
9262
9263 static struct type *
9264 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9265 {
9266 struct objfile *objfile = cu->objfile;
9267 struct type *type;
9268 struct attribute *attr;
9269 char *name;
9270
9271 /* If the definition of this type lives in .debug_types, read that type.
9272 Don't follow DW_AT_specification though, that will take us back up
9273 the chain and we want to go down. */
9274 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9275 if (attr)
9276 {
9277 struct dwarf2_cu *type_cu = cu;
9278 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9279
9280 /* We could just recurse on read_structure_type, but we need to call
9281 get_die_type to ensure only one type for this DIE is created.
9282 This is important, for example, because for c++ classes we need
9283 TYPE_NAME set which is only done by new_symbol. Blech. */
9284 type = read_type_die (type_die, type_cu);
9285
9286 /* TYPE_CU may not be the same as CU.
9287 Ensure TYPE is recorded in CU's type_hash table. */
9288 return set_die_type (die, type, cu);
9289 }
9290
9291 type = alloc_type (objfile);
9292 INIT_CPLUS_SPECIFIC (type);
9293
9294 name = dwarf2_name (die, cu);
9295 if (name != NULL)
9296 {
9297 if (cu->language == language_cplus
9298 || cu->language == language_java)
9299 {
9300 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9301
9302 /* dwarf2_full_name might have already finished building the DIE's
9303 type. If so, there is no need to continue. */
9304 if (get_die_type (die, cu) != NULL)
9305 return get_die_type (die, cu);
9306
9307 TYPE_TAG_NAME (type) = full_name;
9308 if (die->tag == DW_TAG_structure_type
9309 || die->tag == DW_TAG_class_type)
9310 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9311 }
9312 else
9313 {
9314 /* The name is already allocated along with this objfile, so
9315 we don't need to duplicate it for the type. */
9316 TYPE_TAG_NAME (type) = (char *) name;
9317 if (die->tag == DW_TAG_class_type)
9318 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9319 }
9320 }
9321
9322 if (die->tag == DW_TAG_structure_type)
9323 {
9324 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9325 }
9326 else if (die->tag == DW_TAG_union_type)
9327 {
9328 TYPE_CODE (type) = TYPE_CODE_UNION;
9329 }
9330 else
9331 {
9332 TYPE_CODE (type) = TYPE_CODE_CLASS;
9333 }
9334
9335 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9336 TYPE_DECLARED_CLASS (type) = 1;
9337
9338 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9339 if (attr)
9340 {
9341 TYPE_LENGTH (type) = DW_UNSND (attr);
9342 }
9343 else
9344 {
9345 TYPE_LENGTH (type) = 0;
9346 }
9347
9348 if (producer_is_icc (cu))
9349 {
9350 /* ICC does not output the required DW_AT_declaration
9351 on incomplete types, but gives them a size of zero. */
9352 }
9353 else
9354 TYPE_STUB_SUPPORTED (type) = 1;
9355
9356 if (die_is_declaration (die, cu))
9357 TYPE_STUB (type) = 1;
9358 else if (attr == NULL && die->child == NULL
9359 && producer_is_realview (cu->producer))
9360 /* RealView does not output the required DW_AT_declaration
9361 on incomplete types. */
9362 TYPE_STUB (type) = 1;
9363
9364 /* We need to add the type field to the die immediately so we don't
9365 infinitely recurse when dealing with pointers to the structure
9366 type within the structure itself. */
9367 set_die_type (die, type, cu);
9368
9369 /* set_die_type should be already done. */
9370 set_descriptive_type (type, die, cu);
9371
9372 return type;
9373 }
9374
9375 /* Finish creating a structure or union type, including filling in
9376 its members and creating a symbol for it. */
9377
9378 static void
9379 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9380 {
9381 struct objfile *objfile = cu->objfile;
9382 struct die_info *child_die = die->child;
9383 struct type *type;
9384
9385 type = get_die_type (die, cu);
9386 if (type == NULL)
9387 type = read_structure_type (die, cu);
9388
9389 if (die->child != NULL && ! die_is_declaration (die, cu))
9390 {
9391 struct field_info fi;
9392 struct die_info *child_die;
9393 VEC (symbolp) *template_args = NULL;
9394 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9395
9396 memset (&fi, 0, sizeof (struct field_info));
9397
9398 child_die = die->child;
9399
9400 while (child_die && child_die->tag)
9401 {
9402 if (child_die->tag == DW_TAG_member
9403 || child_die->tag == DW_TAG_variable)
9404 {
9405 /* NOTE: carlton/2002-11-05: A C++ static data member
9406 should be a DW_TAG_member that is a declaration, but
9407 all versions of G++ as of this writing (so through at
9408 least 3.2.1) incorrectly generate DW_TAG_variable
9409 tags for them instead. */
9410 dwarf2_add_field (&fi, child_die, cu);
9411 }
9412 else if (child_die->tag == DW_TAG_subprogram)
9413 {
9414 /* C++ member function. */
9415 dwarf2_add_member_fn (&fi, child_die, type, cu);
9416 }
9417 else if (child_die->tag == DW_TAG_inheritance)
9418 {
9419 /* C++ base class field. */
9420 dwarf2_add_field (&fi, child_die, cu);
9421 }
9422 else if (child_die->tag == DW_TAG_typedef)
9423 dwarf2_add_typedef (&fi, child_die, cu);
9424 else if (child_die->tag == DW_TAG_template_type_param
9425 || child_die->tag == DW_TAG_template_value_param)
9426 {
9427 struct symbol *arg = new_symbol (child_die, NULL, cu);
9428
9429 if (arg != NULL)
9430 VEC_safe_push (symbolp, template_args, arg);
9431 }
9432
9433 child_die = sibling_die (child_die);
9434 }
9435
9436 /* Attach template arguments to type. */
9437 if (! VEC_empty (symbolp, template_args))
9438 {
9439 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9440 TYPE_N_TEMPLATE_ARGUMENTS (type)
9441 = VEC_length (symbolp, template_args);
9442 TYPE_TEMPLATE_ARGUMENTS (type)
9443 = obstack_alloc (&objfile->objfile_obstack,
9444 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9445 * sizeof (struct symbol *)));
9446 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9447 VEC_address (symbolp, template_args),
9448 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9449 * sizeof (struct symbol *)));
9450 VEC_free (symbolp, template_args);
9451 }
9452
9453 /* Attach fields and member functions to the type. */
9454 if (fi.nfields)
9455 dwarf2_attach_fields_to_type (&fi, type, cu);
9456 if (fi.nfnfields)
9457 {
9458 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9459
9460 /* Get the type which refers to the base class (possibly this
9461 class itself) which contains the vtable pointer for the current
9462 class from the DW_AT_containing_type attribute. This use of
9463 DW_AT_containing_type is a GNU extension. */
9464
9465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9466 {
9467 struct type *t = die_containing_type (die, cu);
9468
9469 TYPE_VPTR_BASETYPE (type) = t;
9470 if (type == t)
9471 {
9472 int i;
9473
9474 /* Our own class provides vtbl ptr. */
9475 for (i = TYPE_NFIELDS (t) - 1;
9476 i >= TYPE_N_BASECLASSES (t);
9477 --i)
9478 {
9479 const char *fieldname = TYPE_FIELD_NAME (t, i);
9480
9481 if (is_vtable_name (fieldname, cu))
9482 {
9483 TYPE_VPTR_FIELDNO (type) = i;
9484 break;
9485 }
9486 }
9487
9488 /* Complain if virtual function table field not found. */
9489 if (i < TYPE_N_BASECLASSES (t))
9490 complaint (&symfile_complaints,
9491 _("virtual function table pointer "
9492 "not found when defining class '%s'"),
9493 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9494 "");
9495 }
9496 else
9497 {
9498 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9499 }
9500 }
9501 else if (cu->producer
9502 && strncmp (cu->producer,
9503 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9504 {
9505 /* The IBM XLC compiler does not provide direct indication
9506 of the containing type, but the vtable pointer is
9507 always named __vfp. */
9508
9509 int i;
9510
9511 for (i = TYPE_NFIELDS (type) - 1;
9512 i >= TYPE_N_BASECLASSES (type);
9513 --i)
9514 {
9515 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9516 {
9517 TYPE_VPTR_FIELDNO (type) = i;
9518 TYPE_VPTR_BASETYPE (type) = type;
9519 break;
9520 }
9521 }
9522 }
9523 }
9524
9525 /* Copy fi.typedef_field_list linked list elements content into the
9526 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9527 if (fi.typedef_field_list)
9528 {
9529 int i = fi.typedef_field_list_count;
9530
9531 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9532 TYPE_TYPEDEF_FIELD_ARRAY (type)
9533 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9534 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9535
9536 /* Reverse the list order to keep the debug info elements order. */
9537 while (--i >= 0)
9538 {
9539 struct typedef_field *dest, *src;
9540
9541 dest = &TYPE_TYPEDEF_FIELD (type, i);
9542 src = &fi.typedef_field_list->field;
9543 fi.typedef_field_list = fi.typedef_field_list->next;
9544 *dest = *src;
9545 }
9546 }
9547
9548 do_cleanups (back_to);
9549
9550 if (HAVE_CPLUS_STRUCT (type))
9551 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9552 }
9553
9554 quirk_gcc_member_function_pointer (type, objfile);
9555
9556 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9557 snapshots) has been known to create a die giving a declaration
9558 for a class that has, as a child, a die giving a definition for a
9559 nested class. So we have to process our children even if the
9560 current die is a declaration. Normally, of course, a declaration
9561 won't have any children at all. */
9562
9563 while (child_die != NULL && child_die->tag)
9564 {
9565 if (child_die->tag == DW_TAG_member
9566 || child_die->tag == DW_TAG_variable
9567 || child_die->tag == DW_TAG_inheritance
9568 || child_die->tag == DW_TAG_template_value_param
9569 || child_die->tag == DW_TAG_template_type_param)
9570 {
9571 /* Do nothing. */
9572 }
9573 else
9574 process_die (child_die, cu);
9575
9576 child_die = sibling_die (child_die);
9577 }
9578
9579 /* Do not consider external references. According to the DWARF standard,
9580 these DIEs are identified by the fact that they have no byte_size
9581 attribute, and a declaration attribute. */
9582 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9583 || !die_is_declaration (die, cu))
9584 new_symbol (die, type, cu);
9585 }
9586
9587 /* Given a DW_AT_enumeration_type die, set its type. We do not
9588 complete the type's fields yet, or create any symbols. */
9589
9590 static struct type *
9591 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9592 {
9593 struct objfile *objfile = cu->objfile;
9594 struct type *type;
9595 struct attribute *attr;
9596 const char *name;
9597
9598 /* If the definition of this type lives in .debug_types, read that type.
9599 Don't follow DW_AT_specification though, that will take us back up
9600 the chain and we want to go down. */
9601 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9602 if (attr)
9603 {
9604 struct dwarf2_cu *type_cu = cu;
9605 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9606
9607 type = read_type_die (type_die, type_cu);
9608
9609 /* TYPE_CU may not be the same as CU.
9610 Ensure TYPE is recorded in CU's type_hash table. */
9611 return set_die_type (die, type, cu);
9612 }
9613
9614 type = alloc_type (objfile);
9615
9616 TYPE_CODE (type) = TYPE_CODE_ENUM;
9617 name = dwarf2_full_name (NULL, die, cu);
9618 if (name != NULL)
9619 TYPE_TAG_NAME (type) = (char *) name;
9620
9621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9622 if (attr)
9623 {
9624 TYPE_LENGTH (type) = DW_UNSND (attr);
9625 }
9626 else
9627 {
9628 TYPE_LENGTH (type) = 0;
9629 }
9630
9631 /* The enumeration DIE can be incomplete. In Ada, any type can be
9632 declared as private in the package spec, and then defined only
9633 inside the package body. Such types are known as Taft Amendment
9634 Types. When another package uses such a type, an incomplete DIE
9635 may be generated by the compiler. */
9636 if (die_is_declaration (die, cu))
9637 TYPE_STUB (type) = 1;
9638
9639 return set_die_type (die, type, cu);
9640 }
9641
9642 /* Given a pointer to a die which begins an enumeration, process all
9643 the dies that define the members of the enumeration, and create the
9644 symbol for the enumeration type.
9645
9646 NOTE: We reverse the order of the element list. */
9647
9648 static void
9649 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9650 {
9651 struct type *this_type;
9652
9653 this_type = get_die_type (die, cu);
9654 if (this_type == NULL)
9655 this_type = read_enumeration_type (die, cu);
9656
9657 if (die->child != NULL)
9658 {
9659 struct die_info *child_die;
9660 struct symbol *sym;
9661 struct field *fields = NULL;
9662 int num_fields = 0;
9663 int unsigned_enum = 1;
9664 char *name;
9665 int flag_enum = 1;
9666 ULONGEST mask = 0;
9667
9668 child_die = die->child;
9669 while (child_die && child_die->tag)
9670 {
9671 if (child_die->tag != DW_TAG_enumerator)
9672 {
9673 process_die (child_die, cu);
9674 }
9675 else
9676 {
9677 name = dwarf2_name (child_die, cu);
9678 if (name)
9679 {
9680 sym = new_symbol (child_die, this_type, cu);
9681 if (SYMBOL_VALUE (sym) < 0)
9682 {
9683 unsigned_enum = 0;
9684 flag_enum = 0;
9685 }
9686 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9687 flag_enum = 0;
9688 else
9689 mask |= SYMBOL_VALUE (sym);
9690
9691 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9692 {
9693 fields = (struct field *)
9694 xrealloc (fields,
9695 (num_fields + DW_FIELD_ALLOC_CHUNK)
9696 * sizeof (struct field));
9697 }
9698
9699 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9700 FIELD_TYPE (fields[num_fields]) = NULL;
9701 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9702 FIELD_BITSIZE (fields[num_fields]) = 0;
9703
9704 num_fields++;
9705 }
9706 }
9707
9708 child_die = sibling_die (child_die);
9709 }
9710
9711 if (num_fields)
9712 {
9713 TYPE_NFIELDS (this_type) = num_fields;
9714 TYPE_FIELDS (this_type) = (struct field *)
9715 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9716 memcpy (TYPE_FIELDS (this_type), fields,
9717 sizeof (struct field) * num_fields);
9718 xfree (fields);
9719 }
9720 if (unsigned_enum)
9721 TYPE_UNSIGNED (this_type) = 1;
9722 if (flag_enum)
9723 TYPE_FLAG_ENUM (this_type) = 1;
9724 }
9725
9726 /* If we are reading an enum from a .debug_types unit, and the enum
9727 is a declaration, and the enum is not the signatured type in the
9728 unit, then we do not want to add a symbol for it. Adding a
9729 symbol would in some cases obscure the true definition of the
9730 enum, giving users an incomplete type when the definition is
9731 actually available. Note that we do not want to do this for all
9732 enums which are just declarations, because C++0x allows forward
9733 enum declarations. */
9734 if (cu->per_cu->is_debug_types
9735 && die_is_declaration (die, cu))
9736 {
9737 struct signatured_type *sig_type;
9738
9739 sig_type
9740 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9741 cu->per_cu->info_or_types_section,
9742 cu->per_cu->offset);
9743 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9744 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9745 return;
9746 }
9747
9748 new_symbol (die, this_type, cu);
9749 }
9750
9751 /* Extract all information from a DW_TAG_array_type DIE and put it in
9752 the DIE's type field. For now, this only handles one dimensional
9753 arrays. */
9754
9755 static struct type *
9756 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9757 {
9758 struct objfile *objfile = cu->objfile;
9759 struct die_info *child_die;
9760 struct type *type;
9761 struct type *element_type, *range_type, *index_type;
9762 struct type **range_types = NULL;
9763 struct attribute *attr;
9764 int ndim = 0;
9765 struct cleanup *back_to;
9766 char *name;
9767
9768 element_type = die_type (die, cu);
9769
9770 /* The die_type call above may have already set the type for this DIE. */
9771 type = get_die_type (die, cu);
9772 if (type)
9773 return type;
9774
9775 /* Irix 6.2 native cc creates array types without children for
9776 arrays with unspecified length. */
9777 if (die->child == NULL)
9778 {
9779 index_type = objfile_type (objfile)->builtin_int;
9780 range_type = create_range_type (NULL, index_type, 0, -1);
9781 type = create_array_type (NULL, element_type, range_type);
9782 return set_die_type (die, type, cu);
9783 }
9784
9785 back_to = make_cleanup (null_cleanup, NULL);
9786 child_die = die->child;
9787 while (child_die && child_die->tag)
9788 {
9789 if (child_die->tag == DW_TAG_subrange_type)
9790 {
9791 struct type *child_type = read_type_die (child_die, cu);
9792
9793 if (child_type != NULL)
9794 {
9795 /* The range type was succesfully read. Save it for the
9796 array type creation. */
9797 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9798 {
9799 range_types = (struct type **)
9800 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9801 * sizeof (struct type *));
9802 if (ndim == 0)
9803 make_cleanup (free_current_contents, &range_types);
9804 }
9805 range_types[ndim++] = child_type;
9806 }
9807 }
9808 child_die = sibling_die (child_die);
9809 }
9810
9811 /* Dwarf2 dimensions are output from left to right, create the
9812 necessary array types in backwards order. */
9813
9814 type = element_type;
9815
9816 if (read_array_order (die, cu) == DW_ORD_col_major)
9817 {
9818 int i = 0;
9819
9820 while (i < ndim)
9821 type = create_array_type (NULL, type, range_types[i++]);
9822 }
9823 else
9824 {
9825 while (ndim-- > 0)
9826 type = create_array_type (NULL, type, range_types[ndim]);
9827 }
9828
9829 /* Understand Dwarf2 support for vector types (like they occur on
9830 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9831 array type. This is not part of the Dwarf2/3 standard yet, but a
9832 custom vendor extension. The main difference between a regular
9833 array and the vector variant is that vectors are passed by value
9834 to functions. */
9835 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9836 if (attr)
9837 make_vector_type (type);
9838
9839 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9840 implementation may choose to implement triple vectors using this
9841 attribute. */
9842 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9843 if (attr)
9844 {
9845 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9846 TYPE_LENGTH (type) = DW_UNSND (attr);
9847 else
9848 complaint (&symfile_complaints,
9849 _("DW_AT_byte_size for array type smaller "
9850 "than the total size of elements"));
9851 }
9852
9853 name = dwarf2_name (die, cu);
9854 if (name)
9855 TYPE_NAME (type) = name;
9856
9857 /* Install the type in the die. */
9858 set_die_type (die, type, cu);
9859
9860 /* set_die_type should be already done. */
9861 set_descriptive_type (type, die, cu);
9862
9863 do_cleanups (back_to);
9864
9865 return type;
9866 }
9867
9868 static enum dwarf_array_dim_ordering
9869 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9870 {
9871 struct attribute *attr;
9872
9873 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9874
9875 if (attr) return DW_SND (attr);
9876
9877 /* GNU F77 is a special case, as at 08/2004 array type info is the
9878 opposite order to the dwarf2 specification, but data is still
9879 laid out as per normal fortran.
9880
9881 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9882 version checking. */
9883
9884 if (cu->language == language_fortran
9885 && cu->producer && strstr (cu->producer, "GNU F77"))
9886 {
9887 return DW_ORD_row_major;
9888 }
9889
9890 switch (cu->language_defn->la_array_ordering)
9891 {
9892 case array_column_major:
9893 return DW_ORD_col_major;
9894 case array_row_major:
9895 default:
9896 return DW_ORD_row_major;
9897 };
9898 }
9899
9900 /* Extract all information from a DW_TAG_set_type DIE and put it in
9901 the DIE's type field. */
9902
9903 static struct type *
9904 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9905 {
9906 struct type *domain_type, *set_type;
9907 struct attribute *attr;
9908
9909 domain_type = die_type (die, cu);
9910
9911 /* The die_type call above may have already set the type for this DIE. */
9912 set_type = get_die_type (die, cu);
9913 if (set_type)
9914 return set_type;
9915
9916 set_type = create_set_type (NULL, domain_type);
9917
9918 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9919 if (attr)
9920 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9921
9922 return set_die_type (die, set_type, cu);
9923 }
9924
9925 /* First cut: install each common block member as a global variable. */
9926
9927 static void
9928 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9929 {
9930 struct die_info *child_die;
9931 struct attribute *attr;
9932 struct symbol *sym;
9933 CORE_ADDR base = (CORE_ADDR) 0;
9934
9935 attr = dwarf2_attr (die, DW_AT_location, cu);
9936 if (attr)
9937 {
9938 /* Support the .debug_loc offsets. */
9939 if (attr_form_is_block (attr))
9940 {
9941 base = decode_locdesc (DW_BLOCK (attr), cu);
9942 }
9943 else if (attr_form_is_section_offset (attr))
9944 {
9945 dwarf2_complex_location_expr_complaint ();
9946 }
9947 else
9948 {
9949 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9950 "common block member");
9951 }
9952 }
9953 if (die->child != NULL)
9954 {
9955 child_die = die->child;
9956 while (child_die && child_die->tag)
9957 {
9958 LONGEST offset;
9959
9960 sym = new_symbol (child_die, NULL, cu);
9961 if (sym != NULL
9962 && handle_data_member_location (child_die, cu, &offset))
9963 {
9964 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9965 add_symbol_to_list (sym, &global_symbols);
9966 }
9967 child_die = sibling_die (child_die);
9968 }
9969 }
9970 }
9971
9972 /* Create a type for a C++ namespace. */
9973
9974 static struct type *
9975 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9976 {
9977 struct objfile *objfile = cu->objfile;
9978 const char *previous_prefix, *name;
9979 int is_anonymous;
9980 struct type *type;
9981
9982 /* For extensions, reuse the type of the original namespace. */
9983 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9984 {
9985 struct die_info *ext_die;
9986 struct dwarf2_cu *ext_cu = cu;
9987
9988 ext_die = dwarf2_extension (die, &ext_cu);
9989 type = read_type_die (ext_die, ext_cu);
9990
9991 /* EXT_CU may not be the same as CU.
9992 Ensure TYPE is recorded in CU's type_hash table. */
9993 return set_die_type (die, type, cu);
9994 }
9995
9996 name = namespace_name (die, &is_anonymous, cu);
9997
9998 /* Now build the name of the current namespace. */
9999
10000 previous_prefix = determine_prefix (die, cu);
10001 if (previous_prefix[0] != '\0')
10002 name = typename_concat (&objfile->objfile_obstack,
10003 previous_prefix, name, 0, cu);
10004
10005 /* Create the type. */
10006 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10007 objfile);
10008 TYPE_NAME (type) = (char *) name;
10009 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10010
10011 return set_die_type (die, type, cu);
10012 }
10013
10014 /* Read a C++ namespace. */
10015
10016 static void
10017 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10018 {
10019 struct objfile *objfile = cu->objfile;
10020 int is_anonymous;
10021
10022 /* Add a symbol associated to this if we haven't seen the namespace
10023 before. Also, add a using directive if it's an anonymous
10024 namespace. */
10025
10026 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
10027 {
10028 struct type *type;
10029
10030 type = read_type_die (die, cu);
10031 new_symbol (die, type, cu);
10032
10033 namespace_name (die, &is_anonymous, cu);
10034 if (is_anonymous)
10035 {
10036 const char *previous_prefix = determine_prefix (die, cu);
10037
10038 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
10039 NULL, NULL, &objfile->objfile_obstack);
10040 }
10041 }
10042
10043 if (die->child != NULL)
10044 {
10045 struct die_info *child_die = die->child;
10046
10047 while (child_die && child_die->tag)
10048 {
10049 process_die (child_die, cu);
10050 child_die = sibling_die (child_die);
10051 }
10052 }
10053 }
10054
10055 /* Read a Fortran module as type. This DIE can be only a declaration used for
10056 imported module. Still we need that type as local Fortran "use ... only"
10057 declaration imports depend on the created type in determine_prefix. */
10058
10059 static struct type *
10060 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10061 {
10062 struct objfile *objfile = cu->objfile;
10063 char *module_name;
10064 struct type *type;
10065
10066 module_name = dwarf2_name (die, cu);
10067 if (!module_name)
10068 complaint (&symfile_complaints,
10069 _("DW_TAG_module has no name, offset 0x%x"),
10070 die->offset.sect_off);
10071 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10072
10073 /* determine_prefix uses TYPE_TAG_NAME. */
10074 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10075
10076 return set_die_type (die, type, cu);
10077 }
10078
10079 /* Read a Fortran module. */
10080
10081 static void
10082 read_module (struct die_info *die, struct dwarf2_cu *cu)
10083 {
10084 struct die_info *child_die = die->child;
10085
10086 while (child_die && child_die->tag)
10087 {
10088 process_die (child_die, cu);
10089 child_die = sibling_die (child_die);
10090 }
10091 }
10092
10093 /* Return the name of the namespace represented by DIE. Set
10094 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10095 namespace. */
10096
10097 static const char *
10098 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
10099 {
10100 struct die_info *current_die;
10101 const char *name = NULL;
10102
10103 /* Loop through the extensions until we find a name. */
10104
10105 for (current_die = die;
10106 current_die != NULL;
10107 current_die = dwarf2_extension (die, &cu))
10108 {
10109 name = dwarf2_name (current_die, cu);
10110 if (name != NULL)
10111 break;
10112 }
10113
10114 /* Is it an anonymous namespace? */
10115
10116 *is_anonymous = (name == NULL);
10117 if (*is_anonymous)
10118 name = CP_ANONYMOUS_NAMESPACE_STR;
10119
10120 return name;
10121 }
10122
10123 /* Extract all information from a DW_TAG_pointer_type DIE and add to
10124 the user defined type vector. */
10125
10126 static struct type *
10127 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
10128 {
10129 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
10130 struct comp_unit_head *cu_header = &cu->header;
10131 struct type *type;
10132 struct attribute *attr_byte_size;
10133 struct attribute *attr_address_class;
10134 int byte_size, addr_class;
10135 struct type *target_type;
10136
10137 target_type = die_type (die, cu);
10138
10139 /* The die_type call above may have already set the type for this DIE. */
10140 type = get_die_type (die, cu);
10141 if (type)
10142 return type;
10143
10144 type = lookup_pointer_type (target_type);
10145
10146 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
10147 if (attr_byte_size)
10148 byte_size = DW_UNSND (attr_byte_size);
10149 else
10150 byte_size = cu_header->addr_size;
10151
10152 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
10153 if (attr_address_class)
10154 addr_class = DW_UNSND (attr_address_class);
10155 else
10156 addr_class = DW_ADDR_none;
10157
10158 /* If the pointer size or address class is different than the
10159 default, create a type variant marked as such and set the
10160 length accordingly. */
10161 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
10162 {
10163 if (gdbarch_address_class_type_flags_p (gdbarch))
10164 {
10165 int type_flags;
10166
10167 type_flags = gdbarch_address_class_type_flags
10168 (gdbarch, byte_size, addr_class);
10169 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10170 == 0);
10171 type = make_type_with_address_space (type, type_flags);
10172 }
10173 else if (TYPE_LENGTH (type) != byte_size)
10174 {
10175 complaint (&symfile_complaints,
10176 _("invalid pointer size %d"), byte_size);
10177 }
10178 else
10179 {
10180 /* Should we also complain about unhandled address classes? */
10181 }
10182 }
10183
10184 TYPE_LENGTH (type) = byte_size;
10185 return set_die_type (die, type, cu);
10186 }
10187
10188 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10189 the user defined type vector. */
10190
10191 static struct type *
10192 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
10193 {
10194 struct type *type;
10195 struct type *to_type;
10196 struct type *domain;
10197
10198 to_type = die_type (die, cu);
10199 domain = die_containing_type (die, cu);
10200
10201 /* The calls above may have already set the type for this DIE. */
10202 type = get_die_type (die, cu);
10203 if (type)
10204 return type;
10205
10206 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10207 type = lookup_methodptr_type (to_type);
10208 else
10209 type = lookup_memberptr_type (to_type, domain);
10210
10211 return set_die_type (die, type, cu);
10212 }
10213
10214 /* Extract all information from a DW_TAG_reference_type DIE and add to
10215 the user defined type vector. */
10216
10217 static struct type *
10218 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
10219 {
10220 struct comp_unit_head *cu_header = &cu->header;
10221 struct type *type, *target_type;
10222 struct attribute *attr;
10223
10224 target_type = die_type (die, cu);
10225
10226 /* The die_type call above may have already set the type for this DIE. */
10227 type = get_die_type (die, cu);
10228 if (type)
10229 return type;
10230
10231 type = lookup_reference_type (target_type);
10232 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10233 if (attr)
10234 {
10235 TYPE_LENGTH (type) = DW_UNSND (attr);
10236 }
10237 else
10238 {
10239 TYPE_LENGTH (type) = cu_header->addr_size;
10240 }
10241 return set_die_type (die, type, cu);
10242 }
10243
10244 static struct type *
10245 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10246 {
10247 struct type *base_type, *cv_type;
10248
10249 base_type = die_type (die, cu);
10250
10251 /* The die_type call above may have already set the type for this DIE. */
10252 cv_type = get_die_type (die, cu);
10253 if (cv_type)
10254 return cv_type;
10255
10256 /* In case the const qualifier is applied to an array type, the element type
10257 is so qualified, not the array type (section 6.7.3 of C99). */
10258 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10259 {
10260 struct type *el_type, *inner_array;
10261
10262 base_type = copy_type (base_type);
10263 inner_array = base_type;
10264
10265 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10266 {
10267 TYPE_TARGET_TYPE (inner_array) =
10268 copy_type (TYPE_TARGET_TYPE (inner_array));
10269 inner_array = TYPE_TARGET_TYPE (inner_array);
10270 }
10271
10272 el_type = TYPE_TARGET_TYPE (inner_array);
10273 TYPE_TARGET_TYPE (inner_array) =
10274 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10275
10276 return set_die_type (die, base_type, cu);
10277 }
10278
10279 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10280 return set_die_type (die, cv_type, cu);
10281 }
10282
10283 static struct type *
10284 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10285 {
10286 struct type *base_type, *cv_type;
10287
10288 base_type = die_type (die, cu);
10289
10290 /* The die_type call above may have already set the type for this DIE. */
10291 cv_type = get_die_type (die, cu);
10292 if (cv_type)
10293 return cv_type;
10294
10295 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10296 return set_die_type (die, cv_type, cu);
10297 }
10298
10299 /* Extract all information from a DW_TAG_string_type DIE and add to
10300 the user defined type vector. It isn't really a user defined type,
10301 but it behaves like one, with other DIE's using an AT_user_def_type
10302 attribute to reference it. */
10303
10304 static struct type *
10305 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10306 {
10307 struct objfile *objfile = cu->objfile;
10308 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10309 struct type *type, *range_type, *index_type, *char_type;
10310 struct attribute *attr;
10311 unsigned int length;
10312
10313 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10314 if (attr)
10315 {
10316 length = DW_UNSND (attr);
10317 }
10318 else
10319 {
10320 /* Check for the DW_AT_byte_size attribute. */
10321 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10322 if (attr)
10323 {
10324 length = DW_UNSND (attr);
10325 }
10326 else
10327 {
10328 length = 1;
10329 }
10330 }
10331
10332 index_type = objfile_type (objfile)->builtin_int;
10333 range_type = create_range_type (NULL, index_type, 1, length);
10334 char_type = language_string_char_type (cu->language_defn, gdbarch);
10335 type = create_string_type (NULL, char_type, range_type);
10336
10337 return set_die_type (die, type, cu);
10338 }
10339
10340 /* Handle DIES due to C code like:
10341
10342 struct foo
10343 {
10344 int (*funcp)(int a, long l);
10345 int b;
10346 };
10347
10348 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10349
10350 static struct type *
10351 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10352 {
10353 struct objfile *objfile = cu->objfile;
10354 struct type *type; /* Type that this function returns. */
10355 struct type *ftype; /* Function that returns above type. */
10356 struct attribute *attr;
10357
10358 type = die_type (die, cu);
10359
10360 /* The die_type call above may have already set the type for this DIE. */
10361 ftype = get_die_type (die, cu);
10362 if (ftype)
10363 return ftype;
10364
10365 ftype = lookup_function_type (type);
10366
10367 /* All functions in C++, Pascal and Java have prototypes. */
10368 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10369 if ((attr && (DW_UNSND (attr) != 0))
10370 || cu->language == language_cplus
10371 || cu->language == language_java
10372 || cu->language == language_pascal)
10373 TYPE_PROTOTYPED (ftype) = 1;
10374 else if (producer_is_realview (cu->producer))
10375 /* RealView does not emit DW_AT_prototyped. We can not
10376 distinguish prototyped and unprototyped functions; default to
10377 prototyped, since that is more common in modern code (and
10378 RealView warns about unprototyped functions). */
10379 TYPE_PROTOTYPED (ftype) = 1;
10380
10381 /* Store the calling convention in the type if it's available in
10382 the subroutine die. Otherwise set the calling convention to
10383 the default value DW_CC_normal. */
10384 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10385 if (attr)
10386 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10387 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10388 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10389 else
10390 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10391
10392 /* We need to add the subroutine type to the die immediately so
10393 we don't infinitely recurse when dealing with parameters
10394 declared as the same subroutine type. */
10395 set_die_type (die, ftype, cu);
10396
10397 if (die->child != NULL)
10398 {
10399 struct type *void_type = objfile_type (objfile)->builtin_void;
10400 struct die_info *child_die;
10401 int nparams, iparams;
10402
10403 /* Count the number of parameters.
10404 FIXME: GDB currently ignores vararg functions, but knows about
10405 vararg member functions. */
10406 nparams = 0;
10407 child_die = die->child;
10408 while (child_die && child_die->tag)
10409 {
10410 if (child_die->tag == DW_TAG_formal_parameter)
10411 nparams++;
10412 else if (child_die->tag == DW_TAG_unspecified_parameters)
10413 TYPE_VARARGS (ftype) = 1;
10414 child_die = sibling_die (child_die);
10415 }
10416
10417 /* Allocate storage for parameters and fill them in. */
10418 TYPE_NFIELDS (ftype) = nparams;
10419 TYPE_FIELDS (ftype) = (struct field *)
10420 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10421
10422 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10423 even if we error out during the parameters reading below. */
10424 for (iparams = 0; iparams < nparams; iparams++)
10425 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10426
10427 iparams = 0;
10428 child_die = die->child;
10429 while (child_die && child_die->tag)
10430 {
10431 if (child_die->tag == DW_TAG_formal_parameter)
10432 {
10433 struct type *arg_type;
10434
10435 /* DWARF version 2 has no clean way to discern C++
10436 static and non-static member functions. G++ helps
10437 GDB by marking the first parameter for non-static
10438 member functions (which is the this pointer) as
10439 artificial. We pass this information to
10440 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10441
10442 DWARF version 3 added DW_AT_object_pointer, which GCC
10443 4.5 does not yet generate. */
10444 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10445 if (attr)
10446 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10447 else
10448 {
10449 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10450
10451 /* GCC/43521: In java, the formal parameter
10452 "this" is sometimes not marked with DW_AT_artificial. */
10453 if (cu->language == language_java)
10454 {
10455 const char *name = dwarf2_name (child_die, cu);
10456
10457 if (name && !strcmp (name, "this"))
10458 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10459 }
10460 }
10461 arg_type = die_type (child_die, cu);
10462
10463 /* RealView does not mark THIS as const, which the testsuite
10464 expects. GCC marks THIS as const in method definitions,
10465 but not in the class specifications (GCC PR 43053). */
10466 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10467 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10468 {
10469 int is_this = 0;
10470 struct dwarf2_cu *arg_cu = cu;
10471 const char *name = dwarf2_name (child_die, cu);
10472
10473 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10474 if (attr)
10475 {
10476 /* If the compiler emits this, use it. */
10477 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10478 is_this = 1;
10479 }
10480 else if (name && strcmp (name, "this") == 0)
10481 /* Function definitions will have the argument names. */
10482 is_this = 1;
10483 else if (name == NULL && iparams == 0)
10484 /* Declarations may not have the names, so like
10485 elsewhere in GDB, assume an artificial first
10486 argument is "this". */
10487 is_this = 1;
10488
10489 if (is_this)
10490 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10491 arg_type, 0);
10492 }
10493
10494 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10495 iparams++;
10496 }
10497 child_die = sibling_die (child_die);
10498 }
10499 }
10500
10501 return ftype;
10502 }
10503
10504 static struct type *
10505 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10506 {
10507 struct objfile *objfile = cu->objfile;
10508 const char *name = NULL;
10509 struct type *this_type, *target_type;
10510
10511 name = dwarf2_full_name (NULL, die, cu);
10512 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10513 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10514 TYPE_NAME (this_type) = (char *) name;
10515 set_die_type (die, this_type, cu);
10516 target_type = die_type (die, cu);
10517 if (target_type != this_type)
10518 TYPE_TARGET_TYPE (this_type) = target_type;
10519 else
10520 {
10521 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10522 spec and cause infinite loops in GDB. */
10523 complaint (&symfile_complaints,
10524 _("Self-referential DW_TAG_typedef "
10525 "- DIE at 0x%x [in module %s]"),
10526 die->offset.sect_off, objfile->name);
10527 TYPE_TARGET_TYPE (this_type) = NULL;
10528 }
10529 return this_type;
10530 }
10531
10532 /* Find a representation of a given base type and install
10533 it in the TYPE field of the die. */
10534
10535 static struct type *
10536 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10537 {
10538 struct objfile *objfile = cu->objfile;
10539 struct type *type;
10540 struct attribute *attr;
10541 int encoding = 0, size = 0;
10542 char *name;
10543 enum type_code code = TYPE_CODE_INT;
10544 int type_flags = 0;
10545 struct type *target_type = NULL;
10546
10547 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10548 if (attr)
10549 {
10550 encoding = DW_UNSND (attr);
10551 }
10552 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10553 if (attr)
10554 {
10555 size = DW_UNSND (attr);
10556 }
10557 name = dwarf2_name (die, cu);
10558 if (!name)
10559 {
10560 complaint (&symfile_complaints,
10561 _("DW_AT_name missing from DW_TAG_base_type"));
10562 }
10563
10564 switch (encoding)
10565 {
10566 case DW_ATE_address:
10567 /* Turn DW_ATE_address into a void * pointer. */
10568 code = TYPE_CODE_PTR;
10569 type_flags |= TYPE_FLAG_UNSIGNED;
10570 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10571 break;
10572 case DW_ATE_boolean:
10573 code = TYPE_CODE_BOOL;
10574 type_flags |= TYPE_FLAG_UNSIGNED;
10575 break;
10576 case DW_ATE_complex_float:
10577 code = TYPE_CODE_COMPLEX;
10578 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10579 break;
10580 case DW_ATE_decimal_float:
10581 code = TYPE_CODE_DECFLOAT;
10582 break;
10583 case DW_ATE_float:
10584 code = TYPE_CODE_FLT;
10585 break;
10586 case DW_ATE_signed:
10587 break;
10588 case DW_ATE_unsigned:
10589 type_flags |= TYPE_FLAG_UNSIGNED;
10590 if (cu->language == language_fortran
10591 && name
10592 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10593 code = TYPE_CODE_CHAR;
10594 break;
10595 case DW_ATE_signed_char:
10596 if (cu->language == language_ada || cu->language == language_m2
10597 || cu->language == language_pascal
10598 || cu->language == language_fortran)
10599 code = TYPE_CODE_CHAR;
10600 break;
10601 case DW_ATE_unsigned_char:
10602 if (cu->language == language_ada || cu->language == language_m2
10603 || cu->language == language_pascal
10604 || cu->language == language_fortran)
10605 code = TYPE_CODE_CHAR;
10606 type_flags |= TYPE_FLAG_UNSIGNED;
10607 break;
10608 case DW_ATE_UTF:
10609 /* We just treat this as an integer and then recognize the
10610 type by name elsewhere. */
10611 break;
10612
10613 default:
10614 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10615 dwarf_type_encoding_name (encoding));
10616 break;
10617 }
10618
10619 type = init_type (code, size, type_flags, NULL, objfile);
10620 TYPE_NAME (type) = name;
10621 TYPE_TARGET_TYPE (type) = target_type;
10622
10623 if (name && strcmp (name, "char") == 0)
10624 TYPE_NOSIGN (type) = 1;
10625
10626 return set_die_type (die, type, cu);
10627 }
10628
10629 /* Read the given DW_AT_subrange DIE. */
10630
10631 static struct type *
10632 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10633 {
10634 struct type *base_type;
10635 struct type *range_type;
10636 struct attribute *attr;
10637 LONGEST low, high;
10638 int low_default_is_valid;
10639 char *name;
10640 LONGEST negative_mask;
10641
10642 base_type = die_type (die, cu);
10643 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10644 check_typedef (base_type);
10645
10646 /* The die_type call above may have already set the type for this DIE. */
10647 range_type = get_die_type (die, cu);
10648 if (range_type)
10649 return range_type;
10650
10651 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10652 omitting DW_AT_lower_bound. */
10653 switch (cu->language)
10654 {
10655 case language_c:
10656 case language_cplus:
10657 low = 0;
10658 low_default_is_valid = 1;
10659 break;
10660 case language_fortran:
10661 low = 1;
10662 low_default_is_valid = 1;
10663 break;
10664 case language_d:
10665 case language_java:
10666 case language_objc:
10667 low = 0;
10668 low_default_is_valid = (cu->header.version >= 4);
10669 break;
10670 case language_ada:
10671 case language_m2:
10672 case language_pascal:
10673 low = 1;
10674 low_default_is_valid = (cu->header.version >= 4);
10675 break;
10676 default:
10677 low = 0;
10678 low_default_is_valid = 0;
10679 break;
10680 }
10681
10682 /* FIXME: For variable sized arrays either of these could be
10683 a variable rather than a constant value. We'll allow it,
10684 but we don't know how to handle it. */
10685 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10686 if (attr)
10687 low = dwarf2_get_attr_constant_value (attr, low);
10688 else if (!low_default_is_valid)
10689 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10690 "- DIE at 0x%x [in module %s]"),
10691 die->offset.sect_off, cu->objfile->name);
10692
10693 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10694 if (attr)
10695 {
10696 if (attr_form_is_block (attr) || is_ref_attr (attr))
10697 {
10698 /* GCC encodes arrays with unspecified or dynamic length
10699 with a DW_FORM_block1 attribute or a reference attribute.
10700 FIXME: GDB does not yet know how to handle dynamic
10701 arrays properly, treat them as arrays with unspecified
10702 length for now.
10703
10704 FIXME: jimb/2003-09-22: GDB does not really know
10705 how to handle arrays of unspecified length
10706 either; we just represent them as zero-length
10707 arrays. Choose an appropriate upper bound given
10708 the lower bound we've computed above. */
10709 high = low - 1;
10710 }
10711 else
10712 high = dwarf2_get_attr_constant_value (attr, 1);
10713 }
10714 else
10715 {
10716 attr = dwarf2_attr (die, DW_AT_count, cu);
10717 if (attr)
10718 {
10719 int count = dwarf2_get_attr_constant_value (attr, 1);
10720 high = low + count - 1;
10721 }
10722 else
10723 {
10724 /* Unspecified array length. */
10725 high = low - 1;
10726 }
10727 }
10728
10729 /* Dwarf-2 specifications explicitly allows to create subrange types
10730 without specifying a base type.
10731 In that case, the base type must be set to the type of
10732 the lower bound, upper bound or count, in that order, if any of these
10733 three attributes references an object that has a type.
10734 If no base type is found, the Dwarf-2 specifications say that
10735 a signed integer type of size equal to the size of an address should
10736 be used.
10737 For the following C code: `extern char gdb_int [];'
10738 GCC produces an empty range DIE.
10739 FIXME: muller/2010-05-28: Possible references to object for low bound,
10740 high bound or count are not yet handled by this code. */
10741 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10742 {
10743 struct objfile *objfile = cu->objfile;
10744 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10745 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10746 struct type *int_type = objfile_type (objfile)->builtin_int;
10747
10748 /* Test "int", "long int", and "long long int" objfile types,
10749 and select the first one having a size above or equal to the
10750 architecture address size. */
10751 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10752 base_type = int_type;
10753 else
10754 {
10755 int_type = objfile_type (objfile)->builtin_long;
10756 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10757 base_type = int_type;
10758 else
10759 {
10760 int_type = objfile_type (objfile)->builtin_long_long;
10761 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10762 base_type = int_type;
10763 }
10764 }
10765 }
10766
10767 negative_mask =
10768 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10769 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10770 low |= negative_mask;
10771 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10772 high |= negative_mask;
10773
10774 range_type = create_range_type (NULL, base_type, low, high);
10775
10776 /* Mark arrays with dynamic length at least as an array of unspecified
10777 length. GDB could check the boundary but before it gets implemented at
10778 least allow accessing the array elements. */
10779 if (attr && attr_form_is_block (attr))
10780 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10781
10782 /* Ada expects an empty array on no boundary attributes. */
10783 if (attr == NULL && cu->language != language_ada)
10784 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10785
10786 name = dwarf2_name (die, cu);
10787 if (name)
10788 TYPE_NAME (range_type) = name;
10789
10790 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10791 if (attr)
10792 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10793
10794 set_die_type (die, range_type, cu);
10795
10796 /* set_die_type should be already done. */
10797 set_descriptive_type (range_type, die, cu);
10798
10799 return range_type;
10800 }
10801
10802 static struct type *
10803 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10804 {
10805 struct type *type;
10806
10807 /* For now, we only support the C meaning of an unspecified type: void. */
10808
10809 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10810 TYPE_NAME (type) = dwarf2_name (die, cu);
10811
10812 return set_die_type (die, type, cu);
10813 }
10814
10815 /* Read a single die and all its descendents. Set the die's sibling
10816 field to NULL; set other fields in the die correctly, and set all
10817 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10818 location of the info_ptr after reading all of those dies. PARENT
10819 is the parent of the die in question. */
10820
10821 static struct die_info *
10822 read_die_and_children (const struct die_reader_specs *reader,
10823 gdb_byte *info_ptr,
10824 gdb_byte **new_info_ptr,
10825 struct die_info *parent)
10826 {
10827 struct die_info *die;
10828 gdb_byte *cur_ptr;
10829 int has_children;
10830
10831 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10832 if (die == NULL)
10833 {
10834 *new_info_ptr = cur_ptr;
10835 return NULL;
10836 }
10837 store_in_ref_table (die, reader->cu);
10838
10839 if (has_children)
10840 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10841 else
10842 {
10843 die->child = NULL;
10844 *new_info_ptr = cur_ptr;
10845 }
10846
10847 die->sibling = NULL;
10848 die->parent = parent;
10849 return die;
10850 }
10851
10852 /* Read a die, all of its descendents, and all of its siblings; set
10853 all of the fields of all of the dies correctly. Arguments are as
10854 in read_die_and_children. */
10855
10856 static struct die_info *
10857 read_die_and_siblings (const struct die_reader_specs *reader,
10858 gdb_byte *info_ptr,
10859 gdb_byte **new_info_ptr,
10860 struct die_info *parent)
10861 {
10862 struct die_info *first_die, *last_sibling;
10863 gdb_byte *cur_ptr;
10864
10865 cur_ptr = info_ptr;
10866 first_die = last_sibling = NULL;
10867
10868 while (1)
10869 {
10870 struct die_info *die
10871 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10872
10873 if (die == NULL)
10874 {
10875 *new_info_ptr = cur_ptr;
10876 return first_die;
10877 }
10878
10879 if (!first_die)
10880 first_die = die;
10881 else
10882 last_sibling->sibling = die;
10883
10884 last_sibling = die;
10885 }
10886 }
10887
10888 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10889 attributes.
10890 The caller is responsible for filling in the extra attributes
10891 and updating (*DIEP)->num_attrs.
10892 Set DIEP to point to a newly allocated die with its information,
10893 except for its child, sibling, and parent fields.
10894 Set HAS_CHILDREN to tell whether the die has children or not. */
10895
10896 static gdb_byte *
10897 read_full_die_1 (const struct die_reader_specs *reader,
10898 struct die_info **diep, gdb_byte *info_ptr,
10899 int *has_children, int num_extra_attrs)
10900 {
10901 unsigned int abbrev_number, bytes_read, i;
10902 sect_offset offset;
10903 struct abbrev_info *abbrev;
10904 struct die_info *die;
10905 struct dwarf2_cu *cu = reader->cu;
10906 bfd *abfd = reader->abfd;
10907
10908 offset.sect_off = info_ptr - reader->buffer;
10909 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10910 info_ptr += bytes_read;
10911 if (!abbrev_number)
10912 {
10913 *diep = NULL;
10914 *has_children = 0;
10915 return info_ptr;
10916 }
10917
10918 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10919 if (!abbrev)
10920 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10921 abbrev_number,
10922 bfd_get_filename (abfd));
10923
10924 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10925 die->offset = offset;
10926 die->tag = abbrev->tag;
10927 die->abbrev = abbrev_number;
10928
10929 /* Make the result usable.
10930 The caller needs to update num_attrs after adding the extra
10931 attributes. */
10932 die->num_attrs = abbrev->num_attrs;
10933
10934 for (i = 0; i < abbrev->num_attrs; ++i)
10935 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10936 info_ptr);
10937
10938 *diep = die;
10939 *has_children = abbrev->has_children;
10940 return info_ptr;
10941 }
10942
10943 /* Read a die and all its attributes.
10944 Set DIEP to point to a newly allocated die with its information,
10945 except for its child, sibling, and parent fields.
10946 Set HAS_CHILDREN to tell whether the die has children or not. */
10947
10948 static gdb_byte *
10949 read_full_die (const struct die_reader_specs *reader,
10950 struct die_info **diep, gdb_byte *info_ptr,
10951 int *has_children)
10952 {
10953 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10954 }
10955
10956 /* In DWARF version 2, the description of the debugging information is
10957 stored in a separate .debug_abbrev section. Before we read any
10958 dies from a section we read in all abbreviations and install them
10959 in a hash table. This function also sets flags in CU describing
10960 the data found in the abbrev table. */
10961
10962 static void
10963 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10964 struct dwarf2_section_info *abbrev_section)
10965
10966 {
10967 bfd *abfd = abbrev_section->asection->owner;
10968 struct comp_unit_head *cu_header = &cu->header;
10969 gdb_byte *abbrev_ptr;
10970 struct abbrev_info *cur_abbrev;
10971 unsigned int abbrev_number, bytes_read, abbrev_name;
10972 unsigned int abbrev_form, hash_number;
10973 struct attr_abbrev *cur_attrs;
10974 unsigned int allocated_attrs;
10975
10976 /* Initialize dwarf2 abbrevs. */
10977 obstack_init (&cu->abbrev_obstack);
10978 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10979 (ABBREV_HASH_SIZE
10980 * sizeof (struct abbrev_info *)));
10981 memset (cu->dwarf2_abbrevs, 0,
10982 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10983
10984 dwarf2_read_section (cu->objfile, abbrev_section);
10985 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10986 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10987 abbrev_ptr += bytes_read;
10988
10989 allocated_attrs = ATTR_ALLOC_CHUNK;
10990 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10991
10992 /* Loop until we reach an abbrev number of 0. */
10993 while (abbrev_number)
10994 {
10995 cur_abbrev = dwarf_alloc_abbrev (cu);
10996
10997 /* read in abbrev header */
10998 cur_abbrev->number = abbrev_number;
10999 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11000 abbrev_ptr += bytes_read;
11001 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11002 abbrev_ptr += 1;
11003
11004 /* now read in declarations */
11005 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11006 abbrev_ptr += bytes_read;
11007 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11008 abbrev_ptr += bytes_read;
11009 while (abbrev_name)
11010 {
11011 if (cur_abbrev->num_attrs == allocated_attrs)
11012 {
11013 allocated_attrs += ATTR_ALLOC_CHUNK;
11014 cur_attrs
11015 = xrealloc (cur_attrs, (allocated_attrs
11016 * sizeof (struct attr_abbrev)));
11017 }
11018
11019 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11020 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
11021 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11022 abbrev_ptr += bytes_read;
11023 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11024 abbrev_ptr += bytes_read;
11025 }
11026
11027 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
11028 (cur_abbrev->num_attrs
11029 * sizeof (struct attr_abbrev)));
11030 memcpy (cur_abbrev->attrs, cur_attrs,
11031 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11032
11033 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11034 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
11035 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
11036
11037 /* Get next abbreviation.
11038 Under Irix6 the abbreviations for a compilation unit are not
11039 always properly terminated with an abbrev number of 0.
11040 Exit loop if we encounter an abbreviation which we have
11041 already read (which means we are about to read the abbreviations
11042 for the next compile unit) or if the end of the abbreviation
11043 table is reached. */
11044 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
11045 >= abbrev_section->size)
11046 break;
11047 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11048 abbrev_ptr += bytes_read;
11049 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
11050 break;
11051 }
11052
11053 xfree (cur_attrs);
11054 }
11055
11056 /* Release the memory used by the abbrev table for a compilation unit. */
11057
11058 static void
11059 dwarf2_free_abbrev_table (void *ptr_to_cu)
11060 {
11061 struct dwarf2_cu *cu = ptr_to_cu;
11062
11063 obstack_free (&cu->abbrev_obstack, NULL);
11064 cu->dwarf2_abbrevs = NULL;
11065 }
11066
11067 /* Lookup an abbrev_info structure in the abbrev hash table. */
11068
11069 static struct abbrev_info *
11070 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
11071 {
11072 unsigned int hash_number;
11073 struct abbrev_info *abbrev;
11074
11075 hash_number = number % ABBREV_HASH_SIZE;
11076 abbrev = cu->dwarf2_abbrevs[hash_number];
11077
11078 while (abbrev)
11079 {
11080 if (abbrev->number == number)
11081 return abbrev;
11082 else
11083 abbrev = abbrev->next;
11084 }
11085 return NULL;
11086 }
11087
11088 /* Returns nonzero if TAG represents a type that we might generate a partial
11089 symbol for. */
11090
11091 static int
11092 is_type_tag_for_partial (int tag)
11093 {
11094 switch (tag)
11095 {
11096 #if 0
11097 /* Some types that would be reasonable to generate partial symbols for,
11098 that we don't at present. */
11099 case DW_TAG_array_type:
11100 case DW_TAG_file_type:
11101 case DW_TAG_ptr_to_member_type:
11102 case DW_TAG_set_type:
11103 case DW_TAG_string_type:
11104 case DW_TAG_subroutine_type:
11105 #endif
11106 case DW_TAG_base_type:
11107 case DW_TAG_class_type:
11108 case DW_TAG_interface_type:
11109 case DW_TAG_enumeration_type:
11110 case DW_TAG_structure_type:
11111 case DW_TAG_subrange_type:
11112 case DW_TAG_typedef:
11113 case DW_TAG_union_type:
11114 return 1;
11115 default:
11116 return 0;
11117 }
11118 }
11119
11120 /* Load all DIEs that are interesting for partial symbols into memory. */
11121
11122 static struct partial_die_info *
11123 load_partial_dies (const struct die_reader_specs *reader,
11124 gdb_byte *info_ptr, int building_psymtab)
11125 {
11126 struct dwarf2_cu *cu = reader->cu;
11127 struct objfile *objfile = cu->objfile;
11128 struct partial_die_info *part_die;
11129 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11130 struct abbrev_info *abbrev;
11131 unsigned int bytes_read;
11132 unsigned int load_all = 0;
11133 int nesting_level = 1;
11134
11135 parent_die = NULL;
11136 last_die = NULL;
11137
11138 gdb_assert (cu->per_cu != NULL);
11139 if (cu->per_cu->load_all_dies)
11140 load_all = 1;
11141
11142 cu->partial_dies
11143 = htab_create_alloc_ex (cu->header.length / 12,
11144 partial_die_hash,
11145 partial_die_eq,
11146 NULL,
11147 &cu->comp_unit_obstack,
11148 hashtab_obstack_allocate,
11149 dummy_obstack_deallocate);
11150
11151 part_die = obstack_alloc (&cu->comp_unit_obstack,
11152 sizeof (struct partial_die_info));
11153
11154 while (1)
11155 {
11156 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11157
11158 /* A NULL abbrev means the end of a series of children. */
11159 if (abbrev == NULL)
11160 {
11161 if (--nesting_level == 0)
11162 {
11163 /* PART_DIE was probably the last thing allocated on the
11164 comp_unit_obstack, so we could call obstack_free
11165 here. We don't do that because the waste is small,
11166 and will be cleaned up when we're done with this
11167 compilation unit. This way, we're also more robust
11168 against other users of the comp_unit_obstack. */
11169 return first_die;
11170 }
11171 info_ptr += bytes_read;
11172 last_die = parent_die;
11173 parent_die = parent_die->die_parent;
11174 continue;
11175 }
11176
11177 /* Check for template arguments. We never save these; if
11178 they're seen, we just mark the parent, and go on our way. */
11179 if (parent_die != NULL
11180 && cu->language == language_cplus
11181 && (abbrev->tag == DW_TAG_template_type_param
11182 || abbrev->tag == DW_TAG_template_value_param))
11183 {
11184 parent_die->has_template_arguments = 1;
11185
11186 if (!load_all)
11187 {
11188 /* We don't need a partial DIE for the template argument. */
11189 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11190 continue;
11191 }
11192 }
11193
11194 /* We only recurse into c++ subprograms looking for template arguments.
11195 Skip their other children. */
11196 if (!load_all
11197 && cu->language == language_cplus
11198 && parent_die != NULL
11199 && parent_die->tag == DW_TAG_subprogram)
11200 {
11201 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11202 continue;
11203 }
11204
11205 /* Check whether this DIE is interesting enough to save. Normally
11206 we would not be interested in members here, but there may be
11207 later variables referencing them via DW_AT_specification (for
11208 static members). */
11209 if (!load_all
11210 && !is_type_tag_for_partial (abbrev->tag)
11211 && abbrev->tag != DW_TAG_constant
11212 && abbrev->tag != DW_TAG_enumerator
11213 && abbrev->tag != DW_TAG_subprogram
11214 && abbrev->tag != DW_TAG_lexical_block
11215 && abbrev->tag != DW_TAG_variable
11216 && abbrev->tag != DW_TAG_namespace
11217 && abbrev->tag != DW_TAG_module
11218 && abbrev->tag != DW_TAG_member
11219 && abbrev->tag != DW_TAG_imported_unit)
11220 {
11221 /* Otherwise we skip to the next sibling, if any. */
11222 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11223 continue;
11224 }
11225
11226 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11227 info_ptr);
11228
11229 /* This two-pass algorithm for processing partial symbols has a
11230 high cost in cache pressure. Thus, handle some simple cases
11231 here which cover the majority of C partial symbols. DIEs
11232 which neither have specification tags in them, nor could have
11233 specification tags elsewhere pointing at them, can simply be
11234 processed and discarded.
11235
11236 This segment is also optional; scan_partial_symbols and
11237 add_partial_symbol will handle these DIEs if we chain
11238 them in normally. When compilers which do not emit large
11239 quantities of duplicate debug information are more common,
11240 this code can probably be removed. */
11241
11242 /* Any complete simple types at the top level (pretty much all
11243 of them, for a language without namespaces), can be processed
11244 directly. */
11245 if (parent_die == NULL
11246 && part_die->has_specification == 0
11247 && part_die->is_declaration == 0
11248 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11249 || part_die->tag == DW_TAG_base_type
11250 || part_die->tag == DW_TAG_subrange_type))
11251 {
11252 if (building_psymtab && part_die->name != NULL)
11253 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11254 VAR_DOMAIN, LOC_TYPEDEF,
11255 &objfile->static_psymbols,
11256 0, (CORE_ADDR) 0, cu->language, objfile);
11257 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11258 continue;
11259 }
11260
11261 /* The exception for DW_TAG_typedef with has_children above is
11262 a workaround of GCC PR debug/47510. In the case of this complaint
11263 type_name_no_tag_or_error will error on such types later.
11264
11265 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11266 it could not find the child DIEs referenced later, this is checked
11267 above. In correct DWARF DW_TAG_typedef should have no children. */
11268
11269 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11270 complaint (&symfile_complaints,
11271 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11272 "- DIE at 0x%x [in module %s]"),
11273 part_die->offset.sect_off, objfile->name);
11274
11275 /* If we're at the second level, and we're an enumerator, and
11276 our parent has no specification (meaning possibly lives in a
11277 namespace elsewhere), then we can add the partial symbol now
11278 instead of queueing it. */
11279 if (part_die->tag == DW_TAG_enumerator
11280 && parent_die != NULL
11281 && parent_die->die_parent == NULL
11282 && parent_die->tag == DW_TAG_enumeration_type
11283 && parent_die->has_specification == 0)
11284 {
11285 if (part_die->name == NULL)
11286 complaint (&symfile_complaints,
11287 _("malformed enumerator DIE ignored"));
11288 else if (building_psymtab)
11289 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11290 VAR_DOMAIN, LOC_CONST,
11291 (cu->language == language_cplus
11292 || cu->language == language_java)
11293 ? &objfile->global_psymbols
11294 : &objfile->static_psymbols,
11295 0, (CORE_ADDR) 0, cu->language, objfile);
11296
11297 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11298 continue;
11299 }
11300
11301 /* We'll save this DIE so link it in. */
11302 part_die->die_parent = parent_die;
11303 part_die->die_sibling = NULL;
11304 part_die->die_child = NULL;
11305
11306 if (last_die && last_die == parent_die)
11307 last_die->die_child = part_die;
11308 else if (last_die)
11309 last_die->die_sibling = part_die;
11310
11311 last_die = part_die;
11312
11313 if (first_die == NULL)
11314 first_die = part_die;
11315
11316 /* Maybe add the DIE to the hash table. Not all DIEs that we
11317 find interesting need to be in the hash table, because we
11318 also have the parent/sibling/child chains; only those that we
11319 might refer to by offset later during partial symbol reading.
11320
11321 For now this means things that might have be the target of a
11322 DW_AT_specification, DW_AT_abstract_origin, or
11323 DW_AT_extension. DW_AT_extension will refer only to
11324 namespaces; DW_AT_abstract_origin refers to functions (and
11325 many things under the function DIE, but we do not recurse
11326 into function DIEs during partial symbol reading) and
11327 possibly variables as well; DW_AT_specification refers to
11328 declarations. Declarations ought to have the DW_AT_declaration
11329 flag. It happens that GCC forgets to put it in sometimes, but
11330 only for functions, not for types.
11331
11332 Adding more things than necessary to the hash table is harmless
11333 except for the performance cost. Adding too few will result in
11334 wasted time in find_partial_die, when we reread the compilation
11335 unit with load_all_dies set. */
11336
11337 if (load_all
11338 || abbrev->tag == DW_TAG_constant
11339 || abbrev->tag == DW_TAG_subprogram
11340 || abbrev->tag == DW_TAG_variable
11341 || abbrev->tag == DW_TAG_namespace
11342 || part_die->is_declaration)
11343 {
11344 void **slot;
11345
11346 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11347 part_die->offset.sect_off, INSERT);
11348 *slot = part_die;
11349 }
11350
11351 part_die = obstack_alloc (&cu->comp_unit_obstack,
11352 sizeof (struct partial_die_info));
11353
11354 /* For some DIEs we want to follow their children (if any). For C
11355 we have no reason to follow the children of structures; for other
11356 languages we have to, so that we can get at method physnames
11357 to infer fully qualified class names, for DW_AT_specification,
11358 and for C++ template arguments. For C++, we also look one level
11359 inside functions to find template arguments (if the name of the
11360 function does not already contain the template arguments).
11361
11362 For Ada, we need to scan the children of subprograms and lexical
11363 blocks as well because Ada allows the definition of nested
11364 entities that could be interesting for the debugger, such as
11365 nested subprograms for instance. */
11366 if (last_die->has_children
11367 && (load_all
11368 || last_die->tag == DW_TAG_namespace
11369 || last_die->tag == DW_TAG_module
11370 || last_die->tag == DW_TAG_enumeration_type
11371 || (cu->language == language_cplus
11372 && last_die->tag == DW_TAG_subprogram
11373 && (last_die->name == NULL
11374 || strchr (last_die->name, '<') == NULL))
11375 || (cu->language != language_c
11376 && (last_die->tag == DW_TAG_class_type
11377 || last_die->tag == DW_TAG_interface_type
11378 || last_die->tag == DW_TAG_structure_type
11379 || last_die->tag == DW_TAG_union_type))
11380 || (cu->language == language_ada
11381 && (last_die->tag == DW_TAG_subprogram
11382 || last_die->tag == DW_TAG_lexical_block))))
11383 {
11384 nesting_level++;
11385 parent_die = last_die;
11386 continue;
11387 }
11388
11389 /* Otherwise we skip to the next sibling, if any. */
11390 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11391
11392 /* Back to the top, do it again. */
11393 }
11394 }
11395
11396 /* Read a minimal amount of information into the minimal die structure. */
11397
11398 static gdb_byte *
11399 read_partial_die (const struct die_reader_specs *reader,
11400 struct partial_die_info *part_die,
11401 struct abbrev_info *abbrev, unsigned int abbrev_len,
11402 gdb_byte *info_ptr)
11403 {
11404 struct dwarf2_cu *cu = reader->cu;
11405 struct objfile *objfile = cu->objfile;
11406 gdb_byte *buffer = reader->buffer;
11407 unsigned int i;
11408 struct attribute attr;
11409 int has_low_pc_attr = 0;
11410 int has_high_pc_attr = 0;
11411 int high_pc_relative = 0;
11412
11413 memset (part_die, 0, sizeof (struct partial_die_info));
11414
11415 part_die->offset.sect_off = info_ptr - buffer;
11416
11417 info_ptr += abbrev_len;
11418
11419 if (abbrev == NULL)
11420 return info_ptr;
11421
11422 part_die->tag = abbrev->tag;
11423 part_die->has_children = abbrev->has_children;
11424
11425 for (i = 0; i < abbrev->num_attrs; ++i)
11426 {
11427 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11428
11429 /* Store the data if it is of an attribute we want to keep in a
11430 partial symbol table. */
11431 switch (attr.name)
11432 {
11433 case DW_AT_name:
11434 switch (part_die->tag)
11435 {
11436 case DW_TAG_compile_unit:
11437 case DW_TAG_partial_unit:
11438 case DW_TAG_type_unit:
11439 /* Compilation units have a DW_AT_name that is a filename, not
11440 a source language identifier. */
11441 case DW_TAG_enumeration_type:
11442 case DW_TAG_enumerator:
11443 /* These tags always have simple identifiers already; no need
11444 to canonicalize them. */
11445 part_die->name = DW_STRING (&attr);
11446 break;
11447 default:
11448 part_die->name
11449 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11450 &objfile->objfile_obstack);
11451 break;
11452 }
11453 break;
11454 case DW_AT_linkage_name:
11455 case DW_AT_MIPS_linkage_name:
11456 /* Note that both forms of linkage name might appear. We
11457 assume they will be the same, and we only store the last
11458 one we see. */
11459 if (cu->language == language_ada)
11460 part_die->name = DW_STRING (&attr);
11461 part_die->linkage_name = DW_STRING (&attr);
11462 break;
11463 case DW_AT_low_pc:
11464 has_low_pc_attr = 1;
11465 part_die->lowpc = DW_ADDR (&attr);
11466 break;
11467 case DW_AT_high_pc:
11468 has_high_pc_attr = 1;
11469 if (attr.form == DW_FORM_addr
11470 || attr.form == DW_FORM_GNU_addr_index)
11471 part_die->highpc = DW_ADDR (&attr);
11472 else
11473 {
11474 high_pc_relative = 1;
11475 part_die->highpc = DW_UNSND (&attr);
11476 }
11477 break;
11478 case DW_AT_location:
11479 /* Support the .debug_loc offsets. */
11480 if (attr_form_is_block (&attr))
11481 {
11482 part_die->d.locdesc = DW_BLOCK (&attr);
11483 }
11484 else if (attr_form_is_section_offset (&attr))
11485 {
11486 dwarf2_complex_location_expr_complaint ();
11487 }
11488 else
11489 {
11490 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11491 "partial symbol information");
11492 }
11493 break;
11494 case DW_AT_external:
11495 part_die->is_external = DW_UNSND (&attr);
11496 break;
11497 case DW_AT_declaration:
11498 part_die->is_declaration = DW_UNSND (&attr);
11499 break;
11500 case DW_AT_type:
11501 part_die->has_type = 1;
11502 break;
11503 case DW_AT_abstract_origin:
11504 case DW_AT_specification:
11505 case DW_AT_extension:
11506 part_die->has_specification = 1;
11507 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11508 break;
11509 case DW_AT_sibling:
11510 /* Ignore absolute siblings, they might point outside of
11511 the current compile unit. */
11512 if (attr.form == DW_FORM_ref_addr)
11513 complaint (&symfile_complaints,
11514 _("ignoring absolute DW_AT_sibling"));
11515 else
11516 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11517 break;
11518 case DW_AT_byte_size:
11519 part_die->has_byte_size = 1;
11520 break;
11521 case DW_AT_calling_convention:
11522 /* DWARF doesn't provide a way to identify a program's source-level
11523 entry point. DW_AT_calling_convention attributes are only meant
11524 to describe functions' calling conventions.
11525
11526 However, because it's a necessary piece of information in
11527 Fortran, and because DW_CC_program is the only piece of debugging
11528 information whose definition refers to a 'main program' at all,
11529 several compilers have begun marking Fortran main programs with
11530 DW_CC_program --- even when those functions use the standard
11531 calling conventions.
11532
11533 So until DWARF specifies a way to provide this information and
11534 compilers pick up the new representation, we'll support this
11535 practice. */
11536 if (DW_UNSND (&attr) == DW_CC_program
11537 && cu->language == language_fortran)
11538 {
11539 set_main_name (part_die->name);
11540
11541 /* As this DIE has a static linkage the name would be difficult
11542 to look up later. */
11543 language_of_main = language_fortran;
11544 }
11545 break;
11546 case DW_AT_inline:
11547 if (DW_UNSND (&attr) == DW_INL_inlined
11548 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11549 part_die->may_be_inlined = 1;
11550 break;
11551
11552 case DW_AT_import:
11553 if (part_die->tag == DW_TAG_imported_unit)
11554 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11555 break;
11556
11557 default:
11558 break;
11559 }
11560 }
11561
11562 if (high_pc_relative)
11563 part_die->highpc += part_die->lowpc;
11564
11565 if (has_low_pc_attr && has_high_pc_attr)
11566 {
11567 /* When using the GNU linker, .gnu.linkonce. sections are used to
11568 eliminate duplicate copies of functions and vtables and such.
11569 The linker will arbitrarily choose one and discard the others.
11570 The AT_*_pc values for such functions refer to local labels in
11571 these sections. If the section from that file was discarded, the
11572 labels are not in the output, so the relocs get a value of 0.
11573 If this is a discarded function, mark the pc bounds as invalid,
11574 so that GDB will ignore it. */
11575 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11576 {
11577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11578
11579 complaint (&symfile_complaints,
11580 _("DW_AT_low_pc %s is zero "
11581 "for DIE at 0x%x [in module %s]"),
11582 paddress (gdbarch, part_die->lowpc),
11583 part_die->offset.sect_off, objfile->name);
11584 }
11585 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11586 else if (part_die->lowpc >= part_die->highpc)
11587 {
11588 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11589
11590 complaint (&symfile_complaints,
11591 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11592 "for DIE at 0x%x [in module %s]"),
11593 paddress (gdbarch, part_die->lowpc),
11594 paddress (gdbarch, part_die->highpc),
11595 part_die->offset.sect_off, objfile->name);
11596 }
11597 else
11598 part_die->has_pc_info = 1;
11599 }
11600
11601 return info_ptr;
11602 }
11603
11604 /* Find a cached partial DIE at OFFSET in CU. */
11605
11606 static struct partial_die_info *
11607 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11608 {
11609 struct partial_die_info *lookup_die = NULL;
11610 struct partial_die_info part_die;
11611
11612 part_die.offset = offset;
11613 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11614 offset.sect_off);
11615
11616 return lookup_die;
11617 }
11618
11619 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11620 except in the case of .debug_types DIEs which do not reference
11621 outside their CU (they do however referencing other types via
11622 DW_FORM_ref_sig8). */
11623
11624 static struct partial_die_info *
11625 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11626 {
11627 struct objfile *objfile = cu->objfile;
11628 struct dwarf2_per_cu_data *per_cu = NULL;
11629 struct partial_die_info *pd = NULL;
11630
11631 if (offset_in_cu_p (&cu->header, offset))
11632 {
11633 pd = find_partial_die_in_comp_unit (offset, cu);
11634 if (pd != NULL)
11635 return pd;
11636 /* We missed recording what we needed.
11637 Load all dies and try again. */
11638 per_cu = cu->per_cu;
11639 }
11640 else
11641 {
11642 /* TUs don't reference other CUs/TUs (except via type signatures). */
11643 if (cu->per_cu->is_debug_types)
11644 {
11645 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11646 " external reference to offset 0x%lx [in module %s].\n"),
11647 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11648 bfd_get_filename (objfile->obfd));
11649 }
11650 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11651
11652 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11653 load_partial_comp_unit (per_cu);
11654
11655 per_cu->cu->last_used = 0;
11656 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11657 }
11658
11659 /* If we didn't find it, and not all dies have been loaded,
11660 load them all and try again. */
11661
11662 if (pd == NULL && per_cu->load_all_dies == 0)
11663 {
11664 per_cu->load_all_dies = 1;
11665
11666 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11667 THIS_CU->cu may already be in use. So we can't just free it and
11668 replace its DIEs with the ones we read in. Instead, we leave those
11669 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11670 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11671 set. */
11672 load_partial_comp_unit (per_cu);
11673
11674 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11675 }
11676
11677 if (pd == NULL)
11678 internal_error (__FILE__, __LINE__,
11679 _("could not find partial DIE 0x%x "
11680 "in cache [from module %s]\n"),
11681 offset.sect_off, bfd_get_filename (objfile->obfd));
11682 return pd;
11683 }
11684
11685 /* See if we can figure out if the class lives in a namespace. We do
11686 this by looking for a member function; its demangled name will
11687 contain namespace info, if there is any. */
11688
11689 static void
11690 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11691 struct dwarf2_cu *cu)
11692 {
11693 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11694 what template types look like, because the demangler
11695 frequently doesn't give the same name as the debug info. We
11696 could fix this by only using the demangled name to get the
11697 prefix (but see comment in read_structure_type). */
11698
11699 struct partial_die_info *real_pdi;
11700 struct partial_die_info *child_pdi;
11701
11702 /* If this DIE (this DIE's specification, if any) has a parent, then
11703 we should not do this. We'll prepend the parent's fully qualified
11704 name when we create the partial symbol. */
11705
11706 real_pdi = struct_pdi;
11707 while (real_pdi->has_specification)
11708 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11709
11710 if (real_pdi->die_parent != NULL)
11711 return;
11712
11713 for (child_pdi = struct_pdi->die_child;
11714 child_pdi != NULL;
11715 child_pdi = child_pdi->die_sibling)
11716 {
11717 if (child_pdi->tag == DW_TAG_subprogram
11718 && child_pdi->linkage_name != NULL)
11719 {
11720 char *actual_class_name
11721 = language_class_name_from_physname (cu->language_defn,
11722 child_pdi->linkage_name);
11723 if (actual_class_name != NULL)
11724 {
11725 struct_pdi->name
11726 = obsavestring (actual_class_name,
11727 strlen (actual_class_name),
11728 &cu->objfile->objfile_obstack);
11729 xfree (actual_class_name);
11730 }
11731 break;
11732 }
11733 }
11734 }
11735
11736 /* Adjust PART_DIE before generating a symbol for it. This function
11737 may set the is_external flag or change the DIE's name. */
11738
11739 static void
11740 fixup_partial_die (struct partial_die_info *part_die,
11741 struct dwarf2_cu *cu)
11742 {
11743 /* Once we've fixed up a die, there's no point in doing so again.
11744 This also avoids a memory leak if we were to call
11745 guess_partial_die_structure_name multiple times. */
11746 if (part_die->fixup_called)
11747 return;
11748
11749 /* If we found a reference attribute and the DIE has no name, try
11750 to find a name in the referred to DIE. */
11751
11752 if (part_die->name == NULL && part_die->has_specification)
11753 {
11754 struct partial_die_info *spec_die;
11755
11756 spec_die = find_partial_die (part_die->spec_offset, cu);
11757
11758 fixup_partial_die (spec_die, cu);
11759
11760 if (spec_die->name)
11761 {
11762 part_die->name = spec_die->name;
11763
11764 /* Copy DW_AT_external attribute if it is set. */
11765 if (spec_die->is_external)
11766 part_die->is_external = spec_die->is_external;
11767 }
11768 }
11769
11770 /* Set default names for some unnamed DIEs. */
11771
11772 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11773 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11774
11775 /* If there is no parent die to provide a namespace, and there are
11776 children, see if we can determine the namespace from their linkage
11777 name. */
11778 if (cu->language == language_cplus
11779 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11780 && part_die->die_parent == NULL
11781 && part_die->has_children
11782 && (part_die->tag == DW_TAG_class_type
11783 || part_die->tag == DW_TAG_structure_type
11784 || part_die->tag == DW_TAG_union_type))
11785 guess_partial_die_structure_name (part_die, cu);
11786
11787 /* GCC might emit a nameless struct or union that has a linkage
11788 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11789 if (part_die->name == NULL
11790 && (part_die->tag == DW_TAG_class_type
11791 || part_die->tag == DW_TAG_interface_type
11792 || part_die->tag == DW_TAG_structure_type
11793 || part_die->tag == DW_TAG_union_type)
11794 && part_die->linkage_name != NULL)
11795 {
11796 char *demangled;
11797
11798 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11799 if (demangled)
11800 {
11801 const char *base;
11802
11803 /* Strip any leading namespaces/classes, keep only the base name.
11804 DW_AT_name for named DIEs does not contain the prefixes. */
11805 base = strrchr (demangled, ':');
11806 if (base && base > demangled && base[-1] == ':')
11807 base++;
11808 else
11809 base = demangled;
11810
11811 part_die->name = obsavestring (base, strlen (base),
11812 &cu->objfile->objfile_obstack);
11813 xfree (demangled);
11814 }
11815 }
11816
11817 part_die->fixup_called = 1;
11818 }
11819
11820 /* Read an attribute value described by an attribute form. */
11821
11822 static gdb_byte *
11823 read_attribute_value (const struct die_reader_specs *reader,
11824 struct attribute *attr, unsigned form,
11825 gdb_byte *info_ptr)
11826 {
11827 struct dwarf2_cu *cu = reader->cu;
11828 bfd *abfd = reader->abfd;
11829 struct comp_unit_head *cu_header = &cu->header;
11830 unsigned int bytes_read;
11831 struct dwarf_block *blk;
11832
11833 attr->form = form;
11834 switch (form)
11835 {
11836 case DW_FORM_ref_addr:
11837 if (cu->header.version == 2)
11838 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11839 else
11840 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11841 &cu->header, &bytes_read);
11842 info_ptr += bytes_read;
11843 break;
11844 case DW_FORM_addr:
11845 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11846 info_ptr += bytes_read;
11847 break;
11848 case DW_FORM_block2:
11849 blk = dwarf_alloc_block (cu);
11850 blk->size = read_2_bytes (abfd, info_ptr);
11851 info_ptr += 2;
11852 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11853 info_ptr += blk->size;
11854 DW_BLOCK (attr) = blk;
11855 break;
11856 case DW_FORM_block4:
11857 blk = dwarf_alloc_block (cu);
11858 blk->size = read_4_bytes (abfd, info_ptr);
11859 info_ptr += 4;
11860 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11861 info_ptr += blk->size;
11862 DW_BLOCK (attr) = blk;
11863 break;
11864 case DW_FORM_data2:
11865 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11866 info_ptr += 2;
11867 break;
11868 case DW_FORM_data4:
11869 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11870 info_ptr += 4;
11871 break;
11872 case DW_FORM_data8:
11873 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11874 info_ptr += 8;
11875 break;
11876 case DW_FORM_sec_offset:
11877 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11878 info_ptr += bytes_read;
11879 break;
11880 case DW_FORM_string:
11881 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11882 DW_STRING_IS_CANONICAL (attr) = 0;
11883 info_ptr += bytes_read;
11884 break;
11885 case DW_FORM_strp:
11886 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11887 &bytes_read);
11888 DW_STRING_IS_CANONICAL (attr) = 0;
11889 info_ptr += bytes_read;
11890 break;
11891 case DW_FORM_exprloc:
11892 case DW_FORM_block:
11893 blk = dwarf_alloc_block (cu);
11894 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11895 info_ptr += bytes_read;
11896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11897 info_ptr += blk->size;
11898 DW_BLOCK (attr) = blk;
11899 break;
11900 case DW_FORM_block1:
11901 blk = dwarf_alloc_block (cu);
11902 blk->size = read_1_byte (abfd, info_ptr);
11903 info_ptr += 1;
11904 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11905 info_ptr += blk->size;
11906 DW_BLOCK (attr) = blk;
11907 break;
11908 case DW_FORM_data1:
11909 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11910 info_ptr += 1;
11911 break;
11912 case DW_FORM_flag:
11913 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11914 info_ptr += 1;
11915 break;
11916 case DW_FORM_flag_present:
11917 DW_UNSND (attr) = 1;
11918 break;
11919 case DW_FORM_sdata:
11920 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11921 info_ptr += bytes_read;
11922 break;
11923 case DW_FORM_udata:
11924 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11925 info_ptr += bytes_read;
11926 break;
11927 case DW_FORM_ref1:
11928 DW_UNSND (attr) = (cu->header.offset.sect_off
11929 + read_1_byte (abfd, info_ptr));
11930 info_ptr += 1;
11931 break;
11932 case DW_FORM_ref2:
11933 DW_UNSND (attr) = (cu->header.offset.sect_off
11934 + read_2_bytes (abfd, info_ptr));
11935 info_ptr += 2;
11936 break;
11937 case DW_FORM_ref4:
11938 DW_UNSND (attr) = (cu->header.offset.sect_off
11939 + read_4_bytes (abfd, info_ptr));
11940 info_ptr += 4;
11941 break;
11942 case DW_FORM_ref8:
11943 DW_UNSND (attr) = (cu->header.offset.sect_off
11944 + read_8_bytes (abfd, info_ptr));
11945 info_ptr += 8;
11946 break;
11947 case DW_FORM_ref_sig8:
11948 /* Convert the signature to something we can record in DW_UNSND
11949 for later lookup.
11950 NOTE: This is NULL if the type wasn't found. */
11951 DW_SIGNATURED_TYPE (attr) =
11952 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11953 info_ptr += 8;
11954 break;
11955 case DW_FORM_ref_udata:
11956 DW_UNSND (attr) = (cu->header.offset.sect_off
11957 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11958 info_ptr += bytes_read;
11959 break;
11960 case DW_FORM_indirect:
11961 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11962 info_ptr += bytes_read;
11963 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11964 break;
11965 case DW_FORM_GNU_addr_index:
11966 if (reader->dwo_file == NULL)
11967 {
11968 /* For now flag a hard error.
11969 Later we can turn this into a complaint. */
11970 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11971 dwarf_form_name (form),
11972 bfd_get_filename (abfd));
11973 }
11974 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11975 info_ptr += bytes_read;
11976 break;
11977 case DW_FORM_GNU_str_index:
11978 if (reader->dwo_file == NULL)
11979 {
11980 /* For now flag a hard error.
11981 Later we can turn this into a complaint if warranted. */
11982 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11983 dwarf_form_name (form),
11984 bfd_get_filename (abfd));
11985 }
11986 {
11987 ULONGEST str_index =
11988 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11989
11990 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11991 DW_STRING_IS_CANONICAL (attr) = 0;
11992 info_ptr += bytes_read;
11993 }
11994 break;
11995 default:
11996 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11997 dwarf_form_name (form),
11998 bfd_get_filename (abfd));
11999 }
12000
12001 /* We have seen instances where the compiler tried to emit a byte
12002 size attribute of -1 which ended up being encoded as an unsigned
12003 0xffffffff. Although 0xffffffff is technically a valid size value,
12004 an object of this size seems pretty unlikely so we can relatively
12005 safely treat these cases as if the size attribute was invalid and
12006 treat them as zero by default. */
12007 if (attr->name == DW_AT_byte_size
12008 && form == DW_FORM_data4
12009 && DW_UNSND (attr) >= 0xffffffff)
12010 {
12011 complaint
12012 (&symfile_complaints,
12013 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12014 hex_string (DW_UNSND (attr)));
12015 DW_UNSND (attr) = 0;
12016 }
12017
12018 return info_ptr;
12019 }
12020
12021 /* Read an attribute described by an abbreviated attribute. */
12022
12023 static gdb_byte *
12024 read_attribute (const struct die_reader_specs *reader,
12025 struct attribute *attr, struct attr_abbrev *abbrev,
12026 gdb_byte *info_ptr)
12027 {
12028 attr->name = abbrev->name;
12029 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
12030 }
12031
12032 /* Read dwarf information from a buffer. */
12033
12034 static unsigned int
12035 read_1_byte (bfd *abfd, gdb_byte *buf)
12036 {
12037 return bfd_get_8 (abfd, buf);
12038 }
12039
12040 static int
12041 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
12042 {
12043 return bfd_get_signed_8 (abfd, buf);
12044 }
12045
12046 static unsigned int
12047 read_2_bytes (bfd *abfd, gdb_byte *buf)
12048 {
12049 return bfd_get_16 (abfd, buf);
12050 }
12051
12052 static int
12053 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12054 {
12055 return bfd_get_signed_16 (abfd, buf);
12056 }
12057
12058 static unsigned int
12059 read_4_bytes (bfd *abfd, gdb_byte *buf)
12060 {
12061 return bfd_get_32 (abfd, buf);
12062 }
12063
12064 static int
12065 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12066 {
12067 return bfd_get_signed_32 (abfd, buf);
12068 }
12069
12070 static ULONGEST
12071 read_8_bytes (bfd *abfd, gdb_byte *buf)
12072 {
12073 return bfd_get_64 (abfd, buf);
12074 }
12075
12076 static CORE_ADDR
12077 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
12078 unsigned int *bytes_read)
12079 {
12080 struct comp_unit_head *cu_header = &cu->header;
12081 CORE_ADDR retval = 0;
12082
12083 if (cu_header->signed_addr_p)
12084 {
12085 switch (cu_header->addr_size)
12086 {
12087 case 2:
12088 retval = bfd_get_signed_16 (abfd, buf);
12089 break;
12090 case 4:
12091 retval = bfd_get_signed_32 (abfd, buf);
12092 break;
12093 case 8:
12094 retval = bfd_get_signed_64 (abfd, buf);
12095 break;
12096 default:
12097 internal_error (__FILE__, __LINE__,
12098 _("read_address: bad switch, signed [in module %s]"),
12099 bfd_get_filename (abfd));
12100 }
12101 }
12102 else
12103 {
12104 switch (cu_header->addr_size)
12105 {
12106 case 2:
12107 retval = bfd_get_16 (abfd, buf);
12108 break;
12109 case 4:
12110 retval = bfd_get_32 (abfd, buf);
12111 break;
12112 case 8:
12113 retval = bfd_get_64 (abfd, buf);
12114 break;
12115 default:
12116 internal_error (__FILE__, __LINE__,
12117 _("read_address: bad switch, "
12118 "unsigned [in module %s]"),
12119 bfd_get_filename (abfd));
12120 }
12121 }
12122
12123 *bytes_read = cu_header->addr_size;
12124 return retval;
12125 }
12126
12127 /* Read the initial length from a section. The (draft) DWARF 3
12128 specification allows the initial length to take up either 4 bytes
12129 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12130 bytes describe the length and all offsets will be 8 bytes in length
12131 instead of 4.
12132
12133 An older, non-standard 64-bit format is also handled by this
12134 function. The older format in question stores the initial length
12135 as an 8-byte quantity without an escape value. Lengths greater
12136 than 2^32 aren't very common which means that the initial 4 bytes
12137 is almost always zero. Since a length value of zero doesn't make
12138 sense for the 32-bit format, this initial zero can be considered to
12139 be an escape value which indicates the presence of the older 64-bit
12140 format. As written, the code can't detect (old format) lengths
12141 greater than 4GB. If it becomes necessary to handle lengths
12142 somewhat larger than 4GB, we could allow other small values (such
12143 as the non-sensical values of 1, 2, and 3) to also be used as
12144 escape values indicating the presence of the old format.
12145
12146 The value returned via bytes_read should be used to increment the
12147 relevant pointer after calling read_initial_length().
12148
12149 [ Note: read_initial_length() and read_offset() are based on the
12150 document entitled "DWARF Debugging Information Format", revision
12151 3, draft 8, dated November 19, 2001. This document was obtained
12152 from:
12153
12154 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
12155
12156 This document is only a draft and is subject to change. (So beware.)
12157
12158 Details regarding the older, non-standard 64-bit format were
12159 determined empirically by examining 64-bit ELF files produced by
12160 the SGI toolchain on an IRIX 6.5 machine.
12161
12162 - Kevin, July 16, 2002
12163 ] */
12164
12165 static LONGEST
12166 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
12167 {
12168 LONGEST length = bfd_get_32 (abfd, buf);
12169
12170 if (length == 0xffffffff)
12171 {
12172 length = bfd_get_64 (abfd, buf + 4);
12173 *bytes_read = 12;
12174 }
12175 else if (length == 0)
12176 {
12177 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
12178 length = bfd_get_64 (abfd, buf);
12179 *bytes_read = 8;
12180 }
12181 else
12182 {
12183 *bytes_read = 4;
12184 }
12185
12186 return length;
12187 }
12188
12189 /* Cover function for read_initial_length.
12190 Returns the length of the object at BUF, and stores the size of the
12191 initial length in *BYTES_READ and stores the size that offsets will be in
12192 *OFFSET_SIZE.
12193 If the initial length size is not equivalent to that specified in
12194 CU_HEADER then issue a complaint.
12195 This is useful when reading non-comp-unit headers. */
12196
12197 static LONGEST
12198 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12199 const struct comp_unit_head *cu_header,
12200 unsigned int *bytes_read,
12201 unsigned int *offset_size)
12202 {
12203 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12204
12205 gdb_assert (cu_header->initial_length_size == 4
12206 || cu_header->initial_length_size == 8
12207 || cu_header->initial_length_size == 12);
12208
12209 if (cu_header->initial_length_size != *bytes_read)
12210 complaint (&symfile_complaints,
12211 _("intermixed 32-bit and 64-bit DWARF sections"));
12212
12213 *offset_size = (*bytes_read == 4) ? 4 : 8;
12214 return length;
12215 }
12216
12217 /* Read an offset from the data stream. The size of the offset is
12218 given by cu_header->offset_size. */
12219
12220 static LONGEST
12221 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12222 unsigned int *bytes_read)
12223 {
12224 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12225
12226 *bytes_read = cu_header->offset_size;
12227 return offset;
12228 }
12229
12230 /* Read an offset from the data stream. */
12231
12232 static LONGEST
12233 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12234 {
12235 LONGEST retval = 0;
12236
12237 switch (offset_size)
12238 {
12239 case 4:
12240 retval = bfd_get_32 (abfd, buf);
12241 break;
12242 case 8:
12243 retval = bfd_get_64 (abfd, buf);
12244 break;
12245 default:
12246 internal_error (__FILE__, __LINE__,
12247 _("read_offset_1: bad switch [in module %s]"),
12248 bfd_get_filename (abfd));
12249 }
12250
12251 return retval;
12252 }
12253
12254 static gdb_byte *
12255 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12256 {
12257 /* If the size of a host char is 8 bits, we can return a pointer
12258 to the buffer, otherwise we have to copy the data to a buffer
12259 allocated on the temporary obstack. */
12260 gdb_assert (HOST_CHAR_BIT == 8);
12261 return buf;
12262 }
12263
12264 static char *
12265 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12266 {
12267 /* If the size of a host char is 8 bits, we can return a pointer
12268 to the string, otherwise we have to copy the string to a buffer
12269 allocated on the temporary obstack. */
12270 gdb_assert (HOST_CHAR_BIT == 8);
12271 if (*buf == '\0')
12272 {
12273 *bytes_read_ptr = 1;
12274 return NULL;
12275 }
12276 *bytes_read_ptr = strlen ((char *) buf) + 1;
12277 return (char *) buf;
12278 }
12279
12280 static char *
12281 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12282 {
12283 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12284 if (dwarf2_per_objfile->str.buffer == NULL)
12285 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12286 bfd_get_filename (abfd));
12287 if (str_offset >= dwarf2_per_objfile->str.size)
12288 error (_("DW_FORM_strp pointing outside of "
12289 ".debug_str section [in module %s]"),
12290 bfd_get_filename (abfd));
12291 gdb_assert (HOST_CHAR_BIT == 8);
12292 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12293 return NULL;
12294 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12295 }
12296
12297 static char *
12298 read_indirect_string (bfd *abfd, gdb_byte *buf,
12299 const struct comp_unit_head *cu_header,
12300 unsigned int *bytes_read_ptr)
12301 {
12302 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12303
12304 return read_indirect_string_at_offset (abfd, str_offset);
12305 }
12306
12307 static ULONGEST
12308 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12309 {
12310 ULONGEST result;
12311 unsigned int num_read;
12312 int i, shift;
12313 unsigned char byte;
12314
12315 result = 0;
12316 shift = 0;
12317 num_read = 0;
12318 i = 0;
12319 while (1)
12320 {
12321 byte = bfd_get_8 (abfd, buf);
12322 buf++;
12323 num_read++;
12324 result |= ((ULONGEST) (byte & 127) << shift);
12325 if ((byte & 128) == 0)
12326 {
12327 break;
12328 }
12329 shift += 7;
12330 }
12331 *bytes_read_ptr = num_read;
12332 return result;
12333 }
12334
12335 static LONGEST
12336 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12337 {
12338 LONGEST result;
12339 int i, shift, num_read;
12340 unsigned char byte;
12341
12342 result = 0;
12343 shift = 0;
12344 num_read = 0;
12345 i = 0;
12346 while (1)
12347 {
12348 byte = bfd_get_8 (abfd, buf);
12349 buf++;
12350 num_read++;
12351 result |= ((LONGEST) (byte & 127) << shift);
12352 shift += 7;
12353 if ((byte & 128) == 0)
12354 {
12355 break;
12356 }
12357 }
12358 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12359 result |= -(((LONGEST) 1) << shift);
12360 *bytes_read_ptr = num_read;
12361 return result;
12362 }
12363
12364 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12365 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12366 ADDR_SIZE is the size of addresses from the CU header. */
12367
12368 static CORE_ADDR
12369 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12370 {
12371 struct objfile *objfile = dwarf2_per_objfile->objfile;
12372 bfd *abfd = objfile->obfd;
12373 const gdb_byte *info_ptr;
12374
12375 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12376 if (dwarf2_per_objfile->addr.buffer == NULL)
12377 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12378 objfile->name);
12379 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12380 error (_("DW_FORM_addr_index pointing outside of "
12381 ".debug_addr section [in module %s]"),
12382 objfile->name);
12383 info_ptr = (dwarf2_per_objfile->addr.buffer
12384 + addr_base + addr_index * addr_size);
12385 if (addr_size == 4)
12386 return bfd_get_32 (abfd, info_ptr);
12387 else
12388 return bfd_get_64 (abfd, info_ptr);
12389 }
12390
12391 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12392
12393 static CORE_ADDR
12394 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12395 {
12396 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12397 }
12398
12399 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12400
12401 static CORE_ADDR
12402 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12403 unsigned int *bytes_read)
12404 {
12405 bfd *abfd = cu->objfile->obfd;
12406 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12407
12408 return read_addr_index (cu, addr_index);
12409 }
12410
12411 /* Data structure to pass results from dwarf2_read_addr_index_reader
12412 back to dwarf2_read_addr_index. */
12413
12414 struct dwarf2_read_addr_index_data
12415 {
12416 ULONGEST addr_base;
12417 int addr_size;
12418 };
12419
12420 /* die_reader_func for dwarf2_read_addr_index. */
12421
12422 static void
12423 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12424 gdb_byte *info_ptr,
12425 struct die_info *comp_unit_die,
12426 int has_children,
12427 void *data)
12428 {
12429 struct dwarf2_cu *cu = reader->cu;
12430 struct dwarf2_read_addr_index_data *aidata =
12431 (struct dwarf2_read_addr_index_data *) data;
12432
12433 aidata->addr_base = cu->addr_base;
12434 aidata->addr_size = cu->header.addr_size;
12435 }
12436
12437 /* Given an index in .debug_addr, fetch the value.
12438 NOTE: This can be called during dwarf expression evaluation,
12439 long after the debug information has been read, and thus per_cu->cu
12440 may no longer exist. */
12441
12442 CORE_ADDR
12443 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12444 unsigned int addr_index)
12445 {
12446 struct objfile *objfile = per_cu->objfile;
12447 struct dwarf2_cu *cu = per_cu->cu;
12448 ULONGEST addr_base;
12449 int addr_size;
12450
12451 /* This is intended to be called from outside this file. */
12452 dw2_setup (objfile);
12453
12454 /* We need addr_base and addr_size.
12455 If we don't have PER_CU->cu, we have to get it.
12456 Nasty, but the alternative is storing the needed info in PER_CU,
12457 which at this point doesn't seem justified: it's not clear how frequently
12458 it would get used and it would increase the size of every PER_CU.
12459 Entry points like dwarf2_per_cu_addr_size do a similar thing
12460 so we're not in uncharted territory here.
12461 Alas we need to be a bit more complicated as addr_base is contained
12462 in the DIE.
12463
12464 We don't need to read the entire CU(/TU).
12465 We just need the header and top level die.
12466 IWBN to use the aging mechanism to let us lazily later discard the CU.
12467 See however init_cutu_and_read_dies_simple. */
12468
12469 if (cu != NULL)
12470 {
12471 addr_base = cu->addr_base;
12472 addr_size = cu->header.addr_size;
12473 }
12474 else
12475 {
12476 struct dwarf2_read_addr_index_data aidata;
12477
12478 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12479 &aidata);
12480 addr_base = aidata.addr_base;
12481 addr_size = aidata.addr_size;
12482 }
12483
12484 return read_addr_index_1 (addr_index, addr_base, addr_size);
12485 }
12486
12487 /* Given a DW_AT_str_index, fetch the string. */
12488
12489 static char *
12490 read_str_index (const struct die_reader_specs *reader,
12491 struct dwarf2_cu *cu, ULONGEST str_index)
12492 {
12493 struct objfile *objfile = dwarf2_per_objfile->objfile;
12494 const char *dwo_name = objfile->name;
12495 bfd *abfd = objfile->obfd;
12496 struct dwo_sections *sections = &reader->dwo_file->sections;
12497 gdb_byte *info_ptr;
12498 ULONGEST str_offset;
12499
12500 dwarf2_read_section (objfile, &sections->str);
12501 dwarf2_read_section (objfile, &sections->str_offsets);
12502 if (sections->str.buffer == NULL)
12503 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12504 " in CU at offset 0x%lx [in module %s]"),
12505 (long) cu->header.offset.sect_off, dwo_name);
12506 if (sections->str_offsets.buffer == NULL)
12507 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12508 " in CU at offset 0x%lx [in module %s]"),
12509 (long) cu->header.offset.sect_off, dwo_name);
12510 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12511 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12512 " section in CU at offset 0x%lx [in module %s]"),
12513 (long) cu->header.offset.sect_off, dwo_name);
12514 info_ptr = (sections->str_offsets.buffer
12515 + str_index * cu->header.offset_size);
12516 if (cu->header.offset_size == 4)
12517 str_offset = bfd_get_32 (abfd, info_ptr);
12518 else
12519 str_offset = bfd_get_64 (abfd, info_ptr);
12520 if (str_offset >= sections->str.size)
12521 error (_("Offset from DW_FORM_str_index pointing outside of"
12522 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12523 (long) cu->header.offset.sect_off, dwo_name);
12524 return (char *) (sections->str.buffer + str_offset);
12525 }
12526
12527 /* Return the length of an LEB128 number in BUF. */
12528
12529 static int
12530 leb128_size (const gdb_byte *buf)
12531 {
12532 const gdb_byte *begin = buf;
12533 gdb_byte byte;
12534
12535 while (1)
12536 {
12537 byte = *buf++;
12538 if ((byte & 128) == 0)
12539 return buf - begin;
12540 }
12541 }
12542
12543 static void
12544 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12545 {
12546 switch (lang)
12547 {
12548 case DW_LANG_C89:
12549 case DW_LANG_C99:
12550 case DW_LANG_C:
12551 cu->language = language_c;
12552 break;
12553 case DW_LANG_C_plus_plus:
12554 cu->language = language_cplus;
12555 break;
12556 case DW_LANG_D:
12557 cu->language = language_d;
12558 break;
12559 case DW_LANG_Fortran77:
12560 case DW_LANG_Fortran90:
12561 case DW_LANG_Fortran95:
12562 cu->language = language_fortran;
12563 break;
12564 case DW_LANG_Go:
12565 cu->language = language_go;
12566 break;
12567 case DW_LANG_Mips_Assembler:
12568 cu->language = language_asm;
12569 break;
12570 case DW_LANG_Java:
12571 cu->language = language_java;
12572 break;
12573 case DW_LANG_Ada83:
12574 case DW_LANG_Ada95:
12575 cu->language = language_ada;
12576 break;
12577 case DW_LANG_Modula2:
12578 cu->language = language_m2;
12579 break;
12580 case DW_LANG_Pascal83:
12581 cu->language = language_pascal;
12582 break;
12583 case DW_LANG_ObjC:
12584 cu->language = language_objc;
12585 break;
12586 case DW_LANG_Cobol74:
12587 case DW_LANG_Cobol85:
12588 default:
12589 cu->language = language_minimal;
12590 break;
12591 }
12592 cu->language_defn = language_def (cu->language);
12593 }
12594
12595 /* Return the named attribute or NULL if not there. */
12596
12597 static struct attribute *
12598 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12599 {
12600 for (;;)
12601 {
12602 unsigned int i;
12603 struct attribute *spec = NULL;
12604
12605 for (i = 0; i < die->num_attrs; ++i)
12606 {
12607 if (die->attrs[i].name == name)
12608 return &die->attrs[i];
12609 if (die->attrs[i].name == DW_AT_specification
12610 || die->attrs[i].name == DW_AT_abstract_origin)
12611 spec = &die->attrs[i];
12612 }
12613
12614 if (!spec)
12615 break;
12616
12617 die = follow_die_ref (die, spec, &cu);
12618 }
12619
12620 return NULL;
12621 }
12622
12623 /* Return the named attribute or NULL if not there,
12624 but do not follow DW_AT_specification, etc.
12625 This is for use in contexts where we're reading .debug_types dies.
12626 Following DW_AT_specification, DW_AT_abstract_origin will take us
12627 back up the chain, and we want to go down. */
12628
12629 static struct attribute *
12630 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12631 struct dwarf2_cu *cu)
12632 {
12633 unsigned int i;
12634
12635 for (i = 0; i < die->num_attrs; ++i)
12636 if (die->attrs[i].name == name)
12637 return &die->attrs[i];
12638
12639 return NULL;
12640 }
12641
12642 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12643 and holds a non-zero value. This function should only be used for
12644 DW_FORM_flag or DW_FORM_flag_present attributes. */
12645
12646 static int
12647 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12648 {
12649 struct attribute *attr = dwarf2_attr (die, name, cu);
12650
12651 return (attr && DW_UNSND (attr));
12652 }
12653
12654 static int
12655 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12656 {
12657 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12658 which value is non-zero. However, we have to be careful with
12659 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12660 (via dwarf2_flag_true_p) follows this attribute. So we may
12661 end up accidently finding a declaration attribute that belongs
12662 to a different DIE referenced by the specification attribute,
12663 even though the given DIE does not have a declaration attribute. */
12664 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12665 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12666 }
12667
12668 /* Return the die giving the specification for DIE, if there is
12669 one. *SPEC_CU is the CU containing DIE on input, and the CU
12670 containing the return value on output. If there is no
12671 specification, but there is an abstract origin, that is
12672 returned. */
12673
12674 static struct die_info *
12675 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12676 {
12677 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12678 *spec_cu);
12679
12680 if (spec_attr == NULL)
12681 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12682
12683 if (spec_attr == NULL)
12684 return NULL;
12685 else
12686 return follow_die_ref (die, spec_attr, spec_cu);
12687 }
12688
12689 /* Free the line_header structure *LH, and any arrays and strings it
12690 refers to.
12691 NOTE: This is also used as a "cleanup" function. */
12692
12693 static void
12694 free_line_header (struct line_header *lh)
12695 {
12696 if (lh->standard_opcode_lengths)
12697 xfree (lh->standard_opcode_lengths);
12698
12699 /* Remember that all the lh->file_names[i].name pointers are
12700 pointers into debug_line_buffer, and don't need to be freed. */
12701 if (lh->file_names)
12702 xfree (lh->file_names);
12703
12704 /* Similarly for the include directory names. */
12705 if (lh->include_dirs)
12706 xfree (lh->include_dirs);
12707
12708 xfree (lh);
12709 }
12710
12711 /* Add an entry to LH's include directory table. */
12712
12713 static void
12714 add_include_dir (struct line_header *lh, char *include_dir)
12715 {
12716 /* Grow the array if necessary. */
12717 if (lh->include_dirs_size == 0)
12718 {
12719 lh->include_dirs_size = 1; /* for testing */
12720 lh->include_dirs = xmalloc (lh->include_dirs_size
12721 * sizeof (*lh->include_dirs));
12722 }
12723 else if (lh->num_include_dirs >= lh->include_dirs_size)
12724 {
12725 lh->include_dirs_size *= 2;
12726 lh->include_dirs = xrealloc (lh->include_dirs,
12727 (lh->include_dirs_size
12728 * sizeof (*lh->include_dirs)));
12729 }
12730
12731 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12732 }
12733
12734 /* Add an entry to LH's file name table. */
12735
12736 static void
12737 add_file_name (struct line_header *lh,
12738 char *name,
12739 unsigned int dir_index,
12740 unsigned int mod_time,
12741 unsigned int length)
12742 {
12743 struct file_entry *fe;
12744
12745 /* Grow the array if necessary. */
12746 if (lh->file_names_size == 0)
12747 {
12748 lh->file_names_size = 1; /* for testing */
12749 lh->file_names = xmalloc (lh->file_names_size
12750 * sizeof (*lh->file_names));
12751 }
12752 else if (lh->num_file_names >= lh->file_names_size)
12753 {
12754 lh->file_names_size *= 2;
12755 lh->file_names = xrealloc (lh->file_names,
12756 (lh->file_names_size
12757 * sizeof (*lh->file_names)));
12758 }
12759
12760 fe = &lh->file_names[lh->num_file_names++];
12761 fe->name = name;
12762 fe->dir_index = dir_index;
12763 fe->mod_time = mod_time;
12764 fe->length = length;
12765 fe->included_p = 0;
12766 fe->symtab = NULL;
12767 }
12768
12769 /* Read the statement program header starting at OFFSET in
12770 .debug_line, or .debug_line.dwo. Return a pointer
12771 to a struct line_header, allocated using xmalloc.
12772
12773 NOTE: the strings in the include directory and file name tables of
12774 the returned object point into the dwarf line section buffer,
12775 and must not be freed. */
12776
12777 static struct line_header *
12778 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12779 {
12780 struct cleanup *back_to;
12781 struct line_header *lh;
12782 gdb_byte *line_ptr;
12783 unsigned int bytes_read, offset_size;
12784 int i;
12785 char *cur_dir, *cur_file;
12786 struct dwarf2_section_info *section;
12787 bfd *abfd;
12788
12789 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12790 DWO file. */
12791 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12792 section = &cu->dwo_unit->dwo_file->sections.line;
12793 else
12794 section = &dwarf2_per_objfile->line;
12795
12796 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12797 if (section->buffer == NULL)
12798 {
12799 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12800 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12801 else
12802 complaint (&symfile_complaints, _("missing .debug_line section"));
12803 return 0;
12804 }
12805
12806 /* We can't do this until we know the section is non-empty.
12807 Only then do we know we have such a section. */
12808 abfd = section->asection->owner;
12809
12810 /* Make sure that at least there's room for the total_length field.
12811 That could be 12 bytes long, but we're just going to fudge that. */
12812 if (offset + 4 >= section->size)
12813 {
12814 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12815 return 0;
12816 }
12817
12818 lh = xmalloc (sizeof (*lh));
12819 memset (lh, 0, sizeof (*lh));
12820 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12821 (void *) lh);
12822
12823 line_ptr = section->buffer + offset;
12824
12825 /* Read in the header. */
12826 lh->total_length =
12827 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12828 &bytes_read, &offset_size);
12829 line_ptr += bytes_read;
12830 if (line_ptr + lh->total_length > (section->buffer + section->size))
12831 {
12832 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12833 return 0;
12834 }
12835 lh->statement_program_end = line_ptr + lh->total_length;
12836 lh->version = read_2_bytes (abfd, line_ptr);
12837 line_ptr += 2;
12838 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12839 line_ptr += offset_size;
12840 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12841 line_ptr += 1;
12842 if (lh->version >= 4)
12843 {
12844 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12845 line_ptr += 1;
12846 }
12847 else
12848 lh->maximum_ops_per_instruction = 1;
12849
12850 if (lh->maximum_ops_per_instruction == 0)
12851 {
12852 lh->maximum_ops_per_instruction = 1;
12853 complaint (&symfile_complaints,
12854 _("invalid maximum_ops_per_instruction "
12855 "in `.debug_line' section"));
12856 }
12857
12858 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12859 line_ptr += 1;
12860 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12861 line_ptr += 1;
12862 lh->line_range = read_1_byte (abfd, line_ptr);
12863 line_ptr += 1;
12864 lh->opcode_base = read_1_byte (abfd, line_ptr);
12865 line_ptr += 1;
12866 lh->standard_opcode_lengths
12867 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12868
12869 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12870 for (i = 1; i < lh->opcode_base; ++i)
12871 {
12872 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12873 line_ptr += 1;
12874 }
12875
12876 /* Read directory table. */
12877 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12878 {
12879 line_ptr += bytes_read;
12880 add_include_dir (lh, cur_dir);
12881 }
12882 line_ptr += bytes_read;
12883
12884 /* Read file name table. */
12885 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12886 {
12887 unsigned int dir_index, mod_time, length;
12888
12889 line_ptr += bytes_read;
12890 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12891 line_ptr += bytes_read;
12892 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12893 line_ptr += bytes_read;
12894 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12895 line_ptr += bytes_read;
12896
12897 add_file_name (lh, cur_file, dir_index, mod_time, length);
12898 }
12899 line_ptr += bytes_read;
12900 lh->statement_program_start = line_ptr;
12901
12902 if (line_ptr > (section->buffer + section->size))
12903 complaint (&symfile_complaints,
12904 _("line number info header doesn't "
12905 "fit in `.debug_line' section"));
12906
12907 discard_cleanups (back_to);
12908 return lh;
12909 }
12910
12911 /* Subroutine of dwarf_decode_lines to simplify it.
12912 Return the file name of the psymtab for included file FILE_INDEX
12913 in line header LH of PST.
12914 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12915 If space for the result is malloc'd, it will be freed by a cleanup.
12916 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12917
12918 static char *
12919 psymtab_include_file_name (const struct line_header *lh, int file_index,
12920 const struct partial_symtab *pst,
12921 const char *comp_dir)
12922 {
12923 const struct file_entry fe = lh->file_names [file_index];
12924 char *include_name = fe.name;
12925 char *include_name_to_compare = include_name;
12926 char *dir_name = NULL;
12927 const char *pst_filename;
12928 char *copied_name = NULL;
12929 int file_is_pst;
12930
12931 if (fe.dir_index)
12932 dir_name = lh->include_dirs[fe.dir_index - 1];
12933
12934 if (!IS_ABSOLUTE_PATH (include_name)
12935 && (dir_name != NULL || comp_dir != NULL))
12936 {
12937 /* Avoid creating a duplicate psymtab for PST.
12938 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12939 Before we do the comparison, however, we need to account
12940 for DIR_NAME and COMP_DIR.
12941 First prepend dir_name (if non-NULL). If we still don't
12942 have an absolute path prepend comp_dir (if non-NULL).
12943 However, the directory we record in the include-file's
12944 psymtab does not contain COMP_DIR (to match the
12945 corresponding symtab(s)).
12946
12947 Example:
12948
12949 bash$ cd /tmp
12950 bash$ gcc -g ./hello.c
12951 include_name = "hello.c"
12952 dir_name = "."
12953 DW_AT_comp_dir = comp_dir = "/tmp"
12954 DW_AT_name = "./hello.c" */
12955
12956 if (dir_name != NULL)
12957 {
12958 include_name = concat (dir_name, SLASH_STRING,
12959 include_name, (char *)NULL);
12960 include_name_to_compare = include_name;
12961 make_cleanup (xfree, include_name);
12962 }
12963 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12964 {
12965 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12966 include_name, (char *)NULL);
12967 }
12968 }
12969
12970 pst_filename = pst->filename;
12971 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12972 {
12973 copied_name = concat (pst->dirname, SLASH_STRING,
12974 pst_filename, (char *)NULL);
12975 pst_filename = copied_name;
12976 }
12977
12978 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12979
12980 if (include_name_to_compare != include_name)
12981 xfree (include_name_to_compare);
12982 if (copied_name != NULL)
12983 xfree (copied_name);
12984
12985 if (file_is_pst)
12986 return NULL;
12987 return include_name;
12988 }
12989
12990 /* Ignore this record_line request. */
12991
12992 static void
12993 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12994 {
12995 return;
12996 }
12997
12998 /* Subroutine of dwarf_decode_lines to simplify it.
12999 Process the line number information in LH. */
13000
13001 static void
13002 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13003 struct dwarf2_cu *cu, struct partial_symtab *pst)
13004 {
13005 gdb_byte *line_ptr, *extended_end;
13006 gdb_byte *line_end;
13007 unsigned int bytes_read, extended_len;
13008 unsigned char op_code, extended_op, adj_opcode;
13009 CORE_ADDR baseaddr;
13010 struct objfile *objfile = cu->objfile;
13011 bfd *abfd = objfile->obfd;
13012 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13013 const int decode_for_pst_p = (pst != NULL);
13014 struct subfile *last_subfile = NULL;
13015 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13016 = record_line;
13017
13018 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13019
13020 line_ptr = lh->statement_program_start;
13021 line_end = lh->statement_program_end;
13022
13023 /* Read the statement sequences until there's nothing left. */
13024 while (line_ptr < line_end)
13025 {
13026 /* state machine registers */
13027 CORE_ADDR address = 0;
13028 unsigned int file = 1;
13029 unsigned int line = 1;
13030 unsigned int column = 0;
13031 int is_stmt = lh->default_is_stmt;
13032 int basic_block = 0;
13033 int end_sequence = 0;
13034 CORE_ADDR addr;
13035 unsigned char op_index = 0;
13036
13037 if (!decode_for_pst_p && lh->num_file_names >= file)
13038 {
13039 /* Start a subfile for the current file of the state machine. */
13040 /* lh->include_dirs and lh->file_names are 0-based, but the
13041 directory and file name numbers in the statement program
13042 are 1-based. */
13043 struct file_entry *fe = &lh->file_names[file - 1];
13044 char *dir = NULL;
13045
13046 if (fe->dir_index)
13047 dir = lh->include_dirs[fe->dir_index - 1];
13048
13049 dwarf2_start_subfile (fe->name, dir, comp_dir);
13050 }
13051
13052 /* Decode the table. */
13053 while (!end_sequence)
13054 {
13055 op_code = read_1_byte (abfd, line_ptr);
13056 line_ptr += 1;
13057 if (line_ptr > line_end)
13058 {
13059 dwarf2_debug_line_missing_end_sequence_complaint ();
13060 break;
13061 }
13062
13063 if (op_code >= lh->opcode_base)
13064 {
13065 /* Special operand. */
13066 adj_opcode = op_code - lh->opcode_base;
13067 address += (((op_index + (adj_opcode / lh->line_range))
13068 / lh->maximum_ops_per_instruction)
13069 * lh->minimum_instruction_length);
13070 op_index = ((op_index + (adj_opcode / lh->line_range))
13071 % lh->maximum_ops_per_instruction);
13072 line += lh->line_base + (adj_opcode % lh->line_range);
13073 if (lh->num_file_names < file || file == 0)
13074 dwarf2_debug_line_missing_file_complaint ();
13075 /* For now we ignore lines not starting on an
13076 instruction boundary. */
13077 else if (op_index == 0)
13078 {
13079 lh->file_names[file - 1].included_p = 1;
13080 if (!decode_for_pst_p && is_stmt)
13081 {
13082 if (last_subfile != current_subfile)
13083 {
13084 addr = gdbarch_addr_bits_remove (gdbarch, address);
13085 if (last_subfile)
13086 (*p_record_line) (last_subfile, 0, addr);
13087 last_subfile = current_subfile;
13088 }
13089 /* Append row to matrix using current values. */
13090 addr = gdbarch_addr_bits_remove (gdbarch, address);
13091 (*p_record_line) (current_subfile, line, addr);
13092 }
13093 }
13094 basic_block = 0;
13095 }
13096 else switch (op_code)
13097 {
13098 case DW_LNS_extended_op:
13099 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13100 &bytes_read);
13101 line_ptr += bytes_read;
13102 extended_end = line_ptr + extended_len;
13103 extended_op = read_1_byte (abfd, line_ptr);
13104 line_ptr += 1;
13105 switch (extended_op)
13106 {
13107 case DW_LNE_end_sequence:
13108 p_record_line = record_line;
13109 end_sequence = 1;
13110 break;
13111 case DW_LNE_set_address:
13112 address = read_address (abfd, line_ptr, cu, &bytes_read);
13113
13114 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13115 {
13116 /* This line table is for a function which has been
13117 GCd by the linker. Ignore it. PR gdb/12528 */
13118
13119 long line_offset
13120 = line_ptr - dwarf2_per_objfile->line.buffer;
13121
13122 complaint (&symfile_complaints,
13123 _(".debug_line address at offset 0x%lx is 0 "
13124 "[in module %s]"),
13125 line_offset, objfile->name);
13126 p_record_line = noop_record_line;
13127 }
13128
13129 op_index = 0;
13130 line_ptr += bytes_read;
13131 address += baseaddr;
13132 break;
13133 case DW_LNE_define_file:
13134 {
13135 char *cur_file;
13136 unsigned int dir_index, mod_time, length;
13137
13138 cur_file = read_direct_string (abfd, line_ptr,
13139 &bytes_read);
13140 line_ptr += bytes_read;
13141 dir_index =
13142 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13143 line_ptr += bytes_read;
13144 mod_time =
13145 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13146 line_ptr += bytes_read;
13147 length =
13148 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13149 line_ptr += bytes_read;
13150 add_file_name (lh, cur_file, dir_index, mod_time, length);
13151 }
13152 break;
13153 case DW_LNE_set_discriminator:
13154 /* The discriminator is not interesting to the debugger;
13155 just ignore it. */
13156 line_ptr = extended_end;
13157 break;
13158 default:
13159 complaint (&symfile_complaints,
13160 _("mangled .debug_line section"));
13161 return;
13162 }
13163 /* Make sure that we parsed the extended op correctly. If e.g.
13164 we expected a different address size than the producer used,
13165 we may have read the wrong number of bytes. */
13166 if (line_ptr != extended_end)
13167 {
13168 complaint (&symfile_complaints,
13169 _("mangled .debug_line section"));
13170 return;
13171 }
13172 break;
13173 case DW_LNS_copy:
13174 if (lh->num_file_names < file || file == 0)
13175 dwarf2_debug_line_missing_file_complaint ();
13176 else
13177 {
13178 lh->file_names[file - 1].included_p = 1;
13179 if (!decode_for_pst_p && is_stmt)
13180 {
13181 if (last_subfile != current_subfile)
13182 {
13183 addr = gdbarch_addr_bits_remove (gdbarch, address);
13184 if (last_subfile)
13185 (*p_record_line) (last_subfile, 0, addr);
13186 last_subfile = current_subfile;
13187 }
13188 addr = gdbarch_addr_bits_remove (gdbarch, address);
13189 (*p_record_line) (current_subfile, line, addr);
13190 }
13191 }
13192 basic_block = 0;
13193 break;
13194 case DW_LNS_advance_pc:
13195 {
13196 CORE_ADDR adjust
13197 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13198
13199 address += (((op_index + adjust)
13200 / lh->maximum_ops_per_instruction)
13201 * lh->minimum_instruction_length);
13202 op_index = ((op_index + adjust)
13203 % lh->maximum_ops_per_instruction);
13204 line_ptr += bytes_read;
13205 }
13206 break;
13207 case DW_LNS_advance_line:
13208 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13209 line_ptr += bytes_read;
13210 break;
13211 case DW_LNS_set_file:
13212 {
13213 /* The arrays lh->include_dirs and lh->file_names are
13214 0-based, but the directory and file name numbers in
13215 the statement program are 1-based. */
13216 struct file_entry *fe;
13217 char *dir = NULL;
13218
13219 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13220 line_ptr += bytes_read;
13221 if (lh->num_file_names < file || file == 0)
13222 dwarf2_debug_line_missing_file_complaint ();
13223 else
13224 {
13225 fe = &lh->file_names[file - 1];
13226 if (fe->dir_index)
13227 dir = lh->include_dirs[fe->dir_index - 1];
13228 if (!decode_for_pst_p)
13229 {
13230 last_subfile = current_subfile;
13231 dwarf2_start_subfile (fe->name, dir, comp_dir);
13232 }
13233 }
13234 }
13235 break;
13236 case DW_LNS_set_column:
13237 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13238 line_ptr += bytes_read;
13239 break;
13240 case DW_LNS_negate_stmt:
13241 is_stmt = (!is_stmt);
13242 break;
13243 case DW_LNS_set_basic_block:
13244 basic_block = 1;
13245 break;
13246 /* Add to the address register of the state machine the
13247 address increment value corresponding to special opcode
13248 255. I.e., this value is scaled by the minimum
13249 instruction length since special opcode 255 would have
13250 scaled the increment. */
13251 case DW_LNS_const_add_pc:
13252 {
13253 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13254
13255 address += (((op_index + adjust)
13256 / lh->maximum_ops_per_instruction)
13257 * lh->minimum_instruction_length);
13258 op_index = ((op_index + adjust)
13259 % lh->maximum_ops_per_instruction);
13260 }
13261 break;
13262 case DW_LNS_fixed_advance_pc:
13263 address += read_2_bytes (abfd, line_ptr);
13264 op_index = 0;
13265 line_ptr += 2;
13266 break;
13267 default:
13268 {
13269 /* Unknown standard opcode, ignore it. */
13270 int i;
13271
13272 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13273 {
13274 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13275 line_ptr += bytes_read;
13276 }
13277 }
13278 }
13279 }
13280 if (lh->num_file_names < file || file == 0)
13281 dwarf2_debug_line_missing_file_complaint ();
13282 else
13283 {
13284 lh->file_names[file - 1].included_p = 1;
13285 if (!decode_for_pst_p)
13286 {
13287 addr = gdbarch_addr_bits_remove (gdbarch, address);
13288 (*p_record_line) (current_subfile, 0, addr);
13289 }
13290 }
13291 }
13292 }
13293
13294 /* Decode the Line Number Program (LNP) for the given line_header
13295 structure and CU. The actual information extracted and the type
13296 of structures created from the LNP depends on the value of PST.
13297
13298 1. If PST is NULL, then this procedure uses the data from the program
13299 to create all necessary symbol tables, and their linetables.
13300
13301 2. If PST is not NULL, this procedure reads the program to determine
13302 the list of files included by the unit represented by PST, and
13303 builds all the associated partial symbol tables.
13304
13305 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13306 It is used for relative paths in the line table.
13307 NOTE: When processing partial symtabs (pst != NULL),
13308 comp_dir == pst->dirname.
13309
13310 NOTE: It is important that psymtabs have the same file name (via strcmp)
13311 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13312 symtab we don't use it in the name of the psymtabs we create.
13313 E.g. expand_line_sal requires this when finding psymtabs to expand.
13314 A good testcase for this is mb-inline.exp. */
13315
13316 static void
13317 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13318 struct dwarf2_cu *cu, struct partial_symtab *pst,
13319 int want_line_info)
13320 {
13321 struct objfile *objfile = cu->objfile;
13322 const int decode_for_pst_p = (pst != NULL);
13323 struct subfile *first_subfile = current_subfile;
13324
13325 if (want_line_info)
13326 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13327
13328 if (decode_for_pst_p)
13329 {
13330 int file_index;
13331
13332 /* Now that we're done scanning the Line Header Program, we can
13333 create the psymtab of each included file. */
13334 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13335 if (lh->file_names[file_index].included_p == 1)
13336 {
13337 char *include_name =
13338 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13339 if (include_name != NULL)
13340 dwarf2_create_include_psymtab (include_name, pst, objfile);
13341 }
13342 }
13343 else
13344 {
13345 /* Make sure a symtab is created for every file, even files
13346 which contain only variables (i.e. no code with associated
13347 line numbers). */
13348 int i;
13349
13350 for (i = 0; i < lh->num_file_names; i++)
13351 {
13352 char *dir = NULL;
13353 struct file_entry *fe;
13354
13355 fe = &lh->file_names[i];
13356 if (fe->dir_index)
13357 dir = lh->include_dirs[fe->dir_index - 1];
13358 dwarf2_start_subfile (fe->name, dir, comp_dir);
13359
13360 /* Skip the main file; we don't need it, and it must be
13361 allocated last, so that it will show up before the
13362 non-primary symtabs in the objfile's symtab list. */
13363 if (current_subfile == first_subfile)
13364 continue;
13365
13366 if (current_subfile->symtab == NULL)
13367 current_subfile->symtab = allocate_symtab (current_subfile->name,
13368 objfile);
13369 fe->symtab = current_subfile->symtab;
13370 }
13371 }
13372 }
13373
13374 /* Start a subfile for DWARF. FILENAME is the name of the file and
13375 DIRNAME the name of the source directory which contains FILENAME
13376 or NULL if not known. COMP_DIR is the compilation directory for the
13377 linetable's compilation unit or NULL if not known.
13378 This routine tries to keep line numbers from identical absolute and
13379 relative file names in a common subfile.
13380
13381 Using the `list' example from the GDB testsuite, which resides in
13382 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13383 of /srcdir/list0.c yields the following debugging information for list0.c:
13384
13385 DW_AT_name: /srcdir/list0.c
13386 DW_AT_comp_dir: /compdir
13387 files.files[0].name: list0.h
13388 files.files[0].dir: /srcdir
13389 files.files[1].name: list0.c
13390 files.files[1].dir: /srcdir
13391
13392 The line number information for list0.c has to end up in a single
13393 subfile, so that `break /srcdir/list0.c:1' works as expected.
13394 start_subfile will ensure that this happens provided that we pass the
13395 concatenation of files.files[1].dir and files.files[1].name as the
13396 subfile's name. */
13397
13398 static void
13399 dwarf2_start_subfile (char *filename, const char *dirname,
13400 const char *comp_dir)
13401 {
13402 char *fullname;
13403
13404 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13405 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13406 second argument to start_subfile. To be consistent, we do the
13407 same here. In order not to lose the line information directory,
13408 we concatenate it to the filename when it makes sense.
13409 Note that the Dwarf3 standard says (speaking of filenames in line
13410 information): ``The directory index is ignored for file names
13411 that represent full path names''. Thus ignoring dirname in the
13412 `else' branch below isn't an issue. */
13413
13414 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13415 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13416 else
13417 fullname = filename;
13418
13419 start_subfile (fullname, comp_dir);
13420
13421 if (fullname != filename)
13422 xfree (fullname);
13423 }
13424
13425 static void
13426 var_decode_location (struct attribute *attr, struct symbol *sym,
13427 struct dwarf2_cu *cu)
13428 {
13429 struct objfile *objfile = cu->objfile;
13430 struct comp_unit_head *cu_header = &cu->header;
13431
13432 /* NOTE drow/2003-01-30: There used to be a comment and some special
13433 code here to turn a symbol with DW_AT_external and a
13434 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13435 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13436 with some versions of binutils) where shared libraries could have
13437 relocations against symbols in their debug information - the
13438 minimal symbol would have the right address, but the debug info
13439 would not. It's no longer necessary, because we will explicitly
13440 apply relocations when we read in the debug information now. */
13441
13442 /* A DW_AT_location attribute with no contents indicates that a
13443 variable has been optimized away. */
13444 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13445 {
13446 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13447 return;
13448 }
13449
13450 /* Handle one degenerate form of location expression specially, to
13451 preserve GDB's previous behavior when section offsets are
13452 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13453 then mark this symbol as LOC_STATIC. */
13454
13455 if (attr_form_is_block (attr)
13456 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13457 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13458 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13459 && (DW_BLOCK (attr)->size
13460 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13461 {
13462 unsigned int dummy;
13463
13464 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13465 SYMBOL_VALUE_ADDRESS (sym) =
13466 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13467 else
13468 SYMBOL_VALUE_ADDRESS (sym) =
13469 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13470 SYMBOL_CLASS (sym) = LOC_STATIC;
13471 fixup_symbol_section (sym, objfile);
13472 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13473 SYMBOL_SECTION (sym));
13474 return;
13475 }
13476
13477 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13478 expression evaluator, and use LOC_COMPUTED only when necessary
13479 (i.e. when the value of a register or memory location is
13480 referenced, or a thread-local block, etc.). Then again, it might
13481 not be worthwhile. I'm assuming that it isn't unless performance
13482 or memory numbers show me otherwise. */
13483
13484 dwarf2_symbol_mark_computed (attr, sym, cu);
13485 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13486
13487 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13488 cu->has_loclist = 1;
13489 }
13490
13491 /* Given a pointer to a DWARF information entry, figure out if we need
13492 to make a symbol table entry for it, and if so, create a new entry
13493 and return a pointer to it.
13494 If TYPE is NULL, determine symbol type from the die, otherwise
13495 used the passed type.
13496 If SPACE is not NULL, use it to hold the new symbol. If it is
13497 NULL, allocate a new symbol on the objfile's obstack. */
13498
13499 static struct symbol *
13500 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13501 struct symbol *space)
13502 {
13503 struct objfile *objfile = cu->objfile;
13504 struct symbol *sym = NULL;
13505 char *name;
13506 struct attribute *attr = NULL;
13507 struct attribute *attr2 = NULL;
13508 CORE_ADDR baseaddr;
13509 struct pending **list_to_add = NULL;
13510
13511 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13512
13513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13514
13515 name = dwarf2_name (die, cu);
13516 if (name)
13517 {
13518 const char *linkagename;
13519 int suppress_add = 0;
13520
13521 if (space)
13522 sym = space;
13523 else
13524 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13525 OBJSTAT (objfile, n_syms++);
13526
13527 /* Cache this symbol's name and the name's demangled form (if any). */
13528 SYMBOL_SET_LANGUAGE (sym, cu->language);
13529 linkagename = dwarf2_physname (name, die, cu);
13530 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13531
13532 /* Fortran does not have mangling standard and the mangling does differ
13533 between gfortran, iFort etc. */
13534 if (cu->language == language_fortran
13535 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13536 symbol_set_demangled_name (&(sym->ginfo),
13537 (char *) dwarf2_full_name (name, die, cu),
13538 NULL);
13539
13540 /* Default assumptions.
13541 Use the passed type or decode it from the die. */
13542 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13543 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13544 if (type != NULL)
13545 SYMBOL_TYPE (sym) = type;
13546 else
13547 SYMBOL_TYPE (sym) = die_type (die, cu);
13548 attr = dwarf2_attr (die,
13549 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13550 cu);
13551 if (attr)
13552 {
13553 SYMBOL_LINE (sym) = DW_UNSND (attr);
13554 }
13555
13556 attr = dwarf2_attr (die,
13557 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13558 cu);
13559 if (attr)
13560 {
13561 int file_index = DW_UNSND (attr);
13562
13563 if (cu->line_header == NULL
13564 || file_index > cu->line_header->num_file_names)
13565 complaint (&symfile_complaints,
13566 _("file index out of range"));
13567 else if (file_index > 0)
13568 {
13569 struct file_entry *fe;
13570
13571 fe = &cu->line_header->file_names[file_index - 1];
13572 SYMBOL_SYMTAB (sym) = fe->symtab;
13573 }
13574 }
13575
13576 switch (die->tag)
13577 {
13578 case DW_TAG_label:
13579 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13580 if (attr)
13581 {
13582 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13583 }
13584 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13585 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13586 SYMBOL_CLASS (sym) = LOC_LABEL;
13587 add_symbol_to_list (sym, cu->list_in_scope);
13588 break;
13589 case DW_TAG_subprogram:
13590 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13591 finish_block. */
13592 SYMBOL_CLASS (sym) = LOC_BLOCK;
13593 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13594 if ((attr2 && (DW_UNSND (attr2) != 0))
13595 || cu->language == language_ada)
13596 {
13597 /* Subprograms marked external are stored as a global symbol.
13598 Ada subprograms, whether marked external or not, are always
13599 stored as a global symbol, because we want to be able to
13600 access them globally. For instance, we want to be able
13601 to break on a nested subprogram without having to
13602 specify the context. */
13603 list_to_add = &global_symbols;
13604 }
13605 else
13606 {
13607 list_to_add = cu->list_in_scope;
13608 }
13609 break;
13610 case DW_TAG_inlined_subroutine:
13611 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13612 finish_block. */
13613 SYMBOL_CLASS (sym) = LOC_BLOCK;
13614 SYMBOL_INLINED (sym) = 1;
13615 list_to_add = cu->list_in_scope;
13616 break;
13617 case DW_TAG_template_value_param:
13618 suppress_add = 1;
13619 /* Fall through. */
13620 case DW_TAG_constant:
13621 case DW_TAG_variable:
13622 case DW_TAG_member:
13623 /* Compilation with minimal debug info may result in
13624 variables with missing type entries. Change the
13625 misleading `void' type to something sensible. */
13626 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13627 SYMBOL_TYPE (sym)
13628 = objfile_type (objfile)->nodebug_data_symbol;
13629
13630 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13631 /* In the case of DW_TAG_member, we should only be called for
13632 static const members. */
13633 if (die->tag == DW_TAG_member)
13634 {
13635 /* dwarf2_add_field uses die_is_declaration,
13636 so we do the same. */
13637 gdb_assert (die_is_declaration (die, cu));
13638 gdb_assert (attr);
13639 }
13640 if (attr)
13641 {
13642 dwarf2_const_value (attr, sym, cu);
13643 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13644 if (!suppress_add)
13645 {
13646 if (attr2 && (DW_UNSND (attr2) != 0))
13647 list_to_add = &global_symbols;
13648 else
13649 list_to_add = cu->list_in_scope;
13650 }
13651 break;
13652 }
13653 attr = dwarf2_attr (die, DW_AT_location, cu);
13654 if (attr)
13655 {
13656 var_decode_location (attr, sym, cu);
13657 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13658 if (SYMBOL_CLASS (sym) == LOC_STATIC
13659 && SYMBOL_VALUE_ADDRESS (sym) == 0
13660 && !dwarf2_per_objfile->has_section_at_zero)
13661 {
13662 /* When a static variable is eliminated by the linker,
13663 the corresponding debug information is not stripped
13664 out, but the variable address is set to null;
13665 do not add such variables into symbol table. */
13666 }
13667 else if (attr2 && (DW_UNSND (attr2) != 0))
13668 {
13669 /* Workaround gfortran PR debug/40040 - it uses
13670 DW_AT_location for variables in -fPIC libraries which may
13671 get overriden by other libraries/executable and get
13672 a different address. Resolve it by the minimal symbol
13673 which may come from inferior's executable using copy
13674 relocation. Make this workaround only for gfortran as for
13675 other compilers GDB cannot guess the minimal symbol
13676 Fortran mangling kind. */
13677 if (cu->language == language_fortran && die->parent
13678 && die->parent->tag == DW_TAG_module
13679 && cu->producer
13680 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13681 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13682
13683 /* A variable with DW_AT_external is never static,
13684 but it may be block-scoped. */
13685 list_to_add = (cu->list_in_scope == &file_symbols
13686 ? &global_symbols : cu->list_in_scope);
13687 }
13688 else
13689 list_to_add = cu->list_in_scope;
13690 }
13691 else
13692 {
13693 /* We do not know the address of this symbol.
13694 If it is an external symbol and we have type information
13695 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13696 The address of the variable will then be determined from
13697 the minimal symbol table whenever the variable is
13698 referenced. */
13699 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13700 if (attr2 && (DW_UNSND (attr2) != 0)
13701 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13702 {
13703 /* A variable with DW_AT_external is never static, but it
13704 may be block-scoped. */
13705 list_to_add = (cu->list_in_scope == &file_symbols
13706 ? &global_symbols : cu->list_in_scope);
13707
13708 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13709 }
13710 else if (!die_is_declaration (die, cu))
13711 {
13712 /* Use the default LOC_OPTIMIZED_OUT class. */
13713 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13714 if (!suppress_add)
13715 list_to_add = cu->list_in_scope;
13716 }
13717 }
13718 break;
13719 case DW_TAG_formal_parameter:
13720 /* If we are inside a function, mark this as an argument. If
13721 not, we might be looking at an argument to an inlined function
13722 when we do not have enough information to show inlined frames;
13723 pretend it's a local variable in that case so that the user can
13724 still see it. */
13725 if (context_stack_depth > 0
13726 && context_stack[context_stack_depth - 1].name != NULL)
13727 SYMBOL_IS_ARGUMENT (sym) = 1;
13728 attr = dwarf2_attr (die, DW_AT_location, cu);
13729 if (attr)
13730 {
13731 var_decode_location (attr, sym, cu);
13732 }
13733 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13734 if (attr)
13735 {
13736 dwarf2_const_value (attr, sym, cu);
13737 }
13738
13739 list_to_add = cu->list_in_scope;
13740 break;
13741 case DW_TAG_unspecified_parameters:
13742 /* From varargs functions; gdb doesn't seem to have any
13743 interest in this information, so just ignore it for now.
13744 (FIXME?) */
13745 break;
13746 case DW_TAG_template_type_param:
13747 suppress_add = 1;
13748 /* Fall through. */
13749 case DW_TAG_class_type:
13750 case DW_TAG_interface_type:
13751 case DW_TAG_structure_type:
13752 case DW_TAG_union_type:
13753 case DW_TAG_set_type:
13754 case DW_TAG_enumeration_type:
13755 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13756 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13757
13758 {
13759 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13760 really ever be static objects: otherwise, if you try
13761 to, say, break of a class's method and you're in a file
13762 which doesn't mention that class, it won't work unless
13763 the check for all static symbols in lookup_symbol_aux
13764 saves you. See the OtherFileClass tests in
13765 gdb.c++/namespace.exp. */
13766
13767 if (!suppress_add)
13768 {
13769 list_to_add = (cu->list_in_scope == &file_symbols
13770 && (cu->language == language_cplus
13771 || cu->language == language_java)
13772 ? &global_symbols : cu->list_in_scope);
13773
13774 /* The semantics of C++ state that "struct foo {
13775 ... }" also defines a typedef for "foo". A Java
13776 class declaration also defines a typedef for the
13777 class. */
13778 if (cu->language == language_cplus
13779 || cu->language == language_java
13780 || cu->language == language_ada)
13781 {
13782 /* The symbol's name is already allocated along
13783 with this objfile, so we don't need to
13784 duplicate it for the type. */
13785 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13786 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13787 }
13788 }
13789 }
13790 break;
13791 case DW_TAG_typedef:
13792 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13793 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13794 list_to_add = cu->list_in_scope;
13795 break;
13796 case DW_TAG_base_type:
13797 case DW_TAG_subrange_type:
13798 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13799 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13800 list_to_add = cu->list_in_scope;
13801 break;
13802 case DW_TAG_enumerator:
13803 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13804 if (attr)
13805 {
13806 dwarf2_const_value (attr, sym, cu);
13807 }
13808 {
13809 /* NOTE: carlton/2003-11-10: See comment above in the
13810 DW_TAG_class_type, etc. block. */
13811
13812 list_to_add = (cu->list_in_scope == &file_symbols
13813 && (cu->language == language_cplus
13814 || cu->language == language_java)
13815 ? &global_symbols : cu->list_in_scope);
13816 }
13817 break;
13818 case DW_TAG_namespace:
13819 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13820 list_to_add = &global_symbols;
13821 break;
13822 default:
13823 /* Not a tag we recognize. Hopefully we aren't processing
13824 trash data, but since we must specifically ignore things
13825 we don't recognize, there is nothing else we should do at
13826 this point. */
13827 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13828 dwarf_tag_name (die->tag));
13829 break;
13830 }
13831
13832 if (suppress_add)
13833 {
13834 sym->hash_next = objfile->template_symbols;
13835 objfile->template_symbols = sym;
13836 list_to_add = NULL;
13837 }
13838
13839 if (list_to_add != NULL)
13840 add_symbol_to_list (sym, list_to_add);
13841
13842 /* For the benefit of old versions of GCC, check for anonymous
13843 namespaces based on the demangled name. */
13844 if (!processing_has_namespace_info
13845 && cu->language == language_cplus)
13846 cp_scan_for_anonymous_namespaces (sym, objfile);
13847 }
13848 return (sym);
13849 }
13850
13851 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13852
13853 static struct symbol *
13854 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13855 {
13856 return new_symbol_full (die, type, cu, NULL);
13857 }
13858
13859 /* Given an attr with a DW_FORM_dataN value in host byte order,
13860 zero-extend it as appropriate for the symbol's type. The DWARF
13861 standard (v4) is not entirely clear about the meaning of using
13862 DW_FORM_dataN for a constant with a signed type, where the type is
13863 wider than the data. The conclusion of a discussion on the DWARF
13864 list was that this is unspecified. We choose to always zero-extend
13865 because that is the interpretation long in use by GCC. */
13866
13867 static gdb_byte *
13868 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13869 const char *name, struct obstack *obstack,
13870 struct dwarf2_cu *cu, LONGEST *value, int bits)
13871 {
13872 struct objfile *objfile = cu->objfile;
13873 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13874 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13875 LONGEST l = DW_UNSND (attr);
13876
13877 if (bits < sizeof (*value) * 8)
13878 {
13879 l &= ((LONGEST) 1 << bits) - 1;
13880 *value = l;
13881 }
13882 else if (bits == sizeof (*value) * 8)
13883 *value = l;
13884 else
13885 {
13886 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13887 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13888 return bytes;
13889 }
13890
13891 return NULL;
13892 }
13893
13894 /* Read a constant value from an attribute. Either set *VALUE, or if
13895 the value does not fit in *VALUE, set *BYTES - either already
13896 allocated on the objfile obstack, or newly allocated on OBSTACK,
13897 or, set *BATON, if we translated the constant to a location
13898 expression. */
13899
13900 static void
13901 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13902 const char *name, struct obstack *obstack,
13903 struct dwarf2_cu *cu,
13904 LONGEST *value, gdb_byte **bytes,
13905 struct dwarf2_locexpr_baton **baton)
13906 {
13907 struct objfile *objfile = cu->objfile;
13908 struct comp_unit_head *cu_header = &cu->header;
13909 struct dwarf_block *blk;
13910 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13911 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13912
13913 *value = 0;
13914 *bytes = NULL;
13915 *baton = NULL;
13916
13917 switch (attr->form)
13918 {
13919 case DW_FORM_addr:
13920 case DW_FORM_GNU_addr_index:
13921 {
13922 gdb_byte *data;
13923
13924 if (TYPE_LENGTH (type) != cu_header->addr_size)
13925 dwarf2_const_value_length_mismatch_complaint (name,
13926 cu_header->addr_size,
13927 TYPE_LENGTH (type));
13928 /* Symbols of this form are reasonably rare, so we just
13929 piggyback on the existing location code rather than writing
13930 a new implementation of symbol_computed_ops. */
13931 *baton = obstack_alloc (&objfile->objfile_obstack,
13932 sizeof (struct dwarf2_locexpr_baton));
13933 (*baton)->per_cu = cu->per_cu;
13934 gdb_assert ((*baton)->per_cu);
13935
13936 (*baton)->size = 2 + cu_header->addr_size;
13937 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13938 (*baton)->data = data;
13939
13940 data[0] = DW_OP_addr;
13941 store_unsigned_integer (&data[1], cu_header->addr_size,
13942 byte_order, DW_ADDR (attr));
13943 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13944 }
13945 break;
13946 case DW_FORM_string:
13947 case DW_FORM_strp:
13948 case DW_FORM_GNU_str_index:
13949 /* DW_STRING is already allocated on the objfile obstack, point
13950 directly to it. */
13951 *bytes = (gdb_byte *) DW_STRING (attr);
13952 break;
13953 case DW_FORM_block1:
13954 case DW_FORM_block2:
13955 case DW_FORM_block4:
13956 case DW_FORM_block:
13957 case DW_FORM_exprloc:
13958 blk = DW_BLOCK (attr);
13959 if (TYPE_LENGTH (type) != blk->size)
13960 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13961 TYPE_LENGTH (type));
13962 *bytes = blk->data;
13963 break;
13964
13965 /* The DW_AT_const_value attributes are supposed to carry the
13966 symbol's value "represented as it would be on the target
13967 architecture." By the time we get here, it's already been
13968 converted to host endianness, so we just need to sign- or
13969 zero-extend it as appropriate. */
13970 case DW_FORM_data1:
13971 *bytes = dwarf2_const_value_data (attr, type, name,
13972 obstack, cu, value, 8);
13973 break;
13974 case DW_FORM_data2:
13975 *bytes = dwarf2_const_value_data (attr, type, name,
13976 obstack, cu, value, 16);
13977 break;
13978 case DW_FORM_data4:
13979 *bytes = dwarf2_const_value_data (attr, type, name,
13980 obstack, cu, value, 32);
13981 break;
13982 case DW_FORM_data8:
13983 *bytes = dwarf2_const_value_data (attr, type, name,
13984 obstack, cu, value, 64);
13985 break;
13986
13987 case DW_FORM_sdata:
13988 *value = DW_SND (attr);
13989 break;
13990
13991 case DW_FORM_udata:
13992 *value = DW_UNSND (attr);
13993 break;
13994
13995 default:
13996 complaint (&symfile_complaints,
13997 _("unsupported const value attribute form: '%s'"),
13998 dwarf_form_name (attr->form));
13999 *value = 0;
14000 break;
14001 }
14002 }
14003
14004
14005 /* Copy constant value from an attribute to a symbol. */
14006
14007 static void
14008 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14009 struct dwarf2_cu *cu)
14010 {
14011 struct objfile *objfile = cu->objfile;
14012 struct comp_unit_head *cu_header = &cu->header;
14013 LONGEST value;
14014 gdb_byte *bytes;
14015 struct dwarf2_locexpr_baton *baton;
14016
14017 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14018 SYMBOL_PRINT_NAME (sym),
14019 &objfile->objfile_obstack, cu,
14020 &value, &bytes, &baton);
14021
14022 if (baton != NULL)
14023 {
14024 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14025 SYMBOL_LOCATION_BATON (sym) = baton;
14026 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14027 }
14028 else if (bytes != NULL)
14029 {
14030 SYMBOL_VALUE_BYTES (sym) = bytes;
14031 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14032 }
14033 else
14034 {
14035 SYMBOL_VALUE (sym) = value;
14036 SYMBOL_CLASS (sym) = LOC_CONST;
14037 }
14038 }
14039
14040 /* Return the type of the die in question using its DW_AT_type attribute. */
14041
14042 static struct type *
14043 die_type (struct die_info *die, struct dwarf2_cu *cu)
14044 {
14045 struct attribute *type_attr;
14046
14047 type_attr = dwarf2_attr (die, DW_AT_type, cu);
14048 if (!type_attr)
14049 {
14050 /* A missing DW_AT_type represents a void type. */
14051 return objfile_type (cu->objfile)->builtin_void;
14052 }
14053
14054 return lookup_die_type (die, type_attr, cu);
14055 }
14056
14057 /* True iff CU's producer generates GNAT Ada auxiliary information
14058 that allows to find parallel types through that information instead
14059 of having to do expensive parallel lookups by type name. */
14060
14061 static int
14062 need_gnat_info (struct dwarf2_cu *cu)
14063 {
14064 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14065 of GNAT produces this auxiliary information, without any indication
14066 that it is produced. Part of enhancing the FSF version of GNAT
14067 to produce that information will be to put in place an indicator
14068 that we can use in order to determine whether the descriptive type
14069 info is available or not. One suggestion that has been made is
14070 to use a new attribute, attached to the CU die. For now, assume
14071 that the descriptive type info is not available. */
14072 return 0;
14073 }
14074
14075 /* Return the auxiliary type of the die in question using its
14076 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14077 attribute is not present. */
14078
14079 static struct type *
14080 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14081 {
14082 struct attribute *type_attr;
14083
14084 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14085 if (!type_attr)
14086 return NULL;
14087
14088 return lookup_die_type (die, type_attr, cu);
14089 }
14090
14091 /* If DIE has a descriptive_type attribute, then set the TYPE's
14092 descriptive type accordingly. */
14093
14094 static void
14095 set_descriptive_type (struct type *type, struct die_info *die,
14096 struct dwarf2_cu *cu)
14097 {
14098 struct type *descriptive_type = die_descriptive_type (die, cu);
14099
14100 if (descriptive_type)
14101 {
14102 ALLOCATE_GNAT_AUX_TYPE (type);
14103 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14104 }
14105 }
14106
14107 /* Return the containing type of the die in question using its
14108 DW_AT_containing_type attribute. */
14109
14110 static struct type *
14111 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14112 {
14113 struct attribute *type_attr;
14114
14115 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
14116 if (!type_attr)
14117 error (_("Dwarf Error: Problem turning containing type into gdb type "
14118 "[in module %s]"), cu->objfile->name);
14119
14120 return lookup_die_type (die, type_attr, cu);
14121 }
14122
14123 /* Look up the type of DIE in CU using its type attribute ATTR.
14124 If there is no type substitute an error marker. */
14125
14126 static struct type *
14127 lookup_die_type (struct die_info *die, struct attribute *attr,
14128 struct dwarf2_cu *cu)
14129 {
14130 struct objfile *objfile = cu->objfile;
14131 struct type *this_type;
14132
14133 /* First see if we have it cached. */
14134
14135 if (is_ref_attr (attr))
14136 {
14137 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14138
14139 this_type = get_die_type_at_offset (offset, cu->per_cu);
14140 }
14141 else if (attr->form == DW_FORM_ref_sig8)
14142 {
14143 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14144
14145 /* sig_type will be NULL if the signatured type is missing from
14146 the debug info. */
14147 if (sig_type == NULL)
14148 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14149 "at 0x%x [in module %s]"),
14150 die->offset.sect_off, objfile->name);
14151
14152 gdb_assert (sig_type->per_cu.is_debug_types);
14153 /* If we haven't filled in type_offset_in_section yet, then we
14154 haven't read the type in yet. */
14155 this_type = NULL;
14156 if (sig_type->type_offset_in_section.sect_off != 0)
14157 {
14158 this_type =
14159 get_die_type_at_offset (sig_type->type_offset_in_section,
14160 &sig_type->per_cu);
14161 }
14162 }
14163 else
14164 {
14165 dump_die_for_error (die);
14166 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
14167 dwarf_attr_name (attr->name), objfile->name);
14168 }
14169
14170 /* If not cached we need to read it in. */
14171
14172 if (this_type == NULL)
14173 {
14174 struct die_info *type_die;
14175 struct dwarf2_cu *type_cu = cu;
14176
14177 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
14178 /* If we found the type now, it's probably because the type came
14179 from an inter-CU reference and the type's CU got expanded before
14180 ours. */
14181 this_type = get_die_type (type_die, type_cu);
14182 if (this_type == NULL)
14183 this_type = read_type_die_1 (type_die, type_cu);
14184 }
14185
14186 /* If we still don't have a type use an error marker. */
14187
14188 if (this_type == NULL)
14189 {
14190 char *message, *saved;
14191
14192 /* read_type_die already issued a complaint. */
14193 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
14194 objfile->name,
14195 cu->header.offset.sect_off,
14196 die->offset.sect_off);
14197 saved = obstack_copy0 (&objfile->objfile_obstack,
14198 message, strlen (message));
14199 xfree (message);
14200
14201 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
14202 }
14203
14204 return this_type;
14205 }
14206
14207 /* Return the type in DIE, CU.
14208 Returns NULL for invalid types.
14209
14210 This first does a lookup in the appropriate type_hash table,
14211 and only reads the die in if necessary.
14212
14213 NOTE: This can be called when reading in partial or full symbols. */
14214
14215 static struct type *
14216 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
14217 {
14218 struct type *this_type;
14219
14220 this_type = get_die_type (die, cu);
14221 if (this_type)
14222 return this_type;
14223
14224 return read_type_die_1 (die, cu);
14225 }
14226
14227 /* Read the type in DIE, CU.
14228 Returns NULL for invalid types. */
14229
14230 static struct type *
14231 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14232 {
14233 struct type *this_type = NULL;
14234
14235 switch (die->tag)
14236 {
14237 case DW_TAG_class_type:
14238 case DW_TAG_interface_type:
14239 case DW_TAG_structure_type:
14240 case DW_TAG_union_type:
14241 this_type = read_structure_type (die, cu);
14242 break;
14243 case DW_TAG_enumeration_type:
14244 this_type = read_enumeration_type (die, cu);
14245 break;
14246 case DW_TAG_subprogram:
14247 case DW_TAG_subroutine_type:
14248 case DW_TAG_inlined_subroutine:
14249 this_type = read_subroutine_type (die, cu);
14250 break;
14251 case DW_TAG_array_type:
14252 this_type = read_array_type (die, cu);
14253 break;
14254 case DW_TAG_set_type:
14255 this_type = read_set_type (die, cu);
14256 break;
14257 case DW_TAG_pointer_type:
14258 this_type = read_tag_pointer_type (die, cu);
14259 break;
14260 case DW_TAG_ptr_to_member_type:
14261 this_type = read_tag_ptr_to_member_type (die, cu);
14262 break;
14263 case DW_TAG_reference_type:
14264 this_type = read_tag_reference_type (die, cu);
14265 break;
14266 case DW_TAG_const_type:
14267 this_type = read_tag_const_type (die, cu);
14268 break;
14269 case DW_TAG_volatile_type:
14270 this_type = read_tag_volatile_type (die, cu);
14271 break;
14272 case DW_TAG_string_type:
14273 this_type = read_tag_string_type (die, cu);
14274 break;
14275 case DW_TAG_typedef:
14276 this_type = read_typedef (die, cu);
14277 break;
14278 case DW_TAG_subrange_type:
14279 this_type = read_subrange_type (die, cu);
14280 break;
14281 case DW_TAG_base_type:
14282 this_type = read_base_type (die, cu);
14283 break;
14284 case DW_TAG_unspecified_type:
14285 this_type = read_unspecified_type (die, cu);
14286 break;
14287 case DW_TAG_namespace:
14288 this_type = read_namespace_type (die, cu);
14289 break;
14290 case DW_TAG_module:
14291 this_type = read_module_type (die, cu);
14292 break;
14293 default:
14294 complaint (&symfile_complaints,
14295 _("unexpected tag in read_type_die: '%s'"),
14296 dwarf_tag_name (die->tag));
14297 break;
14298 }
14299
14300 return this_type;
14301 }
14302
14303 /* See if we can figure out if the class lives in a namespace. We do
14304 this by looking for a member function; its demangled name will
14305 contain namespace info, if there is any.
14306 Return the computed name or NULL.
14307 Space for the result is allocated on the objfile's obstack.
14308 This is the full-die version of guess_partial_die_structure_name.
14309 In this case we know DIE has no useful parent. */
14310
14311 static char *
14312 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14313 {
14314 struct die_info *spec_die;
14315 struct dwarf2_cu *spec_cu;
14316 struct die_info *child;
14317
14318 spec_cu = cu;
14319 spec_die = die_specification (die, &spec_cu);
14320 if (spec_die != NULL)
14321 {
14322 die = spec_die;
14323 cu = spec_cu;
14324 }
14325
14326 for (child = die->child;
14327 child != NULL;
14328 child = child->sibling)
14329 {
14330 if (child->tag == DW_TAG_subprogram)
14331 {
14332 struct attribute *attr;
14333
14334 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14335 if (attr == NULL)
14336 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14337 if (attr != NULL)
14338 {
14339 char *actual_name
14340 = language_class_name_from_physname (cu->language_defn,
14341 DW_STRING (attr));
14342 char *name = NULL;
14343
14344 if (actual_name != NULL)
14345 {
14346 char *die_name = dwarf2_name (die, cu);
14347
14348 if (die_name != NULL
14349 && strcmp (die_name, actual_name) != 0)
14350 {
14351 /* Strip off the class name from the full name.
14352 We want the prefix. */
14353 int die_name_len = strlen (die_name);
14354 int actual_name_len = strlen (actual_name);
14355
14356 /* Test for '::' as a sanity check. */
14357 if (actual_name_len > die_name_len + 2
14358 && actual_name[actual_name_len
14359 - die_name_len - 1] == ':')
14360 name =
14361 obsavestring (actual_name,
14362 actual_name_len - die_name_len - 2,
14363 &cu->objfile->objfile_obstack);
14364 }
14365 }
14366 xfree (actual_name);
14367 return name;
14368 }
14369 }
14370 }
14371
14372 return NULL;
14373 }
14374
14375 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14376 prefix part in such case. See
14377 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14378
14379 static char *
14380 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14381 {
14382 struct attribute *attr;
14383 char *base;
14384
14385 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14386 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14387 return NULL;
14388
14389 attr = dwarf2_attr (die, DW_AT_name, cu);
14390 if (attr != NULL && DW_STRING (attr) != NULL)
14391 return NULL;
14392
14393 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14394 if (attr == NULL)
14395 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14396 if (attr == NULL || DW_STRING (attr) == NULL)
14397 return NULL;
14398
14399 /* dwarf2_name had to be already called. */
14400 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14401
14402 /* Strip the base name, keep any leading namespaces/classes. */
14403 base = strrchr (DW_STRING (attr), ':');
14404 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14405 return "";
14406
14407 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14408 &cu->objfile->objfile_obstack);
14409 }
14410
14411 /* Return the name of the namespace/class that DIE is defined within,
14412 or "" if we can't tell. The caller should not xfree the result.
14413
14414 For example, if we're within the method foo() in the following
14415 code:
14416
14417 namespace N {
14418 class C {
14419 void foo () {
14420 }
14421 };
14422 }
14423
14424 then determine_prefix on foo's die will return "N::C". */
14425
14426 static const char *
14427 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14428 {
14429 struct die_info *parent, *spec_die;
14430 struct dwarf2_cu *spec_cu;
14431 struct type *parent_type;
14432 char *retval;
14433
14434 if (cu->language != language_cplus && cu->language != language_java
14435 && cu->language != language_fortran)
14436 return "";
14437
14438 retval = anonymous_struct_prefix (die, cu);
14439 if (retval)
14440 return retval;
14441
14442 /* We have to be careful in the presence of DW_AT_specification.
14443 For example, with GCC 3.4, given the code
14444
14445 namespace N {
14446 void foo() {
14447 // Definition of N::foo.
14448 }
14449 }
14450
14451 then we'll have a tree of DIEs like this:
14452
14453 1: DW_TAG_compile_unit
14454 2: DW_TAG_namespace // N
14455 3: DW_TAG_subprogram // declaration of N::foo
14456 4: DW_TAG_subprogram // definition of N::foo
14457 DW_AT_specification // refers to die #3
14458
14459 Thus, when processing die #4, we have to pretend that we're in
14460 the context of its DW_AT_specification, namely the contex of die
14461 #3. */
14462 spec_cu = cu;
14463 spec_die = die_specification (die, &spec_cu);
14464 if (spec_die == NULL)
14465 parent = die->parent;
14466 else
14467 {
14468 parent = spec_die->parent;
14469 cu = spec_cu;
14470 }
14471
14472 if (parent == NULL)
14473 return "";
14474 else if (parent->building_fullname)
14475 {
14476 const char *name;
14477 const char *parent_name;
14478
14479 /* It has been seen on RealView 2.2 built binaries,
14480 DW_TAG_template_type_param types actually _defined_ as
14481 children of the parent class:
14482
14483 enum E {};
14484 template class <class Enum> Class{};
14485 Class<enum E> class_e;
14486
14487 1: DW_TAG_class_type (Class)
14488 2: DW_TAG_enumeration_type (E)
14489 3: DW_TAG_enumerator (enum1:0)
14490 3: DW_TAG_enumerator (enum2:1)
14491 ...
14492 2: DW_TAG_template_type_param
14493 DW_AT_type DW_FORM_ref_udata (E)
14494
14495 Besides being broken debug info, it can put GDB into an
14496 infinite loop. Consider:
14497
14498 When we're building the full name for Class<E>, we'll start
14499 at Class, and go look over its template type parameters,
14500 finding E. We'll then try to build the full name of E, and
14501 reach here. We're now trying to build the full name of E,
14502 and look over the parent DIE for containing scope. In the
14503 broken case, if we followed the parent DIE of E, we'd again
14504 find Class, and once again go look at its template type
14505 arguments, etc., etc. Simply don't consider such parent die
14506 as source-level parent of this die (it can't be, the language
14507 doesn't allow it), and break the loop here. */
14508 name = dwarf2_name (die, cu);
14509 parent_name = dwarf2_name (parent, cu);
14510 complaint (&symfile_complaints,
14511 _("template param type '%s' defined within parent '%s'"),
14512 name ? name : "<unknown>",
14513 parent_name ? parent_name : "<unknown>");
14514 return "";
14515 }
14516 else
14517 switch (parent->tag)
14518 {
14519 case DW_TAG_namespace:
14520 parent_type = read_type_die (parent, cu);
14521 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14522 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14523 Work around this problem here. */
14524 if (cu->language == language_cplus
14525 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14526 return "";
14527 /* We give a name to even anonymous namespaces. */
14528 return TYPE_TAG_NAME (parent_type);
14529 case DW_TAG_class_type:
14530 case DW_TAG_interface_type:
14531 case DW_TAG_structure_type:
14532 case DW_TAG_union_type:
14533 case DW_TAG_module:
14534 parent_type = read_type_die (parent, cu);
14535 if (TYPE_TAG_NAME (parent_type) != NULL)
14536 return TYPE_TAG_NAME (parent_type);
14537 else
14538 /* An anonymous structure is only allowed non-static data
14539 members; no typedefs, no member functions, et cetera.
14540 So it does not need a prefix. */
14541 return "";
14542 case DW_TAG_compile_unit:
14543 case DW_TAG_partial_unit:
14544 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14545 if (cu->language == language_cplus
14546 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14547 && die->child != NULL
14548 && (die->tag == DW_TAG_class_type
14549 || die->tag == DW_TAG_structure_type
14550 || die->tag == DW_TAG_union_type))
14551 {
14552 char *name = guess_full_die_structure_name (die, cu);
14553 if (name != NULL)
14554 return name;
14555 }
14556 return "";
14557 default:
14558 return determine_prefix (parent, cu);
14559 }
14560 }
14561
14562 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14563 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14564 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14565 an obconcat, otherwise allocate storage for the result. The CU argument is
14566 used to determine the language and hence, the appropriate separator. */
14567
14568 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14569
14570 static char *
14571 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14572 int physname, struct dwarf2_cu *cu)
14573 {
14574 const char *lead = "";
14575 const char *sep;
14576
14577 if (suffix == NULL || suffix[0] == '\0'
14578 || prefix == NULL || prefix[0] == '\0')
14579 sep = "";
14580 else if (cu->language == language_java)
14581 sep = ".";
14582 else if (cu->language == language_fortran && physname)
14583 {
14584 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14585 DW_AT_MIPS_linkage_name is preferred and used instead. */
14586
14587 lead = "__";
14588 sep = "_MOD_";
14589 }
14590 else
14591 sep = "::";
14592
14593 if (prefix == NULL)
14594 prefix = "";
14595 if (suffix == NULL)
14596 suffix = "";
14597
14598 if (obs == NULL)
14599 {
14600 char *retval
14601 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14602
14603 strcpy (retval, lead);
14604 strcat (retval, prefix);
14605 strcat (retval, sep);
14606 strcat (retval, suffix);
14607 return retval;
14608 }
14609 else
14610 {
14611 /* We have an obstack. */
14612 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14613 }
14614 }
14615
14616 /* Return sibling of die, NULL if no sibling. */
14617
14618 static struct die_info *
14619 sibling_die (struct die_info *die)
14620 {
14621 return die->sibling;
14622 }
14623
14624 /* Get name of a die, return NULL if not found. */
14625
14626 static char *
14627 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14628 struct obstack *obstack)
14629 {
14630 if (name && cu->language == language_cplus)
14631 {
14632 char *canon_name = cp_canonicalize_string (name);
14633
14634 if (canon_name != NULL)
14635 {
14636 if (strcmp (canon_name, name) != 0)
14637 name = obsavestring (canon_name, strlen (canon_name),
14638 obstack);
14639 xfree (canon_name);
14640 }
14641 }
14642
14643 return name;
14644 }
14645
14646 /* Get name of a die, return NULL if not found. */
14647
14648 static char *
14649 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14650 {
14651 struct attribute *attr;
14652
14653 attr = dwarf2_attr (die, DW_AT_name, cu);
14654 if ((!attr || !DW_STRING (attr))
14655 && die->tag != DW_TAG_class_type
14656 && die->tag != DW_TAG_interface_type
14657 && die->tag != DW_TAG_structure_type
14658 && die->tag != DW_TAG_union_type)
14659 return NULL;
14660
14661 switch (die->tag)
14662 {
14663 case DW_TAG_compile_unit:
14664 case DW_TAG_partial_unit:
14665 /* Compilation units have a DW_AT_name that is a filename, not
14666 a source language identifier. */
14667 case DW_TAG_enumeration_type:
14668 case DW_TAG_enumerator:
14669 /* These tags always have simple identifiers already; no need
14670 to canonicalize them. */
14671 return DW_STRING (attr);
14672
14673 case DW_TAG_subprogram:
14674 /* Java constructors will all be named "<init>", so return
14675 the class name when we see this special case. */
14676 if (cu->language == language_java
14677 && DW_STRING (attr) != NULL
14678 && strcmp (DW_STRING (attr), "<init>") == 0)
14679 {
14680 struct dwarf2_cu *spec_cu = cu;
14681 struct die_info *spec_die;
14682
14683 /* GCJ will output '<init>' for Java constructor names.
14684 For this special case, return the name of the parent class. */
14685
14686 /* GCJ may output suprogram DIEs with AT_specification set.
14687 If so, use the name of the specified DIE. */
14688 spec_die = die_specification (die, &spec_cu);
14689 if (spec_die != NULL)
14690 return dwarf2_name (spec_die, spec_cu);
14691
14692 do
14693 {
14694 die = die->parent;
14695 if (die->tag == DW_TAG_class_type)
14696 return dwarf2_name (die, cu);
14697 }
14698 while (die->tag != DW_TAG_compile_unit
14699 && die->tag != DW_TAG_partial_unit);
14700 }
14701 break;
14702
14703 case DW_TAG_class_type:
14704 case DW_TAG_interface_type:
14705 case DW_TAG_structure_type:
14706 case DW_TAG_union_type:
14707 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14708 structures or unions. These were of the form "._%d" in GCC 4.1,
14709 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14710 and GCC 4.4. We work around this problem by ignoring these. */
14711 if (attr && DW_STRING (attr)
14712 && (strncmp (DW_STRING (attr), "._", 2) == 0
14713 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14714 return NULL;
14715
14716 /* GCC might emit a nameless typedef that has a linkage name. See
14717 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14718 if (!attr || DW_STRING (attr) == NULL)
14719 {
14720 char *demangled = NULL;
14721
14722 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14723 if (attr == NULL)
14724 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14725
14726 if (attr == NULL || DW_STRING (attr) == NULL)
14727 return NULL;
14728
14729 /* Avoid demangling DW_STRING (attr) the second time on a second
14730 call for the same DIE. */
14731 if (!DW_STRING_IS_CANONICAL (attr))
14732 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14733
14734 if (demangled)
14735 {
14736 char *base;
14737
14738 /* FIXME: we already did this for the partial symbol... */
14739 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14740 &cu->objfile->objfile_obstack);
14741 DW_STRING_IS_CANONICAL (attr) = 1;
14742 xfree (demangled);
14743
14744 /* Strip any leading namespaces/classes, keep only the base name.
14745 DW_AT_name for named DIEs does not contain the prefixes. */
14746 base = strrchr (DW_STRING (attr), ':');
14747 if (base && base > DW_STRING (attr) && base[-1] == ':')
14748 return &base[1];
14749 else
14750 return DW_STRING (attr);
14751 }
14752 }
14753 break;
14754
14755 default:
14756 break;
14757 }
14758
14759 if (!DW_STRING_IS_CANONICAL (attr))
14760 {
14761 DW_STRING (attr)
14762 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14763 &cu->objfile->objfile_obstack);
14764 DW_STRING_IS_CANONICAL (attr) = 1;
14765 }
14766 return DW_STRING (attr);
14767 }
14768
14769 /* Return the die that this die in an extension of, or NULL if there
14770 is none. *EXT_CU is the CU containing DIE on input, and the CU
14771 containing the return value on output. */
14772
14773 static struct die_info *
14774 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14775 {
14776 struct attribute *attr;
14777
14778 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14779 if (attr == NULL)
14780 return NULL;
14781
14782 return follow_die_ref (die, attr, ext_cu);
14783 }
14784
14785 /* Convert a DIE tag into its string name. */
14786
14787 static const char *
14788 dwarf_tag_name (unsigned tag)
14789 {
14790 const char *name = get_DW_TAG_name (tag);
14791
14792 if (name == NULL)
14793 return "DW_TAG_<unknown>";
14794
14795 return name;
14796 }
14797
14798 /* Convert a DWARF attribute code into its string name. */
14799
14800 static const char *
14801 dwarf_attr_name (unsigned attr)
14802 {
14803 const char *name;
14804
14805 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14806 if (attr == DW_AT_MIPS_fde)
14807 return "DW_AT_MIPS_fde";
14808 #else
14809 if (attr == DW_AT_HP_block_index)
14810 return "DW_AT_HP_block_index";
14811 #endif
14812
14813 name = get_DW_AT_name (attr);
14814
14815 if (name == NULL)
14816 return "DW_AT_<unknown>";
14817
14818 return name;
14819 }
14820
14821 /* Convert a DWARF value form code into its string name. */
14822
14823 static const char *
14824 dwarf_form_name (unsigned form)
14825 {
14826 const char *name = get_DW_FORM_name (form);
14827
14828 if (name == NULL)
14829 return "DW_FORM_<unknown>";
14830
14831 return name;
14832 }
14833
14834 static char *
14835 dwarf_bool_name (unsigned mybool)
14836 {
14837 if (mybool)
14838 return "TRUE";
14839 else
14840 return "FALSE";
14841 }
14842
14843 /* Convert a DWARF type code into its string name. */
14844
14845 static const char *
14846 dwarf_type_encoding_name (unsigned enc)
14847 {
14848 const char *name = get_DW_ATE_name (enc);
14849
14850 if (name == NULL)
14851 return "DW_ATE_<unknown>";
14852
14853 return name;
14854 }
14855
14856 static void
14857 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14858 {
14859 unsigned int i;
14860
14861 print_spaces (indent, f);
14862 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14863 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14864
14865 if (die->parent != NULL)
14866 {
14867 print_spaces (indent, f);
14868 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14869 die->parent->offset.sect_off);
14870 }
14871
14872 print_spaces (indent, f);
14873 fprintf_unfiltered (f, " has children: %s\n",
14874 dwarf_bool_name (die->child != NULL));
14875
14876 print_spaces (indent, f);
14877 fprintf_unfiltered (f, " attributes:\n");
14878
14879 for (i = 0; i < die->num_attrs; ++i)
14880 {
14881 print_spaces (indent, f);
14882 fprintf_unfiltered (f, " %s (%s) ",
14883 dwarf_attr_name (die->attrs[i].name),
14884 dwarf_form_name (die->attrs[i].form));
14885
14886 switch (die->attrs[i].form)
14887 {
14888 case DW_FORM_addr:
14889 case DW_FORM_GNU_addr_index:
14890 fprintf_unfiltered (f, "address: ");
14891 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14892 break;
14893 case DW_FORM_block2:
14894 case DW_FORM_block4:
14895 case DW_FORM_block:
14896 case DW_FORM_block1:
14897 fprintf_unfiltered (f, "block: size %d",
14898 DW_BLOCK (&die->attrs[i])->size);
14899 break;
14900 case DW_FORM_exprloc:
14901 fprintf_unfiltered (f, "expression: size %u",
14902 DW_BLOCK (&die->attrs[i])->size);
14903 break;
14904 case DW_FORM_ref_addr:
14905 fprintf_unfiltered (f, "ref address: ");
14906 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14907 break;
14908 case DW_FORM_ref1:
14909 case DW_FORM_ref2:
14910 case DW_FORM_ref4:
14911 case DW_FORM_ref8:
14912 case DW_FORM_ref_udata:
14913 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14914 (long) (DW_UNSND (&die->attrs[i])));
14915 break;
14916 case DW_FORM_data1:
14917 case DW_FORM_data2:
14918 case DW_FORM_data4:
14919 case DW_FORM_data8:
14920 case DW_FORM_udata:
14921 case DW_FORM_sdata:
14922 fprintf_unfiltered (f, "constant: %s",
14923 pulongest (DW_UNSND (&die->attrs[i])));
14924 break;
14925 case DW_FORM_sec_offset:
14926 fprintf_unfiltered (f, "section offset: %s",
14927 pulongest (DW_UNSND (&die->attrs[i])));
14928 break;
14929 case DW_FORM_ref_sig8:
14930 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14931 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14932 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14933 else
14934 fprintf_unfiltered (f, "signatured type, offset: unknown");
14935 break;
14936 case DW_FORM_string:
14937 case DW_FORM_strp:
14938 case DW_FORM_GNU_str_index:
14939 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14940 DW_STRING (&die->attrs[i])
14941 ? DW_STRING (&die->attrs[i]) : "",
14942 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14943 break;
14944 case DW_FORM_flag:
14945 if (DW_UNSND (&die->attrs[i]))
14946 fprintf_unfiltered (f, "flag: TRUE");
14947 else
14948 fprintf_unfiltered (f, "flag: FALSE");
14949 break;
14950 case DW_FORM_flag_present:
14951 fprintf_unfiltered (f, "flag: TRUE");
14952 break;
14953 case DW_FORM_indirect:
14954 /* The reader will have reduced the indirect form to
14955 the "base form" so this form should not occur. */
14956 fprintf_unfiltered (f,
14957 "unexpected attribute form: DW_FORM_indirect");
14958 break;
14959 default:
14960 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14961 die->attrs[i].form);
14962 break;
14963 }
14964 fprintf_unfiltered (f, "\n");
14965 }
14966 }
14967
14968 static void
14969 dump_die_for_error (struct die_info *die)
14970 {
14971 dump_die_shallow (gdb_stderr, 0, die);
14972 }
14973
14974 static void
14975 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14976 {
14977 int indent = level * 4;
14978
14979 gdb_assert (die != NULL);
14980
14981 if (level >= max_level)
14982 return;
14983
14984 dump_die_shallow (f, indent, die);
14985
14986 if (die->child != NULL)
14987 {
14988 print_spaces (indent, f);
14989 fprintf_unfiltered (f, " Children:");
14990 if (level + 1 < max_level)
14991 {
14992 fprintf_unfiltered (f, "\n");
14993 dump_die_1 (f, level + 1, max_level, die->child);
14994 }
14995 else
14996 {
14997 fprintf_unfiltered (f,
14998 " [not printed, max nesting level reached]\n");
14999 }
15000 }
15001
15002 if (die->sibling != NULL && level > 0)
15003 {
15004 dump_die_1 (f, level, max_level, die->sibling);
15005 }
15006 }
15007
15008 /* This is called from the pdie macro in gdbinit.in.
15009 It's not static so gcc will keep a copy callable from gdb. */
15010
15011 void
15012 dump_die (struct die_info *die, int max_level)
15013 {
15014 dump_die_1 (gdb_stdlog, 0, max_level, die);
15015 }
15016
15017 static void
15018 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15019 {
15020 void **slot;
15021
15022 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15023 INSERT);
15024
15025 *slot = die;
15026 }
15027
15028 /* DW_ADDR is always stored already as sect_offset; despite for the forms
15029 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15030
15031 static int
15032 is_ref_attr (struct attribute *attr)
15033 {
15034 switch (attr->form)
15035 {
15036 case DW_FORM_ref_addr:
15037 case DW_FORM_ref1:
15038 case DW_FORM_ref2:
15039 case DW_FORM_ref4:
15040 case DW_FORM_ref8:
15041 case DW_FORM_ref_udata:
15042 return 1;
15043 default:
15044 return 0;
15045 }
15046 }
15047
15048 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15049 required kind. */
15050
15051 static sect_offset
15052 dwarf2_get_ref_die_offset (struct attribute *attr)
15053 {
15054 sect_offset retval = { DW_UNSND (attr) };
15055
15056 if (is_ref_attr (attr))
15057 return retval;
15058
15059 retval.sect_off = 0;
15060 complaint (&symfile_complaints,
15061 _("unsupported die ref attribute form: '%s'"),
15062 dwarf_form_name (attr->form));
15063 return retval;
15064 }
15065
15066 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15067 * the value held by the attribute is not constant. */
15068
15069 static LONGEST
15070 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15071 {
15072 if (attr->form == DW_FORM_sdata)
15073 return DW_SND (attr);
15074 else if (attr->form == DW_FORM_udata
15075 || attr->form == DW_FORM_data1
15076 || attr->form == DW_FORM_data2
15077 || attr->form == DW_FORM_data4
15078 || attr->form == DW_FORM_data8)
15079 return DW_UNSND (attr);
15080 else
15081 {
15082 complaint (&symfile_complaints,
15083 _("Attribute value is not a constant (%s)"),
15084 dwarf_form_name (attr->form));
15085 return default_value;
15086 }
15087 }
15088
15089 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15090 unit and add it to our queue.
15091 The result is non-zero if PER_CU was queued, otherwise the result is zero
15092 meaning either PER_CU is already queued or it is already loaded. */
15093
15094 static int
15095 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15096 struct dwarf2_per_cu_data *per_cu,
15097 enum language pretend_language)
15098 {
15099 /* We may arrive here during partial symbol reading, if we need full
15100 DIEs to process an unusual case (e.g. template arguments). Do
15101 not queue PER_CU, just tell our caller to load its DIEs. */
15102 if (dwarf2_per_objfile->reading_partial_symbols)
15103 {
15104 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15105 return 1;
15106 return 0;
15107 }
15108
15109 /* Mark the dependence relation so that we don't flush PER_CU
15110 too early. */
15111 dwarf2_add_dependence (this_cu, per_cu);
15112
15113 /* If it's already on the queue, we have nothing to do. */
15114 if (per_cu->queued)
15115 return 0;
15116
15117 /* If the compilation unit is already loaded, just mark it as
15118 used. */
15119 if (per_cu->cu != NULL)
15120 {
15121 per_cu->cu->last_used = 0;
15122 return 0;
15123 }
15124
15125 /* Add it to the queue. */
15126 queue_comp_unit (per_cu, pretend_language);
15127
15128 return 1;
15129 }
15130
15131 /* Follow reference or signature attribute ATTR of SRC_DIE.
15132 On entry *REF_CU is the CU of SRC_DIE.
15133 On exit *REF_CU is the CU of the result. */
15134
15135 static struct die_info *
15136 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15137 struct dwarf2_cu **ref_cu)
15138 {
15139 struct die_info *die;
15140
15141 if (is_ref_attr (attr))
15142 die = follow_die_ref (src_die, attr, ref_cu);
15143 else if (attr->form == DW_FORM_ref_sig8)
15144 die = follow_die_sig (src_die, attr, ref_cu);
15145 else
15146 {
15147 dump_die_for_error (src_die);
15148 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15149 (*ref_cu)->objfile->name);
15150 }
15151
15152 return die;
15153 }
15154
15155 /* Follow reference OFFSET.
15156 On entry *REF_CU is the CU of the source die referencing OFFSET.
15157 On exit *REF_CU is the CU of the result.
15158 Returns NULL if OFFSET is invalid. */
15159
15160 static struct die_info *
15161 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15162 {
15163 struct die_info temp_die;
15164 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15165
15166 gdb_assert (cu->per_cu != NULL);
15167
15168 target_cu = cu;
15169
15170 if (cu->per_cu->is_debug_types)
15171 {
15172 /* .debug_types CUs cannot reference anything outside their CU.
15173 If they need to, they have to reference a signatured type via
15174 DW_FORM_ref_sig8. */
15175 if (! offset_in_cu_p (&cu->header, offset))
15176 return NULL;
15177 }
15178 else if (! offset_in_cu_p (&cu->header, offset))
15179 {
15180 struct dwarf2_per_cu_data *per_cu;
15181
15182 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15183
15184 /* If necessary, add it to the queue and load its DIEs. */
15185 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15186 load_full_comp_unit (per_cu, cu->language);
15187
15188 target_cu = per_cu->cu;
15189 }
15190 else if (cu->dies == NULL)
15191 {
15192 /* We're loading full DIEs during partial symbol reading. */
15193 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15194 load_full_comp_unit (cu->per_cu, language_minimal);
15195 }
15196
15197 *ref_cu = target_cu;
15198 temp_die.offset = offset;
15199 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15200 }
15201
15202 /* Follow reference attribute ATTR of SRC_DIE.
15203 On entry *REF_CU is the CU of SRC_DIE.
15204 On exit *REF_CU is the CU of the result. */
15205
15206 static struct die_info *
15207 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15208 struct dwarf2_cu **ref_cu)
15209 {
15210 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15211 struct dwarf2_cu *cu = *ref_cu;
15212 struct die_info *die;
15213
15214 die = follow_die_offset (offset, ref_cu);
15215 if (!die)
15216 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15217 "at 0x%x [in module %s]"),
15218 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15219
15220 return die;
15221 }
15222
15223 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15224 Returned value is intended for DW_OP_call*. Returned
15225 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15226
15227 struct dwarf2_locexpr_baton
15228 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15229 struct dwarf2_per_cu_data *per_cu,
15230 CORE_ADDR (*get_frame_pc) (void *baton),
15231 void *baton)
15232 {
15233 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15234 struct dwarf2_cu *cu;
15235 struct die_info *die;
15236 struct attribute *attr;
15237 struct dwarf2_locexpr_baton retval;
15238
15239 dw2_setup (per_cu->objfile);
15240
15241 if (per_cu->cu == NULL)
15242 load_cu (per_cu);
15243 cu = per_cu->cu;
15244
15245 die = follow_die_offset (offset, &cu);
15246 if (!die)
15247 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15248 offset.sect_off, per_cu->objfile->name);
15249
15250 attr = dwarf2_attr (die, DW_AT_location, cu);
15251 if (!attr)
15252 {
15253 /* DWARF: "If there is no such attribute, then there is no effect.".
15254 DATA is ignored if SIZE is 0. */
15255
15256 retval.data = NULL;
15257 retval.size = 0;
15258 }
15259 else if (attr_form_is_section_offset (attr))
15260 {
15261 struct dwarf2_loclist_baton loclist_baton;
15262 CORE_ADDR pc = (*get_frame_pc) (baton);
15263 size_t size;
15264
15265 fill_in_loclist_baton (cu, &loclist_baton, attr);
15266
15267 retval.data = dwarf2_find_location_expression (&loclist_baton,
15268 &size, pc);
15269 retval.size = size;
15270 }
15271 else
15272 {
15273 if (!attr_form_is_block (attr))
15274 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15275 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15276 offset.sect_off, per_cu->objfile->name);
15277
15278 retval.data = DW_BLOCK (attr)->data;
15279 retval.size = DW_BLOCK (attr)->size;
15280 }
15281 retval.per_cu = cu->per_cu;
15282
15283 age_cached_comp_units ();
15284
15285 return retval;
15286 }
15287
15288 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15289 PER_CU. */
15290
15291 struct type *
15292 dwarf2_get_die_type (cu_offset die_offset,
15293 struct dwarf2_per_cu_data *per_cu)
15294 {
15295 sect_offset die_offset_sect;
15296
15297 dw2_setup (per_cu->objfile);
15298
15299 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15300 return get_die_type_at_offset (die_offset_sect, per_cu);
15301 }
15302
15303 /* Follow the signature attribute ATTR in SRC_DIE.
15304 On entry *REF_CU is the CU of SRC_DIE.
15305 On exit *REF_CU is the CU of the result. */
15306
15307 static struct die_info *
15308 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15309 struct dwarf2_cu **ref_cu)
15310 {
15311 struct objfile *objfile = (*ref_cu)->objfile;
15312 struct die_info temp_die;
15313 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15314 struct dwarf2_cu *sig_cu;
15315 struct die_info *die;
15316
15317 /* sig_type will be NULL if the signatured type is missing from
15318 the debug info. */
15319 if (sig_type == NULL)
15320 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15321 "at 0x%x [in module %s]"),
15322 src_die->offset.sect_off, objfile->name);
15323
15324 /* If necessary, add it to the queue and load its DIEs. */
15325
15326 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15327 read_signatured_type (sig_type);
15328
15329 gdb_assert (sig_type->per_cu.cu != NULL);
15330
15331 sig_cu = sig_type->per_cu.cu;
15332 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15333 temp_die.offset = sig_type->type_offset_in_section;
15334 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15335 temp_die.offset.sect_off);
15336 if (die)
15337 {
15338 *ref_cu = sig_cu;
15339 return die;
15340 }
15341
15342 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15343 "from DIE at 0x%x [in module %s]"),
15344 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15345 }
15346
15347 /* Given an offset of a signatured type, return its signatured_type. */
15348
15349 static struct signatured_type *
15350 lookup_signatured_type_at_offset (struct objfile *objfile,
15351 struct dwarf2_section_info *section,
15352 sect_offset offset)
15353 {
15354 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15355 unsigned int length, initial_length_size;
15356 unsigned int sig_offset;
15357 struct signatured_type find_entry, *sig_type;
15358
15359 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15360 sig_offset = (initial_length_size
15361 + 2 /*version*/
15362 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15363 + 1 /*address_size*/);
15364 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15365 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15366
15367 /* This is only used to lookup previously recorded types.
15368 If we didn't find it, it's our bug. */
15369 gdb_assert (sig_type != NULL);
15370 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15371
15372 return sig_type;
15373 }
15374
15375 /* Load the DIEs associated with type unit PER_CU into memory. */
15376
15377 static void
15378 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15379 {
15380 struct objfile *objfile = per_cu->objfile;
15381 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15382 sect_offset offset = per_cu->offset;
15383 struct signatured_type *sig_type;
15384
15385 dwarf2_read_section (objfile, sect);
15386
15387 /* We have the section offset, but we need the signature to do the
15388 hash table lookup. */
15389 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15390 the signature to assert we found the right one.
15391 Ok, but it's a lot of work. We should simplify things so any needed
15392 assert doesn't require all this clumsiness. */
15393 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15394
15395 gdb_assert (&sig_type->per_cu == per_cu);
15396 gdb_assert (sig_type->per_cu.cu == NULL);
15397
15398 read_signatured_type (sig_type);
15399
15400 gdb_assert (sig_type->per_cu.cu != NULL);
15401 }
15402
15403 /* die_reader_func for read_signatured_type.
15404 This is identical to load_full_comp_unit_reader,
15405 but is kept separate for now. */
15406
15407 static void
15408 read_signatured_type_reader (const struct die_reader_specs *reader,
15409 gdb_byte *info_ptr,
15410 struct die_info *comp_unit_die,
15411 int has_children,
15412 void *data)
15413 {
15414 struct dwarf2_cu *cu = reader->cu;
15415
15416 gdb_assert (cu->die_hash == NULL);
15417 cu->die_hash =
15418 htab_create_alloc_ex (cu->header.length / 12,
15419 die_hash,
15420 die_eq,
15421 NULL,
15422 &cu->comp_unit_obstack,
15423 hashtab_obstack_allocate,
15424 dummy_obstack_deallocate);
15425
15426 if (has_children)
15427 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15428 &info_ptr, comp_unit_die);
15429 cu->dies = comp_unit_die;
15430 /* comp_unit_die is not stored in die_hash, no need. */
15431
15432 /* We try not to read any attributes in this function, because not
15433 all CUs needed for references have been loaded yet, and symbol
15434 table processing isn't initialized. But we have to set the CU language,
15435 or we won't be able to build types correctly.
15436 Similarly, if we do not read the producer, we can not apply
15437 producer-specific interpretation. */
15438 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15439 }
15440
15441 /* Read in a signatured type and build its CU and DIEs.
15442 If the type is a stub for the real type in a DWO file,
15443 read in the real type from the DWO file as well. */
15444
15445 static void
15446 read_signatured_type (struct signatured_type *sig_type)
15447 {
15448 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15449
15450 gdb_assert (per_cu->is_debug_types);
15451 gdb_assert (per_cu->cu == NULL);
15452
15453 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15454 }
15455
15456 /* Decode simple location descriptions.
15457 Given a pointer to a dwarf block that defines a location, compute
15458 the location and return the value.
15459
15460 NOTE drow/2003-11-18: This function is called in two situations
15461 now: for the address of static or global variables (partial symbols
15462 only) and for offsets into structures which are expected to be
15463 (more or less) constant. The partial symbol case should go away,
15464 and only the constant case should remain. That will let this
15465 function complain more accurately. A few special modes are allowed
15466 without complaint for global variables (for instance, global
15467 register values and thread-local values).
15468
15469 A location description containing no operations indicates that the
15470 object is optimized out. The return value is 0 for that case.
15471 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15472 callers will only want a very basic result and this can become a
15473 complaint.
15474
15475 Note that stack[0] is unused except as a default error return. */
15476
15477 static CORE_ADDR
15478 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15479 {
15480 struct objfile *objfile = cu->objfile;
15481 int i;
15482 int size = blk->size;
15483 gdb_byte *data = blk->data;
15484 CORE_ADDR stack[64];
15485 int stacki;
15486 unsigned int bytes_read, unsnd;
15487 gdb_byte op;
15488
15489 i = 0;
15490 stacki = 0;
15491 stack[stacki] = 0;
15492 stack[++stacki] = 0;
15493
15494 while (i < size)
15495 {
15496 op = data[i++];
15497 switch (op)
15498 {
15499 case DW_OP_lit0:
15500 case DW_OP_lit1:
15501 case DW_OP_lit2:
15502 case DW_OP_lit3:
15503 case DW_OP_lit4:
15504 case DW_OP_lit5:
15505 case DW_OP_lit6:
15506 case DW_OP_lit7:
15507 case DW_OP_lit8:
15508 case DW_OP_lit9:
15509 case DW_OP_lit10:
15510 case DW_OP_lit11:
15511 case DW_OP_lit12:
15512 case DW_OP_lit13:
15513 case DW_OP_lit14:
15514 case DW_OP_lit15:
15515 case DW_OP_lit16:
15516 case DW_OP_lit17:
15517 case DW_OP_lit18:
15518 case DW_OP_lit19:
15519 case DW_OP_lit20:
15520 case DW_OP_lit21:
15521 case DW_OP_lit22:
15522 case DW_OP_lit23:
15523 case DW_OP_lit24:
15524 case DW_OP_lit25:
15525 case DW_OP_lit26:
15526 case DW_OP_lit27:
15527 case DW_OP_lit28:
15528 case DW_OP_lit29:
15529 case DW_OP_lit30:
15530 case DW_OP_lit31:
15531 stack[++stacki] = op - DW_OP_lit0;
15532 break;
15533
15534 case DW_OP_reg0:
15535 case DW_OP_reg1:
15536 case DW_OP_reg2:
15537 case DW_OP_reg3:
15538 case DW_OP_reg4:
15539 case DW_OP_reg5:
15540 case DW_OP_reg6:
15541 case DW_OP_reg7:
15542 case DW_OP_reg8:
15543 case DW_OP_reg9:
15544 case DW_OP_reg10:
15545 case DW_OP_reg11:
15546 case DW_OP_reg12:
15547 case DW_OP_reg13:
15548 case DW_OP_reg14:
15549 case DW_OP_reg15:
15550 case DW_OP_reg16:
15551 case DW_OP_reg17:
15552 case DW_OP_reg18:
15553 case DW_OP_reg19:
15554 case DW_OP_reg20:
15555 case DW_OP_reg21:
15556 case DW_OP_reg22:
15557 case DW_OP_reg23:
15558 case DW_OP_reg24:
15559 case DW_OP_reg25:
15560 case DW_OP_reg26:
15561 case DW_OP_reg27:
15562 case DW_OP_reg28:
15563 case DW_OP_reg29:
15564 case DW_OP_reg30:
15565 case DW_OP_reg31:
15566 stack[++stacki] = op - DW_OP_reg0;
15567 if (i < size)
15568 dwarf2_complex_location_expr_complaint ();
15569 break;
15570
15571 case DW_OP_regx:
15572 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15573 i += bytes_read;
15574 stack[++stacki] = unsnd;
15575 if (i < size)
15576 dwarf2_complex_location_expr_complaint ();
15577 break;
15578
15579 case DW_OP_addr:
15580 stack[++stacki] = read_address (objfile->obfd, &data[i],
15581 cu, &bytes_read);
15582 i += bytes_read;
15583 break;
15584
15585 case DW_OP_const1u:
15586 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15587 i += 1;
15588 break;
15589
15590 case DW_OP_const1s:
15591 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15592 i += 1;
15593 break;
15594
15595 case DW_OP_const2u:
15596 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15597 i += 2;
15598 break;
15599
15600 case DW_OP_const2s:
15601 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15602 i += 2;
15603 break;
15604
15605 case DW_OP_const4u:
15606 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15607 i += 4;
15608 break;
15609
15610 case DW_OP_const4s:
15611 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15612 i += 4;
15613 break;
15614
15615 case DW_OP_const8u:
15616 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15617 i += 8;
15618 break;
15619
15620 case DW_OP_constu:
15621 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15622 &bytes_read);
15623 i += bytes_read;
15624 break;
15625
15626 case DW_OP_consts:
15627 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15628 i += bytes_read;
15629 break;
15630
15631 case DW_OP_dup:
15632 stack[stacki + 1] = stack[stacki];
15633 stacki++;
15634 break;
15635
15636 case DW_OP_plus:
15637 stack[stacki - 1] += stack[stacki];
15638 stacki--;
15639 break;
15640
15641 case DW_OP_plus_uconst:
15642 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15643 &bytes_read);
15644 i += bytes_read;
15645 break;
15646
15647 case DW_OP_minus:
15648 stack[stacki - 1] -= stack[stacki];
15649 stacki--;
15650 break;
15651
15652 case DW_OP_deref:
15653 /* If we're not the last op, then we definitely can't encode
15654 this using GDB's address_class enum. This is valid for partial
15655 global symbols, although the variable's address will be bogus
15656 in the psymtab. */
15657 if (i < size)
15658 dwarf2_complex_location_expr_complaint ();
15659 break;
15660
15661 case DW_OP_GNU_push_tls_address:
15662 /* The top of the stack has the offset from the beginning
15663 of the thread control block at which the variable is located. */
15664 /* Nothing should follow this operator, so the top of stack would
15665 be returned. */
15666 /* This is valid for partial global symbols, but the variable's
15667 address will be bogus in the psymtab. Make it always at least
15668 non-zero to not look as a variable garbage collected by linker
15669 which have DW_OP_addr 0. */
15670 if (i < size)
15671 dwarf2_complex_location_expr_complaint ();
15672 stack[stacki]++;
15673 break;
15674
15675 case DW_OP_GNU_uninit:
15676 break;
15677
15678 case DW_OP_GNU_addr_index:
15679 case DW_OP_GNU_const_index:
15680 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15681 &bytes_read);
15682 i += bytes_read;
15683 break;
15684
15685 default:
15686 {
15687 const char *name = get_DW_OP_name (op);
15688
15689 if (name)
15690 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15691 name);
15692 else
15693 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15694 op);
15695 }
15696
15697 return (stack[stacki]);
15698 }
15699
15700 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15701 outside of the allocated space. Also enforce minimum>0. */
15702 if (stacki >= ARRAY_SIZE (stack) - 1)
15703 {
15704 complaint (&symfile_complaints,
15705 _("location description stack overflow"));
15706 return 0;
15707 }
15708
15709 if (stacki <= 0)
15710 {
15711 complaint (&symfile_complaints,
15712 _("location description stack underflow"));
15713 return 0;
15714 }
15715 }
15716 return (stack[stacki]);
15717 }
15718
15719 /* memory allocation interface */
15720
15721 static struct dwarf_block *
15722 dwarf_alloc_block (struct dwarf2_cu *cu)
15723 {
15724 struct dwarf_block *blk;
15725
15726 blk = (struct dwarf_block *)
15727 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15728 return (blk);
15729 }
15730
15731 static struct abbrev_info *
15732 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15733 {
15734 struct abbrev_info *abbrev;
15735
15736 abbrev = (struct abbrev_info *)
15737 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15738 memset (abbrev, 0, sizeof (struct abbrev_info));
15739 return (abbrev);
15740 }
15741
15742 static struct die_info *
15743 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15744 {
15745 struct die_info *die;
15746 size_t size = sizeof (struct die_info);
15747
15748 if (num_attrs > 1)
15749 size += (num_attrs - 1) * sizeof (struct attribute);
15750
15751 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15752 memset (die, 0, sizeof (struct die_info));
15753 return (die);
15754 }
15755
15756 \f
15757 /* Macro support. */
15758
15759 /* Return the full name of file number I in *LH's file name table.
15760 Use COMP_DIR as the name of the current directory of the
15761 compilation. The result is allocated using xmalloc; the caller is
15762 responsible for freeing it. */
15763 static char *
15764 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15765 {
15766 /* Is the file number a valid index into the line header's file name
15767 table? Remember that file numbers start with one, not zero. */
15768 if (1 <= file && file <= lh->num_file_names)
15769 {
15770 struct file_entry *fe = &lh->file_names[file - 1];
15771
15772 if (IS_ABSOLUTE_PATH (fe->name))
15773 return xstrdup (fe->name);
15774 else
15775 {
15776 const char *dir;
15777 int dir_len;
15778 char *full_name;
15779
15780 if (fe->dir_index)
15781 dir = lh->include_dirs[fe->dir_index - 1];
15782 else
15783 dir = comp_dir;
15784
15785 if (dir)
15786 {
15787 dir_len = strlen (dir);
15788 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15789 strcpy (full_name, dir);
15790 full_name[dir_len] = '/';
15791 strcpy (full_name + dir_len + 1, fe->name);
15792 return full_name;
15793 }
15794 else
15795 return xstrdup (fe->name);
15796 }
15797 }
15798 else
15799 {
15800 /* The compiler produced a bogus file number. We can at least
15801 record the macro definitions made in the file, even if we
15802 won't be able to find the file by name. */
15803 char fake_name[80];
15804
15805 sprintf (fake_name, "<bad macro file number %d>", file);
15806
15807 complaint (&symfile_complaints,
15808 _("bad file number in macro information (%d)"),
15809 file);
15810
15811 return xstrdup (fake_name);
15812 }
15813 }
15814
15815
15816 static struct macro_source_file *
15817 macro_start_file (int file, int line,
15818 struct macro_source_file *current_file,
15819 const char *comp_dir,
15820 struct line_header *lh, struct objfile *objfile)
15821 {
15822 /* The full name of this source file. */
15823 char *full_name = file_full_name (file, lh, comp_dir);
15824
15825 /* We don't create a macro table for this compilation unit
15826 at all until we actually get a filename. */
15827 if (! pending_macros)
15828 pending_macros = new_macro_table (&objfile->objfile_obstack,
15829 objfile->macro_cache);
15830
15831 if (! current_file)
15832 {
15833 /* If we have no current file, then this must be the start_file
15834 directive for the compilation unit's main source file. */
15835 current_file = macro_set_main (pending_macros, full_name);
15836 macro_define_special (pending_macros);
15837 }
15838 else
15839 current_file = macro_include (current_file, line, full_name);
15840
15841 xfree (full_name);
15842
15843 return current_file;
15844 }
15845
15846
15847 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15848 followed by a null byte. */
15849 static char *
15850 copy_string (const char *buf, int len)
15851 {
15852 char *s = xmalloc (len + 1);
15853
15854 memcpy (s, buf, len);
15855 s[len] = '\0';
15856 return s;
15857 }
15858
15859
15860 static const char *
15861 consume_improper_spaces (const char *p, const char *body)
15862 {
15863 if (*p == ' ')
15864 {
15865 complaint (&symfile_complaints,
15866 _("macro definition contains spaces "
15867 "in formal argument list:\n`%s'"),
15868 body);
15869
15870 while (*p == ' ')
15871 p++;
15872 }
15873
15874 return p;
15875 }
15876
15877
15878 static void
15879 parse_macro_definition (struct macro_source_file *file, int line,
15880 const char *body)
15881 {
15882 const char *p;
15883
15884 /* The body string takes one of two forms. For object-like macro
15885 definitions, it should be:
15886
15887 <macro name> " " <definition>
15888
15889 For function-like macro definitions, it should be:
15890
15891 <macro name> "() " <definition>
15892 or
15893 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15894
15895 Spaces may appear only where explicitly indicated, and in the
15896 <definition>.
15897
15898 The Dwarf 2 spec says that an object-like macro's name is always
15899 followed by a space, but versions of GCC around March 2002 omit
15900 the space when the macro's definition is the empty string.
15901
15902 The Dwarf 2 spec says that there should be no spaces between the
15903 formal arguments in a function-like macro's formal argument list,
15904 but versions of GCC around March 2002 include spaces after the
15905 commas. */
15906
15907
15908 /* Find the extent of the macro name. The macro name is terminated
15909 by either a space or null character (for an object-like macro) or
15910 an opening paren (for a function-like macro). */
15911 for (p = body; *p; p++)
15912 if (*p == ' ' || *p == '(')
15913 break;
15914
15915 if (*p == ' ' || *p == '\0')
15916 {
15917 /* It's an object-like macro. */
15918 int name_len = p - body;
15919 char *name = copy_string (body, name_len);
15920 const char *replacement;
15921
15922 if (*p == ' ')
15923 replacement = body + name_len + 1;
15924 else
15925 {
15926 dwarf2_macro_malformed_definition_complaint (body);
15927 replacement = body + name_len;
15928 }
15929
15930 macro_define_object (file, line, name, replacement);
15931
15932 xfree (name);
15933 }
15934 else if (*p == '(')
15935 {
15936 /* It's a function-like macro. */
15937 char *name = copy_string (body, p - body);
15938 int argc = 0;
15939 int argv_size = 1;
15940 char **argv = xmalloc (argv_size * sizeof (*argv));
15941
15942 p++;
15943
15944 p = consume_improper_spaces (p, body);
15945
15946 /* Parse the formal argument list. */
15947 while (*p && *p != ')')
15948 {
15949 /* Find the extent of the current argument name. */
15950 const char *arg_start = p;
15951
15952 while (*p && *p != ',' && *p != ')' && *p != ' ')
15953 p++;
15954
15955 if (! *p || p == arg_start)
15956 dwarf2_macro_malformed_definition_complaint (body);
15957 else
15958 {
15959 /* Make sure argv has room for the new argument. */
15960 if (argc >= argv_size)
15961 {
15962 argv_size *= 2;
15963 argv = xrealloc (argv, argv_size * sizeof (*argv));
15964 }
15965
15966 argv[argc++] = copy_string (arg_start, p - arg_start);
15967 }
15968
15969 p = consume_improper_spaces (p, body);
15970
15971 /* Consume the comma, if present. */
15972 if (*p == ',')
15973 {
15974 p++;
15975
15976 p = consume_improper_spaces (p, body);
15977 }
15978 }
15979
15980 if (*p == ')')
15981 {
15982 p++;
15983
15984 if (*p == ' ')
15985 /* Perfectly formed definition, no complaints. */
15986 macro_define_function (file, line, name,
15987 argc, (const char **) argv,
15988 p + 1);
15989 else if (*p == '\0')
15990 {
15991 /* Complain, but do define it. */
15992 dwarf2_macro_malformed_definition_complaint (body);
15993 macro_define_function (file, line, name,
15994 argc, (const char **) argv,
15995 p);
15996 }
15997 else
15998 /* Just complain. */
15999 dwarf2_macro_malformed_definition_complaint (body);
16000 }
16001 else
16002 /* Just complain. */
16003 dwarf2_macro_malformed_definition_complaint (body);
16004
16005 xfree (name);
16006 {
16007 int i;
16008
16009 for (i = 0; i < argc; i++)
16010 xfree (argv[i]);
16011 }
16012 xfree (argv);
16013 }
16014 else
16015 dwarf2_macro_malformed_definition_complaint (body);
16016 }
16017
16018 /* Skip some bytes from BYTES according to the form given in FORM.
16019 Returns the new pointer. */
16020
16021 static gdb_byte *
16022 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
16023 enum dwarf_form form,
16024 unsigned int offset_size,
16025 struct dwarf2_section_info *section)
16026 {
16027 unsigned int bytes_read;
16028
16029 switch (form)
16030 {
16031 case DW_FORM_data1:
16032 case DW_FORM_flag:
16033 ++bytes;
16034 break;
16035
16036 case DW_FORM_data2:
16037 bytes += 2;
16038 break;
16039
16040 case DW_FORM_data4:
16041 bytes += 4;
16042 break;
16043
16044 case DW_FORM_data8:
16045 bytes += 8;
16046 break;
16047
16048 case DW_FORM_string:
16049 read_direct_string (abfd, bytes, &bytes_read);
16050 bytes += bytes_read;
16051 break;
16052
16053 case DW_FORM_sec_offset:
16054 case DW_FORM_strp:
16055 bytes += offset_size;
16056 break;
16057
16058 case DW_FORM_block:
16059 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16060 bytes += bytes_read;
16061 break;
16062
16063 case DW_FORM_block1:
16064 bytes += 1 + read_1_byte (abfd, bytes);
16065 break;
16066 case DW_FORM_block2:
16067 bytes += 2 + read_2_bytes (abfd, bytes);
16068 break;
16069 case DW_FORM_block4:
16070 bytes += 4 + read_4_bytes (abfd, bytes);
16071 break;
16072
16073 case DW_FORM_sdata:
16074 case DW_FORM_udata:
16075 case DW_FORM_GNU_addr_index:
16076 case DW_FORM_GNU_str_index:
16077 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16078 if (bytes == NULL)
16079 {
16080 dwarf2_section_buffer_overflow_complaint (section);
16081 return NULL;
16082 }
16083 break;
16084
16085 default:
16086 {
16087 complain:
16088 complaint (&symfile_complaints,
16089 _("invalid form 0x%x in `%s'"),
16090 form,
16091 section->asection->name);
16092 return NULL;
16093 }
16094 }
16095
16096 return bytes;
16097 }
16098
16099 /* A helper for dwarf_decode_macros that handles skipping an unknown
16100 opcode. Returns an updated pointer to the macro data buffer; or,
16101 on error, issues a complaint and returns NULL. */
16102
16103 static gdb_byte *
16104 skip_unknown_opcode (unsigned int opcode,
16105 gdb_byte **opcode_definitions,
16106 gdb_byte *mac_ptr, gdb_byte *mac_end,
16107 bfd *abfd,
16108 unsigned int offset_size,
16109 struct dwarf2_section_info *section)
16110 {
16111 unsigned int bytes_read, i;
16112 unsigned long arg;
16113 gdb_byte *defn;
16114
16115 if (opcode_definitions[opcode] == NULL)
16116 {
16117 complaint (&symfile_complaints,
16118 _("unrecognized DW_MACFINO opcode 0x%x"),
16119 opcode);
16120 return NULL;
16121 }
16122
16123 defn = opcode_definitions[opcode];
16124 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16125 defn += bytes_read;
16126
16127 for (i = 0; i < arg; ++i)
16128 {
16129 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16130 section);
16131 if (mac_ptr == NULL)
16132 {
16133 /* skip_form_bytes already issued the complaint. */
16134 return NULL;
16135 }
16136 }
16137
16138 return mac_ptr;
16139 }
16140
16141 /* A helper function which parses the header of a macro section.
16142 If the macro section is the extended (for now called "GNU") type,
16143 then this updates *OFFSET_SIZE. Returns a pointer to just after
16144 the header, or issues a complaint and returns NULL on error. */
16145
16146 static gdb_byte *
16147 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16148 bfd *abfd,
16149 gdb_byte *mac_ptr,
16150 unsigned int *offset_size,
16151 int section_is_gnu)
16152 {
16153 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16154
16155 if (section_is_gnu)
16156 {
16157 unsigned int version, flags;
16158
16159 version = read_2_bytes (abfd, mac_ptr);
16160 if (version != 4)
16161 {
16162 complaint (&symfile_complaints,
16163 _("unrecognized version `%d' in .debug_macro section"),
16164 version);
16165 return NULL;
16166 }
16167 mac_ptr += 2;
16168
16169 flags = read_1_byte (abfd, mac_ptr);
16170 ++mac_ptr;
16171 *offset_size = (flags & 1) ? 8 : 4;
16172
16173 if ((flags & 2) != 0)
16174 /* We don't need the line table offset. */
16175 mac_ptr += *offset_size;
16176
16177 /* Vendor opcode descriptions. */
16178 if ((flags & 4) != 0)
16179 {
16180 unsigned int i, count;
16181
16182 count = read_1_byte (abfd, mac_ptr);
16183 ++mac_ptr;
16184 for (i = 0; i < count; ++i)
16185 {
16186 unsigned int opcode, bytes_read;
16187 unsigned long arg;
16188
16189 opcode = read_1_byte (abfd, mac_ptr);
16190 ++mac_ptr;
16191 opcode_definitions[opcode] = mac_ptr;
16192 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16193 mac_ptr += bytes_read;
16194 mac_ptr += arg;
16195 }
16196 }
16197 }
16198
16199 return mac_ptr;
16200 }
16201
16202 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16203 including DW_MACRO_GNU_transparent_include. */
16204
16205 static void
16206 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16207 struct macro_source_file *current_file,
16208 struct line_header *lh, char *comp_dir,
16209 struct dwarf2_section_info *section,
16210 int section_is_gnu,
16211 unsigned int offset_size,
16212 struct objfile *objfile,
16213 htab_t include_hash)
16214 {
16215 enum dwarf_macro_record_type macinfo_type;
16216 int at_commandline;
16217 gdb_byte *opcode_definitions[256];
16218
16219 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16220 &offset_size, section_is_gnu);
16221 if (mac_ptr == NULL)
16222 {
16223 /* We already issued a complaint. */
16224 return;
16225 }
16226
16227 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16228 GDB is still reading the definitions from command line. First
16229 DW_MACINFO_start_file will need to be ignored as it was already executed
16230 to create CURRENT_FILE for the main source holding also the command line
16231 definitions. On first met DW_MACINFO_start_file this flag is reset to
16232 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16233
16234 at_commandline = 1;
16235
16236 do
16237 {
16238 /* Do we at least have room for a macinfo type byte? */
16239 if (mac_ptr >= mac_end)
16240 {
16241 dwarf2_section_buffer_overflow_complaint (section);
16242 break;
16243 }
16244
16245 macinfo_type = read_1_byte (abfd, mac_ptr);
16246 mac_ptr++;
16247
16248 /* Note that we rely on the fact that the corresponding GNU and
16249 DWARF constants are the same. */
16250 switch (macinfo_type)
16251 {
16252 /* A zero macinfo type indicates the end of the macro
16253 information. */
16254 case 0:
16255 break;
16256
16257 case DW_MACRO_GNU_define:
16258 case DW_MACRO_GNU_undef:
16259 case DW_MACRO_GNU_define_indirect:
16260 case DW_MACRO_GNU_undef_indirect:
16261 {
16262 unsigned int bytes_read;
16263 int line;
16264 char *body;
16265 int is_define;
16266
16267 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16268 mac_ptr += bytes_read;
16269
16270 if (macinfo_type == DW_MACRO_GNU_define
16271 || macinfo_type == DW_MACRO_GNU_undef)
16272 {
16273 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16274 mac_ptr += bytes_read;
16275 }
16276 else
16277 {
16278 LONGEST str_offset;
16279
16280 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16281 mac_ptr += offset_size;
16282
16283 body = read_indirect_string_at_offset (abfd, str_offset);
16284 }
16285
16286 is_define = (macinfo_type == DW_MACRO_GNU_define
16287 || macinfo_type == DW_MACRO_GNU_define_indirect);
16288 if (! current_file)
16289 {
16290 /* DWARF violation as no main source is present. */
16291 complaint (&symfile_complaints,
16292 _("debug info with no main source gives macro %s "
16293 "on line %d: %s"),
16294 is_define ? _("definition") : _("undefinition"),
16295 line, body);
16296 break;
16297 }
16298 if ((line == 0 && !at_commandline)
16299 || (line != 0 && at_commandline))
16300 complaint (&symfile_complaints,
16301 _("debug info gives %s macro %s with %s line %d: %s"),
16302 at_commandline ? _("command-line") : _("in-file"),
16303 is_define ? _("definition") : _("undefinition"),
16304 line == 0 ? _("zero") : _("non-zero"), line, body);
16305
16306 if (is_define)
16307 parse_macro_definition (current_file, line, body);
16308 else
16309 {
16310 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16311 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16312 macro_undef (current_file, line, body);
16313 }
16314 }
16315 break;
16316
16317 case DW_MACRO_GNU_start_file:
16318 {
16319 unsigned int bytes_read;
16320 int line, file;
16321
16322 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16323 mac_ptr += bytes_read;
16324 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16325 mac_ptr += bytes_read;
16326
16327 if ((line == 0 && !at_commandline)
16328 || (line != 0 && at_commandline))
16329 complaint (&symfile_complaints,
16330 _("debug info gives source %d included "
16331 "from %s at %s line %d"),
16332 file, at_commandline ? _("command-line") : _("file"),
16333 line == 0 ? _("zero") : _("non-zero"), line);
16334
16335 if (at_commandline)
16336 {
16337 /* This DW_MACRO_GNU_start_file was executed in the
16338 pass one. */
16339 at_commandline = 0;
16340 }
16341 else
16342 current_file = macro_start_file (file, line,
16343 current_file, comp_dir,
16344 lh, objfile);
16345 }
16346 break;
16347
16348 case DW_MACRO_GNU_end_file:
16349 if (! current_file)
16350 complaint (&symfile_complaints,
16351 _("macro debug info has an unmatched "
16352 "`close_file' directive"));
16353 else
16354 {
16355 current_file = current_file->included_by;
16356 if (! current_file)
16357 {
16358 enum dwarf_macro_record_type next_type;
16359
16360 /* GCC circa March 2002 doesn't produce the zero
16361 type byte marking the end of the compilation
16362 unit. Complain if it's not there, but exit no
16363 matter what. */
16364
16365 /* Do we at least have room for a macinfo type byte? */
16366 if (mac_ptr >= mac_end)
16367 {
16368 dwarf2_section_buffer_overflow_complaint (section);
16369 return;
16370 }
16371
16372 /* We don't increment mac_ptr here, so this is just
16373 a look-ahead. */
16374 next_type = read_1_byte (abfd, mac_ptr);
16375 if (next_type != 0)
16376 complaint (&symfile_complaints,
16377 _("no terminating 0-type entry for "
16378 "macros in `.debug_macinfo' section"));
16379
16380 return;
16381 }
16382 }
16383 break;
16384
16385 case DW_MACRO_GNU_transparent_include:
16386 {
16387 LONGEST offset;
16388 void **slot;
16389
16390 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16391 mac_ptr += offset_size;
16392
16393 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16394 if (*slot != NULL)
16395 {
16396 /* This has actually happened; see
16397 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16398 complaint (&symfile_complaints,
16399 _("recursive DW_MACRO_GNU_transparent_include in "
16400 ".debug_macro section"));
16401 }
16402 else
16403 {
16404 *slot = mac_ptr;
16405
16406 dwarf_decode_macro_bytes (abfd,
16407 section->buffer + offset,
16408 mac_end, current_file,
16409 lh, comp_dir,
16410 section, section_is_gnu,
16411 offset_size, objfile, include_hash);
16412
16413 htab_remove_elt (include_hash, mac_ptr);
16414 }
16415 }
16416 break;
16417
16418 case DW_MACINFO_vendor_ext:
16419 if (!section_is_gnu)
16420 {
16421 unsigned int bytes_read;
16422 int constant;
16423
16424 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16425 mac_ptr += bytes_read;
16426 read_direct_string (abfd, mac_ptr, &bytes_read);
16427 mac_ptr += bytes_read;
16428
16429 /* We don't recognize any vendor extensions. */
16430 break;
16431 }
16432 /* FALLTHROUGH */
16433
16434 default:
16435 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16436 mac_ptr, mac_end, abfd, offset_size,
16437 section);
16438 if (mac_ptr == NULL)
16439 return;
16440 break;
16441 }
16442 } while (macinfo_type != 0);
16443 }
16444
16445 static void
16446 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16447 char *comp_dir, int section_is_gnu)
16448 {
16449 struct objfile *objfile = dwarf2_per_objfile->objfile;
16450 struct line_header *lh = cu->line_header;
16451 bfd *abfd;
16452 gdb_byte *mac_ptr, *mac_end;
16453 struct macro_source_file *current_file = 0;
16454 enum dwarf_macro_record_type macinfo_type;
16455 unsigned int offset_size = cu->header.offset_size;
16456 gdb_byte *opcode_definitions[256];
16457 struct cleanup *cleanup;
16458 htab_t include_hash;
16459 void **slot;
16460 struct dwarf2_section_info *section;
16461 const char *section_name;
16462
16463 if (cu->dwo_unit != NULL)
16464 {
16465 if (section_is_gnu)
16466 {
16467 section = &cu->dwo_unit->dwo_file->sections.macro;
16468 section_name = ".debug_macro.dwo";
16469 }
16470 else
16471 {
16472 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16473 section_name = ".debug_macinfo.dwo";
16474 }
16475 }
16476 else
16477 {
16478 if (section_is_gnu)
16479 {
16480 section = &dwarf2_per_objfile->macro;
16481 section_name = ".debug_macro";
16482 }
16483 else
16484 {
16485 section = &dwarf2_per_objfile->macinfo;
16486 section_name = ".debug_macinfo";
16487 }
16488 }
16489
16490 dwarf2_read_section (objfile, section);
16491 if (section->buffer == NULL)
16492 {
16493 complaint (&symfile_complaints, _("missing %s section"), section_name);
16494 return;
16495 }
16496 abfd = section->asection->owner;
16497
16498 /* First pass: Find the name of the base filename.
16499 This filename is needed in order to process all macros whose definition
16500 (or undefinition) comes from the command line. These macros are defined
16501 before the first DW_MACINFO_start_file entry, and yet still need to be
16502 associated to the base file.
16503
16504 To determine the base file name, we scan the macro definitions until we
16505 reach the first DW_MACINFO_start_file entry. We then initialize
16506 CURRENT_FILE accordingly so that any macro definition found before the
16507 first DW_MACINFO_start_file can still be associated to the base file. */
16508
16509 mac_ptr = section->buffer + offset;
16510 mac_end = section->buffer + section->size;
16511
16512 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16513 &offset_size, section_is_gnu);
16514 if (mac_ptr == NULL)
16515 {
16516 /* We already issued a complaint. */
16517 return;
16518 }
16519
16520 do
16521 {
16522 /* Do we at least have room for a macinfo type byte? */
16523 if (mac_ptr >= mac_end)
16524 {
16525 /* Complaint is printed during the second pass as GDB will probably
16526 stop the first pass earlier upon finding
16527 DW_MACINFO_start_file. */
16528 break;
16529 }
16530
16531 macinfo_type = read_1_byte (abfd, mac_ptr);
16532 mac_ptr++;
16533
16534 /* Note that we rely on the fact that the corresponding GNU and
16535 DWARF constants are the same. */
16536 switch (macinfo_type)
16537 {
16538 /* A zero macinfo type indicates the end of the macro
16539 information. */
16540 case 0:
16541 break;
16542
16543 case DW_MACRO_GNU_define:
16544 case DW_MACRO_GNU_undef:
16545 /* Only skip the data by MAC_PTR. */
16546 {
16547 unsigned int bytes_read;
16548
16549 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16550 mac_ptr += bytes_read;
16551 read_direct_string (abfd, mac_ptr, &bytes_read);
16552 mac_ptr += bytes_read;
16553 }
16554 break;
16555
16556 case DW_MACRO_GNU_start_file:
16557 {
16558 unsigned int bytes_read;
16559 int line, file;
16560
16561 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16562 mac_ptr += bytes_read;
16563 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16564 mac_ptr += bytes_read;
16565
16566 current_file = macro_start_file (file, line, current_file,
16567 comp_dir, lh, objfile);
16568 }
16569 break;
16570
16571 case DW_MACRO_GNU_end_file:
16572 /* No data to skip by MAC_PTR. */
16573 break;
16574
16575 case DW_MACRO_GNU_define_indirect:
16576 case DW_MACRO_GNU_undef_indirect:
16577 {
16578 unsigned int bytes_read;
16579
16580 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16581 mac_ptr += bytes_read;
16582 mac_ptr += offset_size;
16583 }
16584 break;
16585
16586 case DW_MACRO_GNU_transparent_include:
16587 /* Note that, according to the spec, a transparent include
16588 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16589 skip this opcode. */
16590 mac_ptr += offset_size;
16591 break;
16592
16593 case DW_MACINFO_vendor_ext:
16594 /* Only skip the data by MAC_PTR. */
16595 if (!section_is_gnu)
16596 {
16597 unsigned int bytes_read;
16598
16599 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16600 mac_ptr += bytes_read;
16601 read_direct_string (abfd, mac_ptr, &bytes_read);
16602 mac_ptr += bytes_read;
16603 }
16604 /* FALLTHROUGH */
16605
16606 default:
16607 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16608 mac_ptr, mac_end, abfd, offset_size,
16609 section);
16610 if (mac_ptr == NULL)
16611 return;
16612 break;
16613 }
16614 } while (macinfo_type != 0 && current_file == NULL);
16615
16616 /* Second pass: Process all entries.
16617
16618 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16619 command-line macro definitions/undefinitions. This flag is unset when we
16620 reach the first DW_MACINFO_start_file entry. */
16621
16622 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16623 NULL, xcalloc, xfree);
16624 cleanup = make_cleanup_htab_delete (include_hash);
16625 mac_ptr = section->buffer + offset;
16626 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16627 *slot = mac_ptr;
16628 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16629 current_file, lh, comp_dir, section, section_is_gnu,
16630 offset_size, objfile, include_hash);
16631 do_cleanups (cleanup);
16632 }
16633
16634 /* Check if the attribute's form is a DW_FORM_block*
16635 if so return true else false. */
16636
16637 static int
16638 attr_form_is_block (struct attribute *attr)
16639 {
16640 return (attr == NULL ? 0 :
16641 attr->form == DW_FORM_block1
16642 || attr->form == DW_FORM_block2
16643 || attr->form == DW_FORM_block4
16644 || attr->form == DW_FORM_block
16645 || attr->form == DW_FORM_exprloc);
16646 }
16647
16648 /* Return non-zero if ATTR's value is a section offset --- classes
16649 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16650 You may use DW_UNSND (attr) to retrieve such offsets.
16651
16652 Section 7.5.4, "Attribute Encodings", explains that no attribute
16653 may have a value that belongs to more than one of these classes; it
16654 would be ambiguous if we did, because we use the same forms for all
16655 of them. */
16656
16657 static int
16658 attr_form_is_section_offset (struct attribute *attr)
16659 {
16660 return (attr->form == DW_FORM_data4
16661 || attr->form == DW_FORM_data8
16662 || attr->form == DW_FORM_sec_offset);
16663 }
16664
16665 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16666 zero otherwise. When this function returns true, you can apply
16667 dwarf2_get_attr_constant_value to it.
16668
16669 However, note that for some attributes you must check
16670 attr_form_is_section_offset before using this test. DW_FORM_data4
16671 and DW_FORM_data8 are members of both the constant class, and of
16672 the classes that contain offsets into other debug sections
16673 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16674 that, if an attribute's can be either a constant or one of the
16675 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16676 taken as section offsets, not constants. */
16677
16678 static int
16679 attr_form_is_constant (struct attribute *attr)
16680 {
16681 switch (attr->form)
16682 {
16683 case DW_FORM_sdata:
16684 case DW_FORM_udata:
16685 case DW_FORM_data1:
16686 case DW_FORM_data2:
16687 case DW_FORM_data4:
16688 case DW_FORM_data8:
16689 return 1;
16690 default:
16691 return 0;
16692 }
16693 }
16694
16695 /* Return the .debug_loc section to use for CU.
16696 For DWO files use .debug_loc.dwo. */
16697
16698 static struct dwarf2_section_info *
16699 cu_debug_loc_section (struct dwarf2_cu *cu)
16700 {
16701 if (cu->dwo_unit)
16702 return &cu->dwo_unit->dwo_file->sections.loc;
16703 return &dwarf2_per_objfile->loc;
16704 }
16705
16706 /* A helper function that fills in a dwarf2_loclist_baton. */
16707
16708 static void
16709 fill_in_loclist_baton (struct dwarf2_cu *cu,
16710 struct dwarf2_loclist_baton *baton,
16711 struct attribute *attr)
16712 {
16713 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16714
16715 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16716
16717 baton->per_cu = cu->per_cu;
16718 gdb_assert (baton->per_cu);
16719 /* We don't know how long the location list is, but make sure we
16720 don't run off the edge of the section. */
16721 baton->size = section->size - DW_UNSND (attr);
16722 baton->data = section->buffer + DW_UNSND (attr);
16723 baton->base_address = cu->base_address;
16724 baton->from_dwo = cu->dwo_unit != NULL;
16725 }
16726
16727 static void
16728 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16729 struct dwarf2_cu *cu)
16730 {
16731 struct objfile *objfile = dwarf2_per_objfile->objfile;
16732 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16733
16734 if (attr_form_is_section_offset (attr)
16735 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16736 the section. If so, fall through to the complaint in the
16737 other branch. */
16738 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16739 {
16740 struct dwarf2_loclist_baton *baton;
16741
16742 baton = obstack_alloc (&objfile->objfile_obstack,
16743 sizeof (struct dwarf2_loclist_baton));
16744
16745 fill_in_loclist_baton (cu, baton, attr);
16746
16747 if (cu->base_known == 0)
16748 complaint (&symfile_complaints,
16749 _("Location list used without "
16750 "specifying the CU base address."));
16751
16752 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16753 SYMBOL_LOCATION_BATON (sym) = baton;
16754 }
16755 else
16756 {
16757 struct dwarf2_locexpr_baton *baton;
16758
16759 baton = obstack_alloc (&objfile->objfile_obstack,
16760 sizeof (struct dwarf2_locexpr_baton));
16761 baton->per_cu = cu->per_cu;
16762 gdb_assert (baton->per_cu);
16763
16764 if (attr_form_is_block (attr))
16765 {
16766 /* Note that we're just copying the block's data pointer
16767 here, not the actual data. We're still pointing into the
16768 info_buffer for SYM's objfile; right now we never release
16769 that buffer, but when we do clean up properly this may
16770 need to change. */
16771 baton->size = DW_BLOCK (attr)->size;
16772 baton->data = DW_BLOCK (attr)->data;
16773 }
16774 else
16775 {
16776 dwarf2_invalid_attrib_class_complaint ("location description",
16777 SYMBOL_NATURAL_NAME (sym));
16778 baton->size = 0;
16779 }
16780
16781 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16782 SYMBOL_LOCATION_BATON (sym) = baton;
16783 }
16784 }
16785
16786 /* Return the OBJFILE associated with the compilation unit CU. If CU
16787 came from a separate debuginfo file, then the master objfile is
16788 returned. */
16789
16790 struct objfile *
16791 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16792 {
16793 struct objfile *objfile = per_cu->objfile;
16794
16795 /* Return the master objfile, so that we can report and look up the
16796 correct file containing this variable. */
16797 if (objfile->separate_debug_objfile_backlink)
16798 objfile = objfile->separate_debug_objfile_backlink;
16799
16800 return objfile;
16801 }
16802
16803 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16804 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16805 CU_HEADERP first. */
16806
16807 static const struct comp_unit_head *
16808 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16809 struct dwarf2_per_cu_data *per_cu)
16810 {
16811 struct objfile *objfile;
16812 struct dwarf2_per_objfile *per_objfile;
16813 gdb_byte *info_ptr;
16814
16815 if (per_cu->cu)
16816 return &per_cu->cu->header;
16817
16818 objfile = per_cu->objfile;
16819 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16820 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16821
16822 memset (cu_headerp, 0, sizeof (*cu_headerp));
16823 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16824
16825 return cu_headerp;
16826 }
16827
16828 /* Return the address size given in the compilation unit header for CU. */
16829
16830 int
16831 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16832 {
16833 struct comp_unit_head cu_header_local;
16834 const struct comp_unit_head *cu_headerp;
16835
16836 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16837
16838 return cu_headerp->addr_size;
16839 }
16840
16841 /* Return the offset size given in the compilation unit header for CU. */
16842
16843 int
16844 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16845 {
16846 struct comp_unit_head cu_header_local;
16847 const struct comp_unit_head *cu_headerp;
16848
16849 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16850
16851 return cu_headerp->offset_size;
16852 }
16853
16854 /* See its dwarf2loc.h declaration. */
16855
16856 int
16857 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16858 {
16859 struct comp_unit_head cu_header_local;
16860 const struct comp_unit_head *cu_headerp;
16861
16862 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16863
16864 if (cu_headerp->version == 2)
16865 return cu_headerp->addr_size;
16866 else
16867 return cu_headerp->offset_size;
16868 }
16869
16870 /* Return the text offset of the CU. The returned offset comes from
16871 this CU's objfile. If this objfile came from a separate debuginfo
16872 file, then the offset may be different from the corresponding
16873 offset in the parent objfile. */
16874
16875 CORE_ADDR
16876 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16877 {
16878 struct objfile *objfile = per_cu->objfile;
16879
16880 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16881 }
16882
16883 /* Locate the .debug_info compilation unit from CU's objfile which contains
16884 the DIE at OFFSET. Raises an error on failure. */
16885
16886 static struct dwarf2_per_cu_data *
16887 dwarf2_find_containing_comp_unit (sect_offset offset,
16888 struct objfile *objfile)
16889 {
16890 struct dwarf2_per_cu_data *this_cu;
16891 int low, high;
16892
16893 low = 0;
16894 high = dwarf2_per_objfile->n_comp_units - 1;
16895 while (high > low)
16896 {
16897 int mid = low + (high - low) / 2;
16898
16899 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16900 >= offset.sect_off)
16901 high = mid;
16902 else
16903 low = mid + 1;
16904 }
16905 gdb_assert (low == high);
16906 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16907 > offset.sect_off)
16908 {
16909 if (low == 0)
16910 error (_("Dwarf Error: could not find partial DIE containing "
16911 "offset 0x%lx [in module %s]"),
16912 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16913
16914 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16915 <= offset.sect_off);
16916 return dwarf2_per_objfile->all_comp_units[low-1];
16917 }
16918 else
16919 {
16920 this_cu = dwarf2_per_objfile->all_comp_units[low];
16921 if (low == dwarf2_per_objfile->n_comp_units - 1
16922 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16923 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16924 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16925 return this_cu;
16926 }
16927 }
16928
16929 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16930
16931 static void
16932 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16933 {
16934 memset (cu, 0, sizeof (*cu));
16935 per_cu->cu = cu;
16936 cu->per_cu = per_cu;
16937 cu->objfile = per_cu->objfile;
16938 obstack_init (&cu->comp_unit_obstack);
16939 }
16940
16941 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16942
16943 static void
16944 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16945 enum language pretend_language)
16946 {
16947 struct attribute *attr;
16948
16949 /* Set the language we're debugging. */
16950 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16951 if (attr)
16952 set_cu_language (DW_UNSND (attr), cu);
16953 else
16954 {
16955 cu->language = pretend_language;
16956 cu->language_defn = language_def (cu->language);
16957 }
16958
16959 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16960 if (attr)
16961 cu->producer = DW_STRING (attr);
16962 }
16963
16964 /* Release one cached compilation unit, CU. We unlink it from the tree
16965 of compilation units, but we don't remove it from the read_in_chain;
16966 the caller is responsible for that.
16967 NOTE: DATA is a void * because this function is also used as a
16968 cleanup routine. */
16969
16970 static void
16971 free_heap_comp_unit (void *data)
16972 {
16973 struct dwarf2_cu *cu = data;
16974
16975 gdb_assert (cu->per_cu != NULL);
16976 cu->per_cu->cu = NULL;
16977 cu->per_cu = NULL;
16978
16979 obstack_free (&cu->comp_unit_obstack, NULL);
16980
16981 xfree (cu);
16982 }
16983
16984 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16985 when we're finished with it. We can't free the pointer itself, but be
16986 sure to unlink it from the cache. Also release any associated storage. */
16987
16988 static void
16989 free_stack_comp_unit (void *data)
16990 {
16991 struct dwarf2_cu *cu = data;
16992
16993 gdb_assert (cu->per_cu != NULL);
16994 cu->per_cu->cu = NULL;
16995 cu->per_cu = NULL;
16996
16997 obstack_free (&cu->comp_unit_obstack, NULL);
16998 cu->partial_dies = NULL;
16999 }
17000
17001 /* Free all cached compilation units. */
17002
17003 static void
17004 free_cached_comp_units (void *data)
17005 {
17006 struct dwarf2_per_cu_data *per_cu, **last_chain;
17007
17008 per_cu = dwarf2_per_objfile->read_in_chain;
17009 last_chain = &dwarf2_per_objfile->read_in_chain;
17010 while (per_cu != NULL)
17011 {
17012 struct dwarf2_per_cu_data *next_cu;
17013
17014 next_cu = per_cu->cu->read_in_chain;
17015
17016 free_heap_comp_unit (per_cu->cu);
17017 *last_chain = next_cu;
17018
17019 per_cu = next_cu;
17020 }
17021 }
17022
17023 /* Increase the age counter on each cached compilation unit, and free
17024 any that are too old. */
17025
17026 static void
17027 age_cached_comp_units (void)
17028 {
17029 struct dwarf2_per_cu_data *per_cu, **last_chain;
17030
17031 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17032 per_cu = dwarf2_per_objfile->read_in_chain;
17033 while (per_cu != NULL)
17034 {
17035 per_cu->cu->last_used ++;
17036 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17037 dwarf2_mark (per_cu->cu);
17038 per_cu = per_cu->cu->read_in_chain;
17039 }
17040
17041 per_cu = dwarf2_per_objfile->read_in_chain;
17042 last_chain = &dwarf2_per_objfile->read_in_chain;
17043 while (per_cu != NULL)
17044 {
17045 struct dwarf2_per_cu_data *next_cu;
17046
17047 next_cu = per_cu->cu->read_in_chain;
17048
17049 if (!per_cu->cu->mark)
17050 {
17051 free_heap_comp_unit (per_cu->cu);
17052 *last_chain = next_cu;
17053 }
17054 else
17055 last_chain = &per_cu->cu->read_in_chain;
17056
17057 per_cu = next_cu;
17058 }
17059 }
17060
17061 /* Remove a single compilation unit from the cache. */
17062
17063 static void
17064 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17065 {
17066 struct dwarf2_per_cu_data *per_cu, **last_chain;
17067
17068 per_cu = dwarf2_per_objfile->read_in_chain;
17069 last_chain = &dwarf2_per_objfile->read_in_chain;
17070 while (per_cu != NULL)
17071 {
17072 struct dwarf2_per_cu_data *next_cu;
17073
17074 next_cu = per_cu->cu->read_in_chain;
17075
17076 if (per_cu == target_per_cu)
17077 {
17078 free_heap_comp_unit (per_cu->cu);
17079 per_cu->cu = NULL;
17080 *last_chain = next_cu;
17081 break;
17082 }
17083 else
17084 last_chain = &per_cu->cu->read_in_chain;
17085
17086 per_cu = next_cu;
17087 }
17088 }
17089
17090 /* Release all extra memory associated with OBJFILE. */
17091
17092 void
17093 dwarf2_free_objfile (struct objfile *objfile)
17094 {
17095 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17096
17097 if (dwarf2_per_objfile == NULL)
17098 return;
17099
17100 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17101 free_cached_comp_units (NULL);
17102
17103 if (dwarf2_per_objfile->quick_file_names_table)
17104 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17105
17106 /* Everything else should be on the objfile obstack. */
17107 }
17108
17109 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17110 We store these in a hash table separate from the DIEs, and preserve them
17111 when the DIEs are flushed out of cache.
17112
17113 The CU "per_cu" pointer is needed because offset alone is not enough to
17114 uniquely identify the type. A file may have multiple .debug_types sections,
17115 or the type may come from a DWO file. We have to use something in
17116 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17117 routine, get_die_type_at_offset, from outside this file, and thus won't
17118 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17119 of the objfile. */
17120
17121 struct dwarf2_per_cu_offset_and_type
17122 {
17123 const struct dwarf2_per_cu_data *per_cu;
17124 sect_offset offset;
17125 struct type *type;
17126 };
17127
17128 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17129
17130 static hashval_t
17131 per_cu_offset_and_type_hash (const void *item)
17132 {
17133 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17134
17135 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17136 }
17137
17138 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17139
17140 static int
17141 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17142 {
17143 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17144 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17145
17146 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17147 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17148 }
17149
17150 /* Set the type associated with DIE to TYPE. Save it in CU's hash
17151 table if necessary. For convenience, return TYPE.
17152
17153 The DIEs reading must have careful ordering to:
17154 * Not cause infite loops trying to read in DIEs as a prerequisite for
17155 reading current DIE.
17156 * Not trying to dereference contents of still incompletely read in types
17157 while reading in other DIEs.
17158 * Enable referencing still incompletely read in types just by a pointer to
17159 the type without accessing its fields.
17160
17161 Therefore caller should follow these rules:
17162 * Try to fetch any prerequisite types we may need to build this DIE type
17163 before building the type and calling set_die_type.
17164 * After building type call set_die_type for current DIE as soon as
17165 possible before fetching more types to complete the current type.
17166 * Make the type as complete as possible before fetching more types. */
17167
17168 static struct type *
17169 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17170 {
17171 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17172 struct objfile *objfile = cu->objfile;
17173
17174 /* For Ada types, make sure that the gnat-specific data is always
17175 initialized (if not already set). There are a few types where
17176 we should not be doing so, because the type-specific area is
17177 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17178 where the type-specific area is used to store the floatformat).
17179 But this is not a problem, because the gnat-specific information
17180 is actually not needed for these types. */
17181 if (need_gnat_info (cu)
17182 && TYPE_CODE (type) != TYPE_CODE_FUNC
17183 && TYPE_CODE (type) != TYPE_CODE_FLT
17184 && !HAVE_GNAT_AUX_INFO (type))
17185 INIT_GNAT_SPECIFIC (type);
17186
17187 if (dwarf2_per_objfile->die_type_hash == NULL)
17188 {
17189 dwarf2_per_objfile->die_type_hash =
17190 htab_create_alloc_ex (127,
17191 per_cu_offset_and_type_hash,
17192 per_cu_offset_and_type_eq,
17193 NULL,
17194 &objfile->objfile_obstack,
17195 hashtab_obstack_allocate,
17196 dummy_obstack_deallocate);
17197 }
17198
17199 ofs.per_cu = cu->per_cu;
17200 ofs.offset = die->offset;
17201 ofs.type = type;
17202 slot = (struct dwarf2_per_cu_offset_and_type **)
17203 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17204 if (*slot)
17205 complaint (&symfile_complaints,
17206 _("A problem internal to GDB: DIE 0x%x has type already set"),
17207 die->offset.sect_off);
17208 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17209 **slot = ofs;
17210 return type;
17211 }
17212
17213 /* Look up the type for the die at OFFSET in the appropriate type_hash
17214 table, or return NULL if the die does not have a saved type. */
17215
17216 static struct type *
17217 get_die_type_at_offset (sect_offset offset,
17218 struct dwarf2_per_cu_data *per_cu)
17219 {
17220 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17221
17222 if (dwarf2_per_objfile->die_type_hash == NULL)
17223 return NULL;
17224
17225 ofs.per_cu = per_cu;
17226 ofs.offset = offset;
17227 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17228 if (slot)
17229 return slot->type;
17230 else
17231 return NULL;
17232 }
17233
17234 /* Look up the type for DIE in the appropriate type_hash table,
17235 or return NULL if DIE does not have a saved type. */
17236
17237 static struct type *
17238 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17239 {
17240 return get_die_type_at_offset (die->offset, cu->per_cu);
17241 }
17242
17243 /* Add a dependence relationship from CU to REF_PER_CU. */
17244
17245 static void
17246 dwarf2_add_dependence (struct dwarf2_cu *cu,
17247 struct dwarf2_per_cu_data *ref_per_cu)
17248 {
17249 void **slot;
17250
17251 if (cu->dependencies == NULL)
17252 cu->dependencies
17253 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17254 NULL, &cu->comp_unit_obstack,
17255 hashtab_obstack_allocate,
17256 dummy_obstack_deallocate);
17257
17258 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17259 if (*slot == NULL)
17260 *slot = ref_per_cu;
17261 }
17262
17263 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17264 Set the mark field in every compilation unit in the
17265 cache that we must keep because we are keeping CU. */
17266
17267 static int
17268 dwarf2_mark_helper (void **slot, void *data)
17269 {
17270 struct dwarf2_per_cu_data *per_cu;
17271
17272 per_cu = (struct dwarf2_per_cu_data *) *slot;
17273
17274 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17275 reading of the chain. As such dependencies remain valid it is not much
17276 useful to track and undo them during QUIT cleanups. */
17277 if (per_cu->cu == NULL)
17278 return 1;
17279
17280 if (per_cu->cu->mark)
17281 return 1;
17282 per_cu->cu->mark = 1;
17283
17284 if (per_cu->cu->dependencies != NULL)
17285 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17286
17287 return 1;
17288 }
17289
17290 /* Set the mark field in CU and in every other compilation unit in the
17291 cache that we must keep because we are keeping CU. */
17292
17293 static void
17294 dwarf2_mark (struct dwarf2_cu *cu)
17295 {
17296 if (cu->mark)
17297 return;
17298 cu->mark = 1;
17299 if (cu->dependencies != NULL)
17300 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17301 }
17302
17303 static void
17304 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17305 {
17306 while (per_cu)
17307 {
17308 per_cu->cu->mark = 0;
17309 per_cu = per_cu->cu->read_in_chain;
17310 }
17311 }
17312
17313 /* Trivial hash function for partial_die_info: the hash value of a DIE
17314 is its offset in .debug_info for this objfile. */
17315
17316 static hashval_t
17317 partial_die_hash (const void *item)
17318 {
17319 const struct partial_die_info *part_die = item;
17320
17321 return part_die->offset.sect_off;
17322 }
17323
17324 /* Trivial comparison function for partial_die_info structures: two DIEs
17325 are equal if they have the same offset. */
17326
17327 static int
17328 partial_die_eq (const void *item_lhs, const void *item_rhs)
17329 {
17330 const struct partial_die_info *part_die_lhs = item_lhs;
17331 const struct partial_die_info *part_die_rhs = item_rhs;
17332
17333 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17334 }
17335
17336 static struct cmd_list_element *set_dwarf2_cmdlist;
17337 static struct cmd_list_element *show_dwarf2_cmdlist;
17338
17339 static void
17340 set_dwarf2_cmd (char *args, int from_tty)
17341 {
17342 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17343 }
17344
17345 static void
17346 show_dwarf2_cmd (char *args, int from_tty)
17347 {
17348 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17349 }
17350
17351 /* If section described by INFO was mmapped, munmap it now. */
17352
17353 static void
17354 munmap_section_buffer (struct dwarf2_section_info *info)
17355 {
17356 if (info->map_addr != NULL)
17357 {
17358 #ifdef HAVE_MMAP
17359 int res;
17360
17361 res = munmap (info->map_addr, info->map_len);
17362 gdb_assert (res == 0);
17363 #else
17364 /* Without HAVE_MMAP, we should never be here to begin with. */
17365 gdb_assert_not_reached ("no mmap support");
17366 #endif
17367 }
17368 }
17369
17370 /* munmap debug sections for OBJFILE, if necessary. */
17371
17372 static void
17373 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17374 {
17375 struct dwarf2_per_objfile *data = d;
17376 int ix;
17377 struct dwarf2_section_info *section;
17378
17379 /* This is sorted according to the order they're defined in to make it easier
17380 to keep in sync. */
17381 munmap_section_buffer (&data->info);
17382 munmap_section_buffer (&data->abbrev);
17383 munmap_section_buffer (&data->line);
17384 munmap_section_buffer (&data->loc);
17385 munmap_section_buffer (&data->macinfo);
17386 munmap_section_buffer (&data->macro);
17387 munmap_section_buffer (&data->str);
17388 munmap_section_buffer (&data->ranges);
17389 munmap_section_buffer (&data->addr);
17390 munmap_section_buffer (&data->frame);
17391 munmap_section_buffer (&data->eh_frame);
17392 munmap_section_buffer (&data->gdb_index);
17393
17394 for (ix = 0;
17395 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17396 ++ix)
17397 munmap_section_buffer (section);
17398
17399 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17400 VEC_free (dwarf2_per_cu_ptr,
17401 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17402
17403 VEC_free (dwarf2_section_info_def, data->types);
17404
17405 if (data->dwo_files)
17406 free_dwo_files (data->dwo_files, objfile);
17407 }
17408
17409 \f
17410 /* The "save gdb-index" command. */
17411
17412 /* The contents of the hash table we create when building the string
17413 table. */
17414 struct strtab_entry
17415 {
17416 offset_type offset;
17417 const char *str;
17418 };
17419
17420 /* Hash function for a strtab_entry.
17421
17422 Function is used only during write_hash_table so no index format backward
17423 compatibility is needed. */
17424
17425 static hashval_t
17426 hash_strtab_entry (const void *e)
17427 {
17428 const struct strtab_entry *entry = e;
17429 return mapped_index_string_hash (INT_MAX, entry->str);
17430 }
17431
17432 /* Equality function for a strtab_entry. */
17433
17434 static int
17435 eq_strtab_entry (const void *a, const void *b)
17436 {
17437 const struct strtab_entry *ea = a;
17438 const struct strtab_entry *eb = b;
17439 return !strcmp (ea->str, eb->str);
17440 }
17441
17442 /* Create a strtab_entry hash table. */
17443
17444 static htab_t
17445 create_strtab (void)
17446 {
17447 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17448 xfree, xcalloc, xfree);
17449 }
17450
17451 /* Add a string to the constant pool. Return the string's offset in
17452 host order. */
17453
17454 static offset_type
17455 add_string (htab_t table, struct obstack *cpool, const char *str)
17456 {
17457 void **slot;
17458 struct strtab_entry entry;
17459 struct strtab_entry *result;
17460
17461 entry.str = str;
17462 slot = htab_find_slot (table, &entry, INSERT);
17463 if (*slot)
17464 result = *slot;
17465 else
17466 {
17467 result = XNEW (struct strtab_entry);
17468 result->offset = obstack_object_size (cpool);
17469 result->str = str;
17470 obstack_grow_str0 (cpool, str);
17471 *slot = result;
17472 }
17473 return result->offset;
17474 }
17475
17476 /* An entry in the symbol table. */
17477 struct symtab_index_entry
17478 {
17479 /* The name of the symbol. */
17480 const char *name;
17481 /* The offset of the name in the constant pool. */
17482 offset_type index_offset;
17483 /* A sorted vector of the indices of all the CUs that hold an object
17484 of this name. */
17485 VEC (offset_type) *cu_indices;
17486 };
17487
17488 /* The symbol table. This is a power-of-2-sized hash table. */
17489 struct mapped_symtab
17490 {
17491 offset_type n_elements;
17492 offset_type size;
17493 struct symtab_index_entry **data;
17494 };
17495
17496 /* Hash function for a symtab_index_entry. */
17497
17498 static hashval_t
17499 hash_symtab_entry (const void *e)
17500 {
17501 const struct symtab_index_entry *entry = e;
17502 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17503 sizeof (offset_type) * VEC_length (offset_type,
17504 entry->cu_indices),
17505 0);
17506 }
17507
17508 /* Equality function for a symtab_index_entry. */
17509
17510 static int
17511 eq_symtab_entry (const void *a, const void *b)
17512 {
17513 const struct symtab_index_entry *ea = a;
17514 const struct symtab_index_entry *eb = b;
17515 int len = VEC_length (offset_type, ea->cu_indices);
17516 if (len != VEC_length (offset_type, eb->cu_indices))
17517 return 0;
17518 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17519 VEC_address (offset_type, eb->cu_indices),
17520 sizeof (offset_type) * len);
17521 }
17522
17523 /* Destroy a symtab_index_entry. */
17524
17525 static void
17526 delete_symtab_entry (void *p)
17527 {
17528 struct symtab_index_entry *entry = p;
17529 VEC_free (offset_type, entry->cu_indices);
17530 xfree (entry);
17531 }
17532
17533 /* Create a hash table holding symtab_index_entry objects. */
17534
17535 static htab_t
17536 create_symbol_hash_table (void)
17537 {
17538 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17539 delete_symtab_entry, xcalloc, xfree);
17540 }
17541
17542 /* Create a new mapped symtab object. */
17543
17544 static struct mapped_symtab *
17545 create_mapped_symtab (void)
17546 {
17547 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17548 symtab->n_elements = 0;
17549 symtab->size = 1024;
17550 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17551 return symtab;
17552 }
17553
17554 /* Destroy a mapped_symtab. */
17555
17556 static void
17557 cleanup_mapped_symtab (void *p)
17558 {
17559 struct mapped_symtab *symtab = p;
17560 /* The contents of the array are freed when the other hash table is
17561 destroyed. */
17562 xfree (symtab->data);
17563 xfree (symtab);
17564 }
17565
17566 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17567 the slot.
17568
17569 Function is used only during write_hash_table so no index format backward
17570 compatibility is needed. */
17571
17572 static struct symtab_index_entry **
17573 find_slot (struct mapped_symtab *symtab, const char *name)
17574 {
17575 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17576
17577 index = hash & (symtab->size - 1);
17578 step = ((hash * 17) & (symtab->size - 1)) | 1;
17579
17580 for (;;)
17581 {
17582 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17583 return &symtab->data[index];
17584 index = (index + step) & (symtab->size - 1);
17585 }
17586 }
17587
17588 /* Expand SYMTAB's hash table. */
17589
17590 static void
17591 hash_expand (struct mapped_symtab *symtab)
17592 {
17593 offset_type old_size = symtab->size;
17594 offset_type i;
17595 struct symtab_index_entry **old_entries = symtab->data;
17596
17597 symtab->size *= 2;
17598 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17599
17600 for (i = 0; i < old_size; ++i)
17601 {
17602 if (old_entries[i])
17603 {
17604 struct symtab_index_entry **slot = find_slot (symtab,
17605 old_entries[i]->name);
17606 *slot = old_entries[i];
17607 }
17608 }
17609
17610 xfree (old_entries);
17611 }
17612
17613 /* Add an entry to SYMTAB. NAME is the name of the symbol.
17614 CU_INDEX is the index of the CU in which the symbol appears.
17615 IS_STATIC is one if the symbol is static, otherwise zero (global). */
17616
17617 static void
17618 add_index_entry (struct mapped_symtab *symtab, const char *name,
17619 int is_static, gdb_index_symbol_kind kind,
17620 offset_type cu_index)
17621 {
17622 struct symtab_index_entry **slot;
17623 offset_type cu_index_and_attrs;
17624
17625 ++symtab->n_elements;
17626 if (4 * symtab->n_elements / 3 >= symtab->size)
17627 hash_expand (symtab);
17628
17629 slot = find_slot (symtab, name);
17630 if (!*slot)
17631 {
17632 *slot = XNEW (struct symtab_index_entry);
17633 (*slot)->name = name;
17634 /* index_offset is set later. */
17635 (*slot)->cu_indices = NULL;
17636 }
17637
17638 cu_index_and_attrs = 0;
17639 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17640 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17641 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17642
17643 /* We don't want to record an index value twice as we want to avoid the
17644 duplication.
17645 We process all global symbols and then all static symbols
17646 (which would allow us to avoid the duplication by only having to check
17647 the last entry pushed), but a symbol could have multiple kinds in one CU.
17648 To keep things simple we don't worry about the duplication here and
17649 sort and uniqufy the list after we've processed all symbols. */
17650 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17651 }
17652
17653 /* qsort helper routine for uniquify_cu_indices. */
17654
17655 static int
17656 offset_type_compare (const void *ap, const void *bp)
17657 {
17658 offset_type a = *(offset_type *) ap;
17659 offset_type b = *(offset_type *) bp;
17660
17661 return (a > b) - (b > a);
17662 }
17663
17664 /* Sort and remove duplicates of all symbols' cu_indices lists. */
17665
17666 static void
17667 uniquify_cu_indices (struct mapped_symtab *symtab)
17668 {
17669 int i;
17670
17671 for (i = 0; i < symtab->size; ++i)
17672 {
17673 struct symtab_index_entry *entry = symtab->data[i];
17674
17675 if (entry
17676 && entry->cu_indices != NULL)
17677 {
17678 unsigned int next_to_insert, next_to_check;
17679 offset_type last_value;
17680
17681 qsort (VEC_address (offset_type, entry->cu_indices),
17682 VEC_length (offset_type, entry->cu_indices),
17683 sizeof (offset_type), offset_type_compare);
17684
17685 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17686 next_to_insert = 1;
17687 for (next_to_check = 1;
17688 next_to_check < VEC_length (offset_type, entry->cu_indices);
17689 ++next_to_check)
17690 {
17691 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17692 != last_value)
17693 {
17694 last_value = VEC_index (offset_type, entry->cu_indices,
17695 next_to_check);
17696 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17697 last_value);
17698 ++next_to_insert;
17699 }
17700 }
17701 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17702 }
17703 }
17704 }
17705
17706 /* Add a vector of indices to the constant pool. */
17707
17708 static offset_type
17709 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17710 struct symtab_index_entry *entry)
17711 {
17712 void **slot;
17713
17714 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17715 if (!*slot)
17716 {
17717 offset_type len = VEC_length (offset_type, entry->cu_indices);
17718 offset_type val = MAYBE_SWAP (len);
17719 offset_type iter;
17720 int i;
17721
17722 *slot = entry;
17723 entry->index_offset = obstack_object_size (cpool);
17724
17725 obstack_grow (cpool, &val, sizeof (val));
17726 for (i = 0;
17727 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17728 ++i)
17729 {
17730 val = MAYBE_SWAP (iter);
17731 obstack_grow (cpool, &val, sizeof (val));
17732 }
17733 }
17734 else
17735 {
17736 struct symtab_index_entry *old_entry = *slot;
17737 entry->index_offset = old_entry->index_offset;
17738 entry = old_entry;
17739 }
17740 return entry->index_offset;
17741 }
17742
17743 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17744 constant pool entries going into the obstack CPOOL. */
17745
17746 static void
17747 write_hash_table (struct mapped_symtab *symtab,
17748 struct obstack *output, struct obstack *cpool)
17749 {
17750 offset_type i;
17751 htab_t symbol_hash_table;
17752 htab_t str_table;
17753
17754 symbol_hash_table = create_symbol_hash_table ();
17755 str_table = create_strtab ();
17756
17757 /* We add all the index vectors to the constant pool first, to
17758 ensure alignment is ok. */
17759 for (i = 0; i < symtab->size; ++i)
17760 {
17761 if (symtab->data[i])
17762 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17763 }
17764
17765 /* Now write out the hash table. */
17766 for (i = 0; i < symtab->size; ++i)
17767 {
17768 offset_type str_off, vec_off;
17769
17770 if (symtab->data[i])
17771 {
17772 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17773 vec_off = symtab->data[i]->index_offset;
17774 }
17775 else
17776 {
17777 /* While 0 is a valid constant pool index, it is not valid
17778 to have 0 for both offsets. */
17779 str_off = 0;
17780 vec_off = 0;
17781 }
17782
17783 str_off = MAYBE_SWAP (str_off);
17784 vec_off = MAYBE_SWAP (vec_off);
17785
17786 obstack_grow (output, &str_off, sizeof (str_off));
17787 obstack_grow (output, &vec_off, sizeof (vec_off));
17788 }
17789
17790 htab_delete (str_table);
17791 htab_delete (symbol_hash_table);
17792 }
17793
17794 /* Struct to map psymtab to CU index in the index file. */
17795 struct psymtab_cu_index_map
17796 {
17797 struct partial_symtab *psymtab;
17798 unsigned int cu_index;
17799 };
17800
17801 static hashval_t
17802 hash_psymtab_cu_index (const void *item)
17803 {
17804 const struct psymtab_cu_index_map *map = item;
17805
17806 return htab_hash_pointer (map->psymtab);
17807 }
17808
17809 static int
17810 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17811 {
17812 const struct psymtab_cu_index_map *lhs = item_lhs;
17813 const struct psymtab_cu_index_map *rhs = item_rhs;
17814
17815 return lhs->psymtab == rhs->psymtab;
17816 }
17817
17818 /* Helper struct for building the address table. */
17819 struct addrmap_index_data
17820 {
17821 struct objfile *objfile;
17822 struct obstack *addr_obstack;
17823 htab_t cu_index_htab;
17824
17825 /* Non-zero if the previous_* fields are valid.
17826 We can't write an entry until we see the next entry (since it is only then
17827 that we know the end of the entry). */
17828 int previous_valid;
17829 /* Index of the CU in the table of all CUs in the index file. */
17830 unsigned int previous_cu_index;
17831 /* Start address of the CU. */
17832 CORE_ADDR previous_cu_start;
17833 };
17834
17835 /* Write an address entry to OBSTACK. */
17836
17837 static void
17838 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17839 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17840 {
17841 offset_type cu_index_to_write;
17842 char addr[8];
17843 CORE_ADDR baseaddr;
17844
17845 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17846
17847 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17848 obstack_grow (obstack, addr, 8);
17849 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17850 obstack_grow (obstack, addr, 8);
17851 cu_index_to_write = MAYBE_SWAP (cu_index);
17852 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17853 }
17854
17855 /* Worker function for traversing an addrmap to build the address table. */
17856
17857 static int
17858 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17859 {
17860 struct addrmap_index_data *data = datap;
17861 struct partial_symtab *pst = obj;
17862
17863 if (data->previous_valid)
17864 add_address_entry (data->objfile, data->addr_obstack,
17865 data->previous_cu_start, start_addr,
17866 data->previous_cu_index);
17867
17868 data->previous_cu_start = start_addr;
17869 if (pst != NULL)
17870 {
17871 struct psymtab_cu_index_map find_map, *map;
17872 find_map.psymtab = pst;
17873 map = htab_find (data->cu_index_htab, &find_map);
17874 gdb_assert (map != NULL);
17875 data->previous_cu_index = map->cu_index;
17876 data->previous_valid = 1;
17877 }
17878 else
17879 data->previous_valid = 0;
17880
17881 return 0;
17882 }
17883
17884 /* Write OBJFILE's address map to OBSTACK.
17885 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17886 in the index file. */
17887
17888 static void
17889 write_address_map (struct objfile *objfile, struct obstack *obstack,
17890 htab_t cu_index_htab)
17891 {
17892 struct addrmap_index_data addrmap_index_data;
17893
17894 /* When writing the address table, we have to cope with the fact that
17895 the addrmap iterator only provides the start of a region; we have to
17896 wait until the next invocation to get the start of the next region. */
17897
17898 addrmap_index_data.objfile = objfile;
17899 addrmap_index_data.addr_obstack = obstack;
17900 addrmap_index_data.cu_index_htab = cu_index_htab;
17901 addrmap_index_data.previous_valid = 0;
17902
17903 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17904 &addrmap_index_data);
17905
17906 /* It's highly unlikely the last entry (end address = 0xff...ff)
17907 is valid, but we should still handle it.
17908 The end address is recorded as the start of the next region, but that
17909 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17910 anyway. */
17911 if (addrmap_index_data.previous_valid)
17912 add_address_entry (objfile, obstack,
17913 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17914 addrmap_index_data.previous_cu_index);
17915 }
17916
17917 /* Return the symbol kind of PSYM. */
17918
17919 static gdb_index_symbol_kind
17920 symbol_kind (struct partial_symbol *psym)
17921 {
17922 domain_enum domain = PSYMBOL_DOMAIN (psym);
17923 enum address_class aclass = PSYMBOL_CLASS (psym);
17924
17925 switch (domain)
17926 {
17927 case VAR_DOMAIN:
17928 switch (aclass)
17929 {
17930 case LOC_BLOCK:
17931 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
17932 case LOC_TYPEDEF:
17933 return GDB_INDEX_SYMBOL_KIND_TYPE;
17934 case LOC_COMPUTED:
17935 case LOC_CONST_BYTES:
17936 case LOC_OPTIMIZED_OUT:
17937 case LOC_STATIC:
17938 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17939 case LOC_CONST:
17940 /* Note: It's currently impossible to recognize psyms as enum values
17941 short of reading the type info. For now punt. */
17942 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17943 default:
17944 /* There are other LOC_FOO values that one might want to classify
17945 as variables, but dwarf2read.c doesn't currently use them. */
17946 return GDB_INDEX_SYMBOL_KIND_OTHER;
17947 }
17948 case STRUCT_DOMAIN:
17949 return GDB_INDEX_SYMBOL_KIND_TYPE;
17950 default:
17951 return GDB_INDEX_SYMBOL_KIND_OTHER;
17952 }
17953 }
17954
17955 /* Add a list of partial symbols to SYMTAB. */
17956
17957 static void
17958 write_psymbols (struct mapped_symtab *symtab,
17959 htab_t psyms_seen,
17960 struct partial_symbol **psymp,
17961 int count,
17962 offset_type cu_index,
17963 int is_static)
17964 {
17965 for (; count-- > 0; ++psymp)
17966 {
17967 struct partial_symbol *psym = *psymp;
17968 void **slot;
17969
17970 if (SYMBOL_LANGUAGE (psym) == language_ada)
17971 error (_("Ada is not currently supported by the index"));
17972
17973 /* Only add a given psymbol once. */
17974 slot = htab_find_slot (psyms_seen, psym, INSERT);
17975 if (!*slot)
17976 {
17977 gdb_index_symbol_kind kind = symbol_kind (psym);
17978
17979 *slot = psym;
17980 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
17981 is_static, kind, cu_index);
17982 }
17983 }
17984 }
17985
17986 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17987 exception if there is an error. */
17988
17989 static void
17990 write_obstack (FILE *file, struct obstack *obstack)
17991 {
17992 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17993 file)
17994 != obstack_object_size (obstack))
17995 error (_("couldn't data write to file"));
17996 }
17997
17998 /* Unlink a file if the argument is not NULL. */
17999
18000 static void
18001 unlink_if_set (void *p)
18002 {
18003 char **filename = p;
18004 if (*filename)
18005 unlink (*filename);
18006 }
18007
18008 /* A helper struct used when iterating over debug_types. */
18009 struct signatured_type_index_data
18010 {
18011 struct objfile *objfile;
18012 struct mapped_symtab *symtab;
18013 struct obstack *types_list;
18014 htab_t psyms_seen;
18015 int cu_index;
18016 };
18017
18018 /* A helper function that writes a single signatured_type to an
18019 obstack. */
18020
18021 static int
18022 write_one_signatured_type (void **slot, void *d)
18023 {
18024 struct signatured_type_index_data *info = d;
18025 struct signatured_type *entry = (struct signatured_type *) *slot;
18026 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18027 struct partial_symtab *psymtab = per_cu->v.psymtab;
18028 gdb_byte val[8];
18029
18030 write_psymbols (info->symtab,
18031 info->psyms_seen,
18032 info->objfile->global_psymbols.list
18033 + psymtab->globals_offset,
18034 psymtab->n_global_syms, info->cu_index,
18035 0);
18036 write_psymbols (info->symtab,
18037 info->psyms_seen,
18038 info->objfile->static_psymbols.list
18039 + psymtab->statics_offset,
18040 psymtab->n_static_syms, info->cu_index,
18041 1);
18042
18043 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18044 entry->per_cu.offset.sect_off);
18045 obstack_grow (info->types_list, val, 8);
18046 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18047 entry->type_offset_in_tu.cu_off);
18048 obstack_grow (info->types_list, val, 8);
18049 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18050 obstack_grow (info->types_list, val, 8);
18051
18052 ++info->cu_index;
18053
18054 return 1;
18055 }
18056
18057 /* Recurse into all "included" dependencies and write their symbols as
18058 if they appeared in this psymtab. */
18059
18060 static void
18061 recursively_write_psymbols (struct objfile *objfile,
18062 struct partial_symtab *psymtab,
18063 struct mapped_symtab *symtab,
18064 htab_t psyms_seen,
18065 offset_type cu_index)
18066 {
18067 int i;
18068
18069 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18070 if (psymtab->dependencies[i]->user != NULL)
18071 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18072 symtab, psyms_seen, cu_index);
18073
18074 write_psymbols (symtab,
18075 psyms_seen,
18076 objfile->global_psymbols.list + psymtab->globals_offset,
18077 psymtab->n_global_syms, cu_index,
18078 0);
18079 write_psymbols (symtab,
18080 psyms_seen,
18081 objfile->static_psymbols.list + psymtab->statics_offset,
18082 psymtab->n_static_syms, cu_index,
18083 1);
18084 }
18085
18086 /* Create an index file for OBJFILE in the directory DIR. */
18087
18088 static void
18089 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18090 {
18091 struct cleanup *cleanup;
18092 char *filename, *cleanup_filename;
18093 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18094 struct obstack cu_list, types_cu_list;
18095 int i;
18096 FILE *out_file;
18097 struct mapped_symtab *symtab;
18098 offset_type val, size_of_contents, total_len;
18099 struct stat st;
18100 htab_t psyms_seen;
18101 htab_t cu_index_htab;
18102 struct psymtab_cu_index_map *psymtab_cu_index_map;
18103
18104 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18105 return;
18106
18107 if (dwarf2_per_objfile->using_index)
18108 error (_("Cannot use an index to create the index"));
18109
18110 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18111 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18112
18113 if (stat (objfile->name, &st) < 0)
18114 perror_with_name (objfile->name);
18115
18116 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18117 INDEX_SUFFIX, (char *) NULL);
18118 cleanup = make_cleanup (xfree, filename);
18119
18120 out_file = fopen (filename, "wb");
18121 if (!out_file)
18122 error (_("Can't open `%s' for writing"), filename);
18123
18124 cleanup_filename = filename;
18125 make_cleanup (unlink_if_set, &cleanup_filename);
18126
18127 symtab = create_mapped_symtab ();
18128 make_cleanup (cleanup_mapped_symtab, symtab);
18129
18130 obstack_init (&addr_obstack);
18131 make_cleanup_obstack_free (&addr_obstack);
18132
18133 obstack_init (&cu_list);
18134 make_cleanup_obstack_free (&cu_list);
18135
18136 obstack_init (&types_cu_list);
18137 make_cleanup_obstack_free (&types_cu_list);
18138
18139 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18140 NULL, xcalloc, xfree);
18141 make_cleanup_htab_delete (psyms_seen);
18142
18143 /* While we're scanning CU's create a table that maps a psymtab pointer
18144 (which is what addrmap records) to its index (which is what is recorded
18145 in the index file). This will later be needed to write the address
18146 table. */
18147 cu_index_htab = htab_create_alloc (100,
18148 hash_psymtab_cu_index,
18149 eq_psymtab_cu_index,
18150 NULL, xcalloc, xfree);
18151 make_cleanup_htab_delete (cu_index_htab);
18152 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18153 xmalloc (sizeof (struct psymtab_cu_index_map)
18154 * dwarf2_per_objfile->n_comp_units);
18155 make_cleanup (xfree, psymtab_cu_index_map);
18156
18157 /* The CU list is already sorted, so we don't need to do additional
18158 work here. Also, the debug_types entries do not appear in
18159 all_comp_units, but only in their own hash table. */
18160 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18161 {
18162 struct dwarf2_per_cu_data *per_cu
18163 = dwarf2_per_objfile->all_comp_units[i];
18164 struct partial_symtab *psymtab = per_cu->v.psymtab;
18165 gdb_byte val[8];
18166 struct psymtab_cu_index_map *map;
18167 void **slot;
18168
18169 if (psymtab->user == NULL)
18170 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
18171
18172 map = &psymtab_cu_index_map[i];
18173 map->psymtab = psymtab;
18174 map->cu_index = i;
18175 slot = htab_find_slot (cu_index_htab, map, INSERT);
18176 gdb_assert (slot != NULL);
18177 gdb_assert (*slot == NULL);
18178 *slot = map;
18179
18180 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18181 per_cu->offset.sect_off);
18182 obstack_grow (&cu_list, val, 8);
18183 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18184 obstack_grow (&cu_list, val, 8);
18185 }
18186
18187 /* Dump the address map. */
18188 write_address_map (objfile, &addr_obstack, cu_index_htab);
18189
18190 /* Write out the .debug_type entries, if any. */
18191 if (dwarf2_per_objfile->signatured_types)
18192 {
18193 struct signatured_type_index_data sig_data;
18194
18195 sig_data.objfile = objfile;
18196 sig_data.symtab = symtab;
18197 sig_data.types_list = &types_cu_list;
18198 sig_data.psyms_seen = psyms_seen;
18199 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18200 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18201 write_one_signatured_type, &sig_data);
18202 }
18203
18204 /* Now that we've processed all symbols we can shrink their cu_indices
18205 lists. */
18206 uniquify_cu_indices (symtab);
18207
18208 obstack_init (&constant_pool);
18209 make_cleanup_obstack_free (&constant_pool);
18210 obstack_init (&symtab_obstack);
18211 make_cleanup_obstack_free (&symtab_obstack);
18212 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18213
18214 obstack_init (&contents);
18215 make_cleanup_obstack_free (&contents);
18216 size_of_contents = 6 * sizeof (offset_type);
18217 total_len = size_of_contents;
18218
18219 /* The version number. */
18220 val = MAYBE_SWAP (7);
18221 obstack_grow (&contents, &val, sizeof (val));
18222
18223 /* The offset of the CU list from the start of the file. */
18224 val = MAYBE_SWAP (total_len);
18225 obstack_grow (&contents, &val, sizeof (val));
18226 total_len += obstack_object_size (&cu_list);
18227
18228 /* The offset of the types CU list from the start of the file. */
18229 val = MAYBE_SWAP (total_len);
18230 obstack_grow (&contents, &val, sizeof (val));
18231 total_len += obstack_object_size (&types_cu_list);
18232
18233 /* The offset of the address table from the start of the file. */
18234 val = MAYBE_SWAP (total_len);
18235 obstack_grow (&contents, &val, sizeof (val));
18236 total_len += obstack_object_size (&addr_obstack);
18237
18238 /* The offset of the symbol table from the start of the file. */
18239 val = MAYBE_SWAP (total_len);
18240 obstack_grow (&contents, &val, sizeof (val));
18241 total_len += obstack_object_size (&symtab_obstack);
18242
18243 /* The offset of the constant pool from the start of the file. */
18244 val = MAYBE_SWAP (total_len);
18245 obstack_grow (&contents, &val, sizeof (val));
18246 total_len += obstack_object_size (&constant_pool);
18247
18248 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18249
18250 write_obstack (out_file, &contents);
18251 write_obstack (out_file, &cu_list);
18252 write_obstack (out_file, &types_cu_list);
18253 write_obstack (out_file, &addr_obstack);
18254 write_obstack (out_file, &symtab_obstack);
18255 write_obstack (out_file, &constant_pool);
18256
18257 fclose (out_file);
18258
18259 /* We want to keep the file, so we set cleanup_filename to NULL
18260 here. See unlink_if_set. */
18261 cleanup_filename = NULL;
18262
18263 do_cleanups (cleanup);
18264 }
18265
18266 /* Implementation of the `save gdb-index' command.
18267
18268 Note that the file format used by this command is documented in the
18269 GDB manual. Any changes here must be documented there. */
18270
18271 static void
18272 save_gdb_index_command (char *arg, int from_tty)
18273 {
18274 struct objfile *objfile;
18275
18276 if (!arg || !*arg)
18277 error (_("usage: save gdb-index DIRECTORY"));
18278
18279 ALL_OBJFILES (objfile)
18280 {
18281 struct stat st;
18282
18283 /* If the objfile does not correspond to an actual file, skip it. */
18284 if (stat (objfile->name, &st) < 0)
18285 continue;
18286
18287 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18288 if (dwarf2_per_objfile)
18289 {
18290 volatile struct gdb_exception except;
18291
18292 TRY_CATCH (except, RETURN_MASK_ERROR)
18293 {
18294 write_psymtabs_to_index (objfile, arg);
18295 }
18296 if (except.reason < 0)
18297 exception_fprintf (gdb_stderr, except,
18298 _("Error while writing index for `%s': "),
18299 objfile->name);
18300 }
18301 }
18302 }
18303
18304 \f
18305
18306 int dwarf2_always_disassemble;
18307
18308 static void
18309 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18310 struct cmd_list_element *c, const char *value)
18311 {
18312 fprintf_filtered (file,
18313 _("Whether to always disassemble "
18314 "DWARF expressions is %s.\n"),
18315 value);
18316 }
18317
18318 static void
18319 show_check_physname (struct ui_file *file, int from_tty,
18320 struct cmd_list_element *c, const char *value)
18321 {
18322 fprintf_filtered (file,
18323 _("Whether to check \"physname\" is %s.\n"),
18324 value);
18325 }
18326
18327 void _initialize_dwarf2_read (void);
18328
18329 void
18330 _initialize_dwarf2_read (void)
18331 {
18332 struct cmd_list_element *c;
18333
18334 dwarf2_objfile_data_key
18335 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18336
18337 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18338 Set DWARF 2 specific variables.\n\
18339 Configure DWARF 2 variables such as the cache size"),
18340 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18341 0/*allow-unknown*/, &maintenance_set_cmdlist);
18342
18343 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18344 Show DWARF 2 specific variables\n\
18345 Show DWARF 2 variables such as the cache size"),
18346 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18347 0/*allow-unknown*/, &maintenance_show_cmdlist);
18348
18349 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18350 &dwarf2_max_cache_age, _("\
18351 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18352 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18353 A higher limit means that cached compilation units will be stored\n\
18354 in memory longer, and more total memory will be used. Zero disables\n\
18355 caching, which can slow down startup."),
18356 NULL,
18357 show_dwarf2_max_cache_age,
18358 &set_dwarf2_cmdlist,
18359 &show_dwarf2_cmdlist);
18360
18361 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18362 &dwarf2_always_disassemble, _("\
18363 Set whether `info address' always disassembles DWARF expressions."), _("\
18364 Show whether `info address' always disassembles DWARF expressions."), _("\
18365 When enabled, DWARF expressions are always printed in an assembly-like\n\
18366 syntax. When disabled, expressions will be printed in a more\n\
18367 conversational style, when possible."),
18368 NULL,
18369 show_dwarf2_always_disassemble,
18370 &set_dwarf2_cmdlist,
18371 &show_dwarf2_cmdlist);
18372
18373 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18374 Set debugging of the dwarf2 reader."), _("\
18375 Show debugging of the dwarf2 reader."), _("\
18376 When enabled, debugging messages are printed during dwarf2 reading\n\
18377 and symtab expansion."),
18378 NULL,
18379 NULL,
18380 &setdebuglist, &showdebuglist);
18381
18382 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18383 Set debugging of the dwarf2 DIE reader."), _("\
18384 Show debugging of the dwarf2 DIE reader."), _("\
18385 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18386 The value is the maximum depth to print."),
18387 NULL,
18388 NULL,
18389 &setdebuglist, &showdebuglist);
18390
18391 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18392 Set cross-checking of \"physname\" code against demangler."), _("\
18393 Show cross-checking of \"physname\" code against demangler."), _("\
18394 When enabled, GDB's internal \"physname\" code is checked against\n\
18395 the demangler."),
18396 NULL, show_check_physname,
18397 &setdebuglist, &showdebuglist);
18398
18399 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18400 _("\
18401 Save a gdb-index file.\n\
18402 Usage: save gdb-index DIRECTORY"),
18403 &save_cmdlist);
18404 set_cmd_completer (c, filename_completer);
18405 }
This page took 0.516319 seconds and 5 git commands to generate.